blob: 70e97a78332bcedfde88760453540c47c2ccdf31 [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 */
Mike Reedede7bac2017-07-23 15:30:02 -040038 SkBmpRLECodec(int width, int height, const 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
msarett4ab9d5f2015-08-06 15:34:42 -070044protected:
45
46 Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +000047 size_t dstRowBytes, const Options&,
48 int*) override;
msarett4ab9d5f2015-08-06 15:34:42 -070049
Matt Sarett1b96c6f2016-11-03 16:15:20 -040050 SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +000051 const SkCodec::Options& options) override;
msarett5406d6f2015-08-31 06:55:13 -070052
msarett4ab9d5f2015-08-06 15:34:42 -070053private:
54
55 /*
56 * Creates the color table
halcanary96fcdcc2015-08-27 07:41:13 -070057 * Sets colorCount to the new color count if it is non-nullptr
msarett4ab9d5f2015-08-06 15:34:42 -070058 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000059 bool createColorTable(SkColorType dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070060
61 bool initializeStreamBuffer();
62
63 /*
msarettd0375bc2015-08-12 08:08:56 -070064 * Before signalling kIncompleteInput, we should attempt to load the
65 * stream buffer with additional data.
66 *
67 * @return the number of bytes remaining in the stream buffer after
68 * attempting to read more bytes from the stream
69 */
70 size_t checkForMoreData();
71
72 /*
msarett4ab9d5f2015-08-06 15:34:42 -070073 * Set an RLE pixel using the color table
74 */
75 void setPixel(void* dst, size_t dstRowBytes,
76 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
77 uint8_t index);
78 /*
79 * Set an RLE24 pixel from R, G, B values
80 */
81 void setRGBPixel(void* dst, size_t dstRowBytes,
82 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
83 uint8_t red, uint8_t green, uint8_t blue);
84
msarett9b9497e2016-02-11 13:29:36 -080085 /*
86 * If dst is NULL, this is a signal to skip the rows.
87 */
msarette6dd0042015-10-09 11:07:34 -070088 int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
89 const Options& opts) override;
Matt Sarett1b96c6f2016-11-03 16:15:20 -040090 int decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
msarett4ab9d5f2015-08-06 15:34:42 -070091
msarett9b9497e2016-02-11 13:29:36 -080092 bool skipRows(int count) override;
93
msarette6dd0042015-10-09 11:07:34 -070094 SkSampler* getSampler(bool createIfNecessary) override;
scroggoe7fc14b2015-10-02 13:14:46 -070095
Hal Canary67b39de2016-11-07 11:47:44 -050096 sk_sp<SkColorTable> fColorTable;
benjaminwagner886e5e42015-12-04 08:48:26 -080097 // fNumColors is the number specified in the header, or 0 if not present in the header.
Hal Canary67b39de2016-11-07 11:47:44 -050098 const uint32_t fNumColors;
99 const uint32_t fBytesPerColor;
100 const uint32_t fOffset;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500101
102 static constexpr size_t kBufferSize = 4096;
103 uint8_t fStreamBuffer[kBufferSize];
104 size_t fBytesBuffered;
105
Hal Canary67b39de2016-11-07 11:47:44 -0500106 uint32_t fCurrRLEByte;
107 int fSampleX;
108 std::unique_ptr<SkSampler> fSampler;
msarett4ab9d5f2015-08-06 15:34:42 -0700109
msarett4946b942016-02-11 08:41:01 -0800110 // Scanline decodes allow the client to ask for a single scanline at a time.
111 // This can be tricky when the RLE encoding instructs the decoder to jump down
112 // multiple lines. This field keeps track of lines that need to be skipped
113 // on subsequent calls to decodeRows().
Hal Canary67b39de2016-11-07 11:47:44 -0500114 int fLinesToSkip;
msarett4946b942016-02-11 08:41:01 -0800115
msarett4ab9d5f2015-08-06 15:34:42 -0700116 typedef SkBmpCodec INHERITED;
117};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500118#endif // SkBmpRLECodec_DEFINED