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
use rand::distributions::{Distribution, Uniform};
use rand::{RngCore, SeedableRng};
use resize::Pixel::{Gray8, RGB8, RGBA8};
use resize::Type::Point;
use rgb::FromSlice;
use crate::err::MoshError;
use crate::fx::{Mosh, MoshChunk, MoshLine};
pub mod cli;
pub mod err;
pub mod fx;
pub mod ops;
pub struct MoshOptions {
pub min_rate: u16,
pub max_rate: u16,
pub pixelation: u8,
pub line_shift: f64,
pub reverse: f64,
pub flip: f64,
pub channel_swap: f64,
pub channel_shift: f64,
pub seed: u64,
}
impl Default for MoshOptions {
fn default() -> Self {
Self {
min_rate: 1,
max_rate: 7,
pixelation: 10,
line_shift: 0.3,
reverse: 0.3,
flip: 0.3,
channel_swap: 0.3,
channel_shift: 0.3,
seed: if cfg!(test) {
901_042_006
} else {
rand::thread_rng().next_u64()
},
}
}
}
pub fn mosh(
image_info: &png::OutputInfo,
pixel_buffer: &mut [u8],
options: &MoshOptions,
) -> Result<(), MoshError> {
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 mut dest = vec![0u8; w2 * h2 * image_info.color_type.samples()];
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(options.seed);
let chunk_count_dist = Uniform::from(options.min_rate..=options.max_rate);
let mosh_rate = chunk_count_dist.sample(&mut rng);
for _ in 0..mosh_rate {
chunkmosh(image_info, pixel_buffer, &mut rng, options);
}
if options.pixelation > 1 {
match image_info.color_type {
png::ColorType::GrayscaleAlpha | png::ColorType::Indexed => {}
png::ColorType::Grayscale => {
resize::new(w1, h1, w2, h2, Gray8, Point)?
.resize(pixel_buffer.as_gray(), dest.as_gray_mut())?;
}
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::GrayscaleAlpha | png::ColorType::Indexed => {
return Err(MoshError::UnsupportedColorType);
}
png::ColorType::Grayscale => {
resize::new(w2, h2, w1, h1, Gray8, Point)?
.resize(dest.as_gray(), pixel_buffer.as_gray_mut())?;
}
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: &MoshOptions,
) {
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);
let flip = rng.gen_bool(options.flip);
let line_shift = if rng.gen_bool(options.line_shift) {
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) {
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) {
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);
};
}
#[cfg(test)]
mod util;