688f0e11e2cf41cf83949615121447b22e370f2c
[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_PixMap;
22
23 //! Texture image definition.
24 //! 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.
25 class Image_Texture : public Standard_Transient
26 {
27   DEFINE_STANDARD_RTTIEXT(Image_Texture, Standard_Transient)
28 public:
29
30   //! Constructor pointing to file location.
31   Standard_EXPORT explicit Image_Texture (const TCollection_AsciiString& theFileName);
32
33   //! Constructor pointing to file part.
34   Standard_EXPORT explicit Image_Texture (const TCollection_AsciiString& theFileName,
35                                           int64_t theOffset,
36                                           int64_t theLength);
37
38   //! Constructor pointing to buffer.
39   Standard_EXPORT explicit Image_Texture (const Handle(NCollection_Buffer)& theBuffer,
40                                           const TCollection_AsciiString& theId);
41
42   //! Return generated texture id.
43   const TCollection_AsciiString& TextureId() const { return myTextureId; }
44
45   //! Return image file path.
46   const TCollection_AsciiString& FilePath() const { return myImagePath; }
47
48   //! Return offset within file.
49   int64_t FileOffset() const { return myOffset; }
50
51   //! Return length of image data within the file after offset.
52   int64_t FileLength() const { return myLength; }
53
54   //! Return buffer holding encoded image content.
55   const Handle(NCollection_Buffer)& DataBuffer() const { return myBuffer; }
56
57   //! Return image file format.
58   Standard_EXPORT TCollection_AsciiString ProbeImageFileFormat() const;
59
60   //! Image reader.
61   Standard_EXPORT virtual Handle(Image_PixMap) ReadImage() const;
62
63   //! Write image to specified file without decoding data.
64   Standard_EXPORT virtual Standard_Boolean WriteImage (const TCollection_AsciiString& theFile);
65
66 public: //! @name hasher interface
67
68   //! Hash value, for Map interface.
69   static int HashCode (const Handle(Image_Texture)& theTexture, const int theUpper)
70   {
71     return !theTexture.IsNull()
72           ? TCollection_AsciiString::HashCode (theTexture->myTextureId, theUpper)
73           : 0;
74   }
75
76   //! Matching two instances, for Map interface.
77   static Standard_Boolean IsEqual (const Handle(Image_Texture)& theTex1,
78                                    const Handle(Image_Texture)& theTex2)
79   {
80     if (theTex1.IsNull() != theTex2.IsNull())
81     {
82       return Standard_False;
83     }
84     else if (theTex1.IsNull())
85     {
86       return Standard_True;
87     }
88     return theTex1->myTextureId.IsEqual (theTex2->myTextureId);
89   }
90   
91   //! Dumps the content of me into the stream
92   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
93
94 protected:
95
96   //! Read image from normal image file.
97   Standard_EXPORT virtual Handle(Image_PixMap) loadImageFile (const TCollection_AsciiString& thePath) const;
98
99   //! Read image from file with some offset.
100   Standard_EXPORT virtual Handle(Image_PixMap) loadImageOffset (const TCollection_AsciiString& thePath,
101                                                                 int64_t theOffset,
102                                                                 int64_t theLength) const;
103
104   //! Read image from buffer.
105   Standard_EXPORT virtual Handle(Image_PixMap) loadImageBuffer (const Handle(NCollection_Buffer)& theBuffer,
106                                                                 const TCollection_AsciiString& theId) const;
107
108 protected:
109
110   TCollection_AsciiString myTextureId; //!< generated texture id
111   TCollection_AsciiString myImagePath; //!< image file path
112   Handle(NCollection_Buffer) myBuffer; //!< image buffer
113   int64_t                    myOffset; //!< offset within file
114   int64_t                    myLength; //!< length within file
115
116 };
117
118 #endif // _Image_Texture_HeaderFile