Exemple #1
1
void TextureManager::Preload()
{

#ifdef USE_DEVIL
	ILuint image;
	ilGenImages(1, &image);
	ilBindImage(image);
	ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) M_data, M_bytes);
	GLuint tex = ilutGLBindTexImage();
#else
	 unsigned int tex = SOIL_load_OGL_texture_from_memory(
					  M_data,
					  M_bytes,
					  SOIL_LOAD_AUTO,
					  SOIL_CREATE_NEW_ID,

					    SOIL_FLAG_POWER_OF_TWO
					  |  SOIL_FLAG_MULTIPLY_ALPHA
					 // |  SOIL_FLAG_COMPRESS_TO_DXT
					  );
#endif

  textures["M.tga"]=tex;

#ifdef USE_DEVIL
  ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) project_data,project_bytes);
  tex = ilutGLBindTexImage();
#else
  tex = SOIL_load_OGL_texture_from_memory(
					  project_data,
					  project_bytes,
					  SOIL_LOAD_AUTO,
					  SOIL_CREATE_NEW_ID,

					  SOIL_FLAG_POWER_OF_TWO
					  |  SOIL_FLAG_MULTIPLY_ALPHA
					  //|  SOIL_FLAG_COMPRESS_TO_DXT
					  );
#endif

  textures["project.tga"]=tex;

#ifdef USE_DEVIL
  ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) headphones_data, headphones_bytes);
  tex = ilutGLBindTexImage();
#else
  tex = SOIL_load_OGL_texture_from_memory(
					  headphones_data,
					  headphones_bytes,
					  SOIL_LOAD_AUTO,
					  SOIL_CREATE_NEW_ID,

					  SOIL_FLAG_POWER_OF_TWO
					  |  SOIL_FLAG_MULTIPLY_ALPHA
					 // |  SOIL_FLAG_COMPRESS_TO_DXT
					  );
#endif

  textures["headphones.tga"]=tex;
}
Exemple #2
0
FltkImage* FltkImage::Load(const char *filebuf, int filelen)
{
	unsigned int id;
	ilGenImages (1, &id);
	ilBindImage (id);
	if (!ilLoadL (IL_TYPE_UNKNOWN, (void*)filebuf, filelen)) {
		ilDeleteImages (1, &id);
		return 0;
	}
	ilConvertImage (IL_RGBA, IL_UNSIGNED_BYTE);
	void *data = ilGetData();
	int w,h;
	ilGetIntegerv(IL_IMAGE_WIDTH, &w);
	ilGetIntegerv(IL_IMAGE_HEIGHT, &h);

	unsigned char *imageBuffer = new unsigned char[4*w*h];
	memcpy(imageBuffer, data, 4 * w* h);
	fltk::Image *img = new fltk::Image (imageBuffer, fltk::RGBA, w, h);
	ilDeleteImages(1, &id);

	FltkImage *inst = new FltkImage;
	inst->img = img;
	inst->buffer = (char*)imageBuffer;

	return inst;
}
Exemple #3
0
void CMainDlg::ExportTextures(const CString &path)
{
	ILuint handle;
	ilInit();
	iluInit();
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilEnable(IL_ORIGIN_SET);
	for(int i = 0; i < m_Textures.GetCount(); i++)
	{
		if(texdata[i] != -1)
		{
			ilGenImages(1, & handle);
			ilBindImage(handle);
			CString file;
			m_Textures.GetText(i, file);
			vector<char> data;
			data.resize(sc.index[texdata[i]].fileSize);
			ifstream is;
			is.sync_with_stdio(false);
			is.open(sc.index[texdata[i]].cachename.c_str(), ios::binary|ios::in);
			is.read(reinterpret_cast<char*>(&data[0]), sc.index[texdata[i]].fileSize);
			is.close();
			ILboolean ret = ilLoadL(IL_DDS, reinterpret_cast<char*>(&data[0]), sc.index[texdata[i]].fileSize);
			file.Replace(".dds", ".png");
			iluFlipImage();
			ilSaveImage((path + file));
			/*ofstream out;
			out.sync_with_stdio(false);
			out.open(path + file, ios::binary);
			out.write(reinterpret_cast<char*>(&data[0]), sc.index[texdata[i]].fileSize);
			out.close();*/
			ilDeleteImages(1, & handle);
		}
	}
}
Exemple #4
0
int main(int argc, char **argv)
{
	ILuint	ImgId;
	ILenum	Error;
	char	*Data;
	long	Size;

	// We use the filename specified in the first argument of the command-line.
	if (argc < 3) {
		printf("Please specify a .rar file and file inside to open.\n");
		return 1;
	}

	// Check if the shared lib's version matches the executable's version.
	if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
		iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION) {
		printf("DevIL version is different...exiting!\n");
		return 2;
	}

	// Initialize DevIL.
	ilInit();
	// Generate the main image name to use.
	ilGenImages(1, &ImgId);
	// Bind this image name.
	ilBindImage(ImgId);

	if (!urarlib_get(&Data, &Size, argv[2], argv[1], "none")) {
		printf("Error loading .rar file.\n");
		return 3;
	} 

	// Loads the image specified by File into the image named by ImgId.
	ilLoadL(IL_TGA, Data, Size);

	// Display the image's dimensions to the end user.
	printf("Width: %d  Height: %d  Depth: %d  Bpp: %d\n", ilGetInteger(IL_IMAGE_WIDTH),
		ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_DEPTH), ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));

	// Enable this to let us overwrite the destination file if it already exists.
	ilEnable(IL_FILE_OVERWRITE);

	// If argv[2] is present, we save to this filename, else we save to test.tga.
	if (argc > 2)
		ilSaveImage(argv[3]);
	else
		ilSaveImage("test.tga");

	// We're done with the image, so let's delete it.
	ilDeleteImages(1, &ImgId);

	// Simple Error detection loop that displays the Error to the user in a human-readable form.
	while ((Error = ilGetError())) {
		printf("Error: %s\n", iluErrorString(Error));
	}

	return 0;
}
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;
        }
