-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
405 lines (368 loc) · 14.1 KB
/
main.rs
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
extern crate clap;
extern crate image;
use clap::{Arg,App,SubCommand};
use std::fs::File;
use image::{FilterType, GenericImage, Pixel,ImageBuffer,Rgb};
use image::DynamicImage;
use std::path::Path;
use std::collections::HashMap;
fn main() {
let matches = App::new("IPCLI ")
.version("0.1")
.author("Mikolaj Wawrzyniak <mikolaj.wawrzyniak at fh-dortmund.de>")
.about("Basic CLI for image processing")
.arg(Arg::with_name("operation")
.short("o")
.long("operation")
.help("Specifies operation to be done on the image")
.takes_value(true)
.required(true))
.arg(Arg::with_name("value")
.short("v")
.long("value")
.help("Value for the transformation. To see what values are needed, check the documentation.")
.takes_value(true)
.required(false))
.arg(Arg::with_name("image")
.short("i")
.long("image")
.value_name("FILE")
.help("Opens specified image file and uses it for transformations.")
.takes_value(true)
.required(true))
.get_matches();
let imagePath = matches.value_of("image").unwrap_or("empty");
//println!("Transforming the image: {}", imagePath);
let operation = matches.value_of("operation").unwrap_or("empty");
//println!("Using operation: {}", operation);
let value = matches.value_of("value").unwrap_or("empty");
//println!("Value: {}", value);
match operation.as_ref(){
"average" => {averageColor(imagePath)}
"copy" => {copy(imagePath)}
"thumbnail" => {
let size: u32 = value.parse().unwrap();
createThumnbail(imagePath, size)}
"blur" => {
let v : f32 = value.parse().unwrap();
gaussianBlur(imagePath,v)}
"brighten" => {
let v: i32 = value.parse().unwrap();
brighten(imagePath,v)}
"huerotate" => {
let v: i32 = value.parse().unwrap();
huerotate(imagePath,v)}
"contrast" => {
let v: f32 = value.parse().unwrap();
contrast(imagePath,v);}
"grayscale" => {
grayscale(imagePath);
}
"invert" => {
invert(imagePath);
}
"histogramGrayscale" => {histogramGrayscale(imagePath)}
"histogram" => {histogram(imagePath)}
"binaryTreshold" => {
let v: u8 = value.parse().unwrap();
binary_treshold(imagePath,v);
}
_ => {println!("Not implemented yet!")}
}
}
fn averageColor(i: &str){
let img = image::open(i).expect("Opening image failed");
let mut r: u32 = 0;
let mut g: u32 = 0;
let mut b: u32 = 0;
let (width,height) = img.dimensions();
for x in 0..(width){
for y in 0..(height){
let px = img.get_pixel(x,y);
let rgb = px.to_rgb();
r = (r as u32 + rgb.data[0] as u32)/2;
g = (g as u32 + rgb.data[1] as u32)/2;
b = (b as u32 + rgb.data[2] as u32)/2;
println!("R G B {} {} {}", r,g,b);
}
}
println!("Average color is: RGB {} {} {}",r,g,b);
}
fn createThumnbail(i: &str, size: u32){
let operation = "Thumbnail";
let img = image::open(i).expect("Opening image failed");
let thumbnail = img.resize(size,size, FilterType::Lanczos3);
saveFile(&thumbnail, &i, &operation);
}
fn copy(i: &str){
let operation = "Copy";
let img = image::open(i).expect("Opening image failed");
saveFile(&img, &i, &operation);
}
fn gaussianBlur(i: &str, v: f32){
let operation = "GuassianBlur";
let img = image::open(i).expect("Opening image failed");
let blurred = img.blur(v);
saveFile(&blurred, &i, &operation);
}
fn brighten(i: &str,v: i32){
let operation = "Brighten";
let img = image::open(i).expect("Opening image failed");
let brightened = img.brighten(v);
saveFile(&brightened, &i, &operation);
}
fn huerotate(i: &str, v: i32){
let operation = "Huerotate";
let img = image::open(i).expect("Opening image failed");
let huerotated = img.huerotate(v);
saveFile(&huerotated, &i, &operation);
}
fn contrast(i: &str, v: f32){
let operation = "AdjustContrast";
let img = image::open(i).expect("Opening image failed");
let contrast = img.adjust_contrast(v);
saveFile(&contrast, &i, &operation);
}
fn grayscale(i: &str){
let operation = "Grayscale";
let img = image::open(i).expect("Opening image failed");
let grayscale = img.grayscale();
saveFile(&grayscale, &i, &operation);
}
fn invert(i: &str){
let operation = "Invert";
let mut img = image::open(i).expect("Opening image failed");
img.invert();
saveFile(&img, &i, &operation);
}
fn binary_treshold(i: &str, low: u8){
let operation = "BinaryTreshold";
let img = image::open(i).expect("Opening image failed");
let mut grayscale = img.grayscale();
let (width,height) = img.dimensions();
// create image to draw the image after binary treshold
let mut image = image::ImageBuffer::<Rgb<u8>, Vec<u8>>::new(width, height);
for w in 0..(width){
for h in 0..(height){
let pixel = grayscale.get_pixel(w,h);
let rgb = pixel.to_rgb();
let intensity = rgb.data[0];
if low < intensity {
image.get_pixel_mut(w,h).data = [255,255,255];
}else{
image.get_pixel_mut(w,h,).data = [0,0,0];
}
}
}
let mut outputPath: String = i.chars().take(i.len()-4).collect();
let ext: String = i.chars().skip(i.len()-3).take(3).collect();
outputPath.push_str(operation);
outputPath.push_str(".");
outputPath.push_str(&ext);
println!("Output path: {}", outputPath);
image.save(outputPath).unwrap();
}
/// Generate histogram for grayscale images
fn histogramGrayscale(i: &str){
let operation = "HistogramGrayscale";
// size of the output image containing the histogram
let WIDTH = 255;
let HEIGHT = 200;
// open image and convert it to grayscale
let img = image::open(i).expect("Opening image failed");
let grayscale = img.grayscale();
// create a hashmap for storing the number of occurences of each intensity
let mut occurences = HashMap::new();
// and fill it with a placeholder value
for fill in 0..255{
&occurences.insert(fill,0);
}
// iterate over each pixel in the image and count the occurences of each intensity
let (width,height) = grayscale.dimensions();
for w in 0..(width){
for h in 0..(height){
let pixel = grayscale.get_pixel(w,h);
let rgb = pixel.to_rgb();
let intensity = rgb.data[0];
match occurences.get(&intensity){
Some(&oc) => {
let mut current = oc;
current = current + 1;
&occurences.insert(intensity,current);
}
_ => {&occurences.insert(w as u8,0);}
}
}
}
// find highest value of occurences, so that we can use it as 100% in the histogram
let mut maxValue = 0;
for (occurences, &value) in occurences.iter() {
if(value > maxValue){
maxValue = value;
}
}
// create image to draw the histogram on
let mut image = image::ImageBuffer::<Rgb<u8>, Vec<u8>>::new(WIDTH, HEIGHT);
// dirty hack: fill the image with white pixels
for w in 0..WIDTH{
for h in 0..HEIGHT{
image.get_pixel_mut(w,h).data = [255,255,255];
}
}
// dirty hack: intensity index
let mut cc: u32 = 0;
// Potential bug: 254 and 255 cause panic!
for i in 0..253{
match occurences.get(&i) {
Some(&value) => {
let mut height = ((value as f32 / maxValue as f32) * 300.0) as u8;
let mut pixel = image.get_pixel_mut(cc, (HEIGHT-1) - height as u32);
pixel.data = [0,0,0];
},
_ => println!("Value out of bounds #BarryBonds"),
}
cc = cc + 1;
}
let mut outputPath: String = i.chars().take(i.len()-4).collect();
let ext: String = i.chars().skip(i.len()-3).take(3).collect();
outputPath.push_str(operation);
outputPath.push_str(".");
outputPath.push_str(&ext);
println!("Output path: {}", outputPath);
image.save(outputPath).unwrap();
}
/// Generate histogram for grayscale images
fn histogram(i: &str){
let operation = "Histogram";
// size of the output image containing the histogram
let WIDTH = 255;
let HEIGHT = 200;
// open image and convert it to grayscale
let img = image::open(i).expect("Opening image failed");
// create a hashmap for storing the number of occurences of each intensity
let mut occurencesR = HashMap::new();
let mut occurencesG = HashMap::new();
let mut occurencesB = HashMap::new();
// and fill it with a placeholder value
for fill in 0..255{
&occurencesR.insert(fill,0);
&occurencesG.insert(fill,0);
&occurencesB.insert(fill,0);
}
// iterate over each pixel in the image and count the occurences of each intensity
let (width,height) = img.dimensions();
for w in 0..(width){
for h in 0..(height){
let pixel = img.get_pixel(w,h);
let rgb = pixel.to_rgb();
let intensityR = rgb.data[0];
let intensityG = rgb.data[1];
let intensityB = rgb.data[2];
match occurencesR.get(&intensityR){
Some(&oc) => {
let mut current = oc;
current = current + 1;
&occurencesR.insert(intensityR,current);
}
_ => {&occurencesR.insert(w as u8,0);}
}
match occurencesG.get(&intensityG){
Some(&oc) => {
let mut current = oc;
current = current + 1;
&occurencesG.insert(intensityG,current);
}
_ => {&occurencesG.insert(w as u8,0);}
}
match occurencesB.get(&intensityB){
Some(&oc) => {
let mut current = oc;
current = current + 1;
&occurencesB.insert(intensityB,current);
}
_ => {&occurencesB.insert(w as u8,0);}
}
}
}
// find highest value of occurences, so that we can use it as 100% in the histogram
let mut maxValueR = 0;
let mut maxValueG = 0;
let mut maxValueB = 0;
for (occurencesR, &value) in occurencesR.iter() {
if(value > maxValueR){
maxValueR = value;
}
}
for (occurencesG, &value) in occurencesG.iter() {
if(value > maxValueG){
maxValueG = value;
}
}
for (occurencesB, &value) in occurencesB.iter() {
if(value > maxValueB){
maxValueB = value;
}
}
// create image to draw the histogram on
let mut image = image::ImageBuffer::<Rgb<u8>, Vec<u8>>::new(WIDTH, HEIGHT);
// dirty hack: fill the image with white pixels
for w in 0..WIDTH{
for h in 0..HEIGHT{
image.get_pixel_mut(w,h).data = [255,255,255];
}
}
// dirty hack: intensity index
let mut cc: u32 = 0;
// Potential bug: 254 and 255 cause panic!
for i in 0..253{
match occurencesR.get(&i) {
Some(&value) => {
let mut height = ((value as f32 / maxValueR as f32) * 200.0) as u8;
let mut pixel = image.get_pixel_mut(cc, (HEIGHT-1) - height as u32);
pixel.data = [255,0,0];
},
_ => println!("Value out of bounds #BarryBonds"),
}
match occurencesG.get(&i) {
Some(&value) => {
let mut height = ((value as f32 / maxValueG as f32) * 200.0) as u8;
let mut pixel = image.get_pixel_mut(cc, (HEIGHT-1) - height as u32);
pixel.data = [0,255,0];
},
_ => println!("Value out of bounds #BarryBonds"),
}
match occurencesB.get(&i) {
Some(&value) => {
let mut height = ((value as f32 / maxValueB as f32) * 200.0) as u8;
let mut pixel = image.get_pixel_mut(cc, (HEIGHT-1) - height as u32);
pixel.data = [0,0,255];
},
_ => println!("Value out of bounds #BarryBonds"),
}
cc = cc + 1;
}
let mut outputPath: String = i.chars().take(i.len()-4).collect();
let ext: String = i.chars().skip(i.len()-3).take(3).collect();
outputPath.push_str(operation);
outputPath.push_str(".");
outputPath.push_str(&ext);
println!("Output path: {}", outputPath);
image.save(outputPath).unwrap();
}
fn saveFile(img: &DynamicImage, i: &str, operation: &str){
let mut outputPath: String = i.chars().take(i.len()-4).collect();
let ext: String = i.chars().skip(i.len()-3).take(3).collect();
outputPath.push_str(operation);
outputPath.push_str(".");
outputPath.push_str(&ext);
println!("Output path: {}", outputPath);
let mut out = File::create(outputPath).unwrap();
match ext.as_ref() {
"jpg" | "JPG" | "jpeg" | "JPEG" => {img.save(&mut out, image::JPEG).expect("Saving image failed");}
"png" | "PNG" => {img.save(&mut out, image::PNG).expect("Saving image failed");}
"gif" | "GIF" => {img.save(&mut out, image::GIF).expect("Saving image failed");}
"bmp" | "BMP" => {img.save(&mut out, image::BMP).expect("Saving image failed");}
"ico" | "ICO" => {img.save(&mut out, image::ICO).expect("Saving image failed");}
_ => {println!("Unsupported file format")}
}
}