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