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" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 13 | #include "SkColorData.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 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 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 70 | std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 71 | Result* result) { |
| 72 | return SkBmpCodec::MakeFromStream(std::move(stream), result, false); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 76 | * Creates a bmp decoder for a bmp embedded in ico |
| 77 | * Reads enough of the stream to determine the image format |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 78 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 79 | std::unique_ptr<SkCodec> SkBmpCodec::MakeFromIco(std::unique_ptr<SkStream> stream, Result* result) { |
| 80 | return SkBmpCodec::MakeFromStream(std::move(stream), result, true); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 83 | // Header size constants |
Leon Scroggins III | 862c196 | 2017-10-02 16:28:49 -0400 | [diff] [blame] | 84 | static constexpr uint32_t kBmpHeaderBytes = 14; |
| 85 | static constexpr uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
| 86 | static constexpr uint32_t kBmpOS2V1Bytes = 12; |
| 87 | static constexpr uint32_t kBmpOS2V2Bytes = 64; |
| 88 | static constexpr uint32_t kBmpInfoBaseBytes = 16; |
| 89 | static constexpr uint32_t kBmpInfoV1Bytes = 40; |
| 90 | static constexpr uint32_t kBmpInfoV2Bytes = 52; |
| 91 | static constexpr uint32_t kBmpInfoV3Bytes = 56; |
| 92 | static constexpr uint32_t kBmpInfoV4Bytes = 108; |
| 93 | static constexpr uint32_t kBmpInfoV5Bytes = 124; |
| 94 | static constexpr uint32_t kBmpMaskBytes = 12; |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 95 | |
| 96 | static 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 136 | SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 137 | std::unique_ptr<SkCodec>* codecOut) { |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 138 | // The total bytes in the bmp file |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 139 | // 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; |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 142 | // The offset from the start of the file where the pixel data begins |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 143 | 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 |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 148 | if (!inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 149 | // Read the first header and the size of the second header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 150 | uint8_t hBuffer[kBmpHeaderBytesPlusFour]; |
| 151 | if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) != |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 152 | kBmpHeaderBytesPlusFour) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 153 | SkCodecPrintf("Error: unable to read first bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 154 | return kIncompleteInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 157 | totalBytes = get_int(hBuffer, 2); |
| 158 | offset = get_int(hBuffer, 10); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 159 | if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 160 | SkCodecPrintf("Error: invalid starting location for pixel data\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 161 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 162 | } |
| 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 III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 167 | infoBytes = get_int(hBuffer, 14); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 168 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 169 | SkCodecPrintf("Error: invalid second header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 170 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 171 | } |
| 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 III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 184 | uint8_t hBuffer[4]; |
| 185 | if (stream->read(hBuffer, 4) != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 186 | SkCodecPrintf("Error: unable to read size of second bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 187 | return kIncompleteInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 188 | } |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 189 | infoBytes = get_int(hBuffer, 0); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 190 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 191 | SkCodecPrintf("Error: invalid second header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 192 | return kInvalidInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 193 | } |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 196 | // Determine image information depending on second header format |
| 197 | const BmpHeaderType headerType = get_header_type(infoBytes); |
| 198 | if (kUnknown_BmpHeaderType == headerType) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 199 | return kInvalidInput; |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 200 | } |
| 201 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 202 | // 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] | 203 | const uint32_t infoBytesRemaining = infoBytes - 4; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 204 | |
| 205 | // Read the second header |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 206 | std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 207 | if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 208 | SkCodecPrintf("Error: unable to read second bitmap header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 209 | return kIncompleteInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 210 | } |
| 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 |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 216 | uint32_t compression = kNone_BmpCompressionMethod; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 217 | |
| 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 III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 227 | 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); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 241 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 242 | // 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 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 249 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 250 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 251 | // 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 III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 265 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Check for valid dimensions from header |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 269 | SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 270 | if (height < 0) { |
| 271 | height = -height; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 272 | rowOrder = SkCodec::kTopDown_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 273 | } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 274 | // The height field for bmp in ico is double the actual height because they |
| 275 | // contain an XOR mask followed by an AND mask |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 276 | if (inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 277 | height /= 2; |
| 278 | } |
Leon Scroggins III | 0354c62 | 2017-02-24 15:33:24 -0500 | [diff] [blame] | 279 | |
| 280 | // Arbitrary maximum. Matches Chromium. |
| 281 | constexpr int kMaxDim = 1 << 16; |
| 282 | if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 283 | SkCodecPrintf("Error: invalid bitmap dimensions.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 284 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Create mask struct |
| 288 | SkMasks::InputMasks inputMasks; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 289 | memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 290 | |
| 291 | // Determine the input compression format and set bit masks if necessary |
| 292 | uint32_t maskBytes = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 293 | BmpInputFormat inputFormat = kUnknown_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 294 | switch (compression) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 295 | case kNone_BmpCompressionMethod: |
| 296 | inputFormat = kStandard_BmpInputFormat; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 297 | |
| 298 | // In addition to more standard pixel compression formats, bmp supports |
| 299 | // the use of bit masks to determine pixel components. The standard |
| 300 | // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
| 301 | // which does not map well to any Skia color formats. For this reason, |
| 302 | // we will always enable mask mode with 16 bits per pixel. |
| 303 | if (16 == bitsPerPixel) { |
| 304 | inputMasks.red = 0x7C00; |
| 305 | inputMasks.green = 0x03E0; |
| 306 | inputMasks.blue = 0x001F; |
| 307 | inputFormat = kBitMask_BmpInputFormat; |
| 308 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 309 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 310 | case k8BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 311 | if (bitsPerPixel != 8) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 312 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 313 | bitsPerPixel = 8; |
| 314 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 315 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 316 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 317 | case k4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 318 | if (bitsPerPixel != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 319 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 320 | bitsPerPixel = 4; |
| 321 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 322 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 323 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 324 | case kAlphaBitMasks_BmpCompressionMethod: |
| 325 | case kBitMasks_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 326 | // Load the masks |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 327 | inputFormat = kBitMask_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 328 | switch (headerType) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 329 | case kInfoV1_BmpHeaderType: { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 330 | // The V1 header stores the bit masks after the header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 331 | uint8_t buffer[kBmpMaskBytes]; |
| 332 | if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 333 | SkCodecPrintf("Error: unable to read bit inputMasks.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 334 | return kIncompleteInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 335 | } |
| 336 | maskBytes = kBmpMaskBytes; |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 337 | inputMasks.red = get_int(buffer, 0); |
| 338 | inputMasks.green = get_int(buffer, 4); |
| 339 | inputMasks.blue = get_int(buffer, 8); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 340 | break; |
| 341 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 342 | case kInfoV2_BmpHeaderType: |
| 343 | case kInfoV3_BmpHeaderType: |
| 344 | case kInfoV4_BmpHeaderType: |
| 345 | case kInfoV5_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 346 | // Header types are matched based on size. If the header |
| 347 | // is V2+, we are guaranteed to be able to read at least |
| 348 | // this size. |
| 349 | SkASSERT(infoBytesRemaining >= 48); |
| 350 | inputMasks.red = get_int(iBuffer.get(), 36); |
| 351 | inputMasks.green = get_int(iBuffer.get(), 40); |
| 352 | inputMasks.blue = get_int(iBuffer.get(), 44); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 353 | |
| 354 | if (kInfoV2_BmpHeaderType == headerType || |
| 355 | (kInfoV3_BmpHeaderType == headerType && !inIco)) { |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | // V3+ bmp files introduce an alpha mask and allow the creator of the image |
| 360 | // to use the alpha channels. However, many of these images leave the |
| 361 | // alpha channel blank and expect to be rendered as opaque. This is the |
| 362 | // case for almost all V3 images, so we ignore the alpha mask. For V4+ |
| 363 | // images in kMask mode, we will use the alpha mask. Additionally, V3 |
| 364 | // bmp-in-ico expect us to use the alpha mask. |
| 365 | // |
| 366 | // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard |
| 367 | // mode. We just haven't seen any images that expect this |
| 368 | // behavior. |
| 369 | // |
| 370 | // Header types are matched based on size. If the header is |
| 371 | // V3+, we are guaranteed to be able to read at least this size. |
| 372 | SkASSERT(infoBytesRemaining > 52); |
| 373 | inputMasks.alpha = get_int(iBuffer.get(), 48); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 374 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 375 | case kOS2VX_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 376 | // TODO: Decide if we intend to support this. |
| 377 | // It is unsupported in the previous version and |
| 378 | // in chromium. I have not come across a test case |
| 379 | // that uses this format. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 380 | SkCodecPrintf("Error: huffman format unsupported.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 381 | return kUnimplemented; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 382 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 383 | SkCodecPrintf("Error: invalid bmp bit masks header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 384 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 385 | } |
| 386 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 387 | case kJpeg_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 388 | if (24 == bitsPerPixel) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 389 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | // Fall through |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 393 | case kPng_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 394 | // TODO: Decide if we intend to support this. |
| 395 | // It is unsupported in the previous version and |
| 396 | // in chromium. I think it is used mostly for printers. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 397 | SkCodecPrintf("Error: compression format not supported.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 398 | return kUnimplemented; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 399 | case kCMYK_BmpCompressionMethod: |
| 400 | case kCMYK8BitRLE_BmpCompressionMethod: |
| 401 | case kCMYK4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 402 | // TODO: Same as above. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 403 | SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 404 | return kUnimplemented; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 405 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 406 | SkCodecPrintf("Error: invalid format for bitmap decoding.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 407 | return kInvalidInput; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 408 | } |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 409 | iBuffer.reset(); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 410 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 411 | // Calculate the number of bytes read so far |
| 412 | const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 413 | if (!inIco && offset < bytesRead) { |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 414 | // TODO (msarett): Do we really want to fail if the offset in the header is invalid? |
| 415 | // Seems like we can just assume that the offset is zero and try to decode? |
| 416 | // Maybe we don't want to try to decode corrupt images? |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 417 | SkCodecPrintf("Error: pixel data offset less than header size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 418 | return kInvalidInput; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 419 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 420 | |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 421 | |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 422 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 423 | switch (inputFormat) { |
| 424 | case kStandard_BmpInputFormat: { |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 425 | // BMPs are generally opaque, however BMPs-in-ICOs may contain |
| 426 | // a transparency mask after the image. Therefore, we mark the |
| 427 | // alpha as kBinary if the BMP is contained in an ICO. |
| 428 | // We use |isOpaque| to indicate if the BMP itself is opaque. |
| 429 | SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha : |
| 430 | SkEncodedInfo::kOpaque_Alpha; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 431 | bool isOpaque = true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 432 | |
| 433 | SkEncodedInfo::Color color; |
| 434 | uint8_t bitsPerComponent; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 435 | switch (bitsPerPixel) { |
| 436 | // Palette formats |
| 437 | case 1: |
| 438 | case 2: |
| 439 | case 4: |
| 440 | case 8: |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 441 | // In the case of ICO, kBGRA is actually the closest match, |
| 442 | // since we will need to apply a transparency mask. |
| 443 | if (inIco) { |
| 444 | color = SkEncodedInfo::kBGRA_Color; |
| 445 | bitsPerComponent = 8; |
| 446 | } else { |
| 447 | color = SkEncodedInfo::kPalette_Color; |
| 448 | bitsPerComponent = (uint8_t) bitsPerPixel; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 449 | } |
| 450 | break; |
| 451 | case 24: |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 452 | // In the case of ICO, kBGRA is actually the closest match, |
| 453 | // since we will need to apply a transparency mask. |
| 454 | color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 455 | bitsPerComponent = 8; |
| 456 | break; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 457 | case 32: |
| 458 | // 32-bit BMP-in-ICOs actually use the alpha channel in place of a |
| 459 | // transparency mask. |
| 460 | if (inIco) { |
| 461 | isOpaque = false; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 462 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 463 | color = SkEncodedInfo::kBGRA_Color; |
| 464 | } else { |
| 465 | color = SkEncodedInfo::kBGRX_Color; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 466 | } |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 467 | bitsPerComponent = 8; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 468 | break; |
| 469 | default: |
| 470 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 471 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 472 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 473 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 474 | if (codecOut) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 475 | // We require streams to have a memory base for Bmp-in-Ico decodes. |
| 476 | SkASSERT(!inIco || nullptr != stream->getMemoryBase()); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 477 | |
| 478 | // Set the image info and create a codec. |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 479 | const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 480 | codecOut->reset(new SkBmpStandardCodec(width, height, info, |
| 481 | std::unique_ptr<SkStream>(stream), |
| 482 | bitsPerPixel, numColors, bytesPerColor, |
| 483 | offset - bytesRead, rowOrder, isOpaque, |
| 484 | inIco)); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 485 | return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer() |
| 486 | ? kSuccess : kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 487 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 488 | return kSuccess; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | case kBitMask_BmpInputFormat: { |
| 492 | // Bmp-in-Ico must be standard mode |
| 493 | if (inIco) { |
| 494 | SkCodecPrintf("Error: Icos may not use bit mask format.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 495 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | switch (bitsPerPixel) { |
| 499 | case 16: |
| 500 | case 24: |
| 501 | case 32: |
| 502 | break; |
| 503 | default: |
| 504 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 505 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Skip to the start of the pixel array. |
| 509 | // We can do this here because there is no color table to read |
| 510 | // in bit mask mode. |
| 511 | if (stream->skip(offset - bytesRead) != offset - bytesRead) { |
| 512 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 513 | return kIncompleteInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (codecOut) { |
| 517 | // Check that input bit masks are valid and create the masks object |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 518 | std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 519 | if (nullptr == masks) { |
| 520 | SkCodecPrintf("Error: invalid input masks.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 521 | return kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 522 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 523 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 524 | // Masked bmps are not a great fit for SkEncodedInfo, since they have |
| 525 | // arbitrary component orderings and bits per component. Here we choose |
| 526 | // somewhat reasonable values - it's ok that we don't match exactly |
| 527 | // because SkBmpMaskCodec has its own mask swizzler anyway. |
| 528 | SkEncodedInfo::Color color; |
| 529 | SkEncodedInfo::Alpha alpha; |
| 530 | if (masks->getAlphaMask()) { |
| 531 | color = SkEncodedInfo::kBGRA_Color; |
| 532 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 533 | } else { |
| 534 | color = SkEncodedInfo::kBGR_Color; |
| 535 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 536 | } |
| 537 | const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 538 | codecOut->reset(new SkBmpMaskCodec(width, height, info, |
| 539 | std::unique_ptr<SkStream>(stream), bitsPerPixel, |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 540 | masks.release(), rowOrder)); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 541 | return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer() |
| 542 | ? kSuccess : kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 543 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 544 | return kSuccess; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | case kRLE_BmpInputFormat: { |
| 548 | // We should not reach this point without a valid value of bitsPerPixel. |
| 549 | SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel); |
| 550 | |
| 551 | // Check for a valid number of total bytes when in RLE mode |
| 552 | if (totalBytes <= offset) { |
| 553 | SkCodecPrintf("Error: RLE requires valid input size.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 554 | return kInvalidInput; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 555 | } |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 556 | |
| 557 | // Bmp-in-Ico must be standard mode |
| 558 | // When inIco is true, this line cannot be reached, since we |
| 559 | // require that RLE Bmps have a valid number of totalBytes, and |
| 560 | // Icos skip the header that contains totalBytes. |
| 561 | SkASSERT(!inIco); |
| 562 | |
| 563 | if (codecOut) { |
| 564 | // RLE inputs may skip pixels, leaving them as transparent. This |
| 565 | // is uncommon, but we cannot be certain that an RLE bmp will be |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 566 | // opaque or that we will be able to represent it with a palette. |
| 567 | // For that reason, we always indicate that we are kBGRA. |
| 568 | const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color, |
| 569 | SkEncodedInfo::kBinary_Alpha, 8); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 570 | codecOut->reset(new SkBmpRLECodec(width, height, info, |
| 571 | std::unique_ptr<SkStream>(stream), bitsPerPixel, |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 572 | numColors, bytesPerColor, offset - bytesRead, |
| 573 | rowOrder)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 574 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 575 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 576 | } |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 577 | default: |
| 578 | SkASSERT(false); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 579 | return kInvalidInput; |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 580 | } |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 584 | * Creates a bmp decoder |
| 585 | * Reads enough of the stream to determine the image format |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 586 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 587 | std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 588 | Result* result, bool inIco) { |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 589 | std::unique_ptr<SkCodec> codec; |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 590 | *result = ReadHeader(stream.get(), inIco, &codec); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 591 | if (codec) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 592 | // codec has taken ownership of stream, so we do not need to delete it. |
| 593 | stream.release(); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 594 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 595 | return kSuccess == *result ? std::move(codec) : nullptr; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 598 | SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, |
| 599 | std::unique_ptr<SkStream> stream, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 600 | uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder) |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 601 | : INHERITED(width, height, info, kXformSrcColorFormat, std::move(stream), |
| 602 | SkColorSpace::MakeSRGB()) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 603 | , fBitsPerPixel(bitsPerPixel) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 604 | , fRowOrder(rowOrder) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 605 | , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel))) |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 606 | , fXformBuffer(nullptr) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 607 | {} |
| 608 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 609 | bool SkBmpCodec::onRewind() { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 610 | return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr) == kSuccess; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 611 | } |
| 612 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 613 | int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const { |
| 614 | if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 615 | return y; |
| 616 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 617 | SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 618 | return height - y - 1; |
| 619 | } |
| 620 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 621 | SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 622 | const SkCodec::Options& options) { |
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 | } |