Example usage for android.graphics Bitmap setPixel

List of usage examples for android.graphics Bitmap setPixel

Introduction

In this page you can find the example usage for android.graphics Bitmap setPixel.

Prototype

public void setPixel(int x, int y, @ColorInt int color) 

Source Link

Document

Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate.

Usage

From source file:Main.java

public static Bitmap blur(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int newColor = 0;

    int[][] colors = new int[9][3];
    for (int i = 1, length = width - 1; i < length; i++) {
        for (int k = 1, len = height - 1; k < len; k++) {
            for (int m = 0; m < 9; m++) {
                int s = 0;
                int p = 0;
                switch (m) {
                case 0:
                    s = i - 1;/* www . j  a  v a2  s . c om*/
                    p = k - 1;
                    break;
                case 1:
                    s = i;
                    p = k - 1;
                    break;
                case 2:
                    s = i + 1;
                    p = k - 1;
                    break;
                case 3:
                    s = i + 1;
                    p = k;
                    break;
                case 4:
                    s = i + 1;
                    p = k + 1;
                    break;
                case 5:
                    s = i;
                    p = k + 1;
                    break;
                case 6:
                    s = i - 1;
                    p = k + 1;
                    break;
                case 7:
                    s = i - 1;
                    p = k;
                    break;
                case 8:
                    s = i;
                    p = k;
                }
                pixColor = bitmap.getPixel(s, p);
                colors[m][0] = Color.red(pixColor);
                colors[m][1] = Color.green(pixColor);
                colors[m][2] = Color.blue(pixColor);
            }

            for (int m = 0; m < 9; m++) {
                newR += colors[m][0];
                newG += colors[m][1];
                newB += colors[m][2];
            }

            newR = (int) (newR / 9F);
            newG = (int) (newG / 9F);
            newB = (int) (newB / 9F);

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            newColor = Color.argb(255, newR, newG, newB);
            newBitmap.setPixel(i, k, newColor);

            newR = 0;
            newG = 0;
            newB = 0;
        }
    }
    return newBitmap;
}

From source file:org.i_chera.wolfensteineditor.document.VSwapContainer.java

/**
 * Gets a 64x64 bitmap from a given sprite
 * @param n Index of sprite/*from  w ww  . ja  v a 2  s.c  o m*/
 * @return Null if invalid or out of bounds, the bitmap otherwise
 */
