1 public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) { 2 3 Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); 4 5 if (radius < 1) { 6 return (null); 7 } 8 9 int w = bitmap.getWidth(); 10 int h = bitmap.getHeight(); 11 12 int[] pix = new int[w * h]; 13 // Log.e("pix", w + " " + h + " " + pix.length); 14 bitmap.getPixels(pix, 0, w, 0, 0, w, h); 15 16 int wm = w - 1; 17 int hm = h - 1; 18 int wh = w * h; 19 int div = radius + radius + 1; 20 21 int r[] = new int[wh]; 22 int g[] = new int[wh]; 23 int b[] = new int[wh]; 24 int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; 25 int vmin[] = new int[Math.max(w, h)]; 26 27 int divsum = (div + 1) >> 1; 28 divsum *= divsum; 29 int temp = 256 * divsum; 30 int dv[] = new int[temp]; 31 for (i = 0; i < temp; i++) { 32 dv[i] = (i / divsum); 33 } 34 35 yw = yi = 0; 36 37 int[][] stack = new int[div][3]; 38 int stackpointer; 39 int stackstart; 40 int[] sir; 41 int rbs; 42 int r1 = radius + 1; 43 int routsum, goutsum, boutsum; 44 int rinsum, ginsum, binsum; 45 46 for (y = 0; y < h; y++) { 47 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 48 for (i = -radius; i <= radius; i++) { 49 p = pix[yi + Math.min(wm, Math.max(i, 0))]; 50 sir = stack[i + radius]; 51 sir[0] = (p & 0xff0000) >> 16; 52 sir[1] = (p & 0x00ff00) >> 8; 53 sir[2] = (p & 0x0000ff); 54 rbs = r1 - Math.abs(i); 55 rsum += sir[0] * rbs; 56 gsum += sir[1] * rbs; 57 bsum += sir[2] * rbs; 58 if (i > 0) { 59 rinsum += sir[0]; 60 ginsum += sir[1]; 61 binsum += sir[2]; 62 } else { 63 routsum += sir[0]; 64 goutsum += sir[1]; 65 boutsum += sir[2]; 66 } 67 } 68 stackpointer = radius; 69 70 for (x = 0; x < w; x++) { 71 72 r[yi] = dv[rsum]; 73 g[yi] = dv[gsum]; 74 b[yi] = dv[bsum]; 75 76 rsum -= routsum; 77 gsum -= goutsum; 78 bsum -= boutsum; 79 80 stackstart = stackpointer - radius + div; 81 sir = stack[stackstart % div]; 82 83 routsum -= sir[0]; 84 goutsum -= sir[1]; 85 boutsum -= sir[2]; 86 87 if (y == 0) { 88 vmin[x] = Math.min(x + radius + 1, wm); 89 } 90 p = pix[yw + vmin[x]]; 91 92 sir[0] = (p & 0xff0000) >> 16; 93 sir[1] = (p & 0x00ff00) >> 8; 94 sir[2] = (p & 0x0000ff); 95 96 rinsum += sir[0]; 97 ginsum += sir[1]; 98 binsum += sir[2]; 99 100 rsum += rinsum; 101 gsum += ginsum; 102 bsum += binsum; 103 104 stackpointer = (stackpointer + 1) % div; 105 sir = stack[(stackpointer) % div]; 106 107 routsum += sir[0]; 108 goutsum += sir[1]; 109 boutsum += sir[2]; 110 111 rinsum -= sir[0]; 112 ginsum -= sir[1]; 113 binsum -= sir[2]; 114 115 yi++; 116 } 117 yw += w; 118 } 119 for (x = 0; x < w; x++) { 120 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 121 yp = -radius * w; 122 for (i = -radius; i <= radius; i++) { 123 yi = Math.max(0, yp) + x; 124 125 sir = stack[i + radius]; 126 127 sir[0] = r[yi]; 128 sir[1] = g[yi]; 129 sir[2] = b[yi]; 130 131 rbs = r1 - Math.abs(i); 132 133 rsum += r[yi] * rbs; 134 gsum += g[yi] * rbs; 135 bsum += b[yi] * rbs; 136 137 if (i > 0) { 138 rinsum += sir[0]; 139 ginsum += sir[1]; 140 binsum += sir[2]; 141 } else { 142 routsum += sir[0]; 143 goutsum += sir[1]; 144 boutsum += sir[2]; 145 } 146 147 if (i < hm) { 148 yp += w; 149 } 150 } 151 yi = x; 152 stackpointer = radius; 153 for (y = 0; y < h; y++) { 154 // Preserve alpha channel: ( 0xff000000 & pix[yi] ) 155 pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) 156 | (dv[gsum] << 8) | dv[bsum]; 157 158 rsum -= routsum; 159 gsum -= goutsum; 160 bsum -= boutsum; 161 162 stackstart = stackpointer - radius + div; 163 sir = stack[stackstart % div]; 164 165 routsum -= sir[0]; 166 goutsum -= sir[1]; 167 boutsum -= sir[2]; 168 169 if (x == 0) { 170 vmin[y] = Math.min(y + r1, hm) * w; 171 } 172 p = x + vmin[y]; 173 174 sir[0] = r[p]; 175 sir[1] = g[p]; 176 sir[2] = b[p]; 177 178 rinsum += sir[0]; 179 ginsum += sir[1]; 180 binsum += sir[2]; 181 182 rsum += rinsum; 183 gsum += ginsum; 184 bsum += binsum; 185 186 stackpointer = (stackpointer + 1) % div; 187 sir = stack[stackpointer]; 188 189 routsum += sir[0]; 190 goutsum += sir[1]; 191 boutsum += sir[2]; 192 193 rinsum -= sir[0]; 194 ginsum -= sir[1]; 195 binsum -= sir[2]; 196 197 yi += w; 198 } 199 } 200 201 // Log.e("pix", w + " " + h + " " + pix.length); 202 bitmap.setPixels(pix, 0, w, 0, 0, w, h); 203 return (bitmap); 204 }