FloatImg/lib/contrast.c

188 lines
4.1 KiB
C
Raw Normal View History

2019-11-15 06:07:06 +01:00
/*
* contrast.c - part of libfloatimg
2021-05-17 22:38:56 +02:00
* --------------------------------
*
* you can see some use in 'tools/fimgfx.c', so you can thing
2021-05-20 09:31:28 +02:00
* about the 'maxval' parameter. It was touchy :)
2019-11-15 06:07:06 +01:00
*/
2019-11-12 00:20:28 +01:00
#include <stdio.h>
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2019-11-12 00:20:28 +01:00
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "../floatimg.h"
extern int verbosity;
/* ---------------------------------------------------------------- */
2019-11-18 10:18:30 +01:00
int fimg_id_contraste(char *str)
{
if (!strcmp(str, "none")) return CONTRAST_NONE;
if (!strcmp(str, "sqrt")) return CONTRAST_SQRT;
if (!strcmp(str, "pow2")) return CONTRAST_POW2;
if (!strcmp(str, "cos01")) return CONTRAST_COS01;
if (!strcmp(str, "cos010")) return CONTRAST_COS010;
2021-05-20 09:31:28 +02:00
if (!strcmp(str, "xper")) return CONTRAST_XPER;
#if DEBUG_LEVEL
fprintf(stderr, "%s: unknow contrast name '%s'\n", __func__, str);
#endif
2019-11-18 10:18:30 +01:00
return -1;
}
/* ---------------------------------------------------------------- */
2019-11-15 06:07:06 +01:00
/*
* if the second parameter is NULL, operate 'in-place'
*/
2019-11-12 00:20:28 +01:00
int fimg_square_root(FloatImg *s, FloatImg *d, double maxval)
{
int nbre, idx;
double dval;
if (s->type != FIMG_TYPE_RGB) {
2021-05-17 22:38:56 +02:00
fprintf(stderr, "%s: type %d invalide\n",
2019-11-12 00:20:28 +01:00
__func__, s->type);
return -4;
}
2021-03-17 18:32:51 +01:00
if (NULL==d) { d = s; }
2019-11-15 06:07:06 +01:00
else {
if (d->type != FIMG_TYPE_RGB) {
2021-05-17 22:38:56 +02:00
fprintf(stderr, "%s: dst type %d invalide\n",
2019-11-15 06:07:06 +01:00
__func__, d->type);
return -4;
}
}
2019-11-12 00:20:28 +01:00
nbre = s->width * s->height;
2019-11-12 00:20:28 +01:00
for (idx=0; idx<nbre; idx++) {
dval = s->R[idx] / maxval;
d->R[idx] = maxval * sqrt(dval);
dval = s->G[idx] / maxval;
d->G[idx] = maxval * sqrt(dval);
dval = s->B[idx] / maxval;
d->B[idx] = maxval * sqrt(dval);
2019-11-12 00:20:28 +01:00
}
2021-05-08 21:48:29 +02:00
return 0;
2019-11-12 00:20:28 +01:00
}
/* ---------------------------------------------------------------- */
int fimg_power_2(FloatImg *s, FloatImg *d, double maxval)
{
int nbre, idx;
double dval;
if (s->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s: src type %d invalide\n",
2019-11-12 00:20:28 +01:00
__func__, s->type);
return -4;
}
2021-03-17 18:32:51 +01:00
if (NULL==d) { d = s; }
2019-11-14 14:23:12 +01:00
else {
if (d->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s: dst type %d invalide\n",
2019-11-15 06:07:06 +01:00
__func__, d->type);
2019-11-14 14:23:12 +01:00
return -4;
}
}
2019-11-12 00:20:28 +01:00
nbre = s->width * s->height;
2019-11-12 00:20:28 +01:00
for (idx=0; idx<nbre; idx++) {
dval = s->R[idx] / maxval;
d->R[idx] = maxval * dval * dval;
dval = s->G[idx] / maxval;
d->G[idx] = maxval * dval * dval;
dval = s->B[idx] / maxval;
d->B[idx] = maxval * dval * dval;
2019-11-12 00:20:28 +01:00
}
2019-11-14 14:23:12 +01:00
return 0;
}
/* ---------------------------------------------------------------- */
/*
#macro Cos_01( X )
(0.5-0.5*cos( 3.141592654 * X))
#end
*/
int fimg_cos_01(FloatImg *s, FloatImg *d, double maxval)
{
int nbre, idx;
double dval;
2023-10-08 10:26:03 +02:00
#if DEBUG_LEVEL
2023-10-06 21:34:11 +02:00
fprintf(stderr, ">>> %s ( %p %p %g )\n", __func__, s, d, maxval);
2023-10-08 10:26:03 +02:00
#endif
2023-10-06 21:34:11 +02:00
2019-11-14 14:23:12 +01:00
if (s->type != FIMG_TYPE_RGB) {
2021-05-17 22:38:56 +02:00
fprintf(stderr, "%s: type %d invalide\n",
2019-11-14 14:23:12 +01:00
__func__, s->type);
return -4;
}
2023-10-06 21:34:11 +02:00
if (NULL==d) { d = s; } /* In place */
2019-11-15 06:07:06 +01:00
else {
if (d->type != FIMG_TYPE_RGB) {
2021-05-17 22:38:56 +02:00
fprintf(stderr, "%s: dst type %d invalide\n",
2019-11-15 06:07:06 +01:00
__func__, d->type);
return -4;
}
}
2019-11-14 14:23:12 +01:00
nbre = s->width * s->height;
2019-11-14 14:23:12 +01:00
for (idx=0; idx<nbre; idx++) {
dval = s->R[idx] / maxval;
d->R[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval));
dval = s->G[idx] / maxval;
d->G[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval));
dval = s->B[idx] / maxval;
d->B[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval));
2019-11-14 14:23:12 +01:00
}
return 0;
2019-11-12 00:20:28 +01:00
}
2019-11-14 14:23:12 +01:00
/* ---------------------------------------------------------------- */
int fimg_cos_010(FloatImg *s, FloatImg *d, double maxval)
{
int nbre, idx;
double dval;
if (s->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, s->type);
return -4;
}
2021-03-17 18:32:51 +01:00
if (NULL==d) { d = s; }
else {
if (d->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : dst type %d invalide\n",
__func__, d->type);
return -4;
}
}
nbre = s->width * s->height;
for (idx=0; idx<nbre; idx++) {
dval = s->R[idx] / maxval;
d->R[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval));
dval = s->G[idx] / maxval;
d->G[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval));
dval = s->B[idx] / maxval;
d->B[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval));
}
return 0;
}
2019-11-12 00:20:28 +01:00
/* ---------------------------------------------------------------- */
2019-11-14 14:23:12 +01:00