void ofxTexture::DrawString(string text, ofxBitmapFont* font, ofRectangle dest_rect, unsigned char font_size) { if(m_Locked) return; // TODO: implement draw text with boundary float scale; if(font_size == 0) { scale = 1.0f; } else { scale = (float)font_size/font->GetFontSize(); } ofVec2f cursor(dest_rect.x, dest_rect.y); ilDisable(IL_BLIT_BLEND); for (int i = 0; i < text.size(); i++) { ofVec2f draw_region = scale*font->GetRect(text[i]); ILuint character_image = font->GetImageId(text[i]); ILuint dummy = ilGenImage(); ilBindImage(dummy); ilCopyImage(character_image); iluScale(draw_region.x, draw_region.y, 1); ilBindImage(m_ImageId); ilBlit(dummy, cursor.x, cursor.y, 0, 0, 0, 0, draw_region.x, draw_region.y, 1); ilDeleteImage(dummy); cursor.x += draw_region.x; } ilEnable(IL_BLIT_BLEND); }
ILboolean ilImage::Copy(ILuint Src) { if (this->Id) { this->Bind(); return ilCopyImage(Src); } return IL_FALSE; }
ilImage& ilImage::operator = (const ilImage &Image) { if (Id == 0) Id = Image.GetId(); else { Bind(); ilCopyImage(Image.GetId()); } return *this; }
// // Operators // ilImage& ilImage::operator = (ILuint Image) { if (this->Id == 0) this->Id = Image; else { this->Bind(); ilCopyImage(Image); } return *this; }
void ofxTexture::StretchTransfer(ofxTexture* source, ofRectangle source_rect, ofRectangle dest_rect, int alpha) { if(m_Locked || source->IsLocked()) return; ILuint dummy = ilGenImage(); ilBindImage(dummy); ilCopyImage(source->GetDevilId()); iluScale(dest_rect.width, dest_rect.height, 1); ilBindImage(m_ImageId); ilDisable(IL_BLIT_BLEND); ilBlit(dummy, dest_rect.x, dest_rect.y, 0, source_rect.x, source_rect.y, 0, source_rect.width, source_rect.height, 1); ilEnable(IL_BLIT_BLEND); ilDeleteImage(dummy); }
ILuint ImageManager::GenerateKeeperImage(ILuint TextureDevILID, int16_t BorderColorID) { ILuint NewImageID; if (BorderColorID != -1) { ilGenImages(1, &NewImageID); ilBindImage(NewImageID); ilCopyImage(TextureDevILID); ApplyBorder(NewImageID, BorderColorID); } else { // no reason to copy large amounts of data without changes NewImageID = TextureDevILID; } return NewImageID; }
ILuint CopyImage(Image *src, Image *dest) { ILuint newImageID; ilGenImages(1, &newImageID); ilBindImage(newImageID); if (!ilCopyImage(src->id)) return 0; dest->id = newImageID; dest->rawData = ilGetData(); dest->width = ilGetInteger(IL_IMAGE_WIDTH); dest->height = ilGetInteger(IL_IMAGE_HEIGHT); dest->depth = ilGetInteger(IL_IMAGE_DEPTH); dest->format = ilGetInteger(IL_IMAGE_FORMAT); return 1; }
void FractureBox::LoadFractureMap(const char *filename, bool bolden) { if(mFracTexID) ilDeleteImage(mFracTexID); if(mFracTexScaleID) ilDeleteImage(mFracTexScaleID); mFracTexID = ilGenImage(); ilBindImage(mFracTexID); if(!ilLoadImage(filename)) { std::cout << "Error loading " << filename << std::endl; return; } if(!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE)) { std::cout << "Error converting image " << filename << std::endl; return; } ILinfo imgInfo; iluGetImageInfo(&imgInfo); //if(imgInfo.Origin == IL_ORIGIN_UPPER_LEFT) if(imgInfo.Origin == IL_ORIGIN_LOWER_LEFT) iluFlipImage(); if(bolden) boldenLines(); //Now create the scaled version of the image mFracTexScaleID = ilGenImage(); ilBindImage(mFracTexScaleID); ilCopyImage(mFracTexID); //Scale the image to fit our box const int wid = w() - 2; const int hei = h() - 2; iluImageParameter(ILU_FILTER, ILU_SCALE_MITCHELL); iluScale(wid, hei, imgInfo.Depth); mFracImage = RGBImagePtr(new Fl_RGB_Image(ilGetData(), wid, hei)); image(mFracImage.get()); redraw(); ilBindImage(0); }
void FractureBox::resizeFractureTexture() { ASSERT(mFracTexID); if(mFracTexScaleID) ilDeleteImage(mFracTexScaleID); mFracTexScaleID = ilGenImage(); ilBindImage(mFracTexScaleID); ilCopyImage(mFracTexID); ILinfo imgInfo; iluGetImageInfo(&imgInfo); //Scale the image to fit our box const int wid = w() - 2; const int hei = h() - 2; iluScale(wid, hei, imgInfo.Depth); mFracImage = RGBImagePtr(new Fl_RGB_Image(ilGetData(), wid, hei)); image(mFracImage.get()); redraw(); ilBindImage(0); }
HBITMAP TextureControl::GetBitmap(string name, HDC destDC, bool refresh, unsigned int w, unsigned int h) { if(refresh == false && namedBitmaps.find(name) != namedBitmaps.end()) return namedBitmaps[name]; // else bitmap doesn't exist. if(namedImages.find(name) == namedImages.end()) return NULL; //img not loaded ILuint tmp = getImageHandle(); ilCopyImage(namedImages[name]); iluImageParameter(ILU_FILTER, ILU_SCALE_BSPLINE); iluScale(w,h,1); if(alphaMatte) ilEnable(IL_BMP_MATTEALPHAB); else ilEnable(IL_BMP_MATTEALPHA); namedBitmaps[name] = ilutConvertToHBitmap(destDC); ilDeleteImage(tmp); return namedBitmaps[name]; }
ILuint ImageManager::GeneratedOverLayImage(ILuint TextureDevILID, int16_t PrimaryColorID, int16_t BorderColorID) { ILuint TextureImageID; ilGenImages(1, &TextureImageID); ilBindImage(TextureImageID); ilCopyImage(TextureDevILID); ilConvertImage(IL_LUMINANCE_ALPHA, IL_UNSIGNED_BYTE); uint8_t* TextureImageData = ilGetData(); uint32_t width = ilGetInteger(IL_IMAGE_WIDTH); uint32_t height = ilGetInteger(IL_IMAGE_HEIGHT); ColorData* PrimaryColor = DATA->getColorData(PrimaryColorID); ILuint NewImageID; ilGenImages(1, &NewImageID); ilBindImage(NewImageID); ilTexImage(width, height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL); uint8_t* NewImageData = ilGetData(); uint32_t bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); if(PrimaryColor != NULL) { for(uint32_t i = 0; i < width; i++) { for(uint32_t j = 0; j < height; j++) { float Base = TextureImageData[(i * width * 2) + (j * 2) + 0]; uint8_t Alpha = TextureImageData[(i * width * 2) + (j * 2) + 1]; Base /= 255.0; float OriginalBlue = PrimaryColor->getBlue(); OriginalBlue /= 255.0; float OriginalGreen = PrimaryColor->getGreen(); OriginalGreen /= 255.0; float OriginalRed = PrimaryColor->getRed(); OriginalRed /= 255.0; // coloring using overlay mode if(Base >= 0.5) { NewImageData[(i * width * bpp) + (j * bpp) + 0] = (1.0 - 2.0 * (1.0 - OriginalBlue) * (1.0 - Base)) * 255; // Blue NewImageData[(i * width * bpp) + (j * bpp) + 1] = (1.0 - 2.0 * (1.0 - OriginalGreen) * (1.0 - Base)) * 255; // Green NewImageData[(i * width * bpp) + (j * bpp) + 2] = (1.0 - 2.0 * (1.0 - OriginalRed) * (1.0 - Base)) * 255; // Red NewImageData[(i * width * bpp) + (j * bpp) + 3] = Alpha; } else { NewImageData[(i * width * bpp) + (j * bpp) + 0] = (2.0 * OriginalBlue * Base) * 255; // Blue NewImageData[(i * width * bpp) + (j * bpp) + 1] = (2.0 * OriginalGreen * Base) * 255; // Green NewImageData[(i * width * bpp) + (j * bpp) + 2] = (2.0 * OriginalRed * Base) * 255; // Red NewImageData[(i * width * bpp) + (j * bpp) + 3] = Alpha; } } } } if (BorderColorID != -1) { ApplyBorder(NewImageID, BorderColorID); } return NewImageID; }
ILuint ImageManager::GenerateGradientImage(ILuint TextureDevILID, int16_t PrimaryColorID, int16_t SecondaryColorID, int16_t BorderColorID) { ILuint TextureImageID; ilGenImages(1, &TextureImageID); ilBindImage(TextureImageID); ilCopyImage(TextureDevILID); ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE); //Load as IL_LUMINANCE to avoid convertion? uint8_t* TextureImageData = ilGetData(); uint32_t width = ilGetInteger(IL_IMAGE_WIDTH); uint32_t height = ilGetInteger(IL_IMAGE_HEIGHT); ILuint MaskImageID; ilGenImages(1, &MaskImageID); ilBindImage(MaskImageID); ilTexImage(width, height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL); uint8_t* MaskImageData = ilGetData(); ColorData* PrimaryColor = DATA->getColorData(PrimaryColorID); ColorData* SecondaryColor = DATA->getColorData(SecondaryColorID); uint32_t bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); if(SecondaryColor != NULL) { for(uint32_t i = 0; i < width; i++) { for(uint32_t j = 0; j < height; j++) { MaskImageData[(i * width * bpp) + (j * bpp) + 0] = SecondaryColor->getBlue(); // Blue MaskImageData[(i * width * bpp) + (j * bpp) + 1] = SecondaryColor->getGreen(); // Green MaskImageData[(i * width * bpp) + (j * bpp) + 2] = SecondaryColor->getRed(); // Red MaskImageData[(i * width * bpp) + (j * bpp) + 3] = 255 - TextureImageData[(i * width) + j]; // Alpha } } } ILuint NewImageID; ilGenImages(1, &NewImageID); ilBindImage(NewImageID); ilTexImage(width, height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL); uint8_t* NewImageData = ilGetData(); if(PrimaryColor != NULL) { for(uint32_t i = 0; i < width; i++) { for(uint32_t j = 0; j < height; j++) { NewImageData[(i * width * bpp) + (j * bpp) + 0] = PrimaryColor->getBlue(); // Blue NewImageData[(i * width * bpp) + (j * bpp) + 1] = PrimaryColor->getGreen(); // Green NewImageData[(i * width * bpp) + (j * bpp) + 2] = PrimaryColor->getRed(); // Red NewImageData[(i * width * bpp) + (j * bpp) + 3] = 255; // Alpha } } } ilOverlayImage(MaskImageID, 0, 0, 0); if (BorderColorID != -1) { ApplyBorder(NewImageID, BorderColorID); } return NewImageID; }
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; }
void ilFCopyImage_(int *RetVal, int *Src) { *RetVal = ilCopyImage(*Src); return; }