-#include <HYDROData_Image.h>
-#include <HYDROData_Iterator.h>
+
+#include "HYDROData_Image.h"
+
+#include "HYDROData_Iterator.h"
+#include "HYDROOperations_Factory.h"
+#include "HYDROData_Tool.h"
#include <TDataStd_RealArray.hxx>
-#include <TDataStd_IntegerArray.hxx>
#include <TDataStd_ByteArray.hxx>
+#include <TDataStd_IntegerArray.hxx>
#include <TDataStd_ReferenceList.hxx>
+#include <TDataStd_Name.hxx>
+#include <TDataStd_UAttribute.hxx>
+#include <TDataStd_AsciiString.hxx>
#include <TDF_ListIteratorOfLabelList.hxx>
+#include <ImageComposer_Operator.h>
+#include <ImageComposer_MetaTypes.h>
+
+#include <QStringList>
+
+static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
+static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
+
+#define PYTHON_IMAGE_ID "1"
+
IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
{
}
+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 = Handle(HYDROData_Image)::DownCast( 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 );
+ }
+ }
+
+ // Necessary to update image in case of composed operator
+ aResList << QString( "" );
+ aResList << QString( "%1.Update();" ).arg( anImageName );
+ }
+
+ // 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::Update()
+{
+ HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
+
+ // Update image only if there is an operation
+ ImageComposer_Operator* anOp = aFactory->Operator( this );
+ if ( anOp )
+ {
+ // Fill by arguments and process the operation
+ QVariant anObj1, anObj2;
+ int aNbReferences = NbReferences();
+
+ if ( aNbReferences > 0 )
+ {
+ // First referenced object
+ Handle(HYDROData_Object) aRefObj = Reference( 0 );
+ if ( !aRefObj.IsNull() )
+ {
+ anObj1 = aRefObj->GetDataVariant();
+ if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
+ {
+ ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
+ QTransform aTransform = anImage.transform();
+ SetTrsf( aTransform );
+ }
+ }
+ }
+
+ if ( aNbReferences > 1 )
+ {
+ // Second referenced object
+ Handle(HYDROData_Object) aRefObj = Reference( 1 );
+ if ( !aRefObj.IsNull() )
+ anObj2 = aRefObj->GetDataVariant();
+ }
+
+ ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
+ SetImage( aResImg );
+ }
+
+ Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
+ if ( !aDocument.IsNull() )
+ {
+ // Change the states of this and all depended images
+ MustBeUpdated( true );
+ HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
+ MustBeUpdated( false );
+ }
+}
+
+QVariant HYDROData_Image::GetDataVariant()
+{
+ QTransform aTransform = Trsf();
+
+ ImageComposer_Image anImage = Image();
+ anImage.setTransform( aTransform );
+
+ QVariant aVarData;
+ aVarData.setValue<ImageComposer_Image>( anImage );
+
+ return aVarData;
+}
+
void HYDROData_Image::SetImage(const QImage& theImage)
{
if (theImage.isNull()) {
aParams->SetValue(3, theImage.bytesPerLine());
aParams->SetValue(4, (int)(theImage.format()));
// store data of image in byte array
- Handle(TDataStd_ByteArray) aData;
- int aLen = theImage.byteCount();
- if (!myLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
- aData = TDataStd_ByteArray::Set(myLab, 1, aLen);
- }
- // copy bytes one by one
- const uchar* aBits = theImage.bits();
- if (aData->Length() != aLen) {
- Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, aLen);
- for(int a = 0; a < aLen; a++)
- aNewData->SetValue(a + 1, aBits[a]);
- aData->ChangeArray(aNewData);
- } else {
- for(int a = 0; a < aLen; a++)
- aData->SetValue(a + 1, aBits[a]);
- }
+ const char* aData = (const char*)(theImage.bits());
+ 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;
- Handle(TDataStd_ByteArray) aData;
- if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams) ||
- !myLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
+ if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
+ return QImage(); // return empty image if there is no array
+ int aLen = 0;
+ uchar* anArray = (uchar*)ByteArray(0, aLen);
+ if (!aLen)
return QImage(); // return empty image if there is no array
- /*
- // make uchar array one by one
- int aLen = aData->Upper();
- uchar* anArray = new uchar[aLen];
- for(int a = 0; a < aLen; a++)
- anArray[a] = aData->Value(a + 1);
- // recreate image from integer parameters and array of bytes
- QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
- aParams->Value(3), QImage::Format(aParams->Value(4)));
- delete [] anArray; <- this is wrong, because QImage references to this array
- */
- uchar* anArray = (uchar*)(void*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
aParams->Value(3), QImage::Format(aParams->Value(4)));
-
return aResult;
}
+void HYDROData_Image::SetFilePath(const QString& theFilePath)
+{
+ TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
+ TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
+}
+
+QString HYDROData_Image::GetFilePath() const
+{
+ QString aRes;
+
+ Handle(TDataStd_AsciiString) anAsciiStr;
+ if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
+ aRes = QString( anAsciiStr->Get().ToCString() );
+
+ return aRes;
+}
+
void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
{
// locate 9 coeffs of matrix into the real array
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;
return aTrsf;
}
-void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
+void HYDROData_Image::SetTrsfPoints(const QPoint& thePointAIn,
+ const QPoint& thePointBIn,
+ const QPoint& thePointCIn,
+ const QPointF& thePointAOut,
+ const QPointF& thePointBOut,
+ const QPointF& thePointCOut)
+{
+ Handle(TDataStd_RealArray) anArray;
+ if (!myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
+ anArray = TDataStd_RealArray::Set(myLab.FindChild(DataTag_TrsfPoints), 1, 12);
+ }
+ anArray->SetValue(1, thePointAIn.x());
+ anArray->SetValue(2, thePointAIn.y());
+ anArray->SetValue(3, thePointBIn.x());
+ anArray->SetValue(4, thePointBIn.y());
+ anArray->SetValue(5, thePointCIn.x());
+ anArray->SetValue(6, thePointCIn.y());
+ anArray->SetValue(7, thePointAOut.x());
+ anArray->SetValue(8, thePointAOut.y());
+ anArray->SetValue(9, thePointBOut.x());
+ anArray->SetValue(10, thePointBOut.y());
+ anArray->SetValue(11, thePointCOut.x());
+ anArray->SetValue(12, thePointCOut.y());
+}
+
+void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
+ QPoint& thePointBIn,
+ QPoint& thePointCIn,
+ QPointF& thePointAOut,
+ QPointF& thePointBOut,
+ QPointF& thePointCOut) const
+{
+ Handle(TDataStd_RealArray) anArray;
+ if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
+ thePointAIn = QPointF( anArray->Value(1), anArray->Value(2) ).toPoint();
+ thePointBIn = QPointF( anArray->Value(3), anArray->Value(4) ).toPoint();
+ thePointCIn = QPointF( anArray->Value(5), anArray->Value(6) ).toPoint();
+ thePointAOut = QPointF( anArray->Value(7), anArray->Value(8) );
+ thePointBOut = QPointF( anArray->Value(9), anArray->Value(10) );
+ thePointCOut = QPointF( anArray->Value(11), anArray->Value(12) );
+ }
+}
+
+bool HYDROData_Image::HasTrsfPoints() const
+{
+ Handle(TDataStd_RealArray) anArray;
+ return myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray);
+}
+
+void HYDROData_Image::AppendReference(Handle(HYDROData_Object) theReferenced)
{
Handle(TDataStd_ReferenceList) aRefs;
if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
aRefs->Append(theReferenced->Label());
}
-int HYDROData_Image::NbReferences()
+int HYDROData_Image::NbReferences() const
{
Handle(TDataStd_ReferenceList) aRefs;
if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
return aRefs->Extent();
}
-Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
+Handle(HYDROData_Object) HYDROData_Image::Reference(const int theIndex) const
{
Handle(TDataStd_ReferenceList) aRefs;
if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
- return Handle(HYDROData_Image)();
+ return Handle(HYDROData_Object)();
if (theIndex < 0 || theIndex >= aRefs->Extent())
- return Handle(HYDROData_Image)();
+ return Handle(HYDROData_Object)();
TDF_ListIteratorOfLabelList anIter(aRefs->List());
for(int anIndex = 0; anIndex != theIndex; anIter.Next(), anIndex++);
const TDF_Label& aRefLab = anIter.Value();
- return Handle(HYDROData_Image)::DownCast(HYDROData_Iterator::Object(aRefLab));
+ return Handle(HYDROData_Object)::DownCast(HYDROData_Iterator::Object(aRefLab));
}
void HYDROData_Image::ChangeReference(
- const int theIndex, Handle(HYDROData_Image) theReferenced)
+ const int theIndex, Handle(HYDROData_Object) theReferenced)
{
Handle(TDataStd_ReferenceList) aRefs;
if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
{
myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
}
+
+void HYDROData_Image::SetOperatorName(const QString theOpName)
+{
+ TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
+ TCollection_ExtendedString(theOpName.toLatin1().constData()));
+}
+
+QString HYDROData_Image::OperatorName() const
+{
+ Handle(TDataStd_Name) aName;
+ if (myLab.FindChild(DataTag_Operator).
+ FindAttribute(TDataStd_Name::GetID(), aName)) {
+ TCollection_AsciiString aStr(aName->Get());
+ return QString(aStr.ToCString());
+ }
+ return QString();
+}
+
+void HYDROData_Image::SetArgs(const QByteArray& theArgs)
+{
+ SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
+}
+
+QByteArray HYDROData_Image::Args() const
+{
+ int aLen = 0;
+ const char* aData = ByteArray(DataTag_Operator, aLen);
+ if (!aLen)
+ return QByteArray();
+ return QByteArray(aData, aLen);
+}
+
+void HYDROData_Image::MustBeUpdated(bool theFlag)
+{
+ if (theFlag) {
+ TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
+ } else {
+ myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
+ }
+}
+
+bool HYDROData_Image::MustBeUpdated() const
+{
+ return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
+}
+
+void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
+{
+ if (theFlag) {
+ TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
+ } else {
+ myLab.ForgetAttribute(GUID_SELF_SPLITTED);
+ }
+}
+
+bool HYDROData_Image::IsSelfSplitted() const
+{
+ return myLab.IsAttribute(GUID_SELF_SPLITTED);
+}