blob: aa5b06127329cef971d7365d180a169434bb4093 [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"
11#include "SkTypes.h"
12
13/*
14 * This class implements the decoding for bmp images that use an RLE encoding
15 */
16class SkBmpRLECodec : public SkBmpCodec {
17public:
18
19 /*
20 * Creates an instance of the decoder
21 *
22 * Called only by SkBmpCodec::NewFromStream
23 * There should be no other callers despite this being public
24 *
25 * @param srcInfo contains the source width and height
26 * @param stream the stream of encoded image data
27 * @param bitsPerPixel the number of bits used to store each pixel
28 * @param numColors the number of colors in the color table
29 * @param bytesPerColor the number of bytes in the stream used to represent
30 each color in the color table
31 * @param offset the offset of the image pixel data from the end of the
32 * headers
33 * @param rowOrder indicates whether rows are ordered top-down or bottom-up
34 * @param RLEBytes indicates the amount of data left in the stream
35 * after decoding the headers
36 */
37 SkBmpRLECodec(const SkImageInfo& srcInfo, SkStream* stream,
msarett5406d6f2015-08-31 06:55:13 -070038 uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
39 uint32_t offset, SkScanlineDecoder::SkScanlineOrder rowOrder,
40 size_t RLEBytes);
msarett4ab9d5f2015-08-06 15:34:42 -070041
42protected:
43
44 Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
45 size_t dstRowBytes, const Options&, SkPMColor*,
46 int*) override;
47
msarett5406d6f2015-08-31 06:55:13 -070048 SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo,
49 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 */
58 bool createColorTable(int* colorCount);
59
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
msarett5406d6f2015-08-31 06:55:13 -070084 Result decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
85 const Options& opts) override;
msarett4ab9d5f2015-08-06 15:34:42 -070086
87 SkAutoTUnref<SkColorTable> fColorTable; // owned
88 const uint32_t fNumColors;
89 const uint32_t fBytesPerColor;
90 const uint32_t fOffset;
91 SkAutoTDeleteArray<uint8_t> fStreamBuffer;
92 size_t fRLEBytes;
93 uint32_t fCurrRLEByte;
msarett5406d6f2015-08-31 06:55:13 -070094 int fSampleX;
msarett4ab9d5f2015-08-06 15:34:42 -070095
96 typedef SkBmpCodec INHERITED;
97};