Bug 694762

Summary: Impossible to turn off dithering when converting to monochrome
Product: Ghostscript Reporter: Adam Lesser <alesser>
Component: ColorAssignee: Michael Vrhel <michael.vrhel>
Status: RESOLVED WORKSFORME    
Severity: normal CC: dominic, marcin.zacharczuk
Priority: P4    
Version: master   
Hardware: All   
OS: All   
Customer: Word Size: ---

Description Adam Lesser 2013-11-07 13:24:40 UTC
When output is directed to a monochrome device, for example, with the following command:

gs -q -dSAFER -dNOPAUSE -dBATCH -dUseCropBox -sOutputFile=output.tif -r300 -sDEVICE=tiffg4 input.pdf

The product file (regardless of output format) is always dithered.  The only option available currently is -dDITHERPPI=XXX, but this is unsuitable for many uses and renders the end product unusable.

Because it is a simple way to do conversions, ghostscript should also offer a simple "threshold" option, where a threshold (typically 0-255) is set that determines which pixels are set black and which white.  In such a scheme, typically no dithering occurs, and would solve many people's problems that force them to use other slow programs (e.g. ImageMagick) to do unnecessary threshold operations on the resulting images.

For one of many examples of people struggling with this problem, please see this link:

 http://stackoverflow.com/questions/221455/ghostscript-pdf-tiff-conversion-is-awful-for-me-people-rave-about-it-i-alon
Comment 1 Adam Lesser 2013-11-07 14:19:55 UTC
It turns out you can do this if you know the right commands.. false bug report!
Comment 2 Marcin 2016-08-12 02:19:32 UTC
(In reply to Adam Lesser from comment #1)
> It turns out you can do this if you know the right commands.. false bug
> report!

Dear Adam, 
Can you or someone else share "right command"
Comment 3 Adam Lesser 2016-08-12 07:53:23 UTC
I did a LOT of research and testing in this area to find a solution that works for me.  I still think GS could make this a lot easier than the "hack" I had to come up with below..  Hopefully this analysis I wrote on the topic helps.   For others with this issue: I recommend playing around with the options below, but I found the combination of techniques in my last method (hybrid2) the best for myneeds.  

We have several options on how to fix the grayscale conversion problem. I will explain them in order, since they're a little fancy:

•	First, the method I call BNDither. This is an improvement on the dithering function by using a better dithering array (via the -Ilib stocht.ps option) that produces more visually pleasing output vs the default dithering function. This output basically results in better dithering, but doesn't turn it off. Command: /gs -q -dSAFER -dNOPAUSE -dBATCH -dUseCropBox -sOutputFile=BNDither300_%d.tif -r300 -dDITHER=300 -sDEVICE=tiffg4 -Ilib stocht.ps $FILE
•	Second, the method I call Threshold, uses a simple 50% threshold to determine if a pixel will be black or white. Dithering is turned off completely. Command: gs -q -dSAFER -dNOPAUSE -dBATCH -dUseCropBox -sOutputFile=Threshold_0.5_%d.tif -r300 -sDEVICE=tiffg4 -c "{ .5 gt { 1 } { 0 } ifelse} settransfer" -f $FILE
•	Third, a Hybrid method, that uses the above, improved dithering for everything below 50% threshold, and makes everything above 50% pure black. Command: gs -q -dSAFER -dNOPAUSE -dBATCH -dUseCropBox -sOutputFile=HYBRID_%d.tif -r300 -dDITHER=300 -sDEVICE=tiffg4 -Ilib stocht.ps -c "{ dup .5 lt { pop 0 } if } settransfer" -f $FILE
•	Fourth (best?) method called Hybrid2, improves on #3 above by adding the condition that anything "whiter" than the 70% threshold gets set to white, so that only things between 50-70% get dithered. This has the positive effect of removing a lot of page noise dithering on pages that don't scan perfectly white. It also removes annoying lite-colored backgrounds on color/gray images. Command: gs -q -dSAFER -dNOPAUSE -dBATCH -dUseCropBox -sOutputFile=HYBRID2_%d.tif -r300 -dDITHER=300 -sDEVICE=tiffg4 -Ilib stocht.ps -c "{ dup .5 lt { pop 0 } if dup .7 gt { pop 1 } if } settransfer" -f $FILE
Comment 4 Ray Johnston 2016-08-12 09:35:38 UTC
Adam has described the solution adequately, and this isn't really a bug in
the first place since gs is doing exactly what it is supposed to, and we are
not about to change the "default" settings to please a particular usage since
that would probably break things for millions of others.

Thanks to Adam for his work.

BTW, many Artifex commercial customers have been provided with similar custom
solutions in the past -- that's part of what supported customers get.
Comment 5 Dominic Raferd 2022-04-08 15:21:32 UTC
Just wanted to say thanks @Adam Lesser for great tip: I have incorporated your Hybrid2 into my pdf-compress.sh script. Only difference that I use pngmonod output, but it works the same.