Salome HOME
loadImage() for py scripts
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_Image.h"
20
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Lambert93.h"
23 #include "HYDROData_OperationsFactory.h"
24 #include "HYDROData_Tool.h"
25
26 #include <TDataStd_RealArray.hxx>
27 #include <TDataStd_ByteArray.hxx>
28 #include <TDataStd_Integer.hxx>
29 #include <TDataStd_IntegerArray.hxx>
30 #include <TDataStd_ReferenceList.hxx>
31 #include <TDataStd_UAttribute.hxx>
32 #include <TDataStd_AsciiString.hxx>
33
34 #include <NCSDefs.h>
35 #include <NCSFile.h>
36
37 #ifdef WIN32
38   #pragma warning ( disable: 4251 )
39 #endif
40
41 #include <ImageComposer_Operator.h>
42 #include <ImageComposer_MetaTypes.h>
43
44 #include <QStringList>
45 #include <QFile>
46 #include <QFileInfo>
47
48 #ifdef WIN32
49   #pragma warning ( default: 4251 )
50 #endif
51
52 static const Standard_GUID GUID_SELF_SPLIT("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
53 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
54 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
55
56 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
57
58 HYDROData_Image::HYDROData_Image()
59 : HYDROData_Entity( Geom_2d )
60 {
61 }
62
63 HYDROData_Image::~HYDROData_Image()
64 {
65 }
66
67 QStringList HYDROData_Image::DumpToPython( const QString&       thePyScriptPath,
68                                            MapOfTreatedObjects& theTreatedObjects ) const
69 {
70   QStringList aResList = dumpObjectCreation( theTreatedObjects );
71   QString anImageName = GetObjPyName();
72
73   QString aFilePath = GetFilePath();
74   if ( !aFilePath.isEmpty() )
75   {
76     aResList << QString( "" );
77     aResList << QString( "if not(%1.LoadImage( \"%2\" )):" )
78                 .arg( anImageName ).arg( aFilePath );
79     aResList << QString( "  raise ValueError('problem while loading image')" );
80     aResList << QString( "" );
81
82     // Dump transformation points for image
83
84     bool anIsByTwoPoints = IsByTwoPoints();
85
86     QPoint aLocalPointA, aLocalPointB, aLocalPointC;
87     if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
88     {
89       QString aGap = QString().fill( ' ', anImageName.size() + 17 );
90
91       aResList << QString( "%1.SetLocalPoints( QPoint( %2, %3 )," )
92                   .arg( anImageName ).arg( aLocalPointA.x() ).arg( aLocalPointA.y() );
93       aResList << QString( aGap             + "QPoint( %1, %2 )" )
94                   .arg( aLocalPointB.x() ).arg( aLocalPointB.y() );
95       if ( !anIsByTwoPoints )
96       {
97         aResList.last().append( "," );
98         aResList << QString( aGap             +  "QPoint( %1, %2 ) )" )
99                     .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
100       }
101       else
102       {
103         aResList.last().append( " )" );
104       }
105       aResList << QString( "" );
106     }
107
108     HYDROData_Image::TransformationMode aTransformationMode;
109     QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
110     if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
111     {
112       QString aGap = QString().fill( ' ', anImageName.size() + 18 );
113
114       aResList << QString( "%1.SetGlobalPoints( %2," )
115                   .arg( anImageName ).arg( aTransformationMode );
116       aResList << QString( aGap             +  "QPointF( %1, %2 )," )
117                   .arg( aTrsfPointA.x(), 0, 'f', 3  ).arg( aTrsfPointA.y(), 0, 'f', 3  );
118       aResList << QString( aGap             +  "QPointF( %1, %2 )" )
119                   .arg( aTrsfPointB.x(), 0, 'f', 3  ).arg( aTrsfPointB.y(), 0, 'f', 3  );
120       if ( !anIsByTwoPoints )
121       {
122         aResList.last().append( "," );
123         aResList << QString( aGap             +  "QPointF( %1, %2 ) )" )
124                     .arg( aTrsfPointC.x(), 0, 'f', 3  ).arg( aTrsfPointC.y(), 0, 'f', 3  );
125       }
126       else
127       {
128         aResList.last().append( " )" );
129       }
130
131       if ( aTransformationMode == ReferenceImage )
132       {
133         Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
134         setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
135       }
136     }
137   }
138   else
139   {
140     // Image is composed from other image(s)
141
142     QString anOperatorName = OperatorName();
143     if ( !anOperatorName.isEmpty() )
144     {
145       aResList << QString( "" );
146
147       aResList << QString( "%1.SetOperatorName( \"%2\" )" )
148                   .arg( anImageName ).arg( anOperatorName );
149
150       ImageComposer_Operator* anImageOp = 
151         HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
152       if ( anImageOp )
153       {
154         // Dump operation arguments
155         QString anOpArgsArrayName;
156         QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
157         if ( !anOpArgs.isEmpty() )
158         {
159           aResList << QString( "" );
160           aResList << anOpArgs;
161
162           aResList << QString( "" );
163           aResList << QString( "%1.SetArgs( %2 )" )
164                       .arg( anImageName ).arg( anOpArgsArrayName );
165         }
166       }
167     }
168     
169     int aNbReferences = NbReferences();
170     if ( aNbReferences > 0 )
171     {
172       aResList << QString( "" );
173
174       for ( int i = 0; i < aNbReferences; ++i )
175       {
176         Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
177         setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "AppendReference" );
178       }
179     }
180   }
181
182   aResList << QString( "" );
183   aResList << QString( "%1.Update()" ).arg( anImageName );
184
185   return aResList;
186 }
187
188 void HYDROData_Image::Update()
189 {
190   bool anIsToUpdate = IsMustBeUpdated( Geom_2d );
191
192   HYDROData_Entity::Update();
193
194   if ( !anIsToUpdate )
195     return;
196
197   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
198
199   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
200   if ( anOp ) // Update image if there is an operation
201   {
202     // Fill by arguments and process the operation
203     anOp->setBinArgs( Args() );
204
205     QVariant anObj1, anObj2;
206     int aNbReferences = NbReferences();
207
208     if ( aNbReferences > 0 )
209     {
210       // First referenced object
211       Handle(HYDROData_Entity) aRefObj = Reference( 0 );
212       if ( !aRefObj.IsNull() )
213       {
214         anObj1 = aRefObj->GetDataVariant();
215         if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
216         {
217           ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
218           QTransform aTransform = anImage.transform();
219           SetTrsf( aTransform );
220         }
221       }
222     }
223
224     if ( aNbReferences > 1 )
225     {
226       // Second referenced object
227       Handle(HYDROData_Entity) aRefObj = Reference( 1 );
228       if ( !aRefObj.IsNull() )
229         anObj2 = aRefObj->GetDataVariant();
230     }
231
232     ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
233     SetImage( aResImg );
234     SetTrsf( aResImg.transform() );
235   }
236   else // Update image if it positioned relatively to other image
237   {
238     UpdateTrsf();
239   }
240
241   ClearChanged();
242 }
243
244 bool HYDROData_Image::IsHas2dPrs() const
245 {
246   return true;
247 }
248
249 QVariant HYDROData_Image::GetDataVariant()
250 {
251   QTransform aTransform = Trsf();
252
253   ImageComposer_Image anImage = Image();
254   anImage.setTransform( aTransform );
255
256   QVariant aVarData;
257   aVarData.setValue<ImageComposer_Image>( anImage );
258   
259   return aVarData;
260 }
261
262 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
263 {
264   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
265
266   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
267   if ( !aRefImage.IsNull() )
268     aResSeq.Append( aRefImage );
269
270   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
271   aResSeq.Append( aSeqRefObjects );
272
273   return aResSeq;
274 }
275
276 void HYDROData_Image::SetImage(const QImage& theImage)
277 {
278   if ( theImage.isNull() )
279   {
280     // for empty image remove all previously stored attributes
281     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
282     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
283   }
284   else
285   {
286     QImage anImage;
287
288     // convert 8-bits images
289     if ( theImage.format() == QImage::Format_Indexed8 ) {
290       anImage = theImage.convertToFormat( QImage::Format_RGB32 );
291     } else {
292       anImage = theImage;
293     }
294
295     // store width, height, bytes per line and format in integer array 
296     Handle(TDataStd_IntegerArray) aParams;
297     if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
298       aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
299     }
300     aParams->SetValue(1, anImage.width());
301     aParams->SetValue(2, anImage.height());
302     aParams->SetValue(3, anImage.bytesPerLine());
303     aParams->SetValue(4, (int)(anImage.format()));
304     // store data of image in byte array
305     const char* aData = (const char*)(anImage.bits());
306     SaveByteArray(0, aData, anImage.byteCount());
307   }
308
309   Changed( Geom_2d );
310 }
311
312 bool HYDROData_Image::LoadImage( const QString& theFilePath )
313 {
314   QFileInfo aFI(theFilePath);
315   QImage anImage;
316   HYDROData_Image::ECW_FileInfo* theECWInfo;
317   if (aFI.suffix().toLower() == "ecw")
318   {
319     theECWInfo = new HYDROData_Image::ECW_FileInfo;
320     HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, theECWInfo);
321   }
322   else
323     anImage = QImage(theFilePath);
324   SetImage( anImage );
325   SetFilePath( theFilePath );
326   return !anImage.isNull();
327 }
328
329 bool HYDROData_Image::OpenECW(char* theFileName, QImage& theImage, ECW_FileInfo* theECWInfo)
330 {
331   NCSFileView *pNCSFileView;
332   NCSFileViewFileInfo *pNCSFileInfo;
333   NCSError eError = NCS_SUCCESS;
334   UINT32 band, nBands;
335   UINT32 XSize, YSize;
336   NCSecwInit();
337
338   eError = NCScbmOpenFileView(theFileName, &pNCSFileView, NULL);
339   if(eError != NCS_SUCCESS)
340     return false;
341
342   eError = NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
343   if(eError != NCS_SUCCESS)
344     return false;
345
346   XSize = pNCSFileInfo->nSizeX;
347   YSize = pNCSFileInfo->nSizeY;
348   nBands = pNCSFileInfo->nBands;
349   if (theECWInfo)
350   {
351     //ECW_CellUnits myCellSizeUnits;
352     CellSizeUnits aCellUnits = pNCSFileInfo->eCellSizeUnits;
353     if (aCellUnits == ECW_CELL_UNITS_METERS)
354       theECWInfo->myCellSizeUnits = ECW_CellUnits_Meters;
355     else if (aCellUnits == ECW_CELL_UNITS_DEGREES)
356       theECWInfo->myCellSizeUnits = ECW_CellUnits_Deg;
357     else if (aCellUnits == ECW_CELL_UNITS_FEET)
358       theECWInfo->myCellSizeUnits = ECW_CellUnits_Feet;
359     else
360       theECWInfo->myCellSizeUnits = ECW_CellUnits_Unknown;
361     theECWInfo->myCellIncrementX = pNCSFileInfo->fCellIncrementX;
362     theECWInfo->myCellIncrementY = pNCSFileInfo->fCellIncrementY;
363     theECWInfo->myOriginX = pNCSFileInfo->fOriginX;
364     theECWInfo->myOriginY = pNCSFileInfo->fOriginY;
365     theECWInfo->myXSize = pNCSFileInfo->nSizeX;
366     theECWInfo->myYSize = pNCSFileInfo->nSizeY;
367   }
368
369   std::vector<UINT32> band_list(nBands);
370   for( band = 0; band < nBands; band++ )
371     band_list[band] = band;
372
373   eError = NCScbmSetFileView(pNCSFileView, nBands, &band_list[0], 0, 0, XSize - 1, YSize - 1, XSize, YSize); //view an image into the original size
374
375   if(eError != NCS_SUCCESS)
376   {
377     NCScbmCloseFileView(pNCSFileView);
378     return false;
379   }
380
381   UINT8 *pRGBTriplets;
382   pRGBTriplets = (UINT8 *) malloc(XSize*3);
383
384   QImage anImage(XSize, YSize, QImage::Format_RGB32);
385
386   for(UINT32 line = 0; line < YSize; line++)
387   {
388     NCSEcwReadStatus eStatus;
389     eStatus = NCScbmReadViewLineRGB(pNCSFileView, pRGBTriplets);
390     if(eStatus == NCSECW_READ_OK)
391     {
392       QRgb* crp = (QRgb*)anImage.scanLine(line);  
393       for(UINT32 j = 0; j < XSize; j++)
394       {
395         QRgb val = qRgb((int)pRGBTriplets[j*3],(int)pRGBTriplets[j*3+1],(int)pRGBTriplets[j*3+2]);
396         memcpy((void*)(crp+j), &val, sizeof(QRgb)); 
397       }
398     }
399     else    
400     {
401       free(pRGBTriplets);
402       NCScbmCloseFileView(pNCSFileView);
403       return false;
404     }
405   }
406
407   free(pRGBTriplets);
408   NCScbmCloseFileView(pNCSFileView);
409   theImage = anImage;
410 }
411
412 bool HYDROData_Image::LoadImageECW( const QString& theFilePath )
413 {
414   QImage anImage;
415   if (HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, NULL))
416   {
417     SetImage( anImage );
418     SetFilePath( theFilePath );
419   }
420   return !anImage.isNull();
421 }
422
423 QImage HYDROData_Image::Image()
424 {
425   Handle(TDataStd_IntegerArray) aParams;
426   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
427     return QImage(); // return empty image if there is no array
428   int aLen = 0;
429   uchar* anArray = (uchar*)ByteArray(0, aLen);
430   if (!aLen)
431     return QImage(); // return empty image if there is no array
432   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
433                  aParams->Value(3), QImage::Format(aParams->Value(4)));
434   return aResult;
435 }
436
437 void HYDROData_Image::SetFilePath( const QString& theFilePath )
438 {
439   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
440   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
441
442   Changed( Geom_2d );
443 }
444
445 QString HYDROData_Image::GetFilePath() const
446 {
447   QString aRes;
448
449   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
450   if ( !aLabel.IsNull() )
451   {
452     Handle(TDataStd_AsciiString) anAsciiStr;
453     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
454       aRes = QString( anAsciiStr->Get().ToCString() );
455   }
456
457   return aRes;
458 }
459
460 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
461 {
462   // locate 9 coeffs of matrix into the real array
463   Handle(TDataStd_RealArray) anArray;
464   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
465     if (theTrsf.isIdentity()) return; // no need to store identity transformation
466     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
467   }
468   anArray->SetValue(1, theTrsf.m11());
469   anArray->SetValue(2, theTrsf.m12());
470   anArray->SetValue(3, theTrsf.m13());
471   anArray->SetValue(4, theTrsf.m21());
472   anArray->SetValue(5, theTrsf.m22());
473   anArray->SetValue(6, theTrsf.m23());
474   anArray->SetValue(7, theTrsf.m31());
475   anArray->SetValue(8, theTrsf.m32());
476   anArray->SetValue(9, theTrsf.m33());
477
478   Changed( Geom_2d );
479 }
480
481 QTransform HYDROData_Image::Trsf() const
482 {
483   // get 9 coeffs of matrix from the real array
484   Handle(TDataStd_RealArray) anArray;
485   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
486     return QTransform(); // return identity if there is no array
487   QTransform aTrsf(
488     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
489     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
490     anArray->Value(7), anArray->Value(8), anArray->Value(9));
491   return aTrsf;
492 }
493
494 void HYDROData_Image::UpdateTrsf()
495 {
496   QPoint aPointA, aPointB, aPointC;
497   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
498     return;
499
500   TransformationMode aTrsfMode;
501   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
502   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
503     return;
504
505   QTransform aRefTransform;
506   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
507
508   bool anIsRefImage = aTrsfMode == ReferenceImage;
509   if ( anIsRefImage )
510   {
511     if ( aRefImage.IsNull() )
512       return;
513
514     aRefTransform = aRefImage->Trsf();
515   }
516
517   bool anIsByTwoPoints = IsByTwoPoints();
518
519   // Convert lambert coordinates to cartesian
520   if ( aTrsfMode == ManualGeodesic )
521   {
522     double aXCart = 0, aYCart = 0;
523
524     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
525     aTrsfPointA = QPointF( aXCart, aYCart );
526
527     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
528     aTrsfPointB = QPointF( aXCart, aYCart );
529
530     if ( !anIsByTwoPoints )
531     {
532       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
533       aTrsfPointC = QPointF( aXCart, aYCart );
534     }
535   }
536
537   // generate third points if needed
538   if ( anIsByTwoPoints )
539   {
540     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
541     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
542   }
543
544   int xa = aPointA.x();
545   int ya = aPointA.y();
546   int xb = aPointB.x();
547   int yb = aPointB.y();
548   int xc = aPointC.x();
549   int yc = aPointC.y();
550
551   double xta = aTrsfPointA.x();
552   double yta = aTrsfPointA.y();
553   double xtb = aTrsfPointB.x();
554   double ytb = aTrsfPointB.y();
555   double xtc = aTrsfPointC.x();
556   double ytc = aTrsfPointC.y();
557
558   // first, check that three input points don't belong to a single line
559   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
560     return;
561
562   // the same check for the reference points
563   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
564     return;
565
566   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
567   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
568
569   bool anIsInvertible = false;
570   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
571   if( !anIsInvertible )
572     return;
573
574   QTransform aResTransform = aTransform1Inverted * aTransform2;
575   if( anIsRefImage )
576     aResTransform *= aRefTransform;
577
578   SetTrsf( aResTransform );
579 }
580
581 bool HYDROData_Image::IsByTwoPoints() const
582 {
583   if ( !HasLocalPoints() || !HasGlobalPoints() )
584     return false;
585
586   QPoint aPointA, aPointB, aPointC;
587   GetLocalPoints( aPointA, aPointB, aPointC );
588
589   return aPointC.x() < 0 && aPointC.y() < 0; 
590 }
591
592 bool HYDROData_Image::HasReferences() const
593 {
594   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
595   int aNbReferences = NbReferences();
596
597   return !aRefImage.IsNull() || aNbReferences > 0;
598 }
599
600 void HYDROData_Image::RemoveAllReferences()
601 {
602   if ( !HasReferences() )
603     return;
604
605   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
606   if ( !aRefImage.IsNull() )
607   {
608     RemoveTrsfReferenceImage();
609   }
610   else
611   {
612     ClearReferences();
613     SetOperatorName( "" );
614     SetArgs( "" );
615     SetIsSelfSplit( false );
616   }
617
618   bool anIsByTwoPoints = IsByTwoPoints();
619
620   QImage anImage = Image();
621   if ( anImage.isNull() )
622   {
623     ClearChanged();
624     return;
625   }
626
627   // Set local points to default position
628   QPoint aLocalPointA = QPoint( 0, 0 );
629   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
630   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
631
632   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
633
634   // Calculate global points
635   QTransform aTransform = Trsf();
636
637   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
638   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
639   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
640                                           QPointF( aTransform.map( aLocalPointC ) );
641
642   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
643
644   ClearChanged();
645 }
646
647 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
648                                       const QPoint& thePointB,
649                                       const QPoint& thePointC,
650                                       const bool    theIsUpdate )
651 {
652   Handle(TDataStd_RealArray) anArray;
653   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
654     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
655
656   anArray->SetValue( 1, thePointA.x() );
657   anArray->SetValue( 2, thePointA.y() );
658   anArray->SetValue( 3, thePointB.x() );
659   anArray->SetValue( 4, thePointB.y() );
660   anArray->SetValue( 5, thePointC.x() );
661   anArray->SetValue( 6, thePointC.y() );
662
663   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
664
665   if ( theIsUpdate )
666     UpdateTrsf();
667
668   Changed( Geom_2d );
669 }
670
671 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
672                                       QPoint& thePointB,
673                                       QPoint& thePointC ) const
674 {
675   if ( !HasLocalPoints() )
676     return false;
677
678   Handle(TDataStd_RealArray) anArray;
679   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
680
681   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
682   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
683   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
684
685   return true;
686 }
687
688 bool HYDROData_Image::HasLocalPoints() const
689 {
690   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
691   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
692     return false;
693
694   Handle(TDataStd_RealArray) anArray;
695   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
696 }
697
698
699 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
700                                        const QPointF&            thePointA,
701                                        const QPointF&            thePointB,
702                                        const QPointF&            thePointC,
703                                        const bool                theIsUpdate )
704 {
705   Handle(TDataStd_RealArray) anArray;
706   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
707     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
708
709   anArray->SetValue( 7,  thePointA.x() );
710   anArray->SetValue( 8,  thePointA.y() );
711   anArray->SetValue( 9,  thePointB.x() );
712   anArray->SetValue( 10, thePointB.y() );
713   anArray->SetValue( 11, thePointC.x() );
714   anArray->SetValue( 12, thePointC.y() );
715   
716   SetTrsfMode( theMode );
717
718   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
719
720   if ( theIsUpdate )
721     UpdateTrsf();
722
723   Changed( Geom_2d );
724 }
725
726 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
727                                        QPointF&            thePointA,
728                                        QPointF&            thePointB,
729                                        QPointF&            thePointC ) const
730 {
731   if ( !HasGlobalPoints() )
732     return false;
733
734   theMode = GetTrsfMode();
735
736   Handle(TDataStd_RealArray) anArray;
737   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
738
739   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
740   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
741   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
742
743   return true;
744 }
745
746 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
747 {
748   bool aRes = false;
749
750   // Try to open the file
751   QFile aFile( theFileName );
752   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
753     return aRes;
754   }
755
756   QPointF aPointA, aPointB;
757   double aXmin, anYmin, aXmax, anYmax;
758   aXmin = anYmin = aXmax = anYmax = -1;
759
760   while ( !aFile.atEnd() && 
761           ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
762     // Read line
763     QString aLine = aFile.readLine().simplified();
764     aLine.replace( " ", "" );
765     if ( aLine.isEmpty() ) {
766       continue;
767     }
768
769     // Try to read double value after ":"
770     bool isDoubleOk = false;
771     double aDoubleValue = -1;
772     QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
773     if ( aValues.count() == 2 ) {
774       aDoubleValue = aValues.last().toDouble( &isDoubleOk );
775     }
776
777     // Check the result
778     if ( !isDoubleOk ||
779          HYDROData_Tool::IsNan( aDoubleValue ) ||
780          HYDROData_Tool::IsInf( aDoubleValue ) ) {
781       continue;
782     }
783
784     // Set the value
785     if ( aLine.startsWith( "Xminimum" ) ) {
786       aXmin = aDoubleValue;    
787     } 
788     else if ( aLine.startsWith( "Yminimum" ) ) {
789       anYmin = aDoubleValue; 
790     }
791     else if ( aLine.startsWith( "Xmaximum" ) ) {
792       aXmax = aDoubleValue;
793     }
794     else if ( aLine.startsWith( "Ymaximum" ) ) {
795       anYmax = aDoubleValue;  
796     }
797   }
798
799   // Close the file
800   aFile.close();
801
802   if ( aXmin >= 0 && anYmin >= 0 ) {
803     aPointA.setX( aXmin );
804     aPointA.setY( anYmin );
805   }
806     
807   if ( aXmax >= 0 && anYmax >= 0 ) {
808     aPointB.setX( aXmax );
809     aPointB.setY( anYmax );
810   }
811
812   if ( !aPointA.isNull() && !aPointB.isNull() ) {
813     SetGlobalPoints( ManualCartesian, aPointA, aPointB );
814     aRes = true;
815   }
816
817   return aRes;
818 }
819
820 bool HYDROData_Image::HasGlobalPoints() const
821 {
822   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
823   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
824     return false;
825
826   Handle(TDataStd_RealArray) anArray;
827   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
828 }
829
830 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
831                                           const QPointF&                 thePointA,
832                                           const QPointF&                 thePointB,
833                                           const QPointF&                 thePointC,
834                                           const bool                     theIsUpdate )
835 {
836   SetTrsfReferenceImage( theRefImage );
837   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
838 }
839
840 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
841                                           QPointF&                 thePointA,
842                                           QPointF&                 thePointB,
843                                           QPointF&                 thePointC ) const
844 {
845   if ( !HasReferencePoints() )
846     return false;
847
848   theRefImage = GetTrsfReferenceImage();
849
850   TransformationMode aMode;
851   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
852
853   return true;
854 }
855
856 bool HYDROData_Image::HasReferencePoints() const
857 {
858   if ( !HasGlobalPoints() )
859     return false;
860
861   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
862   if ( aRefImage.IsNull() )
863     return false;
864
865   TransformationMode aTrsfMode = GetTrsfMode();
866   if ( aTrsfMode != ReferenceImage )
867     return false;
868
869   return true;
870 }
871
872 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
873 {
874   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
875   Changed( Geom_2d );
876 }
877
878 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
879 {
880   TransformationMode aResMode = ManualGeodesic;
881
882   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
883   if ( !aLabel.IsNull() )
884   {
885     Handle(TDataStd_Integer) aMode;
886     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
887       aResMode = (TransformationMode)aMode->Get();
888   }
889
890   return aResMode;
891 }
892
893 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
894 {
895   SetReferenceObject( theRefImage, DataTag_TrsfImage );
896   Changed( Geom_2d );
897 }
898
899 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
900 {
901   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
902 }
903
904 void HYDROData_Image::RemoveTrsfReferenceImage()
905 {
906   RemoveReferenceObject( DataTag_TrsfImage );
907   Changed( Geom_2d );
908 }
909
910 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
911 {
912   AddReferenceObject( theReferenced, 0 );
913   Changed( Geom_2d );
914 }
915
916 int HYDROData_Image::NbReferences() const
917 {
918   return NbReferenceObjects( 0 );
919 }
920
921 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
922 {
923   return GetReferenceObject( 0, theIndex );
924 }
925
926 void HYDROData_Image::ChangeReference(
927     const int theIndex, Handle(HYDROData_Entity) theReferenced)
928 {
929   SetReferenceObject( theReferenced, 0, theIndex );
930   Changed( Geom_2d );
931 }
932
933 void HYDROData_Image::RemoveReference(const int theIndex)
934 {
935   RemoveReferenceObject( 0, theIndex );
936   Changed( Geom_2d );
937 }
938
939 void HYDROData_Image::ClearReferences()
940 {
941   ClearReferenceObjects( 0 );
942   Changed( Geom_2d );
943 }
944
945 void HYDROData_Image::SetOperatorName( const QString theOpName )
946 {
947   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
948   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
949   Changed( Geom_2d );
950 }
951
952 QString HYDROData_Image::OperatorName() const
953 {
954   QString aRes;
955
956   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
957   if ( !aLabel.IsNull() )
958   {
959     Handle(TDataStd_AsciiString) anAsciiStr;
960     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
961       aRes = QString( anAsciiStr->Get().ToCString() );
962   }
963
964   return aRes;
965 }
966
967 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
968 {
969   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
970   Changed( Geom_2d );
971 }
972
973 QByteArray HYDROData_Image::Args() const
974 {
975   int aLen = 0;
976   const char* aData = ByteArray(DataTag_Operator, aLen);
977   if (!aLen)
978     return QByteArray();
979   return QByteArray(aData, aLen);
980 }
981
982 void HYDROData_Image::SetIsSelfSplit(bool theFlag)
983 {
984   if (theFlag) {
985     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLIT);
986   } else {
987     myLab.ForgetAttribute(GUID_SELF_SPLIT);
988   }
989   Changed( Geom_2d );
990 }
991
992 bool HYDROData_Image::IsSelfSplit() const
993 {
994   return myLab.IsAttribute(GUID_SELF_SPLIT);
995 }
996
997 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
998                                              const QPointF& thePointB,
999                                              const bool&    theIsLocal ) const
1000 {
1001   // Rotate vector to 90 degrees : clockwise - for local
1002   //                               counterclockwise - for global
1003   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
1004
1005   QPointF aResPoint;
1006
1007   // Move to (0,0) for correct rotation
1008   double x = thePointB.x() - thePointA.x();
1009   double y = thePointB.y() - thePointA.y();
1010
1011   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
1012   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
1013
1014   // Move back to origin position
1015   aResPoint.setX( aResPoint.x() + thePointA.x() );
1016   aResPoint.setY( aResPoint.y() + thePointA.y() );
1017
1018   return aResPoint;
1019 }
1020