예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }