Image Rotation in j2me

Lately I’ve been creating some low level image functions for Java ME just to see if low level bitmap manipulation was way too slow for doing it on real time on Java ME or it was, at least, usable. I’ll publish more functions but I’ll start with Image Rotation.

In Java ME is easy to rotate images if you want to rotate by an angle multiple of 90° but it doesn’t provide any mechanism to rotate it by an arbitrary angle (yes, ok… you could do the same using the Mobile 3D API)

I created a small function that fills that gap and allows image rotation by any angle. The resulting image will have the same size as the original (watch out for the corners..)

public static void rotateImage(Image src, float angle, Graphics g) {
    int sw = src.getWidth();
    int sh = src.getHeight();
    int[] srcData = new int[sw * sh];

    src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
    int[] dstData = new int[sw * sh];

    double rads = angle * Math.PI / 180.f;
    float sa = (float) Math.sin(rads);
    float ca = (float) Math.cos(rads);
    int isa = (int) (256 * sa);
    int ica = (int) (256 * ca);

    int my = - (sh >> 1);
    for(int i = 0; i > 1);
        for(int j = 0; j > 8;
            int srcy = (-mx * isa + my * ica) >> 8;

            srcx += sw >> 1;
            srcy += sh >> 1;

            if(srcx < 0) srcx = 0;
            if(srcy  sw - 1) srcx = sw - 1;
            if(srcy > sh - 1) srcy = sh - 1;

            dstData[j + i * sw] = srcData[srcx + srcy * sw];

            mx++;
        }
        my++;
    }

    g.drawRGB(dstData, 0, sw, 0, 0, sw, sh, true);
}

If we move all the calculations that doesn’t need to be done in the inner loop to the external loop we will have a speed improvement:

public static void rotateImage(Image src, float angle, Graphics g) {
    int sw = src.getWidth();
    int sh = src.getHeight();
    int[] srcData = new int[sw * sh];

    src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
    int[] dstData = new int[sw * sh];

    double rads = angle * Math.PI / 180.f;
    float sa = (float) Math.sin(rads);
    float ca = (float) Math.cos(rads);
    int isa = (int) (256 * sa);
    int ica = (int) (256 * ca);

    int my = - (sh >> 1);
    for(int i = 0; i > 1) * ica + ((sw >> 1) <> 1) * isa + ((sh >> 1) << 8);

        for(int j = 0; j > 8);
            int srcy = (yacc >> 8);

            if(srcx < 0) srcx = 0;
            if(srcy  sw - 1) srcx = sw - 1;
            if(srcy > sh - 1) srcy = sh - 1;

            dstData[wpos++] = srcData[srcx + srcy * sw];

            xacc += ica;
            yacc -= isa;
        }
        my++;
    }

    g.drawRGB(dstData, 0, sw, 0, 0, sw, sh, true);
}

And if we know beforehand the size of the image we want to rotate we could do some extra tricks (for example, here assumes the source image will be 256×256 pixels and will only work with that resolution):

public static void rotateImage(Image src, float angle, Graphics g) {
    int sw = src.getWidth();
    int sh = src.getHeight();
    int[] srcData = new int[sw * sh];

    src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
    int[] dstData = new int[sw * sh];

    double rads = angle * Math.PI / 180.f;
    float sa = (float) Math.sin(rads);
    float ca = (float) Math.cos(rads);
    int isa = (int) (256 * sa);
    int ica = (int) (256 * ca);

    int my = - (sh >> 1);
    for(int i = 0; i > 1) * ica + ((sw >> 1) <> 1) * isa + ((sh >> 1) << 8);

        for(int j = 0; j > 8) & 0xff;
            int srcy = yacc & 0xff00;

            dstData[wpos++] = srcData[srcx + srcy];

            xacc += ica;
            yacc -= isa;
        }
        my++;
    }

    g.drawRGB(dstData, 0, sw, 0, 0, sw, sh, true);
}

These functions paints the rotated image in a Graphics object directly, but doing some minor changes it could generate a new Image with the content:
* Change the function declaration to:

public static Image rotateImage_img(Image src, float angle) {

* Replace the drawRGB call with:

return Image.createRGBImage(dstData, sw, sh, true);

Feel free to use it for whatever you want but it would be nice if you drop me a line and put me somewhere in the credits 🙂

5 thoughts on “Image Rotation in j2me”

  1. Hey many thanks for this….
    I was thinking in the same direction for rotation but I had some doubts in calculation. Thanks for this calculations.

    I’m doing animations in J2ME 2D. This will be very helpful in my future work.

    Let’s keep in touch and share each others UI Development experience in J2ME 🙂

    Like

  2. how well does this work with non-square images – like a typical image of 4:3 ratio – say 320×240 – will it return an image that is 240×320? thanks!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s