Optimized image embedding algorithm

This commit is contained in:
hunbernd 2019-12-26 00:04:19 +01:00
parent 15ac7eecc8
commit cb68cb73d0

View File

@ -73,14 +73,12 @@ bool ImageUtil::optimizeSize(QString &html, const QImage& original, QImage &opti
//nothing to do if it fits into the limits
optimized = original;
if ((maxPixels <= 0) || (optimized.width()*optimized.height() <= maxPixels)) {
if(checkSize(html, optimized, maxBytes) <= maxBytes) {
int s = checkSize(html, optimized, maxBytes);
if((maxBytes <= 0) || (s <= maxBytes)) {
return true;
}
}
QVector<QRgb> ct;
quantization(original, ct);
//Downscale the image to fit into maxPixels
double whratio = (qreal)original.width() / (qreal)original.height();
int maxwidth;
@ -98,6 +96,9 @@ bool ImageUtil::optimizeSize(QString &html, const QImage& original, QImage &opti
return true;
}
QVector<QRgb> ct;
quantization(original, ct);
//Use binary search to find a suitable image size + linear regression to guess the file size
double maxsize = (double)checkSize(html, optimized = original.scaledToWidth(maxwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither), maxBytes);
if(maxsize <= maxBytes) return true; //success