blob: 3e224c13a94b6e6363a4beb077d7100a56dd67e2 [file] [log] [blame]
msarett74114382015-03-16 11:55:18 -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
msarettb46e5e22015-07-30 11:36:40 -07008#include "SkBmpCodec.h"
msarett4ab9d5f2015-08-06 15:34:42 -07009#include "SkBmpMaskCodec.h"
10#include "SkBmpRLECodec.h"
11#include "SkBmpStandardCodec.h"
msarett74114382015-03-16 11:55:18 -070012#include "SkCodecPriv.h"
13#include "SkColorPriv.h"
14#include "SkStream.h"
15
16/*
msarett74114382015-03-16 11:55:18 -070017 * Defines the version and type of the second bitmap header
msarett74114382015-03-16 11:55:18 -070018 */
msarett4ab9d5f2015-08-06 15:34:42 -070019enum BmpHeaderType {
20 kInfoV1_BmpHeaderType,
21 kInfoV2_BmpHeaderType,
22 kInfoV3_BmpHeaderType,
23 kInfoV4_BmpHeaderType,
24 kInfoV5_BmpHeaderType,
25 kOS2V1_BmpHeaderType,
26 kOS2VX_BmpHeaderType,
27 kUnknown_BmpHeaderType
msarett74114382015-03-16 11:55:18 -070028};
29
30/*
msarett74114382015-03-16 11:55:18 -070031 * Possible bitmap compression types
msarett74114382015-03-16 11:55:18 -070032 */
msarett4ab9d5f2015-08-06 15:34:42 -070033enum BmpCompressionMethod {
34 kNone_BmpCompressionMethod = 0,
35 k8BitRLE_BmpCompressionMethod = 1,
36 k4BitRLE_BmpCompressionMethod = 2,
37 kBitMasks_BmpCompressionMethod = 3,
38 kJpeg_BmpCompressionMethod = 4,
39 kPng_BmpCompressionMethod = 5,
40 kAlphaBitMasks_BmpCompressionMethod = 6,
41 kCMYK_BmpCompressionMethod = 11,
42 kCMYK8BitRLE_BmpCompressionMethod = 12,
43 kCMYK4BitRLE_BmpCompressionMethod = 13
msarett74114382015-03-16 11:55:18 -070044};
45
46/*
msarett4ab9d5f2015-08-06 15:34:42 -070047 * Used to define the input format of the bmp
48 */
49enum BmpInputFormat {
50 kStandard_BmpInputFormat,
51 kRLE_BmpInputFormat,
52 kBitMask_BmpInputFormat,
53 kUnknown_BmpInputFormat
54};
55
56/*
msarett74114382015-03-16 11:55:18 -070057 * Checks the start of the stream to see if the image is a bitmap
msarett74114382015-03-16 11:55:18 -070058 */
scroggodb30be22015-12-08 18:54:13 -080059bool SkBmpCodec::IsBmp(const void* buffer, size_t bytesRead) {
msarett74114382015-03-16 11:55:18 -070060 // TODO: Support "IC", "PT", "CI", "CP", "BA"
msarett74114382015-03-16 11:55:18 -070061 const char bmpSig[] = { 'B', 'M' };
scroggodb30be22015-12-08 18:54:13 -080062 return bytesRead >= sizeof(bmpSig) && !memcmp(buffer, bmpSig, sizeof(bmpSig));
msarett74114382015-03-16 11:55:18 -070063}
64
65/*
msarett74114382015-03-16 11:55:18 -070066 * Assumes IsBmp was called and returned true
msarett9bde9182015-03-25 05:27:48 -070067 * Creates a bmp decoder
msarett74114382015-03-16 11:55:18 -070068 * Reads enough of the stream to determine the image format
msarett74114382015-03-16 11:55:18 -070069 */
70SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) {
msarett9bde9182015-03-25 05:27:48 -070071 return SkBmpCodec::NewFromStream(stream, false);
72}
73
74/*
msarett9bde9182015-03-25 05:27:48 -070075 * Creates a bmp decoder for a bmp embedded in ico
76 * Reads enough of the stream to determine the image format
msarett9bde9182015-03-25 05:27:48 -070077 */
78SkCodec* SkBmpCodec::NewFromIco(SkStream* stream) {
79 return SkBmpCodec::NewFromStream(stream, true);
80}
81
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -050082// Header size constants
83static const uint32_t kBmpHeaderBytes = 14;
84static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4;
85static const uint32_t kBmpOS2V1Bytes = 12;
86static const uint32_t kBmpOS2V2Bytes = 64;
87static const uint32_t kBmpInfoBaseBytes = 16;
88static const uint32_t kBmpInfoV1Bytes = 40;
89static const uint32_t kBmpInfoV2Bytes = 52;
90static const uint32_t kBmpInfoV3Bytes = 56;
91static const uint32_t kBmpInfoV4Bytes = 108;
92static const uint32_t kBmpInfoV5Bytes = 124;
93static const uint32_t kBmpMaskBytes = 12;
94
95static BmpHeaderType get_header_type(size_t infoBytes) {
96 if (infoBytes >= kBmpInfoBaseBytes) {
97 // Check the version of the header
98 switch (infoBytes) {
99 case kBmpInfoV1Bytes:
100 return kInfoV1_BmpHeaderType;
101 case kBmpInfoV2Bytes:
102 return kInfoV2_BmpHeaderType;
103 case kBmpInfoV3Bytes:
104 return kInfoV3_BmpHeaderType;
105 case kBmpInfoV4Bytes:
106 return kInfoV4_BmpHeaderType;
107 case kBmpInfoV5Bytes:
108 return kInfoV5_BmpHeaderType;
109 case 16:
110 case 20:
111 case 24:
112 case 28:
113 case 32:
114 case 36:
115 case 42:
116 case 46:
117 case 48:
118 case 60:
119 case kBmpOS2V2Bytes:
120 return kOS2VX_BmpHeaderType;
121 default:
122 SkCodecPrintf("Error: unknown bmp header format.\n");
123 return kUnknown_BmpHeaderType;
124 }
125 } if (infoBytes >= kBmpOS2V1Bytes) {
126 // The OS2V1 is treated separately because it has a unique format
127 return kOS2V1_BmpHeaderType;
128 } else {
129 // There are no valid bmp headers
130 SkCodecPrintf("Error: second bitmap header size is invalid.\n");
131 return kUnknown_BmpHeaderType;
132 }
133}
134
msarett9bde9182015-03-25 05:27:48 -0700135/*
scroggo79e378d2015-04-01 07:39:40 -0700136 * Read enough of the stream to initialize the SkBmpCodec. Returns a bool
137 * representing success or failure. If it returned true, and codecOut was
halcanary96fcdcc2015-08-27 07:41:13 -0700138 * not nullptr, it will be set to a new SkBmpCodec.
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400139 * If codecOut is set to a new SkCodec, it will take ownership of the stream.
140 * Otherwise, the stream will not be deleted.
msarett9bde9182015-03-25 05:27:48 -0700141 */
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400142bool SkBmpCodec::ReadHeader(SkStream* stream, bool inIco,
143 std::unique_ptr<SkCodec>* codecOut) {
tomhudson7aa846c2015-03-24 13:47:41 -0700144 // The total bytes in the bmp file
msarett9bde9182015-03-25 05:27:48 -0700145 // We only need to use this value for RLE decoding, so we will only
146 // check that it is valid in the RLE case.
147 uint32_t totalBytes;
tomhudson7aa846c2015-03-24 13:47:41 -0700148 // The offset from the start of the file where the pixel data begins
msarett9bde9182015-03-25 05:27:48 -0700149 uint32_t offset;
150 // The size of the second (info) header in bytes
151 uint32_t infoBytes;
152
153 // Bmps embedded in Icos skip the first Bmp header
msarett4ab9d5f2015-08-06 15:34:42 -0700154 if (!inIco) {
msarett9bde9182015-03-25 05:27:48 -0700155 // Read the first header and the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400156 uint8_t hBuffer[kBmpHeaderBytesPlusFour];
157 if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) !=
msarett9bde9182015-03-25 05:27:48 -0700158 kBmpHeaderBytesPlusFour) {
scroggo230d4ac2015-03-26 07:15:55 -0700159 SkCodecPrintf("Error: unable to read first bitmap header.\n");
scroggo79e378d2015-04-01 07:39:40 -0700160 return false;
msarett9bde9182015-03-25 05:27:48 -0700161 }
162
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400163 totalBytes = get_int(hBuffer, 2);
164 offset = get_int(hBuffer, 10);
msarett9bde9182015-03-25 05:27:48 -0700165 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700166 SkCodecPrintf("Error: invalid starting location for pixel data\n");
scroggo79e378d2015-04-01 07:39:40 -0700167 return false;
msarett9bde9182015-03-25 05:27:48 -0700168 }
169
170 // The size of the second (info) header in bytes
171 // The size is the first field of the second header, so we have already
172 // read the first four infoBytes.
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400173 infoBytes = get_int(hBuffer, 14);
msarett9bde9182015-03-25 05:27:48 -0700174 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700175 SkCodecPrintf("Error: invalid second header size.\n");
scroggo79e378d2015-04-01 07:39:40 -0700176 return false;
msarett9bde9182015-03-25 05:27:48 -0700177 }
178 } else {
179 // This value is only used by RLE compression. Bmp in Ico files do not
180 // use RLE. If the compression field is incorrectly signaled as RLE,
181 // we will catch this and signal an error below.
182 totalBytes = 0;
183
184 // Bmps in Ico cannot specify an offset. We will always assume that
185 // pixel data begins immediately after the color table. This value
186 // will be corrected below.
187 offset = 0;
188
189 // Read the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400190 uint8_t hBuffer[4];
191 if (stream->read(hBuffer, 4) != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700192 SkCodecPrintf("Error: unable to read size of second bitmap header.\n");
scroggo79e378d2015-04-01 07:39:40 -0700193 return false;
msarett9bde9182015-03-25 05:27:48 -0700194 }
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400195 infoBytes = get_int(hBuffer, 0);
msarett9bde9182015-03-25 05:27:48 -0700196 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700197 SkCodecPrintf("Error: invalid second header size.\n");
scroggo79e378d2015-04-01 07:39:40 -0700198 return false;
msarett9bde9182015-03-25 05:27:48 -0700199 }
tomhudson7aa846c2015-03-24 13:47:41 -0700200 }
201
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500202 // Determine image information depending on second header format
203 const BmpHeaderType headerType = get_header_type(infoBytes);
204 if (kUnknown_BmpHeaderType == headerType) {
205 return false;
206 }
207
msarett9bde9182015-03-25 05:27:48 -0700208 // We already read the first four bytes of the info header to get the size
msarett74114382015-03-16 11:55:18 -0700209 const uint32_t infoBytesRemaining = infoBytes - 4;
msarett74114382015-03-16 11:55:18 -0700210
211 // Read the second header
Ben Wagner7ecc5962016-11-02 17:07:33 -0400212 std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]);
msarett74114382015-03-16 11:55:18 -0700213 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) {
scroggo230d4ac2015-03-26 07:15:55 -0700214 SkCodecPrintf("Error: unable to read second bitmap header.\n");
scroggo79e378d2015-04-01 07:39:40 -0700215 return false;
msarett74114382015-03-16 11:55:18 -0700216 }
217
218 // The number of bits used per pixel in the pixel data
219 uint16_t bitsPerPixel;
220
221 // The compression method for the pixel data
msarett4ab9d5f2015-08-06 15:34:42 -0700222 uint32_t compression = kNone_BmpCompressionMethod;
msarett74114382015-03-16 11:55:18 -0700223
224 // Number of colors in the color table, defaults to 0 or max (see below)
225 uint32_t numColors = 0;
226
227 // Bytes per color in the color table, early versions use 3, most use 4
228 uint32_t bytesPerColor;
229
230 // The image width and height
231 int width, height;
232
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500233 switch (headerType) {
234 case kInfoV1_BmpHeaderType:
235 case kInfoV2_BmpHeaderType:
236 case kInfoV3_BmpHeaderType:
237 case kInfoV4_BmpHeaderType:
238 case kInfoV5_BmpHeaderType:
239 case kOS2VX_BmpHeaderType:
240 // We check the size of the header before entering the if statement.
241 // We should not reach this point unless the size is large enough for
242 // these required fields.
243 SkASSERT(infoBytesRemaining >= 12);
244 width = get_int(iBuffer.get(), 0);
245 height = get_int(iBuffer.get(), 4);
246 bitsPerPixel = get_short(iBuffer.get(), 10);
msarett74114382015-03-16 11:55:18 -0700247
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500248 // Some versions do not have these fields, so we check before
249 // overwriting the default value.
250 if (infoBytesRemaining >= 16) {
251 compression = get_int(iBuffer.get(), 12);
252 if (infoBytesRemaining >= 32) {
253 numColors = get_int(iBuffer.get(), 28);
254 }
msarett74114382015-03-16 11:55:18 -0700255 }
msarett74114382015-03-16 11:55:18 -0700256
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500257 // All of the headers that reach this point, store color table entries
258 // using 4 bytes per pixel.
259 bytesPerColor = 4;
260 break;
261 case kOS2V1_BmpHeaderType:
262 // The OS2V1 is treated separately because it has a unique format
263 width = (int) get_short(iBuffer.get(), 0);
264 height = (int) get_short(iBuffer.get(), 2);
265 bitsPerPixel = get_short(iBuffer.get(), 6);
266 bytesPerColor = 3;
267 break;
268 case kUnknown_BmpHeaderType:
269 // We'll exit above in this case.
270 SkASSERT(false);
271 return false;
msarett74114382015-03-16 11:55:18 -0700272 }
273
274 // Check for valid dimensions from header
scroggo46c57472015-09-30 08:57:13 -0700275 SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700276 if (height < 0) {
277 height = -height;
scroggo46c57472015-09-30 08:57:13 -0700278 rowOrder = SkCodec::kTopDown_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700279 }
msarett9bde9182015-03-25 05:27:48 -0700280 // The height field for bmp in ico is double the actual height because they
281 // contain an XOR mask followed by an AND mask
msarett4ab9d5f2015-08-06 15:34:42 -0700282 if (inIco) {
msarett9bde9182015-03-25 05:27:48 -0700283 height /= 2;
284 }
Leon Scroggins III0354c622017-02-24 15:33:24 -0500285
286 // Arbitrary maximum. Matches Chromium.
287 constexpr int kMaxDim = 1 << 16;
288 if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) {
scroggo230d4ac2015-03-26 07:15:55 -0700289 SkCodecPrintf("Error: invalid bitmap dimensions.\n");
scroggo79e378d2015-04-01 07:39:40 -0700290 return false;
msarett74114382015-03-16 11:55:18 -0700291 }
292
293 // Create mask struct
294 SkMasks::InputMasks inputMasks;
msaretteed039b2015-03-18 11:11:19 -0700295 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks));
msarett74114382015-03-16 11:55:18 -0700296
297 // Determine the input compression format and set bit masks if necessary
298 uint32_t maskBytes = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700299 BmpInputFormat inputFormat = kUnknown_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700300 switch (compression) {
msarett4ab9d5f2015-08-06 15:34:42 -0700301 case kNone_BmpCompressionMethod:
302 inputFormat = kStandard_BmpInputFormat;
msarett1088db92016-03-22 08:58:35 -0700303
304 // In addition to more standard pixel compression formats, bmp supports
305 // the use of bit masks to determine pixel components. The standard
306 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB),
307 // which does not map well to any Skia color formats. For this reason,
308 // we will always enable mask mode with 16 bits per pixel.
309 if (16 == bitsPerPixel) {
310 inputMasks.red = 0x7C00;
311 inputMasks.green = 0x03E0;
312 inputMasks.blue = 0x001F;
313 inputFormat = kBitMask_BmpInputFormat;
314 }
msarett74114382015-03-16 11:55:18 -0700315 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700316 case k8BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700317 if (bitsPerPixel != 8) {
scroggo230d4ac2015-03-26 07:15:55 -0700318 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700319 bitsPerPixel = 8;
320 }
msarett4ab9d5f2015-08-06 15:34:42 -0700321 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700322 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700323 case k4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700324 if (bitsPerPixel != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700325 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700326 bitsPerPixel = 4;
327 }
msarett4ab9d5f2015-08-06 15:34:42 -0700328 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700329 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700330 case kAlphaBitMasks_BmpCompressionMethod:
331 case kBitMasks_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700332 // Load the masks
msarett4ab9d5f2015-08-06 15:34:42 -0700333 inputFormat = kBitMask_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700334 switch (headerType) {
msarett4ab9d5f2015-08-06 15:34:42 -0700335 case kInfoV1_BmpHeaderType: {
msarett74114382015-03-16 11:55:18 -0700336 // The V1 header stores the bit masks after the header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400337 uint8_t buffer[kBmpMaskBytes];
338 if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700339 SkCodecPrintf("Error: unable to read bit inputMasks.\n");
scroggo79e378d2015-04-01 07:39:40 -0700340 return false;
msarett74114382015-03-16 11:55:18 -0700341 }
342 maskBytes = kBmpMaskBytes;
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400343 inputMasks.red = get_int(buffer, 0);
344 inputMasks.green = get_int(buffer, 4);
345 inputMasks.blue = get_int(buffer, 8);
msarett74114382015-03-16 11:55:18 -0700346 break;
347 }
msarett4ab9d5f2015-08-06 15:34:42 -0700348 case kInfoV2_BmpHeaderType:
349 case kInfoV3_BmpHeaderType:
350 case kInfoV4_BmpHeaderType:
351 case kInfoV5_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700352 // Header types are matched based on size. If the header
353 // is V2+, we are guaranteed to be able to read at least
354 // this size.
355 SkASSERT(infoBytesRemaining >= 48);
356 inputMasks.red = get_int(iBuffer.get(), 36);
357 inputMasks.green = get_int(iBuffer.get(), 40);
358 inputMasks.blue = get_int(iBuffer.get(), 44);
msarett1088db92016-03-22 08:58:35 -0700359
360 if (kInfoV2_BmpHeaderType == headerType ||
361 (kInfoV3_BmpHeaderType == headerType && !inIco)) {
362 break;
363 }
364
365 // V3+ bmp files introduce an alpha mask and allow the creator of the image
366 // to use the alpha channels. However, many of these images leave the
367 // alpha channel blank and expect to be rendered as opaque. This is the
368 // case for almost all V3 images, so we ignore the alpha mask. For V4+
369 // images in kMask mode, we will use the alpha mask. Additionally, V3
370 // bmp-in-ico expect us to use the alpha mask.
371 //
372 // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard
373 // mode. We just haven't seen any images that expect this
374 // behavior.
375 //
376 // Header types are matched based on size. If the header is
377 // V3+, we are guaranteed to be able to read at least this size.
378 SkASSERT(infoBytesRemaining > 52);
379 inputMasks.alpha = get_int(iBuffer.get(), 48);
msarett74114382015-03-16 11:55:18 -0700380 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700381 case kOS2VX_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700382 // TODO: Decide if we intend to support this.
383 // It is unsupported in the previous version and
384 // in chromium. I have not come across a test case
385 // that uses this format.
scroggo230d4ac2015-03-26 07:15:55 -0700386 SkCodecPrintf("Error: huffman format unsupported.\n");
scroggo79e378d2015-04-01 07:39:40 -0700387 return false;
msarett74114382015-03-16 11:55:18 -0700388 default:
scroggo230d4ac2015-03-26 07:15:55 -0700389 SkCodecPrintf("Error: invalid bmp bit masks header.\n");
scroggo79e378d2015-04-01 07:39:40 -0700390 return false;
msarett74114382015-03-16 11:55:18 -0700391 }
392 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700393 case kJpeg_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700394 if (24 == bitsPerPixel) {
msarett4ab9d5f2015-08-06 15:34:42 -0700395 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700396 break;
397 }
398 // Fall through
msarett4ab9d5f2015-08-06 15:34:42 -0700399 case kPng_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700400 // TODO: Decide if we intend to support this.
401 // It is unsupported in the previous version and
402 // in chromium. I think it is used mostly for printers.
scroggo230d4ac2015-03-26 07:15:55 -0700403 SkCodecPrintf("Error: compression format not supported.\n");
scroggo79e378d2015-04-01 07:39:40 -0700404 return false;
msarett4ab9d5f2015-08-06 15:34:42 -0700405 case kCMYK_BmpCompressionMethod:
406 case kCMYK8BitRLE_BmpCompressionMethod:
407 case kCMYK4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700408 // TODO: Same as above.
scroggo230d4ac2015-03-26 07:15:55 -0700409 SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n");
scroggo79e378d2015-04-01 07:39:40 -0700410 return false;
msarett74114382015-03-16 11:55:18 -0700411 default:
scroggo230d4ac2015-03-26 07:15:55 -0700412 SkCodecPrintf("Error: invalid format for bitmap decoding.\n");
scroggo79e378d2015-04-01 07:39:40 -0700413 return false;
msarett74114382015-03-16 11:55:18 -0700414 }
mtklein852f15d2016-03-17 10:51:27 -0700415 iBuffer.reset();
msarett74114382015-03-16 11:55:18 -0700416
msaretteed039b2015-03-18 11:11:19 -0700417 // Calculate the number of bytes read so far
418 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes;
msarett4ab9d5f2015-08-06 15:34:42 -0700419 if (!inIco && offset < bytesRead) {
msarett9aa32d12015-09-01 14:40:46 -0700420 // TODO (msarett): Do we really want to fail if the offset in the header is invalid?
421 // Seems like we can just assume that the offset is zero and try to decode?
422 // Maybe we don't want to try to decode corrupt images?
scroggo230d4ac2015-03-26 07:15:55 -0700423 SkCodecPrintf("Error: pixel data offset less than header size.\n");
scroggo79e378d2015-04-01 07:39:40 -0700424 return false;
msaretteed039b2015-03-18 11:11:19 -0700425 }
msarett74114382015-03-16 11:55:18 -0700426
msarett9aa32d12015-09-01 14:40:46 -0700427
msarettf4004f92016-02-11 10:49:31 -0800428
msarett1088db92016-03-22 08:58:35 -0700429 switch (inputFormat) {
430 case kStandard_BmpInputFormat: {
msarettc30c4182016-04-20 11:53:35 -0700431 // BMPs are generally opaque, however BMPs-in-ICOs may contain
432 // a transparency mask after the image. Therefore, we mark the
433 // alpha as kBinary if the BMP is contained in an ICO.
434 // We use |isOpaque| to indicate if the BMP itself is opaque.
435 SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
436 SkEncodedInfo::kOpaque_Alpha;
msarett1088db92016-03-22 08:58:35 -0700437 bool isOpaque = true;
msarettc30c4182016-04-20 11:53:35 -0700438
439 SkEncodedInfo::Color color;
440 uint8_t bitsPerComponent;
msarett1088db92016-03-22 08:58:35 -0700441 switch (bitsPerPixel) {
442 // Palette formats
443 case 1:
444 case 2:
445 case 4:
446 case 8:
msarettc30c4182016-04-20 11:53:35 -0700447 // In the case of ICO, kBGRA is actually the closest match,
448 // since we will need to apply a transparency mask.
449 if (inIco) {
450 color = SkEncodedInfo::kBGRA_Color;
451 bitsPerComponent = 8;
452 } else {
453 color = SkEncodedInfo::kPalette_Color;
454 bitsPerComponent = (uint8_t) bitsPerPixel;
msarett1088db92016-03-22 08:58:35 -0700455 }
456 break;
457 case 24:
msarett3e375b02016-05-04 13:03:48 -0700458 // In the case of ICO, kBGRA is actually the closest match,
459 // since we will need to apply a transparency mask.
460 color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color;
msarettc30c4182016-04-20 11:53:35 -0700461 bitsPerComponent = 8;
462 break;
msarett1088db92016-03-22 08:58:35 -0700463 case 32:
464 // 32-bit BMP-in-ICOs actually use the alpha channel in place of a
465 // transparency mask.
466 if (inIco) {
467 isOpaque = false;
msarettc30c4182016-04-20 11:53:35 -0700468 alpha = SkEncodedInfo::kUnpremul_Alpha;
469 color = SkEncodedInfo::kBGRA_Color;
470 } else {
471 color = SkEncodedInfo::kBGRX_Color;
msarett1088db92016-03-22 08:58:35 -0700472 }
msarettc30c4182016-04-20 11:53:35 -0700473 bitsPerComponent = 8;
msarett1088db92016-03-22 08:58:35 -0700474 break;
475 default:
476 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
477 return false;
478 }
msarett4ab9d5f2015-08-06 15:34:42 -0700479
msarett1088db92016-03-22 08:58:35 -0700480 if (codecOut) {
msarettbe8216a2015-12-04 08:00:50 -0800481 // We require streams to have a memory base for Bmp-in-Ico decodes.
482 SkASSERT(!inIco || nullptr != stream->getMemoryBase());
msarett1088db92016-03-22 08:58:35 -0700483
484 // Set the image info and create a codec.
msarettc30c4182016-04-20 11:53:35 -0700485 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400486 codecOut->reset(new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel,
487 numColors, bytesPerColor, offset - bytesRead,
488 rowOrder, isOpaque, inIco));
489 return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer();
msarett1088db92016-03-22 08:58:35 -0700490 }
491 return true;
492 }
493
494 case kBitMask_BmpInputFormat: {
495 // Bmp-in-Ico must be standard mode
496 if (inIco) {
497 SkCodecPrintf("Error: Icos may not use bit mask format.\n");
498 return false;
499 }
500
501 switch (bitsPerPixel) {
502 case 16:
503 case 24:
504 case 32:
505 break;
506 default:
507 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
508 return false;
509 }
510
511 // Skip to the start of the pixel array.
512 // We can do this here because there is no color table to read
513 // in bit mask mode.
514 if (stream->skip(offset - bytesRead) != offset - bytesRead) {
515 SkCodecPrintf("Error: unable to skip to image data.\n");
516 return false;
517 }
518
519 if (codecOut) {
520 // Check that input bit masks are valid and create the masks object
Ben Wagner145dbcd2016-11-03 14:40:50 -0400521 std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel));
msarett1088db92016-03-22 08:58:35 -0700522 if (nullptr == masks) {
523 SkCodecPrintf("Error: invalid input masks.\n");
msarett4ab9d5f2015-08-06 15:34:42 -0700524 return false;
525 }
msarett4ab9d5f2015-08-06 15:34:42 -0700526
msarettc30c4182016-04-20 11:53:35 -0700527 // Masked bmps are not a great fit for SkEncodedInfo, since they have
528 // arbitrary component orderings and bits per component. Here we choose
529 // somewhat reasonable values - it's ok that we don't match exactly
530 // because SkBmpMaskCodec has its own mask swizzler anyway.
531 SkEncodedInfo::Color color;
532 SkEncodedInfo::Alpha alpha;
533 if (masks->getAlphaMask()) {
534 color = SkEncodedInfo::kBGRA_Color;
535 alpha = SkEncodedInfo::kUnpremul_Alpha;
536 } else {
537 color = SkEncodedInfo::kBGR_Color;
538 alpha = SkEncodedInfo::kOpaque_Alpha;
539 }
540 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400541 codecOut->reset(new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel,
542 masks.release(), rowOrder));
543 return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer();
msarett1088db92016-03-22 08:58:35 -0700544 }
545 return true;
546 }
547
548 case kRLE_BmpInputFormat: {
549 // We should not reach this point without a valid value of bitsPerPixel.
550 SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel);
551
552 // Check for a valid number of total bytes when in RLE mode
553 if (totalBytes <= offset) {
554 SkCodecPrintf("Error: RLE requires valid input size.\n");
555 return false;
556 }
msarett1088db92016-03-22 08:58:35 -0700557
558 // Bmp-in-Ico must be standard mode
559 // When inIco is true, this line cannot be reached, since we
560 // require that RLE Bmps have a valid number of totalBytes, and
561 // Icos skip the header that contains totalBytes.
562 SkASSERT(!inIco);
563
564 if (codecOut) {
565 // RLE inputs may skip pixels, leaving them as transparent. This
566 // is uncommon, but we cannot be certain that an RLE bmp will be
msarettc30c4182016-04-20 11:53:35 -0700567 // opaque or that we will be able to represent it with a palette.
568 // For that reason, we always indicate that we are kBGRA.
569 const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color,
570 SkEncodedInfo::kBinary_Alpha, 8);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400571 codecOut->reset(new SkBmpRLECodec(width, height, info, stream, bitsPerPixel,
572 numColors, bytesPerColor, offset - bytesRead,
573 rowOrder));
msarett1088db92016-03-22 08:58:35 -0700574 }
575 return true;
msarett4ab9d5f2015-08-06 15:34:42 -0700576 }
msarett1088db92016-03-22 08:58:35 -0700577 default:
578 SkASSERT(false);
579 return false;
scroggo79e378d2015-04-01 07:39:40 -0700580 }
scroggo79e378d2015-04-01 07:39:40 -0700581}
582
583/*
scroggo79e378d2015-04-01 07:39:40 -0700584 * Creates a bmp decoder
585 * Reads enough of the stream to determine the image format
scroggo79e378d2015-04-01 07:39:40 -0700586 */
msarett4ab9d5f2015-08-06 15:34:42 -0700587SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool inIco) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400588 std::unique_ptr<SkStream> streamDeleter(stream);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400589 std::unique_ptr<SkCodec> codec;
590 bool success = ReadHeader(stream, inIco, &codec);
591 if (codec) {
scroggo0a7e69c2015-04-03 07:22:22 -0700592 // codec has taken ownership of stream, so we do not need to
593 // delete it.
mtklein18300a32016-03-16 13:53:35 -0700594 streamDeleter.release();
scroggo79e378d2015-04-01 07:39:40 -0700595 }
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400596 return success ? codec.release() : nullptr;
msarett74114382015-03-16 11:55:18 -0700597}
598
msarettc30c4182016-04-20 11:53:35 -0700599SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
scroggo46c57472015-09-30 08:57:13 -0700600 uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400601 : INHERITED(width, height, info, kXformSrcColorFormat, stream, SkColorSpace::MakeSRGB())
msarett74114382015-03-16 11:55:18 -0700602 , fBitsPerPixel(bitsPerPixel)
msarett74114382015-03-16 11:55:18 -0700603 , fRowOrder(rowOrder)
msarettc30c4182016-04-20 11:53:35 -0700604 , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel)))
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400605 , fXformBuffer(nullptr)
msarett74114382015-03-16 11:55:18 -0700606{}
607
scroggob427db12015-08-12 07:24:13 -0700608bool SkBmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700609 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr);
msaretteed039b2015-03-18 11:11:19 -0700610}
611
scroggo46c57472015-09-30 08:57:13 -0700612int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const {
613 if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) {
msarett5406d6f2015-08-31 06:55:13 -0700614 return y;
615 }
scroggo46c57472015-09-30 08:57:13 -0700616 SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder);
msarett5406d6f2015-08-31 06:55:13 -0700617 return height - y - 1;
618}
619
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400620SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000621 const SkCodec::Options& options) {
Matt Sarettcf3f2342017-03-23 15:32:25 -0400622 if (!conversion_possible(dstInfo, this->getInfo()) ||
623 !this->initializeColorXform(dstInfo, options.fPremulBehavior))
624 {
scroggo46c57472015-09-30 08:57:13 -0700625 return kInvalidConversion;
msarett5406d6f2015-08-31 06:55:13 -0700626 }
627
Leon Scroggins571b30f2017-07-11 17:35:31 +0000628 return this->onPrepareToDecode(dstInfo, options);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400629}
630
631SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000632 const SkCodec::Options& options) {
633 return prepareToDecode(dstInfo, options);
scroggo46c57472015-09-30 08:57:13 -0700634}
msarett5406d6f2015-08-31 06:55:13 -0700635
msarette6dd0042015-10-09 11:07:34 -0700636int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700637 // Create a new image info representing the portion of the image to decode
638 SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count);
msarett5406d6f2015-08-31 06:55:13 -0700639
scroggo46c57472015-09-30 08:57:13 -0700640 // Decode the requested rows
641 return this->decodeRows(rowInfo, dst, rowBytes, this->options());
642}
msarett9b9497e2016-02-11 13:29:36 -0800643
644bool SkBmpCodec::skipRows(int count) {
645 const size_t bytesToSkip = count * fSrcRowBytes;
646 return this->stream()->skip(bytesToSkip) == bytesToSkip;
647}
648
649bool SkBmpCodec::onSkipScanlines(int count) {
650 return this->skipRows(count);
651}