3
3
Licensing information can be found at the end of the file.
4
4
------------------------------------------------------------------------------
5
5
6
- cute_aseprite.h - v1.01
6
+ cute_aseprite.h - v1.02
7
7
8
8
To create implementation (the function definitions)
9
9
#define CUTE_ASEPRITE_IMPLEMENTATION
43
43
Revision history:
44
44
1.00 (08/25/2020) initial release
45
45
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)
46
48
*/
47
49
48
50
/*
@@ -102,7 +104,7 @@ void cute_aseprite_free(ase_t* aseprite);
102
104
103
105
#define CUTE_ASEPRITE_MAX_LAYERS (64)
104
106
#define CUTE_ASEPRITE_MAX_SLICES (128)
105
- #define CUTE_ASEPRITE_MAX_PALETTE_ENTRIES (256 )
107
+ #define CUTE_ASEPRITE_MAX_PALETTE_ENTRIES (1024 )
106
108
#define CUTE_ASEPRITE_MAX_TAGS (256)
107
109
108
110
#include <stdint.h>
@@ -162,7 +164,7 @@ struct ase_layer_t
162
164
ase_layer_flags_t flags ;
163
165
ase_layer_type_t type ;
164
166
const char * name ;
165
- int child_level ;
167
+ ase_layer_t * parent ;
166
168
float opacity ;
167
169
ase_udata_t udata ;
168
170
};
@@ -212,6 +214,7 @@ struct ase_tag_t
212
214
ase_animation_direction_t loop_animation_direction ;
213
215
uint8_t r , g , b ;
214
216
const char * name ;
217
+ ase_udata_t udata ;
215
218
};
216
219
217
220
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)
852
855
g = dst .g + (src .g - dst .g ) * src .a / a ;
853
856
b = dst .b + (src .b - dst .b ) * src .a / a ;
854
857
}
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 ;
856
860
}
857
861
858
862
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)
878
882
} else {
879
883
CUTE_ASEPRITE_ASSERT (ase -> mode == ASE_MODE_INDEXED );
880
884
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
+ }
882
893
}
883
894
return result ;
884
895
}
@@ -926,6 +937,10 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
926
937
CUTE_ASEPRITE_MEMSET (ase -> frames , 0 , sizeof (ase_frame_t ) * (size_t )ase -> frame_count );
927
938
928
939
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 ];
929
944
930
945
// Parse all chunks in the .aseprite file.
931
946
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
954
969
ase_layer_t * layer = ase -> layers + ase -> layer_count ++ ;
955
970
layer -> flags = (ase_layer_flags_t )s_read_uint16 (s );
956
971
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
+ }
958
978
s_skip (s , sizeof (uint16_t )); // Default layer width in pixels (ignored).
959
979
s_skip (s , sizeof (uint16_t )); // Default layer height in pixels (ignored).
960
980
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
1037
1057
ase -> color_profile .icc_profile_data_length = s_read_uint32 (s );
1038
1058
ase -> color_profile .icc_profile_data = CUTE_ASEPRITE_ALLOC (ase -> color_profile .icc_profile_data_length , mem_ctx );
1039
1059
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 ;
1040
1061
}
1041
1062
} break ;
1042
1063
@@ -1058,11 +1079,13 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
1058
1079
tag .name = s_read_string (s );
1059
1080
ase -> tags [k ] = tag ;
1060
1081
}
1082
+ was_on_tags = 1 ;
1061
1083
} break ;
1062
1084
1063
1085
case 0x2019 : // Palette chunk.
1064
1086
{
1065
1087
ase -> palette .entry_count = (int )s_read_uint32 (s );
1088
+ CUTE_ASEPRITE_ASSERT (ase -> palette .entry_count <= CUTE_ASEPRITE_MAX_PALETTE_ENTRIES );
1066
1089
int first_index = (int )s_read_uint32 (s );
1067
1090
int last_index = (int )s_read_uint32 (s );
1068
1091
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
1078
1101
} else {
1079
1102
entry .color_name = NULL ;
1080
1103
}
1104
+ CUTE_ASEPRITE_ASSERT (k < CUTE_ASEPRITE_MAX_PALETTE_ENTRIES );
1081
1105
ase -> palette .entries [k ] = entry ;
1082
1106
}
1083
1107
} break ;
1084
1108
1085
1109
case 0x2020 : // Udata chunk.
1086
1110
{
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
+ }
1088
1116
int flags = (int )s_read_uint32 (s );
1089
1117
if (flags & 1 ) {
1090
1118
last_udata -> has_text = 1 ;
@@ -1136,6 +1164,9 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
1136
1164
s_skip (s , (int )chunk_size );
1137
1165
break ;
1138
1166
}
1167
+
1168
+ uint32_t size_read = (uint32_t )(s -> in - chunk_start );
1169
+ CUTE_ASEPRITE_ASSERT (size_read == chunk_size );
1139
1170
}
1140
1171
}
1141
1172
@@ -1150,8 +1181,20 @@ ase_t* cute_aseprite_load_from_memory(const void* memory, int size, void* mem_ct
1150
1181
if (!(cel -> layer -> flags & ASE_LAYER_FLAGS_VISIBLE )) {
1151
1182
continue ;
1152
1183
}
1184
+ if (cel -> layer -> parent && !(cel -> layer -> parent -> flags & ASE_LAYER_FLAGS_VISIBLE )) {
1185
+ continue ;
1186
+ }
1153
1187
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 );
1155
1198
}
1156
1199
void * src = cel -> pixels ;
1157
1200
uint8_t opacity = (uint8_t )(cel -> opacity * cel -> layer -> opacity * 255.0f );
0 commit comments