public Bitmap getSpriteBitmap(int n) {
    if (n < 0 || n >= mSoundStart - mSpriteStart)
        return null;

    if (mSpriteBitmapCache == null) {
        mSpriteBitmapCache = new LruCache<Integer, Bitmap>(mSoundStart - mSpriteStart);
    }

    Bitmap bmp = mSpriteBitmapCache.get(n);

    if (bmp == null) {
        bmp = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);

        byte[] data = mPages.get(mSpriteStart + n);
        int leftPixel = FileUtil.readUInt16(data, 0);
        int rightPixel = FileUtil.readUInt16(data, 2);
        if (leftPixel < 0 || leftPixel >= 64 || rightPixel < 0 || rightPixel >= 64 || rightPixel < leftPixel)
            return null;
        int directoryOffset;
        int x, y, i, j, topPixel, bottomPixel, postStart;
        for (x = leftPixel, i = 4; x <= rightPixel; ++x, i += 2) {
            directoryOffset = FileUtil.readUInt16(data, i);
            j = directoryOffset;
            for (;;) {
                bottomPixel = FileUtil.readUInt16(data, j) / 2;
                j += 2;
                if (bottomPixel == 0)
                    break;
                postStart = FileUtil.readInt16(data, j);
                j += 2;
                topPixel = FileUtil.readUInt16(data, j) / 2;
                j += 2;
                if (bottomPixel < 0 || bottomPixel > 64 || topPixel < 0 || topPixel >= 64
                        || bottomPixel <= topPixel)
                    return null;
                for (y = topPixel; y < bottomPixel; ++y) {
                    bmp.setPixel(x, y, Palette.WL6[data[postStart + y] & 0xff]);
                }
            }
        }
        mSpriteBitmapCache.put(n, bmp);
    }

    return bmp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap myTest(Bitmap bmImage, int amplitude) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());
    // Bitmap bmTemp = bmImage.copy(Config.ARGB_8888, true);

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {

            int sign;
            if (i <= bmImage.getWidth() / 2 || j <= bmImage.getHeight() / 2)
                sign = 1;/*from  w ww  .j  ava 2  s . c o m*/
            else
                sign = -1;
            int ki = (int) (i + amplitude * sign * Math.cos(j * 2 * Math.PI / bmImage.getHeight()));
            int kj = (int) (j + amplitude * sign * Math.cos(i * 2 * Math.PI / bmImage.getWidth()));
            // * Math.tan(i * 2 * Math.PI / bmImage.getWidth()));
            // * Math.sin(i * 2 * Math.PI / bmImage.getWidth()));

            // k = Math.max(0, k);
            // k = Math.min(bmImage.getHeight() - 1, k);

            if (ki >= 0 && ki < bmImage.getWidth() && kj >= 0 && kj < bmImage.getHeight())
                bmTemp.setPixel(i, j, bmImage.getPixel(ki, kj));
            else
                bmTemp.setPixel(i, j, 0x00ffffff);

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap bulging(Bitmap bmImage, int radius, int pointX, int pointY) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());
    // Bitmap bmTemp = bmImage.copy(Config.ARGB_8888, true);

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {

            // get center point
            double cx = pointX; // bmImage.getWidth() / 2;
            double cy = pointY; // bmImage.getHeight() / 2;

            // compute distance to center point
            double r = Math.sqrt(Math.pow(i - cx, 2) + Math.pow(j - cy, 2));
            // compute angle atan2(y, x) in range (-PI..PI] n polar coordinate system
            double a = Math.atan2(j - cy, i - cx);
            // rn = r ^ k, k=1..2
            double rn = Math.pow(r, radius / 10.0) / (radius);
            // compute mapping point and then shift to center point
            int kx = (int) (rn * Math.cos(a) + cx);
            int ky = (int) (rn * Math.sin(a) + cy);

            if (kx >= 0 && kx < bmImage.getWidth() && ky >= 0 && ky < bmImage.getHeight())
                bmTemp.setPixel(i, j, bmImage.getPixel(kx, ky));
            else//from   w w w.j a va2s .c  o m
                bmTemp.setPixel(i, j, 0x00ffffff);

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap tint(Bitmap bmImage, int degree) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    double angle = (3.14159d * (double) degree) / 180.0d;
    int S = (int) (256.0d * Math.sin(angle));
    int C = (int) (256.0d * Math.cos(angle));

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            // int alpha = Color.alpha(p);

            int RY = (70 * r - 59 * g - 11 * b) / 100;
            int BY = (-30 * r - 59 * g + 89 * b) / 100;
            int Y = (30 * r + 59 * g + 11 * b) / 100;
            int RYY = (S * BY + C * RY) / 256;
            int BYY = (C * BY - S * RY) / 256;
            int GYY = (-51 * RYY - 19 * BYY) / 100;
            r = Y + RYY;//from w w w  .j ava  2  s  .  c o  m
            r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
            g = Y + GYY;
            g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
            b = Y + BYY;
            b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
            bmTemp.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
        }
    }

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap mozac(Bitmap bmImage, int level) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    for (int i = 0; i < bmImage.getWidth(); i += level) {
        for (int j = 0; j < bmImage.getHeight(); j += level) {
            int p = 0;
            int r = 0;
            int g = 0;
            int b = 0;

            // compute neighboring area index
            int kx_start = i - level;
            int kx_end = i + level;
            int ky_start = j - level;
            int ky_end = j + level;

            // filter out boundary index
            kx_start = Math.max(0, kx_start);
            kx_end = Math.min(bmImage.getWidth() - 1, kx_end);
            ky_start = Math.max(0, ky_start);
            ky_end = Math.min(bmImage.getHeight() - 1, ky_end);

            // summing and averaging color value within the neighboring area
            for (int ki = kx_start; ki <= kx_end; ki++) {
                for (int kj = ky_start; kj <= ky_end; kj++) {
                    p = bmImage.getPixel(ki, kj);
                    r += Color.red(p);
                    g += Color.green(p);
                    b += Color.blue(p);
                }//from   ww  w .ja v  a2  s . c  o m
            }

            int n = (kx_end - kx_start + 1) * (ky_end - ky_start + 1);
            r /= n;
            g /= n;
            b /= n;

            // copy color value to each pixel on neighboring area
            for (int kx = kx_start; kx <= kx_end; kx++) {
                for (int ky = ky_start; ky <= ky_end; ky++) {
                    bmTemp.setPixel(kx, ky, Color.argb(Color.alpha(p), r, g, b));
                }
            }

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.skytree.epubtest.BookViewActivity.java

private Drawable changeDrawableColor(Drawable drawable, int fromColor, int color) {
    Bitmap src = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            if (colorMatched(bitmap.getPixel(x, y), fromColor, 10)) {
                bitmap.setPixel(x, y, color);
            }//from www  .  j a  v a 2  s .  c o  m
        }
    }
    return new BitmapDrawable(bitmap);
}