Skip to content

Fail to preview a jpg file with APP2 marker #313

@kenya888

Description

@kenya888

Thank you for your cool stuff in the TUI world 😉

Environment

  • felix v2.16.1
  • Chafa 1.16.1
  • Rust 1.85.1
  • Gentoo Linux (kernel 6.14.9)

Issue

A jpg image cannot be shown in the preview pane with chafa.
The header of the jpg file starts "0xFF D8 FF E2"

Description

✅ The header of sample.jpg in felix repo

kenya888@tp14sg6sd ~/devel/com/github $ hexdump -C -n 64 felix/testfiles/images/sample.jpg
00000000  ff d8 ff e0 00 10 4a 46  49 46 00 01 01 01 00 48  |......JFIF.....H|
00000010  00 48 00 00 ff e2 0c 58  49 43 43 5f 50 52 4f 46  |.H.....XICC_PROF|
00000020  49 4c 45 00 01 01 00 00  0c 48 4c 69 6e 6f 02 10  |ILE......HLino..|
00000030  00 00 6d 6e 74 72 52 47  42 20 58 59 5a 20 07 ce  |..mntrRGB XYZ ..|
00000040

🆖 The header of my jpg file with failure. This is not broken, can see with chafa in the terminal directly.

kenya888@tp14sg6sd ~/devel/com/github $ hexdump -C -n 64 ffe2.jpg
00000000  ff d8 ff e2 0c 58 49 43  43 5f 50 52 4f 46 49 4c  |.....XICC_PROFIL|
00000010  45 00 01 01 00 00 0c 48  4c 69 6e 6f 02 10 00 00  |E......HLino....|
00000020  6d 6e 74 72 52 47 42 20  58 59 5a 20 07 ce 00 02  |mntrRGB XYZ ....|
00000030  00 09 00 06 00 31 00 00  61 63 73 70 4d 53 46 54  |.....1..acspMSFT|
00000040

This is because no magic number definition of jpg header with "0xFF D8 FF E2" (APP2 for use of ICC PROFILE and another)
https://github.com/kyoheiu/felix/blob/develop/src/magic_image.rs#L5-L7

I've confirmed my issue can be fixed with a very simple change like this.

diff --git a/src/magic_image.rs b/src/magic_image.rs
index ea0189b..d52a327 100644
--- a/src/magic_image.rs
+++ b/src/magic_image.rs
@@ -4,7 +4,8 @@ use std::{io::Read, path::Path};

 const HEADER_JPG1: [u8; 4] = [0xff, 0xd8, 0xff, 0xdb];
 const HEADER_JPG2: [u8; 4] = [0xff, 0xd8, 0xff, 0xe0];
-const HEADER_JPG3: [u8; 4] = [0xff, 0xd8, 0xff, 0xee];
+const HEADER_JPG3: [u8; 4] = [0xff, 0xd8, 0xff, 0xe2];
+const HEADER_JPG4: [u8; 4] = [0xff, 0xd8, 0xff, 0xee];
 const HEADER_JPG_EXIF: [u8; 4] = [0xff, 0xd8, 0xff, 0xe1];
 const HEADER_JPG_EXIF_AFTER: [u8; 6] = [0x45, 0x78, 0x69, 0x66, 0x00, 0x00];
 const HEADER_PNG: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
@@ -55,6 +56,7 @@ fn inspect_image(p: &Path) -> Result<ImageSignature, FxError> {
     let sign = if buffer[..4] == HEADER_JPG1
         || buffer[..4] == HEADER_JPG2
         || buffer[..4] == HEADER_JPG3
+        || buffer[..4] == HEADER_JPG4
         || (buffer[..4] == HEADER_JPG_EXIF && buffer[6..] == HEADER_JPG_EXIF_AFTER)
     {
         ImageSignature::Jpg
lines 1-22/22 (END)

I'm not a expert of this area and I guess you may exclude this magic number intentionally.
What do you think about this?

By the way, file/libmagic checks only the first 3byte to detect base jpeg file format.

https://github.com/file/file/blob/master/magic/Magdir/jpeg#L17

Thanks!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions