redo im_invertlut() as a class

This commit is contained in:
John Cupitt 2013-09-05 09:11:23 +01:00
parent 222a7abdbf
commit 7a5e04ba00
11 changed files with 106 additions and 468 deletions

View File

@ -4,7 +4,7 @@
INTERPRETATION_MATRIX etc.
- rewrite im_buildlut(), im_identity*(), im_maplut(), im_falsecolour(),
im_gammacorrect(), im_histgr(), im_histcum(), im_histnorm(), im_heq(),
im_histnD(), im_histindexed(), im_histspec() as classes
im_histnD(), im_histindexed(), im_histspec(), im_invertlut() as classes
- thin vips8 wrapper for im_histplot()
- added vips_error_freeze() / vips_error_thaw()
- used freeze() / thaw() to stop file format sniffers logging spurious errors

14
TODO
View File

@ -1,20 +1,6 @@
- VipsHistogram takes many inputs, all of them hists and includes a
buffer-processing class member
VipsHistUnary adds a single input
VipsHistNorm subclasses that
- im_histspec() is a binary hist operator ... need many inputs for
VipsHistogram and VipsHistBuffer
- use g_log() instead of vips_info()
- what's the difference between buildlut and invertlut? can we make invertlut
by just swapping the channels before calling buildlut?
- do we always call copy_fields and demand_hint with ALL input images? what
about the operators in conversion?

View File

@ -4,6 +4,7 @@ libcreate_la_SOURCES = \
create.c \
pcreate.h \
buildlut.c \
invertlut.c \
identity.c \
point.c \
point.h \

View File

@ -121,6 +121,7 @@ vips_create_operation_init( void )
extern GType vips_zone_get_type( void );
extern GType vips_sines_get_type( void );
extern GType vips_buildlut_get_type( void );
extern GType vips_invertlut_get_type( void );
extern GType vips_identity_get_type( void );
vips_black_get_type();
@ -134,6 +135,7 @@ vips_create_operation_init( void )
vips_zone_get_type();
vips_sines_get_type();
vips_buildlut_get_type();
vips_invertlut_get_type();
vips_identity_get_type();
}

View File

