Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit u16 pixels instead of f32 #37

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,18 @@ impl IWICBitmapSource_Impl for JXLWICBitmapFrameDecode {

fn GetPixelFormat(&self) -> windows::core::Result<GUID> {
log::trace!("JXLWICBitmapFrameDecode::GetPixelFormat");
// TODO: Support all formats

match self.pixel_format {
PixelFormat::Gray => Ok(GUID_WICPixelFormat32bppGrayFloat),
PixelFormat::Gray => Ok(GUID_WICPixelFormat16bppGray),
// WIC doesn't support Graya, but maybe can be emulated with RGBA
PixelFormat::Graya => Err(windows::core::Error::new(
WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT,
"Gray alpha image is currently not supported",
)),
PixelFormat::Rgb => Ok(GUID_WICPixelFormat96bppRGBFloat),
PixelFormat::Rgba => Ok(GUID_WICPixelFormat128bppRGBAFloat),
jxl_oxide::PixelFormat::Cmyk | jxl_oxide::PixelFormat::Cmyka => Err(
windows::core::Error::new(WINCODEC_ERR_BADIMAGE, "Cmyk is currently not supported"),
),
PixelFormat::Rgb => Ok(GUID_WICPixelFormat48bppRGB),
PixelFormat::Rgba => Ok(GUID_WICPixelFormat64bppRGBA),
PixelFormat::Cmyk => Ok(GUID_WICPixelFormat64bppCMYK),
PixelFormat::Cmyka => Ok(GUID_WICPixelFormat80bppCMYKAlpha),
}
}

Expand Down Expand Up @@ -285,23 +285,25 @@ impl IWICBitmapSource_Impl for JXLWICBitmapFrameDecode {
return Err(E_INVALIDARG.ok().unwrap_err());
}

let pbbuffer = pbbuffer as *mut u16;

let prc = unsafe { prc.as_ref().unwrap() };
log::trace!("JXLWICBitmapFrameDecode::CopyPixels::WICRect {:?}", prc);

let channels = self.frame.channels();
let channels = self.frame.channels() as i32;
let buf = self.frame.buf();

for y in prc.Y..(prc.Y + prc.Height) {
let src_offset = self.width as i32 * channels as i32 * y;
let dst_offset = prc.Width * 4 * (y - prc.Y);
unsafe {
std::ptr::copy_nonoverlapping(
self.frame
.buf()
.as_ptr()
.offset((src_offset + prc.X) as isize),
(pbbuffer as *mut f32).offset(dst_offset as isize),
(prc.Width as usize) * channels,
);
let src_offset = (self.width as i32 * y + prc.X) * channels;
let dst_offset = prc.Width * (y - prc.Y) * channels;

for x in prc.X..(prc.X + prc.Width) * channels {
// XXX: jxl-oxide emits f32 pixels, but it can't be used as-is because of WIC limitation.
// Thus here we convert f32 to u16 instead. https://github.com/saschanaz/jxl-winthumb/issues/29
unsafe {
*pbbuffer.offset((dst_offset + x) as isize) =
(buf[(src_offset + x) as usize] * u16::MAX as f32) as u16;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/wic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn basic() {
}
.expect("Copy pixels");
assert_eq!(pixels[0], 0, "red");
assert_eq!(pixels[1], 42, "green"); // XXX: But this should be 6...
assert_eq!(pixels[1], 6, "green");
assert_eq!(pixels[2], 0, "blue");
assert_eq!(pixels[3], 255, "alpha");
}
Loading