gaussnoise redone as a class

This commit is contained in:
John Cupitt 2013-05-29 12:21:30 +01:00
parent 10f6842d36
commit 62b52ed624
8 changed files with 56 additions and 258 deletions

View File

@ -3,6 +3,7 @@
- turn off caching for im_copy()/vips_copy(), we use copy to stop sharing, and
it's cheap so caching doesn't help anyway
- auto rshift down to 8 bits on save, if necessary
- im_gaussnoise() redone as a class
14/5/13 started 7.32.4
- icc import and export could segv on very wide images

View File

@ -22,6 +22,7 @@ libconversion_la_SOURCES = \
bandbool.c \
bandary.h \
bandary.c \
gaussnoise.c \
rot.c \
ifthenelse.c \
conver_dispatch.c \
@ -35,7 +36,6 @@ libconversion_la_SOURCES = \
im_system.c \
im_system_image.c \
im_text.c \
im_gaussnoise.c \
im_wrap.c \
im_zoom.c

View File

@ -125,6 +125,7 @@ vips_conversion_operation_init( void )
extern GType vips_bandmean_get_type( void );
extern GType vips_flatten_get_type( void );
extern GType vips_bandbool_get_type( void );
extern GType vips_gaussnoise_get_type( void );
vips_copy_get_type();
vips_tile_cache_get_type();
@ -147,6 +148,7 @@ vips_conversion_operation_init( void )
vips_bandmean_get_type();
vips_flatten_get_type();
vips_bandbool_get_type();
vips_gaussnoise_get_type();
}
/* The common part of most binary conversion

View File

@ -18,6 +18,8 @@
* 29/1/10
* - cleanups
* - gtkdoc
* 29/5/13
* - redo as a class
*/
/*
@ -73,6 +75,18 @@ typedef VipsConversionClass VipsGaussnoiseClass;
G_DEFINE_TYPE( VipsGaussnoise, vips_gaussnoise, VIPS_TYPE_CONVERSION );
/* Make a random number in 0 - 1. Prefer random().
*/
#ifdef HAVE_RANDOM
#define VIPS_RND() ((double) random() / RAND_MAX)
#else /*!HAVE_RANDOM*/
#ifdef HAVE_RAND
#define VIPS_RND() ((double) rand() / RAND_MAX)
#else /*!HAVE_RAND*/
#error "no random number generator found"
#endif /*HAVE_RAND*/
#endif /*HAVE_RANDOM*/
static int
vips_gaussnoise_gen( VipsRegion *or, void *seq, void *a, void *b,
gboolean *stop )
@ -80,27 +94,24 @@ vips_gaussnoise_gen( VipsRegion *or, void *seq, void *a, void *b,
VipsGaussnoise *gaussnoise = (VipsGaussnoise *) a;
int sz = VIPS_REGION_N_ELEMENTS( or );
int x, y, i;
int y;
for( y = 0; y < or->valid.height; y++ ) {
float *q = (float *) VIPS_REGION_ADDR( or,
or->valid.left, y + or->valid.top );
int x;
for( x = 0; x < sz; x++ ) {
double sum = 0.0;
double sum;
int i;
sum = 0.0;
for( i = 0; i < 12; i++ )
#ifdef HAVE_RANDOM
sum += (double) random() / RAND_MAX;
#else /*HAVE_RANDOM*/
#ifdef HAVE_RAND
sum += (double) rand() / RAND_MAX;
#else /*HAVE_RAND*/
#error "no random number generator found"
#endif /*HAVE_RAND*/
#endif /*HAVE_RAND*/
sum += VIPS_RND();
q[x] = (sum - 6.0) * gin->sigma + gin->mean;
q[x] = (sum - 6.0) * gaussnoise->sigma +
gaussnoise->mean;
}
}
@ -117,11 +128,9 @@ vips_gaussnoise_build( VipsObject *object )
return( -1 );
vips_image_init_fields( conversion->out,
gaussnoise->width, gaussnoise->height, gaussnoise->bands,
VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
gaussnoise->bands == 1 ?
VIPS_INTERPRETATION_B_W : VIPS_INTERPRETATION_MULTIBAND,
1.0, 1.0 );
gaussnoise->width, gaussnoise->height, 1,
VIPS_FORMAT_FLOAT, VIPS_CODING_NONE,
VIPS_INTERPRETATION_B_W, 1.0, 1.0 );
vips_demand_hint( conversion->out,
VIPS_DEMAND_STYLE_ANY, NULL );
@ -138,8 +147,6 @@ vips_gaussnoise_class_init( VipsGaussnoiseClass *class )
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VIPS_DEBUG_MSG( "vips_gaussnoise_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
@ -166,22 +173,22 @@ vips_gaussnoise_class_init( VipsGaussnoiseClass *class )
_( "Mean of pixels in generated image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsGaussnoise, mean ),
-10000000, 1000000, 1 );
-10000000, 1000000, 128 );
VIPS_ARG_DOUBLE( class, "sigma", 6,
_( "Sigma" ),
_( "Standard deviation of pixels in generated image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsGaussnoise, sigma ),
1, 100000, 1 );
0, 100000, 30 );
}
static void
vips_gaussnoise_init( VipsGaussnoise *gaussnoise )
{
gaussnoise->mean = 1.0;
gaussnoise->sigma = 1.0;
gaussnoise->mean = 128.0;
gaussnoise->sigma = 30.0;
}
/**
@ -216,87 +223,3 @@ vips_gaussnoise( VipsImage **out, int width, int height, ... )
return( result );
}
/* Generate function --- just fill the region with noise. "dummy" is our
* sequence value: we don't need one.
*/
/*ARGSUSED*/
static int
gnoise_gen( REGION *or, void *seq, void *a, void *b )
{
GnoiseInfo *gin = (GnoiseInfo *) a;
int x, y, i;
int sz = IM_REGION_N_ELEMENTS( or );
for( y = 0; y < or->valid.height; y++ ) {
float *q = (float *)
IM_REGION_ADDR( or, or->valid.left, y + or->valid.top );
for( x = 0; x < sz; x++ ) {
double sum = 0.0;
for( i = 0; i < 12; i++ )
#ifdef HAVE_RANDOM
sum += (double) random() / RAND_MAX;
#else /*HAVE_RANDOM*/
#ifdef HAVE_RAND
sum += (double) rand() / RAND_MAX;
#else /*HAVE_RAND*/
#error "no random number generator found"
#endif /*HAVE_RAND*/
#endif /*HAVE_RAND*/
q[x] = (sum - 6.0) * gin->sigma + gin->mean;
}
}
return( 0 );
}
/**
* im_gaussnoise:
* @out: output image
* @x: output width
* @y: output height
* @mean: average value in output
* @sigma: standard deviation in output
*
* Make a one band float image of gaussian noise with the specified
* distribution. The noise distribution is created by averaging 12 random
* numbers with the appropriate weights.
*
* See also: im_addgnoise(), im_make_xy(), im_text(), im_gaussnoise().
*
* Returns: 0 on success, -1 on error
*/
int
im_gaussnoise( IMAGE *out, int x, int y, double mean, double sigma )
{
GnoiseInfo *gin;
if( x <= 0 || y <= 0 ) {
im_error( "im_gaussnoise", "%s", _( "bad parameter" ) );
return( -1 );
}
if( im_poutcheck( out ) )
return( -1 );
im_initdesc( out,
x, y, 1,
IM_BBITS_FLOAT, IM_BANDFMT_FLOAT, IM_CODING_NONE, IM_TYPE_B_W,
1.0, 1.0, 0, 0 );
if( im_demand_hint( out, IM_ANY, NULL ) )
return( -1 );
/* Save parameters.
*/
if( !(gin = IM_NEW( out, GnoiseInfo )) )
return( -1 );
gin->mean = mean;
gin->sigma = sigma;
if( im_generate( out, NULL, gnoise_gen, NULL, gin, NULL ) )
return( -1 );
return( 0 );
}

