1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using PixiEditor.Models.Position;
- using PixiEditor.Models.Tools;
- using System;
- using System.Collections.Generic;
- using System.Windows.Media.Imaging;
- namespace PixiEditor.Models.Layers
- {
- public class Layer : BasicLayer
- {
- private WriteableBitmap _layerBitmap;
- public string Name { get; set; }
- public bool IsVisible { get; set; } = true;
- public WriteableBitmap LayerBitmap
- {
- get => _layerBitmap;
- set
- {
- _layerBitmap = value;
- RaisePropertyChanged("LayerBitmap");
- }
- }
- public Layer(string name, int width, int height)
- {
- Name = name;
- Layer layer = LayerGenerator.Generate(width, height);
- LayerBitmap = layer.LayerBitmap;
- Width = width;
- Height = height;
- }
- public Layer(WriteableBitmap layerBitmap)
- {
- LayerBitmap = layerBitmap;
- Width = (int)layerBitmap.Width;
- Height = (int)layerBitmap.Height;
- }
- public void ApplyPixels(BitmapPixelChanges pixels)
- {
- LayerBitmap.Lock();
- foreach (var coords in pixels.ChangedPixels)
- {
- LayerBitmap.SetPixel(Math.Clamp(coords.Key.X, 0, Width - 1), Math.Clamp(coords.Key.Y, 0, Height - 1),
- coords.Value);
- }
- LayerBitmap.Unlock();
- }
- public Coordinates[] GetNonTransprarentPixels()
- {
- List<Coordinates> coordinates = new List<Coordinates>();
- for (int y = 0; y < Height; y++)
- {
- for (int x = 0; x < Width; x++)
- {
- if (LayerBitmap.GetPixeli(x, y) != 0)
- {
- coordinates.Add(new Coordinates(x, y));
- }
- }
- }
- return coordinates.ToArray();
- }
- public byte[] ConvertBitmapToBytes()
- {
- LayerBitmap.Lock();
- byte[] byteArray = LayerBitmap.ToByteArray();
- LayerBitmap.Unlock();
- return byteArray;
- }
- public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
- {
- bitmap.Lock();
- byte[] byteArray = bitmap.ToByteArray();
- bitmap.Unlock();
- return byteArray;
- }
- public void Resize(int newWidth, int newHeight)
- {
- LayerBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
- Height = newHeight;
- Width = newWidth;
- }
- }
- }
|