Algorithm of integer scaling
- Marat Tanalin
- Updated:
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 Integer
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:
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)
- 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.
- 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.
- If the aspect ratio of the area is equal to the target one, the needed integer ratios are equal to the maximum ones.
- 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.
- 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
-
Divide the width
SWand the heightSHof the screen in physical pixels, correspondingly, by the widthWand the heightHof the image to scale. Discard fractional parts of the resulting ratios, that results in maximum integer ratiosMRXhorizontally andMRYvertically correspondingly. -
If aspect-ratio correction is not needed (pixels are square, horizontal and vertical scales are equal):
-
The integer scale
Ris the less of the ratiosMRXandMRY. -
Represent each pixel of the original image as a square group
of
R×Rphysical pixels of same color equal to the color of the original-image pixel. -
Calculate the width
UWand the heightUHof the scaled image by multiplying, correspondingly, the widthWand the heightHof the original image by the ratioR.
-
The integer scale
-
If aspect-ratio correction is needed (pixels are potentially rectangular, horizontal and vertical scales are potentially different):
-
Multiply the width
Wand the heightHof the image byMRXandMRYcorrespondingly, that results in the widthMWand the heightMHof the area which the scaled image will fit into. -
Divide the width
MWof this area by its heightMH, that results in the aspect ratioMAof the area. -
If the ratio
MAis equal to the target aspect ratioTA:-
Assign the scales
RXhorizontally andRYvertically the values ofMRXandMRYcorrespondingly. - Go to centering the scaled image (the step 4.1).
-
Assign the scales
-
If the ratio
MAis lower than the target aspect ratioTA:- Assign the horizontal scale
RXthe value ofMRX. -
Divide the maximum width
MWby the target aspect ratioTA, that results in an approximate scaled heightAUH.
- Assign the horizontal scale
-
If the ratio
MAis greater than the target aspect ratioTA:- Assign the vertical scale
RYthe value ofMRY. -
Multiply the maximum height
MHby the target aspect ratioTA, that results in an approximate scaled widthAUW.
- Assign the vertical scale
-
Divide the approximate scaled size
AUWorAUHby the original image sizeWorHon 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) orRY(vertical). -
Calculate the width
UWand the heightUHof the scaled image by multiplying the widthWand heightHof the original image by the scalesRXhorizontally andRYvertically correspondingly.
-
Multiply the width
-
Center the scaled image on the screen:
-
Subtract the width
UWand the heightUHof the scaled image from the widthSWand the heightSHof the screen correspondingly, divide the results by 2, discard fractional part, give the resulting numbers the names ofXandYcorrespondingly. -
Display the scaled image at a distance
of
Xhorizontally andYvertically from the top-left corner of the screen.
-
Subtract the width
- Fill the rest screen space around the scaled image with black.
Reference implementation
The author’s library Integer
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
2032by the target aspect ratio4:3(≈ 1.333), that results in an approximate scaled width of2709.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.