View Raw SPL
/*****************************************************************************
*                                                                            *
*   RGB2GRAY.SPL  Copyright (C) 2019 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts an RGB image to 24 bit monochrome                  *
*                                                                            *
*   Revisions:   23 Apr 2019  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_RGB2GRAY

    RGB2GRAY

    Purpose: Converts an RGB image to a 24 bit monochrome image.

    Syntax:  RGB2GRAY(image, coef)

              image - a series containing the source image

               coef - Optional. A scalar or series, the RGB scaling
                      coefficients:

                      0: {0.2989, 0.5870, 0.1140}, CCIR 601 coefficients
                         (default)

                      1: {0.2126, 0.7152, 0.0722}, BT 709 coefficients

    Returns: An array, a 24 bit monochrome image

    Example:
             W1: density(spline2(ravel(gnorm(100,1),10), 8));rainbow()
             W2: image24(W1);
             W3: rgb2gray(W2)

             W1 contains an image of a random surface shaded with
             the colors of the spectrum ranging from red to blue. W2
             converts the image into a 24 bit color image.

             W3 converts the image to a 24 bit monochrome image.

    Example:
             W1: density(spline2(ravel(gnorm(100,1),10), 8));rainbow()
             W2: image24(W1);
             W3: rgb2gray(W2, 1)

             Same as above except the BT 709 coeffcients are used to
             create the initial monochrome image.

    Remarks:
             RGB2GRAY converts the 24 bit image into a 24 bit
             monochrome image using RGB2MONO.

             By default, CCIR 601 coefficients are used to scale
             each RGB pixel as follows:

                  mono = 0.2989 * r + 0.5870 * g + 0.1140 * b

             where r, g, b are the red, green and blue components of
             the original RGB image. The r, g, b components of the
             24 bit monochrome image are all set equal to the monochrome
             value and scaled between 0.0 and 1.0.

             If COEF is a series, it must be of size 3x1.

    See Also:
             Density
             Getcolormap
             Getrgb
             Image24
             Iminterp
             Rgb2mono
             Rgbimage
             Spline2
             Interp2
#endif


/* convert image to 24 bit monochrome */
rgb2gray(image, coef)
{
        local bw, r, g, b;

        if (argc < 2)
        {
                if (argc < 1) image = refwindow(w0);

                /* CCIR 601 */
                coef = 0;
        }

        /* convert to monochrome: 0 <= val <= 255 */
        bw = rgb2mono(image, coef);

        /* scale RGB components: 0.0 <= val <= 1.0 */
        r = g = b = bw / 255;

        /* return 24 bit image */
        bw = rgbimage(r, g, b);

        return(bw);
}