diff --git a/pipi/pixels.c b/pipi/pixels.c index 2af8ef6..63d38cd 100644 --- a/pipi/pixels.c +++ b/pipi/pixels.c @@ -28,8 +28,11 @@ #include "pipi.h" #include "pipi_internals.h" -#define C2I(p) (pow(((double)p)/255., 2.2)) -#define I2C(p) ((int)255.999*pow(((double)p), 1./2.2)) +static void init_tables(void); + +static float u8tof32_table[256]; +static inline float u8tof32(uint8_t p) { return u8tof32_table[(int)p]; } + /* Return a direct pointer to an image's pixels. */ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) @@ -68,11 +71,13 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels; float *dest = (float *)img->p[type].pixels; + init_tables(); + for(y = 0; y < img->h; y++) for(x = 0; x < img->w; x++) for(i = 0; i < 4; i++) dest[4 * (y * img->w + x) + i] - = C2I(src[4 * (y * img->w + x) + i]); + = u8tof32(src[4 * (y * img->w + x) + i]); } else if(img->last_modified == PIPI_PIXELS_RGBA_F && type == PIPI_PIXELS_RGBA32) @@ -83,8 +88,11 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) for(y = 0; y < img->h; y++) for(x = 0; x < img->w; x++) for(i = 0; i < 4; i++) + { + double p = src[4 * (y * img->w + x) + i]; dest[4 * (y * img->w + x) + i] - = I2C(src[4 * (y * img->w + x) + i]); + = (int)(255.999 * pow(p, 1. / 2.2)); + } } else { @@ -96,3 +104,18 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) return &img->p[type]; } + +static void init_tables(void) +{ + static int done = 0; + int i; + + if(done) + return; + + for(i = 0; i < 256; i++) + u8tof32_table[i] = pow((double)i / 255., 2.2); + + done = 1; +} +