/// <summary> /// indexer to get a Frame or to set a Frame /// </summary> public TextureFrame this[int index] { get { if (index < 0 || index >= _frames.Count) { return(null); } return(_frames[index]); } set { if (index < 0) { return; } if (_frames.Count <= index) { value.Disposed += new EventHandler(TextureFrame_Disposed); _frames.Add(value); } else { TextureFrame frame = _frames[index]; if (frame != value) { frame.Disposed -= new EventHandler(TextureFrame_Disposed); frame.SafeDispose(); value.Disposed += new EventHandler(TextureFrame_Disposed); _frames[index] = value; } } } }
/// <summary> /// Allocate the DirectX resources needed for rendering this GUIImage. /// </summary> public override void AllocResources() { //used for debugging leaks, comment in when needed-. /*_debugAllocResourcesCalled = true; _debugCachedTextureFileName = _textureFileNameTag; _debugCaller = ""; // System.Environment.StackTrace.ToString(); */ try { if (GUIGraphicsContext.DX9Device == null ) { return; } if (GUIGraphicsContext.DX9Device.Disposed) { return; } if (string.IsNullOrEmpty(_textureFileNameTag)) { return; } if (_registeredForEvent == false && _containsProperty) { GUIPropertyManager.OnPropertyChanged -= GUIPropertyManager_OnPropertyChanged; GUIPropertyManager.OnPropertyChanged += GUIPropertyManager_OnPropertyChanged; _registeredForEvent = true; } _propertyChanged = false; // reset animation BeginAnimation(); _listTextures = null; if (_blendableFileName != "" && GUITextureManager.GetPackedTexture(_blendableFileName, out _blendabletexUoff, out _blendabletexVoff, out _blendabletexUmax, out _blendabletexVmax, out _blendableTexWidth, out _blendableTexHeight, out _blendableTexture, out _packedBlendableTextureNo)) { _reCalculate = true; } // get the filename of the texture string fileName = _textureFileNameTag; if (_containsProperty) { fileName = _cachedTextureFileName = GUIPropertyManager.Parse(fileName); } if (GUITextureManager.GetPackedTexture(fileName, out _texUoff, out _texVoff, out _texUmax, out _texVmax, out _textureWidth, out _textureHeight, out _packedTexture, out _packedTextureNo)) { _reCalculate = true; _packedTexture.Disposing -= OnPackedTexturesDisposedEvent; _packedTexture.Disposing += OnPackedTexturesDisposedEvent; return; } // load the texture int frameCount; if (fileName.StartsWith("[")) { if (_memoryImageWidth != 0 && _memoryImageHeight != 0) { var bitmap = new Bitmap(_memoryImageWidth, _memoryImageHeight, PixelFormat.Format32bppArgb); Image memoryImage = bitmap; frameCount = GUITextureManager.LoadFromMemoryEx(memoryImage, fileName, m_dwColorKey, out _memoryImageTexture); } else { frameCount = GUITextureManager.LoadFromMemoryEx(_memoryImage, fileName, m_dwColorKey, out _memoryImageTexture); } } else { frameCount = GUITextureManager.Load(fileName, m_dwColorKey, m_iRenderWidth, _textureHeight, _shouldCache); } if (frameCount == 0) { return; // unable to load texture } // get each frame of the texture int iStartCopy = 0; TextureFrame[] saveList = null; if (_listTextures == null) { _listTextures = new TextureFrame[frameCount]; } else { int newLength = _listTextures.Length + frameCount; iStartCopy = _listTextures.Length; var newList = new TextureFrame[newLength]; saveList = new TextureFrame[_listTextures.Length]; _listTextures.CopyTo(saveList, 0); _listTextures.CopyTo(newList, 0); _listTextures = new TextureFrame[newLength]; newList.CopyTo(_listTextures, 0); } for (int i = 0; i < frameCount; i++) { _listTextures[i + iStartCopy] = GUITextureManager.GetTexture(fileName, i, out _textureWidth, out _textureHeight); if (_listTextures[i + iStartCopy] != null) { _listTextures[i + iStartCopy].Disposed += OnListTexturesDisposedEvent; } else { Log.Debug("GUIImage.AllocResources -> Filename={0} i={1} FrameCount={2}", fileName, i, frameCount); if (saveList != null) { _listTextures = new TextureFrame[saveList.Length]; saveList.CopyTo(_listTextures, 0); } else { UnsubscribeAndReleaseListTextures(); } _currentFrameNumber = 0; break; } } // Set state to render the image _reCalculate = true; base.AllocResources(); } catch (Exception e) { Log.Error(e); } finally { _allocated = true; } }
public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent) { string fileName = GetFileName(fileNameOrg); string cacheKey = fileName.ToLowerInvariant(); if (String.IsNullOrEmpty(fileName)) { return 0; } CachedTexture cached; if (_cacheTextures.TryGetValue(cacheKey, out cached)) { return cached.Frames; } string extension = Path.GetExtension(fileName).ToLowerInvariant(); if (extension == ".gif") { Image theImage = null; try { try { theImage = ImageFast.FromFile(fileName); } catch (FileNotFoundException) { Log.Warn("TextureManager: texture: {0} does not exist", fileName); return 0; } catch (Exception) { Log.Warn("TextureManager: Fast loading texture {0} failed using safer fallback", fileName); theImage = Image.FromFile(fileName); } if (theImage != null) { CachedTexture newCache = new CachedTexture(); newCache.Name = fileName; FrameDimension oDimension = new FrameDimension(theImage.FrameDimensionsList[0]); newCache.Frames = theImage.GetFrameCount(oDimension); int[] frameDelay = new int[newCache.Frames]; for (int num2 = 0; (num2 < newCache.Frames); ++num2) { frameDelay[num2] = 0; } // Getting Frame duration of an animated Gif image try { int num1 = 20736; PropertyItem item1 = theImage.GetPropertyItem(num1); if (item1 != null) { byte[] buffer1 = item1.Value; for (int num2 = 0; (num2 < newCache.Frames); ++num2) { frameDelay[num2] = (((buffer1[(num2 * 4)] + (256 * buffer1[((num2 * 4) + 1)])) + (65536 * buffer1[((num2 * 4) + 2)])) + (16777216 * buffer1[((num2 * 4) + 3)])); } } } catch (Exception) { } for (int i = 0; i < newCache.Frames; ++i) { theImage.SelectActiveFrame(oDimension, i); //load gif into texture using (MemoryStream stream = new MemoryStream()) { theImage.Save(stream, ImageFormat.Png); ImageInformation info2 = new ImageInformation(); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); Texture texture = TextureLoader.FromStream( GUIGraphicsContext.DX9Device, stream, 0, 0, //width/height 1, //mipslevels 0, //Usage.Dynamic, Format.A8R8G8B8, GUIGraphicsContext.GetTexturePoolType(), Filter.None, Filter.None, (int)lColorKey, ref info2); newCache.Width = info2.Width; newCache.Height = info2.Height; newCache[i] = new TextureFrame(fileName, texture, (frameDelay[i] / 5) * 50); } } theImage.SafeDispose(); theImage = null; newCache.Disposed += new EventHandler(cachedTexture_Disposed); if (persistent && !_persistentTextures.ContainsKey(cacheKey)) { _persistentTextures[cacheKey] = true; } _cacheTextures[cacheKey] = newCache; //Log.Info(" TextureManager:added:" + fileName + " total:" + _cache.Count + " mem left:" + GUIGraphicsContext.DX9Device.AvailableTextureMemory.ToString()); return newCache.Frames; } } catch (Exception ex) { Log.Error("TextureManager: exception loading texture {0}", fileName); Log.Error(ex); } return 0; } try { int width, height; if (MediaPortal.Util.Utils.FileExistsInCache(fileName)) { Texture dxtexture = LoadGraphic(fileName, lColorKey, iMaxWidth, iMaxHeight, out width, out height); if (dxtexture != null) { CachedTexture newCache = new CachedTexture(); newCache.Name = fileName; newCache.Frames = 1; newCache.Width = width; newCache.Height = height; newCache.Texture = new TextureFrame(fileName, dxtexture, 0); newCache.Disposed += new EventHandler(cachedTexture_Disposed); if (persistent && !_persistentTextures.ContainsKey(cacheKey)) { _persistentTextures[cacheKey] = true; } _cacheTextures[cacheKey] = newCache; return 1; } } } catch (Exception) { return 0; } return 0; }