Skip to main content
Participant
July 6, 2016
Question

Generic subroutines for inserting PNG files in PostScript

  • July 6, 2016
  • 2 replies
  • 1221 views

Hi, I have a generic subroutine for inserting JPGs in PostScript:

/insertjpg {
     /ypix exch def  %height of jpeg in pixels
     /xpix exch def  %width of jpeg in pixels
     /ymeas exch def %height of jpeg in measured distance, WITHOUT unit of measurement
     /xmeas exch def %width of jpeg in measured distance, WITHOUT unit of measurement
     /ypos exch def  %y-position of jpeg
     /xpos exch def  %x-position of jpeg
     /jpg exch def   %full path/filename of jpeg, enclosed in parentheses

     gsave
     xpos ypos translate
     xmeas ymeas scale
     /DeviceRGB setcolorspace % How color values will be interpreted
     << % Start image dictionary
          /ImageType 1
          /Width xpix % Dimensions of source image
          /Height ypix
          /BitsPerComponent 8
          /Decode [0 1 0 1 0 1] % Decode color values in normal way
     /ImageMatrix [xpix 0 0 ypix neg 0 ypix] % Map unit square to source
     /DataSource jpg (r) file /DCTDecode filter
     >> % End image dictionary
     image

     xpos neg ypos translate
     xmeas pt ymeas pt scale
     grestore

} def

I'm trying to adapt the following code so I can insert PNGs, but I keep getting the error %%[ Error: ioerror; OffendingCommand: imageDistiller ]%%

/insertpng {
     /ypix exch def  %height of png in pixels
     /xpix exch def  %width of png in pixels
     /ymeas exch def %height of png in measured distance, WITHOUT unit of measurement
     /xmeas exch def %width of png in measured distance, WITHOUT unit of measurement
     /ypos exch def  %y-position of png
     /xpos exch def  %x-position of png
     /png exch def   %full path/filename of png, enclosed in parentheses

     gsave
     xpos ypos translate
     xmeas ymeas scale
     /DeviceRGB setcolorspace % How color values will be interpreted

     << % Start image dictionary
          /ImageType 1
          /Width xpix % Dimensions of source image
          /Height ypix
          /BitsPerComponent 8
          /ImageMatrix [xpix 0 0 ypix neg 0 ypix] % Map unit square to source
          /Decode [0 1 0 1 0 1] % Decode color values in normal way
          %/Interpolate true
          /ImageMask true
          /DataSource png (r) file
               %/ASCII85Decode filter
          << /Predictor 15 /Columns xpix /Colors 3 /BitsPerComponent 8 >> /FlateDecode filter
      >>
     image

     xpos neg ypos translate
     xmeas pt ymeas pt scale
     grestore
} def

I appreciate any help you can offer.

This topic has been closed for replies.

2 replies

Legend
July 19, 2016

Sorry, parse the PNG not parse the PDF. An example would not be short. Not of either thing.

Legend
July 9, 2016

PostScript happens to have a filter DCTDecode, which can read JPEG files directly. This is very convenient, but cannot be generalised.  No other raster format can be read in the same way. It is theoretically possible to parse a PNG file in PostScript (as it is a general programming language), but this is not practical. You need to parse and decompress the PDF, and then create PostScript from the raster data.

Participant
July 19, 2016

This sounds very interesting! Could you please paste a short example here to show how to do it? Thanks!