Skip to content

Commit 55e525a

Browse files
committed
[skip ci] Try to debug John's reference implementation
1 parent ba5a210 commit 55e525a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

libvips/resample/shrinkh.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ vips_shrinkh_build( VipsObject *object )
237237
vips_object_local_array( object, 2 );
238238

239239
VipsImage *in;
240+
int width, extra_pixels;
241+
double left;
240242

241243
if( VIPS_OBJECT_CLASS( vips_shrinkh_parent_class )->build( object ) )
242244
return( -1 );
@@ -252,11 +254,31 @@ vips_shrinkh_build( VipsObject *object )
252254
if( shrink->hshrink == 1 )
253255
return( vips_image_write( in, resample->out ) );
254256

257+
/* Output size. We need to always round to nearest, so round(), not
258+
* rint().
259+
*/
260+
width = VIPS_ROUND_UINT(
261+
(double) resample->in->Xsize / shrink->hshrink );
262+
263+
/* How many pixels we are inventing in the input, -ve for
264+
* discarding.
265+
*/
266+
extra_pixels =
267+
width * shrink->hshrink - resample->in->Xsize;
268+
269+
/* If we are rounding down, we are not using some input
270+
* pixels. We need to move the origin *inside* the input image
271+
* by half that distance so that we discard pixels equally
272+
* from left and right.
273+
*/
274+
left = (1 + extra_pixels) / 2.0;
275+
printf( "hshrink: %d, left: %f\n", shrink->hshrink, left );
276+
255277
/* We need new pixels at the right so that we don't have small chunks
256278
* to average down the right edge.
257279
*/
258280
if( vips_embed( in, &t[1],
259-
0, 0,
281+
left, 0,
260282
in->Xsize + shrink->hshrink, in->Ysize,
261283
"extend", VIPS_EXTEND_COPY,
262284
NULL ) )
@@ -274,8 +296,7 @@ vips_shrinkh_build( VipsObject *object )
274296
* example, vipsthumbnail knows the true shrink factor (including the
275297
* fractional part), we just see the integer part here.
276298
*/
277-
resample->out->Xsize = VIPS_ROUND_UINT(
278-
(double) resample->in->Xsize / shrink->hshrink );
299+
resample->out->Xsize = width;
279300
if( resample->out->Xsize <= 0 ) {
280301
vips_error( class->nickname,
281302
"%s", _( "image has shrunk to nothing" ) );

libvips/resample/shrinkv.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ vips_shrinkv_build( VipsObject *object )
332332
vips_object_local_array( object, 4 );
333333

334334
VipsImage *in;
335+
int height, extra_pixels;
336+
double top;
335337

336338
if( VIPS_OBJECT_CLASS( vips_shrinkv_parent_class )->build( object ) )
337339
return( -1 );
@@ -347,11 +349,31 @@ vips_shrinkv_build( VipsObject *object )
347349
if( shrink->vshrink == 1 )
348350
return( vips_image_write( in, resample->out ) );
349351

352+
/* Output size. We need to always round to nearest, so round(), not
353+
* rint().
354+
*/
355+
height = VIPS_ROUND_UINT(
356+
(double) resample->in->Ysize / shrink->vshrink );
357+
358+
/* How many pixels we are inventing in the input, -ve for
359+
* discarding.
360+
*/
361+
extra_pixels =
362+
height * shrink->vshrink - resample->in->Ysize;
363+
364+
/* If we are rounding down, we are not using some input
365+
* pixels. We need to move the origin *inside* the input image
366+
* by half that distance so that we discard pixels equally
367+
* from left and right.
368+
*/
369+
top = (1 + extra_pixels) / 2.0;
370+
printf( "vshrink: %d, top: %f\n", shrink->vshrink, top );
371+
350372
/* Make the height a multiple of the shrink factor so we don't need to
351373
* average half pixels.
352374
*/
353375
if( vips_embed( in, &t[1],
354-
0, 0,
376+
0, top,
355377
in->Xsize, VIPS_ROUND_UP( in->Ysize, shrink->vshrink ),
356378
"extend", VIPS_EXTEND_COPY,
357379
NULL ) )

0 commit comments

Comments
 (0)