FloatImg/lib/fimg-math.c

438 lines
10 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
2021-11-02 16:01:11 +01:00
* fimg-math.c
2019-03-03 16:22:55 +01:00
*
*
*/
#include <stdio.h>
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2019-03-03 16:22:55 +01:00
#include <unistd.h>
2019-08-07 17:30:16 +02:00
#include <string.h>
2020-03-02 01:19:57 +01:00
#include <float.h> /* for FLT_MAX */
2019-08-07 17:30:16 +02:00
#include <math.h>
2019-03-03 16:22:55 +01:00
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
2022-02-28 20:00:20 +01:00
/* ---------------------------------------------------------------- */
/* nouveau 27 fevrier 2022 */
float fimg_get_plane_maxvalue(FloatImg *psrc, char plane)
{
float *ptrplane;
float maxval;
int area, foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%c' )\n", __func__, psrc, plane);
#endif
switch (plane) {
case 'r': case 'R':
ptrplane = psrc->R; break;
case 'g': case 'G':
ptrplane = psrc->G; break;
case 'b': case 'B':
ptrplane = psrc->B; break;
case 'a': case 'A':
ptrplane = psrc->A; break;
default:
fprintf(stderr, "%s: plane error\n", __func__);
abort(); break;
}
area = psrc->width * psrc->height;
maxval = 0.0;
for (foo=0; foo<area; foo++) {
if (ptrplane[foo] > maxval) maxval = ptrplane[foo];
}
return maxval;
}
2019-03-03 16:22:55 +01:00
/* ---------------------------------------------------------------- */
float fimg_get_maxvalue(FloatImg *head)
{
float maxval;
2020-04-11 23:18:33 +02:00
int foo, surface;
2019-03-03 16:22:55 +01:00
2019-08-26 02:35:17 +02:00
if (head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
2019-08-07 17:30:16 +02:00
fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type);
return nanf("wtf ?");
}
2019-03-03 16:22:55 +01:00
maxval = 0.0; /* no negative values allowed */
2020-04-11 23:18:33 +02:00
surface = head->width*head->height;
2019-08-26 02:35:17 +02:00
switch (head->type) {
case FIMG_TYPE_RGB:
2020-04-11 23:18:33 +02:00
for (foo=0; foo<surface; foo++) {
2019-08-26 02:35:17 +02:00
if (head->R[foo] > maxval) maxval = head->R[foo];
if (head->G[foo] > maxval) maxval = head->G[foo];
if (head->B[foo] > maxval) maxval = head->B[foo];
}
2022-01-27 10:29:37 +01:00
break;
2019-08-26 02:35:17 +02:00
case FIMG_TYPE_GRAY:
2020-04-11 23:18:33 +02:00
for (foo=0; foo<surface; foo++) {
2019-08-26 02:35:17 +02:00
if (head->R[foo] > maxval) maxval = head->R[foo];
}
2022-01-27 10:29:37 +01:00
break;
2019-03-03 16:22:55 +01:00
}
return maxval;
}
/* ---------------------------------------------------------------- */
2020-03-02 01:19:57 +01:00
/*
* mmval[0] <- min(R) mmval[1] <- max(R)
*/
int fimg_get_minmax_rgb(FloatImg *head, float mmvals[6])
{
int idx, surface;
float fval;
if (head->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type);
return -2;
}
surface = head->width * head->height;
mmvals[0] = FLT_MAX; mmvals[1] = -FLT_MAX;
mmvals[2] = FLT_MAX; mmvals[3] = -FLT_MAX;
mmvals[4] = FLT_MAX; mmvals[5] = -FLT_MAX;
for (idx=0; idx<surface; idx++) {
fval = head->R[idx];
if (fval < mmvals[0]) mmvals[0] = fval;
else if (fval > mmvals[1]) mmvals[1] = fval;
fval = head->G[idx];
if (fval < mmvals[2]) mmvals[2] = fval;
else if (fval > mmvals[3]) mmvals[3] = fval;
fval = head->B[idx];
if (fval < mmvals[4]) mmvals[4] = fval;
else if (fval > mmvals[5]) mmvals[5] = fval;
}
2023-10-07 19:19:49 +02:00
return 0;
}
/* ---------------------------------------------------------------- */
/* new: Fri Oct 6 19:51:28 UTC 2023
*
* can compute the maxima of a lot of pictures...
*/
2023-11-29 11:26:56 +01:00
int fimg_max_of_max(FloatImg *img, float maxes[3])
2023-10-07 19:19:49 +02:00
{
float localmax[3];
int idx, surface;
float fval;
localmax[0] = localmax[1] = localmax[2] = -1e9;
surface = img->width * img->height;
for (idx=0; idx<surface; idx++) {
fval = img->R[idx];
if (fval > localmax[0]) localmax[0] = fval;
fval = img->G[idx];
if (fval > localmax[1]) localmax[1] = fval;
fval = img->B[idx];
if (fval > localmax[2]) localmax[2] = fval;
}
if (localmax[0] > maxes[0]) maxes[0] = localmax[0];
if (localmax[1] > maxes[1]) maxes[1] = localmax[1];
if (localmax[2] > maxes[2]) maxes[2] = localmax[2];
return 0;
2020-03-02 01:19:57 +01:00
}
/* ---------------------------------------------------------------- */
2019-03-03 16:22:55 +01:00
int fimg_meanvalues(FloatImg *head, float means[4])
{
int idx, surface;
2019-09-28 23:45:51 +02:00
double accus[4];
2019-03-03 16:22:55 +01:00
surface = head->width * head->height;
if (surface < 1) return -1;
2019-09-28 23:45:51 +02:00
memset(accus, 0, 4*sizeof(double));
2019-03-03 16:22:55 +01:00
for (idx=0; idx<surface; idx++) {
2021-03-17 18:32:51 +01:00
accus[0] += (double)head->R[idx];
if (head->type > 2) { /* WTF ? */
2021-03-17 18:32:51 +01:00
accus[1] += (double)head->G[idx];
accus[2] += (double)head->B[idx];
2019-03-03 16:22:55 +01:00
}
}
2019-09-28 23:45:51 +02:00
for (idx=0; idx<4; idx++) {
means[idx] = (float)(accus[idx]/(double)surface);
}
2019-03-03 16:22:55 +01:00
return 0;
}
2019-09-29 13:45:33 +02:00
/* ---------------------------------------------------------------- */
2022-10-30 13:11:30 +01:00
/* d'apres Wikipedia Fr :
2019-10-19 07:03:47 +02:00
| c = 0
| s = x1
| pour j de 2 à n
| s = s+xj
| c = c+(j xj s)2/(j(j1))
| renvoyer c/n
Mais c,a ne semble pas etre la bonne methode. Il faut aller voir :
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2019-09-29 13:45:33 +02:00
*/
2019-03-03 16:22:55 +01:00
/* ---------------------------------------------------------------- */
2019-08-27 09:19:25 +02:00
/*
* more elaborate functions are in fimg-2gray.c
*/
2019-03-03 16:22:55 +01:00
int fimg_to_gray(FloatImg *head)
{
float add;
int foo;
2019-08-07 17:30:16 +02:00
if (head->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type);
return -3;
}
2019-03-03 16:22:55 +01:00
for (foo=0; foo<(head->width*head->height); foo++) {
2024-05-01 21:40:44 +02:00
add = head->R[foo] + head->G[foo] + head->B[foo];
2019-09-17 13:14:33 +02:00
head->R[foo] = head->G[foo] = head->B[foo] = add / 3.0;
2019-03-03 16:22:55 +01:00
}
2021-03-17 18:32:51 +01:00
return 0;
2019-03-03 16:22:55 +01:00
}
/* ---------------------------------------------------------------- */
2021-03-17 18:32:51 +01:00
int fimg_add_cste(FloatImg *fi, float value)
2019-03-03 16:22:55 +01:00
{
int nbre, idx;
2019-08-07 17:30:16 +02:00
if (fi->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
2021-03-17 18:32:51 +01:00
return -44;
2019-08-07 17:30:16 +02:00
}
nbre = fi->width * fi->height ;
2019-03-03 16:22:55 +01:00
#if DEBUG_LEVEL
fprintf(stderr, "%s, nbre is %d\n", __func__, nbre);
#endif
2019-09-10 01:31:48 +02:00
for (idx=0; idx<nbre; idx++) {
2019-03-03 16:22:55 +01:00
fi->R[idx] += value;
fi->G[idx] += value;
fi->B[idx] += value;
2019-03-03 16:22:55 +01:00
}
2021-03-17 18:32:51 +01:00
return 0;
2019-03-03 16:22:55 +01:00
}
/* ---------------------------------------------------------------- */
2021-03-17 18:32:51 +01:00
long fimg_count_negativ(FloatImg *fi)
2019-09-25 09:21:00 +02:00
{
int nbre, idx;
2021-03-17 18:32:51 +01:00
long count;
2019-09-25 09:21:00 +02:00
if (fi->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
return -1;
}
nbre = fi->width * fi->height;
2019-09-25 09:21:00 +02:00
count = 0;
for (idx=0; idx<nbre; idx++) {
2021-03-17 18:32:51 +01:00
if (fi->R[idx] < 0.0) count++;
if (fi->G[idx] < 0.0) count++;
if (fi->B[idx] < 0.0) count++;
2019-09-25 09:21:00 +02:00
}
2020-02-29 20:01:11 +01:00
return count;
}
/* ---------------------------------------------------------------- */
/* nouveau 29 fevrier 2020 */
2021-03-17 18:32:51 +01:00
long fimg_clamp_negativ(FloatImg *fi)
2020-02-29 20:01:11 +01:00
{
int nbre, idx;
2021-03-17 18:32:51 +01:00
long count;
2020-02-29 20:01:11 +01:00
if (fi->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
return -1;
}
2021-03-17 18:32:51 +01:00
nbre = fi->width * fi->height;
2020-02-29 20:01:11 +01:00
count = 0;
for (idx=0; idx<nbre; idx++) {
if (fi->R[idx] < 0.0) {
2021-03-17 18:32:51 +01:00
fi->R[idx] = 0.0; count++;
}
if (fi->G[idx] < 0.0) {
fi->G[idx] = 0.0; count++;
}
if (fi->B[idx] < 0.0) {
fi->B[idx] = 0.0; count++;
2020-02-29 20:01:11 +01:00
}
}
2020-04-11 23:18:33 +02:00
/* WTF 12 avril 2020, valgrind me cause mal ?
==28943== Conditional jump or move depends on uninitialised value(s)
==28943== at 0x4045E9: fimg_clamp_negativ (fimg-math.c:208)
==28943== by 0x4018C9: essai_filtrage_3x3 (t.c:128)
==28943== by 0x4024D5: main (t.c:444)
==28943== Uninitialised value was created by a heap allocation
==28943== at 0x483577F: malloc (vg_replace_malloc.c:299)
==28943== by 0x40284D: fimg_create (fimg-core.c:107)
==28943== by 0x402AB3: fimg_clone (fimg-core.c:174)
==28943== by 0x401861: essai_filtrage_3x3 (t.c:118)
==28943== by 0x4024D5: main (t.c:444)
*/
2019-09-25 09:21:00 +02:00
return count;
}
/* ---------------------------------------------------------------- */
2021-03-17 18:32:51 +01:00
int fimg_mul_cste(FloatImg *fi, float value)
2019-09-10 01:31:48 +02:00
{
int nbre, idx;
2020-01-10 12:01:12 +01:00
if ( (fi->type != FIMG_TYPE_RGB) && (fi->type != FIMG_TYPE_GRAY) ) {
2019-09-10 01:31:48 +02:00
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
2021-03-17 18:32:51 +01:00
return -44;
2019-09-10 01:31:48 +02:00
}
nbre = fi->width * fi->height;
#if DEBUG_LEVEL
fprintf(stderr, "%s, nbre of datum is %d\n", __func__, nbre);
#endif
if (fi->type == FIMG_TYPE_RGB) {
for (idx=0; idx<nbre; idx++) {
fi->R[idx] *= value;
fi->G[idx] *= value;
fi->B[idx] *= value;
}
}
if (fi->type == FIMG_TYPE_GRAY) {
for (idx=0; idx<nbre; idx++) {
fi->R[idx] *= value;
}
2019-09-10 01:31:48 +02:00
}
2021-03-17 18:32:51 +01:00
return 0;
2019-09-10 01:31:48 +02:00
}
/* ---------------------------------------------------------------- */
2022-09-17 19:18:45 +02:00
/* nouveau 17 septembre 2022 */
int fimg_div_cste(FloatImg *fi, float value)
{
int nbre, idx;
if ( (fi->type != FIMG_TYPE_RGB) && (fi->type != FIMG_TYPE_GRAY) ) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
return -44;
}
nbre = fi->width * fi->height;
if (fi->type == FIMG_TYPE_RGB) {
for (idx=0; idx<nbre; idx++) {
fi->R[idx] /= value;
fi->G[idx] /= value;
fi->B[idx] /= value;
}
}
if (fi->type == FIMG_TYPE_GRAY) {
for (idx=0; idx<nbre; idx++) {
fi->R[idx] /= value;
}
}
return 0;
}
/* ---------------------------------------------------------------- */
2021-03-17 18:32:51 +01:00
/*
* oh, please explain the usecase of this function !
*/
2020-10-09 01:26:07 +02:00
int fimg_ajust_from_grab(FloatImg *fi, double maxima, int notused)
{
double coef;
2022-07-06 10:27:55 +02:00
if (notused) {
fprintf(stderr, "notused was %d, must be 0 in %s\n",
notused, __func__);
}
if (fi->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
return -99;
}
if (fi->count < 1) {
fprintf(stderr, "%s : count %d is invalid\n", __func__, fi->count);
return -98;
}
2020-01-09 09:28:23 +01:00
/*
* mmmm, is this real ?
2020-01-10 12:01:12 +01:00
* how to accuratly check the value of 'I.fval' ?
2020-01-09 09:28:23 +01:00
*/
coef = 1.0 / ((double)fi->count * (double)fi->fval);
if (verbosity) {
fprintf(stderr, "image @ %p\n", fi);
fprintf(stderr, "fval %f\n", fi->fval);
fprintf(stderr, "count %d\n", fi->count);
2022-07-06 10:27:55 +02:00
fprintf(stderr, "maxima %f\n", maxima);
fprintf(stderr, "coef %f\n", coef);
}
fimg_mul_cste(fi, coef);
return 0;
}
/* ---------------------------------------------------------------- */
2021-11-02 16:01:11 +01:00
int fimg_absolute(FloatImg *fi)
{
int surface, idx;
surface = fi->width * fi->height;
for (idx=0; idx<surface; idx++) {
fi->R[idx] = fabsf(fi->R[idx]);
fi->G[idx] = fabsf(fi->G[idx]);
fi->B[idx] = fabsf(fi->B[idx]);
}
return 0;
}
/* ---------------------------------------------------------------- */
2019-03-03 16:22:55 +01:00
/* Warning: this function is _very_ slow */
void fimg_drand48(FloatImg *fi, float kmul)
{
int nbre, idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %g )\n", __func__, fi, kmul);
#endif
2019-09-02 10:07:56 +02:00
if (fi->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, fi->type);
return;
}
nbre = fi->width * fi->height;
2023-11-29 11:26:56 +01:00
fprintf(stderr, "in %s, drand48() is %f\n", __func__, drand48());
2023-07-06 10:54:18 +02:00
2019-03-03 16:22:55 +01:00
for (idx=0; idx<nbre; idx++) {
fi->R[idx] = drand48() * kmul;
2019-09-02 10:07:56 +02:00
fi->G[idx] = drand48() * kmul;
fi->B[idx] = drand48() * kmul;
2019-03-03 16:22:55 +01:00
}
}
/* ---------------------------------------------------------------- */