Fix bug in SkGifCodec / Switch SkImageDec tests to use Codec

SkImageDecoder is still used throughout tests, tools, gms etc.
Deleting it from tests is an easy first step.

Bonus is that we add tests of SkCodec.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1733863003

Review URL: https://codereview.chromium.org/1733863003
diff --git a/tests/GifTest.cpp b/tests/GifTest.cpp
index 978e374..caa0f6f 100644
--- a/tests/GifTest.cpp
+++ b/tests/GifTest.cpp
@@ -5,25 +5,16 @@
  * found in the LICENSE file.
  */
 
-#include "SkTypes.h"
-
-// This tests out GIF decoder (SkImageDecoder_libgif.cpp)
-// It is not used on these platforms:
-#if (!defined(SK_BUILD_FOR_WIN32)) &&           \
-    (!defined(SK_BUILD_FOR_IOS)) &&             \
-    (!defined(SK_BUILD_FOR_MAC))
-
+#include "CodecPriv.h"
 #include "Resources.h"
+#include "SkAndroidCodec.h"
 #include "SkBitmap.h"
 #include "SkData.h"
-#include "SkForceLinking.h"
 #include "SkImage.h"
-#include "SkImageDecoder.h"
 #include "SkStream.h"
+#include "SkTypes.h"
 #include "Test.h"
 
-__SK_FORCE_IMAGE_DECODER_LINKING;
-
 static unsigned char gGIFData[] = {
   0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x03, 0x00, 0x03, 0x00, 0xe3, 0x08,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00,
@@ -55,20 +46,18 @@
                                       void* data,
                                       size_t size) {
     SkBitmap bm;
-    bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
-        data, size, &bm);
+    bool imageDecodeSuccess = decode_memory(data, size, &bm);
     REPORTER_ASSERT(r, imageDecodeSuccess);
     REPORTER_ASSERT(r, bm.width() == 1);
     REPORTER_ASSERT(r, bm.height() == 1);
     REPORTER_ASSERT(r, !(bm.empty()));
     if (!(bm.empty())) {
-        REPORTER_ASSERT(r, bm.getColor(0, 0) == 0x00000000);
+        REPORTER_ASSERT(r, bm.getColor(0, 0) == 0xFF000000);
     }
 }
 static void test_gif_data(skiatest::Reporter* r, void* data, size_t size) {
     SkBitmap bm;
-    bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
-        data, size, &bm);
+    bool imageDecodeSuccess = decode_memory(data, size, &bm);
     REPORTER_ASSERT(r, imageDecodeSuccess);
     REPORTER_ASSERT(r, bm.width() == 3);
     REPORTER_ASSERT(r, bm.height() == 3);
@@ -85,12 +74,20 @@
         REPORTER_ASSERT(r, bm.getColor(2, 2) == 0xff0000ff);
     }
 }
+static void test_gif_data_dims(skiatest::Reporter* r, void* data, size_t size, int width,
+        int height) {
+    SkBitmap bm;
+    bool imageDecodeSuccess = decode_memory(data, size, &bm);
+    REPORTER_ASSERT(r, imageDecodeSuccess);
+    REPORTER_ASSERT(r, bm.width() == width);
+    REPORTER_ASSERT(r, bm.height() == height);
+    REPORTER_ASSERT(r, !(bm.empty()));
+}
 static void test_interlaced_gif_data(skiatest::Reporter* r,
                                      void* data,
                                      size_t size) {
     SkBitmap bm;
-    bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
-        data, size, &bm);
+    bool imageDecodeSuccess = decode_memory(data, size, &bm);
     REPORTER_ASSERT(r, imageDecodeSuccess);
     REPORTER_ASSERT(r, bm.width() == 9);
     REPORTER_ASSERT(r, bm.height() == 9);
@@ -122,8 +119,7 @@
                                 void* data,
                                 size_t size) {
     SkBitmap bm;
-    bool imageDecodeSuccess = SkImageDecoder::DecodeMemory(
-        data, size, &bm);
+    bool imageDecodeSuccess = decode_memory(data, size, &bm);
     REPORTER_ASSERT(r, imageDecodeSuccess);
     REPORTER_ASSERT(r, bm.width() == 3);
     REPORTER_ASSERT(r, bm.height() == 3);
@@ -139,7 +135,7 @@
 }
 
 /**
-  This test will test the ability of the SkImageDecoder to deal with
+  This test will test the ability of the SkCodec to deal with
   GIF files which have been mangled somehow.  We want to display as
   much of the GIF as possible.
 */
