Bug 689633 - Floyd-Steinberg dithering for tiffg3, tiffg32d, and tiffg4
Summary: Floyd-Steinberg dithering for tiffg3, tiffg32d, and tiffg4
Status: NOTIFIED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: General (show other bugs)
Version: 0.00
Hardware: PC Linux
: P4 enhancement
Assignee: Default assignee
URL:
Keywords: bountiable
Depends on:
Blocks:
 
Reported: 2007-12-31 07:45 UTC by Lee Howard
Modified: 2008-12-19 08:31 UTC (History)
0 users

See Also:
Customer:
Word Size: ---


Attachments
ht_ccsto.ps (222.85 KB, application/postscript)
2007-12-31 09:32 UTC, Ray Johnston
Details
stocht_tiger.tif (494.34 KB, image/tiff)
2007-12-31 09:33 UTC, Ray Johnston
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lee Howard 2007-12-31 07:45:36 UTC
Can we please get Floyd-Steinberg dithering available in the tiffg3, tiffg32d,
and tiffg4 drivers?

This dithering is available already in, for example, the lj4dither and stcolor
drivers.

Support for -sDithering="fsmono" would be especially nice for transformations
originating with 24/16/8-bit color or greyscale image data.
Comment 1 Ray Johnston 2007-12-31 08:49:27 UTC
Marking this as bountiable, and setting Severity to 'enhancement'.
Comment 2 Ray Johnston 2007-12-31 09:30:02 UTC
As an alternative to FS or other relatively slow error diffusion, a threshold
array based stochastic mask can be used to provide similar results.

In testing this, I discovered that the 'ht_ccsto.ps' threshold arrays included
with Ghostscript were of the wrong polarity. I am attaching sample output and a
ht_ccsto.ps with the correct polarity used to generate the sample tiffg4 output.

Replace the lib/ht_ccsto.ps with the attached and test using:

gs -sDEVICE=tiffg4 -o stocht_tiger.tif stocht.ps examples/tiger.eps

Note that in general, you probably need to set HalftoneMode to 1 as in the
comment at the end of lib/stocht.ps:

<< /HalftoneMode 1 >> setuserparams

This prevents PS (or PDF) files from setting their own halftone/screening.

This can be done from the command line:
gs -sDEVICE=tiffg4 -o stocht_tiger.tif stocht.ps  -c "<< /HalftoneMode 1 >>
setuserparams" examples/tiger.eps
Comment 3 Ray Johnston 2007-12-31 09:32:05 UTC
Created attachment 3664 [details]
ht_ccsto.ps

Stochastic Threshold array with corrected polarity.
Comment 4 Ray Johnston 2007-12-31 09:33:39 UTC
Created attachment 3665 [details]
stocht_tiger.tif

Sample output
Comment 5 Ray Johnston 2007-12-31 09:35:30 UTC
A final note on the Stochastic Threshold array -- this has the advantage that it
can be used with ANY monochrome or color device without needing modifications to
the device.
Comment 6 Lee Howard 2007-12-31 11:10:32 UTC
Thank you for your attention.

Take your favorite JPEG file as "original.jpg".  I happened to be testing with
this one:

http://www.webcamshots.nl/galleries/Kate%20Beckinsale/TN_Kate%20Beckinsale%2011.JPG

I do not see much of a difference (if there is any) between this:

$ jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -r204x196 -sOutputFile=final.tif -

and this:

$ jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiff24nc -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif ht_ccsto.ps -c "<< /HalftoneMode 1 >> setuserparams" -

Whereas this (using libtiff utilities to perform the monochrome dithering) turns
out significantly better:

$ jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiff24nc -dNOPAUSE -dBATCH -r204x196 -sOutputFile=step1.tif -
$ tiff2bw step1.tif step2.tif
$ tiffdither -c g3 step2.tif final.tif
Comment 7 Ray Johnston 2007-12-31 11:21:05 UTC
Comment #6 has an incorrect invocation:

jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiff24nc -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif ht_ccsto.ps -c "<< /HalftoneMode 1 >> setuserparams" -

uses the DEVICE=tiff24NC, which is 24-bit RGB, thus Ghostscript performs no
dithering.

Please look at the output from Ghostscript directly converting to tiffg3 using:

jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif ht_ccsto.ps -c "<< /HalftoneMode 1 >> setuserparams" -

No post-conversion using:
  tiff2bw step1.tif step2.tif; tiffdither -c g3 step2.tif final.tif
should be performed since the output will already be dithered B&W.
Comment 8 Lee Howard 2007-12-31 11:56:54 UTC
I'm sorry, the misplaced tiff24nc was mere cut-n-paste typo on my part.

Yes this:

jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif ht_ccsto.ps -c "<< /HalftoneMode 1 >> setuserparams" -

Looks nearly the same (if not exactly the same) as this:

jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif -

And significantly worse than the dithering performed by libtiff's tools.
Comment 9 Ray Johnston 2007-12-31 13:26:26 UTC
Now I must apologize for seeing the one mistake and missing the other.

The ht_ccsto.ps is a replacement for the (too dark) one in the lib/ directory, but
when you invoke Ghostscript, you need to use 'stocht.ps' which loads the
ht_stocht.ps halftone resource and defines it as the DefaultHalftone.

The 'ht_ccsto.ps' file only defines a Halftone Resource 'StochasticDefault'
which stocht.ps uses.

Please try:

jpeg2ps -r 200 -q original.jpg | \
  gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -r204x196 \
  -sOutputFile=final.tif stocht.ps -c "<< /HalftoneMode 1 >> setuserparams" -

Comment 10 Lee Howard 2007-12-31 14:27:01 UTC
Okay, thank you again.  YES!  That looks very, very good.  Thank you.

Will the corrected ht_ccsto.ps be in the next Ghostscript release? (8.62?)
Comment 11 Ray Johnston 2007-12-31 14:36:49 UTC
The fixed ht_ccsto.ps is committed in rev 8469, so will be in the next GS.