summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Content
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-11-19 13:48:19 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-11-19 13:48:19 -0500
commit593723b7940ba72a786fc4c7366c56f9813d977b (patch)
tree4d23fbef5bc5a20115f10ca04ae3379df78cc8e1 /src/SMAPI/Framework/Content
parent4f28ea33bd7cc65485402c5e85259083e86b49e1 (diff)
parent3dc27a5681dcfc4ae30e95570d9966f2e14a4dd7 (diff)
downloadSMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.tar.gz
SMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.tar.bz2
SMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Content')
-rw-r--r--src/SMAPI/Framework/Content/AssetDataForImage.cs41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/SMAPI/Framework/Content/AssetDataForImage.cs b/src/SMAPI/Framework/Content/AssetDataForImage.cs
index 5c7b87de..f970762a 100644
--- a/src/SMAPI/Framework/Content/AssetDataForImage.cs
+++ b/src/SMAPI/Framework/Content/AssetDataForImage.cs
@@ -8,6 +8,14 @@ namespace StardewModdingAPI.Framework.Content
internal class AssetDataForImage : AssetData<Texture2D>, IAssetDataForImage
{
/*********
+ ** Properties
+ *********/
+ /// <summary>The minimum value to consider non-transparent.</summary>
+ /// <remarks>On Linux/Mac, fully transparent pixels may have an alpha up to 4 for some reason.</remarks>
+ private const byte MinOpacity = 5;
+
+
+ /*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
@@ -53,13 +61,40 @@ namespace StardewModdingAPI.Framework.Content
// merge data in overlay mode
if (patchMode == PatchMode.Overlay)
{
+ // get target data
+ Color[] targetData = new Color[pixelCount];
+ target.GetData(0, targetArea, targetData, 0, pixelCount);
+
+ // merge pixels
Color[] newData = new Color[targetArea.Value.Width * targetArea.Value.Height];
target.GetData(0, targetArea, newData, 0, newData.Length);
for (int i = 0; i < sourceData.Length; i++)
{
- Color pixel = sourceData[i];
- if (pixel.A > 4) // not transparent (note: on Linux/Mac, fully transparent pixels may have an alpha up to 4 for some reason)
- newData[i] = pixel;
+ Color above = sourceData[i];
+ Color below = targetData[i];
+
+ // shortcut transparency
+ if (above.A < AssetDataForImage.MinOpacity)
+ continue;
+ if (below.A < AssetDataForImage.MinOpacity)
+ {
+ newData[i] = above;
+ continue;
+ }
+
+ // merge pixels
+ // This performs a conventional alpha blend for the pixels, which are already
+ // premultiplied by the content pipeline. The formula is derived from
+ // https://blogs.msdn.microsoft.com/shawnhar/2009/11/06/premultiplied-alpha/.
+ // Note: don't use named arguments here since they're different between
+ // Linux/Mac and Windows.
+ float alphaBelow = 1 - (above.A / 255f);
+ newData[i] = new Color(
+ (int)(above.R + (below.R * alphaBelow)), // r
+ (int)(above.G + (below.G * alphaBelow)), // g
+ (int)(above.B + (below.B * alphaBelow)), // b
+ Math.Max(above.A, below.A) // a
+ );
}
sourceData = newData;
}