Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "SkColorTable.h" | |
| 10 #include "SkEndian.h" | |
| 11 #include "SkStream.h" | |
| 12 | |
| 13 #include <algorithm> | |
| 14 | |
| 15 /* | |
| 16 * | |
| 17 * Get a byte from the buffer | |
| 18 * | |
| 19 */ | |
| 20 uint8_t get_byte(uint8_t* buffer, uint32_t i) { | |
| 21 return buffer[i]; | |
| 22 } | |
| 23 | |
| 24 /* | |
| 25 * | |
| 26 * Get a short from the buffer | |
| 27 * | |
| 28 */ | |
| 29 uint16_t get_short(uint8_t* buffer, uint32_t i) { | |
| 30 uint16_t result; | |
| 31 memcpy(&result, &(buffer[i]), 2); | |
| 32 #ifdef SK_CPU_BENDIAN | |
| 33 return SkEndianSwap16(result); | |
| 34 #else | |
| 35 return result; | |
| 36 #endif | |
| 37 } | |
| 38 | |
| 39 /* | |
| 40 * | |
| 41 * Get an int from the buffer | |
| 42 * | |
| 43 */ | |
| 44 uint32_t get_int(uint8_t* buffer, uint32_t i) { | |
| 45 uint32_t result; | |
| 46 memcpy(&result, &(buffer[i]), 4); | |
| 47 #ifdef SK_CPU_BENDIAN | |
| 48 return SkEndianSwap32(result); | |
| 49 #else | |
| 50 return result; | |
| 51 #endif | |
| 52 } | |
| 53 | |
| 54 /* | |
| 55 * | |
| 56 * Defines the version and type of the second bitmap header | |
| 57 * | |
| 58 */ | |
| 59 enum BitmapHeaderType { | |
| 60 kInfoV1_BitmapHeaderType, | |
| 61 kInfoV2_BitmapHeaderType, | |
| 62 kInfoV3_BitmapHeaderType, | |
| 63 kInfoV4_BitmapHeaderType, | |
| 64 kInfoV5_BitmapHeaderType, | |
| 65 kOS2V1_BitmapHeaderType, | |
| 66 kOS2VX_BitmapHeaderType, | |
| 67 kUnknown_BitmapHeaderType | |
| 68 }; | |
| 69 | |
| 70 /* | |
| 71 * | |
| 72 * Possible bitmap compression types | |
| 73 * | |
| 74 */ | |
| 75 enum BitmapCompressionMethod { | |
| 76 kNone_BitmapCompressionMethod = 0, | |
| 77 k8BitRLE_BitmapCompressionMethod = 1, | |
| 78 k4BitRLE_BitmapCompressionMethod = 2, | |
| 79 kBitMasks_BitmapCompressionMethod = 3, | |
| 80 kJpeg_BitmapCompressionMethod = 4, | |
| 81 kPng_BitmapCompressionMethod = 5, | |
| 82 kAlphaBitMasks_BitmapCompressionMethod = 6, | |
| 83 kCMYK_BitmapCompressionMethod = 11, | |
| 84 kCMYK8BitRLE_BitmapCompressionMethod = 12, | |
| 85 kCMYK4BitRLE_BitmapCompressionMethod = 13 | |
| 86 }; | |
| 87 | |
| 88 /* | |
| 89 * | |
| 90 * Checks the start of the stream to see if the image is a bitmap | |
| 91 * | |
| 92 */ | |
| 93 bool SkBmpCodec::IsBmp(SkStream* stream) { | |
| 94 const char bmpSig[] = { 'B', 'M' }; | |
| 95 char buffer[sizeof(bmpSig)]; | |
| 96 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && | |
| 97 !memcmp(buffer, bmpSig, sizeof(bmpSig)); | |
| 98 } | |
| 99 | |
| 100 /* | |
| 101 * | |
| 102 * Assumes IsBmp was called and returned true | |
| 103 * Creates a bitmap decoder | |
| 104 * Reads enough of the stream to determine the image format | |
| 105 * | |
| 106 */ | |
| 107 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { | |
| 108 // Header size constants | |
| 109 static const uint32_t kBmpHeaderBytes = 14; | |
| 110 static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; | |
| 111 static const uint32_t kBmpOS2V1Bytes = 12; | |
| 112 static const uint32_t kBmpOS2V2Bytes = 64; | |
| 113 static const uint32_t kBmpInfoBaseBytes = 16; | |
| 114 static const uint32_t kBmpInfoV1Bytes = 40; | |
| 115 static const uint32_t kBmpInfoV2Bytes = 52; | |
| 116 static const uint32_t kBmpInfoV3Bytes = 56; | |
| 117 static const uint32_t kBmpInfoV4Bytes = 108; | |
| 118 static const uint32_t kBmpInfoV5Bytes = 124; | |
| 119 static const uint32_t kBmpMaskBytes = 12; | |
| 120 | |
| 121 // Read the first header and the size of the second header | |
| 122 SkAutoTDeleteArray<uint8_t> hBuffer( | |
| 123 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); | |
| 124 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != | |
| 125 kBmpHeaderBytesPlusFour) { | |
| 126 SkDebugf("Error: unable to read first bitmap header.\n"); | |
| 127 return NULL; | |
| 128 } | |
| 129 //uint16_t signature = get_short(hBuffer, 0); | |
| 130 | |
| 131 // The total bytes in the bmp file | |
| 132 const uint32_t totalBytes = get_int(hBuffer.get(), 2); | |
| 133 SkASSERT(totalBytes > kBmpHeaderBytes + kBmpOS2V1Bytes); | |
|
scroggo
2015/02/27 17:04:01
This is not a valid assert.
Someone could create
msarett
2015/02/27 21:17:28
Agreed this has already started causing a problem.
| |
| 134 | |
| 135 //uint32_t reserved = get_int(hBuffer, 6); | |
| 136 | |
| 137 // The offset from the start of the file where the pixel data begins | |
| 138 const uint32_t offset = get_int(hBuffer.get(), 10); | |
| 139 SkASSERT(offset >= kBmpHeaderBytes + kBmpOS2V1Bytes); | |
| 140 | |
| 141 // The size of the second (info) header in bytes | |
| 142 // The size is the first field of the second header, so we have already | |
| 143 // read the first four infoBytes. | |
| 144 const uint32_t infoBytes = get_int(hBuffer.get(), 14); | |
| 145 const uint32_t infoBytesRemaining = infoBytes - 4; | |
|
scroggo
2015/02/27 17:04:02
Since infoBytes is read from the buffer, it could
| |
| 146 hBuffer.free(); | |
| 147 | |
| 148 // Read the second header | |
| 149 SkAutoTDeleteArray<uint8_t> iBuffer( | |
| 150 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); | |
|
scroggo
2015/02/27 17:04:01
Nit: Does this fit on one line? (We don't have a s
| |
| 151 if (stream->read(iBuffer.get(), infoBytesRemaining) != | |
| 152 infoBytesRemaining) { | |
| 153 SkDebugf("Error: unable to read second bitmap header.\n"); | |
| 154 return NULL; | |
| 155 } | |
| 156 | |
| 157 // The number of bits used per pixel in the pixel data | |
| 158 uint16_t bitsPerPixel; | |
| 159 | |
| 160 // The compression method for the pixel data | |
| 161 uint32_t compression = kNone_BitmapCompressionMethod; | |
| 162 | |
| 163 // Number of colors in the color table, defaults to 0 or max (see below) | |
| 164 uint32_t numColors = 0; | |
| 165 | |
| 166 // Bytes per color in the color table, early versions use 3, most use 4 | |
| 167 uint32_t bytesPerColor; | |
| 168 | |
| 169 // The image width and height | |
| 170 int width, height; | |
| 171 | |
| 172 // Determine image information depending on second header format | |
| 173 BitmapHeaderType headerType; | |
| 174 if (infoBytes >= kBmpInfoBaseBytes) { | |
| 175 // Check for the many partial versions of the OS 2 header | |
| 176 if ((infoBytes <= kBmpOS2V2Bytes && !(infoBytes & 3)) | |
| 177 || 42 == infoBytes || 46 == infoBytes) { | |
| 178 headerType = kOS2VX_BitmapHeaderType; | |
|
scroggo
2015/02/27 17:04:03
Since this type is unsupported, can you return her
msarett
2015/02/27 21:17:28
This type is supported. You have caught a bug. R
scroggo
2015/02/28 00:25:13
I'm confused, in patch set 6, it looks like we ret
| |
| 179 } | |
| 180 // Check for versions of the Windows headers | |
| 181 switch (infoBytes) { | |
| 182 case kBmpInfoV1Bytes: | |
| 183 headerType = kInfoV1_BitmapHeaderType; | |
| 184 break; | |
| 185 case kBmpInfoV2Bytes: | |
| 186 headerType = kInfoV2_BitmapHeaderType; | |
| 187 break; | |
| 188 case kBmpInfoV3Bytes: | |
| 189 headerType = kInfoV3_BitmapHeaderType; | |
| 190 break; | |
| 191 case kBmpInfoV4Bytes: | |
| 192 headerType = kInfoV4_BitmapHeaderType; | |
| 193 break; | |
| 194 case kBmpInfoV5Bytes: | |
| 195 headerType = kInfoV5_BitmapHeaderType; | |
| 196 break; | |
| 197 default: | |
| 198 // We do not signal an error here because there is the | |
| 199 // possibility of new or undocumented bmp header types. Most | |
| 200 // of the newer versions of bmp headers are similar to and | |
| 201 // build off of the older versions, so we may still be able to | |
| 202 // decode the bmp. | |
| 203 SkDebugf("Warning: unknown bmp header format.\n"); | |
| 204 headerType = kUnknown_BitmapHeaderType; | |
| 205 break; | |
| 206 } | |
| 207 SkASSERT(infoBytesRemaining >= 12); | |
|
scroggo
2015/02/27 17:04:01
Again, we need to handle the case where infoBytesR
msarett
2015/02/27 21:17:28
infoBytesRemaining is guaranteed to be large enoug
| |
| 208 width = get_int(iBuffer.get(), 0); | |
| 209 height = get_int(iBuffer.get(), 4); | |
| 210 //uint16_t planes = get_short(iBuffer, 8); | |
| 211 bitsPerPixel = get_short(iBuffer.get(), 10); | |
| 212 | |
| 213 // Some versions do not have this field, so we check before | |
| 214 // overwriting the default value. | |
| 215 if (infoBytesRemaining >= 16) { | |
| 216 compression = get_int(iBuffer.get(), 12); | |
| 217 } | |
| 218 //uint32_t imageBytes = get_int(iBuffer, 16); | |
| 219 //uint32_t horizontalResolution = get_int(iBuffer, 20); | |
| 220 //uint32_t verticalResolution = get_int(iBuffer, 24); | |
| 221 | |
| 222 // Some versions do not have this field, so we check before | |
| 223 // overwriting the default value. | |
| 224 if (infoBytesRemaining >= 32) { | |
| 225 numColors = get_int(iBuffer.get(), 28); | |
| 226 } | |
| 227 //uint32_t importantColors = get_int(iBuffer, infoBytes - 4, 32); | |
| 228 bytesPerColor = 4; | |
| 229 } else if (infoBytes >= kBmpOS2V1Bytes) { | |
| 230 // The OS2V1 is treated separately because it has a unique format | |
| 231 headerType = kOS2V1_BitmapHeaderType; | |
| 232 width = (int) get_short(iBuffer.get(), 0); | |
| 233 height = (int) get_short(iBuffer.get(), 2); | |
| 234 //uint16_t planes = get_short(iBuffer.get(), 4); | |
| 235 bitsPerPixel = get_short(iBuffer.get(), 6); | |
| 236 bytesPerColor = 3; | |
| 237 } else { | |
| 238 // There are no valid bmp headers | |
| 239 SkDebugf("Error: second bitmap header size is invalid.\n"); | |
| 240 return NULL; | |
| 241 } | |
| 242 | |
| 243 // Check for valid dimensions from header | |
| 244 static const uint32_t kBmpMaxDim = 1 << 16; | |
| 245 bool inverted = true; | |
| 246 if (height < 0) { | |
| 247 height = -height; | |
| 248 inverted = false; | |
| 249 } | |
| 250 if (width <= 0 || width > kBmpMaxDim || !height || height > kBmpMaxDim) { | |
|
scroggo
2015/02/27 17:04:01
Is it possible for width or height to be greater t
msarett
2015/02/27 21:17:28
Yes. One version of the header reads them as int1
scroggo
2015/02/28 00:25:13
Ah, my bad - I missed the other assignment - using
| |
| 251 SkDebugf("Error: invalid bitmap dimensions.\n"); | |
| 252 return NULL; | |
| 253 } | |
| 254 | |
| 255 // Determine the input compression format and set bit masks if necessary | |
| 256 uint32_t redMask = 0, greenMask = 0, blueMask = 0, alphaMask = 0; | |
|
scroggo
2015/02/27 17:04:02
Why not use an SkSwizzler::ColorMask here?
| |
| 257 uint32_t maskBytes = 0; | |
| 258 BitmapInputFormat inputFormat = kUnknown_BitmapInputFormat; | |
| 259 switch (compression) { | |
| 260 case kNone_BitmapCompressionMethod: | |
| 261 inputFormat = kStandard_BitmapInputFormat; | |
| 262 // Always respect alpha mask in V4+ | |
| 263 if (headerType == kInfoV4_BitmapHeaderType || | |
| 264 headerType == kInfoV5_BitmapHeaderType) { | |
| 265 SkASSERT(infoBytesRemaining > 52); | |
|
scroggo
2015/02/27 17:04:03
My mistake. I suggested using a compile assert (wh
msarett
2015/02/27 21:17:28
In this situation, we are guaranteed to have enoug
| |
| 266 alphaMask = get_int(iBuffer.get(), 48); | |
| 267 } | |
| 268 break; | |
| 269 case k8BitRLE_BitmapCompressionMethod: | |
| 270 if (bitsPerPixel != 8) { | |
| 271 SkDebugf("Warning: correcting invalid bitmap format.\n"); | |
| 272 bitsPerPixel = 8; | |
| 273 } | |
| 274 inputFormat = k8BitRLE_BitmapInputFormat; | |
| 275 break; | |
| 276 case k4BitRLE_BitmapCompressionMethod: | |
| 277 if (bitsPerPixel != 4) { | |
| 278 SkDebugf("Warning: correcting invalid bitmap format.\n"); | |
| 279 bitsPerPixel = 4; | |
| 280 } | |
| 281 inputFormat = k4BitRLE_BitmapInputFormat; | |
| 282 break; | |
| 283 case kAlphaBitMasks_BitmapCompressionMethod: | |
| 284 case kBitMasks_BitmapCompressionMethod: | |
| 285 // Load the masks | |
| 286 inputFormat = kBitMask_BitmapInputFormat; | |
| 287 switch (headerType) { | |
| 288 case kInfoV1_BitmapHeaderType: { | |
| 289 // The V1 header stores the bit masks after the header | |
| 290 SkAutoTDeleteArray<uint8_t> mBuffer( | |
| 291 SkNEW_ARRAY(uint8_t, kBmpMaskBytes)); | |
| 292 if (stream->read(mBuffer.get(), kBmpMaskBytes) != | |
| 293 kBmpMaskBytes) { | |
| 294 SkDebugf("Error: unable to read bit masks.\n"); | |
| 295 return NULL; | |
| 296 } | |
| 297 maskBytes = kBmpMaskBytes; | |
| 298 redMask = get_int(mBuffer.get(), 0); | |
| 299 greenMask = get_int(mBuffer.get(), 4); | |
| 300 blueMask = get_int(mBuffer.get(), 8); | |
| 301 break; | |
| 302 } | |
| 303 case kInfoV4_BitmapHeaderType: | |
| 304 case kInfoV5_BitmapHeaderType: | |
| 305 SkASSERT(infoBytesRemaining >= 52); | |
| 306 alphaMask = get_int(iBuffer.get(), 48); | |
| 307 case kInfoV2_BitmapHeaderType: | |
| 308 case kInfoV3_BitmapHeaderType: | |
| 309 SkASSERT(infoBytesRemaining >= 48); | |
| 310 redMask = get_int(iBuffer.get(), 36); | |
| 311 greenMask = get_int(iBuffer.get(), 40); | |
| 312 blueMask = get_int(iBuffer.get(), 44); | |
| 313 break; | |
| 314 case kOS2VX_BitmapHeaderType: | |
| 315 // TODO: Decide if we intend to support this. | |
| 316 // It is unsupported in the previous version and | |
| 317 // in chromium. I have not come across a test case | |
| 318 // that uses this format. | |
| 319 SkDebugf("Error: huffman format unsupported.\n"); | |
| 320 return NULL; | |
| 321 default: | |
| 322 SkDebugf("Error: invalid bmp bit masks header.\n"); | |
| 323 return NULL; | |
| 324 } | |
| 325 break; | |
| 326 case kJpeg_BitmapCompressionMethod: | |
| 327 case kPng_BitmapCompressionMethod: | |
| 328 // TODO: Decide if we intend to support this. | |
| 329 // It is unsupported in the previous version and | |
| 330 // in chromium. I think it is used mostly for printers. | |
| 331 SkDebugf("Error: compression format not supported.\n"); | |
| 332 return NULL; | |
| 333 case kCMYK_BitmapCompressionMethod: | |
| 334 case kCMYK8BitRLE_BitmapCompressionMethod: | |
| 335 case kCMYK4BitRLE_BitmapCompressionMethod: | |
| 336 // TODO: Same as above. | |
| 337 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); | |
| 338 return NULL; | |
| 339 default: | |
| 340 SkDebugf("Error: invalid format for bitmap decoding.\n"); | |
| 341 return NULL; | |
| 342 } | |
| 343 iBuffer.free(); | |
| 344 | |
| 345 // Check for valid bits per pixel input | |
| 346 switch (bitsPerPixel) { | |
| 347 // In addition to more standard pixel compression formats, bmp supports | |
| 348 // the use of bit masks to determine pixel components. The bmp standard | |
| 349 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), | |
| 350 // which does not map well to any Skia color formats. For this reason, | |
| 351 // we will always enable mask mode with 16 bits per pixel. | |
| 352 case 16: | |
| 353 if (kBitMask_BitmapInputFormat != inputFormat) { | |
| 354 redMask = 0x7C00; | |
| 355 greenMask = 0x03E0; | |
| 356 blueMask = 0x001F; | |
| 357 } | |
|
scroggo
2015/02/27 17:04:01
Nit: It was hard for me to notice this fall throug
| |
| 358 case 1: | |
| 359 case 2: | |
| 360 case 4: | |
| 361 case 8: | |
| 362 case 24: | |
| 363 case 32: | |
| 364 break; | |
| 365 default: | |
| 366 SkDebugf("Error: invalid input value for bits per pixel.\n"); | |
| 367 return NULL; | |
| 368 } | |
| 369 | |
| 370 // Create mask struct | |
| 371 SkSwizzler::ColorMasks* masks = SkNEW(SkSwizzler::ColorMasks); | |
|
scroggo
2015/02/27 17:04:01
I'd recommend stack allocating this, and copy cons
| |
| 372 masks->redMask = redMask; | |
| 373 masks->greenMask = greenMask; | |
| 374 masks->blueMask = blueMask; | |
| 375 masks->alphaMask = alphaMask; | |
| 376 | |
| 377 // Verify the number of colors for the color table | |
| 378 if (bitsPerPixel < 16) { | |
| 379 const int maxColors = 1 << bitsPerPixel; | |
| 380 // Zero is a default for maxColors | |
| 381 // Also set numColors to maxColors when input is too large | |
| 382 if (numColors <= 0 || numColors > maxColors) { | |
| 383 numColors = maxColors; | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 // Construct the color table | |
| 388 uint32_t colorBytes = numColors * bytesPerColor; | |
|
scroggo
2015/02/27 17:04:03
Can be const?
| |
| 389 SkPMColor* colorTable = SkNEW_ARRAY(SkPMColor, numColors); | |
|
scroggo
2015/02/27 17:04:02
Any reason you're not using an SkColorTable?
Also
| |
| 390 if (bitsPerPixel < 16) { | |
| 391 SkAutoTDeleteArray<uint8_t> cBuffer( | |
| 392 SkNEW_ARRAY(uint8_t, colorBytes)); | |
|
scroggo
2015/02/27 17:04:03
nit: I think this could fit on one line.
| |
| 393 if (stream->read(cBuffer.get(), colorBytes) != colorBytes) { | |
| 394 SkDebugf("Error: unable to read color table.\n"); | |
| 395 return NULL; | |
| 396 } | |
| 397 // We must respect the alpha channel for V4 and V5. However, if it is | |
| 398 // all zeros, we will display the image as opaque rather than | |
| 399 // transparent. This may require redoing some of the processing. | |
| 400 bool seenNonZeroAlpha = false; | |
| 401 for (uint32_t i = 0; i < numColors; i++) { | |
| 402 uint8_t blue = get_byte(cBuffer.get(), i*bytesPerColor); | |
| 403 uint8_t green = get_byte(cBuffer.get(), i*bytesPerColor + 1); | |
| 404 uint8_t red = get_byte(cBuffer.get(), i*bytesPerColor + 2); | |
| 405 uint8_t alpha = 0xFF; | |
| 406 if (headerType == kInfoV4_BitmapHeaderType || | |
| 407 headerType == kInfoV5_BitmapHeaderType) { | |
| 408 alpha = (alphaMask >> 24) & | |
| 409 get_byte(cBuffer.get(), i*bytesPerColor + 3); | |
| 410 if (!alpha && !seenNonZeroAlpha) { | |
| 411 alpha = 0xFF; | |
| 412 } else { | |
| 413 // If we see a non-zero alpha, we restart the loop | |
| 414 seenNonZeroAlpha = true; | |
| 415 i = -1; | |
| 416 } | |
| 417 } | |
| 418 colorTable[i] = SkPreMultiplyColor(SkColorSetARGBInline(alpha, | |
| 419 red, green, blue)); | |
| 420 } | |
| 421 } else { | |
| 422 // We will not use the color table if bitsPerPixel >= 16, but if there | |
| 423 // is a color table, we may need to skip the color table bytes. | |
| 424 if (stream->skip(colorBytes) != colorBytes) { | |
| 425 SkDebugf("Error: Could not skip color table bytes.\n"); | |
| 426 return NULL; | |
| 427 } | |
| 428 } | |
| 429 | |
| 430 // Ensure that the stream now points to the start of the pixel array | |
| 431 uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes + colorBytes; | |
| 432 if (stream->skip(offset - bytesRead) != offset - bytesRead) { | |
| 433 SkDebugf("Error: unable to skip to image data.\n"); | |
| 434 return NULL; | |
| 435 } | |
| 436 const uint32_t remainingBytes = totalBytes - offset; | |
| 437 | |
| 438 // Return the codec | |
| 439 // Use of image info for input format does not make sense given | |
| 440 // that the possible bitmap input formats do not match up with | |
| 441 // Skia color types. Instead we use ImageInfo for width and height, | |
| 442 // and other fields for input format information. | |
| 443 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, | |
| 444 kN32_SkColorType, kPremul_SkAlphaType); | |
| 445 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, | |
| 446 inputFormat, masks, colorTable, inverted, | |
| 447 remainingBytes)); | |
| 448 } | |
| 449 | |
| 450 /* | |
| 451 * | |
| 452 * Creates an instance of the decoder | |
| 453 * Called only by NewFromStream | |
| 454 * | |
| 455 */ | |
| 456 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, | |
| 457 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, | |
| 458 SkSwizzler::ColorMasks* masks, SkPMColor* colorTable, | |
| 459 bool inverted, const uint32_t remainingBytes) | |
| 460 : INHERITED(info, stream) | |
| 461 , fBitsPerPixel(bitsPerPixel) | |
| 462 , fInputFormat(inputFormat) | |
| 463 , fBitMasks(masks) | |
| 464 , fColorTable(colorTable) | |
| 465 , fInverted(inverted) | |
| 466 , fRemainingBytes(remainingBytes) | |
| 467 {} | |
| 468 | |
| 469 /* | |
| 470 * | |
| 471 * Initiates the bitmap decode | |
| 472 * | |
| 473 */ | |
| 474 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, | |
| 475 void* dst, size_t dstRowBytes, | |
| 476 SkPMColor*, int*) { | |
| 477 // This version of the decoder does not support scaling | |
| 478 if (dstInfo.dimensions() != getOriginalInfo().dimensions()) { | |
| 479 SkDebugf("Error: scaling not supported.\n"); | |
| 480 return kInvalidScale; | |
| 481 } | |
| 482 | |
| 483 switch (fInputFormat) { | |
| 484 case k4BitRLE_BitmapInputFormat: | |
| 485 case k8BitRLE_BitmapInputFormat: | |
| 486 return decodeRLE(dstInfo, dst, dstRowBytes); | |
| 487 case kBitMask_BitmapInputFormat: | |
| 488 case kStandard_BitmapInputFormat: | |
| 489 return decode(dstInfo, dst, dstRowBytes); | |
| 490 default: | |
| 491 SkDebugf("Error: unknown bitmap input format.\n"); | |
| 492 return kInvalidInput; | |
| 493 } | |
| 494 } | |
| 495 | |
| 496 /* | |
| 497 * | |
| 498 * Performs the bitmap decoding for standard and bit masks input format | |
| 499 * | |
| 500 */ | |
| 501 SkCodec::Result SkBmpCodec::decode(const SkImageInfo& dstInfo, | |
| 502 void* dst, uint32_t dstRowBytes) { | |
| 503 // Set constant values | |
| 504 const int width = dstInfo.width(); | |
| 505 const int height = dstInfo.height(); | |
| 506 const uint32_t pixelsPerByte = 8 / fBitsPerPixel; | |
| 507 const uint32_t bytesPerPixel = fBitsPerPixel / 8; | |
| 508 const uint32_t unpaddedRowBytes = fBitsPerPixel < 16 ? | |
| 509 (width + pixelsPerByte - 1) / pixelsPerByte : width * bytesPerPixel; | |
| 510 const uint32_t paddedRowBytes = (unpaddedRowBytes + 3) & (~3); | |
| 511 const uint32_t alphaMask = fBitMasks.get()->alphaMask; | |
|
scroggo
2015/02/27 17:04:01
FYI: SkAutoTDelete defines operator->, so this can
| |
| 512 | |
| 513 // Get swizzler configuration | |
| 514 SkSwizzler::SrcConfig config; | |
| 515 switch (fBitsPerPixel) { | |
| 516 case 1: | |
| 517 config = SkSwizzler::kIndex1; | |
| 518 break; | |
| 519 case 2: | |
| 520 config = SkSwizzler::kIndex2; | |
| 521 break; | |
| 522 case 4: | |
| 523 config = SkSwizzler::kIndex4; | |
| 524 break; | |
| 525 case 8: | |
| 526 config = SkSwizzler::kIndex; | |
| 527 break; | |
| 528 case 16: | |
| 529 config = SkSwizzler::kMask16; | |
| 530 break; | |
| 531 case 24: | |
| 532 if (kBitMask_BitmapInputFormat == fInputFormat) { | |
| 533 config = SkSwizzler::kMask24; | |
| 534 } else { | |
| 535 config = SkSwizzler::kBGR; | |
| 536 } | |
| 537 break; | |
| 538 case 32: | |
| 539 if (kBitMask_BitmapInputFormat == fInputFormat) { | |
| 540 config = SkSwizzler::kMask32; | |
| 541 } else if (!alphaMask) { | |
| 542 config = SkSwizzler::kBGRX; | |
| 543 } else { | |
| 544 config = SkSwizzler::kBGRA; | |
| 545 } | |
| 546 break; | |
| 547 default: | |
| 548 SkDebugf("Error: default case should be unreachable.\n"); | |
|
scroggo
2015/02/27 17:04:01
I'd put an SkASSERT(false), if it should never be
| |
| 549 return kInvalidInput; | |
| 550 } | |
| 551 | |
| 552 // If zeroAlpha is kNormal, it indicates that the image will be | |
| 553 // considered as encoded. If kTransparentAsOpaque, we will respect the | |
| 554 // value of the alpha channel if it is nonzero for any of the pixels. | |
| 555 // However, if it is always zero, we will consider the image opaque instead | |
| 556 // of transparent. This may require redoing some of the decoding. | |
| 557 SkSwizzler::ZeroAlpha zeroAlpha = SkSwizzler::kNormal; | |
| 558 if (alphaMask) { | |
| 559 zeroAlpha = SkSwizzler::kTransparentAsOpaque; | |
| 560 } | |
| 561 | |
| 562 // Create swizzler | |
| 563 SkSwizzler* swizzler = SkSwizzler::CreateSwizzler(config, fColorTable.get(), | |
| 564 dstInfo, dst, dstRowBytes, false, fBitMasks.get(), zeroAlpha, | |
| 565 fInverted ? SkSwizzler::kBottomUp : SkSwizzler::kTopDown); | |
|
scroggo
2015/02/27 17:04:02
I'd recommend storing fInverted as this enum.
| |
| 566 | |
| 567 // Allocate space for a row buffer and a source for the swizzler | |
| 568 SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, paddedRowBytes)); | |
| 569 | |
| 570 // Iterate over rows of the image | |
| 571 for (uint32_t y = 0; y < height; y++) { | |
| 572 // Read a row of the input | |
| 573 if (stream()->read(srcBuffer.get(), paddedRowBytes) != paddedRowBytes) { | |
| 574 return kIncompleteInput; | |
| 575 } | |
| 576 | |
| 577 // Decode the row in destination format | |
| 578 swizzler->next(srcBuffer.get()); | |
| 579 } | |
| 580 | |
| 581 // Finished decoding the entire image | |
| 582 return kSuccess; | |
| 583 } | |
| 584 | |
| 585 /* | |
| 586 * | |
| 587 * Set an RLE pixel using the color table | |
| 588 * | |
| 589 */ | |
| 590 void SkBmpCodec::setPixel(SkPMColor* dst, uint32_t dstRowBytes, int height, | |
|
scroggo
2015/02/27 17:04:01
Maybe this should be called setRLEPixel.
Also, no
| |
| 591 uint32_t x, uint32_t y, uint8_t index) { | |
|
scroggo
2015/02/27 17:04:01
nit: should line up with SkPMColor.
| |
| 592 if (fInverted) { | |
| 593 y = height - y - 1; | |
| 594 } | |
| 595 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, y * dstRowBytes); | |
| 596 dstRow[x] = fColorTable.get()[index]; | |
| 597 return; | |
|
scroggo
2015/02/27 17:04:01
Not needed.
| |
| 598 } | |
| 599 | |
| 600 /* | |
| 601 * | |
| 602 * Performs the bitmap decoding for RLE input format | |
| 603 * RLE decoding is performed all at once, rather than a one row at a time | |
| 604 * | |
| 605 */ | |
| 606 SkCodec::Result SkBmpCodec::decodeRLE(const SkImageInfo& dstInfo, | |
| 607 void* dst, uint32_t dstRowBytes) { | |
| 608 // Set RLE flags | |
| 609 static const uint8_t RLE_ESCAPE = 0; | |
| 610 static const uint8_t RLE_EOL = 0; | |
| 611 static const uint8_t RLE_EOF = 1; | |
| 612 static const uint8_t RLE_DELTA = 2; | |
| 613 | |
| 614 // Set constant values | |
| 615 const int width = dstInfo.width(); | |
| 616 const int height = dstInfo.height(); | |
| 617 const uint32_t ppb = 8 / fBitsPerPixel; | |
| 618 | |
| 619 // Input buffer parameters | |
| 620 uint32_t i = 0; | |
| 621 SkAutoTDeleteArray<uint8_t> buffer(SkNEW_ARRAY(uint8_t, fRemainingBytes)); | |
| 622 uint32_t totalBytes = stream()->read(buffer.get(), fRemainingBytes); | |
| 623 if (totalBytes < fRemainingBytes) { | |
| 624 SkDebugf("Warning: incomplete RLE file.\n"); | |
| 625 } else if (totalBytes <= 0) { | |
| 626 SkDebugf("Error: could not read RLE image data.\n"); | |
| 627 return kInvalidInput; | |
| 628 } | |
| 629 | |
| 630 // Destination parameters | |
| 631 uint32_t x = 0; | |
| 632 uint32_t y = 0; | |
| 633 // If the code hits EOL or EOF early, remaining pixels are transparent | |
|
scroggo
2015/02/27 17:04:01
To be fair, they're only transparent in the case o
| |
| 634 memset(dst, 0, dstRowBytes * height); | |
| 635 SkPMColor* dstPtr = (SkPMColor*) dst; | |
| 636 | |
| 637 while (true) { | |
| 638 // Every entry takes at least two bytes | |
| 639 if (totalBytes - i < 2) { | |
| 640 SkDebugf("Error: incomplete RLE input.\n"); | |
| 641 return kIncompleteInput; | |
| 642 } | |
| 643 | |
| 644 // Read the two bytes and verify we have not reached end of image | |
| 645 const uint8_t count = buffer.get()[i++]; | |
| 646 const uint8_t code = buffer.get()[i++]; | |
| 647 if ((count || (code != RLE_EOF)) && y > height) { | |
| 648 SkDebugf("Error: invalid RLE input.\n"); | |
| 649 return kInvalidInput; | |
| 650 } | |
| 651 | |
| 652 // Perform decoding | |
| 653 if (RLE_ESCAPE == count) { | |
| 654 switch (code) { | |
| 655 case RLE_EOL: | |
| 656 x = 0; | |
| 657 y++; | |
| 658 break; | |
| 659 case RLE_EOF: | |
| 660 return kSuccess; | |
| 661 case RLE_DELTA: { | |
| 662 // Two bytes are needed to specify delta | |
| 663 if (totalBytes - i < 2) { | |
| 664 SkDebugf("Error: incomplete RLE input\n"); | |
| 665 return kIncompleteInput; | |
| 666 } | |
| 667 // Verify that we are not past the end of row or image | |
| 668 const uint8_t dx = buffer.get()[i++]; | |
| 669 const uint8_t dy = buffer.get()[i++]; | |
| 670 if (x + dx > width || y + dy > height) { | |
| 671 SkDebugf("Error: invalid RLE input.\n"); | |
| 672 return kInvalidInput; | |
| 673 } | |
| 674 // Move to new location | |
| 675 x += dx; | |
|
scroggo
2015/02/27 17:04:01
Nit: this is probably premature optimization on my
| |
| 676 y += dy; | |
| 677 break; | |
| 678 } | |
| 679 default: { // Absolute mode | |
| 680 // Check that we have enough bytes and that there are | |
| 681 // enough pixels remaining in the row | |
| 682 uint32_t unpaddedBytes = (code + ppb - 1) / ppb; | |
| 683 uint32_t paddedBytes = (unpaddedBytes + 1) & ~1; | |
| 684 if (x + code > width || totalBytes - i < paddedBytes) { | |
| 685 SkDebugf("Error: invalid RLE input.\n"); | |
| 686 return kInvalidInput; | |
| 687 } | |
| 688 // Use the color table to set the coded number of pixels | |
| 689 uint8_t num = code; | |
| 690 while (num > 0) { | |
| 691 switch(fBitsPerPixel) { | |
| 692 case 4: { | |
| 693 uint8_t val = buffer.get()[i++]; | |
| 694 setPixel(dstPtr, dstRowBytes, height, x++, y, | |
| 695 val >> 4); | |
|
scroggo
2015/02/27 17:04:03
nit: This should line up with dstPtr
| |
| 696 num--; | |
| 697 if (num) { | |
| 698 setPixel(dstPtr, dstRowBytes, height, | |
| 699 x++, y, val & 0xF); | |
| 700 num--; | |
| 701 } | |
| 702 break; | |
| 703 } | |
| 704 case 8: | |
| 705 setPixel(dstPtr, dstRowBytes, height, x++, y, | |
| 706 buffer.get()[i++]); | |
| 707 num--; | |
| 708 break; | |
| 709 default: | |
| 710 SkDebugf("Error: invalid RLE bpp.\n"); | |
| 711 return kInvalidInput; | |
| 712 } | |
| 713 } | |
| 714 // Skip a byte if necessary to maintain alignment | |
| 715 if (unpaddedBytes & 1) { | |
| 716 i++; | |
| 717 } | |
| 718 break; | |
| 719 } | |
| 720 } | |
| 721 } else { // Encoded mode | |
| 722 // Ensure we do not move past the end of the row | |
| 723 const int endX = std::min(x + count, (uint32_t) width); | |
|
scroggo
2015/02/27 17:04:03
Nit: We typically use our own min functions, defin
| |
| 724 | |
| 725 // RLE8 has one color index that gets repeated | |
| 726 // RLE4 has two color indexes in the upper and lower 4 bits of the | |
| 727 // bytes, which are alternated | |
| 728 uint8_t indexes[2] = { code, code }; | |
|
scroggo
2015/02/27 17:04:01
nit: indices?
| |
| 729 if (fBitsPerPixel == 4) { | |
| 730 indexes[0] >>= 4; | |
| 731 indexes[1] &= 0xf; | |
| 732 } | |
| 733 | |
| 734 // Set the indicated number of pixels | |
| 735 for (int which = 0; x < endX; x++) { | |
| 736 setPixel(dstPtr, dstRowBytes, height, x, y, indexes[which]); | |
| 737 which = !which; | |
| 738 } | |
| 739 } | |
| 740 } | |
| 741 } | |
| OLD | NEW |