Fawkes API  Fawkes Development Version
converter.cpp
00001 
00002 /***************************************************************************
00003  *  converter.cpp - Convert between file formats supported by Firevision
00004  *
00005  *  Created: Tue Jul 05 14:34:21 2007
00006  *  Copyright  2007  Daniel Beck
00007  *             2008  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL file in the doc directory.
00022  */
00023 
00024 #include <fvcams/fileloader.h>
00025 #include <fvutils/writers/fvraw.h>
00026 #ifdef HAVE_LIBJPEG
00027 #  include <fvutils/writers/jpeg.h>
00028 #endif
00029 #ifdef HAVE_LIBPNG
00030 #  include <fvutils/writers/png.h>
00031 #endif
00032 #include <fvutils/writers/pnm.h>
00033 
00034 #include <fvutils/readers/fvraw.h>
00035 #include <fvutils/readers/jpeg.h>
00036 
00037 #include <fvutils/color/conversions.h>
00038 #include <utils/system/argparser.h>
00039 
00040 #include <cstring>
00041 #include <cstdlib>
00042 
00043 using namespace fawkes;
00044 using namespace firevision;
00045 
00046 void
00047 print_usage(const char *program_name)
00048 {
00049   printf("Usage: %s [-u -c colorspace -w width -h height] <infile> <outfile>\n\n"
00050          "  -u             Unformatted raw, you must supply -c, -w and -h\n"
00051          "  -c colorspace  colorspace string\n"
00052          "  -w width       width of image in pixels\n"
00053          "  -h height      height of image in pixels\n",
00054          program_name);
00055 }
00056 
00057 
00058 int
00059 main(int argc, char** argv)
00060 {
00061   ArgumentParser argp(argc, argv, "uw:h:c:");
00062   if ( argp.num_items() != 2 )
00063   {
00064     print_usage(argp.program_name());
00065     printf("\nInvalid number of files given\n\n");
00066     return -1;
00067   }
00068 
00069   const char *fn_in  = argp.items()[0];
00070   const char *fn_out = argp.items()[1];
00071 
00072   char* fn_out_copy = strdup(fn_out);
00073   
00074   printf("Input file:  %s\n"
00075          "Output file: %s\n",
00076          fn_in, fn_out);
00077   
00078   // strip off extension
00079   char *t = strtok(fn_out_copy, ".");
00080   if (NULL == t)
00081   {
00082     printf("invalid filename");
00083     return -2;
00084   }
00085 
00086   char* ext_out;
00087   while(NULL != t)
00088   {
00089     ext_out = t;
00090     t = strtok(NULL, ".");
00091   }
00092 
00093   FileLoader *fl = NULL;
00094   Writer* writer = NULL;
00095 
00096   if ( argp.has_arg("u") )
00097   {
00098     if (argp.has_arg("c") && argp.has_arg("w") && argp.has_arg("h"))
00099     {
00100       fl = new FileLoader(colorspace_by_name(argp.arg("c")), fn_in,
00101                           argp.parse_int("w"), argp.parse_int("h"));
00102       printf("Input image: %s, %lix%li\n", argp.arg("c"),
00103              argp.parse_int("w"), argp.parse_int("h"));
00104     }
00105     else
00106     {
00107       printf("You have to supply all of -w, -h, -c when using -u.\n");
00108       return -3;
00109     }
00110   }
00111   else
00112   {
00113     fl = new FileLoader(fn_in);
00114   }
00115 
00116   fl->open();
00117   fl->start();
00118 
00119   unsigned char *tmpbuf = malloc_buffer(YUV422_PLANAR, fl->pixel_width(), fl->pixel_height());
00120   convert(fl->colorspace(), YUV422_PLANAR, fl->buffer(), tmpbuf,
00121           fl->pixel_width(), fl->pixel_height());
00122 
00123   // FvRaw
00124   if ( 0 == strcmp(ext_out, "raw") )
00125   {
00126     printf("Format for out file %s is FvRaw\n", fn_out);
00127     writer = new FvRawWriter();
00128   }
00129 #ifdef HAVE_LIBJPEG
00130   // JPEG
00131   else if ( 0 == strcmp(ext_out, "jpeg") || 0 == strcmp(ext_out, "jpg") )
00132   { 
00133     printf("Format for out file %s is Jpeg\n", fn_out);
00134     writer = new JpegWriter();
00135   }
00136 #endif
00137 #ifdef HAVE_LIBPNG
00138   // PNG
00139   else if ( 0 == strcmp(ext_out, "png") )
00140   {
00141     printf("Format for out file %s is PNG\n", fn_out);
00142     writer = new PNGWriter();
00143   }
00144 #endif
00145   // PNM
00146   else if ( 0 == strcmp(ext_out, "pnm") )
00147   {
00148     printf("Format for out file %s is PNM\n", fn_out);
00149     writer = new PNMWriter(PNM_PPM);
00150   }
00151   else
00152   {
00153     printf("Unknown output file format\n");
00154     exit(-2);
00155   }
00156 
00157   writer->set_filename(fn_out);
00158   writer->set_dimensions(fl->pixel_width(), fl->pixel_height());
00159   writer->set_buffer(YUV422_PLANAR, tmpbuf);
00160   writer->write();
00161 
00162   free(fn_out_copy);
00163 
00164   delete fl;
00165   delete writer;
00166 
00167   free(tmpbuf);
00168   
00169   return 0;
00170 }