blob: c9dac9b497a6bcc65e518942646c3cd3589321c2 [file] [log] [blame]
halcanary@google.com29d4e632013-10-11 18:21:56 +00001/*
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// This tests out GIF decoder (SkImageDecoder_libgif.cpp)
9// It is not used on these platforms:
10#if (!defined(SK_BUILD_FOR_WIN32)) && \
11 (!defined(SK_BUILD_FOR_IOS)) && \
12 (!defined(SK_BUILD_FOR_MAC))
13
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000014#include "Test.h"
15#include "TestClassDef.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000016#include "SkBitmap.h"
17#include "SkData.h"
18#include "SkForceLinking.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000019#include "SkImage.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000020#include "SkImageDecoder.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000021#include "SkStream.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000022
23__SK_FORCE_IMAGE_DECODER_LINKING;
24
25namespace {
26 unsigned char gifData[] = {
27 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00,
28 0x03, 0x00, 0xe3, 0x08, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
30 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00,
31 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00,
35 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x04,
36 0x07, 0x50, 0x1c, 0x43, 0x40, 0x41, 0x23, 0x44,
37 0x00, 0x3b};
38 unsigned char gifDataNoColormap[] = {
39 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00,
40 0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04,
41 0x01, 0x0a, 0x00, 0x01, 0x00, 0x2c, 0x00, 0x00,
42 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
43 0x02, 0x4c, 0x01, 0x00, 0x3b};
44 unsigned char interlacedGif[] = {
45 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x09, 0x00,
46 0x09, 0x00, 0xe3, 0x08, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
48 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00,
49 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
50 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00,
53 0x00, 0x00, 0x09, 0x00, 0x09, 0x00, 0x40, 0x04,
54 0x1b, 0x50, 0x1c, 0x23, 0xe9, 0x44, 0x23, 0x60,
55 0x9d, 0x09, 0x28, 0x1e, 0xf8, 0x6d, 0x64, 0x56,
56 0x9d, 0x53, 0xa8, 0x7e, 0xa8, 0x65, 0x94, 0x5c,
57 0xb0, 0x8a, 0x45, 0x04, 0x00, 0x3b};
58}; // namespace
59
60static void test_gif_data_no_colormap(skiatest::Reporter* r,
61 void* data, size_t size) {
62 SkBitmap bm;
63 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
64 data, size, &bm);
65 REPORTER_ASSERT(r, imageDecodeSuccess);
66 REPORTER_ASSERT(r, bm.width() == 1);
67 REPORTER_ASSERT(r, bm.height() == 1);
68 REPORTER_ASSERT(r, !(bm.empty()));
69 if (!(bm.empty())) {
70 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0x00000000);
71 }
72}
73static void test_gif_data(skiatest::Reporter* r, void* data, size_t size) {
74 SkBitmap bm;
75 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
76 data, size, &bm);
77 REPORTER_ASSERT(r, imageDecodeSuccess);
78 REPORTER_ASSERT(r, bm.width() == 3);
79 REPORTER_ASSERT(r, bm.height() == 3);
80 REPORTER_ASSERT(r, !(bm.empty()));
81 if (!(bm.empty())) {
82 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
83 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
84 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
85 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
86 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
87 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
88 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
89 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
90 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
91 }
92}
93static void test_interlaced_gif_data(skiatest::Reporter* r,
94 void* data,
95 size_t size) {
96 SkBitmap bm;
97 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
98 data, size, &bm);
99 REPORTER_ASSERT(r, imageDecodeSuccess);
100 REPORTER_ASSERT(r, bm.width() == 9);
101 REPORTER_ASSERT(r, bm.height() == 9);
102 REPORTER_ASSERT(r, !(bm.empty()));
103 if (!(bm.empty())) {
104 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
105 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
106 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
107
108 REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
109 REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
110 REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
111
112 REPORTER_ASSERT(r, bm.getColor(0, 4) == 0xff808080);
113 REPORTER_ASSERT(r, bm.getColor(1, 4) == 0xff000000);
114 REPORTER_ASSERT(r, bm.getColor(2, 4) == 0xff00ff00);
115
116 REPORTER_ASSERT(r, bm.getColor(0, 6) == 0xffff0000);
117 REPORTER_ASSERT(r, bm.getColor(1, 6) == 0xffffff00);
118 REPORTER_ASSERT(r, bm.getColor(2, 6) == 0xff00ffff);
119
120 REPORTER_ASSERT(r, bm.getColor(0, 8) == 0xffffffff);
121 REPORTER_ASSERT(r, bm.getColor(1, 8) == 0xffff00ff);
122 REPORTER_ASSERT(r, bm.getColor(2, 8) == 0xff0000ff);
123 }
124}
125
126static void test_gif_data_short(skiatest::Reporter* r,
127 void* data,
128 size_t size) {
129 SkBitmap bm;
130 bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
131 data, size, &bm);
132 REPORTER_ASSERT(r, imageDecodeSuccess);
133 REPORTER_ASSERT(r, bm.width() == 3);
134 REPORTER_ASSERT(r, bm.height() == 3);
135 REPORTER_ASSERT(r, !(bm.empty()));
136 if (!(bm.empty())) {
137 REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
138 REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
139 REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
140 REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
141 REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
142 REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
143 }
144}
145
146/**
147 This test will test the ability of the SkImageDecoder to deal with
148 GIF files which have been mangled somehow. We want to display as
149 much of the GIF as possible.
150*/
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000151DEF_TEST(Gif, reporter) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000152 // test perfectly good images.
153 test_gif_data(reporter, static_cast<void *>(gifData), sizeof(gifData));
154 test_interlaced_gif_data(reporter, static_cast<void *>(interlacedGif),
155 sizeof(interlacedGif));
156
157 unsigned char badData[sizeof(gifData)];
158
159 /* If you set the environment variable
160 skia_images_gif_suppressDecoderWarnings to 'false', you will
161 see warnings on stderr. This is a feature. */
162
163 memcpy(badData, gifData, sizeof(gifData));
164 badData[6] = 0x01; // image too wide
165 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
166 // "libgif warning [image too wide, expanding output to size]"
167
168 memcpy(badData, gifData, sizeof(gifData));
169 badData[8] = 0x01; // image too tall
170 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
171 // "libgif warning [image too tall, expanding output to size]"
172
173 memcpy(badData, gifData, sizeof(gifData));
174 badData[62] = 0x01; // image shifted right
175 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
176 // "libgif warning [shifting image left to fit]"
177
178 memcpy(badData, gifData, sizeof(gifData));
179 badData[64] = 0x01; // image shifted down
180 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
181 // "libgif warning [shifting image up to fit]"
182
183 memcpy(badData, gifData, sizeof(gifData));
184 badData[62] = 0xff; // image shifted left
185 badData[63] = 0xff; // 2's complement -1 short
186 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
187 // "libgif warning [shifting image left to fit]"
188
189 memcpy(badData, gifData, sizeof(gifData));
190 badData[64] = 0xff; // image shifted up
191 badData[65] = 0xff; // 2's complement -1 short
192 test_gif_data(reporter, static_cast<void *>(badData), sizeof(gifData));
193 // "libgif warning [shifting image up to fit]"
194
195 test_gif_data_no_colormap(reporter, static_cast<void *>(gifDataNoColormap),
196 sizeof(gifDataNoColormap));
197 // "libgif warning [missing colormap]"
198
199 // test short Gif. 80 is missing a few bytes.
200 test_gif_data_short(reporter, static_cast<void *>(gifData), 80);
201 // "libgif warning [DGifGetLine]"
202
203 test_interlaced_gif_data(reporter, static_cast<void *>(interlacedGif),
204 100); // 100 is missing a few bytes
205 // "libgif warning [interlace DGifGetLine]"
206}
207
halcanary@google.com29d4e632013-10-11 18:21:56 +0000208#endif // !(SK_BUILD_FOR_WIN32||SK_BUILD_FOR_IOS||SK_BUILD_FOR_MAC)