Skip to content

Commit 0c0fbf3

Browse files
committed
Get sprite example to compile with updated image crate.
1 parent 13ba55e commit 0c0fbf3

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

core/src/types/vertex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl VertexFormat {
168168
attributes
169169
.iter()
170170
.map(|attr| {
171-
(attr.element.offset + attr.element.format.surface_desc().bits as u32 / 8)
171+
attr.element.offset + attr.element.format.surface_desc().bits as u32 / 8
172172
})
173173
.max()
174174
.expect("Vertex format cannot be empty")

texture/src/format/image.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::{pixel, MipLevels, TextureBuilder};
44

5+
use std::convert::TryFrom;
56
use std::num::NonZeroU8;
67

78
// reexport for easy usage in ImageTextureConfig
@@ -193,6 +194,26 @@ fn premultiply_alpha_2channel<P: image::Pixel<Subpixel = u8>>(pixel: &mut P) {
193194
channels_mut[0] = (channels_mut[0] as f32 * alpha).min(255.0).max(0.0) as u8;
194195
}
195196

197+
/// pixel.channel_count() must be 2
198+
fn premultiply_alpha_2channel_u16<P: image::Pixel<Subpixel = u16>>(pixel: &mut P) {
199+
let channels_mut = pixel.channels_mut();
200+
let alpha = channels_mut[1] as f32 / 255.0;
201+
channels_mut[0] = (channels_mut[0] as f32 * alpha).min(255.0).max(0.0) as u16;
202+
}
203+
204+
fn vec_16_to_vec_8(vec_u16: Vec<u16>) -> Vec<u8> {
205+
vec_u16
206+
.into_iter()
207+
.flat_map(|value: u16| {
208+
let high: u8 =
209+
TryFrom::<u16>::try_from(value & 0x00ffu16).expect("Unreachable: within u8 range");
210+
let low: u8 = TryFrom::<u16>::try_from((value >> 8) & 0x00ffu16)
211+
.expect("Unreachable: within u8 range");
212+
std::iter::once(low).chain(std::iter::once(high))
213+
})
214+
.collect::<Vec<u8>>()
215+
}
216+
196217
/// Attempts to load a Texture from an image.
197218
pub fn load_from_image<R>(
198219
mut reader: R,
@@ -217,8 +238,8 @@ where
217238
)?;
218239

219240
let (w, h, vec, format, swizzle) = match (image_format, config.repr) {
220-
(image::ImageFormat::HDR, Repr::Float) => {
221-
let decoder = image::hdr::HDRDecoder::new(reader)?;
241+
(image::ImageFormat::Hdr, Repr::Float) => {
242+
let decoder = image::hdr::HdrDecoder::new(reader)?;
222243
let metadata = decoder.metadata();
223244
let (w, h) = (metadata.width, metadata.height);
224245

@@ -238,6 +259,15 @@ where
238259
dyn_format!(R, _8, config.repr),
239260
Swizzle(Component::R, Component::R, Component::R, Component::One),
240261
),
262+
DynamicImage::ImageLuma16(img) => {
263+
let vec_u16 = img.into_vec();
264+
let vec_u8 = vec_16_to_vec_8(vec_u16);
265+
(
266+
vec_u8,
267+
dyn_format!(R, _8, config.repr),
268+
Swizzle(Component::R, Component::R, Component::R, Component::G),
269+
)
270+
}
241271
DynamicImage::ImageLumaA8(mut img) => {
242272
if config.premultiply_alpha {
243273
for pixel in img.pixels_mut() {
@@ -250,11 +280,44 @@ where
250280
Swizzle(Component::R, Component::R, Component::R, Component::G),
251281
)
252282
}
283+
DynamicImage::ImageLumaA16(mut img) => {
284+
if config.premultiply_alpha {
285+
for pixel in img.pixels_mut() {
286+
premultiply_alpha_2channel_u16(pixel);
287+
}
288+
}
289+
let vec_u16 = img.into_vec();
290+
let vec_u8 = vec_16_to_vec_8(vec_u16);
291+
(
292+
vec_u8,
293+
dyn_format!(Rg, _8, config.repr),
294+
Swizzle(Component::R, Component::R, Component::R, Component::G),
295+
)
296+
}
253297
DynamicImage::ImageRgb8(img) => (
254298
img.into_vec(),
255299
dyn_format!(Rgb, _8, config.repr),
256300
Swizzle::NO,
257301
),
302+
DynamicImage::ImageRgb16(img) => (
303+
vec_16_to_vec_8(img.into_vec()),
304+
dyn_format!(Rgb, _8, config.repr),
305+
Swizzle::NO,
306+
),
307+
DynamicImage::ImageRgba16(mut img) => {
308+
if config.premultiply_alpha {
309+
for pixel in img.pixels_mut() {
310+
premultiply_alpha_2channel_u16(pixel);
311+
}
312+
}
313+
let vec_u16 = img.into_vec();
314+
let vec_u8 = vec_16_to_vec_8(vec_u16);
315+
(
316+
vec_u8,
317+
dyn_format!(Rg, _8, config.repr),
318+
Swizzle(Component::R, Component::R, Component::R, Component::G),
319+
)
320+
}
258321
DynamicImage::ImageRgba8(mut img) => {
259322
if config.premultiply_alpha {
260323
for pixel in img.pixels_mut() {

0 commit comments

Comments
 (0)