__inline__ int write_gray256(char * filename, u8* pixel, int width, int height) { int i; bmp_t bmp; int lwidth = real_width(width); int sizeImage = lwidth * height * (int)sizeof(u8); bmp.file_header.type = 19778; bmp.file_header.reserved1 = bmp.file_header.reserved2 = 0; bmp.file_header.offsetbytes = 54 + 1024; bmp.file_header.fsize = 54 + 1024 + sizeImage; bmp.info_header.hsize = sizeof(bitmapInfoHeader_t); bmp.info_header.width = width; bmp.info_header.height = height; bmp.info_header.planes = 1; bmp.info_header.bitcount = 8; bmp.info_header.compression = 0; bmp.info_header.sizeimage = sizeImage; bmp.info_header.xpelspermeter = 0; bmp.info_header.ypelspermeter = 0; bmp.info_header.colorsused = 0; bmp.info_header.colorsimportant = 0; bmp.data= pixel; bmp.color_table= (RGBQUAD*)malloc(((size_t)1<< (size_t)bmp.info_header.bitcount)*sizeof(RGBQUAD)); for(i=0;i<(1<< bmp.info_header.bitcount);i++){ bmp.color_table[i].B= (u8)i; bmp.color_table[i].G= (u8)i; bmp.color_table[i].R= (u8)i; bmp.color_table[i].Reserved=(u8)i; } return write_gray256(filename, &bmp, pixel); }
__inline__ int write_bmp(char * filename, bmp_t* bmp, rgb_t * pixel) { if(!pixel) pixel = (rgb_t*) bmp->data; if(bmp->bitcount == 8 && bmp->color_table == NULL){ return write_gray256(filename, (u8*)pixel, bmp->width, bmp->height); } FILE * file = fopen(filename, "wb"); if (!file) { printf("File open error: %s\n", filename); return -1; } if(!pixel) return -1; fwrite((void *)&(bmp->file_header), sizeof(bitmapFileHeader_t), 1, file); fwrite((void *)&(bmp->info_header), sizeof(bitmapInfoHeader_t), 1, file); if(bmp->color_table !=NULL) fwrite((void *)bmp->color_table, sizeof(RGBQUAD)*256, 1, file); fwrite((void *)pixel, bmp->info_header.sizeimage, 1, file); fclose(file); return 0; }