user3814700
user3814700

Reputation: 37

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.

Upvotes: 1

Views: 4383

Answers (2)

luser droog
luser droog

Reputation: 19484

You mention in the comments "I stored the image to the hard disk of the printer." But it would appear that the postscript interpreter cannot access it, or uses a different path, or even a different path naming convention. Using the bare filename will work with ghostscript or ghostview if launched from the same directory, so the running program has an environment variable CWD for current working directory. As an aside, it appears to be a clone interpreter, since it throws /undefined instead of /undefinedfilename.

Accessing a printer's disk from a postscript program is going to be very product-specific.

But the method Ken describes will bypass any of these problems.

Using currentfile as the data source means the image operator will read data directly from the postscript program file. So you need to embed the data directly into the program source code text. You can either use your text editor to read-in the file, or maybe some kind of macro-processor with inclusion capabilities.


A much easier way is to use ghostscript to produce a printable file. ps2pdf or ps2ps (possibly with -DNOSAFER) will process the program and distill the output.

Upvotes: 0

KenS
KenS

Reputation: 31141

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.

Upvotes: 3

Related Questions