First, the color of each of the 8 pixels around the sourcepixel 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. Hqx uses the YUV color space to calculate color differences, so that differences in brightness is weighted higher to mimichuman perception. It is possible to use a look-up table for the color space conversion if the source image is 16 bit per pixel. 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. 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:
... 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 resolution using anti-aliasing, and the result is stored in the lookup table.
Ports to Java and C# languages are available. These implementations expand the macros.
A C++ port by byuu, the author of bsnes, which encodes the LUT in a space-efficient way. Byuu exploited the symmetry of patterns to make such an encoding, and wrote some notes on interpreting the original look up tables.
libretro implements two families of shaders in Slang/HLSL, GLSL, and Cg:
* The hqx family, which is true hqx. As it runs on modern GPUs, lookup tables are substituted by textures. The textures were generated by interrogating a modified version of hqx for its switch/case.
* The scalehq family, which is frequently confused with hqx. It is not related to hqx, but is rather a generic upscaling-smoothing algorithm.
hqx has also been implemented in ffmpeg for upscaling images and videos 2x, 3x, or 4x. An account of the production of the translation for ffmpeg is here: http://blog.pkh.me/p/19-butchering-hqx-scaling-filters.html and usage may be something like: ffmpeg -i %1 -filter_complex hqx=2 hqx2-%1 to produce a 2x image or video.