msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
msarett | b46e5e2 | 2015-07-30 11:36:40 -0700 | [diff] [blame] | 8 | #include "SkBmpCodec.h" |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 9 | #include "SkBmpMaskCodec.h" |
| 10 | #include "SkBmpRLECodec.h" |
| 11 | #include "SkBmpStandardCodec.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 12 | #include "SkCodecPriv.h" |
| 13 | #include "SkColorPriv.h" |
| 14 | #include "SkStream.h" |
| 15 | |
| 16 | /* |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 17 | * Defines the version and type of the second bitmap header |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 18 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 19 | enum 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 |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /* |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 31 | * Possible bitmap compression types |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 32 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 33 | enum 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 |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 47 | * Used to define the input format of the bmp |
| 48 | */ |
| 49 | enum BmpInputFormat { |
| 50 | kStandard_BmpInputFormat, |
| 51 | kRLE_BmpInputFormat, |
| 52 | kBitMask_BmpInputFormat, |
| 53 | kUnknown_BmpInputFormat |
| 54 | }; |
| 55 | |
| 56 | /* |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 57 | * Checks the start of the stream to see if the image is a bitmap |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 58 | */ |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 59 | bool SkBmpCodec::IsBmp(const void* buffer, size_t bytesRead) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 60 | // TODO: Support "IC", "PT", "CI", "CP", "BA" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 61 | const char bmpSig[] = { 'B', 'M' }; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 62 | return bytesRead >= sizeof(bmpSig) && !memcmp(buffer, bmpSig, sizeof(bmpSig)); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 66 | * Assumes IsBmp was called and returned true |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 67 | * Creates a bmp decoder |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 68 | * Reads enough of the stream to determine the image format |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 69 | */ |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 70 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result) { |
| 71 | return SkBmpCodec::NewFromStream(stream, result, false); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 75 | * Creates a bmp decoder for a bmp embedded in ico |
| 76 | * Reads enough of the stream to determine the image format |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 77 | */ |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 78 | SkCodec* SkBmpCodec::NewFromIco(SkStream* stream, Result* result) { |
| 79 | return SkBmpCodec::NewFromStream(stream, result, true); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 82 | // Header size constants |
| 83 | static const uint32_t kBmpHeaderBytes = 14; |
| 84 | static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
| 85 | static const uint32_t kBmpOS2V1Bytes = 12; |
| 86 | static const uint32_t kBmpOS2V2Bytes = 64; |
| 87 | static const uint32_t kBmpInfoBaseBytes = 16; |
| 88 | static const uint32_t kBmpInfoV1Bytes = 40; |
| 89 | static const uint32_t kBmpInfoV2Bytes = 52; |
| 90 | static const uint32_t kBmpInfoV3Bytes = 56; |
| 91 | static const uint32_t kBmpInfoV4Bytes = 108; |
| 92 | static const uint32_t kBmpInfoV5Bytes = 124; |
| 93 | static const uint32_t kBmpMaskBytes = 12; |
| 94 | |
| 95 | static 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 135 | SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 136 | std::unique_ptr<SkCodec>* codecOut) { |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 137 | // The total bytes in the bmp file |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 138 | // 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; |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 141 | // The offset from the start of the file where the pixel data begins |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 142 | 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 |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 147 | if (!inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 148 | // Read the first header and the size of the second header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 149 | uint8_t hBuffer[kBmpHeaderBytesPlusFour]; |
| 150 | if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) != |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 151 | kBmpHeaderBytesPlusFour) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 152 | SkCodecPrintf("Error: unable to read first bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 153 | return kIncompleteInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 156 | totalBytes = get_int(hBuffer, 2); |
| 157 | offset = get_int(hBuffer, 10); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 158 | if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 159 | SkCodecPrintf("Error: invalid starting location for pixel data\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 160 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 161 | } |
| 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 III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 166 | infoBytes = get_int(hBuffer, 14); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 167 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 168 | SkCodecPrintf("Error: invalid second header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 169 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 170 | } |
| 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 III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 183 | uint8_t hBuffer[4]; |
| 184 | if (stream->read(hBuffer, 4) != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 185 | SkCodecPrintf("Error: unable to read size of second bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 186 | return kIncompleteInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 187 | } |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 188 | infoBytes = get_int(hBuffer, 0); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 189 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 190 | SkCodecPrintf("Error: invalid second header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 191 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 192 | } |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 195 | // Determine image information depending on second header format |
| 196 | const BmpHeaderType headerType = get_header_type(infoBytes); |
| 197 | if (kUnknown_BmpHeaderType == headerType) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 198 | return kInvalidInput; |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 199 | } |
| 200 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 201 | // We already read the first four bytes of the info header to get the size |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 202 | const uint32_t infoBytesRemaining = infoBytes - 4; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 203 | |
| 204 | // Read the second header |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 205 | std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 206 | if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 207 | SkCodecPrintf("Error: unable to read second bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 208 | return kIncompleteInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 209 | } |
| 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 |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 215 | uint32_t compression = kNone_BmpCompressionMethod; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 216 | |
| 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 III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 226 | 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); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 240 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 241 | // 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 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 248 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 249 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 250 | // 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 264 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Check for valid dimensions from header |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 268 | SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 269 | if (height < 0) { |
| 270 | height = -height; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 271 | rowOrder = SkCodec::kTopDown_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 272 | } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 273 | // The height field for bmp in ico is double the actual height because they |
| 274 | // contain an XOR mask followed by an AND mask |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 275 | if (inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 276 | height /= 2; |
| 277 | } |
Leon Scroggins III | 0354c62 | 2017-02-24 15:33:24 -0500 | [diff] [blame] | 278 | |
| 279 | // Arbitrary maximum. Matches Chromium. |
| 280 | constexpr int kMaxDim = 1 << 16; |
| 281 | if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 282 | SkCodecPrintf("Error: invalid bitmap dimensions.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 283 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Create mask struct |
| 287 | SkMasks::InputMasks inputMasks; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 288 | memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 289 | |
| 290 | // Determine the input compression format and set bit masks if necessary |
| 291 | uint32_t maskBytes = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 292 | BmpInputFormat inputFormat = kUnknown_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 293 | switch (compression) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 294 | case kNone_BmpCompressionMethod: |
| 295 | inputFormat = kStandard_BmpInputFormat; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 296 | |
| 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 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 308 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 309 | case k8BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 310 | if (bitsPerPixel != 8) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 311 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 312 | bitsPerPixel = 8; |
| 313 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 314 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 315 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 316 | case k4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 317 | if (bitsPerPixel != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 318 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 319 | bitsPerPixel = 4; |
| 320 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 321 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 322 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 323 | case kAlphaBitMasks_BmpCompressionMethod: |
| 324 | case kBitMasks_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 325 | // Load the masks |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 326 | inputFormat = kBitMask_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 327 | switch (headerType) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 328 | case kInfoV1_BmpHeaderType: { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 329 | // The V1 header stores the bit masks after the header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 330 | uint8_t buffer[kBmpMaskBytes]; |
| 331 | if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 332 | SkCodecPrintf("Error: unable to read bit inputMasks.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 333 | return kIncompleteInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 334 | } |
| 335 | maskBytes = kBmpMaskBytes; |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 336 | inputMasks.red = get_int(buffer, 0); |
| 337 | inputMasks.green = get_int(buffer, 4); |
| 338 | inputMasks.blue = get_int(buffer, 8); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 339 | break; |
| 340 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 341 | case kInfoV2_BmpHeaderType: |
| 342 | case kInfoV3_BmpHeaderType: |
| 343 | case kInfoV4_BmpHeaderType: |
| 344 | case kInfoV5_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 345 | // 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); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 352 | |
| 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); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 373 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 374 | case kOS2VX_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 375 | // 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. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 379 | SkCodecPrintf("Error: huffman format unsupported.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 380 | return kUnimplemented; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 381 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 382 | SkCodecPrintf("Error: invalid bmp bit masks header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 383 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 384 | } |
| 385 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 386 | case kJpeg_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 387 | if (24 == bitsPerPixel) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 388 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 389 | break; |
| 390 | } |
| 391 | // Fall through |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 392 | case kPng_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 393 | // 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. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 396 | SkCodecPrintf("Error: compression format not supported.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 397 | return kUnimplemented; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 398 | case kCMYK_BmpCompressionMethod: |
| 399 | case kCMYK8BitRLE_BmpCompressionMethod: |
| 400 | case kCMYK4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 401 | // TODO: Same as above. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 402 | SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 403 | return kUnimplemented; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 404 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 405 | SkCodecPrintf("Error: invalid format for bitmap decoding.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 406 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 407 | } |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 408 | iBuffer.reset(); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 409 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 410 | // Calculate the number of bytes read so far |
| 411 | const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 412 | if (!inIco && offset < bytesRead) { |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 413 | // 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? |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 416 | SkCodecPrintf("Error: pixel data offset less than header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 417 | return kInvalidInput; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 418 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 419 | |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 420 | |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 421 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 422 | switch (inputFormat) { |
| 423 | case kStandard_BmpInputFormat: { |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 424 | // 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; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 430 | bool isOpaque = true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 431 | |
| 432 | SkEncodedInfo::Color color; |
| 433 | uint8_t bitsPerComponent; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 434 | switch (bitsPerPixel) { |
| 435 | // Palette formats |
| 436 | case 1: |
| 437 | case 2: |
| 438 | case 4: |
| 439 | case 8: |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 440 | // 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; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 448 | } |
| 449 | break; |
| 450 | case 24: |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 451 | // 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; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 454 | bitsPerComponent = 8; |
| 455 | break; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 456 | 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; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 461 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 462 | color = SkEncodedInfo::kBGRA_Color; |
| 463 | } else { |
| 464 | color = SkEncodedInfo::kBGRX_Color; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 465 | } |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 466 | bitsPerComponent = 8; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 470 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 471 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 472 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 473 | if (codecOut) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 474 | // We require streams to have a memory base for Bmp-in-Ico decodes. |
| 475 | SkASSERT(!inIco || nullptr != stream->getMemoryBase()); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 476 | |
| 477 | // Set the image info and create a codec. |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 478 | const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 479 | codecOut->reset(new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel, |
| 480 | numColors, bytesPerColor, offset - bytesRead, |
| 481 | rowOrder, isOpaque, inIco)); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 482 | return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer() |
| 483 | ? kSuccess : kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 484 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 485 | return kSuccess; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 486 | } |
| 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 492 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 493 | } |
| 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 502 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 503 | } |
| 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 510 | return kIncompleteInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (codecOut) { |
| 514 | // Check that input bit masks are valid and create the masks object |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 515 | std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 516 | if (nullptr == masks) { |
| 517 | SkCodecPrintf("Error: invalid input masks.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 518 | return kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 519 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 520 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 521 | // 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 III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 535 | codecOut->reset(new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel, |
| 536 | masks.release(), rowOrder)); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 537 | return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer() |
| 538 | ? kSuccess : kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 539 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 540 | return kSuccess; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 541 | } |
| 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 550 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 551 | } |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 552 | |
| 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 |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 562 | // 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 III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 566 | codecOut->reset(new SkBmpRLECodec(width, height, info, stream, bitsPerPixel, |
| 567 | numColors, bytesPerColor, offset - bytesRead, |
| 568 | rowOrder)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 569 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 570 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 571 | } |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 572 | default: |
| 573 | SkASSERT(false); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 574 | return kInvalidInput; |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 575 | } |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 579 | * Creates a bmp decoder |
| 580 | * Reads enough of the stream to determine the image format |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 581 | */ |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 582 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result, bool inIco) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 583 | std::unique_ptr<SkStream> streamDeleter(stream); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 584 | std::unique_ptr<SkCodec> codec; |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 585 | *result = ReadHeader(stream, inIco, &codec); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 586 | if (codec) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 587 | // codec has taken ownership of stream, so we do not need to |
| 588 | // delete it. |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 589 | streamDeleter.release(); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 590 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 591 | return kSuccess == *result ? codec.release() : nullptr; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 592 | } |
| 593 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 594 | SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 595 | uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 596 | : INHERITED(width, height, info, kXformSrcColorFormat, stream, SkColorSpace::MakeSRGB()) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 597 | , fBitsPerPixel(bitsPerPixel) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 598 | , fRowOrder(rowOrder) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 599 | , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel))) |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 600 | , fXformBuffer(nullptr) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 601 | {} |
| 602 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 603 | bool SkBmpCodec::onRewind() { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 604 | return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr) == kSuccess; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 605 | } |
| 606 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 607 | int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const { |
| 608 | if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 609 | return y; |
| 610 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 611 | SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 612 | return height - y - 1; |
| 613 | } |
| 614 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 615 | SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 616 | const SkCodec::Options& options) { |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 617 | if (!conversion_possible(dstInfo, this->getInfo()) || |
| 618 | !this->initializeColorXform(dstInfo, options.fPremulBehavior)) |
| 619 | { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 620 | return kInvalidConversion; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 623 | return this->onPrepareToDecode(dstInfo, options); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 627 | const SkCodec::Options& options) { |
| 628 | return prepareToDecode(dstInfo, options); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 629 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 630 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 631 | int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 632 | // Create a new image info representing the portion of the image to decode |
| 633 | SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 634 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 635 | // Decode the requested rows |
| 636 | return this->decodeRows(rowInfo, dst, rowBytes, this->options()); |
| 637 | } |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 638 | |
| 639 | bool SkBmpCodec::skipRows(int count) { |
| 640 | const size_t bytesToSkip = count * fSrcRowBytes; |
| 641 | return this->stream()->skip(bytesToSkip) == bytesToSkip; |
| 642 | } |
| 643 | |
| 644 | bool SkBmpCodec::onSkipScanlines(int count) { |
| 645 | return this->skipRows(count); |
| 646 | } |