| 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 "SkBmpRLECodec.h" | 8 #include "SkBmpRLECodec.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkScaledCodec.h" |
| 11 #include "SkScanlineDecoder.h" | 12 #include "SkScanlineDecoder.h" |
| 12 #include "SkStream.h" | 13 #include "SkStream.h" |
| 13 | 14 |
| 14 /* | 15 /* |
| 15 * Creates an instance of the decoder | 16 * Creates an instance of the decoder |
| 16 * Called only by NewFromStream | 17 * Called only by NewFromStream |
| 17 */ | 18 */ |
| 18 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream, | 19 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream, |
| 19 uint16_t bitsPerPixel, uint32_t numColors, | 20 uint16_t bitsPerPixel, uint32_t numColors, |
| 20 uint32_t bytesPerColor, uint32_t offset, | 21 uint32_t bytesPerColor, uint32_t offset, |
| 21 SkBmpCodec::RowOrder rowOrder, size_t RLEBytes) | 22 SkScanlineDecoder::SkScanlineOrder rowOrder, |
| 23 size_t RLEBytes) |
| 22 : INHERITED(info, stream, bitsPerPixel, rowOrder) | 24 : INHERITED(info, stream, bitsPerPixel, rowOrder) |
| 23 , fColorTable(NULL) | 25 , fColorTable(NULL) |
| 24 , fNumColors(this->computeNumColors(numColors)) | 26 , fNumColors(this->computeNumColors(numColors)) |
| 25 , fBytesPerColor(bytesPerColor) | 27 , fBytesPerColor(bytesPerColor) |
| 26 , fOffset(offset) | 28 , fOffset(offset) |
| 27 , fStreamBuffer(SkNEW_ARRAY(uint8_t, RLEBytes)) | 29 , fStreamBuffer(SkNEW_ARRAY(uint8_t, RLEBytes)) |
| 28 , fRLEBytes(RLEBytes) | 30 , fRLEBytes(RLEBytes) |
| 29 , fCurrRLEByte(0) | 31 , fCurrRLEByte(0) |
| 32 , fSampleX(1) |
| 30 {} | 33 {} |
| 31 | 34 |
| 32 /* | 35 /* |
| 33 * Initiates the bitmap decode | 36 * Initiates the bitmap decode |
| 34 */ | 37 */ |
| 35 SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo, | 38 SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo, |
| 36 void* dst, size_t dstRowBytes, | 39 void* dst, size_t dstRowBytes, |
| 37 const Options& opts, | 40 const Options& opts, |
| 38 SkPMColor* inputColorPtr, | 41 SkPMColor* inputColorPtr, |
| 39 int* inputColorCount) { | 42 int* inputColorCount) { |
| 40 if (!this->rewindIfNeeded()) { | 43 if (!this->rewindIfNeeded()) { |
| 41 return kCouldNotRewind; | 44 return kCouldNotRewind; |
| 42 } | 45 } |
| 43 if (opts.fSubset) { | 46 if (opts.fSubset) { |
| 44 // Subsets are not supported. | 47 // Subsets are not supported. |
| 45 return kUnimplemented; | 48 return kUnimplemented; |
| 46 } | 49 } |
| 47 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | 50 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 48 SkCodecPrintf("Error: scaling not supported.\n"); | 51 SkCodecPrintf("Error: scaling not supported.\n"); |
| 49 return kInvalidScale; | 52 return kInvalidScale; |
| 50 } | 53 } |
| 51 if (!conversion_possible(dstInfo, this->getInfo())) { | 54 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 52 SkCodecPrintf("Error: cannot convert input type to output type.\n"); | 55 SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 53 return kInvalidConversion; | 56 return kInvalidConversion; |
| 54 } | 57 } |
| 55 | 58 |
| 56 // Create the color table if necessary and prepare the stream for decode | 59 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputCol
orCount); |
| 57 // Note that if it is non-NULL, inputColorCount will be modified | 60 if (kSuccess != result) { |
| 58 if (!this->createColorTable(inputColorCount)) { | 61 return result; |
| 59 SkCodecPrintf("Error: could not create color table.\n"); | |
| 60 return kInvalidInput; | |
| 61 } | |
| 62 | |
| 63 // Copy the color table to the client if necessary | |
| 64 copy_color_table(dstInfo, fColorTable, inputColorPtr, inputColorCount); | |
| 65 | |
| 66 // Initialize a swizzler if necessary | |
| 67 if (!this->initializeStreamBuffer()) { | |
| 68 SkCodecPrintf("Error: cannot initialize swizzler.\n"); | |
| 69 return kInvalidConversion; | |
| 70 } | 62 } |
| 71 | 63 |
| 72 // Perform the decode | 64 // Perform the decode |
| 73 return decode(dstInfo, dst, dstRowBytes, opts); | 65 return this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
| 74 } | 66 } |
| 75 | 67 |
| 76 /* | 68 /* |
| 77 * Process the color table for the bmp input | 69 * Process the color table for the bmp input |
| 78 */ | 70 */ |
| 79 bool SkBmpRLECodec::createColorTable(int* numColors) { | 71 bool SkBmpRLECodec::createColorTable(int* numColors) { |
| 80 // Allocate memory for color table | 72 // Allocate memory for color table |
| 81 uint32_t colorBytes = 0; | 73 uint32_t colorBytes = 0; |
| 82 SkPMColor colorTable[256]; | 74 SkPMColor colorTable[256]; |
| 83 if (this->bitsPerPixel() <= 8) { | 75 if (this->bitsPerPixel() <= 8) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 SkCodecPrintf("Error: unable to skip to image data.\n"); | 126 SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 135 return false; | 127 return false; |
| 136 } | 128 } |
| 137 | 129 |
| 138 // Return true on success | 130 // Return true on success |
| 139 return true; | 131 return true; |
| 140 } | 132 } |
| 141 | 133 |
| 142 bool SkBmpRLECodec::initializeStreamBuffer() { | 134 bool SkBmpRLECodec::initializeStreamBuffer() { |
| 143 // Setup a buffer to contain the full input stream | 135 // Setup a buffer to contain the full input stream |
| 136 // TODO (msarett): I'm not sure it is smart or optimal to trust fRLEBytes (r
ead from header) |
| 137 // as the size of our buffer. First of all, the decode fail
s if fRLEBytes is |
| 138 // corrupt (negative, zero, or small) when we might be able
to decode |
| 139 // successfully with a fixed size buffer. Additionally, we
would save memory |
| 140 // using a fixed size buffer if the RLE encoding is large.
On the other hand, |
| 141 // we may also waste memory with a fixed size buffer. And d
etermining a |
| 142 // minimum size for our buffer would depend on the image wid
th (so it's not |
| 143 // really "fixed" size), and we may end up allocating a buff
er that is |
| 144 // generally larger than the average encoded size anyway. |
| 144 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes); | 145 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes); |
| 145 if (totalBytes < fRLEBytes) { | 146 if (totalBytes < fRLEBytes) { |
| 146 fRLEBytes = totalBytes; | 147 fRLEBytes = totalBytes; |
| 147 SkCodecPrintf("Warning: incomplete RLE file.\n"); | 148 SkCodecPrintf("Warning: incomplete RLE file.\n"); |
| 148 } | 149 } |
| 149 if (fRLEBytes == 0) { | 150 if (fRLEBytes == 0) { |
| 150 SkCodecPrintf("Error: could not read RLE image data.\n"); | 151 SkCodecPrintf("Error: could not read RLE image data.\n"); |
| 151 return false; | 152 return false; |
| 152 } | 153 } |
| 153 return true; | 154 return true; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 fRLEBytes = remainingBytes + additionalBytes; | 188 fRLEBytes = remainingBytes + additionalBytes; |
| 188 return fRLEBytes; | 189 return fRLEBytes; |
| 189 } | 190 } |
| 190 | 191 |
| 191 /* | 192 /* |
| 192 * Set an RLE pixel using the color table | 193 * Set an RLE pixel using the color table |
| 193 */ | 194 */ |
| 194 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, | 195 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
| 195 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, | 196 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
| 196 uint8_t index) { | 197 uint8_t index) { |
| 197 // Set the row | 198 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
| 198 int height = dstInfo.height(); | 199 // Set the row |
| 199 int row; | 200 uint32_t row = this->getDstRow(y, dstInfo.height()); |
| 200 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { | |
| 201 row = height - y - 1; | |
| 202 } else { | |
| 203 row = y; | |
| 204 } | |
| 205 | 201 |
| 206 // Set the pixel based on destination color type | 202 // Set the pixel based on destination color type |
| 207 switch (dstInfo.colorType()) { | 203 switch (dstInfo.colorType()) { |
| 208 case kN32_SkColorType: { | 204 case kN32_SkColorType: { |
| 209 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 205 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
| 210 row * (int) dstRowBytes); | 206 row * (int) dstRowBytes); |
| 211 dstRow[x] = fColorTable->operator[](index); | 207 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 212 break; | 208 fColorTable->operator[](index); |
| 209 break; |
| 210 } |
| 211 case kRGB_565_SkColorType: { |
| 212 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo
wBytes); |
| 213 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 214 SkPixel32ToPixel16(fColorTable->operator[](index)); |
| 215 break; |
| 216 } |
| 217 default: |
| 218 // This case should not be reached. We should catch an invalid |
| 219 // color type when we check that the conversion is possible. |
| 220 SkASSERT(false); |
| 221 break; |
| 213 } | 222 } |
| 214 case kRGB_565_SkColorType: { | |
| 215 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowByt
es); | |
| 216 dstRow[x] = SkPixel32ToPixel16(fColorTable->operator[](index)); | |
| 217 break; | |
| 218 } | |
| 219 default: | |
| 220 // This case should not be reached. We should catch an invalid | |
| 221 // color type when we check that the conversion is possible. | |
| 222 SkASSERT(false); | |
| 223 break; | |
| 224 } | 223 } |
| 225 } | 224 } |
| 226 | 225 |
| 227 /* | 226 /* |
| 228 * Set an RLE pixel from R, G, B values | 227 * Set an RLE pixel from R, G, B values |
| 229 */ | 228 */ |
| 230 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, | 229 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, |
| 231 const SkImageInfo& dstInfo, uint32_t x, | 230 const SkImageInfo& dstInfo, uint32_t x, |
| 232 uint32_t y, uint8_t red, uint8_t green, | 231 uint32_t y, uint8_t red, uint8_t green, |
| 233 uint8_t blue) { | 232 uint8_t blue) { |
| 234 // Set the row | 233 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
| 235 int height = dstInfo.height(); | 234 // Set the row |
| 236 int row; | 235 uint32_t row = this->getDstRow(y, dstInfo.height()); |
| 237 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { | 236 |
| 238 row = height - y - 1; | 237 // Set the pixel based on destination color type |
| 239 } else { | 238 switch (dstInfo.colorType()) { |
| 240 row = y; | 239 case kN32_SkColorType: { |
| 240 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
| 241 row * (int) dstRowBytes); |
| 242 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 243 SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 244 break; |
| 245 } |
| 246 case kRGB_565_SkColorType: { |
| 247 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo
wBytes); |
| 248 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 249 SkPack888ToRGB16(red, green, blue); |
| 250 break; |
| 251 } |
| 252 default: |
| 253 // This case should not be reached. We should catch an invalid |
| 254 // color type when we check that the conversion is possible. |
| 255 SkASSERT(false); |
| 256 break; |
| 257 } |
| 258 } |
| 259 } |
| 260 |
| 261 SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 262 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { |
| 263 // Create the color table if necessary and prepare the stream for decode |
| 264 // Note that if it is non-NULL, inputColorCount will be modified |
| 265 if (!this->createColorTable(inputColorCount)) { |
| 266 SkCodecPrintf("Error: could not create color table.\n"); |
| 267 return SkCodec::kInvalidInput; |
| 241 } | 268 } |
| 242 | 269 |
| 243 // Set the pixel based on destination color type | 270 // Copy the color table to the client if necessary |
| 244 switch (dstInfo.colorType()) { | 271 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; |
| 245 case kN32_SkColorType: { | 272 |
| 246 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 273 // Initialize a buffer for encoded RLE data |
| 247 row * (int) dstRowBytes); | 274 if (!this->initializeStreamBuffer()) { |
| 248 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); | 275 SkCodecPrintf("Error: cannot initialize stream buffer.\n"); |
| 249 break; | 276 return SkCodec::kInvalidConversion; |
| 250 } | |
| 251 case kRGB_565_SkColorType: { | |
| 252 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowByt
es); | |
| 253 dstRow[x] = SkPack888ToRGB16(red, green, blue); | |
| 254 break; | |
| 255 } | |
| 256 default: | |
| 257 // This case should not be reached. We should catch an invalid | |
| 258 // color type when we check that the conversion is possible. | |
| 259 SkASSERT(false); | |
| 260 break; | |
| 261 } | 277 } |
| 278 |
| 279 fCurrRLEByte = 0; |
| 280 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &fSampleX, NULL); |
| 281 |
| 282 return SkCodec::kSuccess; |
| 262 } | 283 } |
| 263 | 284 |
| 264 /* | 285 /* |
| 265 * Performs the bitmap decoding for RLE input format | 286 * Performs the bitmap decoding for RLE input format |
| 266 * RLE decoding is performed all at once, rather than a one row at a time | 287 * RLE decoding is performed all at once, rather than a one row at a time |
| 267 */ | 288 */ |
| 268 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, | 289 SkCodec::Result SkBmpRLECodec::decodeRows(const SkImageInfo& dstInfo, |
| 269 void* dst, size_t dstRowBytes, | 290 void* dst, size_t dstRowBytes, |
| 270 const Options& opts) { | 291 const Options& opts) { |
| 271 // Set RLE flags | 292 // Set RLE flags |
| 272 static const uint8_t RLE_ESCAPE = 0; | 293 static const uint8_t RLE_ESCAPE = 0; |
| 273 static const uint8_t RLE_EOL = 0; | 294 static const uint8_t RLE_EOL = 0; |
| 274 static const uint8_t RLE_EOF = 1; | 295 static const uint8_t RLE_EOF = 1; |
| 275 static const uint8_t RLE_DELTA = 2; | 296 static const uint8_t RLE_DELTA = 2; |
| 276 | 297 |
| 277 // Set constant values | 298 // Set constant values |
| 278 const int width = dstInfo.width(); | 299 const int width = this->getInfo().width(); |
| 279 const int height = dstInfo.height(); | 300 const int height = dstInfo.height(); |
| 280 | 301 |
| 281 // Destination parameters | 302 // Destination parameters |
| 282 int x = 0; | 303 int x = 0; |
| 283 int y = 0; | 304 int y = 0; |
| 284 | 305 |
| 285 // Set the background as transparent. Then, if the RLE code skips pixels, | 306 // Set the background as transparent. Then, if the RLE code skips pixels, |
| 286 // the skipped pixels will be transparent. | 307 // the skipped pixels will be transparent. |
| 287 // Because of the need for transparent pixels, kN32 is the only color | 308 // Because of the need for transparent pixels, kN32 is the only color |
| 288 // type that makes sense for the destination format. | 309 // type that makes sense for the destination format. |
| 289 SkASSERT(kN32_SkColorType == dstInfo.colorType()); | 310 SkASSERT(kN32_SkColorType == dstInfo.colorType()); |
| 290 if (kNo_ZeroInitialized == opts.fZeroInitialized) { | 311 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, |
| 291 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT,
NULL); | 312 NULL, opts.fZeroInitialized); |
| 292 } | |
| 293 | 313 |
| 294 while (true) { | 314 while (true) { |
| 295 // If we have reached a row that is beyond the requested height, we have | 315 // If we have reached a row that is beyond the requested height, we have |
| 296 // succeeded. | 316 // succeeded. |
| 297 if (y >= height) { | 317 if (y >= height) { |
| 298 // It would be better to check for the EOF marker before returning | 318 // It would be better to check for the EOF marker before returning |
| 299 // success, but we may be performing a scanline decode, which | 319 // success, but we may be performing a scanline decode, which |
| 300 // may require us to stop before decoding the full height. | 320 // may require us to stop before decoding the full height. |
| 301 return kSuccess; | 321 return kSuccess; |
| 302 } | 322 } |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 // Set the indicated number of pixels | 469 // Set the indicated number of pixels |
| 450 for (int which = 0; x < endX; x++) { | 470 for (int which = 0; x < endX; x++) { |
| 451 setPixel(dst, dstRowBytes, dstInfo, x, y, | 471 setPixel(dst, dstRowBytes, dstInfo, x, y, |
| 452 indices[which]); | 472 indices[which]); |
| 453 which = !which; | 473 which = !which; |
| 454 } | 474 } |
| 455 } | 475 } |
| 456 } | 476 } |
| 457 } | 477 } |
| 458 } | 478 } |
| OLD | NEW |