• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • blendings
SunLightBlending.cpp
Go to the documentation of this file.
1 // Copyright 2010 Jens-Michael Hoffmann <jmho@c-xx.com>
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include "SunLightBlending.h"
17 
18 #include "MarbleDebug.h"
19 #include "SunLocator.h"
20 #include "TextureTile.h"
21 #include "TileLoaderHelper.h"
22 #include "MarbleGlobal.h"
23 
24 #include <QImage>
25 
26 #include <cmath>
27 
28 namespace Marble
29 {
30 
31 SunLightBlending::SunLightBlending( const SunLocator * sunLocator )
32  : Blending(),
33  m_sunLocator( sunLocator ),
34  m_levelZeroColumns( 0 ),
35  m_levelZeroRows( 0 )
36 {
37 }
38 
39 SunLightBlending::~SunLightBlending()
40 {
41 }
42 
43 void SunLightBlending::blend( QImage * const tileImage, TextureTile const * const top ) const
44 {
45  if ( tileImage->depth() != 32 )
46  return;
47 
48  // TODO add support for 8-bit maps?
49  // add sun shading
50  const TileId id = top->id();
51  const qreal global_width = tileImage->width()
52  * TileLoaderHelper::levelToColumn( m_levelZeroColumns, id.zoomLevel() );
53  const qreal global_height = tileImage->height()
54  * TileLoaderHelper::levelToRow( m_levelZeroRows, id.zoomLevel() );
55  const qreal lon_scale = 2*M_PI / global_width;
56  const qreal lat_scale = -M_PI / global_height;
57  const int tileHeight = tileImage->height();
58  const int tileWidth = tileImage->width();
59 
60  // First we determine the supporting point interval for the interpolation.
61  const int n = maxDivisor( 30, tileWidth );
62  const int ipRight = n * (int)( tileWidth / n );
63 
64  const QImage *nighttile = top->image();
65 
66  for ( int cur_y = 0; cur_y < tileHeight; ++cur_y ) {
67  const qreal lat = lat_scale * ( id.y() * tileHeight + cur_y ) - 0.5*M_PI;
68  const qreal a = sin( ( lat+DEG2RAD * m_sunLocator->getLat() )/2.0 );
69  const qreal c = cos(lat)*cos( -DEG2RAD * m_sunLocator->getLat() );
70 
71  QRgb* scanline = (QRgb*)tileImage->scanLine( cur_y );
72  const QRgb* nscanline = (QRgb*)nighttile->scanLine( cur_y );
73 
74  qreal lastShade = -10.0;
75 
76  int cur_x = 0;
77 
78  while ( cur_x < tileWidth ) {
79 
80  const bool interpolate = ( cur_x != 0 && cur_x < ipRight && cur_x + n < tileWidth );
81 
82  qreal shade = 0;
83 
84  if ( interpolate ) {
85  const int check = cur_x + n;
86  const qreal checklon = lon_scale * ( id.x() * tileWidth + check );
87  shade = m_sunLocator->shading( checklon, a, c );
88 
89  // if the shading didn't change across the interpolation
90  // interval move on and don't change anything.
91  if ( shade == lastShade && shade == 1.0 ) {
92  scanline += n;
93  nscanline += n;
94  cur_x += n;
95  continue;
96  }
97  if ( shade == lastShade && shade == 0.0 ) {
98  for ( int t = 0; t < n; ++t ) {
99  m_sunLocator->shadePixelComposite( *scanline, *nscanline, shade );
100  ++scanline;
101  ++nscanline;
102  }
103  cur_x += n;
104  continue;
105  }
106  for ( int t = 0; t < n ; ++t ) {
107  qreal lon = lon_scale * ( id.x() * tileWidth + cur_x );
108  shade = m_sunLocator->shading( lon, a, c );
109  m_sunLocator->shadePixelComposite( *scanline, *nscanline, shade );
110  ++scanline;
111  ++nscanline;
112  ++cur_x;
113  }
114  }
115 
116  else {
117  // Make sure we don't exceed the image memory
118  if ( cur_x < tileWidth ) {
119  qreal lon = lon_scale * ( id.x() * tileWidth + cur_x );
120  shade = m_sunLocator->shading( lon, a, c );
121  m_sunLocator->shadePixelComposite( *scanline, *nscanline, shade );
122  ++scanline;
123  ++nscanline;
124  ++cur_x;
125  }
126  }
127  lastShade = shade;
128  }
129  }
130 }
131 
132 void SunLightBlending::setLevelZeroLayout( int levelZeroColumns, int levelZeroRows )
133 {
134  m_levelZeroColumns = levelZeroColumns;
135  m_levelZeroRows = levelZeroRows;
136 }
137 
138 // TODO: This should likely go into a math class in the future ...
139 int SunLightBlending::maxDivisor( int maximum, int fullLength )
140 {
141  // Find the optimal interpolation interval n for the
142  // current image canvas width
143  int best = 2;
144 
145  int nEvalMin = fullLength;
146  for ( int it = 1; it <= maximum; ++it ) {
147  // The optimum is the interval which results in the least amount
148  // supporting points taking into account the rest which can't
149  // get used for interpolation.
150  int nEval = fullLength / it + fullLength % it;
151  if ( nEval < nEvalMin ) {
152  nEvalMin = nEval;
153  best = it;
154  }
155  }
156  return best;
157 }
158 
159 }
QImage::scanLine
uchar * scanLine(int i)
interpolate
void interpolate(MarbleWidget *widget, qreal value)
Definition: examples/cpp/animation-video/main.cpp:68
Marble::Blending
Definition: Blending.h:25
TextureTile.h
Marble::TextureTile
A class that resembles an image tile (extends Tile).
Definition: TextureTile.h:60
QImage::depth
int depth() const
Marble::TextureTile::image
QImage const * image() const
Returns the QImage that describes the look of the Tile.
Definition: TextureTile.h:90
Marble::Tile::id
TileId const & id() const
Returns a unique ID for the tile.
Definition: Tile.h:74
SunLocator.h
MarbleDebug.h
Marble::SunLocator
Definition: SunLocator.h:33
Marble::SunLocator::getLat
qreal getLat() const
Definition: SunLocator.cpp:218
QImage::width
int width() const
SunLightBlending.h
Marble::SunLightBlending::SunLightBlending
SunLightBlending(const SunLocator *sunLocator)
Definition: SunLightBlending.cpp:31
Marble::SunLocator::shading
qreal shading(qreal lon, qreal a, qreal c) const
Definition: SunLocator.cpp:96
MarbleGlobal.h
Marble::DEG2RAD
const qreal DEG2RAD
Definition: MarbleGlobal.h:219
Marble::SunLightBlending::~SunLightBlending
virtual ~SunLightBlending()
Definition: SunLightBlending.cpp:39
QImage
Marble::SunLocator::shadePixelComposite
void shadePixelComposite(QRgb &pixcol, const QRgb &dpixcol, qreal shade) const
Definition: SunLocator.cpp:154
Marble::TileId
Definition: TileId.h:27
Marble::SunLightBlending::blend
virtual void blend(QImage *const bottom, TextureTile const *const top) const
Definition: SunLightBlending.cpp:43
Marble::TileLoaderHelper::levelToRow
MARBLE_EXPORT int levelToRow(int levelZeroRows, int level)
Get the maximum number of tile rows for a given tile level.
Definition: TileLoaderHelper.cpp:36
Marble::SunLightBlending::setLevelZeroLayout
void setLevelZeroLayout(int levelZeroColumns, int levelZeroRows)
Definition: SunLightBlending.cpp:132
Marble::TileLoaderHelper::levelToColumn
MARBLE_EXPORT int levelToColumn(int levelZeroColumns, int level)
Get the maximum number of tile columns for a given tile level.
Definition: TileLoaderHelper.cpp:46
M_PI
#define M_PI
Definition: GeoDataCoordinates.h:26
QImage::height
int height() const
TileLoaderHelper.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal