Bug 694608

Summary: Conversion of color to grayscale fails for provided postscript figure generated by gnuplot
Product: Ghostscript Reporter: Julius Smith <Bugzilla>
Component: PDF WriterAssignee: Ken Sharp <ken.sharp>
Status: RESOLVED WORKSFORME    
Severity: normal CC: Bugzilla
Priority: P4    
Version: master   
Hardware: Macintosh   
OS: MacOS X   
Customer: Word Size: ---
Attachments: gnuplot-generated postscript file triggering the gs failure to convert

Description Julius Smith 2013-09-21 13:03:31 UTC
Created attachment 10218 [details]
gnuplot-generated postscript file triggering the gs failure to convert

The attached postscript file fails to convert to grayscale using the following command in a Terminal on Mac OS X:

ps2pdf -dProcessColorModel=/DeviceGray -dColorConversionStrategy=/Gray speechspgm.eps temp.pdf

The output is 

GPL Ghostscript 9.10: 
Unable to convert color space to Gray, reverting strategy to LeaveColorUnchanged.

I cloned the latest ghostpdl/gs/ and got the same result.

While PDF was still created, the problem is serious because this is one figure in a book having hundreds of pages.  Problems include the following:

(1) Giving up on color conversion means my print-on-demand publisher (Ingram Sparks) will not accept the PDF, and it gives NO INDICATION where in the large PDF file the problem might be.  There is no actual color in the postscript being converted, so looking at the hundreds of pages of PDF gives no information about the error.

(2) FINDING the bad figure took a LONG time.  Since there is no indication of WHERE in the book eps gs got tripped up, and no verbose mode, or the like, in the gs options, I had no choice but to do a tedious binary search on my book to find the bad figure(s).  This could have been days of work if there were more than one gnuplot imagesc figure to find.  Fortunately, all other figures in the book converted ok (and all are postscript generated by various tools at various times over the past 20 or so years).

In case it might be of interest, here is the GNU Octave source for creating the figure:

% classic spectrogram:
colormap('gray'); map = colormap; imap = flipud(map);
M = round(0.02*fs);  % 20 ms window is typical
N = 2^nextpow2(8*M); % zero padding for interpolation
w = 0.54 - 0.46 * cos(2*pi*[0:M-1]/(M-1)); % w = hamming(M);
colormap(imap); % Octave wants it here
spectrogram(y,N,fs,w,-M/16,1,60); % y contains audio data
colormap(imap); % Matlab wants it here
ylim([0,(fs/2)/1000]); % don't plot neg. frequencies
print -deps -r700 './speechspgm.eps'

spectrogram plots using imagesc.  Apparently I can attach only one file, so speechspgm.eps wins, but I'll gladly attach spectrogram.m in a follow-up post if anyone wants to experiment with it.
Comment 1 Ken Sharp 2013-09-21 13:28:59 UTC
(In reply to comment #0)

> (1) Giving up on color conversion means my print-on-demand publisher (Ingram
> Sparks) will not accept the PDF, and it gives NO INDICATION where in the
> large PDF file the problem might be.

If you convert without -dBATCH or QUIET or many other switches, you wil find out which page the error occurs on. Examining the outptu and looking for the first page with colour output would also be a simple way to detect the problem.

Previously this simply fell back without warning, in my opinion a warning is to be preferred.
 
> (2) FINDING the bad figure took a LONG time.  Since there is no indication
> of WHERE in the book eps gs got tripped up, and no verbose mode, or the
> like, in the gs options, I had no choice but to do a tedious binary search
> on my book to find the bad figure(s).

There are many 'verbose' options, the simplest solution is not to use the ps2pdf script but to use GS directly.

As has been noted on may occasions, the colour code in pdfwrite is being rewritten. If you want to try the latest code you can use the switch -dPDFUseOldCMS, but there are known problems with the code in its current state, although in my opinion it is better than the old code already.
Comment 2 Ken Sharp 2013-09-21 13:34:57 UTC
That should of course have read -dPDFUseOldCMS=false The new colour code seems to work adequatley in this case.
Comment 3 Ken Sharp 2013-09-22 02:00:08 UTC
(In reply to comment #0)

> (1) Giving up on color conversion means my print-on-demand publisher (Ingram
> Sparks) will not accept the PDF, and it gives NO INDICATION where in the
> large PDF file the problem might be.  There is no actual color in the
> postscript being converted, so looking at the hundreds of pages of PDF gives
> no information about the error.

The image in question is in DeviceRGB space so it *is* in colour. It is in fact the case that for every image sample R=G=B so the result is grey scale, but we can't know that in advance, so the image is colour.

In fact the image only contains 63 different gray values, so this is a particularly dumb way to construct the image, but that's up to the PostScript generating application.

 
> (2) FINDING the bad figure took a LONG time. 

And thank you for taking the trouble to do so, many people don't.

As I said, if you convert without -dNOPAUSE you will be prompted to press return for every page, so a simple count until you reach the point with the warning would tell you which page it was.

Many PostScript programs emit a page count on stdout after each page, obviously you haven't sent the full original, so I can't tell if this is the case.

Finally you can write a custom EndPage procedure which writes the count of pages so far to stdout after every page.

 
> spectrogram plots using imagesc.  Apparently I can attach only one file, so
> speechspgm.eps wins, but I'll gladly attach spectrogram.m in a follow-up
> post if anyone wants to experiment with it.

You can only attach one file at a time, you can either attach files multiple times, or put them all in an archive and attach that (which of course has the side benefit of making the attachment smaller). However I wouldnt' be able to do anything with this file.
Comment 4 Julius Smith 2013-09-22 11:05:45 UTC
Thanks for all the excellent explanations and suggestions.  Now all four of my books are converted.  It sounds like this issue retires once and for all when -dPDFUseOldCMS=false becomes the default.