1

I've just begining to learn postscript and I've worked my way up to creating a PDF (which should be in CMYK) and inserting an image.

However, I have searched and search the internet and downloaded as many Postscript manuals as possible without out a simple example of doing this (please correct me if I've missed any).

So, my issue is that I have a CMYK image and I would like it embedded in my PDF. However when I insert it using a simple example based on an RGB image the picture turns out negative (I have changed what I thought would be suitable for CMYK image)

Below is what I'm using as my pdf creation.

    %!
/Times-Roman findfont 14 scalefont setfont 
<< /PageSize [419.528 595.276] >> setpagedevice 
% MM to Units 
% [ W x H ] 
/DeviceCMYK setcolorspace 
% Page 1 

%
% Set the Original to be the top left
%
0 595.276 translate 
1 -1 scale 
gsave 
%
% Save this state before moving x y specifically for images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Image 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 % set the X and Y of the mask here
-8.5039 140.21007874 translate  %Top Left Coordinates of mask

/box {
   newpath
   moveto

   436.535433071 0 rlineto % width of the mask
   0 463.569448819 rlineto % height of the mask  %Top Left Coordinates
   -436.535433071 0 rlineto

   closepath
} def

0 0 box                % Set up our box
  clip                     % Clip to the box

% DO NOT reset the document to original position 
% as we want the translate to be relative to the clipped box
%
%grestore  * Do not use here
%gsave * Do not use here
%

%% Images when flipped to draw correctly are scaled UPWARDS 
%% so you need to move the x,y position to the bottom left

-1.44 621.714330709 translate % Bottom Left Cordinates

% unset the mirror or the image will be flipped!
1 -1 scale 

% scale the image
438.000944882 657.119055118 scale  %%% Need to work out size and width into Units

1825                   % number of columns per row  %width of the image %%%% IN PIXELS!
2738                   % number of rows %height of the images %%%% IN PIXELS!

8                    % bits per color channel (1, 2, 4, or 8)
[1825 0 0 -2738 0 2738]       % transform array... maps unit square to pixel
(cmyk_image.jpg) (r) file /DCTDecode filter % opens the file and filters the image data
false                 % pull channels from separate sources
4
colorimage

%%%%%
% End of Image 1
%%%%%

showpage

I have tried looking at viewjpeg.ps but I want to have the image inserted into the PS and not inserted by command line.

Many Thanks

Edited: Image in question :) (it's via wetransfer due to its size and cmyk colorspace)

cmyk_image.jpg

Edited Again:

I've adjusted the code again, with using a dictionary (based on your post KenS Simple Image Dictionary).

    << /PageSize [419.528 595.276] >> setpagedevice 
0 595.276 translate 
1 -1 scale 

-1.44 621.714330709 translate % Bottom Left Cordinates

% unset the mirror or the image will be flipped!
1 -1 scale 

% scale the image
438.000944882 657.119055118 scale  %%% Need to work out size and width into Units

/OneComponentString (cmyk_image.jpg) (r) file /DCTDecode filter  def

/OneComponentImage1
{
<<
/ImageType 1
/Width 1825
/Height 2738
/ImageMatrix [1825 0 0 -2738 0 2738]
/BitsPerComponent 8
/DataSource OneComponentString
>>
} bind def

gsave

/DeviceCMYK setcolorspace
OneComponentImage1 image
grestore
showpage

However it still is coming up negative (I've explicily leftout the /Decode [0 1] from the dictionary as this was throwing up the following error:

    Error: /rangecheck in --image--
Operand stack:
   --dict:7/7(L)--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1951   1   3   %oparray_pop   1950   1   3   %oparray_pop   1934   1   3   %oparray_pop   1820   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   1847   1   3   %oparray_pop
Dictionary stack:
   --dict:1183/1684(ro)(G)--   --dict:0/20(G)--   --dict:80/200(L)--
Current allocation mode is local
Last OS error: Invalid argument

So, I think I'm getting closer. Could someone explain the Decode options and why it might throw up an error when using a CMYK image?

Many Thanks.

Community
  • 1
  • 1
MissAran
  • 125
  • 9

2 Answers2

1

You haven't actually said which manuals you are using, so its kind of difficult to suggest others.

The first thing I'd note is that you are using colorimage, you probably want to stop doing that and use the dictionary form of the image operator instead, which is much more flexible. Also it will be more useful should you want to try some of the other image types which only have the dictionary form. The colorimage operator is basically a nasty hack from the days between level 1 and level 2 PostScript.

Does viewjpeg.ps work correctly with your JPEG file ? If so then you can use it as a template (note that viewjpeg.ps uses the dictionary form of the image operator).

By the way, you will want to be careful about the pdfwrite settings if you plan to use a JPEG as your image data source, if you don't alter the ColorImageFilter then pdfwrite will apply JPEG compression to your image sampled. JPEG compressing data which has previously been JPEG compressed leads to noticeable loss of quality.

As far as the inversion goes, I would guess that the samples returned from the JPEG image are simply inverted with respect to the PostScript CMYK colour model (0 = 0% colourant, 255 = 100% colourant). Obviously I can't tell without seeing the jpeg file.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Hi Ken, Thanks for that response. The manuals I have are: https://www.adobe.com/products/postscript/pdfs/PLRM.pdf http://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF https://staff.science.uva.nl/a.j.p.heck/Courses/Mastercourse2005/tutorial.pdf If you have any more pointers that would be amazing! Yes the viewjpeg does work - I think my problem is I'm finding it hard to simplify that script. Thanks for the tip about the compression - the scipt will need to keep in mind for high res images (300 dpi). I'll see if I can supply you with the image I'm referring to. – MissAran May 20 '15 at 16:34
  • Well the Red and Blue books are an excellent starting point, you might like to look at the Black&White book if you plan to do anything with fonts (its definitely not required though). If you plan to work with PDF then the PDF Reference or the ISO version of it are worthwhile. In addition, not a manual but a recommendation, take a look at Acumen Training's website and the Acumen Journal. An excellent resource for both PostScript and PDF. http://www.acumentraining.com/acumenjournal.html – KenS May 21 '15 at 07:06
0

Thanks to KenS, I was able to get the CYMK image in at 300 DPI.

    << /PageSize [419.528 595.276] >> setpagedevice 
    % [ W x H ] 
    /DeviceCMYK setcolorspace 
    % Page 1 

    %
    % Set the Original to be the top left
    %
    0 595.276 translate 
    1 -1 scale 
    gsave 


    %% Images when flipped to draw correctly are scaled UPWARDS 
%% so you need to move the x,y position to the bottom left

-1.44 621.714330709 translate % Bottom Left Cordinates

% unset the mirror or the image will be flipped!
1 -1 scale 

% scale the image
438.000944882 657.119055118 scale  %%% Need to work out size and width into Units


/Image1File (cmyk_image.jpg) (r) file /DCTDecode filter  def

/Image1
{
<<
  /ImageType 1
  /Width 1825
  /Height 2738
  /ImageMatrix [1825 0 0 -2738 0 2738]
  /BitsPerComponent 8
  /Decode [1 0 1 0 1 0 1 0] % can either be 1 0 or 0 1
  /DataSource Image1File
>>
} bind def

/DeviceCMYK setcolorspace
Image1 image

% Reset to previous X and Y ( line 13 )
grestore 
gsave

showpage 

Then I execute this from command line.

gs -o output.pdf -sDEVICE=pdfwrite -dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=false -dEncodeGrayImages=false -dEncodeMonoImages=false input.ps
MissAran
  • 125
  • 9