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 🙂

'Realtime' raytracing on j2me

I have been playing a bit this afternoon with my old raytracer and decided to wrote a a small implementation in j2me.. yes.. it sounds completely useless but it was quite fun to remember the old days.
It’s a very basic raytracer, it only supports spheres and planes and then some hard shadows and reflections. I haven’t done any optimitzation, for example checks if the camera ray intersects all objects for every single pixel and, obviously, it runs pretty slow on the real device (even on the emulator..)

Just three screenshots of the ‘realtime’ (ehem..) raytracing:

rt - screenshot 1 rt - screenshot 2
rt - screenshot 3

A long time ago..

Yes.. it has been a long time since last post.. I have been a bit busy (ok.. not the whole 8 months) moving to the Netherlands. Since June-08 I’m living and working in Enschede, a nice city in the east border of the Netherlands.

Some images of the recent snows (november):

Enschede

Enschede

Enschede

As a bonus I’ll include video captures of some applications I did for FuturLink:

http://labs.rafols.org/flvplayer.swf?file=futurlink_mwc.flv&autoStart=false

Soon I will publish some videos / shots of my work here at Service2Media

NFC Competition

My company has been selected finalist for the NFC Forum Global Competition – Touching the Future with another nine world-wide companies in the “The Best NFC Service of the Year 2008″. The winners will be announced next tuesday (April 29).
I designed and developed the J2ME client for Nokia 6131 NFC which allows you to interact with one of our Touch Screens (B200TS) via NFC and Bluetooth. I will wait until the competition is over to post more details and some screenshots and a video of the application.

Move along, nothing to see here..

In this blog I’ll post the experiences of working developing J2ME applications. It will be mainly focused on the graphical part and how the user may interact with them although there will be non-interactive applications like the one I’m introducing below.
This was my first attempt to do a 4k intro with J2ME. I’m not proud of the result, but it was made only in one afternoon to increase dramatically the number of compo entries (from 1 to 2). The binary application requires a J2ME Phone with MMAPI + JSR 184 support. In order to install the application you have to send the mtro.jar file to your phone through bluetooth / cable / irda.

mtro

Binary