# Data Compression with a Time Limit
## 4 Lossy Compression  
As seen so far, the optimization algorithm allows us to obtain all the useful setups when we use lossless compressors. From these useful setups, it is then possible to obtain the so-called optimal mix once the time within which the compression must be completed has been set. If instead of using lossless compressors we consider lossy compressors, we will have to consider a third parameter: _quality_. This is because lossy compression leads to a trade-off between loss of information and compression obtained and it is necessary to evaluate the result of the compression not only in terms of compression but also in terms of quality of the decompressed image. The amount of data loss is determined by the level of compression achievable, as explained by rate-distortion theory. Lossy compression optimization becomes important, for example, in situations where compression must be performed every time data are to be transmitted because the quality is individually chosen by the remote user. In general, for many lossy compression tools, such as JPEG and WebP, when we perform the compression, it is possible to set the quality, often in a range of 0-100, where low values mean lower quality (and greater compression). Therefore, the greater the reduction in the size of the digital image is, the greater the loss of information relating to the image will be (and, consequently, the quality will be reduced). Figure 8: Optimal mix, t = 132,000 ms.  
We have therefore chosen to add a third parameter to the optimization algorithm: the _SSIM index_. The SSIM (Structural Similarity Index) is used to measure the similarity between two images. It is a perceptual metric that qualifies the degradation of image quality caused by processing, such as compression or loss of information in data transmission. This metric evaluates the similarity between two images of the same acquisition: in our case, an original reference image and an image first compressed and then decompressed. The SSIM was designed to improve traditional evaluation methods such as _PSNR_ and mean square error (_MSE_). The idea was to ask the user to set a limit on the SSIM parameter at the beginning of the optimization. Subsequently, we generate the pairs (\(b_{i}\), \(t_{i}\)), obtained by sequentially executing the compression algorithms on a data set D (in our case, the images) and then used as input for the useful setup search algorithm, but we select only those pairs whose compression returns us an SSIM value greater than or equal to of the SSIM limit value set. In detail, the optimization algorithm for lossy image compression (which we called _SSIM_Adaptive_) receives as input the SSIM parameter that we obtain by compressing with the maximum settable quality. Starting from this SSIM, the algorithm at each iteration compresses with a different, lower, quality that is calculated to obtain an SSIM value that is close to the SSIM limit and inserts each setup it tries into an array. At the end, we obtain \(q\) that indicates the minimum quality (0-100) to use during compression to obtain a SSIM that is closest and higher than the quality limit that was set. It is important to clarify that the SSIM value calculated during the execution of the _SSIM_Adaptive_ algorithm is a weighted average on the SSIM values of all the images given as input. Therefore, in addition to the set-up with the \(q\) received as output, we also take all the set-ups that have a quality greater than \(q\). By carrying out this pre-processing, we are sure that among the algorithms that will be chosen from the optimal mix only those with a quality that respects that entered by the user or even higher will appear. In this way, we can compress D, respecting the time budget and using only the algorithms with a quality greater than or equal to the fixed one. We experimentally tested our approach on images that were acquired as we did in Section 3.2.