File indexing completed on 2024-05-05 03:48:22

0001 /*
0002     File                 : ImageTools.cpp
0003     Project              : LabPlot
0004     Description          : Collection of different image processing algorithms
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2017 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "ImageTools.h"
0011 
0012 /*!
0013     \class ImageToools
0014     \brief Provides static functions implementing different image processing algorithms.
0015     \ingroup tools
0016 */
0017 QImage ImageTools::blurred(const QImage& image, QRect rect, int radius, bool alphaOnly) {
0018     int tab[] = {14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2};
0019     int alpha = (radius < 1) ? 16 : (radius > 17) ? 1 : tab[radius - 1];
0020 
0021     QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
0022     int r1 = rect.top();
0023     int r2 = rect.bottom();
0024     int c1 = rect.left();
0025     int c2 = rect.right();
0026 
0027     int bpl = result.bytesPerLine();
0028     int rgba[4];
0029     unsigned char* p;
0030 
0031     int i1 = 0;
0032     int i2 = 3;
0033 
0034     if (alphaOnly)
0035         i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) * 3;
0036 
0037     for (int col = c1; col <= c2; col++) {
0038         p = result.scanLine(r1) + col * 4;
0039         for (int i = i1; i <= i2; i++)
0040             rgba[i] = p[i] << 4;
0041 
0042         p += bpl;
0043         for (int j = r1; j < r2; j++, p += bpl)
0044             for (int i = i1; i <= i2; i++)
0045                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0046     }
0047 
0048     for (int row = r1; row <= r2; row++) {
0049         p = result.scanLine(row) + c1 * 4;
0050         for (int i = i1; i <= i2; i++)
0051             rgba[i] = p[i] << 4;
0052 
0053         p += 4;
0054         for (int j = c1; j < c2; j++, p += 4)
0055             for (int i = i1; i <= i2; i++)
0056                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0057     }
0058 
0059     for (int col = c1; col <= c2; col++) {
0060         p = result.scanLine(r2) + col * 4;
0061         for (int i = i1; i <= i2; i++)
0062             rgba[i] = p[i] << 4;
0063 
0064         p -= bpl;
0065         for (int j = r1; j < r2; j++, p -= bpl)
0066             for (int i = i1; i <= i2; i++)
0067                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0068     }
0069 
0070     for (int row = r1; row <= r2; row++) {
0071         p = result.scanLine(row) + c2 * 4;
0072         for (int i = i1; i <= i2; i++)
0073             rgba[i] = p[i] << 4;
0074 
0075         p -= 4;
0076         for (int j = c1; j < c2; j++, p -= 4)
0077             for (int i = i1; i <= i2; i++)
0078                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0079     }
0080 
0081     return result;
0082 }