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 void nmap_to_hmap(unsigned char *map, int w, int h, double scale, double offset)
45 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
46 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
47 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
48 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
49 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
50 fftw_plan i22f2 = fftw_plan_dft_2d(w, h, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE);
51 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
53 for(y = 0; y < h; ++y)
54 for(x = 0; x < w; ++x)
57 * unnormalized normals:
61 * BUT: darkplaces uses inverted normals, n_y actually is dh/dy by image pixel coordinates
63 nx = (int)map[(w*y+x)*4+2] - 127.5;
64 ny = (int)map[(w*y+x)*4+1] - 127.5;
65 nz = (int)map[(w*y+x)*4+0] - 127.5;
67 /* reconstruct the derivatives from here */
69 imgspace1[(w*y+x)] = nx / nz; /* = dz/dx */
70 imgspace2[(w*y+x)] = -ny / nz; /* = dz/dy */
72 imgspace1[(w*y+x)][0] = nx / nz; /* = dz/dx */
73 imgspace1[(w*y+x)][1] = 0;
74 imgspace2[(w*y+x)][0] = -ny / nz; /* = dz/dy */
75 imgspace2[(w*y+x)][1] = 0;
79 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
84 for(y = 0; y < h; ++y)
85 for(x = 0; x < w; ++x)
95 freqspace1[(w*y+x)] = I * (fx * freqspace1[(w*y+x)] + fy * freqspace2[(w*y+x)]) / (fx*fx + fy*fy);
97 freqspace1[(w*y+x)] = 0;
101 save = freqspace1[(w*y+x)][0];
102 freqspace1[(w*y+x)][0] = -(fx * freqspace1[(w*y+x)][1] + fy * freqspace2[(w*y+x)][1]) / (fx*fx + fy*fy);
103 freqspace1[(w*y+x)][1] = (fx * save + fy * freqspace2[(w*y+x)][0]) / (fx*fx + fy*fy);
107 freqspace1[(w*y+x)][0] = 0;
108 freqspace1[(w*y+x)][1] = 0;
118 vmin = vmax = creal(imgspace1[0]);
120 vmin = vmax = imgspace1[0][0];
122 for(y = 0; y < h; ++y)
123 for(x = 0; x < w; ++x)
126 v = creal(imgspace1[(w*y+x)]);
128 v = imgspace1[(w*y+x)][0];
143 scale = 2 / (vmax - vmin);
144 offset = -(vmax + vmin) / (vmax - vmin);
146 printf("Autocomputed scale: %f\nAutocomputed offset: %f\n", scale, offset);
151 for(y = 0; y < h; ++y)
152 for(x = 0; x < w; ++x)
155 v = creal(imgspace1[(w*y+x)]);
157 v = imgspace1[(w*y+x)][0];
159 v = v * scale + offset;
164 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
167 fftw_destroy_plan(i12f1);
168 fftw_destroy_plan(i22f2);
169 fftw_destroy_plan(f12i1);
171 fftw_free(freqspace2);
172 fftw_free(freqspace1);
173 fftw_free(imgspace2);
174 fftw_free(imgspace1);
177 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
181 double v, vmin, vmax;
186 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
187 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
188 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
189 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
190 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
191 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
192 fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
194 for(y = 0; y < h; ++y)
195 for(x = 0; x < w; ++x)
203 v = map[(w*y+x)*4+src_chan];
206 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
210 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);
214 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
216 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
217 imgspace1[(w*y+x)][1] = 0;
219 map[(w*y+x)*4+3] = floor(v + 0.5);
222 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
226 for(y = 0; y < h; ++y)
227 for(x = 0; x < w; ++x)
236 /* a lowpass to prevent the worst */
237 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
238 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
240 freqspace2[(w*y+x)] = I * fy * freqspace1[(w*y+x)]; /* y derivative */
241 freqspace1[(w*y+x)] = I * fx * freqspace1[(w*y+x)]; /* x derivative */
243 /* a lowpass to prevent the worst */
244 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
245 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
246 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
247 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
249 freqspace2[(w*y+x)][0] = -fy * freqspace1[(w*y+x)][1]; /* y derivative */
250 freqspace2[(w*y+x)][1] = fy * freqspace1[(w*y+x)][0];
251 save = freqspace1[(w*y+x)][0];
252 freqspace1[(w*y+x)][0] = -fx * freqspace1[(w*y+x)][1]; /* x derivative */
253 freqspace1[(w*y+x)][1] = fx * save;
262 for(y = 0; y < h; ++y)
263 for(x = 0; x < w; ++x)
266 nx = creal(imgspace1[(w*y+x)]);
267 ny = creal(imgspace2[(w*y+x)]);
269 nx = imgspace1[(w*y+x)][0];
270 ny = imgspace2[(w*y+x)][0];
273 v = sqrt(nx*nx + ny*ny + nz*nz);
277 ny = -ny; /* DP inverted normals */
278 map[(w*y+x)*4+2] = floor(127.5 + 127.5 * nx);
279 map[(w*y+x)*4+1] = floor(127.5 + 127.5 * ny);
280 map[(w*y+x)*4+0] = floor(127.5 + 127.5 * nz);
283 fftw_destroy_plan(i12f1);
284 fftw_destroy_plan(f12i1);
285 fftw_destroy_plan(f22i2);
287 fftw_free(freqspace2);
288 fftw_free(freqspace1);
289 fftw_free(imgspace2);
290 fftw_free(imgspace1);
293 unsigned char *FS_LoadFile(const char *fn, int *len)
295 unsigned char *buf = NULL;
297 FILE *f = fopen(fn, "rb");
303 buf = realloc(buf, *len + 65536);
311 n = fread(buf + *len, 1, 65536, f);
326 int FS_WriteFile(const char *fn, unsigned char *data, int len)
328 FILE *f = fopen(fn, "wb");
331 if(fwrite(data, len, 1, f) != 1)
341 /* START stuff that originates from image.c in DarkPlaces */
342 int image_width, image_height;
344 typedef struct _TargaHeader
346 unsigned char id_length, colormap_type, image_type;
347 unsigned short colormap_index, colormap_length;
348 unsigned char colormap_size;
349 unsigned short x_origin, y_origin, width, height;
350 unsigned char pixel_size, attributes;
354 void PrintTargaHeader(TargaHeader *t)
356 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);
359 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
361 int x, y, pix_inc, row_inci, runlen, alphabits;
362 unsigned char *image_buffer;
363 unsigned int *pixbufi;
364 const unsigned char *fin, *enddata;
365 TargaHeader targa_header;
366 unsigned int palettei[256];
377 enddata = f + filesize;
379 targa_header.id_length = f[0];
380 targa_header.colormap_type = f[1];
381 targa_header.image_type = f[2];
383 targa_header.colormap_index = f[3] + f[4] * 256;
384 targa_header.colormap_length = f[5] + f[6] * 256;
385 targa_header.colormap_size = f[7];
386 targa_header.x_origin = f[8] + f[9] * 256;
387 targa_header.y_origin = f[10] + f[11] * 256;
388 targa_header.width = image_width = f[12] + f[13] * 256;
389 targa_header.height = image_height = f[14] + f[15] * 256;
390 targa_header.pixel_size = f[16];
391 targa_header.attributes = f[17];
393 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
395 printf("LoadTGA: invalid size\n");
396 PrintTargaHeader(&targa_header);
400 /* advance to end of header */
403 /* skip TARGA image comment (usually 0 bytes) */
404 fin += targa_header.id_length;
406 /* read/skip the colormap if present (note: according to the TARGA spec it */
407 /* can be present even on 1color or greyscale images, just not used by */
408 /* the image data) */
409 if (targa_header.colormap_type)
411 if (targa_header.colormap_length > 256)
413 printf("LoadTGA: only up to 256 colormap_length supported\n");
414 PrintTargaHeader(&targa_header);
417 if (targa_header.colormap_index)
419 printf("LoadTGA: colormap_index not supported\n");
420 PrintTargaHeader(&targa_header);
423 if (targa_header.colormap_size == 24)
425 for (x = 0;x < targa_header.colormap_length;x++)
431 palettei[x] = bgra.i;
434 else if (targa_header.colormap_size == 32)
436 memcpy(palettei, fin, targa_header.colormap_length*4);
437 fin += targa_header.colormap_length * 4;
441 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
442 PrintTargaHeader(&targa_header);
447 /* check our pixel_size restrictions according to image_type */
448 switch (targa_header.image_type & ~8)
451 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
453 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
454 PrintTargaHeader(&targa_header);
459 /* set up a palette to make the loader easier */
460 for (x = 0;x < 256;x++)
462 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
464 palettei[x] = bgra.i;
466 /* fall through to colormap case */
468 if (targa_header.pixel_size != 8)
470 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
471 PrintTargaHeader(&targa_header);
476 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
477 PrintTargaHeader(&targa_header);
481 if (targa_header.attributes & 0x10)
483 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
487 /* number of attribute bits per pixel, we only support 0 or 8 */
488 alphabits = targa_header.attributes & 0x0F;
489 if (alphabits != 8 && alphabits != 0)
491 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
495 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
498 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
502 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
503 if ((targa_header.attributes & 0x20) == 0)
505 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
506 row_inci = -image_width*2;
510 pixbufi = (unsigned int*)image_buffer;
517 if ((targa_header.image_type & ~8) == 2)
518 pix_inc = (targa_header.pixel_size + 7) / 8;
519 switch (targa_header.image_type)
521 case 1: /* colormapped, uncompressed */
522 case 3: /* greyscale, uncompressed */
523 if (fin + image_width * image_height * pix_inc > enddata)
525 for (y = 0;y < image_height;y++, pixbufi += row_inci)
526 for (x = 0;x < image_width;x++)
527 *pixbufi++ = palettei[*fin++];
530 /* BGR or BGRA, uncompressed */
531 if (fin + image_width * image_height * pix_inc > enddata)
533 if (targa_header.pixel_size == 32 && alphabits)
535 for (y = 0;y < image_height;y++)
536 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
540 for (y = 0;y < image_height;y++, pixbufi += row_inci)
542 for (x = 0;x < image_width;x++, fin += pix_inc)
553 case 9: /* colormapped, RLE */
554 case 11: /* greyscale, RLE */
555 for (y = 0;y < image_height;y++, pixbufi += row_inci)
557 for (x = 0;x < image_width;)
560 break; /* error - truncated file */
564 /* RLE - all pixels the same color */
566 if (fin + pix_inc > enddata)
567 break; /* error - truncated file */
568 if (x + runlen > image_width)
569 break; /* error - line exceeds width */
570 bgra.i = palettei[*fin++];
576 /* uncompressed - all pixels different color */
578 if (fin + pix_inc * runlen > enddata)
579 break; /* error - truncated file */
580 if (x + runlen > image_width)
581 break; /* error - line exceeds width */
583 *pixbufi++ = palettei[*fin++];
587 if (x != image_width)
589 /* pixbufi is useless now */
590 printf("LoadTGA: corrupt file\n");
596 /* BGR or BGRA, RLE */
597 if (targa_header.pixel_size == 32 && alphabits)
599 for (y = 0;y < image_height;y++, pixbufi += row_inci)
601 for (x = 0;x < image_width;)
604 break; /* error - truncated file */
608 /* RLE - all pixels the same color */
610 if (fin + pix_inc > enddata)
611 break; /* error - truncated file */
612 if (x + runlen > image_width)
613 break; /* error - line exceeds width */
624 /* uncompressed - all pixels different color */
626 if (fin + pix_inc * runlen > enddata)
627 break; /* error - truncated file */
628 if (x + runlen > image_width)
629 break; /* error - line exceeds width */
642 if (x != image_width)
644 /* pixbufi is useless now */
645 printf("LoadTGA: corrupt file\n");
652 for (y = 0;y < image_height;y++, pixbufi += row_inci)
654 for (x = 0;x < image_width;)
657 break; /* error - truncated file */
661 /* RLE - all pixels the same color */
663 if (fin + pix_inc > enddata)
664 break; /* error - truncated file */
665 if (x + runlen > image_width)
666 break; /* error - line exceeds width */
677 /* uncompressed - all pixels different color */
679 if (fin + pix_inc * runlen > enddata)
680 break; /* error - truncated file */
681 if (x + runlen > image_width)
682 break; /* error - line exceeds width */
695 if (x != image_width)
697 /* pixbufi is useless now */
698 printf("LoadTGA: corrupt file\n");
705 /* unknown image_type */
712 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
715 unsigned char *buffer, *out;
716 const unsigned char *in, *end;
719 buffer = (unsigned char *)malloc(width*height*4 + 18);
721 memset (buffer, 0, 18);
722 buffer[2] = 2; /* uncompressed type */
723 buffer[12] = (width >> 0) & 0xFF;
724 buffer[13] = (width >> 8) & 0xFF;
725 buffer[14] = (height >> 0) & 0xFF;
726 buffer[15] = (height >> 8) & 0xFF;
728 for (y = 3;y < width*height*4;y += 4)
732 if (y < width*height*4)
734 /* save the alpha channel */
735 buffer[16] = 32; /* pixel size */
736 buffer[17] = 8; /* 8 bits of alpha */
738 /* flip upside down */
740 for (y = height - 1;y >= 0;y--)
742 memcpy(out, data + y * width * 4, width * 4);
748 /* save only the color channels */
749 buffer[16] = 24; /* pixel size */
750 buffer[17] = 0; /* 8 bits of alpha */
752 /* truncate bgra to bgr and flip upside down */
754 for (y = height - 1;y >= 0;y--)
756 in = data + y * width * 4;
757 end = in + width * 4;
758 for (;in < end;in += 4)
766 ret = FS_WriteFile (filename, buffer, out - buffer);
772 /* START stuff that originates from image.c in DarkPlaces */
774 int usage(const char *me)
776 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset>]] (get heightmap from normalmap)\n", me);
777 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from R)\n", me);
778 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G)\n", me);
779 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R)\n", me);
780 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -4 [<scale>] (read from A)\n", me);
781 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -5 [<scale>] (read from (R+G+B)/3)\n", me);
782 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -6 [<scale>] (read from Y)\n", me);
786 int main(int argc, char **argv)
788 const char *infile, *outfile;
789 double scale, offset;
791 unsigned char *nmapdata, *nmap;
804 scale = atof(argv[3]);
809 offset = atof(argv[4]);
813 nmapdata = FS_LoadFile(infile, &nmaplen);
816 printf("FS_LoadFile failed\n");
819 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
823 printf("LoadTGA_BGRA failed\n");
827 hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
829 nmap_to_hmap(nmap, image_width, image_height, scale, offset);
830 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
832 printf("Image_WriteTGABGRA failed\n");