Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 37 kBitMasks_BmpCompressionMethod = 3, | 38 kBitMasks_BmpCompressionMethod = 3, |
| 38 kJpeg_BmpCompressionMethod = 4, | 39 kJpeg_BmpCompressionMethod = 4, |
| 39 kPng_BmpCompressionMethod = 5, | 40 kPng_BmpCompressionMethod = 5, |
| 40 kAlphaBitMasks_BmpCompressionMethod = 6, | 41 kAlphaBitMasks_BmpCompressionMethod = 6, |
| 41 kCMYK_BmpCompressionMethod = 11, | 42 kCMYK_BmpCompressionMethod = 11, |
| 42 kCMYK8BitRLE_BmpCompressionMethod = 12, | 43 kCMYK8BitRLE_BmpCompressionMethod = 12, |
| 43 kCMYK4BitRLE_BmpCompressionMethod = 13 | 44 kCMYK4BitRLE_BmpCompressionMethod = 13 |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 /* | 47 /* |
| 47 * Used to define the input format of the bmp | |
| 48 */ | |
| 49 enum BmpInputFormat { | |
| 50 kStandard_BmpInputFormat, | |
| 51 kRLE_BmpInputFormat, | |
| 52 kBitMask_BmpInputFormat, | |
| 53 kUnknown_BmpInputFormat | |
| 54 }; | |
| 55 | |
| 56 /* | |
| 57 * Checks the start of the stream to see if the image is a bitmap | 48 * Checks the start of the stream to see if the image is a bitmap |
| 58 */ | 49 */ |
| 59 bool SkBmpCodec::IsBmp(SkStream* stream) { | 50 bool SkBmpCodec::IsBmp(SkStream* stream) { |
| 60 // TODO: Support "IC", "PT", "CI", "CP", "BA" | 51 // TODO: Support "IC", "PT", "CI", "CP", "BA" |
| 61 const char bmpSig[] = { 'B', 'M' }; | 52 const char bmpSig[] = { 'B', 'M' }; |
| 62 char buffer[sizeof(bmpSig)]; | 53 char buffer[sizeof(bmpSig)]; |
| 63 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && | 54 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && |
| 64 !memcmp(buffer, bmpSig, sizeof(bmpSig)); | 55 !memcmp(buffer, bmpSig, sizeof(bmpSig)); |
| 65 } | 56 } |
| 66 | 57 |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 */ | 547 */ |
| 557 uint32_t SkBmpCodec::computeNumColors(uint32_t numColors) { | 548 uint32_t SkBmpCodec::computeNumColors(uint32_t numColors) { |
| 558 // Zero is a default for maxColors | 549 // Zero is a default for maxColors |
| 559 // Also set numColors to maxColors when it is too large | 550 // Also set numColors to maxColors when it is too large |
| 560 uint32_t maxColors = 1 << fBitsPerPixel; | 551 uint32_t maxColors = 1 << fBitsPerPixel; |
| 561 if (numColors == 0 || numColors >= maxColors) { | 552 if (numColors == 0 || numColors >= maxColors) { |
| 562 return maxColors; | 553 return maxColors; |
| 563 } | 554 } |
| 564 return numColors; | 555 return numColors; |
| 565 } | 556 } |
| 557 | |
| 558 /* | |
| 559 * Scanline decoder for bmps | |
| 560 */ | |
| 561 class SkBmpScanlineDecoder : public SkScanlineDecoder { | |
| 562 public: | |
| 563 SkBmpScanlineDecoder(SkBmpCodec* codec) | |
| 564 : INHERITED(codec->getInfo()) | |
| 565 , fCodec(codec) | |
| 566 {} | |
| 567 | |
| 568 SkEncodedFormat onGetEncodedFormat() const override { | |
| 569 return kBMP_SkEncodedFormat; | |
| 570 } | |
| 571 | |
| 572 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options& options, | |
| 573 SkPMColor inputColorPtr[], int* inputColorCount) ove rride { | |
| 574 if (!fCodec->rewindIfNeeded()) { | |
| 575 return SkCodec::kCouldNotRewind; | |
| 576 } | |
| 577 if (options.fSubset) { | |
| 578 // Subsets are not supported. | |
| 579 return SkCodec::kUnimplemented; | |
| 580 } | |
| 581 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | |
| 582 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) { | |
| 583 return SkCodec::kInvalidScale; | |
| 584 } | |
| 585 } | |
| 586 if (!conversion_possible(dstInfo, this->getInfo())) { | |
| 587 SkCodecPrintf("Error: cannot convert input type to output type.\n"); | |
| 588 return SkCodec::kInvalidConversion; | |
| 589 } | |
| 590 | |
| 591 return fCodec->onStart(dstInfo, options, inputColorPtr, inputColorCount) ; | |
| 592 } | |
| 593 | |
| 594 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { | |
| 595 // Create a new image info representing the portion of the image to deco de | |
| 596 SkImageInfo rowInfo = this->dstInfo().makeWH(fCodec->getInfo().width(), count); | |
| 597 | |
| 598 // Decode the requested rows | |
| 599 return fCodec->decode(rowInfo, dst, rowBytes, this->options()); | |
| 600 } | |
| 601 | |
| 602 SkScanlineOrder onGetScanlineOrder() const override { | |
| 603 if (SkBmpCodec::kTopDown_RowOrder == fCodec->fRowOrder) { | |
| 604 return kTopDown_SkScanlineOrder; | |
| 605 } else { | |
|
scroggo
2015/08/26 22:40:09
nit: This else is unnecessary. Typically, this is
| |
| 606 return kOutOfOrder_SkScanlineOrder; | |
| 607 } | |
| 608 } | |
| 609 | |
| 610 int onGetY() const override { | |
| 611 if (SkBmpCodec::kTopDown_RowOrder == fCodec->fRowOrder) { | |
| 612 return INHERITED::onGetY(); | |
| 613 } else { | |
| 614 return this->dstInfo().height() - INHERITED::onGetY() - 1; | |
| 615 } | |
| 616 } | |
| 617 | |
| 618 // TODO(msarett): Override default skipping with something more clever. | |
| 619 // TODO(msarett): Consider other optimizations for this codec. | |
| 620 | |
| 621 private: | |
| 622 SkAutoTDelete<SkBmpCodec> fCodec; | |
| 623 | |
| 624 typedef SkScanlineDecoder INHERITED; | |
| 625 }; | |
| 626 | |
| 627 SkScanlineDecoder* SkBmpCodec::NewSDFromStream(SkStream* stream) { | |
| 628 SkAutoTDelete<SkBmpCodec> codec(static_cast<SkBmpCodec*>(SkBmpCodec::NewFrom Stream(stream))); | |
| 629 if (!codec) { | |
| 630 return NULL; | |
| 631 } | |
| 632 | |
| 633 return SkNEW_ARGS(SkBmpScanlineDecoder, (codec.detach())); | |
| 634 } | |
| OLD | NEW |