📄 bmpimagereader.cpp
字号:
m_isOS22x = true; } else if ((biCompression == 4) && (m_infoHeader.biBitCount == 24)) { m_infoHeader.biCompression = RLE24; m_isOS22x = true; } else if (biCompression > 5) { // Some type we don't understand. m_failed = true; return false; } else m_infoHeader.biCompression = static_cast<CompressionType>(biCompression); } // Read colors used, if present. if (m_infoHeader.biSize >= 36) m_infoHeader.biClrUsed = readUint32(data, 32); // Windows V4+ can safely read the four bitmasks from 40-56 bytes in, so do // that here. If the bit depth is less than 16, these values will be // ignored by the image data decoders. If the bit depth is at least 16 but // the compression format isn't BITFIELDS, these values will be ignored and // overwritten* in processBitmasks(). // NOTE: We allow alpha here. Microsoft doesn't really document this well, // but some BMPs appear to use it. // // For non-Windows V4+, m_bitMasks[] et. al will be initialized later // during processBitmasks(). // // *Except the alpha channel. Bizarrely, some RGB bitmaps expect decoders // to pay attention to the alpha mask here, so there's a special case in // processBitmasks() that doesn't always overwrite that value. if (isWindowsV4Plus()) { m_bitMasks[0] = readUint32(data, 40); m_bitMasks[1] = readUint32(data, 44); m_bitMasks[2] = readUint32(data, 48); m_bitMasks[3] = readUint32(data, 52); } // Detect top-down BMPs. if (m_infoHeader.biHeight < 0) { m_isTopDown = true; m_infoHeader.biHeight = -m_infoHeader.biHeight; } return true;}bool BMPImageReader::isInfoHeaderValid() const{ // Non-positive widths/heights are invalid. (We've already flipped the // sign of the height for top-down bitmaps.) if ((m_infoHeader.biWidth <= 0) || (m_infoHeader.biHeight == 0)) return false; // Only Windows V3+ has top-down bitmaps. if (m_isTopDown && (m_isOS21x || m_isOS22x)) return false; // Only bit depths of 1, 4, 8, or 24 are universally supported. if ((m_infoHeader.biBitCount != 1) && (m_infoHeader.biBitCount != 4) && (m_infoHeader.biBitCount != 8) && (m_infoHeader.biBitCount != 24)) { // Windows V3+ additionally supports bit depths of 0 (for embedded // JPEG/PNG images), 16, and 32. if (m_isOS21x || m_isOS22x) return false; if ((m_infoHeader.biBitCount != 0) && (m_infoHeader.biBitCount != 16) && (m_infoHeader.biBitCount != 32)) return false; } // Each compression type is only valid with certain bit depths (except RGB, // which can be used with any bit depth). Also, some formats do not // some compression types. switch (m_infoHeader.biCompression) { case RGB: if (m_infoHeader.biBitCount == 0) return false; break; case RLE8: // Supposedly there are undocumented formats like "BitCount = 1, // Compression = RLE4" (which means "4 bit, but with a 2-color table"), // so also allow the paletted RLE compression types to have too low a // bit count; we'll correct this later. if (m_infoHeader.biBitCount == 0 || m_infoHeader.biBitCount > 8) return false; break; case RLE4: // See comments in RLE8. if (m_infoHeader.biBitCount == 0 || m_infoHeader.biBitCount > 4) return false; break; case BITFIELDS: // Only valid for Windows V3+. if (m_isOS21x || m_isOS22x) return false; if ((m_infoHeader.biBitCount != 16) && (m_infoHeader.biBitCount != 32)) return false; break; case JPEG: case PNG: // Only valid for Windows V3+. if (m_isOS21x || m_isOS22x) return false; if (m_infoHeader.biBitCount != 0) return false; break; case HUFFMAN1D: // Only valid for OS/2 2.x. if (!m_isOS22x) return false; if (m_infoHeader.biBitCount != 1) return false; break; case RLE24: // Only valid for OS/2 2.x. if (!m_isOS22x) return false; if (m_infoHeader.biBitCount != 24) return false; break; default: // Some type we don't understand. This should have been caught in // readInfoHeader(). ASSERT_NOT_REACHED(); return false; } // Top-down bitmaps cannot be compressed; they must be RGB or BITFIELDS. if (m_isTopDown && (m_infoHeader.biCompression != RGB) && (m_infoHeader.biCompression != BITFIELDS)) return false; // Reject the following valid bitmap types that we don't currently bother // decoding. Few other people decode these either, they're unlikely to be // in much use. // TODO(pkasting): Consider supporting these someday. // * Bitmaps larger than 2^16 pixels in either dimension (Windows // probably doesn't draw these well anyway, and the decoded data would // take a lot of memory). if ((m_infoHeader.biWidth >= (1 << 16)) || (m_infoHeader.biHeight >= (1 << 16))) return false; // * Windows V3+ JPEG-in-BMP and PNG-in-BMP bitmaps (supposedly not found // in the wild, only used to send data to printers?). if ((m_infoHeader.biCompression == JPEG) || (m_infoHeader.biCompression == PNG)) return false; // * OS/2 2.x Huffman-encoded monochrome bitmaps (see // http://www.fileformat.info/mirror/egff/ch09_05.htm , re: "G31D" // algorithm). if (m_infoHeader.biCompression == HUFFMAN1D) return false; return true;}bool BMPImageReader::processBitmasks(SharedBuffer* data){ // Create m_bitMasks[] values. if (m_infoHeader.biCompression != BITFIELDS) { // The format doesn't actually use bitmasks. To simplify the decode // logic later, create bitmasks for the RGB data. For Windows V4+, // this overwrites the masks we read from the header, which are // supposed to be ignored in non-BITFIELDS cases. // 16 bits: MSB <- xRRRRRGG GGGBBBBB -> LSB // 24/32 bits: MSB <- [AAAAAAAA] RRRRRRRR GGGGGGGG BBBBBBBB -> LSB const int numBits = (m_infoHeader.biBitCount == 16) ? 5 : 8; for (int i = 0; i <= 2; ++i) { m_bitMasks[i] = ((static_cast<uint32_t>(1) << (numBits * (3 - i))) - 1) ^ ((static_cast<uint32_t>(1) << (numBits * (2 - i))) - 1); } // For Windows V4+ 32-bit RGB, don't overwrite the alpha mask from the // header (see note in readInfoHeader()). if (m_infoHeader.biBitCount < 32) m_bitMasks[3] = 0; else if (!isWindowsV4Plus()) m_bitMasks[3] = static_cast<uint32_t>(0xff000000); } else if (!isWindowsV4Plus()) { // For Windows V4+ BITFIELDS mode bitmaps, this was already done when // we read the info header. // Fail if we don't have enough file space for the bitmasks. static const int SIZEOF_BITMASKS = 12; if (((m_headerOffset + m_infoHeader.biSize + SIZEOF_BITMASKS) < (m_headerOffset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize + SIZEOF_BITMASKS)))) { m_failed = true; return false; } // Read bitmasks. if ((data->size() - m_decodedOffset) < SIZEOF_BITMASKS) return false; m_bitMasks[0] = readUint32(data, 0); m_bitMasks[1] = readUint32(data, 4); m_bitMasks[2] = readUint32(data, 8); // No alpha in anything other than Windows V4+. m_bitMasks[3] = 0; m_decodedOffset += SIZEOF_BITMASKS; } // We've now decoded all the non-image data we care about. Skip anything // else before the actual raster data. if (m_imgDataOffset) m_decodedOffset = m_imgDataOffset; m_needToProcessBitmasks = false; // Check masks and set shift values. for (int i = 0; i < 4; ++i) { // Trim the mask to the allowed bit depth. Some Windows V4+ BMPs // specify a bogus alpha channel in bits that don't exist in the pixel // data (for example, bits 25-31 in a 24-bit RGB format). if (m_infoHeader.biBitCount < 32) m_bitMasks[i] &= ((static_cast<uint32_t>(1) << m_infoHeader.biBitCount) - 1); // For empty masks (common on the alpha channel, especially after the // trimming above), quickly clear the shifts and continue, to avoid an // infinite loop in the counting code below. uint32_t tempMask = m_bitMasks[i]; if (!tempMask) { m_bitShiftsRight[i] = m_bitShiftsLeft[i] = 0; continue; } // Make sure bitmask does not overlap any other bitmasks. for (int j = 0; j < i; ++j) { if (tempMask & m_bitMasks[j]) { m_failed = true; return false; } } // Count offset into pixel data. for (m_bitShiftsRight[i] = 0; !(tempMask & 1); tempMask >>= 1) ++m_bitShiftsRight[i]; // Count size of mask. for (m_bitShiftsLeft[i] = 8; tempMask & 1; tempMask >>= 1) --m_bitShiftsLeft[i]; // Make sure bitmask is contiguous. if (tempMask) { m_failed = true; return false; } // Since RGBABuffer tops out at 8 bits per channel, adjust the shift // amounts to use the most significant 8 bits of the channel. if (m_bitShiftsLeft[i] < 0) { m_bitShiftsRight[i] -= m_bitShiftsLeft[i]; m_bitShiftsLeft[i] = 0; } } return true;}bool BMPImageReader::processColorTable(SharedBuffer* data){ m_tableSizeInBytes = m_infoHeader.biClrUsed * (m_isOS21x ? 3 : 4); // Fail if we don't have enough file space for the color table. if (((m_headerOffset + m_infoHeader.biSize + m_tableSizeInBytes) < (m_headerOffset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize + m_tableSizeInBytes)))) { m_failed = true; return false; } // Read color table.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -