My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to save the output as a bitmap image

Tuesday, September 1, 2009

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <alloc.h>

int SaveBMP16(char []);

typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;

struct BMP
{
// BitMap File Header
/* 1 2 */ byte bfType[2]; // must always be set to 'BM' to declare that this is a .bmp file.
/* 3 4 */ dword bfSize; // specifies the size of the file in bytes.
/* 7 2 */ word bfReserved1; // must always set to zero.
/* 9 2 */ word bfReserved2; // must always be set to zero.
/* 11 4 */ dword bfOffset; // specifies the offset from the beginning of the file to the bitmap data.
// BitMap Image Header
/* 15 4 */ dword biSize; // specifies the size of the BitMap Header structure, in bytes.
/* 19 4 */ dword biWidth; // specifies the width of image, in pixels.
/* 23 4 */ dword biHeight; // specifies the height of image, in pixels.
/* 27 2 */ word biPlanes; // specifies the number of planes of the target device, must be set to zero.
/* 29 2 */ word biBitCount; // specifies the number of bits per pixel.
/* 31 4 */ dword biCompression; //Specifies the type of compression, usually set to zero (no compression).
/* 35 4 */ dword biSizeImage; //specifies the size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
/* 39 4 */ dword biXPelsPerMeter; //specifies the the horizontal pixels per meter on the designated targer device, usually set to zero.
/* 43 4 */ dword biYPelsPerMeter; //specifies the the vertical pixels per meter on the designated targer device, usually set to zero
/* 47 4 */ dword biClrUsed; //specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
/* 51 4 */ dword biClrImportant; //specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important.
};
int SaveBMP16(char file[])
{
int i=0, j=0, r, g, b;

FILE *fp;
BMP *bmp;

bmp=(BMP *)malloc(54);

bmp->bfType[0]='B';
bmp->bfType[1]='M';
bmp->bfSize=153718;
bmp->bfReserved1=0;
bmp->bfReserved2=0;
bmp->bfOffset=118;
bmp->biSize=40;
bmp->biWidth=640;
bmp->biHeight=480;
bmp->biPlanes=1;
bmp->biBitCount=4;
bmp->biCompression=0;
bmp->biSizeImage=153600;
bmp->biXPelsPerMeter=0;
bmp->biYPelsPerMeter=0;
bmp->biClrUsed=0;
bmp->biClrImportant=0;

fp=fopen("c:\\testP.bmp", "wb");
if(fp == NULL)
{
printf("File can't be open");
getch();
return 1;
}
fwrite(bmp, 54, 1, fp);
fseek(fp, 54L, SEEK_SET);

fputc(0x0, fp);
fputc(0x0, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(127, fp);
fputc(0x0, fp);
fputc(0x0, fp);
fputc(0x0, fp);
fputc(0x0, fp);
fputc(127, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(127, fp);
fputc(127, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(0x0, fp);
fputc(0x0, fp);
fputc(127, fp);
fputc(0x0, fp);

fputc(127, fp);
fputc(0x0, fp);
fputc(127, fp);
fputc(0x0, fp);

fputc(0x0, fp);
fputc(192, fp);
fputc(192, fp);
fputc(0x0, fp);

fputc(192, fp);
fputc(192, fp);
fputc(192, fp);
fputc(0x0, fp);

fputc(128, fp);
fputc(128, fp);
fputc(128, fp);
fputc(0x0, fp);

fputc(255, fp);
fputc(0x0, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(0x0, fp);
fputc(255, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(255, fp);
fputc(255, fp);
fputc(0x0, fp);
fputc(0x0, fp);

fputc(0x0, fp);
fputc(0x0, fp);
fputc(255, fp);
fputc(0x0, fp);
fputc(255, fp);
fputc(0x0, fp);
fputc(255, fp);
fputc(0x0, fp);

fputc(0x0, fp);
fputc(255, fp);
fputc(255, fp);
fputc(0x0, fp);

fputc(255, fp);
fputc(255, fp);
fputc(255, fp);
fputc(0x0, fp);


i=0;
j=479;

fseek(fp, 118, SEEK_SET);

while(j>=0)
{
i=0;
while(i<640)
{
fputc((getpixel(i, j)<<4) | getpixel(i+1, j), fp);
i+=2;
}
j--;
}
free(bmp);
fclose(fp);
return 0;
}

0 comments: