aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/codec/SkCodec_wbmp.cpp
diff options
context:
space:
mode:
authorGravatar scroggo <scroggo@google.com>2015-09-30 08:57:13 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-09-30 08:57:14 -0700
commit46c574725676b26ada63ac15e42cda309dcd5090 (patch)
treed9ee41e9f1c607ff139eca622bb03809eb9a83d3 /src/codec/SkCodec_wbmp.cpp
parente2bcec38486f2e1a897973c953e5610eaef4221e (diff)
Merge SkCodec with SkScanlineDecoder
Benefits: - This mimics other decoding APIs (including the ones SkCodec relies on, e.g. a png_struct, which can be used to decode an entire image or one line at a time). - It allows a client to ask us to do what we can do efficiently - i.e. start from encoded data and either decode the whole thing or scanlines. - It removes the duplicate methods which appeared in both SkCodec and SkScanlineDecoder (some of which, e.g. in SkJpegScanlineDecoder, just call fCodec->sameMethod()). - It simplifies moving more checks into the base class (e.g. the examples in skbug.com/4284). BUG=skia:4175 BUG=skia:4284 ===================================================================== SkScanlineDecoder.h/.cpp: Removed. SkCodec.h/.cpp: Add methods, enums, and variables which were previously in SkScanlineDecoder. Default fCurrScanline to -1, as a sentinel that start has not been called. General changes: Convert SkScanlineDecoders to SkCodecs. General changes in SkCodec subclasses: Merge SkScanlineDecoder implementation into SkCodec. Most (all?) owned an SkCodec, so they now call this-> instead of fCodec->. SkBmpCodec.h/.cpp: Replace the unused rowOrder method with an override for onGetScanlineOrder. Make getDstRow const, since it is called by onGetY, which is const. SkCodec_libpng.h/.cpp: Make SkPngCodec an abstract class, with two subclasses which handle scanline decoding separately (they share code for decoding the entire image). Reimplement onReallyHasAlpha so that it can return the most recent result (e.g. after a scanline decode which only decoded part of the image) or a better answer (e.g. if the whole image is known to be opaque). Compute fNumberPasses early, so we know which subclass to instantiate. Make SkPngInterlaceScanlineDecoder use the base class' fCurrScanline rather than a separate variable. CodexTest.cpp: Add tests for the state changes in SkCodec (need to call start before decoding scanlines; calling getPixels means that start will need to be called again before decoding more scanlines). Add a test which decodes in stripes, currently only used for an interlaced PNG. TODO: Add tests for onReallyHasAlpha. Review URL: https://codereview.chromium.org/1365313002
Diffstat (limited to 'src/codec/SkCodec_wbmp.cpp')
-rw-r--r--src/codec/SkCodec_wbmp.cpp117
1 files changed, 42 insertions, 75 deletions
diff --git a/src/codec/SkCodec_wbmp.cpp b/src/codec/SkCodec_wbmp.cpp
index 22f2bacce0..0c85eada7c 100644
--- a/src/codec/SkCodec_wbmp.cpp
+++ b/src/codec/SkCodec_wbmp.cpp
@@ -96,6 +96,8 @@ SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) {
SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
: INHERITED(info, stream)
, fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
+ , fColorTable(nullptr)
+ , fSwizzler(nullptr)
{}
SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
@@ -120,7 +122,7 @@ SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
}
if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
- return SkCodec::kInvalidConversion;
+ return kInvalidConversion;
}
// Prepare a color table if necessary
@@ -163,90 +165,55 @@ SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
return new SkWbmpCodec(info, streamDeleter.detach());
}
-class SkWbmpScanlineDecoder : public SkScanlineDecoder {
-public:
- /*
- * Takes ownership of all pointer paramters.
- */
- SkWbmpScanlineDecoder(SkWbmpCodec* codec)
- : INHERITED(codec->getInfo())
- , fCodec(codec)
- , fColorTable(nullptr)
- , fSwizzler(nullptr)
- , fSrcBuffer(codec->fSrcRowBytes)
- {}
-
- SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
- void* dstRow = dst;
- for (int y = 0; y < count; ++y) {
- SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get());
- if (SkCodec::kSuccess != rowResult) {
- return rowResult;
- }
- fSwizzler->swizzle(dstRow, fSrcBuffer.get());
- dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
+SkCodec::Result SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
+ void* dstRow = dst;
+ for (int y = 0; y < count; ++y) {
+ Result rowResult = this->readRow(fSrcBuffer.get());
+ if (kSuccess != rowResult) {
+ return rowResult;
}
- return SkCodec::kSuccess;
+ fSwizzler->swizzle(dstRow, fSrcBuffer.get());
+ dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
}
+ return kSuccess;
+}
- SkCodec::Result onStart(const SkImageInfo& dstInfo,
- const SkCodec::Options& options, SkPMColor inputColorTable[],
- int* inputColorCount) {
- if (!fCodec->rewindIfNeeded()) {
- return SkCodec::kCouldNotRewind;
- }
- if (options.fSubset) {
- // Subsets are not supported.
- return SkCodec::kUnimplemented;
- }
- if (dstInfo.dimensions() != this->getInfo().dimensions()) {
- if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
- return SkCodec::kInvalidScale;
- }
- }
-
- if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
- return SkCodec::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(fCodec->initializeSwizzler(dstInfo,
- get_color_ptr(fColorTable.get()), options));
- if (nullptr == fSwizzler.get()) {
- return SkCodec::kInvalidConversion;
+SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
+ const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
+ if (!this->rewindIfNeeded()) {
+ return kCouldNotRewind;
+ }
+ if (options.fSubset) {
+ // Subsets are not supported.
+ return kUnimplemented;
+ }
+ if (dstInfo.dimensions() != this->getInfo().dimensions()) {
+ if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
+ return kInvalidScale;
}
-
- return SkCodec::kSuccess;
}
- SkEncodedFormat onGetEncodedFormat() const {
- return kWBMP_SkEncodedFormat;
+ if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
+ return kInvalidConversion;
}
-private:
- SkAutoTDelete<SkWbmpCodec> fCodec;
- SkAutoTUnref<SkColorTable> fColorTable;
- SkAutoTDelete<SkSwizzler> fSwizzler;
- SkAutoTMalloc<uint8_t> fSrcBuffer;
+ // Fill in the color table
+ setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
- typedef SkScanlineDecoder INHERITED;
-};
+ // 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));
+ }
-SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
- SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
- SkWbmpCodec::NewFromStream(stream)));
- if (!codec) {
- return nullptr;
+ // Initialize the swizzler
+ fSwizzler.reset(this->initializeSwizzler(dstInfo,
+ get_color_ptr(fColorTable.get()), options));
+ if (nullptr == fSwizzler.get()) {
+ return kInvalidConversion;
}
- // Return the new scanline decoder
- return new SkWbmpScanlineDecoder(codec.detach());
+ fSrcBuffer.reset(fSrcRowBytes);
+
+ return kSuccess;
}
+