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;
117 /* renormalize, find min/max */
119 for(y = 0; y < h; ++y)
120 for(x = 0; x < w; ++x)
123 v = creal(imgspace1[(w*y+x)] /= (w*h));
125 v = (imgspace1[(w*y+x)][0] /= (w*h));
126 imgspace1[(w*y+x)][1] /= (w*h);
128 if(v < vmin || (x == 0 && y == 0))
130 if(v > vmax || (x == 0 && y == 0))
138 double sa, sfa, sffa, sfva, sva;
140 sa = sfa = sffa = sfva = sva = 0;
143 for(y = 0; y < h; ++y)
144 for(x = 0; x < w; ++x)
146 a = (int)refmap[(w*y+x)*4+3];
147 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);
148 v = (v - 128.0) / 127.0;
150 f = creal(imgspace1[(w*y+x)]);
152 f = imgspace1[(w*y+x)][0];
168 /* linear regression ftw */
169 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
170 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
172 else /* all values of v are equal, so we cannot get scale; we can still get offset */
174 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
192 scale = 2 / (vmax - vmin);
193 offset = -(vmax + vmin) / (vmax - vmin);
196 printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n",
197 vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset);
199 for(y = 0; y < h; ++y)
200 for(x = 0; x < w; ++x)
203 v = creal(imgspace1[(w*y+x)]);
205 v = imgspace1[(w*y+x)][0];
207 v = v * scale + offset;
212 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
215 fftw_destroy_plan(i12f1);
216 fftw_destroy_plan(i22f2);
217 fftw_destroy_plan(f12i1);
219 fftw_free(freqspace2);
220 fftw_free(freqspace1);
221 fftw_free(imgspace2);
222 fftw_free(imgspace1);
225 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
234 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
235 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
236 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
237 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
238 fftw_plan i12f1 = fftw_plan_dft_2d(w, h, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
239 fftw_plan f12i1 = fftw_plan_dft_2d(w, h, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
240 fftw_plan f22i2 = fftw_plan_dft_2d(w, h, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
242 for(y = 0; y < h; ++y)
243 for(x = 0; x < w; ++x)
251 v = map[(w*y+x)*4+src_chan];
254 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
258 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);
262 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
264 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
265 imgspace1[(w*y+x)][1] = 0;
267 map[(w*y+x)*4+3] = floor(v + 0.5);
270 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
274 for(y = 0; y < h; ++y)
275 for(x = 0; x < w; ++x)
284 /* a lowpass to prevent the worst */
285 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
286 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
288 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
289 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
291 /* a lowpass to prevent the worst */
292 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
293 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
294 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
295 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
297 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
298 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
299 save = freqspace1[(w*y+x)][0];
300 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
301 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
310 for(y = 0; y < h; ++y)
311 for(x = 0; x < w; ++x)
314 nx = creal(imgspace1[(w*y+x)]);
315 ny = creal(imgspace2[(w*y+x)]);
317 nx = imgspace1[(w*y+x)][0];
318 ny = imgspace2[(w*y+x)][0];
323 v = -sqrt(nx*nx + ny*ny + nz*nz);
327 ny = -ny; /* DP inverted normals */
328 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
329 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
330 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
333 fftw_destroy_plan(i12f1);
334 fftw_destroy_plan(f12i1);
335 fftw_destroy_plan(f22i2);
337 fftw_free(freqspace2);
338 fftw_free(freqspace1);
339 fftw_free(imgspace2);
340 fftw_free(imgspace1);
343 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale)
349 double *img_reduced = malloc(w*h * sizeof(double));
350 static const double filter[3][3] = { /* filter to derive one component */
355 static const double filter_mult = 0.125;
357 for(y = 0; y < h; ++y)
358 for(x = 0; x < w; ++x)
366 v = map[(w*y+x)*4+src_chan];
369 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
373 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);
376 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
377 map[(w*y+x)*4+3] = floor(v + 0.5);
380 for(y = 0; y < h; ++y)
381 for(x = 0; x < w; ++x)
383 nz = -1 / (scale * filter_mult);
386 for(i = -(int)(sizeof(filter) / sizeof(*filter)) / 2; i <= (int)(sizeof(filter) / sizeof(*filter)) / 2; ++i)
387 for(j = -(int)(sizeof(*filter) / sizeof(**filter)) / 2; j <= (int)(sizeof(*filter) / sizeof(**filter)) / 2; ++j)
389 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];
390 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];
393 v = -sqrt(nx*nx + ny*ny + nz*nz);
397 ny = -ny; /* DP inverted normals */
398 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
399 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
400 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
406 unsigned char *FS_LoadFile(const char *fn, int *len)
408 unsigned char *buf = NULL;
410 FILE *f = fopen(fn, "rb");
416 buf = realloc(buf, *len + 65536);
424 n = fread(buf + *len, 1, 65536, f);
439 int FS_WriteFile(const char *fn, unsigned char *data, int len)
441 FILE *f = fopen(fn, "wb");
444 if(fwrite(data, len, 1, f) != 1)
454 /* START stuff that originates from image.c in DarkPlaces */
455 int image_width, image_height;
457 typedef struct _TargaHeader
459 unsigned char id_length, colormap_type, image_type;
460 unsigned short colormap_index, colormap_length;
461 unsigned char colormap_size;
462 unsigned short x_origin, y_origin, width, height;
463 unsigned char pixel_size, attributes;
467 void PrintTargaHeader(TargaHeader *t)
469 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);
472 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
474 int x, y, pix_inc, row_inci, runlen, alphabits;
475 unsigned char *image_buffer;
476 unsigned int *pixbufi;
477 const unsigned char *fin, *enddata;
478 TargaHeader targa_header;
479 unsigned int palettei[256];
490 enddata = f + filesize;
492 targa_header.id_length = f[0];
493 targa_header.colormap_type = f[1];
494 targa_header.image_type = f[2];
496 targa_header.colormap_index = f[3] + f[4] * 256;
497 targa_header.colormap_length = f[5] + f[6] * 256;
498 targa_header.colormap_size = f[7];
499 targa_header.x_origin = f[8] + f[9] * 256;
500 targa_header.y_origin = f[10] + f[11] * 256;
501 targa_header.width = image_width = f[12] + f[13] * 256;
502 targa_header.height = image_height = f[14] + f[15] * 256;
503 targa_header.pixel_size = f[16];
504 targa_header.attributes = f[17];
506 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
508 printf("LoadTGA: invalid size\n");
509 PrintTargaHeader(&targa_header);
513 /* advance to end of header */
516 /* skip TARGA image comment (usually 0 bytes) */
517 fin += targa_header.id_length;
519 /* read/skip the colormap if present (note: according to the TARGA spec it */
520 /* can be present even on 1color or greyscale images, just not used by */
521 /* the image data) */
522 if (targa_header.colormap_type)
524 if (targa_header.colormap_length > 256)
526 printf("LoadTGA: only up to 256 colormap_length supported\n");
527 PrintTargaHeader(&targa_header);
530 if (targa_header.colormap_index)
532 printf("LoadTGA: colormap_index not supported\n");
533 PrintTargaHeader(&targa_header);
536 if (targa_header.colormap_size == 24)
538 for (x = 0;x < targa_header.colormap_length;x++)
544 palettei[x] = bgra.i;
547 else if (targa_header.colormap_size == 32)
549 memcpy(palettei, fin, targa_header.colormap_length*4);
550 fin += targa_header.colormap_length * 4;
554 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
555 PrintTargaHeader(&targa_header);
560 /* check our pixel_size restrictions according to image_type */
561 switch (targa_header.image_type & ~8)
564 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
566 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
567 PrintTargaHeader(&targa_header);
572 /* set up a palette to make the loader easier */
573 for (x = 0;x < 256;x++)
575 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
577 palettei[x] = bgra.i;
579 /* fall through to colormap case */
581 if (targa_header.pixel_size != 8)
583 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
584 PrintTargaHeader(&targa_header);
589 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
590 PrintTargaHeader(&targa_header);
594 if (targa_header.attributes & 0x10)
596 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
600 /* number of attribute bits per pixel, we only support 0 or 8 */
601 alphabits = targa_header.attributes & 0x0F;
602 if (alphabits != 8 && alphabits != 0)
604 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
608 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
611 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
615 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
616 if ((targa_header.attributes & 0x20) == 0)
618 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
619 row_inci = -image_width*2;
623 pixbufi = (unsigned int*)image_buffer;
630 if ((targa_header.image_type & ~8) == 2)
631 pix_inc = (targa_header.pixel_size + 7) / 8;
632 switch (targa_header.image_type)
634 case 1: /* colormapped, uncompressed */
635 case 3: /* greyscale, uncompressed */
636 if (fin + image_width * image_height * pix_inc > enddata)
638 for (y = 0;y < image_height;y++, pixbufi += row_inci)
639 for (x = 0;x < image_width;x++)
640 *pixbufi++ = palettei[*fin++];
643 /* BGR or BGRA, uncompressed */
644 if (fin + image_width * image_height * pix_inc > enddata)
646 if (targa_header.pixel_size == 32 && alphabits)
648 for (y = 0;y < image_height;y++)
649 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
653 for (y = 0;y < image_height;y++, pixbufi += row_inci)
655 for (x = 0;x < image_width;x++, fin += pix_inc)
666 case 9: /* colormapped, RLE */
667 case 11: /* greyscale, RLE */
668 for (y = 0;y < image_height;y++, pixbufi += row_inci)
670 for (x = 0;x < image_width;)
673 break; /* error - truncated file */
677 /* RLE - all pixels the same color */
679 if (fin + pix_inc > enddata)
680 break; /* error - truncated file */
681 if (x + runlen > image_width)
682 break; /* error - line exceeds width */
683 bgra.i = palettei[*fin++];
689 /* uncompressed - all pixels different color */
691 if (fin + pix_inc * runlen > enddata)
692 break; /* error - truncated file */
693 if (x + runlen > image_width)
694 break; /* error - line exceeds width */
696 *pixbufi++ = palettei[*fin++];
700 if (x != image_width)
702 /* pixbufi is useless now */
703 printf("LoadTGA: corrupt file\n");
709 /* BGR or BGRA, RLE */
710 if (targa_header.pixel_size == 32 && alphabits)
712 for (y = 0;y < image_height;y++, pixbufi += row_inci)
714 for (x = 0;x < image_width;)
717 break; /* error - truncated file */
721 /* RLE - all pixels the same color */
723 if (fin + pix_inc > enddata)
724 break; /* error - truncated file */
725 if (x + runlen > image_width)
726 break; /* error - line exceeds width */
737 /* uncompressed - all pixels different color */
739 if (fin + pix_inc * runlen > enddata)
740 break; /* error - truncated file */
741 if (x + runlen > image_width)
742 break; /* error - line exceeds width */
755 if (x != image_width)
757 /* pixbufi is useless now */
758 printf("LoadTGA: corrupt file\n");
765 for (y = 0;y < image_height;y++, pixbufi += row_inci)
767 for (x = 0;x < image_width;)
770 break; /* error - truncated file */
774 /* RLE - all pixels the same color */
776 if (fin + pix_inc > enddata)
777 break; /* error - truncated file */
778 if (x + runlen > image_width)
779 break; /* error - line exceeds width */
790 /* uncompressed - all pixels different color */
792 if (fin + pix_inc * runlen > enddata)
793 break; /* error - truncated file */
794 if (x + runlen > image_width)
795 break; /* error - line exceeds width */
808 if (x != image_width)
810 /* pixbufi is useless now */
811 printf("LoadTGA: corrupt file\n");
818 /* unknown image_type */
825 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
828 unsigned char *buffer, *out;
829 const unsigned char *in, *end;
832 buffer = (unsigned char *)malloc(width*height*4 + 18);
834 memset (buffer, 0, 18);
835 buffer[2] = 2; /* uncompressed type */
836 buffer[12] = (width >> 0) & 0xFF;
837 buffer[13] = (width >> 8) & 0xFF;
838 buffer[14] = (height >> 0) & 0xFF;
839 buffer[15] = (height >> 8) & 0xFF;
841 for (y = 3;y < width*height*4;y += 4)
845 if (y < width*height*4)
847 /* save the alpha channel */
848 buffer[16] = 32; /* pixel size */
849 buffer[17] = 8; /* 8 bits of alpha */
851 /* flip upside down */
853 for (y = height - 1;y >= 0;y--)
855 memcpy(out, data + y * width * 4, width * 4);
861 /* save only the color channels */
862 buffer[16] = 24; /* pixel size */
863 buffer[17] = 0; /* 8 bits of alpha */
865 /* truncate bgra to bgr and flip upside down */
867 for (y = height - 1;y >= 0;y--)
869 in = data + y * width * 4;
870 end = in + width * 4;
871 for (;in < end;in += 4)
879 ret = FS_WriteFile (filename, buffer, out - buffer);
885 /* START stuff that originates from image.c in DarkPlaces */
887 int usage(const char *me)
889 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
890 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -1 [<scale>] (read from B, Diff)\n", me);
891 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -2 [<scale>] (read from G, Diff)\n", me);
892 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -3 [<scale>] (read from R, Diff)\n", me);
893 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -4 [<scale>] (read from A, Diff)\n", me);
894 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -5 [<scale>] (read from (R+G+B)/3, Diff)\n", me);
895 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -6 [<scale>] (read from Y, Diff)\n", me);
896 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -7 [<scale>] (read from B, FFT)\n", me);
897 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -8 [<scale>] (read from G, FFT)\n", me);
898 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -9 [<scale>] (read from R, FFT)\n", me);
899 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -10 [<scale>] (read from A, FFT)\n", me);
900 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -11 [<scale>] (read from (R+G+B)/3, FFT)\n", me);
901 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> -12 [<scale>] (read from Y, FFT)\n", me);
905 int main(int argc, char **argv)
907 const char *infile, *outfile, *reffile;
908 double scale, offset;
910 unsigned char *nmapdata, *nmap, *refmap;
923 scale = atof(argv[3]);
928 offset = atof(argv[4]);
930 offset = (scale<0) ? 1 : 0;
937 nmapdata = FS_LoadFile(infile, &nmaplen);
940 printf("FS_LoadFile failed\n");
943 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
947 printf("LoadTGA_BGRA failed\n");
955 nmapdata = FS_LoadFile(reffile, &nmaplen);
958 printf("FS_LoadFile failed\n");
961 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
965 printf("LoadTGA_BGRA failed\n");
968 if(image_width != w || image_height != h)
970 printf("reference map must have same size as input normalmap\n");
978 hmap_to_nmap(nmap, image_width, image_height, -scale-7, offset);
980 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset);
982 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset);
983 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
985 printf("Image_WriteTGABGRA failed\n");