Merge branch 'master' into add-hsv

This commit is contained in:
JonDeen 2015-06-20 00:14:51 +02:00
commit 38c44abc6d
8 changed files with 140 additions and 35 deletions

2
TODO
View File

@ -1,3 +1,5 @@
- vips_sink_screen() should create its output images
- how about something like vips_grid() which turns a tall thin one-band
image into a much smaller many-band image?

View File

@ -2,6 +2,8 @@
*
* 5/6/15
* - from copy.c
* 10/6/15
* - add @factor option
*/
/*
@ -58,6 +60,8 @@ typedef struct _VipsBandfold {
*/
VipsImage *in;
int factor;
} VipsBandfold;
typedef VipsConversionClass VipsBandfoldClass;
@ -68,8 +72,8 @@ static int
vips_bandfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
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 );
@ -77,21 +81,22 @@ vips_bandfold_gen( VipsRegion *or,
VipsRect need;
int y;
need.left = 0;
need.left = r->left * bandfold->factor;
need.top = r->top;
need.width = in->Xsize;
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, 0, r->top + y );
VipsPel *q = VIPS_REGION_ADDR( or, 0, r->top + y );
VipsPel *p = VIPS_REGION_ADDR( ir,
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
* coordinates.
*/
memcpy( q, p, psize );
memcpy( q, p, psize * r->width );
}
return( 0 );
@ -100,6 +105,7 @@ vips_bandfold_gen( VipsRegion *or,
static int
vips_bandfold_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandfold *bandfold = (VipsBandfold *) object;
@ -109,12 +115,20 @@ vips_bandfold_build( VipsObject *object )
if( vips_image_pio_input( bandfold->in ) )
return( -1 );
if( bandfold->factor == 0 )
bandfold->factor = bandfold->in->Xsize;
if( bandfold->in->Xsize % bandfold->factor != 0 ) {
vips_error( class->nickname,
"%s", _( "@factor must be a factor of image width" ) );
return( -1 );
}
if( vips_image_pipelinev( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, bandfold->in, NULL ) )
return( -1 );
conversion->out->Xsize = 1;
conversion->out->Bands *= bandfold->in->Xsize;
conversion->out->Xsize /= bandfold->factor;
conversion->out->Bands *= bandfold->factor;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandfold_gen, vips_stop_one,
@ -148,11 +162,20 @@ vips_bandfold_class_init( VipsBandfoldClass *class )
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandfold, in ) );
VIPS_ARG_INT( class, "factor", 11,
_( "Factor" ),
_( "Fold by this factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsBandfold, factor ),
0, 10000000, 0 );
}
static void
vips_bandfold_init( VipsBandfold *bandfold )
{
/* 0 means fold by width, see above.
*/
bandfold->factor = 0;
}
/**
@ -161,9 +184,14 @@ vips_bandfold_init( VipsBandfold *bandfold )
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @factor: fold by this factor
*
* Fold up an image horizontally: width is collapsed into bands.
* @out has width 1 and has bands
* equal to @in bands times @in width.
* 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.
*
* See also: vips_csvload(), vips_bandunfold().
*

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.
*/

View File

@ -342,7 +342,7 @@ vips_gsf_has_zip64( void )
{
return( libgsf_major_version > 1 ||
libgsf_minor_version > 14 ||
libgsf_micro_version > 31 );
libgsf_micro_version >= 31 );
}
typedef struct _VipsForeignSaveDz VipsForeignSaveDz;

View File

@ -10,6 +10,8 @@
* - pack and unpack rad to scrgb
* 18/8/14
* - fix conversion to 16-bit RGB, thanks John
* 18/6/15
* - forward progress signals from load
*/
/*
@ -758,6 +760,31 @@ vips_foreign_load_iscompat( VipsImage *a, VipsImage *b )
return( TRUE );
}
/* Forward pre-eval-post signals to our output image so our caller can see
* progress for load via disc or memory.
*/
static void
vips_foreign_load_preeval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_preeval( load->out );
}
static void
vips_foreign_load_eval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_eval( load->out, progress->npels );
}
static void
vips_foreign_load_posteval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_posteval( load->out );
}
/* Our start function ... do the lazy open, if necessary, and return a region
* on the new image.
*/
@ -775,8 +802,20 @@ vips_foreign_load_start( VipsImage *out, void *a, void *b )
printf( "vips_foreign_load_start: triggering ->load()\n" );
#endif /*DEBUG*/
/* Read the image in.
/* Read the image in. This may involve a long computation and
* will finish with load->real holding the decompressed image.
*
* We want our caller to be able to see this computation on
* @out, so we need to forward the signals.
*/
g_signal_connect( load->real, "preeval",
G_CALLBACK( vips_foreign_load_preeval ), load );
g_signal_connect( load->real, "eval",
G_CALLBACK( vips_foreign_load_eval ), load );
g_signal_connect( load->real, "posteval",
G_CALLBACK( vips_foreign_load_posteval ), load );
vips_image_set_progress( load->real, TRUE );
if( class->load( load ) ||
vips_image_pio_input( load->real ) )
return( NULL );

View File

@ -746,7 +746,7 @@ vips_image_save_cb( VipsImage *image, int *result )
/* Progress feedback.
*/
static int
static void
vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
{
int tile_width;
@ -754,7 +754,7 @@ vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
int nlines;
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
*last = -1;
@ -767,15 +767,13 @@ vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
vips_concurrency_get(),
tile_width, tile_height, nlines );
printf( "\n" );
return( 0 );
}
static int
static void
vips_image_eval_cb( VipsImage *image, VipsProgress *progress, int *last )
{
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
if( progress->percent != *last ) {
printf( _( "%s %s: %d%% complete" ),
@ -790,23 +788,19 @@ vips_image_eval_cb( VipsImage *image, VipsProgress *progress, int *last )
vips_region_dump_all();
*/
}
return( 0 );
}
static int
static void
vips_image_posteval_cb( VipsImage *image, VipsProgress *progress )
{
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
/* Spaces at end help to erase the %complete message we overwrite.
*/
printf( _( "%s %s: done in %.3gs \n" ),
g_get_prgname(), image->filename,
g_timer_elapsed( progress->start, NULL ) );
return( 0 );
}
/* Attach progress feedback, if required.

View File

@ -237,6 +237,15 @@ class TestConversion(unittest.TestCase):
self.assertEqual(y.bands, 1)
self.assertEqual(x.avg(), y.avg())
x = self.mono.bandfold(factor = 2)
self.assertEqual(x.width, self.mono.width / 2)
self.assertEqual(x.bands, 2)
y = x.bandunfold(factor = 2)
self.assertEqual(y.width, self.mono.width)
self.assertEqual(y.bands, 1)
self.assertEqual(x.avg(), y.avg())
def test_byteswap(self):
x = self.mono.cast("ushort")
y = x.byteswap().byteswap()