GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


bitmap writing.. writes bmps ACDSee cannot read by dextre
void FileBmp::saveBmp(std::string filename, long *data, int width, int height)
{
    if (FILE *stream = fopen(filename.c_str(), "wb"))
    {
        //=========================================================
        long headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
        long pixelCnt = width * height;
        long dataSize = pixelCnt * sizeof(RGBTRIPLE);
        int bitCount = 24;
        long padding = calcPadding(width, bitCount);
        //=========================================================
        BITMAPFILEHEADER bmfHeader;
        memset(&bmfHeader, 0, sizeof(BITMAPFILEHEADER));
        bmfHeader.bfSize = headerSize + dataSize;
        bmfHeader.bfType = BF_TYPE;
        bmfHeader.bfOffBits = headerSize;
        fwrite(&bmfHeader, sizeof(BITMAPFILEHEADER), 1, stream);
        //=========================================================
        BITMAPINFOHEADER bmiHeader;
        memset(&bmiHeader, 0, sizeof(BITMAPINFOHEADER));
        bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmiHeader.biWidth = width;
        bmiHeader.biHeight = height;
        bmiHeader.biPlanes = 1;
        bmiHeader.biBitCount = bitCount;
        bmiHeader.biCompression = BI_RGB;
        bmiHeader.biSizeImage = dataSize;
        fwrite(&bmiHeader, sizeof(BITMAPINFOHEADER), 1, stream);
        //=========================================================
        RGBTRIPLE *pixel = new RGBTRIPLE[pixelCnt];
        for (int i = 0; i < pixelCnt; i++)
        {
            long c = data[i];
            pixel[i].rgbtRed = getR(c);
            pixel[i].rgbtGreen = getG(c);
            pixel[i].rgbtBlue = getB(c);
        }
        for (int j = 0; j < height; j++)
        {
            fwrite(&(pixel[j * width]), sizeof(RGBTRIPLE), width, stream);
            fseek(stream, padding, SEEK_CUR);
        }
        fclose(stream);
    }
}