FloatImg/Fonderie/glitches.c

317 lines
7.3 KiB
C
Raw Normal View History

2020-11-10 03:58:18 +01:00
/*
* glitches.c
* ----------
*
* initially developped for the interpolator
2020-11-10 03:58:18 +01:00
*/
#include <stdio.h>
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2020-12-31 00:46:12 +01:00
#include <string.h>
#include <math.h>
2020-11-10 03:58:18 +01:00
#include "../floatimg.h"
#include "glitches.h"
2020-11-10 03:58:18 +01:00
extern int verbosity;
2021-05-11 10:36:55 +02:00
/* -------------------------------------------------------------- */
/* 10 mai 2021 a Terre Blanque */
int microglitch(FloatImg *pimg, int notused)
{
int surface;
int offset, idx;
surface = pimg->width * pimg->height;
for (idx=0; idx<10000; idx++) {
offset = rand() % surface;
pimg->R[offset] *= 0.8;
pimg->G[offset] *= 0.8;
pimg->B[offset] *= 0.8;
}
return 0;
}
2021-01-03 15:21:38 +01:00
/* -------------------------------------------------------------- */
/* nouveau du 32 decembre 2020, endless september */
int do_something(FloatImg *pimg, int notused)
{
int ypos, idx, pos, sline;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, notused);
#endif
ypos = rand() % pimg->width;
for (idx=0; idx < pimg->height; idx++) {
sline = idx * pimg->width;
pos = sline + ypos;
// fprintf(stderr, "%6d %6d\n", idx, sline);
pimg->R[pos] = pimg->G[pos];
pimg->B[pos] = pimg->G[pos];
2021-02-23 11:32:10 +01:00
pimg->G[pos] *= 1.717;
2021-01-03 15:21:38 +01:00
}
return 0;
}
2020-11-10 03:58:18 +01:00
/* -------------------------------------------------------------- */
2020-11-10 14:00:22 +01:00
int kill_a_random_line(FloatImg *pvictime, float fval, int bits)
2020-11-10 03:58:18 +01:00
{
int line, xpos, offset;
float ftmp;
2020-11-10 03:58:18 +01:00
2021-04-03 05:37:03 +02:00
#if DEBUG_LEVEL > 1
2020-11-10 14:00:22 +01:00
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pvictime, bits);
2020-11-10 03:58:18 +01:00
#endif
2021-05-05 10:12:01 +02:00
if (verbosity > 1) {
fprintf(stderr, "--> %s: hauteur image = %d fval = %f\n",
__func__, pvictime->height, fval);
}
2021-05-05 10:02:23 +02:00
2020-11-10 03:58:18 +01:00
line = rand() % pvictime->height;
2020-12-08 15:51:07 +01:00
if (verbosity > 2) {
2020-11-10 03:58:18 +01:00
fprintf(stderr, "%s: try to kill line %d\n", __func__, line);
}
offset = pvictime->width * line;
2020-11-10 09:28:53 +01:00
for (xpos=0; xpos<pvictime->width; xpos++) {
if (bits & 1) { ftmp = pvictime->R[offset+xpos] * fval;
pvictime->R[offset+xpos] = sqrt(ftmp); }
2020-11-10 14:00:22 +01:00
else pvictime->R[offset+xpos] = 0.0;
if (bits & 2) { ftmp = pvictime->G[offset+xpos] * fval;
pvictime->G[offset+xpos] = sqrt(ftmp); }
2020-11-10 14:00:22 +01:00
else pvictime->G[offset+xpos] = 0.0;
if (bits & 4) { ftmp = pvictime->B[offset+xpos] * fval;
pvictime->B[offset+xpos] = sqrt(ftmp); }
2020-11-10 14:00:22 +01:00
else pvictime->B[offset+xpos] = 0.0;
2020-11-10 03:58:18 +01:00
}
return 0;
}
/* -------------------------------------------------------------- */
int kill_a_few_lines(FloatImg *who, float fval, int number)
{
int idx, foo;
2020-12-08 15:51:07 +01:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %f %d )\n", __func__, who, fval, number);
#endif
2020-11-10 03:58:18 +01:00
/* Frag the pixels */
for (idx=0; idx<number; idx++) {
2020-12-08 15:51:07 +01:00
foo = kill_a_random_line(who, fval, rand() & 0x07);
2020-11-10 03:58:18 +01:00
if (foo) abort();
}
return foo;
}
/* -------------------------------------------------------------- */
2020-11-10 19:30:49 +01:00
int un_petit_flou_8x8(FloatImg *picture, int xpos, int ypos)
{
float sr, sg, sb;
int x, y, off;
/*
* please add boundary check ?
*/
sr = sg = sb = 0.0;
for (y=0; y<8; y++) {
off = xpos + (picture->width * (y+ypos));
for (x=0; x<8; x++) {
sr += picture->R[off];
sg += picture->G[off];
sb += picture->B[off];
off++;
}
}
sr /= 64.0; sg /= 64.0; sb /= 64.0;
for (y=0; y<8; y++) {
off = xpos + (picture->width * (y+ypos));
for (x=0; x<8; x++) {
picture->R[off] = sr;
picture->G[off] = sg;
picture->B[off] = sb;
off++;
}
}
return 0;
}
/* -------------------------------------------------------------- */
2021-01-16 11:29:40 +01:00
int plot_multidots(FloatImg *picture, int notused)
{
int pass, szimg, osrc, odst;
szimg = picture->width * picture->height;
for (pass=0; pass<szimg/32; pass++) {
osrc = rand() % szimg;
odst = rand() % szimg;
picture->R[odst] = (picture->R[osrc] + picture->R[odst]) / 2.0;
picture->G[odst] = (picture->G[osrc] + picture->G[odst]) / 2.0;
picture->B[odst] = (picture->B[osrc] + picture->B[odst]) / 2.0;
}
return 0;
}
2023-05-29 22:02:15 +02:00
/* -------------------------------------------------------------- */
2023-06-26 17:34:53 +02:00
/*
* FIXME XXX
*/
2020-12-20 11:12:43 +01:00
int random_blocks(FloatImg *picture, int percent)
{
int x, y;
2020-12-26 10:39:50 +01:00
if ( (picture->width%16) || (picture->height%16) )
2020-12-23 16:40:45 +01:00
{
2023-06-26 17:34:53 +02:00
fprintf(stderr, "WARNING %s: %dx%d bad dims\n", __func__,
2020-12-23 16:40:45 +01:00
picture->width, picture->height);
2023-06-26 17:34:53 +02:00
// return -1;
2020-12-23 16:40:45 +01:00
}
2020-12-20 11:12:43 +01:00
2023-06-26 17:34:53 +02:00
for (y=16; y<picture->height-16; y+=16) {
for (x=16; x<picture->width-16; x+=16) {
2020-12-20 11:12:43 +01:00
if (percent < (rand()%100) ) {
2020-12-23 16:40:45 +01:00
un_petit_flou_8x8(picture, x, y);
un_petit_flou_8x8(picture, x+8, y);
un_petit_flou_8x8(picture, x, y+8);
un_petit_flou_8x8(picture, x+8, y+8);
2020-12-20 11:12:43 +01:00
}
}
}
return 0;
}
/* -------------------------------------------------------------- */
2020-11-10 19:30:49 +01:00
int un_moyen_flou_8x8(FloatImg *picture, int xpos, int ypos)
{
int i, j, x, y;
/*
* please add boundary check ?
*/
2020-11-10 19:30:49 +01:00
for (i=y=0; i<8; i++, y+=8) {
for (j=x=0; j<8; j++, x+=8 ) {
un_petit_flou_8x8(picture, x+xpos, y+ypos);
}
}
return 0;
2020-11-10 19:30:49 +01:00
}
/* -------------------------------------------------------------- */
int poke_a_random_pixel(FloatImg *picz, float fval, int kaboo)
{
return -1;
}
/* -------------------------------------------------------------- */
2020-12-02 19:24:54 +01:00
/*
* used by vertical_singlitch()
*/
2020-12-10 19:19:35 +01:00
static int x_delta(float dy, float phy)
2020-12-02 19:24:54 +01:00
{
float param, fv;
param = dy + phy;
2021-01-05 22:55:34 +01:00
fv = 12.11*sin(param+0.22) + 8.5*sin(param*3.02) + 0.42*sin(param*5.1);
2020-12-02 19:24:54 +01:00
return (int)fv;
}
/* -------------------------------------------------------------- */
/*
* please explain arguments
*/
2020-11-15 11:47:37 +01:00
int vertical_singlitch(FloatImg *picz, int xpos, float fval,
float omega, float phi)
2020-11-14 22:26:11 +01:00
{
2020-11-15 11:47:37 +01:00
int y, x, w, h;
2020-12-02 19:24:54 +01:00
float dy;
float fv;
2020-11-14 22:26:11 +01:00
2020-11-15 11:47:37 +01:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d %f %f )\n", __func__, picz,
xpos, omega, phi);
#endif
h = picz->height; w = picz->width;
2021-01-05 22:55:34 +01:00
#define BB 2
2020-11-15 11:47:37 +01:00
for (y=BB; y<h-BB; y++) {
2020-11-15 21:31:02 +01:00
2020-12-02 19:24:54 +01:00
dy = (float)y * omega; /* normalize vertical position */
x = xpos + x_delta(dy, phi); /* add sinus deviation */
2020-11-15 11:47:37 +01:00
/* compute bounding box */
if ( (x>BB) && (x<w-BB) ) {
/* an make the glitch */
fimg_plot_rgb(picz, x, y, fval, fval, fval);
2020-12-02 19:24:54 +01:00
fv = fval / 3.0;
2020-11-15 11:47:37 +01:00
if (rand() & 8)
2020-12-02 19:24:54 +01:00
fimg_plot_rgb(picz, x-1, y, fv, fv, fv);
2020-11-15 11:47:37 +01:00
if (rand() & 8)
2020-12-02 19:24:54 +01:00
fimg_plot_rgb(picz, x+1, y, fv, fv, fv);
2020-11-15 11:47:37 +01:00
}
}
return 0;
2020-11-14 22:26:11 +01:00
}
/* -------------------------------------------------------------- */
2021-01-16 11:29:40 +01:00
static void shifter(float *fs, float *fd, int stp, int largeur)
2020-12-31 00:46:12 +01:00
{
2021-01-16 11:29:40 +01:00
int xpos;
2020-12-31 00:46:12 +01:00
2021-01-16 11:29:40 +01:00
/* move the pixels */
for (xpos=0; xpos<largeur; xpos++) {
fd[xpos] = fs[(xpos+stp)%largeur];
2020-12-31 00:46:12 +01:00
}
/* take your sixpack, film at 11 */
}
2021-01-16 11:29:40 +01:00
static void smooth_line(float *fs, float *fd, int sz)
{
int xpos;
for (xpos=1; xpos<(sz-1); xpos++) {
fd[xpos] = (fs[xpos-1]+fs[xpos]+fs[xpos+1]) / 3.0;
}
fd[0] = fd[sz-1] = 0.0;
}
2020-12-31 00:46:12 +01:00
int multilines_shift_0(FloatImg *picz, int step, int nombre)
{
float *buffline, *sptr;
int idx, ypos;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, picz, step, nombre);
#endif
buffline = alloca(picz->width * sizeof(float));
if (NULL==buffline) {
fprintf(stderr, "%s: memory panic\n", __func__);
abort();
}
for (idx=0; idx<nombre; idx++) {
ypos = rand() % picz->height;
sptr = picz->R + (ypos * picz->width);
shifter(sptr, buffline, step, picz->width);
2021-01-16 11:29:40 +01:00
smooth_line(buffline, sptr, picz->width);
// XXX memcpy (sptr, buffline, picz->width*sizeof(float));
2020-12-31 00:46:12 +01:00
sptr = picz->G + (ypos * picz->width);
shifter(sptr, buffline, step, picz->width);
2021-01-16 11:29:40 +01:00
smooth_line(buffline, sptr, picz->width);
// XXX memcpy (sptr, buffline, picz->width*sizeof(float));
2020-12-31 00:46:12 +01:00
sptr = picz->B + (ypos * picz->width);
shifter(sptr, buffline, step, picz->width);
2021-01-16 11:29:40 +01:00
smooth_line(buffline, sptr, picz->width);
// XXX memcpy (sptr, buffline, picz->width*sizeof(float));
2020-12-31 00:46:12 +01:00
}
return 0;
}
/* -------------------------------------------------------------- */
2020-11-10 03:58:18 +01:00