1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VTKViewer_MarkerUtils.h"
23 #include <vtkImageData.h>
29 #include <QTextStream>
33 bool LoadTextureData( const QString& theFileName,
34 VTK::MarkerScale theMarkerScale,
35 VTK::MarkerTexture& theMarkerTexture )
37 theMarkerTexture.clear();
39 QFile aFile( theFileName );
40 if( !aFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
44 int aWidth = 0, aHeight = 0;
46 int aTextureIndex = theMarkerScale == VTK::MS_NONE ? 0 : (int)theMarkerScale-1;
47 int aCurrentTexture = 0;
49 QTextStream aTextStream( &aFile );
50 while( !aTextStream.atEnd() )
52 QString aLine = aTextStream.readLine();
59 if( aCurrentTexture != aTextureIndex )
62 int aLineSize = aLine.size();
63 for( int i = 0; i < aLineSize; i++ )
66 unsigned int aPixel = QString( aLine.at( i ) ).toUInt( &ok );
68 theMarkerTexture.push_back( aPixel );
75 if( theMarkerTexture.size() != aWidth * aHeight )
78 theMarkerTexture.push_front( aWidth );
79 theMarkerTexture.push_front( aHeight );
83 vtkSmartPointer<vtkImageData> MakeVTKImage( const VTK::MarkerTexture& theMarkerTexture,
84 bool theWhiteForeground )
86 VTK::MarkerTexture::const_iterator anIter = theMarkerTexture.begin();
88 int aWidth = *anIter++;
89 int aHeight = *anIter++;
91 vtkSmartPointer<vtkImageData> anImageData = vtkImageData::New();
92 anImageData->Delete();
94 anImageData->SetExtent( 0, aWidth-1, 0, aHeight-1, 0, 0 );
95 anImageData->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
97 unsigned char* aDataPtr = (unsigned char*)anImageData->GetScalarPointer();
100 int aSize = aWidth * aHeight * 4;
101 int aCoef = theWhiteForeground ? 1 : 0;
102 while( anId < aSize )
104 int aValue = (*anIter++) * 255;
105 aDataPtr[ anId++ ] = aValue * aCoef;
106 aDataPtr[ anId++ ] = aValue * aCoef;
107 aDataPtr[ anId++ ] = aValue * aCoef;
108 aDataPtr[ anId++ ] = aValue;
114 QImage ConvertToQImage( vtkImageData* theImageData )
116 if( theImageData->GetScalarType() != VTK_UNSIGNED_CHAR )
120 theImageData->GetExtent( extent );
121 int width = extent[1] - extent[0] + 1;
122 int height = extent[3] - extent[2] + 1;
127 int xshift = width < wmin ? (wmin-width)/2 : 0;
128 int yshift = height < hmin ? (hmin-height)/2 : 0;
130 QImage anImage(width < wmin ? wmin : width, height < hmin ? hmin : height, QImage::Format_ARGB32);
131 anImage.fill(qRgba(255,255,255,0));
132 for( int i = 0; i < height; i++ )
134 QRgb* bits = reinterpret_cast<QRgb*>( anImage.scanLine(i+yshift) );
135 unsigned char* row = static_cast<unsigned char*>(
136 theImageData->GetScalarPointer( extent[0], extent[2] + height - i - 1, extent[4] ) );
137 for( int j = 0; j < width; j++ )
139 unsigned char* data = &row[ j*4 ];
140 bits[j+xshift] = qRgba( data[0], data[1], data[2], data[3] );
146 int GetUniqueId( const VTK::MarkerMap& theMarkerMap )
149 while( anId++ < 100 ) {
151 VTK::MarkerMap::const_iterator anIter = theMarkerMap.begin();
152 for( ; anIter != theMarkerMap.end(); anIter++ ) {
153 if( anId == anIter->first ) {
161 return theMarkerMap.size() + 1;