@@ -280,6 +280,16 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
280
280
Some ( sub_image_data)
281
281
}
282
282
TextureFormat :: Rgba32Float => {
283
+ let image_data_u8 = image
284
+ . data
285
+ . chunks ( 4 )
286
+ . map ( |chunk| {
287
+ let chunk = chunk. try_into ( ) . unwrap ( ) ;
288
+ let num = bytemuck:: cast_ref :: < [ u8 ; 4 ] , f32 > ( chunk) ;
289
+ ( num * 255.0 ) as u8
290
+ } )
291
+ . collect :: < Vec < u8 > > ( ) ;
292
+
283
293
for y in 0 ..height {
284
294
for x in 0 ..width {
285
295
let src_x = if flip_x { width - 1 - x } else { x } ;
@@ -288,9 +298,7 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
288
298
* image. texture_descriptor . size . width as usize
289
299
+ ( rect. min . x as usize + src_x) )
290
300
* 4 ;
291
- let chunk = & image. data [ index..index + 4 ] ;
292
- let num = bytemuck:: cast_slice :: < u8 , f32 > ( chunk) [ 0 ] ;
293
- sub_image_data. push ( ( num * 255.0 ) as u8 ) ;
301
+ sub_image_data. extend_from_slice ( & image_data_u8[ index..index + 4 ] ) ;
294
302
}
295
303
}
296
304
Some ( sub_image_data)
@@ -324,6 +332,30 @@ mod tests {
324
332
)
325
333
}
326
334
335
+ fn create_image_rgba32float ( data : & [ u8 ] ) -> Image {
336
+ let float_data: Vec < f32 > = data
337
+ . chunks ( 4 )
338
+ . flat_map ( |chunk| {
339
+ chunk
340
+ . iter ( )
341
+ . map ( |& x| x as f32 / 255.0 ) // convert each channel to f32
342
+ . collect :: < Vec < f32 > > ( )
343
+ } )
344
+ . collect ( ) ;
345
+
346
+ Image :: new (
347
+ Extent3d {
348
+ width : 3 ,
349
+ height : 3 ,
350
+ depth_or_array_layers : 1 ,
351
+ } ,
352
+ TextureDimension :: D2 ,
353
+ bytemuck:: cast_slice ( & float_data) . to_vec ( ) ,
354
+ TextureFormat :: Rgba32Float ,
355
+ RenderAssetUsages :: default ( ) ,
356
+ )
357
+ }
358
+
327
359
macro_rules! test_image_to_rgba_pixels {
328
360
( $name: ident, $flip_x: expr, $flip_y: expr, $rect: expr, $expected: expr) => {
329
361
#[ test]
@@ -350,6 +382,14 @@ mod tests {
350
382
let result = image_to_rgba_pixels( & image, $flip_x, $flip_y, rect) ;
351
383
assert_eq!( result, Some ( $expected. to_vec( ) ) ) ;
352
384
}
385
+
386
+ // RGBA32Float test
387
+ {
388
+ let image = create_image_rgba32float( image_data) ;
389
+ let rect = $rect;
390
+ let result = image_to_rgba_pixels( & image, $flip_x, $flip_y, rect) ;
391
+ assert_eq!( result, Some ( $expected. to_vec( ) ) ) ;
392
+ }
353
393
}
354
394
} ;
355
395
}
0 commit comments