change to png only process

This commit is contained in:
olymrifki 2025-07-03 18:40:44 +07:00
parent a546389caa
commit ddfac89ac4
3 changed files with 293 additions and 656 deletions

833
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,4 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
photon-rs = "0.3.3" png = "0.17.16"
tokio = { version = "1.46.0", features = ["full"] }
fast_image_resize = "5.1.4"

View file

@ -1,46 +1,75 @@
use photon_rs::{ use fast_image_resize as fr;
PhotonImage, multiple::watermark, native::open_image, native::save_image, transform::resize, use fr::{PixelType, ResizeAlg, ResizeOptions, Resizer, images::Image};
}; use std::io::Cursor;
use tokio::fs;
fn main() { #[tokio::main]
async fn main() {
let start = std::time::Instant::now();
let base_path = "/mnt/c/Users/Formulatrix/Documents/Explorations/WebApplication1/tiles1705/"; let base_path = "/mnt/c/Users/Formulatrix/Documents/Explorations/WebApplication1/tiles1705/";
const SCALE: f64 = 0.01; const SCALE: f64 = 0.3;
const SIZE: u32 = 720; const SIZE: u32 = 720;
let actual_size = (SIZE as f64 * SCALE).trunc() as u32; let actual_size = (SIZE as f64 * SCALE).trunc() as u32;
// Open the image (a PhotonImage is returned) // Open the image (a PhotonImage is returned)
// let max_col = 30;
// let max_row = 30;
let max_col = 31; let max_col = 31;
let max_row = 55; let max_row = 55;
let mut res = new_empty_image(actual_size * max_row, actual_size * max_col); let canvas_width = (max_row * actual_size) as usize;
let canvas_height = (max_col * actual_size) as usize;
let mut canvas = vec![0u8; canvas_width * canvas_height * 3];
let mut resizer = Resizer::new();
for i in 1..=max_col { for i in 1..=max_col {
for j in 1..=max_row { for j in 1..=max_row {
let filename = format!("{}{}{}.png", base_path, number_to_column(i), j); let filename = format!("{}{}{}.png", base_path, number_to_column(i), j);
// println!("{filename}"); // println!("{filename}");
let pre = open_image(filename).expect("File should open"); let data = Cursor::new(fs::read(filename).await.unwrap()); // .unwrap();
let img = resize( let decoder = png::Decoder::new(data);
&pre, let mut reader = decoder.read_info().unwrap();
actual_size, let mut buf = vec![0; reader.output_buffer_size()];
actual_size, let _info = reader.next_frame(&mut buf).unwrap();
photon_rs::transform::SamplingFilter::Nearest,
);
watermark(
&mut res,
&img,
actual_size as i64 * j as i64,
actual_size as i64 * i as i64,
);
}
}
// Apply a sepia effect to the image. let src_img = Image::from_vec_u8(SIZE, SIZE, buf, PixelType::U8x3).unwrap();
save_image(res, "raw_image.png").expect("File should be saved"); let mut img = Image::new(actual_size, actual_size, PixelType::U8x3);
}
pub fn new_empty_image(width: u32, height: u32) -> PhotonImage { resizer
// 4 bytes per pixel (RGBA) .resize(
let num_pixels = (width * height * 4) as usize; &src_img,
let raw_pixels = vec![0u8; num_pixels]; &mut img,
PhotonImage::new(raw_pixels, width, height) &ResizeOptions::new()
.resize_alg(ResizeAlg::Nearest)
.use_alpha(false),
)
.unwrap();
place_image(
&mut canvas,
canvas_width as usize,
canvas_height as usize,
&img.into_vec(),
(actual_size) as usize,
(actual_size) as usize,
(j * actual_size) as usize,
(i * actual_size) as usize,
);
}
}
let file = std::fs::File::create("raw_image.png").unwrap();
let writer = std::io::BufWriter::new(file);
let mut encoder = png::Encoder::new(
writer,
canvas_width.try_into().unwrap(),
canvas_height.try_into().unwrap(),
);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let mut png_writer = encoder.write_header().unwrap();
png_writer.write_image_data(&canvas).unwrap();
let duration = start.elapsed();
println!("Operation completed in: {:.2?}", duration);
} }
fn number_to_column(mut num: u32) -> String { fn number_to_column(mut num: u32) -> String {
let mut result = String::new(); let mut result = String::new();
@ -54,3 +83,32 @@ fn number_to_column(mut num: u32) -> String {
result result
} }
fn place_image(
canvas: &mut [u8],
canvas_width: usize,
canvas_height: usize,
image: &[u8],
image_width: usize,
image_height: usize,
x_offset: usize,
y_offset: usize,
) {
let channels = 3;
for row in 0..image_height {
let canvas_y = y_offset + row;
if canvas_y >= canvas_height {
continue;
}
let canvas_start = (canvas_y * canvas_width + x_offset) * channels;
let canvas_end = canvas_start + image_width * channels;
let image_start = row * image_width * channels;
let image_end = image_start + image_width * channels;
if canvas_end <= canvas.len() && image_end <= image.len() {
canvas[canvas_start..canvas_end].copy_from_slice(&image[image_start..image_end]);
}
}
}