Browse Source

Added Width/Height impl to Pixmap

flabbet 2 years ago
parent
commit
0a1965921e

+ 23 - 27
src/ChunkyImageLib/Operations/OperationHelper.cs

@@ -23,35 +23,31 @@ public static class OperationHelper
     /// </summary>
     public unsafe static void ClampAlpha(DrawingSurface toModify, DrawingSurface toGetAlphaFrom)
     {
-        using (Pixmap map = toModify.PeekPixels())
+        using Pixmap map = toModify.PeekPixels();
+        using Pixmap refMap = toGetAlphaFrom.PeekPixels();
+        long* pixels = (long*)map.GetPixels();
+        long* refPixels = (long*)refMap.GetPixels();
+        int size = map.Width * map.Height;
+        if (map.Width != refMap.Width || map.Height != refMap.Height)
+            throw new ArgumentException("The surfaces must have the same size");
+
+        for (int i = 0; i < size; i++)
         {
-            using (Pixmap refMap = toGetAlphaFrom.PeekPixels())
+            long* offset = pixels + i;
+            long* refOffset = refPixels + i;
+            Half* alpha = (Half*)offset + 3;
+            Half* refAlpha = (Half*)refOffset + 3;
+            if (*refAlpha < *alpha)
             {
-                long* pixels = (long*)map.GetPixels();
-                long* refPixels = (long*)refMap.GetPixels();
-                int size = map.Width * map.Height;
-                if (map.Width != refMap.Width || map.Height != refMap.Height)
-                    throw new ArgumentException("The surfaces must have the same size");
-
-                for (int i = 0; i < size; i++)
-                {
-                    long* offset = pixels + i;
-                    long* refOffset = refPixels + i;
-                    Half* alpha = (Half*)offset + 3;
-                    Half* refAlpha = (Half*)refOffset + 3;
-                    if (*refAlpha < *alpha)
-                    {
-                        float a = (float)(*alpha);
-                        float r = (float)(*((Half*)offset)) / a;
-                        float g = (float)(*((Half*)offset + 1)) / a;
-                        float b = (float)(*((Half*)offset + 2)) / a;
-                        float newA = (float)(*refAlpha);
-                        Half newR = (Half)(r * newA);
-                        Half newG = (Half)(g * newA);
-                        Half newB = (Half)(b * newA);
-                        *offset = (*(ushort*)(&newR)) | ((long)*(ushort*)(&newG)) << 16 | ((long)*(ushort*)(&newB)) << 32 | ((long)*(ushort*)(refAlpha)) << 48;
-                    }
-                }
+                float a = (float)(*alpha);
+                float r = (float)(*((Half*)offset)) / a;
+                float g = (float)(*((Half*)offset + 1)) / a;
+                float b = (float)(*((Half*)offset + 2)) / a;
+                float newA = (float)(*refAlpha);
+                Half newR = (Half)(r * newA);
+                Half newG = (Half)(g * newA);
+                Half newB = (Half)(b * newA);
+                *offset = (*(ushort*)(&newR)) | ((long)*(ushort*)(&newG)) << 16 | ((long)*(ushort*)(&newB)) << 32 | ((long)*(ushort*)(refAlpha)) << 48;
             }
         }
     }

+ 2 - 0
src/PixiEditor.DrawingApi.Core/Bridge/NativeObjectsImpl/IPixmapImplementation.cs

@@ -10,4 +10,6 @@ public interface IPixmapImplementation
     public IntPtr GetPixels(IntPtr objectPointer);
     public Span<T> GetPixelSpan<T>(Pixmap pixmap) where T : unmanaged;
     public IntPtr Construct(IntPtr dataPtr, ImageInfo imgInfo);
+    public int GetWidth(Pixmap pixmap);
+    public int GetHeight(Pixmap pixmap);
 }

+ 9 - 2
src/PixiEditor.DrawingApi.Core/Surface/Pixmap.cs

@@ -15,8 +15,15 @@ public class Pixmap : NativeObject
         ObjectPointer = DrawingBackendApi.Current.PixmapImplementation.Construct(dataPtr, imgInfo);
     }
 
-    public int Width { get; set; }
-    public int Height { get; set; }
+    public int Width
+    {
+        get => DrawingBackendApi.Current.PixmapImplementation.GetWidth(this);
+    }
+
+    public int Height
+    {
+        get => DrawingBackendApi.Current.PixmapImplementation.GetHeight(this);
+    }
 
     public override void Dispose()
     {

+ 10 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaPixmapImplementation.cs

@@ -39,6 +39,16 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             return pixmap.Handle;
         }
 
+        public int GetWidth(Pixmap pixmap)
+        {
+            return ManagedInstances[pixmap.ObjectPointer].Width;
+        }
+
+        public int GetHeight(Pixmap pixmap)
+        {
+            return ManagedInstances[pixmap.ObjectPointer].Height;
+        }
+
         public Pixmap CreateFrom(SKPixmap pixmap)
         {
             ManagedInstances[pixmap.Handle] = pixmap;