Algorithm of integer scaling

The author’s algorithm of integer scaling. An algorithm is a sequence of actions for achieving the needed result. For a ready-to-use implementation, see the IntegerScaling library (C++/Rust/JS/PHP). The algorithm is used e. g. in the SNES emulator bsnes-mt and the DOSBox shader pixel_perfect by tyrells.

Brief

Without aspect-ratio correction (square pixels)

Divide the screen width and height by the image width and height correspondingly. Discard fractional parts. The less of the results is the needed integer ratio.

As a mathematical formula, this can be represented this way:

R = min(⌊(SW/W)⌋, ⌊(SH/H)⌋)

where R, SW, SH, W, H are the integer ratio, screen width, screen height, image width and image height correspondingly.

With aspect-ratio correction (rectangular pixels)

  1. Divide the screen width and height by the image width and height correspondingly. Discard fractional parts. These are maximum integer ratios horizontally and vertically correspondingly.
  2. Multiply them by the image width and height correspondingly. The results are the width and height of the area the scaled image will be fit into.
  3. If the aspect ratio of the area is equal to the target one, the needed integer ratios are equal to the maximum ones.
  4. Depending on whether the area aspect-ratio is lower or greater than the target one, make the needed horizontal or vertical integer scale equal to the maximum one on this axis.
  5. Calculate the approximate scale on the second axis based on the first-axis scale and the target aspect ratio. Round the result up or down depending on what results in minimal aspect-ratio error.

Detailed

  1. Divide the width SW and the height SH of the screen in physical pixels, correspondingly, by the width W and the height H of the image to scale. Discard fractional parts of the resulting ratios, that results in maximum integer ratios MRX horizontally and MRY vertically correspondingly.
  2. If aspect-ratio correction is not needed (pixels are square, horizontal and vertical scales are equal):

    1. The integer scale R is the less of the ratios MRX and MRY.
    2. Represent each pixel of the original image as a square group of R×R physical pixels of same color equal to the color of the original-image pixel.
    3. Calculate the width UW and the height UH of the scaled image by multiplying, correspondingly, the width W and the height H of the original image by the ratio R.
  3. If aspect-ratio correction is needed (pixels are potentially rectangular, horizontal and vertical scales are potentially different):

    1. Multiply the width W and the height H of the image by MRX and MRY correspondingly, that results in the width MW and the height MH of the area which the scaled image will fit into.
    2. Divide the width MW of this area by its height MH, that results in the aspect ratio MA of the area.
    3. If the ratio MA is equal to the target aspect ratio TA:

      1. Assign the scales RX horizontally and RY vertically the values of MRX and MRY correspondingly.
      2. Go to centering the scaled image (the step 4.1).
    4. If the ratio MA is lower than the target aspect ratio TA:

      1. Assign the horizontal scale RX the value of MRX.
      2. Divide the maximum width MW by the target aspect ratio TA, that results in an approximate scaled height AUH.
    5. If the ratio MA is greater than the target aspect ratio TA:

      1. Assign the vertical scale RY the value of MRY.
      2. Multiply the maximum height MH by the target aspect ratio TA, that results in an approximate scaled width AUW.
    6. Divide the approximate scaled size AUW or AUH by the original image size W or H on the same axis. Round the result in a direction that results in a minimal aspect-ratio error. If errors in both directions are equal or close, round the result in a direction that results in an aspect ratio closest to the original aspect ratio (the scales on different axes are closer to each other, the pixel shape is closer to a square). The resulting integer is the scale on this axis — RX (horizontal) or RY (vertical).
    7. Calculate the width UW and the height UH of the scaled image by multiplying the width W and height H of the original image by the scales RX horizontally and RY vertically correspondingly.
  4. Center the scaled image on the screen:

    1. Subtract the width UW and the height UH of the scaled image from the width SW and the height SH of the screen correspondingly, divide the results by 2, discard fractional part, give the resulting numbers the names of X and Y correspondingly.
    2. Display the scaled image at a distance of X horizontally and Y vertically from the top-left corner of the screen.
  5. Fill the rest screen space around the scaled image with black.

Reference implementation

The author’s library IntegerScaling is the reference software implementation of the algorithm in multiple programming languages: C++, Rust, JavaScript, PHP. The library is used e. g. in the SNES emulator bsnes-mt.

Example without aspect-ratio correction

It’s needed to display a 640×480 (VGA) image on a 1920×1080 (Full HD) screen:

1920 / 640 = 3
1080 / 480 = 2.25

Discarding the fractional part 0.25 results in 2. 2 is less than 3. So each pixel of the original image should be displayed as a group of 2×2 physical pixels.

The width and the height of the scaled image in physical pixels are 1280 (640 × 2) and 960 (480 × 2) correspondingly. So for the purpose of centering on the screen, the scaled image should be displayed at a distance of 320 ((1920 − 1280) / 2) pixels horizontally and 60 ((1080 − 960) / 2) pixels vertically from the top-left corner of the screen.

Example with aspect-ratio correction

It’s needed to display a 400×254 image with a target aspect-ratio of 4:3 on a 3840×2160 (4K) screen:

3840 / 400 = 9.6
2160 / 254 ≈ 8.5

Discarding fractional parts of the ratios, we get the maximum integer ratios 9 horizontally and 8 vertically correspondingly.

Multiply the width 400 and the height 254 of the image by 9 and 8 correspondingly, that results in the width 3600 and the height 2032 of the area the scaled image will fit into.

The aspect ratio 1.77 of the area is greater than the target aspect ratio 4:3 (≈ 1.333). So:

  • set vertical scale to 8;
  • divide the maximum height 2032 by the target aspect ratio 4:3 (≈ 1.333), that results in an approximate scaled width of 2709.333.

Divide 2709.333 by the original width 400 of the image, that results in 6.773, round the result, that results in a horizontal scale of 7. So the pixel size is 7×8.

Width and height of the scaled image are 2800 (400 × 7) and 2032 (254 × 8) correspondingly. The actual aspect ratio is about 1.378 (2800 / 2032), the error compared with the target aspect ratio 4:3 (≈ 1.333) is about 3.35%.

For the purpose of centering on the screen, the scaled image should be displayed at a distance of 520 ((3840 − 2800) / 2) pixels horizontally and 64 ((2160 − 2032) / 2) pixels vertically from the top-left corner of the screen.

Reference rendering

The images below show how the results of integer scaling without aspect-ratio correction must look. With a proper implementation of integer scaling, the results of scaling the provided original images must be identical to the provided reference renderings of scaled images.

To prevent affecting image appearance by the web browser, it’s recommended to save the images to your computer and view them with a standalone application at the zoom of 100%. You can also download all the images at once as an archive (55 KB).

Images Ratio Black background Note
Original Upscaled Full Integer Left +
right
Top +
bottom
320×240
qVGA, 4:3
3840×2160
4K UHD, 16:9
9
900%
480+480 0 Image at full height, black only at the left/right
640×480
VGA, 4:3
1920×1080
Full HD, 16:9
2.25
225%
2
200%
320+320 60+60
1280×720
HD, 16:9
1920×1080
Full HD, 16:9
1.5
150%
1
100%
320+320 180+180 Identical to the “Center” mode
1280×720
HD, 16:9
2560×1600
WQXGA, 16:10
2
200%
0 80+80 Image at full width, black only at the top/bottom
1920×1080
Full HD, 16:9
3840×2160
4K UHD, 16:9
2
200%
0 0 Resolutions are divisible, so no black background

See also the article about integer scaling.