blob: e13dccbd11de2182cdbd1f91962a81e098707e0f [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"
Cary Clarka4083c92017-09-15 11:59:23 -040013#include "SkColorData.h"
msarett74114382015-03-16 11:55:18 -070014#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 */
Mike Reedede7bac2017-07-23 15:30:02 -040070std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
71 Result* result) {
72 return SkBmpCodec::MakeFromStream(std::move(stream), result, false);
msarett9bde9182015-03-25 05:27:48 -070073}
74
75/*
msarett9bde9182015-03-25 05:27:48 -070076 * Creates a bmp decoder for a bmp embedded in ico
77 * Reads enough of the stream to determine the image format
msarett9bde9182015-03-25 05:27:48 -070078 */
Mike Reedede7bac2017-07-23 15:30:02 -040079std::unique_ptr<SkCodec> SkBmpCodec::MakeFromIco(std::unique_ptr<SkStream> stream, Result* result) {
80 return SkBmpCodec::MakeFromStream(std::move(stream), result, true);
msarett9bde9182015-03-25 05:27:48 -070081}
82
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -050083// Header size constants
Leon Scroggins III862c1962017-10-02 16:28:49 -040084static constexpr uint32_t kBmpHeaderBytes = 14;
85static constexpr uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4;
86static constexpr uint32_t kBmpOS2V1Bytes = 12;
87static constexpr uint32_t kBmpOS2V2Bytes = 64;
88static constexpr uint32_t kBmpInfoBaseBytes = 16;
89static constexpr uint32_t kBmpInfoV1Bytes = 40;
90static constexpr uint32_t kBmpInfoV2Bytes = 52;
91static constexpr uint32_t kBmpInfoV3Bytes = 56;
92static constexpr uint32_t kBmpInfoV4Bytes = 108;
93static constexpr uint32_t kBmpInfoV5Bytes = 124;
94static constexpr uint32_t kBmpMaskBytes = 12;
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -050095
96static BmpHeaderType get_header_type(size_t infoBytes) {
97 if (infoBytes >= kBmpInfoBaseBytes) {
98 // Check the version of the header
99 switch (infoBytes) {
100 case kBmpInfoV1Bytes:
101 return kInfoV1_BmpHeaderType;
102 case kBmpInfoV2Bytes:
103 return kInfoV2_BmpHeaderType;
104 case kBmpInfoV3Bytes:
105 return kInfoV3_BmpHeaderType;
106 case kBmpInfoV4Bytes:
107 return kInfoV4_BmpHeaderType;
108 case kBmpInfoV5Bytes:
109 return kInfoV5_BmpHeaderType;
110 case 16:
111 case 20:
112 case 24:
113 case 28:
114 case 32:
115 case 36:
116 case 42:
117 case 46:
118 case 48:
119 case 60:
120 case kBmpOS2V2Bytes:
121 return kOS2VX_BmpHeaderType;
122 default:
123 SkCodecPrintf("Error: unknown bmp header format.\n");
124 return kUnknown_BmpHeaderType;
125 }
126 } if (infoBytes >= kBmpOS2V1Bytes) {
127 // The OS2V1 is treated separately because it has a unique format
128 return kOS2V1_BmpHeaderType;
129 } else {
130 // There are no valid bmp headers
131 SkCodecPrintf("Error: second bitmap header size is invalid.\n");
132 return kUnknown_BmpHeaderType;
133 }
134}
135
Leon Scroggins III588fb042017-07-14 16:32:31 -0400136SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco,
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400137 std::unique_ptr<SkCodec>* codecOut) {
tomhudson7aa846c2015-03-24 13:47:41 -0700138 // The total bytes in the bmp file
msarett9bde9182015-03-25 05:27:48 -0700139 // We only need to use this value for RLE decoding, so we will only
140 // check that it is valid in the RLE case.
141 uint32_t totalBytes;
tomhudson7aa846c2015-03-24 13:47:41 -0700142 // The offset from the start of the file where the pixel data begins
msarett9bde9182015-03-25 05:27:48 -0700143 uint32_t offset;
144 // The size of the second (info) header in bytes
145 uint32_t infoBytes;
146
147 // Bmps embedded in Icos skip the first Bmp header
msarett4ab9d5f2015-08-06 15:34:42 -0700148 if (!inIco) {
msarett9bde9182015-03-25 05:27:48 -0700149 // Read the first header and the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400150 uint8_t hBuffer[kBmpHeaderBytesPlusFour];
151 if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) !=
msarett9bde9182015-03-25 05:27:48 -0700152 kBmpHeaderBytesPlusFour) {
scroggo230d4ac2015-03-26 07:15:55 -0700153 SkCodecPrintf("Error: unable to read first bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400154 return kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700155 }
156
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400157 totalBytes = get_int(hBuffer, 2);
158 offset = get_int(hBuffer, 10);
msarett9bde9182015-03-25 05:27:48 -0700159 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700160 SkCodecPrintf("Error: invalid starting location for pixel data\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400161 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700162 }
163
164 // The size of the second (info) header in bytes
165 // The size is the first field of the second header, so we have already
166 // read the first four infoBytes.
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400167 infoBytes = get_int(hBuffer, 14);
msarett9bde9182015-03-25 05:27:48 -0700168 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700169 SkCodecPrintf("Error: invalid second header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400170 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700171 }
172 } else {
173 // This value is only used by RLE compression. Bmp in Ico files do not
174 // use RLE. If the compression field is incorrectly signaled as RLE,
175 // we will catch this and signal an error below.
176 totalBytes = 0;
177
178 // Bmps in Ico cannot specify an offset. We will always assume that
179 // pixel data begins immediately after the color table. This value
180 // will be corrected below.
181 offset = 0;
182
183 // Read the size of the second header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400184 uint8_t hBuffer[4];
185 if (stream->read(hBuffer, 4) != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700186 SkCodecPrintf("Error: unable to read size of second bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400187 return kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700188 }
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400189 infoBytes = get_int(hBuffer, 0);
msarett9bde9182015-03-25 05:27:48 -0700190 if (infoBytes < kBmpOS2V1Bytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700191 SkCodecPrintf("Error: invalid second header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400192 return kInvalidInput;
msarett9bde9182015-03-25 05:27:48 -0700193 }
tomhudson7aa846c2015-03-24 13:47:41 -0700194 }
195
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500196 // Determine image information depending on second header format
197 const BmpHeaderType headerType = get_header_type(infoBytes);
198 if (kUnknown_BmpHeaderType == headerType) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400199 return kInvalidInput;
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500200 }
201
msarett9bde9182015-03-25 05:27:48 -0700202 // We already read the first four bytes of the info header to get the size
msarett74114382015-03-16 11:55:18 -0700203 const uint32_t infoBytesRemaining = infoBytes - 4;
msarett74114382015-03-16 11:55:18 -0700204
205 // Read the second header
Ben Wagner7ecc5962016-11-02 17:07:33 -0400206 std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]);
msarett74114382015-03-16 11:55:18 -0700207 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) {
scroggo230d4ac2015-03-26 07:15:55 -0700208 SkCodecPrintf("Error: unable to read second bitmap header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400209 return kIncompleteInput;
msarett74114382015-03-16 11:55:18 -0700210 }
211
212 // The number of bits used per pixel in the pixel data
213 uint16_t bitsPerPixel;
214
215 // The compression method for the pixel data
msarett4ab9d5f2015-08-06 15:34:42 -0700216 uint32_t compression = kNone_BmpCompressionMethod;
msarett74114382015-03-16 11:55:18 -0700217
218 // Number of colors in the color table, defaults to 0 or max (see below)
219 uint32_t numColors = 0;
220
221 // Bytes per color in the color table, early versions use 3, most use 4
222 uint32_t bytesPerColor;
223
224 // The image width and height
225 int width, height;
226
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500227 switch (headerType) {
228 case kInfoV1_BmpHeaderType:
229 case kInfoV2_BmpHeaderType:
230 case kInfoV3_BmpHeaderType:
231 case kInfoV4_BmpHeaderType:
232 case kInfoV5_BmpHeaderType:
233 case kOS2VX_BmpHeaderType:
234 // We check the size of the header before entering the if statement.
235 // We should not reach this point unless the size is large enough for
236 // these required fields.
237 SkASSERT(infoBytesRemaining >= 12);
238 width = get_int(iBuffer.get(), 0);
239 height = get_int(iBuffer.get(), 4);
240 bitsPerPixel = get_short(iBuffer.get(), 10);
msarett74114382015-03-16 11:55:18 -0700241
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500242 // Some versions do not have these fields, so we check before
243 // overwriting the default value.
244 if (infoBytesRemaining >= 16) {
245 compression = get_int(iBuffer.get(), 12);
246 if (infoBytesRemaining >= 32) {
247 numColors = get_int(iBuffer.get(), 28);
248 }
msarett74114382015-03-16 11:55:18 -0700249 }
msarett74114382015-03-16 11:55:18 -0700250
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -0500251 // All of the headers that reach this point, store color table entries
252 // using 4 bytes per pixel.
253 bytesPerColor = 4;
254 break;
255 case kOS2V1_BmpHeaderType:
256 // The OS2V1 is treated separately because it has a unique format
257 width = (int) get_short(iBuffer.get(), 0);
258 height = (int) get_short(iBuffer.get(), 2);
259 bitsPerPixel = get_short(iBuffer.get(), 6);
260 bytesPerColor = 3;
261 break;
262 case kUnknown_BmpHeaderType:
263 // We'll exit above in this case.
264 SkASSERT(false);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400265 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700266 }
267
268 // Check for valid dimensions from header
scroggo46c57472015-09-30 08:57:13 -0700269 SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700270 if (height < 0) {
Leon Scroggins IIIce6d93a2018-02-15 09:25:11 -0500271 // We can't negate INT32_MIN.
272 if (height == INT32_MIN) {
273 return kInvalidInput;
274 }
275
msarett74114382015-03-16 11:55:18 -0700276 height = -height;
scroggo46c57472015-09-30 08:57:13 -0700277 rowOrder = SkCodec::kTopDown_SkScanlineOrder;
msarett74114382015-03-16 11:55:18 -0700278 }
msarett9bde9182015-03-25 05:27:48 -0700279 // The height field for bmp in ico is double the actual height because they
280 // contain an XOR mask followed by an AND mask
msarett4ab9d5f2015-08-06 15:34:42 -0700281 if (inIco) {
msarett9bde9182015-03-25 05:27:48 -0700282 height /= 2;
283 }
Leon Scroggins III0354c622017-02-24 15:33:24 -0500284
285 // Arbitrary maximum. Matches Chromium.
286 constexpr int kMaxDim = 1 << 16;
287 if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) {
scroggo230d4ac2015-03-26 07:15:55 -0700288 SkCodecPrintf("Error: invalid bitmap dimensions.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400289 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700290 }
291
292 // Create mask struct
293 SkMasks::InputMasks inputMasks;
msaretteed039b2015-03-18 11:11:19 -0700294 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks));
msarett74114382015-03-16 11:55:18 -0700295
296 // Determine the input compression format and set bit masks if necessary
297 uint32_t maskBytes = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700298 BmpInputFormat inputFormat = kUnknown_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700299 switch (compression) {
msarett4ab9d5f2015-08-06 15:34:42 -0700300 case kNone_BmpCompressionMethod:
301 inputFormat = kStandard_BmpInputFormat;
msarett1088db92016-03-22 08:58:35 -0700302
303 // In addition to more standard pixel compression formats, bmp supports
304 // the use of bit masks to determine pixel components. The standard
305 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB),
306 // which does not map well to any Skia color formats. For this reason,
307 // we will always enable mask mode with 16 bits per pixel.
308 if (16 == bitsPerPixel) {
309 inputMasks.red = 0x7C00;
310 inputMasks.green = 0x03E0;
311 inputMasks.blue = 0x001F;
312 inputFormat = kBitMask_BmpInputFormat;
313 }
msarett74114382015-03-16 11:55:18 -0700314 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700315 case k8BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700316 if (bitsPerPixel != 8) {
scroggo230d4ac2015-03-26 07:15:55 -0700317 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700318 bitsPerPixel = 8;
319 }
msarett4ab9d5f2015-08-06 15:34:42 -0700320 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700321 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700322 case k4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700323 if (bitsPerPixel != 4) {
scroggo230d4ac2015-03-26 07:15:55 -0700324 SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
msarett74114382015-03-16 11:55:18 -0700325 bitsPerPixel = 4;
326 }
msarett4ab9d5f2015-08-06 15:34:42 -0700327 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700328 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700329 case kAlphaBitMasks_BmpCompressionMethod:
330 case kBitMasks_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700331 // Load the masks
msarett4ab9d5f2015-08-06 15:34:42 -0700332 inputFormat = kBitMask_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700333 switch (headerType) {
msarett4ab9d5f2015-08-06 15:34:42 -0700334 case kInfoV1_BmpHeaderType: {
msarett74114382015-03-16 11:55:18 -0700335 // The V1 header stores the bit masks after the header
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400336 uint8_t buffer[kBmpMaskBytes];
337 if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) {
scroggo230d4ac2015-03-26 07:15:55 -0700338 SkCodecPrintf("Error: unable to read bit inputMasks.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400339 return kIncompleteInput;
msarett74114382015-03-16 11:55:18 -0700340 }
341 maskBytes = kBmpMaskBytes;
Leon Scroggins IIIec4400b2017-06-01 14:06:35 -0400342 inputMasks.red = get_int(buffer, 0);
343 inputMasks.green = get_int(buffer, 4);
344 inputMasks.blue = get_int(buffer, 8);
msarett74114382015-03-16 11:55:18 -0700345 break;
346 }
msarett4ab9d5f2015-08-06 15:34:42 -0700347 case kInfoV2_BmpHeaderType:
348 case kInfoV3_BmpHeaderType:
349 case kInfoV4_BmpHeaderType:
350 case kInfoV5_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700351 // Header types are matched based on size. If the header
352 // is V2+, we are guaranteed to be able to read at least
353 // this size.
354 SkASSERT(infoBytesRemaining >= 48);
355 inputMasks.red = get_int(iBuffer.get(), 36);
356 inputMasks.green = get_int(iBuffer.get(), 40);
357 inputMasks.blue = get_int(iBuffer.get(), 44);
msarett1088db92016-03-22 08:58:35 -0700358
359 if (kInfoV2_BmpHeaderType == headerType ||
360 (kInfoV3_BmpHeaderType == headerType && !inIco)) {
361 break;
362 }
363
364 // V3+ bmp files introduce an alpha mask and allow the creator of the image
365 // to use the alpha channels. However, many of these images leave the
366 // alpha channel blank and expect to be rendered as opaque. This is the
367 // case for almost all V3 images, so we ignore the alpha mask. For V4+
368 // images in kMask mode, we will use the alpha mask. Additionally, V3
369 // bmp-in-ico expect us to use the alpha mask.
370 //
371 // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard
372 // mode. We just haven't seen any images that expect this
373 // behavior.
374 //
375 // Header types are matched based on size. If the header is
376 // V3+, we are guaranteed to be able to read at least this size.
Leon Scroggins III57862f62018-10-23 11:17:38 -0400377 SkASSERT(infoBytesRemaining >= 52);
msarett1088db92016-03-22 08:58:35 -0700378 inputMasks.alpha = get_int(iBuffer.get(), 48);
msarett74114382015-03-16 11:55:18 -0700379 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700380 case kOS2VX_BmpHeaderType:
msarett74114382015-03-16 11:55:18 -0700381 // TODO: Decide if we intend to support this.
382 // It is unsupported in the previous version and
383 // in chromium. I have not come across a test case
384 // that uses this format.
scroggo230d4ac2015-03-26 07:15:55 -0700385 SkCodecPrintf("Error: huffman format unsupported.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400386 return kUnimplemented;
msarett74114382015-03-16 11:55:18 -0700387 default:
scroggo230d4ac2015-03-26 07:15:55 -0700388 SkCodecPrintf("Error: invalid bmp bit masks header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400389 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700390 }
391 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700392 case kJpeg_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700393 if (24 == bitsPerPixel) {
msarett4ab9d5f2015-08-06 15:34:42 -0700394 inputFormat = kRLE_BmpInputFormat;
msarett74114382015-03-16 11:55:18 -0700395 break;
396 }
397 // Fall through
msarett4ab9d5f2015-08-06 15:34:42 -0700398 case kPng_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700399 // TODO: Decide if we intend to support this.
400 // It is unsupported in the previous version and
401 // in chromium. I think it is used mostly for printers.
scroggo230d4ac2015-03-26 07:15:55 -0700402 SkCodecPrintf("Error: compression format not supported.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400403 return kUnimplemented;
msarett4ab9d5f2015-08-06 15:34:42 -0700404 case kCMYK_BmpCompressionMethod:
405 case kCMYK8BitRLE_BmpCompressionMethod:
406 case kCMYK4BitRLE_BmpCompressionMethod:
msarett74114382015-03-16 11:55:18 -0700407 // TODO: Same as above.
scroggo230d4ac2015-03-26 07:15:55 -0700408 SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400409 return kUnimplemented;
msarett74114382015-03-16 11:55:18 -0700410 default:
scroggo230d4ac2015-03-26 07:15:55 -0700411 SkCodecPrintf("Error: invalid format for bitmap decoding.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400412 return kInvalidInput;
msarett74114382015-03-16 11:55:18 -0700413 }
mtklein852f15d2016-03-17 10:51:27 -0700414 iBuffer.reset();
msarett74114382015-03-16 11:55:18 -0700415
msaretteed039b2015-03-18 11:11:19 -0700416 // Calculate the number of bytes read so far
417 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes;
msarett4ab9d5f2015-08-06 15:34:42 -0700418 if (!inIco && offset < bytesRead) {
msarett9aa32d12015-09-01 14:40:46 -0700419 // TODO (msarett): Do we really want to fail if the offset in the header is invalid?
420 // Seems like we can just assume that the offset is zero and try to decode?
421 // Maybe we don't want to try to decode corrupt images?
scroggo230d4ac2015-03-26 07:15:55 -0700422 SkCodecPrintf("Error: pixel data offset less than header size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400423 return kInvalidInput;
msaretteed039b2015-03-18 11:11:19 -0700424 }
msarett74114382015-03-16 11:55:18 -0700425
msarett9aa32d12015-09-01 14:40:46 -0700426
msarettf4004f92016-02-11 10:49:31 -0800427
msarett1088db92016-03-22 08:58:35 -0700428 switch (inputFormat) {
429 case kStandard_BmpInputFormat: {
msarettc30c4182016-04-20 11:53:35 -0700430 // BMPs are generally opaque, however BMPs-in-ICOs may contain
431 // a transparency mask after the image. Therefore, we mark the
432 // alpha as kBinary if the BMP is contained in an ICO.
433 // We use |isOpaque| to indicate if the BMP itself is opaque.
434 SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
435 SkEncodedInfo::kOpaque_Alpha;
msarett1088db92016-03-22 08:58:35 -0700436 bool isOpaque = true;
msarettc30c4182016-04-20 11:53:35 -0700437
438 SkEncodedInfo::Color color;
439 uint8_t bitsPerComponent;
msarett1088db92016-03-22 08:58:35 -0700440 switch (bitsPerPixel) {
441 // Palette formats
442 case 1:
443 case 2:
444 case 4:
445 case 8:
msarettc30c4182016-04-20 11:53:35 -0700446 // In the case of ICO, kBGRA is actually the closest match,
447 // since we will need to apply a transparency mask.
448 if (inIco) {
449 color = SkEncodedInfo::kBGRA_Color;
450 bitsPerComponent = 8;
451 } else {
452 color = SkEncodedInfo::kPalette_Color;
453 bitsPerComponent = (uint8_t) bitsPerPixel;
msarett1088db92016-03-22 08:58:35 -0700454 }
455 break;
456 case 24:
msarett3e375b02016-05-04 13:03:48 -0700457 // In the case of ICO, kBGRA is actually the closest match,
458 // since we will need to apply a transparency mask.
459 color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color;
msarettc30c4182016-04-20 11:53:35 -0700460 bitsPerComponent = 8;
461 break;
msarett1088db92016-03-22 08:58:35 -0700462 case 32:
463 // 32-bit BMP-in-ICOs actually use the alpha channel in place of a
464 // transparency mask.
465 if (inIco) {
466 isOpaque = false;
msarettc30c4182016-04-20 11:53:35 -0700467 alpha = SkEncodedInfo::kUnpremul_Alpha;
468 color = SkEncodedInfo::kBGRA_Color;
469 } else {
470 color = SkEncodedInfo::kBGRX_Color;
msarett1088db92016-03-22 08:58:35 -0700471 }
msarettc30c4182016-04-20 11:53:35 -0700472 bitsPerComponent = 8;
msarett1088db92016-03-22 08:58:35 -0700473 break;
474 default:
475 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400476 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700477 }
msarett4ab9d5f2015-08-06 15:34:42 -0700478
msarett1088db92016-03-22 08:58:35 -0700479 if (codecOut) {
msarettbe8216a2015-12-04 08:00:50 -0800480 // We require streams to have a memory base for Bmp-in-Ico decodes.
481 SkASSERT(!inIco || nullptr != stream->getMemoryBase());
msarett1088db92016-03-22 08:58:35 -0700482
483 // Set the image info and create a codec.
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400484 auto info = SkEncodedInfo::Make(width, height, color, alpha, bitsPerComponent);
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400485 codecOut->reset(new SkBmpStandardCodec(std::move(info),
Mike Reedede7bac2017-07-23 15:30:02 -0400486 std::unique_ptr<SkStream>(stream),
487 bitsPerPixel, numColors, bytesPerColor,
488 offset - bytesRead, rowOrder, isOpaque,
489 inIco));
Leon Scroggins III588fb042017-07-14 16:32:31 -0400490 return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer()
491 ? kSuccess : kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700492 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400493 return kSuccess;
msarett1088db92016-03-22 08:58:35 -0700494 }
495
496 case kBitMask_BmpInputFormat: {
497 // Bmp-in-Ico must be standard mode
498 if (inIco) {
499 SkCodecPrintf("Error: Icos may not use bit mask format.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400500 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700501 }
502
503 switch (bitsPerPixel) {
504 case 16:
505 case 24:
506 case 32:
507 break;
508 default:
509 SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400510 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700511 }
512
513 // Skip to the start of the pixel array.
514 // We can do this here because there is no color table to read
515 // in bit mask mode.
516 if (stream->skip(offset - bytesRead) != offset - bytesRead) {
517 SkCodecPrintf("Error: unable to skip to image data.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400518 return kIncompleteInput;
msarett1088db92016-03-22 08:58:35 -0700519 }
520
521 if (codecOut) {
522 // Check that input bit masks are valid and create the masks object
Ben Wagner145dbcd2016-11-03 14:40:50 -0400523 std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel));
msarett1088db92016-03-22 08:58:35 -0700524 if (nullptr == masks) {
525 SkCodecPrintf("Error: invalid input masks.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400526 return kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700527 }
msarett4ab9d5f2015-08-06 15:34:42 -0700528
msarettc30c4182016-04-20 11:53:35 -0700529 // Masked bmps are not a great fit for SkEncodedInfo, since they have
530 // arbitrary component orderings and bits per component. Here we choose
531 // somewhat reasonable values - it's ok that we don't match exactly
532 // because SkBmpMaskCodec has its own mask swizzler anyway.
533 SkEncodedInfo::Color color;
534 SkEncodedInfo::Alpha alpha;
535 if (masks->getAlphaMask()) {
536 color = SkEncodedInfo::kBGRA_Color;
537 alpha = SkEncodedInfo::kUnpremul_Alpha;
538 } else {
539 color = SkEncodedInfo::kBGR_Color;
540 alpha = SkEncodedInfo::kOpaque_Alpha;
541 }
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400542 auto info = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400543 codecOut->reset(new SkBmpMaskCodec(std::move(info),
Mike Reedede7bac2017-07-23 15:30:02 -0400544 std::unique_ptr<SkStream>(stream), bitsPerPixel,
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400545 masks.release(), rowOrder));
Leon Scroggins III588fb042017-07-14 16:32:31 -0400546 return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer()
547 ? kSuccess : kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700548 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400549 return kSuccess;
msarett1088db92016-03-22 08:58:35 -0700550 }
551
552 case kRLE_BmpInputFormat: {
553 // We should not reach this point without a valid value of bitsPerPixel.
554 SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel);
555
556 // Check for a valid number of total bytes when in RLE mode
557 if (totalBytes <= offset) {
558 SkCodecPrintf("Error: RLE requires valid input size.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400559 return kInvalidInput;
msarett1088db92016-03-22 08:58:35 -0700560 }
msarett1088db92016-03-22 08:58:35 -0700561
562 // Bmp-in-Ico must be standard mode
563 // When inIco is true, this line cannot be reached, since we
564 // require that RLE Bmps have a valid number of totalBytes, and
565 // Icos skip the header that contains totalBytes.
566 SkASSERT(!inIco);
567
568 if (codecOut) {
569 // RLE inputs may skip pixels, leaving them as transparent. This
570 // is uncommon, but we cannot be certain that an RLE bmp will be
msarettc30c4182016-04-20 11:53:35 -0700571 // opaque or that we will be able to represent it with a palette.
572 // For that reason, we always indicate that we are kBGRA.
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400573 auto info = SkEncodedInfo::Make(width, height, SkEncodedInfo::kBGRA_Color,
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400574 SkEncodedInfo::kBinary_Alpha, 8);
575 codecOut->reset(new SkBmpRLECodec(std::move(info),
Mike Reedede7bac2017-07-23 15:30:02 -0400576 std::unique_ptr<SkStream>(stream), bitsPerPixel,
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400577 numColors, bytesPerColor, offset - bytesRead,
578 rowOrder));
msarett1088db92016-03-22 08:58:35 -0700579 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400580 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700581 }
msarett1088db92016-03-22 08:58:35 -0700582 default:
583 SkASSERT(false);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400584 return kInvalidInput;
scroggo79e378d2015-04-01 07:39:40 -0700585 }
scroggo79e378d2015-04-01 07:39:40 -0700586}
587
588/*
scroggo79e378d2015-04-01 07:39:40 -0700589 * Creates a bmp decoder
590 * Reads enough of the stream to determine the image format
scroggo79e378d2015-04-01 07:39:40 -0700591 */
Mike Reedede7bac2017-07-23 15:30:02 -0400592std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
593 Result* result, bool inIco) {
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400594 std::unique_ptr<SkCodec> codec;
Mike Reedede7bac2017-07-23 15:30:02 -0400595 *result = ReadHeader(stream.get(), inIco, &codec);
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400596 if (codec) {
Mike Reedede7bac2017-07-23 15:30:02 -0400597 // codec has taken ownership of stream, so we do not need to delete it.
598 stream.release();
scroggo79e378d2015-04-01 07:39:40 -0700599 }
Mike Reedede7bac2017-07-23 15:30:02 -0400600 return kSuccess == *result ? std::move(codec) : nullptr;
msarett74114382015-03-16 11:55:18 -0700601}
602
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400603SkBmpCodec::SkBmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
scroggo46c57472015-09-30 08:57:13 -0700604 uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400605 : INHERITED(std::move(info), kXformSrcColorFormat, std::move(stream))
msarett74114382015-03-16 11:55:18 -0700606 , fBitsPerPixel(bitsPerPixel)
msarett74114382015-03-16 11:55:18 -0700607 , fRowOrder(rowOrder)
Leon Scroggins III712476e2018-10-03 15:47:00 -0400608 , fSrcRowBytes(SkAlign4(compute_row_bytes(this->dimensions().width(), fBitsPerPixel)))
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400609 , fXformBuffer(nullptr)
msarett74114382015-03-16 11:55:18 -0700610{}
611
scroggob427db12015-08-12 07:24:13 -0700612bool SkBmpCodec::onRewind() {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400613 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr) == kSuccess;
msaretteed039b2015-03-18 11:11:19 -0700614}
615
scroggo46c57472015-09-30 08:57:13 -0700616int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const {
617 if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) {
msarett5406d6f2015-08-31 06:55:13 -0700618 return y;
619 }
scroggo46c57472015-09-30 08:57:13 -0700620 SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder);
msarett5406d6f2015-08-31 06:55:13 -0700621 return height - y - 1;
622}
623
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400624SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000625 const SkCodec::Options& options) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000626 return this->onPrepareToDecode(dstInfo, options);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400627}
628
629SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000630 const SkCodec::Options& options) {
631 return prepareToDecode(dstInfo, options);
scroggo46c57472015-09-30 08:57:13 -0700632}
msarett5406d6f2015-08-31 06:55:13 -0700633
msarette6dd0042015-10-09 11:07:34 -0700634int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700635 // Create a new image info representing the portion of the image to decode
636 SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count);
msarett5406d6f2015-08-31 06:55:13 -0700637
scroggo46c57472015-09-30 08:57:13 -0700638 // Decode the requested rows
639 return this->decodeRows(rowInfo, dst, rowBytes, this->options());
640}
msarett9b9497e2016-02-11 13:29:36 -0800641
642bool SkBmpCodec::skipRows(int count) {
643 const size_t bytesToSkip = count * fSrcRowBytes;
644 return this->stream()->skip(bytesToSkip) == bytesToSkip;
645}
646
647bool SkBmpCodec::onSkipScanlines(int count) {
648 return this->skipRows(count);
649}