@@ -146,7 +146,7 @@ impl Image {
146
146
147
147
/// Updates this image from a slice of [Color]s.
148
148
pub fn update ( & mut self , colors : & [ Color ] ) {
149
- assert ! ( self . width as usize * self . height as usize == colors. len( ) ) ;
149
+ assert_eq ! ( self . pixel_amount ( ) , colors. len( ) ) ;
150
150
151
151
for i in 0 ..colors. len ( ) {
152
152
self . bytes [ i * 4 ] = ( colors[ i] . r * 255. ) as u8 ;
@@ -166,27 +166,31 @@ impl Image {
166
166
self . height as usize
167
167
}
168
168
169
+ /// Returns the amount of pixels this image has according to its dimensions.
170
+ pub const fn pixel_amount ( & self ) -> usize {
171
+ self . width as usize * self . height as usize
172
+ }
173
+
174
+ fn assert_same_size ( & self , other : & Self ) {
175
+ assert ! (
176
+ self . width == other. width && self . height == other. height,
177
+ "images have different sizes"
178
+ ) ;
179
+ }
180
+
169
181
/// Returns this image's data as a slice of 4-byte arrays.
170
182
pub fn get_image_data ( & self ) -> & [ [ u8 ; 4 ] ] {
171
183
use std:: slice;
172
184
173
- unsafe {
174
- slice:: from_raw_parts (
175
- self . bytes . as_ptr ( ) as * const [ u8 ; 4 ] ,
176
- self . width as usize * self . height as usize ,
177
- )
178
- }
185
+ unsafe { slice:: from_raw_parts ( self . bytes . as_ptr ( ) as * const [ u8 ; 4 ] , self . pixel_amount ( ) ) }
179
186
}
180
187
181
188
/// Returns this image's data as a mutable slice of 4-byte arrays.
182
189
pub fn get_image_data_mut ( & mut self ) -> & mut [ [ u8 ; 4 ] ] {
183
190
use std:: slice;
184
191
185
192
unsafe {
186
- slice:: from_raw_parts_mut (
187
- self . bytes . as_mut_ptr ( ) as * mut [ u8 ; 4 ] ,
188
- self . width as usize * self . height as usize ,
189
- )
193
+ slice:: from_raw_parts_mut ( self . bytes . as_mut_ptr ( ) as * mut [ u8 ; 4 ] , self . pixel_amount ( ) )
190
194
}
191
195
}
192
196
@@ -233,10 +237,7 @@ impl Image {
233
237
/// Blends this image with another image (of identical dimensions)
234
238
/// Inspired by OpenCV saturated blending
235
239
pub fn blend ( & mut self , other : & Image ) {
236
- assert ! (
237
- self . width as usize * self . height as usize
238
- == other. width as usize * other. height as usize
239
- ) ;
240
+ self . assert_same_size ( other) ;
240
241
241
242
for i in 0 ..self . bytes . len ( ) / 4 {
242
243
let c1: Color = Color {
@@ -269,10 +270,7 @@ impl Image {
269
270
/// overlaying a completely transparent image has no effect
270
271
/// on the original image, though blending them would.
271
272
pub fn overlay ( & mut self , other : & Image ) {
272
- assert ! (
273
- self . width as usize * self . height as usize
274
- == other. width as usize * other. height as usize
275
- ) ;
273
+ self . assert_same_size ( other) ;
276
274
277
275
for i in 0 ..self . bytes . len ( ) / 4 {
278
276
let c1: Color = Color {
0 commit comments