-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbmpRLE4.go
246 lines (237 loc) · 7.06 KB
/
bmpRLE4.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// bmprle4.go
package bmp
import (
"bufio"
//"bytes"
//"fmt"
"image"
"image/color"
"io"
"log"
//"os"
)
func unPack2(b byte) [2]byte {
var x [2]byte
x[0] = (b >> 4) & 0x0f
x[1] = b & 0xf
return x
}
func unwindRLE4(r io.Reader, b *BMP_T) ([]byte, error) {
maxReadBytes := len(b.aBitMapBits)
verbose.Printf("Entry to unwindRLE4\n")
verbose.Printf("Height = %d Width = %d H*W = %d\n",
b.Infoheader.Height, b.Infoheader.Width, b.Infoheader.Height*b.Infoheader.Width)
verbose.Printf("maxBits = %d\n", maxReadBytes)
rowWidth := b.Infoheader.Width
if (rowWidth % 2) != 0 {
rowWidth++
}
// use uncompressed size/2 as rough cap for pixMap
pixMap := make([]byte, 0, b.Infoheader.Height*rowWidth/2)
verbose.Printf("PixMap will be [%d] when full\n", b.Infoheader.Height*rowWidth/2)
br := bufio.NewReader(r)
//BOF := b.fileheader.bfOffsetBits % 4
bytesRead := 0
lineCt := 0
verbose.Printf("len(pixMap)=%d cap(pixMap)=%d\n", len(pixMap), cap(pixMap))
for {
if len(pixMap) == cap(pixMap) {
break
}
if bytesRead >= maxReadBytes { // don't read past source end
break
}
numPix, err := br.ReadByte()
if err != nil {
log.Printf("bmp: bad read in RLE4\n")
return nil, err
} else {
bytesRead++
}
verbose.Printf("byte at hexoffset(%x), numPix(%x)\n", bytesRead, numPix)
pixVal, err := br.ReadByte()
if err != nil {
log.Printf("bmp: bad read in RLE4\n")
return nil, err
} else {
bytesRead++
}
verbose.Printf("byte at hexoffset(%x), pixVal(%x)\n", bytesRead, pixVal)
if numPix > 0 { // encoded mode
//verbose.Printf("copying %d encoded pixels\n", numPix)
loopCt := numPix / 2
loopXtra := numPix - (loopCt * 2)
for x := 0; x < int(loopCt); x++ {
pixMap = append(pixMap, pixVal)
}
if loopXtra != 0 {
pixMap = append(pixMap, pixVal&0xf0)
}
continue
} else { // absolute mode if numPix == 0, can be escaped if second byte 0..2
if inRangeByte(0, pixVal, 2) { // check for escaped mode
switch pixVal {
case 0: // end of line must be on DWORD boundary counting from BOF, not BOpixmap
for {
if (len(pixMap) % 4) == 0 {
break
}
//verbose.Printf("Padding line with 0\n")
pixMap = append(pixMap, 0)
}
verbose.Printf("end of line signal found ")
lineCt++
verbose.Printf("len(pixMap)=%d cap(pixMap)=%d bytesRead(%d) lineCt(%d)\n", len(pixMap), cap(pixMap), bytesRead, lineCt)
continue
case 1: // end of bitmap
verbose.Printf("end of pixmap source signal found ")
lineCt++
verbose.Printf("len(pixMap)=%d cap(pixMap)=%d bytesRead(%d) lineCt(%d)\n", len(pixMap), cap(pixMap), bytesRead, lineCt)
goto xit
case 2: // Delta
// BUG(mdr): TODO - delta encoding not handled in unwindRLE4
log.Printf("Delta value found but no delta handler available\n")
return nil, ErrNoDelta
deltax, err := br.ReadByte()
deltay, err := br.ReadByte()
// LINT req - leave here in case we build out the delta code later
deltax = deltax
deltay = deltay
err = err
bytesRead += 2
// need some magic here to advance over part of image (why would it be used?)
}
log.Printf("can't happen\n")
return nil, ErrCantHappen
}
numPix = pixVal
verbose.Printf("copying %d absolute pixels\n", numPix)
loopCt := numPix / 2
loopXtra := numPix - (loopCt * 2)
for x := 0; x < int(loopCt); x++ {
pixVal, err := br.ReadByte()
if err != nil {
verbose.Printf("bytesRead(%d)\n", bytesRead)
log.Printf("bmp: bad read in RLE4\n")
return nil, err
} else {
bytesRead++
}
pixMap = append(pixMap, pixVal)
}
if loopXtra != 0 {
pixMap = append(pixMap, pixVal&0xf0)
}
if (bytesRead % 2) != 0 { // absolute run must end at word boundary
_, err := br.ReadByte()
if err != nil {
verbose.Printf("bytesRead(%d)\n", bytesRead)
log.Printf("bmp: bad read in RLE4\n")
return nil, err
} else {
bytesRead++
}
}
}
verbose.Printf("pixMap[%d] bytesRead(%d)\n", len(pixMap), bytesRead)
}
xit:
verbose.Printf("len(pixMap)=%d cap(pixMap)=%d bytesRead(%d) lineCt(%d)\n", len(pixMap), cap(pixMap), bytesRead, lineCt)
if len(pixMap) != cap(pixMap) {
verbose.Printf("!Err-> mismatched len & cap - short by(%d)bytes is bad\n", cap(pixMap)-len(pixMap))
}
if bytesRead != len(b.aBitMapBits) {
verbose.Printf("!Err-> mismatched len(source) & bytesRead is bad\n")
verbose.Printf("bytesRead is %d but should be %d\n", bytesRead, len(b.aBitMapBits))
}
// BUG(mdr): OVERKILL? - we fill out pixmap with null bytes if end of source data before map is full
for {
if len(pixMap) >= cap(pixMap) {
break
}
//verbose.Printf("padding map at exit\n")
pixMap = append(pixMap, 0x0)
}
b.aBitMapBits = pixMap
verbose.Printf("pixMap[%d] bytesRead(%d)\n", len(pixMap), bytesRead)
verbose.Printf("Exit unwindRLE4() \n")
return pixMap, nil
}
// decodePaletted reads a 4 bit-per-pixel BMP image from r.
func decodePaletted4(r io.Reader, c image.Config, b *BMP_T) (image.Image, error) {
maxBits := len(b.aBitMapBits)
verbose.Printf("Entry to decodePaletted4\n")
paletted := image.NewPaletted(image.Rect(0, 0, c.Width, c.Height), c.ColorModel.(color.Palette))
br := bufio.NewReader(r)
var bytesRead int
verbose.Printf("Height = %d Width = %d H*W = %d\n", c.Height, c.Width, c.Height*c.Width)
verbose.Printf("maxBits = %d\n", maxBits)
verbose.Printf("paletted.Stride(%d)\n", paletted.Stride)
lastPix := c.Height * c.Width
rowWidth := c.Width / 2
rowXtra := c.Width - (rowWidth * 2)
// N.B. BMP images are stored bottom-up rather than top-down, left to right
for y := c.Height - 1; y >= 0; y-- {
var pix2 byte
var err error
var start, finish int
for x := 0; x < rowWidth*2; x += 2 {
if bytesRead >= maxBits {
break
}
pix2, err := br.ReadByte()
if err != nil {
log.Printf("bmp: bad read in Pal4\n")
return nil, err
}
bytesRead++
b := unPack2(pix2)
start := x + (y * c.Width)
finish := start + 2
if finish > lastPix {
finish = lastPix
}
if start > lastPix {
start = lastPix
}
//verbose.Printf("start(%d) finish(%d) byte[%d] %v\n", start, finish, bytesRead, b)
copy(paletted.Pix[start:finish], b[:])
}
// last byte of scanline may not have all bits used so piece it out
if rowXtra != 0 {
//verbose.Printf("adding last pixel to line\n")
pix2, err = br.ReadByte()
if err != nil {
log.Printf("bmp: bad read in Pal4\n")
return nil, err
}
bytesRead++
b := unPack2(pix2)
start += 2
finish = start + rowXtra
if finish > lastPix {
verbose.Printf("LastPix\n")
finish = lastPix
}
if start > lastPix {
start = lastPix
}
//verbose.Printf("+start(%d) finish(%d) byte[%d] %v\n", start, finish, bytesRead, b)
copy(paletted.Pix[start:finish], b[:rowXtra])
}
// scanlines are padded if necessary to multiple of uint32 (DWORD)
for {
if (bytesRead % 4) == 0 {
break
}
pix2, err = br.ReadByte()
if err != nil {
log.Printf("bmp: bad read in Pal4\n")
return nil, err
}
bytesRead++
verbose.Printf("byte[%d]\n", bytesRead)
}
}
return paletted, nil
}