X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fxonotic.git;a=blobdiff_plain;f=misc%2Ftools%2Ffft-normalmap-to-heightmap.c;h=909f591ff33667355fec0f52bebdcb5d77ab219d;hp=0c06f3dc74e06da915937bd95ef9054edd13ed2b;hb=6dcb9aea907314ace3b160940c9d7204ab02db2f;hpb=e75800ae0e008b79c013bf8cf45840baceca2a8f diff --git a/misc/tools/fft-normalmap-to-heightmap.c b/misc/tools/fft-normalmap-to-heightmap.c index 0c06f3dc..909f591f 100644 --- a/misc/tools/fft-normalmap-to-heightmap.c +++ b/misc/tools/fft-normalmap-to-heightmap.c @@ -38,6 +38,8 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, double scale, double offset) { int x, y; + int fx, fy; + int ffx, ffy; double nx, ny, nz; double v, vmin, vmax; #ifndef C99 @@ -48,9 +50,9 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex)); fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex)); fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex)); - fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE); - fftw_plan i22f2 = fftw_plan_dft_2d(w, h, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE); - fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE); + fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE); + fftw_plan i22f2 = fftw_plan_dft_2d(h, w, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE); + fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE); for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) @@ -86,23 +88,26 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) { - int fx = x; - int fy = y; + fx = x; + fy = y; if(fx > w/2) fx -= w; if(fy > h/2) fy -= h; + /* these must have the same sign as fx and fy (so ffx*fx + ffy*fy is nonzero), otherwise do not matter */ + ffx = fx; + ffy = fy; #ifdef C99 if(fx||fy) - freqspace1[(w*y+x)] = _Complex_I * (fx * freqspace1[(w*y+x)] + fy * freqspace2[(w*y+x)]) / (fx*fx + fy*fy) / TWO_PI; + freqspace1[(w*y+x)] = _Complex_I * (ffx * freqspace1[(w*y+x)] + ffy * freqspace2[(w*y+x)]) / (ffx*fx + ffy*fy) / TWO_PI; else freqspace1[(w*y+x)] = 0; #else if(fx||fy) { save = freqspace1[(w*y+x)][0]; - freqspace1[(w*y+x)][0] = -(fx * freqspace1[(w*y+x)][1] + fy * freqspace2[(w*y+x)][1]) / (fx*fx + fy*fy) / TWO_PI; - freqspace1[(w*y+x)][1] = (fx * save + fy * freqspace2[(w*y+x)][0]) / (fx*fx + fy*fy) / TWO_PI; + freqspace1[(w*y+x)][0] = -(ffx * freqspace1[(w*y+x)][1] + ffy * freqspace2[(w*y+x)][1]) / (ffx*fx + ffy*fy) / TWO_PI; + freqspace1[(w*y+x)][1] = (ffx * save + ffy * freqspace2[(w*y+x)][0]) / (ffx*fx + ffy*fy) / TWO_PI; } else { @@ -114,16 +119,21 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, fftw_execute(f12i1); - /* renormalize */ + /* renormalize, find min/max */ + vmin = vmax = 0; for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) { #ifdef C99 - imgspace1[(w*y+x)] /= (w*h); + v = creal(imgspace1[(w*y+x)] /= (w*h)); #else - imgspace1[(w*y+x)][0] /= (w*h); + v = (imgspace1[(w*y+x)][0] /= (w*h)); imgspace1[(w*y+x)][1] /= (w*h); #endif + if(v < vmin || (x == 0 && y == 0)) + vmin = v; + if(v > vmax || (x == 0 && y == 0)) + vmax = v; } if(refmap) @@ -170,8 +180,6 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, s = 1; } - printf("Ref-computed scale: %f\nRef-computed offset: %f\n", s, o); - /* * now apply user-given offset and scale to these values * (x * s + o) * scale + offset @@ -182,35 +190,17 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, } else if(scale == 0) { -#ifdef C99 - vmin = vmax = creal(imgspace1[0]); -#else - vmin = vmax = imgspace1[0][0]; -#endif - for(y = 0; y < h; ++y) - for(x = 0; x < w; ++x) - { -#ifdef C99 - v = creal(imgspace1[(w*y+x)]); -#else - v = imgspace1[(w*y+x)][0]; -#endif - if(v < vmin) - vmin = v; - if(v > vmax) - vmax = v; - } - /* * map vmin to -1 * map vmax to +1 */ scale = 2 / (vmax - vmin); offset = -(vmax + vmin) / (vmax - vmin); - - printf("Autocomputed scale: %f\nAutocomputed offset: %f\n", scale, offset); } + printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n", + vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset); + for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) { @@ -250,9 +240,9 @@ void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale) fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex)); fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex)); fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex)); - fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE); - fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE); - fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE); + fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE); + fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE); + fftw_plan f22i2 = fftw_plan_dft_2d(h, w, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE); for(y = 0; y < h; ++y) for(x = 0; x < w; ++x)