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 this->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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 SkCodecPrintf("Error: unable to skip to image data.\n"); | 125 SkCodecPrintf("Error: unable to skip to image data.\n"); |
135 return false; | 126 return false; |
136 } | 127 } |
137 | 128 |
138 // Return true on success | 129 // Return true on success |
139 return true; | 130 return true; |
140 } | 131 } |
141 | 132 |
142 bool SkBmpRLECodec::initializeStreamBuffer() { | 133 bool SkBmpRLECodec::initializeStreamBuffer() { |
143 // Setup a buffer to contain the full input stream | 134 // Setup a buffer to contain the full input stream |
135 // TODO (msarett): I'm not sure it is smart or optimal to trust fRLEBytes (r ead from header) | |
scroggo
2015/08/27 20:14:23
FWIW, we could use a constant buffer size, like we
msarett
2015/08/27 21:21:46
Yeah I think both of those are good candidates.
| |
136 // as the size of our buffer. First of all, the decode fail s if fRLEBytes is | |
137 // corrupt (negative, zero, or small) when we might be able to decode | |
138 // successfully with a fixed size buffer. Additionally, we would save memory | |
139 // using a fixed size buffer if the RLE encoding is large. On the other hand, | |
140 // we may also waste memory with a fixed size buffer. And d etermining a | |
141 // minimum size for our buffer would depend on the image wid th (so it's not | |
142 // really "fixed" size), and we may end up allocating a buff er that is | |
143 // generally larger than the average encoded size anyway. | |
144 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes); | 144 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes); |
145 if (totalBytes < fRLEBytes) { | 145 if (totalBytes < fRLEBytes) { |
146 fRLEBytes = totalBytes; | 146 fRLEBytes = totalBytes; |
147 SkCodecPrintf("Warning: incomplete RLE file.\n"); | 147 SkCodecPrintf("Warning: incomplete RLE file.\n"); |
148 } | 148 } |
149 if (fRLEBytes == 0) { | 149 if (fRLEBytes == 0) { |
150 SkCodecPrintf("Error: could not read RLE image data.\n"); | 150 SkCodecPrintf("Error: could not read RLE image data.\n"); |
151 return false; | 151 return false; |
152 } | 152 } |
153 return true; | 153 return true; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 fRLEBytes = remainingBytes + additionalBytes; | 187 fRLEBytes = remainingBytes + additionalBytes; |
188 return fRLEBytes; | 188 return fRLEBytes; |
189 } | 189 } |
190 | 190 |
191 /* | 191 /* |
192 * Set an RLE pixel using the color table | 192 * Set an RLE pixel using the color table |
193 */ | 193 */ |
194 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, | 194 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
195 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, | 195 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
196 uint8_t index) { | 196 uint8_t index) { |
197 // Set the row | 197 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
198 int height = dstInfo.height(); | 198 // Set the row |
199 int row; | 199 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 | 200 |
206 // Set the pixel based on destination color type | 201 // Set the pixel based on destination color type |
207 switch (dstInfo.colorType()) { | 202 switch (dstInfo.colorType()) { |
208 case kN32_SkColorType: { | 203 case kN32_SkColorType: { |
209 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 204 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
210 row * (int) dstRowBytes); | 205 row * (int) dstRowBytes); |
211 dstRow[x] = fColorTable->operator[](index); | 206 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = |
212 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; | |
213 } | 221 } |
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 } | 222 } |
225 } | 223 } |
226 | 224 |
227 /* | 225 /* |
228 * Set an RLE pixel from R, G, B values | 226 * Set an RLE pixel from R, G, B values |
229 */ | 227 */ |
230 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, | 228 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, |
231 const SkImageInfo& dstInfo, uint32_t x, | 229 const SkImageInfo& dstInfo, uint32_t x, |
232 uint32_t y, uint8_t red, uint8_t green, | 230 uint32_t y, uint8_t red, uint8_t green, |
233 uint8_t blue) { | 231 uint8_t blue) { |
234 // Set the row | 232 if (SkScaledCodec::IsCoordNecessary(x, fSampleX, dstInfo.width())) { |
235 int height = dstInfo.height(); | 233 // Set the row |
236 int row; | 234 uint32_t row = this->getDstRow(y, dstInfo.height()); |
237 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { | 235 |
238 row = height - y - 1; | 236 // Set the pixel based on destination color type |
239 } else { | 237 switch (dstInfo.colorType()) { |
240 row = y; | 238 case kN32_SkColorType: { |
239 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | |
240 row * (int) dstRowBytes); | |
241 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = | |
242 SkPackARGB32NoCheck(0xFF, red, green, blue); | |
243 break; | |
244 } | |
245 case kRGB_565_SkColorType: { | |
246 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo wBytes); | |
247 dstRow[SkScaledCodec::GetDstCoord(x, fSampleX)] = | |
248 SkPack888ToRGB16(red, green, blue); | |
249 break; | |
250 } | |
251 default: | |
252 // This case should not be reached. We should catch an invalid | |
253 // color type when we check that the conversion is possible. | |
254 SkASSERT(false); | |
255 break; | |
256 } | |
257 } | |
258 } | |
259 | |
260 SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo, | |
261 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo lorCount) { | |
262 // Create the color table if necessary and prepare the stream for decode | |
263 // Note that if it is non-NULL, inputColorCount will be modified | |
264 if (!this->createColorTable(inputColorCount)) { | |
265 SkCodecPrintf("Error: could not create color table.\n"); | |
266 return SkCodec::kInvalidInput; | |
241 } | 267 } |
242 | 268 |
243 // Set the pixel based on destination color type | 269 // Copy the color table to the client if necessary |
244 switch (dstInfo.colorType()) { | 270 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount) ; |
245 case kN32_SkColorType: { | 271 |
246 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, | 272 // Initialize a buffer for encoded RLE data |
247 row * (int) dstRowBytes); | 273 if (!this->initializeStreamBuffer()) { |
248 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); | 274 SkCodecPrintf("Error: cannot initialize stream buffer.\n"); |
249 break; | 275 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 } | 276 } |
277 | |
278 fCurrRLEByte = 0; | |
279 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &fSampleX, NULL); | |
280 | |
281 return SkCodec::kSuccess; | |
262 } | 282 } |
263 | 283 |
264 /* | 284 /* |
265 * Performs the bitmap decoding for RLE input format | 285 * Performs the bitmap decoding for RLE input format |
266 * RLE decoding is performed all at once, rather than a one row at a time | 286 * RLE decoding is performed all at once, rather than a one row at a time |
267 */ | 287 */ |
268 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, | 288 SkCodec::Result SkBmpRLECodec::decodeRows(const SkImageInfo& dstInfo, |
269 void* dst, size_t dstRowBytes, | 289 void* dst, size_t dstRowBytes, |
270 const Options& opts) { | 290 const Options& opts) { |
271 // Set RLE flags | 291 // Set RLE flags |
272 static const uint8_t RLE_ESCAPE = 0; | 292 static const uint8_t RLE_ESCAPE = 0; |
273 static const uint8_t RLE_EOL = 0; | 293 static const uint8_t RLE_EOL = 0; |
274 static const uint8_t RLE_EOF = 1; | 294 static const uint8_t RLE_EOF = 1; |
275 static const uint8_t RLE_DELTA = 2; | 295 static const uint8_t RLE_DELTA = 2; |
276 | 296 |
277 // Set constant values | 297 // Set constant values |
278 const int width = dstInfo.width(); | 298 const int width = this->getInfo().width(); |
279 const int height = dstInfo.height(); | 299 const int height = dstInfo.height(); |
280 | 300 |
281 // Destination parameters | 301 // Destination parameters |
282 int x = 0; | 302 int x = 0; |
283 int y = 0; | 303 int y = 0; |
284 | 304 |
285 // Set the background as transparent. Then, if the RLE code skips pixels, | 305 // Set the background as transparent. Then, if the RLE code skips pixels, |
286 // the skipped pixels will be transparent. | 306 // the skipped pixels will be transparent. |
287 // Because of the need for transparent pixels, kN32 is the only color | 307 // Because of the need for transparent pixels, kN32 is the only color |
288 // type that makes sense for the destination format. | 308 // type that makes sense for the destination format. |
289 SkASSERT(kN32_SkColorType == dstInfo.colorType()); | 309 SkASSERT(kN32_SkColorType == dstInfo.colorType()); |
290 if (kNo_ZeroInitialized == opts.fZeroInitialized) { | 310 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, |
291 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, NULL); | 311 NULL, opts.fZeroInitialized); |
292 } | |
293 | 312 |
294 while (true) { | 313 while (true) { |
295 // If we have reached a row that is beyond the requested height, we have | 314 // If we have reached a row that is beyond the requested height, we have |
296 // succeeded. | 315 // succeeded. |
297 if (y >= height) { | 316 if (y >= height) { |
298 // It would be better to check for the EOF marker before returning | 317 // It would be better to check for the EOF marker before returning |
299 // success, but we may be performing a scanline decode, which | 318 // success, but we may be performing a scanline decode, which |
300 // may require us to stop before decoding the full height. | 319 // may require us to stop before decoding the full height. |
301 return kSuccess; | 320 return kSuccess; |
302 } | 321 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 // Set the indicated number of pixels | 468 // Set the indicated number of pixels |
450 for (int which = 0; x < endX; x++) { | 469 for (int which = 0; x < endX; x++) { |
451 setPixel(dst, dstRowBytes, dstInfo, x, y, | 470 setPixel(dst, dstRowBytes, dstInfo, x, y, |
452 indices[which]); | 471 indices[which]); |
453 which = !which; | 472 which = !which; |
454 } | 473 } |
455 } | 474 } |
456 } | 475 } |
457 } | 476 } |
458 } | 477 } |
OLD | NEW |