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 "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkColorTable.h" | |
| 10 #include "SkStream.h" | 11 #include "SkStream.h" |
| 11 #include "SkCodec_wbmp.h" | 12 #include "SkCodec_wbmp.h" |
| 12 | 13 |
| 14 // Each bit represents a pixel, so rowBytes can be calculated as width / 8 | |
|
scroggo
2015/07/28 14:03:34
Almost - the code handles SkAlign8, but the commen
msarett
2015/07/28 22:22:07
Agreed that the comment should acknowledge SkAlign
| |
| 15 static size_t get_src_row_bytes(int width) { | |
| 16 return SkAlign8(width >> 3); | |
| 17 } | |
| 18 | |
| 13 // http://en.wikipedia.org/wiki/Variable-length_quantity | 19 // http://en.wikipedia.org/wiki/Variable-length_quantity |
| 14 static bool read_mbf(SkStream* stream, uint64_t* value) { | 20 static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 15 uint64_t n = 0; | 21 uint64_t n = 0; |
| 16 uint8_t data; | 22 uint8_t data; |
| 17 const uint64_t kLimit = 0xFE00000000000000; | 23 const uint64_t kLimit = 0xFE00000000000000; |
| 18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); | 24 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); |
| 19 do { | 25 do { |
| 20 if (n & kLimit) { // Will overflow on shift by 7. | 26 if (n & kLimit) { // Will overflow on shift by 7. |
| 21 return false; | 27 return false; |
| 22 } | 28 } |
| 23 if (stream->read(&data, 1) != 1) { | 29 if (stream->read(&data, 1) != 1) { |
| 24 return false; | 30 return false; |
| 25 } | 31 } |
| 26 n = (n << 7) | (data & 0x7F); | 32 n = (n << 7) | (data & 0x7F); |
| 27 } while (data & 0x80); | 33 } while (data & 0x80); |
| 28 *value = n; | 34 *value = n; |
| 29 return true; | 35 return true; |
| 30 } | 36 } |
| 31 | 37 |
| 32 static bool read_header(SkStream* stream, SkISize* size) { | 38 static bool read_header(SkStream* stream, SkISize* size) { |
| 33 uint64_t width, height; | 39 uint64_t width, height; |
| 34 uint16_t data; | 40 uint16_t data; |
| 35 if (stream->read(&data, 2) != 2 || data != 0) { | 41 if (stream->read(&data, 2) != 2 || data != 0) { |
| 36 return false; | 42 return false; |
| 37 } | 43 } |
| 38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { | 44 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| 39 return false; | 45 return false; |
| 40 } | 46 } |
| 41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { | 47 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
| 42 return false; | 48 return false; |
| 43 } | 49 } |
| 44 if (size) { | 50 if (size) { |
| 45 *size = SkISize::Make(SkToS32(width), SkToS32(height)); | 51 *size = SkISize::Make(SkToS32(width), SkToS32(height)); |
| 46 } | 52 } |
| 47 return true; | 53 return true; |
| 48 } | 54 } |
| 49 | 55 |
| 50 #define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) | 56 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, |
| 51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) | 57 const SkPMColor* ctable, const Options& opts) { |
| 58 // TODO (msarett): Reenable support for 565 if it is desired | |
| 59 // skbug.com/3683 | |
| 52 | 60 |
| 53 #define GRAYSCALE_BLACK 0 | 61 // Create the swizzler based on the desired color type |
| 54 #define GRAYSCALE_WHITE 0xFF | 62 switch (info.colorType()) { |
| 55 | 63 case kIndex_8_SkColorType: |
| 56 #define RGB565_BLACK 0 | 64 case kN32_SkColorType: |
| 57 #define RGB565_WHITE 0xFFFF | 65 case kGray_8_SkColorType: |
| 58 | 66 return SkSwizzler::CreateSwizzler( |
| 59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } | 67 SkSwizzler::kIndex1, ctable, info, opts.fZeroInitialized); |
| 60 | 68 default: |
| 61 static uint8_t bit_to_bit(U8CPU bit) { return bit; } | 69 return NULL; |
| 62 | |
| 63 static uint8_t bit_to_grayscale(U8CPU bit) { | |
| 64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; | |
| 65 } | |
| 66 | |
| 67 static uint16_t bit_to_rgb565(U8CPU bit) { | |
| 68 return bit ? RGB565_WHITE : RGB565_BLACK; | |
| 69 } | |
| 70 | |
| 71 typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int); | |
| 72 | |
| 73 // TODO(halcanary): Add this functionality (grayscale and indexed output) to | |
| 74 // SkSwizzler and use it here. | |
| 75 template <typename T, T (*TRANSFORM)(U8CPU)> | |
| 76 static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) { | |
| 77 T* dst = reinterpret_cast<T*>(dstptr); | |
| 78 int bytes = bits >> 3; | |
| 79 for (int i = 0; i < bytes; i++) { | |
| 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 } | 70 } |
| 94 } | 71 } |
| 95 | 72 |
| 96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) | 73 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) |
| 97 : INHERITED(info, stream) {} | 74 : INHERITED(info, stream) {} |
| 98 | 75 |
| 99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { | 76 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 100 return kWBMP_SkEncodedFormat; | 77 return kWBMP_SkEncodedFormat; |
| 101 } | 78 } |
| 102 | 79 |
| 103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, | 80 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
| 104 void* pixels, | 81 void* dst, |
| 105 size_t rowBytes, | 82 size_t rowBytes, |
| 106 const Options& options, | 83 const Options& options, |
| 107 SkPMColor ctable[], | 84 SkPMColor ctable[], |
| 108 int* ctableCount) { | 85 int* ctableCount) { |
| 109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); | 86 SkCodec::RewindState rewindState = this->rewindIfNeeded(); |
| 110 if (rewindState == kCouldNotRewind_RewindState) { | 87 if (rewindState == kCouldNotRewind_RewindState) { |
| 111 return kCouldNotRewind; | 88 return kCouldNotRewind; |
| 112 } else if (rewindState == kRewound_RewindState) { | 89 } else if (rewindState == kRewound_RewindState) { |
| 113 (void)read_header(this->stream(), NULL); | 90 (void)read_header(this->stream(), NULL); |
| 114 } | 91 } |
| 115 if (options.fSubset) { | 92 if (options.fSubset) { |
| 116 // Subsets are not supported. | 93 // Subsets are not supported. |
| 117 return kUnimplemented; | 94 return kUnimplemented; |
| 118 } | 95 } |
| 119 if (info.dimensions() != this->getInfo().dimensions()) { | 96 if (info.dimensions() != this->getInfo().dimensions()) { |
| 120 return kInvalidScale; | 97 return kInvalidScale; |
| 121 } | 98 } |
| 122 ExpandProc proc = NULL; | 99 |
| 123 switch (info.colorType()) { | 100 // Prepare a color table |
| 124 case kGray_8_SkColorType: | 101 SkPMColor alternateCtable[2]; |
|
scroggo
2015/07/28 14:03:34
I believe in Skia we would typically name this som
msarett
2015/07/28 22:22:07
I agree, I prefer colorStorage :). Having said th
| |
| 125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; | 102 SkPMColor* colorPtr; |
| 126 break; | 103 if (kIndex_8_SkColorType == info.colorType()) { |
| 127 case kN32_SkColorType: | 104 ctable[0] = SK_ColorBLACK; |
|
scroggo
2015/07/28 14:03:34
How do you feel about the following:
if (kIndex_8
msarett
2015/07/28 22:22:06
Agreed, however, the new version only uses the cta
| |
| 128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; | 105 ctable[1] = SK_ColorWHITE; |
| 129 break; | 106 colorPtr = ctable; |
| 130 case kIndex_8_SkColorType: | 107 *ctableCount = 2; |
| 131 ctable[0] = BLACK; | 108 } else { |
| 132 ctable[1] = WHITE; | 109 alternateCtable[0] = SK_ColorBLACK; |
| 133 *ctableCount = 2; | 110 alternateCtable[1] = SK_ColorWHITE; |
| 134 proc = expand_bits_to_T<uint8_t, bit_to_bit>; | 111 colorPtr = alternateCtable; |
| 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 } | 112 } |
| 113 | |
| 114 // Initialize the swizzler | |
| 115 SkAutoTDelete<SkSwizzler> swizzler( | |
| 116 initializeSwizzler(info, colorPtr, options)); | |
| 117 if (NULL == swizzler.get()) { | |
| 118 return kInvalidConversion; | |
| 119 } | |
| 120 | |
| 121 // Perform the decode | |
| 142 SkISize size = info.dimensions(); | 122 SkISize size = info.dimensions(); |
| 143 uint8_t* dst = static_cast<uint8_t*>(pixels); | 123 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width()); |
| 144 size_t srcRowBytes = SkAlign8(size.width()) >> 3; | |
| 145 SkAutoTMalloc<uint8_t> src(srcRowBytes); | 124 SkAutoTMalloc<uint8_t> src(srcRowBytes); |
| 125 void* dstRow = dst; | |
| 146 for (int y = 0; y < size.height(); ++y) { | 126 for (int y = 0; y < size.height(); ++y) { |
| 147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { | 127 if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) { |
| 148 return kIncompleteInput; | 128 return SkCodec::kIncompleteInput; |
| 149 } | 129 } |
| 150 proc(dst, src.get(), size.width()); | 130 swizzler->swizzle(dstRow, src.get()); |
| 151 dst += rowBytes; | 131 dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
| 152 } | 132 } |
| 153 return kSuccess; | 133 return SkCodec::kSuccess; |
|
scroggo
2015/07/28 14:03:34
I do not feel strongly about whether to add SkCode
msarett
2015/07/28 22:22:06
Yep it's not needed. Removing it.
| |
| 154 } | 134 } |
| 155 | 135 |
| 156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { | 136 bool SkWbmpCodec::IsWbmp(SkStream* stream) { |
| 157 return read_header(stream, NULL); | 137 return read_header(stream, NULL); |
| 158 } | 138 } |
| 159 | 139 |
| 160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { | 140 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { |
| 161 SkAutoTDelete<SkStream> streamDeleter(stream); | 141 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 162 SkISize size; | 142 SkISize size; |
| 163 if (!read_header(stream, &size)) { | 143 if (!read_header(stream, &size)) { |
| 164 return NULL; | 144 return NULL; |
| 165 } | 145 } |
| 166 SkImageInfo info = | 146 SkImageInfo info = |
| 167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, | 147 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, |
| 168 kOpaque_SkAlphaType); | 148 kOpaque_SkAlphaType); |
| 169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); | 149 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); |
| 170 } | 150 } |
| 151 | |
| 152 class SkWbmpScanlineDecoder : public SkScanlineDecoder { | |
| 153 public: | |
| 154 SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec, | |
|
scroggo
2015/07/28 14:03:34
/**
* Takes ownership of all its pointer paramete
msarett
2015/07/28 22:22:07
Done.
| |
| 155 SkColorTable* colorTable, SkSwizzler* swizzler) | |
| 156 : INHERITED(dstInfo) | |
| 157 , fCodec(codec) | |
| 158 , fColorTable(colorTable) | |
| 159 , fSwizzler(swizzler) | |
| 160 , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width())) | |
| 161 , fSrcBuffer(fSrcRowBytes) | |
| 162 {} | |
| 163 | |
| 164 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride { | |
| 165 void* dstRow = dst; | |
| 166 for (int y = 0; y < count; ++y) { | |
| 167 if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRo wBytes) { | |
| 168 return SkCodec::kIncompleteInput; | |
| 169 } | |
| 170 this->fSwizzler->swizzle(dstRow, fSrcBuffer.get()); | |
|
scroggo
2015/07/28 14:03:34
We use this->[methodName] so it is easy to disting
msarett
2015/07/28 22:22:06
Gotcha.
| |
| 171 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
| 172 } | |
| 173 return SkCodec::kSuccess; | |
| 174 } | |
| 175 | |
| 176 private: | |
| 177 SkAutoTDelete<SkWbmpCodec> fCodec; | |
| 178 SkAutoTDelete<SkColorTable> fColorTable; | |
| 179 SkAutoTDelete<SkSwizzler> fSwizzler; | |
| 180 const size_t fSrcRowBytes; | |
| 181 SkAutoTMalloc<uint8_t> fSrcBuffer; | |
| 182 | |
| 183 typedef SkScanlineDecoder INHERITED; | |
| 184 }; | |
| 185 | |
| 186 | |
| 187 SkScanlineDecoder* SkWbmpCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | |
| 188 const Options& options, SkPMColor ctable[], int* ctableCount) { | |
| 189 if (options.fSubset) { | |
| 190 // Subsets are not supported. | |
| 191 return NULL; | |
| 192 } | |
| 193 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | |
| 194 return NULL; | |
| 195 } | |
| 196 // Create a new SkWbmpCodec, to be owned by the scanline decoder. | |
| 197 SkStream* stream = this->stream()->duplicate(); | |
| 198 if (!stream) { | |
| 199 return NULL; | |
| 200 } | |
| 201 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>( | |
| 202 SkWbmpCodec::NewFromStream(stream))); | |
| 203 if (!codec) { | |
| 204 return NULL; | |
| 205 } | |
| 206 | |
| 207 // Prepare a color table | |
|
scroggo
2015/07/28 14:03:34
Can you move this into a helper function? (I think
msarett
2015/07/28 22:22:07
Yes done!
| |
| 208 SkPMColor alternateCtable[2]; | |
| 209 SkPMColor* colorPtr; | |
| 210 if (kIndex_8_SkColorType == dstInfo.colorType()) { | |
| 211 ctable[0] = SK_ColorBLACK; | |
| 212 ctable[1] = SK_ColorWHITE; | |
| 213 colorPtr = ctable; | |
| 214 *ctableCount = 2; | |
| 215 } else { | |
| 216 alternateCtable[0] = SK_ColorBLACK; | |
| 217 alternateCtable[1] = SK_ColorWHITE; | |
| 218 colorPtr = alternateCtable; | |
| 219 } | |
| 220 SkAutoTDelete<SkColorTable> colorTable(SkNEW_ARGS(SkColorTable, (colorPtr, 2 ))); | |
|
scroggo
2015/07/28 14:03:34
SkColorTable is refcounted, so it should be in an
msarett
2015/07/28 22:22:07
Acknowledged. I'll try not to forget that next ti
| |
| 221 | |
| 222 // Initialize the swizzler | |
| 223 SkAutoTDelete<SkSwizzler> swizzler( | |
| 224 initializeSwizzler(dstInfo, colorTable->readColors(), options)); | |
| 225 if (NULL == swizzler.get()) { | |
| 226 return NULL; | |
| 227 } | |
| 228 | |
| 229 return SkNEW_ARGS(SkWbmpScanlineDecoder, (dstInfo, codec.detach(), | |
| 230 colorTable.detach(), swizzler.detach())); | |
| 231 } | |
| OLD | NEW |