ILboolean ILAPIENTRY ilutD3D8TexFromFileInMemory(IDirect3DDevice8 *Device, ILvoid *Lump, ILuint Size, IDirect3DTexture8 **Texture)
{
	iBindImageTemp();
	if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
		return IL_FALSE;

	*Texture = ilutD3D8Texture(Device);

	return IL_TRUE;
}
Exemple #7
0
void test_ilLoadL(wchar_t* sourceFN, wchar_t* targetFN)
{
	testHeap();
	ilInit();
	testHeap();
	ILuint handle = ilGenImage();
	testHeap();
	ilBindImage(handle);
	testHeap();
	//printf("Loading " PathCharMod "\n", sourceFN);
	FILE* f = _wfopen(sourceFN, L"rb");
	bool loaded = false;

	if (f != NULL) {
		fseek(f, 0, SEEK_END);
		INT64 size = ftell(f);
		if (size > 0) {
			fseek(f, 0, SEEK_SET);
			char* lump = new char[size];
			size_t read = fread(lump, 1, size, f);
			if (read == size) {
				loaded = ilLoadL(IL_TYPE_UNKNOWN, lump, read);
				if (!loaded) {
					printf("test_ilLoadL: Failed to load " PathCharMod "\n", sourceFN);
					++errors;
				}
			} else {
				printf("test_ilLoadL: Failed to read " PathCharMod "\n", sourceFN);
				++errors;
			}
			delete lump;
		}
		fclose(f);
	} else {
		printf("test_ilLoadL: Failed to load %S\n", sourceFN);
		++errors;
	}

	testHeap();
	//ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
	//ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
	//iluScale(150, 150, 1);
	testHeap();
	DeleteFile(targetFN);
	//printf("Saving " PathCharMod "\n", targetFN);
	if (loaded)
		if (!ilSaveImage(targetFN)) {
			printf("test_ilLoadL: Failed to save " PathCharMod "\n", targetFN);
			++errors;
		}
	testHeap();
	ilDeleteImage(handle);
}
Exemple #8
0
	void ImageData::load(Data * data)
	{
		Lock lock(devilMutex);
		ILuint image;

		// Generate DevIL image.
		ilGenImages(1, &image);

		// Bind the image.
		ilBindImage(image);

		// Try to load the image.
		ILboolean success = ilLoadL(IL_TYPE_UNKNOWN, (void*)data->getData(), data->getSize());

		// Check for errors
		if(!success)
		{
			throw love::Exception("Could not decode image!");
		}

		width = ilGetInteger(IL_IMAGE_WIDTH);
		height = ilGetInteger(IL_IMAGE_HEIGHT);
		origin = ilGetInteger(IL_IMAGE_ORIGIN);

		// Make sure the image is in RGBA format.
		ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

		// This should always be four.
		bpp = ilGetInteger(IL_IMAGE_BPP);

		if(bpp != 4)
		{
			ilDeleteImages(1, &image);
			std::cerr << "Bits per pixel != 4" << std::endl;
			return;
		}

		try
		{
			this->data = new unsigned char[width*height*bpp];
		}
		catch(std::bad_alloc)
		{
			ilDeleteImages(1, &image);
			throw love::Exception("Out of memory");
		}

		memcpy(this->data, ilGetData(), width*height*bpp);

		ilDeleteImages(1, &image);
	}
