home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / gs_l2img.ps < prev    next >
Text File  |  2000-03-09  |  6KB  |  187 lines

  1. %    Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % $Id: gs_l2img.ps,v 1.1 2000/03/09 08:40:40 lpd Exp $
  16. % Emulate the Level 2 dictionary-based image operator in Level 1,
  17. % except for Interpolate (ignored) and MultipleDataSources = true;
  18. % also, we require that the data source be either a procedure of a
  19. % particular form or a stream, not a string or a general procedure.
  20.  
  21. % pdf2ps copies the portion of this file from %BEGIN to %END if Level 1
  22. % compatible output is requested.
  23.  
  24. /currentglobal where
  25.  { pop currentglobal { setglobal } true setglobal }
  26.  { { } }
  27. ifelse
  28.  
  29. /packedarray where
  30.  { pop }
  31.  { /packedarray { array astore readonly } bind def }
  32. ifelse
  33.  
  34. %BEGIN
  35.  
  36. 11 dict /.csncompdict 1 index def begin
  37.   /DeviceGray { 1 /setgray load } bind def
  38.   /DeviceRGB { 3 /setrgbcolor load } bind def
  39.   /DeviceCMYK { 4 /setcmykcolor load } bind def
  40.   /Indexed
  41.    { dup 1 index 1 get //.csncompdict exch get exec
  42.         % Stack: [/Indexed base hival map] ncomp basesetcolor
  43.      3 -1 roll 3 get mark 3 1 roll
  44.         % Stack: ncomp -mark- basesetcolor map
  45.      dup type /stringtype eq
  46.       {  { -
  47.         { exch round cvi get 255 div
  48.         }
  49.        -
  50.         { exch round cvi 3 mul 2 copy 2 copy get 255 div
  51.           3 1 roll 1 add get 255 div
  52.           4 2 roll 2 add get 255 div
  53.         }
  54.         { exch round cvi 4 mul 2 copy 2 copy 2 copy get 255 div
  55.           3 1 roll 1 add get 255 div
  56.           4 2 roll 2 add get 255 div
  57.           5 3 roll 3 add get 255 div
  58.         }
  59.      }
  60.     4 index get aload pop counttomark -1 roll
  61.       }
  62.       { /exec load 3 -1 roll
  63.         % Stack: -mark- mapproc --exec-- basesetcolor
  64.       }
  65.      ifelse .packtomark cvx
  66.      exch pop 1 exch
  67.    } bind def
  68.   /Separation
  69.    { dup 2 index //.csncompdict exch get exec
  70.         % Stack: [/Separation name alt xform] ncomp altsetcolor
  71.      3 -1 roll 3 get /exec load 3 -1 roll 3 array astore readonly cvx
  72.      exch pop 1 exch
  73.    } bind def
  74.     % Substitute device spaces for CIE spaces.
  75.   /CIEBasedA /DeviceGray load def
  76.   /CIEBasedABC /DeviceRGB load def
  77.   /CIEBasedDEF /DeviceRGB load def
  78.   /CIEBasedDEFG /DeviceCMYK load def
  79. end
  80.  
  81. /.packtomark { counttomark packedarray exch pop } bind def
  82.  
  83. /.csinextbits        % - .csinextbits <bits>
  84.             % Uses b, nnb, i, row, mask, BitsPerComponent;
  85.             % sets b, nnb, i.
  86.  { /nnb nnb BitsPerComponent add
  87.     { dup 0 le { exit } if
  88.       /b b 8 bitshift row i get add def
  89.       /i i 1 add def  8 sub
  90.     }
  91.    loop def
  92.    b nnb bitshift mask and
  93.  } bind def
  94.  
  95. % Note that the ColorSpace key must be present in the image dictionary.
  96. /.colorspaceimage        % <imagedict> .colorspaceimage -
  97.  { save exch
  98.    dup length 15 add dict begin { cvlit def } forall
  99.    ColorSpace dup dup type /nametype ne { 0 get } if
  100.    .csncompdict exch get exec
  101.      /setpixelcolor exch def  /ncomp exch def  pop
  102.    /row ncomp BitsPerComponent mul Width mul 7 add 8 idiv string def
  103.       /mask 1 BitsPerComponent bitshift 1 sub def
  104.       /nextbits BitsPerComponent 8 eq
  105.        { { row i get /i i 1 add def } }
  106.        { /.csinextbits load }
  107.       ifelse def
  108.       /nextpixel mark 0 2 ncomp 1 sub 2 mul
  109.        { /nextbits cvx exch
  110.      Decode exch 2 getinterval
  111.      dup aload pop exch sub
  112.      dup mask eq { pop } { mask div /mul load 3 -1 roll } ifelse
  113.      0 get dup 0 eq { pop } { /sub load 3 -1 roll } ifelse
  114.        }
  115.       for
  116.       /setpixelcolor load dup type /operatortype ne { /exec load } if
  117.       .packtomark cvx def
  118.       /readrow
  119.         /DataSource load dup type
  120.         dup /arraytype eq exch /packedarraytype eq or
  121.     {    % Must be { <file> <string> ... }
  122.       aload length 1 add array /pop load exch astore
  123.       dup 1 row put cvx
  124.     }
  125.     { pop
  126.         % Adobe requires readstring to signal an error if given
  127.         % an empty string.  Work around this nonsense here.
  128.           row length 0 eq
  129.        { { } }
  130.        { { DataSource row readstring pop pop } }
  131.       ifelse
  132.     }
  133.       ifelse def
  134.       ImageMatrix matrix invertmatrix concat
  135.       /imat matrix def
  136.       0 1 Height 1 sub
  137.        { imat 5 3 -1 roll neg put
  138.      readrow
  139.      /b 0 def  /nnb 0 def  /i 0 def
  140.      0 1 Width 1 sub
  141.       { imat 4 3 -1 roll neg put nextpixel
  142.         1 1 true imat {<80>} imagemask
  143.       }
  144.      for
  145.        }
  146.       for
  147.    end restore
  148.  } bind def
  149.  
  150. %END
  151. exec
  152. currentfile closefile
  153.  
  154. % Patch for testing.
  155. /.cincompdict 3 dict begin
  156.   1 { {0 1} {/DeviceGray} } def
  157.   3 { {0 1 0 1 0 1} {/DeviceRGB} } def
  158.   4 { {0 1 0 1 0 1 0 1} {/DeviceCMYK} } def
  159. currentdict end def
  160. /.imagekeys [
  161.   /Decode /DataSource /ImageMatrix /BitsPerComponent /Height /Width
  162. ] def
  163. /colorimage        % <width> <height> <bits/comp> <matrix>
  164.             %   <datasrc> false <ncomp> colorimage -
  165.  { 1 index { /colorimage load /rangecheck signalerror } if exch pop
  166.    //.cincompdict exch get exec
  167.    7 dict begin /ColorSpace exch cvlit def
  168.    .imagekeys { exch cvlit def } forall
  169.    currentdict end .colorspaceimage
  170.  } bind odef
  171. /image
  172.  { dup type /dicttype ne
  173.     { 7 dict begin /ColorSpace /DeviceGray def [0 1] 
  174.       .imagekeys { exch cvlit def } forall
  175.       currentdict end
  176.     }
  177.     { dup length 1 add dict .copydict dup /ColorSpace currentcolorspace put
  178.     }
  179.    ifelse
  180.    .colorspaceimage
  181.  } bind odef
  182.  
  183. exec
  184.