static bool loadImage(ofPixels_<PixelType> & pix, const std::string& _fileName, const ofImageLoadSettings& settings){ ofInitFreeImage(); #ifndef TARGET_EMSCRIPTEN Poco::URI uri; try { uri = Poco::URI(_fileName); } catch(const std::exception & exc){ ofLogError("ofImage") << "loadImage(): malformed uri when loading image from uri \"" << _fileName << "\": " << exc.what(); return false; } if(uri.getScheme() == "http" || uri.getScheme() == "https"){ return ofLoadImage(pix, ofLoadURL(_fileName).data); } #endif std::string fileName = ofToDataPath(_fileName); bool bLoaded = false; FIBITMAP * bmp = nullptr; FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; fif = FreeImage_GetFileType(fileName.c_str(), 0); if(fif == FIF_UNKNOWN) { // or guess via filename fif = FreeImage_GetFIFFromFilename(fileName.c_str()); } if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { if(fif == FIF_JPEG) { int option = getJpegOptionFromImageLoadSetting(settings); bmp = FreeImage_Load(fif, fileName.c_str(), option); } else { bmp = FreeImage_Load(fif, fileName.c_str(), 0); } if (bmp != nullptr){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } if (bmp != nullptr){ FreeImage_Unload(bmp); } return bLoaded; }
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; }
static bool loadImage(ofPixels_<PixelType> & pix, const std::filesystem::path& _fileName, const ofImageLoadSettings& settings){ ofInitFreeImage(); auto uriStr = _fileName.string(); UriUriA uri; UriParserStateA state; state.uri = &uri; if(uriParseUriA(&state, uriStr.c_str())!=URI_SUCCESS){ const int bytesNeeded = 8 + 3 * strlen(uriStr.c_str()) + 1; std::vector<char> absUri(bytesNeeded); #ifdef TARGET_WIN32 uriWindowsFilenameToUriStringA(uriStr.c_str(), absUri.data()); #else uriUnixFilenameToUriStringA(uriStr.c_str(), absUri.data()); #endif if(uriParseUriA(&state, absUri.data())!=URI_SUCCESS){ ofLogError("ofImage") << "loadImage(): malformed uri when loading image from uri " << _fileName; uriFreeUriMembersA(&uri); return false; } } std::string scheme(uri.scheme.first, uri.scheme.afterLast); uriFreeUriMembersA(&uri); if(scheme == "http" || scheme == "https"){ return ofLoadImage(pix, ofLoadURL(_fileName.string()).data); } std::string fileName = ofToDataPath(_fileName, true); bool bLoaded = false; FIBITMAP * bmp = nullptr; FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; fif = FreeImage_GetFileType(fileName.c_str(), 0); if(fif == FIF_UNKNOWN) { // or guess via filename fif = FreeImage_GetFIFFromFilename(fileName.c_str()); } if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { if(fif == FIF_JPEG) { int option = getJpegOptionFromImageLoadSetting(settings); bmp = FreeImage_Load(fif, fileName.c_str(), option); } else { bmp = FreeImage_Load(fif, fileName.c_str(), 0); } if (bmp != nullptr){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } if (bmp != nullptr){ FreeImage_Unload(bmp); } return bLoaded; }