void CBitmap::Save(string const& filename) { ilOriginFunc(IL_ORIGIN_UPPER_LEFT); ilEnable(IL_ORIGIN_SET); unsigned char* buf=new unsigned char[xsize*ysize*4]; /* HACK Flip the image so it saves the right way up. (Fiddling with ilOriginFunc didn't do anything?) Duplicated with ReverseYAxis. */ for(int y=0;y<ysize;++y){ for(int x=0;x<xsize;++x){ buf[((ysize-1-y)*xsize+x)*4+0]=mem[((y)*xsize+x)*4+0]; buf[((ysize-1-y)*xsize+x)*4+1]=mem[((y)*xsize+x)*4+1]; buf[((ysize-1-y)*xsize+x)*4+2]=mem[((y)*xsize+x)*4+2]; buf[((ysize-1-y)*xsize+x)*4+3]=mem[((y)*xsize+x)*4+3]; } } ilHint(IL_COMPRESSION_HINT, IL_USE_COMPRESSION); ilSetInteger (IL_JPG_QUALITY, 80); ILuint ImageName = 0; ilGenImages(1, &ImageName); ilBindImage(ImageName); ilTexImage(xsize,ysize,1,4,IL_RGBA,IL_UNSIGNED_BYTE,NULL); ilSetData(buf); ilSaveImage((char*)filename.c_str()); ilDeleteImages(1,&ImageName); delete[] buf; }
bool M_saveImage(const char * filename, void * data, unsigned int quality) { DevILInit(); ILuint ImgId = 0; ilGenImages(1, &ImgId); // bind this image name. ilBindImage(ImgId); MImage * image = (MImage *)data; unsigned int width = image->getWidth(); unsigned int height = image->getHeight(); unsigned int components = image->getComponents(); if(components == 3) ilTexImage(width, height, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, image->getData()); else if(components == 4) ilTexImage(width, height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, image->getData()); iluFlipImage(); if(quality < 100) ilSetInteger(IL_JPG_QUALITY, quality); ilEnable(IL_FILE_OVERWRITE); ilSaveImage(filename); ilDeleteImages(1, &ImgId); DevILShutDown(); return true; }
ImagesLoader::ImagesLoader(){ ilInit(); ilOriginFunc(IL_ORIGIN_UPPER_LEFT); ilEnable(IL_ORIGIN_SET); ilEnable(IL_FILE_OVERWRITE); ilSetInteger(IL_FORMAT_MODE, IL_BGRA); ilEnable(IL_FORMAT_SET); }
bool TextureControl::SaveToTexSet(string imageName, string basepath, string setName, string channel, string subset) { TargetTextureSet* tts = TexSetFromName(setName); if(!tts) return false; if(namedImages.find(imageName) == namedImages.end()) return false; ilBindImage(namedImages[imageName]); string outputPath = FilePathForTexSet(setName, channel, subset); outputPath = basepath + outputPath; ilEnable(IL_FILE_OVERWRITE); bool hasAlpha = tts->SubSetHasAlpha(channel,subset); string format = tts->SubSetFormat(channel, subset); if(format == "DXT1") { ilEnable(IL_SQUISH_COMPRESS); ilSetInteger(IL_DXTC_FORMAT, IL_DXT1); } else if(format == "DXT5") { ilEnable(IL_SQUISH_COMPRESS); ilSetInteger(IL_DXTC_FORMAT, IL_DXT5); } else if(format == "RGB") { ilSetInteger(IL_DXTC_FORMAT, IL_DXT_NO_COMP); if(ilGetInteger(IL_IMAGE_FORMAT) == IL_RGBA) ilConvertImage(IL_RGB,ilGetInteger(IL_IMAGE_TYPE)); } else if(format == "RGBA") { ilSetInteger(IL_DXTC_FORMAT, IL_DXT_NO_COMP); } size_t pos= outputPath.rfind("\\"); if(pos != string::npos) { SHCreateDirectoryEx(NULL,outputPath.substr(0,pos).c_str(),NULL); } if(!ilSaveImage((const ILstring)outputPath.c_str())) { return false; } return true; }
bool CBitmap::Save(std::string const& filename, bool opaque) const { if (type == BitmapTypeDDS) { #ifndef BITMAP_NO_OPENGL return ddsimage->save(filename); #else return false; #endif // !BITMAP_NO_OPENGL } unsigned char* buf = new unsigned char[xsize * ysize * 4]; const int ymax = (ysize - 1); /* HACK Flip the image so it saves the right way up. (Fiddling with ilOriginFunc didn't do anything?) Duplicated with ReverseYAxis. */ for (int y = 0; y < ysize; ++y) { for (int x = 0; x < xsize; ++x) { const int bi = 4 * (x + (xsize * (ymax - y))); const int mi = 4 * (x + (xsize * (y))); buf[bi + 0] = mem[mi + 0]; buf[bi + 1] = mem[mi + 1]; buf[bi + 2] = mem[mi + 2]; buf[bi + 3] = opaque ? 0xff : mem[mi + 3]; } } boost::mutex::scoped_lock lck(devilMutex); ilOriginFunc(IL_ORIGIN_UPPER_LEFT); ilEnable(IL_ORIGIN_SET); ilHint(IL_COMPRESSION_HINT, IL_USE_COMPRESSION); ilSetInteger(IL_JPG_QUALITY, 80); ILuint ImageName = 0; ilGenImages(1, &ImageName); ilBindImage(ImageName); ilTexImage(xsize, ysize, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, buf); const std::string fullpath = dataDirsAccess.LocateFile(filename, FileQueryFlags::WRITE); const bool success = ilSaveImage((char*)fullpath.c_str()); ilDeleteImages(1, &ImageName); ilDisable(IL_ORIGIN_SET); delete[] buf; return success; }
Renderer::Renderer() { IniReader reader( "common/ini/main.ini" ); m_bCheckerror = reader.GetInt( "Window", "GLCheckError" ) != 0; GLenum err = glewInit(); CHECK( err == GLEW_OK, "GLEW unable to initialize!" ); // print info /*char buffer[256]; GetInfo(buffer, sizeof(buffer)); printf("%s\n", buffer);*/ ilInit(); iluInit(); ilEnable(IL_ORIGIN_SET); ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_LOWER_LEFT); m_shaderProgram = NULL; }
int main(int argc, char **argv) { ILuint image = 0; ILuint reference = 0; // syntax: ILtestAlgoQuant <reference> if (argc < 2) { return -1; } ilInit(); iluInit(); ilEnable(IL_ORIGIN_SET); // flip image on load if necessary ilEnable(IL_FILE_MODE); // overwrite files // load reference image ilGenImages(1, &reference); CHECK(reference != 0); CHECK(testLoadImage(argv[1], reference)); // duplicate image = ilCloneCurImage(); CHECK(image != 0); // quantize ilBindImage(image); ilSetInteger(IL_QUANTIZATION_MODE, IL_WU_QUANT); ilConvertImage(IL_COLOUR_INDEX, IL_UNSIGNED_BYTE); CHECK(testSaveImage("test_quant.png", image)); // compare two images CHECK_GREATER(iluSimilarity(reference), 0.98f); // cleanup ilDeleteImages(1, &image); ilDeleteImages(1, &reference); ilShutDown(); return 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; }
void GeometryTerrain::computeLightmap(Vector3 _vlightSource, bool update) { bool bIntegrateNormals = false; std::vector<GLubyte> shadowMapTexture(GetWidth()*GetWidth()*4); float maxHeight = -99999.0f; for(int z =0; z <= GetLength(); ++z) for(int x = 0; x <= GetWidth(); ++x) maxHeight = max(getHeight(x,z), maxHeight); for(int z =0; z <= GetLength(); ++z) { for(int x = 0; x <= GetWidth(); ++x) { float ambientLight = 255; Ray lightRay(Vector3(x, getHeight(x, z), z), _vlightSource ); Vector3 current_ray_pos(Vector3(x, getHeight(x, z), z)); Vector3 direction_to_sun = lightRay.m_v3Direction.normalize(); int numRayHits = 0; while(!(current_ray_pos.x <= 0 || current_ray_pos.x >= GetWidth() || current_ray_pos.z <= 0 || current_ray_pos.z >= GetWidth() )) { if(current_ray_pos.y > maxHeight) break; // Is the terrain blocking the ray at this point? if(getHeight((int)floor(current_ray_pos.x), (int)floor(current_ray_pos.z)) > current_ray_pos.y) { numRayHits++; break; } //light still traveling... current_ray_pos += direction_to_sun; } float ambientLightNormals = 0; if(bIntegrateNormals) { Vector3 n( m_pNormals[x+z * (GetWidth()+1)].x, m_pNormals[x+z * (GetWidth()+1)].y, m_pNormals[x+z * (GetWidth()+1)].z ); ambientLightNormals = 0.5*( 1.0f + dot(n.normalize(), direction_to_sun) ); if(ambientLightNormals > 1.0f) ambientLightNormals = 1.0f; if(ambientLightNormals < 0.0f) ambientLightNormals = 0.0f; } if(numRayHits > 0) { //ambientLight = (current_ray_pos - Vector3(x,getHeight(x,z),z)).magnitude() - ambientLightNormals * 255; ambientLight = 170; if(ambientLight > 255) ambientLight = 255; if(ambientLight < 170) ambientLight = 170; } int index = (x + z * GetWidth()) * 3; for (int i = 0; i < 3; ++i) { shadowMapTexture[index + i] = (GLubyte)ambientLight; } } } for(int z =0; z <= GetLength(); ++z) { for(int x = 0; x <= GetWidth(); ++x) { int factor = 2; ColorOGL colCurrent; colCurrent.m_fRed = shadowMapTexture[(x + z * GetWidth()) * 3 + 0]; colCurrent.m_fGreen = shadowMapTexture[(x + z * GetWidth()) * 3 + 1]; colCurrent.m_fBlue = shadowMapTexture[(x + z * GetWidth()) * 3 + 2]; ColorOGL colT; ColorOGL colD; ColorOGL colL; ColorOGL colR; if(shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 2) ) { colT.m_fRed = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 0]; colT.m_fGreen = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 1]; colT.m_fBlue = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 2) ) { colD.m_fRed = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 0]; colD.m_fGreen = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 1]; colD.m_fBlue = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > ( (x+factor + z * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x+factor + z * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x+factor + z * GetWidth()) * 3 + 2) ) { colL.m_fRed = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 0]; colL.m_fGreen = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 1]; colL.m_fBlue = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > (( x-factor + z * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x-factor + z * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x-factor + z * GetWidth()) * 3 + 2) ) { colR.m_fRed = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 0]; colR.m_fGreen = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 1]; colR.m_fBlue = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 2]; } shadowMapTexture[(x + z * GetWidth()) * 3 + 0] = (colT.m_fRed+colD.m_fRed+colR.m_fRed+colL.m_fRed+colCurrent.m_fRed)/5; shadowMapTexture[(x + z * GetWidth()) * 3 + 1] = (colT.m_fGreen+colD.m_fGreen+colR.m_fGreen+colL.m_fGreen+colCurrent.m_fGreen)/5; shadowMapTexture[(x + z * GetWidth()) * 3 + 2] = (colT.m_fBlue+colD.m_fBlue+colR.m_fBlue+colL.m_fBlue+colCurrent.m_fBlue)/5; } } if(update) { ResourceManager::getInstance()->getTexture2D("resources/textures/lightmap.tga")->bind(2); glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, GetWidth(), GetWidth(), GL_RGBA, GL_UNSIGNED_BYTE, &shadowMapTexture[0]); } else { ilTexImage(GetWidth(),GetWidth(), 1, 3, IL_RGB, IL_UNSIGNED_BYTE, &shadowMapTexture[0]); ilSetData(&shadowMapTexture[0]); ilSetInteger(IL_IMAGE_BITS_PER_PIXEL,32); ilEnable(IL_FILE_OVERWRITE); ilSave(IL_TGA, "resources/textures/lightmap.tga"); } }