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

decodeImageFile error #636

Open
equationl opened this issue Apr 7, 2024 · 1 comment
Open

decodeImageFile error #636

equationl opened this issue Apr 7, 2024 · 1 comment

Comments

@equationl
Copy link

I have a jpg file, but it's named "xx.png", when I try decode this file , I using decodeImageFile("xx.png"); . But get a null .

I check the code of decodeImageFile, it will get decoder by file name at findDecoderForNamedImage, in my case, it will use pngDecoder, of course it a mistake.

I find in pngDecoder, it will check file header:

    final pngHeader = _input.readBytes(8);
    const expectedHeader = [137, 80, 78, 71, 13, 10, 26, 10];
    for (var i = 0; i < 8; ++i) {
      if (pngHeader[i] != expectedHeader[i]) {
        return null;
      }
    }

If not png header, will return null.

So, why don't try another decoder if file header not match file name's extension?

Like change:

Future<Image?> decodeImageFile(String path, {int? frame}) async {
  final bytes = await readFile(path);
  if (bytes == null) {
    return null;
  }

  final decoder = findDecoderForNamedImage(path);
  if (decoder != null) {
-    return decoder.decode(bytes, frame: frame);
  }

  return decodeImage(bytes, frame: frame);
}

To:

Future<Image?> decodeImageFile(String path, {int? frame}) async {
  final bytes = await readFile(path);
  if (bytes == null) {
    return null;
  }

  final decoder = findDecoderForNamedImage(path);
  if (decoder != null) {
+    return decoder.decode(bytes, frame: frame) ?? decodeImage(bytes, frame: frame);
  }
  

  return decodeImage(bytes, frame: frame);
}

How about it?

@brendan-duncan
Copy link
Owner

Yeah, that's a fine suggestion. I just had that file open for another issue, so I pushed that change out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants