Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---- MIT License --- Use for anything you want, public, private, profit, or pro bono.
- #pragma version(1)
- #pragma rs java_package_name(com.photoembroidery.tat.olsennoise)
- rs_allocation raster0;
- rs_allocation raster1;
- int32_t posx;
- int32_t posy;
- int32_t color0 = 0;
- int32_t color1 = -1;
- uint32_t iteration;
- static uint64_t getHash(int64_t hash) {
- int64_t h = hash;
- switch ((int) hash & 3) {
- case 3:
- hash += h;
- hash ^= hash << 32;
- hash ^= h << 36;
- hash += hash >> 22;
- break;
- case 2:
- hash += h;
- hash ^= hash << 22;
- hash += hash >> 34;
- break;
- case 1:
- hash += h;
- hash ^= hash << 20;
- hash += hash >> 2;
- }
- hash ^= hash << 6;
- hash += hash >> 10;
- hash ^= hash << 8;
- hash += hash >> 34;
- hash ^= hash << 50;
- hash += hash >> 12;
- return hash;
- }
- static int64_t positionHash(int32_t x, int32_t y) {
- int64_t hash = x;
- hash = getHash(hash);
- hash = hash ^ y;
- hash = getHash(hash);
- hash = hash ^ iteration;
- hash = getHash(hash);
- return hash;
- }
- uchar4 __attribute__((kernel)) greyscale(const uchar4 in, uint32_t x, uint32_t y) {
- uchar4 out;
- uchar a = in.b;
- uint32_t red0 = (color0 >> 16) & 0xFF;
- uint32_t green0 = (color0 >> 8) & 0xFF;
- uint32_t blue0 = (color0 ) & 0xFF;
- red0 *= a;
- green0 *= a;
- blue0 *= a;
- a = 255-a;
- uint32_t red1 = (color1 >> 16) & 0xFF;
- uint32_t green1 = (color1 >> 8) & 0xFF;
- uint32_t blue1 = (color1 ) & 0xFF;
- red1 *= a;
- green1 *= a;
- blue1 *= a;
- out.r = (red0+red1) / 255;
- out.g = (green0+green1) / 255;
- out.b = (blue0+blue1) / 255;
- out.a = in.a;
- return out;
- }
- uchar4 __attribute__((kernel)) scaleshift(const uchar4 in, uint32_t x, uint32_t y) {
- int32_t xfrom = ((x+(posx & 1))>>1);
- int32_t yfrom = ((y+(posy & 1))>>1);
- return rsGetElementAt_uchar4(raster0,xfrom, yfrom);
- }
- uchar4 __attribute__((kernel)) root(const uchar4 in, uint32_t x, uint32_t y) {
- uint32_t red = 0;
- uint32_t green = 0;
- uint32_t blue = 0;
- if (iteration != 0) {
- uchar4 element;
- for (uint32_t j = x, m = x+2; j <= m; j++) {
- for (uint32_t k = y, n = y+2; k <= n; k++) {
- element = rsGetElementAt_uchar4(raster1,j,k);
- red += element.r;
- blue += element.b;
- green += element.g;
- }
- }
- red /= 9;
- green /= 9;
- blue /= 9;
- }
- uint64_t poshash = positionHash(posx+x,posy+y);
- red += ((poshash ) & (1 << (7 - iteration)));
- green += ((poshash>>1) & (1 << (7 - iteration)));
- blue += ((poshash>>2) & (1 << (7 - iteration)));
- uchar4 out;
- out.r = (red & 0xFF);
- out.g = (green & 0xFF);
- out.b = (blue & 0xFF);
- out.a = 0xFF;
- return out;
- }
- ----- JAVA CODE -------
- public void initializeSubClass(int builtWidth, int builtHeight) {
- mRS = RenderScript.create(context);
- mScript = new ScriptC_olsennoise(mRS, context.getResources(), R.raw.olsennoise);
- allocationRaster0 = Allocation.createFromBitmap(
- mRS,
- drawingBitmap,
- Allocation.MipmapControl.MIPMAP_NONE,
- Allocation.USAGE_SCRIPT
- );
- allocationRaster1 = Allocation.createTyped(mRS, allocationRaster0.getType());
- launchOptions = new Script.LaunchOptions();
- mScript.set_raster0(allocationRaster0);
- mScript.set_raster1(allocationRaster1);
- }
- @Override
- protected void doRefresh(int sx, int sy, int width, int height,int iteration) {
- olsenNoiseLoop(sx, sy, width, height, iteration);
- if (twocolor) {
- mScript.set_color0(color[0]);
- mScript.set_color1(color[1]);
- mScript.forEach_greyscale(allocationRaster0,allocationRaster1);
- allocationRaster1.copyTo(drawingBitmap);
- }
- else {
- allocationRaster0.copyTo(drawingBitmap);
- }
- }
- private void olsenNoiseLoop(int x_within_field, int y_within_field, int width, int height, int iteration) {
- if (iteration > 0) {
- int x_remainder = x_within_field & 1;
- int y_remainder = y_within_field & 1;
- olsenNoiseLoop(
- ((x_within_field + x_remainder) / SCALE_FACTOR) - x_remainder,
- ((y_within_field + y_remainder) / SCALE_FACTOR) - y_remainder,
- ((width + x_remainder) / SCALE_FACTOR) + BLUR_EDGE,
- ((height + y_remainder) / SCALE_FACTOR) + BLUR_EDGE, iteration - 1);
- }
- applyOlsenNoise(width, height, x_within_field, y_within_field, iteration);
- }
- private void applyOlsenNoise(int scope_width, int scope_height, int x_within_field, int y_within_field, int iteration) {
- mScript.set_posx(x_within_field);
- mScript.set_posy(y_within_field);
- mScript.set_iteration(iteration);
- launchOptions.setX(0, scope_width + BLUR_EDGE);
- launchOptions.setY(0, scope_height + BLUR_EDGE);
- mScript.forEach_scaleshift(allocationRaster0, allocationRaster1, launchOptions);
- mScript.forEach_root(allocationRaster1, allocationRaster0, launchOptions);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement