-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
201 lines (169 loc) · 3.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/ghostiam/binstruct"
)
// Portable Network Graphics (PNG) Specification: https://www.w3.org/TR/PNG/
func main() {
file, err := os.Open("sample.png")
if err != nil {
log.Fatal(err)
}
var png PNG
decoder := binstruct.NewDecoder(file, binary.BigEndian)
// decoder.SetDebug(true)
err = decoder.Decode(&png)
if err != nil {
log.Fatal(err)
}
spew.Dump(png)
}
type PNG struct {
Header [8]byte // Magic: 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
IHDR IHDR
Chunks []Chunk `bin:"ReadChunks"`
}
func (png *PNG) ReadChunks(r binstruct.Reader) error {
for {
var c Chunk
err := r.Unmarshal(&c)
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("failed read png chunk: %w", err)
}
png.Chunks = append(png.Chunks, c)
}
}
// https://www.w3.org/TR/PNG/#11IHDR
type IHDR struct {
Len int32
Type string `bin:"len:4"`
Data IHDRData
CRC [4]byte
}
type IHDRData struct {
Width int32
Height int32
BitDepth int8
ColorType PNGColorType
CompressionMethod int8
FilterMethod int8
InterlaceMethod int8
}
type PNGColorType int8
func (t PNGColorType) String() string {
colors := [...]string{
"Greyscale", // 0
"Invalid", // 1
"Truecolor", // 2
"Indexed", // 3
"Greyscale with alpha", // 4
"Invalid", // 5
"Truecolor with alpha", // 6
}
return colors[t]
}
type Chunk struct {
Len int32
Type string `bin:"len:4"`
Data interface{} `bin:"ReadChunkData"`
CRC [4]byte
}
func (c *Chunk) ReadChunkData(r binstruct.Reader) (interface{}, error) {
switch c.Type {
case "PLTE": // https://www.w3.org/TR/PNG/#11PLTE
v := PaletteData{DataLen: c.Len}
err := r.Unmarshal(&v)
return v, err
case "cHRM": // https://www.w3.org/TR/PNG/#11cHRM
var v ChromaticData
err := r.Unmarshal(&v)
return v, err
case "gAMA": // https://www.w3.org/TR/PNG/#11gAMA
gamma, err := r.ReadInt32()
return GammaData(gamma), err
// case "sRGB": // https://www.w3.org/TR/PNG/#11sRGB
// case "bKGD": // https://www.w3.org/TR/PNG/#11bKGD
case "pHYs": // https://www.w3.org/TR/PNG/#11pHYs
var v PhysicalPixelData
err := r.Unmarshal(&v)
return v, err
// case "tIME": // https://www.w3.org/TR/PNG/#11tIME
case "iTXt": // https://www.w3.org/TR/PNG/#11iTXt
v := InternationalTextData{DataLen: c.Len}
err := r.Unmarshal(&v)
return v, err
// case "tEXt": // https://www.w3.org/TR/PNG/#11tEXt
// case "zTXt": // https://www.w3.org/TR/PNG/#11zTXt
default:
// IDAT, IEND and others
// read raw
_, b, err := r.ReadBytes(int(c.Len))
return RawData(b), err
}
}
type RawData []byte
func (RawData) String() string {
return "<skip>"
}
type PaletteData struct {
DataLen int32 `bin:"-"` // Helper
Entries []RGB `bin:"len:DataLen/3"`
}
type RGB struct {
R int8
G int8
B int8
}
type ChromaticData struct {
White Point
Red Point
Green Point
Blue Point
}
type Point struct {
X int32
Y int32
}
type GammaData int32
type PhysicalPixelData struct {
Point
Unit int8
}
type InternationalTextData struct {
DataLen int32 `bin:"-"` // Helper
Keyword string `bin:"NullTerminatedString"`
CompressionFlag byte
CompressionMethod byte
LanguageTag string `bin:"NullTerminatedString"`
TranslatedKeyword string `bin:"NullTerminatedString"`
Text string `bin:"len:DataLen-2"` // DataLen - CompressionFlag - CompressionMethod
}
func (d *InternationalTextData) NullTerminatedString(r binstruct.Reader) (string, error) {
var b []byte
var readCount int32
for {
readByte, err := r.ReadByte()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return "", err
}
readCount++
if readByte == 0x00 {
break
}
b = append(b, readByte)
}
d.DataLen -= readCount
return string(b), nil
}