TextureScale.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Only works on ARGB32, RGB24 and Alpha8 textures that are marked readable
  2. using System.Threading;
  3. using UnityEngine;
  4. namespace Imagine.WebAR{
  5. public class TextureScale
  6. {
  7. public class ThreadData
  8. {
  9. public int start;
  10. public int end;
  11. public ThreadData (int s, int e) {
  12. start = s;
  13. end = e;
  14. }
  15. }
  16. private static Color[] texColors;
  17. private static Color[] newColors;
  18. private static int w;
  19. private static float ratioX;
  20. private static float ratioY;
  21. private static int w2;
  22. private static int finishCount;
  23. private static Mutex mutex;
  24. public static void Point (Texture2D tex, int newWidth, int newHeight)
  25. {
  26. ThreadedScale (tex, newWidth, newHeight, false);
  27. }
  28. public static void Bilinear (Texture2D tex, int newWidth, int newHeight)
  29. {
  30. ThreadedScale (tex, newWidth, newHeight, true);
  31. }
  32. private static void ThreadedScale (Texture2D tex, int newWidth, int newHeight, bool useBilinear)
  33. {
  34. texColors = tex.GetPixels();
  35. newColors = new Color[newWidth * newHeight];
  36. if (useBilinear)
  37. {
  38. ratioX = 1.0f / ((float)newWidth / (tex.width-1));
  39. ratioY = 1.0f / ((float)newHeight / (tex.height-1));
  40. }
  41. else {
  42. ratioX = ((float)tex.width) / newWidth;
  43. ratioY = ((float)tex.height) / newHeight;
  44. }
  45. w = tex.width;
  46. w2 = newWidth;
  47. var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
  48. var slice = newHeight/cores;
  49. finishCount = 0;
  50. if (mutex == null) {
  51. mutex = new Mutex(false);
  52. }
  53. if (cores > 1)
  54. {
  55. int i = 0;
  56. ThreadData threadData;
  57. for (i = 0; i < cores-1; i++) {
  58. threadData = new ThreadData(slice * i, slice * (i + 1));
  59. ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
  60. Thread thread = new Thread(ts);
  61. thread.Start(threadData);
  62. }
  63. threadData = new ThreadData(slice*i, newHeight);
  64. if (useBilinear)
  65. {
  66. BilinearScale(threadData);
  67. }
  68. else
  69. {
  70. PointScale(threadData);
  71. }
  72. while (finishCount < cores)
  73. {
  74. Thread.Sleep(1);
  75. }
  76. }
  77. else
  78. {
  79. ThreadData threadData = new ThreadData(0, newHeight);
  80. if (useBilinear)
  81. {
  82. BilinearScale(threadData);
  83. }
  84. else
  85. {
  86. PointScale(threadData);
  87. }
  88. }
  89. tex.Reinitialize(newWidth, newHeight);
  90. tex.SetPixels(newColors);
  91. tex.Apply();
  92. texColors = null;
  93. newColors = null;
  94. }
  95. public static void BilinearScale (System.Object obj)
  96. {
  97. ThreadData threadData = (ThreadData) obj;
  98. for (var y = threadData.start; y < threadData.end; y++)
  99. {
  100. int yFloor = (int)Mathf.Floor(y * ratioY);
  101. var y1 = yFloor * w;
  102. var y2 = (yFloor+1) * w;
  103. var yw = y * w2;
  104. for (var x = 0; x < w2; x++) {
  105. int xFloor = (int)Mathf.Floor(x * ratioX);
  106. var xLerp = x * ratioX-xFloor;
  107. newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor+1], xLerp),
  108. ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor+1], xLerp),
  109. y*ratioY-yFloor);
  110. }
  111. }
  112. mutex.WaitOne();
  113. finishCount++;
  114. mutex.ReleaseMutex();
  115. }
  116. public static void PointScale (System.Object obj)
  117. {
  118. ThreadData threadData = (ThreadData) obj;
  119. for (var y = threadData.start; y < threadData.end; y++)
  120. {
  121. var thisY = (int)(ratioY * y) * w;
  122. var yw = y * w2;
  123. for (var x = 0; x < w2; x++) {
  124. newColors[yw + x] = texColors[(int)(thisY + ratioX*x)];
  125. }
  126. }
  127. mutex.WaitOne();
  128. finishCount++;
  129. mutex.ReleaseMutex();
  130. }
  131. private static Color ColorLerpUnclamped (Color c1, Color c2, float value)
  132. {
  133. return new Color (c1.r + (c2.r - c1.r)*value,
  134. c1.g + (c2.g - c1.g)*value,
  135. c1.b + (c2.b - c1.b)*value,
  136. c1.a + (c2.a - c1.a)*value);
  137. }
  138. }
  139. }