[bmp] Fix swizzler initialization

We only decode via an intermediate color xform buffer when
xformOnDecode() is true (see decodeRows).

The swizzler selection should use the same heuristic to match the actual
dest buffer type.

Cherrypick of https://skia-review.googlesource.com/c/skia/+/937497

Bug: 388480622
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:c58deb210e62cf57de91b0eb8844b782fc774135)
Merged-In: Iaf240852a9bcf0be02cda84f0e44576da8100dec
Change-Id: Iaf240852a9bcf0be02cda84f0e44576da8100dec
diff --git a/resources/images/bmp-size-32x32-8bpp.bmp b/resources/images/bmp-size-32x32-8bpp.bmp
new file mode 100644
index 0000000..d2f800d
--- /dev/null
+++ b/resources/images/bmp-size-32x32-8bpp.bmp
Binary files differ
diff --git a/src/codec/SkBmpStandardCodec.cpp b/src/codec/SkBmpStandardCodec.cpp
index 0892120..67cf9cf 100644
--- a/src/codec/SkBmpStandardCodec.cpp
+++ b/src/codec/SkBmpStandardCodec.cpp
@@ -191,7 +191,7 @@
 
     SkImageInfo swizzlerInfo = dstInfo;
     SkCodec::Options swizzlerOptions = opts;
-    if (this->colorXform()) {
+    if (this->xformOnDecode()) {
         swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
         if (kPremul_SkAlphaType == dstInfo.alphaType()) {
             swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp
index 4496bf7..61143b6 100644
--- a/tests/CodecTest.cpp
+++ b/tests/CodecTest.cpp
@@ -38,6 +38,7 @@
 #include "src/base/SkAutoMalloc.h"
 #include "src/base/SkRandom.h"
 #include "src/codec/SkCodecImageGenerator.h"
+#include "src/core/SkAutoPixmapStorage.h"
 #include "src/core/SkColorSpacePriv.h"
 #include "src/core/SkMD5.h"
 #include "src/core/SkStreamPriv.h"
@@ -1957,3 +1958,26 @@
     bool success = codec->getPixels(dstInfo, dstBm.getPixels(), dstBm.rowBytes());
     REPORTER_ASSERT(r, SkCodec::kSuccess == success);
 }
+
+DEF_TEST(Codec_bmp_indexed_colorxform, r) {
+    constexpr char path[] = "images/bmp-size-32x32-8bpp.bmp";
+    std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
+    if (!stream) {
+        SkDebugf("Missing resource '%s'\n", path);
+        return;
+    }
+
+    std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(stream));
+    REPORTER_ASSERT(r, codec);
+
+    // decode to a < 32bpp buffer with a color transform
+    const SkImageInfo decodeInfo = codec->getInfo().makeColorType(kRGB_565_SkColorType)
+                                                   .makeColorSpace(SkColorSpace::MakeSRGBLinear());
+    SkAutoPixmapStorage aps;
+    aps.alloc(decodeInfo);
+
+    // should not crash
+    auto res = codec->getPixels(aps);
+
+    REPORTER_ASSERT(r, res == SkCodec::kSuccess);
+}