@@ -146,7 +146,7 @@ impl Image {
146146
147147 /// Updates this image from a slice of [Color]s.
148148 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( ) ) ;
150150
151151 for i in 0 ..colors. len ( ) {
152152 self . bytes [ i * 4 ] = ( colors[ i] . r * 255. ) as u8 ;
@@ -166,27 +166,31 @@ impl Image {
166166 self . height as usize
167167 }
168168
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+
169181 /// Returns this image's data as a slice of 4-byte arrays.
170182 pub fn get_image_data ( & self ) -> & [ [ u8 ; 4 ] ] {
171183 use std:: slice;
172184
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 ( ) ) }
179186 }
180187
181188 /// Returns this image's data as a mutable slice of 4-byte arrays.
182189 pub fn get_image_data_mut ( & mut self ) -> & mut [ [ u8 ; 4 ] ] {
183190 use std:: slice;
184191
185192 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 ( ) )
190194 }
191195 }
192196
@@ -233,10 +237,7 @@ impl Image {
233237 /// Blends this image with another image (of identical dimensions)
234238 /// Inspired by OpenCV saturated blending
235239 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) ;
240241
241242 for i in 0 ..self . bytes . len ( ) / 4 {
242243 let c1: Color = Color {
@@ -269,10 +270,7 @@ impl Image {
269270 /// overlaying a completely transparent image has no effect
270271 /// on the original image, though blending them would.
271272 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) ;
276274
277275 for i in 0 ..self . bytes . len ( ) / 4 {
278276 let c1: Color = Color {
0 commit comments