@ -71,7 +71,7 @@ typedef struct _VipsInvertlut {
*/
VipsImage *mat;
int lut_size; /* Number of output elements to generate */
int size; /* Number of output elements to generate */
double **data; /* Rows of unpacked matrix */
double *buf; /* Output buffer */
@ -84,17 +84,9 @@ G_DEFINE_TYPE( VipsInvertlut, vips_invertlut, VIPS_TYPE_CREATE );
static void
vips_invertlut_dispose( GObject *gobject )
{
VipsBuildlut *lut = (VipsBuildlut *) gobject;
if( lut->data ) {
int i;
for( i = 0; i < lut->input->ysize; i++ )
VIPS_FREE( lut->data[i] );
VIPS_FREE( lut->data );
}
VipsInvertlut *lut = (VipsInvertlut *) gobject;
VIPS_FREE( lut->data );
VIPS_FREE( lut->buf );
VIPS_UNREF( lut->mat );
@ -123,59 +115,55 @@ static int
vips_invertlut_build_init( VipsInvertlut *lut )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( lut );
VipsCreate *create = VIPS_CREATE( lut );
/* Fill our state.
*/
static int
build_state( State *state, DOUBLEMASK *input, IMAGE *output, int lut_size )
{
int x, y, i;
int x, y;
state->input = input;
state->output = output;
state->lut_size = lut_size;
state->data = NULL;
if( !lut->mat ||
lut->mat->Xsize < 2 ||
lut->mat->Ysize < 1 ) {
vips_error( class->nickname, "%s", _( "bad input matrix" ) );
return( -1 );
}
if( lut->size < 1 ||
lut->size > 65536 ) {
vips_error( class->nickname, "%s", _( "bad size" ) );
return( -1 );
}
if( !(state->buf = im_malloc( NULL, IM_IMAGE_SIZEOF_LINE( output ) )) )
if( !(lut->buf =
vips_malloc( NULL, VIPS_IMAGE_SIZEOF_LINE( create->out ) )) )
return( -1 );
if( !(state->data = IM_ARRAY( NULL, input->ysize, double * )) )
if( !(lut->data = VIPS_ARRAY( NULL, lut->mat->Ysize, double * )) )
return( -1 );
for( y = 0; y < input->ysize; y++ )
state->data[y] = NULL;
for( y = 0; y < input->ysize; y++ )
if( !(state->data[y] = IM_ARRAY( NULL, input->xsize, double )) )
return( -1 );
for( i = 0, y = 0; y < input->ysize; y++ )
for( x = 0; x < input->xsize; x++, i++ )
state->data[y][x] = input->coeff[i];
for( y = 0; y < lut->mat->Ysize; y++ )
lut->data[y] = VIPS_MATRIX( lut->mat, 0, y );
/* Sanity check for data range.
*/
for( y = 0; y < input->ysize; y++ )
for( x = 0; x < input->xsize; x++ )
if( state->data[y][x] > 1.0 ||
state->data[y][x] < 0.0 ) {
im_error( "im_invertlut",
for( y = 0; y < lut->mat->Ysize; y++ )
for( x = 0; x < lut->mat->Xsize; x++ )
if( lut->data[y][x] > 1.0 ||
lut->data[y][x] < 0.0 ) {
vips_error( class->nickname,
_( "element (%d, %d) is %g, "
"outside range [0,1]" ),
x, y, state->data[y][x] );
x, y, lut->data[y][x] );
return( -1 );
}
/* Sort by 1st column in input.
*/
qsort( state->data, input->ysize, sizeof( double * ), compare );
qsort( lut->data, lut->mat->Ysize, sizeof( double * ), compare );
#ifdef DEBUG
printf( "Input table, sorted by 1st column\n" );
for( y = 0; y < input->ysize; y++ ) {
for( y = 0; y < lut->mat->Ysize; y++ ) {
printf( "%.4d ", y );
for( x = 0; x < input->xsize; x++ )
printf( "%.9f ", state->data[y][x] );
for( x = 0; x < lut->mat->Xsize; x++ )
printf( "%.9f ", lut->data[y][x] );
printf( "\n" );
}
@ -185,15 +173,10 @@ build_state( State *state, DOUBLEMASK *input, IMAGE *output, int lut_size )
}
static int
vips_invertlut_build_create( VipsBuildlut *lut )
vips_invertlut_build_create( VipsInvertlut *lut )
{
DOUBLEMASK *input = state->input;
int ysize = input->ysize;
int xsize = input->xsize;
double *buf = state->buf;
int bands = xsize - 1;
double **data = state->data;
int lut_size = state->lut_size;
int bands = lut->mat->Xsize - 1;
int height = lut->mat->Ysize;
int b;
@ -202,8 +185,8 @@ vips_invertlut_build_create( VipsBuildlut *lut )
for( b = 0; b < bands; b++ ) {
/* The first and last lut positions we know real values for.
*/
int first = data[0][b + 1] * (lut_size - 1);
int last = data[ysize - 1][b + 1] * (lut_size - 1);
int first = lut->data[0][b + 1] * (lut->size - 1);
int last = lut->data[height - 1][b + 1] * (lut->size - 1);
int k;
@ -213,20 +196,20 @@ vips_invertlut_build_create( VipsBuildlut *lut )
/* Have this inside the loop to avoid /0 errors if
* first == 0.
*/
double fac = data[0][0] / first;
double fac = lut->data[0][0] / first;
buf[b + k * bands] = k * fac;
lut->buf[b + k * bands] = k * fac;
}
for( k = last; k < lut_size; k++ ) {
for( k = last; k < lut->size; k++ ) {
/* Inside the loop to avoid /0 errors for last ==
* (lut_size - 1).
* (size - 1).
*/
double fac = (1 - data[ysize - 1][0]) /
((lut_size - 1) - last);
double fac = (1 - lut->data[height - 1][0]) /
((lut->size - 1) - last);
buf[b + k * bands] =
data[ysize - 1][0] + (k - last) * fac;
lut->buf[b + k * bands] =
lut->data[height - 1][0] + (k - last) * fac;
}
/* Interpolate the data sections.
@ -234,7 +217,7 @@ vips_invertlut_build_create( VipsBuildlut *lut )
for( k = first; k < last; k++ ) {
/* Where we're at in the [0,1] range.
*/
double ki = (double) k / (lut_size - 1);
double ki = (double) k / (lut->size - 1);
double irange, orange;
int j;
@ -242,8 +225,8 @@ vips_invertlut_build_create( VipsBuildlut *lut )
/* Search for the lowest real value < ki. There may
* not be one: if not, just use 0. Tiny error.
*/
for( j = ysize - 1; j >= 0; j-- )
if( data[j][b + 1] < ki )
for( j = height - 1; j >= 0; j-- )
if( lut->data[j][b + 1] < ki )
break;
if( j == -1 )
j = 0;
@ -251,11 +234,11 @@ vips_invertlut_build_create( VipsBuildlut *lut )
/* Interpolate k as being between row data[j] and row
* data[j + 1].
*/
irange = data[j + 1][b + 1] - data[j][b + 1];
orange = data[j + 1][0] - data[j][0];
irange = lut->data[j + 1][b + 1] - lut->data[j][b + 1];
orange = lut->data[j + 1][0] - lut->data[j][0];
buf[b + k * bands] = data[j][0] +
orange * ((ki - data[j][b + 1]) / irange);
lut->buf[b + k * bands] = lut->data[j][0] +
orange * ((ki - lut->data[j][b + 1]) / irange);
}
}
@ -267,7 +250,7 @@ vips_invertlut_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsCreate *create = VIPS_CREATE( object );
VipsBuildlut *lut = (VipsBuildlut *) object;
VipsInvertlut *lut = (VipsInvertlut *) object;
if( VIPS_OBJECT_CLASS( vips_invertlut_parent_class )->build( object ) )
return( -1 );
@ -280,7 +263,7 @@ vips_invertlut_build( VipsObject *object )
return( -1 );
vips_image_init_fields( create->out,
lut->lut_size, 1, lut->mat->Xsize - 1,
lut->size, 1, lut->mat->Xsize - 1,
VIPS_FORMAT_DOUBLE, VIPS_CODING_NONE,
VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
if( vips_image_write_line( create->out, 0, (VipsPel *) lut->buf ) )
@ -290,7 +273,7 @@ vips_invertlut_build( VipsObject *object )
}
static void
vips_invertlut_class_init( VipsBuildlutClass *class )
vips_invertlut_class_init( VipsInvertlutClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
@ -307,20 +290,32 @@ vips_invertlut_class_init( VipsBuildlutClass *class )
_( "Input" ),
_( "Matrix of XY coordinates" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBuildlut, in ) );
G_STRUCT_OFFSET( VipsInvertlut, in ) );
VIPS_ARG_INT( class, "size", 5,
_( "Size" ),
_( "LUT size to generate" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsInvertlut, size ),
1, 1000000, 256 );
}
static void
vips_invertlut_init( VipsBuildlut *lut )
vips_invertlut_init( VipsInvertlut *lut )
{
lut->size = 256;
}
/**
* im_invertlut:
* @input: input mask
* @output: output LUT
* @lut_size: generate this much
* vips_invertlut:
* @in: input mask
* @out: output LUT
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @size: generate this much
*
* Given a mask of target values and real values, generate a LUT which
* will map reals to targets. Handy for linearising images from
@ -360,7 +355,7 @@ vips_invertlut_init( VipsBuildlut *lut )
* channel 1, 30% in channel 2, and 10% in channel 3, and so on.
*
* Inputs don't need to be sorted (we do that). Generate any precision
* LUT, typically you might ask for 256 elements.
* LUT, default to 256 elements.
*
* It won't work too well for non-monotonic camera responses
* (we should fix this). Interpolation is simple piecewise linear; we ought to
@ -382,44 +377,3 @@ vips_invertlut( VipsImage *in, VipsImage **out, ... )
return( result );
}
int
im_invertlut( DOUBLEMASK *input, IMAGE *output, int lut_size )
{
State state;
if( !input ||
input->xsize < 2 ||
input->ysize < 1 ) {
im_error( "im_invertlut", "%s", _( "bad input matrix" ) );
return( -1 );
}
if( lut_size < 1 ||
lut_size > 65536 ) {
im_error( "im_invertlut", "%s", _( "bad lut_size" ) );
return( -1 );
}
im_initdesc( output,
lut_size, 1, input->xsize - 1,
IM_BBITS_DOUBLE, IM_BANDFMT_DOUBLE,
IM_CODING_NONE, IM_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
if( im_setupout( output ) )
return( -1 );
if( build_state( &state, input, output, lut_size ) ||
invertlut( &state ) ||
im_writeline( 0, output, (VipsPel *) state.buf ) ) {
free_state( &state );
return( -1 );
}
free_state( &state );
return( 0 );
}

View File

@ -2550,6 +2550,31 @@ im_buildlut( DOUBLEMASK *input, VipsImage *out )
return( 0 );
}
int
im_invertlut( DOUBLEMASK *input, VipsImage *out, int size )
{
VipsImage *mat;
VipsImage *x;
mat = vips_image_new();
if( im_mask2vips( input, mat ) )
return( -1 );
if( vips_invertlut( mat, &x,
"size", size,
NULL ) ) {
g_object_unref( mat );
return( -1 );
}
g_object_unref( mat );
if( im_copy( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_rightshift_size( IMAGE *in, IMAGE *out,
int xshift, int yshift, int band_fmt )
@ -3492,7 +3517,6 @@ int
im_histspec( IMAGE *in, IMAGE *ref, IMAGE *out )
{
IMAGE *t[5];
VipsImage *x;
guint64 px;
int fmt;

View File

@ -14,7 +14,6 @@ libhistogram_la_SOURCES = \
\
hist_dispatch.c \
im_mpercent.c \
im_invertlut.c \
im_lhisteq.c \
im_project.c \
im_stdif.c \

View File

@ -1,330 +0,0 @@
/* invert a lut
*
* Written on: 5/6/01
* Modified on :
*
* 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
*/
/*
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 <math.h>
#include <vips/vips.h>
/*
#define DEBUG
*/
/* Our state.
*/
typedef struct {
DOUBLEMASK *input; /* Input mask */
IMAGE *output; /* Output lut */
int lut_size; /* Number of output elements to generate */
double **data; /* Rows of unpacked matrix */
double *buf; /* Output buffer */
} State;
/* 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 );
}
/* Free our state.
*/
static void
free_state( State *state )
{
if( state->data ) {
int i;
for( i = 0; i < state->input->ysize; i++ )
if( state->data[i] ) {
im_free( state->data[i] );
state->data[i] = NULL;
}
im_free( state->data );
state->data = NULL;
}
}
/* Fill our state.
*/
static int
build_state( State *state, DOUBLEMASK *input, IMAGE *output, int lut_size )
{
int x, y, i;
state->input = input;
state->output = output;
state->lut_size = lut_size;
state->data = NULL;
if( !(state->buf = im_malloc( NULL, IM_IMAGE_SIZEOF_LINE( output ) )) )
return( -1 );
if( !(state->data = IM_ARRAY( NULL, input->ysize, double * )) )
return( -1 );
for( y = 0; y < input->ysize; y++ )
state->data[y] = NULL;
for( y = 0; y < input->ysize; y++ )
if( !(state->data[y] = IM_ARRAY( NULL, input->xsize, double )) )
return( -1 );
for( i = 0, y = 0; y < input->ysize; y++ )
for( x = 0; x < input->xsize; x++, i++ )
state->data[y][x] = input->coeff[i];
/* Sanity check for data range.
*/
for( y = 0; y < input->ysize; y++ )
for( x = 0; x < input->xsize; x++ )
if( state->data[y][x] > 1.0 ||
state->data[y][x] < 0.0 ) {
im_error( "im_invertlut",
_( "element (%d, %d) is %g, "
"outside range [0,1]" ),
x, y, state->data[y][x] );
return( -1 );
}
/* Sort by 1st column in input.
*/
qsort( state->data, input->ysize, sizeof( double * ), compare );
#ifdef DEBUG
printf( "Input table, sorted by 1st column\n" );
for( y = 0; y < input->ysize; y++ ) {
printf( "%.4d ", y );
for( x = 0; x < input->xsize; x++ )
printf( "%.9f ", state->data[y][x] );
printf( "\n" );
}
#endif /*DEBUG*/
return( 0 );
}
static int
invertlut( State *state )
{
DOUBLEMASK *input = state->input;
int ysize = input->ysize;
int xsize = input->xsize;
double *buf = state->buf;
int bands = xsize - 1;
double **data = state->data;
int lut_size = state->lut_size;
int b;
/* Do each output channel separately.
*/
for( b = 0; b < bands; b++ ) {
/* The first and last lut positions we know real values for.
*/
int first = data[0][b + 1] * (lut_size - 1);
int last = data[ysize - 1][b + 1] * (lut_size - 1);
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.
*/
double fac = data[0][0] / first;
buf[b + k * bands] = k * fac;
}
for( k = last; k < lut_size; k++ ) {
/* Inside the loop to avoid /0 errors for last ==
* (lut_size - 1).
*/
double fac = (1 - data[ysize - 1][0]) /
((lut_size - 1) - last);
buf[b + k * bands] =
data[ysize - 1][0] + (k - last) * fac;
}
/* Interpolate the data sections.
*/
for( k = first; k < last; k++ ) {
/* Where we're at in the [0,1] range.
*/
double ki = (double) k / (lut_size - 1);
double irange, orange;
int j;
/* Search for the lowest real value < ki. There may
* not be one: if not, just use 0. Tiny error.
*/
for( j = ysize - 1; j >= 0; j-- )
if( data[j][b + 1] < ki )
break;
if( j == -1 )
j = 0;
/* Interpolate k as being between row data[j] and row
* data[j + 1].
*/
irange = data[j + 1][b + 1] - data[j][b + 1];
orange = data[j + 1][0] - data[j][0];
buf[b + k * bands] = data[j][0] +
orange * ((ki - data[j][b + 1]) / irange);
}
}
return( 0 );
}
/**
* im_invertlut:
* @input: input mask
* @output: output LUT
* @lut_size: generate this much
*
* 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.
*
* Eg. input like this:
*
* <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.
*
* Inputs don't need to be sorted (we do that). Generate any precision
* LUT, typically you might ask for 256 elements.
*
* It won't work too well for non-monotonic camera responses
* (we should fix this). Interpolation is simple piecewise linear; we ought to
* do something better really.
*
* See also: im_buildlut(), im_invertlut()
*
* Returns: 0 on success, -1 on error
*/
int
im_invertlut( DOUBLEMASK *input, IMAGE *output, int lut_size )
{
State state;
if( !input ||
input->xsize < 2 ||
input->ysize < 1 ) {
im_error( "im_invertlut", "%s", _( "bad input matrix" ) );
return( -1 );
}
if( lut_size < 1 ||
lut_size > 65536 ) {
im_error( "im_invertlut", "%s", _( "bad lut_size" ) );
return( -1 );
}
im_initdesc( output,
lut_size, 1, input->xsize - 1,
IM_BBITS_DOUBLE, IM_BANDFMT_DOUBLE,
IM_CODING_NONE, IM_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
if( im_setupout( output ) )
return( -1 );
if( build_state( &state, input, output, lut_size ) ||
invertlut( &state ) ||
im_writeline( 0, output, (VipsPel *) state.buf ) ) {
free_state( &state );
return( -1 );
}
free_state( &state );
return( 0 );
}

View File

@ -60,9 +60,11 @@ int vips_sines( VipsImage **out, int width, int height, ... )
int vips_zone( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_identity( VipsImage **out, ... )
__attribute__((sentinel));
int vips_buildlut( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_identity( VipsImage **out, ... )
int vips_invertlut( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int im_benchmarkn( VipsImage *in, VipsImage *out, int n );

View File

@ -52,7 +52,6 @@ int vips_hist_match( VipsImage *in, VipsImage *ref, VipsImage **out, ... )
__attribute__((sentinel));
int im_invertlut( DOUBLEMASK *input, VipsImage *output, int lut_size );
int im_project( VipsImage *in, VipsImage *hout, VipsImage *vout );
int im_ismonotonic( VipsImage *lut, int *out );

View File

@ -735,6 +735,7 @@ int im_fgrey( VipsImage *out, const int xsize, const int ysize );
int im_sines( VipsImage *out,
int xsize, int ysize, double horfreq, double verfreq );
int im_buildlut( DOUBLEMASK *input, VipsImage *output );
int im_invertlut( DOUBLEMASK *input, VipsImage *output, int lut_size );
int im_identity( VipsImage *lut, int bands );
int im_identity_ushort( VipsImage *lut, int bands, int sz );