c - byte-shift when writing .bmp-file -
i'm not sure wheather pc trying fool me or if i'm tired find bug. hours , days i'm trying write bitmap file in pure c. don't have problems format or padding, content. here's mwe.
int main() { file *file; int w = 256; int h = 15; int pad = (4 - ((3 * w) % 4)) % 4; int filesize = 54 + (3 * w + pad) * h; // create file header unsigned char bmpfileheader[14] = { 'b','m', 0,0,0,0, 0,0, 0,0, 54,0,0,0 }; bmpfileheader[2] = (unsigned char)(filesize); bmpfileheader[3] = (unsigned char)(filesize >> 8); bmpfileheader[4] = (unsigned char)(filesize >> 16); bmpfileheader[5] = (unsigned char)(filesize >> 24); // create info header unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 }; bmpinfoheader[4] = (unsigned char)(w); bmpinfoheader[5] = (unsigned char)(w >> 8); bmpinfoheader[6] = (unsigned char)(w >> 16); bmpinfoheader[7] = (unsigned char)(w >> 24); bmpinfoheader[8] = (unsigned char)(h); bmpinfoheader[9] = (unsigned char)(h >> 8); bmpinfoheader[10] = (unsigned char)(h >> 16); bmpinfoheader[11] = (unsigned char)(h >> 24); // create content // allocate memory dynamically unsigned char *bmpcontent = (unsigned char *)calloc(filesize - 54, sizeof(unsigned char)); int index; // map data values onto blue-red scale (int j = 0; j < h; j++) { (int = 0; < w; i++) { index = 3 * + (3 * w + pad) * j; // blue *(bmpcontent + index) = 255-i; } } // write file file = fopen("test.bmp", "w"); fwrite(bmpfileheader, sizeof(bmpfileheader[0]), 14, file); fwrite(bmpinfoheader, sizeof(bmpinfoheader[0]), 40, file); fwrite(bmpcontent, sizeof(bmpcontent[0]), filesize - 54, file); fclose(file); // free memory free(bmpcontent); return 0; } as i'm filling 'blue-byte' of each pixel 255-i i'm expecting smooth fade dark blue black in each line. i'm getting this: example1
as can see there byte-shift in each line causes change of color in each new line plus pixel-shift after every third line. curious thing is, happens, when i'm trying write 13 file. using hex editor found out, second 13 written before next value finds way file. when i'm reducing width 240 (now content of bitmap varies between 14 , 255) i'm getting smooth picture without shifts: example2
you may pure coincidence fault appears while writing 13 , may so. tried write data file 13 appeared @ non-deterministic positions, , same effect occured.
i'm @ wits end, please me!!!
it's because value of 13 corresponds newline in windows. windows newlines \r\n, corresponds hex 0d0a. since you're trying write 0d, it's treated newline, it's getting written file 0d0a. if put \n in string, correctly written \r\n file. thus, fix this, should make sure you're writing file in binary mode, prevents "feature" writing byte file. see here more info: strange 0x0d being added binary file
to use binary mode, replace "w" "wb" in fopen, , work fine. i've verified now, , output correct:
on other platforms, problem won't happen in first place, fix work on, , needed for, windows platforms.

Comments
Post a Comment