0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Image / Image_Texture.hxx
1 // Author: Kirill Gavrilov
2 // Copyright (c) 2015-2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _Image_Texture_HeaderFile
16 #define _Image_Texture_HeaderFile
17
18 #include <NCollection_Buffer.hxx>
19 #include <TCollection_AsciiString.hxx>
20
21 class Image_CompressedPixMap;
22 class Image_SupportedFormats;
23 class Image_PixMap;
24
25 //! Texture image definition.
26 //! The image can be stored as path to image file, as file path with the given offset and as a data buffer of encoded image.
27 class Image_Texture : public Standard_Transient
28 {
29   DEFINE_STANDARD_RTTIEXT(Image_Texture, Standard_Transient)
30 public:
31
32   //! Constructor pointing to file location.
33   Standard_EXPORT explicit Image_Texture (const TCollection_AsciiString& theFileName);
34
35   //! Constructor pointing to file part.
36   Standard_EXPORT explicit Image_Texture (const TCollection_AsciiString& theFileName,
37                                           int64_t theOffset,
38                                           int64_t theLength);
39
40   //! Constructor pointing to buffer.
41   Standard_EXPORT explicit Image_Texture (const Handle(NCollection_Buffer)& theBuffer,
42                                           const TCollection_AsciiString& theId);
43
44   //! Return generated texture id.
45   const TCollection_AsciiString& TextureId() const { return myTextureId; }
46
47   //! Return image file path.
48   const TCollection_AsciiString& FilePath() const { return myImagePath; }
49
50   //! Return offset within file.
51   int64_t FileOffset() const { return myOffset; }
52
53   //! Return length of image data within the file after offset.
54   int64_t FileLength() const { return myLength; }
55
56   //! Return buffer holding encoded image content.
57   const Handle(NCollection_Buffer)& DataBuffer() const { return myBuffer; }
58
59   //! Return image file format.
60   Standard_EXPORT TCollection_AsciiString ProbeImageFileFormat() const;
61
62   //! Image reader without decoding data for formats supported natively by GPUs.
63   Standard_EXPORT virtual Handle(Image_CompressedPixMap) ReadCompressedImage (const Handle(Image_SupportedFormats)& theSupported) const;
64
65   //! Image reader.
66   Standard_EXPORT virtual Handle(Image_PixMap) ReadImage (const Handle(Image_SupportedFormats)& theSupported) const;
67
68   //! Write image to specified file without decoding data.
69   Standard_EXPORT virtual Standard_Boolean WriteImage (const TCollection_AsciiString& theFile);
70
71 public: //! @name hasher interface
72
73   //! Hash value, for Map interface.
74   static int HashCode (const Handle(Image_Texture)& theTexture, const int theUpper)
75   {
76     return !theTexture.IsNull()
77           ? TCollection_AsciiString::HashCode (theTexture->myTextureId, theUpper)
78           : 0;
79   }
80
81   //! Matching two instances, for Map interface.
82   static Standard_Boolean IsEqual (const Handle(Image_Texture)& theTex1,
83                                    const Handle(Image_Texture)& theTex2)
84   {
85     if (theTex1.IsNull() != theTex2.IsNull())
86     {
87       return Standard_False;
88     }
89     else if (theTex1.IsNull())
90     {
91       return Standard_True;
92     }
93     return theTex1->myTextureId.IsEqual (theTex2->myTextureId);
94   }
95   
96   //! Dumps the content of me into the stream
97   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
98
99 protected:
100
101   //! Read image from normal image file.
102   Standard_EXPORT virtual Handle(Image_PixMap) loadImageFile (const TCollection_AsciiString& thePath) const;
103
104   //! Read image from file with some offset.
105   Standard_EXPORT virtual Handle(Image_PixMap) loadImageOffset (const TCollection_AsciiString& thePath,
106                                                                 int64_t theOffset,
107                                                                 int64_t theLength) const;
108
109   //! Read image from buffer.
110   Standard_EXPORT virtual Handle(Image_PixMap) loadImageBuffer (const Handle(NCollection_Buffer)& theBuffer,
111                                                                 const TCollection_AsciiString& theId) const;
112
113 protected:
114
115   TCollection_AsciiString myTextureId; //!< generated texture id
116   TCollection_AsciiString myImagePath; //!< image file path
117   Handle(NCollection_Buffer) myBuffer; //!< image buffer
118   int64_t                    myOffset; //!< offset within file
119   int64_t                    myLength; //!< length within file
120
121 };
122
123 #endif // _Image_Texture_HeaderFile