SkBmpCodec.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 SkBmpCodec_DEFINED
  8. #define SkBmpCodec_DEFINED
  9. #include "include/codec/SkCodec.h"
  10. #include "include/core/SkColorSpace.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "include/core/SkStream.h"
  13. #include "include/core/SkTypes.h"
  14. #include "src/codec/SkColorTable.h"
  15. #include "src/codec/SkSwizzler.h"
  16. /*
  17. * This class enables code sharing between its bmp codec subclasses. The
  18. * subclasses actually do the work.
  19. */
  20. class SkBmpCodec : public SkCodec {
  21. public:
  22. static bool IsBmp(const void*, size_t);
  23. /*
  24. * Assumes IsBmp was called and returned true
  25. * Creates a bmp decoder
  26. * Reads enough of the stream to determine the image format
  27. */
  28. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
  29. /*
  30. * Creates a bmp decoder for a bmp embedded in ico
  31. * Reads enough of the stream to determine the image format
  32. */
  33. static std::unique_ptr<SkCodec> MakeFromIco(std::unique_ptr<SkStream>, Result*);
  34. protected:
  35. SkBmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream>,
  36. uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder);
  37. SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kBMP; }
  38. /*
  39. * Read enough of the stream to initialize the SkBmpCodec.
  40. * On kSuccess, if codecOut is not nullptr, it will be set to a new SkBmpCodec.
  41. */
  42. static Result ReadHeader(SkStream*, bool inIco, std::unique_ptr<SkCodec>* codecOut);
  43. bool onRewind() override;
  44. /*
  45. * Returns whether this BMP is part of an ICO image.
  46. */
  47. bool inIco() const {
  48. return this->onInIco();
  49. }
  50. virtual bool onInIco() const {
  51. return false;
  52. }
  53. /*
  54. * Get the destination row number corresponding to the encoded row number.
  55. * For kTopDown, we simply return y, but for kBottomUp, the rows will be
  56. * decoded in reverse order.
  57. *
  58. * @param y Iterates from 0 to height, indicating the current row.
  59. * @param height The height of the current subset of the image that we are
  60. * decoding. This is generally equal to the full height
  61. * when we want to decode the full or one when we are
  62. * sampling.
  63. */
  64. int32_t getDstRow(int32_t y, int32_t height) const;
  65. /*
  66. * Accessors used by subclasses
  67. */
  68. uint16_t bitsPerPixel() const { return fBitsPerPixel; }
  69. SkScanlineOrder onGetScanlineOrder() const override { return fRowOrder; }
  70. size_t srcRowBytes() const { return fSrcRowBytes; }
  71. /*
  72. * To be overriden by bmp subclasses, which provide unique implementations.
  73. * Performs subclass specific setup.
  74. *
  75. * @param dstInfo Contains output information. Height specifies
  76. * the total number of rows that will be decoded.
  77. * @param options Additonal options to pass to the decoder.
  78. */
  79. virtual SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
  80. const SkCodec::Options& options) = 0;
  81. SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo,
  82. const SkCodec::Options& options);
  83. uint32_t* xformBuffer() const { return fXformBuffer.get(); }
  84. void resetXformBuffer(int count) { fXformBuffer.reset(new uint32_t[count]); }
  85. /*
  86. * BMPs are typically encoded as BGRA/BGR so this is a more efficient choice
  87. * than RGBA.
  88. */
  89. static constexpr SkColorType kXformSrcColorType = kBGRA_8888_SkColorType;
  90. static constexpr auto kXformSrcColorFormat = skcms_PixelFormat_BGRA_8888;
  91. private:
  92. /*
  93. * Creates a bmp decoder
  94. * Reads enough of the stream to determine the image format
  95. */
  96. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*, bool inIco);
  97. /*
  98. * Decodes the next dstInfo.height() lines.
  99. *
  100. * onGetPixels() uses this for full image decodes.
  101. * SkScaledCodec::onGetPixels() uses the scanline decoder to call this with
  102. * dstInfo.height() = 1, in order to implement sampling.
  103. * A potential future use is to allow the caller to decode a subset of the
  104. * lines in the image.
  105. *
  106. * @param dstInfo Contains output information. Height specifies the
  107. * number of rows to decode at this time.
  108. * @param dst Memory location to store output pixels
  109. * @param dstRowBytes Bytes in a row of the destination
  110. * @return Number of rows successfully decoded
  111. */
  112. virtual int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
  113. const Options& opts) = 0;
  114. virtual bool skipRows(int count);
  115. Result onStartScanlineDecode(const SkImageInfo& dstInfo,
  116. const SkCodec::Options&) override;
  117. int onGetScanlines(void* dst, int count, size_t rowBytes) override;
  118. bool onSkipScanlines(int count) override;
  119. const uint16_t fBitsPerPixel;
  120. const SkScanlineOrder fRowOrder;
  121. const size_t fSrcRowBytes;
  122. std::unique_ptr<uint32_t[]> fXformBuffer;
  123. typedef SkCodec INHERITED;
  124. };
  125. #endif