Image::Image(const std::string& filename) { //use IL to load the image unsigned int img; ilGenImages(1, &img); ilBindImage(img); ilLoadImage(filename.c_str()); //grab data and release the IL image w = ilGetInteger(IL_IMAGE_WIDTH); h = ilGetInteger(IL_IMAGE_HEIGHT); char *data = new char[w*h*4]; ilCopyPixels(0,0,0, w,h,1, IL_RGBA, IL_UNSIGNED_BYTE, data); ilDeleteImages(1, &img); //make the GL texture glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //TODO: mipmaps glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); //free all the temp data delete[] data; }
void Model::loadImage(Image& image, const char* filepath) { std::cout << "Trying to load image at filepath: " << filepath << std::endl; ILuint imageName; // The image name to return. ilGenImages(1, &imageName); // Grab a new image name. ilBindImage(imageName); if (!ilLoadImage(filepath)) { ILenum Error; while ((Error = ilGetError()) != IL_NO_ERROR) { std::cout << Error << " " << iluErrorString(Error) << std::endl; } ilDeleteImages(1, &imageName); // Delete the image name. } image.path = filepath; image.width = ilGetInteger(IL_IMAGE_WIDTH); // getting image width image.height = ilGetInteger(IL_IMAGE_HEIGHT); // and height image.components = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); if(image.components < 3) image.components = 3; int memory_needed = image.width * image.height * image.components; image.data.resize(memory_needed); //Allocate memory // finally get the image data, and delete the il-image. unsigned int type = IL_RGB; if(image.components == 4) type = IL_RGBA; ilCopyPixels(0, 0, 0, image.width, image.height, 1, type, IL_UNSIGNED_BYTE, &image.data[0]); ilDeleteImages(1, &imageName); }
bool CGuildMarkUploader::__Load(const char* c_szFileName, UINT* peError) { ILuint uImg; ilGenImages(1, &uImg); ilBindImage(uImg); ilEnable(IL_ORIGIN_SET); ilOriginFunc(IL_ORIGIN_UPPER_LEFT); if (!ilLoad(IL_TYPE_UNKNOWN, (const ILstring)c_szFileName)) { *peError=ERROR_LOAD; return false; } if (ilGetInteger(IL_IMAGE_WIDTH)!=SGuildMark::WIDTH) { *peError=ERROR_WIDTH; return false; } if (ilGetInteger(IL_IMAGE_HEIGHT)!=SGuildMark::HEIGHT) { *peError=ERROR_HEIGHT; return false; } ilConvertImage(IL_BGRA, IL_BYTE); ilCopyPixels(0, 0, 0, SGuildMark::WIDTH, SGuildMark::HEIGHT, 1, IL_BGRA, IL_BYTE, (ILvoid*)m_kMark.m_apxBuf); ilDeleteImages(1, &uImg); return true; }
image::image(char* file) { ilInit(); ilGenImages(1, &ID); /* Generation of one image name */ ilBindImage(ID); /* Binding of image name */ if(!ilLoadImage(file)) { img = new unsigned char[2]; return; } width = ilGetInteger(IL_IMAGE_WIDTH); height = ilGetInteger(IL_IMAGE_HEIGHT); type = ilGetInteger(IL_IMAGE_FORMAT); if(type == IL_RGB) size = 3; else size = 4; img = new unsigned char[width * height * size]; ilCopyPixels(0, 0, 0, width, height, 1, type, IL_UNSIGNED_BYTE, img); ilDeleteImages(1, &ID); ID = 0; }
void Texture2D::loadFromFile(std::string fileName) { ilInit(); ILuint devilError = ilGetError(); if (IL_NO_ERROR != devilError) { std::cout << iluErrorString(devilError) << std::endl; } ILuint imageHandle; ilGenImages(1, &imageHandle); ilBindImage(imageHandle); ilLoadImage(fileName.c_str()); if (IL_NO_ERROR != devilError) { std::cout << iluErrorString(devilError) << std::endl; } width = (unsigned int) ilGetInteger(IL_IMAGE_WIDTH); height = (unsigned int) ilGetInteger(IL_IMAGE_HEIGHT); bpp = (unsigned int) ilGetInteger(IL_IMAGE_BITS_PER_PIXEL); ILuint dataSize = ilGetInteger(IL_IMAGE_SIZE_OF_DATA); float* buffer = new float[width * height * 4]; ilCopyPixels(0, 0, 0, width, height, 1, IL_RGBA, IL_FLOAT, buffer); initialize(width, height, bpp); setData(buffer); ilDeleteImages(1, &imageHandle); }
// Sharpens when Factor is in the range [1.0, 2.5]. // Blurs when Factor is in the range [0.0, 1.0]. ILboolean ILAPIENTRY iluSharpen(ILfloat Factor, ILuint Iter) { ILimage *Blur, *CurImage; // iluBlur() changes iluCurImage ILuint i; CurImage = ilGetCurImage(); if (CurImage == NULL) { ilSetError(ILU_ILLEGAL_OPERATION); return IL_FALSE; } Blur = ilNewImage(CurImage->Width, CurImage->Height, CurImage->Depth, CurImage->Bpp, CurImage->Bpc); if (Blur == NULL) { return IL_FALSE; } ilCopyImageAttr(Blur, CurImage); ilCopyPixels(0, 0, 0, CurImage->Width, CurImage->Height, 1, CurImage->Format, CurImage->Type, Blur->Data); ilSetCurImage(Blur); iluBlurGaussian(1); for (i = 0; i < Iter; i++) { iIntExtImg(Blur, CurImage, Factor); } ilCloseImage(Blur); ilSetCurImage(CurImage); return IL_TRUE; }
void tiny_gl::GLMesh::load_specular(tinyobj::material_t & mat, texture_group_t & tex_group) { ILuint ilTexName; ilGenImages(1, &ilTexName); ilBindImage(ilTexName); if (ilLoadImage(mat.specular_texname.c_str()) && ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE)) { unsigned char *data = new unsigned char[ilGetInteger(IL_IMAGE_WIDTH) * ilGetInteger(IL_IMAGE_HEIGHT) * 4]; ilCopyPixels(0, 0, 0, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 1, IL_RGBA, IL_UNSIGNED_BYTE, data); glGenTextures(1, &tex_group.specularid); glBindTexture(GL_TEXTURE_2D, tex_group.specularid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, data); delete[] data; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glGenerateMipmap(GL_TEXTURE_2D); } ilDeleteImages(1, &ilTexName); }
void CDevILCodec::DecodeFromFile(const nstring & filename, CImage *image, CImageFormats::NovaPixelFormats format) { ILuint image_id; // Generate the main image name to use. ilGenImages(1, &image_id); // Bind this image name. ilBindImage(image_id); if(!ilLoadImage(filename.c_str())) { ILenum Error = 0; if((Error = ilGetError()) != NULL) { nstring str("CDevILCodec::DecodeFromFile(): Can not load image file - "); 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_LOWER_LEFT) // iluFlipImage(); // this->size = ilGetInteger(IL_IMAGE_SIZE_OF_DATA); 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::DecodeFromFile(): Can not load image file "); str.append(filename); str.append(", reason: "); str.append(iluErrorString(Error)); throw NOVA_EXP(str.c_str(), BAD_OPERATION); } }
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; }
Image::Image(const string& filename) : _data(NULL) { const boost::regex normalmap_pattern(rtr::kNormalMapFormat()); if (!devil_initialized) { ilInit(); ilEnable(IL_ORIGIN_SET); ilOriginFunc(IL_ORIGIN_LOWER_LEFT); devil_initialized = true; } ILuint il_image; ilGenImages(1, &il_image); ilBindImage(il_image); ILboolean success = ilLoadImage(filename.c_str()); if (!success) { cerr << "Image::Image " << "Failed to load image file " << filename << endl; _data = NULL; return; } _dimensions = 2; _format = ilGetInteger(IL_IMAGE_FORMAT); if (_format == IL_LUMINANCE) { _format = GL_RED; } _type = ilGetInteger(IL_IMAGE_TYPE); _width = ilGetInteger(IL_IMAGE_WIDTH); _height = ilGetInteger(IL_IMAGE_HEIGHT); _depth = 0; _bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); _data = malloc(_bpp * _width * _height); if (boost::regex_match(filename, normalmap_pattern)) { _internal_format = get_linear_format(_format, _type); } else { _internal_format = get_internal_format(_format, _type); } ilCopyPixels(0, 0, 0, _width, _height, 1, ilGetInteger(IL_IMAGE_FORMAT), _type, _data); ilBindImage(0); ilDeleteImages(1, &il_image); }
void TextField::blit(char letter, int x, int y) { int srcX = letter & 0x0F; int srcY = (letter & 0xF0) >> 4; ilBindImage(m_font_id); ilCopyPixels((ILuint)(srcX * m_char_width), (ILuint)(srcY * m_char_height), 0, (ILuint)m_char_width, (ILuint)m_char_height, 1, IL_RGBA, IL_UNSIGNED_BYTE, m_temp); ilBindImage(m_image_id); ilSetPixels((ILint)(x * m_char_width), (ILint)(y * m_char_height), 0, (ILuint)m_char_width, (ILuint)m_char_height, 1, IL_RGBA, IL_UNSIGNED_BYTE, m_temp); }
ILuint ImageManager::ClipImage(ILuint SourceID, ILuint X, ILuint Y, ILuint W, ILuint H) { ILuint ImageID; ilGenImages(1, &ImageID); ilBindImage(ImageID); ilTexImage(W, H, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL); uint8_t* NewImageData = ilGetData(); ilBindImage(SourceID); ilCopyPixels(X, Y, 0, W, H, 1, IL_BGRA, IL_UNSIGNED_BYTE, NewImageData); return ImageID; }
ILboolean iluCrop3D(ILuint XOff, ILuint YOff, ILuint ZOff, ILuint Width, ILuint Height, ILuint Depth) { ILuint x, y, z, c, OldBps, OldPlane; ILubyte *Data; ILenum Origin; iluCurImage = ilGetCurImage(); if (iluCurImage == NULL) { ilSetError(ILU_ILLEGAL_OPERATION); return IL_FALSE; } // Uh-oh, what about 0 dimensions?! if (Width > iluCurImage->Width || Height > iluCurImage->Height || Depth > iluCurImage->Depth) { ilSetError(ILU_ILLEGAL_OPERATION); return IL_FALSE; } Data = (ILubyte*)ialloc(iluCurImage->SizeOfData); if (Data == NULL) { return IL_FALSE; } OldBps = iluCurImage->Bps; OldPlane = iluCurImage->SizeOfPlane; Origin = iluCurImage->Origin; ilCopyPixels(0, 0, 0, iluCurImage->Width, iluCurImage->Height, iluCurImage->Depth, iluCurImage->Format, iluCurImage->Type, Data); if (!ilTexImage(Width - XOff, Height - YOff, Depth - ZOff, iluCurImage->Bpp, iluCurImage->Format, iluCurImage->Type, NULL)) { ifree(Data); } iluCurImage->Origin = Origin; for (z = 0; z < iluCurImage->Depth; z++) { for (y = 0; y < iluCurImage->Height; y++) { for (x = 0; x < iluCurImage->Bps; x += iluCurImage->Bpp) { for (c = 0; c < iluCurImage->Bpp; c++) { iluCurImage->Data[z * iluCurImage->SizeOfPlane + y * iluCurImage->Bps + x + c] = Data[(z + ZOff) * OldPlane + (y + YOff) * OldBps + (x + XOff) + c]; } } } } ifree(Data); return IL_TRUE; }
ILboolean iluCrop2D(ILuint XOff, ILuint YOff, ILuint Width, ILuint Height) { ILuint x, y, c, OldBps; ILubyte *Data; ILenum Origin; iluCurImage = ilGetCurImage(); if (iluCurImage == NULL) { ilSetError(ILU_ILLEGAL_OPERATION); return IL_FALSE; } // Uh-oh, what about 0 dimensions?! if (Width > iluCurImage->Width || Height > iluCurImage->Height) { ilSetError(ILU_ILLEGAL_OPERATION); return IL_FALSE; } Data = (ILubyte*)ialloc(iluCurImage->SizeOfData); if (Data == NULL) { return IL_FALSE; } OldBps = iluCurImage->Bps; Origin = iluCurImage->Origin; ilCopyPixels(0, 0, 0, iluCurImage->Width, iluCurImage->Height, 1, iluCurImage->Format, iluCurImage->Type, Data); if (!ilTexImage(Width, Height, iluCurImage->Depth, iluCurImage->Bpp, iluCurImage->Format, iluCurImage->Type, NULL)) { free(Data); return IL_FALSE; } iluCurImage->Origin = Origin; // @TODO: Optimize! (Especially XOff * iluCurImage->Bpp...get rid of it!) for (y = 0; y < iluCurImage->Height; y++) { for (x = 0; x < iluCurImage->Bps; x += iluCurImage->Bpp) { for (c = 0; c < iluCurImage->Bpp; c++) { iluCurImage->Data[y * iluCurImage->Bps + x + c] = Data[(y + YOff) * OldBps + x + XOff * iluCurImage->Bpp + c]; } } } ifree(Data); return IL_TRUE; }
void QTWindow::setupWindow(QTWindow * window, const char * title, const char * imagePath) { ilInit(); ilLoadImage((const ILstring)imagePath); imageInputWidth = (int) ilGetInteger(IL_IMAGE_WIDTH); imageInputHeight = (int) ilGetInteger(IL_IMAGE_HEIGHT); pixmapInput = new BYTE[3 * imageInputWidth * imageInputHeight]; ilCopyPixels(0, 0, 0, imageInputWidth, imageInputWidth, 1, IL_RGB, IL_UNSIGNED_BYTE, pixmapInput); window->setWindowTitle(QString::fromUtf8(title)); window->resize(imageInputWidth, imageInputHeight); QPixmap pm(imagePath); window->setPixmap(pm); window->show(); }
bool TextureHandler::load_texture(std::wstring file_name) { m_devil_image_id = ilGenImage(); ilBindImage(m_devil_image_id); ilLoadImage(file_name.c_str()); get_DevIL_error(); m_width = ilGetInteger(IL_IMAGE_WIDTH); m_height= ilGetInteger(IL_IMAGE_HEIGHT); ILint format = ilGetInteger(IL_IMAGE_FORMAT); ILint type = ilGetInteger(IL_IMAGE_TYPE); ilLoadImage(file_name.c_str()); get_DevIL_error(); //we want to load unsigned char RGBA images m_data = new unsigned char[m_width * m_height * 4]; ilCopyPixels(0, 0, 0, m_width, m_height, 1, format, type, m_data); get_DevIL_error(); ilDeleteImage(m_devil_image_id); send_to_gpu(); return true; }
void Image::loadImage(const char *filename) { ILboolean ok; ilGenImages(1, &image); CHECK_IL_ERROR("ilGenImages") ilBindImage(image); CHECK_IL_ERROR("ilBindImage") ok = ilLoadImage(filename); if (!ok) { CHECK_IL_ERROR("ilLoadImage") } size.x = ilGetInteger(IL_IMAGE_WIDTH); size.y = ilGetInteger(IL_IMAGE_HEIGHT); buffer = new char[(int)size.x * (int)size.y * 4]; ilCopyPixels(0, 0, 0, size.x, size.y, 1, IL_RGBA, IL_UNSIGNED_BYTE, buffer); }
const Texture* ReadPNG(const std::string& name) { Texture *texture; ILuint img; ilGenImages(1, &img); ilBindImage(img); if(!ilLoadImage(name.c_str())){ ilDeleteImages(1, &img); return nullptr; } iluFlipImage(); texture = new Texture; texture->width = ilGetInteger(IL_IMAGE_WIDTH); texture->height = ilGetInteger(IL_IMAGE_HEIGHT); texture->texels.resize(texture->width * texture->height); ilCopyPixels(0, 0, 0, texture->width, texture->height, 1, IL_RGBA, IL_UNSIGNED_BYTE, &texture->texels[0]); ilDeleteImages(1, &img); return texture; }
bool ImageLoader::loadImage(const char* image) { freeImageData(); // Create the final path... char filePath[260]; sprintf(filePath, "%s%s", m_path, image); // Now let's switch over to using devIL... ILuint handle; // In the next section, we load one image ilGenImages(1, &handle); ilBindImage(handle); const ILboolean loaded = ilLoadImage(filePath); if (loaded == IL_FALSE) { ILenum error = ilGetError(); return false; // Error encountered during loading } // Let’s spy on it a little bit m_width = (u32)ilGetInteger(IL_IMAGE_WIDTH); // getting image width m_height = (u32)ilGetInteger(IL_IMAGE_HEIGHT); // and height // Make sure our buffer is big enough. assert( m_width <= MAX_IMAGE_WIDTH && m_height <= MAX_IMAGE_WIDTH ); // Finally get the image data ilCopyPixels(0, 0, 0, m_width, m_height, 1, IL_RGBA, IL_UNSIGNED_BYTE, m_imageData_Work); // (Now flip the image for OpenGL... does not seem to be necessary so just copy for now) memcpy( m_imageData, m_imageData_Work, 4*m_width*m_height ); // Finally, clean the mess! ilDeleteImages(1, &handle); return true; }
ofColor ofxTexture::GetPixel(ofVec2f position) { if(m_Locked) return ofColor(0.0f, 0.0f, 0.0f); ilBindImage(m_ImageId); ILubyte* data; ofColor color; data = new ILubyte[m_BytePerPixel]; ilCopyPixels(position.x, position.y, 0, 1, 1, 1, m_BytePerPixel==3?IL_RGB:IL_RGBA, IL_UNSIGNED_BYTE, data); color.r = data[0]; color.g = data[1]; color.b = data[2]; if(m_BytePerPixel == 3) { color.a = 255; } else { color.a = data[3]; } delete[] data; return color; }
GameImage* GameResourceManager::loadImgData(DUserClient*,string filename) { ILuint image_id=0; ilGenImages(1,&image_id); ilBindImage(image_id); ILenum type=ilDetermineType(filename.c_str()); if(!ilLoad(type,filename.c_str())) { printf("Failed to load image %s\n",filename.c_str()); ilDeleteImage(image_id); return 0; } int w=ilGetInteger(IL_IMAGE_WIDTH); int h=ilGetInteger(IL_IMAGE_HEIGHT); int d=ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); if(d!=1 && d!=2 && d!=3 && d!=4) { printf("Unupported image depth %d, image %s\n",d,filename.c_str()); ilDeleteImage(image_id); return 0; } if(d==2) d=1; char *data=new char[w*h*d]; ILenum formats[]={0,IL_LUMINANCE,IL_LUMINANCE,IL_RGB,IL_RGBA}; ilCopyPixels(0,0,0,w,h,1,formats[d],IL_UNSIGNED_BYTE,data); ilDeleteImage(image_id); GameImage* img=new GameImage(); img->data=data; img->width=w; img->height=h; img->depth=d; return img; }
/** * Loads an image into memory from file */ static void loadImage(std::string filename, texture& tex) { ILuint ImageName; ilGenImages(1, &ImageName); // Grab a new image name. ilBindImage(ImageName); if (!ilLoadImage(filename.c_str())) { ILenum e; std::stringstream error; while ((e = ilGetError()) != IL_NO_ERROR) { error << e << ": " << iluErrorString(e) << std::endl; } ilDeleteImages(1, &ImageName); // Delete the image name. throw std::runtime_error(error.str()); } tex.width = ilGetInteger(IL_IMAGE_WIDTH); // getting image width tex.height = ilGetInteger(IL_IMAGE_HEIGHT); // and height tex.data.resize(tex.width*tex.height*3); ilCopyPixels(0, 0, 0, tex.width, tex.height, 1, IL_RGB, IL_FLOAT, tex.data.data()); ilDeleteImages(1, &ImageName); // Delete the image name. }
int load_image( texture_image_t * image, int mipmaps ) //, float gamma ) { int format = 0; ILboolean rv; texture_init(); ilGenImages(1, &image->id); ilBindImage(image->id); rv = ilLoadImage(image->path); if (rv == IL_false) return -1; if(mipmaps==0) { rv = iluBuildMipmaps(); if (rv == IL_false) return -1; } //else //{ // // TODO - use iluScale to ensure power of two //} //rv = iluGammaCorrect( gamma ); //if (rv == IL_false) // return -1; image->w = ilGetInteger(IL_IMAGE_WIDTH); image->h = ilGetInteger(IL_IMAGE_HEIGHT); format = ilGetInteger(IL_IMAGE_FORMAT); // by projectx convention // textures that we want to color key are rbga if(format == IL_RGBA) image->colorkey = 1; // true else image->colorkey = 0; // false image->size = image->w * image->h * IMAGE_COMPONENTS * sizeof(unsigned char); image->data = (ILubyte *)malloc(image->size); ilCopyPixels(0, 0, 0, image->w, image->h, 1, IL_RGBA, IL_UNSIGNED_BYTE, image->data); return 0; }
ILboolean GenSides() { ILubyte *Buffer, *Data, Bpp, Bpc; ILuint TempImage; ILenum Format, Type; ILint SizePlane, Bps, c, y, z, i; ilActiveImage(ActiveImage); Bpp = ilGetInteger(IL_IMAGE_BPP); Bpc = ilGetInteger(IL_IMAGE_BPC); Format = ilGetInteger(IL_IMAGE_FORMAT); Type = ilGetInteger(IL_IMAGE_TYPE); // Front TexID1 = ilutGLBindTexImage(); Width = ilGetInteger(IL_IMAGE_WIDTH); Height = ilGetInteger(IL_IMAGE_HEIGHT); Depth = ilGetInteger(IL_IMAGE_DEPTH); ilGenImages(1, &TempImage); SizePlane = ilGetInteger(IL_IMAGE_PLANESIZE); SizePlane = Width * Height * Bpp * Bpc; Bps = Width * Bpp * Bpc; Data = ilGetData(); // Left i = 0; Buffer = (ILubyte*)malloc(Height * Depth * Bpp * Bpc); for (y = 0; y < Height; y++) { for (z = 0; z < Depth; z++) { for (c = 0; c < Bpp * Bpc; c++) { Buffer[i++] = Data[z * SizePlane + y * Bps + c]; } } } ilBindImage(TempImage); ilTexImage(Depth, Height, 1, Bpp, Format, Type, Buffer); TexID2 = ilutGLBindTexImage(); free(Buffer); // Right ilBindImage(ImgId); ilActiveImage(ActiveImage); i = 0; Buffer = (ILubyte*)malloc(Height * Depth * Bpp * Bpc); for (y = 0; y < Height; y++) { for (z = 0; z < Depth; z++) { for (c = 0; c < Bpp * Bpc; c++) { Buffer[i++] = Data[z * SizePlane + y * Bps + (Width - 1) * Bpp * Bpc + c]; } } } ilBindImage(TempImage); ilTexImage(Depth, Height, 1, Bpp, Format, Type, Buffer); TexID3 = ilutGLBindTexImage(); free(Buffer); // Back ilBindImage(ImgId); ilActiveImage(ActiveImage); Buffer = (ILubyte*)malloc(Width * Height * Bpp * Bpc); ilCopyPixels(0, 0, Depth-1, Width, Height, 1, Format, Type, Buffer); ilBindImage(TempImage); ilTexImage(Width, Height, 1, Bpp, Format, Type, Buffer); TexID4 = ilutGLBindTexImage(); free(Buffer); //ilDeleteImages(1, &ImgId); ilDeleteImages(1, &TempImage); ilBindImage(ImgId); return IL_TRUE; }
// Second part of 2 stage image loader... void DevilLoad(nat32 handle,nat32 width,nat32 height,nat32 format,nat32 type,void * data) { ilBindImage(handle); ilCopyPixels(0,0,0,width,height,1,format,type,data); ilDeleteImages(1,(unsigned int *)&handle); }
void Config::readSetOfSlices( const std::vector<std::string>& files, unsigned int downx, unsigned int downy, unsigned int downz ) { ILuint image; ilGenImages( 1, &image ); ilBindImage( image); ILenum type, bpp; switch( m_function_format ) { case FUNC_FLOAT: m_function_iso_min = 0; m_function_iso_max = 0xfff; m_function_isovalue = 0x200; type = IL_FLOAT; bpp = 4; break; case FUNC_UNSIGNED_BYTE: m_function_iso_min = 0; m_function_iso_max = 0xff; m_function_isovalue = 0x20; type = IL_UNSIGNED_BYTE; bpp = 1; break; case FUNC_UNSIGNED_SHORT: m_function_iso_min = 0; m_function_iso_max = 0xfff; m_function_isovalue = 0x200; type = IL_UNSIGNED_SHORT; bpp = 2; break; } int w, h; for(int i=0; i<files.size(); i++) { std::vector<char> filename(files[i].begin(), files[i].end()); filename.push_back('\0'); if( ilLoadImage( &filename[0] ) == IL_FALSE ) { std::cerr << __func__ << ": failed to read image " << files[i] << "\n"; exit( EXIT_FAILURE ); } std::cerr << __func__ << ": read " << files[i] << "\n"; if( i==0 ) { w = ilGetInteger( IL_IMAGE_WIDTH ); h = ilGetInteger( IL_IMAGE_HEIGHT ); std::cerr << __func__ << ": input = [ " << w << " x " << h << "]\n"; std::cerr << __func__ << ": type = " << type << "\n"; std::cerr << __func__ << ": bpp = " << bpp << "\n"; m_function_tsize_log2 = (unsigned int)ceilf(log2(max(w,h))); m_function_tsize = 1<<m_function_tsize_log2; m_function_slices = files.size(); allocFuncMem(); } ilCopyPixels(0, 0, 0, m_function_tsize, m_function_tsize, 1, IL_LUMINANCE, type, (unsigned char*)m_function_data_ + bpp*m_function_tsize*m_function_tsize*i ); } ilDeleteImages( 1, &image ); }
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; } }
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; }
void CGuildMarkImage::GetData(UINT x, UINT y, UINT width, UINT height, void * data) { ilBindImage(m_uImg); ilCopyPixels(x, y, 0, width, height, 1, IL_BGRA, IL_UNSIGNED_BYTE, data); }
bool TextureAtlas::Generate(int textureSize, bool mipmap) { // TODO mipmap pas encore 100% parfait... assert(!mipmap); if (!IsPowerOfTwo(textureSize)) return false; // Initialize Devil only once: static bool alreadyInitialized = false; if (!alreadyInitialized) { ilInit(); iluInit(); alreadyInitialized = true; } for (TextureList::iterator it = m_textureList.begin(); it != m_textureList.end(); ++it) { ILuint texid = it->second.texId; if (texid == (ILuint)-1) { std::cout << "Loading " << it->first << " (id=" << it->second.texIdx << ")..." << std::endl; ilGenImages(1, &texid); ilBindImage(texid); ilOriginFunc(IL_ORIGIN_LOWER_LEFT); ilEnable(IL_ORIGIN_SET); if (!ilLoadImage((const ILstring)it->first.c_str())) return false; if (!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE)) return false; iluScale(textureSize, textureSize, 1); it->second.texId = texid; } } //std::cout << ilGetInteger(IL_IMAGE_BPP) << std::endl; //std::cout << ilGetInteger(IL_IMAGE_FORMAT) << std::endl; //std::cout << ilGetInteger(IL_IMAGE_DEPTH) << std::endl; //std::cout << ilGetInteger(IL_IMAGE_TYPE) << std::endl; //std::cout << ilGetInteger(IL_IMAGE_WIDTH) << std::endl; //std::cout << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl; glGenTextures(1, &m_textureId); glBindTexture(GL_TEXTURE_2D, m_textureId); if (mipmap) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } int level = textureSize; int oglLevel = 0; int mipmapSize = textureSize * m_nbTexturePerSide; while (mipmapSize != 0) { ILuint atlasTex; ilGenImages(1, &atlasTex); ilBindImage(atlasTex); ilTexImage(mipmapSize, mipmapSize, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, 0); ilClearColour(1, 0, 0, 1); ilClearImage(); for (TextureList::iterator it = m_textureList.begin(); it != m_textureList.end(); ++it) { ILuint tmpImg; ilGenImages(1, &tmpImg); ilBindImage(tmpImg); ilCopyImage(it->second.texId); iluImageParameter(ILU_FILTER, ILU_NEAREST); //iluImageParameter(ILU_FILTER, ILU_BILINEAR); if (level != textureSize) iluScale(level, level, 1); char* data = new char[level * level * 4]; ilCopyPixels(0, 0, 0, level, level, 1, IL_RGBA, IL_UNSIGNED_BYTE, data); int imgIdx = it->second.texIdx; int x = imgIdx % m_nbTexturePerSide; int y = m_nbTexturePerSide - 1 - imgIdx / m_nbTexturePerSide; ilBindImage(atlasTex); ilSetPixels(x * level, y * level, 0, level, level, 1, IL_RGBA, IL_UNSIGNED_BYTE, data); //ilOverlayImage(tmpImg, x * level, y * level, 0); delete[] data; ilDeleteImages(1, &tmpImg); } // TODO //if(level == textureSize) //{ //ilEnable(IL_FILE_OVERWRITE); //ilSaveImage("textureatlas.png"); //} //std::cout << oglLevel << ":" << level << ":" << mipmapSize << std::endl; glTexImage2D(GL_TEXTURE_2D, oglLevel++, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData()); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); CHECK_GL_ERROR(); ilDeleteImages(1, &atlasTex); if (!mipmap) break; level /= 2; mipmapSize /= 2; } m_isValid = true; return true; }