image_compressor 1.5.2

A image compressing module using mozjpeg, and image crates.
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! # Image compressor
//!
//! `image_compressor` is a library that compresses images with multiple threads.
//! See [image](https://crates.io/crates/image) crate for check the extension that supported.
//!
//! If you want to compress a single image, see [`Compressor`](Compressor) struct.
//!
//! Or if you want to compress multiple images in a certain directory, see [`FolderCompressor`] struct.
//! It compresses images using multiple threads.
//!
//! To use these structs and its functions, you need to give them a function pointer or closure
//! that calculate size and quality of new compressed images.
//! That calculator function(or closure) need to calculate and returns a [`Factor`]
//! base on image size and file size of the source image.
//! To see more information about it, see [`Factor`].
//!
//! # Examples
//!
//! ### `FolderCompressor` and its `compress` function example.
//!
//! The function will compress all images, using multithreading, in a given source folder
//! and will wait until everything is done.
//! If user set a [`Sender`] for [`FolderCompressor`], the method sends messages whether compressing is complete.
//! ```
//! use std::path::PathBuf;
//! use std::sync::mpsc;
//! use image_compressor::FolderCompressor;
//! use image_compressor::Factor;
//!
//! let source = PathBuf::from("source_dir");   // source directory path
//! let dest = PathBuf::from("dest_dir");       // destination directory path
//! let thread_count = 4;                       // number of threads
//! let (tx, tr) = mpsc::channel();             // Sender and Receiver. for more info, check mpsc and message passing.
//!
//! let mut comp = FolderCompressor::new(source, dest);
//! comp.set_factor(Factor::new(80., 0.8));
//! comp.set_thread_count(4);
//! comp.set_sender(tx);
//!
//! match comp.compress(){
//!     Ok(_) => {},
//!     Err(e) => println!("Cannot compress the folder!: {}", e),
//! }
//! ```
//!
//! ### `Compressor` and `compress_to_jpg` example.
//!
//! Compressing just a one image.
//! ```
//! use std::path::PathBuf;
//! use image_compressor::compressor::Compressor;
//! use image_compressor::Factor;
//!
//! let source_dir = PathBuf::from("source").join("file1.jpg");
//! let dest_dir = PathBuf::from("dest");
//!
//! let mut comp = Compressor::new(source_dir, dest_dir);
//! comp.set_factor(Factor::new(80., 0.8));
//! comp.compress_to_jpg();
//! ```

use compressor::Compressor;
use crawler::get_file_list;
use crossbeam_queue::SegQueue;
use dir::delete_recursive;
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use std::sync::{Arc};
use std::thread;

pub mod compressor;
pub mod crawler;
pub mod dir;

pub use compressor::Factor;

fn try_send_message<T: ToString>(sender: &Option<Sender<T>>, message: T) {
    match sender {
        Some(s) => send_message(s, message),
        None => (),
    }
}

fn send_message<T: ToString>(sender: &Sender<T>, message: T) {
    match sender.send(message) {
        Ok(_) => (),
        Err(e) => println!("Message passing error: {}", e),
    }
}

/// Compressor struct for a directory.
pub struct FolderCompressor {
    factor: Factor,
    source_path: PathBuf,
    dest_path: PathBuf,
    thread_count: u32,
    delete_source: bool,
    sender: Option<Sender<String>>,
}

impl FolderCompressor {
    /// Create a new `FolderCompressor` instance.
    /// Just needs source directory path and destination directory path.
    /// If you do not set the quality calculation function,
    /// it will use the default calculation function which sets the quality only by the file size.
    /// Likewise, if you do not set the number of threads, only one thread is used by default.\
    /// # Examples
    /// ```
    /// use image_compressor::FolderCompressor;
    /// use std::path::Path;
    ///
    /// let source = Path::new("source");
    /// let dest = Path::new("dest");
    ///
    /// let comp = FolderCompressor::new(source, dest);
    /// ```
    pub fn new<O: AsRef<Path>, D: AsRef<Path>>(source_path: O, dest_path: D) -> Self {
        FolderCompressor {
            factor: Factor::default(),
            source_path: source_path.as_ref().to_path_buf(),
            dest_path: dest_path.as_ref().to_path_buf(),
            thread_count: 1,
            delete_source: false,
            sender: None,
        }
    }

    /// Set Factor using to compress images.
    pub fn set_factor(&mut self, factor: Factor) {
        self.factor = factor;
    }

    /// Set whether to delete source files.
    pub fn set_delete_source(&mut self, to_delete: bool) {
        self.delete_source = to_delete;
    }

    /// Set Sender for message passing.
    /// If you set a sender, the method sends messages whether compressing is complete.
    pub fn set_sender(&mut self, sender: Sender<String>) {
        self.sender = Some(sender);
    }

