Adiciona tinyblocks-jpg (libjpeg, build condicional) e melhora o empacotamento AppImage
This commit is contained in:
1 parent
714186a1a1
commit
e36fbc43e1
6 files changed
+337
-7
No files matched your search
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* TinyBlocks - tb_jpg.c
|
||||
* Categoria "JPG": JPG.Load / JPG.Save (libjpeg). Decodifica para RGB e
|
||||
* codifica a partir de RGB.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#include <jpeglib.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_jpg.h"
|
||||
|
||||
static char s_err[200] = "";
|
||||
const char* tb_jpg_last_error(void) { return s_err; }
|
||||
|
||||
typedef struct {
|
||||
struct jpeg_error_mgr pub;
|
||||
jmp_buf jb;
|
||||
} JpgErr;
|
||||
|
||||
static void jpg_error_exit(j_common_ptr cinfo) {
|
||||
JpgErr* e = (JpgErr*)cinfo->err;
|
||||
char msg[JMSG_LENGTH_MAX];
|
||||
(*cinfo->err->format_message)(cinfo, msg);
|
||||
snprintf(s_err, sizeof(s_err), "%s", msg);
|
||||
longjmp(e->jb, 1);
|
||||
}
|
||||
|
||||
TinyBlocks_Image* tb_jpg_load(const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) { snprintf(s_err, sizeof(s_err), "nao abriu '%s'", path); return NULL; }
|
||||
|
||||
struct jpeg_decompress_struct cinfo;
|
||||
JpgErr jerr;
|
||||
cinfo.err = jpeg_std_error(&jerr.pub);
|
||||
jerr.pub.error_exit = jpg_error_exit;
|
||||
|
||||
TinyBlocks_Image* img = NULL;
|
||||
if (setjmp(jerr.jb)) {
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
if (img) tb_image_release(img);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
jpeg_create_decompress(&cinfo);
|
||||
jpeg_stdio_src(&cinfo, f);
|
||||
jpeg_read_header(&cinfo, TRUE);
|
||||
cinfo.out_color_space = JCS_RGB;
|
||||
jpeg_start_decompress(&cinfo);
|
||||
int w = (int)cinfo.output_width, h = (int)cinfo.output_height;
|
||||
img = tb_image_new(w, h, 3);
|
||||
if (!img) { snprintf(s_err, sizeof(s_err), "sem memoria"); jpeg_destroy_decompress(&cinfo); fclose(f); return NULL; }
|
||||
uint8_t* px = tb_image_data(img);
|
||||
int comps = cinfo.output_components;
|
||||
JSAMPARRAY row = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE,
|
||||
(JDIMENSION)w * (JDIMENSION)comps, 1);
|
||||
while (cinfo.output_scanline < cinfo.output_height) {
|
||||
JDIMENSION y = cinfo.output_scanline;
|
||||
jpeg_read_scanlines(&cinfo, row, 1);
|
||||
if (comps == 3) memcpy(px + (size_t)y * w * 3, row[0], (size_t)w * 3);
|
||||
else for (int x = 0; x < w; x++) {
|
||||
uint8_t v = row[0][x];
|
||||
px[((size_t)y*w + x)*3 + 0] = v;
|
||||
px[((size_t)y*w + x)*3 + 1] = v;
|
||||
px[((size_t)y*w + x)*3 + 2] = v;
|
||||
}
|
||||
}
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return img;
|
||||
}
|
||||
|
||||
bool tb_jpg_save(const TinyBlocks_Image* img, const char* path, int quality) {
|
||||
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; }
|
||||
if (quality < 1) quality = 1;
|
||||
if (quality > 100) quality = 100;
|
||||
|
||||
FILE* f = fopen(path, "wb");
|
||||
if (!f) { snprintf(s_err, sizeof(s_err), "nao criou '%s'", path); return false; }
|
||||
|
||||
struct jpeg_compress_struct cinfo;
|
||||
JpgErr jerr;
|
||||
cinfo.err = jpeg_std_error(&jerr.pub);
|
||||
jerr.pub.error_exit = jpg_error_exit;
|
||||
|
||||
uint8_t* rowbuf = (uint8_t*)malloc((size_t)w * 3);
|
||||
if (!rowbuf) { fclose(f); snprintf(s_err, sizeof(s_err), "sem memoria"); return false; }
|
||||
|
||||
bool ok = true;
|
||||
if (setjmp(jerr.jb)) {
|
||||
jpeg_destroy_compress(&cinfo);
|
||||
free(rowbuf); fclose(f);
|
||||
return false;
|
||||
}
|
||||
jpeg_create_compress(&cinfo);
|
||||
jpeg_stdio_dest(&cinfo, f);
|
||||
cinfo.image_width = (JDIMENSION)w;
|
||||
cinfo.image_height = (JDIMENSION)h;
|
||||
cinfo.input_components = 3;
|
||||
cinfo.in_color_space = JCS_RGB;
|
||||
jpeg_set_defaults(&cinfo);
|
||||
jpeg_set_quality(&cinfo, quality, TRUE);
|
||||
jpeg_start_compress(&cinfo, TRUE);
|
||||
while (cinfo.next_scanline < cinfo.image_height) {
|
||||
int y = (int)cinfo.next_scanline;
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
|
||||
rowbuf[x*3+0] = r; rowbuf[x*3+1] = g; rowbuf[x*3+2] = b;
|
||||
}
|
||||
JSAMPROW rp = rowbuf;
|
||||
jpeg_write_scanlines(&cinfo, &rp, 1);
|
||||
}
|
||||
jpeg_finish_compress(&cinfo);
|
||||
jpeg_destroy_compress(&cinfo);
|
||||
free(rowbuf);
|
||||
fclose(f);
|
||||
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 double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
|
||||
return 0;
|
||||
}
|
||||
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_jpg_load(in_text(self, rt, 0));
|
||||
if (!img) { tinyblocks_set_error(rt, "%s", tb_jpg_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_jpg_save(img, in_text(self, rt, 1), (int)in_real(self, rt, 2)))
|
||||
tinyblocks_set_error(rt, "%s", tb_jpg_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 jpg_encode_default(const TinyBlocks_Image* img, const char* path) {
|
||||
return tb_jpg_save(img, path, 90);
|
||||
}
|
||||
|
||||
static bool jpg_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"JPG",
|
||||
{ 239/255.f, 83/255.f, 80/255.f, 1.f},
|
||||
{ 198/255.f, 40/255.f, 40/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},{"qualidade", TB_VALUE_REAL}};
|
||||
|
||||
bool ok = reg(rt, lib, "JPG.Load", load_in, 1, out_img, 1, eval_load)
|
||||
&& reg(rt, lib, "JPG.Save", save_in, 3, NULL, 0, eval_save);
|
||||
if (ok) tb_image_register_format("jpg", tb_jpg_load, jpg_encode_default);
|
||||
return ok;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(jpg, "tinyblocks-jpg", "1.0.0", jpg_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_jpg_get_library(void) {
|
||||
return &_tb_lib_desc_jpg;
|
||||
}
|
||||
Reference in new issue
Block a user