Skip to main content

gcrecomp_runtime/texture/
upscaler.rs

1// Texture upscaling
2use anyhow::Result;
3use image::{imageops, RgbaImage};
4
5pub struct TextureUpscaler {
6    algorithm: UpscaleAlgorithm,
7}
8
9#[derive(Debug, Clone, Copy)]
10pub enum UpscaleAlgorithm {
11    Nearest,
12    Linear,
13    Bicubic,
14    Lanczos3,
15}
16
17impl TextureUpscaler {
18    pub fn new() -> Self {
19        Self {
20            algorithm: UpscaleAlgorithm::Lanczos3,
21        }
22    }
23
24    pub fn set_algorithm(&mut self, algorithm: UpscaleAlgorithm) {
25        self.algorithm = algorithm;
26    }
27
28    pub fn upscale(&self, image: &RgbaImage, factor: f32) -> Result<RgbaImage> {
29        let new_width = (image.width() as f32 * factor) as u32;
30        let new_height = (image.height() as f32 * factor) as u32;
31
32        let upscaled = match self.algorithm {
33            UpscaleAlgorithm::Nearest => {
34                imageops::resize(image, new_width, new_height, imageops::FilterType::Nearest)
35            }
36            UpscaleAlgorithm::Linear => {
37                imageops::resize(image, new_width, new_height, imageops::FilterType::Triangle)
38            }
39            UpscaleAlgorithm::Bicubic => imageops::resize(
40                image,
41                new_width,
42                new_height,
43                imageops::FilterType::CatmullRom,
44            ),
45            UpscaleAlgorithm::Lanczos3 => {
46                imageops::resize(image, new_width, new_height, imageops::FilterType::Lanczos3)
47            }
48        };
49
50        Ok(upscaled)
51    }
52}