A SAMPLE PROGRAM DEMONSTRATING HOW TO READ A BITMAP FILE

/* Structures */
typedef struct
{
int rows; int cols; unsigned char* data;
} sImage;

/* Functions */
long getImageInfo(FILE*, long, int);

int main(int argc, char * argv[])
{

FILE *bmpInput;
char signature[2]; /* Signature of the Image File BM = BMP */
int nRows, nCols; /* Row and Column size of the Image */
int xpixpeRm, ypixpeRm; /* Pixel/m */
long nColors; /* BMP number of colors */
long fileSize; /* BMP file size */
long vectorSize; /* BMP's raster data in number of bytes */
int nBits; /* # of BIts per Pixel */
int rasterOffset; /* Beginning of the Raster Data */
int i;

if(argc<2)>
printf("Usage: %s bmpInput.bmp\n", argv[0]);
exit(0);
}
printf("Reading filename %s\n", argv[1]);

if ((bmpInput = fopen(argv[1], "rb")) == NULL)
{
printf("Can not read BMP file");
exit(0);
} /* Read BMP input file */

fseek(bmpInput, 0L, SEEK_SET); /* File pointer at byte #0 */

/* Signature of the File BM = BMP at byte # 0*/
for(i=0; i<2;>
{
signature[i] = (char)getImageInfo(bmpInput, i, 1);
}
if ((signature[0] == 'B') && (signature[1] == 'M')) printf("It is verified that the Image is in Bitmap format\n");
else
{
printf("The image is not a bitmap format, quitting....\n");
exit(0);
}

/* Read BMP bits/pixel at byte #28 */
nBits = (int)getImageInfo(bmpInput, 28, 2);
printf("The Image is \t%d-bit\n", nBits);

/* Position of First Raster Data at byte # 10*/
rasterOffset = (int)getImageInfo(bmpInput, 10, 4);
printf("The beginning of the Raster Data \nis at \t\t%d byte\n", rasterOffset);

/* Read BMP file size at byte # 2 */
fileSize = getImageInfo(bmpInput, 2, 4);
printf("File size is \t%ld byte\n", fileSize);

/* Read BMP width at byte #18 */
nCols = (int)getImageInfo(bmpInput, 18, 4);
printf("Width: \t\t%d\n", nCols);

/* Read BMP height at byte #22 */
nRows = (int)getImageInfo(bmpInput, 22, 4);
printf("Height: \t%d\n", nRows);

/* # of Pixels in a meter in x direction at byte # 38 */
xpixpeRm = (int)getImageInfo(bmpInput, 38, 4);
printf("Image has \t%d pixels per m in x-dir.\n", xpixpeRm);

/* # of Pixels in a meter in y direction at byte # 42 */
ypixpeRm = getImageInfo(bmpInput, 42, 4);
printf("Image has \t%d pixels per m in y-dir.\n", ypixpeRm);

/* Read number of colors at byte #46 */
nColors = pow(2L,nBits);
printf("There are \t%ld number of Colors \n", nColors);

vectorSize = (long)((long)nCols*(long)nRows);
printf("Vector Size is \t%ld\n", vectorSize);

fclose (bmpInput);

} /* end of main */


long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{

unsigned char *ptrC;
long value=0L;
unsigned char dummy;
int i;

dummy = '0'; ptrC = &dummy;

fseek(inputFile, offset, SEEK_SET);

for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);

value = (long)(value + (*ptrC)*(pow(256, (i-1))));
}

return(value);

} /* end of getImageInfo */

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!