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