diff --git a/doc/the_floatimg_hack.tex b/doc/the_floatimg_hack.tex index 1361e1e..48c7f9d 100644 --- a/doc/the_floatimg_hack.tex +++ b/doc/the_floatimg_hack.tex @@ -130,6 +130,24 @@ La première contient les choses qui sont relativement figées, et la seconde celles qui risquent de bouger. Cette classification est en fait arbitraire. +\subsection{Structures, macros\dots} + +\begin{verbatim} +/* + * in memory descriptor + */ +typedef struct { + int width; + int height; + int type; + float fval; + int count; + float *R, *G, *B, *A; + int reserved; + } FloatImg; +\end{verbatim}\index{FloatImg} + + \subsection{lib/}\index{lib/} Première chose, la gestion dynamique de la mémoire occupées diff --git a/floatimg.h b/floatimg.h index 37c9df3..9dc68b6 100644 --- a/floatimg.h +++ b/floatimg.h @@ -46,6 +46,11 @@ int fimg_plot_rgb (FloatImg *head, int x, int y, float r, float g, float b); int fimg_clear(FloatImg *fimg); int fimg_add_rgb(FloatImg *head, int x, int y, float r, float g, float b); +/* 'operats' module */ +int fimg_add(FloatImg *a, FloatImg *b, FloatImg *d); +int fimg_sub(FloatImg *a, FloatImg *b, FloatImg *d); +int fimg_mul(FloatImg *a, FloatImg *b, FloatImg *d); + /* PNM files module */ int fimg_save_as_pnm(FloatImg *head, char *fname, int notused); int fimg_load_from_pnm(char *fname, FloatImg *head, int notused); diff --git a/lib/Makefile b/lib/Makefile index 8324da0..bc01b17 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -4,7 +4,7 @@ COPT = -Wall -fpic -g -pg -no-pie -DDEBUG_LEVEL=0 OBJS = fimg-core.o fimg-pnm.o fimg-file.o fimg-math.o \ - fimg-timers.o + fimg-timers.o operators.o DEPS = Makefile ../floatimg.h # modify it 'as you like' @@ -13,7 +13,7 @@ AR=ar all: $(OBJS) ../libfloatimg.a t: t.c ../libfloatimg.a $(DEPS) - gcc $(COPT) $< ../libfloatimg.a -o $@ + gcc $(COPT) $< ../libfloatimg.a -lm -o $@ # -------------------------------------------- @@ -23,6 +23,9 @@ t: t.c ../libfloatimg.a $(DEPS) fimg-core.o: fimg-core.c $(DEPS) gcc $(COPT) -c $< +operators.o: operators.c $(DEPS) + gcc $(COPT) -c $< + fimg-pnm.o: fimg-pnm.c $(DEPS) gcc $(COPT) -c $< diff --git a/lib/fimg-file.c b/lib/fimg-file.c index 14b6e11..6289ad0 100644 --- a/lib/fimg-file.c +++ b/lib/fimg-file.c @@ -64,7 +64,7 @@ fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg, #endif if (3 != fimg->type) { - fprintf(stderr, "%s : bat type %d\n", __func__, fimg->type); + fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type); return -8; } diff --git a/lib/fimg-pnm.c b/lib/fimg-pnm.c index a4d3919..c36ce69 100644 --- a/lib/fimg-pnm.c +++ b/lib/fimg-pnm.c @@ -22,7 +22,7 @@ int fimg_load_from_pnm(char *fname, FloatImg *head, int notused) FILE *fp; int width, height, maxval; int foo, line, column; -unsigned char *buffline, *idxrd; +unsigned char *buffline, *idxrd, dummychar; float *Rptr, *Gptr, *Bptr; if (NULL==head) { @@ -56,7 +56,12 @@ if (foo) { exit(1); } +#if DEBUG_LEVEL +fread(&dummychar, 1, 1, fp); +fprintf(stderr, "%s : dummychar %xx\n", __func__, dummychar); +#else fseek(fp, 1L, SEEK_CUR); /* black magic */ +#endif Rptr = head->R; Gptr = head->G; Bptr = head->B; for (line=0; line>> %-25s ( %p '%s' %d )\n", __func__, head, fname, notused); #endif -if (head->type != 3) { +if (head->type != FIMG_TYPE_RGB) { #if DEBUG_LEVEL fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type); #endif diff --git a/lib/operators.c b/lib/operators.c new file mode 100644 index 0000000..b415a04 --- /dev/null +++ b/lib/operators.c @@ -0,0 +1,95 @@ +/* + * OPERATORS + * + * + */ + +#include +#include +#include +#include + +#include "../floatimg.h" + +extern int verbosity; /* must be declared around main() */ + +/* ---------------------------------------------------------------- */ +int fimg_add(FloatImg *a, FloatImg *b, FloatImg *d) +{ +int idx, nbiter; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d); +#endif + +if (3 != a->type || 3 != b->type || 3 != d->type) { + fprintf(stderr, "%s : got a bad type fimg\n", __func__); + return -8; + } + +nbiter = a->width * a->height; + +for (idx=0; idxR[idx] = a->R[idx] + b->R[idx]; + d->G[idx] = a->G[idx] + b->G[idx]; + d->B[idx] = a->B[idx] + b->B[idx]; + } + +return -1; +} +/* ---------------------------------------------------------------- */ +/* + * A - B -> D + */ +int fimg_sub(FloatImg *a, FloatImg *b, FloatImg *d) +{ +int idx, nbiter; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d); +#endif + +if (3 != a->type || 3 != b->type || 3 != d->type) { + fprintf(stderr, "%s : got a bad type fimg\n", __func__); + return -8; + } + +nbiter = a->width * a->height; + +for (idx=0; idxR[idx] = fabs(a->R[idx] - b->R[idx]); + d->G[idx] = fabs(a->G[idx] - b->G[idx]); + d->B[idx] = fabs(a->B[idx] - b->B[idx]); + } + +return -1; +} +/* ---------------------------------------------------------------- */ +/* ---------------------------------------------------------------- */ +/* + * A * B -> D + */ +int fimg_mul(FloatImg *a, FloatImg *b, FloatImg *d) +{ +int idx, nbiter; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d); +#endif + +if (3 != a->type || 3 != b->type || 3 != d->type) { + fprintf(stderr, "%s : got a bad type fimg\n", __func__); + return -8; + } + +nbiter = a->width * a->height; + +for (idx=0; idxR[idx] = a->R[idx] * b->R[idx]; + d->G[idx] = a->G[idx] * b->G[idx]; + d->B[idx] = a->B[idx] * b->B[idx]; + } + +return -1; +} +/* ---------------------------------------------------------------- */ diff --git a/lib/t.c b/lib/t.c index ebb7040..c409aae 100644 --- a/lib/t.c +++ b/lib/t.c @@ -2,35 +2,63 @@ #include #include #include +#include #include "../floatimg.h" int verbosity; +/* ---------------------------------------------------------------- */ +int petit_dessin(FloatImg *img) +{ +int x, y; +float r, g, b; + +for (y=0; yheight; y++) { + + r = (float)y / (float)img->height; + + for (x=0; xwidth; x++) { + + g = (float)x / (float)img->width; + b = 0.0;; + + fimg_plot_rgb(img, x, y, r, g, b); + + } + + } + +return -1; +} +/* ---------------------------------------------------------------- */ int main(int argc, char *argv[]) { int foo; -FloatImg fimg; +FloatImg dessin, noise, result; int datas[3]; -char *fname = "foo.fimg"; + verbosity = 1; -fimg_print_version(0); +fimg_print_version(1); -foo = fimg_create(&fimg, 640, 480, 3); -printf("retour fimg_create ---> %d\n", foo); +foo = fimg_create(&dessin, 640, 480, 3); +petit_dessin(&dessin); +fimg_save_as_pnm(&dessin, "dessin.pnm", 0); -fimg_printhead(&fimg); -fimg_describe(&fimg, "vroum"); +foo = fimg_create(&noise, 640, 480, 3); +fimg_drand48(&noise, 1.0); +fimg_save_as_pnm(&noise, "noise.pnm", 0); -// fimg_save_as_pnm(&fimg, "foo.pnm", 0); -foo = fimg_dump_to_file(&fimg, fname, 0); +foo = fimg_create(&result, 640, 480, 3); -foo = fimg_fileinfos("foo.fimg", datas); - -printf("%s : largeur %d hauteur %d type %d\n", - fname, datas[0], datas[1], datas[2]); +foo = fimg_add(&dessin, &noise, &result); +fimg_save_as_pnm(&result, "r_add.pnm", 0); +foo = fimg_sub(&dessin, &noise, &result); +fimg_save_as_pnm(&result, "r_sub.pnm", 0); +foo = fimg_mul(&dessin, &noise, &result); +fimg_save_as_pnm(&result, "r_mul.pnm", 0); return 0; }