X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fxonotic.git;a=blobdiff_plain;f=misc%2Ftools%2Ffft-normalmap-to-heightmap.c;h=88ac27c9fde1e4288d3907f198669c0b06a6491a;hp=95782ec83cf1ea8d6175d47496800eb1e7fcafb3;hb=395a8b52d5e0b3b6a102cb7e2383902a8c3f0ae2;hpb=ffc605ed11c3269d728bfa433c6640248f2cb58d diff --git a/misc/tools/fft-normalmap-to-heightmap.c b/misc/tools/fft-normalmap-to-heightmap.c index 95782ec8..88ac27c9 100644 --- a/misc/tools/fft-normalmap-to-heightmap.c +++ b/misc/tools/fft-normalmap-to-heightmap.c @@ -35,9 +35,12 @@ #define TWO_PI (4*atan2(1,1) * 2) -void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, 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, const double *filter, int filterw, int filterh) { int x, y; + int i, j; + double fx, fy; + double ffx, ffy; double nx, ny, nz; double v, vmin, vmax; #ifndef C99 @@ -48,9 +51,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) @@ -71,9 +74,9 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, imgspace1[(w*y+x)] = nx / nz * w; /* = dz/dx */ imgspace2[(w*y+x)] = -ny / nz * h; /* = dz/dy */ #else - imgspace1[(w*y+x)][0] = nx / nz; /* = dz/dx */ + imgspace1[(w*y+x)][0] = nx / nz * w; /* = dz/dx */ imgspace1[(w*y+x)][1] = 0; - imgspace2[(w*y+x)][0] = -ny / nz; /* = dz/dy */ + imgspace2[(w*y+x)][0] = -ny / nz * h; /* = dz/dy */ imgspace2[(w*y+x)][1] = 0; #endif } @@ -86,52 +89,121 @@ 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; - if(fx > w/2) - fx -= w; - if(fy > h/2) - fy -= h; + fx = x * 1.0 / w; + fy = y * 1.0 / h; + if(filter) + { + // discontinous case + // we must invert whatever "filter" would do on (x, y)! #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; - else - freqspace1[(w*y+x)] = 0; + fftw_complex response_x = 0; + fftw_complex response_y = 0; + double sum; + for(i = -filterh / 2; i <= filterh / 2; ++i) + for(j = -filterw / 2; j <= filterw / 2; ++j) + { + response_x += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (j * fx + i * fy)); + response_y += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (i * fx + j * fy)); + } + + // we know: + // fourier(df/dx)_xy = fourier(f)_xy * response_x + // fourier(df/dy)_xy = fourier(f)_xy * response_y + // mult by conjugate of response_x, response_y: + // conj(response_x) * fourier(df/dx)_xy = fourier(f)_xy * |response_x^2| + // conj(response_y) * fourier(df/dy)_xy = fourier(f)_xy * |response_y^2| + // and + // fourier(f)_xy = (conj(response_x) * fourier(df/dx)_xy + conj(response_y) * fourier(df/dy)_xy) / (|response_x|^2 + |response_y|^2) + + sum = cabs(response_x) * cabs(response_x) + cabs(response_y) * cabs(response_y); + + if(sum > 0) + freqspace1[(w*y+x)] = (conj(response_x) * freqspace1[(w*y+x)] + conj(response_y) * freqspace2[(w*y+x)]) / sum; + 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; + fftw_complex response_x = {0, 0}; + fftw_complex response_y = {0, 0}; + double sum; + for(i = -filterh / 2; i <= filterh / 2; ++i) + for(j = -filterw / 2; j <= filterw / 2; ++j) + { + response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy)); + response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy)); + response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy)); + response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy)); + } + + sum = response_x[0] * response_x[0] + response_x[1] * response_x[1] + + response_y[0] * response_y[0] + response_y[1] * response_y[1]; + + if(sum > 0) + { + double s = freqspace1[(w*y+x)][0]; + freqspace1[(w*y+x)][0] = (response_x[0] * s + response_x[1] * freqspace1[(w*y+x)][1] + response_y[0] * freqspace2[(w*y+x)][0] + response_y[1] * freqspace2[(w*y+x)][1]) / sum; + freqspace1[(w*y+x)][1] = (response_x[0] * freqspace1[(w*y+x)][1] - response_x[1] * s + response_y[0] * freqspace2[(w*y+x)][1] - response_y[1] * freqspace2[(w*y+x)][0]) / sum; + } + else + { + freqspace1[(w*y+x)][0] = 0; + freqspace1[(w*y+x)][1] = 0; + } +#endif } else { - freqspace1[(w*y+x)][0] = 0; - freqspace1[(w*y+x)][1] = 0; - } + // continuous integration case + if(fx > 0.5) + fx -= 1; + if(fy > 0.5) + fy -= 1; + /* 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)] = _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] = -(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 + { + freqspace1[(w*y+x)][0] = 0; + freqspace1[(w*y+x)][1] = 0; + } #endif + } } fftw_execute(f12i1); - if(refmap) + /* renormalize, find min/max */ + vmin = vmax = 0; + for(y = 0; y < h; ++y) + for(x = 0; x < w; ++x) { - // refmap: a reference map to define the heights - // alpha = weight, color = value - // if more than one color value is used, colors are also matched - - // we do linear regression, basically - // f'(x, y) = f(x, y) * scale + offset - // sum((f(x, y) * scale + offset - ref_y(x, y))^2 * ref_a(x, y)) minimize - - // diff by offset: - // sum(-2*ref_y(x,y)*ref_a(x,y) + 2*scale*f(x,y)*ref_a(x,y) + 2*offset*ref_a(x,y)) = 0 - // diff by scale: - // sum(-2*f(x,y)*ref_a(x,y) + 2*scale*f(x,y)^2*ref_a(x,y) + 2*offset*f(x,y)*ref_a(x,y)) = 0 - // -> - // offset = (sfa*sfya - sffa*sya) / (sfa*sfa-sa*sffa) - // scale = (sfa*sya - sa*sfya) / (sfa*sfa-sa*sffa) +#ifdef C99 + v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5)); +#else + v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5)); + // imgspace1[(w*y+x)][1] /= pow(w*h, 1.5); + // this value is never used +#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; @@ -142,9 +214,9 @@ 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) { - a = (int)refmap[(w*y+x)*4+0]; - v = (map[(w*y+x)*4+0]*0.114 + map[(w*y+x)*4+1]*0.587 + map[(w*y+x)*4+2]*0.299); - v = (v - 128.0) / 127.0; // value 0 is forbidden, 1 -> -1, 255 -> 1 + 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 f = creal(imgspace1[(w*y+x)]); #else @@ -152,71 +224,48 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, #endif if(a <= 0) continue; - if(y < mi) - mi = y; - if(y > ma) - ma = y; + 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; } - sfa /= (w*h); - sffa /= (w*h); - sffa /= (w*h); - sfva /= (w*h); 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 + else /* all values of v are equal, so we cannot get scale; we can still get offset */ { o = ((sva - sfa) / sa); s = 1; } - // now apply user-given offset and scale to these values - // (x * s + o) * scale + offset - // x * s * scale + o * scale + offset + + /* + * 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) { -#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; - } - - vmin /= (w*h); - vmax /= (w*h); - /* * 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) @@ -247,6 +296,7 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale) { int x, y; + double fx, fy; double nx, ny, nz; double v; #ifndef C99 @@ -257,9 +307,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) @@ -286,6 +336,8 @@ void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale) imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0; imgspace1[(w*y+x)][1] = 0; #endif + if(v < 1) + v = 1; /* do not write alpha zero */ map[(w*y+x)*4+3] = floor(v + 0.5); } @@ -296,26 +348,32 @@ void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale) 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; +#ifdef DISCONTINUOUS + fx = sin(fx * TWO_PI / w); + fy = sin(fy * TWO_PI / h); +#else #ifdef C99 /* a lowpass to prevent the worst */ 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 */ #else /* a lowpass to prevent the worst */ freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1); freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1); freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1); freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1); - +#endif +#endif +#ifdef C99 + 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 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */ freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0]; save = freqspace1[(w*y+x)][0]; @@ -362,19 +420,13 @@ void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale) fftw_free(imgspace1); } -void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale) +void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale, const double *filter, int filterw, int filterh) { int x, y; double nx, ny, nz; double v; int i, j; double *img_reduced = malloc(w*h * sizeof(double)); - static const double filter[3][3] = { /* filter to derive one component */ - { -1, 0, 1 }, - { -2, 0, 2 }, - { -1, 0, 1 } - }; - static const double filter_mult = 0.125; for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) @@ -396,20 +448,22 @@ void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double s break; } img_reduced[(w*y+x)] = (v - 128.0) / 127.0; + if(v < 1) + v = 1; /* do not write alpha zero */ map[(w*y+x)*4+3] = floor(v + 0.5); } for(y = 0; y < h; ++y) for(x = 0; x < w; ++x) { - nz = -1 / (scale * filter_mult); + nz = -1 / scale; nx = ny = 0; - for(i = -(int)(sizeof(filter) / sizeof(*filter)) / 2; i <= (int)(sizeof(filter) / sizeof(*filter)) / 2; ++i) - for(j = -(int)(sizeof(*filter) / sizeof(**filter)) / 2; j <= (int)(sizeof(*filter) / sizeof(**filter)) / 2; ++j) + for(i = -filterh / 2; i <= filterh / 2; ++i) + for(j = -filterw / 2; j <= filterw / 2; ++j) { - nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[i+(sizeof(filter) / sizeof(*filter)) / 2][j+(sizeof(*filter) / sizeof(**filter)) / 2]; - ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[i+(sizeof(filter) / sizeof(*filter)) / 2][j+(sizeof(*filter) / sizeof(**filter)) / 2]; + nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2]; + ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2]; } v = -sqrt(nx*nx + ny*ny + nz*nz); @@ -908,28 +962,74 @@ int Image_WriteTGABGRA (const char *filename, int width, int height, const unsig int usage(const char *me) { - printf("Usage: %s [ [ []]] (get heightmap from normalmap)\n", me); - printf("or: %s -1 [] (read from B, Diff)\n", me); - printf("or: %s -2 [] (read from G, Diff)\n", me); - printf("or: %s -3 [] (read from R, Diff)\n", me); - printf("or: %s -4 [] (read from A, Diff)\n", me); - printf("or: %s -5 [] (read from (R+G+B)/3, Diff)\n", me); - printf("or: %s -6 [] (read from Y, Diff)\n", me); - printf("or: %s -7 [] (read from B, FFT)\n", me); - printf("or: %s -8 [] (read from G, FFT)\n", me); - printf("or: %s -9 [] (read from R, FFT)\n", me); - printf("or: %s -10 [] (read from A, FFT)\n", me); - printf("or: %s -11 [] (read from (R+G+B)/3, FFT)\n", me); - printf("or: %s -12 [] (read from Y, FFT)\n", me); + printf("Usage: %s filtertype [ [ []]] (get heightmap from normalmap)\n", me); + printf("or: %s filtertype -1 [] (read from B)\n", me); + printf("or: %s filtertype -2 [] (read from G)\n", me); + printf("or: %s filtertype -3 [] (read from R)\n", me); + printf("or: %s filtertype -4 [] (read from A)\n", me); + printf("or: %s filtertype -5 [] (read from (R+G+B)/3)\n", me); + printf("or: %s filtertype -6 [] (read from Y)\n", me); return 1; } +static const double filter_scharr3[3][3] = { + { -3/32.0, 0, 3/32.0 }, + { -10/32.0, 0, 10/32.0 }, + { -3/32.0, 0, 3/32.0 } +}; + +static const double filter_prewitt3[3][3] = { + { -1/6.0, 0, 1/6.0 }, + { -1/6.0, 0, 1/6.0 }, + { -1/6.0, 0, 1/6.0 } +}; + +// pathologic for inverting +static const double filter_sobel3[3][3] = { + { -1/8.0, 0, 1/8.0 }, + { -2/8.0, 0, 2/8.0 }, + { -1/8.0, 0, 1/8.0 } +}; + +// pathologic for inverting +static const double filter_sobel5[5][5] = { + { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 }, + { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 }, + { -6/128.0, -12/128.0, 0, 12/128.0, 6/128.0 }, + { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 }, + { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 } +}; + +// pathologic for inverting +static const double filter_prewitt5[5][5] = { + { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }, + { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }, + { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }, + { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }, + { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 } +}; + +static const double filter_trivial[1][3] = { + { -0.5, 0, 0.5 } +}; + int main(int argc, char **argv) { const char *infile, *outfile, *reffile; double scale, offset; int nmaplen, w, h; unsigned char *nmapdata, *nmap, *refmap; + const char *filtertype; + const double *filter = NULL; + int filterw = 0, filterh = 0; +#define USE_FILTER(f) \ + do \ + { \ + filterw = sizeof(*(f)) / sizeof(**(f)); \ + filterh = sizeof((f)) / sizeof(*(f)); \ + filter = &(f)[0][0]; \ + } \ + while(0) if(argc > 1) infile = argv[1]; @@ -942,17 +1042,22 @@ int main(int argc, char **argv) return usage(*argv); if(argc > 3) - scale = atof(argv[3]); + filtertype = argv[3]; + else + return usage(*argv); + + if(argc > 4) + scale = atof(argv[4]); else scale = 0; - if(argc > 4) - offset = atof(argv[4]); + if(argc > 5) + offset = atof(argv[5]); else offset = (scale<0) ? 1 : 0; - if(argc > 5) - reffile = argv[5]; + if(argc > 6) + reffile = argv[6]; else reffile = NULL; @@ -974,7 +1079,7 @@ int main(int argc, char **argv) if(reffile) { - nmapdata = FS_LoadFile(infile, &nmaplen); + nmapdata = FS_LoadFile(reffile, &nmaplen); if(!nmapdata) { printf("FS_LoadFile failed\n"); @@ -993,13 +1098,32 @@ int main(int argc, char **argv) return 2; } } - - 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, refmap, image_width, image_height, scale, offset); + refmap = NULL; + + if(!strcmp(filtertype, "trivial")) + USE_FILTER(filter_trivial); + if(!strcmp(filtertype, "prewitt3")) + USE_FILTER(filter_prewitt3); + if(!strcmp(filtertype, "scharr3")) + USE_FILTER(filter_scharr3); + if(!strcmp(filtertype, "sobel3")) + USE_FILTER(filter_sobel3); + if(!strcmp(filtertype, "prewitt5")) + USE_FILTER(filter_prewitt5); + if(!strcmp(filtertype, "sobel5")) + USE_FILTER(filter_sobel5); + + if(scale < 0) + { + if(filter) + hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset, filter, filterw, filterh); + else + hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset); + } + else + nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh); + if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap)) { printf("Image_WriteTGABGRA failed\n");