img-squeeze 0.1.1

A fast and efficient image compression tool with Walrus storage support
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use crate::constants::{
    DEFAULT_QUALITY, LIBDEFLATER_HIGH_LEVEL, LIBDEFLATER_LOW_LEVEL, MAX_FILE_SIZE,
    MAX_IMAGE_DIMENSION, MAX_QUALITY, MIN_QUALITY, ZOPFLI_ITERATIONS,
};
use crate::error::{CompressionError, Result};
use image::{DynamicImage, GenericImageView, ImageEncoder, ImageFormat, ImageReader};
use indicatif::{ProgressBar, ProgressStyle};
use oxipng::{Deflaters, InFile, Options, OutFile};
use std::fs;
use std::num::NonZeroU8;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct CompressionOptions {
    pub quality: u8,
    pub width: Option<u32>,
    pub height: Option<u32>,
    pub format: Option<String>,
}

impl CompressionOptions {
    pub fn new(
        quality: Option<u8>,
        width: Option<u32>,
        height: Option<u32>,
        format: Option<String>,
    ) -> Result<Self> {
        let quality = quality.unwrap_or(DEFAULT_QUALITY);
        if !(MIN_QUALITY..=MAX_QUALITY).contains(&quality) {
            return Err(CompressionError::InvalidQuality(quality));
        }

        Ok(Self {
            quality,
            width,
            height,
            format,
        })
    }
}

/// Validates that a file exists at the given path.
///
/// # Arguments
/// * `path` - The path to check for existence
///
/// # Returns
/// * `Ok(())` if the file exists
/// * `Err(CompressionError::FileNotFound)` if the file does not exist
///
/// # Example
/// ```
/// use std::path::Path;
/// use img_squeeze::validate_file_exists;
///
/// let result = validate_file_exists(Path::new("nonexistent.jpg"));
/// assert!(result.is_err());
/// ```
pub fn validate_file_exists(path: &Path) -> Result<()> {
    if !path.exists() {
        return Err(CompressionError::FileNotFound(path.to_path_buf()));
    }
    Ok(())
}

/// Core image processing pipeline that handles the common workflow:
/// load -> resize -> process -> save
///
/// # Arguments
/// * `input_path` - Path to the input image file
/// * `output_path` - Path where the processed image will be saved
/// * `options` - Compression and processing options
///
/// # Returns
/// * `Ok((original_size, compressed_size))` - Tuple of file sizes in bytes
/// * `Err(CompressionError)` - If any processing step fails
///
/// # Security
/// - Validates file existence and canonical paths to prevent directory traversal
/// - Enforces maximum file size and image dimension limits
/// - Uses secure temporary file handling
pub fn process_image_pipeline(
    input_path: &Path,
    output_path: &Path,
    options: &CompressionOptions,
) -> Result<(u64, u64)> {
    // Load and validate image
    let (mut img, original_size) = load_image_with_metadata(input_path)?;

    // Resize if needed
    resize_image(&mut img, options);

    // Process and save
    let compressed_size = process_and_save_image(&img, output_path, options)?;

    Ok((original_size, compressed_size))
}

/// Loads an image file and returns it along with file metadata.
///
/// # Arguments
/// * `input_path` - Path to the image file to load
///
/// # Returns
/// * `Ok((image, file_size))` - The loaded image and its file size in bytes
/// * `Err(CompressionError)` - If loading fails or security limits are exceeded
///
/// # Security Features
/// - Validates file existence and canonical paths to prevent directory traversal
/// - Enforces maximum file size limit to prevent DoS attacks
/// - Validates image dimensions to prevent memory exhaustion
/// - Checks file size before attempting to load the image
pub fn load_image_with_metadata(input_path: &Path) -> Result<(DynamicImage, u64)> {
    validate_file_exists(input_path)?;

    // Check for unsupported input formats and provide helpful guidance
    if let Some(ext) = input_path.extension().and_then(|s| s.to_str()) {
        match ext.to_ascii_lowercase().as_str() {
            "heic" | "heif" => {
                return Err(CompressionError::UnsupportedFormat(
                    "HEIC/HEIF format is not yet supported in this version. Use AVIF for modern compression with similar quality and efficiency".to_string()
                ));
            }
            "jxl" | "jpegxl" => {
                return Err(CompressionError::UnsupportedFormat(
                    "JPEG XL format is not yet supported in this version. Use AVIF for modern compression with similar quality and efficiency".to_string()
                ));
            }
            _ => {} // Continue with supported formats
        }
    }

    // Security: Validate path to prevent directory traversal attacks
    let canonical_path = input_path
        .canonicalize()
        .map_err(|_| CompressionError::FileNotFound(input_path.to_path_buf()))?;

    // Check file size before loading to prevent DoS attacks
    let file_size = fs::metadata(&canonical_path)?.len();
    if file_size > MAX_FILE_SIZE {
        return Err(CompressionError::FileTooLarge(file_size, MAX_FILE_SIZE));
    }

    let img = ImageReader::open(&canonical_path)?.decode()?;

    // Security: Validate image dimensions to prevent DoS attacks
    let (width, height) = img.dimensions();
    if width > MAX_IMAGE_DIMENSION || height > MAX_IMAGE_DIMENSION {
        return Err(CompressionError::InvalidDimensions(
            width,
            height,
            MAX_IMAGE_DIMENSION,
        ));
    }

    Ok((img, file_size))
}

