FloatImg4PythonBinding/funcs/pixelize.c

103 lines
2.1 KiB
C
Raw Normal View History

2021-10-10 23:38:10 +02:00
/*
* P I X E L I Z E
*/
#include <stdio.h>
#include <stdint.h>
2021-10-15 23:21:02 +02:00
#include <stdlib.h>
2021-10-10 23:38:10 +02:00
#include "../floatimg.h"
extern int verbosity;
/* -------------------------------------------------------------- */
2021-10-15 23:21:02 +02:00
/* nouveau 10 octobre 2021 dans la roulotte de Terreblanque */
#define LARGEUR 16
int fimg_pixelize_h_0(FloatImg *psrc, FloatImg *pdst, int largeur)
2021-10-10 23:38:10 +02:00
{
int line, col, loop, idx;
float cr, cg, cb; /* cumuls */
2021-10-15 23:21:02 +02:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, psrc, pdst, largeur);
#endif
2021-10-10 23:38:10 +02:00
if (fimg_images_not_compatible(psrc, pdst)) {
fprintf(stderr, "%s: err compatibility\n", __func__);
return -8;
}
2021-10-15 23:21:02 +02:00
switch(largeur) {
case 8: case 16: case 32:
break;
default:
fprintf(stderr, "pixeliz bad width %d\n", largeur);
return -77;
}
2021-10-10 23:38:10 +02:00
for (line=0; line<psrc->height; line++) {
2021-10-15 23:21:02 +02:00
for (col=0; col<psrc->width; col+=largeur) {
2021-10-10 23:38:10 +02:00
cr = cg = cb = 0.0;
idx = line * psrc->width + col;
2021-10-15 23:21:02 +02:00
for (loop=0; loop<largeur; loop++) {
2021-10-10 23:38:10 +02:00
cr += psrc->R[idx+loop];
cg += psrc->G[idx+loop];
cb += psrc->B[idx+loop];
}
2021-10-15 23:21:02 +02:00
for (loop=0; loop<largeur; loop++) {
pdst->R[idx+loop] = cr / (float)largeur;
pdst->G[idx+loop] = cg / (float)largeur;
pdst->B[idx+loop] = cb / (float)largeur;
2021-10-10 23:38:10 +02:00
}
}
}
return 0;
}
/* -------------------------------------------------------------- */
2021-10-15 23:21:02 +02:00
/*
* un essai dans la roulotte :)
* 11 oct 2021 : premier jet, essai concluant, mais nécessite
* du travail sur les rand() pour être plus 'noisy'
*
*/
int fimg_pixelize_h_rnd(FloatImg *psrc, FloatImg *pdst, int largeur)
{
static int count = 0;
static int flag = 0;
int foo;
2021-10-10 23:38:10 +02:00
2021-10-15 23:21:02 +02:00
if (0==count) {
if (flag) {
count = irand2(5, 10);
flag = ! flag;
}
else {
count = irand2(20, 40);
flag = ! flag;
}
if (verbosity) {
fprintf(stderr, "%s c=%d f=%c\n", __func__,
count, flag?'T':'F');
}
}
if (verbosity) {
fprintf(stderr, "%s: count=%d flag=%d\n", __func__, count, flag);
}
foo = fimg_pixelize_h_0(psrc, pdst, flag ? 8 : 32);
if (foo) {
fprintf(stderr, "PANIC in %s\n", __func__);
abort();
}
count--; /* nice trick bro */
return 0;
}
2021-10-10 23:38:10 +02:00
/* -------------------------------------------------------------- */