msarett | 4ab9d5f | 2015-08-06 15:34:42 -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 | */ |
| 7 | |
| 8 | #include "SkBmpCodec.h" |
| 9 | #include "SkColorTable.h" |
| 10 | #include "SkImageInfo.h" |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 11 | #include "SkSampler.h" |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
| 14 | /* |
| 15 | * This class implements the decoding for bmp images that use an RLE encoding |
| 16 | */ |
| 17 | class SkBmpRLECodec : public SkBmpCodec { |
| 18 | public: |
| 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 | * |
| 26 | * @param srcInfo contains the source width and height |
| 27 | * @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 |
| 35 | * @param RLEBytes indicates the amount of data left in the stream |
| 36 | * after decoding the headers |
| 37 | */ |
| 38 | SkBmpRLECodec(const SkImageInfo& srcInfo, SkStream* stream, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 39 | uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 40 | uint32_t offset, SkCodec::SkScanlineOrder rowOrder, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 41 | size_t RLEBytes); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 42 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 43 | int setSampleX(int); |
| 44 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 45 | protected: |
| 46 | |
| 47 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, |
| 48 | size_t dstRowBytes, const Options&, SkPMColor*, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 49 | int*, int*) override; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 50 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 51 | SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo, |
| 52 | const SkCodec::Options& options, SkPMColor inputColorPtr[], |
| 53 | int* inputColorCount) override; |
| 54 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 55 | private: |
| 56 | |
| 57 | /* |
| 58 | * Creates the color table |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | * Sets colorCount to the new color count if it is non-nullptr |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 60 | */ |
| 61 | bool createColorTable(int* colorCount); |
| 62 | |
| 63 | bool initializeStreamBuffer(); |
| 64 | |
| 65 | /* |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 66 | * 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 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 75 | * 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 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 87 | int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
| 88 | const Options& opts) override; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 89 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 90 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 91 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 92 | SkAutoTUnref<SkColorTable> fColorTable; // owned |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame^] | 93 | // fNumColors is the number specified in the header, or 0 if not present in the header. |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 94 | const uint32_t fNumColors; |
| 95 | const uint32_t fBytesPerColor; |
| 96 | const uint32_t fOffset; |
| 97 | SkAutoTDeleteArray<uint8_t> fStreamBuffer; |
| 98 | size_t fRLEBytes; |
| 99 | uint32_t fCurrRLEByte; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 100 | int fSampleX; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 101 | SkAutoTDelete<SkSampler> fSampler; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 102 | |
| 103 | typedef SkBmpCodec INHERITED; |
| 104 | }; |