Find scale factors to maximize images size

26 Views Asked by At

I have two images with dimensions $w_1 \times h_1$ and $w_2 \times h_2$ respectively. I want to display them in a screen of size $w_{screen} \times h_{screen}$. My goal is to resize those images with respect to their aspect ratio such that their sizes are maximized in the screen.

The images will be placed next to each other in the screen, and their layout depends on the screen orientation (as a top-bottom in portrait, or as a side-by-side in landscape).

The images can have different aspect ratio, meaning that they are not just squares. They can have any kind of size (with $w_1, h_1, w_2, h_2 \gt 0$).

Hence, I'm looking for two scale factors $\alpha$ and $\beta$ that are associated to the first and second image respectively.

To compute the value of $\alpha$ and $\beta$, I use the following linear programming model:

We consider $\alpha$ and $\beta$ to be the variables to optimize:

$$ \max_{\alpha, \beta} \quad z = \alpha \cdot w_1 \cdot h_1 + \beta \cdot w_2 \cdot h_2 $$

subject to:

$$ \begin{align} \alpha \cdot w_1 &\le w_{screen} \tag{1}\label{eq1}\\ \alpha \cdot h_1 &\le h_{screen} \tag{2}\label{eq2}\\ \beta \cdot w_2 &\le w_{screen} \tag{3}\label{eq3}\\ \beta \cdot h_2 &\le h_{screen \tag{4}\label{eq4}}\\ \alpha, \beta &\ge 0 \tag{5}\label{eq5}\\ \end{align} $$

(In the optimization function $z$, I use the image area as a unit to measure how big they are).

Although I can use the simplex algorithm to resolve the problem, my final goal is to use this method in a computer program to realize a responsive user interface, and thus I would like to find a simpler formula to compute $\alpha$ and $\beta$.