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 <core/exception.h>
24 #include <fvcams/bumblebee2.h>
25 #include <fvutils/color/conversions.h>
26 #include <fvutils/readers/fvraw.h>
27 #include <fvutils/writers/jpeg.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <cstdlib>
32 #include <cstring>
33 #include <dirent.h>
34 #include <list>
35 #include <string>
36 
37 using namespace std;
38 using namespace fawkes;
39 using namespace firevision;
40 
41 /** Interleave to YUV422 planar buffers.
42  * Creates an image buffer which has both images side by side.
43  * @param yuv422_first first buffer
44  * @param yuv
45  * */
46 void
47 interleave_yuv422planar(unsigned char *yuv422_first,
48  unsigned char *yuv422_second,
49  unsigned char *out,
50  unsigned int width,
51  unsigned int height)
52 {
53  unsigned char *y1, *y2, *yo, *u1, *u2, *uo, *v1, *v2, *vo;
54  unsigned int half_width = width / 2;
55  y1 = yuv422_first;
56  u1 = y1 + width * height;
57  v1 = u1 + (width * height / 2);
58  y2 = yuv422_second;
59  u2 = y2 + width * height;
60  v2 = u2 + (width * height / 2);
61  yo = out;
62  uo = yo + width * height * 2;
63  vo = uo + width * height;
64 
65  for (unsigned int i = 0; i < height; ++i) {
66  memcpy(yo, y1, width);
67  yo += width;
68  y1 += width;
69 
70  memcpy(yo, y2, width);
71  yo += width;
72  y2 += width;
73 
74  memcpy(uo, u1, half_width);
75  uo += half_width;
76  u1 += half_width;
77 
78  memcpy(uo, u2, half_width);
79  uo += half_width;
80  u2 += half_width;
81 
82  memcpy(vo, v1, half_width);
83  vo += half_width;
84  v1 += half_width;
85 
86  memcpy(vo, v2, half_width);
87  vo += half_width;
88  v2 += half_width;
89  }
90 
91  /*
92  unsigned int half_width = width / 2;
93  for ( unsigned int i = 0; i < height; ++i) {
94  memcpy(out, yuv422_first, half_width);
95  out += half_width;
96  yuv422_first += half_width;
97  memcpy(out, yuv422_first, half_width);
98  out += half_width;
99  yuv422_second += half_width;
100  }
101  for ( unsigned int i = 0; i < height; ++i) {
102  memcpy(out, yuv422_first, half_width);
103  out += half_width;
104  yuv422_first += half_width;
105  memcpy(out, yuv422_first, half_width);
106  out += half_width;
107  yuv422_second += half_width;
108  }
109  */
110 }
111 
112 int
113 main(int argc, char **argv)
114 {
115  if (argc < 2) {
116  printf("Usage: %s <dir>\n", argv[0]);
117  exit(-1);
118  }
119 
120  string dirname = argv[1];
121 
122  // Get all files
123  DIR * dir;
124  struct dirent *dirp;
125 
126  list<string> files;
127 
128  if (NULL == (dir = opendir(dirname.c_str()))) {
129  printf("Failed to open directory %s\n", dirname.c_str());
130  exit(-2);
131  }
132 
133  while (NULL != (dirp = readdir(dir))) {
134  if (NULL != strstr(dirp->d_name, ".raw")) {
135  files.push_back(dirp->d_name);
136  }
137  }
138 
139  closedir(dir);
140 
141  files.sort();
142 
143  /*
144  // create directories
145  char *tmp;
146  asprintf(&tmp, "%s/%s", dirname.c_str(), "orig_jpeg");
147  mkdir(tmp, 0644);
148  free(tmp);
149 
150  // create directories
151  asprintf(&tmp, "%s/%s", dirname.c_str(), "disp_jpeg");
152  mkdir(tmp, 0644);
153  free(tmp);
154  */
155 
156  JpegWriter *jpeg = new JpegWriter("tmp.jpg");
157 
158  // printf("%lu images to convert\n", files.size());
159 
160  try {
161  unsigned int in = 0;
162  for (list<string>::iterator f = files.begin(); f != files.end(); ++f) {
163  FvRawReader *fvraw = new FvRawReader((dirname + "/" + (*f)).c_str());
164  printf("%4u Converting %s (%s) ",
165  ++in,
166  (dirname + "/" + (*f)).c_str(),
167  colorspace_to_string(fvraw->colorspace()));
168  unsigned char *raw16 =
169  malloc_buffer(fvraw->colorspace(), fvraw->pixel_width(), fvraw->pixel_height() * 2);
170  unsigned char *rgb = (unsigned char *)malloc(
171  colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()) * 2);
172  unsigned char *deinterlaced =
173  (unsigned char *)malloc((size_t)fvraw->pixel_width() * (size_t)fvraw->pixel_height() * 2);
174  unsigned char *yuv = (unsigned char *)malloc_buffer(YUV422_PLANAR,
175  fvraw->pixel_width(),
176  fvraw->pixel_height() * 2);
177  unsigned char *yuv_interleaved = (unsigned char *)malloc_buffer(YUV422_PLANAR,
178  fvraw->pixel_width(),
179  fvraw->pixel_height() * 2);
180  fvraw->set_buffer(raw16);
181  fvraw->read();
182 
183  printf("(%ux%u) ", fvraw->pixel_width(), fvraw->pixel_height());
184 
185  Bumblebee2Camera::deinterlace_stereo(raw16,
186  deinterlaced,
187  fvraw->pixel_width(),
188  fvraw->pixel_height());
189  Bumblebee2Camera::decode_bayer(
190  deinterlaced, rgb, fvraw->pixel_width(), fvraw->pixel_height(), BAYER_PATTERN_BGGR);
191  /*
192  convert(RGB, YUV422_PLANAR,
193  rgb + colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()),
194  yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
195  fvraw->pixel_width(), fvraw->pixel_height());
196  */
197  convert(RGB, YUV422_PLANAR, rgb, yuv, fvraw->pixel_width(), fvraw->pixel_height());
198 
199  convert(
200  RGB,
201  YUV422_PLANAR,
202  rgb + colorspace_buffer_size(RGB, fvraw->pixel_width(), fvraw->pixel_height()),
203  yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
204  fvraw->pixel_width(),
205  fvraw->pixel_height());
206 
207  interleave_yuv422planar(
208  yuv + colorspace_buffer_size(YUV422_PLANAR, fvraw->pixel_width(), fvraw->pixel_height()),
209  yuv,
210  yuv_interleaved,
211  fvraw->pixel_width(),
212  fvraw->pixel_height());
213 
214  *f += ".jpg";
215  printf("to %s\n", (dirname + "/orig_jpeg/" + (*f)).c_str());
216 
217  jpeg->set_filename((dirname + "/orig_jpeg/" + (*f)).c_str());
218  jpeg->set_buffer(YUV422_PLANAR, yuv_interleaved);
219  // jpeg->set_buffer(YUV422_PLANAR, yuv);
220  jpeg->set_dimensions(fvraw->pixel_width() * 2, fvraw->pixel_height());
221  jpeg->write();
222 
223  delete fvraw;
224  free(raw16);
225  free(rgb);
226  free(deinterlaced);
227  free(yuv);
228  free(yuv_interleaved);
229  }
230  } catch (Exception &e) {
231  e.print_trace();
232  throw;
233  }
234 }
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
FvRaw image reader implementation.
Definition: fvraw.h:35
virtual void read()
Read data from file.
Definition: fvraw.cpp:111
virtual unsigned int pixel_width()
Get width of read image in pixels.
Definition: fvraw.cpp:91
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Definition: fvraw.cpp:81
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Definition: fvraw.cpp:75
virtual unsigned int pixel_height()
Get height of read image in pixels.
Definition: fvraw.cpp:101
JPEG file writer.
Definition: jpeg.h:34
virtual void write()
Write to file.
Definition: jpeg.cpp:85
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: jpeg.cpp:75
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:128
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:102
Fawkes library namespace.