// eng: loading from memory
// rus: загрузка из памяти
unsigned char* LoadTextureFromMemory(const unsigned char* buffer, int len, int* width, int* height)
{
	#ifdef LOADER_OPENIL

	if (first_loading_openil)
	{
		first_loading_openil=false;
		ilInit();
	}

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

	ilLoadL(IL_TYPE_UNKNOWN, buffer, len);

	ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);

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

	int* pixels=(int*)ilGetData();
	int* data=new int[len4];
	for (int i=0;i<len4;i++)
		data[i]=pixels[i];

	ilDeleteImages(1, &image);

	return (unsigned char*)data;

	#else
	
		#ifdef LOADER_STDIMAGE

			int comp;
			return stbi_load_from_memory(buffer, len, width, height, &comp, 4);

		#else

			#ifdef LOADER_LIBPNGJPG
				return LoadPNGJPGFromMemory(buffer, len, width, height);
			#endif

		#endif

	#endif
}
Exemple #10
0
ILboolean ILAPIENTRY ilutD3D10TexFromResource(ID3D10Device *Device, HMODULE SrcModule, ILconst_string SrcResource, ID3D10Texture2D **Texture)
{
	HRSRC	Resource;
	ILubyte	*Data;

	iBindImageTemp();

	Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
	Data = (ILubyte*)LockResource(Resource);
	if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
		return IL_FALSE;

	*Texture = ilutD3D10Texture(Device);

	return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8VolTexFromResource(IDirect3DDevice8 *Device, HMODULE SrcModule, char *SrcResource, IDirect3DVolumeTexture8 **Texture)
{
	HRSRC	Resource;
	ILubyte	*Data;

	iBindImageTemp();

	Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
	Data = (ILubyte*)LockResource(Resource);
	if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
		return IL_FALSE;

	*Texture = ilutD3D8VolumeTexture(Device);

	return IL_TRUE;
}
Exemple #12
0
bool CBitmap::LoadGrayscale(const std::string& filename)
{
	type = BitmapTypeStandardAlpha;
	channels = 1;

	CFileHandler file(filename);
	if (!file.FileExists()) {
		return false;
	}

	unsigned char* buffer = new unsigned char[file.FileSize() + 1];
	file.Read(buffer, file.FileSize());

	boost::mutex::scoped_lock lck(devilMutex);
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	const bool success = !!ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
	ilDisable(IL_ORIGIN_SET);
	delete[] buffer;

	if (success == false) {
		return false;
	}

	ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	delete[] mem;
	mem = NULL; // to prevent a dead-pointer in case of an out-of-memory exception on the next line
	mem = new unsigned char[xsize * ysize];
	memcpy(mem, ilGetData(), xsize * ysize);

	ilDeleteImages(1, &ImageName);

	return true;
}
Exemple #13
0
Bitmap *loadFromData(void const *data, size_t size, std::string const &fmt)
{
    ilGetError();
    ILenum ilFormat = IL_RAW;
    ilFormat = get_bitmap_ext_fmt(fmt);
    bitmap_init();
    int img = ilGenImage();
    ilBindImage(img);
    ilEnable(IL_ORIGIN_SET);
    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
    ilEnable(IL_CONV_PAL);
    ilLoadL(ilFormat, data, size);
    int err = ilGetError();
    if (err != IL_NO_ERROR)
    {
        ilDeleteImage(img);
        char errStr[256];
        _snprintf_s(errStr, 256, "Error 0x%x", err);
        errStr[255] = 0;
        throw std::runtime_error(std::string("Error loading image format: ") + fmt
            + "\n" + errStr);
    }
    return new ILBitmap(img);
}
STDMETHODIMP my12doomImageSource::Load(LPCOLESTR pszFileName, __in_opt const AM_MEDIA_TYPE *pmt)
{
	if (m_curfile[0])
		return E_FAIL;

	wcscpy(m_curfile, pszFileName);

	// parse file
	MpoParser mpo;
	_3DPParser _3dp;

	void *data1 = NULL;
	void *data2 = NULL;
	int size1 = 0;
	int size2 = 0;
	ILenum type = IL_TYPE_UNKNOWN;
	bool need_delete = false;
	
	if (2 == mpo.parseFile(pszFileName))
	{
		data1 = mpo.m_datas[0];
		data2 = mpo.m_datas[1];
		size1 = mpo.m_sizes[0];
		size2 = mpo.m_sizes[1];
		type = IL_JPG;
		m_layout = IStereoLayout_SideBySide;
	}
	else if (2 == _3dp.parseFile(pszFileName))
	{
		data1 = _3dp.m_datas[0];
		data2 = _3dp.m_datas[1];
		size1 = _3dp.m_sizes[0];
		size2 = _3dp.m_sizes[1];
		type = IL_JPG;
		m_layout = IStereoLayout_SideBySide;
	}
	else
	{
		FILE *f = _wfopen(pszFileName, L"rb");
		if (NULL != f)
		{
			fseek(f, 0, SEEK_END);
			size1 = ftell(f);
			fseek(f, 0, SEEK_SET);
			data1 = new char[size1];
			need_delete = true;
			fread(data1, 1, size1, f);
			fclose(f);
		}
	}

	// decode file
	if (size1 == 0)
		return VFW_E_INVALID_FILE_FORMAT;

	CAutoLock lck(&g_ILLock);
	ilInit();
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	// first file / single file
	ILboolean result = ilLoadL(type, data1, size1 );
	if (!result)
	{
		ILenum err = ilGetError() ;
		printf( "the error %d\n", err );
		printf( "string is %s\n", ilGetString( err ) );
		if (need_delete) delete [] data1;
		return VFW_E_INVALID_FILE_FORMAT;
	}

	ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
	int decoded_size = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
	int width = ilGetInteger(IL_IMAGE_WIDTH);
	m_height = ilGetInteger(IL_IMAGE_HEIGHT);
	m_width = width;
	m_decoded_data = new char[decoded_size];
	assert(decoded_size >= width * m_height *4);
	char *data = (char*)ilGetData();
	char *dst = m_decoded_data;
	for(int y=0; y<m_height; y++)
	{
		memcpy(dst, data, width*4);
		dst += m_width*4;
		data += width*4;
	}

	// second file
	if (size2 > 0)
	{
		result = ilLoadL(type, data2, size2 );
		if (!result)
		{
			ILenum err = ilGetError() ;
			if (need_delete) delete [] data1;
			safe_delete(m_decoded_data);
			return VFW_E_INVALID_FILE_FORMAT;
		}
		ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
		decoded_size = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
		int width2 = ilGetInteger(IL_IMAGE_WIDTH);
		int height2 = ilGetInteger(IL_IMAGE_HEIGHT);
		m_decoded_data2 = new char[decoded_size];
		assert(decoded_size >= width * m_height *4);
		data = (char*)ilGetData();
		dst = m_decoded_data2;
		for(int y=0; y<min(height2,m_height); y++)
		{
			memcpy(dst, data, min(width, width2)*4);
			dst += m_width*4;
			data += width2*4;
		}
	}

	HRESULT hr = E_FAIL;
	m_paStreams[0] = new my12doomImageStream(&hr, this, L"Image Out", m_decoded_data, m_width, m_height);
	if(m_paStreams[0] == NULL)
	{
		if (need_delete) delete [] data1;
		return E_OUTOFMEMORY;
	}

	if (m_decoded_data2)
	{
	m_paStreams[1] = new my12doomImageStream(&hr, this, L"Image Out 2", m_decoded_data2, m_width, m_height);
	if(m_paStreams[1] == NULL)
	{
		if (need_delete) delete [] data1;
		return E_OUTOFMEMORY;
	}
	}

	if (need_delete) delete [] data1;
	return S_OK;
}
Exemple #15
0
bool CBitmap::Load(std::string const& filename, unsigned char defaultAlpha)
{
#ifndef BITMAP_NO_OPENGL
	ScopedTimer timer("Textures::CBitmap::Load");
#endif

	bool noAlpha = true;

	delete[] mem;
	mem = NULL;

#ifndef BITMAP_NO_OPENGL
	textype = GL_TEXTURE_2D;
#endif // !BITMAP_NO_OPENGL

	if (filename.find(".dds") != std::string::npos) {
#ifndef BITMAP_NO_OPENGL
		type = BitmapTypeDDS;
		xsize = 0;
		ysize = 0;
		channels = 0;

		ddsimage = new nv_dds::CDDSImage();
		bool status = ddsimage->load(filename);

		if (status) {
			xsize = ddsimage->get_width();
			ysize = ddsimage->get_height();
			channels = ddsimage->get_components();
			switch (ddsimage->get_type()) {
				case nv_dds::TextureFlat :
					textype = GL_TEXTURE_2D;
					break;
				case nv_dds::Texture3D :
					textype = GL_TEXTURE_3D;
					break;
				case nv_dds::TextureCubemap :
					textype = GL_TEXTURE_CUBE_MAP;
					break;
				case nv_dds::TextureNone :
				default :
					break;
			}
		}
		return status;
#else // !BITMAP_NO_OPENGL
		AllocDummy(); //allocate a dummy texture, as dds aren't supported in headless
		return true;
#endif // !BITMAP_NO_OPENGL
	}

	type = BitmapTypeStandardRGBA;
	channels = 4;

	CFileHandler file(filename);
	if (file.FileExists() == false) {
		AllocDummy();
		return false;
	}

	unsigned char* buffer = new unsigned char[file.FileSize() + 2];
	file.Read(buffer, file.FileSize());

	boost::mutex::scoped_lock lck(devilMutex);
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	{
		// do not signal floating point exceptions in devil library
		ScopedDisableFpuExceptions fe;

		const bool success = !!ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
		ilDisable(IL_ORIGIN_SET);
		delete[] buffer;

		if (success == false) {
			AllocDummy();
			return false;
		}
	}

	noAlpha = (ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL) != 4);
	ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	mem = new unsigned char[xsize * ysize * 4];
	//ilCopyPixels(0, 0, 0, xsize, ysize, 0, IL_RGBA, IL_UNSIGNED_BYTE, mem);
	memcpy(mem, ilGetData(), xsize * ysize * 4);

	ilDeleteImages(1, &ImageName);

	if (noAlpha) {
		for (int y=0; y < ysize; ++y) {
			for (int x=0; x < xsize; ++x) {
				mem[((y*xsize+x) * 4) + 3] = defaultAlpha;
			}
		}
	}

	return true;
}
Exemple #16
0
void CBitmap::Load(string const& filename, unsigned char defaultAlpha)
{
	delete[] mem;
	mem = NULL;

	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	if(mem != NULL) delete [] mem;

	CFileHandler file(filename);
	if(file.FileExists() == false)
	{
		xsize = 1;
		ysize = 1;
		mem=new unsigned char[4];
		memset(mem, 0, 4);
		return;
	}

	unsigned char *buffer = new unsigned char[file.FileSize()];
	file.Read(buffer, file.FileSize());

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	const bool success = ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
	delete [] buffer;

	if(success == false)
	{
		xsize = 1;
		ysize = 1;
		mem=new unsigned char[4];
		memset(mem, 0, 4);
		return;   
	}

	bool noAlpha=ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL)!=4;
