Bug 687394 - Images not downsampled at some resolutions
Summary: Images not downsampled at some resolutions
Status: NOTIFIED DUPLICATE of bug 493348
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PDF Writer (show other bugs)
Version: 8.14
Hardware: PC Windows XP
: P2 enhancement
Assignee: Ray Johnston
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-03-30 04:58 UTC by Russell Lang
Modified: 2013-04-03 14:24 UTC (History)
2 users (show)

See Also:
Customer: 1110
Word Size: ---


Attachments
PostScript file to draw image at about 300dpi (433 bytes, application/postscript)
2004-03-30 04:59 UTC, Russell Lang
Details
Script to run ghostscript (702 bytes, text/plain)
2004-03-30 05:00 UTC, Russell Lang
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Russell Lang 2004-03-30 04:58:23 UTC
The distiller parameter documentation for ColorImageDownsampleThreshold isn't 
particularly well worded, but my understanding is that if 
ColorImageResolution=150 and ColorImageDownsampleThreshold=1.5, then any 
images with resolution greater than 150*1.5=225dpi will be downsampled to 
150dpi. This is consistent with the user interface of Distiller.

I've found that an image with resolution of 300dpi is downsampled to 150dpi as 
expected when using threshold 1.0 to 1.9.  An image with a fractionally 
smaller resolution is not downsampled.  Attached is an example PostScript file 
and a script to process it.  Change the "scale" line in the PostScript file to 
get different results.

With "72 36 scale", the image is downsampled.
With "72.00001 36 scale", the image is NOT downsampled.
Comment 1 Russell Lang 2004-03-30 04:59:36 UTC
Created attachment 597 [details]
PostScript file to draw image at about 300dpi
Comment 2 Russell Lang 2004-03-30 05:00:11 UTC
Created attachment 598 [details]
Script to run ghostscript
Comment 3 Jack Moffitt 2004-04-01 06:40:39 UTC
Customer #1110 has reported that they've been experiencing this bug for a while
but not gotten around to reporting it.

Customer claims that when making PDF, no scaling is done unless the
image's original resolution is an integer multiple of the desired
resolution.
Comment 4 Igor Melichev 2004-04-02 03:44:22 UTC
Russell,

I guess you assume the bigger scale the bigger image resolution.
At least your example :
   %72.0 36 scale % good 
   72.00001 36 scale % bad 
looks so.
IMO GS assumes vise versa : the bigger scale, the SMALLER image dots per inch.
See gdevpsdi.c ln 319.
Does this help to your problem ? Thnx.

Another related thing is gdevpsdi.c ln 205. It truncates the factor to integers.
I guess that you and the Customer #1110 want no truncation. 
IMO it is safe to change. Do you think it would be enough ? Thnx.
Comment 5 Russell Lang 2004-04-02 05:00:50 UTC
Igor,

No, I assume that larger scale factor = smaller dpi, since the width of the 
image in pixels has not changed.  That is, the "72 scale" gives 300dpi and 
the "72.0001 scale" gives 299.999dpi.

At gdevpsdi.c:205, truncating "factor" is wrong.  This would prevent a 299.999 
dpi image from being downsampled since factor = (int)(299.99/150) = 1, which 
is less than the default 1.5 downsample threshold.  Changing just that line 
isn't sufficient to fix the problem.  Line 223 also needs to be changed to a 
float type, and so it continues.  We do need to downsample by non-integer 
factors.

The current behaviour differs from Distiller, which with a downsample 
threshold of 1.5 and a downsample resolution of 150dpi, will downsample the 
299.999 dpi image to 150dpi.

Comment 6 Igor Melichev 2004-04-02 06:30:43 UTC
Russell,

I guess you've got a technology for testing this feature, so could you please 
patch it ? If you want, assign this bug to yourself.
Comment 7 Ray Johnston 2004-04-07 10:22:39 UTC
I'm not sure whether or not pdfwrite should ever perform non-integer
downsampling, but if we do need to perform this, I will soon commit
an image filter that supports non-integer scaling. This is an
averaging filter that is intended to match Adobe Reader's image
smoothing.

