IntegerScaling — library for pixel-perfect integer-ratio scaling

IntegerScaling is a library for calculating integer ratios and scaled-image sizes for pixel-perfect image upscaling with optional aspect-ratio correction. Implements the author’s algorithm of scaling with perfectly uniform resulting pixels.

There are versions of the library for C++, Rust, JavaScript and PHP programming languages. License: MIT.

API

The library is available as either the object (JavaScript), the module (Rust), or the class (C++, PHP) IntegerScaling that has the following methods/functions:

calculateRatio(areaWidth, areaHeight, imageWidth, imageHeight)
Calculates an integer scaling ratio common for X/Y axes (square pixels). Returns an integer number.
calculateRatios(areaWidth, areaHeight, imageWidth, imageHeight, aspectX, aspectY)
Calculates integer scaling ratios potentially different for X/Y axes as a result of aspect-ratio correction (rectangular pixels). Returns a structure (C++, Rust) or an object (JavaScript, PHP) with two properties: x and y, that contain the horizontal and vertical integer ratios correspondingly.
calculateSize(areaWidth, areaHeight, imageWidth, imageHeight)
Calculates size (width and height) of scaled image without aspect-ratio correction (square pixels). Returns a structure (C++, Rust) or an object (JavaScript, PHP) with two properties: width and height, that contain the width and the height of the scaled image correspondingly.
calculateSizeCorrected(areaWidth, areaHeight, imageWidth, imageHeight, aspectX, aspectY)
Calculates size (width and height) of scaled image with aspect-ratio correction (rectangular pixels). For return-value information, see calculateSize().
calculateSizeCorrectedPerfectY(areaWidth, areaHeight, imageHeight, aspectX, aspectY)
Calculates size (width and height) of scaled image with aspect-ratio correction with integer vertical scaling ratio, but fractional horizontal scaling ratio for the purpose of achieving precise aspect ratio while still having integer vertical scaling e. g. for uniform scanlines. For return-value information, see calculateSize().

There is also the Rust module integer_scaling for those who prefer the snake_case notation.

The class (C++, PHP) is defined in the MaratTanalin namespace.

Parameters

areaWidth, areaHeight
Integer numbers representing the width and the height correspondingly, of the screen area the scaled image should fit into.
imageWidth, imageHeight
Integer numbers representing the width and height correspondingly, of the image to scale.
aspectX, aspectY
Target aspect ratio aspectX:aspectY as two floating-point numbers representing the numerator and the denominator of the aspect ratio.