Advertisement
Tatarize

Renderscript Olsen Noise

Oct 15th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. ---- MIT License --- Use for anything you want, public, private, profit, or pro bono.
  2.  
  3.  
  4.  
  5. #pragma version(1)
  6. #pragma rs java_package_name(com.photoembroidery.tat.olsennoise)
  7.  
  8. rs_allocation raster0;
  9. rs_allocation raster1;
  10.  
  11. int32_t posx;
  12. int32_t posy;
  13. int32_t color0 = 0;
  14. int32_t color1 = -1;
  15.  
  16. uint32_t iteration;
  17.  
  18. static uint64_t getHash(int64_t hash) {
  19. int64_t h = hash;
  20. switch ((int) hash & 3) {
  21. case 3:
  22. hash += h;
  23. hash ^= hash << 32;
  24. hash ^= h << 36;
  25. hash += hash >> 22;
  26. break;
  27. case 2:
  28. hash += h;
  29. hash ^= hash << 22;
  30. hash += hash >> 34;
  31. break;
  32. case 1:
  33. hash += h;
  34. hash ^= hash << 20;
  35. hash += hash >> 2;
  36. }
  37. hash ^= hash << 6;
  38. hash += hash >> 10;
  39. hash ^= hash << 8;
  40. hash += hash >> 34;
  41. hash ^= hash << 50;
  42. hash += hash >> 12;
  43. return hash;
  44. }
  45.  
  46. static int64_t positionHash(int32_t x, int32_t y) {
  47. int64_t hash = x;
  48. hash = getHash(hash);
  49. hash = hash ^ y;
  50. hash = getHash(hash);
  51. hash = hash ^ iteration;
  52. hash = getHash(hash);
  53. return hash;
  54. }
  55.  
  56.  
  57. uchar4 __attribute__((kernel)) greyscale(const uchar4 in, uint32_t x, uint32_t y) {
  58. uchar4 out;
  59. uchar a = in.b;
  60. uint32_t red0 = (color0 >> 16) & 0xFF;
  61. uint32_t green0 = (color0 >> 8) & 0xFF;
  62. uint32_t blue0 = (color0 ) & 0xFF;
  63. red0 *= a;
  64. green0 *= a;
  65. blue0 *= a;
  66.  
  67. a = 255-a;
  68. uint32_t red1 = (color1 >> 16) & 0xFF;
  69. uint32_t green1 = (color1 >> 8) & 0xFF;
  70. uint32_t blue1 = (color1 ) & 0xFF;
  71. red1 *= a;
  72. green1 *= a;
  73. blue1 *= a;
  74.  
  75. out.r = (red0+red1) / 255;
  76. out.g = (green0+green1) / 255;
  77. out.b = (blue0+blue1) / 255;
  78. out.a = in.a;
  79. return out;
  80. }
  81.  
  82. uchar4 __attribute__((kernel)) scaleshift(const uchar4 in, uint32_t x, uint32_t y) {
  83. int32_t xfrom = ((x+(posx & 1))>>1);
  84. int32_t yfrom = ((y+(posy & 1))>>1);
  85. return rsGetElementAt_uchar4(raster0,xfrom, yfrom);
  86. }
  87.  
  88. uchar4 __attribute__((kernel)) root(const uchar4 in, uint32_t x, uint32_t y) {
  89. uint32_t red = 0;
  90. uint32_t green = 0;
  91. uint32_t blue = 0;
  92.  
  93. if (iteration != 0) {
  94. uchar4 element;
  95. for (uint32_t j = x, m = x+2; j <= m; j++) {
  96. for (uint32_t k = y, n = y+2; k <= n; k++) {
  97. element = rsGetElementAt_uchar4(raster1,j,k);
  98. red += element.r;
  99. blue += element.b;
  100. green += element.g;
  101. }
  102. }
  103. red /= 9;
  104. green /= 9;
  105. blue /= 9;
  106. }
  107.  
  108. uint64_t poshash = positionHash(posx+x,posy+y);
  109.  
  110. red += ((poshash ) & (1 << (7 - iteration)));
  111. green += ((poshash>>1) & (1 << (7 - iteration)));
  112. blue += ((poshash>>2) & (1 << (7 - iteration)));
  113. uchar4 out;
  114. out.r = (red & 0xFF);
  115. out.g = (green & 0xFF);
  116. out.b = (blue & 0xFF);
  117. out.a = 0xFF;
  118. return out;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. ----- JAVA CODE -------
  125.  
  126.  
  127.  
  128. public void initializeSubClass(int builtWidth, int builtHeight) {
  129. mRS = RenderScript.create(context);
  130. mScript = new ScriptC_olsennoise(mRS, context.getResources(), R.raw.olsennoise);
  131. allocationRaster0 = Allocation.createFromBitmap(
  132. mRS,
  133. drawingBitmap,
  134. Allocation.MipmapControl.MIPMAP_NONE,
  135. Allocation.USAGE_SCRIPT
  136. );
  137. allocationRaster1 = Allocation.createTyped(mRS, allocationRaster0.getType());
  138. launchOptions = new Script.LaunchOptions();
  139. mScript.set_raster0(allocationRaster0);
  140. mScript.set_raster1(allocationRaster1);
  141. }
  142.  
  143. @Override
  144. protected void doRefresh(int sx, int sy, int width, int height,int iteration) {
  145. olsenNoiseLoop(sx, sy, width, height, iteration);
  146. if (twocolor) {
  147. mScript.set_color0(color[0]);
  148. mScript.set_color1(color[1]);
  149. mScript.forEach_greyscale(allocationRaster0,allocationRaster1);
  150. allocationRaster1.copyTo(drawingBitmap);
  151. }
  152. else {
  153. allocationRaster0.copyTo(drawingBitmap);
  154. }
  155. }
  156.  
  157. private void olsenNoiseLoop(int x_within_field, int y_within_field, int width, int height, int iteration) {
  158. if (iteration > 0) {
  159. int x_remainder = x_within_field & 1;
  160. int y_remainder = y_within_field & 1;
  161. olsenNoiseLoop(
  162. ((x_within_field + x_remainder) / SCALE_FACTOR) - x_remainder,
  163. ((y_within_field + y_remainder) / SCALE_FACTOR) - y_remainder,
  164. ((width + x_remainder) / SCALE_FACTOR) + BLUR_EDGE,
  165. ((height + y_remainder) / SCALE_FACTOR) + BLUR_EDGE, iteration - 1);
  166. }
  167. applyOlsenNoise(width, height, x_within_field, y_within_field, iteration);
  168. }
  169.  
  170. private void applyOlsenNoise(int scope_width, int scope_height, int x_within_field, int y_within_field, int iteration) {
  171. mScript.set_posx(x_within_field);
  172. mScript.set_posy(y_within_field);
  173. mScript.set_iteration(iteration);
  174. launchOptions.setX(0, scope_width + BLUR_EDGE);
  175. launchOptions.setY(0, scope_height + BLUR_EDGE);
  176. mScript.forEach_scaleshift(allocationRaster0, allocationRaster1, launchOptions);
  177. mScript.forEach_root(allocationRaster1, allocationRaster0, launchOptions);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement