bool Image32::load_pixels_from_file_32(const char *path) { // free texture free_texture(); // generate image ILuint image_id = 0; ilGenImages(1, &image_id); ilBindImage(image_id); //Load image bool loaded_image = ilLoadImage( path ); if (loaded_image) { // convert image bool converted = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); if (converted == IL_TRUE) { // init dimensions GLuint image_width = (GLuint)ilGetInteger(IL_IMAGE_WIDTH); GLuint image_height = (GLuint)ilGetInteger(IL_IMAGE_HEIGHT); GLuint texture_width = power_of_two(image_width); GLuint texture_height = power_of_two(image_height); if (image_width != texture_width || image_height != texture_height) { // place image in upper left iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT); // resize image iluEnlargeCanvas((int)texture_width, (int)texture_height, 1); } // allocate memory for texture GLuint size = texture_height*texture_width; _pixels_32 = new GLuint[size]; _image_width = image_width; _image_height = image_height; _texture_width = texture_width; _texture_height = texture_height; memcpy(_pixels_32, ilGetData(), size*4); } ilDeleteImages(1,&image_id); } else { std::cout << "unable to load pixels from file: " << path << std::endl; return false; } _pixel_format = GL_RGBA; return true; }
bool LTexture::loadTextureFromFile32( std::string path ) { //Texture loading success bool textureLoaded = false; //Generate and set current image ID ILuint imgID = 0; ilGenImages( 1, &imgID ); ilBindImage( imgID ); //Load image ILboolean success = ilLoadImage( path.c_str() ); //Image loaded successfully if( success == IL_TRUE ) { //Convert image to RGBA success = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE ); if( success == IL_TRUE ) { //Initialize dimensions GLuint imgWidth = (GLuint)ilGetInteger( IL_IMAGE_WIDTH ); GLuint imgHeight = (GLuint)ilGetInteger( IL_IMAGE_HEIGHT ); //Calculate required texture dimensions GLuint texWidth = powerOfTwo( imgWidth ); GLuint texHeight = powerOfTwo( imgHeight ); //Texture is the wrong size if( imgWidth != texWidth || imgHeight != texHeight ) { //Place image at upper left iluImageParameter( ILU_PLACEMENT, ILU_UPPER_LEFT ); //Resize image iluEnlargeCanvas( (int)texWidth, (int)texHeight, 1 ); } //Create texture from file pixels textureLoaded = loadTextureFromPixels32( (GLuint*)ilGetData(), imgWidth, imgHeight, texWidth, texHeight ); } //Delete file from memory ilDeleteImages( 1, &imgID ); //Set pixel format mPixelFormat = GL_RGBA; } //Report error if( !textureLoaded ) { printf( "Unable to load %s\n", path.c_str() ); } return textureLoaded; }
bool Image32::load_texture_from_file_32(const char *path) { // generate image ILuint image_id = 0; ilGenImages(1, &image_id); ilBindImage(image_id); //Load image bool got = ilLoadImage( path ); if (got == IL_TRUE) { // convert image bool converted = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); if (converted == IL_TRUE) { // init dimensions GLuint image_width = (GLuint)ilGetInteger(IL_IMAGE_WIDTH); GLuint image_height = (GLuint)ilGetInteger(IL_IMAGE_HEIGHT); GLuint texture_width = power_of_two(image_width); GLuint texture_height = power_of_two(image_height); if (image_width != texture_width || image_height != texture_height) { // place image in upper left iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT); // resize image iluEnlargeCanvas((int)texture_width, (int)texture_height, 1); } bool loaded = load_texture_from_pixels_32((GLuint*)ilGetData(), image_width, image_height, texture_width, texture_height); if (!loaded) { std::cout << "unable to load image: " << path << std::endl; return false; } } ilDeleteImages(1,&image_id); } _pixel_format = GL_RGBA; return true; }
std::tr1::shared_ptr<ATexture> ATextureLoader::LoadFile(const std::string& path) { // try to retrieve the texture from the cache, if it exists std::tr1::shared_ptr<ATexture> texture_sp = ATextureCache::GetInstance()->Get(path); if(texture_sp != nullptr){ return texture_sp; } unsigned int width, contentWidth, height, contentHeight; ILuint imageID = ilGenImage(); ilBindImage(imageID); if(!ilLoadImage(path.c_str())){ std::string error = "Fail to load file: " + path; throw std::exception(error.c_str()); return nullptr; } // The content in width = contentWidth = ilGetInteger(IL_IMAGE_WIDTH); height = contentHeight = ilGetInteger(IL_IMAGE_HEIGHT); ILint bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); // Actual texture size padded with extra pixels, ensure width and height are power of two. if(!isPowerOfTwo(contentWidth)) width = nextPowerOfTwo(contentWidth); if(!isPowerOfTwo(contentHeight)) height = nextPowerOfTwo(contentHeight); // default background colour will be solid black ilClearColour(0.0f, 0.0f, 0.0f, 1.0f); // TODO: there is still some confusion here....... // flip texture problem is mentioned here: http://www.gamedev.net/topic/308200-devil-textures-upside-down/ // Together with the ilOriginFunc in the graphics_engine.h initialize function, and followed by iLuFlipImage(). // They ensure the image data will be correctly loaded and place on top left corner. And the data will be always stored from top left corner. iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT); // bitmap image seems like storing data upside down, its origin is on the lower left. // jpg, png data seems like using upper left as the origin. if (ilGetInteger(IL_IMAGE_ORIGIN) == IL_ORIGIN_UPPER_LEFT){ // This is for fixing the loaded image upside down in OpenGL... iluFlipImage(); } // set the canvas size. iluEnlargeCanvas(width, height, bpp); // Allocate the memory for the image data. GLubyte* buffer = new GLubyte[width * height * bpp]; // Copy the loaded image data into the texture data depending on how many bytes per pixel if(bpp == 4){ ilCopyPixels(0, 0, 0, width, height, 1, IL_RGBA, GL_UNSIGNED_BYTE, buffer); } else if(bpp == 3){ ilCopyPixels(0, 0, 0, width, height, 1, IL_RGB, GL_UNSIGNED_BYTE, buffer); } else{ std::string error = "Loading process, byte per pixel error, bpp: "+bpp; throw std::exception(error.c_str()); } // Delete the devIL image data ilDeleteImage(imageID); // create a brand new texture to use // put the texture into the texture cache. texture_sp = ATextureCache::GetInstance()->Cache(path, new ATexture(buffer, contentWidth, contentHeight, width, height, GL_RGBA, bpp)); // after texture is created, the buffer data will be uploaded to OpenGL, so no long needed. delete[] buffer; // This is a pointer to the loaded image data return texture_sp; }
bool LTexture::loadPixelsFromFile32( std::string path ) { //Free texture data if needed freeTexture(); //Texture loading success bool pixelsLoaded = false; //Generate and set current image ID ILuint imgID = 0; ilGenImages( 1, &imgID ); ilBindImage( imgID ); //Load image ILboolean success = ilLoadImage( path.c_str() ); //Image loaded successfully if( success == IL_TRUE ) { //Convert image to RGBA success = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE ); if( success == IL_TRUE ) { //Initialize dimensions GLuint imgWidth = (GLuint)ilGetInteger( IL_IMAGE_WIDTH ); GLuint imgHeight = (GLuint)ilGetInteger( IL_IMAGE_HEIGHT ); //Calculate required texture dimensions GLuint texWidth = powerOfTwo( imgWidth ); GLuint texHeight = powerOfTwo( imgHeight ); //Texture is the wrong size if( imgWidth != texWidth || imgHeight != texHeight ) { //Place image at upper left iluImageParameter( ILU_PLACEMENT, ILU_UPPER_LEFT ); //Resize image iluEnlargeCanvas( (int)texWidth, (int)texHeight, 1 ); } //Allocate memory for texture data GLuint size = texWidth * texHeight; mPixels32 = new GLuint[ size ]; //Get image dimensions mImageWidth = imgWidth; mImageHeight = imgHeight; mTextureWidth = texWidth; mTextureHeight = texHeight; //Copy pixels memcpy( mPixels32, ilGetData(), size * 4 ); pixelsLoaded = true; } //Delete file from memory ilDeleteImages( 1, &imgID ); //Set pixel format mPixelFormat = GL_RGBA; } //Report error if( !pixelsLoaded ) { printf( "Unable to load %s\n", path.c_str() ); } return pixelsLoaded; }
bool CCLUDrawBase::SaveScreen2BMP(const char *pcFilename) { ILuint uImgID; int iWidth, iHeight; unsigned char *pImage; cStr csFilename, csBaseName, csExt; int iExtPos; bool bEPSFile = false; bool bJPS = false; csBaseName = csFilename = pcFilename; iExtPos = ('.' < csBaseName); csExt = csBaseName.Last(csFilename.Len() - iExtPos, 1); csBaseName |= -1; if (csExt == "eps") { csFilename = csBaseName + ".tmp"; bEPSFile = true; } else if (csExt == "jps") { csFilename = csBaseName + ".jpg"; bEPSFile = true; bJPS = true; } iWidth = m_iSizeX; iHeight = m_iSizeY; ilGenImages(1, &uImgID); ilBindImage(uImgID); if (iluEnlargeCanvas(iWidth, iHeight, 1) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); pImage = (unsigned char *) ilGetData(); // Get Pixels glPixelStorei(GL_PACK_ALIGNMENT,1); glReadBuffer(GL_FRONT_LEFT); glReadPixels( 0, 0, iWidth, iHeight, GL_RGB, GL_UNSIGNED_BYTE, pImage ); remove(csFilename.Str()); if (bEPSFile) { if (ilSave(IL_JPG, (ILstring) csFilename.Str()) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } } else { if (ilSaveImage((ILstring) csFilename.Str()) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } } ilDeleteImages(1, &uImgID); if (bEPSFile) { csBaseName += ".eps"; if (!ConvertJPEGtoPS(csFilename.Str(), csBaseName.Str())) return false; if (!bJPS) remove(csFilename.Str()); } return true; }
//Load---------------------------------------------------------------------- bool TextureItem::Load( GLuint minFilter, GLuint maxFilter, bool forceMipmap, bool resizeIfNeeded ) { /** NOTE: This is a test of using an archive file. This will need to be modified to allow direct file access, or archived file access. */ ArchiveFile* file = ArchiveManager::GetSingleton().CreateArchiveFile( mImageFileName ); if ( file ) { UInt32 fileSize = file->Length(); unsigned char* buf = new unsigned char [ fileSize ]; if ( !buf ) { delete file; return false; } file->Read( buf, fileSize ); // Load the texture: //**** ilInit(); iluInit(); // Make sure the DevIL version is valid: if ( ilGetInteger( IL_VERSION_NUM ) < IL_VERSION || iluGetInteger( ILU_VERSION_NUM ) < ILU_VERSION ) { // Invalid version... delete file; delete buf; return false; } // Get the decompressed data ILuint imageID; ilGenImages( 1, &imageID ); ilBindImage( imageID ); //if ( ilLoadImage( const_cast< char* >( mImageFileName.c_str() ) ) ) if ( ilLoadL( IL_TYPE_UNKNOWN, buf, fileSize ) ) { // Convert the image to unsigned bytes: ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE ); // Generate the GL texture glGenTextures( 1, &mTextureID ); glBindTexture( GL_TEXTURE_2D, mTextureID ); mWidth = ilGetInteger( IL_IMAGE_WIDTH ); mHeight = ilGetInteger( IL_IMAGE_HEIGHT ); mOriginalWidth = mWidth; mOriginalHeight = mHeight; // OpenGL will work better with textures that have dimensions // that are a power of 2. If doing a scrolling tile map, then // this is pretty much a necessity. However, there are times // when using a mipmap instead is perfectly fine (ie, when NOT // doing tiles, or in cases where we might be running out of // video memory... if ( resizeIfNeeded && !forceMipmap ) { UInt32 newWidth = mWidth, newHeight = mHeight; if ( !Math::IsPowerOf2( mWidth ) ) { // Find the next power of 2: newWidth = Math::FindNextPowerOf2( mWidth ); } if ( !Math::IsPowerOf2( mHeight ) ) { // Find the next power of 2: newHeight = Math::FindNextPowerOf2( mHeight ); } if ( newWidth != mWidth || newHeight != mHeight ) { // Resize the canvas: ilClearColor( 0, 0, 0, 0 ); iluImageParameter( ILU_PLACEMENT, ILU_UPPER_LEFT ); iluEnlargeCanvas( newWidth, newHeight, ilGetInteger( IL_IMAGE_DEPTH ) ); mWidth = ilGetInteger( IL_IMAGE_WIDTH ); mHeight = ilGetInteger( IL_IMAGE_HEIGHT ); } } // If forcing mipmap generation, or if the size is not a power of 2, // generate as mipmaps if ( forceMipmap || !Math::IsPowerOf2( mWidth ) || !Math::IsPowerOf2( mHeight ) ) { gluBuild2DMipmaps( GL_TEXTURE_2D, ilGetInteger( IL_IMAGE_BPP ), mWidth, mHeight, ilGetInteger( IL_IMAGE_FORMAT ), GL_UNSIGNED_BYTE, ilGetData() ); } else { glTexImage2D( GL_TEXTURE_2D, 0, ilGetInteger( IL_IMAGE_BPP ), mWidth, mHeight, 0, ilGetInteger( IL_IMAGE_FORMAT ), GL_UNSIGNED_BYTE, ilGetData() ); } // Set the minification and magnification filters glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); mIsLoaded = true; } else { ILenum error; error = ilGetError(); //std::string errString = iluErrorString( error ); } ilDeleteImages( 1, &imageID ); //*** // Free memory: delete buf; delete file; } else return false; return true; }
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; }