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
extern crate image;
extern crate terminal_graphics;
use terminal_graphics::Display;
use terminal_graphics::Colour;
use std::path::Path;
use std::fs::File;
use std::{thread, time};
use image::{GenericImage, Pixel, RgbImage, gif, Frames, ImageDecoder, Frame};
use image::{ConvertBuffer, ImageBuffer};
pub fn pixelate_image(img: &mut RgbImage, width: u32, height: u32) -> RgbImage {
let mut imgbuf = image::ImageBuffer::new(width, height);
closest_match(&mut imgbuf, img);
imgbuf
}
pub fn get_gif(filename: &str) -> Frames {
let mut f = File::open(filename).expect("File not found");
let mut decoder = gif::Decoder::new(f);
decoder.into_frames().expect("error decoding gif")
}
pub fn get_image(filename: &str) -> RgbImage {
let img = image::open(&Path::new(filename)).unwrap();
img.to_rgb()
}
pub fn closest_match(imgbuf: &mut RgbImage, img: &mut RgbImage) {
let (width, height) = img.dimensions();
let (out_width, out_height) = imgbuf.dimensions();
let scale_x = (width as f32)/(out_width as f32);
let scale_y = (height as f32)/(out_height as f32);
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let x_f32 = x as f32;
let y_f32 = y as f32;
let closest_pixel_x = (x_f32 * scale_x).floor() as u32;
let closest_pixel_y = (y_f32 * scale_y).floor() as u32;
let closest_pixel = img.get_pixel(closest_pixel_x, closest_pixel_y);
*pixel = closest_pixel.clone();
}
}
pub fn merge_pixels(imgbuf: &mut RgbImage, img: &mut RgbImage) {
let (width, height) = img.dimensions();
let (out_width, out_height) = imgbuf.dimensions();
let scale_x = width/out_width;
let scale_y = height/(out_height);
let num_pixels = (scale_y*scale_x) as f32;
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let mut big_pixel_rgb: [f32; 3] = [0.0, 0.0, 0.0];
let big_pixel = img.sub_image(x*scale_x, y*scale_y, scale_x, scale_y);
for (_, _, pixel) in big_pixel.pixels() {
let rgb_values = pixel.to_rgb().data;
big_pixel_rgb[0] += rgb_values[0] as f32;
big_pixel_rgb[1] += rgb_values[1] as f32;
big_pixel_rgb[2] += rgb_values[2] as f32;
}
big_pixel_rgb[0] *= 1.0/num_pixels;
big_pixel_rgb[1] *= 1.0/num_pixels;
big_pixel_rgb[2] *= 1.0/num_pixels;
*pixel = image::Rgb([big_pixel_rgb[0] as u8, big_pixel_rgb[1] as u8, big_pixel_rgb[2] as u8]);
}
}
fn render_image(mut image: RgbImage, width: u32, height: u32) {
let (img_width, img_height) = image.dimensions();
let ratio = (img_width as f32)/(img_height as f32);
let mut display_height = height;
let mut display_width = width;
if ratio > 1.00 {
display_height = ((display_width as f32)/(ratio * 2.0)).floor() as u32;
} else {
display_width = 2 * ((display_height as f32) * ratio).floor() as u32;
}
let mut scale = 1.00;
if (display_width as f32)*scale > (img_width as f32) {
scale = (img_width as f32)/(width as f32);
}
if (display_height as f32)*scale > (img_height as f32) {
scale = (img_height as f32)/(height as f32);
}
display_width = ((display_width as f32)*scale).floor() as u32;
display_height = ((display_height as f32)*scale).floor() as u32;
scale = 1.0;
if (display_width as f32) * scale > (width as f32) {
scale = (width as f32) / (display_width as f32);
}
if (display_height as f32) * scale > (height as f32) {
scale = (height as f32) / (display_height as f32);
}
display_width = ((display_width as f32)*scale).floor() as u32;
display_height = ((display_height as f32)*scale).floor() as u32;
let mut screen = Display::new(display_width, display_height);
screen.clear();
let img_out = pixelate_image(&mut image, display_width, display_height * 2);
for (x, y, pixel) in img_out.enumerate_pixels() {
let rgb = pixel.to_rgb().data;
let colour = Colour::from_rgb(rgb[0], rgb[1], rgb[2]);
match y % 2 {
0 => screen.set_pixel(x as isize, (y/2) as isize, '▄', colour, colour),
1 => screen.get_mut_pixel(x as isize, ((y - 1)/2) as isize).set_colour(colour),
_ => println!("That shouldn't happen"),
}
}
screen.print();
}
pub fn display_image(image_filepath: &str, width: u32, height: u32) {
let mut img = get_image(image_filepath);
render_image(img, width, height);
println!("");
}
pub fn display_gif(gif_filepath: &str, width: u32, height: u32) {
let mut frames = get_gif(gif_filepath);
let mut modified_frames = Vec::new();
for (i, frame) in frames.enumerate() {
let delay = frame.delay().to_integer() as u64;
let mut image = frame.into_buffer();
modified_frames.push((image.clone(), delay));
render_image(image.convert(), width, height);
thread::sleep(time::Duration::from_millis(delay));
}
loop {
for (frame, delay) in modified_frames.clone() {
render_image(frame.clone().convert(), width, height);
thread::sleep(time::Duration::from_millis(delay));
}
}
}