Quantum GIS API Documentation  1.7.4
src/core/raster/qgsrastertransparency.cpp
Go to the documentation of this file.
00001 /* **************************************************************************
00002                 qgsrastertransparency.cpp -  description
00003                        -------------------
00004 begin                : Mon Nov 30 2007
00005 copyright            : (C) 2007 by Peter J. Ersts
00006 email                : ersts@amnh.org
00007 
00008 ****************************************************************************/
00009 
00010 /* **************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 #include <QList>
00019 
00020 #include "qgsrastertransparency.h"
00021 
00022 QgsRasterTransparency::QgsRasterTransparency()
00023 {
00024 
00025 }
00026 
00030 QList<QgsRasterTransparency::TransparentSingleValuePixel> QgsRasterTransparency::transparentSingleValuePixelList() const
00031 {
00032   return mTransparentSingleValuePixelList;
00033 }
00034 
00038 QList<QgsRasterTransparency::TransparentThreeValuePixel> QgsRasterTransparency::transparentThreeValuePixelList() const
00039 {
00040   return mTransparentThreeValuePixelList;
00041 }
00042 
00046 void QgsRasterTransparency::initializeTransparentPixelList( double theValue )
00047 {
00048   //clear the existing list
00049   mTransparentSingleValuePixelList.clear();
00050 
00051   //add the initial value
00052   TransparentSingleValuePixel myTransparentSingleValuePixel;
00053   myTransparentSingleValuePixel.pixelValue = theValue;
00054   myTransparentSingleValuePixel.percentTransparent = 100.0;
00055   mTransparentSingleValuePixelList.append( myTransparentSingleValuePixel );
00056 }
00057 
00061 void QgsRasterTransparency::initializeTransparentPixelList( double theRedValue, double theGreenValue, double theBlueValue )
00062 {
00063   //clearn the existing list
00064   mTransparentThreeValuePixelList.clear();
00065 
00066   //add the initial values
00067   TransparentThreeValuePixel myTransparentThreeValuePixel;
00068   myTransparentThreeValuePixel.red = theRedValue;
00069   myTransparentThreeValuePixel.green = theGreenValue;
00070   myTransparentThreeValuePixel.blue = theBlueValue;
00071   myTransparentThreeValuePixel.percentTransparent = 100.0;
00072   mTransparentThreeValuePixelList.append( myTransparentThreeValuePixel );
00073 }
00074 
00075 
00079 void QgsRasterTransparency::setTransparentSingleValuePixelList( QList<QgsRasterTransparency::TransparentSingleValuePixel> theNewList )
00080 {
00081   mTransparentSingleValuePixelList = theNewList;
00082 }
00083 
00087 void QgsRasterTransparency::setTransparentThreeValuePixelList( QList<QgsRasterTransparency::TransparentThreeValuePixel> theNewList )
00088 {
00089   mTransparentThreeValuePixelList = theNewList;
00090 }
00091 
00098 int QgsRasterTransparency::alphaValue( double theValue, int theGlobalTransparency ) const
00099 {
00100   //if NaN return 0, transparent
00101   if ( theValue != theValue )
00102   {
00103     return 0;
00104   }
00105 
00106   //Search through he transparency list looking for a match
00107   bool myTransparentPixelFound = false;
00108   TransparentSingleValuePixel myTransparentPixel = {0, 100};
00109   for ( int myListRunner = 0; myListRunner < mTransparentSingleValuePixelList.count(); myListRunner++ )
00110   {
00111     myTransparentPixel = mTransparentSingleValuePixelList[myListRunner];
00112     if ( myTransparentPixel.pixelValue == theValue )
00113     {
00114       myTransparentPixelFound = true;
00115       break;
00116     }
00117   }
00118 
00119   //if a match was found use the stored transparency percentage
00120   if ( myTransparentPixelFound )
00121   {
00122     return ( int )(( float )theGlobalTransparency *( 1.0 - ( myTransparentPixel.percentTransparent / 100.0 ) ) );
00123   }
00124 
00125   return theGlobalTransparency;
00126 }
00127 
00136 int QgsRasterTransparency::alphaValue( double theRedValue, double theGreenValue, double theBlueValue, int theGlobalTransparency ) const
00137 {
00138   //if NaN return 0, transparent
00139   if ( theRedValue != theRedValue || theGreenValue != theGreenValue || theBlueValue != theBlueValue )
00140   {
00141     return 0;
00142   }
00143 
00144   //Search through he transparency list looking for a match
00145   bool myTransparentPixelFound = false;
00146   TransparentThreeValuePixel myTransparentPixel = {0, 0, 0, 100};
00147   for ( int myListRunner = 0; myListRunner < mTransparentThreeValuePixelList.count(); myListRunner++ )
00148   {
00149     myTransparentPixel = mTransparentThreeValuePixelList[myListRunner];
00150     if ( myTransparentPixel.red == theRedValue )
00151     {
00152       if ( myTransparentPixel.green == theGreenValue )
00153       {
00154         if ( myTransparentPixel.blue == theBlueValue )
00155         {
00156           myTransparentPixelFound = true;
00157           break;
00158         }
00159       }
00160     }
00161   }
00162 
00163   //if a match was found use the stored transparency percentage
00164   if ( myTransparentPixelFound )
00165   {
00166     return ( int )(( float )theGlobalTransparency *( 1.0 - ( myTransparentPixel.percentTransparent / 100.0 ) ) );
00167   }
00168 
00169   return theGlobalTransparency;
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines