Adiciona registro de formatos no core e tinyblocks-image (ops genericas + Load/Save)

This commit is contained in:
mguinhos committed 2026-10-20 21:08:18 -03:00
1 parent fca7aa2f63
commit f4e2556110
11 files changed
+479 -9

No files matched your search

+59
View File
@@ -4,6 +4,7 @@
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tinyblocks/tb_image.h"
@@ -93,6 +94,64 @@ bool tb_image_get_pixel(const TinyBlocks_Image* img, int x, int y,
return true;
}
/* -- Registro de formatos ------------------------------------------------- */
#define TB_IMAGE_MAX_FORMATS 24
typedef struct {
char ext[12];
TbImageDecoder dec;
TbImageEncoder enc;
} ImageFormat;
static ImageFormat s_formats[TB_IMAGE_MAX_FORMATS];
static int s_format_n = 0;
bool tb_image_register_format(const char* ext, TbImageDecoder dec, TbImageEncoder enc) {
if (!ext || !ext[0]) return false;
for (int i = 0; i < s_format_n; i++)
if (strcmp(s_formats[i].ext, ext) == 0) { /* sobrescreve */
s_formats[i].dec = dec;
s_formats[i].enc = enc;
return true;
}
if (s_format_n >= TB_IMAGE_MAX_FORMATS) return false;
ImageFormat* f = &s_formats[s_format_n++];
size_t e = strlen(ext);
if (e >= sizeof(f->ext)) e = sizeof(f->ext) - 1;
for (size_t i = 0; i < e; i++) f->ext[i] = (char)tolower((unsigned char)ext[i]);
f->ext[e] = 0;
f->dec = dec;
f->enc = enc;
return true;
}
int tb_image_format_count(void) { return s_format_n; }
static const ImageFormat* format_for(const char* path) {
if (!path) return NULL;
const char* dot = strrchr(path, '.');
if (!dot || !dot[1]) return NULL;
char ext[12];
size_t n = 0;
for (const char* p = dot + 1; *p && n + 1 < sizeof(ext); p++)
ext[n++] = (char)tolower((unsigned char)*p);
ext[n] = 0;
for (int i = 0; i < s_format_n; i++)
if (strcmp(s_formats[i].ext, ext) == 0) return &s_formats[i];
return NULL;
}
TinyBlocks_Image* tb_image_load(const char* path) {
const ImageFormat* f = format_for(path);
return (f && f->dec) ? f->dec(path) : NULL;
}
bool tb_image_save(const TinyBlocks_Image* img, const char* path) {
const ImageFormat* f = format_for(path);
return (f && f->enc) ? f->enc(img, path) : false;
}
TinyBlocks_Value tb_value_image(TinyBlocks_Image* img) {
TinyBlocks_Value v = { .kind = TB_VALUE_IMAGE, .as.image = img };
if (img) tb_image_retain(img);