FloatImg/lib/operators.c

153 lines
3.1 KiB
C
Raw Normal View History

2019-08-09 02:16:20 +11:00
/*
* OPERATORS
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
2019-08-24 12:40:53 +11:00
#include <math.h>
2019-08-09 02:16:20 +11:00
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
2019-12-21 22:37:26 +11:00
/*
* A + B -> D
* why is this func so slow ?
*/
2019-08-09 02:16:20 +11:00
int fimg_add(FloatImg *a, FloatImg *b, FloatImg *d)
{
2020-01-07 23:45:18 +11:00
int idx, nbpixels;
2019-08-09 02:16:20 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
if (3 != a->type || 3 != b->type || 3 != d->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-01-07 23:45:18 +11:00
nbpixels = a->width * a->height;
2019-08-09 02:16:20 +11:00
2020-01-07 23:45:18 +11:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-09 02:16:20 +11:00
d->R[idx] = a->R[idx] + b->R[idx];
d->G[idx] = a->G[idx] + b->G[idx];
d->B[idx] = a->B[idx] + b->B[idx];
}
2019-09-11 22:31:10 +11:00
return 0;
2019-08-09 02:16:20 +11:00
}
/* ---------------------------------------------------------------- */
/*
* A - B -> D
*/
int fimg_sub(FloatImg *a, FloatImg *b, FloatImg *d)
{
2020-01-07 23:45:18 +11:00
int idx, nbpixels;
2019-08-09 02:16:20 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
if (3 != a->type || 3 != b->type || 3 != d->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-01-07 23:45:18 +11:00
nbpixels = a->width * a->height;
2019-08-09 02:16:20 +11:00
2019-12-21 22:37:26 +11:00
/* maybe we can speedup this loop for
* avoiding the cache strashing ?
*/
2020-01-07 23:45:18 +11:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-09 02:16:20 +11:00
d->R[idx] = fabs(a->R[idx] - b->R[idx]);
d->G[idx] = fabs(a->G[idx] - b->G[idx]);
d->B[idx] = fabs(a->B[idx] - b->B[idx]);
}
2019-09-11 22:31:10 +11:00
return 0;
2019-08-09 02:16:20 +11:00
}
/* ---------------------------------------------------------------- */
/*
* A * B -> D
*/
int fimg_mul(FloatImg *a, FloatImg *b, FloatImg *d)
{
2020-01-07 23:45:18 +11:00
int idx, nbpixels;
2019-08-09 02:16:20 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
if (3 != a->type || 3 != b->type || 3 != d->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-01-07 23:45:18 +11:00
nbpixels = a->width * a->height;
2019-08-09 02:16:20 +11:00
2020-01-07 23:45:18 +11:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-09 02:16:20 +11:00
d->R[idx] = a->R[idx] * b->R[idx];
d->G[idx] = a->G[idx] * b->G[idx];
d->B[idx] = a->B[idx] * b->B[idx];
}
2019-09-11 22:31:10 +11:00
return 0;
2019-08-09 02:16:20 +11:00
}
/* ---------------------------------------------------------------- */
2019-11-20 21:12:16 +11:00
int fimg_minimum(FloatImg *a, FloatImg *b, FloatImg *d)
{
int idx, nbiter;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
if (3 != a->type || 3 != b->type || 3 != d->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
nbiter = a->width * a->height * 3;
for (idx=0; idx<nbiter; idx++) {
if (a->R[idx] > b->R[idx])
d->R[idx] = a->R[idx];
else
d->R[idx] = b->R[idx];
}
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_maximum(FloatImg *a, FloatImg *b, FloatImg *d)
{
int idx, nbiter;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
if (3 != a->type || 3 != b->type || 3 != d->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
nbiter = a->width * a->height * 3;
for (idx=0; idx<nbiter; idx++) {
if (a->R[idx] < b->R[idx])
d->R[idx] = a->R[idx];
else
d->R[idx] = b->R[idx];
}
return 0;
}
/* ---------------------------------------------------------------- */