@@ -251,31 +251,16 @@ fn on_remove_cursor_icon(trigger: Trigger<OnRemove, CursorIcon>, mut commands: C
251
251
///
252
252
/// Only supports rgba8 and rgba32float formats.
253
253
fn image_to_rgba_pixels ( image : & Image , flip_x : bool , flip_y : bool , rect : Rect ) -> Option < Vec < u8 > > {
254
- let width = ( rect. max . x - rect. min . x ) as usize ;
255
- let height = ( rect. max . y - rect. min . y ) as usize ;
256
- let mut sub_image_data = Vec :: with_capacity ( width * height * 4 ) ; // Assuming 4 bytes per pixel (RGBA8)
254
+ let image_data_as_u8s: Vec < u8 > ;
257
255
258
- match image. texture_descriptor . format {
256
+ let image_data = match image. texture_descriptor . format {
259
257
TextureFormat :: Rgba8Unorm
260
258
| TextureFormat :: Rgba8UnormSrgb
261
259
| TextureFormat :: Rgba8Snorm
262
260
| TextureFormat :: Rgba8Uint
263
- | TextureFormat :: Rgba8Sint => {
264
- for y in 0 ..height {
265
- for x in 0 ..width {
266
- let src_x = if flip_x { width - 1 - x } else { x } ;
267
- let src_y = if flip_y { height - 1 - y } else { y } ;
268
- let index = ( ( rect. min . y as usize + src_y)
269
- * image. texture_descriptor . size . width as usize
270
- + ( rect. min . x as usize + src_x) )
271
- * 4 ;
272
- sub_image_data. extend_from_slice ( & image. data [ index..index + 4 ] ) ;
273
- }
274
- }
275
- Some ( sub_image_data)
276
- }
261
+ | TextureFormat :: Rgba8Sint => Some ( & image. data ) ,
277
262
TextureFormat :: Rgba32Float => {
278
- let image_data_u8 = image
263
+ image_data_as_u8s = image
279
264
. data
280
265
. chunks ( 4 )
281
266
. map ( |chunk| {
@@ -285,21 +270,30 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
285
270
} )
286
271
. collect :: < Vec < u8 > > ( ) ;
287
272
288
- for y in 0 ..height {
289
- for x in 0 ..width {
290
- let src_x = if flip_x { width - 1 - x } else { x } ;
291
- let src_y = if flip_y { height - 1 - y } else { y } ;
292
- let index = ( ( rect. min . y as usize + src_y)
293
- * image. texture_descriptor . size . width as usize
294
- + ( rect. min . x as usize + src_x) )
295
- * 4 ;
296
- sub_image_data. extend_from_slice ( & image_data_u8[ index..index + 4 ] ) ;
297
- }
298
- }
299
- Some ( sub_image_data)
273
+ Some ( & image_data_as_u8s)
300
274
}
301
275
_ => None ,
276
+ } ;
277
+
278
+ let image_data = image_data?;
279
+
280
+ let width = ( rect. max . x - rect. min . x ) as usize ;
281
+ let height = ( rect. max . y - rect. min . y ) as usize ;
282
+ let mut sub_image_data = Vec :: with_capacity ( width * height * 4 ) ; // assuming 4 bytes per pixel (RGBA8)
283
+
284
+ for y in 0 ..height {
285
+ for x in 0 ..width {
286
+ let src_x = if flip_x { width - 1 - x } else { x } ;
287
+ let src_y = if flip_y { height - 1 - y } else { y } ;
288
+ let index = ( ( rect. min . y as usize + src_y)
289
+ * image. texture_descriptor . size . width as usize
290
+ + ( rect. min . x as usize + src_x) )
291
+ * 4 ;
292
+ sub_image_data. extend_from_slice ( & image_data[ index..index + 4 ] ) ;
293
+ }
302
294
}
295
+
296
+ Some ( sub_image_data)
303
297
}
304
298
305
299
#[ cfg( feature = "custom_cursor" ) ]
0 commit comments