BitmapUtilsTests.cs 3.9 KB

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