img4avif 0.7.0

Serverless-optimized image conversion: JPEG/PNG/WebP/HEIC → AVIF with HDR10 input support (pure Rust, Lambda-ready)
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
//! Output resolution control and image resizing.
//!
//! This module defines the [`OutputResolution`] enum that controls how a decoded
//! image is scaled before AVIF encoding.  The actual downscaling is performed
//! by `resize_raw_image`, which uses a Lanczos-3 filter for high quality.
//!
//! ## Behaviour
//!
//! - **Only downscales** — if the image is already at or below the target width
//!   it is returned unchanged.  This prevents quality loss from unnecessary
//!   upscaling.
//! - **Preserves aspect ratio** — the height is computed proportionally so the
//!   image is never cropped or stretched.
//! - **Both bit-depths** — 8-bit (`Rgba8`) and 16-bit (`Rgba16`) images are
//!   handled; 16-bit precision is preserved through the resize step.

use crate::decoder::{Pixels, RawImage};
use crate::error::Error;
use crate::logging::{img_debug, img_info};
use std::sync::Arc;

/// Controls the output resolution applied before AVIF encoding.
///
/// Set this on [`Config`](crate::Config) via
/// [`Config::output_resolutions`](crate::Config::output_resolutions) to
/// produce one or more outputs at different sizes from a single decode pass.
///
/// # Downscale-only
///
/// When the decoded image is already **at or below** the target width the
/// pixels are passed through unchanged — `img4avif` never upscales.
///
/// # Aspect ratio
///
/// The height is always scaled proportionally so the image is never cropped
/// or distorted.
///
/// # Example
///
/// ```rust,no_run
/// use img4avif::{Config, Converter, OutputResolution};
///
/// # fn main() -> Result<(), img4avif::Error> {
/// // Produce only the 1080-wide variant.
/// let config = Config::default()
///     .output_resolutions(vec![OutputResolution::Width1080]);
/// let converter = Converter::new(config)?;
/// let avif_1080 = converter.convert(&std::fs::read("photo.jpg")?)?;
/// std::fs::write("photo_1080.avif", avif_1080)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OutputResolution {
    /// Preserve the original image dimensions (no resize).
    Original,
    /// Shrink so the width is at most **2560 pixels**, preserving the aspect
    /// ratio.  Images already ≤ 2560 px wide are passed through unchanged.
    Width2560,
    /// Shrink so the width is at most **1080 pixels**, preserving the aspect
    /// ratio.  Images already ≤ 1080 px wide are passed through unchanged.
    Width1080,
    /// Shrink so the width is at most the given number of pixels, preserving
    /// the aspect ratio.  Images already at or below this width are passed
    /// through unchanged.
    ///
    /// A width of `0` is treated the same as [`Original`](Self::Original) —
    /// the image is returned at its full size without any resizing.  This is
    /// a deliberate design choice that makes it safe to derive a target width
    /// from arithmetic that could produce zero; callers that want a hard error
    /// on zero should validate the value before constructing this variant.
    Custom(u32),
}

impl OutputResolution {
    /// Returns the maximum permitted width for this variant, or `None` for
    /// [`OutputResolution::Original`] (no limit) and for
    /// [`OutputResolution::Custom(0)`](OutputResolution::Custom) (treated as
    /// no limit).
    #[must_use]
    pub(crate) fn max_width(self) -> Option<u32> {
        match self {
            Self::Original | Self::Custom(0) => None,
            Self::Width2560 => Some(2560),
            Self::Width1080 => Some(1080),
            Self::Custom(w) => Some(w),
        }
    }
}

