Many image processing algorithms are defined for grayscale (or else monochromatic) images.



Task

Extend the data storage type defined on this page to support grayscale images.

Define two operations, one to convert a color image to a grayscale image and one for the backward conversion.

To get luminance of a color use the formula recommended by CIE:

 L  =  0.2126 × R   +   0.7152 × G   +   0.0722 × B 

When using floating-point arithmetic make sure that rounding errors would not cause run-time problems or else distorted results when calculated luminance is stored as an unsigned integer.




functor
import
   Array2D
export
   ToGraymap
   FromGraymap
define
   fun {ToGraymap bitmap(Arr)}
      graymap({Array2D.map Arr Luminance})
   end
 
   fun {Luminance Color}
      F = {Record.map Color Int.toFloat}
   in
      0.2126*F.1 + 0.7152*F.2 + 0.0722*F.3
   end
 
   fun {FromGraymap graymap(Arr)}
      bitmap({Array2D.map Arr ToColor})
   end
 
   fun {ToColor Lum}
      L = {Float.toInt Lum}
   in
      color(L L L)
   end
end