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 void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
47 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
48 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
49 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
50 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
51 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
52 fftw_plan i22f2 = fftw_plan_dft_2d(w, h, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE);
53 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
55 for(y = 0; y < h; ++y)
56 for(x = 0; x < w; ++x)
59 * unnormalized normals:
63 * BUT: darkplaces uses inverted normals, n_y actually is dh/dy by image pixel coordinates
65 nx = ((int)map[(w*y+x)*4+2] - 127.5) / 128;
66 ny = ((int)map[(w*y+x)*4+1] - 127.5) / 128;
67 nz = ((int)map[(w*y+x)*4+0] - 127.5) / 128;
69 /* reconstruct the derivatives from here */
71 imgspace1[(w*y+x)] = nx / nz * w; /* = dz/dx */
72 imgspace2[(w*y+x)] = -ny / nz * h; /* = dz/dy */
74 imgspace1[(w*y+x)][0] = nx / nz; /* = dz/dx */
75 imgspace1[(w*y+x)][1] = 0;
76 imgspace2[(w*y+x)][0] = -ny / nz; /* = dz/dy */
77 imgspace2[(w*y+x)][1] = 0;
81 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
86 for(y = 0; y < h; ++y)
87 for(x = 0; x < w; ++x)
97 freqspace1[(w*y+x)] = I * (fx * freqspace1[(w*y+x)] + fy * freqspace2[(w*y+x)]) / (fx*fx + fy*fy) / TWO_PI;
99 freqspace1[(w*y+x)] = 0;
103 save = freqspace1[(w*y+x)][0];
104 freqspace1[(w*y+x)][0] = -(fx * freqspace1[(w*y+x)][1] + fy * freqspace2[(w*y+x)][1]) / (fx*fx + fy*fy) / TWO_PI;
105 freqspace1[(w*y+x)][1] = (fx * save + fy * freqspace2[(w*y+x)][0]) / (fx*fx + fy*fy) / TWO_PI;
109 freqspace1[(w*y+x)][0] = 0;
110 freqspace1[(w*y+x)][1] = 0;
120 vmin = vmax = creal(imgspace1[0]);
122 vmin = vmax = imgspace1[0][0];
124 for(y = 0; y < h; ++y)
125 for(x = 0; x < w; ++x)
128 v = creal(imgspace1[(w*y+x)]);
130 v = imgspace1[(w*y+x)][0];
145 scale = 2 / (vmax - vmin);
146 offset = -(vmax + vmin) / (vmax - vmin);
148 printf("Autocomputed scale: %f\nAutocomputed offset: %f\n", scale, offset);
153 for(y = 0; y < h; ++y)
154 for(x = 0; x < w; ++x)
157 v = creal(imgspace1[(w*y+x)]);
159 v = imgspace1[(w*y+x)][0];
161 v = v * scale + offset;
166 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
169 fftw_destroy_plan(i12f1);
170 fftw_destroy_plan(i22f2);
171 fftw_destroy_plan(f12i1);
173 fftw_free(freqspace2);
174 fftw_free(freqspace1);
175 fftw_free(imgspace2);
176 fftw_free(imgspace1);
179 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
188 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
189 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
190 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
191 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
192 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
193 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
194 fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
196 for(y = 0; y < h; ++y)
197 for(x = 0; x < w; ++x)
205 v = map[(w*y+x)*4+src_chan];
208 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
212 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);
216 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
218 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
219 imgspace1[(w*y+x)][1] = 0;
221 map[(w*y+x)*4+3] = floor(v + 0.5);
224 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
228 for(y = 0; y < h; ++y)
229 for(x = 0; x < w; ++x)
238 /* a lowpass to prevent the worst */
239 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
240 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
242 freqspace2[(w*y+x)] = TWO_PI*I * fy * freqspace1[(w*y+x)]; /* y derivative */
243 freqspace1[(w*y+x)] = TWO_PI*I * fx * freqspace1[(w*y+x)]; /* x derivative */
245 /* a lowpass to prevent the worst */
246 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
247 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
248 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
249 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
251 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
252 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
253 save = freqspace1[(w*y+x)][0];
254 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
255 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
264 for(y = 0; y < h; ++y)
265 for(x = 0; x < w; ++x)
268 nx = creal(imgspace1[(w*y+x)]);
269 ny = creal(imgspace2[(w*y+x)]);
271 nx = imgspace1[(w*y+x)][0];
272 ny = imgspace2[(w*y+x)][0];
277 v = -sqrt(nx*nx + ny*ny + nz*nz);
281 ny = -ny; /* DP inverted normals */
282 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
283 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
284 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
287 fftw_destroy_plan(i12f1);
288 fftw_destroy_plan(f12i1);
289 fftw_destroy_plan(f22i2);
291 fftw_free(freqspace2);
292 fftw_free(freqspace1);
293 fftw_free(imgspace2);
294 fftw_free(imgspace1);
297 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale)
303 double *img_reduced = malloc(w*h * sizeof(double));
304 static const double filter[3][3] = { /* filter to derive one component */
309 static const double filter_mult = 0.125;
311 for(y = 0; y < h; ++y)
312 for(x = 0; x < w; ++x)
320 v = map[(w*y+x)*4+src_chan];
323 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
327 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);
330 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
331 map[(w*y+x)*4+3] = floor(v + 0.5);
334 for(y = 0; y < h; ++y)
335 for(x = 0; x < w; ++x)
337 nz = -1 / (scale * filter_mult);
340 for(i = -(int)(sizeof(filter) / sizeof(*filter)) / 2; i <= (int)(sizeof(filter) / sizeof(*filter)) / 2; ++i)
341 for(j = -(int)(sizeof(*filter) / sizeof(**filter)) / 2; j <= (int)(sizeof(*filter) / sizeof(**filter)) / 2; ++j)
343 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];
344 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];
347 v = -sqrt(nx*nx + ny*ny + nz*nz);
351 ny = -ny; /* DP inverted normals */
352 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
353 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
354 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
360 unsigned char *FS_LoadFile(const char *fn, int *len)
362 unsigned char *buf = NULL;
364 FILE *f = fopen(fn, "rb");
370 buf = realloc(buf, *len + 65536);
378 n = fread(buf + *len, 1, 65536, f);
393 int FS_WriteFile(const char *fn, unsigned char *data, int len)
395 FILE *f = fopen(fn, "wb");
398 if(fwrite(data, len, 1, f) != 1)
408 /* START stuff that originates from image.c in DarkPlaces */
409 int image_width, image_height;
411 typedef struct _TargaHeader
413 unsigned char id_length, colormap_type, image_type;
414 unsigned short colormap_index, colormap_length;
415 unsigned char colormap_size;
416 unsigned short x_origin, y_origin, width, height;
417 unsigned char pixel_size, attributes;
421 void PrintTargaHeader(TargaHeader *t)
423 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);
426 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
428 int x, y, pix_inc, row_inci, runlen, alphabits;
429 unsigned char *image_buffer;
430 unsigned int *pixbufi;
431 const unsigned char *fin, *enddata;
432 TargaHeader targa_header;
433 unsigned int palettei[256];
444 enddata = f + filesize;
446 targa_header.id_length = f[0];
447 targa_header.colormap_type = f[1];
448 targa_header.image_type = f[2];
450 targa_header.colormap_index = f[3] + f[4] * 256;
451 targa_header.colormap_length = f[5] + f[6] * 256;
452 targa_header.colormap_size = f[7];
453 targa_header.x_origin = f[8] + f[9] * 256;
454 targa_header.y_origin = f[10] + f[11] * 256;
455 targa_header.width = image_width = f[12] + f[13] * 256;
456 targa_header.height = image_height = f[14] + f[15] * 256;
457 targa_header.pixel_size = f[16];
458 targa_header.attributes = f[17];
460 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
462 printf("LoadTGA: invalid size\n");
463 PrintTargaHeader(&targa_header);
467 /* advance to end of header */
470 /* skip TARGA image comment (usually 0 bytes) */
471 fin += targa_header.id_length;
473 /* read/skip the colormap if present (note: according to the TARGA spec it */
474 /* can be present even on 1color or greyscale images, just not used by */
475 /* the image data) */
476 if (targa_header.colormap_type)
478 if (targa_header.colormap_length > 256)
480 printf("LoadTGA: only up to 256 colormap_length supported\n");
481 PrintTargaHeader(&targa_header);
484 if (targa_header.colormap_index)
486 printf("LoadTGA: colormap_index not supported\n");
487 PrintTargaHeader(&targa_header);
490 if (targa_header.colormap_size == 24)
492 for (x = 0;x < targa_header.colormap_length;x++)
498 palettei[x] = bgra.i;
501 else if (targa_header.colormap_size == 32)
503 memcpy(palettei, fin, targa_header.colormap_length*4);
504 fin += targa_header.colormap_length * 4;
508 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
509 PrintTargaHeader(&targa_header);
514 /* check our pixel_size restrictions according to image_type */
515 switch (targa_header.image_type & ~8)
518 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
520 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
521 PrintTargaHeader(&targa_header);
526 /* set up a palette to make the loader easier */
527 for (x = 0;x < 256;x++)
529 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
531 palettei[x] = bgra.i;
533 /* fall through to colormap case */
535 if (targa_header.pixel_size != 8)
537 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
538 PrintTargaHeader(&targa_header);
543 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
544 PrintTargaHeader(&targa_header);
548 if (targa_header.attributes & 0x10)
550 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
554 /* number of attribute bits per pixel, we only support 0 or 8 */
555 alphabits = targa_header.attributes & 0x0F;
556 if (alphabits != 8 && alphabits != 0)
558 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
562 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
565 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
569 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
570 if ((targa_header.attributes & 0x20) == 0)
572 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
573 row_inci = -image_width*2;
577 pixbufi = (unsigned int*)image_buffer;
584 if ((targa_header.image_type & ~8) == 2)
585 pix_inc = (targa_header.pixel_size + 7) / 8;
586 switch (targa_header.image_type)
588 case 1: /* colormapped, uncompressed */
589 case 3: /* greyscale, uncompressed */
590 if (fin + image_width * image_height * pix_inc > enddata)
592 for (y = 0;y < image_height;y++, pixbufi += row_inci)
593 for (x = 0;x < image_width;x++)
594 *pixbufi++ = palettei[*fin++];
597 /* BGR or BGRA, uncompressed */
598 if (fin + image_width * image_height * pix_inc > enddata)
600 if (targa_header.pixel_size == 32 && alphabits)
602 for (y = 0;y < image_height;y++)
603 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
607 for (y = 0;y < image_height;y++, pixbufi += row_inci)
609 for (x = 0;x < image_width;x++, fin += pix_inc)
620 case 9: /* colormapped, RLE */
621 case 11: /* greyscale, RLE */
622 for (y = 0;y < image_height;y++, pixbufi += row_inci)
624 for (x = 0;x < image_width;)
627 break; /* error - truncated file */
631 /* RLE - all pixels the same color */
633 if (fin + pix_inc > enddata)
634 break; /* error - truncated file */
635 if (x + runlen > image_width)
636 break; /* error - line exceeds width */
637 bgra.i = palettei[*fin++];
643 /* uncompressed - all pixels different color */
645 if (fin + pix_inc * runlen > enddata)
646 break; /* error - truncated file */
647 if (x + runlen > image_width)
648 break; /* error - line exceeds width */
650 *pixbufi++ = palettei[*fin++];
654 if (x != image_width)
656 /* pixbufi is useless now */
657 printf("LoadTGA: corrupt file\n");
663 /* BGR or BGRA, RLE */
664 if (targa_header.pixel_size == 32 && alphabits)
666 for (y = 0;y < image_height;y++, pixbufi += row_inci)
668 for (x = 0;x < image_width;)
671 break; /* error - truncated file */
675 /* RLE - all pixels the same color */
677 if (fin + pix_inc > enddata)
678 break; /* error - truncated file */
679 if (x + runlen > image_width)
680 break; /* error - line exceeds width */
691 /* uncompressed - all pixels different color */
693 if (fin + pix_inc * runlen > enddata)
694 break; /* error - truncated file */
695 if (x + runlen > image_width)
696 break; /* error - line exceeds width */
709 if (x != image_width)
711 /* pixbufi is useless now */
712 printf("LoadTGA: corrupt file\n");
719 for (y = 0;y < image_height;y++, pixbufi += row_inci)
721 for (x = 0;x < image_width;)
724 break; /* error - truncated file */
728 /* RLE - all pixels the same color */
730 if (fin + pix_inc > enddata)
731 break; /* error - truncated file */
732 if (x + runlen > image_width)
733 break; /* error - line exceeds width */
744 /* uncompressed - all pixels different color */
746 if (fin + pix_inc * runlen > enddata)
747 break; /* error - truncated file */
748 if (x + runlen > image_width)
749 break; /* error - line exceeds width */
762 if (x != image_width)
764 /* pixbufi is useless now */
765 printf("LoadTGA: corrupt file\n");
772 /* unknown image_type */
779 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
782 unsigned char *buffer, *out;
783 const unsigned char *in, *end;
786 buffer = (unsigned char *)malloc(width*height*4 + 18);
788 memset (buffer, 0, 18);
789 buffer[2] = 2; /* uncompressed type */
790 buffer[12] = (width >> 0) & 0xFF;
791 buffer[13] = (width >> 8) & 0xFF;
792 buffer[14] = (height >> 0) & 0xFF;
793 buffer[15] = (height >> 8) & 0xFF;
795 for (y = 3;y < width*height*4;y += 4)
799 if (y < width*height*4)
801 /* save the alpha channel */
802 buffer[16] = 32; /* pixel size */
803 buffer[17] = 8; /* 8 bits of alpha */
805 /* flip upside down */
807 for (y = height - 1;y >= 0;y--)
809 memcpy(out, data + y * width * 4, width * 4);
815 /* save only the color channels */
816 buffer[16] = 24; /* pixel size */
817 buffer[17] = 0; /* 8 bits of alpha */
819 /* truncate bgra to bgr and flip upside down */
821 for (y = height - 1;y >= 0;y--)
823 in = data + y * width * 4;
824 end = in + width * 4;
825 for (;in < end;in += 4)
833 ret = FS_WriteFile (filename, buffer, out - buffer);
839 /* START stuff that originates from image.c in DarkPlaces */
841 int usage(const char *me)
843 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset>]] (get heightmap from normalmap)\n", me);
844 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from B, Diff)\n", me);
845 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G, Diff)\n", me);
846 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R, Diff)\n", me);
847 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -4 [<scale>] (read from A, Diff)\n", me);
848 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -5 [<scale>] (read from (R+G+B)/3, Diff)\n", me);
849 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -6 [<scale>] (read from Y, Diff)\n", me);
850 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -7 [<scale>] (read from B, FFT)\n", me);
851 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -8 [<scale>] (read from G, FFT)\n", me);
852 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -9 [<scale>] (read from R, FFT)\n", me);
853 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -10 [<scale>] (read from A, FFT)\n", me);
854 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -11 [<scale>] (read from (R+G+B)/3, FFT)\n", me);
855 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -12 [<scale>] (read from Y, FFT)\n", me);
859 int main(int argc, char **argv)
861 const char *infile, *outfile;
862 double scale, offset;
864 unsigned char *nmapdata, *nmap;
877 scale = atof(argv[3]);
882 offset = atof(argv[4]);
886 nmapdata = FS_LoadFile(infile, &nmaplen);
889 printf("FS_LoadFile failed\n");
892 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
896 printf("LoadTGA_BGRA failed\n");
900 hmap_to_nmap(nmap, image_width, image_height, -scale-7, offset);
902 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset);
904 nmap_to_hmap(nmap, image_width, image_height, scale, offset);
905 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
907 printf("Image_WriteTGABGRA failed\n");