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 perfom 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 everthing 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 = 5000; 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; }
/// <summary> /// Process.1 Switches the bitmap to the correct frame or page, and applies source flipping commands /// </summary> /// <param name="s"></param> /// <returns></returns> protected override RequestedAction PrepareSourceBitmap(ImageState s) { if (base.PrepareSourceBitmap(s) == RequestedAction.Cancel) return RequestedAction.Cancel ; //Call extensions if (s.sourceBitmap == null) return RequestedAction.None ; //Nothing to do if there is no bitmap Bitmap src = s.sourceBitmap; ResizeSettings q = s.settings; int page = 0; if (!string.IsNullOrEmpty(q["page"]) && !int.TryParse(q["page"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out page)) page = 0; int frame = 0; if (!string.IsNullOrEmpty(q["frame"]) && !int.TryParse(q["frame"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out frame)) frame = 0; //So users can use 1-based numbers page--; frame--; //Support page selection in a .tiff document. try { //Stay on the last page/frame if out of bounds if (page > 0 && page >= src.GetFrameCount(FrameDimension.Page)) page = src.GetFrameCount(FrameDimension.Page) - 1; if (frame > 0 && frame >= src.GetFrameCount(FrameDimension.Time)) frame = src.GetFrameCount(FrameDimension.Time) - 1; //Select the right page/frame if specified if (page > 0) src.SelectActiveFrame(FrameDimension.Page, page); if (frame > 0) src.SelectActiveFrame(FrameDimension.Time, frame); s.originalSize = s.sourceBitmap.Size; } catch (ExternalException) { } //When somebody tries &frame or &page on a single-frame image //Flipping has to be done on the original - it can't be done as part of the DrawImage or later, after the borders are drawn. if (s.sourceBitmap != null && (s.settings.SourceFlip != RotateFlipType.RotateNoneFlipNone || !string.IsNullOrEmpty(s.settings["sRotate"]))) { double angle = s.settings.Get<double>("sRotate",0); s.EnsurePreRenderBitmap(); s.preRenderBitmap.RotateFlip(PolygonMath.CombineFlipAndRotate(s.settings.SourceFlip, angle)); s.originalSize = s.preRenderBitmap.Size; } return RequestedAction.None; }
protected override RequestedAction PreRenderImage(ImageState s) { if (s.sourceBitmap == null) return RequestedAction.None; if (!s.settings.WasOneSpecified("a.featheredges")) return RequestedAction.None; s.ApplyCropping(); s.EnsureRGBA(); s.EnsurePreRenderBitmap(); ApplyPreFiltersTo(ref s.preRenderBitmap, s); return RequestedAction.None; }