Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkBmpStandardCodec.cpp
Go to the documentation of this file.
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
9
15#include "include/core/SkSize.h"
19#include "src/base/SkMathPriv.h"
21
22#include <algorithm>
23#include <utility>
24
25/*
26 * Creates an instance of the decoder
27 * Called only by NewFromStream
28 */
29SkBmpStandardCodec::SkBmpStandardCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
30 uint16_t bitsPerPixel, uint32_t numColors,
31 uint32_t bytesPerColor, uint32_t offset,
33 bool isOpaque, bool inIco)
34 : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
35 , fColorTable(nullptr)
36 , fNumColors(numColors)
37 , fBytesPerColor(bytesPerColor)
38 , fOffset(offset)
39 , fSwizzler(nullptr)
40 , fIsOpaque(isOpaque)
41 , fInIco(inIco)
42 , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->dimensions().width(), 1)) : 0)
43{}
44
45/*
46 * Initiates the bitmap decode
47 */
49 void* dst, size_t dstRowBytes,
50 const Options& opts,
51 int* rowsDecoded) {
52 if (opts.fSubset) {
53 // Subsets are not supported.
54 return kUnimplemented;
55 }
56 if (dstInfo.dimensions() != this->dimensions()) {
57 SkCodecPrintf("Error: scaling not supported.\n");
58 return kInvalidScale;
59 }
60
61 Result result = this->prepareToDecode(dstInfo, opts);
62 if (kSuccess != result) {
63 return result;
64 }
65 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
66 if (rows != dstInfo.height()) {
67 *rowsDecoded = rows;
68 return kIncompleteInput;
69 }
70 return kSuccess;
71}
72
73/*
74 * Process the color table for the bmp input
75 */
76 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType) {
77 // Allocate memory for color table
78 uint32_t colorBytes = 0;
79 SkPMColor colorTable[256];
80 if (this->bitsPerPixel() <= 8) {
81 // Inform the caller of the number of colors
82 uint32_t maxColors = 1 << this->bitsPerPixel();
83 // Don't bother reading more than maxColors.
84 const uint32_t numColorsToRead =
85 fNumColors == 0 ? maxColors : std::min(fNumColors, maxColors);
86
87 // Read the color table from the stream
88 colorBytes = numColorsToRead * fBytesPerColor;
89 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
90 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
91 SkCodecPrintf("Error: unable to read color table.\n");
92 return false;
93 }
94
95 SkColorType packColorType = dstColorType;
96 SkAlphaType packAlphaType = dstAlphaType;
97 if (this->colorXform()) {
98 packColorType = kBGRA_8888_SkColorType;
99 packAlphaType = kUnpremul_SkAlphaType;
100 }
101
102 // Choose the proper packing function
103 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
104 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
105
106 // Fill in the color table
107 uint32_t i = 0;
108 for (; i < numColorsToRead; i++) {
109 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
110 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
111 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
112 uint8_t alpha;
113 if (fIsOpaque) {
114 alpha = 0xFF;
115 } else {
116 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
117 }
118 colorTable[i] = packARGB(alpha, red, green, blue);
119 }
120
121 // To avoid segmentation faults on bad pixel data, fill the end of the
122 // color table with black. This is the same the behavior as the
123 // chromium decoder.
124 for (; i < maxColors; i++) {
125 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
126 }
127
128 if (this->colorXform() && !this->xformOnDecode()) {
129 this->applyColorXform(colorTable, colorTable, maxColors);
130 }
131
132 // Set the color table
133 fColorTable.reset(new SkColorPalette(colorTable, maxColors));
134 }
135
136 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
137 // begins. Pixel data always begins immediately after the color table.
138 if (!fInIco) {
139 // Check that we have not read past the pixel array offset
140 if(fOffset < colorBytes) {
141 // This may occur on OS 2.1 and other old versions where the color
142 // table defaults to max size, and the bmp tries to use a smaller
143 // color table. This is invalid, and our decision is to indicate
144 // an error, rather than try to guess the intended size of the
145 // color table.
146 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
147 return false;
148 }
149
150 // After reading the color table, skip to the start of the pixel array
151 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
152 SkCodecPrintf("Error: unable to skip to image data.\n");
153 return false;
154 }
155 }
156
157 // Return true on success
158 return true;
159}
160
162 SkEncodedInfo::Alpha alpha, int bitsPerPixel) {
163 // This is just used for the swizzler, which does not need the width or height.
164 return SkEncodedInfo::Make(0, 0, color, alpha, bitsPerPixel);
165}
166
167SkEncodedInfo SkBmpStandardCodec::swizzlerInfo() const {
168 const auto& info = this->getEncodedInfo();
169 if (fInIco) {
170 if (this->bitsPerPixel() <= 8) {
172 info.alpha(), this->bitsPerPixel());
173 }
174 if (this->bitsPerPixel() == 24) {
177 }
178 }
179
180 return make_info(info.color(), info.alpha(), info.bitsPerComponent());
181}
182
183void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
184 // In the case of bmp-in-icos, we will report BGRA to the client,
185 // since we may be required to apply an alpha mask after the decode.
186 // However, the swizzler needs to know the actual format of the bmp.
187 SkEncodedInfo encodedInfo = this->swizzlerInfo();
188
189 // Get a pointer to the color table if it exists
190 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
191
192 SkImageInfo swizzlerInfo = dstInfo;
193 SkCodec::Options swizzlerOptions = opts;
194 if (this->colorXform()) {
195 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
197 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
198 }
199
200 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
201 }
202
203 fSwizzler = SkSwizzler::Make(encodedInfo, colorPtr, swizzlerInfo, swizzlerOptions);
204 SkASSERT(fSwizzler);
205}
206
208 const SkCodec::Options& options) {
209 if (this->xformOnDecode()) {
210 this->resetXformBuffer(dstInfo.width());
211 }
212
213 // Create the color table if necessary and prepare the stream for decode
214 // Note that if it is non-NULL, inputColorCount will be modified
215 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) {
216 SkCodecPrintf("Error: could not create color table.\n");
218 }
219
220 // Initialize a swizzler
221 this->initializeSwizzler(dstInfo, options);
222 return SkCodec::kSuccess;
223}
224
225/*
226 * Performs the bitmap decoding for standard input format
227 */
228int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
229 const Options& opts) {
230 // Iterate over rows of the image
231 const int height = dstInfo.height();
232 for (int y = 0; y < height; y++) {
233 // Read a row of the input
234 if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) {
235 SkCodecPrintf("Warning: incomplete input stream.\n");
236 return y;
237 }
238
239 // Decode the row in destination format
240 uint32_t row = this->getDstRow(y, dstInfo.height());
241
242 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
243
244 if (this->xformOnDecode()) {
245 SkASSERT(this->colorXform());
246 fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer());
247 this->applyColorXform(dstRow, this->xformBuffer(), fSwizzler->swizzleWidth());
248 } else {
249 fSwizzler->swizzle(dstRow, this->srcBuffer());
250 }
251 }
252
253 if (fInIco && fIsOpaque) {
254 const int startScanline = this->currScanline();
255 if (startScanline < 0) {
256 // We are not performing a scanline decode.
257 // Just decode the entire ICO mask and return.
258 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
259 return height;
260 }
261
262 // In order to perform a scanline ICO decode, we must be able
263 // to skip ahead in the stream in order to apply the AND mask
264 // to the requested scanlines.
265 // We will do this by taking advantage of the fact that
266 // SkIcoCodec always uses a SkMemoryStream as its underlying
267 // representation of the stream.
268 const void* memoryBase = this->stream()->getMemoryBase();
269 SkASSERT(nullptr != memoryBase);
270 SkASSERT(this->stream()->hasLength());
271 SkASSERT(this->stream()->hasPosition());
272
273 const size_t length = this->stream()->getLength();
274 const size_t currPosition = this->stream()->getPosition();
275
276 // Calculate how many bytes we must skip to reach the AND mask.
277 const int remainingScanlines = this->dimensions().height() - startScanline - height;
278 const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() +
279 startScanline * fAndMaskRowBytes;
280 const size_t subStreamStartPosition = currPosition + bytesToSkip;
281 if (subStreamStartPosition >= length) {
282 // FIXME: How can we indicate that this decode was actually incomplete?
283 return height;
284 }
285
286 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
287 // the memory base into a stream in order to safely handle incomplete images
288 // without reading out of bounds memory.
289 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
290 subStreamStartPosition);
291 const size_t subStreamLength = length - subStreamStartPosition;
292 // This call does not transfer ownership of the subStreamMemoryBase.
293 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
294
295 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
296 // indicate the decode was incomplete?
297 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
298 }
299
300 return height;
301}
302
303void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
304 void* dst, size_t dstRowBytes) {
305 // BMP in ICO have transparency, so this cannot be 565. The below code depends
306 // on the output being an SkPMColor.
310
311 // If we are sampling, make sure that we only mask the sampled pixels.
312 // We do not need to worry about sampling in the y-dimension because that
313 // should be handled by SkSampledCodec.
314 const int sampleX = fSwizzler->sampleX();
315 const int sampledWidth = get_scaled_dimension(this->dimensions().width(), sampleX);
316 const int srcStartX = get_start_coord(sampleX);
317
318
319 SkPMColor* dstPtr = (SkPMColor*) dst;
320 for (int y = 0; y < dstInfo.height(); y++) {
321 // The srcBuffer will at least be large enough
322 if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) {
323 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
324 return;
325 }
326
327 auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) {
329 uint64_t* dst64 = (uint64_t*) dstRow;
330 dst64[x] &= bit - 1;
331 } else {
332 uint32_t* dst32 = (uint32_t*) dstRow;
333 dst32[x] &= bit - 1;
334 }
335 };
336
337 int row = this->getDstRow(y, dstInfo.height());
338
339 void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
340
341 int srcX = srcStartX;
342 for (int dstX = 0; dstX < sampledWidth; dstX++) {
343 int quotient;
344 int modulus;
345 SkTDivMod(srcX, 8, &quotient, &modulus);
346 uint32_t shift = 7 - modulus;
347 uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1;
348 applyMask(dstRow, dstX, alphaBit);
349 srcX += sampleX;
350 }
351 }
352}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkColor4f color
kUnpremul_SkAlphaType
static constexpr T SkAlign4(T x)
Definition SkAlign.h:16
SkAlphaType
Definition SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
static uint8_t get_byte(const uint8_t *buffer, uint32_t i)
uint32_t(* PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
static PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType)
static size_t compute_row_bytes(int width, uint32_t bitsPerPixel)
static const SkPMColor * get_color_ptr(SkColorPalette *colorTable)
static int get_scaled_dimension(int srcDimension, int sampleSize)
Definition SkCodecPriv.h:44
#define SkCodecPrintf(...)
Definition SkCodecPriv.h:23
static int get_start_coord(int sampleFactor)
Definition SkCodecPriv.h:57
static SkPMColor SkPackARGB32NoCheck(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
SkColorType
Definition SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
uint32_t SkPMColor
Definition SkColor.h:205
static bool read(SkStream *stream, void *buffer, size_t amount)
static bool skip(SkStream *stream, size_t amount)
void SkTDivMod(In numer, In denom, Out *div, Out *mod)
Definition SkMathPriv.h:38
uint8_t * srcBuffer()
void resetXformBuffer(int count)
Definition SkBmpCodec.h:105
int32_t getDstRow(int32_t y, int32_t height) const
SkCodec::Result prepareToDecode(const SkImageInfo &dstInfo, const SkCodec::Options &options)
size_t srcRowBytes() const
Definition SkBmpCodec.h:89
uint32_t * xformBuffer() const
Definition SkBmpCodec.h:104
static constexpr SkColorType kXformSrcColorType
Definition SkBmpCodec.h:111
uint16_t bitsPerPixel() const
Definition SkBmpCodec.h:87
SkBmpStandardCodec(SkEncodedInfo &&info, std::unique_ptr< SkStream > stream, uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor, uint32_t offset, SkCodec::SkScanlineOrder rowOrder, bool isOpaque, bool inIco)
int decodeRows(const SkImageInfo &dstInfo, void *dst, size_t dstRowBytes, const Options &opts) override
SkCodec::Result onPrepareToDecode(const SkImageInfo &dstInfo, const SkCodec::Options &options) override
Result onGetPixels(const SkImageInfo &dstInfo, void *dst, size_t dstRowBytes, const Options &, int *) override
SkISize dimensions() const
Definition SkCodec.h:230
const SkImageInfo & dstInfo() const
Definition SkCodec.h:878
bool xformOnDecode() const
Definition SkCodec.h:907
SkStream * stream()
Definition SkCodec.h:865
void applyColorXform(void *dst, const void *src, int count) const
Definition SkCodec.cpp:853
@ kNo_ZeroInitialized
Definition SkCodec.h:315
SkScanlineOrder
Definition SkCodec.h:575
const SkEncodedInfo & getEncodedInfo() const
Definition SkCodec.h:788
int currScanline() const
Definition SkCodec.h:888
@ kInvalidScale
Definition SkCodec.h:100
@ kIncompleteInput
Definition SkCodec.h:84
@ kInvalidInput
Definition SkCodec.h:109
@ kUnimplemented
Definition SkCodec.h:123
@ kSuccess
Definition SkCodec.h:80
bool colorXform() const
Definition SkCodec.h:906
const Options & options() const
Definition SkCodec.h:880
virtual size_t getPosition() const
Definition SkStream.h:119
virtual size_t getLength() const
Definition SkStream.h:137
virtual const void * getMemoryBase()
Definition SkStream.h:141
virtual size_t read(void *buffer, size_t size)=0
static std::unique_ptr< SkSwizzler > Make(const SkEncodedInfo &encodedInfo, const SkPMColor *ctable, const SkImageInfo &dstInfo, const SkCodec::Options &, const SkIRect *frame=nullptr)
T * get() const
Definition SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
GAsyncResult * result
size_t length
double y
double x
Definition ref_ptr.h:256
static SkPath make_info()
Definition pathfill.cpp:172
int32_t height
int32_t width
Point offset
const SkIRect * fSubset
Definition SkCodec.h:347
ZeroInitialized fZeroInitialized
Definition SkCodec.h:329
static SkEncodedInfo Make(int width, int height, Color color, Alpha alpha, int bitsPerComponent)
constexpr int32_t height() const
Definition SkSize.h:37
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
SkISize dimensions() const
int width() const
SkAlphaType alphaType() const
SkColorType colorType() const
int height() const
SkImageInfo makeColorType(SkColorType newColorType) const