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

File reader changes #867

Merged
merged 4 commits into from
Mar 6, 2024
Merged
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
57 changes: 41 additions & 16 deletions Backends/System/Android/Sources/kinc/backend/system.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1251,23 +1251,43 @@ void initAndroidFileReader(void) {
(*activity->vm)->DetachCurrentThread(activity->vm);
}

static void kinc_aasset_reader_close(kinc_file_reader_t *reader)
{
AAsset_close((struct AAsset *)reader->data);
}

static size_t kinc_aasset_reader_read(kinc_file_reader_t *reader, void *data, size_t size)
{
return AAsset_read((struct AAsset *)reader->data, data, size);
}

static size_t kinc_aasset_reader_pos(kinc_file_reader_t *reader)
{
return (size_t)AAsset_seek((struct AAsset *)reader->data, 0, SEEK_CUR);
}

static void kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos)
{
AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET);
}

bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) {
reader->pos = 0;
reader->file = NULL;
reader->asset = NULL;
memset(reader, 0, sizeof(kinc_file_reader_t));
reader->type = type;

if (type == KINC_FILE_TYPE_SAVE) {
char filepath[1001];

strcpy(filepath, kinc_internal_save_path());
strcat(filepath, filename);

reader->file = fopen(filepath, "rb");
if (reader->file == NULL) {
reader->data = fopen(filepath, "rb");
if (reader->data == NULL) {
return false;
}
fseek(reader->file, 0, SEEK_END);
reader->size = ftell(reader->file);
fseek(reader->file, 0, SEEK_SET);
fseek((FILE *)reader->data, 0, SEEK_END);
reader->size = ftell((FILE *)reader->data);
fseek((FILE *)reader->data, 0, SEEK_SET);
return true;
}
else {
Expand All @@ -1282,18 +1302,23 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int
strcat(filepath, filename);
}

reader->file = fopen(filepath, "rb");
if (reader->file != NULL) {
fseek(reader->file, 0, SEEK_END);
reader->size = ftell(reader->file);
fseek(reader->file, 0, SEEK_SET);
FILE *stream = fopen(filepath, "rb");
if (stream != NULL) {
reader->data = stream;
fseek(stream, 0, SEEK_END);
reader->size = ftell(stream);
fseek(stream, 0, SEEK_SET);
return true;
}
else {
reader->asset = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM);
if (reader->asset == NULL)
reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM);
if (reader->data == NULL)
return false;
reader->size = AAsset_getLength(reader->asset);
reader->size = AAsset_getLength((struct AAsset *)reader->data);
reader->close = kinc_aasset_reader_close;
reader->read = kinc_aasset_reader_read;
reader->pos = kinc_aasset_reader_pos;
reader->seek = kinc_aasset_reader_seek;
return true;
}
}
Expand Down
61 changes: 31 additions & 30 deletions Sources/kinc/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ typedef struct kinc_image {
unsigned internal_format;
kinc_image_compression_t compression;
void *data;
int data_size;
size_t data_size;
} kinc_image_t;

typedef struct kinc_image_read_callbacks {
int (*read)(void *user_data, void *data, size_t size);
void (*seek)(void *user_data, int pos);
int (*pos)(void *user_data);
size_t (*read)(void *user_data, void *data, size_t size);
void (*seek)(void *user_data, size_t pos);
size_t (*pos)(void *user_data);
size_t (*size)(void *user_data);
} kinc_image_read_callbacks_t;

Expand Down Expand Up @@ -236,13 +236,13 @@ typedef struct {
// fill 'data' with 'size' bytes. return number of bytes actually read
static int stb_read(void *user, char *data, int size) {
read_data *reader = (read_data *)user;
return reader->callbacks.read(reader->user_data, data, size);
return (int)reader->callbacks.read(reader->user_data, data, (size_t)size);
}

// skip the next 'n' bytes, or 'unget' the last -n bytes if negative
static void stb_skip(void *user, int n) {
read_data *reader = (read_data *)user;
reader->callbacks.seek(reader->user_data, reader->callbacks.pos(reader->user_data) + n);
reader->callbacks.seek(reader->user_data, reader->callbacks.pos(reader->user_data) + (size_t)n);
}

// returns nonzero if we are at end of file/data
Expand Down Expand Up @@ -336,7 +336,7 @@ static size_t loadImageSize(kinc_image_read_callbacks_t callbacks, void *user_da
}
}

static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, const char *filename, uint8_t *output, int *outputSize, int *width, int *height,
static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, const char *filename, uint8_t *output, size_t *outputSize, int *width, int *height,
kinc_image_compression_t *compression, kinc_image_format_t *format, unsigned *internalFormat) {
*format = KINC_IMAGE_FORMAT_RGBA32;
if (endsWith(filename, "k")) {
Expand All @@ -355,25 +355,25 @@ static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, co
if (strcmp(fourcc, "LZ4 ") == 0) {
*compression = KINC_IMAGE_COMPRESSION_NONE;
*internalFormat = 0;
*outputSize = *width * *height * 4;
*outputSize = (size_t)(*width * *height * 4);
callbacks.read(user_data, buffer, compressedSize);
LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, *outputSize);
LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, (int)*outputSize);
return true;
}
else if (strcmp(fourcc, "LZ4F") == 0) {
*compression = KINC_IMAGE_COMPRESSION_NONE;
*internalFormat = 0;
*outputSize = *width * *height * 16;
*outputSize = (size_t)(*width * *height * 16);
callbacks.read(user_data, buffer, compressedSize);
LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, *outputSize);
LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, (int)*outputSize);
*format = KINC_IMAGE_FORMAT_RGBA128;
return true;
}
else if (strcmp(fourcc, "ASTC") == 0) {
*compression = KINC_IMAGE_COMPRESSION_ASTC;
*outputSize = *width * *height * 4;
*outputSize = (size_t)(*width * *height * 4);
callbacks.read(user_data, buffer, compressedSize);
*outputSize = LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, *outputSize);
*outputSize = LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, (int)*outputSize);

