Skip to content

Commit c6ba500

Browse files
committed
refactor image_to_rgba_pixels
1 parent 2a24a03 commit c6ba500

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
@@ -251,31 +251,16 @@ fn on_remove_cursor_icon(trigger: Trigger<OnRemove, CursorIcon>, mut commands: C
251251
///
252252
/// Only supports rgba8 and rgba32float formats.
253253
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>;
257255

258-
match image.texture_descriptor.format {
256+
let image_data = match image.texture_descriptor.format {
259257
TextureFormat::Rgba8Unorm
260258
| TextureFormat::Rgba8UnormSrgb
261259
| TextureFormat::Rgba8Snorm
262260
| 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),
277262
TextureFormat::Rgba32Float => {
278-
let image_data_u8 = image
263+
image_data_as_u8s = image
279264
.data
280265
.chunks(4)
281266
.map(|chunk| {
@@ -285,21 +270,30 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
285270
})
286271
.collect::<Vec<u8>>();
287272

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)
300274
}
301275
_ => 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+
}
302294
}
295+
296+
Some(sub_image_data)
303297
}
304298

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

0 commit comments

Comments
 (0)