add @factor to bandunfold

This commit is contained in:
John Cupitt 2015-06-11 09:45:14 +01:00
parent 65c343552c
commit d7d8e03a86
4 changed files with 49 additions and 17 deletions

2
TODO
View File

@ -1,4 +1,4 @@
- bandunfold needs a @factor option too
- tests for band fold/unfold with factor
- how about something like vips_grid() which turns a tall thin one-band
image into a much smaller many-band image?

View File

@ -74,7 +74,6 @@ vips_bandfold_gen( VipsRegion *or,
{
VipsBandfold *bandfold = (VipsBandfold *) b;
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int psize = VIPS_IMAGE_SIZEOF_PEL( out );
@ -82,16 +81,16 @@ vips_bandfold_gen( VipsRegion *or,
VipsRect need;
int y;
need.left = bandfold->factor * r->left;
need.left = r->left * bandfold->factor;
need.top = r->top;
need.width = bandfold->factor * r->width;
need.width = r->width * bandfold->factor;
need.height = r->height;
if( vips_region_prepare( ir, &need ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( ir,
bandfold->factor * r->left, r->top + y );
r->left * bandfold->factor, r->top + y );
VipsPel *q = VIPS_REGION_ADDR( or, r->left, r->top + y );
/* We can't use vips_region_region() since we change pixel
@ -190,7 +189,7 @@ vips_bandfold_init( VipsBandfold *bandfold )
* @factor: fold by this factor
*
* Fold up an image horizontally: width is collapsed into bands.
* Set @factor to set how much to fold by: @factor 3, for example, will make
* Use @factor to set how much to fold by: @factor 3, for example, will make
* the output image three times narrower than the input, and with three times
* as many bands. By default the whole of the input width is folded up.
*

View File

@ -2,6 +2,8 @@
*
* 5/6/15
* - from copy.c
* 10/6/15
* - add @factor option
*/
/*
@ -58,6 +60,8 @@ typedef struct _VipsBandunfold {
*/
VipsImage *in;
int factor;
} VipsBandunfold;
typedef VipsConversionClass VipsBandunfoldClass;
@ -68,30 +72,34 @@ static int
vips_bandunfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsBandunfold *bandunfold = (VipsBandunfold *) b;
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int esize = VIPS_IMAGE_SIZEOF_ELEMENT( in );
int psize = VIPS_IMAGE_SIZEOF_PEL( out );
VipsRect need;
int y;
need.left = 0;
need.left = r->left / bandunfold->factor;
need.top = r->top;
need.width = 1;
need.width = (1 + r->width) / bandunfold->factor;
need.height = r->height;
if( vips_region_prepare( ir, &need ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( ir, 0, r->top + y ) +
r->left * esize;
VipsPel *p = VIPS_REGION_ADDR( ir,
r->left / bandunfold->factor, r->top + y ) +
(r->left % bandunfold->factor) * esize;
VipsPel *q = VIPS_REGION_ADDR( or, r->left, r->top + y );
/* We can't use vips_region_region() since we change pixel
* coordinates.
*/
memcpy( q, p, r->width * esize );
memcpy( q, p, r->width * psize );
}
return( 0 );
@ -100,6 +108,7 @@ vips_bandunfold_gen( VipsRegion *or,
static int
vips_bandunfold_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandunfold *bandunfold = (VipsBandunfold *) object;
@ -109,12 +118,20 @@ vips_bandunfold_build( VipsObject *object )
if( vips_image_pio_input( bandunfold->in ) )
return( -1 );
if( bandunfold->factor == 0 )
bandunfold->factor = bandunfold->in->Bands;
if( bandunfold->in->Bands % bandunfold->factor != 0 ) {
vips_error( class->nickname,
"%s", _( "@factor must be a factor of image bands" ) );
return( -1 );
}
if( vips_image_pipelinev( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, bandunfold->in, NULL ) )
return( -1 );
conversion->out->Xsize *= bandunfold->in->Bands;
conversion->out->Bands = 1;
conversion->out->Xsize *= bandunfold->factor;
conversion->out->Bands /= bandunfold->factor;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandunfold_gen, vips_stop_one,
@ -148,11 +165,20 @@ vips_bandunfold_class_init( VipsBandunfoldClass *class )
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandunfold, in ) );
VIPS_ARG_INT( class, "factor", 11,
_( "Factor" ),
_( "Unfold by this factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsBandunfold, factor ),
0, 10000000, 0 );
}
static void
vips_bandunfold_init( VipsBandunfold *bandunfold )
{
/* 0 means unfold by width, see above.
*/
bandunfold->factor = 0;
}
/**
@ -161,8 +187,14 @@ vips_bandunfold_init( VipsBandunfold *bandunfold )
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Unfold image bands into x axis. @out has 1 band, and has width
* equal to @in bands times @in width.
* Optional arguments:
*
* @factor: unfold by this factor
*
* Unfold image bands into x axis.
* Use @factor to set how much to unfold by: @factor 3, for example, will make
* the output image three times wider than the input, and with one third
* as many bands. By default, all bands are unfolded.
*
* See also: vips_csvload(), vips_bandfold().
*

View File

@ -189,7 +189,8 @@ vips_foreign_load_csv_init( VipsForeignLoadCsv *csv )
* @separator: set of separator characters
*
* Load a CSV (comma-separated values) file. The output image is always 1
* band (monochrome), #VIPS_FORMAT_DOUBLE.
* band (monochrome), #VIPS_FORMAT_DOUBLE. Use vips_bandfold() to turn
* RGBRGBRGB mono images into colour iamges.
*
* Items in lines can be either floating point numbers in the C locale, or
* strings enclosed in double-quotes ("), or empty.
@ -213,7 +214,7 @@ vips_foreign_load_csv_init( VipsForeignLoadCsv *csv )
* @separator sets the characters that separate fields.
* Default ;,<emphasis>tab</emphasis>. Separators are never run together.
*
* See also: vips_image_new_from_file().
* See also: vips_image_new_from_file(), vips_bandfold().
*
* Returns: 0 on success, -1 on error.
*/