/
qgssinglebandcolordatarenderer.cpp
107 lines (91 loc) · 3.39 KB
/
qgssinglebandcolordatarenderer.cpp
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
/***************************************************************************
qgssinglebandcolordatarenderer.cpp
----------------------------------
begin : January 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgssinglebandcolordatarenderer.h"
#include "qgsrasterviewport.h"
#include <QDomDocument>
#include <QDomElement>
#include <QImage>
QgsSingleBandColorDataRenderer::QgsSingleBandColorDataRenderer( QgsRasterInterface* input, int band ):
QgsRasterRenderer( input, "singlebandcolordata" ), mBand( band )
{
}
QgsSingleBandColorDataRenderer::~QgsSingleBandColorDataRenderer()
{
}
QgsRasterRenderer* QgsSingleBandColorDataRenderer::create( const QDomElement& elem, QgsRasterInterface* input )
{
if ( elem.isNull() )
{
return 0;
}
int band = elem.attribute( "band", "-1" ).toInt();
QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band );
r->readXML( elem );
return r;
}
void * QgsSingleBandColorDataRenderer::readBlock( int bandNo, QgsRectangle const & extent, int width, int height )
{
if ( !mInput )
{
return 0;
}
int currentRasterPos;
bool hasTransparency = usesTransparency();
QgsRasterInterface::DataType rasterType = ( QgsRasterInterface::DataType )mInput->dataType( mBand );
void* rasterData = mInput->readBlock( bandNo, extent, width, height );
currentRasterPos = 0;
QImage img( width, height, QImage::Format_ARGB32 );
uchar* scanLine = 0;
for ( int i = 0; i < height; ++i )
{
scanLine = img.scanLine( i );
if ( !hasTransparency )
{
memcpy( scanLine, &((( uint* )rasterData )[currentRasterPos] ), width * 4 );
currentRasterPos += width;
}
else
{
for ( int j = 0; j < width; ++j )
{
QRgb pixelColor;
double alpha = 255.0;
for ( int j = 0; j < nRasterCols; ++j )
{
QRgb c((( uint* )( rasterData ) )[currentRasterPos] );
alpha = qAlpha( c );
pixelColor = qRgba( qRed( c ), qGreen( c ), qBlue( c ), mOpacity * alpha );
memcpy( &( scanLine[j*4] ), &pixelColor, 4 );
++currentRasterPos;
}
}
}
}
VSIFree( rasterData );
void * data = VSIMalloc( img.byteCount() );
return memcpy( data, img.bits(), img.byteCount() );
}
void QgsSingleBandColorDataRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
if ( parentElem.isNull() )
{
return;
}
QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
_writeXML( doc, rasterRendererElem );
rasterRendererElem.setAttribute( "band", mBand );
parentElem.appendChild( rasterRendererElem );
}