    /// Setter for the number of threads used to compress images.
    /// # Examples
    /// ```
    /// use image_compressor::FolderCompressor;
    /// use image_compressor::Factor;
    /// use std::path::Path;
    ///
    /// let source = Path::new("source");
    /// let dest = Path::new("dest");
    ///
    /// let mut comp = FolderCompressor::new(source, dest);
    /// comp.set_thread_count(4);
    /// ```
    pub fn set_thread_count(&mut self, thread_count: u32) {
        self.thread_count = thread_count;
    }

    /// Folder compress function.
    ///
    /// The function will compress all images, using multithreading, in a given source folder and will wait until everything is done.
    /// If user set a [`Sender`] for [`FolderCompressor`] before, the method sends messages whether compressing is complete.
    ///
    /// # Warning
    /// Since this function consume its `self`, the `FolderCompressor` instance (which is self) is no longer available after calling this function.
    /// ```
    /// use std::path::PathBuf;
    /// use std::sync::mpsc;
    /// use image_compressor::FolderCompressor;
    ///
    /// let source = PathBuf::from("source_dir");
    /// let dest = PathBuf::from("dest_dir");
    /// let (tx, tr) = mpsc::channel();
    ///
    /// let mut comp = FolderCompressor::new(source, dest);
    /// comp.set_sender(tx);
    /// comp.set_thread_count(4);
    ///
    /// match comp.compress(){
    ///     Ok(_) => {},
    ///     Err(e) => println!("Cannot compress the folder: {}", e),
    /// }
    /// ```
    pub fn compress(self) -> Result<(), Box<dyn Error>> {
        let to_comp_file_list = get_file_list(&self.source_path)?;
        try_send_message(
            &self.sender,
            format!("Total file count: {}", to_comp_file_list.len()),
        );

        let queue = Arc::new(SegQueue::new());
        for i in to_comp_file_list {
            queue.push(i);
        }
        let mut handles = Vec::new();
        let arc_root = Arc::new(self.source_path);
        let arc_dest = Arc::new(self.dest_path);
        for _ in 0..self.thread_count {
            let arc_root = Arc::clone(&arc_root);
            let arc_dest = Arc::clone(&arc_dest);
            let arc_queue = Arc::clone(&queue);
            let arc_factor = Arc::new(self.factor);
            let handle = match self.sender {
                Some(ref s) => {
                    let new_s = s.clone();
                    thread::spawn(move || {
                        process_with_sender(
                            arc_queue,
                            &arc_root,
                            &arc_dest,
                            self.delete_source,
                            *arc_factor.clone(),
                            new_s,
                        );
                    })
                }
                None => thread::spawn(move || {
                    process(
                        arc_queue,
                        &arc_root,
                        &arc_dest,
                        self.delete_source,
                        *arc_factor.clone(),
                    );
                }),
            };
            handles.push(handle);
        }

        for h in handles {
            h.join().unwrap();
        }

        try_send_message(&self.sender, "Compress complete!".to_string());

        if self.delete_source {
            match delete_recursive(&*arc_root) {
                Ok(_) => try_send_message(
                    &self.sender,
                    "Delete source directories complete!".to_string(),
                ),
                Err(e) => try_send_message(
                    &self.sender,
                    format!("Cannot delete source directories: {}", e),
                ),
            };
        }
        Ok(())
    }
}

/// Process function for multithreaded compression.
/// This function is used when user doesn't set a [`Sender`] for [`FolderCompressor`].
fn process(
    queue: Arc<SegQueue<PathBuf>>,
    root: &Path,
    dest: &Path,
    to_delete_source: bool,
    factor: Factor,
) {
    while !queue.is_empty() {
        match queue.pop() {
            None => break,
            Some(file) => {
                let file_name = match file.file_name() {
                    None => "",
                    Some(s) => s.to_str().unwrap_or_else(|| ""),
                };
                let parent = match file.parent() {
                    Some(p) => match p.strip_prefix(root) {
                        Ok(p) => p,
                        Err(_) => {
                            println!("Cannot strip the prefix of file {}", file_name);
                            continue;
                        }
                    },
                    None => {
                        println!("Cannot find the parent directory of file {}", file_name);
                        continue;
                    }
                };
                let new_dest_dir = dest.join(parent);
                if !new_dest_dir.is_dir() {
                    match fs::create_dir_all(&new_dest_dir) {
                        Ok(_) => {}
                        Err(_) => {
                            println!("Cannot create the parent directory of file {}", file_name);
                            continue;
                        }
                    };
                }
                let mut compressor = Compressor::new(&file, new_dest_dir);
                compressor.set_factor(factor);
                compressor.set_delete_source(to_delete_source);
                match compressor.compress_to_jpg() {
                    Ok(_) => {
                        println!("Compress complete! File: {}", file_name);
                    }
                    Err(e) => {
                        println!("Cannot compress image file {} : {}", file_name, e);
                    }
                };
            }
        }
    }
}

