FloatImg/Fonderie/fifo.c

199 lines
4.2 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>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2020-11-02 01:25:00 +01:00
#include <stdlib.h>
#include <malloc.h>
2021-01-25 17:18:44 +01:00
#include "../floatimg.h"
2020-11-02 01:25:00 +01:00
2021-04-02 19:25:20 +02:00
#include "fifo.h"
2020-11-02 14:51:48 +01:00
#include "crapulator.h"
2020-12-18 12:15:32 +01:00
#include "filterstack.h"
2020-11-02 01:25:00 +01:00
/* -------------------------------------------------------------- */
/* global vars from main
*/
extern int verbosity;
2021-04-03 05:37:03 +02:00
/* private vars of this module - it was very dirty,
* but simple and efficient.
2020-11-02 01:25:00 +01:00
*/
static A_Fifo g_fifo;
/* -------------------------------------------------------------- */
2022-02-04 23:39:52 +01:00
static inline int big_adder(FloatImg *from, FloatImg *to)
2020-11-07 01:29:45 +01:00
{
int size, idx;
size = from->width * from->height;
2020-11-02 01:25:00 +01:00
2020-11-07 01:29:45 +01: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 01:25:00 +01:00
int faire_la_somme(A_Fifo *pfifo, FloatImg *destination, int step)
{
int idx, foo;
FloatImg *pdest;
2021-04-03 05:37:03 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__,
pfifo, destination, step);
#endif
2021-05-10 00:04:10 +02:00
if (step<1){
fprintf(stderr, "***** %s invalid step %d\n", __func__, step);
exit(1);
}
2020-11-02 01:25:00 +01:00
if (NULL==destination) {
pdest = &(pfifo->total); }
else {
pdest = destination; }
fimg_clear(pdest);
2021-04-02 04:16:26 +02:00
2020-11-02 01:25:00 +01:00
for (idx=0; idx<pfifo->nbslots; idx += step) {
2021-04-02 04:16:26 +02:00
2020-11-07 01:29:45 +01:00
foo = big_adder(&(pfifo->slots[idx]), pdest);
2020-11-02 01:25:00 +01:00
if (foo)
{
fprintf(stderr, "%s: err %d on add_2\n", __func__, foo);
abort();
}
}
2021-05-10 00:04:10 +02:00
#if 0
2021-04-03 05:37:03 +02:00
fprintf(stderr, "*** %s:%s writing debugB file ***\n",
2021-04-02 04:16:26 +02:00
__FILE__, __func__);
fimg_dump_to_file(&g_fifo.total, "debugB.fimg", 0);
2021-05-10 00:04:10 +02:00
#endif
2021-04-02 04:16:26 +02:00
2020-11-02 01:25:00 +01:00
return 0;
}
/* -------------------------------------------------------------- */
2020-12-02 14:57:20 +01:00
/* called by 'fonderie.c'
2020-11-02 01:25:00 +01:00
*/
2021-04-23 23:54:47 +02:00
int export_fifo(char *fname, int notused)
2020-11-02 01:25:00 +01:00
{
2024-03-21 16:10:39 +01:00
int foo;
2020-11-02 01:25:00 +01:00
2021-04-03 05:37:03 +02:00
#if DEBUG_LEVEL
2022-02-04 23:39:52 +01:00
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, notused);
2021-04-03 05:37:03 +02:00
#endif
foo = faire_la_somme(&g_fifo, NULL, 1);
2020-11-02 01:25:00 +01:00
2021-04-02 04:16:26 +02:00
foo = filterstack_run(1, &g_fifo.total, 0);
2020-11-02 01:25:00 +01:00
if (foo) {
2020-11-02 14:51:48 +01:00
fprintf(stderr, "%s: ERR post process picture -> %d\n",
__func__, foo);
2020-11-02 01:25:00 +01:00
return foo;
}
2021-05-10 00:04:10 +02:00
#if 0
fimg_dump_to_file(&g_fifo.total, "outputXXX.fimg", 0);
#endif
2021-04-03 05:37:03 +02:00
foo = fimg_export_picture(&g_fifo.total, fname, 0);
2020-11-02 01:25:00 +01:00
if (foo) {
fprintf(stderr, "ERR EXPORT '%s' is %d\n", fname, foo);
exit(3);
}
return 0;
}
/* -------------------------------------------------------------- */
int insert_picture(FloatImg *src)
{
FloatImg *dst;
2024-03-21 16:10:39 +01:00
int nbre;
2021-04-03 05:37:03 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p )\n", __func__, src);
#endif
2020-11-02 01:25:00 +01:00
dst = &g_fifo.slots[g_fifo.next];
2020-12-02 14:57:20 +01: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 01:25:00 +01:00
2021-04-03 05:37:03 +02:00
/* XXX
fprintf(stderr, "*** %s:%s writing debugA file ***\n",
2021-04-02 04:16:26 +02:00
__FILE__, __func__);
2021-04-03 05:37:03 +02:00
foo = fimg_dump_to_file(dst, "debugA.fimg", 0);
fprintf(stderr, " ok file dumped %d\n", foo);
XXX */
2021-04-02 04:16:26 +02:00
g_fifo.next++; g_fifo.next %= g_fifo.nbslots;
2020-11-02 01:25:00 +01:00
if (verbosity > 2) fprintf(stderr, "%s : next slot %d\n",
__func__, g_fifo.next);
return 0;
}
/* -------------------------------------------------------------- */
2021-04-23 23:54:47 +02:00
/*
* this function must have a lot of new parameters,
* -- filetype of exported pictures...
*/
int create_fifo(int nbslot, int w, int h, int imgtype)
2020-11-02 01:25:00 +01:00
{
int foo, idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d %dx%d %d )\n", __func__,
2021-04-23 23:54:47 +02:00
nbslot, w, h, imgtype);
2020-11-02 01:25:00 +01:00
#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++) {
2021-04-23 23:54:47 +02:00
foo = fimg_create(&g_fifo.slots[idx], w, h, imgtype);
2020-11-02 01:25:00 +01:00
if (foo) {
fprintf(stderr, "%s idx %d err%d\n", __func__, idx, foo);
return foo;
}
2021-04-02 04:16:26 +02:00
fimg_clear(&g_fifo.slots[idx]);
2020-11-02 01:25:00 +01:00
}
2021-04-23 23:54:47 +02:00
foo = fimg_create(&g_fifo.total, w, h, imgtype);
2020-11-02 01:25:00 +01:00
g_fifo.next = 0;
g_fifo.magic = MAGIC_FIFO;
return 0;
}
/* -------------------------------------------------------------- */
2021-04-23 23:54:47 +02:00
/*
* omfg ! I've write a setter !
*/
int set_fifo_output_format(int filetype)
{
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d )\n", __func__, filetype);
#endif
g_fifo.export_type = filetype;
return -1;
}