pub fn resize_image(img: &mut DynamicImage, options: &CompressionOptions) {
    if let Some(w) = options.width.filter(|&w| w > 0 && w != img.width()) {
        println!("🔄 Resizing width...");
        *img = img.resize_exact(w, img.height(), image::imageops::FilterType::Lanczos3);
        println!("✅ Resized to width: {}", w);
    }

    if let Some(h) = options.height.filter(|&h| h > 0 && h != img.height()) {
        println!("🔄 Resizing height...");
        *img = img.resize_exact(img.width(), h, image::imageops::FilterType::Lanczos3);
        println!("✅ Resized to height: {}", h);
    }
}

pub fn process_and_save_image(
    img: &DynamicImage,
    output_path: &Path,
    options: &CompressionOptions,
) -> Result<u64> {
    let output_buf = output_path.to_path_buf();
    let output_format = determine_output_format(output_path, &options.format)?;
    save_image(img, &output_buf, output_format, options)?;

    let compressed_size = fs::metadata(output_path)?.len();
    Ok(compressed_size)
}

pub fn compress_image(input: PathBuf, output: PathBuf, options: CompressionOptions) -> Result<()> {
    println!("🗜️  Compressing image: {:?}", input);
    println!("📁 Output: {:?}", output);

    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg}")
            .unwrap(),
    );
    pb.set_message("Loading image...");

    let (mut img, original_size) = load_image_with_metadata(&input)?;
    pb.finish_with_message("✅ Image loaded");

    println!(
        "📊 Original size: {} bytes ({}x{})",
        original_size,
        img.width(),
        img.height()
    );

    // Resize if needed
    resize_image(&mut img, &options);

    pb.set_message("Saving compressed image...");
    let compressed_size = process_and_save_image(&img, &output, &options)?;
    pb.finish_with_message("✅ Compression complete");
    let compression_ratio =
        ((original_size as f64 - compressed_size as f64) / original_size as f64) * 100.0;

    println!("📈 Compressed size: {} bytes", compressed_size);
    println!("🎯 Compression ratio: {:.1}%", compression_ratio);

    if compression_ratio > 0.0 {
        println!(
            "✅ Successfully reduced file size by {:.1}%",
            compression_ratio
        );
    } else {
        println!("⚠️  File size increased by {:.1}%", compression_ratio.abs());
    }

    Ok(())
}

pub fn determine_output_format(output: &Path, format: &Option<String>) -> Result<ImageFormat> {
    if let Some(fmt) = format {
        match fmt.to_lowercase().as_str() {
            "jpeg" | "jpg" => Ok(ImageFormat::Jpeg),
            "png" => Ok(ImageFormat::Png),
            "webp" => Ok(ImageFormat::WebP),
            "avif" => Ok(ImageFormat::Avif),
            "heic" | "heif" => Err(CompressionError::UnsupportedFormat(
                format!("{} format is not yet supported in this version. Use AVIF for modern compression", fmt)
            )),
            "jxl" | "jpegxl" => Err(CompressionError::UnsupportedFormat(
                format!("{} format is not yet supported in this version. Use AVIF for modern compression", fmt)
            )),
            _ => Err(CompressionError::UnsupportedFormat(fmt.clone())),
        }
    } else if let Some(ext) = output.extension().and_then(|ext| ext.to_str()) {
        let ext = ext.to_ascii_lowercase();
        match ext.as_str() {
            "jpg" | "jpeg" => Ok(ImageFormat::Jpeg),
            "png" => Ok(ImageFormat::Png),
            "webp" => Ok(ImageFormat::WebP),
            "avif" => Ok(ImageFormat::Avif),
            "heic" | "heif" => Err(CompressionError::UnsupportedFormat(
                format!("{} format is not yet supported in this version. Use AVIF for modern compression", ext)
            )),
            "jxl" | "jpegxl" => Err(CompressionError::UnsupportedFormat(
                format!("{} format is not yet supported in this version. Use AVIF for modern compression", ext)
            )),
            _ => Ok(ImageFormat::Jpeg),
        }
    } else {
        Ok(ImageFormat::Jpeg)
    }
}

