FloatImg4PythonBinding/lib/fimg-2gray.c

92 lines
2.0 KiB
C
Raw Normal View History

2019-08-24 22:06:51 +11:00
/*
* fimg-2gray.c
2022-07-11 15:12:25 +11:00
*
* This is a tricky job...
2019-08-24 22:06:51 +11:00
*/
#include <stdio.h>
#include <stdlib.h>
2021-05-20 18:31:28 +11:00
#include <stdint.h>
2019-08-24 22:06:51 +11:00
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* --------------------------------------------------------------------- */
/*
2020-04-02 23:49:10 +11:00
* floating resultat img MUST be allocated before calling this func.
2024-05-02 06:40:44 +11:00
* and MUST be of type 'gray image'.
2019-08-24 22:06:51 +11:00
*/
2020-01-11 00:18:38 +11:00
int fimg_mk_gray_from(FloatImg *src, FloatImg *dst, int k)
2019-08-24 22:06:51 +11:00
{
float kr, kg, kb, kdiv;
int nbb, foo;
2022-07-06 19:27:55 +11:00
if (k) {
/* some random funky values ... */
kr = 0.789; kg = 0.523; kb = 0.349;
}
else {
/* ... and canonic random values */
kr = kg = kb = 1.0;
}
2019-08-24 22:06:51 +11:00
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);
2024-05-02 06:40:44 +11:00
/* may be we can convert dst picture on the fly ? */
2019-08-24 22:06:51 +11: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 11:35:17 +11:00
return 0;
2019-08-24 22:06:51 +11:00
}
/* --------------------------------------------------------------------- */
2020-12-20 22:43:08 +11:00
/* this function can work 'in place' */
2020-12-07 21:16:26 +11:00
int fimg_desaturate(FloatImg *src, FloatImg *dst, int notused)
2020-01-23 04:16:26 +11:00
{
int foo, nbb;
/* we must check the validity of our parameters */
2022-07-06 19:27:55 +11:00
if (notused) {
fprintf(stderr, "notused was %d, must be 0 in %s\n",
notused, __func__);
}
2020-01-23 04:16:26 +11:00
if (FIMG_TYPE_RGB != src->type || FIMG_TYPE_RGB != dst->type) {
2020-01-23 22:36:33 +11:00
fprintf(stderr, "%s : bad image type\n", __func__);
2020-01-23 04:16:26 +11: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 20:43:25 +11:00
return 0;
2020-01-23 04:16:26 +11:00
}
/* --------------------------------------------------------------------- */
2019-08-24 22:06:51 +11:00