From 58b8fa878ce3ff2706c28fe56c897dd1f1664e87 Mon Sep 17 00:00:00 2001 From: tTh Date: Sun, 17 Nov 2024 10:44:07 +0100 Subject: [PATCH] add new ugly tools --- Tools/.gitignore | 2 + Tools/Makefile | 13 +- Tools/fractales.c | 384 ++++++++++++++++++++++++++++++++++++++++ Tools/fractales.h | 25 +++ Tools/tga_2x2contrast.c | 58 ++++++ Tools/tga_fractales.c | 128 ++++++++++++++ Tools/tga_resize.c | 145 +++++++++++++++ 7 files changed, 754 insertions(+), 1 deletion(-) create mode 100644 Tools/fractales.c create mode 100644 Tools/fractales.h create mode 100644 Tools/tga_2x2contrast.c create mode 100644 Tools/tga_fractales.c create mode 100644 Tools/tga_resize.c diff --git a/Tools/.gitignore b/Tools/.gitignore index bd4fa26..639d880 100644 --- a/Tools/.gitignore +++ b/Tools/.gitignore @@ -6,3 +6,5 @@ tga_extract tga_to_text tga_plothisto tga_plotmap +tga_resize +tga_fractales diff --git a/Tools/Makefile b/Tools/Makefile index 7c0223a..ac76921 100644 --- a/Tools/Makefile +++ b/Tools/Makefile @@ -13,7 +13,8 @@ all: genplot2 \ tga_combine tga_export tga_alpha tga_extract \ tga_television tga_dither tga_applymap tga_makehf15 \ tga_mires tga_incrust tga_pattern tga_equalize \ - tga_to_text + tga_to_text tga_resize tga_plotmap tga_plothisto \ + tga_fractales # 'tga_info.c' do not compile yet @@ -29,6 +30,16 @@ genplot2: genplot2.c $(DEPS) fonctions.o #----------------------------------------------------------------- +fractales.o: fractales.c fractales.h Makefile + gcc $(CFLAGS) -c $< + +tga_fractales: tga_fractales.c $(DEPS) fractales.o fonctions.o + gcc $(CFLAGS) $< fractales.o ../libtthimage.a fonctions.o \ + -lm -o $@ + +tga_resize: tga_resize.c $(DEPS) fonctions.o + gcc $(CFLAGS) $< ../libtthimage.a fonctions.o -lm -o $@ + tga_plotmap: tga_plotmap.c $(DEPS) fonctions.o gcc $(CFLAGS) $< ../libtthimage.a fonctions.o -lm -o $@ diff --git a/Tools/fractales.c b/Tools/fractales.c new file mode 100644 index 0000000..8ec1791 --- /dev/null +++ b/Tools/fractales.c @@ -0,0 +1,384 @@ +/* + fractales.c + =========== + + *....................................................* + . warning, this is a 'work in progress' . + . . + . don't trust prototypes for building . + . serious applications... . + *....................................................* + +*/ + +#include +#include +#include +#include + +#include + +#include "fractales.h" + +/*::------------------------------------------------------------------::*/ +void Image_fractales_print_version(int flag) +{ +printf("*** Fractales (%s) v 0.0.12\n", __FILE__); +if (flag) + { + printf("\tcompiled %s , %s\n", __DATE__, __TIME__); + } +} +/*::------------------------------------------------------------------::*/ +/* + * fonction de comparaison de deux variables 'double' + */ +int compare_double(double a, double b) +{ + +return 0; +} +/*::------------------------------------------------------------------::*/ +/* + * cette fonction a été repompé (sans honte :) du livre de + * Roger T. Stevens, "Fractal Programming in C" éditée par + * M&T Books. + * Ceci dit, elle est un peu trop monolithique, puisque l'équation + * à résoudre est codée en dur. + */ +int Newton_0(Image_Desc *im, int maxiter) +{ +int col, row, i; +double dcol, drow; +double Xmax, Xmin, Ymax, Ymin; +double deltaX, deltaY, denom; +double X, Y, Xsquare, Ysquare, Xold, Yold; + +Xmax = Ymax = 3.5; Xmin = Ymin = -3.5; +deltaX = (Xmax-Xmin)/(double)im->width; +deltaY = (Ymax-Ymin)/(double)im->height; + +for (col=0; colwidth; col++) + { + dcol = (double)col; + for (row=0; rowheight; row++) + { + drow = (double)row; + X = Xmin + dcol * deltaX; + Y = Ymin + drow * deltaY; + Xsquare = 0.0; + Ysquare = 0.0; + Xold = Yold = 42.0; + for (i=0; i 0) + { + Image_plotRGB(im, col, row, 0, 0, 0); + } + else + { + if (Y > 0) + Image_plotRGB(im, col, row, 255, 0, 0); + else + Image_plotRGB(im, col, row, 0, 0, 255); + } + } + if (0 == col%5) + fprintf(stderr, "newton 0: col %5d / %5d\r", col, im->width); + } + +return 0; +} +/*::------------------------------------------------------------------::*/ +/* + * C'est pas encore fini ce truc ? + */ +int GingerBreadMan(Image_Desc *im, int maxiter) +{ +double x1, y1, x2, y2; +int iter; + +x1 = 10.0; +y1 = 0.333; + +for (iter=0; iterwidth; + y = rand() % im->height; + + ra = Image_R_pixel(im, x, y); + ga = Image_G_pixel(im, x, y); + ba = Image_B_pixel(im, x, y); + + /* printf("\tP%d\t\tX%d\tY%d\t%d,%d,%d\n", pass, x, y, ra, ga, ba); */ + + for (step=0; stepwidth; + if (x>=im->width) x -= im->width; + y += (rand()%3)-1; + if (y<0) y += im->height; + if (y>=im->height) y -= im->height; + + rb = Image_R_pixel(im, x, y); + gb = Image_G_pixel(im, x, y); + bb = Image_B_pixel(im, x, y); + + Image_plotRGB(dst, x, y, + (ra+rb+rb)/3, (ga+gb+gb)/3, (ba+bb+bb)/3); + } + } + +return 0; +} + +/*::------------------------------------------------------------------::*/ + +#define XMIN (-1.5) +#define XMAX (1.5) +#define YMIN (-1.5) +#define YMAX (1.5) + +int Image_Julia_0_0(Image_Desc *im, RGB_map *pmap, int fuzz, + double cx, double cy) +{ +int col, row; +double deltaX, deltaY, dx, dy, x2, y2; +long debut, fin; +int iter, r, g, b, maxiter; + +debut = time(NULL); + +fprintf(stderr, "Julia_0_0: %dx%d\n", im->width, im->height); + +deltaX = (XMAX - XMIN) / im->width; +deltaY = (YMAX - YMIN) / im->height; + +maxiter = 0; + +/* + * si pas de palette définie, en créer une ? + */ +for (row=0; rowheight; row++) + { + dy = YMIN + (double)row * deltaY; + + for (col=0; colwidth; col++) + { +#if DEBUG_LEVEL + printf("%5d %5d ", row, col); +#endif + + dx = XMIN + (double)col * deltaX; + + iter = 0; + x2 = y2 = 0.0; + + while ( (iter < fuzz) && + ((x2 + y2) < 4.0) ) + { + x2 = dx * dx; + y2 = dy * dy; + dx = 2 * dx * dy + cx; + dy = x2 - y2 + cy; + iter++; + } + + if (iter > maxiter) + maxiter = iter; + + if (iter >= fuzz) + { + r = g = b = 255; + } + else + { + r = pmap->red[iter%pmap->nbre]; + g = pmap->green[iter%pmap->nbre]; + b = pmap->blue[iter%pmap->nbre]; + } + + Image_plotRGB(im, col, row, r, g, b); + +#if DEBUG_LEVEL + printf(" %3d \r", iter); +#endif + } + } + +fin = time(NULL); + +printf("duree du calcul: %ld secondes, maxiter: %d\n", fin-debut, maxiter); + +return 0; +} +/*::------------------------------------------------------------------::*/ + +#define XA -2.0 +#define XB 2.0 +#define YA -2.0 +#define YB 1.3 + +int Image_Mandelbrot_0(Image_Desc *image, int maxiter) +{ +int Y, X; +int r, g, b; +double zr, zi, fx, fy, real, imag, fiter, dist2; +long debut, fin, temps; /* pour chronometrer */ +int iter, out, foo; +double tmpr, tmpi; + +debut = time(NULL); + +out = 0; + +for (Y=0; Yheight; Y++) + { + fprintf(stderr, "ligne %4d sur %d\r", Y, image->height); + fy = (double)Y; + real = ( fy / (double)image->height * (YB-YA) ) + YA; + + for (X=0; Xwidth; X++) + { + fx = (double)X; + imag = ( fx / (double)image->width * (XB-XA) ) + XA; + + iter = 0; + zr = zi = 0.0; + + do + { + iter++; + + dist2 = zr*zr + zi*zi; + if (dist2 > 4.0) + { + out++; + break; + } + + /* + * calculer le mandelbrot + * ---------------------- + * la formule est: + * z0 = 0 + 0i + * z1 = z0^2 + c; + * ... + */ + tmpr = zr*zr - zi*zi; + tmpi = 2.0 * zr * zi; + + zr = tmpr + real; + zi = tmpi + imag; + + } while (iter < maxiter); + + fiter = (double)iter / (double)maxiter; + + Image_color_x(zr, zi, &r, &g, &b); + Image_plotRGB(image, X, Y, r, g, b); + + } + + fflush(stderr); + } + +fin = time(NULL); + +temps = fin - debut; +if (temps < 500) + { + foo = 's'; + } +else + { + foo = 'm'; + temps /= 60; + } + +printf("\nMandelbrot_0: temps d'execution = %ld %c\n", temps, foo); +printf("Mandelbrot_0: nombre de points dehors = %d\n", out); + +return 0; +} +/*::------------------------------------------------------------------::*/ +int Lorenz_Orbits(Image_Desc *img, int iters, + double a, double b, double c, double dt) +{ +int pass; +double dx, dy, dz, dxp, dyp, dzp; +FILE *fp; + +if (NULL == (fp=fopen("lorenz.dat", "w"))) { + perror("lorenz datafile"); + exit(1); + } + +fprintf(stderr, "%s ( %p %d / %f %f %f %f )\n", __func__, img, iters, + a, b, c, dt); + +dx = dy = dz = 0.0001; + +for (pass=0; pass +#include + +#include "tga_outils.h" + +/*::------------------------------------------------------------------::*/ +void usage(void) +{ +fprintf(stderr, "* tga_2x2contrast v 0.18 [%s] %s\n", + TGA_OUTILS_VERSION, TGA_OUTILS_COPYLEFT); +fprintf(stderr, "Usage:\n\ttga_2x2contrast \n"); +Image_print_version(0); +exit(5); +} +/*::------------------------------------------------------------------::*/ + +int main(int argc, char *argv[]) +{ +Image_Desc *src, *dst; +int foo; + +dump_command_line(argc, argv, 0); + +if (argc != 3) + usage(); + +if ( (src=Image_TGA_alloc_load(argv[1])) == NULL ) + { + fprintf(stderr, "tga_2x2contrast: can't load \"%s\" ...\n", argv[2]); + exit(1); + } + +if ( (dst=Image_clone(src, 1)) == NULL ) + { + exit(1); + } + +foo = Image_2x2_contrast(src, dst); + +if (foo) + { + fprintf(stderr, "tga_2x2contrast: %d / %s\n", foo, Image_err2str(foo)); + } + +foo = Image_TGA_save(argv[2], dst, 0); + +exit (0); +} +/*::------------------------------------------------------------------::*/ diff --git a/Tools/tga_fractales.c b/Tools/tga_fractales.c new file mode 100644 index 0000000..78bb024 --- /dev/null +++ b/Tools/tga_fractales.c @@ -0,0 +1,128 @@ +/* + FRACTALES + --------- + + Il reste beaucoup de travail sur cet outil: + + - plasma + - popcorn + - newton: oct 2003, c'est en route. janv 2010 pas fini. +*/ + +#include +#include +#include + +#include "tga_outils.h" +#include "fractales.h" + +/*::------------------------------------------------------------------::*/ +#define MANDEL0 1 +#define JULIA00 2 +#define GINGER 3 +#define NEWTON0 10 +#define NEWTON1 11 +#define LORENZ0 12 + +mot_clef mots_clef[] = +{ +{ "ginger", GINGER, "ii", "width & height" }, +{ "julia00", JULIA00, "ii", "width & height" }, +{ "mandel0", MANDEL0, "ii", "width & height" }, +{ "newton0", NEWTON0, "ii", "width & height" }, +{ "lorenz0", LORENZ0, "ii", "W & H" }, +{ NULL, 0, NULL, NULL } +}; + +/*::------------------------------------------------------------------::*/ +void usage(int flag) +{ +Image_fractales_print_version(flag); +fprintf(stderr, "usage:\n\ttga_fractales type nom.tga [params]\n"); +if (flag) + { + Image_print_version(1); + liste_mots_clefs(mots_clef, 42); + } +exit(1); +} +/*::------------------------------------------------------------------::*/ +/* + * argv[1]: type fractale + * argv[2]: nom fichier.tga + */ +#define FIRST_PAR 3 +int main (int argc, char *argv[]) +{ +Image_Desc *dst; +int foo, width, height; +int commande, nbargs, idx; +RGB_map map; + +dump_command_line(argc, argv, 0); +if ( argc==2 && !strcmp(argv[1], "list") ) usage(1); +if ( argc < 3 ) usage(0); + +idx = cherche_mot_clef(argv[1], mots_clef, &commande, &nbargs); +if (idx < 0) + { + fprintf(stderr, "tga_fractales: mot-clef %s inconnu...\n", argv[1]); + exit(5); + } +#if DEBUG_LEVEL +fprintf(stderr, "code commande %d\n", commande); +#endif + +foo = parse_parametres(argc, argv, mots_clef[idx].ptypes, FIRST_PAR); + +dst = NULL; /* gcc warning STFU */ + +switch (commande) + { + case MANDEL0: + width = GIP(0); height = GIP(1); + printf("mandelbrot 0: %dx%d\n", width, height); + dst = Image_alloc(width, height, 3); + foo = Image_Mandelbrot_0(dst, 300); + Image_print_error("mandelbrot 0", foo); + break; + case JULIA00: + width = GIP(0); height = GIP(1); + foo = Image_load_color_Map("volcano.map", "plop", &map); + Image_print_error("julia00: loadmap", foo); + dst = Image_alloc(width, height, 3); + Image_Julia_0_0(dst, &map, 3000, 0.333, 0.333); + Image_print_error("julia00: calcul", foo); + break; + case GINGER: + width = GIP(0); height = GIP(1); + printf("ginger bread man: %dx%d\n", width, height); + dst = Image_alloc(width, height, 3); + foo = GingerBreadMan(dst, 300); + Image_print_error("GingerBreadMan", foo); + break; + case NEWTON0: + width = GIP(0); height = GIP(1); + printf("newton 0: %dx%d\n", width, height); + dst = Image_alloc(width, height, 3); + foo = Newton_0(dst, 42); + Image_print_error("proto Newton 0", foo); + break; + case LORENZ0: + width = GIP(0); height = GIP(1); + printf("lorenz 0: %dx%d\n", width, height); + dst = Image_alloc(width, height, 3); + foo = Lorenz_Orbits(dst, 3000, 5.0, 15.0, 1.0, 0.02); + Image_print_error("lorenz 0", foo); + break; + default: + fprintf(stderr, "Cette fractale n'est pas disponible.\n"); + exit(1); + break; + } + +foo = Image_TGA_save(argv[2], dst, 0); + +return 0; +} +/*::------------------------------------------------------------------::*/ diff --git a/Tools/tga_resize.c b/Tools/tga_resize.c new file mode 100644 index 0000000..7b2f6c2 --- /dev/null +++ b/Tools/tga_resize.c @@ -0,0 +1,145 @@ +/* + ............................... + .redimensionnement d'une image. + ............................... +*/ + +#include +#include +#include + + +#include "tga_outils.h" + +/*::------------------------------------------------------------------::*/ +#define HALF_SIZE 1 +#define DOUBLE_SIZE 2 +#define PERCENT 3 +#define NEWSIZE 4 + +mot_clef mots_clef[] = +{ +{ "half", HALF_SIZE, "f", "0:crude 1:interpolate" }, +{ "double", DOUBLE_SIZE, "f", "" }, +{ "percent", PERCENT, "dii", "%%size, method, 0" }, +{ "newsize", NEWSIZE, "iii", "newW newH method" } +}; +/*::------------------------------------------------------------------::*/ +void usage(int flag) +{ +fprintf(stderr, "* Tga Resize v 0.0.13 [%s] %s\n", + TGA_OUTILS_VERSION, TGA_OUTILS_COPYLEFT); + +fprintf(stderr, "usage:\n"); +fprintf(stderr, "\ttga_resize source.tga methode resultat.tga [params]\n"); +if (flag) + liste_mots_clefs(mots_clef, 42); +else + Image_print_version(0); +exit(5); +} +/*::------------------------------------------------------------------::*/ +/* + * argv[1] source.tga + * argv[2] méthode + * argv[3] destination.tga + */ +#define FIRST_PARAM 4 +int main(int argc, char *argv[]) +{ +Image_Desc *src, *dst; +int foo, idx; +int commande, nbargs, interpol; +double scale, scaleX, scaleY; +int newW, newH; + +dump_command_line(argc, argv, 0); + +if ( argc==2 && !strcmp(argv[1], "list") ) usage(1); +if ( argc < 4) usage(0); + +/* + * rechercher le mot clef dans la table + */ +idx = cherche_mot_clef(argv[2], mots_clef, &commande, &nbargs); +if (idx < 0) + { + fprintf(stderr, "keyword %s unknow\n", argv[1]); + exit(5); + } + +/* + * décodage des paramètres + */ +foo = parse_parametres(argc, argv, mots_clef[idx].ptypes, FIRST_PARAM); +#if DEBUG_LEVEL +fprintf(stderr, "tga_resize: ret parse parameters = %d\n", foo); +#endif + +if ((src = Image_TGA_alloc_load(argv[1]))==NULL) + { + fprintf(stderr, "tga_resize: can't load '%s'\n", argv[1]); + exit(5); + } + +dst = NULL; /* secure the default case */ + +switch (commande) + { + case HALF_SIZE: + if ( (dst=Image_MakeHalfSize(src, GFP(0))) == NULL) + { + fprintf(stderr, "tga_resize: half: fatal error\n"); + exit(1); + } + foo = 0; + break; + + case DOUBLE_SIZE: + if ( (dst=Image_MakeDoubleSize(src, GFP(0))) == NULL) + { + fprintf(stderr, "tga_resize: double: fatal error\n"); + exit(1); + } + foo = 0; + break; + + case PERCENT: + scale = GDP(0) / 100.0; + interpol = GIP(1); +#if DEBUG_LEVEL + printf(" scale = %f\n", scale); + printf(" interpol = %d\n", interpol); +#endif + if ((dst = Image_new_scale(src, scale, scale, interpol))==NULL) + { + fprintf(stderr, "tga_resize: percent: fatal error\n"); + exit(1); + } + break; + + case NEWSIZE: + newW = GIP(0); + newH = GIP(1); + printf("new size: %d x %d\n", newW, newH); + scaleX = ((double)newW / (double)src->width); + scaleY = ((double)newH / (double)src->height); + printf("scales: X %f y %f\n", scaleX, scaleY); + + if ((dst = Image_new_scale(src, scaleX, scaleY, GIP(2)))==NULL) + { + fprintf(stderr, "tga_resize: newsize: fatal error\n"); + exit(1); + } + break; + } + +/* + * sauvons vite ce precieux resultat + */ +if (dst != NULL) + foo = Image_TGA_save(argv[3], dst, 0); + +return 0; +} +/*::------------------------------------------------------------------::*/