Salome HOME
Copyright update 2022
[modules/shaper.git] / src / Model / Model_AttributeImage.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_AttributeImage.h>
21 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_Attribute.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Object.h>
25
26 #include <Standard_TypeDef.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_ExtendedString.hxx>
29
30 #include <TDataStd_IntegerArray.hxx>
31 #include <TDataStd_ByteArray.hxx>
32 #include <TDataStd_Comment.hxx>
33 #include <TDF_CopyLabel.hxx>
34
35 void Model_AttributeImage::setTexture(const int theWidth,
36                                       const int theHeight,
37                                       const std::list<unsigned char>& theByteArray,
38                                       const std::string& theFormat,
39                                       const bool sendUpdated)
40 {
41   if (theWidth > 0 && theHeight > 0 && theByteArray.size() > 0) { // set new data
42     // Find or create attributes
43     Handle_TDataStd_ByteArray aByteArray =
44       TDataStd_ByteArray::Set(myLab, 0, int(theByteArray.size()) - 1);
45     Handle_TDataStd_IntegerArray aDimensions =
46       TDataStd_IntegerArray::Set(myLab, 0, 1);
47     Handle_TDataStd_Comment aFormat = TDataStd_Comment::Set(myLab, theFormat.c_str());
48
49     // Dimensions
50     aDimensions->SetValue(0, theWidth);
51     aDimensions->SetValue(1, theHeight);
52
53     // Texture
54     Handle(TColStd_HArray1OfByte) aNewArray =
55       new TColStd_HArray1OfByte(0, int(theByteArray.size()) - 1);
56     std::list<unsigned char>::const_iterator itBA = theByteArray.begin();
57     for (int j = 0; itBA != theByteArray.end(); ++itBA, ++j) {
58       aNewArray->SetValue(j, (Standard_Byte)(*itBA));
59     }
60     aByteArray->ChangeArray(aNewArray);
61
62     if (sendUpdated)
63       owner()->data()->sendAttributeUpdated(this);
64   }
65   else { // size is zero => arrays must be erased
66     bool isForgotten1 = myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
67     bool isForgotten2 = myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
68     bool isForgotten3 = myLab.ForgetAttribute(TDataStd_Comment::GetID());
69
70     if (sendUpdated && (isForgotten1 || isForgotten2 || isForgotten3))
71       owner()->data()->sendAttributeUpdated(this);
72   }
73 }
74
75 bool Model_AttributeImage::hasTexture()
76 {
77   Handle_TDataStd_ByteArray aByteArray;
78   Handle_TDataStd_IntegerArray aDimensions;
79   if (myLab.FindAttribute(TDataStd_ByteArray::GetID(), aByteArray) == Standard_True &&
80       myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aDimensions) == Standard_True) {
81
82     // Dimensions
83     if (aDimensions->Value(0) > 0 && aDimensions->Value(1) > 0)
84       // Byte array
85       return aByteArray->Length() > 0;
86   }
87   return false;
88 }
89
90 bool Model_AttributeImage::texture(int& theWidth,
91                                    int& theHeight,
92                                    std::list<unsigned char>& theByteArray,
93                                    std::string& theFormat)
94 {
95   // Init return values
96   theWidth = 0;
97   theHeight = 0;
98   theByteArray.clear();
99   theFormat = "";
100
101   Handle_TDataStd_ByteArray aByteArray;
102   Handle_TDataStd_IntegerArray aDimensions;
103   Handle_TDataStd_Comment aFormat;
104   if (myLab.FindAttribute(TDataStd_ByteArray::GetID(), aByteArray) == Standard_True &&
105       myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aDimensions) == Standard_True &&
106       myLab.FindAttribute(TDataStd_Comment::GetID(), aFormat) == Standard_True) {
107
108     // Dimensions
109     theWidth  = aDimensions->Value(0);
110     theHeight = aDimensions->Value(1);
111
112     // Texture
113     const Handle(TColStd_HArray1OfByte) byteArray = aByteArray->InternalArray();
114     for (int j = byteArray->Lower(); j <= byteArray->Upper(); j++) {
115       theByteArray.push_back((unsigned char)byteArray->Value( j ));
116     }
117
118     // Format
119     theFormat = TCollection_AsciiString(aFormat->Get()).ToCString();
120     return true;
121   }
122
123   return false;
124 }
125
126 void Model_AttributeImage::copyTo(AttributeImagePtr theTarget) const
127 {
128   std::shared_ptr<Model_AttributeImage> aTarget =
129     std::dynamic_pointer_cast<Model_AttributeImage>(theTarget);
130   if (aTarget) {
131     //Model_AttributeSelectionList::copyAttrs(myLab, aTarget->myLab);
132     TDF_CopyLabel aCopyAlgo (myLab, aTarget->myLab);
133     aCopyAlgo.Perform();
134     aTarget->reinit();
135   }
136 }
137
138 Model_AttributeImage::Model_AttributeImage(TDF_Label& theLabel)
139 {
140   myLab = theLabel;
141   reinit();
142 }
143
144 void Model_AttributeImage::reinit()
145 {
146   myIsInitialized = myLab.IsAttribute(TDataStd_ByteArray::GetID());
147 }