SkBmpRLECodec.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef SkBmpRLECodec_DEFINED
  8. #define SkBmpRLECodec_DEFINED
  9. #include "include/core/SkImageInfo.h"
  10. #include "include/core/SkTypes.h"
  11. #include "src/codec/SkBmpCodec.h"
  12. #include "src/codec/SkColorTable.h"
  13. #include "src/codec/SkSampler.h"
  14. /*
  15. * This class implements the decoding for bmp images that use an RLE encoding
  16. */
  17. class SkBmpRLECodec : public SkBmpCodec {
  18. public:
  19. /*
  20. * Creates an instance of the decoder
  21. *
  22. * Called only by SkBmpCodec::MakeFromStream
  23. * There should be no other callers despite this being public
  24. *
  25. * @param info contains properties of the encoded data
  26. * @param stream the stream of encoded image data
  27. * @param bitsPerPixel the number of bits used to store each pixel
  28. * @param numColors the number of colors in the color table
  29. * @param bytesPerColor the number of bytes in the stream used to represent
  30. each color in the color table
  31. * @param offset the offset of the image pixel data from the end of the
  32. * headers
  33. * @param rowOrder indicates whether rows are ordered top-down or bottom-up
  34. */
  35. SkBmpRLECodec(SkEncodedInfo&& info, std::unique_ptr<SkStream>,
  36. uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
  37. uint32_t offset, SkCodec::SkScanlineOrder rowOrder);
  38. int setSampleX(int);
  39. int fillWidth() const;
  40. protected:
  41. Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
  42. size_t dstRowBytes, const Options&,
  43. int*) override;
  44. SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
  45. const SkCodec::Options& options) override;
  46. private:
  47. /*
  48. * Creates the color table
  49. * Sets colorCount to the new color count if it is non-nullptr
  50. */
  51. bool createColorTable(SkColorType dstColorType);
  52. bool initializeStreamBuffer();
  53. /*
  54. * Before signalling kIncompleteInput, we should attempt to load the
  55. * stream buffer with additional data.
  56. *
  57. * @return the number of bytes remaining in the stream buffer after
  58. * attempting to read more bytes from the stream
  59. */
  60. size_t checkForMoreData();
  61. /*
  62. * Set an RLE pixel using the color table
  63. */
  64. void setPixel(void* dst, size_t dstRowBytes,
  65. const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
  66. uint8_t index);
  67. /*
  68. * Set an RLE24 pixel from R, G, B values
  69. */
  70. void setRGBPixel(void* dst, size_t dstRowBytes,
  71. const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
  72. uint8_t red, uint8_t green, uint8_t blue);
  73. /*
  74. * If dst is NULL, this is a signal to skip the rows.
  75. */
  76. int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
  77. const Options& opts) override;
  78. int decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
  79. bool skipRows(int count) override;
  80. SkSampler* getSampler(bool createIfNecessary) override;
  81. sk_sp<SkColorTable> fColorTable;
  82. // fNumColors is the number specified in the header, or 0 if not present in the header.
  83. const uint32_t fNumColors;
  84. const uint32_t fBytesPerColor;
  85. const uint32_t fOffset;
  86. static constexpr size_t kBufferSize = 4096;
  87. uint8_t fStreamBuffer[kBufferSize];
  88. size_t fBytesBuffered;
  89. uint32_t fCurrRLEByte;
  90. int fSampleX;
  91. std::unique_ptr<SkSampler> fSampler;
  92. // Scanline decodes allow the client to ask for a single scanline at a time.
  93. // This can be tricky when the RLE encoding instructs the decoder to jump down
  94. // multiple lines. This field keeps track of lines that need to be skipped
  95. // on subsequent calls to decodeRows().
  96. int fLinesToSkip;
  97. typedef SkBmpCodec INHERITED;
  98. };
  99. #endif // SkBmpRLECodec_DEFINED