1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"image"
"io/ioutil"
"os"
"strings"
"testing"
_ "image/png"
)
const testdataDir = "../testdata/"
// Read makes *buffer implements io.Reader, so that we can pass one to Decode.
func (*buffer) Read([]byte) (int, error) {
panic("unimplemented")
}
func load(name string) (image.Image, error) {
f, err := os.Open(testdataDir + name)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
// TestNoRPS tests decoding an image that has no RowsPerStrip tag. The tag is
// mandatory according to the spec but some software omits it in the case of a
// single strip.
func TestNoRPS(t *testing.T) {
_, err := load("no_rps.tiff")
if err != nil {
t.Fatal(err)
}
}
// TestNoCompression tests decoding an image that has no Compression tag. This
// tag is mandatory, but most tools interpret a missing value as no
// compression.
func TestNoCompression(t *testing.T) {
_, err := load("no_compress.tiff")
if err != nil {
t.Fatal(err)
}
}
// TestUnpackBits tests the decoding of PackBits-encoded data.
func TestUnpackBits(t *testing.T) {
var unpackBitsTests = []struct {
compressed string
uncompressed string
}{{
// Example data from Wikipedia.
"\xfe\xaa\x02\x80\x00\x2a\xfd\xaa\x03\x80\x00\x2a\x22\xf7\xaa",
"\xaa\xaa\xaa\x80\x00\x2a\xaa\xaa\xaa\xaa\x80\x00\x2a\x22\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
}}
for _, u := range unpackBitsTests {
buf, err := unpackBits(strings.NewReader(u.compressed))
if err != nil {
t.Fatal(err)
}
if string(buf) != u.uncompressed {
t.Fatalf("unpackBits: want %x, got %x", u.uncompressed, buf)
}
}
}
func TestShortBlockData(t *testing.T) {
b, err := ioutil.ReadFile("../testdata/bw-uncompressed.tiff")
if err != nil {
t.Fatal(err)
}
// The bw-uncompressed.tiff image is a 153x55 bi-level image. This is 1 bit
// per pixel, or 20 bytes per row, times 55 rows, or 1100 bytes of pixel
// data. 1100 in hex is 0x44c, or "\x4c\x04" in little-endian. We replace
// that byte count (StripByteCounts-tagged data) by something less than
// that, so that there is not enough pixel data.
old := []byte{0x4c, 0x04}
new := []byte{0x01, 0x01}
i := bytes.Index(b, old)
if i < 0 {
t.Fatal(`could not find "\x4c\x04" byte count`)
}
if bytes.Contains(b[i+len(old):], old) {
t.Fatal(`too many occurrences of "\x4c\x04"`)
}
b[i+0] = new[0]
b[i+1] = new[1]
if _, err = Decode(bytes.NewReader(b)); err == nil {
t.Fatal("got nil error, want non-nil")
}
}
func TestDecodeInvalidDataType(t *testing.T) {
b, err := ioutil.ReadFile("../testdata/bw-uncompressed.tiff")
if err != nil {
t.Fatal(err)
}
// off is the offset of the ImageWidth tag. It is the offset of the overall
// IFD block (0x00000454), plus 2 for the uint16 number of IFD entries, plus 12
// to skip the first entry.
const off = 0x00000454 + 2 + 12*1
if v := binary.LittleEndian.Uint16(b[off : off+2]); v != tImageWidth {
t.Fatal(`could not find ImageWidth tag`)
}
binary.LittleEndian.PutUint16(b[off+2:], uint16(len(lengths))) // invalid datatype
if _, err = Decode(bytes.NewReader(b)); err == nil {
t.Fatal("got nil error, want non-nil")
}
}
func compare(t *testing.T, img0, img1 image.Image) {
b0 := img0.Bounds()
b1 := img1.Bounds()
if b0.Dx() != b1.Dx() || b0.Dy() != b1.Dy() {
t.Fatalf("wrong image size: want %s, got %s", b0, b1)
}
x1 := b1.Min.X - b0.Min.X
y1 := b1.Min.Y - b0.Min.Y
for y := b0.Min.Y; y < b0.Max.Y; y++ {
for x := b0.Min.X; x < b0.Max.X; x++ {
c0 := img0.At(x, y)
c1 := img1.At(x+x1, y+y1)
r0, g0, b0, a0 := c0.RGBA()
r1, g1, b1, a1 := c1.RGBA()
if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
t.Fatalf("pixel at (%d, %d) has wrong color: want %v, got %v", x, y, c0, c1)
}
}
}
}
// TestDecode tests that decoding a PNG image and a TIFF image result in the
// same pixel data.
func TestDecode(t *testing.T) {
img0, err := load("video-001.png")
if err != nil {
t.Fatal(err)
}
img1, err := load("video-001.tiff")
if err != nil {
t.Fatal(err)
}
img2, err := load("video-001-strip-64.tiff")
if err != nil {
t.Fatal(err)
}
img3, err := load("video-001-tile-64x64.tiff")
if err != nil {
t.Fatal(err)
}
img4, err := load("video-001-16bit.tiff")
if err != nil {
t.Fatal(err)
}
compare(t, img0, img1)
compare(t, img0, img2)
compare(t, img0, img3)
compare(t, img0, img4)
}
// TestDecodeLZW tests that decoding a PNG image and a LZW-compressed TIFF
// image result in the same pixel data.
func TestDecodeLZW(t *testing.T) {
img0, err := load("blue-purple-pink.png")
if err != nil {
t.Fatal(err)
}
img1, err := load("blue-purple-pink.lzwcompressed.tiff")
if err != nil {
t.Fatal(err)
}
compare(t, img0, img1)
}
// TestDecodeTagOrder tests that a malformed image with unsorted IFD entries is
// correctly rejected.
func TestDecodeTagOrder(t *testing.T) {
data, err := ioutil.ReadFile("../testdata/video-001.tiff")
if err != nil {
t.Fatal(err)
}
// Swap the first two IFD entries.
ifdOffset := int64(binary.LittleEndian.Uint32(data[4:8]))
for i := ifdOffset + 2; i < ifdOffset+14; i++ {
data[i], data[i+12] = data[i+12], data[i]
}
if _, _, err := image.Decode(bytes.NewReader(data)); err == nil {
t.Fatal("got nil error, want non-nil")
}
}
// TestDecompress tests that decoding some TIFF images that use different
// compression formats result in the same pixel data.
func TestDecompress(t *testing.T) {
var decompressTests = []string{
"bw-uncompressed.tiff",
"bw-deflate.tiff",
"bw-packbits.tiff",
}
var img0 image.Image
for _, name := range decompressTests {
img1, err := load(name)
if err != nil {
t.Fatalf("decoding %s: %v", name, err)
}
if img0 == nil {
img0 = img1
continue
}
compare(t, img0, img1)
}
}
func replace(src []byte, find, repl string) ([]byte, error) {
removeSpaces := func(r rune) rune {
if r != ' ' {
return r
}
return -1
}
f, err := hex.DecodeString(strings.Map(removeSpaces, find))
if err != nil {
return nil, err
}
r, err := hex.DecodeString(strings.Map(removeSpaces, repl))
if err != nil {
return nil, err
}
dst := bytes.Replace(src, f, r, 1)
if bytes.Equal(dst, src) {
return nil, errors.New("replacement failed")
}
return dst, nil
}
// TestZeroBitsPerSample tests that an IFD with a bitsPerSample of 0 does not
// cause a crash.
// Issue 10711.
func TestZeroBitsPerSample(t *testing.T) {
b0, err := ioutil.ReadFile(testdataDir + "bw-deflate.tiff")
if err != nil {
t.Fatal(err)
}
// Mutate the loaded image to have the problem.
// 02 01: tag number (tBitsPerSample)
// 03 00: data type (short, or uint16)
// 01 00 00 00: count
// ?? 00 00 00: value (1 -> 0)
b1, err := replace(b0,
"02 01 03 00 01 00 00 00 01 00 00 00",
"02 01 03 00 01 00 00 00 00 00 00 00",
)
if err != nil {
t.Fatal(err)
}
_, err = Decode(bytes.NewReader(b1))
if err == nil {
t.Fatal("Decode with 0 bits per sample: got nil error, want non-nil")
}
}
// TestTileTooBig tests that we do not panic when a tile is too big compared to
// the data available.
// Issue 10712
func TestTileTooBig(t *testing.T) {
b0, err := ioutil.ReadFile(testdataDir + "video-001-tile-64x64.tiff")
if err != nil {
t.Fatal(err)
}
// Mutate the loaded image to have the problem.
//
// 42 01: tag number (tTileWidth)
// 03 00: data type (short, or uint16)
// 01 00 00 00: count
// xx 00 00 00: value (0x40 -> 0x44: a wider tile consumes more data
// than is available)
b1, err := replace(b0,
"42 01 03 00 01 00 00 00 40 00 00 00",
"42 01 03 00 01 00 00 00 44 00 00 00",
)
if err != nil {
t.Fatal(err)
}
// Turn off the predictor, which makes it possible to hit the
// place with the defect. Without this patch to the image, we run
// out of data too early, and do not hit the part of the code where
// the original panic was.
//
// 3d 01: tag number (tPredictor)
// 03 00: data type (short, or uint16)
// 01 00 00 00: count
// xx 00 00 00: value (2 -> 1: 2 = horizontal, 1 = none)
b2, err := replace(b1,
"3d 01 03 00 01 00 00 00 02 00 00 00",
"3d 01 03 00 01 00 00 00 01 00 00 00",
)
if err != nil {
t.Fatal(err)
}
_, err = Decode(bytes.NewReader(b2))
if err == nil {
t.Fatal("did not expect nil error")
}
}
// TestZeroSizedImages tests that decoding does not panic when image dimensions
// are zero, and returns a zero-sized image instead.
// Issue 10393.
func TestZeroSizedImages(t *testing.T) {
testsizes := []struct {
w, h int
}{
{0, 0},
{1, 0},
{0, 1},
{1, 1},
}
for _, r := range testsizes {
img := image.NewRGBA(image.Rect(0, 0, r.w, r.h))
var buf bytes.Buffer
if err := Encode(&buf, img, nil); err != nil {
t.Errorf("encode w=%d h=%d: %v", r.w, r.h, err)
continue
}
if _, err := Decode(&buf); err != nil {
t.Errorf("decode w=%d h=%d: %v", r.w, r.h, err)
}
}
}
// TestLargeIFDEntry tests that a large IFD entry does not cause Decode to
// panic.
// Issue 10596.
func TestLargeIFDEntry(t *testing.T) {
testdata := "II*\x00\x08\x00\x00\x00\f\x000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"00000000000000\x17\x01\x04\x00\x01\x00" +
"\x00\xc0000000000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"000000"
_, err := Decode(strings.NewReader(testdata))
if err == nil {
t.Fatal("Decode with large IFD entry: got nil error, want non-nil")
}
}
// benchmarkDecode benchmarks the decoding of an image.
func benchmarkDecode(b *testing.B, filename string) {
b.StopTimer()
contents, err := ioutil.ReadFile(testdataDir + filename)
if err != nil {
b.Fatal(err)
}
r := &buffer{buf: contents}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := Decode(r)
if err != nil {
b.Fatal("Decode:", err)
}
}
}
func BenchmarkDecodeCompressed(b *testing.B) { benchmarkDecode(b, "video-001.tiff") }
func BenchmarkDecodeUncompressed(b *testing.B) { benchmarkDecode(b, "video-001-uncompressed.tiff") }
|