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 "util/misc.h"
#include "gui/notifyqt.h" #include "gui/notifyqt.h"
#include <retroshare/rsidentity.h> #include "retroshare/rsidentity.h"
#include <retroshare/rspeers.h> #include "retroshare/rspeers.h"
#include "gui/common/FilesDefs.h" #include "gui/common/FilesDefs.h"
#include "util/imageutil.h"
#include <iostream> #include <iostream>
@ -576,8 +577,10 @@ void IdEditDialog::createId()
QByteArray ba; QByteArray ba;
QBuffer buffer(&ba); QBuffer buffer(&ba);
bool has_transparency = ImageUtil::hasAlphaContent(mAvatar.toImage());
buffer.open(QIODevice::WriteOnly); 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()); params.mImage.copy((uint8_t *) ba.data(), ba.size());
} }
@ -648,8 +651,10 @@ void IdEditDialog::updateId()
QByteArray ba; QByteArray ba;
QBuffer buffer(&ba); QBuffer buffer(&ba);
bool has_transparency = ImageUtil::hasAlphaContent(mAvatar.toImage());
buffer.open(QIODevice::WriteOnly); 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
mEditGroup.mImage.copy((uint8_t *) ba.data(), ba.size()); mEditGroup.mImage.copy((uint8_t *) ba.data(), ba.size());
} }

View file

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

View file

@ -40,6 +40,7 @@
#include "util/misc.h" #include "util/misc.h"
#include "gui/common/FilesDefs.h" #include "gui/common/FilesDefs.h"
#include "util/HandleRichText.h" #include "util/HandleRichText.h"
#include "util/imageutil.h"
#include "retroshare/rsinit.h" #include "retroshare/rsinit.h"
#define ICONNAME "groupicon.png" #define ICONNAME "groupicon.png"
@ -138,10 +139,11 @@ void AvatarDialog::getAvatar(QByteArray &avatar)
return; return;
} }
bool has_transparency = ImageUtil::hasAlphaContent(pixmap.toImage());
QBuffer buffer(&avatar); QBuffer buffer(&avatar);
buffer.open(QIODevice::WriteOnly); 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() void AvatarDialog::load()

View file

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

View file

@ -137,6 +137,25 @@ bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original,
//std::cout << html.toStdString() << std::endl; //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) bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &optimized, int maxPixels, int maxBytes)
{ {
QByteArray bytearray; QByteArray bytearray;
@ -146,16 +165,7 @@ bool ImageUtil::optimizeSizeHtml(QString &html, const QImage& original, QImage &
} }
// check for transparency // check for transparency
bool has_transparency = false; bool has_transparency = hasAlphaContent(original);
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;
}
if(optimizeSizeBytes(bytearray, original, optimized,has_transparency?"PNG":"JPG",maxPixels, maxBytes)) 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 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, const char *format, int maxPixels, int maxBytes); static bool optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes);
static bool hasAlphaContent(const QImage& image);
private: private:
static int checkSize(QByteArray& embeddedImage, const QImage& img, const char *format); static int checkSize(QByteArray& embeddedImage, const QImage& img, const char *format);