c++ bmp to 2d-array prints unrealistic values -



i'm having trouble small project i'm doing. have convert bmp file 2d array of color (i made own typedef struct each color). code works (it doesn't run errors) , values matching image others don't; 3 first pixels of image white while says first red, green , blue.

#include <string> #include <iostream> #include <fstream>  using namespace std;  const int w = 960, h = 720;  typedef struct colors {     int red;     int green;     int blue; } color;   color image[h][w];  void readbmp(char *filename) {     file *f = fopen(filename, "rb");     unsigned char info[54];     fread(info, sizeof(unsigned char), 54, f); // read 54-byte header      // extract image height , width header     int width = *(int *) &info[18];     int height = *(int *) &info[22];     /* w = width;     h = height;*/     int size = 3 * width * height;     unsigned char *data = new unsigned char[size]; // allocate 3 bytes per pixel     fread(data, sizeof(unsigned char), size, f); // read rest of data @ once     fclose(f);     int index = 0;     (int j = 0; j < h; j++) {         //cout << index << endl;         (int k = 0; k < w; k++) {             index = (j * w) * 3 + k * 3;             color c;             c.blue = (int) data[index];             c.green = (int) data[index + 1];             c.red = (int) data[index + 2];             //cout << "j/width: " << (j / width) << endl << "j%width: " << (j % width) << endl << endl;             image[j][k] = c;             cout << (image[j][k]).red << ' ' << (image[j][k]).green << ' '     << (image[j][k]).blue << endl;         }     } }  int main() {     //std::cout << "this test function" << endl;     readbmp((char *) "test.bmp"); } 

and output of first 50 lines is:

255 0 0 255 0 0 255 0 0 0 0 0 0 0 0 71 66 255 128 115 82 40 245 194 30 184 96 133 32 21 64 1 235 19 51 51 102 102 128 102 64 38 160 6 102 9 153 153 215 10 60 92 36 3 0 50 143 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 107 152 191 105 150 189 104 149 188 105 150 189 104 149 188 100 145 184 97 141 180 96 140 179 96 138 176 96 138 176 98 137 176 99 138 177 101 140 179 103 142 181 106 144 183 106 145 184 111 150 189 106 148 186 107 149 187 105 147 185 104 143 182 112 151 190 ... 

thanks in advance, jari

this not full bitmap parser able read 24 bit bitmaps. doesn't check or read bitfield bitmaps, or clut based bitmaps.

most importantly, it's what's happening. 32 bit bitmaps cause channels shift along next every time colour read. meaning of colours incorrect in way.

you must check bits per pixel in header - it's uint16_t @ 15th & 16th bytes in header.

but aware there quite lot of other issues parser.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -