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
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)
61 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
62 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
63 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
64 fftw_complex *freqspace2 = 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)
127 // we must invert whatever "filter" would do on (x, y)!
129 fftw_complex response_x = 0;
130 fftw_complex response_y = 0;
132 for(i = -filterh / 2; i <= filterh / 2; ++i)
133 for(j = -filterw / 2; j <= filterw / 2; ++j)
135 response_x += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (j * fx + i * fy));
136 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)
148 sum = cabs(response_x) * cabs(response_x) + cabs(response_y) * cabs(response_y);
151 freqspace1[(w*y+x)] = (conj(response_x) * freqspace1[(w*y+x)] + conj(response_y) * freqspace2[(w*y+x)]) / sum;
153 freqspace1[(w*y+x)] = 0;
155 fftw_complex response_x = {0, 0};
156 fftw_complex response_y = {0, 0};
158 for(i = -filterh / 2; i <= filterh / 2; ++i)
159 for(j = -filterw / 2; j <= filterw / 2; ++j)
161 response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy));
162 response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy));
163 response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy));
164 response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy));
167 sum = response_x[0] * response_x[0] + response_x[1] * response_x[1]
168 + response_y[0] * response_y[0] + response_y[1] * response_y[1];
172 double s = freqspace1[(w*y+x)][0];
173 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;
174 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;
178 freqspace1[(w*y+x)][0] = 0;
179 freqspace1[(w*y+x)][1] = 0;
185 // continuous integration case
186 /* these must have the same sign as fx and fy (so ffx*fx + ffy*fy is nonzero), otherwise do not matter */
187 /* it basically decides how artifacts are distributed */
192 freqspace1[(w*y+x)] = _Complex_I * (ffx * freqspace1[(w*y+x)] + ffy * freqspace2[(w*y+x)]) / (ffx*fx + ffy*fy) / TWO_PI;
194 freqspace1[(w*y+x)] = 0;
198 save = freqspace1[(w*y+x)][0];
199 freqspace1[(w*y+x)][0] = -(ffx * freqspace1[(w*y+x)][1] + ffy * freqspace2[(w*y+x)][1]) / (ffx*fx + ffy*fy) / TWO_PI;
200 freqspace1[(w*y+x)][1] = (ffx * save + ffy * freqspace2[(w*y+x)][0]) / (ffx*fx + ffy*fy) / TWO_PI;
204 freqspace1[(w*y+x)][0] = 0;
205 freqspace1[(w*y+x)][1] = 0;
211 double f1 = (fabs(fx)*highpass);
212 double f2 = (fabs(fy)*highpass);
213 // if either of them is < 1, phase out (min at 0.5)
215 (f1 <= 0.5 ? 0 : (f1 >= 1 ? 1 : ((f1 - 0.5) * 2.0)))
217 (f2 <= 0.5 ? 0 : (f2 >= 1 ? 1 : ((f2 - 0.5) * 2.0)));
219 freqspace1[(w*y+x)] *= f;
221 freqspace1[(w*y+x)][0] *= f;
222 freqspace1[(w*y+x)][1] *= f;
229 /* renormalize, find min/max */
231 for(y = 0; y < h; ++y)
232 for(x = 0; x < w; ++x)
235 v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5));
237 v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5));
238 // imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
239 // this value is never used
241 if(v < vmin || (x == 0 && y == 0))
243 if(v > vmax || (x == 0 && y == 0))
251 double sa, sfa, sffa, sfva, sva;
253 sa = sfa = sffa = sfva = sva = 0;
256 for(y = 0; y < h; ++y)
257 for(x = 0; x < w; ++x)
259 a = (int)refmap[(w*y+x)*4+3];
260 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);
261 v = (v - 128.0) / 127.0;
263 f = creal(imgspace1[(w*y+x)]);
265 f = imgspace1[(w*y+x)][0];
281 /* linear regression ftw */
282 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
283 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
285 else /* all values of v are equal, so we cannot get scale; we can still get offset */
287 o = ((sva - sfa) / sa);
292 * now apply user-given offset and scale to these values
293 * (x * s + o) * scale + offset
294 * x * s * scale + o * scale + offset
305 scale = 2 / (vmax - vmin);
306 offset = -(vmax + vmin) / (vmax - vmin);
310 // negative scale = match median to offset
312 fprintf(stderr, "Calculating median...\n");
314 float *medianbuf = malloc(sizeof(float) * w * h);
317 fprintf(stderr, " converting...\n");
318 /* renormalize, find min/max */
319 for(y = 0; y < h; ++y)
320 for(x = 0; x < w; ++x)
323 v = creal(imgspace1[(w*y+x)]);
325 v = (imgspace1[(w*y+x)][0]);
327 medianbuf[w*y+x] = v;
329 fprintf(stderr, " sorting...\n");
330 qsort(medianbuf, w*h, sizeof(*medianbuf), floatcmp);
331 fprintf(stderr, " done.\n");
333 vmed = medianbuf[(w*h-1)/2];
335 vmed = (medianbuf[(w*h)/2] + medianbuf[(w*h-2)/2]) * 0.5;
337 // we actually want (v - vmed) * scale + offset
338 offset -= vmed * scale;
341 printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n",
342 vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset);
344 for(y = 0; y < h; ++y)
345 for(x = 0; x < w; ++x)
348 v = creal(imgspace1[(w*y+x)]);
350 v = imgspace1[(w*y+x)][0];
352 v = v * scale + offset;
357 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
360 fftw_destroy_plan(i12f1);
361 fftw_destroy_plan(i22f2);
362 fftw_destroy_plan(f12i1);
364 fftw_free(freqspace2);
365 fftw_free(freqspace1);
366 fftw_free(imgspace2);
367 fftw_free(imgspace1);
370 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
380 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
381 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
382 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
383 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
384 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
385 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
386 fftw_plan f22i2 = fftw_plan_dft_2d(h, w, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
388 for(y = 0; y < h; ++y)
389 for(x = 0; x < w; ++x)
397 v = map[(w*y+x)*4+src_chan];
400 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
404 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);
408 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
410 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
411 imgspace1[(w*y+x)][1] = 0;
414 v = 1; /* do not write alpha zero */
415 map[(w*y+x)*4+3] = floor(v + 0.5);
418 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
422 for(y = 0; y < h; ++y)
423 for(x = 0; x < w; ++x)
432 fx = sin(fx * TWO_PI / w);
433 fy = sin(fy * TWO_PI / h);
436 /* a lowpass to prevent the worst */
437 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
438 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
440 /* a lowpass to prevent the worst */
441 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
442 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
443 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
444 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
448 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
449 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
451 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
452 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
453 save = freqspace1[(w*y+x)][0];
454 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
455 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
464 for(y = 0; y < h; ++y)
465 for(x = 0; x < w; ++x)
468 nx = creal(imgspace1[(w*y+x)]);
469 ny = creal(imgspace2[(w*y+x)]);
471 nx = imgspace1[(w*y+x)][0];
472 ny = imgspace2[(w*y+x)][0];
477 v = -sqrt(nx*nx + ny*ny + nz*nz);
481 ny = -ny; /* DP inverted normals */
482 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
483 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
484 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
487 fftw_destroy_plan(i12f1);
488 fftw_destroy_plan(f12i1);
489 fftw_destroy_plan(f22i2);
491 fftw_free(freqspace2);
492 fftw_free(freqspace1);
493 fftw_free(imgspace2);
494 fftw_free(imgspace1);
497 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale, const double *filter, int filterw, int filterh)
503 double *img_reduced = malloc(w*h * sizeof(double));
505 for(y = 0; y < h; ++y)
506 for(x = 0; x < w; ++x)
514 v = map[(w*y+x)*4+src_chan];
517 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
521 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);
524 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
526 v = 1; /* do not write alpha zero */
527 map[(w*y+x)*4+3] = floor(v + 0.5);
530 for(y = 0; y < h; ++y)
531 for(x = 0; x < w; ++x)
536 for(i = -filterh / 2; i <= filterh / 2; ++i)
537 for(j = -filterw / 2; j <= filterw / 2; ++j)
539 nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
540 ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
543 v = -sqrt(nx*nx + ny*ny + nz*nz);
547 ny = -ny; /* DP inverted normals */
548 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
549 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
550 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
556 unsigned char *FS_LoadFile(const char *fn, int *len)
558 unsigned char *buf = NULL;
560 FILE *f = fopen(fn, "rb");
566 buf = realloc(buf, *len + 65536);
574 n = fread(buf + *len, 1, 65536, f);
589 int FS_WriteFile(const char *fn, unsigned char *data, int len)
591 FILE *f = fopen(fn, "wb");
594 if(fwrite(data, len, 1, f) != 1)
604 /* START stuff that originates from image.c in DarkPlaces */
605 int image_width, image_height;
607 typedef struct _TargaHeader
609 unsigned char id_length, colormap_type, image_type;
610 unsigned short colormap_index, colormap_length;
611 unsigned char colormap_size;
612 unsigned short x_origin, y_origin, width, height;
613 unsigned char pixel_size, attributes;
617 void PrintTargaHeader(TargaHeader *t)
619 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);
622 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
624 int x, y, pix_inc, row_inci, runlen, alphabits;
625 unsigned char *image_buffer;
626 unsigned int *pixbufi;
627 const unsigned char *fin, *enddata;
628 TargaHeader targa_header;
629 unsigned int palettei[256];
640 enddata = f + filesize;
642 targa_header.id_length = f[0];
643 targa_header.colormap_type = f[1];
644 targa_header.image_type = f[2];
646 targa_header.colormap_index = f[3] + f[4] * 256;
647 targa_header.colormap_length = f[5] + f[6] * 256;
648 targa_header.colormap_size = f[7];
649 targa_header.x_origin = f[8] + f[9] * 256;
650 targa_header.y_origin = f[10] + f[11] * 256;
651 targa_header.width = image_width = f[12] + f[13] * 256;
652 targa_header.height = image_height = f[14] + f[15] * 256;
653 targa_header.pixel_size = f[16];
654 targa_header.attributes = f[17];
656 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
658 printf("LoadTGA: invalid size\n");
659 PrintTargaHeader(&targa_header);
663 /* advance to end of header */
666 /* skip TARGA image comment (usually 0 bytes) */
667 fin += targa_header.id_length;
669 /* read/skip the colormap if present (note: according to the TARGA spec it */
670 /* can be present even on 1color or greyscale images, just not used by */
671 /* the image data) */
672 if (targa_header.colormap_type)
674 if (targa_header.colormap_length > 256)
676 printf("LoadTGA: only up to 256 colormap_length supported\n");
677 PrintTargaHeader(&targa_header);
680 if (targa_header.colormap_index)
682 printf("LoadTGA: colormap_index not supported\n");
683 PrintTargaHeader(&targa_header);
686 if (targa_header.colormap_size == 24)
688 for (x = 0;x < targa_header.colormap_length;x++)
694 palettei[x] = bgra.i;
697 else if (targa_header.colormap_size == 32)
699 memcpy(palettei, fin, targa_header.colormap_length*4);
700 fin += targa_header.colormap_length * 4;
704 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
705 PrintTargaHeader(&targa_header);
710 /* check our pixel_size restrictions according to image_type */
711 switch (targa_header.image_type & ~8)
714 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
716 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
717 PrintTargaHeader(&targa_header);
722 /* set up a palette to make the loader easier */
723 for (x = 0;x < 256;x++)
725 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
727 palettei[x] = bgra.i;
729 /* fall through to colormap case */
731 if (targa_header.pixel_size != 8)
733 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
734 PrintTargaHeader(&targa_header);
739 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
740 PrintTargaHeader(&targa_header);
744 if (targa_header.attributes & 0x10)
746 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
750 /* number of attribute bits per pixel, we only support 0 or 8 */
751 alphabits = targa_header.attributes & 0x0F;
752 if (alphabits != 8 && alphabits != 0)
754 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
758 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
761 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
765 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
766 if ((targa_header.attributes & 0x20) == 0)
768 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
769 row_inci = -image_width*2;
773 pixbufi = (unsigned int*)image_buffer;
780 if ((targa_header.image_type & ~8) == 2)
781 pix_inc = (targa_header.pixel_size + 7) / 8;
782 switch (targa_header.image_type)
784 case 1: /* colormapped, uncompressed */
785 case 3: /* greyscale, uncompressed */
786 if (fin + image_width * image_height * pix_inc > enddata)
788 for (y = 0;y < image_height;y++, pixbufi += row_inci)
789 for (x = 0;x < image_width;x++)
790 *pixbufi++ = palettei[*fin++];
793 /* BGR or BGRA, uncompressed */
794 if (fin + image_width * image_height * pix_inc > enddata)
796 if (targa_header.pixel_size == 32 && alphabits)
798 for (y = 0;y < image_height;y++)
799 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
803 for (y = 0;y < image_height;y++, pixbufi += row_inci)
805 for (x = 0;x < image_width;x++, fin += pix_inc)
816 case 9: /* colormapped, RLE */
817 case 11: /* greyscale, RLE */
818 for (y = 0;y < image_height;y++, pixbufi += row_inci)
820 for (x = 0;x < image_width;)
823 break; /* error - truncated file */
827 /* RLE - all pixels the same color */
829 if (fin + pix_inc > enddata)
830 break; /* error - truncated file */
831 if (x + runlen > image_width)
832 break; /* error - line exceeds width */
833 bgra.i = palettei[*fin++];
839 /* uncompressed - all pixels different color */
841 if (fin + pix_inc * runlen > enddata)
842 break; /* error - truncated file */
843 if (x + runlen > image_width)
844 break; /* error - line exceeds width */
846 *pixbufi++ = palettei[*fin++];
850 if (x != image_width)
852 /* pixbufi is useless now */
853 printf("LoadTGA: corrupt file\n");
859 /* BGR or BGRA, RLE */
860 if (targa_header.pixel_size == 32 && alphabits)
862 for (y = 0;y < image_height;y++, pixbufi += row_inci)
864 for (x = 0;x < image_width;)
867 break; /* error - truncated file */
871 /* RLE - all pixels the same color */
873 if (fin + pix_inc > enddata)
874 break; /* error - truncated file */
875 if (x + runlen > image_width)
876 break; /* error - line exceeds width */
887 /* uncompressed - all pixels different color */
889 if (fin + pix_inc * runlen > enddata)
890 break; /* error - truncated file */
891 if (x + runlen > image_width)
892 break; /* error - line exceeds width */
905 if (x != image_width)
907 /* pixbufi is useless now */
908 printf("LoadTGA: corrupt file\n");
915 for (y = 0;y < image_height;y++, pixbufi += row_inci)
917 for (x = 0;x < image_width;)
920 break; /* error - truncated file */
924 /* RLE - all pixels the same color */
926 if (fin + pix_inc > enddata)
927 break; /* error - truncated file */
928 if (x + runlen > image_width)
929 break; /* error - line exceeds width */
940 /* uncompressed - all pixels different color */
942 if (fin + pix_inc * runlen > enddata)
943 break; /* error - truncated file */
944 if (x + runlen > image_width)
945 break; /* error - line exceeds width */
958 if (x != image_width)
960 /* pixbufi is useless now */
961 printf("LoadTGA: corrupt file\n");
968 /* unknown image_type */
975 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
978 unsigned char *buffer, *out;
979 const unsigned char *in, *end;
982 buffer = (unsigned char *)malloc(width*height*4 + 18);
984 memset (buffer, 0, 18);
985 buffer[2] = 2; /* uncompressed type */
986 buffer[12] = (width >> 0) & 0xFF;
987 buffer[13] = (width >> 8) & 0xFF;
988 buffer[14] = (height >> 0) & 0xFF;
989 buffer[15] = (height >> 8) & 0xFF;
991 for (y = 3;y < width*height*4;y += 4)
995 if (y < width*height*4)
997 /* save the alpha channel */
998 buffer[16] = 32; /* pixel size */
999 buffer[17] = 8; /* 8 bits of alpha */
1001 /* flip upside down */
1003 for (y = height - 1;y >= 0;y--)
1005 memcpy(out, data + y * width * 4, width * 4);
1011 /* save only the color channels */
1012 buffer[16] = 24; /* pixel size */
1013 buffer[17] = 0; /* 8 bits of alpha */
1015 /* truncate bgra to bgr and flip upside down */
1017 for (y = height - 1;y >= 0;y--)
1019 in = data + y * width * 4;
1020 end = in + width * 4;
1021 for (;in < end;in += 4)
1029 ret = FS_WriteFile (filename, buffer, out - buffer);
1035 /* START stuff that originates from image.c in DarkPlaces */
1037 int usage(const char *me)
1039 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> filtertype [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
1040 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -1 [<scale>] (read from B)\n", me);
1041 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -2 [<scale>] (read from G)\n", me);
1042 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -3 [<scale>] (read from R)\n", me);
1043 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -4 [<scale>] (read from A)\n", me);
1044 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -5 [<scale>] (read from (R+G+B)/3)\n", me);
1045 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -6 [<scale>] (read from Y)\n", me);
1049 static const double filter_scharr3[3][3] = {
1050 { -3/32.0, 0, 3/32.0 },
1051 { -10/32.0, 0, 10/32.0 },
1052 { -3/32.0, 0, 3/32.0 }
1055 static const double filter_prewitt3[3][3] = {
1056 { -1/6.0, 0, 1/6.0 },
1057 { -1/6.0, 0, 1/6.0 },
1058 { -1/6.0, 0, 1/6.0 }
1061 // pathologic for inverting
1062 static const double filter_sobel3[3][3] = {
1063 { -1/8.0, 0, 1/8.0 },
1064 { -2/8.0, 0, 2/8.0 },
1065 { -1/8.0, 0, 1/8.0 }
1068 // pathologic for inverting
1069 static const double filter_sobel5[5][5] = {
1070 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 },
1071 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1072 { -6/128.0, -12/128.0, 0, 12/128.0, 6/128.0 },
1073 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1074 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 }
1077 // pathologic for inverting
1078 static const double filter_prewitt5[5][5] = {
1079 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1080 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1081 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1082 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1083 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }
1086 static const double filter_trivial[1][3] = {
1090 int main(int argc, char **argv)
1092 const char *infile, *outfile, *reffile;
1093 double scale, offset;
1096 int renormalize = 0;
1097 double highpass = 0;
1098 unsigned char *nmapdata, *nmap, *refmap;
1099 const char *filtertype;
1100 const double *filter = NULL;
1101 int filterw = 0, filterh = 0;
1102 #define USE_FILTER(f) \
1105 filterw = sizeof(*(f)) / sizeof(**(f)); \
1106 filterh = sizeof((f)) / sizeof(*(f)); \
1107 filter = &(f)[0][0]; \
1114 return usage(*argv);
1119 return usage(*argv);
1122 filtertype = argv[3];
1124 return usage(*argv);
1127 scale = atof(argv[4]);
1132 offset = atof(argv[5]);
1134 offset = (scale<0) ? 1 : 0;
1141 // experimental features
1142 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"))
1143 renormalize = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"));
1144 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"))
1145 highpass = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"));
1146 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"))
1147 use_median = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"));
1149 nmapdata = FS_LoadFile(infile, &nmaplen);
1152 printf("FS_LoadFile failed\n");
1155 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
1159 printf("LoadTGA_BGRA failed\n");
1167 nmapdata = FS_LoadFile(reffile, &nmaplen);
1170 printf("FS_LoadFile failed\n");
1173 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
1177 printf("LoadTGA_BGRA failed\n");
1180 if(image_width != w || image_height != h)
1182 printf("reference map must have same size as input normalmap\n");
1189 if(!strcmp(filtertype, "trivial"))
1190 USE_FILTER(filter_trivial);
1191 if(!strcmp(filtertype, "prewitt3"))
1192 USE_FILTER(filter_prewitt3);
1193 if(!strcmp(filtertype, "scharr3"))
1194 USE_FILTER(filter_scharr3);
1195 if(!strcmp(filtertype, "sobel3"))
1196 USE_FILTER(filter_sobel3);
1197 if(!strcmp(filtertype, "prewitt5"))
1198 USE_FILTER(filter_prewitt5);
1199 if(!strcmp(filtertype, "sobel5"))
1200 USE_FILTER(filter_sobel5);
1205 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset, filter, filterw, filterh);
1207 hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
1210 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh, renormalize, highpass, use_median);
1212 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1214 printf("Image_WriteTGABGRA failed\n");