How do I do high quality scaling of a image?

I'm writing some code to scale a 32 bit RGBA image in C/C++. I have written a few attempts that have been somewhat successful, but they're slow and most importantly the quality of the sized image is not acceptable.

I compared the same image scaled by OpenGL (ie my video card) and my routine and it's miles apart in quality. I've Google Code Searched, scoured source trees of anything I thought would shed some light (SDL, Allegro, wxWidgets, CxImage, GD, ImageMagick, etc.) but usually their code is either convoluted and scattered all over the place or riddled with assembler and little or no comments. I've also read multiple articles on Wikipedia and elsewhere, and I'm just not finding a clear explanation of what I need. I understand the basic concepts of interpolation and sampling, but I'm struggling to get the algorithm right. I do NOT want to rely on an external library for one routine and have to convert to their image format and back. Besides, I'd like to know how to do it myself anyway. :)

I have seen a similar question asked on stack overflow before, but it wasn't really answered in this way, but I'm hoping there's someone out there who can help nudge me in the right direction. Maybe point me to some articles or pseudo code... anything to help me learn and do.

Here's what I'm looking for:

  • No assembler (I'm writing very portable code for multiple processor types).
  • No dependencies on external libraries.
  • I am primarily concerned with scaling DOWN, but will also need to write a scale up routine later.
  • Quality of the result and clarity of the algorithm is most important (I can optimize it later).
  • My routine essentially takes the following form:

    DrawScaled(uint32 *src, uint32 *dst, 
          src_x, src_y, src_w, src_h, 
          dst_x, dst_y, dst_w, dst_h );
    

    Thanks!

    UPDATE: To clarify, I need something more advanced than a box resample for downscaling which blurs the image too much. I suspect what I want is some kind of bicubic (or other) filter that is somewhat the reverse to a bicubic upscaling algorithm (ie each destination pixel is computed from all contributing source pixels combined with a weighting algorithm that keeps things sharp.

    Example

    Here's an example of what I'm getting from the wxWidgets BoxResample algorithm vs. what I want on a 256x256 bitmap scaled to 55x55.

  • www.free_image_hosting.net/uploads/1a25434e0b.png
  • And finally:

  • www.free_image_hosting.net/uploads/eec3065e2f.png
  • the original 256x256 image


    I've found the wxWidgets implementation fairly straightforward to modify as required. It is all C++ so no problems with portability there. The only difference is that their implementation works with unsigned char arrays (which I find to be the easiest way to deal with images anyhow) with a byte order of RGB and the alpha component in a separate array.

    If you refer to the "src/common/image.cpp" file in the wxWidgets source tree there is a down-sampler function which uses a box sampling method "wxImage::ResampleBox" and an up-scaler function called "wxImage::ResampleBicubic".


    一个相当简单和体面的算法来重新采样图像是双三次插值,维基百科单独拥有所有你需要的信息来实现。


    Is it possible that OpenGL is doing the scaling in the vector domain? If so, there is no way that any pixel-based scaling is going to be near it in quality. This is the big advantage of vector based images.

    The bicubic algorithm can be tuned for sharpness vs. artifacts - I'm trying to find a link, I'll edit it in when I do.

    Edit: It was the Mitchell-Netravali work that I was thinking of, which is referenced at the bottom of this link:

    http://www.cg.tuwien.ac.at/~theussl/DA/node11.html

    You might also look into Lanczos resampling as an alternative to bicubic.

    链接地址: http://www.djcxy.com/p/50280.html

    上一篇: 如何在ImageView中缩放图像以保持纵横比

    下一篇: 如何对图像进行高质量缩放?