Advertisement
Tatarize

Olsen Noise still broke

Feb 19th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1.  
  2. package org.scribble.scommon;
  3.  
  4.  
  5. import java.util.Arrays;
  6.  
  7. public class OlsenNoise2D {
  8.  
  9. public static void convolve(int[] pixels, int offset, int stride, int x, int y, int width, int height, int[][] matrix, int parts) {
  10. int startIndex = x + (y * stride);
  11. int lastIndex = (x + width - 1) + ((y + height - 1) * stride);
  12. int x_pos = x;
  13. int y_pos = y;
  14. int indexInc = 1;
  15. int yinc = 0;
  16. int xinc = 1;
  17.  
  18. for (int i = 0, s = width + height; i < s; i++) {
  19. if (i == width) {
  20. indexInc = stride;
  21. yinc = 1;
  22. xinc = 0;
  23. }
  24. int x_counter = x_pos;
  25. int index = startIndex;
  26. while (x_counter >= x && index <= lastIndex) {
  27. pixels[offset + index] = convolve(pixels, stride, offset + index, matrix, parts);
  28. x_counter--;
  29. index += stride - 1;
  30. }
  31. startIndex += indexInc;
  32. x_pos += xinc;
  33. y_pos += yinc;
  34. }
  35. }
  36.  
  37.  
  38. private static int crimp(int color) {
  39. return (color >= 0xFF) ? 0xFF : (color < 0) ? 0 : color;
  40. }
  41.  
  42. private static int convolve(int[] pixels, int stride, int index, int[][] matrix, int parts) {
  43. int redSum = 0;
  44. int greenSum = 0;
  45. int blueSum = 0;
  46. int pixel, factor;
  47. for (int j = 0, m = matrix.length; j < m; j++, index++) {
  48. for (int k = 0, n = matrix[j].length, q = index; k < n; k++, q += stride) {
  49. pixel = pixels[q];
  50. factor = matrix[j][k];
  51. redSum += factor * ((pixel >> 16) & 0xFF);
  52. greenSum += factor * ((pixel >> 8) & 0xFF);
  53. blueSum += factor * ((pixel) & 0xFF);
  54. }
  55. }
  56. return 0xFF000000 | ((crimp(redSum / parts) << 16) | (crimp(greenSum / parts) << 8) | (crimp(blueSum / parts)));
  57. }
  58.  
  59. //occupies same footprint
  60. private void applyNoise(int[] pixels, int stride, int x_within_scope, int y_within_scope, int x_within_field, int y_within_field, int width, int height, int iteration) {
  61. int index = (y_within_scope * stride);
  62. for (int k = y_within_scope, n = y_within_scope + height - 1; k <= n; k++, index += stride) {
  63. for (int j = x_within_scope, m = x_within_scope + width - 1; j <= m; j++) {
  64. int current = index + j;
  65. pixels[current] += (hashrandom(x_within_field + j, y_within_field + k, iteration) & (1 << (7 - iteration)));
  66. }
  67. }
  68. }
  69.  
  70. //requires half the height and width be good.
  71. private void applyScale(int[] pixels, int stride, int x_within_scope, int y_within_scope, int width, int height) {
  72. int index = (y_within_scope + height - 1) * stride;
  73. for (int k = y_within_scope, n = y_within_scope + height - 1; k <= n; n--, index -= stride) {
  74. for (int j = x_within_scope, m = x_within_scope + width - 1; j <= m; m--) {
  75. int current = index + m;
  76. int lower = ((n / 2) * stride) + (m / 2);
  77. pixels[current] = pixels[lower];
  78. }
  79. }
  80. }
  81.  
  82. //requires width + 1 valid, height + 1
  83. private static final int[][] blur2x2 = new int[][]{
  84. {1, 1},
  85. {1, 1},
  86. };
  87.  
  88. private void applyBlur(int[] pixels, int stride, int x_within_scope, int y_within_scope, int width, int height) {
  89. convolve(pixels, 0, stride, x_within_scope, y_within_scope, width, height, blur2x2, 4);
  90. }
  91.  
  92. //You need to give it an array larger than the current one. This math is just a guess at a number big enough, it's not actually the max dim the array would properly need to be.
  93. public int getRequiredDim(int dim) {
  94. return (dim + 5) * 2;
  95. }
  96. private void trim(int[] pixels, int width, int height, int[] workingpixels, int workingstride) {
  97. for (int k = 0; k < height; k++) {
  98. for (int j = 0; j < width; j++) {
  99. int index = j + (k * width);
  100. int workingindex = j + (k * workingstride);
  101. pixels[index] = workingpixels[workingindex];
  102. }
  103. }
  104. }
  105.  
  106. static final int maxiterations = 5;
  107. public void olsennoise(int[] pixels, int stride, int x, int y, int width, int height) {
  108. Arrays.fill(pixels, 0xFF000000);
  109. olsennoise(pixels, stride, x, y, 0, 0, width, height, maxiterations);
  110. }
  111.  
  112. public void olsennoise(int[] pixels, int stride, int x_within_field, int y_within_field, int x_within_scope, int y_within_scope, int width, int height, int iteration) {
  113. if (iteration == 0) {
  114. applyNoise(pixels, stride, x_within_scope, y_within_scope, x_within_field, y_within_field, width, height, iteration);
  115. return;
  116. }
  117. x_within_scope = (x_within_field)&1;
  118. y_within_scope = (y_within_field)&1;
  119.  
  120. int scopeX = (((x_within_field-1)+x_within_scope)/2);
  121. int scopeY = (((y_within_field-1)+y_within_scope)/2);
  122. int halfWidth = (width+1)/2;
  123. int halfHeight = (height+1)/2;
  124. int scopeWidth = (halfWidth) + ((width)&1) ;
  125. int scopeHeight = (halfHeight) + ((height)&1);
  126.  
  127. olsennoise(pixels, stride, scopeX, scopeY, x_within_scope, y_within_scope, scopeWidth, scopeHeight, iteration - 1);
  128.  
  129. scopeWidth -= 1;
  130. scopeHeight -= 1;
  131. //does not change position of x,y
  132. applyBlur(pixels, stride, scopeX, scopeY, scopeWidth, scopeHeight);
  133.  
  134. scopeWidth *= 2;
  135. scopeHeight *= 2;
  136. scopeX *= 2;
  137. scopeY *= 2;
  138. applyScale(pixels, stride, scopeX, scopeY, scopeWidth, scopeHeight);
  139.  
  140.  
  141. //noise requires width, and height valid pixels
  142. applyNoise(pixels, stride, scopeX, scopeY, x_within_field, y_within_field, scopeWidth, scopeHeight, iteration);
  143.  
  144. System.out.println(iteration);
  145. System.out.println(" " + scopeWidth + " == " + width + " && " + scopeHeight + " == " + height);
  146.  
  147. }
  148.  
  149.  
  150. public static int hashrandom(int... elements) {
  151. long hash = 0;
  152. for (int i = 0; i < elements.length; i++) {
  153. hash ^= elements[i];
  154. hash = hash(hash);
  155. }
  156. return (int) hash;
  157. }
  158.  
  159. public static long hash(long v) {
  160. long hash = v;
  161. long h = hash;
  162.  
  163. switch ((int) hash & 3) {
  164. case 3:
  165. hash += h;
  166. hash ^= hash << 32;
  167. hash ^= h << 36;
  168. hash += hash >> 22;
  169. break;
  170. case 2:
  171. hash += h;
  172. hash ^= hash << 22;
  173. hash += hash >> 34;
  174. break;
  175. case 1:
  176. hash += h;
  177. hash ^= hash << 20;
  178. hash += hash >> 2;
  179. }
  180. hash ^= hash << 6;
  181. hash += hash >> 10;
  182. hash ^= hash << 8;
  183. hash += hash >> 34;
  184. hash ^= hash << 50;
  185. hash += hash >> 12;
  186. return hash;
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement