rewrite im_black() as a class

This commit is contained in:
John Cupitt 2011-10-31 14:49:39 +00:00
parent 735749a4a2
commit 3e72edf1b5
9 changed files with 207 additions and 104 deletions

View File

@ -4,7 +4,7 @@
im_flophor(), im_flipver(), im_insert(), im_insert_noexpand(), im_lrjoin(),
im_tbjoin(), im_extract_area(), im_extract_bands(), im_extract_areabands(),
im_replicate(), im_clip2fmt(), im_gbandjoin(), im_bandjoin(), im_invert(),
im_lintra(), im_lintra_vec()
im_lintra(), im_lintra_vec(), im_black()
redone as classes
- added argument priorites to help control arg ordering
- generate has a 'stop' param to signal successful early termination

2
TODO
View File

@ -3,7 +3,7 @@
- do clip etc. in new style, good to get everything that VipsAdd uses in
the new stack
insert* needs clip, lintra_vec
insert* needs im_black()
can we move everything to new-style ops now?

View File

@ -12,8 +12,8 @@ libconversion_la_SOURCES = \
replicate.c \
cast.c \
bandjoin.c \
black.c \
conver_dispatch.c \
im_black.c \
im_c2amph.c \
im_c2rect.c \
im_c2imag.c \

181
libvips/conversion/black.c Normal file
View File

@ -0,0 +1,181 @@
/* im_black.c
*
* Copyright: 1990, J. Cupitt
*
* Author: J. Cupitt
* Written on: 02/08/1990
* Modified on : 16/04/1991 by N. Dessipris to work on a line by line basis
* 15/8/94 JC
* - adapted for partials
* - ANSIfied
* - memory leaks fixed!
* 2/3/98 JC
* - IM_ANY added
* 18/1/09
* - gtkdoc
* 31/10/11
* - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include "conversion.h"
/**
* VipsBlack:
* @out: output image
* @width: output width
* @height: output height
* @bands: output bands
*
* Make a black unsigned char image of a specified size.
*
* See also: im_make_xy(), im_text(), im_gaussnoise().
*/
typedef struct _VipsBlack {
VipsConversion parent_instance;
int width;
int height;
int bands;
} VipsBlack;
typedef VipsConversionClass VipsBlackClass;
G_DEFINE_TYPE( VipsBlack, vips_black, VIPS_TYPE_CONVERSION );
static int
vips_black_gen( VipsRegion *or, void *seq, void *a, void *b,
gboolean *stop )
{
vips_region_black( or );
return( 0 );
}
static int
vips_black_build( VipsObject *object )
{
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBlack *black = (VipsBlack *) object;
if( VIPS_OBJECT_CLASS( vips_black_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_output( conversion->out ) )
return( -1 );
vips_image_init_fields( conversion->out,
black->width, black->height, black->bands,
VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
black->bands == 1 ?
VIPS_INTERPRETATION_B_W : VIPS_INTERPRETATION_MULTIBAND,
1.0, 1.0 );
vips_demand_hint( conversion->out,
VIPS_DEMAND_STYLE_ANY, NULL );
if( vips_image_generate( conversion->out,
NULL, vips_black_gen, NULL, NULL, NULL ) )
return( -1 );
return( 0 );
}
static void
vips_black_class_init( VipsBlackClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VIPS_DEBUG_MSG( "vips_black_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "black";
vobject_class->description = _( "make a black image" );
vobject_class->build = vips_black_build;
VIPS_ARG_INT( class, "width", 4,
_( "Width" ),
_( "Image width in pixels" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBlack, width ),
1, 1000000, 1 );
VIPS_ARG_INT( class, "height", 5,
_( "Height" ),
_( "Image height in pixels" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBlack, height ),
1, 1000000, 1 );
VIPS_ARG_INT( class, "bands", 6,
_( "Bands" ),
_( "Number of bands in image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsBlack, bands ),
1, 1000000, 1 );
}
static void
vips_black_init( VipsBlack *black )
{
black->bands = 1;
}
int
vips_black( VipsImage **out, int width, int height, ... )
{
va_list ap;
int result;
va_start( ap, height );
result = vips_call_split( "black", ap, out, width, height );
va_end( ap );
return( result );
}

View File

@ -112,6 +112,7 @@ vips_conversion_operation_init( void )
extern GType vips_replicate_get_type( void );
extern GType vips_cast_get_type( void );
extern GType vips_bandjoin_get_type( void );
extern GType vips_black_get_type( void );
vips_copy_get_type();
vips_embed_get_type();
@ -123,5 +124,6 @@ vips_conversion_operation_init( void )
vips_replicate_get_type();
vips_cast_get_type();
vips_bandjoin_get_type();
vips_black_get_type();
}

View File

@ -1,100 +0,0 @@
/* im_black.c
*
* Copyright: 1990, J. Cupitt
*
* Author: J. Cupitt
* Written on: 02/08/1990
* Modified on : 16/04/1991 by N. Dessipris to work on a line by line basis
* 15/8/94 JC
* - adapted for partials
* - ANSIfied
* - memory leaks fixed!
* 2/3/98 JC
* - IM_ANY added
* 18/1/09
* - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
/* Generate function --- just black out the region.
*/
static int
black_gen( REGION *or, void *seq, void *a, void *b )
{
im_region_black( or );
return( 0 );
}
/**
* im_black:
* @out: output #IMAGE
* @x: output width
* @y: output height
* @bands: number of output bands
*
* Make a black unsigned char image of a specified size.
*
* See also: im_make_xy(), im_text(), im_gaussnoise().
*
* Returns: 0 on success, -1 on error
*/
int
im_black( IMAGE *out, int x, int y, int bands )
{
if( x <= 0 || y <= 0 || bands <= 0 ) {
im_error( "im_black", "%s", _( "bad parameter" ) );
return( -1 );
}
if( im_poutcheck( out ) )
return( -1 );
im_initdesc( out,
x, y, bands,
IM_BBITS_BYTE, IM_BANDFMT_UCHAR, IM_CODING_NONE,
bands == 1 ? IM_TYPE_B_W : IM_TYPE_MULTIBAND,
1.0, 1.0, 0, 0 );
if( im_demand_hint( out, IM_ANY, NULL ) )
return( -1 );
if( im_generate( out, NULL, black_gen, NULL, NULL, NULL ) )
return( -1 );
return( 0 );
}

View File

@ -1377,3 +1377,21 @@ im_lintra_vec( int n, double *a, IMAGE *in, double *b, IMAGE *out )
return( 0 );
}
int
im_black( IMAGE *out, int x, int y, int bands )
{
VipsImage *t;
if( vips_black( &t, x, y,
"bands", bands,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}

View File

@ -140,6 +140,8 @@ int vips_bandjoin( VipsImage **in, VipsImage **out, int n, ... )
__attribute__((sentinel));
int vips_bandjoin2( VipsImage *in1, VipsImage *in2, VipsImage **out, ... )
__attribute__((sentinel));
int vips_black( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
@ -163,7 +165,6 @@ 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_black( VipsImage *out, int x, int y, int bands );
int im_text( VipsImage *out, const char *text, const char *font,
int width, int alignment, int dpi );

View File

@ -557,6 +557,7 @@ int im_replicate( VipsImage *in, VipsImage *out, int across, int down );
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 );