/* File Header Section * Len Description Value * 4 Signature "8BPS" * 2 Version 1 * 6 Reserved not used. * 2 Channels 1 ~ 56 * 4 Height 1 ~ 30,000 * 4 Width 1 ~ 30,000 * 2 Color Depth 1, 8, 16, 32 * 2 Color Mode 1 ~ 9 */ private static void ReadHeader(ref PsdInfo info, PSDReader reader) { // Signature if (reader.ReadByte() != 0x38 || reader.ReadByte() != 0x42 || reader.ReadByte() != 0x50 || reader.ReadByte() != 0x53) { throw new NotSupportedException(); } // Version var version = reader.ReadInt16(); if (version != 1) { throw new NotSupportedException(); } // Reserved reader.Position += 6; // Channels info.Channels = reader.ReadInt16(); // Height info.Height = reader.ReadInt32(); // Width info.Width = reader.ReadInt32(); // Color Depth info.ColorDepth = reader.ReadInt16(); info.ColorDepthBytes = info.ColorDepth / 8; if (info.ColorDepthBytes != 1 && info.ColorDepthBytes != 2 && info.ColorDepthBytes != 4) { throw new NotSupportedException(); } // Color Mode info.ColorMode = (ColorMods)reader.ReadInt16(); if (info.ColorMode == ColorMods.Bitmap) { throw new NotSupportedException(); } info.Pixcels = info.Width * info.Height; info.SizePerChannel = info.Pixcels * info.ColorDepthBytes; }
/* Layer and Mask Information Section * Len Description * 4 Section Length * v Layer info * v Global layer mask info * v Additional Layer Information */ private static void ReadLayerAndMaskInfomation(ref PsdInfo info, PSDReader reader) { var length = reader.ReadInt32(); reader.Position += length; }
/* Image Resources Section * Len Description Value * 4 Length * n Resource Block Data */ /* Image Resource Blocks * Len Description Value * 4 Signature '8BIM' * 2 Resource Id * n Name * 4 Resource Data Length * n Resource Data */ private static void ReadImageResources(ref PsdInfo info, PSDReader reader) { var length = reader.ReadInt32(); short id; int len; info.TransparencyIndex = -1; long pos = reader.Position + length; while (reader.Position < pos) { // Signature if (reader.ReadByte() != 0x38 || reader.ReadByte() != 0x42 || reader.ReadByte() != 0x49 || reader.ReadByte() != 0x4D) throw new NotSupportedException(); // Resource Id id = reader.ReadInt16(); // Name len = reader.ReadByte(); if (len > 0) { if((len % 2) != 0) len = reader.ReadByte(); reader.Position += len; } reader.Position += 1; // Data Length len = reader.ReadInt32(); if (len % 2 != 0) len++; switch (id) { case 1005: // ResolutionInfo structure info.DpiX = reader.ReadInt16(); reader.Position += 6; info.DpiY = reader.ReadInt16(); reader.Position += 6; break; case 1047: // Transparency Index. 2 bytes for the index of transparent color, if any. info.TransparencyIndex = reader.ReadInt16(); break; default: reader.Position += len; break; } } }
/* Color Mode Data Section * Len Description Value * 4 Length * n Color Data */ private static void ReadColorData(ref PsdInfo info, PSDReader reader) { var len = reader.ReadInt32(); info.ColorData = reader.ReadBytes(len); }
/* File Header Section * Len Description Value * 4 Signature "8BPS" * 2 Version 1 * 6 Reserved not used. * 2 Channels 1 ~ 56 * 4 Height 1 ~ 30,000 * 4 Width 1 ~ 30,000 * 2 Color Depth 1, 8, 16, 32 * 2 Color Mode 1 ~ 9 */ private static void ReadHeader(ref PsdInfo info, PSDReader reader) { // Signature if (reader.ReadByte() != 0x38 || reader.ReadByte() != 0x42 || reader.ReadByte() != 0x50 || reader.ReadByte() != 0x53) throw new NotSupportedException(); // Version var version = reader.ReadInt16(); if (version != 1) throw new NotSupportedException(); // Reserved reader.Position += 6; // Channels info.Channels = reader.ReadInt16(); // Height info.Height = reader.ReadInt32(); // Width info.Width = reader.ReadInt32(); // Color Depth info.ColorDepth = reader.ReadInt16(); info.ColorDepthBytes = info.ColorDepth / 8; if (info.ColorDepthBytes != 1 && info.ColorDepthBytes != 2 && info.ColorDepthBytes != 4) throw new NotSupportedException(); // Color Mode info.ColorMode = (ColorMods)reader.ReadInt16(); if (info.ColorMode == ColorMods.Bitmap) throw new NotSupportedException(); info.Pixcels = info.Width * info.Height; info.SizePerChannel = info.Pixcels * info.ColorDepthBytes; }
/* Image Resources Section * Len Description Value * 4 Length * n Resource Block Data */ /* Image Resource Blocks * Len Description Value * 4 Signature '8BIM' * 2 Resource Id * n Name * 4 Resource Data Length * n Resource Data */ private static void ReadImageResources(ref PsdInfo info, PSDReader reader) { var length = reader.ReadInt32(); short id; int len; info.TransparencyIndex = -1; long pos = reader.Position + length; while (reader.Position < pos) { // Signature if (reader.ReadByte() != 0x38 || reader.ReadByte() != 0x42 || reader.ReadByte() != 0x49 || reader.ReadByte() != 0x4D) { throw new NotSupportedException(); } // Resource Id id = reader.ReadInt16(); // Name len = reader.ReadByte(); if (len > 0) { if ((len % 2) != 0) { len = reader.ReadByte(); } reader.Position += len; } reader.Position += 1; // Data Length len = reader.ReadInt32(); if (len % 2 != 0) { len++; } switch (id) { case 1005: // ResolutionInfo structure info.DpiX = reader.ReadInt16(); reader.Position += 6; info.DpiY = reader.ReadInt16(); reader.Position += 6; break; case 1047: // Transparency Index. 2 bytes for the index of transparent color, if any. info.TransparencyIndex = reader.ReadInt16(); break; default: reader.Position += len; break; } } }