% COLORMAP -- a tool for coloring bitmaps.  Ver. 0.1
%
% \lingraymap[ map.g(0) : map.g(1) ] {contents} % 0 == black
% \lincmykmap[ map.c(0) map.m(0) map.y(0) map.k(0) :
%              map.c(1) map.m(1) map.y(1) map.k(1) ] {contents} % 0 == white
% \gengraymap <lookup string of 256 one-byte entries> {contents} % 
% \gencmykmap <lookup string of 256 four-byte entries> {contents} %
% 
\def\lingraymap[#1:#2]#3{%
  \special{ps:
    /cmori.image /image load def
    /cmori.colorimage /colorimage load def
    /ImgDict 10 dict def
    /image {
      dup type (dicttype) eq {
        dup /Decode known {
          dup dup /Decode get dup length 2 eq {
            aload pop
            exch #2 #1 sub mul #1 add % a'=#1+a*(#2-#1)
            exch #2 #1 sub mul #1 add % b'=#1+b*(#2-#1)
            2 array astore
            /Decode exch put
          }{
            pop pop % not gray image can't map
          } ifelse
        }{
          dup /Decode [#1 #2] put % it shouldn't hapen
        } ifelse
      }{
        ImgDict begin
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [#1 #2] def
        end
        ImgDict 
      } ifelse
      cmori.image
    } def
    /colorimage {
      dup 1 eq {
        ImgDict begin
          pop pop 
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [#1 #2] def
        end
        ImgDict cmori.image
      }{
        cmori.colorimage
      } ifelse
    } def
  }%
  #3%
  \special{ps:
    /image /cmori.image load def
    /colorimage /cmori.colorimage load def
  }%
}
%
\def\lincmykmap[#1:#2]#3{%
  \special{ps:
    /cmori.image /image load def
    /cmori.colorimage /colorimage load def
    /cmori.colorspace currentcolorspace def
    #1 /k.min exch def /y.min exch def /m.min exch def /c.min exch def 
    #2 /k.max exch def /y.max exch def /m.max exch def /c.max exch def 
    /ImgDict 10 dict def
    /transfor {
      dup 255 div 1 exch sub c.max c.min sub mul c.min add exch
      dup 255 div 1 exch sub m.max m.min sub mul m.min add exch
      dup 255 div 1 exch sub y.max y.min sub mul y.min add exch
          255 div 1 exch sub k.max k.min sub mul k.min add 
    } def
    /image {
      dup type (dicttype) eq {
        dup /Decode known {
          dup /Decode get length 2 eq {
            dup /Decode [0 255] put
          } if
        }{
          dup /Decode [0 255] put
        } ifelse
      }{
        ImgDict begin
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
      end
      ImgDict 
      } ifelse
      [/Indexed /DeviceCMYK 255 /transfor load ] setcolorspace
      cmori.image
    } def
    /colorimage {
      dup 1 eq {
        ImgDict begin
          pop pop 
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
        end
        [/Indexed /DeviceCMYK 255 /transfor load ] setcolorspace
        ImgDict cmori.image
      }{
        cmori.colorimage
      } ifelse
    } def
  }%
  #3%
  \special{ps:
    /image /cmori.image load def
    /colorimage /cmori.colorimage load def
    cmori.colorspace setcolorspace
  }%
}
%
\def\gengraymap<#1>#2{%
  \special{ps:
    /cmori.image /image load def
    /cmori.colorimage /colorimage load def
    /cmori.colorspace currentcolorspace def
    /ImgDict 10 dict def
    /image {
      dup type (dicttype) eq {
        dup /Decode known {
          dup /Decode get length 2 eq {
            dup /Decode [0 255] put
          } if
        }{
          dup /Decode [0 255] put
        } ifelse
      }{
        ImgDict begin
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
      end
      ImgDict 
      } ifelse
      [/Indexed /DeviceGray 255 <#1>] setcolorspace
      cmori.image
    } def
    /colorimage {
      dup 1 eq {
        ImgDict begin
          pop pop 
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
        end
        [/Indexed /DeviceGray 255 <#1>] setcolorspace
        ImgDict cmori.image
      }{
        cmori.colorimage
      } ifelse
    } def
  }%
  #2%
  \special{ps:
    /image /cmori.image load def
    /colorimage /cmori.colorimage load def
    cmori.colorspace setcolorspace
  }%
}
%
\def\gencmykmap<#1>#2{%
  \special{ps:
    /cmori.image /image load def
    /cmori.colorimage /colorimage load def
    /cmori.colorspace currentcolorspace def
    /ImgDict 10 dict def
    /image {
      dup type (dicttype) eq {
        dup /Decode known {
          dup /Decode get length 2 eq {
            dup /Decode [0 255] put
          } if
        }{
          dup /Decode [0 255] put
        } ifelse
      }{
        ImgDict begin
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
      end
      ImgDict 
      } ifelse
      [/Indexed /DeviceCMYK 255 <#1>] setcolorspace
      cmori.image
    } def
    /colorimage {
      dup 1 eq {
        ImgDict begin
          pop pop 
          /DataSource exch def
          /ImageMatrix exch def
          /BitsPerComponent exch def
          /Height exch def
          /Width exch def
          /ImageType 1 def
          /Decode [0 255] def
        end
        [/Indexed /DeviceCMYK 255 <#1>] setcolorspace
        ImgDict cmori.image
      }{
        cmori.colorimage
      } ifelse
    } def
  }%
  #2%
  \special{ps:
    /image /cmori.image load def
    /colorimage /cmori.colorimage load def
    cmori.colorspace setcolorspace
  }%
}
\endinput


.
