读/写位图8 bitdepth文件

时间:2015-02-26 13:07:22

标签: bitmap

我有一个宽度为1360且高度为1024的BMP文件。它的bitdepth 8.我想读取BYTE数组中的BMP文件并使用BYTE数组进行进一步处理。为了缓冲区检查的目的,我正在读取BYTE缓冲区中的文件并再次保存回BMP文件。但是我没有获得与源BMP文件相同的目标BMP文件。

BMP文件读取代码如下。

void ReadBMPFileInBYTEBuffer(BYTE *pbSourceData, CString csFileName, int width, int height)
{   
    RGBQUAD plt[256];
    BITMAPINFOHEADER bih1;
    BITMAPFILEHEADER bfh1;
    bfh1.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256 + width*height;
    bfh1.bfReserved1 = 0;
    bfh1.bfReserved2 = 0;
    bfh1.bfType = ((WORD)('M' << 8) | 'B');
    bfh1.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
    bih1.biBitCount = 8;    
    bih1.biHeight = height;
    bih1.biWidth = width;
    bih1.biPlanes = 1;
    bih1.biSize = sizeof(bih1);
    bih1.biSizeImage = width * height;
    bih1.biClrImportant = 0;
    bih1.biClrUsed = 0;
    bih1.biCompression = 0;
    bih1.biXPelsPerMeter = 0;
    bih1.biYPelsPerMeter = 0;
    FILE *fp = NULL;

    fopen_s(&fp, csFileName, "rb");
    if (fp == NULL)
        return;
    fread(&bfh1, sizeof(BITMAPFILEHEADER), 1, fp);
    fread(&bih1, sizeof(BITMAPINFOHEADER), 1, fp);
    int ii;
    for (ii = 0; ii<256; ii++)
    {
        plt[ii].rgbBlue = ii;
        plt[ii].rgbGreen = ii;
        plt[ii].rgbRed = ii;
        plt[ii].rgbReserved = 0;
        fread(&plt[ii], sizeof(RGBQUAD), 1, fp);
    }

    fread(pbSourceData, 1, width * height, fp);
    fclose(fp);
}

BMP文件写作如下。

void SaveBYTEBufferINBmp(CString csFileName, BYTE *pBYTEBuffer, const int iWidth, const int iHeight)
{   
    HANDLE hf = CreateFile((CString)csFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);

    if (hf == INVALID_HANDLE_VALUE)
    {
        return;
    }

    // write out the file header
    //
    BITMAPFILEHEADER bfh;
    memset(&bfh, 0, sizeof(bfh));
    bfh.bfType = 'MB';
    bfh.bfSize = sizeof(bfh) + (iWidth*iHeight) + sizeof(BITMAPINFOHEADER);
    bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER)
        + sizeof(RGBQUAD) * 256;

    DWORD dwWritten = 0;
    WriteFile(hf, &bfh, sizeof(bfh), &dwWritten, NULL);

    // and the bitmap format
    //
    BITMAPINFOHEADER bih;
    memset(&bih, 0, sizeof(bih));
    bih.biSize = sizeof(bih);
    bih.biWidth = iWidth;
    bih.biHeight = iHeight;
    bih.biPlanes = 1;
    bih.biBitCount = 8;
    bih.biSizeImage = iWidth*iHeight;
    bih.biXPelsPerMeter = 1;
    bih.biYPelsPerMeter = 1;

    dwWritten = 0;
    WriteFile(hf, &bih, sizeof(bih), &dwWritten, NULL);

    //Adds color pallate
    RGBQUAD rgb[256];
    for (int i = 0; i < 256; i++)
    {
        rgb[i].rgbBlue = i;
        rgb[i].rgbGreen = i;
        rgb[i].rgbRed = i;
        rgb[i].rgbReserved = 0;
    }

    dwWritten = 0;
    WriteFile(hf, &rgb, sizeof(rgb), &dwWritten, NULL);

    //Adds data buffer
    dwWritten = 0;
    WriteFile(hf, pBYTEBuffer, (iWidth*iHeight), &dwWritten, NULL);

    CloseHandle(hf);
}

感谢。

0 个答案:

没有答案