Skip to content

Commit

Permalink
Add 'undo_alphablend' function to generate transparent .pngs from alp…
Browse files Browse the repository at this point in the history
…hablend format images.
  • Loading branch information
drojf committed Jul 27, 2018
1 parent cf1b265 commit 4125f5e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
56 changes: 33 additions & 23 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions src/alphablend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@ use std::fs;

//nonstandard includes
use image;
use image::{RgbImage};
use image::{RgbImage, RgbaImage};
use walkdir::WalkDir;

fn convert_alphablend_to_transparent_png(filepath : &str, save_path : &str)
{
let img_dyn = image::open(filepath).unwrap();
let img = img_dyn.to_rgba(); //I'm not sure if you can use 'as_rgba8' for 'rgb' images, so just use 'to_rgba8'

//create new image whose size is half the width of the original image, with a proper alpha channel
let transparent_png_width = img.width() / 2;
let mut transparent_png = RgbaImage::new(transparent_png_width, img.height());

for (x, y, pixel) in transparent_png.enumerate_pixels_mut()
{
let color_pixel = img.get_pixel(x,y);
let alpha_pixel = img.get_pixel(x + transparent_png_width, y);

*pixel = image::Rgba( [color_pixel[0], color_pixel[1], color_pixel[2], 0xFF - alpha_pixel[0]] );
}

transparent_png.save(save_path).unwrap();
}

fn convert_to_onscripter_alphablend(filepath : &str, save_path : &str)
{
let img_dyn = image::open(filepath).unwrap();
Expand All @@ -33,7 +53,7 @@ fn convert_to_onscripter_alphablend(filepath : &str, save_path : &str)
}

//TODO: output to 'output_dir' instead of same directory.
pub fn convert_folder_to_alphablend() -> u32
pub fn convert_folder_to_alphablend(reverse : bool) -> u32
{
let mut count = 0;
let recursive_path_iter = WalkDir::new("input_images");
Expand Down Expand Up @@ -62,7 +82,12 @@ pub fn convert_folder_to_alphablend() -> u32
//create the save directory if it doesn't already exist:
fs::create_dir_all(output_path.parent().unwrap()).unwrap();

convert_to_onscripter_alphablend(ent.path().to_str().unwrap(), &save_path);
if reverse{
convert_alphablend_to_transparent_png(ent.path().to_str().unwrap(), &save_path);
}
else {
convert_to_onscripter_alphablend(ent.path().to_str().unwrap(), &save_path);
}

count += 1;
}
Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ fn do_verify(input_folder: &str, output_folder: &str)
}
}

fn do_alphablend()
fn do_alphablend(reverse : bool)
{
let num_converted = convert_folder_to_alphablend();
let num_converted = convert_folder_to_alphablend(reverse);
if num_converted == 0
{
println!("Please place .png files/folders in the 'input_images' directory. They will be converted and placed in the 'output_images' directory.");
Expand All @@ -85,7 +85,7 @@ fn do_alphablend()
fn print_description_and_exit() -> !
{
println!("\n------------------------------- Usage Instructions -------------------------------------");
println!("spritezip [compress|extract [0|1|2|3|4|5|6]|verify|selftest|alphablend]");
println!("spritezip [compress|extract [0|1|2|3|4|5|6]|verify|selftest|alphablend|undo_alphablend]");
println!("If you use 'spritezip extract' by itself, .png files are not optimized");
println!("Specifying a number (2 is recommended) will cause oxipng to optimize the .png files before saving them.");
println!("For example 'spritezip extract 2' will use level 2 compression (where 0 is fast and largest size, 6 is extremely slow and smallest size)");
Expand Down Expand Up @@ -183,7 +183,10 @@ fn main()
do_verify(input_folder, output_folder);
},
Some("alphablend") => {
do_alphablend();
do_alphablend(false);
},
Some("undo_alphablend") => {
do_alphablend(true);
},
Some(_) => {
print_description_and_exit();
Expand Down

0 comments on commit 4125f5e

Please sign in to comment.