add vips_bandfold()/vips_bandunfold()

was part of vips_copy(), but it was very ugly there
This commit is contained in:
John Cupitt 2015-06-05 14:52:05 +01:00
parent dbd852211e
commit e9720afb18
9 changed files with 400 additions and 208 deletions

View File

@ -3,13 +3,13 @@
- change the alpha range rules for vips_flatten() to match vips_premultiply()
- vipsthumbnail uses vips_resize() rather than its own code
- vipsthumbnail uses vips_premultiply() for better alpha quality
- vips_copy() can turn 1xN or Nx1 M-band images into MxN one-band images
- added bandand() bandor() bandeor() convenience funcs to Python
- oops, base64 encode could pad by up to two zero bytes
- added VipsRefString as a thing that gi bindings can unpack
- support "with Vips.Image as ...:" in Python
- support "with Vips.Image as i:" in Python
- try to support DOS CSV and PPM files on linux
- add vips_byteswap(), remove byteswap from vips_copy()
- add vips_byteswap(), remove byteswap option from vips_copy()
- add vips_bandfold()/vips_bandunfold()
7/5/15 started 8.0.3
- dzsave and tif pyr write could fail for some image dimensions, thanks Jonas

25
TODO
View File

@ -1,18 +1,5 @@
- break up vips_copy(), it's become unwieldy
- add tests for bandfold/unfold, byteswap
- just let it make changes which don't change sizeof(pixel)
- split swap out to vips_byteswap()
- split 1xN -> MxN out to vips_band_fold() / vips_band_unfold(), have an
option for fold direction
horizontal (default): M x N image, B bands -> (M * B) x N mono image
vertical: M x N image, B bands -> M x (N * B) mono, copy image N times
vertically
maybe leave direction option for later
- how about something like vips_grid() which turns a tall thin one-band
image into a much smaller many-band image?
@ -24,16 +11,10 @@
much faster to make a very tall, thin image and fold it up in a single
operation
can't make it an option for bandfold() since it would have a different seq
behaviour
- try:
$ vips csvload ~/Desktop/NDVI_VGYRM-lut.txt x.v --skip 1
$ vips copy x.v x2.v --width 256 --height 1 --bands 4
Segmentation fault (core dumped)
see commented-out test in test_conversion.py
- does ruby need to unpack RefString as well? what about C++?

View File

@ -23,6 +23,8 @@ libconversion_la_SOURCES = \
bandrank.c \
recomb.c \
bandmean.c \
bandfold.c \
bandunfold.c \
bandbool.c \
bandary.h \
bandary.c \

View File

