Adiciona tipo imagem (core) + tinyblocks-ppm e previa de imagem no balao

This commit is contained in:
mguinhos committed 2026-10-20 21:04:41 -03:00
1 parent f794d8b8c7
commit 959a4027c3
16 files changed
+721 -10

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
/*
* TinyBlocks - tb_image.c
* TinyBlocks_Image: buffer de pixels ref-counted com 1, 3 ou 4 canais.
*/
#include <stdlib.h>
#include <string.h>
#include "tinyblocks/tb_image.h"
struct TinyBlocks_Image {
int refs;
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; }
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;
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->refs = 1;
img->width = w;
img->height = h;
img->channels = channels;
return img;
}
void tb_image_retain(TinyBlocks_Image* img) {
if (img) img->refs++;
}
void tb_image_release(TinyBlocks_Image* img) {
if (!img) return;
if (--img->refs > 0) return;
free(img->pixels);
free(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;
}
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;
}