public static ImageResource CreateImageResource(PsdBinaryReader reader) { Util.DebugMessage(reader.BaseStream, "Load, Begin, ImageResource"); var signature = reader.ReadAsciiChars(4); var resourceIdInt = reader.ReadUInt16(); var name = reader.ReadPascalString(2); var dataLength = (int)reader.ReadUInt32(); var dataPaddedLength = Util.RoundUp(dataLength, 2); var endPosition = reader.BaseStream.Position + dataPaddedLength; ImageResource resource = null; var resourceId = (ResourceID)resourceIdInt; switch (resourceId) { case ResourceID.ResolutionInfo: resource = new ResolutionInfo(reader, name); break; case ResourceID.ThumbnailRgb: case ResourceID.ThumbnailBgr: resource = new Thumbnail(reader, resourceId, name, dataLength); break; case ResourceID.AlphaChannelNames: resource = new AlphaChannelNames(reader, name, dataLength); break; case ResourceID.UnicodeAlphaNames: resource = new UnicodeAlphaNames(reader, name, dataLength); break; case ResourceID.VersionInfo: resource = new VersionInfo(reader, name); break; default: resource = new RawImageResource(reader, signature, resourceId, name, dataLength); break; } Util.DebugMessage(reader.BaseStream, "Load, End, ImageResource, {0}", resourceId); // Reposition the reader if we do not consume the full resource block. // This takes care of the even-padding, and also preserves forward- // compatibility in case a resource block is later extended with // additional properties. if (reader.BaseStream.Position < endPosition) { reader.BaseStream.Position = endPosition; } // However, overruns are definitely an error. if (reader.BaseStream.Position > endPosition) { throw new PsdInvalidException("Corruption detected in resource."); } return(resource); }
/////////////////////////////////////////////////////////////////////////// private void LoadImageResources(BinaryReverseReader reader) { Debug.WriteLine("LoadImageResources started at " + reader.BaseStream.Position); m_imageResources.Clear(); uint imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) return; long startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { var imgRes = new ImageResource(reader); var resID = (ResourceIDs)imgRes.ID; switch (resID) { case ResourceIDs.ResolutionInfo: imgRes = new ResolutionInfo(imgRes); break; case ResourceIDs.Thumbnail1: case ResourceIDs.Thumbnail2: imgRes = new Thumbnail(imgRes); break; case ResourceIDs.AlphaChannelNames: imgRes = new AlphaChannels(imgRes); break; } m_imageResources.Add(imgRes); } //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; }