Fix colorType/alphaType checks in SkCodec
authormsarett <msarett@google.com>
Fri, 12 Feb 2016 21:07:15 +0000 (13:07 -0800)
committerCommit bot <commit-bot@chromium.org>
Fri, 12 Feb 2016 21:07:15 +0000 (13:07 -0800)
Make getPixels() and startScanlineDecode() behave
consistently.

Require that kGray8 decodes are opaque.

Assert that creating the swizzler succeeds.

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

Review URL: https://codereview.chromium.org/1695473002

src/codec/SkBmpMaskCodec.cpp
src/codec/SkBmpMaskCodec.h
src/codec/SkBmpStandardCodec.cpp
src/codec/SkBmpStandardCodec.h
src/codec/SkCodec.cpp
src/codec/SkCodecPriv.h
src/codec/SkGifCodec.cpp
src/codec/SkGifCodec.h
src/codec/SkJpegCodec.cpp
src/codec/SkWbmpCodec.cpp

index b603de9..21d231b 100644 (file)
@@ -57,25 +57,12 @@ SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo,
     return kSuccess;
 }
 
-bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) {
-    // Create the swizzler
-    fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(dstInfo, this->getInfo(), fMasks,
-            this->bitsPerPixel(), options));
-
-    if (nullptr == fMaskSwizzler.get()) {
-        return false;
-    }
-
-    return true;
-}
-
 SkCodec::Result SkBmpMaskCodec::prepareToDecode(const SkImageInfo& dstInfo,
         const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
-    // Initialize a the mask swizzler
-    if (!this->initializeSwizzler(dstInfo, options)) {
-        SkCodecPrintf("Error: cannot initialize swizzler.\n");
-        return SkCodec::kInvalidConversion;
-    }
+    // Initialize the mask swizzler
+    fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(dstInfo, this->getInfo(), fMasks,
+            this->bitsPerPixel(), options));
+    SkASSERT(fMaskSwizzler);
 
     return SkCodec::kSuccess;
 }
index 73a8b37..93b3bca 100644 (file)
@@ -44,7 +44,6 @@ protected:
 
 private:
 
-    bool initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
     SkSampler* getSampler(bool createIfNecessary) override {
         SkASSERT(fMaskSwizzler);
         return fMaskSwizzler;
index 47b1070..39d357b 100644 (file)
@@ -152,9 +152,9 @@ SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
     return true;
 }
 
-bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
+void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
     // Get swizzler configuration
-    SkSwizzler::SrcConfig config;
+    SkSwizzler::SrcConfig config = SkSwizzler::kUnknown;
     switch (this->bitsPerPixel()) {
         case 1:
             config = SkSwizzler::kIndex1;
@@ -180,7 +180,6 @@ bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Op
             break;
         default:
             SkASSERT(false);
-            return false;
     }
 
     // Get a pointer to the color table if it exists
@@ -188,11 +187,7 @@ bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Op
 
     // Create swizzler
     fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts));
-
-    if (nullptr == fSwizzler.get()) {
-        return false;
-    }
-    return true;
+    SkASSERT(fSwizzler);
 }
 
 SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
@@ -207,11 +202,8 @@ SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
     // Copy the color table to the client if necessary
     copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
 
-    // Initialize a swizzler if necessary
-    if (!this->initializeSwizzler(dstInfo, options)) {
-        SkCodecPrintf("Error: cannot initialize swizzler.\n");
-        return SkCodec::kInvalidConversion;
-    }
+    // Initialize a swizzler
+    this->initializeSwizzler(dstInfo, options);
     return SkCodec::kSuccess;
 }
 
index d3e2ed3..557f653 100644 (file)
@@ -72,7 +72,7 @@ private:
      */
     bool createColorTable(SkAlphaType alphaType, int* colorCount);
 
-    bool initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts);
+    void initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts);
 
     int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
             const Options& opts) override;
index 0da36bb..bb7b265 100644 (file)
@@ -162,15 +162,6 @@ SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t
         ctable = nullptr;
     }
 
-    {
-        SkAlphaType canonical;
-        if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &canonical)
-            || canonical != info.alphaType())
-        {
-            return kInvalidConversion;
-        }
-    }
-
     if (!this->rewindIfNeeded()) {
         return kCouldNotRewind;
     }
