add a byte stream: pxl8_stream
This commit is contained in:
parent
ede16ca7de
commit
82ed6b4ea9
7 changed files with 350 additions and 273 deletions
260
src/pxl8_ase.c
260
src/pxl8_ase.c
|
|
@ -14,35 +14,24 @@
|
|||
#include "pxl8_io.h"
|
||||
#include "pxl8_macros.h"
|
||||
|
||||
static u16 read_u16_le(const u8* data) {
|
||||
return data[0] | (data[1] << 8);
|
||||
}
|
||||
|
||||
static u32 read_u32_le(const u8* data) {
|
||||
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
}
|
||||
|
||||
static i16 read_i16_le(const u8* data) {
|
||||
return (i16)read_u16_le(data);
|
||||
}
|
||||
|
||||
static pxl8_result parse_ase_header(const u8* data, pxl8_ase_header* header) {
|
||||
header->file_size = read_u32_le(data);
|
||||
header->magic = read_u16_le(data + 4);
|
||||
header->frames = read_u16_le(data + 6);
|
||||
header->width = read_u16_le(data + 8);
|
||||
header->height = read_u16_le(data + 10);
|
||||
header->color_depth = read_u16_le(data + 12);
|
||||
header->flags = read_u32_le(data + 14);
|
||||
header->speed = read_u16_le(data + 18);
|
||||
header->transparent_index = read_u32_le(data + 24);
|
||||
header->n_colors = data[28];
|
||||
header->pixel_width = data[29];
|
||||
header->pixel_height = data[30];
|
||||
header->grid_x = read_i16_le(data + 31);
|
||||
header->grid_y = read_i16_le(data + 33);
|
||||
header->grid_width = read_u16_le(data + 35);
|
||||
header->grid_height = read_u16_le(data + 37);
|
||||
static pxl8_result parse_ase_header(pxl8_stream* stream, pxl8_ase_header* header) {
|
||||
header->file_size = pxl8_read_u32(stream);
|
||||
header->magic = pxl8_read_u16(stream);
|
||||
header->frames = pxl8_read_u16(stream);
|
||||
header->width = pxl8_read_u16(stream);
|
||||
header->height = pxl8_read_u16(stream);
|
||||
header->color_depth = pxl8_read_u16(stream);
|
||||
header->flags = pxl8_read_u32(stream);
|
||||
header->speed = pxl8_read_u16(stream);
|
||||
pxl8_skip_bytes(stream, 4);
|
||||
header->transparent_index = pxl8_read_u32(stream);
|
||||
header->n_colors = pxl8_read_u8(stream);
|
||||
header->pixel_width = pxl8_read_u8(stream);
|
||||
header->pixel_height = pxl8_read_u8(stream);
|
||||
header->grid_x = pxl8_read_i16(stream);
|
||||
header->grid_y = pxl8_read_i16(stream);
|
||||
header->grid_width = pxl8_read_u16(stream);
|
||||
header->grid_height = pxl8_read_u16(stream);
|
||||
|
||||
if (header->magic != PXL8_ASE_MAGIC) {
|
||||
pxl8_error("Invalid ASE file magic: 0x%04X", header->magic);
|
||||
|
|
@ -52,20 +41,19 @@ static pxl8_result parse_ase_header(const u8* data, pxl8_ase_header* header) {
|
|||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result parse_old_palette_chunk(const u8* data, pxl8_ase_palette* palette) {
|
||||
u16 packet_count = read_u16_le(data);
|
||||
const u8* packet_data = data + 2;
|
||||
|
||||
static pxl8_result parse_old_palette_chunk(pxl8_stream* stream, pxl8_ase_palette* palette) {
|
||||
u16 packet_count = pxl8_read_u16(stream);
|
||||
|
||||
u32 total_colors = 0;
|
||||
const u8* temp_data = packet_data;
|
||||
u32 temp_pos = pxl8_stream_position(stream);
|
||||
for (u16 packet = 0; packet < packet_count; packet++) {
|
||||
u8 skip_colors = temp_data[0];
|
||||
u8 colors_in_packet = temp_data[1];
|
||||
u8 skip_colors = pxl8_read_u8(stream);
|
||||
u8 colors_in_packet = pxl8_read_u8(stream);
|
||||
u32 actual_colors = (colors_in_packet == 0) ? 256 : colors_in_packet;
|
||||
total_colors = skip_colors + actual_colors;
|
||||
temp_data += 2 + (actual_colors * 3);
|
||||
total_colors += skip_colors + actual_colors;
|
||||
pxl8_skip_bytes(stream, actual_colors * 3);
|
||||
}
|
||||
|
||||
|
||||
palette->entry_count = total_colors;
|
||||
palette->first_color = 0;
|
||||
palette->last_color = total_colors - 1;
|
||||
|
|
@ -73,119 +61,127 @@ static pxl8_result parse_old_palette_chunk(const u8* data, pxl8_ase_palette* pal
|
|||
if (!palette->colors) {
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
|
||||
pxl8_stream_seek(stream, temp_pos);
|
||||
|
||||
u32 color_index = 0;
|
||||
for (u16 packet = 0; packet < packet_count; packet++) {
|
||||
u8 skip_colors = packet_data[0];
|
||||
u8 colors_in_packet = packet_data[1];
|
||||
u8 skip_colors = pxl8_read_u8(stream);
|
||||
u8 colors_in_packet = pxl8_read_u8(stream);
|
||||
u32 actual_colors = (colors_in_packet == 0) ? 256 : colors_in_packet;
|
||||
|
||||
for (u32 i = 0; i < skip_colors && color_index < total_colors; i++, color_index++) {
|
||||
palette->colors[color_index] = 0xFF000000;
|
||||
}
|
||||
|
||||
packet_data += 2;
|
||||
|
||||
for (u32 i = 0; i < actual_colors && color_index < total_colors; i++, color_index++) {
|
||||
u8 r = packet_data[0];
|
||||
u8 g = packet_data[1];
|
||||
u8 b = packet_data[2];
|
||||
|
||||
palette->colors[color_index] = 0xFF000000 | (b << 16) | (g << 8) | r;
|
||||
packet_data += 3;
|
||||
for (u32 skip = 0; skip < skip_colors && color_index < total_colors; skip++) {
|
||||
palette->colors[color_index++] = 0xFF000000;
|
||||
}
|
||||
|
||||
for (u32 color = 0; color < actual_colors && color_index < total_colors; color++) {
|
||||
u8 r = pxl8_read_u8(stream);
|
||||
u8 g = pxl8_read_u8(stream);
|
||||
u8 b = pxl8_read_u8(stream);
|
||||
palette->colors[color_index++] = 0xFF000000 | (b << 16) | (g << 8) | r;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result parse_layer_chunk(const u8* data, pxl8_ase_layer* layer) {
|
||||
layer->flags = read_u16_le(data);
|
||||
layer->layer_type = read_u16_le(data + 2);
|
||||
layer->child_level = read_u16_le(data + 4);
|
||||
layer->blend_mode = read_u16_le(data + 10);
|
||||
layer->opacity = data[12];
|
||||
static pxl8_result parse_layer_chunk(pxl8_stream* stream, pxl8_ase_layer* layer) {
|
||||
layer->flags = pxl8_read_u16(stream);
|
||||
layer->layer_type = pxl8_read_u16(stream);
|
||||
layer->child_level = pxl8_read_u16(stream);
|
||||
pxl8_skip_bytes(stream, 4);
|
||||
layer->blend_mode = pxl8_read_u16(stream);
|
||||
layer->opacity = pxl8_read_u8(stream);
|
||||
pxl8_skip_bytes(stream, 3);
|
||||
|
||||
u16 name_len = read_u16_le(data + 16);
|
||||
u16 name_len = pxl8_read_u16(stream);
|
||||
if (name_len > 0) {
|
||||
layer->name = (char*)SDL_malloc(name_len + 1);
|
||||
if (!layer->name) return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
memcpy(layer->name, data + 18, name_len);
|
||||
pxl8_read_bytes(stream, layer->name, name_len);
|
||||
layer->name[name_len] = '\0';
|
||||
} else {
|
||||
layer->name = NULL;
|
||||
}
|
||||
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result parse_palette_chunk(const u8* data, pxl8_ase_palette* palette) {
|
||||
palette->entry_count = read_u32_le(data);
|
||||
palette->first_color = read_u32_le(data + 4);
|
||||
palette->last_color = read_u32_le(data + 8);
|
||||
|
||||
u32 color_count = palette->entry_count;
|
||||
static pxl8_result parse_palette_chunk(pxl8_stream* stream, pxl8_ase_palette* palette) {
|
||||
pxl8_skip_bytes(stream, 4);
|
||||
palette->first_color = pxl8_read_u32(stream);
|
||||
palette->last_color = pxl8_read_u32(stream);
|
||||
pxl8_skip_bytes(stream, 8);
|
||||
|
||||
u32 color_count = palette->last_color - palette->first_color + 1;
|
||||
|
||||
if (color_count == 0 || color_count > 65536) {
|
||||
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
|
||||
}
|
||||
|
||||
palette->colors = (u32*)SDL_malloc(color_count * sizeof(u32));
|
||||
if (!palette->colors) {
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
const u8* color_data = data + 20;
|
||||
palette->entry_count = color_count;
|
||||
|
||||
for (u32 i = 0; i < color_count; i++) {
|
||||
u16 flags = read_u16_le(color_data);
|
||||
u8 r = color_data[2];
|
||||
u8 g = color_data[3];
|
||||
u8 b = color_data[4];
|
||||
u8 a = color_data[5];
|
||||
u16 flags = pxl8_read_u16(stream);
|
||||
u8 r = pxl8_read_u8(stream);
|
||||
u8 g = pxl8_read_u8(stream);
|
||||
u8 b = pxl8_read_u8(stream);
|
||||
u8 a = pxl8_read_u8(stream);
|
||||
|
||||
palette->colors[i] = (a << 24) | (b << 16) | (g << 8) | r;
|
||||
color_data += 6;
|
||||
|
||||
|
||||
if (flags & 1) {
|
||||
u16 name_len = read_u16_le(color_data);
|
||||
color_data += 2 + name_len;
|
||||
u16 name_len = pxl8_read_u16(stream);
|
||||
pxl8_skip_bytes(stream, name_len);
|
||||
}
|
||||
}
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result parse_cel_chunk(const u8* data, u32 chunk_size, pxl8_ase_cel* cel) {
|
||||
static pxl8_result parse_cel_chunk(pxl8_stream* stream, u32 chunk_size, pxl8_ase_cel* cel) {
|
||||
if (chunk_size < 9) {
|
||||
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
|
||||
}
|
||||
|
||||
cel->layer_index = read_u16_le(data);
|
||||
cel->x = read_i16_le(data + 2);
|
||||
cel->y = read_i16_le(data + 4);
|
||||
cel->opacity = data[6];
|
||||
cel->cel_type = read_u16_le(data + 7);
|
||||
cel->layer_index = pxl8_read_u16(stream);
|
||||
cel->x = pxl8_read_i16(stream);
|
||||
cel->y = pxl8_read_i16(stream);
|
||||
cel->opacity = pxl8_read_u8(stream);
|
||||
cel->cel_type = pxl8_read_u16(stream);
|
||||
pxl8_skip_bytes(stream, 7);
|
||||
|
||||
if (cel->cel_type == 2) {
|
||||
if (chunk_size < 20) {
|
||||
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
|
||||
}
|
||||
|
||||
cel->width = read_u16_le(data + 16);
|
||||
cel->height = read_u16_le(data + 18);
|
||||
|
||||
cel->width = pxl8_read_u16(stream);
|
||||
cel->height = pxl8_read_u16(stream);
|
||||
|
||||
u32 pixel_data_size = cel->width * cel->height;
|
||||
u32 compressed_data_size = chunk_size - 20;
|
||||
|
||||
|
||||
cel->pixel_data = (u8*)SDL_malloc(pixel_data_size);
|
||||
if (!cel->pixel_data) {
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
const u8* compressed_data = pxl8_read_ptr(stream, compressed_data_size);
|
||||
mz_ulong dest_len = pixel_data_size;
|
||||
i32 result = mz_uncompress(cel->pixel_data, &dest_len, data + 20, compressed_data_size);
|
||||
i32 result = mz_uncompress(cel->pixel_data, &dest_len, compressed_data, compressed_data_size);
|
||||
if (result != MZ_OK) {
|
||||
pxl8_error("Failed to decompress cel data: miniz error %d", result);
|
||||
SDL_free(cel->pixel_data);
|
||||
cel->pixel_data = NULL;
|
||||
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
|
||||
}
|
||||
|
||||
|
||||
if (dest_len != pixel_data_size) {
|
||||
pxl8_warn("Decompressed size mismatch: expected %u, got %lu", pixel_data_size, dest_len);
|
||||
}
|
||||
|
|
@ -213,7 +209,9 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
return PXL8_ERROR_ASE_TRUNCATED_FILE;
|
||||
}
|
||||
|
||||
result = parse_ase_header(file_data, &ase_file->header);
|
||||
pxl8_stream stream = pxl8_stream_create(file_data, (u32)file_size);
|
||||
|
||||
result = parse_ase_header(&stream, &ase_file->header);
|
||||
if (result != PXL8_OK) {
|
||||
pxl8_io_free_binary_data(file_data);
|
||||
return result;
|
||||
|
|
@ -226,13 +224,17 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
const u8* frame_data = file_data + 128;
|
||||
pxl8_stream_seek(&stream, 128);
|
||||
|
||||
for (u16 frame_idx = 0; frame_idx < ase_file->header.frames; frame_idx++) {
|
||||
u32 frame_start = pxl8_stream_position(&stream);
|
||||
|
||||
pxl8_ase_frame_header frame_header;
|
||||
frame_header.frame_bytes = read_u32_le(frame_data);
|
||||
frame_header.magic = read_u16_le(frame_data + 4);
|
||||
frame_header.chunks = read_u16_le(frame_data + 6);
|
||||
frame_header.duration = read_u16_le(frame_data + 8);
|
||||
frame_header.frame_bytes = pxl8_read_u32(&stream);
|
||||
frame_header.magic = pxl8_read_u16(&stream);
|
||||
u16 old_chunks = pxl8_read_u16(&stream);
|
||||
frame_header.duration = pxl8_read_u16(&stream);
|
||||
pxl8_skip_bytes(&stream, 2);
|
||||
|
||||
if (frame_header.magic != PXL8_ASE_FRAME_MAGIC) {
|
||||
pxl8_error("Invalid frame magic: 0x%04X", frame_header.magic);
|
||||
|
|
@ -240,12 +242,19 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
break;
|
||||
}
|
||||
|
||||
if (old_chunks == 0xFFFF || old_chunks == 0xFFFE) {
|
||||
frame_header.chunks = pxl8_read_u32(&stream);
|
||||
} else {
|
||||
frame_header.chunks = old_chunks;
|
||||
pxl8_skip_bytes(&stream, 4);
|
||||
}
|
||||
|
||||
pxl8_ase_frame* frame = &ase_file->frames[frame_idx];
|
||||
frame->frame_id = frame_idx;
|
||||
frame->width = ase_file->header.width;
|
||||
frame->height = ase_file->header.height;
|
||||
frame->duration = frame_header.duration;
|
||||
|
||||
|
||||
u32 pixel_count = frame->width * frame->height;
|
||||
frame->pixels = (u8*)SDL_calloc(pixel_count, sizeof(u8));
|
||||
if (!frame->pixels) {
|
||||
|
|
@ -253,22 +262,28 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
break;
|
||||
}
|
||||
|
||||
const u8* chunk_data = frame_data + 16;
|
||||
for (u16 chunk_idx = 0; chunk_idx < frame_header.chunks; chunk_idx++) {
|
||||
pxl8_ase_chunk_header chunk_header;
|
||||
chunk_header.chunk_size = read_u32_le(chunk_data);
|
||||
chunk_header.chunk_type = read_u16_le(chunk_data + 4);
|
||||
u32 chunk_start = pxl8_stream_position(&stream);
|
||||
|
||||
const u8* chunk_payload = chunk_data + 6;
|
||||
pxl8_ase_chunk_header chunk_header;
|
||||
chunk_header.chunk_size = pxl8_read_u32(&stream);
|
||||
chunk_header.chunk_type = pxl8_read_u16(&stream);
|
||||
|
||||
if (chunk_header.chunk_size < 6 || chunk_header.chunk_size > frame_header.frame_bytes) {
|
||||
pxl8_error("Invalid chunk size %u in ASE file (frame_bytes=%u)",
|
||||
chunk_header.chunk_size, frame_header.frame_bytes);
|
||||
result = PXL8_ERROR_ASE_MALFORMED_CHUNK;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (chunk_header.chunk_type) {
|
||||
case PXL8_ASE_CHUNK_OLD_PALETTE: // 0x0004
|
||||
case PXL8_ASE_CHUNK_OLD_PALETTE:
|
||||
if (!ase_file->palette.colors) {
|
||||
result = parse_old_palette_chunk(chunk_payload, &ase_file->palette);
|
||||
result = parse_old_palette_chunk(&stream, &ase_file->palette);
|
||||
}
|
||||
break;
|
||||
|
||||
case PXL8_ASE_CHUNK_LAYER: { // 0x2004
|
||||
|
||||
case PXL8_ASE_CHUNK_LAYER: {
|
||||
ase_file->layers =
|
||||
(pxl8_ase_layer*)SDL_realloc(ase_file->layers,
|
||||
(ase_file->layer_count + 1) * sizeof(pxl8_ase_layer));
|
||||
|
|
@ -276,17 +291,17 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
result = PXL8_ERROR_OUT_OF_MEMORY;
|
||||
break;
|
||||
}
|
||||
|
||||
result = parse_layer_chunk(chunk_payload, &ase_file->layers[ase_file->layer_count]);
|
||||
|
||||
result = parse_layer_chunk(&stream, &ase_file->layers[ase_file->layer_count]);
|
||||
if (result == PXL8_OK) {
|
||||
ase_file->layer_count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case PXL8_ASE_CHUNK_CEL: { // 0x2005
|
||||
|
||||
case PXL8_ASE_CHUNK_CEL: {
|
||||
pxl8_ase_cel cel = {0};
|
||||
result = parse_cel_chunk(chunk_payload, chunk_header.chunk_size - 6, &cel);
|
||||
result = parse_cel_chunk(&stream, chunk_header.chunk_size - 6, &cel);
|
||||
if (result == PXL8_OK && cel.pixel_data) {
|
||||
u32 copy_width = (cel.width < frame->width) ? cel.width : frame->width;
|
||||
u32 copy_height = (cel.height < frame->height) ? cel.height : frame->height;
|
||||
|
|
@ -314,26 +329,27 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
|
|||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case PXL8_ASE_CHUNK_PALETTE: // 0x2019
|
||||
|
||||
case PXL8_ASE_CHUNK_PALETTE:
|
||||
if (ase_file->palette.colors) {
|
||||
SDL_free(ase_file->palette.colors);
|
||||
ase_file->palette.colors = NULL;
|
||||
}
|
||||
result = parse_palette_chunk(chunk_payload, &ase_file->palette);
|
||||
result = parse_palette_chunk(&stream, &ase_file->palette);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (result != PXL8_OK) break;
|
||||
|
||||
chunk_data += chunk_header.chunk_size;
|
||||
pxl8_stream_seek(&stream, chunk_start + chunk_header.chunk_size);
|
||||
}
|
||||
|
||||
|
||||
if (result != PXL8_OK) break;
|
||||
|
||||
frame_data += frame_header.frame_bytes;
|
||||
pxl8_stream_seek(&stream, frame_start + frame_header.frame_bytes);
|
||||
}
|
||||
|
||||
pxl8_io_free_binary_data(file_data);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue