@@ -22,12 +22,16 @@ const (
22
22
trailerStr = "\x3b "
23
23
)
24
24
25
- // lzwEncode returns an LZW encoding (with 2-bit literals) of n zeroes .
26
- func lzwEncode (n int ) []byte {
25
+ // lzwEncode returns an LZW encoding (with 2-bit literals) of in .
26
+ func lzwEncode (in [] byte ) []byte {
27
27
b := & bytes.Buffer {}
28
28
w := lzw .NewWriter (b , lzw .LSB , 2 )
29
- w .Write (make ([]byte , n ))
30
- w .Close ()
29
+ if _ , err := w .Write (in ); err != nil {
30
+ panic (err )
31
+ }
32
+ if err := w .Close (); err != nil {
33
+ panic (err )
34
+ }
31
35
return b .Bytes ()
32
36
}
33
37
@@ -53,7 +57,7 @@ func TestDecode(t *testing.T) {
53
57
// byte, and 2-bit LZW literals.
54
58
b .WriteString ("\x2c \x00 \x00 \x00 \x00 \x02 \x00 \x01 \x00 \x00 \x02 " )
55
59
if tc .nPix > 0 {
56
- enc := lzwEncode (tc .nPix )
60
+ enc := lzwEncode (make ([] byte , tc .nPix ) )
57
61
if len (enc ) > 0xff {
58
62
t .Errorf ("nPix=%d, extra=%t: compressed length %d is too large" , tc .nPix , tc .extra , len (enc ))
59
63
continue
@@ -103,7 +107,7 @@ func TestTransparentIndex(t *testing.T) {
103
107
}
104
108
// Write an image with bounds 2x1, as per TestDecode.
105
109
b .WriteString ("\x2c \x00 \x00 \x00 \x00 \x02 \x00 \x01 \x00 \x00 \x02 " )
106
- enc := lzwEncode (2 )
110
+ enc := lzwEncode ([] byte { 0x00 , 0x00 } )
107
111
if len (enc ) > 0xff {
108
112
t .Fatalf ("compressed length %d is too large" , len (enc ))
109
113
}
@@ -196,21 +200,13 @@ func TestNoPalette(t *testing.T) {
196
200
b .WriteString (headerStr [:len (headerStr )- 3 ])
197
201
b .WriteString ("\x00 \x00 \x00 " ) // No global palette.
198
202
199
- // Image descriptor: 2x1, no local palette.
203
+ // Image descriptor: 2x1, no local palette, and 2-bit LZW literals .
200
204
b .WriteString ("\x2c \x00 \x00 \x00 \x00 \x02 \x00 \x01 \x00 \x00 \x02 " )
201
205
202
206
// Encode the pixels: neither is in range, because there is no palette.
203
- pix := []byte {0 , 3 }
204
- enc := & bytes.Buffer {}
205
- w := lzw .NewWriter (enc , lzw .LSB , 2 )
206
- if _ , err := w .Write (pix ); err != nil {
207
- t .Fatalf ("Write: %v" , err )
208
- }
209
- if err := w .Close (); err != nil {
210
- t .Fatalf ("Close: %v" , err )
211
- }
212
- b .WriteByte (byte (len (enc .Bytes ())))
213
- b .Write (enc .Bytes ())
207
+ enc := lzwEncode ([]byte {0x00 , 0x03 })
208
+ b .WriteByte (byte (len (enc )))
209
+ b .Write (enc )
214
210
b .WriteByte (0x00 ) // An empty block signifies the end of the image data.
215
211
216
212
b .WriteString (trailerStr )
@@ -226,21 +222,13 @@ func TestPixelOutsidePaletteRange(t *testing.T) {
226
222
b .WriteString (headerStr )
227
223
b .WriteString (paletteStr )
228
224
229
- // Image descriptor: 2x1, no local palette.
225
+ // Image descriptor: 2x1, no local palette, and 2-bit LZW literals .
230
226
b .WriteString ("\x2c \x00 \x00 \x00 \x00 \x02 \x00 \x01 \x00 \x00 \x02 " )
231
227
232
228
// Encode the pixels; some pvals trigger the expected error.
233
- pix := []byte {pval , pval }
234
- enc := & bytes.Buffer {}
235
- w := lzw .NewWriter (enc , lzw .LSB , 2 )
236
- if _ , err := w .Write (pix ); err != nil {
237
- t .Fatalf ("Write: %v" , err )
238
- }
239
- if err := w .Close (); err != nil {
240
- t .Fatalf ("Close: %v" , err )
241
- }
242
- b .WriteByte (byte (len (enc .Bytes ())))
243
- b .Write (enc .Bytes ())
229
+ enc := lzwEncode ([]byte {pval , pval })
230
+ b .WriteByte (byte (len (enc )))
231
+ b .Write (enc )
244
232
b .WriteByte (0x00 ) // An empty block signifies the end of the image data.
245
233
246
234
b .WriteString (trailerStr )
@@ -254,6 +242,36 @@ func TestPixelOutsidePaletteRange(t *testing.T) {
254
242
}
255
243
}
256
244
245
+ func TestTransparentPixelOutsidePaletteRange (t * testing.T ) {
246
+ b := & bytes.Buffer {}
247
+
248
+ // Manufacture a GIF with a 2 color palette.
249
+ b .WriteString (headerStr )
250
+ b .WriteString (paletteStr )
251
+
252
+ // Graphic Control Extension: transparency, transparent color index = 3.
253
+ //
254
+ // This index, 3, is out of range of the global palette and there is no
255
+ // local palette in the subsequent image descriptor. This is an error
256
+ // according to the spec, but Firefox and Google Chrome seem OK with this.
257
+ //
258
+ // See golang.org/issue/15059.
259
+ b .WriteString ("\x21 \xf9 \x04 \x01 \x00 \x00 \x03 \x00 " )
260
+
261
+ // Image descriptor: 2x1, no local palette, and 2-bit LZW literals.
262
+ b .WriteString ("\x2c \x00 \x00 \x00 \x00 \x02 \x00 \x01 \x00 \x00 \x02 " )
263
+
264
+ // Encode the pixels.
265
+ enc := lzwEncode ([]byte {0x03 , 0x03 })
266
+ b .WriteByte (byte (len (enc )))
267
+ b .Write (enc )
268
+ b .WriteByte (0x00 ) // An empty block signifies the end of the image data.
269
+
270
+ b .WriteString (trailerStr )
271
+
272
+ try (t , b .Bytes (), "" )
273
+ }
274
+
257
275
func TestLoopCount (t * testing.T ) {
258
276
data := []byte ("GIF89a000\x00 000,0\x00 \x00 \x00 \n \x00 " +
259
277
"\n \x00 \x80 000000\x02 \b \xf0 1u\xb9 \xfd al\x05 \x00 ;" )
0 commit comments