1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2021, assimp team
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 
42 /** @file texture.h
43  *  @brief Defines texture helper structures for the library
44  *
45  * Used for file formats which embed their textures into the model file.
46  * Supported are both normal textures, which are stored as uncompressed
47  * pixels, and "compressed" textures, which are stored in a file format
48  * such as PNG or TGA.
49  */
50 #pragma once
51 #ifndef AI_TEXTURE_H_INC
52 #define AI_TEXTURE_H_INC
53 
54 #ifdef __GNUC__
55 #   pragma GCC system_header
56 #endif
57 
58 #include <assimp/types.h>
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 // --------------------------------------------------------------------------------
65 
66 /** \def AI_EMBEDDED_TEXNAME_PREFIX
67  * \ref AI_MAKE_EMBEDDED_TEXNAME
68  */
69 #ifndef AI_EMBEDDED_TEXNAME_PREFIX
70 #   define AI_EMBEDDED_TEXNAME_PREFIX	"*"
71 #endif
72 
73 /** @def AI_MAKE_EMBEDDED_TEXNAME
74  *  Used to build the reserved path name used by the material system to
75  *  reference textures that are embedded into their corresponding
76  *  model files. The parameter specifies the index of the texture
77  *  (zero-based, in the aiScene::mTextures array)
78  */
79 #if (!defined AI_MAKE_EMBEDDED_TEXNAME)
80 #   define AI_MAKE_EMBEDDED_TEXNAME(_n_) AI_EMBEDDED_TEXNAME_PREFIX # _n_
81 #endif
82 
83 #include "./Compiler/pushpack1.h"
84 
85 // --------------------------------------------------------------------------------
86 /** @brief Helper structure to represent a texel in a ARGB8888 format
87 *
88 *  Used by aiTexture.
89 */
90 struct aiTexel {
91     unsigned char b,g,r,a;
92 
93 #ifdef __cplusplus
94     //! Comparison operator
95     bool operator== (const aiTexel& other) const
96     {
97         return b == other.b && r == other.r &&
98                g == other.g && a == other.a;
99     }
100 
101     //! Inverse comparison operator
102     bool operator!= (const aiTexel& other) const
103     {
104         return b != other.b || r != other.r ||
105                g != other.g || a != other.a;
106     }
107 
108     //! Conversion to a floating-point 4d color
aiColor4DaiTexel109     operator aiColor4D() const
110     {
111         return aiColor4D(r/255.f,g/255.f,b/255.f,a/255.f);
112     }
113 #endif // __cplusplus
114 
115 } PACK_STRUCT;
116 
117 #include "./Compiler/poppack1.h"
118 
119 #define HINTMAXTEXTURELEN 9
120 
121 // --------------------------------------------------------------------------------
122 /** Helper structure to describe an embedded texture
123  *
124  * Normally textures are contained in external files but some file formats embed
125  * them directly in the model file. There are two types of embedded textures:
126  * 1. Uncompressed textures. The color data is given in an uncompressed format.
127  * 2. Compressed textures stored in a file format like png or jpg. The raw file
128  * bytes are given so the application must utilize an image decoder (e.g. DevIL) to
129  * get access to the actual color data.
130  *
131  * Embedded textures are referenced from materials using strings like "*0", "*1", etc.
132  * as the texture paths (a single asterisk character followed by the
133  * zero-based index of the texture in the aiScene::mTextures array).
134  */
135 struct aiTexture {
136     /** Width of the texture, in pixels
137      *
138      * If mHeight is zero the texture is compressed in a format
139      * like JPEG. In this case mWidth specifies the size of the
140      * memory area pcData is pointing to, in bytes.
141      */
142     unsigned int mWidth;
143 
144     /** Height of the texture, in pixels
145      *
146      * If this value is zero, pcData points to an compressed texture
147      * in any format (e.g. JPEG).
148      */
149     unsigned int mHeight;
150 
151     /** A hint from the loader to make it easier for applications
152      *  to determine the type of embedded textures.
153      *
154      * If mHeight != 0 this member is show how data is packed. Hint will consist of
155      * two parts: channel order and channel bitness (count of the bits for every
156      * color channel). For simple parsing by the viewer it's better to not omit
157      * absent color channel and just use 0 for bitness. For example:
158      * 1. Image contain RGBA and 8 bit per channel, achFormatHint == "rgba8888";
159      * 2. Image contain ARGB and 8 bit per channel, achFormatHint == "argb8888";
160      * 3. Image contain RGB and 5 bit for R and B channels and 6 bit for G channel, achFormatHint == "rgba5650";
161      * 4. One color image with B channel and 1 bit for it, achFormatHint == "rgba0010";
162      * If mHeight == 0 then achFormatHint is set set to '\\0\\0\\0\\0' if the loader has no additional
163      * information about the texture file format used OR the
164      * file extension of the format without a trailing dot. If there
165      * are multiple file extensions for a format, the shortest
166      * extension is chosen (JPEG maps to 'jpg', not to 'jpeg').
167      * E.g. 'dds\\0', 'pcx\\0', 'jpg\\0'.  All characters are lower-case.
168      * The fourth character will always be '\\0'.
169      */
170     char achFormatHint[ HINTMAXTEXTURELEN ];// 8 for string + 1 for terminator.
171 
172     /** Data of the texture.
173      *
174      * Points to an array of mWidth * mHeight aiTexel's.
175      * The format of the texture data is always ARGB8888 to
176      * make the implementation for user of the library as easy
177      * as possible. If mHeight = 0 this is a pointer to a memory
178      * buffer of size mWidth containing the compressed texture
179      * data. Good luck, have fun!
180      */
181     C_STRUCT aiTexel* pcData;
182 
183     /** Texture original filename
184     *
185     * Used to get the texture reference
186     */
187     C_STRUCT aiString mFilename;
188 
189 #ifdef __cplusplus
190 
191     //! For compressed textures (mHeight == 0): compare the
192     //! format hint against a given string.
193     //! @param s Input string. 3 characters are maximally processed.
194     //!        Example values: "jpg", "png"
195     //! @return true if the given string matches the format hint
CheckFormataiTexture196     bool CheckFormat(const char* s) const {
197         if (nullptr == s) {
198             return false;
199         }
200 
201 		return (0 == ::strncmp(achFormatHint, s, sizeof(achFormatHint)));
202     }
203 
204     // Construction
aiTextureaiTexture205     aiTexture() AI_NO_EXCEPT
206     : mWidth(0)
207     , mHeight(0)
208     , pcData(nullptr)
209     , mFilename() {
210         memset(achFormatHint, 0, sizeof(achFormatHint));
211     }
212 
213     // Destruction
~aiTextureaiTexture214     ~aiTexture () {
215         delete[] pcData;
216     }
217 #endif
218 };
219 
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif // AI_TEXTURE_H_INC
226