apply filter to the Java Micro edition image. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

apply filter to the Java Micro edition image.

Demo Code

/*/*from   ww  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{
    /**
     * apply filter of size filter_w * filter_h to the image.
     * 
     * This function will process one line at a time
     */
    public static Image applyFilter(Image image, int filter_w,
            int filter_h, ImageFilter filter) {
        // 0. sanity check: filter dimensions must be odd
        if ((filter_w & filter_h & 1) == 0)
            return null; // boolean haxory ftw

        int fw2 = filter_w / 2;
        int fh2 = filter_h / 2;

        // 1. allocate needed buffers & output image
        final int w = image.getWidth();
        final int h = image.getHeight();
        final int scanlength = w + filter_w;

        Image ret = Image.createImage(w, h);
        Graphics g = ret.getGraphics();
        int[] buffer_out = new int[w];
        int[][] buffer_in = new int[filter_h][];

        for (int i = 0; i < buffer_in.length; i++)
            buffer_in[i] = new int[scanlength];

        int next_line = 0;
        // 2. get initial pixels and empty surroundings
        for (int i = 0; i < filter_h; i++) {
            for (int j = 0; j < scanlength; j++)
                buffer_in[i][j] = 0;
            if (i >= fh2)
                image.getRGB(buffer_in[i], fw2, w, 0, next_line++, w, 1);
        }

        // 3. for each row, compute new line, write it to image and then get the next line
        for (int y = 0; y < h; y++) {
            filter.apply(buffer_in, buffer_out, fw2, w, y);
            g.drawRGB(buffer_out, 0, w, 0, y, w, 1, true);

            // buffer rotation instead of copying
            int[] tmp = buffer_in[0];
            for (int i = 1; i < filter_h; i++)
                buffer_in[i - 1] = buffer_in[i];
            buffer_in[filter_h - 1] = tmp;

            // and add the next line (if any)
            if (next_line < h)
                image.getRGB(tmp, fw2, w, 0, next_line++, w, 1);
            else
                for (int i = 0; i < scanlength; i++)
                    tmp[i] = 0;
        }
        return ret;
    }
    /**
     * apply 2D filter
     * 
     * NOTE: this function is currently quite slow!
     * 
     */
    public static Image applyFilter(Image image, final int[][] multiplier,
            final int divider) {

        final int w = multiplier[0].length;
        final int h = multiplier.length;

        return applyFilter(image, w, h, new ImageFxUtils.ImageFilter() {
            public void apply(int[][] pixels, int[] output, int offset,
                    int count, int y_) {
                for (int i = 0; i < count; i++) {
                    int sum_a = 0, sum_r = 0, sum_g = 0, sum_b = 0;

                    // TODO 1: speed up computation by using the SIMD trick here
                    // TODO 2: speed up computation by extracting each point only once?
                    for (int y = 0; y < h; y++) {
                        final int[] line = pixels[y];
                        final int[] mul = multiplier[y];
                        for (int x = 0; x < w; x++) {
                            sum_a += mul[x] * ((line[x + i] >> 24) & 0xFF);
                            sum_r += mul[x] * ((line[x + i] >> 16) & 0xFF);
                            sum_g += mul[x] * ((line[x + i] >> 8) & 0xFF);
                            sum_b += mul[x] * ((line[x + i] >> 0) & 0xFF);
                        }
                    }
                    output[i] = Math.min(255, Math.max(0, sum_a / divider)) << 24
                            | Math.min(255, Math.max(0, sum_r / divider)) << 16
                            | Math.min(255, Math.max(0, sum_g / divider)) << 8
                            | Math.min(255, Math.max(0, sum_b / divider));
                }
            }
        });
    }
}

Related Tutorials