SkBmpMaskCodec.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/private/SkColorData.h"
  8. #include "src/codec/SkBmpMaskCodec.h"
  9. #include "src/codec/SkCodecPriv.h"
  10. /*
  11. * Creates an instance of the decoder
  12. */
  13. SkBmpMaskCodec::SkBmpMaskCodec(SkEncodedInfo&& info,
  14. std::unique_ptr<SkStream> stream,
  15. uint16_t bitsPerPixel, SkMasks* masks,
  16. SkCodec::SkScanlineOrder rowOrder)
  17. : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
  18. , fMasks(masks)
  19. , fMaskSwizzler(nullptr)
  20. {}
  21. /*
  22. * Initiates the bitmap decode
  23. */
  24. SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo,
  25. void* dst, size_t dstRowBytes,
  26. const Options& opts,
  27. int* rowsDecoded) {
  28. if (opts.fSubset) {
  29. // Subsets are not supported.
  30. return kUnimplemented;
  31. }
  32. if (dstInfo.dimensions() != this->dimensions()) {
  33. SkCodecPrintf("Error: scaling not supported.\n");
  34. return kInvalidScale;
  35. }
  36. Result result = this->prepareToDecode(dstInfo, opts);
  37. if (kSuccess != result) {
  38. return result;
  39. }
  40. int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
  41. if (rows != dstInfo.height()) {
  42. *rowsDecoded = rows;
  43. return kIncompleteInput;
  44. }
  45. return kSuccess;
  46. }
  47. SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
  48. const SkCodec::Options& options) {
  49. if (this->colorXform()) {
  50. this->resetXformBuffer(dstInfo.width());
  51. }
  52. SkImageInfo swizzlerInfo = dstInfo;
  53. if (this->colorXform()) {
  54. swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
  55. if (kPremul_SkAlphaType == dstInfo.alphaType()) {
  56. swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
  57. }
  58. }
  59. bool srcIsOpaque = this->getEncodedInfo().opaque();
  60. fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(swizzlerInfo, srcIsOpaque,
  61. fMasks.get(), this->bitsPerPixel(), options));
  62. SkASSERT(fMaskSwizzler);
  63. return SkCodec::kSuccess;
  64. }
  65. /*
  66. * Performs the decoding
  67. */
  68. int SkBmpMaskCodec::decodeRows(const SkImageInfo& dstInfo,
  69. void* dst, size_t dstRowBytes,
  70. const Options& opts) {
  71. // Iterate over rows of the image
  72. uint8_t* srcRow = this->srcBuffer();
  73. const int height = dstInfo.height();
  74. for (int y = 0; y < height; y++) {
  75. // Read a row of the input
  76. if (this->stream()->read(srcRow, this->srcRowBytes()) != this->srcRowBytes()) {
  77. SkCodecPrintf("Warning: incomplete input stream.\n");
  78. return y;
  79. }
  80. // Decode the row in destination format
  81. uint32_t row = this->getDstRow(y, height);
  82. void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
  83. if (this->colorXform()) {
  84. fMaskSwizzler->swizzle(this->xformBuffer(), srcRow);
  85. this->applyColorXform(dstRow, this->xformBuffer(), fMaskSwizzler->swizzleWidth());
  86. } else {
  87. fMaskSwizzler->swizzle(dstRow, srcRow);
  88. }
  89. }
  90. // Finished decoding the entire image
  91. return height;
  92. }