]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/fft-normalmap-to-heightmap.c
Merge branch 'master' into divVerent/crypto2
[xonotic/xonotic.git] / misc / tools / fft-normalmap-to-heightmap.c
index dc48967f22f9cb8635f894428765658bf30ec624..aa4ac7fffe78855f555424ba2cfff178581f75db 100644 (file)
 
 #define TWO_PI (4*atan2(1,1) * 2)
 
-void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
+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;
+       double ffx, ffy;
        double nx, ny, nz;
        double v, vmin, vmax;
 #ifndef C99
@@ -48,9 +50,9 @@ void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
        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,27 @@ void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
        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 */
+               /* it basically decides how artifacts are distributed */
+               ffx = fx;
+               ffy = fy;
 #ifdef C99
                if(fx||fy)
-                       freqspace1[(w*y+x)] = 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,41 +120,87 @@ void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
 
        fftw_execute(f12i1);
 
-       if(scale == 0)
+       /* renormalize, find min/max */
+       vmin = vmax = 0;
+       for(y = 0; y < h; ++y)
+       for(x = 0; x < w; ++x)
        {
 #ifdef C99
-               vmin = vmax = creal(imgspace1[0]);
+               v = creal(imgspace1[(w*y+x)] /= (w*h));
 #else
-               vmin = vmax = imgspace1[0][0];
+               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)
+       {
+               double f, a;
+               double o, s;
+               double sa, sfa, sffa, sfva, sva;
+               double mi, ma;
+               sa = sfa = sffa = sfva = sva = 0;
+               mi = 1;
+               ma = -1;
                for(y = 0; y < h; ++y)
                for(x = 0; x < w; ++x)
                {
+                       a = (int)refmap[(w*y+x)*4+3];
+                       v = (refmap[(w*y+x)*4+0]*0.114 + refmap[(w*y+x)*4+1]*0.587 + refmap[(w*y+x)*4+2]*0.299);
+                       v = (v - 128.0) / 127.0;
 #ifdef C99
-                       v = creal(imgspace1[(w*y+x)]);
+                       f = creal(imgspace1[(w*y+x)]);
 #else
-                       v = imgspace1[(w*y+x)][0];
+                       f = imgspace1[(w*y+x)][0];
 #endif
-                       if(v < vmin)
-                               vmin = v;
-                       if(v > vmax)
-                               vmax = v;
+                       if(a <= 0)
+                               continue;
+                       if(v < mi)
+                               mi = v;
+                       if(v > ma)
+                               ma = v;
+                       sa += a;
+                       sfa += f*a;
+                       sffa += f*f*a;
+                       sfva += f*v*a;
+                       sva += v*a;
+               }
+               if(mi < ma)
+               {
+                       /* linear regression ftw */
+                       o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
+                       s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
+               }
+               else /* all values of v are equal, so we cannot get scale; we can still get offset */
+               {
+                       o = ((sva - sfa) / sa);
+                       s = 1;
                }
 
-               vmin /= (w*h);
-               vmax /= (w*h);
-
+               /*
+                * now apply user-given offset and scale to these values
+                * (x * s + o) * scale + offset
+                * x * s * scale + o * scale + offset
+                */
+               offset += o * scale;
+               scale *= s;
+       }
+       else if(scale == 0)
+       {
                /*
                 * 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);
        }
 
-       scale /= (w*h);
+       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)
@@ -189,9 +241,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)
@@ -239,8 +291,8 @@ void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
                freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
                freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
 
-               freqspace2[(w*y+x)] = TWO_PI*I * fy * freqspace1[(w*y+x)]; /* y derivative */
-               freqspace1[(w*y+x)] = TWO_PI*I * fx * freqspace1[(w*y+x)]; /* x derivative */
+               freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
+               freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
 #else
                /* a lowpass to prevent the worst */
                freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
@@ -840,7 +892,7 @@ int Image_WriteTGABGRA (const char *filename, int width, int height, const unsig
 
 int usage(const char *me)
 {
-       printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset>]] (get heightmap from normalmap)\n", me);
+       printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
        printf("or:    %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from B, Diff)\n", me);
        printf("or:    %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G, Diff)\n", me);
        printf("or:    %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R, Diff)\n", me);
@@ -858,10 +910,10 @@ int usage(const char *me)
 
 int main(int argc, char **argv)
 {
-       const char *infile, *outfile;
+       const char *infile, *outfile, *reffile;
        double scale, offset;
-       int nmaplen;
-       unsigned char *nmapdata, *nmap;
+       int nmaplen, w, h;
+       unsigned char *nmapdata, *nmap, *refmap;
 
        if(argc > 1)
                infile = argv[1];
@@ -881,7 +933,12 @@ int main(int argc, char **argv)
        if(argc > 4)
                offset = atof(argv[4]);
        else
-               offset = 0;
+               offset = (scale<0) ? 1 : 0;
+
+       if(argc > 5)
+               reffile = argv[5];
+       else
+               reffile = NULL;
 
        nmapdata = FS_LoadFile(infile, &nmaplen);
        if(!nmapdata)
@@ -896,12 +953,39 @@ int main(int argc, char **argv)
                printf("LoadTGA_BGRA failed\n");
                return 2;
        }
+       w = image_width;
+       h = image_height;
+
+       if(reffile)
+       {
+               nmapdata = FS_LoadFile(reffile, &nmaplen);
+               if(!nmapdata)
+               {
+                       printf("FS_LoadFile failed\n");
+                       return 2;
+               }
+               refmap = LoadTGA_BGRA(nmapdata, nmaplen);
+               free(nmapdata);
+               if(!refmap)
+               {
+                       printf("LoadTGA_BGRA failed\n");
+                       return 2;
+               }
+               if(image_width != w || image_height != h)
+               {
+                       printf("reference map must have same size as input normalmap\n");
+                       return 2;
+               }
+       }
+       else
+               refmap = NULL;
+
        if(scale < -6)
                hmap_to_nmap(nmap, image_width, image_height, -scale-7, offset);
        else if(scale < 0)
                hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset);
        else
-               nmap_to_hmap(nmap, image_width, image_height, scale, offset);
+               nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset);
        if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
        {
                printf("Image_WriteTGABGRA failed\n");