im_grey*() as a class

This commit is contained in:
John Cupitt 2013-06-13 14:35:26 +01:00
parent cf09f3376e
commit bfa8c85837
7 changed files with 237 additions and 158 deletions

View File

@ -3,12 +3,12 @@ noinst_LTLIBRARIES = libcreate.la
libcreate_la_SOURCES = \
create.c \
eye.c \
grey.c \
xyz.c \
black.c \
text.c \
gaussnoise.c \
im_benchmark.c \
im_grey.c \
im_sines.c \
im_zone.c \
other_dispatch.c

View File

@ -105,6 +105,7 @@ vips_create_operation_init( void )
#endif /*HAVE_PANGOFT2*/
extern GType vips_xyz_get_type( void );
extern GType vips_eye_get_type( void );
extern GType vips_grey_get_type( void );
vips_black_get_type();
vips_gaussnoise_get_type();
@ -113,5 +114,6 @@ vips_create_operation_init( void )
#endif /*HAVE_PANGOFT2*/
vips_xyz_get_type();
vips_eye_get_type();
vips_grey_get_type();
}

192
libvips/create/grey.c Normal file
View File

@ -0,0 +1,192 @@
/* grey ramps
*
* Copyright: 1990, N. Dessipris.
*
* Author: Nicos Dessipris
* Written on: 02/02/1990
* Modified on:
* 22/7/93 JC
* - im_outcheck() added
* - externs removed
* 8/2/95 JC
* - ANSIfied
* - im_fgrey() made from im_grey()
* 31/8/95 JC
* - now makes [0,1], rather than [0,256)
* - im_grey() now defined in terms of im_fgrey()
* 2/3/98 JC
* - partialed
* 1/2/11
* - gtk-doc
* 13/6/13
* - redo as a class
*/
/*
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
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include "create.h"
typedef struct _VipsGrey {
VipsCreate parent_instance;
int width;
int height;
gboolean uchar;
} VipsGrey;
typedef VipsCreateClass VipsGreyClass;
G_DEFINE_TYPE( VipsGrey, vips_grey, VIPS_TYPE_CREATE );
static int
vips_grey_build( VipsObject *object )
{
VipsCreate *create = VIPS_CREATE( object );
VipsGrey *grey = (VipsGrey *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 7 );
VipsImage *in;
if( VIPS_OBJECT_CLASS( vips_grey_parent_class )->build( object ) )
return( -1 );
if( vips_xyz( &t[0], grey->width, grey->height, NULL ) ||
vips_extract_band( t[0], &t[1], 0, NULL ) )
return( -1 );
if( grey->uchar ) {
if( vips_linear1( t[1], &t[2],
255.0 / (grey->width - 1), 0.0, NULL ) ||
vips_cast( t[2], &t[3], VIPS_FORMAT_UCHAR, NULL ) )
return( -1 );
in = t[3];
}
else {
if( vips_linear1( t[1], &t[2],
1.0 / (grey->width - 1), 0, NULL ) )
return( -1 );
in = t[2];
}
if( vips_image_write( in, create->out ) )
return( -1 );
return( 0 );
}
static void
vips_grey_class_init( VipsGreyClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "grey";
vobject_class->description = _( "make a grey ramp image" );
vobject_class->build = vips_grey_build;
VIPS_ARG_INT( class, "width", 4,
_( "Width" ),
_( "Image width in pixels" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGrey, width ),
1, 1000000, 1 );
VIPS_ARG_INT( class, "height", 5,
_( "Height" ),
_( "Image height in pixels" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGrey, height ),
1, 1000000, 1 );
VIPS_ARG_BOOL( class, "uchar", 7,
_( "Uchar" ),
_( "Output an unsigned char image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsGrey, uchar ),
FALSE );
}
static void
vips_grey_init( VipsGrey *grey )
{
}
/**
* vips_grey:
* @out: output image
* @xsize: image size
* @ysize: image size
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @uchar: output a uchar image
*
* Create a one-band float image with the left-most column zero and the
* right-most 1. Intermediate pixels are a linear ramp.
*
* Set @uchar to output a uchar image with the leftmost pixel 0 and the
* rightmost 255.
*
* See also: vips_xyz(), vips_identity().
*
* Returns: 0 on success, -1 on error
*/
int
vips_grey( VipsImage **out, int width, int height, ... )
{
va_list ap;
int result;
va_start( ap, height );
result = vips_call_split( "grey", ap, out, width, height );
va_end( ap );
return( result );
}

View File

