BitmapUtils.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using System.Windows.Media.Imaging;
  3. using PixiEditor.Models.Layers;
  4. using PixiEditor.Models.Position;
  5. using Color = System.Windows.Media.Color;
  6. namespace PixiEditor.Models.ImageManipulation
  7. {
  8. public static class BitmapUtils
  9. {
  10. /// <summary>
  11. /// Converts pixel bytes to WriteableBitmap.
  12. /// </summary>
  13. /// <param name="currentBitmapWidth">Width of bitmap.</param>
  14. /// <param name="currentBitmapHeight">Height of bitmap.</param>
  15. /// <param name="byteArray">Bitmap byte array.</param>
  16. /// <returns>WriteableBitmap.</returns>
  17. public static WriteableBitmap BytesToWriteableBitmap(int currentBitmapWidth, int currentBitmapHeight, byte[] byteArray)
  18. {
  19. WriteableBitmap bitmap = BitmapFactory.New(currentBitmapWidth, currentBitmapHeight);
  20. bitmap.FromByteArray(byteArray);
  21. return bitmap;
  22. }
  23. /// <summary>
  24. /// Converts layers bitmaps into one bitmap.
  25. /// </summary>
  26. /// <param name="layers">Layers to combine.</param>
  27. /// <param name="width">Width of final bitmap.</param>
  28. /// <param name="height">Height of final bitmap.</param>
  29. /// <returns>WriteableBitmap of layered bitmaps.</returns>
  30. public static WriteableBitmap CombineLayers(Layer[] layers, int width, int height)
  31. {
  32. WriteableBitmap finalBitmap = BitmapFactory.New(width, height);
  33. using (finalBitmap.GetBitmapContext())
  34. {
  35. for (int i = 0; i < layers.Length; i++)
  36. {
  37. for (int y = 0; y < finalBitmap.Height; y++)
  38. {
  39. for (int x = 0; x < finalBitmap.Width; x++)
  40. {
  41. Color color = layers[i].GetPixelWithOffset(x, y);
  42. float layerOpacity = layers[i].Opacity;
  43. if (i > 0 && ((color.A < 255 && color.A > 0) || (layerOpacity < 1f && layerOpacity > 0)))
  44. {
  45. var lastLayerPixel = finalBitmap.GetPixel(x, y);
  46. byte lastPixelA = (byte)(lastLayerPixel.A * layers[i - 1].Opacity);
  47. byte pixelA = (byte)(color.A * layerOpacity);
  48. byte r = (byte)((color.R * pixelA / 255) + (lastLayerPixel.R * lastPixelA * (255 - pixelA) / (255 * 255)));
  49. byte g = (byte)((color.G * pixelA / 255) + (lastLayerPixel.G * lastPixelA * (255 - pixelA) / (255 * 255)));
  50. byte b = (byte)((color.B * pixelA / 255) + (lastLayerPixel.B * lastPixelA * (255 - pixelA) / (255 * 255)));
  51. byte a = (byte)(pixelA + (lastPixelA * (255 - pixelA) / 255));
  52. color = Color.FromArgb(a, r, g, b);
  53. }
  54. else
  55. {
  56. color = Color.FromArgb((byte)(color.A * layers[i].Opacity), color.R, color.G, color.B);
  57. }
  58. if (color.A != 0 || color.R != 0 || color.B != 0 || color.G != 0)
  59. {
  60. finalBitmap.SetPixel(x, y, color);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. return finalBitmap;
  67. }
  68. public static Dictionary<Layer, Color[]> GetPixelsForSelection(Layer[] layers, Coordinates[] selection)
  69. {
  70. Dictionary<Layer, Color[]> result = new Dictionary<Layer, Color[]>();
  71. for (int i = 0; i < layers.Length; i++)
  72. {
  73. Color[] pixels = new Color[selection.Length];
  74. using (layers[i].LayerBitmap.GetBitmapContext())
  75. {
  76. for (int j = 0; j < pixels.Length; j++)
  77. {
  78. Coordinates position = layers[i].GetRelativePosition(selection[j]);
  79. if (position.X < 0 || position.X > layers[i].Width - 1 || position.Y < 0 ||
  80. position.Y > layers[i].Height - 1)
  81. {
  82. continue;
  83. }
  84. pixels[j] = layers[i].GetPixel(position.X, position.Y);
  85. }
  86. }
  87. result[layers[i]] = pixels;
  88. }
  89. return result;
  90. }
  91. }
  92. }