tricalcdisp.cpp

00001
00002 /***************************************************************************
00003  *  calcdisp.cpp - Calculate disparities for the given images
00004  *
00005  *  Created: Mon Oct 08 13:42:01 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022
00023 #include <fvutils/writers/jpeg.h>
00024 #include <fvutils/readers/fvraw.h>
00025 #include <fvutils/color/conversions.h>
00026
00027 #include <stereo/triclops.h>
00028
00029 #include <list>
00030 #include <string>
00031
00032 #include <sys/stat.h>
00033 #include <sys/types.h>
00034 #include <dirent.h>
00035 #include <unistd.h>
00036 #include <cstdlib>
00037
00038 using namespace std;
00039 using namespace fawkes;
00040
00041 int
00042 main(int argc, char **argv)
00043 {
00044
00045   if ( argc < 3 ) {
00046     printf("Usage: %s <image> <triclops_context>\n", argv[0]);
00047     exit(-1);
00048   }
00049
00050   const char *file = argv[1];
00051   const char *context_file = argv[2];
00052
00053   char *outfile;
00054   asprintf(&outfile, "%s.jpg", file);
00055
00056   JpegWriter *jpeg = new JpegWriter(outfile);
00057
00058   try {
00059     FvRawReader *fvraw = new FvRawReader(file);
00060
00061     unsigned int width = fvraw->pixel_width();
00062     unsigned int height = fvraw->pixel_height();
00063
00064     if ( fvraw->colorspace() != RAW16 ) {
00065       printf("Can only operate on RAW16 images!\n");
00066       delete jpeg;
00067       delete fvraw;
00068       return -1;
00069     }
00070
00071     printf("Calculating disparity for %s to %s\n", file, outfile);
00072     printf("Using Triclops context file %s\n", context_file);
00073
00074     unsigned char *raw16 = malloc_buffer(RAW16, width, height);
00075     unsigned char *yuv422_planar = malloc_buffer(YUV422_PLANAR, width, height);
00076
00077     TriclopsStereoProcessor *tsp = new TriclopsStereoProcessor(width, height, context_file);
00078
00079     fvraw->set_buffer(raw16);
00080     fvraw->read();
00081
00082     tsp->set_raw_buffer(raw16);
00083
00084     tsp->preprocess_stereo();
00085     tsp->calculate_disparity();
00086
00087     memcpy(yuv422_planar, tsp->disparity_buffer(), width * height);
00088     memset(yuv422_planar + width * height, 128, width * height);
00089
00090     jpeg->set_buffer(YUV422_PLANAR, yuv422_planar);
00091     jpeg->set_dimensions(width, height);
00092     jpeg->write();
00093
00094     delete jpeg;
00095     delete fvraw;
00096     free(raw16);
00097     free(yuv422_planar);
00098     delete tsp;
00099     free(outfile);
00100
00101   } catch (Exception &e) {
00102     e.print_trace();
00103     throw;
00104   }
00105 }