1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/* @(#) Function to perform lighting correction.
* @(#) One band IM_BANDFMT_UCHAR images only. Always writes UCHAR.
* @(#)
* @(#) Function im_litecor() assumes that imin
* @(#) is either memory mapped or in a buffer.
* @(#)
* @(#) int im_litecor(in, w, out, clip, factor)
* @(#) IMAGE *in, *w, *out;
* @(#) int clip;
* @(#) double factor;
* @(#)
* @(#) clip==1
* @(#) - Compute max(white)*factor*(image/white), Clip to 255.
* @(#) clip==0
* @(#) - Compute factor for you.
* @(#)
* @(#)
* @(#)
* @(#)
* @(#) Returns 0 on success and -1 on error
*
* Copyright: 1990, J. Cupitt, 1991 N. Dessipris
*
* Author: J. Cupitt, N. Dessipris
* Written on: 02/08/1990
* Modified on : 6/11/1991, by ND to produce a UCHAR output
* 1/4/93 J.Cupitt
* - bugs if white is smaller than image fixed
* - im_warning() now called
* - clip==0 case not tested or changed! do not use!
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <vips/vips.h>
/* If maximum output is > 255 scale output between minout and maxout,
* by normalising maxout to 255.
* If maximum output is < 255 do the light correction without scaling
*/
static int
im_litecor0( IMAGE *in, IMAGE *white, IMAGE *out )
{ PEL *p, *w;
PEL *q, *bu;
int c;
int x, y;
float xrat = (float) in->Xsize / white->Xsize;
float yrat = (float) in->Ysize / white->Ysize;
int xstep = (int) xrat;
int ystep = (int) yrat;
double max;
int wtmp, maxw, maxout, temp;
/* Check white is some simple multiple of image.
*/
if( xrat < 1.0 || xrat != xstep || yrat < 1.0 || yrat != ystep ) {
im_error( "im_litecor", "white not simple scale of image" );
return( -1 );
}
/* Find the maximum of the white.
*/
if( im_max( white, &max ) )
return( -1 );
maxw = (int)max;
/* Set up the output header.
*/
if( im_cp_desc( out, in ) )
return( -1 );
if( im_setupout( out ) )
return( -1 );
/* Make buffer for outputting to.
*/
if( !(bu = (PEL *) im_malloc( out, out->Xsize )) )
return( -1 );
/* Find largest value we might generate if factor == 1.0
*/
maxout = -1;
p = (PEL *) in->data;
for( y = 0; y < in->Ysize; y++ ) {
/* Point w to the start of the line in the white
* corresponding to the line we are about to correct. c counts
* up to xstep; each time it wraps, we should move w on one.
*/
w = (PEL *) (white->data + white->Xsize * (int)(y/ystep));
c = 0;
/* Scan along line.
*/
for( x = 0; x < out->Xsize; x++ ) {
wtmp = (int)*w;
temp = ( maxw * (int) *p++ + (wtmp>>1) ) / wtmp;
if (temp > maxout )
maxout = temp;
/* Move white pointer on if necessary. */
c++;
if( c == xstep ) {
w++;
c = 0;
}
}
}
/* Do exactly the same as above by scaling the result with respect to
* maxout
*/
p = (PEL *) in->data;
if (maxout <= 255 ) /* no need for rescaling output */
{
for( y = 0; y < in->Ysize; y++ )
{
q = bu;
w = (PEL *) (white->data +
white->Xsize * (int)(y/ystep));
c = 0;
/* Scan along line. */
for( x = 0; x < in->Xsize; x++ )
{
wtmp = (int)*w;
*q++ = (PEL)
( ( maxw * (int) *p++ + (wtmp>>1) ) / wtmp );
/* Move white pointer on if necessary.
*/
c++;
if( c == xstep ) { w++; c = 0; }
}
if( im_writeline( y, out, bu ) )
{
im_error("im_litecor", "im_writeline failed");
return( -1 );
}
}
}
else /* rescale output wrt maxout */
{
for( y = 0; y < in->Ysize; y++ )
{
q = bu;
w = (PEL *) (white->data +
white->Xsize * (int)(y/ystep));
c = 0;
/* Scan along line. */
for( x = 0; x < in->Xsize; x++ )
{
wtmp = maxout * ((int)*w);
*q++ = (PEL)
( ( maxw * (int) *p++ * 255 + (wtmp>>1)) / wtmp );
/* Move white pointer on if necessary.
*/
c++;
if( c == xstep ) { w++; c = 0; }
}
if( im_writeline( y, out, bu ) )
{
im_error("im_litecor", "im_writeline failed");
return( -1 );
}
}
}
return( 0 );
}
/* Clip all corrected values above 255, if any.
*/
static int
im_litecor1( IMAGE *in, IMAGE *white, IMAGE *out, double factor )
{ PEL *p, *w;
PEL *q, *bu;
int c;
int x, y;
float xrat = (float) in->Xsize / white->Xsize;
float yrat = (float) in->Ysize / white->Ysize;
int xstep = (int) xrat;
int ystep = (int) yrat;
double max;
double maxw, temp;
int nclipped = 0;
/* Check white is some simple multiple of image.
*/
if( xrat < 1.0 || xrat != xstep || yrat < 1.0 || yrat != ystep ) {
im_error( "im_litecor", "white not simple scale of image" );
return( -1 );
}
/* Find the maximum of the white.
*/
if( im_max( white, &max ) )
return( -1 );
maxw = max;
/* Set up the output header.
*/
if( im_cp_desc( out, in ) )
return( -1 );
if( im_setupout( out ) )
return( -1 );
/* Make buffer we write to.
*/
if( !(bu = (PEL *) im_malloc( out, out->Xsize )) )
return( -1 );
/* Loop through sorting max output
*/
p = (PEL *) in->data;
for( y = 0; y < in->Ysize; y++ ) {
q = bu;
w = (PEL *) (white->data + white->Xsize * (int)(y / ystep));
c = 0;
for( x = 0; x < out->Xsize; x++ ) {
temp = ((factor * maxw * (int) *p++)/((int) *w)) + 0.5;
if( temp > 255.0 ) {
temp = 255;
nclipped++;
}
*q++ = temp;
/* Move white pointer on if necessary.
*/
c++;
if( c == xstep ) {
w++;
c = 0;
}
}
if( im_writeline( y, out, bu ) )
return( -1 );
}
if( nclipped )
im_warn( "im_litecor", "%d pels over 255 clipped", nclipped );
return( 0 );
}
/* Lighting correction. One band uchar images only.
* Assumes the white is some simple multiple of the image in size; ie. the
* white has been taken with some smaller or equal set of resolution
* parameters.
*/
int
im_litecor( IMAGE *in, IMAGE *white, IMAGE *out, int clip, double factor )
{ /* Check our args.
*/
if( im_iocheck( in, out ) )
return( -1 );
if( in->Bands != 1 ||
in->Coding != IM_CODING_NONE || in->BandFmt != IM_BANDFMT_UCHAR ) {
im_error( "im_litecor", "bad input format" );
return( -1 );
}
if( white->Bands != 1 ||
white->Coding != IM_CODING_NONE || white->BandFmt != IM_BANDFMT_UCHAR ) {
im_error( "im_litecor", "bad white format" );
return( -1 );
}
switch( clip ) {
case 1:
return( im_litecor1( in, white, out, factor ) );
case 0:
return( im_litecor0( in, white, out ) );
default:
im_error( "im_litecor", "unknown flag %d", clip );
return( -1 );
}
}
|