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" |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 SkCodecPrintf("Warning: incomplete RLE file.\n"); | 176 SkCodecPrintf("Warning: incomplete RLE file.\n"); |
177 } | 177 } |
178 if (fRLEBytes == 0) { | 178 if (fRLEBytes == 0) { |
179 SkCodecPrintf("Error: could not read RLE image data.\n"); | 179 SkCodecPrintf("Error: could not read RLE image data.\n"); |
180 return false; | 180 return false; |
181 } | 181 } |
182 return true; | 182 return true; |
183 } | 183 } |
184 | 184 |
185 /* | 185 /* |
186 * It is possible for an encoded image stream to contain more encoded data than | |
187 * it reports that it has. Before signalling kIncompleteInput, we should check | |
188 * if we can reset the stream buffer with additional data. | |
189 */ | |
190 size_t SkBmpRLECodec::resetStreamBuffer() { | |
191 size_t remainingBytes = fRLEBytes - fCurrRLEByte; | |
scroggo
2015/08/07 13:35:35
This can be const?
msarett
2015/08/11 22:35:02
Yes, I have made it const.
| |
192 uint8_t* buffer = fStreamBuffer.get(); | |
193 | |
194 // Store the remaining bytes to the start of our new buffer | |
scroggo
2015/08/07 13:35:35
"new" buffer? It is the same buffer, correct?
msarett
2015/08/11 22:35:02
Yes. I have changed the comment to make this make
| |
195 for (uint32_t i = 0; i < remainingBytes; i++) { | |
scroggo
2015/08/07 13:35:35
Why did you use uint32_t here? remainingBytes is a
msarett
2015/08/11 22:35:02
It should be a size_t. The loop is gone (using me
| |
196 buffer[i] = buffer[fCurrRLEByte + i]; | |
scroggo
2015/08/07 13:35:35
Are these indices guaranteed to be safe/valid?
Al
msarett
2015/08/11 22:35:03
I think the indices are safe. I have run through
| |
197 } | |
198 | |
199 // Adjust the buffer ptr to the start of the data to be overwritten | |
200 buffer += remainingBytes; | |
201 | |
202 // Try to read additional bytes from the stream | |
203 size_t additionalBytes = this->stream()->read(buffer, fRLEBytes - remainingB ytes); | |
scroggo
2015/08/07 13:35:35
Two thoughts: looking at your calculation above:
msarett
2015/08/11 22:35:02
We will either read to the end of the stream or en
| |
204 | |
205 // Update counters and return the number of bytes we currently have availabl e | |
206 fCurrRLEByte = 0; | |
207 fRLEBytes = remainingBytes + additionalBytes; | |
scroggo
2015/08/07 13:35:35
So this is now the total bytes that we have read f
msarett
2015/08/11 22:35:02
Yes. fRLEBytes is no longer the size of the buffe
| |
208 return fRLEBytes - fCurrRLEByte; | |
scroggo
2015/08/07 13:35:35
fCurrRLEByte is 0, so this is just fRLEBytes, righ
msarett
2015/08/11 22:35:02
Yes this is correct.
| |
209 } | |
210 | |
211 /* | |
186 * Set an RLE pixel using the color table | 212 * Set an RLE pixel using the color table |
187 */ | 213 */ |
188 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, | 214 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
189 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, | 215 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
190 uint8_t index) { | 216 uint8_t index) { |
191 // Set the row | 217 // Set the row |
192 int height = dstInfo.height(); | 218 int height = dstInfo.height(); |
193 int row; | 219 int row; |
194 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { | 220 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { |
195 row = height - y - 1; | 221 row = height - y - 1; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 // succeeded. | 306 // succeeded. |
281 if (y >= height) { | 307 if (y >= height) { |
282 // 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 |
283 // success, but we may be performing a scanline decode, which | 309 // success, but we may be performing a scanline decode, which |
284 // may require us to stop before decoding the full height. | 310 // may require us to stop before decoding the full height. |
285 return kSuccess; | 311 return kSuccess; |
286 } | 312 } |
287 | 313 |
288 // Every entry takes at least two bytes | 314 // Every entry takes at least two bytes |
289 if ((int) fRLEBytes - fCurrRLEByte < 2) { | 315 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
290 SkCodecPrintf("Warning: incomplete RLE input.\n"); | 316 SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
291 return kIncompleteInput; | 317 if (this->resetStreamBuffer() < 2) { |
318 return kIncompleteInput; | |
319 } | |
292 } | 320 } |
293 | 321 |
294 // Read the next two bytes. These bytes have different meanings | 322 // Read the next two bytes. These bytes have different meanings |
295 // depending on their values. In the first interpretation, the first | 323 // depending on their values. In the first interpretation, the first |
296 // byte is an escape flag and the second byte indicates what special | 324 // byte is an escape flag and the second byte indicates what special |
297 // task to perform. | 325 // task to perform. |
298 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++]; | 326 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++]; |
299 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++]; | 327 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++]; |
300 | 328 |
301 // Perform decoding | 329 // Perform decoding |
302 if (RLE_ESCAPE == flag) { | 330 if (RLE_ESCAPE == flag) { |
303 switch (task) { | 331 switch (task) { |
304 case RLE_EOL: | 332 case RLE_EOL: |
305 x = 0; | 333 x = 0; |
306 y++; | 334 y++; |
307 break; | 335 break; |
308 case RLE_EOF: | 336 case RLE_EOF: |
309 return kSuccess; | 337 return kSuccess; |
310 case RLE_DELTA: { | 338 case RLE_DELTA: { |
311 // Two bytes are needed to specify delta | 339 // Two bytes are needed to specify delta |
312 if ((int) fRLEBytes - fCurrRLEByte < 2) { | 340 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
313 SkCodecPrintf("Warning: incomplete RLE input\n"); | 341 SkCodecPrintf("Warning: might be incomplete RLE input.\n "); |
314 return kIncompleteInput; | 342 if (this->resetStreamBuffer() < 2) { |
343 return kIncompleteInput; | |
344 } | |
315 } | 345 } |
316 // Modify x and y | 346 // Modify x and y |
317 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++]; | 347 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++]; |
318 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++]; | 348 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++]; |
319 x += dx; | 349 x += dx; |
320 y += dy; | 350 y += dy; |
321 if (x > width || y > height) { | 351 if (x > width || y > height) { |
322 SkCodecPrintf("Warning: invalid RLE input 1.\n"); | 352 SkCodecPrintf("Warning: invalid RLE input.\n"); |
323 return kIncompleteInput; | 353 return kInvalidInput; |
324 } | 354 } |
325 break; | 355 break; |
326 } | 356 } |
327 default: { | 357 default: { |
328 // If task does not match any of the above signals, it | 358 // If task does not match any of the above signals, it |
329 // indicates that we have a sequence of non-RLE pixels. | 359 // indicates that we have a sequence of non-RLE pixels. |
330 // Furthermore, the value of task is equal to the number | 360 // Furthermore, the value of task is equal to the number |
331 // of pixels to interpret. | 361 // of pixels to interpret. |
332 uint8_t numPixels = task; | 362 uint8_t numPixels = task; |
333 const size_t rowBytes = compute_row_bytes(numPixels, | 363 const size_t rowBytes = compute_row_bytes(numPixels, |
334 this->bitsPerPixel()); | 364 this->bitsPerPixel()); |
335 // Abort if setting numPixels moves us off the edge of the | 365 // Abort if setting numPixels moves us off the edge of the |
336 // image. Also abort if there are not enough bytes | 366 // image. |
367 if (x + numPixels > width) { | |
368 SkCodecPrintf("Warning: invalid RLE input.\n"); | |
369 return kInvalidInput; | |
370 } | |
371 // Also abort if there are not enough bytes | |
337 // remaining in the stream to set numPixels. | 372 // remaining in the stream to set numPixels. |
338 if (x + numPixels > width || | 373 if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) { |
339 (int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) { | 374 SkCodecPrintf("Warning: might be incomplete RLE input.\n "); |
340 SkCodecPrintf("Warning: invalid RLE input 2.\n"); | 375 if (this->resetStreamBuffer() < SkAlign2(rowBytes)) { |
341 return kIncompleteInput; | 376 return kIncompleteInput; |
377 } | |
342 } | 378 } |
343 // Set numPixels number of pixels | 379 // Set numPixels number of pixels |
344 while (numPixels > 0) { | 380 while (numPixels > 0) { |
345 switch(this->bitsPerPixel()) { | 381 switch(this->bitsPerPixel()) { |
346 case 4: { | 382 case 4: { |
347 SkASSERT(fCurrRLEByte < fRLEBytes); | 383 SkASSERT(fCurrRLEByte < fRLEBytes); |
348 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++ ]; | 384 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++ ]; |
349 setPixel(dst, dstRowBytes, dstInfo, x++, | 385 setPixel(dst, dstRowBytes, dstInfo, x++, |
350 y, val >> 4); | 386 y, val >> 4); |
351 numPixels--; | 387 numPixels--; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 // If the first byte read is not a flag, it indicates the number of | 423 // If the first byte read is not a flag, it indicates the number of |
388 // pixels to set in RLE mode. | 424 // pixels to set in RLE mode. |
389 const uint8_t numPixels = flag; | 425 const uint8_t numPixels = flag; |
390 const int endX = SkTMin<int>(x + numPixels, width); | 426 const int endX = SkTMin<int>(x + numPixels, width); |
391 | 427 |
392 if (24 == this->bitsPerPixel()) { | 428 if (24 == this->bitsPerPixel()) { |
393 // In RLE24, the second byte read is part of the pixel color. | 429 // In RLE24, the second byte read is part of the pixel color. |
394 // There are two more required bytes to finish encoding the | 430 // There are two more required bytes to finish encoding the |
395 // color. | 431 // color. |
396 if ((int) fRLEBytes - fCurrRLEByte < 2) { | 432 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
397 SkCodecPrintf("Warning: incomplete RLE input\n"); | 433 SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
398 return kIncompleteInput; | 434 if (this->resetStreamBuffer() < 2) { |
435 return kIncompleteInput; | |
436 } | |
399 } | 437 } |
400 | 438 |
401 // Fill the pixels up to endX with the specified color | 439 // Fill the pixels up to endX with the specified color |
402 uint8_t blue = task; | 440 uint8_t blue = task; |
403 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++]; | 441 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++]; |
404 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++]; | 442 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++]; |
405 while (x < endX) { | 443 while (x < endX) { |
406 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, | 444 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, |
407 green, blue); | 445 green, blue); |
408 } | 446 } |
(...skipping 12 matching lines...) Expand all Loading... | |
421 // Set the indicated number of pixels | 459 // Set the indicated number of pixels |
422 for (int which = 0; x < endX; x++) { | 460 for (int which = 0; x < endX; x++) { |
423 setPixel(dst, dstRowBytes, dstInfo, x, y, | 461 setPixel(dst, dstRowBytes, dstInfo, x, y, |
424 indices[which]); | 462 indices[which]); |
425 which = !which; | 463 which = !which; |
426 } | 464 } |
427 } | 465 } |
428 } | 466 } |
429 } | 467 } |
430 } | 468 } |
OLD | NEW |