示例#1
0
        protected override RequestedAction PreRenderImage(ImageState s)
        {
            //Skip this when we are doing simulations
            if (s.destGraphics == null) return RequestedAction.None;

            //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();

            } finally {
                File.Delete(outputTempFile);
            }

            return RequestedAction.Cancel;
        }
示例#2
0
        public bool CairyIt(CairJob job)
        {
            //If we have too many threads waiting to run CAIR, just kill the request.
            if (_maxConcurrentWaitingThreads > 0 &&
                _concurrentWaitingThreads > _maxConcurrentWaitingThreads)
            {
                throw new ImageProcessingException("Content-aware image processing failed - too many threads waiting. Try again later.");
            }

            //If there are any threads waiting in line, or if the permitted number of CAIR.exe instances has been reached, get in line
            if (_concurrentWaitingThreads > 0 || (_maxConcurrentExecutions > 0 &&
                                                  _concurrentExecutions > _maxConcurrentExecutions))
            {
                try {
                    Interlocked.Increment(ref _concurrentWaitingThreads);
                    //Wait for a free slot
                    while (_maxConcurrentExecutions > 0 &&
                           _concurrentExecutions > _maxConcurrentExecutions)
                    {
                        turnstile.WaitOne(1000);
                    }
                } finally {
                    Interlocked.Decrement(ref _concurrentWaitingThreads);
                }
            }
            //Ok, there should be a free slot now.
            try {
                //Register, we have our own process slot now.
                Interlocked.Increment(ref _concurrentExecutions);

                //Make sure CAIR.exe exists. If not, recreate it
                if (!File.Exists(GetCair()))
                {
                    CairMissing();
                }

                /*CAIR CLI Usage: cair -I <input_file>
                 *  Other options:
                 *    -O <output_file>
                 *        Default: Dependent on operation
                 *    -W <weight_file>
                 *        Bitmap with: Black- no weight
                 *                     Green- Protect weight
                 *                     Red- Remove weight
                 *        Default: Weights are all zero
                 *    -S <weight_scale>
                 *        Default: 100,000
                 *    -X <goal_x>
                 *        Default: Source image width
                 *    -Y <goal_y>
                 *        Default: Source image height
                 *    -R <expected_result>
                 *        CAIR: 0
                 *        Grayscale: 1
                 *        Edge: 2
                 *        Vertical Energy: 3
                 *        Horizontal Energy: 4
                 *        Removal: 5
                 *        CAIR_HD: 6
                 *        Default: CAIR
                 *    -C <convolution_type>
                 *        Prewitt: 0
                 *        V1: 1
                 *        V_SQUARE: 2
                 *        Sobel: 3
                 *        Laplacian: 4
                 *        Default: Prewitt
                 *
                 *    -E <energy_type>
                 *        Backward: 0
                 *        Forward: 1
                 *        Default: Backward
                 *    -T <thread_count>
                 *        Default : CAIR_NUM_THREADS (4)
                 *  http://sourceforge.net/projects/c-a-i-r/*/

                string args = "";
                args += " -I \"" + job.SourcePath + "\"";
                args += " -O \"" + job.DestPath + "\"";
                if (job.WeightPath != null)
                {
                    args += " -W \"" + job.WeightPath + "\"";
                }
                args += " -T " + job.Threads;
                args += " -R " + ((int)job.Output).ToString(NumberFormatInfo.InvariantInfo);
                args += " -C " + ((int)job.Filter).ToString(NumberFormatInfo.InvariantInfo);
                args += " -E " + ((int)job.Energy).ToString(NumberFormatInfo.InvariantInfo);
                args += " -X " + job.Size.Width;
                args += " -Y " + job.Size.Height;

                ProcessStartInfo info = new ProcessStartInfo(GetCair(), args);
                info.UseShellExecute        = false;
                info.RedirectStandardError  = true;
                info.RedirectStandardOutput = true;
                info.CreateNoWindow         = true;

                using (Process p = Process.Start(info)) {
                    bool result = p.WaitForExit(job.Timeout);
                    if (!result)
                    {
                        p.Kill(); //Kill the process if it times out.
                        throw new ImageProcessingException("Content-aware image processing failed due to timeout.");
                    }
                    string messages = p.StandardError.ReadToEnd() + p.StandardOutput.ReadToEnd();
                    if (p.ExitCode != 0)
                    {
                        throw new ImageProcessingException("Content-aware image processing failed: " + messages);
                    }
                    return(result);
                }
            } finally {
                Interlocked.Decrement(ref _concurrentExecutions);
                turnstile.Set();
            }
        }
