]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/test_HYDROData_Image.cxx
Salome HOME
Merge branch 'BR_v14_rc' into BR_SINUSX_FORMAT
[modules/hydro.git] / src / HYDROData / test_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<test_HYDROData_Image.h>
20
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Image.h>
23 #include <QPainter>
24
25 static QImage TestImage() {
26   QImage aPic(50, 40, QImage::Format_RGB32);
27   QPainter aPainter(&aPic);
28   aPainter.drawEllipse(6, 7, 38, 30);
29   aPainter.drawLine(0, 40, 10, 0);
30   aPainter.drawLine(10, 0, 25, 35);
31   aPainter.drawLine(25, 35, 40, 0);
32   aPainter.drawLine(40, 0, 50, 40);
33   return aPic;
34 }
35
36 void test_HYDROData_Image::testQImage()
37 {
38   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
39
40   Handle(HYDROData_Image) anImage = 
41     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
42   // empty image
43   QImage anEmpty = anImage->Image();
44   CPPUNIT_ASSERT(anEmpty.isNull());
45
46   // prepare Qt Image for testing
47   QImage aPic(TestImage());
48   anImage->SetImage(aPic);
49   QImage aRestored = anImage->Image();
50
51   CPPUNIT_ASSERT(!aRestored.isNull());
52   //aRestored.save("pic2.bmp");
53   CPPUNIT_ASSERT(aPic == aRestored);
54
55   aDoc->Close();
56 }
57
58 void test_HYDROData_Image::testTrsf()
59 {
60   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
61
62   Handle(HYDROData_Image) anImage = 
63     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
64   // empty trsf
65   QTransform anEmpty = anImage->Trsf();
66   CPPUNIT_ASSERT(anEmpty.isIdentity());
67
68   // prepare Qt transformation for testing
69   QTransform aTrsf;
70   aTrsf.translate(50, 50);
71   aTrsf.rotate(45);
72   aTrsf.scale(0.5, 1.0);
73
74   anImage->SetTrsf(aTrsf);
75   QTransform aRestored = anImage->Trsf();
76
77   CPPUNIT_ASSERT(!aRestored.isIdentity());
78   CPPUNIT_ASSERT(aTrsf == aRestored);
79
80   aDoc->Close();
81 }
82
83 void test_HYDROData_Image::testReferences()
84 {
85   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
86
87   Handle(HYDROData_Image) anImage1 = 
88     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
89   Handle(HYDROData_Image) anImage2 = 
90     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
91   Handle(HYDROData_Image) anImage3 = 
92     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
93
94   // check initially there is no references
95   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 0);
96
97   // append reference
98   anImage3->AppendReference(anImage1);
99   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 1);
100   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage1));
101
102   // change reference
103   anImage3->ChangeReference(0, anImage2);
104   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 1);
105   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage2));
106
107   // append one more reference
108   anImage3->AppendReference(anImage1);
109   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 2);
110   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage2));
111   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(1), anImage1));
112
113   // remove reference
114   anImage3->RemoveReference(0);
115   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 1);
116   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage1));
117
118   // remove reference
119   anImage3->RemoveReference(0);
120   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 0);
121
122   // append two references once again and remove all references
123   anImage3->ChangeReference(0, anImage1);
124   anImage3->ChangeReference(1, anImage2);
125   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 2);
126   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage1));
127   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(1), anImage2));
128   anImage3->ClearReferences();
129   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 0);
130
131   aDoc->Close();
132 }
133
134 void test_HYDROData_Image::testCopy()
135 {
136   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
137
138   Handle(HYDROData_Image) anImage1 = 
139     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
140   // fill image 1
141   anImage1->SetImage(TestImage());
142
143   QTransform aTrsf;
144   aTrsf.translate(50, 50);
145   aTrsf.rotate(45);
146   aTrsf.scale(0.5, 1.0);
147   anImage1->SetTrsf(aTrsf);
148
149   Handle(HYDROData_Image) anImage2 = 
150     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
151
152   anImage1->AppendReference(anImage2);
153
154   // copy image to the new one
155   Handle(HYDROData_Image) anImage3 = 
156     Handle(HYDROData_Image)::DownCast(aDoc->CreateObject(KIND_IMAGE));
157   anImage1->CopyTo(anImage3);
158
159   // check all fields are correctly copied
160   CPPUNIT_ASSERT(anImage1->Image() == anImage3->Image());
161   CPPUNIT_ASSERT(anImage1->Trsf() == anImage3->Trsf());
162   CPPUNIT_ASSERT_EQUAL(anImage3->NbReferences(), 1);
163   CPPUNIT_ASSERT(IsEqual(anImage3->Reference(0), anImage2));
164
165   aDoc->Close();
166 }