inflation / jpegxl-rs

GNU General Public License v3.0
62 stars 12 forks source link

Encoding a RGBA png produces a garbled output. #96

Closed jindalpratik closed 2 days ago

jindalpratik commented 3 days ago

Converting the following sample image whose sample.color() shows as Rgba16: sample with the following code

    let sample = ImageReader::open("./sample.png")
        .unwrap()
        .decode()
        .unwrap();

    println!("{:?}", sample.color());

    // Set encoder options
    let mut encoder = encoder_builder()
        .lossless(true)
        .uses_original_profile(true)
        .speed(EncoderSpeed::Squirrel)
        .build()
        .unwrap();

    let buffer: EncoderResult<f32> = encoder
        .encode(&sample.to_rgba16(), sample.width(), sample.height())
        .unwrap();

    fs::write("./smple.jxl", `buffer.as_ref()).unwrap();

produces the following garbled image: (uploading a screenshot as github doesn't allow jxl images) image

while encoding the same image as rgb16 works fine and produces the following image:

    let buffer: EncoderResult<f32> = encoder
        .encode(&sample.to_rgb16(), sample.width(), sample.height())
        .unwrap();

    fs::write("./smple.jxl", buffer.as_ref()).unwrap();

image.

I still trying to figure stuff out so I might be doing something wrong but I am unable to convert any image with an alpha channel while keeping the alpha channel and any help in this matter is really appreciated.

Tried on jpegxl-rs = { version = "0.11.0", features = ["vendored"] } and jpegxl-rs = { version = "0.10.4", features = ["vendored"] }

inflation commented 2 days ago

To recognize the alpha channel in the original image data, you need to create an EncoderFrame manually, and set the number of channels there. There's also an example in the tests.

To keep the alpha channel in the produced image, set it in the builder.

jindalpratik commented 2 days ago

Thanks a lot that worked.