Revert "Remove support for decoding to kIndex_8"
This reverts commit 742a3e298fda669006147e4a305bab8452369b1f.
Reason for revert: Breaking Android roll:
frameworks/base/core/jni/android/graphics/BitmapFactory.cpp:453:18: error: no member named 'fColorPtr' in 'SkAndroidCodec::AndroidOptions'
codecOptions.fColorPtr = colorPtr;
~~~~~~~~~~~~ ^
frameworks/base/core/jni/android/graphics/BitmapFactory.cpp:454:18: error: no member named 'fColorCount' in 'SkAndroidCodec::AndroidOptions'
codecOptions.fColorCount = colorCount;
~~~~~~~~~~~~ ^
Original change's description:
> Remove support for decoding to kIndex_8
>
> Fix up callsites, and remove tests that no longer make sense.
>
> Bug: skia:6828
> Change-Id: I2548c4b7528b7b1be7412563156f27b52c9d4295
> Reviewed-on: https://skia-review.googlesource.com/21664
> Reviewed-by: Derek Sollenberger <djsollen@google.com>
> Commit-Queue: Leon Scroggins <scroggo@google.com>
TBR=djsollen@google.com,scroggo@google.com
Change-Id: I1bc669441f250690884e75a9a61427fdf75c6907
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:6828
Reviewed-on: https://skia-review.googlesource.com/22120
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Leon Scroggins <scroggo@google.com>
diff --git a/src/android/SkBitmapRegionCodec.cpp b/src/android/SkBitmapRegionCodec.cpp
index c4e1ea1..7ce9d26 100644
--- a/src/android/SkBitmapRegionCodec.cpp
+++ b/src/android/SkBitmapRegionCodec.cpp
@@ -57,7 +57,13 @@
SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
dstColorType, dstAlphaType, dstColorSpace);
- SkASSERT(dstColorType != kIndex_8_SkColorType);
+ // Construct a color table for the decode if necessary
+ sk_sp<SkColorTable> colorTable(nullptr);
+ int maxColors = 256;
+ SkPMColor colors[256];
+ if (kIndex_8_SkColorType == dstColorType) {
+ colorTable.reset(new SkColorTable(colors, maxColors));
+ }
// Initialize the destination bitmap
int scaledOutX = 0;
@@ -84,7 +90,7 @@
outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
}
bitmap->setInfo(outInfo);
- if (!bitmap->tryAllocPixels(allocator, nullptr)) {
+ if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
SkCodecPrintf("Error: Could not allocate pixels.\n");
return false;
}
@@ -106,6 +112,8 @@
SkAndroidCodec::AndroidOptions options;
options.fSampleSize = sampleSize;
options.fSubset = ⊂
+ options.fColorPtr = colors;
+ options.fColorCount = &maxColors;
options.fZeroInitialized = zeroInit;
void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
@@ -116,6 +124,11 @@
return false;
}
+ // Intialize the color table
+ if (kIndex_8_SkColorType == dstColorType) {
+ colorTable->dangerous_overwriteColors(colors, maxColors);
+ }
+
return true;
}
diff --git a/src/codec/SkBmpCodec.cpp b/src/codec/SkBmpCodec.cpp
index 3142f1e..c21a863 100644
--- a/src/codec/SkBmpCodec.cpp
+++ b/src/codec/SkBmpCodec.cpp
@@ -622,19 +622,19 @@
}
SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
+ const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
if (!conversion_possible(dstInfo, this->getInfo()) ||
!this->initializeColorXform(dstInfo, options.fPremulBehavior))
{
return kInvalidConversion;
}
- return this->onPrepareToDecode(dstInfo, options);
+ return this->onPrepareToDecode(dstInfo, options, inputColorPtr, inputColorCount);
}
SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
- return prepareToDecode(dstInfo, options);
+ const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
+ return prepareToDecode(dstInfo, options, inputColorPtr, inputColorCount);
}
int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
diff --git a/src/codec/SkBmpCodec.h b/src/codec/SkBmpCodec.h
index cf14605..e956a9f 100644
--- a/src/codec/SkBmpCodec.h
+++ b/src/codec/SkBmpCodec.h
@@ -91,11 +91,20 @@
* @param dstInfo Contains output information. Height specifies
* the total number of rows that will be decoded.
* @param options Additonal options to pass to the decoder.
+ * @param inputColorPtr Client-provided memory for a color table. Must
+ * be enough for 256 colors. This will be
+ * populated with colors if the encoded image uses
+ * a color table.
+ * @param inputColorCount If the encoded image uses a color table, this
+ * will be set to the number of colors in the
+ * color table.
*/
virtual SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) = 0;
+ const SkCodec::Options& options, SkPMColor inputColorPtr[],
+ int* inputColorCount) = 0;
SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options);
+ const SkCodec::Options& options, SkPMColor inputColorPtr[],
+ int* inputColorCount);
uint32_t* xformBuffer() const { return fXformBuffer.get(); }
void resetXformBuffer(int count) { fXformBuffer.reset(new uint32_t[count]); }
@@ -135,8 +144,8 @@
virtual bool skipRows(int count);
- Result onStartScanlineDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options&) override;
+ Result onStartScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options&,
+ SkPMColor inputColorPtr[], int* inputColorCount) override;
int onGetScanlines(void* dst, int count, size_t rowBytes) override;
diff --git a/src/codec/SkBmpMaskCodec.cpp b/src/codec/SkBmpMaskCodec.cpp
index 51060e5..1fc98a2 100644
--- a/src/codec/SkBmpMaskCodec.cpp
+++ b/src/codec/SkBmpMaskCodec.cpp
@@ -26,6 +26,8 @@
SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes,
const Options& opts,
+ SkPMColor* inputColorPtr,
+ int* inputColorCount,
int* rowsDecoded) {
if (opts.fSubset) {
// Subsets are not supported.
@@ -36,7 +38,7 @@
return kInvalidScale;
}
- Result result = this->prepareToDecode(dstInfo, opts);
+ Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
if (kSuccess != result) {
return result;
}
@@ -50,7 +52,7 @@
}
SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
+ const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
if (this->colorXform()) {
this->resetXformBuffer(dstInfo.width());
}
diff --git a/src/codec/SkBmpMaskCodec.h b/src/codec/SkBmpMaskCodec.h
index 8d8d64c..2f8c060 100644
--- a/src/codec/SkBmpMaskCodec.h
+++ b/src/codec/SkBmpMaskCodec.h
@@ -38,11 +38,12 @@
protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
- size_t dstRowBytes, const Options&,
- int*) override;
+ size_t dstRowBytes, const Options&, SkPMColor*,
+ int*, int*) override;
SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) override;
+ const SkCodec::Options& options, SkPMColor inputColorPtr[],
+ int* inputColorCount) override;
private:
diff --git a/src/codec/SkBmpRLECodec.cpp b/src/codec/SkBmpRLECodec.cpp
index 0fcd290..c6d788b 100644
--- a/src/codec/SkBmpRLECodec.cpp
+++ b/src/codec/SkBmpRLECodec.cpp
@@ -34,13 +34,15 @@
SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes,
const Options& opts,
+ SkPMColor* inputColorPtr,
+ int* inputColorCount,
int* rowsDecoded) {
if (opts.fSubset) {
// Subsets are not supported.
return kUnimplemented;
}
- Result result = this->prepareToDecode(dstInfo, opts);
+ Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
if (kSuccess != result) {
return result;
}
@@ -61,13 +63,19 @@
/*
* Process the color table for the bmp input
*/
- bool SkBmpRLECodec::createColorTable(SkColorType dstColorType) {
+ bool SkBmpRLECodec::createColorTable(SkColorType dstColorType, int* numColors) {
// Allocate memory for color table
uint32_t colorBytes = 0;
SkPMColor colorTable[256];
if (this->bitsPerPixel() <= 8) {
// Inform the caller of the number of colors
uint32_t maxColors = 1 << this->bitsPerPixel();
+ if (nullptr != numColors) {
+ // We set the number of colors to maxColors in order to ensure
+ // safe memory accesses. Otherwise, an invalid pixel could
+ // access memory outside of our color table array.
+ *numColors = maxColors;
+ }
// Don't bother reading more than maxColors.
const uint32_t numColorsToRead =
fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
@@ -233,7 +241,7 @@
}
SkCodec::Result SkBmpRLECodec::onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
+ const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
// FIXME: Support subsets for scanline decodes.
if (options.fSubset) {
// Subsets are not supported.
@@ -248,17 +256,20 @@
SkColorType colorTableColorType = dstInfo.colorType();
if (this->colorXform()) {
// Just set a known colorType for the colorTable. No need to actually transform
- // the colors in the colorTable.
+ // the colors in the colorTable since we do not allow decoding RLE to kIndex8.
colorTableColorType = kBGRA_8888_SkColorType;
}
// Create the color table if necessary and prepare the stream for decode
// Note that if it is non-NULL, inputColorCount will be modified
- if (!this->createColorTable(colorTableColorType)) {
+ if (!this->createColorTable(colorTableColorType, inputColorCount)) {
SkCodecPrintf("Error: could not create color table.\n");
return SkCodec::kInvalidInput;
}
+ // Copy the color table to the client if necessary
+ copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount);
+
// Initialize a buffer for encoded RLE data
if (!this->initializeStreamBuffer()) {
SkCodecPrintf("Error: cannot initialize stream buffer.\n");
diff --git a/src/codec/SkBmpRLECodec.h b/src/codec/SkBmpRLECodec.h
index d6c17c3..030e827 100644
--- a/src/codec/SkBmpRLECodec.h
+++ b/src/codec/SkBmpRLECodec.h
@@ -44,11 +44,12 @@
protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
- size_t dstRowBytes, const Options&,
- int*) override;
+ size_t dstRowBytes, const Options&, SkPMColor*,
+ int*, int*) override;
SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) override;
+ const SkCodec::Options& options, SkPMColor inputColorPtr[],
+ int* inputColorCount) override;
private:
@@ -56,7 +57,7 @@
* Creates the color table
* Sets colorCount to the new color count if it is non-nullptr
*/
- bool createColorTable(SkColorType dstColorType);
+ bool createColorTable(SkColorType dstColorType, int* colorCount);
bool initializeStreamBuffer();
diff --git a/src/codec/SkBmpStandardCodec.cpp b/src/codec/SkBmpStandardCodec.cpp
index 46a5715..959e75b 100644
--- a/src/codec/SkBmpStandardCodec.cpp
+++ b/src/codec/SkBmpStandardCodec.cpp
@@ -36,6 +36,8 @@
SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes,
const Options& opts,
+ SkPMColor* inputColorPtr,
+ int* inputColorCount,
int* rowsDecoded) {
if (opts.fSubset) {
// Subsets are not supported.
@@ -46,7 +48,7 @@
return kInvalidScale;
}
- Result result = this->prepareToDecode(dstInfo, opts);
+ Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
if (kSuccess != result) {
return result;
}
@@ -61,13 +63,20 @@
/*
* Process the color table for the bmp input
*/
- bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType) {
+ bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType,
+ int* numColors) {
// Allocate memory for color table
uint32_t colorBytes = 0;
SkPMColor colorTable[256];
if (this->bitsPerPixel() <= 8) {
// Inform the caller of the number of colors
uint32_t maxColors = 1 << this->bitsPerPixel();
+ if (nullptr != numColors) {
+ // We set the number of colors to maxColors in order to ensure
+ // safe memory accesses. Otherwise, an invalid pixel could
+ // access memory outside of our color table array.
+ *numColors = maxColors;
+ }
// Don't bother reading more than maxColors.
const uint32_t numColorsToRead =
fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
@@ -182,18 +191,21 @@
}
SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
+ const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
if (this->xformOnDecode()) {
this->resetXformBuffer(dstInfo.width());
}
// Create the color table if necessary and prepare the stream for decode
// Note that if it is non-NULL, inputColorCount will be modified
- if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) {
+ if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) {
SkCodecPrintf("Error: could not create color table.\n");
return SkCodec::kInvalidInput;
}
+ // Copy the color table to the client if necessary
+ copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount);
+
// Initialize a swizzler
this->initializeSwizzler(dstInfo, options);
return SkCodec::kSuccess;
@@ -279,8 +291,9 @@
void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes) {
- // BMP in ICO have transparency, so this cannot be 565. The below code depends
- // on the output being an SkPMColor.
+ // BMP in ICO have transparency, so this cannot be 565, and this mask
+ // prevents us from using kIndex8. The below code depends on the output
+ // being an SkPMColor.
SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
kBGRA_8888_SkColorType == dstInfo.colorType() ||
kRGBA_F16_SkColorType == dstInfo.colorType());
diff --git a/src/codec/SkBmpStandardCodec.h b/src/codec/SkBmpStandardCodec.h
index f9ce5c6..ec3d707 100644
--- a/src/codec/SkBmpStandardCodec.h
+++ b/src/codec/SkBmpStandardCodec.h
@@ -47,15 +47,16 @@
protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
- size_t dstRowBytes, const Options&,
- int*) override;
+ size_t dstRowBytes, const Options&, SkPMColor*,
+ int*, int*) override;
bool onInIco() const override {
return fInIco;
}
SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) override;
+ const SkCodec::Options& options, SkPMColor inputColorPtr[],
+ int* inputColorCount) override;
uint64_t onGetFillValue(const SkImageInfo&) const override;
@@ -69,8 +70,9 @@
/*
* Creates the color table
+ * Sets colorCount to the new color count if it is non-nullptr
*/
- bool createColorTable(SkColorType colorType, SkAlphaType alphaType);
+ bool createColorTable(SkColorType colorType, SkAlphaType alphaType, int* colorCount);
void initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts);
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index 4bd3917..26b31ae 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -167,6 +167,19 @@
return this->onRewind();
}
+#define CHECK_COLOR_TABLE \
+ if (kIndex_8_SkColorType == info.colorType()) { \
+ if (nullptr == ctable || nullptr == ctableCount) { \
+ return SkCodec::kInvalidParameters; \
+ } \
+ } else { \
+ if (ctableCount) { \
+ *ctableCount = 0; \
+ } \
+ ctableCount = nullptr; \
+ ctable = nullptr; \
+ }
+
static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
SkIRect frameRect) {
if (!frameRect.intersect(dstInfo.bounds())) {
@@ -192,6 +205,11 @@
return kInvalidParameters;
}
+ // index 8 is not supported beyond the first frame.
+ if (index < 0 || info.colorType() == kIndex_8_SkColorType) {
+ return kInvalidParameters;
+ }
+
if (index >= this->onGetFrameCount()) {
return kIncompleteInput;
}
@@ -234,7 +252,8 @@
Options prevFrameOptions(options);
prevFrameOptions.fFrameIndex = requiredFrame;
prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
- const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
+ const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions,
+ nullptr, nullptr);
if (result == kSuccess) {
const auto* prevFrame = frameHolder->getFrame(requiredFrame);
const auto disposalMethod = prevFrame->getDisposalMethod();
@@ -247,7 +266,7 @@
}
SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
- const Options* options) {
+ const Options* options, SkPMColor ctable[], int* ctableCount) {
if (kUnknown_SkColorType == info.colorType()) {
return kInvalidConversion;
}
@@ -258,6 +277,8 @@
return kInvalidParameters;
}
+ CHECK_COLOR_TABLE;
+
if (!this->rewindIfNeeded()) {
return kCouldNotRewind;
}
@@ -293,7 +314,14 @@
// On an incomplete decode, the subclass will specify the number of scanlines that it decoded
// successfully.
int rowsDecoded = 0;
- const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
+ const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount,
+ &rowsDecoded);
+
+ if (ctableCount) {
+ if (kIncompleteInput == result || kSuccess == result || kErrorInInput == result) {
+ SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
+ }
+ }
// A return value of kIncompleteInput indicates a truncated image stream.
// In this case, we will fill any uninitialized memory with a default value.
@@ -314,8 +342,12 @@
return result;
}
+SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
+ return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr);
+}
+
SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
- size_t rowBytes, const SkCodec::Options* options) {
+ size_t rowBytes, const SkCodec::Options* options, SkPMColor* ctable, int* ctableCount) {
fStartedIncrementalDecode = false;
if (kUnknown_SkColorType == info.colorType()) {
@@ -325,6 +357,9 @@
return kInvalidParameters;
}
+ // Ensure that valid color ptrs are passed in for kIndex8 color type
+ CHECK_COLOR_TABLE;
+
// FIXME: If the rows come after the rows of a previous incremental decode,
// we might be able to skip the rewind, but only the implementation knows
// that. (e.g. PNG will always need to rewind, since we called longjmp, but
@@ -364,7 +399,8 @@
fDstInfo = info;
fOptions = *options;
- const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
+ const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes,
+ fOptions, ctable, ctableCount);
if (kSuccess == result) {
fStartedIncrementalDecode = true;
} else if (kUnimplemented == result) {
@@ -382,9 +418,11 @@
SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
- const SkCodec::Options* options) {
+ const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) {
// Reset fCurrScanline in case of failure.
fCurrScanline = -1;
+ // Ensure that valid color ptrs are passed in for kIndex8 color type
+ CHECK_COLOR_TABLE;
if (!this->rewindIfNeeded()) {
return kCouldNotRewind;
@@ -412,7 +450,7 @@
return kInvalidScale;
}
- const Result result = this->onStartScanlineDecode(info, *options);
+ const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount);
if (result != SkCodec::kSuccess) {
return result;
}
@@ -423,6 +461,12 @@
return kSuccess;
}
+#undef CHECK_COLOR_TABLE
+
+SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) {
+ return this->startScanlineDecode(info, nullptr, nullptr, nullptr);
+}
+
int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
if (fCurrScanline < 0) {
return 0;
@@ -486,7 +530,7 @@
return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
}
default: {
- // This not only handles the kN32 case, but also k565, kGray8, since
+ // This not only handles the kN32 case, but also k565, kGray8, kIndex8, since
// the low bits are zeros.
return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
SK_ColorBLACK : SK_ColorTRANSPARENT;
@@ -539,6 +583,7 @@
case kBGRA_8888_SkColorType:
return SkColorSpaceXform::kBGRA_8888_ColorFormat;
case kRGB_565_SkColorType:
+ case kIndex_8_SkColorType:
#ifdef SK_PMCOLOR_IS_RGBA
return SkColorSpaceXform::kRGBA_8888_ColorFormat;
#else
diff --git a/src/codec/SkCodecImageGenerator.cpp b/src/codec/SkCodecImageGenerator.cpp
index 57f7fb8..20d547a 100644
--- a/src/codec/SkCodecImageGenerator.cpp
+++ b/src/codec/SkCodecImageGenerator.cpp
@@ -44,7 +44,8 @@
const Options& opts) {
SkCodec::Options codecOpts;
codecOpts.fPremulBehavior = opts.fBehavior;
- SkCodec::Result result = fCodec->getPixels(info, pixels, rowBytes, &codecOpts);
+ SkCodec::Result result = fCodec->getPixels(info, pixels, rowBytes, &codecOpts, nullptr,
+ nullptr);
switch (result) {
case SkCodec::kSuccess:
case SkCodec::kIncompleteInput:
diff --git a/src/codec/SkCodecPriv.h b/src/codec/SkCodecPriv.h
index cb7cd94..b69e488 100644
--- a/src/codec/SkCodecPriv.h
+++ b/src/codec/SkCodecPriv.h
@@ -128,6 +128,8 @@
return colorPtr[fillIndex];
case kRGB_565_SkColorType:
return SkPixel32ToPixel16(colorPtr[fillIndex]);
+ case kIndex_8_SkColorType:
+ return fillIndex;
case kRGBA_F16_SkColorType: {
SkASSERT(colorXform);
uint64_t dstColor;
@@ -146,6 +148,20 @@
}
/*
+ *
+ * Copy the codec color table back to the client when kIndex8 color type is requested
+ */
+static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* colorTable,
+ SkPMColor* inputColorPtr, int* inputColorCount) {
+ if (kIndex_8_SkColorType == dstInfo.colorType()) {
+ SkASSERT(nullptr != inputColorPtr);
+ SkASSERT(nullptr != inputColorCount);
+ SkASSERT(nullptr != colorTable);
+ memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * sizeof(SkPMColor));
+ }
+}
+
+/*
* Compute row bytes for an image using pixels per byte
*/
static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
@@ -315,6 +331,7 @@
* Color Type Conversions
* - Always support kRGBA_8888, kBGRA_8888
* - Support kRGBA_F16 when there is a linear dst color space
+ * - Support kIndex8 if it matches the src
* - Support k565 if kOpaque and color correction is not required
* - Support k565 if it matches the src, kOpaque, and color correction is not required
*/
@@ -331,6 +348,8 @@
return true;
case kRGBA_F16_SkColorType:
return dst.colorSpace() && dst.colorSpace()->gammaIsLinear();
+ case kIndex_8_SkColorType:
+ return kIndex_8_SkColorType == src.colorType();
case kRGB_565_SkColorType:
return kOpaque_SkAlphaType == src.alphaType();
case kGray_8_SkColorType:
diff --git a/src/codec/SkGifCodec.cpp b/src/codec/SkGifCodec.cpp
index 081f237..89889d2 100644
--- a/src/codec/SkGifCodec.cpp
+++ b/src/codec/SkGifCodec.cpp
@@ -94,6 +94,10 @@
// expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
+ // Although the encodedInfo is always kPalette_Color, it is possible that kIndex_8 is
+ // unsupported if the frame is subset and there is no transparent pixel.
+ const auto colorType = reader->firstFrameSupportsIndex8() ? kIndex_8_SkColorType
+ : kN32_SkColorType;
// The choice of unpremul versus premul is arbitrary, since all colors are either fully
// opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all
// zeroes, which is arguably premultiplied.
@@ -101,7 +105,7 @@
: kOpaque_SkAlphaType;
const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
- kN32_SkColorType, alphaType,
+ colorType, alphaType,
SkColorSpace::MakeSRGB());
return new SkGifCodec(encodedInfo, imageInfo, reader.release());
}
@@ -185,7 +189,8 @@
}
-SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
+SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
+ int* inputColorCount, const Options& opts) {
if (opts.fSubset) {
return gif_error("Subsets not supported.\n", kUnimplemented);
}
@@ -246,6 +251,11 @@
this->initializeSwizzler(dstInfo, frameIndex);
SkASSERT(fCurrColorTable);
+ if (inputColorCount) {
+ *inputColorCount = fCurrColorTable->count();
+ }
+ copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCount);
+
return kSuccess;
}
@@ -287,8 +297,10 @@
SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
void* pixels, size_t dstRowBytes,
const Options& opts,
+ SkPMColor* inputColorPtr,
+ int* inputColorCount,
int* rowsDecoded) {
- Result result = this->prepareToDecode(dstInfo, opts);
+ Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
switch (result) {
case kSuccess:
break;
@@ -316,8 +328,10 @@
SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
void* pixels, size_t dstRowBytes,
- const SkCodec::Options& opts) {
- Result result = this->prepareToDecode(dstInfo, opts);
+ const SkCodec::Options& opts,
+ SkPMColor* inputColorPtr,
+ int* inputColorCount) {
+ Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
if (result != kSuccess) {
return result;
}
@@ -409,8 +423,28 @@
}
uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
+ // Note: Using fCurrColorTable relies on having called initializeColorTable already.
+ // This is (currently) safe because this method is only called when filling, after
+ // initializeColorTable has been called.
+ // FIXME: Is there a way to make this less fragile?
+ if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) {
+ // We only support index 8 for the first frame, for backwards
+ // compatibity on Android, so we are using the color table for the first frame.
+ SkASSERT(this->options().fFrameIndex == 0);
+ // Use the transparent index for the first frame.
+ const int transPixel = fReader->frameContext(0)->transparentPixel();
+ if (transPixel >= 0 && transPixel < fCurrColorTable->count()) {
+ return transPixel;
+ }
+ // Fall through to return SK_ColorTRANSPARENT (i.e. 0). This choice is arbitrary,
+ // but we have to pick something inside the color table, and this one is as good
+ // as any.
+ }
// Using transparent as the fill value matches the behavior in Chromium,
// which ignores the background color.
+ // If the colorType is kIndex_8, and there was no color table (i.e.
+ // fCurrColorTableIsReal is false), this value (zero) corresponds to the
+ // only entry in the dummy color table provided to the client.
return SK_ColorTRANSPARENT;
}
diff --git a/src/codec/SkGifCodec.h b/src/codec/SkGifCodec.h
index 7bb86f8..33472d8 100644
--- a/src/codec/SkGifCodec.h
+++ b/src/codec/SkGifCodec.h
@@ -40,7 +40,7 @@
* Performs the full gif decode
*/
Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
- int*) override;
+ SkPMColor*, int*, int*) override;
SkEncodedImageFormat onGetEncodedFormat() const override {
return SkEncodedImageFormat::kGIF;
@@ -55,7 +55,7 @@
int onGetRepetitionCount() override;
Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t,
- const SkCodec::Options&) override;
+ const SkCodec::Options&, SkPMColor*, int*) override;
Result onIncrementalDecode(int*) override;
@@ -74,9 +74,11 @@
void initializeColorTable(const SkImageInfo& dstInfo, int frameIndex);
/*
- * Does necessary setup, including setting up the color table and swizzler.
+ * Does necessary setup, including setting up the color table and swizzler,
+ * and reports color info to the client.
*/
- Result prepareToDecode(const SkImageInfo& dstInfo, const Options& opts);
+ Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
+ int* inputColorCount, const Options& opts);
/*
* Initializes the swizzler.
diff --git a/src/codec/SkIcoCodec.cpp b/src/codec/SkIcoCodec.cpp
index d1cbed0..cde233a 100644
--- a/src/codec/SkIcoCodec.cpp
+++ b/src/codec/SkIcoCodec.cpp
@@ -255,8 +255,8 @@
*/
SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes,
- const Options& opts,
- int* rowsDecoded) {
+ const Options& opts, SkPMColor* colorTable,
+ int* colorCount, int* rowsDecoded) {
if (opts.fSubset) {
// Subsets are not supported.
return kUnimplemented;
@@ -271,7 +271,7 @@
}
SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
- result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts);
+ result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts, colorTable, colorCount);
switch (result) {
case kSuccess:
case kIncompleteInput:
@@ -292,7 +292,7 @@
}
SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) {
+ const SkCodec::Options& options, SkPMColor colorTable[], int* colorCount) {
int index = 0;
SkCodec::Result result = kInvalidScale;
while (true) {
@@ -302,7 +302,7 @@
}
SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
- result = embeddedCodec->startScanlineDecode(dstInfo, &options);
+ result = embeddedCodec->startScanlineDecode(dstInfo, &options, colorTable, colorCount);
if (kSuccess == result) {
fCurrScanlineCodec = embeddedCodec;
fCurrIncrementalCodec = nullptr;
@@ -327,7 +327,8 @@
}
SkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
- void* pixels, size_t rowBytes, const SkCodec::Options& options) {
+ void* pixels, size_t rowBytes, const SkCodec::Options& options,
+ SkPMColor* colorTable, int* colorCount) {
int index = 0;
while (true) {
index = this->chooseCodec(dstInfo.dimensions(), index);
@@ -337,7 +338,7 @@
SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
switch (embeddedCodec->startIncrementalDecode(dstInfo,
- pixels, rowBytes, &options)) {
+ pixels, rowBytes, &options, colorTable, colorCount)) {
case kSuccess:
fCurrIncrementalCodec = embeddedCodec;
fCurrScanlineCodec = nullptr;
@@ -355,7 +356,8 @@
// valid for scanline decoding.
// Once BMP supports incremental decoding this workaround can go
// away.
- if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) {
+ if (embeddedCodec->startScanlineDecode(dstInfo, nullptr,
+ colorTable, colorCount) == kSuccess) {
return kUnimplemented;
}
// Move on to the next embedded codec.
diff --git a/src/codec/SkIcoCodec.h b/src/codec/SkIcoCodec.h
index 9f2457a..2a4efb5 100644
--- a/src/codec/SkIcoCodec.h
+++ b/src/codec/SkIcoCodec.h
@@ -40,7 +40,7 @@
* Initiates the Ico decode
*/
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
- int*) override;
+ SkPMColor*, int*, int*) override;
SkEncodedImageFormat onGetEncodedFormat() const override {
return SkEncodedImageFormat::kICO;
@@ -50,15 +50,15 @@
private:
- Result onStartScanlineDecode(const SkImageInfo& dstInfo,
- const SkCodec::Options& options) override;
+ Result onStartScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options& options,
+ SkPMColor inputColorPtr[], int* inputColorCount) override;
int onGetScanlines(void* dst, int count, size_t rowBytes) override;
bool onSkipScanlines(int count) override;
Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
- const SkCodec::Options&) override;
+ const SkCodec::Options&, SkPMColor*, int*) override;
Result onIncrementalDecode(int* rowsDecoded) override;
diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp
index 7282989..4d97cce 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -556,7 +556,7 @@
*/
SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst, size_t dstRowBytes,
- const Options& options,
+ const Options& options, SkPMColor*, int*,
int* rowsDecoded) {
if (options.fSubset) {
// Subsets are not supported.
@@ -673,7 +673,7 @@
}
SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
- const Options& options) {
+ const Options& options, SkPMColor ctable[], int* ctableCount) {
// Set the jump location for libjpeg errors
if (setjmp(fDecoderMgr->getJmpBuf())) {
SkCodecPrintf("setjmp: Error from libjpeg\n");
diff --git a/src/codec/SkJpegCodec.h b/src/codec/SkJpegCodec.h
index 2d6822e..0c2f4a1 100644
--- a/src/codec/SkJpegCodec.h
+++ b/src/codec/SkJpegCodec.h
@@ -45,7 +45,7 @@
* Initiates the jpeg decode
*/
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
- int*) override;
+ SkPMColor*, int*, int*) override;
bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
@@ -120,8 +120,8 @@
* Scanline decoding.
*/
SkSampler* getSampler(bool createIfNecessary) override;
- Result onStartScanlineDecode(const SkImageInfo& dstInfo,
- const Options& options) override;
+ Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
+ SkPMColor ctable[], int* ctableCount) override;
int onGetScanlines(void* dst, int count, size_t rowBytes) override;
bool onSkipScanlines(int count) override;
diff --git a/src/codec/SkPngCodec.cpp b/src/codec/SkPngCodec.cpp
index ea832ff..d77fe40 100644
--- a/src/codec/SkPngCodec.cpp
+++ b/src/codec/SkPngCodec.cpp
@@ -245,7 +245,7 @@
static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
// Note: SkColorTable claims to store SkPMColors, which is not necessarily the case here.
-bool SkPngCodec::createColorTable(const SkImageInfo& dstInfo) {
+bool SkPngCodec::createColorTable(const SkImageInfo& dstInfo, int* ctableCount) {
int numColors;
png_color* palette;
@@ -308,6 +308,11 @@
sk_memset32(colorTable + numColors, lastColor, maxColors - numColors);
}
+ // Set the new color count.
+ if (ctableCount != nullptr) {
+ *ctableCount = maxColors;
+ }
+
fColorTable.reset(new SkColorTable(colorTable, maxColors));
return true;
}
@@ -968,7 +973,8 @@
// Getting the pixels
///////////////////////////////////////////////////////////////////////////////
-SkCodec::Result SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options) {
+SkCodec::Result SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options,
+ SkPMColor ctable[], int* ctableCount) {
if (setjmp(PNG_JMPBUF((png_struct*)fPng_ptr))) {
SkCodecPrintf("Failed on png_read_update_info.\n");
return kInvalidInput;
@@ -1005,11 +1011,14 @@
}
if (SkEncodedInfo::kPalette_Color == this->getEncodedInfo().color()) {
- if (!this->createColorTable(dstInfo)) {
+ if (!this->createColorTable(dstInfo, ctableCount)) {
return kInvalidInput;
}
}
+ // Copy the color table to the client if they request kIndex8 mode.
+ copy_color_table(dstInfo, fColorTable.get(), ctable, ctableCount);
+
this->initializeSwizzler(dstInfo, options, skipFormatConversion);
return kSuccess;
}
@@ -1083,12 +1092,13 @@
SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t rowBytes, const Options& options,
+ SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) {
if (!conversion_possible(dstInfo, this->getInfo())) {
return kInvalidConversion;
}
- Result result = this->initializeXforms(dstInfo, options);
+ Result result = this->initializeXforms(dstInfo, options, ctable, ctableCount);
if (kSuccess != result) {
return result;
}
@@ -1103,12 +1113,13 @@
}
SkCodec::Result SkPngCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
- void* dst, size_t rowBytes, const SkCodec::Options& options) {
+ void* dst, size_t rowBytes, const SkCodec::Options& options,
+ SkPMColor* ctable, int* ctableCount) {
if (!conversion_possible(dstInfo, this->getInfo())) {
return kInvalidConversion;
}
- Result result = this->initializeXforms(dstInfo, options);
+ Result result = this->initializeXforms(dstInfo, options, ctable, ctableCount);
if (kSuccess != result) {
return result;
}
diff --git a/src/codec/SkPngCodec.h b/src/codec/SkPngCodec.h
index 25a5f07..b329fef 100644
--- a/src/codec/SkPngCodec.h
+++ b/src/codec/SkPngCodec.h
@@ -47,7 +47,7 @@
SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, SkStream*, SkPngChunkReader*,
void* png_ptr, void* info_ptr, int bitDepth);
- Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*)
+ Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
override;
SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kPNG; }
bool onRewind() override;
@@ -73,7 +73,8 @@
void processData();
Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
- const SkCodec::Options&) override;
+ const SkCodec::Options&,
+ SkPMColor* ctable, int* ctableCount) override;
Result onIncrementalDecode(int*) override;
sk_sp<SkPngChunkReader> fPngChunkReader;
@@ -100,9 +101,10 @@
kSwizzleColor_XformMode,
};
- bool createColorTable(const SkImageInfo& dstInfo);
+ bool createColorTable(const SkImageInfo& dstInfo, int* ctableCount);
// Helper to set up swizzler, color xforms, and color table. Also calls png_read_update_info.
- SkCodec::Result initializeXforms(const SkImageInfo& dstInfo, const Options&);
+ SkCodec::Result initializeXforms(const SkImageInfo& dstInfo, const Options&,
+ SkPMColor* colorPtr, int* colorCount);
void initializeSwizzler(const SkImageInfo& dstInfo, const Options&, bool skipFormatConversion);
void allocateStorage(const SkImageInfo& dstInfo);
void destroyReadStruct();
diff --git a/src/codec/SkRawAdapterCodec.cpp b/src/codec/SkRawAdapterCodec.cpp
index 4917091..76cbaa1 100644
--- a/src/codec/SkRawAdapterCodec.cpp
+++ b/src/codec/SkRawAdapterCodec.cpp
@@ -24,5 +24,7 @@
SkCodec::Options codecOptions;
codecOptions.fZeroInitialized = options.fZeroInitialized;
codecOptions.fSubset = options.fSubset;
- return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions);
+ return this->codec()->getPixels(
+ info, pixels, rowBytes, &codecOptions, options.fColorPtr,
+ options.fColorCount);
}
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
index 83b7947..b8bcf19 100644
--- a/src/codec/SkRawCodec.cpp
+++ b/src/codec/SkRawCodec.cpp
@@ -688,6 +688,7 @@
SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t dstRowBytes, const Options& options,
+ SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) {
if (!conversion_possible(dstInfo, this->getInfo()) ||
!this->initializeColorXform(dstInfo, options.fPremulBehavior))
diff --git a/src/codec/SkRawCodec.h b/src/codec/SkRawCodec.h
index d2ef921..51bb234 100644
--- a/src/codec/SkRawCodec.h
+++ b/src/codec/SkRawCodec.h
@@ -35,7 +35,7 @@
protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
- int*) override;
+ SkPMColor*, int*, int*) override;
SkEncodedImageFormat onGetEncodedFormat() const override {
return SkEncodedImageFormat::kDNG;
diff --git a/src/codec/SkSampledCodec.cpp b/src/codec/SkSampledCodec.cpp
index b8e2a56..045f184 100644
--- a/src/codec/SkSampledCodec.cpp
+++ b/src/codec/SkSampledCodec.cpp
@@ -80,7 +80,8 @@
SkIRect* subset = options.fSubset;
if (!subset || subset->size() == this->codec()->getInfo().dimensions()) {
if (this->codec()->dimensionsSupported(info.dimensions())) {
- return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions);
+ return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions,
+ options.fColorPtr, options.fColorCount);
}
// If the native codec does not support the requested scale, scale by sampling.
@@ -111,7 +112,8 @@
scaledSubsetWidth, scaledSubsetHeight);
codecOptions.fSubset = &incrementalSubset;
const SkCodec::Result startResult = this->codec()->startIncrementalDecode(
- scaledInfo, pixels, rowBytes, &codecOptions);
+ scaledInfo, pixels, rowBytes, &codecOptions,
+ options.fColorPtr, options.fColorCount);
if (SkCodec::kSuccess == startResult) {
int rowsDecoded;
const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded);
@@ -138,7 +140,7 @@
codecOptions.fSubset = &scanlineSubset;
SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo,
- &codecOptions);
+ &codecOptions, options.fColorPtr, options.fColorCount);
if (SkCodec::kSuccess != result) {
return result;
}
@@ -228,7 +230,7 @@
incrementalOptions.fSubset = &incrementalSubset;
}
const SkCodec::Result startResult = this->codec()->startIncrementalDecode(nativeInfo,
- pixels, rowBytes, &incrementalOptions);
+ pixels, rowBytes, &incrementalOptions, options.fColorPtr, options.fColorCount);
if (SkCodec::kSuccess == startResult) {
SkSampler* sampler = this->codec()->getSampler(true);
if (!sampler) {
@@ -262,7 +264,7 @@
// Start the scanline decode.
SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo,
- &sampledOptions);
+ &sampledOptions, options.fColorPtr, options.fColorCount);
if (SkCodec::kSuccess != result) {
return result;
}
diff --git a/src/codec/SkWbmpCodec.cpp b/src/codec/SkWbmpCodec.cpp
index fe9a291..780ae5e 100644
--- a/src/codec/SkWbmpCodec.cpp
+++ b/src/codec/SkWbmpCodec.cpp
@@ -21,6 +21,15 @@
return SkAlign8(width) >> 3;
}
+static inline void setup_color_table(SkColorType colorType,
+ SkPMColor* colorPtr, int* colorCount) {
+ if (kIndex_8_SkColorType == colorType) {
+ colorPtr[0] = SK_ColorBLACK;
+ colorPtr[1] = SK_ColorWHITE;
+ *colorCount = 2;
+ }
+}
+
static inline bool valid_color_type(const SkImageInfo& dstInfo) {
switch (dstInfo.colorType()) {
case kRGBA_8888_SkColorType:
@@ -88,8 +97,9 @@
return read_header(this->stream(), nullptr);
}
-SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const Options& opts) {
- return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, info, opts);
+SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
+ const Options& opts) {
+ return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), ctable, info, opts);
}
bool SkWbmpCodec::readRow(uint8_t* row) {
@@ -102,6 +112,7 @@
stream, SkColorSpace::MakeSRGB())
, fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
, fSwizzler(nullptr)
+ , fColorTable(nullptr)
{}
SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
@@ -112,6 +123,8 @@
void* dst,
size_t rowBytes,
const Options& options,
+ SkPMColor ctable[],
+ int* ctableCount,
int* rowsDecoded) {
if (options.fSubset) {
// Subsets are not supported.
@@ -122,8 +135,11 @@
return kInvalidConversion;
}
+ // Prepare a color table if necessary
+ setup_color_table(info.colorType(), ctable, ctableCount);
+
// Initialize the swizzler
- std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, options));
+ std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
SkASSERT(swizzler);
// Perform the decode
@@ -175,7 +191,7 @@
}
SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
- const Options& options) {
+ const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
if (options.fSubset) {
// Subsets are not supported.
return kUnimplemented;
@@ -187,8 +203,16 @@
return kInvalidConversion;
}
+ // Fill in the color table
+ setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
+
+ // Copy the color table to a pointer that can be owned by the scanline decoder
+ if (kIndex_8_SkColorType == dstInfo.colorType()) {
+ fColorTable.reset(new SkColorTable(inputColorTable, 2));
+ }
+
// Initialize the swizzler
- fSwizzler.reset(this->initializeSwizzler(dstInfo, options));
+ fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
SkASSERT(fSwizzler);
fSrcBuffer.reset(fSrcRowBytes);
diff --git a/src/codec/SkWbmpCodec.h b/src/codec/SkWbmpCodec.h
index e8a6e40..40f507e 100644
--- a/src/codec/SkWbmpCodec.h
+++ b/src/codec/SkWbmpCodec.h
@@ -26,13 +26,13 @@
protected:
SkEncodedImageFormat onGetEncodedFormat() const override;
Result onGetPixels(const SkImageInfo&, void*, size_t,
- const Options&, int*) override;
+ const Options&, SkPMColor[], int*, int*) override;
bool onRewind() override;
private:
/*
* Returns a swizzler on success, nullptr on failure
*/
- SkSwizzler* initializeSwizzler(const SkImageInfo& info,
+ SkSwizzler* initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
const Options& opts);
SkSampler* getSampler(bool createIfNecessary) override {
SkASSERT(fSwizzler || !createIfNecessary);
@@ -50,12 +50,13 @@
// Used for scanline decodes:
std::unique_ptr<SkSwizzler> fSwizzler;
+ sk_sp<SkColorTable> fColorTable;
SkAutoTMalloc<uint8_t> fSrcBuffer;
int onGetScanlines(void* dst, int count, size_t dstRowBytes) override;
bool onSkipScanlines(int count) override;
- Result onStartScanlineDecode(const SkImageInfo& dstInfo,
- const Options& options) override;
+ Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
+ SkPMColor inputColorTable[], int* inputColorCount) override;
typedef SkCodec INHERITED;
};
diff --git a/src/codec/SkWebpAdapterCodec.cpp b/src/codec/SkWebpAdapterCodec.cpp
index 93400d0..cf1f0e0 100644
--- a/src/codec/SkWebpAdapterCodec.cpp
+++ b/src/codec/SkWebpAdapterCodec.cpp
@@ -41,5 +41,6 @@
codecOptions.fZeroInitialized = options.fZeroInitialized;
codecOptions.fSubset = options.fSubset;
codecOptions.fPremulBehavior = SkTransferFunctionBehavior::kIgnore;
- return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions);
+ return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions, options.fColorPtr,
+ options.fColorCount);
}
diff --git a/src/codec/SkWebpCodec.cpp b/src/codec/SkWebpCodec.cpp
index e8e409a..d94011a 100644
--- a/src/codec/SkWebpCodec.cpp
+++ b/src/codec/SkWebpCodec.cpp
@@ -378,7 +378,8 @@
}
SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
- const Options& options, int* rowsDecodedPtr) {
+ const Options& options, SkPMColor*, int*,
+ int* rowsDecodedPtr) {
const int index = options.fFrameIndex;
SkASSERT(0 == index || index < fFrameHolder.size());
diff --git a/src/codec/SkWebpCodec.h b/src/codec/SkWebpCodec.h
index c911ebd..fc9e683 100644
--- a/src/codec/SkWebpCodec.h
+++ b/src/codec/SkWebpCodec.h
@@ -31,7 +31,8 @@
static SkCodec* NewFromStream(SkStream*);
static bool IsWebp(const void*, size_t);
protected:
- Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
+ Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
+ override;
SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; }
SkISize onGetScaledDimensions(float desiredScale) const override;