• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0
2 // ----------------------------------------------------------------------------
3 // Copyright 2011-2021 Arm Limited
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 // use this file except in compliance with the License. You may obtain a copy
7 // of the License at:
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16 // ----------------------------------------------------------------------------
17 
18 /**
19  * @brief Functions and data declarations.
20  */
21 
22 #ifndef ASTCENCCLI_INTERNAL_INCLUDED
23 #define ASTCENCCLI_INTERNAL_INCLUDED
24 
25 #include <cstddef>
26 #include <cstdint>
27 #include <cstdio>
28 #include <cstdlib>
29 
30 #include "astcenc.h"
31 #include "astcenc_mathlib.h"
32 
33 /**
34  * @brief The payload stored in a compressed ASTC image.
35  */
36 struct astc_compressed_image
37 {
38 	/** @brief The block width in texels. */
39 	unsigned int block_x;
40 
41 	/** @brief The block height in texels. */
42 	unsigned int block_y;
43 
44 	/** @brief The block depth in texels. */
45 	unsigned int block_z;
46 
47 	/** @brief The image width in texels. */
48 	unsigned int dim_x;
49 
50 	/** @brief The image height in texels. */
51 	unsigned int dim_y;
52 
53 	/** @brief The image depth in texels. */
54 	unsigned int dim_z;
55 
56 	/** @brief The binary data payload. */
57 	uint8_t* data;
58 
59 	/** @brief The binary data length in bytes. */
60 	size_t data_len;
61 };
62 
63 /**
64  * @brief Config options that have been read from command line.
65  */
66 struct cli_config_options
67 {
68 	/** @brief The number of threads to use for processing. */
69 	unsigned int thread_count;
70 
71 	/** @brief The number of repeats to execute for benchmarking. */
72 	unsigned int repeat_count;
73 
74 	/** @brief The number of image slices to load for a 3D image. */
75 	unsigned int array_size;
76 
77 	/** @brief @c true if running in silent mode with minimal output. */
78 	bool silentmode;
79 
80 	/** @brief @c true if the images should be y-flipped. */
81 	bool y_flip;
82 
83 	/** @brief @c true if diagnostic images should be stored. */
84 	bool diagnostic_images;
85 
86 	/** @brief The low exposure fstop for error computation. */
87 	int low_fstop;
88 
89 	/** @brief The high exposure fstop for error computation. */
90 	int high_fstop;
91 
92 	/** @brief The  pre-encode swizzle. */
93 	astcenc_swizzle swz_encode;
94 
95 	/** @brief The  post-decode swizzle. */
96 	astcenc_swizzle swz_decode;
97 };
98 
99 /**
100  * @brief Load uncompressed image.
101  *
102  * @param filename               The file path on disk.
103  * @param y_flip                 Should this image be Y flipped?
104  * @param[out] is_hdr            Is the loaded image HDR?
105  * @param[out] component_count   The number of components in the loaded image.
106  *
107  * @return The astc image file, or nullptr on error.
108  */
109 astcenc_image* load_ncimage(
110 	const char* filename,
111 	bool y_flip,
112 	bool& is_hdr,
113 	unsigned int& component_count);
114 
115 /**
116  * @brief Load uncompressed PNG image.
117  *
118  * @param filename               The file path on disk.
119  * @param y_flip                 Should this image be Y flipped?
120  * @param[out] is_hdr            Is the loaded image HDR?
121  * @param[out] component_count   The number of components in the loaded image.
122  *
123  * @return The astc image file, or nullptr on error.
124  */
125 astcenc_image* load_png_with_wuffs(
126 	const char* filename,
127 	bool y_flip,
128 	bool& is_hdr,
129 	unsigned int& component_count);
130 
131 /**
132  * @brief Save an uncompressed image.
133  *
134  * @param img        The source data for the image.
135  * @param filename   The name of the file to save.
136  * @param y_flip     Should the image be vertically flipped?
137  *
138  * @return @c true if the image saved OK, @c false on error.
139  */
140 bool store_ncimage(
141 	const astcenc_image* img,
142 	const char* filename,
143 	int y_flip);
144 
145 /**
146  * @brief Check if the output file type requires a specific bitness.
147  *
148  * @param filename The file name, containing hte extension to check.
149  *
150  * @return Valid values are:
151  *     * -1 - error - unknown file type.
152  *     *  0 - no enforced bitness.
153  *     *  8 - enforced 8-bit UNORM.
154  *     * 16 - enforced 16-bit FP16.
155  */
156 int get_output_filename_enforced_bitness(
157 	const char* filename);
158 
159 /**
160  * @brief Allocate a new image in a canonical format.
161  *
162  * Allocated images must be freed with a @c free_image() call.
163  *
164  * @param bitness   The number of bits per component (8, 16, or 32).
165  * @param dim_x     The width of the image, in texels.
166  * @param dim_y     The height of the image, in texels.
167  * @param dim_z     The depth of the image, in texels.
168  *
169  * @return The allocated image, or @c nullptr on error.
170  */
171 astcenc_image* alloc_image(
172 	unsigned int bitness,
173 	unsigned int dim_x,
174 	unsigned int dim_y,
175 	unsigned int dim_z);
176 
177 /**
178  * @brief Free an image.
179  *
180  * @param img   The image to free.
181  */
182 void free_image(
183 	astcenc_image* img);
184 
185 /**
186  * @brief Determine the number of active components in an image.
187  *
188  * @param img   The image to analyze.
189  *
190  * @return The number of active components in the image.
191  */
192 int determine_image_components(
193 	const astcenc_image* img);
194 
195 /**
196  * @brief Load a compressed .astc image.
197  *
198  * @param filename   The file to load.
199  * @param img        The image to populate with loaded data.
200  *
201  * @return Non-zero on error, zero on success.
202  */
203 int load_cimage(
204 	const char* filename,
205 	astc_compressed_image& img);
206 
207 /**
208  * @brief Store a compressed .astc image.
209  *
210  * @param img        The image to store.
211  * @param filename   The file to save.
212  *
213  * @return Non-zero on error, zero on success.
214  */
215 int store_cimage(
216 	const astc_compressed_image& img,
217 	const char* filename);
218 
219 /**
220  * @brief Load a compressed .ktx image.
221  *
222  * @param filename   The file to load.
223  * @param is_srgb    Is this an sRGB encoded file?
224  * @param img        The image to populate with loaded data.
225  *
226  * @return Non-zero on error, zero on success.
227  */
228 bool load_ktx_compressed_image(
229 	const char* filename,
230 	bool& is_srgb,
231 	astc_compressed_image& img) ;
232 
233 /**
234  * @brief Store a compressed .ktx image.
235  *
236  * @param img        The image to store.
237  * @param filename   The file to store.
238  * @param is_srgb    Is this an sRGB encoded file?
239  *
240  * @return Non-zero on error, zero on success.
241  */
242 bool store_ktx_compressed_image(
243 	const astc_compressed_image& img,
244 	const char* filename,
245 	bool is_srgb);
246 
247 /**
248  * @brief Create an image from a 2D float data array.
249  *
250  * @param data     The raw input data.
251  * @param dim_x    The width of the image, in texels.
252  * @param dim_y    The height of the image, in texels.
253  * @param y_flip   Should this image be vertically flipped?
254  *
255  * @return The populated image.
256  */
257 astcenc_image* astc_img_from_floatx4_array(
258 	const float* data,
259 	unsigned int dim_x,
260 	unsigned int dim_y,
261 	bool y_flip);
262 
263 /**
264  * @brief Create an image from a 2D byte data array.
265  *
266  * @param data     The raw input data.
267  * @param dim_x    The width of the image, in texels.
268  * @param dim_y    The height of the image, in texels.
269  * @param y_flip   Should this image be vertically flipped?
270  *
271  * @return The populated image.
272  */
273 astcenc_image* astc_img_from_unorm8x4_array(
274 	const uint8_t* data,
275 	unsigned int dim_x,
276 	unsigned int dim_y,
277 	bool y_flip);
278 
279 /**
280  * @brief Create a flattened RGBA FLOAT32 data array from an image structure.
281  *
282  * The returned data array is allocated with @c new[] and must be freed with a @c delete[] call.
283  *
284  * @param img      The input image.
285  * @param y_flip   Should the data in the array be Y flipped?
286  *
287  * @return The data array.
288  */
289 float* floatx4_array_from_astc_img(
290 	const astcenc_image* img,
291 	bool y_flip);
292 
293 /**
294  * @brief Create a flattened RGBA UNORM8 data array from an image structure.
295  *
296  * The returned data array is allocated with @c new[] and must be freed with a @c delete[] call.
297  *
298  * @param img      The input image.
299  * @param y_flip   Should the data in the array be Y flipped?
300  *
301  * @return The data array.
302  */
303 uint8_t* unorm8x4_array_from_astc_img(
304 	const astcenc_image* img,
305 	bool y_flip);
306 
307 /* ============================================================================
308   Functions for printing build info and help messages
309 ============================================================================ */
310 
311 /**
312  * @brief Print the tool copyright and version header to stdout.
313  */
314 void astcenc_print_header();
315 
316 /**
317  * @brief Print the tool copyright, version, and short-form help to stdout.
318  */
319 void astcenc_print_shorthelp();
320 
321 /**
322  * @brief Print the tool copyright, version, and long-form help to stdout.
323  */
324 void astcenc_print_longhelp();
325 
326 /**
327  * @brief Compute error metrics comparing two images.
328  *
329  * @param compute_hdr_metrics      True if HDR metrics should be computed.
330  * @param compute_normal_metrics   True if normal map metrics should be computed.
331  * @param input_components         The number of input color components.
332  * @param img1                     The original image.
333  * @param img2                     The compressed image.
334  * @param fstop_lo                 The low exposure fstop (HDR only).
335  * @param fstop_hi                 The high exposure fstop (HDR only).
336  */
337 void compute_error_metrics(
338 	bool compute_hdr_metrics,
339 	bool compute_normal_metrics,
340 	int input_components,
341 	const astcenc_image* img1,
342 	const astcenc_image* img2,
343 	int fstop_lo,
344 	int fstop_hi);
345 
346 /**
347  * @brief Get the current time.
348  *
349  * @return The current time in seconds since arbitrary epoch.
350  */
351 double get_time();
352 
353 /**
354  * @brief Get the number of CPU cores.
355  *
356  * @return The number of online or onlineable CPU cores in the system.
357  */
358 int get_cpu_count();
359 
360 /**
361  * @brief Launch N worker threads and wait for them to complete.
362  *
363  * All threads run the same thread function, and have the same thread payload, but are given a
364  * unique thread ID (0 .. N-1) as a parameter to the run function to allow thread-specific behavior.
365  *
366 |* @param thread_count The number of threads to spawn.
367  * @param func         The function to execute. Must have the signature:
368  *                     void (int thread_count, int thread_id, void* payload)
369  * @param payload      Pointer to an opaque thread payload object.
370  */
371 void launch_threads(
372 	int thread_count,
373 	void (*func)(int, int, void*),
374 	void *payload);
375 
376 #endif
377