Browse Source

making operators from garbage

master
tth 3 years ago
parent
commit
016497c870
  1. 18
      doc/the_floatimg_hack.tex
  2. 5
      floatimg.h
  3. 7
      lib/Makefile
  4. 2
      lib/fimg-file.c
  5. 9
      lib/fimg-pnm.c
  6. 95
      lib/operators.c
  7. 54
      lib/t.c

18
doc/the_floatimg_hack.tex

@ -130,6 +130,24 @@ La première contient les choses qui sont relativement figées, @@ -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

5
floatimg.h

@ -46,6 +46,11 @@ int fimg_plot_rgb (FloatImg *head, int x, int y, float r, float g, float b); @@ -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);

7
lib/Makefile

@ -4,7 +4,7 @@ @@ -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 @@ -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) @@ -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 $<

2
lib/fimg-file.c

@ -64,7 +64,7 @@ fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg, @@ -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;
}

9
lib/fimg-pnm.c

@ -22,7 +22,7 @@ int fimg_load_from_pnm(char *fname, FloatImg *head, int notused) @@ -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) { @@ -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<height; line++) {
@ -89,7 +94,7 @@ fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head, @@ -89,7 +94,7 @@ fprintf(stderr, ">>> %-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

95
lib/operators.c

@ -0,0 +1,95 @@ @@ -0,0 +1,95 @@
/*
* OPERATORS
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#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; idx<nbiter; idx++) {
d->R[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; idx<nbiter; idx++) {
d->R[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; idx<nbiter; idx++) {
d->R[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;
}
/* ---------------------------------------------------------------- */

54
lib/t.c

@ -2,35 +2,63 @@ @@ -2,35 +2,63 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "../floatimg.h"
int verbosity;
/* ---------------------------------------------------------------- */
int petit_dessin(FloatImg *img)
{
int x, y;
float r, g, b;
for (y=0; y<img->height; y++) {
r = (float)y / (float)img->height;
for (x=0; x<img->width; 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);
verbosity = 1;
foo = fimg_create(&fimg, 640, 480, 3);
printf("retour fimg_create ---> %d\n", foo);
fimg_print_version(1);
fimg_printhead(&fimg);
fimg_describe(&fimg, "vroum");
foo = fimg_create(&dessin, 640, 480, 3);
petit_dessin(&dessin);
fimg_save_as_pnm(&dessin, "dessin.pnm", 0);
// fimg_save_as_pnm(&fimg, "foo.pnm", 0);
foo = fimg_dump_to_file(&fimg, fname, 0);
foo = fimg_create(&noise, 640, 480, 3);
fimg_drand48(&noise, 1.0);
fimg_save_as_pnm(&noise, "noise.pnm", 0);
foo = fimg_fileinfos("foo.fimg", datas);
foo = fimg_create(&result, 640, 480, 3);
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;
}

Loading…
Cancel
Save