Skip to content

Commit

Permalink
Allow short palettes support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeymakinen committed Feb 17, 2022
1 parent 28cd479 commit 4ae18cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func (d *decoder) decodeSmallPaletted() (image.Image, error) {
return paletted, nil
}
// There are specified bpp bits per pixel, and each row is 4-byte aligned.
cap := 8 / int(d.bpp)
b := make([]byte, ((d.c.Width+cap-1)/cap+3)&^3)
pixelsPerByte := 8 / int(d.bpp)
b := make([]byte, ((d.c.Width+pixelsPerByte-1)/pixelsPerByte+3)&^3)
y0, y1, yDelta := d.c.Height-1, -1, -1
if d.topDown {
y0, y1, yDelta = 0, d.c.Height, +1
Expand Down
12 changes: 8 additions & 4 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,19 @@ func Encode(w io.Writer, m image.Image) error {
h.bpp = 4
default:
h.bpp = 8
step = (d.X + 3) &^ 3
}
colors := 1 << h.bpp
if len(m.Palette) < 1<<h.bpp {
colors = len(m.Palette)
h.colorUse = uint32(colors)
}
if h.bpp < 8 {
cap := 8 / int(h.bpp)
step = ((d.X+cap-1)/cap + 3) &^ 3
pixelsPerByte := 8 / int(h.bpp)
step = ((d.X+pixelsPerByte-1)/pixelsPerByte + 3) &^ 3
} else {
step = (d.X + 3) &^ 3
}
palette = make([]byte, 1<<h.bpp*4)
palette = make([]byte, colors*4)
for i := 0; i < len(m.Palette) && i < 1<<h.bpp; i++ {
r, g, b, _ := m.Palette[i].RGBA()
palette[i*4+0] = uint8(b >> 8)
Expand Down

0 comments on commit 4ae18cb

Please sign in to comment.