public static Bitmap DecodeImage(Layer layer)
        {
            if (layer.Rect.Width == 0 || layer.Rect.Height == 0) return null;

            Bitmap bitmap = new Bitmap(layer.Rect.Width, layer.Rect.Height, PixelFormat.Format32bppArgb);

            Parallel.For(0, layer.Rect.Height, y =>
            {
                Int32 rowIndex = y * layer.Rect.Width;

                for (Int32 x = 0; x < layer.Rect.Width; x++)
                {
                    Int32 pos = rowIndex + x;

                    Color pixelColor = GetColor(layer, pos);

                    if (layer.SortedChannels.ContainsKey(-2))
                    {
                        Int32 maskAlpha = GetColor(layer.MaskData, x, y);
                        Int32 oldAlpha = pixelColor.A;

                        Int32 newAlpha = (oldAlpha * maskAlpha) / 255;
                        pixelColor = Color.FromArgb(newAlpha, pixelColor);
                    }

                    lock (bitmap)
                    {
                        bitmap.SetPixel(x, y, pixelColor);
                    }
                }
            });

            return bitmap;
        }
			internal Channel(Int16 id, Layer layer)
			{
				ID = id;
				Layer = layer;
				Layer.Channels.Add(this);
				Layer.SortedChannels.Add(ID, this);
			}
			internal Channel(BinaryReverseReader reverseReader, Layer layer)
			{
				Debug.WriteLine("Channel started at " + reverseReader.BaseStream.Position.ToString(CultureInfo.InvariantCulture));

				ID = reverseReader.ReadInt16();
				Length = reverseReader.ReadInt32();

				Layer = layer;
			}
			public BlendingRanges(BinaryReverseReader reader, Layer layer)
			{
				Data = new Byte[0];
				Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture));

				Layer = layer;
				Int32 dataLength = reader.ReadInt32();
				if (dataLength <= 0) return;

				Data = reader.ReadBytes(dataLength);
			}
			public BlendingRanges(Layer layer)
			{
				Data = new Byte[0];
				Layer = layer;
				Layer.BlendingRangesData = this;
			}
        public static Bitmap DecodeImage(Layer.Mask mask)
        {
            Layer layer = mask.Layer;

            if (mask.Rect.Width == 0 || mask.Rect.Height == 0) return null;

            Bitmap bitmap = new Bitmap(mask.Rect.Width, mask.Rect.Height, PixelFormat.Format32bppArgb);

            Parallel.For(0, layer.Rect.Height, y =>
            {
                Int32 rowIndex = y * layer.Rect.Width;

                for (Int32 x = 0; x < layer.Rect.Width; x++)
                {
                    Int32 pos = rowIndex + x;

                    Color pixelColor = Color.FromArgb(mask.ImageData[pos], mask.ImageData[pos], mask.ImageData[pos]);

                    lock (bitmap)
                    {
                        bitmap.SetPixel(x, y, pixelColor);
                    }
                }
            });
            return bitmap;
        }
		private static Int32 GetColor(Layer.Mask mask, Int32 x, Int32 y)
		{
			Int32 c = 255;

			if (mask.PositionIsRelative)
			{
				x -= mask.Rect.X;
				y -= mask.Rect.Y;
			}
			else
			{
				x = (x + mask.Layer.Rect.X) - mask.Rect.X;
				y = (y + mask.Layer.Rect.Y) - mask.Rect.Y;
			}

			if (y >= 0 && y < mask.Rect.Height &&
				x >= 0 && x < mask.Rect.Width)
			{
				Int32 pos = y * mask.Rect.Width + x;
				c = pos < mask.ImageData.Length ? mask.ImageData[pos] : 255;
			}

			return c;
		}
		private static Color GetColor(Layer layer, Int32 pos)
		{
			Color c = Color.White;

			switch (layer.PsdFile.ColorMode)
			{
				case PsdFile.ColorModes.RGB:
					c = Color.FromArgb(layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[1].ImageData[pos], layer.SortedChannels[2].ImageData[pos]);
					break;
				case PsdFile.ColorModes.CMYK:
					c = CMYKToRGB(layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[1].ImageData[pos], layer.SortedChannels[2].ImageData[pos], layer.SortedChannels[3].ImageData[pos]);
					break;
				case PsdFile.ColorModes.Multichannel:
					c = CMYKToRGB(layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[1].ImageData[pos], layer.SortedChannels[2].ImageData[pos], 0);
					break;
				case PsdFile.ColorModes.Grayscale:
				case PsdFile.ColorModes.Duotone:
					c = Color.FromArgb(layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[0].ImageData[pos]);
					break;
				case PsdFile.ColorModes.Indexed:
					{
						Int32 index = layer.SortedChannels[0].ImageData[pos];
						c = Color.FromArgb(layer.PsdFile.ColorModeData[index], layer.PsdFile.ColorModeData[index + 256], layer.PsdFile.ColorModeData[index + 2 * 256]);
					}
					break;
				case PsdFile.ColorModes.Lab:
					{
						c = LabToRGB(layer.SortedChannels[0].ImageData[pos], layer.SortedChannels[1].ImageData[pos], layer.SortedChannels[2].ImageData[pos]);
					}
					break;
			}

			if (layer.SortedChannels.ContainsKey(-1)) c = Color.FromArgb(layer.SortedChannels[-1].ImageData[pos], c);

			return c;
		}
Example #9
0
			internal Mask(BinaryReverseReader reader, Layer layer)
			{
				Debug.WriteLine("Mask started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture));

				Layer = layer;

				uint maskLength = reader.ReadUInt32();

				if (maskLength <= 0)
					return;

				long startPosition = reader.BaseStream.Position;

				Rectangle localRectangle = new Rectangle
				{
					Y = reader.ReadInt32(),
					X = reader.ReadInt32()
				};
				localRectangle.Height = reader.ReadInt32() - localRectangle.Y;
				localRectangle.Width = reader.ReadInt32() - localRectangle.X;

				Rect = localRectangle;

				DefaultColor = reader.ReadByte();

				byte flags = reader.ReadByte();
				_flags = new BitVector32(flags);

				if (maskLength == 36)
				{
#pragma warning disable 168
					BitVector32 realFlags = new BitVector32(reader.ReadByte());

					byte realUserMaskBackground = reader.ReadByte();

					Rectangle rect = new Rectangle
					{
						Y = reader.ReadInt32(),
						X = reader.ReadInt32(),
						Height = reader.ReadInt32() - Rect.Y,
						Width = reader.ReadInt32() - Rect.X
					};
#pragma warning restore 168
                }

				// there is other stuff following, but we will ignore this.
				reader.BaseStream.Position = startPosition + maskLength;
			}
Example #10
0
			internal Mask(Layer layer)
			{
				Layer = layer;
				Layer.MaskData = this;
			}