2 * Texture manipulation routines
4 * Copyright (c) Mark J. Kilgard, 1997.
5 * Code added in april 2003 by Erik Hofman
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.
14 #include <simgear/compiler.h>
25 #include "texture.hxx"
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.";
35 SGTexture::SGTexture()
43 SGTexture::SGTexture(unsigned int width, unsigned int height)
47 texture_data = new GLubyte[ width * height * 3 ];
50 SGTexture::~SGTexture()
52 delete[] texture_data;
54 if ( texture_id != 0 ) {
65 glGenTextures(1, &texture_id);
67 #elif GL_EXT_texture_object
68 glGenTexturesEXT(1, &texture_id);
74 glBindTexture(GL_TEXTURE_2D, texture_id);
76 #elif GL_EXT_texture_object
77 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
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);
89 * A function to resize the OpenGL window which will be used by
90 * the dynamic texture generating routines.
92 * @param width The width of the new window
93 * @param height The height of the new window
96 SGTexture::resize(unsigned int width, unsigned int height)
100 // Make sure that we don't get a divide by zero exception
104 // Set the viewport for the OpenGL window
105 glViewport(0, 0, width, height);
107 // Calculate the aspect ratio of the window
108 aspect = width/height;
110 // Go to the projection matrix, this gets modified by the perspective
112 glMatrixMode(GL_PROJECTION);
115 // Do the perspective calculations
116 gluPerspective(45.0, aspect, 1.0, 400.0);
118 // Return to the modelview matrix
119 glMatrixMode(GL_MODELVIEW);
123 * A function to prepare the OpenGL state machine for dynamic
124 * texture generation.
126 * @param width The width of the texture
127 * @param height The height of the texture
130 SGTexture::prepare(unsigned int width, unsigned int height) {
132 texture_width = width;
133 texture_height = height;
135 // Resize the OpenGL window to the size of our dynamic texture
136 resize(texture_width, texture_height);
138 glClearColor(0.0, 0.0, 0.0, 1.0);
142 * A function to generate the dynamic texture.
144 * The actual texture can be accessed by calling get_texture()
146 * @param width The width of the previous OpenGL window
147 * @param height The height of the previous OpenGL window
150 SGTexture::finish(unsigned int width, unsigned int height) {
151 // If a texture hasn't been created then it gets created, and the contents
152 // of the frame buffer gets copied into it. If the texture has already been
153 // created then its contents just get updated.
157 // Copies the contents of the frame buffer into the texture
158 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
159 texture_width, texture_height, 0);
162 // Copies the contents of the frame buffer into the texture
163 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
164 texture_width, texture_height);
167 // Set the OpenGL window back to its previous size
168 resize(width, height);
170 // Clear the window back to black
171 glClearColor(0.0, 0.0, 0.0, 1.0);
172 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
177 SGTexture::read_alpha_texture(const char *name)
180 SGTexture::ImageRec *image;
183 delete[] texture_data;
185 image = ImageOpen(name);
187 errstr = FILE_OPEN_ERROR;
191 texture_width = image->xsize;
192 texture_height = image->ysize;
194 // printf("image->zsize = %d\n", image->zsize);
196 if (image->zsize != 1) {
198 errstr = WRONG_COUNT;
202 texture_data = new GLubyte[ image->xsize * image->ysize ];
210 for(y=0; y<image->ysize; y++) {
211 ImageGetRow(image,lptr,y,0);
212 lptr += image->xsize;
218 SGTexture::read_rgb_texture(const char *name)
221 GLubyte *rbuf, *gbuf, *bbuf;
222 SGTexture::ImageRec *image;
225 delete[] texture_data;
227 image = ImageOpen(name);
229 errstr = FILE_OPEN_ERROR;
233 texture_width = image->xsize;
234 texture_height = image->ysize;
235 if (image->zsize < 1 || image->zsize > 4) {
237 errstr = WRONG_COUNT;
242 texture_data = new GLubyte[ image->xsize * image->ysize * num_colors ];
243 rbuf = new GLubyte[ image->xsize ];
244 gbuf = new GLubyte[ image->xsize ];
245 bbuf = new GLubyte[ image->xsize ];
246 if(!texture_data || !rbuf || !gbuf || !bbuf) {
247 delete[] texture_data;
251 errstr = OUT_OF_MEMORY;
256 for(y=0; y<image->ysize; y++) {
257 if(image->zsize == 4 || image->zsize == 3) {
258 ImageGetRow(image,rbuf,y,0);
259 ImageGetRow(image,gbuf,y,1);
260 ImageGetRow(image,bbuf,y,2);
262 ImageGetRow(image,rbuf,y,0);
263 memcpy(gbuf,rbuf,image->xsize);
264 memcpy(bbuf,rbuf,image->xsize);
266 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
267 ptr += (image->xsize * num_colors);
279 SGTexture::read_rgba_texture(const char *name)
282 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
283 SGTexture::ImageRec *image;
286 delete[] texture_data;
288 image = ImageOpen(name);
290 errstr = FILE_OPEN_ERROR;
294 texture_width = image->xsize;
295 texture_height = image->ysize;
296 if (image->zsize < 1 || image->zsize > 4) {
298 errstr = WRONG_COUNT;
303 texture_data = new GLubyte[ image->xsize * image->ysize * num_colors ];
304 rbuf = new GLubyte[ image->xsize ];
305 gbuf = new GLubyte[ image->xsize ];
306 bbuf = new GLubyte[ image->xsize ];
307 abuf = new GLubyte[ image->xsize ];
308 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
309 delete[] texture_data;
314 errstr = OUT_OF_MEMORY;
319 for(y=0; y<image->ysize; y++) {
320 if(image->zsize == 4) {
321 ImageGetRow(image,rbuf,y,0);
322 ImageGetRow(image,gbuf,y,1);
323 ImageGetRow(image,bbuf,y,2);
324 ImageGetRow(image,abuf,y,3);
325 } else if(image->zsize == 3) {
326 ImageGetRow(image,rbuf,y,0);
327 ImageGetRow(image,gbuf,y,1);
328 ImageGetRow(image,bbuf,y,2);
329 memset(abuf, 255, image->xsize);
330 } else if(image->zsize == 2) {
331 ImageGetRow(image,rbuf,y,0);
332 memcpy(gbuf,rbuf,image->xsize);
333 memcpy(bbuf,rbuf,image->xsize);
334 ImageGetRow(image,abuf,y,1);
336 ImageGetRow(image,rbuf,y,0);
337 memcpy(gbuf,rbuf,image->xsize);
338 memcpy(bbuf,rbuf,image->xsize);
339 memset(abuf, 255, image->xsize);
341 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
342 ptr += (image->xsize * num_colors);
353 SGTexture::read_raw_texture(const char *name)
356 SGTexture::ImageRec *image;
359 delete[] texture_data;
361 image = RawImageOpen(name);
364 errstr = FILE_OPEN_ERROR;
369 texture_height = 256;
371 texture_data = new GLubyte[ 256 * 256 * 3 ];
373 errstr = OUT_OF_MEMORY;
378 for(y=0; y<256; y++) {
379 gzread(image->file, ptr, 256*3);
386 SGTexture::read_r8_texture(const char *name)
390 SGTexture::ImageRec *image;
393 delete[] texture_data;
395 //it wouldn't make sense to write a new function ...
396 image = RawImageOpen(name);
399 errstr = FILE_OPEN_ERROR;
404 texture_height = 256;
406 texture_data = new GLubyte [ 256 * 256 * 3 ];
408 errstr = OUT_OF_MEMORY;
413 for(xy=0; xy<(256*256); xy++) {
414 gzread(image->file, c, 1);
416 //look in the table for the right colours
417 ptr[0]=msfs_colour[c[0]][0];
418 ptr[1]=msfs_colour[c[0]][1];
419 ptr[2]=msfs_colour[c[0]][2];
428 SGTexture::write_texture(const char *name) {
429 SGTexture::ImageRec *image = ImageWriteOpen(name);
431 for (int c=0; c<num_colors; c++) {
432 GLubyte *ptr = texture_data + c;
433 for (int y=0; y<texture_height; y++) {
434 for (int x=0; x<texture_width; x++) {
436 ptr = ptr + num_colors;
438 fwrite(image->tmp, 1, texture_width, file);
447 SGTexture::set_pixel(GLuint x, GLuint y, GLubyte *c)
454 unsigned int pos = (x + y*texture_width) * num_colors;
455 memcpy(texture_data+pos, c, num_colors);
460 SGTexture::get_pixel(GLuint x, GLuint y)
462 static GLubyte c[4] = {0, 0, 0, 0};
469 unsigned int pos = (x + y*texture_width)*num_colors;
470 memcpy(c, texture_data + pos, num_colors);
475 SGTexture::ImageRec *
476 SGTexture::ImageOpen(const char *fileName)
483 SGTexture::ImageRec *image;
487 endianTest.testWord = 1;
488 if (endianTest.testByte[0] == 1) {
494 image = new SGTexture::ImageRec;
495 memset(image, 0, sizeof(SGTexture::ImageRec));
497 errstr = OUT_OF_MEMORY;
500 if ((image->file = gzopen(fileName, "rb")) == 0) {
501 errstr = FILE_OPEN_ERROR;
505 gzread(image->file, image, 12);
508 ConvertShort(&image->imagic, 6);
511 image->tmp = new GLubyte[ image->xsize * 256 ];
512 if (image->tmp == 0) {
513 errstr = OUT_OF_MEMORY;
517 if ((image->type & 0xFF00) == 0x0100) {
518 x = image->ysize * image->zsize * (int) sizeof(unsigned);
519 image->rowStart = new unsigned[x];
520 image->rowSize = new int[x];
521 if (image->rowStart == 0 || image->rowSize == 0) {
522 errstr = OUT_OF_MEMORY;
525 image->rleEnd = 512 + (2 * x);
526 gzseek(image->file, 512, SEEK_SET);
527 gzread(image->file, image->rowStart, x);
528 gzread(image->file, image->rowSize, x);
530 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
531 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
539 SGTexture::ImageClose(SGTexture::ImageRec *image) {
540 if (image->file) gzclose(image->file);
541 if (file) fclose(file);
543 delete[] image->rowStart;
544 delete[] image->rowSize;
548 SGTexture::ImageRec *
549 SGTexture::RawImageOpen(const char *fileName)
556 SGTexture::ImageRec *image;
559 endianTest.testWord = 1;
560 if (endianTest.testByte[0] == 1) {
566 image = new SGTexture::ImageRec;
567 memset(image, 0, sizeof(SGTexture::ImageRec));
569 errstr = OUT_OF_MEMORY;
572 if ((image->file = gzopen(fileName, "rb")) == 0) {
573 errstr = FILE_OPEN_ERROR;
577 gzread(image->file, image, 12);
580 ConvertShort(&image->imagic, 6);
584 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
585 image->tmp = new GLubyte[1];
587 if (image->tmp == 0) {
588 errstr = OUT_OF_MEMORY;
595 SGTexture::ImageRec *
596 SGTexture::ImageWriteOpen(const char *fileName)
606 endianTest.testWord = 1;
607 if (endianTest.testByte[0] == 1) {
613 image = new SGTexture::ImageRec;
614 memset(image, 0, sizeof(SGTexture::ImageRec));
616 errstr = OUT_OF_MEMORY;
619 if ((file = fopen(fileName, "wb")) == 0) {
620 errstr = FILE_OPEN_ERROR;
625 image->type = 0x0001;
626 image->dim = (num_colors > 1) ? 3 : 2;
627 image->xsize = texture_width;
628 image->ysize = texture_height;
629 image->zsize = num_colors;
632 ConvertShort(&image->imagic, 6);
635 fwrite(image, 1, 12, file);
636 fseek(file, 512, SEEK_SET);
638 image->tmp = new GLubyte[ image->xsize * 256 ];
639 if (image->tmp == 0) {
640 errstr = OUT_OF_MEMORY;
644 if ((image->type & 0xFF00) == 0x0100) {
645 x = image->ysize * image->zsize * (int) sizeof(unsigned);
646 image->rowStart = new unsigned[x];
647 image->rowSize = new int[x];
648 if (image->rowStart == 0 || image->rowSize == 0) {
649 errstr = OUT_OF_MEMORY;
652 image->rleEnd = 512 + (2 * x);
653 fseek(file, 512, SEEK_SET);
654 fread(image->rowStart, 1, x, file);
655 fread(image->rowSize, 1, x, file);
657 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
658 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
667 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
668 GLubyte *iPtr, *oPtr, pixel;
671 if ((image->type & 0xFF00) == 0x0100) {
672 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
673 int size = image->rowSize[y+z*image->ysize];
674 gzread(image->file, image->tmp, size);
678 for (GLubyte *limit = iPtr + size; iPtr < limit;) {
680 count = (int)(pixel & 0x7F);
682 errstr = WRONG_COUNT;
686 while (iPtr < limit && count--) {
689 } else if (iPtr < limit) {
697 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
699 gzread(image->file, buf, image->xsize);
704 SGTexture::ImagePutRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
705 GLubyte *iPtr, *oPtr, pixel;
708 if ((image->type & 0xFF00) == 0x0100) {
709 fseek(file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
710 fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
717 count = (int)(pixel & 0x7F);
719 errstr = WRONG_COUNT;
734 fseek(file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
736 fread(buf, 1, image->xsize, file);
742 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
747 l += 3; r++; g++; b++;
752 SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
759 l += 4; r++; g++; b++; a++;
765 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
766 unsigned short b1, b2;
769 ptr = (unsigned char *)array;
773 *array++ = (b1 << 8) | (b2);
779 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
780 unsigned int b1, b2, b3, b4;
783 ptr = (unsigned char *)array;
789 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
795 SGTexture::make_monochrome(float contrast, GLubyte r, GLubyte g, GLubyte b) {
801 for (int y=0; y<texture_height; y++)
802 for (int x=0; x<texture_width; x++)
804 GLubyte *rgb = get_pixel(x,y);
805 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
807 if (contrast != 1.0) {
808 float pixcol = -1.0 + (avg/128);
809 avg = 128 + int(128*pow(pixcol, contrast));
822 SGTexture::make_grayscale(float contrast) {
826 int colors = (num_colors == 3) ? 1 : 2;
827 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
829 for (int y=0; y<texture_height; y++)
830 for (int x=0; x<texture_width; x++)
832 GLubyte *rgb = get_pixel(x,y);
833 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
835 if (contrast != 1.0) {
836 float pixcol = -1.0 + (avg/128);
837 avg = 128 + int(128*pow(pixcol, contrast));
840 int pos = (x + y*texture_width)*colors;
846 delete[] texture_data;
853 SGTexture::make_maxcolorwindow() {
854 GLubyte minmaxc[2] = {255, 0};
857 int max = num_colors;
858 if (num_colors == 2) max = 1;
859 if (num_colors == 4) max = 3;
860 while (pos < texture_width * texture_height * num_colors) {
861 for (int i=0; i < max; i++) {
862 GLubyte c = texture_data[pos+i];
863 if (c < minmaxc[0]) minmaxc[0] = c;
864 if (c > minmaxc[1]) minmaxc[1] = c;
869 GLubyte offs = minmaxc[0];
870 float factor = 255.0 / float(minmaxc[1] - minmaxc[0]);
871 // printf("Min: %i, Max: %i, Factor: %f\n", offs, minmaxc[1], factor);
874 while (pos < texture_width * texture_height * num_colors) {
875 for (int i=0; i < max; i++) {
876 texture_data[pos+i] -= offs;
877 texture_data[pos+i] = int(factor * texture_data[pos+i]);
885 SGTexture::make_normalmap(float brightness, float contrast) {
886 make_grayscale(contrast);
887 make_maxcolorwindow();
889 int colors = (num_colors == 1) ? 3 : 4;
890 bool alpha = (colors > 3);
891 int tsize = texture_width * texture_height * colors;
892 GLubyte *map = new GLubyte[ tsize ];
894 int mpos = 0, dpos = 0;
895 for (int y=0; y<texture_height; y++) {
896 int ytw = y*texture_width;
898 for (int x=0; x<texture_width; x++)
900 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
901 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
902 int posxp1 = (xp1 + ytw)*num_colors;
903 int posyp1 = (x + yp1*texture_width)*num_colors;
905 GLubyte c = texture_data[dpos];
906 GLubyte cx1 = texture_data[posxp1];
907 GLubyte cy1 = texture_data[posyp1];
910 GLubyte a = texture_data[dpos+1];
911 GLubyte ax1 = texture_data[posxp1+1];
912 GLubyte ay1 = texture_data[posyp1+1];
921 map[mpos+0] = (128+(cx1-c)/2);
922 map[mpos+1] = (128+(cy1-c)/2);
923 map[mpos+2] = 127+int(brightness*128); // 255-c/2;
930 delete[] texture_data;
937 SGTexture::make_bumpmap(float brightness, float contrast) {
938 make_grayscale(contrast);
940 int colors = (num_colors == 1) ? 1 : 2;
941 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
943 for (int y=0; y<texture_height; y++)
944 for (int x=0; x<texture_width; x++)
946 int mpos = (x + y*texture_width)*colors;
947 int dpos = (x + y*texture_width)*num_colors;
949 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
950 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
951 int posxp1 = (xp1 + y*texture_width)*num_colors;
952 int posyp1 = (x + yp1*texture_width)*num_colors;
954 map[mpos] = (127 - ((texture_data[dpos]-texture_data[posxp1]) -
955 ((texture_data[dpos]-texture_data[posyp1]))/4))/2;
957 map[mpos+1] = texture_data[dpos+1];
960 delete[] texture_data;