index 235c4e2..5d66118 100644 (file)
@@ -136,7 +136,12 @@ inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src)
         case kN32_SkColorType:
             return true;
         case kRGB_565_SkColorType:
-            return src.alphaType() == kOpaque_SkAlphaType;
+            return kOpaque_SkAlphaType == dst.alphaType();
+        case kGray_8_SkColorType:
+            if (kOpaque_SkAlphaType != dst.alphaType()) {
+                return false;
+            }
+            // Fall through
         default:
             return dst.colorType() == src.colorType();
     }
index a938f5f..785c17e 100644 (file)
@@ -436,19 +436,16 @@ SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColo
     // Initialize color table and copy to the client if necessary
     this->initializeColorTable(dstInfo, inputColorPtr, inputColorCount);
 
-    return this->initializeSwizzler(dstInfo, opts);
+    this->initializeSwizzler(dstInfo, opts);
+    return kSuccess;
 }
 
-SkCodec::Result SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
+void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
     const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
     const SkIRect* frameRect = fFrameIsSubset ? &fFrameRect : nullptr;
     fSwizzler.reset(SkSwizzler::CreateSwizzler(SkSwizzler::kIndex, colorPtr, dstInfo, opts,
             frameRect));
-
-    if (nullptr != fSwizzler.get()) {
-        return kSuccess;
-    }
-    return kUnimplemented;
+    SkASSERT(fSwizzler);
 }
 
 bool SkGifCodec::readRow() {
index 91861b2..010a344 100644 (file)
@@ -128,7 +128,7 @@ private:
      * @param options  Informs the swizzler if destination memory is zero initialized.
      *                 Contains subset information.
      */
-    Result initializeSwizzler(const SkImageInfo& dstInfo,
+    void initializeSwizzler(const SkImageInfo& dstInfo,
             const Options& options);
 
     SkSampler* getSampler(bool createIfNecessary) override {
index f920c98..e1440af 100644 (file)
@@ -372,6 +372,7 @@ void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options&
     }
 
     fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, options));
+    SkASSERT(fSwizzler);
     fStorage.reset(get_row_bytes(fDecoderMgr->dinfo()));
     fSrcRow = fStorage.get();
 }
index af1ab46..706d5dd 100644 (file)
@@ -30,6 +30,19 @@ static inline void setup_color_table(SkColorType colorType,
     }
 }
 
+static inline bool valid_color_type(SkColorType colorType, SkAlphaType alphaType) {
+    switch (colorType) {
+        case kN32_SkColorType:
+        case kIndex_8_SkColorType:
+            return true;
+        case kGray_8_SkColorType:
+        case kRGB_565_SkColorType:
+            return kOpaque_SkAlphaType == alphaType;
+        default:
+            return false;
+    }
+}
+
 static bool read_byte(SkStream* stream, uint8_t* data)
 {
     return stream->read(data, 1) == 1;
@@ -84,16 +97,6 @@ bool SkWbmpCodec::onRewind() {
 
 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
         const Options& opts) {
-    // Create the swizzler based on the desired color type
-    switch (info.colorType()) {
-        case kIndex_8_SkColorType:
-        case kN32_SkColorType:
-        case kRGB_565_SkColorType:
-        case kGray_8_SkColorType:
-            break;
-        default:
-            return nullptr;
-    }
     return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts);
 }
 
@@ -124,7 +127,8 @@ SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
         return kUnimplemented;
     }
 
-    if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
+    if (!valid_color_type(info.colorType(), info.alphaType()) ||
+            !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
         return kInvalidConversion;
     }
 
@@ -133,9 +137,7 @@ SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
 
     // Initialize the swizzler
     SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
-    if (nullptr == swizzler.get()) {
-        return kInvalidConversion;
-    }
+    SkASSERT(swizzler);
 
     // Perform the decode
     SkISize size = info.dimensions();
@@ -193,7 +195,8 @@ SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
         return kUnimplemented;
     }
 
-    if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
+    if (!valid_color_type(dstInfo.colorType(), dstInfo.alphaType()) ||
+            !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
         return kInvalidConversion;
     }
 
@@ -207,9 +210,7 @@ SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
 
     // Initialize the swizzler
     fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
-    if (nullptr == fSwizzler.get()) {
-        return kInvalidConversion;
-    }
+    SkASSERT(fSwizzler);
 
     fSrcBuffer.reset(fSrcRowBytes);