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); |
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 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
198 int height = dstInfo.height(); | 189 // Set the row |
199 int row; | 190 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 | 191 |
206 // Set the pixel based on destination color type | 192 // Set the pixel based on destination color type |
207 switch (dstInfo.colorType()) { | 193 switch (dstInfo.colorType()) { |
208 case kN32_SkColorType: { | 194 case kN32_SkColorType: { |
209 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 195 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
210 row * (int) dstRowBytes); | 196 row * (int) dstRowBytes); |
211 dstRow[x] = fColorTable->operator[](index); | 197 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
212 break; | 198 fColorTable->operator[](index); |
| 199 break; |
| 200 } |
| 201 case kRGB_565_SkColorType: { |
| 202 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo
wBytes); |
| 203 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 204 SkPixel32ToPixel16(fColorTable->operator[](index)); |
| 205 break; |
| 206 } |
| 207 default: |
| 208 // This case should not be reached. We should catch an invalid |
| 209 // color type when we check that the conversion is possible. |
| 210 SkASSERT(false); |
| 211 break; |
213 } | 212 } |
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 } | 213 } |
225 } | 214 } |
226 | 215 |
227 /* | 216 /* |
228 * Set an RLE pixel from R, G, B values | 217 * Set an RLE pixel from R, G, B values |
229 */ | 218 */ |
230 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, | 219 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, |
231 const SkImageInfo& dstInfo, uint32_t x, | 220 const SkImageInfo& dstInfo, uint32_t x, |
232 uint32_t y, uint8_t red, uint8_t green, | 221 uint32_t y, uint8_t red, uint8_t green, |
233 uint8_t blue) { | 222 uint8_t blue) { |
234 // Set the row | 223 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
235 int height = dstInfo.height(); | 224 // Set the row |
236 int row; | 225 uint32_t row = this->getDstRow(y, dstInfo.height()); |
237 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { | 226 |
238 row = height - y - 1; | 227 // Set the pixel based on destination color type |
239 } else { | 228 switch (dstInfo.colorType()) { |
240 row = y; | 229 case kN32_SkColorType: { |
| 230 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
| 231 row * (int) dstRowBytes); |
| 232 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 233 SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 234 break; |
| 235 } |
| 236 case kRGB_565_SkColorType: { |
| 237 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo
wBytes); |
| 238 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
| 239 SkPack888ToRGB16(red, green, blue); |
| 240 break; |
| 241 } |
| 242 default: |
| 243 // This case should not be reached. We should catch an invalid |
| 244 // color type when we check that the conversion is possible. |
| 245 SkASSERT(false); |
| 246 break; |
| 247 } |
| 248 } |
| 249 } |
| 250 |
| 251 SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 252 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { |
| 253 // Create the color table if necessary and prepare the stream for decode |
| 254 // Note that if it is non-NULL, inputColorCount will be modified |
| 255 if (!this->createColorTable(inputColorCount)) { |
| 256 SkCodecPrintf("Error: could not create color table.\n"); |
| 257 return SkCodec::kInvalidInput; |
241 } | 258 } |
242 | 259 |
243 // Set the pixel based on destination color type | 260 // Copy the color table to the client if necessary |
244 switch (dstInfo.colorType()) { | 261 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; |
245 case kN32_SkColorType: { | 262 |
246 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 263 // Initialize a buffer for encoded RLE data |
247 row * (int) dstRowBytes); | 264 if (!this->initializeStreamBuffer()) { |
248 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); | 265 SkCodecPrintf("Error: cannot initialize stream buffer.\n"); |
249 break; | 266 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 } | 267 } |
| 268 |
| 269 fCurrRLEByte = 0; |
| 270 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &fSampleX, NULL); |
| 271 |
| 272 return SkCodec::kSuccess; |
262 } | 273 } |
263 | 274 |
264 /* | 275 /* |
265 * Performs the bitmap decoding for RLE input format | 276 * Performs the bitmap decoding for RLE input format |
266 * RLE decoding is performed all at once, rather than a one row at a time | 277 * RLE decoding is performed all at once, rather than a one row at a time |
267 */ | 278 */ |
268 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, | 279 SkCodec::Result SkBmpRLECodec::decodeRows(const SkImageInfo& dstInfo, |
269 void* dst, size_t dstRowBytes, | 280 void* dst, size_t dstRowBytes, |
270 const Options& opts) { | 281 const Options& opts) { |
271 // Set RLE flags | 282 // Set RLE flags |
272 static const uint8_t RLE_ESCAPE = 0; | 283 static const uint8_t RLE_ESCAPE = 0; |
273 static const uint8_t RLE_EOL = 0; | 284 static const uint8_t RLE_EOL = 0; |
274 static const uint8_t RLE_EOF = 1; | 285 static const uint8_t RLE_EOF = 1; |
275 static const uint8_t RLE_DELTA = 2; | 286 static const uint8_t RLE_DELTA = 2; |
276 | 287 |
277 // Set constant values | 288 // Set constant values |
278 const int width = dstInfo.width(); | 289 const int width = this->getInfo().width(); |
279 const int height = dstInfo.height(); | 290 const int height = dstInfo.height(); |
280 | 291 |
281 // Destination parameters | 292 // Destination parameters |
282 int x = 0; | 293 int x = 0; |
283 int y = 0; | 294 int y = 0; |
284 | 295 |
285 // Set the background as transparent. Then, if the RLE code skips pixels, | 296 // Set the background as transparent. Then, if the RLE code skips pixels, |
286 // the skipped pixels will be transparent. | 297 // the skipped pixels will be transparent. |
287 // Because of the need for transparent pixels, kN32 is the only color | 298 // Because of the need for transparent pixels, kN32 is the only color |
288 // type that makes sense for the destination format. | 299 // type that makes sense for the destination format. |
289 SkASSERT(kN32_SkColorType == dstInfo.colorType()); | 300 SkASSERT(kN32_SkColorType == dstInfo.colorType()); |
290 if (kNo_ZeroInitialized == opts.fZeroInitialized) { | 301 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, |
291 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT,
NULL); | 302 NULL, opts.fZeroInitialized); |
292 } | |
293 | 303 |
294 while (true) { | 304 while (true) { |
295 // If we have reached a row that is beyond the requested height, we have | 305 // If we have reached a row that is beyond the requested height, we have |
296 // succeeded. | 306 // succeeded. |
297 if (y >= height) { | 307 if (y >= height) { |
298 // It would be better to check for the EOF marker before returning | 308 // It would be better to check for the EOF marker before returning |
299 // success, but we may be performing a scanline decode, which | 309 // success, but we may be performing a scanline decode, which |
300 // may require us to stop before decoding the full height. | 310 // may require us to stop before decoding the full height. |
301 return kSuccess; | 311 return kSuccess; |
302 } | 312 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 // Set the indicated number of pixels | 459 // Set the indicated number of pixels |
450 for (int which = 0; x < endX; x++) { | 460 for (int which = 0; x < endX; x++) { |
451 setPixel(dst, dstRowBytes, dstInfo, x, y, | 461 setPixel(dst, dstRowBytes, dstInfo, x, y, |
452 indices[which]); | 462 indices[which]); |
453 which = !which; | 463 which = !which; |
454 } | 464 } |
455 } | 465 } |
456 } | 466 } |
457 } | 467 } |
458 } | 468 } |
OLD | NEW |