From 3fce0e784318a276ea05e6be15787ca179b0ce4e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 13 Jan 2005 23:59:55 +0000 Subject: [PATCH] * src/bitmap.c: + Implemented caca_set_bitmap_gamma(). --- src/bitmap.c | 42 +++++++++++++++++++++++++++++++++++------- src/caca.h | 1 + 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/bitmap.c b/src/bitmap.c index 1ceadd8..5e95f1c 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -45,6 +45,7 @@ typedef unsigned int uint32_t; #include #include #include +#include #include "caca.h" #include "caca_internals.h" @@ -170,6 +171,8 @@ struct caca_bitmap int rleft, gleft, bleft, aleft; void (*get_hsv)(struct caca_bitmap *, char *, int, int); int red[256], green[256], blue[256], alpha[256]; + float gammaval; + int gammatab[4097]; }; #endif @@ -223,6 +226,7 @@ struct caca_bitmap *caca_create_bitmap(unsigned int bpp, unsigned int w, unsigned int bmask, unsigned int amask) { struct caca_bitmap *bitmap; + int i; /* Minor sanity test */ if(!w || !h || !pitch || bpp > 32 || bpp < 8) @@ -257,7 +261,6 @@ struct caca_bitmap *caca_create_bitmap(unsigned int bpp, unsigned int w, /* In 8 bpp mode, default to a grayscale palette */ if(bpp == 8) { - int i; bitmap->has_palette = 1; bitmap->has_alpha = 0; for(i = 0; i < 256; i++) @@ -268,6 +271,10 @@ struct caca_bitmap *caca_create_bitmap(unsigned int bpp, unsigned int w, } } + /* Default gamma value */ + for(i = 0; i < 4096; i++) + bitmap->gammatab[i] = i; + return bitmap; } @@ -313,6 +320,27 @@ void caca_set_bitmap_palette(struct caca_bitmap *bitmap, bitmap->has_alpha = has_alpha; } +/** + * \brief Set the gamma of a bitmap object. + * + * Set the gamma of bitmap. + * + * \param bitmap Bitmap object. + * \param red Gamma value. + */ +void caca_set_bitmap_gamma(struct caca_bitmap *bitmap, float gammaval) +{ + int i; + + if(gammaval <= 0.0) + return; + + bitmap->gammaval = gammaval; + + for(i = 0; i < 4096; i++) + bitmap->gammatab[i] = 4096.0 * powf((float)i / 4096.0, 1.0 / gammaval); +} + /** * \brief Free the memory associated with a bitmap. * @@ -370,16 +398,16 @@ static void get_rgba_default(struct caca_bitmap const *bitmap, uint8_t *pixels, if(bitmap->has_palette) { - *r += bitmap->red[bits]; - *g += bitmap->green[bits]; - *b += bitmap->blue[bits]; + *r += bitmap->gammatab[bitmap->red[bits]]; + *g += bitmap->gammatab[bitmap->green[bits]]; + *b += bitmap->gammatab[bitmap->blue[bits]]; *a += bitmap->alpha[bits]; } else { - *r += ((bits & bitmap->rmask) >> bitmap->rright) << bitmap->rleft; - *g += ((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft; - *b += ((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft; + *r += bitmap->gammatab[((bits & bitmap->rmask) >> bitmap->rright) << bitmap->rleft]; + *g += bitmap->gammatab[((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft]; + *b += bitmap->gammatab[((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft]; *a += ((bits & bitmap->amask) >> bitmap->aright) << bitmap->aleft; } } diff --git a/src/caca.h b/src/caca.h index fecb08a..38b40ff 100644 --- a/src/caca.h +++ b/src/caca.h @@ -347,6 +347,7 @@ struct caca_bitmap *caca_create_bitmap(unsigned int, unsigned int, void caca_set_bitmap_palette(struct caca_bitmap *, unsigned int r[], unsigned int g[], unsigned int b[], unsigned int a[]); +void caca_set_bitmap_gamma(struct caca_bitmap *, float); void caca_draw_bitmap(int, int, int, int, struct caca_bitmap const *, void *); void caca_free_bitmap(struct caca_bitmap *); /* @} */