/* Converts ppm or pgm format to avi format */ #include #include #include #include #include #include #include #include #define AVIIF_KEYFRAME 0x00000010L // this frame is a key frame. char basename[100]="",filename[100],inputfilename[100],tailname[100]="",ANS[100],line[100]; unsigned char pixel[3]; FILE *ppmfile; int i,no_frames,no_cols,no_rows,header_size,start_frame; int is_grey_scale=0; int frame_rate=30; int field_width; char format_string[100]; PAVIFILE pfile = NULL; AVISTREAMINFO strhdr; PAVISTREAM ps = NULL, psCompressed = NULL; AVICOMPRESSOPTIONS opts; AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts}; BITMAPINFOHEADER *alpbi; int row,col; unsigned char * image_buffer,*clrptr; int main(int argc, char **argv) { printf("do you want a basename? (Y/N)\n"); scanf("%s",ANS); if ((ANS[0]=='y')||(ANS[0]=='Y')) { printf("basename?\n"); scanf("%s",basename); } printf("do you want a tailname? (Y/N)\n"); scanf("%s",ANS); if ((ANS[0]=='y')||(ANS[0]=='Y')) { printf("tailname?\n"); scanf("%s",tailname); } printf("start frame?\n"); scanf("%d",&start_frame); printf("number of frames?\n"); scanf("%d",&no_frames); printf("field width for fle number - 0 for not fixed\n"); scanf("%d",&field_width); if (field_width>0) sprintf(format_string,"%%s%%0%dd%%s",field_width); else sprintf(format_string,"%s","%s%d%s"); sprintf(inputfilename,format_string,basename,start_frame,tailname); printf("frame rate (frames per second - 30 for PAL 25 for NTSC - can be what you like\n"); scanf("%d",&frame_rate); printf("output filename?\n"); scanf("%s",filename); fprintf(stderr,"opening %s\n",inputfilename); ppmfile=fopen(inputfilename,"rb"); fgets(line,100,ppmfile); while (line[0]=='#') fgets(line,100,ppmfile); if ((line[0]!='P')||(line[1]!='6')) { if ((line[0]=='P')&&(line[1]='5')) {is_grey_scale=1; fprintf(stderr,"reading grey scale images \n"); } else { fprintf(stderr,"found %s \n",line); return FALSE; } } else fprintf(stderr,"reading colour images\n"); // MessageBox(NULL,line,"FOUND",MB_OK); fgets(line,100,ppmfile); while (line[0]=='#') fgets(line,100,ppmfile); // MessageBox(NULL,line,"FOUND",MB_OK); sscanf(line,"%d %d",&no_cols,&no_rows); fclose(ppmfile); fprintf(stderr,"will read in %d frames of size %d %d\n",no_frames,no_cols,no_rows); if (is_grey_scale==1) image_buffer = (unsigned char *) malloc(no_cols*no_rows); else image_buffer = (unsigned char *) malloc(3*no_cols*no_rows); /* image buffer */ AVIFileInit(); AVIFileOpen(&pfile, // returned file pointer filename, // file name OF_WRITE | OF_CREATE, // mode to open file with NULL); // Fill in the header for the video stream.... _fmemset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = 1; strhdr.dwRate = frame_rate; // fps if (is_grey_scale==1) strhdr.dwSuggestedBufferSize = no_cols*no_rows; else strhdr.dwSuggestedBufferSize = 3*no_cols*no_rows; SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream (int) no_cols, (int) no_rows); // And create the stream; AVIFileCreateStream(pfile, // file pointer &ps, // returned stream pointer &strhdr); // stream header _fmemset(&opts, 0, sizeof(opts)); //probably don't need save options - probably the 0's initiate the //dialog that gives options //just remind you of form of the structure //STDAPI_(BOOL) AVISaveOptions(HWND hwnd, // UINT uiFlags, // int nStreams, // PAVISTREAM FAR *ppavi, // LPAVICOMPRESSOPTIONS FAR *plpOptions); AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts); if (is_grey_scale==1) alpbi=(BITMAPINFOHEADER *) malloc(sizeof(BITMAPINFOHEADER)+1024); else alpbi=(BITMAPINFOHEADER *) malloc(sizeof(BITMAPINFOHEADER)); AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL); alpbi->biSize = sizeof(BITMAPINFOHEADER) ; alpbi->biWidth = no_cols ; alpbi->biHeight = no_rows ; alpbi->biPlanes = 1 ; if (is_grey_scale==1) alpbi->biBitCount = (WORD) 8 ; else alpbi->biBitCount = (WORD) 24 ; alpbi->biCompression = BI_RGB ; if (is_grey_scale==1) alpbi->biSizeImage = no_cols*no_rows; else alpbi->biSizeImage = 3*no_cols*no_rows; alpbi->biXPelsPerMeter = 0 ; alpbi->biYPelsPerMeter = 0 ; if (is_grey_scale==1) alpbi->biClrUsed = 256; //256 entry colour map?? else alpbi->biClrUsed = 0; //256 entry colour map?? alpbi->biClrImportant = 0; /* fill in colour map */ if (is_grey_scale==1) { clrptr= ((unsigned char *) alpbi)+sizeof(BITMAPINFOHEADER); for (i=0;i<256;i++) { *(clrptr+i*4)=*(clrptr+i*4+1)= *(clrptr+i*4+2)=*(clrptr+i*4+3)= (unsigned char) i; } } if (is_grey_scale==1) AVIStreamSetFormat(psCompressed, 0, alpbi, // stream format alpbi->biSize+1024 ); else AVIStreamSetFormat(psCompressed, 0, alpbi, // stream format alpbi->biSize ); if (is_grey_scale==1) { for (i=start_frame;i<(no_frames+start_frame);i++) { sprintf(inputfilename,format_string,basename,i,tailname); fprintf(stderr,"opening %s\n",inputfilename); fprintf(stderr,"no_frames %d start_frame %d i %d\n",no_frames,start_frame,i); ppmfile=fopen(inputfilename,"rb"); // get signature line fgets(line,100,ppmfile); while (line[0]=='#') fgets(line,100,ppmfile); if ((line[0]!='P')||(line[1]!='6')) { if ((line[0]!='P')||(line[1]!='5')) MessageBox(NULL,line,"FOUND",MB_OK|MB_ICONSTOP); } //get size line fgets(line,100,ppmfile); while (line[0]=='#') fgets(line,100,ppmfile); //get 255 line fgets(line,100,ppmfile); // while (line[0]=='#') // fgets(line,100,ppmfile); //should check for 255 for (row=0;row