Compare commits

..

No commits in common. "1c47b509542f4d96ca350a8eb9be0fa3d3db1dce" and "a07883c0c239b2d72ccaca589c28a327f0dd111b" have entirely different histories.

5 changed files with 71 additions and 112 deletions

25
essai.c
View File

@ -13,6 +13,29 @@
int verbosity; int verbosity;
/* --------------------------------------------------------------------- */
int add(FloatImg *a, FloatImg *b)
{
int x, y;
int offset;
double tb;
fimg_timer_set(0);
for (x=0; x<a->width; x++) {
for (y=0; y<a->height; y++) {
offset = x + (y * a->width);
a->R[offset] += b->R[offset];
a->G[offset] += b->G[offset];
a->B[offset] += b->B[offset];
}
}
tb = fimg_timer_get(0);
fprintf(stderr, "%s = %f seconds\n", __func__, tb);
return 0;
}
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
void fait_un_dessin(char *fname) void fait_un_dessin(char *fname)
{ {
@ -84,7 +107,7 @@ fimg_create(&fimgB, W, H, 3);
fimg_timer_set(0); fimg_timer_set(0);
fimg_drand48(&fimgB, 100.0); fimg_drand48(&fimgB, 100.0);
fimg_drand48(&fimgA, 100.0); fimg_drand48(&fimgA, 100.0);
fimg_add(&fimgA, &fimgB, &fimgA); add(&fimgA, &fimgB);
tb = fimg_timer_get(0); tb = fimg_timer_get(0);
fprintf(stderr, "%s = %f seconds\n", __func__, tb); fprintf(stderr, "%s = %f seconds\n", __func__, tb);
foo = fimg_save_as_pnm(&fimgA, "drand48.pnm", 0); foo = fimg_save_as_pnm(&fimgA, "drand48.pnm", 0);

View File

@ -13,7 +13,7 @@ extern int verbosity; /* must be declared around main() */
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
/* /*
* floating imgs MUST be allocated before calling this func. * floating img MUST be allocated.
*/ */
int fimg_mk_gray_from(FloatImg *src, FloatImg*dst, int k) int fimg_mk_gray_from(FloatImg *src, FloatImg*dst, int k)
{ {
@ -40,13 +40,14 @@ if (FIMG_TYPE_GRAY != dst->type) {
nbb = src->width * src->height; nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) { for (foo=0; foo<nbb; foo++) {
dst->R[foo] = ( (src->R[foo] * kr) + dst->R[foo] = ( (src->R[foo] * kr) +
(src->G[foo] * kg) + (src->G[foo] * kg) +
(src->B[foo] * kb) ) / (src->B[foo] * kb) ) /
kdiv; kdiv;
} }
return 0; return -1;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */

View File

@ -20,7 +20,7 @@ float fimg_get_maxvalue(FloatImg *head)
float maxval; float maxval;
int foo; int foo;
if (head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) { if (head->type != FIMG_TYPE_RGB) {
fprintf(stderr, "%s : type %d invalide\n", fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type); __func__, head->type);
return nanf("wtf ?"); return nanf("wtf ?");
@ -28,18 +28,11 @@ if (head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
maxval = 0.0; /* no negative values allowed */ maxval = 0.0; /* no negative values allowed */
switch (head->type) { for (foo=0; foo<(head->width*head->height); foo++) {
case FIMG_TYPE_RGB:
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo]; if (head->R[foo] > maxval) maxval = head->R[foo];
if (head->G[foo] > maxval) maxval = head->G[foo]; if (head->G[foo] > maxval) maxval = head->G[foo];
if (head->B[foo] > maxval) maxval = head->B[foo]; if (head->B[foo] > maxval) maxval = head->B[foo];
} }
case FIMG_TYPE_GRAY:
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo];
}
}
return maxval; return maxval;
} }
@ -66,9 +59,6 @@ for (idx=0; idx<4; idx++) means[idx] /= (float)surface;
return 0; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/*
* more elaborate functions are in fimg-2gray.c
*/
int fimg_to_gray(FloatImg *head) int fimg_to_gray(FloatImg *head)
{ {
float add; float add;

View File

@ -82,69 +82,19 @@ return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
static void dump_gray_values(FILE *fp, FloatImg *picz, float fk)
{
int cnt, sz, value;
int idx;
cnt = 0;
sz = picz->width * picz->height;
for (idx=0; idx<sz; idx++) {
if (fk > 0) value = (int)(picz->R[idx] / fk);
else value = 0;
cnt += fprintf(fp, "%d", value);
if (cnt > 70) {
fputs("\n", fp); cnt = 0;
}
else {
fputs(" ", fp); cnt++;
}
}
fputs("\n", fp);
}
/* ---------------------------------------------------------------- */
static void dump_rgb_values(FILE *fp, FloatImg *picz, float fk)
{
int cnt, sz, idx;
int Rv, Gv, Bv;
cnt = 0;
sz = picz->width * picz->height;
for (idx=0; idx<sz; idx++) {
if (fk > 0) {
Rv = (int)(picz->R[idx] / fk);
Gv = (int)(picz->G[idx] / fk);
Bv = (int)(picz->B[idx] / fk);
}
else {
Rv = Gv = Bv = 0;
}
cnt += fprintf(fp, "%d %d %d", Rv, Gv, Bv);
if (cnt > 60) {
fputs("\n", fp); cnt = 0;
}
else {
fputs(" ", fp); cnt++;
}
}
fputs("\n", fp);
}
/* ---------------------------------------------------------------- */
/*
*
*/
int fimg_save_as_pnm(FloatImg *head, char *fname, int notused) int fimg_save_as_pnm(FloatImg *head, char *fname, int notused)
{ {
FILE *fp; FILE *fp;
float maximum, fk; float maximum, fk;
char *code; int idx, sz, Rv, Gv, Bv, foo;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head, fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused); fname, notused);
#endif #endif
if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) { if (head->type != FIMG_TYPE_RGB) {
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type); fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif #endif
@ -153,32 +103,36 @@ if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
if (NULL==(fp=fopen(fname, "w"))) { if (NULL==(fp=fopen(fname, "w"))) {
perror(fname); perror(fname);
return -2; return -1;
} }
switch(head->type) { fprintf(fp, "P3\n%d %d\n", head->width, head->height);
case FIMG_TYPE_GRAY: code = "P2"; break;
case FIMG_TYPE_RGB: code = "P3"; break;
}
fprintf(fp, "%s\n%d %d\n", code, head->width, head->height);
maximum = fimg_get_maxvalue(head); maximum = fimg_get_maxvalue(head);
fprintf(fp, "# maxval %15f\n", maximum); fprintf(fp, "# maxval %15f\n", maximum);
fk = maximum / 65536.0; fk = maximum / 65536.0;
fprintf(fp, "# divisor %15f\n", fk); fprintf(fp, "# divisor %15f\n", fk);
fprintf(fp, "65535\n"); fprintf(fp, "65535\n");
fflush(fp); sz = head->width*head->height;
switch(head->type) { foo = 0;
case FIMG_TYPE_GRAY: for (idx=0; idx<sz; idx++) {
dump_gray_values(fp, head, fk); if (fk > 0) {
break; Rv = (int)(head->R[idx] / fk);
case FIMG_TYPE_RGB: Gv = (int)(head->G[idx] / fk);
dump_rgb_values(fp, head, fk); Bv = (int)(head->B[idx] / fk);
break; }
else {
Rv = Gv = Bv = 0;
}
foo += fprintf(fp, "%d %d %d", Rv, Gv, Bv);
if (foo > 60) {
fputs("\n", fp); foo = 0;
}
else {
fputs(" ", fp); foo++;
}
} }
fputs("\n", fp); fputs("\n", fp);
fclose(fp); fclose(fp);

43
lib/t.c
View File

@ -35,39 +35,20 @@ return -1;
int essai_2gray(FloatImg *picz, char *outname) int essai_2gray(FloatImg *picz, char *outname)
{ {
int foo;
FloatImg gray;
fprintf(stderr, ">>> %s ( %p '%s' )\n", __func__, picz, outname);
foo = fimg_create(&gray, picz->width, picz->height, FIMG_TYPE_GRAY);
if (foo) {
fprintf(stderr, "%s : err %d on fimg create\n", __func__, foo);
exit(1);
}
foo = fimg_mk_gray_from(picz, &gray, 0);
if (foo) {
fprintf(stderr, "%s : err %d on fimg mk_gray_from\n", __func__, foo);
exit(1);
}
foo = fimg_save_as_pnm(&gray, outname, 0); return -1;
if (foo) {
fprintf(stderr, "%s : err %d on save_as_pnm\n", __func__, foo);
exit(1);
}
fimg_destroy(&gray);
return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
#define W 2048 #define W 4000
#define H 2048 #define H 3000
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int foo; int foo;
FloatImg dessin, noise, result; FloatImg dessin, noise, result;
int datas[3];
verbosity = 1; verbosity = 1;
@ -75,14 +56,24 @@ fimg_print_version(1);
foo = fimg_create(&dessin, W, H, 3); foo = fimg_create(&dessin, W, H, 3);
petit_dessin(&dessin); petit_dessin(&dessin);
fimg_save_as_pnm(&dessin, "dessin.pnm", 0);
foo = fimg_create(&noise, W, H, 3); foo = fimg_create(&noise, W, H, 3);
fimg_drand48(&noise, 0.1); fimg_drand48(&noise, 1.0);
fimg_save_as_pnm(&noise, "noise.pnm", 0);
foo = fimg_create(&result, W, H, 3); foo = fimg_create(&result, W, H, 3);
foo = fimg_mul(&dessin, &noise, &result);
foo = fimg_add(&dessin, &noise, &result);
fimg_save_as_pnm(&result, "r_add.pnm", 0);
essai_2gray(&result, "gray.pnm"); essai_2gray(&result, "gray.pnm");
fimg_destroy(&dessin), fimg_destroy(&noise), fimg_destroy(&result);
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; return 0;
} }