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

Side by Side Diff: tests/GifTest.cpp

Issue 1757333002: Fix bug in SkGifCodec / Switch SkImageDec tests to use Codec (Closed) Base URL: https://skia.googlesource.com/skia.git@m50
Patch Set: Created 4 years, 9 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
« no previous file with comments | « tests/FrontBufferedStreamTest.cpp ('k') | tests/IndexedPngOverflowTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkTypes.h" 8 #include "CodecPriv.h"
9
10 // This tests out GIF decoder (SkImageDecoder_libgif.cpp)
11 // It is not used on these platforms:
12 #if (!defined(SK_BUILD_FOR_WIN32)) && \
13 (!defined(SK_BUILD_FOR_IOS)) && \
14 (!defined(SK_BUILD_FOR_MAC))
15
16 #include "Resources.h" 9 #include "Resources.h"
10 #include "SkAndroidCodec.h"
17 #include "SkBitmap.h" 11 #include "SkBitmap.h"
18 #include "SkData.h" 12 #include "SkData.h"
19 #include "SkForceLinking.h"
20 #include "SkImage.h" 13 #include "SkImage.h"
21 #include "SkImageDecoder.h"
22 #include "SkStream.h" 14 #include "SkStream.h"
15 #include "SkTypes.h"
23 #include "Test.h" 16 #include "Test.h"
24 17
25 __SK_FORCE_IMAGE_DECODER_LINKING;
26
27 static unsigned char gGIFData[] = { 18 static unsigned char gGIFData[] = {
28 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00, 0x03, 0x00, 0xe3, 0x08, 19 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00, 0x03, 0x00, 0xe3, 0x08,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
30 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 21 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
31 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 22 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 23 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x04, 24 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x04,
34 0x07, 0x50, 0x1c, 0x43, 0x40, 0x41, 0x23, 0x44, 0x00, 0x3b 25 0x07, 0x50, 0x1c, 0x43, 0x40, 0x41, 0x23, 0x44, 0x00, 0x3b
35 }; 26 };
36 27
(...skipping 11 matching lines...) Expand all
48 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00,
49 0x00, 0x09, 0x00, 0x09, 0x00, 0x40, 0x04, 0x1b, 0x50, 0x1c, 0x23, 0xe9, 0x44, 40 0x00, 0x09, 0x00, 0x09, 0x00, 0x40, 0x04, 0x1b, 0x50, 0x1c, 0x23, 0xe9, 0x44,
50 0x23, 0x60, 0x9d, 0x09, 0x28, 0x1e, 0xf8, 0x6d, 0x64, 0x56, 0x9d, 0x53, 0xa8, 41 0x23, 0x60, 0x9d, 0x09, 0x28, 0x1e, 0xf8, 0x6d, 0x64, 0x56, 0x9d, 0x53, 0xa8,
51 0x7e, 0xa8, 0x65, 0x94, 0x5c, 0xb0, 0x8a, 0x45, 0x04, 0x00, 0x3b 42 0x7e, 0xa8, 0x65, 0x94, 0x5c, 0xb0, 0x8a, 0x45, 0x04, 0x00, 0x3b
52 }; 43 };
53 44
54 static void test_gif_data_no_colormap(skiatest::Reporter* r, 45 static void test_gif_data_no_colormap(skiatest::Reporter* r,
55 void* data, 46 void* data,
56 size_t size) { 47 size_t size) {
57 SkBitmap bm; 48 SkBitmap bm;
58 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory( 49 bool imageDecodeSuccess = decode_memory(data, size, &bm);
59 data, size, &bm);
60 REPORTER_ASSERT(r, imageDecodeSuccess); 50 REPORTER_ASSERT(r, imageDecodeSuccess);
61 REPORTER_ASSERT(r, bm.width() == 1); 51 REPORTER_ASSERT(r, bm.width() == 1);
62 REPORTER_ASSERT(r, bm.height() == 1); 52 REPORTER_ASSERT(r, bm.height() == 1);
63 REPORTER_ASSERT(r, !(bm.empty())); 53 REPORTER_ASSERT(r, !(bm.empty()));
64 if (!(bm.empty())) { 54 if (!(bm.empty())) {
65 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0x00000000); 55 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xFF000000);
66 } 56 }
67 } 57 }
68 static void test_gif_data(skiatest::Reporter* r, void* data, size_t size) { 58 static void test_gif_data(skiatest::Reporter* r, void* data, size_t size) {
69 SkBitmap bm; 59 SkBitmap bm;
70 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory( 60 bool imageDecodeSuccess = decode_memory(data, size, &bm);
71 data, size, &bm);
72 REPORTER_ASSERT(r, imageDecodeSuccess); 61 REPORTER_ASSERT(r, imageDecodeSuccess);
73 REPORTER_ASSERT(r, bm.width() == 3); 62 REPORTER_ASSERT(r, bm.width() == 3);
74 REPORTER_ASSERT(r, bm.height() == 3); 63 REPORTER_ASSERT(r, bm.height() == 3);
75 REPORTER_ASSERT(r, !(bm.empty())); 64 REPORTER_ASSERT(r, !(bm.empty()));
76 if (!(bm.empty())) { 65 if (!(bm.empty())) {
77 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000); 66 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
78 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00); 67 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
79 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff); 68 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
80 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080); 69 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
81 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000); 70 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
82 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00); 71 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
83 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff); 72 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
84 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff); 73 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
85 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff); 74 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
86 } 75 }
87 } 76 }
77 static void test_gif_data_dims(skiatest::Reporter* r, void* data, size_t size, i nt width,
78 int height) {
79 SkBitmap bm;
80 bool imageDecodeSuccess = decode_memory(data, size, &bm);
81 REPORTER_ASSERT(r, imageDecodeSuccess);
82 REPORTER_ASSERT(r, bm.width() == width);
83 REPORTER_ASSERT(r, bm.height() == height);
84 REPORTER_ASSERT(r, !(bm.empty()));
85 }
88 static void test_interlaced_gif_data(skiatest::Reporter* r, 86 static void test_interlaced_gif_data(skiatest::Reporter* r,
89 void* data, 87 void* data,
90 size_t size) { 88 size_t size) {
91 SkBitmap bm; 89 SkBitmap bm;
92 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory( 90 bool imageDecodeSuccess = decode_memory(data, size, &bm);
93 data, size, &bm);
94 REPORTER_ASSERT(r, imageDecodeSuccess); 91 REPORTER_ASSERT(r, imageDecodeSuccess);
95 REPORTER_ASSERT(r, bm.width() == 9); 92 REPORTER_ASSERT(r, bm.width() == 9);
96 REPORTER_ASSERT(r, bm.height() == 9); 93 REPORTER_ASSERT(r, bm.height() == 9);
97 REPORTER_ASSERT(r, !(bm.empty())); 94 REPORTER_ASSERT(r, !(bm.empty()));
98 if (!(bm.empty())) { 95 if (!(bm.empty())) {
99 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000); 96 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
100 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00); 97 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
101 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff); 98 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
102 99
103 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff); 100 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
(...skipping 11 matching lines...) Expand all
115 REPORTER_ASSERT(r, bm.getColor(0, 8) == 0xffffffff); 112 REPORTER_ASSERT(r, bm.getColor(0, 8) == 0xffffffff);
116 REPORTER_ASSERT(r, bm.getColor(1, 8) == 0xffff00ff); 113 REPORTER_ASSERT(r, bm.getColor(1, 8) == 0xffff00ff);
117 REPORTER_ASSERT(r, bm.getColor(2, 8) == 0xff0000ff); 114 REPORTER_ASSERT(r, bm.getColor(2, 8) == 0xff0000ff);
118 } 115 }
119 } 116 }
120 117
121 static void test_gif_data_short(skiatest::Reporter* r, 118 static void test_gif_data_short(skiatest::Reporter* r,
122 void* data, 119 void* data,
123 size_t size) { 120 size_t size) {
124 SkBitmap bm; 121 SkBitmap bm;
125 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory( 122 bool imageDecodeSuccess = decode_memory(data, size, &bm);
126 data, size, &bm);
127 REPORTER_ASSERT(r, imageDecodeSuccess); 123 REPORTER_ASSERT(r, imageDecodeSuccess);
128 REPORTER_ASSERT(r, bm.width() == 3); 124 REPORTER_ASSERT(r, bm.width() == 3);
129 REPORTER_ASSERT(r, bm.height() == 3); 125 REPORTER_ASSERT(r, bm.height() == 3);
130 REPORTER_ASSERT(r, !(bm.empty())); 126 REPORTER_ASSERT(r, !(bm.empty()));
131 if (!(bm.empty())) { 127 if (!(bm.empty())) {
132 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000); 128 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
133 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00); 129 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
134 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff); 130 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
135 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080); 131 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
136 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000); 132 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
137 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00); 133 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
138 } 134 }
139 } 135 }
140 136
141 /** 137 /**
142 This test will test the ability of the SkImageDecoder to deal with 138 This test will test the ability of the SkCodec to deal with
143 GIF files which have been mangled somehow. We want to display as 139 GIF files which have been mangled somehow. We want to display as
144 much of the GIF as possible. 140 much of the GIF as possible.
145 */ 141 */
146 DEF_TEST(Gif, reporter) { 142 DEF_TEST(Gif, reporter) {
147 // test perfectly good images. 143 // test perfectly good images.
148 test_gif_data(reporter, static_cast<void *>(gGIFData), sizeof(gGIFData)); 144 test_gif_data(reporter, static_cast<void *>(gGIFData), sizeof(gGIFData));
149 test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF), 145 test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF),
150 sizeof(gInterlacedGIF)); 146 sizeof(gInterlacedGIF));
151 147
152 unsigned char badData[sizeof(gGIFData)]; 148 unsigned char badData[sizeof(gGIFData)];
153 149
154 /* If you set the environment variable
155 skia_images_gif_suppressDecoderWarnings to 'false', you will
156 see warnings on stderr. This is a feature. */
157
158 memcpy(badData, gGIFData, sizeof(gGIFData)); 150 memcpy(badData, gGIFData, sizeof(gGIFData));
159 badData[6] = 0x01; // image too wide 151 badData[6] = 0x01; // image too wide
160 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 152 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
161 // "libgif warning [image too wide, expanding output to size]" 153 // "libgif warning [image too wide, expanding output to size]"
162 154
163 memcpy(badData, gGIFData, sizeof(gGIFData)); 155 memcpy(badData, gGIFData, sizeof(gGIFData));
164 badData[8] = 0x01; // image too tall 156 badData[8] = 0x01; // image too tall
165 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 157 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
166 // "libgif warning [image too tall, expanding output to size]" 158 // "libgif warning [image too tall, expanding output to size]"
167 159
168 memcpy(badData, gGIFData, sizeof(gGIFData)); 160 memcpy(badData, gGIFData, sizeof(gGIFData));
169 badData[62] = 0x01; // image shifted right 161 badData[62] = 0x01; // image shifted right
170 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 162 test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 4, 3);
171 // "libgif warning [shifting image left to fit]"
172 163
173 memcpy(badData, gGIFData, sizeof(gGIFData)); 164 memcpy(badData, gGIFData, sizeof(gGIFData));
174 badData[64] = 0x01; // image shifted down 165 badData[64] = 0x01; // image shifted down
175 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 166 test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 4);
176 // "libgif warning [shifting image up to fit]"
177 167
178 memcpy(badData, gGIFData, sizeof(gGIFData)); 168 memcpy(badData, gGIFData, sizeof(gGIFData));
179 badData[62] = 0xff; // image shifted left 169 badData[62] = 0xff; // image shifted right
180 badData[63] = 0xff; // 2's complement -1 short 170 badData[63] = 0xff;
181 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 171 test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3 + 0xFFFF, 3);
182 // "libgif warning [shifting image left to fit]"
183 172
184 memcpy(badData, gGIFData, sizeof(gGIFData)); 173 memcpy(badData, gGIFData, sizeof(gGIFData));
185 badData[64] = 0xff; // image shifted up 174 badData[64] = 0xff; // image shifted down
186 badData[65] = 0xff; // 2's complement -1 short 175 badData[65] = 0xff;
187 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData)); 176 test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 3 + 0xFFFF);
188 // "libgif warning [shifting image up to fit]"
189 177
190 test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap), 178 test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap),
191 sizeof(gGIFDataNoColormap)); 179 sizeof(gGIFDataNoColormap));
192 // "libgif warning [missing colormap]" 180 // "libgif warning [missing colormap]"
193 181
194 // test short Gif. 80 is missing a few bytes. 182 // test short Gif. 80 is missing a few bytes.
195 test_gif_data_short(reporter, static_cast<void *>(gGIFData), 80); 183 test_gif_data_short(reporter, static_cast<void *>(gGIFData), 80);
196 // "libgif warning [DGifGetLine]" 184 // "libgif warning [DGifGetLine]"
197 185
198 test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF), 186 test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF),
199 100); // 100 is missing a few bytes 187 100); // 100 is missing a few bytes
200 // "libgif warning [interlace DGifGetLine]" 188 // "libgif warning [interlace DGifGetLine]"
201 } 189 }
202 190
203 // Regression test for decoding a gif image with sampleSize of 4, which was 191 // Regression test for decoding a gif image with sampleSize of 4, which was
204 // previously crashing. 192 // previously crashing.
205 DEF_TEST(Gif_Sampled, r) { 193 DEF_TEST(Gif_Sampled, r) {
206 SkFILEStream fileStream(GetResourcePath("test640x479.gif").c_str()); 194 SkAutoTDelete<SkFILEStream> stream(
207 REPORTER_ASSERT(r, fileStream.isValid()); 195 new SkFILEStream(GetResourcePath("test640x479.gif").c_str()));
208 if (!fileStream.isValid()) { 196 REPORTER_ASSERT(r, stream->isValid());
197 if (!stream->isValid()) {
209 return; 198 return;
210 } 199 }
211 200
212 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&fileStream)); 201 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.det ach()));
213 REPORTER_ASSERT(r, decoder); 202 REPORTER_ASSERT(r, codec);
214 if (!decoder) { 203 if (!codec) {
215 return; 204 return;
216 } 205 }
217 decoder->setSampleSize(4); 206
207 // Construct a color table for the decode if necessary
208 SkAutoTUnref<SkColorTable> colorTable(nullptr);
209 SkPMColor* colorPtr = nullptr;
210 int* colorCountPtr = nullptr;
211 int maxColors = 256;
212 if (kIndex_8_SkColorType == codec->getInfo().colorType()) {
213 SkPMColor colors[256];
214 colorTable.reset(new SkColorTable(colors, maxColors));
215 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
216 colorCountPtr = &maxColors;
217 }
218
219 SkAndroidCodec::AndroidOptions options;
220 options.fSampleSize = 4;
221 options.fColorPtr = colorPtr;
222 options.fColorCount = colorCountPtr;
223
218 SkBitmap bm; 224 SkBitmap bm;
219 const SkImageDecoder::Result result = decoder->decode(&fileStream, &bm, 225 bm.allocPixels(codec->getInfo(), nullptr, colorTable.get());
220 SkImageDecoder::kDecodePixels_Mode); 226 const SkCodec::Result result = codec->getAndroidPixels(codec->getInfo(), bm. getPixels(),
221 REPORTER_ASSERT(r, result == SkImageDecoder::kSuccess); 227 bm.rowBytes(), &options);
228 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
222 } 229 }
223
224 #endif // !(SK_BUILD_FOR_WIN32||SK_BUILD_FOR_IOS||SK_BUILD_FOR_MAC)
OLDNEW
« no previous file with comments | « tests/FrontBufferedStreamTest.cpp ('k') | tests/IndexedPngOverflowTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698