FloatImg4PythonBinding/lib/fimg-2gray.c

81 lines
1.8 KiB
C
Raw Normal View History

2019-08-24 13:06:51 +02:00
/*
* fimg-2gray.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* --------------------------------------------------------------------- */
/*
2020-04-02 14:49:10 +02:00
* floating resultat img MUST be allocated before calling this func.
2019-08-24 13:06:51 +02:00
*/
2020-01-10 14:18:38 +01:00
int fimg_mk_gray_from(FloatImg *src, FloatImg *dst, int k)
2019-08-24 13:06:51 +02:00
{
float kr, kg, kb, kdiv;
int nbb, foo;
kr = kg = kb = 1.0; /* canonic random values */
kdiv = kr + kg + kb;
/* we must check the validity of our parameters */
if (FIMG_TYPE_RGB != src->type) {
fprintf(stderr, "%s : bad src type %d on %p\n", __func__,
src->type, src);
return -8;
}
if (FIMG_TYPE_GRAY != dst->type) {
fprintf(stderr, "%s : bad dst type %d on %p\n", __func__,
dst->type, dst);
/*
* may be we can convert dst picture on the fly ?
*/
2019-08-24 13:06:51 +02:00
return -9;
}
/* entering the main processing loop */
nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) {
dst->R[foo] = ( (src->R[foo] * kr) +
(src->G[foo] * kg) +
(src->B[foo] * kb) ) /
kdiv;
}
2019-08-26 02:35:17 +02:00
return 0;
2019-08-24 13:06:51 +02:00
}
/* --------------------------------------------------------------------- */
2020-12-20 12:43:08 +01:00
/* this function can work 'in place' */
2020-12-07 11:16:26 +01:00
int fimg_desaturate(FloatImg *src, FloatImg *dst, int notused)
2020-01-22 18:16:26 +01:00
{
int foo, nbb;
/* we must check the validity of our parameters */
if (FIMG_TYPE_RGB != src->type || FIMG_TYPE_RGB != dst->type) {
2020-01-23 12:36:33 +01:00
fprintf(stderr, "%s : bad image type\n", __func__);
2020-01-22 18:16:26 +01:00
return -18;
}
/* entering the main processing loop */
nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) {
dst->R[foo] = dst->G[foo] = dst->B[foo] =
(src->R[foo] + src->G[foo] + src->B[foo]) / 3.0;
}
2020-09-25 11:43:25 +02:00
return 0;
2020-01-22 18:16:26 +01:00
}
/* --------------------------------------------------------------------- */
2019-08-24 13:06:51 +02:00