Skip to content

Commit f96303c

Browse files
committed
fix RGBA32Float branch of image_to_rgba_pixels with tests
1 parent 197807b commit f96303c

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

crates/bevy_winit/src/cursor.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
280280
Some(sub_image_data)
281281
}
282282
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+
283293
for y in 0..height {
284294
for x in 0..width {
285295
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) -
288298
* image.texture_descriptor.size.width as usize
289299
+ (rect.min.x as usize + src_x))
290300
* 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]);
294302
}
295303
}
296304
Some(sub_image_data)
@@ -324,6 +332,30 @@ mod tests {
324332
)
325333
}
326334

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+
327359
macro_rules! test_image_to_rgba_pixels {
328360
($name:ident, $flip_x:expr, $flip_y:expr, $rect:expr, $expected:expr) => {
329361
#[test]
@@ -350,6 +382,14 @@ mod tests {
350382
let result = image_to_rgba_pixels(&image, $flip_x, $flip_y, rect);
351383
assert_eq!(result, Some($expected.to_vec()));
352384
}
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+
}
353393
}
354394
};
355395
}

0 commit comments

Comments
 (0)