maudit 0.11.0

Library for generating static websites.
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use std::fmt::Display;
use std::hash::Hash;
use std::{path::PathBuf, sync::OnceLock, time::Instant};

use base64::Engine;
use image::{GenericImageView, image_dimensions};
use log::debug;
use thumbhash::{rgba_to_thumb_hash, thumb_hash_to_average_rgba, thumb_hash_to_rgba};

use crate::assets::image_cache::ImageCache;
use crate::assets::{RouteAssetsOptions, make_filename, make_final_path, make_final_url};
use crate::is_dev;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ImageFormat {
    Png,
    Jpeg,
    WebP,
    Avif,
    Gif,
}

impl ImageFormat {
    pub fn extension(&self) -> &'static str {
        match self {
            ImageFormat::Png => "png",
            ImageFormat::Jpeg => "jpg",
            ImageFormat::WebP => "webp",
            ImageFormat::Avif => "avif",
            ImageFormat::Gif => "gif",
        }
    }

    pub(crate) fn to_hash_value(&self) -> u32 {
        match self {
            ImageFormat::Png => 1,
            ImageFormat::Jpeg => 2,
            ImageFormat::WebP => 3,
            ImageFormat::Gif => 4,
            ImageFormat::Avif => 5,
        }
    }
}

impl From<ImageFormat> for image::ImageFormat {
    fn from(val: ImageFormat) -> Self {
        match val {
            ImageFormat::Png => image::ImageFormat::Png,
            ImageFormat::Jpeg => image::ImageFormat::Jpeg,
            ImageFormat::WebP => image::ImageFormat::WebP,
            ImageFormat::Avif => image::ImageFormat::Avif,
            ImageFormat::Gif => image::ImageFormat::Gif,
        }
    }
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImageOptions {
    pub width: Option<u32>,
    pub height: Option<u32>,
    pub format: Option<ImageFormat>,
}

/// Represents an image asset, typically obtained using `ctx.assets.add_image` in a route.
///
/// # Example
/// ```rust
/// use maudit::route::prelude::*;
///
/// #[route("/example")]
/// pub struct ExampleRoute;
///
/// impl Route for ExampleRoute {
///     fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {
///        let image = ctx.assets.add_image("path/to/image.png")?;
///
///        Ok(format!("<img src=\"{}\" alt=\"Example Image\" />", image.url()))
///     }
/// }
/// ```
#[derive(Clone, Debug)]
pub struct Image {
    pub path: PathBuf,
    pub(crate) hash: String,
    pub(crate) options: Option<ImageOptions>,

    pub(crate) filename: PathBuf,
    pub(crate) url: String,
    pub(crate) build_path: PathBuf,
    pub(crate) cache: Option<ImageCache>,
}

impl Hash for Image {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.path.hash(state);
        self.hash.hash(state);
        self.options.hash(state);
        self.filename.hash(state);
        self.url.hash(state);
        self.build_path.hash(state);
        // Intentionally exclude cache from hash
    }
}

impl PartialEq for Image {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
            && self.hash == other.hash
            && self.options == other.options
            && self.filename == other.filename
            && self.url == other.url
            && self.build_path == other.build_path
        // Intentionally exclude cache from equality
    }
}

impl Eq for Image {}

impl Image {
    pub fn new(
        path: PathBuf,
        image_options: Option<ImageOptions>,
        hash: String,
        route_assets_options: &RouteAssetsOptions,
        cache: Option<ImageCache>,
    ) -> Self {
        let filename = make_filename(
            &path,
            &hash,
            image_options
                .as_ref()
                .and_then(|opts| opts.format.as_ref().map(|f| f.extension().into()))
                .or_else(|| {
                    path.extension()
                        .and_then(|ext| ext.to_str())
                        .map(|s| s.to_lowercase())
                })
                .as_deref(),
        );
        let build_path = make_final_path(&route_assets_options.output_assets_dir, &filename);
        let url = make_final_url(&route_assets_options.assets_dir, &filename);

        Self {
            path,
            hash,
            options: image_options.clone(),
            filename,
            url,
            build_path,
            cache,
        }
    }

