Exemplo n.º 1
0
        public static PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> CreateParallelRenderer(PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> renderer, PaintDotNet.Rendering.TilingStrategy tilingStrategy, int tileEdge, PaintDotNet.Threading.WorkItemQueuePriority workItemQueuePriority)
        {
            Type genericType  = GetAssemblyPaintDotNetCore().GetType("PaintDotNet.Rendering.ParallelizeRenderer`1");
            Type concreteType = genericType.MakeGenericType(typeof(PaintDotNet.ColorBgra));

            PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> outRenderer = (PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra>)
                                                                                  GetAssemblyPaintDotNetCore().CreateInstance(
                concreteType.FullName,
                false,
                BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                null, new object[] { renderer, tilingStrategy, tileEdge, workItemQueuePriority },
                null,
                new object[0]);

            if (outRenderer == null)
            {
                throw new NullReferenceException("outRenderer");
            }

            return(outRenderer);
        }
Exemplo n.º 2
0
        public static PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> CreateTileizeRenderer(PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> renderer, PaintDotNet.Rendering.SizeInt32 size)
        {
            Type genericType  = GetAssemblyPaintDotNetCore().GetType("PaintDotNet.Rendering.TileizeRenderer`1");
            Type concreteType = genericType.MakeGenericType(typeof(PaintDotNet.ColorBgra));

            PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> outRenderer = (PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra>)
                                                                                  GetAssemblyPaintDotNetCore().CreateInstance(
                concreteType.FullName,
                false,
                BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                null, new object[] { renderer, size },
                null,
                new object[0]);

            if (outRenderer == null)
            {
                throw new NullReferenceException("outRenderer");
            }

            return(outRenderer);
        }
        public void Process(UpdateContext ctx, Dictionary <string, FileTypeSaveTokenPair> saveTokens, IProcessingEventListener events, int index)
        {
            PaintDotNet.FileType        srcFileType  = ctx.FileTypes[System.IO.Path.GetExtension(this.SourcePath)];
            PaintDotNet.FileType        destFileType = ctx.OutputFileType ?? ctx.FileTypes[System.IO.Path.GetExtension(this.DestinationPath)];
            PaintDotNet.Document        doc          = null;
            PaintDotNet.Document        newDoc       = null;
            PaintDotNet.SaveConfigToken token        = null;

            if (ctx.RenameFiles.Length > 0)
            {
                this.DestinationPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.DestinationPath), Util.SanitizePath(ctx.RenameFiles, index));
            }

            if (System.IO.Path.GetExtension(this.DestinationPath).Length == 0)
            {
                this.DestinationPath += destFileType.DefaultExtension;
            }
            else if (ctx.OutputFileType != null)
            {
                this.DestinationPath = System.IO.Path.ChangeExtension(this.DestinationPath, ctx.OutputFileType.DefaultExtension);
            }

            string dir = System.IO.Path.GetDirectoryName(this.DestinationPath);

            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
                events.LogInfo("Created directory \'{0}\'", dir);
            }

            FileTypeSaveTokenPair tokenPair;

            if (!saveTokens.TryGetValue(destFileType.Name, out tokenPair))
            {
                token = destFileType.GetLastSaveConfigToken();
            }
            else
            {
                token = tokenPair.SaveToken;
            }

            using (System.IO.FileStream file = System.IO.File.OpenRead(this.SourcePath))
            {
                newDoc = doc = srcFileType.Load(file);
            }

            // Image resize
            int newWidth  = ctx.NewWidthInPixels;
            int newHeight = ctx.NewHeightInPixels;

            if (ctx.UsePercent)
            {
                if (ctx.Percent != 100)
                {
                    double percent = ctx.Percent / 100.0;
                    newWidth  = (int)(doc.Width * percent);
                    newHeight = (int)(doc.Height * percent);
                }
                else
                {
                    newWidth  = doc.Width;
                    newHeight = doc.Height;
                }
            }

            if (doc.Width != newWidth || doc.Height != newHeight)
            {
                newDoc = new PaintDotNet.Document(newWidth, newHeight);
                newDoc.ReplaceMetadataFrom(doc);
                newDoc.DpuUnit = ctx.DpuUnit;
                newDoc.DpuX    = ctx.Dpu;
                newDoc.DpuY    = ctx.Dpu;

                for (int i = 0; i < doc.Layers.Count; ++i)
                {
                    PaintDotNet.BitmapLayer oldLayer = (PaintDotNet.BitmapLayer)doc.Layers[i];
                    PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> renderer = null;

                    switch (ctx.ResamplingAlgorithm)
                    {
                    case PaintDotNet.ResamplingAlgorithm.Bicubic:
                    {
                        renderer = Util.CreateResizeBicubic(oldLayer.Surface, newWidth, newHeight);
                        break;
                    }

                    case PaintDotNet.ResamplingAlgorithm.Bilinear:
                    {
                        renderer = Util.CreateResizeBilinear(oldLayer.Surface, newWidth, newHeight);
                        break;
                    }

                    case PaintDotNet.ResamplingAlgorithm.NearestNeighbor:
                    {
                        renderer = Util.CreateResizeNearestNeighbor(oldLayer.Surface, newWidth, newHeight);
                        break;
                    }

                    case PaintDotNet.ResamplingAlgorithm.SuperSampling:
                    {
                        if (newWidth >= oldLayer.Width || newHeight >= oldLayer.Height)
                        {
                            renderer = Util.CreateResizeBicubic(oldLayer.Surface, newWidth, newHeight);
                        }
                        else
                        {
                            renderer = Util.CreateResizeSuperSampling(oldLayer.Surface, newWidth, newHeight);
                        }

                        break;
                    }
                    }

                    PaintDotNet.Rendering.ObservableRenderer <PaintDotNet.ColorBgra> observableRenderer = new PaintDotNet.Rendering.ObservableRenderer <PaintDotNet.ColorBgra>(renderer);
                    PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra>          parallelRenderer   = Environment.ProcessorCount > 1 ?
                                                                                                          Util.CreateParallelRenderer(observableRenderer, PaintDotNet.Rendering.TilingStrategy.Tiles, 7, PaintDotNet.Threading.WorkItemQueuePriority.High) :
                                                                                                          Util.CreateTileizeRenderer(renderer, new PaintDotNet.Rendering.SizeInt32(1 << 7, 1 << 7));

                    PaintDotNet.Surface newSurface = new PaintDotNet.Surface(newWidth, newHeight);
                    parallelRenderer.Render(newSurface, new PaintDotNet.Rendering.Point2Int32(0, 0));

                    PaintDotNet.BitmapLayer newLayer = new PaintDotNet.BitmapLayer(newSurface, true);
                    newLayer.LoadProperties(oldLayer.SaveProperties());

                    newDoc.Layers.Add(newLayer);
                }
            }

            // Canvas resize
            UpdateContext.CanvasResizeContext canvasResize = ctx.CanvasResize;
            if (!((canvasResize.UsePercent == true) && (canvasResize.Percent == 100)))
            {
                System.Drawing.Size newSize = canvasResize.GetNewSize(newDoc.Width, newDoc.Height);
                newDoc = Util.CanvasResize(newDoc, newSize, canvasResize.Anchor, PaintDotNet.ColorBgra.White);
            }

            using (System.IO.FileStream file = System.IO.File.Create(this.DestinationPath))
            {
                using (PaintDotNet.Surface scratchSurface = new PaintDotNet.Surface(newDoc.Width, newDoc.Height))
                {
                    PaintDotNet.ProgressEventHandler progressEventHandler = delegate(object sender, PaintDotNet.ProgressEventArgs e)
                    {
                        // Nothing to do
                    };
                    destFileType.Save(newDoc, file, token, scratchSurface, progressEventHandler, false);
                }
            }

            doc.Dispose();

            if (!newDoc.IsDisposed)
            {
                newDoc.Dispose();
            }
        }
Exemplo n.º 4
0
 public static PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> CreateResizeSuperSampling(PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra> source, int width, int height)
 {
     return((PaintDotNet.Rendering.IRenderer <PaintDotNet.ColorBgra>)GetAssemblyPaintDotNetCore().CreateInstance(
                "PaintDotNet.Rendering.ResizeSuperSamplingRendererBgra",
                false,
                BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                null, new object[] { source, width, height },
                null,
                new object[0]));
 }