Search code examples
postscripteps

How to embed JPEG image in a postscript file?


I want to embed a JPEG to a postscript file. Is there any way to embed it directly like embedding a PS file?

72 300 translate
(incuse.eps) run

I was finally able to display an image using this code in ghostscript.

newpath
25 725 moveto
0 90 rlineto
120 0 rlineto
0 -90 rlineto
-120 0 rlineto
closepath
0 0 0 setrgbcolor
1 setlinewidth
stroke

gsave
25 700 translate   
175 175 scale           
800                   
808                         
8                  
[800 0 0 -808 0 0]       
(ADSM_Logo.jpg) (r) file /DCTDecode filter 
false                 
3
colorimage
grestore


showpage

but I have an error when I print the PS file.

Error Name: /undefined Offending Command: --file-- Operand Stack:

(r)

(ADSM_Logo.jpg)

[800 0 0 -808 0 0]

8

808

800

The Square gets drawn successfully but the image doesn't appear.


Solution

  • PostScript can handle DCT, so yes. You need to use the DCTDecode filter to decompress the image DataSource.

    Look at viewjpeg.ps in Ghostscript's lib folder for a full program, but this is the basics:

    % prepare image dictionary
    << /ImageType 1
       /Width width
       /Height height
       /ImageMatrix [ width 0 0 height neg 0 height ]
       /BitsPerComponent 8
       % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
       colors 4 eq {
         /Decode [ colors { 1 0 } repeat ]
       } {
         /Decode [ colors { 0 1 } repeat ]
       } ifelse
       /DataSource F /DCTDecode filter
    >> image
    

    Update

    You are using the 'file' operator to read a file from disk, presumably you haven't stored this file on your printer's hard disk (if it even has one) so, unsurprisingly it doesn't work.

    The error even says so (more or less) 'undefined' 'Offending command file' and on the top of the operand stack is '(ADSM_Logo.jpg) (r)'

    Assuming you don't have a hard disk on your printer, you need to put the JPEG data into the PostScript program and use currentfile as the data source with a DCTDecode filter of course. You will need to learn how to use the image operator instead of colorimage and it would be better to use the dictionary form of the operator too.