Will Perone

This function scales images using an optimized form of bresenham's formula. You can also add bilinear sampling and such to it but I figured since it's on a cell phone it won't matter much and will also cause it to be slower.
//Code originally written in 2007
public static Image scaleImage(Image original, int newWidth, int newHeight)
        int[] rawInput = new int[original.getHeight() * original.getWidth()];
        original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
        int[] rawOutput = new int[newWidth*newHeight];        

        // YD compensates for the x loop by subtracting the width back out
        int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth(); 
        int YR = original.getHeight() % newHeight;
        int XD = original.getWidth() / newWidth;
        int XR = original.getWidth() % newWidth;        
        int outOffset= 0;
        int inOffset=  0;
        for (int y= newHeight, YE= 0; y > 0; y--) {            
            for (int x= newWidth, XE= 0; x > 0; x--) {
                rawOutput[outOffset++]= rawInput[inOffset];
                if (XE >= newWidth) {
                    XE-= newWidth;
            inOffset+= YD;
            YE+= YR;
            if (YE >= newHeight) {
                YE -= newHeight;     
        return Image.createRGBImage(rawOutput, newWidth, newHeight, false);        
cristian 89 0
very good, indeed
søren 2 0
Hi, this is THE fastest image scaling algorithm i have ever found for J2ME - works super.
Vlado 1 0
Yes, very good algorithm.
lite tisuko yar 1 0
cha antha bagundha, veedu kuda yekkado copy kotti paste chesinde ga, yendukanta pogaddam
GL 1 0
It doesn't work on HTC 3700
Nas 1 0
Great function
Rajiv 1 0
Its efficient taking time considerations...great work
logan 1 0
hey man..its a very handy code.. great work bro..
i get a memory out of bound problem when using larger images i mean one with higher resolution.
Will 2 1
What's most likely happening in that case is that width*height is overflowing; if that specific phone treats ints as 16bit then you can only have a 256x256 image before it overflows 6k when allocating the image.
Will 1 0
oops that should be 64k, not 6k
Chris 1 0
I've worked up a bilinear version of this, would love your feedback.

Felix 1 0
Thanks ... nice pice of work!
name 16 0
thanks a lot
Gangulu 0 0
Nice work; Thanks
0 0
John 0 0
How should I call this function? And where should I tell my picture location?

Kevin 0 0
thanks alot
Elli 0 0
GJeeeeeeeeeez man thx a lot, i wasted 2 days searching for this kind of algorithm, great help!
Victor 0 0
Great work, this function is much faster than the one i have and it works with tranparent images,thank you.
Boris 1 0
This is the algorithm I was looking for, but unfortunately it's not working. All I get is black squares even if I pass the same size as scale parameters |(
Vikram 2010/10/260 0
Here is an example http://codeexample.blogspot.com/2010/10/scaling-image-in-j2me.html
but i think transparency will be lost through this
ignacioroda 2011/04/010 0
Wouldn't it be even faster if you change this:

per something like

and adding before the loop:
Will 2011/04/010 0
Yes you are correct, thanks for the feedback :)
Praveen 2011/04/260 0
Very Very good program .....
Alex 2011/07/0222 0
Thank you very much !
you saved my lazy excuse of a programmer a_s :) .
Tory 2011/07/06 Contact Me0 0
IMHO you've got the right aswenr!
2011/10/090 0
works perfect..!!!
p 2012/02/030 0
guy u 2 much
ant 2012/03/200 0
thank . . that's sweet
tony 2012/04/02 Contact Me0 0
Hi there,

Where did you get this algorithm from? Did you create it? I wanted to know if it is copyrighted or GPLed. I would like to use it at my job. Please contact me and let me know.

Will 2012/04/020 0
I based it off of Bresenham's formula for scaling but did some work to optimize it. I know many people have been asking me about what license to put these under, I'll have to look at the options MIT, GPL etc...
Vuxzu 2012/08/100 0
Thanks A LOT works great!
Tony 2012/10/11 Contact Me1 0
Did you ever put this under any license? I would love to use this algorithm. Thanks.
land 2013/01/17 Contact Me6 2
le le muh me, kam nhi hua...
rob 2013/04/040 0
works awesome, thank you
Will 2013/12/21 Contact Me0 0
Just to clarify; this code is entirely free to use without restriction though I appreciate being mentioned when you use it ;)
dan 2014/05/290 0
the old link for chris's bilinear version does not work. i think this is the new one:

<- for private contact