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 | |
| 82 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 83 | * Read enough of the stream to initialize the SkBmpCodec. Returns a bool |
| 84 | * representing success or failure. If it returned true, and codecOut was |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | * not nullptr, it will be set to a new SkBmpCodec. |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 86 | * Does *not* take ownership of the passed in SkStream. |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 87 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 88 | bool SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, SkCodec** codecOut) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 89 | // Header size constants |
| 90 | static const uint32_t kBmpHeaderBytes = 14; |
| 91 | static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
| 92 | static const uint32_t kBmpOS2V1Bytes = 12; |
| 93 | static const uint32_t kBmpOS2V2Bytes = 64; |
| 94 | static const uint32_t kBmpInfoBaseBytes = 16; |
| 95 | static const uint32_t kBmpInfoV1Bytes = 40; |
| 96 | static const uint32_t kBmpInfoV2Bytes = 52; |
| 97 | static const uint32_t kBmpInfoV3Bytes = 56; |
| 98 | static const uint32_t kBmpInfoV4Bytes = 108; |
| 99 | static const uint32_t kBmpInfoV5Bytes = 124; |
| 100 | static const uint32_t kBmpMaskBytes = 12; |
| 101 | |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 102 | // The total bytes in the bmp file |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 103 | // We only need to use this value for RLE decoding, so we will only |
| 104 | // check that it is valid in the RLE case. |
| 105 | uint32_t totalBytes; |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 106 | // The offset from the start of the file where the pixel data begins |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 107 | uint32_t offset; |
| 108 | // The size of the second (info) header in bytes |
| 109 | uint32_t infoBytes; |
| 110 | |
| 111 | // Bmps embedded in Icos skip the first Bmp header |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 112 | if (!inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 113 | // Read the first header and the size of the second header |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 114 | SkAutoTDeleteArray<uint8_t> hBuffer(new uint8_t[kBmpHeaderBytesPlusFour]); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 115 | if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != |
| 116 | kBmpHeaderBytesPlusFour) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 117 | SkCodecPrintf("Error: unable to read first bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 118 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | totalBytes = get_int(hBuffer.get(), 2); |
| 122 | offset = get_int(hBuffer.get(), 10); |
| 123 | if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 124 | SkCodecPrintf("Error: invalid starting location for pixel data\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 125 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // The size of the second (info) header in bytes |
| 129 | // The size is the first field of the second header, so we have already |
| 130 | // read the first four infoBytes. |
| 131 | infoBytes = get_int(hBuffer.get(), 14); |
| 132 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 133 | SkCodecPrintf("Error: invalid second header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 134 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 135 | } |
| 136 | } else { |
| 137 | // This value is only used by RLE compression. Bmp in Ico files do not |
| 138 | // use RLE. If the compression field is incorrectly signaled as RLE, |
| 139 | // we will catch this and signal an error below. |
| 140 | totalBytes = 0; |
| 141 | |
| 142 | // Bmps in Ico cannot specify an offset. We will always assume that |
| 143 | // pixel data begins immediately after the color table. This value |
| 144 | // will be corrected below. |
| 145 | offset = 0; |
| 146 | |
| 147 | // Read the size of the second header |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 148 | SkAutoTDeleteArray<uint8_t> hBuffer(new uint8_t[4]); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 149 | if (stream->read(hBuffer.get(), 4) != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 150 | SkCodecPrintf("Error: unable to read size of second bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 151 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 152 | } |
| 153 | infoBytes = get_int(hBuffer.get(), 0); |
| 154 | if (infoBytes < kBmpOS2V1Bytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 155 | SkCodecPrintf("Error: invalid second header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 156 | return false; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 157 | } |
tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 158 | } |
| 159 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 160 | // 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] | 161 | const uint32_t infoBytesRemaining = infoBytes - 4; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 162 | |
| 163 | // Read the second header |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 164 | SkAutoTDeleteArray<uint8_t> iBuffer(new uint8_t[infoBytesRemaining]); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 165 | if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 166 | SkCodecPrintf("Error: unable to read second bitmap header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 167 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // The number of bits used per pixel in the pixel data |
| 171 | uint16_t bitsPerPixel; |
| 172 | |
| 173 | // The compression method for the pixel data |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 174 | uint32_t compression = kNone_BmpCompressionMethod; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 175 | |
| 176 | // Number of colors in the color table, defaults to 0 or max (see below) |
| 177 | uint32_t numColors = 0; |
| 178 | |
| 179 | // Bytes per color in the color table, early versions use 3, most use 4 |
| 180 | uint32_t bytesPerColor; |
| 181 | |
| 182 | // The image width and height |
| 183 | int width, height; |
| 184 | |
| 185 | // Determine image information depending on second header format |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 186 | BmpHeaderType headerType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 187 | if (infoBytes >= kBmpInfoBaseBytes) { |
| 188 | // Check the version of the header |
| 189 | switch (infoBytes) { |
| 190 | case kBmpInfoV1Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 191 | headerType = kInfoV1_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 192 | break; |
| 193 | case kBmpInfoV2Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 194 | headerType = kInfoV2_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 195 | break; |
| 196 | case kBmpInfoV3Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 197 | headerType = kInfoV3_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 198 | break; |
| 199 | case kBmpInfoV4Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 200 | headerType = kInfoV4_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 201 | break; |
| 202 | case kBmpInfoV5Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 203 | headerType = kInfoV5_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 204 | break; |
| 205 | case 16: |
| 206 | case 20: |
| 207 | case 24: |
| 208 | case 28: |
| 209 | case 32: |
| 210 | case 36: |
| 211 | case 42: |
| 212 | case 46: |
| 213 | case 48: |
| 214 | case 60: |
| 215 | case kBmpOS2V2Bytes: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 216 | headerType = kOS2VX_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 217 | break; |
| 218 | default: |
| 219 | // We do not signal an error here because there is the |
| 220 | // possibility of new or undocumented bmp header types. Most |
| 221 | // of the newer versions of bmp headers are similar to and |
| 222 | // build off of the older versions, so we may still be able to |
| 223 | // decode the bmp. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 224 | SkCodecPrintf("Warning: unknown bmp header format.\n"); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 225 | headerType = kUnknown_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | // We check the size of the header before entering the if statement. |
| 229 | // We should not reach this point unless the size is large enough for |
| 230 | // these required fields. |
| 231 | SkASSERT(infoBytesRemaining >= 12); |
| 232 | width = get_int(iBuffer.get(), 0); |
| 233 | height = get_int(iBuffer.get(), 4); |
| 234 | bitsPerPixel = get_short(iBuffer.get(), 10); |
| 235 | |
| 236 | // Some versions do not have these fields, so we check before |
| 237 | // overwriting the default value. |
| 238 | if (infoBytesRemaining >= 16) { |
| 239 | compression = get_int(iBuffer.get(), 12); |
| 240 | if (infoBytesRemaining >= 32) { |
| 241 | numColors = get_int(iBuffer.get(), 28); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // All of the headers that reach this point, store color table entries |
| 246 | // using 4 bytes per pixel. |
| 247 | bytesPerColor = 4; |
| 248 | } else if (infoBytes >= kBmpOS2V1Bytes) { |
| 249 | // The OS2V1 is treated separately because it has a unique format |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 250 | headerType = kOS2V1_BmpHeaderType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 251 | width = (int) get_short(iBuffer.get(), 0); |
| 252 | height = (int) get_short(iBuffer.get(), 2); |
| 253 | bitsPerPixel = get_short(iBuffer.get(), 6); |
| 254 | bytesPerColor = 3; |
| 255 | } else { |
| 256 | // There are no valid bmp headers |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 257 | SkCodecPrintf("Error: second bitmap header size is invalid.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 258 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Check for valid dimensions from header |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 262 | SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 263 | if (height < 0) { |
| 264 | height = -height; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 265 | rowOrder = SkCodec::kTopDown_SkScanlineOrder; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 266 | } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 267 | // The height field for bmp in ico is double the actual height because they |
| 268 | // contain an XOR mask followed by an AND mask |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 269 | if (inIco) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 270 | height /= 2; |
| 271 | } |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 272 | if (width <= 0 || height <= 0) { |
| 273 | // TODO: Decide if we want to disable really large bmps as well. |
| 274 | // https://code.google.com/p/skia/issues/detail?id=3617 |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 275 | SkCodecPrintf("Error: invalid bitmap dimensions.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 276 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Create mask struct |
| 280 | SkMasks::InputMasks inputMasks; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 281 | memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 282 | |
| 283 | // Determine the input compression format and set bit masks if necessary |
| 284 | uint32_t maskBytes = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 285 | BmpInputFormat inputFormat = kUnknown_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 286 | switch (compression) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 287 | case kNone_BmpCompressionMethod: |
| 288 | inputFormat = kStandard_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 289 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 290 | case k8BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 291 | if (bitsPerPixel != 8) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 292 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 293 | bitsPerPixel = 8; |
| 294 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 295 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 296 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 297 | case k4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 298 | if (bitsPerPixel != 4) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 299 | SkCodecPrintf("Warning: correcting invalid bitmap format.\n"); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 300 | bitsPerPixel = 4; |
| 301 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 302 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 303 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 304 | case kAlphaBitMasks_BmpCompressionMethod: |
| 305 | case kBitMasks_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 306 | // Load the masks |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 307 | inputFormat = kBitMask_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 308 | switch (headerType) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 309 | case kInfoV1_BmpHeaderType: { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 310 | // The V1 header stores the bit masks after the header |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 311 | SkAutoTDeleteArray<uint8_t> mBuffer(new uint8_t[kBmpMaskBytes]); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 312 | if (stream->read(mBuffer.get(), kBmpMaskBytes) != |
| 313 | kBmpMaskBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 314 | SkCodecPrintf("Error: unable to read bit inputMasks.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 315 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 316 | } |
| 317 | maskBytes = kBmpMaskBytes; |
| 318 | inputMasks.red = get_int(mBuffer.get(), 0); |
| 319 | inputMasks.green = get_int(mBuffer.get(), 4); |
| 320 | inputMasks.blue = get_int(mBuffer.get(), 8); |
| 321 | break; |
| 322 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 323 | case kInfoV2_BmpHeaderType: |
| 324 | case kInfoV3_BmpHeaderType: |
| 325 | case kInfoV4_BmpHeaderType: |
| 326 | case kInfoV5_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 327 | // Header types are matched based on size. If the header |
| 328 | // is V2+, we are guaranteed to be able to read at least |
| 329 | // this size. |
| 330 | SkASSERT(infoBytesRemaining >= 48); |
| 331 | inputMasks.red = get_int(iBuffer.get(), 36); |
| 332 | inputMasks.green = get_int(iBuffer.get(), 40); |
| 333 | inputMasks.blue = get_int(iBuffer.get(), 44); |
| 334 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 335 | case kOS2VX_BmpHeaderType: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 336 | // TODO: Decide if we intend to support this. |
| 337 | // It is unsupported in the previous version and |
| 338 | // in chromium. I have not come across a test case |
| 339 | // that uses this format. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 340 | SkCodecPrintf("Error: huffman format unsupported.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 341 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 342 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 343 | SkCodecPrintf("Error: invalid bmp bit masks header.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 344 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 345 | } |
| 346 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 347 | case kJpeg_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 348 | if (24 == bitsPerPixel) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 349 | inputFormat = kRLE_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | // Fall through |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 353 | case kPng_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 354 | // TODO: Decide if we intend to support this. |
| 355 | // It is unsupported in the previous version and |
| 356 | // in chromium. I think it is used mostly for printers. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 357 | SkCodecPrintf("Error: compression format not supported.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 358 | return false; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 359 | case kCMYK_BmpCompressionMethod: |
| 360 | case kCMYK8BitRLE_BmpCompressionMethod: |
| 361 | case kCMYK4BitRLE_BmpCompressionMethod: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 362 | // TODO: Same as above. |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 363 | SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 364 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 365 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 366 | SkCodecPrintf("Error: invalid format for bitmap decoding.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 367 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Most versions of bmps should be rendered as opaque. Either they do |
| 371 | // not have an alpha channel, or they expect the alpha channel to be |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 372 | // ignored. V3+ bmp files introduce an alpha mask and allow the creator |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 373 | // of the image to use the alpha channels. However, many of these images |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 374 | // leave the alpha channel blank and expect to be rendered as opaque. This |
| 375 | // is the case for almost all V3 images, so we render these as opaque. For |
msarett | ef02b24 | 2016-02-11 08:41:53 -0800 | [diff] [blame] | 376 | // V4+ images in kMask mode, we will use the alpha mask. |
| 377 | // |
| 378 | // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard |
| 379 | // mode. We just haven't seen any images that expect this |
| 380 | // behavior. |
| 381 | // |
| 382 | // Additionally, V3 bmp-in-ico may use the alpha mask. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 383 | SkAlphaType alphaType = kOpaque_SkAlphaType; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 384 | if ((kInfoV3_BmpHeaderType == headerType && inIco) || |
| 385 | kInfoV4_BmpHeaderType == headerType || |
| 386 | kInfoV5_BmpHeaderType == headerType) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 387 | // Header types are matched based on size. If the header is |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 388 | // V3+, we are guaranteed to be able to read at least this size. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 389 | SkASSERT(infoBytesRemaining > 52); |
| 390 | inputMasks.alpha = get_int(iBuffer.get(), 48); |
| 391 | if (inputMasks.alpha != 0) { |
| 392 | alphaType = kUnpremul_SkAlphaType; |
| 393 | } |
| 394 | } |
| 395 | iBuffer.free(); |
| 396 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 397 | // Additionally, 32 bit bmp-in-icos use the alpha channel. |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 398 | // FIXME (msarett): Don't all bmp-in-icos use the alpha channel? |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 399 | // And, RLE inputs may skip pixels, leaving them as transparent. This |
| 400 | // is uncommon, but we cannot be certain that an RLE bmp will be opaque. |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 401 | if ((inIco && 32 == bitsPerPixel) || (kRLE_BmpInputFormat == inputFormat)) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 402 | alphaType = kUnpremul_SkAlphaType; |
| 403 | } |
| 404 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 405 | // Check for valid bits per pixel. |
| 406 | // At the same time, use this information to choose a suggested color type |
| 407 | // and to set default masks. |
| 408 | SkColorType colorType = kN32_SkColorType; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 409 | switch (bitsPerPixel) { |
| 410 | // In addition to more standard pixel compression formats, bmp supports |
| 411 | // the use of bit masks to determine pixel components. The standard |
| 412 | // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
| 413 | // which does not map well to any Skia color formats. For this reason, |
| 414 | // we will always enable mask mode with 16 bits per pixel. |
| 415 | case 16: |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 416 | if (kBitMask_BmpInputFormat != inputFormat) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 417 | inputMasks.red = 0x7C00; |
| 418 | inputMasks.green = 0x03E0; |
| 419 | inputMasks.blue = 0x001F; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 420 | inputFormat = kBitMask_BmpInputFormat; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 421 | } |
| 422 | break; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 423 | // We want to decode to kIndex_8 for input formats that are already |
| 424 | // designed in index format. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 425 | case 1: |
| 426 | case 2: |
| 427 | case 4: |
| 428 | case 8: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 429 | // However, we cannot in RLE format since we may need to leave some |
| 430 | // pixels as transparent. Similarly, we also cannot for ICO images |
| 431 | // since we may need to apply a transparent mask. |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 432 | if (kRLE_BmpInputFormat != inputFormat && !inIco) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 433 | colorType = kIndex_8_SkColorType; |
| 434 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 435 | case 24: |
| 436 | case 32: |
| 437 | break; |
| 438 | default: |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 439 | SkCodecPrintf("Error: invalid input value for bits per pixel.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 440 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // Check that input bit masks are valid and create the masks object |
| 444 | SkAutoTDelete<SkMasks> |
| 445 | masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 446 | if (nullptr == masks) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 447 | SkCodecPrintf("Error: invalid input masks.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 448 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 449 | } |
| 450 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 451 | // Check for a valid number of total bytes when in RLE mode |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 452 | if (totalBytes <= offset && kRLE_BmpInputFormat == inputFormat) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 453 | SkCodecPrintf("Error: RLE requires valid input size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 454 | return false; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 455 | } |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 456 | const size_t RLEBytes = totalBytes - offset; |
| 457 | |
| 458 | // Calculate the number of bytes read so far |
| 459 | const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 460 | if (!inIco && offset < bytesRead) { |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 461 | // TODO (msarett): Do we really want to fail if the offset in the header is invalid? |
| 462 | // Seems like we can just assume that the offset is zero and try to decode? |
| 463 | // Maybe we don't want to try to decode corrupt images? |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 464 | SkCodecPrintf("Error: pixel data offset less than header size.\n"); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 465 | return false; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 466 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 467 | |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 468 | // Skip to the start of the pixel array. |
| 469 | // We can do this here because there is no color table to read |
| 470 | // in bit mask mode. |
| 471 | if (!inIco && kBitMask_BmpInputFormat == inputFormat) { |
| 472 | if (stream->skip(offset - bytesRead) != offset - bytesRead) { |
| 473 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 474 | return false; |
| 475 | } |
| 476 | } |
| 477 | |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 478 | if (codecOut) { |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 479 | // BMPs-in-ICOs contain an alpha mask after the image which means we |
| 480 | // cannot guarantee that an image is opaque, even if the bmp thinks |
| 481 | // it is. |
| 482 | bool isOpaque = kOpaque_SkAlphaType == alphaType; |
| 483 | if (inIco) { |
| 484 | alphaType = kUnpremul_SkAlphaType; |
| 485 | } |
| 486 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 487 | // Set the image info |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 488 | const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 489 | colorType, alphaType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 490 | |
| 491 | // Return the codec |
| 492 | switch (inputFormat) { |
| 493 | case kStandard_BmpInputFormat: |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 494 | // We require streams to have a memory base for Bmp-in-Ico decodes. |
| 495 | SkASSERT(!inIco || nullptr != stream->getMemoryBase()); |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 496 | *codecOut = new SkBmpStandardCodec(imageInfo, stream, bitsPerPixel, numColors, |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 497 | bytesPerColor, offset - bytesRead, rowOrder, isOpaque, inIco); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 498 | return true; |
| 499 | case kBitMask_BmpInputFormat: |
| 500 | // Bmp-in-Ico must be standard mode |
| 501 | if (inIco) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 502 | SkCodecPrintf("Error: Icos may not use bit mask format.\n"); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 505 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 506 | *codecOut = new SkBmpMaskCodec(imageInfo, stream, bitsPerPixel, masks.detach(), |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 507 | rowOrder); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 508 | return true; |
| 509 | case kRLE_BmpInputFormat: |
| 510 | // Bmp-in-Ico must be standard mode |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 511 | // When inIco is true, this line cannot be reached, since we |
| 512 | // require that RLE Bmps have a valid number of totalBytes, and |
| 513 | // Icos skip the header that contains totalBytes. |
| 514 | SkASSERT(!inIco); |
msarett | 9aa32d1 | 2015-09-01 14:40:46 -0700 | [diff] [blame] | 515 | *codecOut = new SkBmpRLECodec(imageInfo, stream, bitsPerPixel, numColors, |
| 516 | bytesPerColor, offset - bytesRead, rowOrder, RLEBytes); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 517 | return true; |
| 518 | default: |
| 519 | SkASSERT(false); |
| 520 | return false; |
| 521 | } |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 522 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 523 | |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | |
| 527 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 528 | * Creates a bmp decoder |
| 529 | * Reads enough of the stream to determine the image format |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 530 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 531 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool inIco) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 532 | SkAutoTDelete<SkStream> streamDeleter(stream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 533 | SkCodec* codec = nullptr; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 534 | if (ReadHeader(stream, inIco, &codec)) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 535 | // codec has taken ownership of stream, so we do not need to |
| 536 | // delete it. |
| 537 | SkASSERT(codec); |
| 538 | streamDeleter.detach(); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 539 | return codec; |
| 540 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 541 | return nullptr; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 542 | } |
| 543 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 544 | SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 545 | uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 546 | : INHERITED(info, stream) |
| 547 | , fBitsPerPixel(bitsPerPixel) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 548 | , fRowOrder(rowOrder) |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 549 | , fSrcRowBytes(SkAlign4(compute_row_bytes(info.width(), fBitsPerPixel))) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 550 | {} |
| 551 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 552 | bool SkBmpCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 553 | return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr); |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 554 | } |
| 555 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 556 | int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const { |
| 557 | if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 558 | return y; |
| 559 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 560 | SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 561 | return height - y - 1; |
| 562 | } |
| 563 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 564 | SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 565 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 566 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 567 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 568 | return kInvalidConversion; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 569 | } |
| 570 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 571 | return prepareToDecode(dstInfo, options, inputColorPtr, inputColorCount); |
| 572 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 573 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 574 | int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 575 | // Create a new image info representing the portion of the image to decode |
| 576 | SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 577 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 578 | // Decode the requested rows |
| 579 | return this->decodeRows(rowInfo, dst, rowBytes, this->options()); |
| 580 | } |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 581 | |
| 582 | bool SkBmpCodec::skipRows(int count) { |
| 583 | const size_t bytesToSkip = count * fSrcRowBytes; |
| 584 | return this->stream()->skip(bytesToSkip) == bytesToSkip; |
| 585 | } |
| 586 | |
| 587 | bool SkBmpCodec::onSkipScanlines(int count) { |
| 588 | return this->skipRows(count); |
| 589 | } |