@ -0,0 +1,183 @@
/* Fold up x into bands.
*
* 5/6/15
* - from copy.c
*/
/*
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 <stdlib.h>
#include <string.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include "pconversion.h"
typedef struct _VipsBandfold {
VipsConversion parent_instance;
/* The input image.
*/
VipsImage *in;
} VipsBandfold;
typedef VipsConversionClass VipsBandfoldClass;
G_DEFINE_TYPE( VipsBandfold, vips_bandfold, VIPS_TYPE_CONVERSION );
static int
vips_bandfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int psize = VIPS_IMAGE_SIZEOF_PEL( out );
VipsRect need;
int y;
need.left = 0;
need.top = r->top;
need.width = in->Xsize;
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 );
/* We can't use vips_region_region() since we change pixel
* coordinates.
*/
memcpy( q, p, psize );
}
return( 0 );
}
static int
vips_bandfold_build( VipsObject *object )
{
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandfold *bandfold = (VipsBandfold *) object;
if( VIPS_OBJECT_CLASS( vips_bandfold_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_input( bandfold->in ) )
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;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandfold_gen, vips_stop_one,
bandfold->in, bandfold ) )
return( -1 );
return( 0 );
}
static void
vips_bandfold_class_init( VipsBandfoldClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
VIPS_DEBUG_MSG( "vips_bandfold_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "bandfold";
vobject_class->description = _( "fold up x axis into bands" );
vobject_class->build = vips_bandfold_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandfold, in ) );
}
static void
vips_bandfold_init( VipsBandfold *bandfold )
{
}
/**
* vips_bandfold:
* @in: input image
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Fold up an image horizontally: width is collapsed into bands.
* @out has width 1 and has bands
* equal to @in bands times @in width.
*
* See also: vips_csvload(), vips_bandunfold().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_bandfold( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "bandfold", ap, in, out );
va_end( ap );
return( result );
}

View File

@ -0,0 +1,182 @@
/* Fold up x into bands.
*
* 5/6/15
* - from copy.c
*/
/*
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 <stdlib.h>
#include <string.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include "pconversion.h"
typedef struct _VipsBandunfold {
VipsConversion parent_instance;
/* The input image.
*/
VipsImage *in;
} VipsBandunfold;
typedef VipsConversionClass VipsBandunfoldClass;
G_DEFINE_TYPE( VipsBandunfold, vips_bandunfold, VIPS_TYPE_CONVERSION );
static int
vips_bandunfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsRect *r = &or->valid;
int esize = VIPS_IMAGE_SIZEOF_ELEMENT( in );
VipsRect need;
int y;
need.left = 0;
need.top = r->top;
need.width = 1;
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 *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 );
}
return( 0 );
}
static int
vips_bandunfold_build( VipsObject *object )
{
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandunfold *bandunfold = (VipsBandunfold *) object;
if( VIPS_OBJECT_CLASS( vips_bandunfold_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_input( bandunfold->in ) )
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;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandunfold_gen, vips_stop_one,
bandunfold->in, bandunfold ) )
return( -1 );
return( 0 );
}
static void
vips_bandunfold_class_init( VipsBandunfoldClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
VIPS_DEBUG_MSG( "vips_bandunfold_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "bandunfold";
vobject_class->description = _( "unfold image bands into x axis" );
vobject_class->build = vips_bandunfold_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandunfold, in ) );
}
static void
vips_bandunfold_init( VipsBandunfold *bandunfold )
{
}
/**
* vips_bandunfold:
* @in: input image
* @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.
*
* See also: vips_csvload(), vips_bandfold().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_bandunfold( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "bandunfold", ap, in, out );
va_end( ap );
return( result );
}

View File

@ -237,6 +237,8 @@ vips_conversion_operation_init( void )
extern GType vips_ifthenelse_get_type( void );
extern GType vips_recomb_get_type( void );
extern GType vips_bandmean_get_type( void );
extern GType vips_bandfold_get_type( void );
extern GType vips_bandunfold_get_type( void );
extern GType vips_flatten_get_type( void );
extern GType vips_premultiply_get_type( void );
extern GType vips_unpremultiply_get_type( void );
@ -279,6 +281,8 @@ vips_conversion_operation_init( void )
vips_ifthenelse_get_type();
vips_recomb_get_type();
vips_bandmean_get_type();
vips_bandfold_get_type();
vips_bandunfold_get_type();
vips_flatten_get_type();
vips_premultiply_get_type();
vips_unpremultiply_get_type();

View File

@ -53,6 +53,7 @@
* - support width -> bands conversion
* 5/6/15
* - move byteswap out to vips_byteswap()
* - move band folding out to vips_bandfold()/vips_unfold()
*/
/*
@ -129,137 +130,14 @@ typedef VipsConversionClass VipsCopyClass;
G_DEFINE_TYPE( VipsCopy, vips_copy, VIPS_TYPE_CONVERSION );
/* Copy, turning bands into the x axis.
*/
static int
vips_copy_unbandize_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsRect *r = &or->valid;
int sze = VIPS_IMAGE_SIZEOF_ELEMENT( in );
VipsRect need;
int x, y;
/* Ask for input we need.
*/
if( in->Xsize == 1 ) {
need.left = 0;
need.top = r->top;
need.width = 1;
need.height = r->height;
}
else {
need.left = r->top;
need.top = 0;
need.width = r->height;
need.height = 1;
}
if( vips_region_prepare( ir, &need ) )
return( -1 );
/* We copy 1 pixel at a time. A vertical input image won't be
* guaranteed to have continuous data.
*/
for( y = 0; y < r->height; y++ ) {
for( x = 0; x < r->width; x++ ) {
VipsPel *p;
VipsPel *q;
if( in->Xsize == 1 )
p = VIPS_REGION_ADDR( ir, 0, r->top + y ) +
(r->left + x) * sze;
else
p = VIPS_REGION_ADDR( ir, r->top + y, 0 ) +
(r->left + x) * sze;
q = VIPS_REGION_ADDR( or, r->left + x, r->top + y );
memcpy( q, p, sze );
}
}
return( 0 );
}
/* Copy, turning the x axis into bands, the inverse of the above. Useful for
* turning CSV files into RGB LUTs, for example.
*
* output has bands == input width, one of width or height 1.
*/
static int
vips_copy_bandize_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int sze = VIPS_IMAGE_SIZEOF_ELEMENT( in );
VipsRect need;
int x, y;
/* Ask for input we need.
*/
if( out->Xsize == 1 ) {
need.left = 0;
need.top = r->top;
need.width = in->Xsize;
need.height = r->height;
}
else {
need.left = 0;
need.top = r->left;
need.width = in->Xsize;
need.height = r->width;
}
if( vips_region_prepare( ir, &need ) )
return( -1 );
/* We have to copy 1 pixel at a time. Each scanline in our input
* becomes a pixel in the output. Scanlines are not guaranteed to be
* continuous after vips_region_prepare(), they may be a window on a
* larger image.
*/
for( y = 0; y < r->height; y++ ) {
for( x = 0; x < r->width; x++ ) {
VipsPel *p;
VipsPel *q;
if( out->Xsize == 1 ) {
p = VIPS_REGION_ADDR( ir, 0, r->top + y );
q = VIPS_REGION_ADDR( or, 0, r->top + y );
}
else {
p = VIPS_REGION_ADDR( ir, 0, r->left + x );
q = VIPS_REGION_ADDR( or, 0, r->left + x );
}
memcpy( q, p, out->Bands * sze );
}
}
return( 0 );
}
/* Copy a small area.
*/
static int
vips_copy_gen( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsRect *r = &or->valid;
/* Ask for input we need.
*/
if( vips_region_prepare( ir, r ) )
return( -1 );
if( vips_region_region( or, ir, r, r->left, r->top ) )
if( vips_region_prepare( ir, r ) ||
vips_region_region( or, ir, r, r->left, r->top ) )
return( -1 );
return( 0 );
@ -287,10 +165,9 @@ vips_copy_build( VipsObject *object )
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsCopy *copy = (VipsCopy *) object;
guint64 image_size_before;
guint64 image_size_after;
guint64 pel_size_before;
guint64 pel_size_after;
VipsImage copy_of_fields;
VipsGenerateFn copy_generate_fn;
int i;
if( VIPS_OBJECT_CLASS( vips_copy_parent_class )->build( object ) )
@ -349,43 +226,18 @@ vips_copy_build( VipsObject *object )
}
}
/* We try to stop the worst crashes by at least ensuring that we don't
* increase the number of pixels which might be addressed.
/* Disallow changes which alter sizeof(pel).
*/
image_size_before = VIPS_IMAGE_SIZEOF_IMAGE( &copy_of_fields );
image_size_after = VIPS_IMAGE_SIZEOF_IMAGE( conversion->out );
if( image_size_after > image_size_before ) {
pel_size_before = VIPS_IMAGE_SIZEOF_PEL( &copy_of_fields );
pel_size_after = VIPS_IMAGE_SIZEOF_PEL( conversion->out );
if( pel_size_after != pel_size_before ) {
vips_error( class->nickname,
"%s", _( "image size too large" ) );
"%s", _( "must not change pel size" ) );
return( -1 );
}
/* Pick a generate function.
*/
copy_generate_fn = vips_copy_gen;
/* We let our caller change a 1xN or Nx1 image with M bands into a MxN
* image. In other words, bands becomes width.
*/
if( (copy_of_fields.Xsize == 1 || copy_of_fields.Ysize == 1) &&
conversion->out->Bands == 1 &&
conversion->out->Xsize == copy_of_fields.Bands &&
conversion->out->Ysize == VIPS_MAX(
copy_of_fields.Xsize, copy_of_fields.Ysize ) )
copy_generate_fn = vips_copy_unbandize_gen;
/* And the inverse: change a MxN one-band image into a 1xN or Nx1
* M-band image. That is, squash M into bands.
*/
if( (conversion->out->Xsize == 1 || conversion->out->Ysize == 1) &&
conversion->out->Bands == copy_of_fields.Xsize &&
copy_of_fields.Bands == 1 &&
copy_of_fields.Ysize == VIPS_MAX(
conversion->out->Xsize, conversion->out->Ysize ) )
copy_generate_fn = vips_copy_bandize_gen;
if( vips_image_generate( conversion->out,
vips_start_one, copy_generate_fn, vips_stop_one,
vips_start_one, vips_copy_gen, vips_stop_one,
copy->in, copy ) )
return( -1 );
@ -528,14 +380,12 @@ vips_copy_init( VipsCopy *copy )
* Copy an image, optionally modifying the header. VIPS copies images by
* copying pointers, so this operation is instant, even for very large images.
*
* You can optionally set any or all header fields during the copy. Some
* header fields, such as "xres", the horizontal resolution, are safe to
* change in any way, others, such as "width" will cause immediate crashes if
* they are not set carefully. The operation will block changes which make the
* image size grow, see VIPS_IMAGE_SIZEOF_IMAGE().
* You can optionally change any or all header fields during the copy. You can
* make any change which does not change the size of a pel, so for example
* you can turn a 4-band uchar image into a 2-band ushort image, but you
* cannot change a 100 x 100 RGB image into a 300 x 100 mono image.
*
* You can use this operation to turn 1xN or Nx1 images with M bands into MxN
* one band images.
* See also: vips_byteswap(), vips_bandfold(), vips_bandunfold().
*
* Returns: 0 on success, -1 on error.
*/

