Skip to content

Commit 2f77a85

Browse files
committed
refactor image_to_rgba_pixels
1 parent f96303c commit 2f77a85

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

crates/bevy_winit/src/cursor.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,16 @@ fn on_remove_cursor_icon(trigger: Trigger<OnRemove, CursorIcon>, mut commands: C
256256
///
257257
/// Only supports rgba8 and rgba32float formats.
258258
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>;
262260

263-
match image.texture_descriptor.format {
261+
let image_data = match image.texture_descriptor.format {
264262
TextureFormat::Rgba8Unorm
265263
| TextureFormat::Rgba8UnormSrgb
266264
| TextureFormat::Rgba8Snorm
267265
| 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),
282267
TextureFormat::Rgba32Float => {
283-
let image_data_u8 = image
268+
image_data_as_u8s = image
284269
.data
285270
.chunks(4)
286271
.map(|chunk| {
@@ -290,21 +275,30 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
290275
})
291276
.collect::<Vec<u8>>();
292277

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)
305279
}
306280
_ => 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+
}
307299
}
300+
301+
Some(sub_image_data)
308302
}
309303

310304
#[cfg(feature = "custom_cursor")]

0 commit comments

Comments
 (0)