@@ -151,10 +147,6 @@
 
     unsigned char badData[sizeof(gGIFData)];
 
-    /* If you set the environment variable
-       skia_images_gif_suppressDecoderWarnings to 'false', you will
-       see warnings on stderr.  This is a feature.  */
-
     memcpy(badData, gGIFData, sizeof(gGIFData));
     badData[6] = 0x01;  // image too wide
     test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
@@ -167,25 +159,21 @@
 
     memcpy(badData, gGIFData, sizeof(gGIFData));
     badData[62] = 0x01;  // image shifted right
-    test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
-    // "libgif warning [shifting image left to fit]"
+    test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 4, 3);
 
     memcpy(badData, gGIFData, sizeof(gGIFData));
     badData[64] = 0x01;  // image shifted down
-    test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
-    // "libgif warning [shifting image up to fit]"
+    test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 4);
 
     memcpy(badData, gGIFData, sizeof(gGIFData));
-    badData[62] = 0xff;  // image shifted left
-    badData[63] = 0xff;  // 2's complement -1 short
-    test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
-    // "libgif warning [shifting image left to fit]"
+    badData[62] = 0xff;  // image shifted right
+    badData[63] = 0xff;
+    test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3 + 0xFFFF, 3);
 
     memcpy(badData, gGIFData, sizeof(gGIFData));
-    badData[64] = 0xff;  // image shifted up
-    badData[65] = 0xff;  // 2's complement -1 short
-    test_gif_data(reporter, static_cast<void *>(badData), sizeof(gGIFData));
-    // "libgif warning [shifting image up to fit]"
+    badData[64] = 0xff;  // image shifted down
+    badData[65] = 0xff;
+    test_gif_data_dims(reporter, static_cast<void *>(badData), sizeof(gGIFData), 3, 3 + 0xFFFF);
 
     test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap),
                               sizeof(gGIFDataNoColormap));
@@ -203,22 +191,39 @@
 // Regression test for decoding a gif image with sampleSize of 4, which was
 // previously crashing.
 DEF_TEST(Gif_Sampled, r) {
-    SkFILEStream fileStream(GetResourcePath("test640x479.gif").c_str());
-    REPORTER_ASSERT(r, fileStream.isValid());
-    if (!fileStream.isValid()) {
+    SkAutoTDelete<SkFILEStream> stream(
+            new SkFILEStream(GetResourcePath("test640x479.gif").c_str()));
+    REPORTER_ASSERT(r, stream->isValid());
+    if (!stream->isValid()) {
         return;
     }
 
-    SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&fileStream));
-    REPORTER_ASSERT(r, decoder);
-    if (!decoder) {
+    SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
+    REPORTER_ASSERT(r, codec);
+    if (!codec) {
         return;
     }
-    decoder->setSampleSize(4);
+
+    // Construct a color table for the decode if necessary
+    SkAutoTUnref<SkColorTable> colorTable(nullptr);
+    SkPMColor* colorPtr = nullptr;
+    int* colorCountPtr = nullptr;
+    int maxColors = 256;
+    if (kIndex_8_SkColorType == codec->getInfo().colorType()) {
+        SkPMColor colors[256];
+        colorTable.reset(new SkColorTable(colors, maxColors));
+        colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
+        colorCountPtr = &maxColors;
+    }
+
+    SkAndroidCodec::AndroidOptions options;
+    options.fSampleSize = 4;
+    options.fColorPtr = colorPtr;
+    options.fColorCount = colorCountPtr;
+
     SkBitmap bm;
-    const SkImageDecoder::Result result = decoder->decode(&fileStream, &bm,
-            SkImageDecoder::kDecodePixels_Mode);
-    REPORTER_ASSERT(r, result == SkImageDecoder::kSuccess);
+    bm.allocPixels(codec->getInfo(), nullptr, colorTable.get());
+    const SkCodec::Result result = codec->getAndroidPixels(codec->getInfo(), bm.getPixels(),
+            bm.rowBytes(), &options);
+    REPORTER_ASSERT(r, result == SkCodec::kSuccess);
 }
-
-#endif  // !(SK_BUILD_FOR_WIN32||SK_BUILD_FOR_IOS||SK_BUILD_FOR_MAC)