transform the components in each pixel according to the transform tables to Java Micro Edition Image. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Pixel

Description

transform the components in each pixel according to the transform tables to Java Micro Edition Image.

Demo Code

/*/*from   w w  w  . j a  va  2  s .  co m*/
 * This file is a part of the TUBE42 imagelib, released under the LGPL license.
 *
 * Development page: https://github.com/tube42/imagelib
 * License:          http://www.gnu.org/copyleft/lesser.html
 */
import javax.microedition.lcdui.*;

public class Main{
    /**
     * transform the components in each pixel according to the 
     * transform tables.
     * 
     * If a table is null, the original value will be used
     */
    public static Image transformARGB(Image image, byte[] ta, byte[] tr,
            byte[] tg, byte[] tb) {
        // 1. get blended image:
        int w = image.getWidth();
        int h = image.getHeight();

        int[] buffer = new int[w * h];

        image.getRGB(buffer, 0, w, 0, 0, w, h);
        image = null; // not needed anymore

        int[] tmp = new int[256];
        // apply A, R, G and B tables when available

        if (ta != null) {
            for (int i = 0; i < 256; i++)
                tmp[i] = (((int) ta[i]) & 0xFF) << 24;

            for (int i = 0; i < buffer.length; i++)
                buffer[i] = (buffer[i] & 0x00FFFFFF)
                        | tmp[(buffer[i] >> 24) & 0xFF];
        }

        if (tr != null) {
            for (int i = 0; i < 256; i++)
                tmp[i] = (((int) tr[i]) & 0xFF) << 16;

            for (int i = 0; i < buffer.length; i++)
                buffer[i] = (buffer[i] & 0xFF00FFFF)
                        | tmp[(buffer[i] >> 16) & 0xFF];
        }

        if (tg != null) {
            for (int i = 0; i < 256; i++)
                tmp[i] = (((int) tg[i]) & 0xFF) << 8;

            for (int i = 0; i < buffer.length; i++)
                buffer[i] = (buffer[i] & 0xFFFF00FF)
                        | tmp[(buffer[i] >> 8) & 0xFF];
        }
        if (tb != null) {
            for (int i = 0; i < 256; i++)
                tmp[i] = (((int) tb[i]) & 0xFF) << 0;

            for (int i = 0; i < buffer.length; i++)
                buffer[i] = (buffer[i] & 0xFFFFFF00)
                        | tmp[(buffer[i] >> 0) & 0xFF];
        }

        return Image.createRGBImage(buffer, w, h, true);
    }
    public static Image transformARGB(Image image, int delta_alpha,
            int delta_red, int delta_green, int delta_blue) {
        // build transformation tables
        byte[] ta = new byte[256];
        byte[] tr = new byte[256];
        byte[] tg = new byte[256];
        byte[] tb = new byte[256];
        for (int i = 0; i < 256; i++) {
            ta[i] = (byte) Math.min(255, Math.max(0, i + delta_alpha));
            tr[i] = (byte) Math.min(255, Math.max(0, i + delta_red));
            tg[i] = (byte) Math.min(255, Math.max(0, i + delta_green));
            tb[i] = (byte) Math.min(255, Math.max(0, i + delta_blue));
        }
        // now, transform it according to the tables
        return transformARGB(image, ta, tr, tg, tb);
    }
}

Related Tutorials