From 23c1b132b1727caee1a4891c34ef27e3a5697df5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 2 Sep 2014 10:46:01 +0200 Subject: [PATCH] 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 --- src/gui/painting/qpdf.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/gui/painting/qpdf.cpp b/src/gui/painting/qpdf.cpp index 0c888d645d..c7f3c0fd5b 100644 --- a/src/gui/painting/qpdf.cpp +++ b/src/gui/painting/qpdf.cpp @@ -2323,6 +2323,14 @@ int QPdfEnginePrivate::addBrushPattern(const QTransform &m, bool *specifyColor, return patternObj; } +static inline bool is_monochrome(const QVector &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;