hqx
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (February 2019) |
In image processing, hqx ("high quality [n]× magnification") is one of the pixel art scaling algorithms developed by Maxim Stepin, used in emulators such as Nestopia, FCEUX, higan, Snes9x, ZSNES and many more There are three hqx filters defined: hq2x, hq3x, and hq4x, which magnify by factor of 2, 3, and 4 respectively. For other magnification factors, this filter is used with nearest-neighbor scaling. In libretro, hqx shaders are referred to as scalehq.[1]
-
Image enlarged 3× with the nearest-neighbor interpolation
-
Image enlarged by 3× with hq3x algorithm
Algorithm
First, the color of each of the 8 pixels around the source pixel is compared to the color of the source pixel. Shapes are detected by checking for pixels of similar color according to a threshold. This gives total of 28 = 256 combinations of similar or dissimilar neighbors. To expand the single pixel into a 2×2, 3×3, or 4×4 block of pixels, the arrangement of neighbors is looked up in a predefined table which contains the necessary interpolation patterns.[2]
Hqx uses the YUV color space to calculate color differences, so that differences in brightness is weighted higher to mimic human perception. It is possible to use a look-up table for the color space conversion if the source image is 16 bit per pixel.[2]
The interpolation data in the lookup tables are constrained by the requirement that continuity of line segments must be preserved, while optimizing for smoothness. Generating these 256-filter lookup tables is relatively slow, and is the major source of complexity in the algorithm: the render stage is very simple and fast, and designed to be capable of being performed in real time on a MMX-capable CPU.[2]
In the source code, the interpolation data is represented as preprocessor macros to be inserted into switch case statements, and there is no source code leading to the generation of a lookup table. The author describes the process of generating a look-up table as:[2]
... for each combination the most probable vector representation of the area has to be determined, with the idea of edges between the different colored areas of the image to be preserved, with the edge direction to be as close to a correct one as possible. That vector representation is then rasterised with higher (3x) resolution using anti-aliasing, and the result is stored in the lookup table.
Implementations
- The original algorithm has been ported to DevIL (but kept in the C language).[2]
- Ports to Java and C# languages are available. These implementations expand the macros but largely keep the lookup tables.[3][4]
- libretro implements hq2x and hq4x as scalehq shaders, in Slang/HLSL, GLSL, and Cg.[1] As it runs on modern GPUs, lookup tables are substituted by the floating-point mathematical operations the very tables were meant to replace.[5]
See also
External links
- hq2x, hq3x, and hq4x at the Wayback Machine
- Port of original project using DevIL A command line tool and C library
- hqxSharp project a port of hqx with added support for transparency, custom tolerances and seamless tiling (C#)
- 2d image filter project at code.google.com including the hqx filters and more (C#)
- hqx-java project Arcnor project - a free Java port of hqxSharp with a demo of usage (Java)
- HqxCli-Java A command line tool that use the Arcnor implementation (Java)
- ^ a b "scalehq". Libretro Docs.
- ^ a b c d e Zemek, Cameron (6 May 2019). "hqx README (grom358 port)".
{{cite journal}}
: Cite journal requires|journal=
(help) - ^ Garcia, Edu (6 March 2019). "hqx-java". GitHub.
- ^ Tamschi. "hqxsharp". bitbucket.
- ^ "2xScaleHQ.glsl". GitHub. libretro. 6 May 2019.