Convert bitmap to sepia in android - android

Is there any way to convert a Bitmap to sepia?
I know to convert to grayScale is to set the setSaturation in ColorMatrix.
But what about Sepia?

If you have instance of image then you can use ColorMartix to draw it in Sepia. Let me describe way how you can do this using Drawable.
public static void setSepiaColorFilter(Drawable drawable) {
if (drawable == null)
return;
final ColorMatrix matrixA = new ColorMatrix();
// making image B&W
matrixA.setSaturation(0);
final ColorMatrix matrixB = new ColorMatrix();
// applying scales for RGB color values
matrixB.setScale(1f, .95f, .82f, 1.0f);
matrixA.setConcat(matrixB, matrixA);
final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA);
drawable.setColorFilter(filter);
}
Sample project was moved from Bitbucket to GitHub. Please check Release section to download APK binary to test without compiling.

I know the answer, but maybe if some have other better solution..
public Bitmap toSephia(Bitmap bmpOriginal)
{
int width, height, r,g, b, c, gry;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
int depth = 20;
Bitmap bmpSephia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpSephia);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setScale(.3f, .3f, .3f, 1.0f);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
canvas.drawBitmap(bmpOriginal, 0, 0, paint);
for(int x=0; x < width; x++) {
for(int y=0; y < height; y++) {
c = bmpOriginal.getPixel(x, y);
r = Color.red(c);
g = Color.green(c);
b = Color.blue(c);
gry = (r + g + b) / 3;
r = g = b = gry;
r = r + (depth * 2);
g = g + depth;
if(r > 255) {
r = 255;
}
if(g > 255) {
g = 255;
}
bmpSephia.setPixel(x, y, Color.rgb(r, g, b));
}
}
return bmpSephia;
}

I've improved on the OP's answer. This runs competitively fast when compared to the ColorMatrix method, but producing a nicer brown tone. (in my opinion)
public Bitmap toSepiaNice(Bitmap color) {
int red, green, blue, pixel, gry;
int height = color.getHeight();
int width = color.getWidth();
int depth = 20;
Bitmap sepia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] pixels = new int[width * height];
color.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < pixels.length; i++) {
pixel = pixels[i];
red = (pixel >> 16) & 0xFF;
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
red = green = blue = (red + green + blue) / 3;
red += (depth * 2);
green += depth;
if (red > 255)
red = 255;
if (green > 255)
green = 255;
pixels[i] = (0xFF << 24) | (red << 16) | (green << 8) | blue;
}
sepia.setPixels(pixels, 0, width, 0, 0, width, height);
return sepia;
}

Related

How to change color of a Bitmap that has anti-aliasing?

I have a drawable that represents a white circle with anti-aliasing that needs to be coloured in runtime.
Here's a scaled image of it:
As you can see, there are few semi-transparent pixels.
If I try to color them the fast way (which takes roughly 6-9 ms for 192x192 px drawable), I will have troubles with semi-transparent.
public static void changeBitmapColor(#NonNull Bitmap src, #ColorInt int newColor) {
Paint paint = new Paint();
ColorFilter filter = new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_IN);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(src);
canvas.drawBitmap(src, 0, 0, paint);
}
Here's the drawable after being coloured by setting ColorFilter:
If I do it using brute-force algorithm, it takes roughly 100ms to go over all pixels and apply alpha parameter to a new color:
public static void changeBitmapColor2(#NonNull Bitmap src, #ColorInt int newColor) {
int w = src.getWidth();
int h = src.getHeight();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int color = src.getPixel(x, y);
int alpha = color >>> 24;
if (alpha == 0) {
continue;
}
color = (newColor & 0x00ffffff) | (alpha << 24);
src.setPixel(x, y, color);
}
}
}
The resulting image of 2nd algorithm:
Is there anything I could do with 1st algorithm that it will result in a better quality colouring without sacrificing the performance?
Turns out, the 2nd algorithm had the right idea, but poor implementation. The key was to batch pixel retrieval and set, i.e. using pixel array:
public static void changeBitmapColor2(#NonNull Bitmap src, #ColorInt int newColor) {
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[height * width];
src.getPixels(pixels, 0, width, 0, 0, width, height);
int newColorNoAlpha = newColor & 0x00ffffff;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int currentPixel = i * width + j;
int color = pixels[currentPixel];
int alpha = color >>> 24;
if (alpha == 0) {
continue;
}
pixels[currentPixel] = newColorNoAlpha | (alpha << 24);
}
}
src.setPixels(pixels, 0, width, 0, 0, width, height);
}
This batching has reduced time to change color of 200x200 image from 100-120ms to 1-4 ms :)

How can we enhance apply Brightness Effect with custom filter?

