Chromium Code Reviews| Index: src/codec/SkCodec_wbmp.cpp |
| diff --git a/src/codec/SkCodec_wbmp.cpp b/src/codec/SkCodec_wbmp.cpp |
| index 35ac808219c625ad1bacf045d6414fb4b8e40b76..aa7999909b6b998166c03bb2bab5029ab30f852a 100644 |
| --- a/src/codec/SkCodec_wbmp.cpp |
| +++ b/src/codec/SkCodec_wbmp.cpp |
| @@ -7,9 +7,27 @@ |
| #include "SkCodec.h" |
| #include "SkColorPriv.h" |
| +#include "SkColorTable.h" |
| #include "SkStream.h" |
| #include "SkCodec_wbmp.h" |
| +// Each bit represents a pixel, so width is actually a number of bits. |
| +// A row will always be stored in bytes, so we round width up to the |
| +// nearest multiple of 8 to get the number of bits actually in the row. |
| +// We then divide by 8 to convert to bytes. |
| +static inline size_t get_src_row_bytes(int width) { |
| + 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; |
| + } |
| +} |
| + |
| // http://en.wikipedia.org/wiki/Variable-length_quantity |
| static bool read_mbf(SkStream* stream, uint64_t* value) { |
| uint64_t n = 0; |
| @@ -38,7 +56,7 @@ static bool read_header(SkStream* stream, SkISize* size) { |
| if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| return false; |
| } |
| - if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { |
| + if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
| return false; |
| } |
| if (size) { |
| @@ -47,49 +65,20 @@ static bool read_header(SkStream* stream, SkISize* size) { |
| return true; |
| } |
| -#define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) |
| -#define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) |
| - |
| -#define GRAYSCALE_BLACK 0 |
| -#define GRAYSCALE_WHITE 0xFF |
| - |
| -#define RGB565_BLACK 0 |
| -#define RGB565_WHITE 0xFFFF |
| - |
| -static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } |
| - |
| -static uint8_t bit_to_bit(U8CPU bit) { return bit; } |
| - |
| -static uint8_t bit_to_grayscale(U8CPU bit) { |
| - return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; |
| -} |
| - |
| -static uint16_t bit_to_rgb565(U8CPU bit) { |
| - return bit ? RGB565_WHITE : RGB565_BLACK; |
| -} |
| +SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, |
| + const SkPMColor* ctable, const Options& opts) { |
| + // TODO (msarett): Reenable support for 565 if it is desired |
| + // skbug.com/3683 |
| -typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int); |
| - |
| -// TODO(halcanary): Add this functionality (grayscale and indexed output) to |
| -// SkSwizzler and use it here. |
| -template <typename T, T (*TRANSFORM)(U8CPU)> |
| -static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) { |
| - T* dst = reinterpret_cast<T*>(dstptr); |
| - int bytes = bits >> 3; |
| - for (int i = 0; i < bytes; i++) { |
| - U8CPU mask = *src++; |
| - for (int j = 0; j < 8; j++) { |
| - dst[j] = TRANSFORM((mask >> (7 - j)) & 1); |
| - } |
| - dst += 8; |
| - } |
| - bits &= 7; |
| - if (bits > 0) { |
| - U8CPU mask = *src; |
| - do { |
| - *dst++ = TRANSFORM((mask >> 7) & 1); |
| - mask <<= 1; |
| - } while (--bits != 0); |
| + // Create the swizzler based on the desired color type |
| + switch (info.colorType()) { |
| + case kIndex_8_SkColorType: |
| + case kN32_SkColorType: |
| + case kGray_8_SkColorType: |
| + return SkSwizzler::CreateSwizzler( |
| + SkSwizzler::kBit, ctable, info, opts.fZeroInitialized); |
| + default: |
| + return NULL; |
| } |
| } |
| @@ -101,7 +90,7 @@ SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { |
| } |
| SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
| - void* pixels, |
| + void* dst, |
| size_t rowBytes, |
| const Options& options, |
| SkPMColor ctable[], |
| @@ -119,36 +108,29 @@ SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
| if (info.dimensions() != this->getInfo().dimensions()) { |
| return kInvalidScale; |
| } |
| - ExpandProc proc = NULL; |
| - switch (info.colorType()) { |
| - case kGray_8_SkColorType: |
| - proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; |
| - break; |
| - case kN32_SkColorType: |
| - proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; |
| - break; |
| - case kIndex_8_SkColorType: |
| - ctable[0] = BLACK; |
| - ctable[1] = WHITE; |
| - *ctableCount = 2; |
| - proc = expand_bits_to_T<uint8_t, bit_to_bit>; |
| - break; |
| - case kRGB_565_SkColorType: |
| - proc = expand_bits_to_T<uint16_t, bit_to_rgb565>; |
| - break; |
| - default: |
| - return kInvalidConversion; |
| + |
| + // Prepare a color table if necessary |
| + setup_color_table(info.colorType(), ctable, ctableCount); |
| + |
| + |
| + // Initialize the swizzler |
| + SkAutoTDelete<SkSwizzler> swizzler( |
| + initializeSwizzler(info, ctable, options)); |
| + if (NULL == swizzler.get()) { |
| + return kInvalidConversion; |
| } |
| + |
| + // Perform the decode |
| SkISize size = info.dimensions(); |
| - uint8_t* dst = static_cast<uint8_t*>(pixels); |
| - size_t srcRowBytes = SkAlign8(size.width()) >> 3; |
| + size_t srcRowBytes = get_src_row_bytes(this->getInfo().width()); |
| SkAutoTMalloc<uint8_t> src(srcRowBytes); |
| + void* dstRow = dst; |
| for (int y = 0; y < size.height(); ++y) { |
| - if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { |
| - return kIncompleteInput; |
| + if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) { |
| + return SkCodec::kIncompleteInput; |
| } |
| - proc(dst, src.get(), size.width()); |
| - dst += rowBytes; |
| + swizzler->swizzle(dstRow, src.get()); |
| + dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
| } |
| return kSuccess; |
| } |
| @@ -168,3 +150,73 @@ SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { |
| kOpaque_SkAlphaType); |
| return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); |
| } |
| + |
| +class SkWbmpScanlineDecoder : public SkScanlineDecoder { |
| +public: |
| + /* |
| + * Takes ownership of both pointer paramters. |
| + */ |
| + SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec, |
| + SkSwizzler* swizzler) |
| + : INHERITED(dstInfo) |
| + , fCodec(codec) |
| + , fSwizzler(swizzler) |
| + , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width())) |
| + , fSrcBuffer(fSrcRowBytes) |
| + {} |
| + |
| + SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override { |
| + void* dstRow = dst; |
| + for (int y = 0; y < count; ++y) { |
| + if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRowBytes) { |
| + return SkCodec::kIncompleteInput; |
| + } |
| + fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| + dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| + } |
| + return SkCodec::kSuccess; |
| + } |
| + |
| +private: |
| + SkAutoTDelete<SkWbmpCodec> fCodec; |
| + SkAutoTDelete<SkSwizzler> fSwizzler; |
| + const size_t fSrcRowBytes; |
| + SkAutoTMalloc<uint8_t> fSrcBuffer; |
| + |
| + typedef SkScanlineDecoder INHERITED; |
| +}; |
| + |
| + |
| +SkScanlineDecoder* SkWbmpCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| + const Options& options, SkPMColor ctable[], int* ctableCount) { |
| + if (options.fSubset) { |
| + // Subsets are not supported. |
| + return NULL; |
| + } |
| + if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| + return NULL; |
| + } |
| + // Create a new SkWbmpCodec, to be owned by the scanline decoder. |
| + SkStream* stream = this->stream()->duplicate(); |
| + if (!stream) { |
| + return NULL; |
| + } |
| + SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>( |
| + SkWbmpCodec::NewFromStream(stream))); |
| + if (!codec) { |
| + return NULL; |
| + } |
| + |
| + // Prepare a color table |
| + setup_color_table(dstInfo.colorType(), ctable, ctableCount); |
| + |
| + // Initialize the swizzler |
| + SkAutoTDelete<SkSwizzler> swizzler( |
| + initializeSwizzler(dstInfo, ctable, options)); |
|
scroggo
2015/07/29 17:47:12
This seems dangerous. The swizzler will hang on to
msarett
2015/07/29 18:53:57
Gotcha agreed.
|
| + if (NULL == swizzler.get()) { |
| + return NULL; |
| + } |
| + |
| + return SkNEW_ARGS(SkWbmpScanlineDecoder, (dstInfo, codec.detach(), |
| + swizzler.detach())); |
| +} |