forked from old/legacy-imlib2
203 lines
4.5 KiB
C
203 lines
4.5 KiB
C
#include "common.h"
|
|
#include "colormod.h"
|
|
#include "file.h"
|
|
#include "loaderpath.h"
|
|
#include <X11/Xlib.h>
|
|
#include <math.h>
|
|
#include "image.h"
|
|
|
|
static DATABIG mod_count = 0;
|
|
|
|
ImlibColorModifier *
|
|
__imlib_CreateCmod(void)
|
|
{
|
|
ImlibColorModifier *cm;
|
|
int i;
|
|
|
|
cm = malloc(sizeof(ImlibColorModifier));
|
|
cm->modification_count = mod_count;
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
cm->red_mapping[i] = (DATA8)i;
|
|
cm->green_mapping[i] = (DATA8)i;
|
|
cm->blue_mapping[i] = (DATA8)i;
|
|
cm->alpha_mapping[i] = (DATA8)i;
|
|
}
|
|
return cm;
|
|
}
|
|
|
|
void
|
|
__imlib_FreeCmod(ImlibColorModifier *cm)
|
|
{
|
|
free(cm);
|
|
}
|
|
|
|
void
|
|
__imlib_CmodChanged(ImlibColorModifier *cm)
|
|
{
|
|
mod_count++;
|
|
cm->modification_count = mod_count;
|
|
}
|
|
|
|
void
|
|
__imlib_CmodSetTables(ImlibColorModifier *cm,
|
|
DATA8 *r, DATA8 *g, DATA8 *b, DATA8 *a)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
if (r)
|
|
cm->red_mapping[i] = r[i];
|
|
if (g)
|
|
cm->green_mapping[i] = g[i];
|
|
if (b)
|
|
cm->blue_mapping[i] = b[i];
|
|
if (a)
|
|
cm->alpha_mapping[i] = a[i];
|
|
}
|
|
__imlib_CmodChanged(cm);
|
|
}
|
|
|
|
void
|
|
__imlib_CmodReset(ImlibColorModifier *cm)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
cm->red_mapping[i] = (DATA8)i;
|
|
cm->green_mapping[i] = (DATA8)i;
|
|
cm->blue_mapping[i] = (DATA8)i;
|
|
cm->alpha_mapping[i] = (DATA8)i;
|
|
}
|
|
__imlib_CmodChanged(cm);
|
|
}
|
|
|
|
void
|
|
__imlib_DataCmodApply(DATA32 *data, int w, int h, int jump,
|
|
ImlibColorModifier *cm)
|
|
{
|
|
int x, y;
|
|
DATA32 *p;
|
|
DATA8 r, g, b, a;
|
|
|
|
p = data;
|
|
for (y = 0; y < h; y++)
|
|
{
|
|
for (x = 0; x < w; x++)
|
|
{
|
|
b = (*p ) & 0xff;
|
|
g = (*p >> 8 ) & 0xff;
|
|
r = (*p >> 16) & 0xff;
|
|
a = (*p >> 24) & 0xff;
|
|
|
|
CMOD_APPLY_RGBA(cm, r, g, b, a);
|
|
|
|
*p = (a << 24) | (r << 16) | (g << 8) | b;
|
|
p++;
|
|
}
|
|
p += jump;
|
|
}
|
|
}
|
|
|
|
void
|
|
__imlib_CmodGetTables(ImlibColorModifier *cm, DATA8 *r, DATA8 *g,
|
|
DATA8 *b, DATA8 *a)
|
|
{
|
|
if (r)
|
|
memcpy(r, cm->red_mapping, (256 * sizeof(DATA8)));
|
|
if (g)
|
|
memcpy(g, cm->green_mapping, (256 * sizeof(DATA8)));
|
|
if (b)
|
|
memcpy(b, cm->blue_mapping, (256 * sizeof(DATA8)));
|
|
if (a)
|
|
memcpy(a, cm->alpha_mapping, (256 * sizeof(DATA8)));
|
|
}
|
|
|
|
void
|
|
__imlib_CmodModBrightness(ImlibColorModifier *cm, double v)
|
|
{
|
|
int i, val, val2;
|
|
|
|
val = (int)(v * 255);
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
val2 = (int)cm->red_mapping[i] + val;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->red_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)cm->green_mapping[i] + val;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->green_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)cm->blue_mapping[i] + val;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->blue_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)cm->alpha_mapping[i] + val;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->alpha_mapping[i] = (DATA8)val2;
|
|
}
|
|
}
|
|
|
|
void
|
|
__imlib_CmodModContrast(ImlibColorModifier *cm, double v)
|
|
{
|
|
int i, val2;
|
|
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
val2 = (int)(((double)cm->red_mapping[i] - 127) * v) + 127;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->red_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(((double)cm->green_mapping[i] - 127) * v) + 127;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->green_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(((double)cm->blue_mapping[i] - 127) * v) + 127;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->blue_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(((double)cm->alpha_mapping[i] - 127) * v) + 127;
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->alpha_mapping[i] = (DATA8)val2;
|
|
}
|
|
}
|
|
|
|
void
|
|
__imlib_CmodModGamma(ImlibColorModifier *cm, double v)
|
|
{
|
|
int i, val2;
|
|
|
|
if (v < 0.01)
|
|
v = 0.01;
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
val2 = (int)(pow(((double)cm->red_mapping[i] / 255), (1 / v)) * 255);
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->red_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(pow(((double)cm->green_mapping[i] / 255), (1 / v)) * 255);
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->green_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(pow(((double)cm->blue_mapping[i] / 255), (1 / v)) * 255);
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->blue_mapping[i] = (DATA8)val2;
|
|
|
|
val2 = (int)(pow(((double)cm->alpha_mapping[i] / 255), (1 / v)) * 255);
|
|
if (val2 < 0) val2 = 0; if (val2 > 255) val2 = 255;
|
|
cm->alpha_mapping[i] = (DATA8)val2;
|
|
}
|
|
}
|
|
|
|
#if 0
|
|
void
|
|
__imlib_ImageCmodApply(ImlibImage *im, ImlibColorModifier *cm)
|
|
{
|
|
__imlib_DataCmodApply(im->data, im->w, im->h, 0, cm);
|
|
}
|
|
#endif
|