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;
165 map[(w*y+x)*4+0] = 128 + 127 * v;
166 map[(w*y+x)*4+1] = 128 + 127 * v;
167 map[(w*y+x)*4+2] = 128 + 127 * v;
168 map[(w*y+x)*4+3] = 255;
170 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
173 fftw_destroy_plan(i12f1);
174 fftw_destroy_plan(i22f2);
175 fftw_destroy_plan(f12i1);
177 fftw_free(freqspace2);
178 fftw_free(freqspace1);
179 fftw_free(imgspace2);
180 fftw_free(imgspace1);
183 unsigned char *FS_LoadFile(const char *fn, int *len)
185 unsigned char *buf = NULL;
187 FILE *f = fopen(fn, "rb");
193 buf = realloc(buf, *len + 65536);
201 n = fread(buf + *len, 1, 65536, f);
216 int FS_WriteFile(const char *fn, unsigned char *data, int len)
218 FILE *f = fopen(fn, "wb");
221 if(fwrite(data, len, 1, f) != 1)
231 /* START stuff that originates from image.c in DarkPlaces */
232 int image_width, image_height;
234 typedef struct _TargaHeader
236 unsigned char id_length, colormap_type, image_type;
237 unsigned short colormap_index, colormap_length;
238 unsigned char colormap_size;
239 unsigned short x_origin, y_origin, width, height;
240 unsigned char pixel_size, attributes;
244 void PrintTargaHeader(TargaHeader *t)
246 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);
249 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
251 int x, y, pix_inc, row_inci, runlen, alphabits;
252 unsigned char *image_buffer;
253 unsigned int *pixbufi;
254 const unsigned char *fin, *enddata;
255 TargaHeader targa_header;
256 unsigned int palettei[256];
267 enddata = f + filesize;
269 targa_header.id_length = f[0];
270 targa_header.colormap_type = f[1];
271 targa_header.image_type = f[2];
273 targa_header.colormap_index = f[3] + f[4] * 256;
274 targa_header.colormap_length = f[5] + f[6] * 256;
275 targa_header.colormap_size = f[7];
276 targa_header.x_origin = f[8] + f[9] * 256;
277 targa_header.y_origin = f[10] + f[11] * 256;
278 targa_header.width = image_width = f[12] + f[13] * 256;
279 targa_header.height = image_height = f[14] + f[15] * 256;
280 targa_header.pixel_size = f[16];
281 targa_header.attributes = f[17];
283 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
285 printf("LoadTGA: invalid size\n");
286 PrintTargaHeader(&targa_header);
290 /* advance to end of header */
293 /* skip TARGA image comment (usually 0 bytes) */
294 fin += targa_header.id_length;
296 /* read/skip the colormap if present (note: according to the TARGA spec it */
297 /* can be present even on 1color or greyscale images, just not used by */
298 /* the image data) */
299 if (targa_header.colormap_type)
301 if (targa_header.colormap_length > 256)
303 printf("LoadTGA: only up to 256 colormap_length supported\n");
304 PrintTargaHeader(&targa_header);
307 if (targa_header.colormap_index)
309 printf("LoadTGA: colormap_index not supported\n");
310 PrintTargaHeader(&targa_header);
313 if (targa_header.colormap_size == 24)
315 for (x = 0;x < targa_header.colormap_length;x++)
321 palettei[x] = bgra.i;
324 else if (targa_header.colormap_size == 32)
326 memcpy(palettei, fin, targa_header.colormap_length*4);
327 fin += targa_header.colormap_length * 4;
331 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
332 PrintTargaHeader(&targa_header);
337 /* check our pixel_size restrictions according to image_type */
338 switch (targa_header.image_type & ~8)
341 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
343 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
344 PrintTargaHeader(&targa_header);
349 /* set up a palette to make the loader easier */
350 for (x = 0;x < 256;x++)
352 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
354 palettei[x] = bgra.i;
356 /* fall through to colormap case */
358 if (targa_header.pixel_size != 8)
360 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
361 PrintTargaHeader(&targa_header);
366 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
367 PrintTargaHeader(&targa_header);
371 if (targa_header.attributes & 0x10)
373 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
377 /* number of attribute bits per pixel, we only support 0 or 8 */
378 alphabits = targa_header.attributes & 0x0F;
379 if (alphabits != 8 && alphabits != 0)
381 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
385 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
388 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
392 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
393 if ((targa_header.attributes & 0x20) == 0)
395 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
396 row_inci = -image_width*2;
400 pixbufi = (unsigned int*)image_buffer;
407 if ((targa_header.image_type & ~8) == 2)
408 pix_inc = (targa_header.pixel_size + 7) / 8;
409 switch (targa_header.image_type)
411 case 1: /* colormapped, uncompressed */
412 case 3: /* greyscale, uncompressed */
413 if (fin + image_width * image_height * pix_inc > enddata)
415 for (y = 0;y < image_height;y++, pixbufi += row_inci)
416 for (x = 0;x < image_width;x++)
417 *pixbufi++ = palettei[*fin++];
420 /* BGR or BGRA, uncompressed */
421 if (fin + image_width * image_height * pix_inc > enddata)
423 if (targa_header.pixel_size == 32 && alphabits)
425 for (y = 0;y < image_height;y++)
426 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
430 for (y = 0;y < image_height;y++, pixbufi += row_inci)
432 for (x = 0;x < image_width;x++, fin += pix_inc)
443 case 9: /* colormapped, RLE */
444 case 11: /* greyscale, RLE */
445 for (y = 0;y < image_height;y++, pixbufi += row_inci)
447 for (x = 0;x < image_width;)
450 break; /* error - truncated file */
454 /* RLE - all pixels the same color */
456 if (fin + pix_inc > enddata)
457 break; /* error - truncated file */
458 if (x + runlen > image_width)
459 break; /* error - line exceeds width */
460 bgra.i = palettei[*fin++];
466 /* uncompressed - all pixels different color */
468 if (fin + pix_inc * runlen > enddata)
469 break; /* error - truncated file */
470 if (x + runlen > image_width)
471 break; /* error - line exceeds width */
473 *pixbufi++ = palettei[*fin++];
477 if (x != image_width)
479 /* pixbufi is useless now */
480 printf("LoadTGA: corrupt file\n");
486 /* BGR or BGRA, RLE */
487 if (targa_header.pixel_size == 32 && alphabits)
489 for (y = 0;y < image_height;y++, pixbufi += row_inci)
491 for (x = 0;x < image_width;)
494 break; /* error - truncated file */
498 /* RLE - all pixels the same color */
500 if (fin + pix_inc > enddata)
501 break; /* error - truncated file */
502 if (x + runlen > image_width)
503 break; /* error - line exceeds width */
514 /* uncompressed - all pixels different color */
516 if (fin + pix_inc * runlen > enddata)
517 break; /* error - truncated file */
518 if (x + runlen > image_width)
519 break; /* error - line exceeds width */
532 if (x != image_width)
534 /* pixbufi is useless now */
535 printf("LoadTGA: corrupt file\n");
542 for (y = 0;y < image_height;y++, pixbufi += row_inci)
544 for (x = 0;x < image_width;)
547 break; /* error - truncated file */
551 /* RLE - all pixels the same color */
553 if (fin + pix_inc > enddata)
554 break; /* error - truncated file */
555 if (x + runlen > image_width)
556 break; /* error - line exceeds width */
567 /* uncompressed - all pixels different color */
569 if (fin + pix_inc * runlen > enddata)
570 break; /* error - truncated file */
571 if (x + runlen > image_width)
572 break; /* error - line exceeds width */
585 if (x != image_width)
587 /* pixbufi is useless now */
588 printf("LoadTGA: corrupt file\n");
595 /* unknown image_type */
602 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
605 unsigned char *buffer, *out;
606 const unsigned char *in, *end;
609 buffer = (unsigned char *)malloc(width*height*4 + 18);
611 memset (buffer, 0, 18);
612 buffer[2] = 2; /* uncompressed type */
613 buffer[12] = (width >> 0) & 0xFF;
614 buffer[13] = (width >> 8) & 0xFF;
615 buffer[14] = (height >> 0) & 0xFF;
616 buffer[15] = (height >> 8) & 0xFF;
618 for (y = 3;y < width*height*4;y += 4)
622 if (y < width*height*4)
624 /* save the alpha channel */
625 buffer[16] = 32; /* pixel size */
626 buffer[17] = 8; /* 8 bits of alpha */
628 /* flip upside down */
630 for (y = height - 1;y >= 0;y--)
632 memcpy(out, data + y * width * 4, width * 4);
638 /* save only the color channels */
639 buffer[16] = 24; /* pixel size */
640 buffer[17] = 0; /* 8 bits of alpha */
642 /* truncate bgra to bgr and flip upside down */
644 for (y = height - 1;y >= 0;y--)
646 in = data + y * width * 4;
647 end = in + width * 4;
648 for (;in < end;in += 4)
656 ret = FS_WriteFile (filename, buffer, out - buffer);
662 /* START stuff that originates from image.c in DarkPlaces */
664 int usage(const char *me)
666 printf("Usage: %s <infile.tga> <outfile.tga> [<scale> [<offset>]]\n", me);
670 int main(int argc, char **argv)
672 const char *infile, *outfile;
673 double scale, offset;
675 unsigned char *nmapdata, *nmap;
688 scale = atof(argv[3]);
693 offset = atof(argv[4]);
697 nmapdata = FS_LoadFile(infile, &nmaplen);
700 printf("FS_LoadFile failed\n");
703 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
707 printf("LoadTGA_BGRA failed\n");
710 nmap_to_hmap(nmap, image_width, image_height, scale, offset);
711 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
713 printf("Image_WriteTGABGRA failed\n");