/// Resize `raw` to fit within `resolution`, preserving the aspect ratio.
///
/// **Only downscales** — if the image width is already ≤ the target width the
/// input is returned unchanged without copying the pixel data.
///
/// Uses the Lanczos-3 filter for high-quality downsampling.  Both
/// [`Pixels::Rgba8`] and [`Pixels::Rgba16`] inputs are supported; 16-bit
/// precision is preserved throughout the resize step.
///
/// Accepts the image by shared reference so that callers can invoke this
/// function multiple times on the same [`RawImage`] (e.g. `convert_multi`)
/// without cloning the pixel buffer upfront.
///
/// # Errors
///
/// Returns [`Error::Internal`] if the pixel buffer does not match the
/// declared image dimensions.  This should never happen with images produced
/// by the built-in decoders; if it does, please report a bug.
pub(crate) fn resize_raw_image(
    raw: &RawImage,
    resolution: OutputResolution,
) -> Result<RawImage, Error> {
    let Some(target_width) = resolution.max_width() else {
        // OutputResolution::Original — return the image unchanged.
        return Ok(raw.clone());
    };

    let &RawImage {
        width,
        height,
        ref pixels,
    } = raw;

    if width <= target_width {
        img_debug!(
            "resize: {}×{} is already within {}px target — skipping",
            width,
            height,
            target_width
        );
        return Ok(RawImage {
            width,
            height,
            pixels: pixels.clone(),
        });
    }

    let new_width = target_width;
    // Proportional height, rounded to nearest pixel.  Use saturating u64
    // arithmetic so that extreme aspect ratios cannot silently produce a
    // 1-pixel-tall output via integer overflow.
    let height_u64 = u64::from(height)
        .saturating_mul(u64::from(new_width))
        .saturating_add(u64::from(width) / 2)
        / u64::from(width);

    let new_height = u32::try_from(height_u64)
        .map_err(|_| {
            Error::Internal(format!(
                "resize calculation overflow: {width}×{height} → width {target_width}"
            ))
        })?
        .max(1);

    img_info!(
        "resize: {}×{} → {}×{} ({} target width, Lanczos3)",
        width,
        height,
        new_width,
        new_height,
        target_width
    );

    match pixels {
        Pixels::Rgba8(data) => {
            let buf =
                image::RgbaImage::from_raw(width, height, data.to_vec()).ok_or_else(|| {
                    Error::Internal(format!(
                    "RGBA8 pixel buffer size does not match declared dimensions {width}×{height}; \
                     this is a bug — please report it"
                ))
                })?;
            let resized = image::imageops::resize(
                &buf,
                new_width,
                new_height,
                image::imageops::FilterType::Lanczos3,
            );
            Ok(RawImage {
                width: new_width,
                height: new_height,
                pixels: Pixels::Rgba8(Arc::from(resized.into_raw())),
            })
        }
        Pixels::Rgba16(data) => {
            use image::{ImageBuffer, Rgba};
            let buf: ImageBuffer<Rgba<u16>, Vec<u16>> =
                ImageBuffer::from_raw(width, height, data.to_vec())
                    .ok_or_else(|| Error::Internal(format!(
                        "RGBA16 pixel buffer size does not match declared dimensions {width}×{height}; \
                         this is a bug — please report it"
                    )))?;
            let resized = image::imageops::resize(
                &buf,
                new_width,
                new_height,
                image::imageops::FilterType::Lanczos3,
            );
            Ok(RawImage {
                width: new_width,
                height: new_height,
                pixels: Pixels::Rgba16(Arc::from(resized.into_raw())),
            })
        }
    }
}

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

    fn solid_rgba8(width: u32, height: u32) -> RawImage {
        let pixel = [255u8, 128, 64, 255];
        RawImage {
            width,
            height,
            pixels: Pixels::Rgba8(Arc::from(pixel.repeat(width as usize * height as usize))),
        }
    }

    fn solid_rgba16(width: u32, height: u32) -> RawImage {
        let pixel = [32768u16, 16384, 8192, 65535];
        RawImage {
            width,
            height,
            pixels: Pixels::Rgba16(Arc::from(pixel.repeat(width as usize * height as usize))),
        }
    }

    #[test]
    fn original_is_unchanged() {
        let raw = solid_rgba8(4000, 3000);
        let out = resize_raw_image(&raw, OutputResolution::Original).unwrap();
        assert_eq!(out.width, 4000);
        assert_eq!(out.height, 3000);
    }

    /// No-op resize paths share the pixel buffer via Arc (no deep copy).
    #[test]
    fn no_op_resize_shares_arc_allocation() {
        let raw = solid_rgba8(640, 480);

        // OutputResolution::Original must share the same Arc allocation.
        let out_original = resize_raw_image(&raw, OutputResolution::Original).unwrap();
        if let (Pixels::Rgba8(src), Pixels::Rgba8(dst)) = (&raw.pixels, &out_original.pixels) {
            assert!(Arc::ptr_eq(src, dst), "Original path must share the Arc");
        } else {
            panic!("expected Rgba8 pixels");
        }

        // Already-small path (width <= target) must also share the Arc.
        let out_2560 = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        if let (Pixels::Rgba8(src), Pixels::Rgba8(dst)) = (&raw.pixels, &out_2560.pixels) {
            assert!(Arc::ptr_eq(src, dst), "No-op resize must share the Arc");
        } else {
            panic!("expected Rgba8 pixels");
        }
    }

    #[test]
    fn no_upscale_when_already_small() {
        // A 640-wide image should not be upscaled to 2560 or 1080.
        let raw = solid_rgba8(640, 480);
        let out2560 = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out2560.width, 640);
        assert_eq!(out2560.height, 480);

        let out1080 = resize_raw_image(&raw, OutputResolution::Width1080).unwrap();
        assert_eq!(out1080.width, 640);
        assert_eq!(out1080.height, 480);
    }

    #[test]
    fn downscales_to_2560() {
        let raw = solid_rgba8(5120, 2880); // 16:9 at 5K
        let out = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out.width, 2560);
        assert_eq!(out.height, 1440); // 16:9 preserved
    }

    #[test]
    fn downscales_to_1080() {
        let raw = solid_rgba8(1920, 1080); // Full HD
        let out = resize_raw_image(&raw, OutputResolution::Width1080).unwrap();
        assert_eq!(out.width, 1080);
        // Height: (1080 * 1080 + 960) / 1920 = 1167360 / 1920 = 608 (exactly)
        assert_eq!(out.height, 608);
    }

    #[test]
    fn aspect_ratio_preserved_portrait() {
        // Portrait 2:3 at 4320×6480 (higher-res)
        let raw = solid_rgba8(4320, 6480);
        let out = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out.width, 2560);
        // height = 6480 * 2560 / 4320 = 3840
        assert_eq!(out.height, 3840);
    }

    #[test]
    fn exact_target_width_is_not_resized() {
        let raw = solid_rgba8(2560, 1440);
        let out = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out.width, 2560);
        assert_eq!(out.height, 1440);
    }

    #[test]
    fn custom_resolution_downscales() {
        let raw = solid_rgba8(1920, 1080);
        let out = resize_raw_image(&raw, OutputResolution::Custom(720)).unwrap();
        assert_eq!(out.width, 720);
    }

    #[test]
    fn custom_resolution_zero_is_original() {
        let raw = solid_rgba8(1920, 1080);
        let out = resize_raw_image(&raw, OutputResolution::Custom(0)).unwrap();
        assert_eq!(out.width, 1920);
        assert_eq!(out.height, 1080);
    }

    #[test]
    fn custom_resolution_no_upscale() {
        let raw = solid_rgba8(640, 480);
        let out = resize_raw_image(&raw, OutputResolution::Custom(1280)).unwrap();
        assert_eq!(out.width, 640);
        assert_eq!(out.height, 480);
    }

    #[test]
    fn output_resolution_max_width() {
        assert_eq!(OutputResolution::Original.max_width(), None);
        assert_eq!(OutputResolution::Width2560.max_width(), Some(2560));
        assert_eq!(OutputResolution::Width1080.max_width(), Some(1080));
        assert_eq!(OutputResolution::Custom(720).max_width(), Some(720));
        assert_eq!(OutputResolution::Custom(3840).max_width(), Some(3840));
        assert_eq!(OutputResolution::Custom(0).max_width(), None);
    }

    // --- 16-bit resize tests ---

    #[test]
    fn rgba16_original_is_unchanged() {
        let raw = solid_rgba16(4000, 3000);
        let out = resize_raw_image(&raw, OutputResolution::Original).unwrap();
        assert_eq!(out.width, 4000);
        assert_eq!(out.height, 3000);
        assert!(matches!(out.pixels, Pixels::Rgba16(_)));
    }

    #[test]
    fn rgba16_downscales_to_2560() {
        let raw = solid_rgba16(5120, 2880);
        let out = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out.width, 2560);
        assert_eq!(out.height, 1440);
        assert!(matches!(out.pixels, Pixels::Rgba16(_)));
    }

    #[test]
    fn rgba16_no_upscale() {
        let raw = solid_rgba16(640, 480);
        let out = resize_raw_image(&raw, OutputResolution::Width1080).unwrap();
        assert_eq!(out.width, 640);
        assert_eq!(out.height, 480);
    }

    // --- buffer mismatch returns Error::Internal ---

    #[test]
    fn mismatched_rgba8_buffer_returns_internal_error() {
        // Declare a wide image (width > 1080) so the resize path is actually
        // triggered and can detect the buffer/dimension mismatch.
        // A width of 100 would be skipped (no downscale needed), so the error
        // would never occur — that was the pre-existing bug in this test.
        let raw = RawImage {
            width: 2000,
            height: 100,
            // Only 1 pixel worth of data instead of 2000 * 100 pixels
            pixels: Pixels::Rgba8(Arc::from([255u8, 0, 0, 255].as_slice())),
        };
        let err = resize_raw_image(&raw, OutputResolution::Width1080).unwrap_err();
        assert!(
            matches!(err, Error::Internal(_)),
            "expected Error::Internal, got {err:?}"
        );
    }

    #[test]
    fn mismatched_rgba16_buffer_returns_internal_error() {
        let raw = RawImage {
            width: 2000,
            height: 100,
            pixels: Pixels::Rgba16(Arc::from([65535u16, 0, 0, 65535].as_slice())),
        };
        let err = resize_raw_image(&raw, OutputResolution::Width1080).unwrap_err();
        assert!(
            matches!(err, Error::Internal(_)),
            "expected Error::Internal, got {err:?}"
        );
    }

    // --- degenerate dimensions ---

    #[test]
    fn very_wide_single_row() {
        // 2000×1 image, resize to Width1080 → 1080×1 (height stays 1)
        let raw = solid_rgba8(2000, 1);
        let out = resize_raw_image(&raw, OutputResolution::Width1080).unwrap();
        assert_eq!(out.width, 1080);
        assert_eq!(out.height, 1); // floor((1 * 1080 + 1000) / 2000) = 1
    }

    #[test]
    fn single_pixel_image() {
        // 1×1 image is below every target width — returned unchanged
        let raw = solid_rgba8(1, 1);
        let out2560 = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out2560.width, 1);
        let out1080 = resize_raw_image(&raw, OutputResolution::Width1080).unwrap();
        assert_eq!(out1080.width, 1);
    }

    #[test]
    fn saturating_arithmetic_does_not_truncate_tall_image() {
        // A very tall portrait image: 4096×16384 resized to Width2560.
        // height_u64 = (16384 * 2560 + 2048) / 4096 = 10,240 — fits in u32.
        // This test verifies the saturating-arithmetic path produces the
        // correct proportional height without any silent truncation.
        let raw = solid_rgba8(4096, 16384);
        let out = resize_raw_image(&raw, OutputResolution::Width2560).unwrap();
        assert_eq!(out.width, 2560);
        // height = (16384 * 2560 + 2048) / 4096 = 10240
        assert_eq!(out.height, 10240);
    }
}