From 10dc0d2204efe2f5c40265b000c3e7cbaf1595f3 Mon Sep 17 00:00:00 2001 From: tth Date: Thu, 30 Jun 2022 20:33:31 +0200 Subject: [PATCH] some small twiks --- .gitignore | 2 + Lib/Makefile | 6 +- Lib/alpha.c | 296 +++++++++++++++++++++++++++++++++++++++++++ Lib/operat.c | 2 + Lib/png.c | 36 +++++- Lib/t_png.c | 39 ++++++ Tools/README.md | 5 + Tools/tga_equalize.c | 14 +- Tools/tga_outils.h | 2 +- tthimage.h | 2 +- 10 files changed, 391 insertions(+), 13 deletions(-) create mode 100644 Lib/alpha.c create mode 100644 Lib/t_png.c diff --git a/.gitignore b/.gitignore index dc9b7a5..6c88bc0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,11 +5,13 @@ libtthimage.a Lib/foo Lib/testtga +Lib/t_png Lib/t_t16x24 Lib/testbmp Tools/genplot2 +Tools/tga_alpha Tools/tga_cadre Tools/tga_combine Tools/tga_dither diff --git a/Lib/Makefile b/Lib/Makefile index 9598e07..9e422fb 100644 --- a/Lib/Makefile +++ b/Lib/Makefile @@ -93,6 +93,7 @@ pht.o: pht.c $(DEPS) pixeliz.o: pixeliz.c $(DEPS) pixels.o: pixels.c $(DEPS) plotteur.o: plotteur.c $(DEPS) +png.o: png.c $(DEPS) pnm.o: pnm.c $(DEPS) pov_hf15a.o: pov_hf15a.c $(DEPS) pov_hf15b.o: pov_hf15b.c $(DEPS) @@ -155,7 +156,7 @@ OBJECTS = 7seg.o \ palettes.o \ patterns.o patterns2.o patterns3.o patterns4.o \ photomaton.o pht.o pixeliz.o pixels.o plotteur.o \ - pnm.o \ + pnm.o png.o \ pov_hf15a.o pov_hf15b.o pov_hf15c.o pov_hf15d.o \ pov_hf15e.o pov_hf15e.o pov_hf15f.o pov_synth.o \ ptlist.o \ @@ -180,6 +181,9 @@ foo: foo.c $(DEPS) ../libtthimage.a t_t16x24: t_t16x24.c $(DEPS) ../libtthimage.a gcc $(CFLAGS) $< ../libtthimage.a -o $@ +t_png: t_png.c $(DEPS) ../libtthimage.a + gcc $(CFLAGS) $< ../libtthimage.a -lpng -lz -o $@ + testbmp: testbmp.c $(DEPS) ../libtthimage.a gcc $(CFLAGS) $< ../libtthimage.a -lm -o $@ diff --git a/Lib/alpha.c b/Lib/alpha.c new file mode 100644 index 0000000..04e2a91 --- /dev/null +++ b/Lib/alpha.c @@ -0,0 +1,296 @@ +/* + alpha.c + ------- + Various transparency operations, aka "alpha channel ops" + + -------------------------------------- + Et c,a va me forcer a installer libpng + -------------------------------------- + 15May01: on attend toujours libPNG + 07Dec01: pfeue, libPNG toujours pas lŕ... + 03Fev14: ... please wait more ... +*/ + +#include +#include +#include +#include "../tthimage.h" + +/*::------------------------------------------------------------------::*/ +int +Image_RGBA_over_RGB(Image_Desc *rgba, Image_Desc *src, Image_Desc *dst) +{ +int x, y; +int rs, gs, bs, rm, gm, bm, am, r, g, b; +int foo; + +if (rgba->type != IMAGE_RGBA) + { + fprintf(stderr, "RGBA over RGB: image is not RGBA (%d)\n", rgba->type); + return IMAGE_BAD_TYPE; + } +if ( (foo=Image_compare_desc(rgba, src)) ) + { + fprintf(stderr, "RGBA over RGB: err on sources: %s\n", + Image_err2str(foo)); + return foo; + } + +#if DEBUG_LEVEL +fprintf(stderr, "RGBA/RGB types: %d %d %d\n", rgba->type, src->type, dst->type); +#endif + +for (y=0; yheight; y++) + { + for (x=0; xwidth; x++) + { + rs = (src->Rpix[y])[x]; + gs = (src->Gpix[y])[x]; + bs = (src->Bpix[y])[x]; + Image_getRGBA(rgba, x, y, &rm, &gm, &bm, &am); + + r = ((rs*(255-am)) + (rm*am)) / 256; + g = ((gs*(255-am)) + (gm*am)) / 256; + b = ((bs*(255-am)) + (bm*am)) / 256; + + Image_plotRGB(dst, x, y, r, g, b); + } + } + +return FUNC_IS_BETA; +} +/*::------------------------------------------------------------------::*/ +/* + * this function do some dirty things with the internals + * of the library. And you got here a 'secret' prototype. + */ +int Image_alloc_pixels(unsigned char ***pix, int w, int h); +/* + * Ahem. You, if you use this prototype, you are going + * to be a big laM3r. Don't do it. You have be warned. + */ +int +Image_add_alpha_channel(Image_Desc *img, int value) +{ +uint8_t **tmp_ptr; +int foo; + +#if DEBUG_LEVEL +fprintf(stderr, "Add Alpha Channel v=%d to Img %p\n", value, img); +#endif + +if (img == NULL) + { + fprintf(stderr, "Add Alpha Channel: ZarWa, 'img' is NULL\n"); + return NULL_DESCRIPTOR; + } + +if (img->Apix != NULL) + { + fprintf(stderr, "Image %p already has an alpha channel\n", img); + return VERY_STRANGE; + } + +if (img->type != IMAGE_RGB) + { + fprintf(stderr, "Image %p is not RGB.\n", img); + return VERY_STRANGE; + } + +foo = Image_alloc_pixels(&tmp_ptr, img->width, img->height); +#if DEBUG_LEVEL +fprintf(stderr, "creation channel alpha foo=%d ptr=%p\n", foo, tmp_ptr); +#endif + +/* Yo, le plan memoire est disponible, on y poke la 'value' */ +for (foo=0; fooheight; foo++) { + memset(tmp_ptr[foo], value, img->width); + } + +/* and now, we update the image descriptor */ +img->type = IMAGE_RGBA; +img->nb_planes = 4; +img->Apix = tmp_ptr; +img->modified = 1; + +#if DEBUG_LEVEL +Image_dump_descriptor(img, "apres creation canal Alpha"); +#endif + +return OLL_KORRECT; +} +/*::------------------------------------------------------------------::*/ +int +Image_kill_alpha_channel(Image_Desc *img) +{ +int line; + +#if DEBUG_LEVEL +Image_dump_descriptor(img, "Killing Alpha Channel"); +#endif + +if (img->type != IMAGE_RGBA) { +#if DEBUG_LEVEL + fprintf(stderr, "Kill Alpha Channel: bad type %d\n", img->type); +#endif + return VERY_STRANGE; + } + +if (img->nb_planes != 4) { + fprintf(stderr, "Kill Alpha Channel: bad planes number %d\n", + img->nb_planes); + return VERY_STRANGE; + } + +if (img->Apix == NULL) { + fprintf(stderr, "Kill Alpha Channel: _no_ alpha channel in %p\n", img); + return NO_ALPHA_CHANNEL; + } + +for (line=0; lineheight; line++) + if (img->Apix[line] != NULL) free(img->Apix[line]); + +free(img->Apix); img->Apix = NULL; +img->nb_planes = 3; +img->type = IMAGE_RGB; +img->modified = 1; + +return OLL_KORRECT; +} +/*::------------------------------------------------------------------::*/ +int +Image_map_alpha_on_img(Image_Desc *img) +{ +register int pix; +int x, y; + +if (img->Apix == NULL) + { + fprintf(stderr, "%s : %p have no alpha channel.\n", __func__, img); + return FULL_NUCKED; + } + +if (img->type != IMAGE_RGBA) + { + fprintf(stderr, "%s : %p is not an RGBA image.\n", __func__, img); + return FULL_NUCKED; + } + +for (y=0; yheight; y++) + { + for (x=0; xwidth; x++) + { + pix = (img->Rpix[y])[x] * (img->Apix[y])[x]; + (img->Rpix[y])[x] = pix / 256; + + pix = (img->Gpix[y])[x] * (img->Apix[y])[x]; + (img->Gpix[y])[x] = pix / 256; + + pix = (img->Bpix[y])[x] * (img->Apix[y])[x]; + (img->Bpix[y])[x] = pix / 256; + } + } + +return OLL_KORRECT; +} +/*::------------------------------------------------------------------::*/ +/* + * given two levels by RGB component, this func build an alpha + * binary mask where pixels in the 3 RGB intervals have the v1 + * alpha value, and all others have the v2 alpha value. + */ +int +Image_alpha_op_0(Image_Desc *src, Image_Desc *dst, int v1, int v2, + int rb, int rt, int gb, int gt, int bb, int bt, + int param) +{ +int x, y, foo, r, g, b; +int c1, c2; + +/* + XXX NO SAFETY CONTROLS ? +*/ +if (dst->Apix == NULL) + { + fprintf(stderr, "Alpha op 0: dst %p have no alpha channel.\n", dst); + return FULL_NUCKED; + } +if (0 != param) + { + fprintf(stderr, "%s: parameter must be 0\n", __func__); + } + +c1 = c2 = 0; +for (y=0; yheight; y++) + { + for (x=0; xwidth; x++) + { + foo = Image_getRGB(src, x, y, &r, &g, &b); + + if ( (r > rb && r < rt) || + (g > gb && g < gt) || + (b > bb && b < bt) ) + { + dst->Apix[y][x] = v1; + c1++; + } + else + { + dst->Apix[y][x] = v2; + c2++; + } + } + } + +#if DEBUG_LEVEL +fprintf(stderr, "Alpha op 0: c1=%d c2=%d\n", c1, c2); +#endif + +return FUNC_IS_BETA; +} +/*::------------------------------------------------------------------::*/ +/* + * New: 16 Oct 2001. + */ +int Image_copy_component_to_alpha(Image_Desc *img, char component) +{ +int x, y; + +if (img->type != IMAGE_RGBA) + { + fprintf(stderr, "Alpha copy comp: %p is not an RGBA image.\n", img); + return NO_ALPHA_CHANNEL; + } + +if (img->Apix == NULL) + { + fprintf(stderr, "Alpha copy comp: %p have no alpha channel.\n", img); + return NO_ALPHA_CHANNEL; + } + +#if DEBUG_LEVEL +fprintf(stderr, "moving component '%c' to alpha channel of %p\n", + component, img); +#endif + +for (y=0; yheight; y++) { + for (x=0; xwidth; x++) { + switch (component) + { + case 'r': case 'R': + img->Apix[y][x] = img->Rpix[y][x]; break; + case 'g': case 'G': + img->Apix[y][x] = img->Gpix[y][x]; break; + case 'b': case 'B': + img->Apix[y][x] = img->Bpix[y][x]; break; + } + } + } +return FUNC_IS_ALPHA; +} +/*::------------------------------------------------------------------::*/ +/* + * bon, et maintenant, qu'est-ce qu'on invente ? + * ben 'alpha2.c' ! + */ +/*::------------------------------------------------------------------::*/ diff --git a/Lib/operat.c b/Lib/operat.c index a6c5806..ebd6926 100644 --- a/Lib/operat.c +++ b/Lib/operat.c @@ -120,6 +120,8 @@ int x, y; fprintf(stderr, "%s ( %p %p %d )\n", __func__, src, dst, factor); #endif +fprintf(stderr, "luminance factor: %d %f\n", factor, (float)factor / 256.0); + if ( (foo=Image_compare_desc(src, dst)) ) { fprintf(stderr, "Image Luminance: images are differents %d\n", foo); return foo; diff --git a/Lib/png.c b/Lib/png.c index b69aa19..5d9ff97 100644 --- a/Lib/png.c +++ b/Lib/png.c @@ -2,23 +2,45 @@ PNG aka "portable network graphics"s. - - Un vrai merdier, en fait :( - */ #include #include #include - #include +#include -#include "tthimage.h" +#undef DEBUG_LEVEL +#define DEBUG_LEVEL 1 +#include "../tthimage.h" + +/* + * les infos et le code ici viennent de + * http://www.libpng.org/pub/png/pngbook.html + */ + + +/* static mainprog_info wpng_info; */ + + + +/*::------------------------------------------------------------------::*/ +static void writepng_version_info(void) +{ + fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n", + PNG_LIBPNG_VER_STRING, png_libpng_ver); + fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n", + ZLIB_VERSION, zlib_version); +} /*::------------------------------------------------------------------::*/ Image_Desc * Image_PNG_alloc_load(char *fname, int k) { +#if DEBUG_LEVEL +fprintf(stderr, "%s ( '%s' %d )\n", __func__, fname, k); +writepng_version_info(); +#endif return 666; } @@ -26,12 +48,12 @@ return 666; int Image_save_as_PNG(Image_Desc *img, char *fname, int p1, int p2) { FILE *fp; -int x, y; +int x, y, foo; png_uint_32 pngv; #if DEBUG_LEVEL fprintf(stderr, "%s ( %p '%s' %d %d )\n", __func__, img, fname, p1, p2); -fprintf(stderr, " png version -> %d\n", png_access_version_number()); +writepng_version_info(); #endif return FULL_NUCKED; diff --git a/Lib/t_png.c b/Lib/t_png.c new file mode 100644 index 0000000..bd9c7ad --- /dev/null +++ b/Lib/t_png.c @@ -0,0 +1,39 @@ +/* + * test des fonctionsPNG + */ + +#include +#include + +#include "../tthimage.h" + + +/*::------------------------------------------------------------------::*/ +/*::------------------------------------------------------------------::*/ +/*::------------------------------------------------------------------::*/ +int main(int argc, char *argv[]) +{ +Image_Desc *img; +int foo; +char *fname = "foo.png"; + +Image_print_version(0); + +writepng_version_info(); + +if (2==argc) { + fname = argv[1]; + } + +img = Image_alloc(640, 480, IMAGE_RGB); + +foo = Image_save_as_PNG(img, fname, 0, 0); +if (foo) { + fprintf(stderr, "save as '%s' -> %d\n", fname, foo); + exit(1); + } + +return 0; +} +/*::------------------------------------------------------------------::*/ + diff --git a/Tools/README.md b/Tools/README.md index 0fa028c..07efbbe 100644 --- a/Tools/README.md +++ b/Tools/README.md @@ -26,7 +26,10 @@ La colonne du milieu undique le type des paramètres : - `s` pour une chaine, ex: un nom de fichier - `f` pour un flag : 0, F, 1, T +## tga_alpha +Manipulation du canal alpha (la transparence), lequel canal +est globalement mal gĂ©rĂ© par l'ensemble de libtthimage. ## tga_applymap @@ -55,6 +58,8 @@ Attendu avec impatience, il aura le support complet des PNG. ## tga_mires +La gĂ©nĂ©ration de diverses image de test ou de calibration. + ## tga_pattern ## tga_remap diff --git a/Tools/tga_equalize.c b/Tools/tga_equalize.c index 0dbe77b..b102671 100644 --- a/Tools/tga_equalize.c +++ b/Tools/tga_equalize.c @@ -19,23 +19,26 @@ #define EQ_SQUARE 16 #define EQ_SQROOT 17 #define EQ_GAMMA 20 +#define EQ_MULT 21 + mot_clef mots_clef[] = { { "std", EQ_STD, "", "standard method" }, { "rgb", EQ_STD, "", "same as 'std'" }, { "gray", EQ_GRAY, "", "gray based" }, { "2x2", EQ_2X2, "", "2x2 matrix" }, -{ "lumin", EQ_LUMIN, "i", "param : ident is 256" }, -{ "gamma", EQ_GAMMA, "d", "not implemented" }, +{ "lumin", EQ_LUMIN, "i", "param: ident is 256" }, +/* { "gamma", EQ_GAMMA, "d", "not implemented" },*/ { "square", EQ_SQUARE, "", "pix**2" }, { "sqroot", EQ_SQROOT, "", "sqr(pix)" }, +{ "mult", EQ_MULT, "d", "multiply by const" }, { NULL, 0, NULL, NULL } }; /*::------------------------------------------------------------------::*/ void usage() { -fprintf(stderr, "* tga_equalize v 0.0.20 [%s] (dwtfywl) tonton Th\n", +fprintf(stderr, "* tga_equalize v 0.0.21 [%s] (dwtfywl) tonton Th\n", TGA_OUTILS_VERSION); fprintf(stderr, " compiled %s at %s\n", __DATE__, __TIME__); fprintf(stderr, "usage:\n\ttga_equalize avant.tga mode apres.tga [params]\n"); @@ -108,6 +111,11 @@ switch (commande) foo = FULL_NUCKED; break; + case EQ_MULT: + foo = PASTIS; + break; + + default: foo=-1; break; diff --git a/Tools/tga_outils.h b/Tools/tga_outils.h index 773f861..3556fe3 100644 --- a/Tools/tga_outils.h +++ b/Tools/tga_outils.h @@ -7,7 +7,7 @@ #include "../tthimage.h" -#define TGA_OUTILS_VERSION "0.59" +#define TGA_OUTILS_VERSION "0.60" /* * 13 Dec 2001: v0.11 a cause du 'mustopen' pour les palettes. * 11 Fev 2002: v0.12 a cause du '-ansi' (hein Kerdeuzz, on y vient) diff --git a/tthimage.h b/tthimage.h index 769ad92..5a3a066 100644 --- a/tthimage.h +++ b/tthimage.h @@ -4,7 +4,7 @@ http:///la.buvette.org/devel/libimage/ */ #ifndef IMAGE_VERSION_STRING - #define IMAGE_VERSION_STRING "0.4.51" + #define IMAGE_VERSION_STRING "0.4.51 pl 8" /*::------------------------------------------------------------------::*/ /*