core, viewer: add support for WebP output

WebP can be selected by passing `--image-format webp` on the command
line. For typical Minecraft worlds, this results in a size reduction of
10-15% without increasing processing time.
This commit is contained in:
Matthias Schiffer 2025-01-11 01:24:58 +01:00
parent bb11b29e92
commit c23b53a8c3
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
11 changed files with 81 additions and 11 deletions

View file

@ -7,6 +7,7 @@ use std::{
};
use anyhow::{Context, Result};
use clap::ValueEnum;
use indexmap::IndexSet;
use regex::{Regex, RegexSet};
use serde::{Deserialize, Serialize};
@ -150,6 +151,8 @@ pub struct Config {
pub viewer_info_path: PathBuf,
/// Path of viewer entities file
pub viewer_entities_path: PathBuf,
/// Format of generated map tiles
pub image_format: ImageFormat,
/// Sign text filter patterns
pub sign_patterns: RegexSet,
/// Sign text transformation pattern
@ -189,6 +192,7 @@ impl Config {
entities_path_final,
viewer_info_path,
viewer_entities_path,
image_format: args.image_format,
sign_patterns,
sign_transforms,
})
@ -264,14 +268,39 @@ impl Config {
[&self.output_dir, Path::new(&dir)].iter().collect()
}
/// Returns the file extension for the configured image format
pub fn tile_extension(&self) -> &'static str {
match self.image_format {
ImageFormat::Png => "png",
ImageFormat::Webp => "webp",
}
}
/// Returns the configurured image format for the image library
pub fn tile_image_format(&self) -> image::ImageFormat {
match self.image_format {
ImageFormat::Png => image::ImageFormat::Png,
ImageFormat::Webp => image::ImageFormat::WebP,
}
}
/// Constructs the path of an output tile image
pub fn tile_path(&self, kind: TileKind, level: usize, coords: TileCoords) -> PathBuf {
let filename = coord_filename(coords, "png");
let filename = coord_filename(coords, self.tile_extension());
let dir = self.tile_dir(kind, level);
[Path::new(&dir), Path::new(&filename)].iter().collect()
}
}
/// Format of generated map tiles
#[derive(Debug, Clone, Copy, Default, ValueEnum)]
pub enum ImageFormat {
/// Generate PNG images
#[default]
Png,
/// Generate WebP images
Webp,
}
/// Copies a chunk image into a region tile
pub fn overlay_chunk<I, J>(image: &mut I, chunk: &J, coords: ChunkCoords)
where

View file

@ -61,6 +61,8 @@ struct Metadata<'t> {
spawn: Spawn,
/// Enabled MinedMap features
features: Features,
/// Format of generated map tiles
tile_extension: &'static str,
}
/// Viewer entity JSON data structure
@ -205,6 +207,7 @@ impl<'a> MetadataWriter<'a> {
mipmaps: Vec::new(),
spawn: Self::spawn(&level_dat),
features,
tile_extension: self.config.tile_extension(),
};
for tile_map in self.tiles.iter() {

View file

@ -16,7 +16,7 @@ use anyhow::{Context, Result};
use clap::Parser;
use git_version::git_version;
use common::Config;
use common::{Config, ImageFormat};
use metadata_writer::MetadataWriter;
use region_processor::RegionProcessor;
use tile_mipmapper::TileMipmapper;
@ -47,6 +47,9 @@ pub struct Args {
/// Enable verbose messages
#[arg(short, long)]
pub verbose: bool,
/// Format of generated map tiles
#[arg(long, value_enum, default_value_t)]
pub image_format: ImageFormat,
/// Prefix for text of signs to show on the map
#[arg(long)]
pub sign_prefix: Vec<String>,

View file

@ -79,6 +79,8 @@ struct SingleRegionProcessor<'a> {
lightmap: image::GrayAlphaImage,
/// Processed entity intermediate data
entities: ProcessedEntities,
/// Format of generated map tiles
image_format: image::ImageFormat,
/// True if any unknown block or biome types were encountered during processing
has_unknown: bool,
}
@ -127,6 +129,7 @@ impl<'a> SingleRegionProcessor<'a> {
processed_region,
lightmap,
entities,
image_format: processor.config.tile_image_format(),
has_unknown: false,
})
}
@ -179,7 +182,7 @@ impl<'a> SingleRegionProcessor<'a> {
self.input_timestamp,
|file| {
self.lightmap
.write_to(file, image::ImageFormat::Png)
.write_to(file, self.image_format)
.context("Failed to save image")
},
)

View file

@ -144,7 +144,7 @@ where
}
image
.write_to(file, image::ImageFormat::Png)
.write_to(file, self.config.tile_image_format())
.context("Failed to save image")
}
}

View file

@ -304,7 +304,7 @@ impl<'a> TileRenderer<'a> {
processed_timestamp,
|file| {
image
.write_to(file, image::ImageFormat::Png)
.write_to(file, self.config.tile_image_format())
.context("Failed to save image")
},
)?;