9#include "GenericScanlineTextureMapper.h"
17#include "GeoPainter.h"
18#include "MarbleDebug.h"
19#include "MarbleDirs.h"
20#include "MathHelper.h"
21#include "ScanlineTextureMapperContext.h"
22#include "StackedTileLoader.h"
23#include "TextureColorizer.h"
29class GenericScanlineTextureMapper::RenderJob :
public QRunnable
32 RenderJob(StackedTileLoader *tileLoader,
int tileLevel, QImage *canvasImage,
const ViewportParams *viewport,
MapQuality mapQuality,
int yTop,
int yBottom);
37 StackedTileLoader *
const m_tileLoader;
38 const int m_tileLevel;
39 QImage *
const m_canvasImage;
40 const ViewportParams *
const m_viewport;
53 : m_tileLoader(tileLoader)
54 , m_tileLevel(tileLevel)
55 , m_canvasImage(canvasImage)
56 , m_viewport(viewport)
57 , m_mapQuality(mapQuality)
63GenericScanlineTextureMapper::GenericScanlineTextureMapper(
StackedTileLoader *tileLoader)
64 : TextureMapperInterface()
65 , m_tileLoader(tileLoader)
71void GenericScanlineTextureMapper::mapTexture(
GeoPainter *painter,
74 const QRect &dirtyRect,
75 TextureColorizer *texColorizer)
77 if (m_canvasImage.size() != viewport->size() || m_radius != viewport->radius()) {
78 const QImage::Format optimalFormat = ScanlineTextureMapperContext::optimalCanvasImageFormat(viewport);
80 if (m_canvasImage.size() != viewport->size() || m_canvasImage.format() != optimalFormat) {
81 m_canvasImage = QImage(viewport->size(), optimalFormat);
84 if (!viewport->mapCoversViewport()) {
85 m_canvasImage.fill(0);
88 m_radius = viewport->radius();
89 m_repaintNeeded =
true;
92 if (m_repaintNeeded) {
93 mapTexture(viewport, tileZoomLevel, painter->
mapQuality());
96 texColorizer->colorize(&m_canvasImage, viewport, painter->
mapQuality());
99 m_repaintNeeded =
false;
102 const int radius = viewport->radius() * viewport->currentProjection()->clippingRadius();
104 QRect rect(viewport->width() / 2 - radius, viewport->height() / 2 - radius, 2 * radius, 2 * radius);
105 rect = rect.intersected(dirtyRect);
106 painter->
drawImage(rect, m_canvasImage, rect);
109void GenericScanlineTextureMapper::mapTexture(
const ViewportParams *viewport,
int tileZoomLevel,
MapQuality mapQuality)
112 m_tileLoader->resetTilehash();
114 const int imageHeight = viewport->height();
115 const qint64 radius = viewport->radius() * viewport->currentProjection()->clippingRadius();
118 const int skip = (mapQuality ==
LowQuality) ? 1 : 0;
119 const int yTop = (imageHeight / 2 - radius >= 0) ? imageHeight / 2 - radius : 0;
120 const int yBottom = (yTop == 0) ? imageHeight - skip : yTop + radius + radius - skip;
122 const int numThreads = m_threadPool.maxThreadCount();
123 const int yStep = qCeil(qreal(yBottom - yTop) / qreal(numThreads));
124 for (
int i = 0; i < numThreads; ++i) {
125 const int yStart = yTop + i * yStep;
126 const int yEnd = qMin(yBottom, yTop + (i + 1) * yStep);
127 QRunnable *
const job =
new RenderJob(m_tileLoader, tileZoomLevel, &m_canvasImage, viewport, mapQuality, yStart, yEnd);
128 m_threadPool.start(job);
131 m_threadPool.waitForDone();
133 m_tileLoader->cleanupTilehash();
136void GenericScanlineTextureMapper::RenderJob::run()
138 const int imageWidth = m_canvasImage->width();
139 const int imageHeight = m_canvasImage->height();
140 const qint64 radius = m_viewport->radius();
142 const bool interlaced = (m_mapQuality ==
LowQuality);
144 const bool printQuality = (m_mapQuality ==
PrintQuality);
147 const int n = ScanlineTextureMapperContext::interpolationStep(m_viewport, m_mapQuality);
150 qreal northPoleX, northPoleY;
151 bool globeHidesNorthPole;
152 GeoDataCoordinates northPole(0, m_viewport->currentProjection()->maxLat(), 0);
153 m_viewport->screenCoordinates(northPole, northPoleX, northPoleY, globeHidesNorthPole);
157 ScanlineTextureMapperContext context(m_tileLoader, m_tileLevel);
159 qreal clipRadius = radius * m_viewport->currentProjection()->clippingRadius();
162 for (
int y = m_yTop; y < m_yBottom; ++y) {
164 const int rx = (int)sqrt((qreal)(clipRadius * clipRadius - ((y - imageHeight / 2) * (y - imageHeight / 2))));
178 const int xLeft = (imageWidth / 2 - rx > 0) ? imageWidth / 2 - rx : 0;
179 const int xRight = (imageWidth / 2 - rx > 0) ? xLeft + rx + rx : imageWidth;
181 QRgb *scanLine = (QRgb *)(m_canvasImage->scanLine(y)) + xLeft;
183 const int xIpLeft = (imageWidth / 2 - rx > 0) ? n * (
int)(xLeft / n + 1) : 1;
184 const int xIpRight = (imageWidth / 2 - rx > 0) ? n * (
int)(xRight / n - 1) : n * (int)(xRight / n - 1) + 1;
187 bool crossingPoleArea =
false;
188 if (!globeHidesNorthPole && northPoleY - (n * 0.75) <= y && northPoleY + (n * 0.75) >= y) {
189 crossingPoleArea =
true;
194 for (
int x = xLeft; x < xRight; ++x) {
196 const int leftInterval = xIpLeft + ncount * n;
198 bool interpolate =
false;
200 if (x >= xIpLeft && x <= xIpRight) {
202 if (crossingPoleArea && northPoleX >= leftInterval + n && northPoleX < leftInterval + 2 * n && x < leftInterval + 3 * n) {
206 interpolate = !printQuality;
214 m_viewport->geoCoordinates(x, y, lon, lat, GeoDataCoordinates::Radian);
218 context.pixelValueApproxF(lon, lat, scanLine, n);
220 context.pixelValueApprox(lon, lat, scanLine, n);
225 if (x < imageWidth) {
227 context.pixelValueF(lon, lat, scanLine);
229 context.pixelValue(lon, lat, scanLine);
236 if (interlaced && y + 1 < m_yBottom) {
237 const int pixelByteSize = m_canvasImage->bytesPerLine() / imageWidth;
239 memcpy(m_canvasImage->scanLine(y + 1) + xLeft * pixelByteSize,
240 m_canvasImage->scanLine(y) + xLeft * pixelByteSize,
241 (xRight - xLeft) * pixelByteSize);
This file contains the headers for AbstractProjection.
This file contains the headers for ViewParameters.
This file contains the headers for ViewportParams.
A painter that allows to draw geometric primitives on the map.
MapQuality mapQuality() const
Returns the map quality.
void drawImage(const GeoDataCoordinates ¢erPosition, const QImage &image)
Draws an image at the given position. The image is placed with its center located at the given center...
Tile loading from a quad tree.
A public class that controls what is visible in the viewport of a Marble map.
Binds a QML item to a specific geodetic location in screen coordinates.
MapQuality
This enum is used to choose the map quality shown in the view.
@ HighQuality
High quality (e.g. antialiasing for lines)
@ PrintQuality
Print quality.
@ LowQuality
Low resolution (e.g. interlaced)