mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-07-24 07:00:57 -04:00
added parameter to specify format to optimizeImageSize()
This commit is contained in:
parent
31bdd6c226
commit
516e4f7c12
10 changed files with 21 additions and 21 deletions
|
@ -115,7 +115,7 @@ void AlbumGroupDialog::prepareAlbumGroup(RsPhotoAlbum &group, const RsGroupMetaD
|
||||||
QBuffer buffer(&ba);
|
QBuffer buffer(&ba);
|
||||||
|
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
pixmap.save(&buffer, "JPG"); // writes image into ba in PNG format
|
pixmap.save(&buffer, "PNG"); // writes image into ba in PNG format
|
||||||
|
|
||||||
group.mThumbnail.copy((uint8_t *) ba.data(), ba.size());
|
group.mThumbnail.copy((uint8_t *) ba.data(), ba.size());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -224,7 +224,7 @@ void PostedCreatePostDialog::addPicture()
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage opt;
|
QImage opt;
|
||||||
if(ImageUtil::optimizeSizeBytes(imagebytes, image, opt, 640*480, MAXMESSAGESIZE - 2000)) { //Leave space for other stuff
|
if(ImageUtil::optimizeSizeBytes(imagebytes, image, opt,"JPG", 640*480, MAXMESSAGESIZE - 2000)) { //Leave space for other stuff
|
||||||
ui->imageLabel->setPixmap(QPixmap::fromImage(opt));
|
ui->imageLabel->setPixmap(QPixmap::fromImage(opt));
|
||||||
ui->stackedWidgetPicture->setCurrentIndex(IMG_PICTURE);
|
ui->stackedWidgetPicture->setCurrentIndex(IMG_PICTURE);
|
||||||
ui->removeButton->show();
|
ui->removeButton->show();
|
||||||
|
|
|
@ -126,7 +126,7 @@ void WireGroupDialog::prepareWireGroup(RsWireGroup &group, const RsGroupMetaData
|
||||||
QBuffer buffer(&ba);
|
QBuffer buffer(&ba);
|
||||||
|
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
pixmap.save(&buffer, "JPG");
|
pixmap.save(&buffer, "PNG");
|
||||||
|
|
||||||
group.mMasthead.copy((uint8_t *) ba.data(), ba.size());
|
group.mMasthead.copy((uint8_t *) ba.data(), ba.size());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -68,12 +68,12 @@ void ImageUtil::extractImage(QWidget *window, QTextCursor cursor, QString file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, int maxPixels, int maxBytes)
|
bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes)
|
||||||
{
|
{
|
||||||
//nothing to do if it fits into the limits
|
//nothing to do if it fits into the limits
|
||||||
optimized = original;
|
optimized = original;
|
||||||
if ((maxPixels <= 0) || (optimized.width()*optimized.height() <= maxPixels)) {
|
if ((maxPixels <= 0) || (optimized.width()*optimized.height() <= maxPixels)) {
|
||||||
int s = checkSize(bytearray, optimized);
|
int s = checkSize(bytearray, optimized,format);
|
||||||
if((maxBytes <= 0) || (s <= maxBytes)) {
|
if((maxBytes <= 0) || (s <= maxBytes)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original,
|
||||||
|
|
||||||
//if maxBytes not defined, do not reduce color space, just downscale
|
//if maxBytes not defined, do not reduce color space, just downscale
|
||||||
if(maxBytes <= 0) {
|
if(maxBytes <= 0) {
|
||||||
checkSize(bytearray, optimized = original.scaledToWidth(maxwidth, Qt::SmoothTransformation));
|
checkSize(bytearray, optimized = original.scaledToWidth(maxwidth, Qt::SmoothTransformation),format);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,9 +100,9 @@ bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original,
|
||||||
quantization(original, ct);
|
quantization(original, ct);
|
||||||
|
|
||||||
//Use binary search to find a suitable image size + linear regression to guess the file size
|
//Use binary search to find a suitable image size + linear regression to guess the file size
|
||||||
double maxsize = (double)checkSize(bytearray, optimized = original.scaledToWidth(maxwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither));
|
double maxsize = (double)checkSize(bytearray, optimized = original.scaledToWidth(maxwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither),format);
|
||||||
if(maxsize <= maxBytes) return true; //success
|
if(maxsize <= maxBytes) return true; //success
|
||||||
double minsize = (double)checkSize(bytearray, optimized = original.scaledToWidth(minwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither));
|
double minsize = (double)checkSize(bytearray, optimized = original.scaledToWidth(minwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither),format);
|
||||||
if(minsize > maxBytes) return false; //impossible
|
if(minsize > maxBytes) return false; //impossible
|
||||||
|
|
||||||
// std::cout << "maxS: " << maxsize << " minS: " << minsize << std::endl;
|
// std::cout << "maxS: " << maxsize << " minS: " << minsize << std::endl;
|
||||||
|
@ -115,7 +115,7 @@ bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original,
|
||||||
double b = maxsize - m * ((double)maxwidth * (double)maxwidth / whratio);
|
double b = maxsize - m * ((double)maxwidth * (double)maxwidth / whratio);
|
||||||
double a = ((double)(maxBytes - region/2) - b) / m; //maxBytes - region/2 target the center of the accepted region
|
double a = ((double)(maxBytes - region/2) - b) / m; //maxBytes - region/2 target the center of the accepted region
|
||||||
int nextwidth = (int)sqrt(a * whratio);
|
int nextwidth = (int)sqrt(a * whratio);
|
||||||
int nextsize = checkSize(bytearray, optimized = original.scaledToWidth(nextwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither));
|
int nextsize = checkSize(bytearray, optimized = original.scaledToWidth(nextwidth, Qt::SmoothTransformation).convertToFormat(QImage::Format_Indexed8, ct, Qt::ThresholdDither),format);
|
||||||
if(nextsize <= maxBytes) {
|
if(nextsize <= maxBytes) {
|
||||||
minsize = nextsize;
|
minsize = nextsize;
|
||||||
minwidth = nextwidth;
|
minwidth = nextwidth;
|
||||||
|
@ -145,7 +145,7 @@ bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &
|
||||||
if(maxBytes < 1) maxBytes = 1;
|
if(maxBytes < 1) maxBytes = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(optimizeSizeBytes(bytearray, original, optimized, maxPixels, maxBytes))
|
if(optimizeSizeBytes(bytearray, original, optimized,"PNG",maxPixels, maxBytes))
|
||||||
{
|
{
|
||||||
QByteArray encodedByteArray = bytearray.toBase64();
|
QByteArray encodedByteArray = bytearray.toBase64();
|
||||||
html = "<img src=\"data:image/png;base64,";
|
html = "<img src=\"data:image/png;base64,";
|
||||||
|
@ -156,7 +156,7 @@ bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ImageUtil::checkSize(QByteArray &bytearray, const QImage &img)
|
int ImageUtil::checkSize(QByteArray &bytearray, const QImage &img,const char *format)
|
||||||
{
|
{
|
||||||
rstime::RsScopeTimer st("Check size");
|
rstime::RsScopeTimer st("Check size");
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ int ImageUtil::checkSize(QByteArray &bytearray, const QImage &img)
|
||||||
|
|
||||||
//std::cout << QString("Trying image: format PNG, size %1x%2, colors %3\n").arg(img.width()).arg(img.height()).arg(img.colorCount()).toStdString();
|
//std::cout << QString("Trying image: format PNG, size %1x%2, colors %3\n").arg(img.width()).arg(img.height()).arg(img.colorCount()).toStdString();
|
||||||
if (buffer.open(QIODevice::WriteOnly)) {
|
if (buffer.open(QIODevice::WriteOnly)) {
|
||||||
if (img.save(&buffer, "JPG", 0)) {
|
if (img.save(&buffer, format, 0)) {
|
||||||
size = bytearray.length();
|
size = bytearray.length();
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "ImageUtil: image can't be saved to buffer" << std::endl;
|
std::cerr << "ImageUtil: image can't be saved to buffer" << std::endl;
|
||||||
|
|
|
@ -33,10 +33,10 @@ public:
|
||||||
|
|
||||||
static void extractImage(QWidget *window, QTextCursor cursor, QString file = "");
|
static void extractImage(QWidget *window, QTextCursor cursor, QString file = "");
|
||||||
static bool optimizeSizeHtml(QString &html, const QImage& original, QImage &optimized, int maxPixels = -1, int maxBytes = -1);
|
static bool optimizeSizeHtml(QString &html, const QImage& original, QImage &optimized, int maxPixels = -1, int maxBytes = -1);
|
||||||
static bool optimizeSizeBytes(QByteArray &bytearray, const QImage& original, QImage &optimized, int maxPixels = -1, int maxBytes = -1);
|
static bool optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int checkSize(QByteArray& embeddedImage, const QImage& img);
|
static int checkSize(QByteArray& embeddedImage, const QImage& img, const char *format);
|
||||||
static void quantization(const QImage& img, QVector<QRgb>& palette);
|
static void quantization(const QImage& img, QVector<QRgb>& palette);
|
||||||
static void quantization(QList<QRgb>::iterator begin, QList<QRgb>::iterator end, int depth, QVector<QRgb>& palette);
|
static void quantization(QList<QRgb>::iterator begin, QList<QRgb>::iterator end, int depth, QVector<QRgb>& palette);
|
||||||
static void avgbucket(QList<QRgb>::iterator begin, QList<QRgb>::iterator end, QVector<QRgb>& palette);
|
static void avgbucket(QList<QRgb>::iterator begin, QList<QRgb>::iterator end, QVector<QRgb>& palette);
|
||||||
|
|
|
@ -291,7 +291,7 @@ bool misc::getOpenAvatarPicture(QWidget *parent, QByteArray &image_data)
|
||||||
// save image in QByteArray
|
// save image in QByteArray
|
||||||
QBuffer buffer(&image_data);
|
QBuffer buffer(&image_data);
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
picture.save(&buffer, "JPG"); // writes image into ba in JPG format
|
picture.save(&buffer, "PNG"); // writes image into ba in PNG format
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue