c - BMP 16-bit Image converting into array -
i have bmp format image archived in following manner
(j = 0; j < 240; j++) { for(i=0;i<320;i++) { data_temp = lcd_readram(); image_buf[i*2+1] = (data_temp&0xff00) >> 8; image_buf[i*2+0] = data_temp & 0x00ff; } ret = f_write(&file, image_buf, 640, &bw);
where lcd_readram function reads pixel @ time lcd screen
i want know, how get pixel positions of image file. , how save values of each pixel in [320][240] matrix
appreciated, thanks.
a bmp file reader want. can bmp file reader , tweak purposes. example: this question , answer gives bmp file reader assumes 24-bit bmp format. format 16-bit, requires tweaking.
here attempt @ doing (didn't test, should take hard-coded details grain of salt).
int i; file* f = fopen(filename, "rb"); unsigned char info[54]; fread(info, sizeof(unsigned char), 54, f); // read 54-byte header int width = 320, height = 240; // might want extract info bmp header instead int size_in_file = 2 * width * height; unsigned char* data_from_file = new unsigned char[size_in_file]; fread(data_from_file, sizeof(unsigned char), size_in_file, f); // read rest fclose(f); unsigned char pixels[240 * 320][3]; for(i = 0; < width * height; ++i) { unsigned char temp0 = data_from_file[i * 2 + 0]; unsigned char temp1 = data_from_file[i * 2 + 1]; unsigned pixel_data = temp1 << 8 | temp0; // extract red, green , blue components 16 bits pixels[i][0] = pixel_data >> 11; pixels[i][1] = (pixel_data >> 5) & 0x3f; pixels[i][2] = pixel_data & 0x1f; }
note: assumes lcd_readram
function (presumably, reading stuff lcd memory) gives pixels in standard 5-6-5 format.
the name 5-6-5 signifies number of bits in each 16-bit number allocated each colour component (red, green, blue). there exist other allocations 5-5-5, have never seen them in practice.
Comments
Post a Comment