30#include <QRegularExpression>
33using namespace Qt::StringLiterals;
50 static const QSet<QString> sFilter { u
"REQUEST"_s, u
"VERSION"_s, u
"SERVICE"_s, u
"LAYERS"_s, u
"STYLES"_s, u
"SLD_VERSION"_s, u
"_DC"_s };
55 const QList<QPair<QString, QString>> queryItems = q.queryItems();
56 for (
const QPair<QString, QString> ¶m : queryItems )
58 if ( sFilter.contains( param.first.toUpper() ) )
59 q.removeAllQueryItems( param.first );
71 if ( format.compare(
"png"_L1, Qt::CaseInsensitive ) == 0 || format.compare(
"image/png"_L1, Qt::CaseInsensitive ) == 0 )
75 else if ( format.compare(
"jpg "_L1, Qt::CaseInsensitive ) == 0 || format.compare(
"image/jpeg"_L1, Qt::CaseInsensitive ) == 0 )
79 else if ( format.compare(
"webp"_L1, Qt::CaseInsensitive ) == 0 || format.compare(
"image/webp"_L1, Qt::CaseInsensitive ) == 0 )
86 const thread_local QRegularExpression modeExpr = QRegularExpression( u
"image/png\\s*;\\s*mode=([^;]+)"_s, QRegularExpression::CaseInsensitiveOption );
88 const QRegularExpressionMatch match = modeExpr.match( format );
89 const QString mode = match.captured( 1 );
90 if ( mode.compare(
"16bit"_L1, Qt::CaseInsensitive ) == 0 )
92 if ( mode.compare(
"8bit"_L1, Qt::CaseInsensitive ) == 0 )
94 if ( mode.compare(
"1bit"_L1, Qt::CaseInsensitive ) == 0 )
108 switch ( outputFormat )
112 contentType = u
"image/png"_s;
113 saveFormat = u
"PNG"_s;
117 QVector<QRgb> colorTable;
122 const QImage img256 = img.convertToFormat( QImage::Format_ARGB32 );
124 result = img256.convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
126 contentType = u
"image/png"_s;
127 saveFormat = u
"PNG"_s;
130 result = img.convertToFormat( QImage::Format_ARGB4444_Premultiplied );
131 contentType = u
"image/png"_s;
132 saveFormat = u
"PNG"_s;
135 result = img.convertToFormat( QImage::Format_Mono, Qt::MonoOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
136 contentType = u
"image/png"_s;
137 saveFormat = u
"PNG"_s;
141 contentType = u
"image/jpeg"_s;
142 saveFormat = u
"JPEG"_s;
146 contentType = u
"image/webp"_s;
147 saveFormat = u
"WEBP"_s;
151 saveFormat = u
"Unknown"_s;
156 result.setDotsPerMeterX( img.dotsPerMeterX() );
157 result.setDotsPerMeterY( img.dotsPerMeterY() );
161 response.
setHeader(
"Content-Type", contentType );
162 if ( saveFormat ==
"JPEG"_L1 || saveFormat ==
"WEBP"_L1 )
164 result.save( response.
io(), qPrintable( saveFormat ), imageQuality );
168 result.save( response.
io(), qPrintable( saveFormat ) );
174 parameter.
mValue = formatStr;
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
static QString wmsServiceUrl(const QgsProject &project, const QgsServerRequest &request=QgsServerRequest(), const QgsServerSettings &settings=QgsServerSettings())
Returns the WMS service url.
Defines requests passed to QgsService classes.
QUrl originalUrl() const
Returns the request url as seen by the web server, by default this is equal to the url seen by QGIS s...
Defines the response interface passed to QgsService.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual QIODevice * io()=0
Returns the underlying QIODevice.
Provides a way to retrieve settings by prioritizing according to environment variables,...
Exception thrown in case of malformed request.
WMS parameter received from the client.
Median cut implementation.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
void medianCut(QVector< QRgb > &colorTable, int nColors, const QImage &inputImage)
Median cut implementation used when reducing RGB colors to palletized colors.
QString implementationVersion()
Returns the highest version supported by this implementation.
ImageOutputFormat
Supported image output format.
@ Unknown
Unknown/invalid format.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
QUrl serviceUrl(const QgsServerRequest &request, const QgsProject *project, const QgsServerSettings &settings)
Returns WMS service URL.