next up previous contents
Next: Displaying images Up: Copying/converting the whole or Previous: Copying/converting the whole or   Contents

Accessing channels of an image

      #include <gandalf/image/image_channel.h>
Gandalf stores images with the channels combined for each pixel. If you wish to extract a channel of an image as a separate image, Gandalf provides the following function:
      Gan_Image *pRGBImage; /* declare RGB image */
      Gan_Image *pRedChannel; /* declare image storing red channel */

      /* ... create and fill RGB image, create red channel image ... */

      /* extract red channel from image */
      gan_image_extract_channel_q ( pRGBImage, GAN_RED_CHANNEL,
                                    0, 0, pRGBImage->height, pRGBImage->width,
                                    pRedChannel );
The second argument specifies which channel is to be extracted. The different options are described by the following enumerated type.
      /**
       * \brief Image channel types for extracting individual channels.
       */
      typedef enum
      {
         /// for grey-level/alpha images
         GAN_INTENSITY_CHANNEL,

         ///for RGB and RGB/alpha images
         GAN_RED_CHANNEL, GAN_GREEN_CHANNEL, GAN_BLUE_CHANNEL,

         /// for grey-level/alpha and RGB/alpha images
         GAN_ALPHA_CHANNEL,

         /// for 2D and 3D vector field images
         GAN_X_CHANNEL,

         /// likewise
         GAN_Y_CHANNEL,

         /// for 3D vector field images
         GAN_Z_CHANNEL,

         /// all channels
         GAN_ALL_CHANNELS
      } Gan_ImageChannelType;
The offset (3,4) and dimension (5,6) arguments allow a sub-region to be extracted rather than the whole image, and work in the same way as with gan_image_extract_q(). There is also a version which extracts the channel as a new image:
      pRedChannel = gan_image_extract_channel_s ( pRGBImage, GAN_RED_CHANNEL,
                                                  0, 0,
                                                  pRGBImage->height, pRGBImage->width );

There are also functions for filling a channel of an RGB image with a constant value. For instance

      Gan_Pixel Pixel;

      /* ... fill pRGBImage as an RGB unsigned character image ...*/

      /* fill green channel of pRGBImage with constant value */
      Pixel.format = GAN_GREY_LEVEL_IMAGE;
      Pixel.type = GAN_UCHAR;
      Pixel.data.gl.uc = 128;
      gan_image_fill_channel_const ( pRGBImage, GAN_GREEN_CHANNEL, &Pixel );
sets the all the green pixel components to the value 128. Note that the format of the pixel is set to grey-level, so defining a single channel pixel. To set the channel to zero there is the macro
      gan_image_fill_channel_zero ( pRGBImage, GAN_GREEN_CHANNEL );
instead.

Error detection: The gan_image_extract_channel_[qs]() return a pointer to the result image, returning NULL and invoking the Gandalf error handler on error. gan_image_fill_channel_const() and
gan_image_fill_channel_zero() return a boolean value, so GAN_FALSE is returned on error.


next up previous contents
Next: Displaying images Up: Copying/converting the whole or Previous: Copying/converting the whole or   Contents
2006-03-17