Skip to content

Commit 7aaced0

Browse files
committed
v4.0.3
1 parent 8bded7c commit 7aaced0

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.11)
22
project (raylib_aseprite
3-
VERSION 4.0.2
3+
VERSION 4.0.3
44
DESCRIPTION "raylib_aseprite: Use Aseprite files in raylib"
55
HOMEPAGE_URL "https://github.com/robloach/raylib-aseprite"
66
LANGUAGES C)

include/cute_aseprite.h

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Licensing information can be found at the end of the file.
44
------------------------------------------------------------------------------
55
6-
cute_aseprite.h - v1.01
6+
cute_aseprite.h - v1.02
77
88
To create implementation (the function definitions)
99
#define CUTE_ASEPRITE_IMPLEMENTATION
@@ -43,6 +43,8 @@
4343
Revision history:
4444
1.00 (08/25/2020) initial release
4545
1.01 (08/31/2020) fixed memleaks, tag parsing bug (crash), blend bugs
46+
1.02 (02/05/2022) fixed icc profile parse bug, support transparent pal-
47+
ette index, can parse 1.3 files (no tileset support)
4648
*/
4749

4850
/*
@@ -102,7 +104,7 @@ void cute_aseprite_free(ase_t* aseprite);
102104

103105
#define CUTE_ASEPRITE_MAX_LAYERS (64)
104106
#define CUTE_ASEPRITE_MAX_SLICES (128)
105-
#define CUTE_ASEPRITE_MAX_PALETTE_ENTRIES (256)
107+
#define CUTE_ASEPRITE_MAX_PALETTE_ENTRIES (1024)
106108
#define CUTE_ASEPRITE_MAX_TAGS (256)
107109

108110
#include <stdint.h>
@@ -162,7 +164,7 @@ struct ase_layer_t
162164
ase_layer_flags_t flags;
163165
ase_layer_type_t type;
164166
const char* name;
165-
int child_level;
167+
ase_layer_t* parent;
166168
float opacity;
167169
ase_udata_t udata;
168170
};
@@ -212,6 +214,7 @@ struct ase_tag_t
212214
ase_animation_direction_t loop_animation_direction;
213215
uint8_t r, g, b;
214216
const char* name;
217+
ase_udata_t udata;
215218
};
216219

217220
struct ase_slice_t
@@ -852,7 +855,8 @@ static ase_color_t s_blend(ase_color_t src, ase_color_t dst, uint8_t opacity)
852855
g = dst.g + (src.g - dst.g) * src.a / a;
853856
b = dst.b + (src.b - dst.b) * src.a / a;
854857
}
855-
return (ase_color_t) { (uint8_t)r, (uint8_t)g, (uint8_t)b, (uint8_t)a };
858+
ase_color_t ret = { (uint8_t)r, (uint8_t)g, (uint8_t)b, (uint8_t)a };
859+
return ret;
856860
}
857861

858862
static int s_min(int a, int b)
@@ -878,7 +882,14 @@ static ase_color_t s_color(ase_t* ase, void* src, int index)
878882
} else {
879883
CUTE_ASEPRITE_ASSERT(ase->mode == ASE_MODE_INDEXED);
880884
uint8_t palette_index = ((uint8_t*)src)[index];
881-
result = ase->palette.entries[palette_index].color;
885+
if (palette_index == ase->transparent_palette_entry_index) {
886+
result.r = 0;
887+
result.g = 0;
888+
result.b = 0;
889+
result.a = 0;
890+
} else {
891+
result = ase->palette.entries[palette_index].color;
892+
}
882893
}
883894
return result;
884895
}
@@ -926,6 +937,10 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
926937
CUTE_ASEPRITE_MEMSET(ase->frames, 0, sizeof(ase_frame_t) * (size_t)ase->frame_count);
927938

928939
ase_udata_t* last_udata = NULL;
940+
int was_on_tags = 0;
941+
int tag_index = 0;
942+
943+
ase_layer_t* layer_stack[CUTE_ASEPRITE_MAX_LAYERS];
929944

930945
// Parse all chunks in the .aseprite file.
931946
for (int i = 0; i < ase->frame_count; ++i) {
@@ -954,7 +969,12 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
954969
ase_layer_t* layer = ase->layers + ase->layer_count++;
955970
layer->flags = (ase_layer_flags_t)s_read_uint16(s);
956971
layer->type = (ase_layer_type_t)s_read_uint16(s);
957-
layer->child_level = (int)s_read_uint16(s);
972+
layer->parent = NULL;
973+
int child_level = (int)s_read_uint16(s);
974+
layer_stack[child_level] = layer;
975+
if (child_level) {
976+
layer->parent = layer_stack[child_level - 1];
977+
}
958978
s_skip(s, sizeof(uint16_t)); // Default layer width in pixels (ignored).
959979
s_skip(s, sizeof(uint16_t)); // Default layer height in pixels (ignored).
960980
int blend_mode = (int)s_read_uint16(s);
@@ -1037,6 +1057,7 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
10371057
ase->color_profile.icc_profile_data_length = s_read_uint32(s);
10381058
ase->color_profile.icc_profile_data = CUTE_ASEPRITE_ALLOC(ase->color_profile.icc_profile_data_length, mem_ctx);
10391059
CUTE_ASEPRITE_MEMCPY(ase->color_profile.icc_profile_data, s->in, ase->color_profile.icc_profile_data_length);
1060+
s->in += ase->color_profile.icc_profile_data_length;
10401061
}
10411062
} break;
10421063

@@ -1058,11 +1079,13 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
10581079
tag.name = s_read_string(s);
10591080
ase->tags[k] = tag;
10601081
}
1082+
was_on_tags = 1;
10611083
} break;
10621084

10631085
case 0x2019: // Palette chunk.
10641086
{
10651087
ase->palette.entry_count = (int)s_read_uint32(s);
1088+
CUTE_ASEPRITE_ASSERT(ase->palette.entry_count <= CUTE_ASEPRITE_MAX_PALETTE_ENTRIES);
10661089
int first_index = (int)s_read_uint32(s);
10671090
int last_index = (int)s_read_uint32(s);
10681091
s_skip(s, 8); // For future (set to zero).
@@ -1078,13 +1101,18 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
10781101
} else {
10791102
entry.color_name = NULL;
10801103
}
1104+
CUTE_ASEPRITE_ASSERT(k < CUTE_ASEPRITE_MAX_PALETTE_ENTRIES);
10811105
ase->palette.entries[k] = entry;
10821106
}
10831107
} break;
10841108

10851109
case 0x2020: // Udata chunk.
10861110
{
1087-
CUTE_ASEPRITE_ASSERT(last_udata);
1111+
CUTE_ASEPRITE_ASSERT(last_udata || was_on_tags);
1112+
if (was_on_tags && !last_udata) {
1113+
CUTE_ASEPRITE_ASSERT(tag_index < ase->tag_count);
1114+
last_udata = &ase->tags[tag_index++].udata;
1115+
}
10881116
int flags = (int)s_read_uint32(s);
10891117
if (flags & 1) {
10901118
last_udata->has_text = 1;
@@ -1136,6 +1164,9 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
11361164
s_skip(s, (int)chunk_size);
11371165
break;
11381166
}
1167+
1168+
uint32_t size_read = (uint32_t)(s->in - chunk_start);
1169+
CUTE_ASEPRITE_ASSERT(size_read == chunk_size);
11391170
}
11401171
}
11411172

@@ -1150,8 +1181,20 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
11501181
if (!(cel->layer->flags & ASE_LAYER_FLAGS_VISIBLE)) {
11511182
continue;
11521183
}
1184+
if (cel->layer->parent && !(cel->layer->parent->flags & ASE_LAYER_FLAGS_VISIBLE)) {
1185+
continue;
1186+
}
11531187
while (cel->is_linked) {
1154-
cel = ase->frames[cel->linked_frame_index].cels + j;
1188+
ase_frame_t* frame = ase->frames + cel->linked_frame_index;
1189+
int found = 0;
1190+
for (int k = 0; k < frame->cel_count; ++k) {
1191+
if (frame->cels[k].layer == cel->layer) {
1192+
cel = frame->cels + k;
1193+
found = 1;
1194+
break;
1195+
}
1196+
}
1197+
CUTE_ASEPRITE_ASSERT(found);
11551198
}
11561199
void* src = cel->pixels;
11571200
uint8_t opacity = (uint8_t)(cel->opacity * cel->layer->opacity * 255.0f);

0 commit comments

Comments
 (0)