BitmapUtilsTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Imaging;
  6. using PixiEditor.Models.DataHolders;
  7. using PixiEditor.Models.ImageManipulation;
  8. using PixiEditor.Models.Layers;
  9. using PixiEditor.Models.Position;
  10. using Xunit;
  11. namespace PixiEditorTests.ModelsTests.ImageManipulationTests
  12. {
  13. public class BitmapUtilsTests
  14. {
  15. [Fact]
  16. public void TestBytesToWriteableBitmap()
  17. {
  18. int width = 10;
  19. int height = 10;
  20. Coordinates[] coloredPoints = { new Coordinates(0, 0), new Coordinates(3, 6), new Coordinates(9, 9) };
  21. WriteableBitmap bmp = BitmapFactory.New(width, height);
  22. for (int i = 0; i < coloredPoints.Length; i++)
  23. {
  24. bmp.SetPixel(coloredPoints[i].X, coloredPoints[i].Y, Colors.Green);
  25. }
  26. byte[] byteArray = bmp.ToByteArray();
  27. WriteableBitmap convertedBitmap = BitmapUtils.BytesToWriteableBitmap(width, height, byteArray);
  28. for (int i = 0; i < coloredPoints.Length; i++)
  29. {
  30. Assert.Equal(Colors.Green, convertedBitmap.GetPixel(coloredPoints[i].X, coloredPoints[i].Y));
  31. }
  32. }
  33. [Fact]
  34. public void TestThatCombineLayersReturnsCorrectBitmap()
  35. {
  36. Coordinates[] cords = { new Coordinates(0, 0), new Coordinates(1, 1) };
  37. Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
  38. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[0] }, Colors.Green));
  39. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
  40. WriteableBitmap outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
  41. Assert.Equal(Colors.Green, outputBitmap.GetPixel(0, 0));
  42. Assert.Equal(Colors.Red, outputBitmap.GetPixel(1, 1));
  43. }
  44. [Fact]
  45. public void TestThatCombineLayersReturnsCorrectBitmapWithSamePixels()
  46. {
  47. Coordinates[] cords = { new Coordinates(0, 0) };
  48. Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
  49. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Green));
  50. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Red));
  51. WriteableBitmap outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
  52. Assert.Equal(Colors.Red, outputBitmap.GetPixel(0, 0));
  53. }
  54. [Fact]
  55. public void TestThatGetPixelsForSelectionReturnsCorrectPixels()
  56. {
  57. Coordinates[] cords =
  58. {
  59. new Coordinates(0, 0),
  60. new Coordinates(1, 1), new Coordinates(0, 1), new Coordinates(1, 0)
  61. };
  62. Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
  63. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[0] }, Colors.Green));
  64. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
  65. Dictionary<Guid, Color[]> output = BitmapUtils.GetPixelsForSelection(layers, cords);
  66. List<Color> colors = new List<Color>();
  67. foreach (KeyValuePair<Guid, Color[]> layerColor in output.ToArray())
  68. {
  69. foreach (Color color in layerColor.Value)
  70. {
  71. colors.Add(color);
  72. }
  73. }
  74. Assert.Single(colors.Where(x => x == Colors.Green));
  75. Assert.Single(colors.Where(x => x == Colors.Red));
  76. Assert.Equal(6, colors.Count(x => x.A == 0)); // 6 because layer is 4 pixels,
  77. // 2 * 4 = 8, 2 other color pixels, so 8 - 2 = 6
  78. }
  79. }
  80. }