tags:

views:

88

answers:

4

What's the shortest solution in c/c++?

+2  A: 

You didn't give too much information so I will go with StretchBlt

For an example, see Scaling an Image.

Shay Erlichmen
Can you provide a demo using `StretchBlt` to rescale?
icurl
The example doesn't qualify as **the shortest solution** IMO.
ollydbg
A: 

I won't give you a demo, but try to do the following:

  • create destination bitmap that is of your desired size
  • select that bitmap into device context
  • StretchBlt original bitmap onto device context previously mentioned
  • unselect bitmap from the device context

That recipe above needs no any library then GDI that is already present in windows. And if you plan to draw something in c++, you should get familiarity with that library anyway.

Look here: http://www.ucancode.net/Free-VC-Draw-Print-gdi-example-tutorial/GDI-Object-VC-MFC-Tutorial.htm

or here: http://www.olivierlanglois.net/clover.html

if you don't plan to use MFC for the task.

Daniel Mošmondor
I'll never use MFC,too big
icurl
well then, you'll have to use native GDI functions for that
Daniel Mošmondor
Do you have a simplified example?
icurl
A: 

One of the easiest rescale algorithms is nearest-neighbour. Suppose your are rescaling from an image in an array of size x1 y1 to another size x2 y2. The idea is to find the nearest integer offset in original array to each target array position. So your rescale algorithm ends for something like this:

const int x1 = 512;
const int y1 = 512;
const int x2 = 64;
const int y2 = 64;
unsigned char orig[x1*y1]; /* Original byte array */
unsigned char target[x2*y2] /* Target byte array */
for(int i=0;i<x2;i++)
{
    for(int j=0;j<y2;j++)
    {
       xoff = (i*x2)/x1;
       yoff = (j*y2)/y1;
       target[i+j*x2] = orig[xoff+yoff*x1]
    }
}

This will give a blocky resized image. For better results you can use average or any other fancier polynomial based interpolators.

whatnick
A: 

What libraries are you using? How do you represent images? Most image libraries should already be able to do that, e.g. Qt has QPixmap with scaled() and GDI has StretchBlt.

Or you could code it yourself with bicubic interpolation.

Sebastian N.