[Gdal-dev] two questions about GDAL data type and data model

Ray Gardener rayg at daylongraphics.com
Tue Feb 21 19:13:06 EST 2006


>> 2.     Does GDAL provide API’s for conversion between RGB, HLS or 
>> CMYK? I found that GDAL can return HLS or CMYK bands directly to the 
>> user.
> 
> In *theory* GDAL supports images in HLS, and CMYK format, but in practice
> I don't think any drivers actually do this.  There is no color model
> conversion code in GDAL.

I believe the TIFF/GeoTIFF driver returns CMYK data.

CMYK-to-RGB is a straightforward CMY-to-RGB inversion followed by 
darkening the RGB overall with the K channel. You may want to support 
your platform's color calibration features or offer one locally in your 
app, to fine-tune the conversion since the gamuts don't roundtrip. Color 
support is complex enough that it should have its own lib (boost?)


HLS-to-RGB is:

static float Hue_2_RGB(float v1, float v2, float vH )
{
    if ( vH < 0 ) vH += 1;
    if ( vH > 1 ) vH -= 1;
    if ( ( 6.0f * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6.0f * vH );
    if ( ( 2.0f * vH ) < 1 ) return ( v2 );
    if ( ( 3.0f * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( 0.6666f ) - 
vH ) * 6 );
    return ( v1 );
}


void _RGBcolor::set(const _HSLcolor& hsl)
{
	if (hsl.s == 0 )
	{
	   r = g = b = hsl.l;
	}
	else
	{
		float var_2;
		if ( hsl.l < 0.5f )
			var_2 = hsl.l * ( 1.0f + hsl.s );
		else
			var_2 = ( hsl.l + hsl.s ) - ( hsl.s * hsl.l );

		float var_1 = 2.0f * hsl.l - var_2;

		r = Hue_2_RGB( var_1, var_2, hsl.h + 0.3333f );
		g = Hue_2_RGB( var_1, var_2, hsl.h );
		b = Hue_2_RGB( var_1, var_2, hsl.h - 0.3333f );
	}
}



Ray



More information about the Gdal-dev mailing list