1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using PixiEditor.Models.DataHolders;
- using PixiEditor.Models.ImageManipulation;
- using PixiEditor.Models.Layers;
- using PixiEditor.Models.Position;
- using Xunit;
- namespace PixiEditorTests.ModelsTests.ImageManipulationTests
- {
- public class BitmapUtilsTests
- {
- [Fact]
- public void TestBytesToWriteableBitmap()
- {
- int width = 10;
- int height = 10;
- Coordinates[] coloredPoints = {new Coordinates(0, 0), new Coordinates(3, 6), new Coordinates(9, 9)};
- WriteableBitmap bmp = BitmapFactory.New(width, height);
- for (int i = 0; i < coloredPoints.Length; i++)
- {
- bmp.SetPixel(coloredPoints[i].X, coloredPoints[i].Y, Colors.Green);
- }
- var byteArray = bmp.ToByteArray();
- var convertedBitmap = BitmapUtils.BytesToWriteableBitmap(width, height, byteArray);
- for (int i = 0; i < coloredPoints.Length; i++)
- {
- Assert.Equal(Colors.Green,convertedBitmap.GetPixel(coloredPoints[i].X, coloredPoints[i].Y));
- }
- }
- [Fact]
- public void TestThatCombineLayersReturnsCorrectBitmap()
- {
- Coordinates[] cords = {new Coordinates(0, 0), new Coordinates(1, 1)};
- Layer[] layers = {new Layer("test", 2,2), new Layer("test2", 2, 2) };
- layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
- layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
- var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
- Assert.Equal(Colors.Green, outputBitmap.GetPixel(0,0));
- Assert.Equal(Colors.Red, outputBitmap.GetPixel(1,1));
- }
- [Fact]
- public void TestThatCombineLayersReturnsCorrectBitmapWithSamePixels()
- {
- Coordinates[] cords = { new Coordinates(0, 0) };
- Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
- layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Green));
- layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Red));
- var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
- Assert.Equal(Colors.Red, outputBitmap.GetPixel(0, 0));
- }
- [Fact]
- public void TestThatGetPixelsForSelectionReturnsCorrectPixels()
- {
- Coordinates[] cords = { new Coordinates(0, 0),
- new Coordinates(1,1), new Coordinates(0, 1), new Coordinates(1, 0) };
- Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
- layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
- layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new [] { cords[1] }, Colors.Red));
- var output = BitmapUtils.GetPixelsForSelection(layers, cords);
- List<Color> colors = new List<Color>();
- foreach (var layerColor in output.ToArray())
- {
- foreach (var color in layerColor.Value)
- {
- colors.Add(color);
- }
- }
- Assert.Single(colors.Where(x=> x == Colors.Green));
- Assert.Single(colors.Where(x=> x == Colors.Red));
- Assert.Equal(6, colors.Count(x => x.A == 0)); //6 because layer is 4 pixels,
- //2 * 4 = 8, 2 other color pixels, so 8 - 2 = 6
- }
- }
- }
|