Link to home
Start Free TrialLog in
Avatar of TKD
TKD

asked on

How to scale image from range 50%~200%

Hi,experts:
I would like to scale image.
The scale factor will lie within the range 50%~200%.
What shall I do???
Thanks a lot!
Avatar of weed
weed
Flag of United States of America image

What program? What platform? Gotta give us some info here.

You do realize that ANY time you resize a raster image, you WILL lose quality...right? The more you change the size, the more you lose.
Avatar of TKD
TKD

ASKER

Program language: C
Platform: Win XP
I know I will lose QUALITY if I resize a raster image.
May be you should try the programming discussions.
The best way is to use the freeware IrfanView.
http://www.tucows.com/preview/194967.html
this freeware program has the best interpolation method ever create "Lanczos". Not even Photoshop CS can increase the size of images with that quality.

Test for yourself. I have tested all know methods and plugins for Photoshop for a magazine article and Irfanview beat them all (genuine fractals, PhotoZoom, etc.).

Open the program and the image, go to menu
Image > Resize/Resample
image filther method = Lanczos
set new size and voila.

Avatar of TKD

ASKER

Thanks...
I would like to write the method by using C source code.
Is there any articles or source codes referenced?
Thanks again.
Well, if you're using C then you're going to have to first figure out how to analyse a raster file so that you can access individual pixels of know co-ordinates. Then, you need to figure out some plain old maths for rescaling and interpolation, and write a routine to do this. Finally, you need to put it all back into a new file.

But I still think you're barking up the wrong tree asking in this forum. Try putting a link pointing to this question in the programming discussion here at EE, and try other sites outside EE like Tek-tips. There are plenty of them around.
LIke Jason said, you need to follow a few steps:

(1)  Get the imageinto memory as an array of pixels.  If you're reading a GIF or JPEG file, you need some library routine to do the reading and decoding into pixels.  It's too hard to do this yourself.  See any big graphics library, they allhave this ability.

(2)  If your scale factor is less than 100%, you need a loop that steps along and generates the required number of pixels.  There are many ways of mapping the intermediate pixel locations to the new pixel, the most obvious ways are:  (a) Take the nearest pixel, (b)  Take the average of the two nearest pixels, (c) Better: take the weighted average of the two pixels.  Example: You want to shirink a 100 pixel wide picture to 66%. So you want to step 1.0 / 0.66 pixels each time.  The first pixel position is 1/0.66 or 1.515.  So take 51% of the second pixel and 49% of the first pixel, that's your  output pixel.  Continue on for all 66 output pixels.  Kinda time consuming but good results.   And oh, you should really be doing this in TWO dimensions, so you shoul dnot only go across 1.515 pixels, but DOWN 1.515 pixels also, and do the average of the four or more surrounding pixels.

(3)  If you're expanding the image, you do the same as (2) except turned inside out.  Each output pixel is going to get its info from less than one pixel.   Even tricier to do right..

(4)  Ideally when you do this kind of "resampling", you should run the input and output pictures thru a spatial frequency-domain filter to filter out all kinds of beats and moire patterns that the resampling tends to produce.  This is waaaaay beyond what we can describe in this tiny little box.

Good luck!


-----------

OH, BY THE WAY, IF YOURE DOING THIS ON A NICE OPERATING SYSTEM, many HAVE API calls like StretchBits(), or CopyBits(), or BitBlt() or something similar THAT DOES ALL THIS FOR YOU!
If you're not required to do it all yourself, do think about using an existing routine to do this.  it will save you months of work!






Hi,
Maybe You will find it interesting:
Quick image scaling by 2

http://www.compuphase.com/graphic/scale2.htm

And this

[1] Riemersma, Thiadmer; "Quick and smooth image scaling with Bresenham"; Dr. Dobb's Journal; May 2002.
Avatar of TKD

ASKER

Oh,it seems look like difficultly to implement.
I already read :
http://www.compuphase.com/graphic/scale2.htm
Quick and smooth image scaling with Bresenham
But I still don't know how to start.
Could any source code be referenced?
Thanks a lot!
Avatar of TKD

ASKER

I would like to resize the raster image,and save it!
Avatar of TKD

ASKER

I use statement following to display the RGB raster image:
StretchDIBits(hdc,0,0,OUTPUT_WIDTH,OUTPUT_HEIGHT,0,0,SOURCE_WIDTH,SOURCE_HEIGHT,(LPVOID)RGB,  &bm,  DIB_RGB_COLORS,  SRCCOPY);
Could I save the output raster image produced by StretchDIBits???
Get an image processing program, such as paintshop pro (http://www.jasc.com) or photoshop (http://www.adobe.com).

StretchDIBits will not achieve the same quality as a bicubic resize.

If you want to resize as a CGI, have a look at GIMP.
Speaking of GIMP, why don't you use libgimp?

http://developer.gimp.org/api/2.0/libgimp/index.html

There you have the full functionality of GIMP within reach from your C program. Resize, zillions of other things, file formats, etc.
This man is not wanting to permanently change the image he wants to change it at runtime using C.

I would write this up in the programming portion of this website for the answer, I dont know enough about C or window's api to tell you.
YOu could check out imagemagick.org  > open source image editing library.

all your desires are in there probably.
Avatar of Jose Parrot
I understand that TKD answered its own question, when told us about StretchDIBits. That's right: that API call does what he wants. Probably DrawDib does it better, because is capable of dithering, when the device has less bits per pixel than the bitmap.

The complete answer, for both scaling and storing, is found in the Win32 Programmer's Reference, at the chapter "Using Bitmaps" and its parts: Capturing an image,       Scaling an image and Storing an image. The samples there have all the code TKD is looking for.

Hope it helps...
Jose Parrot

 
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial