BitmapUtils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. float layerOpacity = layers[i].Opacity;
  38. for (int y = 0; y < finalBitmap.Height; y++)
  39. {
  40. for (int x = 0; x < finalBitmap.Width; x++)
  41. {
  42. Color color = layers[i].GetPixelWithOffset(x, y);
  43. if (i > 0 && ((color.A < 255 && color.A > 0) || (layerOpacity < 1f && layerOpacity > 0 && color.A > 0)))
  44. {
  45. var lastLayerPixel = finalBitmap.GetPixel(x, y);
  46. byte pixelA = (byte)(color.A * layerOpacity);
  47. byte r = (byte)((color.R * pixelA / 255) + (lastLayerPixel.R * lastLayerPixel.A * (255 - pixelA) / (255 * 255)));
  48. byte g = (byte)((color.G * pixelA / 255) + (lastLayerPixel.G * lastLayerPixel.A * (255 - pixelA) / (255 * 255)));
  49. byte b = (byte)((color.B * pixelA / 255) + (lastLayerPixel.B * lastLayerPixel.A * (255 - pixelA) / (255 * 255)));
  50. byte a = (byte)(pixelA + (lastLayerPixel.A * (255 - pixelA) / 255));
  51. color = Color.FromArgb(a, r, g, b);
  52. }
  53. else
  54. {
  55. color = Color.FromArgb(color.A, color.R, color.G, color.B);
  56. }
  57. if (color.A > 0)
  58. {
  59. finalBitmap.SetPixel(x, y, color);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return finalBitmap;
  66. }
  67. public static Dictionary<Layer, Color[]> GetPixelsForSelection(Layer[] layers, Coordinates[] selection)
  68. {
  69. Dictionary<Layer, Color[]> result = new Dictionary<Layer, Color[]>();
  70. for (int i = 0; i < layers.Length; i++)
  71. {
  72. Color[] pixels = new Color[selection.Length];
  73. using (layers[i].LayerBitmap.GetBitmapContext())
  74. {
  75. for (int j = 0; j < pixels.Length; j++)
  76. {
  77. Coordinates position = layers[i].GetRelativePosition(selection[j]);
  78. if (position.X < 0 || position.X > layers[i].Width - 1 || position.Y < 0 ||
  79. position.Y > layers[i].Height - 1)
  80. {
  81. continue;
  82. }
  83. pixels[j] = layers[i].GetPixel(position.X, position.Y);
  84. }
  85. }
  86. result[layers[i]] = pixels;
  87. }
  88. return result;
  89. }
  90. }
  91. }