Skip to content
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

fixed: sprite user data positioned right after "Palette Chunk (0x2019)" leads to error while parsing aseprite file #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl ParseInfo {
})?;
layer.user_data = Some(user_data);
}
UserDataContext::OldPalette => {
UserDataContext::Palette => {
self.sprite_user_data = Some(user_data);
}
UserDataContext::TagIndex(tag_index) => {
Expand Down Expand Up @@ -299,6 +299,10 @@ fn parse_frame<R: Read>(
parse_info.color_profile = Some(profile);
}
ChunkType::Palette => {
// If the next chunk is user data, it is the sprite user data.
// See 3. at https://github.com/aseprite/aseprite/blob/v1.3.8/docs/ase-file-specs.md#user-data-chunk-0x2020
parse_info.user_data_context = Some(UserDataContext::Palette);

let palette = palette::parse_chunk(&data)?;
parse_info.palette = Some(Arc::new(palette));
}
Expand Down Expand Up @@ -335,7 +339,7 @@ fn parse_frame<R: Read>(
ChunkType::OldPalette04 => {
// An old palette chunk precedes the sprite UserData chunk.
// Update the chunk context to reflect the OldPalette chunk.
parse_info.user_data_context = Some(UserDataContext::OldPalette);
parse_info.user_data_context = Some(UserDataContext::Palette);

if parse_info.palette.is_none() {
let palette = palette::parse_old_chunk_04(&data)?;
Expand All @@ -345,7 +349,7 @@ fn parse_frame<R: Read>(
ChunkType::OldPalette11 => {
// An old palette chunk precedes the sprite UserData chunk.
// Update the chunk context to reflect the OldPalette chunk.
parse_info.user_data_context = Some(UserDataContext::OldPalette);
parse_info.user_data_context = Some(UserDataContext::Palette);

if parse_info.palette.is_none() {
let palette = palette::parse_old_chunk_11(&data)?;
Expand All @@ -369,7 +373,7 @@ fn parse_frame<R: Read>(
enum UserDataContext {
CelId(CelId),
LayerIndex(u32),
OldPalette,
Palette,
TagIndex(u16),
SliceIndex(u32),
}
Expand Down Expand Up @@ -428,6 +432,7 @@ impl Chunk {
let chunk_size = reader.dword()?;
let chunk_type_code = reader.word()?;
let chunk_type = parse_chunk_type(chunk_type_code)?;
// println!("Chunk type: {:?}", chunk_type);

check_chunk_bytes(chunk_size, *bytes_available)?;

Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,17 @@ fn gen_random_pixels() {
img.save(&Path::new("tests/data/random-256x256.png")).unwrap();
}
// */



#[test]
fn sprite_user_data_after_old_palette04() {
let ase = load_test_file("sprite_user_data_after_old_palette04");
assert_eq!(ase.sprite_user_data().unwrap().text.clone().unwrap(), "my custom sprite user data");
}

#[test]
fn sprite_user_data_after_palette() {
let ase = load_test_file("sprite_user_data_after_palette");
assert_eq!(ase.sprite_user_data().unwrap().text.clone().unwrap(), "my custom sprite user data");
}
Binary file not shown.
Binary file added tests/data/sprite_user_data_after_palette.aseprite
Binary file not shown.