From 351f09c8bb1b7df5f253a7123705c86a56dbc6e0 Mon Sep 17 00:00:00 2001 From: ichesnok Date: Thu, 31 Aug 2023 15:56:33 +0100 Subject: [PATCH] 0033183: Data Exchange - Lose texture after saving XBF file Texture reading and writing changed in VisMaterial drivers --- .../BinMXCAFDoc_VisMaterialDriver.cxx | 63 ++++++++++++++++--- .../XmlMXCAFDoc_VisMaterialDriver.cxx | 59 ++++++++++++++--- tests/bugs/xde/begin | 2 +- tests/bugs/xde/bug33183_1 | 21 +++++++ tests/bugs/xde/bug33183_2 | 21 +++++++ 5 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 tests/bugs/xde/bug33183_1 create mode 100644 tests/bugs/xde/bug33183_2 diff --git a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx index b6f8031978..f7f183ebab 100644 --- a/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx +++ b/src/BinMXCAFDoc/BinMXCAFDoc_VisMaterialDriver.cxx @@ -126,23 +126,68 @@ static void readColor (const BinObjMgt_Persistent& theSource, static void writeTexture (BinObjMgt_Persistent& theTarget, const Handle(Image_Texture)& theImage) { - theTarget.PutAsciiString (!theImage.IsNull() - && !theImage->FilePath().IsEmpty() - && theImage->FileOffset() == -1 - ? theImage->FilePath() - : ""); + if (theImage.IsNull()) + { + theTarget.PutAsciiString(""); + return; + } + if (theImage->DataBuffer().IsNull()) + { + theTarget.PutAsciiString(theImage->FilePath()); + theTarget.PutBoolean(false); + if (theImage->FileOffset() == -1 || theImage->FileLength() == -1) + { + theTarget.PutBoolean(true); + return; + } + theTarget.PutBoolean(false); + theTarget.PutInteger(static_cast(theImage->FileOffset())); + theTarget.PutInteger(static_cast(theImage->FileLength())); + return; + } + theTarget.PutAsciiString(theImage->TextureId()); + theTarget.PutBoolean(true); + theTarget.PutInteger(static_cast(theImage->DataBuffer()->Size())); + theTarget.PutByteArray((Standard_Byte*)theImage->DataBuffer()->Data(), + static_cast(theImage->DataBuffer()->Size())); } //! Decode texture path. static void readTexture (const BinObjMgt_Persistent& theSource, Handle(Image_Texture)& theTexture) { - TCollection_AsciiString aPath; - theSource.GetAsciiString (aPath); - if (!aPath.IsEmpty()) + TCollection_AsciiString aStr; + theSource.GetAsciiString(aStr); + if (aStr.IsEmpty()) { - theTexture = new Image_Texture (aPath); + return; + } + Standard_Boolean anUseBuffer; + if (!theSource.GetBoolean(anUseBuffer).IsOK()) + { + theTexture = new Image_Texture(aStr); + return; + } + Standard_Integer anOffset = -1, aLength = -1; + if (!anUseBuffer) + { + Standard_Boolean isOnlyFilePath; + theSource.GetBoolean(isOnlyFilePath); + if (isOnlyFilePath) + { + theTexture = new Image_Texture(aStr); + return; + } + theSource.GetInteger(anOffset); + theSource.GetInteger(aLength); + theTexture = new Image_Texture(aStr, anOffset, aLength); + return; } + theSource.GetInteger(aLength); + Handle(NCollection_Buffer) aBuff = + new NCollection_Buffer(NCollection_BaseAllocator::CommonBaseAllocator(), aLength); + theSource.GetByteArray(aBuff->ChangeData(), aLength); + theTexture = new Image_Texture(aBuff, aStr); } //======================================================================= diff --git a/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx b/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx index 257620e455..c5ae23682f 100644 --- a/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx +++ b/src/XmlMXCAFDoc/XmlMXCAFDoc_VisMaterialDriver.cxx @@ -13,9 +13,11 @@ #include +#include #include #include #include +#include #include IMPLEMENT_STANDARD_RTTIEXT(XmlMXCAFDoc_VisMaterialDriver, XmlMDF_ADriver) @@ -42,6 +44,10 @@ IMPLEMENT_DOMSTRING(EmissiveColor, "emissive_color") IMPLEMENT_DOMSTRING(Shininess, "shininess") IMPLEMENT_DOMSTRING(Transparency, "transparency") IMPLEMENT_DOMSTRING(DiffuseTexture, "diffuse_texture") +IMPLEMENT_DOMSTRING(FilePath, "file_path") +IMPLEMENT_DOMSTRING(TextureId, "texture_id") +IMPLEMENT_DOMSTRING(Offset, "offset") +IMPLEMENT_DOMSTRING(Length, "length") //! Encode alpha mode into character. static const char* alphaModeToString (Graphic3d_AlphaMode theMode) @@ -202,11 +208,26 @@ static void writeTexture (XmlObjMgt_Persistent& theTarget, const XmlObjMgt_DOMString& theName, const Handle(Image_Texture)& theImage) { - if (!theImage.IsNull() - && !theImage->FilePath().IsEmpty() - && theImage->FileOffset() == -1) + if (theImage.IsNull()) { - theTarget.Element().setAttribute (theName, theImage->FilePath().ToCString()); + return; + } + XmlObjMgt_Document aDoc(theTarget.Element().getOwnerDocument()); + XmlObjMgt_Element aCurTarget = aDoc.createElement(theName); + theTarget.Element().appendChild(aCurTarget); + if (theImage->DataBuffer().IsNull()) + { + aCurTarget.setAttribute(::FilePath(), theImage->FilePath().ToCString()); + if (theImage->FileOffset() == -1 || theImage->FileLength() == -1) + { + return; + } + aCurTarget.setAttribute(::Offset(), static_cast(theImage->FileOffset())); + aCurTarget.setAttribute(::Length(), static_cast(theImage->FileLength())); + } + else + { + Message::SendWarning(TCollection_AsciiString("Can't write a texture to buffer.")); } } @@ -215,10 +236,34 @@ static void readTexture (const XmlObjMgt_Element& theElement, const XmlObjMgt_DOMString& theName, Handle(Image_Texture)& theImage) { - TCollection_AsciiString aPath (theElement.getAttribute (theName).GetString()); - if (!aPath.IsEmpty()) + TCollection_AsciiString aStr(theElement.getAttribute(theName).GetString()); + if (!aStr.IsEmpty()) + { + theImage = new Image_Texture(aStr); + return; + } + LDOM_Element anElement = theElement.GetChildByTagName(theName); + if (anElement.isNull()) + { + return; + } + TCollection_AsciiString aFilePath(anElement.getAttribute(::FilePath()).GetString()); + TCollection_AsciiString anId(anElement.getAttribute(::TextureId()).GetString()); + Standard_Integer anOffset = -1, aLength = -1; + if (!aFilePath.IsEmpty()) + { + anElement.getAttribute(::Offset()).GetInteger(anOffset); + anElement.getAttribute(::Length()).GetInteger(aLength); + if (anOffset == -1 || aLength == -1) + { + theImage = new Image_Texture(aFilePath); + return; + } + theImage = new Image_Texture(aFilePath, anOffset, aLength); + } + else if (!anId.IsEmpty()) { - theImage = new Image_Texture (aPath); + Message::SendWarning(TCollection_AsciiString("Can't read a texture from buffer.")); } } diff --git a/tests/bugs/xde/begin b/tests/bugs/xde/begin index e5cf220f8b..c212a59ec3 100755 --- a/tests/bugs/xde/begin +++ b/tests/bugs/xde/begin @@ -1,3 +1,3 @@ -pload XDE +pload XDE OCAF VISUALISATION set subgroup xde diff --git a/tests/bugs/xde/bug33183_1 b/tests/bugs/xde/bug33183_1 new file mode 100644 index 0000000000..78aed7068f --- /dev/null +++ b/tests/bugs/xde/bug33183_1 @@ -0,0 +1,21 @@ +puts "========" +puts "0033183: Data Exchange - Lose texture after saving XBF file" +puts "Checking saving of textures for the previous version" +puts "========" + +Close D -silent +XOpen [locate_data_file bug33183_ship_boat.xbf] D +set data [XGetVisMaterial D 0:1:10:3] + +if {[string first "Common.DiffuseTexture" $data] == -1} { + puts "Error: Texture is not found" +} + +vinit View1 +XDisplay -dispMode 1 D +vfit +if { [vreadpixel 130 300 rgb name] != "ROSYBROWN" } { puts "Error: color not match" } +if { [vreadpixel 150 250 rgb name] != "ORANGE2" } { puts "Error: color not match" } +if { [vreadpixel 250 250 rgb name] != "GRAY43" } { puts "Error: color not match" } + +Close D diff --git a/tests/bugs/xde/bug33183_2 b/tests/bugs/xde/bug33183_2 new file mode 100644 index 0000000000..3e1a7fad20 --- /dev/null +++ b/tests/bugs/xde/bug33183_2 @@ -0,0 +1,21 @@ +puts "========" +puts "0033183: Data Exchange - Lose texture after saving XBF file" +puts "Checking saving of textures for the previous version" +puts "========" + +Close D -silent +XOpen [locate_data_file bug33183_ship_boat.xml] D +set data [XGetVisMaterial D 0:1:10:3] + +if {[string first "Common.DiffuseTexture" $data] == -1} { + puts "Error: Texture is not found" +} + +vinit View1 +XDisplay -dispMode 1 D +vfit +if { [vreadpixel 130 300 rgb name] != "ROSYBROWN" } { puts "Error: color not match" } +if { [vreadpixel 150 250 rgb name] != "ORANGE2" } { puts "Error: color not match" } +if { [vreadpixel 250 250 rgb name] != "GRAY43" } { puts "Error: color not match" } + +Close D -- 2.39.5