Пример #1
0
        protected override Document OnLoad(System.IO.Stream input)
        {
            PsdFile psdFile = new PsdFile();

            psdFile.Load(input);

            Document document = new Document(psdFile.Columns, psdFile.Rows);

            if (psdFile.Resolution != null)
            {
                document.DpuUnit = MeasurementUnit.Inch;
                document.DpuX    = psdFile.Resolution.HRes;
                document.DpuY    = psdFile.Resolution.VRes;
            }

            if (psdFile.Layers.Count == 0)
            {
                BitmapLayer layer = ImageDecoderPdn.DecodeImage(psdFile);
                document.Layers.Add(layer);
            }
            else
            {
                PaintDotNet.Threading.PrivateThreadPool threadPool = new PaintDotNet.Threading.PrivateThreadPool();
                var layersList = new List <Layer>();
                foreach (PhotoshopFile.Layer l in psdFile.Layers)
                {
                    if (!l.Rect.IsEmpty)
                    {
                        layersList.Add(null);
                        LoadLayerContext llc          = new LoadLayerContext(l, document, BlendModeKeyToBlendOp(l), layersList, layersList.Count - 1);
                        WaitCallback     waitCallback = new WaitCallback(llc.LoadLayer);
                        threadPool.QueueUserWorkItem(waitCallback);
                    }
                }
                threadPool.Drain();

                foreach (var layer in layersList)
                {
                    document.Layers.Add(layer);
                }
            }
            return(document);
        }
Пример #2
0
        protected override void OnSave(Document input, System.IO.Stream output,
                                       SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
        {
            PsdSaveConfigToken psdToken = (PsdSaveConfigToken)token;
            PsdFile            psdFile  = new PsdFile();

            //-----------------------------------------------------------------------

            psdFile.Rows    = input.Height;
            psdFile.Columns = input.Width;

            // we have an Alpha channel which will be saved,
            // we have to add this to our image resources
            psdFile.Channels = 4;

            // for now we oly save the images as RGB
            psdFile.ColorMode = PsdFile.ColorModes.RGB;

            psdFile.Depth = 8;

            //-----------------------------------------------------------------------
            // no color mode Data

            //-----------------------------------------------------------------------

            ResolutionInfo resInfo = new ResolutionInfo();

            resInfo.HeightUnit = ResolutionInfo.Unit.In;
            resInfo.WidthUnit  = ResolutionInfo.Unit.In;

            if (input.DpuUnit == MeasurementUnit.Inch)
            {
                resInfo.HResUnit = ResolutionInfo.ResUnit.PxPerInch;
                resInfo.VResUnit = ResolutionInfo.ResUnit.PxPerInch;

                resInfo.HRes = (short)input.DpuX;
                resInfo.VRes = (short)input.DpuY;
            }
            else
            {
                resInfo.HResUnit = ResolutionInfo.ResUnit.PxPerCent;
                resInfo.VResUnit = ResolutionInfo.ResUnit.PxPerCent;


                resInfo.HRes = (short)(input.DpuX / 2.54);
                resInfo.VRes = (short)(input.DpuY / 2.54);
            }

            psdFile.Resolution = resInfo;
            //-----------------------------------------------------------------------

            psdFile.ImageCompression = psdToken.RleCompress ? ImageCompression.Rle : ImageCompression.Raw;

            int size = psdFile.Rows * psdFile.Columns;

            psdFile.ImageData = new byte[psdFile.Channels][];
            for (int i = 0; i < psdFile.Channels; i++)
            {
                psdFile.ImageData[i] = new byte[size];
            }

            using (RenderArgs ra = new RenderArgs(scratchSurface))
            {
                input.Flatten(scratchSurface);
            }

            unsafe
            {
                for (int y = 0; y < psdFile.Rows; y++)
                {
                    int        rowIndex = y * psdFile.Columns;
                    ColorBgra *srcRow   = scratchSurface.GetRowAddress(y);
                    ColorBgra *srcPixel = srcRow;

                    for (int x = 0; x < psdFile.Columns; x++)
                    {
                        int pos = rowIndex + x;

                        psdFile.ImageData[0][pos] = srcPixel->R;
                        psdFile.ImageData[1][pos] = srcPixel->G;
                        psdFile.ImageData[2][pos] = srcPixel->B;
                        psdFile.ImageData[3][pos] = srcPixel->A;
                        srcPixel++;
                    }
                }
            }

            PaintDotNet.Threading.PrivateThreadPool threadPool = new PaintDotNet.Threading.PrivateThreadPool();
            foreach (BitmapLayer layer in input.Layers)
            {
                PhotoshopFile.Layer psdLayer = new PhotoshopFile.Layer(psdFile);
                BlendOpToBlendModeKey(layer.BlendOp, psdLayer);

                SaveLayerPixelsContext slc          = new SaveLayerPixelsContext(layer, psdFile, input, psdLayer, psdToken);
                WaitCallback           waitCallback = new WaitCallback(slc.SaveLayer);
                threadPool.QueueUserWorkItem(waitCallback);
            }
            threadPool.Drain();

            psdFile.Save(output);
        }