#if !defined(__APPLE__) // Temporary fix to allow testing of everything
						// else until i get a quicktime image loader written
	ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	mem = new unsigned char[xsize * ysize * 4];
	//	ilCopyPixels(0,0,0,xsize,ysize,0,IL_RGBA,IL_UNSIGNED_BYTE,mem);
	memcpy(mem, ilGetData(), xsize * ysize * 4);
#else
	xsize = 4;
	ysize = 4;

	mem = new unsigned char[xsize * ysize * 4];
#endif

	ilDeleteImages(1, &ImageName); 

	if(noAlpha){
		for(int y=0;y<ysize;++y){
			for(int x=0;x<xsize;++x){
			mem[(y*xsize+x)*4+3]=defaultAlpha;
			}
		}
	}
}
ILboolean ilImage::Load(void* data,int len,ILenum Type)
{
	this->iGenBind();
	return ilLoadL(Type, data,len);
}
void CBitmap::Load(string filename, unsigned char defaultAlpha)
{
	if(mem!=0)
	{
		delete[] mem;
		mem = NULL;
	}

	if(filename.find(".dds")!=string::npos){
		ddsimage = new nv_dds::CDDSImage();
		ddsimage->load(filename);
		type = BitmapTypeDDS;
		return;
	}
	type = BitmapTypeStandar;

	ilInit();
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	if(mem != NULL) delete [] mem;

	CFileHandler file(filename);
	if(file.FileExists() == false)
	{
		xsize = 1;
		ysize = 1;
		mem=new unsigned char[4];
		memset(mem, 0, 4);
		return;
	}

	unsigned char *buffer = new unsigned char[file.FileSize()];
	file.Read(buffer, file.FileSize());

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	const bool success = ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
	delete [] buffer;

	if(success == false)
	{
		xsize = 1;
		ysize = 1;
		mem=new unsigned char[4];
		memset(mem, 0, 4);
		return;   
	}

	bool noAlpha=ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL)!=4;
	ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	mem = new unsigned char[xsize * ysize * 4];
