Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkCodecPriv.h" | |
| 9 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkColorTable.h" | |
| 10 #include "SkStream.h" | 12 #include "SkStream.h" |
| 11 #include "SkCodec_wbmp.h" | 13 #include "SkCodec_wbmp.h" |
| 12 | 14 |
| 15 // Each bit represents a pixel, so width is actually a number of bits. | |
| 16 // A row will always be stored in bytes, so we round width up to the | |
| 17 // nearest multiple of 8 to get the number of bits actually in the row. | |
| 18 // We then divide by 8 to convert to bytes. | |
| 19 static inline size_t get_src_row_bytes(int width) { | |
| 20 return SkAlign8(width) >> 3; | |
| 21 } | |
| 22 | |
| 23 static inline void setup_color_table(SkColorType colorType, | |
| 24 SkPMColor* colorPtr, int* colorCount) { | |
| 25 if (kIndex_8_SkColorType == colorType) { | |
| 26 colorPtr[0] = SK_ColorBLACK; | |
| 27 colorPtr[1] = SK_ColorWHITE; | |
| 28 *colorCount = 2; | |
| 29 } | |
| 30 } | |
| 31 | |
| 13 // http://en.wikipedia.org/wiki/Variable-length_quantity | 32 // http://en.wikipedia.org/wiki/Variable-length_quantity |
| 14 static bool read_mbf(SkStream* stream, uint64_t* value) { | 33 static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 15 uint64_t n = 0; | 34 uint64_t n = 0; |
| 16 uint8_t data; | 35 uint8_t data; |
| 17 const uint64_t kLimit = 0xFE00000000000000; | 36 const uint64_t kLimit = 0xFE00000000000000; |
| 18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); | 37 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); |
| 19 do { | 38 do { |
| 20 if (n & kLimit) { // Will overflow on shift by 7. | 39 if (n & kLimit) { // Will overflow on shift by 7. |
| 21 return false; | 40 return false; |
| 22 } | 41 } |
| 23 if (stream->read(&data, 1) != 1) { | 42 if (stream->read(&data, 1) != 1) { |
| 24 return false; | 43 return false; |
| 25 } | 44 } |
| 26 n = (n << 7) | (data & 0x7F); | 45 n = (n << 7) | (data & 0x7F); |
| 27 } while (data & 0x80); | 46 } while (data & 0x80); |
| 28 *value = n; | 47 *value = n; |
| 29 return true; | 48 return true; |
| 30 } | 49 } |
| 31 | 50 |
| 32 static bool read_header(SkStream* stream, SkISize* size) { | 51 static bool read_header(SkStream* stream, SkISize* size) { |
| 33 uint64_t width, height; | 52 uint64_t width, height; |
| 34 uint16_t data; | 53 uint16_t data; |
| 35 if (stream->read(&data, 2) != 2 || data != 0) { | 54 if (stream->read(&data, 2) != 2 || data != 0) { |
| 36 return false; | 55 return false; |
| 37 } | 56 } |
| 38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { | 57 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| 39 return false; | 58 return false; |
| 40 } | 59 } |
| 41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { | 60 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
| 42 return false; | 61 return false; |
| 43 } | 62 } |
| 44 if (size) { | 63 if (size) { |
| 45 *size = SkISize::Make(SkToS32(width), SkToS32(height)); | 64 *size = SkISize::Make(SkToS32(width), SkToS32(height)); |
| 46 } | 65 } |
| 47 return true; | 66 return true; |
| 48 } | 67 } |
| 49 | 68 |
| 50 #define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) | 69 bool SkWbmpCodec::handleRewind() { |
| 51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) | 70 SkCodec::RewindState rewindState = this->rewindIfNeeded(); |
| 52 | 71 if (rewindState == kCouldNotRewind_RewindState) { |
| 53 #define GRAYSCALE_BLACK 0 | 72 return false; |
| 54 #define GRAYSCALE_WHITE 0xFF | 73 } else if (rewindState == kRewound_RewindState) { |
| 55 | 74 (void)read_header(this->stream(), NULL); |
| 56 #define RGB565_BLACK 0 | 75 } |
| 57 #define RGB565_WHITE 0xFFFF | 76 return true; |
| 58 | |
| 59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } | |
| 60 | |
| 61 static uint8_t bit_to_bit(U8CPU bit) { return bit; } | |
| 62 | |
| 63 static uint8_t bit_to_grayscale(U8CPU bit) { | |
| 64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; | |
| 65 } | 77 } |
| 66 | 78 |
| 67 static uint16_t bit_to_rgb565(U8CPU bit) { | 79 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, |
| 68 return bit ? RGB565_WHITE : RGB565_BLACK; | 80 const SkPMColor* ctable, const Options& opts) { |
| 69 } | 81 // TODO (msarett): Reenable support for 565 if it is desired |
| 82 // skbug.com/3683 | |
| 70 | 83 |
| 71 typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int); | 84 // Create the swizzler based on the desired color type |
| 72 | 85 switch (info.colorType()) { |
| 73 // TODO(halcanary): Add this functionality (grayscale and indexed output) to | 86 case kIndex_8_SkColorType: |
| 74 // SkSwizzler and use it here. | 87 case kN32_SkColorType: |
| 75 template <typename T, T (*TRANSFORM)(U8CPU)> | 88 case kGray_8_SkColorType: |
| 76 static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) { | 89 return SkSwizzler::CreateSwizzler( |
| 77 T* dst = reinterpret_cast<T*>(dstptr); | 90 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized); |
| 78 int bytes = bits >> 3; | 91 default: |
| 79 for (int i = 0; i < bytes; i++) { | 92 return NULL; |
| 80 U8CPU mask = *src++; | |
| 81 for (int j = 0; j < 8; j++) { | |
| 82 dst[j] = TRANSFORM((mask >> (7 - j)) & 1); | |
| 83 } | |
| 84 dst += 8; | |
| 85 } | |
| 86 bits &= 7; | |
| 87 if (bits > 0) { | |
| 88 U8CPU mask = *src; | |
| 89 do { | |
| 90 *dst++ = TRANSFORM((mask >> 7) & 1); | |
| 91 mask <<= 1; | |
| 92 } while (--bits != 0); | |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 | 95 |
| 96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) | 96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) |
| 97 : INHERITED(info, stream) {} | 97 : INHERITED(info, stream) |
| 98 , fSwizzler(NULL) | |
| 99 {} | |
| 98 | 100 |
| 99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { | 101 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 100 return kWBMP_SkEncodedFormat; | 102 return kWBMP_SkEncodedFormat; |
| 101 } | 103 } |
| 102 | 104 |
| 103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, | 105 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
| 104 void* pixels, | 106 void* dst, |
| 105 size_t rowBytes, | 107 size_t rowBytes, |
| 106 const Options& options, | 108 const Options& options, |
| 107 SkPMColor ctable[], | 109 SkPMColor ctable[], |
| 108 int* ctableCount) { | 110 int* ctableCount) { |
| 109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); | 111 if (!this->handleRewind()) { |
|
emmaleer
2015/08/05 15:06:55
I think it's best to call handleRewind after the o
scroggo
2015/08/05 15:23:16
I'm not so sure about that assumption. For an SkMe
msarett
2015/08/05 15:29:42
I think you're right to start thinking about in wh
| |
| 110 if (rewindState == kCouldNotRewind_RewindState) { | |
| 111 return kCouldNotRewind; | 112 return kCouldNotRewind; |
| 112 } else if (rewindState == kRewound_RewindState) { | |
| 113 (void)read_header(this->stream(), NULL); | |
| 114 } | 113 } |
| 115 if (options.fSubset) { | 114 if (options.fSubset) { |
| 116 // Subsets are not supported. | 115 // Subsets are not supported. |
| 117 return kUnimplemented; | 116 return kUnimplemented; |
| 118 } | 117 } |
| 119 if (info.dimensions() != this->getInfo().dimensions()) { | 118 if (info.dimensions() != this->getInfo().dimensions()) { |
| 120 return kInvalidScale; | 119 return kInvalidScale; |
| 121 } | 120 } |
| 122 ExpandProc proc = NULL; | 121 |
| 123 switch (info.colorType()) { | 122 // Prepare a color table if necessary |
| 124 case kGray_8_SkColorType: | 123 setup_color_table(info.colorType(), ctable, ctableCount); |
| 125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; | 124 |
| 126 break; | 125 |
| 127 case kN32_SkColorType: | 126 // Initialize the swizzler |
| 128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; | 127 fSwizzler.reset(initializeSwizzler(info, ctable, options)); |
|
scroggo
2015/08/05 14:36:20
Ah, now I understand. The swizzler *is* used by th
msarett
2015/08/05 15:29:42
No worries. I'll take 99% spot on feedback :).
| |
| 129 break; | 128 if (NULL == fSwizzler.get()) { |
| 130 case kIndex_8_SkColorType: | 129 return kInvalidConversion; |
| 131 ctable[0] = BLACK; | |
| 132 ctable[1] = WHITE; | |
| 133 *ctableCount = 2; | |
| 134 proc = expand_bits_to_T<uint8_t, bit_to_bit>; | |
| 135 break; | |
| 136 case kRGB_565_SkColorType: | |
| 137 proc = expand_bits_to_T<uint16_t, bit_to_rgb565>; | |
| 138 break; | |
| 139 default: | |
| 140 return kInvalidConversion; | |
| 141 } | 130 } |
| 131 | |
| 132 // Perform the decode | |
| 142 SkISize size = info.dimensions(); | 133 SkISize size = info.dimensions(); |
| 143 uint8_t* dst = static_cast<uint8_t*>(pixels); | 134 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width()); |
| 144 size_t srcRowBytes = SkAlign8(size.width()) >> 3; | |
| 145 SkAutoTMalloc<uint8_t> src(srcRowBytes); | 135 SkAutoTMalloc<uint8_t> src(srcRowBytes); |
| 136 void* dstRow = dst; | |
| 146 for (int y = 0; y < size.height(); ++y) { | 137 for (int y = 0; y < size.height(); ++y) { |
| 147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { | 138 if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) { |
| 148 return kIncompleteInput; | 139 return SkCodec::kIncompleteInput; |
|
emmaleer
2015/08/05 15:06:55
Do you need SkCodec:: here? For the other return v
msarett
2015/08/05 15:29:42
Nope it's not needed, nice catch!
| |
| 149 } | 140 } |
| 150 proc(dst, src.get(), size.width()); | 141 fSwizzler->swizzle(dstRow, src.get()); |
| 151 dst += rowBytes; | 142 dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
| 152 } | 143 } |
| 153 return kSuccess; | 144 return kSuccess; |
| 154 } | 145 } |
| 155 | 146 |
| 156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { | 147 bool SkWbmpCodec::IsWbmp(SkStream* stream) { |
| 157 return read_header(stream, NULL); | 148 return read_header(stream, NULL); |
| 158 } | 149 } |
| 159 | 150 |
| 160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { | 151 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { |
| 161 SkAutoTDelete<SkStream> streamDeleter(stream); | 152 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 162 SkISize size; | 153 SkISize size; |
| 163 if (!read_header(stream, &size)) { | 154 if (!read_header(stream, &size)) { |
| 164 return NULL; | 155 return NULL; |
| 165 } | 156 } |
| 166 SkImageInfo info = | 157 SkImageInfo info = |
| 167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, | 158 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, |
| 168 kOpaque_SkAlphaType); | 159 kOpaque_SkAlphaType); |
| 169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); | 160 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); |
| 170 } | 161 } |
| 162 | |
| 163 class SkWbmpScanlineDecoder : public SkScanlineDecoder { | |
| 164 public: | |
| 165 /* | |
| 166 * Takes ownership of all pointer paramters. | |
| 167 */ | |
| 168 SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec) | |
|
scroggo
2015/08/05 14:36:20
It is weird that you call this dstInfo here, but s
msarett
2015/08/05 15:29:42
Yes I'll fix.
| |
| 169 : INHERITED(dstInfo) | |
| 170 , fCodec(codec) | |
| 171 , fColorTable(NULL) | |
| 172 , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width())) | |
|
scroggo
2015/08/05 14:36:20
FWIW, this->getInfo().width() is the same. (Maybe
msarett
2015/08/05 15:29:42
Yeah that is strange...I think it makes sense to u
| |
| 173 , fSrcBuffer(fSrcRowBytes) | |
| 174 {} | |
| 175 | |
| 176 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride { | |
| 177 void* dstRow = dst; | |
| 178 for (int y = 0; y < count; ++y) { | |
| 179 if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRo wBytes) { | |
|
scroggo
2015/08/05 14:36:20
Maybe this should be a function on SkWbmpCodec?
R
msarett
2015/08/05 15:29:42
Sounds good.
I renamed the parameter to src and a
| |
| 180 return SkCodec::kIncompleteInput; | |
| 181 } | |
| 182 fCodec->fSwizzler->swizzle(dstRow, fSrcBuffer.get()); | |
| 183 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
| 184 } | |
| 185 return SkCodec::kSuccess; | |
| 186 } | |
| 187 | |
| 188 SkCodec::Result onStart(const SkImageInfo& dstInfo, | |
| 189 const SkCodec::Options& options, SkPMColor inputColorTable[], | |
| 190 int* inputColorCount) { | |
| 191 if (!fCodec->handleRewind()) { | |
| 192 return SkCodec::kCouldNotRewind; | |
| 193 } | |
| 194 if (options.fSubset) { | |
| 195 // Subsets are not supported. | |
| 196 return SkCodec::kUnimplemented; | |
| 197 } | |
| 198 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | |
| 199 return SkCodec::kInvalidScale; | |
| 200 } | |
| 201 | |
| 202 // Fill in the color table | |
| 203 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount) ; | |
| 204 | |
| 205 // Copy the color table to a pointer that can be owned by the scanline d ecoder | |
| 206 if (kIndex_8_SkColorType == dstInfo.colorType()) { | |
| 207 fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2))); | |
| 208 } | |
| 209 | |
| 210 // Initialize the swizzler | |
| 211 fCodec->fSwizzler.reset(fCodec->initializeSwizzler(dstInfo, | |
| 212 get_color_ptr(fColorTable.get()), options)); | |
| 213 if (NULL == fCodec->fSwizzler.get()) { | |
| 214 return SkCodec::kInvalidInput; | |
| 215 } | |
| 216 | |
| 217 return SkCodec::kSuccess; | |
| 218 } | |
| 219 | |
| 220 private: | |
| 221 SkAutoTDelete<SkWbmpCodec> fCodec; | |
| 222 SkAutoTUnref<SkColorTable> fColorTable; | |
| 223 const size_t fSrcRowBytes; | |
| 224 SkAutoTMalloc<uint8_t> fSrcBuffer; | |
| 225 | |
| 226 typedef SkScanlineDecoder INHERITED; | |
| 227 }; | |
| 228 | |
| 229 SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) { | |
| 230 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>( | |
| 231 SkWbmpCodec::NewFromStream(stream))); | |
| 232 if (!codec) { | |
| 233 return NULL; | |
| 234 } | |
| 235 | |
| 236 const SkImageInfo& srcInfo = codec->getInfo(); | |
|
scroggo
2015/08/05 14:36:20
This seems weird - you pass getInfo to the constru
msarett
2015/08/05 15:29:42
Yes it is unnecessary, will fix.
| |
| 237 // Return the new scanline decoder | |
| 238 return SkNEW_ARGS(SkWbmpScanlineDecoder, (srcInfo, codec.detach())); | |
| 239 } | |
| OLD | NEW |