paralellize image generator
This commit is contained in:
parent
7faafcdda9
commit
0a72d5a203
@ -22,6 +22,7 @@ tracing = "0.1"
|
|||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
image = "0.25.2"
|
image = "0.25.2"
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
|
rayon = "1.10.0"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
rustflags = ["-Z", "threads=8"]
|
rustflags = ["-Z", "threads=8"]
|
||||||
|
@ -31,6 +31,7 @@ async fn main() {
|
|||||||
let app = router::get_router()
|
let app = router::get_router()
|
||||||
.nest_service("/styles", ServeDir::new("styles"))
|
.nest_service("/styles", ServeDir::new("styles"))
|
||||||
.nest_service("/images", ServeDir::new("../static/images"))
|
.nest_service("/images", ServeDir::new("../static/images"))
|
||||||
|
.nest_service("/generated_images", ServeDir::new("generated_images"))
|
||||||
.nest_service("/svg", ServeDir::new("../static/svg"))
|
.nest_service("/svg", ServeDir::new("../static/svg"))
|
||||||
.nest_service(
|
.nest_service(
|
||||||
"/config.yml",
|
"/config.yml",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use image::ImageFormat;
|
use image::ImageFormat;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
pub enum ExportFormat {
|
pub enum ExportFormat {
|
||||||
Jpeg,
|
Jpeg,
|
||||||
Avif,
|
Avif,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::{fs::create_dir_all, path::Path};
|
use std::{fs::create_dir_all, path::Path};
|
||||||
|
|
||||||
use image::{imageops::FilterType, DynamicImage};
|
use image::{imageops::FilterType, DynamicImage};
|
||||||
|
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use super::export_format::ExportFormat;
|
use super::export_format::ExportFormat;
|
||||||
@ -11,8 +12,8 @@ pub fn generate_images(
|
|||||||
resolutions: &[(u32, u32, f32)],
|
resolutions: &[(u32, u32, f32)],
|
||||||
formats: &[ExportFormat],
|
formats: &[ExportFormat],
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
formats.iter().for_each(|format| {
|
formats.par_iter().for_each(|format| {
|
||||||
resolutions.iter().for_each(|resolution| {
|
resolutions.par_iter().for_each(|resolution| {
|
||||||
let (width, height, _) = *resolution;
|
let (width, height, _) = *resolution;
|
||||||
// let image = image.clone();
|
// let image = image.clone();
|
||||||
let resized = image.resize_to_fill(width, height, FilterType::Triangle);
|
let resized = image.resize_to_fill(width, height, FilterType::Triangle);
|
||||||
|
@ -2,6 +2,7 @@ use std::{
|
|||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::FromStr as _,
|
str::FromStr as _,
|
||||||
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
@ -30,13 +31,32 @@ pub fn generate_picture_markup(
|
|||||||
let resolutions = get_resolutions(orig_img_dimensions, width, height);
|
let resolutions = get_resolutions(orig_img_dimensions, width, height);
|
||||||
|
|
||||||
// TODO lets generate images
|
// TODO lets generate images
|
||||||
generate_images(
|
|
||||||
&orig_img,
|
let orig_img_arc = Arc::new(orig_img);
|
||||||
&path_to_generated,
|
let orig_img_clone = Arc::clone(&orig_img_arc);
|
||||||
&resolutions,
|
let path_to_generated_arc = Arc::new(path_to_generated);
|
||||||
&exported_formats,
|
let path_to_generated_clone = Arc::clone(&path_to_generated_arc);
|
||||||
)
|
let resolutions_arc = Arc::new(resolutions);
|
||||||
.with_context(|| "Failed to generate images".to_string())?;
|
let resolutions_clone = Arc::clone(&resolutions_arc);
|
||||||
|
let exported_formats_arc = Arc::new(exported_formats);
|
||||||
|
let exported_formats_clone = Arc::clone(&exported_formats_arc);
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let orig_img = orig_img_clone.as_ref();
|
||||||
|
let path_to_generated = path_to_generated_clone.as_ref();
|
||||||
|
let resolutions = resolutions_clone.as_ref();
|
||||||
|
let exported_formats = exported_formats_clone.as_ref();
|
||||||
|
|
||||||
|
let result = generate_images(orig_img, path_to_generated, resolutions, exported_formats)
|
||||||
|
.with_context(|| "Failed to generate images".to_string());
|
||||||
|
if let Err(e) = result {
|
||||||
|
tracing::error!("Error: {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let exported_formats = Arc::clone(&exported_formats_arc);
|
||||||
|
let path_to_generated = Arc::clone(&path_to_generated_arc);
|
||||||
|
let resolutions = Arc::clone(&resolutions_arc);
|
||||||
|
|
||||||
let source_tags = exported_formats
|
let source_tags = exported_formats
|
||||||
.iter()
|
.iter()
|
||||||
|
Loading…
Reference in New Issue
Block a user