//	ilCopyPixels(0,0,0,xsize,ysize,0,IL_RGBA,IL_UNSIGNED_BYTE,mem);
	memcpy(mem, (unsigned char *) ilGetData() , xsize * ysize * 4);

	ilDeleteImages(1, &ImageName); 

	if(noAlpha){
		for(int y=0;y<ysize;++y){
			for(int x=0;x<xsize;++x){
				mem[(y*xsize+x)*4+3]=defaultAlpha;
			}
		}
	}
}
Exemple #19
0
int NMS_TextureManager::LoadTexture (const char* sFilename,char* textureName) {
	if(_DEBUG)
	{
		sprintf_s (m_Singleton->m_sMessage, m_Singleton->m_iMessageSize, "NMS_TextureManager::Trying to load [%s]\n", sFilename);
		LOG.write(m_sMessage,LOG_DEBUG);
	}
	if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
	  {
		  LOG.write("NMS_TextureManager::Wrong DevIL version!\n",LOG_ERROR);
		  return -1;
	  }
	 
	  //Create the hash on which to compare to see if we have loaded the same two file twice
	  shaMap hash;

	  //Open the file provided and check for the SHA1 hash to see if we have already another texture like this
	  FILE	*fp=NULL;
	  ILubyte *Lump;

	  //Open the texture file
	  fopen_s(&fp,sFilename,"rb");
	  if (!fp)
	  {
		LOG.write("NMS_TextureManager::Cannot open the texture file!\n",LOG_ERROR);
		return -1;
	  }
	 

	  //Calculate the size of the file
	  long fileSize= nmsFileManagement::FileSize(fp);
	  if (fileSize<=0)
	  {
		  LOG.write("NMS_TextureManager::Negative file size for the texture!\n",LOG_ERROR);
		  return -1;
	  }

	  //Convert the file read to a Lump file to be used by DevIL
	  Lump = (ILubyte*)LEVEL_ALLOC->allocMem(fileSize, MEM_TEXTURE);
	  fseek(fp, 0, SEEK_SET);
	  fread(Lump, 1, fileSize, fp);
	  fclose(fp);
	  if(_DEBUG)
			LOG.write("NMS_TextureManager::Lump created correctly!\n",LOG_DEBUG);



	  //Create the sha1
	  hash=nmsSha1::returnSha1(Lump,fileSize);
	  textStruct imageToBeAdded=checkForHash(hash,textureName);

	  GLuint image=NULL;
	  ILuint texid=NULL;
	  //Just a basic check. If both the textID AND the hash are different from NULL that means that 
	  //we have already the texture and we just want to change the name into the map
	  if(imageToBeAdded.textID!=NULL && imageToBeAdded.hash!=NULL)
	  {
		  textureMap[textureName]=imageToBeAdded;
		  image=textureMap[textureName].textID;
	  }
	  else
	  {
		  //It's the same texture with the same name, do nothing!
		  if(imageToBeAdded.textID==NULL && imageToBeAdded.hash!=NULL)
		  {
			  image=textureMap[textureName].textID;
		  }
		  //It's a different texture with the same name, warn the user and exit
		  else if(imageToBeAdded.textID==-1)
			    {throw 0;}
		  else
			  {
				  //It's a new image, load it
				  ILboolean success;
				  ilInit(); // Initialization of DevIL 
				  ilGenImages(1, &texid); // Generation of one image name 
				  ilBindImage(texid); // Binding of image name
				  success = ilLoadL(IL_TYPE_UNKNOWN,Lump,fileSize); // Loading of image "image.jpg"   //USA iLoadImageF
				  //free(Lump);
				  

				  if (success) // If no error occured: 
				  {
					success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); // Convert every colour component into
					  // unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA 
					if (!success)
					{
						LOG.write("NMS_TextureManager::Error in converting the texture in the proper format!\n",LOG_ERROR);
						ilDeleteImages(1, &texid);					 
						// Error occured 
						return -1;
					}


					//Create the new image
					glGenTextures(1, &image); // Texture name generation 
					
					 glBindTexture(GL_TEXTURE_2D, image);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
					// We will use linear interpolation for magnification filter 
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
					// We will use linear interpolation for minifying filter 
					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()); // Texture specification 


					if(_DEBUG)
						LOG.write("NMS_TextureManager::Texture loaded from Lump!\n",LOG_DEBUG);
				  }
				  else
				  {
					// Error occured 
					LOG.write("NMS_TextureManager::Error in loading the texture from the Lump!\n",LOG_ERROR);
					ilDeleteImages(1, &texid);
					return -1;
				  }
			  }
	  }
	  //Pick the image that has been created to be used in our context
	  textureMap[textureName].textID=image;
	  textureMap[textureName].hash=hash;

	  if(_DEBUG)
	  {
		 sprintf_s (m_Singleton->m_sMessage, m_Singleton->m_iMessageSize, "NMS_TextureManager::Loaded [%s] without issues!\n", sFilename);
		 LOG.write(m_sMessage,LOG_DEBUG);
	  }
	 

	  //glBindTexture(GL_TEXTURE_2D, image); // Binding of texture name 
      ilDeleteImages(1, &texid);
	  //Close the file we are done with it
	  fclose(fp);
	  cerr << "Texture id" << textureMap[textureName].textID;
	  return textureMap[textureName].textID;
	  
	  return textureMap[textureName].textID;
}
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;
    }
}
Exemple #21
0
bool CBitmap::Load(std::string const& filename, unsigned char defaultAlpha)
{
	bool noAlpha = true;

	delete[] mem;
	mem = NULL;

#ifndef BITMAP_NO_OPENGL
	textype = GL_TEXTURE_2D;
#endif // !BITMAP_NO_OPENGL

	if (filename.find(".dds") != std::string::npos) {
		type = BitmapTypeDDS;
		xsize = 0;
		ysize = 0;
		channels = 0;
#ifndef BITMAP_NO_OPENGL
		ddsimage = new nv_dds::CDDSImage();
		bool status = ddsimage->load(filename);
		if (status) {
			xsize = ddsimage->get_width();
			ysize = ddsimage->get_height();
			channels = ddsimage->get_components();
			switch (ddsimage->get_type()) {
				case nv_dds::TextureFlat :
					textype = GL_TEXTURE_2D;
					break;
				case nv_dds::Texture3D :
					textype = GL_TEXTURE_3D;
					break;
				case nv_dds::TextureCubemap :
					textype = GL_TEXTURE_CUBE_MAP;
					break;
				case nv_dds::TextureNone :
				default :
					break;
			}
		}
		return status;
#else
		return false;
#endif // !BITMAP_NO_OPENGL
	}
	type = BitmapTypeStandardRGBA;
	channels = 4;

	CFileHandler file(filename);
	if (file.FileExists() == false) {
		Alloc(1, 1);
		return false;
	}

	unsigned char* buffer = new unsigned char[file.FileSize() + 2];
	file.Read(buffer, file.FileSize());

	boost::mutex::scoped_lock lck(devilMutex);
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	const bool success = !!ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
	ilDisable(IL_ORIGIN_SET);
	delete[] buffer;

	if (success == false) {
		xsize = 1;
		ysize = 1;
		mem = new unsigned char[4];
		mem[0] = 255; // Red allows us to easily see textures that failed to load
		mem[1] = 0;
		mem[2] = 0;
		mem[3] = 255; // Non Transparent
		return false;
	}

	noAlpha = (ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL) != 4);
	ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	mem = new unsigned char[xsize * ysize * 4];
	//ilCopyPixels(0, 0, 0, xsize, ysize, 0, IL_RGBA, IL_UNSIGNED_BYTE, mem);
	memcpy(mem, ilGetData(), xsize * ysize * 4);

	ilDeleteImages(1, &ImageName);

	if (noAlpha) {
		for (int y=0; y < ysize; ++y) {
			for (int x=0; x < xsize; ++x) {
				mem[((y*xsize+x) * 4) + 3] = defaultAlpha;
			}
		}
	}

	return true;
}
void CDevILCodec::DecodeFromBuffer(const CMemoryBuffer & input, CImage *image,
		CImageFormats::NovaPixelFormats format, ESaveFormats ext)
{
	ILuint image_id;
	// Generate the main image name to use.
	ilGenImages(1, &image_id);

	// Bind this image name.
	ilBindImage(image_id);

	ILenum type = 0;
	switch(ext)
	{
	case SF_BMP:
		type = IL_BMP;
		break;
	case SF_ICO:
		type = IL_ICO;
		break;
	case SF_JPG:
		type = IL_JPG;
		break;
	case SF_PCX:
		type = IL_PCX;
		break;
	case SF_PIC:
		type = IL_PIC;
		break;
	case SF_PNG:
		type = IL_PNG;
		break;
	case SF_TGA:
		type = IL_TGA;
		break;
	case SF_TIF:
		type = IL_TIF;
		break;
	case SF_GIF:
		type = IL_GIF;
		break;
	case SF_DDS:
		type = IL_DDS;
		break;
	case SF_PIX:
		type = IL_PIX;
		break;
	case SF_HDR:
		type = IL_HDR;
		break;

	default:
		return;
	}

	if(!ilLoadL(type, input.GetBegin(), input.GetBufferSize()))
	{
		ILenum Error = 0;
		if((Error = ilGetError()) != NULL)
		{
			nstring str("CDevILCodec::DecodeFormStream: Can not load image lump - ");
			str.append(iluErrorString(Error));
			throw NOVA_EXP(str.c_str(), BAD_OPERATION);
		}
	}

	image->GetImageSource().mHeight = ilGetInteger(IL_IMAGE_HEIGHT);
	image->GetImageSource().mWidth = ilGetInteger(IL_IMAGE_WIDTH);
	image->GetImageSource().mDepth = ilGetInteger(IL_IMAGE_DEPTH);

	//if (ilGetInteger(IL_IMAGE_ORIGIN) == IL_ORIGIN_UPPER_LEFT)
	//	iluFlipImage();

	if(format == CImageFormats::NF_DEFAULT)
	{
		CDevILFormats inf;
		inf.SetFormat(ilGetInteger(IL_IMAGE_FORMAT));
		format = inf.GetExFormat();
	}

	CDevILFormats informat;
	informat.SetExFormat(format);
	image->GetImageSource().mPixelFormat = format;


	size_t _size = image->GetWidth() * image->GetHeight() * image->GetDepth() * ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
	image->GetBits().FreeBuffer();
	image->GetBits().AllocBuffer(_size);

	ilCopyPixels(0, 0, 0, image->GetWidth(), image->GetHeight(),
		image->GetDepth(), informat.GetFormat(), IL_UNSIGNED_BYTE, image->GetBits().GetBegin());

	ilDeleteImages(1, &image_id);

	ILenum Error = 0;
	if((Error = ilGetError()) != NULL)
	{
		nstring str("CDevILCodec::DecodeFormStream(): Can not load image file - ");
		str.append(iluErrorString(Error));
		throw NOVA_EXP(str.c_str(), BAD_OPERATION);
	}
}
Exemple #23
0
// type is either IL_JPG, IL_TIF, or IL_PNM in this case
Pixels::Pixels(ILenum type, const char* lump, const int size, const std::string& fn)
    :w(0), h(0), loaded(false), fn(fn), gray_shade(GRAY_SHADE)
{
    // Only execute in one thread since DevIL/OpenIL doesn't support multithreading,
    // so use a unique lock here. But, we'll do a bit more that doesn't need to be
    // in a single thread, so make the lock go out of scope when we're done.
    {
        std::unique_lock<std::mutex> lck(lock);

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

        if (ilLoadL(type, lump, static_cast<ILuint>(size)))
        {
            w = ilGetInteger(IL_IMAGE_WIDTH);
            h = ilGetInteger(IL_IMAGE_HEIGHT);

            // If the image height or width is larger than int's max, it will appear
            // to be negative. Just don't use extremely large (many gigapixel) images.
            if (w < 0 || h < 0)
                throw std::runtime_error("use a smaller image, can't store dimensions in int");

            // 3 because IL_RGB
            const int total = w*h*3;
            unsigned char* data = new unsigned char[total];

            ilCopyPixels(0, 0, 0, w, h, 1, IL_RGB, IL_UNSIGNED_BYTE, data);

            // Move data into a nicer format
            int x = 0;
            int y = 0;
            p = std::vector<std::vector<unsigned char>>(h, std::vector<unsigned char>(w));

            // Start at third
            for (int i = 2; i < total; i+=3)
            {
                // Average min and max to get lightness
                //  p[y][x] = smartFloor((min(data[i-2], data[i-1], data[i]) +
                //                        max(data[i-2], data[i-1], data[i]))/2);
                // For average:
                //  p[y][x] = smartFloor((1.0*data[i-2]+data[i-1]+data[i])/3);
                //
                // For luminosity:
                //  p[y][x] = smartFloor(0.2126*data[i-2] + 0.7152*data[i-1] + 0.0722*data[i]);

                // Use the simplest. It doesn't seem to make a difference.
                p[y][x] = smartFloor((1.0*data[i-2]+data[i-1]+data[i])/3);

                // Increase y every time we get to end of row
                if (x+1 == w)
                {
                    x=0;
                    ++y;
                }
                else
                {
                    ++x;
                }
            }

            loaded = true;
            delete[] data;
        }
        else
        {
            ilDeleteImages(1, &name);
            throw std::runtime_error("could not read image");
        }

        ilDeleteImages(1, &name);
    }

    // After loading, determine the real gray shade to view this as a black and white
    // image. We'll be using this constantly, so we might as well do it now.
    const Histogram h(p);
    gray_shade = h.threshold(gray_shade);
}
//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;
}
Exemple #25
0
const char* IMG_LoadImageInternal( const char* fname, byte** imageData, u32* width, u32* height )
{
	*imageData = 0;
	*width = 0;
	*height = 0;
	
	if ( fname == 0 || fname[0] == 0 )
		return 0;
		
	const char* ext = G_strgetExt( fname );
	
	str s = fname;
	s.defaultExtension( "tga" );
	
	byte* buf = 0;
	u32 len;
	len = g_vfs->FS_ReadFile( s, ( void** )&buf );
	if ( buf == 0 )
	{
		for ( u32 i = 0; i < img_numSupportedImageTypes; i++ )
		{
			imgType_s* t = &img_types[i];
			s.setExtension( t->ext );
			len = g_vfs->FS_ReadFile( s, ( void** )&buf );
			if ( buf )
				break;
		}
	}
	if ( buf == 0 )
		return 0; // cannot open image file
		
	ext = s.getExt();
	
	if ( 1 && !stricmp( ext, "tga" ) )
	{
		// devil tga loader swaps colors....
		IMG_LoadTGA( s, imageData, buf, width, height );
		g_vfs->FS_FreeFile( buf );
		strcpy( lastValidFName, s.c_str() );
		return lastValidFName;
	}
#ifdef IMAGE_USE_VTF_LIB
	if ( 1 && !stricmp( ext, "vtf" ) )
	{
		// we have a better loader for vtf...
		// Devil VTF loader cant load SOME OF vtf types
		IMG_LoadVTF( s, buf, len, imageData, width, height );
		g_vfs->FS_FreeFile( buf );
		strcpy( lastValidFName, s.c_str() );
		return lastValidFName;
	}
#endif // IMAGE_USE_VTF_LIB
	
	ILuint ilTexture;
	ilGenImages( 1, &ilTexture );
	ilBindImage( ilTexture );
	
	
	int type = ILTypeForExt( ext ); // get IL_***
	//g_core->Print("Calling ilLoadL for %s with len %i\n",s.c_str(),len);
	ILboolean done = 0;
	done = ilLoadL( type, buf, len );
	//g_core->Print("result %i\n",done);
	g_vfs->FS_FreeFile( buf );
	if ( !done )
	{
		ilBindImage( 0 );
		ilDeleteImages( 1, &ilTexture );
		g_core->RedWarning( "IMG_LoadImageInternal: error while reading image file %s\n", s.c_str() );
		return 0;
	}
	strcpy( lastValidFName, s.c_str() );
	*width = ilGetInteger( IL_IMAGE_WIDTH );
	*height = ilGetInteger( IL_IMAGE_HEIGHT );
	int d = ilGetInteger( IL_IMAGE_BPP );
	if ( d != 4 )
	{
		d = 4;
		ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE );
	}
	u32 numImageBytes = ( *width ) * ( *height ) * ( d );
	*imageData = ( unsigned char* )malloc( numImageBytes );
	if ( *imageData == 0 )
	{
		g_core->RedWarning( "IMG_LoadImageInternal: malloc failed for %i bytes (image %s)\n", numImageBytes, lastValidFName );
		*width = 0;
		*height = 0;
	}
	else
	{
		memcpy( *imageData, ilGetData(), numImageBytes );
	}
	ilBindImage( 0 );
	ilDeleteImages( 1, &ilTexture );
	return lastValidFName;
}
Exemple #26
0
    //---------------------------------------------------------------------
    Codec::DecodeResult ILImageCodec::decode(DataStreamPtr& input) const
    {

        // DevIL variables
        ILuint ImageName;

        ILint ImageFormat, BytesPerPixel, ImageType;
        ImageData* imgData = new ImageData();
        MemoryDataStreamPtr output;

        // Load the image
        ilGenImages( 1, &ImageName );
        ilBindImage( ImageName );

        // Put it right side up
        ilEnable(IL_ORIGIN_SET);
        ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_UPPER_LEFT);

        // Keep DXTC(compressed) data if present
        ilSetInteger(IL_KEEP_DXTC_DATA, IL_TRUE);

        // Load image from stream, cache into memory
        MemoryDataStream memInput(input);
        ilLoadL( 
            mIlType, 
            memInput.getPtr(), 
            static_cast< ILuint >(memInput.size()));

        // Check if everything was ok
        ILenum PossibleError = ilGetError() ;
        if( PossibleError != IL_NO_ERROR ) {
            OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED,
                "IL Error",
                iluErrorString(PossibleError) ) ;
        }

        ImageFormat = ilGetInteger( IL_IMAGE_FORMAT );
        ImageType = ilGetInteger( IL_IMAGE_TYPE );

        // Convert image if ImageType is incompatible with us (double or long)
        if(ImageType != IL_BYTE && ImageType != IL_UNSIGNED_BYTE && 
			ImageType != IL_FLOAT &&
			ImageType != IL_UNSIGNED_SHORT && ImageType != IL_SHORT) {
            ilConvertImage(ImageFormat, IL_FLOAT);
			ImageType = IL_FLOAT;
        }
		// Converted paletted images
		if(ImageFormat == IL_COLOUR_INDEX)
		{
			ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
			ImageFormat = IL_BGRA;
			ImageType = IL_UNSIGNED_BYTE;
		}

        // Now sets some variables
        BytesPerPixel = ilGetInteger( IL_IMAGE_BYTES_PER_PIXEL ); 

        imgData->format = ILUtil::ilFormat2OgreFormat( ImageFormat, ImageType );
        imgData->width = ilGetInteger( IL_IMAGE_WIDTH );
        imgData->height = ilGetInteger( IL_IMAGE_HEIGHT );
        imgData->depth = ilGetInteger( IL_IMAGE_DEPTH );
        imgData->num_mipmaps = ilGetInteger ( IL_NUM_MIPMAPS );
        imgData->flags = 0;
		
		if(imgData->format == PF_UNKNOWN)
		{
			std::stringstream err;
			err << "Unsupported devil format ImageFormat=" << std::hex << ImageFormat << 
				" ImageType="<< ImageType << std::dec;
			ilDeleteImages( 1, &ImageName );
			
			OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED,
                err.str(),
                "ILImageCodec::decode" ) ;
		}

        // Check for cubemap
        //ILuint cubeflags = ilGetInteger ( IL_IMAGE_CUBEFLAGS );
		size_t numFaces = ilGetInteger ( IL_NUM_IMAGES ) + 1;
        if(numFaces == 6) 
			imgData->flags |= IF_CUBEMAP;
        else
            numFaces = 1; // Support only 1 or 6 face images for now
  
        // Keep DXT data (if present at all and the GPU supports it)
        ILuint dxtFormat = ilGetInteger( IL_DXTC_DATA_FORMAT );
        if(dxtFormat != IL_DXT_NO_COMP && Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability( RSC_TEXTURE_COMPRESSION_DXT ))
        {
			imgData->format = ILUtil::ilFormat2OgreFormat( dxtFormat, ImageType );
            imgData->flags |= IF_COMPRESSED;
            
            // Validate that this devil version saves DXT mipmaps
            if(imgData->num_mipmaps>0)
            {
                ilBindImage(ImageName);
                ilActiveMipmap(1);
                if((size_t)ilGetInteger( IL_DXTC_DATA_FORMAT ) != dxtFormat)
                {
                    imgData->num_mipmaps=0;
                    LogManager::getSingleton().logMessage(
                    "Warning: Custom mipmaps for compressed image "+input->getName()+" were ignored because they are not loaded by this DevIL version");
                }
            }
        }
        
        // Calculate total size from number of mipmaps, faces and size
        imgData->size = Image::calculateSize(imgData->num_mipmaps, numFaces, 
            imgData->width, imgData->height, imgData->depth, imgData->format);

        // Bind output buffer
        output.bind(new MemoryDataStream(imgData->size));
        size_t offset = 0;
        
        // Dimensions of current mipmap
        size_t width = imgData->width;
        size_t height = imgData->height;
        size_t depth = imgData->depth;
        
        // Transfer data
        for(size_t mip=0; mip<=imgData->num_mipmaps; ++mip)
        {   
            for(size_t i = 0; i < numFaces; ++i)
            {
                ilBindImage(ImageName);
                if(numFaces > 1)
                    ilActiveImage(i);
                if(imgData->num_mipmaps > 0)
                    ilActiveMipmap(mip);
                /// Size of this face
                size_t imageSize = PixelUtil::getMemorySize(
                        width, height, depth, imgData->format);
                if(imgData->flags & IF_COMPRESSED)
                {

                    // Compare DXT size returned by DevIL with our idea of the compressed size
                    if(imageSize == ilGetDXTCData(NULL, 0, dxtFormat))
                    {
                        // Retrieve data from DevIL
                        ilGetDXTCData((unsigned char*)output->getPtr()+offset, imageSize, dxtFormat);
                    } else
                    {
                        LogManager::getSingleton().logMessage(
                            "Warning: compressed image "+input->getName()+" size mismatch, devilsize="+StringConverter::toString(ilGetDXTCData(NULL, 0, dxtFormat))+" oursize="+
                            StringConverter::toString(imageSize));
                    }
                }
                else
                {
                    /// Retrieve data from DevIL
                    PixelBox dst(width, height, depth, imgData->format, (unsigned char*)output->getPtr()+offset);
                    ILUtil::toOgre(dst);
                }
                offset += imageSize;
            }
            /// Next mip
            if(width!=1) width /= 2;
            if(height!=1) height /= 2;
            if(depth!=1) depth /= 2;
        }

        // Restore IL state
        ilDisable(IL_ORIGIN_SET);
        ilDisable(IL_FORMAT_SET);

        ilDeleteImages( 1, &ImageName );

        DecodeResult ret;
        ret.first = output;
        ret.second = CodecDataPtr(imgData);


        return ret;
    }
