bool Texture::CacheIn()
      {
         fbyte *im;
         if (Bad())
         {
            return false;
         }
         if (!IsCachedIn())
         {
            ILuint image_id;
            ILint format;
            
            ilGenImages(1, &image_id);
            ilBindImage(image_id);

            if(!ilLoadImage((ILstring)m_impl->m_filename.c_str()))
            {
               ilDeleteImages(1, &image_id);
               m_impl->m_bad = true;
               cerr << "Failed to load texture file " << m_impl->m_filename << endl;
               return false;
            }

            format=ilGetInteger(IL_DXTC_DATA_FORMAT);
            switch(format)
            {
            case IL_DXT_NO_COMP:
               break;
            case IL_DXT1:
               m_impl->m_dxt_format = TextureImpl::e_dxt1;
               break;
            case IL_DXT2:
               m_impl->m_dxt_format = TextureImpl::e_dxt2;
               break;
            case IL_DXT3:
               m_impl->m_dxt_format = TextureImpl::e_dxt3;
               break;
            case IL_DXT4:
               m_impl->m_dxt_format = TextureImpl::e_dxt4;
               break;
            case IL_DXT5:
               m_impl->m_dxt_format = TextureImpl::e_dxt5;
               break;
            }
            if(m_impl->m_dxt_format!=TextureImpl::e_dxt_none)
            {
               m_impl->m_width=ilGetInteger(IL_IMAGE_WIDTH);
               m_impl->m_height=ilGetInteger(IL_IMAGE_HEIGHT);
               m_impl->m_widthP2 = m_impl->m_width;
               m_impl->m_heightP2 = m_impl->m_height;
               m_impl->m_type = Texture::TYPE_RGBA;
               ILint s;
               char *data;
               s = ilGetDXTCData(NULL,0,format);
               m_impl->m_dxt_size = s;
               im = new fbyte[s];
               ilGetDXTCData(im,s,format);
               BindTextureToOpenGL(m_impl, im);
               delete [] im;

            }
            else
            {
               format=ilGetInteger(IL_IMAGE_FORMAT);

               if(format == IL_RGBA || format == IL_BGRA || format == IL_LUMINANCE_ALPHA || m_impl->m_autoGenAlphaMask)
               {
                  ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
                  m_impl->m_type = Texture::TYPE_RGBA;
               }
               else
               {
                  ilConvertImage(IL_RGB,IL_UNSIGNED_BYTE);
                  m_impl->m_type = Texture::TYPE_RGB;
               }

               m_impl->m_width=ilGetInteger(IL_IMAGE_WIDTH);
               m_impl->m_height=ilGetInteger(IL_IMAGE_HEIGHT);
               m_impl->m_widthP2 = m_impl->m_width;
               m_impl->m_heightP2 = m_impl->m_height;

               if((m_impl->m_width  & (m_impl->m_width  - 1)) != 0 ||
                  (m_impl->m_height & (m_impl->m_height - 1)) != 0)
               {
                  for(fdword i=2;i<=c_max_texture_size_power;i++)
                  {
                     if((m_impl->m_width<<1) > (1UL<<i))
                     {
                        m_impl->m_widthP2 = (1UL<<i);
                     }
                     if((m_impl->m_height<<1) > (1UL<<i))
                     {
                        m_impl->m_heightP2 = (1UL<<i);
                     }
                  }

				  cerr << m_impl->m_filename << " has invalid texture size: " << 
                     m_impl->m_width << "x" << m_impl->m_height << 
                     "  resizing to " <<
                     m_impl->m_widthP2 << "x" << m_impl->m_heightP2 << endl;

                  cerr << "   Wasted space due to texture resize: " << 
                     (((m_impl->m_widthP2 * m_impl->m_heightP2) - 
                       (m_impl->m_width * m_impl->m_height)) / 
                       float(m_impl->m_widthP2 * m_impl->m_heightP2)) * 100.0f << 
                       "%" << endl;

                  iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT);
                  ilClearColour(1.0f,0.2f,0.8f,1.0f);

                  if(!iluEnlargeCanvas(m_impl->m_widthP2, m_impl->m_heightP2, ilGetInteger(IL_IMAGE_DEPTH)))
                  {
                     ilDeleteImages(1, &image_id);
                     m_impl->m_bad = true;
                     cerr << "Resize of texture canvas failed" << endl;
                     return false;
                  }
               }

               im = ilGetData();
               if(m_impl->m_autoGenAlphaMask)
               {
                  Colour c;
                  fdword i,j;
                  for(j=0;j<m_impl->m_height;j++)
                  {
                     for(i=0;i<m_impl->m_width;i++)
                     {
                        c.FromInteger(((fdword *)im)[j*m_impl->m_widthP2+i],
                           c_rgba_red_mask, c_rgba_red_shift,
                           c_rgba_green_mask, c_rgba_green_shift,
                           c_rgba_blue_mask, c_rgba_blue_shift,
                           c_rgba_alpha_mask, c_rgba_alpha_shift);
                        GF1::Colour cd(c - m_impl->m_autoGenAlphaMaskColour);
                        if(m_impl->m_autoGenAlphaMaskFade && m_impl->m_autoGenAlphaMaskTolerance > 0)
                        {
                           c.a = cd.Length() / m_impl->m_autoGenAlphaMaskTolerance;
                        }
                        else
                        {
                           if(cd.LengthSquared() <= m_impl->m_autoGenAlphaMaskTolerance * m_impl->m_autoGenAlphaMaskTolerance)
                           {
                              c.a=0.0f;
                           }
                           else
                           {
                              c.a=1.0f;
                           }
                        }
                        ((fdword *)im)[j*m_impl->m_widthP2+i] = c.ToInteger(255,0,255,8,255,16,255,24);
                     }
                  }
               }
               BindTextureToOpenGL(m_impl, im);
            }
            ilDeleteImages(1, &image_id);
         }
         return true;
      }