View File

@ -1,149 +0,0 @@
/* im_gaussnoise
*
* Copyright 1990, N. Dessipris.
*
* File written on 2/12/1986
* Author : N. Dessipris
* Updated : 6/6/1991
* 21/7/93 JC
* - im_outcheck() call added
* 1/2/95 JC
* - declaration for drand48() added
* - partialised, adapting im_black()
* 23/10/98 JC
* - drand48() chaged to random() for portability
* 21/10/02 JC
* - tries rand() if random() is not available
* - uses RAND_MAX, d'oh
* 29/1/10
* - cleanups
* - gtkdoc
*/
/*
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>
/* Keep parameters here.
*/
typedef struct {
double mean;
double sigma;
} GnoiseInfo;
/* Generate function --- just fill the region with noise. "dummy" is our
* sequence value: we don't need one.
*/
/*ARGSUSED*/
static int
gnoise_gen( REGION *or, void *seq, void *a, void *b )
{
GnoiseInfo *gin = (GnoiseInfo *) a;
int x, y, i;
int sz = IM_REGION_N_ELEMENTS( or );
for( y = 0; y < or->valid.height; y++ ) {
float *q = (float *)
IM_REGION_ADDR( or, or->valid.left, y + or->valid.top );
for( x = 0; x < sz; x++ ) {
double sum = 0.0;
for( i = 0; i < 12; i++ )
#ifdef HAVE_RANDOM
sum += (double) random() / RAND_MAX;
#else /*HAVE_RANDOM*/
#ifdef HAVE_RAND
sum += (double) rand() / RAND_MAX;
#else /*HAVE_RAND*/
#error "no random number generator found"
#endif /*HAVE_RAND*/
#endif /*HAVE_RAND*/
q[x] = (sum - 6.0) * gin->sigma + gin->mean;
}
}
return( 0 );
}
/**
* im_gaussnoise:
* @out: output image
* @x: output width
* @y: output height
* @mean: average value in output
* @sigma: standard deviation in output
*
* Make a one band float image of gaussian noise with the specified
* distribution. The noise distribution is created by averaging 12 random
* numbers with the appropriate weights.
*
* See also: im_addgnoise(), im_make_xy(), im_text(), im_black().
*
* Returns: 0 on success, -1 on error
*/
int
im_gaussnoise( IMAGE *out, int x, int y, double mean, double sigma )
{
GnoiseInfo *gin;
if( x <= 0 || y <= 0 ) {
im_error( "im_gaussnoise", "%s", _( "bad parameter" ) );
return( -1 );
}
if( im_poutcheck( out ) )
return( -1 );
im_initdesc( out,
x, y, 1,
IM_BBITS_FLOAT, IM_BANDFMT_FLOAT, IM_CODING_NONE, IM_TYPE_B_W,
1.0, 1.0, 0, 0 );
if( im_demand_hint( out, IM_ANY, NULL ) )
return( -1 );
/* Save parameters.
*/
if( !(gin = IM_NEW( out, GnoiseInfo )) )
return( -1 );
gin->mean = mean;
gin->sigma = sigma;
if( im_generate( out, NULL, gnoise_gen, NULL, gin, NULL ) )
return( -1 );
return( 0 );
}

