making operators from garbage

This commit is contained in:
tth 2019-08-08 17:16:20 +02:00
parent 6258bd08ed
commit 016497c870
7 changed files with 172 additions and 18 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 $<

View File

@ -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;
}

View File

@ -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<height; line++) {
@ -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 Normal file
View File

@ -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
View File

@ -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);
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;
}