![]() |
| Gamma (red=1.0,green=1.0,yellow=1.0) |
Create new Android Project
Project Name: PlayingwithBitmaps
Build Target: Android 2.3.3 //or greater than that
Application Name: PlayingwithBitmaps
Package Name: com.hamad.playingwithbitmaps
Create Activity: Main
Min SDK: 10 // or greater than that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:id="@+id/rlMain" >
<ImageView
android:id="@+id/imViewAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/beautiful" />
</RelativeLayout>
2. code of main activity:
package com.shaikhhamadali.blogspot.playingwithbitmaps;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.widget.ImageView;
public class Main extends Activity {
ImageView imViewAndroid;@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
imViewAndroid.setImageBitmap(setGamma(BitmapFactory.decodeResource(getResources(), R.drawable.android_droid),0.8,0.8,0.8));
}
public static Bitmap setGamma(Bitmap src, double red, double green, double blue) {
// create output image
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
// get image size
int width = src.getWidth();
int height = src.getHeight();
// color information
int A, R, G, B;
int pixel;
// constant value curve
final int MAX_SIZE = 256;
final double MAX_VALUE_DBL = 255.0;
final int MAX_VALUE_INT = 255;
final double REVERSE = 1.0;
// gamma arrays
int[] gammaR = new int[MAX_SIZE];
int[] gammaG = new int[MAX_SIZE];
int[] gammaB = new int[MAX_SIZE];
// setting values for every gamma channels
for(int i = 0; i < MAX_SIZE; ++i) {
gammaR[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / red)) + 0.5));
gammaG[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / green)) + 0.5));
gammaB[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / blue)) + 0.5));
}
// apply gamma table
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);
// look up gamma
R = gammaR[Color.red(pixel)];
G = gammaG[Color.green(pixel)];
B = gammaB[Color.blue(pixel)];
// set new color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
}
There are various computations on Gamma correction, however, I just pick up and implement the simplest one as mentioned on wikipedia..
3. note that:
- You might want to refer about gamma correction on Wikipedia.
- with the help of this,setGamma() method you can set the gamma of image on click,on action_down etc
4. conclusion:
- some deep information about gamma.
- Know how to set gamma of an image bitmap from drawables.
5. About the post:
- The code seems to explain itself due to comments, and is very easy to understand.
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code here
Cheers,
you can download the source code here
Hamad Ali Shaikh




