colorize Image micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

colorize Image micro edition

Demo Code


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Main{
    public static Image colorizeImage(Image sourceImage, int prcAlpha) {
        int imageWidth = sourceImage.getWidth();
        int imageHeight = sourceImage.getHeight();
        int[] rgbData = new int[imageWidth * imageHeight];
        sourceImage.getRGB(rgbData, 0, imageWidth, 0, 0, imageWidth,
                imageHeight);/*  ww  w  .java  2s. c o m*/
        int currY, a, r, g, b, currPoint;
        for (int y = 0; y < imageHeight; y++) {
            currY = y * imageWidth;
            for (int x = 0; x < imageWidth; x++) {
                currPoint = rgbData[currY + x];
                a = currPoint & 0xff000000;
                r = currPoint & 0xff;
                g = currPoint & 0xff00;
                b = currPoint & 0xff0000;

                if (a == 0xff000000) {
                    a = 256;
                } else {
                    a >>= 24;
                }
                b >>= 16;
                g >>= 8;
                a -= (255 * prcAlpha) / 100;
                if (a < 0) {
                    a = 0;
                }
                if (a >= 256) {
                    a = 256;
                }
                if (a >= 256) {
                    a = 0xff000000;
                } else {
                    a <<= 24;
                }
                b <<= 16;
                g <<= 8;

                rgbData[currY + x] = a | r | g | b;
            }
        }
        return Image.createRGBImage(rgbData, imageWidth, imageHeight, true);
    }
}

Related Tutorials