| 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 | |
| 8 | #include "SkCodec_libbmp.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkStream.h" |
| 12 | |
| 13 | /* |
| 14 | * |
| 15 | * Checks if the conversion between the input image and the requested output |
| 16 | * image has been implemented |
| 17 | * |
| 18 | */ |
| 19 | static bool conversion_possible(const SkImageInfo& dst, |
| 20 | const SkImageInfo& src) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 21 | // Ensure that the profile type is unchanged |
| 22 | if (dst.profileType() != src.profileType()) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 23 | return false; |
| 24 | } |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 25 | |
| 26 | // Check for supported color and alpha types |
| 27 | switch (dst.colorType()) { |
| 28 | case kN32_SkColorType: |
| 29 | return src.alphaType() == dst.alphaType() || |
| 30 | (kPremul_SkAlphaType == dst.alphaType() && |
| 31 | kUnpremul_SkAlphaType == src.alphaType()); |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 32 | default: |
| 33 | return false; |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 34 | } |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* |
| 38 | * |
| 39 | * Defines the version and type of the second bitmap header |
| 40 | * |
| 41 | */ |
| 42 | enum BitmapHeaderType { |
| 43 | kInfoV1_BitmapHeaderType, |
| 44 | kInfoV2_BitmapHeaderType, |
| 45 | kInfoV3_BitmapHeaderType, |
| 46 | kInfoV4_BitmapHeaderType, |
| 47 | kInfoV5_BitmapHeaderType, |
| 48 | kOS2V1_BitmapHeaderType, |
| 49 | kOS2VX_BitmapHeaderType, |
| 50 | kUnknown_BitmapHeaderType |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * |
| 55 | * Possible bitmap compression types |
| 56 | * |
| 57 | */ |
| 58 | enum BitmapCompressionMethod { |
| 59 | kNone_BitmapCompressionMethod = 0, |
| 60 | k8BitRLE_BitmapCompressionMethod = 1, |
| 61 | k4BitRLE_BitmapCompressionMethod = 2, |
| 62 | kBitMasks_BitmapCompressionMethod = 3, |
| 63 | kJpeg_BitmapCompressionMethod = 4, |
| 64 | kPng_BitmapCompressionMethod = 5, |
| 65 | kAlphaBitMasks_BitmapCompressionMethod = 6, |
| 66 | kCMYK_BitmapCompressionMethod = 11, |
| 67 | kCMYK8BitRLE_BitmapCompressionMethod = 12, |
| 68 | kCMYK4BitRLE_BitmapCompressionMethod = 13 |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * |
| 73 | * Checks the start of the stream to see if the image is a bitmap |
| 74 | * |
| 75 | */ |
| 76 | bool SkBmpCodec::IsBmp(SkStream* stream) { |
| 77 | // TODO: Support "IC", "PT", "CI", "CP", "BA" |
| 78 | // TODO: ICO files may contain a BMP and need to use this decoder |
| 79 | const char bmpSig[] = { 'B', 'M' }; |
| 80 | char buffer[sizeof(bmpSig)]; |
| 81 | return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && |
| 82 | !memcmp(buffer, bmpSig, sizeof(bmpSig)); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * |
| 87 | * Assumes IsBmp was called and returned true |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 88 | * Creates a bmp decoder |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 89 | * Reads enough of the stream to determine the image format |
| 90 | * |
| 91 | */ |
| 92 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 93 | return SkBmpCodec::NewFromStream(stream, false); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * |
| 98 | * Creates a bmp decoder for a bmp embedded in ico |
| 99 | * Reads enough of the stream to determine the image format |
| 100 | * |
| 101 | */ |
| 102 | SkCodec* SkBmpCodec::NewFromIco(SkStream* stream) { |
| 103 | return SkBmpCodec::NewFromStream(stream, true); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * |
| 108 | * Creates a bmp decoder |
| 109 | * Reads enough of the stream to determine the image format |
| 110 | * |
| 111 | */ |
| 112 | SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool isIco) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 113 | // Header size constants |
| 114 | static const uint32_t kBmpHeaderBytes = 14; |
| 115 | static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
| 116 | static const uint32_t kBmpOS2V1Bytes = 12; |
| 117 | static const uint32_t kBmpOS2V2Bytes = 64; |
| 118 | static const uint32_t kBmpInfoBaseBytes = 16; |
| 119 | static const uint32_t kBmpInfoV1Bytes = 40; |
| 120 | static const uint32_t kBmpInfoV2Bytes = 52; |
| 121 | static const uint32_t kBmpInfoV3Bytes = 56; |
| 122 | static const uint32_t kBmpInfoV4Bytes = 108; |
| 123 | static const uint32_t kBmpInfoV5Bytes = 124; |
| 124 | static const uint32_t kBmpMaskBytes = 12; |
| 125 | |
| tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 126 | // The total bytes in the bmp file |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 127 | // We only need to use this value for RLE decoding, so we will only |
| 128 | // check that it is valid in the RLE case. |
| 129 | uint32_t totalBytes; |
| tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 130 | // The offset from the start of the file where the pixel data begins |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 131 | uint32_t offset; |
| 132 | // The size of the second (info) header in bytes |
| 133 | uint32_t infoBytes; |
| 134 | |
| 135 | // Bmps embedded in Icos skip the first Bmp header |
| 136 | if (!isIco) { |
| 137 | // Read the first header and the size of the second header |
| 138 | SkAutoTDeleteArray<uint8_t> hBuffer( |
| 139 | SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); |
| 140 | if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != |
| 141 | kBmpHeaderBytesPlusFour) { |
| 142 | SkDebugf("Error: unable to read first bitmap header.\n"); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | totalBytes = get_int(hBuffer.get(), 2); |
| 147 | offset = get_int(hBuffer.get(), 10); |
| 148 | if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
| 149 | SkDebugf("Error: invalid starting location for pixel data\n"); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | // The size of the second (info) header in bytes |
| 154 | // The size is the first field of the second header, so we have already |
| 155 | // read the first four infoBytes. |
| 156 | infoBytes = get_int(hBuffer.get(), 14); |
| 157 | if (infoBytes < kBmpOS2V1Bytes) { |
| 158 | SkDebugf("Error: invalid second header size.\n"); |
| 159 | return NULL; |
| 160 | } |
| 161 | } else { |
| 162 | // This value is only used by RLE compression. Bmp in Ico files do not |
| 163 | // use RLE. If the compression field is incorrectly signaled as RLE, |
| 164 | // we will catch this and signal an error below. |
| 165 | totalBytes = 0; |
| 166 | |
| 167 | // Bmps in Ico cannot specify an offset. We will always assume that |
| 168 | // pixel data begins immediately after the color table. This value |
| 169 | // will be corrected below. |
| 170 | offset = 0; |
| 171 | |
| 172 | // Read the size of the second header |
| 173 | SkAutoTDeleteArray<uint8_t> hBuffer( |
| 174 | SkNEW_ARRAY(uint8_t, 4)); |
| 175 | if (stream->read(hBuffer.get(), 4) != 4) { |
| 176 | SkDebugf("Error: unable to read size of second bitmap header.\n"); |
| 177 | return NULL; |
| 178 | } |
| 179 | infoBytes = get_int(hBuffer.get(), 0); |
| 180 | if (infoBytes < kBmpOS2V1Bytes) { |
| 181 | SkDebugf("Error: invalid second header size.\n"); |
| 182 | return NULL; |
| 183 | } |
| tomhudson | 7aa846c | 2015-03-24 13:47:41 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 186 | // 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] | 187 | const uint32_t infoBytesRemaining = infoBytes - 4; |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 188 | |
| 189 | // Read the second header |
| 190 | SkAutoTDeleteArray<uint8_t> iBuffer( |
| 191 | SkNEW_ARRAY(uint8_t, infoBytesRemaining)); |
| 192 | if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
| 193 | SkDebugf("Error: unable to read second bitmap header.\n"); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | // The number of bits used per pixel in the pixel data |
| 198 | uint16_t bitsPerPixel; |
| 199 | |
| 200 | // The compression method for the pixel data |
| 201 | uint32_t compression = kNone_BitmapCompressionMethod; |
| 202 | |
| 203 | // Number of colors in the color table, defaults to 0 or max (see below) |
| 204 | uint32_t numColors = 0; |
| 205 | |
| 206 | // Bytes per color in the color table, early versions use 3, most use 4 |
| 207 | uint32_t bytesPerColor; |
| 208 | |
| 209 | // The image width and height |
| 210 | int width, height; |
| 211 | |
| 212 | // Determine image information depending on second header format |
| 213 | BitmapHeaderType headerType; |
| 214 | if (infoBytes >= kBmpInfoBaseBytes) { |
| 215 | // Check the version of the header |
| 216 | switch (infoBytes) { |
| 217 | case kBmpInfoV1Bytes: |
| 218 | headerType = kInfoV1_BitmapHeaderType; |
| 219 | break; |
| 220 | case kBmpInfoV2Bytes: |
| 221 | headerType = kInfoV2_BitmapHeaderType; |
| 222 | break; |
| 223 | case kBmpInfoV3Bytes: |
| 224 | headerType = kInfoV3_BitmapHeaderType; |
| 225 | break; |
| 226 | case kBmpInfoV4Bytes: |
| 227 | headerType = kInfoV4_BitmapHeaderType; |
| 228 | break; |
| 229 | case kBmpInfoV5Bytes: |
| 230 | headerType = kInfoV5_BitmapHeaderType; |
| 231 | break; |
| 232 | case 16: |
| 233 | case 20: |
| 234 | case 24: |
| 235 | case 28: |
| 236 | case 32: |
| 237 | case 36: |
| 238 | case 42: |
| 239 | case 46: |
| 240 | case 48: |
| 241 | case 60: |
| 242 | case kBmpOS2V2Bytes: |
| 243 | headerType = kOS2VX_BitmapHeaderType; |
| 244 | break; |
| 245 | default: |
| 246 | // We do not signal an error here because there is the |
| 247 | // possibility of new or undocumented bmp header types. Most |
| 248 | // of the newer versions of bmp headers are similar to and |
| 249 | // build off of the older versions, so we may still be able to |
| 250 | // decode the bmp. |
| 251 | SkDebugf("Warning: unknown bmp header format.\n"); |
| 252 | headerType = kUnknown_BitmapHeaderType; |
| 253 | break; |
| 254 | } |
| 255 | // We check the size of the header before entering the if statement. |
| 256 | // We should not reach this point unless the size is large enough for |
| 257 | // these required fields. |
| 258 | SkASSERT(infoBytesRemaining >= 12); |
| 259 | width = get_int(iBuffer.get(), 0); |
| 260 | height = get_int(iBuffer.get(), 4); |
| 261 | bitsPerPixel = get_short(iBuffer.get(), 10); |
| 262 | |
| 263 | // Some versions do not have these fields, so we check before |
| 264 | // overwriting the default value. |
| 265 | if (infoBytesRemaining >= 16) { |
| 266 | compression = get_int(iBuffer.get(), 12); |
| 267 | if (infoBytesRemaining >= 32) { |
| 268 | numColors = get_int(iBuffer.get(), 28); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // All of the headers that reach this point, store color table entries |
| 273 | // using 4 bytes per pixel. |
| 274 | bytesPerColor = 4; |
| 275 | } else if (infoBytes >= kBmpOS2V1Bytes) { |
| 276 | // The OS2V1 is treated separately because it has a unique format |
| 277 | headerType = kOS2V1_BitmapHeaderType; |
| 278 | width = (int) get_short(iBuffer.get(), 0); |
| 279 | height = (int) get_short(iBuffer.get(), 2); |
| 280 | bitsPerPixel = get_short(iBuffer.get(), 6); |
| 281 | bytesPerColor = 3; |
| 282 | } else { |
| 283 | // There are no valid bmp headers |
| 284 | SkDebugf("Error: second bitmap header size is invalid.\n"); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | // Check for valid dimensions from header |
| 289 | RowOrder rowOrder = kBottomUp_RowOrder; |
| 290 | if (height < 0) { |
| 291 | height = -height; |
| 292 | rowOrder = kTopDown_RowOrder; |
| 293 | } |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 294 | // The height field for bmp in ico is double the actual height because they |
| 295 | // contain an XOR mask followed by an AND mask |
| 296 | if (isIco) { |
| 297 | height /= 2; |
| 298 | } |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 299 | static const int kBmpMaxDim = 1 << 16; |
| 300 | if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { |
| 301 | // TODO: Decide if we want to support really large bmps. |
| 302 | SkDebugf("Error: invalid bitmap dimensions.\n"); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | // Create mask struct |
| 307 | SkMasks::InputMasks inputMasks; |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 308 | memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 309 | |
| 310 | // Determine the input compression format and set bit masks if necessary |
| 311 | uint32_t maskBytes = 0; |
| 312 | BitmapInputFormat inputFormat = kUnknown_BitmapInputFormat; |
| 313 | switch (compression) { |
| 314 | case kNone_BitmapCompressionMethod: |
| 315 | inputFormat = kStandard_BitmapInputFormat; |
| 316 | break; |
| 317 | case k8BitRLE_BitmapCompressionMethod: |
| 318 | if (bitsPerPixel != 8) { |
| 319 | SkDebugf("Warning: correcting invalid bitmap format.\n"); |
| 320 | bitsPerPixel = 8; |
| 321 | } |
| 322 | inputFormat = kRLE_BitmapInputFormat; |
| 323 | break; |
| 324 | case k4BitRLE_BitmapCompressionMethod: |
| 325 | if (bitsPerPixel != 4) { |
| 326 | SkDebugf("Warning: correcting invalid bitmap format.\n"); |
| 327 | bitsPerPixel = 4; |
| 328 | } |
| 329 | inputFormat = kRLE_BitmapInputFormat; |
| 330 | break; |
| 331 | case kAlphaBitMasks_BitmapCompressionMethod: |
| 332 | case kBitMasks_BitmapCompressionMethod: |
| 333 | // Load the masks |
| 334 | inputFormat = kBitMask_BitmapInputFormat; |
| 335 | switch (headerType) { |
| 336 | case kInfoV1_BitmapHeaderType: { |
| 337 | // The V1 header stores the bit masks after the header |
| 338 | SkAutoTDeleteArray<uint8_t> mBuffer( |
| 339 | SkNEW_ARRAY(uint8_t, kBmpMaskBytes)); |
| 340 | if (stream->read(mBuffer.get(), kBmpMaskBytes) != |
| 341 | kBmpMaskBytes) { |
| 342 | SkDebugf("Error: unable to read bit inputMasks.\n"); |
| 343 | return NULL; |
| 344 | } |
| 345 | maskBytes = kBmpMaskBytes; |
| 346 | inputMasks.red = get_int(mBuffer.get(), 0); |
| 347 | inputMasks.green = get_int(mBuffer.get(), 4); |
| 348 | inputMasks.blue = get_int(mBuffer.get(), 8); |
| 349 | break; |
| 350 | } |
| 351 | case kInfoV2_BitmapHeaderType: |
| 352 | case kInfoV3_BitmapHeaderType: |
| 353 | case kInfoV4_BitmapHeaderType: |
| 354 | case kInfoV5_BitmapHeaderType: |
| 355 | // Header types are matched based on size. If the header |
| 356 | // is V2+, we are guaranteed to be able to read at least |
| 357 | // this size. |
| 358 | SkASSERT(infoBytesRemaining >= 48); |
| 359 | inputMasks.red = get_int(iBuffer.get(), 36); |
| 360 | inputMasks.green = get_int(iBuffer.get(), 40); |
| 361 | inputMasks.blue = get_int(iBuffer.get(), 44); |
| 362 | break; |
| 363 | case kOS2VX_BitmapHeaderType: |
| 364 | // TODO: Decide if we intend to support this. |
| 365 | // It is unsupported in the previous version and |
| 366 | // in chromium. I have not come across a test case |
| 367 | // that uses this format. |
| 368 | SkDebugf("Error: huffman format unsupported.\n"); |
| 369 | return NULL; |
| 370 | default: |
| 371 | SkDebugf("Error: invalid bmp bit masks header.\n"); |
| 372 | return NULL; |
| 373 | } |
| 374 | break; |
| 375 | case kJpeg_BitmapCompressionMethod: |
| 376 | if (24 == bitsPerPixel) { |
| 377 | inputFormat = kRLE_BitmapInputFormat; |
| 378 | break; |
| 379 | } |
| 380 | // Fall through |
| 381 | case kPng_BitmapCompressionMethod: |
| 382 | // TODO: Decide if we intend to support this. |
| 383 | // It is unsupported in the previous version and |
| 384 | // in chromium. I think it is used mostly for printers. |
| 385 | SkDebugf("Error: compression format not supported.\n"); |
| 386 | return NULL; |
| 387 | case kCMYK_BitmapCompressionMethod: |
| 388 | case kCMYK8BitRLE_BitmapCompressionMethod: |
| 389 | case kCMYK4BitRLE_BitmapCompressionMethod: |
| 390 | // TODO: Same as above. |
| 391 | SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); |
| 392 | return NULL; |
| 393 | default: |
| 394 | SkDebugf("Error: invalid format for bitmap decoding.\n"); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | // Most versions of bmps should be rendered as opaque. Either they do |
| 399 | // not have an alpha channel, or they expect the alpha channel to be |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 400 | // ignored. V3+ bmp files introduce an alpha mask and allow the creator |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 401 | // of the image to use the alpha channels. However, many of these images |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 402 | // leave the alpha channel blank and expect to be rendered as opaque. This |
| 403 | // is the case for almost all V3 images, so we render these as opaque. For |
| 404 | // V4+, we will use the alpha channel, and fix the image later if it turns |
| 405 | // out to be fully transparent. |
| 406 | // As an exception, V3 bmp-in-ico may use an alpha mask. |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 407 | SkAlphaType alphaType = kOpaque_SkAlphaType; |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 408 | if ((kInfoV3_BitmapHeaderType == headerType && isIco) || |
| 409 | kInfoV4_BitmapHeaderType == headerType || |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 410 | kInfoV5_BitmapHeaderType == headerType) { |
| 411 | // Header types are matched based on size. If the header is |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 412 | // V3+, we are guaranteed to be able to read at least this size. |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 413 | SkASSERT(infoBytesRemaining > 52); |
| 414 | inputMasks.alpha = get_int(iBuffer.get(), 48); |
| 415 | if (inputMasks.alpha != 0) { |
| 416 | alphaType = kUnpremul_SkAlphaType; |
| 417 | } |
| 418 | } |
| 419 | iBuffer.free(); |
| 420 | |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 421 | // Additionally, 32 bit bmp-in-icos use the alpha channel |
| 422 | if (isIco && 32 == bitsPerPixel) { |
| 423 | alphaType = kUnpremul_SkAlphaType; |
| 424 | } |
| 425 | |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 426 | // Check for valid bits per pixel input |
| 427 | switch (bitsPerPixel) { |
| 428 | // In addition to more standard pixel compression formats, bmp supports |
| 429 | // the use of bit masks to determine pixel components. The standard |
| 430 | // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
| 431 | // which does not map well to any Skia color formats. For this reason, |
| 432 | // we will always enable mask mode with 16 bits per pixel. |
| 433 | case 16: |
| 434 | if (kBitMask_BitmapInputFormat != inputFormat) { |
| 435 | inputMasks.red = 0x7C00; |
| 436 | inputMasks.green = 0x03E0; |
| 437 | inputMasks.blue = 0x001F; |
| 438 | inputFormat = kBitMask_BitmapInputFormat; |
| 439 | } |
| 440 | break; |
| 441 | case 1: |
| 442 | case 2: |
| 443 | case 4: |
| 444 | case 8: |
| 445 | case 24: |
| 446 | case 32: |
| 447 | break; |
| 448 | default: |
| 449 | SkDebugf("Error: invalid input value for bits per pixel.\n"); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | // Check that input bit masks are valid and create the masks object |
| 454 | SkAutoTDelete<SkMasks> |
| 455 | masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel)); |
| 456 | if (NULL == masks) { |
| 457 | SkDebugf("Error: invalid input masks.\n"); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 461 | // Check for a valid number of total bytes when in RLE mode |
| 462 | if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 463 | SkDebugf("Error: RLE requires valid input size.\n"); |
| 464 | return NULL; |
| 465 | } |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 466 | const size_t RLEBytes = totalBytes - offset; |
| 467 | |
| 468 | // Calculate the number of bytes read so far |
| 469 | const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 470 | if (!isIco && offset < bytesRead) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 471 | SkDebugf("Error: pixel data offset less than header size.\n"); |
| 472 | return NULL; |
| 473 | } |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 474 | |
| 475 | // Return the codec |
| 476 | // We will use ImageInfo to store width, height, and alpha type. We will |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 477 | // set color type to kN32_SkColorType because that should be the default |
| 478 | // output. |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 479 | const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, |
| 480 | kN32_SkColorType, alphaType); |
| 481 | return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 482 | inputFormat, masks.detach(), numColors, |
| 483 | bytesPerColor, offset - bytesRead, |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 484 | rowOrder, RLEBytes, isIco)); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | * |
| 489 | * Creates an instance of the decoder |
| 490 | * Called only by NewFromStream |
| 491 | * |
| 492 | */ |
| 493 | SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, |
| 494 | uint16_t bitsPerPixel, BitmapInputFormat inputFormat, |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 495 | SkMasks* masks, uint32_t numColors, |
| 496 | uint32_t bytesPerColor, uint32_t offset, |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 497 | RowOrder rowOrder, size_t RLEBytes, bool isIco) |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 498 | : INHERITED(info, stream) |
| 499 | , fBitsPerPixel(bitsPerPixel) |
| 500 | , fInputFormat(inputFormat) |
| 501 | , fMasks(masks) |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 502 | , fColorTable(NULL) |
| 503 | , fNumColors(numColors) |
| 504 | , fBytesPerColor(bytesPerColor) |
| 505 | , fOffset(offset) |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 506 | , fRowOrder(rowOrder) |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 507 | , fRLEBytes(RLEBytes) |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 508 | , fIsIco(isIco) |
| 509 | |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 510 | {} |
| 511 | |
| 512 | /* |
| 513 | * |
| 514 | * Initiates the bitmap decode |
| 515 | * |
| 516 | */ |
| 517 | SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 518 | void* dst, size_t dstRowBytes, |
| scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 519 | const Options&, |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 520 | SkPMColor*, int*) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 521 | // Check for proper input and output formats |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 522 | if (!this->rewindIfNeeded()) { |
| 523 | return kCouldNotRewind; |
| 524 | } |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 525 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 526 | SkDebugf("Error: scaling not supported.\n"); |
| 527 | return kInvalidScale; |
| 528 | } |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 529 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 530 | SkDebugf("Error: cannot convert input type to output type.\n"); |
| 531 | return kInvalidConversion; |
| 532 | } |
| 533 | |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 534 | // Create the color table if necessary and prepare the stream for decode |
| 535 | if (!createColorTable(dstInfo.alphaType())) { |
| 536 | SkDebugf("Error: could not create color table.\n"); |
| 537 | return kInvalidInput; |
| 538 | } |
| 539 | |
| 540 | // Perform the decode |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 541 | switch (fInputFormat) { |
| 542 | case kBitMask_BitmapInputFormat: |
| 543 | return decodeMask(dstInfo, dst, dstRowBytes); |
| 544 | case kRLE_BitmapInputFormat: |
| 545 | return decodeRLE(dstInfo, dst, dstRowBytes); |
| 546 | case kStandard_BitmapInputFormat: |
| 547 | return decode(dstInfo, dst, dstRowBytes); |
| 548 | default: |
| 549 | SkASSERT(false); |
| 550 | return kInvalidInput; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 556 | * Process the color table for the bmp input |
| 557 | * |
| 558 | */ |
| 559 | bool SkBmpCodec::createColorTable(SkAlphaType alphaType) { |
| 560 | // Allocate memory for color table |
| 561 | uint32_t colorBytes = 0; |
| 562 | uint32_t maxColors = 0; |
| 563 | SkPMColor colorTable[256]; |
| 564 | if (fBitsPerPixel <= 8) { |
| 565 | // Zero is a default for maxColors |
| 566 | // Also set fNumColors to maxColors when it is too large |
| 567 | maxColors = 1 << fBitsPerPixel; |
| 568 | if (fNumColors == 0 || fNumColors >= maxColors) { |
| 569 | fNumColors = maxColors; |
| 570 | } |
| 571 | |
| 572 | // Read the color table from the stream |
| 573 | colorBytes = fNumColors * fBytesPerColor; |
| 574 | SkAutoTDeleteArray<uint8_t> cBuffer(SkNEW_ARRAY(uint8_t, colorBytes)); |
| 575 | if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
| 576 | SkDebugf("Error: unable to read color table.\n"); |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | // Choose the proper packing function |
| 581 | SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t); |
| 582 | switch (alphaType) { |
| 583 | case kOpaque_SkAlphaType: |
| 584 | case kUnpremul_SkAlphaType: |
| 585 | packARGB = &SkPackARGB32NoCheck; |
| 586 | break; |
| 587 | case kPremul_SkAlphaType: |
| 588 | packARGB = &SkPreMultiplyARGB; |
| 589 | break; |
| 590 | default: |
| 591 | // This should not be reached because conversion possible |
| 592 | // should fail if the alpha type is not one of the above |
| 593 | // values. |
| 594 | SkASSERT(false); |
| 595 | packARGB = NULL; |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | // Fill in the color table |
| 600 | uint32_t i = 0; |
| 601 | for (; i < fNumColors; i++) { |
| 602 | uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 603 | uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 604 | uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 605 | uint8_t alpha = kOpaque_SkAlphaType == alphaType ? 0xFF : |
| 606 | (fMasks->getAlphaMask() >> 24) & |
| 607 | get_byte(cBuffer.get(), i*fBytesPerColor + 3); |
| 608 | colorTable[i] = packARGB(alpha, red, green, blue); |
| 609 | } |
| 610 | |
| 611 | // To avoid segmentation faults on bad pixel data, fill the end of the |
| 612 | // color table with black. This is the same the behavior as the |
| 613 | // chromium decoder. |
| 614 | for (; i < maxColors; i++) { |
| 615 | colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 616 | } |
| 617 | } |
| 618 | |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 619 | // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
| 620 | // begins. Pixel data always begins immediately after the color table. |
| 621 | if (!fIsIco) { |
| 622 | // Check that we have not read past the pixel array offset |
| 623 | if(fOffset < colorBytes) { |
| 624 | // This may occur on OS 2.1 and other old versions where the color |
| 625 | // table defaults to max size, and the bmp tries to use a smaller |
| 626 | // color table. This is invalid, and our decision is to indicate |
| 627 | // an error, rather than try to guess the intended size of the |
| 628 | // color table. |
| 629 | SkDebugf("Error: pixel data offset less than color table size.\n"); |
| 630 | return false; |
| 631 | } |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 632 | |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 633 | // After reading the color table, skip to the start of the pixel array |
| 634 | if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 635 | SkDebugf("Error: unable to skip to image data.\n"); |
| 636 | return false; |
| 637 | } |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | // Set the color table and return true on success |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 641 | fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 642 | return true; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 647 | * Performs the bitmap decoding for bit masks input format |
| 648 | * |
| 649 | */ |
| 650 | SkCodec::Result SkBmpCodec::decodeMask(const SkImageInfo& dstInfo, |
| 651 | void* dst, size_t dstRowBytes) { |
| 652 | // Set constant values |
| 653 | const int width = dstInfo.width(); |
| 654 | const int height = dstInfo.height(); |
| 655 | const size_t rowBytes = SkAlign4(compute_row_bytes(width, fBitsPerPixel)); |
| 656 | |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 657 | // Allocate a buffer large enough to hold the full image |
| 658 | SkAutoTDeleteArray<uint8_t> |
| 659 | srcBuffer(SkNEW_ARRAY(uint8_t, height*rowBytes)); |
| 660 | uint8_t* srcRow = srcBuffer.get(); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 661 | |
| 662 | // Create the swizzler |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 663 | SkAutoTDelete<SkMaskSwizzler> maskSwizzler( |
| 664 | SkMaskSwizzler::CreateMaskSwizzler(dstInfo, dst, dstRowBytes, |
| 665 | fMasks, fBitsPerPixel)); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 666 | |
| 667 | // Iterate over rows of the image |
| 668 | bool transparent = true; |
| 669 | for (int y = 0; y < height; y++) { |
| 670 | // Read a row of the input |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 671 | if (stream()->read(srcRow, rowBytes) != rowBytes) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 672 | SkDebugf("Warning: incomplete input stream.\n"); |
| 673 | return kIncompleteInput; |
| 674 | } |
| 675 | |
| 676 | // Decode the row in destination format |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 677 | int row = kBottomUp_RowOrder == fRowOrder ? height - 1 - y : y; |
| 678 | SkSwizzler::ResultAlpha r = maskSwizzler->next(srcRow, row); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 679 | transparent &= SkSwizzler::IsTransparent(r); |
| 680 | |
| 681 | // Move to the next row |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 682 | srcRow = SkTAddOffset<uint8_t>(srcRow, rowBytes); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // Some fully transparent bmp images are intended to be opaque. Here, we |
| 686 | // correct for this possibility. |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 687 | if (transparent) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 688 | const SkImageInfo& opaqueInfo = |
| 689 | dstInfo.makeAlphaType(kOpaque_SkAlphaType); |
| 690 | SkAutoTDelete<SkMaskSwizzler> opaqueSwizzler( |
| 691 | SkMaskSwizzler::CreateMaskSwizzler(opaqueInfo, dst, dstRowBytes, |
| 692 | fMasks, fBitsPerPixel)); |
| 693 | srcRow = srcBuffer.get(); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 694 | for (int y = 0; y < height; y++) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 695 | // Decode the row in opaque format |
| 696 | int row = kBottomUp_RowOrder == fRowOrder ? height - 1 - y : y; |
| 697 | opaqueSwizzler->next(srcRow, row); |
| 698 | |
| 699 | // Move to the next row |
| 700 | srcRow = SkTAddOffset<uint8_t>(srcRow, rowBytes); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | // Finished decoding the entire image |
| 705 | return kSuccess; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * |
| 710 | * Set an RLE pixel using the color table |
| 711 | * |
| 712 | */ |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 713 | void SkBmpCodec::setRLEPixel(SkPMColor* dst, size_t dstRowBytes, |
| 714 | const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
| 715 | uint8_t index) { |
| 716 | // Set the row |
| 717 | int height = dstInfo.height(); |
| 718 | int row; |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 719 | if (kBottomUp_RowOrder == fRowOrder) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 720 | row = height - y - 1; |
| 721 | } else { |
| 722 | row = y; |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 723 | } |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 724 | |
| 725 | // Set the pixel based on destination color type |
| 726 | switch (dstInfo.colorType()) { |
| 727 | case kN32_SkColorType: { |
| 728 | SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, |
| 729 | row * (int) dstRowBytes); |
| 730 | dstRow[x] = fColorTable->operator[](index); |
| 731 | break; |
| 732 | } |
| 733 | case kRGB_565_SkColorType: { |
| 734 | uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, |
| 735 | row * (int) dstRowBytes); |
| 736 | dstRow[x] = SkPixel32ToPixel16(fColorTable->operator[](index)); |
| 737 | break; |
| 738 | } |
| 739 | default: |
| 740 | // This case should not be reached. We should catch an invalid |
| 741 | // color type when we check that the conversion is possible. |
| 742 | SkASSERT(false); |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * |
| 749 | * Set an RLE pixel from R, G, B values |
| 750 | * |
| 751 | */ |
| 752 | void SkBmpCodec::setRLE24Pixel(SkPMColor* dst, size_t dstRowBytes, |
| 753 | const SkImageInfo& dstInfo, uint32_t x, |
| 754 | uint32_t y, uint8_t red, uint8_t green, |
| 755 | uint8_t blue) { |
| 756 | // Set the row |
| 757 | int height = dstInfo.height(); |
| 758 | int row; |
| 759 | if (kBottomUp_RowOrder == fRowOrder) { |
| 760 | row = height - y - 1; |
| 761 | } else { |
| 762 | row = y; |
| 763 | } |
| 764 | |
| 765 | // Set the pixel based on destination color type |
| 766 | switch (dstInfo.colorType()) { |
| 767 | case kN32_SkColorType: { |
| 768 | SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, |
| 769 | row * (int) dstRowBytes); |
| 770 | dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 771 | break; |
| 772 | } |
| 773 | case kRGB_565_SkColorType: { |
| 774 | uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, |
| 775 | row * (int) dstRowBytes); |
| 776 | dstRow[x] = SkPack888ToRGB16(red, green, blue); |
| 777 | break; |
| 778 | } |
| 779 | default: |
| 780 | // This case should not be reached. We should catch an invalid |
| 781 | // color type when we check that the conversion is possible. |
| 782 | SkASSERT(false); |
| 783 | break; |
| 784 | } |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
| 788 | * |
| 789 | * Performs the bitmap decoding for RLE input format |
| 790 | * RLE decoding is performed all at once, rather than a one row at a time |
| 791 | * |
| 792 | */ |
| 793 | SkCodec::Result SkBmpCodec::decodeRLE(const SkImageInfo& dstInfo, |
| 794 | void* dst, size_t dstRowBytes) { |
| 795 | // Set RLE flags |
| 796 | static const uint8_t RLE_ESCAPE = 0; |
| 797 | static const uint8_t RLE_EOL = 0; |
| 798 | static const uint8_t RLE_EOF = 1; |
| 799 | static const uint8_t RLE_DELTA = 2; |
| 800 | |
| 801 | // Set constant values |
| 802 | const int width = dstInfo.width(); |
| 803 | const int height = dstInfo.height(); |
| 804 | |
| 805 | // Input buffer parameters |
| 806 | uint32_t currByte = 0; |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 807 | SkAutoTDeleteArray<uint8_t> buffer(SkNEW_ARRAY(uint8_t, fRLEBytes)); |
| 808 | size_t totalBytes = stream()->read(buffer.get(), fRLEBytes); |
| 809 | if (totalBytes < fRLEBytes) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 810 | SkDebugf("Warning: incomplete RLE file.\n"); |
| 811 | } else if (totalBytes <= 0) { |
| 812 | SkDebugf("Error: could not read RLE image data.\n"); |
| 813 | return kInvalidInput; |
| 814 | } |
| 815 | |
| 816 | // Destination parameters |
| 817 | int x = 0; |
| 818 | int y = 0; |
| 819 | // If the code skips pixels, remaining pixels are transparent or black |
| 820 | // TODO: Skip this if memory was already zeroed. |
| 821 | memset(dst, 0, dstRowBytes * height); |
| 822 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 823 | |
| 824 | while (true) { |
| 825 | // Every entry takes at least two bytes |
| 826 | if ((int) totalBytes - currByte < 2) { |
| 827 | SkDebugf("Warning: incomplete RLE input.\n"); |
| 828 | return kIncompleteInput; |
| 829 | } |
| 830 | |
| 831 | // Read the next two bytes. These bytes have different meanings |
| 832 | // depending on their values. In the first interpretation, the first |
| 833 | // byte is an escape flag and the second byte indicates what special |
| 834 | // task to perform. |
| 835 | const uint8_t flag = buffer.get()[currByte++]; |
| 836 | const uint8_t task = buffer.get()[currByte++]; |
| 837 | |
| 838 | // If we have reached a row that is beyond the image size, and the RLE |
| 839 | // code does not indicate end of file, abort and signal a warning. |
| 840 | if (y >= height && (flag != RLE_ESCAPE || (task != RLE_EOF))) { |
| 841 | SkDebugf("Warning: invalid RLE input.\n"); |
| 842 | return kIncompleteInput; |
| 843 | } |
| 844 | |
| 845 | // Perform decoding |
| 846 | if (RLE_ESCAPE == flag) { |
| 847 | switch (task) { |
| 848 | case RLE_EOL: |
| 849 | x = 0; |
| 850 | y++; |
| 851 | break; |
| 852 | case RLE_EOF: |
| 853 | return kSuccess; |
| 854 | case RLE_DELTA: { |
| 855 | // Two bytes are needed to specify delta |
| 856 | if ((int) totalBytes - currByte < 2) { |
| 857 | SkDebugf("Warning: incomplete RLE input\n"); |
| 858 | return kIncompleteInput; |
| 859 | } |
| 860 | // Modify x and y |
| 861 | const uint8_t dx = buffer.get()[currByte++]; |
| 862 | const uint8_t dy = buffer.get()[currByte++]; |
| 863 | x += dx; |
| 864 | y += dy; |
| 865 | if (x > width || y > height) { |
| 866 | SkDebugf("Warning: invalid RLE input.\n"); |
| 867 | return kIncompleteInput; |
| 868 | } |
| 869 | break; |
| 870 | } |
| 871 | default: { |
| 872 | // If task does not match any of the above signals, it |
| 873 | // indicates that we have a sequence of non-RLE pixels. |
| 874 | // Furthermore, the value of task is equal to the number |
| 875 | // of pixels to interpret. |
| 876 | uint8_t numPixels = task; |
| 877 | const size_t rowBytes = compute_row_bytes(numPixels, |
| 878 | fBitsPerPixel); |
| 879 | // Abort if setting numPixels moves us off the edge of the |
| 880 | // image. Also abort if there are not enough bytes |
| 881 | // remaining in the stream to set numPixels. |
| 882 | if (x + numPixels > width || |
| 883 | (int) totalBytes - currByte < SkAlign2(rowBytes)) { |
| 884 | SkDebugf("Warning: invalid RLE input.\n"); |
| 885 | return kIncompleteInput; |
| 886 | } |
| 887 | // Set numPixels number of pixels |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 888 | while (numPixels > 0) { |
| 889 | switch(fBitsPerPixel) { |
| 890 | case 4: { |
| 891 | SkASSERT(currByte < totalBytes); |
| 892 | uint8_t val = buffer.get()[currByte++]; |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 893 | setRLEPixel(dstPtr, dstRowBytes, dstInfo, x++, |
| 894 | y, val >> 4); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 895 | numPixels--; |
| 896 | if (numPixels != 0) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 897 | setRLEPixel(dstPtr, dstRowBytes, dstInfo, |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 898 | x++, y, val & 0xF); |
| 899 | numPixels--; |
| 900 | } |
| 901 | break; |
| 902 | } |
| 903 | case 8: |
| 904 | SkASSERT(currByte < totalBytes); |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 905 | setRLEPixel(dstPtr, dstRowBytes, dstInfo, x++, |
| 906 | y, buffer.get()[currByte++]); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 907 | numPixels--; |
| 908 | break; |
| 909 | case 24: { |
| 910 | SkASSERT(currByte + 2 < totalBytes); |
| 911 | uint8_t blue = buffer.get()[currByte++]; |
| 912 | uint8_t green = buffer.get()[currByte++]; |
| 913 | uint8_t red = buffer.get()[currByte++]; |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 914 | setRLE24Pixel(dstPtr, dstRowBytes, dstInfo, |
| 915 | x++, y, red, green, blue); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 916 | numPixels--; |
| 917 | } |
| 918 | default: |
| 919 | SkASSERT(false); |
| 920 | return kInvalidInput; |
| 921 | } |
| 922 | } |
| 923 | // Skip a byte if necessary to maintain alignment |
| 924 | if (!SkIsAlign2(rowBytes)) { |
| 925 | currByte++; |
| 926 | } |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | } else { |
| 931 | // If the first byte read is not a flag, it indicates the number of |
| 932 | // pixels to set in RLE mode. |
| 933 | const uint8_t numPixels = flag; |
| 934 | const int endX = SkTMin<int>(x + numPixels, width); |
| 935 | |
| 936 | if (24 == fBitsPerPixel) { |
| 937 | // In RLE24, the second byte read is part of the pixel color. |
| 938 | // There are two more required bytes to finish encoding the |
| 939 | // color. |
| 940 | if ((int) totalBytes - currByte < 2) { |
| 941 | SkDebugf("Warning: incomplete RLE input\n"); |
| 942 | return kIncompleteInput; |
| 943 | } |
| 944 | |
| 945 | // Fill the pixels up to endX with the specified color |
| 946 | uint8_t blue = task; |
| 947 | uint8_t green = buffer.get()[currByte++]; |
| 948 | uint8_t red = buffer.get()[currByte++]; |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 949 | while (x < endX) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 950 | setRLE24Pixel(dstPtr, dstRowBytes, dstInfo, x++, y, red, |
| 951 | green, blue); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 952 | } |
| 953 | } else { |
| 954 | // In RLE8 or RLE4, the second byte read gives the index in the |
| 955 | // color table to look up the pixel color. |
| 956 | // RLE8 has one color index that gets repeated |
| 957 | // RLE4 has two color indexes in the upper and lower 4 bits of |
| 958 | // the bytes, which are alternated |
| 959 | uint8_t indices[2] = { task, task }; |
| 960 | if (4 == fBitsPerPixel) { |
| 961 | indices[0] >>= 4; |
| 962 | indices[1] &= 0xf; |
| 963 | } |
| 964 | |
| 965 | // Set the indicated number of pixels |
| 966 | for (int which = 0; x < endX; x++) { |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 967 | setRLEPixel(dstPtr, dstRowBytes, dstInfo, x, y, |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 968 | indices[which]); |
| 969 | which = !which; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * |
| 978 | * Performs the bitmap decoding for standard input format |
| 979 | * |
| 980 | */ |
| 981 | SkCodec::Result SkBmpCodec::decode(const SkImageInfo& dstInfo, |
| 982 | void* dst, size_t dstRowBytes) { |
| 983 | // Set constant values |
| 984 | const int width = dstInfo.width(); |
| 985 | const int height = dstInfo.height(); |
| 986 | const size_t rowBytes = SkAlign4(compute_row_bytes(width, fBitsPerPixel)); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 987 | |
| 988 | // Get swizzler configuration |
| 989 | SkSwizzler::SrcConfig config; |
| 990 | switch (fBitsPerPixel) { |
| 991 | case 1: |
| 992 | config = SkSwizzler::kIndex1; |
| 993 | break; |
| 994 | case 2: |
| 995 | config = SkSwizzler::kIndex2; |
| 996 | break; |
| 997 | case 4: |
| 998 | config = SkSwizzler::kIndex4; |
| 999 | break; |
| 1000 | case 8: |
| 1001 | config = SkSwizzler::kIndex; |
| 1002 | break; |
| 1003 | case 24: |
| 1004 | config = SkSwizzler::kBGR; |
| 1005 | break; |
| 1006 | case 32: |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 1007 | if (kOpaque_SkAlphaType == dstInfo.alphaType()) { |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 1008 | config = SkSwizzler::kBGRX; |
| 1009 | } else { |
| 1010 | config = SkSwizzler::kBGRA; |
| 1011 | } |
| 1012 | break; |
| 1013 | default: |
| 1014 | SkASSERT(false); |
| 1015 | return kInvalidInput; |
| 1016 | } |
| 1017 | |
| 1018 | // Create swizzler |
| msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 1019 | SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler(config, |
| 1020 | fColorTable->readColors(), dstInfo, dst, dstRowBytes, |
| 1021 | SkImageGenerator::kNo_ZeroInitialized)); |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 1022 | |
| 1023 | // Allocate space for a row buffer and a source for the swizzler |
| 1024 | SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, rowBytes)); |
| 1025 | |
| 1026 | // Iterate over rows of the image |
| 1027 | // FIXME: bool transparent = true; |
| 1028 | for (int y = 0; y < height; y++) { |
| 1029 | // Read a row of the input |
| 1030 | if (stream()->read(srcBuffer.get(), rowBytes) != rowBytes) { |
| 1031 | SkDebugf("Warning: incomplete input stream.\n"); |
| 1032 | return kIncompleteInput; |
| 1033 | } |
| 1034 | |
| 1035 | // Decode the row in destination format |
| 1036 | uint32_t row; |
| 1037 | if (kTopDown_RowOrder == fRowOrder) { |
| 1038 | row = y; |
| 1039 | } else { |
| 1040 | row = height - 1 - y; |
| 1041 | } |
| 1042 | |
| 1043 | swizzler->next(srcBuffer.get(), row); |
| 1044 | // FIXME: SkSwizzler::ResultAlpha r = |
| 1045 | // swizzler->next(srcBuffer.get(), row); |
| 1046 | // FIXME: transparent &= SkSwizzler::IsTransparent(r); |
| 1047 | } |
| 1048 | |
| 1049 | // FIXME: This code exists to match the behavior in the chromium decoder |
| 1050 | // and to follow the bmp specification as it relates to alpha masks. It is |
| 1051 | // commented out because we have yet to discover a test image that provides |
| 1052 | // an alpha mask and uses this decode mode. |
| 1053 | |
| 1054 | // Now we adjust the output image with some additional behavior that |
| 1055 | // SkSwizzler does not support. Firstly, all bmp images that contain |
| 1056 | // alpha are masked by the alpha mask. Secondly, many fully transparent |
| 1057 | // bmp images are intended to be opaque. Here, we make those corrections. |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 1058 | /* |
| 1059 | SkPMColor* dstRow = (SkPMColor*) dst; |
| 1060 | if (SkSwizzler::kBGRA == config) { |
| 1061 | for (int y = 0; y < height; y++) { |
| 1062 | for (int x = 0; x < width; x++) { |
| 1063 | if (transparent) { |
| 1064 | dstRow[x] |= 0xFF000000; |
| 1065 | } else { |
| 1066 | dstRow[x] &= alphaMask; |
| 1067 | } |
| 1068 | dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | */ |
| 1073 | |
| msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 1074 | // Finally, apply the AND mask for bmp-in-ico images |
| 1075 | if (fIsIco) { |
| 1076 | // The AND mask is always 1 bit per pixel |
| 1077 | const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1)); |
| 1078 | |
| 1079 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 1080 | for (int y = 0; y < height; y++) { |
| 1081 | // The srcBuffer will at least be large enough |
| 1082 | if (stream()->read(srcBuffer.get(), rowBytes) != rowBytes) { |
| 1083 | SkDebugf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
| 1084 | return kIncompleteInput; |
| 1085 | } |
| 1086 | |
| 1087 | int row; |
| 1088 | if (kBottomUp_RowOrder == fRowOrder) { |
| 1089 | row = height - y - 1; |
| 1090 | } else { |
| 1091 | row = y; |
| 1092 | } |
| 1093 | |
| 1094 | SkPMColor* dstRow = |
| 1095 | SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
| 1096 | |
| 1097 | for (int x = 0; x < width; x++) { |
| 1098 | int quotient; |
| 1099 | int modulus; |
| 1100 | SkTDivMod(x, 8, "ient, &modulus); |
| 1101 | uint32_t shift = 7 - modulus; |
| 1102 | uint32_t alphaBit = |
| 1103 | (srcBuffer.get()[quotient] >> shift) & 0x1; |
| 1104 | dstRow[x] &= alphaBit - 1; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | |
| msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 1109 | // Finished decoding the entire image |
| 1110 | return kSuccess; |
| 1111 | } |