c - Scaling bmp files by memory size -
c - Scaling bmp files by memory size -
i have bmp file. objective scale downwards file 70% of total file size. have read bmp headers , pixel array src_buffer
. have function intakes pixel array , src_buffer
, width of bitmap(in pixels) src_width
, height of bitmap(in pixels) src_height
, reduction factor r
as input. functions writes output after scaling dest_buffer
, width of bitmap(in pixels)dest_width
, height of bitmap(in pixels)dest_height
. want write in bmp format. face difficulty. write back, find difficulty populate info members of struct
bitmapinfoheader
, bitmapfileheader
. other thing should take in account. have read padding between rows of pixel array. how can accomplish it. coding in c. dont want help coding here. want help exact algorithm write bmp file.
i figured out if have scale downwards bmp file. talk 24 bit bmp file. 24 bit bmp file consist of 2 headers followed image pixel array. first header in bmp file called bitmapfileheader
total size of 14 bytes. looks this.
typedef struct { uint16_t bftype; //2 bytes storing "bm". means bmp uint32_t bfsize; //4 bytes total file size of bmp. including both header , pixel array uint16_t bfreserved1; //always 0 uint16_t bfreserved2; //always 0 uint32_t bfoffbits; // stores 54. (40 +14)total size of headers. } __attribute__((__packed__)) bitmapfileheader;
followed struct bitmapinfoheader total size of 40 bytes. next declaration
typedef struct { uint32_t bisize; //40 bytes. size of header. int32_t biwidth; //width of bitmap image. how many pixel wide image. int32_t biheight; //height of bitmap image. uint16_t biplanes; // 1. uint16_t bibitcount; //24 24 bit bitmap. number of bit representing each pixel. uint32_t bicompression; uint32_t bisizeimage; //total size of bitmap pixel array including padding. int32_t bixpelspermeter; int32_t biypelspermeter; uint32_t biclrused; uint32_t biclrimportant; } __attribute__((__packed__)) bitmapinfoheader;
now, these 2 structures followed pixel array. single pixel represented 3 bytes. represented
typedef struct { uint8_t rgbtblue; uint8_t rgbtgreen; byte rgbtred; } __attribute__((__packed__)) rgbtriple;
we have array of rgbtriple set in linear fashion. precise number of pixel given next formula.
bitmapinfoheader.biwidth * bitmapinfoheader.biheight
the amount of memory required store info of image given :
bitmapinfoheader.biwidth * bitmapinfoheader.biheight * sizeof(rgbtriple)
the memory required single row hence :
bitmapinfoheader.biwidth * sizeof(rgbtriple)
here catch, if total memory of single row not multiple of 4 bytes, padding added row create multiple of 4. image 41x30 in size. number of bytes required store row 41*3 = 123bytes
. padding of 1 byte added row in bmp format. hence bitmapinfoheader.bisizeimage = 30 * 124 = 3720 bytes
while decoding bmp raw pixel array, 1 should maintain in mind remove these paddings. while writing bmp file(encoding), create sure add together these paddings. note that,
bitmapinfoheader.bisizeimage = (bitmapinfoheader.biwidth * sizeof(rgbtriple) +padding) * bitmapinfoheader.biheight
or can calculated formula:
bitmapinfoheader.bisizeimage = (((bitmapinfoheader.biwidth * bmpiheader.bibitcount) + 31) / 32) * 4 * bitmapinfoheader.biheight
please note bitmapinfoheader.bibitcount = sizeof(rgbtriple) * 8 = 24 bits
24 bit bitmap.
c bmp
Comments
Post a Comment