Exemple #1
0
	ILinfo Image::getInfo() const 
	{
		ilBindImage(_image);
		ILinfo info;
		iluGetImageInfo(&info);
		return info;
	}
Texture* DevILImageCodec::load(const RawDataContainer& data, Texture* result)
{
    ilPushAttrib(IL_ORIGIN_SET);
    ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
    ilEnable(IL_ORIGIN_SET);

    ILuint imgName;
    ilGenImages(1, &imgName);
    ilBindImage(imgName);

    if (IL_FALSE != ilLoadL(IL_TYPE_UNKNOWN,
                            static_cast<const void*>(data.getDataPtr()),
                            data.getSize()))
    {
        // get details about size of loaded image
        ILinfo imgInfo;
        memset(&imgInfo, 0, sizeof(ILinfo));
        iluGetImageInfo(&imgInfo);
        // set dimensions of texture
        size_t width = imgInfo.Width;
        size_t height = imgInfo.Height;
        // allocate temp buffer to receive image data
        uchar* tmpBuff = new uchar[width * height * 4];

        // get image data in required format
        Texture::PixelFormat cefmt;
        ILenum ilfmt;
        switch (imgInfo.Format)
        {
        case IL_RGBA:
        case IL_BGRA:
            ilfmt = IL_RGBA;
            cefmt = Texture::PF_RGBA;
            break;
        default:
            ilfmt = IL_RGB;
            cefmt = Texture::PF_RGB;
            break;
        };
        ilCopyPixels(0, 0, 0, width, height, 1, ilfmt, IL_UNSIGNED_BYTE,
                     static_cast<void*>(tmpBuff));

        // delete DevIL image
        ilDeleteImages(1, &imgName);
        ilPopAttrib();

        // create cegui texture
        CEGUI_TRY
        {
            result->loadFromMemory(tmpBuff, Size(width, height), cefmt);
        }
        CEGUI_CATCH(...)
        {
            delete [] tmpBuff;
            CEGUI_RETHROW;
        }
Exemple #3
0
void ofxTexture::SubmitChanges()
{
    if(m_Locked) return;
    ilBindImage(m_ImageId);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    {
        ILinfo info;
        iluGetImageInfo(&info);
        m_BytePerPixel = info.Bpp;
        m_Width = info.Width;
        m_Height = info.Height;
        m_UnitWidth = m_Width>0?1.0f/m_Width:0.0f;
        m_UnitHeight = m_Height>0?1.0f/m_Height:0.0f;
    }
    {
        GLenum format = m_BytePerPixel==3?GL_RGB:GL_RGBA;
        ILubyte* pixel_data = ilGetData();
        if(m_Compressed)
        {
            ILuint compressed_size;
            ILubyte* compressed_data;
            compressed_size = ilGetDXTCData(NULL, 0, IL_DXT3);
            compressed_data = new ILubyte[compressed_size];
            ilGetDXTCData(compressed_data, compressed_size, IL_DXT3);
            glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
                                      m_Width, m_Height, 0, compressed_size, compressed_data);
            delete[] compressed_data;
#ifdef _DEBUG
            ILuint uncompressed_size = m_Width*m_Height*m_BytePerPixel;
            float ratio = (float)compressed_size/uncompressed_size*100;
            ofLogNotice() <<"compressed texture, m_TextureId = "<<m_TextureId
                          <<endl<<"before "<<uncompressed_size/1024.0<<" Kbytes, after "<<compressed_size/1024.0<<" Kbytes, ratio = "<<ratio<<"%";
#endif
        }
        else
        {
            glTexImage2D(GL_TEXTURE_2D, 0, format, m_Width, m_Height, 0, format, GL_UNSIGNED_BYTE, pixel_data);
        }
    }
    {
        GLint param = GL_CLAMP_TO_EDGE;
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,param);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,param);
    }
    {
        GLint param = GL_NEAREST;
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
        if(param == GL_LINEAR_MIPMAP_LINEAR || GL_LINEAR_MIPMAP_NEAREST)
        {
            glGenerateMipmap(GL_TEXTURE_2D);
        }
    }
}
Exemple #4
0
// Function load a image, turn it into a texture, and return the texture ID as a GLuint for use
GLuint ModelObject::loadImageTex(const char* theFileName) {
    ILuint      imageID;                        // Create an image ID as a ULuint
    ILboolean   success;                        // Create a flag to keep track of success/failure
    ILenum      error;				// Create a flag to keep track of the IL error state
    ilGenImages(1, &imageID);                   // Generate the image ID
    ilBindImage(imageID); 			// Bind the image
    success = ilLoadImage(theFileName);         // Load the image file
    // If we managed to load the image, then we can start to do things with it...
    if (success) {
        // If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
        ILinfo ImageInfo;
        iluGetImageInfo(&ImageInfo);
        if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT) {
            iluFlipImage();
        }
        // Convert the image into a suitable format to work with
        // NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA
        success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
        // Quit out if we failed the conversion
        if (!success) {
            error = ilGetError();
            std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
            exit(-1);
        }
        // Generate a new texture
        glGenTextures(1, &textureID);
        // Bind the texture to a name
        glBindTexture(GL_TEXTURE_2D, textureID);
        // Set texture clamping method
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        // Set texture interpolation method to use linear interpolation (no MIPMAPS)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        // Specify the texture specification
        glTexImage2D(GL_TEXTURE_2D, 				// Type of texture
                     0,				// Pyramid level (for mip-mapping) - 0 is the top level
                     ilGetInteger(IL_IMAGE_BPP),	// Image colour depth
                     ilGetInteger(IL_IMAGE_WIDTH),	// Image width
                     ilGetInteger(IL_IMAGE_HEIGHT),	// Image height
                     0,				// Border width in pixels (can either be 1 or 0)
                     ilGetInteger(IL_IMAGE_FORMAT),	// Image format (i.e. RGB, RGBA, BGR etc.)
                     GL_UNSIGNED_BYTE,		// Image data type
                     ilGetData());			// The actual image data itself
        //glGenerateMipmap(GL_TEXTURE_2D); // Note: This requires OpenGL 3.0 or higher
    } else { // If we failed to open the image file in the first place...
        error = ilGetError();
        std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
        exit(-1);
    }

    ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image.
    std::cout << "Texture creation successful." << std::endl;
    return textureID; // Return the GLuint to the texture so you can use it!
}
ILenum ilImage::GetOrigin(void)
{
    ILinfo Info;

	if (this->Id) {
		this->Bind();
        iluGetImageInfo(&Info);
		return Info.Origin;
	}
	return 0;
}
	Texture TextureHandler::getTexture(const string &path, const string &def, GLenum format)
	{
		if(hasTexture(path))
			return existingTextures[path];

		Texture ret(path);
		ILuint imageID;
		ILboolean success;
		ILenum error;
		ilGenImages(1, &imageID);
		ilBindImage(imageID);
		success = ilLoadImage((const ILstring)path.c_str());

		if(!success)
		{
			error = ilGetError();
			std::cout << "Image load failed " + path + " - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
			success = ilLoadImage((const ILstring)def.c_str());
			if(!success)
			{
				error = ilGetError();
				std::cout << "Image load failed " + def + " - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
				exit(-1);
			}
		}

		ILinfo ImageInfo;
		iluGetImageInfo(&ImageInfo);
		if(ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
			iluFlipImage();

		GLenum type;
		success = convertAndGetType(format, type);

		if(!success)
		{
			error = ilGetError();
			cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << endl;
			exit(-1);
		}
		ret.generate();
		ret.bind();
		ret.texImage(0, format, vec3(ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0), ilGetInteger(IL_IMAGE_FORMAT), type, ilGetData());

		ilDeleteImages(1, &imageID);

		existingTextures[path] = ret;
		return ret;
	}
Exemple #7
0
int image_load(lua_State *L, const char *path, const char *name) {
    ILuint imageID;
    ilGenImages(1, &imageID);
    ilBindImage(imageID);
 
    if (!ilLoadImage(path)) {
        ilDeleteImages(1, &imageID);
        return luaL_error(L, "loading %s failed: %s",
            path, iluErrorString(ilGetError()));
    }
 
    ILinfo ImageInfo;
    iluGetImageInfo(&ImageInfo);

    if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
        iluFlipImage();
 
    if (!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE)) {
        ilDeleteImages(1, &imageID);
        return luaL_error(L, "converting %s failed: %s",
            path, iluErrorString(ilGetError()));
    }

    int width = ilGetInteger(IL_IMAGE_WIDTH);
    int height = ilGetInteger(IL_IMAGE_HEIGHT);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), width, height, 0,
                 ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
    glGenerateMipmap(GL_TEXTURE_2D);
 
    ilDeleteImages(1, &imageID);
 
    image_t *image = push_image(L);
    image->tex = tex;
    image->fbo = 0;
    image->width = width;
    image->height = height;
    return 1;
}
Exemple #8
0
GLuint CGLTexture::LoadGL(string f) {
    glBmap=0;
    filename=f;
	ILuint imageID;
	ILboolean success;
	ILenum error;
	ilGenImages(1, &imageID);
	ilBindImage(imageID);
	success = ilLoadImage(filename.c_str());
	if (success) {
		ILinfo ImageInfo;
		iluGetImageInfo(&ImageInfo);
		if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT) iluFlipImage();
		success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
		if (!success){
			error = ilGetError();
			pLog->_Add("  CGLTexture::LoadGL() Image conversion failed - IL reports error: %d %s",error,iluErrorString(error));
			return 0;
        }
        width=ilGetInteger(IL_IMAGE_WIDTH);
        height=ilGetInteger(IL_IMAGE_HEIGHT);
        format=ilGetInteger(IL_IMAGE_FORMAT);
        bpp=ilGetInteger(IL_IMAGE_BPP)*8;
        glEnable(GL_TEXTURE_2D);
		glGenTextures(1, &glBmap);
		glBindTexture(GL_TEXTURE_2D, glBmap);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(   GL_TEXTURE_2D,  0,
                        ilGetInteger(IL_IMAGE_BPP),
                        ilGetInteger(IL_IMAGE_WIDTH),
                        ilGetInteger(IL_IMAGE_HEIGHT),	 0,
                        ilGetInteger(IL_IMAGE_FORMAT),
                        GL_UNSIGNED_BYTE,
                        ilGetData());
 	}
  	else {
		error = ilGetError();
		pLog->_Add("  CGLTexture::LoadGL() Image load failed - IL reports error: %d %s",error,iluErrorString(error));
		return 0;
  	}
 	ilDeleteImages(1, &imageID);
    return glBmap;
}
Exemple #9
0
GLuint Renderer::loadTexture(const std::string &fname)
{
	ILuint imageID;
	GLuint textureID;
	ILboolean success;
	ILenum error;
	ilGenImages(1, &imageID); 
	ilBindImage(imageID); 
	success = ilLoadImage(fname.c_str());
	if (success)
	{
		ILinfo ImageInfo;
		iluGetImageInfo(&ImageInfo);
		if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
		{
			iluFlipImage();
		}
	
		success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

		if (!success)
		{
			error = ilGetError();
			return 0;
		}

		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
	}
	else
	{
		error = ilGetError();
		return 0;
	}

	ilDeleteImages(1, &imageID);
	return textureID;
}
void FractureBox::LoadFractureMap(const char *filename, bool bolden)
{
    if(mFracTexID)
        ilDeleteImage(mFracTexID);
    if(mFracTexScaleID)
        ilDeleteImage(mFracTexScaleID);
    mFracTexID = ilGenImage();
    ilBindImage(mFracTexID);

    if(!ilLoadImage(filename))
    {
        std::cout << "Error loading " << filename << std::endl;
        return;
    }
    if(!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
    {
        std::cout << "Error converting image " << filename << std::endl;
        return;
    }

    ILinfo imgInfo;
    iluGetImageInfo(&imgInfo);
    //if(imgInfo.Origin == IL_ORIGIN_UPPER_LEFT)
    if(imgInfo.Origin == IL_ORIGIN_LOWER_LEFT)
        iluFlipImage();
    if(bolden)
        boldenLines();

    //Now create the scaled version of the image
    mFracTexScaleID = ilGenImage();
    ilBindImage(mFracTexScaleID);
    ilCopyImage(mFracTexID);
    //Scale the image to fit our box
    const int wid = w() - 2;
    const int hei = h() - 2;
    iluImageParameter(ILU_FILTER, ILU_SCALE_MITCHELL);
    iluScale(wid, hei, imgInfo.Depth);

    mFracImage = RGBImagePtr(new Fl_RGB_Image(ilGetData(), wid, hei));
    image(mFracImage.get());
    redraw();
    ilBindImage(0);
}
void FractureBox::resizeFractureTexture()
{
    ASSERT(mFracTexID);
    if(mFracTexScaleID)
        ilDeleteImage(mFracTexScaleID);
    mFracTexScaleID = ilGenImage();
    ilBindImage(mFracTexScaleID);
    ilCopyImage(mFracTexID);

    ILinfo imgInfo;
    iluGetImageInfo(&imgInfo);
    //Scale the image to fit our box
    const int wid = w() - 2;
    const int hei = h() - 2;
    iluScale(wid, hei, imgInfo.Depth);

    mFracImage = RGBImagePtr(new Fl_RGB_Image(ilGetData(), wid, hei));
    image(mFracImage.get());
    redraw();
    ilBindImage(0);
}
		//---------------------------------------------------------------------
		void LoadTexture(	const std::string& _filename,
							Texture2D& _texture,
							bool _srgb,
							bool _allocateMipmap,
							bool _verbose)
		{
			try
			{
				ilInit();
				
				ILuint imgH;
				ilGenImages(1, &imgH);
				ilBindImage(imgH);
				if(!ilLoadImage((const ILstring)_filename.c_str()))
				{
					Error("Load image error : file does not exist (%s)",_filename.c_str());
				}
				
				if(_verbose)
				{
					Info("Load image : %s",_filename.c_str());
				}

				// Convert all to RGBA. TODO Need improvement ...
				GLenum format;
				bool convert = false;
				ILenum target;
				switch(ilGetInteger(IL_IMAGE_FORMAT))
				{
					case IL_RGB  			: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_RGBA 			: format = GL_RGBA; break;
					case IL_BGR  			: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_BGRA 			: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_LUMINANCE 		: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_COLOUR_INDEX	: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_ALPHA			: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					case IL_LUMINANCE_ALPHA	: format = GL_RGBA; convert=true; target = IL_RGBA; break;
					default 				: Error("Load image error : unsupported format (%s)",_filename.c_str());
				}

				GLenum type;
				switch(ilGetInteger(IL_IMAGE_TYPE))
				{
					case IL_UNSIGNED_BYTE	: type = GL_UNSIGNED_BYTE; break;
					case IL_FLOAT			: type = GL_FLOAT; break;
					//case IL_BYTE			: type = GL_BYTE; break;
					//case IL_SHORT			: type = GL_SHORT; break;
					//case IL_UNSIGNED_SHORT: type = GL_UNSIGNED_SHORT; break;
					//case IL_INT			: type = GL_INTEGER; break;
					//case IL_UNSIGNED_INT	: type = GL_UNSIGNED_INT; break;
					//case IL_DOUBLE		: Error("Load image error : double data are not supported (%s)",_filename.c_str()); break;
					default 				: Error("Load image error : unsupported data (%s)",_filename.c_str());
				}

				if(convert)
					 ilConvertImage(target, ilGetInteger(IL_IMAGE_TYPE));

				// Flip image
				ILinfo ImageInfo;
				iluGetImageInfo(&ImageInfo);
				if( ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT )
				{
					iluFlipImage();
					if(_verbose) Info("Flip image");
				}

				switch(type)
				{
					case GL_UNSIGNED_BYTE	:
					{
						if(_srgb)
						{
							_texture.Allocate( GL_SRGB8_ALPHA8, ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT),_allocateMipmap);
							if(_verbose) Info("Allocate texture - format:GL_SRGB8_ALPHA8, w:%d, h:%d, mipmap:%s",ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT), (_allocateMipmap?"TRUE":"FALSE") );
						}
						else
						{
							_texture.Allocate( GL_RGBA8, ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT),_allocateMipmap);
							if(_verbose) Info("Allocate texture - format:GL_RGBA8, w:%d, h:%d, mipmap:%s",ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT), (_allocateMipmap?"TRUE":"FALSE") );
						}
					}
					break;
					case GL_FLOAT			:
					{
						if(_srgb)
							Warning("Try to convert to SRGB, but texture format is not compatible");
						_texture.Allocate( GL_RGBA32F, ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT),_allocateMipmap);
						if(_verbose) Info("Allocate texture - format:GL_RGBA32F, w:%d, h:%d, mipmap:%s",ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT), (_allocateMipmap?"TRUE":"FALSE") );
					}
					break;
					default 				:
					{
						Error("Load image error : unsupported data (%s)",_filename.c_str());
					}
				}
				_texture.Fill(format,type,ilGetData());

				ilDeleteImages(1, &imgH);
				ilShutDown();
			}
			catch (const std::exception &e) 
			{
				Error("Unable to read image file \"%s\": %s",_filename.c_str(),e.what());
			}
		}
