void SaveTextureToImageFile( uint32 aWidth, uint32 aHeight, GLuint aTexture, std::string aPath, bool aOverwrite ) { //TODO auto_ptr on textures ILuint imageID; // The image name to return. DEVIL_CHECKED_CALL( ilGenImages( 1, &imageID ) ); DEVIL_CHECKED_CALL( ilBindImage( imageID ) ); if ( aOverwrite ) { DEVIL_CHECKED_CALL( ilEnable(IL_FILE_OVERWRITE) ); } else { DEVIL_CHECKED_CALL( ilDisable(IL_FILE_OVERWRITE) ); } DEVIL_CHECKED_CALL( ilTexImage( aWidth, aHeight, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL ) ); ILubyte* data = new ILubyte[ 3 * aWidth * aHeight ]; GL_CHECKED_CALL( glBindTexture( GL_TEXTURE_2D, aTexture ) ); GL_CHECKED_CALL( glGetTexImage( GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)data ) ); GL_CHECKED_CALL( glBindTexture( GL_TEXTURE_2D, 0 ) ); DEVIL_CHECKED_CALL( ilSetPixels( 0, 0, 0, aWidth, aHeight, 1, IL_RGB, IL_UNSIGNED_BYTE, data ) ); delete [] data; DEVIL_CHECKED_CALL( ilSaveImage( aPath.data() ) ); DEVIL_CHECKED_CALL( ilDeleteImages( 1, &imageID) ); }
//------------------------------------------------------------------------------------ // Returns a region of the image. Note: the OutData have not to be NULL //------------------------------------------------------------------------------------ void Image::SetPixels( int XOff, int YOff, int ZOff, int Width, int Height, int Depth, ImageFormat imgFormat, u8* OutData, u32 MipMapLevel ) { ILenum fmt; switch (imgFormat) { case EIF_RGB: fmt = IL_RGB; break; case EIF_RGBA: fmt = IL_RGBA; break; case EIF_BGR: fmt = IL_BGR; break; case EIF_BGRA: fmt = IL_BGRA; break; } ilBindImage(m_iImgID); if (!ilActiveMipmap(MipMapLevel)) { CheckDevilErrors(m_pFileName); io::Logger::Log(io::ELM_Error, "Incorrect mipmap level for image=%", m_pName); return; } ilSetPixels(XOff, YOff, ZOff, Width, Height, Depth, fmt, IL_UNSIGNED_BYTE, OutData); CheckDevilErrors(m_pFileName); } // SetPixels
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); }
void ofxTexture::SetPixel(ofVec2f position, ofColor color) { if(m_Locked) return; ilBindImage(m_ImageId); ILubyte* data; data = new ILubyte[m_BytePerPixel]; data[0] = color.r; data[1] = color.g; data[2] = color.b; if(m_BytePerPixel == 4) { data[3] = color.a; } ilSetPixels(position.x, position.y, 0, 1, 1, 1, m_BytePerPixel==3?IL_RGB:IL_RGBA, IL_UNSIGNED_BYTE, data); delete[] data; }
void CGuildMarkImage::PutData(UINT x, UINT y, UINT width, UINT height, void * data) { ilBindImage(m_uImg); ilSetPixels(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; }
PVideoFrame ImageWriter::GetFrame(int n, IScriptEnvironment* env) { PVideoFrame frame = child->GetFrame(n, env); // check bounds if ((n<start)||(n>end)) { if (info) { ostringstream ss; ss << "ImageWriter: frame " << n << " not in range"; env->MakeWritable(&frame); ApplyMessage(&frame, vi, ss.str().c_str(), vi.width/4, TEXT_COLOR,0,0 , env); } return frame; } // construct filename ostringstream fn_oss; fn_oss << base_name << setfill('0') << setw(6) << n << '.' << ext; string filename = fn_oss.str(); if (!lstrcmpi(ext, "ebmp")) /* Use internal 'ebmp' writer */ { // initialize file object ofstream file(filename.c_str(), ios::out | ios::trunc | ios::binary); if (!file) { ostringstream ss; ss << "ImageWriter: could not create file '" << filename << "'"; env->MakeWritable(&frame); ApplyMessage(&frame, vi, ss.str().c_str(), vi.width/4, TEXT_COLOR,0,0 , env); return frame; } // write headers file.write(reinterpret_cast<const char *>( &fileHeader ), sizeof(BITMAPFILEHEADER)); file.write(reinterpret_cast<const char *>( &infoHeader ), sizeof(BITMAPINFOHEADER)); // write raster const BYTE * srcPtr = frame->GetReadPtr(); int pitch = frame->GetPitch(); int row_size = frame->GetRowSize(); int height = frame->GetHeight(); if (0) // (vi.IsY8()) { // write upside down const BYTE * endPtr = srcPtr + pitch * (height-1); fileWrite(file, endPtr, -pitch, row_size, height); } else { fileWrite(file, srcPtr, pitch, row_size, height); if (vi.IsPlanar()) { srcPtr = frame->GetReadPtr(PLANAR_U); pitch = frame->GetPitch(PLANAR_U); row_size = frame->GetRowSize(PLANAR_U); height = frame->GetHeight(PLANAR_U); fileWrite(file, srcPtr, pitch, row_size, height); srcPtr = frame->GetReadPtr(PLANAR_V); fileWrite(file, srcPtr, pitch, row_size, height); } } // clean up file.close(); } else { /* Use DevIL library */ // Set up DevIL ILuint myImage=0; ilGenImages(1, &myImage); // Initialize 1 image structure ilBindImage(myImage); // Set this as the current image const ILenum il_format = vi.IsRGB32() ? IL_BGRA : IL_BGR; // Set image parameters if (IL_TRUE == ilTexImage(vi.width, vi.height, 1, vi.BitsPerPixel() / 8, il_format, IL_UNSIGNED_BYTE, NULL)) { // Program actual image raster const BYTE * srcPtr = frame->GetReadPtr(); int pitch = frame->GetPitch(); for (int y=0; y<vi.height; ++y) { ilSetPixels(0, y, 0, vi.width, 1, 1, il_format, IL_UNSIGNED_BYTE, (void*) srcPtr); srcPtr += pitch; } // DevIL writer fails if the file exists, so delete first DeleteFile(filename.c_str()); // Save to disk (format automatically inferred from extension) ilSaveImage(const_cast<char * const> (filename.c_str()) ); } // Get errors if any ILenum err = ilGetError(); // Clean up ilDeleteImages(1, &myImage); if (err != IL_NO_ERROR) { ostringstream ss; ss << "ImageWriter: error '" << getErrStr(err) << "' in DevIL library\n writing file " << filename; env->MakeWritable(&frame); ApplyMessage(&frame, vi, ss.str().c_str(), vi.width/4, TEXT_COLOR,0,0 , env); return frame; } } if (info) { // overlay on video output: progress indicator ostringstream text; text << "Frame " << n << " written to: " << filename; env->MakeWritable(&frame); ApplyMessage(&frame, vi, text.str().c_str(), vi.width/4, TEXT_COLOR,0,0 , env); } return frame; }
void main(int argc, char **argv) { ILubyte *Lump; ILubyte *final_tga; ILubyte *Data; ILuint Size; ILuint Image; FILE *File; ILenum Error; ILuint ilFormat; int x0,y0,x1,y1; //char filename[]="103,692-205,768.tga"; char filename[64]; if(argc!=0 && !strcmp(argv[2],"-laststep")) { File=fopen(argv[1], "r+"); fseek(File, 17, SEEK_SET); fprintf(File, "%c", 0x20); fclose(File); ilInit(); ilGenImages(1, &Image); ilBindImage(Image); if(!ilLoadImage(argv[1])) { printf("Error loading image final.tga"); return ; } ilEnable(IL_FILE_OVERWRITE); strcpy(filename, argv[1]); *strchr(filename, '\\')='\0'; strcat(filename, "\\final.png"); if(!ilSave(IL_PNG, filename)) printf("Error occurs when saving image."); else printf("Image is saved successfuly."); return ; } strcpy(filename, strchr(argv[2], '\\')+1); sscanf(filename, "%d, %d - %d, %d", &x0,&y0,&x1,&y1); //printf("%d %d %d %d\n", x0,y0,x1,y1); x0--; //start from (0,0), not (1,1) y0--; ilInit(); File = fopen(argv[2], "rb"); fseek(File, 0, SEEK_END); Size = ftell(File); fclose(File); Data = (ILubyte*)malloc(Size); ilGenImages(1, &Image); ilBindImage(Image); if(!ilLoadImage(argv[2])) { printf("Error loading image %s", filename); return ; } ilFormat=ilGetInteger(IL_IMAGE_FORMAT); ilCopyPixels(x0, y0, 0, x1-x0, y1-y0, 1, ilFormat, IL_UNSIGNED_BYTE, Data); if(!ilLoadImage(argv[1])) { printf("Error loading image final.tga"); return ; } ilSetPixels(x0, y0, 0, x1-x0, y1-y0, 1, ilFormat, IL_UNSIGNED_BYTE, Data); ilEnable(IL_FILE_OVERWRITE); if(!ilSaveImage(argv[1])) printf("Error occurs when saving image."); else printf("Image is saved successfuly."); ilDeleteImages(1, &Image); ilShutDown(); while ((Error = ilGetError()) != IL_NO_ERROR) { printf("%d: %s/n", Error, iluErrorString(Error)); } }