resize Image Smooth Proportional micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

resize Image Smooth Proportional micro edition

Demo Code


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

public class Main{
    public static Image resizeImageSmoothProportional(Image sourceImage,
            int prcSize) {
        if (prcSize > 100) {
            prcSize = 100;/*from w  w w.  jav  a2  s  .com*/
        } else if (prcSize <= 0) {
            return Image.createImage(1, 1);
        }
        int imageWidth = sourceImage.getWidth();
        int imageHeight = sourceImage.getHeight();
        int destWidth = imageWidth * prcSize / 100;
        int destHeight = imageHeight * prcSize / 100;
        /** Buffer **/
        int[] rgbData = new int[imageWidth * imageHeight];
        /** Fill buffer **/
        sourceImage.getRGB(rgbData, 0, imageWidth, 0, 0, imageWidth,
                imageHeight);
        int[] lines = new int[destWidth * imageHeight];
        int[] columns = new int[destWidth * destHeight];
        /** Fast **/
        if (destWidth < imageWidth) {
            for (int k = 0; k < imageHeight; k++) { // trough all lines
                int i = k * imageWidth; // index in old pix
                int j = k * destWidth; // index in new pix
                int part = destWidth;
                int addon = 0, r = 0, g = 0, b = 0, a = 0;
                for (int m = 0; m < destWidth; m++) {
                    int total = imageWidth;
                    int R = 0, G = 0, B = 0, A = 0;
                    if (addon != 0) {
                        R = r * addon;
                        G = g * addon;
                        B = b * addon;
                        A = a * addon;
                        total -= addon;
                    }
                    while (0 < total) {
                        a = (rgbData[i] >> 24) & 0xff;
                        r = (rgbData[i] >> 16) & 0xff;
                        g = (rgbData[i] >> 8) & 0xff;
                        b = rgbData[i++] & 0xff;
                        if (total > part) {
                            R += r * part;
                            G += g * part;
                            B += b * part;
                            A += a * part;
                        } else {
                            R += r * total;
                            G += g * total;
                            B += b * total;
                            A += a * total;
                            addon = part - total;
                            lines[j++] = ((R / imageWidth) << 16)
                                    | ((G / imageWidth) << 8)
                                    | (B / imageWidth)
                                    | ((A / imageWidth) << 24); // A??
                        }
                        total -= part;
                    }
                }
            }
        } else { /// destWidth > imageWidth
            int part = imageWidth;
            for (int k = 0; k < imageHeight; k++) { // trough all lines
                int i = k * imageWidth; // index in old pix
                int j = k * destWidth; // index in new pix
                int total = 0;
                int r = 0, g = 0, b = 0, a = 0;
                for (int m = 0; m < destWidth; m++) {
                    int R = 0, G = 0, B = 0, A = 0;
                    if (total >= part) {
                        R = r * part;
                        G = g * part;
                        B = b * part;
                        A = a * part;
                        total -= part;
                    } else {
                        if (0 != total) {
                            R = r * total;
                            G = g * total;
                            B = b * total;
                            A = a * total;
                        }
                        a = (rgbData[i] >> 24) & 0xff;
                        r = (rgbData[i] >> 16) & 0xff;
                        g = (rgbData[i] >> 8) & 0xff;
                        b = rgbData[i++] & 0xff;
                        int addon = part - total;
                        R += r * addon;
                        G += g * addon;
                        B += b * addon;
                        A += a * addon;
                        total = destWidth - addon;
                    }
                    // set new pixel
                    lines[j++] = ((R / imageWidth) << 16)
                            | ((G / imageWidth) << 8) | (B / imageWidth)
                            | ((A / imageWidth) << 24); // A??
                }
            }
        }
        if (destHeight < imageHeight) {
            for (int k = 0; k < destWidth; k++) { // trough columns
                int i = k; // index in lines pix
                int j = k; // index in new pix
                int part = destHeight;
                int addon = 0, r = 0, g = 0, b = 0, a = 0;
                for (int m = 0; m < destHeight; m++) {
                    int total = imageHeight;
                    int R = 0, G = 0, B = 0, A = 0;
                    if (addon != 0) {
                        R = r * addon;
                        G = g * addon;
                        B = b * addon;
                        A = a;//*addon;
                        total -= addon;
                    }
                    while (0 < total) {
                        //            a = (lines[i] >> 24) & 0xff;// may no rotate
                        a = lines[i] & 0xff000000;
                        r = (lines[i] >> 16) & 0xff;
                        g = (lines[i] >> 8) & 0xff;
                        b = lines[i] & 0xff;
                        i += destWidth;
                        if (total > part) {
                            R += r * part;
                            G += g * part;
                            B += b * part;
                            A += a;//*part;
                        } else {
                            R += r * total;
                            G += g * total;
                            B += b * total;
                            A += a;//*total;
                            addon = part - total;
                            ///set new pixel
                            if (0 != A) {
                                columns[j] = ((R / imageHeight) << 16)
                                        | ((G / imageHeight) << 8)
                                        | (B / imageHeight) | 0xff000000; // A??
                            } else {
                                columns[j] = 0;
                                //((R/imageHeight)<<16)
                                //|((G/imageHeight)<<8)
                                //|(B/imageHeight); // A??
                            }
                            j += destWidth;
                        }
                        total -= part;
                    }
                }
            }
        } else {
            int part = imageHeight;
            for (int k = 0; k < destWidth; k++) { // trough all lines
                int i = k; // index in old pix
                int j = k; // index in new pix
                int total = 0;
                int r = 0, g = 0, b = 0, a = 0;
                for (int m = 0; m < destHeight; m++) {
                    int R = 0, G = 0, B = 0, A = 0;
                    if (total >= part) {
                        R = r * part;
                        G = g * part;
                        B = b * part;
                        A = a;//*part;
                        total -= part;
                    } else {
                        if (0 != total) {
                            R = r * total;
                            G = g * total;
                            B = b * total;
                            A = a;//*total;
                        }
                        //            a = (lines[i] >> 24) & 0xff;// may no rotate
                        a = lines[i] & 0xff000000;
                        r = (lines[i] >> 16) & 0xff;
                        g = (lines[i] >> 8) & 0xff;
                        b = lines[i] & 0xff;
                        i += destWidth;
                        int addon = part - total;
                        R += r * addon;
                        G += g * addon;
                        B += b * addon;
                        A += a;//*addon;
                        total = destHeight - addon;
                    }
                    // set new pixel
                    if (0 != A) {
                        columns[j] = ((R / imageHeight) << 16)
                                | ((G / imageHeight) << 8)
                                | (B / imageHeight) | 0xff000000; // A??
                    } else {
                        columns[j] = 0;
                        //((R/imageHeight)<<16)
                        //|((G/imageHeight)<<8)
                        //|(B/imageHeight);
                    }
                    j += destWidth;
                }
            }
        }
        return Image.createRGBImage(columns, destWidth, destHeight, true);
    }
}

Related Tutorials