public virtual void Extract(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata) { PsdHeaderDirectory directory = metadata.GetOrCreateDirectory <PsdHeaderDirectory>(); try { int signature = reader.GetInt32(0); if (signature != unchecked ((int)(0x38425053))) { directory.AddError("Invalid PSD file signature"); return; } int version = reader.GetUInt16(4); if (version != 1 && version != 2) { directory.AddError("Invalid PSD file version (must be 1 or 2)"); return; } // 6 reserved bytes are skipped here. They should be zero. int channelCount = reader.GetUInt16(12); directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount); // even though this is probably an unsigned int, the max height in practice is 300,000 int imageHeight = reader.GetInt32(14); directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight); // even though this is probably an unsigned int, the max width in practice is 300,000 int imageWidth = reader.GetInt32(18); directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth); int bitsPerChannel = reader.GetUInt16(22); directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel); int colorMode = reader.GetUInt16(24); directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode); } catch (IOException) { directory.AddError("Unable to read PSD header"); } }
public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata) { PsdHeaderDirectory directory = new PsdHeaderDirectory(); metadata.AddDirectory(directory); // FILE HEADER SECTION try { int signature = reader.GetInt32(); if (signature != unchecked ((int)(0x38425053))) { // "8BPS" directory.AddError("Invalid PSD file signature"); return; } int version = reader.GetUInt16(); if (version != 1 && version != 2) { directory.AddError("Invalid PSD file version (must be 1 or 2)"); return; } // 6 reserved bytes are skipped here. They should be zero. reader.Skip(6); int channelCount = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount); // even though this is probably an unsigned int, the max height in practice is 300,000 int imageHeight = reader.GetInt32(); directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight); // even though this is probably an unsigned int, the max width in practice is 300,000 int imageWidth = reader.GetInt32(); directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth); int bitsPerChannel = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel); int colorMode = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode); } catch (IOException) { directory.AddError("Unable to read PSD header"); return; } // COLOR MODE DATA SECTION try { long sectionLength = reader.GetUInt32(); /* * Only indexed color and duotone (see the mode field in the File header section) have color mode data. * For all other modes, this section is just the 4-byte length field, which is set to zero. * * Indexed color images: length is 768; color data contains the color table for the image, * in non-interleaved order. * Duotone images: color data contains the duotone specification (the format of which is not documented). * Other applications that read Photoshop files can treat a duotone image as a gray image, * and just preserve the contents of the duotone information when reading and writing the * file. */ reader.Skip(sectionLength); } catch (IOException) { return; } // IMAGE RESOURCES SECTION try { long sectionLength = reader.GetUInt32(); System.Diagnostics.Debug.Assert((sectionLength <= int.MaxValue)); new PhotoshopReader().Extract(reader, (int)sectionLength, metadata); } catch (IOException) { } }
public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata) { PsdHeaderDirectory directory = new PsdHeaderDirectory(); metadata.AddDirectory(directory); // FILE HEADER SECTION try { int signature = reader.GetInt32(); if (signature != unchecked((int)(0x38425053))) { // "8BPS" directory.AddError("Invalid PSD file signature"); return; } int version = reader.GetUInt16(); if (version != 1 && version != 2) { directory.AddError("Invalid PSD file version (must be 1 or 2)"); return; } // 6 reserved bytes are skipped here. They should be zero. reader.Skip(6); int channelCount = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount); // even though this is probably an unsigned int, the max height in practice is 300,000 int imageHeight = reader.GetInt32(); directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight); // even though this is probably an unsigned int, the max width in practice is 300,000 int imageWidth = reader.GetInt32(); directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth); int bitsPerChannel = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel); int colorMode = reader.GetUInt16(); directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode); } catch (IOException) { directory.AddError("Unable to read PSD header"); return; } // COLOR MODE DATA SECTION try { long sectionLength = reader.GetUInt32(); /* * Only indexed color and duotone (see the mode field in the File header section) have color mode data. * For all other modes, this section is just the 4-byte length field, which is set to zero. * * Indexed color images: length is 768; color data contains the color table for the image, * in non-interleaved order. * Duotone images: color data contains the duotone specification (the format of which is not documented). * Other applications that read Photoshop files can treat a duotone image as a gray image, * and just preserve the contents of the duotone information when reading and writing the * file. */ reader.Skip(sectionLength); } catch (IOException) { return; } // IMAGE RESOURCES SECTION try { long sectionLength = reader.GetUInt32(); System.Diagnostics.Debug.Assert((sectionLength <= int.MaxValue)); new PhotoshopReader().Extract(reader, (int)sectionLength, metadata); } catch (IOException) { } }