/// Process function for multithreaded compression.
/// This function is used when user sets a [`Sender`] for [`FolderCompressor`].
/// This function sends messages to the [`Sender`] when compressing is complete.
fn process_with_sender(
    queue: Arc<SegQueue<PathBuf>>,
    root: &Path,
    dest: &Path,
    to_delete_source: bool,
    factor: Factor,
    sender: Sender<String>,
) {
    while !queue.is_empty() {
        match queue.pop() {
            None => break,
            Some(file) => {
                let file_name = match file.file_name() {
                    None => "",
                    Some(s) => s.to_str().unwrap_or_else(|| ""),
                };
                let parent = match file.parent() {
                    Some(p) => match p.strip_prefix(root) {
                        Ok(p) => p,
                        Err(_) => {
                            println!("Cannot strip the prefix of file {}", file_name);
                            continue;
                        }
                    },
                    None => {
                        println!("Cannot find the parent directory of file {}", file_name);
                        continue;
                    }
                };
                let new_dest_dir = dest.join(parent);
                if !new_dest_dir.is_dir() {
                    match fs::create_dir_all(&new_dest_dir) {
                        Ok(_) => {}
                        Err(_) => {
                            println!("Cannot create the parent directory of file {}", file_name);
                            continue;
                        }
                    };
                }
                let mut compressor = Compressor::new(&file, new_dest_dir);
                compressor.set_factor(factor);
                compressor.set_delete_source(to_delete_source);
                match compressor.compress_to_jpg() {
                    Ok(p) => send_message(
                        &sender,
                        format!(
                            "Compress complete! File: {}",
                            p.file_name().unwrap().to_str().unwrap()
                        ),
                    ),
                    Err(e) => send_message(&sender, e.to_string()),
                };
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use image::ImageBuffer;
    use rand::Rng;
    use std::fs;

    /// Create test directory and an image file in it.
    fn setup<T: AsRef<Path>>(test_name: T) -> (PathBuf, Vec<PathBuf>) {
        let test_dir = test_name.as_ref().to_path_buf();
        if test_dir.is_dir() {
            fs::remove_dir_all(&test_dir).unwrap();
        }
        fs::create_dir_all(&test_dir).unwrap();

        const WIDTH: u32 = 256;
        const HEIGHT: u32 = 256;
        let img_stripe = ImageBuffer::from_fn(WIDTH, HEIGHT, |x, _| {
            if x % 2 == 0 {
                image::Luma([0u8])
            } else {
                image::Luma([255u8])
            }
        });
        let stripe_path = test_dir.join("img_stripe.png");
        img_stripe.save(&stripe_path).unwrap();
        let img_random_rgb = ImageBuffer::from_fn(WIDTH, HEIGHT, |_, _| {
            let r = rand::thread_rng().gen_range(0..256) as u8;
            let g = rand::thread_rng().gen_range(0..256) as u8;
            let b = rand::thread_rng().gen_range(0..256) as u8;
            image::Rgb([r, g, b])
        });
        let rgb_path = test_dir.join("img_random_rgb.gif");
        img_random_rgb.save(&rgb_path).unwrap();
        (test_dir, vec![stripe_path, rgb_path])
    }

    fn cleanup<T: AsRef<Path>>(test_dir: T) {
        if test_dir.as_ref().is_dir() {
            fs::remove_dir_all(&test_dir).unwrap();
        }
    }

    #[test]
    fn folder_compress_test() {
        let (test_source_dir, _) = setup("folder_compress_test_source");
        let test_dest_dir = PathBuf::from("folder_compress_test_dest");
        if test_dest_dir.is_dir() {
            fs::remove_dir_all(&test_dest_dir).unwrap();
        }
        fs::create_dir_all(&test_dest_dir).unwrap();

        let mut folder_compressor = FolderCompressor::new(&test_source_dir, &test_dest_dir);
        folder_compressor.set_thread_count(4);
        folder_compressor.compress().unwrap();
        let a = get_file_list(&test_source_dir).unwrap();
        let b = get_file_list(&test_dest_dir).unwrap();
        let mut source_file_list = a.iter().map(|i| i.file_stem().unwrap()).collect::<Vec<_>>();
        let mut dest_file_list = b.iter().map(|i| i.file_stem().unwrap()).collect::<Vec<_>>();
        source_file_list.sort();
        dest_file_list.sort();
        assert_eq!(source_file_list, dest_file_list);
        cleanup(test_source_dir);
        cleanup(test_dest_dir);
    }
}