1 // Created on: 1997-07-28
2 // Created by: Pierre CHALAMET
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Graphic3d_TextureRoot.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_TextureParams.hxx>
21 #include <Image_AlienPixMap.hxx>
22 #include <Image_DDSParser.hxx>
23 #include <Image_SupportedFormats.hxx>
24 #include <OSD_Directory.hxx>
25 #include <OSD_Environment.hxx>
26 #include <OSD_File.hxx>
27 #include <OSD_OpenFile.hxx>
28 #include <OSD_Protection.hxx>
29 #include <Standard_Atomic.hxx>
31 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_TextureRoot, Standard_Transient)
35 static volatile Standard_Integer THE_TEXTURE_COUNTER = 0;
38 // =======================================================================
39 // function : TexturesFolder
41 // =======================================================================
42 TCollection_AsciiString Graphic3d_TextureRoot::TexturesFolder()
44 static Standard_Boolean IsDefined = Standard_False;
45 static TCollection_AsciiString VarName;
48 IsDefined = Standard_True;
49 OSD_Environment aTexDirEnv ("CSF_MDTVTexturesDirectory");
50 VarName = aTexDirEnv.Value();
51 if (VarName.IsEmpty())
53 OSD_Environment aCasRootEnv ("CASROOT");
54 VarName = aCasRootEnv.Value();
55 if (!VarName.IsEmpty())
57 VarName += "/src/Textures";
61 if (VarName.IsEmpty())
64 std::cerr << "Both environment variables CSF_MDTVTexturesDirectory and CASROOT are undefined!\n"
65 << "At least one should be defined to use standard Textures.\n";
67 throw Standard_Failure("CSF_MDTVTexturesDirectory and CASROOT are undefined");
70 const OSD_Path aDirPath (VarName);
71 OSD_Directory aDir (aDirPath);
72 const TCollection_AsciiString aTexture = VarName + "/2d_MatraDatavision.rgb";
73 OSD_File aTextureFile (aTexture);
74 if (!aDir.Exists() || !aTextureFile.Exists())
77 std::cerr << " CSF_MDTVTexturesDirectory or CASROOT not correctly setted\n";
78 std::cerr << " not all files are found in : "<< VarName.ToCString() << std::endl;
80 throw Standard_Failure("CSF_MDTVTexturesDirectory or CASROOT not correctly setted");
86 // =======================================================================
87 // function : Graphic3d_TextureRoot
89 // =======================================================================
90 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const TCollection_AsciiString& theFileName,
91 const Graphic3d_TypeOfTexture theType)
92 : myParams (new Graphic3d_TextureParams()),
102 // =======================================================================
103 // function : Graphic3d_TextureRoot
105 // =======================================================================
106 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const Handle(Image_PixMap)& thePixMap,
107 const Graphic3d_TypeOfTexture theType)
108 : myParams (new Graphic3d_TextureParams()),
109 myPixMap (thePixMap),
118 // =======================================================================
119 // function : ~Graphic3d_TextureRoot
121 // =======================================================================
122 Graphic3d_TextureRoot::~Graphic3d_TextureRoot()
127 // =======================================================================
128 // function : generateId
130 // =======================================================================
131 void Graphic3d_TextureRoot::generateId()
133 myTexId = TCollection_AsciiString ("Graphic3d_TextureRoot_")
134 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_TEXTURE_COUNTER));
137 // =======================================================================
138 // function : GetCompressedImage
140 // =======================================================================
141 Handle(Image_CompressedPixMap) Graphic3d_TextureRoot::GetCompressedImage (const Handle(Image_SupportedFormats)& theSupported)
143 if (!myPixMap.IsNull())
145 return Handle(Image_CompressedPixMap)();
148 // Case 2: texture source is specified as path
149 TCollection_AsciiString aFilePath;
150 myPath.SystemName (aFilePath);
151 if (aFilePath.IsEmpty())
153 return Handle(Image_CompressedPixMap)();
156 TCollection_AsciiString aFilePathLower = aFilePath;
157 aFilePathLower.LowerCase();
158 if (!aFilePathLower.EndsWith (".dds"))
160 // do not waste time on file system access in case of wrong file extension
161 return Handle(Image_CompressedPixMap)();
164 if (Handle(Image_CompressedPixMap) anImage = Image_DDSParser::Load (theSupported, aFilePath, 0))
166 myIsTopDown = anImage->IsTopDown();
169 return Handle(Image_CompressedPixMap)();
172 // =======================================================================
173 // function : GetImage
175 // =======================================================================
176 Handle(Image_PixMap) Graphic3d_TextureRoot::GetImage (const Handle(Image_SupportedFormats)& theSupported)
178 if (Handle(Image_PixMap) anOldImage = GetImage())
180 myIsTopDown = anOldImage->IsTopDown();
181 return anOldImage; // compatibility with old API
184 // Case 1: texture source is specified as pixmap
185 if (!myPixMap.IsNull())
187 myIsTopDown = myPixMap->IsTopDown();
191 // Case 2: texture source is specified as path
192 TCollection_AsciiString aFilePath;
193 myPath.SystemName (aFilePath);
194 if (aFilePath.IsEmpty())
196 return Handle(Image_PixMap)();
199 Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
200 if (anImage->Load (aFilePath))
202 myIsTopDown = anImage->IsTopDown();
203 convertToCompatible (theSupported, anImage);
207 return Handle(Image_PixMap)();
210 // =======================================================================
211 // function : convertToCompatible
213 // =======================================================================
214 void Graphic3d_TextureRoot::convertToCompatible (const Handle(Image_SupportedFormats)& theSupported,
215 const Handle(Image_PixMap)& theImage)
217 if (theSupported.IsNull()
218 || theSupported->IsSupported (theImage->Format())
219 || theImage.IsNull())
224 if ((theImage->Format() == Image_Format_BGR32
225 || theImage->Format() == Image_Format_BGR32))
227 Image_PixMap::SwapRgbaBgra (*theImage);
228 theImage->SetFormat (theImage->Format() == Image_Format_BGR32
230 : Image_Format_RGBA);
234 // =======================================================================
237 // =======================================================================
238 Standard_Boolean Graphic3d_TextureRoot::IsDone() const
240 // Case 1: texture source is specified as pixmap
241 if (!myPixMap.IsNull())
243 return !myPixMap->IsEmpty();
246 // Case 2: texture source is specified as path
247 OSD_File aTextureFile (myPath);
248 return aTextureFile.Exists();