blob: b5f56f0a2aef048096c80fd3e4e906394abac09b [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 "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 */
18class SkBmpStandardCodec : public SkBmpCodec {
19public:
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
30 * @param format the format of the bmp file
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
37 */
38 SkBmpStandardCodec(const SkImageInfo& srcInfo, SkStream* stream,
msarett5406d6f2015-08-31 06:55:13 -070039 uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
scroggo46c57472015-09-30 08:57:13 -070040 uint32_t offset, SkCodec::SkScanlineOrder rowOrder,
msarett5406d6f2015-08-31 06:55:13 -070041 bool isIco);
msarett4ab9d5f2015-08-06 15:34:42 -070042
43protected:
44
45 Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
46 size_t dstRowBytes, const Options&, SkPMColor*,
msarette6dd0042015-10-09 11:07:34 -070047 int*, int*) override;
msarett4ab9d5f2015-08-06 15:34:42 -070048
scroggob427db12015-08-12 07:24:13 -070049 bool onInIco() const override {
50 return fInIco;
51 }
msarett5406d6f2015-08-31 06:55:13 -070052
53 SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo,
54 const SkCodec::Options& options, SkPMColor inputColorPtr[],
55 int* inputColorCount) override;
56
msarette6dd0042015-10-09 11:07:34 -070057
scroggoc5560be2016-02-03 09:42:42 -080058 uint32_t onGetFillValue(SkColorType) const override;
msarette6dd0042015-10-09 11:07:34 -070059
60 SkSampler* getSampler(bool createIfNecessary) override {
61 SkASSERT(fSwizzler);
62 return fSwizzler;
63 }
scroggoe7fc14b2015-10-02 13:14:46 -070064
msarett4ab9d5f2015-08-06 15:34:42 -070065private:
66
67 /*
68 * Creates the color table
halcanary96fcdcc2015-08-27 07:41:13 -070069 * Sets colorCount to the new color count if it is non-nullptr
msarett4ab9d5f2015-08-06 15:34:42 -070070 */
71 bool createColorTable(SkAlphaType alphaType, int* colorCount);
72
73 bool initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts);
74
msarette6dd0042015-10-09 11:07:34 -070075 int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
76 const Options& opts) override;
msarett5406d6f2015-08-31 06:55:13 -070077
msarettbe8216a2015-12-04 08:00:50 -080078 /*
79 * @param stream This may be a pointer to the stream owned by the parent SkCodec
80 * or a sub-stream of the stream owned by the parent SkCodec.
81 * Either way, this stream is unowned.
82 */
83 void decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
msarett4ab9d5f2015-08-06 15:34:42 -070084
85 SkAutoTUnref<SkColorTable> fColorTable; // owned
benjaminwagner886e5e42015-12-04 08:48:26 -080086 // fNumColors is the number specified in the header, or 0 if not present in the header.
msarett4ab9d5f2015-08-06 15:34:42 -070087 const uint32_t fNumColors;
88 const uint32_t fBytesPerColor;
89 const uint32_t fOffset;
90 SkAutoTDelete<SkSwizzler> fSwizzler;
msarett5406d6f2015-08-31 06:55:13 -070091 const size_t fSrcRowBytes;
msarett4ab9d5f2015-08-06 15:34:42 -070092 SkAutoTDeleteArray<uint8_t> fSrcBuffer;
93 const bool fInIco;
msarettbe8216a2015-12-04 08:00:50 -080094 const size_t fAndMaskRowBytes; // only used for fInIco decodes
msarett4ab9d5f2015-08-06 15:34:42 -070095
96 typedef SkBmpCodec INHERITED;
97};