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, const double *filter, int filterw, int filterh, int renormalize, double highpass)
50 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
51 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
52 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
53 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
54 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
55 fftw_plan i22f2 = fftw_plan_dft_2d(h, w, imgspace2, freqspace2, FFTW_FORWARD, FFTW_ESTIMATE);
56 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
58 for(y = 0; y < h; ++y)
59 for(x = 0; x < w; ++x)
62 * unnormalized normals:
66 * BUT: darkplaces uses inverted normals, n_y actually is dh/dy by image pixel coordinates
68 nx = ((int)map[(w*y+x)*4+2] - 127.5) / 128;
69 ny = ((int)map[(w*y+x)*4+1] - 127.5) / 128;
70 nz = ((int)map[(w*y+x)*4+0] - 127.5) / 128;
72 /* reconstruct the derivatives from here */
74 imgspace1[(w*y+x)] = nx / nz * w; /* = dz/dx */
75 imgspace2[(w*y+x)] = -ny / nz * h; /* = dz/dy */
77 imgspace1[(w*y+x)][0] = nx / nz * w; /* = dz/dx */
78 imgspace1[(w*y+x)][1] = 0;
79 imgspace2[(w*y+x)][0] = -ny / nz * h; /* = dz/dy */
80 imgspace2[(w*y+x)][1] = 0;
85 double v = nx * nx + ny * ny + nz * nz;
92 map[(w*y+x)*4+2] = floor(nx * 127.5 + 128);
93 map[(w*y+x)*4+1] = floor(ny * 127.5 + 128);
94 map[(w*y+x)*4+0] = floor(nz * 127.5 + 128);
99 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
104 for(y = 0; y < h; ++y)
105 for(x = 0; x < w; ++x)
116 // we must invert whatever "filter" would do on (x, y)!
118 fftw_complex response_x = 0;
119 fftw_complex response_y = 0;
121 for(i = -filterh / 2; i <= filterh / 2; ++i)
122 for(j = -filterw / 2; j <= filterw / 2; ++j)
124 response_x += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (j * fx + i * fy));
125 response_y += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (i * fx + j * fy));
129 // fourier(df/dx)_xy = fourier(f)_xy * response_x
130 // fourier(df/dy)_xy = fourier(f)_xy * response_y
131 // mult by conjugate of response_x, response_y:
132 // conj(response_x) * fourier(df/dx)_xy = fourier(f)_xy * |response_x^2|
133 // conj(response_y) * fourier(df/dy)_xy = fourier(f)_xy * |response_y^2|
135 // fourier(f)_xy = (conj(response_x) * fourier(df/dx)_xy + conj(response_y) * fourier(df/dy)_xy) / (|response_x|^2 + |response_y|^2)
137 sum = cabs(response_x) * cabs(response_x) + cabs(response_y) * cabs(response_y);
140 freqspace1[(w*y+x)] = (conj(response_x) * freqspace1[(w*y+x)] + conj(response_y) * freqspace2[(w*y+x)]) / sum;
142 freqspace1[(w*y+x)] = 0;
144 fftw_complex response_x = {0, 0};
145 fftw_complex response_y = {0, 0};
147 for(i = -filterh / 2; i <= filterh / 2; ++i)
148 for(j = -filterw / 2; j <= filterw / 2; ++j)
150 response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy));
151 response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy));
152 response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy));
153 response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy));
156 sum = response_x[0] * response_x[0] + response_x[1] * response_x[1]
157 + response_y[0] * response_y[0] + response_y[1] * response_y[1];
161 double s = freqspace1[(w*y+x)][0];
162 freqspace1[(w*y+x)][0] = (response_x[0] * s + response_x[1] * freqspace1[(w*y+x)][1] + response_y[0] * freqspace2[(w*y+x)][0] + response_y[1] * freqspace2[(w*y+x)][1]) / sum;
163 freqspace1[(w*y+x)][1] = (response_x[0] * freqspace1[(w*y+x)][1] - response_x[1] * s + response_y[0] * freqspace2[(w*y+x)][1] - response_y[1] * freqspace2[(w*y+x)][0]) / sum;
167 freqspace1[(w*y+x)][0] = 0;
168 freqspace1[(w*y+x)][1] = 0;
174 // continuous integration case
175 /* these must have the same sign as fx and fy (so ffx*fx + ffy*fy is nonzero), otherwise do not matter */
176 /* it basically decides how artifacts are distributed */
181 freqspace1[(w*y+x)] = _Complex_I * (ffx * freqspace1[(w*y+x)] + ffy * freqspace2[(w*y+x)]) / (ffx*fx + ffy*fy) / TWO_PI;
183 freqspace1[(w*y+x)] = 0;
187 save = freqspace1[(w*y+x)][0];
188 freqspace1[(w*y+x)][0] = -(ffx * freqspace1[(w*y+x)][1] + ffy * freqspace2[(w*y+x)][1]) / (ffx*fx + ffy*fy) / TWO_PI;
189 freqspace1[(w*y+x)][1] = (ffx * save + ffy * freqspace2[(w*y+x)][0]) / (ffx*fx + ffy*fy) / TWO_PI;
193 freqspace1[(w*y+x)][0] = 0;
194 freqspace1[(w*y+x)][1] = 0;
200 double f1 = (fabs(fx)*highpass);
201 double f2 = (fabs(fy)*highpass);
202 // if either of them is < 1, phase out (min at 0.5)
204 (f1 <= 0.5 ? 0 : (f1 >= 1 ? 1 : ((f1 - 0.5) * 2.0)))
206 (f2 <= 0.5 ? 0 : (f2 >= 1 ? 1 : ((f2 - 0.5) * 2.0)));
208 freqspace1[(w*y+x)] *= f;
210 freqspace1[(w*y+x)][0] *= f;
211 freqspace1[(w*y+x)][1] *= f;
218 /* renormalize, find min/max */
220 for(y = 0; y < h; ++y)
221 for(x = 0; x < w; ++x)
224 v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5));
226 v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5));
227 // imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
228 // this value is never used
230 if(v < vmin || (x == 0 && y == 0))
232 if(v > vmax || (x == 0 && y == 0))
240 double sa, sfa, sffa, sfva, sva;
242 sa = sfa = sffa = sfva = sva = 0;
245 for(y = 0; y < h; ++y)
246 for(x = 0; x < w; ++x)
248 a = (int)refmap[(w*y+x)*4+3];
249 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);
250 v = (v - 128.0) / 127.0;
252 f = creal(imgspace1[(w*y+x)]);
254 f = imgspace1[(w*y+x)][0];
270 /* linear regression ftw */
271 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
272 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
274 else /* all values of v are equal, so we cannot get scale; we can still get offset */
276 o = ((sva - sfa) / sa);
281 * now apply user-given offset and scale to these values
282 * (x * s + o) * scale + offset
283 * x * s * scale + o * scale + offset
294 scale = 2 / (vmax - vmin);
295 offset = -(vmax + vmin) / (vmax - vmin);
298 printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n",
299 vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset);
301 for(y = 0; y < h; ++y)
302 for(x = 0; x < w; ++x)
305 v = creal(imgspace1[(w*y+x)]);
307 v = imgspace1[(w*y+x)][0];
309 v = v * scale + offset;
314 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
317 fftw_destroy_plan(i12f1);
318 fftw_destroy_plan(i22f2);
319 fftw_destroy_plan(f12i1);
321 fftw_free(freqspace2);
322 fftw_free(freqspace1);
323 fftw_free(imgspace2);
324 fftw_free(imgspace1);
327 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
337 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
338 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
339 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
340 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
341 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
342 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
343 fftw_plan f22i2 = fftw_plan_dft_2d(h, w, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
345 for(y = 0; y < h; ++y)
346 for(x = 0; x < w; ++x)
354 v = map[(w*y+x)*4+src_chan];
357 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
361 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);
365 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
367 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
368 imgspace1[(w*y+x)][1] = 0;
371 v = 1; /* do not write alpha zero */
372 map[(w*y+x)*4+3] = floor(v + 0.5);
375 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
379 for(y = 0; y < h; ++y)
380 for(x = 0; x < w; ++x)
389 fx = sin(fx * TWO_PI / w);
390 fy = sin(fy * TWO_PI / h);
393 /* a lowpass to prevent the worst */
394 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
395 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
397 /* a lowpass to prevent the worst */
398 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
399 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
400 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
401 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
405 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
406 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
408 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
409 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
410 save = freqspace1[(w*y+x)][0];
411 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
412 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
421 for(y = 0; y < h; ++y)
422 for(x = 0; x < w; ++x)
425 nx = creal(imgspace1[(w*y+x)]);
426 ny = creal(imgspace2[(w*y+x)]);
428 nx = imgspace1[(w*y+x)][0];
429 ny = imgspace2[(w*y+x)][0];
434 v = -sqrt(nx*nx + ny*ny + nz*nz);
438 ny = -ny; /* DP inverted normals */
439 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
440 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
441 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
444 fftw_destroy_plan(i12f1);
445 fftw_destroy_plan(f12i1);
446 fftw_destroy_plan(f22i2);
448 fftw_free(freqspace2);
449 fftw_free(freqspace1);
450 fftw_free(imgspace2);
451 fftw_free(imgspace1);
454 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale, const double *filter, int filterw, int filterh)
460 double *img_reduced = malloc(w*h * sizeof(double));
462 for(y = 0; y < h; ++y)
463 for(x = 0; x < w; ++x)
471 v = map[(w*y+x)*4+src_chan];
474 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
478 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);
481 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
483 v = 1; /* do not write alpha zero */
484 map[(w*y+x)*4+3] = floor(v + 0.5);
487 for(y = 0; y < h; ++y)
488 for(x = 0; x < w; ++x)
493 for(i = -filterh / 2; i <= filterh / 2; ++i)
494 for(j = -filterw / 2; j <= filterw / 2; ++j)
496 nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
497 ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
500 v = -sqrt(nx*nx + ny*ny + nz*nz);
504 ny = -ny; /* DP inverted normals */
505 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
506 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
507 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
513 unsigned char *FS_LoadFile(const char *fn, int *len)
515 unsigned char *buf = NULL;
517 FILE *f = fopen(fn, "rb");
523 buf = realloc(buf, *len + 65536);
531 n = fread(buf + *len, 1, 65536, f);
546 int FS_WriteFile(const char *fn, unsigned char *data, int len)
548 FILE *f = fopen(fn, "wb");
551 if(fwrite(data, len, 1, f) != 1)
561 /* START stuff that originates from image.c in DarkPlaces */
562 int image_width, image_height;
564 typedef struct _TargaHeader
566 unsigned char id_length, colormap_type, image_type;
567 unsigned short colormap_index, colormap_length;
568 unsigned char colormap_size;
569 unsigned short x_origin, y_origin, width, height;
570 unsigned char pixel_size, attributes;
574 void PrintTargaHeader(TargaHeader *t)
576 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);
579 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
581 int x, y, pix_inc, row_inci, runlen, alphabits;
582 unsigned char *image_buffer;
583 unsigned int *pixbufi;
584 const unsigned char *fin, *enddata;
585 TargaHeader targa_header;
586 unsigned int palettei[256];
597 enddata = f + filesize;
599 targa_header.id_length = f[0];
600 targa_header.colormap_type = f[1];
601 targa_header.image_type = f[2];
603 targa_header.colormap_index = f[3] + f[4] * 256;
604 targa_header.colormap_length = f[5] + f[6] * 256;
605 targa_header.colormap_size = f[7];
606 targa_header.x_origin = f[8] + f[9] * 256;
607 targa_header.y_origin = f[10] + f[11] * 256;
608 targa_header.width = image_width = f[12] + f[13] * 256;
609 targa_header.height = image_height = f[14] + f[15] * 256;
610 targa_header.pixel_size = f[16];
611 targa_header.attributes = f[17];
613 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
615 printf("LoadTGA: invalid size\n");
616 PrintTargaHeader(&targa_header);
620 /* advance to end of header */
623 /* skip TARGA image comment (usually 0 bytes) */
624 fin += targa_header.id_length;
626 /* read/skip the colormap if present (note: according to the TARGA spec it */
627 /* can be present even on 1color or greyscale images, just not used by */
628 /* the image data) */
629 if (targa_header.colormap_type)
631 if (targa_header.colormap_length > 256)
633 printf("LoadTGA: only up to 256 colormap_length supported\n");
634 PrintTargaHeader(&targa_header);
637 if (targa_header.colormap_index)
639 printf("LoadTGA: colormap_index not supported\n");
640 PrintTargaHeader(&targa_header);
643 if (targa_header.colormap_size == 24)
645 for (x = 0;x < targa_header.colormap_length;x++)
651 palettei[x] = bgra.i;
654 else if (targa_header.colormap_size == 32)
656 memcpy(palettei, fin, targa_header.colormap_length*4);
657 fin += targa_header.colormap_length * 4;
661 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
662 PrintTargaHeader(&targa_header);
667 /* check our pixel_size restrictions according to image_type */
668 switch (targa_header.image_type & ~8)
671 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
673 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
674 PrintTargaHeader(&targa_header);
679 /* set up a palette to make the loader easier */
680 for (x = 0;x < 256;x++)
682 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
684 palettei[x] = bgra.i;
686 /* fall through to colormap case */
688 if (targa_header.pixel_size != 8)
690 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
691 PrintTargaHeader(&targa_header);
696 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
697 PrintTargaHeader(&targa_header);
701 if (targa_header.attributes & 0x10)
703 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
707 /* number of attribute bits per pixel, we only support 0 or 8 */
708 alphabits = targa_header.attributes & 0x0F;
709 if (alphabits != 8 && alphabits != 0)
711 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
715 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
718 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
722 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
723 if ((targa_header.attributes & 0x20) == 0)
725 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
726 row_inci = -image_width*2;
730 pixbufi = (unsigned int*)image_buffer;
737 if ((targa_header.image_type & ~8) == 2)
738 pix_inc = (targa_header.pixel_size + 7) / 8;
739 switch (targa_header.image_type)
741 case 1: /* colormapped, uncompressed */
742 case 3: /* greyscale, uncompressed */
743 if (fin + image_width * image_height * pix_inc > enddata)
745 for (y = 0;y < image_height;y++, pixbufi += row_inci)
746 for (x = 0;x < image_width;x++)
747 *pixbufi++ = palettei[*fin++];
750 /* BGR or BGRA, uncompressed */
751 if (fin + image_width * image_height * pix_inc > enddata)
753 if (targa_header.pixel_size == 32 && alphabits)
755 for (y = 0;y < image_height;y++)
756 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
760 for (y = 0;y < image_height;y++, pixbufi += row_inci)
762 for (x = 0;x < image_width;x++, fin += pix_inc)
773 case 9: /* colormapped, RLE */
774 case 11: /* greyscale, RLE */
775 for (y = 0;y < image_height;y++, pixbufi += row_inci)
777 for (x = 0;x < image_width;)
780 break; /* error - truncated file */
784 /* RLE - all pixels the same color */
786 if (fin + pix_inc > enddata)
787 break; /* error - truncated file */
788 if (x + runlen > image_width)
789 break; /* error - line exceeds width */
790 bgra.i = palettei[*fin++];
796 /* uncompressed - all pixels different color */
798 if (fin + pix_inc * runlen > enddata)
799 break; /* error - truncated file */
800 if (x + runlen > image_width)
801 break; /* error - line exceeds width */
803 *pixbufi++ = palettei[*fin++];
807 if (x != image_width)
809 /* pixbufi is useless now */
810 printf("LoadTGA: corrupt file\n");
816 /* BGR or BGRA, RLE */
817 if (targa_header.pixel_size == 32 && alphabits)
819 for (y = 0;y < image_height;y++, pixbufi += row_inci)
821 for (x = 0;x < image_width;)
824 break; /* error - truncated file */
828 /* RLE - all pixels the same color */
830 if (fin + pix_inc > enddata)
831 break; /* error - truncated file */
832 if (x + runlen > image_width)
833 break; /* error - line exceeds width */
844 /* uncompressed - all pixels different color */
846 if (fin + pix_inc * runlen > enddata)
847 break; /* error - truncated file */
848 if (x + runlen > image_width)
849 break; /* error - line exceeds width */
862 if (x != image_width)
864 /* pixbufi is useless now */
865 printf("LoadTGA: corrupt file\n");
872 for (y = 0;y < image_height;y++, pixbufi += row_inci)
874 for (x = 0;x < image_width;)
877 break; /* error - truncated file */
881 /* RLE - all pixels the same color */
883 if (fin + pix_inc > enddata)
884 break; /* error - truncated file */
885 if (x + runlen > image_width)
886 break; /* error - line exceeds width */
897 /* uncompressed - all pixels different color */
899 if (fin + pix_inc * runlen > enddata)
900 break; /* error - truncated file */
901 if (x + runlen > image_width)
902 break; /* error - line exceeds width */
915 if (x != image_width)
917 /* pixbufi is useless now */
918 printf("LoadTGA: corrupt file\n");
925 /* unknown image_type */
932 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
935 unsigned char *buffer, *out;
936 const unsigned char *in, *end;
939 buffer = (unsigned char *)malloc(width*height*4 + 18);
941 memset (buffer, 0, 18);
942 buffer[2] = 2; /* uncompressed type */
943 buffer[12] = (width >> 0) & 0xFF;
944 buffer[13] = (width >> 8) & 0xFF;
945 buffer[14] = (height >> 0) & 0xFF;
946 buffer[15] = (height >> 8) & 0xFF;
948 for (y = 3;y < width*height*4;y += 4)
952 if (y < width*height*4)
954 /* save the alpha channel */
955 buffer[16] = 32; /* pixel size */
956 buffer[17] = 8; /* 8 bits of alpha */
958 /* flip upside down */
960 for (y = height - 1;y >= 0;y--)
962 memcpy(out, data + y * width * 4, width * 4);
968 /* save only the color channels */
969 buffer[16] = 24; /* pixel size */
970 buffer[17] = 0; /* 8 bits of alpha */
972 /* truncate bgra to bgr and flip upside down */
974 for (y = height - 1;y >= 0;y--)
976 in = data + y * width * 4;
977 end = in + width * 4;
978 for (;in < end;in += 4)
986 ret = FS_WriteFile (filename, buffer, out - buffer);
992 /* START stuff that originates from image.c in DarkPlaces */
994 int usage(const char *me)
996 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> filtertype [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
997 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -1 [<scale>] (read from B)\n", me);
998 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -2 [<scale>] (read from G)\n", me);
999 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -3 [<scale>] (read from R)\n", me);
1000 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -4 [<scale>] (read from A)\n", me);
1001 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -5 [<scale>] (read from (R+G+B)/3)\n", me);
1002 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -6 [<scale>] (read from Y)\n", me);
1006 static const double filter_scharr3[3][3] = {
1007 { -3/32.0, 0, 3/32.0 },
1008 { -10/32.0, 0, 10/32.0 },
1009 { -3/32.0, 0, 3/32.0 }
1012 static const double filter_prewitt3[3][3] = {
1013 { -1/6.0, 0, 1/6.0 },
1014 { -1/6.0, 0, 1/6.0 },
1015 { -1/6.0, 0, 1/6.0 }
1018 // pathologic for inverting
1019 static const double filter_sobel3[3][3] = {
1020 { -1/8.0, 0, 1/8.0 },
1021 { -2/8.0, 0, 2/8.0 },
1022 { -1/8.0, 0, 1/8.0 }
1025 // pathologic for inverting
1026 static const double filter_sobel5[5][5] = {
1027 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 },
1028 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1029 { -6/128.0, -12/128.0, 0, 12/128.0, 6/128.0 },
1030 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1031 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 }
1034 // pathologic for inverting
1035 static const double filter_prewitt5[5][5] = {
1036 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1037 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1038 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1039 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1040 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }
1043 static const double filter_trivial[1][3] = {
1047 int main(int argc, char **argv)
1049 const char *infile, *outfile, *reffile;
1050 double scale, offset;
1052 int renormalize = 0;
1053 double highpass = 0;
1054 unsigned char *nmapdata, *nmap, *refmap;
1055 const char *filtertype;
1056 const double *filter = NULL;
1057 int filterw = 0, filterh = 0;
1058 #define USE_FILTER(f) \
1061 filterw = sizeof(*(f)) / sizeof(**(f)); \
1062 filterh = sizeof((f)) / sizeof(*(f)); \
1063 filter = &(f)[0][0]; \
1070 return usage(*argv);
1075 return usage(*argv);
1078 filtertype = argv[3];
1080 return usage(*argv);
1083 scale = atof(argv[4]);
1088 offset = atof(argv[5]);
1090 offset = (scale<0) ? 1 : 0;
1097 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"))
1098 renormalize = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"));
1099 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"))
1100 highpass = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"));
1102 nmapdata = FS_LoadFile(infile, &nmaplen);
1105 printf("FS_LoadFile failed\n");
1108 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
1112 printf("LoadTGA_BGRA failed\n");
1120 nmapdata = FS_LoadFile(reffile, &nmaplen);
1123 printf("FS_LoadFile failed\n");
1126 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
1130 printf("LoadTGA_BGRA failed\n");
1133 if(image_width != w || image_height != h)
1135 printf("reference map must have same size as input normalmap\n");
1142 if(!strcmp(filtertype, "trivial"))
1143 USE_FILTER(filter_trivial);
1144 if(!strcmp(filtertype, "prewitt3"))
1145 USE_FILTER(filter_prewitt3);
1146 if(!strcmp(filtertype, "scharr3"))
1147 USE_FILTER(filter_scharr3);
1148 if(!strcmp(filtertype, "sobel3"))
1149 USE_FILTER(filter_sobel3);
1150 if(!strcmp(filtertype, "prewitt5"))
1151 USE_FILTER(filter_prewitt5);
1152 if(!strcmp(filtertype, "sobel5"))
1153 USE_FILTER(filter_sobel5);
1158 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset, filter, filterw, filterh);
1160 hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
1163 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh, renormalize, highpass);
1165 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1167 printf("Image_WriteTGABGRA failed\n");