Skip to content

Instantly share code, notes, and snippets.

@lilith
Created September 21, 2011 16:57
Show Gist options
  • Save lilith/1232647 to your computer and use it in GitHub Desktop.
Save lilith/1232647 to your computer and use it in GitHub Desktop.
Test of FreeImage performance
protected virtual bool BuildUnmanaged(object source, object dest, ResizeSettings settings) {
if (!FreeImageAPI.FreeImage.IsAvailable()) return false;
if (!"true".Equals(settings["freeimage"])) return false;
// Load the example bitmap.
FIBITMAP original = FIBITMAP.Zero;
FIBITMAP final = FIBITMAP.Zero;
if (source is String)
original = FreeImage.LoadEx((String)source); //Supports all kinds of input formats.
else if (source is Stream)
original = FreeImage.LoadFromStream((Stream)source);
if (original.IsNull) return false;
try{
//What is our destination format
IEncoder managedEncoder = EncoderProvider.GetEncoder(settings, source); //Use the existing pipeline to parse the querystring
FREE_IMAGE_FORMAT destFormat = FreeImage.GetFIFFromMime(managedEncoder.MimeType); //Use the resulting mime-type to determine the output format.
//This prevents us from supporting output formats that don't already have registered encoders. Good, right?
//Find the image size
Size orig = new Size( (int)FreeImage.GetWidth(original), (int)FreeImage.GetHeight(original));
//Calculate the new size of the image and the canvas.
ImageState state = new ImageState(settings, orig, true);
Layout(state);
bool supportsTransparency = true;
RectangleF imageDest = PolygonMath.GetBoundingBox(state.layout["image"]);
//Rescale
final = FreeImage.Rescale(original,(int)imageDest.Width,(int)imageDest.Height,FREE_IMAGE_FILTER.FILTER_BILINEAR);
FreeImage.UnloadEx(ref original);
if (final.IsNull) return false;
RGBQUAD bgcolor = default(RGBQUAD);
bgcolor.Color = settings.BackgroundColor;
if (settings.BackgroundColor == Color.Transparent && !supportsTransparency)
bgcolor.Color = Color.White;
//If we need to leave padding, do so.
BoxPadding outsideImage = new BoxPadding(imageDest.Left, imageDest.Top, state.destSize.Width - imageDest.Right, state.destSize.Height - imageDest.Bottom);
if (outsideImage.All != 0) {
original = final;
//Extend canvas
final = FreeImage.EnlargeCanvas<RGBQUAD>(original,
(int)outsideImage.Left, (int)outsideImage.Top, (int)outsideImage.Right, (int)outsideImage.Bottom,
bgcolor.Color != Color.Transparent ? new Nullable<RGBQUAD>(bgcolor) : null,
FREE_IMAGE_COLOR_OPTIONS.FICO_RGBA);
FreeImage.UnloadEx(ref original);
if (final.IsNull) return false;
}
// Try to save the bitmap
if (dest is string){
if (!FreeImage.Save(destFormat, final, (string)dest, FREE_IMAGE_SAVE_FLAGS.DEFAULT)) return false;
}else if (dest is Stream){
if (!FreeImage.SaveToStream(final,(Stream)dest,destFormat)) return false;
} else if (dest is BitmapHolder){
((BitmapHolder)dest).bitmap = FreeImage.GetBitmap(final);
}
}finally{
if (!original.IsNull) FreeImage.UnloadEx(ref original);
if (!original.IsNull) FreeImage.UnloadEx(ref final);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment