#include #include #include #include #include "array_utility.h" // Sample program to convert a limited number of BMP file types // to a PPM (color) or PGM (grey) image file char *strstr(); typedef struct { unsigned short int type; // Magic identifier unsigned int size; // File size in bytes unsigned short int reserved1, reserved2; unsigned int offset; // Offset to image data, bytes } FILEHEADER; typedef struct { unsigned int size; // Header size in bytes int width,height; // Width and height of image unsigned short int planes; // Number of color planes unsigned short int bits; // Bits per pixel unsigned int compression; // Compression type unsigned int imagesize; // Image size in bytes int xresolution, yresolution; // Pixels per meter unsigned int ncolors; // Number of colors unsigned int importantcolors; // Important colors } INFOHEADER; int main(int argc,char **argv) { int i,j,k, M,N, color, gotindex = 0; unsigned char grey,r,g,b; FILEHEADER fileheader; INFOHEADER infoheader; int **colortable; char fname_in[50], *fname_out, *sp; FILE *fp; float ***img_color, **img_grey; // Check arguments if (argc < 2) { printf("Usage: %s filename\n",argv[0]); printf("Enter filename: "); scanf("%s",&fname_in); } else { strcpy(fname_in,argv[1]); } if ((fp = fopen(fname_in,"r")) == NULL) { fprintf(stderr,"Unable to open BMP file \"%s\"\n",fname_in); exit(-1); } // Read and check the fileheader // FILEHEADER struct has 14 bytes, not a multiple of 4 bytes, but some // machines/compilers assume so and pad this struct by 2 bytes to make it 16, // which will unalign the future fread() calls. That is why here the components // are read individually. fread(&fileheader.type,sizeof(unsigned short int),1,fp); fprintf(stderr,"ID is: %d, should be %d\n",fileheader.type,'M'*256+'B'); if (fileheader.type != 'M'*256+'B') { printf("Input file is not in BMP format.\n"); exit(-1); } fread(&fileheader.size,sizeof(unsigned int),1,fp); fprintf(stderr,"File size is %d bytes\n",fileheader.size); fread(&fileheader.reserved1,sizeof(unsigned short int),1,fp); fread(&fileheader.reserved2,sizeof(unsigned short int),1,fp); fread(&fileheader.offset,sizeof(unsigned int),1,fp); fprintf(stderr,"Offset to image data is %d bytes\n",fileheader.offset); // Read and check the information header if (fread(&infoheader,sizeof(INFOHEADER),1,fp) != 1) { fprintf(stderr,"Failed to read BMP info header\n"); exit(-1); } N=infoheader.width; M=infoheader.height; fprintf(stderr,"Infoheader size = %d \n",infoheader.size); fprintf(stderr,"Image size = %d x %d\n",infoheader.width,infoheader.height); fprintf(stderr,"Number of color planes is %d\n",infoheader.planes); fprintf(stderr,"Bits per pixel is %d\n",infoheader.bits); fprintf(stderr,"Compression type is %d\n",infoheader.compression); fprintf(stderr,"Image size is %d\n",infoheader.imagesize); fprintf(stderr,"Resolution, x=%d, y=%d\n", infoheader.xresolution,infoheader.yresolution); fprintf(stderr,"Number of colors is %d\n",infoheader.ncolors); fprintf(stderr,"Number of required colors is %d\n", infoheader.importantcolors); printf("size of fileheader: %d\n",sizeof(FILEHEADER)); printf("size of infoheader: %d\n",sizeof(INFOHEADER)); color=0; // Read color lookup table if there is one // if (infoheader.ncolors > 0) { if (infoheader.bits == 24) { printf("bits/pixel=24, color image.\n"); color=1; } else if (infoheader.bits < 24) { gotindex = 1; colortable=alloc2di(infoheader.ncolors,4); printf("Color table:\n"); for (i=0; i=0; i--) { for (j=0; j