Hi Hervé,
here is another new function I'd like you to add to FreeImage. Function FreeImage_RescaleEx provides support for rescaling only a rectangular area of an image. Basically, that's a much faster solution than coding:
FIBITMAP dibTmp = FreeImage_Copy(dib, 10, 10, 140, 140);
FIBITMAP dibDst = FreeImage_Rescale(dibTmp, 260, 260);
FreeImage_Unload(dibTmp);
Much faster and easier to write is:
FIBITMAP dibDst = FreeImage_RescaleEx(dib, 260, 260, 10, 10, 140, 140);
Of course, the new function does not rely on FreeImage_Copy but reads and rescales the bits inside the specified rectangle only. This capability is available in CResizeEngine::scale as of version 3.15.4. I've added this along with the performance improving refactoring of FreeImage_Rescale.
Additionally, function FreeImage_RescaleEx takes a flags parameter. Currently, there are two flags/options defined and implemented:
#define FI_RESCALE_TRUE_COLOR 0x01 // do not return a palletized image for a greyscale source image
#define FI_RESCALE_OMIT_METADATA 0x02 // do not copy metadata to the rescaled image
We could argue about whether the second, self-explanatory option is really useful (the performance gain is not significant), but in general, I do prefer these flags over a simple boolean (like BOOL bAlwaysReturnTrueColorImage for the first option) due to their expandability.
The first option needs some explanation. After having refactored FreeImage_Rescale/CResizeEngine::scale, the source image src is converted to 24- or 32-bits (the latter, if src is transparent) in-place, that is, during the filtering in CResizeEngine::scale. For additional time and memory saving, this method, however, returns an 8-bit greyscale image, if src is <= 8 bpp and consists of grey colors only (that is, r == g == b is true for the whole palette).
This actually makes the color type of the image returned by FreeImage_Rescale depending on the source image's pixels, which may be a problem for some use cases, since the caller can't control the content of the image. By using flag FI_RESCALE_TRUE_COLOR, now one can ensure to get a 24- or a 32-bit image depending on the image's transparency only, which the caller can control by calling FreeImage_SetTransparent.
You'll find all updated files in the attached Zip archive.
Carsten
Anonymous
Hi Cartsen,
Thanks for the patch, it is now available in the CVS.
I've made little modifications / corrections to this patch :
CResizeEngine::horizontalFilter
case 16
if (IS_FORMAT_RGB565(src))
=> missing "dst_bits += 3;"
=> small simplifications in the code (removed redundant code)
FreeImage_RescaleEx
if ((flags & FI_RESCALE_OMIT_METADATA) == FI_RESCALE_OMIT_METADATA)
=> should be the inverse
=> input parameters set to integers to stay compatible with Rescale / Copy / Paste
=> in fact everything should be unsigned ...
replaced CHECK_RANGE by a test similar to the one in FreeImage_Copy
renamed FreeImage_RescaleEx by FreeImage_RescaleRect
A preliminary documentation for the function is available at http://freeimage.sourceforge.net/doc/FreeImage3170.pdf
As for future improvments:
1) We could first convert 1- or 4-bit bitmaps to the dst bitdepth, then perform the rescaling
=> more memory needed but simpler code and probably faster processing
2) We could use FreeImage_CreateView to rescale a RECT
=> only useful if point 1) is applied
Hervé
Hi Hervé,
sorry, for contributing so many bugs with that patch... => the missing "dst_bits += 3;" and the wrong/inverse FI_RESCALE_OMIT_METADATA check are really beginner's bugs. Thanks for reviewing the patch so closely.
Using FreeImage_CreateView could have been an option for FreeImage_RescaleRect. However, I have added support for rescaling only part of an image long before FreeImage_CreateView and FreeImage_ConvertFromRawBitsEx was available. We were just missing a public/exported function that makes use of that support (which BTW is quite simple and does not need much code).
Maybe I will do some tests in the future to see whether converting 1- and 4-bit images to 8-bit prior to rescaling (first phase) performs much better than the current solution. This would actually save a lot of code lines (but also requires more memory).
Carsten
fixed in release 3.17.0