Layer.cs 2.3 KB

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