When this is complete, it will be a working version of the siinterp.c
module (currently it does nothing -- a pass through filter).
Comment 8 Russell Lang 2004-04-08 02:21:33 UTC
Distiller does downsample by non-integer fractions.  With the threshold set to 
225dpi (1.5), and an input image resolution of 250dpi (86.4 43.2 scale in 
example file), distiller downsamples the width from 300pixels down to 
180pixels, a ratio of 150dpi/250dpi.

The original problem file was actually doing something like 299.9999dpi, so at 
least rounding to 300dpi instead of truncating to 299dpi would give a better 
result until the downsampling algorithmn can be updated.
Comment 9 Igor Melichev 2004-04-08 04:10:48 UTC
Russell,

When you say "an input image resolution of 250dpi", I'm not sure what does this 
mean exactly : Is this just an inverse of the image matrix ? How do you convert 
the inverse matrix into a single number ?

Since you're experimenting with this problem, maybe you know (or can get) an 
answer for the following key question :

If an image to be downsampled, what scale to be used ?
In other words, how to compute the scale from the image size, 
DownsampleTrheshold and ImageResolution ?
Comment 10 Russell Lang 2004-04-08 04:38:55 UTC
Igor, 

For the testing with 300dpi, I used
  72 36 scale
  300 150 8 [300 0 0 150 0 0] {proc} image
That is, it spreads 300 pixels over 72 points.

For the 250dpi test I used
  86.4 43.2 scale
  300 150 8 [300 0 0 150 0 0] {proc} image
So the resolution of the image is 72/86.4 * 300 = 250dpi.

This gets messy if horizontal and vertical resolutions differ.  Some checks 
with Distiller 6 show that if either horizontal or vertical resolution are 
below the threshold, do not downsample.
If (hdpi > threshold) and (vdpi > threshold), downsample so that the new hdpi 
and vdpi are the desired resolution, even if this requires different 
horizontal and vertical scale factors.

So to determine the scaling factor, I would suggest:
Take image size and matrix and calculate image_hdpi and image_vdpi.
(Don't even think about the case when the image is slanted :-)
Take the smaller of the two as image_dpi.

If image_dpi > ImageResolution*ImageDownsampleThreshold then
downsample image.  
Horizontal scale factor is ImageResolution / image_hdpi.
Vertical scale factor is ImageResolution / image_vdpi.

Comment 11 Igor Melichev 2004-04-08 06:42:47 UTC
Russell,

Thank you for the helpful analyzis.
However there is nothing to fix before the new Ray's code.
Fixing gdevpsdi.c ln 205 just forces a downsampling with the factor 1 due to 
the current downsampler handles integer factors only. Thus this change appears 
equivalent. Must wait for the Ray's patch.

Igor.

Comment 12 Ray Johnston 2004-09-20 10:33:55 UTC
Moving the bug to me until the image downsampling filter is available,
then it will move back to igor for pdfwrite implementation.
Comment 13 Ray Johnston 2005-01-25 18:08:23 UTC

*** This bug has been marked as a duplicate of 493348 ***
Comment 14 Marcos H. Woehrmann 2011-09-18 21:46:46 UTC
Changing customer bugs that have been resolved more than a year ago to closed.
Comment 15 Peter 2013-04-03 14:24:01 UTC
(In reply to comment #13)
> *** This bug has been marked as a duplicate of 493348 ***

This is not really a duplicate.  493348 is about a particular kind of downsampling (interpolation).  This bug is about downsampling only by integer scales.  Ghostscript (still) can only scale by 2.0, 3.0, etc., so anything requiring downsampling of less than 2x doesn't get downsampled at all.

This is in particular conflict with the default thresholds of 1.5.  Until non-integer scaling is supported, there needs to be some warning when non-integer thresholds are specified (since they will be rounded or ignored).

But Ghostscript should really be able to downscale by non-integer factors.