private RectangleF determineManualCropWindow(ResizeSettings settings) { RectangleF cropWindow = copyRect; if (cropWindow.IsEmpty) { //Use the crop size if present. cropWindow = new RectangleF(new PointF(0, 0), originalSize); if (settings.GetList <double>("crop", 0, 4) != null) { cropWindow = PolygonMath.ToRectangle(settings.getCustomCropSourceRect(originalSize)); //Round the custom crop rectangle coordinates //Ensure right/bottom bounded after rounding completes cropWindow.Width = Math.Min(originalSize.Width - cropWindow.Left, cropWindow.Width); cropWindow.Height = Math.Min(originalSize.Height - cropWindow.Top, cropWindow.Height); if (cropWindow.Size.IsEmpty) { throw new Exception("You must specify a custom crop rectangle if crop=custom"); } } } return(cropWindow); }
public void ApplySettings(ResizeSettings settings) { copyRect = determineManualCropWindow(settings); //Save the manual crop size. SizeF manualCropSize = copyRect.Size; RectangleF manualCropRect = copyRect; FitMode fit = determineFitMode(settings); //Aspect ratio of the image double imageRatio = copyRect.Width / copyRect.Height; //Zoom factor double zoom = settings.Get <double>("zoom", 1); //The target size for the image targetSize = new SizeF(-1, -1); //Target area for the image areaSize = new SizeF(-1, -1); //If any dimensions are specified, calculate. Otherwise, use original image dimensions if (settings.Width != -1 || settings.Height != -1 || settings.MaxHeight != -1 || settings.MaxWidth != -1) { //A dimension was specified. //We first calculate the largest size the image can be under the width/height/maxwidth/maxheight restriction //- pretending stretch=fill and scale=both //Temp vars - results stored in targetSize and areaSize double width = settings.Width; double height = settings.Height; double maxwidth = settings.MaxWidth; double maxheight = settings.MaxHeight; //Eliminate cases where both a value and a max value are specified: use the smaller value for the width/height if (maxwidth > 0 && width > 0) { width = Math.Min(maxwidth, width); maxwidth = -1; } if (maxheight > 0 && height > 0) { height = Math.Min(maxheight, height); maxheight = -1; } //Handle cases of width/maxheight and height/maxwidth as in legacy version if (width != -1 && maxheight != -1) { maxheight = Math.Min(maxheight, (width / imageRatio)); } if (height != -1 && maxwidth != -1) { maxwidth = Math.Min(maxwidth, (height * imageRatio)); } //Move max values to width/height. FitMode should already reflect the mode we are using, and we've already resolved mixed modes above. width = Math.Max(width, maxwidth); height = Math.Max(height, maxheight); //Calculate missing value (a missing value is handled the same everywhere). if (width > 0 && height <= 0) { height = width / imageRatio; } else if (height > 0 && width <= 0) { width = height * imageRatio; } //We now have width & height, our target size. It will only be a different aspect ratio from the image if both 'width' and 'height' are specified. //FitMode.Max if (fit == FitMode.Max) { areaSize = targetSize = PolygonMath.ScaleInside(manualCropSize, new SizeF((float)width, (float)height)); //FitMode.Pad } else if (fit == FitMode.Pad) { areaSize = new SizeF((float)width, (float)height); targetSize = PolygonMath.ScaleInside(manualCropSize, areaSize); //FitMode.crop } else if (fit == FitMode.Crop) { //We autocrop - so both target and area match the requested size areaSize = targetSize = new SizeF((float)width, (float)height); RectangleF copyRect; ScaleMode scale = settings.Scale; bool cropWidthSmaller = manualCropSize.Width <= (float)width; bool cropHeightSmaller = manualCropSize.Height <= (float)height; //TODO: consider mode=crop;fit=upscale // With both DownscaleOnly (where only one dimension is smaller than // requested) and UpscaleCanvas, we will have a targetSize based on the // minWidth & minHeight. // TODO: what happens if mode=crop;scale=down but the target is larger than the source? if ((scale == ScaleMode.DownscaleOnly && (cropWidthSmaller != cropHeightSmaller)) || (scale == ScaleMode.UpscaleCanvas && (cropHeightSmaller || cropWidthSmaller))) { var minWidth = Math.Min(manualCropSize.Width, (float)width); var minHeight = Math.Min(manualCropSize.Height, (float)height); targetSize = new SizeF(minWidth, minHeight); copyRect = manualCropRect = new RectangleF(0, 0, minWidth, minHeight); // For DownscaleOnly, the areaSize is adjusted to the new targetSize as well. if (scale == ScaleMode.DownscaleOnly) { areaSize = targetSize; } } else { //Determine the size of the area we are copying Size sourceSize = PolygonMath.RoundPoints(PolygonMath.ScaleInside(areaSize, manualCropSize)); //Center the portion we are copying within the manualCropSize copyRect = new RectangleF(0, 0, sourceSize.Width, sourceSize.Height); } // Align the actual source-copy rectangle inside the available // space based on the anchor. this.copyRect = PolygonMath.ToRectangle(PolygonMath.AlignWith(copyRect, this.copyRect, settings.Anchor)); } else { //Stretch and carve both act like stretching, so do that: areaSize = targetSize = new SizeF((float)width, (float)height); } } else { //No dimensions specified, no fit mode needed. Use manual crop dimensions areaSize = targetSize = manualCropSize; } //Multiply both areaSize and targetSize by zoom. areaSize.Width *= (float)zoom; areaSize.Height *= (float)zoom; targetSize.Width *= (float)zoom; targetSize.Height *= (float)zoom; //Todo: automatic crop is permitted to break the scaling rule Fix!! //Now do upscale/downscale check If they take effect, set targetSize to imageSize if (settings.Scale == ScaleMode.DownscaleOnly) { if (PolygonMath.FitsInside(manualCropSize, targetSize)) { //The image is smaller or equal to its target polygon. Use original image coordinates instead. areaSize = targetSize = manualCropSize; copyRect = manualCropRect; } } else if (settings.Scale == ScaleMode.UpscaleOnly) { if (!PolygonMath.FitsInside(manualCropSize, targetSize)) { //The image is larger than its target. Use original image coordinates instead areaSize = targetSize = manualCropSize; copyRect = manualCropRect; } } else if (settings.Scale == ScaleMode.UpscaleCanvas) { //Same as downscaleonly, except areaSize isn't changed. if (PolygonMath.FitsInside(manualCropSize, targetSize)) { //The image is smaller or equal to its target polygon. //Use manual copy rect/size instead. targetSize = manualCropSize; copyRect = manualCropRect; } } //May 12: require max dimension and round values to minimize rounding differences later. areaSize.Width = Math.Max(1, (float)Math.Round(areaSize.Width)); areaSize.Height = Math.Max(1, (float)Math.Round(areaSize.Height)); targetSize.Width = Math.Max(1, (float)Math.Round(targetSize.Width)); targetSize.Height = Math.Max(1, (float)Math.Round(targetSize.Height)); }
protected override RequestedAction RenderImage(ImageState s) { //Skip this when we are doing simulations if (s.destGraphics == null) { return(RequestedAction.None); } //If there's pre-rendering involved this optimization is utterly pointless. if (s.preRenderBitmap != null) { return(RequestedAction.None); } //Find out what the speed setting is. int speed = 0; if (string.IsNullOrEmpty(s.settings["speed"]) || !int.TryParse(s.settings["speed"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out speed)) { speed = 0; } if (speed < 1) { return(RequestedAction.None); } s.destGraphics.CompositingMode = CompositingMode.SourceCopy; s.destGraphics.CompositingQuality = CompositingQuality.HighSpeed; if (speed == 1) { s.destGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear; } else { s.destGraphics.InterpolationMode = InterpolationMode.Bilinear; } s.destGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed; s.destGraphics.SmoothingMode = SmoothingMode.HighSpeed; s.copyAttibutes.SetWrapMode(WrapMode.TileFlipXY); if (speed < 3) { s.destGraphics.DrawImage(s.sourceBitmap, PolygonMath.getParallelogram(s.layout["image"]), s.copyRect, GraphicsUnit.Pixel, s.copyAttibutes); } else if (speed < 4) { Rectangle midsize = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(s.layout["image"])); using (Image thumb = s.sourceBitmap.GetThumbnailImage(midsize.Width, midsize.Height, delegate() { return(false); }, IntPtr.Zero)) { double xfactor = (double)thumb.Width / (double)s.sourceBitmap.Width; double yfactor = (double)thumb.Height / (double)s.sourceBitmap.Height; RectangleF copyPart = new RectangleF((float)(s.copyRect.Left * xfactor), (float)(s.copyRect.Top * yfactor), (float)(s.copyRect.Width * xfactor), (float)(s.copyRect.Height * yfactor)); if (Math.Floor(copyPart.Height) == thumb.Height || Math.Ceiling(copyPart.Height) == thumb.Height) { copyPart.Height = thumb.Height; } if (Math.Floor(copyPart.Width) == thumb.Width || Math.Ceiling(copyPart.Width) == thumb.Width) { copyPart.Width = thumb.Width; } s.destGraphics.DrawImage(thumb, PolygonMath.getParallelogram(s.layout["image"]), copyPart, GraphicsUnit.Pixel, s.copyAttibutes); } } else { RectangleF box = PolygonMath.GetBoundingBox(PolygonMath.getParallelogram(s.layout["image"])); s.destGraphics.CompositingMode = CompositingMode.SourceCopy; s.destGraphics.DrawImage(s.sourceBitmap, box.Left, box.Top, box.Width, box.Height); } return(RequestedAction.Cancel); }
public static WpfImageSettings WpfDestinationImageSettings(this ImageState imageState, ResizeSettings settings) { WpfImageSettings wpfImageSettings = new WpfImageSettings(); Rectangle imageDest = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(imageState.layout["image"])); /* test - provo a ricavare i dati di resize che mi servono direttamente dagli oggetti Drawing sottostanti */ SizeF imageAreaSizes = PolygonMath.getParallelogramSize(imageState.layout["image"]); wpfImageSettings.DestinationImageWidth = imageAreaSizes.Width; wpfImageSettings.DestinationImageHeight = imageAreaSizes.Height; // Correct the settings.Mode according to the documentation if (settings.Mode == FitMode.None && (settings.Width != -1 && settings.Height != -1)) { settings.Mode = FitMode.Pad; } else if (settings.Mode == FitMode.None && (settings.MaxWidth != -1 && settings.MaxHeight != -1)) { settings.Mode = FitMode.Max; } #region -- Manage the image dimensions -- //double widthToApply = (settings.Width == -1 ? (double)settings.MaxWidth : (double)settings.Width); //double heightToApply = (settings.Height == -1 ? (double)settings.MaxHeight : (double)settings.Height); //var proportionWidth = (double)imageState.originalSize.Width / widthToApply; //var proportionHeight = (double)imageState.originalSize.Height / heightToApply; switch (settings.Mode) { case FitMode.None: break; case FitMode.Carve: // TODO //case FitMode.Pad: case FitMode.Max: //if (proportionWidth > proportionHeight) //{ // wpfImageSettings.DestinationImageHeight = Convert.ToInt32(imageState.originalSize.Height / proportionWidth); // wpfImageSettings.DestinationImageWidth = Convert.ToInt32(widthToApply); //} //else //{ // wpfImageSettings.DestinationImageWidth = Convert.ToInt32(imageState.originalSize.Width / proportionHeight); // wpfImageSettings.DestinationImageHeight = Convert.ToInt32(heightToApply); //} /* * * * * TODO: * * verificare la necessità di calcolare gli offset per il PAD * * * * */ break; case FitMode.Crop: case FitMode.Pad: //int scaleWidth, scaleHeight; //scaleWidth = scaleHeight = 0; //wpfImageSettings.DestinationImageCanvasWidth = scaleWidth = imageDest.Width; //wpfImageSettings.DestinationImageCanvasHeight = scaleHeight = imageDest.Height; //// If only a dimension is missing make it square //if (wpfImageSettings.DestinationImageCanvasWidth == 0 || wpfImageSettings.DestinationImageCanvasHeight == 0) //{ // wpfImageSettings.DestinationImageCanvasWidth = wpfImageSettings.DestinationImageCanvasHeight = Math.Max(wpfImageSettings.DestinationImageCanvasWidth, wpfImageSettings.DestinationImageCanvasHeight); //} //double originalProportions = (double)imageState.originalSize.Width / (double)imageState.originalSize.Height; //double viewportProportions = (double)wpfImageSettings.DestinationImageCanvasWidth / (double)wpfImageSettings.DestinationImageCanvasHeight; //// Calculates the new scale proportions to make touche-from-inside crop //if ((originalProportions > 1 && viewportProportions <= 1) || (originalProportions < 1 && viewportProportions > 1)) //{ // scaleHeight = Math.Max(wpfImageSettings.DestinationImageCanvasHeight, wpfImageSettings.DestinationImageCanvasWidth); // scaleWidth = Convert.ToInt32(((float)(scaleHeight) / (float)(imageState.originalSize.Height)) * imageState.originalSize.Width); //} //else //{ // scaleWidth = Math.Max(wpfImageSettings.DestinationImageCanvasHeight, wpfImageSettings.DestinationImageCanvasWidth); // scaleHeight = Convert.ToInt32(((float)(scaleWidth) / (float)(imageState.originalSize.Width)) * imageState.originalSize.Height); //} //wpfImageSettings.DestinationImageWidth = scaleWidth; //wpfImageSettings.DestinationImageHeight = scaleHeight; //if ((imageState.copyRect.Y == 0) && (imageState.copyRect.X != 0)) if ((imageState.originalSize.Width / imageState.originalSize.Height) >= (imageDest.Width / imageDest.Height)) { wpfImageSettings.DestinationImageWidth = (imageState.originalSize.Width * imageDest.Height) / imageState.copyRect.Height; if (settings.Mode == FitMode.Pad) { wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height; } else { wpfImageSettings.DestinationImageHeight = imageDest.Height; } wpfImageSettings.OffsetX = -(wpfImageSettings.DestinationImageWidth * imageState.copyRect.X) / imageState.originalSize.Width; wpfImageSettings.OffsetY = 0; } else // if ((imageState.copyRect.X == 0) && (imageState.copyRect.Y != 0)) { if (settings.Mode == FitMode.Pad) { wpfImageSettings.DestinationImageWidth = imageState.originalSize.Width; } else { wpfImageSettings.DestinationImageWidth = imageDest.Width; } wpfImageSettings.DestinationImageHeight = (imageState.originalSize.Height * imageDest.Width) / imageState.copyRect.Width; wpfImageSettings.OffsetX = 0; wpfImageSettings.OffsetY = -(wpfImageSettings.DestinationImageHeight * imageState.copyRect.Y) / imageState.originalSize.Height; } /*else * { * * }*/ break; //case FitMode.Pad: // wpfImageSettings.DestinationImageHeight = Convert.ToInt32(imageState.layout["image"][3].Y - imageState.layout["image"][0].Y); // wpfImageSettings.DestinationImageWidth = Convert.ToInt32(imageState.layout["image"][1].X - imageState.layout["image"][3].X); // break; case FitMode.Stretch: //wpfImageSettings.DestinationImageWidth = Convert.ToInt32(widthToApply); //wpfImageSettings.DestinationImageHeight = Convert.ToInt32(heightToApply); break; default: wpfImageSettings.DestinationImageWidth = imageState.originalSize.Width; wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height; break; } #endregion #region -- Manage the allignments -- switch (settings.Mode) { case FitMode.None: case FitMode.Crop: case FitMode.Pad: RectangleF croppedSize = settings.getCustomCropSourceRect(imageState.originalSize); if ((croppedSize.X != 0) || (croppedSize.Y != 0)) { wpfImageSettings.OffsetX = -Convert.ToInt32(croppedSize.X); wpfImageSettings.OffsetY = -Convert.ToInt32(croppedSize.Y); wpfImageSettings.DestinationImageCanvasWidth = croppedSize.Right - croppedSize.Left; wpfImageSettings.DestinationImageCanvasHeight = croppedSize.Bottom - croppedSize.Top; } else { wpfImageSettings.OffsetX = imageState.layout["image"][0].X; wpfImageSettings.OffsetY = imageState.layout["image"][0].Y; } //wpfImageSettings.DestinationImageCanvasWidth = imageDest.Width; //wpfImageSettings.DestinationImageCanvasHeight = imageDest.Height; //// In crop or pad I've to calculate the Offsets //switch (settings.Anchor) //{ // case ContentAlignment.BottomCenter: // wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2); // wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight; // break; // case ContentAlignment.BottomLeft: // wpfImageSettings.OffsetX = 0; // wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight; // break; // case ContentAlignment.BottomRight: // wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth; // wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight; // break; // case ContentAlignment.MiddleCenter: // wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2); // wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2); // break; // case ContentAlignment.MiddleLeft: // wpfImageSettings.OffsetX = 0; // wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2); // break; // case ContentAlignment.MiddleRight: // wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth; // wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2); // break; // case ContentAlignment.TopCenter: // wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2); // wpfImageSettings.OffsetY = 0; // break; // case ContentAlignment.TopLeft: // wpfImageSettings.OffsetX = 0; // wpfImageSettings.OffsetY = 0; // break; // case ContentAlignment.TopRight: // wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth; // wpfImageSettings.OffsetY = 0; // break; // default: // break; //} break; //case FitMode.Crop: // break; default: /* * * * TODO: risistemare!!! * * */ // Supposing I'm on manual cropping, I'll use the underlying calculations //wpfImageSettings.DestinationImageWidth = imageState.originalSize.Width; //wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height; //RectangleF croppedSize = settings.getCustomCropSourceRect(imageState.originalSize); //wpfImageSettings.OffsetX = -Convert.ToInt32(croppedSize.X); //wpfImageSettings.OffsetY = -Convert.ToInt32(croppedSize.Y); //wpfImageSettings.DestinationImageCanvasWidth = croppedSize.Right - croppedSize.Left; //wpfImageSettings.DestinationImageCanvasHeight = croppedSize.Bottom - croppedSize.Top; break; } #endregion if ((settings.Rotate % 360) != 0) { wpfImageSettings.OffsetX = (imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2; wpfImageSettings.OffsetY = (imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2; } return(wpfImageSettings); }
public override void RenderTo(Resizing.ImageState s) { if (string.IsNullOrEmpty(Path)) { return; } s.destGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; s.destGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; s.destGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; Bitmap img = null; if (this.ShouldLoadAsOriginalSize()) { img = GetMemCachedBitmap(Path, ImageQuery); } //Calculate the location for the bitmap RectangleF imgBounds = this.CalculateLayerCoordinates(s, delegate(double maxwidth, double maxheight) { ResizeSettings opts = new ResizeSettings(ImageQuery); if (Fill && string.IsNullOrEmpty(opts["scale"])) { opts.Scale = ScaleMode.Both; } if (!double.IsNaN(maxwidth)) { opts.MaxWidth = (int)Math.Floor(maxwidth); } if (!double.IsNaN(maxheight)) { opts.MaxHeight = (int)Math.Floor(maxheight); } if (img == null) { img = GetMemCachedBitmap(Path, opts); //Delayed creation allows the maxwidth/maxheight to be used in gradient plugin } lock (img) { return(ImageBuilder.Current.GetFinalSize(img.Size, opts)); } }, true); lock (img) { //Only one reader from the cached bitmap at a time. //Skip rendering unless we have room to work with. if (imgBounds.Width < 2 || imgBounds.Height < 2) { return; } if (ImageQuery.Keys.Count > 0 || Fill) { ResizeSettings settings = new ResizeSettings(ImageQuery); if (Fill && string.IsNullOrEmpty(settings["scale"])) { settings.Scale = ScaleMode.Both; } settings.MaxWidth = (int)Math.Floor(imgBounds.Width); settings.MaxHeight = (int)Math.Floor(imgBounds.Height); using (Bitmap final = ImageBuilder.Current.Build(img, settings, false)) { s.destGraphics.DrawImage(final, PolygonMath.ToRectangle(PolygonMath.CenterInside(PolygonMath.DownScaleInside(final.Size, imgBounds.Size), imgBounds))); } } else { s.destGraphics.DrawImage(img, PolygonMath.ToRectangle(PolygonMath.CenterInside(PolygonMath.DownScaleInside(img.Size, imgBounds.Size), imgBounds))); } } }
protected override RequestedAction BuildJob(ImageJob job) { if (!"wpf".Equals(job.Settings["builder"])) { return(RequestedAction.None); } // Estrazione delle ResizeSettings ResizeSettings settings = job.Settings; Stream s = null; bool disposeStream = !(job.Source is Stream); long originalPosition = 0; bool restoreStreamPosition = false; string path; s = c.CurrentImageBuilder.GetStreamFromSource(job.Source, job.Settings, ref disposeStream, out path, out restoreStreamPosition); if (s == null) { return(RequestedAction.None); //We don't support the source object! } if (job.ResetSourceStream) { restoreStreamPosition = true; } job.SourcePathData = path; // Instanzio uno stream locale per le operazioni WPF using (MemoryStream localStream = (s is MemoryStream) ? (MemoryStream)s : StreamExtensions.CopyToMemoryStream(s)) { if (s != null && restoreStreamPosition && s.CanSeek) { s.Seek(originalPosition, SeekOrigin.Begin); } if (disposeStream) { s.Dispose(); } /* ? ? ? */ IEncoder managedEncoder = c.Plugins.GetEncoder(job.Settings, job.SourcePathData); bool supportsTransparency = managedEncoder.SupportsTransparency; // Recupero le dimensioni originali var frame = BitmapFrame.Create(StreamExtensions.CopyToMemoryStream(localStream)); System.Windows.Size originalSize = new System.Windows.Size(frame.PixelWidth, frame.PixelHeight); // Resetto lo stream locale alla posizione iniziale, dopo aver letto i metadata localStream.Position = 0; // Uhm... sono costretto a referenziare le System.Drawing (GDI) per questo, // TODO: chiedere al tipo se si può prevedere un costruttore di ImageState che non preveda un System.Drawing.Size come parametro System.Drawing.Size orig = new System.Drawing.Size((int)originalSize.Width, (int)originalSize.Height); using (ImageState imageState = new ImageState(settings, orig, true)) { c.CurrentImageBuilder.Process(imageState); Rectangle imageDest = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(imageState.layout["image"])); BitmapSource finalImage; BitmapImage bi = new BitmapImage(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.BeginInit(); bi.StreamSource = localStream; WpfImageSettings wpfImageSettings = imageState.WpfDestinationImageSettings(settings); bi.DecodePixelWidth = Convert.ToInt32(wpfImageSettings.DestinationImageWidth); bi.DecodePixelHeight = Convert.ToInt32(wpfImageSettings.DestinationImageHeight); bi.EndInit(); // Creation of the encoder WpfEncoderPlugin wpfEncoder = new WpfEncoderPlugin(settings, job.SourcePathData); RenderTargetBitmap final = new RenderTargetBitmap(imageState.finalSize.Width, imageState.finalSize.Height, settings.Get <int>("dpi", 96), settings.Get <int>("dpi", 96), PixelFormats.Default); DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { string ARGBBackgroundColor = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", wpfEncoder.MimeType.Equals("image/jpeg") ? 255 : settings.BackgroundColor.A, settings.BackgroundColor.R, settings.BackgroundColor.G, settings.BackgroundColor.B); System.Windows.Media.Brush BrushBackgroundColor = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(ARGBBackgroundColor)); /* todo: verificare */ dc.DrawRectangle(BrushBackgroundColor, null, new Rect(0, 0, wpfImageSettings.DestinationImageWidth, wpfImageSettings.DestinationImageHeight)); Rect rect = new Rect(wpfImageSettings.OffsetX, wpfImageSettings.OffsetY, wpfImageSettings.DestinationImageWidth, wpfImageSettings.DestinationImageHeight); //dc.PushTransform(new RotateTransform(settings.Rotate, (double)imageState.finalSize.Width / 2, (double)imageState.finalSize.Height / 2)); dc.DrawImage(bi, rect); } final.Render(dv); finalImage = final; // Write the image to the output stream wpfEncoder.Write(finalImage, (Stream)job.Dest); } } return(RequestedAction.None); }
/// <summary> /// Decodes the image in byte[] data, performs the image proccessing, and encodes it to job.Dest /// </summary> /// <param name="data">The buffer containing the encoded image file</param> /// <param name="lData">The number of bytes to read</param> /// <param name="job"></param> /// <param name="supportsTransparency"></param> /// <returns></returns> protected virtual RequestedAction BuildJobWic(byte[] data, long lData, ImageJob job, bool supportsTransparency) { ResizeSettings settings = job.Settings; ResizeSettings q = settings; string path = job.SourcePathData; //A list of COM objects to destroy List <object> com = new List <object>(); try { //Create the factory IWICComponentFactory factory = (IWICComponentFactory) new WICImagingFactory(); com.Add(factory); //Wrap the byte[] with a IWICStream instance var streamWrapper = factory.CreateStream(); streamWrapper.InitializeFromMemory(data, (uint)lData); com.Add(streamWrapper); var decoder = factory.CreateDecoderFromStream(streamWrapper, null, WICDecodeOptions.WICDecodeMetadataCacheOnLoad); com.Add(decoder); //Figure out which frame to work with int frameIndex = 0; if (!string.IsNullOrEmpty(q["page"]) && !int.TryParse(q["page"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out frameIndex)) { if (!string.IsNullOrEmpty(q["frame"]) && !int.TryParse(q["frame"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out frameIndex)) { frameIndex = 0; } } //So users can use 1-based numbers frameIndex--; if (frameIndex > 0) { int frameCount = (int)decoder.GetFrameCount(); //Don't let the user go past the end. if (frameIndex >= frameCount) { frameIndex = frameCount - 1; } } IWICBitmapFrameDecode frame = decoder.GetFrame((uint)Math.Max(0, frameIndex)); com.Add(frame); WICBitmapInterpolationMode interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeFant; if ("nearest".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase)) { interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeNearestNeighbor; } if ("bicubic".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase)) { interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeCubic; } if ("linear".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase)) { interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeLinear; } if ("nearestneighbor".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase)) { interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeLinear; } //Find the original image size uint origWidth, origHeight; frame.GetSize(out origWidth, out origHeight); Size orig = new Size((int)origWidth, (int)origHeight); Guid pixelFormat; frame.GetPixelFormat(out pixelFormat); //Calculate the new size of the image and the canvas. ImageState state = new ImageState(settings, orig, true); c.CurrentImageBuilder.Process(state); Rectangle imageDest = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(state.layout["image"])); IWICBitmapSource imageData = frame; //Are we cropping? then daisy-chain a clipper if (state.copyRect.Left != 0 || state.copyRect.Top != 0 || state.copyRect.Width != state.originalSize.Width || state.copyRect.Height != state.originalSize.Height) { //Cropping is absurdly slow... 4x slower than resizing! //Cropping after resizing (unintuitively) is faster. if (imageDest.Width != state.originalSize.Width || imageDest.Height != state.originalSize.Height) { double sx = (double)imageDest.Width / (double)state.copyRect.Width; double sy = (double)imageDest.Height / (double)state.copyRect.Height; uint uncroppedDestWidth = (uint)Math.Round(sx * state.originalSize.Width); uint uncroppedDestHeight = (uint)Math.Round(sy * state.originalSize.Height); var scaler = factory.CreateBitmapScaler(); scaler.Initialize(imageData, uncroppedDestWidth, uncroppedDestHeight, interpolationMode); com.Add(scaler); //TODO: cropping is not consistent with GDI. var clipper = factory.CreateBitmapClipper(); clipper.Initialize(scaler, new WICRect { X = (int)Math.Floor((double)state.copyRect.X * sx), Y = (int)Math.Floor((double)state.copyRect.Y * sy), Width = imageDest.Width, Height = imageDest.Height }); com.Add(clipper); imageData = clipper; } else { var clipper = factory.CreateBitmapClipper(); clipper.Initialize(imageData, new WICRect { X = (int)state.copyRect.X, Y = (int)state.copyRect.Y, Width = (int)state.copyRect.Width, Height = (int)state.copyRect.Height }); com.Add(clipper); imageData = clipper; } //If we're scaling but not cropping. } else if (imageDest.Width != state.originalSize.Width || imageDest.Height != state.originalSize.Height) { var scaler = factory.CreateBitmapScaler(); scaler.Initialize(imageData, (uint)imageDest.Width, (uint)imageDest.Height, interpolationMode); com.Add(scaler); imageData = scaler; } //Are we padding? Then we have to do an intermediate write. if (state.destSize.Width != imageDest.Width || state.destSize.Height != imageDest.Height) { byte[] bgcolor = ConversionUtils.ConvertColor(job.Settings.BackgroundColor, pixelFormat); for (int i = 0; i < bgcolor.Length; i++) { bgcolor[i] = 255; //White } var padder = new WicBitmapPadder(imageData, imageDest.X, imageDest.Y, state.destSize.Width - (imageDest.X + imageDest.Width), state.destSize.Height - (imageDest.Y + imageDest.Height), bgcolor, null); imageData = padder; } //Now encode imageData and be done with it... return(Encode(factory, imageData, imageDest.Size, job)); } finally { //Manually cleanup all the com reference counts, aggressively while (com.Count > 0) { Marshal.ReleaseComObject(com[com.Count - 1]); //In reverse order, so no item is ever deleted out from under another. com.RemoveAt(com.Count - 1); } } }