/* * reads all the BMP informations out of the file and stores them into * the dataStructure defined in BMP.h * * all routines written by Yannick Suter aka AsC/Nowadays! 1998 * * no comments. if you have questions email me: [asc@netlink.ch] */ #include #include #include #include "BMP.H" extern FILE *BMPf; extern BITMAPFILEHEADER bmp_filehead; extern BITMAPINFOHEADER bmp_infohead; extern unsigned char infile2[100]; extern unsigned char bmp_4bitpal[4*16]; extern unsigned char bmp_8bitpal[4*256]; extern unsigned char *bmpdata; void read_BMPfilehead(){ unsigned char rbuf[4]; unsigned short *t; printf(" : READING [BMP] filehead\n"); bmp_filehead.bfType = read_word(); bmp_filehead.bfSize = read_dword(); bmp_filehead.bfReserved1 = read_word(); bmp_filehead.bfReserved2 = read_word(); bmp_filehead.bfOffBits = read_dword(); } void read_BMPinfohead(){ unsigned char rbuf[4]; unsigned short *t; printf(" : READING [BMP] infohead\n"); bmp_infohead.biSize = read_dword(); bmp_infohead.biWidth = read_dword(); bmp_infohead.biHeight = read_dword(); bmp_infohead.biPlanes = read_word(); bmp_infohead.biBitCount = read_word(); bmp_infohead.biCompression = read_dword(); bmp_infohead.biSizeImage = read_dword(); bmp_infohead.biXPelsPerMeter = read_dword(); bmp_infohead.biYPelsPerMeter = read_dword(); bmp_infohead.biClrUsed = read_dword(); bmp_infohead.biClrImportant = read_dword(); } void read_BMPpalette(){ int x; printf(" : READING [BMP] bitmap palette\n"); if(bmp_infohead.biBitCount == 4) fread(&bmp_4bitpal,1,16*4,BMPf); if(bmp_infohead.biBitCount == 8) fread(&bmp_8bitpal,1,256*4,BMPf); } void read_BMPdata(){ printf(" : READING [BMP] image data\n"); if(bmp_infohead.biBitCount == 4) { bmpdata = (unsigned char *)malloc(bmp_infohead.biSizeImage>>1); fread(bmpdata,1,bmp_infohead.biSizeImage>>1,BMPf); } if(bmp_infohead.biBitCount == 8) { bmpdata = (unsigned char *)malloc(bmp_infohead.biSizeImage); fread(bmpdata,1,bmp_infohead.biSizeImage,BMPf); } if(bmp_infohead.biBitCount == 24) { bmpdata = (unsigned char *)malloc(bmp_infohead.biSizeImage); fread(bmpdata,1,bmp_infohead.biSizeImage,BMPf); } } void read_BMP(char *filename){ if ((BMPf = fopen(filename,"rb")) != NULL){ printf("  OPEN [BMP] file [%s]\n",infile2); read_BMPfilehead(); read_BMPinfohead(); if(bmp_infohead.biBitCount<=8)read_BMPpalette(); read_BMPdata(); fclose(BMPf); } else { printf(" : couldn't open BMPfile [%s]",infile2);} }