/** Read image in @e filename into memory. * * @throw ImageException from open() * @throw ImageException from readFileHeader() * @throw ImageException from readInfoHeader() * @throw ImageException from readPixels() * * @see getFormat() * @see getPixels() * @see getSize() * @see getWidth() * @see getHeight() */ void BmpImageReader::read(const string &filename) { try { open(filename); readFileHeader(); readInfoHeader(); readPixels(); close(); } catch (BasicException &e) { close(); throw e; } }
bool BMPImageReader::processInfoHeader() { // Read info header. ASSERT(m_decodedOffset == m_headerOffset); if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < m_infoHeader.biSize) || !readInfoHeader()) return false; m_decodedOffset += m_infoHeader.biSize; DEFINE_THREAD_SAFE_STATIC_LOCAL( blink::CustomCountHistogram, dimensionsLocationHistogram, new blink::CustomCountHistogram( "Blink.DecodedImage.EffectiveDimensionsLocation.BMP", 0, 50000, 50)); dimensionsLocationHistogram.count(m_decodedOffset - 1); // Sanity-check header values. if (!isInfoHeaderValid()) return m_parent->setFailed(); // Set our size. if (!m_parent->setSize(m_infoHeader.biWidth, m_infoHeader.biHeight)) return false; // For paletted images, bitmaps can set biClrUsed to 0 to mean "all // colors", so set it to the maximum number of colors for this bit depth. // Also do this for bitmaps that put too large a value here. if (m_infoHeader.biBitCount < 16) { const uint32_t maxColors = static_cast<uint32_t>(1) << m_infoHeader.biBitCount; if (!m_infoHeader.biClrUsed || (m_infoHeader.biClrUsed > maxColors)) m_infoHeader.biClrUsed = maxColors; } // For any bitmaps that set their BitCount to the wrong value, reset the // counts now that we've calculated the number of necessary colors, since // other code relies on this value being correct. if (m_infoHeader.biCompression == RLE8) m_infoHeader.biBitCount = 8; else if (m_infoHeader.biCompression == RLE4) m_infoHeader.biBitCount = 4; // Tell caller what still needs to be processed. if (m_infoHeader.biBitCount >= 16) m_needToProcessBitmasks = true; else if (m_infoHeader.biBitCount) m_needToProcessColorTable = true; return true; }
/*------------------------------------------------------------------------------------------------------------ * ビットマップファイルを読み込む * * path: 読み込むビットマップファイル */ void Bitmap::readFile(const char *path) { FILE *ifp = fopen(path, "rb"); if (ifp == 0) { perror("fopen"); fprintf(stderr, "file: %s\n", path); throw MyError("bitmap file open failed", __FUNCTION__); } int error; error = readFileHeader(ifp); if (error > 0) { throw MyError("failed to read bitmap file header", __FUNCTION__); } error = readInfoHeader(ifp); if (error > 0) { throw MyError("failed to read bitmap info header", __FUNCTION__); } mDataSize = mFileHeader.bfSize - mInfoHeader.biSize - sizeof(mFileHeader); if (mDataSize != 0) { delete [] mData; } mData = new unsigned char[mDataSize]; error = readBitmapData(ifp); if (error > 0) { throw MyError("failed toread bitmap data", __FUNCTION__); } mWidth = mInfoHeader.biWidth; mHeight = mInfoHeader.biHeight; fclose(ifp); }