public bool Get(string fileName, out float uoffs, out float voffs, out float umax, out float vmax, out int iWidth,
                        out int iHeight, out Texture tex, out int TextureNo)
        {
            uoffs     = voffs = umax = vmax = 0.0f;
            iWidth    = iHeight = 0;
            TextureNo = -1;
            tex       = null;
            if (_packedTextures == null)
            {
                return(false);
            }

            if (fileName.StartsWith(@"\"))
            {
                fileName = fileName.Remove(0, 1);
            }
            fileName = fileName.ToLower();
            if (fileName == string.Empty)
            {
                return(false);
            }
            int index = 0;

            foreach (PackedTexture bigOne in _packedTextures)
            {
                PackedTextureNode foundNode = bigOne.root.Get(fileName);
                if (foundNode != null)
                {
                    uoffs   = ((float)foundNode.Rect.Left + 1) / ((float)bigOne.root.Rect.Width);
                    voffs   = ((float)foundNode.Rect.Top + 1) / ((float)bigOne.root.Rect.Height);
                    umax    = ((float)foundNode.Rect.Width - 2) / ((float)bigOne.root.Rect.Width);
                    vmax    = ((float)foundNode.Rect.Height - 2) / ((float)bigOne.root.Rect.Height);
                    iWidth  = foundNode.Rect.Width - 2;
                    iHeight = foundNode.Rect.Height - 2;
                    if (bigOne.texture == null)
                    {
                        LoadPackedGraphics(index);
                    }

                    tex = bigOne.texture;
                    if (bigOne.textureNo == -1)
                    {
                        unsafe
                        {
                            IntPtr ptr = DirectShowUtil.GetUnmanagedTexture(bigOne.texture);
                            bigOne.textureNo = FontEngineAddTexture(ptr.ToInt32(), true, (void *)ptr.ToPointer());
                            Log.Info("TexturePacker: fontengine add texure:{0}", bigOne.textureNo);
                        }
                    }
                    TextureNo = bigOne.textureNo;
                    return(true);
                }
                index++;
            }
            return(false);
        }
        private bool AddBitmap(PackedTextureNode root, Image rootImage, string file, out bool dontAdd)
        {
            bool result = false;

            dontAdd = false;
            Image bmp = null;

            try
            {
                bmp = ImageFast.FromFile(file);
            }
            catch (Exception)
            {
                Log.Warn("TexturePacker: Fast loading of texture {0} failed - trying safe fallback now", file);
                bmp = Image.FromFile(file);
            }

            //if (bmp.Width >= GUIGraphicsContext.Width || bmp.Height >= GUIGraphicsContext.Height)
            if (bmp.Width > (_maxTextureHeight / 2) || bmp.Height > (_maxTextureWidth / 2))
            {
                Log.Warn("TexturePacker: Texture {0} is too large to be cached. Texture {1}x{2} - limit {3}x{4}",
                         file, bmp.Width, bmp.Height, _maxTextureHeight, _maxTextureWidth);
                dontAdd = true;
                return(false);
            }

            string skinName = String.Format(@"{0}\media", GUIGraphicsContext.Skin).ToLower();
            int    pos      = file.IndexOf(skinName);

            if (pos >= 0)
            {
                file = file.Remove(pos, skinName.Length);
            }

            string themeName = String.Format(@"{0}\themes", GUIGraphicsContext.Skin).ToLower();

            pos = file.IndexOf(themeName);
            if (pos >= 0)
            {
                file = file.Remove(pos, themeName.Length);
            }

            if (file.StartsWith(@"\"))
            {
                file = file.Remove(0, 1);
            }
            result = Add(root, bmp, rootImage, file);
            bmp.SafeDispose();
            bmp = null;

            return(result);
        }
Example #3
0
        private bool Add(PackedTextureNode root, Image img, Image rootImage, string fileName)
        {
            PackedTextureNode node = root.Insert(fileName, img, rootImage);

            if (node != null)
            {
                // Log.Debug("*** TexturePacker: Added {0} at ({1},{2}) {3}x{4}", fileName, node.Rect.X, node.Rect.Y, node.Rect.Width, node.Rect.Height);
                node.FileName = fileName;
                return(true);
            }
            // Log.Debug("*** TexturePacker: No room anymore to add: {0}", fileName);
            return(false);
        }
Example #4
0
 private PackedTextureNode GetInternal(string fileName)
 {
     if (FileName == fileName)
     {
         return(this);
     }
     if (ChildLeft != null)
     {
         PackedTextureNode node = ChildLeft.GetInternal(fileName);
         if (node != null)
         {
             return(node);
         }
     }
     return(ChildRight != null?ChildRight.GetInternal(fileName) : null);
 }
Example #5
0
    private bool AddBitmap(PackedTextureNode root, Image rootImage, string file, out bool dontAdd)
    {
      bool result = false;
      dontAdd = false;
      Image bmp = null;

      try
      {
        bmp = ImageFast.FromFile(file);
      }
      catch (Exception)
      {
        Log.Warn("TexturePacker: Fast loading of texture {0} failed - trying safe fallback now", file);
        bmp = Image.FromFile(file);
      }

      //if (bmp.Width >= GUIGraphicsContext.Width || bmp.Height >= GUIGraphicsContext.Height)
      if (bmp.Width > (_maxTextureHeight / 2) || bmp.Height > (_maxTextureWidth / 2))
      {
        Log.Warn("TexturePacker: Texture {0} is too large to be cached. Texture {1}x{2} - limit {3}x{4}",
                 file, bmp.Width, bmp.Height, _maxTextureHeight, _maxTextureWidth);
        dontAdd = true;
        return false;
      }

      string skinName = String.Format(@"{0}\media", GUIGraphicsContext.Skin).ToLower();
      int pos = file.IndexOf(skinName);
      if (pos >= 0)
      {
        file = file.Remove(pos, skinName.Length);
      }

      string themeName = String.Format(@"{0}\themes", GUIGraphicsContext.Skin).ToLower();
      pos = file.IndexOf(themeName);
      if (pos >= 0)
      {
        file = file.Remove(pos, themeName.Length);
      }

      if (file.StartsWith(@"\"))
      {
        file = file.Remove(0, 1);
      }
      result = Add(root, bmp, rootImage, file);
      bmp.SafeDispose();
      bmp = null;

      return result;
    }
Example #6
0
 private bool Add(PackedTextureNode root, Image img, Image rootImage, string fileName)
 {
   PackedTextureNode node = root.Insert(fileName, img, rootImage);
   if (node != null)
   {
     // Log.Debug("*** TexturePacker: Added {0} at ({1},{2}) {3}x{4}", fileName, node.Rect.X, node.Rect.Y, node.Rect.Width, node.Rect.Height);
     node.FileName = fileName;
     return true;
   }
   // Log.Debug("*** TexturePacker: No room anymore to add: {0}", fileName);
   return false;
 }
Example #7
0
      public PackedTextureNode Insert(string fileName, Image img, Image rootImage)
      {
        //Log.Info("rect:({0},{1}) {2}x{3} img:{4}x{5} filename:{6} left:{7} right:{8}",
        //				Rect.Left,Rect.Top,Rect.Width,Rect.Height,img.Width,img.Height,FileName, ChildLeft,ChildRight);
        if (ChildLeft != null && ChildRight != null)
        {
          PackedTextureNode node = ChildLeft.Insert(fileName, img, rootImage);
          if (node != null)
          {
            return node;
          }
          return ChildRight.Insert(fileName, img, rootImage);
        }
        //(if there's already a lightmap here, return)
        if (FileName != null && FileName.Length > 0)
        {
          return null;
        }

        //(if we're too small, return)
        if ((img.Width + 2) > Rect.Width || (img.Height + 2) > Rect.Height)
        {
          return null;
        }
        //(if we're just right, accept)
        if ((img.Width + 2) == Rect.Width && (img.Height + 2) == Rect.Height)
        {
          using (Graphics g = Graphics.FromImage(rootImage))
          {
            FileName = fileName;
            g.CompositingQuality = CompositingQuality.HighQuality; // Thumbs.Compositing;
            g.CompositingMode = CompositingMode.SourceCopy;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic; // Thumbs.Interpolation;
            g.SmoothingMode = SmoothingMode.HighQuality; // Thumbs.Smoothing;
            // draw oversized image first
            g.DrawImage(img, Rect.Left, Rect.Top, Rect.Width, Rect.Height);
            // draw original image ontop of oversized imagealt
            g.DrawImage(img, Rect.Left + 1, Rect.Top + 1, Rect.Width - 2, Rect.Height - 2);
          }
          return this;
        }

        if (Rect.Width <= 2 || Rect.Height <= 2)
        {
          return null;
        }
        //(otherwise, gotta split this node and create some kids)
        ChildLeft = new PackedTextureNode();
        ChildRight = new PackedTextureNode();

        //(decide which way to split)
        int dw = Rect.Width - (img.Width + 2);
        int dh = Rect.Height - (img.Height + 2);

        if (dw > dh)
        {
          ChildLeft.Rect = new Rectangle(Rect.Left, Rect.Top, (img.Width + 2), Rect.Height);
          ChildRight.Rect = new Rectangle(Rect.Left + (img.Width + 2), Rect.Top, Rect.Width - (img.Width + 2),
                                          Rect.Height);
        }
        else
        {
          ChildLeft.Rect = new Rectangle(Rect.Left, Rect.Top, Rect.Width, (img.Height + 2));
          ChildRight.Rect = new Rectangle(Rect.Left, Rect.Top + (img.Height + 2), Rect.Width,
                                          Rect.Height - (img.Height + 2));
        }
        //(insert into first child we created)
        PackedTextureNode newNode = ChildLeft.Insert(fileName, img, rootImage);
        if (newNode != null)
        {
          return newNode;
        }
        return ChildRight.Insert(fileName, img, rootImage);
      }
Example #8
0
            public PackedTextureNode Insert(string fileName, Image img, Image rootImage)
            {
                if (ChildLeft != null && ChildRight != null)
                {
                    PackedTextureNode node = ChildLeft.Insert(fileName, img, rootImage);
                    return(node ?? ChildRight.Insert(fileName, img, rootImage));
                }
                //(if there's already a lightmap here, return)
                if (!string.IsNullOrEmpty(FileName))
                {
                    return(null);
                }

                //(if we're too small, return)
                if ((img.Width + 2) > Rect.Width || (img.Height + 2) > Rect.Height)
                {
                    return(null);
                }
                //(if we're just right, accept)
                if ((img.Width + 2) == Rect.Width && (img.Height + 2) == Rect.Height)
                {
                    using (Graphics g = Graphics.FromImage(rootImage))
                    {
                        FileName             = fileName;
                        g.CompositingQuality = CompositingQuality.HighQuality;       // Thumbs.Compositing;
                        g.CompositingMode    = CompositingMode.SourceCopy;
                        g.InterpolationMode  = InterpolationMode.HighQualityBicubic; // Thumbs.Interpolation;
                        g.SmoothingMode      = SmoothingMode.HighQuality;            // Thumbs.Smoothing;
                        // draw oversized image first
                        g.DrawImage(img, Rect.Left, Rect.Top, Rect.Width, Rect.Height);
                        // draw original image ontop of oversized imagealt
                        g.DrawImage(img, Rect.Left + 1, Rect.Top + 1, Rect.Width - 2, Rect.Height - 2);
                    }
                    return(this);
                }

                if (Rect.Width <= 2 || Rect.Height <= 2)
                {
                    return(null);
                }
                //(otherwise, gotta split this node and create some kids)
                ChildLeft  = new PackedTextureNode();
                ChildRight = new PackedTextureNode();

                //(decide which way to split)
                int dw = Rect.Width - (img.Width + 2);
                int dh = Rect.Height - (img.Height + 2);

                if (dw > dh)
                {
                    ChildLeft.Rect  = new Rectangle(Rect.Left, Rect.Top, (img.Width + 2), Rect.Height);
                    ChildRight.Rect = new Rectangle(Rect.Left + (img.Width + 2), Rect.Top, Rect.Width - (img.Width + 2),
                                                    Rect.Height);
                }
                else
                {
                    ChildLeft.Rect  = new Rectangle(Rect.Left, Rect.Top, Rect.Width, (img.Height + 2));
                    ChildRight.Rect = new Rectangle(Rect.Left, Rect.Top + (img.Height + 2), Rect.Width,
                                                    Rect.Height - (img.Height + 2));
                }
                //(insert into first child we created)
                PackedTextureNode newNode = ChildLeft.Insert(fileName, img, rootImage);

                return(newNode ?? ChildRight.Insert(fileName, img, rootImage));
            }
Example #9
0
        public bool Get(string fileName, out float uoffs, out float voffs, out float umax, out float vmax, out int iWidth,
                        out int iHeight, out Texture tex, out int textureNo)
        {
            uoffs     = voffs = umax = vmax = 0.0f;
            iWidth    = iHeight = 0;
            textureNo = -1;
            tex       = null;
            if (_packedTextures == null)
            {
                return(false);
            }

            if (fileName.StartsWith(@"\"))
            {
                fileName = fileName.Remove(0, 1);
            }
            fileName = fileName.ToLowerInvariant();
            if (fileName == string.Empty)
            {
                return(false);
            }

            int               index     = 0;
            PackedTexture     bigOne    = null;
            PackedTextureNode foundNode = null;

            // Look for textures first in the current theme location.  Theme textures override default textures.
            // If the default theme is selected then avoid looking through the theme.
            if (!GUIThemeManager.CurrentThemeIsDefault)
            {
                string skinThemeTexturePath = GUIThemeManager.CurrentTheme + @"\media\";
                skinThemeTexturePath = skinThemeTexturePath.ToLowerInvariant();

                // If a theme texture exists but was not able to be packed then avoid trying to unpack the texture all together.  This prevents
                // a base skin texture from being unpacked and returned when the base texture could be packed but the overriding theme texture
                // could not be packed.
                if (!IsTexturePacked(skinThemeTexturePath + fileName))
                {
                    return(false);
                }

                foreach (PackedTexture texture in _packedTextures)
                {
                    if ((foundNode = texture.root.Get(skinThemeTexturePath + fileName)) != null)
                    {
                        bigOne = texture;
                        break;
                    }
                    index++;
                }
            }

            // No theme texture was found.  Check the default skin location.
            if (foundNode == null)
            {
                index = 0;
                foreach (PackedTexture texture in _packedTextures)
                {
                    if ((foundNode = texture.root.Get(fileName)) != null)
                    {
                        bigOne = texture;
                        break;
                    }
                    index++;
                }
            }

            if (foundNode != null)
            {
                uoffs   = (float)(foundNode.Rect.Left + 1) / bigOne.root.Rect.Width;
                voffs   = (float)(foundNode.Rect.Top + 1) / bigOne.root.Rect.Height;
                umax    = (float)(foundNode.Rect.Width - 2) / bigOne.root.Rect.Width;
                vmax    = (float)(foundNode.Rect.Height - 2) / bigOne.root.Rect.Height;
                iWidth  = foundNode.Rect.Width - 2;
                iHeight = foundNode.Rect.Height - 2;
                if (bigOne.texture == null)
                {
                    LoadPackedGraphics(index);
                }

                tex = bigOne.texture;
                if (bigOne.textureNo == -1)
                {
                    unsafe
                    {
                        IntPtr ptr = DirectShowUtil.GetUnmanagedTexture(bigOne.texture);
                        bigOne.textureNo = DXNative.FontEngineAddTexture(ptr.ToInt32(), true, ptr.ToPointer());
                        Log.Info("TexturePacker: fontengine add texure:{0}", bigOne.textureNo);
                    }
                }
                textureNo = bigOne.textureNo;
                return(true);
            }
            return(false);
        }