Skip to content

Image crate conversion and unpacked image alpha bleeding #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ blake3 = "0.1.3"
env_logger = "0.7.0"
fs-err = "2.3.0"
globset = "0.4.4"
image = "0.23.7"
lazy_static = "1.4.0"
log = "0.4.8"
png = "0.15.3"
Expand Down
28 changes: 14 additions & 14 deletions src/alpha_bleed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use std::collections::VecDeque;

use crate::image::{Image, Pixel};
use image::{DynamicImage, GenericImage, GenericImageView, Rgba};

pub(crate) fn alpha_bleed(image: &mut Image) {
let (w, h) = image.size();
pub(crate) fn alpha_bleed(img: &mut DynamicImage) {
let (w, h) = img.dimensions();

// Tells whether a given position has been touched by the bleeding algorithm
// yet and is safe to sample colors from. In the first pass, we'll set all
Expand Down Expand Up @@ -45,9 +45,9 @@ pub(crate) fn alpha_bleed(image: &mut Image) {
// are valid to sample from.
for y in 0..h {
for x in 0..w {
let pixel = image.get_pixel((x, y));
let pixel = img.get_pixel(x, y);

if pixel.a != 0 {
if pixel[3] != 0 {
// This pixel is not totally transparent, so we don't need to
// modify it. We'll add it to the `can_be_sampled` set to
// indicate it's okay to sample from this pixel.
Expand All @@ -58,8 +58,8 @@ pub(crate) fn alpha_bleed(image: &mut Image) {

// Check if any adjacent pixels have non-zero alpha.
let borders_opaque = adjacent_positions(x, y).any(|(x_source, y_source)| {
let source = image.get_pixel((x_source, y_source));
source.a != 0
let source = img.get_pixel(x_source, y_source);
source[3] != 0
});

if borders_opaque {
Expand All @@ -80,26 +80,26 @@ pub(crate) fn alpha_bleed(image: &mut Image) {

for (x_source, y_source) in adjacent_positions(x, y) {
if can_be_sampled.get(x_source, y_source) {
let source = image.get_pixel((x_source, y_source));
let source = img.get_pixel(x_source, y_source);

contributing += 1;
new_color.0 += source.r as u16;
new_color.1 += source.g as u16;
new_color.2 += source.b as u16;
new_color.0 += source[0] as u16;
new_color.1 += source[1] as u16;
new_color.2 += source[2] as u16;
} else if !visited.get(x_source, y_source) {
visited.set(x_source, y_source);
to_visit.push_back((x_source, y_source));
}
}

let new_color = Pixel::new(
let pixel = Rgba([
(new_color.0 / contributing) as u8,
(new_color.1 / contributing) as u8,
(new_color.2 / contributing) as u8,
0,
);
]);

image.set_pixel((x, y), new_color);
img.put_pixel(x, y, pixel);

// Now that we've bled this pixel, it's eligible to be sampled from for
// future iterations.
Expand Down
58 changes: 46 additions & 12 deletions src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use packos::{InputItem, SimplePacker};
use thiserror::Error;
use walkdir::WalkDir;

use image::{imageops, png::PNGEncoder, DynamicImage, GenericImageView, ImageError};

use crate::{
alpha_bleed::alpha_bleed,
asset_name::AssetName,
auth_cookie::get_auth_cookie,
codegen::perform_codegen,
data::{Config, ConfigError, ImageSlice, InputManifest, Manifest, ManifestError, SyncInput},
dpi_scale,
image::Image,
options::{GlobalOptions, SyncOptions, SyncTarget},
roblox_web_api::{RobloxApiClient, RobloxApiError},
sync_backend::{
Expand Down Expand Up @@ -101,7 +102,7 @@ struct InputKind {
}

struct PackedImage {
image: Image,
img: DynamicImage,
slices: HashMap<AssetName, ImageSlice>,
}

Expand Down Expand Up @@ -352,7 +353,7 @@ impl SyncSession {
for (i, packed_image) in packed_images.iter_mut().enumerate() {
log::trace!("Bleeding image {}", i);

alpha_bleed(&mut packed_image.image);
alpha_bleed(&mut packed_image.img);
}

log::trace!("Syncing packed images...");
Expand Down Expand Up @@ -393,9 +394,9 @@ impl SyncSession {

for name in group {
let input = &self.inputs[&name];
let image = Image::decode_png(input.contents.as_slice())?;
let image = image::load_from_memory(input.contents.as_slice())?;

let input = InputItem::new(image.size());
let input = InputItem::new(image.dimensions());

images_by_id.insert(input.id(), (name, image));
packos_inputs.push(input);
Expand All @@ -409,19 +410,21 @@ impl SyncSession {
let mut packed_images = Vec::new();

for bucket in pack_results.buckets() {
let mut image = Image::new_empty_rgba8(bucket.size());
let (width, height) = bucket.size();
let mut img = DynamicImage::new_rgba8(width, height);
let mut slices: HashMap<AssetName, _> = HashMap::new();

for item in bucket.items() {
let (name, sprite_image) = &images_by_id[&item.id()];
let (x, y) = item.position();

image.blit(sprite_image, item.position());
imageops::overlay(&mut img, sprite_image, x, y);

let slice = ImageSlice::new(item.position(), item.max());
slices.insert((*name).clone(), slice);
}

packed_images.push(PackedImage { image, slices });
packed_images.push(PackedImage { img, slices });
}

Ok(packed_images)
Expand All @@ -432,14 +435,25 @@ impl SyncSession {
backend: &mut S,
packed_image: &PackedImage,
) -> Result<(), SyncError> {
let mut encoded_image = Vec::new();
packed_image.image.encode_png(&mut encoded_image)?;
let mut encoded_image: Vec<u8> = Vec::new();

let (width, height) = packed_image.img.dimensions();

let encoder = PNGEncoder::new(&mut encoded_image);
encoder
.encode(
&packed_image.img.to_bytes(),
width,
height,
packed_image.img.color(),
)
.unwrap();

let hash = generate_asset_hash(&encoded_image);

let upload_data = UploadInfo {
name: "spritesheet".to_owned(),
contents: encoded_image,
contents: encoded_image.to_vec(),
hash: hash.clone(),
};

Expand All @@ -463,9 +477,23 @@ impl SyncSession {
) -> Result<(), SyncError> {
let input = self.inputs.get_mut(input_name).unwrap();

let mut img = image::load_from_memory(input.contents.as_slice())?;

img.save("test.png").unwrap();

alpha_bleed(&mut img);

let (width, height) = img.dimensions();

let mut encoded_image: Vec<u8> = Vec::new();
let encoder = PNGEncoder::new(&mut encoded_image);
encoder
.encode(&img.to_bytes(), width, height, img.color())
.unwrap();

let upload_data = UploadInfo {
name: input.human_name(),
contents: input.contents.clone(),
contents: encoded_image.to_vec(),
hash: input.hash.clone(),
};

Expand Down Expand Up @@ -708,6 +736,12 @@ pub enum SyncError {
source: ConfigError,
},

#[error(transparent)]
Image {
#[from]
source: ImageError,
},

#[error(transparent)]
Backend {
#[from]
Expand Down
Loading