Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: tests/GifTest.cpp

Issue 26743002: GIF decode: optional error messages and fault tolerance. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: whitespace lint changes Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBitmap.h"
9 #include "SkData.h"
10 #include "SkForceLinking.h"
11 #include "SkImageDecoder.h"
12 #include "SkImage.h"
13 #include "SkStream.h"
14 #include "Test.h"
15
16 __SK_FORCE_IMAGE_DECODER_LINKING;
17
18 namespace {
19 unsigned char gifData[] = {
20 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00,
21 0x03, 0x00, 0xe3, 0x08, 0x00, 0x00, 0x00, 0x00,
22 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
23 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00,
24 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
25 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
27 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00,
28 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x04,
29 0x07, 0x50, 0x1c, 0x43, 0x40, 0x41, 0x23, 0x44,
30 0x00, 0x3b};
31 unsigned char gifDataNoColormap[] = {
32 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00,
33 0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04,
34 0x01, 0x0a, 0x00, 0x01, 0x00, 0x2c, 0x00, 0x00,
35 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
36 0x02, 0x4c, 0x01, 0x00, 0x3b};
37 unsigned char interlacedGif[] = {
38 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x09, 0x00,
39 0x09, 0x00, 0xe3, 0x08, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
41 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00,
42 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00,
46 0x00, 0x00, 0x09, 0x00, 0x09, 0x00, 0x40, 0x04,
47 0x1b, 0x50, 0x1c, 0x23, 0xe9, 0x44, 0x23, 0x60,
48 0x9d, 0x09, 0x28, 0x1e, 0xf8, 0x6d, 0x64, 0x56,
49 0x9d, 0x53, 0xa8, 0x7e, 0xa8, 0x65, 0x94, 0x5c,
50 0xb0, 0x8a, 0x45, 0x04, 0x00, 0x3b};
51 }; // namespace
52
53 static void testGifDataNoColormap(skiatest::Reporter* r,
scroggo 2013/10/10 20:02:12 nit: Typically we write our static functions with
54 void* data, size_t size) {
55 SkBitmap bm;
56 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
57 data, size, &bm);
58 REPORTER_ASSERT(r, imageDecodeSuccess);
59 REPORTER_ASSERT(r, bm.width() == 1);
60 REPORTER_ASSERT(r, bm.height() == 1);
61 REPORTER_ASSERT(r, !(bm.empty()));
62 if (!(bm.empty())) {
63 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0x00000000);
64 }
65 }
66 static void testGifData(skiatest::Reporter* r, void* data, size_t size) {
67 SkBitmap bm;
68 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
69 data, size, &bm);
70 REPORTER_ASSERT(r, imageDecodeSuccess);
71 REPORTER_ASSERT(r, bm.width() == 3);
72 REPORTER_ASSERT(r, bm.height() == 3);
73 REPORTER_ASSERT(r, !(bm.empty()));
74 if (!(bm.empty())) {
75 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
76 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
77 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
78 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
79 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
80 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
81 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
82 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
83 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
84 }
85 }
86 static void testInterlacedGifData(skiatest::Reporter* r,
87 void* data,
88 size_t size) {
89 SkBitmap bm;
90 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
91 data, size, &bm);
92 REPORTER_ASSERT(r, imageDecodeSuccess);
93 REPORTER_ASSERT(r, bm.width() == 9);
94 REPORTER_ASSERT(r, bm.height() == 9);
95 REPORTER_ASSERT(r, !(bm.empty()));
96 if (!(bm.empty())) {
97 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
98 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
99 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
100
101 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
102 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
103 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
104
105 REPORTER_ASSERT(r, bm.getColor(0, 4) == 0xff808080);
106 REPORTER_ASSERT(r, bm.getColor(1, 4) == 0xff000000);
107 REPORTER_ASSERT(r, bm.getColor(2, 4) == 0xff00ff00);
108
109 REPORTER_ASSERT(r, bm.getColor(0, 6) == 0xffff0000);
110 REPORTER_ASSERT(r, bm.getColor(1, 6) == 0xffffff00);
111 REPORTER_ASSERT(r, bm.getColor(2, 6) == 0xff00ffff);
112
113 REPORTER_ASSERT(r, bm.getColor(0, 8) == 0xffffffff);
114 REPORTER_ASSERT(r, bm.getColor(1, 8) == 0xffff00ff);
115 REPORTER_ASSERT(r, bm.getColor(2, 8) == 0xff0000ff);
116 }
117 }
118
119 static void testGifDataShort(skiatest::Reporter* r, void* data, size_t size) {
120 SkBitmap bm;
121 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
122 data, size, &bm);
123 REPORTER_ASSERT(r, imageDecodeSuccess);
124 REPORTER_ASSERT(r, bm.width() == 3);
125 REPORTER_ASSERT(r, bm.height() == 3);
126 REPORTER_ASSERT(r, !(bm.empty()));
127 if (!(bm.empty())) {
128 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
129 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
130 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
131 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
132 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
133 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
134 }
135 }
136
137 /**
138 This test will test the ability of the SkImageDecoder to deal with
139 GIF files which have been mangled somehow. We want to display as
140 much of the GIF as possible.
141 */
142 static void TestGif(skiatest::Reporter* reporter) {
143 // test perfectly good images.
144 testGifData(reporter, static_cast<void *>(gifData), sizeof(gifData));
145 testInterlacedGifData(reporter, static_cast<void *>(interlacedGif),
146 sizeof(interlacedGif));
147
148 unsigned char badData[sizeof(gifData)];
149
150 /* If you set the environment variable
151 skia_images_gif_suppressDecoderWarnings to 'false', you will
152 see warnings on stderr. This is a feature. */
153
154 memcpy(badData, gifData, sizeof(gifData));
155 badData[6] = 0x01; // image too wide
156 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
157 // "libgif warning [image too wide, expanding output to size]"
158
159 memcpy(badData, gifData, sizeof(gifData));
160 badData[8] = 0x01; // image too tall
161 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
162 // "libgif warning [image too tall, expanding output to size]"
163
164 memcpy(badData, gifData, sizeof(gifData));
165 badData[62] = 0x01; // image shifted right
166 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
167 // "libgif warning [shifting image left to fit]"
168
169 memcpy(badData, gifData, sizeof(gifData));
170 badData[64] = 0x01; // image shifted down
171 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
172 // "libgif warning [shifting image up to fit]"
173
174 memcpy(badData, gifData, sizeof(gifData));
175 badData[62] = 0xff; // image shifted left
176 badData[63] = 0xff; // 2's complement -1 short
177 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
178 // "libgif warning [shifting image left to fit]"
179
180 memcpy(badData, gifData, sizeof(gifData));
181 badData[64] = 0xff; // image shifted up
182 badData[65] = 0xff; // 2's complement -1 short
183 testGifData(reporter, static_cast<void *>(badData), sizeof(gifData));
184 // "libgif warning [shifting image up to fit]"
185
186 testGifDataNoColormap(reporter, static_cast<void *>(gifDataNoColormap),
187 sizeof(gifDataNoColormap));
188 // "libgif warning [missing colormap]"
189
190 // test short Gif. 80 is missing a few bytes.
191 testGifDataShort(reporter, static_cast<void *>(gifData), 80);
192 // "libgif warning [DGifGetLine]"
193
194 testInterlacedGifData(reporter, static_cast<void *>(interlacedGif),
195 100); // 100 is missing a few bytes
196 // "libgif warning [interlace DGifGetLine]"
197 }
198
199 #include "TestClassDef.h"
200 DEFINE_TESTCLASS("GifTest", GifTestClass, TestGif)
201
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698