void MMKP_BBA::competitiveUpdateSol (MMKPBatSolution& sol, MMKPBatSolution& newSol){ dataSet.updateSolution(sol.solution); dataSet.updateSolution(newSol.solution); bool solFeas = this->dataSet.isFeasible(sol.solution); bool newFeas = this->dataSet.isFeasible(newSol.solution); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dib(0, 1); if(dib(gen) < sol.a){ //if both feasible, choose best profit if(newFeas && solFeas){ if((newSol.solution).getProfit() > (sol.solution).getProfit()){ sol = newSol; sol.a = sol.a - (sol.a * .05); sol.r = sol.r + (sol.r * .05); } //if one is feasible, that automatically is choosen }else if((!solFeas) && newFeas){ sol.a = sol.a - (sol.a * .05); sol.r = sol.r + (sol.r * .05); sol = newSol; //if neither is feasible, choose greatest profit. }else if ((!solFeas && !newFeas)&&(newSol.solution.getProfit() > sol.solution.getProfit())){ sol = newSol; } } }
void MMKP_BBA::globalSearch(std::vector<MMKPBatSolution>& population){ //already in sorted order in this implementation. //MMKP_BBA::quickSort(population,0,(population.size()-1)); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dib(0, 1); MMKPBatSolution bestBat = population[0]; ReactiveLocalSearch RLS(dataSet); bestBat.solution = RLS(bestBat.solution); this->currentFuncEvals += RLS.getFuncEvals(); for(int i=0;i<population.size();i++){ MMKPBatSolution currentBat = population[i]; //equation 1 float B = dib(gen); float fTemp = currentBat.fmax + ((currentBat.fmax -currentBat.fmin)*B); //equation 2 float averageV = 0.0; int averageVSize = 0; for(int j=0;j<currentBat.solution.size();j++){ for(int k=0;k<currentBat.solution[j].size();k++){ this->currentFuncEvals++; float vTemp = currentBat.v + ((currentBat.solution[j][k] -bestBat.solution[j][k])*fTemp); averageV += vTemp; averageVSize++; //equation "3"--binary uses 7 and 8 float normalizedV = 1/(1+(exp(-(vTemp)))); float r = dib(gen); if(r <= normalizedV){ currentBat.solution[j][k] = 1; }else{ currentBat.solution[j][k] = 0; } } } currentBat.v = (averageV/averageVSize); MMKP_MetaHeuristic::makeFeasible(currentBat.solution); if(dib(gen) > currentBat.r){ int localSolutionIndex = rand() % 10; MMKPBatSolution bestBat = population[localSolutionIndex]; MMKP_BBA::localSearch(bestBat,currentBat); }else{ MMKP_BBA::Mutate(currentBat); } //MMKP_BBA::randomSearch(currentBat); MMKP_BBA::competitiveUpdateSol(population[i],currentBat); } }
void MMKP_BBA::randomSearch(MMKPBatSolution& sol){ std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dib(0, 1); //generate new random solution MMKPSolution newSol = sol.solution; for(int i=0;i<newSol.size();i++){ int randomIndx = rand() % newSol[i].size(); newSol[i].at(randomIndx) = true; } MMKP_MetaHeuristic::makeFeasible(newSol); //update sol float r_1 = dib(gen); if(r_1 < sol.a){ bool updateAandR = false; float rateOfChange = .85; dataSet.updateSolution(sol.solution); dataSet.updateSolution(newSol); bool solFeas = this->dataSet.isFeasible(sol.solution); bool newFeas = this->dataSet.isFeasible(newSol); //if both feasible, choose best profit if(newFeas && solFeas){ if((newSol).getProfit() > (sol.solution).getProfit()){ sol.solution = newSol; updateAandR = true; } //if one is feasible, that automatically is choosen }else if((!solFeas) && newFeas){ sol.solution = newSol; updateAandR = true; //if neither is feasible, choose greatest profit. }else if ((!solFeas && !newFeas)&&(newSol.getProfit() > sol.solution.getProfit())){ sol.solution = newSol; updateAandR = true; } if(updateAandR){ sol.r = sol.r_0 * (1.0-(exp(-(this->currentGeneration*rateOfChange)))); sol.a = sol.a * rateOfChange; } } }
bool TextureManager::LoadTexture(const char *filename, const unsigned int texID, bool generate, GLenum target, GLenum image_format, GLint internal_format, GLint level, GLint border) { // image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; // pointer to the image, once loaded FIBITMAP *dib(0); // pointer to the image data BYTE *bits(0); // image width and height unsigned int width(0), height(0); // OpenGL's image ID to map to GLuint gl_texID; // check the file signature and deduce its format fif = FreeImage_GetFileType(filename, 0); // if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); // if still unkown, return failure if (fif == FIF_UNKNOWN) return false; // check that the plugin has reading capabilities and load the file if (FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); // if the image failed to load, return failure if (!dib) return false; // retrieve the image data bits = FreeImage_GetBits(dib); // get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); // if this somehow one of these failed (they shouldn't), return failure if ((bits == 0) || (width == 0) || (height == 0)) return false; // if this texture ID is in use, unload the current texture if (m_texID.find(texID) != m_texID.end()) glDeleteTextures(1, &(m_texID[texID])); if (generate) { // generate an OpenGL texture ID for this texture glGenTextures(1, &gl_texID); // store the texture ID mapping m_texID[texID] = gl_texID; // bind to the new texture ID glBindTexture(target, gl_texID); } // store the texture data for OpenGL use glTexImage2D(target, level, internal_format, width, height, border, image_format, GL_UNSIGNED_BYTE, bits); // Free FreeImage's copy of the data FreeImage_Unload(dib); // return success return true; }
int _tmain(int argc, _TCHAR* argv[]) { const char* filename = "1.jpg"; FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename, 0); FIBITMAP *dib(0); //pointer to the image data BYTE* bits(0); //image width and height unsigned int width(0), height(0); if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); if(fif == FIF_UNKNOWN) return 1; if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); //retrieve the image data bits = FreeImage_GetBits(dib); //get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); //if this somehow one of these failed (they shouldn't), return failure if((bits == 0) || (width == 0) || (height == 0)) return 1; return 0; }
static Dib logo2dib(Application* application, unsigned char* logo, size_t logoSize, int width, int height) { uLong imageLen = width * height * 4; unsigned char* image = (unsigned char*)malloc(imageLen); uncompress(image, &imageLen, logo, logoSize); Dib dib(application, width, height, true); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { int index = (x + y * width) * 4; unsigned int r = image[index + 0]; unsigned int g = image[index + 1]; unsigned int b = image[index + 2]; unsigned int a = image[index + 3]; unsigned char rgba[] = {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a}; dib.setPixel(x, y, rgba); } free(image); return dib; }
bool CTexture::loadTexture2D(std::string sTexturePath, bool bGenerateMipMaps) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); fif = FreeImage_GetFileType(sTexturePath.c_str(), 0); // 检查文件签名,推导其格式 if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(sTexturePath.c_str()); // 从扩展名猜测格式 if(fif == FIF_UNKNOWN) return false; //clock_t begin = clock(); if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, sTexturePath.c_str()); if(!dib) return false; //clock_t end = clock(); //cout << end - begin << endl; GLubyte* bDataPointer = FreeImage_GetBits(dib); if(bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0) return false; GLenum format = FreeImage_GetBPP(dib) == 24 ? GL_BGR : FreeImage_GetBPP(dib) == 8 ? GL_LUMINANCE : 0; createFromData(bDataPointer, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), FreeImage_GetBPP(dib), format, bGenerateMipMaps); FreeImage_Unload(dib); m_sTexturePath = sTexturePath; return true; }
bool CTexture::loadTexture2D(string a_sPath, bool bGenerateMipMaps) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); fif = FreeImage_GetFileType(a_sPath.c_str(), 0); // Check the file signature and deduce its format if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension fif = FreeImage_GetFIFFromFilename(a_sPath.c_str()); if(fif == FIF_UNKNOWN) // If still unknown, return failure return false; if(FreeImage_FIFSupportsReading(fif)) // Check if the plugin has reading capabilities and load the file dib = FreeImage_Load(fif, a_sPath.c_str()); if(!dib) return false; BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data // If somehow one of these failed (they shouldn't), return failure if(bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0) return false; GLenum format = FreeImage_GetBPP(dib) == 24 ? GL_BGR : FreeImage_GetBPP(dib) == 8 ? GL_LUMINANCE : 0; createFromData(bDataPointer, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), FreeImage_GetBPP(dib), format, bGenerateMipMaps); FreeImage_Unload(dib); sPath = a_sPath; return true; // Success }
bool LoadTexture(const char* filename, //where to load the file from GLuint &texID, //does not have to be generated with glGenTextures GLenum image_format, //format the image is in GLint internal_format, //format to store the image in GLint level , //mipmapping level GLint border ) { //image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //pointer to the image, once loaded FIBITMAP *dib(0); //pointer to the image data BYTE* bits(0); //image width and height unsigned int width(0), height(0); //check the file signature and deduce its format fif = FreeImage_GetFileType(filename, 0); //if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); //if still unkown, return failure if (fif == FIF_UNKNOWN) return false; //check that the plugin has reading capabilities and load the file if (FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); //if the image failed to load, return failure if (!dib) return false; //retrieve the image data bits = FreeImage_GetBits(dib); //get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); //if this somehow one of these failed (they shouldn't), return failure if ((bits == 0) || (width == 0) || (height == 0)) return false; //generate an OpenGL texture ID for this texture glGenTextures(1, &texID); //store the texture ID mapping //bind to the new texture ID glBindTexture(GL_TEXTURE_2D, texID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // 线形滤波 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 线形滤波 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //store the texture data for OpenGL use glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height, border, image_format, GL_UNSIGNED_BYTE, bits); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //Free FreeImage's copy of the data FreeImage_Unload(dib); //return success return true; }
/* * \brief Method that import a texture to be used on the model * \param[in] fTexture a constant char array containing the path to the file of the texture * \param[in] tex_number an unsigned integer representing the type of texture (i.e. 0 = Diffuse color, 1 = Normalmap, 2 = Specular Color) * \return a boolean indicating if the import was successful of not */ GLboolean Model::importTexture(const GLchar * fTexture, GLuint tex_number) { GLboolean import_is_ok = GL_FALSE; FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib(0); BYTE* bits(0); GLuint width(0), height(0); fif = FreeImage_GetFileType(fTexture, 0); if (FreeImage_FIFSupportsReading(fif)) { dib = FreeImage_Load(fif, fTexture); dib = FreeImage_ConvertTo32Bits(dib); bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); import_is_ok = GL_TRUE; glBindTexture(GL_TEXTURE_2D, m_texture_id[tex_number]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, &bits[0]); glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); FreeImage_Unload(dib); } return import_is_ok; }
unsigned char* GLES2Texture::loadImage(const char *filename, int& colorDepth) { //image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //pointer to the image, once loaded FIBITMAP *dib(0); //pointer to the image data BYTE* bits(0); //image width and height unsigned int width(0), height(0); //check the file signature and deduce its format fif = FreeImage_GetFileType(filename, 0); //if still unknown, try to guess the file format from the file extension if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); //if still unkown, return failure if(fif == FIF_UNKNOWN) return NULL; //check that the plugin has reading capabilities and load the file if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); //if the image failed to load, return failure if(!dib) return NULL; int datasize = FreeImage_GetDIBSize(dib); //retrieve the image data bits = FreeImage_GetBits(dib); //get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); //get the image bpp colorDepth = FreeImage_GetBPP(dib); //if this somehow one of these failed (they shouldn't), return failure if((bits == 0) || (width == 0) || (height == 0)) return NULL; mWidth = width; mHeight = height; // BGRA 转成 RGBA unsigned char* buffer = new unsigned char[datasize]; int i=0; for (i=0; i<datasize; i+=4) { (buffer+i)[0] = (bits+i)[2]; (buffer+i)[1] = (bits+i)[1]; (buffer+i)[2] = (bits+i)[0]; (buffer+i)[3] = (bits+i)[3]; } //Free FreeImage's copy of the data FreeImage_Unload(dib); return buffer; }
void MMKP_BBA::localSearch(MMKPBatSolution& bestBat,MMKPBatSolution& solution){ //already in sorted order in this implementation. //MMKP_BBA::quickSort(population,0,(population.size()-1)); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dib(0, 1); float r_1 = dib(gen); //find P1 and P2 int P1 = rand() % solution.solution.size(); int P2 = (rand() % (solution.solution.size()-P1)) + (P1); //equation 1 float B = dib(gen); float fTemp = solution.fmax + ((solution.fmax -solution.fmin)*B); //equation 2 float averageV = 0.0; int averageVSize = 0; for(int j=P1;j<P2;j++){ for(int k=0;k<solution.solution[j].size();k++){ this->currentFuncEvals++; float vTemp = solution.v + ((solution.solution[j][k] -bestBat.solution[j][k])*fTemp); averageV += vTemp; averageVSize++; //equation "3"--binary uses 7 and 8 float normalizedV = 1/(1+(exp(-(vTemp)))); float r = dib(gen); if(r <= normalizedV){ solution.solution[j][k] = 1; }else{ solution.solution[j][k] = 0; } } solution.v = (averageV/averageVSize); MMKP_MetaHeuristic::makeFeasible(solution.solution); } }
GLuint Entity::LoadTextureRAW(string filename) { GLuint texture; //image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //pointer to the image, once loaded FIBITMAP *dib(0); //pointer to the image data BYTE* bits(0); //image width and height unsigned int width(0), height(0); fif = FreeImage_GetFileType(filename.c_str(), 0); //if still unknown, try to guess the file format from the file extension if(fif == FIF_UNKNOWN){ fif = FreeImage_GetFIFFromFilename(filename.c_str()); cout << "Getting Image Type" << endl; } //if still unkown, return failure if(fif == FIF_UNKNOWN) cout << "Failed to get image type" << endl; //check that the plugin has reading capabilities and load the file if(FreeImage_FIFSupportsReading(fif)){ //dib = FreeImage_ConvertTo32Bits(dib); dib = FreeImage_Load(fif, filename.c_str()); } //if the image failed to load, return failure if(!dib) cout << "Failed to load image" << endl; bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); // allocate a texture name glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); //Tell OpenGL which texture to edit //Map the image to the texture glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D 0, //0 for now GL_RGB, //Format OpenGL uses for image width, height, //Width and height 0, //The border of the image GL_BGR, //GL_RGB, because pixels are stored in RGB format GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored //as unsigned numbers bits); //The actual pixel data glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR ); FreeImage_Unload(dib); return texture; }
Font::Font(Application* application) : BMFontBase(application) { std::stringstream stream(fontGlymp); int doMipmaps_; stream >> doMipmaps_; while (1) { TextureGlyph glyph; int chr; stream >> chr; if (stream.eof()) break; glyph.chr = chr; stream >> glyph.x >> glyph.y; stream >> glyph.width >> glyph.height; stream >> glyph.left >> glyph.top; stream >> glyph.advancex >> glyph.advancey; fontInfo_.textureGlyphs[chr] = glyph; } fontInfo_.kernings.clear(); fontInfo_.isSetTextColorAvailable = true; fontInfo_.height = 10; fontInfo_.ascender = 8; sizescalex_ = 1; sizescaley_ = 1; uvscalex_ = 1; uvscaley_ = 1; Dib dib(application, 128, 128); for (int y = 0; y < 128; ++y) for (int x = 0; x < 128; ++x) { unsigned char c = 255 - fontImage[x + y * 128]; unsigned char rgba[4] = {c, c, c, 1}; dib.setPixel(x, y, rgba); } TextureParameters parameters; parameters.filter = eNearest; parameters.wrap = eClamp; parameters.grayscale = true; data_ = application->getTextureManager()->createTextureFromDib(dib, parameters); }
void sprite::load(config* simConfig, string fpath) { //generate texture data //get texture file name char newname[512]; string textPath = "../bin/sprites/" + fpath; isFont = false; strcpy(newname, textPath.c_str()); //give it its texture texID = simConfig->texCount; simConfig->texCount++; FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; BYTE * bits(0); FIBITMAP * dib(0); fif = FreeImage_GetFileType(newname, 0); //if still unknown, try to guess the file format from the file extension if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(newname); if(fif == FIF_UNKNOWN) cout<<"WE DON'T KNOW WHAT FIF THIS IS!"<<endl; if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, newname, 0); else cout<<"Bad texture file format!"<<endl; if(!dib) cout<<"Dib failed to load! Are your file paths set up properly?? "<<newname<<endl; bits = FreeImage_GetBits(dib); //get the image width and height texWidth = FreeImage_GetWidth(dib); texHeight = FreeImage_GetHeight(dib); //generate an OpenGL texture ID for this texture glGenTextures(1, &texID); glBindTexture( GL_TEXTURE_2D, texID); //store the texture data for OpenGL use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, bits); FreeImage_Unload(dib); //reset texture frame sizes to max frameSize[0] = texWidth; frameSize[1] = texHeight; }
bool FileLoader::ReadTexture(const char *filename, TexContainer& out) { //image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //pointer to the image, once loaded FIBITMAP *dib(0); //pointer to the image data BYTE* bits(0); //image width and height unsigned int width(0), height(0); //check the file signature and deduce its format fif = FreeImage_GetFileType(filename, 0); //if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); //if still unkown, return failure if (fif == FIF_UNKNOWN) return false; //check that the plugin has reading capabilities and load the file if (FreeImage_FIFSupportsReading(fif)) { //dib = FreeImage_Load(fif, filename.c_str()); FIBITMAP *test = FreeImage_Load(fif, filename); dib = FreeImage_ConvertTo32Bits(test); } //if the image failed to load, return failure if (!dib) return false; //retrieve the image data bits = FreeImage_GetBits(dib); //get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); //if this somehow one of these failed (they shouldn't), return failure if ((bits == 0) || (width == 0) || (height == 0)) return false; out.SetData(width * height, bits, width, height, GL_BGRA); //Texture* texture = Texture::Create2D(level, internalFormat, width, height, border, format, GL_UNSIGNED_BYTE, bits); //Free FreeImage's copy of the data FreeImage_Unload(dib); return true; }
bool CCubemap::LoadTexture(string filename, BYTE **bmpBytes, int &iWidth, int &iHeight) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); fif = FreeImage_GetFileType(filename.c_str(), 0); // Check the file signature and deduce its format if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension fif = FreeImage_GetFIFFromFilename(filename.c_str()); if(fif == FIF_UNKNOWN) // If still unknown, return failure return false; if(FreeImage_FIFSupportsReading(fif)) // Check if the plugin has reading capabilities and load the file dib = FreeImage_Load(fif, filename.c_str()); if(!dib) { char message[1024]; sprintf_s(message, "Cannot load image\n%s\n", filename.c_str()); MessageBox(NULL, message, "Error", MB_ICONERROR); return false; } iWidth = FreeImage_GetWidth(dib); iHeight = FreeImage_GetWidth(dib); int bpp = FreeImage_GetBPP(dib); BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data // If somehow one of these failed (they shouldn't), return failure if(*bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0) return false; int test = FreeImage_GetDIBSize(dib); *bmpBytes = new BYTE [iWidth*iHeight*bpp/8]; memcpy(*bmpBytes, bDataPointer, iWidth*iHeight*bpp/8); /* GLenum format; FreeImage_GetBPP(dib); if(FreeImage_GetBPP(dib) == 32)format = GL_RGBA; if(FreeImage_GetBPP(dib) == 24)format = GL_BGR; if(FreeImage_GetBPP(dib) == 8)format = GL_LUMINANCE; */ FreeImage_Unload(dib); return true; // Success }
bool CTexture::loadTexture2D(string a_sPath, bool bGenerateMipMaps) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); fif = FreeImage_GetFileType(a_sPath.c_str(), 0); // Check the file signature and deduce its format if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension fif = FreeImage_GetFIFFromFilename(a_sPath.c_str()); if(fif == FIF_UNKNOWN) // If still unkown, return failure return false; if(FreeImage_FIFSupportsReading(fif)) // Check if the plugin has reading capabilities and load the file dib = FreeImage_Load(fif, a_sPath.c_str()); if(!dib) return false; BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data iWidth = FreeImage_GetWidth(dib); // Get the image width and height iHeight = FreeImage_GetHeight(dib); iBPP = FreeImage_GetBPP(dib); // If somehow one of these failed (they shouldn't), return failure if(bDataPointer == NULL || iWidth == 0 || iHeight == 0) return false; // Generate an OpenGL texture ID for this texture glGenTextures(1, &uiTexture); glBindTexture(GL_TEXTURE_2D, uiTexture); int iFormat = iBPP == 24 ? GL_BGR : iBPP == 8 ? GL_LUMINANCE : 0; int iInternalFormat = iBPP == 24 ? GL_RGB : GL_DEPTH_COMPONENT; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 0, iFormat, GL_UNSIGNED_BYTE, bDataPointer); if(bGenerateMipMaps)glGenerateMipmap(GL_TEXTURE_2D); FreeImage_Unload(dib); glGenSamplers(1, &uiSampler); sPath = a_sPath; bMipMapsGenerated = bGenerateMipMaps; return true; // Success }
FIBITMAP *get_dib(char* file) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib(0); fif = FreeImage_GetFileType(file, 0); if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(file); if (fif == FIF_UNKNOWN) return NULL; if (FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, file); if (!dib) return NULL; FreeImage_FlipVertical(dib); return dib; }
void CPaletteApp::SetAppPalette(UINT uIdDib) { if (m_pAppActivePalette) delete m_pAppActivePalette; if (m_pAppInactivePalette) delete m_pAppInactivePalette; CDib dib(uIdDib); m_pAppActivePalette = dib.ClonePalette(PC_NOCOLLAPSE, TRUE); m_pAppInactivePalette = dib.ClonePalette(0, TRUE); //REVIEW: doesn't seem to happen immediately. CWindowDC dc(CWnd::GetDesktopWindow()); RealizeAppPalette(&dc); }
// helper of DoDrawBitmap() and DoBlit() static bool DrawBitmapUsingStretchDIBits(HDC hdc, const wxBitmap& bmp, wxCoord x, wxCoord y) { #if wxUSE_WXDIB wxDIB dib(bmp); bool ok = dib.IsOk(); if ( !ok ) return false; DIBSECTION ds; if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) ) { wxLogLastError(wxT("GetObject(DIBSECTION)")); return false; } // ok, we've got all data we need, do blit it if ( ::StretchDIBits ( hdc, x, y, ds.dsBmih.biWidth, ds.dsBmih.biHeight, 0, 0, ds.dsBmih.biWidth, ds.dsBmih.biHeight, ds.dsBm.bmBits, (LPBITMAPINFO)&ds.dsBmih, DIB_RGB_COLORS, SRCCOPY ) == GDI_ERROR ) { wxLogLastError(wxT("StretchDIBits")); return false; } return true; #else return false; #endif }
ImagePtr ResourceLoader::load<Image>(std::string file) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); BYTE* bits(0); unsigned int width(0), height(0); fif = FreeImage_GetFileType(file.c_str(),0); if(fif==FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(file.c_str()); } if(fif == FIF_UNKNOWN) { throw ServiceException("Cannot load file named: "+file); } if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif,file.c_str()); if(!dib) throw ServiceException("Cannot load file named: "+file); //FreeImage_ConvertToRGBAF() dib = FreeImage_ConvertTo32Bits(dib); FreeImage_FlipVertical(dib); bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); if((bits == 0)||(width == 0)||(height == 0)) throw ServiceException("Cannot load file named: "+file); Pixel* p = reinterpret_cast<Pixel*>(bits); ImagePtr res = make_shared<Image>(p,width,height); FreeImage_Unload(dib); return res; }
Texture::Texture(const char* filename, GLenum image_format, GLint internal_format, GLint level, GLint border) { glGenTextures(1, &this->id); FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib(0); BYTE* bits(0); unsigned int width(0), height(0); GLuint gl_texID; fif = FreeImage_GetFileType(filename, 0); //pobieranie typu tekstury if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); if (fif == FIF_UNKNOWN){ ReportWarning("Texture file format undefined"); return; } if (FreeImage_FIFSupportsReading(fif)) //sprawdza czy moze odczytac dib = FreeImage_Load(fif, filename); //odczytuje if (!dib) ReportWarning("Could not load texture"); bits = FreeImage_GetBits(dib); //rozmiar piksela width = FreeImage_GetWidth(dib); //wielkosc tekstury height = FreeImage_GetHeight(dib); //if((bits == 0) || (width == 0) || (height == 0)) //ReportWarning("Wrong bits"); glBindTexture(GL_TEXTURE_2D, this->id); //bindowanie i ustawianie parametrow tekstury glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height, //generowanie tekstury border, image_format, GL_UNSIGNED_BYTE, bits); FreeImage_Unload(dib); }
bool GrayScaleHMap::loadMap(char* strFile) { //image format FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //pointer to the image, once loaded FIBITMAP *dib(0); //check the file signature and deduce its format fif = FreeImage_GetFileType(strFile, 0); //if still unknown, try to guess the file format from the file extension if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(strFile); //if still unkown, return failure if(fif == FIF_UNKNOWN) return false; //check that the plugin has reading capabilities and load the file if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, strFile); //if the image failed to load, return failure if(!dib) return false; //retrieve the image data bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); if((bits == NULL) || (width == 0) || (height == 0)) return false; //Free FreeImage's copy of the data FreeImage_Unload(dib); return true; }
static GLuint loadTexture(const char *filename) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib(0); BYTE* bits(0); GLuint gl_texID; unsigned int width(0), height(0); fif = FreeImage_GetFileType(filename, 0); if(fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(filename); return 0; } if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); if(!dib) return false; dib = FreeImage_ConvertTo24Bits(dib); bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); if((bits == 0) || (width == 0) || (height == 0)) return 0; glGenTextures(1, &gl_texID); glBindTexture(GL_TEXTURE_2D, gl_texID); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, bits); // FreeImage loads textures in BGR format. FreeImage_Unload(dib); return gl_texID; }
bool CTexture::ReloadTexture() { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP* dib(0); fif = FreeImage_GetFileType(sPath.c_str(), 0); // Check the file signature and deduce its format if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension fif = FreeImage_GetFIFFromFilename(sPath.c_str()); if(fif == FIF_UNKNOWN) // If still unknown, return failure return false; if(FreeImage_FIFSupportsReading(fif)) // Check if the plugin has reading capabilities and load the file dib = FreeImage_Load(fif, sPath.c_str()); if(!dib) return false; BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data // If somehow one of these failed (they shouldn't), return failure if(bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0) return false; GLenum format; int bada = FreeImage_GetBPP(dib); if(FreeImage_GetBPP(dib) == 32)format = GL_RGBA; if(FreeImage_GetBPP(dib) == 24)format = GL_BGR; if(FreeImage_GetBPP(dib) == 8)format = GL_LUMINANCE; glBindTexture(GL_TEXTURE_2D, uiTexture); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iWidth, iHeight, format, GL_UNSIGNED_BYTE, bDataPointer); if(bMipMapsGenerated)glGenerateMipmap(GL_TEXTURE_2D); FreeImage_Unload(dib); return true; // Success }
BOOL CDib::DrawTransparent (CDC* pDC, int x, int y, COLORREF crColor) { ASSERT( IsIndexed() ); CRect rect(y,x,y+GetWidth(), x-GetHeight()); UtilWin::CIntArray selectionArray; GetSelectionIndex(crColor, selectionArray); HDC hDestDC = pDC->GetSafeHdc(); if( selectionArray.GetSize()==1 ) { return CImage::TransparentBlt(pDC->GetSafeHdc(), rect, crColor); } else if( selectionArray.GetSize()>=1 ) { CDib dib(GetWidth(), GetHeight(), 32); /*for(int i=0; i<GetWidth(); i++) { for(int j=0; j<GetHeight(); j++) { if( SetPixel(i,j, ); } } CImage::AlphaBlend(dib.GetDC(), rect, crColor); */ //return } return CDib::Draw(pDC, x, y); }
void ReportView::OnFileCopyactivewindow() { DIB dib(this->GetParentFrame()); dib.PutOnClipboard(); // TODO printing /* CDib dib(this->GetParentFrame()); //Attempt to open the clipboard if ( ! this->OpenClipboard() ) { AfxMessageBox( "CDibView::CopyDIBToClipboard: Cannot open the Clipboard" ); return; } //Remove the current Clipboard contents if( ! EmptyClipboard() ) { AfxMessageBox( "DibView::CopyDIBToClipboard: Cannot empty the Clipboard" ); return; } //Get the data to copy HANDLE lBitMapInfoHdl = dib.BitMapInfoHdl(); if( lBitMapInfoHdl != NULL ) { //Give to the clipboard if ( ::SetClipboardData( CF_DIB, lBitMapInfoHdl ) == NULL ) { AfxMessageBox( "DibView::CopyDIBToClipboard: Unable to set Clipboard data" ); } } CloseClipboard();*/ }
/** Generic image loader @param lpszPathName Pointer to the full file name @param flag Optional load flag constant @return Returns the loaded dib if successful, returns NULL otherwise */ bitmap_ptr GenericLoader(const std::string& lpszPathName, int flag = 0) { auto fif = FIF_UNKNOWN; // check if file path is not empty if (lpszPathName.empty()) return nullptr; // check the file signature and deduce its format // (the second argument is currently not used by FreeImage) fif = FreeImage_GetFileType(lpszPathName.c_str(), 0); if (fif == FIF_UNKNOWN) { // no signature ? // try to guess the file format from the file extension fif = FreeImage_GetFIFFromFilename(lpszPathName.c_str()); } // check that the plugin has reading capabilities ... if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { // ok, let's load the file bitmap_ptr dib(FreeImage_Load(fif, lpszPathName.c_str(), flag)); // unless a bad file format, we are done ! return dib; } return nullptr; }
bool wxSetClipboardData(wxDataFormat dataFormat, const void *data, int width, int height) { HANDLE handle = 0; // return value of SetClipboardData switch (dataFormat) { case wxDF_BITMAP: { wxBitmap *bitmap = (wxBitmap *)data; HDC hdcMem = CreateCompatibleDC((HDC) NULL); HDC hdcSrc = CreateCompatibleDC((HDC) NULL); HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP()); HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, bitmap->GetWidth(), bitmap->GetHeight()); if (!hBitmap) { SelectObject(hdcSrc, old); DeleteDC(hdcMem); DeleteDC(hdcSrc); return false; } HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap); BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(), hdcSrc, 0, 0, SRCCOPY); // Select new bitmap out of memory DC SelectObject(hdcMem, old1); // Set the data handle = ::SetClipboardData(CF_BITMAP, hBitmap); // Clean up SelectObject(hdcSrc, old); DeleteDC(hdcSrc); DeleteDC(hdcMem); break; } #if wxUSE_WXDIB case wxDF_DIB: { wxBitmap *bitmap = (wxBitmap *)data; if ( bitmap && bitmap->Ok() ) { wxDIB dib(*bitmap); if ( dib.IsOk() ) { handle = ::SetClipboardData(CF_DIB, dib.Detach()); } } break; } #endif // VZ: I'm told that this code works, but it doesn't seem to work for me // and, anyhow, I'd be highly surprised if it did. So I leave it here // but IMNSHO it is completely broken. #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__) case wxDF_METAFILE: { wxMetafile *wxMF = (wxMetafile *)data; HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); mf->mm = wxMF->GetWindowsMappingMode(); mf->xExt = width; mf->yExt = height; mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE(); GlobalUnlock(data); wxMF->SetHMETAFILE((WXHANDLE) NULL); handle = SetClipboardData(CF_METAFILEPICT, data); break; } #endif // wxUSE_METAFILE #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) case wxDF_ENHMETAFILE: { wxEnhMetaFile *emf = (wxEnhMetaFile *)data; wxEnhMetaFile emfCopy = *emf; handle = SetClipboardData(CF_ENHMETAFILE, (void *)emfCopy.GetHENHMETAFILE()); } break; #endif // wxUSE_ENH_METAFILE case CF_SYLK: case CF_DIF: case CF_TIFF: case CF_PALETTE: default: { wxLogError(_("Unsupported clipboard format.")); return false; } case wxDF_OEMTEXT: dataFormat = wxDF_TEXT; // fall through case wxDF_TEXT: { char *s = (char *)data; width = strlen(s) + 1; height = 1; DWORD l = (width * height); HANDLE hGlobalMemory = GlobalAlloc(GHND, l); if ( hGlobalMemory ) { LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); memcpy(lpGlobalMemory, s, l); GlobalUnlock(hGlobalMemory); } handle = SetClipboardData(dataFormat, hGlobalMemory); break; } // Only tested with non-Unicode, Visual C++ 6.0 so far #if defined(__VISUALC__) && !defined(UNICODE) case wxDF_HTML: { char* html = (char *)data; // Create temporary buffer for HTML header... char *buf = new char [400 + strlen(html)]; if(!buf) return false; // Get clipboard id for HTML format... static int cfid = 0; if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format")); // Create a template string for the HTML header... strcpy(buf, "Version:0.9\r\n" "StartHTML:00000000\r\n" "EndHTML:00000000\r\n" "StartFragment:00000000\r\n" "EndFragment:00000000\r\n" "<html><body>\r\n" "<!--StartFragment -->\r\n"); // Append the HTML... strcat(buf, html); strcat(buf, "\r\n"); // Finish up the HTML format... strcat(buf, "<!--EndFragment-->\r\n" "</body>\r\n" "</html>"); // Now go back, calculate all the lengths, and write out the // necessary header information. Note, wsprintf() truncates the // string when you overwrite it so you follow up with code to replace // the 0 appended at the end with a '\r'... char *ptr = strstr(buf, "StartHTML"); wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf); *(ptr+10+8) = '\r'; ptr = strstr(buf, "EndHTML"); wsprintf(ptr+8, "%08u", strlen(buf)); *(ptr+8+8) = '\r'; ptr = strstr(buf, "StartFragment"); wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf); *(ptr+14+8) = '\r'; ptr = strstr(buf, "EndFragment"); wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf); *(ptr+12+8) = '\r'; // Now you have everything in place ready to put on the // clipboard. // Allocate global memory for transfer... HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4); // Put your string in the global memory... ptr = (char *)GlobalLock(hText); strcpy(ptr, buf); GlobalUnlock(hText); handle = ::SetClipboardData(cfid, hText); // Free memory... GlobalFree(hText); // Clean up... delete [] buf; break; } #endif } if ( handle == 0 ) { wxLogSysError(_("Failed to set clipboard data.")); return false; } return true; }