FloatImg/Fonderie/sfx.c

380 lines
8.7 KiB
C
Raw Normal View History

2020-11-02 11:25:00 +11:00
/*
* SPECIAL EFFECTS
*
* Du code bien cracra / tTh / Tetalab
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
2020-12-29 22:56:26 +11:00
#include <math.h>
2020-11-02 11:25:00 +11:00
2020-12-03 05:55:06 +11:00
#include "../floatimg.h"
2020-11-02 11:25:00 +11:00
#include "fonctions.h"
2020-12-03 05:55:06 +11:00
#include "sfx.h"
2020-11-02 11:25:00 +11:00
/* -------------------------------------------------------------- */
2020-11-05 22:48:35 +11:00
/* here are global vars exported by the main module
2020-11-02 11:25:00 +11:00
*/
2020-11-03 00:51:48 +11:00
extern int verbosity;
2020-11-02 11:25:00 +11:00
2020-12-29 10:54:15 +11:00
/* -------------------------------------------------------------- */
/* nouveau du 27 decembre 2020, un soir de grand froid... */
2020-12-29 22:56:26 +11:00
int octotree_classif(FloatImg *pimg, float kdist, int notused)
2020-12-29 10:54:15 +11:00
{
int foo;
2020-12-29 22:56:26 +11:00
float mm[6], means[4], delta[3];
float r, g, b, kr, kg, kb, dp, trig;
int idx, sz, n8, count;
typedef struct {
float x, y, z;
} ptc_t;
ptc_t ptc[8];
2020-12-29 10:54:15 +11:00
2020-12-29 23:08:19 +11:00
#if DEBUG_LEVEL
2020-12-29 22:56:26 +11:00
fprintf(stderr, ">>> %s ( %p %f %d )\n", __func__,
pimg, kdist, notused);
2020-12-29 23:08:19 +11:00
#endif
2020-12-29 22:56:26 +11:00
#if 0
2020-12-29 10:54:15 +11:00
foo = fimg_meanvalues(pimg, means);
if (foo) {
fprintf(stderr, "oups %d in meanvalues\n", foo);
return foo;
}
2020-12-29 22:56:26 +11:00
fprintf(stderr, "means: %11f %11f %11f\n",
means[0], means[1], means[2]);
#endif
2020-12-29 10:54:15 +11:00
foo = fimg_get_minmax_rgb(pimg, mm);
if (foo) {
fprintf(stderr, "oups %d in get minmax\n", foo);
return foo;
}
2020-12-29 22:56:26 +11:00
if (verbosity) fimg_print_minmax(mm, "input pic");
2020-12-29 10:54:15 +11:00
/*
2020-12-29 22:56:26 +11:00
* compute the 8 center points
*/
delta[0] = mm[1] - mm[0]; /* R */
delta[1] = mm[3] - mm[2]; /* G */
delta[2] = mm[5] - mm[4]; /* B */
2020-12-29 23:08:19 +11:00
// fprintf(stderr, "delta: %11.3f %11.3f %11.3f\n",
// delta[0], delta[1], delta[2]);
2020-12-29 22:56:26 +11:00
for (idx=0; idx<8; idx++) {
kr = 0.25 * ((idx & 0x4) ? 1 : 3);
kg = 0.25 * ((idx & 0x2) ? 1 : 3);
kb = 0.25 * ((idx & 0x1) ? 1 : 3);
// fprintf(stderr, "%6d %.2f %.2f %.2f\n", idx, kr, kg, kb);
ptc[idx].x = (delta[0] * kr) + mm[0];
ptc[idx].y = (delta[1] * kg) + mm[2];
ptc[idx].z = (delta[2] * kb) + mm[4];
2020-12-29 23:08:19 +11:00
// fprintf(stderr, "%6d %.3f %.3f %.3f\n", idx,
// ptc[idx].x, ptc[idx].y, ptc[idx].z);
2020-12-29 22:56:26 +11:00
}
sz = pimg->width * pimg->height;
trig = kdist * ((mm[1] + mm[3] + mm[5])/6.0);
2020-12-29 23:08:19 +11:00
// fprintf(stderr, "trig value %f\n", trig);
2020-12-29 10:54:15 +11:00
2020-12-29 22:56:26 +11:00
count = 0;
#define X(a,b) ( ((a)-(b)) * ((a)-(b)) )
for (idx=0; idx<sz; idx++) {
r = pimg->R[idx]; g = pimg->G[idx]; b = pimg->B[idx];
for (n8=0; n8<8; n8++) {
dp = sqrt(X(r,ptc[n8].x) + X(g,ptc[n8].y) + X(b,ptc[n8].z) );
if (dp < trig) {
pimg->R[idx] = ptc[n8].x;
pimg->G[idx] = ptc[n8].y;
pimg->B[idx] = ptc[n8].z;
count++;
break;
}
else {
pimg->R[idx]=pimg->G[idx]=pimg->B[idx]=0.0;
}
}
}
2020-12-29 10:54:15 +11:00
2020-12-29 23:08:19 +11:00
fprintf(stderr, "%s: %d/%d pixels, ratio %f\n", __func__, count, sz,
(float)count/(float)sz);
2020-12-29 10:54:15 +11:00
2020-12-29 22:56:26 +11:00
return 0;
2020-12-29 10:54:15 +11:00
}
2020-12-18 20:18:09 +11:00
/* -------------------------------------------------------------- */
/* nouveau du 19 decembre 2020, pour le grand ecran de da Scritch */
int upside_down(FloatImg *pimg)
{
float *rowpix;
float *Ps, *Pd;
int Os, Od; /* offset of lines */
int wsz;
int ya, y2;
2020-12-19 18:48:17 +11:00
if (verbosity>1) fprintf(stderr, "%s: image width is %d\n",
2020-12-18 22:16:24 +11:00
__func__, pimg->width);
2020-12-18 20:18:09 +11:00
rowpix = calloc(pimg->width, sizeof(float));
if (NULL==rowpix) {
fprintf(stderr, "%s : memory full\n", __func__);
exit(1);
}
wsz = pimg->width * sizeof(float);
2020-12-19 18:48:17 +11:00
if (verbosity>1) fprintf(stderr, "%s: wsx = %d\n", __func__, wsz);
2020-12-18 20:18:09 +11:00
for (ya=0; ya<pimg->height/2; ya++) {
y2 = pimg->height - (ya+1);
Os = (pimg->width * ya);
Od = (pimg->width * y2);
/* let's go, crash coredumping... */
Ps = pimg->R + Os;
Pd = pimg->R + Od;
memcpy(rowpix, Ps, wsz);
memcpy(Ps, Pd, wsz);
memcpy(Pd, rowpix, wsz);
Ps = pimg->G + Os;
Pd = pimg->G + Od;
memcpy(rowpix, Ps, wsz);
memcpy(Ps, Pd, wsz);
memcpy(Pd, rowpix, wsz);
Ps = pimg->B + Os;
Pd = pimg->B + Od;
memcpy(rowpix, Ps, wsz);
memcpy(Ps, Pd, wsz);
memcpy(Pd, rowpix, wsz);
}
free(rowpix);
return 0;
}
2020-12-11 05:19:35 +11:00
/* -------------------------------------------------------------- */
/* nouveau du 9 decembre 2020, en ecoutant le Fermion raconter du
superbe portnawak */
int bouger_les_pixels(FloatImg *pimg, int intensite)
{
int x, y, nx, ny;
float rgb[3];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, intensite);
#endif
if (intensite < 1) {
fprintf(stderr, "%s: %d bad intensity\n", __func__, intensite);
return -1;
}
for (x=0; x<pimg->width; x++) {
for (y=0; y<pimg->height; y++) {
nx = x+(rand()%intensite)-(intensite/2);
ny = y+(rand()%intensite)-(intensite/2);
if ( nx<0 || ny<0 || nx>=pimg->width
|| ny>=pimg->height )
continue;
fimg_get_rgb(pimg, nx, ny, rgb);
fimg_put_rgb(pimg, x, y, rgb);
}
}
return 0;
}
/* -------------------------------------------------------------- */
/* nouveau du 9 decembre 2020, en ecoutant les Cernettes */
int mirror_split(FloatImg *pimg, int kaboo)
{
int line, x, xs, xd;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, kaboo);
#endif
for (line=0; line<pimg->height; line++) {
for (x=0; x<pimg->width/2; x++) {
xs = (pimg->width * line) + x;
xd = (pimg->width * line) + (pimg->width -x);
pimg->R[xd] = pimg->R[xs];
pimg->G[xd] = pimg->G[xs];
pimg->B[xd] = pimg->B[xs];
}
}
return 0;
}
2020-11-21 08:25:30 +11:00
/* -------------------------------------------------------------- */
/* nouveau du 20 novembre 2020, pour encoder une vidz du vernissage
* du festival Sauvageonnes de Mixart-Myrys */
int des_bords_sombres_a(FloatImg *pimg, int offset)
{
float coef;
2020-11-25 20:32:02 +11:00
int xpos, xp2, lidx, y;
2020-11-21 08:25:30 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, offset);
#endif
2020-11-25 20:32:02 +11:00
if (offset<0 || offset>=pimg->width) {
2020-11-21 08:25:30 +11:00
fprintf(stderr, "%s offset %d is bad\n", __func__, offset);
return -66;
}
for (y=0; y<pimg->height; y++) {
2020-11-25 20:32:02 +11:00
lidx = y * pimg->width; /* start of the
'y' line */
2020-11-21 08:25:30 +11:00
for (xpos=0; xpos<offset; xpos++) {
coef = (float)xpos / (float)offset;
pimg->R[xpos+lidx] *= coef;
pimg->G[xpos+lidx] *= coef;
pimg->B[xpos+lidx] *= coef;
2020-11-25 20:32:02 +11:00
xp2 = pimg->width-xpos;
pimg->R[xp2+lidx] *= coef;
pimg->G[xp2+lidx] *= coef;
pimg->B[xp2+lidx] *= coef;
2020-11-21 08:25:30 +11:00
}
}
return 0;
}
2020-11-10 10:50:25 +11:00
/* -------------------------------------------------------------- */
int trinarize(FloatImg *pimg, int notused)
{
2020-11-25 20:32:02 +11:00
float mm[6], mRa, mGa, mBa, mRb, mGb, mBb;
2020-12-18 20:18:09 +11:00
float *fptr;
2020-11-25 20:32:02 +11:00
int foo, size;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, notused);
#endif
foo = fimg_get_minmax_rgb(pimg, mm);
mRa = (mm[1] - mm[0]) * 0.33333;
mGa = (mm[3] - mm[2]) * 0.33333;
mBa = (mm[5] - mm[4]) * 0.33333;
mRb = (mm[1] - mm[0]) * 0.66666;
mGb = (mm[3] - mm[2]) * 0.66666;
mBb = (mm[5] - mm[4]) * 0.66666;
size = pimg->width * pimg->height;
for (foo=0; foo<size; foo++) {
2020-12-01 23:10:27 +11:00
fptr = pimg->R;
if (fptr[foo] < mRa || fptr[foo] > mRb)
2020-12-03 05:55:06 +11:00
fptr[foo] = mm[0];
2020-12-01 23:10:27 +11:00
else
2020-12-02 01:26:15 +11:00
fptr[foo] = mm[1];
2020-11-25 20:32:02 +11:00
2020-12-01 23:10:27 +11:00
fptr = pimg->G;
if (fptr[foo] < mGa || fptr[foo] > mGb)
2020-12-03 05:55:06 +11:00
fptr[foo] = mm[2];
2020-12-01 23:10:27 +11:00
else
2020-12-02 01:26:15 +11:00
fptr[foo] = mm[3];
2020-11-10 10:50:25 +11:00
2020-12-01 23:10:27 +11:00
fptr = pimg->B;
if (fptr[foo] < mBa || fptr[foo] > mBb)
2020-12-03 05:55:06 +11:00
fptr[foo] = mm[4];
2020-12-01 23:10:27 +11:00
else
2020-12-02 01:26:15 +11:00
fptr[foo] = mm[5];
2020-12-01 23:10:27 +11:00
}
2020-11-10 10:50:25 +11:00
2020-12-01 23:26:00 +11:00
return 0;
2020-11-10 10:50:25 +11:00
}
2020-11-02 11:25:00 +11:00
/* -------------------------------------------------------------- */
2020-11-05 22:48:35 +11:00
int binarize(FloatImg *pimg, int notused)
{
float mm[6], mR, mG, mB;
2020-11-06 07:07:38 +11:00
int foo, size;
2020-11-05 22:48:35 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, notused);
#endif
foo = fimg_get_minmax_rgb(pimg, mm);
mR = (mm[1] - mm[0]) / 2.0;
mG = (mm[3] - mm[2]) / 2.0;
mB = (mm[5] - mm[4]) / 2.0;
if (verbosity > 1)
2020-11-06 07:07:38 +11:00
fprintf(stderr, "%s: %f %f %f\n", __func__, mR, mG, mB);
size = pimg->width * pimg->height;
for (foo=0; foo<size; foo++) {
if (pimg->R[foo] < mR) pimg->R[foo] = mm[0];
else pimg->R[foo] = mm[1];
if (pimg->G[foo] < mG) pimg->G[foo] = mm[2];
else pimg->G[foo] = mm[3];
if (pimg->B[foo] < mB) pimg->B[foo] = mm[4];
else pimg->B[foo] = mm[5];
2020-11-05 22:48:35 +11:00
}
2020-11-02 11:25:00 +11:00
2020-11-05 22:48:35 +11:00
return 0;
}
2020-11-03 00:51:48 +11:00
/* -------------------------------------------------------------- */
int brotche_rand48_a(FloatImg *fimg, float ratio, float mval)
2020-11-02 11:25:00 +11:00
{
int nbpix, todo, foo;
int x, y;
float fval;
nbpix = fimg->width * fimg->height;
todo = (int)((float)nbpix * ratio);
2020-11-03 00:51:48 +11:00
if (verbosity > 1) {
fprintf(stderr, "%s: ratio %f nbpix %d todo %d\n", __func__,
ratio, nbpix, todo);
}
2020-11-02 11:25:00 +11:00
for (foo=0; foo<todo; foo++)
{
fval = (float)drand48() * mval;
x = rand() % fimg->width;
y = rand() % fimg->height;
fimg_plot_rgb(fimg, x, y, fval, fval, fval);
}
return 0;
}
/* -------------------------------------------------------------- */
int brotche_rand48_b(FloatImg *fimg, float ratio, float mval)
{
int nbpix, todo, foo;
int x, y;
float fval;
nbpix = fimg->width * fimg->height;
todo = (int)((float)nbpix * ratio);
if (verbosity > 1) {
2020-11-03 00:51:48 +11:00
fprintf(stderr, "%s: ratio %f nbpix %d todo %d\n", __func__,
2020-11-02 11:25:00 +11:00
ratio, nbpix, todo);
}
for (foo=0; foo<todo; foo++)
{
fval = (float)drand48() * mval;
x = 1 + (rand() % (fimg->width-2));
y = rand() % fimg->height;
fimg_plot_rgb(fimg, x-1, y, fval, 0.0, 0.0);
fimg_plot_rgb(fimg, x , y, 0.0, 0.0, fval);
fimg_plot_rgb(fimg, x+1, y, 0.0, fval, 0.0);
}
return 0;
}
/* -------------------------------------------------------------- */