From: adv Date: Tue, 10 Sep 2013 06:25:37 +0000 (+0000) Subject: Dump Image data to python script (Feature #13). X-Git-Tag: BR_hydro_v_0_1~49 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=9723b0802834ab7ee2b9ac3e18e76e46a27c8dca;p=modules%2Fhydro.git Dump Image data to python script (Feature #13). --- diff --git a/src/HYDROData/HYDROData_Image.cxx b/src/HYDROData/HYDROData_Image.cxx index 930d2a0e..41b194c7 100644 --- a/src/HYDROData/HYDROData_Image.cxx +++ b/src/HYDROData/HYDROData_Image.cxx @@ -1,6 +1,8 @@ #include #include +#include + #include #include #include @@ -10,8 +12,14 @@ #include #include +#include + +#include + static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000"); +#define PYTHON_IMAGE_ID "1" + IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object) IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object) @@ -23,6 +31,142 @@ HYDROData_Image::~HYDROData_Image() { } +QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const +{ + QStringList aResList; + + Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this ); + if ( aDocument.IsNull() ) + return aResList; + + QString aDocName = aDocument->GetDocPyName(); + QString anImageName = GetName(); + + aResList << QString( "%1 = %2.CreateObject( %3 );" ) + .arg( anImageName ).arg( aDocName ).arg( PYTHON_IMAGE_ID ); + aResList << QString( "%1.SetName( \"%2\" );" ) + .arg( anImageName ).arg( anImageName ); + + QString aFilePath = GetFilePath(); + if ( !aFilePath.isEmpty() ) + { + aResList << QString( "" ); + aResList << QString( "%1.LoadImage( \"%2\" );" ) + .arg( anImageName ).arg( aFilePath ); + } + else + { + // Image is composed from other image(s) + + QString anOperatorName = OperatorName(); + if ( !anOperatorName.isEmpty() ) + { + aResList << QString( "" ); + + aResList << QString( "%1.SetOperatorName( \"%2\" );" ) + .arg( anImageName ).arg( anOperatorName ); + + ImageComposer_Operator* anImageOp = HYDROOperations_Factory::Factory()->Operator( this ); + if ( anImageOp ) + { + // Dump operation arguments + QString anOpArgsArrayName; + QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName ); + if ( !anOpArgs.isEmpty() ) + { + aResList << QString( "" ); + aResList << anOpArgs; + + aResList << QString( "" ); + aResList << QString( "%1.SetArgs( \"%2\" );" ) + .arg( anImageName ).arg( anOpArgsArrayName ); + } + } + } + + int aNbReferences = NbReferences(); + if ( aNbReferences > 0 ) + { + aResList << QString( "" ); + + for ( int i = 0; i < aNbReferences; ++i ) + { + Handle(HYDROData_Image) aRefImg = Reference( i ); + if ( aRefImg.IsNull() ) + continue; + + QString aRefImgName = aRefImg->GetName(); + + // The definition of reference image must be dumped before this + if ( !theTreatedObjects.contains( aRefImgName ) ) + { + // Write definition of reference image + QStringList aRefImgDump = aRefImg->DumpToPython( theTreatedObjects ); + if ( aRefImgDump.isEmpty() ) + continue; + + QStringList aTmpList = aResList; + aResList = aRefImgDump; + + aResList.append( "" ); + aResList.append( aTmpList ); + + theTreatedObjects.insert( aRefImgName, aRefImg ); + } + + aResList << QString( "%1.AppendReference( %2 );" ) + .arg( anImageName ).arg( aRefImgName ); + } + } + } + + // Dump transformation matrix for image + aResList << QString( "" ); + + QTransform aTrsf = Trsf(); + + aResList << QString( "trsf = QTransform( %2, %3, %4," ) + .arg( aTrsf.m11() ).arg( aTrsf.m12() ).arg( aTrsf.m13() ); + aResList << QString( " %1, %2, %3," ) + .arg( aTrsf.m21() ).arg( aTrsf.m22() ).arg( aTrsf.m23() ); + aResList << QString( " %1, %2, %3 );" ) + .arg( aTrsf.m31() ).arg( aTrsf.m32() ).arg( aTrsf.m33() ); + + aResList << QString( "%1.SetTrsf( trsf );" ).arg( anImageName ); + + // Dump transformation points for image + aResList << QString( "" ); + + QPoint aPointAIn, aPointBIn, aPointCIn; + QPointF aPointAOut, aPointBOut, aPointCOut; + TrsfPoints( aPointAIn, aPointBIn, aPointCIn, + aPointAOut, aPointBOut, aPointCOut ); + + aResList << QString( "a_in = QPoint( %1, %2 );" ) + .arg( aPointAIn.x() ).arg( aPointAIn.y() ); + + aResList << QString( "b_in = QPoint( %1, %2 );" ) + .arg( aPointBIn.x() ).arg( aPointBIn.y() ); + + aResList << QString( "c_in = QPoint( %1, %2 );" ) + .arg( aPointCIn.x() ).arg( aPointCIn.y() ); + + aResList << QString( "a_out = QPointF( %1, %2 );" ) + .arg( aPointAOut.x() ).arg( aPointAOut.y() ); + + aResList << QString( "b_out = QPointF( %1, %2 );" ) + .arg( aPointBOut.x() ).arg( aPointBOut.y() ); + + aResList << QString( "c_out = QPointF( %1, %2 );" ) + .arg( aPointCOut.x() ).arg( aPointCOut.y() ); + + QString aGap = QString().fill( ' ', anImageName.size() + 16 ); + aResList << QString( "%1.SetTrsfPoints( a_in, b_in, c_in," ).arg( anImageName ); + aResList << QString( aGap + "a_out, b_out, c_out );" ); + + return aResList; +} + void HYDROData_Image::SetImage(const QImage& theImage) { if (theImage.isNull()) { @@ -45,6 +189,13 @@ void HYDROData_Image::SetImage(const QImage& theImage) SaveByteArray(0, aData, theImage.byteCount()); } +bool HYDROData_Image::LoadImage(const QString& theFilePath) +{ + QImage anImage( theFilePath ); + SetImage( anImage ); + return !anImage.isNull(); +} + QImage HYDROData_Image::Image() { Handle(TDataStd_IntegerArray) aParams; @@ -95,7 +246,7 @@ void HYDROData_Image::SetTrsf(const QTransform& theTrsf) anArray->SetValue(9, theTrsf.m33()); } -QTransform HYDROData_Image::Trsf() +QTransform HYDROData_Image::Trsf() const { // get 9 coeffs of matrix from the real array Handle(TDataStd_RealArray) anArray; @@ -138,7 +289,7 @@ void HYDROData_Image::TrsfPoints(QPoint& thePointAIn, QPoint& thePointCIn, QPointF& thePointAOut, QPointF& thePointBOut, - QPointF& thePointCOut) + QPointF& thePointCOut) const { Handle(TDataStd_RealArray) anArray; if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) { @@ -159,7 +310,7 @@ void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced) aRefs->Append(theReferenced->Label()); } -int HYDROData_Image::NbReferences() +int HYDROData_Image::NbReferences() const { Handle(TDataStd_ReferenceList) aRefs; if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs)) @@ -230,7 +381,7 @@ void HYDROData_Image::SetOperatorName(const QString theOpName) TCollection_ExtendedString(theOpName.toLatin1().constData())); } -QString HYDROData_Image::OperatorName() +QString HYDROData_Image::OperatorName() const { Handle(TDataStd_Name) aName; if (myLab.FindChild(DataTag_Operator). @@ -246,7 +397,7 @@ void HYDROData_Image::SetArgs(const QByteArray& theArgs) SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length()); } -QByteArray HYDROData_Image::Args() +QByteArray HYDROData_Image::Args() const { int aLen = 0; const char* aData = ByteArray(DataTag_Operator, aLen); diff --git a/src/HYDROData/HYDROData_Image.h b/src/HYDROData/HYDROData_Image.h index 8b069add..b9db13c6 100644 --- a/src/HYDROData/HYDROData_Image.h +++ b/src/HYDROData/HYDROData_Image.h @@ -36,12 +36,23 @@ public: */ HYDRODATA_EXPORT virtual const ObjectKind GetKind() const {return KIND_IMAGE;} + /** + * Dump Image object to Python script representation. + */ + HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + /** * Stores the image * \param theImage new image */ HYDRODATA_EXPORT void SetImage(const QImage& theImage); + /** + * Load the image from file + * \param theFilePath path to image + */ + HYDRODATA_EXPORT bool LoadImage(const QString& theFilePath); + /** * Returns the kept image */ @@ -67,7 +78,7 @@ public: /** * Returns the kept transformation, or "identity" if not yet stored */ - HYDRODATA_EXPORT QTransform Trsf(); + HYDRODATA_EXPORT QTransform Trsf() const; /** * Stores the image transformation points (3 input + 3 output) @@ -99,7 +110,7 @@ public: QPoint& thePointCIn, QPointF& thePointAOut, QPointF& thePointBOut, - QPointF& thePointCOut); + QPointF& thePointCOut) const; /** * Appends reference to other image. @@ -111,7 +122,7 @@ public: * Returns the number of referenced images * \return zero if there is no references */ - HYDRODATA_EXPORT int NbReferences(); + HYDRODATA_EXPORT int NbReferences() const; /** * Returns reference by index. @@ -150,7 +161,7 @@ public: * Returns the operator name * \returns the name of the operator that must be executed for image update */ - HYDRODATA_EXPORT QString OperatorName(); + HYDRODATA_EXPORT QString OperatorName() const; /** * Stores the operator arguments @@ -162,7 +173,7 @@ public: * Returns the operator arguments * \returns array that stores the operator arguments, needed for execution */ - HYDRODATA_EXPORT QByteArray Args(); + HYDRODATA_EXPORT QByteArray Args() const; /** * Sets the "MustBeUpdated" flag: if image is depended on updated features. diff --git a/src/HYDROData/HYDROData_Object.cxx b/src/HYDROData/HYDROData_Object.cxx index c2bad239..1b6337a0 100644 --- a/src/HYDROData/HYDROData_Object.cxx +++ b/src/HYDROData/HYDROData_Object.cxx @@ -96,7 +96,7 @@ void HYDROData_Object::SaveByteArray(const int theTag, } } -const char* HYDROData_Object::ByteArray(const int theTag, int& theLen) +const char* HYDROData_Object::ByteArray(const int theTag, int& theLen) const { TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag); Handle(TDataStd_ByteArray) aData; diff --git a/src/HYDROData/HYDROData_Object.h b/src/HYDROData/HYDROData_Object.h index 84d60156..0c733954 100644 --- a/src/HYDROData/HYDROData_Object.h +++ b/src/HYDROData/HYDROData_Object.h @@ -132,7 +132,7 @@ protected: * \returns pointer to the internal data structure wit harray content, * or NULL if array size is zero */ - const char* ByteArray(const int theTag, int& theLen); + const char* ByteArray(const int theTag, int& theLen) const; protected: /// Array of pointers to the properties of this object; index in this array is returned by \a AddProperty. diff --git a/src/HYDROPy/HYDROData_Image.sip b/src/HYDROPy/HYDROData_Image.sip index 08617394..f1e7cff9 100644 --- a/src/HYDROPy/HYDROData_Image.sip +++ b/src/HYDROPy/HYDROData_Image.sip @@ -50,6 +50,12 @@ public: */ void SetImage( const QImage& theImage ); + /** + * Load the image from file + * \param theFilePath path to image + */ + bool LoadImage( const QString& theFilePath ); + /** * Returns the kept image */