@ -1,153 +0,0 @@
/* grey ramps
*
* Copyright: 1990, N. Dessipris.
*
* Author: Nicos Dessipris
* Written on: 02/02/1990
* Modified on:
* 22/7/93 JC
* - im_outcheck() added
* - externs removed
* 8/2/95 JC
* - ANSIfied
* - im_fgrey() made from im_grey()
* 31/8/95 JC
* - now makes [0,1], rather than [0,256)
* - im_grey() now defined in terms of im_fgrey()
* 2/3/98 JC
* - partialed
* 1/2/11
* - gtk-doc
*/
/*
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>
/* Generate function.
*/
static int
fgrey_gen( REGION *or, void *seq, void *a, void *b )
{
Rect *r = &or->valid;
int le = r->left;
int to = r->top;
int iwm = or->im->Xsize - 1;
int x, y;
for( y = 0; y < r->height; y++ ) {
float *q = (float *) IM_REGION_ADDR( or, le, y + to );
for( x = 0; x < r->width; x++ )
q[x] = (float) (x + le) / iwm;
}
return( 0 );
}
/**
* im_fgrey:
* @out: output image
* @xsize: image size
* @ysize: image size
*
* Create a one-band float image with the left-most column zero and the
* right-most 1. Intermediate pixels are a linear ramp.
*
* See also: im_grey(), im_make_xy(), im_identity().
*
* Returns: 0 on success, -1 on error
*/
int
im_fgrey( IMAGE *out, const int xsize, const int ysize )
{
/* Check args.
*/
if( xsize <=0 || ysize <= 0 ) {
im_error( "im_fgrey", "%s", _( "bad size" ) );
return( -1 );
}
if( im_poutcheck( out ) )
return( -1 );
/* Set image.
*/
im_initdesc( out, xsize, ysize, 1, IM_BBITS_FLOAT, IM_BANDFMT_FLOAT,
IM_CODING_NONE, IM_TYPE_B_W, 1.0, 1.0, 0, 0 );
/* Set hints - ANY is ok with us.
*/
if( im_demand_hint( out, IM_ANY, NULL ) )
return( -1 );
/* Generate image.
*/
if( im_generate( out, NULL, fgrey_gen, NULL, NULL, NULL ) )
return( -1 );
return( 0 );
}
/**
* im_grey:
* @out: output image
* @xsize: image size
* @ysize: image size
*
* Create a one-band uchar image with the left-most column zero and the
* right-most 255. Intermediate pixels are a linear ramp.
*
* See also: im_fgrey(), im_make_xy(), im_identity().
*
* Returns: 0 on success, -1 on error
*/
int
im_grey( IMAGE *out, const int xsize, const int ysize )
{
IMAGE *t[2];
/* Change range to [0,255].
*/
if( im_open_local_array( out, t, 2, "im_grey", "p" ) ||
im_fgrey( t[0], xsize, ysize ) ||
im_lintra( 255.0, t[0], 0.0, t[1] ) ||
im_clip2fmt( t[1], out, IM_BANDFMT_UCHAR ) )
return( -1 );
return( 0 );
}

View File

@ -2400,6 +2400,41 @@ im_feye( IMAGE *out, const int xsize, const int ysize, const double factor )
return( 0 );
}
int
im_grey( IMAGE *out, const int xsize, const int ysize )
{
VipsImage *x;
if( vips_grey( &x, xsize, ysize,
"uchar", TRUE,
NULL ) )
return( -1 );
if( im_copy( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_fgrey( IMAGE *out, const int xsize, const int ysize )
{
VipsImage *x;
if( vips_grey( &x, xsize, ysize,
NULL ) )
return( -1 );
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 )

View File

@ -42,10 +42,15 @@ void vips_create_operation_init( void );
int vips_black( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_xyz( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_grey( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_text( VipsImage **out, const char *text, ... )
__attribute__((sentinel));
int vips_gaussnoise( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_eye( VipsImage **out, int width, int height, ... )
@ -53,10 +58,6 @@ int vips_eye( VipsImage **out, int width, int height, ... )
int im_grey( VipsImage *out, const int xsize, const int ysize );
int im_fgrey( VipsImage *out, const int xsize, const int ysize );
int im_zone( VipsImage *out, int size );
int im_fzone( VipsImage *out, int size );
int im_sines( VipsImage *out,

View File

@ -745,6 +745,8 @@ int im_feye( VipsImage *out,
const int xsize, const int ysize, const double factor );
int im_eye( VipsImage *out,
const int xsize, const int ysize, const double factor );
int im_grey( VipsImage *out, const int xsize, const int ysize );
int im_fgrey( VipsImage *out, const int xsize, const int ysize );
DOUBLEMASK *im_vips2mask( VipsImage *in, const char *filename );
int im_mask2vips( DOUBLEMASK *in, VipsImage *out );