簡體   English   中英

在C ++中讀取BMP異常行為

[英]Reading BMP in C++ strange behavior

先感謝您!

我正在使用自定義的BITMAPFILEHEADER和BITMAPINFOHEADER讀取C ++中的BMP。 然后我使用SetPixel()將其打印出來。 但是像素的打印順序存在一些問題,我無法解決它。 為了說明它們,請參閱提供的圖像。 我的信譽不足,無法在此處上傳圖片,因此請檢查網址。 抱歉給你帶來不便:(

http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html

上面兩個是256 * 256 lena.bmp。 第一個是原始的,第二個是我的程序的結果。 看到最左邊的亂序條? 它應該出現在原始圖像的最右邊。

我還嘗試了一些隨機的bmp文件。 但是有了這個,我得到了一種奇怪的顏色。

(仍請參見上面的鏈接)其尺寸為146 * 146。 第三張圖像是原始圖像,第四張是打印結果。 我使用了一個屏幕捕獲工具從互聯網上截取它,所以我不知道問題是否出在我的程序中(但lena.bmp中不存在此問題)或bmp本身。

我的代碼如下:

/*****BMP.h*****/
#include <fstream>
#include <Windows.h>
#define N 384
using namespace std;

typedef struct {
   WORD bfType;                 /* Magic identifier            */
   DWORD bfSize;                       /* File size in bytes          */
   WORD bfReserved1;
   WORD bfReserved2;
   DWORD bfOffBits;                     /* Offset to image data, bytes */
} HEADER;

typedef struct {
   DWORD biSize;               /* Header size in bytes      */
   LONG biWidth;                /* Width and height of image */
   LONG biHeight;
   WORD biPlanes;       /* Number of colour planes   */
   WORD biBitCount;         /* Bits per pixel            */
   DWORD biCompression;        /* Compression type          */
   DWORD biSizeImage;          /* Image size in bytes       */
   LONG biXPelsPerMeter;     /* Pixels per meter          */
   LONG biYPelsPerMeter;
   DWORD biClrUsed;           /* Number of colours         */
   DWORD biClrImportant;   /* Important colours         */
} INFOHEADER;



/*****main.cpp*****/
#include "BMP.h"

HEADER bmfh;        //bitmap file header
INFOHEADER bmih;    //bitmap info header
BYTE R[N][N], G[N][N], B[N][N];                     //RGB value of every pixel
COLORREF color[N][N];   //color
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();  //initialize the screen
void ReadBmp(char *BmpFileName);
void GetColor(int i, int j);

void main()
{
    HWND hwnd;  //handlers HWND and HDC
    HDC hdc;
    char BmpFileName[10];
    int i, j;

    hwnd = GetConsoleWindow();  //initialize the screen
    hdc = GetDC(hwnd);  
    scanf("%s", BmpFileName);
    ReadBmp(BmpFileName);   //read the BMP file
    for(i = 0; i < bmih.biHeight; i++)
        for(j = 0; j < bmih.biWidth; j++)
            SetPixel(hdc, i, j, color[bmih.biWidth - j][i]);    //draw the BMP image
    ReleaseDC(hwnd,hdc);
}

void ReadBmp(char *BmpFileName)     //read the BMP file
{
    int patch;  //number of 0s for complement in every row
    ifstream in(BmpFileName, ios_base::binary);     //open the BMP file
    in.read((char *)(&bmfh), sizeof(bmfh) - 2);     //read in BITMAPFILEHEADER. Here sizeof returns a value larger than struct size, so "-2"
    if(bmfh.bfType != 0x4d42)                       //if not BMP, exit
    {
        printf("File type is not BMP!\n");
        exit(1);
    }
    in.read((char *)(&bmih),sizeof(bmih));      //read in BITMAPINFOHEADER
    in.seekg(bmfh.bfOffBits, ios_base::beg);    //seek bitmap data
    patch = (4 - (bmih.biWidth * 3) % 4) % 4;   //calculate number of 0s for complement in every row
    for(int i = 0; i < abs(bmih.biHeight); i++) 
    {
        for(int j = 0; j < abs(bmih.biWidth); j++)
        {
            in.read((char *)(&R[i][j]), 1);     //read in data pixel by pixel
            in.read((char *)(&G[i][j]), 1);
            in.read((char *)(&B[i][j]), 1);
            GetColor(i, j);     //obtain the original and greyscaled color
        }
        in.seekg(patch, ios_base::cur); //skip 0s for complement in every row
    }
}

void GetColor(int i, int j)     //obtain the original and greyscaled color
{
    int iB, iG, iR;
    iB = (int)(B[i][j]);
    iG = (int)(G[i][j]);
    iR = (int)(R[i][j]);
    color[i][j] = RGB(iB, iG, iR);  //original color
}

問題與HEADER的結構打包有關(請參閱http://en.wikipedia.org/wiki/Data_structure_alignment的含義)。

- 2in.read((char *)(&bmfh), sizeof(bmfh) - 2); 嘗試解決此問題,但實際上不會。 bfType之后的所有字段(包括bfOffBits )將具有錯誤的偏移量,因此其值將不正確。

使用MSVC,您可以像這樣禁用HEADERINFOHEADER結構打包:

#pragma pack(push)
#pragma pack(1)

typedef struct {
...
} HEADER;

typedef struct {
...
} INFOHEADER;

#pragma pack(pop)

然后刪除- 2 ,就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM