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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*
* This file is part of the XForms library package.
*
* XForms 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.1, or
* (at your option) any later version.
*
* XForms 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 XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file is part of the XForms library package.
* Copyright (c) 1993, 1998-2002 By T.C. Zhao
* All rights reserved.
*
* simple image processing routines
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include/forms.h"
#include "flimage.h"
#include "flimage_int.h"
/********************************************************************
* general pixel transformation
********************************************************************/
int
flimage_transform_pixels( FL_IMAGE * im,
int * red,
int * green,
int * blue )
{
int i,
j;
FL_PCTYPE *r,
*g,
*b;
SubImage *sub;
if ( ! im || im->w <= 0 )
return -1;
flimage_convert( im, FL_IMAGE_RGB, 0 );
flimage_invalidate_pixels( im );
if ( ! ( sub = flimage_get_subimage( im, 1 ) ) )
return -1;
im->total = sub->h;
im->visual_cue( im,"Transforming" );
for ( i = 0; i < sub->h; i++ )
{
r = ( ( FL_PCTYPE ** ) sub->mat[ 0 ] )[ i ];
g = ( ( FL_PCTYPE ** ) sub->mat[ 1 ] )[ i ];
b = ( ( FL_PCTYPE ** ) sub->mat[ 2 ] )[ i ];
if ( ! ( i & FLIMAGE_REPFREQ ) )
{
im->completed = i;
im->visual_cue( im,"Transforming" );
}
for ( j = 0; j < sub->w; j++ )
{
r[ j ] = red[ r[ j ] ];
g[ j ] = green[ g[ j ] ];
b[ j ] = blue[ b[ j ] ];
}
}
im->completed = sub->h;
im->visual_cue( im,"Transforming" );
if ( im->subw )
{
fl_free_matrix( sub->mat[ 0 ] );
fl_free_matrix( sub->mat[ 1 ] );
fl_free_matrix( sub->mat[ 2 ] );
}
im->modified = 1;
return 0;
}
/********************************************************************
* tint.
******************************************************************{*/
int
flimage_tint( FL_IMAGE * im,
unsigned int packed,
double opacity )
{
float r = FL_GETR( packed ) * opacity + 0.001;
float g = FL_GETG( packed ) * opacity + 0.001;
float b = FL_GETB( packed ) * opacity + 0.001;
float trans = 1 - opacity,
tmp;
int i;
if ( ! im || im->w <= 0 )
return -1;
if ( flimage_get_linearlut( im ) < 0 )
return -1;
for ( i = 0; i < im->map_len; i++ )
{
tmp = i * trans;
im->llut[ 0 ][ i ] = tmp + r;
im->llut[ 1 ][ i ] = tmp + g;
im->llut[ 2 ][ i ] = tmp + b;
}
return flimage_transform_pixels( im, im->llut[ 0 ],
im->llut[ 1 ], im->llut[ 2 ]);
}
/* get a subimage of the image. if parameter make is true,
we fake a matrix so processing is done in place
*/
#define MAX_RETBUF 6 /* hack to be limited re-entrent */
SubImage *
flimage_get_subimage( FL_IMAGE * im,
int make )
{
static SubImage subimage[ MAX_RETBUF ];
static int buf;
int err;
SubImage *sub = subimage + buf;
void * ( * submat )( void *, int, int, int, int, int, int, unsigned int );
submat = make ? fl_make_submatrix : fl_get_submatrix;
im->subx = FL_clamp( im->subx, 0, im->w - 1 );
im->suby = FL_clamp( im->suby, 0, im->h - 1 );
sub->comp = FL_IsGray( im->type) ? 1 : 3;
sub->mat[ 0 ] = sub->mat[ 1 ] = sub->mat[ 2 ] = NULL;
if ( im->subw < 0 || im->subh < 0 )
im->subw = im->subh = 0;
if ( im->subx + im->subw > im->w )
im->subw = im->subh = 0;
if ( im->suby + im->subh > im->h )
im->subw = im->subh = 0;
if ( im->subw == 0 )
{
sub->w = im->w;
sub->h = im->h;
if ( sub->comp == 1 )
sub->mat[ 0 ] = im->gray;
else
{
sub->mat[ 0 ] = im->red;
sub->mat[ 1 ] = im->green;
sub->mat[ 2 ] = im->blue;
}
}
else
{
sub->w = im->subw;
sub->h = im->subh;
if ( sub->comp == 3 )
{
err = ! ( sub->mat[ 0 ] = submat( im->red, im->h, im->w,
im->suby, im->subx, im->subh,
im->subw,
sizeof **im->red ) );
err = err || ! ( sub->mat[ 1 ] = submat( im->green, im->h, im->w,
im->suby, im->subx,
im->subh, im->subw,
sizeof **im->red ) );
err = err || ! ( sub->mat[ 2 ] = submat( im->blue, im->h, im->w,
im->suby, im->subx,
im->subh, im->subw,
sizeof **im->blue ) );
}
else
err = ! ( sub->mat[ 0 ] = submat( im->gray, im->h, im->w,
im->suby, im->subx, im->subh,
im->subw,
sizeof **im->gray ) );
if ( err )
{
im->error_message( im, "Failed to get working memory" );
fl_free_matrix( sub->mat[ 0 ]);
fl_free_matrix( sub->mat[ 1 ]);
fl_free_matrix( sub->mat[ 2 ] );
return NULL;
}
}
buf = ( buf + 1 ) % MAX_RETBUF;
return sub;
}
/**********************************************************************
* Simple matrix operations
**********************************************************************/
/* grab a piece of a matrix without copying */
void *
fl_make_submatrix( void * in,
int rows,
int cols,
int r1,
int c1,
int rs,
int cs,
unsigned int esize )
{
int i;
unsigned int offset = c1 * esize;
char **subm;
char **mat = in;
if (r1 < 0 || c1 < 0 || (r1 + rs - 1) >= rows || (c1 + cs - 1) >= cols)
{
M_err("MakeSubMatrix", "Bad arguments");
return 0;
}
if ( mat[ -1 ] != ( char * ) FL_GET_MATRIX
&& mat[ -1 ] != ( char * ) FL_MAKE_MATRIX )
{
M_err( "MakeSubMatrix", "input is not a matrix" );
return NULL;
}
subm = fl_malloc( ( rs + 1 ) * sizeof(void *));
subm[ 0 ] = ( char * ) FL_MAKE_MATRIX;
for ( i = 1; i <= rs; i++ )
subm[ i ] = mat[ r1 + i - 1 ] + offset;
return subm + 1;
}
/* grab a piece of a matrix */
void *
fl_get_submatrix( void * in,
int rows,
int cols,
int r1,
int c1,
int rs,
int cs,
unsigned int esize )
{
int i;
unsigned int offset = c1 * esize,
size = cs * esize;
char **subm;
char **mat = in;
if ( r1 < 0 || c1 < 0 || r1 + rs - 1 >= rows || c1 + cs - 1 >= cols )
{
M_err("GetSubMatrix", "Bad arguments");
return 0;
}
if ( mat[ -1 ] != ( char * ) FL_GET_MATRIX
&& mat[ -1 ] != ( char * ) FL_MAKE_MATRIX )
{
M_err( "GetSubMatrix", "input is not a matrix" );
return NULL;
}
subm = fl_get_matrix( rows, cols, esize );
for ( i = 0; i < rs; i++ )
memcpy( subm[ i ], mat[ r1 + i ] + offset, size );
return subm;
}
/***********************************************************************
* Histogram equalization
***********************************************************************/
static int
get_histogram(FL_IMAGE * im)
{
unsigned int *rhist,
*ghist,
*bhist,
*grhist;
unsigned int size = ( FL_PCMAX + 3 ) * sizeof **im->hist;
int g,
n;
if ( ! im->hist[ 0 ] )
{
im->hist[ 0 ] = fl_malloc( size );
im->hist[ 1 ] = fl_malloc( size );
im->hist[ 2 ] = fl_malloc( size );
im->hist[ 3 ] = fl_malloc( size );
}
memset( rhist = im->hist[ 0 ], 0, size );
memset( ghist = im->hist[ 1 ], 0, size );
memset( bhist = im->hist[ 2 ], 0, size );
memset( grhist = im->hist[ 3 ], 0, size );
if ( im->type == FL_IMAGE_RGB )
for ( n = im->w * im->h; --n >= 0; )
{
if ( ++rhist[ im->red[ 0 ][ n ] ] == 0 )
rhist[ im->red[ 0 ][ n ] ]--;
if ( ++ghist[ im->green[ 0 ][ n ] ] == 0 )
ghist[ im->green[ 0 ][ n ] ]--;
if ( ++bhist[ im->blue[ 0 ][ n ] ] == 0 )
bhist[ im->blue[ 0 ][ n ] ]--;
g = FL_RGB2GRAY( im->red[ 0 ][ n ],
im->green[ 0 ][ n ],
im->blue[ 0 ][ n ] );
if ( ++grhist[ g ] == 0 )
grhist[ g ]--;
}
else if ( im->type == FL_IMAGE_GRAY )
for ( n = im->w * im->h; --n >= 0; )
{
if ( ++grhist[ im->gray[ 0 ][ n ] ] == 0 )
grhist[ im->gray[ 0 ][ n ] ]--;
}
else if ( im->type == FL_IMAGE_CI )
{
unsigned short *ci = im->ci[ 0 ];
for ( ci = im->ci[ 0 ] + im->w * im->h; --ci >= im->ci[ 0 ]; )
{
if ( ++rhist[ im->red_lut[ *ci ] ] )
rhist[ im->red_lut[ *ci ] ]--;
if ( ++ghist[ im->green_lut[ *ci ] ] )
ghist[ im->green_lut[ *ci ] ]--;
if ( ++bhist[ im->blue_lut[ *ci ] ] )
bhist[ im->blue_lut[ *ci ] ]--;
g = FL_RGB2GRAY( im->red_lut[ *ci ],
im->green_lut[ *ci ],
im->blue_lut[ *ci ] );
if ( ++grhist[ g ] == 0 )
grhist[ g ]--;
}
}
else
{
im->error_message( im, "histogram: unhandled" );
return -1;
}
return 0;
}
int
flimage_enhance( FL_IMAGE * im,
int delta FL_UNUSED_ARG )
{
long sum[ FL_PCMAX + 2 ];
int i,
n;
if (im->type == FL_IMAGE_CI)
flimage_convert(im, FL_IMAGE_RGB, 0);
else if (im->type == FL_IMAGE_MONO)
flimage_convert(im, FL_IMAGE_GRAY, 0);
get_histogram( im );
memset( sum, 0, sizeof sum );
sum[ 0 ] = im->hist[ 3 ][ 0 ];
for ( i = 1; i <= FL_PCMAX; i++ )
sum[ i ] = sum[ i - 1 ] + im->hist[ 3 ][ i ];
for ( i = 0; i <= FL_PCMAX; i++ )
sum[ i ] *= ( FL_PCMAX - 0.999f ) / ( im->w * im->h );
if (im->type == FL_IMAGE_RGB)
for (n = im->w * im->h; --n >= 0;)
{
im->red[0][n] = sum[im->red[0][n]];
im->green[0][n] = sum[im->green[0][n]];
im->blue[0][n] = sum[im->blue[0][n]];
}
else if (im->type == FL_IMAGE_GRAY)
for (n = im->w * im->h; --n >= 0;)
im->gray[0][n] = sum[im->gray[0][n]];
else
fprintf(stderr, "image_enhance: unhandled");
im->modified = 1;
return 0;
}
int
flimage_get_closest_color_from_map( FL_IMAGE * im,
unsigned int col )
{
int r = FL_GETR( col ),
g = FL_GETG( col ),
b = FL_GETB( col );
int mindiff = 0x7fffffff,
diff;
int dr,
dg,
db,
k,
i;
for ( k = i = 0; i < im->map_len; i++ )
{
dr = r - im->red_lut[ i ];
dg = g - im->green_lut[ i ];
db = b - im->blue_lut[ i ];
diff = 3 * dr * dr + 4 * dg * dg + 2 * db * db;
if (mindiff > diff)
{
mindiff = diff;
k = i;
}
}
return k;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|