    /// Get a placeholder for the image, which can be used for low-quality image placeholders (LQIP) or similar techniques.
    ///
    /// This uses the [ThumbHash](https://evanw.github.io/thumbhash/) algorithm to generate a very small placeholder image.
    ///
    /// Returns an error if the image cannot be loaded.
    pub fn placeholder(&self) -> Result<ImagePlaceholder, crate::errors::AssetError> {
        get_placeholder(&self.path, self.cache.as_ref())
    }

    // Get the dimensions of an image. Note that at this time, unsupported file formats such as SVGs will return (0, 0).
    pub fn dimensions(&self) -> (u32, u32) {
        image_dimensions(&self.path).unwrap_or((0, 0))
    }
}

#[derive(Debug)]
pub struct ImagePlaceholder {
    pub thumbhash: Vec<u8>,
    pub thumbhash_base64: String,
    average_rgba_cache: OnceLock<Option<(u8, u8, u8, u8)>>,
    data_uri_cache: OnceLock<String>,
}

impl Clone for ImagePlaceholder {
    fn clone(&self) -> Self {
        Self {
            thumbhash: self.thumbhash.clone(),
            thumbhash_base64: self.thumbhash_base64.clone(),
            average_rgba_cache: OnceLock::new(),
            data_uri_cache: OnceLock::new(),
        }
    }
}

impl Default for ImagePlaceholder {
    fn default() -> Self {
        Self {
            thumbhash: Vec::new(),
            thumbhash_base64: String::new(),
            average_rgba_cache: OnceLock::new(),
            data_uri_cache: OnceLock::new(),
        }
    }
}

impl ImagePlaceholder {
    pub fn new(thumbhash: Vec<u8>, thumbhash_base64: String) -> Self {
        Self {
            thumbhash,
            thumbhash_base64,
            average_rgba_cache: OnceLock::new(),
            data_uri_cache: OnceLock::new(),
        }
    }

    pub fn average_rgba(&self) -> Option<(u8, u8, u8, u8)> {
        *self.average_rgba_cache.get_or_init(|| {
            let start = Instant::now();
            let result = thumb_hash_to_average_rgba(&self.thumbhash)
                .ok()
                .map(|(r, g, b, a)| {
                    (
                        (r * 255.0) as u8,
                        (g * 255.0) as u8,
                        (b * 255.0) as u8,
                        (a * 255.0) as u8,
                    )
                });
            debug!("Average RGBA calculation took {:?}", start.elapsed());
            result
        })
    }

    pub fn data_uri(&self) -> &str {
        self.data_uri_cache.get_or_init(|| {
            let start = Instant::now();

            let rgba_start = Instant::now();
            let thumbhash_rgba = thumb_hash_to_rgba(&self.thumbhash).unwrap();
            debug!(
                "ThumbHash to RGBA conversion took {:?}",
                rgba_start.elapsed()
            );

            let png_start = Instant::now();
            let thumbhash_png = thumbhash_to_png(&thumbhash_rgba);
            debug!("PNG generation took {:?}", png_start.elapsed());

            let optimized_png = if is_dev() {
                thumbhash_png
            } else {
                let optimize_start = Instant::now();
                let result =
                    oxipng::optimize_from_memory(&thumbhash_png, &Default::default()).unwrap();
                debug!("PNG optimization took {:?}", optimize_start.elapsed());
                result
            };

            let encode_start = Instant::now();
            let base64 = base64::engine::general_purpose::STANDARD.encode(&optimized_png);
            let result = format!("data:image/png;base64,{}", base64);
            debug!("Data URI encoding took {:?}", encode_start.elapsed());

            debug!("Total data URI generation took {:?}", start.elapsed());
            result
        })
    }
}

