Skip to content

[Bug]: decode_gray8 returns Image<u8, 3> instead of Image<u8, 1>Β #830

@ana-jiangR

Description

@ana-jiangR

πŸ› Describe the bug

decode_gray8() (line 166) in crates/kornia-io/src/jpegturbo.rs returns Image<u8, 3> but should return Image<u8, 1>, as it decodes with PixelFormat::GRAY (1 byte per pixel) and the docstring already says it should return Image<u8, 1>.
Meanwhile the pitch (line 188) is hardcoded to 3 * image_size.width instead of C * image_size.width, which works for decode_rgb8 but breaks decode_gray8.

πŸ”„ Steps to Reproduce

1. Create a JpegTurboDecoder and call `decode_gray8` with any valid JPEG
2. Check num_channels() on the result, which returns 3 instead of 1

πŸ’» Minimal Code Example

use kornia_io::jpegturbo::JpegTurboDecoder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let decoder = JpegTurboDecoder::new()?;
    let jpeg_data = std::fs::read("path/to/dog.jpeg")?;
    let image = decoder.decode_gray8(&jpeg_data)?;
    // Returns 3, should be 1
    println!("channels: {}", image.num_channels());
    Ok(())
}

βœ… Expected behavior

decode_gray8 should return Image<u8, 1> with 1 channel and correct pixel layout, consistent with its docstring and PixelFormat::GRAY.

❌ Actual behavior

decode_gray8 returns Image<u8, 3> with 3 channels. The buffer is 3x too large and the pitch is wrong, resulting in incorrect grayscale data.

πŸ”§ Environment

- kornia-rs version: 0.1.11 (workspace)
- Rust version (`rustc -V`): rustc 1.94.0 (4a4ef493e 2026-03-02)
- Cargo version (`cargo -V`): cargo 1.94.0 (85eff7c80 2026-01-15)
- OS (e.g., Linux, macOS, Windows): Linux

πŸ“ Additional context

The fix should be really simple by changing the return type of decode_gray8 from Image<u8, 3> to Image<u8, 1> and change pitch: 3 * image_size.width to pitch: C * image_size.width in decode<C>.
Happy to submit a PR if approved!

🀝 Contribution Intent

  • I plan to submit a PR to fix this bug
  • I'm reporting this bug but not planning to fix it

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededtriagewait for a maintainer to approve and assign this ticket

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions