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 <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) <nigeltao@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Nigel Tao <nigeltao@golang.org>
pull/12/merge
entooone 2023-07-09 14:16:53 +09:00 committed by Gopher Robot
parent f9550b04a5
commit a5392f068b
6 changed files with 13 additions and 3 deletions

View File

@ -191,14 +191,22 @@ func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown b
}
switch bpp {
case 8:
if offset != fileHeaderLen+infoLen+256*4 {
colorUsed := readUint32(b[46:50])
// If colorUsed is 0, it is set to the maximum number of colors for the given bpp, which is 2^bpp.
if colorUsed == 0 {
colorUsed = 256
} else if colorUsed > 256 {
return image.Config{}, 0, false, false, ErrUnsupported
}
_, err = io.ReadFull(r, b[:256*4])
if offset != fileHeaderLen+infoLen+colorUsed*4 {
return image.Config{}, 0, false, false, ErrUnsupported
}
_, err = io.ReadFull(r, b[:colorUsed*4])
if err != nil {
return image.Config{}, 0, false, false, err
}
pcm := make(color.Palette, 256)
pcm := make(color.Palette, colorUsed)
for i := range pcm {
// BMP images are stored in BGR order rather than RGB order.
// Every 4th byte is padding.

View File

@ -41,6 +41,8 @@ func compare(img0, img1 image.Image) error {
func TestDecode(t *testing.T) {
testCases := []string{
"colormap",
"colormap-0",
"colormap-251",
"video-001",
"yellow_rose-small",
"yellow_rose-small-v5",

BIN
testdata/colormap-0.bmp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
testdata/colormap-0.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
testdata/colormap-251.bmp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
testdata/colormap-251.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB