FloatImg4PythonBinding/funcs/saturation.c

110 lines
2.5 KiB
C
Raw Normal View History

2020-07-28 08:19:38 +02:00
/*
2021-04-28 00:21:45 +02:00
* FloatImg library from tTh - really ugly code inside
2020-07-28 08:19:38 +02:00
*/
#include <stdio.h>
#include "../floatimg.h"
/* -------------------------------------------------------------- */
2021-04-28 00:21:45 +02:00
/* global vars exported from main
2020-07-28 08:19:38 +02:00
*/
extern int verbosity;
/* -------------------------------------------------------------- */
2020-08-07 06:46:41 +02:00
/*
* parameter mix is between 0.0 and 1.0 but other
* values give sometime good vibrations.
*/
2020-07-28 08:19:38 +02:00
int fimg_mix_rgb_gray(FloatImg *img, float mix)
{
int x, y, p;
float gr;
if (FIMG_TYPE_RGB != img->type) {
fprintf(stderr, "%s bad type\n", __func__);
return -6;
}
for (y=0; y<img->height; y++) {
2020-08-07 06:46:41 +02:00
p = y * img->width; /* first pixel of the row */
2020-07-28 08:19:38 +02:00
for (x=0; x<img->width; x++) {
gr = (img->R[p] + img->G[p] + img->R[p]) / 3.0;
img->R[p] = ((gr * mix) + (img->R[p] * (1.0-mix))) / 2.0;
img->G[p] = ((gr * mix) + (img->G[p] * (1.0-mix))) / 2.0;
img->B[p] = ((gr * mix) + (img->B[p] * (1.0-mix))) / 2.0;
p++; /* next pixel in the row */
}
}
return 0;
}
2020-08-06 13:13:10 +02:00
/* -------------------------------------------------------------- */
2020-08-07 06:46:41 +02:00
/*
* The third parameter was a six value array with min and max
* values maybe computed by the 'fimg_get_minmax_rgb' function.
*/
2020-08-06 13:13:10 +02:00
int fimg_shift_to_zero(FloatImg *s, FloatImg *d, float coefs[6])
{
int sz, idx;
2020-08-06 23:58:09 +02:00
if (FIMG_TYPE_RGB != s->type) {
2020-08-06 13:13:10 +02:00
fprintf(stderr, "%s bad type\n", __func__);
return -6;
}
2020-08-07 06:46:41 +02:00
sz = s->width * s->height;
2020-08-06 13:13:10 +02:00
for (idx=0; idx<sz; idx++) {
d->R[idx] = s->R[idx] - coefs[0];
d->G[idx] = s->G[idx] - coefs[2];
d->B[idx] = s->B[idx] - coefs[4];
}
return 0;
}
2021-02-24 12:11:59 +01:00
/* -------------------------------------------------------------- */
2021-02-25 08:23:27 +01:00
/*
2021-04-28 00:21:45 +02:00
* I think that this function is fully buggy, and need
* more explanations.
2021-02-25 08:23:27 +01:00
*/
2021-02-24 12:11:59 +01:00
int fimg_auto_shift_to_zero(FloatImg *src, FloatImg *dst)
{
float coefs[6];
int foo;
2021-02-25 08:23:27 +01:00
float minima = 1e7; /* magic value ? */
2021-02-24 12:11:59 +01:00
2021-04-28 00:21:45 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, src, dst);
#endif
2021-02-24 12:11:59 +01:00
if (FIMG_TYPE_RGB != src->type) {
fprintf(stderr, "%s: bad image type %d\n", __func__, src->type);
return -6;
}
foo = fimg_get_minmax_rgb(src, coefs);
if (foo) {
fprintf(stderr, "%s: err %d get minmax\n", __func__, foo);
return foo;
}
2021-02-25 08:23:27 +01:00
/* crude hack for now */
if (coefs[0] < minima) minima = coefs[0];
if (coefs[2] < minima) minima = coefs[2];
if (coefs[4] < minima) minima = coefs[4];
coefs[0] = coefs[2] = coefs[4] = minima;
2021-02-24 12:11:59 +01:00
foo = fimg_shift_to_zero(src, dst, coefs);
if (foo) {
fprintf(stderr, "%s WTF?\n", __func__);
return foo;
}
2020-08-06 13:13:10 +02:00
2021-02-24 12:11:59 +01:00
return 0;
}
2020-07-28 08:19:38 +02:00
/* -------------------------------------------------------------- */
2021-02-24 12:11:59 +01:00