Texture* DevILImageCodec::load(const RawDataContainer& data, Texture* result)
{
    ilPushAttrib(IL_ORIGIN_SET);
    ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
    ilEnable(IL_ORIGIN_SET);

    ILuint imgName;
    ilGenImages(1, &imgName);
    ilBindImage(imgName);

    if (ilLoadL(IL_TYPE_UNKNOWN, (ILvoid*)data.getDataPtr(), data.getSize()) != IL_FALSE)
    {
        // get details about size of loaded image
        ILinfo imgInfo;
        memset(&imgInfo, 0, sizeof(ILinfo));
        iluGetImageInfo(&imgInfo);
        // set dimensions of texture
        size_t width = imgInfo.Width;
        size_t height = imgInfo.Height;
        // allocate temp buffer to receive image data
        uchar* tmpBuff = new uchar[width * height * 4];

        // get image data in required format
        Texture::PixelFormat cefmt;
        ILenum ilfmt;
        switch (imgInfo.Format)
        {
        case IL_RGBA:
        case IL_BGRA:
            ilfmt = IL_RGBA;
            cefmt = Texture::PF_RGBA;
            break;
        default:
            ilfmt = IL_RGB;
            cefmt = Texture::PF_RGB;
            break;
        };
        ilCopyPixels(0, 0, 0, width, height, 1, ilfmt, IL_UNSIGNED_BYTE, (ILvoid*)tmpBuff);

        // delete DevIL image
        ilDeleteImages(1, &imgName);
        ilPopAttrib();

        // create cegui texture
        try
        {
            result->loadFromMemory(tmpBuff, width, height, cefmt);
        }
        catch(...)
        {
            delete [] tmpBuff;
            throw;
        }

        // free temp buffer
        delete [] tmpBuff;

        return result;
    }
	// failed to load image properly.
	else
	{
		// delete DevIL image
		ilDeleteImages(1, &imgName);
		ilPopAttrib();
        return 0;
    }
}
Containers::Optional<ImageData2D> DevIlImageImporter::doImage2D(UnsignedInt) {
    ILuint imgID = 0;
    ilGenImages(1, &imgID);
    ilBindImage(imgID);

    /* So we can use the shorter if(!ilFoo()) */
    static_assert(!IL_FALSE, "IL_FALSE doesn't have a zero value");

    if(!ilLoadL(IL_TYPE_UNKNOWN, _in, _in.size())) {
        /* iluGetString() returns empty string for 0x512, which is even more
           useless than just returning the error ID */
        Error() << "Trade::DevIlImageImporter::image2D(): cannot open the image:" << ilGetError();
        return Containers::NullOpt;
    }

    const Vector2i size{ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT)};

    Int components;
    bool rgbaNeeded = false;
    PixelFormat format;
    switch(ilGetInteger(IL_IMAGE_FORMAT)) {
        /* Grayscale */
        case IL_LUMINANCE:
            format = PixelFormat::R8Unorm;
            components = 1;
            break;

        /* Grayscale + alpha */
        case IL_LUMINANCE_ALPHA:
            format = PixelFormat::RG8Unorm;
            components = 2;
            break;

        /* BGR */
        case IL_BGR:
            rgbaNeeded = true;
            format = PixelFormat::RGB8Unorm;
            components = 3;
            break;

        /* BGRA */
        case IL_BGRA:
            rgbaNeeded = true;
            format = PixelFormat::RGBA8Unorm;
            components = 4;
            break;

        /* RGB */
        case IL_RGB:
            format = PixelFormat::RGB8Unorm;
            components = 3;
            break;

        /* RGBA */
        case IL_RGBA:
            format = PixelFormat::RGBA8Unorm;
            components = 4;
            break;

        /* No idea, convert to RGBA */
        default:
            format = PixelFormat::RGBA8Unorm;
            components = 4;
            rgbaNeeded = true;
    }

    /* If the format isn't one we recognize, convert to RGB(A) */
    if(rgbaNeeded && !ilConvertImage(components == 3 ? IL_RGB : IL_RGBA, IL_UNSIGNED_BYTE)) {
        /* iluGetString() returns empty string for 0x512, which is even
           more useless than just returning the error ID */
        Error() << "Trade::DevIlImageImporter::image2D(): cannot convert image:" << ilGetError();
        return Containers::NullOpt;
    }

    /* Flip the image to match OpenGL's conventions */
    /** @todo use our own routine to avoid linking to ILU */
    ILinfo ImageInfo;
    iluGetImageInfo(&ImageInfo);
    if(ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
        iluFlipImage();

    /* Copy the data into array that is owned by us and not by IL */
    Containers::Array<char> imageData{std::size_t(size.product()*components)};
    std::copy_n(reinterpret_cast<char*>(ilGetData()), imageData.size(), imageData.begin());

    /* Release the texture back to DevIL */
    ilDeleteImages(1, &imgID);

    /* Adjust pixel storage if row size is not four byte aligned */
    PixelStorage storage;
    if((size.x()*components)%4 != 0)
        storage.setAlignment(1);

    return Trade::ImageData2D{storage, format, size, std::move(imageData)};
}
Exemple #15
0
bool Texture::load()
{
    if (mLoaded) {
        return true;
    }

    ILuint imageID;
    ILboolean success;
    ILenum error;

    ilGenImages(1, &imageID);
    ilBindImage(imageID);

    LOG_DEBUG("Trying to load texture: '%s'", mFilename.c_str());

    success = ilLoadImage(mFilename.c_str());

    if (success)
    {
        ILinfo ImageInfo;

        iluGetImageInfo(&ImageInfo);

        success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

        if (!success)
        {
            error = ilGetError();
            LOG_ERROR("Rendering: Image conversion failed: '%s'", iluErrorString(error));
        }

        mWidth = ilGetInteger(IL_IMAGE_WIDTH);
        mHeight = ilGetInteger(IL_IMAGE_HEIGHT);

        glGenTextures(1, &mTexture);

        glBindTexture(GL_TEXTURE_2D, mTexture);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        glTexImage2D(
            GL_TEXTURE_2D,
            0,
            ilGetInteger(IL_IMAGE_FORMAT),
            mWidth,
            mHeight,
            0,
            ilGetInteger(IL_IMAGE_FORMAT),
            GL_UNSIGNED_BYTE,
            ilGetData()
        );
    }
    else
    {
        error = ilGetError();
        LOG_ERROR("Rendering: Image loading failed: '%s'", iluErrorString(error));
    }

    ilDeleteImages(1, &imageID);

    mLoaded = true;
}