pub fn save_image(
    img: &DynamicImage,
    output: &PathBuf,
    format: ImageFormat,
    options: &CompressionOptions,
) -> Result<()> {
    if let Some(parent) = output.parent() {
        fs::create_dir_all(parent)
            .map_err(|_| CompressionError::DirectoryCreationFailed(parent.to_path_buf()))?;
    }

    match format {
        ImageFormat::Jpeg => {
            img.save_with_format(output, image::ImageFormat::Jpeg)?;
        }
        ImageFormat::Png => {
            // 使用 oxipng 进行 PNG 优化
            let (_width, _height) = img.dimensions();

            // Performance: Use secure temp file with proper cleanup
            let temp_path = output.with_extension("temp.png");
            img.save_with_format(&temp_path, image::ImageFormat::Png)?;

            // Performance: Ensure cleanup on any error using RAII pattern
            struct TempFileGuard(PathBuf);
            impl Drop for TempFileGuard {
                fn drop(&mut self) {
                    let _ = fs::remove_file(&self.0);
                }
            }
            let _guard = TempFileGuard(temp_path.clone());

            // 配置 oxipng 选项
            let mut oxipng_options = Options::from_preset(4); // 使用预设 4 (最高压缩)
            oxipng_options.force = true; // 强制覆盖

            // 根据质量设置调整压缩级别
            if options.quality >= 90 {
                oxipng_options.deflate = Deflaters::Zopfli {
                    iterations: NonZeroU8::new(ZOPFLI_ITERATIONS).unwrap(),
                };
            } else if options.quality >= 70 {
                oxipng_options.deflate = Deflaters::Libdeflater {
                    compression: LIBDEFLATER_HIGH_LEVEL,
                };
            } else {
                oxipng_options.deflate = Deflaters::Libdeflater {
                    compression: LIBDEFLATER_LOW_LEVEL,
                };
            }

            // 使用 oxipng 优化文件
            let input = InFile::Path(temp_path.clone());
            let out = OutFile::Path {
                path: Some(output.clone()),
                preserve_attrs: false,
            };
            oxipng::optimize(&input, &out, &oxipng_options)
                .map_err(|e| CompressionError::PngOptimization(e.to_string()))?;

            // Temp file automatically cleaned up by guard
        }
        ImageFormat::WebP => {
            img.save_with_format(output, image::ImageFormat::WebP)?;
        }
        ImageFormat::Avif => {
            // Honor quality and enable parallel encoding (when "image/rayon" is enabled).
            use image::codecs::avif::AvifEncoder;
            let rgba = img.to_rgba8();
            let (w, h) = (rgba.width(), rgba.height());
            let mut file = std::fs::File::create(output)?;
            // Heuristic speed; consider surfacing as an option later.
            let speed: u8 = 6;
            let enc = AvifEncoder::new_with_speed_quality(&mut file, speed, options.quality);
            // If you later thread this via CLI, call: enc = enc.with_num_threads(Some(n));
            enc.write_image(
                rgba.as_raw(),
                w,
                h,
                image::ExtendedColorType::Rgba8
            )?;
        }
        _ => {
            return Err(CompressionError::UnsupportedFormat(format!("{:?}", format)));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compression_options_creation() {
        let options =
            CompressionOptions::new(Some(85), Some(800), Some(600), Some("webp".to_string()))
                .unwrap();
        assert_eq!(options.quality, 85);
        assert_eq!(options.width, Some(800));
        assert_eq!(options.height, Some(600));
        assert_eq!(options.format, Some("webp".to_string()));
    }

    #[test]
    fn test_compression_options_default() {
        let options = CompressionOptions::new(None, None, None, None).unwrap();
        assert_eq!(options.quality, 80);
        assert_eq!(options.width, None);
        assert_eq!(options.height, None);
        assert_eq!(options.format, None);
    }

    #[test]
    fn test_compression_options_invalid_quality() {
        let result = CompressionOptions::new(Some(0), None, None, None);
        assert!(matches!(result, Err(CompressionError::InvalidQuality(0))));

        let result = CompressionOptions::new(Some(101), None, None, None);
        assert!(matches!(result, Err(CompressionError::InvalidQuality(101))));
    }

    #[test]
    fn test_determine_output_format() {
        let path = Path::new("test.jpg");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::Jpeg);

        let path = Path::new("test.png");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::Png);

        let path = Path::new("test.webp");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::WebP);

        let path = Path::new("test.avif");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::Avif);

        let path = Path::new("test.unknown");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::Jpeg);
    }

    #[test]
    fn test_determine_output_format_with_override() {
        let path = Path::new("test.jpg");
        let format = determine_output_format(path, &Some("png".to_string())).unwrap();
        assert_eq!(format, ImageFormat::Png);

        let path = Path::new("test.png");
        let format = determine_output_format(path, &Some("avif".to_string())).unwrap();
        assert_eq!(format, ImageFormat::Avif);
    }

    #[test]
    fn test_determine_output_format_unsupported() {
        let path = Path::new("test.jpg");
        let result = determine_output_format(path, &Some("unsupported".to_string()));
        assert!(matches!(
            result,
            Err(CompressionError::UnsupportedFormat(_))
        ));

        // Test HEIC/HEIF recognition with helpful error message
        let result = determine_output_format(path, &Some("heic".to_string()));
        assert!(matches!(
            result,
            Err(CompressionError::UnsupportedFormat(_))
        ));
        if let Err(CompressionError::UnsupportedFormat(msg)) = result {
            assert!(msg.contains("not yet supported"));
            assert!(msg.contains("AVIF"));
        }

        // Test JPEG XL recognition with helpful error message  
        let result = determine_output_format(path, &Some("jxl".to_string()));
        assert!(matches!(
            result,
            Err(CompressionError::UnsupportedFormat(_))
        ));
        if let Err(CompressionError::UnsupportedFormat(msg)) = result {
            assert!(msg.contains("not yet supported"));
            assert!(msg.contains("AVIF"));
        }

        // Alias: jpegxl should behave like jxl
        let result = determine_output_format(path, &Some("jpegxl".to_string()));
        assert!(matches!(result, Err(CompressionError::UnsupportedFormat(_))));
        if let Err(CompressionError::UnsupportedFormat(msg)) = result {
            assert!(msg.contains("not yet supported"));
            assert!(msg.contains("AVIF"));
        }
    }

    #[test]
    fn test_determine_output_format_case_insensitive_exts() {
        // After normalizing extension cases, uppercase should work.
        let path = Path::new("IMAGE.AVIF");
        let format = determine_output_format(path, &None).unwrap();
        assert_eq!(format, ImageFormat::Avif);

        // Unsupported alias via extension
        let path = Path::new("photo.jpegxl");
        let result = determine_output_format(path, &None);
        assert!(matches!(result, Err(CompressionError::UnsupportedFormat(_))));
    }

    #[test]
    fn test_resize_image_dimensions() {
        let mut img = DynamicImage::new_rgb8(2000, 1500);
        let options = CompressionOptions::new(Some(80), Some(1000), None, None).unwrap();

        resize_image(&mut img, &options);

        assert_eq!(img.dimensions(), (1000, 1500));
    }

    #[test]
    fn test_resize_image_height_only() {
        let mut img = DynamicImage::new_rgb8(2000, 1500);
        let options = CompressionOptions::new(Some(80), None, Some(750), None).unwrap();

        resize_image(&mut img, &options);

        assert_eq!(img.dimensions(), (2000, 750));
    }

    #[test]
    fn test_resize_image_both_dimensions() {
        let mut img = DynamicImage::new_rgb8(2000, 1500);
        let options = CompressionOptions::new(Some(80), Some(800), Some(600), None).unwrap();

        resize_image(&mut img, &options);

        assert_eq!(img.dimensions(), (800, 600));
    }

    #[test]
    fn test_resize_image_no_dimensions() {
        let mut img = DynamicImage::new_rgb8(2000, 1500);
        let options = CompressionOptions::new(Some(80), None, None, None).unwrap();

        resize_image(&mut img, &options);

        assert_eq!(img.dimensions(), (2000, 1500));
    }

    #[test]
    fn test_resize_image_same_dimensions() {
        let mut img = DynamicImage::new_rgb8(2000, 1500);
        let options = CompressionOptions::new(Some(80), Some(2000), Some(1500), None).unwrap();

        resize_image(&mut img, &options);

        assert_eq!(img.dimensions(), (2000, 1500));
    }

    #[test]
    fn test_load_image_with_metadata_not_found() {
        let path = Path::new("nonexistent.jpg");
        let result = load_image_with_metadata(path);
        assert!(matches!(result, Err(CompressionError::FileNotFound(_))));
    }
}