2 * FFT based normalmap to heightmap converter
3 * Copyright (C) 2010 Rudolf Polzer
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #if __STDC_VERSION__ >= 199901L || __cplusplus__
36 #define TWO_PI (4*atan2(1,1) * 2)
38 int floatcmp(const void *a_, const void *b_)
40 float a = *(float *)a_;
41 float b = *(float *)b_;
49 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 renormalize, double highpass, int use_median)
56 double v, vmin, vmed, vmax;
60 float *medianbuf = (float *) malloc(w*h * sizeof(*medianbuf));
61 fftw_complex *imgspace1 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
62 fftw_complex *imgspace2 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
63 fftw_complex *freqspace1 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
64 fftw_complex *freqspace2 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
65 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
66 fftw_plan i22f2 = fftw_plan_dft_2d(h, w, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE);
67 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
69 for(y = 0; y < h; ++y)
70 for(x = 0; x < w; ++x)
73 * unnormalized normals:
77 * BUT: darkplaces uses inverted normals, n_y actually is dh/dy by image pixel coordinates
79 nx = ((int)map[(w*y+x)*4+2] - 127.5) / 128;
80 ny = ((int)map[(w*y+x)*4+1] - 127.5) / 128;
81 nz = ((int)map[(w*y+x)*4+0] - 127.5) / 128;
83 /* reconstruct the derivatives from here */
85 imgspace1[(w*y+x)] = nx / nz * w; /* = dz/dx */
86 imgspace2[(w*y+x)] = -ny / nz * h; /* = dz/dy */
88 imgspace1[(w*y+x)][0] = nx / nz * w; /* = dz/dx */
89 imgspace1[(w*y+x)][1] = 0;
90 imgspace2[(w*y+x)][0] = -ny / nz * h; /* = dz/dy */
91 imgspace2[(w*y+x)][1] = 0;
96 double v = nx * nx + ny * ny + nz * nz;
103 map[(w*y+x)*4+2] = floor(nx * 127.5 + 128);
104 map[(w*y+x)*4+1] = floor(ny * 127.5 + 128);
105 map[(w*y+x)*4+0] = floor(nz * 127.5 + 128);
110 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
115 for(y = 0; y < h; ++y)
116 for(x = 0; x < w; ++x)
126 /* discontinous case; we must invert whatever "filter" would do on (x, y)! */
128 fftw_complex response_x = 0;
129 fftw_complex response_y = 0;
131 for(i = -filterh / 2; i <= filterh / 2; ++i)
132 for(j = -filterw / 2; j <= filterw / 2; ++j)
134 response_x += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (j * fx + i * fy));
135 response_y += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (i * fx + j * fy));
140 * fourier(df/dx)_xy = fourier(f)_xy * response_x
141 * fourier(df/dy)_xy = fourier(f)_xy * response_y
142 * mult by conjugate of response_x, response_y:
143 * conj(response_x) * fourier(df/dx)_xy = fourier(f)_xy * |response_x^2|
144 * conj(response_y) * fourier(df/dy)_xy = fourier(f)_xy * |response_y^2|
146 * fourier(f)_xy = (conj(response_x) * fourier(df/dx)_xy + conj(response_y) * fourier(df/dy)_xy) / (|response_x|^2 + |response_y|^2)
149 sum = cabs(response_x) * cabs(response_x) + cabs(response_y) * cabs(response_y);
152 freqspace1[(w*y+x)] = (conj(response_x) * freqspace1[(w*y+x)] + conj(response_y) * freqspace2[(w*y+x)]) / sum;
154 freqspace1[(w*y+x)] = 0;
156 fftw_complex response_x = {0, 0};
157 fftw_complex response_y = {0, 0};
159 for(i = -filterh / 2; i <= filterh / 2; ++i)
160 for(j = -filterw / 2; j <= filterw / 2; ++j)
162 response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy));
163 response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy));
164 response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy));
165 response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy));
168 sum = response_x[0] * response_x[0] + response_x[1] * response_x[1]
169 + response_y[0] * response_y[0] + response_y[1] * response_y[1];
173 double s = freqspace1[(w*y+x)][0];
174 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;
175 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;
179 freqspace1[(w*y+x)][0] = 0;
180 freqspace1[(w*y+x)][1] = 0;
186 /* continuous integration case */
187 /* these must have the same sign as fx and fy (so ffx*fx + ffy*fy is nonzero), otherwise do not matter */
188 /* it basically decides how artifacts are distributed */
193 freqspace1[(w*y+x)] = _Complex_I * (ffx * freqspace1[(w*y+x)] + ffy * freqspace2[(w*y+x)]) / (ffx*fx + ffy*fy) / TWO_PI;
195 freqspace1[(w*y+x)] = 0;
199 save = freqspace1[(w*y+x)][0];
200 freqspace1[(w*y+x)][0] = -(ffx * freqspace1[(w*y+x)][1] + ffy * freqspace2[(w*y+x)][1]) / (ffx*fx + ffy*fy) / TWO_PI;
201 freqspace1[(w*y+x)][1] = (ffx * save + ffy * freqspace2[(w*y+x)][0]) / (ffx*fx + ffy*fy) / TWO_PI;
205 freqspace1[(w*y+x)][0] = 0;
206 freqspace1[(w*y+x)][1] = 0;
212 double f1 = (fabs(fx)*highpass);
213 double f2 = (fabs(fy)*highpass);
214 /* if either of them is < 1, phase out (min at 0.5) */
216 (f1 <= 0.5 ? 0 : (f1 >= 1 ? 1 : ((f1 - 0.5) * 2.0)))
218 (f2 <= 0.5 ? 0 : (f2 >= 1 ? 1 : ((f2 - 0.5) * 2.0)));
220 freqspace1[(w*y+x)] *= f;
222 freqspace1[(w*y+x)][0] *= f;
223 freqspace1[(w*y+x)][1] *= f;
230 /* renormalize, find min/max */
231 vmin = vmed = vmax = 0;
232 for(y = 0; y < h; ++y)
233 for(x = 0; x < w; ++x)
236 v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5));
238 v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5));
240 * imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
241 * this value is never used
244 if(v < vmin || (x == 0 && y == 0))
246 if(v > vmax || (x == 0 && y == 0))
248 medianbuf[w*y+x] = v;
250 qsort(medianbuf, w*h, sizeof(*medianbuf), floatcmp);
252 vmed = medianbuf[(w*h-1)/2];
254 vmed = (medianbuf[(w*h)/2] + medianbuf[(w*h-2)/2]) * 0.5;
260 double sa, sfa, sffa, sfva, sva;
262 sa = sfa = sffa = sfva = sva = 0;
265 for(y = 0; y < h; ++y)
266 for(x = 0; x < w; ++x)
268 a = (int)refmap[(w*y+x)*4+3];
269 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);
270 v = (v - 128.0) / 127.0;
272 f = creal(imgspace1[(w*y+x)]);
274 f = imgspace1[(w*y+x)][0];
290 /* linear regression ftw */
291 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
292 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
294 else /* all values of v are equal, so we cannot get scale; we can still get offset */
296 o = ((sva - sfa) / sa);
301 * now apply user-given offset and scale to these values
302 * (x * s + o) * scale + offset
303 * x * s * scale + o * scale + offset
314 scale = 2 / (vmax - vmin);
315 offset = -(vmax + vmin) / (vmax - vmin);
320 * negative scale = match median to offset
321 * we actually want (v - vmed) * scale + offset
323 offset -= vmed * scale;
326 printf("Min: %f\nAvg: %f\nMed: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Med: %f\nScaled-Max: %f\n",
327 vmin, 0.0, vmed, vmax, scale, offset, vmin * scale + offset, offset, vmed * scale + offset, vmax * scale + offset);
329 for(y = 0; y < h; ++y)
330 for(x = 0; x < w; ++x)
333 v = creal(imgspace1[(w*y+x)]);
335 v = imgspace1[(w*y+x)][0];
337 v = v * scale + offset;
342 map[(w*y+x)*4+3] = floor(128.5 + 127 * v); /* in heightmaps, we avoid pixel value 0 as many imaging apps cannot handle it */
345 fftw_destroy_plan(i12f1);
346 fftw_destroy_plan(i22f2);
347 fftw_destroy_plan(f12i1);
349 fftw_free(freqspace2);
350 fftw_free(freqspace1);
351 fftw_free(imgspace2);
352 fftw_free(imgspace1);
356 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
366 fftw_complex *imgspace1 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
367 fftw_complex *imgspace2 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
368 fftw_complex *freqspace1 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
369 fftw_complex *freqspace2 = (fftw_complex *) fftw_malloc(w*h * sizeof(fftw_complex));
370 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
371 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
372 fftw_plan f22i2 = fftw_plan_dft_2d(h, w, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
374 for(y = 0; y < h; ++y)
375 for(x = 0; x < w; ++x)
383 v = map[(w*y+x)*4+src_chan];
386 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
390 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);
394 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
396 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
397 imgspace1[(w*y+x)][1] = 0;
400 v = 1; /* do not write alpha zero */
401 map[(w*y+x)*4+3] = floor(v + 0.5);
404 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
408 for(y = 0; y < h; ++y)
409 for(x = 0; x < w; ++x)
418 fx = sin(fx * TWO_PI / w);
419 fy = sin(fy * TWO_PI / h);
422 /* a lowpass to prevent the worst */
423 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
424 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
426 /* a lowpass to prevent the worst */
427 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
428 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
429 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
430 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
434 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
435 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
437 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
438 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
439 save = freqspace1[(w*y+x)][0];
440 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
441 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
450 for(y = 0; y < h; ++y)
451 for(x = 0; x < w; ++x)
454 nx = creal(imgspace1[(w*y+x)]);
455 ny = creal(imgspace2[(w*y+x)]);
457 nx = imgspace1[(w*y+x)][0];
458 ny = imgspace2[(w*y+x)][0];
463 v = -sqrt(nx*nx + ny*ny + nz*nz);
467 ny = -ny; /* DP inverted normals */
468 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
469 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
470 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
473 fftw_destroy_plan(i12f1);
474 fftw_destroy_plan(f12i1);
475 fftw_destroy_plan(f22i2);
477 fftw_free(freqspace2);
478 fftw_free(freqspace1);
479 fftw_free(imgspace2);
480 fftw_free(imgspace1);
483 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale, const double *filter, int filterw, int filterh)
489 double *img_reduced = (double *) malloc(w*h * sizeof(double));
491 for(y = 0; y < h; ++y)
492 for(x = 0; x < w; ++x)
500 v = map[(w*y+x)*4+src_chan];
503 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
507 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);
510 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
512 v = 1; /* do not write alpha zero */
513 map[(w*y+x)*4+3] = floor(v + 0.5);
516 for(y = 0; y < h; ++y)
517 for(x = 0; x < w; ++x)
522 for(i = -filterh / 2; i <= filterh / 2; ++i)
523 for(j = -filterw / 2; j <= filterw / 2; ++j)
525 nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
526 ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
529 v = -sqrt(nx*nx + ny*ny + nz*nz);
533 ny = -ny; /* DP inverted normals */
534 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
535 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
536 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
542 unsigned char *FS_LoadFile(const char *fn, int *len)
544 unsigned char *buf = NULL;
546 FILE *f = fopen(fn, "rb");
552 buf = (unsigned char *) realloc(buf, *len + 65536);
560 n = fread(buf + *len, 1, 65536, f);
575 int FS_WriteFile(const char *fn, unsigned char *data, int len)
577 FILE *f = fopen(fn, "wb");
580 if(fwrite(data, len, 1, f) != 1)
590 /* START stuff that originates from image.c in DarkPlaces */
591 int image_width, image_height;
593 typedef struct _TargaHeader
595 unsigned char id_length, colormap_type, image_type;
596 unsigned short colormap_index, colormap_length;
597 unsigned char colormap_size;
598 unsigned short x_origin, y_origin, width, height;
599 unsigned char pixel_size, attributes;
603 void PrintTargaHeader(TargaHeader *t)
605 printf("TargaHeader:\nuint8 id_length = %i;\nuint8 colormap_type = %i;\nuint8 image_type = %i;\nuint16 colormap_index = %i;\nuint16 colormap_length = %i;\nuint8 colormap_size = %i;\nuint16 x_origin = %i;\nuint16 y_origin = %i;\nuint16 width = %i;\nuint16 height = %i;\nuint8 pixel_size = %i;\nuint8 attributes = %i;\n", t->id_length, t->colormap_type, t->image_type, t->colormap_index, t->colormap_length, t->colormap_size, t->x_origin, t->y_origin, t->width, t->height, t->pixel_size, t->attributes);
608 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
610 int x, y, pix_inc, row_inci, runlen, alphabits;
611 unsigned char *image_buffer;
612 unsigned int *pixbufi;
613 const unsigned char *fin, *enddata;
614 TargaHeader targa_header;
615 unsigned int palettei[256];
626 enddata = f + filesize;
628 targa_header.id_length = f[0];
629 targa_header.colormap_type = f[1];
630 targa_header.image_type = f[2];
632 targa_header.colormap_index = f[3] + f[4] * 256;
633 targa_header.colormap_length = f[5] + f[6] * 256;
634 targa_header.colormap_size = f[7];
635 targa_header.x_origin = f[8] + f[9] * 256;
636 targa_header.y_origin = f[10] + f[11] * 256;
637 targa_header.width = image_width = f[12] + f[13] * 256;
638 targa_header.height = image_height = f[14] + f[15] * 256;
639 targa_header.pixel_size = f[16];
640 targa_header.attributes = f[17];
642 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
644 printf("LoadTGA: invalid size\n");
645 PrintTargaHeader(&targa_header);
649 /* advance to end of header */
652 /* skip TARGA image comment (usually 0 bytes) */
653 fin += targa_header.id_length;
655 /* read/skip the colormap if present (note: according to the TARGA spec it */
656 /* can be present even on 1color or greyscale images, just not used by */
657 /* the image data) */
658 if (targa_header.colormap_type)
660 if (targa_header.colormap_length > 256)
662 printf("LoadTGA: only up to 256 colormap_length supported\n");
663 PrintTargaHeader(&targa_header);
666 if (targa_header.colormap_index)
668 printf("LoadTGA: colormap_index not supported\n");
669 PrintTargaHeader(&targa_header);
672 if (targa_header.colormap_size == 24)
674 for (x = 0;x < targa_header.colormap_length;x++)
680 palettei[x] = bgra.i;
683 else if (targa_header.colormap_size == 32)
685 memcpy(palettei, fin, targa_header.colormap_length*4);
686 fin += targa_header.colormap_length * 4;
690 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
691 PrintTargaHeader(&targa_header);
696 /* check our pixel_size restrictions according to image_type */
697 switch (targa_header.image_type & ~8)
700 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
702 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
703 PrintTargaHeader(&targa_header);
708 /* set up a palette to make the loader easier */
709 for (x = 0;x < 256;x++)
711 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
713 palettei[x] = bgra.i;
715 /* fall through to colormap case */
717 if (targa_header.pixel_size != 8)
719 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
720 PrintTargaHeader(&targa_header);
725 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
726 PrintTargaHeader(&targa_header);
730 if (targa_header.attributes & 0x10)
732 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
736 /* number of attribute bits per pixel, we only support 0 or 8 */
737 alphabits = targa_header.attributes & 0x0F;
738 if (alphabits != 8 && alphabits != 0)
740 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
744 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
747 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
751 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
752 if ((targa_header.attributes & 0x20) == 0)
754 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
755 row_inci = -image_width*2;
759 pixbufi = (unsigned int*)image_buffer;
766 if ((targa_header.image_type & ~8) == 2)
767 pix_inc = (targa_header.pixel_size + 7) / 8;
768 switch (targa_header.image_type)
770 case 1: /* colormapped, uncompressed */
771 case 3: /* greyscale, uncompressed */
772 if (fin + image_width * image_height * pix_inc > enddata)
774 for (y = 0;y < image_height;y++, pixbufi += row_inci)
775 for (x = 0;x < image_width;x++)
776 *pixbufi++ = palettei[*fin++];
779 /* BGR or BGRA, uncompressed */
780 if (fin + image_width * image_height * pix_inc > enddata)
782 if (targa_header.pixel_size == 32 && alphabits)
784 for (y = 0;y < image_height;y++)
785 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
789 for (y = 0;y < image_height;y++, pixbufi += row_inci)
791 for (x = 0;x < image_width;x++, fin += pix_inc)
802 case 9: /* colormapped, RLE */
803 case 11: /* greyscale, RLE */
804 for (y = 0;y < image_height;y++, pixbufi += row_inci)
806 for (x = 0;x < image_width;)
809 break; /* error - truncated file */
813 /* RLE - all pixels the same color */
815 if (fin + pix_inc > enddata)
816 break; /* error - truncated file */
817 if (x + runlen > image_width)
818 break; /* error - line exceeds width */
819 bgra.i = palettei[*fin++];
825 /* uncompressed - all pixels different color */
827 if (fin + pix_inc * runlen > enddata)
828 break; /* error - truncated file */
829 if (x + runlen > image_width)
830 break; /* error - line exceeds width */
832 *pixbufi++ = palettei[*fin++];
836 if (x != image_width)
838 /* pixbufi is useless now */
839 printf("LoadTGA: corrupt file\n");
845 /* BGR or BGRA, RLE */
846 if (targa_header.pixel_size == 32 && alphabits)
848 for (y = 0;y < image_height;y++, pixbufi += row_inci)
850 for (x = 0;x < image_width;)
853 break; /* error - truncated file */
857 /* RLE - all pixels the same color */
859 if (fin + pix_inc > enddata)
860 break; /* error - truncated file */
861 if (x + runlen > image_width)
862 break; /* error - line exceeds width */
873 /* uncompressed - all pixels different color */
875 if (fin + pix_inc * runlen > enddata)
876 break; /* error - truncated file */
877 if (x + runlen > image_width)
878 break; /* error - line exceeds width */
891 if (x != image_width)
893 /* pixbufi is useless now */
894 printf("LoadTGA: corrupt file\n");
901 for (y = 0;y < image_height;y++, pixbufi += row_inci)
903 for (x = 0;x < image_width;)
906 break; /* error - truncated file */
910 /* RLE - all pixels the same color */
912 if (fin + pix_inc > enddata)
913 break; /* error - truncated file */
914 if (x + runlen > image_width)
915 break; /* error - line exceeds width */
926 /* uncompressed - all pixels different color */
928 if (fin + pix_inc * runlen > enddata)
929 break; /* error - truncated file */
930 if (x + runlen > image_width)
931 break; /* error - line exceeds width */
944 if (x != image_width)
946 /* pixbufi is useless now */
947 printf("LoadTGA: corrupt file\n");
954 /* unknown image_type */
961 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
964 unsigned char *buffer, *out;
965 const unsigned char *in, *end;
968 buffer = (unsigned char *)malloc(width*height*4 + 18);
970 memset (buffer, 0, 18);
971 buffer[2] = 2; /* uncompressed type */
972 buffer[12] = (width >> 0) & 0xFF;
973 buffer[13] = (width >> 8) & 0xFF;
974 buffer[14] = (height >> 0) & 0xFF;
975 buffer[15] = (height >> 8) & 0xFF;
977 for (y = 3;y < width*height*4;y += 4)
981 if (y < width*height*4)
983 /* save the alpha channel */
984 buffer[16] = 32; /* pixel size */
985 buffer[17] = 8; /* 8 bits of alpha */
987 /* flip upside down */
989 for (y = height - 1;y >= 0;y--)
991 memcpy(out, data + y * width * 4, width * 4);
997 /* save only the color channels */
998 buffer[16] = 24; /* pixel size */
999 buffer[17] = 0; /* 8 bits of alpha */
1001 /* truncate bgra to bgr and flip upside down */
1003 for (y = height - 1;y >= 0;y--)
1005 in = data + y * width * 4;
1006 end = in + width * 4;
1007 for (;in < end;in += 4)
1015 ret = FS_WriteFile (filename, buffer, out - buffer);
1021 /* START stuff that originates from image.c in DarkPlaces */
1023 int usage(const char *me)
1025 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> filtertype [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
1026 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -1 [<scale>] (read from B)\n", me);
1027 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -2 [<scale>] (read from G)\n", me);
1028 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -3 [<scale>] (read from R)\n", me);
1029 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -4 [<scale>] (read from A)\n", me);
1030 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -5 [<scale>] (read from (R+G+B)/3)\n", me);
1031 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -6 [<scale>] (read from Y)\n", me);
1035 static const double filter_scharr3[3][3] = {
1036 { -3/32.0, 0, 3/32.0 },
1037 { -10/32.0, 0, 10/32.0 },
1038 { -3/32.0, 0, 3/32.0 }
1041 static const double filter_prewitt3[3][3] = {
1042 { -1/6.0, 0, 1/6.0 },
1043 { -1/6.0, 0, 1/6.0 },
1044 { -1/6.0, 0, 1/6.0 }
1047 /* pathologic for inverting */
1048 static const double filter_sobel3[3][3] = {
1049 { -1/8.0, 0, 1/8.0 },
1050 { -2/8.0, 0, 2/8.0 },
1051 { -1/8.0, 0, 1/8.0 }
1054 /* pathologic for inverting */
1055 static const double filter_sobel5[5][5] = {
1056 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 },
1057 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1058 { -6/128.0, -12/128.0, 0, 12/128.0, 6/128.0 },
1059 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1060 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 }
1063 /* pathologic for inverting */
1064 static const double filter_prewitt5[5][5] = {
1065 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1066 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1067 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1068 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1069 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }
1072 static const double filter_trivial[1][3] = {
1076 int main(int argc, char **argv)
1078 const char *infile, *outfile, *reffile;
1079 double scale, offset;
1082 int renormalize = 0;
1083 double highpass = 0;
1084 unsigned char *nmapdata, *nmap, *refmap;
1085 const char *filtertype;
1086 const double *filter = NULL;
1087 int filterw = 0, filterh = 0;
1088 #define USE_FILTER(f) \
1091 filterw = sizeof(*(f)) / sizeof(**(f)); \
1092 filterh = sizeof((f)) / sizeof(*(f)); \
1093 filter = &(f)[0][0]; \
1100 return usage(*argv);
1105 return usage(*argv);
1108 filtertype = argv[3];
1110 return usage(*argv);
1113 scale = atof(argv[4]);
1118 offset = atof(argv[5]);
1120 offset = (scale<0) ? 1 : 0;
1127 /* experimental features */
1128 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"))
1129 renormalize = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"));
1130 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"))
1131 highpass = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"));
1132 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"))
1133 use_median = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"));
1135 nmapdata = FS_LoadFile(infile, &nmaplen);
1138 printf("FS_LoadFile failed\n");
1141 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
1145 printf("LoadTGA_BGRA failed\n");
1153 nmapdata = FS_LoadFile(reffile, &nmaplen);
1156 printf("FS_LoadFile failed\n");
1159 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
1163 printf("LoadTGA_BGRA failed\n");
1166 if(image_width != w || image_height != h)
1168 printf("reference map must have same size as input normalmap\n");
1175 if(!strcmp(filtertype, "trivial"))
1176 USE_FILTER(filter_trivial);
1177 if(!strcmp(filtertype, "prewitt3"))
1178 USE_FILTER(filter_prewitt3);
1179 if(!strcmp(filtertype, "scharr3"))
1180 USE_FILTER(filter_scharr3);
1181 if(!strcmp(filtertype, "sobel3"))
1182 USE_FILTER(filter_sobel3);
1183 if(!strcmp(filtertype, "prewitt5"))
1184 USE_FILTER(filter_prewitt5);
1185 if(!strcmp(filtertype, "sobel5"))
1186 USE_FILTER(filter_sobel5);
1191 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset, filter, filterw, filterh);
1193 hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
1196 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh, renormalize, highpass, use_median);
1198 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1200 printf("Image_WriteTGABGRA failed\n");