FloatImg/lib/operators.c

218 lines
4.7 KiB
C
Raw Normal View History

2019-08-08 17:16:20 +02:00
/*
* OPERATORS
*
2020-01-23 12:36:33 +01:00
* operations entre des images.
2019-08-08 17:16:20 +02:00
*/
#include <stdio.h>
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2019-08-08 17:16:20 +02:00
#include <unistd.h>
#include <string.h>
2019-08-24 03:40:53 +02:00
#include <math.h>
2019-08-08 17:16:20 +02:00
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
2019-12-21 12:37:26 +01:00
/*
* A + B -> D
2019-12-21 12:37:26 +01:00
*/
int fimg_add_3(FloatImg *a, FloatImg *b, FloatImg *d)
2019-08-08 17:16:20 +02:00
{
2020-01-07 13:45:18 +01:00
int idx, nbpixels;
2019-08-08 17:16:20 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type ||
FIMG_TYPE_RGB != b->type ||
FIMG_TYPE_RGB != d->type) {
2019-08-08 17:16:20 +02:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-01-07 13:45:18 +01:00
nbpixels = a->width * a->height;
2019-08-08 17:16:20 +02:00
2020-01-07 13:45:18 +01:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-08 17:16:20 +02: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];
}
return 0;
}
/* ---------------------------------------------------------------- */
/*
* B += A may be faster than fimg_add_3 ?
*/
int fimg_add_2(FloatImg *a, FloatImg *b)
{
int idx, nbpixels;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, a, b);
#endif
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type) {
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
nbpixels = a->width * a->height;
for (idx=0; idx<nbpixels; idx++) {
b->R[idx] += a->R[idx];
b->G[idx] += a->G[idx];
b->B[idx] += a->B[idx];
}
2019-09-11 13:31:10 +02:00
return 0;
2019-08-08 17:16:20 +02:00
}
/* ---------------------------------------------------------------- */
/*
* A - B -> D
*/
int fimg_sub_3(FloatImg *a, FloatImg *b, FloatImg *d)
2019-08-08 17:16:20 +02:00
{
2020-01-07 13:45:18 +01:00
int idx, nbpixels;
2019-08-08 17:16:20 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
2020-06-23 10:08:59 +02:00
if ( FIMG_TYPE_RGB != a->type ||
FIMG_TYPE_RGB != b->type ||
2020-01-22 09:33:35 +01:00
FIMG_TYPE_RGB != d->type) {
2019-08-08 17:16:20 +02:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2019-08-08 17:16:20 +02:00
2020-01-07 13:45:18 +01:00
nbpixels = a->width * a->height;
2019-08-08 17:16:20 +02:00
2019-12-21 12:37:26 +01:00
/* maybe we can speedup this loop for
* avoiding the cache strashing ?
*/
2020-01-07 13:45:18 +01:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-08 17:16:20 +02: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 13:31:10 +02:00
return 0;
2020-01-17 10:53:45 +01:00
}
/* ---------------------------------------------------------------- */
/*
* B *= A may be faster than fimg_mul_3 ?
*/
int fimg_mul_2(FloatImg *a, FloatImg *b)
{
int idx, nbpixels;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, a, b);
#endif
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type) {
2020-01-17 10:53:45 +01:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
nbpixels = a->width * a->height;
for (idx=0; idx<nbpixels; idx++) {
b->R[idx] *= a->R[idx];
b->G[idx] *= a->G[idx];
b->B[idx] *= a->B[idx];
}
2020-01-22 09:33:35 +01:00
return 0;
2019-08-08 17:16:20 +02:00
}
/* ---------------------------------------------------------------- */
/*
* A * B -> D
*/
int fimg_mul_3(FloatImg *a, FloatImg *b, FloatImg *d)
2019-08-08 17:16:20 +02:00
{
2020-01-07 13:45:18 +01:00
int idx, nbpixels;
2019-08-08 17:16:20 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
#endif
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type ||
FIMG_TYPE_RGB != d->type) {
2019-08-08 17:16:20 +02:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-01-07 13:45:18 +01:00
nbpixels = a->width * a->height;
2019-08-08 17:16:20 +02:00
2020-01-07 13:45:18 +01:00
for (idx=0; idx<nbpixels; idx++) {
2019-08-08 17:16:20 +02: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 13:31:10 +02:00
return 0;
2019-08-08 17:16:20 +02:00
}
/* ---------------------------------------------------------------- */
2019-11-20 11:12:16 +01: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
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type ||
FIMG_TYPE_RGB != d->type) {
2019-11-20 11:12:16 +01:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-06-23 10:08:59 +02:00
nbiter = a->width * a->height;
2019-11-20 11:12:16 +01:00
for (idx=0; idx<nbiter; idx++) {
2021-05-15 19:41:42 +02:00
if (a->R[idx] < b->R[idx]) d->R[idx] = a->R[idx];
2020-06-23 10:08:59 +02:00
else d->R[idx] = b->R[idx];
2021-05-15 19:41:42 +02:00
if (a->G[idx] < b->G[idx]) d->G[idx] = a->G[idx];
2020-06-23 10:08:59 +02:00
else d->G[idx] = b->G[idx];
2021-05-15 19:41:42 +02:00
if (a->B[idx] < b->B[idx]) d->B[idx] = a->B[idx];
2020-06-23 10:08:59 +02:00
else d->B[idx] = b->B[idx];
2019-11-20 11:12:16 +01:00
}
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
2020-01-22 09:33:35 +01:00
if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type ||
FIMG_TYPE_RGB != d->type) {
2019-11-20 11:12:16 +01:00
fprintf(stderr, "%s : got a bad type fimg\n", __func__);
return -8;
}
2020-06-23 10:08:59 +02:00
nbiter = a->width * a->height ;
2019-11-20 11:12:16 +01:00
for (idx=0; idx<nbiter; idx++) {
2021-05-15 19:41:42 +02:00
if (a->R[idx] > b->R[idx]) d->R[idx] = a->R[idx];
2020-06-23 10:08:59 +02:00
else d->R[idx] = b->R[idx];
2021-05-15 19:41:42 +02:00
if (a->G[idx] > b->G[idx]) d->G[idx] = a->G[idx];
2020-06-23 10:08:59 +02:00
else d->G[idx] = b->G[idx];
2021-05-15 19:41:42 +02:00
if (a->B[idx] > b->B[idx]) d->B[idx] = a->B[idx];
2020-06-23 10:08:59 +02:00
else d->B[idx] = b->B[idx];
2019-11-20 11:12:16 +01:00
}
return 0;
}
/* ---------------------------------------------------------------- */