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, const unsigned char *refmap, 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;
119 // refmap: a reference map to define the heights
120 // alpha = weight, color = value
121 // if more than one color value is used, colors are also matched
123 // we do linear regression, basically
124 // f'(x, y) = f(x, y) * scale + offset
125 // sum((f(x, y) * scale + offset - ref_y(x, y))^2 * ref_a(x, y)) minimize
128 // sum(-2*ref_y(x,y)*ref_a(x,y) + 2*scale*f(x,y)*ref_a(x,y) + 2*offset*ref_a(x,y)) = 0
130 // sum(-2*f(x,y)*ref_a(x,y) + 2*scale*f(x,y)^2*ref_a(x,y) + 2*offset*f(x,y)*ref_a(x,y)) = 0
132 // offset = (sfa*sfya - sffa*sya) / (sfa*sfa-sa*sffa)
133 // scale = (sfa*sya - sa*sfya) / (sfa*sfa-sa*sffa)
137 double sa, sfa, sffa, sfva, sva;
139 sa = sfa = sffa = sfva = sva = 0;
142 for(y = 0; y < h; ++y)
143 for(x = 0; x < w; ++x)
145 a = (int)refmap[(w*y+x)*4+0];
146 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);
147 v = (v - 128.0) / 127.0; // value 0 is forbidden, 1 -> -1, 255 -> 1
149 f = creal(imgspace1[(w*y+x)]);
151 f = imgspace1[(w*y+x)][0];
171 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
172 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
174 else // all values of v are equal, so we cannot get scale; we can still get offset
176 o = ((sva - sfa) / sa);
179 // now apply user-given offset and scale to these values
180 // (x * s + o) * scale + offset
181 // x * s * scale + o * scale + offset
188 vmin = vmax = creal(imgspace1[0]);
190 vmin = vmax = imgspace1[0][0];
192 for(y = 0; y < h; ++y)
193 for(x = 0; x < w; ++x)
196 v = creal(imgspace1[(w*y+x)]);
198 v = imgspace1[(w*y+x)][0];
213 scale = 2 / (vmax - vmin);
214 offset = -(vmax + vmin) / (vmax - vmin);
216 printf("Autocomputed scale: %f\nAutocomputed offset: %f\n", scale, offset);
221 for(y = 0; y < h; ++y)
222 for(x = 0; x < w; ++x)
225 v = creal(imgspace1[(w*y+x)]);
227 v = imgspace1[(w*y+x)][0];
229 v = v * scale + offset;
234 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
237 fftw_destroy_plan(i12f1);
238 fftw_destroy_plan(i22f2);
239 fftw_destroy_plan(f12i1);
241 fftw_free(freqspace2);
242 fftw_free(freqspace1);
243 fftw_free(imgspace2);
244 fftw_free(imgspace1);
247 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
256 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
257 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
258 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
259 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
260 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
261 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
262 fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
264 for(y = 0; y < h; ++y)
265 for(x = 0; x < w; ++x)
273 v = map[(w*y+x)*4+src_chan];
276 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
280 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);
284 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
286 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
287 imgspace1[(w*y+x)][1] = 0;
289 map[(w*y+x)*4+3] = floor(v + 0.5);
292 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
296 for(y = 0; y < h; ++y)
297 for(x = 0; x < w; ++x)
306 /* a lowpass to prevent the worst */
307 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
308 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
310 freqspace2[(w*y+x)] = TWO_PI*I * fy * freqspace1[(w*y+x)]; /* y derivative */
311 freqspace1[(w*y+x)] = TWO_PI*I * fx * freqspace1[(w*y+x)]; /* x derivative */
313 /* a lowpass to prevent the worst */
314 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
315 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
316 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
317 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
319 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
320 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
321 save = freqspace1[(w*y+x)][0];
322 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
323 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
332 for(y = 0; y < h; ++y)
333 for(x = 0; x < w; ++x)
336 nx = creal(imgspace1[(w*y+x)]);
337 ny = creal(imgspace2[(w*y+x)]);
339 nx = imgspace1[(w*y+x)][0];
340 ny = imgspace2[(w*y+x)][0];
345 v = -sqrt(nx*nx + ny*ny + nz*nz);
349 ny = -ny; /* DP inverted normals */
350 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
351 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
352 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
355 fftw_destroy_plan(i12f1);
356 fftw_destroy_plan(f12i1);
357 fftw_destroy_plan(f22i2);
359 fftw_free(freqspace2);
360 fftw_free(freqspace1);
361 fftw_free(imgspace2);
362 fftw_free(imgspace1);
365 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale)
371 double *img_reduced = malloc(w*h * sizeof(double));
372 static const double filter[3][3] = { /* filter to derive one component */
377 static const double filter_mult = 0.125;
379 for(y = 0; y < h; ++y)
380 for(x = 0; x < w; ++x)
388 v = map[(w*y+x)*4+src_chan];
391 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
395 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);
398 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
399 map[(w*y+x)*4+3] = floor(v + 0.5);
402 for(y = 0; y < h; ++y)
403 for(x = 0; x < w; ++x)
405 nz = -1 / (scale * filter_mult);
408 for(i = -(int)(sizeof(filter) / sizeof(*filter)) / 2; i <= (int)(sizeof(filter) / sizeof(*filter)) / 2; ++i)
409 for(j = -(int)(sizeof(*filter) / sizeof(**filter)) / 2; j <= (int)(sizeof(*filter) / sizeof(**filter)) / 2; ++j)
411 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];
412 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];
415 v = -sqrt(nx*nx + ny*ny + nz*nz);
419 ny = -ny; /* DP inverted normals */
420 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
421 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
422 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
428 unsigned char *FS_LoadFile(const char *fn, int *len)
430 unsigned char *buf = NULL;
432 FILE *f = fopen(fn, "rb");
438 buf = realloc(buf, *len + 65536);
446 n = fread(buf + *len, 1, 65536, f);
461 int FS_WriteFile(const char *fn, unsigned char *data, int len)
463 FILE *f = fopen(fn, "wb");
466 if(fwrite(data, len, 1, f) != 1)
476 /* START stuff that originates from image.c in DarkPlaces */
477 int image_width, image_height;
479 typedef struct _TargaHeader
481 unsigned char id_length, colormap_type, image_type;
482 unsigned short colormap_index, colormap_length;
483 unsigned char colormap_size;
484 unsigned short x_origin, y_origin, width, height;
485 unsigned char pixel_size, attributes;
489 void PrintTargaHeader(TargaHeader *t)
491 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);
494 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
496 int x, y, pix_inc, row_inci, runlen, alphabits;
497 unsigned char *image_buffer;
498 unsigned int *pixbufi;
499 const unsigned char *fin, *enddata;
500 TargaHeader targa_header;
501 unsigned int palettei[256];
512 enddata = f + filesize;
514 targa_header.id_length = f[0];
515 targa_header.colormap_type = f[1];
516 targa_header.image_type = f[2];
518 targa_header.colormap_index = f[3] + f[4] * 256;
519 targa_header.colormap_length = f[5] + f[6] * 256;
520 targa_header.colormap_size = f[7];
521 targa_header.x_origin = f[8] + f[9] * 256;
522 targa_header.y_origin = f[10] + f[11] * 256;
523 targa_header.width = image_width = f[12] + f[13] * 256;
524 targa_header.height = image_height = f[14] + f[15] * 256;
525 targa_header.pixel_size = f[16];
526 targa_header.attributes = f[17];
528 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
530 printf("LoadTGA: invalid size\n");
531 PrintTargaHeader(&targa_header);
535 /* advance to end of header */
538 /* skip TARGA image comment (usually 0 bytes) */
539 fin += targa_header.id_length;
541 /* read/skip the colormap if present (note: according to the TARGA spec it */
542 /* can be present even on 1color or greyscale images, just not used by */
543 /* the image data) */
544 if (targa_header.colormap_type)
546 if (targa_header.colormap_length > 256)
548 printf("LoadTGA: only up to 256 colormap_length supported\n");
549 PrintTargaHeader(&targa_header);
552 if (targa_header.colormap_index)
554 printf("LoadTGA: colormap_index not supported\n");
555 PrintTargaHeader(&targa_header);
558 if (targa_header.colormap_size == 24)
560 for (x = 0;x < targa_header.colormap_length;x++)
566 palettei[x] = bgra.i;
569 else if (targa_header.colormap_size == 32)
571 memcpy(palettei, fin, targa_header.colormap_length*4);
572 fin += targa_header.colormap_length * 4;
576 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
577 PrintTargaHeader(&targa_header);
582 /* check our pixel_size restrictions according to image_type */
583 switch (targa_header.image_type & ~8)
586 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
588 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
589 PrintTargaHeader(&targa_header);
594 /* set up a palette to make the loader easier */
595 for (x = 0;x < 256;x++)
597 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
599 palettei[x] = bgra.i;
601 /* fall through to colormap case */
603 if (targa_header.pixel_size != 8)
605 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
606 PrintTargaHeader(&targa_header);
611 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
612 PrintTargaHeader(&targa_header);
616 if (targa_header.attributes & 0x10)
618 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
622 /* number of attribute bits per pixel, we only support 0 or 8 */
623 alphabits = targa_header.attributes & 0x0F;
624 if (alphabits != 8 && alphabits != 0)
626 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
630 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
633 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
637 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
638 if ((targa_header.attributes & 0x20) == 0)
640 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
641 row_inci = -image_width*2;
645 pixbufi = (unsigned int*)image_buffer;
652 if ((targa_header.image_type & ~8) == 2)
653 pix_inc = (targa_header.pixel_size + 7) / 8;
654 switch (targa_header.image_type)
656 case 1: /* colormapped, uncompressed */
657 case 3: /* greyscale, uncompressed */
658 if (fin + image_width * image_height * pix_inc > enddata)
660 for (y = 0;y < image_height;y++, pixbufi += row_inci)
661 for (x = 0;x < image_width;x++)
662 *pixbufi++ = palettei[*fin++];
665 /* BGR or BGRA, uncompressed */
666 if (fin + image_width * image_height * pix_inc > enddata)
668 if (targa_header.pixel_size == 32 && alphabits)
670 for (y = 0;y < image_height;y++)
671 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
675 for (y = 0;y < image_height;y++, pixbufi += row_inci)
677 for (x = 0;x < image_width;x++, fin += pix_inc)
688 case 9: /* colormapped, RLE */
689 case 11: /* greyscale, RLE */
690 for (y = 0;y < image_height;y++, pixbufi += row_inci)
692 for (x = 0;x < image_width;)
695 break; /* error - truncated file */
699 /* RLE - all pixels the same color */
701 if (fin + pix_inc > enddata)
702 break; /* error - truncated file */
703 if (x + runlen > image_width)
704 break; /* error - line exceeds width */
705 bgra.i = palettei[*fin++];
711 /* uncompressed - all pixels different color */
713 if (fin + pix_inc * runlen > enddata)
714 break; /* error - truncated file */
715 if (x + runlen > image_width)
716 break; /* error - line exceeds width */
718 *pixbufi++ = palettei[*fin++];
722 if (x != image_width)
724 /* pixbufi is useless now */
725 printf("LoadTGA: corrupt file\n");
731 /* BGR or BGRA, RLE */
732 if (targa_header.pixel_size == 32 && alphabits)
734 for (y = 0;y < image_height;y++, pixbufi += row_inci)
736 for (x = 0;x < image_width;)
739 break; /* error - truncated file */
743 /* RLE - all pixels the same color */
745 if (fin + pix_inc > enddata)
746 break; /* error - truncated file */
747 if (x + runlen > image_width)
748 break; /* error - line exceeds width */
759 /* uncompressed - all pixels different color */
761 if (fin + pix_inc * runlen > enddata)
762 break; /* error - truncated file */
763 if (x + runlen > image_width)
764 break; /* error - line exceeds width */
777 if (x != image_width)
779 /* pixbufi is useless now */
780 printf("LoadTGA: corrupt file\n");
787 for (y = 0;y < image_height;y++, pixbufi += row_inci)
789 for (x = 0;x < image_width;)
792 break; /* error - truncated file */
796 /* RLE - all pixels the same color */
798 if (fin + pix_inc > enddata)
799 break; /* error - truncated file */
800 if (x + runlen > image_width)
801 break; /* error - line exceeds width */
812 /* uncompressed - all pixels different color */
814 if (fin + pix_inc * runlen > enddata)
815 break; /* error - truncated file */
816 if (x + runlen > image_width)
817 break; /* error - line exceeds width */
830 if (x != image_width)
832 /* pixbufi is useless now */
833 printf("LoadTGA: corrupt file\n");
840 /* unknown image_type */
847 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
850 unsigned char *buffer, *out;
851 const unsigned char *in, *end;
854 buffer = (unsigned char *)malloc(width*height*4 + 18);
856 memset (buffer, 0, 18);
857 buffer[2] = 2; /* uncompressed type */
858 buffer[12] = (width >> 0) & 0xFF;
859 buffer[13] = (width >> 8) & 0xFF;
860 buffer[14] = (height >> 0) & 0xFF;
861 buffer[15] = (height >> 8) & 0xFF;
863 for (y = 3;y < width*height*4;y += 4)
867 if (y < width*height*4)
869 /* save the alpha channel */
870 buffer[16] = 32; /* pixel size */
871 buffer[17] = 8; /* 8 bits of alpha */
873 /* flip upside down */
875 for (y = height - 1;y >= 0;y--)
877 memcpy(out, data + y * width * 4, width * 4);
883 /* save only the color channels */
884 buffer[16] = 24; /* pixel size */
885 buffer[17] = 0; /* 8 bits of alpha */
887 /* truncate bgra to bgr and flip upside down */
889 for (y = height - 1;y >= 0;y--)
891 in = data + y * width * 4;
892 end = in + width * 4;
893 for (;in < end;in += 4)
901 ret = FS_WriteFile (filename, buffer, out - buffer);
907 /* START stuff that originates from image.c in DarkPlaces */
909 int usage(const char *me)
911 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
912 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from B, Diff)\n", me);
913 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G, Diff)\n", me);
914 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R, Diff)\n", me);
915 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -4 [<scale>] (read from A, Diff)\n", me);
916 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -5 [<scale>] (read from (R+G+B)/3, Diff)\n", me);
917 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -6 [<scale>] (read from Y, Diff)\n", me);
918 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -7 [<scale>] (read from B, FFT)\n", me);
919 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -8 [<scale>] (read from G, FFT)\n", me);
920 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -9 [<scale>] (read from R, FFT)\n", me);
921 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -10 [<scale>] (read from A, FFT)\n", me);
922 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -11 [<scale>] (read from (R+G+B)/3, FFT)\n", me);
923 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -12 [<scale>] (read from Y, FFT)\n", me);
927 int main(int argc, char **argv)
929 const char *infile, *outfile, *reffile;
930 double scale, offset;
932 unsigned char *nmapdata, *nmap, *refmap;
945 scale = atof(argv[3]);
950 offset = atof(argv[4]);
952 offset = (scale<0) ? 1 : 0;
959 nmapdata = FS_LoadFile(infile, &nmaplen);
962 printf("FS_LoadFile failed\n");
965 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
969 printf("LoadTGA_BGRA failed\n");
977 nmapdata = FS_LoadFile(infile, &nmaplen);
980 printf("FS_LoadFile failed\n");
983 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
987 printf("LoadTGA_BGRA failed\n");
990 if(image_width != w || image_height != h)
992 printf("reference map must have same size as input normalmap\n");
998 hmap_to_nmap(nmap, image_width, image_height, -scale-7, offset);
1000 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset);
1002 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset);
1003 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1005 printf("Image_WriteTGABGRA failed\n");