7

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?

2 Answers 2

6

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.

2
  • How might I generate those coordinates?
    – Someone
    Oct 16, 2012 at 22:51
  • x0 = x * w / w0
    – Javier
    Oct 17, 2012 at 17:13
5

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.

3
  • 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. Oct 17, 2012 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, 2012 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? Oct 17, 2012 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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