Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Created November 1, 2021 10:36
Show Gist options
  • Save LeoAdamek/4fea72236bf82e1f57c42db5d04b2cfa to your computer and use it in GitHub Desktop.
Save LeoAdamek/4fea72236bf82e1f57c42db5d04b2cfa to your computer and use it in GitHub Desktop.
PNG Pattern for ImHex
#pragma endian big
enum ChunkType : u32 {
// Critical Chunks (uppercase first char)
ImageHeader = 0x49484452, //"IHDR",
Palette = 0x504c5445, //"PLTE",
ImageData = 0x49444154, //"IDAT",
ImageEnd = 0x49454E44, //"IEND",
// Ancillery chunks (lowercase first char)
Background = 0x624B4744, //"bKGD",
ChromaticityCoords = "cHRM",
Signature = "dSIG",
Exif = "eXIf",
Gamma = 0x67414d41,//"gAMA",
ColourHist = "hIST",
PixelDims = 0x70485973, //"pHYs"
Text = 0x74455874, //"tEXt"
ColorProfile= 0x69434350, // iCCP
Timestamp = 0x74494D45, // tIME
SRGB = 0x73524742, // sRGB
};
enum ColorType : u8 {
Grayscale = 0,
RGB = 2,
Indexed = 3,
GrayAlpha = 4,
RGBA = 6
};
enum PixelUnit : u8 {
Unknown,
Metres
};
namespace ChunkData {
struct ImageHeader {
u32 imageWidth [[name("Image Width")]];
u32 imageHeight [[name("Image Height")]];
u8 bitsPerPixel [[name("Bits per Channel")]];
ColorType colorType [[name("Colour Type")]];
u8 compression [[name("Compression Type")]];
u8 filter [[name("Filter Type")]];
bool interlace [[name("Interlaced Image?")]];
};
struct ImageData {
u8 compression;
u8 fCheck;
u8 dataBlock[6];
u32 alder32;
};
struct PixelPhysicalDimensions {
u32 x [[name("Width")]];
u32 y [[name("Height")]];
PixelUnit unit [[name("Unit")]];
};
struct PaletteEntry {
u8 red [[name("Red"), color("ff0000ff")]];
u8 green [[name("Green"),color("00ff0000")]];
u8 blue [[name("Blue"), color("0000ff00")]];
};
struct Color16 {
u16 red;
u16 green;
u16 blue;
};
struct Timestamp {
u16 year;
u8 month;
u8 day;
u8 hour;
u8 minute;
u8 second;
};
enum SRGBIntent : u8 {
Perceptual,
RelativeColorimetric,
Saturation,
AbsoluteColorimetric
};
}
struct Header {
u8 binaryFlag [[comment("Byte with MSB high to avoid 7-bit interpretation"),name("Binary Flag")]];
char magic[3] [[comment("PNG Magic"), name("File Magic")]];
char crlf[2] [[comment("CRLF to detect DOS line endings"), name("CRLF")]];
char eof [[comment("EOF Marker to flag file as binary"), name("EOF")]];
char lf [[comment("LF to detect UNIX line endings"), name("LF")]];
};
struct Chunk {
u32 length [[comment("Chunk Length"), color("48006C00")]];
be ChunkType chunkType [[comment("Chunk type/name"), color("00566C00")]];
if (chunkType == ChunkType::ImageHeader) {
ChunkData::ImageHeader data;
} else if (chunkType == ChunkType::Palette) {
ChunkData::PaletteEntry palette[length / 3];
} else if (chunkType == ChunkType::ImageData) {
u8 compression;
u8 fcheck;
u8 data[length - 6];
u32 checksum;
} else if (chunkType == ChunkType::PixelDims) {
ChunkData::PixelPhysicalDimensions data;
} else if (chunkType == ChunkType::Background) {
if (length == 1) {
u8 colorIndex;
} else if (length == 2) {
u16 grayness;
} else if (length == 6) {
ChunkData::Color16 background;
}
} else if (chunkType == ChunkType::Text) {
char data[length];
} else if (chunkType == ChunkType::Timestamp) {
ChunkData::Timestamp timestamp;
} else if (chunkType == ChunkType::SRGB) {
ChunkData::SRGBIntent intent;
} else if (chunkType == ChunkType::Gamma) {
u32 gamma;
} else {
u8 data[length];
}
u32 crc32 [[comment("Data CRC-32")]];
};
Header header @ 0x00;
Chunk chunks[while($ < std::mem::size())] @ 0x08;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment