-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff-glitch2.c
227 lines (200 loc) · 4.7 KB
/
ff-glitch2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// ┳━┓┳━┓ ┏━┓┳ o┏┓┓┏━┓┳ ┳
// ┣━ ┣━ ━━┃ ┳┃ ┃ ┃ ┃ ┃━┫
// ┇ ┇ ┇━┛┇━┛┇ ┇ ┗━┛┇ ┻
// ff-glitch: the farbfeld glitcher.
// usage: <farbfeld source> | ff-glitch | <farbfeld sink>
// thanks to xero for the idea! you rock.
// made by vifino. ISC (C) vifino 2018
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <arpa/inet.h>
// TODO: replace ntohl
static uint32_t w, h;
static unsigned char buf[8] = {0};
static unsigned char img;
static struct timeval tv;
typedef struct {
unsigned short r, g, b, a;
} pixel;
// IO helpers.
static inline void bread(int bytes) {
if (!fread(buf, bytes, 1, stdin)) {
fprintf(stderr, "wanted more bytes, didn't get any?\n");
exit(1);
}
}
static inline void bwrite(unsigned char* buffer, size_t bytes) {
if (!fwrite(buffer, bytes, 1, stdout)) {
fprintf(stderr, "write failed.\n");
exit(1);
}
}
// random helper, random from 0-n, inclusive.
// tries to get evenly spread out random.
unsigned int randn(unsigned int n) {
if (!n) return 0;
if (n == RAND_MAX) return rand();
n++;
long end = RAND_MAX / n;
end *= n;
int r;
while ((r = rand()) >= end);
return r % n;
}
// Glitch a short.
#define SH16MAX (2 * UINT8_MAX)
#define GLITCHPX() randn(SH16MAX)
#define INVERT(v) htons(SH16MAX - ntohs(v))
// Probability.
// TODO: Figure out how to make the number of glitching related
// to the amount of pixels.
#define GLITCHES() (20 + randn(30))
#define GLITCH_INVERT 10
#define GLITCH_INVERT_PXM 1
#define GLITCH_SKIP 70
#define GLITCH_SKIP_PXM 1
#define GLITCH_CORRUPT 50
#define GLITCH_CORRUPT_PXM 4
#define GLITCH_SWAP 20
#define GLITCH_SWAP_PXM 4
#define GLITCHN (GLITCH_INVERT + GLITCH_SKIP + GLITCH_CORRUPT + GLITCH_SWAP)
#define SEL(i, name) if (n < (name)) return (i); n -= (name)
static inline unsigned int selglitch() {
unsigned int n = randn(GLITCHN);
SEL(0, GLITCH_INVERT);
SEL(1, GLITCH_SKIP);
SEL(2, GLITCH_CORRUPT);
return 3;
}
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
int main(int argc, char* argv[]) {
// TODO: parse argv[1] for range;
unsigned int glitches = GLITCHES();
bread(8);
if (strncmp(buf, "farbfeld", 8) != 0) {
fprintf(stderr, "stdin is not a farbfeld image?\n");
return 1;
}
bwrite(buf, 8);
// seed like a dumbass.
if (gettimeofday(&tv, NULL)) {
fprintf(stderr, "failed to get the time? that's messed up.\n");
return 2;
}
srand(tv.tv_usec);
// parse width and height
bread(8);
uint32_t w = ntohl(*(uint32_t*)buf);
uint32_t h = ntohl(*((uint32_t*)(buf + 4)));
if (!w && !h) {
fprintf(stderr, "image size has zero dimension? %i*%i\n", w, h);
return 1;
}
bwrite(buf, 8);
unsigned int pxn;
unsigned int end = w*h;
unsigned int skipped = 0;
int glitchpx = 0;
unsigned int gt = 0;
pixel* px = (pixel*) buf;
unsigned int glm; // selector
// corrupter
unsigned short gv = GLITCHPX();
unsigned char gp = randn(2);
// swapper
unsigned short gs = randn(2);
for(pxn = 0; pxn < end; pxn++) {
if (randn(end / glitches) == 0) {
// glitch it. glitch it hard.
gt = selglitch();
switch (gt) {
case 0: // invert
glm = GLITCH_INVERT_PXM;
break;
case 1: // skip
glm = GLITCH_SKIP_PXM;
break;
case 2: // corrupt
glm = GLITCH_CORRUPT_PXM;
break;
case 3: // swap
glm = GLITCH_SWAP_PXM;
break;
}
glitchpx = randn((end * glm) / glitches) + 1;
}
if (glitchpx) {
switch (gt) {
case 0: // invert
bread(8);
px->r = INVERT(px->r);
px->g = INVERT(px->g);
px->b = INVERT(px->b);
bwrite(buf, 8);
glitchpx--;
break;
case 1: // skip reading and trash
skipped++;
px->r = GLITCHPX();
px->g = GLITCHPX();
px->b = GLITCHPX();
bwrite(buf, 8);
glitchpx--;
break;
case 2: // glitch
glitchpx--;
if (!glitchpx) { // randomize next
gp = randn(2);
gv = GLITCHPX();
}
bread(8);
switch (gp) {
case 0:
px->r = gv;
break;
case 1:
px->g = gv;
break;
case 2:
px->b = gv;
break;
}
bwrite(buf, 8);
break;
case 3: // swap
glitchpx--;
if (!glitchpx) { // randomize next
gs = randn(2);
}
bread(8);
pixel tpx = *px;
switch (gs) {
case 0: // swap r with g;
px->r = tpx.g;
px->g = tpx.r;
case 1: // swap r with b;
px->r = tpx.b;
px->b = tpx.r;
case 2: // swap b with g;
px->g = tpx.b;
px->b = tpx.g;
}
bwrite(buf, 8);
break;
}
continue;
}
// copy and compensate.
bread(8);
bwrite(buf, 8);
}
// read skipped things
for (; skipped > 0; skipped--) {
bread(8);
}
return 0;
}