Jump to content

Hqx: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5
 
(28 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{About|[[image scaling]] filter|.hqx files|BinHex}}
{{About|[[image scaling]] filter|.hqx files|BinHex}}

{{lowercase title}}
{{lowercase title}}


{{Infobox software
In [[image processing]], '''hqx''' ("hq" stands for "high quality" and "x" stands for magnification) is one of the [[pixel art scaling algorithms]] developed by [[Maxim Stepin]], used in [[emulator]]s such as [[Nestopia]], [[FCEUX]], [[higan (emulator)|higan]], [[Snes9x]], [[ZSNES]] and many more. There are three hqx filters: '''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 interpolation|nearest-neighbor]] scaling {{Citation needed|date=September 2016}}.
| title = hqx
| developer = Maxim Stepin
| released = {{Start date and age|2003|7|25|df=no}}
| latest release version = 1.1
| latest release date = {{Start date and age|2003|9|29|df=no}}
| programming language = [[C++]]
| genre = [[Pixel-art scaling algorithm]]
| license = [[LGPL 2|LGPL 2.1]]
| website = {{URL|https://www.gimp.org/|gimp.org}}
}}

'''hqx''' ("high quality scale") is a set of 3 image upscaling algorithms developed by Maxim Stepin. The algorithms are '''hq2x''', '''hq3x''', and '''hq4x''', which magnify by a factor of 2, 3, and 4 respectively. It was initially created in 2003 for the Super NES emulator [[ZSNES]],<ref>{{Cite web |date=2013-12-05 |title=HiEnd3D |url=http://www.hiend3d.com/hq2x.html |access-date=2023-02-12 |archive-url=https://web.archive.org/web/20131205091805/http://www.hiend3d.com/hq2x.html |archive-date=2013-12-05 }}</ref> and is used in [[emulator]]s such as [[Nestopia]], [[FCEUX|F. CEUX]]Snes9x., and [[Snes9x]].

==Algorithm==

The source image's pixels are iterated through from top-left to bottom-right. For each pixel, the surrounding 8 pixels are compared to the color of the source pixel. Shapes are detected by checking for [[pixel]]s of similar color according to a [[YUV]] threshold. hqx uses the [[YUV color space]] to calculate color differences, so that differences in brightness are weighted higher to order to mimic human perception.<ref name="grom358">{{cite journal |last1=Zemek |first1=Cameron |title=hqx README (grom358 port) |date=6 May 2019 |url=https://github.com/grom358/hqx/blob/master/README}}</ref> This gives a total of <math>2^8 = 256</math> 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.<ref name="grom358"/>

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 (instruction set)|MMX]]-capable CPU.<ref name="grom358"/>

In the source code, the interpolation data is represented as preprocessor [[Macro (computer science)|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:<ref name="grom358"/>
<blockquote>... 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.</blockquote>


{{Gallery
{{Gallery
|align=center
|align=center
|width=384
|width=350
|File:Test nn.png
|File:Test nn.png
|Image enlarged 3× with the [[nearest-neighbor interpolation]]
|Image enlarged 3× with [[nearest-neighbor interpolation]]
|File:Test hq3x.png
|File:Test hq3x.png
|Image enlarged by 3× with hq3x algorithm
|Image enlarged by 3× with hq3x
}}
}}


==Algorithm==
==Implementations==
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 [[pixel]]s of similar color according to a threshold. This gives total of 2<sup>8</sup>&nbsp;=&nbsp;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.


* The original algorithm has been ported to [[DevIL]] (but kept in the C language).<ref name="grom358"/>
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 lookup tables{{explain|reason=How are they generated?|date=April 2016}} 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.
* Ports to Java and C# languages are available. These implementations expand the macros.<ref>{{cite web |last1=Garcia |first1=Edu |title=hqx-java |url=https://github.com/Arcnor/hqx-java/ |website=GitHub |date=6 March 2019}}</ref><ref>{{cite web |author1=Tamschi |title=hqxsharp |url=https://github.com/Tamschi/hqxSharp |website=GitHub}}</ref>

* 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.<ref name=byuu>Byuu. [http://nesdev.parodius.com/bbs/viewtopic.php?p=82770#82770 Release announcement] {{Webarchive|url=https://web.archive.org/web/20110930163308/http://nesdev.parodius.com/bbs/viewtopic.php?p=82770#82770 |date=2011-09-30 }} Accessed 2011-08-14. [https://pastebin.com/YXpmqvW5 public domain implementation released on pastebin]</ref>
=== A description from the original author ===
* [[libretro]] implements two families of shaders in Slang/[[HLSL]], [[GLSL]], and [[Cg (programming language)|Cg]]:
A brief description of the implementation taken from the original hqx Library README ( see original hqx project at the external links section )<blockquote>''The first step is an analysis of the 3x3 area of the source pixel. At first, we calculate the color difference between the central pixel and its 8 nearest neighbors. Then that difference is compared to a predefined threshold, and these pixels are sorted into two categories: "close" and "distant" colored. There are 8 neighbors, so we are getting 256 possible combinations.''</blockquote><blockquote>''For the next step, which is filtering, a lookup table with 256 entries is used, one entry per each combination of close/distant colored neighbors. Each entry describes how to mix the colors of the source pixels from 3x3 area to get interpolated pixels of the filtered image.''</blockquote><blockquote>''The present implementation is using [[YUV color space]] to calculate color differences, with more tolerance on Y (brightness) component, then on color components U and V. That color space conversion is quite easy to implement if the format of the source image is 16 bit per pixel, using a simple lookup table. It is also possible to calculate the color differences and compare them to a threshold very fast, using MMX instructions.''</blockquote><blockquote>''Creating a lookup table was the most difficult part - 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.''</blockquote><blockquote>''The filter was not designed for photographs, but for images with clear sharp edges, like line graphics or cartoon sprites. It was also designed to be fast enough to process 256x256 images in real-time.''</blockquote>
** The '''hqx''' family, which is true hqx.<ref>{{cite web |title=hqx |url=http://docs.libretro.com/shader/hqx/ |website=Libretro Docs}}</ref> As it runs on modern [[GPU]]s, lookup tables are substituted by textures.<ref name="hqx_inc">{{cite web |last1=Blok |first1=Jules |title=hqx-shader |url=https://github.com/Armada651/hqx-shader |website=GitHub |date=12 April 2019}}</ref> 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.<ref>{{cite web |last1=K |first1=Hunter |title=Filthy Pants: A Computer Blog: True Hq2x Shader Comparison With xBR |url=http://filthypants.blogspot.com/2014/06/true-hq2x-shader-comparison-with-xbr.html |website=Filthy Pants |date=20 June 2014}}</ref>
* 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 [http://blog.pkh.me/p/19-butchering-hqx-scaling-filters.html here] and usage may be something like: <code>ffmpeg -i %1 -filter_complex hqx=2 hqx2-%1</code> to produce a 2x image or video.


==See also==
==See also==

* [[Image scaling]]
* [[Image scaling]]
* [[Pixel-art scaling algorithms]]
* [[Pixel art]]

==References==
{{Reflist}}


==External links==
==External links==
* [https://web.archive.org/web/20131205091805/http://www.hiend3d.com/hq2x.html hq2x], [https://web.archive.org/web/20131114143602/http://www.hiend3d.com/hq3x.html hq3x], and [https://web.archive.org/web/20131216092117/http://www.hiend3d.com/hq4x.html hq4x] at the [[Wayback Machine]]
* [https://web.archive.org/web/20131205091805/http://www.hiend3d.com/hq2x.html hq2x], [https://web.archive.org/web/20131114143602/http://www.hiend3d.com/hq3x.html hq3x], and [https://web.archive.org/web/20131216092117/http://www.hiend3d.com/hq4x.html hq4x] at the [[Wayback Machine]]
* [https://code.google.com/p/hqx/ original hqx project] at code.google.com (C)
* [https://github.com/grom358/hqx/ Port of original project using DevIL] A command line tool and C library
* [https://github.com/grom358/hqx/ C hqx.h and command line tool] A command line tool and C library
* [https://github.com/Tamschi/hqxSharp hqxSharp project] a port of hqx with added support for transparency, custom tolerances and seamless tiling (C#)
* [https://github.com/VincenzoLaSpesa/hqxcli-java HqxCli-Java] A command line tool that use the Arcnor implementation (Java)
* [https://bitbucket.org/Tamschi/hqxsharp hqxSharp project] a port of hqx with added support for transparency, custom tolerances and seamless tiling (C#)
* [https://code.google.com/p/2dimagefilter/ 2d image filter project] at code.google.com including the hqx filters and more (C#)
* [https://code.google.com/p/2dimagefilter/ 2d image filter project] at code.google.com including the hqx filters and more (C#)
* [https://github.com/Arcnor/hqx-java/wiki/Usage hqx-java project] Arcnor project - a free Java port of hqxSharp with a demo of usage (Java)
* [https://github.com/Arcnor/hqx-java/wiki/Usage hqx-java project] Arcnor project - a free Java port of hqxSharp with a demo of usage (Java)
** [https://github.com/VincenzoLaSpesa/hqxcli-java HqxCli-Java] A command line tool that use the Arcnor implementation (Java)
* [http://blog.pkh.me/p/19-butchering-hqx-scaling-filters.html ffmpeg implementation story] ffmpeg -i %1 -filter_complex hqx=2 hqx2-%1 to produce a 2x image or video.


[[Category:Image processing]]
[[Category:Image processing]]


{{graphics-software-stub}}

Latest revision as of 04:05, 23 August 2024


hqx
Developer(s)Maxim Stepin
Initial releaseJuly 25, 2003; 21 years ago (2003-07-25)
Stable release
1.1 / September 29, 2003; 21 years ago (2003-09-29)
Written inC++
TypePixel-art scaling algorithm
LicenseLGPL 2.1
Websitegimp.org

hqx ("high quality scale") is a set of 3 image upscaling algorithms developed by Maxim Stepin. The algorithms are hq2x, hq3x, and hq4x, which magnify by a factor of 2, 3, and 4 respectively. It was initially created in 2003 for the Super NES emulator ZSNES,[1] and is used in emulators such as Nestopia, F. CEUXSnes9x., and Snes9x.

Algorithm

[edit]

The source image's pixels are iterated through from top-left to bottom-right. For each pixel, the surrounding 8 pixels are compared to the color of the source pixel. Shapes are detected by checking for pixels of similar color according to a YUV threshold. hqx uses the YUV color space to calculate color differences, so that differences in brightness are weighted higher to order to mimic human perception.[2] This gives a total of 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]

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

[edit]
  • 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.[3][4]
  • 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.[5]
  • libretro implements two families of shaders in Slang/HLSL, GLSL, and Cg:
    • The hqx family, which is true hqx.[6] As it runs on modern GPUs, lookup tables are substituted by textures.[7] 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.[8]
  • 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 and usage may be something like: ffmpeg -i %1 -filter_complex hqx=2 hqx2-%1 to produce a 2x image or video.

See also

[edit]

References

[edit]
  1. ^ "HiEnd3D". 2013-12-05. Archived from the original on 2013-12-05. Retrieved 2023-02-12.
  2. ^ a b c d e Zemek, Cameron (6 May 2019). "hqx README (grom358 port)". {{cite journal}}: Cite journal requires |journal= (help)
  3. ^ Garcia, Edu (6 March 2019). "hqx-java". GitHub.
  4. ^ Tamschi. "hqxsharp". GitHub.
  5. ^ Byuu. Release announcement Archived 2011-09-30 at the Wayback Machine Accessed 2011-08-14. public domain implementation released on pastebin
  6. ^ "hqx". Libretro Docs.
  7. ^ Blok, Jules (12 April 2019). "hqx-shader". GitHub.
  8. ^ K, Hunter (20 June 2014). "Filthy Pants: A Computer Blog: True Hq2x Shader Comparison With xBR". Filthy Pants.
[edit]