Fawkes API  Fawkes Development Version
stereodecoder.cpp
1 
2 /***************************************************************************
3  * stereodecoder.cpp - Stereo decoder utility
4  *
5  * Created: Wed Jul 11 15:50:10 2007 (Atlanta Airport)
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <fvcams/bumblebee2.h>
24 #include <fvutils/writers/jpeg.h>
25 #include <fvutils/readers/fvraw.h>
26 #include <fvutils/color/conversions.h>
27 
28 #include <list>
29 #include <string>
30 #include <cstdlib>
31 
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 
36 using namespace std;
37 using namespace fawkes;
38 using namespace firevision;
39 
40 /** Interleave to YUV422 planar buffers.
41  * Creates an image buffer which has both images side by side.
42  * @param yuv422_first first buffer
43  * @param yuv
44  * */
45 void
46 interleave_yuv422planar(unsigned char *yuv422_first, unsigned char *yuv422_second,
47  unsigned char *out,
48  unsigned int width, unsigned int height)
49 {
50  unsigned char *y1, *y2, *yo, *u1, *u2, *uo, *v1, *v2, *vo;
51  unsigned int half_width = width / 2;
52  y1 = yuv422_first;
53  u1 = y1 + width * height;
54  v1 = u1 + (width * height / 2);
55  y2 = yuv422_second;
56  u2 = y2 + width * height;
57  v2 = u2 + (width * height / 2);
58  yo = out;
59  uo = yo + width * height * 2;
60  vo = uo + width * height;
61 
62  for ( unsigned int i = 0; i < height; ++i) {
63 
64  memcpy(yo, y1, width);
65  yo += width;
66  y1 += width;
67 
68  memcpy(yo, y2, width);
69  yo += width;
70  y2 += width;
71 
72  memcpy(uo, u1, half_width);
73  uo += half_width;
74  u1 += half_width;
75 
76  memcpy(uo, u2, half_width);
77  uo += half_width;
78  u2 += half_width;
79 
80  memcpy(vo, v1, half_width);
81  vo += half_width;
82  v1 += half_width;
83 
84  memcpy(vo, v2, half_width);
85  vo += half_width;
86  v2 += half_width;
87  }
88 
89  /*
90  unsigned int half_width = width / 2;
91  for ( unsigned int i = 0; i < height; ++i) {
92  memcpy(out, yuv422_first, half_width);
93  out += half_width;
94  yuv422_first += half_width;
95  memcpy(out, yuv422_first, half_width);
96  out += half_width;
97  yuv422_second += half_width;
98  }
99  for ( unsigned int i = 0; i < height; ++i) {
100  memcpy(out, yuv422_first, half_width);
101  out += half_width;
102  yuv422_first += half_width;
103  memcpy(out, yuv422_first, half_width);
104  out += half_width;
105  yuv422_second += half_width;
106  }
107  */
108 }
109 
110 int
111 main(int argc, char **argv)
112 {
113 
114  if ( argc < 2 ) {
115  printf("Usage: %s <dir>\n", argv[0]);
116  exit(-1);
117  }
118 
119  string dirname = argv[1];
120 
121  // Get all files
122  DIR *dir;
123  struct dirent *dirp;
124 
125  list<string> files;
126 
127  if ( NULL == (dir = opendir(dirname.c_str())) ) {
128  printf("Failed to open directory %s\n", dirname.c_str());
129  exit(-2);
130  }
131 
132  while ( NULL != (dirp = readdir(dir)) ) {
133  if ( NULL != strstr(dirp->d_name, ".raw") ) {
134  files.push_back(dirp->d_name);
135  }
136  }
137 
138  closedir(dir);
139 
140  files.sort();
141 
142  /*
143  // create directories
144  char *tmp;
145  asprintf(&tmp, "%s/%s", dirname.c_str(), "orig_jpeg");
146  mkdir(tmp, 0644);
147  free(tmp);
148 
149  // create directories
150  asprintf(&tmp, "%s/%s", dirname.c_str(), "disp_jpeg");
151  mkdir(tmp, 0644);
152  free(tmp);
153  */
154 
155  JpegWriter *jpeg = new JpegWriter("tmp.jpg");
156 
157  // printf("%lu images to convert\n", files.size());
158 
159  unsigned int in = 0;
160  try {
161  for (list<string>::iterator f = files.begin(); f != files.end(); ++f) {
162  FvRawReader *fvraw = new FvRawReader((dirname + "/" + (*f)).c_str());
163  printf("%4u Converting %s (%s) ", ++in, (dirname + "/" + (*f)).c_str(), colorspace_to_string(fvraw->colorspace()));
164  unsigned char *raw16 = malloc_buffer(fvraw->colorspace(), fvraw->pixel_width(), fvraw->pixel_height() * 2);
165  unsigned char *rgb = (unsigned char *)malloc(colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()) * 2);
166  unsigned char *deinterlaced = (unsigned char *)malloc(fvraw->pixel_width() * fvraw->pixel_height() * 2);
167  unsigned char *yuv = (unsigned char *)malloc_buffer(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height() * 2);
168  unsigned char *yuv_interleaved = (unsigned char *)malloc_buffer(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height() * 2);
169  fvraw->set_buffer(raw16);
170  fvraw->read();
171 
172  printf("(%ux%u) ", fvraw->pixel_width(), fvraw->pixel_height());
173 
174  Bumblebee2Camera::deinterlace_stereo(raw16, deinterlaced,
175  fvraw->pixel_width(), fvraw->pixel_height());
176  Bumblebee2Camera::decode_bayer(deinterlaced, rgb,
177  fvraw->pixel_width(), fvraw->pixel_height(),
178  BAYER_PATTERN_BGGR);
179  /*
180  convert(RGB, YUV422_PLANAR,
181  rgb + colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()),
182  yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
183  fvraw->pixel_width(), fvraw->pixel_height());
184  */
185  convert(RGB, YUV422_PLANAR,
186  rgb,
187  yuv,
188  fvraw->pixel_width(), fvraw->pixel_height());
189 
190  convert(RGB, YUV422_PLANAR,
191  rgb + colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()),
192  yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
193  fvraw->pixel_width(), fvraw->pixel_height());
194 
195  interleave_yuv422planar(yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
196  yuv, yuv_interleaved, fvraw->pixel_width(), fvraw->pixel_height());
197 
198  *f += ".jpg";
199  printf("to %s\n", (dirname + "/orig_jpeg/" + (*f)).c_str());
200 
201  jpeg->set_filename((dirname + "/orig_jpeg/" + (*f)).c_str());
202  jpeg->set_buffer(YUV422_PLANAR, yuv_interleaved);
203  // jpeg->set_buffer(YUV422_PLANAR, yuv);
204  jpeg->set_dimensions(fvraw->pixel_width() * 2, fvraw->pixel_height());
205  jpeg->write();
206 
207  delete fvraw;
208  free(raw16);
209  free(rgb);
210  free(deinterlaced);
211  free(yuv);
212  free(yuv_interleaved);
213  }
214  } catch (Exception &e) {
215  e.print_trace();
216  throw;
217  }
218 }
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: jpeg.cpp:82
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Definition: fvraw.cpp:81
Fawkes library namespace.
STL namespace.
virtual void read()
Read data from file.
Definition: fvraw.cpp:121
virtual void write()
Write to file.
Definition: jpeg.cpp:93
Base class for exceptions in Fawkes.
Definition: exception.h:36
JPEG file writer.
Definition: jpeg.h:36
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:106
virtual unsigned int pixel_width()
Get width of read image in pixels.
Definition: fvraw.cpp:99
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Definition: fvraw.cpp:88
virtual unsigned int pixel_height()
Get height of read image in pixels.
Definition: fvraw.cpp:110
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:132
FvRaw image reader implementation.
Definition: fvraw.h:37