From 2c16adba855195fb939f0ed15029357aefb16c5f Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:34:17 -0500 Subject: [PATCH 1/6] patterns/jpeg: exclude EOI marker from SOS image data --- patterns/jpeg.hexpat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index cb08cedf..c82d662f 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -99,7 +99,7 @@ struct SOS { u8 endSpectral; u8 apprBitPos; - u8 image_data[while(!std::mem::eof())] [[sealed]]; + u8 image_data[std::mem::size() - $ - 2] [[sealed]]; }; struct Segment { From f5c22ac64f785dbe8ccf7f02e57a5c485451323e Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:33:17 -0500 Subject: [PATCH 2/6] patterns/jpeg: use type::Magic for magic numbers --- patterns/jpeg.hexpat | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index c82d662f..10614b44 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -2,6 +2,7 @@ #pragma description JPEG Image Format #include +#include #pragma endian big #pragma MIME image/jpeg @@ -58,7 +59,7 @@ fn transform_pixel(Pixel pixel) { struct APP0 { - char magic[5]; + type::Magic<"JFIF\x00"> magic; u8 versionMajor, versionMinor; DensityUnit densityUnit; u16 densityX, densityY; @@ -103,7 +104,7 @@ struct SOS { }; struct Segment { - u8 magic; + type::Magic<"\xff"> magic; Marker marker; if (marker == Marker::SOI || marker == Marker::EOI) { From b6dd321ac7d5515d99d40e8f9e6e0909daa520dc Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:35:06 -0500 Subject: [PATCH 3/6] patterns/jpeg: add RGB component IDs --- patterns/jpeg.hexpat | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index 10614b44..c83392dd 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -72,7 +72,10 @@ enum ComponentId : u8 { CB = 2, CR = 3, I = 4, - Q = 5 + Q = 5, + B = 66, + G = 71, + R = 82 }; struct SOF0Component { From 25d9936006428f24e7aa29dbd69b1052db38afd1 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:36:22 -0500 Subject: [PATCH 4/6] patterns/jpeg: add struct for APP14 marker --- patterns/jpeg.hexpat | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index c83392dd..84885d1f 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -49,6 +49,12 @@ enum DensityUnit : u8 { PixelsPerCm = 0x02 }; +enum ColorTransform : u8 { + Unknown = 0x00, + YCbCr = 0x01, + YCCK = 0x02 +}; + struct Pixel { u8 r, g, b; } [[sealed, transform("transform_pixel")]]; @@ -67,6 +73,14 @@ struct APP0 { Pixel thumbnail[thumbnailX * thumbnailY] [[sealed]]; }; +struct APP14 { + type::Magic<"Adobe"> magic; + u16 version; + u16 flags0; + u16 flags1; + ColorTransform transform; +}; + enum ComponentId : u8 { Y = 1, CB = 2, @@ -116,6 +130,8 @@ struct Segment { u16 length; if (marker == Marker::APP0) { APP0 data; + } else if (marker == Marker::APP14) { + APP14 data; } else if (marker == Marker::SOF0) { SOF0 data; } else if (marker == Marker::SOS) { From b5e6a95f8f758a62b967b897fafbfe31bd2aa43c Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:43:22 -0500 Subject: [PATCH 5/6] patterns/jpeg: add definition for COM marker --- patterns/jpeg.hexpat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index 84885d1f..26a33ddb 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -132,6 +132,8 @@ struct Segment { APP0 data; } else if (marker == Marker::APP14) { APP14 data; + } else if (marker == Marker::COM) { + char data[length - sizeof(length)]; } else if (marker == Marker::SOF0) { SOF0 data; } else if (marker == Marker::SOS) { From d78476d94bace55f9dc574bf391eb7a7898f9509 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 24 Oct 2023 23:37:41 -0500 Subject: [PATCH 6/6] patterns/jpeg: add some format_read() for better legibility --- patterns/jpeg.hexpat | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/patterns/jpeg.hexpat b/patterns/jpeg.hexpat index 26a33ddb..6e0c542c 100644 --- a/patterns/jpeg.hexpat +++ b/patterns/jpeg.hexpat @@ -1,6 +1,7 @@ #pragma author WerWolv #pragma description JPEG Image Format +#include #include #include #pragma endian big @@ -96,6 +97,10 @@ struct SOF0Component { ComponentId componentId; u8 samplingFactors; u8 quantizationTableId; +} [[format_read("sof0_component_read")]]; + +fn sof0_component_read(SOF0Component c) { + return std::format("({}, {}, {})", c.componentId, c.samplingFactors, c.quantizationTableId); }; struct SOF0 { @@ -142,6 +147,10 @@ struct Segment { u8 data[length - sizeof(length)] [[sealed]]; } } +} [[format_read("segment_read")]]; + +fn segment_read(Segment s) { + return std::format("{}", s.marker); }; Segment segments[while(!std::mem::eof())] @ 0x00 [[hex::visualize("image", this)]]; \ No newline at end of file