File indexing completed on 2024-04-28 15:14:03

0001 /***************************************************************************
0002     File                 : ImageTools.cpp
0003     Project              : LabPlot
0004     Description          : Collection of different image processing algorithms
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2017 by Alexander Semke (alexander.semke@web.de)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 #include "ImageTools.h"
0029 
0030 /*!
0031     \class ImageToools
0032     \brief Provides static functions implementing different image processing algorithms.
0033     \ingroup tools
0034 */
0035 QImage ImageTools::blurred(const QImage& image, QRect rect, int radius, bool alphaOnly) {
0036     int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 };
0037     int alpha = (radius < 1)  ? 16 : (radius > 17) ? 1 : tab[radius-1];
0038 
0039     QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
0040     int r1 = rect.top();
0041     int r2 = rect.bottom();
0042     int c1 = rect.left();
0043     int c2 = rect.right();
0044 
0045     int bpl = result.bytesPerLine();
0046     int rgba[4];
0047     unsigned char* p;
0048 
0049     int i1 = 0;
0050     int i2 = 3;
0051 
0052     if (alphaOnly)
0053         i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::LittleEndian)*3;
0054 
0055     for (int col = c1; col <= c2; col++) {
0056         p = result.scanLine(r1) + col * 4;
0057         for (int i = i1; i <= i2; i++)
0058             rgba[i] = p[i] << 4;
0059 
0060         p += bpl;
0061         for (int j = r1; j < r2; j++, p += bpl)
0062             for (int i = i1; i <= i2; i++)
0063                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0064     }
0065 
0066     for (int row = r1; row <= r2; row++) {
0067         p = result.scanLine(row) + c1 * 4;
0068         for (int i = i1; i <= i2; i++)
0069             rgba[i] = p[i] << 4;
0070 
0071         p += 4;
0072         for (int j = c1; j < c2; j++, p += 4)
0073             for (int i = i1; i <= i2; i++)
0074                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0075     }
0076 
0077     for (int col = c1; col <= c2; col++) {
0078         p = result.scanLine(r2) + col * 4;
0079         for (int i = i1; i <= i2; i++)
0080             rgba[i] = p[i] << 4;
0081 
0082         p -= bpl;
0083         for (int j = r1; j < r2; j++, p -= bpl)
0084             for (int i = i1; i <= i2; i++)
0085                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0086     }
0087 
0088     for (int row = r1; row <= r2; row++) {
0089         p = result.scanLine(row) + c2 * 4;
0090         for (int i = i1; i <= i2; i++)
0091             rgba[i] = p[i] << 4;
0092 
0093         p -= 4;
0094         for (int j = c1; j < c2; j++, p -= 4)
0095             for (int i = i1; i <= i2; i++)
0096                 p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
0097     }
0098 
0099     return result;
0100 }