Search code examples
postscriptcmyk

How do I convert CMYK (tiff) file in postscript file?


For testing purpose I want to create a postscript file from a Tiff file. The image is in CMYK mode and I don't want to convert it to RGB.

I'm on Windows, and using Ghostscript to view the postscript file. I've tried to use the colorimage operator to draw the colored image form the picture encoded in hex.


3508 2480 scale
3508 2480 8 [3508 0 0 -2480 0 2480]
{currentfile 4 3508 mul string readhexstring pop} bind
false 4 colorimage
49492a00080000001200fe00040001000000000000000001030001000000

-- More Hex Data --

I expected this to compile with ghostscript but it return an error that I can't understand.

Error: /undefined in ??3
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
  --nostringval--   --nostringval--   false   1   %stopped_push   2045   1   3   %oparray_pop   2044   1   3   %oparray_
pop   2025   1   3   %oparray_pop   1884   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostr
ingval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
Dictionary stack:
   --dict:973/1684(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Current file position is 4
GPL Ghostscript 9.26: Unrecoverable error, exit code 1


EDIT : So I succeed to extract raw binary data of a small image for test, thanks to photoshop and I wanted to create the image with each data source for each color so one source for cyan, one for magenta , ... And i got this file :

/cyan { 
zz!!*'!s8W-!s8W-!zz!!*'!s8W-!s8W-!zz!!*'!s8W-!s8W-!zz!!*'!s8W-!s8W-!
  -- More Data --
} /ASCII85Decode filter def

/magenta {
s8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!
  -- More Data --
} /ASCII85Decode filter def

/yellow {
s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!
  --More Data--
} /ASCII85Decode filter def

/black{
s8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!zzs8W-!s8W-!s8N'!
  --More Data--
} /ASCII85Decode filter def

/DeviceCMYK setcolorspace

20 20 8 [20 0 0 20 0 0]
cyan
magenta
yellow
black
true 4 colorimage

If you want the full code to test it : https://pastebin.com/6MFtw0M0

But Ghostscript keep returning me a cryptic error :

Error: /undefined in --colorimage--
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   colorimage   --nostringval--   2   %stopped_push   --nostringval--   col
orimage   colorimage   false   1   %stopped_push   2045   1   3   %oparray_pop   2044   1   3   %oparray_pop   2025   1
  3   %oparray_pop   1884   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   colo
rimage   --nostringval--   2   %stopped_push   --nostringval--   1913   10   3   %oparray_pop
Dictionary stack:
   --dict:973/1684(ro)(G)--   --dict:0/20(G)--   --dict:82/200(L)--
Current allocation mode is local
GPL Ghostscript 9.26: Unrecoverable error, exit code 1

EDIT2 : So the data of my file was incorrect I tried with a different file and it worked !


Solution

  • Since you haven't (and can't) post the entire program here, its not possible to tell you exactly what is wrong. At a guess you have the number of bytes incorrectly calculated so that Ghostscript picks up interpreting either too early (and tries to interpret the image data) or too late and can't understand the corrupted content.

    Most likely (from the error) its trying to interpret the image data, its trying to tell you that the binary value 0x03 is not defined as an operator or procedure or name.

    This is likely to be because you have just read the data directly from the TIFF file and stored it into the data stream. You can't do that, TIFF is a file format, it has a header and tags, and the data may be compressed. The colorimage operator is expecting that you will supply only the image components.

    You need to strip off the header and tags, potentially decompress the data stream, deal with offsets etc before you hand in the data.

    This answer has a link to the Google group with a PostScript program I wrote some time ago to read TIFF files. I cannot honestly recall if it will read a CMYK TIFF file, but at the very least it will give you some clues about what is going on. I'm sure the program can be modified to read a CMYK TIFF file if it doesn't already do so.

    You could run that program (pointing it to your TIFF file) through Ghostscript, using the ps2write device, and it will produce a PostScript file which will contain a CMYK image for you.

    Otherwise you could extract the image components yourself to raw binary data, and then wrap that up with your existing PostScript.