void exitmessage(char *message)
{
printf("%s\n",message);exit(0);
}
void write_buf_to_BMP(BYTE *im_buffer, WORD X_bitmap, WORD Y_bitmap, char *BMPname)
{
SWORD x,y;
RGB *pixel;
BM_header BH;
FILE *fp_bitmap;
DWORD im_loc_bytes;
BYTE nr_fillingbytes, i;
BYTE zero_byte=0;
fp_bitmap=fopen(BMPname,"wb");
if (fp_bitmap==NULL) exitmessage("File cannot be created");
if (X_bitmap%4!=0) nr_fillingbytes=4-((X_bitmap*2L)%4);
else nr_fillingbytes=0;
BH.BMP_id = 'M'*256+'B'; fwrite(&BH.BMP_id,2,1,fp_bitmap);
BH.size=54+Y_bitmap*(X_bitmap*2+nr_fillingbytes);fwrite(&BH.size,4,1,fp_bitmap);
BH.zero_res = 0; fwrite(&BH.zero_res,4,1,fp_bitmap);
BH.offbits = 54; fwrite(&BH.offbits,4,1,fp_bitmap);
BH.biSize = 0x28; fwrite(&BH.biSize,4,1,fp_bitmap);
BH.Width = X_bitmap; fwrite(&BH.Width,4,1,fp_bitmap);
BH.Height = Y_bitmap; fwrite(&BH.Height,4,1,fp_bitmap);
BH.biPlanes = 1; fwrite(&BH.biPlanes,2,1,fp_bitmap);
BH.biBitCount = 16; fwrite(&BH.biBitCount,2,1,fp_bitmap);
BH.biCompression = 0; fwrite(&BH.biCompression,4,1,fp_bitmap);
BH.biSizeImage = 0; fwrite(&BH.biSizeImage,4,1,fp_bitmap);
BH.biXPelsPerMeter = 0xB40; fwrite(&BH.biXPelsPerMeter,4,1,fp_bitmap);
BH.biYPelsPerMeter = 0xB40; fwrite(&BH.biYPelsPerMeter,4,1,fp_bitmap);
BH.biClrUsed = 0; fwrite(&BH.biClrUsed,4,1,fp_bitmap);
BH.biClrImportant = 0; fwrite(&BH.biClrImportant,4,1,fp_bitmap);
printf("Writing bitmap ...\n");
im_loc_bytes=(DWORD)im_buffer+((DWORD)Y_bitmap-1)*X_bitmap*2;
for (y=0;y