uint8_t blockdim_x = 6;
uint8_t blockdim_y = 6;
Expand Down Expand Up @@ -413,9 +413,9 @@ static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, co
}
else if (strcmp(fourcc, "DXT5") == 0) {
*compression = KINC_IMAGE_COMPRESSION_DXT5;
*outputSize = *width * *height;
*outputSize = (size_t)(*width * *height);
callbacks.read(user_data, buffer, compressedSize);
*outputSize = LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, *outputSize);
*outputSize = LZ4_decompress_safe((char *)buffer, (char *)output, compressedSize, (int)*outputSize);
*internalFormat = 0;
return true;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, co
*compression = KINC_IMAGE_COMPRESSION_PVRTC;
*internalFormat = 0;

*outputSize = ww * hh / 2;
*outputSize = (size_t)(ww * hh / 2);
callbacks.read(user_data, output, *outputSize);
return true;
}
Expand All @@ -495,7 +495,7 @@ static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, co
kinc_log(KINC_LOG_LEVEL_ERROR, stbi_failure_reason());
return false;
}
*outputSize = *width * *height * 16;
*outputSize = (size_t)(*width * *height * 16);
memcpy(output, uncompressed, *outputSize);
*format = KINC_IMAGE_FORMAT_RGBA128;
buffer_offset = 0;
Expand Down Expand Up @@ -535,7 +535,7 @@ static bool loadImage(kinc_image_read_callbacks_t callbacks, void *user_data, co
output[y * *width * 4 + x * 4 + 3] = (uint8_t)kinc_round(a * 255.0f);
}
}
*outputSize = *width * *height * 4;
*outputSize = (size_t)(*width * *height * 4);
buffer_offset = 0;
return true;
}
Expand Down Expand Up @@ -593,19 +593,19 @@ void kinc_image_init_from_bytes3d(kinc_image_t *image, void *data, int width, in
image->data = data;
}

static int read_callback(void *user_data, void *data, size_t size) {
static size_t read_callback(void *user_data, void *data, size_t size) {
return kinc_file_reader_read((kinc_file_reader_t *)user_data, data, size);
}

static size_t size_callback(void *user_data) {
return kinc_file_reader_size((kinc_file_reader_t *)user_data);
}

static int pos_callback(void *user_data) {
static size_t pos_callback(void *user_data) {
return kinc_file_reader_pos((kinc_file_reader_t *)user_data);
}

static void seek_callback(void *user_data, int pos) {
static void seek_callback(void *user_data, size_t pos) {
kinc_file_reader_seek((kinc_file_reader_t *)user_data, pos);
}

Expand All @@ -615,27 +615,27 @@ struct kinc_internal_image_memory {
size_t offset;
};

static int memory_read_callback(void *user_data, void *data, size_t size) {
static size_t memory_read_callback(void *user_data, void *data, size_t size) {
struct kinc_internal_image_memory *memory = (struct kinc_internal_image_memory *)user_data;
size_t read_size = memory->size - memory->offset < size ? memory->size - memory->offset : size;
memcpy(data, &memory->data[memory->offset], read_size);
memory->offset += read_size;
return (int)read_size;
return read_size;
}

static size_t memory_size_callback(void *user_data) {
struct kinc_internal_image_memory *memory = (struct kinc_internal_image_memory *)user_data;
return memory->size;
}

static int memory_pos_callback(void *user_data) {
static size_t memory_pos_callback(void *user_data) {
struct kinc_internal_image_memory *memory = (struct kinc_internal_image_memory *)user_data;
return (int)memory->offset;
return memory->offset;
}

static void memory_seek_callback(void *user_data, int pos) {
static void memory_seek_callback(void *user_data, size_t pos) {
struct kinc_internal_image_memory *memory = (struct kinc_internal_image_memory *)user_data;
memory->offset = (size_t)pos;
memory->offset = pos;
}

size_t kinc_image_size_from_callbacks(kinc_image_read_callbacks_t callbacks, void *user_data, const char *filename) {
Expand Down Expand Up @@ -674,9 +674,10 @@ size_t kinc_image_size_from_encoded_bytes(void *data, size_t data_size, const ch
}

size_t kinc_image_init_from_callbacks(kinc_image_t *image, void *memory, kinc_image_read_callbacks_t callbacks, void *user_data, const char *filename) {
int dataSize;
size_t dataSize = 0;
loadImage(callbacks, user_data, filename, memory, &dataSize, &image->width, &image->height, &image->compression, &image->format, &image->internal_format);
image->data = memory;
image->data_size = dataSize;
return dataSize;
}

Expand All @@ -689,7 +690,7 @@ size_t kinc_image_init_from_file(kinc_image_t *image, void *memory, const char *
callbacks.pos = pos_callback;
callbacks.seek = seek_callback;

int dataSize;
size_t dataSize = 0;
loadImage(callbacks, &reader, filename, memory, &dataSize, &image->width, &image->height, &image->compression, &image->format, &image->internal_format);
kinc_file_reader_close(&reader);
image->data = memory;
Expand All @@ -712,7 +713,7 @@ size_t kinc_image_init_from_encoded_bytes(kinc_image_t *image, void *memory, voi
image_memory.size = data_size;
image_memory.offset = 0;

int dataSize;
size_t dataSize = 0;
loadImage(callbacks, &image_memory, format, memory, &dataSize, &image->width, &image->height, &image->compression, &image->format, &image->internal_format);
image->data = memory;
image->data_size = dataSize;
Expand Down
Loading
Loading