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