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 "SkBmpCodec.h" | 8 #include "SkBmpCodec.h" |
9 #include "SkBmpMaskCodec.h" | 9 #include "SkBmpMaskCodec.h" |
10 #include "SkBmpRLECodec.h" | 10 #include "SkBmpRLECodec.h" |
11 #include "SkBmpStandardCodec.h" | 11 #include "SkBmpStandardCodec.h" |
12 #include "SkCodecPriv.h" | 12 #include "SkCodecPriv.h" |
13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
| 14 #include "SkScaledCodec.h" |
14 #include "SkStream.h" | 15 #include "SkStream.h" |
15 | 16 |
16 /* | 17 /* |
17 * Defines the version and type of the second bitmap header | 18 * Defines the version and type of the second bitmap header |
18 */ | 19 */ |
19 enum BmpHeaderType { | 20 enum BmpHeaderType { |
20 kInfoV1_BmpHeaderType, | 21 kInfoV1_BmpHeaderType, |
21 kInfoV2_BmpHeaderType, | 22 kInfoV2_BmpHeaderType, |
22 kInfoV3_BmpHeaderType, | 23 kInfoV3_BmpHeaderType, |
23 kInfoV4_BmpHeaderType, | 24 kInfoV4_BmpHeaderType, |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 height = (int) get_short(iBuffer.get(), 2); | 255 height = (int) get_short(iBuffer.get(), 2); |
255 bitsPerPixel = get_short(iBuffer.get(), 6); | 256 bitsPerPixel = get_short(iBuffer.get(), 6); |
256 bytesPerColor = 3; | 257 bytesPerColor = 3; |
257 } else { | 258 } else { |
258 // There are no valid bmp headers | 259 // There are no valid bmp headers |
259 SkCodecPrintf("Error: second bitmap header size is invalid.\n"); | 260 SkCodecPrintf("Error: second bitmap header size is invalid.\n"); |
260 return false; | 261 return false; |
261 } | 262 } |
262 | 263 |
263 // Check for valid dimensions from header | 264 // Check for valid dimensions from header |
264 RowOrder rowOrder = kBottomUp_RowOrder; | 265 SkScanlineDecoder::SkScanlineOrder rowOrder = SkScanlineDecoder::kBottomUp_S
kScanlineOrder; |
265 if (height < 0) { | 266 if (height < 0) { |
266 height = -height; | 267 height = -height; |
267 rowOrder = kTopDown_RowOrder; | 268 rowOrder = SkScanlineDecoder::kTopDown_SkScanlineOrder; |
268 } | 269 } |
269 // The height field for bmp in ico is double the actual height because they | 270 // The height field for bmp in ico is double the actual height because they |
270 // contain an XOR mask followed by an AND mask | 271 // contain an XOR mask followed by an AND mask |
271 if (inIco) { | 272 if (inIco) { |
272 height /= 2; | 273 height /= 2; |
273 } | 274 } |
274 if (width <= 0 || height <= 0) { | 275 if (width <= 0 || height <= 0) { |
275 // TODO: Decide if we want to disable really large bmps as well. | 276 // TODO: Decide if we want to disable really large bmps as well. |
276 // https://code.google.com/p/skia/issues/detail?id=3617 | 277 // https://code.google.com/p/skia/issues/detail?id=3617 |
277 SkCodecPrintf("Error: invalid bitmap dimensions.\n"); | 278 SkCodecPrintf("Error: invalid bitmap dimensions.\n"); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 // codec has taken ownership of stream, so we do not need to | 520 // codec has taken ownership of stream, so we do not need to |
520 // delete it. | 521 // delete it. |
521 SkASSERT(codec); | 522 SkASSERT(codec); |
522 streamDeleter.detach(); | 523 streamDeleter.detach(); |
523 return codec; | 524 return codec; |
524 } | 525 } |
525 return nullptr; | 526 return nullptr; |
526 } | 527 } |
527 | 528 |
528 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, | 529 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, |
529 uint16_t bitsPerPixel, RowOrder rowOrder) | 530 uint16_t bitsPerPixel, SkScanlineDecoder::SkScanlineOrder rowOrder) |
530 : INHERITED(info, stream) | 531 : INHERITED(info, stream) |
531 , fBitsPerPixel(bitsPerPixel) | 532 , fBitsPerPixel(bitsPerPixel) |
532 , fRowOrder(rowOrder) | 533 , fRowOrder(rowOrder) |
533 {} | 534 {} |
534 | 535 |
535 bool SkBmpCodec::onRewind() { | 536 bool SkBmpCodec::onRewind() { |
536 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr); | 537 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr); |
537 } | 538 } |
538 | 539 |
| 540 int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) { |
| 541 if (SkScanlineDecoder::kTopDown_SkScanlineOrder == fRowOrder) { |
| 542 return y; |
| 543 } |
| 544 SkASSERT(SkScanlineDecoder::kBottomUp_SkScanlineOrder == fRowOrder); |
| 545 return height - y - 1; |
| 546 } |
| 547 |
539 /* | 548 /* |
540 * Get the destination row to start filling from | 549 * Get the destination row to start filling from |
541 * Used to fill the remainder of the image on incomplete input for bmps | 550 * Used to fill the remainder of the image on incomplete input for bmps |
542 * This is tricky since bmps may be kTopDown or kBottomUp. For kTopDown, | 551 * This is tricky since bmps may be kTopDown or kBottomUp. For kTopDown, |
543 * we start filling from where we left off, but for kBottomUp we start | 552 * we start filling from where we left off, but for kBottomUp we start |
544 * filling at the top of the image. | 553 * filling at the top of the image. |
545 */ | 554 */ |
546 void* SkBmpCodec::getDstStartRow(void* dst, size_t dstRowBytes, int32_t y) const
{ | 555 void* SkBmpCodec::getDstStartRow(void* dst, size_t dstRowBytes, int32_t y) const
{ |
547 return (kTopDown_RowOrder == fRowOrder) ? SkTAddOffset<void*>(dst, y * dstRo
wBytes) : dst; | 556 return (SkScanlineDecoder::kTopDown_SkScanlineOrder == fRowOrder) ? |
| 557 SkTAddOffset<void*>(dst, y * dstRowBytes) : dst; |
548 } | 558 } |
549 | 559 |
550 /* | 560 /* |
551 * Compute the number of colors in the color table | 561 * Compute the number of colors in the color table |
552 */ | 562 */ |
553 uint32_t SkBmpCodec::computeNumColors(uint32_t numColors) { | 563 uint32_t SkBmpCodec::computeNumColors(uint32_t numColors) { |
554 // Zero is a default for maxColors | 564 // Zero is a default for maxColors |
555 // Also set numColors to maxColors when it is too large | 565 // Also set numColors to maxColors when it is too large |
556 uint32_t maxColors = 1 << fBitsPerPixel; | 566 uint32_t maxColors = 1 << fBitsPerPixel; |
557 if (numColors == 0 || numColors >= maxColors) { | 567 if (numColors == 0 || numColors >= maxColors) { |
558 return maxColors; | 568 return maxColors; |
559 } | 569 } |
560 return numColors; | 570 return numColors; |
561 } | 571 } |
| 572 |
| 573 /* |
| 574 * Scanline decoder for bmps |
| 575 */ |
| 576 class SkBmpScanlineDecoder : public SkScanlineDecoder { |
| 577 public: |
| 578 SkBmpScanlineDecoder(SkBmpCodec* codec) |
| 579 : INHERITED(codec->getInfo()) |
| 580 , fCodec(codec) |
| 581 {} |
| 582 |
| 583 SkEncodedFormat onGetEncodedFormat() const override { |
| 584 return kBMP_SkEncodedFormat; |
| 585 } |
| 586 |
| 587 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options&
options, |
| 588 SkPMColor inputColorPtr[], int* inputColorCount) ove
rride { |
| 589 if (!fCodec->rewindIfNeeded()) { |
| 590 return SkCodec::kCouldNotRewind; |
| 591 } |
| 592 if (options.fSubset) { |
| 593 // Subsets are not supported. |
| 594 return SkCodec::kUnimplemented; |
| 595 } |
| 596 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 597 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(),
dstInfo)) { |
| 598 return SkCodec::kInvalidScale; |
| 599 } |
| 600 } |
| 601 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 602 SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 603 return SkCodec::kInvalidConversion; |
| 604 } |
| 605 |
| 606 return fCodec->prepareToDecode(dstInfo, options, inputColorPtr, inputCol
orCount); |
| 607 } |
| 608 |
| 609 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri
de { |
| 610 // Create a new image info representing the portion of the image to deco
de |
| 611 SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), co
unt); |
| 612 |
| 613 // Decode the requested rows |
| 614 return fCodec->decodeRows(rowInfo, dst, rowBytes, this->options()); |
| 615 } |
| 616 |
| 617 SkScanlineOrder onGetScanlineOrder() const override { |
| 618 return fCodec->fRowOrder; |
| 619 } |
| 620 |
| 621 int onGetY() const override { |
| 622 return fCodec->getDstRow(this->INHERITED::onGetY(), this->dstInfo().heig
ht()); |
| 623 } |
| 624 |
| 625 // TODO(msarett): Override default skipping with something more clever. |
| 626 |
| 627 private: |
| 628 SkAutoTDelete<SkBmpCodec> fCodec; |
| 629 |
| 630 typedef SkScanlineDecoder INHERITED; |
| 631 }; |
| 632 |
| 633 SkScanlineDecoder* SkBmpCodec::NewSDFromStream(SkStream* stream) { |
| 634 SkAutoTDelete<SkBmpCodec> codec(static_cast<SkBmpCodec*>(SkBmpCodec::NewFrom
Stream(stream))); |
| 635 if (!codec) { |
| 636 return NULL; |
| 637 } |
| 638 |
| 639 return SkNEW_ARGS(SkBmpScanlineDecoder, (codec.detach())); |
| 640 } |
OLD | NEW |