Bug 693303

Summary: problem with redirect output of gs api to stdout
Product: Ghostscript Reporter: vg <vguedes>
Component: Client APIAssignee: Default assignee <ghostpdl-bugs>
Status: RESOLVED WONTFIX    
Severity: normal CC: alex
Priority: P4    
Version: 9.05   
Hardware: PC   
OS: Windows XP   
Customer: Word Size: ---

Description vg 2012-08-28 19:28:51 UTC
Hi,

I have a problem when I want to redirect the gs output to stdout. I need it because I want work in memory without generate file.
I used a wrapper for GS API (Gouda) on .NET.
Below the part of code (C# with wrapper but it's the same concept) :

=========================

  ConsoleStdioHandler hand = new ConsoleStdioHandler();
  
  Ghostscript.SetStdio(instance, hand.StdInCallBack, hand.StdOutCallBack, hand.StdErrCallBack);
  
  string[] gsArgs = new string[] {
      "-q",
      "-dQUIET",
      "-dSAFER",
      "-dBATCH",
      "-dNOPAUSE",
      "-dNOPROMPT",
      //"-dDEBUG",
      "-dMaxBitmap=500000000",
      "-dEPSCrop",
      "-dAlignToPixels=0",
      "-dGridFitTT=2",
      "-sDEVICE=tiff24nc",
      "-dTextAlphaBits=4",
      "-dGraphicsAlphaBits=4",
      "-r300",
      //"-sOutputFile="+testPsFile+".tif",
      "-sOutputFile=-",
      testPsFile
  };

  int init = Ghostscript.InitWithArgs(instance, gsArgs.Length, gsArgs);

=========================

This code is working fine when file is defined but don't work when is specified redirect to stdout.

Below the generated error :

=========================

II*     II*         tiff24nc: Error writing data for field "BitsPerSample".
TIFFAppendToStrip: Write error at scanline 421.
Error: /unknownerror in --showpage--
Operand stack:
   1   true
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1926   1   3   %oparray_pop   1925   1   3   %oparray_pop   --nostringval--   1809   0   3   %oparray_pop   --nost
ringval--   --nostringval--
Dictionary stack:
   --dict:1174/1684(ro)(G)--   --dict:1/20(G)--   --dict:96/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.05: Unrecoverable error, exit code 1
TIFFAppendToStrip: Write error at scanline 422.

=========================

I have the same problem with command line when i don't use pipe or redirect in the command :

gswin32c.exe -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=tiff24nc -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 "-sOutputFile=-" "test.eps" > test.tif
OK

gswin32c.exe -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=tiff24nc -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 "-sOutputFile=-" "test.eps"
KO (same error)

Have you an idea what's wrong ?
Thank for your help.

PS : sorry for my basic english :c)
Comment 1 Alex Cherepanov 2012-08-29 02:27:47 UTC
TIFF format depends on offsets to access various data tables. Usually, the
offsets are known only when all the data are consumed and compressed.
Only uncompressed files can have all the offsets calculated in advance.

Ghostscript uses libTiff to write TIFF files. libTiff is not designed
for this special case and always requires direct access to the output file. 

Changing libTiff to use sequential access to uncompressed files is difficult,
and beyond the control of Ghostscript developers.
You need to use a file format, that can be generated sequentially, for instance
PNM format, instead of TIFF.
Comment 2 vg 2012-08-29 08:35:59 UTC
Thank for your quickly answer.

Ok.
In this case, I think the generated tiff 
isn't compressed.
And I don't understand why it works when I redirect the stdout to a file or pipe to echo in command line context.


(In reply to comment #1)
> TIFF format depends on offsets to access various data tables. Usually, the
> offsets are known only when all the data are consumed and compressed.
> Only uncompressed files can have all the offsets calculated in advance.
> 
> Ghostscript uses libTiff to write TIFF files. libTiff is not designed
> for this special case and always requires direct access to the output file. 
> 
> Changing libTiff to use sequential access to uncompressed files is difficult,
> and beyond the control of Ghostscript developers.
> You need to use a file format, that can be generated sequentially, for instance
> PNM format, instead of TIFF.