blob: 70397f656f2f06ebda4b5d9344eb7a8eb3d3deec [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 */
Leon Scroggins III588fb042017-07-14 16:32:31 -040070SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result) {
71 return SkBmpCodec::NewFromStream(stream, result, false);
msarett9bde9182015-03-25 05:27:48 -070072}
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 */
Leon Scroggins III588fb042017-07-14 16:32:31 -040078SkCodec* SkBmpCodec::NewFromIco(SkStream* stream, Result* result) {
79 return SkBmpCodec::NewFromStream(stream, result, true);
msarett9bde9182015-03-25 05:27:48 -070080}
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
Leon Scroggins III588fb042017-07-14 16:32:31 -0400135SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco,
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400136 std::unique_ptr<SkCodec>* codecOut) {
tomhudson7aa846c2015-03-24 13:47:41 -0700137 // The total bytes in the bmp file
msarett9bde9182015-03-25 05:27:48 -0700138 // We only need to use this value for RLE decoding, so we will only
139 // check that it is valid in the RLE case.
140 uint32_t totalBytes;
tomhudson7aa846c2015-03-24 13:47:41 -0700141 // The offset from the start of the file where the pixel data begins
msarett9bde9182015-03-25 05:27:48 -0700142 uint32_t offset;
143 // The size of the second (info) header in bytes
144 uint32_t infoBytes;
145
146 // Bmps embedded in Icos skip the first Bmp header
msarett4ab9d5f2015-08-06 15:34:42 -0700147 if (!inIco) {
msarett9bde9182015-03-25 05:27:48 -0700148 // Read the first header and the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400149 uint8_t hBuffer[kBmpHeaderBytesPlusFour];
150 if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) !=
msarett9bde9182015-03-25 05:27:48 -0700151 kBmpHeaderBytesPlusFour) {
scroggo230d4ac2015-03-26 07:15:55 -0700152 SkCodecPrintf("Error: unable to read first bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400153 return kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700154 }
155
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400156 totalBytes = get_int(hBuffer, 2);
157 offset = get_int(hBuffer, 10);
msarett9bde9182015-03-25 05:27:48 -0700158 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700159 SkCodecPrintf("Error: invalid starting location for pixel data\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400160 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700161 }
162
163 // The size of the second (info) header in bytes
164 // The size is the first field of the second header, so we have already
165 // read the first four infoBytes.
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400166 infoBytes = get_int(hBuffer, 14);
msarett9bde9182015-03-25 05:27:48 -0700167 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700168 SkCodecPrintf("Error: invalid second header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400169 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700170 }
171 } else {
172 // This value is only used by RLE compression. Bmp in Ico files do not
173 // use RLE. If the compression field is incorrectly signaled as RLE,
174 // we will catch this and signal an error below.
175 totalBytes = 0;
176
177 // Bmps in Ico cannot specify an offset. We will always assume that
178 // pixel data begins immediately after the color table. This value
179 // will be corrected below.
180 offset = 0;
181
182 // Read the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400183 uint8_t hBuffer[4];
184 if (stream->read(hBuffer, 4) != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700185 SkCodecPrintf("Error: unable to read size of second bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400186 return kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700187 }
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400188 infoBytes = get_int(hBuffer, 0);
msarett9bde9182015-03-25 05:27:48 -0700189 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700190 SkCodecPrintf("Error: invalid second header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400191 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700192 }
tomhudson7aa846c2015-03-24 13:47:41 -0700193 }
194
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500195 // Determine image information depending on second header format
196 const BmpHeaderType headerType = get_header_type(infoBytes);
197 if (kUnknown_BmpHeaderType == headerType) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400198 return kInvalidInput;
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500199 }
200
msarett9bde9182015-03-25 05:27:48 -0700201 // We already read the first four bytes of the info header to get the size
msarett74114382015-03-16 11:55:18 -0700202 const uint32_t infoBytesRemaining = infoBytes - 4;
msarett74114382015-03-16 11:55:18 -0700203
204 // Read the second header
Ben Wagner7ecc5962016-11-02 17:07:33 -0400205 std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]);
msarett74114382015-03-16 11:55:18 -0700206 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) {
scroggo230d4ac2015-03-26 07:15:55 -0700207 SkCodecPrintf("Error: unable to read second bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400208 return kIncompleteInput;
msarett74114382015-03-16 11:55:18 -0700209 }
210
211 // The number of bits used per pixel in the pixel data
212 uint16_t bitsPerPixel;
213
214 // The compression method for the pixel data
msarett4ab9d5f2015-08-06 15:34:42 -0700215 uint32_t compression = kNone_BmpCompressionMethod;
msarett74114382015-03-16 11:55:18 -0700216
217 // Number of colors in the color table, defaults to 0 or max (see below)
218 uint32_t numColors = 0;
219
220 // Bytes per color in the color table, early versions use 3, most use 4
221 uint32_t bytesPerColor;
222
223 // The image width and height
224 int width, height;
225
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500226 switch (headerType) {
227 case kInfoV1_BmpHeaderType:
228 case kInfoV2_BmpHeaderType:
229 case kInfoV3_BmpHeaderType:
230 case kInfoV4_BmpHeaderType:
231 case kInfoV5_BmpHeaderType:
232 case kOS2VX_BmpHeaderType:
233 // We check the size of the header before entering the if statement.
234 // We should not reach this point unless the size is large enough for
235 // these required fields.
236 SkASSERT(infoBytesRemaining >= 12);
237 width = get_int(iBuffer.get(), 0);
238 height = get_int(iBuffer.get(), 4);
239 bitsPerPixel = get_short(iBuffer.get(), 10);
msarett74114382015-03-16 11:55:18 -0700240
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500241 // Some versions do not have these fields, so we check before
242 // overwriting the default value.
243 if (infoBytesRemaining >= 16) {
244 compression = get_int(iBuffer.get(), 12);
245 if (infoBytesRemaining >= 32) {
246 numColors = get_int(iBuffer.get(), 28);
247 }
msarett74114382015-03-16 11:55:18 -0700248 }
msarett74114382015-03-16 11:55:18 -0700249
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500250 // All of the headers that reach this point, store color table entries
251 // using 4 bytes per pixel.
252 bytesPerColor = 4;
253 break;
254 case kOS2V1_BmpHeaderType:
255 // The OS2V1 is treated separately because it has a unique format
256 width = (int) get_short(iBuffer.get(), 0);
257 height = (int) get_short(iBuffer.get(), 2);
258 bitsPerPixel = get_short(iBuffer.get(), 6);
259 bytesPerColor = 3;
260 break;
261 case kUnknown_BmpHeaderType:
262 // We'll exit above in this case.
263 SkASSERT(false);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400264 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700265 }
266
267 // Check for valid dimensions from header
scroggo46c57472015-09-30 08:57:13 -0700268 SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700269 if (height < 0) {
270 height = -height;
scroggo46c57472015-09-30 08:57:13 -0700271 rowOrder = SkCodec::kTopDown_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700272 }
msarett9bde9182015-03-25 05:27:48 -0700273 // The height field for bmp in ico is double the actual height because they
274 // contain an XOR mask followed by an AND mask
msarett4ab9d5f2015-08-06 15:34:42 -0700275 if (inIco) {
msarett9bde9182015-03-25 05:27:48 -0700276 height /= 2;
277 }
Leon Scroggins III0354c622017-02-24 15:33:24 -0500278
279 // Arbitrary maximum. Matches Chromium.
280 constexpr int kMaxDim = 1 << 16;
281 if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) {
scroggo230d4ac2015-03-26 07:15:55 -0700282 SkCodecPrintf("Error: invalid bitmap dimensions.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400283 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700284 }
285
286 // Create mask struct
287 SkMasks::InputMasks inputMasks;
msaretteed039b2015-03-18 11:11:19 -0700288 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks));
msarett74114382015-03-16 11:55:18 -0700289
290 // Determine the input compression format and set bit masks if necessary
291 uint32_t maskBytes = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700292 BmpInputFormat inputFormat = kUnknown_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700293 switch (compression) {
msarett4ab9d5f2015-08-06 15:34:42 -0700294 case kNone_BmpCompressionMethod:
295 inputFormat = kStandard_BmpInputFormat;
msarett1088db92016-03-22 08:58:35 -0700296
297 // In addition to more standard pixel compression formats, bmp supports
298 // the use of bit masks to determine pixel components. The standard
299 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB),
300 // which does not map well to any Skia color formats. For this reason,
301 // we will always enable mask mode with 16 bits per pixel.
302 if (16 == bitsPerPixel) {
303 inputMasks.red = 0x7C00;
304 inputMasks.green = 0x03E0;
305 inputMasks.blue = 0x001F;
306 inputFormat = kBitMask_BmpInputFormat;
307 }
msarett74114382015-03-16 11:55:18 -0700308 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700309 case k8BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700310 if (bitsPerPixel != 8) {
scroggo230d4ac2015-03-26 07:15:55 -0700311 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700312 bitsPerPixel = 8;
313 }
msarett4ab9d5f2015-08-06 15:34:42 -0700314 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700315 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700316 case k4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700317 if (bitsPerPixel != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700318 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700319 bitsPerPixel = 4;
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 kAlphaBitMasks_BmpCompressionMethod:
324 case kBitMasks_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700325 // Load the masks
msarett4ab9d5f2015-08-06 15:34:42 -0700326 inputFormat = kBitMask_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700327 switch (headerType) {
msarett4ab9d5f2015-08-06 15:34:42 -0700328 case kInfoV1_BmpHeaderType: {
msarett74114382015-03-16 11:55:18 -0700329 // The V1 header stores the bit masks after the header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400330 uint8_t buffer[kBmpMaskBytes];
331 if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700332 SkCodecPrintf("Error: unable to read bit inputMasks.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400333 return kIncompleteInput;
msarett74114382015-03-16 11:55:18 -0700334 }
335 maskBytes = kBmpMaskBytes;
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400336 inputMasks.red = get_int(buffer, 0);
337 inputMasks.green = get_int(buffer, 4);
338 inputMasks.blue = get_int(buffer, 8);
msarett74114382015-03-16 11:55:18 -0700339 break;
340 }
msarett4ab9d5f2015-08-06 15:34:42 -0700341 case kInfoV2_BmpHeaderType:
342 case kInfoV3_BmpHeaderType:
343 case kInfoV4_BmpHeaderType:
344 case kInfoV5_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700345 // Header types are matched based on size. If the header
346 // is V2+, we are guaranteed to be able to read at least
347 // this size.
348 SkASSERT(infoBytesRemaining >= 48);
349 inputMasks.red = get_int(iBuffer.get(), 36);
350 inputMasks.green = get_int(iBuffer.get(), 40);
351 inputMasks.blue = get_int(iBuffer.get(), 44);
msarett1088db92016-03-22 08:58:35 -0700352
353 if (kInfoV2_BmpHeaderType == headerType ||
354 (kInfoV3_BmpHeaderType == headerType && !inIco)) {
355 break;
356 }
357
358 // V3+ bmp files introduce an alpha mask and allow the creator of the image
359 // to use the alpha channels. However, many of these images leave the
360 // alpha channel blank and expect to be rendered as opaque. This is the
361 // case for almost all V3 images, so we ignore the alpha mask. For V4+
362 // images in kMask mode, we will use the alpha mask. Additionally, V3
363 // bmp-in-ico expect us to use the alpha mask.
364 //
365 // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard
366 // mode. We just haven't seen any images that expect this
367 // behavior.
368 //
369 // Header types are matched based on size. If the header is
370 // V3+, we are guaranteed to be able to read at least this size.
371 SkASSERT(infoBytesRemaining > 52);
372 inputMasks.alpha = get_int(iBuffer.get(), 48);
msarett74114382015-03-16 11:55:18 -0700373 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700374 case kOS2VX_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700375 // TODO: Decide if we intend to support this.
376 // It is unsupported in the previous version and
377 // in chromium. I have not come across a test case
378 // that uses this format.
scroggo230d4ac2015-03-26 07:15:55 -0700379 SkCodecPrintf("Error: huffman format unsupported.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400380 return kUnimplemented;
msarett74114382015-03-16 11:55:18 -0700381 default:
scroggo230d4ac2015-03-26 07:15:55 -0700382 SkCodecPrintf("Error: invalid bmp bit masks header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400383 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700384 }
385 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700386 case kJpeg_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700387 if (24 == bitsPerPixel) {
msarett4ab9d5f2015-08-06 15:34:42 -0700388 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700389 break;
390 }
391 // Fall through
msarett4ab9d5f2015-08-06 15:34:42 -0700392 case kPng_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700393 // TODO: Decide if we intend to support this.
394 // It is unsupported in the previous version and
395 // in chromium. I think it is used mostly for printers.
scroggo230d4ac2015-03-26 07:15:55 -0700396 SkCodecPrintf("Error: compression format not supported.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400397 return kUnimplemented;
msarett4ab9d5f2015-08-06 15:34:42 -0700398 case kCMYK_BmpCompressionMethod:
399 case kCMYK8BitRLE_BmpCompressionMethod:
400 case kCMYK4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700401 // TODO: Same as above.
scroggo230d4ac2015-03-26 07:15:55 -0700402 SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400403 return kUnimplemented;
msarett74114382015-03-16 11:55:18 -0700404 default:
scroggo230d4ac2015-03-26 07:15:55 -0700405 SkCodecPrintf("Error: invalid format for bitmap decoding.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400406 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700407 }
mtklein852f15d2016-03-17 10:51:27 -0700408 iBuffer.reset();
msarett74114382015-03-16 11:55:18 -0700409
msaretteed039b2015-03-18 11:11:19 -0700410 // Calculate the number of bytes read so far
411 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes;
msarett4ab9d5f2015-08-06 15:34:42 -0700412 if (!inIco && offset < bytesRead) {
msarett9aa32d12015-09-01 14:40:46 -0700413 // TODO (msarett): Do we really want to fail if the offset in the header is invalid?
414 // Seems like we can just assume that the offset is zero and try to decode?
415 // Maybe we don't want to try to decode corrupt images?
scroggo230d4ac2015-03-26 07:15:55 -0700416 SkCodecPrintf("Error: pixel data offset less than header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400417 return kInvalidInput;
msaretteed039b2015-03-18 11:11:19 -0700418 }
msarett74114382015-03-16 11:55:18 -0700419
msarett9aa32d12015-09-01 14:40:46 -0700420
msarettf4004f92016-02-11 10:49:31 -0800421
msarett1088db92016-03-22 08:58:35 -0700422 switch (inputFormat) {
423 case kStandard_BmpInputFormat: {
msarettc30c4182016-04-20 11:53:35 -0700424 // BMPs are generally opaque, however BMPs-in-ICOs may contain
425 // a transparency mask after the image. Therefore, we mark the
426 // alpha as kBinary if the BMP is contained in an ICO.
427 // We use |isOpaque| to indicate if the BMP itself is opaque.
428 SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
429 SkEncodedInfo::kOpaque_Alpha;
msarett1088db92016-03-22 08:58:35 -0700430 bool isOpaque = true;
msarettc30c4182016-04-20 11:53:35 -0700431
432 SkEncodedInfo::Color color;
433 uint8_t bitsPerComponent;
msarett1088db92016-03-22 08:58:35 -0700434 switch (bitsPerPixel) {
435 // Palette formats
436 case 1:
437 case 2:
438 case 4:
439 case 8:
msarettc30c4182016-04-20 11:53:35 -0700440 // In the case of ICO, kBGRA is actually the closest match,
441 // since we will need to apply a transparency mask.
442 if (inIco) {
443 color = SkEncodedInfo::kBGRA_Color;
444 bitsPerComponent = 8;
445 } else {
446 color = SkEncodedInfo::kPalette_Color;
447 bitsPerComponent = (uint8_t) bitsPerPixel;
msarett1088db92016-03-22 08:58:35 -0700448 }
449 break;
450 case 24:
msarett3e375b02016-05-04 13:03:48 -0700451 // In the case of ICO, kBGRA is actually the closest match,
452 // since we will need to apply a transparency mask.
453 color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color;
msarettc30c4182016-04-20 11:53:35 -0700454 bitsPerComponent = 8;
455 break;
msarett1088db92016-03-22 08:58:35 -0700456 case 32:
457 // 32-bit BMP-in-ICOs actually use the alpha channel in place of a
458 // transparency mask.
459 if (inIco) {
460 isOpaque = false;
msarettc30c4182016-04-20 11:53:35 -0700461 alpha = SkEncodedInfo::kUnpremul_Alpha;
462 color = SkEncodedInfo::kBGRA_Color;
463 } else {
464 color = SkEncodedInfo::kBGRX_Color;
msarett1088db92016-03-22 08:58:35 -0700465 }
msarettc30c4182016-04-20 11:53:35 -0700466 bitsPerComponent = 8;
msarett1088db92016-03-22 08:58:35 -0700467 break;
468 default:
469 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400470 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700471 }
msarett4ab9d5f2015-08-06 15:34:42 -0700472
msarett1088db92016-03-22 08:58:35 -0700473 if (codecOut) {
msarettbe8216a2015-12-04 08:00:50 -0800474 // We require streams to have a memory base for Bmp-in-Ico decodes.
475 SkASSERT(!inIco || nullptr != stream->getMemoryBase());
msarett1088db92016-03-22 08:58:35 -0700476
477 // Set the image info and create a codec.
msarettc30c4182016-04-20 11:53:35 -0700478 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400479 codecOut->reset(new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel,
480 numColors, bytesPerColor, offset - bytesRead,
481 rowOrder, isOpaque, inIco));
Leon Scroggins III588fb042017-07-14 16:32:31 -0400482 return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer()
483 ? kSuccess : kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700484 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400485 return kSuccess;
msarett1088db92016-03-22 08:58:35 -0700486 }
487
488 case kBitMask_BmpInputFormat: {
489 // Bmp-in-Ico must be standard mode
490 if (inIco) {
491 SkCodecPrintf("Error: Icos may not use bit mask format.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400492 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700493 }
494
495 switch (bitsPerPixel) {
496 case 16:
497 case 24:
498 case 32:
499 break;
500 default:
501 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400502 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700503 }
504
505 // Skip to the start of the pixel array.
506 // We can do this here because there is no color table to read
507 // in bit mask mode.
508 if (stream->skip(offset - bytesRead) != offset - bytesRead) {
509 SkCodecPrintf("Error: unable to skip to image data.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400510 return kIncompleteInput;
msarett1088db92016-03-22 08:58:35 -0700511 }
512
513 if (codecOut) {
514 // Check that input bit masks are valid and create the masks object
Ben Wagner145dbcd2016-11-03 14:40:50 -0400515 std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel));
msarett1088db92016-03-22 08:58:35 -0700516 if (nullptr == masks) {
517 SkCodecPrintf("Error: invalid input masks.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400518 return kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700519 }
msarett4ab9d5f2015-08-06 15:34:42 -0700520
msarettc30c4182016-04-20 11:53:35 -0700521 // Masked bmps are not a great fit for SkEncodedInfo, since they have
522 // arbitrary component orderings and bits per component. Here we choose
523 // somewhat reasonable values - it's ok that we don't match exactly
524 // because SkBmpMaskCodec has its own mask swizzler anyway.
525 SkEncodedInfo::Color color;
526 SkEncodedInfo::Alpha alpha;
527 if (masks->getAlphaMask()) {
528 color = SkEncodedInfo::kBGRA_Color;
529 alpha = SkEncodedInfo::kUnpremul_Alpha;
530 } else {
531 color = SkEncodedInfo::kBGR_Color;
532 alpha = SkEncodedInfo::kOpaque_Alpha;
533 }
534 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400535 codecOut->reset(new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel,
536 masks.release(), rowOrder));
Leon Scroggins III588fb042017-07-14 16:32:31 -0400537 return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer()
538 ? kSuccess : kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700539 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400540 return kSuccess;
msarett1088db92016-03-22 08:58:35 -0700541 }
542
543 case kRLE_BmpInputFormat: {
544 // We should not reach this point without a valid value of bitsPerPixel.
545 SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel);
546
547 // Check for a valid number of total bytes when in RLE mode
548 if (totalBytes <= offset) {
549 SkCodecPrintf("Error: RLE requires valid input size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400550 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700551 }
msarett1088db92016-03-22 08:58:35 -0700552
553 // Bmp-in-Ico must be standard mode
554 // When inIco is true, this line cannot be reached, since we
555 // require that RLE Bmps have a valid number of totalBytes, and
556 // Icos skip the header that contains totalBytes.
557 SkASSERT(!inIco);
558
559 if (codecOut) {
560 // RLE inputs may skip pixels, leaving them as transparent. This
561 // is uncommon, but we cannot be certain that an RLE bmp will be
msarettc30c4182016-04-20 11:53:35 -0700562 // opaque or that we will be able to represent it with a palette.
563 // For that reason, we always indicate that we are kBGRA.
564 const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color,
565 SkEncodedInfo::kBinary_Alpha, 8);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400566 codecOut->reset(new SkBmpRLECodec(width, height, info, stream, bitsPerPixel,
567 numColors, bytesPerColor, offset - bytesRead,
568 rowOrder));
msarett1088db92016-03-22 08:58:35 -0700569 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400570 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700571 }
msarett1088db92016-03-22 08:58:35 -0700572 default:
573 SkASSERT(false);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400574 return kInvalidInput;
scroggo79e378d2015-04-01 07:39:40 -0700575 }
scroggo79e378d2015-04-01 07:39:40 -0700576}
577
578/*
scroggo79e378d2015-04-01 07:39:40 -0700579 * Creates a bmp decoder
580 * Reads enough of the stream to determine the image format
scroggo79e378d2015-04-01 07:39:40 -0700581 */
Leon Scroggins III588fb042017-07-14 16:32:31 -0400582SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result, bool inIco) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400583 std::unique_ptr<SkStream> streamDeleter(stream);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400584 std::unique_ptr<SkCodec> codec;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400585 *result = ReadHeader(stream, inIco, &codec);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400586 if (codec) {
scroggo0a7e69c2015-04-03 07:22:22 -0700587 // codec has taken ownership of stream, so we do not need to
588 // delete it.
mtklein18300a32016-03-16 13:53:35 -0700589 streamDeleter.release();
scroggo79e378d2015-04-01 07:39:40 -0700590 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400591 return kSuccess == *result ? codec.release() : nullptr;
msarett74114382015-03-16 11:55:18 -0700592}
593
msarettc30c4182016-04-20 11:53:35 -0700594SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
scroggo46c57472015-09-30 08:57:13 -0700595 uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400596 : INHERITED(width, height, info, kXformSrcColorFormat, stream, SkColorSpace::MakeSRGB())
msarett74114382015-03-16 11:55:18 -0700597 , fBitsPerPixel(bitsPerPixel)
msarett74114382015-03-16 11:55:18 -0700598 , fRowOrder(rowOrder)
msarettc30c4182016-04-20 11:53:35 -0700599 , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel)))
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400600 , fXformBuffer(nullptr)
msarett74114382015-03-16 11:55:18 -0700601{}
602
scroggob427db12015-08-12 07:24:13 -0700603bool SkBmpCodec::onRewind() {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400604 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr) == kSuccess;
msaretteed039b2015-03-18 11:11:19 -0700605}
606
scroggo46c57472015-09-30 08:57:13 -0700607int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const {
608 if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) {
msarett5406d6f2015-08-31 06:55:13 -0700609 return y;
610 }
scroggo46c57472015-09-30 08:57:13 -0700611 SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder);
msarett5406d6f2015-08-31 06:55:13 -0700612 return height - y - 1;
613}
614
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400615SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000616 const SkCodec::Options& options) {
Matt Sarettcf3f2342017-03-23 15:32:25 -0400617 if (!conversion_possible(dstInfo, this->getInfo()) ||
618 !this->initializeColorXform(dstInfo, options.fPremulBehavior))
619 {
scroggo46c57472015-09-30 08:57:13 -0700620 return kInvalidConversion;
msarett5406d6f2015-08-31 06:55:13 -0700621 }
622
Leon Scroggins571b30f2017-07-11 17:35:31 +0000623 return this->onPrepareToDecode(dstInfo, options);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400624}
625
626SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000627 const SkCodec::Options& options) {
628 return prepareToDecode(dstInfo, options);
scroggo46c57472015-09-30 08:57:13 -0700629}
msarett5406d6f2015-08-31 06:55:13 -0700630
msarette6dd0042015-10-09 11:07:34 -0700631int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700632 // Create a new image info representing the portion of the image to decode
633 SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count);
msarett5406d6f2015-08-31 06:55:13 -0700634
scroggo46c57472015-09-30 08:57:13 -0700635 // Decode the requested rows
636 return this->decodeRows(rowInfo, dst, rowBytes, this->options());
637}
msarett9b9497e2016-02-11 13:29:36 -0800638
639bool SkBmpCodec::skipRows(int count) {
640 const size_t bytesToSkip = count * fSrcRowBytes;
641 return this->stream()->skip(bytesToSkip) == bytesToSkip;
642}
643
644bool SkBmpCodec::onSkipScanlines(int count) {
645 return this->skipRows(count);
646}