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