libvips/libvips/create/invertlut.c

379 lines
8.8 KiB
C
Raw Permalink Normal View History

2013-09-04 15:58:07 +02:00
/* invert a lut
*
* Written on: 5/6/01
* Modified on :
2013-09-04 15:58:07 +02:00
*
* 7/7/03 JC
* - generate image rather than doublemask (arrg)
* 23/3/10
* - gtkdoc
* 23/5/13
* - fix 1 high input matrices
* - fix file output
* 4/9/13
* - convert to a class
*/
/*
This file is part of VIPS.
2013-09-04 15:58:07 +02:00
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 <glib/gi18n-lib.h>
2013-09-04 15:58:07 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include "pcreate.h"
/*
#define DEBUG
*/
/* Our state.
*/
typedef struct _VipsInvertlut {
VipsCreate parent_instance;
/* Input image.
*/
VipsImage *in;
2013-09-04 15:58:07 +02:00
/* .. and cast to a matrix.
*/
VipsImage *mat;
2013-09-05 10:11:23 +02:00
int size; /* Number of output elements to generate */
2013-09-04 15:58:07 +02:00
double **data; /* Rows of unpacked matrix */
double *buf; /* Output buffer */
} VipsInvertlut;
typedef VipsCreateClass VipsInvertlutClass;
G_DEFINE_TYPE( VipsInvertlut, vips_invertlut, VIPS_TYPE_CREATE );
static void
vips_invertlut_dispose( GObject *gobject )
{
2013-09-05 10:11:23 +02:00
VipsInvertlut *lut = (VipsInvertlut *) gobject;
2013-09-04 15:58:07 +02:00
2013-09-05 10:11:23 +02:00
VIPS_FREE( lut->data );
2013-09-04 15:58:07 +02:00
VIPS_FREE( lut->buf );
VIPS_UNREF( lut->mat );
G_OBJECT_CLASS( vips_invertlut_parent_class )->dispose( gobject );
}
/* Use this to sort our input rows by the first column.
*/
static int
compare( const void *a, const void *b )
{
double **r1 = (double **) a;
double **r2 = (double **) b;
double diff = r1[0][0] - r2[0][0];
if( diff > 0 )
return( 1 );
else if( diff == 0 )
return( 0 );
else
return( -1 );
}
static int
vips_invertlut_build_init( VipsInvertlut *lut )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( lut );
2013-09-05 10:11:23 +02:00
int x, y;
2013-09-04 15:58:07 +02:00
2013-09-05 10:11:23 +02:00
if( !lut->mat ||
lut->mat->Xsize < 2 ||
2013-09-05 10:11:23 +02:00
lut->mat->Ysize < 1 ) {
vips_error( class->nickname, "%s", _( "bad input matrix" ) );
2013-09-04 15:58:07 +02:00
return( -1 );
2013-09-05 10:11:23 +02:00
}
if( lut->size < 1 ||
2013-09-05 10:11:23 +02:00
lut->size > 65536 ) {
vips_error( class->nickname, "%s", _( "bad size" ) );
2013-09-04 15:58:07 +02:00
return( -1 );
2013-09-05 10:11:23 +02:00
}
2013-09-04 15:58:07 +02:00
if( !(lut->buf =
2013-09-17 22:54:28 +02:00
VIPS_ARRAY( NULL, lut->size * (lut->mat->Xsize - 1), double )) )
2013-09-05 10:11:23 +02:00
return( -1 );
2013-09-04 15:58:07 +02:00
2013-09-05 10:11:23 +02:00
if( !(lut->data = VIPS_ARRAY( NULL, lut->mat->Ysize, double * )) )
return( -1 );
for( y = 0; y < lut->mat->Ysize; y++ )
2013-09-05 10:11:23 +02:00
lut->data[y] = VIPS_MATRIX( lut->mat, 0, y );
2013-09-04 15:58:07 +02:00
/* Sanity check for data range.
*/
for( y = 0; y < lut->mat->Ysize; y++ )
for( x = 0; x < lut->mat->Xsize; x++ )
if( lut->data[y][x] > 1.0 ||
2013-09-05 10:11:23 +02:00
lut->data[y][x] < 0.0 ) {
vips_error( class->nickname,
2013-09-04 15:58:07 +02:00
_( "element (%d, %d) is %g, "
"outside range [0,1]" ),
2013-09-05 10:11:23 +02:00
x, y, lut->data[y][x] );
2013-09-04 15:58:07 +02:00
return( -1 );
}
/* Sort by 1st column in input.
*/
2013-09-05 10:11:23 +02:00
qsort( lut->data, lut->mat->Ysize, sizeof( double * ), compare );
2013-09-04 15:58:07 +02:00
#ifdef DEBUG
printf( "Input table, sorted by 1st column\n" );
2013-09-05 10:11:23 +02:00
for( y = 0; y < lut->mat->Ysize; y++ ) {
2013-09-04 15:58:07 +02:00
printf( "%.4d ", y );
2013-09-05 10:11:23 +02:00
for( x = 0; x < lut->mat->Xsize; x++ )
printf( "%.9f ", lut->data[y][x] );
2013-09-04 15:58:07 +02:00
printf( "\n" );
}
#endif /*DEBUG*/
return( 0 );
}
static int
2013-09-05 10:11:23 +02:00
vips_invertlut_build_create( VipsInvertlut *lut )
2013-09-04 15:58:07 +02:00
{
2013-09-05 10:11:23 +02:00
int bands = lut->mat->Xsize - 1;
int height = lut->mat->Ysize;
2013-09-04 15:58:07 +02:00
int b;
/* Do each output channel separately.
*/
for( b = 0; b < bands; b++ ) {
/* The first and last lut positions we know real values for.
*/
2013-09-05 10:11:23 +02:00
int first = lut->data[0][b + 1] * (lut->size - 1);
int last = lut->data[height - 1][b + 1] * (lut->size - 1);
2013-09-04 15:58:07 +02:00
int k;
/* Extrapolate bottom and top segments to (0,0) and (1,1).
*/
for( k = 0; k < first; k++ ) {
/* Have this inside the loop to avoid /0 errors if
* first == 0.
*/
2013-09-05 10:11:23 +02:00
double fac = lut->data[0][0] / first;
2013-09-04 15:58:07 +02:00
2013-09-05 10:11:23 +02:00
lut->buf[b + k * bands] = k * fac;
2013-09-04 15:58:07 +02:00
}
2013-09-05 10:11:23 +02:00
for( k = last; k < lut->size; k++ ) {
2013-09-04 15:58:07 +02:00
/* Inside the loop to avoid /0 errors for last ==
2013-09-05 10:11:23 +02:00
* (size - 1).
2013-09-04 15:58:07 +02:00
*/
double fac = (1 - lut->data[height - 1][0]) /
2013-09-05 10:11:23 +02:00
((lut->size - 1) - last);
2013-09-04 15:58:07 +02:00
lut->buf[b + k * bands] =
2013-09-05 10:11:23 +02:00
lut->data[height - 1][0] + (k - last) * fac;
2013-09-04 15:58:07 +02:00
}
/* Interpolate the data sections.
*/
for( k = first; k < last; k++ ) {
/* Where we're at in the [0,1] range.
*/
2013-09-05 10:11:23 +02:00
double ki = (double) k / (lut->size - 1);
2013-09-04 15:58:07 +02:00
double irange, orange;
int j;
/* Search for the lowest real value < ki. There may
* not be one: if not, just use 0. Tiny error.
*/
2013-09-05 10:11:23 +02:00
for( j = height - 1; j >= 0; j-- )
if( lut->data[j][b + 1] < ki )
2013-09-04 15:58:07 +02:00
break;
if( j == -1 )
j = 0;
/* Interpolate k as being between row data[j] and row
* data[j + 1].
*/
2013-09-05 10:11:23 +02:00
irange = lut->data[j + 1][b + 1] - lut->data[j][b + 1];
orange = lut->data[j + 1][0] - lut->data[j][0];
2013-09-04 15:58:07 +02:00
2013-09-05 10:11:23 +02:00
lut->buf[b + k * bands] = lut->data[j][0] +
orange * ((ki - lut->data[j][b + 1]) / irange);
2013-09-04 15:58:07 +02:00
}
}
return( 0 );
}
static int
vips_invertlut_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsCreate *create = VIPS_CREATE( object );
2013-09-05 10:11:23 +02:00
VipsInvertlut *lut = (VipsInvertlut *) object;
2013-09-04 15:58:07 +02:00
if( VIPS_OBJECT_CLASS( vips_invertlut_parent_class )->build( object ) )
return( -1 );
if( vips_check_matrix( class->nickname, lut->in, &lut->mat ) )
return( -1 );
2013-09-04 15:58:07 +02:00
if( vips_invertlut_build_init( lut ) ||
vips_invertlut_build_create( lut ) )
return( -1 );
2013-09-04 15:58:07 +02:00
vips_image_init_fields( create->out,
lut->size, 1, lut->mat->Xsize - 1,
VIPS_FORMAT_DOUBLE, VIPS_CODING_NONE,
2013-09-04 15:58:07 +02:00
VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
if( vips_image_write_line( create->out, 0, (VipsPel *) lut->buf ) )
2013-09-04 15:58:07 +02:00
return( -1 );
return( 0 );
}
static void
2013-09-05 10:11:23 +02:00
vips_invertlut_class_init( VipsInvertlutClass *class )
2013-09-04 15:58:07 +02:00
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
gobject_class->dispose = vips_invertlut_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "invertlut";
vobject_class->description = _( "build an inverted look-up table" );
vobject_class->build = vips_invertlut_build;
VIPS_ARG_IMAGE( class, "in", 0,
_( "Input" ),
2013-09-04 15:58:07 +02:00
_( "Matrix of XY coordinates" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsInvertlut, in ) );
2013-09-05 10:11:23 +02:00
VIPS_ARG_INT( class, "size", 5,
_( "Size" ),
2013-09-05 10:11:23 +02:00
_( "LUT size to generate" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsInvertlut, size ),
1, 1000000, 256 );
2013-09-04 15:58:07 +02:00
}
static void
2013-09-05 10:11:23 +02:00
vips_invertlut_init( VipsInvertlut *lut )
2013-09-04 15:58:07 +02:00
{
lut->size = 256;
2013-09-04 15:58:07 +02:00
}
/**
* vips_invertlut: (method)
2013-09-05 10:11:23 +02:00
* @in: input mask
* @out: (out): output LUT
2013-09-05 10:11:23 +02:00
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @size: generate this much
2013-09-04 15:58:07 +02:00
*
* Given a mask of target values and real values, generate a LUT which
* will map reals to targets. Handy for linearising images from
* measurements of a colour chart. All values in [0,1]. Piecewise linear
* interpolation, extrapolate head and tail to 0 and 1.
*
2013-09-04 15:58:07 +02:00
* Eg. input like this:
*
2013-09-04 15:58:07 +02:00
* <tgroup cols='4' align='left' colsep='1' rowsep='1'>
* <tbody>
* <row>
* <entry>4</entry>
* <entry>3</entry>
* </row>
* <row>
* <entry>0.1</entry>
* <entry>0.2</entry>
* <entry>0.3</entry>
* <entry>0.1</entry>
* </row>
* <row>
* <entry>0.2</entry>
* <entry>0.4</entry>
* <entry>0.4</entry>
* <entry>0.2</entry>
* </row>
* <row>
* <entry>0.7</entry>
* <entry>0.5</entry>
* <entry>0.6</entry>
* <entry>0.3</entry>
* </row>
* </tbody>
* </tgroup>
*
* Means a patch with 10% reflectance produces an image with 20% in
* channel 1, 30% in channel 2, and 10% in channel 3, and so on.
*
2013-09-04 15:58:07 +02:00
* Inputs don't need to be sorted (we do that). Generate any precision
2013-09-05 10:11:23 +02:00
* LUT, default to 256 elements.
2013-09-04 15:58:07 +02:00
*
* It won't work too well for non-monotonic camera responses
* (we should fix this). Interpolation is simple piecewise linear; we ought to
2013-09-04 15:58:07 +02:00
* do something better really.
*
* See also: vips_buildlut().
*
* Returns: 0 on success, -1 on error
*/
int
vips_invertlut( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "invertlut", ap, in, out );
va_end( ap );
return( result );
}