Skip to content

Instantly share code, notes, and snippets.

@ninp0
Last active January 23, 2024 17:56
Show Gist options
  • Save ninp0/955812b1766c10fb9d86adb7255bdec2 to your computer and use it in GitHub Desktop.
Save ninp0/955812b1766c10fb9d86adb7255bdec2 to your computer and use it in GitHub Desktop.

xxd Dump - 1x1-255_255_255_1.png

00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR
00000010: 0000 0001 0000 0001 0806 0000 001f 15c4  ................
00000020: 8900 0000 0173 5247 4200 aece 1ce9 0000  .....sRGB.......
00000030: 000d 4944 4154 1857 63f8 ffff ff7f 0009  ..IDAT.Wc.......
00000040: fb03 fd05 4345 ca00 0000 0049 454e 44ae  ....CE.....IEND.
00000050: 4260 820a                                B`..

Breakdown - 1x1-255_255_255_1.png:

  1. 89 50 4E 47 0D 0A 1A 0A: This is the PNG file signature. It's always the same for every PNG file.
  2. 00 00 00 0D 49 48 44 52: This is the IHDR chunk, which contains the image's dimensions and color type. The first four bytes are the length of the chunk (13 bytes), and the next four bytes are the chunk type (IHDR).
  3. 00 00 00 01 00 00 00 01 08 06 00 00 00 1F 15 C4 89: This is the IHDR chunk data. It contains the width (1 pixel), height (1 pixel), bit depth (8), color type (6), compression method (0), filter method (0), and interlace method (0). The last four bytes are the CRC of the chunk data.
  4. 00 00 00 01 73 52 47 42 00 AE CE 1C E9: This is the sRGB chunk, which specifies the standard RGB color space. The first four bytes are the length of the chunk (1 byte), and the next four bytes are the chunk type (sRGB). The next byte is the rendering intent, and the last four bytes are the CRC of the chunk data.
  5. 00 00 00 0D 49 44 41 54 18 57 63 F8 FF FF FF 7F 00 09 FB 03 FD 05 43 45 CA: This is the IDAT chunk, which contains the actual image data. The first four bytes are the length of the chunk (13 bytes), and the next four bytes are the chunk type (IDAT). The rest of the bytes are the chunk data and the CRC of the chunk data.
  6. 00 00 00 00 49 45 4E 44 AE 42 60 82: This is the IEND chunk, which marks the end of the PNG file. The first four bytes are the length of the chunk (0 bytes), and the next four bytes are the chunk type (IEND). The last four bytes are the CRC of the chunk data.

xxd Dump - 1x1-0_0_0_1.png:

00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR
00000010: 0000 0001 0000 0001 0806 0000 001f 15c4  ................
00000020: 8900 0000 0173 5247 4200 aece 1ce9 0000  .....sRGB.......
00000030: 000d 4944 4154 1857 6360 6060 f80f 0001  ..IDAT.Wc```....
00000040: 0401 0070 2065 0b00 0000 0049 454e 44ae  ...p e.....IEND.
00000050: 4260 820a                                B`..

Breakdown - 1x1-0_0_0_1.png:

  1. 89 50 4E 47 0D 0A 1A 0A: This is the PNG file signature. It's always the same for every PNG file.

  2. 00 00 00 0D 49 48 44 52: This is the IHDR chunk. It's the first chunk in a PNG file and contains the header information. The 00 00 00 0D is the length of the chunk (13 bytes), and 49 48 44 52 is the chunk type (IHDR).

  3. 00 00 00 01 00 00 00 01 08 06 00 00 00 1F 15 C4 89: This is the IHDR chunk data. It contains the image's width, height, bit depth, color type, compression method, filter method, and interlace method.

  4. 00 00 00 01 73 52 47 42 00 AE CE 1C E9: This is the sRGB chunk. It indicates that the image uses standard RGB color.

  5. 00 00 00 0D 49 44 41 54 18 57 63 60 60 60 F8 0F 00 01 04 01 00 70 20 65 0B: This is the IDAT chunk. It contains the actual image data.

  6. 00 00 00 00 49 45 4E 44 AE 42 60 82 0A: This is the IEND chunk. It marks the end of the PNG file.

xxd Dump - 8x8-80_87_78_0.7331.png:

00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR
00000010: 0000 0008 0000 0008 0806 0000 00c4 0fbe  ................
00000020: 8b00 0000 1349 4441 5478 da63 0c08 f7db  .....IDATx.c....
00000030: c580 0730 8e0c 0500 afb1 0d81 8576 8c72  ...0.........v.r
00000040: 0000 0000 4945 4e44 ae42 6082 0a         ....IEND.B`..

Breakdown - 8x8-80_87_78_0.7331.png:

  1. 89 50 4E 47 0D 0A 1A 0A: This is the PNG file signature. It's always the same for every PNG file.
  2. 00 00 00 0D 49 48 44 52: This is the IHDR chunk, which contains the image's dimensions and color type. The first four bytes are the length of the chunk (13 bytes), and the next four bytes are the chunk type (IHDR).
  3. 00 00 00 08 00 00 00 08 08 06 00 00 00 C4 0F BE 8B: This is the IHDR chunk data. It contains the width (8 pixels), height (8 pixels), bit depth (8), color type (6), compression method (0), filter method (0), and interlace method (0). The last four bytes are the CRC of the chunk data.
  4. 00 00 00 13 49 44 41 54 78 DA 63 0C 08 F7 DB C5 80 07 30 8E 0C 05 00 AF B1 0D 81 85 76 8C 72: This is the IDAT chunk, which contains the actual image data. The first four bytes are the length of the chunk (19 bytes), and the next four bytes are the chunk type (IDAT). The rest of the bytes are the chunk data and the CRC of the chunk data.
  5. 00 00 00 00 49 45 4E 44 AE 42 60 82: This is the IEND chunk, which marks the end of the PNG file. The first four bytes are the length of the chunk (0 bytes), and the next four bytes are the chunk type (IEND). The last four bytes are the CRC of the chunk data.

zlib decompression of the IDAT chunk with its CRC to convey the RGBA value using an example in ruby:

require 'zlib'
require 'stringio'

data = 'IDAT_CHUNK_DATA_PLACEHOLDER_SEE_VALUES_BELOW'
data = [data].pack('H*')

inflate = Zlib::Inflate.new
decompressed_data = inflate.inflate(data)
puts decompressed_data.unpack('H*')
data = '18 57 63 F8 FF FF FF 7F 00 09 FB 03 FD 05 43 45 CA'.delete("\s")
decompressed_data returns:
=> "\x00\xFF\xFF\xFF\xFF"
data = '18 57 63 60 60 60 F8 0F 00 01 04 01 00 70 20 65 0B'.delete("\s")
decompressed_data returns:
=> "\x00\x00\x00\x00\xFF"
data = '78 DA 63 0C 08 F7 DB C5 80 07 30 8E 0C 05 00 AF B1 0D 81 85 76 8C 72'.delete("\s")
decompressed_data returns:
=> "\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x01PWN\xBA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

Compress it back up:

require 'zlib'                                                                                                         
require 'stringio'                                                                                                     
                                                                                                                     
deflate = Zlib::Deflate.new                                                                                            
compressed_data = deflate.deflate(decompressed_data, Zlib::FINISH)                                                            puts compressed_data.unpack('H*')   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment