File indexing completed on 2024-04-21 14:52:06

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2016 Torsten Rahn <tackat@kde.org>
0004 //
0005 
0006 #include "tileprocessor.h"
0007 
0008 #include <QFile>
0009 #include <QTextStream>
0010 #include <QPainter>
0011 #include <QDebug>
0012 #include <QFileInfo>
0013 #include <QDir>
0014 
0015 #include <cmath>
0016 
0017 int TileProcessor::progress = 0;
0018 
0019 TileProcessor::TileProcessor(QObject *parent) : QObject(parent),
0020     m_tileLevel(14)
0021 {
0022 
0023 }
0024 
0025 void TileProcessor::parseFileList(const QString& fileListUrl) {
0026 
0027     m_fileList.clear();
0028 
0029     QDir rootDir(fileListUrl);
0030 
0031     QStringList dirList = rootDir.entryList(QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot);
0032 
0033     for (int i = 0; i < dirList.length(); ++i) {
0034         QString urlPath = fileListUrl + QLatin1Char('/') + dirList.at(i);
0035         QDir columnDir = QDir(urlPath);
0036         QStringList files = columnDir.entryList(QStringList() << "*.jpg", QDir::Files);
0037         for (int j = 0; j < files.length(); ++j) {
0038             QString filePath = urlPath + QLatin1Char('/') + files.at(j);
0039             m_fileList << filePath;
0040         }
0041     }
0042     qDebug() << "Total file count: " << m_fileList.length();
0043 }
0044 
0045 void TileProcessor::loadReferenceImages(const QString& maskPath, const QString& bathymetryPath) {
0046     qDebug() << "Loading mask from" << maskPath;
0047     bool success = m_mask.load(maskPath);
0048     if (!success) qDebug() << "Loading mask failed: " << maskPath;
0049     qDebug() << "Loading bathymetry from" << bathymetryPath;
0050     success = m_bathymetry.load(bathymetryPath);
0051     if (!success) qDebug() << "Loading bathymetry failed: " << bathymetryPath;
0052     qDebug() << "Reference images loaded.";
0053 
0054     if (m_mask.width() != m_bathymetry.width() || m_mask.height() != m_bathymetry.height()) {
0055         qDebug() << "Mask and bathymetry geometries don't match:";
0056         qDebug() << "Mask: " << m_mask.width() << "x" << m_mask.height();
0057         qDebug() << "Bathymetry: " << m_bathymetry.width() << "x" << m_bathymetry.height();
0058     }
0059 }
0060 
0061 void TileProcessor::process() {
0062     for (int i = 0; i < m_fileList.length(); ++i) {
0063         QFileInfo fileInfo(m_fileList.at(i));
0064         if (fileInfo.isFile()) {
0065             colorForFile(m_fileList.at(i));
0066         }
0067     }
0068 }
0069 
0070 void TileProcessor::colorForFile(const QString& filePath){
0071     ++progress;
0072 
0073 //    int tileCountBathymetry = pow(2, m_tileLevel);
0074     int tileCount = pow(2, m_tileLevel);
0075 
0076     int xTile = filePath.section(QLatin1Char('/'), -2, -2).toUInt();
0077     int yTile = filePath.section(QLatin1Char('/'), -1).section(QLatin1Char('.'), 0, 0).toUInt();
0078     int x = m_bathymetry.width() * xTile / (double)tileCount;
0079 
0080     qreal lat_rad = atan(sinh(M_PI * (1 - 2 * yTile / (double)tileCount)));
0081     int y = (int)(m_bathymetry.height() * (-lat_rad + M_PI/2) / M_PI);
0082 
0083     int maskValueTopLeft = qRed(m_mask.pixel(2*x, 2*y));
0084     int maskValueTopRight = qRed(m_mask.pixel(2*x + 1, 2*y));
0085     int maskValueBottomLeft = qRed(m_mask.pixel(2*x, 2*y + 1));
0086     int maskValueBottomRight = qRed(m_mask.pixel(2*x + 1, 2*y + 1));
0087 
0088     if (maskValueTopLeft != 0 || maskValueTopRight != 0
0089             || maskValueBottomLeft != 0 || maskValueBottomRight != 0 ) { // for all areas which are not black
0090         QColor bathymetryColor = QColor(m_bathymetry.pixel(x, y));
0091 
0092         QImage tile(256, 256, QImage::Format_RGB32);
0093         tile.fill(bathymetryColor);
0094 
0095         QImage origTile;
0096         bool success = origTile.load(filePath);
0097 
0098         if (!success) qDebug() << "Loading tile failed: " << filePath;
0099 
0100         QPainter painter;
0101         painter.begin(&tile);
0102         qreal opacity;
0103         opacity = 1.0 - (double)(maskValueTopLeft)/255.0;
0104         painter.setOpacity(opacity);
0105         painter.drawImage(QRect(0, 0, 128, 128), origTile,
0106                           QRect(0, 0, 128, 128));
0107         opacity = 1.0 - (double)(maskValueTopRight)/255.0;
0108         painter.setOpacity(opacity);
0109         painter.drawImage(QRect(128, 0, 128, 128), origTile,
0110                           QRect(128, 0, 128, 128));
0111         opacity = 1.0 - (double)(maskValueBottomLeft)/255.0;
0112         painter.setOpacity(opacity);
0113         painter.drawImage(QRect(0, 128, 128, 128), origTile,
0114                           QRect(0, 128, 128, 128));
0115         opacity = 1.0 - (double)(maskValueBottomRight)/255.0;
0116         painter.setOpacity(opacity);
0117         painter.drawImage(QRect(128, 128, 128, 128), origTile,
0118                           QRect(128, 128, 128, 128));
0119         painter.end();
0120 
0121         QString modFilePath = filePath;
0122 //        modFilePath = modFilePath.replace(".", "_mod.");
0123 
0124         tile.save(modFilePath, "JPG", 85);
0125         if (opacity > 0.0) {
0126             qDebug() << progress << filePath.section(QLatin1Char('/'), -2, -2) << filePath.section(QLatin1Char('/'), -1).section(QLatin1Char('.'), 0, 0);
0127             qDebug() << maskValueTopLeft << modFilePath;
0128         }
0129     }
0130     else {
0131 //        qDebug() << maskValue;
0132     }
0133 }
0134 
0135 #include "moc_tileprocessor.cpp"