I want to read bmp image and draw the pixel values in GUI window but it is not giving me the correct result,the picture it is showing is completely different then the original image I don't know where I am going wrong. any help?
int main() {
char filename[100];
printf("Enter the bitmap image name:");
scanf("%s",filename);
int i;
FILE* f = fopen(filename, "rb");
if(f == NULL)
throw "Argument Exception";
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int gdriver = DETECT, gmode;
initgraph (&gdriver, &gmode,"");
cout << " Name: " << filename << endl;
cout << " Width: " << width << endl;
cout << "Height: " << height << endl;
int row_padded = (width*3 + 3) & (~3);
unsigned char* data = new unsigned char[row_padded];
unsigned char tmp;
for(int i = 0; i < height; i++)
{
fread(data, sizeof(unsigned char), row_padded, f);
for(int j = 0; j < width; j += 3)
{
// Convert (B, G, R) to (R, G, B)
tmp = data[j];
data[j] = data[j+2];
data[j+2] = tmp;
int last=width*height;
int index=last;
cout << "R: "<< (int)data[j] << " G: " << (int)data[j+1]<< " B: " << (int)data[j+2]<< endl;
cout <<((data[j] & 0xff) << 16) + ((data[j+1] & 0xff) << 8) + (data[j+2] & 0xff);
cout<<"number of time" <<i;
unsigned long rgb = 0xFA09CA;
rgb =((data[j] & 0xff) << 16) + ((data[j+1] & 0xff) << 8) + (data[j+2] & 0xff);
putpixel(j,i,data[j]);
putpixel(j,i,data[j+1]);
putpixel(j,i,data[j+1]);
}
}
getch();
}