Fawkes API  Fawkes Development Version
converter.cpp
1 
2 /***************************************************************************
3  * converter.cpp - Convert between file formats supported by Firevision
4  *
5  * Created: Tue Jul 05 14:34:21 2007
6  * Copyright 2007 Daniel Beck
7  * 2008 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 #include <fvcams/fileloader.h>
25 #include <fvutils/writers/fvraw.h>
26 #ifdef HAVE_LIBJPEG
27 # include <fvutils/writers/jpeg.h>
28 #endif
29 #ifdef HAVE_LIBPNG
30 # include <fvutils/writers/png.h>
31 #endif
32 #include <fvutils/writers/pnm.h>
33 
34 #include <fvutils/readers/fvraw.h>
35 #include <fvutils/readers/jpeg.h>
36 
37 #include <fvutils/color/conversions.h>
38 #include <utils/system/argparser.h>
39 
40 #include <cstring>
41 #include <cstdlib>
42 
43 using namespace fawkes;
44 using namespace firevision;
45 
46 void
47 print_usage(const char *program_name)
48 {
49  printf("Usage: %s [-u -c colorspace -w width -h height] <infile> <outfile>\n\n"
50  " -u Unformatted raw, you must supply -c, -w and -h\n"
51  " -c colorspace colorspace string\n"
52  " -w width width of image in pixels\n"
53  " -h height height of image in pixels\n",
54  program_name);
55 }
56 
57 
58 int
59 main(int argc, char** argv)
60 {
61  ArgumentParser argp(argc, argv, "uw:h:c:");
62  if ( argp.num_items() != 2 )
63  {
64  print_usage(argp.program_name());
65  printf("\nInvalid number of files given\n\n");
66  return -1;
67  }
68 
69  const char *fn_in = argp.items()[0];
70  const char *fn_out = argp.items()[1];
71 
72  char* fn_out_copy = strdup(fn_out);
73 
74  printf("Input file: %s\n"
75  "Output file: %s\n",
76  fn_in, fn_out);
77 
78  // strip off extension
79  char *t = strtok(fn_out_copy, ".");
80  if (NULL == t)
81  {
82  printf("invalid filename");
83  return -2;
84  }
85 
86  char* ext_out;
87  while(NULL != t)
88  {
89  ext_out = t;
90  t = strtok(NULL, ".");
91  }
92 
93  FileLoader *fl = NULL;
94  Writer* writer = NULL;
95 
96  if ( argp.has_arg("u") )
97  {
98  if (argp.has_arg("c") && argp.has_arg("w") && argp.has_arg("h"))
99  {
100  fl = new FileLoader(colorspace_by_name(argp.arg("c")), fn_in,
101  argp.parse_int("w"), argp.parse_int("h"));
102  printf("Input image: %s, %lix%li\n", argp.arg("c"),
103  argp.parse_int("w"), argp.parse_int("h"));
104  }
105  else
106  {
107  printf("You have to supply all of -w, -h, -c when using -u.\n");
108  return -3;
109  }
110  }
111  else
112  {
113  fl = new FileLoader(fn_in);
114  }
115 
116  fl->open();
117  fl->start();
118 
119  unsigned char *tmpbuf = malloc_buffer(YUV422_PLANAR, fl->pixel_width(), fl->pixel_height());
120  convert(fl->colorspace(), YUV422_PLANAR, fl->buffer(), tmpbuf,
121  fl->pixel_width(), fl->pixel_height());
122 
123  // FvRaw
124  if ( 0 == strcmp(ext_out, "raw") )
125  {
126  printf("Format for out file %s is FvRaw\n", fn_out);
127  writer = new FvRawWriter();
128  }
129 #ifdef HAVE_LIBJPEG
130  // JPEG
131  else if ( 0 == strcmp(ext_out, "jpeg") || 0 == strcmp(ext_out, "jpg") )
132  {
133  printf("Format for out file %s is Jpeg\n", fn_out);
134  writer = new JpegWriter();
135  }
136 #endif
137 #ifdef HAVE_LIBPNG
138  // PNG
139  else if ( 0 == strcmp(ext_out, "png") )
140  {
141  printf("Format for out file %s is PNG\n", fn_out);
142  writer = new PNGWriter();
143  }
144 #endif
145  // PNM
146  else if ( 0 == strcmp(ext_out, "pnm") )
147  {
148  printf("Format for out file %s is PNM\n", fn_out);
149  writer = new PNMWriter(PNM_PPM);
150  }
151  else
152  {
153  printf("Unknown output file format\n");
154  exit(-2);
155  }
156 
157  writer->set_filename(fn_out);
158  writer->set_dimensions(fl->pixel_width(), fl->pixel_height());
159  writer->set_buffer(YUV422_PLANAR, tmpbuf);
160  writer->write();
161 
162  free(fn_out_copy);
163 
164  delete fl;
165  delete writer;
166 
167  free(tmpbuf);
168 
169  return 0;
170 }
virtual colorspace_t colorspace()
Colorspace of returned image.
Definition: fileloader.cpp:319
PNG file writer.
Definition: png.h:34
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: writer.cpp:143
Fawkes library namespace.
Interface to write images.
Definition: writer.h:34
virtual void write()=0
Write to file.
Parse command line arguments.
Definition: argparser.h:66
virtual unsigned int pixel_height()
Height of image in pixels.
Definition: fileloader.cpp:312
FvRaw Writer implementation.
Definition: fvraw.h:34
virtual void open()
Open the camera.
Definition: fileloader.cpp:195
JPEG file writer.
Definition: jpeg.h:36
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:106
virtual unsigned char * buffer()
Get access to current image buffer.
Definition: fileloader.cpp:255
virtual unsigned int pixel_width()
Width of image in pixels.
Definition: fileloader.cpp:305
PNM file writer.
Definition: pnm.h:49
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:132
virtual void start()
Start image transfer from the camera.
Definition: fileloader.cpp:213
Load images from files.
Definition: fileloader.h:39