Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golang/image: support small palleted for BMP image #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions bmp/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ func readUint32(b []byte) uint32 {
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}

// decodeSmallPaletted reads an 1, 2, 4 bit-per-pixel BMP image from r.
// If topDown is false, the image rows will be read bottom-up.
func decodeSmallPaletted(r io.Reader, c image.Config, topDown bool, bpp int) (image.Image, error) {
paletted := image.NewPaletted(image.Rect(0, 0, c.Width, c.Height), c.ColorModel.(color.Palette))
if c.Width == 0 || c.Height == 0 {
return paletted, nil
}
y0, y1, yDelta := c.Height-1, -1, -1
if topDown {
y0, y1, yDelta = 0, c.Height, +1
}

pixelsPerByte := 8 / bpp
// pad up to ensure each row is 4-bytes aligned
bytesPerRow := ((c.Width+pixelsPerByte-1)/pixelsPerByte + 3) &^ 3
b := make([]byte, bytesPerRow)

for y := y0; y != y1; y += yDelta {
p := paletted.Pix[y*paletted.Stride : y*paletted.Stride+c.Width]
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
byteIndex := 0
bitIndex := 8 - bpp
for pixIndex := 0; pixIndex < c.Width; pixIndex++ {
p[pixIndex] = (b[byteIndex] >> bitIndex) & (1<<bpp - 1)
if bitIndex == 0 {
byteIndex++
bitIndex = 8 - bpp
} else {
bitIndex -= bpp
}
}
}
return paletted, nil
}

// decodePaletted reads an 8 bit-per-pixel BMP image from r.
// If topDown is false, the image rows will be read bottom-up.
func decodePaletted(r io.Reader, c image.Config, topDown bool) (image.Image, error) {
Expand Down Expand Up @@ -118,6 +155,8 @@ func Decode(r io.Reader) (image.Image, error) {
return nil, err
}
switch bpp {
case 1, 2, 4:
return decodeSmallPaletted(r, c, topDown, bpp)
case 8:
return decodePaletted(r, c, topDown)
case 24:
Expand Down Expand Up @@ -190,6 +229,30 @@ func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown b
return image.Config{}, 0, false, false, ErrUnsupported
}
switch bpp {
case 1, 2, 4:
colorUsed := readUint32(b[46:50])

if colorUsed != 0 && colorUsed > (1<<bpp) {
return image.Config{}, 0, false, false, ErrUnsupported
}
if colorUsed == 0 {
colorUsed = 1 << bpp
}

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, colorUsed)
for i := range pcm {
// BMP images are stored in BGR order rather than RGB order.
// Every 4th byte is padding.
pcm[i] = color.RGBA{b[4*i+2], b[4*i+1], b[4*i+0], 0xFF}
}
return image.Config{ColorModel: pcm, Width: width, Height: height}, int(bpp), topDown, false, nil
case 8:
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.
Expand Down
2 changes: 2 additions & 0 deletions bmp/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func TestDecode(t *testing.T) {
"video-001",
"yellow_rose-small",
"yellow_rose-small-v5",
"bmp_1bpp",
"bmp_4bpp",
}

for _, tc := range testCases {
Expand Down
Binary file added testdata/bmp_1bpp.bmp
Binary file not shown.
Binary file added testdata/bmp_1bpp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/bmp_4bpp.bmp
Binary file not shown.
Binary file added testdata/bmp_4bpp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.