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)
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)
112 // we must invert whatever "filter" would do on (x, y)!
114 fftw_complex response_x = 0;
115 fftw_complex response_y = 0;
117 for(i = -filterh / 2; i <= filterh / 2; ++i)
118 for(j = -filterw / 2; j <= filterw / 2; ++j)
120 response_x += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (j * fx + i * fy));
121 response_y += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cexp(-_Complex_I * TWO_PI * (i * fx + j * fy));
125 // fourier(df/dx)_xy = fourier(f)_xy * response_x
126 // fourier(df/dy)_xy = fourier(f)_xy * response_y
127 // mult by conjugate of response_x, response_y:
128 // conj(response_x) * fourier(df/dx)_xy = fourier(f)_xy * |response_x^2|
129 // conj(response_y) * fourier(df/dy)_xy = fourier(f)_xy * |response_y^2|
131 // fourier(f)_xy = (conj(response_x) * fourier(df/dx)_xy + conj(response_y) * fourier(df/dy)_xy) / (|response_x|^2 + |response_y|^2)
133 sum = cabs(response_x) * cabs(response_x) + cabs(response_y) * cabs(response_y);
136 freqspace1[(w*y+x)] = (conj(response_x) * freqspace1[(w*y+x)] + conj(response_y) * freqspace2[(w*y+x)]) / sum;
138 freqspace1[(w*y+x)] = 0;
140 fftw_complex response_x = {0, 0};
141 fftw_complex response_y = {0, 0};
143 for(i = -filterh / 2; i <= filterh / 2; ++i)
144 for(j = -filterw / 2; j <= filterw / 2; ++j)
146 response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy));
147 response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy));
148 response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy));
149 response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy));
152 sum = response_x[0] * response_x[0] + response_x[1] * response_x[1]
153 + response_y[0] * response_y[0] + response_y[1] * response_y[1];
157 double s = freqspace1[(w*y+x)][0];
158 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;
159 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;
163 freqspace1[(w*y+x)][0] = 0;
164 freqspace1[(w*y+x)][1] = 0;
170 // 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;
202 /* renormalize, find min/max */
204 for(y = 0; y < h; ++y)
205 for(x = 0; x < w; ++x)
208 v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5));
210 v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5));
211 // imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
212 // this value is never used
214 if(v < vmin || (x == 0 && y == 0))
216 if(v > vmax || (x == 0 && y == 0))
224 double sa, sfa, sffa, sfva, sva;
226 sa = sfa = sffa = sfva = sva = 0;
229 for(y = 0; y < h; ++y)
230 for(x = 0; x < w; ++x)
232 a = (int)refmap[(w*y+x)*4+3];
233 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);
234 v = (v - 128.0) / 127.0;
236 f = creal(imgspace1[(w*y+x)]);
238 f = imgspace1[(w*y+x)][0];
254 /* linear regression ftw */
255 o = (sfa*sfva - sffa*sva) / (sfa*sfa-sa*sffa);
256 s = (sfa*sva - sa*sfva) / (sfa*sfa-sa*sffa);
258 else /* all values of v are equal, so we cannot get scale; we can still get offset */
260 o = ((sva - sfa) / sa);
265 * now apply user-given offset and scale to these values
266 * (x * s + o) * scale + offset
267 * x * s * scale + o * scale + offset
278 scale = 2 / (vmax - vmin);
279 offset = -(vmax + vmin) / (vmax - vmin);
282 printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n",
283 vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset);
285 for(y = 0; y < h; ++y)
286 for(x = 0; x < w; ++x)
289 v = creal(imgspace1[(w*y+x)]);
291 v = imgspace1[(w*y+x)][0];
293 v = v * scale + offset;
298 map[(w*y+x)*4+3] = floor(128.5 + 127 * v);
301 fftw_destroy_plan(i12f1);
302 fftw_destroy_plan(i22f2);
303 fftw_destroy_plan(f12i1);
305 fftw_free(freqspace2);
306 fftw_free(freqspace1);
307 fftw_free(imgspace2);
308 fftw_free(imgspace1);
311 void hmap_to_nmap(unsigned char *map, int w, int h, int src_chan, double scale)
321 fftw_complex *imgspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
322 fftw_complex *imgspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
323 fftw_complex *freqspace1 = fftw_malloc(w*h * sizeof(fftw_complex));
324 fftw_complex *freqspace2 = fftw_malloc(w*h * sizeof(fftw_complex));
325 fftw_plan i12f1 = fftw_plan_dft_2d(h, w, imgspace1, freqspace1, FFTW_FORWARD, FFTW_ESTIMATE);
326 fftw_plan f12i1 = fftw_plan_dft_2d(h, w, freqspace1, imgspace1, FFTW_BACKWARD, FFTW_ESTIMATE);
327 fftw_plan f22i2 = fftw_plan_dft_2d(h, w, freqspace2, imgspace2, FFTW_BACKWARD, FFTW_ESTIMATE);
329 for(y = 0; y < h; ++y)
330 for(x = 0; x < w; ++x)
338 v = map[(w*y+x)*4+src_chan];
341 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
345 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);
349 imgspace1[(w*y+x)] = (v - 128.0) / 127.0;
351 imgspace1[(w*y+x)][0] = (v - 128.0) / 127.0;
352 imgspace1[(w*y+x)][1] = 0;
355 v = 1; /* do not write alpha zero */
356 map[(w*y+x)*4+3] = floor(v + 0.5);
359 /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
363 for(y = 0; y < h; ++y)
364 for(x = 0; x < w; ++x)
373 fx = sin(fx * TWO_PI / w);
374 fy = sin(fy * TWO_PI / h);
377 /* a lowpass to prevent the worst */
378 freqspace1[(w*y+x)] *= 1 - pow(abs(fx) / (double)(w/2), 1);
379 freqspace1[(w*y+x)] *= 1 - pow(abs(fy) / (double)(h/2), 1);
381 /* a lowpass to prevent the worst */
382 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fx) / (double)(w/2), 1);
383 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fx) / (double)(w/2), 1);
384 freqspace1[(w*y+x)][0] *= 1 - pow(abs(fy) / (double)(h/2), 1);
385 freqspace1[(w*y+x)][1] *= 1 - pow(abs(fy) / (double)(h/2), 1);
389 freqspace2[(w*y+x)] = TWO_PI*_Complex_I * fy * freqspace1[(w*y+x)]; /* y derivative */
390 freqspace1[(w*y+x)] = TWO_PI*_Complex_I * fx * freqspace1[(w*y+x)]; /* x derivative */
392 freqspace2[(w*y+x)][0] = -TWO_PI * fy * freqspace1[(w*y+x)][1]; /* y derivative */
393 freqspace2[(w*y+x)][1] = TWO_PI * fy * freqspace1[(w*y+x)][0];
394 save = freqspace1[(w*y+x)][0];
395 freqspace1[(w*y+x)][0] = -TWO_PI * fx * freqspace1[(w*y+x)][1]; /* x derivative */
396 freqspace1[(w*y+x)][1] = TWO_PI * fx * save;
405 for(y = 0; y < h; ++y)
406 for(x = 0; x < w; ++x)
409 nx = creal(imgspace1[(w*y+x)]);
410 ny = creal(imgspace2[(w*y+x)]);
412 nx = imgspace1[(w*y+x)][0];
413 ny = imgspace2[(w*y+x)][0];
418 v = -sqrt(nx*nx + ny*ny + nz*nz);
422 ny = -ny; /* DP inverted normals */
423 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
424 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
425 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
428 fftw_destroy_plan(i12f1);
429 fftw_destroy_plan(f12i1);
430 fftw_destroy_plan(f22i2);
432 fftw_free(freqspace2);
433 fftw_free(freqspace1);
434 fftw_free(imgspace2);
435 fftw_free(imgspace1);
438 void hmap_to_nmap_local(unsigned char *map, int w, int h, int src_chan, double scale, const double *filter, int filterw, int filterh)
444 double *img_reduced = malloc(w*h * sizeof(double));
446 for(y = 0; y < h; ++y)
447 for(x = 0; x < w; ++x)
455 v = map[(w*y+x)*4+src_chan];
458 v = (map[(w*y+x)*4+0] + map[(w*y+x)*4+1] + map[(w*y+x)*4+2]) / 3;
462 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);
465 img_reduced[(w*y+x)] = (v - 128.0) / 127.0;
467 v = 1; /* do not write alpha zero */
468 map[(w*y+x)*4+3] = floor(v + 0.5);
471 for(y = 0; y < h; ++y)
472 for(x = 0; x < w; ++x)
477 for(i = -filterh / 2; i <= filterh / 2; ++i)
478 for(j = -filterw / 2; j <= filterw / 2; ++j)
480 nx += img_reduced[w*((y+i+h)%h)+(x+j+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
481 ny += img_reduced[w*((y+j+h)%h)+(x+i+w)%w] * filter[(i + filterh / 2) * filterw + j + filterw / 2];
484 v = -sqrt(nx*nx + ny*ny + nz*nz);
488 ny = -ny; /* DP inverted normals */
489 map[(w*y+x)*4+2] = floor(128 + 127.5 * nx);
490 map[(w*y+x)*4+1] = floor(128 + 127.5 * ny);
491 map[(w*y+x)*4+0] = floor(128 + 127.5 * nz);
497 unsigned char *FS_LoadFile(const char *fn, int *len)
499 unsigned char *buf = NULL;
501 FILE *f = fopen(fn, "rb");
507 buf = realloc(buf, *len + 65536);
515 n = fread(buf + *len, 1, 65536, f);
530 int FS_WriteFile(const char *fn, unsigned char *data, int len)
532 FILE *f = fopen(fn, "wb");
535 if(fwrite(data, len, 1, f) != 1)
545 /* START stuff that originates from image.c in DarkPlaces */
546 int image_width, image_height;
548 typedef struct _TargaHeader
550 unsigned char id_length, colormap_type, image_type;
551 unsigned short colormap_index, colormap_length;
552 unsigned char colormap_size;
553 unsigned short x_origin, y_origin, width, height;
554 unsigned char pixel_size, attributes;
558 void PrintTargaHeader(TargaHeader *t)
560 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);
563 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
565 int x, y, pix_inc, row_inci, runlen, alphabits;
566 unsigned char *image_buffer;
567 unsigned int *pixbufi;
568 const unsigned char *fin, *enddata;
569 TargaHeader targa_header;
570 unsigned int palettei[256];
581 enddata = f + filesize;
583 targa_header.id_length = f[0];
584 targa_header.colormap_type = f[1];
585 targa_header.image_type = f[2];
587 targa_header.colormap_index = f[3] + f[4] * 256;
588 targa_header.colormap_length = f[5] + f[6] * 256;
589 targa_header.colormap_size = f[7];
590 targa_header.x_origin = f[8] + f[9] * 256;
591 targa_header.y_origin = f[10] + f[11] * 256;
592 targa_header.width = image_width = f[12] + f[13] * 256;
593 targa_header.height = image_height = f[14] + f[15] * 256;
594 targa_header.pixel_size = f[16];
595 targa_header.attributes = f[17];
597 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
599 printf("LoadTGA: invalid size\n");
600 PrintTargaHeader(&targa_header);
604 /* advance to end of header */
607 /* skip TARGA image comment (usually 0 bytes) */
608 fin += targa_header.id_length;
610 /* read/skip the colormap if present (note: according to the TARGA spec it */
611 /* can be present even on 1color or greyscale images, just not used by */
612 /* the image data) */
613 if (targa_header.colormap_type)
615 if (targa_header.colormap_length > 256)
617 printf("LoadTGA: only up to 256 colormap_length supported\n");
618 PrintTargaHeader(&targa_header);
621 if (targa_header.colormap_index)
623 printf("LoadTGA: colormap_index not supported\n");
624 PrintTargaHeader(&targa_header);
627 if (targa_header.colormap_size == 24)
629 for (x = 0;x < targa_header.colormap_length;x++)
635 palettei[x] = bgra.i;
638 else if (targa_header.colormap_size == 32)
640 memcpy(palettei, fin, targa_header.colormap_length*4);
641 fin += targa_header.colormap_length * 4;
645 printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
646 PrintTargaHeader(&targa_header);
651 /* check our pixel_size restrictions according to image_type */
652 switch (targa_header.image_type & ~8)
655 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
657 printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
658 PrintTargaHeader(&targa_header);
663 /* set up a palette to make the loader easier */
664 for (x = 0;x < 256;x++)
666 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
668 palettei[x] = bgra.i;
670 /* fall through to colormap case */
672 if (targa_header.pixel_size != 8)
674 printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
675 PrintTargaHeader(&targa_header);
680 printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
681 PrintTargaHeader(&targa_header);
685 if (targa_header.attributes & 0x10)
687 printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
691 /* number of attribute bits per pixel, we only support 0 or 8 */
692 alphabits = targa_header.attributes & 0x0F;
693 if (alphabits != 8 && alphabits != 0)
695 printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
699 image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
702 printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
706 /* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
707 if ((targa_header.attributes & 0x20) == 0)
709 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
710 row_inci = -image_width*2;
714 pixbufi = (unsigned int*)image_buffer;
721 if ((targa_header.image_type & ~8) == 2)
722 pix_inc = (targa_header.pixel_size + 7) / 8;
723 switch (targa_header.image_type)
725 case 1: /* colormapped, uncompressed */
726 case 3: /* greyscale, uncompressed */
727 if (fin + image_width * image_height * pix_inc > enddata)
729 for (y = 0;y < image_height;y++, pixbufi += row_inci)
730 for (x = 0;x < image_width;x++)
731 *pixbufi++ = palettei[*fin++];
734 /* BGR or BGRA, uncompressed */
735 if (fin + image_width * image_height * pix_inc > enddata)
737 if (targa_header.pixel_size == 32 && alphabits)
739 for (y = 0;y < image_height;y++)
740 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
744 for (y = 0;y < image_height;y++, pixbufi += row_inci)
746 for (x = 0;x < image_width;x++, fin += pix_inc)
757 case 9: /* colormapped, RLE */
758 case 11: /* greyscale, RLE */
759 for (y = 0;y < image_height;y++, pixbufi += row_inci)
761 for (x = 0;x < image_width;)
764 break; /* error - truncated file */
768 /* RLE - all pixels the same color */
770 if (fin + pix_inc > enddata)
771 break; /* error - truncated file */
772 if (x + runlen > image_width)
773 break; /* error - line exceeds width */
774 bgra.i = palettei[*fin++];
780 /* uncompressed - all pixels different color */
782 if (fin + pix_inc * runlen > enddata)
783 break; /* error - truncated file */
784 if (x + runlen > image_width)
785 break; /* error - line exceeds width */
787 *pixbufi++ = palettei[*fin++];
791 if (x != image_width)
793 /* pixbufi is useless now */
794 printf("LoadTGA: corrupt file\n");
800 /* BGR or BGRA, RLE */
801 if (targa_header.pixel_size == 32 && alphabits)
803 for (y = 0;y < image_height;y++, pixbufi += row_inci)
805 for (x = 0;x < image_width;)
808 break; /* error - truncated file */
812 /* RLE - all pixels the same color */
814 if (fin + pix_inc > enddata)
815 break; /* error - truncated file */
816 if (x + runlen > image_width)
817 break; /* error - line exceeds width */
828 /* uncompressed - all pixels different color */
830 if (fin + pix_inc * runlen > enddata)
831 break; /* error - truncated file */
832 if (x + runlen > image_width)
833 break; /* error - line exceeds width */
846 if (x != image_width)
848 /* pixbufi is useless now */
849 printf("LoadTGA: corrupt file\n");
856 for (y = 0;y < image_height;y++, pixbufi += row_inci)
858 for (x = 0;x < image_width;)
861 break; /* error - truncated file */
865 /* RLE - all pixels the same color */
867 if (fin + pix_inc > enddata)
868 break; /* error - truncated file */
869 if (x + runlen > image_width)
870 break; /* error - line exceeds width */
881 /* uncompressed - all pixels different color */
883 if (fin + pix_inc * runlen > enddata)
884 break; /* error - truncated file */
885 if (x + runlen > image_width)
886 break; /* error - line exceeds width */
899 if (x != image_width)
901 /* pixbufi is useless now */
902 printf("LoadTGA: corrupt file\n");
909 /* unknown image_type */
916 int Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
919 unsigned char *buffer, *out;
920 const unsigned char *in, *end;
923 buffer = (unsigned char *)malloc(width*height*4 + 18);
925 memset (buffer, 0, 18);
926 buffer[2] = 2; /* uncompressed type */
927 buffer[12] = (width >> 0) & 0xFF;
928 buffer[13] = (width >> 8) & 0xFF;
929 buffer[14] = (height >> 0) & 0xFF;
930 buffer[15] = (height >> 8) & 0xFF;
932 for (y = 3;y < width*height*4;y += 4)
936 if (y < width*height*4)
938 /* save the alpha channel */
939 buffer[16] = 32; /* pixel size */
940 buffer[17] = 8; /* 8 bits of alpha */
942 /* flip upside down */
944 for (y = height - 1;y >= 0;y--)
946 memcpy(out, data + y * width * 4, width * 4);
952 /* save only the color channels */
953 buffer[16] = 24; /* pixel size */
954 buffer[17] = 0; /* 8 bits of alpha */
956 /* truncate bgra to bgr and flip upside down */
958 for (y = height - 1;y >= 0;y--)
960 in = data + y * width * 4;
961 end = in + width * 4;
962 for (;in < end;in += 4)
970 ret = FS_WriteFile (filename, buffer, out - buffer);
976 /* START stuff that originates from image.c in DarkPlaces */
978 int usage(const char *me)
980 printf("Usage: %s <infile_norm.tga> <outfile_normandheight.tga> filtertype [<scale> [<offset> [<infile_ref.tga>]]] (get heightmap from normalmap)\n", me);
981 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -1 [<scale>] (read from B)\n", me);
982 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -2 [<scale>] (read from G)\n", me);
983 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -3 [<scale>] (read from R)\n", me);
984 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -4 [<scale>] (read from A)\n", me);
985 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -5 [<scale>] (read from (R+G+B)/3)\n", me);
986 printf("or: %s <infile_height.tga> <outfile_normandheight.tga> filtertype -6 [<scale>] (read from Y)\n", me);
990 static const double filter_scharr3[3][3] = {
991 { -3/32.0, 0, 3/32.0 },
992 { -10/32.0, 0, 10/32.0 },
993 { -3/32.0, 0, 3/32.0 }
996 static const double filter_prewitt3[3][3] = {
997 { -1/6.0, 0, 1/6.0 },
998 { -1/6.0, 0, 1/6.0 },
1002 // pathologic for inverting
1003 static const double filter_sobel3[3][3] = {
1004 { -1/8.0, 0, 1/8.0 },
1005 { -2/8.0, 0, 2/8.0 },
1006 { -1/8.0, 0, 1/8.0 }
1009 // pathologic for inverting
1010 static const double filter_sobel5[5][5] = {
1011 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 },
1012 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1013 { -6/128.0, -12/128.0, 0, 12/128.0, 6/128.0 },
1014 { -4/128.0, -8/128.0, 0, 8/128.0, 4/128.0 },
1015 { -1/128.0, -2/128.0, 0, 2/128.0, 1/128.0 }
1018 // pathologic for inverting
1019 static const double filter_prewitt5[5][5] = {
1020 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1021 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1022 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1023 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 },
1024 { -1/40.0, -2/40.0, 0, 2/40.0, 1/40.0 }
1027 static const double filter_trivial[1][3] = {
1031 int main(int argc, char **argv)
1033 const char *infile, *outfile, *reffile;
1034 double scale, offset;
1036 int renormalize = 0;
1037 unsigned char *nmapdata, *nmap, *refmap;
1038 const char *filtertype;
1039 const double *filter = NULL;
1040 int filterw = 0, filterh = 0;
1041 #define USE_FILTER(f) \
1044 filterw = sizeof(*(f)) / sizeof(**(f)); \
1045 filterh = sizeof((f)) / sizeof(*(f)); \
1046 filter = &(f)[0][0]; \
1053 return usage(*argv);
1058 return usage(*argv);
1061 filtertype = argv[3];
1063 return usage(*argv);
1066 scale = atof(argv[4]);
1071 offset = atof(argv[5]);
1073 offset = (scale<0) ? 1 : 0;
1080 if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"))
1081 renormalize = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"));
1083 nmapdata = FS_LoadFile(infile, &nmaplen);
1086 printf("FS_LoadFile failed\n");
1089 nmap = LoadTGA_BGRA(nmapdata, nmaplen);
1093 printf("LoadTGA_BGRA failed\n");
1101 nmapdata = FS_LoadFile(reffile, &nmaplen);
1104 printf("FS_LoadFile failed\n");
1107 refmap = LoadTGA_BGRA(nmapdata, nmaplen);
1111 printf("LoadTGA_BGRA failed\n");
1114 if(image_width != w || image_height != h)
1116 printf("reference map must have same size as input normalmap\n");
1123 if(!strcmp(filtertype, "trivial"))
1124 USE_FILTER(filter_trivial);
1125 if(!strcmp(filtertype, "prewitt3"))
1126 USE_FILTER(filter_prewitt3);
1127 if(!strcmp(filtertype, "scharr3"))
1128 USE_FILTER(filter_scharr3);
1129 if(!strcmp(filtertype, "sobel3"))
1130 USE_FILTER(filter_sobel3);
1131 if(!strcmp(filtertype, "prewitt5"))
1132 USE_FILTER(filter_prewitt5);
1133 if(!strcmp(filtertype, "sobel5"))
1134 USE_FILTER(filter_sobel5);
1139 hmap_to_nmap_local(nmap, image_width, image_height, -scale-1, offset, filter, filterw, filterh);
1141 hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
1144 nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh, renormalize);
1146 if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
1148 printf("Image_WriteTGABGRA failed\n");