@@ -237,6 +237,8 @@ vips_shrinkh_build( VipsObject *object )
237
237
vips_object_local_array ( object , 2 );
238
238
239
239
VipsImage * in ;
240
+ int width , extra_pixels ;
241
+ double left ;
240
242
241
243
if ( VIPS_OBJECT_CLASS ( vips_shrinkh_parent_class )-> build ( object ) )
242
244
return ( -1 );
@@ -252,11 +254,31 @@ vips_shrinkh_build( VipsObject *object )
252
254
if ( shrink -> hshrink == 1 )
253
255
return ( vips_image_write ( in , resample -> out ) );
254
256
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
+
255
277
/* We need new pixels at the right so that we don't have small chunks
256
278
* to average down the right edge.
257
279
*/
258
280
if ( vips_embed ( in , & t [1 ],
259
- 0 , 0 ,
281
+ left , 0 ,
260
282
in -> Xsize + shrink -> hshrink , in -> Ysize ,
261
283
"extend" , VIPS_EXTEND_COPY ,
262
284
NULL ) )
@@ -274,8 +296,7 @@ vips_shrinkh_build( VipsObject *object )
274
296
* example, vipsthumbnail knows the true shrink factor (including the
275
297
* fractional part), we just see the integer part here.
276
298
*/
277
- resample -> out -> Xsize = VIPS_ROUND_UINT (
278
- (double ) resample -> in -> Xsize / shrink -> hshrink );
299
+ resample -> out -> Xsize = width ;
279
300
if ( resample -> out -> Xsize <= 0 ) {
280
301
vips_error ( class -> nickname ,
281
302
"%s" , _ ( "image has shrunk to nothing" ) );
0 commit comments