encode kAlpha_8 as grayalpha with sigbits for gray==1

Bug: skia:
Change-Id: Ib61e8e0f62af92d8746f5e73469002e7804a8447
Reviewed-on: https://skia-review.googlesource.com/78481
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index 59978c6..a0f1fb2 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -175,6 +175,10 @@
         case kGray_8_SkColorType:
             return SkEncodedInfo::kGray_Color == srcColor && srcIsOpaque &&
                    !needs_color_xform(dst, srcCS, false);
+        case kAlpha_8_SkColorType:
+            // conceptually we can convert anything into alpha_8, but we haven't actually coded
+            // all of those other conversions yet, so only return true for the case we have codec.
+            return fSrcInfo.colorType() == kAlpha_8_SkColorType;;
         default:
             return false;
     }
diff --git a/src/codec/SkPngCodec.cpp b/src/codec/SkPngCodec.cpp
index e9fff97..9df7bc6 100644
--- a/src/codec/SkPngCodec.cpp
+++ b/src/codec/SkPngCodec.cpp
@@ -14,6 +14,7 @@
 #include "SkMath.h"
 #include "SkOpts.h"
 #include "SkPngCodec.h"
+#include "SkPngPriv.h"
 #include "SkPoint3.h"
 #include "SkSize.h"
 #include "SkStream.h"
@@ -935,7 +936,14 @@
         SkEncodedInfo encodedInfo = SkEncodedInfo::Make(color, alpha, bitDepth);
         SkImageInfo imageInfo = encodedInfo.makeImageInfo(origWidth, origHeight, colorSpace);
 
-        if (SkEncodedInfo::kOpaque_Alpha == alpha) {
+        if (encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
+            png_color_8p sigBits;
+            if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
+                if (8 == sigBits->alpha && kGraySigBit_GrayAlphaIsJustAlpha == sigBits->gray) {
+                    imageInfo = imageInfo.makeColorType(kAlpha_8_SkColorType);
+                }
+            }
+        } else if (SkEncodedInfo::kOpaque_Alpha == alpha) {
             png_color_8p sigBits;
             if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
                 if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) {
diff --git a/src/codec/SkPngPriv.h b/src/codec/SkPngPriv.h
new file mode 100644
index 0000000..3269309
--- /dev/null
+++ b/src/codec/SkPngPriv.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkPngPriv_DEFINED
+#define SkPngPriv_DEFINED
+
+#include "SkTypes.h"
+
+// We store kAlpha_8 images as GrayAlpha in png. Our private signal is significant bits for gray.
+// If that is set to 1, we assume the gray channel can be ignored, and we output just alpha.
+// We tried 0 at first, but png doesn't like a 0 sigbit for a channel it expects, hence we chose 1.
+
+static constexpr int kGraySigBit_GrayAlphaIsJustAlpha = 1;
+
+#endif
diff --git a/src/codec/SkSwizzler.cpp b/src/codec/SkSwizzler.cpp
index 84bb097..f3d97af 100644
--- a/src/codec/SkSwizzler.cpp
+++ b/src/codec/SkSwizzler.cpp
@@ -357,6 +357,16 @@
     SkOpts::grayA_to_rgbA((uint32_t*) dst, src + offset, width);
 }
 
+static void swizzle_grayalpha_to_a8(void* dst, const uint8_t* src, int width, int bpp,
+                                    int deltaSrc, int offset, const SkPMColor[]) {
+    src += offset;
+    uint8_t* dst8 = (uint8_t*)dst;
+    for (int x = 0; x < width; ++x) {
+        dst8[x] = src[1];   // src[0] is gray, ignored
+        src += deltaSrc;
+    }
+}
+
 // kBGR
 
 static void swizzle_bgr_to_565(
@@ -906,6 +916,9 @@
                             }
                         }
                         break;
+                    case kAlpha_8_SkColorType:
+                        proc = &swizzle_grayalpha_to_a8;
+                        break;
                     default:
                         return nullptr;
                 }