QPdf: Extract Method is_monochrome()

The old code repeatedly evaluated QImage::colorTable(), which returns
a vector by value.

Instead, factor the checks performed on the color table into
a helper function and pass the color table to it, reducing
the number of evaluations from three to one.

Also makes the code more readable, because the condition now
fits on a single line.

Change-Id: I82773c235047e76b87c8a9d630f7df9440430351
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@digia.com>
bb10
Marc Mutz 2014-09-02 10:46:01 +02:00
parent 3ded19c865
commit 23c1b132b1
1 changed files with 9 additions and 4 deletions

View File

@ -2323,6 +2323,14 @@ int QPdfEnginePrivate::addBrushPattern(const QTransform &m, bool *specifyColor,
return patternObj;
}
static inline bool is_monochrome(const QVector<QRgb> &colorTable)
{
return colorTable.size() == 2
&& colorTable.at(0) == QColor(Qt::black).rgba()
&& colorTable.at(1) == QColor(Qt::white).rgba()
;
}
/*!
* Adds an image to the pdf and return the pdf-object id. Returns -1 if adding the image failed.
*/
@ -2337,10 +2345,7 @@ int QPdfEnginePrivate::addImage(const QImage &img, bool *bitmap, qint64 serial_n
QImage image = img;
QImage::Format format = image.format();
if (image.depth() == 1 && *bitmap && img.colorTable().size() == 2
&& img.colorTable().at(0) == QColor(Qt::black).rgba()
&& img.colorTable().at(1) == QColor(Qt::white).rgba())
{
if (image.depth() == 1 && *bitmap && is_monochrome(img.colorTable())) {
if (format == QImage::Format_MonoLSB)
image = image.convertToFormat(QImage::Format_Mono);
format = QImage::Format_Mono;