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)] = _Complex_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;
118 for(y = 0; y < h; ++y)
119 for(x = 0; x < w; ++x)
122 imgspace1[(w*y+x)] /= (w*h);
124 imgspace1[(w*y+x)][0] /= (w*h);
125 imgspace1[(w*y+x)][1] /= (w*h);
133 double sa, sfa, sffa, sfva, sva;
135 sa = sfa = sffa = sfva = sva = 0;
138 for(y = 0; y < h; ++y)
139 for(x = 0; x < w; ++x)
141 a = (int)refmap[(w*y+x)*4+3];
142 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);
143 v = (v - 128.0) / 127.0;
145 f = creal(imgspace1[(w*y+x)]);
147 f = imgspace1[(w*y+x)][0];
163 /* linear regression ftw */
164 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
165 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
167 else /* all values of v are equal, so we cannot get scale; we can still get offset */
169 o = ((sva - sfa) / sa);
173 printf("Ref-computed scale: %f\nRef-computed offset: %f\n", s, o);
176 * now apply user-given offset and scale to these values
177 * (x * s + o) * scale + offset
178 * x * s * scale + o * scale + offset
186 vmin = vmax = creal(imgspace1[0]);
188 vmin = vmax = imgspace1[0][0];
190 for(y = 0; y < h; ++y)
191 for(x = 0; x < w; ++x)
194 v = creal(imgspace1[(w*y+x)]);
196 v = imgspace1[(w*y+x)][0];
208 scale = 2 / (vmax - vmin);
209 offset = -(vmax + vmin) / (vmax - vmin);
211 printf("Autocomputed scale: %f\nAutocomputed offset: %f\n", scale, offset);
214 for(y = 0; y < h; ++y)
215 for(x = 0; x < w; ++x)
218 v = creal(imgspace1[(w*y+x)]);
220 v = imgspace1[(w*y+x)][0];
222 v = v * scale + offset;
227 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
230 fftw_destroy_plan(i12f1);
231 fftw_destroy_plan(i22f2);
232 fftw_destroy_plan(f12i1);
234 fftw_free(freqspace2);
235 fftw_free(freqspace1);
236 fftw_free(imgspace2);
237 fftw_free(imgspace1);
240 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
249 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
250 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
251 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
252 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
253 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
254 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
255 fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
257 for(y = 0; y < h; ++y)
258 for(x = 0; x < w; ++x)
266 v = map[(w*y+x)*4+src_chan];
269 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
273 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);
277 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
279 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
280 imgspace1[(w*y+x)][1] = 0;
282 map[(w*y+x)*4+3] = floor(v + 0.5);
285 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
289 for(y = 0; y < h; ++y)
290 for(x = 0; x < w; ++x)
299 /* a lowpass to prevent the worst */
300 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
301 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
303 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
304 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
306 /* a lowpass to prevent the worst */
307 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
308 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
309 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
310 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
312 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
313 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
314 save = freqspace1[(w*y+x)][0];
315 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
316 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
325 for(y = 0; y < h; ++y)
326 for(x = 0; x < w; ++x)
329 nx = creal(imgspace1[(w*y+x)]);
330 ny = creal(imgspace2[(w*y+x)]);
332 nx = imgspace1[(w*y+x)][0];
333 ny = imgspace2[(w*y+x)][0];
338 v = -sqrt(nx*nx + ny*ny + nz*nz);
342 ny = -ny; /* DP inverted normals */
343 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
344 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
345 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
348 fftw_destroy_plan(i12f1);
349 fftw_destroy_plan(f12i1);
350 fftw_destroy_plan(f22i2);
352 fftw_free(freqspace2);
353 fftw_free(freqspace1);
354 fftw_free(imgspace2);
355 fftw_free(imgspace1);
358 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale)
364 double *img_reduced = malloc(w*h * sizeof(double));
365 static const double filter[3][3] = { /* filter to derive one component */
370 static const double filter_mult = 0.125;
372 for(y = 0; y < h; ++y)
373 for(x = 0; x < w; ++x)
381 v = map[(w*y+x)*4+src_chan];
384 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
388 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);
391 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
392 map[(w*y+x)*4+3] = floor(v + 0.5);
395 for(y = 0; y < h; ++y)
396 for(x = 0; x < w; ++x)
398 nz = -1 / (scale * filter_mult);
401 for(i = -(int)(sizeof(filter) / sizeof(*filter)) / 2; i <= (int)(sizeof(filter) / sizeof(*filter)) / 2; ++i)
402 for(j = -(int)(sizeof(*filter) / sizeof(**filter)) / 2; j <= (int)(sizeof(*filter) / sizeof(**filter)) / 2; ++j)
404 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];
405 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];
408 v = -sqrt(nx*nx + ny*ny + nz*nz);
412 ny = -ny; /* DP inverted normals */
413 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
414 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
415 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
421 unsigned char *FS_LoadFile(const char *fn, int *len)
423 unsigned char *buf = NULL;
425 FILE *f = fopen(fn, "rb");
431 buf = realloc(buf, *len + 65536);
439 n = fread(buf + *len, 1, 65536, f);
454 int FS_WriteFile(const char *fn, unsigned char *data, int len)
456 FILE *f = fopen(fn, "wb");
459 if(fwrite(data, len, 1, f) != 1)
469 /* START stuff that originates from image.c in DarkPlaces */
470 int image_width, image_height;
472 typedef struct _TargaHeader
474 unsigned char id_length, colormap_type, image_type;
475 unsigned short colormap_index, colormap_length;
476 unsigned char colormap_size;
477 unsigned short x_origin, y_origin, width, height;
478 unsigned char pixel_size, attributes;
482 void PrintTargaHeader(TargaHeader *t)
484 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);
487 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
489 int x, y, pix_inc, row_inci, runlen, alphabits;
490 unsigned char *image_buffer;
491 unsigned int *pixbufi;
492 const unsigned char *fin, *enddata;
493 TargaHeader targa_header;
494 unsigned int palettei[256];
505 enddata = f + filesize;
507 targa_header.id_length = f[0];
508 targa_header.colormap_type = f[1];
509 targa_header.image_type = f[2];
511 targa_header.colormap_index = f[3] + f[4] * 256;
512 targa_header.colormap_length = f[5] + f[6] * 256;
513 targa_header.colormap_size = f[7];
514 targa_header.x_origin = f[8] + f[9] * 256;
515 targa_header.y_origin = f[10] + f[11] * 256;
516 targa_header.width = image_width = f[12] + f[13] * 256;
517 targa_header.height = image_height = f[14] + f[15] * 256;
518 targa_header.pixel_size = f[16];
519 targa_header.attributes = f[17];
521 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
523 printf("LoadTGA: invalid size\n");
524 PrintTargaHeader(&targa_header);
528 /* advance to end of header */
531 /* skip TARGA image comment (usually 0 bytes) */
532 fin += targa_header.id_length;
534 /* read/skip the colormap if present (note: according to the TARGA spec it */
535 /* can be present even on 1color or greyscale images, just not used by */
536 /* the image data) */
537 if (targa_header.colormap_type)
539 if (targa_header.colormap_length > 256)
541 printf("LoadTGA: only up to 256 colormap_length supported\n");
542 PrintTargaHeader(&targa_header);
545 if (targa_header.colormap_index)
547 printf("LoadTGA: colormap_index not supported\n");
548 PrintTargaHeader(&targa_header);
551 if (targa_header.colormap_size == 24)
553 for (x = 0;x < targa_header.colormap_length;x++)
559 palettei[x] = bgra.i;
562 else if (targa_header.colormap_size == 32)
564 memcpy(palettei, fin, targa_header.colormap_length*4);
565 fin += targa_header.colormap_length * 4;
569 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
570 PrintTargaHeader(&targa_header);
575 /* check our pixel_size restrictions according to image_type */
576 switch (targa_header.image_type & ~8)
579 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
581 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
582 PrintTargaHeader(&targa_header);
587 /* set up a palette to make the loader easier */
588 for (x = 0;x < 256;x++)
590 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
592 palettei[x] = bgra.i;
594 /* fall through to colormap case */
596 if (targa_header.pixel_size != 8)
598 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
599 PrintTargaHeader(&targa_header);
604 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
605 PrintTargaHeader(&targa_header);
609 if (targa_header.attributes & 0x10)
611 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
615 /* number of attribute bits per pixel, we only support 0 or 8 */
616 alphabits = targa_header.attributes & 0x0F;
617 if (alphabits != 8 && alphabits != 0)
619 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
623 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
626 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
630 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
631 if ((targa_header.attributes & 0x20) == 0)
633 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
634 row_inci = -image_width*2;
638 pixbufi = (unsigned int*)image_buffer;
645 if ((targa_header.image_type & ~8) == 2)
646 pix_inc = (targa_header.pixel_size + 7) / 8;
647 switch (targa_header.image_type)
649 case 1: /* colormapped, uncompressed */
650 case 3: /* greyscale, uncompressed */
651 if (fin + image_width * image_height * pix_inc > enddata)
653 for (y = 0;y < image_height;y++, pixbufi += row_inci)
654 for (x = 0;x < image_width;x++)
655 *pixbufi++ = palettei[*fin++];
658 /* BGR or BGRA, uncompressed */
659 if (fin + image_width * image_height * pix_inc > enddata)
661 if (targa_header.pixel_size == 32 && alphabits)
663 for (y = 0;y < image_height;y++)
664 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
668 for (y = 0;y < image_height;y++, pixbufi += row_inci)
670 for (x = 0;x < image_width;x++, fin += pix_inc)
681 case 9: /* colormapped, RLE */
682 case 11: /* greyscale, RLE */
683 for (y = 0;y < image_height;y++, pixbufi += row_inci)
685 for (x = 0;x < image_width;)
688 break; /* error - truncated file */
692 /* RLE - all pixels the same color */
694 if (fin + pix_inc > enddata)
695 break; /* error - truncated file */
696 if (x + runlen > image_width)
697 break; /* error - line exceeds width */
698 bgra.i = palettei[*fin++];
704 /* uncompressed - all pixels different color */
706 if (fin + pix_inc * runlen > enddata)
707 break; /* error - truncated file */
708 if (x + runlen > image_width)
709 break; /* error - line exceeds width */
711 *pixbufi++ = palettei[*fin++];
715 if (x != image_width)
717 /* pixbufi is useless now */
718 printf("LoadTGA: corrupt file\n");
724 /* BGR or BGRA, RLE */
725 if (targa_header.pixel_size == 32 && alphabits)
727 for (y = 0;y < image_height;y++, pixbufi += row_inci)
729 for (x = 0;x < image_width;)
732 break; /* error - truncated file */
736 /* RLE - all pixels the same color */
738 if (fin + pix_inc > enddata)
739 break; /* error - truncated file */
740 if (x + runlen > image_width)
741 break; /* error - line exceeds width */
752 /* uncompressed - all pixels different color */
754 if (fin + pix_inc * runlen > enddata)
755 break; /* error - truncated file */
756 if (x + runlen > image_width)
757 break; /* error - line exceeds width */
770 if (x != image_width)
772 /* pixbufi is useless now */
773 printf("LoadTGA: corrupt file\n");
780 for (y = 0;y < image_height;y++, pixbufi += row_inci)
782 for (x = 0;x < image_width;)
785 break; /* error - truncated file */
789 /* RLE - all pixels the same color */
791 if (fin + pix_inc > enddata)
792 break; /* error - truncated file */
793 if (x + runlen > image_width)
794 break; /* error - line exceeds width */
805 /* uncompressed - all pixels different color */
807 if (fin + pix_inc * runlen > enddata)
808 break; /* error - truncated file */
809 if (x + runlen > image_width)
810 break; /* error - line exceeds width */
823 if (x != image_width)
825 /* pixbufi is useless now */
826 printf("LoadTGA: corrupt file\n");
833 /* unknown image_type */
840 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
843 unsigned char *buffer, *out;
844 const unsigned char *in, *end;
847 buffer = (unsigned char *)malloc(width*height*4 + 18);
849 memset (buffer, 0, 18);
850 buffer[2] = 2; /* uncompressed type */
851 buffer[12] = (width >> 0) & 0xFF;
852 buffer[13] = (width >> 8) & 0xFF;
853 buffer[14] = (height >> 0) & 0xFF;
854 buffer[15] = (height >> 8) & 0xFF;
856 for (y = 3;y < width*height*4;y += 4)
860 if (y < width*height*4)
862 /* save the alpha channel */
863 buffer[16] = 32; /* pixel size */
864 buffer[17] = 8; /* 8 bits of alpha */
866 /* flip upside down */
868 for (y = height - 1;y >= 0;y--)
870 memcpy(out, data + y * width * 4, width * 4);
876 /* save only the color channels */
877 buffer[16] = 24; /* pixel size */
878 buffer[17] = 0; /* 8 bits of alpha */
880 /* truncate bgra to bgr and flip upside down */
882 for (y = height - 1;y >= 0;y--)
884 in = data + y * width * 4;
885 end = in + width * 4;
886 for (;in < end;in += 4)
894 ret = FS_WriteFile (filename, buffer, out - buffer);
900 /* START stuff that originates from image.c in DarkPlaces */
902 int usage(const char *me)
904 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
905 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from B, Diff)\n", me);
906 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G, Diff)\n", me);
907 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R, Diff)\n", me);
908 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -4 [<scale>] (read from A, Diff)\n", me);
909 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -5 [<scale>] (read from (R+G+B)/3, Diff)\n", me);
910 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -6 [<scale>] (read from Y, Diff)\n", me);
911 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -7 [<scale>] (read from B, FFT)\n", me);
912 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -8 [<scale>] (read from G, FFT)\n", me);
913 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -9 [<scale>] (read from R, FFT)\n", me);
914 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -10 [<scale>] (read from A, FFT)\n", me);
915 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -11 [<scale>] (read from (R+G+B)/3, FFT)\n", me);
916 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -12 [<scale>] (read from Y, FFT)\n", me);
920 int main(int argc, char **argv)
922 const char *infile, *outfile, *reffile;
923 double scale, offset;
925 unsigned char *nmapdata, *nmap, *refmap;
938 scale = atof(argv[3]);
943 offset = atof(argv[4]);
945 offset = (scale<0) ? 1 : 0;
952 nmapdata = FS_LoadFile(infile, &nmaplen);
955 printf("FS_LoadFile failed\n");
958 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
962 printf("LoadTGA_BGRA failed\n");
970 nmapdata = FS_LoadFile(reffile, &nmaplen);
973 printf("FS_LoadFile failed\n");
976 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
980 printf("LoadTGA_BGRA failed\n");
983 if(image_width != w || image_height != h)
985 printf("reference map must have same size as input normalmap\n");
993 hmap_to_nmap(nmap, image_width, image_height, -scale-7, offset);
995 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset);
997 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset);
998 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1000 printf("Image_WriteTGABGRA failed\n");