|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgssinglebandcolordatarenderer.cpp 00003 ---------------------------------- 00004 begin : January 2012 00005 copyright : (C) 2012 by Marco Hugentobler 00006 email : marco at sourcepole dot ch 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "qgssinglebandcolordatarenderer.h" 00019 #include "qgsrasterviewport.h" 00020 #include <QDomDocument> 00021 #include <QDomElement> 00022 #include <QImage> 00023 00024 QgsSingleBandColorDataRenderer::QgsSingleBandColorDataRenderer( QgsRasterInterface* input, int band ): 00025 QgsRasterRenderer( input, "singlebandcolordata" ), mBand( band ) 00026 { 00027 00028 } 00029 00030 QgsSingleBandColorDataRenderer::~QgsSingleBandColorDataRenderer() 00031 { 00032 } 00033 00034 QgsRasterInterface * QgsSingleBandColorDataRenderer::clone() const 00035 { 00036 QgsSingleBandColorDataRenderer * renderer = new QgsSingleBandColorDataRenderer( 0, mBand ); 00037 renderer->setOpacity( mOpacity ); 00038 renderer->setAlphaBand( mAlphaBand ); 00039 renderer->setRasterTransparency( mRasterTransparency ); 00040 return renderer; 00041 } 00042 00043 QgsRasterRenderer* QgsSingleBandColorDataRenderer::create( const QDomElement& elem, QgsRasterInterface* input ) 00044 { 00045 if ( elem.isNull() ) 00046 { 00047 return 0; 00048 } 00049 00050 int band = elem.attribute( "band", "-1" ).toInt(); 00051 QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band ); 00052 r->readXML( elem ); 00053 return r; 00054 } 00055 00056 QgsRasterBlock* QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height ) 00057 { 00058 Q_UNUSED( bandNo ); 00059 00060 QgsRasterBlock *outputBlock = new QgsRasterBlock(); 00061 if ( !mInput ) 00062 { 00063 return outputBlock; 00064 } 00065 00066 QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height ); 00067 if ( !inputBlock || inputBlock->isEmpty() ) 00068 { 00069 QgsDebugMsg( "No raster data!" ); 00070 delete inputBlock; 00071 return outputBlock; 00072 } 00073 00074 bool hasTransparency = usesTransparency(); 00075 if ( !hasTransparency ) 00076 { 00077 // Nothing to do, just retype if necessary 00078 inputBlock->convert( QGis::ARGB32_Premultiplied ); 00079 delete outputBlock; 00080 return inputBlock; 00081 } 00082 00083 if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) ) 00084 { 00085 delete inputBlock; 00086 return outputBlock; 00087 } 00088 00089 for ( size_t i = 0; i < ( size_t )width*height; i++ ) 00090 { 00091 QRgb pixelColor; 00092 double alpha = 255.0; 00093 QRgb c = inputBlock->color( i ); 00094 alpha = qAlpha( c ); 00095 pixelColor = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * alpha ); 00096 outputBlock->setColor( i, pixelColor ); 00097 } 00098 00099 delete inputBlock; 00100 return outputBlock; 00101 } 00102 00103 void QgsSingleBandColorDataRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const 00104 { 00105 if ( parentElem.isNull() ) 00106 { 00107 return; 00108 } 00109 00110 QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" ); 00111 _writeXML( doc, rasterRendererElem ); 00112 rasterRendererElem.setAttribute( "band", mBand ); 00113 parentElem.appendChild( rasterRendererElem ); 00114 } 00115 00116 QList<int> QgsSingleBandColorDataRenderer::usesBands() const 00117 { 00118 QList<int> bandList; 00119 if ( mBand != -1 ) 00120 { 00121 bandList << mBand; 00122 } 00123 return bandList; 00124 } 00125 00126 bool QgsSingleBandColorDataRenderer::setInput( QgsRasterInterface* input ) 00127 { 00128 // Renderer can only work with numerical values in at least 1 band 00129 if ( !input ) return false; 00130 00131 if ( !mOn ) 00132 { 00133 // In off mode we can connect to anything 00134 mInput = input; 00135 return true; 00136 } 00137 00138 if ( input->dataType( 1 ) == QGis::ARGB32 || 00139 input->dataType( 1 ) == QGis::ARGB32_Premultiplied ) 00140 { 00141 mInput = input; 00142 return true; 00143 } 00144 return false; 00145 }