1 /* vertical shrink with a box filter
2  *
3  * Copyright: 1990, N. Dessipris.
4  *
5  * Authors: Nicos Dessipris and Kirk Martinez
6  * Written on: 29/04/1991
7  * Modified on: 2/11/92, 22/2/93 Kirk Martinez - Xres Yres & cleanup
8  incredibly inefficient for box filters as LUTs are used instead of +
9  Needs converting to a smoother filter: eg Gaussian!  KM
10  * 15/7/93 JC
11  *	- rewritten for partial v2
12  *	- ANSIfied
13  *	- now shrinks any non-complex type
14  *	- no longer cloned from im_convsub()
15  *	- could be much better! see km comments above
16  * 3/8/93 JC
17  *	- rounding bug fixed
18  * 11/1/94 JC
19  *	- problems with .000001 and round up/down ignored! Try shrink 3738
20  *	  pixel image by 9.345000000001
21  * 7/10/94 JC
22  *	- IM_NEW and IM_ARRAY added
23  *	- more typedef
24  * 3/7/95 JC
25  *	- IM_CODING_LABQ handling added here
26  * 20/12/08
27  * 	- fall back to im_copy() for 1/1 shrink
28  * 2/2/11
29  * 	- gtk-doc
30  * 10/2/12
31  * 	- shrink in chunks to reduce peak memuse for large shrinks
32  * 	- simpler
33  * 12/6/12
34  * 	- redone as a class
35  * 	- warn about non-int shrinks
36  * 	- some tuning .. tried an int coordinate path, not worthwhile
37  * 16/11/12
38  * 	- don't change xres/yres, see comment below
39  * 8/4/13
40  * 	- oops demand_hint was incorrect, thanks Jan
41  * 6/6/13
42  * 	- don't chunk horizontally, fixes seq problems with large shrink
43  * 	  factors
44  * 15/8/16
45  * 	- rename yshrink -> vshrink for greater consistency
46  * 7/3/17
47  * 	- add a seq line cache
48  * 6/8/19
49  * 	- use a double sum buffer for int32 types
50  */
51 
52 /*
53 
54     This file is part of VIPS.
55 
56     VIPS is free software; you can redistribute it and/or modify
57     it under the terms of the GNU Lesser General Public License as published by
58     the Free Software Foundation; either version 2 of the License, or
59     (at your option) any later version.
60 
61     This program is distributed in the hope that it will be useful,
62     but WITHOUT ANY WARRANTY; without even the implied warranty of
63     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64     GNU Lesser General Public License for more details.
65 
66     You should have received a copy of the GNU Lesser General Public License
67     along with this program; if not, write to the Free Software
68     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
69     02110-1301  USA
70 
71  */
72 
73 /*
74 
75     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
76 
77  */
78 
79 /*
80 #define DEBUG
81  */
82 
83 #ifdef HAVE_CONFIG_H
84 #include <config.h>
85 #endif /*HAVE_CONFIG_H*/
86 #include <vips/intl.h>
87 
88 #include <stdio.h>
89 #include <string.h>
90 #include <stdlib.h>
91 #include <math.h>
92 
93 #include <vips/vips.h>
94 #include <vips/debug.h>
95 #include <vips/internal.h>
96 
97 #include "presample.h"
98 
99 typedef struct _VipsShrinkv {
100 	VipsResample parent_instance;
101 
102 	int vshrink;
103 	size_t sizeof_line_buffer;
104 
105 } VipsShrinkv;
106 
107 typedef VipsResampleClass VipsShrinkvClass;
108 
109 G_DEFINE_TYPE( VipsShrinkv, vips_shrinkv, VIPS_TYPE_RESAMPLE );
110 
111 /* Our per-sequence parameter struct. Somewhere to sum band elements.
112  */
113 typedef struct {
114 	VipsRegion *ir;
115 
116 	VipsPel *sum;
117 } VipsShrinkvSequence;
118 
119 /* Free a sequence value.
120  */
121 static int
vips_shrinkv_stop(void * vseq,void * a,void * b)122 vips_shrinkv_stop( void *vseq, void *a, void *b )
123 {
124 	VipsShrinkvSequence *seq = (VipsShrinkvSequence *) vseq;
125 
126 	VIPS_FREEF( g_object_unref, seq->ir );
127 	VIPS_FREE( seq->sum );
128 	VIPS_FREE( seq );
129 
130 	return( 0 );
131 }
132 
133 /* Make a sequence value.
134  */
135 static void *
vips_shrinkv_start(VipsImage * out,void * a,void * b)136 vips_shrinkv_start( VipsImage *out, void *a, void *b )
137 {
138 	VipsImage *in = (VipsImage *) a;
139 	VipsShrinkv *shrink = (VipsShrinkv *) b;
140 	VipsShrinkvSequence *seq;
141 
142 	if( !(seq = VIPS_NEW( NULL, VipsShrinkvSequence )) )
143 		return( NULL );
144 
145 	seq->ir = vips_region_new( in );
146 
147 	/* Big enough for the largest intermediate .. a whole scanline.
148 	 */
149 	seq->sum = VIPS_ARRAY( NULL, shrink->sizeof_line_buffer, VipsPel );
150 
151 	return( (void *) seq );
152 }
153 
154 #define ADD( ACC_TYPE, TYPE ) { \
155 	ACC_TYPE * restrict sum = (ACC_TYPE *) seq->sum; \
156 	TYPE * restrict p = (TYPE *) in; \
157 	\
158 	for( x = 0; x < sz; x++ ) \
159 		sum[x] += p[x]; \
160 }
161 
162 /* Add a line of pixels to sum.
163  */
164 static void
vips_shrinkv_add_line(VipsShrinkv * shrink,VipsShrinkvSequence * seq,VipsRegion * ir,int left,int top,int width)165 vips_shrinkv_add_line( VipsShrinkv *shrink, VipsShrinkvSequence *seq,
166 	VipsRegion *ir, int left, int top, int width )
167 {
168 	VipsResample *resample = VIPS_RESAMPLE( shrink );
169 	const int bands = resample->in->Bands *
170 		(vips_band_format_iscomplex( resample->in->BandFmt ) ?
171 		 	2 : 1);
172 	const int sz = bands * width;
173 
174 	int x;
175 
176 	VipsPel *in = VIPS_REGION_ADDR( ir, left, top );
177 	switch( resample->in->BandFmt ) {
178 	case VIPS_FORMAT_UCHAR:
179 		ADD( int, unsigned char ); break;
180 	case VIPS_FORMAT_CHAR:
181 		ADD( int, char ); break;
182 	case VIPS_FORMAT_USHORT:
183 		ADD( int, unsigned short ); break;
184 	case VIPS_FORMAT_SHORT:
185 		ADD( int, short ); break;
186 	case VIPS_FORMAT_UINT:
187 		ADD( double, unsigned int ); break;
188 	case VIPS_FORMAT_INT:
189 		ADD( double, int );  break;
190 	case VIPS_FORMAT_FLOAT:
191 		ADD( double, float ); break;
192 	case VIPS_FORMAT_DOUBLE:
193 		ADD( double, double ); break;
194 	case VIPS_FORMAT_COMPLEX:
195 		ADD( double, float ); break;
196 	case VIPS_FORMAT_DPCOMPLEX:
197 		ADD( double, double ); break;
198 
199 	default:
200 		g_assert_not_reached();
201 	}
202 }
203 
204 /* Integer average.
205  */
206 #define IAVG( ACC_TYPE, TYPE ) { \
207 	ACC_TYPE * restrict sum = (ACC_TYPE *) seq->sum; \
208 	TYPE * restrict q = (TYPE *) out; \
209 	\
210 	for( x = 0; x < sz; x++ ) \
211 		q[x] = (sum[x] + shrink->vshrink / 2) / shrink->vshrink; \
212 }
213 
214 /* Float average.
215  */
216 #define FAVG( TYPE ) { \
217 	double * restrict sum = (double *) seq->sum; \
218 	TYPE * restrict q = (TYPE *) out; \
219 	\
220 	for( x = 0; x < sz; x++ ) \
221 		q[x] = sum[x] / shrink->vshrink; \
222 }
223 
224 /* Average the line of sums to out.
225  */
226 static void
vips_shrinkv_write_line(VipsShrinkv * shrink,VipsShrinkvSequence * seq,VipsRegion * or,int left,int top,int width)227 vips_shrinkv_write_line( VipsShrinkv *shrink, VipsShrinkvSequence *seq,
228 	VipsRegion *or, int left, int top, int width )
229 {
230 	VipsResample *resample = VIPS_RESAMPLE( shrink );
231 	const int bands = resample->in->Bands *
232 		(vips_band_format_iscomplex( resample->in->BandFmt ) ?
233 		 	2 : 1);
234 	const int sz = bands * width;
235 
236 	int x;
237 
238 	VipsPel *out = VIPS_REGION_ADDR( or, left, top );
239 	switch( resample->in->BandFmt ) {
240 	case VIPS_FORMAT_UCHAR:
241 		IAVG( int, unsigned char ); break;
242 	case VIPS_FORMAT_CHAR:
243 		IAVG( int, char ); break;
244 	case VIPS_FORMAT_USHORT:
245 		IAVG( int, unsigned short ); break;
246 	case VIPS_FORMAT_SHORT:
247 		IAVG( int, short ); break;
248 	case VIPS_FORMAT_UINT:
249 		IAVG( double, unsigned int ); break;
250 	case VIPS_FORMAT_INT:
251 		IAVG( double, int );  break;
252 	case VIPS_FORMAT_FLOAT:
253 		FAVG( float ); break;
254 	case VIPS_FORMAT_DOUBLE:
255 		FAVG( double ); break;
256 	case VIPS_FORMAT_COMPLEX:
257 		FAVG( float ); break;
258 	case VIPS_FORMAT_DPCOMPLEX:
259 		FAVG( double ); break;
260 
261 	default:
262 		g_assert_not_reached();
263 	}
264 }
265 
266 static int
vips_shrinkv_gen(VipsRegion * or,void * vseq,void * a,void * b,gboolean * stop)267 vips_shrinkv_gen( VipsRegion *or, void *vseq,
268 	void *a, void *b, gboolean *stop )
269 {
270 	VipsShrinkvSequence *seq = (VipsShrinkvSequence *) vseq;
271 	VipsShrinkv *shrink = (VipsShrinkv *) b;
272 	VipsRegion *ir = seq->ir;
273 	VipsRect *r = &or->valid;
274 
275 	int y, y1;
276 
277 	/* How do we chunk up the image? We don't want to prepare the whole of
278 	 * the input region corresponding to *r since it could be huge.
279 	 *
280 	 * Request input a line at a time, average to a line buffer.
281 	 */
282 
283 #ifdef DEBUG
284 	printf( "vips_shrinkv_gen: generating %d x %d at %d x %d\n",
285 		r->width, r->height, r->left, r->top );
286 #endif /*DEBUG*/
287 
288 	for( y = 0; y < r->height; y++ ) {
289 		memset( seq->sum, 0, shrink->sizeof_line_buffer );
290 
291 		for( y1 = 0; y1 < shrink->vshrink; y1++ ) {
292 			VipsRect s;
293 
294 			s.left = r->left;
295 			s.top = y1 + (y + r->top) * shrink->vshrink;
296 			s.width = r->width;
297 			s.height = 1;
298 #ifdef DEBUG
299 			printf( "shrink_gen: requesting line %d\n", s.top );
300 #endif /*DEBUG*/
301 			if( vips_region_prepare( ir, &s ) )
302 				return( -1 );
303 
304 			VIPS_GATE_START( "vips_shrinkv_gen: work" );
305 
306 			vips_shrinkv_add_line( shrink, seq, ir,
307 				s.left, s.top, s.width );
308 
309 			VIPS_GATE_STOP( "vips_shrinkv_gen: work" );
310 		}
311 
312 		VIPS_GATE_START( "vips_shrinkv_gen: work" );
313 
314 		vips_shrinkv_write_line( shrink, seq, or,
315 			r->left, r->top + y, r->width );
316 
317 		VIPS_GATE_STOP( "vips_shrinkv_gen: work" );
318 	}
319 
320 	VIPS_COUNT_PIXELS( or, "vips_shrinkv_gen" );
321 
322 	return( 0 );
323 }
324 
325 static int
vips_shrinkv_build(VipsObject * object)326 vips_shrinkv_build( VipsObject *object )
327 {
328 	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
329 	VipsResample *resample = VIPS_RESAMPLE( object );
330 	VipsShrinkv *shrink = (VipsShrinkv *) object;
331 	VipsImage **t = (VipsImage **)
332 		vips_object_local_array( object, 4 );
333 
334 	VipsImage *in;
335 
336 	if( VIPS_OBJECT_CLASS( vips_shrinkv_parent_class )->build( object ) )
337 		return( -1 );
338 
339 	in = resample->in;
340 
341 	if( shrink->vshrink < 1 ) {
342 		vips_error( class->nickname,
343 			"%s", _( "shrink factors should be >= 1" ) );
344 		return( -1 );
345 	}
346 
347 	if( shrink->vshrink == 1 )
348 		return( vips_image_write( in, resample->out ) );
349 
350 	/* Make the height a multiple of the shrink factor so we don't need to
351 	 * average half pixels.
352 	 */
353 	if( vips_embed( in, &t[1],
354 		0, 0,
355 		in->Xsize, VIPS_ROUND_UP( in->Ysize, shrink->vshrink ),
356 		"extend", VIPS_EXTEND_COPY,
357 		NULL ) )
358 		return( -1 );
359 	in = t[1];
360 
361 	/* We have to keep a line buffer as we sum columns.
362 	 */
363 	shrink->sizeof_line_buffer =
364 		in->Xsize * in->Bands *
365 		vips_format_sizeof( VIPS_FORMAT_DPCOMPLEX );
366 
367 	/* SMALLTILE or we'll need huge input areas for our output. In seq
368 	 * mode, the linecache above will keep us sequential.
369 	 */
370 	t[2] = vips_image_new();
371 	if( vips_image_pipelinev( t[2],
372 		VIPS_DEMAND_STYLE_SMALLTILE, in, NULL ) )
373 		return( -1 );
374 
375 	/* Size output. We need to always round to nearest, so round(), not
376 	 * rint().
377 	 *
378 	 * Don't change xres/yres, leave that to the application layer. For
379 	 * example, vipsthumbnail knows the true shrink factor (including the
380 	 * fractional part), we just see the integer part here.
381 	 */
382 	t[2]->Ysize = VIPS_ROUND_UINT(
383 		(double) resample->in->Ysize / shrink->vshrink );
384 	if( t[2]->Ysize <= 0 ) {
385 		vips_error( class->nickname,
386 			"%s", _( "image has shrunk to nothing" ) );
387 		return( -1 );
388 	}
389 
390 #ifdef DEBUG
391 	printf( "vips_shrinkv_build: shrinking %d x %d image to %d x %d\n",
392 		in->Xsize, in->Ysize,
393 		t[2]->Xsize, t[2]->Ysize );
394 #endif /*DEBUG*/
395 
396 	if( vips_image_generate( t[2],
397 		vips_shrinkv_start, vips_shrinkv_gen, vips_shrinkv_stop,
398 		in, shrink ) )
399 		return( -1 );
400 
401 	in = t[2];
402 
403 	/* Large vshrinks will throw off sequential mode. Suppose thread1 is
404 	 * generating tile (0, 0), but stalls. thread2 generates tile
405 	 * (0, 1), 128 lines further down the output. After it has done,
406 	 * thread1 tries to generate (0, 0), but by then the pixels it needs
407 	 * have gone from the input image line cache if the vshrink is large.
408 	 *
409 	 * To fix this, put another seq on the output of vshrink. Now we'll
410 	 * always have the previous XX lines of the shrunk image, and we won't
411 	 * fetch out of order.
412 	 */
413 	if( vips_image_is_sequential( in ) ) {
414 		g_info( "shrinkv sequential line cache" );
415 
416 		if( vips_sequential( in, &t[3],
417 			"tile_height", 10,
418 			NULL ) )
419 			return( -1 );
420 		in = t[3];
421 	}
422 
423 	if( vips_image_write( in, resample->out ) )
424 		return( -1 );
425 
426 	return( 0 );
427 }
428 
429 static void
vips_shrinkv_class_init(VipsShrinkvClass * class)430 vips_shrinkv_class_init( VipsShrinkvClass *class )
431 {
432 	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
433 	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
434 	VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
435 
436 	VIPS_DEBUG_MSG( "vips_shrinkv_class_init\n" );
437 
438 	gobject_class->set_property = vips_object_set_property;
439 	gobject_class->get_property = vips_object_get_property;
440 
441 	vobject_class->nickname = "shrinkv";
442 	vobject_class->description = _( "shrink an image vertically" );
443 	vobject_class->build = vips_shrinkv_build;
444 
445 	operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
446 
447 	VIPS_ARG_INT( class, "vshrink", 9,
448 		_( "Vshrink" ),
449 		_( "Vertical shrink factor" ),
450 		VIPS_ARGUMENT_REQUIRED_INPUT,
451 		G_STRUCT_OFFSET( VipsShrinkv, vshrink ),
452 		1, 1000000, 1 );
453 
454 	/* The old name .. now use h and v everywhere.
455 	 */
456 	VIPS_ARG_INT( class, "yshrink", 9,
457 		_( "Yshrink" ),
458 		_( "Vertical shrink factor" ),
459 		VIPS_ARGUMENT_REQUIRED_INPUT | VIPS_ARGUMENT_DEPRECATED,
460 		G_STRUCT_OFFSET( VipsShrinkv, vshrink ),
461 		1, 1000000, 1 );
462 
463 }
464 
465 static void
vips_shrinkv_init(VipsShrinkv * shrink)466 vips_shrinkv_init( VipsShrinkv *shrink )
467 {
468 }
469 
470 /**
471  * vips_shrinkv: (method)
472  * @in: input image
473  * @out: (out): output image
474  * @vshrink: vertical shrink
475  * @...: %NULL-terminated list of optional named arguments
476  *
477  * Shrink @in vertically by an integer factor. Each pixel in the output is
478  * the average of the corresponding column of @vshrink pixels in the input.
479  *
480  * This is a very low-level operation: see vips_resize() for a more
481  * convenient way to resize images.
482  *
483  * This operation does not change xres or yres. The image resolution needs to
484  * be updated by the application.
485  *
486  * See also: vips_shrinkh(), vips_shrink(), vips_resize(), vips_affine().
487  *
488  * Returns: 0 on success, -1 on error
489  */
490 int
vips_shrinkv(VipsImage * in,VipsImage ** out,int vshrink,...)491 vips_shrinkv( VipsImage *in, VipsImage **out, int vshrink, ... )
492 {
493 	va_list ap;
494 	int result;
495 
496 	va_start( ap, vshrink );
497 	result = vips_call_split( "shrinkv", ap, in, out, vshrink );
498 	va_end( ap );
499 
500 	return( result );
501 }
502