cmpp.cpp

00001
00002 /***************************************************************************
00003  *  cmpp.cpp - Colormap Postprocessor. Extends the regions in the colormap
00004  *
00005  *  Created: Tue April 23 17:42:14 2009
00006  *  Copyright  2009  Daniel Beck
00007  *             2009  Stefan Schiffer
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL file in the doc directory.
00022  */
00023
00024 #include <utils/system/argparser.h>
00025 #include <fvutils/colormap/colormap.h>
00026 #include <fvutils/colormap/yuvcm.h>
00027 #include <fvutils/colormap/cmfile.h>
00028
00029 #include <cstring>
00030 #include <cmath>
00031 #include <cstdio>
00032
00033 using namespace fawkes;
00034
00035 int main( int argc, char** argv )
00036 {
00037   ArgumentParser* argp = new ArgumentParser( argc, argv, "i:o:" );
00038
00039   char* in_file  = NULL;
00040   char* out_file = NULL;
00041
00042   if ( argp->has_arg( "i" ) )
00043   {
00044     in_file = strdup( argp->arg( "i" ) );
00045   }
00046
00047   if ( argp->has_arg( "o" ) )
00048   {
00049     out_file = strdup( argp->arg( "o" ) );
00050   }
00051
00052   if ( !in_file || !out_file )
00053   {
00054     printf("Usage: argv[0] -i <input colormap> -o <output colormap>\n");
00055   }
00056   else
00057   {
00058     printf("Reading colormap from file %s.\n", in_file);
00059     printf("Writing modified colormap to file %s.\n", out_file);
00060
00061     ColormapFile cmfile;
00062     cmfile.read( in_file );
00063     Colormap *cm = cmfile.get_colormap();
00064     unsigned int cm_width  = cm->width();
00065     unsigned int cm_height = cm->height();
00066     unsigned int cm_depth  = cm->depth();
00067
00068     unsigned char* cm_buffer = (unsigned char*) malloc( cm->size() );
00069     memcpy( (void*) cm_buffer, cm->get_buffer(), cm->size() );
00070
00071     YuvColormap* cmpp = new YuvColormap( cm_depth, cm_width, cm_height );
00072     cmpp->set( cm_buffer );
00073
00074     for ( unsigned int d = 0; d < cm_depth; ++d )
00075     {
00076       for ( unsigned int w = 0; w < cm_width; ++w )
00077       {
00078         for ( unsigned int h = 0; h < cm_height; ++h )
00079         {
00080           float yuvfac = cm->deepness() / (float) cm->depth();
00081           unsigned int y = (unsigned int) (d * yuvfac);
00082
00083           color_t cur_color = cm->determine(y, w, h);
00084
00085           // skip current cell if it already has a color
00086           if ( cur_color != C_OTHER )
00087           { continue; }
00088
00089           unsigned int cm_counter[ C_OTHER + 1 ];
00090
00091           for ( unsigned int i = 0; i <= C_OTHER; ++i )
00092           { cm_counter[ i ] = 0; }
00093
00094           unsigned int tst_radius_dp = 1;
00095           unsigned int tst_radius_uv = 4;
00096
00097           unsigned int num_neighbours = 0;
00098
00099           for ( unsigned int dd = (unsigned int) fmax(d - tst_radius_dp, 0);
00100                 dd <= fmin( d + tst_radius_dp, cm_depth - 1);
00101                 ++dd )
00102           {
00103             for ( unsigned int ww = (unsigned int) fmax(w - tst_radius_uv, 0);
00104                   ww <= fmin( w + tst_radius_uv, cm_width - 1 );
00105                   ++ww )
00106             {
00107               for ( unsigned int hh = (unsigned int) fmax(h - tst_radius_uv, 0);
00108                     hh <= fmin( h + tst_radius_uv, cm_height - 1);
00109                     ++hh )
00110               {
00111                 color_t cur_color = cm->determine( (unsigned int) (dd * yuvfac), ww, hh );
00112                 ++cm_counter[ cur_color ];
00113
00114                 ++num_neighbours;
00115               }
00116             }
00117           }
00118
00119           unsigned int max  = 0;
00120           color_t max_color = C_OTHER;
00121
00122           for ( unsigned int i = 0; i < C_OTHER; ++i )
00123           {
00124             if ( cm_counter[ i ] > max )
00125             {
00126               max = cm_counter[ i ];
00127               max_color = (color_t) i;
00128             }
00129           }
00130
00131           if ( max > num_neighbours * 0.1 && max_color != C_OTHER )
00132           {
00133             printf("max=%d  max_color=%d  num_neighbours=%d\n", max, max_color, num_neighbours);
00134             cmpp->set( y, w, h, max_color );
00135           }
00136         }  // h
00137       }  // w
00138     }  // d
00139
00140     ColormapFile cmout( cm_depth, cm_width, cm_height );
00141     cmout.add_colormap( cmpp );
00142     printf( "Writing modified colormap.\n" );
00143     cmout.write( out_file );
00144   }
00145
00146   return 0;
00147 }