2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkColorTable.h"
10 #include "SkImageInfo.h"
11 #include "SkMaskSwizzler.h"
13 #include "SkSwizzler.h"
16 // TODO: rename SkCodec_libbmp files to SkBmpCodec
17 // TODO: define a wrapper for SkDebugf that doesn't always print
20 * This class implements the decoding for bmp images
23 class SkBmpCodec : public SkCodec {
28 * Describes if rows of the input start at the top or bottom of the image
38 * Checks the start of the stream to see if the image is a bmp
41 static bool IsBmp(SkStream*);
45 * Assumes IsBmp was called and returned true
46 * Creates a bmp decoder
47 * Reads enough of the stream to determine the image format
50 static SkCodec* NewFromStream(SkStream*);
54 * Creates a bmp decoder for a bmp embedded in ico
55 * Reads enough of the stream to determine the image format
58 static SkCodec* NewFromIco(SkStream*);
64 * Initiates the bmp decode
67 virtual Result onGetPixels(const SkImageInfo& dstInfo, void* dst,
68 size_t dstRowBytes, const Options&, SkPMColor*,
71 SkEncodedFormat onGetEncodedFormat() const SK_OVERRIDE { return kBMP_SkEncodedFormat; }
77 * Used to define the input format of the bmp
80 enum BitmapInputFormat {
81 kStandard_BitmapInputFormat,
82 kRLE_BitmapInputFormat,
83 kBitMask_BitmapInputFormat,
84 kUnknown_BitmapInputFormat
89 * Creates the color table
92 bool createColorTable(SkAlphaType alphaType);
96 * Creates a bmp decoder
97 * Reads enough of the stream to determine the image format
100 static SkCodec* NewFromStream(SkStream*, bool isIco);
104 * Performs the bitmap decoding for bit masks input format
107 Result decodeMask(const SkImageInfo& dstInfo, void* dst,
112 * Set an RLE pixel using the color table
115 void setRLEPixel(SkPMColor* dst, size_t dstRowBytes,
116 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
120 * Set an RLE24 pixel from R, G, B values
123 void setRLE24Pixel(SkPMColor* dst, size_t dstRowBytes,
124 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
125 uint8_t red, uint8_t green, uint8_t blue);
129 * Performs the bitmap decoding for RLE input format
132 Result decodeRLE(const SkImageInfo& dstInfo, void* dst,
137 * Performs the bitmap decoding for standard input format
140 Result decode(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes);
144 * Creates an instance of the decoder
145 * Called only by NewFromStream
147 * @param srcInfo contains the source width and height
148 * @param stream the stream of image data
149 * @param bitsPerPixel the number of bits used to store each pixel
150 * @param format the format of the bmp file
151 * @param masks optional color masks for certain bmp formats, passes
152 ownership to SkBmpCodec
153 * @param numColors the number of colors in the color table
154 * @param bytesPerColor the number of bytes in the stream used to represent
155 each color in the color table
156 * @param offset the offset of the image pixel data from the end of the
158 * @param rowOrder indicates whether rows are ordered top-down or bottom-up
159 * @param RLEBytes used only for RLE decodes, as we must decode all
160 * of the data at once rather than row by row
161 * it indicates the amount of data left in the stream
162 * after decoding the headers
165 SkBmpCodec(const SkImageInfo& srcInfo, SkStream* stream,
166 uint16_t bitsPerPixel, BitmapInputFormat format,
167 SkMasks* masks, uint32_t numColors, uint32_t bytesPerColor,
168 uint32_t offset, RowOrder rowOrder, size_t RLEBytes,
172 const uint16_t fBitsPerPixel;
173 const BitmapInputFormat fInputFormat;
174 SkAutoTDelete<SkMasks> fMasks; // owned
175 SkAutoTDelete<SkColorTable> fColorTable; // owned
177 const uint32_t fBytesPerColor;
178 const uint32_t fOffset;
179 const RowOrder fRowOrder;
180 const size_t fRLEBytes;
183 typedef SkCodec INHERITED;