void ofImage_<PixelType>::changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType newType){ int oldType = pix.getImageType(); if (oldType == newType) { return; // no need to reallocate } FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = nullptr; switch (newType){ case OF_IMAGE_GRAYSCALE: convertedBmp = FreeImage_ConvertToGreyscale(bmp); break; case OF_IMAGE_COLOR: convertedBmp = FreeImage_ConvertTo24Bits(bmp); break; case OF_IMAGE_COLOR_ALPHA: convertedBmp = FreeImage_ConvertTo32Bits(bmp); break; default: ofLogError("ofImage") << "changeTypeOfPixels(): unknown image type: " << newType; break; } putBmpIntoPixels(convertedBmp, pix, false); if (bmp != nullptr) { FreeImage_Unload(bmp); } if (convertedBmp != nullptr) { FreeImage_Unload(convertedBmp); } }
void ofImage_<PixelType>::resizePixels(ofPixels_<PixelType> &pix, int newWidth, int newHeight){ FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; convertedBmp = FreeImage_Rescale(bmp, newWidth, newHeight, FILTER_BICUBIC); putBmpIntoPixels(convertedBmp, pix, false); if (bmp != NULL) FreeImage_Unload(bmp); if (convertedBmp != NULL) FreeImage_Unload(convertedBmp); }
//---------------------------------------------------- void ofxImage::rotatePixels(ofPixels &pix, float angle){ if(angle == 0.0) return; FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; convertedBmp = FreeImage_RotateClassic(bmp, angle); putBmpIntoPixels(convertedBmp, pix); if (bmp != NULL) FreeImage_Unload(bmp); if (convertedBmp != NULL) FreeImage_Unload(convertedBmp); }
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; }
//---------------------------------------------------- void ofImage::changeTypeOfPixels(ofPixels &pix, ofImageType newType){ if (pix.getImageType() == newType) return; FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; // check if we need to reallocate the texture. bool bNeedNewTexture = false; int oldType = pix.getImageType(); if (newType > oldType){ bNeedNewTexture = true; } // new type ! switch (newType){ //------------------------------------ case OF_IMAGE_GRAYSCALE: convertedBmp = FreeImage_ConvertToGreyscale(bmp); break; //------------------------------------ case OF_IMAGE_COLOR: convertedBmp = FreeImage_ConvertTo24Bits(bmp); if (bNeedNewTexture){ tex.clear(); tex.allocate(pixels.getWidth(), pixels.getHeight(), GL_RGB); } break; //------------------------------------ case OF_IMAGE_COLOR_ALPHA: convertedBmp = FreeImage_ConvertTo32Bits(bmp); if (bNeedNewTexture){ tex.clear(); tex.allocate(pixels.getWidth(), pixels.getHeight(), GL_RGBA); } break; default: ofLog(OF_LOG_ERROR,"ofImage: format not supported"); } putBmpIntoPixels(convertedBmp, pix, false); if (bmp != NULL) FreeImage_Unload(bmp); if (convertedBmp != NULL) FreeImage_Unload(convertedBmp); }
//---------------------------------------------------- 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; }
//---------------------------------------------------- void ofImage::changeTypeOfPixels(ofPixels &pix, int newType){ if (pix.ofImageType == newType) return; FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; // check if we need to reallocate the texture. bool bNeedNewTexture = false; int oldType = pix.ofImageType; if (newType > oldType){ bNeedNewTexture = true; } // new type ! switch (newType){ //------------------------------------ case OF_IMAGE_GRAYSCALE: convertedBmp = FreeImage_ConvertToGreyscale(bmp); break; //------------------------------------ case OF_IMAGE_COLOR: convertedBmp = FreeImage_ConvertTo24Bits(bmp); if (bNeedNewTexture){ tex.clear(); tex.allocate(myPixels.width, myPixels.height, GL_RGB); } break; //------------------------------------ case OF_IMAGE_COLOR_ALPHA: convertedBmp = FreeImage_ConvertTo32Bits(bmp); if (bNeedNewTexture){ tex.clear(); tex.allocate(myPixels.width, myPixels.height, GL_RGBA); } break; } putBmpIntoPixels(convertedBmp, pix); if (bmp != NULL) FreeImage_Unload(bmp); if (convertedBmp != NULL) FreeImage_Unload(convertedBmp); }
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, string fileName){ ofInitFreeImage(); #ifndef TARGET_EMSCRIPTEN // Attempt to parse the fileName as a url - specifically it must be a full address starting with http/https // Poco::URI normalizes to lowercase Poco::URI uri; try { uri = Poco::URI(fileName); } catch(const Poco::SyntaxException& exc){ ofLogError("ofImage") << "loadImage(): malformed url when loading image from url \"" << fileName << "\": " << exc.displayText(); return false; } if(uri.getScheme() == "http" || uri.getScheme() == "https"){ return ofLoadImage(pix, ofLoadURL(fileName).data); } #endif fileName = ofToDataPath(fileName); bool bLoaded = false; FIBITMAP * bmp = NULL; 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)) { bmp = FreeImage_Load(fif, fileName.c_str(), 0); if (bmp != NULL){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } if (bmp != NULL){ FreeImage_Unload(bmp); } return bLoaded; }
static bool loadImage(ofPixels_<PixelType> & pix, string fileName){ ofInitFreeImage(); #ifndef TARGET_EMSCRIPTEN // Attempt to parse the fileName as a url - specifically it must be a full address starting with http/https network::uri uri; try { uri = network::uri(fileName); } catch(const std::exception &){ } if(uri.scheme() != boost::none && (uri.scheme().get() == "http" || uri.scheme().get() == "https")){ return ofLoadImage(pix, ofLoadURL(fileName).data); }else if(uri.scheme()!=boost::none){ ofLogError() << "protocol " << uri.scheme().get() << " not supported"; } #endif fileName = ofToDataPath(fileName); bool bLoaded = false; FIBITMAP * bmp = NULL; 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)) { bmp = FreeImage_Load(fif, fileName.c_str(), 0); if (bmp != NULL){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } if (bmp != NULL){ FreeImage_Unload(bmp); } return bLoaded; }
//---------------------------------------------------- void ofxImage::flipPixels(ofPixels &pix, bool horizontal, bool vertical){ if(!horizontal && !vertical) return; FIBITMAP * bmp = getBmpFromPixels(pix); bool horSuccess = false, vertSuccess = false; if(horizontal) horSuccess = FreeImage_FlipHorizontal(bmp); if(vertical) vertSuccess = FreeImage_FlipVertical(bmp); if(horSuccess || vertSuccess) putBmpIntoPixels(bmp, pix); if (bmp != NULL) FreeImage_Unload(bmp); }
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 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; }
void ofImage_<PixelType>::changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType newType){ int oldType = pix.getImageType(); if (oldType == newType) { return; // no need to reallocate } FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; switch (newType){ case OF_IMAGE_GRAYSCALE: convertedBmp = FreeImage_ConvertToGreyscale(bmp); break; case OF_IMAGE_COLOR: convertedBmp = FreeImage_ConvertTo24Bits(bmp); break; case OF_IMAGE_COLOR_ALPHA: convertedBmp = FreeImage_ConvertTo32Bits(bmp); break; default: ofLogError("ofImage") << "changeTypeOfPixels(): unknown image type: " << newType; break; } putBmpIntoPixels(convertedBmp, pix, false); if (bmp != NULL) { FreeImage_Unload(bmp); } if (convertedBmp != NULL) { FreeImage_Unload(convertedBmp); } if(bUseTexture){ // always reallocate the texture. if ofTexture doesn't need reallocation, // it doesn't have to. but it needs to change the internal format. tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels)); if(ofGetGLProgrammableRenderer() && (pixels.getNumChannels()==1 || pixels.getNumChannels()==2)){ tex.setRGToRGBASwizzles(true); } } }
void ofImage_<PixelType>::changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType newType){ int oldType = pix.getImageType(); if (oldType == newType) { return; // no need to reallocate } FIBITMAP * bmp = getBmpFromPixels(pix); FIBITMAP * convertedBmp = NULL; switch (newType){ case OF_IMAGE_GRAYSCALE: convertedBmp = FreeImage_ConvertToGreyscale(bmp); break; case OF_IMAGE_COLOR: convertedBmp = FreeImage_ConvertTo24Bits(bmp); break; case OF_IMAGE_COLOR_ALPHA: convertedBmp = FreeImage_ConvertTo32Bits(bmp); break; default: ofLog(OF_LOG_ERROR, "changeTypeOfPixels: format not supported"); break; } putBmpIntoPixels(convertedBmp, pix, false); if (bmp != NULL) { FreeImage_Unload(bmp); } if (convertedBmp != NULL) { FreeImage_Unload(convertedBmp); } if(bUseTexture){ // always reallocate the texture. if ofTexture doesn't need reallocation, // it doesn't have to. but it needs to change the internal format. tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels)); } }
static bool loadImage(ofPixels_<PixelType> & pix, string fileName){ ofInitFreeImage(); if(fileName.substr(0, 7) == "http://") { return ofLoadImage(pix, ofLoadURL(fileName).data); } int width, height, bpp; fileName = ofToDataPath(fileName); bool bLoaded = false; FIBITMAP * bmp = NULL; 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)) { bmp = FreeImage_Load(fif, fileName.c_str(), 0); if (bmp != NULL){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } else { width = height = bpp = 0; } if (bmp != NULL){ FreeImage_Unload(bmp); } return bLoaded; }
void ofMemoryImage::loadFromData(const unsigned char * datasource, int len) { bool bLoadedOk = false; bLoadedOk = loadFromDataIntoPixels(datasource, len, myPixels); if (bLoadedOk == true) { cout << "it's been loaded " << endl; if (myPixels.bAllocated == true && bUseTexture == true) { tex.allocate(myPixels.width, myPixels.height, myPixels.glDataType); } } FIBITMAP * bmp = getBmpFromPixels(myPixels); bool horSuccess = false;// FreeImage_FlipHorizontal(bmp); bool vertSuccess = FreeImage_FlipVertical(bmp); if(horSuccess || vertSuccess) { putBmpIntoPixels(bmp, myPixels); } if (bmp != NULL) FreeImage_Unload(bmp); update(); }
//------------------------------------------------------------------ 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(); }
//---------------------------------------------------- bool ofImage::loadImageIntoPixels(string fileName, ofPixels &pix){ int width, height, bpp; ofImageType type; fileName = ofToDataPath(fileName); bool bLoaded = false; FIBITMAP * bmp = NULL; 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)) { bmp = FreeImage_Load(fif, fileName.c_str(), 0); if (bmp){ bLoaded = true; } } //----------------------------- if ( bLoaded ){ putBmpIntoPixels(bmp,pix); } else { width = height = bpp = 0; } if (bmp != NULL){ FreeImage_Unload(bmp); } 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; }