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()
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, *abuf;
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 != 3 && image->zsize != 4) {
237 errstr = WRONG_COUNT;
241 texture_data = new GLubyte[ image->xsize * image->ysize * 3 ];
243 rbuf = new GLubyte[ image->xsize ];
244 gbuf = new GLubyte[ image->xsize ];
245 bbuf = new GLubyte[ image->xsize ];
246 abuf = new GLubyte[ image->xsize ];
247 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
248 delete[] texture_data;
253 errstr = OUT_OF_MEMORY;
258 for(y=0; y<image->ysize; y++) {
259 if(image->zsize == 4) {
260 ImageGetRow(image,rbuf,y,0);
261 ImageGetRow(image,gbuf,y,1);
262 ImageGetRow(image,bbuf,y,2);
263 ImageGetRow(image,abuf,y,3); // discard
264 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
265 ptr += (image->xsize * 3);
267 ImageGetRow(image,rbuf,y,0);
268 ImageGetRow(image,gbuf,y,1);
269 ImageGetRow(image,bbuf,y,2);
270 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
271 ptr += (image->xsize * 3);
285 SGTexture::read_rgba_texture(const char *name)
288 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
289 SGTexture::ImageRec *image;
292 delete[] texture_data;
294 image = ImageOpen(name);
296 errstr = FILE_OPEN_ERROR;
300 texture_width = image->xsize;
301 texture_height = image->ysize;
302 if (image->zsize != 3 && image->zsize != 4) {
304 errstr = WRONG_COUNT;
308 texture_data = new GLubyte[ image->xsize * image->ysize * 4 ];
310 rbuf = new GLubyte[ image->xsize ];
311 gbuf = new GLubyte[ image->xsize ];
312 bbuf = new GLubyte[ image->xsize ];
313 abuf = new GLubyte[ image->xsize ];
314 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
315 delete[] texture_data;
320 errstr = OUT_OF_MEMORY;
325 memset(abuf, 255, image->xsize);
326 for(y=0; y<image->ysize; y++) {
327 if(image->zsize == 4) {
328 ImageGetRow(image,rbuf,y,0);
329 ImageGetRow(image,gbuf,y,1);
330 ImageGetRow(image,bbuf,y,2);
331 ImageGetRow(image,abuf,y,3);
332 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
333 ptr += (image->xsize * 4);
335 ImageGetRow(image,rbuf,y,0);
336 ImageGetRow(image,gbuf,y,1);
337 ImageGetRow(image,bbuf,y,2);
338 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
339 ptr += (image->xsize * 3);
351 SGTexture::read_raw_texture(const char *name)
354 SGTexture::ImageRec *image;
357 delete[] texture_data;
359 image = RawImageOpen(name);
362 errstr = FILE_OPEN_ERROR;
367 texture_height = 256;
369 texture_data = new GLubyte[ 256 * 256 * 3 ];
371 errstr = OUT_OF_MEMORY;
376 for(y=0; y<256; y++) {
377 gzread(image->file, ptr, 256*3);
384 SGTexture::read_r8_texture(const char *name)
388 SGTexture::ImageRec *image;
391 delete[] texture_data;
393 //it wouldn't make sense to write a new function ...
394 image = RawImageOpen(name);
397 errstr = FILE_OPEN_ERROR;
402 texture_height = 256;
404 texture_data = new GLubyte [ 256 * 256 * 3 ];
406 errstr = OUT_OF_MEMORY;
411 for(xy=0; xy<(256*256); xy++) {
412 gzread(image->file, c, 1);
414 //look in the table for the right colours
415 ptr[0]=msfs_colour[c[0]][0];
416 ptr[1]=msfs_colour[c[0]][1];
417 ptr[2]=msfs_colour[c[0]][2];
426 SGTexture::write_texture(const char *name) {
427 SGTexture::ImageRec *image = ImageWriteOpen(name);
429 for (int c=0; c<num_colors; c++) {
430 GLubyte *ptr = texture_data + c;
431 for (int y=0; y<texture_height; y++) {
432 for (int x=0; x<texture_width; x++) {
434 ptr = ptr + num_colors;
436 fwrite(image->tmp, 1, texture_width, file);
445 SGTexture::set_pixel(GLuint x, GLuint y, GLubyte *c)
452 unsigned int pos = (x + y*texture_width) * num_colors;
453 memcpy(texture_data+pos, c, num_colors);
458 SGTexture::get_pixel(GLuint x, GLuint y)
460 static GLubyte c[4] = {0, 0, 0, 0};
467 unsigned int pos = (x + y*texture_width)*num_colors;
468 memcpy(c, texture_data + pos, num_colors);
473 SGTexture::ImageRec *
474 SGTexture::ImageOpen(const char *fileName)
481 SGTexture::ImageRec *image;
485 endianTest.testWord = 1;
486 if (endianTest.testByte[0] == 1) {
492 image = new SGTexture::ImageRec;
493 memset(image, 0, sizeof(SGTexture::ImageRec));
495 errstr = OUT_OF_MEMORY;
498 if ((image->file = gzopen(fileName, "rb")) == 0) {
499 errstr = FILE_OPEN_ERROR;
503 gzread(image->file, image, 12);
506 ConvertShort(&image->imagic, 6);
509 image->tmp = new GLubyte[ image->xsize * 256 ];
510 if (image->tmp == 0) {
511 errstr = OUT_OF_MEMORY;
515 if ((image->type & 0xFF00) == 0x0100) {
516 x = image->ysize * image->zsize * (int) sizeof(unsigned);
517 image->rowStart = new unsigned[x];
518 image->rowSize = new int[x];
519 if (image->rowStart == 0 || image->rowSize == 0) {
520 errstr = OUT_OF_MEMORY;
523 image->rleEnd = 512 + (2 * x);
524 gzseek(image->file, 512, SEEK_SET);
525 gzread(image->file, image->rowStart, x);
526 gzread(image->file, image->rowSize, x);
528 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
529 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
537 SGTexture::ImageClose(SGTexture::ImageRec *image) {
538 if (image->file) gzclose(image->file);
539 if (file) fclose(file);
544 SGTexture::ImageRec *
545 SGTexture::RawImageOpen(const char *fileName)
552 SGTexture::ImageRec *image;
555 endianTest.testWord = 1;
556 if (endianTest.testByte[0] == 1) {
562 image = new SGTexture::ImageRec;
563 memset(image, 0, sizeof(SGTexture::ImageRec));
565 errstr = OUT_OF_MEMORY;
568 if ((image->file = gzopen(fileName, "rb")) == 0) {
569 errstr = FILE_OPEN_ERROR;
573 gzread(image->file, image, 12);
576 ConvertShort(&image->imagic, 6);
580 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
581 image->tmp = new GLubyte[1];
583 if (image->tmp == 0) {
584 errstr = OUT_OF_MEMORY;
591 SGTexture::ImageRec *
592 SGTexture::ImageWriteOpen(const char *fileName)
602 endianTest.testWord = 1;
603 if (endianTest.testByte[0] == 1) {
609 image = new SGTexture::ImageRec;
610 memset(image, 0, sizeof(SGTexture::ImageRec));
612 errstr = OUT_OF_MEMORY;
615 if ((file = fopen(fileName, "wb")) == 0) {
616 errstr = FILE_OPEN_ERROR;
621 image->type = 0x0001;
622 image->dim = (num_colors > 1) ? 3 : 2;
623 image->xsize = texture_width;
624 image->ysize = texture_height;
625 image->zsize = num_colors;
628 ConvertShort(&image->imagic, 6);
631 fwrite(image, 1, 12, file);
632 fseek(file, 512, SEEK_SET);
634 image->tmp = new GLubyte[ image->xsize * 256 ];
635 if (image->tmp == 0) {
636 errstr = OUT_OF_MEMORY;
640 if ((image->type & 0xFF00) == 0x0100) {
641 x = image->ysize * image->zsize * (int) sizeof(unsigned);
642 image->rowStart = new unsigned[x];
643 image->rowSize = new int[x];
644 if (image->rowStart == 0 || image->rowSize == 0) {
645 errstr = OUT_OF_MEMORY;
648 image->rleEnd = 512 + (2 * x);
649 fseek(file, 512, SEEK_SET);
650 fread(image->rowStart, 1, x, file);
651 fread(image->rowSize, 1, x, file);
653 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
654 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
663 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
664 GLubyte *iPtr, *oPtr, pixel;
667 if ((image->type & 0xFF00) == 0x0100) {
668 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
669 gzread(image->file, image->tmp,
670 (unsigned int)image->rowSize[y+z*image->ysize]);
676 count = (int)(pixel & 0x7F);
678 errstr = WRONG_COUNT;
693 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
695 gzread(image->file, buf, image->xsize);
700 SGTexture::ImagePutRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
701 GLubyte *iPtr, *oPtr, pixel;
704 if ((image->type & 0xFF00) == 0x0100) {
705 fseek(file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
706 fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
713 count = (int)(pixel & 0x7F);
715 errstr = WRONG_COUNT;
730 fseek(file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
732 fread(buf, 1, image->xsize, file);
738 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
743 l += 3; r++; g++; b++;
748 SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
755 l += 4; r++; g++; b++; a++;
761 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
762 unsigned short b1, b2;
765 ptr = (unsigned char *)array;
769 *array++ = (b1 << 8) | (b2);
775 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
776 unsigned int b1, b2, b3, b4;
779 ptr = (unsigned char *)array;
785 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
791 SGTexture::make_monochrome(float contrast, GLubyte r, GLubyte g, GLubyte b) {
797 for (int y=0; y<texture_height; y++)
798 for (int x=0; x<texture_width; x++)
800 GLubyte *rgb = get_pixel(x,y);
801 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
803 if (contrast != 1.0) {
804 float pixcol = -1.0 + (avg/128);
805 avg = 128 + int(128*pow(pixcol, contrast));
818 SGTexture::make_grayscale(float contrast) {
822 int colors = (num_colors == 3) ? 1 : 2;
823 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
825 for (int y=0; y<texture_height; y++)
826 for (int x=0; x<texture_width; x++)
828 GLubyte *rgb = get_pixel(x,y);
829 GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
831 if (contrast != 1.0) {
832 float pixcol = -1.0 + (avg/128);
833 avg = 128 + int(128*pow(pixcol, contrast));
836 int pos = (x + y*texture_width)*colors;
842 delete[] texture_data;
849 SGTexture::make_maxcolorwindow() {
850 GLubyte minmaxc[2] = {255, 0};
853 int max = num_colors;
854 if (num_colors == 2) max = 1;
855 if (num_colors == 4) max = 3;
856 while (pos < texture_width * texture_height * num_colors) {
857 for (int i=0; i < max; i++) {
858 GLubyte c = texture_data[pos+i];
859 if (c < minmaxc[0]) minmaxc[0] = c;
860 if (c > minmaxc[1]) minmaxc[1] = c;
865 GLubyte offs = minmaxc[0];
866 float factor = 255.0 / float(minmaxc[1] - minmaxc[0]);
867 // printf("Min: %i, Max: %i, Factor: %f\n", offs, minmaxc[1], factor);
870 while (pos < texture_width * texture_height * num_colors) {
871 for (int i=0; i < max; i++) {
872 texture_data[pos+i] -= offs;
873 texture_data[pos+i] = int(factor * texture_data[pos+i]);
881 SGTexture::make_normalmap(float brightness, float contrast) {
882 make_grayscale(contrast);
883 make_maxcolorwindow();
885 int colors = (num_colors == 1) ? 3 : 4;
886 bool alpha = (colors > 3);
887 int tsize = texture_width * texture_height * colors;
888 GLubyte *map = new GLubyte[ tsize ];
890 int mpos = 0, dpos = 0;
891 for (int y=0; y<texture_height; y++) {
892 int ytw = y*texture_width;
894 for (int x=0; x<texture_width; x++)
896 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
897 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
898 int posxp1 = (xp1 + ytw)*num_colors;
899 int posyp1 = (x + yp1*texture_width)*num_colors;
901 GLubyte c = texture_data[dpos];
902 GLubyte cx1 = texture_data[posxp1];
903 GLubyte cy1 = texture_data[posyp1];
906 GLubyte a = texture_data[dpos+1];
907 GLubyte ax1 = texture_data[posxp1+1];
908 GLubyte ay1 = texture_data[posyp1+1];
917 map[mpos+0] = (128+(cx1-c)/2);
918 map[mpos+1] = (128+(cy1-c)/2);
919 map[mpos+2] = 127+int(brightness*128); // 255-c/2;
926 delete[] texture_data;
933 SGTexture::make_bumpmap(float brightness, float contrast) {
934 make_grayscale(contrast);
936 int colors = (num_colors == 1) ? 1 : 2;
937 GLubyte *map = new GLubyte[ texture_width * texture_height * colors ];
939 for (int y=0; y<texture_height; y++)
940 for (int x=0; x<texture_width; x++)
942 int mpos = (x + y*texture_width)*colors;
943 int dpos = (x + y*texture_width)*num_colors;
945 int xp1 = (x < (texture_width-1)) ? x+1 : 0;
946 int yp1 = (y < (texture_height-1)) ? y+1 : 0;
947 int posxp1 = (xp1 + y*texture_width)*num_colors;
948 int posyp1 = (x + yp1*texture_width)*num_colors;
950 map[mpos] = (127 - ((texture_data[dpos]-texture_data[posxp1]) -
951 ((texture_data[dpos]-texture_data[posyp1]))/4))/2;
953 map[mpos+1] = texture_data[dpos+1];
956 delete[] texture_data;