Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm resizing an image based on a non-integral conversion factor (a bmp image, in fact). I understand how resizing works, just non with a decimal number. For instance, if the factor were 2, I would simply write the individual pixels twice to the output file. You can't really write 1.5 pixels if the factor were, say, 1.5. Conceptually, how might this play out?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Don't think of the process as "where would this pixel go?", but rather as "from where did that pixel came?".

Then, it's easy to see that each pixel in the final picture will come from a position at a fractional coordinates in the original image, that just means it will have a color that is a weighted average of the near pixels.

That's why Photoshop (and others) lets you choose the subsampling algorithm. Some are prettier but use more origin pixels, or need more precise math, while others are fast and doable with integer numbers but not so good looking.

share|improve this answer
    
How might I generate those coordinates? –  Someone Oct 16 '12 at 22:51
    
x0 = x * w / w0 –  Javier Oct 17 '12 at 17:13
add comment

This is actually a big topic. What you are asking about is called image resampling. And there are many different algorithms that can be used. See the wikipedia article for a basic introduction.

But as Javier hinted at, you dont generally take a pixel and write out a scaled version. Rather, you look at a destination pixel, and figure out what source pixels you need to reference and how to weigh them to calculate the destination pixel.

share|improve this answer
2  
It can be done in any language, they are algorithms. What I would recommend, unless you are interested in really getting into the inner workings of these algorithms, is to use a 3rd party library that provides resampling functionality as the algorithms can be complex to implement correctly. –  GrandmasterB Oct 17 '12 at 3:47
    
@Someone, in C, anything can be a one-liner! :-) but in fact, the simplest algorithms can be just 7 very readable lines: two nested loops (x and y), an interpolation expression for x and y, and the averaging expression for R, G and B channels. –  Javier Oct 17 '12 at 17:12
    
Are you asking what library to use, or what algorithm? How are we supposed to give a recommendation when we dont know what platform(s) you are using, and what you want to do with the images? –  GrandmasterB Oct 17 '12 at 20:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.