Skip to content

Commit a5392f0

Browse files
entooonegopherbot
authored andcommitted
bmp: support to decode 8-bit format with up to 256 color palette
If colorUsed is 0, the number of palette is 2 to the power of bit per pixel. testdata/colormap-251.{bmp,png} are added for testing 8-bit format with colorUsed less than 256. testdata/colormap-0.{bmp,png} are added for testing 8-bit format with colorUsed 0. Fixes golang/go#61240 Change-Id: I1a627a570f667874a91c517f4a771e9e97d2af6b Reviewed-on: https://go-review.googlesource.com/c/image/+/508575 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Nigel Tao <[email protected]> Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Run-TryBot: Nigel Tao <[email protected]>
1 parent f9550b0 commit a5392f0

File tree

6 files changed

+13
-3
lines changed

6 files changed

+13
-3
lines changed

bmp/reader.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,22 @@ func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown b
191191
}
192192
switch bpp {
193193
case 8:
194-
if offset != fileHeaderLen+infoLen+256*4 {
194+
colorUsed := readUint32(b[46:50])
195+
// If colorUsed is 0, it is set to the maximum number of colors for the given bpp, which is 2^bpp.
196+
if colorUsed == 0 {
197+
colorUsed = 256
198+
} else if colorUsed > 256 {
195199
return image.Config{}, 0, false, false, ErrUnsupported
196200
}
197-
_, err = io.ReadFull(r, b[:256*4])
201+
202+
if offset != fileHeaderLen+infoLen+colorUsed*4 {
203+
return image.Config{}, 0, false, false, ErrUnsupported
204+
}
205+
_, err = io.ReadFull(r, b[:colorUsed*4])
198206
if err != nil {
199207
return image.Config{}, 0, false, false, err
200208
}
201-
pcm := make(color.Palette, 256)
209+
pcm := make(color.Palette, colorUsed)
202210
for i := range pcm {
203211
// BMP images are stored in BGR order rather than RGB order.
204212
// Every 4th byte is padding.

bmp/reader_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func compare(img0, img1 image.Image) error {
4141
func TestDecode(t *testing.T) {
4242
testCases := []string{
4343
"colormap",
44+
"colormap-0",
45+
"colormap-251",
4446
"video-001",
4547
"yellow_rose-small",
4648
"yellow_rose-small-v5",

testdata/colormap-0.bmp

16.4 KB
Binary file not shown.

testdata/colormap-0.png

12.3 KB
Loading

testdata/colormap-251.bmp

16.4 KB
Binary file not shown.

testdata/colormap-251.png

12 KB
Loading

0 commit comments

Comments
 (0)