Files
modernkeepasslib/ModernKeePassLib/PwCustomIcon.cs

129 lines
3.6 KiB
C#
Raw Normal View History

2019-07-25 16:39:43 +02:00
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2020 Dominik Reichl <dominik.reichl@t-online.de>
2019-07-25 16:39:43 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if ModernKeePassLib
using SixLabors.ImageSharp;
2019-07-25 16:39:43 +02:00
#else
using System.Drawing;
2019-07-25 16:39:43 +02:00
#endif
using ModernKeePassLib.Utility;
using SixLabors.ImageSharp.PixelFormats;
2019-07-25 16:39:43 +02:00
namespace ModernKeePassLib
{
/// <summary>
/// Custom icon. <c>PwCustomIcon</c> objects are immutable.
/// </summary>
public sealed class PwCustomIcon
{
private readonly PwUuid m_pwUuid;
private readonly byte[] m_pbImageDataPng;
private readonly Image<Rgba32> m_imgOrg;
private Dictionary<long, Image<Rgba32>> m_dImageCache = new Dictionary<long, Image<Rgba32>>();
2019-07-25 16:39:43 +02:00
// Recommended maximum sizes, not obligatory
internal const int MaxWidth = 128;
internal const int MaxHeight = 128;
public PwUuid Uuid
{
get { return m_pwUuid; }
}
public byte[] ImageDataPng
{
get { return m_pbImageDataPng; }
}
[Obsolete("Use GetImage instead.")]
public Image<Rgba32> Image
2019-07-25 16:39:43 +02:00
{
#if (!KeePassLibSD && !KeePassUAP)
get { return GetImage(16, 16); } // Backward compatibility
#else
get { return GetImage(); } // Backward compatibility
#endif
}
public PwCustomIcon(PwUuid pwUuid, byte[] pbImageDataPng)
{
Debug.Assert(pwUuid != null);
if(pwUuid == null) throw new ArgumentNullException("pwUuid");
Debug.Assert(!pwUuid.Equals(PwUuid.Zero));
if(pwUuid.Equals(PwUuid.Zero)) throw new ArgumentException("pwUuid == 0.");
Debug.Assert(pbImageDataPng != null);
if(pbImageDataPng == null) throw new ArgumentNullException("pbImageDataPng");
m_pwUuid = pwUuid;
m_pbImageDataPng = pbImageDataPng;
// MemoryStream ms = new MemoryStream(m_pbImageDataPng, false);
// m_imgOrg = Image.FromStream(ms);
// ms.Close();
try { m_imgOrg = GfxUtil.LoadImage(m_pbImageDataPng); }
catch(Exception) { Debug.Assert(false); m_imgOrg = null; }
if(m_imgOrg != null)
m_dImageCache[GetID((int)m_imgOrg.Width, (int)m_imgOrg.Height)] =
m_imgOrg;
}
private static long GetID(int w, int h)
{
return (((long)w << 32) ^ (long)h);
}
/// <summary>
/// Get the icon as an <c>Image</c> (original size).
/// </summary>
public Image<Rgba32> GetImage()
2019-07-25 16:39:43 +02:00
{
return m_imgOrg;
}
#if (!KeePassLibSD && !KeePassUAP)
/// <summary>
/// Get the icon as an <c>Image</c> (with the specified size).
/// </summary>
/// <param name="w">Width of the returned image.</param>
/// <param name="h">Height of the returned image.</param>
public Image<Rgba32> GetImage(int w, int h)
2019-07-25 16:39:43 +02:00
{
if(w < 0) { Debug.Assert(false); return m_imgOrg; }
if(h < 0) { Debug.Assert(false); return m_imgOrg; }
if(m_imgOrg == null) return null;
long lID = GetID(w, h);
Image<Rgba32> img;
2019-07-25 16:39:43 +02:00
if(m_dImageCache.TryGetValue(lID, out img)) return img;
img = GfxUtil.ScaleImage(m_imgOrg, w, h, ScaleTransformFlags.UIIcon);
m_dImageCache[lID] = img;
return img;
}
#endif
}
}