NEED MORE DEBUG

This commit is contained in:
tTh 2023-07-01 00:48:50 +02:00
parent 17a4cc4dd2
commit 7244b4c829

View File

@ -1,5 +1,5 @@
/* /*
* Modeles Numeriques de Terrain * Modeles Numeriques de Terrain -- UGLY CODE INSIDE !!
* *
*/ */
@ -7,51 +7,145 @@
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "../floatimg.h" #include "../floatimg.h"
int verbosity; int verbosity;
/* ------------------------------------------------------------------- */
/* for debug purpose */
int calcul_histo_gray(FloatImg *img, char *fname, int nbslots, float *pmax)
{
int offset, nbpix, ival;
FILE *fp;
float pixel, minp, maxp;
int *counts;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%s' %d )\n", __func__,
img, fname, nbslots);
#endif
if (FIMG_TYPE_GRAY != img->type) {
return -2;
}
/* allocate memory for histogram computation */
counts = calloc(nbslots, sizeof(int));
if (NULL == counts) {
fprintf(stderr, "malloc fail in %s\n", __func__);
abort();
}
nbpix = img->width * img->height;
minp = 1e10, maxp = -1e10;
for (offset=0; offset<nbpix; offset++) {
pixel = img->R[offset];
if (pixel < minp) minp = pixel;
if (pixel > maxp) maxp = pixel;
}
fprintf(stderr, " values = %g < %g\n", minp, maxp);
*pmax = maxp; /* copy value for the caller */
/* calcul de l'histogramme avec scaling */
for (offset=0; offset<nbpix; offset++) {
pixel = img->R[offset];
ival = (int)((pixel * (float)nbslots) / maxp);
counts[ival]++;
// fprintf(stderr, "%6d %10.6f %i\n", offset, pixel, ival);
}
if (NULL == (fp = fopen(fname, "w"))) {
perror(fname);
exit(1);
}
for (ival=0; ival<nbslots; ival++) {
fprintf(fp, "%6d %6d\n", ival, counts[ival]);
}
fclose(fp);
/* garbage collect stuff */
free(counts);
return 0;
}
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
/* /*
* First try * Second try - ahem?
*/ */
int brotche_mnt_style(FloatImg *src, FloatImg *dst) int brotche_mnt_style(FloatImg *src, FloatImg *dst)
{ {
int x, y; FloatImg tmp;
int x, y, foo;
int offset; int offset;
float z1, z2, z3, z4; float z1, z2, z3, z4;
float a, b, c; float a, b, c;
float pente; float pente, minp, maxp, seuil;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, src, dst); fprintf(stderr, ">>> %s ( %p %p )\n", __func__, src, dst);
#endif
#define W (src->width) #define W (src->width)
#define DX 1.0 #define DX 1.0
#define DY 1.0 #define DY 1.0
/* allocate a graylevel image for storing la 'pente' */
memset(&tmp, 0, sizeof(FloatImg));
foo = fimg_create(&tmp, src->width, src->height, FIMG_TYPE_GRAY);
if (foo) {
fprintf(stderr, "create tmp pic --> %d\n", foo);
return foo;
}
/* calcul de la pente : a vérifier ! */
for (y=0; y<(src->height-1); y++) { for (y=0; y<(src->height-1); y++) {
for (x=0; x<(src->width-1); x++) { for (x=0; x<(src->width-1); x++) {
offset = (y * src->width) + x; offset = (y * W) + x;
z1 = src->R[offset]; z1 = src->R[offset];
z2 = src->R[offset+1]; z2 = src->R[offset+1];
z3 = src->R[offset+W]; z3 = src->R[offset+W];
z4 = src->R[offset+W+1]; z4 = src->R[offset+W+1];
a = ( z1 + z2 + z3 + z4) / 4.0; a = ( z1 + z2 + z3 + z4) / 4.0;
b = (-z1 + z2 - z3 + z4) / 2.0 / DX; b = (-z1 + z2 - z3 + z4) / 2.0 / DX;
c = (-z1 - z2 + z3 + z4) / 2.0 / DY; c = (-z1 - z2 + z3 + z4) / 2.0 / DY;
pente = atanf(sqrt(b*b + c*c)); pente = atanf(sqrt(b*b + c*c));
tmp.R[offset] = pente;
dst->R[offset] = 0;
if (pente < 0.0) dst->G[offset] = pente;
else dst->B[offset] = pente;
} }
} }
foo = calcul_histo_gray(&tmp, "histogramme.data", 499, &maxp);
if (foo) fprintf(stderr, "<<< calcul histo -> %d\n", foo);
minp = 1e10;
seuil = 0.640 * maxp;
fprintf(stderr, "seuil = %f\n", seuil);
for (offset=0; offset<(src->width*src->height); offset++) {
pente = tmp.R[offset];
if (pente > seuil) {
if (pente < minp) minp = pente;
}
if (pente > maxp) maxp = pente;
}
fprintf(stderr, "minp --> %f maxp --> %f\n", minp, maxp);
/* recopie dans l'image destination avec translation hauteur */
for (offset=0; offset<(src->width*src->height); offset++) {
pente = tmp.R[offset] - minp;
if (pente < 0.0) pente = 0.0;
dst->R[offset] = pente;
dst->G[offset] = pente;
dst->B[offset] = pente;
}
/* clean the memory */
fimg_destroy(&tmp);
return 0; return 0;
} }
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
@ -61,15 +155,17 @@ FloatImg src, dst;
char *infile, *outfile; char *infile, *outfile;
int foo; int foo;
verbosity = 1; /* FIXME */
if (3 != argc) { if (3 != argc) {
fprintf(stderr, "%s need 2 args : infile & outfile\n", argv[0]); fprintf(stderr, "'%s' need 2 args : infile & outfile\n", argv[0]);
fimg_print_version(0); fimg_print_version(0);
exit(1); exit(1);
} }
infile = argv[1]; outfile = argv[2]; infile = argv[1]; outfile = argv[2];
fprintf(stderr,"--- working on %s\n", infile); if (verbosity) fprintf(stderr,"*** MNT %s -> %s\n", infile, outfile);
foo = fimg_create_from_dump(infile, &src); foo = fimg_create_from_dump(infile, &src);
if (foo) { if (foo) {
@ -82,6 +178,7 @@ if (foo) {
fprintf(stderr, "err %d cloning image\n", foo); fprintf(stderr, "err %d cloning image\n", foo);
exit(1); exit(1);
} }
fimg_clear(&dst);
foo = brotche_mnt_style(&src, &dst); foo = brotche_mnt_style(&src, &dst);
if (foo) { if (foo) {
@ -95,6 +192,10 @@ if (foo) {
exit(1); exit(1);
} }
/* clean the memory */
fimg_destroy(&src);
fimg_destroy(&dst);
return 0; return 0;
} }
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */