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