Sign in to follow this  
DrTwox

Transform texture color range for television

Recommended Posts

DrTwox    192
Hi all.

I've been using the following code from SOIL to preprocess texture color range from 0-255 values per channel to 16-235 - suitable for television. This is a simple linear conversion, as shown in the lookup table:

#define COLOR_R 0
#define COLOR_G 1
#define COLOR_B 2
#define COLOR_A 3
#define CHANNELS 4

/* Convert the RGB pixels in image to a
range suitable for television display */

extern void IMAGE_TV_Range( struct rgbaImage *const image ) {
const float scale_lo = 16.0f - 0.499f;
const float scale_hi = 235.0f - 0.499f;

/*
* Lookup table for range conversion.
*/

uint8_t lookup[ 256 ];
for ( int i = 0; i < 256; ++i ) {
lookup[ i ] = (uint8_t)( ( scale_hi - scale_lo ) * i / 255.0f + scale_lo );
}

/*
* Convert all RGB pixel data from 0-255 to 16-235
* using the lookup table above.
*/

const size_t components = image->width * image->height * CHANNELS;
for ( size_t i = 0; i < components; i+= CHANNELS ) {
image->data[ i + COLOR_R ] = lookup[ image->data[ i + COLOR_R ] ];
image->data[ i + COLOR_G ] = lookup[ image->data[ i + COLOR_G ] ];
image->data[ i + COLOR_B ] = lookup[ image->data[ i + COLOR_B ] ];
/* Skip alpha */
}
}



However, I recall reading an article some time ago that suggested that a non-linear conversion would give better color, and perhaps each channel should have its own distinct range due to color reproduction on the average television.

Do you know what I'm talking about? Should I just focus on gamma correction and not worry about this? Double plus karma for code examples in C! Any other thoughts or ideas on the subject?

Share this post


Link to post
Share on other sites
TyrianFin    122
Hmm... why to clamp/limit color range?
(modern TV monitors are LCD and not analoque, end there is no forbidden colors!)

Just use gamma correction & linear color space.

/Tyrian

Ps. Please don`t save preproced texture files to some weird color space,
do processing on file loading.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this