Layer.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using PixiEditor.Models.Position;
  2. using PixiEditor.Models.Tools;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows.Media.Imaging;
  6. namespace PixiEditor.Models.Layers
  7. {
  8. public class Layer : BasicLayer
  9. {
  10. private WriteableBitmap _layerBitmap;
  11. public string Name { get; set; }
  12. public bool IsVisible { get; set; } = true;
  13. public WriteableBitmap LayerBitmap
  14. {
  15. get => _layerBitmap;
  16. set
  17. {
  18. _layerBitmap = value;
  19. RaisePropertyChanged("LayerBitmap");
  20. }
  21. }
  22. public Layer(string name, int width, int height)
  23. {
  24. Name = name;
  25. Layer layer = LayerGenerator.Generate(width, height);
  26. LayerBitmap = layer.LayerBitmap;
  27. Width = width;
  28. Height = height;
  29. }
  30. public Layer(WriteableBitmap layerBitmap)
  31. {
  32. LayerBitmap = layerBitmap;
  33. Width = (int)layerBitmap.Width;
  34. Height = (int)layerBitmap.Height;
  35. }
  36. public void ApplyPixels(BitmapPixelChanges pixels)
  37. {
  38. LayerBitmap.Lock();
  39. foreach (var coords in pixels.ChangedPixels)
  40. {
  41. LayerBitmap.SetPixel(Math.Clamp(coords.Key.X, 0, Width - 1), Math.Clamp(coords.Key.Y, 0, Height - 1),
  42. coords.Value);
  43. }
  44. LayerBitmap.Unlock();
  45. }
  46. public Coordinates[] GetNonTransprarentPixels()
  47. {
  48. List<Coordinates> coordinates = new List<Coordinates>();
  49. for (int y = 0; y < Height; y++)
  50. {
  51. for (int x = 0; x < Width; x++)
  52. {
  53. if (LayerBitmap.GetPixeli(x, y) != 0)
  54. {
  55. coordinates.Add(new Coordinates(x, y));
  56. }
  57. }
  58. }
  59. return coordinates.ToArray();
  60. }
  61. public byte[] ConvertBitmapToBytes()
  62. {
  63. LayerBitmap.Lock();
  64. byte[] byteArray = LayerBitmap.ToByteArray();
  65. LayerBitmap.Unlock();
  66. return byteArray;
  67. }
  68. public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
  69. {
  70. bitmap.Lock();
  71. byte[] byteArray = bitmap.ToByteArray();
  72. bitmap.Unlock();
  73. return byteArray;
  74. }
  75. public void Resize(int newWidth, int newHeight)
  76. {
  77. LayerBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
  78. Height = newHeight;
  79. Width = newWidth;
  80. }
  81. }
  82. }