File: draw_image.c

package info (click to toggle)
vips 8.4.5-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,724 kB
  • sloc: ansic: 102,605; cpp: 57,527; sh: 4,957; xml: 3,317; python: 3,105; makefile: 1,089; perl: 40
file content (328 lines) | stat: -rw-r--r-- 8,104 bytes parent folder | download
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
/* in-place insert
 *
 * Copyright: J. Cupitt
 * Written: 15/06/1992
 * 22/7/93 JC
 *	- im_incheck() added
 * 16/8/94 JC
 *	- im_incheck() changed to im_makerw()
 * 1/9/04 JC
 *	- checks bands/types/etc match (thanks Matt)
 *	- smarter pixel size calculations
 * 5/12/06
 * 	- im_invalidate() after paint
 * 24/3/09
 * 	- added IM_CODING_RAD support
 * 21/10/09
 * 	- allow sub to be outside main
 * 	- gtkdoc
 * 6/3/10
 * 	- don't im_invalidate() after paint, this now needs to be at a higher
 * 	  level
 * 25/8/10
 * 	- cast and bandalike sub to main
 * 22/9/10
 * 	- rename to im_draw_image()
 * 	- gtk-doc
 * 9/2/14
 * 	- redo as a class, based on draw_image
 * 28/3/14
 * 	- add "mode" param
 */

/*

    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

 */

#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 <limits.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "pdraw.h"

typedef struct _VipsDrawImage {
	VipsDraw parent_object;

	/* Parameters.
	 */
	VipsImage *sub;
	int x;
	int y;
	VipsCombineMode mode; 

} VipsDrawImage;

typedef struct _VipsDrawImageClass {
	VipsDrawClass parent_class;

} VipsDrawImageClass; 

G_DEFINE_TYPE( VipsDrawImage, vips_draw_image, VIPS_TYPE_DRAW );

#define LOOP( TYPE, TEMP, MIN, MAX ) { \
	TYPE * restrict pt = (TYPE *) p; \
	TYPE * restrict qt = (TYPE *) q; \
	\
	for( x = 0; x < sz; x++ ) { \
		TEMP v; \
		\
		v = pt[x] + qt[x]; \
		\
		qt[x] = VIPS_CLIP( MIN, v, MAX ); \
	} \
}

#define LOOPF( TYPE ) { \
	TYPE * restrict pt = (TYPE *) p; \
	TYPE * restrict qt = (TYPE *) q; \
	\
	for( x = 0; x < sz; x++ ) \
		qt[x] += pt[x]; \
}

static void
vips_draw_image_mode_add( VipsDrawImage *draw_image, VipsImage *im, 
	VipsPel *q, VipsPel *p, int n )
{
	/* Complex just doubles the size.
	 */
	const int sz = n * im->Bands * 
		(vips_band_format_iscomplex( im->BandFmt ) ?  2 : 1);

	int x;

	switch( im->BandFmt ) {
	case VIPS_FORMAT_UCHAR: 	
		LOOP( unsigned char, int, 0, UCHAR_MAX ); break; 
	case VIPS_FORMAT_CHAR: 	
		LOOP( signed char, int, SCHAR_MIN, SCHAR_MAX ); break; 
	case VIPS_FORMAT_USHORT: 
		LOOP( unsigned short, int, 0, USHRT_MAX ); break; 
	case VIPS_FORMAT_SHORT: 	
		LOOP( signed short, int, SCHAR_MIN, SCHAR_MAX ); break; 
	case VIPS_FORMAT_UINT: 	
		LOOP( unsigned int, gint64, 0, UINT_MAX ); break; 
	case VIPS_FORMAT_INT: 	
		LOOP( signed int, gint64, INT_MIN, INT_MAX ); break; 

	case VIPS_FORMAT_FLOAT: 		
	case VIPS_FORMAT_COMPLEX: 
		LOOPF( float ); break; 

	case VIPS_FORMAT_DOUBLE:	
	case VIPS_FORMAT_DPCOMPLEX: 
		LOOPF( double ); break;

	default:
		g_assert_not_reached();
	}
}

