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 image slices to load for a 3D image. */ 72 unsigned int array_size; 73 74 /** @brief @c true if running in silent mode with minimal output. */ 75 bool silentmode; 76 77 /** @brief @c true if the images should be y-flipped. */ 78 bool y_flip; 79 80 /** @brief The low exposure fstop for error computation. */ 81 int low_fstop; 82 83 /** @brief The high exposure fstop for error computation. */ 84 int high_fstop; 85 86 /** @brief The pre-encode swizzle. */ 87 astcenc_swizzle swz_encode; 88 89 /** @brief The post-decode swizzle. */ 90 astcenc_swizzle swz_decode; 91 }; 92 93 /** 94 * @brief Load uncompressed image. 95 * 96 * @param filename The file path on disk. 97 * @param y_flip Should this image be Y flipped? 98 * @param[out] is_hdr Is the loaded image HDR? 99 * @param[out] component_count The number of components in the loaded image. 100 * 101 * @return The astc image file, or nullptr on error. 102 */ 103 astcenc_image* load_ncimage( 104 const char* filename, 105 bool y_flip, 106 bool& is_hdr, 107 unsigned int& component_count); 108 109 /** 110 * @brief Load uncompressed PNG image. 111 * 112 * @param filename The file path on disk. 113 * @param y_flip Should this image be Y flipped? 114 * @param[out] is_hdr Is the loaded image HDR? 115 * @param[out] component_count The number of components in the loaded image. 116 * 117 * @return The astc image file, or nullptr on error. 118 */ 119 astcenc_image* load_png_with_wuffs( 120 const char* filename, 121 bool y_flip, 122 bool& is_hdr, 123 unsigned int& component_count); 124 125 /** 126 * @brief Save an uncompressed image. 127 * 128 * @param img The source data for the image. 129 * @param filename The name of the file to save. 130 * @param y_flip Should the image be vertically flipped? 131 * 132 * @return @c true if the image saved OK, @c false on error. 133 */ 134 bool store_ncimage( 135 const astcenc_image* img, 136 const char* filename, 137 int y_flip); 138 139 /** 140 * @brief Check if the output file type requires a specific bitness. 141 * 142 * @param filename The file name, containing hte extension to check. 143 * 144 * @return Valid values are: 145 * * -1 - error - unknown file type. 146 * * 0 - no enforced bitness. 147 * * 8 - enforced 8-bit UNORM. 148 * * 16 - enforced 16-bit FP16. 149 */ 150 int get_output_filename_enforced_bitness( 151 const char* filename); 152 153 /** 154 * @brief Allocate a new image in a canonical format. 155 * 156 * Allocated images must be freed with a @c free_image() call. 157 * 158 * @param bitness The number of bits per component (8, 16, or 32). 159 * @param dim_x The width of the image, in texels. 160 * @param dim_y The height of the image, in texels. 161 * @param dim_z The depth of the image, in texels. 162 * 163 * @return The allocated image, or @c nullptr on error. 164 */ 165 astcenc_image* alloc_image( 166 unsigned int bitness, 167 unsigned int dim_x, 168 unsigned int dim_y, 169 unsigned int dim_z); 170 171 /** 172 * @brief Free an image. 173 * 174 * @param img The image to free. 175 */ 176 void free_image( 177 astcenc_image* img); 178 179 /** 180 * @brief Determine the number of active components in an image. 181 * 182 * @param img The image to analyze. 183 * 184 * @return The number of active components in the image. 185 */ 186 int determine_image_components( 187 const astcenc_image* img); 188 189 /** 190 * @brief Load a compressed .astc image. 191 * 192 * @param filename The file to load. 193 * @param img The image to populate with loaded data. 194 * 195 * @return Non-zero on error, zero on success. 196 */ 197 int load_cimage( 198 const char* filename, 199 astc_compressed_image& img); 200 201 /** 202 * @brief Store a compressed .astc image. 203 * 204 * @param img The image to store. 205 * @param filename The file to save. 206 * 207 * @return Non-zero on error, zero on success. 208 */ 209 int store_cimage( 210 const astc_compressed_image& img, 211 const char* filename); 212 213 /** 214 * @brief Load a compressed .ktx image. 215 * 216 * @param filename The file to load. 217 * @param is_srgb Is this an sRGB encoded file? 218 * @param img The image to populate with loaded data. 219 * 220 * @return Non-zero on error, zero on success. 221 */ 222 bool load_ktx_compressed_image( 223 const char* filename, 224 bool& is_srgb, 225 astc_compressed_image& img) ; 226 227 /** 228 * @brief Store a compressed .ktx image. 229 * 230 * @param img The image to store. 231 * @param filename The file to store. 232 * @param is_srgb Is this an sRGB encoded file? 233 * 234 * @return Non-zero on error, zero on success. 235 */ 236 bool store_ktx_compressed_image( 237 const astc_compressed_image& img, 238 const char* filename, 239 bool is_srgb); 240 241 /** 242 * @brief Create an image from a 2D float data array. 243 * 244 * @param data The raw input data. 245 * @param dim_x The width of the image, in texels. 246 * @param dim_y The height of the image, in texels. 247 * @param y_flip Should this image be vertically flipped? 248 * 249 * @return The populated image. 250 */ 251 astcenc_image* astc_img_from_floatx4_array( 252 const float* data, 253 unsigned int dim_x, 254 unsigned int dim_y, 255 bool y_flip); 256 257 /** 258 * @brief Create an image from a 2D byte data array. 259 * 260 * @param data The raw input data. 261 * @param dim_x The width of the image, in texels. 262 * @param dim_y The height of the image, in texels. 263 * @param y_flip Should this image be vertically flipped? 264 * 265 * @return The populated image. 266 */ 267 astcenc_image* astc_img_from_unorm8x4_array( 268 const uint8_t* data, 269 unsigned int dim_x, 270 unsigned int dim_y, 271 bool y_flip); 272 273 /** 274 * @brief Create a flattened RGBA FLOAT32 data array from an image structure. 275 * 276 * The returned data array is allocated with @c new[] and must be freed with a @c delete[] call. 277 * 278 * @param img The input image. 279 * @param y_flip Should the data in the array be Y flipped? 280 * 281 * @return The data array. 282 */ 283 float* floatx4_array_from_astc_img( 284 const astcenc_image* img, 285 bool y_flip); 286 287 /** 288 * @brief Create a flattened RGBA UNORM8 data array from an image structure. 289 * 290 * The returned data array is allocated with @c new[] and must be freed with a @c delete[] call. 291 * 292 * @param img The input image. 293 * @param y_flip Should the data in the array be Y flipped? 294 * 295 * @return The data array. 296 */ 297 uint8_t* unorm8x4_array_from_astc_img( 298 const astcenc_image* img, 299 bool y_flip); 300 301 /* ============================================================================ 302 Functions for printing build info and help messages 303 ============================================================================ */ 304 305 /** 306 * @brief Print the tool copyright and version header to stdout. 307 */ 308 void astcenc_print_header(); 309 310 /** 311 * @brief Print the tool copyright, version, and short-form help to stdout. 312 */ 313 void astcenc_print_shorthelp(); 314 315 /** 316 * @brief Print the tool copyright, version, and long-form help to stdout. 317 */ 318 void astcenc_print_longhelp(); 319 320 /** 321 * @brief Compute error metrics comparing two images. 322 * 323 * @param compute_hdr_metrics True if HDR metrics should be computed. 324 * @param compute_normal_metrics True if normal map metrics should be computed. 325 * @param input_components The number of input color components. 326 * @param img1 The original image. 327 * @param img2 The compressed image. 328 * @param fstop_lo The low exposure fstop (HDR only). 329 * @param fstop_hi The high exposure fstop (HDR only). 330 */ 331 void compute_error_metrics( 332 bool compute_hdr_metrics, 333 bool compute_normal_metrics, 334 int input_components, 335 const astcenc_image* img1, 336 const astcenc_image* img2, 337 int fstop_lo, 338 int fstop_hi); 339 340 /** 341 * @brief Get the current time. 342 * 343 * @return The current time in seconds since arbitrary epoch. 344 */ 345 double get_time(); 346 347 /** 348 * @brief Get the number of CPU cores. 349 * 350 * @return The number of online or onlineable CPU cores in the system. 351 */ 352 int get_cpu_count(); 353 354 /** 355 * @brief Launch N worker threads and wait for them to complete. 356 * 357 * All threads run the same thread function, and have the same thread payload, but are given a 358 * unique thread ID (0 .. N-1) as a parameter to the run function to allow thread-specific behavior. 359 * 360 |* @param thread_count The number of threads to spawn. 361 * @param func The function to execute. Must have the signature: 362 * void (int thread_count, int thread_id, void* payload) 363 * @param payload Pointer to an opaque thread payload object. 364 */ 365 void launch_threads( 366 int thread_count, 367 void (*func)(int, int, void*), 368 void *payload); 369 370 #endif 371