added auto-choose of image format when transparency is present

This commit is contained in:
csoler 2023-01-16 22:11:44 +01:00
parent de1b8f08d2
commit 7aacfb9aef
6 changed files with 47 additions and 23 deletions

View File

@ -31,9 +31,10 @@
#include "util/misc.h"
#include "gui/notifyqt.h"
#include <retroshare/rsidentity.h>
#include <retroshare/rspeers.h>
#include "retroshare/rsidentity.h"
#include "retroshare/rspeers.h"
#include "gui/common/FilesDefs.h"
#include "util/imageutil.h"
#include <iostream>
@ -576,8 +577,10 @@ void IdEditDialog::createId()
QByteArray ba;
QBuffer buffer(&ba);
bool has_transparency = ImageUtil::hasAlphaContent(mAvatar.toImage());
buffer.open(QIODevice::WriteOnly);
mAvatar.save(&buffer, "PNG"); // writes image into ba in PNG format
mAvatar.save(&buffer, has_transparency?"PNG":"JPG"); // writes image into ba in PNG format
params.mImage.copy((uint8_t *) ba.data(), ba.size());
}
@ -648,8 +651,10 @@ void IdEditDialog::updateId()
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
mAvatar.save(&buffer, "PNG"); // writes image into ba in PNG format
bool has_transparency = ImageUtil::hasAlphaContent(mAvatar.toImage());
buffer.open(QIODevice::WriteOnly);
mAvatar.save(&buffer, has_transparency?"PNG":"JPG"); // writes image into ba in PNG format
mEditGroup.mImage.copy((uint8_t *) ba.data(), ba.size());
}

View File

@ -22,8 +22,9 @@
#include "PostedGroupDialog.h"
#include "gui/gxs/GxsIdDetails.h"
#include "gui/common/FilesDefs.h"
#include "util/imageutil.h"
#include <retroshare/rswiki.h>
#include "retroshare/rswiki.h"
#include <iostream>
const uint32_t PostedCreateEnabledFlags = (
@ -104,8 +105,10 @@ void PostedGroupDialog::preparePostedGroup(RsPostedGroup &group, const RsGroupMe
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes image into ba in PNG format
bool has_transparency = ImageUtil::hasAlphaContent(pixmap.toImage());
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, has_transparency?"PNG":"JPG"); // writes image into ba in PNG format
group.mGroupImage.copy((uint8_t *) ba.data(), ba.size());
} else {

View File

@ -40,6 +40,7 @@
#include "util/misc.h"
#include "gui/common/FilesDefs.h"
#include "util/HandleRichText.h"
#include "util/imageutil.h"
#include "retroshare/rsinit.h"
#define ICONNAME "groupicon.png"
@ -138,10 +139,11 @@ void AvatarDialog::getAvatar(QByteArray &avatar)
return;
}
QBuffer buffer(&avatar);
bool has_transparency = ImageUtil::hasAlphaContent(pixmap.toImage());
QBuffer buffer(&avatar);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes image into ba in PNG format
pixmap.save(&buffer, has_transparency?"PNG":"JPG"); // writes image into ba in PNG format
}
void AvatarDialog::load()

View File

@ -21,10 +21,11 @@
#include <QBuffer>
#include "gui/gxs/GxsIdDetails.h"
#include "util/imageutil.h"
#include "gui/common/FilesDefs.h"
#include "GxsChannelGroupDialog.h"
#include <retroshare/rsgxschannels.h>
#include "retroshare/rsgxschannels.h"
#include <iostream>
// To start with we only have open forums - with distribution controls.
@ -119,8 +120,10 @@ void GxsChannelGroupDialog::prepareChannelGroup(RsGxsChannelGroup &group, const
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes image into ba in PNG format
bool has_transparency = ImageUtil::hasAlphaContent(pixmap.toImage());
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, has_transparency?"PNG":"JPG"); // writes image into ba in PNG format
group.mImage.copy((uint8_t *) ba.data(), ba.size());
} else {

View File

@ -137,6 +137,25 @@ bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original,
//std::cout << html.toStdString() << std::endl;
}
bool ImageUtil::hasAlphaContent(const QImage& image)
{
if(!image.hasAlphaChannel())
{
std::cerr << "Image of size " << image.width() << " x " << image.height() << ": No transparency content detected." << std::endl;
return false;
}
for(int i=0;i<image.width();++i)
for(int j=0;j<image.height();++j)
if(qAlpha(image.pixel(i,j)) < 255)
{
std::cerr << "Image of size " << image.width() << " x " << image.height() << ": Transparency content detected." << std::endl;
return true;
}
std::cerr << "Image of size " << image.width() << " x " << image.height() << ": No transparency content detected." << std::endl;
return false;
}
bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &optimized, int maxPixels, int maxBytes)
{
QByteArray bytearray;
@ -146,16 +165,7 @@ bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &
}
// check for transparency
bool has_transparency = false;
if(original.hasAlphaChannel())
for(int i=0;i<original.width();++i)
for(int j=0;j<original.height();++j)
if(qAlpha(original.pixel(i,j)) < 255)
{
has_transparency = true;
break;
}
bool has_transparency = hasAlphaContent(original);
if(optimizeSizeBytes(bytearray, original, optimized,has_transparency?"PNG":"JPG",maxPixels, maxBytes))
{

View File

@ -34,6 +34,7 @@ public:
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 optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes);
static bool hasAlphaContent(const QImage& image);
private:
static int checkSize(QByteArray& embeddedImage, const QImage& img, const char *format);