fn get_placeholder(
    path: &PathBuf,
    cache: Option<&ImageCache>,
) -> Result<ImagePlaceholder, crate::errors::AssetError> {
    // Check cache first if provided
    if let Some(cache) = cache
        && let Some(cached) = cache.get_placeholder(path)
    {
        debug!("Using cached placeholder for {}", path.display());
        let thumbhash_base64 = base64::engine::general_purpose::STANDARD.encode(&cached.thumbhash);
        return Ok(ImagePlaceholder::new(cached.thumbhash, thumbhash_base64));
    }

    let total_start = Instant::now();

    let load_start = Instant::now();
    let image = image::open(path).map_err(|e| crate::errors::AssetError::ImageLoadFailed {
        path: path.clone(),
        source: e,
    })?;
    let (width, height) = image.dimensions();
    let (width, height) = (width as usize, height as usize);
    debug!(
        "Image load took {:?} for {}",
        load_start.elapsed(),
        path.display()
    );

    // If width or height > 100, resize image down to max 100
    let (width, height, rgba) = if width.max(height) > 100 {
        let resize_start = Instant::now();
        let scale = 100.0 / width.max(height) as f32;
        let new_width = (width as f32 * scale).round() as usize;
        let new_height = (height as f32 * scale).round() as usize;

        let resized = image::imageops::resize(
            &image,
            new_width as u32,
            new_height as u32,
            image::imageops::FilterType::Nearest,
        );
        let result = (new_width, new_height, resized.into_raw());
        debug!(
            "Image resize took {:?} ({}x{} -> {}x{})",
            resize_start.elapsed(),
            width,
            height,
            new_width,
            new_height
        );
        result
    } else {
        let convert_start = Instant::now();
        let result = (width, height, image.to_rgba8().into_raw());
        debug!("Image RGBA conversion took {:?}", convert_start.elapsed());
        result
    };

    let thumbhash_start = Instant::now();
    let thumb_hash = rgba_to_thumb_hash(width, height, &rgba);
    debug!("ThumbHash generation took {:?}", thumbhash_start.elapsed());

    let encode_start = Instant::now();
    let thumbhash_base64 = base64::engine::general_purpose::STANDARD.encode(&thumb_hash);
    debug!("Base64 encoding took {:?}", encode_start.elapsed());

    debug!(
        "Total placeholder generation took {:?} for {}",
        total_start.elapsed(),
        path.display()
    );

    // Cache the result if cache is provided
    if let Some(cache) = cache {
        cache.cache_placeholder(path, thumb_hash.clone());
    }

    Ok(ImagePlaceholder::new(thumb_hash, thumbhash_base64))
}

/// Port of https://github.com/evanw/thumbhash/blob/a652ce6ed691242f459f468f0a8756cda3b90a82/js/thumbhash.js#L234
/// TODO: Do this some other way, not only is the code, well, unreadable, the result is also quite inefficient.
fn thumbhash_to_png(thumbhash_rgba: &(usize, usize, Vec<u8>)) -> Vec<u8> {
    let w = thumbhash_rgba.0 as u32;
    let h = thumbhash_rgba.1 as u32;
    let rgba = &thumbhash_rgba.2;

    let row = w * 4 + 1;
    let idat = 6 + h * (5 + row);

    let mut bytes = vec![
        137,
        80,
        78,
        71,
        13,
        10,
        26,
        10,
        0,
        0,
        0,
        13,
        73,
        72,
        68,
        82,
        0,
        0,
        (w >> 8) as u8,
        (w & 255) as u8,
        0,
        0,
        (h >> 8) as u8,
        (h & 255) as u8,
        8,
        6,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        (idat >> 24) as u8,
        ((idat >> 16) & 255) as u8,
        ((idat >> 8) & 255) as u8,
        (idat & 255) as u8,
        73,
        68,
        65,
        84,
        120,
        1,
    ];

    let table = [
        0u32, 498536548, 997073096, 651767980, 1994146192, 1802195444, 1303535960, 1342533948,
        3988292384, 4027552580, 3604390888, 3412177804, 2607071920, 2262029012, 2685067896,
        3183342108,
    ];

    let mut a = 1u32;
    let mut b = 0u32;
    let mut i = 0usize;
    let mut end = (row - 1) as usize;

    for y in 0..h {
        let filter_type = if y + 1 < h { 0 } else { 1 };
        bytes.extend_from_slice(&[
            filter_type,
            (row & 255) as u8,
            (row >> 8) as u8,
            (!row & 255) as u8,
            ((row >> 8) ^ 255) as u8,
            0,
        ]);

        b = (b + a) % 65521;
        while i < end {
            let u = rgba[i];
            bytes.push(u);
            a = (a + u as u32) % 65521;
            b = (b + a) % 65521;
            i += 1;
        }
        end += (row - 1) as usize;
    }

    bytes.extend_from_slice(&[
        (b >> 8) as u8,
        (b & 255) as u8,
        (a >> 8) as u8,
        (a & 255) as u8,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        73,
        69,
        78,
        68,
        174,
        66,
        96,
        130,
    ]);

    let ranges = [(12usize, 29usize), (37usize, 41 + idat as usize)];

    for (start, end_pos) in ranges {
        let mut c = !0u32;
        for &byte in &bytes[start..end_pos] {
            c ^= byte as u32;
            c = (c >> 4) ^ table[(c & 15) as usize];
            c = (c >> 4) ^ table[(c & 15) as usize];
        }
        c = !c;
        let mut end_idx = end_pos;
        bytes[end_idx] = (c >> 24) as u8;
        end_idx += 1;
        bytes[end_idx] = ((c >> 16) & 255) as u8;
        end_idx += 1;
        bytes[end_idx] = ((c >> 8) & 255) as u8;
        end_idx += 1;
        bytes[end_idx] = (c & 255) as u8;
    }

    bytes
}

