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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use rand::distributions::{Distribution, Uniform};
use resize::Pixel::{Gray8, RGB8, RGBA8};
use resize::Type::Point;
use rgb::FromSlice;
pub struct Options {
pub min_rate: u16,
pub max_rate: u16,
pub line_shift_rng: f64,
pub reverse_rng: f64,
pub flip_rng: f64,
pub channel_swap_rng: f64,
pub channel_shift_rng: f64,
pub pixelation: u8,
}
trait Mosh {
fn run(&self, chunk: &mut [u8]);
}
enum MoshChunk {
ChannelSwap(usize, usize, usize),
Flip,
}
enum MoshLine {
ChannelShift(usize, usize, usize),
Shift(usize),
Reverse,
}
pub fn mosh(
image_info: &png::OutputInfo,
pixel_buffer: &mut [u8],
rng: &mut impl rand::Rng,
options: &Options,
) -> Result<(), resize::Error> {
let (w1, h1) = (image_info.width as usize, image_info.height as usize);
let (w2, h2) = (
w1 / options.pixelation as usize,
h1 / options.pixelation as usize,
);
let chunk_count_dist = Uniform::from(options.min_rate..=options.max_rate);
let mosh_rate = chunk_count_dist.sample(rng);
let mut dest = vec![0u8; w2 * h2 * image_info.color_type.samples()];
for _ in 0..mosh_rate {
chunkmosh(image_info, pixel_buffer, rng, options);
}
match image_info.color_type {
png::ColorType::Grayscale => {
resize::new(w1, h1, w2, h2, Gray8, Point)?
.resize(pixel_buffer.as_gray(), dest.as_gray_mut())?;
}
png::ColorType::GrayscaleAlpha | png::ColorType::Indexed => {
eprintln!("\x1b[1;31merror:\x1b[0m Unsupported color type");
std::process::exit(1)
}
png::ColorType::Rgb => {
resize::new(w1, h1, w2, h2, RGB8, Point)?
.resize(pixel_buffer.as_rgb(), dest.as_rgb_mut())?;
}
png::ColorType::Rgba => {
resize::new(w1, h1, w2, h2, RGBA8, Point)?
.resize(pixel_buffer.as_rgba(), dest.as_rgba_mut())?;
}
};
match image_info.color_type {
png::ColorType::Grayscale => {
resize::new(w2, h2, w1, h1, Gray8, Point)?
.resize(dest.as_gray(), pixel_buffer.as_gray_mut())?;
}
png::ColorType::GrayscaleAlpha | png::ColorType::Indexed => {
eprintln!("\x1b[1;31merror:\x1b[0m Unsupported color type");
std::process::exit(1)
}
png::ColorType::Rgb => {
resize::new(w2, h2, w1, h1, RGB8, Point)?
.resize(dest.as_rgb(), pixel_buffer.as_rgb_mut())?;
}
png::ColorType::Rgba => {
resize::new(w2, h2, w1, h1, RGBA8, Point)?
.resize(dest.as_rgba(), pixel_buffer.as_rgba_mut())?;
}
};
Ok(())
}
fn chunkmosh(
image_info: &png::OutputInfo,
pixel_buffer: &mut [u8],
rng: &mut impl rand::Rng,
options: &Options,
) {
let line_count = pixel_buffer.len() / image_info.line_size;
let channel_count = match image_info.color_type {
png::ColorType::Grayscale | png::ColorType::Indexed => 1,
png::ColorType::GrayscaleAlpha => 2,
png::ColorType::Rgb => 3,
png::ColorType::Rgba => 4,
};
let line_shift_dist = Uniform::from(0..image_info.line_size);
let line_number_dist = Uniform::from(0..line_count);
let channel_count_dist = Uniform::from(0..channel_count);
let first_line = line_number_dist.sample(rng);
let chunk_size = line_number_dist.sample(rng) / 2;
let last_line = if (first_line + chunk_size) > line_count {
line_count
} else {
first_line + chunk_size
};
let reverse = rng.gen_bool(options.reverse_rng);
let flip = rng.gen_bool(options.flip_rng);
let line_shift = if rng.gen_bool(options.line_shift_rng) {
let line_shift_amount = line_shift_dist.sample(rng);
Some(MoshLine::Shift(line_shift_amount))
} else {
None
};
let channel_shift = if rng.gen_bool(options.channel_shift_rng) {
let amount = line_shift_dist.sample(rng) / channel_count;
let channel = channel_count_dist.sample(rng);
Some(MoshLine::ChannelShift(amount, channel, channel_count))
} else {
None
};
let channel_swap = if rng.gen_bool(options.channel_swap_rng) {
let channel_1 = channel_count_dist.sample(rng);
let channel_2 = channel_count_dist.sample(rng);
Some(MoshChunk::ChannelSwap(channel_1, channel_2, channel_count))
} else {
None
};
for line_number in first_line..last_line {
let line_start = line_number * image_info.line_size;
let line_end = line_start + image_info.line_size;
let line = &mut pixel_buffer[line_start..line_end];
if let Some(channel_shift) = &channel_shift {
channel_shift.run(line);
}
if let Some(line_shift) = &line_shift {
line_shift.run(line);
}
if reverse {
MoshLine::Reverse.run(line);
}
}
let chunk_start = first_line * image_info.line_size;
let chunk_end = last_line * image_info.line_size;
let chunk = &mut pixel_buffer[chunk_start..chunk_end];
if let Some(channel_swap) = channel_swap {
channel_swap.run(chunk);
};
if flip {
MoshChunk::Flip.run(chunk);
};
}
impl Mosh for MoshChunk {
fn run(&self, chunk: &mut [u8]) {
match self {
Self::ChannelSwap(channel_1, channel_2, channel_count) => {
let chunk_length = chunk.len();
let channel_value_count = chunk_length / channel_count;
for i in 0..channel_value_count {
let channel_1_index = (i * channel_count) + channel_1;
let channel_2_index = (i * channel_count) + channel_2;
let channel_1_value = chunk[channel_1_index];
let channel_2_value = chunk[channel_2_index];
chunk[channel_1_index] = channel_2_value;
chunk[channel_2_index] = channel_1_value;
}
}
Self::Flip => {
chunk.reverse();
}
}
}
}
impl Mosh for MoshLine {
fn run(&self, line: &mut [u8]) {
match self {
Self::ChannelShift(amount, channel, channel_count) => {
let line_length = line.len();
let channel_value_count = line_length / channel_count;
for i in 0..channel_value_count {
line[(i * channel_count + channel) % line_length] =
line[(i * channel_count + channel + (channel + 1) * amount) % line_length];
}
}
Self::Shift(amount) => {
line.rotate_left(*amount);
}
Self::Reverse => {
line.reverse();
}
}
}
}