| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "WatermarkImagePreparer.h" |
| 21 | |
| 22 | WatermarkImagePreparer::WatermarkImagePreparer() |
| 23 | { |
| 24 | mOpacity = 0.15; |
| 25 | } |
| 26 | |
| 27 | QPixmap WatermarkImagePreparer::prepare(const QPixmap &image, const QSize &availableSpace, bool rotated) const |
| 28 | { |
| 29 | auto finishedWatermarkImage = getPreparedWatermarkImage(image, rotated); |
| 30 | return fitWatermarkIntoCapture(finishedWatermarkImage, availableSpace); |
| 31 | } |
| 32 | |
| 33 | QPixmap WatermarkImagePreparer::getPreparedWatermarkImage(const QPixmap &watermarkImage, bool rotated) const |
| 34 | { |
| 35 | auto preparedImage = watermarkImage; |
| 36 | if(rotated) { |
| 37 | preparedImage = getRotatedImage(watermarkImage); |
| 38 | } |
| 39 | |
| 40 | QPixmap transparentWatermark(preparedImage.size()); |
| 41 | transparentWatermark.fill(Qt::transparent); |
| 42 | QPainter painter(&transparentWatermark); |
| 43 | painter.setOpacity(mOpacity); |
| 44 | painter.drawPixmap(0, 0, preparedImage); |
| 45 | return transparentWatermark; |
| 46 | } |
| 47 | |
| 48 | QPixmap WatermarkImagePreparer::getRotatedImage(const QPixmap &watermarkImage) const |
| 49 | { |
| 50 | QTransform transform; |
| 51 | transform.rotate(45); |
| 52 | return watermarkImage.transformed(transform); |
| 53 | } |
| 54 | |
| 55 | QPixmap &WatermarkImagePreparer::fitWatermarkIntoCapture(QPixmap &finishedWatermarkImage, const QSize &availableSpace) const |
| 56 | { |
| 57 | auto widthRatio = (qreal) availableSpace.width() / (qreal) finishedWatermarkImage.size().width(); |
| 58 | auto heightRatio = (qreal) availableSpace.height() / (qreal) finishedWatermarkImage.size().height(); |
| 59 | if (widthRatio < 1 || heightRatio < 1) { |
| 60 | auto minRatio = qMin(widthRatio, heightRatio); |
| 61 | QTransform transform; |
| 62 | transform.scale(minRatio, minRatio); |
| 63 | finishedWatermarkImage = finishedWatermarkImage.transformed(transform); |
| 64 | } |
| 65 | return finishedWatermarkImage; |
| 66 | } |