Advertisement
Tatarize

Olsen Noise 3d Java Memoryless.

Feb 20th, 2015
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.76 KB | None | 0 0
  1. package com.photoembroidery.tat.olsennoise;
  2.  
  3. //MIT License, use for anything you want commercial or open, with or without attribution.
  4.  
  5. import java.util.Arrays;
  6.  
  7. public class OlsenNoise2D {
  8.  
  9. public static final int MAX_ITERATIONS = 7;
  10. //just definition for other classes to use.
  11.  
  12. public static final int SCALE_FACTOR = 2;
  13. //The scale factor is kind of arbitrary, but the code is only consistent for 2 currently. Gives noise for other scale but not location proper.
  14.  
  15. private static final int[][] blur3x3 = new int[][]{
  16. {1, 1, 1},
  17. {1, 1, 1},
  18. {1, 1, 1}
  19. }; // Matrix for the blur.
  20. private static final int blurEdge = 2; //extra pixels are needed for the blur (3 - 1).
  21.  
  22.  
  23.  
  24. //This function has a soft coded iteration.
  25.  
  26. /**
  27. * Function adds all the required pixels into the pixels array.
  28. * Note that the scanline should not actually equal the width.
  29. * It should be larger as per the getRequiredDim function.
  30. *
  31. * @param iterations Number of iterations to perform.
  32. * @param pixels pixel array to be used to insert values. (Pass by reference)
  33. * @param stride distance in the array to the next y value.
  34. * @param x requested X location.
  35. * @param y requested Y location.
  36. * @param width width of the image.
  37. * @param height height of the image.
  38. */
  39.  
  40. public static void olsennoise( int iterations, int[] pixels, int stride, int x, int y, int width, int height) {
  41. olsennoise(pixels, stride, x, y, width, height, iterations); //Calls the main routine.
  42. applyColor(pixels, stride, width, height);
  43. }
  44.  
  45. /**
  46. * Places all the grayscale colors of Olsen Noise into the array, provided.
  47. * @param pixels pixel array to be used to insert values. (Pass by reference)
  48. * @param stride distance in the array to the next y value.
  49. * @param x requested X location.
  50. * @param y requested Y location.
  51. * @param width width of the image.
  52. * @param height height of the image.
  53. */
  54. public static void olsennoise(int[] pixels, int stride, int x, int y, int width, int height) {
  55. olsennoise(pixels, stride, x, y, width, height, MAX_ITERATIONS);
  56. applyColor(pixels, stride, width, height);
  57. }
  58.  
  59. /**
  60. * Converts a dimension into the dimension required by the algorithm.
  61. * Due to the blurring, to get valid data the array must be slightly larger.
  62. * Due to the interpixel location at lowest levels it needs to be bigger by
  63. * the max value that can be. (SCALE_FACTOR)
  64. * @param dim
  65. * @return
  66. */
  67. public static int getRequiredDim(int dim) {
  68. return dim + blurEdge + SCALE_FACTOR;
  69. }
  70.  
  71. //Function inserts the values into the given pixels array (pass by reference)
  72. //The results will be within 0-255 assuming the requested iterations are 7.
  73. private static void olsennoise(int[] pixels, int stride, int x_within_field, int y_within_field, int width, int height, int iteration) {
  74. if (iteration == 0) {
  75. //Base case. If we are at the bottom. Do not run the rest of the function. Return random values.
  76. clearValues(pixels, stride, width, height); //base case needs zero, apply Noise will not eat garbage.
  77. applyNoise(pixels, stride, x_within_field, y_within_field, width, height, iteration);
  78. return;
  79. }
  80.  
  81. int x_remainder = x_within_field & 1; //Adjust the x_remainder so we know how much more into the pixel are.
  82. int y_remainder = y_within_field & 1; //Math.abs(y_within_field % SCALE_FACTOR) - Would be assumed for larger scalefactors.
  83. /*
  84. Pass the pixels, and the stride for that set of pixels.
  85. Recurse the call to the function moving the x_within_field forward if we actaully want half a pixel at the start.
  86. Same for the y.
  87. The width should expanded by the x_remainder, and then half the size, with enough extra to store the extra pixels from the blur.
  88. If the width is too long, it'll just run more stuff than it needs to.
  89. */
  90. olsennoise(pixels, stride,
  91. ((x_within_field + x_remainder) / SCALE_FACTOR) - x_remainder,
  92. ((y_within_field + y_remainder) / SCALE_FACTOR) - y_remainder,
  93. ((width + x_remainder) / SCALE_FACTOR) + blurEdge,
  94. ((height + y_remainder) / SCALE_FACTOR) + blurEdge, iteration - 1);
  95.  
  96. //This will scale the image from half the width and half the height. bounds.
  97. //The scale function assumes you have at least width/2 and height/2 good pixels.
  98. //We requested those from olsennoise above, so we should have that.
  99. applyScale(pixels, stride, width + blurEdge, height + blurEdge, SCALE_FACTOR);
  100.  
  101. //We shift the pixels over. By the amount within those pixels we want.
  102. //We sometimes want half a pixel into a scaled pixel.
  103. applyShift(pixels, stride, x_remainder, y_remainder, width + blurEdge, height + blurEdge);
  104.  
  105. //This applies the blur and uses the given bounds.
  106. //Since the blur loses two at the edge, this will result
  107. //in us having width x height of good pixels and required
  108. // width + blurEdge of good pixels. height + blurEdge of good pixels.
  109. applyBlur(pixels, stride, width + blurEdge, height + blurEdge);
  110.  
  111. //Applies noise to all the given pixels. Does not require more or less than pixels. Just offsets them all randomly.
  112. applyNoise(pixels, stride, x_within_field, y_within_field, width, height, iteration);
  113. }
  114.  
  115.  
  116. private static void applyNoise(int[] pixels, int stride, int x_within_field, int y_within_field, int width, int height, int iteration) {
  117. int index = 0;
  118. for (int k = 0, n = height - 1; k <= n; k++, index += stride) { //iterate the y positions. Offsetting the index by stride each time.
  119. for (int j = 0, m = width - 1; j <= m; j++) { //iterate the x positions through width.
  120. int current = index + j; // The current position of the pixel is the index which will have added stride each, y iteration
  121. pixels[current] += (hashrandom(j + x_within_field, k + y_within_field, iteration) & (1 << (7 - iteration)));
  122. //add on to this pixel the hash function with the set reduction.
  123. //The amount of randomness here somewhat arbitary. Just have it give self-normalized results 0-255.
  124. //It simply must scale down with the larger number of iterations.
  125. }
  126. }
  127. }
  128.  
  129. private static void applyScale(int[] pixels, int stride, int width, int height, int factor) {
  130. int index = (height - 1) * stride; //We must iteration backwards to scale so index starts at last Y position.
  131. for (int k = 0, n = height - 1; k <= n; n--, index -= stride) { // we iterate the y, removing stride from index.
  132. for (int j = 0, m = width - 1; j <= m; m--) { // iterate the x positions from width to 0.
  133. int current = index + m; //current position is the index (position of that scanline of Y) plus our current iteration in scale.
  134. int lower = ((n / factor) * stride) + (m / factor); //We find the position that is half that size. From where we scale them out.
  135. pixels[current] = pixels[lower]; // Set the outer position to the inner position. Applying the scale.
  136. }
  137. }
  138. }
  139.  
  140. //This function is to insure position stability with interpixel locations.
  141. //the pixels at the lowest iteration get scaled up, and we need positions within this pixels at the higher level.
  142. //We'd still get noise without this function, it would just line up with the lower pixels.
  143. private static void applyShift(int[] pixels, int stride, int shiftX, int shiftY, int width, int height) {
  144. if ((shiftX == 0) && (shiftY == 0)) { //if we aren't actually trying to move it.
  145. return; //return
  146. }
  147.  
  148. int index;
  149. int indexoffset = shiftX + (shiftY * stride); //The offset within the array that that shift would correspond to.
  150. //Since down and to the right is still (stride + 1) every loop. We preset it.
  151. index = 0;
  152. for (int k = 0, n = height - 1; k <= n; k++, index += stride) { // iterate the y values, add stride to index.
  153. for (int j = 0, m = width - 1; j <= m; j++) { //iterate the x values with j.
  154. int current = index + j; // current position is all our added up stride values, and our position in the X.
  155. pixels[current] = pixels[current + indexoffset]; //set the current pixel equal to the one shifted by offset.
  156. }
  157. }
  158. }
  159.  
  160. //colorizes the image. This takes our values between 0-255 and makes them pixels with alpha.
  161. //0xAARRGGBB
  162. private static void applyColor(int[] pixels, int stride, int width, int height) {
  163. int index;
  164. index = 0;
  165. for (int k = 0, n = height - 1; k <= n; k++, index += stride) { //iterate the y values.
  166. for (int j = 0, m = width - 1; j <= m; j++) { //iterate the x values.
  167. int current = index + j; // current position is the sum of the strides plus the position in the x.
  168. int pixel = pixels[current]; //get the current pixel.
  169. pixels[current] = 0xFF000000 | pixel << 16 | pixel << 8 | pixel; //turn it into a greyscale.
  170. }
  171. }
  172. }
  173.  
  174. private static void clearValues(int[] pixels, int stride, int width, int height) {
  175. int index;
  176. index = 0;
  177. for (int k = 0, n = height - 1; k <= n; k++, index += stride) { //iterate the y values.
  178. for (int j = 0, m = width - 1; j <= m; j++) { //iterate the x values.
  179. int current = index + j; // current position is the sum of the strides plus the position in the x.
  180. int pixel = pixels[current]; //get the current pixel.
  181. pixels[current] = 0; //clears those values.
  182. }
  183. }
  184. }
  185.  
  186. //Applies the blur.
  187. private static void applyBlur(int[] pixels, int stride, int width, int height) {
  188. //Calls my convolve routine, with the matrix we setup initially.
  189. convolve(pixels, 0, stride, 0, 0, width, height, blur3x3);
  190. }
  191.  
  192. /**
  193. * Memory Free In-Place Convolution.
  194. *
  195. * It is modified to not actually do all the color blending work.
  196. * The values passed to it are between 0-255
  197. * So it does a proper average.
  198. *
  199. * @param pixels pixels to be modified (pass by reference).
  200. * @param offset offset within the pixel array to call zero.
  201. * @param stride width of the memory block to next Y.
  202. * @param x the start x value.
  203. * @param y the start y value.
  204. * @param width the width of blocks to be used for the convolution.
  205. * @param height the height of the convolution area.
  206. * @param matrix matrix of the convolution.
  207. */
  208. public static void convolve(int[] pixels, int offset, int stride, int x, int y, int width, int height, int[][] matrix) {
  209. int index = offset + x + (y*stride); //index is where we are in the pixels. All equal 0 for our use. Y=0, X=0, offset = 0.
  210. for (int j = 0; j < height; j++, index += stride) { // iterate the y values. adding stride to index each time.
  211. for (int k = 0; k < width; k++) {//iterate the x values
  212. int pos = index + k; //current position sum of the strides and the position in the x.
  213. pixels[pos] = convolve(pixels,stride,pos, matrix); //convolves the matrix down and to the right from the current position.
  214. //this is somewhat non-standard, but that's because everybody's been doing it wrong for basically ever.
  215. }
  216. }
  217. }
  218.  
  219. //crimps the values between 255 and 0. Required for some other convolutions like emboss where they go out of register.
  220. private static int crimp(int color) {
  221. return (color >= 0xFF) ? 0xFF : (color < 0) ? 0 : color;
  222. }
  223.  
  224. //performs the convolution on that pixel by the given matrix.
  225. //Note all values within the matrix are down and to the right from the current pixel.
  226. //None are up or to the left. This is by design.
  227. private static int convolve(int[] pixels, int stride, int index, int[][] matrix) {
  228. int parts = 0;
  229. int sum = 0;
  230. int factor;
  231. for (int j = 0, m = matrix.length; j < m; j++, index+=stride) { //iterates the matrix
  232. for (int k = 0, n = matrix[j].length; k < n; k++) { //iterates the matrix[] within.
  233. factor = matrix[j][k]; //gets the multiple from that matrix.
  234. parts += factor; //keeps a running total for the parts.
  235. sum += factor * pixels[index + k]; //keeps a total of the sum of the factors and the pixels they correspond to.
  236. }
  237. }
  238. if (parts == 0) return crimp(sum);
  239. return crimp(sum/parts);
  240. }
  241.  
  242.  
  243. /**
  244. * XOR hash the hashed values of each element, in elements
  245. * @param elements elements to be hashed and xor'ed together.
  246. * @return
  247. */
  248. public static int hashrandom(int... elements) {
  249. long hash = 0;
  250. for (int i = 0; i < elements.length; i++) {
  251. hash ^= elements[i];
  252. hash = hash(hash);
  253. }
  254. return (int) hash;
  255. }
  256.  
  257. private static long hash(long v) {
  258. long hash = v;
  259. long h = hash;
  260.  
  261. switch ((int) hash & 3) {
  262. case 3:
  263. hash += h;
  264. hash ^= hash << 32;
  265. hash ^= h << 36;
  266. hash += hash >> 22;
  267. break;
  268. case 2:
  269. hash += h;
  270. hash ^= hash << 22;
  271. hash += hash >> 34;
  272. break;
  273. case 1:
  274. hash += h;
  275. hash ^= hash << 20;
  276. hash += hash >> 2;
  277. }
  278. hash ^= hash << 6;
  279. hash += hash >> 10;
  280. hash ^= hash << 8;
  281. hash += hash >> 34;
  282. hash ^= hash << 50;
  283. hash += hash >> 12;
  284. return hash;
  285. }
  286.  
  287. /**
  288. * Trim off the edge pixels if you need such a function.
  289. * Gets rid of the side bits.
  290. *
  291. * @param pixels destination array
  292. * @param width Width of the new image
  293. * @param height height of the new image
  294. * @param workingpixels source array
  295. * @param workingstride stride of source array
  296. */
  297. public static void trim(int[] pixels, int width, int height, int[] workingpixels, int workingstride) {
  298. for (int k = 0; k < height; k++) {
  299. for (int j = 0; j < width; j++) {
  300. int index = j + (k * width);
  301. int workingindex = j + (k * workingstride);
  302. pixels[index] = workingpixels[workingindex];
  303. }
  304. }
  305. }
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement