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