Adiciona tinyblocks-bmp e tinyblocks-png (libpng)
This commit is contained in:
1 parent
959a4027c3
commit
fca7aa2f63
11 files changed
+574
-6
No files matched your search
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* TinyBlocks - tb_png.c
|
||||
* Categoria "PNG": PNG.Load / PNG.Save (libpng, API simplificada png_image).
|
||||
* As imagens sao sempre convertidas para RGBA em memoria, de modo que a
|
||||
* previa (balao) do editor funciona independente do formato de origem.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <png.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_png.h"
|
||||
|
||||
static char s_err[200] = "";
|
||||
const char* tb_png_last_error(void) { return s_err; }
|
||||
|
||||
TinyBlocks_Image* tb_png_load(const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
|
||||
png_image pv;
|
||||
memset(&pv, 0, sizeof(pv));
|
||||
pv.version = PNG_IMAGE_VERSION;
|
||||
if (!png_image_begin_read_from_file(&pv, path)) {
|
||||
snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
|
||||
return NULL;
|
||||
}
|
||||
pv.format = PNG_FORMAT_RGBA;
|
||||
size_t sz = PNG_IMAGE_SIZE(pv);
|
||||
uint8_t* buf = (uint8_t*)malloc(sz ? sz : 1);
|
||||
if (!buf) { png_image_free(&pv); snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
|
||||
if (!png_image_finish_read(&pv, NULL, buf, 0, NULL)) {
|
||||
snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
|
||||
free(buf); png_image_free(&pv); return NULL;
|
||||
}
|
||||
int w = (int)pv.width, h = (int)pv.height;
|
||||
png_image_free(&pv);
|
||||
TinyBlocks_Image* img = tb_image_new(w, h, 4);
|
||||
if (!img) { free(buf); snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
|
||||
memcpy(tb_image_data(img), buf, (size_t)w * h * 4);
|
||||
free(buf);
|
||||
return img;
|
||||
}
|
||||
|
||||
bool tb_png_save(const TinyBlocks_Image* img, const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!img || !path) { snprintf(s_err, sizeof(s_err), "imagem/caminho nulo"); return false; }
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
if (w <= 0 || h <= 0) { snprintf(s_err, sizeof(s_err), "imagem vazia"); return false; }
|
||||
size_t sz = (size_t)w * h * 4;
|
||||
uint8_t* buf = (uint8_t*)malloc(sz);
|
||||
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return false; }
|
||||
for (int y = 0; y < h; y++)
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
|
||||
uint8_t* p = buf + ((size_t)y * w + x) * 4;
|
||||
p[0] = r; p[1] = g; p[2] = b; p[3] = a;
|
||||
}
|
||||
png_image pv;
|
||||
memset(&pv, 0, sizeof(pv));
|
||||
pv.version = PNG_IMAGE_VERSION;
|
||||
pv.width = (png_uint_32)w;
|
||||
pv.height = (png_uint_32)h;
|
||||
pv.format = PNG_FORMAT_RGBA;
|
||||
bool ok = png_image_write_to_file(&pv, path, 0, buf, 0, NULL) != 0;
|
||||
if (!ok) snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
|
||||
free(buf);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* -- Blocos -------------------------------------------------------------- */
|
||||
|
||||
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
}
|
||||
static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL;
|
||||
}
|
||||
|
||||
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = tb_png_load(in_text(self, rt, 0));
|
||||
if (!img) { tinyblocks_set_error(rt, "%s", tb_png_last_error()); self->dirty = false; return; }
|
||||
tb_block_set_output(self, 0, tb_value_image(img));
|
||||
tb_image_release(img);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
if (!img || !tb_png_save(img, in_text(self, rt, 1)))
|
||||
tinyblocks_set_error(rt, "%s", tb_png_last_error());
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool png_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"PNG",
|
||||
{ 0/255.f, 172/255.f, 193/255.f, 1.f},
|
||||
{ 0/255.f, 131/255.f, 143/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc out_img[] = {{"imagem", TB_VALUE_IMAGE}};
|
||||
static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc save_in[] = {{"imagem", TB_VALUE_IMAGE}, {"caminho", TB_VALUE_TEXT}};
|
||||
|
||||
return reg(rt, lib, "PNG.Load", load_in, 1, out_img, 1, eval_load)
|
||||
&& reg(rt, lib, "PNG.Save", save_in, 2, NULL, 0, eval_save);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(png, "tinyblocks-png", "1.0.0", png_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_png_get_library(void) {
|
||||
return &_tb_lib_desc_png;
|
||||
}
|
||||
Reference in new issue
Block a user