Note that there are some explanatory texts on larger screens.

plurals
  1. POReading BMP in C++ strange behavior
    primarykey
    data
    text
    <p>Thank you in advance!</p> <p>I'm reading a BMP in C++ using self-defined BITMAPFILEHEADER and BITMAPINFOHEADER. Then I print it out using SetPixel(). But there're some problems with the printing order of pixels and I failed to fix it. To illustrate them, please see the images provided. I don't have enough reputations to upload images here so please check the url. Sorry for the inconvenience:(</p> <p><a href="http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html" rel="nofollow">http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html</a></p> <p>Above two are 256*256 lena.bmp. The first one is the original one, and the second one is the result of my program. See the left-most out-of-order bar? It should appear in the right-most as in the original image.</p> <p>I also tried some random bmp files. But with this one I got a strange color.</p> <p>(Still see the above link) Its size is 146*146. The third image is the original and fourth is the printed result. I used a screen capturing tool to snip it from the internet, so I don't know if the problem is in my program (but it didn't exist in lena.bmp!) or is in the bmp itself.</p> <p>My code is as below:</p> <pre><code>/*****BMP.h*****/ #include &lt;fstream&gt; #include &lt;Windows.h&gt; #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 &lt; bmih.biHeight; i++) for(j = 0; j &lt; 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 *)(&amp;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 *)(&amp;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 &lt; abs(bmih.biHeight); i++) { for(int j = 0; j &lt; abs(bmih.biWidth); j++) { in.read((char *)(&amp;R[i][j]), 1); //read in data pixel by pixel in.read((char *)(&amp;G[i][j]), 1); in.read((char *)(&amp;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 } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload