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" |
| 11 | #include "SkSwizzler.h" |
| 12 | #include "SkTypes.h" |
| 13 | |
| 14 | /* |
| 15 | * This class implements the decoding for bmp images that use "standard" modes, |
| 16 | * which essentially means they do not contain bit masks or RLE codes. |
| 17 | */ |
| 18 | class SkBmpStandardCodec : public SkBmpCodec { |
| 19 | public: |
| 20 | |
| 21 | /* |
| 22 | * Creates an instance of the decoder |
| 23 | * |
| 24 | * Called only by SkBmpCodec::NewFromStream |
| 25 | * There should be no other callers despite this being public |
| 26 | * |
| 27 | * @param srcInfo contains the source width and height |
| 28 | * @param stream the stream of encoded image data |
| 29 | * @param bitsPerPixel the number of bits used to store each pixel |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 30 | * @param numColors the number of colors in the color table |
| 31 | * @param bytesPerColor the number of bytes in the stream used to represent |
| 32 | each color in the color table |
| 33 | * @param offset the offset of the image pixel data from the end of the |
| 34 | * headers |
| 35 | * @param rowOrder indicates whether rows are ordered top-down or bottom-up |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame^] | 36 | * @param isOpaque indicates if the bmp itself is opaque (before applying |
| 37 | * the icp mask, if there is one) |
| 38 | * @param inIco indicates if the bmp is embedded in an ico file |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 39 | */ |
| 40 | SkBmpStandardCodec(const SkImageInfo& srcInfo, SkStream* stream, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 41 | uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor, |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame^] | 42 | uint32_t offset, SkCodec::SkScanlineOrder rowOrder, bool isOpaque, |
| 43 | bool inIco); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 44 | |
| 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 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 51 | bool onInIco() const override { |
| 52 | return fInIco; |
| 53 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 54 | |
| 55 | SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo, |
| 56 | const SkCodec::Options& options, SkPMColor inputColorPtr[], |
| 57 | int* inputColorCount) override; |
| 58 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 59 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 60 | uint32_t onGetFillValue(SkColorType) const override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 61 | |
| 62 | SkSampler* getSampler(bool createIfNecessary) override { |
| 63 | SkASSERT(fSwizzler); |
| 64 | return fSwizzler; |
| 65 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 66 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 67 | private: |
| 68 | |
| 69 | /* |
| 70 | * Creates the color table |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | * Sets colorCount to the new color count if it is non-nullptr |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 72 | */ |
| 73 | bool createColorTable(SkAlphaType alphaType, int* colorCount); |
| 74 | |
| 75 | bool initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts); |
| 76 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 77 | int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
| 78 | const Options& opts) override; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 79 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 80 | /* |
| 81 | * @param stream This may be a pointer to the stream owned by the parent SkCodec |
| 82 | * or a sub-stream of the stream owned by the parent SkCodec. |
| 83 | * Either way, this stream is unowned. |
| 84 | */ |
| 85 | void decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 86 | |
| 87 | SkAutoTUnref<SkColorTable> fColorTable; // owned |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 88 | // 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] | 89 | const uint32_t fNumColors; |
| 90 | const uint32_t fBytesPerColor; |
| 91 | const uint32_t fOffset; |
| 92 | SkAutoTDelete<SkSwizzler> fSwizzler; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 93 | const size_t fSrcRowBytes; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 94 | SkAutoTDeleteArray<uint8_t> fSrcBuffer; |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame^] | 95 | const bool fIsOpaque; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 96 | const bool fInIco; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 97 | const size_t fAndMaskRowBytes; // only used for fInIco decodes |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 98 | |
| 99 | typedef SkBmpCodec INHERITED; |
| 100 | }; |