/// Trait to render an image with an alt text.
pub trait RenderWithAlt {
    /// Render the image as an HTML `<img>` tag with the given alt text.
    fn render(&self, alt: &str) -> RenderedImage;
}

/// Newtype around a String representing a rendered image HTML tag.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenderedImage(String);

impl From<String> for RenderedImage {
    fn from(value: String) -> Self {
        RenderedImage(value)
    }
}

impl Display for RenderedImage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl RenderWithAlt for Image {
    fn render(&self, alt: &str) -> RenderedImage {
        let (width, height) = self.dimensions();

        // HACK: Only include width and height attributes if they are greater than 0
        // This is to workaround the fact that some unsupported image formats by `image` will return (0, 0)
        let width_attr = if width > 0 {
            format!(r#" width="{width}""#)
        } else {
            String::new()
        };

        let height_attr = if height > 0 {
            format!(r#" height="{height}""#)
        } else {
            String::new()
        };

        format!(
            r#"<img src="{src}"{width_attr}{height_attr} loading="lazy" decoding="async" alt="{alt}"/>"#,
            src = self.url,
            width_attr = width_attr,
            height_attr = height_attr,
            alt = alt
        ).into()
    }
}

#[cfg(test)]
mod tests {
    use crate::errors::AssetError;

    use super::*;
    use std::{error::Error, path::PathBuf};

    #[test]
    fn test_placeholder_with_missing_file() {
        let nonexistent_path = PathBuf::from("/this/file/does/not/exist.png");

        let result = get_placeholder(&nonexistent_path, None);

        assert!(result.is_err());
        if let Err(AssetError::ImageLoadFailed { path, .. }) = result {
            assert_eq!(path, nonexistent_path);
        } else {
            panic!("Expected ImageLoadFailed error");
        }
    }

    #[test]
    fn test_placeholder_with_valid_image() {
        let temp_dir = tempfile::tempdir().unwrap();
        let image_path = temp_dir.path().join("test.png");

        // Create a minimal valid 1x1 PNG file using the image crate to ensure correct CRCs
        let img = image::ImageBuffer::<image::Rgba<u8>, _>::from_fn(1, 1, |_x, _y| {
            image::Rgba([255, 0, 0, 255])
        });
        img.save(&image_path).unwrap();

        let result = get_placeholder(&image_path, None);

        if let Err(e) = &result {
            eprintln!("get_placeholder failed: {:?}", e.source());
        }

        assert!(result.is_ok());
        let placeholder = result.unwrap();
        assert!(!placeholder.thumbhash.is_empty());
        assert!(!placeholder.thumbhash_base64.is_empty());
    }
}