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); } }
//---------------------------------------------------------------- void ofImage::saveImageFromPixels(string fileName, ofPixels &pix){ if (pix.bAllocated == false){ ofLog(OF_LOG_ERROR,"error saving image - pixels aren't allocated"); return; } #ifdef TARGET_LITTLE_ENDIAN if (pix.bytesPerPixel != 1) swapRgb(pix); #endif FIBITMAP * bmp = getBmpFromPixels(pix); #ifdef TARGET_LITTLE_ENDIAN if (pix.bytesPerPixel != 1) swapRgb(pix); #endif fileName = ofToDataPath(fileName); if (pix.bAllocated == true){ 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)) { FreeImage_Save(fif, bmp, fileName.c_str(), 0); } } if (bmp != NULL){ FreeImage_Unload(bmp); } }
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); }
//---------------------------------------------------- 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); }
//------------------------------------------------------------------ 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 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); }
//---------------------------------------------------------------- void ofSaveImage(ofPixels & pix, string fileName, ofImageQualityType qualityLevel) { if (pix.isAllocated() == false){ ofLog(OF_LOG_ERROR,"error saving image - pixels aren't allocated"); return; } #ifdef TARGET_LITTLE_ENDIAN pix.swapRgb(); #endif FIBITMAP * bmp = getBmpFromPixels(pix); #ifdef TARGET_LITTLE_ENDIAN pix.swapRgb(); #endif fileName = ofToDataPath(fileName); 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 quality = JPEG_QUALITYSUPERB; switch(qualityLevel) { case OF_IMAGE_QUALITY_WORST: quality = JPEG_QUALITYBAD; break; case OF_IMAGE_QUALITY_LOW: quality = JPEG_QUALITYAVERAGE; break; case OF_IMAGE_QUALITY_MEDIUM: quality = JPEG_QUALITYNORMAL; break; case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break; case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break; } FreeImage_Save(fif, bmp, fileName.c_str(), quality); } else { if(qualityLevel != OF_IMAGE_QUALITY_BEST) { ofLog(OF_LOG_WARNING, "ofImageCompressionType only applies to JPEG images, ignoring value."); } FreeImage_Save(fif, bmp, fileName.c_str()); } } if (bmp != NULL){ FreeImage_Unload(bmp); } }
//---------------------------------------------------- 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); }
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)); } }
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(); }
static void saveImage(ofPixels_<PixelType> & pix, ofBuffer & buffer, ofImageFormat format, ofImageQualityType qualityLevel) { //thanks to alvaro casinelli for the implementation ofInitFreeImage(); if (pix.isAllocated() == false){ ofLog(OF_LOG_ERROR,"error saving image - pixels aren't allocated"); return; } #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1) { pix.swapRgb(); } #endif FIBITMAP * bmp = getBmpFromPixels(pix); #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1) { pix.swapRgb(); } #endif if (bmp) // bitmap successfully created { // (b) open a memory stream to compress the image onto mem_buffer: // FIMEMORY *hmem = FreeImage_OpenMemory(); // (c) encode and save the image to the memory (on dib FIBITMAP image): // if(FREE_IMAGE_FORMAT(format) == FIF_JPEG) { int quality = JPEG_QUALITYSUPERB; switch(qualityLevel) { case OF_IMAGE_QUALITY_WORST: quality = JPEG_QUALITYBAD; break; case OF_IMAGE_QUALITY_LOW: quality = JPEG_QUALITYAVERAGE; break; case OF_IMAGE_QUALITY_MEDIUM: quality = JPEG_QUALITYNORMAL; break; case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break; case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break; } FreeImage_SaveToMemory(FIF_JPEG, bmp, hmem, quality); }else{ FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)format, bmp, hmem); } /* NOTE: at this point, hmem contains the entire data in memory stored in fif format. the amount of space used by the memory is equal to file_size: long file_size = FreeImage_TellMemory(hmem); but can also be retrieved by FreeImage_AcquireMemory that retrieves both the length of the buffer, and the buffer memory address. */ #ifdef TARGET_WIN32 DWORD size_in_bytes = 0; #else uint32_t size_in_bytes = 0; #endif // Save compressed data on mem_buffer // note: FreeImage_AquireMemory allocates space for aux_mem_buffer): // unsigned char *mem_buffer = NULL; if (!FreeImage_AcquireMemory(hmem, &mem_buffer, &size_in_bytes)) cout << "Error aquiring compressed image from memory" << endl; /* Now, before closing the memory stream, copy the content of mem_buffer to an auxiliary buffer */ buffer.set((char*)mem_buffer,size_in_bytes); // Finally, close the FIBITMAP object, or we will get a memory leak: FreeImage_Unload(bmp); // Close the memory stream (otherwise we may get a memory leak). FreeImage_CloseMemory(hmem); } }
static void saveImage(ofPixels_<PixelType> & pix, string fileName, ofImageQualityType qualityLevel) { ofInitFreeImage(); if (pix.isAllocated() == false){ ofLog(OF_LOG_ERROR,"error saving image - pixels aren't allocated"); return; } #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1) { pix.swapRgb(); } #endif FIBITMAP * bmp = getBmpFromPixels(pix); #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1) { pix.swapRgb(); } #endif fileName = ofToDataPath(fileName); 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 quality = JPEG_QUALITYSUPERB; switch(qualityLevel) { case OF_IMAGE_QUALITY_WORST: quality = JPEG_QUALITYBAD; break; case OF_IMAGE_QUALITY_LOW: quality = JPEG_QUALITYAVERAGE; break; case OF_IMAGE_QUALITY_MEDIUM: quality = JPEG_QUALITYNORMAL; break; case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break; case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break; } FreeImage_Save(fif, bmp, fileName.c_str(), quality); } else { if(qualityLevel != OF_IMAGE_QUALITY_BEST) { ofLogWarning() << "ofImageCompressionType only applies to JPEG images, ignoring value"; } if (fif == FIF_GIF) { FIBITMAP* convertedBmp; if(pix.getImageType() == OF_IMAGE_COLOR_ALPHA) { // this just converts the image to grayscale so it can save something convertedBmp = FreeImage_ConvertTo8Bits(bmp); } else { // this will create a 256-color palette from the image convertedBmp = FreeImage_ColorQuantize(bmp, FIQ_NNQUANT); } FreeImage_Save(fif, convertedBmp, fileName.c_str()); if (convertedBmp != NULL){ FreeImage_Unload(convertedBmp); } } else { FreeImage_Save(fif, bmp, fileName.c_str()); } } } if (bmp != NULL){ FreeImage_Unload(bmp); } }
static void saveImage(const ofPixels_<PixelType> & _pix, const std::string& _fileName, ofImageQualityType qualityLevel) { // Make a local copy. ofPixels_<PixelType> pix = _pix; ofInitFreeImage(); if (pix.isAllocated() == false){ ofLogError("ofImage") << "saveImage(): couldn't save \"" << _fileName << "\", pixels are not allocated"; return; } #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1 && (pix.getPixelFormat()==OF_PIXELS_RGB || pix.getPixelFormat()==OF_PIXELS_RGBA)) { pix.swapRgb(); } #endif FIBITMAP * bmp = getBmpFromPixels(pix); #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1 && (pix.getPixelFormat()==OF_PIXELS_BGR || pix.getPixelFormat()==OF_PIXELS_BGRA)) { pix.swapRgb(); } #endif ofFilePath::createEnclosingDirectory(_fileName); std::string fileName = ofToDataPath(_fileName); 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 quality = JPEG_QUALITYSUPERB; switch(qualityLevel) { case OF_IMAGE_QUALITY_WORST: quality = JPEG_QUALITYBAD; break; case OF_IMAGE_QUALITY_LOW: quality = JPEG_QUALITYAVERAGE; break; case OF_IMAGE_QUALITY_MEDIUM: quality = JPEG_QUALITYNORMAL; break; case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break; case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break; } FreeImage_Save(fif, bmp, fileName.c_str(), quality); } else { if(qualityLevel != OF_IMAGE_QUALITY_BEST) { ofLogWarning("ofImage") << "saveImage(): ofImageCompressionType only applies to JPEGs," << " ignoring value for \" "<< fileName << "\""; } if (fif == FIF_GIF) { FIBITMAP* convertedBmp; if(pix.getImageType() == OF_IMAGE_COLOR_ALPHA) { // this just converts the image to grayscale so it can save something convertedBmp = FreeImage_ConvertTo8Bits(bmp); } else { // this will create a 256-color palette from the image convertedBmp = FreeImage_ColorQuantize(bmp, FIQ_NNQUANT); } FreeImage_Save(fif, convertedBmp, fileName.c_str()); if (convertedBmp != nullptr){ FreeImage_Unload(convertedBmp); } } else { FreeImage_Save(fif, bmp, fileName.c_str()); } } } if (bmp != nullptr){ FreeImage_Unload(bmp); } }
static bool saveImage(const ofPixels_<PixelType> & _pix, ofBuffer & buffer, ofImageFormat format, ofImageQualityType qualityLevel) { // thanks to alvaro casinelli for the implementation ofInitFreeImage(); if (_pix.isAllocated() == false){ ofLogError("ofImage","saveImage(): couldn't save to ofBuffer, pixels are not allocated"); return false; } if(format==OF_IMAGE_FORMAT_JPEG && (_pix.getNumChannels()==4 || _pix.getBitsPerChannel() > 8)){ ofPixels pix3 = _pix; pix3.setNumChannels(3); return saveImage(pix3,buffer,format,qualityLevel); } FIBITMAP * bmp = nullptr; #ifdef TARGET_LITTLE_ENDIAN if(sizeof(PixelType) == 1 && (_pix.getPixelFormat()==OF_PIXELS_RGB || _pix.getPixelFormat()==OF_PIXELS_RGBA)) { // Make a local copy. ofPixels_<PixelType> pix = _pix; pix.swapRgb(); bmp = getBmpFromPixels(pix); }else{ #endif bmp = getBmpFromPixels(_pix); #ifdef TARGET_LITTLE_ENDIAN } #endif if (bmp) // bitmap successfully created { bool returnValue; // (b) open a memory stream to compress the image onto mem_buffer: // FIMEMORY *hmem = FreeImage_OpenMemory(); // (c) encode and save the image to the memory (on dib FIBITMAP image): // if(FREE_IMAGE_FORMAT(format) == FIF_JPEG) { int quality = JPEG_QUALITYSUPERB; switch(qualityLevel) { case OF_IMAGE_QUALITY_WORST: quality = JPEG_QUALITYBAD; break; case OF_IMAGE_QUALITY_LOW: quality = JPEG_QUALITYAVERAGE; break; case OF_IMAGE_QUALITY_MEDIUM: quality = JPEG_QUALITYNORMAL; break; case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break; case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break; } returnValue = FreeImage_SaveToMemory(FIF_JPEG, bmp, hmem, quality); }else{ returnValue = FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)format, bmp, hmem); } /* NOTE: at this point, hmem contains the entire data in memory stored in fif format. the amount of space used by the memory is equal to file_size: long file_size = FreeImage_TellMemory(hmem); but can also be retrieved by FreeImage_AcquireMemory that retrieves both the length of the buffer, and the buffer memory address. */ #ifdef TARGET_WIN32 DWORD size_in_bytes = 0; #else std::uint32_t size_in_bytes = 0; #endif // Save compressed data on mem_buffer // note: FreeImage_AquireMemory allocates space for aux_mem_buffer): // unsigned char *mem_buffer = nullptr; if (!FreeImage_AcquireMemory(hmem, &mem_buffer, &size_in_bytes)){ ofLogError("ofImage") << "saveImage(): couldn't save to ofBuffer, aquiring compressed image from memory failed"; return false; } /* Now, before closing the memory stream, copy the content of mem_buffer to an auxiliary buffer */ buffer.set((char*)mem_buffer,size_in_bytes); // Finally, close the FIBITMAP object, or we will get a memory leak: FreeImage_Unload(bmp); // Close the memory stream (otherwise we may get a memory leak). FreeImage_CloseMemory(hmem); return returnValue; }else{ return false; } }