Skip to content

Commit

Permalink
Optimize png data
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Jan 18, 2024
1 parent fccb912 commit 641a5e0
Show file tree
Hide file tree
Showing 61 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions create-data/Cargo.toml
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
image = { version = "0.24.8", default-features = false, features = ["png"] }
oxipng = "9.0.0"
resvg = "0.37.0"
69 changes: 49 additions & 20 deletions create-data/src/main.rs
@@ -1,9 +1,11 @@
use image::imageops::FilterType;
use image::{DynamicImage, ImageBuffer};
use image::codecs::png::PngEncoder;
use image::imageops::{self, FilterType};
use image::RgbaImage;
use oxipng::{Options, StripChunks};
use resvg::usvg::{TreeParsing, TreeTextToPath};
use resvg::{tiny_skia, usvg, Tree};
use std::fs::{create_dir_all, File};
use std::io::Read;
use std::io::{BufWriter, Cursor, Read, Write};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -28,19 +30,25 @@ fn process_board(filenames: &[&str]) -> Result<(), Box<dyn std::error::Error>> {
if let Some(mut pixmap) = tiny_skia::Pixmap::new(size.width(), size.height()) {
tree.render(Default::default(), &mut pixmap.as_mut());
// convert to image::ImageBuffer
if let Some(image) =
ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take())
if let Some(image) = RgbaImage::from_raw(pixmap.width(), pixmap.height(), pixmap.take())
{
// resize to 527:572 with Lanczos3 filter
// and write to png file
DynamicImage::ImageRgba8(image)
.resize_exact(527, 572, FilterType::Lanczos3)
.save(outdir.join(format!("{}.png",
filename
.split('_')
.next()
.expect("filename should have `_`")
)))?;
let path = outdir.join(format!(
"{}.png",
filename
.split('_')
.next()
.expect("filename should have `_`")
));
let mut cursor = Cursor::new(Vec::new());
imageops::resize(&image, 527, 572, FilterType::Lanczos3)
.write_with_encoder(PngEncoder::new(BufWriter::new(&mut cursor)))?;
let mut file = File::create(path)?;
file.write_all(&oxipng::optimize_from_memory(
cursor.get_ref(),
&oxipng_options(),
)?)?;
}
}
}
Expand All @@ -66,26 +74,47 @@ fn process_pieces(names: &[&str]) -> Result<(), Box<dyn std::error::Error>> {
if let Some(mut pixmap) = tiny_skia::Pixmap::new(size.width(), size.height()) {
tree.render(Default::default(), &mut pixmap.as_mut());
// convert to image::ImageBuffer
if let Some(image) =
ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take())
if let Some(image) = RgbaImage::from_raw(pixmap.width(), pixmap.height(), pixmap.take())
{
let (width, height) = image.dimensions();
let image = DynamicImage::ImageRgba8(image);
// crop to width/8:height/4 and
// resize to 53:56 with Lanczos3 filter
for i in 0..4 {
for j in 0..8 {
if (i % 2 == 0 && j == 0) || (i % 2 == 1 && j == 3) {
continue;
}
image
.crop_imm(width * j / 8, height * i / 4, width / 8, height / 4)
.resize(53, 56, FilterType::Lanczos3)
.save(outdir.join(name).join(format!("{i}{j}.png")))?;
let mut cursor = Cursor::new(Vec::new());
imageops::resize(
&imageops::crop_imm(
&image,
width * j / 8,
height * i / 4,
width / 8,
height / 4,
)
.to_image(),
53,
56,
FilterType::Lanczos3,
)
.write_with_encoder(PngEncoder::new(BufWriter::new(&mut cursor)))?;
let mut file = File::create(outdir.join(name).join(format!("{i}{j}.png")))?;
file.write_all(&oxipng::optimize_from_memory(
cursor.get_ref(),
&oxipng_options(),
)?)?;
}
}
}
}
}
Ok(())
}

fn oxipng_options() -> Options {
let mut opt = Options::from_preset(4);
opt.strip = StripChunks::Safe;
opt.optimize_alpha = true;
opt
}
Binary file modified shogi-img/src/data/board/light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/board/resin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/board/warm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/02.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/03.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/04.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/05.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/06.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/07.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/14.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/15.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/17.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/21.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/22.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/23.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/24.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/25.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/26.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/27.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/30.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified shogi-img/src/data/pieces/hitomoji/31.png
Binary file modified shogi-img/src/data/pieces/hitomoji/32.png
Binary file modified shogi-img/src/data/pieces/hitomoji/34.png
Binary file modified shogi-img/src/data/pieces/hitomoji/35.png
Binary file modified shogi-img/src/data/pieces/hitomoji/36.png
Binary file modified shogi-img/src/data/pieces/hitomoji/37.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/01.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/02.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/03.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/04.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/05.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/06.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/07.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/10.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/11.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/12.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/14.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/15.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/16.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/17.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/21.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/22.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/23.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/24.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/25.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/26.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/27.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/30.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/31.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/32.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/34.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/35.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/36.png
Binary file modified shogi-img/src/data/pieces/hitomoji_gothic/37.png

0 comments on commit 641a5e0

Please sign in to comment.