FloatImg4PythonBinding/Fonderie/fonctions.c

253 lines
5.3 KiB
C
Raw Normal View History

2020-11-02 01:25:00 +01:00
/*
* Fonctions de la Fonderie du Cumul
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Du code bien cracra / tTh / Tetalab
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <floatimg.h>
#include "fonctions.h"
/* -------------------------------------------------------------- */
/* global vars from main
*/
extern int verbosity;
/* private vars of this module - it was very dirty
*/
static A_Fifo g_fifo;
/* -------------------------------------------------------------- */
int faire_la_somme(A_Fifo *pfifo, FloatImg *destination, int step)
{
int idx, foo;
FloatImg *pdest;
if (NULL==destination) {
pdest = &(pfifo->total); }
else {
pdest = destination; }
fimg_clear(pdest);
for (idx=0; idx<pfifo->nbslots; idx += step) {
foo = fimg_add_2(&(pfifo->slots[idx]), pdest);
if (foo)
{
fprintf(stderr, "%s: err %d on add_2\n", __func__, foo);
abort();
}
}
return 0;
}
/* -------------------------------------------------------------- */
int post_process_picture(FloatImg *image, int numFx, float fval)
{
int retval = 0;
int foo;
float value;
FloatImg imgtmp;
static int count, flag_debug;
if (666==count) {
flag_debug = 1;
fprintf(stderr, "DEBUG POINT 1 in %s\n", __func__);
fimg_save_as_png(image, "source.png", 0);
}
switch (numFx) {
case 0: /* DO NOTHING */ break;
case 1:
fimg_cos_01(image, image,
fimg_get_maxvalue(image));
break;
case 2:
fimg_cos_010(image, image,
fimg_get_maxvalue(image));
break;
case 3:
value = fimg_get_maxvalue(image);
fimg_mul_cste(image, -1.0);
fimg_add_cste(image, value);
foo = fimg_count_negativ(image);
if (foo) {
fimg_dump_to_file(image, "err.fimg", 0);
fprintf(stderr, "%s negativ %d\n", __func__, foo);
return -78;
}
break;
case 4:
brotche_rand48(image, 0.20,
fimg_get_maxvalue(image));
break;
case 5:
brotche_rand48_b(image, 0.10,
fimg_get_maxvalue(image)*0.8);
break;
case 6:
kill_colors_a(image, 0.0);
break;
case 7:
retval = fimg_colors_mixer_a(image, 2.0);
break;
case 8:
fimg_clone(image, &imgtmp, 0);
fimg_clear(&imgtmp);
if (flag_debug) {
fprintf(stderr, "DEBUG A contour 2x2\n");
fimg_save_as_png(image, "before.png", 0);
fimg_dump_to_file(image, "before.fimg", 0);
}
retval = fimg_contour_2x2(image, &imgtmp, 0);
if (retval) {
fprintf(stderr, "%s : err contour %d\n",
__func__, retval);
exit(1);
}
if (flag_debug) {
fprintf(stderr, "DEBUG B contour 2x2\n");
// fimg_save_as_png(&imgtmp, "contour.png", 0);
fimg_dump_to_file(&imgtmp, "contour.fimg", 0);
}
fimg_copy_data(&imgtmp, image);
fimg_destroy(&imgtmp);
break;
case 9:
retval = fimg_classif_trial(image, image, 0.42, 0);
if (retval) {
fprintf(stderr, "err %d in classif\n", retval);
exit(1);
}
break;
default :
fprintf(stderr, "%s : postproc #%d invalid\n",
__func__, numFx);
return -77;
}
if (flag_debug) {
fprintf(stderr, "DEBUG POINT 2 in %s\n", __func__);
fimg_save_as_png(image, "after.png", 0);
}
count++; flag_debug = 0;
return retval;
}
/* -------------------------------------------------------------- */
/*
* this func save the fifo content as
* - float FIMG
* - 16 bits PNM
* - 8 bits PNG
*/
int export_fifo(char *fname, int postproc, int step)
{
int foo, type;
foo = faire_la_somme(&g_fifo, NULL, step);
/* BEGIN GRUIK CODE */
extern int convert_to_gray;
/* END OF THE KLUGE */
if (convert_to_gray) {
fimg_to_gray(&g_fifo.total);
// fprintf(stderr, "%p gray-washed\n", &fifo.total);
}
foo = post_process_picture(&g_fifo.total, postproc, 0.0);
if (foo) {
fprintf(stderr, "ERR post process picture -> %d\n", foo);
return foo;
}
type = format_from_extension(fname);
switch (type) {
case FILE_TYPE_PNG:
foo = fimg_save_as_png(&g_fifo.total, fname, 0);
break;
case FILE_TYPE_PNM:
foo = fimg_save_as_pnm(&g_fifo.total, fname, 0);
break;
case FILE_TYPE_FIMG:
foo = fimg_dump_to_file(&g_fifo.total, fname, 0);
break;
default:
fprintf(stderr, "%s : type of '%s' unknow\n",
__func__, fname);
foo = 888;
break;
}
if (foo) {
fprintf(stderr, "ERR EXPORT '%s' is %d\n", fname, foo);
exit(3);
}
return 0;
}
/* -------------------------------------------------------------- */
int insert_picture(FloatImg *src)
{
FloatImg *dst;
int nbre;
/*
* this is the where we can insert the 'input filter'
*/
dst = &g_fifo.slots[g_fifo.next];
nbre = dst->width * dst->height * dst->type;
memcpy(dst->R, src->R, nbre*sizeof(float));
g_fifo.next++, g_fifo.next %= g_fifo.nbslots;
// maybe we can write :
// (++fifo.next) %= fifo.nbslots;
if (verbosity > 2) fprintf(stderr, "%s : next slot %d\n",
__func__, g_fifo.next);
return 0;
}
/* -------------------------------------------------------------- */
int create_fifo(int nbslot, int w, int h, int t)
{
int foo, idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d %dx%d %d )\n", __func__,
nbslot, w, h, t);
#endif
memset(&g_fifo, 0, sizeof(A_Fifo));
g_fifo.nbslots = nbslot;
g_fifo.slots = calloc(nbslot, sizeof(FloatImg));
if (NULL==g_fifo.slots) abort();
for (idx=0; idx<nbslot; idx++) {
foo = fimg_create(&g_fifo.slots[idx], w, h, t);
if (foo) {
fprintf(stderr, "%s idx %d err%d\n", __func__, idx, foo);
return foo;
}
}
foo = fimg_create(&g_fifo.total, w, h, t);
g_fifo.next = 0;
g_fifo.magic = MAGIC_FIFO;
return 0;
}
/* -------------------------------------------------------------- */