@@ -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,14 +166,26 @@ 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
173185 unsafe {
174186 slice:: from_raw_parts (
175187 self . bytes . as_ptr ( ) as * const [ u8 ; 4 ] ,
176- self . width as usize * self . height as usize ,
188+ self . pixel_amount ( ) ,
177189 )
178190 }
179191 }
@@ -185,7 +197,7 @@ impl Image {
185197 unsafe {
186198 slice:: from_raw_parts_mut (
187199 self . bytes . as_mut_ptr ( ) as * mut [ u8 ; 4 ] ,
188- self . width as usize * self . height as usize ,
200+ self . pixel_amount ( ) ,
189201 )
190202 }
191203 }
@@ -233,10 +245,7 @@ impl Image {
233245 /// Blends this image with another image (of identical dimensions)
234246 /// Inspired by OpenCV saturated blending
235247 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- ) ;
248+ self . assert_same_size ( other) ;
240249
241250 for i in 0 ..self . bytes . len ( ) / 4 {
242251 let c1: Color = Color {
@@ -269,10 +278,7 @@ impl Image {
269278 /// overlaying a completely transparent image has no effect
270279 /// on the original image, though blending them would.
271280 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- ) ;
281+ self . assert_same_size ( other) ;
276282
277283 for i in 0 ..self . bytes . len ( ) / 4 {
278284 let c1: Color = Color {
0 commit comments