FloatImg4PythonBinding/Fonderie/fonctions.c

173 lines
3.8 KiB
C
Raw Normal View History

2020-11-02 11:25:00 +11: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>
2021-01-26 03:18:44 +11:00
#include "../floatimg.h"
2020-11-02 11:25:00 +11:00
#include "fonctions.h"
2020-11-03 00:51:48 +11:00
#include "crapulator.h"
2020-12-18 22:15:32 +11:00
#include "filterstack.h"
2020-11-02 11:25:00 +11:00
/* -------------------------------------------------------------- */
/* global vars from main
*/
extern int verbosity;
/* private vars of this module - it was very dirty
*/
static A_Fifo g_fifo;
/* -------------------------------------------------------------- */
2020-11-07 11:29:45 +11:00
static int big_adder(FloatImg *from, FloatImg *to)
{
int size, idx;
size = from->width * from->height;
2020-11-02 11:25:00 +11:00
2020-11-07 11:29:45 +11:00
for (idx=0; idx<size; idx++) to->R[idx] += from->R[idx];
for (idx=0; idx<size; idx++) to->G[idx] += from->G[idx];
for (idx=0; idx<size; idx++) to->B[idx] += from->B[idx];
return 0;
}
/* -------------------------------------------------------------- */
/*
* 97% of the total run time was in that function \o_
*/
2020-11-02 11:25:00 +11:00
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) {
2020-11-07 11:29:45 +11:00
/***************************************
* Here was the giant bootleneck *
***************************************/
// HERE -> foo = fimg_add_2(&(pfifo->slots[idx]), pdest);
foo = big_adder(&(pfifo->slots[idx]), pdest);
2020-11-02 11:25:00 +11:00
if (foo)
{
fprintf(stderr, "%s: err %d on add_2\n", __func__, foo);
abort();
}
}
return 0;
}
/* -------------------------------------------------------------- */
2020-12-03 00:57:20 +11:00
/* called by 'fonderie.c'
2020-11-02 11:25:00 +11:00
* 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);
2020-12-16 03:49:12 +11:00
if (postproc)
foo = crapulator(&g_fifo.total, postproc, 0.0);
else
foo = filterstack_run(1, &g_fifo.total, 0);
2020-11-02 11:25:00 +11:00
if (foo) {
2020-11-03 00:51:48 +11:00
fprintf(stderr, "%s: ERR post process picture -> %d\n",
__func__, foo);
2020-11-02 11:25:00 +11:00
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];
2020-12-03 00:57:20 +11:00
nbre = dst->width * dst->height * sizeof(float);
memcpy(dst->R, src->R, nbre);
memcpy(dst->G, src->G, nbre);
memcpy(dst->B, src->B, nbre);
2020-11-02 11:25:00 +11:00
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;
}
/* -------------------------------------------------------------- */