public static void Prepare() { if (ResourceTypes != null) return; //Get ImageResource types //TODO: is there a way to not get *all* types, but only those in a sub-namespace? ResourceTypes = new Dictionary<ResourceIDs, Type>(); Type[] types = typeof(ImageResource).Assembly.GetTypes(); foreach (Type type in types) { if (type.FullName.Contains("ImageResources.")) { if (type.FullName.Contains("+")) continue; Type actualType = type; // type.ReflectedType; //Because strangely, when type has internal classes, it wraps both of them somehow? e.g. GridGuidesInfo+GridGuide System.Reflection.ConstructorInfo ci = actualType.GetConstructor(new Type[] { }); ImageResource ir = (ImageResource)ci.Invoke(new object[] { }); ResourceIDs[] rids = ir.AcceptedResourceIDs; if (rids == null) { string name = actualType.FullName.Substring(type.FullName.LastIndexOf(".") + 1); rids = new ResourceIDs[] { (ResourceIDs)Enum.Parse(typeof(ResourceIDs), name) }; } foreach (ResourceIDs rid in rids) { ResourceTypes.Add(rid, actualType); } } } }
public void FreeRenderTarget(RenderTarget rt) { int id = rt.Id.ToInt32(); var rtw = ResourceIDs.Lookup[id] as RenderTargetWrapper; rtw.Target.Dispose(); ResourceIDs.Free(rt.Id); }
public unsafe RenderTarget CreateRenderTarget(int w, int h) { Texture2d tex = null; var rt = new RenderTarget(this, ResourceIDs.Alloc(ResourceIdManager.EResourceType.RenderTarget), tex); int id = rt.Id.ToInt32(); RenderTargetWrapper rtw = new RenderTargetWrapper(this); rtw.Target = rt; ResourceIDs.Lookup[id] = rtw; return(rt); }
/////////////////////////////////////////////////////////////////////////// private void LoadImageResources(BinaryReverseReader reader) { Debug.WriteLine("LoadImageResources started at " + reader.BaseStream.Position.ToString()); m_imageResources.Clear(); uint imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) { return; } long startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { ImageResource imgRes = new ImageResource(reader); ResourceIDs 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 sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; }
public static List <ImageResource> ReadImageResources(BinaryPSDReader reader) { List <ImageResource> result = new List <ImageResource>(); while (true) { long nBefore = reader.BaseStream.Position; string settingSignature = new string(reader.ReadPSDChars(4)); if (settingSignature != "8BIM") { reader.BaseStream.Position = nBefore; //reader.BaseStream.Position-=4; break; } ImageResource imgRes = new ImageResource(reader); ResourceIDs resID = (ResourceIDs)imgRes.ID; if (!Enum.IsDefined(typeof(ResourceIDs), (int)imgRes.ID)) { if (imgRes.ID > 2000 && imgRes.ID <= 2999) { //Stupid Adobe engineers... This is SO not using the same pattern as everything else!!! resID = ResourceIDs.PathInfo; } } if (ResourceTypes.ContainsKey(resID)) { Type type = ResourceTypes[resID]; System.Reflection.ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(ImageResource) }); imgRes = (ImageResource)ci.Invoke(new object[] { imgRes }); } //if (resID != ResourceIDs.Undefined) result.Add(imgRes); } return(result); }
public void Load(string filename) { using (FileStream stream = new FileStream(filename, FileMode.Open)) { //binary reverse reader reads data types in big-endian format. BinaryReverseReader reader = new BinaryReverseReader(stream); #region "Headers" //The headers area is used to check for a valid PSD file Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString()); string signature = new string(reader.ReadChars(4)); if (signature != "8BPS") { throw new IOException("Bad or invalid file stream supplied"); } //get the version number, should be 1 always if ((m_version = reader.ReadInt16()) != 1) { throw new IOException("Invalid version number supplied"); } //get rid of the 6 bytes reserverd in PSD format reader.BaseStream.Position += 6; //get the rest of the information from the PSD file. //Everytime ReadInt16() is called, it reads 2 bytes. //Everytime ReadInt32() is called, it reads 4 bytes. m_channels = reader.ReadInt16(); m_rows = reader.ReadInt32(); m_columns = reader.ReadInt32(); m_depth = reader.ReadInt16(); m_colorMode = (ColorModes)reader.ReadInt16(); //by end of headers, the reader has read 26 bytes into the file. #endregion //End Headers #region "ColorModeData" /// <summary> /// If ColorMode is ColorModes.Indexed, the following 768 bytes will contain /// a 256-color palette. If the ColorMode is ColorModes.Duotone, the data /// following presumably consists of screen parameters and other related information. /// Unfortunately, it is intentionally not documented by Adobe, and non-Photoshop /// readers are advised to treat duotone images as gray-scale images. /// </summary> Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString()); uint paletteLength = reader.ReadUInt32(); //readUint32() advances the reader 4 bytes. if (paletteLength > 0) { ColorModeData = reader.ReadBytes((int)paletteLength); } #endregion //End ColorModeData #region "Loading Image Resources" //This part takes extensive use of classes that I didn't write therefore //I can't document much on what they do. Debug.WriteLine("LoadingImageResources started at " + reader.BaseStream.Position.ToString()); m_imageResources.Clear(); uint imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) { return; } long startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { ImageResource imgRes = new ImageResource(reader); ResourceIDs 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 sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; #endregion //End LoadingImageResources #region "Layer and Mask Info" //We are gonna load up all the layers and masking of the PSD now. Debug.WriteLine("LoadLayerAndMaskInfo - Part1 started at " + reader.BaseStream.Position.ToString()); uint layersAndMaskLength = reader.ReadUInt32(); if (layersAndMaskLength <= 0) { return; } //new start position startPosition = reader.BaseStream.Position; //Lets start by loading up all the layers LoadLayers(reader); //we are done the layers, load up the masks LoadGlobalLayerMask(reader); // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersAndMaskLength; #endregion //End Layer and Mask info #region "Loading Final Image" //we have loaded up all the information from the PSD file //into variables we can use later on. //lets finish loading the raw data that defines the image //in the picture. Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position.ToString()); m_imageCompression = (ImageCompression)reader.ReadInt16(); m_imageData = new byte[m_channels][]; //--------------------------------------------------------------- if (m_imageCompression == ImageCompression.Rle) { // The RLE-compressed data is proceeded by a 2-byte data count for each row in the data, // which we're going to just skip. reader.BaseStream.Position += m_rows * m_channels * 2; } //--------------------------------------------------------------- int bytesPerRow = 0; switch (m_depth) { case 1: bytesPerRow = m_columns; //NOT Shure break; case 8: bytesPerRow = m_columns; break; case 16: bytesPerRow = m_columns * 2; break; } //--------------------------------------------------------------- for (int ch = 0; ch < m_channels; ch++) { m_imageData[ch] = new byte[m_rows * bytesPerRow]; switch (m_imageCompression) { case ImageCompression.Raw: reader.Read(m_imageData[ch], 0, m_imageData[ch].Length); break; case ImageCompression.Rle: { for (int i = 0; i < m_rows; i++) { int rowIndex = i * m_columns; RleHelper.DecodedRow(reader.BaseStream, m_imageData[ch], rowIndex, bytesPerRow); } } break; default: break; } } #endregion //End LoadingFinalImage } } //end Load()
public PsdFile Load(Stream stream) { //binary reverse reader reads data types in big-endian format. BinaryReverseReader reader = new BinaryReverseReader(stream); #region "Headers" //The headers area is used to check for a valid PSD file Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); String signature = new String(reader.ReadChars(4)); if (signature != "8BPS") { throw new IOException("Bad or invalid file stream supplied"); } //get the version number, should be 1 always if ((Version = reader.ReadInt16()) != 1) { throw new IOException("Invalid version number supplied"); } //get rid of the 6 bytes reserverd in PSD format reader.BaseStream.Position += 6; //get the rest of the information from the PSD file. //Everytime ReadInt16() is called, it reads 2 bytes. //Everytime ReadInt32() is called, it reads 4 bytes. _channels = reader.ReadInt16(); _rows = reader.ReadInt32(); _columns = reader.ReadInt32(); _depth = reader.ReadInt16(); ColorMode = (ColorModes)reader.ReadInt16(); //by end of headers, the reader has read 26 bytes into the file. #endregion //End Headers #region "ColorModeData" Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); UInt32 paletteLength = reader.ReadUInt32(); //readUint32() advances the reader 4 bytes. if (paletteLength > 0) { ColorModeData = reader.ReadBytes((Int32)paletteLength); } #endregion //End ColorModeData #region "Loading Image Resources" //This part takes extensive use of classes that I didn't write therefore //I can't document much on what they do. Debug.WriteLine("LoadingImageResources started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); _imageResources.Clear(); UInt32 imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) { return(null); } Int64 startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { ImageResource imgRes = new ImageResource(reader); ResourceIDs 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; } _imageResources.Add(imgRes); } // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; #endregion //End LoadingImageResources #region "Layer and Mask Info" //We are gonna load up all the layers and masking of the PSD now. Debug.WriteLine("LoadLayerAndMaskInfo - Part1 started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); UInt32 layersAndMaskLength = reader.ReadUInt32(); if (layersAndMaskLength <= 0) { return(null); } //new start position startPosition = reader.BaseStream.Position; //Lets start by loading up all the layers LoadLayers(reader); //we are done the layers, load up the masks LoadGlobalLayerMask(reader); // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersAndMaskLength; #endregion //End Layer and Mask info #region "Loading Final Image" //we have loaded up all the information from the PSD file //into variables we can use later on. //lets finish loading the raw data that defines the image //in the picture. Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); ImageCompression = (ImageCompression)reader.ReadInt16(); ImageData = new Byte[_channels][]; //--------------------------------------------------------------- if (ImageCompression == ImageCompression.Rle) { // The RLE-compressed data is proceeded by a 2-byte data count for each row in the data, // which we're going to just skip. reader.BaseStream.Position += _rows * _channels * 2; } //--------------------------------------------------------------- Int32 bytesPerRow = 0; switch (_depth) { case 1: bytesPerRow = _columns; //NOT Shure break; case 8: bytesPerRow = _columns; break; case 16: bytesPerRow = _columns * 2; break; } //--------------------------------------------------------------- for (Int32 ch = 0; ch < _channels; ch++) { ImageData[ch] = new Byte[_rows * bytesPerRow]; switch (ImageCompression) { case ImageCompression.Raw: reader.Read(ImageData[ch], 0, ImageData[ch].Length); break; case ImageCompression.Rle: { for (Int32 i = 0; i < _rows; i++) { Int32 rowIndex = i * _columns; RleHelper.DecodedRow(reader.BaseStream, ImageData[ch], rowIndex, bytesPerRow); } } break; } } #endregion //End LoadingFinalImage return(this); } //end Load()
public void FreeTexture(Texture2d tex) { ResourceIDs.Free(tex.Id); }
public IntPtr GenTexture() { return(ResourceIDs.Alloc(ResourceIdManager.EResourceType.Texture)); }
public Document(string a_sFilename) { FileStream stream = new FileStream(a_sFilename, FileMode.Open, FileAccess.Read); //stream. BinaryReverseReader reader = new BinaryReverseReader(stream); //, System.Text.Encoding.UTF8); //System.Text.Encoding.BigEndianUnicode); string signature = new string(reader.ReadChars(4)); if (signature != "8BPS") { return; } #region Header this.Version = reader.ReadUInt16(); if (Version != 1) { throw new Exception("Can not read .psd version " + Version); } byte[] buf = new byte[256]; reader.Read(buf, (int)reader.BaseStream.Position, 6); //6 bytes reserved this.Channels = reader.ReadInt16(); this.Rows = reader.ReadUInt32(); this.Columns = reader.ReadUInt32(); this.BitsPerPixel = (int)reader.ReadUInt16(); this.ColorMode = (ColorModes)reader.ReadInt16(); #endregion #region Palette uint nPaletteLength = reader.ReadUInt32(); if (nPaletteLength > 0) { this.ColorData = reader.ReadBytes((int)nPaletteLength); if (this.ColorMode == ColorModes.Duotone) { } else { } } #endregion #region ImageResource section uint nResLength = reader.ReadUInt32(); ResourceIDs resID = ResourceIDs.Undefined; if (nResLength > 0) { //read settings while (true) { long nBefore = reader.BaseStream.Position; string settingSignature = new string(reader.ReadChars(4)); if (settingSignature != "8BIM") { reader.BaseStream.Position = nBefore; //TODO: it SHOULD be 4 bytes back - but sometimes ReadChars(4) advances 5 positions. WHY?!? // reader.BaseStream.Position-=4; break; } ImageResource imgRes = new ImageResource(reader); resID = (ResourceIDs)imgRes.ID; switch (resID) //imgRes.ID) { case ResourceIDs.ResolutionInfo: this.ResolutionInfo = new Endogine.Serialization.Photoshop.ImageResources.ResolutionInfo(imgRes); break; case ResourceIDs.DisplayInfo: ImageResources.DisplayInfo displayInfo = new Endogine.Serialization.Photoshop.ImageResources.DisplayInfo(imgRes); break; case ResourceIDs.CopyrightInfo: ImageResources.CopyrightInfo copyright = new Endogine.Serialization.Photoshop.ImageResources.CopyrightInfo(imgRes); break; case ResourceIDs.Thumbnail1: case ResourceIDs.Thumbnail2: ImageResources.Thumbnail thumbnail = new Endogine.Serialization.Photoshop.ImageResources.Thumbnail(imgRes); break; case ResourceIDs.GlobalAngle: //m_nGlobalAngle = reader.ReadInt32(); break; case ResourceIDs.IndexedColorTableCount: this.NumColors = reader.ReadInt16(); break; case ResourceIDs.TransparentIndex: //m_nTransparentIndex = reader.ReadInt16(); break; case ResourceIDs.Slices: //Slices. What's that..? //Leftlong, Botmlong etc etc break; case ResourceIDs.XMLInfo: break; case ResourceIDs.Unknown: //Seems to be very common... break; } } } #endregion if (resID == ResourceIDs.Unknown4) { //it seems this one is } //reader.JumpToEvenNthByte(4); int nTotalLayersBytes = reader.ReadInt32(); long nAfterLayersDefinitions = reader.BaseStream.Position + nTotalLayersBytes; //TODO: ?? if (nTotalLayersBytes == 8) { stream.Position += nTotalLayersBytes; } uint nSize = reader.ReadUInt32(); long nLayersEndPos = reader.BaseStream.Position + nSize; short nNumLayers = reader.ReadInt16(); bool bSkipFirstAlpha = false; if (nNumLayers < 0) { bSkipFirstAlpha = true; nNumLayers = (short)-nNumLayers; } List <Layer> loadOrderLayers = new List <Layer>(); this._layers = new Dictionary <int, Layer>(); for (int nLayerNum = 0; nLayerNum < nNumLayers; nLayerNum++) { Layer layerInfo = new Layer(reader, this); if (this._layers.ContainsKey(layerInfo.LayerID)) { throw(new Exception("Duplicate layer IDs! " + layerInfo.LayerID.ToString())); } else { this._layers.Add(layerInfo.LayerID, layerInfo); } loadOrderLayers.Add(layerInfo); } //I have no idea what this is: // ushort nWhat = reader.ReadUInt16(); // reader.BaseStream.Position+=(long)this.Header.Rows*2*2; //this.Header.Channels; //*bitsperpixel for (int layerNum = 0; layerNum < nNumLayers; layerNum++) { Layer layer = (Layer)loadOrderLayers[layerNum]; layer.ReadPixels(reader); } reader.BaseStream.Position = nAfterLayersDefinitions; if (false) { //the big merged bitmap (which is how the photoshop document looked when it was saved) //TODO: read! //Bitmap bmp = null; //if (bmp != null) //{ // Sprite sp = new Sprite(); // MemberSpriteBitmap mb = new MemberSpriteBitmap(bmp); // sp.Member = mb; //} } reader.Close(); stream.Close(); }