Debian Codegraph @

utils/climate/texture.cxx

raw symbols SCIP
1 /*
2 * Texture manipulation routines
3 *
4 * Copyright (c) Mark J. Kilgard, 1997.
5 * Code added in april 2003 by Erik Hofman
6 *
7 * This program is freely distributable without licensing fees
8 * and is provided without guarantee or warrantee expressed or
9 * implied. This program is -not- in the public domain.
10 *
11 * $Id$
12 */
13
14 #include <simgear/compiler.h>
15
16 #ifdef WIN32
17 # include <windows.h>
18 #endif
19
20 #include <osg/Matrixf>
21
22 #include <math.h>
23 #include <zlib.h>
24
25 #include "texture.hxx"
26 #include "colours.h"
27
28
29 const char *FILE_OPEN_ERROR = "Unable to open file.";
30 const char *WRONG_COUNT = "Unsupported number of color channels.";
31 const char *NO_TEXTURE = "No texture data resident.";
32 const char *OUT_OF_MEMORY = "Out of memory.";
33
34
35 SGTexture::SGTexture()
36 : texture_id(0),
37 texture_data(0),
38 num_colors(3),
39 file(0)
40 {
41 }
42
43 SGTexture::SGTexture(unsigned int width, unsigned int height)
44 : texture_id(0),
45 errstr("")
46 {
47 texture_data = new GLubyte[ width * height * 3 ];
48 }
49
50 SGTexture::~SGTexture()
51 {
52 delete[] texture_data;
53
54 if ( texture_id != 0 ) {
55 free_id();
56 }
57 }
58
59 void
60 SGTexture::bind()
61 {
62 bool gen = false;
63 if (!texture_id) {
64 #ifdef GL_VERSION_1_1
65 glGenTextures(1, &texture_id);
66
67 #elif GL_EXT_texture_object
68 glGenTexturesEXT(1, &texture_id);
69 #endif
70 gen = true;
71 }
72
73 #ifdef GL_VERSION_1_1
74 glBindTexture(GL_TEXTURE_2D, texture_id);
75
76 #elif GL_EXT_texture_object
77 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
78 #endif
79
80 if (gen) {
81 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
82 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
83 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
84 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
85 }
86 }
87
88 /**
89 * A function to resize the OpenGL window which will be used by
90 * the dynamic texture generating routines.
91 *
92 * @param width The width of the new window
93 * @param height The height of the new window
94 */
95 void
96 SGTexture::resize(unsigned int width, unsigned int height)
97 {
98 using namespace osg;
99 GLfloat aspect;
100
101 // Make sure that we don't get a divide by zero exception
102 if (height == 0)
103 height = 1;
104
105 // Set the viewport for the OpenGL window
106 glViewport(0, 0, width, height);
107
108 // Calculate the aspect ratio of the window
109 aspect = width/height;
110
111 // Go to the projection matrix, this gets modified by the perspective
112 // calulations
113 glMatrixMode(GL_PROJECTION);
114
115 // Do the perspective calculations
116 Matrixf proj = Matrixf::perspective(45.0, aspect, 1.0, 400.0);
117 glLoadMatrix(proj.ptr());
118
119 // Return to the modelview matrix
120 glMatrixMode(GL_MODELVIEW);
121 }
122
123 /**
124 * A function to prepare the OpenGL state machine for dynamic
125 * texture generation.
126 *
127 * @param width The width of the texture
128 * @param height The height of the texture
129 */
130 void
131 SGTexture::prepare(unsigned int width, unsigned int height) {
132
133 texture_width = width;
134 texture_height = height;
135
136 // Resize the OpenGL window to the size of our dynamic texture
137 resize(texture_width, texture_height);
138
139 glClearColor(0.0, 0.0, 0.0, 1.0);
140 }
141
142 /**
143 * A function to generate the dynamic texture.
144 *
145 * The actual texture can be accessed by calling get_texture()
146 *
147 * @param width The width of the previous OpenGL window
148 * @param height The height of the previous OpenGL window
149 */
150 void
151 SGTexture::finish(unsigned int width, unsigned int height) {
152 // If a texture hasn't been created then it gets created, and the contents
153 // of the frame buffer gets copied into it. If the texture has already been
154 // created then its contents just get updated.
155 bind();
156 if (!texture_data)
157 {
158 // Copies the contents of the frame buffer into the texture
159 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
160 texture_width, texture_height, 0);
161
162 } else {
163 // Copies the contents of the frame buffer into the texture
164 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
165 texture_width, texture_height);
166 }
167
168 // Set the OpenGL window back to its previous size
169 resize(width, height);
170
171 // Clear the window back to black
172 glClearColor(0.0, 0.0, 0.0, 1.0);
173 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174 }
175
176
177 void
178 SGTexture::read_alpha_texture(const char *name)
179 {
180 GLubyte *lptr;
181 SGTexture::ImageRec *image;
182 int y;
183
184 delete[] texture_data;
185
186 image = ImageOpen(name);
187 if(!image) {
188 errstr = FILE_OPEN_ERROR;
189 return;
190 }
191
192 texture_width = image->xsize;
193 texture_height = image->ysize;
194
195 // printf("image->zsize = %d\n", image->zsize);
196
197 if (image->zsize != 1) {
198 ImageClose(image);
199 errstr = WRONG_COUNT;
200 return;
201 }
202
203 texture_data = new GLubyte[ image->xsize * image->ysize ];
204 num_colors = 1;
205 if (!texture_data) {
206 errstr = NO_TEXTURE;
207 return;
208 }
209
210 lptr = texture_data;
211 for(y=0; y<image->ysize; y++) {
212 ImageGetRow(image,lptr,y,0);
213 lptr += image->xsize;
214 }
215 ImageClose(image);
216 }
217
218 void
219 SGTexture::read_rgb_texture(const char *name)
220 {
221 GLubyte *ptr;
222 GLubyte *rbuf, *gbuf, *bbuf;
223 SGTexture::ImageRec *image;
224 int y;
225
226 delete[] texture_data;
227
228 image = ImageOpen(name);
229 if(!image) {
230 errstr = FILE_OPEN_ERROR;
231 return;
232 }
233
234 texture_width = image->xsize;
235 texture_height = image->ysize;
236 if (image->zsize < 1 || image->zsize > 4) {
237 ImageClose(image);
238 errstr = WRONG_COUNT;
239 return;
240 }
241
242 num_colors = 3;
243 texture_data = new GLubyte[ image->xsize * image->ysize * num_colors ];
244 rbuf = new GLubyte[ image->xsize ];
245 gbuf = new GLubyte[ image->xsize ];
246 bbuf = new GLubyte[ image->xsize ];
247 if(!texture_data || !rbuf || !gbuf || !bbuf) {
248 delete[] texture_data;
249 delete[] rbuf;
250 delete[] gbuf;
251 delete[] bbuf;
252 errstr = OUT_OF_MEMORY;
253 return;
254 }
255
256 ptr = texture_data;
257 for(y=0; y<image->ysize; y++) {
258 if(image->zsize == 4 || image->zsize == 3) {
259 ImageGetRow(image,rbuf,y,0);
260 ImageGetRow(image,gbuf,y,1);
261 ImageGetRow(image,bbuf,y,2);
262 } else {
263 ImageGetRow(image,rbuf,y,0);
264 memcpy(gbuf,rbuf,image->xsize);
265 memcpy(bbuf,rbuf,image->xsize);
266 }
267 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
268 ptr += (image->xsize * num_colors);
269 }
270
271 ImageClose(image);
272 delete[] rbuf;
273 delete[] gbuf;
274 delete[] bbuf;
275 }
276
277
278
279 void
280 SGTexture::read_rgba_texture(const char *name)
281 {
282 GLubyte *ptr;
283 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
284 SGTexture::ImageRec *image;
285 int y;
286
287 delete[] texture_data;
288
289 image = ImageOpen(name);
290 if(!image) {
291 errstr = FILE_OPEN_ERROR;
292 return;
293 }
294
295 texture_width = image->xsize;
296 texture_height = image->ysize;
297 if (image->zsize < 1 || image->zsize > 4) {
298 ImageClose(image);
299 errstr = WRONG_COUNT;
300 return;
301 }
302
303 num_colors = 4;
304 texture_data = new GLubyte[ image->xsize * image->ysize * num_colors ];
305 rbuf = new GLubyte[ image->xsize ];
306 gbuf = new GLubyte[ image->xsize ];
307 bbuf = new GLubyte[ image->xsize ];
308 abuf = new GLubyte[ image->xsize ];
309 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
310 delete[] texture_data;
311 delete[] rbuf;
312 delete[] gbuf;
313 delete[] bbuf;
314 delete[] abuf;
315 errstr = OUT_OF_MEMORY;
316 return;
317 }
318
319 ptr = texture_data;
320 for(y=0; y<image->ysize; y++) {
321 if(image->zsize == 4) {
322 ImageGetRow(image,rbuf,y,0);
323 ImageGetRow(image,gbuf,y,1);
324 ImageGetRow(image,bbuf,y,2);
325 ImageGetRow(image,abuf,y,3);
326 } else if(image->zsize == 3) {
327 ImageGetRow(image,rbuf,y,0);
328 ImageGetRow(image,gbuf,y,1);
329 ImageGetRow(image,bbuf,y,2);
330 memset(abuf, 255, image->xsize);
331 } else if(image->zsize == 2) {
332 ImageGetRow(image,rbuf,y,0);
333 memcpy(gbuf,rbuf,image->xsize);
334 memcpy(bbuf,rbuf,image->xsize);
335 ImageGetRow(image,abuf,y,1);
336 } else {
337 ImageGetRow(image,rbuf,y,0);
338 memcpy(gbuf,rbuf,image->xsize);
339 memcpy(bbuf,rbuf,image->xsize);
340 memset(abuf, 255, image->xsize);
341 }
342 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
343 ptr += (image->xsize * num_colors);
344 }
345
346 ImageClose(image);
347 delete[] rbuf;
348 delete[] gbuf;
349 delete[] bbuf;
350 delete[] abuf;
351 }
352
353 void
354 SGTexture::read_raw_texture(const char *name)
355 {
356 GLubyte *ptr;
357 SGTexture::ImageRec *image;
358 int y;
359
360 delete[] texture_data;
361
362 image = RawImageOpen(name);
363
364 if(!image) {
365 errstr = FILE_OPEN_ERROR;
366 return;
367 }
368
369 texture_width = 256;
370 texture_height = 256;
371
372 texture_data = new GLubyte[ 256 * 256 * 3 ];
373 if(!texture_data) {
374 errstr = OUT_OF_MEMORY;
375 return;
376 }
377
378 ptr = texture_data;
379 for(y=0; y<256; y++) {
380 gzread(image->file, ptr, 256*3);
381 ptr+=256*3;
382 }
383 ImageClose(image);
384 }
385
386 void
387 SGTexture::read_r8_texture(const char *name)
388 {
389 unsigned char c[1];
390 GLubyte *ptr;
391 SGTexture::ImageRec *image;
392 int xy;
393
394 delete[] texture_data;
395
396 //it wouldn't make sense to write a new function ...
397 image = RawImageOpen(name);
398
399 if(!image) {
400 errstr = FILE_OPEN_ERROR;
401 return;
402 }
403
404 texture_width = 256;
405 texture_height = 256;
406
407 texture_data = new GLubyte [ 256 * 256 * 3 ];
408 if(!texture_data) {
409 errstr = OUT_OF_MEMORY;
410 return;
411 }
412
413 ptr = texture_data;
414 for(xy=0; xy<(256*256); xy++) {
415 gzread(image->file, c, 1);
416
417 //look in the table for the right colours
418 ptr[0]=msfs_colour[c[0]][0];
419 ptr[1]=msfs_colour[c[0]][1];
420 ptr[2]=msfs_colour[c[0]][2];
421
422 ptr+=3;
423 }
424 ImageClose(image);
425 }
426
427
428 void
429 SGTexture::write_texture(const char *name) {
430 SGTexture::ImageRec *image = ImageWriteOpen(name);
431
432 printf("colors: %i, height: %i, width: %i\n", num_colors, texture_height, texture_width);
433 for (int c=0; c<num_colors; c++) {
434 GLubyte *ptr = texture_data + c;
435 for (int y=0; y<texture_height; y++) {
436 for (int x=0; x<texture_width; x++) {
437 image->tmp[x]=*ptr;
438 ptr = ptr + num_colors;
439 }
440 fwrite(image->tmp, 1, texture_width, file);
441 }
442 }
443
444 ImageClose(image);
445 }
446
447
448 void
449 SGTexture::set_pixel(GLuint x, GLuint y, GLubyte *c)
450 {
451 if (!texture_data) {
452 errstr = NO_TEXTURE;
453 return;
454 }
455
456 unsigned int pos = (x + y*texture_width) * num_colors;
457 memcpy(texture_data+pos, c, num_colors);
458 }
459
460
461 GLubyte *
462 SGTexture::get_pixel(GLuint x, GLuint y)
463 {
464 static GLubyte c[4] = {0, 0, 0, 0};
465
466 if (!texture_data) {
467 errstr = NO_TEXTURE;
468 return c;
469 }
470
471 unsigned int pos = (x + y*texture_width)*num_colors;
472 memcpy(c, texture_data + pos, num_colors);
473
474 return c;
475 }
476
477 SGTexture::ImageRec *
478 SGTexture::ImageOpen(const char *fileName)
479 {
480 union {
481 int testWord;
482 char testByte[4];
483 } endianTest;
484
485 SGTexture::ImageRec *image;
486 int swapFlag;
487 int x;
488
489 endianTest.testWord = 1;
490 if (endianTest.testByte[0] == 1) {
491 swapFlag = 1;
492 } else {
493 swapFlag = 0;
494 }
495
496 image = new SGTexture::ImageRec;
497 memset(image, 0, sizeof(SGTexture::ImageRec));
498 if (image == 0) {
499 errstr = OUT_OF_MEMORY;
500 return 0;
501 }
502 if ((image->file = gzopen(fileName, "rb")) == 0) {
503 errstr = FILE_OPEN_ERROR;
504 return 0;
505 }
506
507 gzread(image->file, image, 12);
508
509 if (swapFlag) {
510 ConvertShort(&image->imagic, 6);
511 }
512
513 image->tmp = new GLubyte[ image->xsize * 256 ];
514 if (image->tmp == 0) {
515 errstr = OUT_OF_MEMORY;
516 return 0;
517 }
518
519 if ((image->type & 0xFF00) == 0x0100) {
520 x = image->ysize * image->zsize * (int) sizeof(unsigned);
521 image->rowStart = new unsigned[x];
522 image->rowSize = new int[x];
523 if (image->rowStart == 0 || image->rowSize == 0) {
524 errstr = OUT_OF_MEMORY;
525 return 0;
526 }
527 image->rleEnd = 512 + (2 * x);
528 gzseek(image->file, 512, SEEK_SET);
529 gzread(image->file, image->rowStart, x);
530 gzread(image->file, image->rowSize, x);
531 if (swapFlag) {
532 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
533 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
534 }
535 }
536 return image;
537 }
538
539
540 void
541 SGTexture::ImageClose(SGTexture::ImageRec *image) {
542 if (image->file) gzclose(image->file);
543 if (file) fclose(file);
544 delete[] image->tmp;
545 delete[] image->rowStart;
546 delete[] image->rowSize;
547 delete image;
548 }
549
550 SGTexture::ImageRec *
551 SGTexture::RawImageOpen(const char *fileName)
552 {
553 union {
554 int testWord;
555 char testByte[4];
556 } endianTest;
557
558 SGTexture::ImageRec *image;
559 int swapFlag;
560
561 endianTest.testWord = 1;
562 if (endianTest.testByte[0] == 1) {
563 swapFlag = 1;
564 } else {
565 swapFlag = 0;
566 }
567
568 image = new SGTexture::ImageRec;
569 memset(image, 0, sizeof(SGTexture::ImageRec));
570 if (image == 0) {
571 errstr = OUT_OF_MEMORY;
572 return 0;
573 }
574 if ((image->file = gzopen(fileName, "rb")) == 0) {
575 errstr = FILE_OPEN_ERROR;
576 return 0;
577 }
578
579 gzread(image->file, image, 12);
580
581 if (swapFlag) {
582 ConvertShort(&image->imagic, 6);
583 }
584
585
586 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
587 image->tmp = new GLubyte[1];
588
589 if (image->tmp == 0) {
590 errstr = OUT_OF_MEMORY;
591 return 0;
592 }
593
594 return image;
595 }
596
597 SGTexture::ImageRec *
598 SGTexture::ImageWriteOpen(const char *fileName)
599 {
600 union {
601 int testWord;
602 char testByte[4];
603 } endianTest;
604 ImageRec* image;
605 int swapFlag;
606 int x;
607
608 endianTest.testWord = 1;
609 if (endianTest.testByte[0] == 1) {
610 swapFlag = 1;
611 } else {
612 swapFlag = 0;
613 }
614
615 image = new SGTexture::ImageRec;
616 memset(image, 0, sizeof(SGTexture::ImageRec));
617 if (image == 0) {
618 errstr = OUT_OF_MEMORY;
619 return 0;
620 }
621 if ((file = fopen(fileName, "wb")) == 0) {
622 errstr = FILE_OPEN_ERROR;
623 return 0;
624 }
625
626 image->imagic = 474;
627 image->type = 0x0001;
628 image->dim = (num_colors > 1) ? 3 : 2;
629 image->xsize = texture_width;
630 image->ysize = texture_height;
631 image->zsize = num_colors;
632
633 if (swapFlag) {
634 ConvertShort(&image->imagic, 6);
635 }
636
637 fwrite(image, 1, 12, file);
638 fseek(file, 512, SEEK_SET);
639
640 image->tmp = new GLubyte[ image->xsize * 256 ];
641 if (image->tmp == 0) {
642 errstr = OUT_OF_MEMORY;
643 return 0;
644 }
645
646 if ((image->type & 0xFF00) == 0x0100) {
647 x = image->ysize * image->zsize * (int) sizeof(unsigned);
648 image->rowStart = new unsigned[x];
649 image->rowSize = new int[x];
650 if (image->rowStart == 0 || image->rowSize == 0) {
651 errstr = OUT_OF_MEMORY;
652 return 0;
653 }
654 image->rleEnd = 512 + (2 * x);
655 fseek(file, 512, SEEK_SET);
656 fread(image->rowStart, 1, x, file);
657 fread(image->rowSize, 1, x, file);
658 if (swapFlag) {
659 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
660 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
661 }
662 }
663
664 return image;
665
666 }
667
668 void
669 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
670 GLubyte *iPtr, *oPtr, pixel;
671 int count;
672
673 if ((image->type & 0xFF00) == 0x0100) {
674 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
675 int size = image->rowSize[y+z*image->ysize];
676 gzread(image->file, image->tmp, size);
677
678 iPtr = image->tmp;
679 oPtr = buf;
680 for (GLubyte *limit = iPtr + size; iPtr < limit;) {
681 pixel = *iPtr++;
682 count = (int)(pixel & 0x7F);
683 if (!count) {
684 errstr = WRONG_COUNT;
685 return;
686 }
687 if (pixel & 0x80) {
688 while (iPtr < limit && count--) {
689 *oPtr++ = *iPtr++;
690 }
691 } else if (iPtr < limit) {
692 pixel = *iPtr++;
693 while (count--) {
694 *oPtr++ = pixel;
695 }
696 }
697 }
698 } else {
699 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
700 SEEK_SET);
701 gzread(image->file, buf, image->xsize);
702 }
703 }
704
705 void
706 SGTexture::ImagePutRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
707 GLubyte *iPtr, *oPtr, pixel;
708 int count;
709
710 if ((image->type & 0xFF00) == 0x0100) {
711 fseek(file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
712 fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
713 file);
714
715 iPtr = image->tmp;
716 oPtr = buf;
717 for (;;) {
718 pixel = *iPtr++;
719 count = (int)(pixel & 0x7F);
720 if (!count) {
721 errstr = WRONG_COUNT;
722 return;
723 }
724 if (pixel & 0x80) {
725 while (count--) {
726 *oPtr++ = *iPtr++;
727 }
728 } else {
729 pixel = *iPtr++;
730 while (count--) {
731 *oPtr++ = pixel;
732 }
733 }
734 }
735 } else {
736 fseek(file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
737 SEEK_SET);
738 fread(buf, 1, image->xsize, file);
739 }
740 }
741
742
743 void
744 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
745 while(n--) {
746 l[0] = r[0];
747 l[1] = g[0];
748 l[2] = b[0];
749 l += 3; r++; g++; b++;
750 }
751 }
752
753 void
754 SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
755 GLubyte *l, int n) {
756 while(n--) {
757 l[0] = r[0];
758 l[1] = g[0];
759 l[2] = b[0];
760 l[3] = a[0];
761 l += 4; r++; g++; b++; a++;
762 }
763 }
764
765
766 void
767 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
768 unsigned short b1, b2;
769 unsigned char *ptr;
770
771 ptr = (unsigned char *)array;
772 while (length--) {
773 b1 = *ptr++;
774 b2 = *ptr++;
775 *array++ = (b1 << 8) | (b2);
776 }
777 }
778
779
780 void
781 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
782 unsigned int b1, b2, b3, b4;
783 unsigned char *ptr;
784
785 ptr = (unsigned char *)array;
786 while (length--) {
787 b1 = *ptr++;
788 b2 = *ptr++;
789 b3 = *ptr++;
790 b4 = *ptr++;
791 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
792 }
793 }
794
795
796 void
797 SGTexture::make_monochrome(float contrast, GLubyte r, GLubyte g, GLubyte b) {
798
799 if (num_colors >= 3)
800 return;
801
802 GLubyte ap[3];
803 for (int y=0; y<texture_height; y++)
804 for (int x=0; x<texture_width; x++)
805 {
806 GLubyte *rgb = get_pixel(x,y);
807 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
808
809 if (contrast != 1.0) {
810 float pixcol = -1.0 + (avg/128);
811 avg = 128 + int(128*pow(pixcol, contrast));
812 }
813
814 ap[0] = avg*r/255;
815 ap[1] = avg*g/255;
816 ap[2] = avg*b/255;
817
818 set_pixel(x,y,ap);
819 }
820 }
821
822
823 void
824 SGTexture::make_grayscale(float contrast) {
825 if (num_colors < 3)
826 return;
827
828 int colors = (num_colors == 3) ? 1 : 2;
829 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
830
831 for (int y=0; y<texture_height; y++)
832 for (int x=0; x<texture_width; x++)
833 {
834 GLubyte *rgb = get_pixel(x,y);
835 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
836
837 if (contrast != 1.0) {
838 float pixcol = -1.0 + (avg/128);
839 avg = 128 + int(128*pow(pixcol, contrast));
840 }
841
842 int pos = (x + y*texture_width)*colors;
843 map[pos] = avg;
844 if (colors > 1)
845 map[pos+1] = rgb[3];
846 }
847
848 delete[] texture_data;
849 texture_data = map;
850 num_colors = colors;
851 }
852
853
854 void
855 SGTexture::make_maxcolorwindow() {
856 GLubyte minmaxc[2] = {255, 0};
857
858 int pos = 0;
859 int max = num_colors;
860 if (num_colors == 2) max = 1;
861 if (num_colors == 4) max = 3;
862 while (pos < texture_width * texture_height * num_colors) {
863 for (int i=0; i < max; i++) {
864 GLubyte c = texture_data[pos+i];
865 if (c < minmaxc[0]) minmaxc[0] = c;
866 if (c > minmaxc[1]) minmaxc[1] = c;
867 }
868 pos += num_colors;
869 }
870
871 GLubyte offs = minmaxc[0];
872 float factor = 255.0 / float(minmaxc[1] - minmaxc[0]);
873 // printf("Min: %i, Max: %i, Factor: %f\n", offs, minmaxc[1], factor);
874
875 pos = 0;
876 while (pos < texture_width * texture_height * num_colors) {
877 for (int i=0; i < max; i++) {
878 texture_data[pos+i] -= offs;
879 texture_data[pos+i] = int(factor * texture_data[pos+i]);
880 }
881 pos += num_colors;
882 }
883 }
884
885
886 void
887 SGTexture::make_normalmap(float brightness, float contrast) {
888 make_grayscale(contrast);
889 make_maxcolorwindow();
890
891 int colors = (num_colors == 1) ? 3 : 4;
892 bool alpha = (colors > 3);
893 int tsize = texture_width * texture_height * colors;
894 GLubyte *map = new GLubyte[ tsize ];
895
896 int mpos = 0, dpos = 0;
897 for (int y=0; y<texture_height; y++) {
898 int ytw = y*texture_width;
899
900 for (int x=0; x<texture_width; x++)
901 {
902 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
903 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
904 int posxp1 = (xp1 + ytw)*num_colors;
905 int posyp1 = (x + yp1*texture_width)*num_colors;
906 float fx,fy;
907
908 GLubyte c = texture_data[dpos];
909 GLubyte cx1 = texture_data[posxp1];
910 GLubyte cy1 = texture_data[posyp1];
911
912 if (alpha) {
913 GLubyte a = texture_data[dpos+1];
914 GLubyte ax1 = texture_data[posxp1+1];
915 GLubyte ay1 = texture_data[posyp1+1];
916
917 c = (c + a)/2;
918 cx1 = (cx1 + ax1)/2;
919 cy1 = (cy1 + ay1)/2;
920
921 map[mpos+3] = a;
922 }
923
924 fx = asin((c/256.0-cx1/256.0))/1.57079633;
925 fy = asin((cy1/256.0-c/256.0))/1.57079633;
926
927 map[mpos+0] = (GLuint)(fx*256.0)-128;
928 map[mpos+1] = (GLuint)(fy*256.0)-128;
929 map[mpos+2] = 127+int(brightness*128); // 255-c/2;
930
931 mpos += colors;
932 dpos += num_colors;
933 }
934 }
935
936 delete[] texture_data;
937 texture_data = map;
938 num_colors = colors;
939 }
940
941
942 void
943 SGTexture::make_bumpmap(float brightness, float contrast) {
944 make_grayscale(contrast);
945
946 int colors = (num_colors == 1) ? 1 : 2;
947 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
948
949 for (int y=0; y<texture_height; y++)
950 for (int x=0; x<texture_width; x++)
951 {
952 int mpos = (x + y*texture_width)*colors;
953 int dpos = (x + y*texture_width)*num_colors;
954
955 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
956 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
957 int posxp1 = (xp1 + y*texture_width)*num_colors;
958 int posyp1 = (x + yp1*texture_width)*num_colors;
959
960 map[mpos] = (127 - ((texture_data[dpos]-texture_data[posxp1]) -
961 ((texture_data[dpos]-texture_data[posyp1]))/4))/2;
962 if (colors > 1)
963 map[mpos+1] = texture_data[dpos+1];
964 }
965
966 delete[] texture_data;
967 texture_data = map;
968 num_colors = colors;
969 }
970