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.




function to_gray(sequence image)
    sequence color
    for i = 1 to length(image) do
        for j = 1 to length(image[i]) do
            color = and_bits(image[i][j], {#FF0000,#FF00,#FF}) /
                                          {#010000,#0100,#01} -- unpack color triple
            image[i][j] = floor(0.2126*color[1] + 0.7152*color[2] + 0.0722*color[3])
        end for
    end for
    return image
end function
 
function to_color(sequence image)
    for i = 1 to length(image) do
        for j = 1 to length(image[i]) do
            image[i][j] = image[i][j]*#010101
        end for
    end for
    return image
end function