Debian Codegraph @

thirdparty/libktx/lib/checkheader.c

raw symbols SCIP
1 /* -*- tab-width: 4; -*- */
2 /* vi: set sw=2 ts=4 expandtab: */
3
4 /* $Id$ */
5
6 /*
7 * Copyright 2010-2020 The Khronos Group Inc.
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 /**
12 * @internal
13 * @file
14 * @~English
15 *
16 * @brief Function to verify a KTX file header
17 *
18 * @author Mark Callow, HI Corporation
19 */
20
21 /*
22 * Author: Georg Kolling, Imagination Technology with modifications
23 * by Mark Callow, HI Corporation.
24 */
25 #include <assert.h>
26 #include <string.h>
27
28 #include "ktx.h"
29 #include "ktxint.h"
30 #include "vkformat_enum.h"
31
32 bool isProhibitedFormat(VkFormat format);
33 bool isValidFormat(VkFormat format);
34
35 /**
36 * @internal
37 * @~English
38 * @brief Check a KTX file header.
39 *
40 * As well as checking that the header identifies a KTX file, the function
41 * sanity checks the values and returns information about the texture in a
42 * struct KTX_supplementary_info.
43 *
44 * @param pHeader pointer to the KTX header to check
45 * @param pSuppInfo pointer to a KTX_supplementary_info structure in which to
46 * return information about the texture.
47 *
48 * @author Georg Kolling, Imagination Technology
49 * @author Mark Callow, HI Corporation
50 */
51
52 KTX_error_code ktxCheckHeader1_(KTX_header* pHeader,
53 KTX_supplemental_info* pSuppInfo)
54 {
55 ktx_uint8_t identifier_reference[12] = KTX_IDENTIFIER_REF;
56 ktx_uint32_t max_dim;
57
58 assert(pHeader != NULL && pSuppInfo != NULL);
59
60 /* Compare identifier, is this a KTX file? */
61 if (memcmp(pHeader->identifier, identifier_reference, 12) != 0)
62 {
63 return KTX_UNKNOWN_FILE_FORMAT;
64 }
65
66 if (pHeader->endianness == KTX_ENDIAN_REF_REV)
67 {
68 /* Convert endianness of pHeader fields. */
69 _ktxSwapEndian32(&pHeader->glType, 12);
70
71 if (pHeader->glTypeSize != 1 &&
72 pHeader->glTypeSize != 2 &&
73 pHeader->glTypeSize != 4)
74 {
75 /* Only 8-, 16-, and 32-bit types supported so far. */
76 return KTX_FILE_DATA_ERROR;
77 }
78 }
79 else if (pHeader->endianness != KTX_ENDIAN_REF)
80 {
81 return KTX_FILE_DATA_ERROR;
82 }
83
84 /* Check glType and glFormat */
85 pSuppInfo->compressed = 0;
86 if (pHeader->glType == 0 || pHeader->glFormat == 0)
87 {
88 if (pHeader->glType + pHeader->glFormat != 0)
89 {
90 /* either both or none of glType, glFormat must be zero */
91 return KTX_FILE_DATA_ERROR;
92 }
93 pSuppInfo->compressed = 1;
94 }
95
96 if (pHeader->glFormat == pHeader->glInternalformat) {
97 // glInternalFormat is either unsized (which is no longer and should
98 // never have been supported by libktx) or glFormat is sized.
99 return KTX_FILE_DATA_ERROR;
100 }
101
102 /* Check texture dimensions. KTX files can store 8 types of textures:
103 1D, 2D, 3D, cube, and array variants of these. There is currently
104 no GL extension for 3D array textures. */
105 if ((pHeader->pixelWidth == 0) ||
106 (pHeader->pixelDepth > 0 && pHeader->pixelHeight == 0))
107 {
108 /* texture must have width */
109 /* texture must have height if it has depth */
110 return KTX_FILE_DATA_ERROR;
111 }
112
113
114 if (pHeader->pixelDepth > 0)
115 {
116 if (pHeader->numberOfArrayElements > 0)
117 {
118 /* No 3D array textures yet. */
119 return KTX_UNSUPPORTED_FEATURE;
120 }
121 pSuppInfo->textureDimension = 3;
122 }
123 else if (pHeader->pixelHeight > 0)
124 {
125 pSuppInfo->textureDimension = 2;
126 }
127 else
128 {
129 pSuppInfo->textureDimension = 1;
130 }
131
132 if (pHeader->numberOfFaces == 6)
133 {
134 if (pSuppInfo->textureDimension != 2)
135 {
136 /* cube map needs 2D faces */
137 return KTX_FILE_DATA_ERROR;
138 }
139 }
140 else if (pHeader->numberOfFaces != 1)
141 {
142 /* numberOfFaces must be either 1 or 6 */
143 return KTX_FILE_DATA_ERROR;
144 }
145
146 /* Check number of mipmap levels */
147 if (pHeader->numberOfMipLevels == 0)
148 {
149 pSuppInfo->generateMipmaps = 1;
150 pHeader->numberOfMipLevels = 1;
151 }
152 else
153 {
154 pSuppInfo->generateMipmaps = 0;
155 }
156
157 /* This test works for arrays too because height or depth will be 0. */
158 max_dim = MAX(MAX(pHeader->pixelWidth, pHeader->pixelHeight), pHeader->pixelDepth);
159 if (max_dim < ((ktx_uint32_t)1 << (pHeader->numberOfMipLevels - 1)))
160 {
161 /* Can't have more mip levels than 1 + log2(max(width, height, depth)) */
162 return KTX_FILE_DATA_ERROR;
163 }
164
165 return KTX_SUCCESS;
166 }
167
168 /**
169 * @internal
170 * @~English
171 * @brief Check a KTX2 file header.
172 *
173 * As well as checking that the header identifies a KTX 2 file, the function
174 * sanity checks the values and returns information about the texture in a
175 * struct KTX_supplementary_info.
176 *
177 * @param pHeader pointer to the KTX header to check
178 * @param pSuppInfo pointer to a KTX_supplementary_info structure in which to
179 * return information about the texture.
180 *
181 * @author Mark Callow, HI Corporation
182 */
183 KTX_error_code ktxCheckHeader2_(KTX_header2* pHeader,
184 KTX_supplemental_info* pSuppInfo)
185 {
186 // supp info is compressed, generateMipmaps and num dimensions. Don't need
187 // compressed as formatSize gives us that. I think the other 2 aren't needed.
188 ktx_uint8_t identifier_reference[12] = KTX2_IDENTIFIER_REF;
189
190 assert(pHeader != NULL && pSuppInfo != NULL);
191 ktx_uint32_t max_dim;
192
193 /* Compare identifier, is this a KTX file? */
194 if (memcmp(pHeader->identifier, identifier_reference, 12) != 0)
195 {
196 return KTX_UNKNOWN_FILE_FORMAT;
197 }
198
199 /* Check format */
200 if (isProhibitedFormat(pHeader->vkFormat))
201 {
202 return KTX_FILE_DATA_ERROR;
203 }
204 if (!isValidFormat(pHeader->vkFormat))
205 {
206 return KTX_UNSUPPORTED_FEATURE;
207 }
208 if (pHeader->supercompressionScheme == KTX_SS_BASIS_LZ && pHeader->vkFormat != VK_FORMAT_UNDEFINED)
209 {
210 return KTX_FILE_DATA_ERROR;
211 }
212
213 /* Check texture dimensions. KTX files can store 8 types of textures:
214 1D, 2D, 3D, cube, and array variants of these. There is currently
215 no extension for 3D array textures in any 3D API. */
216 if ((pHeader->pixelWidth == 0) ||
217 (pHeader->pixelDepth > 0 && pHeader->pixelHeight == 0))
218 {
219 /* texture must have width */
220 /* texture must have height if it has depth */
221 return KTX_FILE_DATA_ERROR;
222 }
223
224 if (pHeader->pixelDepth > 0)
225 {
226 if (pHeader->layerCount > 0)
227 {
228 /* No 3D array textures yet. */
229 return KTX_UNSUPPORTED_FEATURE;
230 }
231 pSuppInfo->textureDimension = 3;
232 }
233 else if (pHeader->pixelHeight > 0)
234 {
235 pSuppInfo->textureDimension = 2;
236 }
237 else
238 {
239 pSuppInfo->textureDimension = 1;
240 }
241
242 if (pHeader->faceCount == 6)
243 {
244 if (pSuppInfo->textureDimension != 2)
245 {
246 /* cube map needs 2D faces */
247 return KTX_FILE_DATA_ERROR;
248 }
249 if (pHeader->pixelDepth != 0)
250 {
251 /* cube map cannot have depth */
252 return KTX_FILE_DATA_ERROR;
253 }
254 if (pHeader->pixelWidth != pHeader->pixelHeight)
255 {
256 /* cube map needs square faces */
257 return KTX_FILE_DATA_ERROR;
258 }
259 }
260 else if (pHeader->faceCount != 1)
261 {
262 /* numberOfFaces must be either 1 or 6 */
263 return KTX_FILE_DATA_ERROR;
264 }
265
266 // Check number of mipmap levels
267 if (pHeader->levelCount == 0)
268 {
269 pSuppInfo->generateMipmaps = 1;
270 pHeader->levelCount = 1;
271 }
272 else
273 {
274 pSuppInfo->generateMipmaps = 0;
275 }
276
277 // Check supercompression
278 switch (pHeader->supercompressionScheme) {
279 case KTX_SS_NONE:
280 case KTX_SS_BASIS_LZ:
281 case KTX_SS_ZSTD:
282 case KTX_SS_ZLIB:
283 break;
284 default:
285 // Unsupported supercompression
286 return KTX_UNSUPPORTED_FEATURE;
287 }
288
289 // This test works for arrays too because height or depth will be 0.
290 max_dim = MAX(MAX(pHeader->pixelWidth, pHeader->pixelHeight), pHeader->pixelDepth);
291 if (max_dim < ((ktx_uint32_t)1 << (pHeader->levelCount - 1)))
292 {
293 // Can't have more mip levels than 1 + log2(max(width, height, depth))
294 return KTX_FILE_DATA_ERROR;
295 }
296
297 return KTX_SUCCESS;
298
299 }