View File

@ -1476,6 +1476,25 @@ im_black( IMAGE *out, int x, int y, int bands )
return( 0 );
}
int
im_gaussnoise( IMAGE *out, int x, int y, double mean, double sigma )
{
VipsImage *t;
if( vips_gaussnoise( &t, x, y,
"mean", mean,
"sigma", sigma,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
static int
vips__math( VipsImage *in, VipsImage *out, VipsOperationMath math )
{

View File

@ -233,6 +233,8 @@ int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
int vips_black( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_gaussnoise( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_rot( VipsImage *in, VipsImage **out, VipsAngle angle, ... )
__attribute__((sentinel));
@ -255,7 +257,6 @@ int im_msb_band( VipsImage *in, VipsImage *out, int band );
int im_scaleps( VipsImage *in, VipsImage *out );
int im_falsecolour( VipsImage *in, VipsImage *out );
int im_gaussnoise( VipsImage *out, int x, int y, double mean, double sigma );
int im_text( VipsImage *out, const char *text, const char *font,
int width, int alignment, int dpi );

View File

@ -708,6 +708,7 @@ int im_clip2fmt( VipsImage *in, VipsImage *out, VipsBandFormat fmt );
int im_bandjoin( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_gbandjoin( VipsImage **in, VipsImage *out, int n );
int im_black( VipsImage *out, int x, int y, int bands );
int im_gaussnoise( VipsImage *out, int x, int y, double mean, double sigma );
int im_c2amph( VipsImage *in, VipsImage *out );
int im_c2rect( VipsImage *in, VipsImage *out );