Resizing image according to form algorithm?

Is there any good algorithm to change the

size of an image, when u change size on the form.


The image shold remain with the same proportional, and

still use max of the size of the form area. Depending

on what is smaller, the form height or the form width...


Any good ideas...or source codeexampel is welcome...

Thanks!

mathias


Comments

  • : Is there any good algorithm to change the

    : size of an image, when u change size on the form.


    : The image shold remain with the same proportional, and

    : still use max of the size of the form area. Depending

    : on what is smaller, the form height or the form width...




    I'm not sure what you mean by 'form' in this context, so I'll be general.


    I'll presume that at some point you are getting some form of 'resize' message for a window or something, and that from that you can extract width and height. I'll assume that you have some rectangular region with width and height w1 and h1 respectively that you wish to glue an image into that has width and height w2 and h2.


    I'll next assume that all you want to do is compute a new width and height for the image that enables the image to fit in the rectangular region maximally without overflowing the rectangular region. Further, I'll assume that the image is not to be rotated in any way and that the aspect ratio of the image is unchanged (not squashed).


    You need to essentially know how much to scale both the height and the width of the image. If the same scale value is applied to both, then the image won't get stretched or squashed. There are two such scales to choose from:


    scale1=w2/w1;

    scale2=h2/h1;


    Note that the divisions have to be done as real numbers and stored as real numbers, and not as integers, or rounding will take place in a lot of languages. One will be bigger and the other smaller (or possibly equal). If one is bigger, use that one.


    if (scale1>scale2)

    scale=scale1;

    else

    scale=scale2;


    The width and height of the image must be multiplied by the computed real value 'scale'. Depending on how your image boundaries are represented, it may require changing the location of the upper left or bottom right in order to get the image to the right size. If the bottom right needs changing, then:


    newbottom=oldtop+(oldbottom-oldtop)*scale;

    newright=oldleft+(oldleft-oldright)*scale;


    This will move the bottom right point in such a way that the width and height are correct. Similar mechanisms can be done to move the top left, or center the image, if you need to do any of this by hand.


    If you have a CopyImage or DrawImage routine that you can specify the boundaries to, you're done. If you can simply adjust the picture boundary values and invalidate the image so that it's redrawn by the operating system at its new scale, you're also done. If you actually need an algorithm for copying the image from one size rectangle to another, stay tuned.


    To do that, you need to know the width and height of the destination rectangle (w1,h1) and the width and height of the source image rectangle (w2,h2). I'll assume you can do a c=picture.GetPixel(x,y) to get the pixel of colour c at coordinate x and y in the picture, and that you can do a form.PutPixel(x,y,c) to put a pixel of colour c at coordinate x and y in the form rectangle.


    You'd do something like the following in pseudocode, which will place the image at the top left corner. Keep in mind that all mathematical operations are done in floating point.


    scale1=w2/w1;

    scale2=h2/h1;

    aspectratio=w2/h2;


    if (scale1>scale2)

    for y=0 to w1/aspectratio - 1 step 1

    picy=y*scale1;

    for x=0 to w1 - 1 step 1

    picx=x*scale1;

    c=picture.GetPixel(picx,picy);

    form.PutPixel(x,y,c);

    end for

    end for

    else

    for y=0 to h1 - 1 step 1

    picy=y*scale2;

    for x=0 to h1*aspectratio - 1 step 1

    picx=x*scale2;

    c=picture.GetPixel(picx,picy);

    form.PutPixel(x,y,c);

    end for

    end for

    end if


    The basic principle is: for every pixel in the form rectangle, compute where that pixel must come from in the image by scaling the form rectangle point (x,y) in the loops to a picture point (picx,picy).


    You may have to make offset adjustments if the coordinate system doesn't start with (0,0) in the upper left corner.


    You will also be able to make this faster if you can get rid of the floating point operations or shuffle common computations around a bit. I didn't bother here because I don't really have time. There may be a few bugs in the code, just to make life interesting. I haven't actually compiled any of this in any language.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories