public static bool ShouldChangeFormatToJpeg(Image image)
{
try
{
using(var safetyImage = new Bitmap(image))
//nb: there are cases (notably http://jira.palaso.org/issues/browse/WS-34711, after cropping a jpeg) where we get out of memory if we are not operating on a copy
{
using(var jpegFile = new TempFile())
using(var pngFile = new TempFile())
{
SIL.IO.RobustIO.SaveImage(image, pngFile.Path, ImageFormat.Png);
SaveAsTopQualityJpeg(safetyImage, jpegFile.Path);
var jpegInfo = new FileInfo(jpegFile.Path);
var pngInfo = new FileInfo(pngFile.Path);
// this is just our heuristic.
const double fractionOfTheOriginalThatWouldWarrantChangingToJpeg = .5;
return jpegInfo.Length < (pngInfo.Length*(1.0 - fractionOfTheOriginalThatWouldWarrantChangingToJpeg));
}
}
}
catch(OutOfMemoryException e)
{
NonFatalProblem.Report(ModalIf.Alpha, PassiveIf.All,"Could not attempt conversion to jpeg.", "ref BL-3387", exception: e);
return false;
}
}