ありがとうございます!C++でBMPを読むと奇妙な動作
私は自分で定義したBITMAPFILEHEADERとBITMAPINFOHEADERを使用してC++でBMPを読んでいます。それからSetPixel()を使って印刷します。しかし、ピクセルの印刷順序にはいくつか問題があり、問題を解決できませんでした。それらを説明するために、提供された画像をご覧ください。私はここに画像をアップロードするのに十分な評判がないので、URLをチェックしてください。不便:(上記の二つの
http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html
申し訳ありませんが256 * 256 lena.bmpある。一つ目は、元の一つであり、2つ目は、私のプログラムの結果です。一番左のアウトを参照してください。バーオーダー--of?それは、元の画像のように一番右に表示されます。
私はまた、いくつかのランダムなBMPファイルを試してみました。しかし、このいずれかで、私は奇妙な色を得た。
(まだご覧ください上記のリンク)そのサイズは146 * 146です.3番目の画像は元の画像で、4番目は印刷結果です。私のプログラムに問題があるかどうかは分かりませんが(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
}
'Windows.h'ヘッダファイルは、すでに[' BITMAPFILEHEADER'](http://msdn.microsoft.com/en-us/library/dd183374.aspx)と['BITMAPINFOHEADER'](http:// msdn.microsoft.com/en-us/library/dd183376.aspx)。あなたがそれを含んでいるなら、なぜ構造を再定義していますか? –
BTW:あなたのレナイメージも間違っています。色は影響を受けないので、それほど印象的ではありません。それは他の画像と同じように*右にシフトされます。 –
@AndréCaron単に宿題の中で我々はそうする必要があるので... – goldfrapp04