Skip to content

add a few simple helpers to Image #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Image {

/// Updates this image from a slice of [Color]s.
pub fn update(&mut self, colors: &[Color]) {
assert!(self.width as usize * self.height as usize == colors.len());
assert_eq!(self.pixel_amount(), colors.len());

for i in 0..colors.len() {
self.bytes[i * 4] = (colors[i].r * 255.) as u8;
Expand All @@ -166,27 +166,31 @@ impl Image {
self.height as usize
}

/// Returns the amount of pixels this image has according to its dimensions.
pub const fn pixel_amount(&self) -> usize {
self.width as usize * self.height as usize
}

fn assert_same_size(&self, other: &Self) {
assert!(
self.width == other.width && self.height == other.height,
"images have different sizes"
);
}

/// Returns this image's data as a slice of 4-byte arrays.
pub fn get_image_data(&self) -> &[[u8; 4]] {
use std::slice;

unsafe {
slice::from_raw_parts(
self.bytes.as_ptr() as *const [u8; 4],
self.width as usize * self.height as usize,
)
}
unsafe { slice::from_raw_parts(self.bytes.as_ptr() as *const [u8; 4], self.pixel_amount()) }
}

/// Returns this image's data as a mutable slice of 4-byte arrays.
pub fn get_image_data_mut(&mut self) -> &mut [[u8; 4]] {
use std::slice;

unsafe {
slice::from_raw_parts_mut(
self.bytes.as_mut_ptr() as *mut [u8; 4],
self.width as usize * self.height as usize,
)
slice::from_raw_parts_mut(self.bytes.as_mut_ptr() as *mut [u8; 4], self.pixel_amount())
}
}

Expand Down Expand Up @@ -233,10 +237,7 @@ impl Image {
/// Blends this image with another image (of identical dimensions)
/// Inspired by OpenCV saturated blending
pub fn blend(&mut self, other: &Image) {
assert!(
self.width as usize * self.height as usize
== other.width as usize * other.height as usize
);
self.assert_same_size(other);

for i in 0..self.bytes.len() / 4 {
let c1: Color = Color {
Expand Down Expand Up @@ -269,10 +270,7 @@ impl Image {
/// overlaying a completely transparent image has no effect
/// on the original image, though blending them would.
pub fn overlay(&mut self, other: &Image) {
assert!(
self.width as usize * self.height as usize
== other.width as usize * other.height as usize
);
self.assert_same_size(other);

for i in 0..self.bytes.len() / 4 {
let c1: Color = Color {
Expand Down
Loading