Files
tinyblocks/src/tb_image.c
T
mguinhosandClaude Opus 5.5 80bd330766 Comentários: separadores com no máximo "---"
Trechos com quatro ou mais hífens dentro de comentários viram "---"
(strings não mudam).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 07:24:19 -03:00

244 lines
8.1 KiB
C

/*
* TinyBlocks - tb_image.c
* TinyBlocks_Image: buffer de pixels ref-counted com 1, 3 ou 4 canais.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tinyblocks/tb_image.h"
struct TinyBlocks_Image {
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
int width;
int height;
int channels; /* 1 = cinza, 3 = RGB, 4 = RGBA */
uint8_t* pixels; /* width * height * channels */
};
static int valid_channels(int c) { return c == 1 || c == 3 || c == 4; }
static void image_destroy(TinyBlocks_Object* o) {
TinyBlocks_Image* img = (TinyBlocks_Image*)o;
free(img->pixels);
free(img);
}
static const TinyBlocks_ObjectClass s_image_class = { "imagem", image_destroy };
TinyBlocks_Image* tb_image_new(int w, int h, int channels) {
if (w <= 0 || h <= 0 || !valid_channels(channels)) return NULL;
TinyBlocks_Image* img = (TinyBlocks_Image*)calloc(1, sizeof(*img));
if (!img) return NULL;
tb_object_init(&img->obj, &s_image_class);
size_t n = (size_t)w * (size_t)h * (size_t)channels;
img->pixels = (uint8_t*)calloc(n ? n : 1, 1);
if (!img->pixels) { free(img); return NULL; }
img->width = w;
img->height = h;
img->channels = channels;
return img;
}
void tb_image_retain(TinyBlocks_Image* img) { tb_object_retain(img); }
void tb_image_release(TinyBlocks_Image* img) { tb_object_release(img); }
TinyBlocks_Image* tb_image_clone(const TinyBlocks_Image* img) {
if (!img) return NULL;
TinyBlocks_Image* c = tb_image_new(img->width, img->height, img->channels);
if (!c) return NULL;
size_t n = (size_t)img->width * (size_t)img->height * (size_t)img->channels;
memcpy(c->pixels, img->pixels, n);
return c;
}
int tb_image_width (const TinyBlocks_Image* img) { return img ? img->width : 0; }
int tb_image_height (const TinyBlocks_Image* img) { return img ? img->height : 0; }
int tb_image_channels(const TinyBlocks_Image* img) { return img ? img->channels : 0; }
uint8_t* tb_image_data (TinyBlocks_Image* img) { return img ? img->pixels : NULL; }
const uint8_t* tb_image_data_const(const TinyBlocks_Image* img) { return img ? img->pixels : NULL; }
static bool in_bounds(const TinyBlocks_Image* img, int x, int y) {
return img && x >= 0 && y >= 0 && x < img->width && y < img->height;
}
bool tb_image_set_pixel(TinyBlocks_Image* img, int x, int y,
uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
if (!in_bounds(img, x, y)) return false;
uint8_t* p = img->pixels + ((size_t)y * img->width + x) * img->channels;
switch (img->channels) {
case 1: { unsigned v = (r * 299u + g * 587u + b * 114u) / 1000u; p[0] = (uint8_t)v; break; }
case 3: p[0] = r; p[1] = g; p[2] = b; break;
case 4: p[0] = r; p[1] = g; p[2] = b; p[3] = a; break;
default: return false;
}
return true;
}
bool tb_image_get_pixel(const TinyBlocks_Image* img, int x, int y,
uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) {
if (!in_bounds(img, x, y)) return false;
const uint8_t* p = img->pixels + ((size_t)y * img->width + x) * img->channels;
uint8_t rr = 0, gg = 0, bb = 0, aa = 255;
switch (img->channels) {
case 1: rr = gg = bb = p[0]; aa = 255; break;
case 3: rr = p[0]; gg = p[1]; bb = p[2]; aa = 255; break;
case 4: rr = p[0]; gg = p[1]; bb = p[2]; aa = p[3]; break;
default: return false;
}
if (r) *r = rr;
if (g) *g = gg;
if (b) *b = bb;
if (a) *a = aa;
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;
}
/* == Armazem de imagens nomeadas ========================================
* Cada imagem carregada ganha um NOME (nome do arquivo, com e sem extensao)
* e um NUMERO (ordem de entrada). Os blocos "desenhe imagem nome/numero"
* usam isto para achar a imagem depois. */
typedef struct {
char name[64];
TinyBlocks_Image* img;
} TbStoredImage;
static TbStoredImage s_store[TB_IMAGE_STORE_MAX];
static int s_store_n = 0;
static const char* store_base(const char* p) {
const char* b = strrchr(p, '/');
return b ? b + 1 : p;
}
int tb_image_store_put(const char* name, TinyBlocks_Image* img) {
if (!img) return 0;
const char* nm = (name && name[0]) ? name : "imagem";
for (int i = 0; i < s_store_n; i++) {
if (strcmp(s_store[i].name, nm) == 0) {
tb_image_release(s_store[i].img);
tb_image_retain(img);
s_store[i].img = img;
return i + 1;
}
}
if (s_store_n >= TB_IMAGE_STORE_MAX) return 0;
TbStoredImage* e = &s_store[s_store_n];
memset(e, 0, sizeof(*e));
snprintf(e->name, sizeof(e->name), "%s", nm);
tb_image_retain(img);
e->img = img;
s_store_n++;
return s_store_n;
}
TinyBlocks_Image* tb_image_store_get(const char* name) {
if (!name || !name[0]) return NULL;
for (int i = 0; i < s_store_n; i++)
if (strcmp(s_store[i].name, name) == 0) return s_store[i].img;
/* aceita com/sem extensao e com caminho */
const char* base = store_base(name);
char stem[64];
snprintf(stem, sizeof(stem), "%s", base);
char* dot = strrchr(stem, '.');
if (dot && dot != stem) *dot = 0;
for (int i = 0; i < s_store_n; i++) {
char s2[64];
snprintf(s2, sizeof(s2), "%s", s_store[i].name);
char* d2 = strrchr(s2, '.');
if (d2 && d2 != s2) *d2 = 0;
if (strcmp(s_store[i].name, base) == 0 || strcmp(s2, base) == 0 ||
strcmp(s_store[i].name, stem) == 0 || strcmp(s2, stem) == 0)
return s_store[i].img;
}
return NULL;
}
TinyBlocks_Image* tb_image_store_at(int number) {
if (number < 1 || number > s_store_n) return NULL;
return s_store[number - 1].img;
}
int tb_image_store_count(void) { return s_store_n; }
const char* tb_image_store_name(int number) {
if (number < 1 || number > s_store_n) return "";
return s_store[number - 1].name;
}
void tb_image_store_clear(void) {
for (int i = 0; i < s_store_n; i++) tb_image_release(s_store[i].img);
memset(s_store, 0, sizeof(s_store));
s_store_n = 0;
}
TinyBlocks_Value tb_value_image(TinyBlocks_Image* img) {
TinyBlocks_Value v = { .kind = TB_VALUE_IMAGE, .as.image = img };
if (img) tb_image_retain(img);
return v;
}
TinyBlocks_Image* tb_value_as_image(TinyBlocks_Value v) {
return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL;
}