View File

@ -167,6 +167,10 @@ int vips_bandjoin2( VipsImage *in1, VipsImage *in2, VipsImage **out, ... )
__attribute__((sentinel));
int vips_bandrank( VipsImage **in, VipsImage **out, int n, ... )
__attribute__((sentinel));
int vips_bandfold( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_bandunfold( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_bandbool( VipsImage *in, VipsImage **out,
VipsOperationBoolean boolean, ... )

View File

@ -227,30 +227,16 @@ class TestConversion(unittest.TestCase):
x = self.colour.copy(coding = Vips.Coding.NONE)
self.assertEqual(x.coding, Vips.Coding.NONE)
# test squashing width of mono image into bands
x = self.mono.copy(width = 1, bands = self.mono.width)
def test_bandfold(self):
x = self.mono.bandfold()
self.assertEqual(x.width, 1)
self.assertEqual(x.bands, self.mono.width)
# back the other way
y = x.copy(width = self.mono.width, bands = 1)
y = x.bandunfold()
self.assertEqual(y.width, self.mono.width)
self.assertEqual(y.bands, 1)
self.assertEqual(x.avg(), y.avg())
# test squashing width of mono image into bands
x = self.mono.copy(width = self.mono.height, height = 1, bands = self.mono.width)
self.assertEqual(x.width, self.mono.height)
self.assertEqual(x.height, 1)
self.assertEqual(x.bands, self.mono.width)
# back the other way
y = x.copy(width = self.mono.width, height = self.mono.height, bands = 1)
self.assertEqual(y.width, self.mono.width)
self.assertEqual(y.height, self.mono.height)
self.assertEqual(y.bands, 1)
#self.assertEqual(x.avg(), y.avg())
def test_embed(self):
for fmt in all_formats:
test = self.colour.cast(fmt)