static int
vips_draw_image_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsDraw *draw = VIPS_DRAW( object );
	VipsDrawImage *draw_image = (VipsDrawImage *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array( object, 3 );

	VipsImage *im;
	VipsRect image_rect;
	VipsRect sub_rect; 
	VipsRect clip_rect;

	if( VIPS_OBJECT_CLASS( vips_draw_image_parent_class )->build( object ) )
		return( -1 );

	if( vips_check_coding_known( class->nickname, draw->image ) ||
		vips_check_coding_same( class->nickname, 
			draw->image, draw_image->sub ) ||
		vips_check_bands_1orn_unary( class->nickname, 
			draw_image->sub, draw->image->Bands ) )
		return( -1 );

	/* SET will work for any matching coding, but every other mode needs 
	 * uncoded images. 
	 */
	if( draw_image->mode != VIPS_COMBINE_MODE_SET &&
		vips_check_uncoded( class->nickname, draw->image ) )
		return( -1 ); 

	/* Cast sub to match main in bands and format.
	 */
	im = draw_image->sub;
	if( im->Coding == VIPS_CODING_NONE ) {
		if( vips__bandup( class->nickname, 
			im, &t[0], draw->image->Bands ) ||
			vips_cast( t[0], &t[1], draw->image->BandFmt, NULL ) )
			return( -1 );

		im = t[1];
	}

	/* Make rects for main and sub and clip.
	 */
	image_rect.left = 0;
	image_rect.top = 0;
	image_rect.width = draw->image->Xsize;
	image_rect.height = draw->image->Ysize;
	sub_rect.left = draw_image->x;
	sub_rect.top = draw_image->y;
	sub_rect.width = im->Xsize;
	sub_rect.height = im->Ysize;
	vips_rect_intersectrect( &image_rect, &sub_rect, &clip_rect );

	if( !vips_rect_isempty( &clip_rect ) ) {
		VipsPel *p, *q;
		int y;

		if( vips_image_wio_input( im ) )
			return( -1 ); 

		p = VIPS_IMAGE_ADDR( im, 
			clip_rect.left - draw_image->x, 
			clip_rect.top - draw_image->y );
		q = VIPS_IMAGE_ADDR( draw->image, 
			clip_rect.left, clip_rect.top );

		for( y = 0; y < clip_rect.height; y++ ) {
			switch( draw_image->mode ) {
			case VIPS_COMBINE_MODE_SET:
				memcpy( (char *) q, (char *) p, 
					clip_rect.width * 
						VIPS_IMAGE_SIZEOF_PEL( im ) );
				break;

			case VIPS_COMBINE_MODE_ADD:
				vips_draw_image_mode_add( draw_image, 
					im, q, p, clip_rect.width ); 
				break;

			default:
				g_assert_not_reached();
			}

			p += VIPS_IMAGE_SIZEOF_LINE( im );
			q += VIPS_IMAGE_SIZEOF_LINE( draw->image );
		}
	}

	return( 0 );
}

static void
vips_draw_image_class_init( VipsDrawImageClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	vobject_class->nickname = "draw_image";
	vobject_class->description = _( "paint an image into another image" );
	vobject_class->build = vips_draw_image_build;

	VIPS_ARG_IMAGE( class, "sub", 5, 
		_( "Sub-image" ), 
		_( "Sub-image to insert into main image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsDrawImage, sub ) );

	VIPS_ARG_INT( class, "x", 6, 
		_( "x" ), 
		_( "Draw image here" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsDrawImage, x ),
		-1000000000, 1000000000, 0 );

	VIPS_ARG_INT( class, "y", 7, 
		_( "y" ), 
		_( "Draw image here" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsDrawImage, y ),
		-1000000000, 1000000000, 0 );

	VIPS_ARG_ENUM( class, "mode", 8, 
		_( "Mode" ), 
		_( "Combining mode" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsDrawImage, mode ),
		VIPS_TYPE_COMBINE_MODE, VIPS_COMBINE_MODE_SET ); 

}

static void
vips_draw_image_init( VipsDrawImage *draw_image )
{
	draw_image->mode = VIPS_COMBINE_MODE_SET;
}

/**
 * vips_draw_image:
 * @image: image to draw on
 * @sub: image to paint
 * @x: draw @sub here
 * @y: draw @sub here
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @mode: how to combine pixels 
 *
 * Draw @sub on top of @image at position @x, @y. The two images must have the 
 * same Coding. If @sub has 1 band, the bands will be duplicated to match the
 * number of bands in @image. @sub will be converted to @image's format, see
 * vips_cast().
 *
 * Use @mode to set how pixels are combined. If you use 
 * #VIPS_COMBINE_MODE_ADD, both images muct be uncoded. 
 *
 * See also: vips_draw_mask(), vips_insert().
 *
 * Returns: 0 on success, or -1 on error.
 */
int
vips_draw_image( VipsImage *image, VipsImage *sub, int x, int y, ... )
{
	va_list ap;
	int result;

	va_start( ap, y );
	result = vips_call_split( "draw_image", ap, image, sub, x, y );
	va_end( ap );

	return( result );
}