RectangleF GetZoomCrop() { RectangleF box = SalientBoundingBox(); var bounds = new RectangleF(new PointF(0, 0), ImageSize); //Clip box to original image bounds box = PolygonMath.ClipRectangle(box, bounds); //Crop close var copySize = PolygonMath.ScaleOutside(box.Size, TargetSize); //Clip to bounds. return(PolygonMath.ClipRectangle(PolygonMath.ExpandTo(box, copySize), bounds)); }
protected override RequestedAction LayoutImage(ImageState s) { //Only activated if both width and height are specified, and mode=crop. if (s.settings.Mode != FitMode.Crop || s.settings.Width < 0 || s.settings.Height < 0) { return(RequestedAction.None); } //Calculate bounding box for all coordinates specified. double[] focus = NameValueCollectionExtensions.GetList <double>(s.settings, "c.focus", null, 2, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72); if (focus == null) { return(RequestedAction.None); } RectangleF box = PolygonMath.GetBoundingBox(focus); var bounds = new RectangleF(new PointF(0, 0), s.originalSize); //Clip box to original image bounds box = PolygonMath.ClipRectangle(box, bounds); var targetSize = new SizeF(s.settings.Width, s.settings.Height); SizeF copySize; //Now, we can either crop as closely as possible or as loosely as possible. if (NameValueCollectionExtensions.Get <bool>(s.settings, "c.zoom", false) && box.Width > 0 && box.Height > 0) { //Crop close copySize = PolygonMath.ScaleOutside(box.Size, targetSize); } else { //Crop minimally copySize = PolygonMath.ScaleInside(targetSize, bounds.Size); //Ensure it's outside the box if (!PolygonMath.FitsInside(box.Size, copySize)) { copySize = PolygonMath.ScaleOutside(box.Size, copySize); } } //Clip to bounds. box = PolygonMath.ClipRectangle(PolygonMath.ExpandTo(box, copySize), bounds); s.copyRect = box; ///What is the vertical and horizontal aspect ratio different in result pixels? var padding = PolygonMath.ScaleInside(box.Size, targetSize); padding = new SizeF(targetSize.Width - padding.Width, targetSize.Height - padding.Height); //So, if we haven't met the aspect ratio yet, what mode will we pass on? var finalmode = NameValueCollectionExtensions.Get <FitMode>(s.settings, "c.finalmode", FitMode.Pad); //Crop off 1 or 2 pixels instead of padding without worrying too much if (finalmode == FitMode.Pad && padding.Width + padding.Height < 3) { finalmode = FitMode.Crop; } s.settings.Mode = finalmode; return(RequestedAction.None); }
protected override RequestedAction PreRenderImage(ImageState s) { //Skip this when we are doing simulations if (s.destGraphics == null) { return(RequestedAction.None); } string sf = s.settings["fi.scale"]; if (string.IsNullOrEmpty(sf)) { return(RequestedAction.None); } bool validAlg = false; FREE_IMAGE_FILTER filter = ParseResizeAlgorithm(sf, FREE_IMAGE_FILTER.FILTER_CATMULLROM, out validAlg); if (!validAlg) { throw new ImageProcessingException("The specified resizing filter '" + sf + "' did not match bicubic, bilinear, box, bspline, catmullrom, or lanczos."); } //Set copy attributes s.copyAttibutes.SetWrapMode(WrapMode.TileFlipXY); //The minimum dimensions of the temporary bitmap. SizeF targetSize = PolygonMath.getParallelogramSize(s.layout["image"]); targetSize = new SizeF((float)Math.Ceiling(targetSize.Width), (float)Math.Ceiling(targetSize.Height)); s.ApplyCropping(); s.EnsurePreRenderBitmap(); //The size of the temporary bitmap. //We want it larger than the size we'll use on the final copy, so we never upscale it //- but we also want it as small as possible so processing is fast. SizeF tempSize = PolygonMath.ScaleOutside(targetSize, s.copyRect.Size); int tempWidth = (int)Math.Ceiling(tempSize.Width); int tempHeight = (int)Math.Ceiling(tempSize.Height); FIBITMAP src = FIBITMAP.Zero; FIBITMAP midway = FIBITMAP.Zero; try { var oldbit = s.preRenderBitmap ?? s.sourceBitmap; //Crop if needed, Convert, scale, then convert back. src = FreeImage.CreateFromBitmap(oldbit); midway = FreeImage.Rescale(src, tempWidth, tempHeight, filter); FreeImage.UnloadEx(ref src); //Clear the old pre-rendered image if needed if (s.preRenderBitmap != null) { s.preRenderBitmap.Dispose(); } //Reassign the pre-rendered image s.preRenderBitmap = FreeImage.GetBitmap(midway); s.copyRect = new RectangleF(0, 0, s.preRenderBitmap.Width, s.preRenderBitmap.Height); FreeImage.UnloadEx(ref midway); s.preRenderBitmap.MakeTransparent(); } finally { if (!src.IsNull) { FreeImage.UnloadEx(ref src); } if (!midway.IsNull) { FreeImage.UnloadEx(ref midway); } } return(RequestedAction.Cancel); }
protected override RequestedAction PreRenderImage(ImageState s) { //Skip this when we are doing simulations if (s.destGraphics == null) { return(RequestedAction.None); } s.ApplyCropping(); s.EnsurePreRenderBitmap(); //Parse carve algorithm kind FilterType ftype = s.settings.Get <FilterType>("carve", FilterType.None); if ("true".Equals(s.settings["carve"], StringComparison.OrdinalIgnoreCase)) { ftype = FilterType.Prewitt; } if (string.IsNullOrEmpty(s.settings["carve"]) && s.settings.Mode == FitMode.Carve) { ftype = FilterType.Prewitt; } //If we have carve data CarveDataPlotter carveData = s.Data.ContainsKey(CarveData) ? (s.Data[CarveData] as CarveDataPlotter) : null; if (carveData != null && ftype == FilterType.None) { ftype = FilterType.Prewitt; } RectangleF copyRect = s.copyRect; if (carveData != null) { copyRect = new RectangleF(new PointF(0, 0), s.sourceBitmap.Size); } if (ftype == FilterType.None) { return(RequestedAction.None); //Only override rendering when carving is requested. } //The minimum dimensions of the temporary bitmap. SizeF targetSize = PolygonMath.getParallelogramSize(s.layout["image"]); targetSize = new SizeF((float)Math.Ceiling(targetSize.Width), (float)Math.Ceiling(targetSize.Height)); //The size of the temporary bitmap. //We want it larger than the size we'll use on the final copy, so we never upscale it //- but we also want it as small as possible so processing is fast. SizeF tempSize = PolygonMath.ScaleOutside(targetSize, copyRect.Size); int tempWidth = (int)Math.Ceiling(tempSize.Width); int tempHeight = (int)Math.Ceiling(tempSize.Height); //The intermediate and seam carved files string tempFile = Path.GetTempFileName(); string outputTempFile = Path.GetTempFileName(); try { try { //Create a temporary bitmap that is 'halfway resized', so we can efficiently perform seam carving. //Unless it's already been done for us by FreeImageResize or something if (s.preRenderBitmap != null && (tempWidth - s.preRenderBitmap.Width < 50 && tempHeight - s.preRenderBitmap.Height < 50)) { s.preRenderBitmap.Save(tempFile, ImageFormat.Bmp); tempWidth = s.preRenderBitmap.Width; tempHeight = s.preRenderBitmap.Height; } else { //Create the temporary bitmap and graphics. using (Bitmap temp = new Bitmap(tempWidth, tempHeight, PixelFormat.Format32bppArgb)) using (Graphics g = Graphics.FromImage(temp)) using (ImageAttributes ia = new ImageAttributes()) { //High quality everything g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.CompositingMode = CompositingMode.SourceOver; ia.SetWrapMode(WrapMode.TileFlipXY); if (s.preRenderBitmap != null) { g.DrawImage(s.preRenderBitmap, new Rectangle(0, 0, tempWidth, tempHeight), 0, 0, s.preRenderBitmap.Width, s.preRenderBitmap.Height, GraphicsUnit.Pixel, ia); } else { g.DrawImage(s.sourceBitmap, new Rectangle(0, 0, tempWidth, tempHeight), copyRect.X, copyRect.Y, copyRect.Width, copyRect.Height, GraphicsUnit.Pixel, ia); } g.Flush(FlushIntention.Flush); //Save temp.Save(tempFile, ImageFormat.Bmp); } } string maskFile = carveData != null?Path.GetTempFileName() : null; try { if (carveData != null) { carveData.SaveBitmapAs(maskFile, tempWidth, tempHeight); } Size intTargetSize = new Size((int)targetSize.Width, (int)targetSize.Height); CairJob job = new CairJob(); if (maskFile != null) { job.WeightPath = maskFile; } job.SourcePath = tempFile; job.DestPath = outputTempFile; job.Size = intTargetSize; job.Filter = ftype; job.Timeout = Timeout; cair.CairyIt(job); } finally { if (maskFile != null) { File.Delete(maskFile); } } } finally { File.Delete(tempFile); } //Dispose old intermediate bitmap first if (s.preRenderBitmap != null) { s.preRenderBitmap.Dispose(); } //Load the new intermediate file from disk s.preRenderBitmap = new Bitmap(outputTempFile); s.preRenderBitmap.MakeTransparent(); //Reset the s.copyRect to match the new bitmap s.copyRect = new RectangleF(new PointF(0, 0), new SizeF(targetSize.Width, targetSize.Height)); } finally { File.Delete(outputTempFile); } return(RequestedAction.Cancel); }