-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.go
136 lines (112 loc) · 2.73 KB
/
decoder.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
package pnm
import (
"bufio"
"bytes"
"errors"
"io"
"strconv"
)
var (
errWrongFormat = errors.New("Wrong image format")
errMalformedHeader = errors.New("Malformed image header")
)
type header struct {
magicNumber int
width int
height int
maxValue byte
}
// Decoder decodes a byte or text stream into a pnm image format
type Decoder struct {
reader *bufio.Reader
format int64
}
// NewDecoder creates a pnm image decoder
func NewDecoder(reader io.Reader, format int64) *Decoder {
return &Decoder{reader: bufio.NewReader(reader), format: format}
}
func (d *Decoder) decodeHeader() (*header, error) {
headerBuf := make([]byte, 0)
var err error
var b byte
var h header
var numField int
switch d.format {
case PPMBinary, PGMBinary, PPMText, PGMText:
numField = 4
default:
numField = 3
}
comment := false
for fields := 0; fields < numField; {
b, _ = d.reader.ReadByte()
if b == '#' {
comment = true
} else if !comment {
headerBuf = append(headerBuf, b)
}
if comment && b == '\n' {
comment = false
} else if !comment && (b == ' ' || b == '\n' || b == '\t') {
fields++
}
}
hfields := bytes.Fields(headerBuf)
mn := string(string(hfields[0]))
if len(mn) < 2 {
return nil, errMalformedHeader
}
h.magicNumber, err = strconv.Atoi(string(mn[1]))
if err != nil {
return nil, err
}
if h.magicNumber != int(d.format) {
return nil, errWrongFormat
}
h.width, err = strconv.Atoi(string(hfields[1]))
if err != nil {
return nil, err
}
h.height, err = strconv.Atoi(string(hfields[2]))
if err != nil {
return nil, err
}
if numField == 4 {
mv, err := strconv.Atoi(string(hfields[3]))
if err != nil {
return nil, err
}
h.maxValue = byte(mv)
}
return &h, nil
}
// Decode decodes an open image format file and fills an image struct with these data
func (d *Decoder) Decode(image Image) error {
header, err := d.decodeHeader()
if err != nil {
return err
}
updateBuffer := func(i Image) error {
b := make([]byte, len(i.Buffer()))
_, err = io.ReadFull(d.reader, b)
if err != nil {
return err
}
i.SetBuffer(b)
return nil
}
switch image := image.(type) {
case *PGMImage:
*image = *NewPGMImage(int64(header.width), int64(header.height), header.maxValue, int64(header.magicNumber))
updateBuffer(image)
case *PPMImage:
*image = *NewPPMImage(int64(header.width), int64(header.height), header.maxValue, int64(header.magicNumber))
updateBuffer(image)
case *PBMImage:
*image = *NewPBMImage(int64(header.width), int64(header.height), int64(header.magicNumber))
updateBuffer(image)
default:
return errWrongFormat
}
return nil
}