package com.after.engine.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import java.util.HashMap; /** * * @author KiQDominaN */ public final class AFTResourceUtils { private static final HashMap cached_images = new HashMap(); //Image cache private static final Bitmap null_image = create_null_image(); //Emo-texture for missed images :) private static Bitmap create_null_image() { Bitmap tmp = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(tmp); Paint p = new Paint(); c.drawColor(0); p.setColor(0xff00ff); p.setStyle(Paint.Style.FILL); for (int y = 0; y < 32; y += 16) { for (int x = 0; x < 32; x += 16) { c.drawRect(x, y, 8, 8, p); c.drawRect(x + 8, y + 8, 8, 8, p); } } return tmp; } public static boolean image_exists(String url) { return (BitmapFactory.decodeFile(url) != null); } public static Bitmap load_image(String imageID) { if (!cached_images.containsKey(imageID)) { cached_images.put(imageID, BitmapFactory.decodeFile(imageID)); } return cached_images.get(imageID); } public static Bitmap load_image(String imageID, int width, int height) { if (!image_exists(imageID)) { return resize_image(null_image, width, height, false); } return resize_image(load_image(imageID), width, height, true); } //Resize public static Bitmap resize_image(Bitmap img, int w2, int h2, boolean interpolation) { if (img.getWidth() == w2 && img.getHeight() == h2) return img; return Bitmap.createScaledBitmap(img, w2, h2, interpolation); } public final static void set_transparency(Bitmap img, float transparency, boolean save_alpha) { int w = img.getWidth(), h = img.getHeight(); int[] tmp = new int[w * h]; img.getPixels(tmp, 0, w, 0, 0, w, h); if (save_alpha) { for (int i = 0; i < tmp.length; i++) { tmp[i] &= ~((int) ((0xff & tmp[i] >> 24) * transparency) << 24); } } else { int a = ~(int) (0xff * transparency) << 24; for (int i = 0; i < tmp.length; i++) { tmp[i] |= a; } } img.setPixels(tmp, 0, w, 0, 0, w, h); } //Blur public static void smooth_image(Bitmap img, int blurHor, int blurVer) { int w = img.getWidth(), h = img.getHeight(); int[] pixels = new int[w * h], pixels2 = new int[pixels.length]; img.getPixels(pixels, 0, w, 0, 0, w, h); pixels2 = box_blur(pixels, pixels2, w, h, blurHor); pixels = box_blur(pixels2, pixels, h, w, blurVer); img.setPixels(pixels, 0, w, 0, 0, w, h); } private static int[] box_blur(int[] in, int[] out, int width, int height, int radius) { if (radius <= 0) { return in; } int widthMinus1 = width - 1; int tableSize = 2 * radius + 1; int divideLength = 256 * tableSize; int[] divide = new int[divideLength]; for (int i = 0; i < divideLength; i++) { divide[i] = i / tableSize; } int inIndex = 0; for (int y = 0; y < height; y++) { int outIndex = y; int ta = 0, tr = 0, tg = 0, tb = 0; for (int i = -radius; i <= radius; i++) { int rgb = in[inIndex + clamp(i, 0, widthMinus1)]; ta += (rgb >> 24) & 0xff; tr += (rgb >> 16) & 0xff; tg += (rgb >> 8) & 0xff; tb += rgb & 0xff; } for (int x = 0; x < width; x++) { out[outIndex] = (ta << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb]; int i1 = x + radius + 1; if (i1 > widthMinus1) { i1 = widthMinus1; } int i2 = x - radius; if (i2 < 0) { i2 = 0; } int rgb1 = in[inIndex + i1]; int rgb2 = in[inIndex + i2]; ta += ((rgb1 & rgb1 & 0xff000000) - (rgb2 & 0xff000000)) >> 24; tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16; tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8; tb += (rgb1 & 0xff) - (rgb2 & 0xff); outIndex += height; } inIndex += width; } return out; } private static int clamp(int x, int a, int b) { return (x < a) ? a : (x > b) ? b : x; } }