| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "platform/image-decoders/bmp/BMPImageReader.h" | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // See comments on lookup_table_addresses_ in the header. | |
| 36 const uint8_t nBitTo8BitlookupTable[] = { | |
| 37 // 1 bit | |
| 38 0, 255, | |
| 39 // 2 bits | |
| 40 0, 85, 170, 255, | |
| 41 // 3 bits | |
| 42 0, 36, 73, 109, 146, 182, 219, 255, | |
| 43 // 4 bits | |
| 44 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, | |
| 45 // 5 bits | |
| 46 0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, | |
| 47 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255, | |
| 48 // 6 bits | |
| 49 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 45, 49, 53, 57, 61, 65, 69, 73, 77, | |
| 50 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142, | |
| 51 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 202, | |
| 52 206, 210, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, | |
| 53 // 7 bits | |
| 54 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, | |
| 55 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, | |
| 56 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, | |
| 57 112, 114, 116, 118, 120, 122, 124, 126, 129, 131, 133, 135, 137, 139, 141, | |
| 58 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, | |
| 59 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, | |
| 60 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, | |
| 61 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, | |
| 62 }; | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 namespace blink { | |
| 67 | |
| 68 BMPImageReader::BMPImageReader(ImageDecoder* parent, | |
| 69 size_t decoded_and_header_offset, | |
| 70 size_t img_data_offset, | |
| 71 bool is_in_ico) | |
| 72 : parent_(parent), | |
| 73 buffer_(0), | |
| 74 fast_reader_(nullptr), | |
| 75 decoded_offset_(decoded_and_header_offset), | |
| 76 header_offset_(decoded_and_header_offset), | |
| 77 img_data_offset_(img_data_offset), | |
| 78 is_os21x_(false), | |
| 79 is_os22x_(false), | |
| 80 is_top_down_(false), | |
| 81 need_to_process_bitmasks_(false), | |
| 82 need_to_process_color_table_(false), | |
| 83 seen_non_zero_alpha_pixel_(false), | |
| 84 seen_zero_alpha_pixel_(false), | |
| 85 is_in_ico_(is_in_ico), | |
| 86 decoding_and_mask_(false) { | |
| 87 // Clue-in decodeBMP() that we need to detect the correct info header size. | |
| 88 memset(&info_header_, 0, sizeof(info_header_)); | |
| 89 } | |
| 90 | |
| 91 bool BMPImageReader::DecodeBMP(bool only_size) { | |
| 92 // Defensively clear the FastSharedBufferReader's cache, as another caller | |
| 93 // may have called SharedBuffer::MergeSegmentsIntoBuffer(). | |
| 94 fast_reader_.ClearCache(); | |
| 95 | |
| 96 // Calculate size of info header. | |
| 97 if (!info_header_.bi_size && !ReadInfoHeaderSize()) | |
| 98 return false; | |
| 99 | |
| 100 const size_t header_end = header_offset_ + info_header_.bi_size; | |
| 101 // Read and process info header. | |
| 102 if ((decoded_offset_ < header_end) && !ProcessInfoHeader()) | |
| 103 return false; | |
| 104 | |
| 105 // ProcessInfoHeader() set the size, so if that's all we needed, we're done. | |
| 106 if (only_size) | |
| 107 return true; | |
| 108 | |
| 109 // Read and process the bitmasks, if needed. | |
| 110 if (need_to_process_bitmasks_ && !ProcessBitmasks()) | |
| 111 return false; | |
| 112 | |
| 113 // Read and process the color table, if needed. | |
| 114 if (need_to_process_color_table_ && !ProcessColorTable()) | |
| 115 return false; | |
| 116 | |
| 117 // Initialize the framebuffer if needed. | |
| 118 DCHECK(buffer_); // Parent should set this before asking us to decode! | |
| 119 if (buffer_->GetStatus() == ImageFrame::kFrameEmpty) { | |
| 120 if (!buffer_->AllocatePixelData(parent_->Size().Width(), | |
| 121 parent_->Size().Height(), | |
| 122 parent_->ColorSpaceForSkImages())) { | |
| 123 return parent_->SetFailed(); // Unable to allocate. | |
| 124 } | |
| 125 buffer_->ZeroFillPixelData(); | |
| 126 buffer_->SetStatus(ImageFrame::kFramePartial); | |
| 127 // SetSize() calls EraseARGB(), which resets the alpha flag, so we force | |
| 128 // it back to false here. We'll set it true below in all cases where | |
| 129 // these 0s could actually show through. | |
| 130 buffer_->SetHasAlpha(false); | |
| 131 | |
| 132 // For BMPs, the frame always fills the entire image. | |
| 133 buffer_->SetOriginalFrameRect(IntRect(IntPoint(), parent_->Size())); | |
| 134 | |
| 135 if (!is_top_down_) | |
| 136 coord_.SetY(parent_->Size().Height() - 1); | |
| 137 } | |
| 138 | |
| 139 // Decode the data. | |
| 140 if (!decoding_and_mask_ && !PastEndOfImage(0) && | |
| 141 !DecodePixelData((info_header_.bi_compression != RLE4) && | |
| 142 (info_header_.bi_compression != RLE8) && | |
| 143 (info_header_.bi_compression != RLE24))) | |
| 144 return false; | |
| 145 | |
| 146 // If the image has an AND mask and there was no alpha data, process the | |
| 147 // mask. | |
| 148 if (is_in_ico_ && !decoding_and_mask_ && | |
| 149 ((info_header_.bi_bit_count < 16) || !bit_masks_[3] || | |
| 150 !seen_non_zero_alpha_pixel_)) { | |
| 151 // Reset decoding coordinates to start of image. | |
| 152 coord_.SetX(0); | |
| 153 coord_.SetY(is_top_down_ ? 0 : (parent_->Size().Height() - 1)); | |
| 154 | |
| 155 // The AND mask is stored as 1-bit data. | |
| 156 info_header_.bi_bit_count = 1; | |
| 157 | |
| 158 decoding_and_mask_ = true; | |
| 159 } | |
| 160 if (decoding_and_mask_ && !DecodePixelData(true)) | |
| 161 return false; | |
| 162 | |
| 163 // Done! | |
| 164 buffer_->SetStatus(ImageFrame::kFrameComplete); | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 bool BMPImageReader::DecodePixelData(bool non_rle) { | |
| 169 const IntPoint coord(coord_); | |
| 170 const ProcessingResult result = | |
| 171 non_rle ? ProcessNonRLEData(false, 0) : ProcessRLEData(); | |
| 172 if (coord_ != coord) | |
| 173 buffer_->SetPixelsChanged(true); | |
| 174 return (result == kFailure) ? parent_->SetFailed() : (result == kSuccess); | |
| 175 } | |
| 176 | |
| 177 bool BMPImageReader::ReadInfoHeaderSize() { | |
| 178 // Get size of info header. | |
| 179 DCHECK_EQ(decoded_offset_, header_offset_); | |
| 180 if ((decoded_offset_ > data_->size()) || | |
| 181 ((data_->size() - decoded_offset_) < 4)) | |
| 182 return false; | |
| 183 info_header_.bi_size = ReadUint32(0); | |
| 184 // Don't increment decoded_offset here, it just makes the code in | |
| 185 // ProcessInfoHeader() more confusing. | |
| 186 | |
| 187 // Don't allow the header to overflow (which would be harmless here, but | |
| 188 // problematic or at least confusing in other places), or to overrun the | |
| 189 // image data. | |
| 190 const size_t header_end = header_offset_ + info_header_.bi_size; | |
| 191 if ((header_end < header_offset_) || | |
| 192 (img_data_offset_ && (img_data_offset_ < header_end))) | |
| 193 return parent_->SetFailed(); | |
| 194 | |
| 195 // See if this is a header size we understand: | |
| 196 // OS/2 1.x: 12 | |
| 197 if (info_header_.bi_size == 12) | |
| 198 is_os21x_ = true; | |
| 199 // Windows V3: 40 | |
| 200 else if ((info_header_.bi_size == 40) || IsWindowsV4Plus()) | |
| 201 ; | |
| 202 // OS/2 2.x: any multiple of 4 between 16 and 64, inclusive, or 42 or 46 | |
| 203 else if ((info_header_.bi_size >= 16) && (info_header_.bi_size <= 64) && | |
| 204 (!(info_header_.bi_size & 3) || (info_header_.bi_size == 42) || | |
| 205 (info_header_.bi_size == 46))) | |
| 206 is_os22x_ = true; | |
| 207 else | |
| 208 return parent_->SetFailed(); | |
| 209 | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 bool BMPImageReader::ProcessInfoHeader() { | |
| 214 // Read info header. | |
| 215 DCHECK_EQ(decoded_offset_, header_offset_); | |
| 216 if ((decoded_offset_ > data_->size()) || | |
| 217 ((data_->size() - decoded_offset_) < info_header_.bi_size) || | |
| 218 !ReadInfoHeader()) | |
| 219 return false; | |
| 220 decoded_offset_ += info_header_.bi_size; | |
| 221 | |
| 222 // Sanity-check header values. | |
| 223 if (!IsInfoHeaderValid()) | |
| 224 return parent_->SetFailed(); | |
| 225 | |
| 226 // Set our size. | |
| 227 if (!parent_->SetSize(info_header_.bi_width, info_header_.bi_height)) | |
| 228 return false; | |
| 229 | |
| 230 // For paletted images, bitmaps can set biClrUsed to 0 to mean "all | |
| 231 // colors", so set it to the maximum number of colors for this bit depth. | |
| 232 // Also do this for bitmaps that put too large a value here. | |
| 233 if (info_header_.bi_bit_count < 16) { | |
| 234 const uint32_t max_colors = static_cast<uint32_t>(1) | |
| 235 << info_header_.bi_bit_count; | |
| 236 if (!info_header_.bi_clr_used || (info_header_.bi_clr_used > max_colors)) | |
| 237 info_header_.bi_clr_used = max_colors; | |
| 238 } | |
| 239 | |
| 240 // For any bitmaps that set their BitCount to the wrong value, reset the | |
| 241 // counts now that we've calculated the number of necessary colors, since | |
| 242 // other code relies on this value being correct. | |
| 243 if (info_header_.bi_compression == RLE8) | |
| 244 info_header_.bi_bit_count = 8; | |
| 245 else if (info_header_.bi_compression == RLE4) | |
| 246 info_header_.bi_bit_count = 4; | |
| 247 | |
| 248 // Tell caller what still needs to be processed. | |
| 249 if (info_header_.bi_bit_count >= 16) | |
| 250 need_to_process_bitmasks_ = true; | |
| 251 else if (info_header_.bi_bit_count) | |
| 252 need_to_process_color_table_ = true; | |
| 253 | |
| 254 return true; | |
| 255 } | |
| 256 | |
| 257 bool BMPImageReader::ReadInfoHeader() { | |
| 258 // Pre-initialize some fields that not all headers set. | |
| 259 info_header_.bi_compression = RGB; | |
| 260 info_header_.bi_clr_used = 0; | |
| 261 | |
| 262 if (is_os21x_) { | |
| 263 info_header_.bi_width = ReadUint16(4); | |
| 264 info_header_.bi_height = ReadUint16(6); | |
| 265 DCHECK(!is_in_ico_); // ICO is a Windows format, not OS/2! | |
| 266 info_header_.bi_bit_count = ReadUint16(10); | |
| 267 return true; | |
| 268 } | |
| 269 | |
| 270 info_header_.bi_width = ReadUint32(4); | |
| 271 info_header_.bi_height = ReadUint32(8); | |
| 272 if (is_in_ico_) | |
| 273 info_header_.bi_height /= 2; | |
| 274 info_header_.bi_bit_count = ReadUint16(14); | |
| 275 | |
| 276 // Read compression type, if present. | |
| 277 if (info_header_.bi_size >= 20) { | |
| 278 uint32_t bi_compression = ReadUint32(16); | |
| 279 | |
| 280 // Detect OS/2 2.x-specific compression types. | |
| 281 if ((bi_compression == 3) && (info_header_.bi_bit_count == 1)) { | |
| 282 info_header_.bi_compression = HUFFMAN1D; | |
| 283 is_os22x_ = true; | |
| 284 } else if ((bi_compression == 4) && (info_header_.bi_bit_count == 24)) { | |
| 285 info_header_.bi_compression = RLE24; | |
| 286 is_os22x_ = true; | |
| 287 } else if (bi_compression > 5) | |
| 288 return parent_->SetFailed(); // Some type we don't understand. | |
| 289 else | |
| 290 info_header_.bi_compression = | |
| 291 static_cast<CompressionType>(bi_compression); | |
| 292 } | |
| 293 | |
| 294 // Read colors used, if present. | |
| 295 if (info_header_.bi_size >= 36) | |
| 296 info_header_.bi_clr_used = ReadUint32(32); | |
| 297 | |
| 298 // Windows V4+ can safely read the four bitmasks from 40-56 bytes in, so do | |
| 299 // that here. If the bit depth is less than 16, these values will be ignored | |
| 300 // by the image data decoders. If the bit depth is at least 16 but the | |
| 301 // compression format isn't BITFIELDS, the RGB bitmasks will be ignored and | |
| 302 // overwritten in processBitmasks(). (The alpha bitmask will never be | |
| 303 // overwritten: images that actually want alpha have to specify a valid | |
| 304 // alpha mask. See comments in ProcessBitmasks().) | |
| 305 // | |
| 306 // For non-Windows V4+, bit_masks_[] et. al will be initialized later | |
| 307 // during ProcessBitmasks(). | |
| 308 if (IsWindowsV4Plus()) { | |
| 309 bit_masks_[0] = ReadUint32(40); | |
| 310 bit_masks_[1] = ReadUint32(44); | |
| 311 bit_masks_[2] = ReadUint32(48); | |
| 312 bit_masks_[3] = ReadUint32(52); | |
| 313 } | |
| 314 | |
| 315 // Detect top-down BMPs. | |
| 316 if (info_header_.bi_height < 0) { | |
| 317 // We can't negate INT32_MIN below to get a positive int32_t. | |
| 318 // IsInfoHeaderValid() will reject heights of 1 << 16 or larger anyway, | |
| 319 // so just reject this bitmap now. | |
| 320 if (info_header_.bi_height == INT32_MIN) | |
| 321 return parent_->SetFailed(); | |
| 322 is_top_down_ = true; | |
| 323 info_header_.bi_height = -info_header_.bi_height; | |
| 324 } | |
| 325 | |
| 326 return true; | |
| 327 } | |
| 328 | |
| 329 bool BMPImageReader::IsInfoHeaderValid() const { | |
| 330 // Non-positive widths/heights are invalid. (We've already flipped the | |
| 331 // sign of the height for top-down bitmaps.) | |
| 332 if ((info_header_.bi_width <= 0) || !info_header_.bi_height) | |
| 333 return false; | |
| 334 | |
| 335 // Only Windows V3+ has top-down bitmaps. | |
| 336 if (is_top_down_ && (is_os21x_ || is_os22x_)) | |
| 337 return false; | |
| 338 | |
| 339 // Only bit depths of 1, 4, 8, or 24 are universally supported. | |
| 340 if ((info_header_.bi_bit_count != 1) && (info_header_.bi_bit_count != 4) && | |
| 341 (info_header_.bi_bit_count != 8) && (info_header_.bi_bit_count != 24)) { | |
| 342 // Windows V3+ additionally supports bit depths of 0 (for embedded | |
| 343 // JPEG/PNG images), 16, and 32. | |
| 344 if (is_os21x_ || is_os22x_ || | |
| 345 (info_header_.bi_bit_count && (info_header_.bi_bit_count != 16) && | |
| 346 (info_header_.bi_bit_count != 32))) | |
| 347 return false; | |
| 348 } | |
| 349 | |
| 350 // Each compression type is only valid with certain bit depths (except RGB, | |
| 351 // which can be used with any bit depth). Also, some formats do not support | |
| 352 // some compression types. | |
| 353 switch (info_header_.bi_compression) { | |
| 354 case RGB: | |
| 355 if (!info_header_.bi_bit_count) | |
| 356 return false; | |
| 357 break; | |
| 358 | |
| 359 case RLE8: | |
| 360 // Supposedly there are undocumented formats like "BitCount = 1, | |
| 361 // Compression = RLE4" (which means "4 bit, but with a 2-color table"), | |
| 362 // so also allow the paletted RLE compression types to have too low a | |
| 363 // bit count; we'll correct this later. | |
| 364 if (!info_header_.bi_bit_count || (info_header_.bi_bit_count > 8)) | |
| 365 return false; | |
| 366 break; | |
| 367 | |
| 368 case RLE4: | |
| 369 // See comments in RLE8. | |
| 370 if (!info_header_.bi_bit_count || (info_header_.bi_bit_count > 4)) | |
| 371 return false; | |
| 372 break; | |
| 373 | |
| 374 case BITFIELDS: | |
| 375 // Only valid for Windows V3+. | |
| 376 if (is_os21x_ || is_os22x_ || | |
| 377 ((info_header_.bi_bit_count != 16) && | |
| 378 (info_header_.bi_bit_count != 32))) | |
| 379 return false; | |
| 380 break; | |
| 381 | |
| 382 case JPEG: | |
| 383 case PNG: | |
| 384 // Only valid for Windows V3+. | |
| 385 if (is_os21x_ || is_os22x_ || info_header_.bi_bit_count) | |
| 386 return false; | |
| 387 break; | |
| 388 | |
| 389 case HUFFMAN1D: | |
| 390 // Only valid for OS/2 2.x. | |
| 391 if (!is_os22x_ || (info_header_.bi_bit_count != 1)) | |
| 392 return false; | |
| 393 break; | |
| 394 | |
| 395 case RLE24: | |
| 396 // Only valid for OS/2 2.x. | |
| 397 if (!is_os22x_ || (info_header_.bi_bit_count != 24)) | |
| 398 return false; | |
| 399 break; | |
| 400 | |
| 401 default: | |
| 402 // Some type we don't understand. This should have been caught in | |
| 403 // ReadInfoHeader(). | |
| 404 NOTREACHED(); | |
| 405 return false; | |
| 406 } | |
| 407 | |
| 408 // Top-down bitmaps cannot be compressed; they must be RGB or BITFIELDS. | |
| 409 if (is_top_down_ && (info_header_.bi_compression != RGB) && | |
| 410 (info_header_.bi_compression != BITFIELDS)) | |
| 411 return false; | |
| 412 | |
| 413 // Reject the following valid bitmap types that we don't currently bother | |
| 414 // decoding. Few other people decode these either, they're unlikely to be | |
| 415 // in much use. | |
| 416 // TODO(pkasting): Consider supporting these someday. | |
| 417 // * Bitmaps larger than 2^16 pixels in either dimension (Windows | |
| 418 // probably doesn't draw these well anyway, and the decoded data would | |
| 419 // take a lot of memory). | |
| 420 if ((info_header_.bi_width >= (1 << 16)) || | |
| 421 (info_header_.bi_height >= (1 << 16))) | |
| 422 return false; | |
| 423 // * Windows V3+ JPEG-in-BMP and PNG-in-BMP bitmaps (supposedly not found | |
| 424 // in the wild, only used to send data to printers?). | |
| 425 if ((info_header_.bi_compression == JPEG) || | |
| 426 (info_header_.bi_compression == PNG)) | |
| 427 return false; | |
| 428 // * OS/2 2.x Huffman-encoded monochrome bitmaps (see | |
| 429 // http://www.fileformat.info/mirror/egff/ch09_05.htm , re: "G31D" | |
| 430 // algorithm). | |
| 431 if (info_header_.bi_compression == HUFFMAN1D) | |
| 432 return false; | |
| 433 | |
| 434 return true; | |
| 435 } | |
| 436 | |
| 437 bool BMPImageReader::ProcessBitmasks() { | |
| 438 // Create bit_masks_[] values for R/G/B. | |
| 439 if (info_header_.bi_compression != BITFIELDS) { | |
| 440 // The format doesn't actually use bitmasks. To simplify the decode | |
| 441 // logic later, create bitmasks for the RGB data. For Windows V4+, | |
| 442 // this overwrites the masks we read from the header, which are | |
| 443 // supposed to be ignored in non-BITFIELDS cases. | |
| 444 // 16 bits: MSB <- xRRRRRGG GGGBBBBB -> LSB | |
| 445 // 24/32 bits: MSB <- [AAAAAAAA] RRRRRRRR GGGGGGGG BBBBBBBB -> LSB | |
| 446 const int num_bits = (info_header_.bi_bit_count == 16) ? 5 : 8; | |
| 447 for (int i = 0; i <= 2; ++i) | |
| 448 bit_masks_[i] = ((static_cast<uint32_t>(1) << (num_bits * (3 - i))) - 1) ^ | |
| 449 ((static_cast<uint32_t>(1) << (num_bits * (2 - i))) - 1); | |
| 450 } else if (!IsWindowsV4Plus()) { | |
| 451 // For Windows V4+ BITFIELDS mode bitmaps, this was already done when | |
| 452 // we read the info header. | |
| 453 | |
| 454 // Fail if we don't have enough file space for the bitmasks. | |
| 455 const size_t header_end = header_offset_ + info_header_.bi_size; | |
| 456 const size_t kBitmasksSize = 12; | |
| 457 const size_t bitmasks_end = header_end + kBitmasksSize; | |
| 458 if ((bitmasks_end < header_end) || | |
| 459 (img_data_offset_ && (img_data_offset_ < bitmasks_end))) | |
| 460 return parent_->SetFailed(); | |
| 461 | |
| 462 // Read bitmasks. | |
| 463 if ((data_->size() - decoded_offset_) < kBitmasksSize) | |
| 464 return false; | |
| 465 bit_masks_[0] = ReadUint32(0); | |
| 466 bit_masks_[1] = ReadUint32(4); | |
| 467 bit_masks_[2] = ReadUint32(8); | |
| 468 | |
| 469 decoded_offset_ += kBitmasksSize; | |
| 470 } | |
| 471 | |
| 472 // Alpha is a poorly-documented and inconsistently-used feature. | |
| 473 // | |
| 474 // Windows V4+ has an alpha bitmask in the info header. Unlike the R/G/B | |
| 475 // bitmasks, the MSDN docs don't indicate that it is only valid for the | |
| 476 // BITFIELDS compression format, so we respect it at all times. | |
| 477 // | |
| 478 // To complicate things, Windows V3 BMPs, which lack this mask, can specify | |
| 479 // 32bpp format, which to any sane reader would imply an 8-bit alpha | |
| 480 // channel -- and for BMPs-in-ICOs, that's precisely what's intended to | |
| 481 // happen. There also exist standalone BMPs in this format which clearly | |
| 482 // expect the alpha channel to be respected. However, there are many other | |
| 483 // BMPs which, for example, fill this channel with all 0s, yet clearly | |
| 484 // expect to not be displayed as a fully-transparent rectangle. | |
| 485 // | |
| 486 // If these were the only two types of Windows V3, 32bpp BMPs in the wild, | |
| 487 // we could distinguish between them by scanning the alpha channel in the | |
| 488 // image, looking for nonzero values, and only enabling alpha if we found | |
| 489 // some. (It turns out we have to do this anyway, because, crazily, there | |
| 490 // are also Windows V4+ BMPs with an explicit, non-zero alpha mask, which | |
| 491 // then zero-fill their alpha channels! See comments in | |
| 492 // processNonRLEData().) | |
| 493 // | |
| 494 // Unfortunately there are also V3 BMPs -- indeed, probably more than the | |
| 495 // number of 32bpp, V3 BMPs which intentionally use alpha -- which specify | |
| 496 // 32bpp format, use nonzero (and non-255) alpha values, and yet expect to | |
| 497 // be rendered fully-opaque. And other browsers do so. | |
| 498 // | |
| 499 // So it's impossible to display every BMP in the way its creators intended, | |
| 500 // and we have to choose what to break. Given the paragraph above, we match | |
| 501 // other browsers and ignore alpha in Windows V3 BMPs except inside ICO | |
| 502 // files. | |
| 503 if (!IsWindowsV4Plus()) | |
| 504 bit_masks_[3] = (is_in_ico_ && (info_header_.bi_compression != BITFIELDS) && | |
| 505 (info_header_.bi_bit_count == 32)) | |
| 506 ? static_cast<uint32_t>(0xff000000) | |
| 507 : 0; | |
| 508 | |
| 509 // We've now decoded all the non-image data we care about. Skip anything | |
| 510 // else before the actual raster data. | |
| 511 if (img_data_offset_) | |
| 512 decoded_offset_ = img_data_offset_; | |
| 513 need_to_process_bitmasks_ = false; | |
| 514 | |
| 515 // Check masks and set shift and LUT address values. | |
| 516 for (int i = 0; i < 4; ++i) { | |
| 517 // Trim the mask to the allowed bit depth. Some Windows V4+ BMPs | |
| 518 // specify a bogus alpha channel in bits that don't exist in the pixel | |
| 519 // data (for example, bits 25-31 in a 24-bit RGB format). | |
| 520 if (info_header_.bi_bit_count < 32) | |
| 521 bit_masks_[i] &= | |
| 522 ((static_cast<uint32_t>(1) << info_header_.bi_bit_count) - 1); | |
| 523 | |
| 524 // For empty masks (common on the alpha channel, especially after the | |
| 525 // trimming above), quickly clear the shift and LUT address and | |
| 526 // continue, to avoid an infinite loop in the counting code below. | |
| 527 uint32_t temp_mask = bit_masks_[i]; | |
| 528 if (!temp_mask) { | |
| 529 bit_shifts_right_[i] = 0; | |
| 530 lookup_table_addresses_[i] = 0; | |
| 531 continue; | |
| 532 } | |
| 533 | |
| 534 // Make sure bitmask does not overlap any other bitmasks. | |
| 535 for (int j = 0; j < i; ++j) { | |
| 536 if (temp_mask & bit_masks_[j]) | |
| 537 return parent_->SetFailed(); | |
| 538 } | |
| 539 | |
| 540 // Count offset into pixel data. | |
| 541 for (bit_shifts_right_[i] = 0; !(temp_mask & 1); temp_mask >>= 1) | |
| 542 ++bit_shifts_right_[i]; | |
| 543 | |
| 544 // Count size of mask. | |
| 545 size_t num_bits = 0; | |
| 546 for (; temp_mask & 1; temp_mask >>= 1) | |
| 547 ++num_bits; | |
| 548 | |
| 549 // Make sure bitmask is contiguous. | |
| 550 if (temp_mask) | |
| 551 return parent_->SetFailed(); | |
| 552 | |
| 553 // Since RGBABuffer tops out at 8 bits per channel, adjust the shift | |
| 554 // amounts to use the most significant 8 bits of the channel. | |
| 555 if (num_bits >= 8) { | |
| 556 bit_shifts_right_[i] += (num_bits - 8); | |
| 557 num_bits = 0; | |
| 558 } | |
| 559 | |
| 560 // Calculate LUT address. | |
| 561 lookup_table_addresses_[i] = | |
| 562 num_bits ? (nBitTo8BitlookupTable + (1 << num_bits) - 2) : 0; | |
| 563 } | |
| 564 | |
| 565 return true; | |
| 566 } | |
| 567 | |
| 568 bool BMPImageReader::ProcessColorTable() { | |
| 569 // Fail if we don't have enough file space for the color table. | |
| 570 const size_t header_end = header_offset_ + info_header_.bi_size; | |
| 571 const size_t table_size_in_bytes = | |
| 572 info_header_.bi_clr_used * (is_os21x_ ? 3 : 4); | |
| 573 const size_t table_end = header_end + table_size_in_bytes; | |
| 574 if ((table_end < header_end) || | |
| 575 (img_data_offset_ && (img_data_offset_ < table_end))) | |
| 576 return parent_->SetFailed(); | |
| 577 | |
| 578 // Read color table. | |
| 579 if ((decoded_offset_ > data_->size()) || | |
| 580 ((data_->size() - decoded_offset_) < table_size_in_bytes)) | |
| 581 return false; | |
| 582 color_table_.resize(info_header_.bi_clr_used); | |
| 583 | |
| 584 // On non-OS/2 1.x, an extra padding byte is present, which we need to skip. | |
| 585 const size_t bytes_per_color = is_os21x_ ? 3 : 4; | |
| 586 for (size_t i = 0; i < info_header_.bi_clr_used; ++i) { | |
| 587 color_table_[i].rgb_blue = ReadUint8(0); | |
| 588 color_table_[i].rgb_green = ReadUint8(1); | |
| 589 color_table_[i].rgb_red = ReadUint8(2); | |
| 590 decoded_offset_ += bytes_per_color; | |
| 591 } | |
| 592 | |
| 593 // We've now decoded all the non-image data we care about. Skip anything | |
| 594 // else before the actual raster data. | |
| 595 if (img_data_offset_) | |
| 596 decoded_offset_ = img_data_offset_; | |
| 597 need_to_process_color_table_ = false; | |
| 598 | |
| 599 return true; | |
| 600 } | |
| 601 | |
| 602 BMPImageReader::ProcessingResult BMPImageReader::ProcessRLEData() { | |
| 603 if (decoded_offset_ > data_->size()) | |
| 604 return kInsufficientData; | |
| 605 | |
| 606 // RLE decoding is poorly specified. Two main problems: | |
| 607 // (1) Are EOL markers necessary? What happens when we have too many | |
| 608 // pixels for one row? | |
| 609 // http://www.fileformat.info/format/bmp/egff.htm says extra pixels | |
| 610 // should wrap to the next line. Real BMPs I've encountered seem to | |
| 611 // instead expect extra pixels to be ignored until the EOL marker is | |
| 612 // seen, although this has only happened in a few cases and I suspect | |
| 613 // those BMPs may be invalid. So we only change lines on EOL (or Delta | |
| 614 // with dy > 0), and fail in most cases when pixels extend past the end | |
| 615 // of the line. | |
| 616 // (2) When Delta, EOL, or EOF are seen, what happens to the "skipped" | |
| 617 // pixels? | |
| 618 // http://www.daubnet.com/formats/BMP.html says these should be filled | |
| 619 // with color 0. However, the "do nothing" and "don't care" comments | |
| 620 // of other references suggest leaving these alone, i.e. letting them | |
| 621 // be transparent to the background behind the image. This seems to | |
| 622 // match how MSPAINT treats BMPs, so we do that. Note that when we | |
| 623 // actually skip pixels for a case like this, we need to note on the | |
| 624 // framebuffer that we have alpha. | |
| 625 | |
| 626 // Impossible to decode row-at-a-time, so just do things as a stream of | |
| 627 // bytes. | |
| 628 while (true) { | |
| 629 // Every entry takes at least two bytes; bail if there isn't enough | |
| 630 // data. | |
| 631 if ((data_->size() - decoded_offset_) < 2) | |
| 632 return kInsufficientData; | |
| 633 | |
| 634 // For every entry except EOF, we'd better not have reached the end of | |
| 635 // the image. | |
| 636 const uint8_t count = ReadUint8(0); | |
| 637 const uint8_t code = ReadUint8(1); | |
| 638 if ((count || (code != 1)) && PastEndOfImage(0)) | |
| 639 return kFailure; | |
| 640 | |
| 641 // Decode. | |
| 642 if (!count) { | |
| 643 switch (code) { | |
| 644 case 0: // Magic token: EOL | |
| 645 // Skip any remaining pixels in this row. | |
| 646 if (coord_.X() < parent_->Size().Width()) | |
| 647 buffer_->SetHasAlpha(true); | |
| 648 MoveBufferToNextRow(); | |
| 649 | |
| 650 decoded_offset_ += 2; | |
| 651 break; | |
| 652 | |
| 653 case 1: // Magic token: EOF | |
| 654 // Skip any remaining pixels in the image. | |
| 655 if ((coord_.X() < parent_->Size().Width()) || | |
| 656 (is_top_down_ ? (coord_.Y() < (parent_->Size().Height() - 1)) | |
| 657 : (coord_.Y() > 0))) | |
| 658 buffer_->SetHasAlpha(true); | |
| 659 // There's no need to move |coord_| here to trigger the caller | |
| 660 // to call SetPixelsChanged(). If the only thing that's changed | |
| 661 // is the alpha state, that will be properly written into the | |
| 662 // underlying SkBitmap when we mark the frame complete. | |
| 663 return kSuccess; | |
| 664 | |
| 665 case 2: { // Magic token: Delta | |
| 666 // The next two bytes specify dx and dy. Bail if there isn't | |
| 667 // enough data. | |
| 668 if ((data_->size() - decoded_offset_) < 4) | |
| 669 return kInsufficientData; | |
| 670 | |
| 671 // Fail if this takes us past the end of the desired row or | |
| 672 // past the end of the image. | |
| 673 const uint8_t dx = ReadUint8(2); | |
| 674 const uint8_t dy = ReadUint8(3); | |
| 675 if (dx || dy) | |
| 676 buffer_->SetHasAlpha(true); | |
| 677 if (((coord_.X() + dx) > parent_->Size().Width()) || | |
| 678 PastEndOfImage(dy)) | |
| 679 return kFailure; | |
| 680 | |
| 681 // Skip intervening pixels. | |
| 682 coord_.Move(dx, is_top_down_ ? dy : -dy); | |
| 683 | |
| 684 decoded_offset_ += 4; | |
| 685 break; | |
| 686 } | |
| 687 | |
| 688 default: { // Absolute mode | |
| 689 // |code| pixels specified as in BI_RGB, zero-padded at the end | |
| 690 // to a multiple of 16 bits. | |
| 691 // Because ProcessNonRLEData() expects decoded_offset_ to | |
| 692 // point to the beginning of the pixel data, bump it past | |
| 693 // the escape bytes and then reset if decoding failed. | |
| 694 decoded_offset_ += 2; | |
| 695 const ProcessingResult result = ProcessNonRLEData(true, code); | |
| 696 if (result != kSuccess) { | |
| 697 decoded_offset_ -= 2; | |
| 698 return result; | |
| 699 } | |
| 700 break; | |
| 701 } | |
| 702 } | |
| 703 } else { // Encoded mode | |
| 704 // The following color data is repeated for |count| total pixels. | |
| 705 // Strangely, some BMPs seem to specify excessively large counts | |
| 706 // here; ignore pixels past the end of the row. | |
| 707 const int end_x = std::min(coord_.X() + count, parent_->Size().Width()); | |
| 708 | |
| 709 if (info_header_.bi_compression == RLE24) { | |
| 710 // Bail if there isn't enough data. | |
| 711 if ((data_->size() - decoded_offset_) < 4) | |
| 712 return kInsufficientData; | |
| 713 | |
| 714 // One BGR triple that we copy |count| times. | |
| 715 FillRGBA(end_x, ReadUint8(3), ReadUint8(2), code, 0xff); | |
| 716 decoded_offset_ += 4; | |
| 717 } else { | |
| 718 // RLE8 has one color index that gets repeated; RLE4 has two | |
| 719 // color indexes in the upper and lower 4 bits of the byte, | |
| 720 // which are alternated. | |
| 721 size_t color_indexes[2] = {code, code}; | |
| 722 if (info_header_.bi_compression == RLE4) { | |
| 723 color_indexes[0] = (color_indexes[0] >> 4) & 0xf; | |
| 724 color_indexes[1] &= 0xf; | |
| 725 } | |
| 726 for (int which = 0; coord_.X() < end_x;) { | |
| 727 // Some images specify color values past the end of the | |
| 728 // color table; set these pixels to black. | |
| 729 if (color_indexes[which] < info_header_.bi_clr_used) | |
| 730 SetI(color_indexes[which]); | |
| 731 else | |
| 732 SetRGBA(0, 0, 0, 255); | |
| 733 which = !which; | |
| 734 } | |
| 735 | |
| 736 decoded_offset_ += 2; | |
| 737 } | |
| 738 } | |
| 739 } | |
| 740 } | |
| 741 | |
| 742 BMPImageReader::ProcessingResult BMPImageReader::ProcessNonRLEData( | |
| 743 bool in_rle, | |
| 744 int num_pixels) { | |
| 745 if (decoded_offset_ > data_->size()) | |
| 746 return kInsufficientData; | |
| 747 | |
| 748 if (!in_rle) | |
| 749 num_pixels = parent_->Size().Width(); | |
| 750 | |
| 751 // Fail if we're being asked to decode more pixels than remain in the row. | |
| 752 const int end_x = coord_.X() + num_pixels; | |
| 753 if (end_x > parent_->Size().Width()) | |
| 754 return kFailure; | |
| 755 | |
| 756 // Determine how many bytes of data the requested number of pixels | |
| 757 // requires. | |
| 758 const size_t pixels_per_byte = 8 / info_header_.bi_bit_count; | |
| 759 const size_t bytes_per_pixel = info_header_.bi_bit_count / 8; | |
| 760 const size_t unpadded_num_bytes = | |
| 761 (info_header_.bi_bit_count < 16) | |
| 762 ? ((num_pixels + pixels_per_byte - 1) / pixels_per_byte) | |
| 763 : (num_pixels * bytes_per_pixel); | |
| 764 // RLE runs are zero-padded at the end to a multiple of 16 bits. Non-RLE | |
| 765 // data is in rows and is zero-padded to a multiple of 32 bits. | |
| 766 const size_t align_bits = in_rle ? 1 : 3; | |
| 767 const size_t padded_num_bytes = | |
| 768 (unpadded_num_bytes + align_bits) & ~align_bits; | |
| 769 | |
| 770 // Decode as many rows as we can. (For RLE, where we only want to decode | |
| 771 // one row, we've already checked that this condition is true.) | |
| 772 while (!PastEndOfImage(0)) { | |
| 773 // Bail if we don't have enough data for the desired number of pixels. | |
| 774 if ((data_->size() - decoded_offset_) < padded_num_bytes) | |
| 775 return kInsufficientData; | |
| 776 | |
| 777 if (info_header_.bi_bit_count < 16) { | |
| 778 // Paletted data. Pixels are stored little-endian within bytes. | |
| 779 // Decode pixels one byte at a time, left to right (so, starting at | |
| 780 // the most significant bits in the byte). | |
| 781 const uint8_t mask = (1 << info_header_.bi_bit_count) - 1; | |
| 782 for (size_t byte = 0; byte < unpadded_num_bytes; ++byte) { | |
| 783 uint8_t pixel_data = ReadUint8(byte); | |
| 784 for (size_t pixel = 0; | |
| 785 (pixel < pixels_per_byte) && (coord_.X() < end_x); ++pixel) { | |
| 786 const size_t color_index = | |
| 787 (pixel_data >> (8 - info_header_.bi_bit_count)) & mask; | |
| 788 if (decoding_and_mask_) { | |
| 789 // There's no way to accurately represent an AND + XOR | |
| 790 // operation as an RGBA image, so where the AND values | |
| 791 // are 1, we simply set the framebuffer pixels to fully | |
| 792 // transparent, on the assumption that most ICOs on the | |
| 793 // web will not be doing a lot of inverting. | |
| 794 if (color_index) { | |
| 795 SetRGBA(0, 0, 0, 0); | |
| 796 buffer_->SetHasAlpha(true); | |
| 797 } else | |
| 798 coord_.Move(1, 0); | |
| 799 } else { | |
| 800 // See comments near the end of ProcessRLEData(). | |
| 801 if (color_index < info_header_.bi_clr_used) | |
| 802 SetI(color_index); | |
| 803 else | |
| 804 SetRGBA(0, 0, 0, 255); | |
| 805 } | |
| 806 pixel_data <<= info_header_.bi_bit_count; | |
| 807 } | |
| 808 } | |
| 809 } else { | |
| 810 // RGB data. Decode pixels one at a time, left to right. | |
| 811 while (coord_.X() < end_x) { | |
| 812 const uint32_t pixel = ReadCurrentPixel(bytes_per_pixel); | |
| 813 | |
| 814 // Some BMPs specify an alpha channel but don't actually use it | |
| 815 // (it contains all 0s). To avoid displaying these images as | |
| 816 // fully-transparent, decode as if images are fully opaque | |
| 817 // until we actually see a non-zero alpha value; at that point, | |
| 818 // reset any previously-decoded pixels to fully transparent and | |
| 819 // continue decoding based on the real alpha channel values. | |
| 820 // As an optimization, avoid calling SetHasAlpha(true) for | |
| 821 // images where all alpha values are 255; opaque images are | |
| 822 // faster to draw. | |
| 823 int alpha = GetAlpha(pixel); | |
| 824 if (!seen_non_zero_alpha_pixel_ && !alpha) { | |
| 825 seen_zero_alpha_pixel_ = true; | |
| 826 alpha = 255; | |
| 827 } else { | |
| 828 seen_non_zero_alpha_pixel_ = true; | |
| 829 if (seen_zero_alpha_pixel_) { | |
| 830 buffer_->ZeroFillPixelData(); | |
| 831 seen_zero_alpha_pixel_ = false; | |
| 832 } else if (alpha != 255) | |
| 833 buffer_->SetHasAlpha(true); | |
| 834 } | |
| 835 | |
| 836 SetRGBA(GetComponent(pixel, 0), GetComponent(pixel, 1), | |
| 837 GetComponent(pixel, 2), alpha); | |
| 838 } | |
| 839 } | |
| 840 | |
| 841 // Success, keep going. | |
| 842 decoded_offset_ += padded_num_bytes; | |
| 843 if (in_rle) | |
| 844 return kSuccess; | |
| 845 MoveBufferToNextRow(); | |
| 846 } | |
| 847 | |
| 848 // Finished decoding whole image. | |
| 849 return kSuccess; | |
| 850 } | |
| 851 | |
| 852 void BMPImageReader::MoveBufferToNextRow() { | |
| 853 coord_.Move(-coord_.X(), is_top_down_ ? 1 : -1); | |
| 854 } | |
| 855 | |
| 856 } // namespace blink | |
| OLD | NEW |