FloatImg4PythonBinding/lib/fimg-math.c

100 lines
2.1 KiB
C

/*
* fimg-core.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
float fimg_get_maxvalue(FloatImg *head)
{
float maxval;
int foo;
maxval = 0.0; /* no negative values allowed */
for (foo=0; foo<(head->width*head->height); foo++) {
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];
}
return maxval;
}
/* ---------------------------------------------------------------- */
int fimg_meanvalues(FloatImg *head, float means[4])
{
int idx, surface;
surface = head->width * head->height;
if (surface < 1) return -1;
memset(means, 0, 4*sizeof(float));
for (idx=0; idx<surface; idx++) {
means[0] += head->R[idx];
if (head->type > 2) {
means[1] += head->G[idx];
means[2] += head->B[idx];
}
}
for (idx=0; idx<4; idx++) means[idx] /= (float)surface;
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_to_gray(FloatImg *head)
{
float add;
int foo;
for (foo=0; foo<(head->width*head->height); foo++) {
add = head->R[foo];
add += head->G[foo];
add += head->B[foo];
head->R[foo] = head->G[foo] = head->B[foo] = add;
}
return -1;
}
/* ---------------------------------------------------------------- */
void fimg_add_cste(FloatImg *fi, float value)
{
int nbre, idx;
nbre = fi->width * fi->height * fi->type;
#if DEBUG_LEVEL
fprintf(stderr, "%s, nbre is %d\n", __func__, nbre);
#endif
for (idx=0; idx<nbre; nbre++) {
fi->R[idx] += value;
}
}
/* ---------------------------------------------------------------- */
/* 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
nbre = fi->width * fi->height * fi->type;
for (idx=0; idx<nbre; idx++) {
fi->R[idx] = drand48() * kmul;
}
}
/* ---------------------------------------------------------------- */