Home > Tutorials > Image Processing – Tint Color (Yet Another)

Image Processing – Tint Color (Yet Another)


It’s been a long time since  I haven’t touched anything related to image processing. Well, today just got a new algorithm, which is pretty much nicer than the one I posted before.

Original Image

Original Image

A Tinted Image (50)

A Tinted Image (50)

Of course, this is how it’s done.

package pete.android.study.imageprocessing;

import android.graphics.Bitmap;

public class ImageProcessor {
	
	public static final double PI = 3.14159d;
	public static final double FULL_CIRCLE_DEGREE = 360d;
	public static final double HALF_CIRCLE_DEGREE = 180d;
	public static final double RANGE = 256d;
	
	public static Bitmap tintImage(Bitmap src, int degree) {

	    int width = src.getWidth();
		int height = src.getHeight();

	    int[] pix = new int[width * height];
	    src.getPixels(pix, 0, width, 0, 0, width, height);

	    int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
	    double angle = (PI * (double)degree) / HALF_CIRCLE_DEGREE;
	   
	    int S = (int)(RANGE * Math.sin(angle));
	    int C = (int)(RANGE * Math.cos(angle));

	    for (int y = 0; y < height; y++)
	        for (int x = 0; x < width; x++) {
		    	int index = y * width + x;
		    	int r = ( pix[index] >> 16 ) & 0xff;
		    	int g = ( pix[index] >> 8 ) & 0xff;
		    	int b = pix[index] & 0xff;
		    	RY = ( 70 * r - 59 * g - 11 * b ) / 100;
		    	GY = (-30 * r + 41 * g - 11 * b ) / 100;
		    	BY = (-30 * r - 59 * g + 89 * b ) / 100;
		    	Y  = ( 30 * r + 59 * g + 11 * b ) / 100;
		    	RYY = ( S * BY + C * RY ) / 256;
		    	BYY = ( C * BY - S * RY ) / 256;
		    	GYY = (-51 * RYY - 19 * BYY ) / 100;
		    	R = Y + RYY;
		    	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 );
		    	pix[index] = 0xff000000 | (R << 16) | (G << 8 ) | B;
	    	}
	    
	    Bitmap outBitmap = Bitmap.createBitmap(width, height, src.getConfig());	   
	    outBitmap.setPixels(pix, 0, width, 0, 0, width, height);
	   
	    pix = null;
	   
	    return outBitmap;
	}
}

This is a quite complicated algorithm, yet I don’t really understand it clearly.
I’ve found it at this link Developer.Com – Working with images in Google Android

Another tinted one, nice one:

Nice Tinted Image

Nice Tinted Image

Have fun with it!

Cheers,

Pete Houston

Categories: Tutorials Tags: , , , , ,
  1. Hardik Vaghasiya
    January 1, 2015 at 5:30 pm

    how can i use this mathod….
    Please send me full project.

  2. rameez@hotmail.com
    November 4, 2013 at 9:51 pm

    Thanks its working…

  3. Luser
    November 19, 2012 at 3:43 am

    Can u tell me, how to prevent OutOfMemory Exception ? I tried every sample in Internet, but nothing helps.

    Your using… Bitmap.create(widht, height, ….) which causes a memory exception.

    Thanks for help

  4. August 29, 2012 at 2:22 pm

    Hi Pete. Can you explain to me what is “degree” in the line 12. Thanks 🙂

  5. Rahul
    June 24, 2012 at 1:18 pm

    its not working..

  6. Sam
    May 4, 2012 at 5:39 pm

    Hi Pete,
    it is hard to understand this code as you said it is complicated algorithm. It would be great if you add some documentation for variables and some proper naming conventions, so that at least we can understand the responsibility of each variable.

    Thanks,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: