blob: 8ea3a86dba8dab8a90c7ce2101d04675813d56d2 [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 */
7
8#include "SkBmpCodec.h"
9#include "SkColorTable.h"
10#include "SkImageInfo.h"
scroggoe7fc14b2015-10-02 13:14:46 -070011#include "SkSampler.h"
msarett4ab9d5f2015-08-06 15:34:42 -070012#include "SkTypes.h"
13
14/*
15 * This class implements the decoding for bmp images that use an RLE encoding
16 */
17class SkBmpRLECodec : public SkBmpCodec {
18public:
19
20 /*
21 * Creates an instance of the decoder
22 *
23 * Called only by SkBmpCodec::NewFromStream
24 * There should be no other callers despite this being public
25 *
msarettc30c4182016-04-20 11:53:35 -070026 * @param info contains properties of the encoded data
msarett4ab9d5f2015-08-06 15:34:42 -070027 * @param stream the stream of encoded image data
28 * @param bitsPerPixel the number of bits used to store each pixel
29 * @param numColors the number of colors in the color table
30 * @param bytesPerColor the number of bytes in the stream used to represent
31 each color in the color table
32 * @param offset the offset of the image pixel data from the end of the
33 * headers
34 * @param rowOrder indicates whether rows are ordered top-down or bottom-up
msarett4ab9d5f2015-08-06 15:34:42 -070035 */
msarettc30c4182016-04-20 11:53:35 -070036 SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
msarett5406d6f2015-08-31 06:55:13 -070037 uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050038 uint32_t offset, SkCodec::SkScanlineOrder rowOrder);
msarett4ab9d5f2015-08-06 15:34:42 -070039
scroggoe7fc14b2015-10-02 13:14:46 -070040 int setSampleX(int);
41
msarett4ab9d5f2015-08-06 15:34:42 -070042protected:
43
44 Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
45 size_t dstRowBytes, const Options&, SkPMColor*,
msarette6dd0042015-10-09 11:07:34 -070046 int*, int*) override;
msarett4ab9d5f2015-08-06 15:34:42 -070047
Matt Sarett1b96c6f2016-11-03 16:15:20 -040048 SkCodec::Result onPrepareToDecode(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -070049 const SkCodec::Options& options, SkPMColor inputColorPtr[],
50 int* inputColorCount) override;
51
msarett4ab9d5f2015-08-06 15:34:42 -070052private:
53
54 /*
55 * Creates the color table
halcanary96fcdcc2015-08-27 07:41:13 -070056 * Sets colorCount to the new color count if it is non-nullptr
msarett4ab9d5f2015-08-06 15:34:42 -070057 */
msarett34e0ec42016-04-22 16:27:24 -070058 bool createColorTable(SkColorType dstColorType, int* colorCount);
msarett4ab9d5f2015-08-06 15:34:42 -070059
60 bool initializeStreamBuffer();
61
62 /*
msarettd0375bc2015-08-12 08:08:56 -070063 * Before signalling kIncompleteInput, we should attempt to load the
64 * stream buffer with additional data.
65 *
66 * @return the number of bytes remaining in the stream buffer after
67 * attempting to read more bytes from the stream
68 */
69 size_t checkForMoreData();
70
71 /*
msarett4ab9d5f2015-08-06 15:34:42 -070072 * Set an RLE pixel using the color table
73 */
74 void setPixel(void* dst, size_t dstRowBytes,
75 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
76 uint8_t index);
77 /*
78 * Set an RLE24 pixel from R, G, B values
79 */
80 void setRGBPixel(void* dst, size_t dstRowBytes,
81 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
82 uint8_t red, uint8_t green, uint8_t blue);
83
msarett9b9497e2016-02-11 13:29:36 -080084 /*
85 * If dst is NULL, this is a signal to skip the rows.
86 */
msarette6dd0042015-10-09 11:07:34 -070087 int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
88 const Options& opts) override;
Matt Sarett1b96c6f2016-11-03 16:15:20 -040089 int decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
msarett4ab9d5f2015-08-06 15:34:42 -070090
msarett9b9497e2016-02-11 13:29:36 -080091 bool skipRows(int count) override;
92
msarette6dd0042015-10-09 11:07:34 -070093 SkSampler* getSampler(bool createIfNecessary) override;
scroggoe7fc14b2015-10-02 13:14:46 -070094
Hal Canary67b39de2016-11-07 11:47:44 -050095 sk_sp<SkColorTable> fColorTable;
benjaminwagner886e5e42015-12-04 08:48:26 -080096 // fNumColors is the number specified in the header, or 0 if not present in the header.
Hal Canary67b39de2016-11-07 11:47:44 -050097 const uint32_t fNumColors;
98 const uint32_t fBytesPerColor;
99 const uint32_t fOffset;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500100
101 static constexpr size_t kBufferSize = 4096;
102 uint8_t fStreamBuffer[kBufferSize];
103 size_t fBytesBuffered;
104
Hal Canary67b39de2016-11-07 11:47:44 -0500105 uint32_t fCurrRLEByte;
106 int fSampleX;
107 std::unique_ptr<SkSampler> fSampler;
msarett4ab9d5f2015-08-06 15:34:42 -0700108
msarett4946b942016-02-11 08:41:01 -0800109 // Scanline decodes allow the client to ask for a single scanline at a time.
110 // This can be tricky when the RLE encoding instructs the decoder to jump down
111 // multiple lines. This field keeps track of lines that need to be skipped
112 // on subsequent calls to decodeRows().
Hal Canary67b39de2016-11-07 11:47:44 -0500113 int fLinesToSkip;
msarett4946b942016-02-11 08:41:01 -0800114
msarett4ab9d5f2015-08-06 15:34:42 -0700115 typedef SkBmpCodec INHERITED;
116};