GifTest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #include "include/codec/SkAndroidCodec.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkData.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkStream.h"
  13. #include "include/core/SkTypes.h"
  14. #include "tests/CodecPriv.h"
  15. #include "tests/Test.h"
  16. #include "tools/Resources.h"
  17. static unsigned char gGIFData[] = {
  18. 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00, 0x03, 0x00, 0xe3, 0x08,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
  20. 0xff, 0x80, 0x80, 0x80, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  21. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23. 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x04,
  24. 0x07, 0x50, 0x1c, 0x43, 0x40, 0x41, 0x23, 0x44, 0x00, 0x3b
  25. };
  26. static unsigned char gGIFDataNoColormap[] = {
  27. // Header
  28. 0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
  29. // Screen descriptor
  30. 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  31. // Graphics control extension
  32. 0x21, 0xf9, 0x04, 0x01, 0x0a, 0x00, 0x01, 0x00,
  33. // Image descriptor
  34. 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  35. // Image data
  36. 0x02, 0x02, 0x4c, 0x01, 0x00,
  37. // Trailer
  38. 0x3b
  39. };
  40. static unsigned char gInterlacedGIF[] = {
  41. 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x09, 0x00, 0x09, 0x00, 0xe3, 0x08, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x80,
  43. 0x80, 0x80, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff,
  44. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  45. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00,
  46. 0x00, 0x09, 0x00, 0x09, 0x00, 0x40, 0x04, 0x1b, 0x50, 0x1c, 0x23, 0xe9, 0x44,
  47. 0x23, 0x60, 0x9d, 0x09, 0x28, 0x1e, 0xf8, 0x6d, 0x64, 0x56, 0x9d, 0x53, 0xa8,
  48. 0x7e, 0xa8, 0x65, 0x94, 0x5c, 0xb0, 0x8a, 0x45, 0x04, 0x00, 0x3b
  49. };
  50. static void test_gif_data_no_colormap(skiatest::Reporter* r,
  51. void* data,
  52. size_t size) {
  53. SkBitmap bm;
  54. bool imageDecodeSuccess = decode_memory(data, size, &bm);
  55. REPORTER_ASSERT(r, imageDecodeSuccess);
  56. REPORTER_ASSERT(r, bm.width() == 1);
  57. REPORTER_ASSERT(r, bm.height() == 1);
  58. REPORTER_ASSERT(r, !(bm.empty()));
  59. if (!(bm.empty())) {
  60. REPORTER_ASSERT(r, bm.getColor(0, 0) == 0x00000000);
  61. }
  62. }
  63. static void test_gif_data(skiatest::Reporter* r, void* data, size_t size) {
  64. SkBitmap bm;
  65. bool imageDecodeSuccess = decode_memory(data, size, &bm);
  66. REPORTER_ASSERT(r, imageDecodeSuccess);
  67. REPORTER_ASSERT(r, bm.width() == 3);
  68. REPORTER_ASSERT(r, bm.height() == 3);
  69. REPORTER_ASSERT(r, !(bm.empty()));
  70. if (!(bm.empty())) {
  71. REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
  72. REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
  73. REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
  74. REPORTER_ASSERT(r, bm.getColor(0, 1) == 0xff808080);
  75. REPORTER_ASSERT(r, bm.getColor(1, 1) == 0xff000000);
  76. REPORTER_ASSERT(r, bm.getColor(2, 1) == 0xff00ff00);
  77. REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
  78. REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
  79. REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
  80. }
  81. }
  82. static void test_gif_data_dims(skiatest::Reporter* r, void* data, size_t size, int width,
  83. int height) {
  84. SkBitmap bm;
  85. bool imageDecodeSuccess = decode_memory(data, size, &bm);
  86. REPORTER_ASSERT(r, imageDecodeSuccess);
  87. REPORTER_ASSERT(r, bm.width() == width);
  88. REPORTER_ASSERT(r, bm.height() == height);
  89. REPORTER_ASSERT(r, !(bm.empty()));
  90. }
  91. static void test_interlaced_gif_data(skiatest::Reporter* r,
  92. void* data,
  93. size_t size) {
  94. SkBitmap bm;
  95. bool imageDecodeSuccess = decode_memory(data, size, &bm);
  96. REPORTER_ASSERT(r, imageDecodeSuccess);
  97. REPORTER_ASSERT(r, bm.width() == 9);
  98. REPORTER_ASSERT(r, bm.height() == 9);
  99. REPORTER_ASSERT(r, !(bm.empty()));
  100. if (!(bm.empty())) {
  101. REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xffff0000);
  102. REPORTER_ASSERT(r, bm.getColor(1, 0) == 0xffffff00);
  103. REPORTER_ASSERT(r, bm.getColor(2, 0) == 0xff00ffff);
  104. REPORTER_ASSERT(r, bm.getColor(0, 2) == 0xffffffff);
  105. REPORTER_ASSERT(r, bm.getColor(1, 2) == 0xffff00ff);
  106. REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
  107. REPORTER_ASSERT(r, bm.getColor(0, 4) == 0xff808080);
  108. REPORTER_ASSERT(r, bm.getColor(1, 4) == 0xff000000);
  109. REPORTER_ASSERT(r, bm.getColor(2, 4) == 0xff00ff00);
  110. REPORTER_ASSERT(r, bm.getColor(0, 6) == 0xffff0000);
  111. REPORTER_ASSERT(r, bm.getColor(1, 6) == 0xffffff00);
  112. REPORTER_ASSERT(r, bm.getColor(2, 6) == 0xff00ffff);
  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. static void test_gif_data_short(skiatest::Reporter* r,
  119. void* data,
  120. size_t size) {
  121. SkBitmap bm;
  122. bool imageDecodeSuccess = decode_memory(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. This test will test the ability of the SkCodec to deal with
  138. GIF files which have been mangled somehow. We want to display as
  139. much of the GIF as possible.
  140. */
  141. DEF_TEST(Gif, reporter) {
  142. // test perfectly good images.
  143. test_gif_data(reporter, static_cast<void *>(gGIFData), sizeof(gGIFData));
  144. test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF),
  145. sizeof(gInterlacedGIF));
  146. unsigned char badData[sizeof(gGIFData)];
  147. memcpy(badData, gGIFData, sizeof(gGIFData));
  148. badData[6] = 0x01; // image too wide
  149. test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
  150. // "libgif warning [image too wide, expanding output to size]"
  151. memcpy(badData, gGIFData, sizeof(gGIFData));
  152. badData[8] = 0x01; // image too tall
  153. test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
  154. // "libgif warning [image too tall, expanding output to size]"
  155. memcpy(badData, gGIFData, sizeof(gGIFData));
  156. badData[62] = 0x01; // image shifted right
  157. test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 4, 3);
  158. memcpy(badData, gGIFData, sizeof(gGIFData));
  159. badData[64] = 0x01; // image shifted down
  160. test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 4);
  161. memcpy(badData, gGIFData, sizeof(gGIFData));
  162. badData[62] = 0xff; // image shifted right
  163. badData[63] = 0xff;
  164. test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3 + 0xFFFF, 3);
  165. memcpy(badData, gGIFData, sizeof(gGIFData));
  166. badData[64] = 0xff; // image shifted down
  167. badData[65] = 0xff;
  168. test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 3 + 0xFFFF);
  169. test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap),
  170. sizeof(gGIFDataNoColormap));
  171. #ifdef SK_HAS_WUFFS_LIBRARY
  172. // We are transitioning from an old GIF implementation to a new (Wuffs) GIF
  173. // implementation.
  174. //
  175. // This test (without SK_HAS_WUFFS_LIBRARY) is overly specific to the old
  176. // implementation. It claims that, for invalid (truncated) input, we can
  177. // still 'decode' all of the pixels because no matter what palette index
  178. // each pixel is, they're all equivalently transparent. It's not obvious
  179. // that this off-spec behavior is worth preserving. Are real world users
  180. // decoding truncated all-transparent GIF images??
  181. //
  182. // Once the transition is complete, we can remove the #ifdef and delete the
  183. // #else branch.
  184. #else
  185. // Since there is no color map, we do not even need to parse the image data
  186. // to know that we should draw transparent. Truncate the file before the
  187. // data. This should still succeed.
  188. test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap), 31);
  189. // Likewise, incremental decoding should succeed here.
  190. {
  191. sk_sp<SkData> data = SkData::MakeWithoutCopy(gGIFDataNoColormap, 31);
  192. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
  193. REPORTER_ASSERT(reporter, codec);
  194. if (codec) {
  195. auto info = codec->getInfo().makeColorType(kN32_SkColorType);
  196. SkBitmap bm;
  197. bm.allocPixels(info);
  198. REPORTER_ASSERT(reporter, SkCodec::kSuccess == codec->startIncrementalDecode(
  199. info, bm.getPixels(), bm.rowBytes()));
  200. REPORTER_ASSERT(reporter, SkCodec::kSuccess == codec->incrementalDecode());
  201. REPORTER_ASSERT(reporter, bm.width() == 1);
  202. REPORTER_ASSERT(reporter, bm.height() == 1);
  203. REPORTER_ASSERT(reporter, !(bm.empty()));
  204. if (!(bm.empty())) {
  205. REPORTER_ASSERT(reporter, bm.getColor(0, 0) == 0x00000000);
  206. }
  207. }
  208. }
  209. #endif
  210. // test short Gif. 80 is missing a few bytes.
  211. test_gif_data_short(reporter, static_cast<void *>(gGIFData), 80);
  212. // "libgif warning [DGifGetLine]"
  213. test_interlaced_gif_data(reporter, static_cast<void *>(gInterlacedGIF),
  214. 100); // 100 is missing a few bytes
  215. // "libgif warning [interlace DGifGetLine]"
  216. }
  217. // Regression test for decoding a gif image with sampleSize of 4, which was
  218. // previously crashing.
  219. DEF_TEST(Gif_Sampled, r) {
  220. auto data = GetResourceAsData("images/test640x479.gif");
  221. REPORTER_ASSERT(r, data);
  222. if (!data) {
  223. return;
  224. }
  225. std::unique_ptr<SkStreamAsset> stream(new SkMemoryStream(std::move(data)));
  226. std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream)));
  227. REPORTER_ASSERT(r, codec);
  228. if (!codec) {
  229. return;
  230. }
  231. SkAndroidCodec::AndroidOptions options;
  232. options.fSampleSize = 4;
  233. SkBitmap bm;
  234. bm.allocPixels(codec->getInfo());
  235. const SkCodec::Result result = codec->getAndroidPixels(codec->getInfo(), bm.getPixels(),
  236. bm.rowBytes(), &options);
  237. REPORTER_ASSERT(r, result == SkCodec::kSuccess);
  238. }
  239. // If a GIF file is truncated before the header for the first image is defined,
  240. // we should not create an SkCodec.
  241. DEF_TEST(Codec_GifTruncated, r) {
  242. sk_sp<SkData> data(GetResourceAsData("images/test640x479.gif"));
  243. if (!data) {
  244. return;
  245. }
  246. // This is right before the header for the first image.
  247. data = SkData::MakeSubset(data.get(), 0, 446);
  248. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
  249. REPORTER_ASSERT(r, !codec);
  250. }
  251. /*
  252. For the Codec_GifTruncated2 test, immediately below,
  253. resources/images/box.gif's first 23 bytes are:
  254. 00000000: 4749 4638 3961 c800 3700 203f 002c 0000 GIF89a..7. ?.,..
  255. 00000010: 0000 c800 3700 85 ....7..
  256. The breakdown:
  257. @000 6 bytes magic "GIF89a"
  258. @006 7 bytes Logical Screen Descriptor: 0xC8 0x00 ... 0x00
  259. - width = 200
  260. - height = 55
  261. - flags = 0x20
  262. - background color index, pixel aspect ratio bytes ignored
  263. @00D 10 bytes Image Descriptor header: 0x2C 0x00 ... 0x85
  264. - origin_x = 0
  265. - origin_y = 0
  266. - width = 200
  267. - height = 55
  268. - flags = 0x85, local color table, 64 RGB entries
  269. In particular, 23 bytes is after the header, but before the color table.
  270. */
  271. DEF_TEST(Codec_GifTruncated2, r) {
  272. // Truncate box.gif at 21, 22 and 23 bytes.
  273. //
  274. // See also Codec_GifTruncated3 in this file, below.
  275. //
  276. // See also Codec_trunc in CodecAnimTest.cpp for this magic 23.
  277. //
  278. // See also Codec_GifPreMap in CodecPartialTest.cpp for this magic 23.
  279. for (int i = 21; i < 24; i++) {
  280. sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
  281. if (!data) {
  282. return;
  283. }
  284. data = SkData::MakeSubset(data.get(), 0, i);
  285. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
  286. if (i <= 21) {
  287. if (codec) {
  288. ERRORF(r, "Invalid data gave non-nullptr codec");
  289. }
  290. return;
  291. }
  292. if (!codec) {
  293. ERRORF(r, "Failed to create codec with partial data (truncated at %d)", i);
  294. return;
  295. }
  296. #ifdef SK_HAS_WUFFS_LIBRARY
  297. // We are transitioning from an old GIF implementation to a new (Wuffs)
  298. // GIF implementation.
  299. //
  300. // The input is truncated in the Image Descriptor, before the local
  301. // color table, and before (21) or after (22, 23) the first frame's
  302. // XYWH (left / top / width / height) can be decoded. A detailed
  303. // breakdown of those 23 bytes is in a comment above this function.
  304. //
  305. // With the old implementation, this test claimed that "no frame is
  306. // complete enough that it has its metadata". In terms of the
  307. // underlying file format, this claim is true for truncating at 21
  308. // bytes, but not true for 22 or 23.
  309. //
  310. // At 21 bytes, both the old and new implementation's MakeFromStream
  311. // factory method returns a nullptr SkCodec*, because creating a
  312. // SkCodec requires knowing the image width and height (as its
  313. // constructor takes an SkEncodedInfo argument), and specifically for
  314. // GIF, decoding the image width and height requires decoding the first
  315. // frame's XYWH, as per
  316. // https://raw.githubusercontent.com/google/wuffs/master/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt
  317. //
  318. // At 22 or 23 bytes, the first frame is complete enough that we can
  319. // fill in all of a SkCodec::FrameInfo's fields (other than
  320. // fFullyReceived). Specifically, we can fill in fRequiredFrame and
  321. // fAlphaType, even though we haven't yet decoded the frame's RGB
  322. // palette entries, as we do know the frame rectangle and that every
  323. // palette entry is fully opaque, due to the lack of a Graphic Control
  324. // Extension before the Image Descriptor.
  325. //
  326. // The new implementation correctly reports that the first frame's
  327. // metadata is complete enough. The old implementation does not.
  328. //
  329. // Once the transition is complete, we can remove the #ifdef and delete
  330. // the #else code.
  331. REPORTER_ASSERT(r, codec->getFrameCount() == 1);
  332. #else
  333. // The old implementation claimed:
  334. //
  335. // Although we correctly created a codec, no frame is
  336. // complete enough that it has its metadata. Returning 0
  337. // ensures that Chromium will not try to create a frame
  338. // too early.
  339. REPORTER_ASSERT(r, codec->getFrameCount() == 0);
  340. #endif
  341. }
  342. }
  343. #ifdef SK_HAS_WUFFS_LIBRARY
  344. // This tests that, after truncating the input, the pixels are still
  345. // zero-initialized. If you comment out the SkSampler::Fill call in
  346. // SkWuffsCodec::onStartIncrementalDecode, the test could still pass (in a
  347. // standard configuration) but should fail with the MSAN memory sanitizer.
  348. DEF_TEST(Codec_GifTruncated3, r) {
  349. sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
  350. if (!data) {
  351. return;
  352. }
  353. data = SkData::MakeSubset(data.get(), 0, 23);
  354. sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
  355. if (!image) {
  356. ERRORF(r, "Missing image");
  357. return;
  358. }
  359. REPORTER_ASSERT(r, image->width() == 200);
  360. REPORTER_ASSERT(r, image->height() == 55);
  361. SkBitmap bm;
  362. if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(200, 55))) {
  363. ERRORF(r, "Failed to allocate pixels");
  364. return;
  365. }
  366. bm.eraseColor(SK_ColorTRANSPARENT);
  367. SkCanvas canvas(bm);
  368. canvas.drawImage(image, 0, 0, nullptr);
  369. for (int i = 0; i < image->width(); ++i)
  370. for (int j = 0; j < image->height(); ++j) {
  371. SkColor actual = SkUnPreMultiply::PMColorToColor(*bm.getAddr32(i, j));
  372. if (actual != SK_ColorTRANSPARENT) {
  373. ERRORF(r, "did not initialize pixels! %i, %i is %x", i, j, actual);
  374. }
  375. }
  376. }
  377. #endif
  378. DEF_TEST(Codec_gif_out_of_palette, r) {
  379. if (GetResourcePath().isEmpty()) {
  380. return;
  381. }
  382. const char* path = "images/out-of-palette.gif";
  383. auto data = GetResourceAsData(path);
  384. if (!data) {
  385. ERRORF(r, "failed to find %s", path);
  386. return;
  387. }
  388. auto codec = SkCodec::MakeFromData(std::move(data));
  389. if (!codec) {
  390. ERRORF(r, "Could not create codec from %s", path);
  391. return;
  392. }
  393. SkBitmap bm;
  394. bm.allocPixels(codec->getInfo());
  395. auto result = codec->getPixels(bm.pixmap());
  396. REPORTER_ASSERT(r, result == SkCodec::kSuccess, "Failed to decode %s with error %s",
  397. path, SkCodec::ResultToString(result));
  398. struct {
  399. int x;
  400. int y;
  401. SkColor expected;
  402. } pixels[] = {
  403. { 0, 0, SK_ColorBLACK },
  404. { 1, 0, SK_ColorWHITE },
  405. { 0, 1, SK_ColorTRANSPARENT },
  406. { 1, 1, SK_ColorTRANSPARENT },
  407. };
  408. for (auto& pixel : pixels) {
  409. auto actual = bm.getColor(pixel.x, pixel.y);
  410. REPORTER_ASSERT(r, actual == pixel.expected,
  411. "pixel (%i,%i) mismatch! expected: %x actual: %x",
  412. pixel.x, pixel.y, pixel.expected, actual);
  413. }
  414. }