public static Bitmap DecodeImage(PsdFile psdFile) { Bitmap bitmap = new Bitmap(psdFile.Columns, psdFile.Rows, PixelFormat.Format32bppArgb); #if TEST for (int y = 0; y < psdFile.Rows; y++) { int rowIndex = y * psdFile.Columns; for (int x = 0; x < psdFile.Columns; x++) { int pos = rowIndex + x; Color pixelColor=GetColor(psdFile,pos); bitmap.SetPixel(x, y, pixelColor); } } #else Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData bd = bitmap.LockBits(r, ImageLockMode.ReadWrite, bitmap.PixelFormat); unsafe { byte* pCurrRowPixel = (byte*)bd.Scan0.ToPointer(); for (int y = 0; y < psdFile.Rows; y++) { int rowIndex = y * psdFile.Columns; PixelData* pCurrPixel = (PixelData*)pCurrRowPixel; for (int x = 0; x < psdFile.Columns; x++) { int pos = rowIndex + x; Color pixelColor = GetColor(psdFile, pos); pCurrPixel->Alpha = 255; pCurrPixel->Red = pixelColor.R; pCurrPixel->Green = pixelColor.G; pCurrPixel->Blue = pixelColor.B; pCurrPixel += 1; } pCurrRowPixel += bd.Stride; } } bitmap.UnlockBits(bd); #endif return bitmap; }
/////////////////////////////////////////////////////////////////////////// private static Color GetColor(PsdFile psdFile, int pos) { Color c = Color.White; switch (psdFile.ColorMode) { case PsdFile.ColorModes.RGB: c = Color.FromArgb(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos]); break; case PsdFile.ColorModes.CMYK: c = CMYKToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos], psdFile.ImageData[3][pos]); break; case PsdFile.ColorModes.Multichannel: c = CMYKToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos], 0); break; case PsdFile.ColorModes.Grayscale: case PsdFile.ColorModes.Duotone: c = Color.FromArgb(psdFile.ImageData[0][pos], psdFile.ImageData[0][pos], psdFile.ImageData[0][pos]); break; case PsdFile.ColorModes.Indexed: { int index = (int)psdFile.ImageData[0][pos]; c = Color.FromArgb((int)psdFile.ColorModeData[index], psdFile.ColorModeData[index + 256], psdFile.ColorModeData[index + 2 * 256]); } break; case PsdFile.ColorModes.Lab: { c = LabToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos]); } break; } return(c); }
/////////////////////////////////////////////////////////////////////////// public static Bitmap DecodeImage(PsdFile psdFile) { Bitmap bitmap = new Bitmap(psdFile.Columns, psdFile.Rows, PixelFormat.Format32bppArgb); #if TEST for (int y = 0; y < psdFile.Rows; y++) { int rowIndex = y * psdFile.Columns; for (int x = 0; x < psdFile.Columns; x++) { int pos = rowIndex + x; Color pixelColor = GetColor(psdFile, pos); bitmap.SetPixel(x, y, pixelColor); } } #else Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData bd = bitmap.LockBits(r, ImageLockMode.ReadWrite, bitmap.PixelFormat); unsafe { byte *pCurrRowPixel = (byte *)bd.Scan0.ToPointer(); for (int y = 0; y < psdFile.Rows; y++) { int rowIndex = y * psdFile.Columns; PixelData *pCurrPixel = (PixelData *)pCurrRowPixel; for (int x = 0; x < psdFile.Columns; x++) { int pos = rowIndex + x; Color pixelColor = GetColor(psdFile, pos); pCurrPixel->Alpha = 255; pCurrPixel->Red = pixelColor.R; pCurrPixel->Green = pixelColor.G; pCurrPixel->Blue = pixelColor.B; pCurrPixel += 1; } pCurrRowPixel += bd.Stride; } } bitmap.UnlockBits(bd); #endif return(bitmap); }
public Layer(BinaryReverseReader reader, PsdFile psdFile) { Debug.WriteLine("Layer started at " + reader.BaseStream.Position.ToString()); m_psdFile = psdFile; m_rect = new Rectangle(); m_rect.Y = reader.ReadInt32(); m_rect.X = reader.ReadInt32(); m_rect.Height = reader.ReadInt32() - m_rect.Y; m_rect.Width = reader.ReadInt32() - m_rect.X; //----------------------------------------------------------------------- int numberOfChannels = reader.ReadUInt16(); this.m_channels.Clear(); for (int channel = 0; channel < numberOfChannels; channel++) { Channel ch = new Channel(reader, this); m_channels.Add(ch); m_sortedChannels.Add(ch.ID, ch); } //----------------------------------------------------------------------- string signature = new string(reader.ReadChars(4)); if (signature != "8BIM") throw (new IOException("Layer Channelheader error!")); m_blendModeKey = new string(reader.ReadChars(4)); m_opacity = reader.ReadByte(); m_clipping = reader.ReadByte() > 0; //----------------------------------------------------------------------- byte flags = reader.ReadByte(); m_flags = new BitVector32(flags); //----------------------------------------------------------------------- reader.ReadByte(); //padding //----------------------------------------------------------------------- Debug.WriteLine("Layer extraDataSize started at " + reader.BaseStream.Position.ToString()); // this is the total size of the MaskData, the BlendingRangesData, the // Name and the AdjustmenLayerInfo uint extraDataSize = reader.ReadUInt32(); // remember the start position for calculation of the // AdjustmenLayerInfo size long extraDataStartPosition = reader.BaseStream.Position; m_maskData = new Mask(reader, this); m_blendingRangesData = new BlendingRanges(reader, this); //----------------------------------------------------------------------- long namePosition = reader.BaseStream.Position; m_name = reader.ReadPascalString(); int paddingBytes = (int)((reader.BaseStream.Position - namePosition) % 4); Debug.Print("Layer {0} padding bytes after name", paddingBytes); reader.ReadBytes(paddingBytes); //----------------------------------------------------------------------- m_adjustmentInfo.Clear(); long adjustmenLayerEndPos = extraDataStartPosition + extraDataSize; while (reader.BaseStream.Position < adjustmenLayerEndPos) { try { m_adjustmentInfo.Add(new AdjusmentLayerInfo(reader, this)); } catch { reader.BaseStream.Position = adjustmenLayerEndPos; } } //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = adjustmenLayerEndPos; }
public Layer(PsdFile psdFile) { m_psdFile = psdFile; m_psdFile.Layers.Add(this); }
public Layer(BinaryReverseReader reader, PsdFile psdFile) { Debug.WriteLine("Layer started at " + reader.BaseStream.Position.ToString()); m_psdFile = psdFile; m_rect = new Rectangle(); m_rect.Y = reader.ReadInt32(); m_rect.X = reader.ReadInt32(); m_rect.Height = reader.ReadInt32() - m_rect.Y; m_rect.Width = reader.ReadInt32() - m_rect.X; //----------------------------------------------------------------------- int numberOfChannels = reader.ReadUInt16(); this.m_channels.Clear(); for (int channel = 0; channel < numberOfChannels; channel++) { Channel ch = new Channel(reader, this); m_channels.Add(ch); m_sortedChannels.Add(ch.ID, ch); } //----------------------------------------------------------------------- string signature = new string(reader.ReadChars(4)); if (signature != "8BIM") { throw (new IOException("Layer Channelheader error!")); } m_blendModeKey = new string(reader.ReadChars(4)); m_opacity = reader.ReadByte(); m_clipping = reader.ReadByte() > 0; //----------------------------------------------------------------------- byte flags = reader.ReadByte(); m_flags = new BitVector32(flags); //----------------------------------------------------------------------- reader.ReadByte(); //padding //----------------------------------------------------------------------- Debug.WriteLine("Layer extraDataSize started at " + reader.BaseStream.Position.ToString()); // this is the total size of the MaskData, the BlendingRangesData, the // Name and the AdjustmenLayerInfo uint extraDataSize = reader.ReadUInt32(); // remember the start position for calculation of the // AdjustmenLayerInfo size long extraDataStartPosition = reader.BaseStream.Position; m_maskData = new Mask(reader, this); m_blendingRangesData = new BlendingRanges(reader, this); //----------------------------------------------------------------------- long namePosition = reader.BaseStream.Position; m_name = reader.ReadPascalString(); int paddingBytes = (int)((reader.BaseStream.Position - namePosition) % 4); Debug.Print("Layer {0} padding bytes after name", paddingBytes); reader.ReadBytes(paddingBytes); //----------------------------------------------------------------------- m_adjustmentInfo.Clear(); long adjustmenLayerEndPos = extraDataStartPosition + extraDataSize; while (reader.BaseStream.Position < adjustmenLayerEndPos) { try { m_adjustmentInfo.Add(new AdjusmentLayerInfo(reader, this)); } catch { reader.BaseStream.Position = adjustmenLayerEndPos; } } //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = adjustmenLayerEndPos; }
private static Color GetColor(PsdFile psdFile, int pos) { Color c = Color.White; switch (psdFile.ColorMode) { case PsdFile.ColorModes.RGB: c = Color.FromArgb(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos]); break; case PsdFile.ColorModes.CMYK: c = CMYKToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos], psdFile.ImageData[3][pos]); break; case PsdFile.ColorModes.Multichannel: c = CMYKToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos], 0); break; case PsdFile.ColorModes.Grayscale: case PsdFile.ColorModes.Duotone: c = Color.FromArgb(psdFile.ImageData[0][pos], psdFile.ImageData[0][pos], psdFile.ImageData[0][pos]); break; case PsdFile.ColorModes.Indexed: { int index = (int)psdFile.ImageData[0][pos]; c = Color.FromArgb((int)psdFile.ColorModeData[index], psdFile.ColorModeData[index + 256], psdFile.ColorModeData[index + 2 * 256]); } break; case PsdFile.ColorModes.Lab: { c = LabToRGB(psdFile.ImageData[0][pos], psdFile.ImageData[1][pos], psdFile.ImageData[2][pos]); } break; } return c; }