--- trunk/xvidcore/examples/xvid_decraw.c 2004/04/11 11:19:57 1422 +++ trunk/xvidcore/examples/xvid_decraw.c 2004/04/12 14:05:08 1423 @@ -20,7 +20,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: xvid_decraw.c,v 1.11 2004-04-10 04:25:31 suxen_drol Exp $ + * $Id: xvid_decraw.c,v 1.12 2004-04-12 14:05:08 edgomez Exp $ * ****************************************************************************/ @@ -57,12 +57,17 @@ /* max number of frames */ #define ABS_MAXFRAMENR 9999 +#define USE_PNM 0 +#define USE_TGA 1 + static int XDIM = 0; static int YDIM = 0; static int ARG_SAVEDECOUTPUT = 0; static int ARG_SAVEMPEGSTREAM = 0; static char *ARG_INPUTFILE = NULL; - +static int CSP = XVID_CSP_I420; +static int BPP = 1; +static int FORMAT = USE_PNM; static char filepath[256] = "./"; static void *dec_handle = NULL; @@ -145,6 +150,31 @@ ARG_INPUTFILE = argv[i]; } else if (strcmp("-m", argv[i]) == 0) { ARG_SAVEMPEGSTREAM = 1; + } else if (strcmp("-c", argv[i]) == 0 && i < argc - 1 ) { + i++; + if (strcmp(argv[i], "rgb16") == 0) { + CSP = XVID_CSP_RGB555; + BPP = 2; + } else if (strcmp(argv[i], "rgb24") == 0) { + CSP = XVID_CSP_BGR; + BPP = 3; + } else if (strcmp(argv[i], "rgb32") == 0) { + CSP = XVID_CSP_BGRA; + BPP = 4; + } else if (strcmp(argv[i], "yv12") == 0) { + CSP = XVID_CSP_YV12; + BPP = 1; + } else { + CSP = XVID_CSP_I420; + BPP = 1; + } + } else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { + i++; + if (strcmp(argv[i], "tga") == 0) { + FORMAT = USE_TGA; + } else { + FORMAT = USE_PNM; + } } else if (strcmp("-help", argv[i]) == 0) { usage(); return(0); @@ -170,6 +200,11 @@ } } + /* PNM/PGM format can't handle 16/32 bit data */ + if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { + FORMAT = USE_TGA; + } + /***************************************************************************** * Memory allocation ****************************************************************************/ @@ -305,8 +340,8 @@ /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { - sprintf(filename, "%sdec%05d.pgm", filepath, filenr); - if(write_pgm(filename,out_buffer)) { + sprintf(filename, "%sdec%05d", filepath, filenr); + if(write_image(filename, out_buffer)) { fprintf(stderr, "Error writing decoded PGM frame %s\n", filename); @@ -346,8 +381,8 @@ /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { - sprintf(filename, "%sdec%05d.pgm", filepath, filenr); - if(write_pgm(filename, out_buffer)) { + sprintf(filename, "%sdec%05d", filepath, filenr); + if(write_image(filename, out_buffer)) { fprintf(stderr, "Error writing decoded PGM frame %s\n", filename); @@ -399,6 +434,8 @@ fprintf(stderr, " -debug : debug level (debug=0)\n"); fprintf(stderr, " -i string : input filename (default=stdin)\n"); fprintf(stderr, " -d : save decoder output\n"); + fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); + fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm)\n"); fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); fprintf(stderr, " -help : This help message\n"); fprintf(stderr, " (* means default)\n"); @@ -428,49 +465,154 @@ * output functions ****************************************************************************/ -static int -write_pgm(char *filename, - unsigned char *image) +int write_image(char *prefix, unsigned char *image) { - int loop; + char filename[1024]; + char *ext; + int ret; - unsigned char *y = image; - unsigned char *u = image + XDIM*YDIM; - unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; + if (FORMAT == USE_PNM && BPP == 1) { + ext = "pgm"; + } else if (FORMAT == USE_PNM && BPP == 3) { + ext = "pnm"; + } else if (FORMAT == USE_TGA) { + ext = "tga"; + } else { + fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used"); + exit(-1); + } - FILE *filehandle; - filehandle=fopen(filename,"w+b"); - if (filehandle) { + sprintf(filename, "%s.%s", prefix, ext); - /* Write header */ - fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); + if (FORMAT == USE_PNM) { + ret = write_pnm(filename, image); + } else { + ret = write_tga(filename, image); + } - /* Write Y data */ - fwrite(y, 1, XDIM*YDIM, filehandle); + return(ret); +} - for(loop=0; loop1)?2:3; /* Uncompressed true color (2) or greymap (3) */ + hdr[3] = 0; /* Color map specification (not used) */ + hdr[4] = 0; /* Color map specification (not used) */ + hdr[5] = 0; /* Color map specification (not used) */ + hdr[6] = 0; /* Color map specification (not used) */ + hdr[7] = 0; /* Color map specification (not used) */ + hdr[8] = 0; /* LSB X origin */ + hdr[9] = 0; /* MSB X origin */ + hdr[10] = 0; /* LSB Y origin */ + hdr[11] = 0; /* MSB Y origin */ + hdr[12] = (XDIM>>0)&0xff; /* LSB Width */ + hdr[13] = (XDIM>>8)&0xff; /* MSB Width */ + if (BPP > 1) { + hdr[14] = (YDIM>>0)&0xff; /* LSB Height */ + hdr[15] = (YDIM>>8)&0xff; /* MSB Height */ + } else { + hdr[14] = ((YDIM*3)>>1)&0xff; /* LSB Height */ + hdr[15] = ((YDIM*3)>>9)&0xff; /* MSB Height */ + } + hdr[16] = BPP*8; + hdr[17] = 0x00 | (1<<5) /* Up to down */ | (0<<4); /* Image descriptor */ + + /* Write header */ + fwrite(hdr, 1, sizeof(hdr), f); +#ifdef ARCH_IS_LITTLE_ENDIAN + /* write first plane */ + fwrite(image, 1, XDIM*YDIM*BPP, f); +#else + { + int i; + for (i=0; i