Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

sample2.c

Go to the documentation of this file.
00001 /*
00002  * sample2.c
00003  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
00004  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
00005  *
00006  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
00007  * See http://libmpeg2.sourceforge.net/ for updates.
00008  *
00009  * mpeg2dec is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * mpeg2dec is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <inttypes.h>
00027 
00028 #include "mpeg2.h"
00029 #include "convert.h"
00030 
00031 char *prefix = NULL ;  /* RWCox */
00032 int   count  = 0    ;
00033 int docount  = 0    ;
00034 #include <string.h>
00035 
00036 static void save_ppm (int width, int height, uint8_t * buf, int num)
00037 {
00038     char filename[1000];
00039     FILE * ppmfile;
00040 
00041     count++ ;
00042     if( docount ) return ;
00043 
00044     if( prefix == NULL )
00045       sprintf (filename, "%06d.ppm", num);
00046     else
00047       sprintf (filename, "%s%06d.ppm", prefix,num);
00048     ppmfile = fopen (filename, "wb");
00049     if (!ppmfile) return;
00050     fprintf (ppmfile, "P6\n%d %d\n255\n", width, height);
00051     fwrite (buf, 3 * width, height, ppmfile);
00052     fclose (ppmfile);
00053 }
00054 
00055 static void sample2 (FILE * file)
00056 {
00057 #define BUFFER_SIZE 4096
00058     uint8_t buffer[BUFFER_SIZE];
00059     mpeg2dec_t * mpeg2dec;
00060     const mpeg2_info_t * info;
00061     int state;
00062     int size;
00063     int framenum = 0;
00064 
00065     mpeg2dec = mpeg2_init ();
00066     if (mpeg2dec == NULL)
00067         exit (1);
00068     info = mpeg2_info (mpeg2dec);
00069 
00070     size = BUFFER_SIZE;
00071     do {
00072         state = mpeg2_parse (mpeg2dec);
00073         switch (state) {
00074         case -1:
00075             size = fread (buffer, 1, BUFFER_SIZE, file);
00076             mpeg2_buffer (mpeg2dec, buffer, buffer + size);
00077             break;
00078         case STATE_SEQUENCE:
00079             mpeg2_convert (mpeg2dec, convert_rgb24, NULL);
00080             break;
00081         case STATE_SLICE:
00082         case STATE_END:
00083             if (info->display_fbuf)
00084                 save_ppm (info->sequence->width, info->sequence->height,
00085                           info->display_fbuf->buf[0], framenum++);
00086             break;
00087         }
00088     } while (size);
00089 
00090     mpeg2_close (mpeg2dec);
00091 }
00092 
00093 int main (int argc, char ** argv)
00094 {
00095     FILE * file;
00096     int iarg=1 ;
00097 
00098     if( argc < 2 || strstr(argv[1],"-help") != NULL ){       /* RWCox */
00099       printf("Usage:  mpegtoppm [-prefix ppp] file.mpg\n"
00100              "Writes files named 'ppp'000001.ppm, etc.\n" ) ;
00101       exit(0) ;
00102     }
00103 
00104     while( iarg < argc && argv[iarg][0] == '-' ){   /* RWCox: options */
00105       if( strncmp(argv[iarg],"-c",2) == 0 ){
00106         docount = 1 ; ++iarg ; continue ;
00107       }
00108 
00109       if( strncmp(argv[iarg],"-p",2) == 0 ){
00110         prefix = argv[++iarg] ; ++iarg ; continue ;
00111       }
00112     }
00113 
00114     if( iarg < argc ){
00115         file = fopen (argv[iarg], "rb");
00116         if (!file) {
00117             fprintf (stderr, "Could not open file %s\n", argv[iarg]);
00118             exit (1);
00119         }
00120     } else
00121         file = stdin;
00122 
00123     sample2 (file);
00124 
00125     if( docount && count > 0 ){   /* RWCox */
00126       char filename[1000] = "\0" ;
00127       FILE *fp ;
00128       if( prefix != NULL ) strcpy(filename,prefix) ;
00129       strcat(filename,"COUNT") ;
00130       fp = fopen (filename, "wb");
00131       fprintf(fp,"%d\n",count) ;
00132       fclose(fp) ;
00133     }
00134 
00135     return 0;
00136 }
 

Powered by Plone

This site conforms to the following standards: