1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/* join an array of images together
*
* 11/12/15
* - from join.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 <string.h>
#include <stdlib.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include "pconversion.h"
typedef struct _VipsArrayjoin {
VipsConversion parent_instance;
/* Params.
*/
VipsArrayImage *in;
int across;
int shim;
VipsArea *background;
VipsAlign halign;
VipsAlign valign;
int hspacing;
int vspacing;
int down;
VipsRect *rects;
} VipsArrayjoin;
typedef VipsConversionClass VipsArrayjoinClass;
G_DEFINE_TYPE( VipsArrayjoin, vips_arrayjoin, VIPS_TYPE_CONVERSION );
static int
vips_arrayjoin_gen( VipsRegion *or, void *seq,
void *a, void *b, gboolean *stop )
{
VipsRegion **ir = (VipsRegion **) seq;
VipsArrayjoin *join = (VipsArrayjoin *) b;
VipsRect *r = &or->valid;
int n = ((VipsArea *) join->in)->n;
int i;
/* Does this rect fit within one of our inputs? If it does, we
* can pass just the request on.
*/
for( i = 0; i < n; i++ )
if( vips_rect_includesrect( &join->rects[i], r ) )
return( vips__insert_just_one( or, ir[i],
join->rects[i].left, join->rects[i].top ) );
/* Output requires more than one input. Paste all touching inputs into
* the output.
*/
for( i = 0; i < n; i++ )
if( vips__insert_paste_region( or, ir[i], &join->rects[i] ) )
return( -1 );
return( 0 );
}
static int
vips_arrayjoin_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsArrayjoin *join = (VipsArrayjoin *) object;
VipsImage **in;
int n;
VipsImage **format;
VipsImage **band;
VipsImage **size;
int hspacing;
int vspacing;
int output_width;
int output_height;
int i;
if( VIPS_OBJECT_CLASS( vips_arrayjoin_parent_class )->build( object ) )
return( -1 );
in = vips_array_image_get( join->in, &n );
/* Array length zero means error.
*/
if( n == 0 )
return( -1 );
/* Move all input images to a common format and number of bands.
*/
format = (VipsImage **) vips_object_local_array( object, n );
if( vips__formatalike_vec( in, format, n ) )
return( -1 );
in = format;
/* We have to include the number of bands in @background in our
* calculation.
*/
band = (VipsImage **) vips_object_local_array( object, n );
if( vips__bandalike_vec( class->nickname,
in, band, n, join->background->n ) )
return( -1 );
in = band;
/* Now sizealike: search for the largest image.
*/
hspacing = in[0]->Xsize;
vspacing = in[0]->Ysize;
for( i = 1; i < n; i++ ) {
if( in[i]->Xsize > hspacing )
hspacing = in[i]->Xsize;
if( in[i]->Ysize > vspacing )
vspacing = in[i]->Ysize;
}
if( !vips_object_argument_isset( object, "hspacing" ) )
g_object_set( object, "hspacing", hspacing, NULL );
if( !vips_object_argument_isset( object, "vspacing" ) )
g_object_set( object, "vspacing", vspacing, NULL );
hspacing = join->hspacing;
vspacing = join->vspacing;
if( !vips_object_argument_isset( object, "across" ) )
g_object_set( object, "across", n, NULL );
/* How many images down the grid?
*/
join->down = VIPS_ROUND_UP( n, join->across ) / join->across;
/* The output size.
*/
output_width = hspacing * join->across +
join->shim * (join->across - 1);
output_height = vspacing * join->down +
join->shim * (join->down - 1);
/* Make a rect for the position of each input.
*/
join->rects = VIPS_ARRAY( join, n, VipsRect );
for( i = 0; i < n; i++ ) {
int x = i % join->across;
int y = i / join->across;
join->rects[i].left = x * (hspacing + join->shim);
join->rects[i].top = y * (vspacing + join->shim);
join->rects[i].width = hspacing;
join->rects[i].height = vspacing;
/* In the centre of the array, we make width / height larger
* by shim.
*/
if( x != join->across - 1 )
join->rects[i].width += join->shim;
if( y != join->down - 1 )
join->rects[i].height += join->shim;
/* The right edge of the final image is stretched to the right
* to fill the whole row.
*/
if( i == n - 1 )
join->rects[i].width =
output_width - join->rects[i].left;
}
/* Each image must be cropped and aligned within an @hspacing by
* @vspacing box.
*/
size = (VipsImage **) vips_object_local_array( object, n );
for( i = 0; i < n; i++ ) {
int left, top;
int width, height;
/* Compiler warnings.
*/
left = 0;
top = 0;
switch( join->halign ) {
case VIPS_ALIGN_LOW:
left = 0;
break;
case VIPS_ALIGN_CENTRE:
left = (hspacing - in[i]->Xsize) / 2;
break;
case VIPS_ALIGN_HIGH:
left = hspacing - in[i]->Xsize;
break;
default:
g_assert_not_reached();
break;
}
switch( join->valign ) {
case VIPS_ALIGN_LOW:
top = 0;
break;
case VIPS_ALIGN_CENTRE:
top = (vspacing - in[i]->Ysize) / 2;
break;
case VIPS_ALIGN_HIGH:
top = vspacing - in[i]->Ysize;
break;
default:
g_assert_not_reached();
break;
}
width = join->rects[i].width;
height = join->rects[i].height;
if( vips_embed( in[i], &size[i], left, top, width, height,
"extend", VIPS_EXTEND_BACKGROUND,
"background", join->background,
NULL ) )
return( -1 );
}
if( vips_image_pipeline_array( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, size ) )
return( -1 );
conversion->out->Xsize = output_width;
conversion->out->Ysize = output_height;
if( vips_image_generate( conversion->out,
vips_start_many, vips_arrayjoin_gen, vips_stop_many,
size, join ) )
return( -1 );
return( 0 );
}
static void
vips_arrayjoin_class_init( VipsArrayjoinClass *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_arrayjoin_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "arrayjoin";
vobject_class->description = _( "join an array of images" );
vobject_class->build = vips_arrayjoin_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_BOXED( class, "in", -1,
_( "Input" ),
_( "Array of input images" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, in ),
VIPS_TYPE_ARRAY_IMAGE );
VIPS_ARG_INT( class, "across", 4,
_( "Across" ),
_( "Number of images across grid" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, across ),
1, 1000000, 1 );
VIPS_ARG_INT( class, "shim", 5,
_( "Shim" ),
_( "Pixels between images" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, shim ),
0, 1000000, 0 );
VIPS_ARG_BOXED( class, "background", 6,
_( "Background" ),
_( "Colour for new pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, background ),
VIPS_TYPE_ARRAY_DOUBLE );
VIPS_ARG_ENUM( class, "halign", 7,
_( "Horizontal align" ),
_( "Align on the left, centre or right" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, halign ),
VIPS_TYPE_ALIGN, VIPS_ALIGN_LOW );
VIPS_ARG_ENUM( class, "valign", 8,
_( "Vertical align" ),
_( "Align on the top, centre or bottom" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, valign ),
VIPS_TYPE_ALIGN, VIPS_ALIGN_LOW );
VIPS_ARG_INT( class, "hspacing", 9,
_( "Horizontal spacing" ),
_( "Horizontal spacing between images" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, hspacing ),
1, 1000000, 1 );
VIPS_ARG_INT( class, "vspacing", 10,
_( "Vertical spacing" ),
_( "Vertical spacing between images" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsArrayjoin, vspacing ),
1, 1000000, 1 );
}
static void
vips_arrayjoin_init( VipsArrayjoin *join )
{
/* Init our instance fields.
*/
join->background =
vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
((double *) (join->background->data))[0] = 0.0;
}
static int
vips_arrayjoinv( VipsImage **in, VipsImage **out, int n, va_list ap )
{
VipsArrayImage *array;
int result;
array = vips_array_image_new( in, n );
result = vips_call_split( "arrayjoin", ap, array, out );
vips_area_unref( VIPS_AREA( array ) );
return( result );
}
/**
* vips_arrayjoin:
* @in: (array length=n) (transfer none): array of input images
* @out: output image
* @n: number of input images
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @across: %gint, number of images per row
* * @shim: %gint, space between images, in pixels
* * @background: #VipsArrayDouble, background ink colour
* * @halign: #VipsAlign, low, centre or high alignment
* * @valign: #VipsAlign, low, centre or high alignment
* * @hspacing: %gint, horizontal distance between images
* * @vspacing: %gint, vertical distance between images
*
* Lay out the images in @in in a grid. The grid is @across images across and
* however high is necessary to use up all of @in. Images are set down
* left-to-right and top-to-bottom. @across defaults to @n.
*
* Each input image is placed with a box of size @hspacing by @vspacing
* pixels and cropped. These default to the largest width and largest height
* of the input images.
*
* Space between images is filled with @background. This defaults to 0
* (black).
*
* Images are positioned within their @hspacing by @vspacing box at low,
* centre or high coordinate values, controlled by @halign and @valign. These
* default to left-top.
*
* Boxes are joined and separated by @shim pixels. This defaults to 0.
*
* If the number of bands in the input images differs, all but one of the
* images must have one band. In this case, an n-band image is formed from the
* one-band image by joining n copies of the one-band image together, and then
* the n-band images are operated upon.
*
* The input images are cast up to the smallest common type (see table
* Smallest common format in
* <link linkend="libvips-arithmetic">arithmetic</link>).
*
* See also: vips_join(), vips_insert().
*
* Returns: 0 on success, -1 on error
*/
int
vips_arrayjoin( VipsImage **in, VipsImage **out, int n, ... )
{
va_list ap;
int result;
va_start( ap, n );
result = vips_arrayjoinv( in, out, n, ap );
va_end( ap );
return( result );
}
|