Scaling sprites using hqnx and scalenx

Most image scaling techniques use different interpolation methods such as the nearest neighbor, bilinear and bicubic interpolation. However, since they work by interpolating pixel color values, they tend to add some amount of blur into the output. Although this is fine for most images, sprites in games usually have sharp, well defined lines and this technique therefore tends to either destroy these edges by blurring them to a large extent as in the case of bicubic interpolation, or preserve the edges but make the sprite visibly blocky, as in the case of nearest neighbor interpolation. Therefore, 2D game engines require different methods for scaling images.

One of the most important algorithms in sprite scaling are the hqnx family of algorithms. hq2x, hq3x, and hq4x are for scale factors of 2:1, 3:1 and 4:1 respectively. All three follow a similar algorithm. The following is the algorithm for hq3x: (Visit http://www.hiend3d.com/hq3x.html for more information)

  • Analyze 3×3 area of source pixel.
  • Calculate color difference between the central pixel and 8 nearest neighbors.
  • Compare the difference to a predefined threshold and sort the pixels in two 2 categories: “close” and “distant”. Since there are 8 neighbors, there are 28 = 256 possible combinations
  • Have a lookup table with 256 entries for each possible combination. Each entry states how to mix the colors of the source pixels from the 3×3 area to get interpolated and anti-aliased pixels of the filtered image.

hq2x

The Scalenx family of algorithms are also used extensively in scaling sprites. Though they are not as accurate as hqnx, they are more computationally efficient. The basic idea is to consider a 3×3 array of pixels on the sprite. Then take the center pixel and split it into an nxn grid. Each element in the grid is determined by the relationship between the pixels in the original 3×3 array. The exact algorithm used can be obtained from http://scale2x.sourceforge.net/algorithm.html.

Blurring

The above images give a situation where hq2x in fact blots out a part of the image that should not ideally be blotted out, and therefore provides an output that is far from ideal. However, note that in comparison to Scale2x, the edges on the output sprite are more accurate. Therefore, it is preferable to use Scale2x if you want to preserve texture of the image, and hq2x if you are looking to preserve the quality of line-art and edges.

Posted in Topics: Uncategorized

Jump down to leave a comment.

Leave a Comment

You must be logged in to post a comment.



* You can follow any responses to this entry through the RSS 2.0 feed.