blob: 4ed12d09cb4d46955683686902129608a9b3e023 [file] [log] [blame]
msarett4ab9d5f2015-08-06 15:34:42 -07001/*
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 */
Hal Canary03a7f5f2017-02-10 09:06:38 -05007#ifndef SkBmpRLECodec_DEFINED
8#define SkBmpRLECodec_DEFINED
msarett4ab9d5f2015-08-06 15:34:42 -07009
10#include "SkBmpCodec.h"
11#include "SkColorTable.h"
12#include "SkImageInfo.h"
scroggoe7fc14b2015-10-02 13:14:46 -070013#include "SkSampler.h"
msarett4ab9d5f2015-08-06 15:34:42 -070014#include "SkTypes.h"
15
16/*
17 * This class implements the decoding for bmp images that use an RLE encoding
18 */
19class SkBmpRLECodec : public SkBmpCodec {
20public:
21
22 /*
23 * Creates an instance of the decoder
24 *
Mike Reedede7bac2017-07-23 15:30:02 -040025 * Called only by SkBmpCodec::MakeFromStream
msarett4ab9d5f2015-08-06 15:34:42 -070026 * There should be no other callers despite this being public
27 *
msarettc30c4182016-04-20 11:53:35 -070028 * @param info contains properties of the encoded data
msarett4ab9d5f2015-08-06 15:34:42 -070029 * @param stream the stream of encoded image data
30 * @param bitsPerPixel the number of bits used to store each pixel
31 * @param numColors the number of colors in the color table
32 * @param bytesPerColor the number of bytes in the stream used to represent
33 each color in the color table
34 * @param offset the offset of the image pixel data from the end of the
35 * headers
36 * @param rowOrder indicates whether rows are ordered top-down or bottom-up
msarett4ab9d5f2015-08-06 15:34:42 -070037 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040038 SkBmpRLECodec(SkEncodedInfo&& info, std::unique_ptr<SkStream>,
msarett5406d6f2015-08-31 06:55:13 -070039 uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050040 uint32_t offset, SkCodec::SkScanlineOrder rowOrder);
msarett4ab9d5f2015-08-06 15:34:42 -070041
scroggoe7fc14b2015-10-02 13:14:46 -070042 int setSampleX(int);
43
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -040044 int fillWidth() const;
45
msarett4ab9d5f2015-08-06 15:34:42 -070046protected:
47
48 Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +000049 size_t dstRowBytes, const Options&,
50 int*) override;
msarett4ab9d5f2015-08-06 15:34:42 -070051
Matt Sarett1b96c6f2016-11-03 16:15:20 -040052 SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +000053 const SkCodec::Options& options) override;
msarett5406d6f2015-08-31 06:55:13 -070054
msarett4ab9d5f2015-08-06 15:34:42 -070055private:
56
57 /*
58 * Creates the color table
halcanary96fcdcc2015-08-27 07:41:13 -070059 * Sets colorCount to the new color count if it is non-nullptr
msarett4ab9d5f2015-08-06 15:34:42 -070060 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000061 bool createColorTable(SkColorType dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070062
63 bool initializeStreamBuffer();
64
65 /*
msarettd0375bc2015-08-12 08:08:56 -070066 * Before signalling kIncompleteInput, we should attempt to load the
67 * stream buffer with additional data.
68 *
69 * @return the number of bytes remaining in the stream buffer after
70 * attempting to read more bytes from the stream
71 */
72 size_t checkForMoreData();
73
74 /*
msarett4ab9d5f2015-08-06 15:34:42 -070075 * Set an RLE pixel using the color table
76 */
77 void setPixel(void* dst, size_t dstRowBytes,
78 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
79 uint8_t index);
80 /*
81 * Set an RLE24 pixel from R, G, B values
82 */
83 void setRGBPixel(void* dst, size_t dstRowBytes,
84 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
85 uint8_t red, uint8_t green, uint8_t blue);
86
msarett9b9497e2016-02-11 13:29:36 -080087 /*
88 * If dst is NULL, this is a signal to skip the rows.
89 */
msarette6dd0042015-10-09 11:07:34 -070090 int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
91 const Options& opts) override;
Matt Sarett1b96c6f2016-11-03 16:15:20 -040092 int decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
msarett4ab9d5f2015-08-06 15:34:42 -070093
msarett9b9497e2016-02-11 13:29:36 -080094 bool skipRows(int count) override;
95
msarette6dd0042015-10-09 11:07:34 -070096 SkSampler* getSampler(bool createIfNecessary) override;
scroggoe7fc14b2015-10-02 13:14:46 -070097
Hal Canary67b39de2016-11-07 11:47:44 -050098 sk_sp<SkColorTable> fColorTable;
benjaminwagner886e5e42015-12-04 08:48:26 -080099 // fNumColors is the number specified in the header, or 0 if not present in the header.
Hal Canary67b39de2016-11-07 11:47:44 -0500100 const uint32_t fNumColors;
101 const uint32_t fBytesPerColor;
102 const uint32_t fOffset;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500103
104 static constexpr size_t kBufferSize = 4096;
105 uint8_t fStreamBuffer[kBufferSize];
106 size_t fBytesBuffered;
107
Hal Canary67b39de2016-11-07 11:47:44 -0500108 uint32_t fCurrRLEByte;
109 int fSampleX;
110 std::unique_ptr<SkSampler> fSampler;
msarett4ab9d5f2015-08-06 15:34:42 -0700111
msarett4946b942016-02-11 08:41:01 -0800112 // Scanline decodes allow the client to ask for a single scanline at a time.
113 // This can be tricky when the RLE encoding instructs the decoder to jump down
114 // multiple lines. This field keeps track of lines that need to be skipped
115 // on subsequent calls to decodeRows().
Hal Canary67b39de2016-11-07 11:47:44 -0500116 int fLinesToSkip;
msarett4946b942016-02-11 08:41:01 -0800117
msarett4ab9d5f2015-08-06 15:34:42 -0700118 typedef SkBmpCodec INHERITED;
119};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500120#endif // SkBmpRLECodec_DEFINED