Fawkes API  Fawkes Development Version
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 <fvstereo/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 using namespace firevision;
00041 
00042 int
00043 main(int argc, char **argv)
00044 {
00045 
00046   if ( argc < 3 ) {
00047     printf("Usage: %s <image> <triclops_context>\n", argv[0]);
00048     exit(-1);
00049   }
00050 
00051   const char *file = argv[1];
00052   const char *context_file = argv[2];
00053 
00054   char *outfile;
00055   asprintf(&outfile, "%s.jpg", file);
00056 
00057   JpegWriter *jpeg = new JpegWriter(outfile);
00058 
00059   try {
00060     FvRawReader *fvraw = new FvRawReader(file);
00061 
00062     unsigned int width = fvraw->pixel_width();
00063     unsigned int height = fvraw->pixel_height();
00064 
00065     if ( fvraw->colorspace() != RAW16 ) {
00066       printf("Can only operate on RAW16 images!\n");
00067       delete jpeg;
00068       delete fvraw;
00069       return -1;
00070     }
00071 
00072     printf("Calculating disparity for %s to %s\n", file, outfile);
00073     printf("Using Triclops context file %s\n", context_file);
00074 
00075     unsigned char *raw16 = malloc_buffer(RAW16, width, height);
00076     unsigned char *yuv422_planar = malloc_buffer(YUV422_PLANAR, width, height);
00077 
00078     TriclopsStereoProcessor *tsp = new TriclopsStereoProcessor(width, height, context_file);
00079 
00080     fvraw->set_buffer(raw16);
00081     fvraw->read();
00082 
00083     tsp->set_raw_buffer(raw16);
00084 
00085     tsp->preprocess_stereo();
00086     tsp->calculate_disparity();
00087 
00088     memcpy(yuv422_planar, tsp->disparity_buffer(), width * height);
00089     memset(yuv422_planar + width * height, 128, width * height);
00090 
00091     jpeg->set_buffer(YUV422_PLANAR, yuv422_planar);
00092     jpeg->set_dimensions(width, height);
00093     jpeg->write();
00094 
00095     delete jpeg;
00096     delete fvraw;
00097     free(raw16);
00098     free(yuv422_planar);
00099     delete tsp;
00100     free(outfile);
00101 
00102   } catch (Exception &e) {
00103     e.print_trace();
00104     throw;
00105   }
00106 }