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/color/conversions.h>
33 #include <fvutils/readers/fvraw.h>
34 #include <fvutils/readers/jpeg.h>
35 #include <fvutils/writers/pnm.h>
36 #include <utils/system/argparser.h>
37 
38 #include <cstdlib>
39 #include <cstring>
40 
41 using namespace fawkes;
42 using namespace firevision;
43 
44 void
45 print_usage(const char *program_name)
46 {
47  printf("Usage: %s [-u -c colorspace -w width -h height] <infile> <outfile>\n\n"
48  " -u Unformatted raw, you must supply -c, -w and -h\n"
49  " -c colorspace colorspace string\n"
50  " -w width width of image in pixels\n"
51  " -h height height of image in pixels\n",
52  program_name);
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58  ArgumentParser argp(argc, argv, "uw:h:c:");
59  if (argp.num_items() != 2) {
60  print_usage(argp.program_name());
61  printf("\nInvalid number of files given\n\n");
62  return -1;
63  }
64 
65  const char *fn_in = argp.items()[0];
66  const char *fn_out = argp.items()[1];
67 
68  char *fn_out_copy = strdup(fn_out);
69 
70  printf("Input file: %s\n"
71  "Output file: %s\n",
72  fn_in,
73  fn_out);
74 
75  // strip off extension
76  char *t = strtok(fn_out_copy, ".");
77  if (NULL == t) {
78  printf("invalid filename");
79  return -2;
80  }
81 
82  char *ext_out;
83  while (NULL != t) {
84  ext_out = t;
85  t = strtok(NULL, ".");
86  }
87 
88  FileLoader *fl = NULL;
89  Writer * writer = NULL;
90 
91  if (argp.has_arg("u")) {
92  if (argp.has_arg("c") && argp.has_arg("w") && argp.has_arg("h")) {
93  fl = new FileLoader(colorspace_by_name(argp.arg("c")),
94  fn_in,
95  argp.parse_int("w"),
96  argp.parse_int("h"));
97  printf("Input image: %s, %lix%li\n", argp.arg("c"), argp.parse_int("w"), argp.parse_int("h"));
98  } else {
99  printf("You have to supply all of -w, -h, -c when using -u.\n");
100  return -3;
101  }
102  } else {
103  fl = new FileLoader(fn_in);
104  }
105 
106  fl->open();
107  fl->start();
108 
109  unsigned char *tmpbuf = malloc_buffer(YUV422_PLANAR, fl->pixel_width(), fl->pixel_height());
110  convert(
111  fl->colorspace(), YUV422_PLANAR, fl->buffer(), tmpbuf, fl->pixel_width(), fl->pixel_height());
112 
113  // FvRaw
114  if (0 == strcmp(ext_out, "raw")) {
115  printf("Format for out file %s is FvRaw\n", fn_out);
116  writer = new FvRawWriter();
117  }
118 #ifdef HAVE_LIBJPEG
119  // JPEG
120  else if (0 == strcmp(ext_out, "jpeg") || 0 == strcmp(ext_out, "jpg")) {
121  printf("Format for out file %s is Jpeg\n", fn_out);
122  writer = new JpegWriter();
123  }
124 #endif
125 #ifdef HAVE_LIBPNG
126  // PNG
127  else if (0 == strcmp(ext_out, "png")) {
128  printf("Format for out file %s is PNG\n", fn_out);
129  writer = new PNGWriter();
130  }
131 #endif
132  // PNM
133  else if (0 == strcmp(ext_out, "pnm")) {
134  printf("Format for out file %s is PNM\n", fn_out);
135  writer = new PNMWriter(PNM_PPM);
136  } else {
137  printf("Unknown output file format\n");
138  exit(-2);
139  }
140 
141  writer->set_filename(fn_out);
142  writer->set_dimensions(fl->pixel_width(), fl->pixel_height());
143  writer->set_buffer(YUV422_PLANAR, tmpbuf);
144  writer->write();
145 
146  free(fn_out_copy);
147 
148  delete fl;
149  delete writer;
150 
151  free(tmpbuf);
152 
153  return 0;
154 }
Parse command line arguments.
Definition: argparser.h:64
Load images from files.
Definition: fileloader.h:38
virtual colorspace_t colorspace()
Colorspace of returned image.
Definition: fileloader.cpp:305
virtual void start()
Start image transfer from the camera.
Definition: fileloader.cpp:210
virtual unsigned char * buffer()
Get access to current image buffer.
Definition: fileloader.cpp:250
virtual void open()
Open the camera.
Definition: fileloader.cpp:192
virtual unsigned int pixel_height()
Height of image in pixels.
Definition: fileloader.cpp:299
virtual unsigned int pixel_width()
Width of image in pixels.
Definition: fileloader.cpp:293
FvRaw Writer implementation.
Definition: fvraw.h:32
JPEG file writer.
Definition: jpeg.h:34
PNG file writer.
Definition: png.h:32
PNM file writer.
Definition: pnm.h:45
Interface to write images.
Definition: writer.h:32
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:128
virtual void write()=0
Write to file.
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:102
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: writer.cpp:139
Fawkes library namespace.