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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
/* VipsInsert
*
* Copyright: 1990, J. Cupitt
*
* Author: J. Cupitt
* Written on: 02/08/1990
* Modified on :
* 31/8/93 JC
* - ANSIfied
* - Nicos' reformatting undone. Grr!
* 22/12/94
* - modernised
* - now does IM_CODING_LABQ too
* 22/6/95 JC
* - partialized
* 10/2/02 JC
* - adapted for im_prepare_to() stuff
* 14/4/04
* - sets Xoffset / Yoffset
* 3/7/06
* - add sanity range checks
* 24/3/09
* - added IM_CODING_RAD support
* 30/1/10
* - cleanups
* - formatalike/bandalike
* - gtkdoc
* 29/9/11
* - rewrite as a class
* - add expand, bg options
*/
/*
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 _VipsInsert {
VipsConversion parent_instance;
/* Params.
*/
VipsImage *main;
VipsImage *sub;
int x;
int y;
gboolean expand;
VipsArrayDouble *background;
/* Pixel we paint calculated from background.
*/
VipsPel *ink;
/* Inputs cast and banded up.
*/
VipsImage *main_processed;
VipsImage *sub_processed;
/* Geometry.
*/
VipsRect rout; /* Output space */
VipsRect rmain; /* Position of main in output */
VipsRect rsub; /* Position of sub in output */
} VipsInsert;
typedef VipsConversionClass VipsInsertClass;
G_DEFINE_TYPE( VipsInsert, vips_insert, VIPS_TYPE_CONVERSION );
/* Trivial case: we just need pels from one of the inputs.
*
* Also used by vips_arrayjoin.
*/
int
vips__insert_just_one( VipsRegion *or, VipsRegion *ir, int x, int y )
{
VipsRect need;
/* Find the part of pos we need.
*/
need = or->valid;
need.left -= x;
need.top -= y;
if( vips_region_prepare( ir, &need ) )
return( -1 );
/* Attach our output to it.
*/
if( vips_region_region( or, ir, &or->valid, need.left, need.top ) )
return( -1 );
return( 0 );
}
/* Paste in parts of ir that fall within or --- ir is an input REGION for an
* image positioned at pos within or.
*
* Also used by vips_arrayjoin.
*/
int
vips__insert_paste_region( VipsRegion *or, VipsRegion *ir, VipsRect *pos )
{
VipsRect ovl;
/* Does any of the sub-image appear in the area we have been asked
* to make?
*/
vips_rect_intersectrect( &or->valid, pos, &ovl );
if( !vips_rect_isempty( &ovl ) ) {
/* Find the part of in we need.
*/
ovl.left -= pos->left;
ovl.top -= pos->top;
/* Paint this area of pixels into or.
*/
if( vips_region_prepare_to( ir, or, &ovl,
ovl.left + pos->left, ovl.top + pos->top ) )
return( -1 );
}
return( 0 );
}
/* Insert generate function.
*/
static int
vips_insert_gen( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion **ir = (VipsRegion **) seq;
VipsRect *r = &or->valid;
VipsInsert *insert = (VipsInsert *) b;
VipsRect ovl;
/* Does the rect we have been asked for fall entirely inside the
* sub-image?
*/
if( vips_rect_includesrect( &insert->rsub, &or->valid ) )
return( vips__insert_just_one( or, ir[1],
insert->rsub.left, insert->rsub.top ) );
/* Does it fall entirely inside the main, and not at all inside the
* sub?
*/
vips_rect_intersectrect( &or->valid, &insert->rsub, &ovl );
if( vips_rect_includesrect( &insert->rmain, &or->valid ) &&
vips_rect_isempty( &ovl ) )
return( vips__insert_just_one( or, ir[0],
insert->rmain.left, insert->rmain.top ) );
/* Output requires both (or neither) input. If it is not entirely
* inside both the main and the sub, then there is going to be some
* background.
*/
if( !(vips_rect_includesrect( &insert->rsub, &or->valid ) &&
vips_rect_includesrect( &insert->rmain, &or->valid )) )
vips_region_paint_pel( or, r, insert->ink );
/* Paste from main.
*/
if( vips__insert_paste_region( or, ir[0], &insert->rmain ) )
return( -1 );
/* Paste from sub.
*/
if( vips__insert_paste_region( or, ir[1], &insert->rsub ) )
return( -1 );
return( 0 );
}
/* Make a pair of vector constants into a set of formatted pixels. bands can
* be 3 while n is 1, meaning expand the constant to the number of bands.
* imag can be NULL, meaning all zero for the imaginary component.
*/
VipsPel *
vips__vector_to_pels( const char *domain,
int bands, VipsBandFormat format, VipsCoding coding,
double *real, double *imag, int n )
{
/* Run our pipeline relative to this.
*/
VipsImage *context = vips_image_new();
VipsImage **t = (VipsImage **)
vips_object_local_array( VIPS_OBJECT( context ), 8 );
VipsImage *in;
double *ones;
VipsPel *result;
int i;
#ifdef VIPS_DEBUG
printf( "vips__vector_to_pels: starting\n" );
#endif /*VIPS_DEBUG*/
ones = VIPS_ARRAY( context, n, double );
for( i = 0; i < n; i++ )
ones[i] = 1.0;
/* Make the real and imaginary parts.
*/
if( vips_black( &t[0], 1, 1, "bands", bands, NULL ) ||
vips_linear( t[0], &t[1], ones, real, n, NULL ) ) {
g_object_unref( context );
return( NULL );
}
in = t[1];
if( imag ) {
if( vips_black( &t[2], 1, 1, "bands", bands, NULL ) ||
vips_linear( t[2], &t[3], ones, imag, n, NULL ) ||
vips_complexform( in, t[3], &t[4], NULL ) ) {
g_object_unref( context );
return( NULL );
}
in = t[4];
}
/* Cast to the output type and coding.
*/
if( vips_cast( in, &t[5], format, NULL ) ||
vips_image_encode( t[5], &t[6], coding ) ) {
g_object_unref( context );
return( NULL );
}
in = t[6];
/* Write to memory, copy to output buffer.
*/
if( !(t[7] = vips_image_new_memory()) ||
vips_image_write( in, t[7] ) ) {
g_object_unref( context );
return( NULL );
}
in = t[7];
if( !(result =
VIPS_ARRAY( NULL, VIPS_IMAGE_SIZEOF_PEL( in ), VipsPel )) ) {
g_object_unref( context );
return( NULL );
}
memcpy( result, in->data, VIPS_IMAGE_SIZEOF_PEL( in ) );
#ifdef VIPS_DEBUG
{
int i;
printf( "vips__vector_to_ink:\n" );
printf( "\t(real, imag) = " );
for( i = 0; i < n; i++ )
printf( "(%g, %g) ", real[i], imag ? imag[i] : 0 );
printf( "\n" );
printf( "\tink = " );
for( i = 0; i < VIPS_IMAGE_SIZEOF_PEL( in ); i++ )
printf( "%d ", result[i] );
printf( "\n" );
}
#endif /*VIPS_DEBUG*/
g_object_unref( context );
return( result );
}
static void
vips__vector_to_ink_cb( VipsObject *object, char *buf )
{
g_free( buf );
}
/* Calculate a pixel for an image from a vec of double. Valid while im is
* valid.
*/
VipsPel *
vips__vector_to_ink( const char *domain,
VipsImage *im, double *real, double *imag, int n )
{
int bands;
VipsBandFormat format;
VipsPel *result;
vips_image_decode_predict( im, &bands, &format );
if( !(result = vips__vector_to_pels( domain,
bands, format, im->Coding, real, imag, n )) )
return( NULL );
g_signal_connect( im, "postclose",
G_CALLBACK( vips__vector_to_ink_cb ), result );
return( result );
}
/* The inverse: take ink to a vec of double. Used in the vips7 compat
* wrappers. Result valid while im is valid.
*/
double *
vips__ink_to_vector( const char *domain, VipsImage *im, VipsPel *ink, int *n )
{
VipsImage **t = (VipsImage **)
vips_object_local_array( VIPS_OBJECT( im ), 6 );
double *result;
#ifdef VIPS_DEBUG
printf( "vips__ink_to_vector: starting\n" );
#endif /*VIPS_DEBUG*/
/* Wrap a VipsImage around ink.
*/
t[0] = vips_image_new_from_memory( ink, VIPS_IMAGE_SIZEOF_PEL( im ),
1, 1, VIPS_IMAGE_SIZEOF_PEL( im ), VIPS_FORMAT_UCHAR );
if( vips_copy( t[0], &t[1],
"bands", im->Bands,
"format", im->BandFmt,
"coding", im->Coding,
"interpretation", im->Type,
NULL ) )
return( NULL );
/* The image may be coded .. unpack to double.
*/
if( vips_image_decode( t[1], &t[2] ) ||
vips_cast( t[2], &t[3], VIPS_FORMAT_DOUBLE, NULL ) )
return( NULL );
/* To a mem buffer, then copy to out.
*/
if( !(t[4] = vips_image_new_memory()) ||
vips_image_write( t[3], t[4] ) )
return( NULL );
if( !(result = VIPS_ARRAY( im, t[4]->Bands, double )) )
return( NULL );
memcpy( result, t[4]->data, VIPS_IMAGE_SIZEOF_PEL( t[4] ) );
*n = t[4]->Bands;
#ifdef VIPS_DEBUG
{
int i;
printf( "vips__ink_to_vector:\n" );
printf( "\tink = " );
for( i = 0; i < n; i++ )
printf( "%d ", ink[i] );
printf( "\n" );
printf( "\tvec = " );
for( i = 0; i < *n; i++ )
printf( "%g ", result[i] );
printf( "\n" );
}
#endif /*VIPS_DEBUG*/
return( result );
}
static int
vips_insert_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsInsert *insert = (VipsInsert *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 6 );
VipsImage **arry;
if( VIPS_OBJECT_CLASS( vips_insert_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_input( insert->main ) ||
vips_image_pio_input( insert->sub ) ||
vips_check_bands_1orn( class->nickname,
insert->main, insert->sub ) ||
vips_check_coding_known( class->nickname, insert->main ) ||
vips_check_coding_same( class->nickname,
insert->main, insert->sub ) )
return( -1 );
/* Cast our input images up to a common format and bands.
*/
if( vips__formatalike( insert->main, insert->sub, &t[0], &t[1] ) ||
vips__bandalike( class->nickname, t[0], t[1], &t[2], &t[3] ) )
return( -1 );
insert->main_processed = t[2];
insert->sub_processed = t[3];
if( !(arry = vips_allocate_input_array( conversion->out,
insert->main_processed, insert->sub_processed, NULL )) )
return( -1 );
/* Joins can get very wide (eg. consider joining a set of tiles
* horizontally to make a large image), we don't want mem use to shoot
* up. SMALLTILE will guarantee we keep small and local.
*/
if( vips_image_pipeline_array( conversion->out,
VIPS_DEMAND_STYLE_SMALLTILE, arry ) )
return( -1 );
/* Calculate geometry.
*/
insert->rmain.left = 0;
insert->rmain.top = 0;
insert->rmain.width = insert->main_processed->Xsize;
insert->rmain.height = insert->main_processed->Ysize;
insert->rsub.left = insert->x;
insert->rsub.top = insert->y;
insert->rsub.width = insert->sub_processed->Xsize;
insert->rsub.height = insert->sub_processed->Ysize;
if( insert->expand ) {
/* Expand output to bounding box of these two.
*/
vips_rect_unionrect( &insert->rmain, &insert->rsub,
&insert->rout );
/* Translate origin to top LH corner of rout.
*/
insert->rmain.left -= insert->rout.left;
insert->rmain.top -= insert->rout.top;
insert->rsub.left -= insert->rout.left;
insert->rsub.top -= insert->rout.top;
insert->rout.left = 0;
insert->rout.top = 0;
}
else
insert->rout = insert->rmain;
conversion->out->Xsize = insert->rout.width;
conversion->out->Ysize = insert->rout.height;
if( !(insert->ink = vips__vector_to_ink(
class->nickname, conversion->out,
(double *) VIPS_ARRAY_ADDR( insert->background, 0 ), NULL,
VIPS_AREA( insert->background )->n )) )
return( -1 );
if( vips_image_generate( conversion->out,
vips_start_many, vips_insert_gen, vips_stop_many,
arry, insert ) )
return( -1 );
return( 0 );
}
/* xy range we sanity check on ... just to stop crazy numbers from 1/0 etc.
* causing g_assert() failures later.
*/
#define RANGE (100000000)
static void
vips_insert_class_init( VipsInsertClass *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_insert_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "insert";
vobject_class->description =
_( "insert image @sub into @main at @x, @y" );
vobject_class->build = vips_insert_build;
/* Can't be UNBUFFERED, we are a SMALLTILE operation.
*/
operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
VIPS_ARG_IMAGE( class, "main", -1,
_( "Main" ),
_( "Main input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsInsert, main ) );
VIPS_ARG_IMAGE( class, "sub", 0,
_( "Sub-image" ),
_( "Sub-image to insert into main image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsInsert, sub ) );
VIPS_ARG_INT( class, "x", 2,
_( "X" ),
_( "Left edge of sub in main" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsInsert, x ),
-RANGE, RANGE, 0 );
VIPS_ARG_INT( class, "y", 3,
_( "Y" ),
_( "Top edge of sub in main" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsInsert, y ),
-RANGE, RANGE, 0 );
VIPS_ARG_BOOL( class, "expand", 4,
_( "Expand" ),
_( "Expand output to hold all of both inputs" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsInsert, expand ),
FALSE );
VIPS_ARG_BOXED( class, "background", 5,
_( "Background" ),
_( "Color for new pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsInsert, background ),
VIPS_TYPE_ARRAY_DOUBLE );
}
static void
vips_insert_init( VipsInsert *insert )
{
/* Init our instance fields.
*/
insert->background = vips_array_double_newv( 1, 0.0 );
}
/**
* vips_insert:
* @main: big image
* @sub: small image
* @out: output image
* @x: left position of @sub
* @y: top position of @sub
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @expand: expand output to hold whole of both images
* * @background: colour for new pixels
*
* Insert @sub into @main at position @x, @y.
*
* Normally @out shows the whole of @main. If @expand is #TRUE then @out is
* made large enough to hold all of @main and @sub.
* Any areas of @out not coming from
* either @main or @sub are set to @background (default 0).
*
* If @sub overlaps @main,
* @sub will appear on top of @main.
*
* If the number of bands differs, 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 two n-band images are operated upon.
*
* The two 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_embed(), vips_extract_area().
*
* Returns: 0 on success, -1 on error
*/
int
vips_insert( VipsImage *main, VipsImage *sub, VipsImage **out,
int x, int y, ... )
{
va_list ap;
int result;
va_start( ap, y );
result = vips_call_split( "insert", ap, main, sub, out, x, y );
va_end( ap );
return( result );
}
|