What's up with basic rasterization?
David Kastrup <dak <at> gnu.org>
2013-04-18 13:07:38 GMT
Hi, to get a hang of what settings to use for gs rendering and ps2pdf
(which is the way in which LilyPond, a note typesetter, produces PDF
from PostScript), I wrote the following short test file.
100 100 translate
matrix currentmatrix
currentstrokeadjust dup ==
2 setlinewidth 1 setlinejoin 4 { 0 0 20 30 rectstroke 30 5 translate } repeat
false setstrokeadjust
4 { 0 0 20 30 rectstroke 30 5 translate } repeat
true setstrokeadjust
4 { 0 0 20 30 rectstroke 30 5 translate } repeat
2 copy
setstrokeadjust
setmatrix
0 100 dtransform round idtransform translate
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
stroke 30 5 translate } repeat
false setstrokeadjust
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
stroke 30 5 translate } repeat
true setstrokeadjust
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
stroke 30 5 translate } repeat
setstrokeadjust
setmatrix
0 200 dtransform round idtransform translate
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
strokepath fill newpath 30 5 translate } repeat
false setstrokeadjust
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
strokepath fill newpath 30 5 translate } repeat
true setstrokeadjust
4 { 0 0 moveto 20 0 lineto 20 30 lineto 0 30 lineto closepath
strokepath fill newpath 30 5 translate } repeat
showpage
To not repeat myself, I quote from what I wrote about various runs of
this code with Ghostscript 9.07 on
<URL:http://code.google.com/p/lilypond/issues/detail?id=2658#c44>.
Bad news. I've tried some experiments to see how ps2pdf would transfer
setstrokeadjust to PDF and how it affects typesetting. Attaching the
experimental file I worked with. From left to right, there are four
staggered boxes each drawn with a) default style of strokeadjust
(printed to terminal, too) b) strokeadjust set to true c) strokeadjust
set to false. Four staggered boxes why? To get different vertical and
horizontal offsets.
There are three vertical systems. The bottom uses rectstroke, the
middle one uses the equivalent discrete strokes, the top system uses the
equivalent discrete strokes, but with strokepath fill newpath instead of
stroke.
Called with
gs -dGraphicsAlphaBits=1 test.ps
we get the straight black and white rendition. As expected, the default
of strokeadjust (left and right) looks best. Without antialiasing, the
strokepath+fill combination is perfectly equivalent to stroke according
to the PostScript specification.
Except that it isn't. There are bumps at some corners in the
strokeadjusted rectangles.
Now lets start antialiasing, first with -dGraphicsAlphaBits=2 and see
where we get.
The lower two lines look pretty much indiscriminately bad now, with
stroke adjustment not helping noticeably. The top row (using filled
paths) retains its corner bumps with strokeadjustment but gets the
thickness nicest.
Taking the default of -dGraphicsAlphaBits=4, we arrive at the version
more or less corresponding with the proposed defaults. The "stroke
adjusted" rectangles in the lower rows have been considerably _fattened_
contrasted with the not-strokeadjusted ones and are at best a tiny bit
more consistent in thickness. The top row looks pretty unimpressed in
the strokeadjusted versions (including corner bumps, but those are
smoothed to some less obvious gray level), but get irregularly thick in
the non-strokeadjusted version in the middle.
[...]
Now is that the end of it? We run ps2pdf on the file (which duly
outputs "false", telling us that the default of strokeadjustment
accessible during conversion, whatever it may be worth, is off).
There is some positioning magic in the file intended to make all
vertical starting positions be different by whole pixel numbers to make
rows comparable. This magic is resolution-dependent and does not
transfer to the PDF, so rendering differences in the two lower rows are
expected regarded the vertical positioning.
However, the results can be called nothing but appalling. When calling
gs -dGraphicsAlphaBits=1 test.pdf
on the resulting file, the lower row looks unilaterally reasonable
independent of the strokeadjust setting. The middle row (drawn with the
presumably equivalent strokes) has several uneven strokes in the left 8
squares drawn without strokeadjustment. It would appear that rectstroke
_always_ uses strokeadjustment here regardless of the setting, while the
explicit strokes obey the setting. Top row is consistently irregular.
Now what with
gs -dGraphicsAlphaBits=4 test.pdf ?
Bottom row fuzzy and consistently too thick. Middle row partly fuzzy,
with the right strokeadjusted squares being consistently fuzzy and too
thick, and the non-strokeadjustes squares having several unexpectedly
sharp borders. The top row is slightly sharper, with not much of a
noticeable difference between stroke adjustment (maybe a tiny bit
sharper/lighter) and not.
Conclusion? Ghostscript significantly changes the available information
when converting from PostScript to PDF. And even before converting to
PDF, its rasterization does not conform to PostScript rendering
specifications which require strokepath fill newpath to be equivalent to
stroke. Not even if we switch off antialiasing and stroke adjustment.
Where does that leave us? No really good idea. For direct
rasterization, I like the strokepath/fill solution with antialiasing
best, but for best print results, strokeadjustment should likely be left
at its default of "off". That drawing a rectangular path using
rectstroke gives different (and apparently automatically
stroke-adjusted) results when converted to PDF from stroking a
rectangular closed path is a catastrophic failure. I have no good
proposal how to work around this.
End of quoted material
One conclusion is that we need to stop using ps2pdf altogether and have
to write our own PDF files if we care about quality. We need to shut
out Ghostscript from as much of our production processes as possible
since people will judge the results based on the PDF, and the PDF
"downstream" from GhostScript has already suffered a significant
degradation of quality. Producing our bitmap files currently uses
Ghostscript as renderer. The version actually being used is not under
our control when considering distributions for GNU/Linux and similar.
One can improve quality quite a bit when ps2pdf is not involved and one
can play with rendering parameters. Given the overall bugginess of the
rasterization process, I am skeptical that we'll find a set of settings
that will work over a reasonable range of Ghostscript versions.
I've already reported a bug with strokeadjustment last week to the bug
tracker. There has not been any reply so far.
What parts of the big picture am I missing?
--
--
David Kastrup
_______________________________________________
gs-devel mailing list
gs-devel <at> ghostscript.com
http://ghostscript.com/cgi-bin/mailman/listinfo/gs-devel