I am trying to add custom filter and add brightness and contrast, but it takes to much time.
How can i decrease these times?
Can we use other way or add filter over filter?
//main class
private ImageView imgMain;
imgMain = (ImageView) findViewById(R.id.effect_main);
imgMain.setColorFilter(filterClass.setFilter(2));
Bitmap bitmap = ((BitmapDrawable)imgMain.getDrawable()).getBitmap();
bitmap =imgFilter.applyBrightnessEffect(bitmap , 150);
imgMain.setImageBitmap(bitmap);
for custom filter
//filter methords
public ColorMatrixColorFilter catNightFilter() {
float[] matrix = {
89, 61, -5, 0, 0, //red
77, 67, 24, 0, 0, //green
-57, -13, 81, 0, 0, //blue
0, 0, 0, 0, 0 //alpha
};
ColorMatrix cm = new ColorMatrix(matrix);``
return new ColorMatrixColorFilter(cm);
}
This Brightness code takes to much time it convert each pixel.
// scan through all pixels
//britness methord
public Bitmap applyBrightnessEffect(Bitmap src, int value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// increase/decrease each channel
R += value;
if(R > 255) { R = 255; }
else if(R < 0) { R = 0; }
G += value;
if(G > 255) { G = 255; }
else if(G < 0) { G = 0; }
B += value;
if(B > 255) { B = 255; }
else if(B < 0) { B = 0; }
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}

How adjust brightness of bitmap in gradient format in android?

I have seen so many links and posts of to adjust birightness of bitmap in android, but i want to adjust brightness with gradient, like adjust brightness of only center of the bitmap. Anybody have any idea how to do this ?
You can generate a bitmap and draw a gradient:
How to draw a smooth/dithered gradient on a canvas in Android
Then use each pixel from gradient bitmap and get the brightness and pass it to the function that adjust brightness.
look here Formula to determine brightness of RGB color
public Bitmap SetBrightness(Bitmap src, Bitmap gradient) {
// original image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
int gradientPixel = gradient.getPixel(x, y);
int value = getLuminance(gradientPixel);
// increase/decrease each channel
R += value;
if (R > 255) {
R = 255;
}
else if (R < 0) {
R = 0;
}
G += value;
if (G > 255) {
G = 255;
}
else if (G < 0) {
G = 0;
}
B += value;
if (B > 255) {
B = 255;
}
else if (B < 0) {
B = 0;
}
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
public int getLuminance(int pixel) {
int A, R, G, B;
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
}

how to make the color image to black and white in Android

I wanted to know the way to convert the color image (which i am downloading from net) to black and white when i am displaying it to the user in android.
can anybody found this requirement in any of your android work. Please let me know.
Thanks
Lakshman
Hi you can make the image black n white using contrast.
See the code..
public static Bitmap createContrast(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
G = Color.red(pixel);
G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
B = Color.red(pixel);
B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
Set the double value to 50 on mathod call. For Example createContrast(Bitmap src, 50)
Use the built-in methods:
public static Bitmap toGrayscale(Bitmap srcImage) {
Bitmap bmpGrayscale = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(srcImage, 0, 0, paint);
return bmpGrayscale;
}

blur and emboss an image

I'm working on an android application, and I have a drawable that I'm loading up from a source image. On this image i want make a image blur and embross ,i have read How to change colors of a Drawable in Android?
can you give me some advice?thank you
edit:i have emboss an image,but i cannot blue an image,can you give me some blue
public Bitmap fudiao(Bitmap bmpOriginal) {
int width, height, r,g, b, c,a, gry,c1,a1,r1,g1,b1,red,green,blue;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
int depth = 30;
Bitmap bmpSephia = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpSephia);
Paint paint = new Paint();
// ColorMatrix cm = new ColorMatrix();
// cm.setScale(.3f, .3f, .3f, 1.0f);
// ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
// paint.setColorFilter(f);
canvas.drawBitmap(bmpOriginal, 0, 0, null);
for(int y=1; y< height-1; y++) {
for(int x=1; x < width-1; x++) {
c = bmpOriginal.getPixel(x, y);
r = Color.red(c);
g = Color.green(c);
b = Color.blue(c);
c1 = bmpOriginal.getPixel(x-1, y-1);
r1 = Color.red(c1);
g1 = Color.green(c1);
b1 = Color.blue(c1);
red = Math.max(67, Math.min(255, Math.abs(r - r1 + 128)));
green = Math.max(67, Math.min(255, Math.abs(g - g1 + 128)));
blue = Math.max(67, Math.min(255, Math.abs(b - b1 + 128)));
if (red > 255)
{
red = 255;
}
else if (red < 0)
{
red = 0;
}
if (green > 255)
{
green = 255;
}
else if (green < 0)
{
green = 0;
}
if (blue > 255)
{
blue = 255;
}
else if (blue < 0)
{
blue = 0;
}
bmpSephia.setPixel(x, y, Color.rgb(red, green, blue));
// bmpSephia.setPixel(x, y, Color.argb(a1, red, green, blue));
}
}
return bmpSephia;
}
this only emboss a pic
I think you are probably looking for something like this: http://code.google.com/p/jjil/
paint.setColorFilter( <LightingColorFilter> );

Categories

Resources