[comp.lang.postscript] Runlength-encoded Gray or Color Images

cristy@eplrx7.UUCP (cristy) (08/31/89)

For your toolbox.  This procedure displays a grayscale or color 
runlength-encoded image as specified by the grayscale parameter.  This is my 
first postscript program so be gentle.  Note that all user definable 
parameters is read as data at the end of the postscript program.

  %!
  %
  % Display a runlength-encoded image.
  %
  /buffer 512 string def
  /byte 1 string def
  /color_packet 3 string def
  /gray_packet 1 string def
  /expanded_packet 768 string def
   
  /ReadColorPacket
  {
    %
    % read one runlength-encoded color packet (length red green blue).
    %
    currentfile byte readhexstring pop 0 get
    /count exch 1 add def
    /count count 3 mul def
    currentfile color_packet readhexstring pop
    0 3 count 1 sub
    {
      expanded_packet exch color_packet putinterval
    } for pop
    expanded_packet 0 count getinterval
  } def
   
  /DisplayColorImage
  {
    columns rows 8
    [
      columns 0 0
      rows neg 0 rows
    ]
    { ReadColorPacket } false 3 colorimage
  } def
   
  /ReadGrayPacket
  {
    %
    % read one runlength-encoded grayscale packet (length gray).
    %
    currentfile byte readhexstring pop 0 get
    /count exch 1 add def
    currentfile gray_packet readhexstring pop
    0 1 count 1 sub
    {
      expanded_packet exch gray_packet putinterval
    } for pop
    expanded_packet 0 count getinterval
  } def
   
  /DisplayGrayImage
  {
    columns rows 8
    [
      columns 0 0
      rows neg 0 rows
    ]
    { ReadGrayPacket } image
  } def
   
  /DisplayImage
  {
    %
    % display runlength-encoded image.
    %
    initgraphics
    gsave
    currentfile buffer readline pop
    token { /degrees exch def } { } ifelse
    degrees rotate
    currentfile buffer readline pop
    token { /x exch def } { } ifelse
    token { /y exch def } { } ifelse
    x y translate
    currentfile buffer readline pop
    token { /x exch def } { } ifelse
    token { /y exch def } { } ifelse
    x y scale
    currentfile buffer readline pop
    currentfile buffer readline pop
    token { /columns exch def } { } ifelse
    token { /rows exch def } { } ifelse
    currentfile buffer readline pop
    token { /grayscale exch def } { } ifelse
    grayscale 0 gt { DisplayGrayImage } { DisplayColorImage } ifelse
  } def
   
  %
  % DisplayImage parameters:
  %   degrees rotation.
  %   x & y translation.
  %   x & y scale.
  %   image name.
  %   image columns & rows.
  %   grayscale.
  %   hex runlength encoded packets.
  % 
  DisplayImage
  0.000000
  274.000000 364.000000
  64.000000 64.000000
  image name
  64 64
  0
  3daaaacd00a3a3c5009796b83daaaacd00a6a5c8008c8db43daaaacd00a6a5c8009999bc
	< lots more data >
  0015394a0713384a0413394c0015394a00323e4c
  grestore
  showpage