示例#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);
        }
示例#4
0
        public bool CairyIt(CairJob job)
        {
            //If we have too many threads waiting to run CAIR, just kill the request.
            if (_maxConcurrentWaitingThreads > 0 &&
                _concurrentWaitingThreads > _maxConcurrentWaitingThreads)
                throw new ImageProcessingException("Content-aware image processing failed - too many threads waiting. Try again later.");

            //If there are any threads waiting in line, or if the permitted number of CAIR.exe instances has been reached, get in line
            if (_concurrentWaitingThreads > 0 || (_maxConcurrentExecutions > 0 &&
                    _concurrentExecutions > _maxConcurrentExecutions)) {
                try {
                    Interlocked.Increment(ref _concurrentWaitingThreads);
                    //Wait for a free slot
                    while (_maxConcurrentExecutions > 0 &&
                        _concurrentExecutions > _maxConcurrentExecutions) {
                        turnstile.WaitOne(1000);
                    }
                } finally {
                    Interlocked.Decrement(ref _concurrentWaitingThreads);
                }
            }
            //Ok, there should be a free slot now.
            try {
                //Register, we have our own process slot now.
                Interlocked.Increment(ref _concurrentExecutions);

                //Make sure CAIR.exe exists. If not, recreate it
                if (!File.Exists(GetCair())) CairMissing();

                /*CAIR CLI Usage: cair -I <input_file>
                    Other options:
                      -O <output_file>
                          Default: Dependent on operation
                      -W <weight_file>
                          Bitmap with: Black- no weight
                                       Green- Protect weight
                                       Red- Remove weight
                          Default: Weights are all zero
                      -S <weight_scale>
                          Default: 100,000
                      -X <goal_x>
                          Default: Source image width
                      -Y <goal_y>
                          Default: Source image height
                      -R <expected_result>
                          CAIR: 0
                          Grayscale: 1
                          Edge: 2
                          Vertical Energy: 3
                          Horizontal Energy: 4
                          Removal: 5
                          CAIR_HD: 6
                          Default: CAIR
                      -C <convoluton_type>
                          Prewitt: 0
                          V1: 1
                          V_SQUARE: 2
                          Sobel: 3
                          Laplacian: 4
                          Default: Prewitt

                      -E <energy_type>
                          Backward: 0
                          Forward: 1
                          Default: Backward
                      -T <thread_count>
                          Default : CAIR_NUM_THREADS (4)
                    http://sourceforge.net/projects/c-a-i-r/*/

                string args = "";
                args += " -I \"" + job.SourcePath + "\"";
                args += " -O \"" + job.DestPath + "\"";
                if (job.WeightPath != null) args += " -W \"" + job.WeightPath + "\"";
                args += " -T " + job.Threads;
                args += " -R " + ((int)job.Output).ToString(NumberFormatInfo.InvariantInfo);
                args += " -C " + ((int)job.Filter).ToString(NumberFormatInfo.InvariantInfo);
                args += " -E " + ((int)job.Energy).ToString(NumberFormatInfo.InvariantInfo);
                args += " -X " + job.Size.Width;
                args += " -Y " + job.Size.Height;

                ProcessStartInfo info = new ProcessStartInfo(GetCair(), args);
                info.UseShellExecute = false;
                info.RedirectStandardError = true;
                info.RedirectStandardOutput = true;
                info.CreateNoWindow = true;

                using (Process p = Process.Start(info)) {
                    bool result = p.WaitForExit(job.Timeout);
                    if (!result) {
                        p.Kill(); //Kill the process if it times out.
                        throw new ImageProcessingException("Content-aware image processing failed due to timeout.");
                    }
                    string messages = p.StandardError.ReadToEnd() + p.StandardOutput.ReadToEnd();
                    if (p.ExitCode != 0)
                        throw new ImageProcessingException("Content-aware image processing failed: " + messages);
                    return result;
                }
            } finally {
                Interlocked.Decrement(ref _concurrentExecutions);
                turnstile.Set();
            }
        }