/
reader.go
232 lines (202 loc) · 5.27 KB
/
reader.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package hli
import (
"bufio"
"encoding/binary"
"image"
"io"
"github.com/fxamacker/cbor/v2"
"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/format"
"github.com/mdouchement/hdr/hdrcolor"
)
type decoder struct {
r io.Reader
cr io.ReadCloser
h *Header
config image.Config
convert func(pixel []byte) (float64, float64, float64)
nbOfchannel int
channelSize int
}
func newDecoder(r io.Reader) (*decoder, error) {
d := &decoder{
r: bufio.NewReader(r),
h: new(Header),
}
return d, d.parseHeader()
}
//--------------------------------------//
// Header parser //
//--------------------------------------//
func (d *decoder) parseHeader() error {
magic, err := readN(d.r, len(header)) // magic number
if err != nil {
return err
}
if string(magic) != header {
return FormatError("format not compatible")
}
l, err := readN(d.r, 1) // 1-byte header-size length
if err != nil {
return err
}
size, err := readN(d.r, int(l[0])) // variable-length header-size
if err != nil {
return err
}
header, err := readN(d.r, bytesToLength(size))
if err != nil {
return err
}
if err := cbor.Unmarshal(header, d.h); err != nil {
return err
}
d.cr = newCompresserReader(d.r, d.h)
switch d.h.Format {
case FormatRGBE:
d.config.ColorModel = hdrcolor.RGBModel
d.channelSize = 1
d.nbOfchannel = 4
d.convert = func(p []byte) (float64, float64, float64) {
return format.FromRadianceBytes(p[0], p[1], p[2], p[3], 1)
}
case FormatXYZE:
d.config.ColorModel = hdrcolor.XYZModel
d.channelSize = 1
d.nbOfchannel = 4
d.convert = func(p []byte) (float64, float64, float64) {
return format.FromRadianceBytes(p[0], p[1], p[2], p[3], 1)
}
case FormatRGB:
d.channelSize = 4
d.nbOfchannel = 3
d.config.ColorModel = hdrcolor.RGBModel
d.convert = func(p []byte) (float64, float64, float64) {
return format.FromBytes(binary.LittleEndian, p)
}
case FormatXYZ:
d.config.ColorModel = hdrcolor.XYZModel
d.channelSize = 4
d.nbOfchannel = 3
d.convert = func(p []byte) (float64, float64, float64) {
return format.FromBytes(binary.LittleEndian, p)
}
case FormatLogLuv:
d.config.ColorModel = hdrcolor.XYZModel
d.channelSize = 1
d.nbOfchannel = 4
d.convert = func(p []byte) (float64, float64, float64) {
return format.LogLuvToXYZ(p[0], p[1], p[2], p[3])
}
}
d.config.Width = d.h.Width
d.config.Height = d.h.Height
return nil
}
//--------------------------------------//
// Pixels parser //
//--------------------------------------//
func (d *decoder) decode(dst image.Image, y int, scanline []byte) {
size := d.nbOfchannel * d.channelSize
for x := 0; x < d.config.Width; x++ {
b0, b1, b2 := d.convert(scanline[x*size : x*size+size])
switch d.h.Format {
case FormatRGBE:
fallthrough
case FormatRGB:
img := dst.(*hdr.RGB)
img.SetRGB(x, y, hdrcolor.RGB{R: b0, G: b1, B: b2})
case FormatXYZE:
fallthrough
case FormatLogLuv:
fallthrough
case FormatXYZ:
img := dst.(*hdr.XYZ)
img.SetXYZ(x, y, hdrcolor.XYZ{X: b0, Y: b1, Z: b2})
}
}
}
func (d *decoder) decodeSeparately(dst image.Image, y int, scanline []byte) {
for x := 0; x < d.config.Width; x++ {
pixel := make([]byte, 0, d.nbOfchannel*d.channelSize)
for c := 0; c < d.nbOfchannel; c++ {
pos := x*d.channelSize + c*d.channelSize*d.config.Width
pixel = append(pixel, scanline[pos:pos+d.channelSize]...)
}
b0, b1, b2 := d.convert(pixel)
switch d.h.Format {
case FormatRGBE:
fallthrough
case FormatRGB:
img := dst.(*hdr.RGB)
img.SetRGB(x, y, hdrcolor.RGB{R: b0, G: b1, B: b2})
case FormatXYZE:
fallthrough
case FormatLogLuv:
fallthrough
case FormatXYZ:
img := dst.(*hdr.XYZ)
img.SetXYZ(x, y, hdrcolor.XYZ{X: b0, Y: b1, Z: b2})
}
}
}
//--------------------------------------//
// Reader //
//--------------------------------------//
// DecodeHeader returns the Header without decoding the entire image.
func DecodeHeader(r io.Reader) (Header, error) {
d, err := newDecoder(r)
if err != nil {
return Header{}, err
}
return *d.h, nil
}
// DecodeConfig returns the color model and dimensions of a RGBE image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (image.Config, error) {
d, err := newDecoder(r)
if err != nil {
return image.Config{}, err
}
return d.config, nil
}
// Decode reads a HDR image from r and returns an image.Image.
func Decode(r io.Reader) (img image.Image, err error) {
d, err := newDecoder(r)
if err != nil {
return nil, err
}
imgRect := image.Rect(0, 0, d.config.Width, d.config.Height)
switch d.h.Format {
case FormatRGBE:
fallthrough
case FormatRGB:
img = hdr.NewRGB(imgRect)
case FormatXYZE:
fallthrough
case FormatLogLuv:
fallthrough
case FormatXYZ:
img = hdr.NewXYZ(imgRect)
default:
err = UnsupportedError("image mode")
return
}
scanline := make([]byte, d.config.Width*d.nbOfchannel*d.channelSize)
for y := 0; y < d.config.Height; y++ {
_, err = io.ReadFull(d.cr, scanline)
if err != nil {
return
}
switch d.h.RasterMode {
case RasterModeNormal:
d.decode(img, y, scanline)
case RasterModeSeparately:
d.decodeSeparately(img, y, scanline)
}
}
return img, nil
}
func init() {
image.RegisterFormat("hli", header, Decode, DecodeConfig)
}