public DecodeContext(PhotoshopFile.Layer layer, Rectangle bounds)
            {
                Layer           = layer;
                ByteDepth       = Util.BytesFromBitDepth(layer.PsdFile.BitDepth);
                HasAlphaChannel = 0;
                Channels        = layer.Channels.ToIdArray();

                var alphaSize = 4;

                if (layer.AlphaChannel != null && layer.AlphaChannel.ImageData.Length > 0)
                {
                    HasAlphaChannel = 1;
                    alphaSize       = layer.AlphaChannel.ImageData.Length;
                    alphaSize       = (alphaSize / 4) + (alphaSize % 4 > 0 ? 1 : 0);
                    alphaSize       = alphaSize * 4;
                }
                AlphaChannel = new NativeArray <byte>(alphaSize, Allocator.TempJob);
                if (HasAlphaChannel > 0)
                {
                    NativeArray <byte> .Copy(layer.AlphaChannel.ImageData, AlphaChannel, layer.AlphaChannel.ImageData.Length);
                }
                ColorMode     = layer.PsdFile.ColorMode;
                ColorModeData = new NativeArray <byte>(layer.PsdFile.ColorModeData, Allocator.TempJob);

                // Clip the layer to the specified bounds
                Rectangle = Layer.Rect.IntersectWith(bounds);

                if (layer.Masks != null)
                {
                    LayerMaskContext = GetMaskContext(layer.Masks.LayerMask);
                    UserMaskContext  = GetMaskContext(layer.Masks.UserMask);
                }
            }
예제 #2
0
            public DecodeContext(PhotoshopFile.Layer layer, Rectangle bounds)
            {
                Layer         = layer;
                ByteDepth     = Util.BytesFromBitDepth(layer.PsdFile.BitDepth);
                Channels      = layer.Channels.ToIdArray();
                AlphaChannel  = layer.AlphaChannel;
                ColorMode     = layer.PsdFile.ColorMode;
                ColorModeData = layer.PsdFile.ColorModeData;

                // Clip the layer to the specified bounds
                Rectangle = Layer.Rect.IntersectWith(bounds);

                if (layer.Masks != null)
                {
                    LayerMaskContext = GetMaskContext(layer.Masks.LayerMask);
                    UserMaskContext  = GetMaskContext(layer.Masks.UserMask);
                }
            }
예제 #3
0
        ///////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets one row of alpha values from the mask.
        /// </summary>
        /// <param name="y">The y-coordinate of the row.</param>
        /// <param name="layerContext">The decode context for the layer containing
        ///   the mask.</param>
        /// <param name="maskContext">The decode context for the mask.</param>
        /// <returns>An array of alpha values for the row, corresponding to the
        ///   width of the layer decode context.</returns>
        private static byte[] GetMaskAlphaRow(
            int y, DecodeContext layerContext, MaskDecodeContext maskContext)
        {
            if (maskContext == null)
            {
                return(null);
            }
            var mask = maskContext.Mask;

            // Background color for areas not covered by the mask
            byte backgroundColor = mask.InvertOnBlend
                ? (byte)(255 - mask.BackgroundColor)
                : mask.BackgroundColor;

            {
                Util.Fill(maskContext.AlphaBuffer, 0, maskContext.AlphaBuffer.Length,
                          backgroundColor);
            }
            if (maskContext.IsRowEmpty(y))
            {
                return(maskContext.AlphaBuffer);
            }

            //////////////////////////////////////
            // Transfer mask into the alpha array
            var pAlphaRow = maskContext.AlphaBuffer;
            var pMaskData = mask.ImageData;

            {
                // Get pointers to starting positions
                int alphaColumn = maskContext.Rectangle.X - layerContext.Rectangle.X;
                var pAlpha      = alphaColumn;
                var pAlphaEnd   = pAlpha + maskContext.Rectangle.Width;

                int maskRow      = y - maskContext.Mask.Rect.Y;
                int maskColumn   = maskContext.Rectangle.X - maskContext.Mask.Rect.X;
                int idxMaskPixel = (maskRow * mask.Rect.Width) + maskColumn;
                var pMask        = idxMaskPixel * layerContext.ByteDepth;

                // Take the high-order byte if values are 16-bit (little-endian)
                if (layerContext.ByteDepth == 2)
                {
                    pMask++;
                }

                // Decode mask into the alpha array.
                if (layerContext.ByteDepth == 4)
                {
                    DecodeMaskAlphaRow32(pAlphaRow, pAlpha, pAlphaEnd, pMaskData, pMask);
                }
                else
                {
                    DecodeMaskAlphaRow(pAlphaRow, pAlpha, pAlphaEnd, pMaskData, pMask, layerContext.ByteDepth);
                }

                // Obsolete since Photoshop CS6, but retained for compatibility with
                // older versions.  Note that the background has already been inverted.
                if (mask.InvertOnBlend)
                {
                    Util.Invert(pAlphaRow, pAlpha, pAlphaEnd);
                }
            }

            return(maskContext.AlphaBuffer);
        }