| 1 |
/* pgmabel.c - read a portable graymap and making the deconvolution |
| 2 |
** |
| 3 |
** Deconvolution of an axial-symmetric image of an rotation symmetrical |
| 4 |
** process by solving the linear equation system with y-Axis as |
| 5 |
** symmetry-line |
| 6 |
** |
| 7 |
** Copyright (C) 1997-2006 by German Aerospace Research establishment |
| 8 |
** |
| 9 |
** Author: Volker Schmidt |
| 10 |
** [email protected] |
| 11 |
** |
| 12 |
** Permission to use, copy, modify, and distribute this software and its |
| 13 |
** documentation for any purpose and without fee is hereby granted, provided |
| 14 |
** that the above copyright notice appear in all copies and that both that |
| 15 |
** copyright notice and this permission notice appear in supporting |
| 16 |
** documentation. This software is provided "as is" without express or |
| 17 |
** implied warranty. |
| 18 |
** |
| 19 |
** $HISTORY: |
| 20 |
** |
| 21 |
** 24 Jan 2002 : 001.009 : some optimzization |
| 22 |
** 22 Jan 2002 : 001.008 : some stupid calculations changed |
| 23 |
** 08 Aug 2001 : 001.007 : new usage (netpbm-conform) |
| 24 |
** 27 Jul 1998 : 001.006 : First try of error correction |
| 25 |
** 26 Mar 1998 : 001.005 : Calculating the dl's before transformation |
| 26 |
** 06 Feb 1998 : 001.004 : Include of a logo in the upper left edge |
| 27 |
** 26 Nov 1997 : 001.003 : Some bug fixes and reading only lines |
| 28 |
** 25 Nov 1997 : 001.002 : include of pixsize for getting scale invariant |
| 29 |
** 03 Sep 1997 : 001.001 : only define for PID2 |
| 30 |
** 03 Sep 1997 : 001.000 : First public release |
| 31 |
** 21 Aug 1997 : 000.909 : Recalculate the streching-factor |
| 32 |
** 20 Aug 1997 : 000.908 : -left and -right for calculating only one side |
| 33 |
** 20 Aug 1997 : 000.906 : correction of divisor, include of -factor |
| 34 |
** 15 Aug 1997 : 000.905 : Include of -help and -axis |
| 35 |
*/ |
| 36 |
|
| 37 |
static const char* const version="$VER: pgmabel 1.009 (24 Jan 2002)"; |
| 38 |
|
| 39 |
#include <math.h> |
| 40 |
#include <stdlib.h> /* for calloc */ |
| 41 |
|
| 42 |
#include "pm_c_util.h" |
| 43 |
#include "mallocvar.h" |
| 44 |
#include "pgm.h" |
| 45 |
|
| 46 |
#ifndef PID2 /* PI/2 (on AMIGA always defined) */ |
| 47 |
#define PID2 1.57079632679489661923 |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
/* some global variables */ |
| 52 |
static double *aldl, *ardl; /* pointer for weighting factors */ |
| 53 |
|
| 54 |
/* ---------------------------------------------------------------------------- |
| 55 |
** procedure for calculating the sum of the calculated surfaces with the |
| 56 |
** weight of the surface |
| 57 |
** n <- index of end point of the summation |
| 58 |
** N <- width of the calculated row |
| 59 |
** xr <- array of the calculated elements of the row |
| 60 |
** adl <- pre-calculated surface coefficient for each segment |
| 61 |
*/ |
| 62 |
static double |
| 63 |
Sum ( int const n, |
| 64 |
double const *xr, |
| 65 |
int const N, |
| 66 |
double const *adl) |
| 67 |
{ |
| 68 |
int k; |
| 69 |
double result=0.0; |
| 70 |
|
| 71 |
if (n==0) return(0.0); /* outer ring is 0 per definition */ |
| 72 |
for (k=0 ; k<=(n-1) ; k++) |
| 73 |
{ |
| 74 |
result += xr[k] * ( adl[k*N+n] - adl[(k+1)*N+n]); |
| 75 |
/* result += xr[k] * ( dr(k,n+0.5,N) - dr(k+1,n+0.5,N)); */ |
| 76 |
} |
| 77 |
return(result); |
| 78 |
} |
| 79 |
|
| 80 |
/* ---------------------------------------------------------------------------- |
| 81 |
** procedure for calculating the surface coefficient for the Integration |
| 82 |
** R, N <- indizes of the coefficient |
| 83 |
** r <- radial position of the center of the surface |
| 84 |
*/ |
| 85 |
static double |
| 86 |
dr ( int const R, |
| 87 |
double const r, |
| 88 |
int const N) |
| 89 |
{ |
| 90 |
double a; |
| 91 |
double b; |
| 92 |
a=(double) N-R ; |
| 93 |
b=(double) N-r ; |
| 94 |
return(sqrt(a*a-b*b)); |
| 95 |
} |
| 96 |
|
| 97 |
/* ---------------------------------------------------------------------------- |
| 98 |
** procedure for making the Abel integration for deconvolution of the image |
| 99 |
** y <-> array with values for deconvolution and results |
| 100 |
** N <- width of the array |
| 101 |
** adl <- array with pre-calculated weighting factors |
| 102 |
*/ |
| 103 |
static void |
| 104 |
abel ( float * y, |
| 105 |
int const N, |
| 106 |
double * const adl) |
| 107 |
{ |
| 108 |
int n; |
| 109 |
double *rho, *rhop; /* results and new index */ |
| 110 |
|
| 111 |
MALLOCARRAY(rho, N); |
| 112 |
if( !rho ) |
| 113 |
pm_error( "out of memory" ); |
| 114 |
for (n=0 ; n<N ; n++) |
| 115 |
rho[n] = 0; |
| 116 |
|
| 117 |
rhop = rho; |
| 118 |
|
| 119 |
for (n=0 ; n<N ; n++) |
| 120 |
{ |
| 121 |
rhop[n] = MAX(0, (y[n] - Sum(n,rho,N,adl))/(adl[n*N+n])); |
| 122 |
/* Clip to 0 for error correction ! */ |
| 123 |
} |
| 124 |
for (n=0 ; n<N ; n++) |
| 125 |
{ |
| 126 |
if (( n>=1 )&&( n<N-1 )) |
| 127 |
(*y++) = ((rho[n-1]*0.5+rho[n]+rho[n+1]*0.5)/2.0);/*1D median filter*/ |
| 128 |
else (*y++) = rho[n]; |
| 129 |
} |
| 130 |
free(rho); |
| 131 |
} |
| 132 |
|
| 133 |
/* ---------------------------------------------------------------------------- |
| 134 |
** printing a help message if Option -h(elp) is chosen |
| 135 |
*/ |
| 136 |
static void |
| 137 |
help() |
| 138 |
{ |
| 139 |
pm_message("-----------------------------------------------------------------"); |
| 140 |
pm_message("| pgmabel |"); |
| 141 |
pm_message("| make a deconvolution with vertical axis as symmetry-line |"); |
| 142 |
pm_message("| usage: |"); |
| 143 |
pm_message("| pgmabel [-help] [-axis N] [-factor N] [-left|-right] |"); |
| 144 |
pm_message("| [-pixsize] [-verbose] [pgmfile] |"); |
| 145 |
pm_message("| axis : horizontal position of the axis |"); |
| 146 |
pm_message("| factor : user defines stretch-factor for the gray levels |"); |
| 147 |
pm_message("| pixsize : size of one pixel in mm (default = 0.1) |"); |
| 148 |
pm_message("| left : calculating only the left (or right) side |"); |
| 149 |
pm_message("| verbose : output of useful data |"); |
| 150 |
pm_message("| pgmfile : Name of a pgmfile (optional) |"); |
| 151 |
pm_message("| |"); |
| 152 |
pm_message("| for further information please contact the manpage |"); |
| 153 |
pm_message("-----------------------------------------------------------------"); |
| 154 |
pm_message("%s",version); /* telling the version */ |
| 155 |
exit(-1); /* retur-code for no result */ |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
/* ---------------------------------------------------------------------------- |
| 163 |
** main program |
| 164 |
*/ |
| 165 |
int main(int argc, char * argv[] ) { |
| 166 |
|
| 167 |
FILE* ifp; |
| 168 |
gray maxval; /* maximum gray-level */ |
| 169 |
gray* grayorig; |
| 170 |
gray* grayrow; /* one line in the image */ |
| 171 |
int argn, rows, cols, row, format; |
| 172 |
int col, midcol=0, temp, tc; |
| 173 |
float *trow; /* temporary row for deconvolution */ |
| 174 |
float l_div, r_div, fac=1.0, cfac=4.0; /* factor for scaling gray-level */ |
| 175 |
float pixsize=0.1; |
| 176 |
/* no verbose, calculating both sides */ |
| 177 |
int verb = FALSE, left = TRUE, right = TRUE; |
| 178 |
const char* const usage = "[-help] [-axis N] [-factor N] [-pixsize N] [-left|-right] [-verbose] [pgmfile]"; |
| 179 |
|
| 180 |
pgm_init( &argc, argv ); |
| 181 |
argn = 1; |
| 182 |
|
| 183 |
/* Check for flags. */ |
| 184 |
while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) |
| 185 |
{ |
| 186 |
if ( pm_keymatch( argv[argn], "-help", 1 ) ) help(); |
| 187 |
else if ( pm_keymatch( argv[argn], "-axis", 1 ) ) |
| 188 |
{ |
| 189 |
++argn; |
| 190 |
if ( argn == argc || sscanf( argv[argn], "%i", &midcol ) !=1 ) |
| 191 |
pm_usage( usage ); |
| 192 |
} |
| 193 |
else if ( pm_keymatch( argv[argn], "-factor", 1 ) ) |
| 194 |
{ |
| 195 |
++argn; |
| 196 |
if ( argn == argc || sscanf( argv[argn], "%f", &fac ) !=1 ) |
| 197 |
pm_usage( usage ); |
| 198 |
} |
| 199 |
else if ( pm_keymatch( argv[argn], "-pixsize", 1 ) ) |
| 200 |
{ |
| 201 |
++argn; |
| 202 |
if ( argn == argc || sscanf( argv[argn], "%f", &pixsize ) !=1 ) |
| 203 |
pm_usage( usage ); |
| 204 |
} |
| 205 |
else if ( pm_keymatch( argv[argn], "-verbose", 1 ) ) |
| 206 |
{ |
| 207 |
verb = TRUE; |
| 208 |
} |
| 209 |
else if ( pm_keymatch( argv[argn], "-left", 1 ) ) |
| 210 |
{ |
| 211 |
if ( left ) right = FALSE; |
| 212 |
else pm_usage( usage ); |
| 213 |
} |
| 214 |
else if ( pm_keymatch( argv[argn], "-right", 1 ) ) |
| 215 |
{ |
| 216 |
if ( right ) left = FALSE; |
| 217 |
else pm_usage( usage ); |
| 218 |
} |
| 219 |
else |
| 220 |
pm_usage( usage ); |
| 221 |
++ argn; |
| 222 |
} |
| 223 |
if ( argn < argc ) |
| 224 |
{ |
| 225 |
ifp = pm_openr( argv[argn] ); /* open the picture */ |
| 226 |
++argn; |
| 227 |
} |
| 228 |
else |
| 229 |
ifp = stdin; /* or reading from STDIN */ |
| 230 |
if ( argn != argc ) |
| 231 |
pm_usage( usage ); |
| 232 |
|
| 233 |
pgm_readpgminit( ifp, &cols, &rows, &maxval, &format ); /* read picture */ |
| 234 |
pgm_writepgminit( stdout, cols, rows, maxval, 0 ); /* write the header */ |
| 235 |
grayorig = pgm_allocrow(cols); |
| 236 |
grayrow = pgm_allocrow( cols ); /* allocate a row */ |
| 237 |
|
| 238 |
if (midcol == 0) midcol = cols/2; /* if no axis set take the center */ |
| 239 |
if (left ) l_div = (float)(PID2*pixsize)/(cfac*fac); |
| 240 |
else l_div=1.0; /* weighting the left side */ |
| 241 |
if (right) r_div = (float)(PID2*pixsize)/(cfac*fac); |
| 242 |
else r_div=1.0; /* weighting the right side */ |
| 243 |
|
| 244 |
if (verb) |
| 245 |
{ |
| 246 |
pm_message("%s",version); |
| 247 |
pm_message("Calculating a portable graymap with %i rows and %i cols",rows,cols); |
| 248 |
pm_message(" resuming a pixelsize of %f mm",pixsize); |
| 249 |
if ( !right ) pm_message(" only the left side!"); |
| 250 |
if ( !left ) pm_message(" only the right side!"); |
| 251 |
pm_message(" axis = %i, stretching factor = %f",midcol,cfac*fac); |
| 252 |
if ( left ) pm_message(" left side weighting = %f",l_div); |
| 253 |
if ( right ) pm_message(" right side weighting = %f",r_div); |
| 254 |
} |
| 255 |
|
| 256 |
/* allocating the memory for the arrays aldl and ardl */ |
| 257 |
aldl = calloc ( midcol*midcol, sizeof(double)); |
| 258 |
if( !aldl ) |
| 259 |
pm_error( "out of memory" ); |
| 260 |
ardl = calloc ( (cols-midcol)*(cols-midcol), sizeof(double)); |
| 261 |
if( !ardl ) |
| 262 |
pm_error( "out of memory" ); |
| 263 |
|
| 264 |
MALLOCARRAY(trow, cols); |
| 265 |
if( !trow ) |
| 266 |
pm_error( "out of memory" ); |
| 267 |
|
| 268 |
/* now precalculating the weighting-factors for the abel-transformation */ |
| 269 |
for (col = 0; col < midcol; ++col) /* factors for left side */ |
| 270 |
{ |
| 271 |
for (tc = 0; tc < midcol; ++tc) aldl[col*midcol+tc] = dr(col,tc+0.5,midcol); |
| 272 |
} |
| 273 |
for (col = 0; col < (cols-midcol); ++col) /* factors for right side */ |
| 274 |
{ |
| 275 |
for (tc = 0; tc < (cols-midcol); ++tc) |
| 276 |
ardl[col*(cols-midcol)+tc] = dr(col,tc+0.5,cols-midcol); |
| 277 |
} |
| 278 |
|
| 279 |
/* abel-transformation for each row split into right and left side */ |
| 280 |
for ( row = 0; row < rows ; ++row ) |
| 281 |
{ |
| 282 |
pgm_readpgmrow( ifp, grayorig, cols, maxval, format ); |
| 283 |
for ( col = 0; col < midcol; ++col) /* left side */ |
| 284 |
{ |
| 285 |
trow[col] = (float) (grayorig[col]); |
| 286 |
} |
| 287 |
if (left ) abel(trow, midcol, aldl); /* deconvolution */ |
| 288 |
for ( col = 0; col < midcol; ++col) /* writing left side */ |
| 289 |
{ |
| 290 |
temp = (int)(trow[col]/l_div); |
| 291 |
grayrow[col] = (temp>0?temp:0); |
| 292 |
} |
| 293 |
for ( col = midcol; col < cols; ++col ) /* right side */ |
| 294 |
{ |
| 295 |
trow[cols-col-1] = (float) (grayorig[col]); |
| 296 |
} |
| 297 |
if ( right ) abel(trow,(cols-midcol),ardl); /* deconvolution */ |
| 298 |
for ( col = midcol; col < cols; ++col) /* writing right side */ |
| 299 |
{ |
| 300 |
temp = (int)(trow[cols-col-1]/r_div); |
| 301 |
temp = (temp>0?temp:0); |
| 302 |
grayrow[col] = temp; |
| 303 |
} |
| 304 |
pgm_writepgmrow( stdout, grayrow, cols, maxval, 0 ); /* saving row */ |
| 305 |
} |
| 306 |
pm_close( ifp ); |
| 307 |
pm_close( stdout ); /* closing output */ |
| 308 |
free( trow ); /* deconvolution is done, clear memory */ |
| 309 |
pgm_freerow( grayorig ); |
| 310 |
pgm_freerow( grayrow ); |
| 311 |
free(aldl); |
| 312 |
free(ardl); /* all used memory freed (i hope) */ |
| 313 |
exit( 0 ); /* end of procedure */ |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|