Fawkes API  Fawkes Development Version
tricalcdisp.cpp
1 
2 /***************************************************************************
3  * calcdisp.cpp - Calculate disparities for the given images
4  *
5  * Created: Mon Oct 08 13:42:01 2007
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 <fvstereo/triclops.h>
24 #include <fvutils/color/conversions.h>
25 #include <fvutils/readers/fvraw.h>
26 #include <fvutils/writers/jpeg.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 
30 #include <cstdlib>
31 #include <dirent.h>
32 #include <list>
33 #include <string>
34 #include <unistd.h>
35 
36 using namespace std;
37 using namespace fawkes;
38 using namespace firevision;
39 
40 int
41 main(int argc, char **argv)
42 {
43  if (argc < 3) {
44  printf("Usage: %s <image> <triclops_context>\n", argv[0]);
45  exit(-1);
46  }
47 
48  const char *file = argv[1];
49  const char *context_file = argv[2];
50 
51  char *outfile;
52  asprintf(&outfile, "%s.jpg", file);
53 
54  JpegWriter *jpeg = new JpegWriter(outfile);
55 
56  try {
57  FvRawReader *fvraw = new FvRawReader(file);
58 
59  unsigned int width = fvraw->pixel_width();
60  unsigned int height = fvraw->pixel_height();
61 
62  if (fvraw->colorspace() != RAW16) {
63  printf("Can only operate on RAW16 images!\n");
64  delete jpeg;
65  delete fvraw;
66  return -1;
67  }
68 
69  printf("Calculating disparity for %s to %s\n", file, outfile);
70  printf("Using Triclops context file %s\n", context_file);
71 
72  unsigned char *raw16 = malloc_buffer(RAW16, width, height);
73  unsigned char *yuv422_planar = malloc_buffer(YUV422_PLANAR, width, height);
74 
75  TriclopsStereoProcessor *tsp = new TriclopsStereoProcessor(width, height, context_file);
76 
77  fvraw->set_buffer(raw16);
78  fvraw->read();
79 
80  tsp->set_raw_buffer(raw16);
81 
82  tsp->preprocess_stereo();
83  tsp->calculate_disparity();
84 
85  memcpy(yuv422_planar, tsp->disparity_buffer(), width * height);
86  memset(yuv422_planar + width * height, 128, width * height);
87 
88  jpeg->set_buffer(YUV422_PLANAR, yuv422_planar);
89  jpeg->set_dimensions(width, height);
90  jpeg->write();
91 
92  delete jpeg;
93  delete fvraw;
94  free(raw16);
95  free(yuv422_planar);
96  delete tsp;
97  free(outfile);
98 
99  } catch (Exception &e) {
100  e.print_trace();
101  throw;
102  }
103 }
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
Stereo processing using PGR Triclops SDK.
Definition: triclops.h:38
virtual void preprocess_stereo()
Do any pre-processing needed.
Definition: triclops.cpp:523
virtual void calculate_disparity(ROI *roi=0)
Caculate disparity images.
Definition: triclops.cpp:546
virtual void set_raw_buffer(unsigned char *raw16_buffer)
Set raw buffer.
Definition: triclops.cpp:256
virtual unsigned char * disparity_buffer()
Get the disparity image buffer.
Definition: triclops.cpp:632
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: writer.cpp:128
Fawkes library namespace.