ImageGear for C and C++ on Windows v19.3 - Updated
Convert Image Color Space
User Guide > How to Work with... > Color > Convert Image Color Space

To convert an image from one color space to another, use the IG_image_colorspace_convert() function. It takes image, new color space identifier, and conversion options (optionally) as incoming parameters.

C and C++
Copy Code
// Load an image into memory and create a handle for the image.
HIGEAR image = 0;
IG_load_file("RGB.tif", &image);

// Change color space.
IG_image_colorspace_convert(image, IG_COLOR_SPACE_ID_CMYK, NULL);

// Save the image.
IG_save_file(image, "CMYK.tif", IG_SAVE_TIF_LZW);

// Cleanup the image page.
IG_image_delete(image);

Note that the channels structure of the converted image may differ from the initial one. The most common changes are the quantity of channels and the depth of channels. The quantity of channels may be changed because of differences in color space and because of changes in extra channels (controlled by conversion options). Changes in depth usually occur if the source channels have different depths (e.g., if the source image has 5-6-5 bits per pixel, the converted image will have 6-6-6 channel depths).

The AT_COLORSPACE_CONVERSION_OPTIONS structure provides advanced control for color space conversion. Below is the explanation of each attribute:

Advanced color space conversion routines allow you to operate not only on images, but also on particular rasters. It's also possible to supply a custom channels structure to produce conversion data with the necessary channel depths.

To start operating conversion, a HIGCSCONVERTER instance must be created. Any of IG_colorspace_conversion_create(), IG_colorspace_conversion_create_from_image(), or IG_colorspace_conversion_create_from_dib_info() may be used for this purpose. The first variation has eight incoming parameters and one output:

IG_colorspace_conversion_create_from_image() and IG_colorspace_conversion_create_from_dib_info() are designed for convenience and retrieve source attributes (space, palette, channels) from supplied image or DIB information.

Once a color space converter has been allocated, it may be used in any of IG_colorspace_conversion_apply_to_image() and IG_colorspace_conversion_apply_to_raster() routines.

To release conversion, IG_colorspace_conversion_destroy() routine must be used.

It's necessary to note that advanced conversion routines may be used not only for color space conversion, but for channel depth change, too (even if IsRaw attribute of conversion options is turned on). It's possible to provide the desired channels structure, and the converter will produce output according to the specified structure.

Auxiliary routines of advanced color space conversion are IG_colorspace_conversion_result_palette_get() and IG_colorspace_conversion_result_channels_build().

IG_colorspace_conversion_result_palette_get() is designed to produce a palette if the new color space is indexed. But note that conversion to IG_COLOR_SPACE_ID_I color space is possible only from IG_COLOR_SPACE_ID_I and IG_COLOR_SPACE_ID_Gy with a depth not greater than eight.

IG_colorspace_conversion_result_channels_build() may be used to get the default channels structure for converted data. The result of this routine is used by conversion routines if resultChannelDepths parameter has a NULL value.

Images stored in CMYK can be optionally converted to RGB when loaded, or converted to RGB at any time after loading.

CMYK images can be color separated or converted to several other color spaces.