Commit a6f95ad3 authored by Andrew Bonventre's avatar Andrew Bonventre Committed by Nigel Tao

image/gif: don't write superfluous global color table

R=r, nigeltao
CC=golang-dev
https://golang.org/cl/11446043
parent c5c52f07
...@@ -52,9 +52,6 @@ type encoder struct { ...@@ -52,9 +52,6 @@ type encoder struct {
err error err error
// g is a reference to the data that is being encoded. // g is a reference to the data that is being encoded.
g *GIF g *GIF
// bitsPerPixel is the number of bits required to represent each color
// in the image.
bitsPerPixel int
// buf is a scratch buffer. It must be at least 768 so we can write the color map. // buf is a scratch buffer. It must be at least 768 so we can write the color map.
buf [1024]byte buf [1024]byte
} }
...@@ -118,23 +115,19 @@ func (e *encoder) writeHeader() { ...@@ -118,23 +115,19 @@ func (e *encoder) writeHeader() {
return return
} }
// TODO: This bases the global color table on the first image
// only.
pm := e.g.Image[0] pm := e.g.Image[0]
// Logical screen width and height. // Logical screen width and height.
writeUint16(e.buf[0:2], uint16(pm.Bounds().Dx())) writeUint16(e.buf[0:2], uint16(pm.Bounds().Dx()))
writeUint16(e.buf[2:4], uint16(pm.Bounds().Dy())) writeUint16(e.buf[2:4], uint16(pm.Bounds().Dy()))
e.write(e.buf[:4]) e.write(e.buf[:4])
e.bitsPerPixel = log2(len(pm.Palette)) + 1 // All frames have a local color table, so a global color table
e.buf[0] = 0x80 | ((uint8(e.bitsPerPixel) - 1) << 4) | (uint8(e.bitsPerPixel) - 1) // is not needed.
e.buf[0] = 0x00
e.buf[1] = 0x00 // Background Color Index. e.buf[1] = 0x00 // Background Color Index.
e.buf[2] = 0x00 // Pixel Aspect Ratio. e.buf[2] = 0x00 // Pixel Aspect Ratio.
e.write(e.buf[:3]) e.write(e.buf[:3])
// Global Color Table.
e.writeColorTable(pm.Palette, e.bitsPerPixel-1)
// Add animation info if necessary. // Add animation info if necessary.
if len(e.g.Image) > 1 { if len(e.g.Image) > 1 {
e.buf[0] = 0x21 // Extension Introducer. e.buf[0] = 0x21 // Extension Introducer.
...@@ -232,7 +225,7 @@ func (e *encoder) writeImageBlock(pm *image.Paletted, delay int) { ...@@ -232,7 +225,7 @@ func (e *encoder) writeImageBlock(pm *image.Paletted, delay int) {
// Local Color Table. // Local Color Table.
e.writeColorTable(pm.Palette, paddedSize) e.writeColorTable(pm.Palette, paddedSize)
litWidth := e.bitsPerPixel litWidth := paddedSize + 1
if litWidth < 2 { if litWidth < 2 {
litWidth = 2 litWidth = 2
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment