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