LayerBitmapContext.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using PixiEditor.Models.Layers;
  2. using System;
  3. using System.Windows.Media;
  4. using System.Windows.Media.Imaging;
  5. namespace PixiEditor.Helpers
  6. {
  7. class LayerBitmapContext : IDisposable
  8. {
  9. public static Color Premultiply(Color c)
  10. {
  11. float fraction = c.A / 255f;
  12. return Color.FromArgb(c.A, (byte)Math.Round(c.R * fraction), (byte)Math.Round(c.G * fraction), (byte)Math.Round(c.B * fraction));
  13. }
  14. private Layer layer;
  15. private BitmapContext ctx;
  16. public LayerBitmapContext(Layer layer)
  17. {
  18. this.layer = layer;
  19. //ctx = layer.LayerBitmap.GetBitmapContext();
  20. }
  21. public void Dispose() => ctx.Dispose();
  22. public bool IsPixelMatching(int x, int y, Color premult)
  23. {
  24. int realX = x - layer.OffsetX;
  25. int realY = y - layer.OffsetY;
  26. if (realX < 0 || realY < 0 || realX >= ctx.Width || realY >= ctx.Height)
  27. return premult.A == 0;
  28. unsafe
  29. {
  30. int pos = (realY * ctx.Width + realX) * 4;
  31. byte* pixels = (byte*)ctx.Pixels;
  32. return pixels[pos] == premult.B && pixels[pos + 1] == premult.G && pixels[pos + 2] == premult.R && pixels[pos + 3] == premult.A;
  33. }
  34. }
  35. }
  36. }