Back to index

salome-gui  6.5.0
VTKViewer_MarkerUtils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "VTKViewer_MarkerUtils.h"
00021 
00022 // VTK Includes
00023 #include <vtkImageData.h>
00024 
00025 // QT Includes
00026 #include <QFile>
00027 #include <QImage>
00028 #include <QString>
00029 #include <QTextStream>
00030 
00031 namespace VTK
00032 {
00033   bool LoadTextureData( const QString& theFileName,
00034                         VTK::MarkerScale theMarkerScale,
00035                         VTK::MarkerTexture& theMarkerTexture )
00036   {
00037     theMarkerTexture.clear();
00038 
00039     QFile aFile( theFileName );
00040     if( !aFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
00041       return false;
00042 
00043     bool ok;
00044     int aWidth = 0, aHeight = 0;
00045 
00046     int aTextureIndex = theMarkerScale == VTK::MS_NONE ? 0 : (int)theMarkerScale-1;
00047     int aCurrentTexture = 0;
00048 
00049     QTextStream aTextStream( &aFile );
00050     while( !aTextStream.atEnd() )
00051     {
00052       QString aLine = aTextStream.readLine();
00053       if( aLine.isEmpty() )
00054       {
00055         aCurrentTexture++;
00056         continue;
00057       }
00058 
00059       if( aCurrentTexture != aTextureIndex )
00060         continue;
00061 
00062       int aLineSize = aLine.size();
00063       for( int i = 0; i < aLineSize; i++ )
00064       {
00065         ok = false;
00066         unsigned int aPixel = QString( aLine.at( i ) ).toUInt( &ok );
00067         if( ok )
00068           theMarkerTexture.push_back( aPixel );
00069       }
00070       if( aWidth == 0 )
00071         aWidth = aLineSize;
00072       aHeight++;
00073     }
00074 
00075     if( theMarkerTexture.size() != aWidth * aHeight )
00076       return false;
00077 
00078     theMarkerTexture.push_front( aWidth );
00079     theMarkerTexture.push_front( aHeight );
00080     return true;
00081   }
00082 
00083   vtkSmartPointer<vtkImageData> MakeVTKImage( const VTK::MarkerTexture& theMarkerTexture,
00084                                               bool theWhiteForeground )
00085   {
00086     VTK::MarkerTexture::const_iterator anIter = theMarkerTexture.begin();
00087 
00088     int aWidth = *anIter++;
00089     int aHeight = *anIter++;
00090 
00091     vtkSmartPointer<vtkImageData> anImageData = vtkImageData::New();
00092     anImageData->Delete();
00093 
00094     anImageData->SetExtent( 0, aWidth-1, 0, aHeight-1, 0, 0 );
00095     anImageData->SetScalarTypeToUnsignedChar();
00096     anImageData->SetNumberOfScalarComponents( 4 );
00097     anImageData->AllocateScalars();
00098 
00099     unsigned char* aDataPtr = (unsigned char*)anImageData->GetScalarPointer();
00100 
00101     int anId = 0;
00102     int aSize = aWidth * aHeight * 4;
00103     int aCoef = theWhiteForeground ? 1 : 0;
00104     while( anId < aSize )
00105     {
00106       int aValue = (*anIter++) * 255;
00107       aDataPtr[ anId++ ] = aValue * aCoef;
00108       aDataPtr[ anId++ ] = aValue * aCoef;
00109       aDataPtr[ anId++ ] = aValue * aCoef;
00110       aDataPtr[ anId++ ] = aValue;
00111     }
00112     anImageData->Update();
00113 
00114     return anImageData;
00115   }
00116 
00117   QImage ConvertToQImage( vtkImageData* theImageData )
00118   {
00119     if( theImageData->GetScalarType() != VTK_UNSIGNED_CHAR )
00120       return QImage();
00121 
00122     int extent[6];
00123     theImageData->GetExtent( extent );
00124     int width = extent[1] - extent[0] + 1;
00125     int height = extent[3] - extent[2] + 1;
00126   
00127     QImage anImage(width, height, QImage::Format_ARGB32);
00128     for( int i = 0; i < height; i++ )
00129     {
00130       QRgb* bits = reinterpret_cast<QRgb*>( anImage.scanLine(i) );
00131       unsigned char* row = static_cast<unsigned char*>(
00132         theImageData->GetScalarPointer( extent[0], extent[2] + height - i - 1, extent[4] ) );
00133       for( int j = 0; j < width; j++ )
00134       {
00135         unsigned char* data = &row[ j*4 ];
00136         bits[j] = qRgba( data[0], data[1], data[2], data[3] );
00137       }
00138     }
00139     return anImage;
00140   }
00141 
00142   int GetUniqueId( const VTK::MarkerMap& theMarkerMap )
00143   {
00144     int anId = 0;
00145     while( anId++ < 100 ) {
00146       bool anOk = true;
00147       VTK::MarkerMap::const_iterator anIter = theMarkerMap.begin();
00148       for( ; anIter != theMarkerMap.end(); anIter++ ) {
00149         if( anId == anIter->first ) {
00150           anOk = false;
00151           continue;
00152         }
00153       }
00154       if( anOk )
00155         return anId;
00156     }
00157     return theMarkerMap.size() + 1;
00158   }
00159 }