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 | */ |
| 70 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 71 | return SkBmpCodec::NewFromStream(stream, false); |
| 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 | */ |
| 78 | SkCodec* SkBmpCodec::NewFromIco(SkStream* stream) { |
| 79 | return SkBmpCodec::NewFromStream(stream, true); |
| 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 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 135 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 136 | * Read enough of the stream to initialize the SkBmpCodec. Returns a bool |
| 137 | * representing success or failure. If it returned true, and codecOut was |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | * not nullptr, it will be set to a new SkBmpCodec. |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 139 | * If codecOut is set to a new SkCodec, it will take ownership of the stream. |
| 140 | * Otherwise, the stream will not be deleted. |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 141 | */ |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 142 | bool SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, |
| 143 | std::unique_ptr<SkCodec>* codecOut) { |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 144 | // The total bytes in the bmp file |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 145 | // We only need to use this value for RLE decoding, so we will only |
| 146 | // check that it is valid in the RLE case. |
| 147 | uint32_t totalBytes; |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 148 | // The offset from the start of the file where the pixel data begins |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 149 | uint32_t offset; |
| 150 | // The size of the second (info) header in bytes |
| 151 | uint32_t infoBytes; |
| 152 | |
| 153 | // Bmps embedded in Icos skip the first Bmp header |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 154 | if (!inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 155 | // Read the first header and the size of the second header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 156 | uint8_t hBuffer[kBmpHeaderBytesPlusFour]; |
| 157 | if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) != |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 158 | kBmpHeaderBytesPlusFour) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 159 | SkCodecPrintf("Error: unable to read first bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 160 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 163 | totalBytes = get_int(hBuffer, 2); |
| 164 | offset = get_int(hBuffer, 10); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 165 | if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 166 | SkCodecPrintf("Error: invalid starting location for pixel data\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 167 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // The size of the second (info) header in bytes |
| 171 | // The size is the first field of the second header, so we have already |
| 172 | // read the first four infoBytes. |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 173 | infoBytes = get_int(hBuffer, 14); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 174 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 175 | SkCodecPrintf("Error: invalid second header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 176 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 177 | } |
| 178 | } else { |
| 179 | // This value is only used by RLE compression. Bmp in Ico files do not |
| 180 | // use RLE. If the compression field is incorrectly signaled as RLE, |
| 181 | // we will catch this and signal an error below. |
| 182 | totalBytes = 0; |
| 183 | |
| 184 | // Bmps in Ico cannot specify an offset. We will always assume that |
| 185 | // pixel data begins immediately after the color table. This value |
| 186 | // will be corrected below. |
| 187 | offset = 0; |
| 188 | |
| 189 | // Read the size of the second header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 190 | uint8_t hBuffer[4]; |
| 191 | if (stream->read(hBuffer, 4) != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 192 | SkCodecPrintf("Error: unable to read size of second bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 193 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 194 | } |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 195 | infoBytes = get_int(hBuffer, 0); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 196 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 197 | SkCodecPrintf("Error: invalid second header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 198 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 199 | } |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 202 | // Determine image information depending on second header format |
| 203 | const BmpHeaderType headerType = get_header_type(infoBytes); |
| 204 | if (kUnknown_BmpHeaderType == headerType) { |
| 205 | return false; |
| 206 | } |
| 207 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 208 | // 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] | 209 | const uint32_t infoBytesRemaining = infoBytes - 4; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 210 | |
| 211 | // Read the second header |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 212 | std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 213 | if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 214 | SkCodecPrintf("Error: unable to read second bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 215 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // The number of bits used per pixel in the pixel data |
| 219 | uint16_t bitsPerPixel; |
| 220 | |
| 221 | // The compression method for the pixel data |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 222 | uint32_t compression = kNone_BmpCompressionMethod; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 223 | |
| 224 | // Number of colors in the color table, defaults to 0 or max (see below) |
| 225 | uint32_t numColors = 0; |
| 226 | |
| 227 | // Bytes per color in the color table, early versions use 3, most use 4 |
| 228 | uint32_t bytesPerColor; |
| 229 | |
| 230 | // The image width and height |
| 231 | int width, height; |
| 232 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 233 | switch (headerType) { |
| 234 | case kInfoV1_BmpHeaderType: |
| 235 | case kInfoV2_BmpHeaderType: |
| 236 | case kInfoV3_BmpHeaderType: |
| 237 | case kInfoV4_BmpHeaderType: |
| 238 | case kInfoV5_BmpHeaderType: |
| 239 | case kOS2VX_BmpHeaderType: |
| 240 | // We check the size of the header before entering the if statement. |
| 241 | // We should not reach this point unless the size is large enough for |
| 242 | // these required fields. |
| 243 | SkASSERT(infoBytesRemaining >= 12); |
| 244 | width = get_int(iBuffer.get(), 0); |
| 245 | height = get_int(iBuffer.get(), 4); |
| 246 | bitsPerPixel = get_short(iBuffer.get(), 10); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 247 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 248 | // Some versions do not have these fields, so we check before |
| 249 | // overwriting the default value. |
| 250 | if (infoBytesRemaining >= 16) { |
| 251 | compression = get_int(iBuffer.get(), 12); |
| 252 | if (infoBytesRemaining >= 32) { |
| 253 | numColors = get_int(iBuffer.get(), 28); |
| 254 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 255 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 256 | |
Leon Scroggins III | 0b24cbd | 2016-12-15 15:58:22 -0500 | [diff] [blame] | 257 | // All of the headers that reach this point, store color table entries |
| 258 | // using 4 bytes per pixel. |
| 259 | bytesPerColor = 4; |
| 260 | break; |
| 261 | case kOS2V1_BmpHeaderType: |
| 262 | // The OS2V1 is treated separately because it has a unique format |
| 263 | width = (int) get_short(iBuffer.get(), 0); |
| 264 | height = (int) get_short(iBuffer.get(), 2); |
| 265 | bitsPerPixel = get_short(iBuffer.get(), 6); |
| 266 | bytesPerColor = 3; |
| 267 | break; |
| 268 | case kUnknown_BmpHeaderType: |
| 269 | // We'll exit above in this case. |
| 270 | SkASSERT(false); |
| 271 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Check for valid dimensions from header |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 275 | SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 276 | if (height < 0) { |
| 277 | height = -height; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 278 | rowOrder = SkCodec::kTopDown_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 279 | } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 280 | // The height field for bmp in ico is double the actual height because they |
| 281 | // contain an XOR mask followed by an AND mask |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 282 | if (inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 283 | height /= 2; |
| 284 | } |
Leon Scroggins III | 0354c62 | 2017-02-24 15:33:24 -0500 | [diff] [blame] | 285 | |
| 286 | // Arbitrary maximum. Matches Chromium. |
| 287 | constexpr int kMaxDim = 1 << 16; |
| 288 | if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 289 | SkCodecPrintf("Error: invalid bitmap dimensions.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 290 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Create mask struct |
| 294 | SkMasks::InputMasks inputMasks; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 295 | memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 296 | |
| 297 | // Determine the input compression format and set bit masks if necessary |
| 298 | uint32_t maskBytes = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 299 | BmpInputFormat inputFormat = kUnknown_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 300 | switch (compression) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 301 | case kNone_BmpCompressionMethod: |
| 302 | inputFormat = kStandard_BmpInputFormat; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 303 | |
| 304 | // In addition to more standard pixel compression formats, bmp supports |
| 305 | // the use of bit masks to determine pixel components. The standard |
| 306 | // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
| 307 | // which does not map well to any Skia color formats. For this reason, |
| 308 | // we will always enable mask mode with 16 bits per pixel. |
| 309 | if (16 == bitsPerPixel) { |
| 310 | inputMasks.red = 0x7C00; |
| 311 | inputMasks.green = 0x03E0; |
| 312 | inputMasks.blue = 0x001F; |
| 313 | inputFormat = kBitMask_BmpInputFormat; |
| 314 | } |
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 k8BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 317 | if (bitsPerPixel != 8) { |
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 = 8; |
| 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 k4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 324 | if (bitsPerPixel != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 325 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 326 | bitsPerPixel = 4; |
| 327 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 328 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 329 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 330 | case kAlphaBitMasks_BmpCompressionMethod: |
| 331 | case kBitMasks_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 332 | // Load the masks |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 333 | inputFormat = kBitMask_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 334 | switch (headerType) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 335 | case kInfoV1_BmpHeaderType: { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 336 | // The V1 header stores the bit masks after the header |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 337 | uint8_t buffer[kBmpMaskBytes]; |
| 338 | if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 339 | SkCodecPrintf("Error: unable to read bit inputMasks.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 340 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 341 | } |
| 342 | maskBytes = kBmpMaskBytes; |
Leon Scroggins III | ec4400b | 2017-06-01 14:06:35 -0400 | [diff] [blame] | 343 | inputMasks.red = get_int(buffer, 0); |
| 344 | inputMasks.green = get_int(buffer, 4); |
| 345 | inputMasks.blue = get_int(buffer, 8); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 346 | break; |
| 347 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 348 | case kInfoV2_BmpHeaderType: |
| 349 | case kInfoV3_BmpHeaderType: |
| 350 | case kInfoV4_BmpHeaderType: |
| 351 | case kInfoV5_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 352 | // Header types are matched based on size. If the header |
| 353 | // is V2+, we are guaranteed to be able to read at least |
| 354 | // this size. |
| 355 | SkASSERT(infoBytesRemaining >= 48); |
| 356 | inputMasks.red = get_int(iBuffer.get(), 36); |
| 357 | inputMasks.green = get_int(iBuffer.get(), 40); |
| 358 | inputMasks.blue = get_int(iBuffer.get(), 44); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 359 | |
| 360 | if (kInfoV2_BmpHeaderType == headerType || |
| 361 | (kInfoV3_BmpHeaderType == headerType && !inIco)) { |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | // V3+ bmp files introduce an alpha mask and allow the creator of the image |
| 366 | // to use the alpha channels. However, many of these images leave the |
| 367 | // alpha channel blank and expect to be rendered as opaque. This is the |
| 368 | // case for almost all V3 images, so we ignore the alpha mask. For V4+ |
| 369 | // images in kMask mode, we will use the alpha mask. Additionally, V3 |
| 370 | // bmp-in-ico expect us to use the alpha mask. |
| 371 | // |
| 372 | // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard |
| 373 | // mode. We just haven't seen any images that expect this |
| 374 | // behavior. |
| 375 | // |
| 376 | // Header types are matched based on size. If the header is |
| 377 | // V3+, we are guaranteed to be able to read at least this size. |
| 378 | SkASSERT(infoBytesRemaining > 52); |
| 379 | inputMasks.alpha = get_int(iBuffer.get(), 48); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 380 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 381 | case kOS2VX_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 382 | // TODO: Decide if we intend to support this. |
| 383 | // It is unsupported in the previous version and |
| 384 | // in chromium. I have not come across a test case |
| 385 | // that uses this format. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 386 | SkCodecPrintf("Error: huffman format unsupported.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 387 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 388 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 389 | SkCodecPrintf("Error: invalid bmp bit masks header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 390 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 391 | } |
| 392 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 393 | case kJpeg_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 394 | if (24 == bitsPerPixel) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 395 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | // Fall through |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 399 | case kPng_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 400 | // TODO: Decide if we intend to support this. |
| 401 | // It is unsupported in the previous version and |
| 402 | // in chromium. I think it is used mostly for printers. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 403 | SkCodecPrintf("Error: compression format not supported.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 404 | return false; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 405 | case kCMYK_BmpCompressionMethod: |
| 406 | case kCMYK8BitRLE_BmpCompressionMethod: |
| 407 | case kCMYK4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 408 | // TODO: Same as above. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 409 | SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 410 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 411 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 412 | SkCodecPrintf("Error: invalid format for bitmap decoding.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 413 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 414 | } |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 415 | iBuffer.reset(); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 416 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 417 | // Calculate the number of bytes read so far |
| 418 | const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 419 | if (!inIco && offset < bytesRead) { |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 420 | // TODO (msarett): Do we really want to fail if the offset in the header is invalid? |
| 421 | // Seems like we can just assume that the offset is zero and try to decode? |
| 422 | // Maybe we don't want to try to decode corrupt images? |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 423 | SkCodecPrintf("Error: pixel data offset less than header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 424 | return false; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 425 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 426 | |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 427 | |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 428 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 429 | switch (inputFormat) { |
| 430 | case kStandard_BmpInputFormat: { |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 431 | // BMPs are generally opaque, however BMPs-in-ICOs may contain |
| 432 | // a transparency mask after the image. Therefore, we mark the |
| 433 | // alpha as kBinary if the BMP is contained in an ICO. |
| 434 | // We use |isOpaque| to indicate if the BMP itself is opaque. |
| 435 | SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha : |
| 436 | SkEncodedInfo::kOpaque_Alpha; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 437 | bool isOpaque = true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 438 | |
| 439 | SkEncodedInfo::Color color; |
| 440 | uint8_t bitsPerComponent; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 441 | switch (bitsPerPixel) { |
| 442 | // Palette formats |
| 443 | case 1: |
| 444 | case 2: |
| 445 | case 4: |
| 446 | case 8: |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 447 | // In the case of ICO, kBGRA is actually the closest match, |
| 448 | // since we will need to apply a transparency mask. |
| 449 | if (inIco) { |
| 450 | color = SkEncodedInfo::kBGRA_Color; |
| 451 | bitsPerComponent = 8; |
| 452 | } else { |
| 453 | color = SkEncodedInfo::kPalette_Color; |
| 454 | bitsPerComponent = (uint8_t) bitsPerPixel; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | case 24: |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 458 | // In the case of ICO, kBGRA is actually the closest match, |
| 459 | // since we will need to apply a transparency mask. |
| 460 | color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 461 | bitsPerComponent = 8; |
| 462 | break; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 463 | case 32: |
| 464 | // 32-bit BMP-in-ICOs actually use the alpha channel in place of a |
| 465 | // transparency mask. |
| 466 | if (inIco) { |
| 467 | isOpaque = false; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 468 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 469 | color = SkEncodedInfo::kBGRA_Color; |
| 470 | } else { |
| 471 | color = SkEncodedInfo::kBGRX_Color; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 472 | } |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 473 | bitsPerComponent = 8; |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 474 | break; |
| 475 | default: |
| 476 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
| 477 | return false; |
| 478 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 479 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 480 | if (codecOut) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 481 | // We require streams to have a memory base for Bmp-in-Ico decodes. |
| 482 | SkASSERT(!inIco || nullptr != stream->getMemoryBase()); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 483 | |
| 484 | // Set the image info and create a codec. |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 485 | const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 486 | codecOut->reset(new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel, |
| 487 | numColors, bytesPerColor, offset - bytesRead, |
| 488 | rowOrder, isOpaque, inIco)); |
| 489 | return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer(); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 490 | } |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | case kBitMask_BmpInputFormat: { |
| 495 | // Bmp-in-Ico must be standard mode |
| 496 | if (inIco) { |
| 497 | SkCodecPrintf("Error: Icos may not use bit mask format.\n"); |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | switch (bitsPerPixel) { |
| 502 | case 16: |
| 503 | case 24: |
| 504 | case 32: |
| 505 | break; |
| 506 | default: |
| 507 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | // Skip to the start of the pixel array. |
| 512 | // We can do this here because there is no color table to read |
| 513 | // in bit mask mode. |
| 514 | if (stream->skip(offset - bytesRead) != offset - bytesRead) { |
| 515 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | if (codecOut) { |
| 520 | // Check that input bit masks are valid and create the masks object |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 521 | std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 522 | if (nullptr == masks) { |
| 523 | SkCodecPrintf("Error: invalid input masks.\n"); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 524 | return false; |
| 525 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 526 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 527 | // Masked bmps are not a great fit for SkEncodedInfo, since they have |
| 528 | // arbitrary component orderings and bits per component. Here we choose |
| 529 | // somewhat reasonable values - it's ok that we don't match exactly |
| 530 | // because SkBmpMaskCodec has its own mask swizzler anyway. |
| 531 | SkEncodedInfo::Color color; |
| 532 | SkEncodedInfo::Alpha alpha; |
| 533 | if (masks->getAlphaMask()) { |
| 534 | color = SkEncodedInfo::kBGRA_Color; |
| 535 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 536 | } else { |
| 537 | color = SkEncodedInfo::kBGR_Color; |
| 538 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 539 | } |
| 540 | const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 541 | codecOut->reset(new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel, |
| 542 | masks.release(), rowOrder)); |
| 543 | return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer(); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | case kRLE_BmpInputFormat: { |
| 549 | // We should not reach this point without a valid value of bitsPerPixel. |
| 550 | SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel); |
| 551 | |
| 552 | // Check for a valid number of total bytes when in RLE mode |
| 553 | if (totalBytes <= offset) { |
| 554 | SkCodecPrintf("Error: RLE requires valid input size.\n"); |
| 555 | return false; |
| 556 | } |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 557 | |
| 558 | // Bmp-in-Ico must be standard mode |
| 559 | // When inIco is true, this line cannot be reached, since we |
| 560 | // require that RLE Bmps have a valid number of totalBytes, and |
| 561 | // Icos skip the header that contains totalBytes. |
| 562 | SkASSERT(!inIco); |
| 563 | |
| 564 | if (codecOut) { |
| 565 | // RLE inputs may skip pixels, leaving them as transparent. This |
| 566 | // is uncommon, but we cannot be certain that an RLE bmp will be |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 567 | // opaque or that we will be able to represent it with a palette. |
| 568 | // For that reason, we always indicate that we are kBGRA. |
| 569 | const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color, |
| 570 | SkEncodedInfo::kBinary_Alpha, 8); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 571 | codecOut->reset(new SkBmpRLECodec(width, height, info, stream, bitsPerPixel, |
| 572 | numColors, bytesPerColor, offset - bytesRead, |
| 573 | rowOrder)); |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 574 | } |
| 575 | return true; |
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); |
| 579 | return false; |
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 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 587 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool inIco) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 588 | std::unique_ptr<SkStream> streamDeleter(stream); |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 589 | std::unique_ptr<SkCodec> codec; |
| 590 | bool success = ReadHeader(stream, inIco, &codec); |
| 591 | if (codec) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 592 | // codec has taken ownership of stream, so we do not need to |
| 593 | // delete it. |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 594 | streamDeleter.release(); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 595 | } |
Leon Scroggins III | fc4ee22 | 2017-07-14 11:48:52 -0400 | [diff] [blame] | 596 | return success ? codec.release() : nullptr; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 597 | } |
| 598 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 599 | SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 600 | uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 601 | : INHERITED(width, height, info, kXformSrcColorFormat, stream, SkColorSpace::MakeSRGB()) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 602 | , fBitsPerPixel(bitsPerPixel) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 603 | , fRowOrder(rowOrder) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 604 | , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel))) |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 605 | , fXformBuffer(nullptr) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 606 | {} |
| 607 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 608 | bool SkBmpCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 609 | return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr); |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 610 | } |
| 611 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 612 | int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const { |
| 613 | if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 614 | return y; |
| 615 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 616 | SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 617 | return height - y - 1; |
| 618 | } |
| 619 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 620 | SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 621 | const SkCodec::Options& options) { |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 622 | if (!conversion_possible(dstInfo, this->getInfo()) || |
| 623 | !this->initializeColorXform(dstInfo, options.fPremulBehavior)) |
| 624 | { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 625 | return kInvalidConversion; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 628 | return this->onPrepareToDecode(dstInfo, options); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 632 | const SkCodec::Options& options) { |
| 633 | return prepareToDecode(dstInfo, options); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 634 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 635 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 636 | int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 637 | // Create a new image info representing the portion of the image to decode |
| 638 | SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 639 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 640 | // Decode the requested rows |
| 641 | return this->decodeRows(rowInfo, dst, rowBytes, this->options()); |
| 642 | } |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 643 | |
| 644 | bool SkBmpCodec::skipRows(int count) { |
| 645 | const size_t bytesToSkip = count * fSrcRowBytes; |
| 646 | return this->stream()->skip(bytesToSkip) == bytesToSkip; |
| 647 | } |
| 648 | |
| 649 | bool SkBmpCodec::onSkipScanlines(int count) { |
| 650 | return this->skipRows(count); |
| 651 | } |