Solved

How to scale image from range 50%~200%

Posted on 2004-09-24
19
834 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:TKD
  • 5
  • 2
  • 2
  • +8
19 Comments
 
LVL 30

Expert Comment

by:weed
ID: 12149321
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.
0
 

Author Comment

by:TKD
ID: 12149338
Program language: C
Platform: Win XP
I know I will lose QUALITY if I resize a raster image.
0
 
LVL 11

Expert Comment

by:Jason210
ID: 12154597
May be you should try the programming discussions.
0
 
LVL 2

Expert Comment

by:mikelima
ID: 12208111
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.

0
 

Author Comment

by:TKD
ID: 12209968
Thanks...
I would like to write the method by using C source code.
Is there any articles or source codes referenced?
Thanks again.
0
 
LVL 11

Expert Comment

by:Jason210
ID: 12210892
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.
0
 
LVL 22

Expert Comment

by:grg99
ID: 12212211
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!






0
 
LVL 17

Expert Comment

by:mokule
ID: 12212932
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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:TKD
ID: 12213918
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!
0
 

Author Comment

by:TKD
ID: 12213922
I would like to resize the raster image,and save it!
0
 

Author Comment

by:TKD
ID: 12214348
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???
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12258384
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12258449
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.
0
 
LVL 2

Expert Comment

by:ThePCNerd
ID: 12292311
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.
0
 
LVL 6

Expert Comment

by:michaelbuddy
ID: 12589496
YOu could check out imagemagick.org  > open source image editing library.

all your desires are in there probably.
0
 
LVL 18

Expert Comment

by:JoseParrot
ID: 12600197
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

 
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12637708
PAQed with points refunded (200)

modulo
Community Support Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In this Micro Tutorial viewers will learn the basic shortcuts and functions in InDesign.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now