Open Frameworks has always been able to save JPGs, but as its stands there is no control over JPG compression settings, which are by default set pretty low. This codes adds a second save method, saveJpg(string fileName, int quality) which allows you to explicitly set the compression of the image. For me the main reason for doing this is that full screen PNG files can be quite large and its easy enough to end up with 20gigs of png files if you have an app that captures every x seconds. With this I can save out very high quality JPG images with a minimum of filesize.
I have been using this for myself for a year or so now, but would love to see it get added to the OF core.
in ofImage.h:
// file loading / saving
bool loadImage(string fileName);
void saveImage(string fileName);
void saveJpg(string fileName, int quality);
bool loadImageIntoPixels(string fileName, ofPixels &pix);
void saveImageFromPixels(string fileName, ofPixels &pix);
void saveImageFromPixelsJPG(string fileName, ofPixels &pix, int quality);
if ofImage.cpp:
void ofImage::saveJpg(string fileName, int quality){
saveImageFromPixelsJPG( fileName, myPixels, quality);
}
//---------------------------------------------------------------------------------------
void ofImage::saveImageFromPixelsJPG(string fileName, ofPixels &pix, int quality){
if(quality > 100) quality = 100;
else if(quality <= 0)quality = 1;
if (pix.bAllocated == false){
printf("error saving image - pixels aren't allocated \n");
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());
}
//FIMD_COMMENTS;
if((fif == FIF_JPEG) && FreeImage_FIFSupportsReading(fif)) {
FreeImage_Save(fif, bmp, fileName.c_str(), quality);
}
else{
printf("saveJpg - requires you to save with .jpg extension - image not saved\n");
return;
}
}
if (bmp != NULL){
FreeImage_Unload(bmp);
}
}