static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings){ ofInitFreeImage(); bool bLoaded = false; FIBITMAP* bmp = nullptr; FIMEMORY* hmem = nullptr; hmem = FreeImage_OpenMemory((unsigned char*) buffer.getData(), buffer.size()); if (hmem == nullptr){ ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, opening FreeImage memory failed"; return false; } //get the file type! FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); if( fif == -1 ){ ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, unable to guess image format from memory"; FreeImage_CloseMemory(hmem); return false; } //make the image!! if(fif == FIF_JPEG) { int option = getJpegOptionFromImageLoadSetting(settings); bmp = FreeImage_LoadFromMemory(fif, hmem, option); } else { bmp = FreeImage_LoadFromMemory(fif, hmem, 0); } if( bmp != nullptr ){ bLoaded = true; } //----------------------------- if (bLoaded){ putBmpIntoPixels(bmp,pix); } if (bmp != nullptr){ FreeImage_Unload(bmp); } if( hmem != nullptr ){ FreeImage_CloseMemory(hmem); } return bLoaded; }
FIBITMAP *GraphicsHelps::loadImageRC(const char *file) { unsigned char *memory = nullptr; size_t fileSize = 0; SDL_assert_release(RES_getMem(file, memory, fileSize)); //{ //pLogCritical("Resource file \"%s\" is not found!", file); //return nullptr; //} FIMEMORY *imgMEM = FreeImage_OpenMemory(memory, static_cast<FI_DWORD>(fileSize)); FREE_IMAGE_FORMAT formato = FreeImage_GetFileTypeFromMemory(imgMEM); if(formato == FIF_UNKNOWN) return nullptr; FIBITMAP *img = FreeImage_LoadFromMemory(formato, imgMEM, 0); FreeImage_CloseMemory(imgMEM); if(!img) return nullptr; FIBITMAP *temp; temp = FreeImage_ConvertTo32Bits(img); if(!temp) return nullptr; FreeImage_Unload(img); img = temp; return img; }
FIBITMAP * PngLoader::loadPng(const QString & inFile) { QFile file(inFile); if (!file.open(QIODevice::ReadOnly)) { std::cerr << "[ERROR] Qt cannot open file: " << inFile.toStdString() << std::endl; return nullptr; } QByteArray bytes = file.readAll(); FREE_IMAGE_FORMAT fiFormat = getFiFormat(inFile, bytes); if (fiFormat == FIF_UNKNOWN) return false; if (!isReadableFiFormat(fiFormat)) return false; FIMEMORY fiMemStream; fiMemStream.data = bytes.data(); FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(fiFormat, &fiMemStream); if (!fiBitmap) { std::cerr << "[ERROR] FreeImage cannot load image: " << inFile.toStdString() << std::endl; return nullptr; } if (!makeValidBpp(fiBitmap)) { std::cerr << "[ERROR] Program cannot make valid BPP in image: " << inFile.toStdString() << std::endl; return nullptr; } return fiBitmap; }
unsigned char * HVSTGFX::loadImageFile(CFileData *image, HVSTGFX::IMAGEFILE *imgFile, GLuint &texture) { FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(image->getName().c_str()); FIMEMORY * stream = 0; unsigned char tempRGB; GLuint tempTex = 0; GLenum errCode; bool error = false; stream = FreeImage_OpenMemory(image->getData()); if(FreeImage_FIFSupportsReading(fif)) imgFile->dib = FreeImage_LoadFromMemory(fif, stream); if(!imgFile->dib) { glbl->debugger->writeString("failed to open sprite " + image->getName()); return NULL; } //pointer to image data unsigned char* bits; bits = FreeImage_GetBits(imgFile->dib); imgFile->width = FreeImage_GetWidth(imgFile->dib); imgFile->height = FreeImage_GetHeight(imgFile->dib); imgFile->size = sizeof(bits); int size = imgFile->width*imgFile->height;//(FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib)); for (int imageIDx = 0; imageIDx < size * 4; imageIDx += 4) { tempRGB = bits[imageIDx]; bits[imageIDx] = bits[imageIDx + 2]; bits[imageIDx + 2] = tempRGB; } glGenTextures(1, &tempTex); texture = tempTex; errCode = glGetError(); if (errCode != GL_NO_ERROR) { MessageBox(NULL, _T("Unable to detect OpenGL support. Check if you have your graphics driver installed, or if your graphics card supports OpenGL."), NULL, NULL); } glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgFile->width, imgFile->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits); //FreeImage_Unload(dib); #ifdef _DEBUG if (!error) glbl->debugger->writeString("successfully loaded sprite " + image->getName() + "\n"); #endif return bits; }
Ogre::TexturePtr textureFromBytes(const QByteArray &ba, const std::string &name) { static bool fi_init = false; if (!fi_init) { FreeImage_Initialise(); } void *data = const_cast<char *>(ba.data()); FIMEMORY *mem = FreeImage_OpenMemory(reinterpret_cast<BYTE *>(data), ba.size()); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, 0); if (fif == FIF_UNKNOWN) { FreeImage_CloseMemory(mem); throw std::runtime_error("Image format is not supported for loading"); } FIBITMAP *bmp = FreeImage_LoadFromMemory(fif, mem, 0); FreeImage_CloseMemory(mem); if (!bmp) { throw std::runtime_error("Failed to decode image"); } FIBITMAP *converted = FreeImage_ConvertTo24Bits(bmp); FreeImage_Unload(bmp); if (!converted) { throw std::runtime_error("Failed to convert image to 24 bit"); } const unsigned w = FreeImage_GetWidth(converted); const unsigned h = FreeImage_GetHeight(converted); const unsigned data_size = w * h * 3; BYTE *image_data = FreeImage_GetBits(converted); ROS_INFO("Loading a %u x %u texture", w, h); // create texture Ogre::TexturePtr texture; try { Ogre::DataStreamPtr data_stream; data_stream.bind(new Ogre::MemoryDataStream(image_data, data_size)); const Ogre::String res_group = Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME; Ogre::TextureManager &texture_manager = Ogre::TextureManager::getSingleton(); texture = texture_manager.loadRawData(name, res_group, data_stream, w, h, Ogre::PF_R8G8B8, Ogre::TEX_TYPE_2D, 0); } catch (...) { // clean up FreeImage before re-throwing FreeImage_Unload(converted); throw; } return texture; }
/** Get the embedded JPEG preview image from RAW picture with included Exif Data. @param RawProcessor Libraw handle @param flags JPEG load flags @return Returns the loaded dib if successfull, returns NULL otherwise */ static FIBITMAP * libraw_LoadEmbeddedPreview(LibRaw *RawProcessor, int flags) { FIBITMAP *dib = NULL; libraw_processed_image_t *thumb_image = NULL; try { // unpack data if(RawProcessor->unpack_thumb() != LIBRAW_SUCCESS) { // run silently "LibRaw : failed to run unpack_thumb" return NULL; } // retrieve thumb image int error_code = 0; thumb_image = RawProcessor->dcraw_make_mem_thumb(&error_code); if(thumb_image) { if(thumb_image->type != LIBRAW_IMAGE_BITMAP) { // attach the binary data to a memory stream FIMEMORY *hmem = FreeImage_OpenMemory((BYTE*)thumb_image->data, (DWORD)thumb_image->data_size); // get the file type FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0); if(fif == FIF_JPEG) { // rotate according to Exif orientation flags |= JPEG_EXIFROTATE; } // load an image from the memory stream dib = FreeImage_LoadFromMemory(fif, hmem, flags); // close the stream FreeImage_CloseMemory(hmem); } else if((flags & FIF_LOAD_NOPIXELS) != FIF_LOAD_NOPIXELS) { // convert processed data to output dib dib = libraw_ConvertProcessedImageToDib(thumb_image); } } else { throw "LibRaw : failed to run dcraw_make_mem_thumb"; } // clean-up and return RawProcessor->dcraw_clear_mem(thumb_image); return dib; } catch(const char *text) { // clean-up and return if(thumb_image) { RawProcessor->dcraw_clear_mem(thumb_image); } if(text != NULL) { FreeImage_OutputMessageProc(s_format_id, text); } } return NULL; }
FIBITMAP* FreeImage_LoadFromMem(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags) { if (handle && handle->data) { FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)handle->data, handle->datalen); FREE_IMAGE_FORMAT _fif = (fif != FIF_UNKNOWN) ? fif : FreeImage_GetFileTypeFromMemory(hmem, 0); FIBITMAP *dib = FreeImage_LoadFromMemory(_fif, hmem, flags); FreeImage_CloseMemory(hmem); } return NULL; }
void FI(loadImageFile)(Bitmap& bitmap, Gosu::Reader input) { // Read all available input std::vector<BYTE> data(input.resource().size() - input.position()); input.read(&data[0], data.size()); FIMEMORY* fim = FreeImage_OpenMemory(&data[0], data.size()); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fim); FIBITMAP* fib = FreeImage_LoadFromMemory(fif, fim, GOSU_FIFLAGS); checkForFreeImageErrors(fib != 0); fibToBitmap(bitmap, fib, fif); }
int CPropertiesFiles::previewWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { static bool fDowned=false; switch (uMsg) { case WM_INITDIALOG: if (!fiBitmap) { FIMEMORY* fiMemory=FreeImage_OpenMemory((BYTE*)lParam,bitmapInfo->unc_size); RECT rect; POINT pt={0,0}; if (fiBitmap=FreeImage_LoadFromMemory(_tcsicmp(bitmapInfo->name+_tcslen(bitmapInfo->name)-4,".tga")?_tcsicmp(bitmapInfo->name+_tcslen(bitmapInfo->name)-4,".bmp")?FIF_JPEG:FIF_BMP:FIF_TARGA,fiMemory)) { ::GetWindowRect(hwndDlg,&rect); ::ClientToScreen(hwndDlg,&pt); rect.right=FreeImage_GetWidth(fiBitmap)+(pt.x-rect.left); rect.bottom=FreeImage_GetHeight(fiBitmap)+(pt .y-rect.top);; ::MoveWindow(hwndDlg,rect.left,rect.top,rect.right,rect.bottom,FALSE); FreeImage_CloseMemory(fiMemory); ::SetWindowText(hwndDlg,_tcsrchr(bitmapInfo->name,'/')+1); fDowned=false; } else { fDowned=true; ::EndDialog(hwndDlg,0); } } break; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: fDowned=true; break; case WM_CLOSE: fDowned=true; case WM_LBUTTONUP: case WM_RBUTTONUP: if (fDowned) { FreeImage_Unload(fiBitmap); fiBitmap=NULL; ::EndDialog(hwndDlg,0); } break; case WM_PAINT: { HDC hDC=::GetDC(hwndDlg); ::SetStretchBltMode(hDC, COLORONCOLOR); ::StretchDIBits(hDC, 0,0,FreeImage_GetWidth(fiBitmap), FreeImage_GetHeight(fiBitmap), 0, 0, FreeImage_GetWidth(fiBitmap), FreeImage_GetHeight(fiBitmap), FreeImage_GetBits(fiBitmap), FreeImage_GetInfo(fiBitmap), DIB_RGB_COLORS, SRCCOPY); ::ReleaseDC(hwndDlg,hDC); } } return FALSE; }
void TextureManager::loadTexture2DMem(unsigned char* imageData,long length) { FIMEMORY* hmem = FreeImage_OpenMemory(imageData,length); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); FIBITMAP* image = FreeImage_LoadFromMemory(fif, hmem, 0); FIBITMAP* temp = image; image = FreeImage_ConvertTo32Bits(image); FreeImage_Unload(temp); FreeImage_CloseMemory(hmem); int w = FreeImage_GetWidth(image); int h = FreeImage_GetHeight(image); printf("[Texture Manager] Image loaded from memory, is %d x %d\n",w,h); GLubyte* textureBytes = new GLubyte[4*w*h]; char* pixels = (char*)FreeImage_GetBits(image); //FreeImage loads in BGR format, so we need to swap some bytes(Or use GL_BGR) for(int j= 0; j<w*h; j++) { textureBytes[j*4+0]= pixels[j*4+2]; textureBytes[j*4+1]= pixels[j*4+1]; textureBytes[j*4+2]= pixels[j*4+0]; textureBytes[j*4+3]= pixels[j*4+3]; } glGenTextures(1, &(textInt)); glBindTexture(GL_TEXTURE_2D, textInt); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textureBytes ); Textures.push_back(textInt); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); GLenum errorLoading = glGetError(); if(errorLoading) { cout<<"There was an error loading the texture: "<<errorLoading<<endl; } }
bool FreeImageData::loadMem(unsigned char*data, unsigned long size, std::string filename) { FIMEMORY * mem = FreeImage_OpenMemory(data, size); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, size); if (fif == FIF_UNKNOWN){ fif = FreeImage_GetFIFFromFilename(filename.c_str()); } if(fif == FIF_UNKNOWN) return false; m_bitmap = FreeImage_LoadFromMemory(fif, mem); FreeImage_CloseMemory(mem); return m_bitmap != NULL; }
//---------------------------------------------------- bool ofImage::loadImageFromMemory(const ofBuffer & buffer, ofPixels &pix){ int width, height, bpp; bool bLoaded = false; FIBITMAP * bmp = NULL; FIMEMORY *hmem = NULL; printf("loadImageFromMemory\n"); hmem = FreeImage_OpenMemory((unsigned char*)buffer.getBuffer(), buffer.size()); if (hmem == NULL){ ofLog(OF_LOG_ERROR,"couldn't create memory handle! \n"); return false; } //get the file type! FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); if( fif == -1 ){ ofLog(OF_LOG_ERROR,"unable to guess format", fif); return false; FreeImage_CloseMemory(hmem); } //make the image!! bmp = FreeImage_LoadFromMemory(fif, hmem, 0); if( bmp != NULL ){ bLoaded = true; ofLog(OF_LOG_VERBOSE,"FreeImage_LoadFromMemory worked!\n"); } //----------------------------- if (bLoaded){ putBmpIntoPixels(bmp,pix); } else { width = height = bpp = 0; } if (bmp != NULL){ FreeImage_Unload(bmp); } if( hmem != NULL ){ FreeImage_CloseMemory(hmem); } return bLoaded; }
static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer){ ofInitFreeImage(); bool bLoaded = false; FIBITMAP* bmp = NULL; FIMEMORY* hmem = NULL; hmem = FreeImage_OpenMemory((unsigned char*) buffer.getBinaryBuffer(), buffer.size()); if (hmem == NULL){ ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, opening FreeImage memory failed"; return false; } //get the file type! FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); if( fif == -1 ){ ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, unable to guess image format from memory"; return false; FreeImage_CloseMemory(hmem); } //make the image!! bmp = FreeImage_LoadFromMemory(fif, hmem, 0); if( bmp != NULL ){ bLoaded = true; } //----------------------------- if (bLoaded){ putBmpIntoPixels(bmp,pix); } if (bmp != NULL){ FreeImage_Unload(bmp); } if( hmem != NULL ){ FreeImage_CloseMemory(hmem); } return bLoaded; }
static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer){ ofInitFreeImage(); bool bLoaded = false; FIBITMAP* bmp = NULL; FIMEMORY* hmem = NULL; hmem = FreeImage_OpenMemory((unsigned char*) buffer.getBinaryBuffer(), buffer.size()); if (hmem == NULL){ ofLog(OF_LOG_ERROR, "couldn't create memory handle!"); return false; } //get the file type! FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); if( fif == -1 ){ ofLog(OF_LOG_ERROR, "unable to guess format", fif); return false; FreeImage_CloseMemory(hmem); } //make the image!! bmp = FreeImage_LoadFromMemory(fif, hmem, 0); if( bmp != NULL ){ bLoaded = true; } //----------------------------- if (bLoaded){ putBmpIntoPixels(bmp,pix); } if (bmp != NULL){ FreeImage_Unload(bmp); } if( hmem != NULL ){ FreeImage_CloseMemory(hmem); } return bLoaded; }
static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam) { IMGSRVC_MEMIO *mio = (IMGSRVC_MEMIO *)wParam; if(mio->iLen == 0 || mio->pBuf == NULL) return 0; FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)mio->pBuf, mio->iLen); FREE_IMAGE_FORMAT fif = (mio->fif != FIF_UNKNOWN) ? mio->fif : mio->fif = FreeImage_GetFileTypeFromMemory(hmem, 0); FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, mio->flags); FreeImage_CloseMemory(hmem); if(dib == NULL || (lParam & IMGL_RETURNDIB)) return (INT_PTR)dib; HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib); FreeImage_Unload(dib); return (INT_PTR)hbm; }
std::shared_ptr<FIBITMAP> load_png_from_memory(const void* memory_location, size_t size) { FREE_IMAGE_FORMAT fif = FIF_PNG; auto memory = std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)>( FreeImage_OpenMemory(static_cast<BYTE*>(const_cast<void*>(memory_location)), size), FreeImage_CloseMemory); auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadFromMemory(fif, memory.get(), 0), FreeImage_Unload); if (FreeImage_GetBPP(bitmap.get()) != 32) { bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload); if (!bitmap) BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format.")); } return bitmap; }
static FIBITMAP* mng_LoadFromMemoryHandle(FIMEMORY *hmem, int flags = 0) { long offset = 0; FIBITMAP *dib = NULL; if(hmem) { // seek to the start of the stream FreeImage_SeekMemory(hmem, offset, SEEK_SET); // check the file signature and deduce its format // (the second argument is currently not used by FreeImage) FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0); if(fif != FIF_UNKNOWN) { dib = FreeImage_LoadFromMemory(fif, hmem, flags); } } return dib; }
void testSaveMemIO(const char *lpszPathName) { FIMEMORY *hmem = NULL; // load a regular file FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName); FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0); // open a memory handle hmem = FreeImage_OpenMemory(); // save the file to memory FreeImage_SaveToMemory(fif, dib, hmem, 0); // at this point, hmem contains the entire PNG data in memory. // the amount of space used by the memory is equal to file_size long file_size = FreeImage_TellMemory(hmem); printf("File size : %ld\n", file_size); // its easy load an image from memory as well // seek to the start of the memory stream FreeImage_SeekMemory(hmem, 0L, SEEK_SET); // get the file type FREE_IMAGE_FORMAT mem_fif = FreeImage_GetFileTypeFromMemory(hmem, 0); // load an image from the memory handle FIBITMAP *check = FreeImage_LoadFromMemory(mem_fif, hmem, 0); // save as a regular file FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT); // make sure to free the data since FreeImage_SaveToMemory // will cause it to be malloc'd FreeImage_CloseMemory(hmem); FreeImage_Unload(check); FreeImage_Unload(dib); }
FIBITMAP* loadWatermark() { HRSRC hRes = ::FindResource(NULL, MAKEINTRESOURCE(PNG_Visus_Logo), "PNG"); DWORD dwResSize = SizeofResource(NULL, hRes); HGLOBAL hGlobal = ::LoadResource(NULL, hRes); BYTE* pData = (BYTE*)LockResource(hGlobal); FIMEMORY* fiMem = FreeImage_OpenMemory(pData, dwResSize); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fiMem); FIBITMAP* fiBmp = FreeImage_LoadFromMemory(fif, fiMem); FreeImage_CloseMemory(fiMem); UnlockResource(hGlobal); DeleteObject(hGlobal); DeleteObject(hRes); return fiBmp; }
VALUE rb_graffik_from_memory(VALUE self, VALUE rb_data) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; Check_Type(rb_data, T_STRING); // Get info about our in-memory image BYTE *data_ptr = (BYTE *) RSTRING_PTR(rb_data); DWORD data_length = RSTRING_LEN(rb_data); // Open up the memory stream FIMEMORY *stream = FreeImage_OpenMemory(data_ptr, data_length); // Make sure the stream was open if (stream == NULL) { rb_raise(rb_eTypeError, "Unable to read image from memory"); } fif = FreeImage_GetFileTypeFromMemory(stream, 0); if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { int flags = ((fif == FIF_JPEG) ? JPEG_ACCURATE : 0); // Load the image from disk FIBITMAP *image = FreeImage_LoadFromMemory(fif, stream, flags); // Release memory FreeImage_CloseMemory(stream); // Develop an instance for Ruby VALUE instance = Data_Wrap_Struct(self, NULL, NULL, image); // Store the image type as a FixNum rb_iv_set(instance, "@file_type", INT2FIX(fif)); // If a block is given, yield to it, if not, return the instance if (rb_block_given_p()) { return rb_ensure(rb_yield, instance, rb_graffik_close, instance); } else { return instance; } } // If we couldn't load it, throw and error rb_raise(rb_eTypeError, "Unknown file format"); }
// 从内存中载入图片 Bool FreeImageImage::loadFromMemory(PCVoid pData, UInt nDataSize) { unloadImage(); FREE_IMAGE_FORMAT eImgFormat = FIF_UNKNOWN; FIMEMORY* pFIMemory = FreeImage_OpenMemory((UInt8*) pData, nDataSize); FIBITMAP* pFIBitmap = 0; eImgFormat = FreeImage_GetFileTypeFromMemory(pFIMemory); if(eImgFormat != FIF_UNKNOWN && FreeImage_FIFSupportsReading(eImgFormat)) { pFIBitmap = FreeImage_LoadFromMemory(eImgFormat, pFIMemory, 0); } Bool isSuccess = False; if(pFIBitmap) { // 非 32 位色图片,转换为 32 位 UInt nBPP = FreeImage_GetBPP(pFIBitmap); if(nBPP != 32) { FIBITMAP* pFIBitmap32 = FreeImage_ConvertTo32Bits(pFIBitmap); FreeImage_Unload(pFIBitmap); pFIBitmap = pFIBitmap32; } } if(pFIBitmap) { // 加载到内存数据中 isSuccess = _load(pFIBitmap); FreeImage_Unload(pFIBitmap); } FreeImage_CloseMemory(pFIMemory); return isSuccess; }
//------------------------------------------------------------------ void ofxAdvancedImage::loadFromData(unsigned char * datasource, int len) { //Load image from memory code, based on Zach's code updated by madparker on OF Forum //if we already have a loaded image clear it // if(isValid()){ clear(); // } //create a freeimage memory handle from the buffer address FIMEMORY *hmem = NULL; hmem = FreeImage_OpenMemory((BYTE *)datasource,len); if (hmem == NULL){ printf("couldn't create memory handle! \n"); return; } //get the file type! FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem); //make the image!! putBmpIntoPixels(FreeImage_LoadFromMemory(fif, hmem, 0), myPixels); // bmp = FreeImage_LoadFromMemory(fif, hmem, 0); //free our memory FreeImage_CloseMemory(hmem); if (getBmpFromPixels(myPixels) == NULL){ printf("couldn't create bmp! \n"); return; } //flip it! FreeImage_FlipVertical(getBmpFromPixels(myPixels)); if (myPixels.bAllocated == true && bUseTexture == true){ tex.allocate(myPixels.width, myPixels.height, myPixels.glDataType); } swapRgb(myPixels); update(); reset(); }
void testLoadMemIO(const char *lpszPathName) { struct stat buf; int result; // get data associated with lpszPathName result = stat(lpszPathName, &buf); if(result == 0) { // allocate a memory buffer and load temporary data BYTE *mem_buffer = (BYTE*)malloc(buf.st_size * sizeof(BYTE)); if(mem_buffer) { FILE *stream = fopen(lpszPathName, "rb"); if(stream) { fread(mem_buffer, sizeof(BYTE), buf.st_size, stream); fclose(stream); // attach the binary data to a memory stream FIMEMORY *hmem = FreeImage_OpenMemory(mem_buffer, buf.st_size); // get the file type FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0); // load an image from the memory stream FIBITMAP *check = FreeImage_LoadFromMemory(fif, hmem, PNG_DEFAULT); // save as a regular file FreeImage_Save(FIF_PNG, check, "blob.png", PNG_DEFAULT); FreeImage_Unload(check); // close the stream FreeImage_CloseMemory(hmem); } } // user is responsible for freeing the data free(mem_buffer); } }
static FIBITMAP *loadImage(const std::string &file, bool convertTo32bit = true) { #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(!fileMap.open_file(file.c_str())) return NULL; FIMEMORY *imgMEM = FreeImage_OpenMemory(reinterpret_cast<unsigned char *>(fileMap.data()), (unsigned int)fileMap.size()); FREE_IMAGE_FORMAT formato = FreeImage_GetFileTypeFromMemory(imgMEM); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_LoadFromMemory(formato, imgMEM, 0); FreeImage_CloseMemory(imgMEM); fileMap.close_file(); if(!img) return NULL; #else FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(file.c_str(), 0); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_Load(formato, file.c_str()); if(!img) return NULL; #endif if(convertTo32bit) { FIBITMAP *temp; temp = FreeImage_ConvertTo32Bits(img); if(!temp) return NULL; FreeImage_Unload(img); img = temp; } return img; }
FIBITMAP* getFIInfo(MemChunk& data, SImage::info_t& info) { // Get FreeImage bitmap info from entry data FIMEMORY* mem = FreeImage_OpenMemory((BYTE*)data.getData(), data.getSize()); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, 0); FIBITMAP* bm = FreeImage_LoadFromMemory(fif, mem, 0); FreeImage_CloseMemory(mem); // Check it created/read ok if (!bm) return nullptr; // Get info from image info.width = FreeImage_GetWidth(bm); info.height = FreeImage_GetHeight(bm); info.colformat = RGBA; // Generic images always converted to RGBA on loading info.format = id_; // Check if palette supplied if (FreeImage_GetColorsUsed(bm) > 0) info.has_palette = true; return bm; }
BOOL DLL_CALLCONV FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) { if (bitmap) { BOOL success = TRUE; if (bitmap->data) { MULTIBITMAPHEADER *header = FreeImage_GetMultiBitmapHeader(bitmap); if (header->changed) { // open a temp file char spool_name[256]; ReplaceExtension(spool_name, header->m_filename, "fispool"); // open the spool file and the source file FILE *f = fopen(spool_name, "w+b"); void *data = FreeImage_Open(header->node, header->io, (fi_handle)f, FALSE); void *data_read = NULL; if (header->handle) { header->io->seek_proc(header->handle, 0, SEEK_SET); data_read = FreeImage_Open(header->node, header->io, header->handle, TRUE); } // write all the pages to the temp file int count = 0; for (BlockListIterator i = header->m_blocks.begin(); i != header->m_blocks.end(); i++) { if (success) { switch((*i)->m_type) { case BLOCK_CONTINUEUS : { BlockContinueus *block = (BlockContinueus *)(*i); for (int j = block->m_start; j <= block->m_end; j++) { FIBITMAP *dib = header->node->m_plugin->load_proc(header->io, header->handle, j, header->load_flags, data_read); success = header->node->m_plugin->save_proc(header->io, dib, (fi_handle)f, count, flags, data); count++; FreeImage_Unload(dib); } break; } case BLOCK_REFERENCE : { BlockReference *ref = (BlockReference *)(*i); // read the compressed data BYTE *compressed_data = (BYTE*)malloc(ref->m_size * sizeof(BYTE)); header->m_cachefile->readFile((BYTE *)compressed_data, ref->m_reference, ref->m_size); // uncompress the data FIMEMORY *hmem = FreeImage_OpenMemory(compressed_data, ref->m_size); FIBITMAP *dib = FreeImage_LoadFromMemory(header->cache_fif, hmem, 0); FreeImage_CloseMemory(hmem); // get rid of the buffer free(compressed_data); // save the data success = header->node->m_plugin->save_proc(header->io, dib, (fi_handle)f, count, flags, data); count++; // unload the dib FreeImage_Unload(dib); break; } } } else { break; } } // close the files FreeImage_Close(header->node, header->io, (fi_handle)f, data); fclose(f); if (header->handle) { FreeImage_Close(header->node, header->io, header->handle, data_read); fclose((FILE *)header->handle); } if (success) { remove(header->m_filename); rename(spool_name, header->m_filename); } else { remove(spool_name); } } else { if (header->handle && header->m_filename) { fclose((FILE *)header->handle); } } // clear the blocks list for (BlockListIterator i = header->m_blocks.begin(); i != header->m_blocks.end(); ++i) delete *i; // flush and dispose the cache if (header->m_cachefile) { header->m_cachefile->close(); delete header->m_cachefile; } // delete the last open bitmaps while (!header->locked_pages.empty()) { FreeImage_Unload(header->locked_pages.begin()->first); header->locked_pages.erase(header->locked_pages.begin()->first); } // get rid of the IO structure delete header->io; // delete the filename if(header->m_filename) delete[] header->m_filename; // delete the FIMULTIBITMAPHEADER delete header; } delete bitmap; return success; } return FALSE; }
FIBITMAP* fipMemoryIO::load(FREE_IMAGE_FORMAT fif, int flags) const { return FreeImage_LoadFromMemory(fif, _hmem, flags); }
GLuint OGLRenderSystem::GetTextureCache( TextureBase* tex ) { GLuint& cache = (GLuint&)tex->mCache; if( tex->IsDirty( ) ) { if( cache ) { glDeleteTextures( 1, &cache ); cache = 0; } int texformat = tex->GetFormat( ); if( texformat == 0 ) { glGenTextures( 1, &cache ); if( !cache ) { printf( "Failed to generate texture :: cache = 0\n" ); return 0; } glBindTexture( GL_TEXTURE_2D, cache ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); DWORD dwFlags = *(DWORD*)(tex->mData + 0x50); if( dwFlags & 0x40 ) { DWORD bpp = *(DWORD*)(tex->mData + 0x58); int hasAlpha = dwFlags & 0x1; DWORD aMask = *(DWORD*)(tex->mData + 0x68); int type; int format; int data; if( bpp == 16 ) { if( aMask == 0x80 ) { //a1r5g5b5 format = GL_RGB5_A1; type = GL_BGRA; data = GL_UNSIGNED_SHORT_1_5_5_5_REV; } else { //a4r4g4b4 if( hasAlpha ) { format = GL_RGBA4; type = GL_BGRA; data = GL_UNSIGNED_SHORT_4_4_4_4_REV; } else { format = GL_RGB4; type = GL_BGR; data = GL_UNSIGNED_SHORT_4_4_4_4_REV; } } } else if( bpp == 32 ) { if( hasAlpha ) { format = GL_RGBA; type = GL_BGRA; data = GL_UNSIGNED_INT_8_8_8_8_REV; } else { format = GL_RGB; type = GL_BGR; data = GL_UNSIGNED_INT_8_8_8_8_REV; } } DWORD width = tex->GetWidth( ); DWORD height = tex->GetHeight( ); glTexImage2D( GL_TEXTURE_2D, 0, format, width, height, 0, type, data, (GLvoid*)(tex->mData + 0x80) ); } else { FIMEMORY* imgmem = FreeImage_OpenMemory( tex->mData, tex->mDataLen ); FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory( imgmem ); FIBITMAP* img = FreeImage_LoadFromMemory( fif, imgmem, 0 ); FIBITMAP* temp = img; img = FreeImage_ConvertTo32Bits( img ); FreeImage_FlipVertical( img ); glTexImage2D( GL_TEXTURE_2D, 0, 4, FreeImage_GetWidth( img ), FreeImage_GetHeight( img ), 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)FreeImage_GetBits( img ) ); FreeImage_Unload( temp ); FreeImage_Unload( img ); FreeImage_CloseMemory( imgmem ); } } else if( texformat == TextureFormat::A8 ) { glGenTextures( 1, &cache ); glBindTexture( GL_TEXTURE_2D, cache ); glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA8, tex->GetWidth( ), tex->GetHeight( ), 0, GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid*)tex->mData ); } else { printf( "Failed to create Texture Cache :: Invalid format: %d\n", texformat ); return 0; } tex->SetDirty( false ); } return cache; };
BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToHandle(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FreeImageIO *io, fi_handle handle, int flags) { if(!bitmap || !bitmap->data || !io || !handle) { return FALSE; } BOOL success = TRUE; // retrieve the plugin list to find the node belonging to this plugin PluginList *list = FreeImage_GetPluginList(); if (list) { PluginNode *node = list->FindNodeFromFIF(fif); if(node) { MULTIBITMAPHEADER *header = FreeImage_GetMultiBitmapHeader(bitmap); // dst data void *data = FreeImage_Open(node, io, handle, FALSE); // src data void *data_read = NULL; if(header->handle) { // open src header->io->seek_proc(header->handle, 0, SEEK_SET); data_read = FreeImage_Open(header->node, header->io, header->handle, TRUE); } // write all the pages to the file using handle and io int count = 0; for (BlockListIterator i = header->m_blocks.begin(); i != header->m_blocks.end(); i++) { if (success) { switch((*i)->m_type) { case BLOCK_CONTINUEUS: { BlockContinueus *block = (BlockContinueus *)(*i); for (int j = block->m_start; j <= block->m_end; j++) { // load the original source data FIBITMAP *dib = header->node->m_plugin->load_proc(header->io, header->handle, j, header->load_flags, data_read); // save the data success = node->m_plugin->save_proc(io, dib, handle, count, flags, data); count++; FreeImage_Unload(dib); } break; } case BLOCK_REFERENCE: { BlockReference *ref = (BlockReference *)(*i); // read the compressed data BYTE *compressed_data = (BYTE*)malloc(ref->m_size * sizeof(BYTE)); header->m_cachefile->readFile((BYTE *)compressed_data, ref->m_reference, ref->m_size); // uncompress the data FIMEMORY *hmem = FreeImage_OpenMemory(compressed_data, ref->m_size); FIBITMAP *dib = FreeImage_LoadFromMemory(header->cache_fif, hmem, 0); FreeImage_CloseMemory(hmem); // get rid of the buffer free(compressed_data); // save the data success = node->m_plugin->save_proc(io, dib, handle, count, flags, data); count++; // unload the dib FreeImage_Unload(dib); break; } } } else { break; } } // close the files FreeImage_Close(header->node, header->io, header->handle, data_read); FreeImage_Close(node, io, handle, data); return success; } } return FALSE; }
FIBITMAP *GraphicsHelps::loadImage(std::string file, bool convertTo32bit) { #ifdef DEBUG_BUILD ElapsedTimer loadingTime; ElapsedTimer fReadTime; ElapsedTimer imgConvTime; loadingTime.start(); fReadTime.start(); #endif #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(!fileMap.open_file(file.c_str())) return NULL; FIMEMORY *imgMEM = FreeImage_OpenMemory(reinterpret_cast<unsigned char *>(fileMap.data()), static_cast<unsigned int>(fileMap.size())); FREE_IMAGE_FORMAT formato = FreeImage_GetFileTypeFromMemory(imgMEM); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_LoadFromMemory(formato, imgMEM, 0); FreeImage_CloseMemory(imgMEM); fileMap.close_file(); if(!img) return NULL; #else FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(file.toUtf8().data(), 0); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_Load(formato, file.toUtf8().data()); if(!img) return NULL; #endif #ifdef DEBUG_BUILD long long fReadTimeElapsed = static_cast<long long>(fReadTime.elapsed()); long long imgConvertElapsed = 0; #endif if(convertTo32bit) { #ifdef DEBUG_BUILD imgConvTime.start(); #endif FIBITMAP *temp; temp = FreeImage_ConvertTo32Bits(img); if(!temp) return NULL; FreeImage_Unload(img); img = temp; #ifdef DEBUG_BUILD imgConvertElapsed = static_cast<long long>(imgConvTime.elapsed()); #endif } #ifdef DEBUG_BUILD D_pLogDebug("File read of texture %s passed in %d milliseconds", file.c_str(), static_cast<int>(fReadTimeElapsed)); D_pLogDebug("Conv to 32-bit of %s passed in %d milliseconds", file.c_str(), static_cast<int>(imgConvertElapsed)); D_pLogDebug("Total Loading of image %s passed in %d milliseconds", file.c_str(), static_cast<int>(loadingTime.elapsed())); #endif return img; }