Texture* TextureReaderDevil::loadTexture(const std::string& filename, Texture::Filter filter,
                                         bool compress, bool keepPixels, bool createOGLTex,
                                         bool textureRectangle)
{

#ifndef GL_TEXTURE_RECTANGLE_ARB
    if (textureRectangle){
        LERROR("Texture Rectangles not supported!");
        textureRectangle = false;
    }
#endif

    File* file = FileSys.open(filename);

    // check if file is open
    if (!file || !file->isOpen()) {
        delete file;
        return 0;
    }

    size_t len = file->size();

    // check if file is empty
    if (len == 0) {
        delete file;
        return 0;
    }

    // allocate memory
    char* imdata = new char[len];

    if (imdata == 0) {
        delete file;
        return 0;   // allocation failed
    }

    file->read(imdata, len);

    file->close();
    delete file;

    /*
        FIXME: I think the keepPixels option does not work properly
        -> I don't see why...afaik keepPixels has been used in some project (stefan)
    */
    ILuint ImageName;
    ilGenImages(1, &ImageName);
    ilBindImage(ImageName);
    Texture* t = new Texture();
    t->setName(filename);

    if (!ilLoadL(IL_TYPE_UNKNOWN, imdata, static_cast<ILuint>(len))) {
        LERROR("Failed to open via ilLoadL " << filename);
        delete[] imdata;
        delete t;
        return 0;
    }
    delete[] imdata;
    imdata = 0;

    t->setBpp(ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));

    // determine image format
    ILint devilFormat;
    switch (ilGetInteger(IL_IMAGE_FORMAT)) {
        case IL_LUMINANCE:         // intensity channel only
            devilFormat = IL_LUMINANCE;
            t->setFormat(GL_LUMINANCE);
            break;
        case IL_LUMINANCE_ALPHA:   // intensity-alpha channels
            devilFormat = IL_LUMINANCE_ALPHA;
            t->setFormat(GL_LUMINANCE_ALPHA);
            break;
        case IL_RGB:
            devilFormat = IL_RGB;  // three color channels
            t->setFormat(GL_RGB);
            break;
        case IL_RGBA:
            devilFormat = IL_RGBA; // color-alpha channels
            t->setFormat(GL_RGBA);
            break;
        case IL_BGR:
            devilFormat = IL_RGB;  // B-G-R ordered color channels, convert to RGB
            t->setFormat(GL_RGB);
            break;
        case IL_BGRA:
            devilFormat = IL_RGBA; // R-G-B-A ordered color channels, convert to RGBA
            t->setFormat(GL_RGBA);
            break;
        default:
            LERROR("unsupported format: " << ilGetInteger(IL_IMAGE_FORMAT) << " (" << filename << ")");
            delete t;
            return 0;
    }

    // determine data type
    ILint devilDataType;
    switch (ilGetInteger(IL_IMAGE_TYPE)) {
    case IL_UNSIGNED_BYTE:
        devilDataType = IL_UNSIGNED_BYTE;
        t->setDataType(GL_UNSIGNED_BYTE);
        break;
    case IL_BYTE:
        devilDataType = IL_BYTE;
        t->setDataType(GL_BYTE);
        break;
    case IL_UNSIGNED_SHORT:
        devilDataType = IL_UNSIGNED_SHORT;
        t->setDataType(GL_UNSIGNED_SHORT);
        break;
    case IL_SHORT:
        devilDataType = IL_SHORT;
        t->setDataType(GL_SHORT);
        break;
    case IL_UNSIGNED_INT:
        devilDataType = IL_UNSIGNED_INT;
        t->setDataType(GL_UNSIGNED_INT);
        break;
    case IL_INT:
        devilDataType = IL_INT;
        t->setDataType(GL_INT);
        break;
    case IL_FLOAT:
        devilDataType = IL_FLOAT;
        t->setDataType(GL_FLOAT);
        break;
    default:
        LERROR("unsupported data type: " << ilGetInteger(IL_IMAGE_TYPE) << " (" << filename << ")");
        delete t;
        return 0;
    }

    if (!ilConvertImage(devilFormat, devilDataType)) {
        LERROR("failed to convert loaded image: " << filename);
        delete t;
        return 0;
    }

    tgt::ivec3 dims;
    dims.x = ilGetInteger(IL_IMAGE_WIDTH);
    dims.y = ilGetInteger(IL_IMAGE_HEIGHT);
    dims.z = ilGetInteger(IL_IMAGE_DEPTH);
    t->setDimensions(dims);
    LDEBUG("Image dimensions: " << t->getDimensions());
    tgtAssert( dims.z == 1, "depth is not equal 1");

#ifdef GL_TEXTURE_RECTANGLE_ARB
    if (textureRectangle)
        t->setType( GL_TEXTURE_RECTANGLE_ARB );
    else
#endif
        t->setType( GL_TEXTURE_2D );

    t->alloc();
    memcpy(t->getPixelData(), ilGetData(), t->getArraySize());

    bool success;
    if (textureRectangle)
        success = createRectangleTexture(t, filter, compress, createOGLTex);
    else {
        if (dims.y == 1)
            success = create1DTexture(t, filter, compress, createOGLTex);
        else
            success = create2DTexture(t, filter, compress, createOGLTex);
    }
    if (!success) {
        ilDeleteImages(1, &ImageName);
        if (!keepPixels)
            t->setPixelData(0);
        delete t;
        return 0;
    }

    ilDeleteImages(1, &ImageName);

    if (!keepPixels) {
        delete[] t->getPixelData();
        t->setPixelData(0);
    }

    return t;
}
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)};
}