Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: src/codec/SkBmpRLECodec.cpp

Issue 1287423002: Scanline decoding for bmp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix windows errors Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/codec/SkBmpRLECodec.h ('k') | src/codec/SkBmpStandardCodec.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
154 fCurrRLEByte = 0;
156 return true; 155 return true;
157 } 156 }
158 157
159 /* 158 /*
160 * Before signalling kIncompleteInput, we should attempt to load the 159 * Before signalling kIncompleteInput, we should attempt to load the
161 * stream buffer with additional data. 160 * stream buffer with additional data.
162 * 161 *
163 * @return the number of bytes remaining in the stream buffer after 162 * @return the number of bytes remaining in the stream buffer after
164 * attempting to read more bytes from the stream 163 * attempting to read more bytes from the stream
165 */ 164 */
(...skipping 24 matching lines...) Expand all
190 fRLEBytes = remainingBytes + additionalBytes; 189 fRLEBytes = remainingBytes + additionalBytes;
191 return fRLEBytes; 190 return fRLEBytes;
192 } 191 }
193 192
194 /* 193 /*
195 * Set an RLE pixel using the color table 194 * Set an RLE pixel using the color table
196 */ 195 */
197 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, 196 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
198 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, 197 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
199 uint8_t index) { 198 uint8_t index) {
200 // Set the row 199 if (is_coord_necessary(x, fSampleX, dstInfo.width())) {
201 int height = dstInfo.height(); 200 // Set the row
202 int row; 201 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 202
209 // Set the pixel based on destination color type 203 // Set the pixel based on destination color type
210 switch (dstInfo.colorType()) { 204 const int dstX = get_dst_coord(x, fSampleX);
211 case kN32_SkColorType: { 205 switch (dstInfo.colorType()) {
212 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowB ytes); 206 case kN32_SkColorType: {
213 dstRow[x] = fColorTable->operator[](index); 207 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dst RowBytes);
214 break; 208 dstRow[dstX] = 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[dstX] = 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 (is_coord_necessary(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 const int dstX = get_dst_coord(x, fSampleX);
242 row = y; 238 switch (dstInfo.colorType()) {
239 case kN32_SkColorType: {
240 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dst RowBytes);
241 dstRow[dstX] = 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[dstX] = SkPack888ToRGB16(red, green, blue);
247 break;
248 }
249 default:
250 // This case should not be reached. We should catch an invalid
251 // color type when we check that the conversion is possible.
252 SkASSERT(false);
253 break;
254 }
255 }
256 }
257
258 SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo,
259 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo lorCount) {
260 // Create the color table if necessary and prepare the stream for decode
261 // Note that if it is non-NULL, inputColorCount will be modified
262 if (!this->createColorTable(inputColorCount)) {
263 SkCodecPrintf("Error: could not create color table.\n");
264 return SkCodec::kInvalidInput;
243 } 265 }
244 266
245 // Set the pixel based on destination color type 267 // Copy the color table to the client if necessary
246 switch (dstInfo.colorType()) { 268 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount) ;
247 case kN32_SkColorType: { 269
248 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowB ytes); 270 // Initialize a buffer for encoded RLE data
249 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); 271 if (!this->initializeStreamBuffer()) {
250 break; 272 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
251 } 273 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 } 274 }
275
276 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &fSampleX, NULL);
277
278 return SkCodec::kSuccess;
263 } 279 }
264 280
265 /* 281 /*
266 * Performs the bitmap decoding for RLE input format 282 * Performs the bitmap decoding for RLE input format
267 * RLE decoding is performed all at once, rather than a one row at a time 283 * RLE decoding is performed all at once, rather than a one row at a time
268 */ 284 */
269 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, 285 SkCodec::Result SkBmpRLECodec::decodeRows(const SkImageInfo& dstInfo,
270 void* dst, size_t dstRowBytes, 286 void* dst, size_t dstRowBytes,
271 const Options& opts) { 287 const Options& opts) {
272 // Set RLE flags 288 // Set RLE flags
273 static const uint8_t RLE_ESCAPE = 0; 289 static const uint8_t RLE_ESCAPE = 0;
274 static const uint8_t RLE_EOL = 0; 290 static const uint8_t RLE_EOL = 0;
275 static const uint8_t RLE_EOF = 1; 291 static const uint8_t RLE_EOF = 1;
276 static const uint8_t RLE_DELTA = 2; 292 static const uint8_t RLE_DELTA = 2;
277 293
278 // Set constant values 294 // Set constant values
279 const int width = dstInfo.width(); 295 const int width = this->getInfo().width();
280 const int height = dstInfo.height(); 296 const int height = dstInfo.height();
281 297
282 // Destination parameters 298 // Destination parameters
283 int x = 0; 299 int x = 0;
284 int y = 0; 300 int y = 0;
285 301
286 // Set the background as transparent. Then, if the RLE code skips pixels, 302 // Set the background as transparent. Then, if the RLE code skips pixels,
287 // the skipped pixels will be transparent. 303 // the skipped pixels will be transparent.
288 // Because of the need for transparent pixels, kN32 is the only color 304 // Because of the need for transparent pixels, kN32 is the only color
289 // type that makes sense for the destination format. 305 // type that makes sense for the destination format.
290 SkASSERT(kN32_SkColorType == dstInfo.colorType()); 306 SkASSERT(kN32_SkColorType == dstInfo.colorType());
291 if (kNo_ZeroInitialized == opts.fZeroInitialized) { 307 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT,
292 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, nullptr); 308 NULL, opts.fZeroInitialized);
293 }
294 309
295 while (true) { 310 while (true) {
296 // If we have reached a row that is beyond the requested height, we have 311 // If we have reached a row that is beyond the requested height, we have
297 // succeeded. 312 // succeeded.
298 if (y >= height) { 313 if (y >= height) {
299 // It would be better to check for the EOF marker before returning 314 // It would be better to check for the EOF marker before returning
300 // success, but we may be performing a scanline decode, which 315 // success, but we may be performing a scanline decode, which
301 // may require us to stop before decoding the full height. 316 // may require us to stop before decoding the full height.
302 return kSuccess; 317 return kSuccess;
303 } 318 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 463
449 // Set the indicated number of pixels 464 // Set the indicated number of pixels
450 for (int which = 0; x < endX; x++) { 465 for (int which = 0; x < endX; x++) {
451 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]); 466 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
452 which = !which; 467 which = !which;
453 } 468 }
454 } 469 }
455 } 470 }
456 } 471 }
457 } 472 }
OLDNEW
« no previous file with comments | « src/codec/SkBmpRLECodec.h ('k') | src/codec/SkBmpStandardCodec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698