Skip to content

Commit

Permalink
png: Add proper color type
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Nov 23, 2021
1 parent 2af08da commit fc76907
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 36 deletions.
4 changes: 2 additions & 2 deletions format/flac/testdata/picture_seek_gain.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ $ fq -d flac verbose /picture_seek_gain.flac
0x0160| 00 00 00 04| ....| width: 4 0x16c-0x16f.7 (4)
0x0170|00 00 00 04 |.... | height: 4 0x170-0x173.7 (4)
0x0170| 01 | . | bit_depth: 1 0x174-0x174.7 (1)
0x0170| 00 | . | color_type: 0 0x175-0x175.7 (1)
0x0170| 00 | . | color_type: "g" (0) (Grayscale) 0x175-0x175.7 (1)
0x0170| 00 | . | compression_method: "deflate" (0) 0x176-0x176.7 (1)
0x0170| 00 | . | filter_method: "Adaptive filtering" (0) 0x177-0x177.7 (1)
0x0170| 00 | . | interlace_method: "No interlace" (0) 0x178-0x178.7 (1)
Expand Down Expand Up @@ -139,7 +139,7 @@ $ fq -d flac verbose /picture_seek_gain.flac
0x01b0| 4b | K | private: false 0x1be.3-0x1be.3 (0.1)
0x01b0| 47| G| reserved: false 0x1bf.3-0x1bf.3 (0.1)
0x01c0|44 |D | safe_to_copy: false 0x1c0.3-0x1c0.3 (0.1)
0x01c0| 00 01 | .. | value: 1 0x1c1-0x1c2.7 (2)
0x01c0| 00 01 | .. | gray: 1 0x1c1-0x1c2.7 (2)
0x01c0| dd 8a 13 a4 | .... | crc: "dd8a13a4" (raw bits) (valid) 0x1c3-0x1c6.7 (4)
| | | [4]: chunk {} 0x1c7-0x1d9.7 (19)
0x01c0| 00 00 00 07 | .... | length: 7 0x1c7-0x1ca.7 (4)
Expand Down
2 changes: 1 addition & 1 deletion format/id3/testdata/apic.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $ fq -d id3v2 verbose /apic
0x40| 00 00| ..| height: 4 0x4e-0x51.7 (4)
0x50|00 04 |.. |
0x50| 08 | . | bit_depth: 8 0x52-0x52.7 (1)
0x50| 02 | . | color_type: 2 0x53-0x53.7 (1)
0x50| 02 | . | color_type: "rgb" (2) (RGB) 0x53-0x53.7 (1)
0x50| 00 | . | compression_method: "deflate" (0) 0x54-0x54.7 (1)
0x50| 00 | . | filter_method: "Adaptive filtering" (0) 0x55-0x55.7 (1)
0x50| 00 | . | interlace_method: "No interlace" (0) 0x56-0x56.7 (1)
Expand Down
4 changes: 2 additions & 2 deletions format/id3/testdata/utf16-apic.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $ fq -d id3v2 verbose /utf16-apic
0x030| 00 00 00 04| ....| width: 4 0x3c-0x3f.7 (4)
0x040|00 00 00 04 |.... | height: 4 0x40-0x43.7 (4)
0x040| 01 | . | bit_depth: 1 0x44-0x44.7 (1)
0x040| 00 | . | color_type: 0 0x45-0x45.7 (1)
0x040| 00 | . | color_type: "g" (0) (Grayscale) 0x45-0x45.7 (1)
0x040| 00 | . | compression_method: "deflate" (0) 0x46-0x46.7 (1)
0x040| 00 | . | filter_method: "Adaptive filtering" (0) 0x47-0x47.7 (1)
0x040| 00 | . | interlace_method: "No interlace" (0) 0x48-0x48.7 (1)
Expand Down Expand Up @@ -90,7 +90,7 @@ $ fq -d id3v2 verbose /utf16-apic
0x080| 4b | K | private: false 0x8e.3-0x8e.3 (0.1)
0x080| 47| G| reserved: false 0x8f.3-0x8f.3 (0.1)
0x090|44 |D | safe_to_copy: false 0x90.3-0x90.3 (0.1)
0x090| 00 01 | .. | value: 1 0x91-0x92.7 (2)
0x090| 00 01 | .. | gray: 1 0x91-0x92.7 (2)
0x090| dd 8a 13 a4 | .... | crc: "dd8a13a4" (raw bits) (valid) 0x93-0x96.7 (4)
| | | [4]: chunk {} 0x97-0xa9.7 (19)
0x090| 00 00 00 07 | .... | length: 7 0x97-0x9a.7 (4)
Expand Down
2 changes: 1 addition & 1 deletion format/matroska/testdata/flac.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ $ fq -d matroska verbose /flac.mkv
0x250| ff f8 | .. | sync: 0b11111111111110 (valid) 0x255-0x256.5 (1.6)
0x250| f8 | . | reserved0: 0 (valid) 0x256.6-0x256.6 (0.1)
0x250| f8 | . | blocking_strategy: "Fixed" (0) 0x256.7-0x256.7 (0.1)
0x250| 79 | y | block_size: 7 (end of header (16 bit)) 0x257-0x257.3 (0.4)
0x250| 79 | y | block_size: 0 (7) (end of header (16 bit)) 0x257-0x257.3 (0.4)
0x250| 79 | y | sample_rate: 44100 (9) 0x257.4-0x257.7 (0.4)
0x250| 88 | . | channel_assignment: 2 (8) (left/side) 0x258-0x258.3 (0.4)
| | | side_channel_index: 1 0x258.4-NA (0)
Expand Down
2 changes: 1 addition & 1 deletion format/mp4/testdata/flac.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ $ fq -d mp4 verbose /flac.mp4
0x020| ff f8 | .. | sync: 0b11111111111110 (valid) 0x2c-0x2d.5 (1.6)
0x020| f8 | . | reserved0: 0 (valid) 0x2d.6-0x2d.6 (0.1)
0x020| f8 | . | blocking_strategy: "Fixed" (0) 0x2d.7-0x2d.7 (0.1)
0x020| 79 | y | block_size: 7 (end of header (16 bit)) 0x2e-0x2e.3 (0.4)
0x020| 79 | y | block_size: 0 (7) (end of header (16 bit)) 0x2e-0x2e.3 (0.4)
0x020| 79 | y | sample_rate: 44100 (9) 0x2e.4-0x2e.7 (0.4)
0x020| 88| .| channel_assignment: 2 (8) (left/side) 0x2f-0x2f.3 (0.4)
| | | side_channel_index: 1 0x2f.4-NA (0)
Expand Down
2 changes: 1 addition & 1 deletion format/ogg/testdata/flac.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ $ fq -d ogg verbose /flac.ogg
0x000|ff f8 |.. | sync: 0b11111111111110 (valid) 0x0-0x1.5 (1.6)
0x000| f8 | . | reserved0: 0 (valid) 0x1.6-0x1.6 (0.1)
0x000| f8 | . | blocking_strategy: "Fixed" (0) 0x1.7-0x1.7 (0.1)
0x000| 79 | y | block_size: 7 (end of header (16 bit)) 0x2-0x2.3 (0.4)
0x000| 79 | y | block_size: 0 (7) (end of header (16 bit)) 0x2-0x2.3 (0.4)
0x000| 79 | y | sample_rate: 44100 (9) 0x2.4-0x2.7 (0.4)
0x000| 08 | . | channel_assignment: 1 (0) (mono) 0x3-0x3.3 (0.4)
0x000| 08 | . | sample_size: 16 (4) 0x3.4-0x3.6 (0.3)
Expand Down
49 changes: 37 additions & 12 deletions format/png/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package png
// http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html
// https://ftp-osl.osuosl.org/pub/libpng/documents/pngext-1.5.0.html
// https://wiki.mozilla.org/APNG_Specification
// TODO: color types

