C/raster data to bmp file
Expert: Prince M. Premnath - 12/11/2006
QuestionSir, Thank for your reply. Another question, how can i use malloc function to allocate a memory space for a variable?
-------------------------------------------
The text above is a follow-up to ...
-----Question-----
Sir, my question is : By using C language, how to create a new image(.bmp) when i have all the raster data of a original image(.bmp)?
Thank you in advance!
-----Answer-----
Dear Wei!
Really i cant understand your question , but one thing i wish to tell you now , we can manipulate any type of files using C ,( say JPEG ,and so on ) but the only thing we needs to know is about its internal file structure ,ie how the file is organized , how the data's are packed and other informations regarding to file operations, ( The file structure is always available in starting few bytes in a file ) , Using that data's you can easily duplicate , create , or manipulate any kind of files!
Regards !
Prince M. Premnath.
AnswerDear Wei!
Here i got yet another chance to clear your doubt ! regardng BMP file processing !
I supposed to answer one questioner using a cmplete C Program that will display the BMP file on screen !
Here ill present you the program !
#include <alloc.h>
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
struct BMP
{
char Type[2]; //File type. Set to "BM".
unsigned long Size; //Size in BYTES of the file.
unsigned long Reserved; //Reserved. Set to zero.
unsigned long OffSet; //Offset to the data.
unsigned long headsize; //Size of rest of header. Set to 40.
unsigned long Width; //Width of bitmap in pixels.
unsigned long Height; // Height of bitmap in pixels.
unsigned int Planes; //Number of Planes. Set to 1.
unsigned int BitsPerPixel; //Number of Bits per pixels.
unsigned long Compression; //Compression. Usually set to 0.
unsigned long SizeImage; //Size in bytes of the bitmap.
unsigned long XPixelsPreMeter; //Horizontal pixels per meter.
unsigned long YPixelsPreMeter; //Vertical pixels per meter.
unsigned long ColorsUsed; //Number of colors used.
unsigned long ColorsImportant; //Number of "important" colors.
};
int ShowBMP(int x, int y, char* FileName)
{
int b,a;
BMP Obj;
unsigned char* Datas;
int in=0;
unsigned char c=0;
FILE * fp;
fp = fopen(FileName,"rb");
if(!fp){
printf("Error : Unable to open file ..");
exit(0);
}
fread(&Obj, sizeof(Obj), 1, fp);
if(Obj.BitsPerPixel!=4) // This isn't a 16 color bmp we can read;
{
fclose(fp);
printf("Error : File format not supported ..");
exit(0);
};
fseek(fp,Obj.OffSet,SEEK_SET);
Datas=(unsigned char*) calloc(Obj.Width/2+1, sizeof(unsigned char));
for(b=Obj.Height;b>=0;b--)
{
fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);
c=0;
in=0;
for(a=0;a<=Obj.Width;a+=2)
{
c = (Datas[in] | 0x00) >>4;
putpixel(a+x,b+y,c);
c = (Datas[in] | 0xF0) & 0x0F;
putpixel(a+1+x,b+y,c);
in++;
}
}
free (Datas);
fclose(fp);
return 1;
}
void main()
{
int color;
int gd , gm ;
gd = VGA ; gm = VGAHI;
initgraph(&gd,&gm,"");
ShowBMP(0,0,"tune.bmp");
getch();
closegraph();
}
Note:
The program presented above is strictly compatible with Turbo C/C++ of any versions that support BGI.
Here im unable to present you the bitmap file "sample.bmp" , so i recommend you to crteate your own file to test this program !
While creating your own file make sure that the new file should be compatible to owr program ! other wise undesired results will come !
Regards!
Prince M. Premnath