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 <cams/fileloader.h>
00025 #include <fvutils/writers/fvraw.h>
00026 #include <fvutils/writers/jpeg.h>
00027 #include <fvutils/writers/png.h>
00028 #include <fvutils/writers/pnm.h>
00029 
00030 #include <fvutils/readers/fvraw.h>
00031 #include <fvutils/readers/jpeg.h>
00032 
00033 #include <fvutils/color/conversions.h>
00034 #include <utils/system/argparser.h>
00035 
00036 #include <cstring>
00037 #include <cstdlib>
00038 
00039 using namespace fawkes;
00040 using namespace firevision;
00041 
00042 void
00043 print_usage(const char *program_name)
00044 {
00045   printf("Usage: %s [-u -c colorspace -w width -h height] <infile> <outfile>\n\n"
00046          "  -u             Unformatted raw, you must supply -c, -w and -h\n"
00047          "  -c colorspace  colorspace string\n"
00048          "  -w width       width of image in pixels\n"
00049          "  -h height      height of image in pixels\n",
00050          program_name);
00051 }
00052 
00053 
00054 int
00055 main(int argc, char** argv)
00056 {
00057   ArgumentParser argp(argc, argv, "uw:h:c:");
00058   if ( argp.num_items() != 2 )
00059   {
00060     print_usage(argp.program_name());
00061     printf("\nInvalid number of files given\n\n");
00062     return -1;
00063   }
00064 
00065   const char *fn_in  = argp.items()[0];
00066   const char *fn_out = argp.items()[1];
00067 
00068   char* fn_out_copy = strdup(fn_out);
00069   
00070   printf("Input file:  %s\n"
00071          "Output file: %s\n",
00072          fn_in, fn_out);
00073   
00074   // strip off extension
00075   char *t = strtok(fn_out_copy, ".");
00076   if (NULL == t)
00077   {
00078     printf("invalid filename");
00079     return -2;
00080   }
00081 
00082   char* ext_out;
00083   while(NULL != t)
00084   {
00085     ext_out = t;
00086     t = strtok(NULL, ".");
00087   }
00088 
00089   FileLoader *fl = NULL;
00090   Writer* writer = NULL;
00091 
00092   if ( argp.has_arg("u") )
00093   {
00094     if (argp.has_arg("c") && argp.has_arg("w") && argp.has_arg("h"))
00095     {
00096       fl = new FileLoader(colorspace_by_name(argp.arg("c")), fn_in,
00097                           argp.parse_int("w"), argp.parse_int("h"));
00098       printf("Input image: %s, %lix%li\n", argp.arg("c"),
00099              argp.parse_int("w"), argp.parse_int("h"));
00100     }
00101     else
00102     {
00103       printf("You have to supply all of -w, -h, -c when using -u.\n");
00104       return -3;
00105     }
00106   }
00107   else
00108   {
00109     fl = new FileLoader(fn_in);
00110   }
00111 
00112   fl->open();
00113   fl->start();
00114 
00115   unsigned char *tmpbuf = malloc_buffer(YUV422_PLANAR, fl->pixel_width(), fl->pixel_height());
00116   convert(fl->colorspace(), YUV422_PLANAR, fl->buffer(), tmpbuf,
00117           fl->pixel_width(), fl->pixel_height());
00118 
00119   // FvRaw
00120   if ( 0 == strcmp(ext_out, "raw") )
00121   {
00122     printf("Format for out file %s is FvRaw\n", fn_out);
00123     writer = new FvRawWriter();
00124   }
00125   // JPEG
00126   else if ( 0 == strcmp(ext_out, "jpeg") || 0 == strcmp(ext_out, "jpg") )
00127   { 
00128     printf("Format for out file %s is Jpeg\n", fn_out);
00129     writer = new JpegWriter();
00130   }
00131   // PNG
00132   else if ( 0 == strcmp(ext_out, "png") )
00133   {
00134     printf("Format for out file %s is PNG\n", fn_out);
00135     writer = new PNGWriter();
00136   }
00137   // PNM
00138   else if ( 0 == strcmp(ext_out, "pnm") )
00139   {
00140     printf("Format for out file %s is PNM\n", fn_out);
00141     writer = new PNMWriter(PNM_PPM);
00142   }
00143   else
00144   {
00145     printf("Unknown output file format\n");
00146     exit(-2);
00147   }
00148 
00149   writer->set_filename(fn_out);
00150   writer->set_dimensions(fl->pixel_width(), fl->pixel_height());
00151   writer->set_buffer(YUV422_PLANAR, tmpbuf);
00152   writer->write();
00153 
00154   free(fn_out_copy);
00155 
00156   delete fl;
00157   delete writer;
00158 
00159   free(tmpbuf);
00160   
00161   return 0;
00162 }