Преглед изворни кода

Merge branch 'master' into development

Krzysztof Krysiński пре 3 недеља
родитељ
комит
a1b33ece95

+ 27 - 25
src/ChunkyImageLib/DataHolders/ColorBounds.cs

@@ -21,27 +21,9 @@ public struct ColorBounds
 
     public float UpperA { get; set; }
 
-    public ColorBounds(Color color, double tolerance = 0)
+    public ColorBounds(ColorF color, double tolerance = 0)
     {
-        static (float lower, float upper) FindInclusiveBoundaryPremul(byte channel, float alpha)
-        {
-            float subHalf = channel > 0 ? channel - 1f : channel;
-            float addHalf = channel < 255 ? channel + 1f : channel;
-            
-            var lower = subHalf * alpha / 255f;
-            var upper = addHalf * alpha / 255f;
-            
-            return (lower, upper);
-        }
-
-        static (float lower, float upper) FindInclusiveBoundary(byte channel)
-        {
-            float subHalf = channel > 0 ? channel - .5f : channel;
-            float addHalf = channel < 255 ? channel + .5f : channel;
-            return (subHalf / 255f, addHalf / 255f);
-        }
-
-        float a = color.A / 255f;
+        float a = color.A;
 
         (LowerR, UpperR) = FindInclusiveBoundaryPremul(color.R, a);
         LowerR -= (float)tolerance;
@@ -60,6 +42,26 @@ public struct ColorBounds
         UpperA += (float)tolerance;
     }
 
+    private static (float lower, float upper) FindInclusiveBoundaryPremul(float channel, float alpha)
+    {
+        var step = 1f / 255f;
+        float subHalf = channel > 0 ? channel - step : channel;
+        float addHalf = channel < 1 ? channel + step : channel;
+
+        var lower = subHalf * alpha;
+        var upper = addHalf * alpha;
+
+        return (lower, upper);
+    }
+
+    private static (float lower, float upper) FindInclusiveBoundary(float channel)
+    {
+        float halfStep = 0.5f / 255f;
+        float subHalf = channel > 0 ? channel - halfStep : channel;
+        float addHalf = channel < 1 ? channel + halfStep : channel;
+        return (subHalf, addHalf);
+    }
+
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public unsafe bool IsWithinBounds(Half* pixel)
     {
@@ -78,12 +80,12 @@ public struct ColorBounds
         return true;
     }
 
-    public bool IsWithinBounds(Color toCompare)
+    public bool IsWithinBounds(ColorF toCompare)
     {
-        float a = toCompare.A / 255f;
-        float r = (toCompare.R / 255f) * a;
-        float g = (toCompare.G / 255f) * a;
-        float b = (toCompare.B / 255f) * a;
+        float a = toCompare.A;
+        float r = (toCompare.R) * a;
+        float g = (toCompare.G) * a;
+        float b = (toCompare.B) * a;
         
         if (r < LowerR || r > UpperR)
             return false;

+ 1 - 1
src/Drawie

@@ -1 +1 @@
-Subproject commit 081ed88104da2c20e7356cdf34400c1e35c71c63
+Subproject commit b3b3a342c4b9d188de984ecefd4a9f8d020d6d4c

+ 8 - 7
src/PixiEditor.ChangeableDocument/Changes/Drawing/FloodFill/FloodFillHelper.cs

@@ -58,20 +58,20 @@ public static class FloodFillHelper
         VecI imageSizeInChunks = (VecI)(document.Size / (double)chunkSize).Ceiling();
         VecI initPosOnChunk = startingPos - initChunkPos * chunkSize;
         var chunkAtPos = cache.GetChunk(initChunkPos);
-        Color colorToReplace = chunkAtPos.Match(
-            (Chunk chunk) => chunk.Surface.GetRawPixel(initPosOnChunk),
+        ColorF colorToReplace = chunkAtPos.Match(
+            (Chunk chunk) => chunk.Surface.GetRawPixelPrecise(initPosOnChunk),
             static (EmptyChunk _) => Colors.Transparent
         );
 
         ulong uLongColor = drawingColor.ToULong();
-        Color colorSpaceCorrectedColor = drawingColor;
+        ColorF colorSpaceCorrectedColor = drawingColor;
         if (!document.ProcessingColorSpace.IsSrgb)
         {
             var srgbTransform = ColorSpace.CreateSrgb().GetTransformFunction();
 
             var fixedColor = drawingColor.TransformColor(srgbTransform);
             uLongColor = fixedColor.ToULong();
-            colorSpaceCorrectedColor = (Color)fixedColor;
+            colorSpaceCorrectedColor = fixedColor;
         }
 
         if ((colorSpaceCorrectedColor.A == 0) || colorToReplace == colorSpaceCorrectedColor)
@@ -192,15 +192,16 @@ public static class FloodFillHelper
         VecI chunkPos,
         int chunkSize,
         ulong colorBits,
-        Color color,
+        ColorF color,
         VecI pos,
         ColorBounds bounds,
         bool checkFirstPixel)
     {
+        var rawPixelRef = referenceChunk.Surface.GetRawPixelPrecise(pos);
         // color should be a fixed color
-        if (referenceChunk.Surface.GetRawPixel(pos) == color || drawingChunk.Surface.GetRawPixel(pos) == color)
+        if ((Color)rawPixelRef == (Color)color || (Color)drawingChunk.Surface.GetRawPixelPrecise(pos) == (Color)color)
             return null;
-        if (checkFirstPixel && !bounds.IsWithinBounds(referenceChunk.Surface.GetRawPixel(pos)))
+        if (checkFirstPixel && !bounds.IsWithinBounds(rawPixelRef))
             return null;
         
         if(!SelectionIntersectsChunk(selection, chunkPos, chunkSize))

+ 3 - 3
src/PixiEditor.ChangeableDocument/Changes/Selection/MagicWand/MagicWandHelper.cs

@@ -125,8 +125,8 @@ internal class MagicWandHelper
         VecI initPosOnChunk = startingPos - initChunkPos * chunkSize;
 
 
-        Color colorToReplace = cache.GetChunk(initChunkPos).Match(
-            (Chunk chunk) => chunk.Surface.GetRawPixel(initPosOnChunk),
+        ColorF colorToReplace = cache.GetChunk(initChunkPos).Match(
+            (Chunk chunk) => chunk.Surface.GetRawPixelPrecise(initPosOnChunk),
             static (EmptyChunk _) => Colors.Transparent
         );
 
@@ -257,7 +257,7 @@ internal class MagicWandHelper
         VecI pos,
         ColorBounds bounds, Lines lines)
     {
-        if (!bounds.IsWithinBounds(referenceChunk.Surface.GetRawPixel(pos)))
+        if (!bounds.IsWithinBounds(referenceChunk.Surface.GetRawPixelPrecise(pos)))
         {
             return null;
         }