2011-11-20 22:21:36 +01:00
|
|
|
/* base class for various operations on bands
|
|
|
|
*
|
|
|
|
* Copyright: 1991, N. Dessipris, modification of im_bandary()
|
|
|
|
*
|
|
|
|
* Author: N. Dessipris
|
|
|
|
* Written on: 17/04/1991
|
|
|
|
* Modified on :
|
|
|
|
* 16/3/94 JC
|
|
|
|
* - rewritten for partials
|
|
|
|
* - now in ANSI C
|
|
|
|
* - now works for any number of input images, except zero
|
|
|
|
* 7/10/94 JC
|
|
|
|
* - new IM_NEW()
|
|
|
|
* 16/4/07
|
|
|
|
* - fall back to im_copy() for 1 input image
|
|
|
|
* 17/1/09
|
|
|
|
* - cleanups
|
|
|
|
* - gtk-doc
|
|
|
|
* - im_bandary() just calls this
|
|
|
|
* - works for RAD coding too
|
|
|
|
* 27/1/10
|
|
|
|
* - formatalike inputs
|
|
|
|
* 17/5/11
|
|
|
|
* - sizealike inputs
|
|
|
|
* 27/10/11
|
|
|
|
* - rewrite as a class
|
|
|
|
* 20/11/11
|
2011-11-21 13:32:42 +01:00
|
|
|
* - from bandjoin
|
2017-12-15 15:21:31 +01:00
|
|
|
* 15/12/17
|
|
|
|
* - remove max images restriction
|
2011-11-20 22:21:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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
|
2013-03-07 06:40:19 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
2011-11-20 22:21:36 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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 <vips/internal.h>
|
|
|
|
#include <vips/debug.h>
|
|
|
|
|
|
|
|
#include "bandary.h"
|
|
|
|
|
|
|
|
G_DEFINE_ABSTRACT_TYPE( VipsBandary, vips_bandary, VIPS_TYPE_CONVERSION );
|
|
|
|
|
2017-12-15 15:21:31 +01:00
|
|
|
/* Our sequence value.
|
2011-11-20 22:21:36 +01:00
|
|
|
*/
|
2017-12-15 15:21:31 +01:00
|
|
|
typedef struct {
|
|
|
|
VipsBandary *bandary;
|
|
|
|
|
|
|
|
/* Set of input regions.
|
|
|
|
*/
|
|
|
|
VipsRegion **ir;
|
|
|
|
|
|
|
|
/* For each input, an input pointer.
|
|
|
|
*/
|
|
|
|
VipsPel **p;
|
|
|
|
|
|
|
|
} VipsBandarySequence;
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_bandary_stop( void *vseq, void *a, void *b )
|
|
|
|
{
|
|
|
|
VipsBandarySequence *seq = (VipsBandarySequence *) vseq;
|
|
|
|
|
|
|
|
if( seq->ir ) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; seq->ir[i]; i++ )
|
|
|
|
VIPS_UNREF( seq->ir[i] );
|
|
|
|
VIPS_FREE( seq->ir );
|
|
|
|
}
|
|
|
|
|
|
|
|
VIPS_FREE( seq->p );
|
|
|
|
|
|
|
|
VIPS_FREE( seq );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_bandary_start( VipsImage *out, void *a, void *b )
|
|
|
|
{
|
|
|
|
VipsImage **in = (VipsImage **) a;
|
|
|
|
VipsBandary *bandary = (VipsBandary *) b;
|
|
|
|
|
|
|
|
VipsBandarySequence *seq;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
if( !(seq = VIPS_NEW( NULL, VipsBandarySequence )) )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
seq->bandary = bandary;
|
|
|
|
seq->ir = NULL;
|
|
|
|
seq->p = NULL;
|
|
|
|
|
|
|
|
/* How many images?
|
|
|
|
*/
|
|
|
|
for( n = 0; in[n]; n++ )
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Alocate space for region array.
|
|
|
|
*/
|
|
|
|
if( !(seq->ir = VIPS_ARRAY( NULL, n + 1, VipsRegion * )) ) {
|
|
|
|
vips_bandary_stop( seq, NULL, NULL );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a set of regions.
|
|
|
|
*/
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
if( !(seq->ir[i] = vips_region_new( in[i] )) ) {
|
|
|
|
vips_bandary_stop( seq, NULL, NULL );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
seq->ir[n] = NULL;
|
|
|
|
|
|
|
|
/* Input pointers.
|
|
|
|
*/
|
|
|
|
if( !(seq->p = VIPS_ARRAY( NULL, n + 1, VipsPel * )) ) {
|
|
|
|
vips_bandary_stop( seq, NULL, NULL );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( seq );
|
|
|
|
}
|
2011-11-20 22:21:36 +01:00
|
|
|
|
|
|
|
static int
|
2017-12-15 15:21:31 +01:00
|
|
|
vips_bandary_gen( VipsRegion *or, void *vseq, void *a, void *b, gboolean *stop )
|
2011-11-20 22:21:36 +01:00
|
|
|
{
|
2017-12-15 15:21:31 +01:00
|
|
|
VipsBandarySequence *seq = (VipsBandarySequence *) vseq;
|
2011-11-20 22:21:36 +01:00
|
|
|
VipsBandary *bandary = (VipsBandary *) b;
|
|
|
|
VipsBandaryClass *class = VIPS_BANDARY_GET_CLASS( bandary );
|
2014-07-25 19:06:52 +02:00
|
|
|
VipsRect *r = &or->valid;
|
2011-11-20 22:21:36 +01:00
|
|
|
|
2017-12-15 15:21:31 +01:00
|
|
|
VipsPel *q;
|
2011-11-20 22:21:36 +01:00
|
|
|
int y, i;
|
|
|
|
|
2017-12-15 15:21:31 +01:00
|
|
|
if( vips_reorder_prepare_many( or->im, seq->ir, r ) )
|
2017-01-12 15:36:44 +01:00
|
|
|
return( -1 );
|
|
|
|
for( i = 0; i < bandary->n; i++ )
|
2017-12-15 15:21:31 +01:00
|
|
|
seq->p[i] = VIPS_REGION_ADDR( seq->ir[i], r->left, r->top );
|
|
|
|
seq->p[i] = NULL;
|
2011-12-31 19:22:42 +01:00
|
|
|
q = VIPS_REGION_ADDR( or, r->left, r->top );
|
2011-11-20 22:21:36 +01:00
|
|
|
|
2013-11-26 15:57:56 +01:00
|
|
|
VIPS_GATE_START( "vips_bandary_gen: work" );
|
|
|
|
|
2011-11-20 22:21:36 +01:00
|
|
|
for( y = 0; y < r->height; y++ ) {
|
2017-12-15 15:21:31 +01:00
|
|
|
class->process_line( bandary, q, seq->p, r->width );
|
2011-11-20 22:21:36 +01:00
|
|
|
|
2011-12-16 13:25:39 +01:00
|
|
|
for( i = 0; i < bandary->n; i++ )
|
2017-12-15 15:21:31 +01:00
|
|
|
seq->p[i] += VIPS_REGION_LSKIP( seq->ir[i] );
|
2011-11-20 22:21:36 +01:00
|
|
|
q += VIPS_REGION_LSKIP( or );
|
|
|
|
}
|
|
|
|
|
2013-11-26 15:57:56 +01:00
|
|
|
VIPS_GATE_STOP( "vips_bandary_gen: work" );
|
|
|
|
|
2011-11-20 22:21:36 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_bandary_build( VipsObject *object )
|
|
|
|
{
|
2014-06-15 12:14:57 +02:00
|
|
|
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
|
|
|
|
VipsBandaryClass *class = VIPS_BANDARY_GET_CLASS( object );
|
2011-11-20 22:21:36 +01:00
|
|
|
VipsConversion *conversion = VIPS_CONVERSION( object );
|
2014-06-15 12:14:57 +02:00
|
|
|
VipsBandary *bandary = VIPS_BANDARY( object );
|
2011-11-20 22:21:36 +01:00
|
|
|
|
|
|
|
int i;
|
2014-01-23 14:19:37 +01:00
|
|
|
VipsImage **decode;
|
2011-11-20 22:21:36 +01:00
|
|
|
VipsImage **format;
|
|
|
|
VipsImage **size;
|
|
|
|
|
|
|
|
if( VIPS_OBJECT_CLASS( vips_bandary_parent_class )->build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
2011-11-22 10:26:31 +01:00
|
|
|
if( bandary->n <= 0 ) {
|
2014-06-15 12:14:57 +02:00
|
|
|
vips_error( object_class->nickname,
|
2012-11-02 15:41:47 +01:00
|
|
|
"%s", _( "no input images" ) );
|
2011-11-22 10:26:31 +01:00
|
|
|
return( -1 );
|
|
|
|
}
|
2011-11-20 22:21:36 +01:00
|
|
|
|
2014-01-23 14:19:37 +01:00
|
|
|
decode = (VipsImage **) vips_object_local_array( object, bandary->n );
|
2011-11-21 13:32:42 +01:00
|
|
|
format = (VipsImage **) vips_object_local_array( object, bandary->n );
|
|
|
|
size = (VipsImage **) vips_object_local_array( object, bandary->n );
|
2014-01-23 14:19:37 +01:00
|
|
|
|
|
|
|
for( i = 0; i < bandary->n; i++ )
|
2014-02-03 15:29:57 +01:00
|
|
|
if( vips_image_decode( bandary->in[i], &decode[i] ) )
|
2014-01-23 14:19:37 +01:00
|
|
|
return( -1 );
|
|
|
|
if( vips__formatalike_vec( decode, format, bandary->n ) ||
|
2011-11-20 22:21:36 +01:00
|
|
|
vips__sizealike_vec( format, size, bandary->n ) )
|
|
|
|
return( -1 );
|
|
|
|
bandary->ready = size;
|
|
|
|
|
2013-10-22 10:29:40 +02:00
|
|
|
if( vips_image_pipeline_array( conversion->out,
|
|
|
|
VIPS_DEMAND_STYLE_THINSTRIP, bandary->ready ) )
|
2011-11-20 22:21:36 +01:00
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
conversion->out->Bands = bandary->out_bands;
|
2014-06-15 12:14:57 +02:00
|
|
|
if( class->format_table )
|
|
|
|
conversion->out->BandFmt =
|
|
|
|
class->format_table[bandary->ready[0]->BandFmt];
|
2011-11-20 22:21:36 +01:00
|
|
|
|
|
|
|
if( vips_image_generate( conversion->out,
|
2017-12-15 15:21:31 +01:00
|
|
|
vips_bandary_start, vips_bandary_gen, vips_bandary_stop,
|
2011-11-20 22:21:36 +01:00
|
|
|
bandary->ready, bandary ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_bandary_class_init( VipsBandaryClass *class )
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
|
2012-07-10 11:51:40 +02:00
|
|
|
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
|
2011-11-20 22:21:36 +01:00
|
|
|
|
|
|
|
VIPS_DEBUG_MSG( "vips_bandary_class_init\n" );
|
|
|
|
|
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
|
|
|
vobject_class->nickname = "bandary";
|
|
|
|
vobject_class->description = _( "operations on image bands" );
|
|
|
|
vobject_class->build = vips_bandary_build;
|
|
|
|
|
2017-03-06 12:55:38 +01:00
|
|
|
operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
|
2011-11-20 22:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-03 11:26:09 +02:00
|
|
|
vips_bandary_init( VipsBandary *bandary )
|
2011-11-20 22:21:36 +01:00
|
|
|
{
|
2013-06-03 11:26:09 +02:00
|
|
|
bandary->out_bands = -1;
|
2011-11-20 22:21:36 +01:00
|
|
|
}
|
2011-11-22 13:00:32 +01:00
|
|
|
|
|
|
|
/* Call this before chaining up in _build() to make the operation fall back to
|
|
|
|
* copy.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_bandary_copy( VipsBandary *bandary )
|
|
|
|
{
|
|
|
|
VipsConversion *conversion = VIPS_CONVERSION( bandary );
|
|
|
|
|
|
|
|
/* This isn't set by arith until build(), so we have to set
|
|
|
|
* again here.
|
|
|
|
*
|
|
|
|
* Should arith set out in _init()?
|
|
|
|
*/
|
|
|
|
g_object_set( bandary, "out", vips_image_new(), NULL );
|
|
|
|
|
|
|
|
return( vips_image_write( bandary->in[0], conversion->out ) );
|
|
|
|
}
|