FloatImg/Fonderie/fonctions.c

181 lines
4.0 KiB
C

/*
* 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"
#include "crapulator.h"
#include "filterstack.h"
/* -------------------------------------------------------------- */
/* global vars from main
*/
extern int verbosity;
/* private vars of this module - it was very dirty
*/
static A_Fifo g_fifo;
/* -------------------------------------------------------------- */
static int big_adder(FloatImg *from, FloatImg *to)
{
int size, idx;
size = from->width * from->height;
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_
*/
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) {
/***************************************
* Here was the giant bootleneck *
***************************************/
// HERE -> foo = fimg_add_2(&(pfifo->slots[idx]), pdest);
foo = big_adder(&(pfifo->slots[idx]), pdest);
if (foo)
{
fprintf(stderr, "%s: err %d on add_2\n", __func__, foo);
abort();
}
}
return 0;
}
/* -------------------------------------------------------------- */
/* called by 'fonderie.c'
* 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);
}
if (postproc)
foo = crapulator(&g_fifo.total, postproc, 0.0);
else
foo = filterstack_run(1, &g_fifo.total, 0);
if (foo) {
fprintf(stderr, "%s: ERR post process picture -> %d\n",
__func__, 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 * sizeof(float);
memcpy(dst->R, src->R, nbre);
memcpy(dst->G, src->G, nbre);
memcpy(dst->B, src->B, nbre);
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;
}
/* -------------------------------------------------------------- */