import (
"compress/zlib"
Expand Down Expand Up @@ -45,9 +44,9 @@ const (
)

var disposeOpNames = decode.UToStr{
disposeOpNone: "None",
disposeOpBackground: "Background",
disposeOpPrevious: "Previous",
disposeOpNone: "none",
disposeOpBackground: "background",
disposeOpPrevious: "previous",
}

const (
Expand All @@ -56,16 +55,33 @@ const (
)

var blendOpNames = decode.UToStr{
blendOpNone: "Source",
blendOpBackground: "Over",
blendOpNone: "source",
blendOpBackground: "over",
}

const (
colorTypeGrayscale = 0
colorTypeRGB = 2
colorTypePalette = 3
colorTypeGrayscaleWithAlpha = 4
colorTypeRGBA = 6
)

var colorTypeMap = decode.UToScalar{
colorTypeGrayscale: {Sym: "g", Description: "Grayscale"},
colorTypeRGB: {Sym: "rgb", Description: "RGB"},
colorTypePalette: {Sym: "p", Description: "Palette"},
colorTypeGrayscaleWithAlpha: {Sym: "ga", Description: "Grayscale with alpha"},
colorTypeRGBA: {Sym: "rgba", Description: "RGBA"},
}

func pngDecode(d *decode.D, in interface{}) interface{} {
iEndFound := false
var colorType uint64

d.FieldRawLen("signature", 8*8, d.AssertBitBuf([]byte("\x89PNG\r\n\x1a\n")))
d.FieldStructArrayLoop("chunks", "chunk", func() bool { return d.NotEnd() && !iEndFound }, func(d *decode.D) {
chunkLength := int(d.FieldU32("length"))
chunkLength := d.FieldU32("length")
crcStartPos := d.Pos()
// TODO: this is a bit weird, use struct?
chunkType := d.FieldStrFn("type", func(d *decode.D) string {
Expand All @@ -90,7 +106,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32("width")
d.FieldU32("height")
d.FieldU8("bit_depth")
d.FieldU8("color_type")
colorType = d.FieldU8("color_type", d.MapUToScalar(colorTypeMap))
d.FieldU8("compression_method", d.MapUToStrSym(compressionNames))
d.FieldU8("filter_method", d.MapUToStrSym(decode.UToStr{
0: "Adaptive filtering",
Expand Down Expand Up @@ -139,7 +155,16 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32("y_pixels_per_unit")
d.FieldU8("unit")
case "bKGD":
d.FieldU16("value")
switch colorType {
case colorTypePalette:
d.FieldU8("index")
case colorTypeGrayscale, colorTypeGrayscaleWithAlpha:
d.FieldU16("gray")
case colorTypeRGB, colorTypeRGBA:
d.FieldU16("r")
d.FieldU16("g")
d.FieldU16("b")
}
case "gAMA":
d.FieldU32("value")
case "cHRM":
Expand All @@ -153,7 +178,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
d.FieldFFn("blue_x", df)
d.FieldFFn("blue_y", df)
case "eXIf":
d.FieldFormatLen("exif", int64(chunkLength)*8, exifFormat, nil)
d.FieldFormatLen("exif", d.BitsLeft(), exifFormat, nil)
case "acTL":
d.FieldU32("num_frames")
d.FieldU32("num_plays")
Expand All @@ -169,12 +194,12 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
d.FieldU8("blend_op", d.MapUToStrSym(blendOpNames))
case "fdAT":
d.FieldU32("sequence_number")
d.FieldRawLen("data", int64(chunkLength-4)*8)
d.FieldRawLen("data", d.BitsLeft()-32)
default:
if chunkType == "IEND" {
iEndFound = true
} else {
d.FieldRawLen("data", int64(chunkLength)*8)
d.FieldRawLen("data", d.BitsLeft())
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions format/png/testdata/4x4.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ fq -d png verbose /4x4.png
0x010|00 00 00 04 |.... | width: 4 0x10-0x13.7 (4)
0x010| 00 00 00 04 | .... | height: 4 0x14-0x17.7 (4)
0x010| 01 | . | bit_depth: 1 0x18-0x18.7 (1)
0x010| 00 | . | color_type: 0 0x19-0x19.7 (1)
0x010| 00 | . | color_type: "g" (0) (Grayscale) 0x19-0x19.7 (1)
0x010| 00 | . | compression_method: "deflate" (0) 0x1a-0x1a.7 (1)
0x010| 00 | . | filter_method: "Adaptive filtering" (0) 0x1b-0x1b.7 (1)
0x010| 00 | . | interlace_method: "No interlace" (0) 0x1c-0x1c.7 (1)
Expand Down Expand Up @@ -57,7 +57,7 @@ $ fq -d png verbose /4x4.png
0x060| 4b | K | private: false 0x62.3-0x62.3 (0.1)
0x060| 47 | G | reserved: false 0x63.3-0x63.3 (0.1)
0x060| 44 | D | safe_to_copy: false 0x64.3-0x64.3 (0.1)
0x060| 00 01 | .. | value: 1 0x65-0x66.7 (2)
0x060| 00 01 | .. | gray: 1 0x65-0x66.7 (2)
0x060| dd 8a 13 a4 | .... | crc: "dd8a13a4" (raw bits) (valid) 0x67-0x6a.7 (4)
| | | [4]: chunk {} 0x6b-0x7d.7 (19)
0x060| 00 00 00 07 | .... | length: 7 0x6b-0x6e.7 (4)
Expand Down
14 changes: 7 additions & 7 deletions format/png/testdata/4x4a.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ fq -d png verbose /4x4a.apng
0x10|00 00 00 04 |.... | width: 4 0x10-0x13.7 (4)
0x10| 00 00 00 04 | .... | height: 4 0x14-0x17.7 (4)
0x10| 08 | . | bit_depth: 8 0x18-0x18.7 (1)
0x10| 02 | . | color_type: 2 0x19-0x19.7 (1)
0x10| 02 | . | color_type: "rgb" (2) (RGB) 0x19-0x19.7 (1)
0x10| 00 | . | compression_method: "deflate" (0) 0x1a-0x1a.7 (1)
0x10| 00 | . | filter_method: "Adaptive filtering" (0) 0x1b-0x1b.7 (1)
0x10| 00 | . | interlace_method: "No interlace" (0) 0x1c-0x1c.7 (1)
Expand Down Expand Up @@ -58,8 +58,8 @@ $ fq -d png verbose /4x4a.apng
0x60| 00 00 00 00 | .... | y_offset: 0 0x62-0x65.7 (4)
0x60| 00 01 | .. | delay_num: 1 0x66-0x67.7 (2)
0x60| 00 01 | .. | delay_sep: 1 0x68-0x69.7 (2)
0x60| 00 | . | dispose_op: "None" (0) 0x6a-0x6a.7 (1)
0x60| 00 | . | blend_op: "Source" (0) 0x6b-0x6b.7 (1)
0x60| 00 | . | dispose_op: "none" (0) 0x6a-0x6a.7 (1)
0x60| 00 | . | blend_op: "source" (0) 0x6b-0x6b.7 (1)
0x60| 5b 27 ec 00| ['..| crc: "5b27ec00" (raw bits) (valid) 0x6c-0x6f.7 (4)
| | | [4]: chunk {} 0x70-0x9d.7 (46)
0x70|00 00 00 22 |..." | length: 34 0x70-0x73.7 (4)
Expand Down Expand Up @@ -88,8 +88,8 @@ $ fq -d png verbose /4x4a.apng
0xb0| 00 00 00 03 | .... | y_offset: 3 0xb6-0xb9.7 (4)
0xb0| 00 01 | .. | delay_num: 1 0xba-0xbb.7 (2)
0xb0| 00 01 | .. | delay_sep: 1 0xbc-0xbd.7 (2)
0xb0| 00 | . | dispose_op: "None" (0) 0xbe-0xbe.7 (1)
0xb0| 00| .| blend_op: "Source" (0) 0xbf-0xbf.7 (1)
0xb0| 00 | . | dispose_op: "none" (0) 0xbe-0xbe.7 (1)
0xb0| 00| .| blend_op: "source" (0) 0xbf-0xbf.7 (1)
0xc0|c2 3b a2 c2 |.;.. | crc: "c23ba2c2" (raw bits) (valid) 0xc0-0xc3.7 (4)
| | | [6]: chunk {} 0xc4-0xe7.7 (36)
0xc0| 00 00 00 18 | .... | length: 24 0xc4-0xc7.7 (4)
Expand All @@ -99,8 +99,7 @@ $ fq -d png verbose /4x4a.apng
0xc0| 41 | A | reserved: false 0xca.3-0xca.3 (0.1)
0xc0| 54 | T | safe_to_copy: true 0xcb.3-0xcb.3 (0.1)
0xc0| 00 00 00 02| ....| sequence_number: 2 0xcc-0xcf.7 (4)
0xd0|78 9c 63 f8 ff 9f 81 e1 7f 03 10 ff 67 a8 07 00|x.c.........g...| data: raw bits 0xd0-0xe3.7 (20)
0xe0|29 e6 05 fb |)... |
0xd0|78 9c 63 f8 ff 9f 81 e1 7f 03 10 ff 67 a8 07 00|x.c.........g...| data: raw bits 0xd0-0xdf.7 (16)
0xe0| 7b f5 c3 3d | {..= | crc: "7bf5c33d" (raw bits) (valid) 0xe4-0xe7.7 (4)
| | | [7]: chunk {} 0xe8-0xf3.7 (12)
0xe0| 00 00 00 00 | .... | length: 0 0xe8-0xeb.7 (4)
Expand All @@ -110,3 +109,4 @@ $ fq -d png verbose /4x4a.apng
0xe0| 4e | N | reserved: false 0xee.3-0xee.3 (0.1)
0xe0| 44| D| safe_to_copy: false 0xef.3-0xef.3 (0.1)
0xf0|ae 42 60 82| |.B`.| | crc: "ae426082" (raw bits) (valid) 0xf0-0xf3.7 (4)
0xe0|29 e6 05 fb |)... | unknown0: raw bits 0xe0-0xe3.7 (4)
2 changes: 1 addition & 1 deletion format/vorbis/testdata/vorbis-comment-picture.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $ fq -d vorbis_comment verbose /vorbis-comment-picture
0x30| 00 00 00| ...| height: 4 0x3d-0x40.7 (4)
0x40|04 |. |
0x40| 08 | . | bit_depth: 8 0x41-0x41.7 (1)
0x40| 02 | . | color_type: 2 0x42-0x42.7 (1)
0x40| 02 | . | color_type: "rgb" (2) (RGB) 0x42-0x42.7 (1)
0x40| 00 | . | compression_method: "deflate" (0) 0x43-0x43.7 (1)
0x40| 00 | . | filter_method: "Adaptive filtering" (0) 0x44-0x44.7 (1)
0x40| 00 | . | interlace_method: "No interlace" (0) 0x45-0x45.7 (1)
Expand Down

0 comments on commit fc76907

Please sign in to comment.