Texture.C

Go to the documentation of this file.
00001 /*!@file GUI/Texture.C test opengl viewport */
00002 
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00004 // by the University of Southern California (USC) and the iLab at USC.  //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00032 // Originally obtained from ode
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/Texture.C $
00034 // $Id: Texture.C 12951 2010-03-05 19:06:12Z lior $
00035 
00036 #include "GUI/Texture.H"
00037 
00038 Texture::Texture (const char *filename)
00039 {
00040   image = new VPImage (filename);
00041   itsInitialized = false;
00042   if (image->itsInitialized)
00043   {
00044     glGenTextures (1,&name);
00045     glBindTexture (GL_TEXTURE_2D,name);
00046 
00047     // set pixel unpacking mode
00048     glPixelStorei (GL_UNPACK_SWAP_BYTES, 0);
00049     glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
00050     glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00051     glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
00052     glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
00053 
00054     // glTexImage2D (GL_TEXTURE_2D, 0, 3, image->width(), image->height(), 0,
00055     //                   GL_RGB, GL_UNSIGNED_BYTE, image->data());
00056 #ifdef INVT_HAVE_LIBGLUT
00057     gluBuild2DMipmaps (GL_TEXTURE_2D, 3, image->width(), image->height(),
00058         GL_RGB, GL_UNSIGNED_BYTE, image->data());
00059 #endif
00060 
00061     // set texture parameters - will these also be bound to the texture???
00062     glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00063     glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00064 
00065     glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00066     glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00067         GL_LINEAR_MIPMAP_LINEAR);
00068 
00069     glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
00070     itsInitialized = true;
00071   }
00072 }
00073 
00074 Texture::~Texture()
00075 {
00076   delete image;
00077   glDeleteTextures (1,&name);
00078 }
00079 
00080 
00081 void Texture::bind (int modulate)
00082 {
00083   glBindTexture (GL_TEXTURE_2D,name);
00084   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
00085              modulate ? GL_MODULATE : GL_DECAL);
00086 }
00087 
00088 VPImage::VPImage (const char *filename)
00089 {
00090   itsInitialized = false;
00091   FILE *f = fopen (filename,"rb");
00092   if (!f)
00093   {
00094     LINFO ("Can't open image file `%s'",filename);
00095     return;
00096   }
00097 
00098   // read in header
00099   if (fgetc(f) != 'P' || fgetc(f) != '6')
00100   {
00101     LINFO ("image file \"%s\" is not a binary PPM (no P6 header)",filename);
00102     return;
00103   }
00104   skipWhiteSpace (filename,f);
00105 
00106   // read in image parameters
00107   image_width = readNumber (filename,f);
00108   skipWhiteSpace (filename,f);
00109   image_height = readNumber (filename,f);
00110   skipWhiteSpace (filename,f);
00111   int max_value = readNumber (filename,f);
00112 
00113   // check values
00114   if (image_width < 1 || image_height < 1)
00115     LFATAL ("bad image file \"%s\"",filename);
00116   if (max_value != 255)
00117     LFATAL ("image file \"%s\" must have color range of 255",filename);
00118 
00119   // read either nothing, LF (10), or CR,LF (13,10)
00120   int c = fgetc(f);
00121   if (c == 10) {
00122     // LF
00123   }
00124   else if (c == 13) {
00125     // CR
00126     c = fgetc(f);
00127     if (c != 10) ungetc (c,f);
00128   }
00129   else ungetc (c,f);
00130 
00131   // read in rest of data
00132   image_data = new byte [image_width*image_height*3];
00133   if (fread (image_data,image_width*image_height*3,1,f) != 1)
00134     LFATAL ("Can not read data from image file `%s'",filename);
00135   fclose (f);
00136 
00137   itsInitialized = true;
00138 }
00139 
00140 
00141 VPImage::~VPImage()
00142 {
00143   delete[] image_data;
00144 }
00145 
00146 void VPImage::skipWhiteSpace (const char *filename, FILE *f)
00147 {
00148   int c,d;
00149   for(;;) {
00150     c = fgetc(f);
00151     if (c==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00152 
00153     // skip comments
00154     if (c == '#') {
00155       do {
00156         d = fgetc(f);
00157         if (d==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00158       } while (d != '\n');
00159       continue;
00160     }
00161 
00162     if (c > ' ') {
00163       ungetc (c,f);
00164       return;
00165     }
00166   }
00167 }
00168 
00169 
00170 // read a number from a stream, this return 0 if there is none (that's okay
00171 // because 0 is a bad value for all PPM numbers anyway).
00172 
00173 int VPImage::readNumber (const char *filename, FILE *f)
00174 {
00175   int c,n=0;
00176   for(;;) {
00177     c = fgetc(f);
00178     if (c==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00179     if (c >= '0' && c <= '9') n = n*10 + (c - '0');
00180     else {
00181       ungetc (c,f);
00182       return n;
00183     }
00184   }
00185 }
00186 
00187 
Generated on Sun May 8 08:04:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3