1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "SunLightBlending.h"
// C++ includes
#include <cmath>
// Qt includes
#include <QImage>
#include <QColor>
// Local includes
#include "SunLocator.h"
#include "TextureTile.h"
#include "TileLoaderHelper.h"
#include "MarbleGlobal.h"
#include "digikam_debug.h"
namespace Marble
{
SunLightBlending::SunLightBlending(const SunLocator* sunLocator)
: Blending(),
m_sunLocator(sunLocator),
m_levelZeroColumns(0),
m_levelZeroRows(0)
{
}
SunLightBlending::~SunLightBlending()
{
}
void SunLightBlending::blend(QImage* const tileImage, TextureTile const* const top) const
{
if (tileImage->depth() != 32)
{
return;
}
// TODO add support for 8-bit maps?
// add sun shading
const TileId id = top->id();
const qreal global_width = tileImage->width()
* TileLoaderHelper::levelToColumn(m_levelZeroColumns, id.zoomLevel());
const qreal global_height = tileImage->height()
* TileLoaderHelper::levelToRow(m_levelZeroRows, id.zoomLevel());
const qreal lon_scale = 2 * M_PI / global_width;
const qreal lat_scale = -M_PI / global_height;
const int tileHeight = tileImage->height();
const int tileWidth = tileImage->width();
// First we determine the supporting point interval for the interpolation.
const int n = maxDivisor(30, tileWidth);
const int ipRight = n * (int)(tileWidth / n);
const QImage* nighttile = top->image();
for (int cur_y = 0; cur_y < tileHeight; ++cur_y)
{
const qreal lat = lat_scale * (id.y() * tileHeight + cur_y) - 0.5 * M_PI;
const qreal a = sin((lat + DEG2RAD * m_sunLocator->getLat()) / 2.0);
const qreal c = cos(lat) * cos(-DEG2RAD * m_sunLocator->getLat());
QRgb* scanline = reinterpret_cast<QRgb*>(tileImage->scanLine(cur_y));
const QRgb* nscanline = reinterpret_cast<const QRgb*>(nighttile->scanLine(cur_y));
qreal lastShade = -10.0;
int cur_x = 0;
while (cur_x < tileWidth)<--- Assuming that condition 'cur_x
{
const bool interpolate = (cur_x != 0 && cur_x < ipRight && cur_x + n < tileWidth);
qreal shade = 0;
if (interpolate)
{
const int check = cur_x + n;
const qreal checklon = lon_scale * (id.x() * tileWidth + check);
shade = m_sunLocator->shading(checklon, a, c);
// if the shading didn't change across the interpolation
// interval move on and don't change anything.
if (shade == lastShade && shade == 1.0)
{
scanline += n;
nscanline += n;
cur_x += n;
continue;
}
if (shade == lastShade && shade == 0.0)
{
for (int t = 0; t < n; ++t)
{
SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
++scanline;
++nscanline;
}
cur_x += n;
continue;
}
qreal lon = lon_scale * (id.x() * tileWidth + cur_x);
for (int t = 0; t < n ; ++t)
{
shade = m_sunLocator->shading(lon, a, c);
SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
++scanline;
++nscanline;
lon += lon_scale;
}
cur_x += n;
}
else
{
// Make sure we don't exceed the image memory
if (cur_x < tileWidth)<--- Condition 'cur_x
{
qreal lon = lon_scale * (id.x() * tileWidth + cur_x);
shade = m_sunLocator->shading(lon, a, c);
SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
++scanline;
++nscanline;
++cur_x;
}
}
lastShade = shade;
}
}
}
void SunLightBlending::setLevelZeroLayout(int levelZeroColumns, int levelZeroRows)
{
m_levelZeroColumns = levelZeroColumns;
m_levelZeroRows = levelZeroRows;
}
// TODO: This should likely go into a math class Q_DECL_HIDDEN in the future ...
int SunLightBlending::maxDivisor(int maximum, int fullLength)
{
// Find the optimal interpolation interval n for the
// current image canvas width
int best = 2;
int nEvalMin = fullLength;
for (int it = 1; it <= maximum; ++it)
{
// The optimum is the interval which results in the least amount
// supporting points taking into account the rest which can't
// get used for interpolation.
int nEval = fullLength / it + fullLength % it;
if (nEval < nEvalMin)
{
nEvalMin = nEval;
best = it;
}
}
return best;
}
} // namespace Marble
|