diff options
Diffstat (limited to '3rdparty/bimg')
105 files changed, 47777 insertions, 53965 deletions
diff --git a/3rdparty/bimg/.appveyor.yml b/3rdparty/bimg/.appveyor.yml index 4d54558e367..014bb991a7d 100644 --- a/3rdparty/bimg/.appveyor.yml +++ b/3rdparty/bimg/.appveyor.yml @@ -1,11 +1,12 @@ shallow_clone: true os: - - Visual Studio 2017 + - Visual Studio 2019 environment: matrix: - TOOLSET: vs2017 + - TOOLSET: vs2019 configuration: - Debug diff --git a/3rdparty/bimg/.editorconfig b/3rdparty/bimg/.editorconfig index 4fa189592fd..3a55382bae2 100644 --- a/3rdparty/bimg/.editorconfig +++ b/3rdparty/bimg/.editorconfig @@ -1,19 +1,14 @@ root = true [*] -indent_style = tab -indent_size = 4 -end_of_line = lf +charset = utf-8 +indent_style = tab +indent_size = 4 +end_of_line = lf max_line_length = 100 insert_final_newline = true trim_trailing_whitespace = true -[include/bgfx/c99/*.h] -indent_style = space - -[*.ninja] -indent_style = space - [*.md] trim_trailing_whitespace = false max_line_length = 80 diff --git a/3rdparty/bimg/.travis.yml b/3rdparty/bimg/.travis.yml deleted file mode 100644 index c656c72ade1..00000000000 --- a/3rdparty/bimg/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: cpp -matrix: - include: - - compiler: gcc - os: linux - - compiler: clang - os: osx - -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - gcc-5 - - g++-5 - - clang - -before_script: - git clone https://github.com/bkaradzic/bx ../bx - -script: - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then make build CXX="g++-5" CC="gcc-5"; fi - - if [ "$TRAVIS_OS_NAME" == "osx" ]; then make build; fi - -branches: - only: - - master - -notifications: - email: false - -osx_image: xcode9.3 diff --git a/3rdparty/bimg/3rdparty/astc-encoder/LICENSE.txt b/3rdparty/bimg/3rdparty/astc-encoder/LICENSE.txt new file mode 100644 index 00000000000..b82735a3103 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/LICENSE.txt @@ -0,0 +1,175 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. diff --git a/3rdparty/bimg/3rdparty/astc-encoder/include/astcenc.h b/3rdparty/bimg/3rdparty/astc-encoder/include/astcenc.h new file mode 100644 index 00000000000..56f1ad8058c --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/include/astcenc.h @@ -0,0 +1,834 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2020-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief The core astcenc codec library interface. + * + * This interface is the entry point to the core astcenc codec. It aims to be easy to use for + * non-experts, but also to allow experts to have fine control over the compressor heuristics if + * needed. The core codec only handles compression and decompression, transferring all inputs and + * outputs via memory buffers. To catch obvious input/output buffer sizing issues, which can cause + * security and stability problems, all transfer buffers are explicitly sized. + * + * While the aim is that we keep this interface mostly stable, it should be viewed as a mutable + * interface tied to a specific source version. We are not trying to maintain backwards + * compatibility across codec versions. + * + * The API state management is based around an explicit context object, which is the context for all + * allocated memory resources needed to compress and decompress a single image. A context can be + * used to sequentially compress multiple images using the same configuration, allowing setup + * overheads to be amortized over multiple images, which is particularly important when images are + * small. + * + * Multi-threading can be used two ways. + * + * * An application wishing to process multiple images in parallel can allocate multiple + * contexts and assign each context to a thread. + * * An application wishing to process a single image in using multiple threads can configure + * contexts for multi-threaded use, and invoke astcenc_compress/decompress() once per thread + * for faster processing. The caller is responsible for creating the worker threads, and + * synchronizing between images. + * + * Threading + * ========= + * + * In pseudo-code, the usage for manual user threading looks like this: + * + * // Configure the compressor run + * astcenc_config my_config; + * astcenc_config_init(..., &my_config); + * + * // Power users can tweak <my_config> settings here ... + * + * // Allocate working state given config and thread_count + * astcenc_context* my_context; + * astcenc_context_alloc(&my_config, thread_count, &my_context); + * + * // Compress each image using these config settings + * foreach image: + * // For each thread in the thread pool + * for i in range(0, thread_count): + * astcenc_compress_image(my_context, &my_input, my_output, i); + * + * astcenc_compress_reset(my_context); + * + * // Clean up + * astcenc_context_free(my_context); + * + * Images + * ====== + * + * The codec supports compressing single images, which can be either 2D images or volumetric 3D + * images. Calling code is responsible for any handling of aggregate types, such as mipmap chains, + * texture arrays, or sliced 3D textures. + * + * Images are passed in as an astcenc_image structure. Inputs can be either 8-bit unorm, 16-bit + * half-float, or 32-bit float, as indicated by the data_type field. + * + * Images can be any dimension; there is no requirement to be a multiple of the ASTC block size. + * + * Data is always passed in as 4 color components, and accessed as an array of 2D image slices. Data + * within an image slice is always tightly packed without padding. Addressing looks like this: + * + * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 ] // Red + * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 1] // Green + * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 2] // Blue + * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 3] // Alpha + * + * Common compressor usage + * ======================= + * + * One of the most important things for coding image quality is to align the input data component + * count with the ASTC color endpoint mode. This avoids wasting bits encoding components you don't + * actually need in the endpoint colors. + * + * | Input data | Encoding swizzle | Sampling swizzle | + * | ------------ | ---------------- | ---------------- | + * | 1 component | RRR1 | .[rgb] | + * | 2 components | RRRG | .[rgb]a | + * | 3 components | RGB1 | .rgb | + * | 4 components | RGBA | .rgba | + * + * The 1 and 2 component modes recommend sampling from "g" to recover the luminance value as this + * provide best compatibility with other texture formats where the green component may be stored at + * higher precision than the others, such as RGB565. For ASTC any of the RGB components can be used; + * the luminance endpoint component will be returned for all three. + * + * When using the normal map compression mode ASTC will store normals as a two component X+Y map. + * Input images must contain unit-length normalized and should be passed in using a two component + * swizzle. The astcenc command line tool defaults to an RRRG swizzle, but some developers prefer + * to use GGGR for compatability with BC5n which will work just as well. The Z component can be + * recovered programmatically in shader code, using knowledge that the vector is unit length and + * that Z must be positive for a tangent-space normal map. + * + * Decompress-only usage + * ===================== + * + * For some use cases it is useful to have a cut-down context and/or library which supports + * decompression but not compression. + * + * A context can be made decompress-only using the ASTCENC_FLG_DECOMPRESS_ONLY flag when the context + * is allocated. These contexts have lower dynamic memory footprint than a full context. + * + * The entire library can be made decompress-only by building the files with the define + * ASTCENC_DECOMPRESS_ONLY set. In this build the context will be smaller, and the library will + * exclude the functionality which is only needed for compression. This reduces the binary size by + * ~180KB. For these builds contexts must be created with the ASTCENC_FLG_DECOMPRESS_ONLY flag. + * + * Note that context structures returned by a library built as decompress-only are incompatible with + * a library built with compression included, and visa versa, as they have different sizes and + * memory layout. + * + * Self-decompress-only usage + * ========================== + * + * ASTC is a complex format with a large search space. The parts of this search space that are + * searched is determined by heuristics that are, in part, tied to the quality level used when + * creating the context. + * + * A normal context is capable of decompressing any ASTC texture, including those generated by other + * compressors with unknown heuristics. This is the most flexible implementation, but forces the + * data tables used by the codec to include entries that are not needed during compression. This + * can slow down context creation by a significant amount, especially for the faster compression + * modes where few data table entries are actually used. To optimize this use case the context can + * be created with the ASTCENC_FLG_SELF_DECOMPRESS_ONLY flag. This tells the compressor that it will + * only be asked to decompress images that it compressed itself, allowing the data tables to + * exclude entries that are not needed by the current compression configuration. This reduces the + * size of the context data tables in memory and improves context creation performance. Note that, + * as of the 3.6 release, this flag no longer affects compression performance. + * + * Using this flag while attempting to decompress an valid image which was created by another + * compressor, or even another astcenc compressor version or configuration, may result in blocks + * returning as solid magenta or NaN value error blocks. + */ + +#ifndef ASTCENC_INCLUDED +#define ASTCENC_INCLUDED + +#include <cstddef> +#include <cstdint> + +#if defined(ASTCENC_DYNAMIC_LIBRARY) + #if defined(_MSC_VER) + #define ASTCENC_PUBLIC extern "C" __declspec(dllexport) + #else + #define ASTCENC_PUBLIC extern "C" __attribute__ ((visibility ("default"))) + #endif +#else + #define ASTCENC_PUBLIC +#endif + +/* ============================================================================ + Data declarations +============================================================================ */ + +/** + * @brief An opaque structure; see astcenc_internal.h for definition. + */ +struct astcenc_context; + +/** + * @brief A codec API error code. + */ +enum astcenc_error { + /** @brief The call was successful. */ + ASTCENC_SUCCESS = 0, + /** @brief The call failed due to low memory, or undersized I/O buffers. */ + ASTCENC_ERR_OUT_OF_MEM, + /** @brief The call failed due to the build using fast math. */ + ASTCENC_ERR_BAD_CPU_FLOAT, + /** @brief The call failed due to the build using an unsupported ISA. */ + ASTCENC_ERR_BAD_CPU_ISA, + /** @brief The call failed due to an out-of-spec parameter. */ + ASTCENC_ERR_BAD_PARAM, + /** @brief The call failed due to an out-of-spec block size. */ + ASTCENC_ERR_BAD_BLOCK_SIZE, + /** @brief The call failed due to an out-of-spec color profile. */ + ASTCENC_ERR_BAD_PROFILE, + /** @brief The call failed due to an out-of-spec quality value. */ + ASTCENC_ERR_BAD_QUALITY, + /** @brief The call failed due to an out-of-spec component swizzle. */ + ASTCENC_ERR_BAD_SWIZZLE, + /** @brief The call failed due to an out-of-spec flag set. */ + ASTCENC_ERR_BAD_FLAGS, + /** @brief The call failed due to the context not supporting the operation. */ + ASTCENC_ERR_BAD_CONTEXT, + /** @brief The call failed due to unimplemented functionality. */ + ASTCENC_ERR_NOT_IMPLEMENTED, +#if defined(ASTCENC_DIAGNOSTICS) + /** @brief The call failed due to an issue with diagnostic tracing. */ + ASTCENC_ERR_DTRACE_FAILURE, +#endif +}; + +/** + * @brief A codec color profile. + */ +enum astcenc_profile { + /** @brief The LDR sRGB color profile. */ + ASTCENC_PRF_LDR_SRGB = 0, + /** @brief The LDR linear color profile. */ + ASTCENC_PRF_LDR, + /** @brief The HDR RGB with LDR alpha color profile. */ + ASTCENC_PRF_HDR_RGB_LDR_A, + /** @brief The HDR RGBA color profile. */ + ASTCENC_PRF_HDR +}; + +/** @brief The fastest, lowest quality, search preset. */ +static const float ASTCENC_PRE_FASTEST = 0.0f; + +/** @brief The fast search preset. */ +static const float ASTCENC_PRE_FAST = 10.0f; + +/** @brief The medium quality search preset. */ +static const float ASTCENC_PRE_MEDIUM = 60.0f; + +/** @brief The thorough quality search preset. */ +static const float ASTCENC_PRE_THOROUGH = 98.0f; + +/** @brief The thorough quality search preset. */ +static const float ASTCENC_PRE_VERYTHOROUGH = 99.0f; + +/** @brief The exhaustive, highest quality, search preset. */ +static const float ASTCENC_PRE_EXHAUSTIVE = 100.0f; + +/** + * @brief A codec component swizzle selector. + */ +enum astcenc_swz +{ + /** @brief Select the red component. */ + ASTCENC_SWZ_R = 0, + /** @brief Select the green component. */ + ASTCENC_SWZ_G = 1, + /** @brief Select the blue component. */ + ASTCENC_SWZ_B = 2, + /** @brief Select the alpha component. */ + ASTCENC_SWZ_A = 3, + /** @brief Use a constant zero component. */ + ASTCENC_SWZ_0 = 4, + /** @brief Use a constant one component. */ + ASTCENC_SWZ_1 = 5, + /** @brief Use a reconstructed normal vector Z component. */ + ASTCENC_SWZ_Z = 6 +}; + +/** + * @brief A texel component swizzle. + */ +struct astcenc_swizzle +{ + /** @brief The red component selector. */ + astcenc_swz r; + /** @brief The green component selector. */ + astcenc_swz g; + /** @brief The blue component selector. */ + astcenc_swz b; + /** @brief The alpha component selector. */ + astcenc_swz a; +}; + +/** + * @brief A texel component data format. + */ +enum astcenc_type +{ + /** @brief Unorm 8-bit data per component. */ + ASTCENC_TYPE_U8 = 0, + /** @brief 16-bit float per component. */ + ASTCENC_TYPE_F16 = 1, + /** @brief 32-bit float per component. */ + ASTCENC_TYPE_F32 = 2 +}; + +/** + * @brief Enable normal map compression. + * + * Input data will be treated a two component normal map, storing X and Y, and the codec will + * optimize for angular error rather than simple linear PSNR. In this mode the input swizzle should + * be e.g. rrrg (the default ordering for ASTC normals on the command line) or gggr (the ordering + * used by BC5n). + */ +static const unsigned int ASTCENC_FLG_MAP_NORMAL = 1 << 0; + +/** + * @brief Enable mask map compression. + * + * Input data will be treated a multi-layer mask map, where is is desirable for the color components + * to be treated independently for the purposes of error analysis. + */ +static const unsigned int ASTCENC_FLG_MAP_MASK = 1 << 1; + +/** + * @brief Enable alpha weighting. + * + * The input alpha value is used for transparency, so errors in the RGB components are weighted by + * the transparency level. This allows the codec to more accurately encode the alpha value in areas + * where the color value is less significant. + */ +static const unsigned int ASTCENC_FLG_USE_ALPHA_WEIGHT = 1 << 2; + +/** + * @brief Enable perceptual error metrics. + * + * This mode enables perceptual compression mode, which will optimize for perceptual error rather + * than best PSNR. Only some input modes support perceptual error metrics. + */ +static const unsigned int ASTCENC_FLG_USE_PERCEPTUAL = 1 << 3; + +/** + * @brief Create a decompression-only context. + * + * This mode disables support for compression. This enables context allocation to skip some + * transient buffer allocation, resulting in lower memory usage. + */ +static const unsigned int ASTCENC_FLG_DECOMPRESS_ONLY = 1 << 4; + +/** + * @brief Create a self-decompression context. + * + * This mode configures the compressor so that it is only guaranteed to be able to decompress images + * that were actually created using the current context. This is the common case for compression use + * cases, and setting this flag enables additional optimizations, but does mean that the context + * cannot reliably decompress arbitrary ASTC images. + */ +static const unsigned int ASTCENC_FLG_SELF_DECOMPRESS_ONLY = 1 << 5; + +/** + * @brief Enable RGBM map compression. + * + * Input data will be treated as HDR data that has been stored in an LDR RGBM-encoded wrapper + * format. Data must be preprocessed by the user to be in LDR RGBM format before calling the + * compression function, this flag is only used to control the use of RGBM-specific heuristics and + * error metrics. + * + * IMPORTANT: The ASTC format is prone to bad failure modes with unconstrained RGBM data; very small + * M values can round to zero due to quantization and result in black or white pixels. It is highly + * recommended that the minimum value of M used in the encoding is kept above a lower threshold (try + * 16 or 32). Applying this threshold reduces the number of very dark colors that can be + * represented, but is still higher precision than 8-bit LDR. + * + * When this flag is set the value of @c rgbm_m_scale in the context must be set to the RGBM scale + * factor used during reconstruction. This defaults to 5 when in RGBM mode. + * + * It is recommended that the value of @c cw_a_weight is set to twice the value of the multiplier + * scale, ensuring that the M value is accurately encoded. This defaults to 10 when in RGBM mode, + * matching the default scale factor. + */ +static const unsigned int ASTCENC_FLG_MAP_RGBM = 1 << 6; + +/** + * @brief The bit mask of all valid flags. + */ +static const unsigned int ASTCENC_ALL_FLAGS = + ASTCENC_FLG_MAP_MASK | + ASTCENC_FLG_MAP_NORMAL | + ASTCENC_FLG_MAP_RGBM | + ASTCENC_FLG_USE_ALPHA_WEIGHT | + ASTCENC_FLG_USE_PERCEPTUAL | + ASTCENC_FLG_DECOMPRESS_ONLY | + ASTCENC_FLG_SELF_DECOMPRESS_ONLY; + +/** + * @brief The config structure. + * + * This structure will initially be populated by a call to astcenc_config_init, but power users may + * modify it before calling astcenc_context_alloc. See astcenccli_toplevel_help.cpp for full user + * documentation of the power-user settings. + * + * Note for any settings which are associated with a specific color component, the value in the + * config applies to the component that exists after any compression data swizzle is applied. + */ +struct astcenc_config +{ + /** @brief The color profile. */ + astcenc_profile profile; + + /** @brief The set of set flags. */ + unsigned int flags; + + /** @brief The ASTC block size X dimension. */ + unsigned int block_x; + + /** @brief The ASTC block size Y dimension. */ + unsigned int block_y; + + /** @brief The ASTC block size Z dimension. */ + unsigned int block_z; + + /** @brief The red component weight scale for error weighting (-cw). */ + float cw_r_weight; + + /** @brief The green component weight scale for error weighting (-cw). */ + float cw_g_weight; + + /** @brief The blue component weight scale for error weighting (-cw). */ + float cw_b_weight; + + /** @brief The alpha component weight scale for error weighting (-cw). */ + float cw_a_weight; + + /** + * @brief The radius for any alpha-weight scaling (-a). + * + * It is recommended that this is set to 1 when using FLG_USE_ALPHA_WEIGHT on a texture that + * will be sampled using linear texture filtering to minimize color bleed out of transparent + * texels that are adjacent to non-transparent texels. + */ + unsigned int a_scale_radius; + + /** @brief The RGBM scale factor for the shared multiplier (-rgbm). */ + float rgbm_m_scale; + + /** + * @brief The maximum number of partitions searched (-partitioncountlimit). + * + * Valid values are between 1 and 4. + */ + unsigned int tune_partition_count_limit; + + /** + * @brief The maximum number of partitions searched (-2partitionindexlimit). + * + * Valid values are between 1 and 1024. + */ + unsigned int tune_2partition_index_limit; + + /** + * @brief The maximum number of partitions searched (-3partitionindexlimit). + * + * Valid values are between 1 and 1024. + */ + unsigned int tune_3partition_index_limit; + + /** + * @brief The maximum number of partitions searched (-4partitionindexlimit). + * + * Valid values are between 1 and 1024. + */ + unsigned int tune_4partition_index_limit; + + /** + * @brief The maximum centile for block modes searched (-blockmodelimit). + * + * Valid values are between 1 and 100. + */ + unsigned int tune_block_mode_limit; + + /** + * @brief The maximum iterative refinements applied (-refinementlimit). + * + * Valid values are between 1 and N; there is no technical upper limit + * but little benefit is expected after N=4. + */ + unsigned int tune_refinement_limit; + + /** + * @brief The number of trial candidates per mode search (-candidatelimit). + * + * Valid values are between 1 and TUNE_MAX_TRIAL_CANDIDATES (default 4). + */ + unsigned int tune_candidate_limit; + + /** + * @brief The number of trial partitionings per search (-2partitioncandidatelimit). + * + * Valid values are between 1 and TUNE_MAX_PARTITIIONING_CANDIDATES. + */ + unsigned int tune_2partitioning_candidate_limit; + + /** + * @brief The number of trial partitionings per search (-3partitioncandidatelimit). + * + * Valid values are between 1 and TUNE_MAX_PARTITIIONING_CANDIDATES. + */ + unsigned int tune_3partitioning_candidate_limit; + + /** + * @brief The number of trial partitionings per search (-4partitioncandidatelimit). + * + * Valid values are between 1 and TUNE_MAX_PARTITIIONING_CANDIDATES. + */ + unsigned int tune_4partitioning_candidate_limit; + + /** + * @brief The dB threshold for stopping block search (-dblimit). + * + * This option is ineffective for HDR textures. + */ + float tune_db_limit; + + /** + * @brief The amount of overshoot needed to early-out mode 0 fast path. + * + * We have a fast-path for mode 0 (1 partition, 1 plane) which uses only essential block modes + * as an initial search. This can short-cut compression for simple blocks, but to avoid + * short-cutting too much we force this to overshoot the MSE threshold needed to hit the + * block-local db_limit e.g. 1.0 = no overshoot, 2.0 = need half the error to trigger. + */ + float tune_mode0_mse_overshoot; + + /** + * @brief The amount of overshoot needed to early-out refinement. + * + * The codec will refine block candidates iteratively to improve the encoding, based on the + * @c tune_refinement_limit count. Earlier implementations will use all refinement iterations, + * even if the target threshold is reached. This tuning parameter allows an early out, but with + * an overshoot MSE threshold. Setting this to 1.0 will early-out as soon as the target is hit, + * but does reduce image quality vs the default behavior of over-refinement. + */ + float tune_refinement_mse_overshoot; + + /** + * @brief The threshold for skipping 3.1/4.1 trials (-2partitionlimitfactor). + * + * This option is further scaled for normal maps, so it skips less often. + */ + float tune_2_partition_early_out_limit_factor; + + /** + * @brief The threshold for skipping 4.1 trials (-3partitionlimitfactor). + * + * This option is further scaled for normal maps, so it skips less often. + */ + float tune_3_partition_early_out_limit_factor; + + /** + * @brief The threshold for skipping two weight planes (-2planelimitcorrelation). + * + * This option is ineffective for normal maps. + */ + float tune_2_plane_early_out_limit_correlation; + +#if defined(ASTCENC_DIAGNOSTICS) + /** + * @brief The path to save the diagnostic trace data to. + * + * This option is not part of the public API, and requires special builds + * of the library. + */ + const char* trace_file_path; +#endif +}; + +/** + * @brief An uncompressed 2D or 3D image. + * + * 3D image are passed in as an array of 2D slices. Each slice has identical + * size and color format. + */ +struct astcenc_image +{ + /** @brief The X dimension of the image, in texels. */ + unsigned int dim_x; + + /** @brief The Y dimension of the image, in texels. */ + unsigned int dim_y; + + /** @brief The Z dimension of the image, in texels. */ + unsigned int dim_z; + + /** @brief The data type per component. */ + astcenc_type data_type; + + /** @brief The array of 2D slices, of length @c dim_z. */ + void** data; +}; + +/** + * @brief A block encoding metadata query result. + * + * If the block is an error block or a constant color block or an error block all fields other than + * the profile, block dimensions, and error/constant indicator will be zero. + */ +struct astcenc_block_info +{ + /** @brief The block encoding color profile. */ + astcenc_profile profile; + + /** @brief The number of texels in the X dimension. */ + unsigned int block_x; + + /** @brief The number of texels in the Y dimension. */ + unsigned int block_y; + + /** @brief The number of texel in the Z dimension. */ + unsigned int block_z; + + /** @brief The number of texels in the block. */ + unsigned int texel_count; + + /** @brief True if this block is an error block. */ + bool is_error_block; + + /** @brief True if this block is a constant color block. */ + bool is_constant_block; + + /** @brief True if this block is an HDR block. */ + bool is_hdr_block; + + /** @brief True if this block uses two weight planes. */ + bool is_dual_plane_block; + + /** @brief The number of partitions if not constant color. */ + unsigned int partition_count; + + /** @brief The partition index if 2 - 4 partitions used. */ + unsigned int partition_index; + + /** @brief The component index of the second plane if dual plane. */ + unsigned int dual_plane_component; + + /** @brief The color endpoint encoding mode for each partition. */ + unsigned int color_endpoint_modes[4]; + + /** @brief The number of color endpoint quantization levels. */ + unsigned int color_level_count; + + /** @brief The number of weight quantization levels. */ + unsigned int weight_level_count; + + /** @brief The number of weights in the X dimension. */ + unsigned int weight_x; + + /** @brief The number of weights in the Y dimension. */ + unsigned int weight_y; + + /** @brief The number of weights in the Z dimension. */ + unsigned int weight_z; + + /** @brief The unpacked color endpoints for each partition. */ + float color_endpoints[4][2][4]; + + /** @brief The per-texel interpolation weights for the block. */ + float weight_values_plane1[216]; + + /** @brief The per-texel interpolation weights for the block. */ + float weight_values_plane2[216]; + + /** @brief The per-texel partition assignments for the block. */ + uint8_t partition_assignment[216]; +}; + +/** + * Populate a codec config based on default settings. + * + * Power users can edit the returned config struct to fine tune before allocating the context. + * + * @param profile Color profile. + * @param block_x ASTC block size X dimension. + * @param block_y ASTC block size Y dimension. + * @param block_z ASTC block size Z dimension. + * @param quality Search quality preset / effort level. Either an + * @c ASTCENC_PRE_* value, or a effort level between 0 + * and 100. Performance is not linear between 0 and 100. + + * @param flags A valid set of @c ASTCENC_FLG_* flag bits. + * @param[out] config Output config struct to populate. + * + * @return @c ASTCENC_SUCCESS on success, or an error if the inputs are invalid + * either individually, or in combination. + */ +ASTCENC_PUBLIC astcenc_error astcenc_config_init( + astcenc_profile profile, + unsigned int block_x, + unsigned int block_y, + unsigned int block_z, + float quality, + unsigned int flags, + astcenc_config* config); + +/** + * @brief Allocate a new codec context based on a config. + * + * This function allocates all of the memory resources and threads needed by the codec. This can be + * slow, so it is recommended that contexts are reused to serially compress or decompress multiple + * images to amortize setup cost. + * + * Contexts can be allocated to support only decompression using the @c ASTCENC_FLG_DECOMPRESS_ONLY + * flag when creating the configuration. The compression functions will fail if invoked. For a + * decompress-only library build the @c ASTCENC_FLG_DECOMPRESS_ONLY flag must be set when creating + * any context. + * + * @param[in] config Codec config. + * @param thread_count Thread count to configure for. + * @param[out] context Location to store an opaque context pointer. + * + * @return @c ASTCENC_SUCCESS on success, or an error if context creation failed. + */ +ASTCENC_PUBLIC astcenc_error astcenc_context_alloc( + const astcenc_config* config, + unsigned int thread_count, + astcenc_context** context); + +/** + * @brief Compress an image. + * + * A single context can only compress or decompress a single image at a time. + * + * For a context configured for multi-threading, any set of the N threads can call this function. + * Work will be dynamically scheduled across the threads available. Each thread must have a unique + * @c thread_index. + * + * @param context Codec context. + * @param[in,out] image An input image, in 2D slices. + * @param swizzle Compression data swizzle, applied before compression. + * @param[out] data_out Pointer to output data array. + * @param data_len Length of the output data array. + * @param thread_index Thread index [0..N-1] of calling thread. + * + * @return @c ASTCENC_SUCCESS on success, or an error if compression failed. + */ +ASTCENC_PUBLIC astcenc_error astcenc_compress_image( + astcenc_context* context, + astcenc_image* image, + const astcenc_swizzle* swizzle, + uint8_t* data_out, + size_t data_len, + unsigned int thread_index); + +/** + * @brief Reset the codec state for a new compression. + * + * The caller is responsible for synchronizing threads in the worker thread pool. This function must + * only be called when all threads have exited the @c astcenc_compress_image() function for image N, + * but before any thread enters it for image N + 1. + * + * Calling this is not required (but won't hurt), if the context is created for single threaded use. + * + * @param context Codec context. + * + * @return @c ASTCENC_SUCCESS on success, or an error if reset failed. + */ +ASTCENC_PUBLIC astcenc_error astcenc_compress_reset( + astcenc_context* context); + +/** + * @brief Decompress an image. + * + * @param context Codec context. + * @param[in] data Pointer to compressed data. + * @param data_len Length of the compressed data, in bytes. + * @param[in,out] image_out Output image. + * @param swizzle Decompression data swizzle, applied after decompression. + * @param thread_index Thread index [0..N-1] of calling thread. + * + * @return @c ASTCENC_SUCCESS on success, or an error if decompression failed. + */ +ASTCENC_PUBLIC astcenc_error astcenc_decompress_image( + astcenc_context* context, + const uint8_t* data, + size_t data_len, + astcenc_image* image_out, + const astcenc_swizzle* swizzle, + unsigned int thread_index); + +/** + * @brief Reset the codec state for a new decompression. + * + * The caller is responsible for synchronizing threads in the worker thread pool. This function must + * only be called when all threads have exited the @c astcenc_decompress_image() function for image + * N, but before any thread enters it for image N + 1. + * + * Calling this is not required (but won't hurt), if the context is created for single threaded use. + * + * @param context Codec context. + * + * @return @c ASTCENC_SUCCESS on success, or an error if reset failed. + */ +ASTCENC_PUBLIC astcenc_error astcenc_decompress_reset( + astcenc_context* context); + +/** + * Free the compressor context. + * + * @param context The codec context. + */ +ASTCENC_PUBLIC void astcenc_context_free( + astcenc_context* context); + +/** + * @brief Provide a high level summary of a block's encoding. + * + * This feature is primarily useful for codec developers but may be useful for developers building + * advanced content packaging pipelines. + * + * @param context Codec context. + * @param data One block of compressed ASTC data. + * @param info The output info structure to populate. + * + * @return @c ASTCENC_SUCCESS if the block was decoded, or an error otherwise. Note that this + * function will return success even if the block itself was an error block encoding, as the + * decode was correctly handled. + */ +ASTCENC_PUBLIC astcenc_error astcenc_get_block_info( + astcenc_context* context, + const uint8_t data[16], + astcenc_block_info* info); + +/** + * @brief Get a printable string for specific status code. + * + * @param status The status value. + * + * @return A human readable nul-terminated string. + */ +ASTCENC_PUBLIC const char* astcenc_get_error_string( + astcenc_error status); + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_averages_and_directions.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_averages_and_directions.cpp new file mode 100644 index 00000000000..d1f003844a3 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_averages_and_directions.cpp @@ -0,0 +1,995 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for finding dominant direction of a set of colors. + */ +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +#include "astcenc_internal.h" + +#include <cassert> + +/** + * @brief Compute the average RGB color of each partition. + * + * The algorithm here uses a vectorized sequential scan and per-partition + * color accumulators, using select() to mask texel lanes in other partitions. + * + * We only accumulate sums for N-1 partitions during the scan; the value for + * the last partition can be computed given that we know the block-wide average + * already. + * + * Because of this we could reduce the loop iteration count so it "just" spans + * the max texel index needed for the N-1 partitions, which could need fewer + * iterations than the full block texel count. However, this makes the loop + * count erratic and causes more branch mispredictions so is a net loss. + * + * @param pi The partitioning to use. + * @param blk The block data to process. + * @param[out] averages The output averages. Unused partition indices will + * not be initialized, and lane<3> will be zero. + */ +static void compute_partition_averages_rgb( + const partition_info& pi, + const image_block& blk, + vfloat4 averages[BLOCK_MAX_PARTITIONS] +) { + unsigned int partition_count = pi.partition_count; + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + + // For 1 partition just use the precomputed mean + if (partition_count == 1) + { + averages[0] = blk.data_mean.swz<0, 1, 2>(); + } + // For 2 partitions scan results for partition 0, compute partition 1 + else if (partition_count == 2) + { + vfloatacc pp_avg_rgb[3] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgb[0], data_r, p0_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgb[1], data_g, p0_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgb[2], data_b, p0_mask); + } + + vfloat4 block_total = blk.data_mean.swz<0, 1, 2>() * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat3(hadd_s(pp_avg_rgb[0]), + hadd_s(pp_avg_rgb[1]), + hadd_s(pp_avg_rgb[2])); + + vfloat4 p1_total = block_total - p0_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + } + // For 3 partitions scan results for partition 0/1, compute partition 2 + else if (partition_count == 3) + { + vfloatacc pp_avg_rgb[2][3] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + vmask p1_mask = lane_mask & (texel_partition == vint(1)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgb[0][0], data_r, p0_mask); + haccumulate(pp_avg_rgb[1][0], data_r, p1_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgb[0][1], data_g, p0_mask); + haccumulate(pp_avg_rgb[1][1], data_g, p1_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgb[0][2], data_b, p0_mask); + haccumulate(pp_avg_rgb[1][2], data_b, p1_mask); + } + + vfloat4 block_total = blk.data_mean.swz<0, 1, 2>() * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat3(hadd_s(pp_avg_rgb[0][0]), + hadd_s(pp_avg_rgb[0][1]), + hadd_s(pp_avg_rgb[0][2])); + + vfloat4 p1_total = vfloat3(hadd_s(pp_avg_rgb[1][0]), + hadd_s(pp_avg_rgb[1][1]), + hadd_s(pp_avg_rgb[1][2])); + + vfloat4 p2_total = block_total - p0_total - p1_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + averages[2] = p2_total / static_cast<float>(pi.partition_texel_count[2]); + } + else + { + // For 4 partitions scan results for partition 0/1/2, compute partition 3 + vfloatacc pp_avg_rgb[3][3] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + vmask p1_mask = lane_mask & (texel_partition == vint(1)); + vmask p2_mask = lane_mask & (texel_partition == vint(2)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgb[0][0], data_r, p0_mask); + haccumulate(pp_avg_rgb[1][0], data_r, p1_mask); + haccumulate(pp_avg_rgb[2][0], data_r, p2_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgb[0][1], data_g, p0_mask); + haccumulate(pp_avg_rgb[1][1], data_g, p1_mask); + haccumulate(pp_avg_rgb[2][1], data_g, p2_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgb[0][2], data_b, p0_mask); + haccumulate(pp_avg_rgb[1][2], data_b, p1_mask); + haccumulate(pp_avg_rgb[2][2], data_b, p2_mask); + } + + vfloat4 block_total = blk.data_mean.swz<0, 1, 2>() * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat3(hadd_s(pp_avg_rgb[0][0]), + hadd_s(pp_avg_rgb[0][1]), + hadd_s(pp_avg_rgb[0][2])); + + vfloat4 p1_total = vfloat3(hadd_s(pp_avg_rgb[1][0]), + hadd_s(pp_avg_rgb[1][1]), + hadd_s(pp_avg_rgb[1][2])); + + vfloat4 p2_total = vfloat3(hadd_s(pp_avg_rgb[2][0]), + hadd_s(pp_avg_rgb[2][1]), + hadd_s(pp_avg_rgb[2][2])); + + vfloat4 p3_total = block_total - p0_total - p1_total- p2_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + averages[2] = p2_total / static_cast<float>(pi.partition_texel_count[2]); + averages[3] = p3_total / static_cast<float>(pi.partition_texel_count[3]); + } +} + +/** + * @brief Compute the average RGBA color of each partition. + * + * The algorithm here uses a vectorized sequential scan and per-partition + * color accumulators, using select() to mask texel lanes in other partitions. + * + * We only accumulate sums for N-1 partitions during the scan; the value for + * the last partition can be computed given that we know the block-wide average + * already. + * + * Because of this we could reduce the loop iteration count so it "just" spans + * the max texel index needed for the N-1 partitions, which could need fewer + * iterations than the full block texel count. However, this makes the loop + * count erratic and causes more branch mispredictions so is a net loss. + * + * @param pi The partitioning to use. + * @param blk The block data to process. + * @param[out] averages The output averages. Unused partition indices will + * not be initialized. + */ +static void compute_partition_averages_rgba( + const partition_info& pi, + const image_block& blk, + vfloat4 averages[BLOCK_MAX_PARTITIONS] +) { + unsigned int partition_count = pi.partition_count; + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + + // For 1 partition just use the precomputed mean + if (partition_count == 1) + { + averages[0] = blk.data_mean; + } + // For 2 partitions scan results for partition 0, compute partition 1 + else if (partition_count == 2) + { + vfloat4 pp_avg_rgba[4] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgba[0], data_r, p0_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgba[1], data_g, p0_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgba[2], data_b, p0_mask); + + vfloat data_a = loada(blk.data_a + i); + haccumulate(pp_avg_rgba[3], data_a, p0_mask); + } + + vfloat4 block_total = blk.data_mean * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat4(hadd_s(pp_avg_rgba[0]), + hadd_s(pp_avg_rgba[1]), + hadd_s(pp_avg_rgba[2]), + hadd_s(pp_avg_rgba[3])); + + vfloat4 p1_total = block_total - p0_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + } + // For 3 partitions scan results for partition 0/1, compute partition 2 + else if (partition_count == 3) + { + vfloat4 pp_avg_rgba[2][4] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + vmask p1_mask = lane_mask & (texel_partition == vint(1)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgba[0][0], data_r, p0_mask); + haccumulate(pp_avg_rgba[1][0], data_r, p1_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgba[0][1], data_g, p0_mask); + haccumulate(pp_avg_rgba[1][1], data_g, p1_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgba[0][2], data_b, p0_mask); + haccumulate(pp_avg_rgba[1][2], data_b, p1_mask); + + vfloat data_a = loada(blk.data_a + i); + haccumulate(pp_avg_rgba[0][3], data_a, p0_mask); + haccumulate(pp_avg_rgba[1][3], data_a, p1_mask); + } + + vfloat4 block_total = blk.data_mean * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat4(hadd_s(pp_avg_rgba[0][0]), + hadd_s(pp_avg_rgba[0][1]), + hadd_s(pp_avg_rgba[0][2]), + hadd_s(pp_avg_rgba[0][3])); + + vfloat4 p1_total = vfloat4(hadd_s(pp_avg_rgba[1][0]), + hadd_s(pp_avg_rgba[1][1]), + hadd_s(pp_avg_rgba[1][2]), + hadd_s(pp_avg_rgba[1][3])); + + vfloat4 p2_total = block_total - p0_total - p1_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + averages[2] = p2_total / static_cast<float>(pi.partition_texel_count[2]); + } + else + { + // For 4 partitions scan results for partition 0/1/2, compute partition 3 + vfloat4 pp_avg_rgba[3][4] {}; + + vint lane_id = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint texel_partition(pi.partition_of_texel + i); + + vmask lane_mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + + vmask p0_mask = lane_mask & (texel_partition == vint(0)); + vmask p1_mask = lane_mask & (texel_partition == vint(1)); + vmask p2_mask = lane_mask & (texel_partition == vint(2)); + + vfloat data_r = loada(blk.data_r + i); + haccumulate(pp_avg_rgba[0][0], data_r, p0_mask); + haccumulate(pp_avg_rgba[1][0], data_r, p1_mask); + haccumulate(pp_avg_rgba[2][0], data_r, p2_mask); + + vfloat data_g = loada(blk.data_g + i); + haccumulate(pp_avg_rgba[0][1], data_g, p0_mask); + haccumulate(pp_avg_rgba[1][1], data_g, p1_mask); + haccumulate(pp_avg_rgba[2][1], data_g, p2_mask); + + vfloat data_b = loada(blk.data_b + i); + haccumulate(pp_avg_rgba[0][2], data_b, p0_mask); + haccumulate(pp_avg_rgba[1][2], data_b, p1_mask); + haccumulate(pp_avg_rgba[2][2], data_b, p2_mask); + + vfloat data_a = loada(blk.data_a + i); + haccumulate(pp_avg_rgba[0][3], data_a, p0_mask); + haccumulate(pp_avg_rgba[1][3], data_a, p1_mask); + haccumulate(pp_avg_rgba[2][3], data_a, p2_mask); + } + + vfloat4 block_total = blk.data_mean * static_cast<float>(blk.texel_count); + + vfloat4 p0_total = vfloat4(hadd_s(pp_avg_rgba[0][0]), + hadd_s(pp_avg_rgba[0][1]), + hadd_s(pp_avg_rgba[0][2]), + hadd_s(pp_avg_rgba[0][3])); + + vfloat4 p1_total = vfloat4(hadd_s(pp_avg_rgba[1][0]), + hadd_s(pp_avg_rgba[1][1]), + hadd_s(pp_avg_rgba[1][2]), + hadd_s(pp_avg_rgba[1][3])); + + vfloat4 p2_total = vfloat4(hadd_s(pp_avg_rgba[2][0]), + hadd_s(pp_avg_rgba[2][1]), + hadd_s(pp_avg_rgba[2][2]), + hadd_s(pp_avg_rgba[2][3])); + + vfloat4 p3_total = block_total - p0_total - p1_total- p2_total; + + averages[0] = p0_total / static_cast<float>(pi.partition_texel_count[0]); + averages[1] = p1_total / static_cast<float>(pi.partition_texel_count[1]); + averages[2] = p2_total / static_cast<float>(pi.partition_texel_count[2]); + averages[3] = p3_total / static_cast<float>(pi.partition_texel_count[3]); + } +} + +/* See header for documentation. */ +void compute_avgs_and_dirs_4_comp( + const partition_info& pi, + const image_block& blk, + partition_metrics pm[BLOCK_MAX_PARTITIONS] +) { + int partition_count = pi.partition_count; + promise(partition_count > 0); + + // Pre-compute partition_averages + vfloat4 partition_averages[BLOCK_MAX_PARTITIONS]; + compute_partition_averages_rgba(pi, blk, partition_averages); + + for (int partition = 0; partition < partition_count; partition++) + { + const uint8_t *texel_indexes = pi.texels_of_partition[partition]; + unsigned int texel_count = pi.partition_texel_count[partition]; + promise(texel_count > 0); + + vfloat4 average = partition_averages[partition]; + pm[partition].avg = average; + + vfloat4 sum_xp = vfloat4::zero(); + vfloat4 sum_yp = vfloat4::zero(); + vfloat4 sum_zp = vfloat4::zero(); + vfloat4 sum_wp = vfloat4::zero(); + + for (unsigned int i = 0; i < texel_count; i++) + { + unsigned int iwt = texel_indexes[i]; + vfloat4 texel_datum = blk.texel(iwt); + texel_datum = texel_datum - average; + + vfloat4 zero = vfloat4::zero(); + + vmask4 tdm0 = texel_datum.swz<0,0,0,0>() > zero; + sum_xp += select(zero, texel_datum, tdm0); + + vmask4 tdm1 = texel_datum.swz<1,1,1,1>() > zero; + sum_yp += select(zero, texel_datum, tdm1); + + vmask4 tdm2 = texel_datum.swz<2,2,2,2>() > zero; + sum_zp += select(zero, texel_datum, tdm2); + + vmask4 tdm3 = texel_datum.swz<3,3,3,3>() > zero; + sum_wp += select(zero, texel_datum, tdm3); + } + + vfloat4 prod_xp = dot(sum_xp, sum_xp); + vfloat4 prod_yp = dot(sum_yp, sum_yp); + vfloat4 prod_zp = dot(sum_zp, sum_zp); + vfloat4 prod_wp = dot(sum_wp, sum_wp); + + vfloat4 best_vector = sum_xp; + vfloat4 best_sum = prod_xp; + + vmask4 mask = prod_yp > best_sum; + best_vector = select(best_vector, sum_yp, mask); + best_sum = select(best_sum, prod_yp, mask); + + mask = prod_zp > best_sum; + best_vector = select(best_vector, sum_zp, mask); + best_sum = select(best_sum, prod_zp, mask); + + mask = prod_wp > best_sum; + best_vector = select(best_vector, sum_wp, mask); + + pm[partition].dir = best_vector; + } +} + +/* See header for documentation. */ +void compute_avgs_and_dirs_3_comp( + const partition_info& pi, + const image_block& blk, + unsigned int omitted_component, + partition_metrics pm[BLOCK_MAX_PARTITIONS] +) { + // Pre-compute partition_averages + vfloat4 partition_averages[BLOCK_MAX_PARTITIONS]; + compute_partition_averages_rgba(pi, blk, partition_averages); + + const float* data_vr = blk.data_r; + const float* data_vg = blk.data_g; + const float* data_vb = blk.data_b; + + // TODO: Data-driven permute would be useful to avoid this ... + if (omitted_component == 0) + { + partition_averages[0] = partition_averages[0].swz<1, 2, 3>(); + partition_averages[1] = partition_averages[1].swz<1, 2, 3>(); + partition_averages[2] = partition_averages[2].swz<1, 2, 3>(); + partition_averages[3] = partition_averages[3].swz<1, 2, 3>(); + + data_vr = blk.data_g; + data_vg = blk.data_b; + data_vb = blk.data_a; + } + else if (omitted_component == 1) + { + partition_averages[0] = partition_averages[0].swz<0, 2, 3>(); + partition_averages[1] = partition_averages[1].swz<0, 2, 3>(); + partition_averages[2] = partition_averages[2].swz<0, 2, 3>(); + partition_averages[3] = partition_averages[3].swz<0, 2, 3>(); + + data_vg = blk.data_b; + data_vb = blk.data_a; + } + else if (omitted_component == 2) + { + partition_averages[0] = partition_averages[0].swz<0, 1, 3>(); + partition_averages[1] = partition_averages[1].swz<0, 1, 3>(); + partition_averages[2] = partition_averages[2].swz<0, 1, 3>(); + partition_averages[3] = partition_averages[3].swz<0, 1, 3>(); + + data_vb = blk.data_a; + } + else + { + partition_averages[0] = partition_averages[0].swz<0, 1, 2>(); + partition_averages[1] = partition_averages[1].swz<0, 1, 2>(); + partition_averages[2] = partition_averages[2].swz<0, 1, 2>(); + partition_averages[3] = partition_averages[3].swz<0, 1, 2>(); + } + + unsigned int partition_count = pi.partition_count; + promise(partition_count > 0); + + for (unsigned int partition = 0; partition < partition_count; partition++) + { + const uint8_t *texel_indexes = pi.texels_of_partition[partition]; + unsigned int texel_count = pi.partition_texel_count[partition]; + promise(texel_count > 0); + + vfloat4 average = partition_averages[partition]; + pm[partition].avg = average; + + vfloat4 sum_xp = vfloat4::zero(); + vfloat4 sum_yp = vfloat4::zero(); + vfloat4 sum_zp = vfloat4::zero(); + + for (unsigned int i = 0; i < texel_count; i++) + { + unsigned int iwt = texel_indexes[i]; + + vfloat4 texel_datum = vfloat3(data_vr[iwt], + data_vg[iwt], + data_vb[iwt]); + texel_datum = texel_datum - average; + + vfloat4 zero = vfloat4::zero(); + + vmask4 tdm0 = texel_datum.swz<0,0,0,0>() > zero; + sum_xp += select(zero, texel_datum, tdm0); + + vmask4 tdm1 = texel_datum.swz<1,1,1,1>() > zero; + sum_yp += select(zero, texel_datum, tdm1); + + vmask4 tdm2 = texel_datum.swz<2,2,2,2>() > zero; + sum_zp += select(zero, texel_datum, tdm2); + } + + vfloat4 prod_xp = dot(sum_xp, sum_xp); + vfloat4 prod_yp = dot(sum_yp, sum_yp); + vfloat4 prod_zp = dot(sum_zp, sum_zp); + + vfloat4 best_vector = sum_xp; + vfloat4 best_sum = prod_xp; + + vmask4 mask = prod_yp > best_sum; + best_vector = select(best_vector, sum_yp, mask); + best_sum = select(best_sum, prod_yp, mask); + + mask = prod_zp > best_sum; + best_vector = select(best_vector, sum_zp, mask); + + pm[partition].dir = best_vector; + } +} + +/* See header for documentation. */ +void compute_avgs_and_dirs_3_comp_rgb( + const partition_info& pi, + const image_block& blk, + partition_metrics pm[BLOCK_MAX_PARTITIONS] +) { + unsigned int partition_count = pi.partition_count; + promise(partition_count > 0); + + // Pre-compute partition_averages + vfloat4 partition_averages[BLOCK_MAX_PARTITIONS]; + compute_partition_averages_rgb(pi, blk, partition_averages); + + for (unsigned int partition = 0; partition < partition_count; partition++) + { + const uint8_t *texel_indexes = pi.texels_of_partition[partition]; + unsigned int texel_count = pi.partition_texel_count[partition]; + promise(texel_count > 0); + + vfloat4 average = partition_averages[partition]; + pm[partition].avg = average; + + vfloat4 sum_xp = vfloat4::zero(); + vfloat4 sum_yp = vfloat4::zero(); + vfloat4 sum_zp = vfloat4::zero(); + + for (unsigned int i = 0; i < texel_count; i++) + { + unsigned int iwt = texel_indexes[i]; + + vfloat4 texel_datum = blk.texel3(iwt); + texel_datum = texel_datum - average; + + vfloat4 zero = vfloat4::zero(); + + vmask4 tdm0 = texel_datum.swz<0,0,0,0>() > zero; + sum_xp += select(zero, texel_datum, tdm0); + + vmask4 tdm1 = texel_datum.swz<1,1,1,1>() > zero; + sum_yp += select(zero, texel_datum, tdm1); + + vmask4 tdm2 = texel_datum.swz<2,2,2,2>() > zero; + sum_zp += select(zero, texel_datum, tdm2); + } + + vfloat4 prod_xp = dot(sum_xp, sum_xp); + vfloat4 prod_yp = dot(sum_yp, sum_yp); + vfloat4 prod_zp = dot(sum_zp, sum_zp); + + vfloat4 best_vector = sum_xp; + vfloat4 best_sum = prod_xp; + + vmask4 mask = prod_yp > best_sum; + best_vector = select(best_vector, sum_yp, mask); + best_sum = select(best_sum, prod_yp, mask); + + mask = prod_zp > best_sum; + best_vector = select(best_vector, sum_zp, mask); + + pm[partition].dir = best_vector; + } +} + +/* See header for documentation. */ +void compute_avgs_and_dirs_2_comp( + const partition_info& pt, + const image_block& blk, + unsigned int component1, + unsigned int component2, + partition_metrics pm[BLOCK_MAX_PARTITIONS] +) { + vfloat4 average; + + const float* data_vr = nullptr; + const float* data_vg = nullptr; + + if (component1 == 0 && component2 == 1) + { + average = blk.data_mean.swz<0, 1>(); + + data_vr = blk.data_r; + data_vg = blk.data_g; + } + else if (component1 == 0 && component2 == 2) + { + average = blk.data_mean.swz<0, 2>(); + + data_vr = blk.data_r; + data_vg = blk.data_b; + } + else // (component1 == 1 && component2 == 2) + { + assert(component1 == 1 && component2 == 2); + + average = blk.data_mean.swz<1, 2>(); + + data_vr = blk.data_g; + data_vg = blk.data_b; + } + + unsigned int partition_count = pt.partition_count; + promise(partition_count > 0); + + for (unsigned int partition = 0; partition < partition_count; partition++) + { + const uint8_t *texel_indexes = pt.texels_of_partition[partition]; + unsigned int texel_count = pt.partition_texel_count[partition]; + promise(texel_count > 0); + + // Only compute a partition mean if more than one partition + if (partition_count > 1) + { + average = vfloat4::zero(); + for (unsigned int i = 0; i < texel_count; i++) + { + unsigned int iwt = texel_indexes[i]; + average += vfloat2(data_vr[iwt], data_vg[iwt]); + } + + average = average / static_cast<float>(texel_count); + } + + pm[partition].avg = average; + + vfloat4 sum_xp = vfloat4::zero(); + vfloat4 sum_yp = vfloat4::zero(); + + for (unsigned int i = 0; i < texel_count; i++) + { + unsigned int iwt = texel_indexes[i]; + vfloat4 texel_datum = vfloat2(data_vr[iwt], data_vg[iwt]); + texel_datum = texel_datum - average; + + vfloat4 zero = vfloat4::zero(); + + vmask4 tdm0 = texel_datum.swz<0,0,0,0>() > zero; + sum_xp += select(zero, texel_datum, tdm0); + + vmask4 tdm1 = texel_datum.swz<1,1,1,1>() > zero; + sum_yp += select(zero, texel_datum, tdm1); + } + + vfloat4 prod_xp = dot(sum_xp, sum_xp); + vfloat4 prod_yp = dot(sum_yp, sum_yp); + + vfloat4 best_vector = sum_xp; + vfloat4 best_sum = prod_xp; + + vmask4 mask = prod_yp > best_sum; + best_vector = select(best_vector, sum_yp, mask); + + pm[partition].dir = best_vector; + } +} + +/* See header for documentation. */ +void compute_error_squared_rgba( + const partition_info& pi, + const image_block& blk, + const processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS], + const processed_line4 samec_plines[BLOCK_MAX_PARTITIONS], + float uncor_lengths[BLOCK_MAX_PARTITIONS], + float samec_lengths[BLOCK_MAX_PARTITIONS], + float& uncor_error, + float& samec_error +) { + unsigned int partition_count = pi.partition_count; + promise(partition_count > 0); + + vfloatacc uncor_errorsumv = vfloatacc::zero(); + vfloatacc samec_errorsumv = vfloatacc::zero(); + + for (unsigned int partition = 0; partition < partition_count; partition++) + { + const uint8_t *texel_indexes = pi.texels_of_partition[partition]; + + float uncor_loparam = 1e10f; + float uncor_hiparam = -1e10f; + + float samec_loparam = 1e10f; + float samec_hiparam = -1e10f; + + processed_line4 l_uncor = uncor_plines[partition]; + processed_line4 l_samec = samec_plines[partition]; + + unsigned int texel_count = pi.partition_texel_count[partition]; + promise(texel_count > 0); + + // Vectorize some useful scalar inputs + vfloat l_uncor_bs0(l_uncor.bs.lane<0>()); + vfloat l_uncor_bs1(l_uncor.bs.lane<1>()); + vfloat l_uncor_bs2(l_uncor.bs.lane<2>()); + vfloat l_uncor_bs3(l_uncor.bs.lane<3>()); + + vfloat l_uncor_amod0(l_uncor.amod.lane<0>()); + vfloat l_uncor_amod1(l_uncor.amod.lane<1>()); + vfloat l_uncor_amod2(l_uncor.amod.lane<2>()); + vfloat l_uncor_amod3(l_uncor.amod.lane<3>()); + + vfloat l_samec_bs0(l_samec.bs.lane<0>()); + vfloat l_samec_bs1(l_samec.bs.lane<1>()); + vfloat l_samec_bs2(l_samec.bs.lane<2>()); + vfloat l_samec_bs3(l_samec.bs.lane<3>()); + + assert(all(l_samec.amod == vfloat4(0.0f))); + + vfloat uncor_loparamv(1e10f); + vfloat uncor_hiparamv(-1e10f); + + vfloat samec_loparamv(1e10f); + vfloat samec_hiparamv(-1e10f); + + vfloat ew_r(blk.channel_weight.lane<0>()); + vfloat ew_g(blk.channel_weight.lane<1>()); + vfloat ew_b(blk.channel_weight.lane<2>()); + vfloat ew_a(blk.channel_weight.lane<3>()); + + // This implementation over-shoots, but this is safe as we initialize the texel_indexes + // array to extend the last value. This means min/max are not impacted, but we need to mask + // out the dummy values when we compute the line weighting. + vint lane_ids = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vmask mask = lane_ids < vint(texel_count); + vint texel_idxs(texel_indexes + i); + + vfloat data_r = gatherf(blk.data_r, texel_idxs); + vfloat data_g = gatherf(blk.data_g, texel_idxs); + vfloat data_b = gatherf(blk.data_b, texel_idxs); + vfloat data_a = gatherf(blk.data_a, texel_idxs); + + vfloat uncor_param = (data_r * l_uncor_bs0) + + (data_g * l_uncor_bs1) + + (data_b * l_uncor_bs2) + + (data_a * l_uncor_bs3); + + uncor_loparamv = min(uncor_param, uncor_loparamv); + uncor_hiparamv = max(uncor_param, uncor_hiparamv); + + vfloat uncor_dist0 = (l_uncor_amod0 - data_r) + + (uncor_param * l_uncor_bs0); + vfloat uncor_dist1 = (l_uncor_amod1 - data_g) + + (uncor_param * l_uncor_bs1); + vfloat uncor_dist2 = (l_uncor_amod2 - data_b) + + (uncor_param * l_uncor_bs2); + vfloat uncor_dist3 = (l_uncor_amod3 - data_a) + + (uncor_param * l_uncor_bs3); + + vfloat uncor_err = (ew_r * uncor_dist0 * uncor_dist0) + + (ew_g * uncor_dist1 * uncor_dist1) + + (ew_b * uncor_dist2 * uncor_dist2) + + (ew_a * uncor_dist3 * uncor_dist3); + + haccumulate(uncor_errorsumv, uncor_err, mask); + + // Process samechroma data + vfloat samec_param = (data_r * l_samec_bs0) + + (data_g * l_samec_bs1) + + (data_b * l_samec_bs2) + + (data_a * l_samec_bs3); + + samec_loparamv = min(samec_param, samec_loparamv); + samec_hiparamv = max(samec_param, samec_hiparamv); + + vfloat samec_dist0 = samec_param * l_samec_bs0 - data_r; + vfloat samec_dist1 = samec_param * l_samec_bs1 - data_g; + vfloat samec_dist2 = samec_param * l_samec_bs2 - data_b; + vfloat samec_dist3 = samec_param * l_samec_bs3 - data_a; + + vfloat samec_err = (ew_r * samec_dist0 * samec_dist0) + + (ew_g * samec_dist1 * samec_dist1) + + (ew_b * samec_dist2 * samec_dist2) + + (ew_a * samec_dist3 * samec_dist3); + + haccumulate(samec_errorsumv, samec_err, mask); + + lane_ids += vint(ASTCENC_SIMD_WIDTH); + } + + uncor_loparam = hmin_s(uncor_loparamv); + uncor_hiparam = hmax_s(uncor_hiparamv); + + samec_loparam = hmin_s(samec_loparamv); + samec_hiparam = hmax_s(samec_hiparamv); + + float uncor_linelen = uncor_hiparam - uncor_loparam; + float samec_linelen = samec_hiparam - samec_loparam; + + // Turn very small numbers and NaNs into a small number + uncor_lengths[partition] = astc::max(uncor_linelen, 1e-7f); + samec_lengths[partition] = astc::max(samec_linelen, 1e-7f); + } + + uncor_error = hadd_s(uncor_errorsumv); + samec_error = hadd_s(samec_errorsumv); +} + +/* See header for documentation. */ +void compute_error_squared_rgb( + const partition_info& pi, + const image_block& blk, + partition_lines3 plines[BLOCK_MAX_PARTITIONS], + float& uncor_error, + float& samec_error +) { + unsigned int partition_count = pi.partition_count; + promise(partition_count > 0); + + vfloatacc uncor_errorsumv = vfloatacc::zero(); + vfloatacc samec_errorsumv = vfloatacc::zero(); + + for (unsigned int partition = 0; partition < partition_count; partition++) + { + partition_lines3& pl = plines[partition]; + const uint8_t *texel_indexes = pi.texels_of_partition[partition]; + unsigned int texel_count = pi.partition_texel_count[partition]; + promise(texel_count > 0); + + float uncor_loparam = 1e10f; + float uncor_hiparam = -1e10f; + + float samec_loparam = 1e10f; + float samec_hiparam = -1e10f; + + processed_line3 l_uncor = pl.uncor_pline; + processed_line3 l_samec = pl.samec_pline; + + // This implementation is an example vectorization of this function. + // It works for - the codec is a 2-4% faster than not vectorizing - but + // the benefit is limited by the use of gathers and register pressure + + // Vectorize some useful scalar inputs + vfloat l_uncor_bs0(l_uncor.bs.lane<0>()); + vfloat l_uncor_bs1(l_uncor.bs.lane<1>()); + vfloat l_uncor_bs2(l_uncor.bs.lane<2>()); + + vfloat l_uncor_amod0(l_uncor.amod.lane<0>()); + vfloat l_uncor_amod1(l_uncor.amod.lane<1>()); + vfloat l_uncor_amod2(l_uncor.amod.lane<2>()); + + vfloat l_samec_bs0(l_samec.bs.lane<0>()); + vfloat l_samec_bs1(l_samec.bs.lane<1>()); + vfloat l_samec_bs2(l_samec.bs.lane<2>()); + + assert(all(l_samec.amod == vfloat4(0.0f))); + + vfloat uncor_loparamv(1e10f); + vfloat uncor_hiparamv(-1e10f); + + vfloat samec_loparamv(1e10f); + vfloat samec_hiparamv(-1e10f); + + vfloat ew_r(blk.channel_weight.lane<0>()); + vfloat ew_g(blk.channel_weight.lane<1>()); + vfloat ew_b(blk.channel_weight.lane<2>()); + + // This implementation over-shoots, but this is safe as we initialize the weights array + // to extend the last value. This means min/max are not impacted, but we need to mask + // out the dummy values when we compute the line weighting. + vint lane_ids = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vmask mask = lane_ids < vint(texel_count); + vint texel_idxs(texel_indexes + i); + + vfloat data_r = gatherf(blk.data_r, texel_idxs); + vfloat data_g = gatherf(blk.data_g, texel_idxs); + vfloat data_b = gatherf(blk.data_b, texel_idxs); + + vfloat uncor_param = (data_r * l_uncor_bs0) + + (data_g * l_uncor_bs1) + + (data_b * l_uncor_bs2); + + uncor_loparamv = min(uncor_param, uncor_loparamv); + uncor_hiparamv = max(uncor_param, uncor_hiparamv); + + vfloat uncor_dist0 = (l_uncor_amod0 - data_r) + + (uncor_param * l_uncor_bs0); + vfloat uncor_dist1 = (l_uncor_amod1 - data_g) + + (uncor_param * l_uncor_bs1); + vfloat uncor_dist2 = (l_uncor_amod2 - data_b) + + (uncor_param * l_uncor_bs2); + + vfloat uncor_err = (ew_r * uncor_dist0 * uncor_dist0) + + (ew_g * uncor_dist1 * uncor_dist1) + + (ew_b * uncor_dist2 * uncor_dist2); + + haccumulate(uncor_errorsumv, uncor_err, mask); + + // Process samechroma data + vfloat samec_param = (data_r * l_samec_bs0) + + (data_g * l_samec_bs1) + + (data_b * l_samec_bs2); + + samec_loparamv = min(samec_param, samec_loparamv); + samec_hiparamv = max(samec_param, samec_hiparamv); + + vfloat samec_dist0 = samec_param * l_samec_bs0 - data_r; + vfloat samec_dist1 = samec_param * l_samec_bs1 - data_g; + vfloat samec_dist2 = samec_param * l_samec_bs2 - data_b; + + vfloat samec_err = (ew_r * samec_dist0 * samec_dist0) + + (ew_g * samec_dist1 * samec_dist1) + + (ew_b * samec_dist2 * samec_dist2); + + haccumulate(samec_errorsumv, samec_err, mask); + + lane_ids += vint(ASTCENC_SIMD_WIDTH); + } + + uncor_loparam = hmin_s(uncor_loparamv); + uncor_hiparam = hmax_s(uncor_hiparamv); + + samec_loparam = hmin_s(samec_loparamv); + samec_hiparam = hmax_s(samec_hiparamv); + + float uncor_linelen = uncor_hiparam - uncor_loparam; + float samec_linelen = samec_hiparam - samec_loparam; + + // Turn very small numbers and NaNs into a small number + pl.uncor_line_len = astc::max(uncor_linelen, 1e-7f); + pl.samec_line_len = astc::max(samec_linelen, 1e-7f); + } + + uncor_error = hadd_s(uncor_errorsumv); + samec_error = hadd_s(samec_errorsumv); +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_block_sizes.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_block_sizes.cpp new file mode 100644 index 00000000000..b3e2efb9155 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_block_sizes.cpp @@ -0,0 +1,1210 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions to generate block size descriptor and decimation tables. + */ + +#include "astcenc_internal.h" + +/** + * @brief Decode the properties of an encoded 2D block mode. + * + * @param block_mode The encoded block mode. + * @param[out] x_weights The number of weights in the X dimension. + * @param[out] y_weights The number of weights in the Y dimension. + * @param[out] is_dual_plane True if this block mode has two weight planes. + * @param[out] quant_mode The quantization level for the weights. + * @param[out] weight_bits The storage bit count for the weights. + * + * @return Returns true if a valid mode, false otherwise. + */ +static bool decode_block_mode_2d( + unsigned int block_mode, + unsigned int& x_weights, + unsigned int& y_weights, + bool& is_dual_plane, + unsigned int& quant_mode, + unsigned int& weight_bits +) { + unsigned int base_quant_mode = (block_mode >> 4) & 1; + unsigned int H = (block_mode >> 9) & 1; + unsigned int D = (block_mode >> 10) & 1; + unsigned int A = (block_mode >> 5) & 0x3; + + x_weights = 0; + y_weights = 0; + + if ((block_mode & 3) != 0) + { + base_quant_mode |= (block_mode & 3) << 1; + unsigned int B = (block_mode >> 7) & 3; + switch ((block_mode >> 2) & 3) + { + case 0: + x_weights = B + 4; + y_weights = A + 2; + break; + case 1: + x_weights = B + 8; + y_weights = A + 2; + break; + case 2: + x_weights = A + 2; + y_weights = B + 8; + break; + case 3: + B &= 1; + if (block_mode & 0x100) + { + x_weights = B + 2; + y_weights = A + 2; + } + else + { + x_weights = A + 2; + y_weights = B + 6; + } + break; + } + } + else + { + base_quant_mode |= ((block_mode >> 2) & 3) << 1; + if (((block_mode >> 2) & 3) == 0) + { + return false; + } + + unsigned int B = (block_mode >> 9) & 3; + switch ((block_mode >> 7) & 3) + { + case 0: + x_weights = 12; + y_weights = A + 2; + break; + case 1: + x_weights = A + 2; + y_weights = 12; + break; + case 2: + x_weights = A + 6; + y_weights = B + 6; + D = 0; + H = 0; + break; + case 3: + switch ((block_mode >> 5) & 3) + { + case 0: + x_weights = 6; + y_weights = 10; + break; + case 1: + x_weights = 10; + y_weights = 6; + break; + case 2: + case 3: + return false; + } + break; + } + } + + unsigned int weight_count = x_weights * y_weights * (D + 1); + quant_mode = (base_quant_mode - 2) + 6 * H; + is_dual_plane = D != 0; + + weight_bits = get_ise_sequence_bitcount(weight_count, static_cast<quant_method>(quant_mode)); + return (weight_count <= BLOCK_MAX_WEIGHTS && + weight_bits >= BLOCK_MIN_WEIGHT_BITS && + weight_bits <= BLOCK_MAX_WEIGHT_BITS); +} + +/** + * @brief Decode the properties of an encoded 3D block mode. + * + * @param block_mode The encoded block mode. + * @param[out] x_weights The number of weights in the X dimension. + * @param[out] y_weights The number of weights in the Y dimension. + * @param[out] z_weights The number of weights in the Z dimension. + * @param[out] is_dual_plane True if this block mode has two weight planes. + * @param[out] quant_mode The quantization level for the weights. + * @param[out] weight_bits The storage bit count for the weights. + * + * @return Returns true if a valid mode, false otherwise. + */ +static bool decode_block_mode_3d( + unsigned int block_mode, + unsigned int& x_weights, + unsigned int& y_weights, + unsigned int& z_weights, + bool& is_dual_plane, + unsigned int& quant_mode, + unsigned int& weight_bits +) { + unsigned int base_quant_mode = (block_mode >> 4) & 1; + unsigned int H = (block_mode >> 9) & 1; + unsigned int D = (block_mode >> 10) & 1; + unsigned int A = (block_mode >> 5) & 0x3; + + x_weights = 0; + y_weights = 0; + z_weights = 0; + + if ((block_mode & 3) != 0) + { + base_quant_mode |= (block_mode & 3) << 1; + unsigned int B = (block_mode >> 7) & 3; + unsigned int C = (block_mode >> 2) & 0x3; + x_weights = A + 2; + y_weights = B + 2; + z_weights = C + 2; + } + else + { + base_quant_mode |= ((block_mode >> 2) & 3) << 1; + if (((block_mode >> 2) & 3) == 0) + { + return false; + } + + int B = (block_mode >> 9) & 3; + if (((block_mode >> 7) & 3) != 3) + { + D = 0; + H = 0; + } + switch ((block_mode >> 7) & 3) + { + case 0: + x_weights = 6; + y_weights = B + 2; + z_weights = A + 2; + break; + case 1: + x_weights = A + 2; + y_weights = 6; + z_weights = B + 2; + break; + case 2: + x_weights = A + 2; + y_weights = B + 2; + z_weights = 6; + break; + case 3: + x_weights = 2; + y_weights = 2; + z_weights = 2; + switch ((block_mode >> 5) & 3) + { + case 0: + x_weights = 6; + break; + case 1: + y_weights = 6; + break; + case 2: + z_weights = 6; + break; + case 3: + return false; + } + break; + } + } + + unsigned int weight_count = x_weights * y_weights * z_weights * (D + 1); + quant_mode = (base_quant_mode - 2) + 6 * H; + is_dual_plane = D != 0; + + weight_bits = get_ise_sequence_bitcount(weight_count, static_cast<quant_method>(quant_mode)); + return (weight_count <= BLOCK_MAX_WEIGHTS && + weight_bits >= BLOCK_MIN_WEIGHT_BITS && + weight_bits <= BLOCK_MAX_WEIGHT_BITS); +} + +/** + * @brief Create a 2D decimation entry for a block-size and weight-decimation pair. + * + * @param x_texels The number of texels in the X dimension. + * @param y_texels The number of texels in the Y dimension. + * @param x_weights The number of weights in the X dimension. + * @param y_weights The number of weights in the Y dimension. + * @param[out] di The decimation info structure to populate. + * @param[out] wb The decimation table init scratch working buffers. + */ +static void init_decimation_info_2d( + unsigned int x_texels, + unsigned int y_texels, + unsigned int x_weights, + unsigned int y_weights, + decimation_info& di, + dt_init_working_buffers& wb +) { + unsigned int texels_per_block = x_texels * y_texels; + unsigned int weights_per_block = x_weights * y_weights; + + uint8_t max_texel_count_of_weight = 0; + + promise(weights_per_block > 0); + promise(texels_per_block > 0); + promise(x_texels > 0); + promise(y_texels > 0); + + for (unsigned int i = 0; i < weights_per_block; i++) + { + wb.texel_count_of_weight[i] = 0; + } + + for (unsigned int i = 0; i < texels_per_block; i++) + { + wb.weight_count_of_texel[i] = 0; + } + + for (unsigned int y = 0; y < y_texels; y++) + { + for (unsigned int x = 0; x < x_texels; x++) + { + unsigned int texel = y * x_texels + x; + + unsigned int x_weight = (((1024 + x_texels / 2) / (x_texels - 1)) * x * (x_weights - 1) + 32) >> 6; + unsigned int y_weight = (((1024 + y_texels / 2) / (y_texels - 1)) * y * (y_weights - 1) + 32) >> 6; + + unsigned int x_weight_frac = x_weight & 0xF; + unsigned int y_weight_frac = y_weight & 0xF; + unsigned int x_weight_int = x_weight >> 4; + unsigned int y_weight_int = y_weight >> 4; + + unsigned int qweight[4]; + qweight[0] = x_weight_int + y_weight_int * x_weights; + qweight[1] = qweight[0] + 1; + qweight[2] = qweight[0] + x_weights; + qweight[3] = qweight[2] + 1; + + // Truncated-precision bilinear interpolation + unsigned int prod = x_weight_frac * y_weight_frac; + + unsigned int weight[4]; + weight[3] = (prod + 8) >> 4; + weight[1] = x_weight_frac - weight[3]; + weight[2] = y_weight_frac - weight[3]; + weight[0] = 16 - x_weight_frac - y_weight_frac + weight[3]; + + for (unsigned int i = 0; i < 4; i++) + { + if (weight[i] != 0) + { + wb.grid_weights_of_texel[texel][wb.weight_count_of_texel[texel]] = static_cast<uint8_t>(qweight[i]); + wb.weights_of_texel[texel][wb.weight_count_of_texel[texel]] = static_cast<uint8_t>(weight[i]); + wb.weight_count_of_texel[texel]++; + wb.texels_of_weight[qweight[i]][wb.texel_count_of_weight[qweight[i]]] = static_cast<uint8_t>(texel); + wb.texel_weights_of_weight[qweight[i]][wb.texel_count_of_weight[qweight[i]]] = static_cast<uint8_t>(weight[i]); + wb.texel_count_of_weight[qweight[i]]++; + max_texel_count_of_weight = astc::max(max_texel_count_of_weight, wb.texel_count_of_weight[qweight[i]]); + } + } + } + } + + uint8_t max_texel_weight_count = 0; + for (unsigned int i = 0; i < texels_per_block; i++) + { + di.texel_weight_count[i] = wb.weight_count_of_texel[i]; + max_texel_weight_count = astc::max(max_texel_weight_count, di.texel_weight_count[i]); + + for (unsigned int j = 0; j < wb.weight_count_of_texel[i]; j++) + { + di.texel_weights_int_4t[j][i] = wb.weights_of_texel[i][j]; + di.texel_weights_float_4t[j][i] = static_cast<float>(wb.weights_of_texel[i][j]) * (1.0f / WEIGHTS_TEXEL_SUM); + di.texel_weights_4t[j][i] = wb.grid_weights_of_texel[i][j]; + } + + // Init all 4 entries so we can rely on zeros for vectorization + for (unsigned int j = wb.weight_count_of_texel[i]; j < 4; j++) + { + di.texel_weights_int_4t[j][i] = 0; + di.texel_weights_float_4t[j][i] = 0.0f; + di.texel_weights_4t[j][i] = 0; + } + } + + di.max_texel_weight_count = max_texel_weight_count; + + for (unsigned int i = 0; i < weights_per_block; i++) + { + unsigned int texel_count_wt = wb.texel_count_of_weight[i]; + di.weight_texel_count[i] = static_cast<uint8_t>(texel_count_wt); + + for (unsigned int j = 0; j < texel_count_wt; j++) + { + uint8_t texel = wb.texels_of_weight[i][j]; + + // Create transposed versions of these for better vectorization + di.weight_texel[j][i] = texel; + di.weights_flt[j][i] = static_cast<float>(wb.texel_weights_of_weight[i][j]); + + // perform a layer of array unrolling. An aspect of this unrolling is that + // one of the texel-weight indexes is an identity-mapped index; we will use this + // fact to reorder the indexes so that the first one is the identity index. + int swap_idx = -1; + for (unsigned int k = 0; k < 4; k++) + { + uint8_t dttw = di.texel_weights_4t[k][texel]; + float dttwf = di.texel_weights_float_4t[k][texel]; + if (dttw == i && dttwf != 0.0f) + { + swap_idx = k; + } + di.texel_weights_texel[i][j][k] = dttw; + di.texel_weights_float_texel[i][j][k] = dttwf; + } + + if (swap_idx != 0) + { + uint8_t vi = di.texel_weights_texel[i][j][0]; + float vf = di.texel_weights_float_texel[i][j][0]; + di.texel_weights_texel[i][j][0] = di.texel_weights_texel[i][j][swap_idx]; + di.texel_weights_float_texel[i][j][0] = di.texel_weights_float_texel[i][j][swap_idx]; + di.texel_weights_texel[i][j][swap_idx] = vi; + di.texel_weights_float_texel[i][j][swap_idx] = vf; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + // Match last texel in active lane in SIMD group, for better gathers + uint8_t last_texel = di.weight_texel[texel_count_wt - 1][i]; + for (unsigned int j = texel_count_wt; j < max_texel_count_of_weight; j++) + { + di.weight_texel[j][i] = last_texel; + di.weights_flt[j][i] = 0.0f; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + unsigned int texels_per_block_simd = round_up_to_simd_multiple_vla(texels_per_block); + for (unsigned int i = texels_per_block; i < texels_per_block_simd; i++) + { + di.texel_weight_count[i] = 0; + + for (unsigned int j = 0; j < 4; j++) + { + di.texel_weights_float_4t[j][i] = 0; + di.texel_weights_4t[j][i] = 0; + di.texel_weights_int_4t[j][i] = 0; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + // Match last texel in active lane in SIMD group, for better gathers + unsigned int last_texel_count_wt = wb.texel_count_of_weight[weights_per_block - 1]; + uint8_t last_texel = di.weight_texel[last_texel_count_wt - 1][weights_per_block - 1]; + + unsigned int weights_per_block_simd = round_up_to_simd_multiple_vla(weights_per_block); + for (unsigned int i = weights_per_block; i < weights_per_block_simd; i++) + { + di.weight_texel_count[i] = 0; + + for (unsigned int j = 0; j < max_texel_count_of_weight; j++) + { + di.weight_texel[j][i] = last_texel; + di.weights_flt[j][i] = 0.0f; + } + } + + di.texel_count = static_cast<uint8_t>(texels_per_block); + di.weight_count = static_cast<uint8_t>(weights_per_block); + di.weight_x = static_cast<uint8_t>(x_weights); + di.weight_y = static_cast<uint8_t>(y_weights); + di.weight_z = 1; +} + +/** + * @brief Create a 3D decimation entry for a block-size and weight-decimation pair. + * + * @param x_texels The number of texels in the X dimension. + * @param y_texels The number of texels in the Y dimension. + * @param z_texels The number of texels in the Z dimension. + * @param x_weights The number of weights in the X dimension. + * @param y_weights The number of weights in the Y dimension. + * @param z_weights The number of weights in the Z dimension. + * @param[out] di The decimation info structure to populate. + @param[out] wb The decimation table init scratch working buffers. + */ +static void init_decimation_info_3d( + unsigned int x_texels, + unsigned int y_texels, + unsigned int z_texels, + unsigned int x_weights, + unsigned int y_weights, + unsigned int z_weights, + decimation_info& di, + dt_init_working_buffers& wb +) { + unsigned int texels_per_block = x_texels * y_texels * z_texels; + unsigned int weights_per_block = x_weights * y_weights * z_weights; + + uint8_t max_texel_count_of_weight = 0; + + promise(weights_per_block > 0); + promise(texels_per_block > 0); + + for (unsigned int i = 0; i < weights_per_block; i++) + { + wb.texel_count_of_weight[i] = 0; + } + + for (unsigned int i = 0; i < texels_per_block; i++) + { + wb.weight_count_of_texel[i] = 0; + } + + for (unsigned int z = 0; z < z_texels; z++) + { + for (unsigned int y = 0; y < y_texels; y++) + { + for (unsigned int x = 0; x < x_texels; x++) + { + int texel = (z * y_texels + y) * x_texels + x; + + int x_weight = (((1024 + x_texels / 2) / (x_texels - 1)) * x * (x_weights - 1) + 32) >> 6; + int y_weight = (((1024 + y_texels / 2) / (y_texels - 1)) * y * (y_weights - 1) + 32) >> 6; + int z_weight = (((1024 + z_texels / 2) / (z_texels - 1)) * z * (z_weights - 1) + 32) >> 6; + + int x_weight_frac = x_weight & 0xF; + int y_weight_frac = y_weight & 0xF; + int z_weight_frac = z_weight & 0xF; + int x_weight_int = x_weight >> 4; + int y_weight_int = y_weight >> 4; + int z_weight_int = z_weight >> 4; + int qweight[4]; + int weight[4]; + qweight[0] = (z_weight_int * y_weights + y_weight_int) * x_weights + x_weight_int; + qweight[3] = ((z_weight_int + 1) * y_weights + (y_weight_int + 1)) * x_weights + (x_weight_int + 1); + + // simplex interpolation + int fs = x_weight_frac; + int ft = y_weight_frac; + int fp = z_weight_frac; + + int cas = ((fs > ft) << 2) + ((ft > fp) << 1) + ((fs > fp)); + int N = x_weights; + int NM = x_weights * y_weights; + + int s1, s2, w0, w1, w2, w3; + switch (cas) + { + case 7: + s1 = 1; + s2 = N; + w0 = 16 - fs; + w1 = fs - ft; + w2 = ft - fp; + w3 = fp; + break; + case 3: + s1 = N; + s2 = 1; + w0 = 16 - ft; + w1 = ft - fs; + w2 = fs - fp; + w3 = fp; + break; + case 5: + s1 = 1; + s2 = NM; + w0 = 16 - fs; + w1 = fs - fp; + w2 = fp - ft; + w3 = ft; + break; + case 4: + s1 = NM; + s2 = 1; + w0 = 16 - fp; + w1 = fp - fs; + w2 = fs - ft; + w3 = ft; + break; + case 2: + s1 = N; + s2 = NM; + w0 = 16 - ft; + w1 = ft - fp; + w2 = fp - fs; + w3 = fs; + break; + case 0: + s1 = NM; + s2 = N; + w0 = 16 - fp; + w1 = fp - ft; + w2 = ft - fs; + w3 = fs; + break; + default: + s1 = NM; + s2 = N; + w0 = 16 - fp; + w1 = fp - ft; + w2 = ft - fs; + w3 = fs; + break; + } + + qweight[1] = qweight[0] + s1; + qweight[2] = qweight[1] + s2; + weight[0] = w0; + weight[1] = w1; + weight[2] = w2; + weight[3] = w3; + + for (unsigned int i = 0; i < 4; i++) + { + if (weight[i] != 0) + { + wb.grid_weights_of_texel[texel][wb.weight_count_of_texel[texel]] = static_cast<uint8_t>(qweight[i]); + wb.weights_of_texel[texel][wb.weight_count_of_texel[texel]] = static_cast<uint8_t>(weight[i]); + wb.weight_count_of_texel[texel]++; + wb.texels_of_weight[qweight[i]][wb.texel_count_of_weight[qweight[i]]] = static_cast<uint8_t>(texel); + wb.texel_weights_of_weight[qweight[i]][wb.texel_count_of_weight[qweight[i]]] = static_cast<uint8_t>(weight[i]); + wb.texel_count_of_weight[qweight[i]]++; + max_texel_count_of_weight = astc::max(max_texel_count_of_weight, wb.texel_count_of_weight[qweight[i]]); + } + } + } + } + } + + uint8_t max_texel_weight_count = 0; + for (unsigned int i = 0; i < texels_per_block; i++) + { + di.texel_weight_count[i] = wb.weight_count_of_texel[i]; + max_texel_weight_count = astc::max(max_texel_weight_count, di.texel_weight_count[i]); + + // Init all 4 entries so we can rely on zeros for vectorization + for (unsigned int j = 0; j < 4; j++) + { + di.texel_weights_int_4t[j][i] = 0; + di.texel_weights_float_4t[j][i] = 0.0f; + di.texel_weights_4t[j][i] = 0; + } + + for (unsigned int j = 0; j < wb.weight_count_of_texel[i]; j++) + { + di.texel_weights_int_4t[j][i] = wb.weights_of_texel[i][j]; + di.texel_weights_float_4t[j][i] = static_cast<float>(wb.weights_of_texel[i][j]) * (1.0f / WEIGHTS_TEXEL_SUM); + di.texel_weights_4t[j][i] = wb.grid_weights_of_texel[i][j]; + } + } + + di.max_texel_weight_count = max_texel_weight_count; + + for (unsigned int i = 0; i < weights_per_block; i++) + { + unsigned int texel_count_wt = wb.texel_count_of_weight[i]; + di.weight_texel_count[i] = static_cast<uint8_t>(texel_count_wt); + + for (unsigned int j = 0; j < texel_count_wt; j++) + { + unsigned int texel = wb.texels_of_weight[i][j]; + + // Create transposed versions of these for better vectorization + di.weight_texel[j][i] = static_cast<uint8_t>(texel); + di.weights_flt[j][i] = static_cast<float>(wb.texel_weights_of_weight[i][j]); + + // perform a layer of array unrolling. An aspect of this unrolling is that + // one of the texel-weight indexes is an identity-mapped index; we will use this + // fact to reorder the indexes so that the first one is the identity index. + int swap_idx = -1; + for (unsigned int k = 0; k < 4; k++) + { + uint8_t dttw = di.texel_weights_4t[k][texel]; + float dttwf = di.texel_weights_float_4t[k][texel]; + if (dttw == i && dttwf != 0.0f) + { + swap_idx = k; + } + di.texel_weights_texel[i][j][k] = dttw; + di.texel_weights_float_texel[i][j][k] = dttwf; + } + + if (swap_idx != 0) + { + uint8_t vi = di.texel_weights_texel[i][j][0]; + float vf = di.texel_weights_float_texel[i][j][0]; + di.texel_weights_texel[i][j][0] = di.texel_weights_texel[i][j][swap_idx]; + di.texel_weights_float_texel[i][j][0] = di.texel_weights_float_texel[i][j][swap_idx]; + di.texel_weights_texel[i][j][swap_idx] = vi; + di.texel_weights_float_texel[i][j][swap_idx] = vf; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + // Match last texel in active lane in SIMD group, for better gathers + uint8_t last_texel = di.weight_texel[texel_count_wt - 1][i]; + for (unsigned int j = texel_count_wt; j < max_texel_count_of_weight; j++) + { + di.weight_texel[j][i] = last_texel; + di.weights_flt[j][i] = 0.0f; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + unsigned int texels_per_block_simd = round_up_to_simd_multiple_vla(texels_per_block); + for (unsigned int i = texels_per_block; i < texels_per_block_simd; i++) + { + di.texel_weight_count[i] = 0; + + for (unsigned int j = 0; j < 4; j++) + { + di.texel_weights_float_4t[j][i] = 0; + di.texel_weights_4t[j][i] = 0; + di.texel_weights_int_4t[j][i] = 0; + } + } + + // Initialize array tail so we can over-fetch with SIMD later to avoid loop tails + // Match last texel in active lane in SIMD group, for better gathers + int last_texel_count_wt = wb.texel_count_of_weight[weights_per_block - 1]; + uint8_t last_texel = di.weight_texel[last_texel_count_wt - 1][weights_per_block - 1]; + + unsigned int weights_per_block_simd = round_up_to_simd_multiple_vla(weights_per_block); + for (unsigned int i = weights_per_block; i < weights_per_block_simd; i++) + { + di.weight_texel_count[i] = 0; + + for (int j = 0; j < max_texel_count_of_weight; j++) + { + di.weight_texel[j][i] = last_texel; + di.weights_flt[j][i] = 0.0f; + } + } + + di.texel_count = static_cast<uint8_t>(texels_per_block); + di.weight_count = static_cast<uint8_t>(weights_per_block); + di.weight_x = static_cast<uint8_t>(x_weights); + di.weight_y = static_cast<uint8_t>(y_weights); + di.weight_z = static_cast<uint8_t>(z_weights); +} + +/** + * @brief Assign the texels to use for kmeans clustering. + * + * The max limit is @c BLOCK_MAX_KMEANS_TEXELS; above this a random selection is used. + * The @c bsd.texel_count is an input and must be populated beforehand. + * + * @param[in,out] bsd The block size descriptor to populate. + */ +static void assign_kmeans_texels( + block_size_descriptor& bsd +) { + // Use all texels for kmeans on a small block + if (bsd.texel_count <= BLOCK_MAX_KMEANS_TEXELS) + { + for (uint8_t i = 0; i < bsd.texel_count; i++) + { + bsd.kmeans_texels[i] = i; + } + + return; + } + + // Select a random subset of BLOCK_MAX_KMEANS_TEXELS for kmeans on a large block + uint64_t rng_state[2]; + astc::rand_init(rng_state); + + // Initialize array used for tracking used indices + bool seen[BLOCK_MAX_TEXELS]; + for (uint8_t i = 0; i < bsd.texel_count; i++) + { + seen[i] = false; + } + + // Assign 64 random indices, retrying if we see repeats + unsigned int arr_elements_set = 0; + while (arr_elements_set < BLOCK_MAX_KMEANS_TEXELS) + { + uint8_t texel = static_cast<uint8_t>(astc::rand(rng_state)); + texel = texel % bsd.texel_count; + if (!seen[texel]) + { + bsd.kmeans_texels[arr_elements_set++] = texel; + seen[texel] = true; + } + } +} + +/** + * @brief Allocate a single 2D decimation table entry. + * + * @param x_texels The number of texels in the X dimension. + * @param y_texels The number of texels in the Y dimension. + * @param x_weights The number of weights in the X dimension. + * @param y_weights The number of weights in the Y dimension. + * @param bsd The block size descriptor we are populating. + * @param wb The decimation table init scratch working buffers. + * @param index The packed array index to populate. + */ +static void construct_dt_entry_2d( + unsigned int x_texels, + unsigned int y_texels, + unsigned int x_weights, + unsigned int y_weights, + block_size_descriptor& bsd, + dt_init_working_buffers& wb, + unsigned int index +) { + unsigned int weight_count = x_weights * y_weights; + assert(weight_count <= BLOCK_MAX_WEIGHTS); + + bool try_2planes = (2 * weight_count) <= BLOCK_MAX_WEIGHTS; + + decimation_info& di = bsd.decimation_tables[index]; + init_decimation_info_2d(x_texels, y_texels, x_weights, y_weights, di, wb); + + int maxprec_1plane = -1; + int maxprec_2planes = -1; + for (int i = 0; i < 12; i++) + { + unsigned int bits_1plane = get_ise_sequence_bitcount(weight_count, static_cast<quant_method>(i)); + if (bits_1plane >= BLOCK_MIN_WEIGHT_BITS && bits_1plane <= BLOCK_MAX_WEIGHT_BITS) + { + maxprec_1plane = i; + } + + if (try_2planes) + { + unsigned int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, static_cast<quant_method>(i)); + if (bits_2planes >= BLOCK_MIN_WEIGHT_BITS && bits_2planes <= BLOCK_MAX_WEIGHT_BITS) + { + maxprec_2planes = i; + } + } + } + + // At least one of the two should be valid ... + assert(maxprec_1plane >= 0 || maxprec_2planes >= 0); + bsd.decimation_modes[index].maxprec_1plane = static_cast<int8_t>(maxprec_1plane); + bsd.decimation_modes[index].maxprec_2planes = static_cast<int8_t>(maxprec_2planes); + bsd.decimation_modes[index].refprec_1_plane = 0; + bsd.decimation_modes[index].refprec_2_planes = 0; +} + +/** + * @brief Allocate block modes and decimation tables for a single 2D block size. + * + * @param x_texels The number of texels in the X dimension. + * @param y_texels The number of texels in the Y dimension. + * @param can_omit_modes Can we discard modes that astcenc won't use, even if legal? + * @param mode_cutoff Percentile cutoff in range [0,1]. Low values more likely to be used. + * @param[out] bsd The block size descriptor to populate. + */ +static void construct_block_size_descriptor_2d( + unsigned int x_texels, + unsigned int y_texels, + bool can_omit_modes, + float mode_cutoff, + block_size_descriptor& bsd +) { + // Store a remap table for storing packed decimation modes. + // Indexing uses [Y * 16 + X] and max size for each axis is 12. + static const unsigned int MAX_DMI = 12 * 16 + 12; + int decimation_mode_index[MAX_DMI]; + + dt_init_working_buffers* wb = new dt_init_working_buffers; + + bsd.xdim = static_cast<uint8_t>(x_texels); + bsd.ydim = static_cast<uint8_t>(y_texels); + bsd.zdim = 1; + bsd.texel_count = static_cast<uint8_t>(x_texels * y_texels); + + for (unsigned int i = 0; i < MAX_DMI; i++) + { + decimation_mode_index[i] = -1; + } + + // Gather all the decimation grids that can be used with the current block +#if !defined(ASTCENC_DECOMPRESS_ONLY) + const float *percentiles = get_2d_percentile_table(x_texels, y_texels); + float always_cutoff = 0.0f; +#else + // Unused in decompress-only builds + (void)can_omit_modes; + (void)mode_cutoff; +#endif + + // Construct the list of block formats referencing the decimation tables + unsigned int packed_bm_idx = 0; + unsigned int packed_dm_idx = 0; + + // Trackers + unsigned int bm_counts[4] { 0 }; + unsigned int dm_counts[4] { 0 }; + + // Clear the list to a known-bad value + for (unsigned int i = 0; i < WEIGHTS_MAX_BLOCK_MODES; i++) + { + bsd.block_mode_packed_index[i] = BLOCK_BAD_BLOCK_MODE; + } + + // Iterate four times to build a usefully ordered list: + // - Pass 0 - keep selected single plane "always" block modes + // - Pass 1 - keep selected single plane "non-always" block modes + // - Pass 2 - keep select dual plane block modes + // - Pass 3 - keep everything else that's legal + unsigned int limit = can_omit_modes ? 3 : 4; + for (unsigned int j = 0; j < limit; j ++) + { + for (unsigned int i = 0; i < WEIGHTS_MAX_BLOCK_MODES; i++) + { + // Skip modes we've already included in a previous pass + if (bsd.block_mode_packed_index[i] != BLOCK_BAD_BLOCK_MODE) + { + continue; + } + + // Decode parameters + unsigned int x_weights; + unsigned int y_weights; + bool is_dual_plane; + unsigned int quant_mode; + unsigned int weight_bits; + bool valid = decode_block_mode_2d(i, x_weights, y_weights, is_dual_plane, quant_mode, weight_bits); + + // Always skip invalid encodings for the current block size + if (!valid || (x_weights > x_texels) || (y_weights > y_texels)) + { + continue; + } + + // Selectively skip dual plane encodings + if (((j <= 1) && is_dual_plane) || (j == 2 && !is_dual_plane)) + { + continue; + } + + // Always skip encodings we can't physically encode based on + // generic encoding bit availability + if (is_dual_plane) + { + // This is the only check we need as only support 1 partition + if ((109 - weight_bits) <= 0) + { + continue; + } + } + else + { + // This is conservative - fewer bits may be available for > 1 partition + if ((111 - weight_bits) <= 0) + { + continue; + } + } + + // Selectively skip encodings based on percentile + bool percentile_hit = false; + #if !defined(ASTCENC_DECOMPRESS_ONLY) + if (j == 0) + { + percentile_hit = percentiles[i] <= always_cutoff; + } + else + { + percentile_hit = percentiles[i] <= mode_cutoff; + } + #endif + + if (j != 3 && !percentile_hit) + { + continue; + } + + // Allocate and initialize the decimation table entry if we've not used it yet + int decimation_mode = decimation_mode_index[y_weights * 16 + x_weights]; + if (decimation_mode < 0) + { + construct_dt_entry_2d(x_texels, y_texels, x_weights, y_weights, bsd, *wb, packed_dm_idx); + decimation_mode_index[y_weights * 16 + x_weights] = packed_dm_idx; + decimation_mode = packed_dm_idx; + + dm_counts[j]++; + packed_dm_idx++; + } + + auto& bm = bsd.block_modes[packed_bm_idx]; + + bm.decimation_mode = static_cast<uint8_t>(decimation_mode); + bm.quant_mode = static_cast<uint8_t>(quant_mode); + bm.is_dual_plane = static_cast<uint8_t>(is_dual_plane); + bm.weight_bits = static_cast<uint8_t>(weight_bits); + bm.mode_index = static_cast<uint16_t>(i); + + auto& dm = bsd.decimation_modes[decimation_mode]; + + if (is_dual_plane) + { + dm.set_ref_2_plane(bm.get_weight_quant_mode()); + } + else + { + dm.set_ref_1_plane(bm.get_weight_quant_mode()); + } + + bsd.block_mode_packed_index[i] = static_cast<uint16_t>(packed_bm_idx); + + packed_bm_idx++; + bm_counts[j]++; + } + } + + bsd.block_mode_count_1plane_always = bm_counts[0]; + bsd.block_mode_count_1plane_selected = bm_counts[0] + bm_counts[1]; + bsd.block_mode_count_1plane_2plane_selected = bm_counts[0] + bm_counts[1] + bm_counts[2]; + bsd.block_mode_count_all = bm_counts[0] + bm_counts[1] + bm_counts[2] + bm_counts[3]; + + bsd.decimation_mode_count_always = dm_counts[0]; + bsd.decimation_mode_count_selected = dm_counts[0] + dm_counts[1] + dm_counts[2]; + bsd.decimation_mode_count_all = dm_counts[0] + dm_counts[1] + dm_counts[2] + dm_counts[3]; + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + assert(bsd.block_mode_count_1plane_always > 0); + assert(bsd.decimation_mode_count_always > 0); + + delete[] percentiles; +#endif + + // Ensure the end of the array contains valid data (should never get read) + for (unsigned int i = bsd.decimation_mode_count_all; i < WEIGHTS_MAX_DECIMATION_MODES; i++) + { + bsd.decimation_modes[i].maxprec_1plane = -1; + bsd.decimation_modes[i].maxprec_2planes = -1; + bsd.decimation_modes[i].refprec_1_plane = 0; + bsd.decimation_modes[i].refprec_2_planes = 0; + } + + // Determine the texels to use for kmeans clustering. + assign_kmeans_texels(bsd); + + delete wb; +} + +/** + * @brief Allocate block modes and decimation tables for a single 3D block size. + * + * TODO: This function doesn't include all of the heuristics that we use for 2D block sizes such as + * the percentile mode cutoffs. If 3D becomes more widely used we should look at this. + * + * @param x_texels The number of texels in the X dimension. + * @param y_texels The number of texels in the Y dimension. + * @param z_texels The number of texels in the Z dimension. + * @param[out] bsd The block size descriptor to populate. + */ +static void construct_block_size_descriptor_3d( + unsigned int x_texels, + unsigned int y_texels, + unsigned int z_texels, + block_size_descriptor& bsd +) { + // Store a remap table for storing packed decimation modes. + // Indexing uses [Z * 64 + Y * 8 + X] and max size for each axis is 6. + static constexpr unsigned int MAX_DMI = 6 * 64 + 6 * 8 + 6; + int decimation_mode_index[MAX_DMI]; + unsigned int decimation_mode_count = 0; + + dt_init_working_buffers* wb = new dt_init_working_buffers; + + bsd.xdim = static_cast<uint8_t>(x_texels); + bsd.ydim = static_cast<uint8_t>(y_texels); + bsd.zdim = static_cast<uint8_t>(z_texels); + bsd.texel_count = static_cast<uint8_t>(x_texels * y_texels * z_texels); + + for (unsigned int i = 0; i < MAX_DMI; i++) + { + decimation_mode_index[i] = -1; + } + + // gather all the infill-modes that can be used with the current block size + for (unsigned int x_weights = 2; x_weights <= x_texels; x_weights++) + { + for (unsigned int y_weights = 2; y_weights <= y_texels; y_weights++) + { + for (unsigned int z_weights = 2; z_weights <= z_texels; z_weights++) + { + unsigned int weight_count = x_weights * y_weights * z_weights; + if (weight_count > BLOCK_MAX_WEIGHTS) + { + continue; + } + + decimation_info& di = bsd.decimation_tables[decimation_mode_count]; + decimation_mode_index[z_weights * 64 + y_weights * 8 + x_weights] = decimation_mode_count; + init_decimation_info_3d(x_texels, y_texels, z_texels, x_weights, y_weights, z_weights, di, *wb); + + int maxprec_1plane = -1; + int maxprec_2planes = -1; + for (unsigned int i = 0; i < 12; i++) + { + unsigned int bits_1plane = get_ise_sequence_bitcount(weight_count, static_cast<quant_method>(i)); + if (bits_1plane >= BLOCK_MIN_WEIGHT_BITS && bits_1plane <= BLOCK_MAX_WEIGHT_BITS) + { + maxprec_1plane = i; + } + + unsigned int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, static_cast<quant_method>(i)); + if (bits_2planes >= BLOCK_MIN_WEIGHT_BITS && bits_2planes <= BLOCK_MAX_WEIGHT_BITS) + { + maxprec_2planes = i; + } + } + + if ((2 * weight_count) > BLOCK_MAX_WEIGHTS) + { + maxprec_2planes = -1; + } + + bsd.decimation_modes[decimation_mode_count].maxprec_1plane = static_cast<int8_t>(maxprec_1plane); + bsd.decimation_modes[decimation_mode_count].maxprec_2planes = static_cast<int8_t>(maxprec_2planes); + bsd.decimation_modes[decimation_mode_count].refprec_1_plane = maxprec_1plane == -1 ? 0 : 0xFFFF; + bsd.decimation_modes[decimation_mode_count].refprec_2_planes = maxprec_2planes == -1 ? 0 : 0xFFFF; + decimation_mode_count++; + } + } + } + + // Ensure the end of the array contains valid data (should never get read) + for (unsigned int i = decimation_mode_count; i < WEIGHTS_MAX_DECIMATION_MODES; i++) + { + bsd.decimation_modes[i].maxprec_1plane = -1; + bsd.decimation_modes[i].maxprec_2planes = -1; + bsd.decimation_modes[i].refprec_1_plane = 0; + bsd.decimation_modes[i].refprec_2_planes = 0; + } + + bsd.decimation_mode_count_always = 0; // Skipped for 3D modes + bsd.decimation_mode_count_selected = decimation_mode_count; + bsd.decimation_mode_count_all = decimation_mode_count; + + // Construct the list of block formats referencing the decimation tables + + // Clear the list to a known-bad value + for (unsigned int i = 0; i < WEIGHTS_MAX_BLOCK_MODES; i++) + { + bsd.block_mode_packed_index[i] = BLOCK_BAD_BLOCK_MODE; + } + + unsigned int packed_idx = 0; + unsigned int bm_counts[2] { 0 }; + + // Iterate two times to build a usefully ordered list: + // - Pass 0 - keep valid single plane block modes + // - Pass 1 - keep valid dual plane block modes + for (unsigned int j = 0; j < 2; j++) + { + for (unsigned int i = 0; i < WEIGHTS_MAX_BLOCK_MODES; i++) + { + // Skip modes we've already included in a previous pass + if (bsd.block_mode_packed_index[i] != BLOCK_BAD_BLOCK_MODE) + { + continue; + } + + unsigned int x_weights; + unsigned int y_weights; + unsigned int z_weights; + bool is_dual_plane; + unsigned int quant_mode; + unsigned int weight_bits; + + bool valid = decode_block_mode_3d(i, x_weights, y_weights, z_weights, is_dual_plane, quant_mode, weight_bits); + // Skip invalid encodings + if (!valid || x_weights > x_texels || y_weights > y_texels || z_weights > z_texels) + { + continue; + } + + // Skip encodings in the wrong iteration + if ((j == 0 && is_dual_plane) || (j == 1 && !is_dual_plane)) + { + continue; + } + + // Always skip encodings we can't physically encode based on bit availability + if (is_dual_plane) + { + // This is the only check we need as only support 1 partition + if ((109 - weight_bits) <= 0) + { + continue; + } + } + else + { + // This is conservative - fewer bits may be available for > 1 partition + if ((111 - weight_bits) <= 0) + { + continue; + } + } + + int decimation_mode = decimation_mode_index[z_weights * 64 + y_weights * 8 + x_weights]; + bsd.block_modes[packed_idx].decimation_mode = static_cast<uint8_t>(decimation_mode); + bsd.block_modes[packed_idx].quant_mode = static_cast<uint8_t>(quant_mode); + bsd.block_modes[packed_idx].weight_bits = static_cast<uint8_t>(weight_bits); + bsd.block_modes[packed_idx].is_dual_plane = static_cast<uint8_t>(is_dual_plane); + bsd.block_modes[packed_idx].mode_index = static_cast<uint16_t>(i); + + bsd.block_mode_packed_index[i] = static_cast<uint16_t>(packed_idx); + bm_counts[j]++; + packed_idx++; + } + } + + bsd.block_mode_count_1plane_always = 0; // Skipped for 3D modes + bsd.block_mode_count_1plane_selected = bm_counts[0]; + bsd.block_mode_count_1plane_2plane_selected = bm_counts[0] + bm_counts[1]; + bsd.block_mode_count_all = bm_counts[0] + bm_counts[1]; + + // Determine the texels to use for kmeans clustering. + assign_kmeans_texels(bsd); + + delete wb; +} + +/* See header for documentation. */ +void init_block_size_descriptor( + unsigned int x_texels, + unsigned int y_texels, + unsigned int z_texels, + bool can_omit_modes, + unsigned int partition_count_cutoff, + float mode_cutoff, + block_size_descriptor& bsd +) { + if (z_texels > 1) + { + construct_block_size_descriptor_3d(x_texels, y_texels, z_texels, bsd); + } + else + { + construct_block_size_descriptor_2d(x_texels, y_texels, can_omit_modes, mode_cutoff, bsd); + } + + init_partition_tables(bsd, can_omit_modes, partition_count_cutoff); +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_quantize.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_quantize.cpp new file mode 100644 index 00000000000..1f5a4d92ee3 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_quantize.cpp @@ -0,0 +1,2071 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions for color quantization. + * + * The design of the color quantization functionality requires the caller to use higher level error + * analysis to determine the base encoding that should be used. This earlier analysis will select + * the basic type of the endpoint that should be used: + * + * * Mode: LDR or HDR + * * Quantization level + * * Channel count: L, LA, RGB, or RGBA + * * Endpoint 2 type: Direct color endcode, or scaled from endpoint 1. + * + * However, this leaves a number of decisions about exactly how to pack the endpoints open. In + * particular we need to determine if blue contraction can be used, or/and if delta encoding can be + * used. If they can be applied these will allow us to maintain higher precision in the endpoints + * without needing additional storage. + */ + +#include <stdio.h> +#include <assert.h> + +#include "astcenc_internal.h" + +/** + * @brief Determine the quantized value given a quantization level. + * + * @param quant_level The quantization level to use. + * @param value The value to convert. This may be outside of the 0-255 range and will be + * clamped before the value is looked up. + * + * @return The encoded quantized value. These are not necessarily in order; the compressor + * scrambles the values slightly to make hardware implementation easier. + */ +static inline uint8_t quant_color( + quant_method quant_level, + int value +) { + return color_unquant_to_uquant_tables[quant_level - QUANT_6][value]; +} + +/** + * @brief Quantize an LDR RGB color. + * + * Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result. + * For this encoding @c color0 cannot be larger than @c color1. If @c color0 is actually larger + * than @c color1, @c color0 is reduced and @c color1 is increased until the constraint is met. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r0, r1, g0, g1, b0, b1). + * @param quant_level The quantization level to use. + */ +static void quantize_rgb( + vfloat4 color0, + vfloat4 color1, + uint8_t output[6], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float r0 = astc::clamp255f(color0.lane<0>() * scale); + float g0 = astc::clamp255f(color0.lane<1>() * scale); + float b0 = astc::clamp255f(color0.lane<2>() * scale); + + float r1 = astc::clamp255f(color1.lane<0>() * scale); + float g1 = astc::clamp255f(color1.lane<1>() * scale); + float b1 = astc::clamp255f(color1.lane<2>() * scale); + + int ri0, gi0, bi0, ri1, gi1, bi1; + float rgb0_addon = 0.5f; + float rgb1_addon = 0.5f; + do + { + ri0 = quant_color(quant_level, astc::max(astc::flt2int_rd(r0 + rgb0_addon), 0)); + gi0 = quant_color(quant_level, astc::max(astc::flt2int_rd(g0 + rgb0_addon), 0)); + bi0 = quant_color(quant_level, astc::max(astc::flt2int_rd(b0 + rgb0_addon), 0)); + ri1 = quant_color(quant_level, astc::min(astc::flt2int_rd(r1 + rgb1_addon), 255)); + gi1 = quant_color(quant_level, astc::min(astc::flt2int_rd(g1 + rgb1_addon), 255)); + bi1 = quant_color(quant_level, astc::min(astc::flt2int_rd(b1 + rgb1_addon), 255)); + + rgb0_addon -= 0.2f; + rgb1_addon += 0.2f; + } while (ri0 + gi0 + bi0 > ri1 + gi1 + bi1); + + output[0] = static_cast<uint8_t>(ri0); + output[1] = static_cast<uint8_t>(ri1); + output[2] = static_cast<uint8_t>(gi0); + output[3] = static_cast<uint8_t>(gi1); + output[4] = static_cast<uint8_t>(bi0); + output[5] = static_cast<uint8_t>(bi1); +} + +/** + * @brief Quantize an LDR RGBA color. + * + * Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result. + * For this encoding @c color0.rgb cannot be larger than @c color1.rgb (this indicates blue + * contraction). If @c color0.rgb is actually larger than @c color1.rgb, @c color0.rgb is reduced + * and @c color1.rgb is increased until the constraint is met. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r0, r1, g0, g1, b0, b1, a0, a1). + * @param quant_level The quantization level to use. + */ +static void quantize_rgba( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + output[6] = quant_color(quant_level, astc::flt2int_rtn(a0)); + output[7] = quant_color(quant_level, astc::flt2int_rtn(a1)); + + quantize_rgb(color0, color1, output, quant_level); +} + +/** + * @brief Try to quantize an LDR RGB color using blue-contraction. + * + * Blue-contraction is only usable if encoded color 1 is larger than color 0. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r1, r0, g1, g0, b1, b0). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_rgb_blue_contract( + vfloat4 color0, + vfloat4 color1, + uint8_t output[6], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float r0 = color0.lane<0>() * scale; + float g0 = color0.lane<1>() * scale; + float b0 = color0.lane<2>() * scale; + + float r1 = color1.lane<0>() * scale; + float g1 = color1.lane<1>() * scale; + float b1 = color1.lane<2>() * scale; + + // Apply inverse blue-contraction. This can produce an overflow; which means BC cannot be used. + r0 += (r0 - b0); + g0 += (g0 - b0); + r1 += (r1 - b1); + g1 += (g1 - b1); + + if (r0 < 0.0f || r0 > 255.0f || g0 < 0.0f || g0 > 255.0f || b0 < 0.0f || b0 > 255.0f || + r1 < 0.0f || r1 > 255.0f || g1 < 0.0f || g1 > 255.0f || b1 < 0.0f || b1 > 255.0f) + { + return false; + } + + // Quantize the inverse-blue-contracted color + int ri0 = quant_color(quant_level, astc::flt2int_rtn(r0)); + int gi0 = quant_color(quant_level, astc::flt2int_rtn(g0)); + int bi0 = quant_color(quant_level, astc::flt2int_rtn(b0)); + + int ri1 = quant_color(quant_level, astc::flt2int_rtn(r1)); + int gi1 = quant_color(quant_level, astc::flt2int_rtn(g1)); + int bi1 = quant_color(quant_level, astc::flt2int_rtn(b1)); + + // If color #1 is not larger than color #0 then blue-contraction cannot be used. Note that + // blue-contraction and quantization change this order, which is why we must test afterwards. + if (ri1 + gi1 + bi1 <= ri0 + gi0 + bi0) + { + return false; + } + + output[0] = static_cast<uint8_t>(ri1); + output[1] = static_cast<uint8_t>(ri0); + output[2] = static_cast<uint8_t>(gi1); + output[3] = static_cast<uint8_t>(gi0); + output[4] = static_cast<uint8_t>(bi1); + output[5] = static_cast<uint8_t>(bi0); + + return true; +} + +/** + * @brief Try to quantize an LDR RGBA color using blue-contraction. + * + * Blue-contraction is only usable if encoded color 1 RGB is larger than color 0 RGB. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r1, r0, g1, g0, b1, b0, a1, a0). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static int try_quantize_rgba_blue_contract( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + output[6] = quant_color(quant_level, astc::flt2int_rtn(a1)); + output[7] = quant_color(quant_level, astc::flt2int_rtn(a0)); + + return try_quantize_rgb_blue_contract(color0, color1, output, quant_level); +} + +/** + * @brief Try to quantize an LDR RGB color using delta encoding. + * + * At decode time we move one bit from the offset to the base and seize another bit as a sign bit; + * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is + * non-negative, then we encode a regular delta. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r0, r1, g0, g1, b0, b1). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_rgb_delta( + vfloat4 color0, + vfloat4 color1, + uint8_t output[6], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float r0 = astc::clamp255f(color0.lane<0>() * scale); + float g0 = astc::clamp255f(color0.lane<1>() * scale); + float b0 = astc::clamp255f(color0.lane<2>() * scale); + + float r1 = astc::clamp255f(color1.lane<0>() * scale); + float g1 = astc::clamp255f(color1.lane<1>() * scale); + float b1 = astc::clamp255f(color1.lane<2>() * scale); + + // Transform r0 to unorm9 + int r0a = astc::flt2int_rtn(r0); + int g0a = astc::flt2int_rtn(g0); + int b0a = astc::flt2int_rtn(b0); + + r0a <<= 1; + g0a <<= 1; + b0a <<= 1; + + // Mask off the top bit + int r0b = r0a & 0xFF; + int g0b = g0a & 0xFF; + int b0b = b0a & 0xFF; + + // Quantize then unquantize in order to get a value that we take differences against + int r0be = quant_color(quant_level, r0b); + int g0be = quant_color(quant_level, g0b); + int b0be = quant_color(quant_level, b0b); + + r0b = r0be | (r0a & 0x100); + g0b = g0be | (g0a & 0x100); + b0b = b0be | (b0a & 0x100); + + // Get hold of the second value + int r1d = astc::flt2int_rtn(r1); + int g1d = astc::flt2int_rtn(g1); + int b1d = astc::flt2int_rtn(b1); + + r1d <<= 1; + g1d <<= 1; + b1d <<= 1; + + // ... and take differences + r1d -= r0b; + g1d -= g0b; + b1d -= b0b; + + // Check if the difference is too large to be encodable + if (r1d > 63 || g1d > 63 || b1d > 63 || r1d < -64 || g1d < -64 || b1d < -64) + { + return false; + } + + // Insert top bit of the base into the offset + r1d &= 0x7F; + g1d &= 0x7F; + b1d &= 0x7F; + + r1d |= (r0b & 0x100) >> 1; + g1d |= (g0b & 0x100) >> 1; + b1d |= (b0b & 0x100) >> 1; + + // Then quantize and unquantize; if this causes either top two bits to flip, then encoding fails + // since we have then corrupted either the top bit of the base or the sign bit of the offset + int r1de = quant_color(quant_level, r1d); + int g1de = quant_color(quant_level, g1d); + int b1de = quant_color(quant_level, b1d); + + if (((r1d ^ r1de) | (g1d ^ g1de) | (b1d ^ b1de)) & 0xC0) + { + return false; + } + + // If the sum of offsets triggers blue-contraction then encoding fails + vint4 ep0(r0be, g0be, b0be, 0); + vint4 ep1(r1de, g1de, b1de, 0); + bit_transfer_signed(ep1, ep0); + if (hadd_rgb_s(ep1) < 0) + { + return false; + } + + // Check that the offsets produce legitimate sums as well + ep0 = ep0 + ep1; + if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF)))) + { + return false; + } + + output[0] = static_cast<uint8_t>(r0be); + output[1] = static_cast<uint8_t>(r1de); + output[2] = static_cast<uint8_t>(g0be); + output[3] = static_cast<uint8_t>(g1de); + output[4] = static_cast<uint8_t>(b0be); + output[5] = static_cast<uint8_t>(b1de); + + return true; +} + +static bool try_quantize_rgb_delta_blue_contract( + vfloat4 color0, + vfloat4 color1, + uint8_t output[6], + quant_method quant_level +) { + // Note: Switch around endpoint colors already at start + float scale = 1.0f / 257.0f; + + float r1 = color0.lane<0>() * scale; + float g1 = color0.lane<1>() * scale; + float b1 = color0.lane<2>() * scale; + + float r0 = color1.lane<0>() * scale; + float g0 = color1.lane<1>() * scale; + float b0 = color1.lane<2>() * scale; + + // Apply inverse blue-contraction. This can produce an overflow; which means BC cannot be used. + r0 += (r0 - b0); + g0 += (g0 - b0); + r1 += (r1 - b1); + g1 += (g1 - b1); + + if (r0 < 0.0f || r0 > 255.0f || g0 < 0.0f || g0 > 255.0f || b0 < 0.0f || b0 > 255.0f || + r1 < 0.0f || r1 > 255.0f || g1 < 0.0f || g1 > 255.0f || b1 < 0.0f || b1 > 255.0f) + { + return false; + } + + // Transform r0 to unorm9 + int r0a = astc::flt2int_rtn(r0); + int g0a = astc::flt2int_rtn(g0); + int b0a = astc::flt2int_rtn(b0); + r0a <<= 1; + g0a <<= 1; + b0a <<= 1; + + // Mask off the top bit + int r0b = r0a & 0xFF; + int g0b = g0a & 0xFF; + int b0b = b0a & 0xFF; + + // Quantize, then unquantize in order to get a value that we take differences against. + int r0be = quant_color(quant_level, r0b); + int g0be = quant_color(quant_level, g0b); + int b0be = quant_color(quant_level, b0b); + + r0b = r0be | (r0a & 0x100); + g0b = g0be | (g0a & 0x100); + b0b = b0be | (b0a & 0x100); + + // Get hold of the second value + int r1d = astc::flt2int_rtn(r1); + int g1d = astc::flt2int_rtn(g1); + int b1d = astc::flt2int_rtn(b1); + + r1d <<= 1; + g1d <<= 1; + b1d <<= 1; + + // .. and take differences! + r1d -= r0b; + g1d -= g0b; + b1d -= b0b; + + // Check if the difference is too large to be encodable + if (r1d > 63 || g1d > 63 || b1d > 63 || r1d < -64 || g1d < -64 || b1d < -64) + { + return false; + } + + // Insert top bit of the base into the offset + r1d &= 0x7F; + g1d &= 0x7F; + b1d &= 0x7F; + + r1d |= (r0b & 0x100) >> 1; + g1d |= (g0b & 0x100) >> 1; + b1d |= (b0b & 0x100) >> 1; + + // Then quantize and unquantize; if this causes any of the top two bits to flip, + // then encoding fails, since we have then corrupted either the top bit of the base + // or the sign bit of the offset. + int r1de = quant_color(quant_level, r1d); + int g1de = quant_color(quant_level, g1d); + int b1de = quant_color(quant_level, b1d); + + if (((r1d ^ r1de) | (g1d ^ g1de) | (b1d ^ b1de)) & 0xC0) + { + return false; + } + + // If the sum of offsets does not trigger blue-contraction then encoding fails + vint4 ep0(r0be, g0be, b0be, 0); + vint4 ep1(r1de, g1de, b1de, 0); + bit_transfer_signed(ep1, ep0); + if (hadd_rgb_s(ep1) >= 0) + { + return false; + } + + // Check that the offsets produce legitimate sums as well + ep0 = ep0 + ep1; + if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF)))) + { + return false; + } + + output[0] = static_cast<uint8_t>(r0be); + output[1] = static_cast<uint8_t>(r1de); + output[2] = static_cast<uint8_t>(g0be); + output[3] = static_cast<uint8_t>(g1de); + output[4] = static_cast<uint8_t>(b0be); + output[5] = static_cast<uint8_t>(b1de); + + return true; +} + +/** + * @brief Try to quantize an LDR A color using delta encoding. + * + * At decode time we move one bit from the offset to the base and seize another bit as a sign bit; + * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is + * non-negative, then we encode a regular delta. + * + * This function only compressed the alpha - the other elements in the output array are not touched. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (x, x, x, x, x, x, a0, a1). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_alpha_delta( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + int a0a = astc::flt2int_rtn(a0); + a0a <<= 1; + int a0b = a0a & 0xFF; + int a0be = quant_color(quant_level, a0b); + a0b = a0be; + a0b |= a0a & 0x100; + int a1d = astc::flt2int_rtn(a1); + a1d <<= 1; + a1d -= a0b; + + if (a1d > 63 || a1d < -64) + { + return false; + } + + a1d &= 0x7F; + a1d |= (a0b & 0x100) >> 1; + + int a1de = quant_color(quant_level, a1d); + int a1du = a1de; + if ((a1d ^ a1du) & 0xC0) + { + return false; + } + + a1du &= 0x7F; + if (a1du & 0x40) + { + a1du -= 0x80; + } + + a1du += a0b; + if (a1du < 0 || a1du > 0x1FF) + { + return false; + } + + output[6] = static_cast<uint8_t>(a0be); + output[7] = static_cast<uint8_t>(a1de); + + return true; +} + +/** + * @brief Try to quantize an LDR LA color using delta encoding. + * + * At decode time we move one bit from the offset to the base and seize another bit as a sign bit; + * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is + * non-negative, then we encode a regular delta. + * + * This function only compressed the alpha - the other elements in the output array are not touched. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (l0, l1, a0, a1). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_luminance_alpha_delta( + vfloat4 color0, + vfloat4 color1, + uint8_t output[4], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float l0 = astc::clamp255f(hadd_rgb_s(color0) * ((1.0f / 3.0f) * scale)); + float l1 = astc::clamp255f(hadd_rgb_s(color1) * ((1.0f / 3.0f) * scale)); + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + int l0a = astc::flt2int_rtn(l0); + int a0a = astc::flt2int_rtn(a0); + l0a <<= 1; + a0a <<= 1; + + int l0b = l0a & 0xFF; + int a0b = a0a & 0xFF; + int l0be = quant_color(quant_level, l0b); + int a0be = quant_color(quant_level, a0b); + l0b = l0be; + a0b = a0be; + l0b |= l0a & 0x100; + a0b |= a0a & 0x100; + + int l1d = astc::flt2int_rtn(l1); + int a1d = astc::flt2int_rtn(a1); + l1d <<= 1; + a1d <<= 1; + l1d -= l0b; + a1d -= a0b; + + if (l1d > 63 || l1d < -64) + { + return false; + } + + if (a1d > 63 || a1d < -64) + { + return false; + } + + l1d &= 0x7F; + a1d &= 0x7F; + l1d |= (l0b & 0x100) >> 1; + a1d |= (a0b & 0x100) >> 1; + + int l1de = quant_color(quant_level, l1d); + int a1de = quant_color(quant_level, a1d); + int l1du = l1de; + int a1du = a1de; + + if ((l1d ^ l1du) & 0xC0) + { + return false; + } + + if ((a1d ^ a1du) & 0xC0) + { + return false; + } + + l1du &= 0x7F; + a1du &= 0x7F; + + if (l1du & 0x40) + { + l1du -= 0x80; + } + + if (a1du & 0x40) + { + a1du -= 0x80; + } + + l1du += l0b; + a1du += a0b; + + if (l1du < 0 || l1du > 0x1FF) + { + return false; + } + + if (a1du < 0 || a1du > 0x1FF) + { + return false; + } + + output[0] = static_cast<uint8_t>(l0be); + output[1] = static_cast<uint8_t>(l1de); + output[2] = static_cast<uint8_t>(a0be); + output[3] = static_cast<uint8_t>(a1de); + + return true; +} + +/** + * @brief Try to quantize an LDR RGBA color using delta encoding. + * + * At decode time we move one bit from the offset to the base and seize another bit as a sign bit; + * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is + * non-negative, then we encode a regular delta. + * + * This function only compressed the alpha - the other elements in the output array are not touched. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r0, r1, b0, b1, g0, g1, a0, a1). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_rgba_delta( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + return try_quantize_rgb_delta(color0, color1, output, quant_level) && + try_quantize_alpha_delta(color0, color1, output, quant_level); +} + + +/** + * @brief Try to quantize an LDR RGBA color using delta and blue contract encoding. + * + * At decode time we move one bit from the offset to the base and seize another bit as a sign bit; + * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is + * non-negative, then we encode a regular delta. + * + * This function only compressed the alpha - the other elements in the output array are not touched. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (r0, r1, b0, b1, g0, g1, a0, a1). + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_rgba_delta_blue_contract( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + // Note that we swap the color0 and color1 ordering for alpha to match RGB blue-contract + return try_quantize_rgb_delta_blue_contract(color0, color1, output, quant_level) && + try_quantize_alpha_delta(color1, color0, output, quant_level); +} + +/** + * @brief Quantize an LDR RGB color using scale encoding. + * + * @param color The input unquantized color endpoint and scale factor. + * @param[out] output The output endpoints, returned as (r0, g0, b0, s). + * @param quant_level The quantization level to use. + */ +static void quantize_rgbs( + vfloat4 color, + uint8_t output[4], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float r = astc::clamp255f(color.lane<0>() * scale); + float g = astc::clamp255f(color.lane<1>() * scale); + float b = astc::clamp255f(color.lane<2>() * scale); + + int ri = quant_color(quant_level, astc::flt2int_rtn(r)); + int gi = quant_color(quant_level, astc::flt2int_rtn(g)); + int bi = quant_color(quant_level, astc::flt2int_rtn(b)); + + float oldcolorsum = hadd_rgb_s(color) * scale; + float newcolorsum = static_cast<float>(ri + gi + bi); + + float scalea = astc::clamp1f(color.lane<3>() * (oldcolorsum + 1e-10f) / (newcolorsum + 1e-10f)); + int scale_idx = astc::flt2int_rtn(scalea * 256.0f); + scale_idx = astc::clamp(scale_idx, 0, 255); + + output[0] = static_cast<uint8_t>(ri); + output[1] = static_cast<uint8_t>(gi); + output[2] = static_cast<uint8_t>(bi); + output[3] = quant_color(quant_level, scale_idx); +} + +/** + * @brief Quantize an LDR RGBA color using scale encoding. + * + * @param color The input unquantized color endpoint and scale factor. + * @param[out] output The output endpoints, returned as (r0, g0, b0, s, a0, a1). + * @param quant_level The quantization level to use. + */ +static void quantize_rgbs_alpha( + vfloat4 color0, + vfloat4 color1, + vfloat4 color, + uint8_t output[6], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + output[4] = quant_color(quant_level, astc::flt2int_rtn(a0)); + output[5] = quant_color(quant_level, astc::flt2int_rtn(a1)); + + quantize_rgbs(color, output, quant_level); +} + +/** + * @brief Quantize a LDR L color. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (l0, l1). + * @param quant_level The quantization level to use. + */ +static void quantize_luminance( + vfloat4 color0, + vfloat4 color1, + uint8_t output[2], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + color0 = color0 * scale; + color1 = color1 * scale; + + float lum0 = astc::clamp255f(hadd_rgb_s(color0) * (1.0f / 3.0f)); + float lum1 = astc::clamp255f(hadd_rgb_s(color1) * (1.0f / 3.0f)); + + if (lum0 > lum1) + { + float avg = (lum0 + lum1) * 0.5f; + lum0 = avg; + lum1 = avg; + } + + output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0)); + output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1)); +} + +/** + * @brief Quantize a LDR LA color. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as (l0, l1, a0, a1). + * @param quant_level The quantization level to use. + */ +static void quantize_luminance_alpha( + vfloat4 color0, + vfloat4 color1, + uint8_t output[4], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + color0 = color0 * scale; + color1 = color1 * scale; + + float lum0 = astc::clamp255f(hadd_rgb_s(color0) * (1.0f / 3.0f)); + float lum1 = astc::clamp255f(hadd_rgb_s(color1) * (1.0f / 3.0f)); + + float a0 = astc::clamp255f(color0.lane<3>()); + float a1 = astc::clamp255f(color1.lane<3>()); + + // If endpoints are close then pull apart slightly; this gives > 8 bit normal map precision. + if (quant_level > 18) + { + if (fabsf(lum0 - lum1) < 3.0f) + { + if (lum0 < lum1) + { + lum0 -= 0.5f; + lum1 += 0.5f; + } + else + { + lum0 += 0.5f; + lum1 -= 0.5f; + } + + lum0 = astc::clamp255f(lum0); + lum1 = astc::clamp255f(lum1); + } + + if (fabsf(a0 - a1) < 3.0f) + { + if (a0 < a1) + { + a0 -= 0.5f; + a1 += 0.5f; + } + else + { + a0 += 0.5f; + a1 -= 0.5f; + } + + a0 = astc::clamp255f(a0); + a1 = astc::clamp255f(a1); + } + } + + output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0)); + output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1)); + output[2] = quant_color(quant_level, astc::flt2int_rtn(a0)); + output[3] = quant_color(quant_level, astc::flt2int_rtn(a1)); +} + +/** + * @brief Quantize and unquantize a value ensuring top two bits are the same. + * + * @param quant_level The quantization level to use. + * @param value The input unquantized value. + * @param[out] quant_value The quantized value. + */ +static inline void quantize_and_unquantize_retain_top_two_bits( + quant_method quant_level, + uint8_t value, + uint8_t& quant_value +) { + int perform_loop; + uint8_t quantval; + + do + { + quantval = quant_color(quant_level, value); + + // Perform looping if the top two bits were modified by quant/unquant + perform_loop = (value & 0xC0) != (quantval & 0xC0); + + if ((quantval & 0xC0) > (value & 0xC0)) + { + // Quant/unquant rounded UP so that the top two bits changed; + // decrement the input in hopes that this will avoid rounding up. + value--; + } + else if ((quantval & 0xC0) < (value & 0xC0)) + { + // Quant/unquant rounded DOWN so that the top two bits changed; + // decrement the input in hopes that this will avoid rounding down. + value--; + } + } while (perform_loop); + + quant_value = quantval; +} + +/** + * @brief Quantize and unquantize a value ensuring top four bits are the same. + * + * @param quant_level The quantization level to use. + * @param value The input unquantized value. + * @param[out] quant_value The quantized value in 0-255 range. + */ +static inline void quantize_and_unquantize_retain_top_four_bits( + quant_method quant_level, + uint8_t value, + uint8_t& quant_value +) { + uint8_t perform_loop; + uint8_t quantval; + + do + { + quantval = quant_color(quant_level, value); + // Perform looping if the top four bits were modified by quant/unquant + perform_loop = (value & 0xF0) != (quantval & 0xF0); + + if ((quantval & 0xF0) > (value & 0xF0)) + { + // Quant/unquant rounded UP so that the top four bits changed; + // decrement the input value in hopes that this will avoid rounding up. + value--; + } + else if ((quantval & 0xF0) < (value & 0xF0)) + { + // Quant/unquant rounded DOWN so that the top four bits changed; + // decrement the input value in hopes that this will avoid rounding down. + value--; + } + } while (perform_loop); + + quant_value = quantval; +} + +/** + * @brief Quantize a HDR RGB color using RGB + offset. + * + * @param color The input unquantized color endpoint and offset. + * @param[out] output The output endpoints, returned as packed RGBS with some mode bits. + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_rgbo( + vfloat4 color, + uint8_t output[4], + quant_method quant_level +) { + color.set_lane<0>(color.lane<0>() + color.lane<3>()); + color.set_lane<1>(color.lane<1>() + color.lane<3>()); + color.set_lane<2>(color.lane<2>() + color.lane<3>()); + + color = clamp(0.0f, 65535.0f, color); + + vfloat4 color_bak = color; + + int majcomp; + if (color.lane<0>() > color.lane<1>() && color.lane<0>() > color.lane<2>()) + { + majcomp = 0; // red is largest component + } + else if (color.lane<1>() > color.lane<2>()) + { + majcomp = 1; // green is largest component + } + else + { + majcomp = 2; // blue is largest component + } + + // swap around the red component and the largest component. + switch (majcomp) + { + case 1: + color = color.swz<1, 0, 2, 3>(); + break; + case 2: + color = color.swz<2, 1, 0, 3>(); + break; + default: + break; + } + + static const int mode_bits[5][3] { + {11, 5, 7}, + {11, 6, 5}, + {10, 5, 8}, + {9, 6, 7}, + {8, 7, 6} + }; + + static const float mode_cutoffs[5][2] { + {1024, 4096}, + {2048, 1024}, + {2048, 16384}, + {8192, 16384}, + {32768, 16384} + }; + + static const float mode_rscales[5] { + 32.0f, + 32.0f, + 64.0f, + 128.0f, + 256.0f, + }; + + static const float mode_scales[5] { + 1.0f / 32.0f, + 1.0f / 32.0f, + 1.0f / 64.0f, + 1.0f / 128.0f, + 1.0f / 256.0f, + }; + + float r_base = color.lane<0>(); + float g_base = color.lane<0>() - color.lane<1>() ; + float b_base = color.lane<0>() - color.lane<2>() ; + float s_base = color.lane<3>() ; + + for (int mode = 0; mode < 5; mode++) + { + if (g_base > mode_cutoffs[mode][0] || b_base > mode_cutoffs[mode][0] || s_base > mode_cutoffs[mode][1]) + { + continue; + } + + // Encode the mode into a 4-bit vector + int mode_enc = mode < 4 ? (mode | (majcomp << 2)) : (majcomp | 0xC); + + float mode_scale = mode_scales[mode]; + float mode_rscale = mode_rscales[mode]; + + int gb_intcutoff = 1 << mode_bits[mode][1]; + int s_intcutoff = 1 << mode_bits[mode][2]; + + // Quantize and unquantize R + int r_intval = astc::flt2int_rtn(r_base * mode_scale); + + int r_lowbits = r_intval & 0x3f; + + r_lowbits |= (mode_enc & 3) << 6; + + uint8_t r_quantval; + quantize_and_unquantize_retain_top_two_bits( + quant_level, static_cast<uint8_t>(r_lowbits), r_quantval); + + r_intval = (r_intval & ~0x3f) | (r_quantval & 0x3f); + float r_fval = static_cast<float>(r_intval) * mode_rscale; + + // Recompute G and B, then quantize and unquantize them + float g_fval = r_fval - color.lane<1>() ; + float b_fval = r_fval - color.lane<2>() ; + + g_fval = astc::clamp(g_fval, 0.0f, 65535.0f); + b_fval = astc::clamp(b_fval, 0.0f, 65535.0f); + + int g_intval = astc::flt2int_rtn(g_fval * mode_scale); + int b_intval = astc::flt2int_rtn(b_fval * mode_scale); + + if (g_intval >= gb_intcutoff || b_intval >= gb_intcutoff) + { + continue; + } + + int g_lowbits = g_intval & 0x1f; + int b_lowbits = b_intval & 0x1f; + + int bit0 = 0; + int bit1 = 0; + int bit2 = 0; + int bit3 = 0; + + switch (mode) + { + case 0: + case 2: + bit0 = (r_intval >> 9) & 1; + break; + case 1: + case 3: + bit0 = (r_intval >> 8) & 1; + break; + case 4: + case 5: + bit0 = (g_intval >> 6) & 1; + break; + } + + switch (mode) + { + case 0: + case 1: + case 2: + case 3: + bit2 = (r_intval >> 7) & 1; + break; + case 4: + case 5: + bit2 = (b_intval >> 6) & 1; + break; + } + + switch (mode) + { + case 0: + case 2: + bit1 = (r_intval >> 8) & 1; + break; + case 1: + case 3: + case 4: + case 5: + bit1 = (g_intval >> 5) & 1; + break; + } + + switch (mode) + { + case 0: + bit3 = (r_intval >> 10) & 1; + break; + case 2: + bit3 = (r_intval >> 6) & 1; + break; + case 1: + case 3: + case 4: + case 5: + bit3 = (b_intval >> 5) & 1; + break; + } + + g_lowbits |= (mode_enc & 0x4) << 5; + b_lowbits |= (mode_enc & 0x8) << 4; + + g_lowbits |= bit0 << 6; + g_lowbits |= bit1 << 5; + b_lowbits |= bit2 << 6; + b_lowbits |= bit3 << 5; + + uint8_t g_quantval; + uint8_t b_quantval; + + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(g_lowbits), g_quantval); + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(b_lowbits), b_quantval); + + g_intval = (g_intval & ~0x1f) | (g_quantval & 0x1f); + b_intval = (b_intval & ~0x1f) | (b_quantval & 0x1f); + + g_fval = static_cast<float>(g_intval) * mode_rscale; + b_fval = static_cast<float>(b_intval) * mode_rscale; + + // Recompute the scale value, based on the errors introduced to red, green and blue + + // If the error is positive, then the R,G,B errors combined have raised the color + // value overall; as such, the scale value needs to be increased. + float rgb_errorsum = (r_fval - color.lane<0>() ) + (r_fval - g_fval - color.lane<1>() ) + (r_fval - b_fval - color.lane<2>() ); + + float s_fval = s_base + rgb_errorsum * (1.0f / 3.0f); + s_fval = astc::clamp(s_fval, 0.0f, 1e9f); + + int s_intval = astc::flt2int_rtn(s_fval * mode_scale); + + if (s_intval >= s_intcutoff) + { + continue; + } + + int s_lowbits = s_intval & 0x1f; + + int bit4; + int bit5; + int bit6; + switch (mode) + { + case 1: + bit6 = (r_intval >> 9) & 1; + break; + default: + bit6 = (s_intval >> 5) & 1; + break; + } + + switch (mode) + { + case 4: + bit5 = (r_intval >> 7) & 1; + break; + case 1: + bit5 = (r_intval >> 10) & 1; + break; + default: + bit5 = (s_intval >> 6) & 1; + break; + } + + switch (mode) + { + case 2: + bit4 = (s_intval >> 7) & 1; + break; + default: + bit4 = (r_intval >> 6) & 1; + break; + } + + s_lowbits |= bit6 << 5; + s_lowbits |= bit5 << 6; + s_lowbits |= bit4 << 7; + + uint8_t s_quantval; + + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(s_lowbits), s_quantval); + + output[0] = r_quantval; + output[1] = g_quantval; + output[2] = b_quantval; + output[3] = s_quantval; + return; + } + + // Failed to encode any of the modes above? In that case encode using mode #5 + float vals[4]; + vals[0] = color_bak.lane<0>(); + vals[1] = color_bak.lane<1>(); + vals[2] = color_bak.lane<2>(); + vals[3] = color_bak.lane<3>(); + + int ivals[4]; + float cvals[3]; + + for (int i = 0; i < 3; i++) + { + vals[i] = astc::clamp(vals[i], 0.0f, 65020.0f); + ivals[i] = astc::flt2int_rtn(vals[i] * (1.0f / 512.0f)); + cvals[i] = static_cast<float>(ivals[i]) * 512.0f; + } + + float rgb_errorsum = (cvals[0] - vals[0]) + (cvals[1] - vals[1]) + (cvals[2] - vals[2]); + vals[3] += rgb_errorsum * (1.0f / 3.0f); + + vals[3] = astc::clamp(vals[3], 0.0f, 65020.0f); + ivals[3] = astc::flt2int_rtn(vals[3] * (1.0f / 512.0f)); + + int encvals[4]; + encvals[0] = (ivals[0] & 0x3f) | 0xC0; + encvals[1] = (ivals[1] & 0x7f) | 0x80; + encvals[2] = (ivals[2] & 0x7f) | 0x80; + encvals[3] = (ivals[3] & 0x7f) | ((ivals[0] & 0x40) << 1); + + for (uint8_t i = 0; i < 4; i++) + { + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(encvals[i]), output[i]); + } + + return; +} + +/** + * @brief Quantize a HDR RGB color using direct RGB encoding. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed RGB+RGB pairs with mode bits. + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_rgb( + vfloat4 color0, + vfloat4 color1, + uint8_t output[6], + quant_method quant_level +) { + // Note: color*.lane<3> is not used so we can ignore it + color0 = clamp(0.0f, 65535.0f, color0); + color1 = clamp(0.0f, 65535.0f, color1); + + vfloat4 color0_bak = color0; + vfloat4 color1_bak = color1; + + int majcomp; + if (color1.lane<0>() > color1.lane<1>() && color1.lane<0>() > color1.lane<2>()) + { + majcomp = 0; + } + else if (color1.lane<1>() > color1.lane<2>()) + { + majcomp = 1; + } + else + { + majcomp = 2; + } + + // Swizzle the components + switch (majcomp) + { + case 1: // red-green swap + color0 = color0.swz<1, 0, 2, 3>(); + color1 = color1.swz<1, 0, 2, 3>(); + break; + case 2: // red-blue swap + color0 = color0.swz<2, 1, 0, 3>(); + color1 = color1.swz<2, 1, 0, 3>(); + break; + default: + break; + } + + float a_base = color1.lane<0>(); + a_base = astc::clamp(a_base, 0.0f, 65535.0f); + + float b0_base = a_base - color1.lane<1>(); + float b1_base = a_base - color1.lane<2>(); + float c_base = a_base - color0.lane<0>(); + float d0_base = a_base - b0_base - c_base - color0.lane<1>(); + float d1_base = a_base - b1_base - c_base - color0.lane<2>(); + + // Number of bits in the various fields in the various modes + static const int mode_bits[8][4] { + {9, 7, 6, 7}, + {9, 8, 6, 6}, + {10, 6, 7, 7}, + {10, 7, 7, 6}, + {11, 8, 6, 5}, + {11, 6, 8, 6}, + {12, 7, 7, 5}, + {12, 6, 7, 6} + }; + + // Cutoffs to use for the computed values of a,b,c,d, assuming the + // range 0..65535 are LNS values corresponding to fp16. + static const float mode_cutoffs[8][4] { + {16384, 8192, 8192, 8}, // mode 0: 9,7,6,7 + {32768, 8192, 4096, 8}, // mode 1: 9,8,6,6 + {4096, 8192, 4096, 4}, // mode 2: 10,6,7,7 + {8192, 8192, 2048, 4}, // mode 3: 10,7,7,6 + {8192, 2048, 512, 2}, // mode 4: 11,8,6,5 + {2048, 8192, 1024, 2}, // mode 5: 11,6,8,6 + {2048, 2048, 256, 1}, // mode 6: 12,7,7,5 + {1024, 2048, 512, 1}, // mode 7: 12,6,7,6 + }; + + static const float mode_scales[8] { + 1.0f / 128.0f, + 1.0f / 128.0f, + 1.0f / 64.0f, + 1.0f / 64.0f, + 1.0f / 32.0f, + 1.0f / 32.0f, + 1.0f / 16.0f, + 1.0f / 16.0f, + }; + + // Scaling factors when going from what was encoded in the mode to 16 bits. + static const float mode_rscales[8] { + 128.0f, + 128.0f, + 64.0f, + 64.0f, + 32.0f, + 32.0f, + 16.0f, + 16.0f + }; + + // Try modes one by one, with the highest-precision mode first. + for (int mode = 7; mode >= 0; mode--) + { + // For each mode, test if we can in fact accommodate the computed b, c, and d values. + // If we clearly can't, then we skip to the next mode. + + float b_cutoff = mode_cutoffs[mode][0]; + float c_cutoff = mode_cutoffs[mode][1]; + float d_cutoff = mode_cutoffs[mode][2]; + + if (b0_base > b_cutoff || b1_base > b_cutoff || c_base > c_cutoff || fabsf(d0_base) > d_cutoff || fabsf(d1_base) > d_cutoff) + { + continue; + } + + float mode_scale = mode_scales[mode]; + float mode_rscale = mode_rscales[mode]; + + int b_intcutoff = 1 << mode_bits[mode][1]; + int c_intcutoff = 1 << mode_bits[mode][2]; + int d_intcutoff = 1 << (mode_bits[mode][3] - 1); + + // Quantize and unquantize A, with the assumption that its high bits can be handled safely. + int a_intval = astc::flt2int_rtn(a_base * mode_scale); + int a_lowbits = a_intval & 0xFF; + + int a_quantval = quant_color(quant_level, a_lowbits); + int a_uquantval = a_quantval; + a_intval = (a_intval & ~0xFF) | a_uquantval; + float a_fval = static_cast<float>(a_intval) * mode_rscale; + + // Recompute C, then quantize and unquantize it + float c_fval = a_fval - color0.lane<0>(); + c_fval = astc::clamp(c_fval, 0.0f, 65535.0f); + + int c_intval = astc::flt2int_rtn(c_fval * mode_scale); + + if (c_intval >= c_intcutoff) + { + continue; + } + + int c_lowbits = c_intval & 0x3f; + + c_lowbits |= (mode & 1) << 7; + c_lowbits |= (a_intval & 0x100) >> 2; + + uint8_t c_quantval; + + quantize_and_unquantize_retain_top_two_bits( + quant_level, static_cast<uint8_t>(c_lowbits), c_quantval); + + c_intval = (c_intval & ~0x3F) | (c_quantval & 0x3F); + c_fval = static_cast<float>(c_intval) * mode_rscale; + + // Recompute B0 and B1, then quantize and unquantize them + float b0_fval = a_fval - color1.lane<1>(); + float b1_fval = a_fval - color1.lane<2>(); + + b0_fval = astc::clamp(b0_fval, 0.0f, 65535.0f); + b1_fval = astc::clamp(b1_fval, 0.0f, 65535.0f); + int b0_intval = astc::flt2int_rtn(b0_fval * mode_scale); + int b1_intval = astc::flt2int_rtn(b1_fval * mode_scale); + + if (b0_intval >= b_intcutoff || b1_intval >= b_intcutoff) + { + continue; + } + + int b0_lowbits = b0_intval & 0x3f; + int b1_lowbits = b1_intval & 0x3f; + + int bit0 = 0; + int bit1 = 0; + switch (mode) + { + case 0: + case 1: + case 3: + case 4: + case 6: + bit0 = (b0_intval >> 6) & 1; + break; + case 2: + case 5: + case 7: + bit0 = (a_intval >> 9) & 1; + break; + } + + switch (mode) + { + case 0: + case 1: + case 3: + case 4: + case 6: + bit1 = (b1_intval >> 6) & 1; + break; + case 2: + bit1 = (c_intval >> 6) & 1; + break; + case 5: + case 7: + bit1 = (a_intval >> 10) & 1; + break; + } + + b0_lowbits |= bit0 << 6; + b1_lowbits |= bit1 << 6; + + b0_lowbits |= ((mode >> 1) & 1) << 7; + b1_lowbits |= ((mode >> 2) & 1) << 7; + + uint8_t b0_quantval; + uint8_t b1_quantval; + + quantize_and_unquantize_retain_top_two_bits( + quant_level, static_cast<uint8_t>(b0_lowbits), b0_quantval); + quantize_and_unquantize_retain_top_two_bits( + quant_level, static_cast<uint8_t>(b1_lowbits), b1_quantval); + + b0_intval = (b0_intval & ~0x3f) | (b0_quantval & 0x3f); + b1_intval = (b1_intval & ~0x3f) | (b1_quantval & 0x3f); + b0_fval = static_cast<float>(b0_intval) * mode_rscale; + b1_fval = static_cast<float>(b1_intval) * mode_rscale; + + // Recompute D0 and D1, then quantize and unquantize them + float d0_fval = a_fval - b0_fval - c_fval - color0.lane<1>(); + float d1_fval = a_fval - b1_fval - c_fval - color0.lane<2>(); + + d0_fval = astc::clamp(d0_fval, -65535.0f, 65535.0f); + d1_fval = astc::clamp(d1_fval, -65535.0f, 65535.0f); + + int d0_intval = astc::flt2int_rtn(d0_fval * mode_scale); + int d1_intval = astc::flt2int_rtn(d1_fval * mode_scale); + + if (abs(d0_intval) >= d_intcutoff || abs(d1_intval) >= d_intcutoff) + { + continue; + } + + int d0_lowbits = d0_intval & 0x1f; + int d1_lowbits = d1_intval & 0x1f; + + int bit2 = 0; + int bit3 = 0; + int bit4; + int bit5; + switch (mode) + { + case 0: + case 2: + bit2 = (d0_intval >> 6) & 1; + break; + case 1: + case 4: + bit2 = (b0_intval >> 7) & 1; + break; + case 3: + bit2 = (a_intval >> 9) & 1; + break; + case 5: + bit2 = (c_intval >> 7) & 1; + break; + case 6: + case 7: + bit2 = (a_intval >> 11) & 1; + break; + } + switch (mode) + { + case 0: + case 2: + bit3 = (d1_intval >> 6) & 1; + break; + case 1: + case 4: + bit3 = (b1_intval >> 7) & 1; + break; + case 3: + case 5: + case 6: + case 7: + bit3 = (c_intval >> 6) & 1; + break; + } + + switch (mode) + { + case 4: + case 6: + bit4 = (a_intval >> 9) & 1; + bit5 = (a_intval >> 10) & 1; + break; + default: + bit4 = (d0_intval >> 5) & 1; + bit5 = (d1_intval >> 5) & 1; + break; + } + + d0_lowbits |= bit2 << 6; + d1_lowbits |= bit3 << 6; + d0_lowbits |= bit4 << 5; + d1_lowbits |= bit5 << 5; + + d0_lowbits |= (majcomp & 1) << 7; + d1_lowbits |= ((majcomp >> 1) & 1) << 7; + + uint8_t d0_quantval; + uint8_t d1_quantval; + + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(d0_lowbits), d0_quantval); + quantize_and_unquantize_retain_top_four_bits( + quant_level, static_cast<uint8_t>(d1_lowbits), d1_quantval); + + output[0] = static_cast<uint8_t>(a_quantval); + output[1] = c_quantval; + output[2] = b0_quantval; + output[3] = b1_quantval; + output[4] = d0_quantval; + output[5] = d1_quantval; + return; + } + + // If neither of the modes fit we will use a flat representation for storing data, using 8 bits + // for red and green, and 7 bits for blue. This gives color accuracy roughly similar to LDR + // 4:4:3 which is not at all great but usable. This representation is used if the light color is + // more than 4x the color value of the dark color. + float vals[6]; + vals[0] = color0_bak.lane<0>(); + vals[1] = color1_bak.lane<0>(); + vals[2] = color0_bak.lane<1>(); + vals[3] = color1_bak.lane<1>(); + vals[4] = color0_bak.lane<2>(); + vals[5] = color1_bak.lane<2>(); + + for (int i = 0; i < 6; i++) + { + vals[i] = astc::clamp(vals[i], 0.0f, 65020.0f); + } + + for (int i = 0; i < 4; i++) + { + int idx = astc::flt2int_rtn(vals[i] * 1.0f / 256.0f); + output[i] = quant_color(quant_level, idx); + } + + for (int i = 4; i < 6; i++) + { + int idx = astc::flt2int_rtn(vals[i] * 1.0f / 512.0f) + 128; + quantize_and_unquantize_retain_top_two_bits( + quant_level, static_cast<uint8_t>(idx), output[i]); + } + + return; +} + +/** + * @brief Quantize a HDR RGB + LDR A color using direct RGBA encoding. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits. + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_rgb_ldr_alpha( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + float scale = 1.0f / 257.0f; + + float a0 = astc::clamp255f(color0.lane<3>() * scale); + float a1 = astc::clamp255f(color1.lane<3>() * scale); + + output[6] = quant_color(quant_level, astc::flt2int_rtn(a0)); + output[7] = quant_color(quant_level, astc::flt2int_rtn(a1)); + + quantize_hdr_rgb(color0, color1, output, quant_level); +} + +/** + * @brief Quantize a HDR L color using the large range encoding. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed (l0, l1). + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_luminance_large_range( + vfloat4 color0, + vfloat4 color1, + uint8_t output[2], + quant_method quant_level +) { + float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f); + float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f); + + if (lum1 < lum0) + { + float avg = (lum0 + lum1) * 0.5f; + lum0 = avg; + lum1 = avg; + } + + int ilum1 = astc::flt2int_rtn(lum1); + int ilum0 = astc::flt2int_rtn(lum0); + + // Find the closest encodable point in the upper half of the code-point space + int upper_v0 = (ilum0 + 128) >> 8; + int upper_v1 = (ilum1 + 128) >> 8; + + upper_v0 = astc::clamp(upper_v0, 0, 255); + upper_v1 = astc::clamp(upper_v1, 0, 255); + + // Find the closest encodable point in the lower half of the code-point space + int lower_v0 = (ilum1 + 256) >> 8; + int lower_v1 = ilum0 >> 8; + + lower_v0 = astc::clamp(lower_v0, 0, 255); + lower_v1 = astc::clamp(lower_v1, 0, 255); + + // Determine the distance between the point in code-point space and the input value + int upper0_dec = upper_v0 << 8; + int upper1_dec = upper_v1 << 8; + int lower0_dec = (lower_v1 << 8) + 128; + int lower1_dec = (lower_v0 << 8) - 128; + + int upper0_diff = upper0_dec - ilum0; + int upper1_diff = upper1_dec - ilum1; + int lower0_diff = lower0_dec - ilum0; + int lower1_diff = lower1_dec - ilum1; + + int upper_error = (upper0_diff * upper0_diff) + (upper1_diff * upper1_diff); + int lower_error = (lower0_diff * lower0_diff) + (lower1_diff * lower1_diff); + + int v0, v1; + if (upper_error < lower_error) + { + v0 = upper_v0; + v1 = upper_v1; + } + else + { + v0 = lower_v0; + v1 = lower_v1; + } + + // OK; encode + output[0] = quant_color(quant_level, v0); + output[1] = quant_color(quant_level, v1); +} + +/** + * @brief Quantize a HDR L color using the small range encoding. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed (l0, l1) with mode bits. + * @param quant_level The quantization level to use. + * + * @return Returns @c false on failure, @c true on success. + */ +static bool try_quantize_hdr_luminance_small_range( + vfloat4 color0, + vfloat4 color1, + uint8_t output[2], + quant_method quant_level +) { + float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f); + float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f); + + if (lum1 < lum0) + { + float avg = (lum0 + lum1) * 0.5f; + lum0 = avg; + lum1 = avg; + } + + int ilum1 = astc::flt2int_rtn(lum1); + int ilum0 = astc::flt2int_rtn(lum0); + + // Difference of more than a factor-of-2 results in immediate failure + if (ilum1 - ilum0 > 2048) + { + return false; + } + + int lowval, highval, diffval; + int v0, v1; + int v0e, v1e; + int v0d, v1d; + + // Try to encode the high-precision submode + lowval = (ilum0 + 16) >> 5; + highval = (ilum1 + 16) >> 5; + + lowval = astc::clamp(lowval, 0, 2047); + highval = astc::clamp(highval, 0, 2047); + + v0 = lowval & 0x7F; + v0e = quant_color(quant_level, v0); + v0d = v0e; + + if (v0d < 0x80) + { + lowval = (lowval & ~0x7F) | v0d; + diffval = highval - lowval; + if (diffval >= 0 && diffval <= 15) + { + v1 = ((lowval >> 3) & 0xF0) | diffval; + v1e = quant_color(quant_level, v1); + v1d = v1e; + if ((v1d & 0xF0) == (v1 & 0xF0)) + { + output[0] = static_cast<uint8_t>(v0e); + output[1] = static_cast<uint8_t>(v1e); + return true; + } + } + } + + // Try to encode the low-precision submode + lowval = (ilum0 + 32) >> 6; + highval = (ilum1 + 32) >> 6; + + lowval = astc::clamp(lowval, 0, 1023); + highval = astc::clamp(highval, 0, 1023); + + v0 = (lowval & 0x7F) | 0x80; + v0e = quant_color(quant_level, v0); + v0d = v0e; + if ((v0d & 0x80) == 0) + { + return false; + } + + lowval = (lowval & ~0x7F) | (v0d & 0x7F); + diffval = highval - lowval; + if (diffval < 0 || diffval > 31) + { + return false; + } + + v1 = ((lowval >> 2) & 0xE0) | diffval; + v1e = quant_color(quant_level, v1); + v1d = v1e; + if ((v1d & 0xE0) != (v1 & 0xE0)) + { + return false; + } + + output[0] = static_cast<uint8_t>(v0e); + output[1] = static_cast<uint8_t>(v1e); + return true; +} + +/** + * @brief Quantize a HDR A color using either delta or direct RGBA encoding. + * + * @param alpha0 The input unquantized color0 endpoint. + * @param alpha1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits. + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_alpha( + float alpha0, + float alpha1, + uint8_t output[2], + quant_method quant_level +) { + alpha0 = astc::clamp(alpha0, 0.0f, 65280.0f); + alpha1 = astc::clamp(alpha1, 0.0f, 65280.0f); + + int ialpha0 = astc::flt2int_rtn(alpha0); + int ialpha1 = astc::flt2int_rtn(alpha1); + + int val0, val1, diffval; + int v6, v7; + int v6e, v7e; + int v6d, v7d; + + // Try to encode one of the delta submodes, in decreasing-precision order + for (int i = 2; i >= 0; i--) + { + val0 = (ialpha0 + (128 >> i)) >> (8 - i); + val1 = (ialpha1 + (128 >> i)) >> (8 - i); + + v6 = (val0 & 0x7F) | ((i & 1) << 7); + v6e = quant_color(quant_level, v6); + v6d = v6e; + + if ((v6 ^ v6d) & 0x80) + { + continue; + } + + val0 = (val0 & ~0x7f) | (v6d & 0x7f); + diffval = val1 - val0; + int cutoff = 32 >> i; + int mask = 2 * cutoff - 1; + + if (diffval < -cutoff || diffval >= cutoff) + { + continue; + } + + v7 = ((i & 2) << 6) | ((val0 >> 7) << (6 - i)) | (diffval & mask); + v7e = quant_color(quant_level, v7); + v7d = v7e; + + static const int testbits[3] { 0xE0, 0xF0, 0xF8 }; + + if ((v7 ^ v7d) & testbits[i]) + { + continue; + } + + output[0] = static_cast<uint8_t>(v6e); + output[1] = static_cast<uint8_t>(v7e); + return; + } + + // Could not encode any of the delta modes; instead encode a flat value + val0 = (ialpha0 + 256) >> 9; + val1 = (ialpha1 + 256) >> 9; + v6 = val0 | 0x80; + v7 = val1 | 0x80; + + output[0] = quant_color(quant_level, v6); + output[1] = quant_color(quant_level, v7); + + return; +} + +/** + * @brief Quantize a HDR RGBA color using either delta or direct RGBA encoding. + * + * @param color0 The input unquantized color0 endpoint. + * @param color1 The input unquantized color1 endpoint. + * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits. + * @param quant_level The quantization level to use. + */ +static void quantize_hdr_rgb_alpha( + vfloat4 color0, + vfloat4 color1, + uint8_t output[8], + quant_method quant_level +) { + quantize_hdr_rgb(color0, color1, output, quant_level); + quantize_hdr_alpha(color0.lane<3>(), color1.lane<3>(), output + 6, quant_level); +} + +/* See header for documentation. */ +uint8_t pack_color_endpoints( + vfloat4 color0, + vfloat4 color1, + vfloat4 rgbs_color, + vfloat4 rgbo_color, + int format, + uint8_t* output, + quant_method quant_level +) { + assert(QUANT_6 <= quant_level && quant_level <= QUANT_256); + + // We do not support negative colors + color0 = max(color0, 0.0f); + color1 = max(color1, 0.0f); + + uint8_t retval = 0; + + switch (format) + { + case FMT_RGB: + if (quant_level <= 18) + { + if (try_quantize_rgb_delta_blue_contract(color0, color1, output, quant_level)) + { + retval = FMT_RGB_DELTA; + break; + } + if (try_quantize_rgb_delta(color0, color1, output, quant_level)) + { + retval = FMT_RGB_DELTA; + break; + } + } + if (try_quantize_rgb_blue_contract(color0, color1, output, quant_level)) + { + retval = FMT_RGB; + break; + } + quantize_rgb(color0, color1, output, quant_level); + retval = FMT_RGB; + break; + + case FMT_RGBA: + if (quant_level <= 18) + { + if (try_quantize_rgba_delta_blue_contract(color0, color1, output, quant_level)) + { + retval = FMT_RGBA_DELTA; + break; + } + if (try_quantize_rgba_delta(color0, color1, output, quant_level)) + { + retval = FMT_RGBA_DELTA; + break; + } + } + if (try_quantize_rgba_blue_contract(color0, color1, output, quant_level)) + { + retval = FMT_RGBA; + break; + } + quantize_rgba(color0, color1, output, quant_level); + retval = FMT_RGBA; + break; + + case FMT_RGB_SCALE: + quantize_rgbs(rgbs_color, output, quant_level); + retval = FMT_RGB_SCALE; + break; + + case FMT_HDR_RGB_SCALE: + quantize_hdr_rgbo(rgbo_color, output, quant_level); + retval = FMT_HDR_RGB_SCALE; + break; + + case FMT_HDR_RGB: + quantize_hdr_rgb(color0, color1, output, quant_level); + retval = FMT_HDR_RGB; + break; + + case FMT_RGB_SCALE_ALPHA: + quantize_rgbs_alpha(color0, color1, rgbs_color, output, quant_level); + retval = FMT_RGB_SCALE_ALPHA; + break; + + case FMT_HDR_LUMINANCE_SMALL_RANGE: + case FMT_HDR_LUMINANCE_LARGE_RANGE: + if (try_quantize_hdr_luminance_small_range(color0, color1, output, quant_level)) + { + retval = FMT_HDR_LUMINANCE_SMALL_RANGE; + break; + } + quantize_hdr_luminance_large_range(color0, color1, output, quant_level); + retval = FMT_HDR_LUMINANCE_LARGE_RANGE; + break; + + case FMT_LUMINANCE: + quantize_luminance(color0, color1, output, quant_level); + retval = FMT_LUMINANCE; + break; + + case FMT_LUMINANCE_ALPHA: + if (quant_level <= 18) + { + if (try_quantize_luminance_alpha_delta(color0, color1, output, quant_level)) + { + retval = FMT_LUMINANCE_ALPHA_DELTA; + break; + } + } + quantize_luminance_alpha(color0, color1, output, quant_level); + retval = FMT_LUMINANCE_ALPHA; + break; + + case FMT_HDR_RGB_LDR_ALPHA: + quantize_hdr_rgb_ldr_alpha(color0, color1, output, quant_level); + retval = FMT_HDR_RGB_LDR_ALPHA; + break; + + case FMT_HDR_RGBA: + quantize_hdr_rgb_alpha(color0, color1, output, quant_level); + retval = FMT_HDR_RGBA; + break; + } + + return retval; +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_unquantize.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_unquantize.cpp new file mode 100644 index 00000000000..d31895a627b --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_color_unquantize.cpp @@ -0,0 +1,941 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#include <utility> + +/** + * @brief Functions for color unquantization. + */ + +#include "astcenc_internal.h" + +/** + * @brief Un-blue-contract a color. + * + * This function reverses any applied blue contraction. + * + * @param input The input color that has been blue-contracted. + * + * @return The uncontracted color. + */ +static ASTCENC_SIMD_INLINE vint4 uncontract_color( + vint4 input +) { + vmask4 mask(true, true, false, false); + vint4 bc0 = asr<1>(input + input.lane<2>()); + return select(input, bc0, mask); +} + +/** + * @brief Unpack an LDR RGBA color that uses delta encoding. + * + * @param input0 The packed endpoint 0 color. + * @param input1 The packed endpoint 1 color deltas. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgba_delta_unpack( + vint4 input0, + vint4 input1, + vint4& output0, + vint4& output1 +) { + // Apply bit transfer + bit_transfer_signed(input1, input0); + + // Apply blue-uncontraction if needed + int rgb_sum = hadd_rgb_s(input1); + input1 = input1 + input0; + if (rgb_sum < 0) + { + input0 = uncontract_color(input0); + input1 = uncontract_color(input1); + std::swap(input0, input1); + } + + output0 = clamp(0, 255, input0); + output1 = clamp(0, 255, input1); +} + +/** + * @brief Unpack an LDR RGB color that uses delta encoding. + * + * Output alpha set to 255. + * + * @param input0 The packed endpoint 0 color. + * @param input1 The packed endpoint 1 color deltas. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgb_delta_unpack( + vint4 input0, + vint4 input1, + vint4& output0, + vint4& output1 +) { + rgba_delta_unpack(input0, input1, output0, output1); + output0.set_lane<3>(255); + output1.set_lane<3>(255); +} + +/** + * @brief Unpack an LDR RGBA color that uses direct encoding. + * + * @param input0 The packed endpoint 0 color. + * @param input1 The packed endpoint 1 color. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgba_unpack( + vint4 input0, + vint4 input1, + vint4& output0, + vint4& output1 +) { + // Apply blue-uncontraction if needed + if (hadd_rgb_s(input0) > hadd_rgb_s(input1)) + { + input0 = uncontract_color(input0); + input1 = uncontract_color(input1); + std::swap(input0, input1); + } + + output0 = input0; + output1 = input1; +} + +/** + * @brief Unpack an LDR RGB color that uses direct encoding. + * + * Output alpha set to 255. + * + * @param input0 The packed endpoint 0 color. + * @param input1 The packed endpoint 1 color. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgb_unpack( + vint4 input0, + vint4 input1, + vint4& output0, + vint4& output1 +) { + rgba_unpack(input0, input1, output0, output1); + output0.set_lane<3>(255); + output1.set_lane<3>(255); +} + +/** + * @brief Unpack an LDR RGBA color that uses scaled encoding. + * + * Note only the RGB channels use the scaled encoding, alpha uses direct. + * + * @param input0 The packed endpoint 0 color. + * @param alpha1 The packed endpoint 1 alpha value. + * @param scale The packed quantized scale. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgb_scale_alpha_unpack( + vint4 input0, + uint8_t alpha1, + uint8_t scale, + vint4& output0, + vint4& output1 +) { + output1 = input0; + output1.set_lane<3>(alpha1); + + output0 = asr<8>(input0 * scale); + output0.set_lane<3>(input0.lane<3>()); +} + +/** + * @brief Unpack an LDR RGB color that uses scaled encoding. + * + * Output alpha is 255. + * + * @param input0 The packed endpoint 0 color. + * @param scale The packed scale. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void rgb_scale_unpack( + vint4 input0, + int scale, + vint4& output0, + vint4& output1 +) { + output1 = input0; + output1.set_lane<3>(255); + + output0 = asr<8>(input0 * scale); + output0.set_lane<3>(255); +} + +/** + * @brief Unpack an LDR L color that uses direct encoding. + * + * Output alpha is 255. + * + * @param input The packed endpoints. + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void luminance_unpack( + const uint8_t input[2], + vint4& output0, + vint4& output1 +) { + int lum0 = input[0]; + int lum1 = input[1]; + output0 = vint4(lum0, lum0, lum0, 255); + output1 = vint4(lum1, lum1, lum1, 255); +} + +/** + * @brief Unpack an LDR L color that uses delta encoding. + * + * Output alpha is 255. + * + * @param input The packed endpoints (L0, L1). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void luminance_delta_unpack( + const uint8_t input[2], + vint4& output0, + vint4& output1 +) { + int v0 = input[0]; + int v1 = input[1]; + int l0 = (v0 >> 2) | (v1 & 0xC0); + int l1 = l0 + (v1 & 0x3F); + + l1 = astc::min(l1, 255); + + output0 = vint4(l0, l0, l0, 255); + output1 = vint4(l1, l1, l1, 255); +} + +/** + * @brief Unpack an LDR LA color that uses direct encoding. + * + * @param input The packed endpoints (L0, L1, A0, A1). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void luminance_alpha_unpack( + const uint8_t input[4], + vint4& output0, + vint4& output1 +) { + int lum0 = input[0]; + int lum1 = input[1]; + int alpha0 = input[2]; + int alpha1 = input[3]; + output0 = vint4(lum0, lum0, lum0, alpha0); + output1 = vint4(lum1, lum1, lum1, alpha1); +} + +/** + * @brief Unpack an LDR LA color that uses delta encoding. + * + * @param input The packed endpoints (L0, L1, A0, A1). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void luminance_alpha_delta_unpack( + const uint8_t input[4], + vint4& output0, + vint4& output1 +) { + int lum0 = input[0]; + int lum1 = input[1]; + int alpha0 = input[2]; + int alpha1 = input[3]; + + lum0 |= (lum1 & 0x80) << 1; + alpha0 |= (alpha1 & 0x80) << 1; + lum1 &= 0x7F; + alpha1 &= 0x7F; + + if (lum1 & 0x40) + { + lum1 -= 0x80; + } + + if (alpha1 & 0x40) + { + alpha1 -= 0x80; + } + + lum0 >>= 1; + lum1 >>= 1; + alpha0 >>= 1; + alpha1 >>= 1; + lum1 += lum0; + alpha1 += alpha0; + + lum1 = astc::clamp(lum1, 0, 255); + alpha1 = astc::clamp(alpha1, 0, 255); + + output0 = vint4(lum0, lum0, lum0, alpha0); + output1 = vint4(lum1, lum1, lum1, alpha1); +} + +/** + * @brief Unpack an HDR RGB + offset encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_rgbo_unpack( + const uint8_t input[4], + vint4& output0, + vint4& output1 +) { + int v0 = input[0]; + int v1 = input[1]; + int v2 = input[2]; + int v3 = input[3]; + + int modeval = ((v0 & 0xC0) >> 6) | (((v1 & 0x80) >> 7) << 2) | (((v2 & 0x80) >> 7) << 3); + + int majcomp; + int mode; + if ((modeval & 0xC) != 0xC) + { + majcomp = modeval >> 2; + mode = modeval & 3; + } + else if (modeval != 0xF) + { + majcomp = modeval & 3; + mode = 4; + } + else + { + majcomp = 0; + mode = 5; + } + + int red = v0 & 0x3F; + int green = v1 & 0x1F; + int blue = v2 & 0x1F; + int scale = v3 & 0x1F; + + int bit0 = (v1 >> 6) & 1; + int bit1 = (v1 >> 5) & 1; + int bit2 = (v2 >> 6) & 1; + int bit3 = (v2 >> 5) & 1; + int bit4 = (v3 >> 7) & 1; + int bit5 = (v3 >> 6) & 1; + int bit6 = (v3 >> 5) & 1; + + int ohcomp = 1 << mode; + + if (ohcomp & 0x30) + green |= bit0 << 6; + if (ohcomp & 0x3A) + green |= bit1 << 5; + if (ohcomp & 0x30) + blue |= bit2 << 6; + if (ohcomp & 0x3A) + blue |= bit3 << 5; + + if (ohcomp & 0x3D) + scale |= bit6 << 5; + if (ohcomp & 0x2D) + scale |= bit5 << 6; + if (ohcomp & 0x04) + scale |= bit4 << 7; + + if (ohcomp & 0x3B) + red |= bit4 << 6; + if (ohcomp & 0x04) + red |= bit3 << 6; + + if (ohcomp & 0x10) + red |= bit5 << 7; + if (ohcomp & 0x0F) + red |= bit2 << 7; + + if (ohcomp & 0x05) + red |= bit1 << 8; + if (ohcomp & 0x0A) + red |= bit0 << 8; + + if (ohcomp & 0x05) + red |= bit0 << 9; + if (ohcomp & 0x02) + red |= bit6 << 9; + + if (ohcomp & 0x01) + red |= bit3 << 10; + if (ohcomp & 0x02) + red |= bit5 << 10; + + // expand to 12 bits. + static const int shamts[6] { 1, 1, 2, 3, 4, 5 }; + int shamt = shamts[mode]; + red <<= shamt; + green <<= shamt; + blue <<= shamt; + scale <<= shamt; + + // on modes 0 to 4, the values stored for "green" and "blue" are differentials, + // not absolute values. + if (mode != 5) + { + green = red - green; + blue = red - blue; + } + + // switch around components. + int temp; + switch (majcomp) + { + case 1: + temp = red; + red = green; + green = temp; + break; + case 2: + temp = red; + red = blue; + blue = temp; + break; + default: + break; + } + + int red0 = red - scale; + int green0 = green - scale; + int blue0 = blue - scale; + + // clamp to [0,0xFFF]. + if (red < 0) + red = 0; + if (green < 0) + green = 0; + if (blue < 0) + blue = 0; + + if (red0 < 0) + red0 = 0; + if (green0 < 0) + green0 = 0; + if (blue0 < 0) + blue0 = 0; + + output0 = vint4(red0 << 4, green0 << 4, blue0 << 4, 0x7800); + output1 = vint4(red << 4, green << 4, blue << 4, 0x7800); +} + +/** + * @brief Unpack an HDR RGB direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_rgb_unpack( + const uint8_t input[6], + vint4& output0, + vint4& output1 +) { + + int v0 = input[0]; + int v1 = input[1]; + int v2 = input[2]; + int v3 = input[3]; + int v4 = input[4]; + int v5 = input[5]; + + // extract all the fixed-placement bitfields + int modeval = ((v1 & 0x80) >> 7) | (((v2 & 0x80) >> 7) << 1) | (((v3 & 0x80) >> 7) << 2); + + int majcomp = ((v4 & 0x80) >> 7) | (((v5 & 0x80) >> 7) << 1); + + if (majcomp == 3) + { + output0 = vint4(v0 << 8, v2 << 8, (v4 & 0x7F) << 9, 0x7800); + output1 = vint4(v1 << 8, v3 << 8, (v5 & 0x7F) << 9, 0x7800); + return; + } + + int a = v0 | ((v1 & 0x40) << 2); + int b0 = v2 & 0x3f; + int b1 = v3 & 0x3f; + int c = v1 & 0x3f; + int d0 = v4 & 0x7f; + int d1 = v5 & 0x7f; + + // get hold of the number of bits in 'd0' and 'd1' + static const int dbits_tab[8] { 7, 6, 7, 6, 5, 6, 5, 6 }; + int dbits = dbits_tab[modeval]; + + // extract six variable-placement bits + int bit0 = (v2 >> 6) & 1; + int bit1 = (v3 >> 6) & 1; + int bit2 = (v4 >> 6) & 1; + int bit3 = (v5 >> 6) & 1; + int bit4 = (v4 >> 5) & 1; + int bit5 = (v5 >> 5) & 1; + + // and prepend the variable-placement bits depending on mode. + int ohmod = 1 << modeval; // one-hot-mode + if (ohmod & 0xA4) + a |= bit0 << 9; + if (ohmod & 0x8) + a |= bit2 << 9; + if (ohmod & 0x50) + a |= bit4 << 9; + + if (ohmod & 0x50) + a |= bit5 << 10; + if (ohmod & 0xA0) + a |= bit1 << 10; + + if (ohmod & 0xC0) + a |= bit2 << 11; + + if (ohmod & 0x4) + c |= bit1 << 6; + if (ohmod & 0xE8) + c |= bit3 << 6; + + if (ohmod & 0x20) + c |= bit2 << 7; + + if (ohmod & 0x5B) + { + b0 |= bit0 << 6; + b1 |= bit1 << 6; + } + + if (ohmod & 0x12) + { + b0 |= bit2 << 7; + b1 |= bit3 << 7; + } + + if (ohmod & 0xAF) + { + d0 |= bit4 << 5; + d1 |= bit5 << 5; + } + + if (ohmod & 0x5) + { + d0 |= bit2 << 6; + d1 |= bit3 << 6; + } + + // sign-extend 'd0' and 'd1' + // note: this code assumes that signed right-shift actually sign-fills, not zero-fills. + int32_t d0x = d0; + int32_t d1x = d1; + int sx_shamt = 32 - dbits; + d0x <<= sx_shamt; + d0x >>= sx_shamt; + d1x <<= sx_shamt; + d1x >>= sx_shamt; + d0 = d0x; + d1 = d1x; + + // expand all values to 12 bits, with left-shift as needed. + int val_shamt = (modeval >> 1) ^ 3; + a <<= val_shamt; + b0 <<= val_shamt; + b1 <<= val_shamt; + c <<= val_shamt; + d0 <<= val_shamt; + d1 <<= val_shamt; + + // then compute the actual color values. + int red1 = a; + int green1 = a - b0; + int blue1 = a - b1; + int red0 = a - c; + int green0 = a - b0 - c - d0; + int blue0 = a - b1 - c - d1; + + // clamp the color components to [0,2^12 - 1] + red0 = astc::clamp(red0, 0, 4095); + green0 = astc::clamp(green0, 0, 4095); + blue0 = astc::clamp(blue0, 0, 4095); + + red1 = astc::clamp(red1, 0, 4095); + green1 = astc::clamp(green1, 0, 4095); + blue1 = astc::clamp(blue1, 0, 4095); + + // switch around the color components + int temp0, temp1; + switch (majcomp) + { + case 1: // switch around red and green + temp0 = red0; + temp1 = red1; + red0 = green0; + red1 = green1; + green0 = temp0; + green1 = temp1; + break; + case 2: // switch around red and blue + temp0 = red0; + temp1 = red1; + red0 = blue0; + red1 = blue1; + blue0 = temp0; + blue1 = temp1; + break; + case 0: // no switch + break; + } + + output0 = vint4(red0 << 4, green0 << 4, blue0 << 4, 0x7800); + output1 = vint4(red1 << 4, green1 << 4, blue1 << 4, 0x7800); +} + +/** + * @brief Unpack an HDR RGB + LDR A direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_rgb_ldr_alpha_unpack( + const uint8_t input[8], + vint4& output0, + vint4& output1 +) { + hdr_rgb_unpack(input, output0, output1); + + int v6 = input[6]; + int v7 = input[7]; + output0.set_lane<3>(v6); + output1.set_lane<3>(v7); +} + +/** + * @brief Unpack an HDR L (small range) direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_luminance_small_range_unpack( + const uint8_t input[2], + vint4& output0, + vint4& output1 +) { + int v0 = input[0]; + int v1 = input[1]; + + int y0, y1; + if (v0 & 0x80) + { + y0 = ((v1 & 0xE0) << 4) | ((v0 & 0x7F) << 2); + y1 = (v1 & 0x1F) << 2; + } + else + { + y0 = ((v1 & 0xF0) << 4) | ((v0 & 0x7F) << 1); + y1 = (v1 & 0xF) << 1; + } + + y1 += y0; + if (y1 > 0xFFF) + { + y1 = 0xFFF; + } + + output0 = vint4(y0 << 4, y0 << 4, y0 << 4, 0x7800); + output1 = vint4(y1 << 4, y1 << 4, y1 << 4, 0x7800); +} + +/** + * @brief Unpack an HDR L (large range) direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_luminance_large_range_unpack( + const uint8_t input[2], + vint4& output0, + vint4& output1 +) { + int v0 = input[0]; + int v1 = input[1]; + + int y0, y1; + if (v1 >= v0) + { + y0 = v0 << 4; + y1 = v1 << 4; + } + else + { + y0 = (v1 << 4) + 8; + y1 = (v0 << 4) - 8; + } + + output0 = vint4(y0 << 4, y0 << 4, y0 << 4, 0x7800); + output1 = vint4(y1 << 4, y1 << 4, y1 << 4, 0x7800); +} + +/** + * @brief Unpack an HDR A direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_alpha_unpack( + const uint8_t input[2], + int& output0, + int& output1 +) { + + int v6 = input[0]; + int v7 = input[1]; + + int selector = ((v6 >> 7) & 1) | ((v7 >> 6) & 2); + v6 &= 0x7F; + v7 &= 0x7F; + if (selector == 3) + { + output0 = v6 << 5; + output1 = v7 << 5; + } + else + { + v6 |= (v7 << (selector + 1)) & 0x780; + v7 &= (0x3f >> selector); + v7 ^= 32 >> selector; + v7 -= 32 >> selector; + v6 <<= (4 - selector); + v7 <<= (4 - selector); + v7 += v6; + + if (v7 < 0) + { + v7 = 0; + } + else if (v7 > 0xFFF) + { + v7 = 0xFFF; + } + + output0 = v6; + output1 = v7; + } + + output0 <<= 4; + output1 <<= 4; +} + +/** + * @brief Unpack an HDR RGBA direct encoding. + * + * @param input The packed endpoints (packed and modal). + * @param[out] output0 The unpacked endpoint 0 color. + * @param[out] output1 The unpacked endpoint 1 color. + */ +static void hdr_rgb_hdr_alpha_unpack( + const uint8_t input[8], + vint4& output0, + vint4& output1 +) { + hdr_rgb_unpack(input, output0, output1); + + int alpha0, alpha1; + hdr_alpha_unpack(input + 6, alpha0, alpha1); + + output0.set_lane<3>(alpha0); + output1.set_lane<3>(alpha1); +} + +/* See header for documentation. */ +void unpack_color_endpoints( + astcenc_profile decode_mode, + int format, + const uint8_t* input, + bool& rgb_hdr, + bool& alpha_hdr, + vint4& output0, + vint4& output1 +) { + // Assume no NaNs and LDR endpoints unless set later + rgb_hdr = false; + alpha_hdr = false; + + bool alpha_hdr_default = false; + + switch (format) + { + case FMT_LUMINANCE: + luminance_unpack(input, output0, output1); + break; + + case FMT_LUMINANCE_DELTA: + luminance_delta_unpack(input, output0, output1); + break; + + case FMT_HDR_LUMINANCE_SMALL_RANGE: + rgb_hdr = true; + alpha_hdr_default = true; + hdr_luminance_small_range_unpack(input, output0, output1); + break; + + case FMT_HDR_LUMINANCE_LARGE_RANGE: + rgb_hdr = true; + alpha_hdr_default = true; + hdr_luminance_large_range_unpack(input, output0, output1); + break; + + case FMT_LUMINANCE_ALPHA: + luminance_alpha_unpack(input, output0, output1); + break; + + case FMT_LUMINANCE_ALPHA_DELTA: + luminance_alpha_delta_unpack(input, output0, output1); + break; + + case FMT_RGB_SCALE: + { + vint4 input0q(input[0], input[1], input[2], 0); + uint8_t scale = input[3]; + rgb_scale_unpack(input0q, scale, output0, output1); + } + break; + + case FMT_RGB_SCALE_ALPHA: + { + vint4 input0q(input[0], input[1], input[2], input[4]); + uint8_t alpha1q = input[5]; + uint8_t scaleq = input[3]; + rgb_scale_alpha_unpack(input0q, alpha1q, scaleq, output0, output1); + } + break; + + case FMT_HDR_RGB_SCALE: + rgb_hdr = true; + alpha_hdr_default = true; + hdr_rgbo_unpack(input, output0, output1); + break; + + case FMT_RGB: + { + vint4 input0q(input[0], input[2], input[4], 0); + vint4 input1q(input[1], input[3], input[5], 0); + rgb_unpack(input0q, input1q, output0, output1); + } + break; + + case FMT_RGB_DELTA: + { + vint4 input0q(input[0], input[2], input[4], 0); + vint4 input1q(input[1], input[3], input[5], 0); + rgb_delta_unpack(input0q, input1q, output0, output1); + } + break; + + case FMT_HDR_RGB: + rgb_hdr = true; + alpha_hdr_default = true; + hdr_rgb_unpack(input, output0, output1); + break; + + case FMT_RGBA: + { + vint4 input0q(input[0], input[2], input[4], input[6]); + vint4 input1q(input[1], input[3], input[5], input[7]); + rgba_unpack(input0q, input1q, output0, output1); + } + break; + + case FMT_RGBA_DELTA: + { + vint4 input0q(input[0], input[2], input[4], input[6]); + vint4 input1q(input[1], input[3], input[5], input[7]); + rgba_delta_unpack(input0q, input1q, output0, output1); + } + break; + + case FMT_HDR_RGB_LDR_ALPHA: + rgb_hdr = true; + hdr_rgb_ldr_alpha_unpack(input, output0, output1); + break; + + case FMT_HDR_RGBA: + rgb_hdr = true; + alpha_hdr = true; + hdr_rgb_hdr_alpha_unpack(input, output0, output1); + break; + } + + // Assign a correct default alpha + if (alpha_hdr_default) + { + if (decode_mode == ASTCENC_PRF_HDR) + { + output0.set_lane<3>(0x7800); + output1.set_lane<3>(0x7800); + alpha_hdr = true; + } + else + { + output0.set_lane<3>(0x00FF); + output1.set_lane<3>(0x00FF); + alpha_hdr = false; + } + } + + vint4 ldr_scale(257); + vint4 hdr_scale(1); + vint4 output_scale = ldr_scale; + + // An LDR profile image + if ((decode_mode == ASTCENC_PRF_LDR) || + (decode_mode == ASTCENC_PRF_LDR_SRGB)) + { + // Also matches HDR alpha, as cannot have HDR alpha without HDR RGB + if (rgb_hdr == true) + { + output0 = vint4(0xFF00, 0x0000, 0xFF00, 0xFF00); + output1 = vint4(0xFF00, 0x0000, 0xFF00, 0xFF00); + output_scale = hdr_scale; + + rgb_hdr = false; + alpha_hdr = false; + } + } + // An HDR profile image + else + { + vmask4 hdr_lanes(rgb_hdr, rgb_hdr, rgb_hdr, alpha_hdr); + output_scale = select(ldr_scale, hdr_scale, hdr_lanes); + } + + output0 = output0 * output_scale; + output1 = output1 * output_scale; +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compress_symbolic.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compress_symbolic.cpp new file mode 100644 index 00000000000..4dbe6f1946f --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compress_symbolic.cpp @@ -0,0 +1,1459 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions to compress a symbolic block. + */ + +#include "astcenc_internal.h" +#include "astcenc_diagnostic_trace.h" + +#include <cassert> + +/** + * @brief Merge two planes of endpoints into a single vector. + * + * @param ep_plane1 The endpoints for plane 1. + * @param ep_plane2 The endpoints for plane 2. + * @param component_plane2 The color component for plane 2. + * @param[out] result The merged output. + */ +static void merge_endpoints( + const endpoints& ep_plane1, + const endpoints& ep_plane2, + unsigned int component_plane2, + endpoints& result +) { + unsigned int partition_count = ep_plane1.partition_count; + assert(partition_count == 1); + + vmask4 sep_mask = vint4::lane_id() == vint4(component_plane2); + + result.partition_count = partition_count; + result.endpt0[0] = select(ep_plane1.endpt0[0], ep_plane2.endpt0[0], sep_mask); + result.endpt1[0] = select(ep_plane1.endpt1[0], ep_plane2.endpt1[0], sep_mask); +} + +/** + * @brief Attempt to improve weights given a chosen configuration. + * + * Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per + * partition and per plane) and attempt to improve image quality by moving each weight up by one or + * down by one quantization step. + * + * This is a specialized function which only supports operating on undecimated weight grids, + * therefore primarily improving the performance of 4x4 and 5x5 blocks where grid decimation + * is needed less often. + * + * @param decode_mode The decode mode (LDR, HDR). + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param[out] scb The symbolic compressed block output. + */ +static bool realign_weights_undecimated( + astcenc_profile decode_mode, + const block_size_descriptor& bsd, + const image_block& blk, + symbolic_compressed_block& scb +) { + // Get the partition descriptor + unsigned int partition_count = scb.partition_count; + const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); + + // Get the quantization table + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + unsigned int weight_quant_level = bm.quant_mode; + const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level]; + + unsigned int max_plane = bm.is_dual_plane; + int plane2_component = bm.is_dual_plane ? scb.plane2_component : -1; + vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component); + + // Decode the color endpoints + bool rgb_hdr; + bool alpha_hdr; + vint4 endpnt0[BLOCK_MAX_PARTITIONS]; + vint4 endpnt1[BLOCK_MAX_PARTITIONS]; + vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS]; + vfloat4 offset[BLOCK_MAX_PARTITIONS]; + + promise(partition_count > 0); + + for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) + { + unpack_color_endpoints(decode_mode, + scb.color_formats[pa_idx], + scb.color_values[pa_idx], + rgb_hdr, alpha_hdr, + endpnt0[pa_idx], + endpnt1[pa_idx]); + } + + uint8_t* dec_weights_uquant = scb.weights; + bool adjustments = false; + + // For each plane and partition ... + for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++) + { + for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) + { + // Compute the endpoint delta for all components in current plane + vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx]; + epd = select(epd, vint4::zero(), plane_mask); + + endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]); + offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f); + } + + // For each weight compute previous, current, and next errors + promise(bsd.texel_count > 0); + for (unsigned int texel = 0; texel < bsd.texel_count; texel++) + { + int uqw = dec_weights_uquant[texel]; + + uint32_t prev_and_next = qat.prev_next_values[uqw]; + int uqw_down = prev_and_next & 0xFF; + int uqw_up = (prev_and_next >> 8) & 0xFF; + + // Interpolate the colors to create the diffs + float weight_base = static_cast<float>(uqw); + float weight_down = static_cast<float>(uqw_down - uqw); + float weight_up = static_cast<float>(uqw_up - uqw); + + unsigned int partition = pi.partition_of_texel[texel]; + vfloat4 color_offset = offset[partition]; + vfloat4 color_base = endpnt0f[partition]; + + vfloat4 color = color_base + color_offset * weight_base; + vfloat4 orig_color = blk.texel(texel); + vfloat4 error_weight = blk.channel_weight; + + vfloat4 color_diff = color - orig_color; + vfloat4 color_diff_down = color_diff + color_offset * weight_down; + vfloat4 color_diff_up = color_diff + color_offset * weight_up; + + float error_base = dot_s(color_diff * color_diff, error_weight); + float error_down = dot_s(color_diff_down * color_diff_down, error_weight); + float error_up = dot_s(color_diff_up * color_diff_up, error_weight); + + // Check if the prev or next error is better, and if so use it + if ((error_up < error_base) && (error_up < error_down) && (uqw < 64)) + { + dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_up); + adjustments = true; + } + else if ((error_down < error_base) && (uqw > 0)) + { + dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_down); + adjustments = true; + } + } + + // Prepare iteration for plane 2 + dec_weights_uquant += WEIGHTS_PLANE2_OFFSET; + plane_mask = ~plane_mask; + } + + return adjustments; +} + +/** + * @brief Attempt to improve weights given a chosen configuration. + * + * Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per + * partition and per plane) and attempt to improve image quality by moving each weight up by one or + * down by one quantization step. + * + * @param decode_mode The decode mode (LDR, HDR). + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param[out] scb The symbolic compressed block output. + */ +static bool realign_weights_decimated( + astcenc_profile decode_mode, + const block_size_descriptor& bsd, + const image_block& blk, + symbolic_compressed_block& scb +) { + // Get the partition descriptor + unsigned int partition_count = scb.partition_count; + const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); + + // Get the quantization table + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + unsigned int weight_quant_level = bm.quant_mode; + const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level]; + + // Get the decimation table + const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); + unsigned int weight_count = di.weight_count; + assert(weight_count != bsd.texel_count); + + unsigned int max_plane = bm.is_dual_plane; + int plane2_component = bm.is_dual_plane ? scb.plane2_component : -1; + vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component); + + // Decode the color endpoints + bool rgb_hdr; + bool alpha_hdr; + vint4 endpnt0[BLOCK_MAX_PARTITIONS]; + vint4 endpnt1[BLOCK_MAX_PARTITIONS]; + vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS]; + vfloat4 offset[BLOCK_MAX_PARTITIONS]; + + promise(partition_count > 0); + promise(weight_count > 0); + + for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) + { + unpack_color_endpoints(decode_mode, + scb.color_formats[pa_idx], + scb.color_values[pa_idx], + rgb_hdr, alpha_hdr, + endpnt0[pa_idx], + endpnt1[pa_idx]); + } + + uint8_t* dec_weights_uquant = scb.weights; + bool adjustments = false; + + // For each plane and partition ... + for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++) + { + for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) + { + // Compute the endpoint delta for all components in current plane + vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx]; + epd = select(epd, vint4::zero(), plane_mask); + + endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]); + offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f); + } + + // Create an unquantized weight grid for this decimation level + alignas(ASTCENC_VECALIGN) float uq_weightsf[BLOCK_MAX_WEIGHTS]; + for (unsigned int we_idx = 0; we_idx < weight_count; we_idx += ASTCENC_SIMD_WIDTH) + { + vint unquant_value(dec_weights_uquant + we_idx); + vfloat unquant_valuef = int_to_float(unquant_value); + storea(unquant_valuef, uq_weightsf + we_idx); + } + + // For each weight compute previous, current, and next errors + for (unsigned int we_idx = 0; we_idx < weight_count; we_idx++) + { + int uqw = dec_weights_uquant[we_idx]; + uint32_t prev_and_next = qat.prev_next_values[uqw]; + + float uqw_base = uq_weightsf[we_idx]; + float uqw_down = static_cast<float>(prev_and_next & 0xFF); + float uqw_up = static_cast<float>((prev_and_next >> 8) & 0xFF); + + float uqw_diff_down = uqw_down - uqw_base; + float uqw_diff_up = uqw_up - uqw_base; + + vfloat4 error_basev = vfloat4::zero(); + vfloat4 error_downv = vfloat4::zero(); + vfloat4 error_upv = vfloat4::zero(); + + // Interpolate the colors to create the diffs + unsigned int texels_to_evaluate = di.weight_texel_count[we_idx]; + promise(texels_to_evaluate > 0); + for (unsigned int te_idx = 0; te_idx < texels_to_evaluate; te_idx++) + { + unsigned int texel = di.weight_texel[te_idx][we_idx]; + + const uint8_t *texel_weights = di.texel_weights_texel[we_idx][te_idx]; + const float *texel_weights_float = di.texel_weights_float_texel[we_idx][te_idx]; + + float tw_base = texel_weights_float[0]; + + float weight_base = (uqw_base * tw_base + + uq_weightsf[texel_weights[1]] * texel_weights_float[1]) + + (uq_weightsf[texel_weights[2]] * texel_weights_float[2] + + uq_weightsf[texel_weights[3]] * texel_weights_float[3]); + + // Ideally this is integer rounded, but IQ gain it isn't worth the overhead + // float weight = astc::flt_rd(weight_base + 0.5f); + // float weight_down = astc::flt_rd(weight_base + 0.5f + uqw_diff_down * tw_base) - weight; + // float weight_up = astc::flt_rd(weight_base + 0.5f + uqw_diff_up * tw_base) - weight; + float weight_down = weight_base + uqw_diff_down * tw_base - weight_base; + float weight_up = weight_base + uqw_diff_up * tw_base - weight_base; + + unsigned int partition = pi.partition_of_texel[texel]; + vfloat4 color_offset = offset[partition]; + vfloat4 color_base = endpnt0f[partition]; + + vfloat4 color = color_base + color_offset * weight_base; + vfloat4 orig_color = blk.texel(texel); + + vfloat4 color_diff = color - orig_color; + vfloat4 color_down_diff = color_diff + color_offset * weight_down; + vfloat4 color_up_diff = color_diff + color_offset * weight_up; + + error_basev += color_diff * color_diff; + error_downv += color_down_diff * color_down_diff; + error_upv += color_up_diff * color_up_diff; + } + + vfloat4 error_weight = blk.channel_weight; + float error_base = hadd_s(error_basev * error_weight); + float error_down = hadd_s(error_downv * error_weight); + float error_up = hadd_s(error_upv * error_weight); + + // Check if the prev or next error is better, and if so use it + if ((error_up < error_base) && (error_up < error_down) && (uqw < 64)) + { + uq_weightsf[we_idx] = uqw_up; + dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_up); + adjustments = true; + } + else if ((error_down < error_base) && (uqw > 0)) + { + uq_weightsf[we_idx] = uqw_down; + dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_down); + adjustments = true; + } + } + + // Prepare iteration for plane 2 + dec_weights_uquant += WEIGHTS_PLANE2_OFFSET; + plane_mask = ~plane_mask; + } + + return adjustments; +} + +/** + * @brief Compress a block using a chosen partitioning and 1 plane of weights. + * + * @param config The compressor configuration. + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param only_always True if we only use "always" percentile block modes. + * @param tune_errorval_threshold The error value threshold. + * @param partition_count The partition count. + * @param partition_index The partition index if @c partition_count is 2-4. + * @param[out] scb The symbolic compressed block output. + * @param[out] tmpbuf The quantized weights for plane 1. + */ +static float compress_symbolic_block_for_partition_1plane( + const astcenc_config& config, + const block_size_descriptor& bsd, + const image_block& blk, + bool only_always, + float tune_errorval_threshold, + unsigned int partition_count, + unsigned int partition_index, + symbolic_compressed_block& scb, + compression_working_buffers& tmpbuf, + int quant_limit +) { + promise(partition_count > 0); + promise(config.tune_candidate_limit > 0); + promise(config.tune_refinement_limit > 0); + + int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit); + + auto compute_difference = &compute_symbolic_block_difference_1plane; + if ((partition_count == 1) && !(config.flags & ASTCENC_FLG_MAP_RGBM)) + { + compute_difference = &compute_symbolic_block_difference_1plane_1partition; + } + + const auto& pi = bsd.get_partition_info(partition_count, partition_index); + + // Compute ideal weights and endpoint colors, with no quantization or decimation + endpoints_and_weights& ei = tmpbuf.ei1; + compute_ideal_colors_and_weights_1plane(blk, pi, ei); + + // Compute ideal weights and endpoint colors for every decimation + float* dec_weights_ideal = tmpbuf.dec_weights_ideal; + uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant; + + // For each decimation mode, compute an ideal set of weights with no quantization + unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always + : bsd.decimation_mode_count_selected; + promise(max_decimation_modes > 0); + for (unsigned int i = 0; i < max_decimation_modes; i++) + { + const auto& dm = bsd.get_decimation_mode(i); + if (!dm.is_ref_1_plane(static_cast<quant_method>(max_weight_quant))) + { + continue; + } + + const auto& di = bsd.get_decimation_info(i); + + compute_ideal_weights_for_decimation( + ei, + di, + dec_weights_ideal + i * BLOCK_MAX_WEIGHTS); + } + + // Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal + // weight pair, compute the smallest weight that will result in a color value greater than 1 + vfloat4 min_ep(10.0f); + for (unsigned int i = 0; i < partition_count; i++) + { + vfloat4 ep = (vfloat4(1.0f) - ei.ep.endpt0[i]) / (ei.ep.endpt1[i] - ei.ep.endpt0[i]); + + vmask4 use_ep = (ep > vfloat4(0.5f)) & (ep < min_ep); + min_ep = select(min_ep, ep, use_ep); + } + + float min_wt_cutoff = hmin_s(min_ep); + + // For each mode, use the angular method to compute a shift + compute_angular_endpoints_1plane( + only_always, bsd, dec_weights_ideal, max_weight_quant, tmpbuf); + + float* weight_low_value = tmpbuf.weight_low_value1; + float* weight_high_value = tmpbuf.weight_high_value1; + int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts; + float* qwt_errors = tmpbuf.qwt_errors; + + // For each mode (which specifies a decimation and a quantization): + // * Compute number of bits needed for the quantized weights + // * Generate an optimized set of quantized weights + // * Compute quantization errors for the mode + + + static const int8_t free_bits_for_partition_count[4] { + 115 - 4, 111 - 4 - PARTITION_INDEX_BITS, 108 - 4 - PARTITION_INDEX_BITS, 105 - 4 - PARTITION_INDEX_BITS + }; + + unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always + : bsd.block_mode_count_1plane_selected; + promise(max_block_modes > 0); + for (unsigned int i = 0; i < max_block_modes; i++) + { + const block_mode& bm = bsd.block_modes[i]; + + if (bm.quant_mode > max_weight_quant) + { + qwt_errors[i] = 1e38f; + continue; + } + + assert(!bm.is_dual_plane); + int bitcount = free_bits_for_partition_count[partition_count - 1] - bm.weight_bits; + if (bitcount <= 0) + { + qwt_errors[i] = 1e38f; + continue; + } + + if (weight_high_value[i] > 1.02f * min_wt_cutoff) + { + weight_high_value[i] = 1.0f; + } + + int decimation_mode = bm.decimation_mode; + const auto& di = bsd.get_decimation_info(decimation_mode); + + qwt_bitcounts[i] = static_cast<int8_t>(bitcount); + + alignas(ASTCENC_VECALIGN) float dec_weights_uquantf[BLOCK_MAX_WEIGHTS]; + + // Generate the optimized set of weights for the weight mode + compute_quantized_weights_for_decimation( + di, + weight_low_value[i], weight_high_value[i], + dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode, + dec_weights_uquantf, + dec_weights_uquant + BLOCK_MAX_WEIGHTS * i, + bm.get_weight_quant_mode()); + + // Compute weight quantization errors for the block mode + qwt_errors[i] = compute_error_of_weight_set_1plane( + ei, + di, + dec_weights_uquantf); + } + + // Decide the optimal combination of color endpoint encodings and weight encodings + uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS]; + int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES]; + + quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES]; + quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES]; + + unsigned int candidate_count = compute_ideal_endpoint_formats( + pi, blk, ei.ep, qwt_bitcounts, qwt_errors, + config.tune_candidate_limit, 0, max_block_modes, + partition_format_specifiers, block_mode_index, + color_quant_level, color_quant_level_mod, tmpbuf); + + // Iterate over the N believed-to-be-best modes to find out which one is actually best + float best_errorval_in_mode = ERROR_CALC_DEFAULT; + float best_errorval_in_scb = scb.errorval; + + for (unsigned int i = 0; i < candidate_count; i++) + { + TRACE_NODE(node0, "candidate"); + + const int bm_packed_index = block_mode_index[i]; + assert(bm_packed_index >= 0 && bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_selected)); + const block_mode& qw_bm = bsd.block_modes[bm_packed_index]; + + int decimation_mode = qw_bm.decimation_mode; + const auto& di = bsd.get_decimation_info(decimation_mode); + promise(di.weight_count > 0); + + trace_add_data("weight_x", di.weight_x); + trace_add_data("weight_y", di.weight_y); + trace_add_data("weight_z", di.weight_z); + trace_add_data("weight_quant", qw_bm.quant_mode); + + // Recompute the ideal color endpoints before storing them + vfloat4 rgbs_colors[BLOCK_MAX_PARTITIONS]; + vfloat4 rgbo_colors[BLOCK_MAX_PARTITIONS]; + + symbolic_compressed_block workscb; + endpoints workep = ei.ep; + + uint8_t* u8_weight_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index; + + for (unsigned int j = 0; j < di.weight_count; j++) + { + workscb.weights[j] = u8_weight_src[j]; + } + + for (unsigned int l = 0; l < config.tune_refinement_limit; l++) + { + recompute_ideal_colors_1plane( + blk, pi, di, workscb.weights, + workep, rgbs_colors, rgbo_colors); + + // Quantize the chosen color, tracking if worth trying the mod value + bool all_same = color_quant_level[i] != color_quant_level_mod[i]; + for (unsigned int j = 0; j < partition_count; j++) + { + workscb.color_formats[j] = pack_color_endpoints( + workep.endpt0[j], + workep.endpt1[j], + rgbs_colors[j], + rgbo_colors[j], + partition_format_specifiers[i][j], + workscb.color_values[j], + color_quant_level[i]); + + all_same = all_same && workscb.color_formats[j] == workscb.color_formats[0]; + } + + // If all the color endpoint modes are the same, we get a few more bits to store colors; + // let's see if we can take advantage of this: requantize all the colors and see if the + // endpoint modes remain the same. + workscb.color_formats_matched = 0; + if (partition_count >= 2 && all_same) + { + uint8_t colorvals[BLOCK_MAX_PARTITIONS][12]; + uint8_t color_formats_mod[BLOCK_MAX_PARTITIONS] { 0 }; + bool all_same_mod = true; + for (unsigned int j = 0; j < partition_count; j++) + { + color_formats_mod[j] = pack_color_endpoints( + workep.endpt0[j], + workep.endpt1[j], + rgbs_colors[j], + rgbo_colors[j], + partition_format_specifiers[i][j], + colorvals[j], + color_quant_level_mod[i]); + + // Early out as soon as it's no longer possible to use mod + if (color_formats_mod[j] != color_formats_mod[0]) + { + all_same_mod = false; + break; + } + } + + if (all_same_mod) + { + workscb.color_formats_matched = 1; + for (unsigned int j = 0; j < BLOCK_MAX_PARTITIONS; j++) + { + for (unsigned int k = 0; k < 8; k++) + { + workscb.color_values[j][k] = colorvals[j][k]; + } + + workscb.color_formats[j] = color_formats_mod[j]; + } + } + } + + // Store header fields + workscb.partition_count = static_cast<uint8_t>(partition_count); + workscb.partition_index = static_cast<uint16_t>(partition_index); + workscb.plane2_component = -1; + workscb.quant_mode = workscb.color_formats_matched ? color_quant_level_mod[i] : color_quant_level[i]; + workscb.block_mode = qw_bm.mode_index; + workscb.block_type = SYM_BTYPE_NONCONST; + + // Pre-realign test + if (l == 0) + { + float errorval = compute_difference(config, bsd, workscb, blk); + if (errorval == -ERROR_CALC_DEFAULT) + { + errorval = -errorval; + workscb.block_type = SYM_BTYPE_ERROR; + } + + trace_add_data("error_prerealign", errorval); + best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); + + // Average refinement improvement is 3.5% per iteration (allow 5%), but the first + // iteration can help more so we give it a extra 10% leeway. Use this knowledge to + // drive a heuristic to skip blocks that are unlikely to catch up with the best + // block we have already. + unsigned int iters_remaining = config.tune_refinement_limit - l; + float threshold = (0.05f * static_cast<float>(iters_remaining)) + 1.1f; + if (errorval > (threshold * best_errorval_in_scb)) + { + break; + } + + if (errorval < best_errorval_in_scb) + { + best_errorval_in_scb = errorval; + workscb.errorval = errorval; + scb = workscb; + + if (errorval < tune_errorval_threshold) + { + // Skip remaining candidates - this is "good enough" + i = candidate_count; + break; + } + } + } + + bool adjustments; + if (di.weight_count != bsd.texel_count) + { + adjustments = realign_weights_decimated( + config.profile, bsd, blk, workscb); + } + else + { + adjustments = realign_weights_undecimated( + config.profile, bsd, blk, workscb); + } + + // Post-realign test + float errorval = compute_difference(config, bsd, workscb, blk); + if (errorval == -ERROR_CALC_DEFAULT) + { + errorval = -errorval; + workscb.block_type = SYM_BTYPE_ERROR; + } + + trace_add_data("error_postrealign", errorval); + best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); + + // Average refinement improvement is 3.5% per iteration, so skip blocks that are + // unlikely to catch up with the best block we have already. Assume a 5% per step to + // give benefit of the doubt ... + unsigned int iters_remaining = config.tune_refinement_limit - 1 - l; + float threshold = (0.05f * static_cast<float>(iters_remaining)) + 1.0f; + if (errorval > (threshold * best_errorval_in_scb)) + { + break; + } + + if (errorval < best_errorval_in_scb) + { + best_errorval_in_scb = errorval; + workscb.errorval = errorval; + scb = workscb; + + if (errorval < tune_errorval_threshold) + { + // Skip remaining candidates - this is "good enough" + i = candidate_count; + break; + } + } + + if (!adjustments) + { + break; + } + } + } + + return best_errorval_in_mode; +} + +/** + * @brief Compress a block using a chosen partitioning and 2 planes of weights. + * + * @param config The compressor configuration. + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param tune_errorval_threshold The error value threshold. + * @param plane2_component The component index for the second plane of weights. + * @param[out] scb The symbolic compressed block output. + * @param[out] tmpbuf The quantized weights for plane 1. + */ +static float compress_symbolic_block_for_partition_2planes( + const astcenc_config& config, + const block_size_descriptor& bsd, + const image_block& blk, + float tune_errorval_threshold, + unsigned int plane2_component, + symbolic_compressed_block& scb, + compression_working_buffers& tmpbuf, + int quant_limit +) { + promise(config.tune_candidate_limit > 0); + promise(config.tune_refinement_limit > 0); + promise(bsd.decimation_mode_count_selected > 0); + + int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit); + + // Compute ideal weights and endpoint colors, with no quantization or decimation + endpoints_and_weights& ei1 = tmpbuf.ei1; + endpoints_and_weights& ei2 = tmpbuf.ei2; + + compute_ideal_colors_and_weights_2planes(bsd, blk, plane2_component, ei1, ei2); + + // Compute ideal weights and endpoint colors for every decimation + float* dec_weights_ideal = tmpbuf.dec_weights_ideal; + uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant; + + // For each decimation mode, compute an ideal set of weights with no quantization + for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++) + { + const auto& dm = bsd.get_decimation_mode(i); + if (!dm.is_ref_2_plane(static_cast<quant_method>(max_weight_quant))) + { + continue; + } + + const auto& di = bsd.get_decimation_info(i); + + compute_ideal_weights_for_decimation( + ei1, + di, + dec_weights_ideal + i * BLOCK_MAX_WEIGHTS); + + compute_ideal_weights_for_decimation( + ei2, + di, + dec_weights_ideal + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET); + } + + // Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal + // weight pair, compute the smallest weight that will result in a color value greater than 1 + vfloat4 min_ep1(10.0f); + vfloat4 min_ep2(10.0f); + + vfloat4 ep1 = (vfloat4(1.0f) - ei1.ep.endpt0[0]) / (ei1.ep.endpt1[0] - ei1.ep.endpt0[0]); + vmask4 use_ep1 = (ep1 > vfloat4(0.5f)) & (ep1 < min_ep1); + min_ep1 = select(min_ep1, ep1, use_ep1); + + vfloat4 ep2 = (vfloat4(1.0f) - ei2.ep.endpt0[0]) / (ei2.ep.endpt1[0] - ei2.ep.endpt0[0]); + vmask4 use_ep2 = (ep2 > vfloat4(0.5f)) & (ep2 < min_ep2); + min_ep2 = select(min_ep2, ep2, use_ep2); + + vfloat4 err_max(ERROR_CALC_DEFAULT); + vmask4 err_mask = vint4::lane_id() == vint4(plane2_component); + + // Set the plane2 component to max error in ep1 + min_ep1 = select(min_ep1, err_max, err_mask); + + float min_wt_cutoff1 = hmin_s(min_ep1); + + // Set the minwt2 to the plane2 component min in ep2 + float min_wt_cutoff2 = hmin_s(select(err_max, min_ep2, err_mask)); + + compute_angular_endpoints_2planes( + bsd, dec_weights_ideal, max_weight_quant, tmpbuf); + + // For each mode (which specifies a decimation and a quantization): + // * Compute number of bits needed for the quantized weights + // * Generate an optimized set of quantized weights + // * Compute quantization errors for the mode + + float* weight_low_value1 = tmpbuf.weight_low_value1; + float* weight_high_value1 = tmpbuf.weight_high_value1; + float* weight_low_value2 = tmpbuf.weight_low_value2; + float* weight_high_value2 = tmpbuf.weight_high_value2; + + int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts; + float* qwt_errors = tmpbuf.qwt_errors; + + unsigned int start_2plane = bsd.block_mode_count_1plane_selected; + unsigned int end_2plane = bsd.block_mode_count_1plane_2plane_selected; + + for (unsigned int i = start_2plane; i < end_2plane; i++) + { + const block_mode& bm = bsd.block_modes[i]; + assert(bm.is_dual_plane); + + if (bm.quant_mode > max_weight_quant) + { + qwt_errors[i] = 1e38f; + continue; + } + + qwt_bitcounts[i] = static_cast<int8_t>(109 - bm.weight_bits); + + if (weight_high_value1[i] > 1.02f * min_wt_cutoff1) + { + weight_high_value1[i] = 1.0f; + } + + if (weight_high_value2[i] > 1.02f * min_wt_cutoff2) + { + weight_high_value2[i] = 1.0f; + } + + unsigned int decimation_mode = bm.decimation_mode; + const auto& di = bsd.get_decimation_info(decimation_mode); + + alignas(ASTCENC_VECALIGN) float dec_weights_uquantf[BLOCK_MAX_WEIGHTS]; + + // Generate the optimized set of weights for the mode + compute_quantized_weights_for_decimation( + di, + weight_low_value1[i], + weight_high_value1[i], + dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode, + dec_weights_uquantf, + dec_weights_uquant + BLOCK_MAX_WEIGHTS * i, + bm.get_weight_quant_mode()); + + compute_quantized_weights_for_decimation( + di, + weight_low_value2[i], + weight_high_value2[i], + dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode + WEIGHTS_PLANE2_OFFSET, + dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET, + dec_weights_uquant + BLOCK_MAX_WEIGHTS * i + WEIGHTS_PLANE2_OFFSET, + bm.get_weight_quant_mode()); + + // Compute weight quantization errors for the block mode + qwt_errors[i] = compute_error_of_weight_set_2planes( + ei1, + ei2, + di, + dec_weights_uquantf, + dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET); + } + + // Decide the optimal combination of color endpoint encodings and weight encodings + uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS]; + int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES]; + + quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES]; + quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES]; + + endpoints epm; + merge_endpoints(ei1.ep, ei2.ep, plane2_component, epm); + + const auto& pi = bsd.get_partition_info(1, 0); + unsigned int candidate_count = compute_ideal_endpoint_formats( + pi, blk, epm, qwt_bitcounts, qwt_errors, + config.tune_candidate_limit, + bsd.block_mode_count_1plane_selected, bsd.block_mode_count_1plane_2plane_selected, + partition_format_specifiers, block_mode_index, + color_quant_level, color_quant_level_mod, tmpbuf); + + // Iterate over the N believed-to-be-best modes to find out which one is actually best + float best_errorval_in_mode = ERROR_CALC_DEFAULT; + float best_errorval_in_scb = scb.errorval; + + for (unsigned int i = 0; i < candidate_count; i++) + { + TRACE_NODE(node0, "candidate"); + + const int bm_packed_index = block_mode_index[i]; + assert(bm_packed_index >= static_cast<int>(bsd.block_mode_count_1plane_selected) && + bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_2plane_selected)); + const block_mode& qw_bm = bsd.block_modes[bm_packed_index]; + + int decimation_mode = qw_bm.decimation_mode; + const auto& di = bsd.get_decimation_info(decimation_mode); + promise(di.weight_count > 0); + + trace_add_data("weight_x", di.weight_x); + trace_add_data("weight_y", di.weight_y); + trace_add_data("weight_z", di.weight_z); + trace_add_data("weight_quant", qw_bm.quant_mode); + + vfloat4 rgbs_color; + vfloat4 rgbo_color; + + symbolic_compressed_block workscb; + endpoints workep = epm; + + uint8_t* u8_weight1_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index; + uint8_t* u8_weight2_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index + WEIGHTS_PLANE2_OFFSET; + + for (int j = 0; j < di.weight_count; j++) + { + workscb.weights[j] = u8_weight1_src[j]; + workscb.weights[j + WEIGHTS_PLANE2_OFFSET] = u8_weight2_src[j]; + } + + for (unsigned int l = 0; l < config.tune_refinement_limit; l++) + { + recompute_ideal_colors_2planes( + blk, bsd, di, + workscb.weights, workscb.weights + WEIGHTS_PLANE2_OFFSET, + workep, rgbs_color, rgbo_color, plane2_component); + + // Quantize the chosen color + workscb.color_formats[0] = pack_color_endpoints( + workep.endpt0[0], + workep.endpt1[0], + rgbs_color, rgbo_color, + partition_format_specifiers[i][0], + workscb.color_values[0], + color_quant_level[i]); + + // Store header fields + workscb.partition_count = 1; + workscb.partition_index = 0; + workscb.quant_mode = color_quant_level[i]; + workscb.color_formats_matched = 0; + workscb.block_mode = qw_bm.mode_index; + workscb.plane2_component = static_cast<int8_t>(plane2_component); + workscb.block_type = SYM_BTYPE_NONCONST; + + // Pre-realign test + if (l == 0) + { + float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk); + if (errorval == -ERROR_CALC_DEFAULT) + { + errorval = -errorval; + workscb.block_type = SYM_BTYPE_ERROR; + } + + trace_add_data("error_prerealign", errorval); + best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); + + // Average refinement improvement is 3.5% per iteration (allow 5%), but the first + // iteration can help more so we give it a extra 10% leeway. Use this knowledge to + // drive a heuristic to skip blocks that are unlikely to catch up with the best + // block we have already. + unsigned int iters_remaining = config.tune_refinement_limit - l; + float threshold = (0.05f * static_cast<float>(iters_remaining)) + 1.1f; + if (errorval > (threshold * best_errorval_in_scb)) + { + break; + } + + if (errorval < best_errorval_in_scb) + { + best_errorval_in_scb = errorval; + workscb.errorval = errorval; + scb = workscb; + + if (errorval < tune_errorval_threshold) + { + // Skip remaining candidates - this is "good enough" + i = candidate_count; + break; + } + } + } + + // Perform a final pass over the weights to try to improve them. + bool adjustments; + if (di.weight_count != bsd.texel_count) + { + adjustments = realign_weights_decimated( + config.profile, bsd, blk, workscb); + } + else + { + adjustments = realign_weights_undecimated( + config.profile, bsd, blk, workscb); + } + + // Post-realign test + float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk); + if (errorval == -ERROR_CALC_DEFAULT) + { + errorval = -errorval; + workscb.block_type = SYM_BTYPE_ERROR; + } + + trace_add_data("error_postrealign", errorval); + best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); + + // Average refinement improvement is 3.5% per iteration, so skip blocks that are + // unlikely to catch up with the best block we have already. Assume a 5% per step to + // give benefit of the doubt ... + unsigned int iters_remaining = config.tune_refinement_limit - 1 - l; + float threshold = (0.05f * static_cast<float>(iters_remaining)) + 1.0f; + if (errorval > (threshold * best_errorval_in_scb)) + { + break; + } + + if (errorval < best_errorval_in_scb) + { + best_errorval_in_scb = errorval; + workscb.errorval = errorval; + scb = workscb; + + if (errorval < tune_errorval_threshold) + { + // Skip remaining candidates - this is "good enough" + i = candidate_count; + break; + } + } + + if (!adjustments) + { + break; + } + } + } + + return best_errorval_in_mode; +} + +/** + * @brief Determine the lowest cross-channel correlation factor. + * + * @param texels_per_block The number of texels in a block. + * @param blk The image block color data to compress. + * + * @return Return the lowest correlation factor. + */ +static float prepare_block_statistics( + int texels_per_block, + const image_block& blk +) { + // Compute covariance matrix, as a collection of 10 scalars that form the upper-triangular row + // of the matrix. The matrix is symmetric, so this is all we need for this use case. + float rs = 0.0f; + float gs = 0.0f; + float bs = 0.0f; + float as = 0.0f; + float rr_var = 0.0f; + float gg_var = 0.0f; + float bb_var = 0.0f; + float aa_var = 0.0f; + float rg_cov = 0.0f; + float rb_cov = 0.0f; + float ra_cov = 0.0f; + float gb_cov = 0.0f; + float ga_cov = 0.0f; + float ba_cov = 0.0f; + + float weight_sum = 0.0f; + + promise(texels_per_block > 0); + for (int i = 0; i < texels_per_block; i++) + { + float weight = hadd_s(blk.channel_weight) / 4.0f; + assert(weight >= 0.0f); + weight_sum += weight; + + float r = blk.data_r[i]; + float g = blk.data_g[i]; + float b = blk.data_b[i]; + float a = blk.data_a[i]; + + float rw = r * weight; + rs += rw; + rr_var += r * rw; + rg_cov += g * rw; + rb_cov += b * rw; + ra_cov += a * rw; + + float gw = g * weight; + gs += gw; + gg_var += g * gw; + gb_cov += b * gw; + ga_cov += a * gw; + + float bw = b * weight; + bs += bw; + bb_var += b * bw; + ba_cov += a * bw; + + float aw = a * weight; + as += aw; + aa_var += a * aw; + } + + float rpt = 1.0f / astc::max(weight_sum, 1e-7f); + + rr_var -= rs * (rs * rpt); + rg_cov -= gs * (rs * rpt); + rb_cov -= bs * (rs * rpt); + ra_cov -= as * (rs * rpt); + + gg_var -= gs * (gs * rpt); + gb_cov -= bs * (gs * rpt); + ga_cov -= as * (gs * rpt); + + bb_var -= bs * (bs * rpt); + ba_cov -= as * (bs * rpt); + + aa_var -= as * (as * rpt); + + // These will give a NaN if a channel is constant - these are fixed up in the next step + rg_cov *= astc::rsqrt(rr_var * gg_var); + rb_cov *= astc::rsqrt(rr_var * bb_var); + ra_cov *= astc::rsqrt(rr_var * aa_var); + gb_cov *= astc::rsqrt(gg_var * bb_var); + ga_cov *= astc::rsqrt(gg_var * aa_var); + ba_cov *= astc::rsqrt(bb_var * aa_var); + + if (astc::isnan(rg_cov)) rg_cov = 1.0f; + if (astc::isnan(rb_cov)) rb_cov = 1.0f; + if (astc::isnan(ra_cov)) ra_cov = 1.0f; + if (astc::isnan(gb_cov)) gb_cov = 1.0f; + if (astc::isnan(ga_cov)) ga_cov = 1.0f; + if (astc::isnan(ba_cov)) ba_cov = 1.0f; + + float lowest_correlation = astc::min(fabsf(rg_cov), fabsf(rb_cov)); + lowest_correlation = astc::min(lowest_correlation, fabsf(ra_cov)); + lowest_correlation = astc::min(lowest_correlation, fabsf(gb_cov)); + lowest_correlation = astc::min(lowest_correlation, fabsf(ga_cov)); + lowest_correlation = astc::min(lowest_correlation, fabsf(ba_cov)); + + // Diagnostic trace points + trace_add_data("min_r", blk.data_min.lane<0>()); + trace_add_data("max_r", blk.data_max.lane<0>()); + trace_add_data("min_g", blk.data_min.lane<1>()); + trace_add_data("max_g", blk.data_max.lane<1>()); + trace_add_data("min_b", blk.data_min.lane<2>()); + trace_add_data("max_b", blk.data_max.lane<2>()); + trace_add_data("min_a", blk.data_min.lane<3>()); + trace_add_data("max_a", blk.data_max.lane<3>()); + trace_add_data("cov_rg", fabsf(rg_cov)); + trace_add_data("cov_rb", fabsf(rb_cov)); + trace_add_data("cov_ra", fabsf(ra_cov)); + trace_add_data("cov_gb", fabsf(gb_cov)); + trace_add_data("cov_ga", fabsf(ga_cov)); + trace_add_data("cov_ba", fabsf(ba_cov)); + + return lowest_correlation; +} + +/* See header for documentation. */ +void compress_block( + const astcenc_contexti& ctx, + const image_block& blk, + physical_compressed_block& pcb, + compression_working_buffers& tmpbuf) +{ + astcenc_profile decode_mode = ctx.config.profile; + symbolic_compressed_block scb; + const block_size_descriptor& bsd = *ctx.bsd; + float lowest_correl; + + TRACE_NODE(node0, "block"); + trace_add_data("pos_x", blk.xpos); + trace_add_data("pos_y", blk.ypos); + trace_add_data("pos_z", blk.zpos); + + // Set stricter block targets for luminance data as we have more bits to play with + bool block_is_l = blk.is_luminance(); + float block_is_l_scale = block_is_l ? 1.0f / 1.5f : 1.0f; + + // Set slightly stricter block targets for lumalpha data as we have more bits to play with + bool block_is_la = blk.is_luminancealpha(); + float block_is_la_scale = block_is_la ? 1.0f / 1.05f : 1.0f; + + bool block_skip_two_plane = false; + int max_partitions = ctx.config.tune_partition_count_limit; + + unsigned int requested_partition_indices[3] { + ctx.config.tune_2partition_index_limit, + ctx.config.tune_3partition_index_limit, + ctx.config.tune_4partition_index_limit + }; + + unsigned int requested_partition_trials[3] { + ctx.config.tune_2partitioning_candidate_limit, + ctx.config.tune_3partitioning_candidate_limit, + ctx.config.tune_4partitioning_candidate_limit + }; + +#if defined(ASTCENC_DIAGNOSTICS) + // Do this early in diagnostic builds so we can dump uniform metrics + // for every block. Do it later in release builds to avoid redundant work! + float error_weight_sum = hadd_s(blk.channel_weight) * bsd.texel_count; + float error_threshold = ctx.config.tune_db_limit + * error_weight_sum + * block_is_l_scale + * block_is_la_scale; + + lowest_correl = prepare_block_statistics(bsd.texel_count, blk); + trace_add_data("lowest_correl", lowest_correl); + trace_add_data("tune_error_threshold", error_threshold); +#endif + + // Detected a constant-color block + if (all(blk.data_min == blk.data_max)) + { + TRACE_NODE(node1, "pass"); + trace_add_data("partition_count", 0); + trace_add_data("plane_count", 1); + + scb.partition_count = 0; + + // Encode as FP16 if using HDR + if ((decode_mode == ASTCENC_PRF_HDR) || + (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A)) + { + scb.block_type = SYM_BTYPE_CONST_F16; + vint4 color_f16 = float_to_float16(blk.origin_texel); + store(color_f16, scb.constant_color); + } + // Encode as UNORM16 if NOT using HDR + else + { + scb.block_type = SYM_BTYPE_CONST_U16; + vfloat4 color_f32 = clamp(0.0f, 1.0f, blk.origin_texel) * 65535.0f; + vint4 color_u16 = float_to_int_rtn(color_f32); + store(color_u16, scb.constant_color); + } + + trace_add_data("exit", "quality hit"); + + symbolic_to_physical(bsd, scb, pcb); + return; + } + +#if !defined(ASTCENC_DIAGNOSTICS) + float error_weight_sum = hadd_s(blk.channel_weight) * bsd.texel_count; + float error_threshold = ctx.config.tune_db_limit + * error_weight_sum + * block_is_l_scale + * block_is_la_scale; +#endif + + // Set SCB and mode errors to a very high error value + scb.errorval = ERROR_CALC_DEFAULT; + scb.block_type = SYM_BTYPE_ERROR; + + float best_errorvals_for_pcount[BLOCK_MAX_PARTITIONS] { + ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT + }; + + float exit_thresholds_for_pcount[BLOCK_MAX_PARTITIONS] { + 0.0f, + ctx.config.tune_2_partition_early_out_limit_factor, + ctx.config.tune_3_partition_early_out_limit_factor, + 0.0f + }; + + // Trial using 1 plane of weights and 1 partition. + + // Most of the time we test it twice, first with a mode cutoff of 0 and then with the specified + // mode cutoff. This causes an early-out that speeds up encoding of easy blocks. However, this + // optimization is disabled for 4x4 and 5x4 blocks where it nearly always slows down the + // compression and slightly reduces image quality. + + float errorval_mult[2] { + 1.0f / ctx.config.tune_mode0_mse_overshoot, + 1.0f + }; + + static const float errorval_overshoot = 1.0f / ctx.config.tune_refinement_mse_overshoot; + + // Only enable MODE0 fast path (trial 0) if 2D and more than 25 texels + int start_trial = 1; + if ((bsd.texel_count >= TUNE_MIN_TEXELS_MODE0_FASTPATH) && (bsd.zdim == 1)) + { + start_trial = 0; + } + + int quant_limit = QUANT_32; + for (int i = start_trial; i < 2; i++) + { + TRACE_NODE(node1, "pass"); + trace_add_data("partition_count", 1); + trace_add_data("plane_count", 1); + trace_add_data("search_mode", i); + + float errorval = compress_symbolic_block_for_partition_1plane( + ctx.config, bsd, blk, i == 0, + error_threshold * errorval_mult[i] * errorval_overshoot, + 1, 0, scb, tmpbuf, QUANT_32); + + // Record the quant level so we can use the filter later searches + const auto& bm = bsd.get_block_mode(scb.block_mode); + quant_limit = bm.get_weight_quant_mode(); + + best_errorvals_for_pcount[0] = astc::min(best_errorvals_for_pcount[0], errorval); + if (errorval < (error_threshold * errorval_mult[i])) + { + trace_add_data("exit", "quality hit"); + goto END_OF_TESTS; + } + } + +#if !defined(ASTCENC_DIAGNOSTICS) + lowest_correl = prepare_block_statistics(bsd.texel_count, blk); +#endif + + block_skip_two_plane = lowest_correl > ctx.config.tune_2_plane_early_out_limit_correlation; + + // Test the four possible 1-partition, 2-planes modes. Do this in reverse, as + // alpha is the most likely to be non-correlated if it is present in the data. + for (int i = BLOCK_MAX_COMPONENTS - 1; i >= 0; i--) + { + TRACE_NODE(node1, "pass"); + trace_add_data("partition_count", 1); + trace_add_data("plane_count", 2); + trace_add_data("plane_component", i); + + if (block_skip_two_plane) + { + trace_add_data("skip", "tune_2_plane_early_out_limit_correlation"); + continue; + } + + if (blk.grayscale && i != 3) + { + trace_add_data("skip", "grayscale block"); + continue; + } + + if (blk.is_constant_channel(i)) + { + trace_add_data("skip", "constant component"); + continue; + } + + float errorval = compress_symbolic_block_for_partition_2planes( + ctx.config, bsd, blk, error_threshold * errorval_overshoot, + i, scb, tmpbuf, quant_limit); + + // If attempting two planes is much worse than the best one plane result + // then further two plane searches are unlikely to help so move on ... + if (errorval > (best_errorvals_for_pcount[0] * 2.0f)) + { + break; + } + + if (errorval < error_threshold) + { + trace_add_data("exit", "quality hit"); + goto END_OF_TESTS; + } + } + + // Find best blocks for 2, 3 and 4 partitions + for (int partition_count = 2; partition_count <= max_partitions; partition_count++) + { + unsigned int partition_indices[TUNE_MAX_PARTITIIONING_CANDIDATES]; + + unsigned int requested_indices = requested_partition_indices[partition_count - 2]; + + unsigned int requested_trials = requested_partition_trials[partition_count - 2]; + requested_trials = astc::min(requested_trials, requested_indices); + + unsigned int actual_trials = find_best_partition_candidates( + bsd, blk, partition_count, requested_indices, partition_indices, requested_trials); + + float best_error_in_prev = best_errorvals_for_pcount[partition_count - 2]; + + for (unsigned int i = 0; i < actual_trials; i++) + { + TRACE_NODE(node1, "pass"); + trace_add_data("partition_count", partition_count); + trace_add_data("partition_index", partition_indices[i]); + trace_add_data("plane_count", 1); + trace_add_data("search_mode", i); + + float errorval = compress_symbolic_block_for_partition_1plane( + ctx.config, bsd, blk, false, + error_threshold * errorval_overshoot, + partition_count, partition_indices[i], + scb, tmpbuf, quant_limit); + + best_errorvals_for_pcount[partition_count - 1] = astc::min(best_errorvals_for_pcount[partition_count - 1], errorval); + + // If using N partitions doesn't improve much over using N-1 partitions then skip trying + // N+1. Error can dramatically improve if the data is correlated or non-correlated and + // aligns with a partitioning that suits that encoding, so for this inner loop check add + // a large error scale because the "other" trial could be a lot better. In total the + // error must be at least 2x worse than the best existing error to early-out. + float best_error = best_errorvals_for_pcount[partition_count - 1]; + float best_error_scale = exit_thresholds_for_pcount[partition_count - 1] * 2.0f; + if (best_error > (best_error_in_prev * best_error_scale)) + { + trace_add_data("skip", "tune_partition_early_out_limit_factor"); + goto END_OF_TESTS; + } + + if (errorval < error_threshold) + { + trace_add_data("exit", "quality hit"); + goto END_OF_TESTS; + } + } + + // If using N partitions doesn't improve much over using N-1 partitions then skip trying N+1 + float best_error = best_errorvals_for_pcount[partition_count - 1]; + float best_error_scale = exit_thresholds_for_pcount[partition_count - 1]; + if (best_error > (best_error_in_prev * best_error_scale)) + { + trace_add_data("skip", "tune_partition_early_out_limit_factor"); + goto END_OF_TESTS; + } + } + + trace_add_data("exit", "quality not hit"); + +END_OF_TESTS: + // If we still have an error block then convert to something we can encode + // TODO: Do something more sensible here, such as average color block + if (scb.block_type == SYM_BTYPE_ERROR) + { +#if defined(ASTCENC_DIAGNOSTICS) + static bool printed_once = false; + if (!printed_once) + { + printed_once = true; + printf("WARN: At least one block failed to find a valid encoding.\n" + " Try increasing compression quality settings.\n\n"); + } +#endif + + scb.block_type = SYM_BTYPE_CONST_U16; + vfloat4 color_f32 = clamp(0.0f, 1.0f, blk.origin_texel) * 65535.0f; + vint4 color_u16 = float_to_int_rtn(color_f32); + store(color_u16, scb.constant_color); + } + + // Compress to a physical block + symbolic_to_physical(bsd, scb, pcb); +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compute_variance.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compute_variance.cpp new file mode 100644 index 00000000000..48a4af8cef4 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_compute_variance.cpp @@ -0,0 +1,472 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions to calculate variance per component in a NxN footprint. + * + * We need N to be parametric, so the routine below uses summed area tables in order to execute in + * O(1) time independent of how big N is. + * + * The addition uses a Brent-Kung-based parallel prefix adder. This uses the prefix tree to first + * perform a binary reduction, and then distributes the results. This method means that there is no + * serial dependency between a given element and the next one, and also significantly improves + * numerical stability allowing us to use floats rather than doubles. + */ + +#include "astcenc_internal.h" + +#include <cassert> + +/** + * @brief Generate a prefix-sum array using the Brent-Kung algorithm. + * + * This will take an input array of the form: + * v0, v1, v2, ... + * ... and modify in-place to turn it into a prefix-sum array of the form: + * v0, v0+v1, v0+v1+v2, ... + * + * @param d The array to prefix-sum. + * @param items The number of items in the array. + * @param stride The item spacing in the array; i.e. dense arrays should use 1. + */ +static void brent_kung_prefix_sum( + vfloat4* d, + size_t items, + int stride +) { + if (items < 2) + return; + + size_t lc_stride = 2; + size_t log2_stride = 1; + + // The reduction-tree loop + do { + size_t step = lc_stride >> 1; + size_t start = lc_stride - 1; + size_t iters = items >> log2_stride; + + vfloat4 *da = d + (start * stride); + ptrdiff_t ofs = -static_cast<ptrdiff_t>(step * stride); + size_t ofs_stride = stride << log2_stride; + + while (iters) + { + *da = *da + da[ofs]; + da += ofs_stride; + iters--; + } + + log2_stride += 1; + lc_stride <<= 1; + } while (lc_stride <= items); + + // The expansion-tree loop + do { + log2_stride -= 1; + lc_stride >>= 1; + + size_t step = lc_stride >> 1; + size_t start = step + lc_stride - 1; + size_t iters = (items - step) >> log2_stride; + + vfloat4 *da = d + (start * stride); + ptrdiff_t ofs = -static_cast<ptrdiff_t>(step * stride); + size_t ofs_stride = stride << log2_stride; + + while (iters) + { + *da = *da + da[ofs]; + da += ofs_stride; + iters--; + } + } while (lc_stride > 2); +} + +/* See header for documentation. */ +void compute_pixel_region_variance( + astcenc_contexti& ctx, + const pixel_region_args& arg +) { + // Unpack the memory structure into local variables + const astcenc_image* img = arg.img; + astcenc_swizzle swz = arg.swz; + bool have_z = arg.have_z; + + int size_x = arg.size_x; + int size_y = arg.size_y; + int size_z = arg.size_z; + + int offset_x = arg.offset_x; + int offset_y = arg.offset_y; + int offset_z = arg.offset_z; + + int alpha_kernel_radius = arg.alpha_kernel_radius; + + float* input_alpha_averages = ctx.input_alpha_averages; + vfloat4* work_memory = arg.work_memory; + + // Compute memory sizes and dimensions that we need + int kernel_radius = alpha_kernel_radius; + int kerneldim = 2 * kernel_radius + 1; + int kernel_radius_xy = kernel_radius; + int kernel_radius_z = have_z ? kernel_radius : 0; + + int padsize_x = size_x + kerneldim; + int padsize_y = size_y + kerneldim; + int padsize_z = size_z + (have_z ? kerneldim : 0); + int sizeprod = padsize_x * padsize_y * padsize_z; + + int zd_start = have_z ? 1 : 0; + + vfloat4 *varbuf1 = work_memory; + vfloat4 *varbuf2 = work_memory + sizeprod; + + // Scaling factors to apply to Y and Z for accesses into the work buffers + int yst = padsize_x; + int zst = padsize_x * padsize_y; + + // Scaling factors to apply to Y and Z for accesses into result buffers + int ydt = img->dim_x; + int zdt = img->dim_x * img->dim_y; + + // Macros to act as accessor functions for the work-memory + #define VARBUF1(z, y, x) varbuf1[z * zst + y * yst + x] + #define VARBUF2(z, y, x) varbuf2[z * zst + y * yst + x] + + // Load N and N^2 values into the work buffers + if (img->data_type == ASTCENC_TYPE_U8) + { + // Swizzle data structure 4 = ZERO, 5 = ONE + uint8_t data[6]; + data[ASTCENC_SWZ_0] = 0; + data[ASTCENC_SWZ_1] = 255; + + for (int z = zd_start; z < padsize_z; z++) + { + int z_src = (z - zd_start) + offset_z - kernel_radius_z; + z_src = astc::clamp(z_src, 0, static_cast<int>(img->dim_z - 1)); + uint8_t* data8 = static_cast<uint8_t*>(img->data[z_src]); + + for (int y = 1; y < padsize_y; y++) + { + int y_src = (y - 1) + offset_y - kernel_radius_xy; + y_src = astc::clamp(y_src, 0, static_cast<int>(img->dim_y - 1)); + + for (int x = 1; x < padsize_x; x++) + { + int x_src = (x - 1) + offset_x - kernel_radius_xy; + x_src = astc::clamp(x_src, 0, static_cast<int>(img->dim_x - 1)); + + data[0] = data8[(4 * img->dim_x * y_src) + (4 * x_src )]; + data[1] = data8[(4 * img->dim_x * y_src) + (4 * x_src + 1)]; + data[2] = data8[(4 * img->dim_x * y_src) + (4 * x_src + 2)]; + data[3] = data8[(4 * img->dim_x * y_src) + (4 * x_src + 3)]; + + uint8_t r = data[swz.r]; + uint8_t g = data[swz.g]; + uint8_t b = data[swz.b]; + uint8_t a = data[swz.a]; + + vfloat4 d = vfloat4 (r * (1.0f / 255.0f), + g * (1.0f / 255.0f), + b * (1.0f / 255.0f), + a * (1.0f / 255.0f)); + + VARBUF1(z, y, x) = d; + VARBUF2(z, y, x) = d * d; + } + } + } + } + else if (img->data_type == ASTCENC_TYPE_F16) + { + // Swizzle data structure 4 = ZERO, 5 = ONE (in FP16) + uint16_t data[6]; + data[ASTCENC_SWZ_0] = 0; + data[ASTCENC_SWZ_1] = 0x3C00; + + for (int z = zd_start; z < padsize_z; z++) + { + int z_src = (z - zd_start) + offset_z - kernel_radius_z; + z_src = astc::clamp(z_src, 0, static_cast<int>(img->dim_z - 1)); + uint16_t* data16 = static_cast<uint16_t*>(img->data[z_src]); + + for (int y = 1; y < padsize_y; y++) + { + int y_src = (y - 1) + offset_y - kernel_radius_xy; + y_src = astc::clamp(y_src, 0, static_cast<int>(img->dim_y - 1)); + + for (int x = 1; x < padsize_x; x++) + { + int x_src = (x - 1) + offset_x - kernel_radius_xy; + x_src = astc::clamp(x_src, 0, static_cast<int>(img->dim_x - 1)); + + data[0] = data16[(4 * img->dim_x * y_src) + (4 * x_src )]; + data[1] = data16[(4 * img->dim_x * y_src) + (4 * x_src + 1)]; + data[2] = data16[(4 * img->dim_x * y_src) + (4 * x_src + 2)]; + data[3] = data16[(4 * img->dim_x * y_src) + (4 * x_src + 3)]; + + vint4 di(data[swz.r], data[swz.g], data[swz.b], data[swz.a]); + vfloat4 d = float16_to_float(di); + + VARBUF1(z, y, x) = d; + VARBUF2(z, y, x) = d * d; + } + } + } + } + else // if (img->data_type == ASTCENC_TYPE_F32) + { + assert(img->data_type == ASTCENC_TYPE_F32); + + // Swizzle data structure 4 = ZERO, 5 = ONE (in FP16) + float data[6]; + data[ASTCENC_SWZ_0] = 0.0f; + data[ASTCENC_SWZ_1] = 1.0f; + + for (int z = zd_start; z < padsize_z; z++) + { + int z_src = (z - zd_start) + offset_z - kernel_radius_z; + z_src = astc::clamp(z_src, 0, static_cast<int>(img->dim_z - 1)); + float* data32 = static_cast<float*>(img->data[z_src]); + + for (int y = 1; y < padsize_y; y++) + { + int y_src = (y - 1) + offset_y - kernel_radius_xy; + y_src = astc::clamp(y_src, 0, static_cast<int>(img->dim_y - 1)); + + for (int x = 1; x < padsize_x; x++) + { + int x_src = (x - 1) + offset_x - kernel_radius_xy; + x_src = astc::clamp(x_src, 0, static_cast<int>(img->dim_x - 1)); + + data[0] = data32[(4 * img->dim_x * y_src) + (4 * x_src )]; + data[1] = data32[(4 * img->dim_x * y_src) + (4 * x_src + 1)]; + data[2] = data32[(4 * img->dim_x * y_src) + (4 * x_src + 2)]; + data[3] = data32[(4 * img->dim_x * y_src) + (4 * x_src + 3)]; + + float r = data[swz.r]; + float g = data[swz.g]; + float b = data[swz.b]; + float a = data[swz.a]; + + vfloat4 d(r, g, b, a); + + VARBUF1(z, y, x) = d; + VARBUF2(z, y, x) = d * d; + } + } + } + } + + // Pad with an extra layer of 0s; this forms the edge of the SAT tables + vfloat4 vbz = vfloat4::zero(); + for (int z = 0; z < padsize_z; z++) + { + for (int y = 0; y < padsize_y; y++) + { + VARBUF1(z, y, 0) = vbz; + VARBUF2(z, y, 0) = vbz; + } + + for (int x = 0; x < padsize_x; x++) + { + VARBUF1(z, 0, x) = vbz; + VARBUF2(z, 0, x) = vbz; + } + } + + if (have_z) + { + for (int y = 0; y < padsize_y; y++) + { + for (int x = 0; x < padsize_x; x++) + { + VARBUF1(0, y, x) = vbz; + VARBUF2(0, y, x) = vbz; + } + } + } + + // Generate summed-area tables for N and N^2; this is done in-place, using + // a Brent-Kung parallel-prefix based algorithm to minimize precision loss + for (int z = zd_start; z < padsize_z; z++) + { + for (int y = 1; y < padsize_y; y++) + { + brent_kung_prefix_sum(&(VARBUF1(z, y, 1)), padsize_x - 1, 1); + brent_kung_prefix_sum(&(VARBUF2(z, y, 1)), padsize_x - 1, 1); + } + } + + for (int z = zd_start; z < padsize_z; z++) + { + for (int x = 1; x < padsize_x; x++) + { + brent_kung_prefix_sum(&(VARBUF1(z, 1, x)), padsize_y - 1, yst); + brent_kung_prefix_sum(&(VARBUF2(z, 1, x)), padsize_y - 1, yst); + } + } + + if (have_z) + { + for (int y = 1; y < padsize_y; y++) + { + for (int x = 1; x < padsize_x; x++) + { + brent_kung_prefix_sum(&(VARBUF1(1, y, x)), padsize_z - 1, zst); + brent_kung_prefix_sum(&(VARBUF2(1, y, x)), padsize_z - 1, zst); + } + } + } + + // Compute a few constants used in the variance-calculation. + float alpha_kdim = static_cast<float>(2 * alpha_kernel_radius + 1); + float alpha_rsamples; + + if (have_z) + { + alpha_rsamples = 1.0f / (alpha_kdim * alpha_kdim * alpha_kdim); + } + else + { + alpha_rsamples = 1.0f / (alpha_kdim * alpha_kdim); + } + + // Use the summed-area tables to compute variance for each neighborhood + if (have_z) + { + for (int z = 0; z < size_z; z++) + { + int z_src = z + kernel_radius_z; + int z_dst = z + offset_z; + int z_low = z_src - alpha_kernel_radius; + int z_high = z_src + alpha_kernel_radius + 1; + + for (int y = 0; y < size_y; y++) + { + int y_src = y + kernel_radius_xy; + int y_dst = y + offset_y; + int y_low = y_src - alpha_kernel_radius; + int y_high = y_src + alpha_kernel_radius + 1; + + for (int x = 0; x < size_x; x++) + { + int x_src = x + kernel_radius_xy; + int x_dst = x + offset_x; + int x_low = x_src - alpha_kernel_radius; + int x_high = x_src + alpha_kernel_radius + 1; + + // Summed-area table lookups for alpha average + float vasum = ( VARBUF1(z_high, y_low, x_low).lane<3>() + - VARBUF1(z_high, y_low, x_high).lane<3>() + - VARBUF1(z_high, y_high, x_low).lane<3>() + + VARBUF1(z_high, y_high, x_high).lane<3>()) - + ( VARBUF1(z_low, y_low, x_low).lane<3>() + - VARBUF1(z_low, y_low, x_high).lane<3>() + - VARBUF1(z_low, y_high, x_low).lane<3>() + + VARBUF1(z_low, y_high, x_high).lane<3>()); + + int out_index = z_dst * zdt + y_dst * ydt + x_dst; + input_alpha_averages[out_index] = (vasum * alpha_rsamples); + } + } + } + } + else + { + for (int y = 0; y < size_y; y++) + { + int y_src = y + kernel_radius_xy; + int y_dst = y + offset_y; + int y_low = y_src - alpha_kernel_radius; + int y_high = y_src + alpha_kernel_radius + 1; + + for (int x = 0; x < size_x; x++) + { + int x_src = x + kernel_radius_xy; + int x_dst = x + offset_x; + int x_low = x_src - alpha_kernel_radius; + int x_high = x_src + alpha_kernel_radius + 1; + + // Summed-area table lookups for alpha average + float vasum = VARBUF1(0, y_low, x_low).lane<3>() + - VARBUF1(0, y_low, x_high).lane<3>() + - VARBUF1(0, y_high, x_low).lane<3>() + + VARBUF1(0, y_high, x_high).lane<3>(); + + int out_index = y_dst * ydt + x_dst; + input_alpha_averages[out_index] = (vasum * alpha_rsamples); + } + } + } +} + +/* See header for documentation. */ +unsigned int init_compute_averages( + const astcenc_image& img, + unsigned int alpha_kernel_radius, + const astcenc_swizzle& swz, + avg_args& ag +) { + unsigned int size_x = img.dim_x; + unsigned int size_y = img.dim_y; + unsigned int size_z = img.dim_z; + + // Compute maximum block size and from that the working memory buffer size + unsigned int kernel_radius = alpha_kernel_radius; + unsigned int kerneldim = 2 * kernel_radius + 1; + + bool have_z = (size_z > 1); + unsigned int max_blk_size_xy = have_z ? 16 : 32; + unsigned int max_blk_size_z = astc::min(size_z, have_z ? 16u : 1u); + + unsigned int max_padsize_xy = max_blk_size_xy + kerneldim; + unsigned int max_padsize_z = max_blk_size_z + (have_z ? kerneldim : 0); + + // Perform block-wise averages calculations across the image + // Initialize fields which are not populated until later + ag.arg.size_x = 0; + ag.arg.size_y = 0; + ag.arg.size_z = 0; + ag.arg.offset_x = 0; + ag.arg.offset_y = 0; + ag.arg.offset_z = 0; + ag.arg.work_memory = nullptr; + + ag.arg.img = &img; + ag.arg.swz = swz; + ag.arg.have_z = have_z; + ag.arg.alpha_kernel_radius = alpha_kernel_radius; + + ag.img_size_x = size_x; + ag.img_size_y = size_y; + ag.img_size_z = size_z; + ag.blk_size_xy = max_blk_size_xy; + ag.blk_size_z = max_blk_size_z; + ag.work_memory_size = 2 * max_padsize_xy * max_padsize_xy * max_padsize_z; + + // The parallel task count + unsigned int z_tasks = (size_z + max_blk_size_z - 1) / max_blk_size_z; + unsigned int y_tasks = (size_y + max_blk_size_xy - 1) / max_blk_size_xy; + return z_tasks * y_tasks; +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_decompress_symbolic.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_decompress_symbolic.cpp new file mode 100644 index 00000000000..8be222b7b3f --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_decompress_symbolic.cpp @@ -0,0 +1,623 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions to decompress a symbolic block. + */ + +#include "astcenc_internal.h" + +#include <stdio.h> +#include <assert.h> + +/** + * @brief Compute the integer linear interpolation of two color endpoints. + * + * @param decode_mode The ASTC profile (linear or sRGB) + * @param color0 The endpoint0 color. + * @param color1 The endpoint1 color. + * @param weights The interpolation weight (between 0 and 64). + * + * @return The interpolated color. + */ +static vint4 lerp_color_int( + astcenc_profile decode_mode, + vint4 color0, + vint4 color1, + vint4 weights +) { + vint4 weight1 = weights; + vint4 weight0 = vint4(64) - weight1; + + if (decode_mode == ASTCENC_PRF_LDR_SRGB) + { + color0 = asr<8>(color0); + color1 = asr<8>(color1); + } + + vint4 color = (color0 * weight0) + (color1 * weight1) + vint4(32); + color = asr<6>(color); + + if (decode_mode == ASTCENC_PRF_LDR_SRGB) + { + color = color * vint4(257); + } + + return color; +} + + +/** + * @brief Convert integer color value into a float value for the decoder. + * + * @param data The integer color value post-interpolation. + * @param lns_mask If set treat lane as HDR (LNS) else LDR (unorm16). + * + * @return The float color value. + */ +static inline vfloat4 decode_texel( + vint4 data, + vmask4 lns_mask +) { + vint4 color_lns = vint4::zero(); + vint4 color_unorm = vint4::zero(); + + if (any(lns_mask)) + { + color_lns = lns_to_sf16(data); + } + + if (!all(lns_mask)) + { + color_unorm = unorm16_to_sf16(data); + } + + // Pick components and then convert to FP16 + vint4 datai = select(color_unorm, color_lns, lns_mask); + return float16_to_float(datai); +} + +/* See header for documentation. */ +void unpack_weights( + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const decimation_info& di, + bool is_dual_plane, + int weights_plane1[BLOCK_MAX_TEXELS], + int weights_plane2[BLOCK_MAX_TEXELS] +) { + // Safe to overshoot as all arrays are allocated to full size + if (!is_dual_plane) + { + // Build full 64-entry weight lookup table + vint4 tab0(reinterpret_cast<const int*>(scb.weights + 0)); + vint4 tab1(reinterpret_cast<const int*>(scb.weights + 16)); + vint4 tab2(reinterpret_cast<const int*>(scb.weights + 32)); + vint4 tab3(reinterpret_cast<const int*>(scb.weights + 48)); + + vint tab0p, tab1p, tab2p, tab3p; + vtable_prepare(tab0, tab1, tab2, tab3, tab0p, tab1p, tab2p, tab3p); + + for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint summed_value(8); + vint weight_count(di.texel_weight_count + i); + int max_weight_count = hmax(weight_count).lane<0>(); + + promise(max_weight_count > 0); + for (int j = 0; j < max_weight_count; j++) + { + vint texel_weights(di.texel_weights_4t[j] + i); + vint texel_weights_int(di.texel_weights_int_4t[j] + i); + + summed_value += vtable_8bt_32bi(tab0p, tab1p, tab2p, tab3p, texel_weights) * texel_weights_int; + } + + store(lsr<4>(summed_value), weights_plane1 + i); + } + } + else + { + // Build a 32-entry weight lookup table per plane + // Plane 1 + vint4 tab0_plane1(reinterpret_cast<const int*>(scb.weights + 0)); + vint4 tab1_plane1(reinterpret_cast<const int*>(scb.weights + 16)); + vint tab0_plane1p, tab1_plane1p; + vtable_prepare(tab0_plane1, tab1_plane1, tab0_plane1p, tab1_plane1p); + + // Plane 2 + vint4 tab0_plane2(reinterpret_cast<const int*>(scb.weights + 32)); + vint4 tab1_plane2(reinterpret_cast<const int*>(scb.weights + 48)); + vint tab0_plane2p, tab1_plane2p; + vtable_prepare(tab0_plane2, tab1_plane2, tab0_plane2p, tab1_plane2p); + + for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint sum_plane1(8); + vint sum_plane2(8); + + vint weight_count(di.texel_weight_count + i); + int max_weight_count = hmax(weight_count).lane<0>(); + + promise(max_weight_count > 0); + for (int j = 0; j < max_weight_count; j++) + { + vint texel_weights(di.texel_weights_4t[j] + i); + vint texel_weights_int(di.texel_weights_int_4t[j] + i); + + sum_plane1 += vtable_8bt_32bi(tab0_plane1p, tab1_plane1p, texel_weights) * texel_weights_int; + sum_plane2 += vtable_8bt_32bi(tab0_plane2p, tab1_plane2p, texel_weights) * texel_weights_int; + } + + store(lsr<4>(sum_plane1), weights_plane1 + i); + store(lsr<4>(sum_plane2), weights_plane2 + i); + } + } +} + +/** + * @brief Return an FP32 NaN value for use in error colors. + * + * This NaN encoding will turn into 0xFFFF when converted to an FP16 NaN. + * + * @return The float color value. + */ +static float error_color_nan() +{ + if32 v; + v.u = 0xFFFFE000U; + return v.f; +} + +/* See header for documentation. */ +void decompress_symbolic_block( + astcenc_profile decode_mode, + const block_size_descriptor& bsd, + int xpos, + int ypos, + int zpos, + const symbolic_compressed_block& scb, + image_block& blk +) { + blk.xpos = xpos; + blk.ypos = ypos; + blk.zpos = zpos; + + blk.data_min = vfloat4::zero(); + blk.data_mean = vfloat4::zero(); + blk.data_max = vfloat4::zero(); + blk.grayscale = false; + + // If we detected an error-block, blow up immediately. + if (scb.block_type == SYM_BTYPE_ERROR) + { + for (unsigned int i = 0; i < bsd.texel_count; i++) + { + blk.data_r[i] = error_color_nan(); + blk.data_g[i] = error_color_nan(); + blk.data_b[i] = error_color_nan(); + blk.data_a[i] = error_color_nan(); + blk.rgb_lns[i] = 0; + blk.alpha_lns[i] = 0; + } + + return; + } + + if ((scb.block_type == SYM_BTYPE_CONST_F16) || + (scb.block_type == SYM_BTYPE_CONST_U16)) + { + vfloat4 color; + uint8_t use_lns = 0; + + // UNORM16 constant color block + if (scb.block_type == SYM_BTYPE_CONST_U16) + { + vint4 colori(scb.constant_color); + + // For sRGB decoding a real decoder would just use the top 8 bits for color conversion. + // We don't color convert, so rescale the top 8 bits into the full 16 bit dynamic range. + if (decode_mode == ASTCENC_PRF_LDR_SRGB) + { + colori = asr<8>(colori) * 257; + } + + vint4 colorf16 = unorm16_to_sf16(colori); + color = float16_to_float(colorf16); + } + // FLOAT16 constant color block + else + { + switch (decode_mode) + { + case ASTCENC_PRF_LDR_SRGB: + case ASTCENC_PRF_LDR: + color = vfloat4(error_color_nan()); + break; + case ASTCENC_PRF_HDR_RGB_LDR_A: + case ASTCENC_PRF_HDR: + // Constant-color block; unpack from FP16 to FP32. + color = float16_to_float(vint4(scb.constant_color)); + use_lns = 1; + break; + } + } + + for (unsigned int i = 0; i < bsd.texel_count; i++) + { + blk.data_r[i] = color.lane<0>(); + blk.data_g[i] = color.lane<1>(); + blk.data_b[i] = color.lane<2>(); + blk.data_a[i] = color.lane<3>(); + blk.rgb_lns[i] = use_lns; + blk.alpha_lns[i] = use_lns; + } + + return; + } + + // Get the appropriate partition-table entry + int partition_count = scb.partition_count; + const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); + + // Get the appropriate block descriptors + const auto& bm = bsd.get_block_mode(scb.block_mode); + const auto& di = bsd.get_decimation_info(bm.decimation_mode); + + bool is_dual_plane = static_cast<bool>(bm.is_dual_plane); + + // Unquantize and undecimate the weights + int plane1_weights[BLOCK_MAX_TEXELS]; + int plane2_weights[BLOCK_MAX_TEXELS]; + unpack_weights(bsd, scb, di, is_dual_plane, plane1_weights, plane2_weights); + + // Now that we have endpoint colors and weights, we can unpack texel colors + int plane2_component = is_dual_plane ? scb.plane2_component : -1; + vmask4 plane2_mask = vint4::lane_id() == vint4(plane2_component); + + for (int i = 0; i < partition_count; i++) + { + // Decode the color endpoints for this partition + vint4 ep0; + vint4 ep1; + bool rgb_lns; + bool a_lns; + + unpack_color_endpoints(decode_mode, + scb.color_formats[i], + scb.color_values[i], + rgb_lns, a_lns, + ep0, ep1); + + vmask4 lns_mask(rgb_lns, rgb_lns, rgb_lns, a_lns); + + int texel_count = pi.partition_texel_count[i]; + for (int j = 0; j < texel_count; j++) + { + int tix = pi.texels_of_partition[i][j]; + vint4 weight = select(vint4(plane1_weights[tix]), vint4(plane2_weights[tix]), plane2_mask); + vint4 color = lerp_color_int(decode_mode, ep0, ep1, weight); + vfloat4 colorf = decode_texel(color, lns_mask); + + blk.data_r[tix] = colorf.lane<0>(); + blk.data_g[tix] = colorf.lane<1>(); + blk.data_b[tix] = colorf.lane<2>(); + blk.data_a[tix] = colorf.lane<3>(); + } + } +} + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/* See header for documentation. */ +float compute_symbolic_block_difference_2plane( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk +) { + // If we detected an error-block, blow up immediately. + if (scb.block_type == SYM_BTYPE_ERROR) + { + return ERROR_CALC_DEFAULT; + } + + assert(scb.block_mode >= 0); + assert(scb.partition_count == 1); + assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 1); + + // Get the appropriate block descriptor + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); + + // Unquantize and undecimate the weights + int plane1_weights[BLOCK_MAX_TEXELS]; + int plane2_weights[BLOCK_MAX_TEXELS]; + unpack_weights(bsd, scb, di, true, plane1_weights, plane2_weights); + + vmask4 plane2_mask = vint4::lane_id() == vint4(scb.plane2_component); + + vfloat4 summa = vfloat4::zero(); + + // Decode the color endpoints for this partition + vint4 ep0; + vint4 ep1; + bool rgb_lns; + bool a_lns; + + unpack_color_endpoints(config.profile, + scb.color_formats[0], + scb.color_values[0], + rgb_lns, a_lns, + ep0, ep1); + + // Unpack and compute error for each texel in the partition + unsigned int texel_count = bsd.texel_count; + for (unsigned int i = 0; i < texel_count; i++) + { + vint4 weight = select(vint4(plane1_weights[i]), vint4(plane2_weights[i]), plane2_mask); + vint4 colori = lerp_color_int(config.profile, ep0, ep1, weight); + + vfloat4 color = int_to_float(colori); + vfloat4 oldColor = blk.texel(i); + + // Compare error using a perceptual decode metric for RGBM textures + if (config.flags & ASTCENC_FLG_MAP_RGBM) + { + // Fail encodings that result in zero weight M pixels. Note that this can cause + // "interesting" artifacts if we reject all useful encodings - we typically get max + // brightness encodings instead which look just as bad. We recommend users apply a + // bias to their stored M value, limiting the lower value to 16 or 32 to avoid + // getting small M values post-quantization, but we can't prove it would never + // happen, especially at low bit rates ... + if (color.lane<3>() == 0.0f) + { + return -ERROR_CALC_DEFAULT; + } + + // Compute error based on decoded RGBM color + color = vfloat4( + color.lane<0>() * color.lane<3>() * config.rgbm_m_scale, + color.lane<1>() * color.lane<3>() * config.rgbm_m_scale, + color.lane<2>() * color.lane<3>() * config.rgbm_m_scale, + 1.0f + ); + + oldColor = vfloat4( + oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale, + oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale, + oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale, + 1.0f + ); + } + + vfloat4 error = oldColor - color; + error = min(abs(error), 1e15f); + error = error * error; + + summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT); + } + + return summa.lane<0>(); +} + +/* See header for documentation. */ +float compute_symbolic_block_difference_1plane( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk +) { + assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 0); + + // If we detected an error-block, blow up immediately. + if (scb.block_type == SYM_BTYPE_ERROR) + { + return ERROR_CALC_DEFAULT; + } + + assert(scb.block_mode >= 0); + + // Get the appropriate partition-table entry + unsigned int partition_count = scb.partition_count; + const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); + + // Get the appropriate block descriptor + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); + + // Unquantize and undecimate the weights + int plane1_weights[BLOCK_MAX_TEXELS]; + unpack_weights(bsd, scb, di, false, plane1_weights, nullptr); + + vfloat4 summa = vfloat4::zero(); + for (unsigned int i = 0; i < partition_count; i++) + { + // Decode the color endpoints for this partition + vint4 ep0; + vint4 ep1; + bool rgb_lns; + bool a_lns; + + unpack_color_endpoints(config.profile, + scb.color_formats[i], + scb.color_values[i], + rgb_lns, a_lns, + ep0, ep1); + + // Unpack and compute error for each texel in the partition + unsigned int texel_count = pi.partition_texel_count[i]; + for (unsigned int j = 0; j < texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + vint4 colori = lerp_color_int(config.profile, ep0, ep1, + vint4(plane1_weights[tix])); + + vfloat4 color = int_to_float(colori); + vfloat4 oldColor = blk.texel(tix); + + // Compare error using a perceptual decode metric for RGBM textures + if (config.flags & ASTCENC_FLG_MAP_RGBM) + { + // Fail encodings that result in zero weight M pixels. Note that this can cause + // "interesting" artifacts if we reject all useful encodings - we typically get max + // brightness encodings instead which look just as bad. We recommend users apply a + // bias to their stored M value, limiting the lower value to 16 or 32 to avoid + // getting small M values post-quantization, but we can't prove it would never + // happen, especially at low bit rates ... + if (color.lane<3>() == 0.0f) + { + return -ERROR_CALC_DEFAULT; + } + + // Compute error based on decoded RGBM color + color = vfloat4( + color.lane<0>() * color.lane<3>() * config.rgbm_m_scale, + color.lane<1>() * color.lane<3>() * config.rgbm_m_scale, + color.lane<2>() * color.lane<3>() * config.rgbm_m_scale, + 1.0f + ); + + oldColor = vfloat4( + oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale, + oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale, + oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale, + 1.0f + ); + } + + vfloat4 error = oldColor - color; + error = min(abs(error), 1e15f); + error = error * error; + + summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT); + } + } + + return summa.lane<0>(); +} + +/* See header for documentation. */ +float compute_symbolic_block_difference_1plane_1partition( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk +) { + // If we detected an error-block, blow up immediately. + if (scb.block_type == SYM_BTYPE_ERROR) + { + return ERROR_CALC_DEFAULT; + } + + assert(scb.block_mode >= 0); + assert(bsd.get_partition_info(scb.partition_count, scb.partition_index).partition_count == 1); + + // Get the appropriate block descriptor + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); + + // Unquantize and undecimate the weights + alignas(ASTCENC_VECALIGN) int plane1_weights[BLOCK_MAX_TEXELS]; + unpack_weights(bsd, scb, di, false, plane1_weights, nullptr); + + // Decode the color endpoints for this partition + vint4 ep0; + vint4 ep1; + bool rgb_lns; + bool a_lns; + + unpack_color_endpoints(config.profile, + scb.color_formats[0], + scb.color_values[0], + rgb_lns, a_lns, + ep0, ep1); + + + // Pre-shift sRGB so things round correctly + if (config.profile == ASTCENC_PRF_LDR_SRGB) + { + ep0 = asr<8>(ep0); + ep1 = asr<8>(ep1); + } + + // Unpack and compute error for each texel in the partition + vfloatacc summav = vfloatacc::zero(); + + vint lane_id = vint::lane_id(); + vint srgb_scale(config.profile == ASTCENC_PRF_LDR_SRGB ? 257 : 1); + + unsigned int texel_count = bsd.texel_count; + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Compute EP1 contribution + vint weight1 = vint::loada(plane1_weights + i); + vint ep1_r = vint(ep1.lane<0>()) * weight1; + vint ep1_g = vint(ep1.lane<1>()) * weight1; + vint ep1_b = vint(ep1.lane<2>()) * weight1; + vint ep1_a = vint(ep1.lane<3>()) * weight1; + + // Compute EP0 contribution + vint weight0 = vint(64) - weight1; + vint ep0_r = vint(ep0.lane<0>()) * weight0; + vint ep0_g = vint(ep0.lane<1>()) * weight0; + vint ep0_b = vint(ep0.lane<2>()) * weight0; + vint ep0_a = vint(ep0.lane<3>()) * weight0; + + // Shift so things round correctly + vint colori_r = asr<6>(ep0_r + ep1_r + vint(32)) * srgb_scale; + vint colori_g = asr<6>(ep0_g + ep1_g + vint(32)) * srgb_scale; + vint colori_b = asr<6>(ep0_b + ep1_b + vint(32)) * srgb_scale; + vint colori_a = asr<6>(ep0_a + ep1_a + vint(32)) * srgb_scale; + + // Compute color diff + vfloat color_r = int_to_float(colori_r); + vfloat color_g = int_to_float(colori_g); + vfloat color_b = int_to_float(colori_b); + vfloat color_a = int_to_float(colori_a); + + vfloat color_orig_r = loada(blk.data_r + i); + vfloat color_orig_g = loada(blk.data_g + i); + vfloat color_orig_b = loada(blk.data_b + i); + vfloat color_orig_a = loada(blk.data_a + i); + + vfloat color_error_r = min(abs(color_orig_r - color_r), vfloat(1e15f)); + vfloat color_error_g = min(abs(color_orig_g - color_g), vfloat(1e15f)); + vfloat color_error_b = min(abs(color_orig_b - color_b), vfloat(1e15f)); + vfloat color_error_a = min(abs(color_orig_a - color_a), vfloat(1e15f)); + + // Compute squared error metric + color_error_r = color_error_r * color_error_r; + color_error_g = color_error_g * color_error_g; + color_error_b = color_error_b * color_error_b; + color_error_a = color_error_a * color_error_a; + + vfloat metric = color_error_r * blk.channel_weight.lane<0>() + + color_error_g * blk.channel_weight.lane<1>() + + color_error_b * blk.channel_weight.lane<2>() + + color_error_a * blk.channel_weight.lane<3>(); + + // Mask off bad lanes + vmask mask = lane_id < vint(texel_count); + lane_id += vint(ASTCENC_SIMD_WIDTH); + haccumulate(summav, metric, mask); + } + + return hadd_s(summav); +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.cpp new file mode 100644 index 00000000000..7fa7ab1a8b1 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.cpp @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2021-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for the library entrypoint. + */ + +#if defined(ASTCENC_DIAGNOSTICS) + +#include <cassert> +#include <cstdarg> +#include <cstdio> +#include <string> + +#include "astcenc_diagnostic_trace.h" + +/** @brief The global trace logger. */ +static TraceLog* g_TraceLog = nullptr; + +/** @brief The JSON indentation level. */ +static const size_t g_trace_indent = 2; + +TraceLog::TraceLog( + const char* file_name): + m_file(file_name, std::ofstream::out | std::ofstream::binary) +{ + assert(!g_TraceLog); + g_TraceLog = this; + m_root = new TraceNode("root"); +} + +/* See header for documentation. */ +TraceNode* TraceLog::get_current_leaf() +{ + if (m_stack.size()) + { + return m_stack.back(); + } + + return nullptr; +} + +/* See header for documentation. */ +size_t TraceLog::get_depth() +{ + return m_stack.size(); +} + +/* See header for documentation. */ +TraceLog::~TraceLog() +{ + assert(g_TraceLog == this); + delete m_root; + g_TraceLog = nullptr; +} + +/* See header for documentation. */ +TraceNode::TraceNode( + const char* format, + ... +) { + // Format the name string + constexpr size_t bufsz = 256; + char buffer[bufsz]; + + va_list args; + va_start (args, format); + vsnprintf (buffer, bufsz, format, args); + va_end (args); + + // Guarantee there is a nul terminator + buffer[bufsz - 1] = 0; + + // Generate the node + TraceNode* parent = g_TraceLog->get_current_leaf(); + size_t depth = g_TraceLog->get_depth(); + g_TraceLog->m_stack.push_back(this); + + bool comma = parent && parent->m_attrib_count; + auto& out = g_TraceLog->m_file; + + if (parent) + { + parent->m_attrib_count++; + } + + if (comma) + { + out << ','; + } + + if (depth) + { + out << '\n'; + } + + size_t out_indent = (depth * 2) * g_trace_indent; + size_t in_indent = (depth * 2 + 1) * g_trace_indent; + + std::string out_indents(""); + if (out_indent) + { + out_indents = std::string(out_indent, ' '); + } + + std::string in_indents(in_indent, ' '); + + out << out_indents << "[ \"node\", \"" << buffer << "\",\n"; + out << in_indents << "["; +} + +/* See header for documentation. */ +void TraceNode::add_attrib( + std::string type, + std::string key, + std::string value +) { + (void)type; + + size_t depth = g_TraceLog->get_depth(); + size_t indent = (depth * 2) * g_trace_indent; + auto& out = g_TraceLog->m_file; + bool comma = m_attrib_count; + m_attrib_count++; + + if (comma) + { + out << ','; + } + + out << '\n'; + out << std::string(indent, ' ') << "[ " + << "\"" << key << "\", " + << value << " ]"; +} + +/* See header for documentation. */ +TraceNode::~TraceNode() +{ + g_TraceLog->m_stack.pop_back(); + + auto& out = g_TraceLog->m_file; + size_t depth = g_TraceLog->get_depth(); + size_t out_indent = (depth * 2) * g_trace_indent; + size_t in_indent = (depth * 2 + 1) * g_trace_indent; + + std::string out_indents(""); + if (out_indent) + { + out_indents = std::string(out_indent, ' '); + } + + std::string in_indents(in_indent, ' '); + + if (m_attrib_count) + { + out << "\n" << in_indents; + } + out << "]\n"; + + out << out_indents << "]"; +} + +/* See header for documentation. */ +void trace_add_data( + const char* key, + const char* format, + ... +) { + constexpr size_t bufsz = 256; + char buffer[bufsz]; + + va_list args; + va_start (args, format); + vsnprintf (buffer, bufsz, format, args); + va_end (args); + + // Guarantee there is a nul terminator + buffer[bufsz - 1] = 0; + + std::string value = "\"" + std::string(buffer) + "\""; + + TraceNode* node = g_TraceLog->get_current_leaf(); + node->add_attrib("str", key, value); +} + +/* See header for documentation. */ +void trace_add_data( + const char* key, + float value +) { + char buffer[256]; + sprintf(buffer, "%.20g", (double)value); + TraceNode* node = g_TraceLog->get_current_leaf(); + node->add_attrib("float", key, buffer); +} + +/* See header for documentation. */ +void trace_add_data( + const char* key, + int value +) { + TraceNode* node = g_TraceLog->get_current_leaf(); + node->add_attrib("int", key, std::to_string(value)); +} + +/* See header for documentation. */ +void trace_add_data( + const char* key, + unsigned int value +) { + TraceNode* node = g_TraceLog->get_current_leaf(); + node->add_attrib("int", key, std::to_string(value)); +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.h new file mode 100644 index 00000000000..f5586b0ad59 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.h @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2021-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief This module provides a set of diagnostic tracing utilities. + * + * Overview + * ======== + * + * The built-in diagnostic trace tool generates a hierarchical JSON tree structure. The tree + * hierarchy contains three levels: + * + * - block + * - pass + * - candidate + * + * One block node exists for each compressed block in the image. One pass node exists for each major + * pass (N partition, M planes, O components) applied to a block. One candidate node exists for each + * encoding candidate trialed for a pass. + * + * Each node contains both the hierarchy but also a number of attributes which explain the behavior. + * For example, the block node contains the block coordinates in the image, the pass explains the + * pass configuration, and the candidate will explain the candidate encoding such as weight + * decimation, refinement error, etc. + * + * Trace Nodes are designed as scope-managed C++ objects with stack-like push/pop behavior. + * Constructing a trace node on the stack will automatically add it to the current node as a child, + * and then make it the current node. Destroying the current node will pop the stack and set the + * parent to the current node. This provides a robust mechanism for ensuring reliable nesting in the + * tree structure. + * + * A set of utility macros are provided to add attribute annotations to the current trace node. + * + * Usage + * ===== + * + * Create Trace Nodes on the stack using the @c TRACE_NODE() macro. This will compile-out completely + * in builds with diagnostics disabled. + * + * Add annotations to the current trace node using the @c trace_add_data() macro. This will + * similarly compile out completely in builds with diagnostics disabled. + * + * If you need to add additional code to support diagnostics-only behavior wrap + * it in preprocessor guards: + * + * #if defined(ASTCENC_DIAGNOSTICS) + * #endif + */ + +#ifndef ASTCENC_DIAGNOSTIC_TRACE_INCLUDED +#define ASTCENC_DIAGNOSTIC_TRACE_INCLUDED + +#if defined(ASTCENC_DIAGNOSTICS) + +#include <iostream> +#include <fstream> +#include <vector> + +/** + * @brief Class representing a single node in the trace hierarchy. + */ +class TraceNode +{ +public: + /** + * @brief Construct a new node. + * + * Constructing a node will push to the the top of the stack, automatically making it a child of + * the current node, and then setting it to become the current node. + * + * @param format The format template for the node name. + * @param ... The format parameters. + */ + TraceNode(const char* format, ...); + + /** + * @brief Add an attribute to this node. + * + * Note that no quoting is applied to the @c value, so if quoting is needed it must be done by + * the caller. + * + * @param type The type of the attribute. + * @param key The key of the attribute. + * @param value The value of the attribute. + */ + void add_attrib(std::string type, std::string key, std::string value); + + /** + * @brief Destroy this node. + * + * Destroying a node will pop it from the top of the stack, making its parent the current node. + * It is invalid behavior to destroy a node that is not the current node; usage must conform to + * stack push-pop semantics. + */ + ~TraceNode(); + + /** + * @brief The number of attributes and child nodes in this node. + */ + unsigned int m_attrib_count { 0 }; +}; + +/** + * @brief Class representing the trace log file being written. + */ +class TraceLog +{ +public: + /** + * @brief Create a new trace log. + * + * The trace log is global; there can be only one at a time. + * + * @param file_name The name of the file to write. + */ + TraceLog(const char* file_name); + + /** + * @brief Detroy the trace log. + * + * Trace logs MUST be cleanly destroyed to ensure the file gets written. + */ + ~TraceLog(); + + /** + * @brief Get the current child node. + * + * @return The current leaf node. + */ + TraceNode* get_current_leaf(); + + /** + * @brief Get the stack depth of the current child node. + * + * @return The current leaf node stack depth. + */ + size_t get_depth(); + + /** + * @brief The file stream to write to. + */ + std::ofstream m_file; + + /** + * @brief The stack of nodes (newest at the back). + */ + std::vector<TraceNode*> m_stack; + +private: + /** + * @brief The root node in the JSON file. + */ + TraceNode* m_root; +}; + +/** + * @brief Utility macro to create a trace node on the stack. + * + * @param name The variable name to use. + * @param ... The name template and format parameters. + */ +#define TRACE_NODE(name, ...) TraceNode name(__VA_ARGS__); + +/** + * @brief Add a string annotation to the current node. + * + * @param key The name of the attribute. + * @param format The format template for the attribute value. + * @param ... The format parameters. + */ +void trace_add_data(const char* key, const char* format, ...); + +/** + * @brief Add a float annotation to the current node. + * + * @param key The name of the attribute. + * @param value The value of the attribute. + */ +void trace_add_data(const char* key, float value); + +/** + * @brief Add an integer annotation to the current node. + * + * @param key The name of the attribute. + * @param value The value of the attribute. + */ +void trace_add_data(const char* key, int value); + +/** + * @brief Add an unsigned integer annotation to the current node. + * + * @param key The name of the attribute. + * @param value The value of the attribute. + */ +void trace_add_data(const char* key, unsigned int value); + +#else + +#define TRACE_NODE(name, ...) + +#define trace_add_data(...) + +#endif + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_entry.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_entry.cpp new file mode 100644 index 00000000000..95125b3998e --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_entry.cpp @@ -0,0 +1,1442 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for the library entrypoint. + */ + +#include <array> +#include <cstring> +#include <new> + +#include "astcenc.h" +#include "astcenc_internal_entry.h" +#include "astcenc_diagnostic_trace.h" + +/** + * @brief Record of the quality tuning parameter values. + * + * See the @c astcenc_config structure for detailed parameter documentation. + * + * Note that the mse_overshoot entries are scaling factors relative to the base MSE to hit db_limit. + * A 20% overshoot is harder to hit for a higher base db_limit, so we may actually use lower ratios + * for the more through search presets because the underlying db_limit is so much higher. + */ +struct astcenc_preset_config +{ + float quality; + unsigned int tune_partition_count_limit; + unsigned int tune_2partition_index_limit; + unsigned int tune_3partition_index_limit; + unsigned int tune_4partition_index_limit; + unsigned int tune_block_mode_limit; + unsigned int tune_refinement_limit; + unsigned int tune_candidate_limit; + unsigned int tune_2partitioning_candidate_limit; + unsigned int tune_3partitioning_candidate_limit; + unsigned int tune_4partitioning_candidate_limit; + float tune_db_limit_a_base; + float tune_db_limit_b_base; + float tune_mode0_mse_overshoot; + float tune_refinement_mse_overshoot; + float tune_2_partition_early_out_limit_factor; + float tune_3_partition_early_out_limit_factor; + float tune_2_plane_early_out_limit_correlation; +}; + + +/** + * @brief The static quality presets that are built-in for high bandwidth + * presets (x < 25 texels per block). + */ +static const std::array<astcenc_preset_config, 6> preset_configs_high {{ + { + ASTCENC_PRE_FASTEST, + 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 3.5f, 1.0f, 1.0f, 0.85f + }, { + ASTCENC_PRE_FAST, + 3, 18, 10, 8, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 3.5f, 1.0f, 1.0f, 0.90f + }, { + ASTCENC_PRE_MEDIUM, + 4, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 2.5f, 2.5f, 1.1f, 1.05f, 0.95f + }, { + ASTCENC_PRE_THOROUGH, + 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 10.0f, 1.35f, 1.15f, 0.97f + }, { + ASTCENC_PRE_VERYTHOROUGH, + 4, 256, 128, 64, 98, 4, 6, 20, 14, 8, 200.0f, 200.0f, 10.0f, 10.0f, 1.6f, 1.4f, 0.98f + }, { + ASTCENC_PRE_EXHAUSTIVE, + 4, 512, 512, 512, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 10.0f, 2.0f, 2.0f, 0.99f + } +}}; + +/** + * @brief The static quality presets that are built-in for medium bandwidth + * presets (25 <= x < 64 texels per block). + */ +static const std::array<astcenc_preset_config, 6> preset_configs_mid {{ + { + ASTCENC_PRE_FASTEST, + 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 3.5f, 1.0f, 1.0f, 0.80f + }, { + ASTCENC_PRE_FAST, + 3, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 3.5f, 1.0f, 1.0f, 0.85f + }, { + ASTCENC_PRE_MEDIUM, + 4, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.0f, 3.0f, 1.1f, 1.05f, 0.90f + }, { + ASTCENC_PRE_THOROUGH, + 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 10.0f, 1.4f, 1.2f, 0.95f + }, { + ASTCENC_PRE_VERYTHOROUGH, + 4, 256, 128, 64, 98, 4, 6, 12, 8, 3, 200.0f, 200.0f, 10.0f, 10.0f, 1.6f, 1.4f, 0.98f + }, { + ASTCENC_PRE_EXHAUSTIVE, + 4, 256, 256, 256, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 10.0f, 2.0f, 2.0f, 0.99f + } +}}; + +/** + * @brief The static quality presets that are built-in for low bandwidth + * presets (64 <= x texels per block). + */ +static const std::array<astcenc_preset_config, 6> preset_configs_low {{ + { + ASTCENC_PRE_FASTEST, + 2, 10, 6, 4, 40, 2, 2, 2, 2, 2, 85.0f, 63.0f, 3.5f, 3.5f, 1.0f, 1.0f, 0.80f + }, { + ASTCENC_PRE_FAST, + 2, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.0f, 63.0f, 3.5f, 3.5f, 1.0f, 1.0f, 0.85f + }, { + ASTCENC_PRE_MEDIUM, + 3, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.5f, 3.5f, 1.1f, 1.05f, 0.90f + }, { + ASTCENC_PRE_THOROUGH, + 4, 82, 60, 30, 93, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 10.0f, 1.3f, 1.2f, 0.97f + }, { + ASTCENC_PRE_VERYTHOROUGH, + 4, 256, 128, 64, 98, 4, 6, 9, 5, 2, 200.0f, 200.0f, 10.0f, 10.0f, 1.6f, 1.4f, 0.98f + }, { + ASTCENC_PRE_EXHAUSTIVE, + 4, 256, 256, 256, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 10.0f, 2.0f, 2.0f, 0.99f + } +}}; + +/** + * @brief Validate CPU floating point meets assumptions made in the codec. + * + * The codec is written with the assumption that a float threaded through the @c if32 union will be + * stored and reloaded as a 32-bit IEEE-754 float with round-to-nearest rounding. This is always the + * case in an IEEE-754 compliant system, however not every system or compilation mode is actually + * IEEE-754 compliant. This normally fails if the code is compiled with fast math enabled. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_cpu_float() +{ + if32 p; + volatile float xprec_testval = 2.51f; + p.f = xprec_testval + 12582912.0f; + float q = p.f - 12582912.0f; + + if (q != 3.0f) + { + return ASTCENC_ERR_BAD_CPU_FLOAT; + } + + return ASTCENC_SUCCESS; +} + +/** + * @brief Validate CPU ISA support meets the requirements of this build of the library. + * + * Each library build is statically compiled for a particular set of CPU ISA features, such as the + * SIMD support or other ISA extensions such as POPCNT. This function checks that the host CPU + * actually supports everything this build needs. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_cpu_isa() +{ + #if ASTCENC_SSE >= 41 + if (!cpu_supports_sse41()) + { + return ASTCENC_ERR_BAD_CPU_ISA; + } + #endif + + #if ASTCENC_POPCNT >= 1 + if (!cpu_supports_popcnt()) + { + return ASTCENC_ERR_BAD_CPU_ISA; + } + #endif + + #if ASTCENC_F16C >= 1 + if (!cpu_supports_f16c()) + { + return ASTCENC_ERR_BAD_CPU_ISA; + } + #endif + + #if ASTCENC_AVX >= 2 + if (!cpu_supports_avx2()) + { + return ASTCENC_ERR_BAD_CPU_ISA; + } + #endif + + return ASTCENC_SUCCESS; +} + +/** + * @brief Validate config profile. + * + * @param profile The profile to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_profile( + astcenc_profile profile +) { + // Values in this enum are from an external user, so not guaranteed to be + // bounded to the enum values + switch (static_cast<int>(profile)) + { + case ASTCENC_PRF_LDR_SRGB: + case ASTCENC_PRF_LDR: + case ASTCENC_PRF_HDR_RGB_LDR_A: + case ASTCENC_PRF_HDR: + return ASTCENC_SUCCESS; + default: + return ASTCENC_ERR_BAD_PROFILE; + } +} + +/** + * @brief Validate block size. + * + * @param block_x The block x dimensions. + * @param block_y The block y dimensions. + * @param block_z The block z dimensions. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_block_size( + unsigned int block_x, + unsigned int block_y, + unsigned int block_z +) { + // Test if this is a legal block size at all + bool is_legal = (((block_z <= 1) && is_legal_2d_block_size(block_x, block_y)) || + ((block_z >= 2) && is_legal_3d_block_size(block_x, block_y, block_z))); + if (!is_legal) + { + return ASTCENC_ERR_BAD_BLOCK_SIZE; + } + + // Test if this build has sufficient capacity for this block size + bool have_capacity = (block_x * block_y * block_z) <= BLOCK_MAX_TEXELS; + if (!have_capacity) + { + return ASTCENC_ERR_NOT_IMPLEMENTED; + } + + return ASTCENC_SUCCESS; +} + +/** + * @brief Validate flags. + * + * @param flags The flags to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_flags( + unsigned int flags +) { + // Flags field must not contain any unknown flag bits + unsigned int exMask = ~ASTCENC_ALL_FLAGS; + if (popcount(flags & exMask) != 0) + { + return ASTCENC_ERR_BAD_FLAGS; + } + + // Flags field must only contain at most a single map type + exMask = ASTCENC_FLG_MAP_MASK + | ASTCENC_FLG_MAP_NORMAL + | ASTCENC_FLG_MAP_RGBM; + if (popcount(flags & exMask) > 1) + { + return ASTCENC_ERR_BAD_FLAGS; + } + + return ASTCENC_SUCCESS; +} + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Validate single channel compression swizzle. + * + * @param swizzle The swizzle to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_compression_swz( + astcenc_swz swizzle +) { + // Not all enum values are handled; SWZ_Z is invalid for compression + switch (static_cast<int>(swizzle)) + { + case ASTCENC_SWZ_R: + case ASTCENC_SWZ_G: + case ASTCENC_SWZ_B: + case ASTCENC_SWZ_A: + case ASTCENC_SWZ_0: + case ASTCENC_SWZ_1: + return ASTCENC_SUCCESS; + default: + return ASTCENC_ERR_BAD_SWIZZLE; + } +} + +/** + * @brief Validate overall compression swizzle. + * + * @param swizzle The swizzle to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_compression_swizzle( + const astcenc_swizzle& swizzle +) { + if (validate_compression_swz(swizzle.r) || + validate_compression_swz(swizzle.g) || + validate_compression_swz(swizzle.b) || + validate_compression_swz(swizzle.a)) + { + return ASTCENC_ERR_BAD_SWIZZLE; + } + + return ASTCENC_SUCCESS; +} +#endif + +/** + * @brief Validate single channel decompression swizzle. + * + * @param swizzle The swizzle to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_decompression_swz( + astcenc_swz swizzle +) { + // Values in this enum are from an external user, so not guaranteed to be + // bounded to the enum values + switch (static_cast<int>(swizzle)) + { + case ASTCENC_SWZ_R: + case ASTCENC_SWZ_G: + case ASTCENC_SWZ_B: + case ASTCENC_SWZ_A: + case ASTCENC_SWZ_0: + case ASTCENC_SWZ_1: + case ASTCENC_SWZ_Z: + return ASTCENC_SUCCESS; + default: + return ASTCENC_ERR_BAD_SWIZZLE; + } +} + +/** + * @brief Validate overall decompression swizzle. + * + * @param swizzle The swizzle to check. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_decompression_swizzle( + const astcenc_swizzle& swizzle +) { + if (validate_decompression_swz(swizzle.r) || + validate_decompression_swz(swizzle.g) || + validate_decompression_swz(swizzle.b) || + validate_decompression_swz(swizzle.a)) + { + return ASTCENC_ERR_BAD_SWIZZLE; + } + + return ASTCENC_SUCCESS; +} + +/** + * Validate that an incoming configuration is in-spec. + * + * This function can respond in two ways: + * + * * Numerical inputs that have valid ranges are clamped to those valid ranges. No error is thrown + * for out-of-range inputs in this case. + * * Numerical inputs and logic inputs are are logically invalid and which make no sense + * algorithmically will return an error. + * + * @param[in,out] config The input compressor configuration. + * + * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure. + */ +static astcenc_error validate_config( + astcenc_config &config +) { + astcenc_error status; + + status = validate_profile(config.profile); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + status = validate_flags(config.flags); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + status = validate_block_size(config.block_x, config.block_y, config.block_z); + if (status != ASTCENC_SUCCESS) + { + return status; + } + +#if defined(ASTCENC_DECOMPRESS_ONLY) + // Decompress-only builds only support decompress-only contexts + if (!(config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)) + { + return ASTCENC_ERR_BAD_PARAM; + } +#endif + + config.rgbm_m_scale = astc::max(config.rgbm_m_scale, 1.0f); + + config.tune_partition_count_limit = astc::clamp(config.tune_partition_count_limit, 1u, 4u); + config.tune_2partition_index_limit = astc::clamp(config.tune_2partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS); + config.tune_3partition_index_limit = astc::clamp(config.tune_3partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS); + config.tune_4partition_index_limit = astc::clamp(config.tune_4partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS); + config.tune_block_mode_limit = astc::clamp(config.tune_block_mode_limit, 1u, 100u); + config.tune_refinement_limit = astc::max(config.tune_refinement_limit, 1u); + config.tune_candidate_limit = astc::clamp(config.tune_candidate_limit, 1u, TUNE_MAX_TRIAL_CANDIDATES); + config.tune_2partitioning_candidate_limit = astc::clamp(config.tune_2partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_3partitioning_candidate_limit = astc::clamp(config.tune_3partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_4partitioning_candidate_limit = astc::clamp(config.tune_4partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_db_limit = astc::max(config.tune_db_limit, 0.0f); + config.tune_mode0_mse_overshoot = astc::max(config.tune_mode0_mse_overshoot, 1.0f); + config.tune_refinement_mse_overshoot = astc::max(config.tune_refinement_mse_overshoot, 1.0f); + config.tune_2_partition_early_out_limit_factor = astc::max(config.tune_2_partition_early_out_limit_factor, 0.0f); + config.tune_3_partition_early_out_limit_factor = astc::max(config.tune_3_partition_early_out_limit_factor, 0.0f); + config.tune_2_plane_early_out_limit_correlation = astc::max(config.tune_2_plane_early_out_limit_correlation, 0.0f); + + // Specifying a zero weight color component is not allowed; force to small value + float max_weight = astc::max(astc::max(config.cw_r_weight, config.cw_g_weight), + astc::max(config.cw_b_weight, config.cw_a_weight)); + if (max_weight > 0.0f) + { + max_weight /= 1000.0f; + config.cw_r_weight = astc::max(config.cw_r_weight, max_weight); + config.cw_g_weight = astc::max(config.cw_g_weight, max_weight); + config.cw_b_weight = astc::max(config.cw_b_weight, max_weight); + config.cw_a_weight = astc::max(config.cw_a_weight, max_weight); + } + // If all color components error weights are zero then return an error + else + { + return ASTCENC_ERR_BAD_PARAM; + } + + return ASTCENC_SUCCESS; +} + +/* See header for documentation. */ +astcenc_error astcenc_config_init( + astcenc_profile profile, + unsigned int block_x, + unsigned int block_y, + unsigned int block_z, + float quality, + unsigned int flags, + astcenc_config* configp +) { + astcenc_error status; + + // Check basic library compatibility options here so they are checked early. Note, these checks + // are repeated in context_alloc for cases where callers use a manually defined config struct + status = validate_cpu_isa(); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + status = validate_cpu_float(); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + // Zero init all config fields; although most of will be over written + astcenc_config& config = *configp; + std::memset(&config, 0, sizeof(config)); + + // Process the block size + block_z = astc::max(block_z, 1u); // For 2D blocks Z==0 is accepted, but convert to 1 + status = validate_block_size(block_x, block_y, block_z); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + config.block_x = block_x; + config.block_y = block_y; + config.block_z = block_z; + + float texels = static_cast<float>(block_x * block_y * block_z); + float ltexels = logf(texels) / logf(10.0f); + + // Process the performance quality level or preset; note that this must be done before we + // process any additional settings, such as color profile and flags, which may replace some of + // these settings with more use case tuned values + if (quality < ASTCENC_PRE_FASTEST || + quality > ASTCENC_PRE_EXHAUSTIVE) + { + return ASTCENC_ERR_BAD_QUALITY; + } + + static const std::array<astcenc_preset_config, 6>* preset_configs; + int texels_int = block_x * block_y * block_z; + if (texels_int < 25) + { + preset_configs = &preset_configs_high; + } + else if (texels_int < 64) + { + preset_configs = &preset_configs_mid; + } + else + { + preset_configs = &preset_configs_low; + } + + // Determine which preset to use, or which pair to interpolate + size_t start; + size_t end; + for (end = 0; end < preset_configs->size(); end++) + { + if ((*preset_configs)[end].quality >= quality) + { + break; + } + } + + start = end == 0 ? 0 : end - 1; + + // Start and end node are the same - so just transfer the values. + if (start == end) + { + config.tune_partition_count_limit = (*preset_configs)[start].tune_partition_count_limit; + config.tune_2partition_index_limit = (*preset_configs)[start].tune_2partition_index_limit; + config.tune_3partition_index_limit = (*preset_configs)[start].tune_3partition_index_limit; + config.tune_4partition_index_limit = (*preset_configs)[start].tune_4partition_index_limit; + config.tune_block_mode_limit = (*preset_configs)[start].tune_block_mode_limit; + config.tune_refinement_limit = (*preset_configs)[start].tune_refinement_limit; + config.tune_candidate_limit = astc::min((*preset_configs)[start].tune_candidate_limit, TUNE_MAX_TRIAL_CANDIDATES); + config.tune_2partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_2partitioning_candidate_limit, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_3partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_3partitioning_candidate_limit, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_4partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_4partitioning_candidate_limit, TUNE_MAX_PARTITIIONING_CANDIDATES); + config.tune_db_limit = astc::max((*preset_configs)[start].tune_db_limit_a_base - 35 * ltexels, + (*preset_configs)[start].tune_db_limit_b_base - 19 * ltexels); + + config.tune_mode0_mse_overshoot = (*preset_configs)[start].tune_mode0_mse_overshoot; + config.tune_refinement_mse_overshoot = (*preset_configs)[start].tune_refinement_mse_overshoot; + + config.tune_2_partition_early_out_limit_factor = (*preset_configs)[start].tune_2_partition_early_out_limit_factor; + config.tune_3_partition_early_out_limit_factor =(*preset_configs)[start].tune_3_partition_early_out_limit_factor; + config.tune_2_plane_early_out_limit_correlation = (*preset_configs)[start].tune_2_plane_early_out_limit_correlation; + } + // Start and end node are not the same - so interpolate between them + else + { + auto& node_a = (*preset_configs)[start]; + auto& node_b = (*preset_configs)[end]; + + float wt_range = node_b.quality - node_a.quality; + assert(wt_range > 0); + + // Compute interpolation factors + float wt_node_a = (node_b.quality - quality) / wt_range; + float wt_node_b = (quality - node_a.quality) / wt_range; + + #define LERP(param) ((node_a.param * wt_node_a) + (node_b.param * wt_node_b)) + #define LERPI(param) astc::flt2int_rtn(\ + (static_cast<float>(node_a.param) * wt_node_a) + \ + (static_cast<float>(node_b.param) * wt_node_b)) + #define LERPUI(param) static_cast<unsigned int>(LERPI(param)) + + config.tune_partition_count_limit = LERPI(tune_partition_count_limit); + config.tune_2partition_index_limit = LERPI(tune_2partition_index_limit); + config.tune_3partition_index_limit = LERPI(tune_3partition_index_limit); + config.tune_4partition_index_limit = LERPI(tune_4partition_index_limit); + config.tune_block_mode_limit = LERPI(tune_block_mode_limit); + config.tune_refinement_limit = LERPI(tune_refinement_limit); + config.tune_candidate_limit = astc::min(LERPUI(tune_candidate_limit), + TUNE_MAX_TRIAL_CANDIDATES); + config.tune_2partitioning_candidate_limit = astc::min(LERPUI(tune_2partitioning_candidate_limit), + BLOCK_MAX_PARTITIONINGS); + config.tune_3partitioning_candidate_limit = astc::min(LERPUI(tune_3partitioning_candidate_limit), + BLOCK_MAX_PARTITIONINGS); + config.tune_4partitioning_candidate_limit = astc::min(LERPUI(tune_4partitioning_candidate_limit), + BLOCK_MAX_PARTITIONINGS); + config.tune_db_limit = astc::max(LERP(tune_db_limit_a_base) - 35 * ltexels, + LERP(tune_db_limit_b_base) - 19 * ltexels); + + config.tune_mode0_mse_overshoot = LERP(tune_mode0_mse_overshoot); + config.tune_refinement_mse_overshoot = LERP(tune_refinement_mse_overshoot); + + config.tune_2_partition_early_out_limit_factor = LERP(tune_2_partition_early_out_limit_factor); + config.tune_3_partition_early_out_limit_factor = LERP(tune_3_partition_early_out_limit_factor); + config.tune_2_plane_early_out_limit_correlation = LERP(tune_2_plane_early_out_limit_correlation); + #undef LERP + #undef LERPI + #undef LERPUI + } + + // Set heuristics to the defaults for each color profile + config.cw_r_weight = 1.0f; + config.cw_g_weight = 1.0f; + config.cw_b_weight = 1.0f; + config.cw_a_weight = 1.0f; + + config.a_scale_radius = 0; + + config.rgbm_m_scale = 0.0f; + + config.profile = profile; + + // Values in this enum are from an external user, so not guaranteed to be + // bounded to the enum values + switch (static_cast<int>(profile)) + { + case ASTCENC_PRF_LDR: + case ASTCENC_PRF_LDR_SRGB: + break; + case ASTCENC_PRF_HDR_RGB_LDR_A: + case ASTCENC_PRF_HDR: + config.tune_db_limit = 999.0f; + break; + default: + return ASTCENC_ERR_BAD_PROFILE; + } + + // Flags field must not contain any unknown flag bits + status = validate_flags(flags); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + if (flags & ASTCENC_FLG_MAP_NORMAL) + { + // Normal map encoding uses L+A blocks, so allow one more partitioning + // than normal. We need need fewer bits for endpoints, so more likely + // to be able to use more partitions than an RGB/RGBA block + config.tune_partition_count_limit = astc::min(config.tune_partition_count_limit + 1u, 4u); + + config.cw_g_weight = 0.0f; + config.cw_b_weight = 0.0f; + config.tune_2_partition_early_out_limit_factor *= 1.5f; + config.tune_3_partition_early_out_limit_factor *= 1.5f; + config.tune_2_plane_early_out_limit_correlation = 0.99f; + + // Normals are prone to blocking artifacts on smooth curves + // so force compressor to try harder here ... + config.tune_db_limit *= 1.03f; + } + else if (flags & ASTCENC_FLG_MAP_MASK) + { + // Masks are prone to blocking artifacts on mask edges + // so force compressor to try harder here ... + config.tune_db_limit *= 1.03f; + } + else if (flags & ASTCENC_FLG_MAP_RGBM) + { + config.rgbm_m_scale = 5.0f; + config.cw_a_weight = 2.0f * config.rgbm_m_scale; + } + else // (This is color data) + { + // This is a very basic perceptual metric for RGB color data, which weights error + // significance by the perceptual luminance contribution of each color channel. For + // luminance the usual weights to compute luminance from a linear RGB value are as + // follows: + // + // l = r * 0.3 + g * 0.59 + b * 0.11 + // + // ... but we scale these up to keep a better balance between color and alpha. Note + // that if the content is using alpha we'd recommend using the -a option to weight + // the color contribution by the alpha transparency. + if (flags & ASTCENC_FLG_USE_PERCEPTUAL) + { + config.cw_r_weight = 0.30f * 2.25f; + config.cw_g_weight = 0.59f * 2.25f; + config.cw_b_weight = 0.11f * 2.25f; + } + } + config.flags = flags; + + return ASTCENC_SUCCESS; +} + +/* See header for documentation. */ +astcenc_error astcenc_context_alloc( + const astcenc_config* configp, + unsigned int thread_count, + astcenc_context** context +) { + astcenc_error status; + const astcenc_config& config = *configp; + + status = validate_cpu_isa(); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + status = validate_cpu_float(); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + if (thread_count == 0) + { + return ASTCENC_ERR_BAD_PARAM; + } + +#if defined(ASTCENC_DIAGNOSTICS) + // Force single threaded compressor use in diagnostic mode. + if (thread_count != 1) + { + return ASTCENC_ERR_BAD_PARAM; + } +#endif + + astcenc_context* ctxo = new astcenc_context; + astcenc_contexti* ctx = &ctxo->context; + ctx->thread_count = thread_count; + ctx->config = config; + ctx->working_buffers = nullptr; + + // These are allocated per-compress, as they depend on image size + ctx->input_alpha_averages = nullptr; + + // Copy the config first and validate the copy (we may modify it) + status = validate_config(ctx->config); + if (status != ASTCENC_SUCCESS) + { + delete ctxo; + return status; + } + + ctx->bsd = aligned_malloc<block_size_descriptor>(sizeof(block_size_descriptor), ASTCENC_VECALIGN); + bool can_omit_modes = static_cast<bool>(config.flags & ASTCENC_FLG_SELF_DECOMPRESS_ONLY); + init_block_size_descriptor(config.block_x, config.block_y, config.block_z, + can_omit_modes, + config.tune_partition_count_limit, + static_cast<float>(config.tune_block_mode_limit) / 100.0f, + *ctx->bsd); + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + // Do setup only needed by compression + if (!(status & ASTCENC_FLG_DECOMPRESS_ONLY)) + { + // Turn a dB limit into a per-texel error for faster use later + if ((ctx->config.profile == ASTCENC_PRF_LDR) || (ctx->config.profile == ASTCENC_PRF_LDR_SRGB)) + { + ctx->config.tune_db_limit = astc::pow(0.1f, ctx->config.tune_db_limit * 0.1f) * 65535.0f * 65535.0f; + } + else + { + ctx->config.tune_db_limit = 0.0f; + } + + size_t worksize = sizeof(compression_working_buffers) * thread_count; + ctx->working_buffers = aligned_malloc<compression_working_buffers>(worksize, ASTCENC_VECALIGN); + static_assert((sizeof(compression_working_buffers) % ASTCENC_VECALIGN) == 0, + "compression_working_buffers size must be multiple of vector alignment"); + if (!ctx->working_buffers) + { + aligned_free<block_size_descriptor>(ctx->bsd); + delete ctxo; + *context = nullptr; + return ASTCENC_ERR_OUT_OF_MEM; + } + } +#endif + +#if defined(ASTCENC_DIAGNOSTICS) + ctx->trace_log = new TraceLog(ctx->config.trace_file_path); + if (!ctx->trace_log->m_file) + { + return ASTCENC_ERR_DTRACE_FAILURE; + } + + trace_add_data("block_x", config.block_x); + trace_add_data("block_y", config.block_y); + trace_add_data("block_z", config.block_z); +#endif + + *context = ctxo; + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + prepare_angular_tables(); +#endif + + return ASTCENC_SUCCESS; +} + +/* See header dor documentation. */ +void astcenc_context_free( + astcenc_context* ctxo +) { + if (ctxo) + { + astcenc_contexti* ctx = &ctxo->context; + aligned_free<compression_working_buffers>(ctx->working_buffers); + aligned_free<block_size_descriptor>(ctx->bsd); +#if defined(ASTCENC_DIAGNOSTICS) + delete ctx->trace_log; +#endif + delete ctxo; + } +} + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Compress an image, after any preflight has completed. + * + * @param[out] ctxo The compressor context. + * @param thread_index The thread index. + * @param image The intput image. + * @param swizzle The input swizzle. + * @param[out] buffer The output array for the compressed data. + */ +static void compress_image( + astcenc_context& ctxo, + unsigned int thread_index, + const astcenc_image& image, + const astcenc_swizzle& swizzle, + uint8_t* buffer +) { + astcenc_contexti& ctx = ctxo.context; + const block_size_descriptor& bsd = *ctx.bsd; + astcenc_profile decode_mode = ctx.config.profile; + + image_block blk; + + int block_x = bsd.xdim; + int block_y = bsd.ydim; + int block_z = bsd.zdim; + blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z); + + int dim_x = image.dim_x; + int dim_y = image.dim_y; + int dim_z = image.dim_z; + + int xblocks = (dim_x + block_x - 1) / block_x; + int yblocks = (dim_y + block_y - 1) / block_y; + int zblocks = (dim_z + block_z - 1) / block_z; + int block_count = zblocks * yblocks * xblocks; + + int row_blocks = xblocks; + int plane_blocks = xblocks * yblocks; + + // Populate the block channel weights + blk.channel_weight = vfloat4(ctx.config.cw_r_weight, + ctx.config.cw_g_weight, + ctx.config.cw_b_weight, + ctx.config.cw_a_weight); + + // Use preallocated scratch buffer + auto& temp_buffers = ctx.working_buffers[thread_index]; + + // Only the first thread actually runs the initializer + ctxo.manage_compress.init(block_count); + + // Determine if we can use an optimized load function + bool needs_swz = (swizzle.r != ASTCENC_SWZ_R) || (swizzle.g != ASTCENC_SWZ_G) || + (swizzle.b != ASTCENC_SWZ_B) || (swizzle.a != ASTCENC_SWZ_A); + + bool needs_hdr = (decode_mode == ASTCENC_PRF_HDR) || + (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A); + + bool use_fast_load = !needs_swz && !needs_hdr && + block_z == 1 && image.data_type == ASTCENC_TYPE_U8; + + auto load_func = load_image_block; + if (use_fast_load) + { + load_func = load_image_block_fast_ldr; + } + + // All threads run this processing loop until there is no work remaining + while (true) + { + unsigned int count; + unsigned int base = ctxo.manage_compress.get_task_assignment(16, count); + if (!count) + { + break; + } + + for (unsigned int i = base; i < base + count; i++) + { + // Decode i into x, y, z block indices + int z = i / plane_blocks; + unsigned int rem = i - (z * plane_blocks); + int y = rem / row_blocks; + int x = rem - (y * row_blocks); + + // Test if we can apply some basic alpha-scale RDO + bool use_full_block = true; + if (ctx.config.a_scale_radius != 0 && block_z == 1) + { + int start_x = x * block_x; + int end_x = astc::min(dim_x, start_x + block_x); + + int start_y = y * block_y; + int end_y = astc::min(dim_y, start_y + block_y); + + // SATs accumulate error, so don't test exactly zero. Test for + // less than 1 alpha in the expanded block footprint that + // includes the alpha radius. + int x_footprint = block_x + 2 * (ctx.config.a_scale_radius - 1); + + int y_footprint = block_y + 2 * (ctx.config.a_scale_radius - 1); + + float footprint = static_cast<float>(x_footprint * y_footprint); + float threshold = 0.9f / (255.0f * footprint); + + // Do we have any alpha values? + use_full_block = false; + for (int ay = start_y; ay < end_y; ay++) + { + for (int ax = start_x; ax < end_x; ax++) + { + float a_avg = ctx.input_alpha_averages[ay * dim_x + ax]; + if (a_avg > threshold) + { + use_full_block = true; + ax = end_x; + ay = end_y; + } + } + } + } + + // Fetch the full block for compression + if (use_full_block) + { + load_func(decode_mode, image, blk, bsd, x * block_x, y * block_y, z * block_z, swizzle); + + // Scale RGB error contribution by the maximum alpha in the block + // This encourages preserving alpha accuracy in regions with high + // transparency, and can buy up to 0.5 dB PSNR. + if (ctx.config.flags & ASTCENC_FLG_USE_ALPHA_WEIGHT) + { + float alpha_scale = blk.data_max.lane<3>() * (1.0f / 65535.0f); + blk.channel_weight = vfloat4(ctx.config.cw_r_weight * alpha_scale, + ctx.config.cw_g_weight * alpha_scale, + ctx.config.cw_b_weight * alpha_scale, + ctx.config.cw_a_weight); + } + } + // Apply alpha scale RDO - substitute constant color block + else + { + blk.origin_texel = vfloat4::zero(); + blk.data_min = vfloat4::zero(); + blk.data_mean = vfloat4::zero(); + blk.data_max = vfloat4::zero(); + blk.grayscale = true; + } + + int offset = ((z * yblocks + y) * xblocks + x) * 16; + uint8_t *bp = buffer + offset; + physical_compressed_block* pcb = reinterpret_cast<physical_compressed_block*>(bp); + compress_block(ctx, blk, *pcb, temp_buffers); + } + + ctxo.manage_compress.complete_task_assignment(count); + } +} + +/** + * @brief Compute regional averages in an image. + * + * This function can be called by multiple threads, but only after a single + * thread calls the setup function @c init_compute_averages(). + * + * Results are written back into @c img->input_alpha_averages. + * + * @param[out] ctx The context. + * @param ag The average and variance arguments created during setup. + */ +static void compute_averages( + astcenc_context& ctx, + const avg_args &ag +) { + pixel_region_args arg = ag.arg; + arg.work_memory = new vfloat4[ag.work_memory_size]; + + int size_x = ag.img_size_x; + int size_y = ag.img_size_y; + int size_z = ag.img_size_z; + + int step_xy = ag.blk_size_xy; + int step_z = ag.blk_size_z; + + int y_tasks = (size_y + step_xy - 1) / step_xy; + + // All threads run this processing loop until there is no work remaining + while (true) + { + unsigned int count; + unsigned int base = ctx.manage_avg.get_task_assignment(16, count); + if (!count) + { + break; + } + + for (unsigned int i = base; i < base + count; i++) + { + int z = (i / (y_tasks)) * step_z; + int y = (i - (z * y_tasks)) * step_xy; + + arg.size_z = astc::min(step_z, size_z - z); + arg.offset_z = z; + + arg.size_y = astc::min(step_xy, size_y - y); + arg.offset_y = y; + + for (int x = 0; x < size_x; x += step_xy) + { + arg.size_x = astc::min(step_xy, size_x - x); + arg.offset_x = x; + compute_pixel_region_variance(ctx.context, arg); + } + } + + ctx.manage_avg.complete_task_assignment(count); + } + + delete[] arg.work_memory; +} + +#endif + +/* See header for documentation. */ +astcenc_error astcenc_compress_image( + astcenc_context* ctxo, + astcenc_image* imagep, + const astcenc_swizzle* swizzle, + uint8_t* data_out, + size_t data_len, + unsigned int thread_index +) { +#if defined(ASTCENC_DECOMPRESS_ONLY) + (void)ctxo; + (void)imagep; + (void)swizzle; + (void)data_out; + (void)data_len; + (void)thread_index; + return ASTCENC_ERR_BAD_CONTEXT; +#else + astcenc_contexti* ctx = &ctxo->context; + astcenc_error status; + astcenc_image& image = *imagep; + + if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY) + { + return ASTCENC_ERR_BAD_CONTEXT; + } + + status = validate_compression_swizzle(*swizzle); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + if (thread_index >= ctx->thread_count) + { + return ASTCENC_ERR_BAD_PARAM; + } + + unsigned int block_x = ctx->config.block_x; + unsigned int block_y = ctx->config.block_y; + unsigned int block_z = ctx->config.block_z; + + unsigned int xblocks = (image.dim_x + block_x - 1) / block_x; + unsigned int yblocks = (image.dim_y + block_y - 1) / block_y; + unsigned int zblocks = (image.dim_z + block_z - 1) / block_z; + + // Check we have enough output space (16 bytes per block) + size_t size_needed = xblocks * yblocks * zblocks * 16; + if (data_len < size_needed) + { + return ASTCENC_ERR_OUT_OF_MEM; + } + + // If context thread count is one then implicitly reset + if (ctx->thread_count == 1) + { + astcenc_compress_reset(ctxo); + } + + if (ctx->config.a_scale_radius != 0) + { + // First thread to enter will do setup, other threads will subsequently + // enter the critical section but simply skip over the initialization + auto init_avg = [ctx, &image, swizzle]() { + // Perform memory allocations for the destination buffers + size_t texel_count = image.dim_x * image.dim_y * image.dim_z; + ctx->input_alpha_averages = new float[texel_count]; + + return init_compute_averages( + image, ctx->config.a_scale_radius, *swizzle, + ctx->avg_preprocess_args); + }; + + // Only the first thread actually runs the initializer + ctxo->manage_avg.init(init_avg); + + // All threads will enter this function and dynamically grab work + compute_averages(*ctxo, ctx->avg_preprocess_args); + } + + // Wait for compute_averages to complete before compressing + ctxo->manage_avg.wait(); + + compress_image(*ctxo, thread_index, image, *swizzle, data_out); + + // Wait for compress to complete before freeing memory + ctxo->manage_compress.wait(); + + auto term_compress = [ctx]() { + delete[] ctx->input_alpha_averages; + ctx->input_alpha_averages = nullptr; + }; + + // Only the first thread to arrive actually runs the term + ctxo->manage_compress.term(term_compress); + + return ASTCENC_SUCCESS; +#endif +} + +/* See header for documentation. */ +astcenc_error astcenc_compress_reset( + astcenc_context* ctxo +) { +#if defined(ASTCENC_DECOMPRESS_ONLY) + (void)ctxo; + return ASTCENC_ERR_BAD_CONTEXT; +#else + astcenc_contexti* ctx = &ctxo->context; + if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY) + { + return ASTCENC_ERR_BAD_CONTEXT; + } + + ctxo->manage_avg.reset(); + ctxo->manage_compress.reset(); + return ASTCENC_SUCCESS; +#endif +} + +/* See header for documentation. */ +astcenc_error astcenc_decompress_image( + astcenc_context* ctxo, + const uint8_t* data, + size_t data_len, + astcenc_image* image_outp, + const astcenc_swizzle* swizzle, + unsigned int thread_index +) { + astcenc_error status; + astcenc_image& image_out = *image_outp; + astcenc_contexti* ctx = &ctxo->context; + + // Today this doesn't matter (working set on stack) but might in future ... + if (thread_index >= ctx->thread_count) + { + return ASTCENC_ERR_BAD_PARAM; + } + + status = validate_decompression_swizzle(*swizzle); + if (status != ASTCENC_SUCCESS) + { + return status; + } + + unsigned int block_x = ctx->config.block_x; + unsigned int block_y = ctx->config.block_y; + unsigned int block_z = ctx->config.block_z; + + unsigned int xblocks = (image_out.dim_x + block_x - 1) / block_x; + unsigned int yblocks = (image_out.dim_y + block_y - 1) / block_y; + unsigned int zblocks = (image_out.dim_z + block_z - 1) / block_z; + + int row_blocks = xblocks; + int plane_blocks = xblocks * yblocks; + + // Check we have enough output space (16 bytes per block) + size_t size_needed = xblocks * yblocks * zblocks * 16; + if (data_len < size_needed) + { + return ASTCENC_ERR_OUT_OF_MEM; + } + + image_block blk; + blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z); + + // If context thread count is one then implicitly reset + if (ctx->thread_count == 1) + { + astcenc_decompress_reset(ctxo); + } + + // Only the first thread actually runs the initializer + ctxo->manage_decompress.init(zblocks * yblocks * xblocks); + + // All threads run this processing loop until there is no work remaining + while (true) + { + unsigned int count; + unsigned int base = ctxo->manage_decompress.get_task_assignment(128, count); + if (!count) + { + break; + } + + for (unsigned int i = base; i < base + count; i++) + { + // Decode i into x, y, z block indices + int z = i / plane_blocks; + unsigned int rem = i - (z * plane_blocks); + int y = rem / row_blocks; + int x = rem - (y * row_blocks); + + unsigned int offset = (((z * yblocks + y) * xblocks) + x) * 16; + const uint8_t* bp = data + offset; + + const physical_compressed_block& pcb = *reinterpret_cast<const physical_compressed_block*>(bp); + symbolic_compressed_block scb; + + physical_to_symbolic(*ctx->bsd, pcb, scb); + + decompress_symbolic_block(ctx->config.profile, *ctx->bsd, + x * block_x, y * block_y, z * block_z, + scb, blk); + + store_image_block(image_out, blk, *ctx->bsd, + x * block_x, y * block_y, z * block_z, *swizzle); + } + + ctxo->manage_decompress.complete_task_assignment(count); + } + + return ASTCENC_SUCCESS; +} + +/* See header for documentation. */ +astcenc_error astcenc_decompress_reset( + astcenc_context* ctxo +) { + ctxo->manage_decompress.reset(); + return ASTCENC_SUCCESS; +} + +/* See header for documentation. */ +astcenc_error astcenc_get_block_info( + astcenc_context* ctxo, + const uint8_t data[16], + astcenc_block_info* info +) { +#if defined(ASTCENC_DECOMPRESS_ONLY) + (void)ctxo; + (void)data; + (void)info; + return ASTCENC_ERR_BAD_CONTEXT; +#else + astcenc_contexti* ctx = &ctxo->context; + + // Decode the compressed data into a symbolic form + const physical_compressed_block&pcb = *reinterpret_cast<const physical_compressed_block*>(data); + symbolic_compressed_block scb; + physical_to_symbolic(*ctx->bsd, pcb, scb); + + // Fetch the appropriate partition and decimation tables + block_size_descriptor& bsd = *ctx->bsd; + + // Start from a clean slate + memset(info, 0, sizeof(*info)); + + // Basic info we can always populate + info->profile = ctx->config.profile; + + info->block_x = ctx->config.block_x; + info->block_y = ctx->config.block_y; + info->block_z = ctx->config.block_z; + info->texel_count = bsd.texel_count; + + // Check for error blocks first + info->is_error_block = scb.block_type == SYM_BTYPE_ERROR; + if (info->is_error_block) + { + return ASTCENC_SUCCESS; + } + + // Check for constant color blocks second + info->is_constant_block = scb.block_type == SYM_BTYPE_CONST_F16 || + scb.block_type == SYM_BTYPE_CONST_U16; + if (info->is_constant_block) + { + return ASTCENC_SUCCESS; + } + + // Otherwise handle a full block ; known to be valid after conditions above have been checked + int partition_count = scb.partition_count; + const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); + + const block_mode& bm = bsd.get_block_mode(scb.block_mode); + const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); + + info->weight_x = di.weight_x; + info->weight_y = di.weight_y; + info->weight_z = di.weight_z; + + info->is_dual_plane_block = bm.is_dual_plane != 0; + + info->partition_count = scb.partition_count; + info->partition_index = scb.partition_index; + info->dual_plane_component = scb.plane2_component; + + info->color_level_count = get_quant_level(scb.get_color_quant_mode()); + info->weight_level_count = get_quant_level(bm.get_weight_quant_mode()); + + // Unpack color endpoints for each active partition + for (unsigned int i = 0; i < scb.partition_count; i++) + { + bool rgb_hdr; + bool a_hdr; + vint4 endpnt[2]; + + unpack_color_endpoints(ctx->config.profile, + scb.color_formats[i], + scb.color_values[i], + rgb_hdr, a_hdr, + endpnt[0], endpnt[1]); + + // Store the color endpoint mode info + info->color_endpoint_modes[i] = scb.color_formats[i]; + info->is_hdr_block = info->is_hdr_block || rgb_hdr || a_hdr; + + // Store the unpacked and decoded color endpoint + vmask4 hdr_mask(rgb_hdr, rgb_hdr, rgb_hdr, a_hdr); + for (int j = 0; j < 2; j++) + { + vint4 color_lns = lns_to_sf16(endpnt[j]); + vint4 color_unorm = unorm16_to_sf16(endpnt[j]); + vint4 datai = select(color_unorm, color_lns, hdr_mask); + store(float16_to_float(datai), info->color_endpoints[i][j]); + } + } + + // Unpack weights for each texel + int weight_plane1[BLOCK_MAX_TEXELS]; + int weight_plane2[BLOCK_MAX_TEXELS]; + + unpack_weights(bsd, scb, di, bm.is_dual_plane, weight_plane1, weight_plane2); + for (unsigned int i = 0; i < bsd.texel_count; i++) + { + info->weight_values_plane1[i] = static_cast<float>(weight_plane1[i]) * (1.0f / WEIGHTS_TEXEL_SUM); + if (info->is_dual_plane_block) + { + info->weight_values_plane2[i] = static_cast<float>(weight_plane2[i]) * (1.0f / WEIGHTS_TEXEL_SUM); + } + } + + // Unpack partition assignments for each texel + for (unsigned int i = 0; i < bsd.texel_count; i++) + { + info->partition_assignment[i] = pi.partition_of_texel[i]; + } + + return ASTCENC_SUCCESS; +#endif +} + +/* See header for documentation. */ +const char* astcenc_get_error_string( + astcenc_error status +) { + // Values in this enum are from an external user, so not guaranteed to be + // bounded to the enum values + switch (static_cast<int>(status)) + { + case ASTCENC_SUCCESS: + return "ASTCENC_SUCCESS"; + case ASTCENC_ERR_OUT_OF_MEM: + return "ASTCENC_ERR_OUT_OF_MEM"; + case ASTCENC_ERR_BAD_CPU_FLOAT: + return "ASTCENC_ERR_BAD_CPU_FLOAT"; + case ASTCENC_ERR_BAD_CPU_ISA: + return "ASTCENC_ERR_BAD_CPU_ISA"; + case ASTCENC_ERR_BAD_PARAM: + return "ASTCENC_ERR_BAD_PARAM"; + case ASTCENC_ERR_BAD_BLOCK_SIZE: + return "ASTCENC_ERR_BAD_BLOCK_SIZE"; + case ASTCENC_ERR_BAD_PROFILE: + return "ASTCENC_ERR_BAD_PROFILE"; + case ASTCENC_ERR_BAD_QUALITY: + return "ASTCENC_ERR_BAD_QUALITY"; + case ASTCENC_ERR_BAD_FLAGS: + return "ASTCENC_ERR_BAD_FLAGS"; + case ASTCENC_ERR_BAD_SWIZZLE: + return "ASTCENC_ERR_BAD_SWIZZLE"; + case ASTCENC_ERR_BAD_CONTEXT: + return "ASTCENC_ERR_BAD_CONTEXT"; + case ASTCENC_ERR_NOT_IMPLEMENTED: + return "ASTCENC_ERR_NOT_IMPLEMENTED"; +#if defined(ASTCENC_DIAGNOSTICS) + case ASTCENC_ERR_DTRACE_FAILURE: + return "ASTCENC_ERR_DTRACE_FAILURE"; +#endif + default: + return nullptr; + } +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_find_best_partitioning.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_find_best_partitioning.cpp new file mode 100644 index 00000000000..c59e093e848 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_find_best_partitioning.cpp @@ -0,0 +1,776 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions for finding best partition for a block. + * + * The partition search operates in two stages. The first pass uses kmeans clustering to group + * texels into an ideal partitioning for the requested partition count, and then compares that + * against the 1024 partitionings generated by the ASTC partition hash function. The generated + * partitions are then ranked by the number of texels in the wrong partition, compared to the ideal + * clustering. All 1024 partitions are tested for similarity and ranked, apart from duplicates and + * partitionings that actually generate fewer than the requested partition count, but only the top + * N candidates are actually put through a more detailed search. N is determined by the compressor + * quality preset. + * + * For the detailed search, each candidate is checked against two possible encoding methods: + * + * - The best partitioning assuming different chroma colors (RGB + RGB or RGB + delta endpoints). + * - The best partitioning assuming same chroma colors (RGB + scale endpoints). + * + * This is implemented by computing the compute mean color and dominant direction for each + * partition. This defines two lines, both of which go through the mean color value. + * + * - One line has a direction defined by the dominant direction; this is used to assess the error + * from using an uncorrelated color representation. + * - The other line goes through (0,0,0,1) and is used to assess the error from using a same chroma + * (RGB + scale) color representation. + * + * The best candidate is selected by computing the squared-errors that result from using these + * lines for endpoint selection. + */ + +#include <limits> +#include "astcenc_internal.h" + +/** + * @brief Pick some initial kmeans cluster centers. + * + * @param blk The image block color data to compress. + * @param texel_count The number of texels in the block. + * @param partition_count The number of partitions in the block. + * @param[out] cluster_centers The initial partition cluster center colors. + */ +static void kmeans_init( + const image_block& blk, + unsigned int texel_count, + unsigned int partition_count, + vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS] +) { + promise(texel_count > 0); + promise(partition_count > 0); + + unsigned int clusters_selected = 0; + float distances[BLOCK_MAX_TEXELS]; + + // Pick a random sample as first cluster center; 145897 from random.org + unsigned int sample = 145897 % texel_count; + vfloat4 center_color = blk.texel(sample); + cluster_centers[clusters_selected] = center_color; + clusters_selected++; + + // Compute the distance to the first cluster center + float distance_sum = 0.0f; + for (unsigned int i = 0; i < texel_count; i++) + { + vfloat4 color = blk.texel(i); + vfloat4 diff = color - center_color; + float distance = dot_s(diff * diff, blk.channel_weight); + distance_sum += distance; + distances[i] = distance; + } + + // More numbers from random.org for weighted-random center selection + const float cluster_cutoffs[9] { + 0.626220f, 0.932770f, 0.275454f, + 0.318558f, 0.240113f, 0.009190f, + 0.347661f, 0.731960f, 0.156391f + }; + + unsigned int cutoff = (clusters_selected - 1) + 3 * (partition_count - 2); + + // Pick the remaining samples as needed + while (true) + { + // Pick the next center in a weighted-random fashion. + float summa = 0.0f; + float distance_cutoff = distance_sum * cluster_cutoffs[cutoff++]; + for (sample = 0; sample < texel_count; sample++) + { + summa += distances[sample]; + if (summa >= distance_cutoff) + { + break; + } + } + + // Clamp to a valid range and store the selected cluster center + sample = astc::min(sample, texel_count - 1); + + center_color = blk.texel(sample); + cluster_centers[clusters_selected++] = center_color; + if (clusters_selected >= partition_count) + { + break; + } + + // Compute the distance to the new cluster center, keep the min dist + distance_sum = 0.0f; + for (unsigned int i = 0; i < texel_count; i++) + { + vfloat4 color = blk.texel(i); + vfloat4 diff = color - center_color; + float distance = dot_s(diff * diff, blk.channel_weight); + distance = astc::min(distance, distances[i]); + distance_sum += distance; + distances[i] = distance; + } + } +} + +/** + * @brief Assign texels to clusters, based on a set of chosen center points. + * + * @param blk The image block color data to compress. + * @param texel_count The number of texels in the block. + * @param partition_count The number of partitions in the block. + * @param cluster_centers The partition cluster center colors. + * @param[out] partition_of_texel The partition assigned for each texel. + */ +static void kmeans_assign( + const image_block& blk, + unsigned int texel_count, + unsigned int partition_count, + const vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS], + uint8_t partition_of_texel[BLOCK_MAX_TEXELS] +) { + promise(texel_count > 0); + promise(partition_count > 0); + + uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS] { 0 }; + + // Find the best partition for every texel + for (unsigned int i = 0; i < texel_count; i++) + { + float best_distance = std::numeric_limits<float>::max(); + unsigned int best_partition = 0; + + vfloat4 color = blk.texel(i); + for (unsigned int j = 0; j < partition_count; j++) + { + vfloat4 diff = color - cluster_centers[j]; + float distance = dot_s(diff * diff, blk.channel_weight); + if (distance < best_distance) + { + best_distance = distance; + best_partition = j; + } + } + + partition_of_texel[i] = static_cast<uint8_t>(best_partition); + partition_texel_count[best_partition]++; + } + + // It is possible to get a situation where a partition ends up without any texels. In this case, + // assign texel N to partition N. This is silly, but ensures that every partition retains at + // least one texel. Reassigning a texel in this manner may cause another partition to go empty, + // so if we actually did a reassignment, run the whole loop over again. + bool problem_case; + do + { + problem_case = false; + for (unsigned int i = 0; i < partition_count; i++) + { + if (partition_texel_count[i] == 0) + { + partition_texel_count[partition_of_texel[i]]--; + partition_texel_count[i]++; + partition_of_texel[i] = static_cast<uint8_t>(i); + problem_case = true; + } + } + } while (problem_case); +} + +/** + * @brief Compute new cluster centers based on their center of gravity. + * + * @param blk The image block color data to compress. + * @param texel_count The number of texels in the block. + * @param partition_count The number of partitions in the block. + * @param[out] cluster_centers The new cluster center colors. + * @param partition_of_texel The partition assigned for each texel. + */ +static void kmeans_update( + const image_block& blk, + unsigned int texel_count, + unsigned int partition_count, + vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS], + const uint8_t partition_of_texel[BLOCK_MAX_TEXELS] +) { + promise(texel_count > 0); + promise(partition_count > 0); + + vfloat4 color_sum[BLOCK_MAX_PARTITIONS] { + vfloat4::zero(), + vfloat4::zero(), + vfloat4::zero(), + vfloat4::zero() + }; + + uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS] { 0 }; + + // Find the center-of-gravity in each cluster + for (unsigned int i = 0; i < texel_count; i++) + { + uint8_t partition = partition_of_texel[i]; + color_sum[partition] += blk.texel(i); + partition_texel_count[partition]++; + } + + // Set the center of gravity to be the new cluster center + for (unsigned int i = 0; i < partition_count; i++) + { + float scale = 1.0f / static_cast<float>(partition_texel_count[i]); + cluster_centers[i] = color_sum[i] * scale; + } +} + +/** + * @brief Compute bit-mismatch for partitioning in 2-partition mode. + * + * @param a The texel assignment bitvector for the block. + * @param b The texel assignment bitvector for the partition table. + * + * @return The number of bit mismatches. + */ +static inline unsigned int partition_mismatch2( + const uint64_t a[2], + const uint64_t b[2] +) { + int v1 = popcount(a[0] ^ b[0]) + popcount(a[1] ^ b[1]); + int v2 = popcount(a[0] ^ b[1]) + popcount(a[1] ^ b[0]); + return astc::min(v1, v2); +} + +/** + * @brief Compute bit-mismatch for partitioning in 3-partition mode. + * + * @param a The texel assignment bitvector for the block. + * @param b The texel assignment bitvector for the partition table. + * + * @return The number of bit mismatches. + */ +static inline unsigned int partition_mismatch3( + const uint64_t a[3], + const uint64_t b[3] +) { + int p00 = popcount(a[0] ^ b[0]); + int p01 = popcount(a[0] ^ b[1]); + int p02 = popcount(a[0] ^ b[2]); + + int p10 = popcount(a[1] ^ b[0]); + int p11 = popcount(a[1] ^ b[1]); + int p12 = popcount(a[1] ^ b[2]); + + int p20 = popcount(a[2] ^ b[0]); + int p21 = popcount(a[2] ^ b[1]); + int p22 = popcount(a[2] ^ b[2]); + + int s0 = p11 + p22; + int s1 = p12 + p21; + int v0 = astc::min(s0, s1) + p00; + + int s2 = p10 + p22; + int s3 = p12 + p20; + int v1 = astc::min(s2, s3) + p01; + + int s4 = p10 + p21; + int s5 = p11 + p20; + int v2 = astc::min(s4, s5) + p02; + + return astc::min(v0, v1, v2); +} + +/** + * @brief Compute bit-mismatch for partitioning in 4-partition mode. + * + * @param a The texel assignment bitvector for the block. + * @param b The texel assignment bitvector for the partition table. + * + * @return The number of bit mismatches. + */ +static inline unsigned int partition_mismatch4( + const uint64_t a[4], + const uint64_t b[4] +) { + int p00 = popcount(a[0] ^ b[0]); + int p01 = popcount(a[0] ^ b[1]); + int p02 = popcount(a[0] ^ b[2]); + int p03 = popcount(a[0] ^ b[3]); + + int p10 = popcount(a[1] ^ b[0]); + int p11 = popcount(a[1] ^ b[1]); + int p12 = popcount(a[1] ^ b[2]); + int p13 = popcount(a[1] ^ b[3]); + + int p20 = popcount(a[2] ^ b[0]); + int p21 = popcount(a[2] ^ b[1]); + int p22 = popcount(a[2] ^ b[2]); + int p23 = popcount(a[2] ^ b[3]); + + int p30 = popcount(a[3] ^ b[0]); + int p31 = popcount(a[3] ^ b[1]); + int p32 = popcount(a[3] ^ b[2]); + int p33 = popcount(a[3] ^ b[3]); + + int mx23 = astc::min(p22 + p33, p23 + p32); + int mx13 = astc::min(p21 + p33, p23 + p31); + int mx12 = astc::min(p21 + p32, p22 + p31); + int mx03 = astc::min(p20 + p33, p23 + p30); + int mx02 = astc::min(p20 + p32, p22 + p30); + int mx01 = astc::min(p21 + p30, p20 + p31); + + int v0 = p00 + astc::min(p11 + mx23, p12 + mx13, p13 + mx12); + int v1 = p01 + astc::min(p10 + mx23, p12 + mx03, p13 + mx02); + int v2 = p02 + astc::min(p11 + mx03, p10 + mx13, p13 + mx01); + int v3 = p03 + astc::min(p11 + mx02, p12 + mx01, p10 + mx12); + + return astc::min(v0, v1, v2, v3); +} + +using mismatch_dispatch = unsigned int (*)(const uint64_t*, const uint64_t*); + +/** + * @brief Count the partition table mismatches vs the data clustering. + * + * @param bsd The block size information. + * @param partition_count The number of partitions in the block. + * @param bitmaps The block texel partition assignment patterns. + * @param[out] mismatch_counts The array storing per partitioning mismatch counts. + */ +static void count_partition_mismatch_bits( + const block_size_descriptor& bsd, + unsigned int partition_count, + const uint64_t bitmaps[BLOCK_MAX_PARTITIONS], + unsigned int mismatch_counts[BLOCK_MAX_PARTITIONINGS] +) { + unsigned int active_count = bsd.partitioning_count_selected[partition_count - 1]; + + if (partition_count == 2) + { + for (unsigned int i = 0; i < active_count; i++) + { + mismatch_counts[i] = partition_mismatch2(bitmaps, bsd.coverage_bitmaps_2[i]); + } + } + else if (partition_count == 3) + { + for (unsigned int i = 0; i < active_count; i++) + { + mismatch_counts[i] = partition_mismatch3(bitmaps, bsd.coverage_bitmaps_3[i]); + } + } + else + { + for (unsigned int i = 0; i < active_count; i++) + { + mismatch_counts[i] = partition_mismatch4(bitmaps, bsd.coverage_bitmaps_4[i]); + } + } +} + +/** + * @brief Use counting sort on the mismatch array to sort partition candidates. + * + * @param partitioning_count The number of packed partitionings. + * @param mismatch_count Partitioning mismatch counts, in index order. + * @param[out] partition_ordering Partition index values, in mismatch order. + * + * @return The number of active partitions in this selection. + */ +static unsigned int get_partition_ordering_by_mismatch_bits( + unsigned int partitioning_count, + const unsigned int mismatch_count[BLOCK_MAX_PARTITIONINGS], + unsigned int partition_ordering[BLOCK_MAX_PARTITIONINGS] +) { + unsigned int mscount[256] { 0 }; + + // Create the histogram of mismatch counts + for (unsigned int i = 0; i < partitioning_count; i++) + { + mscount[mismatch_count[i]]++; + } + + unsigned int active_count = partitioning_count - mscount[255]; + + // Create a running sum from the histogram array + // Cells store previous values only; i.e. exclude self after sum + unsigned int summa = 0; + for (unsigned int i = 0; i < 256; i++) + { + unsigned int cnt = mscount[i]; + mscount[i] = summa; + summa += cnt; + } + + // Use the running sum as the index, incrementing after read to allow + // sequential entries with the same count + for (unsigned int i = 0; i < partitioning_count; i++) + { + unsigned int idx = mscount[mismatch_count[i]]++; + partition_ordering[idx] = i; + } + + return active_count; +} + +/** + * @brief Use k-means clustering to compute a partition ordering for a block.. + * + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param partition_count The desired number of partitions in the block. + * @param[out] partition_ordering The list of recommended partition indices, in priority order. + * + * @return The number of active partitionings in this selection. + */ +static unsigned int compute_kmeans_partition_ordering( + const block_size_descriptor& bsd, + const image_block& blk, + unsigned int partition_count, + unsigned int partition_ordering[BLOCK_MAX_PARTITIONINGS] +) { + vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS]; + uint8_t texel_partitions[BLOCK_MAX_TEXELS]; + + // Use three passes of k-means clustering to partition the block data + for (unsigned int i = 0; i < 3; i++) + { + if (i == 0) + { + kmeans_init(blk, bsd.texel_count, partition_count, cluster_centers); + } + else + { + kmeans_update(blk, bsd.texel_count, partition_count, cluster_centers, texel_partitions); + } + + kmeans_assign(blk, bsd.texel_count, partition_count, cluster_centers, texel_partitions); + } + + // Construct the block bitmaps of texel assignments to each partition + uint64_t bitmaps[BLOCK_MAX_PARTITIONS] { 0 }; + unsigned int texels_to_process = astc::min(bsd.texel_count, BLOCK_MAX_KMEANS_TEXELS); + promise(texels_to_process > 0); + for (unsigned int i = 0; i < texels_to_process; i++) + { + unsigned int idx = bsd.kmeans_texels[i]; + bitmaps[texel_partitions[idx]] |= 1ULL << i; + } + + // Count the mismatch between the block and the format's partition tables + unsigned int mismatch_counts[BLOCK_MAX_PARTITIONINGS]; + count_partition_mismatch_bits(bsd, partition_count, bitmaps, mismatch_counts); + + // Sort the partitions based on the number of mismatched bits + return get_partition_ordering_by_mismatch_bits( + bsd.partitioning_count_selected[partition_count - 1], + mismatch_counts, partition_ordering); +} + +/** + * @brief Insert a partitioning into an order list of results, sorted by error. + * + * @param max_values The max number of entries in the best result arrays/ + * @param this_error The error of the new entry. + * @param this_partition The partition ID of the new entry. + * @param[out] best_errors The array of best error values. + * @param[out] best_partitions The array of best partition values. + */ +static void insert_result( + unsigned int max_values, + float this_error, + unsigned int this_partition, + float* best_errors, + unsigned int* best_partitions) +{ + // Don't bother searching if the current worst error beats the new error + if (this_error >= best_errors[max_values - 1]) + { + return; + } + + // Else insert into the list in error-order + for (unsigned int i = 0; i < max_values; i++) + { + // Existing result is better - move on ... + if (this_error > best_errors[i]) + { + continue; + } + + // Move existing results down one + for (unsigned int j = max_values - 1; j > i; j--) + { + best_errors[j] = best_errors[j - 1]; + best_partitions[j] = best_partitions[j - 1]; + } + + // Insert new result + best_errors[i] = this_error; + best_partitions[i] = this_partition; + break; + } +} + +/* See header for documentation. */ +unsigned int find_best_partition_candidates( + const block_size_descriptor& bsd, + const image_block& blk, + unsigned int partition_count, + unsigned int partition_search_limit, + unsigned int best_partitions[TUNE_MAX_PARTITIIONING_CANDIDATES], + unsigned int requested_candidates +) { + // Constant used to estimate quantization error for a given partitioning; the optimal value for + // this depends on bitrate. These values have been determined empirically. + unsigned int texels_per_block = bsd.texel_count; + float weight_imprecision_estim = 0.055f; + if (texels_per_block <= 20) + { + weight_imprecision_estim = 0.03f; + } + else if (texels_per_block <= 31) + { + weight_imprecision_estim = 0.04f; + } + else if (texels_per_block <= 41) + { + weight_imprecision_estim = 0.05f; + } + + promise(partition_count > 0); + promise(partition_search_limit > 0); + + weight_imprecision_estim = weight_imprecision_estim * weight_imprecision_estim; + + unsigned int partition_sequence[BLOCK_MAX_PARTITIONINGS]; + unsigned int sequence_len = compute_kmeans_partition_ordering(bsd, blk, partition_count, partition_sequence); + partition_search_limit = astc::min(partition_search_limit, sequence_len); + requested_candidates = astc::min(partition_search_limit, requested_candidates); + + bool uses_alpha = !blk.is_constant_channel(3); + + // Partitioning errors assuming uncorrelated-chrominance endpoints + float uncor_best_errors[TUNE_MAX_PARTITIIONING_CANDIDATES]; + unsigned int uncor_best_partitions[TUNE_MAX_PARTITIIONING_CANDIDATES]; + + // Partitioning errors assuming same-chrominance endpoints + float samec_best_errors[TUNE_MAX_PARTITIIONING_CANDIDATES]; + unsigned int samec_best_partitions[TUNE_MAX_PARTITIIONING_CANDIDATES]; + + for (unsigned int i = 0; i < requested_candidates; i++) + { + uncor_best_errors[i] = ERROR_CALC_DEFAULT; + samec_best_errors[i] = ERROR_CALC_DEFAULT; + } + + if (uses_alpha) + { + for (unsigned int i = 0; i < partition_search_limit; i++) + { + unsigned int partition = partition_sequence[i]; + const auto& pi = bsd.get_raw_partition_info(partition_count, partition); + + // Compute weighting to give to each component in each partition + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + + compute_avgs_and_dirs_4_comp(pi, blk, pms); + + line4 uncor_lines[BLOCK_MAX_PARTITIONS]; + line4 samec_lines[BLOCK_MAX_PARTITIONS]; + + processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS]; + processed_line4 samec_plines[BLOCK_MAX_PARTITIONS]; + + float uncor_line_lens[BLOCK_MAX_PARTITIONS]; + float samec_line_lens[BLOCK_MAX_PARTITIONS]; + + for (unsigned int j = 0; j < partition_count; j++) + { + partition_metrics& pm = pms[j]; + + uncor_lines[j].a = pm.avg; + uncor_lines[j].b = normalize_safe(pm.dir, unit4()); + + uncor_plines[j].amod = uncor_lines[j].a - uncor_lines[j].b * dot(uncor_lines[j].a, uncor_lines[j].b); + uncor_plines[j].bs = uncor_lines[j].b; + + samec_lines[j].a = vfloat4::zero(); + samec_lines[j].b = normalize_safe(pm.avg, unit4()); + + samec_plines[j].amod = vfloat4::zero(); + samec_plines[j].bs = samec_lines[j].b; + } + + float uncor_error = 0.0f; + float samec_error = 0.0f; + + compute_error_squared_rgba(pi, + blk, + uncor_plines, + samec_plines, + uncor_line_lens, + samec_line_lens, + uncor_error, + samec_error); + + // Compute an estimate of error introduced by weight quantization imprecision. + // This error is computed as follows, for each partition + // 1: compute the principal-axis vector (full length) in error-space + // 2: convert the principal-axis vector to regular RGB-space + // 3: scale the vector by a constant that estimates average quantization error + // 4: for each texel, square the vector, then do a dot-product with the texel's + // error weight; sum up the results across all texels. + // 4(optimized): square the vector once, then do a dot-product with the average + // texel error, then multiply by the number of texels. + + for (unsigned int j = 0; j < partition_count; j++) + { + float tpp = static_cast<float>(pi.partition_texel_count[j]); + vfloat4 error_weights(tpp * weight_imprecision_estim); + + vfloat4 uncor_vector = uncor_lines[j].b * uncor_line_lens[j]; + vfloat4 samec_vector = samec_lines[j].b * samec_line_lens[j]; + + uncor_error += dot_s(uncor_vector * uncor_vector, error_weights); + samec_error += dot_s(samec_vector * samec_vector, error_weights); + } + + insert_result(requested_candidates, uncor_error, partition, uncor_best_errors, uncor_best_partitions); + insert_result(requested_candidates, samec_error, partition, samec_best_errors, samec_best_partitions); + } + } + else + { + for (unsigned int i = 0; i < partition_search_limit; i++) + { + unsigned int partition = partition_sequence[i]; + const auto& pi = bsd.get_raw_partition_info(partition_count, partition); + + // Compute weighting to give to each component in each partition + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms); + + partition_lines3 plines[BLOCK_MAX_PARTITIONS]; + + for (unsigned int j = 0; j < partition_count; j++) + { + partition_metrics& pm = pms[j]; + partition_lines3& pl = plines[j]; + + pl.uncor_line.a = pm.avg; + pl.uncor_line.b = normalize_safe(pm.dir, unit3()); + + pl.samec_line.a = vfloat4::zero(); + pl.samec_line.b = normalize_safe(pm.avg, unit3()); + + pl.uncor_pline.amod = pl.uncor_line.a - pl.uncor_line.b * dot3(pl.uncor_line.a, pl.uncor_line.b); + pl.uncor_pline.bs = pl.uncor_line.b; + + pl.samec_pline.amod = vfloat4::zero(); + pl.samec_pline.bs = pl.samec_line.b; + } + + float uncor_error = 0.0f; + float samec_error = 0.0f; + + compute_error_squared_rgb(pi, + blk, + plines, + uncor_error, + samec_error); + + // Compute an estimate of error introduced by weight quantization imprecision. + // This error is computed as follows, for each partition + // 1: compute the principal-axis vector (full length) in error-space + // 2: convert the principal-axis vector to regular RGB-space + // 3: scale the vector by a constant that estimates average quantization error + // 4: for each texel, square the vector, then do a dot-product with the texel's + // error weight; sum up the results across all texels. + // 4(optimized): square the vector once, then do a dot-product with the average + // texel error, then multiply by the number of texels. + + for (unsigned int j = 0; j < partition_count; j++) + { + partition_lines3& pl = plines[j]; + + float tpp = static_cast<float>(pi.partition_texel_count[j]); + vfloat4 error_weights(tpp * weight_imprecision_estim); + + vfloat4 uncor_vector = pl.uncor_line.b * pl.uncor_line_len; + vfloat4 samec_vector = pl.samec_line.b * pl.samec_line_len; + + uncor_error += dot3_s(uncor_vector * uncor_vector, error_weights); + samec_error += dot3_s(samec_vector * samec_vector, error_weights); + } + + insert_result(requested_candidates, uncor_error, partition, uncor_best_errors, uncor_best_partitions); + insert_result(requested_candidates, samec_error, partition, samec_best_errors, samec_best_partitions); + } + } + + bool best_is_uncor = uncor_best_partitions[0] > samec_best_partitions[0]; + + unsigned int interleave[2 * TUNE_MAX_PARTITIIONING_CANDIDATES]; + for (unsigned int i = 0; i < requested_candidates; i++) + { + if (best_is_uncor) + { + interleave[2 * i] = bsd.get_raw_partition_info(partition_count, uncor_best_partitions[i]).partition_index; + interleave[2 * i + 1] = bsd.get_raw_partition_info(partition_count, samec_best_partitions[i]).partition_index; + } + else + { + interleave[2 * i] = bsd.get_raw_partition_info(partition_count, samec_best_partitions[i]).partition_index; + interleave[2 * i + 1] = bsd.get_raw_partition_info(partition_count, uncor_best_partitions[i]).partition_index; + } + } + + uint64_t bitmasks[1024/64] { 0 }; + unsigned int emitted = 0; + + // Deduplicate the first "requested" entries + for (unsigned int i = 0; i < requested_candidates * 2; i++) + { + unsigned int partition = interleave[i]; + + unsigned int word = partition / 64; + unsigned int bit = partition % 64; + + bool written = bitmasks[word] & (1ull << bit); + + if (!written) + { + best_partitions[emitted] = partition; + bitmasks[word] |= 1ull << bit; + emitted++; + + if (emitted == requested_candidates) + { + break; + } + } + } + + return emitted; +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_ideal_endpoints_and_weights.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_ideal_endpoints_and_weights.cpp new file mode 100644 index 00000000000..3c18e870146 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_ideal_endpoints_and_weights.cpp @@ -0,0 +1,1656 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions for computing color endpoints and texel weights. + */ + +#include <cassert> + +#include "astcenc_internal.h" +#include "astcenc_vecmathlib.h" + +/** + * @brief Compute the infilled weight for N texel indices in a decimated grid. + * + * @param di The weight grid decimation to use. + * @param weights The decimated weight values to use. + * @param index The first texel index to interpolate. + * + * @return The interpolated weight for the given set of SIMD_WIDTH texels. + */ +static vfloat bilinear_infill_vla( + const decimation_info& di, + const float* weights, + unsigned int index +) { + // Load the bilinear filter texel weight indexes in the decimated grid + vint weight_idx0 = vint(di.texel_weights_4t[0] + index); + vint weight_idx1 = vint(di.texel_weights_4t[1] + index); + vint weight_idx2 = vint(di.texel_weights_4t[2] + index); + vint weight_idx3 = vint(di.texel_weights_4t[3] + index); + + // Load the bilinear filter weights from the decimated grid + vfloat weight_val0 = gatherf(weights, weight_idx0); + vfloat weight_val1 = gatherf(weights, weight_idx1); + vfloat weight_val2 = gatherf(weights, weight_idx2); + vfloat weight_val3 = gatherf(weights, weight_idx3); + + // Load the weight contribution factors for each decimated weight + vfloat tex_weight_float0 = loada(di.texel_weights_float_4t[0] + index); + vfloat tex_weight_float1 = loada(di.texel_weights_float_4t[1] + index); + vfloat tex_weight_float2 = loada(di.texel_weights_float_4t[2] + index); + vfloat tex_weight_float3 = loada(di.texel_weights_float_4t[3] + index); + + // Compute the bilinear interpolation to generate the per-texel weight + return (weight_val0 * tex_weight_float0 + weight_val1 * tex_weight_float1) + + (weight_val2 * tex_weight_float2 + weight_val3 * tex_weight_float3); +} + +/** + * @brief Compute the infilled weight for N texel indices in a decimated grid. + * + * This is specialized version which computes only two weights per texel for + * encodings that are only decimated in a single axis. + * + * @param di The weight grid decimation to use. + * @param weights The decimated weight values to use. + * @param index The first texel index to interpolate. + * + * @return The interpolated weight for the given set of SIMD_WIDTH texels. + */ +static vfloat bilinear_infill_vla_2( + const decimation_info& di, + const float* weights, + unsigned int index +) { + // Load the bilinear filter texel weight indexes in the decimated grid + vint weight_idx0 = vint(di.texel_weights_4t[0] + index); + vint weight_idx1 = vint(di.texel_weights_4t[1] + index); + + // Load the bilinear filter weights from the decimated grid + vfloat weight_val0 = gatherf(weights, weight_idx0); + vfloat weight_val1 = gatherf(weights, weight_idx1); + + // Load the weight contribution factors for each decimated weight + vfloat tex_weight_float0 = loada(di.texel_weights_float_4t[0] + index); + vfloat tex_weight_float1 = loada(di.texel_weights_float_4t[1] + index); + + // Compute the bilinear interpolation to generate the per-texel weight + return (weight_val0 * tex_weight_float0 + weight_val1 * tex_weight_float1); +} + +/** + * @brief Compute the ideal endpoints and weights for 1 color component. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param[out] ei The computed ideal endpoints and weights. + * @param component The color component to compute. + */ +static void compute_ideal_colors_and_weights_1_comp( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei, + unsigned int component +) { + unsigned int partition_count = pi.partition_count; + ei.ep.partition_count = partition_count; + promise(partition_count > 0); + + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + + float error_weight; + const float* data_vr = nullptr; + + assert(component < BLOCK_MAX_COMPONENTS); + switch (component) + { + case 0: + error_weight = blk.channel_weight.lane<0>(); + data_vr = blk.data_r; + break; + case 1: + error_weight = blk.channel_weight.lane<1>(); + data_vr = blk.data_g; + break; + case 2: + error_weight = blk.channel_weight.lane<2>(); + data_vr = blk.data_b; + break; + default: + assert(component == 3); + error_weight = blk.channel_weight.lane<3>(); + data_vr = blk.data_a; + break; + } + + vmask4 sep_mask = vint4::lane_id() == vint4(component); + bool is_constant_wes { true }; + float partition0_len_sq { 0.0f }; + + for (unsigned int i = 0; i < partition_count; i++) + { + float lowvalue { 1e10f }; + float highvalue { -1e10f }; + + unsigned int partition_texel_count = pi.partition_texel_count[i]; + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + float value = data_vr[tix]; + lowvalue = astc::min(value, lowvalue); + highvalue = astc::max(value, highvalue); + } + + if (highvalue <= lowvalue) + { + lowvalue = 0.0f; + highvalue = 1e-7f; + } + + float length = highvalue - lowvalue; + float length_squared = length * length; + float scale = 1.0f / length; + + if (i == 0) + { + partition0_len_sq = length_squared; + } + else + { + is_constant_wes = is_constant_wes && length_squared == partition0_len_sq; + } + + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + float value = (data_vr[tix] - lowvalue) * scale; + value = astc::clamp1f(value); + + ei.weights[tix] = value; + ei.weight_error_scale[tix] = length_squared * error_weight; + assert(!astc::isnan(ei.weight_error_scale[tix])); + } + + ei.ep.endpt0[i] = select(blk.data_min, vfloat4(lowvalue), sep_mask); + ei.ep.endpt1[i] = select(blk.data_max, vfloat4(highvalue), sep_mask); + } + + // Zero initialize any SIMD over-fetch + unsigned int texel_count_simd = round_up_to_simd_multiple_vla(texel_count); + for (unsigned int i = texel_count; i < texel_count_simd; i++) + { + ei.weights[i] = 0.0f; + ei.weight_error_scale[i] = 0.0f; + } + + ei.is_constant_weight_error_scale = is_constant_wes; +} + +/** + * @brief Compute the ideal endpoints and weights for 2 color components. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param[out] ei The computed ideal endpoints and weights. + * @param component1 The first color component to compute. + * @param component2 The second color component to compute. + */ +static void compute_ideal_colors_and_weights_2_comp( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei, + int component1, + int component2 +) { + unsigned int partition_count = pi.partition_count; + ei.ep.partition_count = partition_count; + promise(partition_count > 0); + + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + + float error_weight; + const float* data_vr = nullptr; + const float* data_vg = nullptr; + + if (component1 == 0 && component2 == 1) + { + error_weight = hadd_s(blk.channel_weight.swz<0, 1>()) / 2.0f; + + data_vr = blk.data_r; + data_vg = blk.data_g; + } + else if (component1 == 0 && component2 == 2) + { + error_weight = hadd_s(blk.channel_weight.swz<0, 2>()) / 2.0f; + + data_vr = blk.data_r; + data_vg = blk.data_b; + } + else // (component1 == 1 && component2 == 2) + { + assert(component1 == 1 && component2 == 2); + + error_weight = hadd_s(blk.channel_weight.swz<1, 2>()) / 2.0f; + + data_vr = blk.data_g; + data_vg = blk.data_b; + } + + compute_avgs_and_dirs_2_comp(pi, blk, component1, component2, pms); + + bool is_constant_wes { true }; + float partition0_len_sq { 0.0f }; + + vmask4 comp1_mask = vint4::lane_id() == vint4(component1); + vmask4 comp2_mask = vint4::lane_id() == vint4(component2); + + for (unsigned int i = 0; i < partition_count; i++) + { + vfloat4 dir = pms[i].dir; + if (hadd_s(dir) < 0.0f) + { + dir = vfloat4::zero() - dir; + } + + line2 line { pms[i].avg, normalize_safe(dir, unit2()) }; + float lowparam { 1e10f }; + float highparam { -1e10f }; + + unsigned int partition_texel_count = pi.partition_texel_count[i]; + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + vfloat4 point = vfloat2(data_vr[tix], data_vg[tix]); + float param = dot_s(point - line.a, line.b); + ei.weights[tix] = param; + + lowparam = astc::min(param, lowparam); + highparam = astc::max(param, highparam); + } + + // It is possible for a uniform-color partition to produce length=0; + // this causes NaN issues so set to small value to avoid this problem + if (highparam <= lowparam) + { + lowparam = 0.0f; + highparam = 1e-7f; + } + + float length = highparam - lowparam; + float length_squared = length * length; + float scale = 1.0f / length; + + if (i == 0) + { + partition0_len_sq = length_squared; + } + else + { + is_constant_wes = is_constant_wes && length_squared == partition0_len_sq; + } + + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + float idx = (ei.weights[tix] - lowparam) * scale; + idx = astc::clamp1f(idx); + + ei.weights[tix] = idx; + ei.weight_error_scale[tix] = length_squared * error_weight; + assert(!astc::isnan(ei.weight_error_scale[tix])); + } + + vfloat4 lowvalue = line.a + line.b * lowparam; + vfloat4 highvalue = line.a + line.b * highparam; + + vfloat4 ep0 = select(blk.data_min, vfloat4(lowvalue.lane<0>()), comp1_mask); + vfloat4 ep1 = select(blk.data_max, vfloat4(highvalue.lane<0>()), comp1_mask); + + ei.ep.endpt0[i] = select(ep0, vfloat4(lowvalue.lane<1>()), comp2_mask); + ei.ep.endpt1[i] = select(ep1, vfloat4(highvalue.lane<1>()), comp2_mask); + } + + // Zero initialize any SIMD over-fetch + unsigned int texel_count_simd = round_up_to_simd_multiple_vla(texel_count); + for (unsigned int i = texel_count; i < texel_count_simd; i++) + { + ei.weights[i] = 0.0f; + ei.weight_error_scale[i] = 0.0f; + } + + ei.is_constant_weight_error_scale = is_constant_wes; +} + +/** + * @brief Compute the ideal endpoints and weights for 3 color components. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param[out] ei The computed ideal endpoints and weights. + * @param omitted_component The color component excluded from the calculation. + */ +static void compute_ideal_colors_and_weights_3_comp( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei, + unsigned int omitted_component +) { + unsigned int partition_count = pi.partition_count; + ei.ep.partition_count = partition_count; + promise(partition_count > 0); + + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + + float error_weight; + const float* data_vr = nullptr; + const float* data_vg = nullptr; + const float* data_vb = nullptr; + if (omitted_component == 0) + { + error_weight = hadd_s(blk.channel_weight.swz<0, 1, 2>()); + data_vr = blk.data_g; + data_vg = blk.data_b; + data_vb = blk.data_a; + } + else if (omitted_component == 1) + { + error_weight = hadd_s(blk.channel_weight.swz<0, 2, 3>()); + data_vr = blk.data_r; + data_vg = blk.data_b; + data_vb = blk.data_a; + } + else if (omitted_component == 2) + { + error_weight = hadd_s(blk.channel_weight.swz<0, 1, 3>()); + data_vr = blk.data_r; + data_vg = blk.data_g; + data_vb = blk.data_a; + } + else + { + assert(omitted_component == 3); + + error_weight = hadd_s(blk.channel_weight.swz<0, 1, 2>()); + data_vr = blk.data_r; + data_vg = blk.data_g; + data_vb = blk.data_b; + } + + error_weight = error_weight * (1.0f / 3.0f); + + if (omitted_component == 3) + { + compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms); + } + else + { + compute_avgs_and_dirs_3_comp(pi, blk, omitted_component, pms); + } + + bool is_constant_wes { true }; + float partition0_len_sq { 0.0f }; + + for (unsigned int i = 0; i < partition_count; i++) + { + vfloat4 dir = pms[i].dir; + if (hadd_rgb_s(dir) < 0.0f) + { + dir = vfloat4::zero() - dir; + } + + line3 line { pms[i].avg, normalize_safe(dir, unit3()) }; + float lowparam { 1e10f }; + float highparam { -1e10f }; + + unsigned int partition_texel_count = pi.partition_texel_count[i]; + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + vfloat4 point = vfloat3(data_vr[tix], data_vg[tix], data_vb[tix]); + float param = dot3_s(point - line.a, line.b); + ei.weights[tix] = param; + + lowparam = astc::min(param, lowparam); + highparam = astc::max(param, highparam); + } + + // It is possible for a uniform-color partition to produce length=0; + // this causes NaN issues so set to small value to avoid this problem + if (highparam <= lowparam) + { + lowparam = 0.0f; + highparam = 1e-7f; + } + + float length = highparam - lowparam; + float length_squared = length * length; + float scale = 1.0f / length; + + if (i == 0) + { + partition0_len_sq = length_squared; + } + else + { + is_constant_wes = is_constant_wes && length_squared == partition0_len_sq; + } + + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + float idx = (ei.weights[tix] - lowparam) * scale; + idx = astc::clamp1f(idx); + + ei.weights[tix] = idx; + ei.weight_error_scale[tix] = length_squared * error_weight; + assert(!astc::isnan(ei.weight_error_scale[tix])); + } + + vfloat4 ep0 = line.a + line.b * lowparam; + vfloat4 ep1 = line.a + line.b * highparam; + + vfloat4 bmin = blk.data_min; + vfloat4 bmax = blk.data_max; + + assert(omitted_component < BLOCK_MAX_COMPONENTS); + switch (omitted_component) + { + case 0: + ei.ep.endpt0[i] = vfloat4(bmin.lane<0>(), ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>()); + ei.ep.endpt1[i] = vfloat4(bmax.lane<0>(), ep1.lane<0>(), ep1.lane<1>(), ep1.lane<2>()); + break; + case 1: + ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), bmin.lane<1>(), ep0.lane<1>(), ep0.lane<2>()); + ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), bmax.lane<1>(), ep1.lane<1>(), ep1.lane<2>()); + break; + case 2: + ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), bmin.lane<2>(), ep0.lane<2>()); + ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), ep1.lane<1>(), bmax.lane<2>(), ep1.lane<2>()); + break; + default: + ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), bmin.lane<3>()); + ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), ep1.lane<1>(), ep1.lane<2>(), bmax.lane<3>()); + break; + } + } + + // Zero initialize any SIMD over-fetch + unsigned int texel_count_simd = round_up_to_simd_multiple_vla(texel_count); + for (unsigned int i = texel_count; i < texel_count_simd; i++) + { + ei.weights[i] = 0.0f; + ei.weight_error_scale[i] = 0.0f; + } + + ei.is_constant_weight_error_scale = is_constant_wes; +} + +/** + * @brief Compute the ideal endpoints and weights for 4 color components. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param[out] ei The computed ideal endpoints and weights. + */ +static void compute_ideal_colors_and_weights_4_comp( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei +) { + const float error_weight = hadd_s(blk.channel_weight) / 4.0f; + + unsigned int partition_count = pi.partition_count; + + unsigned int texel_count = blk.texel_count; + promise(texel_count > 0); + promise(partition_count > 0); + + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + + compute_avgs_and_dirs_4_comp(pi, blk, pms); + + bool is_constant_wes { true }; + float partition0_len_sq { 0.0f }; + + for (unsigned int i = 0; i < partition_count; i++) + { + vfloat4 dir = pms[i].dir; + if (hadd_rgb_s(dir) < 0.0f) + { + dir = vfloat4::zero() - dir; + } + + line4 line { pms[i].avg, normalize_safe(dir, unit4()) }; + float lowparam { 1e10f }; + float highparam { -1e10f }; + + unsigned int partition_texel_count = pi.partition_texel_count[i]; + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + vfloat4 point = blk.texel(tix); + float param = dot_s(point - line.a, line.b); + ei.weights[tix] = param; + + lowparam = astc::min(param, lowparam); + highparam = astc::max(param, highparam); + } + + // It is possible for a uniform-color partition to produce length=0; + // this causes NaN issues so set to small value to avoid this problem + if (highparam <= lowparam) + { + lowparam = 0.0f; + highparam = 1e-7f; + } + + float length = highparam - lowparam; + float length_squared = length * length; + float scale = 1.0f / length; + + if (i == 0) + { + partition0_len_sq = length_squared; + } + else + { + is_constant_wes = is_constant_wes && length_squared == partition0_len_sq; + } + + ei.ep.endpt0[i] = line.a + line.b * lowparam; + ei.ep.endpt1[i] = line.a + line.b * highparam; + + for (unsigned int j = 0; j < partition_texel_count; j++) + { + unsigned int tix = pi.texels_of_partition[i][j]; + float idx = (ei.weights[tix] - lowparam) * scale; + idx = astc::clamp1f(idx); + + ei.weights[tix] = idx; + ei.weight_error_scale[tix] = length_squared * error_weight; + assert(!astc::isnan(ei.weight_error_scale[tix])); + } + } + + // Zero initialize any SIMD over-fetch + unsigned int texel_count_simd = round_up_to_simd_multiple_vla(texel_count); + for (unsigned int i = texel_count; i < texel_count_simd; i++) + { + ei.weights[i] = 0.0f; + ei.weight_error_scale[i] = 0.0f; + } + + ei.is_constant_weight_error_scale = is_constant_wes; +} + +/* See header for documentation. */ +void compute_ideal_colors_and_weights_1plane( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei +) { + bool uses_alpha = !blk.is_constant_channel(3); + + if (uses_alpha) + { + compute_ideal_colors_and_weights_4_comp(blk, pi, ei); + } + else + { + compute_ideal_colors_and_weights_3_comp(blk, pi, ei, 3); + } +} + +/* See header for documentation. */ +void compute_ideal_colors_and_weights_2planes( + const block_size_descriptor& bsd, + const image_block& blk, + unsigned int plane2_component, + endpoints_and_weights& ei1, + endpoints_and_weights& ei2 +) { + const auto& pi = bsd.get_partition_info(1, 0); + bool uses_alpha = !blk.is_constant_channel(3); + + assert(plane2_component < BLOCK_MAX_COMPONENTS); + switch (plane2_component) + { + case 0: // Separate weights for red + if (uses_alpha) + { + compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 0); + } + else + { + compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 1, 2); + } + compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 0); + break; + + case 1: // Separate weights for green + if (uses_alpha) + { + compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 1); + } + else + { + compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 0, 2); + } + compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 1); + break; + + case 2: // Separate weights for blue + if (uses_alpha) + { + compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 2); + } + else + { + compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 0, 1); + } + compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 2); + break; + + default: // Separate weights for alpha + assert(uses_alpha); + compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 3); + compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 3); + break; + } +} + +/* See header for documentation. */ +float compute_error_of_weight_set_1plane( + const endpoints_and_weights& eai, + const decimation_info& di, + const float* dec_weight_quant_uvalue +) { + vfloatacc error_summav = vfloatacc::zero(); + unsigned int texel_count = di.texel_count; + + // Process SIMD-width chunks, safe to over-fetch - the extra space is zero initialized + if (di.max_texel_weight_count > 2) + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values = bilinear_infill_vla(di, dec_weight_quant_uvalue, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values = loada(eai.weights + i); + vfloat diff = current_values - actual_values; + vfloat significance = loada(eai.weight_error_scale + i); + vfloat error = diff * diff * significance; + + haccumulate(error_summav, error); + } + } + else if (di.max_texel_weight_count > 1) + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values = bilinear_infill_vla_2(di, dec_weight_quant_uvalue, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values = loada(eai.weights + i); + vfloat diff = current_values - actual_values; + vfloat significance = loada(eai.weight_error_scale + i); + vfloat error = diff * diff * significance; + + haccumulate(error_summav, error); + } + } + else + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Load the weight set directly, without interpolation + vfloat current_values = loada(dec_weight_quant_uvalue + i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values = loada(eai.weights + i); + vfloat diff = current_values - actual_values; + vfloat significance = loada(eai.weight_error_scale + i); + vfloat error = diff * diff * significance; + + haccumulate(error_summav, error); + } + } + + // Resolve the final scalar accumulator sum + return hadd_s(error_summav); +} + +/* See header for documentation. */ +float compute_error_of_weight_set_2planes( + const endpoints_and_weights& eai1, + const endpoints_and_weights& eai2, + const decimation_info& di, + const float* dec_weight_quant_uvalue_plane1, + const float* dec_weight_quant_uvalue_plane2 +) { + vfloatacc error_summav = vfloatacc::zero(); + unsigned int texel_count = di.texel_count; + + // Process SIMD-width chunks, safe to over-fetch - the extra space is zero initialized + if (di.max_texel_weight_count > 2) + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Plane 1 + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values1 = bilinear_infill_vla(di, dec_weight_quant_uvalue_plane1, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values1 = loada(eai1.weights + i); + vfloat diff = current_values1 - actual_values1; + vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i); + + // Plane 2 + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values2 = bilinear_infill_vla(di, dec_weight_quant_uvalue_plane2, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values2 = loada(eai2.weights + i); + diff = current_values2 - actual_values2; + vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i); + + haccumulate(error_summav, error1 + error2); + } + } + else if (di.max_texel_weight_count > 1) + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Plane 1 + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values1 = bilinear_infill_vla_2(di, dec_weight_quant_uvalue_plane1, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values1 = loada(eai1.weights + i); + vfloat diff = current_values1 - actual_values1; + vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i); + + // Plane 2 + // Compute the bilinear interpolation of the decimated weight grid + vfloat current_values2 = bilinear_infill_vla_2(di, dec_weight_quant_uvalue_plane2, i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values2 = loada(eai2.weights + i); + diff = current_values2 - actual_values2; + vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i); + + haccumulate(error_summav, error1 + error2); + } + } + else + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + // Plane 1 + // Load the weight set directly, without interpolation + vfloat current_values1 = loada(dec_weight_quant_uvalue_plane1 + i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values1 = loada(eai1.weights + i); + vfloat diff = current_values1 - actual_values1; + vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i); + + // Plane 2 + // Load the weight set directly, without interpolation + vfloat current_values2 = loada(dec_weight_quant_uvalue_plane2 + i); + + // Compute the error between the computed value and the ideal weight + vfloat actual_values2 = loada(eai2.weights + i); + diff = current_values2 - actual_values2; + vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i); + + haccumulate(error_summav, error1 + error2); + } + } + + // Resolve the final scalar accumulator sum + return hadd_s(error_summav); +} + +/* See header for documentation. */ +void compute_ideal_weights_for_decimation( + const endpoints_and_weights& ei, + const decimation_info& di, + float* dec_weight_ideal_value +) { + unsigned int texel_count = di.texel_count; + unsigned int weight_count = di.weight_count; + bool is_direct = texel_count == weight_count; + promise(texel_count > 0); + promise(weight_count > 0); + + // Ensure that the end of the output arrays that are used for SIMD paths later are filled so we + // can safely run SIMD elsewhere without a loop tail. Note that this is always safe as weight + // arrays always contain space for 64 elements + unsigned int prev_weight_count_simd = round_down_to_simd_multiple_vla(weight_count - 1); + storea(vfloat::zero(), dec_weight_ideal_value + prev_weight_count_simd); + + // If we have a 1:1 mapping just shortcut the computation. Transfer enough to also copy the + // zero-initialized SIMD over-fetch region + if (is_direct) + { + unsigned int texel_count_simd = round_up_to_simd_multiple_vla(texel_count); + for (unsigned int i = 0; i < texel_count_simd; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight(ei.weights + i); + storea(weight, dec_weight_ideal_value + i); + } + + return; + } + + // Otherwise compute an estimate and perform single refinement iteration + alignas(ASTCENC_VECALIGN) float infilled_weights[BLOCK_MAX_TEXELS]; + + // Compute an initial average for each decimated weight + bool constant_wes = ei.is_constant_weight_error_scale; + vfloat weight_error_scale(ei.weight_error_scale[0]); + + // This overshoots - this is OK as we initialize the array tails in the + // decimation table structures to safe values ... + for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + // Start with a small value to avoid div-by-zero later + vfloat weight_weight(1e-10f); + vfloat initial_weight = vfloat::zero(); + + // Accumulate error weighting of all the texels using this weight + vint weight_texel_count(di.weight_texel_count + i); + unsigned int max_texel_count = hmax(weight_texel_count).lane<0>(); + promise(max_texel_count > 0); + + for (unsigned int j = 0; j < max_texel_count; j++) + { + vint texel(di.weight_texel[j] + i); + vfloat weight = loada(di.weights_flt[j] + i); + + if (!constant_wes) + { + weight_error_scale = gatherf(ei.weight_error_scale, texel); + } + + vfloat contrib_weight = weight * weight_error_scale; + + weight_weight += contrib_weight; + initial_weight += gatherf(ei.weights, texel) * contrib_weight; + } + + storea(initial_weight / weight_weight, dec_weight_ideal_value + i); + } + + // Populate the interpolated weight grid based on the initial average + // Process SIMD-width texel coordinates at at time while we can. Safe to + // over-process full SIMD vectors - the tail is zeroed. + if (di.max_texel_weight_count <= 2) + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla_2(di, dec_weight_ideal_value, i); + storea(weight, infilled_weights + i); + } + } + else + { + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla(di, dec_weight_ideal_value, i); + storea(weight, infilled_weights + i); + } + } + + // Perform a single iteration of refinement + // Empirically determined step size; larger values don't help but smaller drops image quality + constexpr float stepsize = 0.25f; + constexpr float chd_scale = -WEIGHTS_TEXEL_SUM; + + for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight_val = loada(dec_weight_ideal_value + i); + + // Accumulate error weighting of all the texels using this weight + // Start with a small value to avoid div-by-zero later + vfloat error_change0(1e-10f); + vfloat error_change1(0.0f); + + // Accumulate error weighting of all the texels using this weight + vint weight_texel_count(di.weight_texel_count + i); + unsigned int max_texel_count = hmax(weight_texel_count).lane<0>(); + promise(max_texel_count > 0); + + for (unsigned int j = 0; j < max_texel_count; j++) + { + vint texel(di.weight_texel[j] + i); + vfloat contrib_weight = loada(di.weights_flt[j] + i); + + if (!constant_wes) + { + weight_error_scale = gatherf(ei.weight_error_scale, texel); + } + + vfloat scale = weight_error_scale * contrib_weight; + vfloat old_weight = gatherf(infilled_weights, texel); + vfloat ideal_weight = gatherf(ei.weights, texel); + + error_change0 += contrib_weight * scale; + error_change1 += (old_weight - ideal_weight) * scale; + } + + vfloat step = (error_change1 * chd_scale) / error_change0; + step = clamp(-stepsize, stepsize, step); + + // Update the weight; note this can store negative values. + storea(weight_val + step, dec_weight_ideal_value + i); + } +} + +/* See header for documentation. */ +void compute_quantized_weights_for_decimation( + const decimation_info& di, + float low_bound, + float high_bound, + const float* dec_weight_ideal_value, + float* weight_set_out, + uint8_t* quantized_weight_set, + quant_method quant_level +) { + int weight_count = di.weight_count; + promise(weight_count > 0); + const quant_and_transfer_table& qat = quant_and_xfer_tables[quant_level]; + + // The available quant levels, stored with a minus 1 bias + static const float quant_levels_m1[12] { + 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 7.0f, 9.0f, 11.0f, 15.0f, 19.0f, 23.0f, 31.0f + }; + + vint steps_m1(get_quant_level(quant_level) - 1); + float quant_level_m1 = quant_levels_m1[quant_level]; + + // Quantize the weight set using both the specified low/high bounds and standard 0..1 bounds + + // TODO: Oddity to investigate; triggered by test in issue #265. + if (high_bound <= low_bound) + { + low_bound = 0.0f; + high_bound = 1.0f; + } + + float rscale = high_bound - low_bound; + float scale = 1.0f / rscale; + + float scaled_low_bound = low_bound * scale; + rscale *= 1.0f / 64.0f; + + vfloat scalev(scale); + vfloat scaled_low_boundv(scaled_low_bound); + vfloat quant_level_m1v(quant_level_m1); + vfloat rscalev(rscale); + vfloat low_boundv(low_bound); + + // This runs to the rounded-up SIMD size, which is safe as the loop tail is filled with known + // safe data in compute_ideal_weights_for_decimation and arrays are always 64 elements + if (get_quant_level(quant_level) <= 16) + { + vint4 tab0(reinterpret_cast<const int*>(qat.quant_to_unquant)); + vint tab0p; + vtable_prepare(tab0, tab0p); + + for (int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat ix = loada(dec_weight_ideal_value + i) * scalev - scaled_low_boundv; + ix = clampzo(ix); + + // Look up the two closest indexes and return the one that was closest + vfloat ix1 = ix * quant_level_m1v; + + vint weightl = float_to_int(ix1); + vint weighth = min(weightl + vint(1), steps_m1); + + vint ixli = vtable_8bt_32bi(tab0p, weightl); + vint ixhi = vtable_8bt_32bi(tab0p, weighth); + + vfloat ixl = int_to_float(ixli); + vfloat ixh = int_to_float(ixhi); + + vmask mask = (ixl + ixh) < (vfloat(128.0f) * ix); + vint weight = select(ixli, ixhi, mask); + ixl = select(ixl, ixh, mask); + + // Invert the weight-scaling that was done initially + storea(ixl * rscalev + low_boundv, weight_set_out + i); + vint scn = pack_low_bytes(weight); + store_nbytes(scn, quantized_weight_set + i); + } + } + else + { + vint4 tab0(reinterpret_cast<const int*>(qat.quant_to_unquant)); + vint4 tab1(reinterpret_cast<const int*>(qat.quant_to_unquant + 16)); + vint tab0p, tab1p; + vtable_prepare(tab0, tab1, tab0p, tab1p); + + for (int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat ix = loada(dec_weight_ideal_value + i) * scalev - scaled_low_boundv; + ix = clampzo(ix); + + // Look up the two closest indexes and return the one that was closest + vfloat ix1 = ix * quant_level_m1v; + + vint weightl = float_to_int(ix1); + vint weighth = min(weightl + vint(1), steps_m1); + + vint ixli = vtable_8bt_32bi(tab0p, tab1p, weightl); + vint ixhi = vtable_8bt_32bi(tab0p, tab1p, weighth); + + vfloat ixl = int_to_float(ixli); + vfloat ixh = int_to_float(ixhi); + + vmask mask = (ixl + ixh) < (vfloat(128.0f) * ix); + vint weight = select(ixli, ixhi, mask); + ixl = select(ixl, ixh, mask); + + // Invert the weight-scaling that was done initially + storea(ixl * rscalev + low_boundv, weight_set_out + i); + vint scn = pack_low_bytes(weight); + store_nbytes(scn, quantized_weight_set + i); + } + } +} + +/** + * @brief Compute the RGB + offset for a HDR endpoint mode #7. + * + * Since the matrix needed has a regular structure we can simplify the inverse calculation. This + * gives us ~24 multiplications vs. 96 for a generic inverse. + * + * mat[0] = vfloat4(rgba_ws.x, 0.0f, 0.0f, wght_ws.x); + * mat[1] = vfloat4( 0.0f, rgba_ws.y, 0.0f, wght_ws.y); + * mat[2] = vfloat4( 0.0f, 0.0f, rgba_ws.z, wght_ws.z); + * mat[3] = vfloat4(wght_ws.x, wght_ws.y, wght_ws.z, psum); + * mat = invert(mat); + * + * @param rgba_weight_sum Sum of partition component error weights. + * @param weight_weight_sum Sum of partition component error weights * texel weight. + * @param rgbq_sum Sum of partition component error weights * texel weight * color data. + * @param psum Sum of RGB color weights * texel weight^2. + */ +static inline vfloat4 compute_rgbo_vector( + vfloat4 rgba_weight_sum, + vfloat4 weight_weight_sum, + vfloat4 rgbq_sum, + float psum +) { + float X = rgba_weight_sum.lane<0>(); + float Y = rgba_weight_sum.lane<1>(); + float Z = rgba_weight_sum.lane<2>(); + float P = weight_weight_sum.lane<0>(); + float Q = weight_weight_sum.lane<1>(); + float R = weight_weight_sum.lane<2>(); + float S = psum; + + float PP = P * P; + float QQ = Q * Q; + float RR = R * R; + + float SZmRR = S * Z - RR; + float DT = SZmRR * Y - Z * QQ; + float YP = Y * P; + float QX = Q * X; + float YX = Y * X; + float mZYP = -Z * YP; + float mZQX = -Z * QX; + float mRYX = -R * YX; + float ZQP = Z * Q * P; + float RYP = R * YP; + float RQX = R * QX; + + // Compute the reciprocal of matrix determinant + float rdet = 1.0f / (DT * X + mZYP * P); + + // Actually compute the adjugate, and then apply 1/det separately + vfloat4 mat0(DT, ZQP, RYP, mZYP); + vfloat4 mat1(ZQP, SZmRR * X - Z * PP, RQX, mZQX); + vfloat4 mat2(RYP, RQX, (S * Y - QQ) * X - Y * PP, mRYX); + vfloat4 mat3(mZYP, mZQX, mRYX, Z * YX); + vfloat4 vect = rgbq_sum * rdet; + + return vfloat4(dot_s(mat0, vect), + dot_s(mat1, vect), + dot_s(mat2, vect), + dot_s(mat3, vect)); +} + +/* See header for documentation. */ +void recompute_ideal_colors_1plane( + const image_block& blk, + const partition_info& pi, + const decimation_info& di, + const uint8_t* dec_weights_uquant, + endpoints& ep, + vfloat4 rgbs_vectors[BLOCK_MAX_PARTITIONS], + vfloat4 rgbo_vectors[BLOCK_MAX_PARTITIONS] +) { + unsigned int weight_count = di.weight_count; + unsigned int total_texel_count = blk.texel_count; + unsigned int partition_count = pi.partition_count; + + promise(weight_count > 0); + promise(total_texel_count > 0); + promise(partition_count > 0); + + alignas(ASTCENC_VECALIGN) float dec_weight[BLOCK_MAX_WEIGHTS]; + for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + vint unquant_value(dec_weights_uquant + i); + vfloat unquant_valuef = int_to_float(unquant_value) * vfloat(1.0f / 64.0f); + storea(unquant_valuef, dec_weight + i); + } + + alignas(ASTCENC_VECALIGN) float undec_weight[BLOCK_MAX_TEXELS]; + float* undec_weight_ref; + if (di.max_texel_weight_count == 1) + { + undec_weight_ref = dec_weight; + } + else if (di.max_texel_weight_count <= 2) + { + for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla_2(di, dec_weight, i); + storea(weight, undec_weight + i); + } + + undec_weight_ref = undec_weight; + } + else + { + for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla(di, dec_weight, i); + storea(weight, undec_weight + i); + } + + undec_weight_ref = undec_weight; + } + + vfloat4 rgba_sum(blk.data_mean * static_cast<float>(blk.texel_count)); + + for (unsigned int i = 0; i < partition_count; i++) + { + unsigned int texel_count = pi.partition_texel_count[i]; + const uint8_t *texel_indexes = pi.texels_of_partition[i]; + + // Only compute a partition mean if more than one partition + if (partition_count > 1) + { + rgba_sum = vfloat4(1e-17f); + promise(texel_count > 0); + for (unsigned int j = 0; j < texel_count; j++) + { + unsigned int tix = texel_indexes[j]; + rgba_sum += blk.texel(tix); + } + } + + rgba_sum = rgba_sum * blk.channel_weight; + vfloat4 rgba_weight_sum = max(blk.channel_weight * static_cast<float>(texel_count), 1e-17f); + vfloat4 scale_dir = normalize((rgba_sum / rgba_weight_sum).swz<0, 1, 2>()); + + float scale_max = 0.0f; + float scale_min = 1e10f; + + float wmin1 = 1.0f; + float wmax1 = 0.0f; + + float left_sum_s = 0.0f; + float middle_sum_s = 0.0f; + float right_sum_s = 0.0f; + + vfloat4 color_vec_x = vfloat4::zero(); + vfloat4 color_vec_y = vfloat4::zero(); + + vfloat4 scale_vec = vfloat4::zero(); + + float weight_weight_sum_s = 1e-17f; + + vfloat4 color_weight = blk.channel_weight; + float ls_weight = hadd_rgb_s(color_weight); + + for (unsigned int j = 0; j < texel_count; j++) + { + unsigned int tix = texel_indexes[j]; + + vfloat4 rgba = blk.texel(tix); + + float idx0 = undec_weight_ref[tix]; + + float om_idx0 = 1.0f - idx0; + wmin1 = astc::min(idx0, wmin1); + wmax1 = astc::max(idx0, wmax1); + + float scale = dot3_s(scale_dir, rgba); + scale_min = astc::min(scale, scale_min); + scale_max = astc::max(scale, scale_max); + + left_sum_s += om_idx0 * om_idx0; + middle_sum_s += om_idx0 * idx0; + right_sum_s += idx0 * idx0; + weight_weight_sum_s += idx0; + + vfloat4 color_idx(idx0); + vfloat4 cwprod = rgba; + vfloat4 cwiprod = cwprod * color_idx; + + color_vec_y += cwiprod; + color_vec_x += cwprod - cwiprod; + + scale_vec += vfloat2(om_idx0, idx0) * (scale * ls_weight); + } + + vfloat4 left_sum = vfloat4(left_sum_s) * color_weight; + vfloat4 middle_sum = vfloat4(middle_sum_s) * color_weight; + vfloat4 right_sum = vfloat4(right_sum_s) * color_weight; + vfloat4 lmrs_sum = vfloat3(left_sum_s, middle_sum_s, right_sum_s) * ls_weight; + + vfloat4 weight_weight_sum = vfloat4(weight_weight_sum_s) * color_weight; + float psum = right_sum_s * hadd_rgb_s(color_weight); + + color_vec_x = color_vec_x * color_weight; + color_vec_y = color_vec_y * color_weight; + + // Initialize the luminance and scale vectors with a reasonable default + float scalediv = scale_min / astc::max(scale_max, 1e-10f); + scalediv = astc::clamp1f(scalediv); + + vfloat4 sds = scale_dir * scale_max; + + rgbs_vectors[i] = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), scalediv); + + if (wmin1 >= wmax1 * 0.999f) + { + // If all weights in the partition were equal, then just take average of all colors in + // the partition and use that as both endpoint colors + vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum; + + vmask4 notnan_mask = avg == avg; + ep.endpt0[i] = select(ep.endpt0[i], avg, notnan_mask); + ep.endpt1[i] = select(ep.endpt1[i], avg, notnan_mask); + + rgbs_vectors[i] = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), 1.0f); + } + else + { + // Otherwise, complete the analytic calculation of ideal-endpoint-values for the given + // set of texel weights and pixel colors + vfloat4 color_det1 = (left_sum * right_sum) - (middle_sum * middle_sum); + vfloat4 color_rdet1 = 1.0f / color_det1; + + float ls_det1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<2>()) - (lmrs_sum.lane<1>() * lmrs_sum.lane<1>()); + float ls_rdet1 = 1.0f / ls_det1; + + vfloat4 color_mss1 = (left_sum * left_sum) + + (2.0f * middle_sum * middle_sum) + + (right_sum * right_sum); + + float ls_mss1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<0>()) + + (2.0f * lmrs_sum.lane<1>() * lmrs_sum.lane<1>()) + + (lmrs_sum.lane<2>() * lmrs_sum.lane<2>()); + + vfloat4 ep0 = (right_sum * color_vec_x - middle_sum * color_vec_y) * color_rdet1; + vfloat4 ep1 = (left_sum * color_vec_y - middle_sum * color_vec_x) * color_rdet1; + + vmask4 det_mask = abs(color_det1) > (color_mss1 * 1e-4f); + vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1); + vmask4 full_mask = det_mask & notnan_mask; + + ep.endpt0[i] = select(ep.endpt0[i], ep0, full_mask); + ep.endpt1[i] = select(ep.endpt1[i], ep1, full_mask); + + float scale_ep0 = (lmrs_sum.lane<2>() * scale_vec.lane<0>() - lmrs_sum.lane<1>() * scale_vec.lane<1>()) * ls_rdet1; + float scale_ep1 = (lmrs_sum.lane<0>() * scale_vec.lane<1>() - lmrs_sum.lane<1>() * scale_vec.lane<0>()) * ls_rdet1; + + if (fabsf(ls_det1) > (ls_mss1 * 1e-4f) && scale_ep0 == scale_ep0 && scale_ep1 == scale_ep1 && scale_ep0 < scale_ep1) + { + float scalediv2 = scale_ep0 / scale_ep1; + vfloat4 sdsm = scale_dir * scale_ep1; + rgbs_vectors[i] = vfloat4(sdsm.lane<0>(), sdsm.lane<1>(), sdsm.lane<2>(), scalediv2); + } + } + + // Calculations specific to mode #7, the HDR RGB-scale mode + vfloat4 rgbq_sum = color_vec_x + color_vec_y; + rgbq_sum.set_lane<3>(hadd_rgb_s(color_vec_y)); + + vfloat4 rgbovec = compute_rgbo_vector(rgba_weight_sum, weight_weight_sum, rgbq_sum, psum); + rgbo_vectors[i] = rgbovec; + + // We can get a failure due to the use of a singular (non-invertible) matrix + // If it failed, compute rgbo_vectors[] with a different method ... + if (astc::isnan(dot_s(rgbovec, rgbovec))) + { + vfloat4 v0 = ep.endpt0[i]; + vfloat4 v1 = ep.endpt1[i]; + + float avgdif = hadd_rgb_s(v1 - v0) * (1.0f / 3.0f); + avgdif = astc::max(avgdif, 0.0f); + + vfloat4 avg = (v0 + v1) * 0.5f; + vfloat4 ep0 = avg - vfloat4(avgdif) * 0.5f; + rgbo_vectors[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), avgdif); + } + } +} + +/* See header for documentation. */ +void recompute_ideal_colors_2planes( + const image_block& blk, + const block_size_descriptor& bsd, + const decimation_info& di, + const uint8_t* dec_weights_uquant_plane1, + const uint8_t* dec_weights_uquant_plane2, + endpoints& ep, + vfloat4& rgbs_vector, + vfloat4& rgbo_vector, + int plane2_component +) { + unsigned int weight_count = di.weight_count; + unsigned int total_texel_count = blk.texel_count; + + promise(total_texel_count > 0); + promise(weight_count > 0); + + alignas(ASTCENC_VECALIGN) float dec_weight_plane1[BLOCK_MAX_WEIGHTS_2PLANE]; + alignas(ASTCENC_VECALIGN) float dec_weight_plane2[BLOCK_MAX_WEIGHTS_2PLANE]; + + assert(weight_count <= BLOCK_MAX_WEIGHTS_2PLANE); + + for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + vint unquant_value1(dec_weights_uquant_plane1 + i); + vfloat unquant_value1f = int_to_float(unquant_value1) * vfloat(1.0f / 64.0f); + storea(unquant_value1f, dec_weight_plane1 + i); + + vint unquant_value2(dec_weights_uquant_plane2 + i); + vfloat unquant_value2f = int_to_float(unquant_value2) * vfloat(1.0f / 64.0f); + storea(unquant_value2f, dec_weight_plane2 + i); + } + + alignas(ASTCENC_VECALIGN) float undec_weight_plane1[BLOCK_MAX_TEXELS]; + alignas(ASTCENC_VECALIGN) float undec_weight_plane2[BLOCK_MAX_TEXELS]; + + float* undec_weight_plane1_ref; + float* undec_weight_plane2_ref; + + if (di.max_texel_weight_count == 1) + { + undec_weight_plane1_ref = dec_weight_plane1; + undec_weight_plane2_ref = dec_weight_plane2; + } + else if (di.max_texel_weight_count <= 2) + { + for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla_2(di, dec_weight_plane1, i); + storea(weight, undec_weight_plane1 + i); + + weight = bilinear_infill_vla_2(di, dec_weight_plane2, i); + storea(weight, undec_weight_plane2 + i); + } + + undec_weight_plane1_ref = undec_weight_plane1; + undec_weight_plane2_ref = undec_weight_plane2; + } + else + { + for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH) + { + vfloat weight = bilinear_infill_vla(di, dec_weight_plane1, i); + storea(weight, undec_weight_plane1 + i); + + weight = bilinear_infill_vla(di, dec_weight_plane2, i); + storea(weight, undec_weight_plane2 + i); + } + + undec_weight_plane1_ref = undec_weight_plane1; + undec_weight_plane2_ref = undec_weight_plane2; + } + + unsigned int texel_count = bsd.texel_count; + vfloat4 rgba_weight_sum = max(blk.channel_weight * static_cast<float>(texel_count), 1e-17f); + vfloat4 scale_dir = normalize(blk.data_mean.swz<0, 1, 2>()); + + float scale_max = 0.0f; + float scale_min = 1e10f; + + float wmin1 = 1.0f; + float wmax1 = 0.0f; + + float wmin2 = 1.0f; + float wmax2 = 0.0f; + + float left1_sum_s = 0.0f; + float middle1_sum_s = 0.0f; + float right1_sum_s = 0.0f; + + float left2_sum_s = 0.0f; + float middle2_sum_s = 0.0f; + float right2_sum_s = 0.0f; + + vfloat4 color_vec_x = vfloat4::zero(); + vfloat4 color_vec_y = vfloat4::zero(); + + vfloat4 scale_vec = vfloat4::zero(); + + vfloat4 weight_weight_sum = vfloat4(1e-17f); + + vmask4 p2_mask = vint4::lane_id() == vint4(plane2_component); + vfloat4 color_weight = blk.channel_weight; + float ls_weight = hadd_rgb_s(color_weight); + + for (unsigned int j = 0; j < texel_count; j++) + { + vfloat4 rgba = blk.texel(j); + + float idx0 = undec_weight_plane1_ref[j]; + + float om_idx0 = 1.0f - idx0; + wmin1 = astc::min(idx0, wmin1); + wmax1 = astc::max(idx0, wmax1); + + float scale = dot3_s(scale_dir, rgba); + scale_min = astc::min(scale, scale_min); + scale_max = astc::max(scale, scale_max); + + left1_sum_s += om_idx0 * om_idx0; + middle1_sum_s += om_idx0 * idx0; + right1_sum_s += idx0 * idx0; + + float idx1 = undec_weight_plane2_ref[j]; + + float om_idx1 = 1.0f - idx1; + wmin2 = astc::min(idx1, wmin2); + wmax2 = astc::max(idx1, wmax2); + + left2_sum_s += om_idx1 * om_idx1; + middle2_sum_s += om_idx1 * idx1; + right2_sum_s += idx1 * idx1; + + vfloat4 color_idx = select(vfloat4(idx0), vfloat4(idx1), p2_mask); + + vfloat4 cwprod = rgba; + vfloat4 cwiprod = cwprod * color_idx; + + color_vec_y += cwiprod; + color_vec_x += cwprod - cwiprod; + + scale_vec += vfloat2(om_idx0, idx0) * (ls_weight * scale); + weight_weight_sum += (color_weight * color_idx); + } + + vfloat4 left1_sum = vfloat4(left1_sum_s) * color_weight; + vfloat4 middle1_sum = vfloat4(middle1_sum_s) * color_weight; + vfloat4 right1_sum = vfloat4(right1_sum_s) * color_weight; + vfloat4 lmrs_sum = vfloat3(left1_sum_s, middle1_sum_s, right1_sum_s) * ls_weight; + + vfloat4 left2_sum = vfloat4(left2_sum_s) * color_weight; + vfloat4 middle2_sum = vfloat4(middle2_sum_s) * color_weight; + vfloat4 right2_sum = vfloat4(right2_sum_s) * color_weight; + + float psum = dot3_s(select(right1_sum, right2_sum, p2_mask), color_weight); + + color_vec_x = color_vec_x * color_weight; + color_vec_y = color_vec_y * color_weight; + + // Initialize the luminance and scale vectors with a reasonable default + float scalediv = scale_min / astc::max(scale_max, 1e-10f); + scalediv = astc::clamp1f(scalediv); + + vfloat4 sds = scale_dir * scale_max; + + rgbs_vector = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), scalediv); + + if (wmin1 >= wmax1 * 0.999f) + { + // If all weights in the partition were equal, then just take average of all colors in + // the partition and use that as both endpoint colors + vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum; + + vmask4 p1_mask = vint4::lane_id() != vint4(plane2_component); + vmask4 notnan_mask = avg == avg; + vmask4 full_mask = p1_mask & notnan_mask; + + ep.endpt0[0] = select(ep.endpt0[0], avg, full_mask); + ep.endpt1[0] = select(ep.endpt1[0], avg, full_mask); + + rgbs_vector = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), 1.0f); + } + else + { + // Otherwise, complete the analytic calculation of ideal-endpoint-values for the given + // set of texel weights and pixel colors + vfloat4 color_det1 = (left1_sum * right1_sum) - (middle1_sum * middle1_sum); + vfloat4 color_rdet1 = 1.0f / color_det1; + + float ls_det1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<2>()) - (lmrs_sum.lane<1>() * lmrs_sum.lane<1>()); + float ls_rdet1 = 1.0f / ls_det1; + + vfloat4 color_mss1 = (left1_sum * left1_sum) + + (2.0f * middle1_sum * middle1_sum) + + (right1_sum * right1_sum); + + float ls_mss1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<0>()) + + (2.0f * lmrs_sum.lane<1>() * lmrs_sum.lane<1>()) + + (lmrs_sum.lane<2>() * lmrs_sum.lane<2>()); + + vfloat4 ep0 = (right1_sum * color_vec_x - middle1_sum * color_vec_y) * color_rdet1; + vfloat4 ep1 = (left1_sum * color_vec_y - middle1_sum * color_vec_x) * color_rdet1; + + float scale_ep0 = (lmrs_sum.lane<2>() * scale_vec.lane<0>() - lmrs_sum.lane<1>() * scale_vec.lane<1>()) * ls_rdet1; + float scale_ep1 = (lmrs_sum.lane<0>() * scale_vec.lane<1>() - lmrs_sum.lane<1>() * scale_vec.lane<0>()) * ls_rdet1; + + vmask4 p1_mask = vint4::lane_id() != vint4(plane2_component); + vmask4 det_mask = abs(color_det1) > (color_mss1 * 1e-4f); + vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1); + vmask4 full_mask = p1_mask & det_mask & notnan_mask; + + ep.endpt0[0] = select(ep.endpt0[0], ep0, full_mask); + ep.endpt1[0] = select(ep.endpt1[0], ep1, full_mask); + + if (fabsf(ls_det1) > (ls_mss1 * 1e-4f) && scale_ep0 == scale_ep0 && scale_ep1 == scale_ep1 && scale_ep0 < scale_ep1) + { + float scalediv2 = scale_ep0 / scale_ep1; + vfloat4 sdsm = scale_dir * scale_ep1; + rgbs_vector = vfloat4(sdsm.lane<0>(), sdsm.lane<1>(), sdsm.lane<2>(), scalediv2); + } + } + + if (wmin2 >= wmax2 * 0.999f) + { + // If all weights in the partition were equal, then just take average of all colors in + // the partition and use that as both endpoint colors + vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum; + + vmask4 notnan_mask = avg == avg; + vmask4 full_mask = p2_mask & notnan_mask; + + ep.endpt0[0] = select(ep.endpt0[0], avg, full_mask); + ep.endpt1[0] = select(ep.endpt1[0], avg, full_mask); + } + else + { + // Otherwise, complete the analytic calculation of ideal-endpoint-values for the given + // set of texel weights and pixel colors + vfloat4 color_det2 = (left2_sum * right2_sum) - (middle2_sum * middle2_sum); + vfloat4 color_rdet2 = 1.0f / color_det2; + + vfloat4 color_mss2 = (left2_sum * left2_sum) + + (2.0f * middle2_sum * middle2_sum) + + (right2_sum * right2_sum); + + vfloat4 ep0 = (right2_sum * color_vec_x - middle2_sum * color_vec_y) * color_rdet2; + vfloat4 ep1 = (left2_sum * color_vec_y - middle2_sum * color_vec_x) * color_rdet2; + + vmask4 det_mask = abs(color_det2) > (color_mss2 * 1e-4f); + vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1); + vmask4 full_mask = p2_mask & det_mask & notnan_mask; + + ep.endpt0[0] = select(ep.endpt0[0], ep0, full_mask); + ep.endpt1[0] = select(ep.endpt1[0], ep1, full_mask); + } + + // Calculations specific to mode #7, the HDR RGB-scale mode + vfloat4 rgbq_sum = color_vec_x + color_vec_y; + rgbq_sum.set_lane<3>(hadd_rgb_s(color_vec_y)); + + rgbo_vector = compute_rgbo_vector(rgba_weight_sum, weight_weight_sum, rgbq_sum, psum); + + // We can get a failure due to the use of a singular (non-invertible) matrix + // If it failed, compute rgbo_vectors[] with a different method ... + if (astc::isnan(dot_s(rgbo_vector, rgbo_vector))) + { + vfloat4 v0 = ep.endpt0[0]; + vfloat4 v1 = ep.endpt1[0]; + + float avgdif = hadd_rgb_s(v1 - v0) * (1.0f / 3.0f); + avgdif = astc::max(avgdif, 0.0f); + + vfloat4 avg = (v0 + v1) * 0.5f; + vfloat4 ep0 = avg - vfloat4(avgdif) * 0.5f; + + rgbo_vector = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), avgdif); + } +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_image.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_image.cpp new file mode 100644 index 00000000000..9c0d6727d01 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_image.cpp @@ -0,0 +1,558 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for creating in-memory ASTC image structures. + */ + +#include <cassert> +#include <cstring> + +#include "astcenc_internal.h" + +/** + * @brief Loader pipeline function type for data fetch from memory. + */ +using pixel_loader = vfloat4(*)(const void*, int); + +/** + * @brief Loader pipeline function type for swizzling data in a vector. + */ +using pixel_swizzler = vfloat4(*)(vfloat4, const astcenc_swizzle&); + +/** + * @brief Loader pipeline function type for converting data in a vector to LNS. + */ +using pixel_converter = vfloat4(*)(vfloat4, vmask4); + +/** + * @brief Load a 8-bit UNORM texel from a data array. + * + * @param data The data pointer. + * @param base_offset The index offset to the start of the pixel. + */ +static vfloat4 load_texel_u8( + const void* data, + int base_offset +) { + const uint8_t* data8 = static_cast<const uint8_t*>(data); + return int_to_float(vint4(data8 + base_offset)) / 255.0f; +} + +/** + * @brief Load a 16-bit fp16 texel from a data array. + * + * @param data The data pointer. + * @param base_offset The index offset to the start of the pixel. + */ +static vfloat4 load_texel_f16( + const void* data, + int base_offset +) { + const uint16_t* data16 = static_cast<const uint16_t*>(data); + int r = data16[base_offset ]; + int g = data16[base_offset + 1]; + int b = data16[base_offset + 2]; + int a = data16[base_offset + 3]; + return float16_to_float(vint4(r, g, b, a)); +} + +/** + * @brief Load a 32-bit float texel from a data array. + * + * @param data The data pointer. + * @param base_offset The index offset to the start of the pixel. + */ +static vfloat4 load_texel_f32( + const void* data, + int base_offset +) { + const float* data32 = static_cast<const float*>(data); + return vfloat4(data32 + base_offset); +} + +/** + * @brief Dummy no-op swizzle function. + * + * @param data The source RGBA vector to swizzle. + * @param swz The swizzle to use. + */ +static vfloat4 swz_texel_skip( + vfloat4 data, + const astcenc_swizzle& swz +) { + (void)swz; + return data; +} + +/** + * @brief Swizzle a texel into a new arrangement. + * + * @param data The source RGBA vector to swizzle. + * @param swz The swizzle to use. + */ +static vfloat4 swz_texel( + vfloat4 data, + const astcenc_swizzle& swz +) { + alignas(16) float datas[6]; + + storea(data, datas); + datas[ASTCENC_SWZ_0] = 0.0f; + datas[ASTCENC_SWZ_1] = 1.0f; + + return vfloat4(datas[swz.r], datas[swz.g], datas[swz.b], datas[swz.a]); +} + +/** + * @brief Encode a texel that is entirely LDR linear. + * + * @param data The RGBA data to encode. + * @param lns_mask The mask for the HDR channels than need LNS encoding. + */ +static vfloat4 encode_texel_unorm( + vfloat4 data, + vmask4 lns_mask +) { + (void)lns_mask; + return data * 65535.0f; +} + +/** + * @brief Encode a texel that includes at least some HDR LNS texels. + * + * @param data The RGBA data to encode. + * @param lns_mask The mask for the HDR channels than need LNS encoding. + */ +static vfloat4 encode_texel_lns( + vfloat4 data, + vmask4 lns_mask +) { + vfloat4 datav_unorm = data * 65535.0f; + vfloat4 datav_lns = float_to_lns(data); + return select(datav_unorm, datav_lns, lns_mask); +} + +/* See header for documentation. */ +void load_image_block( + astcenc_profile decode_mode, + const astcenc_image& img, + image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz +) { + unsigned int xsize = img.dim_x; + unsigned int ysize = img.dim_y; + unsigned int zsize = img.dim_z; + + blk.xpos = xpos; + blk.ypos = ypos; + blk.zpos = zpos; + + // True if any non-identity swizzle + bool needs_swz = (swz.r != ASTCENC_SWZ_R) || (swz.g != ASTCENC_SWZ_G) || + (swz.b != ASTCENC_SWZ_B) || (swz.a != ASTCENC_SWZ_A); + + int idx = 0; + + vfloat4 data_min(1e38f); + vfloat4 data_mean(0.0f); + vfloat4 data_mean_scale(1.0f / static_cast<float>(bsd.texel_count)); + vfloat4 data_max(-1e38f); + vmask4 grayscalev(true); + + // This works because we impose the same choice everywhere during encode + uint8_t rgb_lns = (decode_mode == ASTCENC_PRF_HDR) || + (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A) ? 1 : 0; + uint8_t a_lns = decode_mode == ASTCENC_PRF_HDR ? 1 : 0; + vint4 use_lns(rgb_lns, rgb_lns, rgb_lns, a_lns); + vmask4 lns_mask = use_lns != vint4::zero(); + + // Set up the function pointers for loading pipeline as needed + pixel_loader loader = load_texel_u8; + if (img.data_type == ASTCENC_TYPE_F16) + { + loader = load_texel_f16; + } + else if (img.data_type == ASTCENC_TYPE_F32) + { + loader = load_texel_f32; + } + + pixel_swizzler swizzler = swz_texel_skip; + if (needs_swz) + { + swizzler = swz_texel; + } + + pixel_converter converter = encode_texel_unorm; + if (any(lns_mask)) + { + converter = encode_texel_lns; + } + + for (unsigned int z = 0; z < bsd.zdim; z++) + { + unsigned int zi = astc::min(zpos + z, zsize - 1); + void* plane = img.data[zi]; + + for (unsigned int y = 0; y < bsd.ydim; y++) + { + unsigned int yi = astc::min(ypos + y, ysize - 1); + + for (unsigned int x = 0; x < bsd.xdim; x++) + { + unsigned int xi = astc::min(xpos + x, xsize - 1); + + vfloat4 datav = loader(plane, (4 * xsize * yi) + (4 * xi)); + datav = swizzler(datav, swz); + datav = converter(datav, lns_mask); + + // Compute block metadata + data_min = min(data_min, datav); + data_mean += datav * data_mean_scale; + data_max = max(data_max, datav); + + grayscalev = grayscalev & (datav.swz<0,0,0,0>() == datav.swz<1,1,2,2>()); + + blk.data_r[idx] = datav.lane<0>(); + blk.data_g[idx] = datav.lane<1>(); + blk.data_b[idx] = datav.lane<2>(); + blk.data_a[idx] = datav.lane<3>(); + + blk.rgb_lns[idx] = rgb_lns; + blk.alpha_lns[idx] = a_lns; + + idx++; + } + } + } + + // Reverse the encoding so we store origin block in the original format + vfloat4 data_enc = blk.texel(0); + vfloat4 data_enc_unorm = data_enc / 65535.0f; + vfloat4 data_enc_lns = vfloat4::zero(); + + if (rgb_lns || a_lns) + { + data_enc_lns = float16_to_float(lns_to_sf16(float_to_int(data_enc))); + } + + blk.origin_texel = select(data_enc_unorm, data_enc_lns, lns_mask); + + // Store block metadata + blk.data_min = data_min; + blk.data_mean = data_mean; + blk.data_max = data_max; + blk.grayscale = all(grayscalev); +} + +/* See header for documentation. */ +void load_image_block_fast_ldr( + astcenc_profile decode_mode, + const astcenc_image& img, + image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz +) { + (void)swz; + (void)decode_mode; + + unsigned int xsize = img.dim_x; + unsigned int ysize = img.dim_y; + + blk.xpos = xpos; + blk.ypos = ypos; + blk.zpos = zpos; + + vfloat4 data_min(1e38f); + vfloat4 data_mean = vfloat4::zero(); + vfloat4 data_max(-1e38f); + vmask4 grayscalev(true); + int idx = 0; + + const uint8_t* plane = static_cast<const uint8_t*>(img.data[0]); + for (unsigned int y = ypos; y < ypos + bsd.ydim; y++) + { + unsigned int yi = astc::min(y, ysize - 1); + + for (unsigned int x = xpos; x < xpos + bsd.xdim; x++) + { + unsigned int xi = astc::min(x, xsize - 1); + + vint4 datavi = vint4(plane + (4 * xsize * yi) + (4 * xi)); + vfloat4 datav = int_to_float(datavi) * (65535.0f / 255.0f); + + // Compute block metadata + data_min = min(data_min, datav); + data_mean += datav; + data_max = max(data_max, datav); + + grayscalev = grayscalev & (datav.swz<0,0,0,0>() == datav.swz<1,1,2,2>()); + + blk.data_r[idx] = datav.lane<0>(); + blk.data_g[idx] = datav.lane<1>(); + blk.data_b[idx] = datav.lane<2>(); + blk.data_a[idx] = datav.lane<3>(); + + idx++; + } + } + + // Reverse the encoding so we store origin block in the original format + blk.origin_texel = blk.texel(0) / 65535.0f; + + // Store block metadata + blk.rgb_lns[0] = 0; + blk.alpha_lns[0] = 0; + blk.data_min = data_min; + blk.data_mean = data_mean / static_cast<float>(bsd.texel_count); + blk.data_max = data_max; + blk.grayscale = all(grayscalev); +} + +/* See header for documentation. */ +void store_image_block( + astcenc_image& img, + const image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz +) { + unsigned int x_size = img.dim_x; + unsigned int x_start = xpos; + unsigned int x_end = astc::min(x_size, xpos + bsd.xdim); + unsigned int x_count = x_end - x_start; + unsigned int x_nudge = bsd.xdim - x_count; + + unsigned int y_size = img.dim_y; + unsigned int y_start = ypos; + unsigned int y_end = astc::min(y_size, ypos + bsd.ydim); + unsigned int y_count = y_end - y_start; + unsigned int y_nudge = (bsd.ydim - y_count) * bsd.xdim; + + unsigned int z_size = img.dim_z; + unsigned int z_start = zpos; + unsigned int z_end = astc::min(z_size, zpos + bsd.zdim); + + // True if any non-identity swizzle + bool needs_swz = (swz.r != ASTCENC_SWZ_R) || (swz.g != ASTCENC_SWZ_G) || + (swz.b != ASTCENC_SWZ_B) || (swz.a != ASTCENC_SWZ_A); + + // True if any swizzle uses Z reconstruct + bool needs_z = (swz.r == ASTCENC_SWZ_Z) || (swz.g == ASTCENC_SWZ_Z) || + (swz.b == ASTCENC_SWZ_Z) || (swz.a == ASTCENC_SWZ_Z); + + int idx = 0; + if (img.data_type == ASTCENC_TYPE_U8) + { + for (unsigned int z = z_start; z < z_end; z++) + { + // Fetch the image plane + uint8_t* data8 = static_cast<uint8_t*>(img.data[z]); + + for (unsigned int y = y_start; y < y_end; y++) + { + uint8_t* data8_row = data8 + (4 * x_size * y) + (4 * x_start); + + for (unsigned int x = 0; x < x_count; x += ASTCENC_SIMD_WIDTH) + { + unsigned int max_texels = ASTCENC_SIMD_WIDTH; + unsigned int used_texels = astc::min(x_count - x, max_texels); + + // Unaligned load as rows are not always SIMD_WIDTH long + vfloat data_r(blk.data_r + idx); + vfloat data_g(blk.data_g + idx); + vfloat data_b(blk.data_b + idx); + vfloat data_a(blk.data_a + idx); + + vint data_ri = float_to_int_rtn(min(data_r, 1.0f) * 255.0f); + vint data_gi = float_to_int_rtn(min(data_g, 1.0f) * 255.0f); + vint data_bi = float_to_int_rtn(min(data_b, 1.0f) * 255.0f); + vint data_ai = float_to_int_rtn(min(data_a, 1.0f) * 255.0f); + + if (needs_swz) + { + vint swizzle_table[7]; + swizzle_table[ASTCENC_SWZ_0] = vint(0); + swizzle_table[ASTCENC_SWZ_1] = vint(255); + swizzle_table[ASTCENC_SWZ_R] = data_ri; + swizzle_table[ASTCENC_SWZ_G] = data_gi; + swizzle_table[ASTCENC_SWZ_B] = data_bi; + swizzle_table[ASTCENC_SWZ_A] = data_ai; + + if (needs_z) + { + vfloat data_x = (data_r * vfloat(2.0f)) - vfloat(1.0f); + vfloat data_y = (data_a * vfloat(2.0f)) - vfloat(1.0f); + vfloat data_z = vfloat(1.0f) - (data_x * data_x) - (data_y * data_y); + data_z = max(data_z, 0.0f); + data_z = (sqrt(data_z) * vfloat(0.5f)) + vfloat(0.5f); + + swizzle_table[ASTCENC_SWZ_Z] = float_to_int_rtn(min(data_z, 1.0f) * 255.0f); + } + + data_ri = swizzle_table[swz.r]; + data_gi = swizzle_table[swz.g]; + data_bi = swizzle_table[swz.b]; + data_ai = swizzle_table[swz.a]; + } + + // Errors are NaN encoded - convert to magenta error color + // Branch is OK here - it is almost never true so predicts well + vmask nan_mask = data_r != data_r; + if (any(nan_mask)) + { + data_ri = select(data_ri, vint(0xFF), nan_mask); + data_gi = select(data_gi, vint(0x00), nan_mask); + data_bi = select(data_bi, vint(0xFF), nan_mask); + data_ai = select(data_ai, vint(0xFF), nan_mask); + } + + vint data_rgbai = interleave_rgba8(data_ri, data_gi, data_bi, data_ai); + vmask store_mask = vint::lane_id() < vint(used_texels); + store_lanes_masked(reinterpret_cast<int*>(data8_row), data_rgbai, store_mask); + + data8_row += ASTCENC_SIMD_WIDTH * 4; + idx += used_texels; + } + idx += x_nudge; + } + idx += y_nudge; + } + } + else if (img.data_type == ASTCENC_TYPE_F16) + { + for (unsigned int z = z_start; z < z_end; z++) + { + // Fetch the image plane + uint16_t* data16 = static_cast<uint16_t*>(img.data[z]); + + for (unsigned int y = y_start; y < y_end; y++) + { + uint16_t* data16_row = data16 + (4 * x_size * y) + (4 * x_start); + + for (unsigned int x = 0; x < x_count; x++) + { + vint4 color; + + // NaNs are handled inline - no need to special case + if (needs_swz) + { + float data[7]; + data[ASTCENC_SWZ_0] = 0.0f; + data[ASTCENC_SWZ_1] = 1.0f; + data[ASTCENC_SWZ_R] = blk.data_r[idx]; + data[ASTCENC_SWZ_G] = blk.data_g[idx]; + data[ASTCENC_SWZ_B] = blk.data_b[idx]; + data[ASTCENC_SWZ_A] = blk.data_a[idx]; + + if (needs_z) + { + float xN = (data[0] * 2.0f) - 1.0f; + float yN = (data[3] * 2.0f) - 1.0f; + float zN = 1.0f - xN * xN - yN * yN; + if (zN < 0.0f) + { + zN = 0.0f; + } + data[ASTCENC_SWZ_Z] = (astc::sqrt(zN) * 0.5f) + 0.5f; + } + + vfloat4 colorf(data[swz.r], data[swz.g], data[swz.b], data[swz.a]); + color = float_to_float16(colorf); + } + else + { + vfloat4 colorf = blk.texel(idx); + color = float_to_float16(colorf); + } + + // TODO: Vectorize with store N shorts? + data16_row[0] = static_cast<uint16_t>(color.lane<0>()); + data16_row[1] = static_cast<uint16_t>(color.lane<1>()); + data16_row[2] = static_cast<uint16_t>(color.lane<2>()); + data16_row[3] = static_cast<uint16_t>(color.lane<3>()); + data16_row += 4; + idx++; + } + idx += x_nudge; + } + idx += y_nudge; + } + } + else // if (img.data_type == ASTCENC_TYPE_F32) + { + assert(img.data_type == ASTCENC_TYPE_F32); + + for (unsigned int z = z_start; z < z_end; z++) + { + // Fetch the image plane + float* data32 = static_cast<float*>(img.data[z]); + + for (unsigned int y = y_start; y < y_end; y++) + { + float* data32_row = data32 + (4 * x_size * y) + (4 * x_start); + + for (unsigned int x = 0; x < x_count; x++) + { + vfloat4 color = blk.texel(idx); + + // NaNs are handled inline - no need to special case + if (needs_swz) + { + float data[7]; + data[ASTCENC_SWZ_0] = 0.0f; + data[ASTCENC_SWZ_1] = 1.0f; + data[ASTCENC_SWZ_R] = color.lane<0>(); + data[ASTCENC_SWZ_G] = color.lane<1>(); + data[ASTCENC_SWZ_B] = color.lane<2>(); + data[ASTCENC_SWZ_A] = color.lane<3>(); + + if (needs_z) + { + float xN = (data[0] * 2.0f) - 1.0f; + float yN = (data[3] * 2.0f) - 1.0f; + float zN = 1.0f - xN * xN - yN * yN; + if (zN < 0.0f) + { + zN = 0.0f; + } + data[ASTCENC_SWZ_Z] = (astc::sqrt(zN) * 0.5f) + 0.5f; + } + + color = vfloat4(data[swz.r], data[swz.g], data[swz.b], data[swz.a]); + } + + store(color, data32_row); + data32_row += 4; + idx++; + } + idx += x_nudge; + } + idx += y_nudge; + } + } +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_integer_sequence.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_integer_sequence.cpp new file mode 100644 index 00000000000..9675ab5b29f --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_integer_sequence.cpp @@ -0,0 +1,744 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for encoding/decoding Bounded Integer Sequence Encoding. + */ + +#include "astcenc_internal.h" + +#include <array> + +/** @brief Unpacked quint triplets <low,middle,high> for each packed value */ +static const uint8_t quints_of_integer[128][3] { + {0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, + {4, 0, 0}, {0, 4, 0}, {4, 4, 0}, {4, 4, 4}, + {0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, + {4, 1, 0}, {1, 4, 0}, {4, 4, 1}, {4, 4, 4}, + {0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, + {4, 2, 0}, {2, 4, 0}, {4, 4, 2}, {4, 4, 4}, + {0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, + {4, 3, 0}, {3, 4, 0}, {4, 4, 3}, {4, 4, 4}, + {0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {3, 0, 1}, + {4, 0, 1}, {0, 4, 1}, {4, 0, 4}, {0, 4, 4}, + {0, 1, 1}, {1, 1, 1}, {2, 1, 1}, {3, 1, 1}, + {4, 1, 1}, {1, 4, 1}, {4, 1, 4}, {1, 4, 4}, + {0, 2, 1}, {1, 2, 1}, {2, 2, 1}, {3, 2, 1}, + {4, 2, 1}, {2, 4, 1}, {4, 2, 4}, {2, 4, 4}, + {0, 3, 1}, {1, 3, 1}, {2, 3, 1}, {3, 3, 1}, + {4, 3, 1}, {3, 4, 1}, {4, 3, 4}, {3, 4, 4}, + {0, 0, 2}, {1, 0, 2}, {2, 0, 2}, {3, 0, 2}, + {4, 0, 2}, {0, 4, 2}, {2, 0, 4}, {3, 0, 4}, + {0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {3, 1, 2}, + {4, 1, 2}, {1, 4, 2}, {2, 1, 4}, {3, 1, 4}, + {0, 2, 2}, {1, 2, 2}, {2, 2, 2}, {3, 2, 2}, + {4, 2, 2}, {2, 4, 2}, {2, 2, 4}, {3, 2, 4}, + {0, 3, 2}, {1, 3, 2}, {2, 3, 2}, {3, 3, 2}, + {4, 3, 2}, {3, 4, 2}, {2, 3, 4}, {3, 3, 4}, + {0, 0, 3}, {1, 0, 3}, {2, 0, 3}, {3, 0, 3}, + {4, 0, 3}, {0, 4, 3}, {0, 0, 4}, {1, 0, 4}, + {0, 1, 3}, {1, 1, 3}, {2, 1, 3}, {3, 1, 3}, + {4, 1, 3}, {1, 4, 3}, {0, 1, 4}, {1, 1, 4}, + {0, 2, 3}, {1, 2, 3}, {2, 2, 3}, {3, 2, 3}, + {4, 2, 3}, {2, 4, 3}, {0, 2, 4}, {1, 2, 4}, + {0, 3, 3}, {1, 3, 3}, {2, 3, 3}, {3, 3, 3}, + {4, 3, 3}, {3, 4, 3}, {0, 3, 4}, {1, 3, 4} +}; + +/** @brief Packed quint values for each unpacked value, indexed [hi][mid][lo]. */ +static const uint8_t integer_of_quints[5][5][5] { + { + {0, 1, 2, 3, 4}, + {8, 9, 10, 11, 12}, + {16, 17, 18, 19, 20}, + {24, 25, 26, 27, 28}, + {5, 13, 21, 29, 6} + }, + { + {32, 33, 34, 35, 36}, + {40, 41, 42, 43, 44}, + {48, 49, 50, 51, 52}, + {56, 57, 58, 59, 60}, + {37, 45, 53, 61, 14} + }, + { + {64, 65, 66, 67, 68}, + {72, 73, 74, 75, 76}, + {80, 81, 82, 83, 84}, + {88, 89, 90, 91, 92}, + {69, 77, 85, 93, 22} + }, + { + {96, 97, 98, 99, 100}, + {104, 105, 106, 107, 108}, + {112, 113, 114, 115, 116}, + {120, 121, 122, 123, 124}, + {101, 109, 117, 125, 30} + }, + { + {102, 103, 70, 71, 38}, + {110, 111, 78, 79, 46}, + {118, 119, 86, 87, 54}, + {126, 127, 94, 95, 62}, + {39, 47, 55, 63, 31} + } +}; + +/** @brief Unpacked trit quintuplets <low,...,high> for each packed value */ +static const uint8_t trits_of_integer[256][5] { + {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {2, 0, 0, 0, 0}, {0, 0, 2, 0, 0}, + {0, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {2, 1, 0, 0, 0}, {1, 0, 2, 0, 0}, + {0, 2, 0, 0, 0}, {1, 2, 0, 0, 0}, {2, 2, 0, 0, 0}, {2, 0, 2, 0, 0}, + {0, 2, 2, 0, 0}, {1, 2, 2, 0, 0}, {2, 2, 2, 0, 0}, {2, 0, 2, 0, 0}, + {0, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {2, 0, 1, 0, 0}, {0, 1, 2, 0, 0}, + {0, 1, 1, 0, 0}, {1, 1, 1, 0, 0}, {2, 1, 1, 0, 0}, {1, 1, 2, 0, 0}, + {0, 2, 1, 0, 0}, {1, 2, 1, 0, 0}, {2, 2, 1, 0, 0}, {2, 1, 2, 0, 0}, + {0, 0, 0, 2, 2}, {1, 0, 0, 2, 2}, {2, 0, 0, 2, 2}, {0, 0, 2, 2, 2}, + {0, 0, 0, 1, 0}, {1, 0, 0, 1, 0}, {2, 0, 0, 1, 0}, {0, 0, 2, 1, 0}, + {0, 1, 0, 1, 0}, {1, 1, 0, 1, 0}, {2, 1, 0, 1, 0}, {1, 0, 2, 1, 0}, + {0, 2, 0, 1, 0}, {1, 2, 0, 1, 0}, {2, 2, 0, 1, 0}, {2, 0, 2, 1, 0}, + {0, 2, 2, 1, 0}, {1, 2, 2, 1, 0}, {2, 2, 2, 1, 0}, {2, 0, 2, 1, 0}, + {0, 0, 1, 1, 0}, {1, 0, 1, 1, 0}, {2, 0, 1, 1, 0}, {0, 1, 2, 1, 0}, + {0, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {2, 1, 1, 1, 0}, {1, 1, 2, 1, 0}, + {0, 2, 1, 1, 0}, {1, 2, 1, 1, 0}, {2, 2, 1, 1, 0}, {2, 1, 2, 1, 0}, + {0, 1, 0, 2, 2}, {1, 1, 0, 2, 2}, {2, 1, 0, 2, 2}, {1, 0, 2, 2, 2}, + {0, 0, 0, 2, 0}, {1, 0, 0, 2, 0}, {2, 0, 0, 2, 0}, {0, 0, 2, 2, 0}, + {0, 1, 0, 2, 0}, {1, 1, 0, 2, 0}, {2, 1, 0, 2, 0}, {1, 0, 2, 2, 0}, + {0, 2, 0, 2, 0}, {1, 2, 0, 2, 0}, {2, 2, 0, 2, 0}, {2, 0, 2, 2, 0}, + {0, 2, 2, 2, 0}, {1, 2, 2, 2, 0}, {2, 2, 2, 2, 0}, {2, 0, 2, 2, 0}, + {0, 0, 1, 2, 0}, {1, 0, 1, 2, 0}, {2, 0, 1, 2, 0}, {0, 1, 2, 2, 0}, + {0, 1, 1, 2, 0}, {1, 1, 1, 2, 0}, {2, 1, 1, 2, 0}, {1, 1, 2, 2, 0}, + {0, 2, 1, 2, 0}, {1, 2, 1, 2, 0}, {2, 2, 1, 2, 0}, {2, 1, 2, 2, 0}, + {0, 2, 0, 2, 2}, {1, 2, 0, 2, 2}, {2, 2, 0, 2, 2}, {2, 0, 2, 2, 2}, + {0, 0, 0, 0, 2}, {1, 0, 0, 0, 2}, {2, 0, 0, 0, 2}, {0, 0, 2, 0, 2}, + {0, 1, 0, 0, 2}, {1, 1, 0, 0, 2}, {2, 1, 0, 0, 2}, {1, 0, 2, 0, 2}, + {0, 2, 0, 0, 2}, {1, 2, 0, 0, 2}, {2, 2, 0, 0, 2}, {2, 0, 2, 0, 2}, + {0, 2, 2, 0, 2}, {1, 2, 2, 0, 2}, {2, 2, 2, 0, 2}, {2, 0, 2, 0, 2}, + {0, 0, 1, 0, 2}, {1, 0, 1, 0, 2}, {2, 0, 1, 0, 2}, {0, 1, 2, 0, 2}, + {0, 1, 1, 0, 2}, {1, 1, 1, 0, 2}, {2, 1, 1, 0, 2}, {1, 1, 2, 0, 2}, + {0, 2, 1, 0, 2}, {1, 2, 1, 0, 2}, {2, 2, 1, 0, 2}, {2, 1, 2, 0, 2}, + {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 0, 2, 2, 2}, + {0, 0, 0, 0, 1}, {1, 0, 0, 0, 1}, {2, 0, 0, 0, 1}, {0, 0, 2, 0, 1}, + {0, 1, 0, 0, 1}, {1, 1, 0, 0, 1}, {2, 1, 0, 0, 1}, {1, 0, 2, 0, 1}, + {0, 2, 0, 0, 1}, {1, 2, 0, 0, 1}, {2, 2, 0, 0, 1}, {2, 0, 2, 0, 1}, + {0, 2, 2, 0, 1}, {1, 2, 2, 0, 1}, {2, 2, 2, 0, 1}, {2, 0, 2, 0, 1}, + {0, 0, 1, 0, 1}, {1, 0, 1, 0, 1}, {2, 0, 1, 0, 1}, {0, 1, 2, 0, 1}, + {0, 1, 1, 0, 1}, {1, 1, 1, 0, 1}, {2, 1, 1, 0, 1}, {1, 1, 2, 0, 1}, + {0, 2, 1, 0, 1}, {1, 2, 1, 0, 1}, {2, 2, 1, 0, 1}, {2, 1, 2, 0, 1}, + {0, 0, 1, 2, 2}, {1, 0, 1, 2, 2}, {2, 0, 1, 2, 2}, {0, 1, 2, 2, 2}, + {0, 0, 0, 1, 1}, {1, 0, 0, 1, 1}, {2, 0, 0, 1, 1}, {0, 0, 2, 1, 1}, + {0, 1, 0, 1, 1}, {1, 1, 0, 1, 1}, {2, 1, 0, 1, 1}, {1, 0, 2, 1, 1}, + {0, 2, 0, 1, 1}, {1, 2, 0, 1, 1}, {2, 2, 0, 1, 1}, {2, 0, 2, 1, 1}, + {0, 2, 2, 1, 1}, {1, 2, 2, 1, 1}, {2, 2, 2, 1, 1}, {2, 0, 2, 1, 1}, + {0, 0, 1, 1, 1}, {1, 0, 1, 1, 1}, {2, 0, 1, 1, 1}, {0, 1, 2, 1, 1}, + {0, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {2, 1, 1, 1, 1}, {1, 1, 2, 1, 1}, + {0, 2, 1, 1, 1}, {1, 2, 1, 1, 1}, {2, 2, 1, 1, 1}, {2, 1, 2, 1, 1}, + {0, 1, 1, 2, 2}, {1, 1, 1, 2, 2}, {2, 1, 1, 2, 2}, {1, 1, 2, 2, 2}, + {0, 0, 0, 2, 1}, {1, 0, 0, 2, 1}, {2, 0, 0, 2, 1}, {0, 0, 2, 2, 1}, + {0, 1, 0, 2, 1}, {1, 1, 0, 2, 1}, {2, 1, 0, 2, 1}, {1, 0, 2, 2, 1}, + {0, 2, 0, 2, 1}, {1, 2, 0, 2, 1}, {2, 2, 0, 2, 1}, {2, 0, 2, 2, 1}, + {0, 2, 2, 2, 1}, {1, 2, 2, 2, 1}, {2, 2, 2, 2, 1}, {2, 0, 2, 2, 1}, + {0, 0, 1, 2, 1}, {1, 0, 1, 2, 1}, {2, 0, 1, 2, 1}, {0, 1, 2, 2, 1}, + {0, 1, 1, 2, 1}, {1, 1, 1, 2, 1}, {2, 1, 1, 2, 1}, {1, 1, 2, 2, 1}, + {0, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {2, 2, 1, 2, 1}, {2, 1, 2, 2, 1}, + {0, 2, 1, 2, 2}, {1, 2, 1, 2, 2}, {2, 2, 1, 2, 2}, {2, 1, 2, 2, 2}, + {0, 0, 0, 1, 2}, {1, 0, 0, 1, 2}, {2, 0, 0, 1, 2}, {0, 0, 2, 1, 2}, + {0, 1, 0, 1, 2}, {1, 1, 0, 1, 2}, {2, 1, 0, 1, 2}, {1, 0, 2, 1, 2}, + {0, 2, 0, 1, 2}, {1, 2, 0, 1, 2}, {2, 2, 0, 1, 2}, {2, 0, 2, 1, 2}, + {0, 2, 2, 1, 2}, {1, 2, 2, 1, 2}, {2, 2, 2, 1, 2}, {2, 0, 2, 1, 2}, + {0, 0, 1, 1, 2}, {1, 0, 1, 1, 2}, {2, 0, 1, 1, 2}, {0, 1, 2, 1, 2}, + {0, 1, 1, 1, 2}, {1, 1, 1, 1, 2}, {2, 1, 1, 1, 2}, {1, 1, 2, 1, 2}, + {0, 2, 1, 1, 2}, {1, 2, 1, 1, 2}, {2, 2, 1, 1, 2}, {2, 1, 2, 1, 2}, + {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2} +}; + +/** @brief Packed trit values for each unpacked value, indexed [hi][][][][lo]. */ +static const uint8_t integer_of_trits[3][3][3][3][3] { + { + { + { + {0, 1, 2}, + {4, 5, 6}, + {8, 9, 10} + }, + { + {16, 17, 18}, + {20, 21, 22}, + {24, 25, 26} + }, + { + {3, 7, 15}, + {19, 23, 27}, + {12, 13, 14} + } + }, + { + { + {32, 33, 34}, + {36, 37, 38}, + {40, 41, 42} + }, + { + {48, 49, 50}, + {52, 53, 54}, + {56, 57, 58} + }, + { + {35, 39, 47}, + {51, 55, 59}, + {44, 45, 46} + } + }, + { + { + {64, 65, 66}, + {68, 69, 70}, + {72, 73, 74} + }, + { + {80, 81, 82}, + {84, 85, 86}, + {88, 89, 90} + }, + { + {67, 71, 79}, + {83, 87, 91}, + {76, 77, 78} + } + } + }, + { + { + { + {128, 129, 130}, + {132, 133, 134}, + {136, 137, 138} + }, + { + {144, 145, 146}, + {148, 149, 150}, + {152, 153, 154} + }, + { + {131, 135, 143}, + {147, 151, 155}, + {140, 141, 142} + } + }, + { + { + {160, 161, 162}, + {164, 165, 166}, + {168, 169, 170} + }, + { + {176, 177, 178}, + {180, 181, 182}, + {184, 185, 186} + }, + { + {163, 167, 175}, + {179, 183, 187}, + {172, 173, 174} + } + }, + { + { + {192, 193, 194}, + {196, 197, 198}, + {200, 201, 202} + }, + { + {208, 209, 210}, + {212, 213, 214}, + {216, 217, 218} + }, + { + {195, 199, 207}, + {211, 215, 219}, + {204, 205, 206} + } + } + }, + { + { + { + {96, 97, 98}, + {100, 101, 102}, + {104, 105, 106} + }, + { + {112, 113, 114}, + {116, 117, 118}, + {120, 121, 122} + }, + { + {99, 103, 111}, + {115, 119, 123}, + {108, 109, 110} + } + }, + { + { + {224, 225, 226}, + {228, 229, 230}, + {232, 233, 234} + }, + { + {240, 241, 242}, + {244, 245, 246}, + {248, 249, 250} + }, + { + {227, 231, 239}, + {243, 247, 251}, + {236, 237, 238} + } + }, + { + { + {28, 29, 30}, + {60, 61, 62}, + {92, 93, 94} + }, + { + {156, 157, 158}, + {188, 189, 190}, + {220, 221, 222} + }, + { + {31, 63, 127}, + {159, 191, 255}, + {252, 253, 254} + } + } + } +}; + +/** + * @brief The number of bits, trits, and quints needed for a quant level. + */ +struct btq_count +{ + /** @brief The quantization level. */ + uint8_t quant; + + /** @brief The number of bits. */ + uint8_t bits; + + /** @brief The number of trits. */ + uint8_t trits; + + /** @brief The number of quints. */ + uint8_t quints; +}; + +/** + * @brief The table of bits, trits, and quints needed for a quant encode. + */ +static const std::array<btq_count, 21> btq_counts {{ + { QUANT_2, 1, 0, 0 }, + { QUANT_3, 0, 1, 0 }, + { QUANT_4, 2, 0, 0 }, + { QUANT_5, 0, 0, 1 }, + { QUANT_6, 1, 1, 0 }, + { QUANT_8, 3, 0, 0 }, + { QUANT_10, 1, 0, 1 }, + { QUANT_12, 2, 1, 0 }, + { QUANT_16, 4, 0, 0 }, + { QUANT_20, 2, 0, 1 }, + { QUANT_24, 3, 1, 0 }, + { QUANT_32, 5, 0, 0 }, + { QUANT_40, 3, 0, 1 }, + { QUANT_48, 4, 1, 0 }, + { QUANT_64, 6, 0, 0 }, + { QUANT_80, 4, 0, 1 }, + { QUANT_96, 5, 1, 0 }, + { QUANT_128, 7, 0, 0 }, + { QUANT_160, 5, 0, 1 }, + { QUANT_192, 6, 1, 0 }, + { QUANT_256, 8, 0, 0 } +}}; + +/** + * @brief The sequence scale, round, and divisors needed to compute sizing. + * + * The length of a quantized sequence in bits is: + * (scale * <sequence_len> + round) / divisor + */ +struct ise_size +{ + /** @brief The quantization level. */ + uint8_t quant; + + /** @brief The scaling parameter. */ + uint8_t scale; + + /** @brief The rounding parameter. */ + uint8_t round; + + /** @brief The divisor parameter. */ + uint8_t divisor; +}; + +/** + * @brief The table of scale, round, and divisors needed for quant sizing. + */ +static const std::array<ise_size, 21> ise_sizes {{ + { QUANT_2, 1, 0, 1 }, + { QUANT_3, 8, 4, 5 }, + { QUANT_4, 2, 0, 1 }, + { QUANT_5, 7, 2, 3 }, + { QUANT_6, 13, 4, 5 }, + { QUANT_8, 3, 0, 1 }, + { QUANT_10, 10, 2, 3 }, + { QUANT_12, 18, 4, 5 }, + { QUANT_16, 4, 0, 1 }, + { QUANT_20, 13, 2, 3 }, + { QUANT_24, 23, 4, 5 }, + { QUANT_32, 5, 0, 1 }, + { QUANT_40, 16, 2, 3 }, + { QUANT_48, 28, 4, 5 }, + { QUANT_64, 6, 0, 1 }, + { QUANT_80, 19, 2, 3 }, + { QUANT_96, 33, 4, 5 }, + { QUANT_128, 7, 0, 1 }, + { QUANT_160, 22, 2, 3 }, + { QUANT_192, 38, 4, 5 }, + { QUANT_256, 8, 0, 1 } +}}; + +/* See header for documentation. */ +unsigned int get_ise_sequence_bitcount( + unsigned int character_count, + quant_method quant_level +) { + // Cope with out-of bounds values - input might be invalid + if (static_cast<size_t>(quant_level) >= ise_sizes.size()) + { + // Arbitrary large number that's more than an ASTC block can hold + return 1024; + } + + auto& entry = ise_sizes[quant_level]; + return (entry.scale * character_count + entry.round) / entry.divisor; +} + +/** + * @brief Write up to 8 bits at an arbitrary bit offset. + * + * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may + * span two separate bytes in memory. + * + * @param value The value to write. + * @param bitcount The number of bits to write, starting from LSB. + * @param bitoffset The bit offset to store at, between 0 and 7. + * @param[in,out] ptr The data pointer to write to. + */ +static inline void write_bits( + unsigned int value, + unsigned int bitcount, + unsigned int bitoffset, + uint8_t ptr[2] +) { + unsigned int mask = (1 << bitcount) - 1; + value &= mask; + ptr += bitoffset >> 3; + bitoffset &= 7; + value <<= bitoffset; + mask <<= bitoffset; + mask = ~mask; + + ptr[0] &= mask; + ptr[0] |= value; + ptr[1] &= mask >> 8; + ptr[1] |= value >> 8; +} + +/** + * @brief Read up to 8 bits at an arbitrary bit offset. + * + * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may + * span two separate bytes in memory. + * + * @param bitcount The number of bits to read. + * @param bitoffset The bit offset to read from, between 0 and 7. + * @param[in,out] ptr The data pointer to read from. + * + * @return The read value. + */ +static inline unsigned int read_bits( + unsigned int bitcount, + unsigned int bitoffset, + const uint8_t* ptr +) { + unsigned int mask = (1 << bitcount) - 1; + ptr += bitoffset >> 3; + bitoffset &= 7; + unsigned int value = ptr[0] | (ptr[1] << 8); + value >>= bitoffset; + value &= mask; + return value; +} + +/* See header for documentation. */ +void encode_ise( + quant_method quant_level, + unsigned int character_count, + const uint8_t* input_data, + uint8_t* output_data, + unsigned int bit_offset +) { + promise(character_count > 0); + + unsigned int bits = btq_counts[quant_level].bits; + unsigned int trits = btq_counts[quant_level].trits; + unsigned int quints = btq_counts[quant_level].quints; + unsigned int mask = (1 << bits) - 1; + + // Write out trits and bits + if (trits) + { + unsigned int i = 0; + unsigned int full_trit_blocks = character_count / 5; + + for (unsigned int j = 0; j < full_trit_blocks; j++) + { + unsigned int i4 = input_data[i + 4] >> bits; + unsigned int i3 = input_data[i + 3] >> bits; + unsigned int i2 = input_data[i + 2] >> bits; + unsigned int i1 = input_data[i + 1] >> bits; + unsigned int i0 = input_data[i + 0] >> bits; + + uint8_t T = integer_of_trits[i4][i3][i2][i1][i0]; + + // The max size of a trit bit count is 6, so we can always safely + // pack a single MX value with the following 1 or 2 T bits. + uint8_t pack; + + // Element 0 + T0 + T1 + pack = (input_data[i++] & mask) | (((T >> 0) & 0x3) << bits); + write_bits(pack, bits + 2, bit_offset, output_data); + bit_offset += bits + 2; + + // Element 1 + T2 + T3 + pack = (input_data[i++] & mask) | (((T >> 2) & 0x3) << bits); + write_bits(pack, bits + 2, bit_offset, output_data); + bit_offset += bits + 2; + + // Element 2 + T4 + pack = (input_data[i++] & mask) | (((T >> 4) & 0x1) << bits); + write_bits(pack, bits + 1, bit_offset, output_data); + bit_offset += bits + 1; + + // Element 3 + T5 + T6 + pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits); + write_bits(pack, bits + 2, bit_offset, output_data); + bit_offset += bits + 2; + + // Element 4 + T7 + pack = (input_data[i++] & mask) | (((T >> 7) & 0x1) << bits); + write_bits(pack, bits + 1, bit_offset, output_data); + bit_offset += bits + 1; + } + + // Loop tail for a partial block + if (i != character_count) + { + // i4 cannot be present - we know the block is partial + // i0 must be present - we know the block isn't empty + unsigned int i4 = 0; + unsigned int i3 = i + 3 >= character_count ? 0 : input_data[i + 3] >> bits; + unsigned int i2 = i + 2 >= character_count ? 0 : input_data[i + 2] >> bits; + unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits; + unsigned int i0 = input_data[i + 0] >> bits; + + uint8_t T = integer_of_trits[i4][i3][i2][i1][i0]; + + for (unsigned int j = 0; i < character_count; i++, j++) + { + // Truncated table as this iteration is always partital + static const uint8_t tbits[4] { 2, 2, 1, 2 }; + static const uint8_t tshift[4] { 0, 2, 4, 5 }; + + uint8_t pack = (input_data[i] & mask) | + (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits); + + write_bits(pack, bits + tbits[j], bit_offset, output_data); + bit_offset += bits + tbits[j]; + } + } + } + // Write out quints and bits + else if (quints) + { + unsigned int i = 0; + unsigned int full_quint_blocks = character_count / 3; + + for (unsigned int j = 0; j < full_quint_blocks; j++) + { + unsigned int i2 = input_data[i + 2] >> bits; + unsigned int i1 = input_data[i + 1] >> bits; + unsigned int i0 = input_data[i + 0] >> bits; + + uint8_t T = integer_of_quints[i2][i1][i0]; + + // The max size of a quint bit count is 5, so we can always safely + // pack a single M value with the following 2 or 3 T bits. + uint8_t pack; + + // Element 0 + pack = (input_data[i++] & mask) | (((T >> 0) & 0x7) << bits); + write_bits(pack, bits + 3, bit_offset, output_data); + bit_offset += bits + 3; + + // Element 1 + pack = (input_data[i++] & mask) | (((T >> 3) & 0x3) << bits); + write_bits(pack, bits + 2, bit_offset, output_data); + bit_offset += bits + 2; + + // Element 2 + pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits); + write_bits(pack, bits + 2, bit_offset, output_data); + bit_offset += bits + 2; + } + + // Loop tail for a partial block + if (i != character_count) + { + // i2 cannot be present - we know the block is partial + // i0 must be present - we know the block isn't empty + unsigned int i2 = 0; + unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits; + unsigned int i0 = input_data[i + 0] >> bits; + + uint8_t T = integer_of_quints[i2][i1][i0]; + + for (unsigned int j = 0; i < character_count; i++, j++) + { + // Truncated table as this iteration is always partital + static const uint8_t tbits[2] { 3, 2 }; + static const uint8_t tshift[2] { 0, 3 }; + + uint8_t pack = (input_data[i] & mask) | + (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits); + + write_bits(pack, bits + tbits[j], bit_offset, output_data); + bit_offset += bits + tbits[j]; + } + } + } + // Write out just bits + else + { + promise(character_count > 0); + for (unsigned int i = 0; i < character_count; i++) + { + write_bits(input_data[i], bits, bit_offset, output_data); + bit_offset += bits; + } + } +} + +/* See header for documentation. */ +void decode_ise( + quant_method quant_level, + unsigned int character_count, + const uint8_t* input_data, + uint8_t* output_data, + unsigned int bit_offset +) { + promise(character_count > 0); + + // Note: due to how the trit/quint-block unpacking is done in this function, we may write more + // temporary results than the number of outputs. The maximum actual number of results is 64 bit, + // but we keep 4 additional character_count of padding. + uint8_t results[68]; + uint8_t tq_blocks[22] { 0 }; // Trit-blocks or quint-blocks, must be zeroed + + unsigned int bits = btq_counts[quant_level].bits; + unsigned int trits = btq_counts[quant_level].trits; + unsigned int quints = btq_counts[quant_level].quints; + + unsigned int lcounter = 0; + unsigned int hcounter = 0; + + // Collect bits for each element, as well as bits for any trit-blocks and quint-blocks. + for (unsigned int i = 0; i < character_count; i++) + { + results[i] = static_cast<uint8_t>(read_bits(bits, bit_offset, input_data)); + bit_offset += bits; + + if (trits) + { + static const unsigned int bits_to_read[5] { 2, 2, 1, 2, 1 }; + static const unsigned int block_shift[5] { 0, 2, 4, 5, 7 }; + static const unsigned int next_lcounter[5] { 1, 2, 3, 4, 0 }; + static const unsigned int hcounter_incr[5] { 0, 0, 0, 0, 1 }; + unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data); + bit_offset += bits_to_read[lcounter]; + tq_blocks[hcounter] |= tdata << block_shift[lcounter]; + hcounter += hcounter_incr[lcounter]; + lcounter = next_lcounter[lcounter]; + } + + if (quints) + { + static const unsigned int bits_to_read[3] { 3, 2, 2 }; + static const unsigned int block_shift[3] { 0, 3, 5 }; + static const unsigned int next_lcounter[3] { 1, 2, 0 }; + static const unsigned int hcounter_incr[3] { 0, 0, 1 }; + unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data); + bit_offset += bits_to_read[lcounter]; + tq_blocks[hcounter] |= tdata << block_shift[lcounter]; + hcounter += hcounter_incr[lcounter]; + lcounter = next_lcounter[lcounter]; + } + } + + // Unpack trit-blocks or quint-blocks as needed + if (trits) + { + unsigned int trit_blocks = (character_count + 4) / 5; + for (unsigned int i = 0; i < trit_blocks; i++) + { + const uint8_t *tritptr = trits_of_integer[tq_blocks[i]]; + results[5 * i ] |= tritptr[0] << bits; + results[5 * i + 1] |= tritptr[1] << bits; + results[5 * i + 2] |= tritptr[2] << bits; + results[5 * i + 3] |= tritptr[3] << bits; + results[5 * i + 4] |= tritptr[4] << bits; + } + } + + if (quints) + { + unsigned int quint_blocks = (character_count + 2) / 3; + for (unsigned int i = 0; i < quint_blocks; i++) + { + const uint8_t *quintptr = quints_of_integer[tq_blocks[i]]; + results[3 * i ] |= quintptr[0] << bits; + results[3 * i + 1] |= quintptr[1] << bits; + results[3 * i + 2] |= quintptr[2] << bits; + } + } + + for (unsigned int i = 0; i < character_count; i++) + { + output_data[i] = results[i]; + } +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal.h new file mode 100644 index 00000000000..0fa8ec67027 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal.h @@ -0,0 +1,2185 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions and data declarations. + */ + +#ifndef ASTCENC_INTERNAL_INCLUDED +#define ASTCENC_INTERNAL_INCLUDED + +#include <algorithm> +#include <cstddef> +#include <cstdint> +#if defined(ASTCENC_DIAGNOSTICS) + #include <cstdio> +#endif +#include <cstdlib> + +#include "astcenc.h" +#include "astcenc_mathlib.h" +#include "astcenc_vecmathlib.h" + +/** + * @brief Make a promise to the compiler's optimizer. + * + * A promise is an expression that the optimizer is can assume is true for to help it generate + * faster code. Common use cases for this are to promise that a for loop will iterate more than + * once, or that the loop iteration count is a multiple of a vector length, which avoids pre-loop + * checks and can avoid loop tails if loops are unrolled by the auto-vectorizer. + */ +#if defined(NDEBUG) + #if !defined(__clang__) && defined(_MSC_VER) + #define promise(cond) __assume(cond) + #elif defined(__clang__) + #if __has_builtin(__builtin_assume) + #define promise(cond) __builtin_assume(cond) + #elif __has_builtin(__builtin_unreachable) + #define promise(cond) if (!(cond)) { __builtin_unreachable(); } + #else + #define promise(cond) + #endif + #else // Assume GCC + #define promise(cond) if (!(cond)) { __builtin_unreachable(); } + #endif +#else + #define promise(cond) assert(cond) +#endif + +/* ============================================================================ + Constants +============================================================================ */ +#if !defined(ASTCENC_BLOCK_MAX_TEXELS) + #define ASTCENC_BLOCK_MAX_TEXELS 216 // A 3D 6x6x6 block +#endif + +/** @brief The maximum number of texels a block can support (6x6x6 block). */ +static constexpr unsigned int BLOCK_MAX_TEXELS { ASTCENC_BLOCK_MAX_TEXELS }; + +/** @brief The maximum number of components a block can support. */ +static constexpr unsigned int BLOCK_MAX_COMPONENTS { 4 }; + +/** @brief The maximum number of partitions a block can support. */ +static constexpr unsigned int BLOCK_MAX_PARTITIONS { 4 }; + +/** @brief The number of partitionings, per partition count, suported by the ASTC format. */ +static constexpr unsigned int BLOCK_MAX_PARTITIONINGS { 1024 }; + +/** @brief The maximum number of weights used during partition selection for texel clustering. */ +static constexpr uint8_t BLOCK_MAX_KMEANS_TEXELS { 64 }; + +/** @brief The maximum number of weights a block can support. */ +static constexpr unsigned int BLOCK_MAX_WEIGHTS { 64 }; + +/** @brief The maximum number of weights a block can support per plane in 2 plane mode. */ +static constexpr unsigned int BLOCK_MAX_WEIGHTS_2PLANE { BLOCK_MAX_WEIGHTS / 2 }; + +/** @brief The minimum number of weight bits a candidate encoding must encode. */ +static constexpr unsigned int BLOCK_MIN_WEIGHT_BITS { 24 }; + +/** @brief The maximum number of weight bits a candidate encoding can encode. */ +static constexpr unsigned int BLOCK_MAX_WEIGHT_BITS { 96 }; + +/** @brief The index indicating a bad (unused) block mode in the remap array. */ +static constexpr uint16_t BLOCK_BAD_BLOCK_MODE { 0xFFFFu }; + +/** @brief The index indicating a bad (unused) partitioning in the remap array. */ +static constexpr uint16_t BLOCK_BAD_PARTITIONING { 0xFFFFu }; + +/** @brief The number of partition index bits supported by the ASTC format . */ +static constexpr unsigned int PARTITION_INDEX_BITS { 10 }; + +/** @brief The offset of the plane 2 weights in shared weight arrays. */ +static constexpr unsigned int WEIGHTS_PLANE2_OFFSET { BLOCK_MAX_WEIGHTS_2PLANE }; + +/** @brief The sum of quantized weights for one texel. */ +static constexpr float WEIGHTS_TEXEL_SUM { 16.0f }; + +/** @brief The number of block modes supported by the ASTC format. */ +static constexpr unsigned int WEIGHTS_MAX_BLOCK_MODES { 2048 }; + +/** @brief The number of weight grid decimation modes supported by the ASTC format. */ +static constexpr unsigned int WEIGHTS_MAX_DECIMATION_MODES { 87 }; + +/** @brief The high default error used to initialize error trackers. */ +static constexpr float ERROR_CALC_DEFAULT { 1e30f }; + +/** + * @brief The minimum texel count for a block to use the one partition fast path. + * + * This setting skips 4x4 and 5x4 block sizes. + */ +static constexpr unsigned int TUNE_MIN_TEXELS_MODE0_FASTPATH { 24 }; + +/** + * @brief The maximum number of candidate encodings tested for each encoding mode. + * + * This can be dynamically reduced by the compression quality preset. + */ +static constexpr unsigned int TUNE_MAX_TRIAL_CANDIDATES { 8 }; + +/** + * @brief The maximum number of candidate partitionings tested for each encoding mode. + * + * This can be dynamically reduced by the compression quality preset. + */ +static constexpr unsigned int TUNE_MAX_PARTITIIONING_CANDIDATES { 32 }; + +/** + * @brief The maximum quant level using full angular endpoint search method. + * + * The angular endpoint search is used to find the min/max weight that should + * be used for a given quantization level. It is effective but expensive, so + * we only use it where it has the most value - low quant levels with wide + * spacing. It is used below TUNE_MAX_ANGULAR_QUANT (inclusive). Above this we + * assume the min weight is 0.0f, and the max weight is 1.0f. + * + * Note the angular algorithm is vectorized, and using QUANT_12 exactly fills + * one 8-wide vector. Decreasing by one doesn't buy much performance, and + * increasing by one is disproportionately expensive. + */ +static constexpr unsigned int TUNE_MAX_ANGULAR_QUANT { 7 }; /* QUANT_12 */ + + +static_assert((BLOCK_MAX_TEXELS % ASTCENC_SIMD_WIDTH) == 0, + "BLOCK_MAX_TEXELS must be multiple of ASTCENC_SIMD_WIDTH"); + +static_assert((BLOCK_MAX_WEIGHTS % ASTCENC_SIMD_WIDTH) == 0, + "BLOCK_MAX_WEIGHTS must be multiple of ASTCENC_SIMD_WIDTH"); + +static_assert((WEIGHTS_MAX_BLOCK_MODES % ASTCENC_SIMD_WIDTH) == 0, + "WEIGHTS_MAX_BLOCK_MODES must be multiple of ASTCENC_SIMD_WIDTH"); + + +/* ============================================================================ + Commonly used data structures +============================================================================ */ + +/** + * @brief The ASTC endpoint formats. + * + * Note, the values here are used directly in the encoding in the format so do not rearrange. + */ +enum endpoint_formats +{ + FMT_LUMINANCE = 0, + FMT_LUMINANCE_DELTA = 1, + FMT_HDR_LUMINANCE_LARGE_RANGE = 2, + FMT_HDR_LUMINANCE_SMALL_RANGE = 3, + FMT_LUMINANCE_ALPHA = 4, + FMT_LUMINANCE_ALPHA_DELTA = 5, + FMT_RGB_SCALE = 6, + FMT_HDR_RGB_SCALE = 7, + FMT_RGB = 8, + FMT_RGB_DELTA = 9, + FMT_RGB_SCALE_ALPHA = 10, + FMT_HDR_RGB = 11, + FMT_RGBA = 12, + FMT_RGBA_DELTA = 13, + FMT_HDR_RGB_LDR_ALPHA = 14, + FMT_HDR_RGBA = 15 +}; + +/** + * @brief The ASTC quantization methods. + * + * Note, the values here are used directly in the encoding in the format so do not rearrange. + */ +enum quant_method +{ + QUANT_2 = 0, + QUANT_3 = 1, + QUANT_4 = 2, + QUANT_5 = 3, + QUANT_6 = 4, + QUANT_8 = 5, + QUANT_10 = 6, + QUANT_12 = 7, + QUANT_16 = 8, + QUANT_20 = 9, + QUANT_24 = 10, + QUANT_32 = 11, + QUANT_40 = 12, + QUANT_48 = 13, + QUANT_64 = 14, + QUANT_80 = 15, + QUANT_96 = 16, + QUANT_128 = 17, + QUANT_160 = 18, + QUANT_192 = 19, + QUANT_256 = 20 +}; + +/** + * @brief The number of levels use by an ASTC quantization method. + * + * @param method The quantization method + * + * @return The number of levels used by @c method. + */ +static inline unsigned int get_quant_level(quant_method method) +{ + switch (method) + { + case QUANT_2: return 2; + case QUANT_3: return 3; + case QUANT_4: return 4; + case QUANT_5: return 5; + case QUANT_6: return 6; + case QUANT_8: return 8; + case QUANT_10: return 10; + case QUANT_12: return 12; + case QUANT_16: return 16; + case QUANT_20: return 20; + case QUANT_24: return 24; + case QUANT_32: return 32; + case QUANT_40: return 40; + case QUANT_48: return 48; + case QUANT_64: return 64; + case QUANT_80: return 80; + case QUANT_96: return 96; + case QUANT_128: return 128; + case QUANT_160: return 160; + case QUANT_192: return 192; + case QUANT_256: return 256; + } + + // Unreachable - the enum is fully described + return 0; +} + +/** + * @brief Computed metrics about a partition in a block. + */ +struct partition_metrics +{ + /** @brief The error-weighted average color in the partition. */ + vfloat4 avg; + + /** @brief The dominant error-weighted direction in the partition. */ + vfloat4 dir; +}; + +/** + * @brief Computed lines for a a three component analysis. + */ +struct partition_lines3 +{ + /** @brief Line for uncorrelated chroma. */ + line3 uncor_line; + + /** @brief Line for correlated chroma, passing though the origin. */ + line3 samec_line; + + /** @brief Post-processed line for uncorrelated chroma. */ + processed_line3 uncor_pline; + + /** @brief Post-processed line for correlated chroma, passing though the origin. */ + processed_line3 samec_pline; + + /** @brief The length of the line for uncorrelated chroma. */ + float uncor_line_len; + + /** @brief The length of the line for correlated chroma. */ + float samec_line_len; +}; + +/** + * @brief The partition information for a single partition. + * + * ASTC has a total of 1024 candidate partitions for each of 2/3/4 partition counts, although this + * 1024 includes seeds that generate duplicates of other seeds and seeds that generate completely + * empty partitions. These are both valid encodings, but astcenc will skip both during compression + * as they are not useful. + */ +struct partition_info +{ + /** @brief The number of partitions in this partitioning. */ + uint16_t partition_count; + + /** @brief The index (seed) of this partitioning. */ + uint16_t partition_index; + + /** + * @brief The number of texels in each partition. + * + * Note that some seeds result in zero texels assigned to a partition are valid, but are skipped + * by this compressor as there is no point spending bits encoding an unused color endpoint. + */ + uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS]; + + /** @brief The partition of each texel in the block. */ + uint8_t partition_of_texel[BLOCK_MAX_TEXELS]; + + /** @brief The list of texels in each partition. */ + uint8_t texels_of_partition[BLOCK_MAX_PARTITIONS][BLOCK_MAX_TEXELS]; +}; + +/** + * @brief The weight grid information for a single decimation pattern. + * + * ASTC can store one weight per texel, but is also capable of storing lower resolution weight grids + * that are interpolated during decompression to assign a with to a texel. Storing fewer weights + * can free up a substantial amount of bits that we can then spend on more useful things, such as + * more accurate endpoints and weights, or additional partitions. + * + * This data structure is used to store information about a single weight grid decimation pattern, + * for a single block size. + */ +struct decimation_info +{ + /** @brief The total number of texels in the block. */ + uint8_t texel_count; + + /** @brief The maximum number of stored weights that contribute to each texel, between 1 and 4. */ + uint8_t max_texel_weight_count; + + /** @brief The total number of weights stored. */ + uint8_t weight_count; + + /** @brief The number of stored weights in the X dimension. */ + uint8_t weight_x; + + /** @brief The number of stored weights in the Y dimension. */ + uint8_t weight_y; + + /** @brief The number of stored weights in the Z dimension. */ + uint8_t weight_z; + + /** @brief The number of stored weights that contribute to each texel, between 1 and 4. */ + uint8_t texel_weight_count[BLOCK_MAX_TEXELS]; + + /** @brief The weight index of the N weights that need to be interpolated for each texel. */ + uint8_t texel_weights_4t[4][BLOCK_MAX_TEXELS]; + + /** @brief The bilinear interpolation weighting of the N input weights for each texel, between 0 and 16. */ + uint8_t texel_weights_int_4t[4][BLOCK_MAX_TEXELS]; + + /** @brief The bilinear interpolation weighting of the N input weights for each texel, between 0 and 1. */ + alignas(ASTCENC_VECALIGN) float texel_weights_float_4t[4][BLOCK_MAX_TEXELS]; + + /** @brief The number of texels that each stored weight contributes to. */ + uint8_t weight_texel_count[BLOCK_MAX_WEIGHTS]; + + /** @brief The list of weights that contribute to each texel. */ + uint8_t weight_texel[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS]; + + /** @brief The list of weight indices that contribute to each texel. */ + alignas(ASTCENC_VECALIGN) float weights_flt[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS]; + + /** + * @brief Folded structure for faster access: + * texel_weights_texel[i][j][.] = texel_weights[.][weight_texel[i][j]] + */ + uint8_t texel_weights_texel[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS][4]; + + /** + * @brief Folded structure for faster access: + * texel_weights_float_texel[i][j][.] = texel_weights_float[.][weight_texel[i][j]] + */ + float texel_weights_float_texel[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS][4]; +}; + +/** + * @brief Metadata for single block mode for a specific block size. + */ +struct block_mode +{ + /** @brief The block mode index in the ASTC encoded form. */ + uint16_t mode_index; + + /** @brief The decimation mode index in the compressor reindexed list. */ + uint8_t decimation_mode; + + /** @brief The weight quantization used by this block mode. */ + uint8_t quant_mode; + + /** @brief The weight quantization used by this block mode. */ + uint8_t weight_bits; + + /** @brief Is a dual weight plane used by this block mode? */ + uint8_t is_dual_plane : 1; + + /** + * @brief Get the weight quantization used by this block mode. + * + * @return The quantization level. + */ + inline quant_method get_weight_quant_mode() const + { + return static_cast<quant_method>(this->quant_mode); + } +}; + +/** + * @brief Metadata for single decimation mode for a specific block size. + */ +struct decimation_mode +{ + /** @brief The max weight precision for 1 plane, or -1 if not supported. */ + int8_t maxprec_1plane; + + /** @brief The max weight precision for 2 planes, or -1 if not supported. */ + int8_t maxprec_2planes; + + /** + * @brief Bitvector indicating weight quant modes used by active 1 plane block modes. + * + * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc. + */ + uint16_t refprec_1_plane; + + /** + * @brief Bitvector indicating weight quant methods used by active 2 plane block modes. + * + * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc. + */ + uint16_t refprec_2_planes; + + /** + * @brief Set a 1 plane weight quant as active. + * + * @param weight_quant The quant method to set. + */ + void set_ref_1_plane(quant_method weight_quant) + { + refprec_1_plane |= (1 << weight_quant); + } + + /** + * @brief Test if this mode is active below a given 1 plane weight quant (inclusive). + * + * @param max_weight_quant The max quant method to test. + */ + bool is_ref_1_plane(quant_method max_weight_quant) const + { + uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1); + return (refprec_1_plane & mask) != 0; + } + + /** + * @brief Set a 2 plane weight quant as active. + * + * @param weight_quant The quant method to set. + */ + void set_ref_2_plane(quant_method weight_quant) + { + refprec_2_planes |= static_cast<uint16_t>(1 << weight_quant); + } + + /** + * @brief Test if this mode is active below a given 2 plane weight quant (inclusive). + * + * @param max_weight_quant The max quant method to test. + */ + bool is_ref_2_plane(quant_method max_weight_quant) const + { + uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1); + return (refprec_2_planes & mask) != 0; + } +}; + +/** + * @brief Data tables for a single block size. + * + * The decimation tables store the information to apply weight grid dimension reductions. We only + * store the decimation modes that are actually needed by the current context; many of the possible + * modes will be unused (too many weights for the current block size or disabled by heuristics). The + * actual number of weights stored is @c decimation_mode_count, and the @c decimation_modes and + * @c decimation_tables arrays store the active modes contiguously at the start of the array. These + * entries are not stored in any particular order. + * + * The block mode tables store the unpacked block mode settings. Block modes are stored in the + * compressed block as an 11 bit field, but for any given block size and set of compressor + * heuristics, only a subset of the block modes will be used. The actual number of block modes + * stored is indicated in @c block_mode_count, and the @c block_modes array store the active modes + * contiguously at the start of the array. These entries are stored in incrementing "packed" value + * order, which doesn't mean much once unpacked. To allow decompressors to reference the packed data + * efficiently the @c block_mode_packed_index array stores the mapping between physical ID and the + * actual remapped array index. + */ +struct block_size_descriptor +{ + /** @brief The block X dimension, in texels. */ + uint8_t xdim; + + /** @brief The block Y dimension, in texels. */ + uint8_t ydim; + + /** @brief The block Z dimension, in texels. */ + uint8_t zdim; + + /** @brief The block total texel count. */ + uint8_t texel_count; + + /** + * @brief The number of stored decimation modes which are "always" modes. + * + * Always modes are stored at the start of the decimation_modes list. + */ + unsigned int decimation_mode_count_always; + + /** @brief The number of stored decimation modes for selected encodings. */ + unsigned int decimation_mode_count_selected; + + /** @brief The number of stored decimation modes for any encoding. */ + unsigned int decimation_mode_count_all; + + /** + * @brief The number of stored block modes which are "always" modes. + * + * Always modes are stored at the start of the block_modes list. + */ + unsigned int block_mode_count_1plane_always; + + /** @brief The number of stored block modes for active 1 plane encodings. */ + unsigned int block_mode_count_1plane_selected; + + /** @brief The number of stored block modes for active 1 and 2 plane encodings. */ + unsigned int block_mode_count_1plane_2plane_selected; + + /** @brief The number of stored block modes for any encoding. */ + unsigned int block_mode_count_all; + + /** @brief The number of selected partitionings for 1/2/3/4 partitionings. */ + unsigned int partitioning_count_selected[BLOCK_MAX_PARTITIONS]; + + /** @brief The number of partitionings for 1/2/3/4 partitionings. */ + unsigned int partitioning_count_all[BLOCK_MAX_PARTITIONS]; + + /** @brief The active decimation modes, stored in low indices. */ + decimation_mode decimation_modes[WEIGHTS_MAX_DECIMATION_MODES]; + + /** @brief The active decimation tables, stored in low indices. */ + alignas(ASTCENC_VECALIGN) decimation_info decimation_tables[WEIGHTS_MAX_DECIMATION_MODES]; + + /** @brief The packed block mode array index, or @c BLOCK_BAD_BLOCK_MODE if not active. */ + uint16_t block_mode_packed_index[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The active block modes, stored in low indices. */ + block_mode block_modes[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The active partition tables, stored in low indices per-count. */ + partition_info partitionings[(3 * BLOCK_MAX_PARTITIONINGS) + 1]; + + /** + * @brief The packed partition table array index, or @c BLOCK_BAD_PARTITIONING if not active. + * + * Indexed by partition_count - 2, containing 2, 3 and 4 partitions. + */ + uint16_t partitioning_packed_index[3][BLOCK_MAX_PARTITIONINGS]; + + /** @brief The active texels for k-means partition selection. */ + uint8_t kmeans_texels[BLOCK_MAX_KMEANS_TEXELS]; + + /** + * @brief The canonical 2-partition coverage pattern used during block partition search. + * + * Indexed by remapped index, not physical index. + */ + uint64_t coverage_bitmaps_2[BLOCK_MAX_PARTITIONINGS][2]; + + /** + * @brief The canonical 3-partition coverage pattern used during block partition search. + * + * Indexed by remapped index, not physical index. + */ + uint64_t coverage_bitmaps_3[BLOCK_MAX_PARTITIONINGS][3]; + + /** + * @brief The canonical 4-partition coverage pattern used during block partition search. + * + * Indexed by remapped index, not physical index. + */ + uint64_t coverage_bitmaps_4[BLOCK_MAX_PARTITIONINGS][4]; + + /** + * @brief Get the block mode structure for index @c block_mode. + * + * This function can only return block modes that are enabled by the current compressor config. + * Decompression from an arbitrary source should not use this without first checking that the + * packed block mode index is not @c BLOCK_BAD_BLOCK_MODE. + * + * @param block_mode The packed block mode index. + * + * @return The block mode structure. + */ + const block_mode& get_block_mode(unsigned int block_mode) const + { + unsigned int packed_index = this->block_mode_packed_index[block_mode]; + assert(packed_index != BLOCK_BAD_BLOCK_MODE && packed_index < this->block_mode_count_all); + return this->block_modes[packed_index]; + } + + /** + * @brief Get the decimation mode structure for index @c decimation_mode. + * + * This function can only return decimation modes that are enabled by the current compressor + * config. The mode array is stored packed, but this is only ever indexed by the packed index + * stored in the @c block_mode and never exists in an unpacked form. + * + * @param decimation_mode The packed decimation mode index. + * + * @return The decimation mode structure. + */ + const decimation_mode& get_decimation_mode(unsigned int decimation_mode) const + { + return this->decimation_modes[decimation_mode]; + } + + /** + * @brief Get the decimation info structure for index @c decimation_mode. + * + * This function can only return decimation modes that are enabled by the current compressor + * config. The mode array is stored packed, but this is only ever indexed by the packed index + * stored in the @c block_mode and never exists in an unpacked form. + * + * @param decimation_mode The packed decimation mode index. + * + * @return The decimation info structure. + */ + const decimation_info& get_decimation_info(unsigned int decimation_mode) const + { + return this->decimation_tables[decimation_mode]; + } + + /** + * @brief Get the partition info table for a given partition count. + * + * @param partition_count The number of partitions we want the table for. + * + * @return The pointer to the table of 1024 entries (for 2/3/4 parts) or 1 entry (for 1 part). + */ + const partition_info* get_partition_table(unsigned int partition_count) const + { + if (partition_count == 1) + { + partition_count = 5; + } + unsigned int index = (partition_count - 2) * BLOCK_MAX_PARTITIONINGS; + return this->partitionings + index; + } + + /** + * @brief Get the partition info structure for a given partition count and seed. + * + * @param partition_count The number of partitions we want the info for. + * @param index The partition seed (between 0 and 1023). + * + * @return The partition info structure. + */ + const partition_info& get_partition_info(unsigned int partition_count, unsigned int index) const + { + unsigned int packed_index = 0; + if (partition_count >= 2) + { + packed_index = this->partitioning_packed_index[partition_count - 2][index]; + } + + assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]); + auto& result = get_partition_table(partition_count)[packed_index]; + assert(index == result.partition_index); + return result; + } + + /** + * @brief Get the partition info structure for a given partition count and seed. + * + * @param partition_count The number of partitions we want the info for. + * @param packed_index The raw array offset. + * + * @return The partition info structure. + */ + const partition_info& get_raw_partition_info(unsigned int partition_count, unsigned int packed_index) const + { + assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]); + auto& result = get_partition_table(partition_count)[packed_index]; + return result; + } +}; + +/** + * @brief The image data for a single block. + * + * The @c data_[rgba] fields store the image data in an encoded SoA float form designed for easy + * vectorization. Input data is converted to float and stored as values between 0 and 65535. LDR + * data is stored as direct UNORM data, HDR data is stored as LNS data. + * + * The @c rgb_lns and @c alpha_lns fields that assigned a per-texel use of HDR are only used during + * decompression. The current compressor will always use HDR endpoint formats when in HDR mode. + */ +struct image_block +{ + /** @brief The input (compress) or output (decompress) data for the red color component. */ + alignas(ASTCENC_VECALIGN) float data_r[BLOCK_MAX_TEXELS]; + + /** @brief The input (compress) or output (decompress) data for the green color component. */ + alignas(ASTCENC_VECALIGN) float data_g[BLOCK_MAX_TEXELS]; + + /** @brief The input (compress) or output (decompress) data for the blue color component. */ + alignas(ASTCENC_VECALIGN) float data_b[BLOCK_MAX_TEXELS]; + + /** @brief The input (compress) or output (decompress) data for the alpha color component. */ + alignas(ASTCENC_VECALIGN) float data_a[BLOCK_MAX_TEXELS]; + + /** @brief The number of texels in the block. */ + uint8_t texel_count; + + /** @brief The original data for texel 0 for constant color block encoding. */ + vfloat4 origin_texel; + + /** @brief The min component value of all texels in the block. */ + vfloat4 data_min; + + /** @brief The mean component value of all texels in the block. */ + vfloat4 data_mean; + + /** @brief The max component value of all texels in the block. */ + vfloat4 data_max; + + /** @brief The relative error significance of the color channels. */ + vfloat4 channel_weight; + + /** @brief Is this grayscale block where R == G == B for all texels? */ + bool grayscale; + + /** @brief Set to 1 if a texel is using HDR RGB endpoints (decompression only). */ + uint8_t rgb_lns[BLOCK_MAX_TEXELS]; + + /** @brief Set to 1 if a texel is using HDR alpha endpoints (decompression only). */ + uint8_t alpha_lns[BLOCK_MAX_TEXELS]; + + /** @brief The X position of this block in the input or output image. */ + unsigned int xpos; + + /** @brief The Y position of this block in the input or output image. */ + unsigned int ypos; + + /** @brief The Z position of this block in the input or output image. */ + unsigned int zpos; + + /** + * @brief Get an RGBA texel value from the data. + * + * @param index The texel index. + * + * @return The texel in RGBA component ordering. + */ + inline vfloat4 texel(unsigned int index) const + { + return vfloat4(data_r[index], + data_g[index], + data_b[index], + data_a[index]); + } + + /** + * @brief Get an RGB texel value from the data. + * + * @param index The texel index. + * + * @return The texel in RGB0 component ordering. + */ + inline vfloat4 texel3(unsigned int index) const + { + return vfloat3(data_r[index], + data_g[index], + data_b[index]); + } + + /** + * @brief Get the default alpha value for endpoints that don't store it. + * + * The default depends on whether the alpha endpoint is LDR or HDR. + * + * @return The alpha value in the scaled range used by the compressor. + */ + inline float get_default_alpha() const + { + return this->alpha_lns[0] ? static_cast<float>(0x7800) : static_cast<float>(0xFFFF); + } + + /** + * @brief Test if a single color channel is constant across the block. + * + * Constant color channels are easier to compress as interpolating between two identical colors + * always returns the same value, irrespective of the weight used. They therefore can be ignored + * for the purposes of weight selection and use of a second weight plane. + * + * @return @c true if the channel is constant across the block, @c false otherwise. + */ + inline bool is_constant_channel(int channel) const + { + vmask4 lane_mask = vint4::lane_id() == vint4(channel); + vmask4 color_mask = this->data_min == this->data_max; + return any(lane_mask & color_mask); + } + + /** + * @brief Test if this block is a luminance block with constant 1.0 alpha. + * + * @return @c true if the block is a luminance block , @c false otherwise. + */ + inline bool is_luminance() const + { + float default_alpha = this->get_default_alpha(); + bool alpha1 = (this->data_min.lane<3>() == default_alpha) && + (this->data_max.lane<3>() == default_alpha); + return this->grayscale && alpha1; + } + + /** + * @brief Test if this block is a luminance block with variable alpha. + * + * @return @c true if the block is a luminance + alpha block , @c false otherwise. + */ + inline bool is_luminancealpha() const + { + float default_alpha = this->get_default_alpha(); + bool alpha1 = (this->data_min.lane<3>() == default_alpha) && + (this->data_max.lane<3>() == default_alpha); + return this->grayscale && !alpha1; + } +}; + +/** + * @brief Data structure storing the color endpoints for a block. + */ +struct endpoints +{ + /** @brief The number of partition endpoints stored. */ + unsigned int partition_count; + + /** @brief The colors for endpoint 0. */ + vfloat4 endpt0[BLOCK_MAX_PARTITIONS]; + + /** @brief The colors for endpoint 1. */ + vfloat4 endpt1[BLOCK_MAX_PARTITIONS]; +}; + +/** + * @brief Data structure storing the color endpoints and weights. + */ +struct endpoints_and_weights +{ + /** @brief True if all active values in weight_error_scale are the same. */ + bool is_constant_weight_error_scale; + + /** @brief The color endpoints. */ + endpoints ep; + + /** @brief The ideal weight for each texel; may be undecimated or decimated. */ + alignas(ASTCENC_VECALIGN) float weights[BLOCK_MAX_TEXELS]; + + /** @brief The ideal weight error scaling for each texel; may be undecimated or decimated. */ + alignas(ASTCENC_VECALIGN) float weight_error_scale[BLOCK_MAX_TEXELS]; +}; + +/** + * @brief Utility storing estimated errors from choosing particular endpoint encodings. + */ +struct encoding_choice_errors +{ + /** @brief Error of using LDR RGB-scale instead of complete endpoints. */ + float rgb_scale_error; + + /** @brief Error of using HDR RGB-scale instead of complete endpoints. */ + float rgb_luma_error; + + /** @brief Error of using luminance instead of RGB. */ + float luminance_error; + + /** @brief Error of discarding alpha and using a constant 1.0 alpha. */ + float alpha_drop_error; + + /** @brief Can we use delta offset encoding? */ + bool can_offset_encode; + + /** @brief Can we use blue contraction encoding? */ + bool can_blue_contract; +}; + +/** + * @brief Preallocated working buffers, allocated per thread during context creation. + */ +struct alignas(ASTCENC_VECALIGN) compression_working_buffers +{ + /** @brief Ideal endpoints and weights for plane 1. */ + endpoints_and_weights ei1; + + /** @brief Ideal endpoints and weights for plane 2. */ + endpoints_and_weights ei2; + + /** + * @brief Decimated ideal weight values in the ~0-1 range. + * + * Note that values can be slightly below zero or higher than one due to + * endpoint extents being inside the ideal color representation. + * + * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets. + */ + alignas(ASTCENC_VECALIGN) float dec_weights_ideal[WEIGHTS_MAX_DECIMATION_MODES * BLOCK_MAX_WEIGHTS]; + + /** + * @brief Decimated quantized weight values in the unquantized 0-64 range. + * + * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets. + */ + uint8_t dec_weights_uquant[WEIGHTS_MAX_BLOCK_MODES * BLOCK_MAX_WEIGHTS]; + + /** @brief Error of the best encoding combination for each block mode. */ + alignas(ASTCENC_VECALIGN) float errors_of_best_combination[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The best color quant for each block mode. */ + uint8_t best_quant_levels[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The best color quant for each block mode if modes are the same and we have spare bits. */ + uint8_t best_quant_levels_mod[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The best endpoint format for each partition. */ + uint8_t best_ep_formats[WEIGHTS_MAX_BLOCK_MODES][BLOCK_MAX_PARTITIONS]; + + /** @brief The total bit storage needed for quantized weights for each block mode. */ + int8_t qwt_bitcounts[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The cumulative error for quantized weights for each block mode. */ + float qwt_errors[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The low weight value in plane 1 for each block mode. */ + float weight_low_value1[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The high weight value in plane 1 for each block mode. */ + float weight_high_value1[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The low weight value in plane 1 for each quant level and decimation mode. */ + float weight_low_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; + + /** @brief The high weight value in plane 1 for each quant level and decimation mode. */ + float weight_high_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; + + /** @brief The low weight value in plane 2 for each block mode. */ + float weight_low_value2[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The high weight value in plane 2 for each block mode. */ + float weight_high_value2[WEIGHTS_MAX_BLOCK_MODES]; + + /** @brief The low weight value in plane 2 for each quant level and decimation mode. */ + float weight_low_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; + + /** @brief The high weight value in plane 2 for each quant level and decimation mode. */ + float weight_high_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; +}; + +struct dt_init_working_buffers +{ + uint8_t weight_count_of_texel[BLOCK_MAX_TEXELS]; + uint8_t grid_weights_of_texel[BLOCK_MAX_TEXELS][4]; + uint8_t weights_of_texel[BLOCK_MAX_TEXELS][4]; + + uint8_t texel_count_of_weight[BLOCK_MAX_WEIGHTS]; + uint8_t texels_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS]; + uint8_t texel_weights_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS]; +}; + +/** + * @brief Weight quantization transfer table. + * + * ASTC can store texel weights at many quantization levels, so for performance we store essential + * information about each level as a precomputed data structure. Unquantized weights are integers + * or floats in the range [0, 64]. + * + * This structure provides a table, used to estimate the closest quantized weight for a given + * floating-point weight. For each quantized weight, the corresponding unquantized values. For each + * quantized weight, a previous-value and a next-value. +*/ +struct quant_and_transfer_table +{ + /** @brief The quantization level used. */ + quant_method method; + + /** @brief The unscrambled unquantized value. */ + int8_t quant_to_unquant[32]; + + /** @brief The scrambling order: scrambled_quant = map[unscrambled_quant]. */ + int8_t scramble_map[32]; + + /** @brief The unscrambling order: unscrambled_unquant = map[scrambled_quant]. */ + int8_t unscramble_and_unquant_map[32]; + + /** + * @brief A table of previous-and-next weights, indexed by the current unquantized value. + * * bits 7:0 = previous-index, unquantized + * * bits 15:8 = next-index, unquantized + */ + uint16_t prev_next_values[65]; +}; + +/** @brief The precomputed quant and transfer table. */ +extern const quant_and_transfer_table quant_and_xfer_tables[12]; + +/** @brief The block is an error block, and will return error color or NaN. */ +static constexpr uint8_t SYM_BTYPE_ERROR { 0 }; + +/** @brief The block is a constant color block using FP16 colors. */ +static constexpr uint8_t SYM_BTYPE_CONST_F16 { 1 }; + +/** @brief The block is a constant color block using UNORM16 colors. */ +static constexpr uint8_t SYM_BTYPE_CONST_U16 { 2 }; + +/** @brief The block is a normal non-constant color block. */ +static constexpr uint8_t SYM_BTYPE_NONCONST { 3 }; + +/** + * @brief A symbolic representation of a compressed block. + * + * The symbolic representation stores the unpacked content of a single + * @c physical_compressed_block, in a form which is much easier to access for + * the rest of the compressor code. + */ +struct symbolic_compressed_block +{ + /** @brief The block type, one of the @c SYM_BTYPE_* constants. */ + uint8_t block_type; + + /** @brief The number of partitions; valid for @c NONCONST blocks. */ + uint8_t partition_count; + + /** @brief Non-zero if the color formats matched; valid for @c NONCONST blocks. */ + uint8_t color_formats_matched; + + /** @brief The plane 2 color component, or -1 if single plane; valid for @c NONCONST blocks. */ + int8_t plane2_component; + + /** @brief The block mode; valid for @c NONCONST blocks. */ + uint16_t block_mode; + + /** @brief The partition index; valid for @c NONCONST blocks if 2 or more partitions. */ + uint16_t partition_index; + + /** @brief The endpoint color formats for each partition; valid for @c NONCONST blocks. */ + uint8_t color_formats[BLOCK_MAX_PARTITIONS]; + + /** @brief The endpoint color quant mode; valid for @c NONCONST blocks. */ + quant_method quant_mode; + + /** @brief The error of the current encoding; valid for @c NONCONST blocks. */ + float errorval; + + // We can't have both of these at the same time + union { + /** @brief The constant color; valid for @c CONST blocks. */ + int constant_color[BLOCK_MAX_COMPONENTS]; + + /** @brief The quantized endpoint color pairs; valid for @c NONCONST blocks. */ + uint8_t color_values[BLOCK_MAX_PARTITIONS][8]; + }; + + /** @brief The quantized and decimated weights. + * + * Weights are stored in the 0-64 unpacked range allowing them to be used + * directly in encoding passes without per-use unpacking. Packing happens + * when converting to/from the physical bitstream encoding. + * + * If dual plane, the second plane starts at @c weights[WEIGHTS_PLANE2_OFFSET]. + */ + uint8_t weights[BLOCK_MAX_WEIGHTS]; + + /** + * @brief Get the weight quantization used by this block mode. + * + * @return The quantization level. + */ + inline quant_method get_color_quant_mode() const + { + return this->quant_mode; + } +}; + +/** + * @brief A physical representation of a compressed block. + * + * The physical representation stores the raw bytes of the format in memory. + */ +struct physical_compressed_block +{ + /** @brief The ASTC encoded data for a single block. */ + uint8_t data[16]; +}; + + +/** + * @brief Parameter structure for @c compute_pixel_region_variance(). + * + * This function takes a structure to avoid spilling arguments to the stack on every function + * invocation, as there are a lot of parameters. + */ +struct pixel_region_args +{ + /** @brief The image to analyze. */ + const astcenc_image* img; + + /** @brief The component swizzle pattern. */ + astcenc_swizzle swz; + + /** @brief Should the algorithm bother with Z axis processing? */ + bool have_z; + + /** @brief The kernel radius for alpha processing. */ + unsigned int alpha_kernel_radius; + + /** @brief The X dimension of the working data to process. */ + unsigned int size_x; + + /** @brief The Y dimension of the working data to process. */ + unsigned int size_y; + + /** @brief The Z dimension of the working data to process. */ + unsigned int size_z; + + /** @brief The X position of first src and dst data in the data set. */ + unsigned int offset_x; + + /** @brief The Y position of first src and dst data in the data set. */ + unsigned int offset_y; + + /** @brief The Z position of first src and dst data in the data set. */ + unsigned int offset_z; + + /** @brief The working memory buffer. */ + vfloat4 *work_memory; +}; + +/** + * @brief Parameter structure for @c compute_averages_proc(). + */ +struct avg_args +{ + /** @brief The arguments for the nested variance computation. */ + pixel_region_args arg; + + /** @brief The image X dimensions. */ + unsigned int img_size_x; + + /** @brief The image Y dimensions. */ + unsigned int img_size_y; + + /** @brief The image Z dimensions. */ + unsigned int img_size_z; + + /** @brief The maximum working block dimensions in X and Y dimensions. */ + unsigned int blk_size_xy; + + /** @brief The maximum working block dimensions in Z dimensions. */ + unsigned int blk_size_z; + + /** @brief The working block memory size. */ + unsigned int work_memory_size; +}; + +#if defined(ASTCENC_DIAGNOSTICS) +/* See astcenc_diagnostic_trace header for details. */ +class TraceLog; +#endif + +/** + * @brief The astcenc compression context. + */ +struct astcenc_contexti +{ + /** @brief The configuration this context was created with. */ + astcenc_config config; + + /** @brief The thread count supported by this context. */ + unsigned int thread_count; + + /** @brief The block size descriptor this context was created with. */ + block_size_descriptor* bsd; + + /* + * Fields below here are not needed in a decompress-only build, but some remain as they are + * small and it avoids littering the code with #ifdefs. The most significant contributors to + * large structure size are omitted. + */ + + /** @brief The input image alpha channel averages table, may be @c nullptr if not needed. */ + float* input_alpha_averages; + + /** @brief The scratch working buffers, one per thread (see @c thread_count). */ + compression_working_buffers* working_buffers; + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + /** @brief The pixel region and variance worker arguments. */ + avg_args avg_preprocess_args; +#endif + +#if defined(ASTCENC_DIAGNOSTICS) + /** + * @brief The diagnostic trace logger. + * + * Note that this is a singleton, so can only be used in single threaded mode. It only exists + * here so we have a reference to close the file at the end of the capture. + */ + TraceLog* trace_log; +#endif +}; + +/* ============================================================================ + Functionality for managing block sizes and partition tables. +============================================================================ */ + +/** + * @brief Populate the block size descriptor for the target block size. + * + * This will also initialize the partition table metadata, which is stored as part of the BSD + * structure. + * + * @param x_texels The number of texels in the block X dimension. + * @param y_texels The number of texels in the block Y dimension. + * @param z_texels The number of texels in the block Z dimension. + * @param can_omit_modes Can we discard modes and partitionings that astcenc won't use? + * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings. + * @param mode_cutoff The block mode percentile cutoff [0-1]. + * @param[out] bsd The descriptor to initialize. + */ +void init_block_size_descriptor( + unsigned int x_texels, + unsigned int y_texels, + unsigned int z_texels, + bool can_omit_modes, + unsigned int partition_count_cutoff, + float mode_cutoff, + block_size_descriptor& bsd); + +/** + * @brief Populate the partition tables for the target block size. + * + * Note the @c bsd descriptor must be initialized by calling @c init_block_size_descriptor() before + * calling this function. + * + * @param[out] bsd The block size information structure to populate. + * @param can_omit_partitionings True if we can we drop partitionings that astcenc won't use. + * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings. + */ +void init_partition_tables( + block_size_descriptor& bsd, + bool can_omit_partitionings, + unsigned int partition_count_cutoff); + +/** + * @brief Get the percentile table for 2D block modes. + * + * This is an empirically determined prioritization of which block modes to use in the search in + * terms of their centile (lower centiles = more useful). + * + * Returns a dynamically allocated array; caller must free with delete[]. + * + * @param xdim The block x size. + * @param ydim The block y size. + * + * @return The unpacked table. + */ +const float* get_2d_percentile_table( + unsigned int xdim, + unsigned int ydim); + +/** + * @brief Query if a 2D block size is legal. + * + * @return True if legal, false otherwise. + */ +bool is_legal_2d_block_size( + unsigned int xdim, + unsigned int ydim); + +/** + * @brief Query if a 3D block size is legal. + * + * @return True if legal, false otherwise. + */ +bool is_legal_3d_block_size( + unsigned int xdim, + unsigned int ydim, + unsigned int zdim); + +/* ============================================================================ + Functionality for managing BISE quantization and unquantization. +============================================================================ */ + +/** + * @brief The precomputed table for quantizing color values. + * + * Converts unquant value in 0-255 range into quant value in 0-255 range. + * No BISE scrambling is applied at this stage. + * + * Indexed by [quant_mode - 4][data_value]. + */ +extern const uint8_t color_unquant_to_uquant_tables[17][256]; + +/** + * @brief The precomputed table for packing quantized color values. + * + * Converts quant value in 0-255 range into packed quant value in 0-N range, + * with BISE scrambling applied. + * + * Indexed by [quant_mode - 4][data_value]. + */ +extern const uint8_t color_uquant_to_scrambled_pquant_tables[17][256]; + +/** + * @brief The precomputed table for unpacking color values. + * + * Converts quant value in 0-N range into unpacked value in 0-255 range, + * with BISE unscrambling applied. + * + * Indexed by [quant_mode - 4][data_value]. + */ +extern const uint8_t* color_scrambled_pquant_to_uquant_tables[17]; + +/** + * @brief The precomputed quant mode storage table. + * + * Indexing by [integer_count/2][bits] gives us the quantization level for a given integer count and + * number of compressed storage bits. Returns -1 for cases where the requested integer count cannot + * ever fit in the supplied storage size. + */ +extern const int8_t quant_mode_table[10][128]; + +/** + * @brief Encode a packed string using BISE. + * + * Note that BISE can return strings that are not a whole number of bytes in length, and ASTC can + * start storing strings in a block at arbitrary bit offsets in the encoded data. + * + * @param quant_level The BISE alphabet size. + * @param character_count The number of characters in the string. + * @param input_data The unpacked string, one byte per character. + * @param[in,out] output_data The output packed string. + * @param bit_offset The starting offset in the output storage. + */ +void encode_ise( + quant_method quant_level, + unsigned int character_count, + const uint8_t* input_data, + uint8_t* output_data, + unsigned int bit_offset); + +/** + * @brief Decode a packed string using BISE. + * + * Note that BISE input strings are not a whole number of bytes in length, and ASTC can start + * strings at arbitrary bit offsets in the encoded data. + * + * @param quant_level The BISE alphabet size. + * @param character_count The number of characters in the string. + * @param input_data The packed string. + * @param[in,out] output_data The output storage, one byte per character. + * @param bit_offset The starting offset in the output storage. + */ +void decode_ise( + quant_method quant_level, + unsigned int character_count, + const uint8_t* input_data, + uint8_t* output_data, + unsigned int bit_offset); + +/** + * @brief Return the number of bits needed to encode an ISE sequence. + * + * This implementation assumes that the @c quant level is untrusted, given it may come from random + * data being decompressed, so we return an arbitrary unencodable size if that is the case. + * + * @param character_count The number of items in the sequence. + * @param quant_level The desired quantization level. + * + * @return The number of bits needed to encode the BISE string. + */ +unsigned int get_ise_sequence_bitcount( + unsigned int character_count, + quant_method quant_level); + +/* ============================================================================ + Functionality for managing color partitioning. +============================================================================ */ + +/** + * @brief Compute averages and dominant directions for each partition in a 2 component texture. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param component1 The first component included in the analysis. + * @param component2 The second component included in the analysis. + * @param[out] pm The output partition metrics. + * - Only pi.partition_count array entries actually get initialized. + * - Direction vectors @c pm.dir are not normalized. + */ +void compute_avgs_and_dirs_2_comp( + const partition_info& pi, + const image_block& blk, + unsigned int component1, + unsigned int component2, + partition_metrics pm[BLOCK_MAX_PARTITIONS]); + +/** + * @brief Compute averages and dominant directions for each partition in a 3 component texture. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param omitted_component The component excluded from the analysis. + * @param[out] pm The output partition metrics. + * - Only pi.partition_count array entries actually get initialized. + * - Direction vectors @c pm.dir are not normalized. + */ +void compute_avgs_and_dirs_3_comp( + const partition_info& pi, + const image_block& blk, + unsigned int omitted_component, + partition_metrics pm[BLOCK_MAX_PARTITIONS]); + +/** + * @brief Compute averages and dominant directions for each partition in a 3 component texture. + * + * This is a specialization of @c compute_avgs_and_dirs_3_comp where the omitted component is + * always alpha, a common case during partition search. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param[out] pm The output partition metrics. + * - Only pi.partition_count array entries actually get initialized. + * - Direction vectors @c pm.dir are not normalized. + */ +void compute_avgs_and_dirs_3_comp_rgb( + const partition_info& pi, + const image_block& blk, + partition_metrics pm[BLOCK_MAX_PARTITIONS]); + +/** + * @brief Compute averages and dominant directions for each partition in a 4 component texture. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param[out] pm The output partition metrics. + * - Only pi.partition_count array entries actually get initialized. + * - Direction vectors @c pm.dir are not normalized. + */ +void compute_avgs_and_dirs_4_comp( + const partition_info& pi, + const image_block& blk, + partition_metrics pm[BLOCK_MAX_PARTITIONS]); + +/** + * @brief Compute the RGB error for uncorrelated and same chroma projections. + * + * The output of compute averages and dirs is post processed to define two lines, both of which go + * through the mean-color-value. One line has a direction defined by the dominant direction; this + * is used to assess the error from using an uncorrelated color representation. The other line goes + * through (0,0,0) and is used to assess the error from using an RGBS color representation. + * + * This function computes the squared error when using these two representations. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param[in,out] plines Processed line inputs, and line length outputs. + * @param[out] uncor_error The cumulative error for using the uncorrelated line. + * @param[out] samec_error The cumulative error for using the same chroma line. + */ +void compute_error_squared_rgb( + const partition_info& pi, + const image_block& blk, + partition_lines3 plines[BLOCK_MAX_PARTITIONS], + float& uncor_error, + float& samec_error); + +/** + * @brief Compute the RGBA error for uncorrelated and same chroma projections. + * + * The output of compute averages and dirs is post processed to define two lines, both of which go + * through the mean-color-value. One line has a direction defined by the dominant direction; this + * is used to assess the error from using an uncorrelated color representation. The other line goes + * through (0,0,0,1) and is used to assess the error from using an RGBS color representation. + * + * This function computes the squared error when using these two representations. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to be compressed. + * @param uncor_plines Processed uncorrelated partition lines for each partition. + * @param samec_plines Processed same chroma partition lines for each partition. + * @param[out] uncor_lengths The length of each components deviation from the line. + * @param[out] samec_lengths The length of each components deviation from the line. + * @param[out] uncor_error The cumulative error for using the uncorrelated line. + * @param[out] samec_error The cumulative error for using the same chroma line. + */ +void compute_error_squared_rgba( + const partition_info& pi, + const image_block& blk, + const processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS], + const processed_line4 samec_plines[BLOCK_MAX_PARTITIONS], + float uncor_lengths[BLOCK_MAX_PARTITIONS], + float samec_lengths[BLOCK_MAX_PARTITIONS], + float& uncor_error, + float& samec_error); + +/** + * @brief Find the best set of partitions to trial for a given block. + * + * On return the @c best_partitions list will contain the two best partition + * candidates; one assuming data has uncorrelated chroma and one assuming the + * data has correlated chroma. The best candidate is returned first in the list. + * + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param partition_count The number of partitions in the block. + * @param partition_search_limit The number of candidate partition encodings to trial. + * @param[out] best_partitions The best partition candidates. + * @param requested_candidates The number of requested partitionings. May return fewer if + * candidates are not available. + * + * @return The actual number of candidates returned. + */ +unsigned int find_best_partition_candidates( + const block_size_descriptor& bsd, + const image_block& blk, + unsigned int partition_count, + unsigned int partition_search_limit, + unsigned int best_partitions[TUNE_MAX_PARTITIIONING_CANDIDATES], + unsigned int requested_candidates); + +/* ============================================================================ + Functionality for managing images and image related data. +============================================================================ */ + +/** + * @brief Setup computation of regional averages in an image. + * + * This must be done by only a single thread per image, before any thread calls + * @c compute_averages(). + * + * Results are written back into @c img->input_alpha_averages. + * + * @param img The input image data, also holds output data. + * @param alpha_kernel_radius The kernel radius (in pixels) for alpha mods. + * @param swz Input data component swizzle. + * @param[out] ag The average variance arguments to init. + * + * @return The number of tasks in the processing stage. + */ +unsigned int init_compute_averages( + const astcenc_image& img, + unsigned int alpha_kernel_radius, + const astcenc_swizzle& swz, + avg_args& ag); + +/** + * @brief Compute averages for a pixel region. + * + * The routine computes both in a single pass, using a summed-area table to decouple the running + * time from the averaging/variance kernel size. + * + * @param[out] ctx The compressor context storing the output data. + * @param arg The input parameter structure. + */ +void compute_pixel_region_variance( + astcenc_contexti& ctx, + const pixel_region_args& arg); +/** + * @brief Load a single image block from the input image. + * + * @param decode_mode The compression color profile. + * @param img The input image data. + * @param[out] blk The image block to populate. + * @param bsd The block size information. + * @param xpos The block X coordinate in the input image. + * @param ypos The block Y coordinate in the input image. + * @param zpos The block Z coordinate in the input image. + * @param swz The swizzle to apply on load. + */ +void load_image_block( + astcenc_profile decode_mode, + const astcenc_image& img, + image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz); + +/** + * @brief Load a single image block from the input image. + * + * This specialized variant can be used only if the block is 2D LDR U8 data, + * with no swizzle. + * + * @param decode_mode The compression color profile. + * @param img The input image data. + * @param[out] blk The image block to populate. + * @param bsd The block size information. + * @param xpos The block X coordinate in the input image. + * @param ypos The block Y coordinate in the input image. + * @param zpos The block Z coordinate in the input image. + * @param swz The swizzle to apply on load. + */ +void load_image_block_fast_ldr( + astcenc_profile decode_mode, + const astcenc_image& img, + image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz); + +/** + * @brief Store a single image block to the output image. + * + * @param[out] img The output image data. + * @param blk The image block to export. + * @param bsd The block size information. + * @param xpos The block X coordinate in the input image. + * @param ypos The block Y coordinate in the input image. + * @param zpos The block Z coordinate in the input image. + * @param swz The swizzle to apply on store. + */ +void store_image_block( + astcenc_image& img, + const image_block& blk, + const block_size_descriptor& bsd, + unsigned int xpos, + unsigned int ypos, + unsigned int zpos, + const astcenc_swizzle& swz); + +/* ============================================================================ + Functionality for computing endpoint colors and weights for a block. +============================================================================ */ + +/** + * @brief Compute ideal endpoint colors and weights for 1 plane of weights. + * + * The ideal endpoints define a color line for the partition. For each texel the ideal weight + * defines an exact position on the partition color line. We can then use these to assess the error + * introduced by removing and quantizing the weight grid. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param[out] ei The endpoint and weight values. + */ +void compute_ideal_colors_and_weights_1plane( + const image_block& blk, + const partition_info& pi, + endpoints_and_weights& ei); + +/** + * @brief Compute ideal endpoint colors and weights for 2 planes of weights. + * + * The ideal endpoints define a color line for the partition. For each texel the ideal weight + * defines an exact position on the partition color line. We can then use these to assess the error + * introduced by removing and quantizing the weight grid. + * + * @param bsd The block size information. + * @param blk The image block color data to compress. + * @param plane2_component The component assigned to plane 2. + * @param[out] ei1 The endpoint and weight values for plane 1. + * @param[out] ei2 The endpoint and weight values for plane 2. + */ +void compute_ideal_colors_and_weights_2planes( + const block_size_descriptor& bsd, + const image_block& blk, + unsigned int plane2_component, + endpoints_and_weights& ei1, + endpoints_and_weights& ei2); + +/** + * @brief Compute the optimal unquantized weights for a decimation table. + * + * After computing ideal weights for the case for a complete weight grid, we we want to compute the + * ideal weights for the case where weights exist only for some texels. We do this with a + * steepest-descent grid solver which works as follows: + * + * First, for each actual weight, perform a weighted averaging of the texels affected by the weight. + * Then, set step size to <some initial value> and attempt one step towards the original ideal + * weight if it helps to reduce error. + * + * @param ei The non-decimated endpoints and weights. + * @param di The selected weight decimation. + * @param[out] dec_weight_ideal_value The ideal values for the decimated weight set. + */ +void compute_ideal_weights_for_decimation( + const endpoints_and_weights& ei, + const decimation_info& di, + float* dec_weight_ideal_value); + +/** + * @brief Compute the optimal quantized weights for a decimation table. + * + * We test the two closest weight indices in the allowed quantization range and keep the weight that + * is the closest match. + * + * @param di The selected weight decimation. + * @param low_bound The lowest weight allowed. + * @param high_bound The highest weight allowed. + * @param dec_weight_ideal_value The ideal weight set. + * @param[out] dec_weight_quant_uvalue The output quantized weight as a float. + * @param[out] dec_weight_uquant The output quantized weight as encoded int. + * @param quant_level The desired weight quant level. + */ +void compute_quantized_weights_for_decimation( + const decimation_info& di, + float low_bound, + float high_bound, + const float* dec_weight_ideal_value, + float* dec_weight_quant_uvalue, + uint8_t* dec_weight_uquant, + quant_method quant_level); + +/** + * @brief Compute the error of a decimated weight set for 1 plane. + * + * After computing ideal weights for the case with one weight per texel, we want to compute the + * error for decimated weight grids where weights are stored at a lower resolution. This function + * computes the error of the reduced grid, compared to the full grid. + * + * @param eai The ideal weights for the full grid. + * @param di The selected weight decimation. + * @param dec_weight_quant_uvalue The quantized weights for the decimated grid. + * + * @return The accumulated error. + */ +float compute_error_of_weight_set_1plane( + const endpoints_and_weights& eai, + const decimation_info& di, + const float* dec_weight_quant_uvalue); + +/** + * @brief Compute the error of a decimated weight set for 2 planes. + * + * After computing ideal weights for the case with one weight per texel, we want to compute the + * error for decimated weight grids where weights are stored at a lower resolution. This function + * computes the error of the reduced grid, compared to the full grid. + * + * @param eai1 The ideal weights for the full grid and plane 1. + * @param eai2 The ideal weights for the full grid and plane 2. + * @param di The selected weight decimation. + * @param dec_weight_quant_uvalue_plane1 The quantized weights for the decimated grid plane 1. + * @param dec_weight_quant_uvalue_plane2 The quantized weights for the decimated grid plane 2. + * + * @return The accumulated error. + */ +float compute_error_of_weight_set_2planes( + const endpoints_and_weights& eai1, + const endpoints_and_weights& eai2, + const decimation_info& di, + const float* dec_weight_quant_uvalue_plane1, + const float* dec_weight_quant_uvalue_plane2); + +/** + * @brief Pack a single pair of color endpoints as effectively as possible. + * + * The user requests a base color endpoint mode in @c format, but the quantizer may choose a + * delta-based representation. It will report back the format variant it actually used. + * + * @param color0 The input unquantized color0 endpoint for absolute endpoint pairs. + * @param color1 The input unquantized color1 endpoint for absolute endpoint pairs. + * @param rgbs_color The input unquantized RGBS variant endpoint for same chroma endpoints. + * @param rgbo_color The input unquantized RGBS variant endpoint for HDR endpoints. + * @param format The desired base format. + * @param[out] output The output storage for the quantized colors/ + * @param quant_level The quantization level requested. + * + * @return The actual endpoint mode used. + */ +uint8_t pack_color_endpoints( + vfloat4 color0, + vfloat4 color1, + vfloat4 rgbs_color, + vfloat4 rgbo_color, + int format, + uint8_t* output, + quant_method quant_level); + +/** + * @brief Unpack a single pair of encoded endpoints. + * + * Endpoints must be unscrambled and converted into the 0-255 range before calling this functions. + * + * @param decode_mode The decode mode (LDR, HDR). + * @param format The color endpoint mode used. + * @param input The raw array of encoded input integers. The length of this array + * depends on @c format; it can be safely assumed to be large enough. + * @param[out] rgb_hdr Is the endpoint using HDR for the RGB channels? + * @param[out] alpha_hdr Is the endpoint using HDR for the A channel? + * @param[out] output0 The output color for endpoint 0. + * @param[out] output1 The output color for endpoint 1. + */ +void unpack_color_endpoints( + astcenc_profile decode_mode, + int format, + const uint8_t* input, + bool& rgb_hdr, + bool& alpha_hdr, + vint4& output0, + vint4& output1); + +/** + * @brief Unpack a set of quantized and decimated weights. + * + * TODO: Can we skip this for non-decimated weights now that the @c scb is + * already storing unquantized weights? + * + * @param bsd The block size information. + * @param scb The symbolic compressed encoding. + * @param di The weight grid decimation table. + * @param is_dual_plane @c true if this is a dual plane block, @c false otherwise. + * @param[out] weights_plane1 The output array for storing the plane 1 weights. + * @param[out] weights_plane2 The output array for storing the plane 2 weights. + */ +void unpack_weights( + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const decimation_info& di, + bool is_dual_plane, + int weights_plane1[BLOCK_MAX_TEXELS], + int weights_plane2[BLOCK_MAX_TEXELS]); + +/** + * @brief Identify, for each mode, which set of color endpoint produces the best result. + * + * Returns the best @c tune_candidate_limit best looking modes, along with the ideal color encoding + * combination for each. The modified quantization level can be used when all formats are the same, + * as this frees up two additional bits of storage. + * + * @param pi The partition info for the current trial. + * @param blk The image block color data to compress. + * @param ep The ideal endpoints. + * @param qwt_bitcounts Bit counts for different quantization methods. + * @param qwt_errors Errors for different quantization methods. + * @param tune_candidate_limit The max number of candidates to return, may be less. + * @param start_block_mode The first block mode to inspect. + * @param end_block_mode The last block mode to inspect. + * @param[out] partition_format_specifiers The best formats per partition. + * @param[out] block_mode The best packed block mode indexes. + * @param[out] quant_level The best color quant level. + * @param[out] quant_level_mod The best color quant level if endpoints are the same. + * @param[out] tmpbuf Preallocated scratch buffers for the compressor. + * + * @return The actual number of candidate matches returned. + */ +unsigned int compute_ideal_endpoint_formats( + const partition_info& pi, + const image_block& blk, + const endpoints& ep, + const int8_t* qwt_bitcounts, + const float* qwt_errors, + unsigned int tune_candidate_limit, + unsigned int start_block_mode, + unsigned int end_block_mode, + uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS], + int block_mode[TUNE_MAX_TRIAL_CANDIDATES], + quant_method quant_level[TUNE_MAX_TRIAL_CANDIDATES], + quant_method quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES], + compression_working_buffers& tmpbuf); + +/** + * @brief For a given 1 plane weight set recompute the endpoint colors. + * + * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must + * recompute the ideal colors for a specific weight set. + * + * @param blk The image block color data to compress. + * @param pi The partition info for the current trial. + * @param di The weight grid decimation table. + * @param dec_weights_uquant The quantized weight set. + * @param[in,out] ep The color endpoints (modifed in place). + * @param[out] rgbs_vectors The RGB+scale vectors for LDR blocks. + * @param[out] rgbo_vectors The RGB+offset vectors for HDR blocks. + */ +void recompute_ideal_colors_1plane( + const image_block& blk, + const partition_info& pi, + const decimation_info& di, + const uint8_t* dec_weights_uquant, + endpoints& ep, + vfloat4 rgbs_vectors[BLOCK_MAX_PARTITIONS], + vfloat4 rgbo_vectors[BLOCK_MAX_PARTITIONS]); + +/** + * @brief For a given 2 plane weight set recompute the endpoint colors. + * + * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must + * recompute the ideal colors for a specific weight set. + * + * @param blk The image block color data to compress. + * @param bsd The block_size descriptor. + * @param di The weight grid decimation table. + * @param dec_weights_uquant_plane1 The quantized weight set for plane 1. + * @param dec_weights_uquant_plane2 The quantized weight set for plane 2. + * @param[in,out] ep The color endpoints (modifed in place). + * @param[out] rgbs_vector The RGB+scale color for LDR blocks. + * @param[out] rgbo_vector The RGB+offset color for HDR blocks. + * @param plane2_component The component assigned to plane 2. + */ +void recompute_ideal_colors_2planes( + const image_block& blk, + const block_size_descriptor& bsd, + const decimation_info& di, + const uint8_t* dec_weights_uquant_plane1, + const uint8_t* dec_weights_uquant_plane2, + endpoints& ep, + vfloat4& rgbs_vector, + vfloat4& rgbo_vector, + int plane2_component); + +/** + * @brief Expand the angular tables needed for the alternative to PCA that we use. + */ +void prepare_angular_tables(); + +/** + * @brief Compute the angular endpoints for one plane for each block mode. + * + * @param only_always Only consider block modes that are always enabled. + * @param bsd The block size descriptor for the current trial. + * @param dec_weight_ideal_value The ideal decimated unquantized weight values. + * @param max_weight_quant The maximum block mode weight quantization allowed. + * @param[out] tmpbuf Preallocated scratch buffers for the compressor. + */ +void compute_angular_endpoints_1plane( + bool only_always, + const block_size_descriptor& bsd, + const float* dec_weight_ideal_value, + unsigned int max_weight_quant, + compression_working_buffers& tmpbuf); + +/** + * @brief Compute the angular endpoints for two planes for each block mode. + * + * @param bsd The block size descriptor for the current trial. + * @param dec_weight_ideal_value The ideal decimated unquantized weight values. + * @param max_weight_quant The maximum block mode weight quantization allowed. + * @param[out] tmpbuf Preallocated scratch buffers for the compressor. + */ +void compute_angular_endpoints_2planes( + const block_size_descriptor& bsd, + const float* dec_weight_ideal_value, + unsigned int max_weight_quant, + compression_working_buffers& tmpbuf); + +/* ============================================================================ + Functionality for high level compression and decompression access. +============================================================================ */ + +/** + * @brief Compress an image block into a physical block. + * + * @param ctx The compressor context and configuration. + * @param blk The image block color data to compress. + * @param[out] pcb The physical compressed block output. + * @param[out] tmpbuf Preallocated scratch buffers for the compressor. + */ +void compress_block( + const astcenc_contexti& ctx, + const image_block& blk, + physical_compressed_block& pcb, + compression_working_buffers& tmpbuf); + +/** + * @brief Decompress a symbolic block in to an image block. + * + * @param decode_mode The decode mode (LDR, HDR, etc). + * @param bsd The block size information. + * @param xpos The X coordinate of the block in the overall image. + * @param ypos The Y coordinate of the block in the overall image. + * @param zpos The Z coordinate of the block in the overall image. + * @param[out] blk The decompressed image block color data. + */ +void decompress_symbolic_block( + astcenc_profile decode_mode, + const block_size_descriptor& bsd, + int xpos, + int ypos, + int zpos, + const symbolic_compressed_block& scb, + image_block& blk); + +/** + * @brief Compute the error between a symbolic block and the original input data. + * + * This function is specialized for 2 plane and 1 partition search. + * + * In RGBM mode this will reject blocks that attempt to encode a zero M value. + * + * @param config The compressor config. + * @param bsd The block size information. + * @param scb The symbolic compressed encoding. + * @param blk The original image block color data. + * + * @return Returns the computed error, or a negative value if the encoding + * should be rejected for any reason. + */ +float compute_symbolic_block_difference_2plane( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk); + +/** + * @brief Compute the error between a symbolic block and the original input data. + * + * This function is specialized for 1 plane and N partition search. + * + * In RGBM mode this will reject blocks that attempt to encode a zero M value. + * + * @param config The compressor config. + * @param bsd The block size information. + * @param scb The symbolic compressed encoding. + * @param blk The original image block color data. + * + * @return Returns the computed error, or a negative value if the encoding + * should be rejected for any reason. + */ +float compute_symbolic_block_difference_1plane( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk); + +/** + * @brief Compute the error between a symbolic block and the original input data. + * + * This function is specialized for 1 plane and 1 partition search. + * + * In RGBM mode this will reject blocks that attempt to encode a zero M value. + * + * @param config The compressor config. + * @param bsd The block size information. + * @param scb The symbolic compressed encoding. + * @param blk The original image block color data. + * + * @return Returns the computed error, or a negative value if the encoding + * should be rejected for any reason. + */ +float compute_symbolic_block_difference_1plane_1partition( + const astcenc_config& config, + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + const image_block& blk); + +/** + * @brief Convert a symbolic representation into a binary physical encoding. + * + * It is assumed that the symbolic encoding is valid and encodable, or + * previously flagged as an error block if an error color it to be encoded. + * + * @param bsd The block size information. + * @param scb The symbolic representation. + * @param[out] pcb The binary encoded data. + */ +void symbolic_to_physical( + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + physical_compressed_block& pcb); + +/** + * @brief Convert a binary physical encoding into a symbolic representation. + * + * This function can cope with arbitrary input data; output blocks will be + * flagged as an error block if the encoding is invalid. + * + * @param bsd The block size information. + * @param pcb The binary encoded data. + * @param[out] scb The output symbolic representation. + */ +void physical_to_symbolic( + const block_size_descriptor& bsd, + const physical_compressed_block& pcb, + symbolic_compressed_block& scb); + +/* ============================================================================ +Platform-specific functions. +============================================================================ */ +/** + * @brief Run-time detection if the host CPU supports the POPCNT extension. + * + * @return @c true if supported, @c false if not. + */ +bool cpu_supports_popcnt(); + +/** + * @brief Run-time detection if the host CPU supports F16C extension. + * + * @return @c true if supported, @c false if not. + */ +bool cpu_supports_f16c(); + +/** + * @brief Run-time detection if the host CPU supports SSE 4.1 extension. + * + * @return @c true if supported, @c false if not. + */ +bool cpu_supports_sse41(); + +/** + * @brief Run-time detection if the host CPU supports AVX 2 extension. + * + * @return @c true if supported, @c false if not. + */ +bool cpu_supports_avx2(); + +/** + * @brief Allocate an aligned memory buffer. + * + * Allocated memory must be freed by aligned_free; + * + * @param size The desired buffer size. + * @param align The desired buffer alignment; must be 2^N. + * + * @return The memory buffer pointer or nullptr on allocation failure. + */ +template<typename T> +T* aligned_malloc(size_t size, size_t align) +{ + void* ptr; + int error = 0; + +#if defined(_WIN32) + ptr = _aligned_malloc(size, align); +#else + error = posix_memalign(&ptr, align, size); +#endif + + if (error || (!ptr)) + { + return nullptr; + } + + return static_cast<T*>(ptr); +} + +/** + * @brief Free an aligned memory buffer. + * + * @param ptr The buffer to free. + */ +template<typename T> +void aligned_free(T* ptr) +{ +#if defined(_WIN32) + _aligned_free(reinterpret_cast<void*>(ptr)); +#else + free(reinterpret_cast<void*>(ptr)); +#endif +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal_entry.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal_entry.h new file mode 100644 index 00000000000..4e8794547ab --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_internal_entry.h @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions and data declarations for the outer context. + * + * The outer context includes thread-pool management, which is slower to + * compile due to increased use of C++ stdlib. The inner context used in the + * majority of the codec library does not include this. + */ + +#ifndef ASTCENC_INTERNAL_ENTRY_INCLUDED +#define ASTCENC_INTERNAL_ENTRY_INCLUDED + +#include <atomic> +#include <condition_variable> +#include <functional> +#include <mutex> + +#include "astcenc_internal.h" + +/* ============================================================================ + Parallel execution control +============================================================================ */ + +/** + * @brief A simple counter-based manager for parallel task execution. + * + * The task processing execution consists of: + * + * * A single-threaded init stage. + * * A multi-threaded processing stage. + * * A condition variable so threads can wait for processing completion. + * + * The init stage will be executed by the first thread to arrive in the critical section, there is + * no main thread in the thread pool. + * + * The processing stage uses dynamic dispatch to assign task tickets to threads on an on-demand + * basis. Threads may each therefore executed different numbers of tasks, depending on their + * processing complexity. The task queue and the task tickets are just counters; the caller must map + * these integers to an actual processing partition in a specific problem domain. + * + * The exit wait condition is needed to ensure processing has finished before a worker thread can + * progress to the next stage of the pipeline. Specifically a worker may exit the processing stage + * because there are no new tasks to assign to it while other worker threads are still processing. + * Calling @c wait() will ensure that all other worker have finished before the thread can proceed. + * + * The basic usage model: + * + * // --------- From single-threaded code --------- + * + * // Reset the tracker state + * manager->reset() + * + * // --------- From multi-threaded code --------- + * + * // Run the stage init; only first thread actually runs the lambda + * manager->init(<lambda>) + * + * do + * { + * // Request a task assignment + * uint task_count; + * uint base_index = manager->get_tasks(<granule>, task_count); + * + * // Process any tasks we were given (task_count <= granule size) + * if (task_count) + * { + * // Run the user task processing code for N tasks here + * ... + * + * // Flag these tasks as complete + * manager->complete_tasks(task_count); + * } + * } while (task_count); + * + * // Wait for all threads to complete tasks before progressing + * manager->wait() + * + * // Run the stage term; only first thread actually runs the lambda + * manager->term(<lambda>) + */ +class ParallelManager +{ +private: + /** @brief Lock used for critical section and condition synchronization. */ + std::mutex m_lock; + + /** @brief True if the stage init() step has been executed. */ + bool m_init_done; + + /** @brief True if the stage term() step has been executed. */ + bool m_term_done; + + /** @brief Condition variable for tracking stage processing completion. */ + std::condition_variable m_complete; + + /** @brief Number of tasks started, but not necessarily finished. */ + std::atomic<unsigned int> m_start_count; + + /** @brief Number of tasks finished. */ + unsigned int m_done_count; + + /** @brief Number of tasks that need to be processed. */ + unsigned int m_task_count; + +public: + /** @brief Create a new ParallelManager. */ + ParallelManager() + { + reset(); + } + + /** + * @brief Reset the tracker for a new processing batch. + * + * This must be called from single-threaded code before starting the multi-threaded processing + * operations. + */ + void reset() + { + m_init_done = false; + m_term_done = false; + m_start_count = 0; + m_done_count = 0; + m_task_count = 0; + } + + /** + * @brief Trigger the pipeline stage init step. + * + * This can be called from multi-threaded code. The first thread to hit this will process the + * initialization. Other threads will block and wait for it to complete. + * + * @param init_func Callable which executes the stage initialization. It must return the + * total number of tasks in the stage. + */ + void init(std::function<unsigned int(void)> init_func) + { + std::lock_guard<std::mutex> lck(m_lock); + if (!m_init_done) + { + m_task_count = init_func(); + m_init_done = true; + } + } + + /** + * @brief Trigger the pipeline stage init step. + * + * This can be called from multi-threaded code. The first thread to hit this will process the + * initialization. Other threads will block and wait for it to complete. + * + * @param task_count Total number of tasks needing processing. + */ + void init(unsigned int task_count) + { + std::lock_guard<std::mutex> lck(m_lock); + if (!m_init_done) + { + m_task_count = task_count; + m_init_done = true; + } + } + + /** + * @brief Request a task assignment. + * + * Assign up to @c granule tasks to the caller for processing. + * + * @param granule Maximum number of tasks that can be assigned. + * @param[out] count Actual number of tasks assigned, or zero if no tasks were assigned. + * + * @return Task index of the first assigned task; assigned tasks increment from this. + */ + unsigned int get_task_assignment(unsigned int granule, unsigned int& count) + { + unsigned int base = m_start_count.fetch_add(granule, std::memory_order_relaxed); + if (base >= m_task_count) + { + count = 0; + return 0; + } + + count = astc::min(m_task_count - base, granule); + return base; + } + + /** + * @brief Complete a task assignment. + * + * Mark @c count tasks as complete. This will notify all threads blocked on @c wait() if this + * completes the processing of the stage. + * + * @param count The number of completed tasks. + */ + void complete_task_assignment(unsigned int count) + { + // Note: m_done_count cannot use an atomic without the mutex; this has a race between the + // update here and the wait() for other threads + std::unique_lock<std::mutex> lck(m_lock); + this->m_done_count += count; + if (m_done_count == m_task_count) + { + lck.unlock(); + m_complete.notify_all(); + } + } + + /** + * @brief Wait for stage processing to complete. + */ + void wait() + { + std::unique_lock<std::mutex> lck(m_lock); + m_complete.wait(lck, [this]{ return m_done_count == m_task_count; }); + } + + /** + * @brief Trigger the pipeline stage term step. + * + * This can be called from multi-threaded code. The first thread to hit this will process the + * work pool termination. Caller must have called @c wait() prior to calling this function to + * ensure that processing is complete. + * + * @param term_func Callable which executes the stage termination. + */ + void term(std::function<void(void)> term_func) + { + std::lock_guard<std::mutex> lck(m_lock); + if (!m_term_done) + { + term_func(); + m_term_done = true; + } + } +}; + +/** + * @brief The astcenc compression context. + */ +struct astcenc_context +{ + /** @brief The context internal state. */ + astcenc_contexti context; + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + /** @brief The parallel manager for averages computation. */ + ParallelManager manage_avg; + + /** @brief The parallel manager for compression. */ + ParallelManager manage_compress; +#endif + + /** @brief The parallel manager for decompression. */ + ParallelManager manage_decompress; +}; + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.cpp new file mode 100644 index 00000000000..f276ac7e3db --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.cpp @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#include "astcenc_mathlib.h" + +/** + * @brief 64-bit rotate left. + * + * @param val The value to rotate. + * @param count The rotation, in bits. + */ +static inline uint64_t rotl(uint64_t val, int count) +{ + return (val << count) | (val >> (64 - count)); +} + +/* See header for documentation. */ +void astc::rand_init(uint64_t state[2]) +{ + state[0] = 0xfaf9e171cea1ec6bULL; + state[1] = 0xf1b318cc06af5d71ULL; +} + +/* See header for documentation. */ +uint64_t astc::rand(uint64_t state[2]) +{ + uint64_t s0 = state[0]; + uint64_t s1 = state[1]; + uint64_t res = s0 + s1; + s1 ^= s0; + state[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); + state[1] = rotl(s1, 37); + return res; +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.h new file mode 100644 index 00000000000..67e989e7f50 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.h @@ -0,0 +1,478 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/* + * This module implements a variety of mathematical data types and library + * functions used by the codec. + */ + +#ifndef ASTC_MATHLIB_H_INCLUDED +#define ASTC_MATHLIB_H_INCLUDED + +#include <cassert> +#include <cstdint> +#include <cmath> + +#ifndef ASTCENC_POPCNT + #if defined(__POPCNT__) + #define ASTCENC_POPCNT 1 + #else + #define ASTCENC_POPCNT 0 + #endif +#endif + +#ifndef ASTCENC_F16C + #if defined(__F16C__) + #define ASTCENC_F16C 1 + #else + #define ASTCENC_F16C 0 + #endif +#endif + +#ifndef ASTCENC_SSE + #if defined(__SSE4_2__) + #define ASTCENC_SSE 42 + #elif defined(__SSE4_1__) + #define ASTCENC_SSE 41 + #elif defined(__SSE3__) + #define ASTCENC_SSE 30 + #elif defined(__SSE2__) + #define ASTCENC_SSE 20 + #else + #define ASTCENC_SSE 0 + #endif +#endif + +#ifndef ASTCENC_AVX + #if defined(__AVX2__) + #define ASTCENC_AVX 2 + #elif defined(__AVX__) + #define ASTCENC_AVX 1 + #else + #define ASTCENC_AVX 0 + #endif +#endif + +#ifndef ASTCENC_NEON + #if defined(__aarch64__) + #define ASTCENC_NEON 1 + #else + #define ASTCENC_NEON 0 + #endif +#endif + +#if ASTCENC_AVX + #define ASTCENC_VECALIGN 32 +#else + #define ASTCENC_VECALIGN 16 +#endif + +#if ASTCENC_SSE != 0 || ASTCENC_AVX != 0 || ASTCENC_POPCNT != 0 + #include <immintrin.h> +#endif + +/* ============================================================================ + Fast math library; note that many of the higher-order functions in this set + use approximations which are less accurate, but faster, than <cmath> standard + library equivalents. + + Note: Many of these are not necessarily faster than simple C versions when + used on a single scalar value, but are included for testing purposes as most + have an option based on SSE intrinsics and therefore provide an obvious route + to future vectorization. +============================================================================ */ + +// Union for manipulation of float bit patterns +typedef union +{ + uint32_t u; + int32_t s; + float f; +} if32; + +// These are namespaced to avoid colliding with C standard library functions. +namespace astc +{ + +static const float PI = 3.14159265358979323846f; +static const float PI_OVER_TWO = 1.57079632679489661923f; + +/** + * @brief SP float absolute value. + * + * @param v The value to make absolute. + * + * @return The absolute value. + */ +static inline float fabs(float v) +{ + return std::fabs(v); +} + +/** + * @brief Test if a float value is a nan. + * + * @param v The value test. + * + * @return Zero is not a NaN, non-zero otherwise. + */ +static inline bool isnan(float v) +{ + return v != v; +} + +/** + * @brief Return the minimum of two values. + * + * For floats, NaNs are turned into @c q. + * + * @param p The first value to compare. + * @param q The second value to compare. + * + * @return The smallest value. + */ +template<typename T> +static inline T min(T p, T q) +{ + return p < q ? p : q; +} + +/** + * @brief Return the minimum of three values. + * + * For floats, NaNs are turned into @c r. + * + * @param p The first value to compare. + * @param q The second value to compare. + * @param r The third value to compare. + * + * @return The smallest value. + */ +template<typename T> +static inline T min(T p, T q, T r) +{ + return min(min(p, q), r); +} + +/** + * @brief Return the minimum of four values. + * + * For floats, NaNs are turned into @c s. + * + * @param p The first value to compare. + * @param q The second value to compare. + * @param r The third value to compare. + * @param s The fourth value to compare. + * + * @return The smallest value. + */ +template<typename T> +static inline T min(T p, T q, T r, T s) +{ + return min(min(p, q), min(r, s)); +} + +/** + * @brief Return the maximum of two values. + * + * For floats, NaNs are turned into @c q. + * + * @param p The first value to compare. + * @param q The second value to compare. + * + * @return The largest value. + */ +template<typename T> +static inline T max(T p, T q) +{ + return p > q ? p : q; +} + +/** + * @brief Return the maximum of three values. + * + * For floats, NaNs are turned into @c r. + * + * @param p The first value to compare. + * @param q The second value to compare. + * @param r The third value to compare. + * + * @return The largest value. + */ +template<typename T> +static inline T max(T p, T q, T r) +{ + return max(max(p, q), r); +} + +/** + * @brief Return the maximum of four values. + * + * For floats, NaNs are turned into @c s. + * + * @param p The first value to compare. + * @param q The second value to compare. + * @param r The third value to compare. + * @param s The fourth value to compare. + * + * @return The largest value. + */ +template<typename T> +static inline T max(T p, T q, T r, T s) +{ + return max(max(p, q), max(r, s)); +} + +/** + * @brief Clamp a value value between @c mn and @c mx. + * + * For floats, NaNs are turned into @c mn. + * + * @param v The value to clamp. + * @param mn The min value (inclusive). + * @param mx The max value (inclusive). + * + * @return The clamped value. + */ +template<typename T> +inline T clamp(T v, T mn, T mx) +{ + // Do not reorder; correct NaN handling relies on the fact that comparison + // with NaN returns false and will fall-though to the "min" value. + if (v > mx) return mx; + if (v > mn) return v; + return mn; +} + +/** + * @brief Clamp a float value between 0.0f and 1.0f. + * + * NaNs are turned into 0.0f. + * + * @param v The value to clamp. + * + * @return The clamped value. + */ +static inline float clamp1f(float v) +{ + return astc::clamp(v, 0.0f, 1.0f); +} + +/** + * @brief Clamp a float value between 0.0f and 255.0f. + * + * NaNs are turned into 0.0f. + * + * @param v The value to clamp. + * + * @return The clamped value. + */ +static inline float clamp255f(float v) +{ + return astc::clamp(v, 0.0f, 255.0f); +} + +/** + * @brief SP float round-down. + * + * @param v The value to round. + * + * @return The rounded value. + */ +static inline float flt_rd(float v) +{ + return std::floor(v); +} + +/** + * @brief SP float round-to-nearest and convert to integer. + * + * @param v The value to round. + * + * @return The rounded value. + */ +static inline int flt2int_rtn(float v) +{ + + return static_cast<int>(v + 0.5f); +} + +/** + * @brief SP float round down and convert to integer. + * + * @param v The value to round. + * + * @return The rounded value. + */ +static inline int flt2int_rd(float v) +{ + return static_cast<int>(v); +} + +/** + * @brief SP float bit-interpreted as an integer. + * + * @param v The value to bitcast. + * + * @return The converted value. + */ +static inline int float_as_int(float v) +{ + union { int a; float b; } u; + u.b = v; + return u.a; +} + +/** + * @brief Integer bit-interpreted as an SP float. + * + * @param v The value to bitcast. + * + * @return The converted value. + */ +static inline float int_as_float(int v) +{ + union { int a; float b; } u; + u.a = v; + return u.b; +} + +/** + * @brief Fast approximation of 1.0 / sqrt(val). + * + * @param v The input value. + * + * @return The approximated result. + */ +static inline float rsqrt(float v) +{ + return 1.0f / std::sqrt(v); +} + +/** + * @brief Fast approximation of sqrt(val). + * + * @param v The input value. + * + * @return The approximated result. + */ +static inline float sqrt(float v) +{ + return std::sqrt(v); +} + +/** + * @brief Extract mantissa and exponent of a float value. + * + * @param v The input value. + * @param[out] expo The output exponent. + * + * @return The mantissa. + */ +static inline float frexp(float v, int* expo) +{ + if32 p; + p.f = v; + *expo = ((p.u >> 23) & 0xFF) - 126; + p.u = (p.u & 0x807fffff) | 0x3f000000; + return p.f; +} + +/** + * @brief Initialize the seed structure for a random number generator. + * + * Important note: For the purposes of ASTC we want sets of random numbers to + * use the codec, but we want the same seed value across instances and threads + * to ensure that image output is stable across compressor runs and across + * platforms. Every PRNG created by this call will therefore return the same + * sequence of values ... + * + * @param state The state structure to initialize. + */ +void rand_init(uint64_t state[2]); + +/** + * @brief Return the next random number from the generator. + * + * This RNG is an implementation of the "xoroshoro-128+ 1.0" PRNG, based on the + * public-domain implementation given by David Blackman & Sebastiano Vigna at + * http://vigna.di.unimi.it/xorshift/xoroshiro128plus.c + * + * @param state The state structure to use/update. + */ +uint64_t rand(uint64_t state[2]); + +} + +/* ============================================================================ + Softfloat library with fp32 and fp16 conversion functionality. +============================================================================ */ +#if (ASTCENC_F16C == 0) && (ASTCENC_NEON == 0) + /* narrowing float->float conversions */ + uint16_t float_to_sf16(float val); + float sf16_to_float(uint16_t val); +#endif + +/********************************* + Vector library +*********************************/ +#include "astcenc_vecmathlib.h" + +/********************************* + Declaration of line types +*********************************/ +// parametric line, 2D: The line is given by line = a + b * t. + +struct line2 +{ + vfloat4 a; + vfloat4 b; +}; + +// parametric line, 3D +struct line3 +{ + vfloat4 a; + vfloat4 b; +}; + +struct line4 +{ + vfloat4 a; + vfloat4 b; +}; + + +struct processed_line2 +{ + vfloat4 amod; + vfloat4 bs; +}; + +struct processed_line3 +{ + vfloat4 amod; + vfloat4 bs; +}; + +struct processed_line4 +{ + vfloat4 amod; + vfloat4 bs; +}; + +#endif diff --git a/3rdparty/bimg/3rdparty/astc/softfloat.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib_softfloat.cpp index 11131bcac37..42db7645496 100644 --- a/3rdparty/bimg/3rdparty/astc/softfloat.cpp +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_mathlib_softfloat.cpp @@ -1,21 +1,33 @@ -/*----------------------------------------------------------------------------*/ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + /** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Soft IEEE-754 floating point library. + * @brief Soft-float library for IEEE-754. */ -/*----------------------------------------------------------------------------*/ +#if (ASTCENC_F16C == 0) && (ASTCENC_NEON == 0) -#include "softfloat.h" +#include "astcenc_mathlib.h" -#define SOFTFLOAT_INLINE +/* sized soft-float types. These are mapped to the sized integer + types of C99, instead of C's floating-point types; this is because + the library needs to maintain exact, bit-level control on all + operations on these data types. */ +typedef uint16_t sf16; +typedef uint32_t sf32; /****************************************** helper functions and their lookup tables @@ -48,14 +60,13 @@ }; #endif - /* 32-bit count-leading-zeros function: use the Assembly instruction whenever possible. */ -SOFTFLOAT_INLINE uint32_t clz32(uint32_t inp) +static uint32_t clz32(uint32_t inp) { #if defined(__GNUC__) && (defined(__i386) || defined(__amd64)) uint32_t bsr; - __asm__("bsrl %1, %0": "=r"(bsr):"r"(inp | 1)); + __asm__("bsrl %1, %0": "=r"(bsr):"r"(inp | 1)); return 31 - bsr; #else #if defined(__arm__) && defined(__ARMCC_VERSION) @@ -63,7 +74,7 @@ SOFTFLOAT_INLINE uint32_t clz32(uint32_t inp) #else #if defined(__arm__) && defined(__GNUC__) uint32_t lz; - __asm__("clz %0, %1": "=r"(lz):"r"(inp)); + __asm__("clz %0, %1": "=r"(lz):"r"(inp)); return lz; #else /* slow default version */ @@ -84,7 +95,18 @@ SOFTFLOAT_INLINE uint32_t clz32(uint32_t inp) #endif } -static SOFTFLOAT_INLINE uint32_t rtne_shift32(uint32_t inp, uint32_t shamt) +/* the five rounding modes that IEEE-754r defines */ +typedef enum +{ + SF_UP = 0, /* round towards positive infinity */ + SF_DOWN = 1, /* round towards negative infinity */ + SF_TOZERO = 2, /* round towards zero */ + SF_NEARESTEVEN = 3, /* round toward nearest value; if mid-between, round to even value */ + SF_NEARESTAWAY = 4 /* round toward nearest value; if mid-between, round away from zero */ +} roundmode; + + +static uint32_t rtne_shift32(uint32_t inp, uint32_t shamt) { uint32_t vl1 = UINT32_C(1) << shamt; uint32_t inp2 = inp + (vl1 >> 1); /* added 0.5 ULP */ @@ -95,7 +117,7 @@ static SOFTFLOAT_INLINE uint32_t rtne_shift32(uint32_t inp, uint32_t shamt) return inp2; } -static SOFTFLOAT_INLINE uint32_t rtna_shift32(uint32_t inp, uint32_t shamt) +static uint32_t rtna_shift32(uint32_t inp, uint32_t shamt) { uint32_t vl1 = (UINT32_C(1) << shamt) >> 1; inp += vl1; @@ -103,8 +125,7 @@ static SOFTFLOAT_INLINE uint32_t rtna_shift32(uint32_t inp, uint32_t shamt) return inp; } - -static SOFTFLOAT_INLINE uint32_t rtup_shift32(uint32_t inp, uint32_t shamt) +static uint32_t rtup_shift32(uint32_t inp, uint32_t shamt) { uint32_t vl1 = UINT32_C(1) << shamt; inp += vl1; @@ -113,11 +134,8 @@ static SOFTFLOAT_INLINE uint32_t rtup_shift32(uint32_t inp, uint32_t shamt) return inp; } - - - /* convert from FP16 to FP32. */ -sf32 sf16_to_sf32(sf16 inp) +static sf32 sf16_to_sf32(sf16 inp) { uint32_t inpx = inp; @@ -129,50 +147,54 @@ sf32 sf16_to_sf32(sf16 inp) with just 1 table lookup, 2 shifts and 1 add. */ - #define WITH_MB(a) INT32_C((a) | (1 << 31)) - static const int32_t tbl[64] = + #define WITH_MSB(a) (UINT32_C(a) | (1u << 31)) + static const uint32_t tbl[64] = { - WITH_MB(0x00000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), - INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), - INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), - INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), INT32_C(0x1C000), WITH_MB(0x38000), - WITH_MB(0x38000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), - INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), - INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), - INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), INT32_C(0x54000), WITH_MB(0x70000) + WITH_MSB(0x00000), 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, + 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, + 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, + 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, WITH_MSB(0x38000), + WITH_MSB(0x38000), 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, + 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, + 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, + 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, WITH_MSB(0x70000) }; - int32_t res = tbl[inpx >> 10]; + uint32_t res = tbl[inpx >> 10]; res += inpx; - /* the normal cases: the MSB of 'res' is not set. */ - if (res >= 0) /* signed compare */ + /* Normal cases: MSB of 'res' not set. */ + if ((res & WITH_MSB(0)) == 0) + { return res << 13; + } - /* Infinity and Zero: the bottom 10 bits of 'res' are clear. */ - if ((res & UINT32_C(0x3FF)) == 0) + /* Infinity and Zero: 10 LSB of 'res' not set. */ + if ((res & 0x3FF) == 0) + { return res << 13; + } - /* NaN: the exponent field of 'inp' is not zero; NaNs must be quietened. */ + /* NaN: the exponent field of 'inp' is non-zero. */ if ((inpx & 0x7C00) != 0) - return (res << 13) | UINT32_C(0x400000); - - /* the remaining cases are Denormals. */ { - uint32_t sign = (inpx & UINT32_C(0x8000)) << 16; - uint32_t mskval = inpx & UINT32_C(0x7FFF); - uint32_t leadingzeroes = clz32(mskval); - mskval <<= leadingzeroes; - return (mskval >> 8) + ((0x85 - leadingzeroes) << 23) + sign; + /* All NaNs are quietened. */ + return (res << 13) | 0x400000; } + + /* Denormal cases */ + uint32_t sign = (inpx & 0x8000) << 16; + uint32_t mskval = inpx & 0x7FFF; + uint32_t leadingzeroes = clz32(mskval); + mskval <<= leadingzeroes; + return (mskval >> 8) + ((0x85 - leadingzeroes) << 23) + sign; } /* Conversion routine that converts from FP32 to FP16. It supports denormals and all rounding modes. If a NaN is given as input, it is quietened. */ - -sf16 sf32_to_sf16(sf32 inp, roundmode rmode) +static sf16 sf32_to_sf16(sf32 inp, roundmode rmode) { /* for each possible sign/exponent combination, store a case index. This gives a 512-byte table */ - static const uint8_t tab[512] = { + static const uint8_t tab[512] { 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, @@ -210,7 +232,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) /* many of the cases below use a case-dependent magic constant. So we look up a magic constant before actually performing the switch. This table allows us to group cases, thereby minimizing code size. */ - static const uint32_t tabx[60] = { + static const uint32_t tabx[60] { UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x80000000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(1), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x8001), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), @@ -228,7 +250,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) switch (idx) { /* - Positive number which may be Infinity or NaN. + Positive number which may be Infinity or NaN. We need to check whether it is NaN; if it is, quieten it by setting the top bit of the mantissa. (If we don't do this quieting, then a NaN that is distinguished only by having its low-order bits set, would be turned into an INF. */ @@ -251,7 +273,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) of the mantissa is set.) */ p = (inp - 1) & UINT32_C(0x800000); /* zero if INF, nonzero if NaN. */ - return ((inp + vlx) >> 13) | (p >> 14); + return static_cast<sf16>(((inp + vlx) >> 13) | (p >> 14)); /* positive, exponent = 0, round-mode == UP; need to check whether number actually is 0. If it is, then return 0, else return 1 (the smallest representable nonzero number) @@ -261,7 +283,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) -inp will set the MSB if the input number is nonzero. Thus (-inp) >> 31 will turn into 0 if the input number is 0 and 1 otherwise. */ - return (uint32_t) (-(int32_t) inp) >> 31; + return static_cast<sf16>(static_cast<uint32_t>((-static_cast<int32_t>(inp))) >> 31); /* negative, exponent = , round-mode == DOWN, need to check whether number is @@ -274,7 +296,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) the MSB set if it isn't. We then right-shift the value by 31 places to get a value that is 0 if the input is -0.0 and 1 otherwise. */ - return ((vlx - inp) >> 31) + UINT32_C(0x8000); + return static_cast<sf16>(((vlx - inp) >> 31) + UINT32_C(0x8000)); /* for all other cases involving underflow/overflow, we don't need to @@ -308,7 +330,7 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) case 47: case 48: case 49: - return vlx; + return static_cast<sf16>(vlx); /* for normal numbers, 'vlx' is the difference between the FP32 value of a number and the @@ -327,14 +349,14 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) case 36: case 37: case 39: - return (inp + vlx) >> 13; + return static_cast<sf16>((inp + vlx) >> 13); /* normal number, round-to-nearest-even. */ case 33: case 38: p = inp + vlx; p += (inp >> 13) & 1; - return p >> 13; + return static_cast<sf16>(p >> 13); /* the various denormal cases. These are not expected to be common, so their performance is a bit @@ -349,39 +371,29 @@ sf16 sf32_to_sf16(sf32 inp, roundmode rmode) case 27: /* denormal, round towards zero. */ p = 126 - ((inp >> 23) & 0xFF); - return (((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000)) >> p) | vlx; + return static_cast<sf16>((((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000)) >> p) | vlx); case 20: case 26: /* denormal, round away from zero. */ p = 126 - ((inp >> 23) & 0xFF); - return rtup_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx; + return static_cast<sf16>(rtup_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx); case 24: case 29: /* denormal, round to nearest-away */ p = 126 - ((inp >> 23) & 0xFF); - return rtna_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx; + return static_cast<sf16>(rtna_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx); case 23: case 28: /* denormal, round to nearest-even. */ p = 126 - ((inp >> 23) & 0xFF); - return rtne_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx; + return static_cast<sf16>(rtne_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx); } return 0; } - - -typedef union if32_ -{ - uint32_t u; - int32_t s; - float f; -} if32; - /* convert from soft-float to native-float */ - -float sf16_to_float(sf16 p) +float sf16_to_float(uint16_t p) { if32 i; i.u = sf16_to_sf32(p); @@ -389,10 +401,11 @@ float sf16_to_float(sf16 p) } /* convert from native-float to soft-float */ - -sf16 float_to_sf16(float p, roundmode rm) +uint16_t float_to_sf16(float p) { if32 i; i.f = p; - return sf32_to_sf16(i.u, rm); + return sf32_to_sf16(i.u, SF_NEARESTEVEN); } + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_partition_tables.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_partition_tables.cpp new file mode 100644 index 00000000000..5846a2632c4 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_partition_tables.cpp @@ -0,0 +1,461 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for generating partition tables on demand. + */ + +#include "astcenc_internal.h" + +/** + * @brief Generate a canonical representation of a partition pattern. + * + * The returned value stores two bits per texel, for up to 6x6x6 texels, where the two bits store + * the remapped texel index. Remapping ensures that we only match on the partition pattern, + * independent of the partition order generated by the hash. + * + * @param texel_count The number of texels in the block. + * @param partition_of_texel The partition assignments, in hash order. + * @param[out] bit_pattern The output bit pattern representation. + */ +static void generate_canonical_partitioning( + unsigned int texel_count, + const uint8_t* partition_of_texel, + uint64_t bit_pattern[7] +) { + // Clear the pattern + for (unsigned int i = 0; i < 7; i++) + { + bit_pattern[i] = 0; + } + + // Store a mapping to reorder the raw partitions so that the the partitions are ordered such + // that the lowest texel index in partition N is smaller than the lowest texel index in + // partition N + 1. + int mapped_index[BLOCK_MAX_PARTITIONS]; + int map_weight_count = 0; + + for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONS; i++) + { + mapped_index[i] = -1; + } + + for (unsigned int i = 0; i < texel_count; i++) + { + int index = partition_of_texel[i]; + if (mapped_index[index] < 0) + { + mapped_index[index] = map_weight_count++; + } + + uint64_t xlat_index = mapped_index[index]; + bit_pattern[i >> 5] |= xlat_index << (2 * (i & 0x1F)); + } +} + +/** + * @brief Compare two canonical patterns to see if they are the same. + * + * @param part1 The first canonical bit pattern to check. + * @param part2 The second canonical bit pattern to check. + * + * @return @c true if the patterns are the same, @c false otherwise. + */ +static bool compare_canonical_partitionings( + const uint64_t part1[7], + const uint64_t part2[7] +) { + return (part1[0] == part2[0]) && (part1[1] == part2[1]) && + (part1[2] == part2[2]) && (part1[3] == part2[3]) && + (part1[4] == part2[4]) && (part1[5] == part2[5]) && + (part1[6] == part2[6]); +} + +/** + * @brief Hash function used for procedural partition assignment. + * + * @param inp The hash seed. + * + * @return The hashed value. + */ +static uint32_t hash52( + uint32_t inp +) { + inp ^= inp >> 15; + + // (2^4 + 1) * (2^7 + 1) * (2^17 - 1) + inp *= 0xEEDE0891; + inp ^= inp >> 5; + inp += inp << 16; + inp ^= inp >> 7; + inp ^= inp >> 3; + inp ^= inp << 6; + inp ^= inp >> 17; + return inp; +} + +/** + * @brief Select texel assignment for a single coordinate. + * + * @param seed The seed - the partition index from the block. + * @param x The texel X coordinate in the block. + * @param y The texel Y coordinate in the block. + * @param z The texel Z coordinate in the block. + * @param partition_count The total partition count of this encoding. + * @param small_block @c true if the blockhas fewer than 32 texels. + * + * @return The assigned partition index for this texel. + */ +static uint8_t select_partition( + int seed, + int x, + int y, + int z, + int partition_count, + bool small_block +) { + // For small blocks bias the coordinates to get better distribution + if (small_block) + { + x <<= 1; + y <<= 1; + z <<= 1; + } + + seed += (partition_count - 1) * 1024; + + uint32_t rnum = hash52(seed); + + uint8_t seed1 = rnum & 0xF; + uint8_t seed2 = (rnum >> 4) & 0xF; + uint8_t seed3 = (rnum >> 8) & 0xF; + uint8_t seed4 = (rnum >> 12) & 0xF; + uint8_t seed5 = (rnum >> 16) & 0xF; + uint8_t seed6 = (rnum >> 20) & 0xF; + uint8_t seed7 = (rnum >> 24) & 0xF; + uint8_t seed8 = (rnum >> 28) & 0xF; + uint8_t seed9 = (rnum >> 18) & 0xF; + uint8_t seed10 = (rnum >> 22) & 0xF; + uint8_t seed11 = (rnum >> 26) & 0xF; + uint8_t seed12 = ((rnum >> 30) | (rnum << 2)) & 0xF; + + // Squaring all the seeds in order to bias their distribution towards lower values. + seed1 *= seed1; + seed2 *= seed2; + seed3 *= seed3; + seed4 *= seed4; + seed5 *= seed5; + seed6 *= seed6; + seed7 *= seed7; + seed8 *= seed8; + seed9 *= seed9; + seed10 *= seed10; + seed11 *= seed11; + seed12 *= seed12; + + int sh1, sh2; + if (seed & 1) + { + sh1 = (seed & 2 ? 4 : 5); + sh2 = (partition_count == 3 ? 6 : 5); + } + else + { + sh1 = (partition_count == 3 ? 6 : 5); + sh2 = (seed & 2 ? 4 : 5); + } + + int sh3 = (seed & 0x10) ? sh1 : sh2; + + seed1 >>= sh1; + seed2 >>= sh2; + seed3 >>= sh1; + seed4 >>= sh2; + seed5 >>= sh1; + seed6 >>= sh2; + seed7 >>= sh1; + seed8 >>= sh2; + + seed9 >>= sh3; + seed10 >>= sh3; + seed11 >>= sh3; + seed12 >>= sh3; + + int a = seed1 * x + seed2 * y + seed11 * z + (rnum >> 14); + int b = seed3 * x + seed4 * y + seed12 * z + (rnum >> 10); + int c = seed5 * x + seed6 * y + seed9 * z + (rnum >> 6); + int d = seed7 * x + seed8 * y + seed10 * z + (rnum >> 2); + + // Apply the saw + a &= 0x3F; + b &= 0x3F; + c &= 0x3F; + d &= 0x3F; + + // Remove some of the components if we are to output < 4 partitions. + if (partition_count <= 3) + { + d = 0; + } + + if (partition_count <= 2) + { + c = 0; + } + + if (partition_count <= 1) + { + b = 0; + } + + uint8_t partition; + if (a >= b && a >= c && a >= d) + { + partition = 0; + } + else if (b >= c && b >= d) + { + partition = 1; + } + else if (c >= d) + { + partition = 2; + } + else + { + partition = 3; + } + + return partition; +} + +/** + * @brief Generate a single partition info structure. + * + * @param[out] bsd The block size information. + * @param partition_count The partition count of this partitioning. + * @param partition_index The partition index / seed of this partitioning. + * @param partition_remap_index The remapped partition index of this partitioning. + * @param[out] pi The partition info structure to populate. + * + * @return True if this is a useful partition index, False if we can skip it. + */ +static bool generate_one_partition_info_entry( + block_size_descriptor& bsd, + unsigned int partition_count, + unsigned int partition_index, + unsigned int partition_remap_index, + partition_info& pi +) { + int texels_per_block = bsd.texel_count; + bool small_block = texels_per_block < 32; + + uint8_t *partition_of_texel = pi.partition_of_texel; + + // Assign texels to partitions + int texel_idx = 0; + int counts[BLOCK_MAX_PARTITIONS] { 0 }; + for (unsigned int z = 0; z < bsd.zdim; z++) + { + for (unsigned int y = 0; y < bsd.ydim; y++) + { + for (unsigned int x = 0; x < bsd.xdim; x++) + { + uint8_t part = select_partition(partition_index, x, y, z, partition_count, small_block); + pi.texels_of_partition[part][counts[part]++] = static_cast<uint8_t>(texel_idx++); + *partition_of_texel++ = part; + } + } + } + + // Fill loop tail so we can overfetch later + for (unsigned int i = 0; i < partition_count; i++) + { + int ptex_count = counts[i]; + int ptex_count_simd = round_up_to_simd_multiple_vla(ptex_count); + for (int j = ptex_count; j < ptex_count_simd; j++) + { + pi.texels_of_partition[i][j] = pi.texels_of_partition[i][ptex_count - 1]; + } + } + + // Populate the actual procedural partition count + if (counts[0] == 0) + { + pi.partition_count = 0; + } + else if (counts[1] == 0) + { + pi.partition_count = 1; + } + else if (counts[2] == 0) + { + pi.partition_count = 2; + } + else if (counts[3] == 0) + { + pi.partition_count = 3; + } + else + { + pi.partition_count = 4; + } + + // Populate the partition index + pi.partition_index = static_cast<uint16_t>(partition_index); + + // Populate the coverage bitmaps for 2/3/4 partitions + uint64_t* bitmaps { nullptr }; + if (partition_count == 2) + { + bitmaps = bsd.coverage_bitmaps_2[partition_remap_index]; + } + else if (partition_count == 3) + { + bitmaps = bsd.coverage_bitmaps_3[partition_remap_index]; + } + else if (partition_count == 4) + { + bitmaps = bsd.coverage_bitmaps_4[partition_remap_index]; + } + + for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONS; i++) + { + pi.partition_texel_count[i] = static_cast<uint8_t>(counts[i]); + } + + // Valid partitionings have texels in all of the requested partitions + bool valid = pi.partition_count == partition_count; + + if (bitmaps) + { + // Populate the partition coverage bitmap + for (unsigned int i = 0; i < partition_count; i++) + { + bitmaps[i] = 0ULL; + } + + unsigned int texels_to_process = astc::min(bsd.texel_count, BLOCK_MAX_KMEANS_TEXELS); + for (unsigned int i = 0; i < texels_to_process; i++) + { + unsigned int idx = bsd.kmeans_texels[i]; + bitmaps[pi.partition_of_texel[idx]] |= 1ULL << i; + } + } + + return valid; +} + +static void build_partition_table_for_one_partition_count( + block_size_descriptor& bsd, + bool can_omit_partitionings, + unsigned int partition_count_cutoff, + unsigned int partition_count, + partition_info* ptab, + uint64_t* canonical_patterns +) { + unsigned int next_index = 0; + bsd.partitioning_count_selected[partition_count - 1] = 0; + bsd.partitioning_count_all[partition_count - 1] = 0; + + // Skip tables larger than config max partition count if we can omit modes + if (can_omit_partitionings && (partition_count > partition_count_cutoff)) + { + return; + } + + // Iterate through twice + // - Pass 0: Keep selected partitionings + // - Pass 1: Keep non-selected partitionings (skip if in omit mode) + unsigned int max_iter = can_omit_partitionings ? 1 : 2; + + // Tracker for things we built in the first iteration + uint8_t build[BLOCK_MAX_PARTITIONINGS] { 0 }; + for (unsigned int x = 0; x < max_iter; x++) + { + for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONINGS; i++) + { + // Don't include things we built in the first pass + if ((x == 1) && build[i]) + { + continue; + } + + bool keep_useful = generate_one_partition_info_entry(bsd, partition_count, i, next_index, ptab[next_index]); + if ((x == 0) && !keep_useful) + { + continue; + } + + generate_canonical_partitioning(bsd.texel_count, ptab[next_index].partition_of_texel, canonical_patterns + next_index * 7); + bool keep_canonical = true; + for (unsigned int j = 0; j < next_index; j++) + { + bool match = compare_canonical_partitionings(canonical_patterns + 7 * next_index, canonical_patterns + 7 * j); + if (match) + { + keep_canonical = false; + break; + } + } + + if (keep_useful && keep_canonical) + { + if (x == 0) + { + bsd.partitioning_packed_index[partition_count - 2][i] = static_cast<uint16_t>(next_index); + bsd.partitioning_count_selected[partition_count - 1]++; + bsd.partitioning_count_all[partition_count - 1]++; + build[i] = 1; + next_index++; + } + } + else + { + if (x == 1) + { + bsd.partitioning_packed_index[partition_count - 2][i] = static_cast<uint16_t>(next_index); + bsd.partitioning_count_all[partition_count - 1]++; + next_index++; + } + } + } + } +} + +/* See header for documentation. */ +void init_partition_tables( + block_size_descriptor& bsd, + bool can_omit_partitionings, + unsigned int partition_count_cutoff +) { + partition_info* par_tab2 = bsd.partitionings; + partition_info* par_tab3 = par_tab2 + BLOCK_MAX_PARTITIONINGS; + partition_info* par_tab4 = par_tab3 + BLOCK_MAX_PARTITIONINGS; + partition_info* par_tab1 = par_tab4 + BLOCK_MAX_PARTITIONINGS; + + generate_one_partition_info_entry(bsd, 1, 0, 0, *par_tab1); + bsd.partitioning_count_selected[0] = 1; + bsd.partitioning_count_all[0] = 1; + + uint64_t* canonical_patterns = new uint64_t[BLOCK_MAX_PARTITIONINGS * 7]; + build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 2, par_tab2, canonical_patterns); + build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 3, par_tab3, canonical_patterns); + build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 4, par_tab4, canonical_patterns); + + delete[] canonical_patterns; +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_percentile_tables.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_percentile_tables.cpp new file mode 100644 index 00000000000..3914ef24b8b --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_percentile_tables.cpp @@ -0,0 +1,1251 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Percentile data tables for different block encodings. + * + * To reduce binary size the tables are stored using a packed differential encoding. + */ + +#include "astcenc_internal.h" + +#if !defined(ASTCENC_DECOMPRESS_ONLY) +/** + * @brief Structure containing packed percentile metadata. + * + * Note that percentile tables do not exist for 3D textures, so no zdim is stored. + */ +struct packed_percentile_table +{ + /** The block X dimension. */ + uint8_t xdim; + + /** The block Y dimension. */ + uint8_t ydim; + + /** The number of packed items in the 1 and 2 plane data. */ + uint16_t item_count[2]; + + /** The accumulator divisor for 1 and 2 plane data. */ + uint16_t difscales[2]; + + /** The initial accumulator values for 1 and 2 plane data. */ + uint16_t initial_percs[2]; + + /** The packed data for the 1 and 2 plane data. */ + const uint16_t *items[2]; +}; + +#if ASTCENC_BLOCK_MAX_TEXELS >= (4 * 4) +static const uint16_t percentile_arr_4x4_0[61] { + 0x0242, 0x7243, 0x6A51, 0x6A52, 0x5A41, 0x4A53, 0x8851, 0x3842, + 0x3852, 0x3853, 0x3043, 0xFA33, 0x1BDF, 0x2022, 0x1032, 0x29CE, + 0x21DE, 0x2823, 0x0813, 0x0A13, 0x0A31, 0x0A23, 0x09CF, 0x0833, + 0x0A32, 0x01DF, 0x0BDD, 0x0BCF, 0x0221, 0x095F, 0x0A01, 0x0BDE, + 0x0BCD, 0x0A22, 0x09AF, 0x0B5F, 0x0B4D, 0x0BCE, 0x0BBF, 0x0A11, + 0x01BF, 0x0202, 0x0B5D, 0x1203, 0x034E, 0x0B8E, 0x035E, 0x0212, + 0x032E, 0x0B4F, 0x03AF, 0x03AD, 0x03BD, 0x0BBE, 0x03AE, 0x039F, + 0x039E, 0x033E, 0x033F, 0x038F, 0x032F +}; + +static const uint16_t percentile_arr_4x4_1[84] { + 0x0452, 0xFFAE, 0x2433, 0x1DDF, 0x17CD, 0x1E21, 0x1C43, 0x1442, + 0x3FBE, 0x1FDD, 0x0E31, 0x0F4F, 0x1423, 0x0FBD, 0x1451, 0x0E03, + 0x05CF, 0x0C32, 0x0DDE, 0x27AD, 0x274E, 0x0E02, 0x0F5E, 0x07AF, + 0x0F5F, 0x0DCE, 0x0C41, 0x0422, 0x0613, 0x0E12, 0x0611, 0x0F3F, + 0x0601, 0x0DBF, 0x05DD, 0x075D, 0x0C02, 0x054E, 0x0431, 0x0413, + 0x079F, 0x05BE, 0x0F4D, 0x0403, 0x05AF, 0x055F, 0x05AE, 0x054F, + 0x0421, 0x05BD, 0x0DCD, 0x0411, 0x0412, 0x055E, 0x055D, 0x073D, + 0x058E, 0x072F, 0x072D, 0x079D, 0x0D2E, 0x0453, 0x078D, 0x053E, + 0x053F, 0x059E, 0x052F, 0x058F, 0x072E, 0x078F, 0x059F, 0x078E, + 0x071F, 0x073E, 0x051F, 0x070D, 0x079E, 0x070E, 0x071D, 0x0622, + 0x070F, 0x071E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_4x4 { + 4, 4, + { 61, 84 }, + { 184, 141 }, + { 0, 53 }, + { percentile_arr_4x4_0, percentile_arr_4x4_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (5 * 4) +static const uint16_t percentile_arr_5x4_0[91] { + 0x02C1, 0xFAD1, 0xE8D3, 0xDAC2, 0xA8D2, 0x70D1, 0x50C2, 0x80C3, + 0xD2C3, 0x4AA2, 0x2AD2, 0x2242, 0x2251, 0x42A3, 0x1A43, 0x4A52, + 0x32B3, 0x2A41, 0x1042, 0x1851, 0x5892, 0x10A2, 0x2253, 0x10B2, + 0x10B3, 0x13DF, 0x3083, 0x08B1, 0x1043, 0x12B1, 0x0AB2, 0x1A93, + 0x1852, 0x1A33, 0x09CE, 0x08A3, 0x1022, 0x1283, 0x0853, 0x1AA1, + 0x1093, 0x11DE, 0x135F, 0x1832, 0x195F, 0x0A81, 0x11CF, 0x0A31, + 0x09DF, 0x0B4D, 0x09AF, 0x03CF, 0x0813, 0x03DD, 0x0A92, 0x0A82, + 0x03CD, 0x0023, 0x0BDE, 0x0BBF, 0x1232, 0x0221, 0x0291, 0x0A23, + 0x0833, 0x035D, 0x0BCE, 0x01BF, 0x0222, 0x134E, 0x0213, 0x0A01, + 0x0B4F, 0x0B5E, 0x038E, 0x032E, 0x03AF, 0x0A11, 0x03AD, 0x0203, + 0x0202, 0x0BBD, 0x033E, 0x03AE, 0x03BE, 0x0212, 0x033F, 0x039E, + 0x039F, 0x032F, 0x038F +}; + +static const uint16_t percentile_arr_5x4_1[104] { + 0x0433, 0xB621, 0x5452, 0x4443, 0x7FAE, 0xFCA3, 0x7CC2, 0x24B2, + 0x45DF, 0x44B3, 0x7631, 0x27CD, 0x1CD1, 0x1E03, 0x4FBE, 0x774F, + 0x1C42, 0x7691, 0x24A2, 0x2681, 0x3C23, 0x3C93, 0x0FBD, 0x1C32, + 0x1E82, 0x1E12, 0x0F4E, 0x1602, 0x0FAD, 0x0C51, 0x1FDD, 0x0E13, + 0x0DCF, 0x175E, 0x0C22, 0x175F, 0x15DE, 0x0CB1, 0x17AF, 0x1CC1, + 0x1F3F, 0x1483, 0x0441, 0x0C91, 0x04D2, 0x0DCE, 0x154E, 0x079F, + 0x0CA1, 0x0F5D, 0x0431, 0x15DD, 0x05BF, 0x0C92, 0x0611, 0x0C82, + 0x0402, 0x074D, 0x0DBD, 0x055E, 0x05BE, 0x0DCD, 0x0421, 0x05AF, + 0x0403, 0x0D4F, 0x055F, 0x05AE, 0x0413, 0x0E01, 0x055D, 0x073D, + 0x0C12, 0x0692, 0x0411, 0x072D, 0x078D, 0x079D, 0x058E, 0x0D2E, + 0x0453, 0x072F, 0x059E, 0x052F, 0x071F, 0x053F, 0x053E, 0x078F, + 0x058F, 0x051F, 0x0F2E, 0x059F, 0x078E, 0x073E, 0x071D, 0x070D, + 0x070E, 0x079E, 0x0622, 0x0683, 0x070F, 0x071E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_5x4 { + 5, 4, + { 91, 104 }, + { 322, 464 }, + { 0, 202 }, + { percentile_arr_5x4_0, percentile_arr_5x4_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (5 * 5) +static const uint16_t percentile_arr_5x5_0[129] { + 0x00F3, 0xF8F2, 0x70E3, 0x62E1, 0x60E1, 0x4AC1, 0x3261, 0x38D3, + 0x3271, 0x5AF1, 0x5873, 0x2AD1, 0x28E2, 0x28F1, 0x2262, 0x9AC2, + 0x18D2, 0x1072, 0x1071, 0x22A2, 0x2062, 0x1A51, 0x10C2, 0x0892, + 0x08D1, 0x1AA3, 0x23EE, 0x08C3, 0x0BEF, 0x2242, 0x0863, 0x0AB3, + 0x0BFF, 0x0A93, 0x08A2, 0x0A41, 0x1083, 0x0842, 0x10B3, 0x21EE, + 0x10B2, 0x00B1, 0x1263, 0x12C3, 0x0A83, 0x0851, 0x11FE, 0x0253, + 0x09FD, 0x0A72, 0x09FF, 0x1AB2, 0x0BDF, 0x0A33, 0x0243, 0x0B7F, + 0x0AB1, 0x12D2, 0x0252, 0x096F, 0x00A3, 0x0893, 0x0822, 0x0843, + 0x097E, 0x097F, 0x01EF, 0x09CE, 0x03FE, 0x0A81, 0x036F, 0x0052, + 0x13FD, 0x0AA1, 0x1853, 0x036D, 0x0A92, 0x0832, 0x01DE, 0x0A82, + 0x0BED, 0x0231, 0x0BBF, 0x03DD, 0x0B6E, 0x01AF, 0x0813, 0x0023, + 0x0A91, 0x015F, 0x037E, 0x01CF, 0x0232, 0x0BCD, 0x0221, 0x0BDE, + 0x0213, 0x035F, 0x0B7D, 0x0223, 0x01BF, 0x0BCF, 0x01DF, 0x0033, + 0x0222, 0x03CE, 0x0A01, 0x03AF, 0x034D, 0x0B8E, 0x032E, 0x0203, + 0x0211, 0x0202, 0x0B5D, 0x03AD, 0x034E, 0x03AE, 0x034F, 0x033F, + 0x039F, 0x03BD, 0x03BE, 0x035E, 0x0212, 0x033E, 0x039E, 0x032F, + 0x038F +}; + +static const uint16_t percentile_arr_5x5_1[126] { + 0x0443, 0x6452, 0xFE21, 0x27AE, 0x2433, 0x1FCD, 0x25DF, 0x6CC2, + 0x2C62, 0x1F4F, 0x4C42, 0x1FBE, 0x0DEF, 0x34A3, 0x0E03, 0x54B2, + 0x1F7D, 0x17DD, 0x0DFF, 0x0CD1, 0x0E31, 0x0C71, 0x1CF1, 0x15FE, + 0x1691, 0x1681, 0x24B3, 0x174E, 0x0F6E, 0x0493, 0x175E, 0x1C51, + 0x17BD, 0x076D, 0x2CA2, 0x05EE, 0x1472, 0x2423, 0x0DCF, 0x0432, + 0x15DE, 0x0612, 0x0CD2, 0x0682, 0x0F5F, 0x07AD, 0x0602, 0x0CE1, + 0x0C91, 0x0FAF, 0x073F, 0x0E13, 0x0D7F, 0x0DCE, 0x0422, 0x0D7D, + 0x0441, 0x05FD, 0x0CB1, 0x0C83, 0x04C1, 0x0461, 0x0F9F, 0x0DDD, + 0x056E, 0x0C92, 0x0482, 0x0431, 0x05ED, 0x0D6F, 0x075D, 0x0402, + 0x057E, 0x0DBF, 0x04A1, 0x054E, 0x0F4D, 0x0403, 0x05CD, 0x0453, + 0x05AE, 0x0421, 0x0F1F, 0x05BE, 0x0601, 0x0611, 0x05BD, 0x05AF, + 0x078D, 0x072D, 0x073D, 0x055E, 0x0F9D, 0x0411, 0x0413, 0x0412, + 0x055F, 0x077E, 0x055D, 0x052E, 0x054F, 0x053E, 0x058E, 0x078F, + 0x059E, 0x071D, 0x0E92, 0x053F, 0x059F, 0x051F, 0x072F, 0x052F, + 0x070D, 0x079E, 0x058F, 0x072E, 0x070E, 0x078E, 0x070F, 0x073E, + 0x0622, 0x0683, 0x071E, 0x076F, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_5x5 { + 5, 5, + { 129, 126 }, + { 258, 291 }, + { 0, 116 }, + { percentile_arr_5x5_0, percentile_arr_5x5_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (6 * 5) +static const uint16_t percentile_arr_6x5_0[165] { + 0x0163, 0xF8F3, 0x9962, 0x8972, 0x7961, 0x7173, 0x6953, 0x5943, + 0x4B41, 0x3AE1, 0x38E3, 0x6971, 0x32C1, 0x28D3, 0x2A61, 0xC8F2, + 0x2271, 0x4873, 0x5B21, 0x3AD1, 0x1B13, 0x1952, 0x1B51, 0x12F1, + 0x1A62, 0x1322, 0x1951, 0x10E2, 0x1B31, 0x20F1, 0x2102, 0x2072, + 0x10D2, 0x1142, 0x2912, 0x3871, 0x2BEE, 0x0862, 0x1123, 0x0AC2, + 0x12A2, 0x0A51, 0x1922, 0x0941, 0x1BEF, 0x0B42, 0x08D1, 0x13FF, + 0x1933, 0x08C3, 0x08C2, 0x1131, 0x08E1, 0x2903, 0x0863, 0x0B32, + 0x1132, 0x1AC3, 0x0A42, 0x1A41, 0x0042, 0x21EE, 0x09FF, 0x03DF, + 0x0AA3, 0x11FE, 0x02B3, 0x0B11, 0x10B3, 0x0B03, 0x11FD, 0x0913, + 0x0A53, 0x037F, 0x1263, 0x0051, 0x0A33, 0x0B01, 0x016F, 0x0A72, + 0x1312, 0x08A2, 0x10B1, 0x0BFE, 0x11EF, 0x0B02, 0x0A52, 0x0043, + 0x0822, 0x01CE, 0x0A43, 0x097F, 0x036F, 0x08B2, 0x03FD, 0x0A83, + 0x0B33, 0x0AB1, 0x017E, 0x0B23, 0x0852, 0x02D2, 0x0BBF, 0x0BDD, + 0x03ED, 0x0AB2, 0x02A1, 0x0853, 0x036D, 0x0892, 0x0032, 0x0A31, + 0x0083, 0x09DE, 0x0A93, 0x08A3, 0x1213, 0x0BDE, 0x03CD, 0x036E, + 0x037E, 0x0A21, 0x0023, 0x0BCF, 0x01CF, 0x0013, 0x01AF, 0x0A92, + 0x0232, 0x035F, 0x0093, 0x0B7D, 0x015F, 0x0282, 0x01BF, 0x09DF, + 0x03CE, 0x0223, 0x0833, 0x0222, 0x03AF, 0x0A01, 0x0291, 0x0B4D, + 0x032E, 0x038E, 0x0203, 0x0281, 0x035D, 0x03AD, 0x0B9F, 0x0202, + 0x034F, 0x03BE, 0x0211, 0x03AE, 0x03BD, 0x0212, 0x034E, 0x033F, + 0x033E, 0x035E, 0x039E, 0x032F, 0x038F +}; + +static const uint16_t percentile_arr_6x5_1[145] { + 0x0443, 0xEFAE, 0x2CC2, 0x2E21, 0x2C52, 0x7C33, 0x47CD, 0x25DF, + 0x3CA3, 0xFFBE, 0x2551, 0x24B3, 0x474F, 0x1513, 0x2691, 0x1603, + 0x1462, 0x1D32, 0x14B2, 0x5442, 0x2CD2, 0x35EF, 0x0CD1, 0x3D22, + 0x17BD, 0x0FDD, 0x0DFF, 0x2631, 0x177D, 0x0CF1, 0x1E81, 0x0E82, + 0x1DFE, 0x0F5E, 0x0701, 0x2CA2, 0x1D03, 0x0F4E, 0x1471, 0x0C51, + 0x1F6E, 0x2FAF, 0x0561, 0x0C72, 0x176D, 0x0FAD, 0x0DEE, 0x05CF, + 0x0E13, 0x0F5F, 0x0E12, 0x0C23, 0x1E02, 0x1D12, 0x0CB1, 0x0C32, + 0x0C93, 0x15DE, 0x0F9F, 0x0F3F, 0x0D41, 0x0C41, 0x0CC1, 0x0D31, + 0x0C22, 0x05FD, 0x057F, 0x0D01, 0x0461, 0x04E1, 0x0D7D, 0x05CE, + 0x0502, 0x0C31, 0x05ED, 0x05DD, 0x0511, 0x0F11, 0x0491, 0x0D6F, + 0x0521, 0x056E, 0x0C83, 0x0D23, 0x04A1, 0x0C02, 0x075D, 0x05BF, + 0x0C21, 0x079D, 0x0482, 0x05BD, 0x0DBE, 0x05CD, 0x054E, 0x057E, + 0x0DAE, 0x074D, 0x078D, 0x0542, 0x0492, 0x05AF, 0x0611, 0x0F3D, + 0x0601, 0x071F, 0x055E, 0x059E, 0x0571, 0x054F, 0x0412, 0x0453, + 0x058E, 0x0413, 0x0D3E, 0x077E, 0x072D, 0x052E, 0x059F, 0x055D, + 0x072F, 0x0403, 0x0411, 0x058F, 0x055F, 0x0692, 0x078E, 0x053F, + 0x0D2F, 0x078F, 0x070D, 0x071D, 0x051F, 0x072E, 0x079E, 0x070E, + 0x070F, 0x073E, 0x0622, 0x0683, 0x0702, 0x071E, 0x076F, 0x07BF, + 0x07CE +}; + +static const packed_percentile_table block_pcd_6x5 { + 6, 5, + { 165, 145 }, + { 388, 405 }, + { 0, 156 }, + { percentile_arr_6x5_0, percentile_arr_6x5_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (6 * 6) +static const uint16_t percentile_arr_6x6_0[206] { + 0x006F, 0xF908, 0xF104, 0xE918, 0xE963, 0xD114, 0xB0F3, 0xA07E, + 0x7972, 0x705F, 0x687F, 0x6162, 0x5953, 0x586E, 0x610C, 0x524D, + 0x5973, 0x9943, 0x98E3, 0x904F, 0x8341, 0x7AC1, 0x3A61, 0x70D3, + 0xA073, 0x6AE1, 0x30F2, 0x3313, 0x2B21, 0x9A2E, 0x4322, 0x225D, + 0x2331, 0x2271, 0x22D1, 0x1A2D, 0x221F, 0x22F1, 0x1971, 0x6952, + 0x1951, 0x187D, 0x18F1, 0x1902, 0x185E, 0x1B51, 0x105D, 0x1A3D, + 0x30E2, 0x10D2, 0x1961, 0x12A2, 0x6072, 0x3942, 0x386D, 0x33EE, + 0x104E, 0x4923, 0x101E, 0x2122, 0x1251, 0x1141, 0x182F, 0x3133, + 0x080E, 0x1262, 0x123E, 0x1B32, 0x102E, 0x1931, 0x10D1, 0x1912, + 0x0871, 0x12C2, 0x08C2, 0x1103, 0x0B03, 0x1062, 0x083D, 0x08E1, + 0x1132, 0x184D, 0x0863, 0x08C3, 0x303F, 0x083E, 0x10B3, 0x12A3, + 0x0BEF, 0x0B11, 0x1A42, 0x2233, 0x13FF, 0x080F, 0x0A41, 0x0AC3, + 0x0842, 0x1A63, 0x0BDF, 0x09FF, 0x12B3, 0x124E, 0x0B12, 0x0B42, + 0x0A2F, 0x1253, 0x0913, 0x1051, 0x0B01, 0x120F, 0x0B02, 0x08A2, + 0x0BBF, 0x00B1, 0x22B1, 0x01EE, 0x1B33, 0x0B23, 0x0283, 0x13FD, + 0x0AB2, 0x11FD, 0x09FE, 0x0A43, 0x08B2, 0x0A1D, 0x0A52, 0x023F, + 0x101F, 0x01CE, 0x0A31, 0x0BDD, 0x0293, 0x1822, 0x12A1, 0x03FE, + 0x121E, 0x0843, 0x0272, 0x0B6F, 0x0052, 0x0A0D, 0x0BED, 0x12D2, + 0x1B7F, 0x1053, 0x0032, 0x01DE, 0x08A3, 0x020E, 0x0883, 0x09EF, + 0x0892, 0x0A21, 0x03CD, 0x0B5F, 0x0213, 0x0A32, 0x016F, 0x1292, + 0x03DE, 0x017E, 0x0BAF, 0x0223, 0x1093, 0x0BCF, 0x037E, 0x01DF, + 0x09CF, 0x015F, 0x09AF, 0x0023, 0x01BF, 0x0222, 0x0282, 0x03CE, + 0x1013, 0x036E, 0x097F, 0x0033, 0x0A01, 0x0B6D, 0x03BE, 0x037D, + 0x0281, 0x0BAE, 0x0203, 0x032E, 0x034D, 0x034F, 0x0291, 0x0211, + 0x038E, 0x03BD, 0x039E, 0x0BAD, 0x033E, 0x034E, 0x039F, 0x0202, + 0x035D, 0x0212, 0x033F, 0x035E, 0x038F, 0x032F +}; + +static const uint16_t percentile_arr_6x6_1[164] { + 0x07AE, 0x8443, 0x7E21, 0x77CD, 0x6C62, 0x9433, 0x6452, 0x34C2, + 0x5DDF, 0xC7BE, 0x25EF, 0x24A3, 0x3CF1, 0xFDFF, 0x177D, 0x1F4F, + 0xC551, 0x5CB3, 0x1532, 0x1513, 0x143E, 0x245D, 0x14B2, 0x2472, + 0x14D2, 0x1FBD, 0x1631, 0x2DFE, 0x1691, 0x17DD, 0x2E03, 0x376E, + 0x2442, 0x0F6D, 0x3C71, 0x2CD1, 0x2522, 0x6C51, 0x260D, 0x17AF, + 0x0DEE, 0x1C1F, 0x2F01, 0x142E, 0x0CA2, 0x0FAD, 0x3D03, 0x275E, + 0x1681, 0x274E, 0x1682, 0x1C23, 0x273F, 0x0F5F, 0x05DE, 0x15FD, + 0x0DCF, 0x1E02, 0x04B1, 0x144D, 0x0E12, 0x0D12, 0x1CC1, 0x0E13, + 0x1C6D, 0x0C32, 0x043D, 0x0C61, 0x0F9F, 0x04E1, 0x0DCE, 0x0D41, + 0x1C93, 0x0C22, 0x061D, 0x0D7F, 0x0C41, 0x0561, 0x0531, 0x0D21, + 0x0711, 0x0C91, 0x0501, 0x0C1E, 0x040F, 0x15DD, 0x0431, 0x0C2F, + 0x057D, 0x0C2D, 0x0DBE, 0x040E, 0x0D02, 0x0D11, 0x054E, 0x040D, + 0x0D23, 0x0DBF, 0x04A1, 0x05ED, 0x0C1D, 0x05BD, 0x072D, 0x056E, + 0x0483, 0x0F3D, 0x0482, 0x078D, 0x0F5D, 0x0453, 0x0D9E, 0x0C4E, + 0x05CD, 0x079D, 0x0402, 0x05AE, 0x0F1F, 0x0542, 0x074D, 0x056F, + 0x0421, 0x0D4F, 0x0601, 0x0571, 0x0492, 0x059F, 0x053F, 0x05AF, + 0x0611, 0x055E, 0x0D8E, 0x053E, 0x055D, 0x047D, 0x0411, 0x052E, + 0x058F, 0x051F, 0x055F, 0x0D7E, 0x072F, 0x052F, 0x0412, 0x078F, + 0x0403, 0x077E, 0x070D, 0x070E, 0x078E, 0x0F1D, 0x072E, 0x0413, + 0x070F, 0x0692, 0x079E, 0x060E, 0x0622, 0x0683, 0x0702, 0x071E, + 0x073E, 0x076F, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_6x6 { + 6, 6, + { 206, 164 }, + { 769, 644 }, + { 0, 256 }, + { percentile_arr_6x6_0, percentile_arr_6x6_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 5) +static const uint16_t percentile_arr_8x5_0[226] { + 0x0066, 0xF865, 0xE963, 0xA856, 0xA1F2, 0x9875, 0x91C3, 0x91E2, + 0x80F3, 0x8076, 0x61E3, 0x6153, 0x5172, 0x59D2, 0x51D3, 0x5047, + 0xA943, 0x49B3, 0x4846, 0x4962, 0xC037, 0x4173, 0x39F1, 0x7027, + 0xA2C1, 0x3AE1, 0x9341, 0x30D3, 0x5225, 0x2A61, 0x33C1, 0x28E3, + 0x53A1, 0x49C2, 0x2A06, 0x4055, 0x2006, 0x21D1, 0x2271, 0x4321, + 0x3873, 0x18F2, 0x2015, 0x1A15, 0x1857, 0x52D1, 0x3045, 0x4835, + 0x1952, 0x29E1, 0x3207, 0x1036, 0x1816, 0x2A16, 0x2971, 0x13B1, + 0x2A17, 0x2351, 0x1025, 0x1826, 0x30E2, 0x1262, 0x20F1, 0x1007, + 0x1072, 0x1151, 0x10D2, 0x1235, 0x1205, 0x1062, 0x4AF1, 0x1251, + 0x0B31, 0x1381, 0x13EE, 0x1B92, 0x13EF, 0x0942, 0x1AA2, 0x13FF, + 0x1161, 0x0B93, 0x19A2, 0x11B1, 0x08D1, 0x12C2, 0x0B13, 0x1B22, + 0x2123, 0x09A3, 0x2071, 0x1B7F, 0x1817, 0x0A42, 0x10C2, 0x1233, + 0x08C3, 0x0A41, 0x0B42, 0x09C1, 0x0933, 0x1AB3, 0x1382, 0x1BDF, + 0x2122, 0x0A53, 0x0AC3, 0x20E1, 0x0941, 0x0931, 0x0042, 0x0BA2, + 0x0AA3, 0x0992, 0x0863, 0x08B3, 0x11B2, 0x0902, 0x1283, 0x09FF, + 0x0B83, 0x0982, 0x0932, 0x0BFE, 0x0B32, 0x0BBF, 0x11FE, 0x036F, + 0x0851, 0x08B1, 0x18A2, 0x11EE, 0x0A52, 0x0BB2, 0x01FD, 0x0A43, + 0x1A63, 0x1193, 0x0B91, 0x0043, 0x1231, 0x0A26, 0x0AB1, 0x03FD, + 0x096F, 0x00B2, 0x0983, 0x0A72, 0x01CE, 0x0BDD, 0x0022, 0x0B11, + 0x1213, 0x0B6D, 0x017E, 0x1333, 0x0112, 0x0852, 0x02D2, 0x097F, + 0x01EF, 0x0AB2, 0x0293, 0x0853, 0x0BED, 0x0B12, 0x1303, 0x02A1, + 0x0892, 0x0032, 0x0883, 0x0B6E, 0x0292, 0x0A32, 0x037E, 0x0B23, + 0x0103, 0x0A21, 0x0B01, 0x0302, 0x0BCD, 0x00A3, 0x0BCF, 0x0BDE, + 0x0113, 0x01DE, 0x0B5F, 0x0013, 0x0BAF, 0x0223, 0x0222, 0x0A82, + 0x0833, 0x0023, 0x09CF, 0x037D, 0x01AF, 0x095F, 0x03CE, 0x09DF, + 0x01BF, 0x0893, 0x0203, 0x0201, 0x0B4D, 0x03BE, 0x032E, 0x03AE, + 0x0291, 0x0A02, 0x0211, 0x039F, 0x0281, 0x038E, 0x03AD, 0x033F, + 0x035D, 0x033E, 0x034E, 0x034F, 0x0212, 0x03BD, 0x032F, 0x035E, + 0x038F, 0x039E +}; + +static const uint16_t percentile_arr_8x5_1[167] { + 0x0621, 0xFCC2, 0x3443, 0xA433, 0x5532, 0x2551, 0x6CA3, 0x27AE, + 0x6452, 0x8E03, 0x3CB3, 0x4DA2, 0x6DDF, 0x37CD, 0x6F01, 0x1691, + 0x2E82, 0x27BE, 0x1513, 0x34D2, 0x1D22, 0x3E31, 0x2593, 0x2CB2, + 0x1C16, 0x374F, 0x0DD1, 0x2583, 0x6613, 0x0CD1, 0x0C35, 0x1462, + 0x3E81, 0x2612, 0x2C42, 0x3407, 0x14A2, 0x0E02, 0x1CF1, 0x0C06, + 0x17BD, 0x0F7D, 0x1D23, 0x35B1, 0x179F, 0x0D92, 0x0F5E, 0x1451, + 0x04B1, 0x1F6E, 0x0DEF, 0x0D31, 0x374E, 0x15C1, 0x0541, 0x2405, + 0x17AD, 0x0471, 0x1472, 0x0DFE, 0x0711, 0x0FDD, 0x0DFF, 0x0432, + 0x1D82, 0x0423, 0x0F6D, 0x07AF, 0x0F5F, 0x04C1, 0x1542, 0x0561, + 0x0DCF, 0x1D03, 0x1493, 0x0422, 0x0445, 0x0D12, 0x0C25, 0x0415, + 0x0DA1, 0x1591, 0x0DEE, 0x05DE, 0x0C31, 0x0491, 0x0441, 0x0D21, + 0x078D, 0x057D, 0x0C61, 0x0F3F, 0x0581, 0x0D6E, 0x0501, 0x0CA1, + 0x04E1, 0x0DFD, 0x057F, 0x0502, 0x0511, 0x0C82, 0x0483, 0x0C03, + 0x079D, 0x0402, 0x0DDD, 0x0611, 0x05AE, 0x0DCE, 0x056F, 0x0421, + 0x057E, 0x071F, 0x0DBF, 0x05BE, 0x0412, 0x059F, 0x054E, 0x077E, + 0x0C26, 0x05ED, 0x073D, 0x0601, 0x0492, 0x0453, 0x075D, 0x058E, + 0x0F2D, 0x05CD, 0x0571, 0x053E, 0x0692, 0x05BD, 0x054F, 0x055E, + 0x0411, 0x0F1D, 0x074D, 0x059E, 0x05AF, 0x070D, 0x053F, 0x058F, + 0x0413, 0x070F, 0x055D, 0x070E, 0x078F, 0x052E, 0x072F, 0x055F, + 0x078E, 0x0F2E, 0x052F, 0x051F, 0x0417, 0x071E, 0x0781, 0x0622, + 0x0683, 0x0702, 0x073E, 0x076F, 0x079E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_8x5 { + 8, 5, + { 226, 167 }, + { 763, 517 }, + { 0, 178 }, + { percentile_arr_8x5_0, percentile_arr_8x5_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 6) +static const uint16_t percentile_arr_8x6_0[273] { + 0x0154, 0xF944, 0xE066, 0xA128, 0x9963, 0x8118, 0x806F, 0x79F2, + 0x79E2, 0x7108, 0xD934, 0x6056, 0x69C3, 0x60F3, 0x5972, 0x59E3, + 0x5075, 0x91B3, 0xC9D2, 0x807E, 0x385F, 0x4153, 0x3943, 0x4162, + 0x3837, 0x3847, 0x7173, 0x31D3, 0x6948, 0x3046, 0x307F, 0x5827, + 0x3114, 0x32C1, 0x3076, 0x2A4D, 0x58E3, 0x306E, 0x2924, 0x2A61, + 0x29F1, 0x50D3, 0x704F, 0x210C, 0x2BA1, 0x2225, 0x2873, 0x4865, + 0x2206, 0x8341, 0x2006, 0x3B21, 0x18F2, 0x21C2, 0x1A1F, 0x23C1, + 0x3AE1, 0x1855, 0x19D1, 0x1A15, 0x3815, 0x1207, 0x1835, 0x2A2E, + 0x1A16, 0x1836, 0x2271, 0x2845, 0x1A2D, 0x11E1, 0x1816, 0x1171, + 0x2217, 0x1952, 0x12D1, 0x3904, 0x125D, 0x4BB1, 0x207D, 0x10E2, + 0x1026, 0x2025, 0x12F1, 0x28F1, 0x105D, 0x1235, 0x12A2, 0x1007, + 0x123D, 0x1A05, 0x1072, 0x1331, 0x101E, 0x0951, 0x10D2, 0x1057, + 0x1B92, 0x185E, 0x1251, 0x19A2, 0x186D, 0x0B81, 0x2BEE, 0x080E, + 0x1A33, 0x1942, 0x0B13, 0x0B51, 0x11A3, 0x0923, 0x2322, 0x09B1, + 0x184E, 0x1161, 0x18D1, 0x0933, 0x0B93, 0x4A62, 0x1017, 0x082F, + 0x0A42, 0x0B82, 0x0AA3, 0x0A41, 0x08C2, 0x08B3, 0x0A3E, 0x22B3, + 0x0871, 0x1BBF, 0x09C1, 0x0AC2, 0x09B2, 0x0BEF, 0x082E, 0x1062, + 0x0922, 0x08C3, 0x1063, 0x0A53, 0x0BDF, 0x080F, 0x0B42, 0x0A83, + 0x084D, 0x103F, 0x0931, 0x08E1, 0x0A0F, 0x1BA2, 0x09FF, 0x1332, + 0x03FF, 0x0941, 0x12C3, 0x0A63, 0x003D, 0x0842, 0x083E, 0x0B83, + 0x0BB2, 0x0A31, 0x0932, 0x1102, 0x0992, 0x0982, 0x1051, 0x08B1, + 0x0A2F, 0x121E, 0x02B1, 0x0A4E, 0x11EE, 0x00A2, 0x1022, 0x0043, + 0x0A52, 0x0A1D, 0x0226, 0x1193, 0x03DD, 0x08B2, 0x0BFD, 0x0A43, + 0x0A13, 0x0AB2, 0x01FD, 0x09FE, 0x020D, 0x081F, 0x0B33, 0x0053, + 0x0B91, 0x0293, 0x0B11, 0x0B7F, 0x0AA1, 0x0B03, 0x0A0E, 0x03FE, + 0x01CE, 0x0B6F, 0x0183, 0x0912, 0x023F, 0x0852, 0x0A21, 0x0323, + 0x03ED, 0x0A32, 0x13AF, 0x0272, 0x08A3, 0x0B12, 0x0083, 0x0832, + 0x13CD, 0x0223, 0x0A92, 0x0092, 0x0AD2, 0x0301, 0x0302, 0x0BDE, + 0x0A22, 0x01EF, 0x0B5F, 0x0103, 0x0BCF, 0x096F, 0x017E, 0x0113, + 0x01DE, 0x0823, 0x0282, 0x0B6E, 0x015F, 0x0813, 0x01AF, 0x01CF, + 0x0B7E, 0x0033, 0x01DF, 0x0BCE, 0x01BF, 0x036D, 0x0A03, 0x017F, + 0x03BE, 0x0201, 0x0893, 0x038E, 0x034D, 0x03AE, 0x0202, 0x039F, + 0x0291, 0x0A11, 0x032E, 0x033F, 0x034F, 0x0281, 0x037D, 0x03BD, + 0x0212, 0x033E, 0x035E, 0x034E, 0x035D, 0x03AD, 0x032F, 0x038F, + 0x039E +}; + +static const uint16_t percentile_arr_8x6_1[186] { + 0x0621, 0xFC33, 0x37AE, 0x1CC2, 0x2C43, 0xAD32, 0x34A3, 0x4551, + 0x6452, 0x5C62, 0x1FCD, 0x14F1, 0x4CB3, 0x24D2, 0x15DF, 0x0FBE, + 0x2603, 0x3DA2, 0x2E31, 0x25D1, 0x25EF, 0x0D22, 0x2E91, 0x1E82, + 0x0FBD, 0x1513, 0x0CB2, 0x0CD1, 0x0F4F, 0x1F7D, 0x1701, 0x0C16, + 0x2593, 0x2C42, 0x0C72, 0x14A2, 0x0F6E, 0x0C35, 0x0C71, 0x0D83, + 0x0C07, 0x1DFF, 0x043E, 0x1613, 0x07DD, 0x0FAD, 0x1451, 0x076D, + 0x0E81, 0x05FE, 0x0406, 0x0E0D, 0x045D, 0x2612, 0x0E02, 0x07AF, + 0x0DB1, 0x0F5E, 0x15C1, 0x0C23, 0x1523, 0x0C1F, 0x0D92, 0x04B1, + 0x0D31, 0x0432, 0x0D61, 0x0F4E, 0x0D41, 0x0DEE, 0x0D42, 0x04C1, + 0x0CE1, 0x079F, 0x0C2E, 0x0405, 0x0C22, 0x0461, 0x0E1D, 0x0582, + 0x073F, 0x0571, 0x0C4D, 0x0DFD, 0x05CE, 0x0C6D, 0x05DE, 0x0415, + 0x0C45, 0x075F, 0x0C41, 0x0D03, 0x05A1, 0x0711, 0x05CF, 0x0425, + 0x0C93, 0x0D21, 0x0591, 0x043D, 0x0D12, 0x0501, 0x040F, 0x0511, + 0x0431, 0x0C03, 0x04A1, 0x078D, 0x0581, 0x041E, 0x040D, 0x0C02, + 0x040E, 0x05DD, 0x057F, 0x079D, 0x042D, 0x0D9F, 0x0502, 0x056E, + 0x0412, 0x071F, 0x044E, 0x05BF, 0x0C1D, 0x0482, 0x05AE, 0x042F, + 0x057D, 0x0491, 0x054E, 0x047D, 0x0DBE, 0x0611, 0x0492, 0x0601, + 0x05BD, 0x05CD, 0x0426, 0x05ED, 0x072D, 0x073D, 0x0483, 0x0F5D, + 0x0421, 0x056F, 0x053F, 0x058E, 0x054F, 0x078F, 0x053E, 0x059E, + 0x057E, 0x051F, 0x055D, 0x0413, 0x070D, 0x05AF, 0x0411, 0x0453, + 0x0D5E, 0x077E, 0x052F, 0x070F, 0x074D, 0x0692, 0x070E, 0x072F, + 0x072E, 0x058F, 0x071D, 0x052E, 0x0417, 0x073E, 0x0781, 0x078E, + 0x055F, 0x060E, 0x0622, 0x0683, 0x0702, 0x071E, 0x076F, 0x079E, + 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_8x6 { + 8, 6, + { 273, 186 }, + { 880, 300 }, + { 0, 64 }, + { percentile_arr_8x6_0, percentile_arr_8x6_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 8) +static const uint16_t percentile_arr_8x8_0[347] { + 0x0334, 0xFD44, 0xDD14, 0x9154, 0x9B08, 0x906A, 0x8928, 0x8108, + 0xE866, 0xC918, 0x606F, 0xC0FE, 0x5963, 0x58EE, 0x6534, 0x505A, + 0x51E2, 0xA8CF, 0x5354, 0x5314, 0x5134, 0x5524, 0x48F3, 0x504B, + 0x487E, 0x5344, 0x49C3, 0x4972, 0x49F2, 0x4856, 0xD0EF, 0x81D2, + 0x78DE, 0x4261, 0x3AC1, 0x71E3, 0x6879, 0x390C, 0x3143, 0x31B3, + 0x385F, 0x3153, 0x306E, 0x3037, 0x30DF, 0x3162, 0x304F, 0x3075, + 0xB03B, 0x2847, 0x28E3, 0x2914, 0x507F, 0x28BF, 0x5173, 0x5073, + 0x20D3, 0x2A06, 0x2827, 0x2508, 0x2229, 0x29D3, 0x204A, 0x207A, + 0x2046, 0x4148, 0x20FD, 0x4225, 0x23A1, 0x3944, 0x2065, 0x1924, + 0x2324, 0x1806, 0x19F1, 0x2215, 0x1876, 0x22AD, 0x502B, 0x1B04, + 0x18F2, 0x3A4D, 0x3216, 0x3504, 0x18DD, 0x1B21, 0x10CE, 0x1869, + 0x1B41, 0x1855, 0x1207, 0x1AE1, 0x2845, 0x19D1, 0x2A0A, 0x1A2D, + 0x2A1A, 0x11C2, 0x1A0B, 0x1217, 0x2816, 0x121B, 0x1271, 0x2AD1, + 0x1035, 0x1015, 0x287D, 0x12F1, 0x43C1, 0x1171, 0x1A05, 0x08E2, + 0x11E1, 0x3251, 0x2049, 0x20F1, 0x12CD, 0x0A39, 0x1219, 0x1059, + 0x1104, 0x1036, 0x1872, 0x3007, 0x08ED, 0x205E, 0x1026, 0x0952, + 0x1392, 0x1019, 0x0951, 0x100A, 0x13EE, 0x08D2, 0x1242, 0x0ABD, + 0x22A2, 0x0BDF, 0x2B81, 0x0A35, 0x13B1, 0x0839, 0x13BF, 0x0A33, + 0x1B31, 0x205D, 0x1241, 0x183A, 0x2025, 0x0B93, 0x0A3D, 0x1017, + 0x1313, 0x1253, 0x082A, 0x204E, 0x09A2, 0x080B, 0x0A1F, 0x125D, + 0x0A2E, 0x081A, 0x08D1, 0x082F, 0x086D, 0x1B82, 0x0A09, 0x0B22, + 0x1062, 0x11A3, 0x2161, 0x0923, 0x129F, 0x1A62, 0x0871, 0x0942, + 0x081B, 0x1133, 0x18AE, 0x0A9E, 0x0863, 0x09FF, 0x18C2, 0x0B51, + 0x08BD, 0x0AA3, 0x09B1, 0x1AC2, 0x08B3, 0x0829, 0x0BEF, 0x0B83, + 0x0AAE, 0x0A8D, 0x1857, 0x185B, 0x08AF, 0x103F, 0x08C3, 0x09B2, + 0x0A4E, 0x11C1, 0x0A31, 0x0B42, 0x0A83, 0x0BFF, 0x13DD, 0x00CD, + 0x0AB3, 0x0842, 0x08BE, 0x0922, 0x1A8E, 0x08E1, 0x002E, 0x0BA2, + 0x0A8F, 0x2263, 0x0252, 0x0B32, 0x0AC3, 0x0941, 0x0A43, 0x083D, + 0x083E, 0x0A3E, 0x084D, 0x1131, 0x136F, 0x0AB1, 0x0193, 0x0BFD, + 0x0391, 0x0851, 0x13AF, 0x0843, 0x0213, 0x1226, 0x0932, 0x03B2, + 0x0902, 0x0BCD, 0x0221, 0x089E, 0x00B1, 0x0BDE, 0x03FE, 0x02A1, + 0x0982, 0x009F, 0x080E, 0x0B5F, 0x02BE, 0x0A32, 0x0A2A, 0x01EE, + 0x0053, 0x0AB2, 0x0192, 0x09FD, 0x0052, 0x0B03, 0x0293, 0x00A2, + 0x0B7F, 0x0BED, 0x0311, 0x08B2, 0x0A72, 0x088E, 0x0333, 0x0B12, + 0x0A23, 0x0822, 0x0083, 0x11CE, 0x021D, 0x08A3, 0x088F, 0x029D, + 0x0A22, 0x0A3F, 0x01FE, 0x020F, 0x0983, 0x02D2, 0x0292, 0x0B23, + 0x001E, 0x0BCF, 0x03CE, 0x09AF, 0x0B02, 0x0301, 0x022F, 0x137E, + 0x021E, 0x09EF, 0x016F, 0x0112, 0x097E, 0x080F, 0x020D, 0x0092, + 0x01DE, 0x09DF, 0x0032, 0x0033, 0x0A82, 0x03BE, 0x0B6E, 0x001F, + 0x020E, 0x0023, 0x09CF, 0x0113, 0x0103, 0x0013, 0x0BAE, 0x0203, + 0x0BAD, 0x01BF, 0x034F, 0x095F, 0x036D, 0x0202, 0x017F, 0x0093, + 0x0201, 0x034D, 0x0212, 0x035D, 0x03BD, 0x0B3F, 0x035E, 0x0211, + 0x0281, 0x0291, 0x032E, 0x037D, 0x034E, 0x038E, 0x039F, 0x032F, + 0x033E, 0x038F, 0x039E +}; + +static const uint16_t percentile_arr_8x8_1[208] { + 0x0621, 0x3443, 0x47CD, 0x97AE, 0xFC62, 0x14F1, 0x24C2, 0x25DF, + 0x3C33, 0x1C52, 0x9C72, 0x0FBE, 0x0C5D, 0x343E, 0x24A3, 0x1551, + 0x5D32, 0x1CD2, 0x15EF, 0x4E31, 0x04DD, 0x1FDD, 0x174F, 0x0DD1, + 0x3E0D, 0x15FF, 0x0DA2, 0x1E03, 0x17BD, 0x177D, 0x14B3, 0x0471, + 0x0CAE, 0x1C1F, 0x04D1, 0x0F6E, 0x0DFE, 0x1C42, 0x0C16, 0x0D22, + 0x0C9F, 0x2C2E, 0x0FAD, 0x0571, 0x147D, 0x0C07, 0x04B2, 0x0F6D, + 0x0F5E, 0x07AF, 0x146D, 0x0C51, 0x0593, 0x2583, 0x0C4E, 0x040B, + 0x0C35, 0x0513, 0x0E91, 0x0406, 0x073F, 0x144D, 0x0561, 0x048F, + 0x0F01, 0x0F4E, 0x0CA2, 0x075F, 0x1682, 0x04E1, 0x0C1A, 0x04BD, + 0x0542, 0x0D41, 0x0DEE, 0x04CD, 0x0DCF, 0x04B1, 0x0C15, 0x0C3D, + 0x0423, 0x0592, 0x0DDE, 0x0422, 0x0432, 0x05FD, 0x0DC1, 0x05B1, + 0x0DCE, 0x0612, 0x0C2F, 0x0445, 0x0602, 0x0531, 0x0439, 0x0E81, + 0x0582, 0x0C61, 0x061D, 0x049E, 0x0405, 0x0409, 0x0DBE, 0x079F, + 0x0D21, 0x04C1, 0x0C0A, 0x0E13, 0x04AD, 0x040E, 0x0581, 0x0419, + 0x05DD, 0x0D03, 0x049D, 0x0449, 0x0429, 0x048E, 0x0DA1, 0x0425, + 0x0512, 0x0501, 0x0431, 0x0523, 0x0441, 0x042D, 0x040F, 0x0D7D, + 0x0511, 0x0502, 0x05BF, 0x04A1, 0x0C03, 0x0402, 0x079D, 0x05AE, + 0x075D, 0x057F, 0x041D, 0x048D, 0x042A, 0x0453, 0x05AF, 0x078D, + 0x0C0D, 0x073D, 0x0491, 0x0591, 0x05BD, 0x072D, 0x057E, 0x051F, + 0x0482, 0x0492, 0x041E, 0x0412, 0x0D9F, 0x0421, 0x0493, 0x0711, + 0x056E, 0x059E, 0x054E, 0x0611, 0x05ED, 0x074D, 0x070F, 0x056F, + 0x052F, 0x053F, 0x071F, 0x054F, 0x05CD, 0x0483, 0x055E, 0x072F, + 0x0E01, 0x0426, 0x058F, 0x0413, 0x078F, 0x071D, 0x055F, 0x058E, + 0x0411, 0x053E, 0x071E, 0x055D, 0x077E, 0x052E, 0x0692, 0x0417, + 0x070D, 0x078E, 0x070E, 0x072E, 0x041B, 0x060E, 0x0622, 0x0683, + 0x068D, 0x0702, 0x073E, 0x076F, 0x0781, 0x079E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_8x8 { + 8, 8, + { 347, 208 }, + { 1144, 267 }, + { 0, 38 }, + { percentile_arr_8x8_0, percentile_arr_8x8_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 5) +static const uint16_t percentile_arr_10x5_0[274] { + 0x0165, 0xF975, 0xD866, 0xC056, 0xA946, 0x90C6, 0x90F5, 0x8963, + 0x80D6, 0x80E6, 0x60F3, 0x61C3, 0x59F2, 0xA927, 0x5075, 0x4847, + 0x5153, 0x4955, 0x49E2, 0x48B6, 0x41D2, 0x4943, 0x8305, 0x8172, + 0x4046, 0x4037, 0x40A7, 0x70B7, 0x7AC1, 0x31E3, 0x7027, 0x30E5, + 0x69D3, 0x99B3, 0x3315, 0x6115, 0x3136, 0x3076, 0x3173, 0x30D5, + 0x3106, 0x8962, 0x2916, 0x30C7, 0x5126, 0x30D3, 0x2956, 0x5117, + 0x2B41, 0x2AE1, 0x2A61, 0x29F1, 0x2306, 0x2145, 0x4A85, 0x2057, + 0x40E3, 0x4137, 0x3B21, 0x23C1, 0x2065, 0x1925, 0x51C2, 0x5225, + 0x4935, 0x1AD1, 0x23A1, 0x19D1, 0x1A71, 0x4055, 0x1873, 0x1A86, + 0x1295, 0x18F2, 0x28A6, 0x1952, 0x4AA5, 0x20B5, 0x10C5, 0x2AA2, + 0x11E1, 0x1107, 0x10D2, 0x2171, 0x1351, 0x3036, 0x1331, 0x1BEE, + 0x2035, 0x1045, 0x1313, 0x0A15, 0x1087, 0x1296, 0x13EF, 0x18E2, + 0x1151, 0x1086, 0x10F1, 0x08A5, 0x12C2, 0x1BFF, 0x1095, 0x1A62, + 0x1322, 0x0942, 0x1026, 0x1872, 0x1062, 0x0897, 0x1123, 0x08D1, + 0x1A06, 0x0806, 0x137F, 0x13B1, 0x13DF, 0x1A51, 0x09B1, 0x0A83, + 0x1015, 0x22F1, 0x0961, 0x0B81, 0x12B3, 0x0A35, 0x0AA3, 0x20B3, + 0x08C3, 0x2342, 0x0933, 0x0A33, 0x09A2, 0x10C2, 0x0896, 0x2205, + 0x0825, 0x20E1, 0x0922, 0x1242, 0x0B16, 0x0B32, 0x09A3, 0x0AC3, + 0x0BBF, 0x0B93, 0x0071, 0x0931, 0x0A41, 0x2392, 0x13FE, 0x09C1, + 0x0B07, 0x0016, 0x1182, 0x09B2, 0x0A26, 0x0132, 0x0941, 0x0A93, + 0x0992, 0x1063, 0x1217, 0x01FF, 0x11EE, 0x1216, 0x0B23, 0x0B82, + 0x0042, 0x1102, 0x0213, 0x0B6F, 0x09FE, 0x1207, 0x0807, 0x18B1, + 0x0253, 0x0AB1, 0x08A2, 0x13FD, 0x01FD, 0x1983, 0x0AB2, 0x0A31, + 0x016F, 0x0B11, 0x00B2, 0x0851, 0x0AD2, 0x0993, 0x0BDD, 0x12A1, + 0x017F, 0x0A97, 0x1022, 0x0383, 0x0843, 0x0A52, 0x03A2, 0x097E, + 0x0817, 0x03B2, 0x0A43, 0x09EF, 0x0A63, 0x0B33, 0x0B03, 0x0292, + 0x0272, 0x09CE, 0x0287, 0x136D, 0x0053, 0x0B12, 0x0083, 0x0892, + 0x0112, 0x1282, 0x03ED, 0x0852, 0x0301, 0x1391, 0x0232, 0x0B7E, + 0x0221, 0x08A3, 0x0BCD, 0x0BCF, 0x036E, 0x09DE, 0x0103, 0x03DE, + 0x0832, 0x0BAF, 0x0302, 0x13CE, 0x035F, 0x0093, 0x0A23, 0x01DF, + 0x0013, 0x0A22, 0x0023, 0x0113, 0x09AF, 0x01BF, 0x0033, 0x095F, + 0x0203, 0x0281, 0x09CF, 0x037D, 0x0201, 0x0B4D, 0x03AE, 0x03BE, + 0x0291, 0x035E, 0x038E, 0x0B9F, 0x03AD, 0x0202, 0x034F, 0x0211, + 0x035D, 0x0212, 0x032E, 0x039E, 0x033F, 0x034E, 0x03BD, 0x032F, + 0x033E, 0x038F +}; + +static const uint16_t percentile_arr_10x5_1[180] { + 0x0532, 0xFCA3, 0x3621, 0x6E82, 0x2CC2, 0x3D51, 0x3F01, 0x2691, + 0x17AE, 0x35A2, 0x74B3, 0x1603, 0x4433, 0x3C43, 0x6C35, 0x25D1, + 0x1D13, 0x15DF, 0x37CD, 0x0D93, 0x1D22, 0x0E81, 0x1452, 0x0CD2, + 0x37BE, 0x0CB2, 0x3407, 0x1523, 0x0C16, 0x0CB5, 0x0C96, 0x1486, + 0x2631, 0x1506, 0x0F4F, 0x1583, 0x0CD1, 0x2CA2, 0x2612, 0x1613, + 0x1602, 0x1F11, 0x179F, 0x17BD, 0x15B1, 0x0406, 0x1D41, 0x0CF1, + 0x0D31, 0x0442, 0x1C62, 0x0F6E, 0x077D, 0x0C51, 0x0445, 0x0D15, + 0x2592, 0x0CB1, 0x05EF, 0x0542, 0x17AF, 0x1425, 0x075E, 0x0FAD, + 0x0CC1, 0x0503, 0x0512, 0x15C1, 0x0C95, 0x0415, 0x0505, 0x0F4E, + 0x04A5, 0x0493, 0x0C32, 0x0F5F, 0x04E1, 0x0521, 0x0C85, 0x07DD, + 0x0582, 0x15FF, 0x05CF, 0x0405, 0x0D91, 0x05A1, 0x05FE, 0x0C23, + 0x0561, 0x0472, 0x0471, 0x0C22, 0x0DEE, 0x076D, 0x0502, 0x0426, + 0x0C61, 0x0D7D, 0x0525, 0x05DE, 0x0DCE, 0x079D, 0x0692, 0x0441, + 0x0C91, 0x05DD, 0x0511, 0x057F, 0x0611, 0x0DFD, 0x078D, 0x056E, + 0x0492, 0x04A1, 0x073F, 0x0C31, 0x05BE, 0x0483, 0x0571, 0x056F, + 0x0D9F, 0x0581, 0x0501, 0x057E, 0x05BF, 0x078F, 0x0516, 0x05ED, + 0x0402, 0x0F7E, 0x0482, 0x054E, 0x075D, 0x071F, 0x05CD, 0x0535, + 0x05AE, 0x0C11, 0x058F, 0x05AF, 0x0421, 0x0413, 0x0601, 0x054F, + 0x073D, 0x059E, 0x0487, 0x070F, 0x078E, 0x0781, 0x053E, 0x0403, + 0x072D, 0x055D, 0x05BD, 0x079E, 0x0D8E, 0x0412, 0x052E, 0x074D, + 0x053F, 0x051F, 0x070E, 0x055F, 0x072F, 0x052F, 0x070D, 0x055E, + 0x0417, 0x0453, 0x072E, 0x0622, 0x0683, 0x0702, 0x071D, 0x071E, + 0x073E, 0x076F, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_10x5 { + 10, 5, + { 274, 180 }, + { 954, 324 }, + { 0, 79 }, + { percentile_arr_10x5_0, percentile_arr_10x5_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 6) +static const uint16_t percentile_arr_10x6_0[325] { + 0x01A4, 0xF954, 0xA066, 0x9975, 0x80F5, 0x7056, 0x6918, 0x6963, + 0x58C6, 0x5946, 0x5928, 0x5174, 0x586F, 0xA0E6, 0x5108, 0x48D6, + 0x49E2, 0x40F3, 0x9172, 0x41F2, 0xB875, 0x3927, 0x39C3, 0xA953, + 0x3934, 0x3305, 0x30B6, 0x6943, 0x31D2, 0x3876, 0x3037, 0x2955, + 0x30A7, 0x32C1, 0x29B3, 0x3027, 0x287E, 0x30B7, 0x29E3, 0x5846, + 0x2B15, 0x2847, 0x3162, 0x5173, 0x4936, 0x285F, 0x48D3, 0x2164, + 0x4906, 0x20E5, 0x2915, 0x2116, 0x407F, 0x20D5, 0x2A61, 0x4117, + 0x20E3, 0x2126, 0x4148, 0x206E, 0x39D3, 0x2145, 0x41B4, 0x1B06, + 0x2114, 0x2165, 0x5321, 0x5A85, 0x1A4D, 0x1A1F, 0x19F1, 0x3341, + 0x184F, 0x1956, 0x3125, 0x30C7, 0x28F2, 0x1937, 0x1AE1, 0x1073, + 0x1BA1, 0x1935, 0x110C, 0x1BC1, 0x3A25, 0x19C2, 0x1295, 0x122E, + 0x1944, 0x11D1, 0x1124, 0x1857, 0x22D1, 0x2286, 0x1A2D, 0x12A2, + 0x2107, 0x1055, 0x2065, 0x0A71, 0x2152, 0x10C5, 0x10D2, 0x1331, + 0x08B5, 0x1171, 0x2836, 0x10A6, 0x0904, 0x123D, 0x20F1, 0x12A5, + 0x10E2, 0x107D, 0x1AF1, 0x1313, 0x0951, 0x11E1, 0x1B22, 0x1B51, + 0x0835, 0x101E, 0x0A5D, 0x0A15, 0x3045, 0x0A96, 0x08A5, 0x1142, + 0x12A3, 0x1872, 0x085D, 0x09B1, 0x100E, 0x0887, 0x0886, 0x086D, + 0x0933, 0x12B3, 0x0897, 0x08B3, 0x0A33, 0x0923, 0x1095, 0x0BEE, + 0x2BB1, 0x085E, 0x1283, 0x0A51, 0x1026, 0x0A06, 0x12C2, 0x08D1, + 0x11A2, 0x13BF, 0x08C3, 0x10C2, 0x0A3E, 0x0BDF, 0x0B81, 0x13EF, + 0x0A35, 0x0B16, 0x082F, 0x2161, 0x1B32, 0x0806, 0x084E, 0x11A3, + 0x1015, 0x1122, 0x2931, 0x0342, 0x0825, 0x0A0F, 0x0896, 0x0A05, + 0x0241, 0x09C1, 0x083F, 0x0A42, 0x0071, 0x0B07, 0x082E, 0x0393, + 0x12B1, 0x0A62, 0x0226, 0x0A2F, 0x0B92, 0x0063, 0x0932, 0x0862, + 0x09FF, 0x0A31, 0x00E1, 0x12B2, 0x09B2, 0x0AC3, 0x0941, 0x0293, + 0x1323, 0x104D, 0x003E, 0x083D, 0x0992, 0x1382, 0x03FF, 0x0A13, + 0x1016, 0x0A53, 0x0182, 0x1007, 0x0AA1, 0x080F, 0x0A16, 0x0A1E, + 0x0042, 0x0902, 0x13DD, 0x0BB2, 0x0A63, 0x00A2, 0x08B1, 0x03FE, + 0x1207, 0x08B2, 0x0B83, 0x09EE, 0x0311, 0x0A87, 0x0BAF, 0x03A2, + 0x09FD, 0x0051, 0x0B33, 0x020D, 0x09CE, 0x0217, 0x021D, 0x0817, + 0x020E, 0x0A4E, 0x001F, 0x0BFD, 0x0297, 0x0983, 0x0A92, 0x0252, + 0x0243, 0x0B03, 0x0193, 0x036F, 0x0B12, 0x0043, 0x0822, 0x0A21, + 0x01FE, 0x0853, 0x037F, 0x023F, 0x0BED, 0x02D2, 0x0B91, 0x0232, + 0x0282, 0x0912, 0x08A3, 0x0852, 0x0223, 0x0BCD, 0x0083, 0x0301, + 0x0832, 0x01EF, 0x0892, 0x0302, 0x0A72, 0x03DE, 0x0893, 0x0BCF, + 0x09DE, 0x03CE, 0x035F, 0x0833, 0x0023, 0x0103, 0x017E, 0x0813, + 0x01CF, 0x01BF, 0x016F, 0x0A22, 0x037E, 0x0113, 0x01AF, 0x0B6E, + 0x03BE, 0x0201, 0x0A03, 0x01DF, 0x036D, 0x03AE, 0x015F, 0x0281, + 0x033E, 0x0A02, 0x038E, 0x017F, 0x0291, 0x034D, 0x03BD, 0x0B7D, + 0x03AD, 0x0211, 0x0212, 0x034F, 0x032E, 0x039F, 0x034E, 0x035D, + 0x035E, 0x033F, 0x039E, 0x032F, 0x038F +}; + +static const uint16_t percentile_arr_10x6_1[199] { + 0x0621, 0xBD32, 0x5CA3, 0x1FAE, 0x64C2, 0x1D51, 0x6C33, 0xFC43, + 0x5CB3, 0x25A2, 0x2E82, 0x35D1, 0x4F01, 0x3FBE, 0x3691, 0x2DDF, + 0x2E03, 0x3FCD, 0x14D2, 0x1CF1, 0x0C52, 0x3C35, 0x2D22, 0x1513, + 0x1462, 0x54B2, 0x0E31, 0x4E81, 0x1593, 0x1D23, 0x1CD1, 0x14B5, + 0x2FBD, 0x0C07, 0x1D06, 0x0DEF, 0x14A2, 0x1612, 0x1F4F, 0x0C16, + 0x1F7D, 0x0C96, 0x0486, 0x1F9F, 0x0D42, 0x4583, 0x0E02, 0x0472, + 0x0DB1, 0x1613, 0x0FAD, 0x0D41, 0x0F11, 0x0E0D, 0x1C42, 0x143E, + 0x076E, 0x04B1, 0x0FAF, 0x0D61, 0x0531, 0x0C71, 0x0DFF, 0x0DFE, + 0x0406, 0x0C45, 0x0451, 0x0D15, 0x05C1, 0x2CC1, 0x141F, 0x0CE1, + 0x0FDD, 0x0C22, 0x0582, 0x0D92, 0x0571, 0x0F6D, 0x0C93, 0x045D, + 0x0F5E, 0x044D, 0x0423, 0x0D05, 0x0425, 0x0C95, 0x04A5, 0x0DCE, + 0x075F, 0x0E1D, 0x0503, 0x042E, 0x0D91, 0x0512, 0x0DDE, 0x05A1, + 0x074E, 0x0C32, 0x0431, 0x0415, 0x0D21, 0x05EE, 0x040E, 0x0DDD, + 0x0485, 0x1525, 0x0491, 0x0C26, 0x046D, 0x0C05, 0x05CF, 0x05FD, + 0x0E92, 0x073F, 0x0C0D, 0x043D, 0x0502, 0x0C1E, 0x041D, 0x0461, + 0x04A1, 0x0511, 0x0581, 0x05BD, 0x0C41, 0x059F, 0x05BF, 0x040F, + 0x0C7D, 0x0402, 0x054E, 0x057D, 0x0403, 0x078D, 0x05AE, 0x042D, + 0x0483, 0x079D, 0x0D7F, 0x0482, 0x0611, 0x056E, 0x0516, 0x05BE, + 0x0535, 0x044E, 0x05AF, 0x0DED, 0x042F, 0x0492, 0x058E, 0x078F, + 0x0412, 0x057E, 0x053E, 0x0F1F, 0x073D, 0x0601, 0x0501, 0x075D, + 0x059E, 0x05CD, 0x053F, 0x054F, 0x055E, 0x055D, 0x0421, 0x074D, + 0x051F, 0x072F, 0x0781, 0x0411, 0x0D6F, 0x077E, 0x0487, 0x070E, + 0x070F, 0x072D, 0x058F, 0x078E, 0x079E, 0x052E, 0x0413, 0x072E, + 0x071D, 0x052F, 0x055F, 0x073E, 0x0417, 0x0453, 0x060E, 0x0622, + 0x0683, 0x0702, 0x070D, 0x071E, 0x076F, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_10x6 { + 10, 6, + { 325, 199 }, + { 922, 381 }, + { 0, 78 }, + { percentile_arr_10x6_0, percentile_arr_10x6_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 8) +static const uint16_t percentile_arr_10x8_0[400] { + 0x0154, 0xAB34, 0xAD44, 0x8308, 0x7866, 0x7B64, 0x79A4, 0x7975, + 0x686A, 0x6908, 0xC514, 0x6174, 0x6128, 0x6118, 0x5B54, 0x5163, + 0xF856, 0x50F5, 0x986F, 0xDD34, 0x48FE, 0x4972, 0x48E6, 0x4146, + 0x48EE, 0x40F3, 0x4AC1, 0x38C6, 0x41E2, 0xBB05, 0x707E, 0x38D6, + 0x3927, 0x6B14, 0x384B, 0x3948, 0x3153, 0x385A, 0x3134, 0x6B15, + 0x39F2, 0x30CF, 0x3143, 0x91D2, 0x31C3, 0x60EF, 0x5973, 0x3076, + 0x28D3, 0x3261, 0x2875, 0x28DE, 0x290C, 0x51E3, 0x28A7, 0x20E3, + 0x2962, 0x2B06, 0x2917, 0x483B, 0x20B6, 0x2D24, 0x206E, 0x285F, + 0x20B7, 0x2936, 0x4047, 0x2037, 0x20DF, 0x28BF, 0x21B4, 0x21B3, + 0x1D08, 0x2027, 0x404F, 0x3846, 0x2116, 0x187F, 0x1879, 0x2285, + 0x1A29, 0x3915, 0x4873, 0x1955, 0x3114, 0x1B44, 0x2165, 0x107A, + 0x1956, 0x6137, 0x1106, 0x3145, 0x1B21, 0x19D3, 0x12AD, 0x1B41, + 0x1AD1, 0x1126, 0x18F2, 0x282B, 0x40E5, 0x20D5, 0x2A0A, 0x284A, + 0x1286, 0x1295, 0x121A, 0x2A0B, 0x321B, 0x122D, 0x10FD, 0x13A1, + 0x32A2, 0x12E1, 0x1164, 0x13C1, 0x124D, 0x1239, 0x4504, 0x10C7, + 0x22F1, 0x11F1, 0x0AC2, 0x2125, 0x1225, 0x0B04, 0x1107, 0x1069, + 0x1A19, 0x13BF, 0x2A96, 0x08D2, 0x1271, 0x0952, 0x2BDF, 0x0B31, + 0x1251, 0x2124, 0x0B13, 0x12BD, 0x1233, 0x13EE, 0x2144, 0x0B16, + 0x0A15, 0x18E2, 0x08DD, 0x1097, 0x0857, 0x0B24, 0x0AA5, 0x12A3, + 0x11C2, 0x11D1, 0x10CE, 0x0865, 0x123D, 0x08B3, 0x0B51, 0x1971, + 0x0A41, 0x0A06, 0x1039, 0x080A, 0x0B22, 0x0923, 0x0836, 0x08C3, + 0x0A1F, 0x1072, 0x080B, 0x0935, 0x0855, 0x18A6, 0x0A42, 0x1133, + 0x0A83, 0x0A09, 0x0ACD, 0x0A2E, 0x0887, 0x083A, 0x10C5, 0x085E, + 0x13B1, 0x087D, 0x0819, 0x0A9F, 0x0049, 0x08F1, 0x0BEF, 0x1161, + 0x0B42, 0x09E1, 0x0A05, 0x0904, 0x12AE, 0x029E, 0x0A31, 0x09FF, + 0x0951, 0x0859, 0x001A, 0x082F, 0x0B81, 0x08B5, 0x0A35, 0x082A, + 0x08ED, 0x1142, 0x1262, 0x0B32, 0x08A5, 0x12D2, 0x03DD, 0x0B07, + 0x18AE, 0x083F, 0x00AF, 0x0AB3, 0x086D, 0x0287, 0x0A93, 0x025D, + 0x0816, 0x13FF, 0x0A8D, 0x005D, 0x08D1, 0x0392, 0x0845, 0x0AC3, + 0x08C2, 0x01A3, 0x0AB1, 0x09A2, 0x005B, 0x0B93, 0x02B2, 0x1086, + 0x001B, 0x0863, 0x0216, 0x0AA1, 0x0896, 0x0A8F, 0x084E, 0x0A8E, + 0x0A53, 0x0026, 0x0A26, 0x0382, 0x0807, 0x0862, 0x0029, 0x0871, + 0x00BD, 0x0835, 0x024E, 0x0806, 0x0941, 0x0895, 0x03AF, 0x0A13, + 0x0932, 0x03ED, 0x0BFD, 0x0207, 0x0B83, 0x0993, 0x09B1, 0x03CD, + 0x0A3E, 0x03FE, 0x0A21, 0x0015, 0x0B11, 0x0A43, 0x00E1, 0x136F, + 0x00BE, 0x00A2, 0x0842, 0x0043, 0x0825, 0x082E, 0x0A2A, 0x03DE, + 0x0BA2, 0x0122, 0x0BCF, 0x004D, 0x0323, 0x09C1, 0x0292, 0x083E, + 0x0252, 0x0017, 0x0A72, 0x00CD, 0x0182, 0x0A63, 0x0131, 0x09B2, + 0x0303, 0x0902, 0x0053, 0x035F, 0x0A32, 0x003D, 0x0992, 0x0A2F, + 0x03B2, 0x0ABE, 0x009F, 0x0183, 0x0312, 0x08B1, 0x0B02, 0x0A17, + 0x0B7F, 0x0333, 0x0297, 0x0A23, 0x020F, 0x0282, 0x0851, 0x0822, + 0x03CE, 0x01EE, 0x000E, 0x08B2, 0x0083, 0x0A1D, 0x00A3, 0x0222, + 0x088F, 0x0112, 0x029D, 0x0092, 0x0A3F, 0x0391, 0x089E, 0x0301, + 0x01FD, 0x09BF, 0x01CE, 0x0852, 0x01FE, 0x0013, 0x0903, 0x088E, + 0x037E, 0x021E, 0x01EF, 0x095F, 0x016F, 0x09DE, 0x03BE, 0x020E, + 0x0113, 0x01DF, 0x080F, 0x020D, 0x0833, 0x03AE, 0x0032, 0x03BD, + 0x0823, 0x001E, 0x01AF, 0x0203, 0x034F, 0x0093, 0x0A81, 0x036E, + 0x0291, 0x038E, 0x0A01, 0x001F, 0x017F, 0x01CF, 0x017E, 0x0202, + 0x0BAD, 0x0211, 0x035D, 0x035E, 0x039F, 0x0212, 0x032E, 0x033F, + 0x034D, 0x034E, 0x036D, 0x032F, 0x033E, 0x037D, 0x038F, 0x039E +}; + +static const uint16_t percentile_arr_10x8_1[221] { + 0x0621, 0xDFAE, 0x2443, 0x54C2, 0x37CD, 0x1CF1, 0xFCA3, 0x14D2, + 0x2D32, 0x5551, 0x7DDF, 0x5C33, 0x15D1, 0x3462, 0x24B3, 0x7452, + 0x5FBE, 0x6472, 0x65A2, 0x1D06, 0x445D, 0x15EF, 0x0E31, 0x1D71, + 0x343E, 0x0D42, 0x0CDD, 0x1F01, 0x4691, 0x1435, 0x0E82, 0x0DFF, + 0x17DD, 0x0D22, 0x24B2, 0x1603, 0x04B5, 0x24AE, 0x060D, 0x2D13, + 0x0C7D, 0x0496, 0x17BD, 0x1F4F, 0x1F7D, 0x1486, 0x0593, 0x1C16, + 0x0C07, 0x15FE, 0x041F, 0x14D1, 0x0C9F, 0x0E81, 0x0D15, 0x27AF, + 0x0C2E, 0x0D23, 0x176E, 0x0FAD, 0x1C06, 0x1561, 0x0DB1, 0x040B, + 0x1C4E, 0x0D83, 0x1711, 0x0C42, 0x0C71, 0x1C1A, 0x0D25, 0x04A2, + 0x0C45, 0x076D, 0x0F9F, 0x075F, 0x0E12, 0x046D, 0x048F, 0x1D92, + 0x0602, 0x0C39, 0x174E, 0x0C51, 0x0CA1, 0x075E, 0x05C1, 0x14BD, + 0x0D31, 0x0423, 0x0F3F, 0x0495, 0x0C93, 0x049E, 0x0D05, 0x04E1, + 0x0DEE, 0x0415, 0x04B1, 0x0503, 0x0CCD, 0x042F, 0x0DCF, 0x044D, + 0x0541, 0x1582, 0x05DE, 0x0D01, 0x0487, 0x040A, 0x0516, 0x0CA5, + 0x05FD, 0x05BF, 0x057D, 0x0DA1, 0x0426, 0x040F, 0x071F, 0x0613, + 0x0432, 0x0D12, 0x043D, 0x0425, 0x0461, 0x061D, 0x0D21, 0x0591, + 0x079D, 0x048D, 0x0429, 0x0C49, 0x04C1, 0x042A, 0x040E, 0x0485, + 0x0511, 0x0405, 0x0502, 0x0441, 0x0C19, 0x0692, 0x0535, 0x058F, + 0x041D, 0x059F, 0x072D, 0x04AD, 0x049D, 0x05CE, 0x048E, 0x0C31, + 0x057F, 0x078D, 0x0409, 0x041E, 0x05AE, 0x0611, 0x058E, 0x05DD, + 0x05CD, 0x056E, 0x0483, 0x073D, 0x054E, 0x0D9E, 0x0402, 0x0491, + 0x040D, 0x056F, 0x042D, 0x0581, 0x0421, 0x057E, 0x0781, 0x053E, + 0x0482, 0x078F, 0x0413, 0x052E, 0x0601, 0x0422, 0x0492, 0x055E, + 0x05BE, 0x0F9E, 0x072F, 0x074D, 0x0412, 0x070F, 0x075D, 0x05BD, + 0x051F, 0x071D, 0x073E, 0x077E, 0x0403, 0x0411, 0x078E, 0x055D, + 0x05AF, 0x05ED, 0x052F, 0x053F, 0x070D, 0x070E, 0x072E, 0x054F, + 0x0417, 0x041B, 0x0453, 0x055F, 0x060E, 0x0622, 0x0683, 0x068D, + 0x0702, 0x071E, 0x076F, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_10x8 = +{ + 10, 8, + { 400, 221 }, + { 1119, 376 }, + { 0, 52 }, + { percentile_arr_10x8_0, percentile_arr_10x8_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 10) +static const uint16_t percentile_arr_10x10_0[453] { + 0x0334, 0x9514, 0x8954, 0x806A, 0x6F14, 0x6724, 0x6108, 0x6364, + 0x5175, 0x5D44, 0x5866, 0x5118, 0x5308, 0xA179, 0x5128, 0xF534, + 0x49A4, 0x5354, 0x9174, 0x486F, 0x48EA, 0x40F3, 0x4963, 0x414A, + 0xF8F9, 0x3984, 0x4172, 0x387E, 0x405A, 0x38DA, 0x38F5, 0x9B05, + 0x30EE, 0x32C1, 0x3261, 0x3D08, 0x31E2, 0x3056, 0x292B, 0x3146, + 0x3127, 0x3315, 0x58CA, 0x58E6, 0x290C, 0x3314, 0x8134, 0x28E3, + 0x28FE, 0x2948, 0x28C6, 0x78DE, 0x28BB, 0x68D6, 0x286E, 0x2173, + 0x2962, 0x21D2, 0x205F, 0x49F2, 0x2917, 0x2306, 0x207F, 0x404F, + 0x2153, 0x2943, 0x20CF, 0x21C3, 0x2073, 0x20D3, 0x2136, 0x183B, + 0x430A, 0x40A7, 0x18B6, 0x2079, 0x2309, 0x2075, 0x184B, 0x20EF, + 0x187A, 0x7837, 0x1B19, 0x20AB, 0x18BA, 0x20B7, 0x1994, 0x19E3, + 0x21B4, 0x49B3, 0x38BF, 0x193B, 0x1876, 0x182B, 0x30F2, 0x193A, + 0x1827, 0x1965, 0x1914, 0x184A, 0x4047, 0x1916, 0x1285, 0x1937, + 0x122D, 0x1915, 0x1321, 0x1955, 0x1046, 0x191B, 0x2106, 0x2919, + 0x1344, 0x1524, 0x12E1, 0x3926, 0x10E5, 0x2295, 0x1159, 0x1145, + 0x10DF, 0x124D, 0x1271, 0x092A, 0x2169, 0x1704, 0x22A2, 0x1164, + 0x13EE, 0x12F1, 0x0AD1, 0x128A, 0x110A, 0x11D3, 0x1286, 0x115A, + 0x2BA1, 0x0BBF, 0x3956, 0x2A89, 0x12AD, 0x10E9, 0x0B41, 0x1A29, + 0x2225, 0x08FD, 0x1107, 0x08D5, 0x191A, 0x1125, 0x1A96, 0x0B04, + 0x18D9, 0x2B16, 0x11F1, 0x0A33, 0x0924, 0x131A, 0x1149, 0x1324, + 0x0BEF, 0x0A99, 0x08CB, 0x123D, 0x1331, 0x0BDF, 0x0872, 0x22A3, + 0x0AC2, 0x1144, 0x0D04, 0x08D2, 0x08CE, 0x0AA9, 0x0A9A, 0x0B13, + 0x1251, 0x0865, 0x1069, 0x0897, 0x1215, 0x18B3, 0x1A62, 0x08C7, + 0x185E, 0x10E2, 0x0AA5, 0x21FF, 0x090B, 0x0952, 0x09E1, 0x0A42, + 0x08F1, 0x0A06, 0x0B22, 0x087D, 0x1139, 0x021F, 0x122E, 0x082F, + 0x09C2, 0x0887, 0x0A0A, 0x03C1, 0x0929, 0x0A5D, 0x0A83, 0x0BFF, + 0x0935, 0x085B, 0x0104, 0x08DD, 0x0923, 0x083F, 0x0241, 0x09D1, + 0x0A39, 0x0863, 0x0A8B, 0x08A6, 0x008B, 0x1133, 0x13B1, 0x089B, + 0x0AB3, 0x0036, 0x0BDD, 0x08ED, 0x0857, 0x0971, 0x0219, 0x1235, + 0x0AB1, 0x0ACD, 0x036F, 0x0A31, 0x08AA, 0x003A, 0x08C3, 0x0A05, + 0x02BD, 0x0B92, 0x0B07, 0x12B2, 0x08C5, 0x0B51, 0x0381, 0x0A8D, + 0x01A3, 0x0896, 0x0855, 0x0BFD, 0x005D, 0x0BFE, 0x023E, 0x08AF, + 0x00B9, 0x0A93, 0x00B5, 0x0862, 0x0A0B, 0x0A09, 0x0A72, 0x0332, + 0x0AA1, 0x08C9, 0x024E, 0x1382, 0x0951, 0x00A5, 0x0A2A, 0x0059, + 0x0A9E, 0x0B42, 0x004E, 0x0942, 0x03ED, 0x09B2, 0x02D2, 0x0849, + 0x0035, 0x0216, 0x0961, 0x0BAF, 0x00AE, 0x0826, 0x0287, 0x0A1A, + 0x0393, 0x0221, 0x09A2, 0x086D, 0x0226, 0x0871, 0x0039, 0x082A, + 0x08C2, 0x08E1, 0x0845, 0x0207, 0x0B23, 0x0015, 0x00D1, 0x0B83, + 0x037F, 0x0252, 0x08A9, 0x0099, 0x0A13, 0x0053, 0x0807, 0x03CD, + 0x0BDE, 0x0016, 0x089A, 0x0232, 0x035F, 0x0A8E, 0x0AC3, 0x022F, + 0x0263, 0x0829, 0x004D, 0x0132, 0x0806, 0x0311, 0x01B1, 0x0941, + 0x0086, 0x000B, 0x1122, 0x0025, 0x0842, 0x00BD, 0x0BCF, 0x03A2, + 0x0043, 0x0B03, 0x0895, 0x0A8F, 0x008A, 0x09EF, 0x0253, 0x0A1B, + 0x0182, 0x0243, 0x0A92, 0x00CD, 0x083E, 0x030B, 0x0223, 0x081A, + 0x0A9F, 0x0193, 0x00BE, 0x0017, 0x0931, 0x0391, 0x037E, 0x09C1, + 0x0312, 0x0333, 0x03B2, 0x083D, 0x08B1, 0x00B2, 0x002E, 0x021D, + 0x0A9D, 0x0192, 0x02AE, 0x0102, 0x0022, 0x081B, 0x0222, 0x009E, + 0x021E, 0x000A, 0x089F, 0x0217, 0x0BCE, 0x0052, 0x020F, 0x0A97, + 0x0282, 0x008E, 0x0A3F, 0x01FD, 0x00A3, 0x0019, 0x08A2, 0x0301, + 0x036E, 0x01FE, 0x03BE, 0x0ABE, 0x01CE, 0x0302, 0x029B, 0x0051, + 0x0883, 0x008F, 0x0BAE, 0x01DF, 0x0183, 0x0912, 0x000E, 0x020D, + 0x01EE, 0x0B4F, 0x0033, 0x0103, 0x020E, 0x0832, 0x01AF, 0x0913, + 0x01DE, 0x0203, 0x001E, 0x0092, 0x0093, 0x000F, 0x015F, 0x0291, + 0x0281, 0x0813, 0x001F, 0x01CF, 0x033F, 0x0023, 0x01BF, 0x0202, + 0x016F, 0x017E, 0x03AD, 0x0201, 0x034E, 0x0BBD, 0x036D, 0x017F, + 0x0211, 0x038E, 0x0212, 0x032E, 0x034D, 0x035E, 0x037D, 0x039E, + 0x032F, 0x033E, 0x035D, 0x038F, 0x039F +}; + +static const uint16_t percentile_arr_10x10_1[234] { + 0x07CD, 0x6E21, 0x24F1, 0x8443, 0xD7AE, 0x24C2, 0x1C62, 0xCCA3, + 0x1C33, 0xFDEF, 0x2532, 0x55DF, 0x1472, 0x6C3E, 0x14D2, 0x34DD, + 0x1452, 0x745D, 0x4D51, 0x8DD1, 0x247D, 0x75FF, 0x0CB3, 0x17BE, + 0x6CAE, 0x17DD, 0x1571, 0x3D06, 0x4E31, 0x0DA2, 0x67BD, 0x160D, + 0x2C4E, 0x0D22, 0x176E, 0x3CB2, 0x142E, 0x4DFE, 0x0F4F, 0x1435, + 0x0F01, 0x0D42, 0x0F7D, 0x0CB5, 0x1E03, 0x149F, 0x1C96, 0x141F, + 0x14B9, 0x0FAF, 0x0439, 0x0E91, 0x2682, 0x1D13, 0x1FAD, 0x0407, + 0x3471, 0x0C86, 0x0F6D, 0x0D15, 0x0D61, 0x040B, 0x0C6D, 0x0C16, + 0x0C9A, 0x0D0A, 0x0593, 0x0CD1, 0x248F, 0x0C2F, 0x3C42, 0x1523, + 0x0445, 0x0E81, 0x0CA2, 0x1525, 0x0406, 0x1C8A, 0x0C1A, 0x04BD, + 0x0F5E, 0x0F3F, 0x1F4E, 0x0E1D, 0x0423, 0x0DCF, 0x044D, 0x0D92, + 0x0583, 0x0DB1, 0x1449, 0x15EE, 0x0F5F, 0x079F, 0x0D19, 0x0409, + 0x04CD, 0x05FD, 0x143D, 0x0612, 0x0D03, 0x0D82, 0x04B1, 0x0C95, + 0x0C2A, 0x049E, 0x05AF, 0x0D31, 0x05BE, 0x04E1, 0x0D05, 0x0516, + 0x0711, 0x05C1, 0x0509, 0x0D41, 0x0493, 0x048E, 0x0602, 0x05BF, + 0x0CA5, 0x0529, 0x0535, 0x0D12, 0x0539, 0x0451, 0x0C29, 0x071F, + 0x040A, 0x0F3D, 0x0432, 0x059F, 0x0425, 0x0C99, 0x05DE, 0x05CE, + 0x0C0F, 0x0489, 0x051A, 0x0501, 0x0415, 0x057F, 0x0431, 0x0E13, + 0x040D, 0x041D, 0x075D, 0x0C53, 0x0502, 0x04C1, 0x049D, 0x0426, + 0x040E, 0x05A1, 0x055F, 0x0781, 0x0591, 0x04A9, 0x048B, 0x0D8E, + 0x052E, 0x0412, 0x0521, 0x0405, 0x04AD, 0x074D, 0x0611, 0x077E, + 0x078F, 0x078D, 0x048D, 0x041E, 0x0487, 0x0461, 0x0C85, 0x05ED, + 0x0402, 0x0483, 0x0419, 0x0511, 0x0491, 0x0482, 0x059E, 0x068D, + 0x055D, 0x072E, 0x05DD, 0x054E, 0x0441, 0x0422, 0x052F, 0x057D, + 0x072D, 0x079D, 0x0CA1, 0x072F, 0x079E, 0x0581, 0x042D, 0x055E, + 0x0601, 0x0413, 0x0692, 0x0403, 0x051F, 0x053F, 0x054F, 0x05CD, + 0x070F, 0x071D, 0x05AE, 0x05BD, 0x0492, 0x056E, 0x0411, 0x0417, + 0x041B, 0x0421, 0x053E, 0x056F, 0x057E, 0x058F, 0x060E, 0x0622, + 0x0683, 0x0702, 0x070D, 0x070E, 0x071E, 0x073E, 0x076F, 0x078E, + 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_10x10 { + 10, 10, + { 453, 234 }, + { 1095, 472 }, + { 0, 70 }, + { percentile_arr_10x10_0, percentile_arr_10x10_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (12 * 10) +static const uint16_t percentile_arr_12x10_0[491] { + 0x0334, 0x9954, 0x8514, 0x7128, 0x6364, 0xC174, 0x5D34, 0x5866, + 0x5975, 0x5354, 0xAF14, 0x506A, 0x5108, 0x5724, 0x5308, 0x4544, + 0x4918, 0x4064, 0x49E2, 0x4179, 0x8163, 0x4054, 0xF81C, 0x394A, + 0x38F3, 0x4172, 0x38F5, 0xA06F, 0x68EA, 0x69F2, 0x3134, 0x31A4, + 0x305A, 0x68DA, 0x3056, 0x3146, 0x31F5, 0x3148, 0x5A61, 0x32C1, + 0x31D2, 0x307E, 0x29E3, 0x30E6, 0x59C3, 0x2984, 0x29B6, 0x28F9, + 0x5204, 0x28EE, 0x50CA, 0x2997, 0x48C6, 0x4838, 0x2953, 0x200C, + 0x2943, 0x2173, 0x2D08, 0x4162, 0x29B4, 0x2314, 0x21B3, 0x212B, + 0x210C, 0x48E3, 0x60DE, 0x205F, 0x20FE, 0x2028, 0x21A6, 0x404F, + 0x20D6, 0x2214, 0x2127, 0x1873, 0x40CF, 0x206E, 0x1B09, 0x21C6, + 0x2075, 0x19D5, 0x2305, 0x18D3, 0x2076, 0x1804, 0x230A, 0x304B, + 0x20BB, 0x18B6, 0x1936, 0x1B19, 0x3037, 0x187F, 0x18A7, 0x1B85, + 0x30BA, 0x183B, 0x1027, 0x18EF, 0x1B21, 0x1879, 0x10AB, 0x1917, + 0x1114, 0x18BF, 0x1074, 0x1994, 0x2847, 0x111B, 0x28F2, 0x11E5, + 0x19A7, 0x113A, 0x1046, 0x28B7, 0x207A, 0x182B, 0x1155, 0x104A, + 0x1344, 0x293B, 0x11D3, 0x2014, 0x1044, 0x1018, 0x13A1, 0x1315, + 0x2524, 0x20DF, 0x10E5, 0x1126, 0x12A2, 0x1824, 0x2271, 0x11F1, + 0x2964, 0x12D1, 0x115A, 0x092A, 0x2341, 0x1A2D, 0x12E1, 0x090A, + 0x13BF, 0x0A4D, 0x2119, 0x0BC1, 0x1233, 0x1A8A, 0x2008, 0x1159, + 0x1A89, 0x08D5, 0x1156, 0x0834, 0x13EE, 0x1169, 0x1187, 0x1AA3, + 0x1229, 0x1331, 0x0A85, 0x0937, 0x1704, 0x08FD, 0x2124, 0x0B13, + 0x1251, 0x0AAD, 0x082C, 0x091A, 0x18D9, 0x0A99, 0x1848, 0x18E9, + 0x0B95, 0x1144, 0x0AF1, 0x1A25, 0x131A, 0x09C5, 0x0986, 0x1BDF, + 0x0B24, 0x0965, 0x1262, 0x0949, 0x0872, 0x09C2, 0x12C2, 0x0916, + 0x085E, 0x0B06, 0x08CB, 0x08C7, 0x1242, 0x1BEF, 0x0A9A, 0x1152, + 0x08B3, 0x0AA9, 0x090B, 0x08D2, 0x1B22, 0x0B04, 0x0865, 0x0A15, + 0x1286, 0x0A83, 0x0A95, 0x09D1, 0x0A06, 0x0196, 0x1139, 0x0A3D, + 0x0933, 0x13B1, 0x0123, 0x0D04, 0x08E2, 0x122E, 0x08A6, 0x00CE, + 0x0A31, 0x1241, 0x0B51, 0x1057, 0x1171, 0x007D, 0x1145, 0x0A0A, + 0x0129, 0x09FF, 0x089B, 0x085B, 0x0063, 0x0AB1, 0x0A1F, 0x0A5D, + 0x0AA5, 0x0036, 0x0904, 0x0B86, 0x0A8B, 0x0897, 0x11E1, 0x0332, + 0x083F, 0x0A19, 0x02B3, 0x0859, 0x08C3, 0x0855, 0x11B5, 0x01A5, + 0x0AB2, 0x0392, 0x10DD, 0x09A3, 0x00ED, 0x0907, 0x1161, 0x002F, + 0x0887, 0x0216, 0x0ABD, 0x0B81, 0x0A93, 0x0A21, 0x003A, 0x0ACD, + 0x0AA1, 0x0A35, 0x0272, 0x0BDD, 0x03FE, 0x0BAF, 0x0869, 0x0213, + 0x088B, 0x020B, 0x00B5, 0x1035, 0x08F1, 0x0151, 0x0A4E, 0x0239, + 0x0BA2, 0x00AA, 0x0896, 0x0382, 0x0A08, 0x0A05, 0x0A09, 0x0142, + 0x086D, 0x004E, 0x0B23, 0x0106, 0x0807, 0x036F, 0x0995, 0x03FD, + 0x08AF, 0x08C5, 0x0062, 0x0053, 0x0B42, 0x0826, 0x021A, 0x01A2, + 0x09B1, 0x00C9, 0x09B2, 0x0045, 0x0207, 0x08B9, 0x00A5, 0x0AD2, + 0x0095, 0x003E, 0x0A32, 0x0383, 0x0849, 0x0135, 0x029E, 0x0A26, + 0x023E, 0x0BFF, 0x0A52, 0x0311, 0x001B, 0x0915, 0x0A8D, 0x0223, + 0x022A, 0x0BED, 0x0086, 0x0A96, 0x0222, 0x035F, 0x0A43, 0x085D, + 0x0303, 0x0393, 0x0A63, 0x082A, 0x037F, 0x0932, 0x0043, 0x0292, + 0x03CD, 0x0BDE, 0x009F, 0x0125, 0x08A9, 0x0253, 0x0015, 0x0192, + 0x0A17, 0x08C2, 0x0316, 0x00D1, 0x0282, 0x0871, 0x0312, 0x0122, + 0x0A9F, 0x02AE, 0x0006, 0x0A8E, 0x08E1, 0x0016, 0x0B0B, 0x00AE, + 0x0025, 0x0193, 0x0AC3, 0x0017, 0x0307, 0x00BD, 0x08BE, 0x0039, + 0x0BB2, 0x021B, 0x01FD, 0x084D, 0x03CE, 0x00A3, 0x0302, 0x0BCF, + 0x0033, 0x0391, 0x028F, 0x0852, 0x0287, 0x008A, 0x0333, 0x080B, + 0x0131, 0x01C1, 0x037E, 0x0A0F, 0x00B1, 0x002E, 0x0099, 0x0902, + 0x009A, 0x003D, 0x0982, 0x0301, 0x00CD, 0x0941, 0x0042, 0x0183, + 0x029D, 0x08A2, 0x021D, 0x001A, 0x0A97, 0x01EF, 0x01CE, 0x0051, + 0x0BAE, 0x022F, 0x03BE, 0x021E, 0x000A, 0x09DF, 0x0029, 0x020D, + 0x02BE, 0x029B, 0x09EE, 0x00B2, 0x0912, 0x036E, 0x009E, 0x0022, + 0x0019, 0x0892, 0x0032, 0x01FE, 0x0083, 0x023F, 0x0B96, 0x000E, + 0x008F, 0x0113, 0x0103, 0x001E, 0x0A0E, 0x0013, 0x008E, 0x0281, + 0x09AF, 0x017E, 0x0203, 0x016F, 0x0291, 0x0023, 0x0093, 0x03BD, + 0x001F, 0x01CF, 0x01DE, 0x0201, 0x01BF, 0x0B4F, 0x000F, 0x0202, + 0x037D, 0x038E, 0x0211, 0x0212, 0x034E, 0x039F, 0x03AD, 0x015F, + 0x017F, 0x032E, 0x033F, 0x034D, 0x035E, 0x036D, 0x032F, 0x033E, + 0x035D, 0x038F, 0x039E +}; + +static const uint16_t percentile_arr_12x10_1[240] { + 0x0621, 0xA443, 0xFCC2, 0x3CA3, 0x1D32, 0x14F1, 0x7462, 0x1433, + 0x27CD, 0x2571, 0x57AE, 0x5DD1, 0x64B3, 0x44D2, 0x2C72, 0x25A2, + 0x1E31, 0x55DF, 0x4C52, 0x1DEF, 0x0D51, 0x3C5D, 0x3C3E, 0x74DD, + 0x347D, 0x27BE, 0x5CB5, 0x17DD, 0x2C14, 0x0CAE, 0x24B2, 0x15FF, + 0x2701, 0x0D42, 0x1FBD, 0x0C35, 0x1603, 0x060D, 0x1D93, 0x0C96, + 0x1C07, 0x1522, 0x0D06, 0x0F4F, 0x0C9F, 0x1F6E, 0x0D86, 0x0C2E, + 0x1DFE, 0x0682, 0x1E91, 0x0F7D, 0x0C86, 0x040B, 0x1513, 0x044E, + 0x14D1, 0x0C39, 0x14B9, 0x1C71, 0x05B1, 0x0C1F, 0x0681, 0x1445, + 0x0C16, 0x0D95, 0x1583, 0x0D61, 0x0FAD, 0x1442, 0x048F, 0x0D0A, + 0x049A, 0x0F6D, 0x146D, 0x0C2F, 0x0D25, 0x0406, 0x0C1A, 0x0D23, + 0x0612, 0x0FAF, 0x0F11, 0x0592, 0x0515, 0x14E1, 0x0602, 0x048A, + 0x0E1D, 0x0CBD, 0x0F9F, 0x0423, 0x075E, 0x174E, 0x0426, 0x0404, + 0x0C22, 0x0CA2, 0x0DEE, 0x0CA5, 0x0F3F, 0x05C1, 0x0CCD, 0x0503, + 0x044D, 0x0D16, 0x0449, 0x0D82, 0x0613, 0x0585, 0x0519, 0x0C95, + 0x075F, 0x0D35, 0x04B1, 0x0509, 0x0531, 0x0DA1, 0x049E, 0x040A, + 0x05CF, 0x0D41, 0x0415, 0x0692, 0x05FD, 0x0C25, 0x04A1, 0x0529, + 0x0591, 0x0C93, 0x057F, 0x04C1, 0x0512, 0x051A, 0x078D, 0x0451, + 0x0C0F, 0x0487, 0x0611, 0x0432, 0x042A, 0x05AF, 0x0461, 0x072D, + 0x0409, 0x0405, 0x0D39, 0x05DE, 0x048E, 0x0499, 0x0483, 0x04A9, + 0x0491, 0x042D, 0x049D, 0x0429, 0x040E, 0x05AE, 0x0521, 0x043D, + 0x0581, 0x05DD, 0x0492, 0x0CAD, 0x041E, 0x058F, 0x071F, 0x072F, + 0x0419, 0x073D, 0x057D, 0x0511, 0x05CE, 0x041D, 0x0485, 0x056E, + 0x0412, 0x0431, 0x05BF, 0x0441, 0x054E, 0x0489, 0x0421, 0x0502, + 0x0408, 0x040D, 0x051F, 0x059F, 0x073E, 0x078F, 0x0482, 0x079D, + 0x0C02, 0x05BE, 0x048B, 0x0411, 0x0505, 0x057E, 0x052E, 0x074D, + 0x077E, 0x054F, 0x0601, 0x055F, 0x068D, 0x070D, 0x070F, 0x071E, + 0x072E, 0x05CD, 0x0403, 0x0501, 0x055D, 0x059E, 0x0781, 0x0413, + 0x0417, 0x041B, 0x0453, 0x048D, 0x052F, 0x053E, 0x053F, 0x055E, + 0x056F, 0x058E, 0x05BD, 0x05ED, 0x060E, 0x0622, 0x0683, 0x0702, + 0x070E, 0x071D, 0x075D, 0x076F, 0x078E, 0x079E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_12x10 = +{ + 12, 10, + { 491, 240 }, + { 1099, 341 }, + { 0, 23 }, + { percentile_arr_12x10_0, percentile_arr_12x10_1 } +}; +#endif + +#if ASTCENC_BLOCK_MAX_TEXELS >= (12 * 12) +static const uint16_t percentile_arr_12x12_0[529] { + 0x0334, 0xF534, 0x8514, 0x8954, 0x7F14, 0xFB54, 0x7B08, 0x7128, + 0x7974, 0x6179, 0x6B64, 0x6908, 0x606A, 0x6724, 0xB544, 0xB066, + 0xA14A, 0x5118, 0x9975, 0x51F9, 0x981C, 0x49CA, 0x4854, 0x886F, + 0x88D4, 0x48EE, 0x41E2, 0x4163, 0x40F3, 0x4261, 0x4064, 0x407E, + 0x385A, 0x42C1, 0x4172, 0x38EA, 0x3946, 0x78CF, 0xA056, 0x38DE, + 0x3D08, 0x38F9, 0x3B14, 0x38FE, 0xA134, 0x38B8, 0x31A4, 0x71D2, + 0x60DA, 0x39C3, 0x99BA, 0x60CA, 0x39F2, 0x30F5, 0x304F, 0x31B6, + 0x31F5, 0x3204, 0x3148, 0x305F, 0x2953, 0x3194, 0x3184, 0x310C, + 0x889C, 0x300C, 0x2943, 0x30EF, 0x28C6, 0x2997, 0x2838, 0x58E6, + 0x20E4, 0x28E3, 0x2873, 0x29E3, 0x2A84, 0x28D3, 0x492B, 0x2962, + 0x286E, 0x20BF, 0x21AA, 0x29A6, 0x6A14, 0x2828, 0x89C6, 0x21B3, + 0x2305, 0x29B4, 0x2173, 0x2127, 0x20D6, 0x407F, 0x2294, 0x21D9, + 0x21D5, 0x2004, 0x404B, 0x18DF, 0x2079, 0x219B, 0x18A8, 0x2385, + 0x1936, 0x21AB, 0x188C, 0x1B09, 0x18BA, 0x203B, 0x187A, 0x1875, + 0x2344, 0x18BB, 0x18B6, 0x193A, 0x1837, 0x1914, 0x1846, 0x1876, + 0x1884, 0x1D24, 0x182B, 0x284A, 0x18A7, 0x18AB, 0x1917, 0x322D, + 0x1047, 0x1874, 0x1818, 0x18F2, 0x1164, 0x1B89, 0x2959, 0x1B21, + 0x39E5, 0x1827, 0x10F4, 0x18B7, 0x11D3, 0x1A4D, 0x1315, 0x12AD, + 0x1AD1, 0x3A71, 0x1319, 0x11A7, 0x2044, 0x2F04, 0x2341, 0x10E5, + 0x1155, 0x195A, 0x1024, 0x111B, 0x1251, 0x1233, 0x12E1, 0x13A1, + 0x13BF, 0x212A, 0x22A2, 0x113B, 0x23DF, 0x10D5, 0x2399, 0x0814, + 0x1126, 0x13EE, 0x1285, 0x10C4, 0x18FD, 0x20D9, 0x0987, 0x1242, + 0x29C5, 0x2313, 0x0898, 0x13C1, 0x08C8, 0x11F1, 0x1034, 0x1B24, + 0x0B0A, 0x11E9, 0x0808, 0x125D, 0x18E9, 0x0848, 0x1395, 0x0965, + 0x123D, 0x2186, 0x1295, 0x18CE, 0x098B, 0x0BEF, 0x1504, 0x082C, + 0x0A41, 0x1144, 0x0A89, 0x0956, 0x1331, 0x085E, 0x0B04, 0x128A, + 0x12A3, 0x1937, 0x19C2, 0x0952, 0x0872, 0x08B4, 0x1262, 0x1124, + 0x1969, 0x1063, 0x0AF1, 0x1225, 0x0894, 0x11C9, 0x18D2, 0x0ACD, + 0x0A29, 0x0B06, 0x09B5, 0x18C7, 0x0916, 0x1088, 0x09FF, 0x2206, + 0x0A15, 0x08B3, 0x0B51, 0x0A1F, 0x18CB, 0x0AC2, 0x0A2E, 0x1865, + 0x08AC, 0x0A31, 0x08A4, 0x138A, 0x0A99, 0x09D1, 0x0A86, 0x189B, + 0x0283, 0x0BDD, 0x0ABD, 0x1933, 0x083F, 0x1386, 0x0923, 0x0322, + 0x0869, 0x10DD, 0x13B1, 0x082F, 0x087D, 0x11B9, 0x085B, 0x08ED, + 0x00C3, 0x08E2, 0x084E, 0x0887, 0x0855, 0x0A0A, 0x0857, 0x0B92, + 0x1036, 0x12A5, 0x0293, 0x0945, 0x08A6, 0x0196, 0x19A3, 0x036F, + 0x0904, 0x1205, 0x09E1, 0x0381, 0x0971, 0x1219, 0x0BAF, 0x0949, + 0x00AF, 0x0AA9, 0x018A, 0x0907, 0x0BFD, 0x003A, 0x0BCD, 0x0AB2, + 0x088B, 0x0252, 0x0A4E, 0x03FF, 0x0845, 0x0897, 0x0059, 0x090B, + 0x0B42, 0x0807, 0x0A16, 0x0853, 0x0A8D, 0x01B2, 0x0AB1, 0x091A, + 0x0195, 0x0A35, 0x00B5, 0x10AA, 0x0115, 0x0A21, 0x0096, 0x0A08, + 0x03FE, 0x0B7F, 0x08B9, 0x12B3, 0x023E, 0x0A23, 0x029E, 0x08F1, + 0x01A9, 0x0BDE, 0x0843, 0x02D2, 0x0A1A, 0x08C5, 0x0151, 0x0A43, + 0x0332, 0x0383, 0x0826, 0x0BED, 0x10C2, 0x00AE, 0x0B82, 0x0213, + 0x0232, 0x085D, 0x02A1, 0x101B, 0x035F, 0x0303, 0x0A39, 0x0207, + 0x0A53, 0x0142, 0x01A5, 0x082A, 0x0099, 0x0A17, 0x03CF, 0x0906, + 0x0125, 0x0A96, 0x0A9A, 0x0209, 0x0393, 0x0961, 0x0131, 0x0A88, + 0x0139, 0x099A, 0x0292, 0x0272, 0x0862, 0x08BE, 0x0141, 0x02C3, + 0x0886, 0x0039, 0x08A9, 0x01A2, 0x01B1, 0x0851, 0x020B, 0x086D, + 0x0312, 0x08CD, 0x020F, 0x0311, 0x0BCE, 0x0135, 0x0006, 0x0849, + 0x0132, 0x0A8F, 0x022F, 0x022A, 0x0AAE, 0x0A8E, 0x0263, 0x03A2, + 0x083E, 0x009A, 0x021B, 0x0835, 0x0323, 0x0871, 0x0993, 0x0226, + 0x0302, 0x0922, 0x0119, 0x0222, 0x021D, 0x0B07, 0x08C9, 0x037E, + 0x08BD, 0x0042, 0x00D1, 0x0B33, 0x01C1, 0x0B9A, 0x0282, 0x088A, + 0x0182, 0x083D, 0x004D, 0x010A, 0x0A1E, 0x0019, 0x00B2, 0x0999, + 0x00A5, 0x0095, 0x0817, 0x0022, 0x031A, 0x0902, 0x00A3, 0x01BF, + 0x029F, 0x0816, 0x03B2, 0x0015, 0x0391, 0x0BBE, 0x01FE, 0x1129, + 0x002E, 0x01DF, 0x0301, 0x0033, 0x0B6E, 0x00E1, 0x0297, 0x00B1, + 0x009F, 0x0B16, 0x000A, 0x001A, 0x0052, 0x080B, 0x030B, 0x029D, + 0x0BAE, 0x01FD, 0x020E, 0x00A2, 0x0A3F, 0x0192, 0x0ABE, 0x020D, + 0x008F, 0x028B, 0x0083, 0x0025, 0x09EE, 0x01EF, 0x0029, 0x0291, + 0x0B4F, 0x0396, 0x0287, 0x008E, 0x0092, 0x0B4E, 0x017E, 0x001E, + 0x009E, 0x0103, 0x080F, 0x000E, 0x0113, 0x0203, 0x01CF, 0x0183, + 0x01CE, 0x001F, 0x0112, 0x01DE, 0x038E, 0x0832, 0x033E, 0x0212, + 0x029B, 0x0023, 0x016F, 0x0201, 0x09AF, 0x0202, 0x0281, 0x035E, + 0x034D, 0x037D, 0x03AD, 0x0013, 0x0093, 0x015F, 0x0211, 0x033F, + 0x036D, 0x039F, 0x03BD, 0x017F, 0x032E, 0x032F, 0x035D, 0x038F, + 0x039E +}; + +static const uint16_t percentile_arr_12x12_1[246] { + 0x0443, 0xFFCD, 0x2C62, 0x2E21, 0x3CF1, 0x34C2, 0x4CDD, 0x2452, + 0xD5DF, 0x1DD1, 0x0FAE, 0x64A3, 0x0C7D, 0x3433, 0x1CD2, 0x2DEF, + 0x0C3E, 0x1D71, 0xA472, 0x0D32, 0x54B3, 0x4D51, 0x445D, 0x0E31, + 0x1FDD, 0x0DFF, 0x0CAE, 0x45A2, 0x2FBE, 0xA4B9, 0x1C4E, 0x2C9F, + 0x160D, 0x0D42, 0x342E, 0x074F, 0x1414, 0x0F6E, 0x0CB2, 0x34B5, + 0x0DFE, 0x0D86, 0x1496, 0x1D22, 0x0691, 0x140B, 0x041F, 0x0C35, + 0x1D93, 0x1506, 0x1439, 0x0C9A, 0x0F01, 0x2442, 0x0C8F, 0x04D1, + 0x1486, 0x0C6D, 0x0513, 0x0C71, 0x0E82, 0x177D, 0x0E03, 0x07BD, + 0x0C2F, 0x0D83, 0x07AF, 0x0D61, 0x1407, 0x0DB1, 0x050A, 0x0C94, + 0x07AD, 0x0D8A, 0x0C04, 0x0416, 0x0C49, 0x0445, 0x15C1, 0x0C1A, + 0x0525, 0x0595, 0x0C8A, 0x075E, 0x0CBD, 0x0681, 0x0F4E, 0x075F, + 0x061D, 0x1541, 0x0CB1, 0x0F3F, 0x0406, 0x076D, 0x0DCF, 0x05EE, + 0x0D23, 0x0599, 0x0CCD, 0x0711, 0x0C23, 0x079F, 0x0D15, 0x0585, + 0x04A2, 0x042A, 0x0D31, 0x05BF, 0x0D92, 0x0C26, 0x043D, 0x0C93, + 0x0502, 0x0C15, 0x048B, 0x0D03, 0x0613, 0x0516, 0x0495, 0x0C29, + 0x04A5, 0x040F, 0x0425, 0x0539, 0x0D19, 0x04E1, 0x05BE, 0x0422, + 0x0432, 0x0C0A, 0x0431, 0x041E, 0x0492, 0x04A9, 0x0582, 0x0529, + 0x0487, 0x0C4D, 0x0512, 0x049E, 0x0505, 0x0451, 0x0D7F, 0x0489, + 0x0602, 0x05DE, 0x0591, 0x0535, 0x074D, 0x055E, 0x04C1, 0x0612, + 0x05DD, 0x05FD, 0x0C61, 0x0521, 0x0484, 0x05CE, 0x0581, 0x0491, + 0x051A, 0x04A1, 0x048E, 0x040D, 0x0499, 0x071F, 0x072E, 0x075D, + 0x0441, 0x0589, 0x057E, 0x0CAD, 0x0501, 0x054F, 0x0692, 0x0511, + 0x049D, 0x0509, 0x056E, 0x040E, 0x0409, 0x0601, 0x048D, 0x0413, + 0x053E, 0x0419, 0x072D, 0x0408, 0x0485, 0x042D, 0x041D, 0x05A1, + 0x0781, 0x0402, 0x05ED, 0x0C82, 0x0403, 0x057D, 0x05CD, 0x0611, + 0x0488, 0x0411, 0x054E, 0x051F, 0x053F, 0x056F, 0x059F, 0x070F, + 0x071D, 0x073D, 0x073E, 0x077E, 0x078F, 0x0405, 0x079D, 0x079E, + 0x058E, 0x0412, 0x055D, 0x05AE, 0x041B, 0x0421, 0x0453, 0x0417, + 0x0483, 0x052E, 0x052F, 0x055F, 0x058F, 0x059E, 0x05AF, 0x05BD, + 0x060E, 0x0622, 0x0683, 0x068D, 0x0702, 0x070D, 0x070E, 0x071E, + 0x072F, 0x076F, 0x078D, 0x078E, 0x07BF, 0x07CE +}; + +static const packed_percentile_table block_pcd_12x12 { + 12, 12, + { 529, 246 }, + { 1435, 335 }, + { 0, 22 }, + { percentile_arr_12x12_0, percentile_arr_12x12_1 } +}; +#endif + +/** + * @brief Fetch the packed percentile table for the given 2D block size. + * + * @param xdim The block x size. + * @param ydim The block y size. + * + * @return The packed table. + */ +static const packed_percentile_table *get_packed_table( + int xdim, + int ydim +) { + int idx = (ydim << 8) | xdim; + switch (idx) + { +#if ASTCENC_BLOCK_MAX_TEXELS >= (4 * 4) + case 0x0404: return &block_pcd_4x4; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (5 * 4) + case 0x0405: return &block_pcd_5x4; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (5 * 5) + case 0x0505: return &block_pcd_5x5; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (6 * 5) + case 0x0506: return &block_pcd_6x5; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (6 * 6) + case 0x0606: return &block_pcd_6x6; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 5) + case 0x0508: return &block_pcd_8x5; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 6) + case 0x0608: return &block_pcd_8x6; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (8 * 8) + case 0x0808: return &block_pcd_8x8; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 5) + case 0x050A: return &block_pcd_10x5; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 6) + case 0x060A: return &block_pcd_10x6; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 8) + case 0x080A: return &block_pcd_10x8; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (10 * 10) + case 0x0A0A: return &block_pcd_10x10; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (12 * 10) + case 0x0A0C: return &block_pcd_12x10; +#endif +#if ASTCENC_BLOCK_MAX_TEXELS >= (12 * 12) + case 0x0C0C: return &block_pcd_12x12; +#endif + } + + // Should never hit this with a valid 2D block size + return nullptr; +} + +/* See header for documentation. */ +const float *get_2d_percentile_table( + unsigned int xdim, + unsigned int ydim +) { + float* unpacked_table = new float[2048]; + const packed_percentile_table *apt = get_packed_table(xdim, ydim); + + // Set the default percentile + for (unsigned int i = 0; i < 2048; i++) + { + unpacked_table[i] = 1.0f; + } + + // Populate the unpacked percentile values + for (int i = 0; i < 2; i++) + { + unsigned int itemcount = apt->item_count[i]; + unsigned int difscale = apt->difscales[i]; + unsigned int accum = apt->initial_percs[i]; + const uint16_t *item_ptr = apt->items[i]; + + for (unsigned int j = 0; j < itemcount; j++) + { + uint16_t item = item_ptr[j]; + unsigned int idx = item & 0x7FF; + unsigned int weight = (item >> 11) & 0x1F; + accum += weight; + unpacked_table[idx] = static_cast<float>(accum) / static_cast<float>(difscale); + } + } + + return unpacked_table; +} +#endif + +/* See header for documentation. */ +bool is_legal_2d_block_size( + unsigned int xdim, + unsigned int ydim +) { + unsigned int idx = (xdim << 8) | ydim; + switch (idx) + { + case 0x0404: + case 0x0504: + case 0x0505: + case 0x0605: + case 0x0606: + case 0x0805: + case 0x0806: + case 0x0808: + case 0x0A05: + case 0x0A06: + case 0x0A08: + case 0x0A0A: + case 0x0C0A: + case 0x0C0C: + return true; + } + + return false; +} + +/* See header for documentation. */ +bool is_legal_3d_block_size( + unsigned int xdim, + unsigned int ydim, + unsigned int zdim +) { + unsigned int idx = (xdim << 16) | (ydim << 8) | zdim; + switch (idx) + { + case 0x030303: + case 0x040303: + case 0x040403: + case 0x040404: + case 0x050404: + case 0x050504: + case 0x050505: + case 0x060505: + case 0x060605: + case 0x060606: + return true; + } + + return false; +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_pick_best_endpoint_format.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_pick_best_endpoint_format.cpp new file mode 100644 index 00000000000..5bf0b367897 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_pick_best_endpoint_format.cpp @@ -0,0 +1,1357 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions for finding best endpoint format. + * + * We assume there are two independent sources of error in any given partition: + * + * - Encoding choice errors + * - Quantization errors + * + * Encoding choice errors are caused by encoder decisions. For example: + * + * - Using luminance instead of separate RGB components. + * - Using a constant 1.0 alpha instead of storing an alpha component. + * - Using RGB+scale instead of storing two full RGB endpoints. + * + * Quantization errors occur due to the limited precision we use for storage. These errors generally + * scale with quantization level, but are not actually independent of color encoding. In particular: + * + * - If we can use offset encoding then quantization error is halved. + * - If we can use blue-contraction then quantization error for RG is halved. + * - If we use HDR endpoints the quantization error is higher. + * + * Apart from these effects, we assume the error is proportional to the quantization step size. + */ + + +#include "astcenc_internal.h" +#include "astcenc_vecmathlib.h" + +#include <assert.h> + +/** + * @brief Compute the errors of the endpoint line options for one partition. + * + * Uncorrelated data assumes storing completely independent RGBA channels for each endpoint. Same + * chroma data assumes storing RGBA endpoints which pass though the origin (LDR only). RGBL data + * assumes storing RGB + lumashift (HDR only). Luminance error assumes storing RGB channels as a + * single value. + * + * + * @param pi The partition info data. + * @param partition_index The partition index to compule the error for. + * @param blk The image block. + * @param uncor_pline The endpoint line assuming uncorrelated endpoints. + * @param[out] uncor_err The computed error for the uncorrelated endpoint line. + * @param samec_pline The endpoint line assuming the same chroma for both endpoints. + * @param[out] samec_err The computed error for the uncorrelated endpoint line. + * @param rgbl_pline The endpoint line assuming RGB + lumashift data. + * @param[out] rgbl_err The computed error for the RGB + lumashift endpoint line. + * @param l_pline The endpoint line assuming luminance data. + * @param[out] l_err The computed error for the luminance endpoint line. + * @param[out] a_drop_err The computed error for dropping the alpha component. + */ +static void compute_error_squared_rgb_single_partition( + const partition_info& pi, + int partition_index, + const image_block& blk, + const processed_line3& uncor_pline, + float& uncor_err, + const processed_line3& samec_pline, + float& samec_err, + const processed_line3& rgbl_pline, + float& rgbl_err, + const processed_line3& l_pline, + float& l_err, + float& a_drop_err +) { + vfloat4 ews = blk.channel_weight; + + unsigned int texel_count = pi.partition_texel_count[partition_index]; + const uint8_t* texel_indexes = pi.texels_of_partition[partition_index]; + promise(texel_count > 0); + + vfloatacc a_drop_errv = vfloatacc::zero(); + vfloat default_a(blk.get_default_alpha()); + + vfloatacc uncor_errv = vfloatacc::zero(); + vfloat uncor_bs0(uncor_pline.bs.lane<0>()); + vfloat uncor_bs1(uncor_pline.bs.lane<1>()); + vfloat uncor_bs2(uncor_pline.bs.lane<2>()); + + vfloat uncor_amod0(uncor_pline.amod.lane<0>()); + vfloat uncor_amod1(uncor_pline.amod.lane<1>()); + vfloat uncor_amod2(uncor_pline.amod.lane<2>()); + + vfloatacc samec_errv = vfloatacc::zero(); + vfloat samec_bs0(samec_pline.bs.lane<0>()); + vfloat samec_bs1(samec_pline.bs.lane<1>()); + vfloat samec_bs2(samec_pline.bs.lane<2>()); + + vfloatacc rgbl_errv = vfloatacc::zero(); + vfloat rgbl_bs0(rgbl_pline.bs.lane<0>()); + vfloat rgbl_bs1(rgbl_pline.bs.lane<1>()); + vfloat rgbl_bs2(rgbl_pline.bs.lane<2>()); + + vfloat rgbl_amod0(rgbl_pline.amod.lane<0>()); + vfloat rgbl_amod1(rgbl_pline.amod.lane<1>()); + vfloat rgbl_amod2(rgbl_pline.amod.lane<2>()); + + vfloatacc l_errv = vfloatacc::zero(); + vfloat l_bs0(l_pline.bs.lane<0>()); + vfloat l_bs1(l_pline.bs.lane<1>()); + vfloat l_bs2(l_pline.bs.lane<2>()); + + vint lane_ids = vint::lane_id(); + for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) + { + vint tix(texel_indexes + i); + + vmask mask = lane_ids < vint(texel_count); + lane_ids += vint(ASTCENC_SIMD_WIDTH); + + // Compute the error that arises from just ditching alpha + vfloat data_a = gatherf(blk.data_a, tix); + vfloat alpha_diff = data_a - default_a; + alpha_diff = alpha_diff * alpha_diff; + + haccumulate(a_drop_errv, alpha_diff, mask); + + vfloat data_r = gatherf(blk.data_r, tix); + vfloat data_g = gatherf(blk.data_g, tix); + vfloat data_b = gatherf(blk.data_b, tix); + + // Compute uncorrelated error + vfloat param = data_r * uncor_bs0 + + data_g * uncor_bs1 + + data_b * uncor_bs2; + + vfloat dist0 = (uncor_amod0 + param * uncor_bs0) - data_r; + vfloat dist1 = (uncor_amod1 + param * uncor_bs1) - data_g; + vfloat dist2 = (uncor_amod2 + param * uncor_bs2) - data_b; + + vfloat error = dist0 * dist0 * ews.lane<0>() + + dist1 * dist1 * ews.lane<1>() + + dist2 * dist2 * ews.lane<2>(); + + haccumulate(uncor_errv, error, mask); + + // Compute same chroma error - no "amod", its always zero + param = data_r * samec_bs0 + + data_g * samec_bs1 + + data_b * samec_bs2; + + dist0 = (param * samec_bs0) - data_r; + dist1 = (param * samec_bs1) - data_g; + dist2 = (param * samec_bs2) - data_b; + + error = dist0 * dist0 * ews.lane<0>() + + dist1 * dist1 * ews.lane<1>() + + dist2 * dist2 * ews.lane<2>(); + + haccumulate(samec_errv, error, mask); + + // Compute rgbl error + param = data_r * rgbl_bs0 + + data_g * rgbl_bs1 + + data_b * rgbl_bs2; + + dist0 = (rgbl_amod0 + param * rgbl_bs0) - data_r; + dist1 = (rgbl_amod1 + param * rgbl_bs1) - data_g; + dist2 = (rgbl_amod2 + param * rgbl_bs2) - data_b; + + error = dist0 * dist0 * ews.lane<0>() + + dist1 * dist1 * ews.lane<1>() + + dist2 * dist2 * ews.lane<2>(); + + haccumulate(rgbl_errv, error, mask); + + // Compute luma error - no "amod", its always zero + param = data_r * l_bs0 + + data_g * l_bs1 + + data_b * l_bs2; + + dist0 = (param * l_bs0) - data_r; + dist1 = (param * l_bs1) - data_g; + dist2 = (param * l_bs2) - data_b; + + error = dist0 * dist0 * ews.lane<0>() + + dist1 * dist1 * ews.lane<1>() + + dist2 * dist2 * ews.lane<2>(); + + haccumulate(l_errv, error, mask); + } + + a_drop_err = hadd_s(a_drop_errv) * ews.lane<3>(); + uncor_err = hadd_s(uncor_errv); + samec_err = hadd_s(samec_errv); + rgbl_err = hadd_s(rgbl_errv); + l_err = hadd_s(l_errv); +} + +/** + * @brief For a given set of input colors and partitioning determine endpoint encode errors. + * + * This function determines the color error that results from RGB-scale encoding (LDR only), + * RGB-lumashift encoding (HDR only), luminance-encoding, and alpha drop. Also determines whether + * the endpoints are eligible for offset encoding or blue-contraction + * + * @param blk The image block. + * @param pi The partition info data. + * @param ep The idealized endpoints. + * @param[out] eci The resulting encoding choice error metrics. + */ +static void compute_encoding_choice_errors( + const image_block& blk, + const partition_info& pi, + const endpoints& ep, + encoding_choice_errors eci[BLOCK_MAX_PARTITIONS]) +{ + int partition_count = pi.partition_count; + promise(partition_count > 0); + + partition_metrics pms[BLOCK_MAX_PARTITIONS]; + + compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms); + + for (int i = 0; i < partition_count; i++) + { + partition_metrics& pm = pms[i]; + + line3 uncor_rgb_lines; + line3 samec_rgb_lines; // for LDR-RGB-scale + line3 rgb_luma_lines; // for HDR-RGB-scale + + processed_line3 uncor_rgb_plines; + processed_line3 samec_rgb_plines; + processed_line3 rgb_luma_plines; + processed_line3 luminance_plines; + + float uncorr_rgb_error; + float samechroma_rgb_error; + float rgb_luma_error; + float luminance_rgb_error; + float alpha_drop_error; + + uncor_rgb_lines.a = pm.avg; + uncor_rgb_lines.b = normalize_safe(pm.dir, unit3()); + + samec_rgb_lines.a = vfloat4::zero(); + samec_rgb_lines.b = normalize_safe(pm.avg, unit3()); + + rgb_luma_lines.a = pm.avg; + rgb_luma_lines.b = unit3(); + + uncor_rgb_plines.amod = uncor_rgb_lines.a - uncor_rgb_lines.b * dot3(uncor_rgb_lines.a, uncor_rgb_lines.b); + uncor_rgb_plines.bs = uncor_rgb_lines.b; + + // Same chroma always goes though zero, so this is simpler than the others + samec_rgb_plines.amod = vfloat4::zero(); + samec_rgb_plines.bs = samec_rgb_lines.b; + + rgb_luma_plines.amod = rgb_luma_lines.a - rgb_luma_lines.b * dot3(rgb_luma_lines.a, rgb_luma_lines.b); + rgb_luma_plines.bs = rgb_luma_lines.b; + + // Luminance always goes though zero, so this is simpler than the others + luminance_plines.amod = vfloat4::zero(); + luminance_plines.bs = unit3(); + + compute_error_squared_rgb_single_partition( + pi, i, blk, + uncor_rgb_plines, uncorr_rgb_error, + samec_rgb_plines, samechroma_rgb_error, + rgb_luma_plines, rgb_luma_error, + luminance_plines, luminance_rgb_error, + alpha_drop_error); + + // Determine if we can offset encode RGB lanes + vfloat4 endpt0 = ep.endpt0[i]; + vfloat4 endpt1 = ep.endpt1[i]; + vfloat4 endpt_diff = abs(endpt1 - endpt0); + vmask4 endpt_can_offset = endpt_diff < vfloat4(0.12f * 65535.0f); + bool can_offset_encode = (mask(endpt_can_offset) & 0x7) == 0x7; + + // Store out the settings + eci[i].rgb_scale_error = (samechroma_rgb_error - uncorr_rgb_error) * 0.7f; // empirical + eci[i].rgb_luma_error = (rgb_luma_error - uncorr_rgb_error) * 1.5f; // wild guess + eci[i].luminance_error = (luminance_rgb_error - uncorr_rgb_error) * 3.0f; // empirical + eci[i].alpha_drop_error = alpha_drop_error * 3.0f; + eci[i].can_offset_encode = can_offset_encode; + eci[i].can_blue_contract = !blk.is_luminance(); + } +} + +/** + * @brief For a given partition compute the error for every endpoint integer count and quant level. + * + * @param encode_hdr_rgb @c true if using HDR for RGB, @c false for LDR. + * @param encode_hdr_alpha @c true if using HDR for alpha, @c false for LDR. + * @param partition_index The partition index. + * @param pi The partition info. + * @param eci The encoding choice error metrics. + * @param ep The idealized endpoints. + * @param error_weight The resulting encoding choice error metrics. + * @param[out] best_error The best error for each integer count and quant level. + * @param[out] format_of_choice The preferred endpoint format for each integer count and quant level. + */ +static void compute_color_error_for_every_integer_count_and_quant_level( + bool encode_hdr_rgb, + bool encode_hdr_alpha, + int partition_index, + const partition_info& pi, + const encoding_choice_errors& eci, + const endpoints& ep, + vfloat4 error_weight, + float best_error[21][4], + uint8_t format_of_choice[21][4] +) { + int partition_size = pi.partition_texel_count[partition_index]; + + static const float baseline_quant_error[21] { + (65536.0f * 65536.0f / 18.0f), // 2 values, 1 step + (65536.0f * 65536.0f / 18.0f) / (2 * 2), // 3 values, 2 steps + (65536.0f * 65536.0f / 18.0f) / (3 * 3), // 4 values, 3 steps + (65536.0f * 65536.0f / 18.0f) / (4 * 4), // 5 values + (65536.0f * 65536.0f / 18.0f) / (5 * 5), + (65536.0f * 65536.0f / 18.0f) / (7 * 7), + (65536.0f * 65536.0f / 18.0f) / (9 * 9), + (65536.0f * 65536.0f / 18.0f) / (11 * 11), + (65536.0f * 65536.0f / 18.0f) / (15 * 15), + (65536.0f * 65536.0f / 18.0f) / (19 * 19), + (65536.0f * 65536.0f / 18.0f) / (23 * 23), + (65536.0f * 65536.0f / 18.0f) / (31 * 31), + (65536.0f * 65536.0f / 18.0f) / (39 * 39), + (65536.0f * 65536.0f / 18.0f) / (47 * 47), + (65536.0f * 65536.0f / 18.0f) / (63 * 63), + (65536.0f * 65536.0f / 18.0f) / (79 * 79), + (65536.0f * 65536.0f / 18.0f) / (95 * 95), + (65536.0f * 65536.0f / 18.0f) / (127 * 127), + (65536.0f * 65536.0f / 18.0f) / (159 * 159), + (65536.0f * 65536.0f / 18.0f) / (191 * 191), + (65536.0f * 65536.0f / 18.0f) / (255 * 255) + }; + + vfloat4 ep0 = ep.endpt0[partition_index]; + vfloat4 ep1 = ep.endpt1[partition_index]; + + float ep1_min = hmin_rgb_s(ep1); + ep1_min = astc::max(ep1_min, 0.0f); + + float error_weight_rgbsum = hadd_rgb_s(error_weight); + + float range_upper_limit_rgb = encode_hdr_rgb ? 61440.0f : 65535.0f; + float range_upper_limit_alpha = encode_hdr_alpha ? 61440.0f : 65535.0f; + + // It is possible to get endpoint colors significantly outside [0,upper-limit] even if the + // input data are safely contained in [0,upper-limit]; we need to add an error term for this + vfloat4 offset(range_upper_limit_rgb, range_upper_limit_rgb, range_upper_limit_rgb, range_upper_limit_alpha); + vfloat4 ep0_range_error_high = max(ep0 - offset, 0.0f); + vfloat4 ep1_range_error_high = max(ep1 - offset, 0.0f); + + vfloat4 ep0_range_error_low = min(ep0, 0.0f); + vfloat4 ep1_range_error_low = min(ep1, 0.0f); + + vfloat4 sum_range_error = + (ep0_range_error_low * ep0_range_error_low) + + (ep1_range_error_low * ep1_range_error_low) + + (ep0_range_error_high * ep0_range_error_high) + + (ep1_range_error_high * ep1_range_error_high); + + float rgb_range_error = dot3_s(sum_range_error, error_weight) + * 0.5f * static_cast<float>(partition_size); + float alpha_range_error = sum_range_error.lane<3>() * error_weight.lane<3>() + * 0.5f * static_cast<float>(partition_size); + + if (encode_hdr_rgb) + { + + // Collect some statistics + float af, cf; + if (ep1.lane<0>() > ep1.lane<1>() && ep1.lane<0>() > ep1.lane<2>()) + { + af = ep1.lane<0>(); + cf = ep1.lane<0>() - ep0.lane<0>(); + } + else if (ep1.lane<1>() > ep1.lane<2>()) + { + af = ep1.lane<1>(); + cf = ep1.lane<1>() - ep0.lane<1>(); + } + else + { + af = ep1.lane<2>(); + cf = ep1.lane<2>() - ep0.lane<2>(); + } + + // Estimate of color-component spread in high endpoint color + float bf = af - ep1_min; + vfloat4 prd = (ep1 - vfloat4(cf)).swz<0, 1, 2>(); + vfloat4 pdif = prd - ep0.swz<0, 1, 2>(); + // Estimate of color-component spread in low endpoint color + float df = hmax_s(abs(pdif)); + + int b = static_cast<int>(bf); + int c = static_cast<int>(cf); + int d = static_cast<int>(df); + + // Determine which one of the 6 submodes is likely to be used in case of an RGBO-mode + int rgbo_mode = 5; // 7 bits per component + // mode 4: 8 7 6 + if (b < 32768 && c < 16384) + { + rgbo_mode = 4; + } + + // mode 3: 9 6 7 + if (b < 8192 && c < 16384) + { + rgbo_mode = 3; + } + + // mode 2: 10 5 8 + if (b < 2048 && c < 16384) + { + rgbo_mode = 2; + } + + // mode 1: 11 6 5 + if (b < 2048 && c < 1024) + { + rgbo_mode = 1; + } + + // mode 0: 11 5 7 + if (b < 1024 && c < 4096) + { + rgbo_mode = 0; + } + + // Determine which one of the 9 submodes is likely to be used in case of an RGB-mode. + int rgb_mode = 8; // 8 bits per component, except 7 bits for blue + + // mode 0: 9 7 6 7 + if (b < 16384 && c < 8192 && d < 8192) + { + rgb_mode = 0; + } + + // mode 1: 9 8 6 6 + if (b < 32768 && c < 8192 && d < 4096) + { + rgb_mode = 1; + } + + // mode 2: 10 6 7 7 + if (b < 4096 && c < 8192 && d < 4096) + { + rgb_mode = 2; + } + + // mode 3: 10 7 7 6 + if (b < 8192 && c < 8192 && d < 2048) + { + rgb_mode = 3; + } + + // mode 4: 11 8 6 5 + if (b < 8192 && c < 2048 && d < 512) + { + rgb_mode = 4; + } + + // mode 5: 11 6 8 6 + if (b < 2048 && c < 8192 && d < 1024) + { + rgb_mode = 5; + } + + // mode 6: 12 7 7 5 + if (b < 2048 && c < 2048 && d < 256) + { + rgb_mode = 6; + } + + // mode 7: 12 6 7 6 + if (b < 1024 && c < 2048 && d < 512) + { + rgb_mode = 7; + } + + static const float rgbo_error_scales[6] { 4.0f, 4.0f, 16.0f, 64.0f, 256.0f, 1024.0f }; + static const float rgb_error_scales[9] { 64.0f, 64.0f, 16.0f, 16.0f, 4.0f, 4.0f, 1.0f, 1.0f, 384.0f }; + + float mode7mult = rgbo_error_scales[rgbo_mode] * 0.0015f; // Empirically determined .... + float mode11mult = rgb_error_scales[rgb_mode] * 0.010f; // Empirically determined .... + + + float lum_high = hadd_rgb_s(ep1) * (1.0f / 3.0f); + float lum_low = hadd_rgb_s(ep0) * (1.0f / 3.0f); + float lumdif = lum_high - lum_low; + float mode23mult = lumdif < 960 ? 4.0f : lumdif < 3968 ? 16.0f : 128.0f; + + mode23mult *= 0.0005f; // Empirically determined .... + + // Pick among the available HDR endpoint modes + for (int i = QUANT_2; i < QUANT_16; i++) + { + best_error[i][3] = ERROR_CALC_DEFAULT; + best_error[i][2] = ERROR_CALC_DEFAULT; + best_error[i][1] = ERROR_CALC_DEFAULT; + best_error[i][0] = ERROR_CALC_DEFAULT; + + format_of_choice[i][3] = static_cast<uint8_t>(encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA); + format_of_choice[i][2] = FMT_HDR_RGB; + format_of_choice[i][1] = FMT_HDR_RGB_SCALE; + format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE; + } + + for (int i = QUANT_16; i <= QUANT_256; i++) + { + // The base_quant_error should depend on the scale-factor that would be used during + // actual encode of the color value + + float base_quant_error = baseline_quant_error[i] * static_cast<float>(partition_size); + float rgb_quantization_error = error_weight_rgbsum * base_quant_error * 2.0f; + float alpha_quantization_error = error_weight.lane<3>() * base_quant_error * 2.0f; + float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error; + + // For 8 integers, we have two encodings: one with HDR A and another one with LDR A + + float full_hdr_rgba_error = rgba_quantization_error + rgb_range_error + alpha_range_error; + best_error[i][3] = full_hdr_rgba_error; + format_of_choice[i][3] = static_cast<uint8_t>(encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA); + + // For 6 integers, we have one HDR-RGB encoding + float full_hdr_rgb_error = (rgb_quantization_error * mode11mult) + rgb_range_error + eci.alpha_drop_error; + best_error[i][2] = full_hdr_rgb_error; + format_of_choice[i][2] = FMT_HDR_RGB; + + // For 4 integers, we have one HDR-RGB-Scale encoding + float hdr_rgb_scale_error = (rgb_quantization_error * mode7mult) + rgb_range_error + eci.alpha_drop_error + eci.rgb_luma_error; + + best_error[i][1] = hdr_rgb_scale_error; + format_of_choice[i][1] = FMT_HDR_RGB_SCALE; + + // For 2 integers, we assume luminance-with-large-range + float hdr_luminance_error = (rgb_quantization_error * mode23mult) + rgb_range_error + eci.alpha_drop_error + eci.luminance_error; + best_error[i][0] = hdr_luminance_error; + format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE; + } + } + else + { + for (int i = QUANT_2; i < QUANT_6; i++) + { + best_error[i][3] = ERROR_CALC_DEFAULT; + best_error[i][2] = ERROR_CALC_DEFAULT; + best_error[i][1] = ERROR_CALC_DEFAULT; + best_error[i][0] = ERROR_CALC_DEFAULT; + + format_of_choice[i][3] = FMT_RGBA; + format_of_choice[i][2] = FMT_RGB; + format_of_choice[i][1] = FMT_RGB_SCALE; + format_of_choice[i][0] = FMT_LUMINANCE; + } + + float base_quant_error_rgb = error_weight_rgbsum * static_cast<float>(partition_size); + float base_quant_error_a = error_weight.lane<3>() * static_cast<float>(partition_size); + float base_quant_error_rgba = base_quant_error_rgb + base_quant_error_a; + + float error_scale_bc_rgba = eci.can_blue_contract ? 0.625f : 1.0f; + float error_scale_oe_rgba = eci.can_offset_encode ? 0.5f : 1.0f; + + float error_scale_bc_rgb = eci.can_blue_contract ? 0.5f : 1.0f; + float error_scale_oe_rgb = eci.can_offset_encode ? 0.25f : 1.0f; + + // Pick among the available LDR endpoint modes + for (int i = QUANT_6; i <= QUANT_256; i++) + { + // Offset encoding not possible at higher quant levels + if (i >= QUANT_192) + { + error_scale_oe_rgba = 1.0f; + error_scale_oe_rgb = 1.0f; + } + + float base_quant_error = baseline_quant_error[i]; + float quant_error_rgb = base_quant_error_rgb * base_quant_error; + float quant_error_rgba = base_quant_error_rgba * base_quant_error; + + // 8 integers can encode as RGBA+RGBA + float full_ldr_rgba_error = quant_error_rgba + * error_scale_bc_rgba + * error_scale_oe_rgba + + rgb_range_error + + alpha_range_error; + + best_error[i][3] = full_ldr_rgba_error; + format_of_choice[i][3] = FMT_RGBA; + + // 6 integers can encode as RGB+RGB or RGBS+AA + float full_ldr_rgb_error = quant_error_rgb + * error_scale_bc_rgb + * error_scale_oe_rgb + + rgb_range_error + + eci.alpha_drop_error; + + float rgbs_alpha_error = quant_error_rgba + + eci.rgb_scale_error + + rgb_range_error + + alpha_range_error; + + if (rgbs_alpha_error < full_ldr_rgb_error) + { + best_error[i][2] = rgbs_alpha_error; + format_of_choice[i][2] = FMT_RGB_SCALE_ALPHA; + } + else + { + best_error[i][2] = full_ldr_rgb_error; + format_of_choice[i][2] = FMT_RGB; + } + + // 4 integers can encode as RGBS or LA+LA + float ldr_rgbs_error = quant_error_rgb + + rgb_range_error + + eci.alpha_drop_error + + eci.rgb_scale_error; + + float lum_alpha_error = quant_error_rgba + + rgb_range_error + + alpha_range_error + + eci.luminance_error; + + if (ldr_rgbs_error < lum_alpha_error) + { + best_error[i][1] = ldr_rgbs_error; + format_of_choice[i][1] = FMT_RGB_SCALE; + } + else + { + best_error[i][1] = lum_alpha_error; + format_of_choice[i][1] = FMT_LUMINANCE_ALPHA; + } + + // 2 integers can encode as L+L + float luminance_error = quant_error_rgb + + rgb_range_error + + eci.alpha_drop_error + + eci.luminance_error; + + best_error[i][0] = luminance_error; + format_of_choice[i][0] = FMT_LUMINANCE; + } + } +} + +/** + * @brief For one partition compute the best format and quantization for a given bit count. + * + * @param best_combined_error The best error for each quant level and integer count. + * @param best_combined_format The best format for each quant level and integer count. + * @param bits_available The number of bits available for encoding. + * @param[out] best_quant_level The output best color quant level. + * @param[out] best_format The output best color format. + * + * @return The output error for the best pairing. + */ +static float one_partition_find_best_combination_for_bitcount( + const float best_combined_error[21][4], + const uint8_t best_combined_format[21][4], + int bits_available, + uint8_t& best_quant_level, + uint8_t& best_format +) { + int best_integer_count = 0; + float best_integer_count_error = ERROR_CALC_DEFAULT; + + for (int integer_count = 1; integer_count <= 4; integer_count++) + { + // Compute the quantization level for a given number of integers and a given number of bits + int quant_level = quant_mode_table[integer_count][bits_available]; + + // Don't have enough bits to represent a given endpoint format at all! + if (quant_level < QUANT_6) + { + continue; + } + + float integer_count_error = best_combined_error[quant_level][integer_count - 1]; + if (integer_count_error < best_integer_count_error) + { + best_integer_count_error = integer_count_error; + best_integer_count = integer_count - 1; + } + } + + int ql = quant_mode_table[best_integer_count + 1][bits_available]; + + best_quant_level = static_cast<uint8_t>(ql); + best_format = FMT_LUMINANCE; + + if (ql >= QUANT_6) + { + best_format = best_combined_format[ql][best_integer_count]; + } + + return best_integer_count_error; +} + +/** + * @brief For 2 partitions compute the best format combinations for every pair of quant mode and integer count. + * + * @param best_error The best error for a single endpoint quant level and integer count. + * @param best_format The best format for a single endpoint quant level and integer count. + * @param[out] best_combined_error The best combined error pairings for the 2 partitions. + * @param[out] best_combined_format The best combined format pairings for the 2 partitions. + */ +static void two_partitions_find_best_combination_for_every_quantization_and_integer_count( + const float best_error[2][21][4], // indexed by (partition, quant-level, integer-pair-count-minus-1) + const uint8_t best_format[2][21][4], + float best_combined_error[21][7], // indexed by (quant-level, integer-pair-count-minus-2) + uint8_t best_combined_format[21][7][2] +) { + for (int i = QUANT_2; i <= QUANT_256; i++) + { + for (int j = 0; j < 7; j++) + { + best_combined_error[i][j] = ERROR_CALC_DEFAULT; + } + } + + for (int quant = QUANT_6; quant <= QUANT_256; quant++) + { + for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair + { + for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair + { + int low2 = astc::min(i, j); + int high2 = astc::max(i, j); + if ((high2 - low2) > 1) + { + continue; + } + + int intcnt = i + j; + float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j], 1e10f); + if (errorterm <= best_combined_error[quant][intcnt]) + { + best_combined_error[quant][intcnt] = errorterm; + best_combined_format[quant][intcnt][0] = best_format[0][quant][i]; + best_combined_format[quant][intcnt][1] = best_format[1][quant][j]; + } + } + } + } +} + +/** + * @brief For 2 partitions compute the best format and quantization for a given bit count. + * + * @param best_combined_error The best error for each quant level and integer count. + * @param best_combined_format The best format for each quant level and integer count. + * @param bits_available The number of bits available for encoding. + * @param[out] best_quant_level The output best color quant level. + * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available. + * @param[out] best_formats The output best color formats. + * + * @return The output error for the best pairing. + */ +static float two_partitions_find_best_combination_for_bitcount( + float best_combined_error[21][7], + uint8_t best_combined_format[21][7][2], + int bits_available, + uint8_t& best_quant_level, + uint8_t& best_quant_level_mod, + uint8_t* best_formats +) { + int best_integer_count = 0; + float best_integer_count_error = ERROR_CALC_DEFAULT; + + for (int integer_count = 2; integer_count <= 8; integer_count++) + { + // Compute the quantization level for a given number of integers and a given number of bits + int quant_level = quant_mode_table[integer_count][bits_available]; + + // Don't have enough bits to represent a given endpoint format at all! + if (quant_level < QUANT_6) + { + break; + } + + float integer_count_error = best_combined_error[quant_level][integer_count - 2]; + if (integer_count_error < best_integer_count_error) + { + best_integer_count_error = integer_count_error; + best_integer_count = integer_count; + } + } + + int ql = quant_mode_table[best_integer_count][bits_available]; + int ql_mod = quant_mode_table[best_integer_count][bits_available + 2]; + + best_quant_level = static_cast<uint8_t>(ql); + best_quant_level_mod = static_cast<uint8_t>(ql_mod); + + if (ql >= QUANT_6) + { + for (int i = 0; i < 2; i++) + { + best_formats[i] = best_combined_format[ql][best_integer_count - 2][i]; + } + } + else + { + for (int i = 0; i < 2; i++) + { + best_formats[i] = FMT_LUMINANCE; + } + } + + return best_integer_count_error; +} + +/** + * @brief For 3 partitions compute the best format combinations for every pair of quant mode and integer count. + * + * @param best_error The best error for a single endpoint quant level and integer count. + * @param best_format The best format for a single endpoint quant level and integer count. + * @param[out] best_combined_error The best combined error pairings for the 3 partitions. + * @param[out] best_combined_format The best combined format pairings for the 3 partitions. + */ +static void three_partitions_find_best_combination_for_every_quantization_and_integer_count( + const float best_error[3][21][4], // indexed by (partition, quant-level, integer-count) + const uint8_t best_format[3][21][4], + float best_combined_error[21][10], + uint8_t best_combined_format[21][10][3] +) { + for (int i = QUANT_2; i <= QUANT_256; i++) + { + for (int j = 0; j < 10; j++) + { + best_combined_error[i][j] = ERROR_CALC_DEFAULT; + } + } + + for (int quant = QUANT_6; quant <= QUANT_256; quant++) + { + for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair + { + for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair + { + int low2 = astc::min(i, j); + int high2 = astc::max(i, j); + if ((high2 - low2) > 1) + { + continue; + } + + for (int k = 0; k < 4; k++) // integer-count for third endpoint-pair + { + int low3 = astc::min(k, low2); + int high3 = astc::max(k, high2); + if ((high3 - low3) > 1) + { + continue; + } + + int intcnt = i + j + k; + float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k], 1e10f); + if (errorterm <= best_combined_error[quant][intcnt]) + { + best_combined_error[quant][intcnt] = errorterm; + best_combined_format[quant][intcnt][0] = best_format[0][quant][i]; + best_combined_format[quant][intcnt][1] = best_format[1][quant][j]; + best_combined_format[quant][intcnt][2] = best_format[2][quant][k]; + } + } + } + } + } +} + +/** + * @brief For 3 partitions compute the best format and quantization for a given bit count. + * + * @param best_combined_error The best error for each quant level and integer count. + * @param best_combined_format The best format for each quant level and integer count. + * @param bits_available The number of bits available for encoding. + * @param[out] best_quant_level The output best color quant level. + * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available. + * @param[out] best_formats The output best color formats. + * + * @return The output error for the best pairing. + */ +static float three_partitions_find_best_combination_for_bitcount( + const float best_combined_error[21][10], + const uint8_t best_combined_format[21][10][3], + int bits_available, + uint8_t& best_quant_level, + uint8_t& best_quant_level_mod, + uint8_t* best_formats +) { + int best_integer_count = 0; + float best_integer_count_error = ERROR_CALC_DEFAULT; + + for (int integer_count = 3; integer_count <= 9; integer_count++) + { + // Compute the quantization level for a given number of integers and a given number of bits + int quant_level = quant_mode_table[integer_count][bits_available]; + + // Don't have enough bits to represent a given endpoint format at all! + if (quant_level < QUANT_6) + { + break; + } + + float integer_count_error = best_combined_error[quant_level][integer_count - 3]; + if (integer_count_error < best_integer_count_error) + { + best_integer_count_error = integer_count_error; + best_integer_count = integer_count; + } + } + + int ql = quant_mode_table[best_integer_count][bits_available]; + int ql_mod = quant_mode_table[best_integer_count][bits_available + 5]; + + best_quant_level = static_cast<uint8_t>(ql); + best_quant_level_mod = static_cast<uint8_t>(ql_mod); + + if (ql >= QUANT_6) + { + for (int i = 0; i < 3; i++) + { + best_formats[i] = best_combined_format[ql][best_integer_count - 3][i]; + } + } + else + { + for (int i = 0; i < 3; i++) + { + best_formats[i] = FMT_LUMINANCE; + } + } + + return best_integer_count_error; +} + +/** + * @brief For 4 partitions compute the best format combinations for every pair of quant mode and integer count. + * + * @param best_error The best error for a single endpoint quant level and integer count. + * @param best_format The best format for a single endpoint quant level and integer count. + * @param[out] best_combined_error The best combined error pairings for the 4 partitions. + * @param[out] best_combined_format The best combined format pairings for the 4 partitions. + */ +static void four_partitions_find_best_combination_for_every_quantization_and_integer_count( + const float best_error[4][21][4], // indexed by (partition, quant-level, integer-count) + const uint8_t best_format[4][21][4], + float best_combined_error[21][13], + uint8_t best_combined_format[21][13][4] +) { + for (int i = QUANT_2; i <= QUANT_256; i++) + { + for (int j = 0; j < 13; j++) + { + best_combined_error[i][j] = ERROR_CALC_DEFAULT; + } + } + + for (int quant = QUANT_6; quant <= QUANT_256; quant++) + { + for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair + { + for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair + { + int low2 = astc::min(i, j); + int high2 = astc::max(i, j); + if ((high2 - low2) > 1) + { + continue; + } + + for (int k = 0; k < 4; k++) // integer-count for third endpoint-pair + { + int low3 = astc::min(k, low2); + int high3 = astc::max(k, high2); + if ((high3 - low3) > 1) + { + continue; + } + + for (int l = 0; l < 4; l++) // integer-count for fourth endpoint-pair + { + int low4 = astc::min(l, low3); + int high4 = astc::max(l, high3); + if ((high4 - low4) > 1) + { + continue; + } + + int intcnt = i + j + k + l; + float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k] + best_error[3][quant][l], 1e10f); + if (errorterm <= best_combined_error[quant][intcnt]) + { + best_combined_error[quant][intcnt] = errorterm; + best_combined_format[quant][intcnt][0] = best_format[0][quant][i]; + best_combined_format[quant][intcnt][1] = best_format[1][quant][j]; + best_combined_format[quant][intcnt][2] = best_format[2][quant][k]; + best_combined_format[quant][intcnt][3] = best_format[3][quant][l]; + } + } + } + } + } + } +} + +/** + * @brief For 4 partitions compute the best format and quantization for a given bit count. + * + * @param best_combined_error The best error for each quant level and integer count. + * @param best_combined_format The best format for each quant level and integer count. + * @param bits_available The number of bits available for encoding. + * @param[out] best_quant_level The output best color quant level. + * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available. + * @param[out] best_formats The output best color formats. + * + * @return best_error The output error for the best pairing. + */ +static float four_partitions_find_best_combination_for_bitcount( + const float best_combined_error[21][13], + const uint8_t best_combined_format[21][13][4], + int bits_available, + uint8_t& best_quant_level, + uint8_t& best_quant_level_mod, + uint8_t* best_formats +) { + int best_integer_count = 0; + float best_integer_count_error = ERROR_CALC_DEFAULT; + + for (int integer_count = 4; integer_count <= 9; integer_count++) + { + // Compute the quantization level for a given number of integers and a given number of bits + int quant_level = quant_mode_table[integer_count][bits_available]; + + // Don't have enough bits to represent a given endpoint format at all! + if (quant_level < QUANT_6) + { + break; + } + + float integer_count_error = best_combined_error[quant_level][integer_count - 4]; + if (integer_count_error < best_integer_count_error) + { + best_integer_count_error = integer_count_error; + best_integer_count = integer_count; + } + } + + int ql = quant_mode_table[best_integer_count][bits_available]; + int ql_mod = quant_mode_table[best_integer_count][bits_available + 8]; + + best_quant_level = static_cast<uint8_t>(ql); + best_quant_level_mod = static_cast<uint8_t>(ql_mod); + + if (ql >= QUANT_6) + { + for (int i = 0; i < 4; i++) + { + best_formats[i] = best_combined_format[ql][best_integer_count - 4][i]; + } + } + else + { + for (int i = 0; i < 4; i++) + { + best_formats[i] = FMT_LUMINANCE; + } + } + + return best_integer_count_error; +} + +/* See header for documentation. */ +unsigned int compute_ideal_endpoint_formats( + const partition_info& pi, + const image_block& blk, + const endpoints& ep, + // bitcounts and errors computed for the various quantization methods + const int8_t* qwt_bitcounts, + const float* qwt_errors, + unsigned int tune_candidate_limit, + unsigned int start_block_mode, + unsigned int end_block_mode, + // output data + uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS], + int block_mode[TUNE_MAX_TRIAL_CANDIDATES], + quant_method quant_level[TUNE_MAX_TRIAL_CANDIDATES], + quant_method quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES], + compression_working_buffers& tmpbuf +) { + int partition_count = pi.partition_count; + + promise(partition_count > 0); + + bool encode_hdr_rgb = static_cast<bool>(blk.rgb_lns[0]); + bool encode_hdr_alpha = static_cast<bool>(blk.alpha_lns[0]); + + // Compute the errors that result from various encoding choices (such as using luminance instead + // of RGB, discarding Alpha, using RGB-scale in place of two separate RGB endpoints and so on) + encoding_choice_errors eci[BLOCK_MAX_PARTITIONS]; + compute_encoding_choice_errors(blk, pi, ep, eci); + + float best_error[BLOCK_MAX_PARTITIONS][21][4]; + uint8_t format_of_choice[BLOCK_MAX_PARTITIONS][21][4]; + for (int i = 0; i < partition_count; i++) + { + compute_color_error_for_every_integer_count_and_quant_level( + encode_hdr_rgb, encode_hdr_alpha, i, + pi, eci[i], ep, blk.channel_weight, best_error[i], + format_of_choice[i]); + } + + float* errors_of_best_combination = tmpbuf.errors_of_best_combination; + uint8_t* best_quant_levels = tmpbuf.best_quant_levels; + uint8_t* best_quant_levels_mod = tmpbuf.best_quant_levels_mod; + uint8_t (&best_ep_formats)[WEIGHTS_MAX_BLOCK_MODES][BLOCK_MAX_PARTITIONS] = tmpbuf.best_ep_formats; + + // Ensure that the first iteration understep contains data that will never be picked + unsigned int packed_start_block_mode = round_down_to_simd_multiple_vla(start_block_mode); + for (unsigned int i = packed_start_block_mode; i < start_block_mode; i++) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + best_quant_levels[i] = QUANT_2; + best_quant_levels_mod[i] = QUANT_2; + } + + // Ensure that last iteration overstep contains data that will never be picked + const unsigned int packed_end_block_mode = round_up_to_simd_multiple_vla(end_block_mode); + for (unsigned int i = end_block_mode; i < packed_end_block_mode; i++) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + best_quant_levels[i] = QUANT_2; + best_quant_levels_mod[i] = QUANT_2; + } + + // Track a scalar best to avoid expensive search at least once ... + float error_of_best_combination = ERROR_CALC_DEFAULT; + int index_of_best_combination = -1; + + // The block contains 1 partition + if (partition_count == 1) + { + for (unsigned int i = start_block_mode; i < end_block_mode; i++) + { + if (qwt_errors[i] >= ERROR_CALC_DEFAULT) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + continue; + } + + float error_of_best = one_partition_find_best_combination_for_bitcount( + best_error[0], format_of_choice[0], qwt_bitcounts[i], + best_quant_levels[i], best_ep_formats[i][0]); + + float total_error = error_of_best + qwt_errors[i]; + errors_of_best_combination[i] = total_error; + best_quant_levels_mod[i] = best_quant_levels[i]; + + if (total_error < error_of_best_combination) + { + error_of_best_combination = total_error; + index_of_best_combination = i; + } + } + } + // The block contains 2 partitions + else if (partition_count == 2) + { + float combined_best_error[21][7]; + uint8_t formats_of_choice[21][7][2]; + + two_partitions_find_best_combination_for_every_quantization_and_integer_count( + best_error, format_of_choice, combined_best_error, formats_of_choice); + + assert(start_block_mode == 0); + for (unsigned int i = 0; i < end_block_mode; i++) + { + if (qwt_errors[i] >= ERROR_CALC_DEFAULT) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + continue; + } + + float error_of_best = two_partitions_find_best_combination_for_bitcount( + combined_best_error, formats_of_choice, qwt_bitcounts[i], + best_quant_levels[i], best_quant_levels_mod[i], + best_ep_formats[i]); + + float total_error = error_of_best + qwt_errors[i]; + errors_of_best_combination[i] = total_error; + + if (total_error < error_of_best_combination) + { + error_of_best_combination = total_error; + index_of_best_combination = i; + } + } + } + // The block contains 3 partitions + else if (partition_count == 3) + { + float combined_best_error[21][10]; + uint8_t formats_of_choice[21][10][3]; + + three_partitions_find_best_combination_for_every_quantization_and_integer_count( + best_error, format_of_choice, combined_best_error, formats_of_choice); + + assert(start_block_mode == 0); + for (unsigned int i = 0; i < end_block_mode; i++) + { + if (qwt_errors[i] >= ERROR_CALC_DEFAULT) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + continue; + } + + float error_of_best = three_partitions_find_best_combination_for_bitcount( + combined_best_error, formats_of_choice, qwt_bitcounts[i], + best_quant_levels[i], best_quant_levels_mod[i], + best_ep_formats[i]); + + float total_error = error_of_best + qwt_errors[i]; + errors_of_best_combination[i] = total_error; + + if (total_error < error_of_best_combination) + { + error_of_best_combination = total_error; + index_of_best_combination = i; + } + } + } + // The block contains 4 partitions + else // if (partition_count == 4) + { + assert(partition_count == 4); + float combined_best_error[21][13]; + uint8_t formats_of_choice[21][13][4]; + + four_partitions_find_best_combination_for_every_quantization_and_integer_count( + best_error, format_of_choice, combined_best_error, formats_of_choice); + + assert(start_block_mode == 0); + for (unsigned int i = 0; i < end_block_mode; i++) + { + if (qwt_errors[i] >= ERROR_CALC_DEFAULT) + { + errors_of_best_combination[i] = ERROR_CALC_DEFAULT; + continue; + } + + float error_of_best = four_partitions_find_best_combination_for_bitcount( + combined_best_error, formats_of_choice, qwt_bitcounts[i], + best_quant_levels[i], best_quant_levels_mod[i], + best_ep_formats[i]); + + float total_error = error_of_best + qwt_errors[i]; + errors_of_best_combination[i] = total_error; + + if (total_error < error_of_best_combination) + { + error_of_best_combination = total_error; + index_of_best_combination = i; + } + } + } + + int best_error_weights[TUNE_MAX_TRIAL_CANDIDATES]; + + // Fast path the first result and avoid the list search for trial 0 + best_error_weights[0] = index_of_best_combination; + if (index_of_best_combination >= 0) + { + errors_of_best_combination[index_of_best_combination] = ERROR_CALC_DEFAULT; + } + + // Search the remaining results and pick the best candidate modes for trial 1+ + for (unsigned int i = 1; i < tune_candidate_limit; i++) + { + vint vbest_error_index(-1); + vfloat vbest_ep_error(ERROR_CALC_DEFAULT); + + start_block_mode = round_down_to_simd_multiple_vla(start_block_mode); + vint lane_ids = vint::lane_id() + vint(start_block_mode); + for (unsigned int j = start_block_mode; j < end_block_mode; j += ASTCENC_SIMD_WIDTH) + { + vfloat err = vfloat(errors_of_best_combination + j); + vmask mask = err < vbest_ep_error; + vbest_ep_error = select(vbest_ep_error, err, mask); + vbest_error_index = select(vbest_error_index, lane_ids, mask); + lane_ids += vint(ASTCENC_SIMD_WIDTH); + } + + // Pick best mode from the SIMD result, using lowest matching index to ensure invariance + vmask lanes_min_error = vbest_ep_error == hmin(vbest_ep_error); + vbest_error_index = select(vint(0x7FFFFFFF), vbest_error_index, lanes_min_error); + vbest_error_index = hmin(vbest_error_index); + int best_error_index = vbest_error_index.lane<0>(); + + best_error_weights[i] = best_error_index; + + // Max the error for this candidate so we don't pick it again + if (best_error_index >= 0) + { + errors_of_best_combination[best_error_index] = ERROR_CALC_DEFAULT; + } + // Early-out if no more candidates are valid + else + { + break; + } + } + + for (unsigned int i = 0; i < tune_candidate_limit; i++) + { + if (best_error_weights[i] < 0) + { + return i; + } + + block_mode[i] = best_error_weights[i]; + + quant_level[i] = static_cast<quant_method>(best_quant_levels[best_error_weights[i]]); + quant_level_mod[i] = static_cast<quant_method>(best_quant_levels_mod[best_error_weights[i]]); + + assert(quant_level[i] >= QUANT_6 && quant_level[i] <= QUANT_256); + assert(quant_level_mod[i] >= QUANT_6 && quant_level_mod[i] <= QUANT_256); + + for (int j = 0; j < partition_count; j++) + { + partition_format_specifiers[i][j] = best_ep_formats[best_error_weights[i]][j]; + } + } + + return tune_candidate_limit; +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_platform_isa_detection.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_platform_isa_detection.cpp new file mode 100644 index 00000000000..4158da31bef --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_platform_isa_detection.cpp @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2020-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Platform-specific function implementations. + * + * This module contains functions for querying the host extended ISA support. + */ + +// Include before the defines below to pick up any auto-setup based on compiler +// built-in config, if not being set explicitly by the build system +#include "astcenc_internal.h" + +#if (ASTCENC_SSE > 0) || (ASTCENC_AVX > 0) || \ + (ASTCENC_POPCNT > 0) || (ASTCENC_F16C > 0) + +static bool g_init { false }; + +/** Does this CPU support SSE 4.1? Set to -1 if not yet initialized. */ +static bool g_cpu_has_sse41 { false }; + +/** Does this CPU support AVX2? Set to -1 if not yet initialized. */ +static bool g_cpu_has_avx2 { false }; + +/** Does this CPU support POPCNT? Set to -1 if not yet initialized. */ +static bool g_cpu_has_popcnt { false }; + +/** Does this CPU support F16C? Set to -1 if not yet initialized. */ +static bool g_cpu_has_f16c { false }; + +/* ============================================================================ + Platform code for Visual Studio +============================================================================ */ +#if !defined(__clang__) && defined(_MSC_VER) +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> +#include <intrin.h> + +/** + * @brief Detect platform CPU ISA support and update global trackers. + */ +static void detect_cpu_isa() +{ + int data[4]; + + __cpuid(data, 0); + int num_id = data[0]; + + if (num_id >= 1) + { + __cpuidex(data, 1, 0); + // SSE41 = Bank 1, ECX, bit 19 + g_cpu_has_sse41 = data[2] & (1 << 19) ? true : false; + // POPCNT = Bank 1, ECX, bit 23 + g_cpu_has_popcnt = data[2] & (1 << 23) ? true : false; + // F16C = Bank 1, ECX, bit 29 + g_cpu_has_f16c = data[2] & (1 << 29) ? true : false; + } + + if (num_id >= 7) + { + __cpuidex(data, 7, 0); + // AVX2 = Bank 7, EBX, bit 5 + g_cpu_has_avx2 = data[1] & (1 << 5) ? true : false; + } + + // Ensure state bits are updated before init flag is updated + MemoryBarrier(); + g_init = true; +} + +/* ============================================================================ + Platform code for GCC and Clang +============================================================================ */ +#else +#include <cpuid.h> + +/** + * @brief Detect platform CPU ISA support and update global trackers. + */ +static void detect_cpu_isa() +{ + unsigned int data[4]; + + if (__get_cpuid_count(1, 0, &data[0], &data[1], &data[2], &data[3])) + { + // SSE41 = Bank 1, ECX, bit 19 + g_cpu_has_sse41 = data[2] & (1 << 19) ? true : false; + // POPCNT = Bank 1, ECX, bit 23 + g_cpu_has_popcnt = data[2] & (1 << 23) ? true : false; + // F16C = Bank 1, ECX, bit 29 + g_cpu_has_f16c = data[2] & (1 << 29) ? true : false; + } + + g_cpu_has_avx2 = 0; + if (__get_cpuid_count(7, 0, &data[0], &data[1], &data[2], &data[3])) + { + // AVX2 = Bank 7, EBX, bit 5 + g_cpu_has_avx2 = data[1] & (1 << 5) ? true : false; + } + + // Ensure state bits are updated before init flag is updated + __sync_synchronize(); + g_init = true; +} +#endif + +/* See header for documentation. */ +bool cpu_supports_popcnt() +{ + if (!g_init) + { + detect_cpu_isa(); + } + + return g_cpu_has_popcnt; +} + +/* See header for documentation. */ +bool cpu_supports_f16c() +{ + if (!g_init) + { + detect_cpu_isa(); + } + + return g_cpu_has_f16c; +} + +/* See header for documentation. */ +bool cpu_supports_sse41() +{ + if (!g_init) + { + detect_cpu_isa(); + } + + return g_cpu_has_sse41; +} + +/* See header for documentation. */ +bool cpu_supports_avx2() +{ + if (!g_init) + { + detect_cpu_isa(); + } + + return g_cpu_has_avx2; +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_quantization.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_quantization.cpp new file mode 100644 index 00000000000..478a21ead71 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_quantization.cpp @@ -0,0 +1,904 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions and data tables for numeric quantization.. + */ + +#include "astcenc_internal.h" + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +// Starts from QUANT_6 +// Not scrambled +const uint8_t color_unquant_to_uquant_tables[17][256] { + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 102, 102, 102, + 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, + 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, + 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, + 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, + 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, + 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, + 153, 153, 153, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, + 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, + 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, + 204, 204, 204, 204, 204, 204, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 109, 109, 109, 109, + 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, + 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, + 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, + 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, + 146, 146, 146, 146, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, + 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, + 182, 182, 182, 182, 182, 182, 182, 182, 182, 219, 219, 219, 219, 219, 219, 219, + 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, + 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 219, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 56, 56, 56, 56, 56, + 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, + 56, 56, 56, 56, 56, 56, 56, 84, 84, 84, 84, 84, 84, 84, 84, 84, + 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, + 84, 84, 84, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, + 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, + 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, + 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 171, 171, 171, + 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, + 171, 171, 171, 171, 171, 171, 171, 171, 171, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, + 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, 227, + 227, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 69, 69, 69, 69, 69, 69, + 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, + 69, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 92, 92, 92, 92, 92, 92, 116, 116, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, + 139, 139, 139, 139, 139, 139, 139, 163, 163, 163, 163, 163, 163, 163, 163, 163, + 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 186, + 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, + 186, 186, 186, 186, 186, 186, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, + 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 232, 232, 232, + 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, + 232, 232, 232, 232, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 34, 34, 34, 34, 34, 34, + 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 68, 68, 68, 68, + 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 85, 85, 85, + 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 102, 102, + 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 119, + 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, + 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, + 136, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, + 153, 153, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, + 170, 170, 170, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, + 187, 187, 187, 187, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, + 204, 204, 204, 204, 204, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, + 221, 221, 221, 221, 221, 221, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, + 238, 238, 238, 238, 238, 238, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 80, 80, 80, 80, 80, 80, + 80, 80, 80, 80, 80, 80, 80, 80, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, + 107, 107, 107, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, + 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 148, 148, 148, + 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 161, 161, 161, 161, 161, + 161, 161, 161, 161, 161, 161, 161, 161, 175, 175, 175, 175, 175, 175, 175, 175, + 175, 175, 175, 175, 175, 175, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, + 188, 188, 188, 201, 201, 201, 201, 201, 201, 201, 201, 201, 201, 201, 201, 201, + 215, 215, 215, 215, 215, 215, 215, 215, 215, 215, 215, 215, 215, 215, 228, 228, + 228, 228, 228, 228, 228, 228, 228, 228, 228, 228, 228, 242, 242, 242, 242, 242, + 242, 242, 242, 242, 242, 242, 242, 242, 242, 255, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 44, 44, 44, 44, 44, 44, 44, 44, 44, + 44, 44, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 110, 110, 110, 110, 110, 110, 110, + 110, 110, 110, 110, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, + 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 145, 145, 145, 145, + 145, 145, 145, 145, 145, 145, 145, 156, 156, 156, 156, 156, 156, 156, 156, 156, + 156, 156, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 178, 178, 178, + 178, 178, 178, 178, 178, 178, 178, 178, 189, 189, 189, 189, 189, 189, 189, 189, + 189, 189, 189, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 211, 211, + 211, 211, 211, 211, 211, 211, 211, 211, 211, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 233, 233, 233, 233, 233, 233, 233, 233, 233, 233, 233, 244, + 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 255, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16, 16, + 16, 16, 16, 16, 16, 24, 24, 24, 24, 24, 24, 24, 24, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 41, 41, 41, 41, 41, 41, 41, 41, 49, 49, + 49, 49, 49, 49, 49, 49, 57, 57, 57, 57, 57, 57, 57, 57, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 74, 74, 74, 74, 74, 74, 74, 74, 82, + 82, 82, 82, 82, 82, 82, 82, 90, 90, 90, 90, 90, 90, 90, 90, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 107, 107, 107, 107, 107, 107, 107, 107, + 115, 115, 115, 115, 115, 115, 115, 115, 123, 123, 123, 123, 123, 123, 123, 123, + 132, 132, 132, 132, 132, 132, 132, 132, 140, 140, 140, 140, 140, 140, 140, 140, + 148, 148, 148, 148, 148, 148, 148, 148, 156, 156, 156, 156, 156, 156, 156, 156, + 156, 165, 165, 165, 165, 165, 165, 165, 165, 173, 173, 173, 173, 173, 173, 173, + 173, 181, 181, 181, 181, 181, 181, 181, 181, 189, 189, 189, 189, 189, 189, 189, + 189, 189, 198, 198, 198, 198, 198, 198, 198, 198, 206, 206, 206, 206, 206, 206, + 206, 206, 214, 214, 214, 214, 214, 214, 214, 214, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 231, 231, 231, 231, 231, 231, 231, 231, 239, 239, 239, 239, 239, + 239, 239, 239, 247, 247, 247, 247, 247, 247, 247, 247, 255, 255, 255, 255, 255 + }, + { + 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 13, 13, 13, 13, 13, 13, + 13, 19, 19, 19, 19, 19, 19, 26, 26, 26, 26, 26, 26, 26, 32, 32, + 32, 32, 32, 32, 39, 39, 39, 39, 39, 39, 39, 45, 45, 45, 45, 45, + 45, 52, 52, 52, 52, 52, 52, 52, 58, 58, 58, 58, 58, 58, 65, 65, + 65, 65, 65, 65, 65, 71, 71, 71, 71, 71, 71, 78, 78, 78, 78, 78, + 78, 78, 84, 84, 84, 84, 84, 84, 91, 91, 91, 91, 91, 91, 91, 97, + 97, 97, 97, 97, 97, 104, 104, 104, 104, 104, 104, 104, 110, 110, 110, 110, + 110, 110, 117, 117, 117, 117, 117, 117, 117, 123, 123, 123, 123, 123, 123, 123, + 132, 132, 132, 132, 132, 132, 132, 138, 138, 138, 138, 138, 138, 138, 145, 145, + 145, 145, 145, 145, 151, 151, 151, 151, 151, 151, 151, 158, 158, 158, 158, 158, + 158, 164, 164, 164, 164, 164, 164, 164, 171, 171, 171, 171, 171, 171, 177, 177, + 177, 177, 177, 177, 177, 184, 184, 184, 184, 184, 184, 190, 190, 190, 190, 190, + 190, 190, 197, 197, 197, 197, 197, 197, 203, 203, 203, 203, 203, 203, 203, 210, + 210, 210, 210, 210, 210, 216, 216, 216, 216, 216, 216, 216, 223, 223, 223, 223, + 223, 223, 229, 229, 229, 229, 229, 229, 229, 236, 236, 236, 236, 236, 236, 242, + 242, 242, 242, 242, 242, 242, 249, 249, 249, 249, 249, 249, 255, 255, 255, 255 + }, + { + 0, 0, 0, 5, 5, 5, 5, 5, 5, 11, 11, 11, 11, 11, 16, 16, + 16, 16, 16, 21, 21, 21, 21, 21, 21, 27, 27, 27, 27, 27, 32, 32, + 32, 32, 32, 32, 38, 38, 38, 38, 38, 43, 43, 43, 43, 43, 48, 48, + 48, 48, 48, 48, 54, 54, 54, 54, 54, 59, 59, 59, 59, 59, 59, 65, + 65, 65, 65, 65, 70, 70, 70, 70, 70, 70, 76, 76, 76, 76, 76, 81, + 81, 81, 81, 81, 86, 86, 86, 86, 86, 86, 92, 92, 92, 92, 92, 97, + 97, 97, 97, 97, 97, 103, 103, 103, 103, 103, 108, 108, 108, 108, 108, 113, + 113, 113, 113, 113, 113, 119, 119, 119, 119, 119, 124, 124, 124, 124, 124, 124, + 131, 131, 131, 131, 131, 131, 136, 136, 136, 136, 136, 142, 142, 142, 142, 142, + 142, 147, 147, 147, 147, 147, 152, 152, 152, 152, 152, 158, 158, 158, 158, 158, + 158, 163, 163, 163, 163, 163, 169, 169, 169, 169, 169, 169, 174, 174, 174, 174, + 174, 179, 179, 179, 179, 179, 185, 185, 185, 185, 185, 185, 190, 190, 190, 190, + 190, 196, 196, 196, 196, 196, 196, 201, 201, 201, 201, 201, 207, 207, 207, 207, + 207, 207, 212, 212, 212, 212, 212, 217, 217, 217, 217, 217, 223, 223, 223, 223, + 223, 223, 228, 228, 228, 228, 228, 234, 234, 234, 234, 234, 234, 239, 239, 239, + 239, 239, 244, 244, 244, 244, 244, 250, 250, 250, 250, 250, 250, 255, 255, 255 + }, + { + 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12, 16, + 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 24, 28, 28, 28, 28, 32, + 32, 32, 32, 36, 36, 36, 36, 40, 40, 40, 40, 44, 44, 44, 44, 48, + 48, 48, 48, 52, 52, 52, 52, 56, 56, 56, 56, 60, 60, 60, 60, 65, + 65, 65, 65, 65, 69, 69, 69, 69, 73, 73, 73, 73, 77, 77, 77, 77, + 81, 81, 81, 81, 85, 85, 85, 85, 89, 89, 89, 89, 93, 93, 93, 93, + 97, 97, 97, 97, 101, 101, 101, 101, 105, 105, 105, 105, 109, 109, 109, 109, + 113, 113, 113, 113, 117, 117, 117, 117, 121, 121, 121, 121, 125, 125, 125, 125, + 130, 130, 130, 130, 134, 134, 134, 134, 138, 138, 138, 138, 142, 142, 142, 142, + 146, 146, 146, 146, 150, 150, 150, 150, 154, 154, 154, 154, 158, 158, 158, 158, + 162, 162, 162, 162, 166, 166, 166, 166, 170, 170, 170, 170, 174, 174, 174, 174, + 178, 178, 178, 178, 182, 182, 182, 182, 186, 186, 186, 186, 190, 190, 190, 190, + 190, 195, 195, 195, 195, 199, 199, 199, 199, 203, 203, 203, 203, 207, 207, 207, + 207, 211, 211, 211, 211, 215, 215, 215, 215, 219, 219, 219, 219, 223, 223, 223, + 223, 227, 227, 227, 227, 231, 231, 231, 231, 235, 235, 235, 235, 239, 239, 239, + 239, 243, 243, 243, 243, 247, 247, 247, 247, 251, 251, 251, 251, 255, 255, 255 + }, + { + 0, 0, 3, 3, 3, 6, 6, 6, 9, 9, 9, 9, 13, 13, 13, 16, + 16, 16, 19, 19, 19, 22, 22, 22, 25, 25, 25, 25, 29, 29, 29, 32, + 32, 32, 35, 35, 35, 38, 38, 38, 38, 42, 42, 42, 45, 45, 45, 48, + 48, 48, 51, 51, 51, 54, 54, 54, 54, 58, 58, 58, 61, 61, 61, 64, + 64, 64, 67, 67, 67, 67, 71, 71, 71, 74, 74, 74, 77, 77, 77, 80, + 80, 80, 83, 83, 83, 83, 87, 87, 87, 90, 90, 90, 93, 93, 93, 96, + 96, 96, 96, 100, 100, 100, 103, 103, 103, 106, 106, 106, 109, 109, 109, 112, + 112, 112, 112, 116, 116, 116, 119, 119, 119, 122, 122, 122, 125, 125, 125, 125, + 130, 130, 130, 130, 133, 133, 133, 136, 136, 136, 139, 139, 139, 143, 143, 143, + 143, 146, 146, 146, 149, 149, 149, 152, 152, 152, 155, 155, 155, 159, 159, 159, + 159, 162, 162, 162, 165, 165, 165, 168, 168, 168, 172, 172, 172, 172, 175, 175, + 175, 178, 178, 178, 181, 181, 181, 184, 184, 184, 188, 188, 188, 188, 191, 191, + 191, 194, 194, 194, 197, 197, 197, 201, 201, 201, 201, 204, 204, 204, 207, 207, + 207, 210, 210, 210, 213, 213, 213, 217, 217, 217, 217, 220, 220, 220, 223, 223, + 223, 226, 226, 226, 230, 230, 230, 230, 233, 233, 233, 236, 236, 236, 239, 239, + 239, 242, 242, 242, 246, 246, 246, 246, 249, 249, 249, 252, 252, 252, 255, 255 + }, + { + 0, 0, 2, 2, 5, 5, 5, 8, 8, 8, 10, 10, 13, 13, 13, 16, + 16, 16, 18, 18, 21, 21, 21, 24, 24, 24, 26, 26, 29, 29, 29, 32, + 32, 32, 35, 35, 35, 37, 37, 40, 40, 40, 43, 43, 43, 45, 45, 48, + 48, 48, 51, 51, 51, 53, 53, 56, 56, 56, 59, 59, 59, 61, 61, 64, + 64, 64, 67, 67, 67, 70, 70, 70, 72, 72, 75, 75, 75, 78, 78, 78, + 80, 80, 83, 83, 83, 86, 86, 86, 88, 88, 91, 91, 91, 94, 94, 94, + 96, 96, 99, 99, 99, 102, 102, 102, 104, 104, 107, 107, 107, 110, 110, 110, + 112, 112, 115, 115, 115, 118, 118, 118, 120, 120, 123, 123, 123, 126, 126, 126, + 129, 129, 129, 132, 132, 132, 135, 135, 137, 137, 137, 140, 140, 140, 143, 143, + 145, 145, 145, 148, 148, 148, 151, 151, 153, 153, 153, 156, 156, 156, 159, 159, + 161, 161, 161, 164, 164, 164, 167, 167, 169, 169, 169, 172, 172, 172, 175, 175, + 177, 177, 177, 180, 180, 180, 183, 183, 185, 185, 185, 188, 188, 188, 191, 191, + 191, 194, 194, 196, 196, 196, 199, 199, 199, 202, 202, 204, 204, 204, 207, 207, + 207, 210, 210, 212, 212, 212, 215, 215, 215, 218, 218, 220, 220, 220, 223, 223, + 223, 226, 226, 226, 229, 229, 231, 231, 231, 234, 234, 234, 237, 237, 239, 239, + 239, 242, 242, 242, 245, 245, 247, 247, 247, 250, 250, 250, 253, 253, 255, 255 + }, + { + 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, + 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 26, 26, 28, 28, 30, 30, + 32, 32, 34, 34, 36, 36, 38, 38, 40, 40, 42, 42, 44, 44, 46, 46, + 48, 48, 50, 50, 52, 52, 54, 54, 56, 56, 58, 58, 60, 60, 62, 62, + 64, 64, 66, 66, 68, 68, 70, 70, 72, 72, 74, 74, 76, 76, 78, 78, + 80, 80, 82, 82, 84, 84, 86, 86, 88, 88, 90, 90, 92, 92, 94, 94, + 96, 96, 98, 98, 100, 100, 102, 102, 104, 104, 106, 106, 108, 108, 110, 110, + 112, 112, 114, 114, 116, 116, 118, 118, 120, 120, 122, 122, 124, 124, 126, 126, + 129, 129, 131, 131, 133, 133, 135, 135, 137, 137, 139, 139, 141, 141, 143, 143, + 145, 145, 147, 147, 149, 149, 151, 151, 153, 153, 155, 155, 157, 157, 159, 159, + 161, 161, 163, 163, 165, 165, 167, 167, 169, 169, 171, 171, 173, 173, 175, 175, + 177, 177, 179, 179, 181, 181, 183, 183, 185, 185, 187, 187, 189, 189, 191, 191, + 193, 193, 195, 195, 197, 197, 199, 199, 201, 201, 203, 203, 205, 205, 207, 207, + 209, 209, 211, 211, 213, 213, 215, 215, 217, 217, 219, 219, 221, 221, 223, 223, + 225, 225, 227, 227, 229, 229, 231, 231, 233, 233, 235, 235, 237, 237, 239, 239, + 241, 241, 243, 243, 245, 245, 247, 247, 249, 249, 251, 251, 253, 253, 255, 255 + }, + { + 0, 1, 1, 3, 4, 4, 6, 6, 8, 9, 9, 11, 12, 12, 14, 14, + 16, 17, 17, 19, 20, 20, 22, 22, 24, 25, 25, 27, 28, 28, 30, 30, + 32, 33, 33, 35, 36, 36, 38, 38, 40, 41, 41, 43, 44, 44, 46, 46, + 48, 49, 49, 51, 52, 52, 54, 54, 56, 57, 57, 59, 60, 60, 62, 62, + 64, 65, 65, 67, 68, 68, 70, 70, 72, 73, 73, 75, 76, 76, 78, 78, + 80, 81, 81, 83, 84, 84, 86, 86, 88, 89, 89, 91, 92, 92, 94, 94, + 96, 97, 97, 99, 100, 100, 102, 102, 104, 105, 105, 107, 108, 108, 110, 110, + 112, 113, 113, 115, 116, 116, 118, 118, 120, 121, 121, 123, 124, 124, 126, 126, + 129, 129, 131, 131, 132, 134, 134, 135, 137, 137, 139, 139, 140, 142, 142, 143, + 145, 145, 147, 147, 148, 150, 150, 151, 153, 153, 155, 155, 156, 158, 158, 159, + 161, 161, 163, 163, 164, 166, 166, 167, 169, 169, 171, 171, 172, 174, 174, 175, + 177, 177, 179, 179, 180, 182, 182, 183, 185, 185, 187, 187, 188, 190, 190, 191, + 193, 193, 195, 195, 196, 198, 198, 199, 201, 201, 203, 203, 204, 206, 206, 207, + 209, 209, 211, 211, 212, 214, 214, 215, 217, 217, 219, 219, 220, 222, 222, 223, + 225, 225, 227, 227, 228, 230, 230, 231, 233, 233, 235, 235, 236, 238, 238, 239, + 241, 241, 243, 243, 244, 246, 246, 247, 249, 249, 251, 251, 252, 254, 254, 255 + }, + { + 0, 1, 2, 2, 4, 5, 6, 6, 8, 9, 10, 10, 12, 13, 14, 14, + 16, 17, 18, 18, 20, 21, 22, 22, 24, 25, 26, 26, 28, 29, 30, 30, + 32, 33, 34, 34, 36, 37, 38, 38, 40, 41, 42, 42, 44, 45, 46, 46, + 48, 49, 50, 50, 52, 53, 54, 54, 56, 57, 58, 58, 60, 61, 62, 62, + 64, 65, 66, 66, 68, 69, 70, 70, 72, 73, 74, 74, 76, 77, 78, 78, + 80, 81, 82, 82, 84, 85, 86, 86, 88, 89, 90, 90, 92, 93, 94, 94, + 96, 97, 98, 98, 100, 101, 102, 102, 104, 105, 106, 106, 108, 109, 110, 110, + 112, 113, 114, 114, 116, 117, 118, 118, 120, 121, 122, 122, 124, 125, 126, 126, + 129, 129, 130, 131, 133, 133, 134, 135, 137, 137, 138, 139, 141, 141, 142, 143, + 145, 145, 146, 147, 149, 149, 150, 151, 153, 153, 154, 155, 157, 157, 158, 159, + 161, 161, 162, 163, 165, 165, 166, 167, 169, 169, 170, 171, 173, 173, 174, 175, + 177, 177, 178, 179, 181, 181, 182, 183, 185, 185, 186, 187, 189, 189, 190, 191, + 193, 193, 194, 195, 197, 197, 198, 199, 201, 201, 202, 203, 205, 205, 206, 207, + 209, 209, 210, 211, 213, 213, 214, 215, 217, 217, 218, 219, 221, 221, 222, 223, + 225, 225, 226, 227, 229, 229, 230, 231, 233, 233, 234, 235, 237, 237, 238, 239, + 241, 241, 242, 243, 245, 245, 246, 247, 249, 249, 250, 251, 253, 253, 254, 255 + }, + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, + 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 + } +}; + +// Starts from QUANT_6 +// Scrambled +const uint8_t color_uquant_to_scrambled_pquant_tables[17][256] { + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 + }, + { + 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1 + }, + { + 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1 + }, + { + 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, + 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, + 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, + 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, + 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, + 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, + 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, + 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, + 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31 + }, + { + 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, + 16, 24, 24, 24, 24, 24, 24, 32, 32, 32, 32, 32, 32, 32, 2, 2, + 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 10, 18, 18, 18, 18, 18, + 18, 26, 26, 26, 26, 26, 26, 26, 34, 34, 34, 34, 34, 34, 4, 4, + 4, 4, 4, 4, 4, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, + 20, 20, 28, 28, 28, 28, 28, 28, 36, 36, 36, 36, 36, 36, 36, 6, + 6, 6, 6, 6, 6, 14, 14, 14, 14, 14, 14, 14, 22, 22, 22, 22, + 22, 22, 30, 30, 30, 30, 30, 30, 30, 38, 38, 38, 38, 38, 38, 38, + 39, 39, 39, 39, 39, 39, 39, 31, 31, 31, 31, 31, 31, 31, 23, 23, + 23, 23, 23, 23, 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, + 7, 37, 37, 37, 37, 37, 37, 37, 29, 29, 29, 29, 29, 29, 21, 21, + 21, 21, 21, 21, 21, 13, 13, 13, 13, 13, 13, 5, 5, 5, 5, 5, + 5, 5, 35, 35, 35, 35, 35, 35, 27, 27, 27, 27, 27, 27, 27, 19, + 19, 19, 19, 19, 19, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, + 3, 3, 33, 33, 33, 33, 33, 33, 33, 25, 25, 25, 25, 25, 25, 17, + 17, 17, 17, 17, 17, 17, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1 + }, + { + 0, 0, 0, 16, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32, 2, 2, + 2, 2, 2, 18, 18, 18, 18, 18, 18, 34, 34, 34, 34, 34, 4, 4, + 4, 4, 4, 4, 20, 20, 20, 20, 20, 36, 36, 36, 36, 36, 6, 6, + 6, 6, 6, 6, 22, 22, 22, 22, 22, 38, 38, 38, 38, 38, 38, 8, + 8, 8, 8, 8, 24, 24, 24, 24, 24, 24, 40, 40, 40, 40, 40, 10, + 10, 10, 10, 10, 26, 26, 26, 26, 26, 26, 42, 42, 42, 42, 42, 12, + 12, 12, 12, 12, 12, 28, 28, 28, 28, 28, 44, 44, 44, 44, 44, 14, + 14, 14, 14, 14, 14, 30, 30, 30, 30, 30, 46, 46, 46, 46, 46, 46, + 47, 47, 47, 47, 47, 47, 31, 31, 31, 31, 31, 15, 15, 15, 15, 15, + 15, 45, 45, 45, 45, 45, 29, 29, 29, 29, 29, 13, 13, 13, 13, 13, + 13, 43, 43, 43, 43, 43, 27, 27, 27, 27, 27, 27, 11, 11, 11, 11, + 11, 41, 41, 41, 41, 41, 25, 25, 25, 25, 25, 25, 9, 9, 9, 9, + 9, 39, 39, 39, 39, 39, 39, 23, 23, 23, 23, 23, 7, 7, 7, 7, + 7, 7, 37, 37, 37, 37, 37, 21, 21, 21, 21, 21, 5, 5, 5, 5, + 5, 5, 35, 35, 35, 35, 35, 19, 19, 19, 19, 19, 19, 3, 3, 3, + 3, 3, 33, 33, 33, 33, 33, 17, 17, 17, 17, 17, 17, 1, 1, 1 + }, + { + 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, + 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, + 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, + 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, + 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, + 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, + 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, + 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, + 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, + 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39, + 40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, + 44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, + 47, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, + 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, + 55, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, + 59, 60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63 + }, + { + 0, 0, 16, 16, 16, 32, 32, 32, 48, 48, 48, 48, 64, 64, 64, 2, + 2, 2, 18, 18, 18, 34, 34, 34, 50, 50, 50, 50, 66, 66, 66, 4, + 4, 4, 20, 20, 20, 36, 36, 36, 36, 52, 52, 52, 68, 68, 68, 6, + 6, 6, 22, 22, 22, 38, 38, 38, 38, 54, 54, 54, 70, 70, 70, 8, + 8, 8, 24, 24, 24, 24, 40, 40, 40, 56, 56, 56, 72, 72, 72, 10, + 10, 10, 26, 26, 26, 26, 42, 42, 42, 58, 58, 58, 74, 74, 74, 12, + 12, 12, 12, 28, 28, 28, 44, 44, 44, 60, 60, 60, 76, 76, 76, 14, + 14, 14, 14, 30, 30, 30, 46, 46, 46, 62, 62, 62, 78, 78, 78, 78, + 79, 79, 79, 79, 63, 63, 63, 47, 47, 47, 31, 31, 31, 15, 15, 15, + 15, 77, 77, 77, 61, 61, 61, 45, 45, 45, 29, 29, 29, 13, 13, 13, + 13, 75, 75, 75, 59, 59, 59, 43, 43, 43, 27, 27, 27, 27, 11, 11, + 11, 73, 73, 73, 57, 57, 57, 41, 41, 41, 25, 25, 25, 25, 9, 9, + 9, 71, 71, 71, 55, 55, 55, 39, 39, 39, 39, 23, 23, 23, 7, 7, + 7, 69, 69, 69, 53, 53, 53, 37, 37, 37, 37, 21, 21, 21, 5, 5, + 5, 67, 67, 67, 51, 51, 51, 51, 35, 35, 35, 19, 19, 19, 3, 3, + 3, 65, 65, 65, 49, 49, 49, 49, 33, 33, 33, 17, 17, 17, 1, 1 + }, + { + 0, 0, 32, 32, 64, 64, 64, 2, 2, 2, 34, 34, 66, 66, 66, 4, + 4, 4, 36, 36, 68, 68, 68, 6, 6, 6, 38, 38, 70, 70, 70, 8, + 8, 8, 40, 40, 40, 72, 72, 10, 10, 10, 42, 42, 42, 74, 74, 12, + 12, 12, 44, 44, 44, 76, 76, 14, 14, 14, 46, 46, 46, 78, 78, 16, + 16, 16, 48, 48, 48, 80, 80, 80, 18, 18, 50, 50, 50, 82, 82, 82, + 20, 20, 52, 52, 52, 84, 84, 84, 22, 22, 54, 54, 54, 86, 86, 86, + 24, 24, 56, 56, 56, 88, 88, 88, 26, 26, 58, 58, 58, 90, 90, 90, + 28, 28, 60, 60, 60, 92, 92, 92, 30, 30, 62, 62, 62, 94, 94, 94, + 95, 95, 95, 63, 63, 63, 31, 31, 93, 93, 93, 61, 61, 61, 29, 29, + 91, 91, 91, 59, 59, 59, 27, 27, 89, 89, 89, 57, 57, 57, 25, 25, + 87, 87, 87, 55, 55, 55, 23, 23, 85, 85, 85, 53, 53, 53, 21, 21, + 83, 83, 83, 51, 51, 51, 19, 19, 81, 81, 81, 49, 49, 49, 17, 17, + 17, 79, 79, 47, 47, 47, 15, 15, 15, 77, 77, 45, 45, 45, 13, 13, + 13, 75, 75, 43, 43, 43, 11, 11, 11, 73, 73, 41, 41, 41, 9, 9, + 9, 71, 71, 71, 39, 39, 7, 7, 7, 69, 69, 69, 37, 37, 5, 5, + 5, 67, 67, 67, 35, 35, 3, 3, 3, 65, 65, 65, 33, 33, 1, 1 + }, + { + 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, + 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, + 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, + 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, + 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, + 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47, + 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, + 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, + 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, + 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79, + 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, + 88, 88, 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 94, 94, 95, 95, + 96, 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 103, 103, + 104, 104, 105, 105, 106, 106, 107, 107, 108, 108, 109, 109, 110, 110, 111, 111, + 112, 112, 113, 113, 114, 114, 115, 115, 116, 116, 117, 117, 118, 118, 119, 119, + 120, 120, 121, 121, 122, 122, 123, 123, 124, 124, 125, 125, 126, 126, 127, 127 + }, + { + 0, 32, 32, 64, 96, 96, 128, 128, 2, 34, 34, 66, 98, 98, 130, 130, + 4, 36, 36, 68, 100, 100, 132, 132, 6, 38, 38, 70, 102, 102, 134, 134, + 8, 40, 40, 72, 104, 104, 136, 136, 10, 42, 42, 74, 106, 106, 138, 138, + 12, 44, 44, 76, 108, 108, 140, 140, 14, 46, 46, 78, 110, 110, 142, 142, + 16, 48, 48, 80, 112, 112, 144, 144, 18, 50, 50, 82, 114, 114, 146, 146, + 20, 52, 52, 84, 116, 116, 148, 148, 22, 54, 54, 86, 118, 118, 150, 150, + 24, 56, 56, 88, 120, 120, 152, 152, 26, 58, 58, 90, 122, 122, 154, 154, + 28, 60, 60, 92, 124, 124, 156, 156, 30, 62, 62, 94, 126, 126, 158, 158, + 159, 159, 127, 127, 95, 63, 63, 31, 157, 157, 125, 125, 93, 61, 61, 29, + 155, 155, 123, 123, 91, 59, 59, 27, 153, 153, 121, 121, 89, 57, 57, 25, + 151, 151, 119, 119, 87, 55, 55, 23, 149, 149, 117, 117, 85, 53, 53, 21, + 147, 147, 115, 115, 83, 51, 51, 19, 145, 145, 113, 113, 81, 49, 49, 17, + 143, 143, 111, 111, 79, 47, 47, 15, 141, 141, 109, 109, 77, 45, 45, 13, + 139, 139, 107, 107, 75, 43, 43, 11, 137, 137, 105, 105, 73, 41, 41, 9, + 135, 135, 103, 103, 71, 39, 39, 7, 133, 133, 101, 101, 69, 37, 37, 5, + 131, 131, 99, 99, 67, 35, 35, 3, 129, 129, 97, 97, 65, 33, 33, 1 + }, + { + 0, 64, 128, 128, 2, 66, 130, 130, 4, 68, 132, 132, 6, 70, 134, 134, + 8, 72, 136, 136, 10, 74, 138, 138, 12, 76, 140, 140, 14, 78, 142, 142, + 16, 80, 144, 144, 18, 82, 146, 146, 20, 84, 148, 148, 22, 86, 150, 150, + 24, 88, 152, 152, 26, 90, 154, 154, 28, 92, 156, 156, 30, 94, 158, 158, + 32, 96, 160, 160, 34, 98, 162, 162, 36, 100, 164, 164, 38, 102, 166, 166, + 40, 104, 168, 168, 42, 106, 170, 170, 44, 108, 172, 172, 46, 110, 174, 174, + 48, 112, 176, 176, 50, 114, 178, 178, 52, 116, 180, 180, 54, 118, 182, 182, + 56, 120, 184, 184, 58, 122, 186, 186, 60, 124, 188, 188, 62, 126, 190, 190, + 191, 191, 127, 63, 189, 189, 125, 61, 187, 187, 123, 59, 185, 185, 121, 57, + 183, 183, 119, 55, 181, 181, 117, 53, 179, 179, 115, 51, 177, 177, 113, 49, + 175, 175, 111, 47, 173, 173, 109, 45, 171, 171, 107, 43, 169, 169, 105, 41, + 167, 167, 103, 39, 165, 165, 101, 37, 163, 163, 99, 35, 161, 161, 97, 33, + 159, 159, 95, 31, 157, 157, 93, 29, 155, 155, 91, 27, 153, 153, 89, 25, + 151, 151, 87, 23, 149, 149, 85, 21, 147, 147, 83, 19, 145, 145, 81, 17, + 143, 143, 79, 15, 141, 141, 77, 13, 139, 139, 75, 11, 137, 137, 73, 9, + 135, 135, 71, 7, 133, 133, 69, 5, 131, 131, 67, 3, 129, 129, 65, 1 + }, + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, + 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 + } +}; + +#endif + +// Starts from QUANT_6 +// Scrambled +static const uint8_t color_scrambled_pquant_to_uquant_q6[6] { + 0, 255, 51, 204, 102, 153 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q8[8] { + 0, 36, 73, 109, 146, 182, 219, 255 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q10[10] { + 0, 255, 28, 227, 56, 199, 84, 171, 113, 142 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q12[12] { + 0, 255, 69, 186, 23, 232, 92, 163, 46, 209, 116, 139 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q16[16] { + 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q20[20] { + 0, 255, 67, 188, 13, 242, 80, 175, 27, 228, 94, 161, 40, 215, 107, 148, + 54, 201, 121, 134 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q24[24] { + 0, 255, 33, 222, 66, 189, 99, 156, 11, 244, 44, 211, 77, 178, 110, 145, + 22, 233, 55, 200, 88, 167, 121, 134 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q32[32] { + 0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90, 99, 107, 115, 123, + 132, 140, 148, 156, 165, 173, 181, 189, 198, 206, 214, 222, 231, 239, 247, 255 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q40[40] { + 0, 255, 32, 223, 65, 190, 97, 158, 6, 249, 39, 216, 71, 184, 104, 151, + 13, 242, 45, 210, 78, 177, 110, 145, 19, 236, 52, 203, 84, 171, 117, 138, + 26, 229, 58, 197, 91, 164, 123, 132 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q48[48] { + 0, 255, 16, 239, 32, 223, 48, 207, 65, 190, 81, 174, 97, 158, 113, 142, + 5, 250, 21, 234, 38, 217, 54, 201, 70, 185, 86, 169, 103, 152, 119, 136, + 11, 244, 27, 228, 43, 212, 59, 196, 76, 179, 92, 163, 108, 147, 124, 131 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q64[64] { + 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, + 65, 69, 73, 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, + 130, 134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, + 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q80[80] { + 0, 255, 16, 239, 32, 223, 48, 207, 64, 191, 80, 175, 96, 159, 112, 143, + 3, 252, 19, 236, 35, 220, 51, 204, 67, 188, 83, 172, 100, 155, 116, 139, + 6, 249, 22, 233, 38, 217, 54, 201, 71, 184, 87, 168, 103, 152, 119, 136, + 9, 246, 25, 230, 42, 213, 58, 197, 74, 181, 90, 165, 106, 149, 122, 133, + 13, 242, 29, 226, 45, 210, 61, 194, 77, 178, 93, 162, 109, 146, 125, 130 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q96[96] { + 0, 255, 8, 247, 16, 239, 24, 231, 32, 223, 40, 215, 48, 207, 56, 199, + 64, 191, 72, 183, 80, 175, 88, 167, 96, 159, 104, 151, 112, 143, 120, 135, + 2, 253, 10, 245, 18, 237, 26, 229, 35, 220, 43, 212, 51, 204, 59, 196, + 67, 188, 75, 180, 83, 172, 91, 164, 99, 156, 107, 148, 115, 140, 123, 132, + 5, 250, 13, 242, 21, 234, 29, 226, 37, 218, 45, 210, 53, 202, 61, 194, + 70, 185, 78, 177, 86, 169, 94, 161, 102, 153, 110, 145, 118, 137, 126, 129 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q128[128] { + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, + 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, + 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, + 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, + 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, + 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, + 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, + 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q160[160] { + 0, 255, 8, 247, 16, 239, 24, 231, 32, 223, 40, 215, 48, 207, 56, 199, + 64, 191, 72, 183, 80, 175, 88, 167, 96, 159, 104, 151, 112, 143, 120, 135, + 1, 254, 9, 246, 17, 238, 25, 230, 33, 222, 41, 214, 49, 206, 57, 198, + 65, 190, 73, 182, 81, 174, 89, 166, 97, 158, 105, 150, 113, 142, 121, 134, + 3, 252, 11, 244, 19, 236, 27, 228, 35, 220, 43, 212, 51, 204, 59, 196, + 67, 188, 75, 180, 83, 172, 91, 164, 99, 156, 107, 148, 115, 140, 123, 132, + 4, 251, 12, 243, 20, 235, 28, 227, 36, 219, 44, 211, 52, 203, 60, 195, + 68, 187, 76, 179, 84, 171, 92, 163, 100, 155, 108, 147, 116, 139, 124, 131, + 6, 249, 14, 241, 22, 233, 30, 225, 38, 217, 46, 209, 54, 201, 62, 193, + 70, 185, 78, 177, 86, 169, 94, 161, 102, 153, 110, 145, 118, 137, 126, 129 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q192[192] { + 0, 255, 4, 251, 8, 247, 12, 243, 16, 239, 20, 235, 24, 231, 28, 227, + 32, 223, 36, 219, 40, 215, 44, 211, 48, 207, 52, 203, 56, 199, 60, 195, + 64, 191, 68, 187, 72, 183, 76, 179, 80, 175, 84, 171, 88, 167, 92, 163, + 96, 159, 100, 155, 104, 151, 108, 147, 112, 143, 116, 139, 120, 135, 124, 131, + 1, 254, 5, 250, 9, 246, 13, 242, 17, 238, 21, 234, 25, 230, 29, 226, + 33, 222, 37, 218, 41, 214, 45, 210, 49, 206, 53, 202, 57, 198, 61, 194, + 65, 190, 69, 186, 73, 182, 77, 178, 81, 174, 85, 170, 89, 166, 93, 162, + 97, 158, 101, 154, 105, 150, 109, 146, 113, 142, 117, 138, 121, 134, 125, 130, + 2, 253, 6, 249, 10, 245, 14, 241, 18, 237, 22, 233, 26, 229, 30, 225, + 34, 221, 38, 217, 42, 213, 46, 209, 50, 205, 54, 201, 58, 197, 62, 193, + 66, 189, 70, 185, 74, 181, 78, 177, 82, 173, 86, 169, 90, 165, 94, 161, + 98, 157, 102, 153, 106, 149, 110, 145, 114, 141, 118, 137, 122, 133, 126, 129 +}; + +static const uint8_t color_scrambled_pquant_to_uquant_q256[256] { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, + 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 +}; + +const uint8_t* color_scrambled_pquant_to_uquant_tables[17] { + color_scrambled_pquant_to_uquant_q6, + color_scrambled_pquant_to_uquant_q8, + color_scrambled_pquant_to_uquant_q10, + color_scrambled_pquant_to_uquant_q12, + color_scrambled_pquant_to_uquant_q16, + color_scrambled_pquant_to_uquant_q20, + color_scrambled_pquant_to_uquant_q24, + color_scrambled_pquant_to_uquant_q32, + color_scrambled_pquant_to_uquant_q40, + color_scrambled_pquant_to_uquant_q48, + color_scrambled_pquant_to_uquant_q64, + color_scrambled_pquant_to_uquant_q80, + color_scrambled_pquant_to_uquant_q96, + color_scrambled_pquant_to_uquant_q128, + color_scrambled_pquant_to_uquant_q160, + color_scrambled_pquant_to_uquant_q192, + color_scrambled_pquant_to_uquant_q256 +}; + +// The quant_mode_table[integer_count/2][bits] gives us the quantization level for a given integer +// count and number of bits that the integer may fit into. +const int8_t quant_mode_table[10][128] { + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 + }, + { + -1, -1, 0, 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, 0, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, + 8, 8, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 17, 18, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, + 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, + 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, + 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, + 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, + 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, + 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, + 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 19, 19, 19, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, + 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, + 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, + 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, + 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, + 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, + 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, + 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, + 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, + 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 19, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, + 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, + 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, + 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19 + }, + { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, + 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, + 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, + 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, + 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, + 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, + 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17 + } +}; diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_symbolic_physical.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_symbolic_physical.cpp new file mode 100644 index 00000000000..a19b907a24b --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_symbolic_physical.cpp @@ -0,0 +1,530 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Functions for converting between symbolic and physical encodings. + */ + +#include "astcenc_internal.h" + +#include <cassert> + +/** + * @brief Write up to 8 bits at an arbitrary bit offset. + * + * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so + * may span two separate bytes in memory. + * + * @param value The value to write. + * @param bitcount The number of bits to write, starting from LSB. + * @param bitoffset The bit offset to store at, between 0 and 7. + * @param[in,out] ptr The data pointer to write to. + */ +static inline void write_bits( + int value, + int bitcount, + int bitoffset, + uint8_t* ptr +) { + int mask = (1 << bitcount) - 1; + value &= mask; + ptr += bitoffset >> 3; + bitoffset &= 7; + value <<= bitoffset; + mask <<= bitoffset; + mask = ~mask; + + ptr[0] &= mask; + ptr[0] |= value; + ptr[1] &= mask >> 8; + ptr[1] |= value >> 8; +} + +/** + * @brief Read up to 8 bits at an arbitrary bit offset. + * + * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may + * span two separate bytes in memory. + * + * @param bitcount The number of bits to read. + * @param bitoffset The bit offset to read from, between 0 and 7. + * @param[in,out] ptr The data pointer to read from. + * + * @return The read value. + */ +static inline int read_bits( + int bitcount, + int bitoffset, + const uint8_t* ptr +) { + int mask = (1 << bitcount) - 1; + ptr += bitoffset >> 3; + bitoffset &= 7; + int value = ptr[0] | (ptr[1] << 8); + value >>= bitoffset; + value &= mask; + return value; +} + +/** + * @brief Reverse bits in a byte. + * + * @param p The value to reverse. + * + * @return The reversed result. + */ +static inline int bitrev8(int p) +{ + p = ((p & 0x0F) << 4) | ((p >> 4) & 0x0F); + p = ((p & 0x33) << 2) | ((p >> 2) & 0x33); + p = ((p & 0x55) << 1) | ((p >> 1) & 0x55); + return p; +} + +/* See header for documentation. */ +void symbolic_to_physical( + const block_size_descriptor& bsd, + const symbolic_compressed_block& scb, + physical_compressed_block& pcb +) { + assert(scb.block_type != SYM_BTYPE_ERROR); + + // Constant color block using UNORM16 colors + if (scb.block_type == SYM_BTYPE_CONST_U16) + { + // There is currently no attempt to coalesce larger void-extents + static const uint8_t cbytes[8] { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + for (unsigned int i = 0; i < 8; i++) + { + pcb.data[i] = cbytes[i]; + } + + for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++) + { + pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF; + pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF; + } + + return; + } + + // Constant color block using FP16 colors + if (scb.block_type == SYM_BTYPE_CONST_F16) + { + // There is currently no attempt to coalesce larger void-extents + static const uint8_t cbytes[8] { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + for (unsigned int i = 0; i < 8; i++) + { + pcb.data[i] = cbytes[i]; + } + + for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++) + { + pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF; + pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF; + } + + return; + } + + unsigned int partition_count = scb.partition_count; + + // Compress the weights. + // They are encoded as an ordinary integer-sequence, then bit-reversed + uint8_t weightbuf[16] { 0 }; + + const auto& bm = bsd.get_block_mode(scb.block_mode); + const auto& di = bsd.get_decimation_info(bm.decimation_mode); + int weight_count = di.weight_count; + quant_method weight_quant_method = bm.get_weight_quant_mode(); + float weight_quant_levels = static_cast<float>(get_quant_level(weight_quant_method)); + int is_dual_plane = bm.is_dual_plane; + + const auto& qat = quant_and_xfer_tables[weight_quant_method]; + + int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count; + + int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method); + + uint8_t weights[64]; + if (is_dual_plane) + { + for (int i = 0; i < weight_count; i++) + { + float uqw = static_cast<float>(scb.weights[i]); + float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f); + int qwi = static_cast<int>(qw + 0.5f); + weights[2 * i] = qat.scramble_map[qwi]; + + uqw = static_cast<float>(scb.weights[i + WEIGHTS_PLANE2_OFFSET]); + qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f); + qwi = static_cast<int>(qw + 0.5f); + weights[2 * i + 1] = qat.scramble_map[qwi]; + } + } + else + { + for (int i = 0; i < weight_count; i++) + { + float uqw = static_cast<float>(scb.weights[i]); + float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f); + int qwi = static_cast<int>(qw + 0.5f); + weights[i] = qat.scramble_map[qwi]; + } + } + + encode_ise(weight_quant_method, real_weight_count, weights, weightbuf, 0); + + for (int i = 0; i < 16; i++) + { + pcb.data[i] = static_cast<uint8_t>(bitrev8(weightbuf[15 - i])); + } + + write_bits(scb.block_mode, 11, 0, pcb.data); + write_bits(partition_count - 1, 2, 11, pcb.data); + + int below_weights_pos = 128 - bits_for_weights; + + // Encode partition index and color endpoint types for blocks with 2+ partitions + if (partition_count > 1) + { + write_bits(scb.partition_index, 6, 13, pcb.data); + write_bits(scb.partition_index >> 6, PARTITION_INDEX_BITS - 6, 19, pcb.data); + + if (scb.color_formats_matched) + { + write_bits(scb.color_formats[0] << 2, 6, 13 + PARTITION_INDEX_BITS, pcb.data); + } + else + { + // Check endpoint types for each partition to determine the lowest class present + int low_class = 4; + + for (unsigned int i = 0; i < partition_count; i++) + { + int class_of_format = scb.color_formats[i] >> 2; + low_class = astc::min(class_of_format, low_class); + } + + if (low_class == 3) + { + low_class = 2; + } + + int encoded_type = low_class + 1; + int bitpos = 2; + + for (unsigned int i = 0; i < partition_count; i++) + { + int classbit_of_format = (scb.color_formats[i] >> 2) - low_class; + encoded_type |= classbit_of_format << bitpos; + bitpos++; + } + + for (unsigned int i = 0; i < partition_count; i++) + { + int lowbits_of_format = scb.color_formats[i] & 3; + encoded_type |= lowbits_of_format << bitpos; + bitpos += 2; + } + + int encoded_type_lowpart = encoded_type & 0x3F; + int encoded_type_highpart = encoded_type >> 6; + int encoded_type_highpart_size = (3 * partition_count) - 4; + int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size; + write_bits(encoded_type_lowpart, 6, 13 + PARTITION_INDEX_BITS, pcb.data); + write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, pcb.data); + below_weights_pos -= encoded_type_highpart_size; + } + } + else + { + write_bits(scb.color_formats[0], 4, 13, pcb.data); + } + + // In dual-plane mode, encode the color component of the second plane of weights + if (is_dual_plane) + { + write_bits(scb.plane2_component, 2, below_weights_pos - 2, pcb.data); + } + + // Encode the color components + uint8_t values_to_encode[32]; + int valuecount_to_encode = 0; + + const uint8_t* pack_table = color_uquant_to_scrambled_pquant_tables[scb.quant_mode - QUANT_6]; + for (unsigned int i = 0; i < scb.partition_count; i++) + { + int vals = 2 * (scb.color_formats[i] >> 2) + 2; + assert(vals <= 8); + for (int j = 0; j < vals; j++) + { + values_to_encode[j + valuecount_to_encode] = pack_table[scb.color_values[i][j]]; + } + valuecount_to_encode += vals; + } + + encode_ise(scb.get_color_quant_mode(), valuecount_to_encode, values_to_encode, pcb.data, + scb.partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS); +} + +/* See header for documentation. */ +void physical_to_symbolic( + const block_size_descriptor& bsd, + const physical_compressed_block& pcb, + symbolic_compressed_block& scb +) { + uint8_t bswapped[16]; + + scb.block_type = SYM_BTYPE_NONCONST; + + // Extract header fields + int block_mode = read_bits(11, 0, pcb.data); + if ((block_mode & 0x1FF) == 0x1FC) + { + // Constant color block + + // Check what format the data has + if (block_mode & 0x200) + { + scb.block_type = SYM_BTYPE_CONST_F16; + } + else + { + scb.block_type = SYM_BTYPE_CONST_U16; + } + + scb.partition_count = 0; + for (int i = 0; i < 4; i++) + { + scb.constant_color[i] = pcb.data[2 * i + 8] | (pcb.data[2 * i + 9] << 8); + } + + // Additionally, check that the void-extent + if (bsd.zdim == 1) + { + // 2D void-extent + int rsvbits = read_bits(2, 10, pcb.data); + if (rsvbits != 3) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + + int vx_low_s = read_bits(8, 12, pcb.data) | (read_bits(5, 12 + 8, pcb.data) << 8); + int vx_high_s = read_bits(8, 25, pcb.data) | (read_bits(5, 25 + 8, pcb.data) << 8); + int vx_low_t = read_bits(8, 38, pcb.data) | (read_bits(5, 38 + 8, pcb.data) << 8); + int vx_high_t = read_bits(8, 51, pcb.data) | (read_bits(5, 51 + 8, pcb.data) << 8); + + int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF && vx_low_t == 0x1FFF && vx_high_t == 0x1FFF; + + if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + } + else + { + // 3D void-extent + int vx_low_s = read_bits(9, 10, pcb.data); + int vx_high_s = read_bits(9, 19, pcb.data); + int vx_low_t = read_bits(9, 28, pcb.data); + int vx_high_t = read_bits(9, 37, pcb.data); + int vx_low_p = read_bits(9, 46, pcb.data); + int vx_high_p = read_bits(9, 55, pcb.data); + + int all_ones = vx_low_s == 0x1FF && vx_high_s == 0x1FF && vx_low_t == 0x1FF && vx_high_t == 0x1FF && vx_low_p == 0x1FF && vx_high_p == 0x1FF; + + if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_p >= vx_high_p) && !all_ones) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + } + + return; + } + + unsigned int packed_index = bsd.block_mode_packed_index[block_mode]; + if (packed_index == BLOCK_BAD_BLOCK_MODE) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + + const auto& bm = bsd.get_block_mode(block_mode); + const auto& di = bsd.get_decimation_info(bm.decimation_mode); + + int weight_count = di.weight_count; + quant_method weight_quant_method = static_cast<quant_method>(bm.quant_mode); + int is_dual_plane = bm.is_dual_plane; + + int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count; + + int partition_count = read_bits(2, 11, pcb.data) + 1; + + scb.block_mode = static_cast<uint16_t>(block_mode); + scb.partition_count = static_cast<uint8_t>(partition_count); + + for (int i = 0; i < 16; i++) + { + bswapped[i] = static_cast<uint8_t>(bitrev8(pcb.data[15 - i])); + } + + int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method); + + int below_weights_pos = 128 - bits_for_weights; + + uint8_t indices[64]; + const auto& qat = quant_and_xfer_tables[weight_quant_method]; + + decode_ise(weight_quant_method, real_weight_count, bswapped, indices, 0); + + if (is_dual_plane) + { + for (int i = 0; i < weight_count; i++) + { + scb.weights[i] = qat.unscramble_and_unquant_map[indices[2 * i]]; + scb.weights[i + WEIGHTS_PLANE2_OFFSET] = qat.unscramble_and_unquant_map[indices[2 * i + 1]]; + } + } + else + { + for (int i = 0; i < weight_count; i++) + { + scb.weights[i] = qat.unscramble_and_unquant_map[indices[i]]; + } + } + + if (is_dual_plane && partition_count == 4) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + + scb.color_formats_matched = 0; + + // Determine the format of each endpoint pair + int color_formats[BLOCK_MAX_PARTITIONS]; + int encoded_type_highpart_size = 0; + if (partition_count == 1) + { + color_formats[0] = read_bits(4, 13, pcb.data); + scb.partition_index = 0; + } + else + { + encoded_type_highpart_size = (3 * partition_count) - 4; + below_weights_pos -= encoded_type_highpart_size; + int encoded_type = read_bits(6, 13 + PARTITION_INDEX_BITS, pcb.data) | (read_bits(encoded_type_highpart_size, below_weights_pos, pcb.data) << 6); + int baseclass = encoded_type & 0x3; + if (baseclass == 0) + { + for (int i = 0; i < partition_count; i++) + { + color_formats[i] = (encoded_type >> 2) & 0xF; + } + + below_weights_pos += encoded_type_highpart_size; + scb.color_formats_matched = 1; + encoded_type_highpart_size = 0; + } + else + { + int bitpos = 2; + baseclass--; + + for (int i = 0; i < partition_count; i++) + { + color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2; + bitpos++; + } + + for (int i = 0; i < partition_count; i++) + { + color_formats[i] |= (encoded_type >> bitpos) & 3; + bitpos += 2; + } + } + scb.partition_index = static_cast<uint16_t>(read_bits(6, 13, pcb.data) | (read_bits(PARTITION_INDEX_BITS - 6, 19, pcb.data) << 6)); + } + + for (int i = 0; i < partition_count; i++) + { + scb.color_formats[i] = static_cast<uint8_t>(color_formats[i]); + } + + // Determine number of color endpoint integers + int color_integer_count = 0; + for (int i = 0; i < partition_count; i++) + { + int endpoint_class = color_formats[i] >> 2; + color_integer_count += (endpoint_class + 1) * 2; + } + + if (color_integer_count > 18) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + + // Determine the color endpoint format to use + static const int color_bits_arr[5] { -1, 115 - 4, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS }; + int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size; + if (is_dual_plane) + { + color_bits -= 2; + } + + if (color_bits < 0) + { + color_bits = 0; + } + + int color_quant_level = quant_mode_table[color_integer_count >> 1][color_bits]; + if (color_quant_level < QUANT_6) + { + scb.block_type = SYM_BTYPE_ERROR; + return; + } + + // Unpack the integer color values and assign to endpoints + scb.quant_mode = static_cast<quant_method>(color_quant_level); + + uint8_t values_to_decode[32]; + decode_ise(static_cast<quant_method>(color_quant_level), color_integer_count, pcb.data, + values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS)); + + int valuecount_to_decode = 0; + const uint8_t* unpack_table = color_scrambled_pquant_to_uquant_tables[scb.quant_mode - QUANT_6]; + for (int i = 0; i < partition_count; i++) + { + int vals = 2 * (color_formats[i] >> 2) + 2; + for (int j = 0; j < vals; j++) + { + scb.color_values[i][j] = unpack_table[values_to_decode[j + valuecount_to_decode]]; + } + valuecount_to_decode += vals; + } + + // Fetch component for second-plane in the case of dual plane of weights. + if (is_dual_plane) + { + scb.plane2_component = static_cast<int8_t>(read_bits(2, below_weights_pos - 2, pcb.data)); + } +} diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib.h new file mode 100644 index 00000000000..6085c8eb2a6 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib.h @@ -0,0 +1,570 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2019-2022 Arm Limited +// Copyright 2008 Jose Fonseca +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/* + * This module implements vector support for floats, ints, and vector lane + * control masks. It provides access to both explicit vector width types, and + * flexible N-wide types where N can be determined at compile time. + * + * The design of this module encourages use of vector length agnostic code, via + * the vint, vfloat, and vmask types. These will take on the widest SIMD vector + * with that is available at compile time. The current vector width is + * accessible for e.g. loop strides via the ASTCENC_SIMD_WIDTH constant. + * + * Explicit scalar types are acessible via the vint1, vfloat1, vmask1 types. + * These are provided primarily for prototyping and algorithm debug of VLA + * implementations. + * + * Explicit 4-wide types are accessible via the vint4, vfloat4, and vmask4 + * types. These are provided for use by VLA code, but are also expected to be + * used as a fixed-width type and will supported a reference C++ fallback for + * use on platforms without SIMD intrinsics. + * + * Explicit 8-wide types are accessible via the vint8, vfloat8, and vmask8 + * types. These are provide for use by VLA code, and are not expected to be + * used as a fixed-width type in normal code. No reference C implementation is + * provided on platforms without underlying SIMD intrinsics. + * + * With the current implementation ISA support is provided for: + * + * * 1-wide for scalar reference. + * * 4-wide for Armv8-A NEON. + * * 4-wide for x86-64 SSE2. + * * 4-wide for x86-64 SSE4.1. + * * 8-wide for x86-64 AVX2. + */ + +#ifndef ASTC_VECMATHLIB_H_INCLUDED +#define ASTC_VECMATHLIB_H_INCLUDED + +#if ASTCENC_SSE != 0 || ASTCENC_AVX != 0 + #include <immintrin.h> +#elif ASTCENC_NEON != 0 + #include <arm_neon.h> +#endif + +#if !defined(__clang__) && defined(_MSC_VER) + #define ASTCENC_SIMD_INLINE __forceinline + #define ASTCENC_NO_INLINE +#elif defined(__GNUC__) && !defined(__clang__) + #define ASTCENC_SIMD_INLINE __attribute__((always_inline)) inline + #define ASTCENC_NO_INLINE __attribute__ ((noinline)) +#else + #define ASTCENC_SIMD_INLINE __attribute__((always_inline, nodebug)) inline + #define ASTCENC_NO_INLINE __attribute__ ((noinline)) +#endif + +#if ASTCENC_AVX >= 2 + /* If we have AVX2 expose 8-wide VLA. */ + #include "astcenc_vecmathlib_sse_4.h" + #include "astcenc_vecmathlib_common_4.h" + #include "astcenc_vecmathlib_avx2_8.h" + + #define ASTCENC_SIMD_WIDTH 8 + + using vfloat = vfloat8; + + #if defined(ASTCENC_NO_INVARIANCE) + using vfloatacc = vfloat8; + #else + using vfloatacc = vfloat4; + #endif + + using vint = vint8; + using vmask = vmask8; + + constexpr auto loada = vfloat8::loada; + constexpr auto load1 = vfloat8::load1; + +#elif ASTCENC_SSE >= 20 + /* If we have SSE expose 4-wide VLA, and 4-wide fixed width. */ + #include "astcenc_vecmathlib_sse_4.h" + #include "astcenc_vecmathlib_common_4.h" + + #define ASTCENC_SIMD_WIDTH 4 + + using vfloat = vfloat4; + using vfloatacc = vfloat4; + using vint = vint4; + using vmask = vmask4; + + constexpr auto loada = vfloat4::loada; + constexpr auto load1 = vfloat4::load1; + +#elif ASTCENC_NEON > 0 + /* If we have NEON expose 4-wide VLA. */ + #include "astcenc_vecmathlib_neon_4.h" + #include "astcenc_vecmathlib_common_4.h" + + #define ASTCENC_SIMD_WIDTH 4 + + using vfloat = vfloat4; + using vfloatacc = vfloat4; + using vint = vint4; + using vmask = vmask4; + + constexpr auto loada = vfloat4::loada; + constexpr auto load1 = vfloat4::load1; + +#else + // If we have nothing expose 4-wide VLA, and 4-wide fixed width. + + // Note: We no longer expose the 1-wide scalar fallback because it is not + // invariant with the 4-wide path due to algorithms that use horizontal + // operations that accumulate a local vector sum before accumulating into + // a running sum. + // + // For 4 items adding into an accumulator using 1-wide vectors the sum is: + // + // result = ((((sum + l0) + l1) + l2) + l3) + // + // ... whereas the accumulator for a 4-wide vector sum is: + // + // result = sum + ((l0 + l2) + (l1 + l3)) + // + // In "normal maths" this is the same, but the floating point reassociation + // differences mean that these will not produce the same result. + + #include "astcenc_vecmathlib_none_4.h" + #include "astcenc_vecmathlib_common_4.h" + + #define ASTCENC_SIMD_WIDTH 4 + + using vfloat = vfloat4; + using vfloatacc = vfloat4; + using vint = vint4; + using vmask = vmask4; + + constexpr auto loada = vfloat4::loada; + constexpr auto load1 = vfloat4::load1; +#endif + +/** + * @brief Round a count down to the largest multiple of 8. + * + * @param count The unrounded value. + * + * @return The rounded value. + */ +ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_8(unsigned int count) +{ + return count & static_cast<unsigned int>(~(8 - 1)); +} + +/** + * @brief Round a count down to the largest multiple of 4. + * + * @param count The unrounded value. + * + * @return The rounded value. + */ +ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_4(unsigned int count) +{ + return count & static_cast<unsigned int>(~(4 - 1)); +} + +/** + * @brief Round a count down to the largest multiple of the SIMD width. + * + * Assumption that the vector width is a power of two ... + * + * @param count The unrounded value. + * + * @return The rounded value. + */ +ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_vla(unsigned int count) +{ + return count & static_cast<unsigned int>(~(ASTCENC_SIMD_WIDTH - 1)); +} + +/** + * @brief Round a count up to the largest multiple of the SIMD width. + * + * Assumption that the vector width is a power of two ... + * + * @param count The unrounded value. + * + * @return The rounded value. + */ +ASTCENC_SIMD_INLINE unsigned int round_up_to_simd_multiple_vla(unsigned int count) +{ + unsigned int multiples = (count + ASTCENC_SIMD_WIDTH - 1) / ASTCENC_SIMD_WIDTH; + return multiples * ASTCENC_SIMD_WIDTH; +} + +/** + * @brief Return @c a with lanes negated if the @c b lane is negative. + */ +ASTCENC_SIMD_INLINE vfloat change_sign(vfloat a, vfloat b) +{ + vint ia = float_as_int(a); + vint ib = float_as_int(b); + vint sign_mask(static_cast<int>(0x80000000)); + vint r = ia ^ (ib & sign_mask); + return int_as_float(r); +} + +/** + * @brief Return fast, but approximate, vector atan(x). + * + * Max error of this implementation is 0.004883. + */ +ASTCENC_SIMD_INLINE vfloat atan(vfloat x) +{ + vmask c = abs(x) > vfloat(1.0f); + vfloat z = change_sign(vfloat(astc::PI_OVER_TWO), x); + vfloat y = select(x, vfloat(1.0f) / x, c); + y = y / (y * y * vfloat(0.28f) + vfloat(1.0f)); + return select(y, z - y, c); +} + +/** + * @brief Return fast, but approximate, vector atan2(x, y). + */ +ASTCENC_SIMD_INLINE vfloat atan2(vfloat y, vfloat x) +{ + vfloat z = atan(abs(y / x)); + vmask xmask = vmask(float_as_int(x).m); + return change_sign(select_msb(z, vfloat(astc::PI) - z, xmask), y); +} + +/* + * @brief Factory that returns a unit length 4 component vfloat4. + */ +static ASTCENC_SIMD_INLINE vfloat4 unit4() +{ + return vfloat4(0.5f); +} + +/** + * @brief Factory that returns a unit length 3 component vfloat4. + */ +static ASTCENC_SIMD_INLINE vfloat4 unit3() +{ + float val = 0.577350258827209473f; + return vfloat4(val, val, val, 0.0f); +} + +/** + * @brief Factory that returns a unit length 2 component vfloat4. + */ +static ASTCENC_SIMD_INLINE vfloat4 unit2() +{ + float val = 0.707106769084930420f; + return vfloat4(val, val, 0.0f, 0.0f); +} + +/** + * @brief Factory that returns a 3 component vfloat4. + */ +static ASTCENC_SIMD_INLINE vfloat4 vfloat3(float a, float b, float c) +{ + return vfloat4(a, b, c, 0.0f); +} + +/** + * @brief Factory that returns a 2 component vfloat4. + */ +static ASTCENC_SIMD_INLINE vfloat4 vfloat2(float a, float b) +{ + return vfloat4(a, b, 0.0f, 0.0f); +} + +/** + * @brief Normalize a non-zero length vector to unit length. + */ +static ASTCENC_SIMD_INLINE vfloat4 normalize(vfloat4 a) +{ + vfloat4 length = dot(a, a); + return a / sqrt(length); +} + +/** + * @brief Normalize a vector, returning @c safe if len is zero. + */ +static ASTCENC_SIMD_INLINE vfloat4 normalize_safe(vfloat4 a, vfloat4 safe) +{ + vfloat4 length = dot(a, a); + if (length.lane<0>() != 0.0f) + { + return a / sqrt(length); + } + + return safe; +} + + + +#define POLY0(x, c0) ( c0) +#define POLY1(x, c0, c1) ((POLY0(x, c1) * x) + c0) +#define POLY2(x, c0, c1, c2) ((POLY1(x, c1, c2) * x) + c0) +#define POLY3(x, c0, c1, c2, c3) ((POLY2(x, c1, c2, c3) * x) + c0) +#define POLY4(x, c0, c1, c2, c3, c4) ((POLY3(x, c1, c2, c3, c4) * x) + c0) +#define POLY5(x, c0, c1, c2, c3, c4, c5) ((POLY4(x, c1, c2, c3, c4, c5) * x) + c0) + +/** + * @brief Compute an approximate exp2(x) for each lane in the vector. + * + * Based on 5th degree minimax polynomials, ported from this blog + * https://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html + */ +static ASTCENC_SIMD_INLINE vfloat4 exp2(vfloat4 x) +{ + x = clamp(-126.99999f, 129.0f, x); + + vint4 ipart = float_to_int(x - 0.5f); + vfloat4 fpart = x - int_to_float(ipart); + + // Integer contrib, using 1 << ipart + vfloat4 iexp = int_as_float(lsl<23>(ipart + 127)); + + // Fractional contrib, using polynomial fit of 2^x in range [-0.5, 0.5) + vfloat4 fexp = POLY5(fpart, + 9.9999994e-1f, + 6.9315308e-1f, + 2.4015361e-1f, + 5.5826318e-2f, + 8.9893397e-3f, + 1.8775767e-3f); + + return iexp * fexp; +} + +/** + * @brief Compute an approximate log2(x) for each lane in the vector. + * + * Based on 5th degree minimax polynomials, ported from this blog + * https://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html + */ +static ASTCENC_SIMD_INLINE vfloat4 log2(vfloat4 x) +{ + vint4 exp(0x7F800000); + vint4 mant(0x007FFFFF); + vint4 one(0x3F800000); + + vint4 i = float_as_int(x); + + vfloat4 e = int_to_float(lsr<23>(i & exp) - 127); + + vfloat4 m = int_as_float((i & mant) | one); + + // Polynomial fit of log2(x)/(x - 1), for x in range [1, 2) + vfloat4 p = POLY4(m, + 2.8882704548164776201f, + -2.52074962577807006663f, + 1.48116647521213171641f, + -0.465725644288844778798f, + 0.0596515482674574969533f); + + // Increases the polynomial degree, but ensures that log2(1) == 0 + p = p * (m - 1.0f); + + return p + e; +} + +/** + * @brief Compute an approximate pow(x, y) for each lane in the vector. + * + * Power function based on the exp2(log2(x) * y) transform. + */ +static ASTCENC_SIMD_INLINE vfloat4 pow(vfloat4 x, vfloat4 y) +{ + vmask4 zero_mask = y == vfloat4(0.0f); + vfloat4 estimate = exp2(log2(x) * y); + + // Guarantee that y == 0 returns exactly 1.0f + return select(estimate, vfloat4(1.0f), zero_mask); +} + +/** + * @brief Count the leading zeros for each lane in @c a. + * + * Valid for all data values of @c a; will return a per-lane value [0, 32]. + */ +static ASTCENC_SIMD_INLINE vint4 clz(vint4 a) +{ + // This function is a horrible abuse of floating point exponents to convert + // the original integer value into a 2^N encoding we can recover easily. + + // Convert to float without risk of rounding up by keeping only top 8 bits. + // This trick is is guranteed to keep top 8 bits and clear the 9th. + a = (~lsr<8>(a)) & a; + a = float_as_int(int_to_float(a)); + + // Extract and unbias exponent + a = vint4(127 + 31) - lsr<23>(a); + + // Clamp result to a valid 32-bit range + return clamp(0, 32, a); +} + +/** + * @brief Return lanewise 2^a for each lane in @c a. + * + * Use of signed int means that this is only valid for values in range [0, 31]. + */ +static ASTCENC_SIMD_INLINE vint4 two_to_the_n(vint4 a) +{ + // 2^30 is the largest signed number than can be represented + assert(all(a < vint4(31))); + + // This function is a horrible abuse of floating point to use the exponent + // and float conversion to generate a 2^N multiple. + + // Bias the exponent + vint4 exp = a + 127; + exp = lsl<23>(exp); + + // Reinterpret the bits as a float, and then convert to an int + vfloat4 f = int_as_float(exp); + return float_to_int(f); +} + +/** + * @brief Convert unorm16 [0, 65535] to float16 in range [0, 1]. + */ +static ASTCENC_SIMD_INLINE vint4 unorm16_to_sf16(vint4 p) +{ + vint4 fp16_one = vint4(0x3C00); + vint4 fp16_small = lsl<8>(p); + + vmask4 is_one = p == vint4(0xFFFF); + vmask4 is_small = p < vint4(4); + + // Manually inline clz() on Visual Studio to avoid release build codegen bug + // see https://github.com/ARM-software/astc-encoder/issues/259 +#if !defined(__clang__) && defined(_MSC_VER) + vint4 a = (~lsr<8>(p)) & p; + a = float_as_int(int_to_float(a)); + a = vint4(127 + 31) - lsr<23>(a); + vint4 lz = clamp(0, 32, a) - 16; +#else + vint4 lz = clz(p) - 16; +#endif + + p = p * two_to_the_n(lz + 1); + p = p & vint4(0xFFFF); + + p = lsr<6>(p); + + p = p | lsl<10>(vint4(14) - lz); + + vint4 r = select(p, fp16_one, is_one); + r = select(r, fp16_small, is_small); + return r; +} + +/** + * @brief Convert 16-bit LNS to float16. + */ +static ASTCENC_SIMD_INLINE vint4 lns_to_sf16(vint4 p) +{ + vint4 mc = p & 0x7FF; + vint4 ec = lsr<11>(p); + + vint4 mc_512 = mc * 3; + vmask4 mask_512 = mc < vint4(512); + + vint4 mc_1536 = mc * 4 - 512; + vmask4 mask_1536 = mc < vint4(1536); + + vint4 mc_else = mc * 5 - 2048; + + vint4 mt = mc_else; + mt = select(mt, mc_1536, mask_1536); + mt = select(mt, mc_512, mask_512); + + vint4 res = lsl<10>(ec) | lsr<3>(mt); + return min(res, vint4(0x7BFF)); +} + +/** + * @brief Extract mantissa and exponent of a float value. + * + * @param a The input value. + * @param[out] exp The output exponent. + * + * @return The mantissa. + */ +static ASTCENC_SIMD_INLINE vfloat4 frexp(vfloat4 a, vint4& exp) +{ + // Interpret the bits as an integer + vint4 ai = float_as_int(a); + + // Extract and unbias the exponent + exp = (lsr<23>(ai) & 0xFF) - 126; + + // Extract and unbias the mantissa + vint4 manti = (ai & static_cast<int>(0x807FFFFF)) | 0x3F000000; + return int_as_float(manti); +} + +/** + * @brief Convert float to 16-bit LNS. + */ +static ASTCENC_SIMD_INLINE vfloat4 float_to_lns(vfloat4 a) +{ + vint4 exp; + vfloat4 mant = frexp(a, exp); + + // Do these early before we start messing about ... + vmask4 mask_underflow_nan = ~(a > vfloat4(1.0f / 67108864.0f)); + vmask4 mask_infinity = a >= vfloat4(65536.0f); + + // If input is smaller than 2^-14, multiply by 2^25 and don't bias. + vmask4 exp_lt_m13 = exp < vint4(-13); + + vfloat4 a1a = a * 33554432.0f; + vint4 expa = vint4::zero(); + + vfloat4 a1b = (mant - 0.5f) * 4096; + vint4 expb = exp + 14; + + a = select(a1b, a1a, exp_lt_m13); + exp = select(expb, expa, exp_lt_m13); + + vmask4 a_lt_384 = a < vfloat4(384.0f); + vmask4 a_lt_1408 = a <= vfloat4(1408.0f); + + vfloat4 a2a = a * (4.0f / 3.0f); + vfloat4 a2b = a + 128.0f; + vfloat4 a2c = (a + 512.0f) * (4.0f / 5.0f); + + a = a2c; + a = select(a, a2b, a_lt_1408); + a = select(a, a2a, a_lt_384); + + a = a + (int_to_float(exp) * 2048.0f) + 1.0f; + + a = select(a, vfloat4(65535.0f), mask_infinity); + a = select(a, vfloat4::zero(), mask_underflow_nan); + + return a; +} + +namespace astc +{ + +static ASTCENC_SIMD_INLINE float pow(float x, float y) +{ + return pow(vfloat4(x), vfloat4(y)).lane<0>(); +} + +} + +#endif // #ifndef ASTC_VECMATHLIB_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_avx2_8.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_avx2_8.h new file mode 100644 index 00000000000..a785aca75b2 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_avx2_8.h @@ -0,0 +1,1204 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2019-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief 8x32-bit vectors, implemented using AVX2. + * + * This module implements 8-wide 32-bit float, int, and mask vectors for x86 + * AVX2. + * + * There is a baseline level of functionality provided by all vector widths and + * implementations. This is implemented using identical function signatures, + * modulo data type, so we can use them as substitutable implementations in VLA + * code. + */ + +#ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED +#define ASTC_VECMATHLIB_AVX2_8_H_INCLUDED + +#ifndef ASTCENC_SIMD_INLINE + #error "Include astcenc_vecmathlib.h, do not include directly" +#endif + +#include <cstdio> + +// Define convenience intrinsics that are missing on older compilers +#define astcenc_mm256_set_m128i(m, n) _mm256_insertf128_si256(_mm256_castsi128_si256((n)), (m), 1) + +// ============================================================================ +// vfloat8 data type +// ============================================================================ + +/** + * @brief Data type for 8-wide floats. + */ +struct vfloat8 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vfloat8() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vfloat8(const float *p) + { + m = _mm256_loadu_ps(p); + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vfloat8(float a) + { + m = _mm256_set1_ps(a); + } + + /** + * @brief Construct from 8 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat8( + float a, float b, float c, float d, + float e, float f, float g, float h) + { + m = _mm256_set_ps(h, g, f, e, d, c, b, a); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat8(__m256 a) + { + m = a; + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE float lane() const + { + #if !defined(__clang__) && defined(_MSC_VER) + return m.m256_f32[l]; + #else + union { __m256 m; float f[8]; } cvt; + cvt.m = m; + return cvt.f[l]; + #endif + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vfloat8 zero() + { + return vfloat8(_mm256_setzero_ps()); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vfloat8 load1(const float* p) + { + return vfloat8(_mm256_broadcast_ss(p)); + } + + /** + * @brief Factory that returns a vector loaded from 32B aligned memory. + */ + static ASTCENC_SIMD_INLINE vfloat8 loada(const float* p) + { + return vfloat8(_mm256_load_ps(p)); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vfloat8 lane_id() + { + return vfloat8(_mm256_set_ps(7, 6, 5, 4, 3, 2, 1, 0)); + } + + /** + * @brief The vector ... + */ + __m256 m; +}; + +// ============================================================================ +// vint8 data type +// ============================================================================ + +/** + * @brief Data type for 8-wide ints. + */ +struct vint8 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vint8() = default; + + /** + * @brief Construct from 8 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vint8(const int *p) + { + m = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p)); + } + + /** + * @brief Construct from 8 uint8_t loaded from an unaligned address. + */ + ASTCENC_SIMD_INLINE explicit vint8(const uint8_t *p) + { + // _mm_loadu_si64 would be nicer syntax, but missing on older GCC + m = _mm256_cvtepu8_epi32(_mm_cvtsi64_si128(*reinterpret_cast<const long long*>(p))); + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using vfloat4::zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vint8(int a) + { + m = _mm256_set1_epi32(a); + } + + /** + * @brief Construct from 8 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint8( + int a, int b, int c, int d, + int e, int f, int g, int h) + { + m = _mm256_set_epi32(h, g, f, e, d, c, b, a); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint8(__m256i a) + { + m = a; + } + + /** + * @brief Get the scalar from a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE int lane() const + { + #if !defined(__clang__) && defined(_MSC_VER) + return m.m256i_i32[l]; + #else + union { __m256i m; int f[8]; } cvt; + cvt.m = m; + return cvt.f[l]; + #endif + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vint8 zero() + { + return vint8(_mm256_setzero_si256()); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vint8 load1(const int* p) + { + __m128i a = _mm_set1_epi32(*p); + return vint8(_mm256_broadcastd_epi32(a)); + } + + /** + * @brief Factory that returns a vector loaded from 32B aligned memory. + */ + static ASTCENC_SIMD_INLINE vint8 loada(const int* p) + { + return vint8(_mm256_load_si256(reinterpret_cast<const __m256i*>(p))); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vint8 lane_id() + { + return vint8(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)); + } + + /** + * @brief The vector ... + */ + __m256i m; +}; + +// ============================================================================ +// vmask8 data type +// ============================================================================ + +/** + * @brief Data type for 8-wide control plane masks. + */ +struct vmask8 +{ + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask8(__m256 a) + { + m = a; + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask8(__m256i a) + { + m = _mm256_castsi256_ps(a); + } + + /** + * @brief Construct from 1 scalar value. + */ + ASTCENC_SIMD_INLINE explicit vmask8(bool a) + { + vint8 mask(a == false ? 0 : -1); + m = _mm256_castsi256_ps(mask.m); + } + + /** + * @brief The vector ... + */ + __m256 m; +}; + +// ============================================================================ +// vmask8 operators and functions +// ============================================================================ + +/** + * @brief Overload: mask union (or). + */ +ASTCENC_SIMD_INLINE vmask8 operator|(vmask8 a, vmask8 b) +{ + return vmask8(_mm256_or_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask intersect (and). + */ +ASTCENC_SIMD_INLINE vmask8 operator&(vmask8 a, vmask8 b) +{ + return vmask8(_mm256_and_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask difference (xor). + */ +ASTCENC_SIMD_INLINE vmask8 operator^(vmask8 a, vmask8 b) +{ + return vmask8(_mm256_xor_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask invert (not). + */ +ASTCENC_SIMD_INLINE vmask8 operator~(vmask8 a) +{ + return vmask8(_mm256_xor_si256(_mm256_castps_si256(a.m), _mm256_set1_epi32(-1))); +} + +/** + * @brief Return a 8-bit mask code indicating mask status. + * + * bit0 = lane 0 + */ +ASTCENC_SIMD_INLINE unsigned int mask(vmask8 a) +{ + return static_cast<unsigned int>(_mm256_movemask_ps(a.m)); +} + +/** + * @brief True if any lanes are enabled, false otherwise. + */ +ASTCENC_SIMD_INLINE bool any(vmask8 a) +{ + return mask(a) != 0; +} + +/** + * @brief True if all lanes are enabled, false otherwise. + */ +ASTCENC_SIMD_INLINE bool all(vmask8 a) +{ + return mask(a) == 0xFF; +} + +// ============================================================================ +// vint8 operators and functions +// ============================================================================ +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vint8 operator+(vint8 a, vint8 b) +{ + return vint8(_mm256_add_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector incremental addition. + */ +ASTCENC_SIMD_INLINE vint8& operator+=(vint8& a, const vint8& b) +{ + a = a + b; + return a; +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vint8 operator-(vint8 a, vint8 b) +{ + return vint8(_mm256_sub_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vint8 operator*(vint8 a, vint8 b) +{ + return vint8(_mm256_mullo_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector bit invert. + */ +ASTCENC_SIMD_INLINE vint8 operator~(vint8 a) +{ + return vint8(_mm256_xor_si256(a.m, _mm256_set1_epi32(-1))); +} + +/** + * @brief Overload: vector by vector bitwise or. + */ +ASTCENC_SIMD_INLINE vint8 operator|(vint8 a, vint8 b) +{ + return vint8(_mm256_or_si256(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise and. + */ +ASTCENC_SIMD_INLINE vint8 operator&(vint8 a, vint8 b) +{ + return vint8(_mm256_and_si256(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise xor. + */ +ASTCENC_SIMD_INLINE vint8 operator^(vint8 a, vint8 b) +{ + return vint8(_mm256_xor_si256(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask8 operator==(vint8 a, vint8 b) +{ + return vmask8(_mm256_cmpeq_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask8 operator!=(vint8 a, vint8 b) +{ + return ~vmask8(_mm256_cmpeq_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask8 operator<(vint8 a, vint8 b) +{ + return vmask8(_mm256_cmpgt_epi32(b.m, a.m)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask8 operator>(vint8 a, vint8 b) +{ + return vmask8(_mm256_cmpgt_epi32(a.m, b.m)); +} + +/** + * @brief Logical shift left. + */ +template <int s> ASTCENC_SIMD_INLINE vint8 lsl(vint8 a) +{ + return vint8(_mm256_slli_epi32(a.m, s)); +} + +/** + * @brief Arithmetic shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint8 asr(vint8 a) +{ + return vint8(_mm256_srai_epi32(a.m, s)); +} + +/** + * @brief Logical shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint8 lsr(vint8 a) +{ + return vint8(_mm256_srli_epi32(a.m, s)); +} + +/** + * @brief Return the min vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint8 min(vint8 a, vint8 b) +{ + return vint8(_mm256_min_epi32(a.m, b.m)); +} + +/** + * @brief Return the max vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint8 max(vint8 a, vint8 b) +{ + return vint8(_mm256_max_epi32(a.m, b.m)); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vint8 hmin(vint8 a) +{ + __m128i m = _mm_min_epi32(_mm256_extracti128_si256(a.m, 0), _mm256_extracti128_si256(a.m, 1)); + m = _mm_min_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,3,2))); + m = _mm_min_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,1))); + m = _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,0)); + + __m256i r = astcenc_mm256_set_m128i(m, m); + vint8 vmin(r); + return vmin; +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vint8 hmax(vint8 a) +{ + __m128i m = _mm_max_epi32(_mm256_extracti128_si256(a.m, 0), _mm256_extracti128_si256(a.m, 1)); + m = _mm_max_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,3,2))); + m = _mm_max_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,1))); + m = _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,0)); + + __m256i r = astcenc_mm256_set_m128i(m, m); + vint8 vmax(r); + return vmax; +} + +/** + * @brief Store a vector to a 16B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vint8 a, int* p) +{ + _mm256_store_si256(reinterpret_cast<__m256i*>(p), a.m); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vint8 a, int* p) +{ + _mm256_storeu_si256(reinterpret_cast<__m256i*>(p), a.m); +} + +/** + * @brief Store lowest N (vector width) bytes into an unaligned address. + */ +ASTCENC_SIMD_INLINE void store_nbytes(vint8 a, uint8_t* p) +{ + // This is the most logical implementation, but the convenience intrinsic + // is missing on older compilers (supported in g++ 9 and clang++ 9). + // _mm_storeu_si64(ptr, _mm256_extracti128_si256(v.m, 0)) + _mm_storel_epi64(reinterpret_cast<__m128i*>(p), _mm256_extracti128_si256(a.m, 0)); +} + +/** + * @brief Gather N (vector width) indices from the array. + */ +ASTCENC_SIMD_INLINE vint8 gatheri(const int* base, vint8 indices) +{ + return vint8(_mm256_i32gather_epi32(base, indices.m, 4)); +} + +/** + * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector. + */ +ASTCENC_SIMD_INLINE vint8 pack_low_bytes(vint8 v) +{ + __m256i shuf = _mm256_set_epi8(0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 28, 24, 20, 16, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 12, 8, 4, 0); + __m256i a = _mm256_shuffle_epi8(v.m, shuf); + __m128i a0 = _mm256_extracti128_si256(a, 0); + __m128i a1 = _mm256_extracti128_si256(a, 1); + __m128i b = _mm_unpacklo_epi32(a0, a1); + + __m256i r = astcenc_mm256_set_m128i(b, b); + return vint8(r); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vint8 select(vint8 a, vint8 b, vmask8 cond) +{ + __m256i condi = _mm256_castps_si256(cond.m); + return vint8(_mm256_blendv_epi8(a.m, b.m, condi)); +} + +// ============================================================================ +// vfloat4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vfloat8 operator+(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_add_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector incremental addition. + */ +ASTCENC_SIMD_INLINE vfloat8& operator+=(vfloat8& a, const vfloat8& b) +{ + a = a + b; + return a; +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vfloat8 operator-(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_sub_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_mul_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by scalar multiplication. + */ +ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, float b) +{ + return vfloat8(_mm256_mul_ps(a.m, _mm256_set1_ps(b))); +} + +/** + * @brief Overload: scalar by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat8 operator*(float a, vfloat8 b) +{ + return vfloat8(_mm256_mul_ps(_mm256_set1_ps(a), b.m)); +} + +/** + * @brief Overload: vector by vector division. + */ +ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_div_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by scalar division. + */ +ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, float b) +{ + return vfloat8(_mm256_div_ps(a.m, _mm256_set1_ps(b))); +} + + +/** + * @brief Overload: scalar by vector division. + */ +ASTCENC_SIMD_INLINE vfloat8 operator/(float a, vfloat8 b) +{ + return vfloat8(_mm256_div_ps(_mm256_set1_ps(a), b.m)); +} + + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask8 operator==(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_EQ_OQ)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask8 operator!=(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_NEQ_OQ)); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask8 operator<(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LT_OQ)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask8 operator>(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GT_OQ)); +} + +/** + * @brief Overload: vector by vector less than or equal. + */ +ASTCENC_SIMD_INLINE vmask8 operator<=(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LE_OQ)); +} + +/** + * @brief Overload: vector by vector greater than or equal. + */ +ASTCENC_SIMD_INLINE vmask8 operator>=(vfloat8 a, vfloat8 b) +{ + return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GE_OQ)); +} + +/** + * @brief Return the min vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_min_ps(a.m, b.m)); +} + +/** + * @brief Return the min vector of a vector and a scalar. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, float b) +{ + return min(a, vfloat8(b)); +} + +/** + * @brief Return the max vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, vfloat8 b) +{ + return vfloat8(_mm256_max_ps(a.m, b.m)); +} + +/** + * @brief Return the max vector of a vector and a scalar. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, float b) +{ + return max(a, vfloat8(b)); +} + +/** + * @brief Return the clamped value between min and max. + * + * It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN + * then @c min will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 clamp(float min, float max, vfloat8 a) +{ + // Do not reorder - second operand will return if either is NaN + a.m = _mm256_max_ps(a.m, _mm256_set1_ps(min)); + a.m = _mm256_min_ps(a.m, _mm256_set1_ps(max)); + return a; +} + +/** + * @brief Return a clamped value between 0.0f and max. + * + * It is assumed that @c max is not a NaN value. If @c a is NaN then zero will + * be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 clampz(float max, vfloat8 a) +{ + a.m = _mm256_max_ps(a.m, _mm256_setzero_ps()); + a.m = _mm256_min_ps(a.m, _mm256_set1_ps(max)); + return a; +} + +/** + * @brief Return a clamped value between 0.0f and 1.0f. + * + * If @c a is NaN then zero will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat8 clampzo(vfloat8 a) +{ + a.m = _mm256_max_ps(a.m, _mm256_setzero_ps()); + a.m = _mm256_min_ps(a.m, _mm256_set1_ps(1.0f)); + return a; +} + +/** + * @brief Return the absolute value of the float vector. + */ +ASTCENC_SIMD_INLINE vfloat8 abs(vfloat8 a) +{ + __m256 msk = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff)); + return vfloat8(_mm256_and_ps(a.m, msk)); +} + +/** + * @brief Return a float rounded to the nearest integer value. + */ +ASTCENC_SIMD_INLINE vfloat8 round(vfloat8 a) +{ + constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC; + return vfloat8(_mm256_round_ps(a.m, flags)); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat8 hmin(vfloat8 a) +{ + __m128 vlow = _mm256_castps256_ps128(a.m); + __m128 vhigh = _mm256_extractf128_ps(a.m, 1); + vlow = _mm_min_ps(vlow, vhigh); + + // First do an horizontal reduction. + __m128 shuf = _mm_shuffle_ps(vlow, vlow, _MM_SHUFFLE(2, 3, 0, 1)); + __m128 mins = _mm_min_ps(vlow, shuf); + shuf = _mm_movehl_ps(shuf, mins); + mins = _mm_min_ss(mins, shuf); + + // This is the most logical implementation, but the convenience intrinsic + // is missing on older compilers (supported in g++ 9 and clang++ 9). + //__m256i r = _mm256_set_m128(m, m) + __m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(mins), mins, 1); + + return vfloat8(_mm256_permute_ps(r, 0)); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE float hmin_s(vfloat8 a) +{ + return hmin(a).lane<0>(); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat8 hmax(vfloat8 a) +{ + __m128 vlow = _mm256_castps256_ps128(a.m); + __m128 vhigh = _mm256_extractf128_ps(a.m, 1); + vhigh = _mm_max_ps(vlow, vhigh); + + // First do an horizontal reduction. + __m128 shuf = _mm_shuffle_ps(vhigh, vhigh, _MM_SHUFFLE(2, 3, 0, 1)); + __m128 maxs = _mm_max_ps(vhigh, shuf); + shuf = _mm_movehl_ps(shuf,maxs); + maxs = _mm_max_ss(maxs, shuf); + + // This is the most logical implementation, but the convenience intrinsic + // is missing on older compilers (supported in g++ 9 and clang++ 9). + //__m256i r = _mm256_set_m128(m, m) + __m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(maxs), maxs, 1); + return vfloat8(_mm256_permute_ps(r, 0)); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE float hmax_s(vfloat8 a) +{ + return hmax(a).lane<0>(); +} + +/** + * @brief Return the horizontal sum of a vector. + */ +ASTCENC_SIMD_INLINE float hadd_s(vfloat8 a) +{ + // Two sequential 4-wide adds gives invariance with 4-wide code + vfloat4 lo(_mm256_extractf128_ps(a.m, 0)); + vfloat4 hi(_mm256_extractf128_ps(a.m, 1)); + return hadd_s(lo) + hadd_s(hi); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat8 select(vfloat8 a, vfloat8 b, vmask8 cond) +{ + return vfloat8(_mm256_blendv_ps(a.m, b.m, cond.m)); +} + +/** + * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat8 select_msb(vfloat8 a, vfloat8 b, vmask8 cond) +{ + return vfloat8(_mm256_blendv_ps(a.m, b.m, cond.m)); +} + +/** + * @brief Accumulate lane-wise sums for a vector, folded 4-wide. + * + * This is invariant with 4-wide implementations. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a) +{ + vfloat4 lo(_mm256_extractf128_ps(a.m, 0)); + haccumulate(accum, lo); + + vfloat4 hi(_mm256_extractf128_ps(a.m, 1)); + haccumulate(accum, hi); +} + +/** + * @brief Accumulate lane-wise sums for a vector. + * + * This is NOT invariant with 4-wide implementations. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a) +{ + accum += a; +} + +/** + * @brief Accumulate masked lane-wise sums for a vector, folded 4-wide. + * + * This is invariant with 4-wide implementations. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a, vmask8 m) +{ + a = select(vfloat8::zero(), a, m); + haccumulate(accum, a); +} + +/** + * @brief Accumulate masked lane-wise sums for a vector. + * + * This is NOT invariant with 4-wide implementations. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a, vmask8 m) +{ + a = select(vfloat8::zero(), a, m); + haccumulate(accum, a); +} + +/** + * @brief Return the sqrt of the lanes in the vector. + */ +ASTCENC_SIMD_INLINE vfloat8 sqrt(vfloat8 a) +{ + return vfloat8(_mm256_sqrt_ps(a.m)); +} + +/** + * @brief Load a vector of gathered results from an array; + */ +ASTCENC_SIMD_INLINE vfloat8 gatherf(const float* base, vint8 indices) +{ + return vfloat8(_mm256_i32gather_ps(base, indices.m, 4)); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vfloat8 a, float* p) +{ + _mm256_storeu_ps(p, a.m); +} + +/** + * @brief Store a vector to a 32B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vfloat8 a, float* p) +{ + _mm256_store_ps(p, a.m); +} + +/** + * @brief Return a integer value for a float vector, using truncation. + */ +ASTCENC_SIMD_INLINE vint8 float_to_int(vfloat8 a) +{ + return vint8(_mm256_cvttps_epi32(a.m)); +} + +/** + * @brief Return a integer value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint8 float_to_int_rtn(vfloat8 a) +{ + a = round(a); + return vint8(_mm256_cvttps_epi32(a.m)); +} + + +/** + * @brief Return a float value for an integer vector. + */ +ASTCENC_SIMD_INLINE vfloat8 int_to_float(vint8 a) +{ + return vfloat8(_mm256_cvtepi32_ps(a.m)); +} + +/** + * @brief Return a float value as an integer bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the first half of that flip. + */ +ASTCENC_SIMD_INLINE vint8 float_as_int(vfloat8 a) +{ + return vint8(_mm256_castps_si256(a.m)); +} + +/** + * @brief Return a integer value as a float bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the second half of that flip. + */ +ASTCENC_SIMD_INLINE vfloat8 int_as_float(vint8 a) +{ + return vfloat8(_mm256_castsi256_ps(a.m)); +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint8& t0p) +{ + // AVX2 duplicates the table within each 128-bit lane + __m128i t0n = t0.m; + t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n)); +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint8& t0p, vint8& t1p) +{ + // AVX2 duplicates the table within each 128-bit lane + __m128i t0n = t0.m; + t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n)); + + __m128i t1n = _mm_xor_si128(t0.m, t1.m); + t1p = vint8(astcenc_mm256_set_m128i(t1n, t1n)); +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare( + vint4 t0, vint4 t1, vint4 t2, vint4 t3, + vint8& t0p, vint8& t1p, vint8& t2p, vint8& t3p) +{ + // AVX2 duplicates the table within each 128-bit lane + __m128i t0n = t0.m; + t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n)); + + __m128i t1n = _mm_xor_si128(t0.m, t1.m); + t1p = vint8(astcenc_mm256_set_m128i(t1n, t1n)); + + __m128i t2n = _mm_xor_si128(t1.m, t2.m); + t2p = vint8(astcenc_mm256_set_m128i(t2n, t2n)); + + __m128i t3n = _mm_xor_si128(t2.m, t3.m); + t3p = vint8(astcenc_mm256_set_m128i(t3n, t3n)); +} + +/** + * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 idx) +{ + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m256i result = _mm256_shuffle_epi8(t0.m, idxx); + return vint8(result); +} + +/** + * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 t1, vint8 idx) +{ + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m256i result = _mm256_shuffle_epi8(t0.m, idxx); + idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16)); + + __m256i result2 = _mm256_shuffle_epi8(t1.m, idxx); + result = _mm256_xor_si256(result, result2); + return vint8(result); +} + +/** + * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 t1, vint8 t2, vint8 t3, vint8 idx) +{ + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m256i result = _mm256_shuffle_epi8(t0.m, idxx); + idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16)); + + __m256i result2 = _mm256_shuffle_epi8(t1.m, idxx); + result = _mm256_xor_si256(result, result2); + idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16)); + + result2 = _mm256_shuffle_epi8(t2.m, idxx); + result = _mm256_xor_si256(result, result2); + idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16)); + + result2 = _mm256_shuffle_epi8(t3.m, idxx); + result = _mm256_xor_si256(result, result2); + + return vint8(result); +} + +/** + * @brief Return a vector of interleaved RGBA data. + * + * Input vectors have the value stored in the bottom 8 bits of each lane, + * with high bits set to zero. + * + * Output vector stores a single RGBA texel packed in each lane. + */ +ASTCENC_SIMD_INLINE vint8 interleave_rgba8(vint8 r, vint8 g, vint8 b, vint8 a) +{ + return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a); +} + +/** + * @brief Store a vector, skipping masked lanes. + * + * All masked lanes must be at the end of vector, after all non-masked lanes. + */ +ASTCENC_SIMD_INLINE void store_lanes_masked(int* base, vint8 data, vmask8 mask) +{ + _mm256_maskstore_epi32(base, _mm256_castps_si256(mask.m), data.m); +} + +/** + * @brief Debug function to print a vector of ints. + */ +ASTCENC_SIMD_INLINE void print(vint8 a) +{ + alignas(ASTCENC_VECALIGN) int v[8]; + storea(a, v); + printf("v8_i32:\n %8d %8d %8d %8d %8d %8d %8d %8d\n", + v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); +} + +/** + * @brief Debug function to print a vector of ints. + */ +ASTCENC_SIMD_INLINE void printx(vint8 a) +{ + alignas(ASTCENC_VECALIGN) int v[8]; + storea(a, v); + printf("v8_i32:\n %08x %08x %08x %08x %08x %08x %08x %08x\n", + v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); +} + +/** + * @brief Debug function to print a vector of floats. + */ +ASTCENC_SIMD_INLINE void print(vfloat8 a) +{ + alignas(ASTCENC_VECALIGN) float v[8]; + storea(a, v); + printf("v8_f32:\n %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n", + static_cast<double>(v[0]), static_cast<double>(v[1]), + static_cast<double>(v[2]), static_cast<double>(v[3]), + static_cast<double>(v[4]), static_cast<double>(v[5]), + static_cast<double>(v[6]), static_cast<double>(v[7])); +} + +/** + * @brief Debug function to print a vector of masks. + */ +ASTCENC_SIMD_INLINE void print(vmask8 a) +{ + print(select(vint8(0), vint8(1), a)); +} + +#endif // #ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_common_4.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_common_4.h new file mode 100644 index 00000000000..86ee4fd3e1f --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_common_4.h @@ -0,0 +1,423 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2020-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Generic 4x32-bit vector functions. + * + * This module implements generic 4-wide vector functions that are valid for + * all instruction sets, typically implemented using lower level 4-wide + * operations that are ISA-specific. + */ + +#ifndef ASTC_VECMATHLIB_COMMON_4_H_INCLUDED +#define ASTC_VECMATHLIB_COMMON_4_H_INCLUDED + +#ifndef ASTCENC_SIMD_INLINE + #error "Include astcenc_vecmathlib.h, do not include directly" +#endif + +#include <cstdio> + +// ============================================================================ +// vmask4 operators and functions +// ============================================================================ + +/** + * @brief True if any lanes are enabled, false otherwise. + */ +ASTCENC_SIMD_INLINE bool any(vmask4 a) +{ + return mask(a) != 0; +} + +/** + * @brief True if all lanes are enabled, false otherwise. + */ +ASTCENC_SIMD_INLINE bool all(vmask4 a) +{ + return mask(a) == 0xF; +} + +// ============================================================================ +// vint4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by scalar addition. + */ +ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, int b) +{ + return a + vint4(b); +} + +/** + * @brief Overload: vector by vector incremental addition. + */ +ASTCENC_SIMD_INLINE vint4& operator+=(vint4& a, const vint4& b) +{ + a = a + b; + return a; +} + +/** + * @brief Overload: vector by scalar subtraction. + */ +ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, int b) +{ + return a - vint4(b); +} + +/** + * @brief Overload: vector by scalar multiplication. + */ +ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, int b) +{ + return a * vint4(b); +} + +/** + * @brief Overload: vector by scalar bitwise or. + */ +ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, int b) +{ + return a | vint4(b); +} + +/** + * @brief Overload: vector by scalar bitwise and. + */ +ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, int b) +{ + return a & vint4(b); +} + +/** + * @brief Overload: vector by scalar bitwise xor. + */ +ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, int b) +{ + return a ^ vint4(b); +} + +/** + * @brief Return the clamped value between min and max. + */ +ASTCENC_SIMD_INLINE vint4 clamp(int minv, int maxv, vint4 a) +{ + return min(max(a, vint4(minv)), vint4(maxv)); +} + +/** + * @brief Return the horizontal sum of RGB vector lanes as a scalar. + */ +ASTCENC_SIMD_INLINE int hadd_rgb_s(vint4 a) +{ + return a.lane<0>() + a.lane<1>() + a.lane<2>(); +} + +// ============================================================================ +// vfloat4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector incremental addition. + */ +ASTCENC_SIMD_INLINE vfloat4& operator+=(vfloat4& a, const vfloat4& b) +{ + a = a + b; + return a; +} + +/** + * @brief Overload: vector by scalar addition. + */ +ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, float b) +{ + return a + vfloat4(b); +} + +/** + * @brief Overload: vector by scalar subtraction. + */ +ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, float b) +{ + return a - vfloat4(b); +} + +/** + * @brief Overload: vector by scalar multiplication. + */ +ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, float b) +{ + return a * vfloat4(b); +} + +/** + * @brief Overload: scalar by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat4 operator*(float a, vfloat4 b) +{ + return vfloat4(a) * b; +} + +/** + * @brief Overload: vector by scalar division. + */ +ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, float b) +{ + return a / vfloat4(b); +} + +/** + * @brief Overload: scalar by vector division. + */ +ASTCENC_SIMD_INLINE vfloat4 operator/(float a, vfloat4 b) +{ + return vfloat4(a) / b; +} + +/** + * @brief Return the min vector of a vector and a scalar. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, float b) +{ + return min(a, vfloat4(b)); +} + +/** + * @brief Return the max vector of a vector and a scalar. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, float b) +{ + return max(a, vfloat4(b)); +} + +/** + * @brief Return the clamped value between min and max. + * + * It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN + * then @c min will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 clamp(float minv, float maxv, vfloat4 a) +{ + // Do not reorder - second operand will return if either is NaN + return min(max(a, minv), maxv); +} + +/** + * @brief Return the clamped value between 0.0f and max. + * + * It is assumed that @c max is not a NaN value. If @c a is NaN then zero will + * be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 clampz(float maxv, vfloat4 a) +{ + // Do not reorder - second operand will return if either is NaN + return min(max(a, vfloat4::zero()), maxv); +} + +/** + * @brief Return the clamped value between 0.0f and 1.0f. + * + * If @c a is NaN then zero will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 clampzo(vfloat4 a) +{ + // Do not reorder - second operand will return if either is NaN + return min(max(a, vfloat4::zero()), 1.0f); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE float hmin_s(vfloat4 a) +{ + return hmin(a).lane<0>(); +} + +/** + * @brief Return the horizontal min of RGB vector lanes as a scalar. + */ +ASTCENC_SIMD_INLINE float hmin_rgb_s(vfloat4 a) +{ + a.set_lane<3>(a.lane<0>()); + return hmin_s(a); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE float hmax_s(vfloat4 a) +{ + return hmax(a).lane<0>(); +} + +/** + * @brief Accumulate lane-wise sums for a vector. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat4 a) +{ + accum = accum + a; +} + +/** + * @brief Accumulate lane-wise sums for a masked vector. + */ +ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat4 a, vmask4 m) +{ + a = select(vfloat4::zero(), a, m); + haccumulate(accum, a); +} + +/** + * @brief Return the horizontal sum of RGB vector lanes as a scalar. + */ +ASTCENC_SIMD_INLINE float hadd_rgb_s(vfloat4 a) +{ + return a.lane<0>() + a.lane<1>() + a.lane<2>(); +} + +#if !defined(ASTCENC_USE_NATIVE_DOT_PRODUCT) + +/** + * @brief Return the dot product for the full 4 lanes, returning scalar. + */ +ASTCENC_SIMD_INLINE float dot_s(vfloat4 a, vfloat4 b) +{ + vfloat4 m = a * b; + return hadd_s(m); +} + +/** + * @brief Return the dot product for the full 4 lanes, returning vector. + */ +ASTCENC_SIMD_INLINE vfloat4 dot(vfloat4 a, vfloat4 b) +{ + vfloat4 m = a * b; + return vfloat4(hadd_s(m)); +} + +/** + * @brief Return the dot product for the bottom 3 lanes, returning scalar. + */ +ASTCENC_SIMD_INLINE float dot3_s(vfloat4 a, vfloat4 b) +{ + vfloat4 m = a * b; + return hadd_rgb_s(m); +} + +/** + * @brief Return the dot product for the bottom 3 lanes, returning vector. + */ +ASTCENC_SIMD_INLINE vfloat4 dot3(vfloat4 a, vfloat4 b) +{ + vfloat4 m = a * b; + float d3 = hadd_rgb_s(m); + return vfloat4(d3, d3, d3, 0.0f); +} + +#endif + +#if !defined(ASTCENC_USE_NATIVE_POPCOUNT) + +/** + * @brief Population bit count. + * + * @param v The value to population count. + * + * @return The number of 1 bits. + */ +static inline int popcount(uint64_t v) +{ + uint64_t mask1 = 0x5555555555555555ULL; + uint64_t mask2 = 0x3333333333333333ULL; + uint64_t mask3 = 0x0F0F0F0F0F0F0F0FULL; + v -= (v >> 1) & mask1; + v = (v & mask2) + ((v >> 2) & mask2); + v += v >> 4; + v &= mask3; + v *= 0x0101010101010101ULL; + v >>= 56; + return static_cast<int>(v); +} + +#endif + +/** + * @brief Apply signed bit transfer. + * + * @param input0 The first encoded endpoint. + * @param input1 The second encoded endpoint. + */ +static ASTCENC_SIMD_INLINE void bit_transfer_signed( + vint4& input0, + vint4& input1 +) { + input1 = lsr<1>(input1) | (input0 & 0x80); + input0 = lsr<1>(input0) & 0x3F; + + vmask4 mask = (input0 & 0x20) != vint4::zero(); + input0 = select(input0, input0 - 0x40, mask); +} + +/** + * @brief Debug function to print a vector of ints. + */ +ASTCENC_SIMD_INLINE void print(vint4 a) +{ + alignas(16) int v[4]; + storea(a, v); + printf("v4_i32:\n %8d %8d %8d %8d\n", + v[0], v[1], v[2], v[3]); +} + +/** + * @brief Debug function to print a vector of ints. + */ +ASTCENC_SIMD_INLINE void printx(vint4 a) +{ + alignas(16) int v[4]; + storea(a, v); + printf("v4_i32:\n %08x %08x %08x %08x\n", + v[0], v[1], v[2], v[3]); +} + +/** + * @brief Debug function to print a vector of floats. + */ +ASTCENC_SIMD_INLINE void print(vfloat4 a) +{ + alignas(16) float v[4]; + storea(a, v); + printf("v4_f32:\n %0.4f %0.4f %0.4f %0.4f\n", + static_cast<double>(v[0]), static_cast<double>(v[1]), + static_cast<double>(v[2]), static_cast<double>(v[3])); +} + +/** + * @brief Debug function to print a vector of masks. + */ +ASTCENC_SIMD_INLINE void print(vmask4 a) +{ + print(select(vint4(0), vint4(1), a)); +} + +#endif // #ifndef ASTC_VECMATHLIB_COMMON_4_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_neon_4.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_neon_4.h new file mode 100644 index 00000000000..e668850f88f --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_neon_4.h @@ -0,0 +1,1073 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2019-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief 4x32-bit vectors, implemented using Armv8-A NEON. + * + * This module implements 4-wide 32-bit float, int, and mask vectors for + * Armv8-A NEON. + * + * There is a baseline level of functionality provided by all vector widths and + * implementations. This is implemented using identical function signatures, + * modulo data type, so we can use them as substitutable implementations in VLA + * code. + * + * The 4-wide vectors are also used as a fixed-width type, and significantly + * extend the functionality above that available to VLA code. + */ + +#ifndef ASTC_VECMATHLIB_NEON_4_H_INCLUDED +#define ASTC_VECMATHLIB_NEON_4_H_INCLUDED + +#ifndef ASTCENC_SIMD_INLINE + #error "Include astcenc_vecmathlib.h, do not include directly" +#endif + +#include <cstdio> + +// ============================================================================ +// vfloat4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide floats. + */ +struct vfloat4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vfloat4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(const float *p) + { + m = vld1q_f32(p); + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a) + { + m = vdupq_n_f32(a); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d) + { + float v[4] { a, b, c, d }; + m = vld1q_f32(v); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float32x4_t a) + { + m = a; + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE float lane() const + { + return vgetq_lane_f32(m, l); + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(float a) + { + m = vld1q_lane_f32(&a, m, l); + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vfloat4 zero() + { + return vfloat4(vdupq_n_f32(0.0f)); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p) + { + return vfloat4(vdupq_n_f32(*p)); + } + + /** + * @brief Factory that returns a vector loaded from 16B aligned memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p) + { + return vfloat4(vld1q_f32(p)); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vfloat4 lane_id() + { + alignas(16) float data[4] { 0.0f, 1.0f, 2.0f, 3.0f }; + return vfloat4(vld1q_f32(data)); + } + + /** + * @brief Return a swizzled float 2. + */ + template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), 0.0f, 0.0f); + } + + /** + * @brief Return a swizzled float 3. + */ + template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), 0.0f); + } + + /** + * @brief Return a swizzled float 4. + */ + template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), lane<l3>()); + } + + /** + * @brief The vector ... + */ + float32x4_t m; +}; + +// ============================================================================ +// vint4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide ints. + */ +struct vint4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vint4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vint4(const int *p) + { + m = vld1q_s32(p); + } + + /** + * @brief Construct from 4 uint8_t loaded from an unaligned address. + */ + ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p) + { + uint32x2_t t8 {}; + // Cast is safe - NEON loads are allowed to be unaligned + t8 = vld1_lane_u32(reinterpret_cast<const uint32_t*>(p), t8, 0); + uint16x4_t t16 = vget_low_u16(vmovl_u8(vreinterpret_u8_u32(t8))); + m = vreinterpretq_s32_u32(vmovl_u16(t16)); + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using vfloat4::zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a) + { + m = vdupq_n_s32(a); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d) + { + int v[4] { a, b, c, d }; + m = vld1q_s32(v); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint4(int32x4_t a) + { + m = a; + } + + /** + * @brief Get the scalar from a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE int lane() const + { + return vgetq_lane_s32(m, l); + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(int a) + { + m = vld1q_lane_s32(&a, m, l); + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vint4 zero() + { + return vint4(0); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vint4 load1(const int* p) + { + return vint4(*p); + } + + /** + * @brief Factory that returns a vector loaded from 16B aligned memory. + */ + static ASTCENC_SIMD_INLINE vint4 loada(const int* p) + { + return vint4(p); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vint4 lane_id() + { + alignas(16) static const int data[4] { 0, 1, 2, 3 }; + return vint4(vld1q_s32(data)); + } + + /** + * @brief The vector ... + */ + int32x4_t m; +}; + +// ============================================================================ +// vmask4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide control plane masks. + */ +struct vmask4 +{ + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(uint32x4_t a) + { + m = a; + } + +#if !defined(_MSC_VER) + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(int32x4_t a) + { + m = vreinterpretq_u32_s32(a); + } +#endif + + /** + * @brief Construct from 1 scalar value. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a) + { + m = vreinterpretq_u32_s32(vdupq_n_s32(a == true ? -1 : 0)); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d) + { + int v[4] { + a == true ? -1 : 0, + b == true ? -1 : 0, + c == true ? -1 : 0, + d == true ? -1 : 0 + }; + + int32x4_t ms = vld1q_s32(v); + m = vreinterpretq_u32_s32(ms); + } + + /** + * @brief Get the scalar from a single lane. + */ + template <int32_t l> ASTCENC_SIMD_INLINE uint32_t lane() const + { + return vgetq_lane_u32(m, l); + } + + /** + * @brief The vector ... + */ + uint32x4_t m; +}; + +// ============================================================================ +// vmask4 operators and functions +// ============================================================================ + +/** + * @brief Overload: mask union (or). + */ +ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b) +{ + return vmask4(vorrq_u32(a.m, b.m)); +} + +/** + * @brief Overload: mask intersect (and). + */ +ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b) +{ + return vmask4(vandq_u32(a.m, b.m)); +} + +/** + * @brief Overload: mask difference (xor). + */ +ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b) +{ + return vmask4(veorq_u32(a.m, b.m)); +} + +/** + * @brief Overload: mask invert (not). + */ +ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a) +{ + return vmask4(vmvnq_u32(a.m)); +} + +/** + * @brief Return a 4-bit mask code indicating mask status. + * + * bit0 = lane 0 + */ +ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a) +{ + static const int shifta[4] { 0, 1, 2, 3 }; + static const int32x4_t shift = vld1q_s32(shifta); + + uint32x4_t tmp = vshrq_n_u32(a.m, 31); + return vaddvq_u32(vshlq_u32(tmp, shift)); +} + +// ============================================================================ +// vint4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b) +{ + return vint4(vaddq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b) +{ + return vint4(vsubq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b) +{ + return vint4(vmulq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector bit invert. + */ +ASTCENC_SIMD_INLINE vint4 operator~(vint4 a) +{ + return vint4(vmvnq_s32(a.m)); +} + +/** + * @brief Overload: vector by vector bitwise or. + */ +ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b) +{ + return vint4(vorrq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise and. + */ +ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b) +{ + return vint4(vandq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise xor. + */ +ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b) +{ + return vint4(veorq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b) +{ + return vmask4(vceqq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b) +{ + return ~vmask4(vceqq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b) +{ + return vmask4(vcltq_s32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b) +{ + return vmask4(vcgtq_s32(a.m, b.m)); +} + +/** + * @brief Logical shift left. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a) +{ + return vint4(vshlq_s32(a.m, vdupq_n_s32(s))); +} + +/** + * @brief Logical shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a) +{ + uint32x4_t ua = vreinterpretq_u32_s32(a.m); + ua = vshlq_u32(ua, vdupq_n_s32(-s)); + return vint4(vreinterpretq_s32_u32(ua)); +} + +/** + * @brief Arithmetic shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a) +{ + return vint4(vshlq_s32(a.m, vdupq_n_s32(-s))); +} + +/** + * @brief Return the min vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b) +{ + return vint4(vminq_s32(a.m, b.m)); +} + +/** + * @brief Return the max vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b) +{ + return vint4(vmaxq_s32(a.m, b.m)); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vint4 hmin(vint4 a) +{ + return vint4(vminvq_s32(a.m)); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vint4 hmax(vint4 a) +{ + return vint4(vmaxvq_s32(a.m)); +} + +/** + * @brief Return the horizontal sum of a vector. + */ +ASTCENC_SIMD_INLINE int hadd_s(vint4 a) +{ + int32x2_t t = vadd_s32(vget_high_s32(a.m), vget_low_s32(a.m)); + return vget_lane_s32(vpadd_s32(t, t), 0); +} + +/** + * @brief Store a vector to a 16B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vint4 a, int* p) +{ + vst1q_s32(p, a.m); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vint4 a, int* p) +{ + vst1q_s32(p, a.m); +} + +/** + * @brief Store lowest N (vector width) bytes into an unaligned address. + */ +ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p) +{ + vst1q_lane_s32(reinterpret_cast<int32_t*>(p), a.m, 0); +} + +/** + * @brief Gather N (vector width) indices from the array. + */ +ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices) +{ + alignas(16) int idx[4]; + storea(indices, idx); + alignas(16) int vals[4]; + vals[0] = base[idx[0]]; + vals[1] = base[idx[1]]; + vals[2] = base[idx[2]]; + vals[3] = base[idx[3]]; + return vint4(vals); +} + +/** + * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector. + */ +ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a) +{ + alignas(16) uint8_t shuf[16] { + 0, 4, 8, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + uint8x16_t idx = vld1q_u8(shuf); + int8x16_t av = vreinterpretq_s8_s32(a.m); + return vint4(vreinterpretq_s32_s8(vqtbl1q_s8(av, idx))); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond) +{ + return vint4(vbslq_s32(cond.m, b.m, a.m)); +} + +// ============================================================================ +// vfloat4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b) +{ + return vfloat4(vaddq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b) +{ + return vfloat4(vsubq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b) +{ + return vfloat4(vmulq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector division. + */ +ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b) +{ + return vfloat4(vdivq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b) +{ + return vmask4(vceqq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b) +{ + return vmask4(vmvnq_u32(vceqq_f32(a.m, b.m))); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b) +{ + return vmask4(vcltq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b) +{ + return vmask4(vcgtq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b) +{ + return vmask4(vcleq_f32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b) +{ + return vmask4(vcgeq_f32(a.m, b.m)); +} + +/** + * @brief Return the min vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b) +{ + // Do not reorder - second operand will return if either is NaN + return vfloat4(vminnmq_f32(a.m, b.m)); +} + +/** + * @brief Return the max vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b) +{ + // Do not reorder - second operand will return if either is NaN + return vfloat4(vmaxnmq_f32(a.m, b.m)); +} + +/** + * @brief Return the absolute value of the float vector. + */ +ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a) +{ + float32x4_t zero = vdupq_n_f32(0.0f); + float32x4_t inv = vsubq_f32(zero, a.m); + return vfloat4(vmaxq_f32(a.m, inv)); +} + +/** + * @brief Return a float rounded to the nearest integer value. + */ +ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a) +{ + return vfloat4(vrndnq_f32(a.m)); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a) +{ + return vfloat4(vminvq_f32(a.m)); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a) +{ + return vfloat4(vmaxvq_f32(a.m)); +} + +/** + * @brief Return the horizontal sum of a vector. + */ +ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a) +{ + // Perform halving add to ensure invariance; we cannot use vaddqv as this + // does (0 + 1 + 2 + 3) which is not invariant with x86 (0 + 2) + (1 + 3). + float32x2_t t = vadd_f32(vget_high_f32(a.m), vget_low_f32(a.m)); + return vget_lane_f32(vpadd_f32(t, t), 0); +} + +/** + * @brief Return the sqrt of the lanes in the vector. + */ +ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a) +{ + return vfloat4(vsqrtq_f32(a.m)); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond) +{ + return vfloat4(vbslq_f32(cond.m, b.m, a.m)); +} + +/** + * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond) +{ + static const uint32x4_t msb = vdupq_n_u32(0x80000000u); + uint32x4_t mask = vcgeq_u32(cond.m, msb); + return vfloat4(vbslq_f32(mask, b.m, a.m)); +} + +/** + * @brief Load a vector of gathered results from an array; + */ +ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices) +{ + alignas(16) int idx[4]; + storea(indices, idx); + alignas(16) float vals[4]; + vals[0] = base[idx[0]]; + vals[1] = base[idx[1]]; + vals[2] = base[idx[2]]; + vals[3] = base[idx[3]]; + return vfloat4(vals); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p) +{ + vst1q_f32(p, a.m); +} + +/** + * @brief Store a vector to a 16B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p) +{ + vst1q_f32(p, a.m); +} + +/** + * @brief Return a integer value for a float vector, using truncation. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a) +{ + return vint4(vcvtq_s32_f32(a.m)); +} + +/** + * @brief Return a integer value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a) +{ + a = round(a); + return vint4(vcvtq_s32_f32(a.m)); +} + +/** + * @brief Return a float value for an integer vector. + */ +ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a) +{ + return vfloat4(vcvtq_f32_s32(a.m)); +} + +/** + * @brief Return a float16 value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a) +{ + // Generate float16 value + float16x4_t f16 = vcvt_f16_f32(a.m); + + // Convert each 16-bit float pattern to a 32-bit pattern + uint16x4_t u16 = vreinterpret_u16_f16(f16); + uint32x4_t u32 = vmovl_u16(u16); + return vint4(vreinterpretq_s32_u32(u32)); +} + +/** + * @brief Return a float16 value for a float scalar, using round-to-nearest. + */ +static inline uint16_t float_to_float16(float a) +{ + vfloat4 av(a); + return static_cast<uint16_t>(float_to_float16(av).lane<0>()); +} + +/** + * @brief Return a float value for a float16 vector. + */ +ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a) +{ + // Convert each 32-bit float pattern to a 16-bit pattern + uint32x4_t u32 = vreinterpretq_u32_s32(a.m); + uint16x4_t u16 = vmovn_u32(u32); + float16x4_t f16 = vreinterpret_f16_u16(u16); + + // Generate float16 value + return vfloat4(vcvt_f32_f16(f16)); +} + +/** + * @brief Return a float value for a float16 scalar. + */ +ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a) +{ + vint4 av(a); + return float16_to_float(av).lane<0>(); +} + +/** + * @brief Return a float value as an integer bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the first half of that flip. + */ +ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a) +{ + return vint4(vreinterpretq_s32_f32(a.m)); +} + +/** + * @brief Return a integer value as a float bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the second half of that flip. + */ +ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v) +{ + return vfloat4(vreinterpretq_f32_s32(v.m)); +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p) +{ + t0p = t0; +} + + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p) +{ + t0p = t0; + t1p = t1; +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare( + vint4 t0, vint4 t1, vint4 t2, vint4 t3, + vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p) +{ + t0p = t0; + t1p = t1; + t2p = t2; + t3p = t3; +} + +/** + * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx) +{ + int8x16_t table { + vreinterpretq_s8_s32(t0.m) + }; + + // Set index byte above max index for unused bytes so table lookup returns zero + int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00)); + uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked); + + return vint4(vreinterpretq_s32_s8(vqtbl1q_s8(table, idx_bytes))); +} + +/** + * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx) +{ + int8x16x2_t table { + vreinterpretq_s8_s32(t0.m), + vreinterpretq_s8_s32(t1.m) + }; + + // Set index byte above max index for unused bytes so table lookup returns zero + int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00)); + uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked); + + return vint4(vreinterpretq_s32_s8(vqtbl2q_s8(table, idx_bytes))); +} + +/** + * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx) +{ + int8x16x4_t table { + vreinterpretq_s8_s32(t0.m), + vreinterpretq_s8_s32(t1.m), + vreinterpretq_s8_s32(t2.m), + vreinterpretq_s8_s32(t3.m) + }; + + // Set index byte above max index for unused bytes so table lookup returns zero + int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00)); + uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked); + + return vint4(vreinterpretq_s32_s8(vqtbl4q_s8(table, idx_bytes))); +} + +/** + * @brief Return a vector of interleaved RGBA data. + * + * Input vectors have the value stored in the bottom 8 bits of each lane, + * with high bits set to zero. + * + * Output vector stores a single RGBA texel packed in each lane. + */ +ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a) +{ + return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a); +} + +/** + * @brief Store a vector, skipping masked lanes. + * + * All masked lanes must be at the end of vector, after all non-masked lanes. + */ +ASTCENC_SIMD_INLINE void store_lanes_masked(int* base, vint4 data, vmask4 mask) +{ + if (mask.lane<3>()) + { + store(data, base); + } + else if (mask.lane<2>()) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + base[2] = data.lane<2>(); + } + else if (mask.lane<1>()) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + } + else if (mask.lane<0>()) + { + base[0] = data.lane<0>(); + } +} + +#define ASTCENC_USE_NATIVE_POPCOUNT 1 + +/** + * @brief Population bit count. + * + * @param v The value to population count. + * + * @return The number of 1 bits. + */ +ASTCENC_SIMD_INLINE int popcount(uint64_t v) +{ + return static_cast<int>(vaddlv_u8(vcnt_u8(vcreate_u8(v)))); +} + +#endif // #ifndef ASTC_VECMATHLIB_NEON_4_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_none_4.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_none_4.h new file mode 100644 index 00000000000..d9b52be3e42 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_none_4.h @@ -0,0 +1,1169 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2019-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief 4x32-bit vectors, implemented using plain C++. + * + * This module implements 4-wide 32-bit float, int, and mask vectors. This + * module provides a scalar fallback for VLA code, primarily useful for + * debugging VLA algorithms without the complexity of handling SIMD. Only the + * baseline level of functionality needed to support VLA is provided. + * + * Note that the vector conditional operators implemented by this module are + * designed to behave like SIMD conditional operators that generate lane masks. + * Rather than returning 0/1 booleans like normal C++ code they will return + * 0/-1 to give a full lane-width bitmask. + * + * Note that the documentation for this module still talks about "vectors" to + * help developers think about the implied VLA behavior when writing optimized + * paths. + */ + +#ifndef ASTC_VECMATHLIB_NONE_4_H_INCLUDED +#define ASTC_VECMATHLIB_NONE_4_H_INCLUDED + +#ifndef ASTCENC_SIMD_INLINE + #error "Include astcenc_vecmathlib.h, do not include directly" +#endif + +#include <algorithm> +#include <cstdio> +#include <cstring> +#include <cfenv> + +// ============================================================================ +// vfloat4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide floats. + */ +struct vfloat4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vfloat4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with wider VLA vectors if data is + * aligned to vector length. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(const float* p) + { + m[0] = p[0]; + m[1] = p[1]; + m[2] = p[2]; + m[3] = p[3]; + } + + /** + * @brief Construct from 4 scalar values replicated across all lanes. + * + * Consider using zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a) + { + m[0] = a; + m[1] = a; + m[2] = a; + m[3] = a; + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d) + { + m[0] = a; + m[1] = b; + m[2] = c; + m[3] = d; + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE float lane() const + { + return m[l]; + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(float a) + { + m[l] = a; + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vfloat4 zero() + { + return vfloat4(0.0f); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p) + { + return vfloat4(*p); + } + + /** + * @brief Factory that returns a vector loaded from aligned memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p) + { + return vfloat4(p); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vfloat4 lane_id() + { + return vfloat4(0.0f, 1.0f, 2.0f, 3.0f); + } + + /** + * @brief Return a swizzled float 2. + */ + template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), 0.0f, 0.0f); + } + + /** + * @brief Return a swizzled float 3. + */ + template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), 0.0f); + } + + /** + * @brief Return a swizzled float 4. + */ + template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), lane<l3>()); + } + + /** + * @brief The vector ... + */ + float m[4]; +}; + +// ============================================================================ +// vint4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide ints. + */ +struct vint4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vint4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using vint4::loada() which is better with wider VLA vectors + * if data is aligned. + */ + ASTCENC_SIMD_INLINE explicit vint4(const int* p) + { + m[0] = p[0]; + m[1] = p[1]; + m[2] = p[2]; + m[3] = p[3]; + } + + /** + * @brief Construct from 4 uint8_t loaded from an unaligned address. + */ + ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p) + { + m[0] = p[0]; + m[1] = p[1]; + m[2] = p[2]; + m[3] = p[3]; + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d) + { + m[0] = a; + m[1] = b; + m[2] = c; + m[3] = d; + } + + + /** + * @brief Construct from 4 scalar values replicated across all lanes. + * + * Consider using vint4::zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a) + { + m[0] = a; + m[1] = a; + m[2] = a; + m[3] = a; + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE int lane() const + { + return m[l]; + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(int a) + { + m[l] = a; + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vint4 zero() + { + return vint4(0); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vint4 load1(const int* p) + { + return vint4(*p); + } + + /** + * @brief Factory that returns a vector loaded from 16B aligned memory. + */ + static ASTCENC_SIMD_INLINE vint4 loada(const int* p) + { + return vint4(p); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vint4 lane_id() + { + return vint4(0, 1, 2, 3); + } + + /** + * @brief The vector ... + */ + int m[4]; +}; + +// ============================================================================ +// vmask4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide control plane masks. + */ +struct vmask4 +{ + /** + * @brief Construct from an existing mask value. + */ + ASTCENC_SIMD_INLINE explicit vmask4(int* p) + { + m[0] = p[0]; + m[1] = p[1]; + m[2] = p[2]; + m[3] = p[3]; + } + + /** + * @brief Construct from 1 scalar value. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a) + { + m[0] = a == false ? 0 : -1; + m[1] = a == false ? 0 : -1; + m[2] = a == false ? 0 : -1; + m[3] = a == false ? 0 : -1; + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d) + { + m[0] = a == false ? 0 : -1; + m[1] = b == false ? 0 : -1; + m[2] = c == false ? 0 : -1; + m[3] = d == false ? 0 : -1; + } + + + /** + * @brief The vector ... + */ + int m[4]; +}; + +// ============================================================================ +// vmask4 operators and functions +// ============================================================================ + +/** + * @brief Overload: mask union (or). + */ +ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b) +{ + return vmask4(a.m[0] | b.m[0], + a.m[1] | b.m[1], + a.m[2] | b.m[2], + a.m[3] | b.m[3]); +} + +/** + * @brief Overload: mask intersect (and). + */ +ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b) +{ + return vmask4(a.m[0] & b.m[0], + a.m[1] & b.m[1], + a.m[2] & b.m[2], + a.m[3] & b.m[3]); +} + +/** + * @brief Overload: mask difference (xor). + */ +ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b) +{ + return vmask4(a.m[0] ^ b.m[0], + a.m[1] ^ b.m[1], + a.m[2] ^ b.m[2], + a.m[3] ^ b.m[3]); +} + +/** + * @brief Overload: mask invert (not). + */ +ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a) +{ + return vmask4(~a.m[0], + ~a.m[1], + ~a.m[2], + ~a.m[3]); +} + +/** + * @brief Return a 1-bit mask code indicating mask status. + * + * bit0 = lane 0 + */ +ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a) +{ + return ((a.m[0] >> 31) & 0x1) | + ((a.m[1] >> 30) & 0x2) | + ((a.m[2] >> 29) & 0x4) | + ((a.m[3] >> 28) & 0x8); +} + +// ============================================================================ +// vint4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b) +{ + return vint4(a.m[0] + b.m[0], + a.m[1] + b.m[1], + a.m[2] + b.m[2], + a.m[3] + b.m[3]); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b) +{ + return vint4(a.m[0] - b.m[0], + a.m[1] - b.m[1], + a.m[2] - b.m[2], + a.m[3] - b.m[3]); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b) +{ + return vint4(a.m[0] * b.m[0], + a.m[1] * b.m[1], + a.m[2] * b.m[2], + a.m[3] * b.m[3]); +} + +/** + * @brief Overload: vector bit invert. + */ +ASTCENC_SIMD_INLINE vint4 operator~(vint4 a) +{ + return vint4(~a.m[0], + ~a.m[1], + ~a.m[2], + ~a.m[3]); +} + +/** + * @brief Overload: vector by vector bitwise or. + */ +ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b) +{ + return vint4(a.m[0] | b.m[0], + a.m[1] | b.m[1], + a.m[2] | b.m[2], + a.m[3] | b.m[3]); +} + +/** + * @brief Overload: vector by vector bitwise and. + */ +ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b) +{ + return vint4(a.m[0] & b.m[0], + a.m[1] & b.m[1], + a.m[2] & b.m[2], + a.m[3] & b.m[3]); +} + +/** + * @brief Overload: vector by vector bitwise xor. + */ +ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b) +{ + return vint4(a.m[0] ^ b.m[0], + a.m[1] ^ b.m[1], + a.m[2] ^ b.m[2], + a.m[3] ^ b.m[3]); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b) +{ + return vmask4(a.m[0] == b.m[0], + a.m[1] == b.m[1], + a.m[2] == b.m[2], + a.m[3] == b.m[3]); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b) +{ + return vmask4(a.m[0] != b.m[0], + a.m[1] != b.m[1], + a.m[2] != b.m[2], + a.m[3] != b.m[3]); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b) +{ + return vmask4(a.m[0] < b.m[0], + a.m[1] < b.m[1], + a.m[2] < b.m[2], + a.m[3] < b.m[3]); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b) +{ + return vmask4(a.m[0] > b.m[0], + a.m[1] > b.m[1], + a.m[2] > b.m[2], + a.m[3] > b.m[3]); +} + +/** + * @brief Logical shift left. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a) +{ + return vint4(a.m[0] << s, + a.m[1] << s, + a.m[2] << s, + a.m[3] << s); +} + +/** + * @brief Logical shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a) +{ + unsigned int as0 = static_cast<unsigned int>(a.m[0]) >> s; + unsigned int as1 = static_cast<unsigned int>(a.m[1]) >> s; + unsigned int as2 = static_cast<unsigned int>(a.m[2]) >> s; + unsigned int as3 = static_cast<unsigned int>(a.m[3]) >> s; + + return vint4(static_cast<int>(as0), + static_cast<int>(as1), + static_cast<int>(as2), + static_cast<int>(as3)); +} + +/** + * @brief Arithmetic shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a) +{ + return vint4(a.m[0] >> s, + a.m[1] >> s, + a.m[2] >> s, + a.m[3] >> s); +} + +/** + * @brief Return the min vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b) +{ + return vint4(a.m[0] < b.m[0] ? a.m[0] : b.m[0], + a.m[1] < b.m[1] ? a.m[1] : b.m[1], + a.m[2] < b.m[2] ? a.m[2] : b.m[2], + a.m[3] < b.m[3] ? a.m[3] : b.m[3]); +} + +/** + * @brief Return the min vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b) +{ + return vint4(a.m[0] > b.m[0] ? a.m[0] : b.m[0], + a.m[1] > b.m[1] ? a.m[1] : b.m[1], + a.m[2] > b.m[2] ? a.m[2] : b.m[2], + a.m[3] > b.m[3] ? a.m[3] : b.m[3]); +} + +/** + * @brief Return the horizontal minimum of a single vector. + */ +ASTCENC_SIMD_INLINE vint4 hmin(vint4 a) +{ + int b = std::min(a.m[0], a.m[1]); + int c = std::min(a.m[2], a.m[3]); + return vint4(std::min(b, c)); +} + +/** + * @brief Return the horizontal maximum of a single vector. + */ +ASTCENC_SIMD_INLINE vint4 hmax(vint4 a) +{ + int b = std::max(a.m[0], a.m[1]); + int c = std::max(a.m[2], a.m[3]); + return vint4(std::max(b, c)); +} + +/** + * @brief Return the horizontal sum of vector lanes as a scalar. + */ +ASTCENC_SIMD_INLINE int hadd_s(vint4 a) +{ + return a.m[0] + a.m[1] + a.m[2] + a.m[3]; +} + +/** + * @brief Store a vector to an aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vint4 a, int* p) +{ + p[0] = a.m[0]; + p[1] = a.m[1]; + p[2] = a.m[2]; + p[3] = a.m[3]; +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vint4 a, int* p) +{ + p[0] = a.m[0]; + p[1] = a.m[1]; + p[2] = a.m[2]; + p[3] = a.m[3]; +} + +/** + * @brief Store lowest N (vector width) bytes into an unaligned address. + */ +ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p) +{ + int* pi = reinterpret_cast<int*>(p); + *pi = a.m[0]; +} + +/** + * @brief Gather N (vector width) indices from the array. + */ +ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices) +{ + return vint4(base[indices.m[0]], + base[indices.m[1]], + base[indices.m[2]], + base[indices.m[3]]); +} + +/** + * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector. + */ +ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a) +{ + int b0 = a.m[0] & 0xFF; + int b1 = a.m[1] & 0xFF; + int b2 = a.m[2] & 0xFF; + int b3 = a.m[3] & 0xFF; + + int b = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24); + return vint4(b, 0, 0, 0); +} + +/** + * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond) +{ + return vint4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0], + (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1], + (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2], + (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]); +} + +// ============================================================================ +// vfloat4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] + b.m[0], + a.m[1] + b.m[1], + a.m[2] + b.m[2], + a.m[3] + b.m[3]); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] - b.m[0], + a.m[1] - b.m[1], + a.m[2] - b.m[2], + a.m[3] - b.m[3]); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] * b.m[0], + a.m[1] * b.m[1], + a.m[2] * b.m[2], + a.m[3] * b.m[3]); +} + +/** + * @brief Overload: vector by vector division. + */ +ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] / b.m[0], + a.m[1] / b.m[1], + a.m[2] / b.m[2], + a.m[3] / b.m[3]); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] == b.m[0], + a.m[1] == b.m[1], + a.m[2] == b.m[2], + a.m[3] == b.m[3]); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] != b.m[0], + a.m[1] != b.m[1], + a.m[2] != b.m[2], + a.m[3] != b.m[3]); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] < b.m[0], + a.m[1] < b.m[1], + a.m[2] < b.m[2], + a.m[3] < b.m[3]); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] > b.m[0], + a.m[1] > b.m[1], + a.m[2] > b.m[2], + a.m[3] > b.m[3]); +} + +/** + * @brief Overload: vector by vector less than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] <= b.m[0], + a.m[1] <= b.m[1], + a.m[2] <= b.m[2], + a.m[3] <= b.m[3]); +} + +/** + * @brief Overload: vector by vector greater than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b) +{ + return vmask4(a.m[0] >= b.m[0], + a.m[1] >= b.m[1], + a.m[2] >= b.m[2], + a.m[3] >= b.m[3]); +} + +/** + * @brief Return the min vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] < b.m[0] ? a.m[0] : b.m[0], + a.m[1] < b.m[1] ? a.m[1] : b.m[1], + a.m[2] < b.m[2] ? a.m[2] : b.m[2], + a.m[3] < b.m[3] ? a.m[3] : b.m[3]); +} + +/** + * @brief Return the max vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b) +{ + return vfloat4(a.m[0] > b.m[0] ? a.m[0] : b.m[0], + a.m[1] > b.m[1] ? a.m[1] : b.m[1], + a.m[2] > b.m[2] ? a.m[2] : b.m[2], + a.m[3] > b.m[3] ? a.m[3] : b.m[3]); +} + +/** + * @brief Return the absolute value of the float vector. + */ +ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a) +{ + return vfloat4(std::abs(a.m[0]), + std::abs(a.m[1]), + std::abs(a.m[2]), + std::abs(a.m[3])); +} + +/** + * @brief Return a float rounded to the nearest integer value. + */ +ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a) +{ + assert(std::fegetround() == FE_TONEAREST); + return vfloat4(std::nearbyint(a.m[0]), + std::nearbyint(a.m[1]), + std::nearbyint(a.m[2]), + std::nearbyint(a.m[3])); +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a) +{ + float tmp1 = std::min(a.m[0], a.m[1]); + float tmp2 = std::min(a.m[2], a.m[3]); + return vfloat4(std::min(tmp1, tmp2)); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a) +{ + float tmp1 = std::max(a.m[0], a.m[1]); + float tmp2 = std::max(a.m[2], a.m[3]); + return vfloat4(std::max(tmp1, tmp2)); +} + +/** + * @brief Return the horizontal sum of a vector. + */ +ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a) +{ + // Use halving add, gives invariance with SIMD versions + return (a.m[0] + a.m[2]) + (a.m[1] + a.m[3]); +} + +/** + * @brief Return the sqrt of the lanes in the vector. + */ +ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a) +{ + return vfloat4(std::sqrt(a.m[0]), + std::sqrt(a.m[1]), + std::sqrt(a.m[2]), + std::sqrt(a.m[3])); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond) +{ + return vfloat4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0], + (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1], + (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2], + (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]); +} + +/** + * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond) +{ + return vfloat4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0], + (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1], + (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2], + (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]); +} + +/** + * @brief Load a vector of gathered results from an array; + */ +ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices) +{ + return vfloat4(base[indices.m[0]], + base[indices.m[1]], + base[indices.m[2]], + base[indices.m[3]]); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vfloat4 a, float* ptr) +{ + ptr[0] = a.m[0]; + ptr[1] = a.m[1]; + ptr[2] = a.m[2]; + ptr[3] = a.m[3]; +} + +/** + * @brief Store a vector to an aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* ptr) +{ + ptr[0] = a.m[0]; + ptr[1] = a.m[1]; + ptr[2] = a.m[2]; + ptr[3] = a.m[3]; +} + +/** + * @brief Return a integer value for a float vector, using truncation. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a) +{ + return vint4(static_cast<int>(a.m[0]), + static_cast<int>(a.m[1]), + static_cast<int>(a.m[2]), + static_cast<int>(a.m[3])); +} + +/**f + * @brief Return a integer value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a) +{ + return vint4(static_cast<int>(a.m[0] + 0.5f), + static_cast<int>(a.m[1] + 0.5f), + static_cast<int>(a.m[2] + 0.5f), + static_cast<int>(a.m[3] + 0.5f)); +} + +/** + * @brief Return a float value for a integer vector. + */ +ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a) +{ + return vfloat4(static_cast<float>(a.m[0]), + static_cast<float>(a.m[1]), + static_cast<float>(a.m[2]), + static_cast<float>(a.m[3])); +} + +/** + * @brief Return a float16 value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a) +{ + return vint4( + float_to_sf16(a.lane<0>()), + float_to_sf16(a.lane<1>()), + float_to_sf16(a.lane<2>()), + float_to_sf16(a.lane<3>())); +} + +/** + * @brief Return a float16 value for a float scalar, using round-to-nearest. + */ +static inline uint16_t float_to_float16(float a) +{ + return float_to_sf16(a); +} + +/** + * @brief Return a float value for a float16 vector. + */ +ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a) +{ + return vfloat4( + sf16_to_float(static_cast<uint16_t>(a.lane<0>())), + sf16_to_float(static_cast<uint16_t>(a.lane<1>())), + sf16_to_float(static_cast<uint16_t>(a.lane<2>())), + sf16_to_float(static_cast<uint16_t>(a.lane<3>()))); +} + +/** + * @brief Return a float value for a float16 scalar. + */ +ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a) +{ + return sf16_to_float(a); +} + +/** + * @brief Return a float value as an integer bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the first half of that flip. + */ +ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a) +{ + vint4 r; + memcpy(r.m, a.m, 4 * 4); + return r; +} + +/** + * @brief Return a integer value as a float bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the second half of that flip. + */ +ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 a) +{ + vfloat4 r; + memcpy(r.m, a.m, 4 * 4); + return r; +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p) +{ + t0p = t0; +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p) +{ + t0p = t0; + t1p = t1; +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare( + vint4 t0, vint4 t1, vint4 t2, vint4 t3, + vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p) +{ + t0p = t0; + t1p = t1; + t2p = t2; + t3p = t3; +} + +/** + * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx) +{ + uint8_t table[16]; + storea(t0, reinterpret_cast<int*>(table + 0)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +} + + +/** + * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx) +{ + uint8_t table[32]; + storea(t0, reinterpret_cast<int*>(table + 0)); + storea(t1, reinterpret_cast<int*>(table + 16)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +} + +/** + * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx) +{ + uint8_t table[64]; + storea(t0, reinterpret_cast<int*>(table + 0)); + storea(t1, reinterpret_cast<int*>(table + 16)); + storea(t2, reinterpret_cast<int*>(table + 32)); + storea(t3, reinterpret_cast<int*>(table + 48)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +} + +/** + * @brief Return a vector of interleaved RGBA data. + * + * Input vectors have the value stored in the bottom 8 bits of each lane, + * with high bits set to zero. + * + * Output vector stores a single RGBA texel packed in each lane. + */ +ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a) +{ + return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a); +} + +/** + * @brief Store a vector, skipping masked lanes. + * + * All masked lanes must be at the end of vector, after all non-masked lanes. + */ +ASTCENC_SIMD_INLINE void store_lanes_masked(int* base, vint4 data, vmask4 mask) +{ + if (mask.m[3]) + { + store(data, base); + } + else if (mask.m[2]) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + base[2] = data.lane<2>(); + } + else if (mask.m[1]) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + } + else if (mask.m[0]) + { + base[0] = data.lane<0>(); + } +} + +#endif // #ifndef ASTC_VECMATHLIB_NONE_4_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_sse_4.h b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_sse_4.h new file mode 100644 index 00000000000..76fe577a899 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_vecmathlib_sse_4.h @@ -0,0 +1,1283 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2019-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief 4x32-bit vectors, implemented using SSE. + * + * This module implements 4-wide 32-bit float, int, and mask vectors for x86 + * SSE. The implementation requires at least SSE2, but higher levels of SSE can + * be selected at compile time to improve performance. + * + * There is a baseline level of functionality provided by all vector widths and + * implementations. This is implemented using identical function signatures, + * modulo data type, so we can use them as substitutable implementations in VLA + * code. + * + * The 4-wide vectors are also used as a fixed-width type, and significantly + * extend the functionality above that available to VLA code. + */ + +#ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED +#define ASTC_VECMATHLIB_SSE_4_H_INCLUDED + +#ifndef ASTCENC_SIMD_INLINE + #error "Include astcenc_vecmathlib.h, do not include directly" +#endif + +#include <cstdio> + +// ============================================================================ +// vfloat4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide floats. + */ +struct vfloat4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vfloat4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(const float *p) + { + m = _mm_loadu_ps(p); + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a) + { + m = _mm_set1_ps(a); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d) + { + m = _mm_set_ps(d, c, b, a); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vfloat4(__m128 a) + { + m = a; + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE float lane() const + { + return _mm_cvtss_f32(_mm_shuffle_ps(m, m, l)); + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(float a) + { +#if ASTCENC_SSE >= 41 + __m128 v = _mm_set1_ps(a); + m = _mm_insert_ps(m, v, l << 6 | l << 4); +#else + alignas(16) float idx[4]; + _mm_store_ps(idx, m); + idx[l] = a; + m = _mm_load_ps(idx); +#endif + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vfloat4 zero() + { + return vfloat4(_mm_setzero_ps()); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p) + { + return vfloat4(_mm_load_ps1(p)); + } + + /** + * @brief Factory that returns a vector loaded from 16B aligned memory. + */ + static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p) + { + return vfloat4(_mm_load_ps(p)); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vfloat4 lane_id() + { + return vfloat4(_mm_set_ps(3, 2, 1, 0)); + } + + /** + * @brief Return a swizzled float 2. + */ + template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2)); + result.set_lane<2>(0.0f); + result.set_lane<3>(0.0f); + return result; + } + + /** + * @brief Return a swizzled float 3. + */ + template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4)); + result.set_lane<3>(0.0f); + return result; + } + + /** + * @brief Return a swizzled float 4. + */ + template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const + { + return vfloat4(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4 | l3 << 6)); + } + + /** + * @brief The vector ... + */ + __m128 m; +}; + +// ============================================================================ +// vint4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide ints. + */ +struct vint4 +{ + /** + * @brief Construct from zero-initialized value. + */ + ASTCENC_SIMD_INLINE vint4() = default; + + /** + * @brief Construct from 4 values loaded from an unaligned address. + * + * Consider using loada() which is better with vectors if data is aligned + * to vector length. + */ + ASTCENC_SIMD_INLINE explicit vint4(const int *p) + { + m = _mm_loadu_si128(reinterpret_cast<const __m128i*>(p)); + } + + /** + * @brief Construct from 4 uint8_t loaded from an unaligned address. + */ + ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p) + { + // _mm_loadu_si32 would be nicer syntax, but missing on older GCC + __m128i t = _mm_cvtsi32_si128(*reinterpret_cast<const int*>(p)); + +#if ASTCENC_SSE >= 41 + m = _mm_cvtepu8_epi32(t); +#else + t = _mm_unpacklo_epi8(t, _mm_setzero_si128()); + m = _mm_unpacklo_epi16(t, _mm_setzero_si128()); +#endif + } + + /** + * @brief Construct from 1 scalar value replicated across all lanes. + * + * Consider using vfloat4::zero() for constexpr zeros. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a) + { + m = _mm_set1_epi32(a); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d) + { + m = _mm_set_epi32(d, c, b, a); + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vint4(__m128i a) + { + m = a; + } + + /** + * @brief Get the scalar from a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE int lane() const + { + return _mm_cvtsi128_si32(_mm_shuffle_epi32(m, l)); + } + + /** + * @brief Set the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE void set_lane(int a) + { +#if ASTCENC_SSE >= 41 + m = _mm_insert_epi32(m, a, l); +#else + alignas(16) int idx[4]; + _mm_store_si128(reinterpret_cast<__m128i*>(idx), m); + idx[l] = a; + m = _mm_load_si128(reinterpret_cast<const __m128i*>(idx)); +#endif + } + + /** + * @brief Factory that returns a vector of zeros. + */ + static ASTCENC_SIMD_INLINE vint4 zero() + { + return vint4(_mm_setzero_si128()); + } + + /** + * @brief Factory that returns a replicated scalar loaded from memory. + */ + static ASTCENC_SIMD_INLINE vint4 load1(const int* p) + { + return vint4(*p); + } + + /** + * @brief Factory that returns a vector loaded from 16B aligned memory. + */ + static ASTCENC_SIMD_INLINE vint4 loada(const int* p) + { + return vint4(_mm_load_si128(reinterpret_cast<const __m128i*>(p))); + } + + /** + * @brief Factory that returns a vector containing the lane IDs. + */ + static ASTCENC_SIMD_INLINE vint4 lane_id() + { + return vint4(_mm_set_epi32(3, 2, 1, 0)); + } + + /** + * @brief The vector ... + */ + __m128i m; +}; + +// ============================================================================ +// vmask4 data type +// ============================================================================ + +/** + * @brief Data type for 4-wide control plane masks. + */ +struct vmask4 +{ + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(__m128 a) + { + m = a; + } + + /** + * @brief Construct from an existing SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(__m128i a) + { + m = _mm_castsi128_ps(a); + } + + /** + * @brief Construct from 1 scalar value. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a) + { + vint4 mask(a == false ? 0 : -1); + m = _mm_castsi128_ps(mask.m); + } + + /** + * @brief Construct from 4 scalar values. + * + * The value of @c a is stored to lane 0 (LSB) in the SIMD register. + */ + ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d) + { + vint4 mask(a == false ? 0 : -1, + b == false ? 0 : -1, + c == false ? 0 : -1, + d == false ? 0 : -1); + + m = _mm_castsi128_ps(mask.m); + } + + /** + * @brief Get the scalar value of a single lane. + */ + template <int l> ASTCENC_SIMD_INLINE float lane() const + { + return _mm_cvtss_f32(_mm_shuffle_ps(m, m, l)); + } + + /** + * @brief The vector ... + */ + __m128 m; +}; + +// ============================================================================ +// vmask4 operators and functions +// ============================================================================ + +/** + * @brief Overload: mask union (or). + */ +ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b) +{ + return vmask4(_mm_or_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask intersect (and). + */ +ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b) +{ + return vmask4(_mm_and_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask difference (xor). + */ +ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b) +{ + return vmask4(_mm_xor_ps(a.m, b.m)); +} + +/** + * @brief Overload: mask invert (not). + */ +ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a) +{ + return vmask4(_mm_xor_si128(_mm_castps_si128(a.m), _mm_set1_epi32(-1))); +} + +/** + * @brief Return a 4-bit mask code indicating mask status. + * + * bit0 = lane 0 + */ +ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a) +{ + return static_cast<unsigned int>(_mm_movemask_ps(a.m)); +} + +// ============================================================================ +// vint4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b) +{ + return vint4(_mm_add_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b) +{ + return vint4(_mm_sub_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b) +{ +#if ASTCENC_SSE >= 41 + return vint4(_mm_mullo_epi32 (a.m, b.m)); +#else + __m128i t1 = _mm_mul_epu32(a.m, b.m); + __m128i t2 = _mm_mul_epu32( + _mm_srli_si128(a.m, 4), + _mm_srli_si128(b.m, 4)); + __m128i r = _mm_unpacklo_epi32( + _mm_shuffle_epi32(t1, _MM_SHUFFLE (0, 0, 2, 0)), + _mm_shuffle_epi32(t2, _MM_SHUFFLE (0, 0, 2, 0))); + return vint4(r); +#endif +} + +/** + * @brief Overload: vector bit invert. + */ +ASTCENC_SIMD_INLINE vint4 operator~(vint4 a) +{ + return vint4(_mm_xor_si128(a.m, _mm_set1_epi32(-1))); +} + +/** + * @brief Overload: vector by vector bitwise or. + */ +ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b) +{ + return vint4(_mm_or_si128(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise and. + */ +ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b) +{ + return vint4(_mm_and_si128(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector bitwise xor. + */ +ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b) +{ + return vint4(_mm_xor_si128(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b) +{ + return vmask4(_mm_cmpeq_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b) +{ + return ~vmask4(_mm_cmpeq_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b) +{ + return vmask4(_mm_cmplt_epi32(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b) +{ + return vmask4(_mm_cmpgt_epi32(a.m, b.m)); +} + +/** + * @brief Logical shift left. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a) +{ + return vint4(_mm_slli_epi32(a.m, s)); +} + +/** + * @brief Logical shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a) +{ + return vint4(_mm_srli_epi32(a.m, s)); +} + +/** + * @brief Arithmetic shift right. + */ +template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a) +{ + return vint4(_mm_srai_epi32(a.m, s)); +} + +/** + * @brief Return the min vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b) +{ +#if ASTCENC_SSE >= 41 + return vint4(_mm_min_epi32(a.m, b.m)); +#else + vmask4 d = a < b; + __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m); + __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m); + return vint4(_mm_or_si128(ap,bp)); +#endif +} + +/** + * @brief Return the max vector of two vectors. + */ +ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b) +{ +#if ASTCENC_SSE >= 41 + return vint4(_mm_max_epi32(a.m, b.m)); +#else + vmask4 d = a > b; + __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m); + __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m); + return vint4(_mm_or_si128(ap,bp)); +#endif +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vint4 hmin(vint4 a) +{ + a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2)))); + a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1)))); + return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0))); +} + +/* + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vint4 hmax(vint4 a) +{ + a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2)))); + a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1)))); + return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0))); +} + +/** + * @brief Return the horizontal sum of a vector as a scalar. + */ +ASTCENC_SIMD_INLINE int hadd_s(vint4 a) +{ + // Add top and bottom halves, lane 1/0 + __m128i fold = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(a.m), + _mm_castsi128_ps(a.m))); + __m128i t = _mm_add_epi32(a.m, fold); + + // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow) + t = _mm_add_epi32(t, _mm_shuffle_epi32(t, 0x55)); + + return _mm_cvtsi128_si32(t); +} + +/** + * @brief Store a vector to a 16B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vint4 a, int* p) +{ + _mm_store_si128(reinterpret_cast<__m128i*>(p), a.m); +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vint4 a, int* p) +{ + // Cast due to missing intrinsics + _mm_storeu_ps(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m)); +} + +/** + * @brief Store lowest N (vector width) bytes into an unaligned address. + */ +ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p) +{ + // Cast due to missing intrinsics + _mm_store_ss(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m)); +} + +/** + * @brief Gather N (vector width) indices from the array. + */ +ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices) +{ +#if ASTCENC_AVX >= 2 + return vint4(_mm_i32gather_epi32(base, indices.m, 4)); +#else + alignas(16) int idx[4]; + storea(indices, idx); + return vint4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]); +#endif +} + +/** + * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector. + */ +ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a) +{ +#if ASTCENC_SSE >= 41 + __m128i shuf = _mm_set_epi8(0,0,0,0, 0,0,0,0, 0,0,0,0, 12,8,4,0); + return vint4(_mm_shuffle_epi8(a.m, shuf)); +#else + __m128i va = _mm_unpacklo_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(1,1,1,1))); + __m128i vb = _mm_unpackhi_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(3,3,3,3))); + return vint4(_mm_unpacklo_epi16(va, vb)); +#endif +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond) +{ + __m128i condi = _mm_castps_si128(cond.m); + +#if ASTCENC_SSE >= 41 + return vint4(_mm_blendv_epi8(a.m, b.m, condi)); +#else + return vint4(_mm_or_si128(_mm_and_si128(condi, b.m), _mm_andnot_si128(condi, a.m))); +#endif +} + +// ============================================================================ +// vfloat4 operators and functions +// ============================================================================ + +/** + * @brief Overload: vector by vector addition. + */ +ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_add_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector subtraction. + */ +ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_sub_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector multiplication. + */ +ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_mul_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector division. + */ +ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_div_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector equality. + */ +ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmpeq_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector inequality. + */ +ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmpneq_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than. + */ +ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmplt_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than. + */ +ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmpgt_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector less than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmple_ps(a.m, b.m)); +} + +/** + * @brief Overload: vector by vector greater than or equal. + */ +ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b) +{ + return vmask4(_mm_cmpge_ps(a.m, b.m)); +} + +/** + * @brief Return the min vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b) +{ + // Do not reorder - second operand will return if either is NaN + return vfloat4(_mm_min_ps(a.m, b.m)); +} + +/** + * @brief Return the max vector of two vectors. + * + * If either lane value is NaN, @c b will be returned for that lane. + */ +ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b) +{ + // Do not reorder - second operand will return if either is NaN + return vfloat4(_mm_max_ps(a.m, b.m)); +} + +/** + * @brief Return the absolute value of the float vector. + */ +ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a) +{ + return vfloat4(_mm_max_ps(_mm_sub_ps(_mm_setzero_ps(), a.m), a.m)); +} + +/** + * @brief Return a float rounded to the nearest integer value. + */ +ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a) +{ +#if ASTCENC_SSE >= 41 + constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC; + return vfloat4(_mm_round_ps(a.m, flags)); +#else + __m128 v = a.m; + __m128 neg_zero = _mm_castsi128_ps(_mm_set1_epi32(static_cast<int>(0x80000000))); + __m128 no_fraction = _mm_set1_ps(8388608.0f); + __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF)); + __m128 sign = _mm_and_ps(v, neg_zero); + __m128 s_magic = _mm_or_ps(no_fraction, sign); + __m128 r1 = _mm_add_ps(v, s_magic); + r1 = _mm_sub_ps(r1, s_magic); + __m128 r2 = _mm_and_ps(v, abs_mask); + __m128 mask = _mm_cmple_ps(r2, no_fraction); + r2 = _mm_andnot_ps(mask, v); + r1 = _mm_and_ps(r1, mask); + return vfloat4(_mm_xor_ps(r1, r2)); +#endif +} + +/** + * @brief Return the horizontal minimum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a) +{ + a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2)))); + a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1)))); + return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0))); +} + +/** + * @brief Return the horizontal maximum of a vector. + */ +ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a) +{ + a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2)))); + a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1)))); + return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0))); +} + +/** + * @brief Return the horizontal sum of a vector as a scalar. + */ +ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a) +{ + // Add top and bottom halves, lane 1/0 + __m128 t = _mm_add_ps(a.m, _mm_movehl_ps(a.m, a.m)); + + // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow) + t = _mm_add_ss(t, _mm_shuffle_ps(t, t, 0x55)); + + return _mm_cvtss_f32(t); +} + +/** + * @brief Return the sqrt of the lanes in the vector. + */ +ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a) +{ + return vfloat4(_mm_sqrt_ps(a.m)); +} + +/** + * @brief Return lanes from @c b if @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond) +{ +#if ASTCENC_SSE >= 41 + return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m)); +#else + return vfloat4(_mm_or_ps(_mm_and_ps(cond.m, b.m), _mm_andnot_ps(cond.m, a.m))); +#endif +} + +/** + * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. + */ +ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond) +{ +#if ASTCENC_SSE >= 41 + return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m)); +#else + __m128 d = _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(cond.m), 31)); + return vfloat4(_mm_or_ps(_mm_and_ps(d, b.m), _mm_andnot_ps(d, a.m))); +#endif +} + +/** + * @brief Load a vector of gathered results from an array; + */ +ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices) +{ +#if ASTCENC_AVX >= 2 + return vfloat4(_mm_i32gather_ps(base, indices.m, 4)); +#else + alignas(16) int idx[4]; + storea(indices, idx); + return vfloat4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]); +#endif +} + +/** + * @brief Store a vector to an unaligned memory address. + */ +ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p) +{ + _mm_storeu_ps(p, a.m); +} + +/** + * @brief Store a vector to a 16B aligned memory address. + */ +ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p) +{ + _mm_store_ps(p, a.m); +} + +/** + * @brief Return a integer value for a float vector, using truncation. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a) +{ + return vint4(_mm_cvttps_epi32(a.m)); +} + +/** + * @brief Return a integer value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a) +{ + a = round(a); + return vint4(_mm_cvttps_epi32(a.m)); +} + +/** + * @brief Return a float value for an integer vector. + */ +ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a) +{ + return vfloat4(_mm_cvtepi32_ps(a.m)); +} + +/** + * @brief Return a float16 value for a float vector, using round-to-nearest. + */ +ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a) +{ +#if ASTCENC_F16C >= 1 + __m128i packedf16 = _mm_cvtps_ph(a.m, 0); + __m128i f16 = _mm_cvtepu16_epi32(packedf16); + return vint4(f16); +#else + return vint4( + float_to_sf16(a.lane<0>()), + float_to_sf16(a.lane<1>()), + float_to_sf16(a.lane<2>()), + float_to_sf16(a.lane<3>())); +#endif +} + +/** + * @brief Return a float16 value for a float scalar, using round-to-nearest. + */ +static inline uint16_t float_to_float16(float a) +{ +#if ASTCENC_F16C >= 1 + __m128i f16 = _mm_cvtps_ph(_mm_set1_ps(a), 0); + return static_cast<uint16_t>(_mm_cvtsi128_si32(f16)); +#else + return float_to_sf16(a); +#endif +} + +/** + * @brief Return a float value for a float16 vector. + */ +ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a) +{ +#if ASTCENC_F16C >= 1 + __m128i packed = _mm_packs_epi32(a.m, a.m); + __m128 f32 = _mm_cvtph_ps(packed); + return vfloat4(f32); +#else + return vfloat4( + sf16_to_float(static_cast<uint16_t>(a.lane<0>())), + sf16_to_float(static_cast<uint16_t>(a.lane<1>())), + sf16_to_float(static_cast<uint16_t>(a.lane<2>())), + sf16_to_float(static_cast<uint16_t>(a.lane<3>()))); +#endif +} + +/** + * @brief Return a float value for a float16 scalar. + */ +ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a) +{ +#if ASTCENC_F16C >= 1 + __m128i packed = _mm_set1_epi16(static_cast<short>(a)); + __m128 f32 = _mm_cvtph_ps(packed); + return _mm_cvtss_f32(f32); +#else + return sf16_to_float(a); +#endif +} + +/** + * @brief Return a float value as an integer bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the first half of that flip. + */ +ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a) +{ + return vint4(_mm_castps_si128(a.m)); +} + +/** + * @brief Return a integer value as a float bit pattern (i.e. no conversion). + * + * It is a common trick to convert floats into integer bit patterns, perform + * some bit hackery based on knowledge they are IEEE 754 layout, and then + * convert them back again. This is the second half of that flip. + */ +ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v) +{ + return vfloat4(_mm_castsi128_ps(v.m)); +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p) +{ + t0p = t0; +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p) +{ +#if ASTCENC_SSE >= 30 + t0p = t0; + t1p = t0 ^ t1; +#else + t0p = t0; + t1p = t1; +#endif +} + +/** + * @brief Prepare a vtable lookup table for use with the native SIMD size. + */ +ASTCENC_SIMD_INLINE void vtable_prepare( + vint4 t0, vint4 t1, vint4 t2, vint4 t3, + vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p) +{ +#if ASTCENC_SSE >= 30 + t0p = t0; + t1p = t0 ^ t1; + t2p = t1 ^ t2; + t3p = t2 ^ t3; +#else + t0p = t0; + t1p = t1; + t2p = t2; + t3p = t3; +#endif +} + +/** + * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx) +{ +#if ASTCENC_SSE >= 30 + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m128i result = _mm_shuffle_epi8(t0.m, idxx); + return vint4(result); +#else + alignas(ASTCENC_VECALIGN) uint8_t table[16]; + storea(t0, reinterpret_cast<int*>(table + 0)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +#endif +} + +/** + * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx) +{ +#if ASTCENC_SSE >= 30 + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m128i result = _mm_shuffle_epi8(t0.m, idxx); + idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16)); + + __m128i result2 = _mm_shuffle_epi8(t1.m, idxx); + result = _mm_xor_si128(result, result2); + + return vint4(result); +#else + alignas(ASTCENC_VECALIGN) uint8_t table[32]; + storea(t0, reinterpret_cast<int*>(table + 0)); + storea(t1, reinterpret_cast<int*>(table + 16)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +#endif +} + +/** + * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes. + */ +ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx) +{ +#if ASTCENC_SSE >= 30 + // Set index byte MSB to 1 for unused bytes so shuffle returns zero + __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00))); + + __m128i result = _mm_shuffle_epi8(t0.m, idxx); + idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16)); + + __m128i result2 = _mm_shuffle_epi8(t1.m, idxx); + result = _mm_xor_si128(result, result2); + idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16)); + + result2 = _mm_shuffle_epi8(t2.m, idxx); + result = _mm_xor_si128(result, result2); + idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16)); + + result2 = _mm_shuffle_epi8(t3.m, idxx); + result = _mm_xor_si128(result, result2); + + return vint4(result); +#else + alignas(ASTCENC_VECALIGN) uint8_t table[64]; + storea(t0, reinterpret_cast<int*>(table + 0)); + storea(t1, reinterpret_cast<int*>(table + 16)); + storea(t2, reinterpret_cast<int*>(table + 32)); + storea(t3, reinterpret_cast<int*>(table + 48)); + + return vint4(table[idx.lane<0>()], + table[idx.lane<1>()], + table[idx.lane<2>()], + table[idx.lane<3>()]); +#endif +} + +/** + * @brief Return a vector of interleaved RGBA data. + * + * Input vectors have the value stored in the bottom 8 bits of each lane, + * with high bits set to zero. + * + * Output vector stores a single RGBA texel packed in each lane. + */ +ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a) +{ +// Workaround an XCode compiler internal fault; note is slower than slli_epi32 +// so we should revert this when we get the opportunity +#if defined(__APPLE__) + __m128i value = r.m; + value = _mm_add_epi32(value, _mm_bslli_si128(g.m, 1)); + value = _mm_add_epi32(value, _mm_bslli_si128(b.m, 2)); + value = _mm_add_epi32(value, _mm_bslli_si128(a.m, 3)); + return vint4(value); +#else + __m128i value = r.m; + value = _mm_add_epi32(value, _mm_slli_epi32(g.m, 8)); + value = _mm_add_epi32(value, _mm_slli_epi32(b.m, 16)); + value = _mm_add_epi32(value, _mm_slli_epi32(a.m, 24)); + return vint4(value); +#endif +} + +/** + * @brief Store a vector, skipping masked lanes. + * + * All masked lanes must be at the end of vector, after all non-masked lanes. + */ +ASTCENC_SIMD_INLINE void store_lanes_masked(int* base, vint4 data, vmask4 mask) +{ +#if ASTCENC_AVX >= 2 + _mm_maskstore_epi32(base, _mm_castps_si128(mask.m), data.m); +#else + // Note - we cannot use _mm_maskmoveu_si128 as the underlying hardware doesn't guarantee + // fault suppression on masked lanes so we can get page faults at the end of an image. + if (mask.lane<3>() != 0.0f) + { + store(data, base); + } + else if (mask.lane<2>() != 0.0f) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + base[2] = data.lane<2>(); + } + else if (mask.lane<1>() != 0.0f) + { + base[0] = data.lane<0>(); + base[1] = data.lane<1>(); + } + else if (mask.lane<0>() != 0.0f) + { + base[0] = data.lane<0>(); + } +#endif +} + +#if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41) + +#define ASTCENC_USE_NATIVE_DOT_PRODUCT 1 + +/** + * @brief Return the dot product for the full 4 lanes, returning scalar. + */ +ASTCENC_SIMD_INLINE float dot_s(vfloat4 a, vfloat4 b) +{ + return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0xFF)); +} + +/** + * @brief Return the dot product for the full 4 lanes, returning vector. + */ +ASTCENC_SIMD_INLINE vfloat4 dot(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_dp_ps(a.m, b.m, 0xFF)); +} + +/** + * @brief Return the dot product for the bottom 3 lanes, returning scalar. + */ +ASTCENC_SIMD_INLINE float dot3_s(vfloat4 a, vfloat4 b) +{ + return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0x77)); +} + +/** + * @brief Return the dot product for the bottom 3 lanes, returning vector. + */ +ASTCENC_SIMD_INLINE vfloat4 dot3(vfloat4 a, vfloat4 b) +{ + return vfloat4(_mm_dp_ps(a.m, b.m, 0x77)); +} + +#endif // #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41) + +#if ASTCENC_POPCNT >= 1 + +#define ASTCENC_USE_NATIVE_POPCOUNT 1 + +/** + * @brief Population bit count. + * + * @param v The value to population count. + * + * @return The number of 1 bits. + */ +ASTCENC_SIMD_INLINE int popcount(uint64_t v) +{ + return static_cast<int>(_mm_popcnt_u64(v)); +} + +#endif // ASTCENC_POPCNT >= 1 + +#endif // #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_align.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_align.cpp new file mode 100644 index 00000000000..e40a318cf52 --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_align.cpp @@ -0,0 +1,479 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2022 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +#if !defined(ASTCENC_DECOMPRESS_ONLY) + +/** + * @brief Functions for angular-sum algorithm for weight alignment. + * + * This algorithm works as follows: + * - we compute a complex number P as (cos s*i, sin s*i) for each weight, + * where i is the input value and s is a scaling factor based on the spacing between the weights. + * - we then add together complex numbers for all the weights. + * - we then compute the length and angle of the resulting sum. + * + * This should produce the following results: + * - perfect alignment results in a vector whose length is equal to the sum of lengths of all inputs + * - even distribution results in a vector of length 0. + * - all samples identical results in perfect alignment for every scaling. + * + * For each scaling factor within a given set, we compute an alignment factor from 0 to 1. This + * should then result in some scalings standing out as having particularly good alignment factors; + * we can use this to produce a set of candidate scale/shift values for various quantization levels; + * we should then actually try them and see what happens. + */ + +#include "astcenc_internal.h" +#include "astcenc_vecmathlib.h" + +#include <stdio.h> +#include <cassert> +#include <cstring> + +static constexpr unsigned int ANGULAR_STEPS { 32 }; + +static_assert((ANGULAR_STEPS % ASTCENC_SIMD_WIDTH) == 0, + "ANGULAR_STEPS must be multiple of ASTCENC_SIMD_WIDTH"); + +static_assert(ANGULAR_STEPS >= 32, + "ANGULAR_STEPS must be at least max(steps_for_quant_level)"); + +// Store a reduced sin/cos table for 64 possible weight values; this causes +// slight quality loss compared to using sin() and cos() directly. Must be 2^N. +static constexpr unsigned int SINCOS_STEPS { 64 }; + +static const uint8_t steps_for_quant_level[12] { + 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32 +}; + +alignas(ASTCENC_VECALIGN) static float sin_table[SINCOS_STEPS][ANGULAR_STEPS]; +alignas(ASTCENC_VECALIGN) static float cos_table[SINCOS_STEPS][ANGULAR_STEPS]; + +#if defined(ASTCENC_DIAGNOSTICS) + static bool print_once { true }; +#endif + +/* See header for documentation. */ +void prepare_angular_tables() +{ + for (unsigned int i = 0; i < ANGULAR_STEPS; i++) + { + float angle_step = static_cast<float>(i + 1); + + for (unsigned int j = 0; j < SINCOS_STEPS; j++) + { + sin_table[j][i] = static_cast<float>(sinf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j))); + cos_table[j][i] = static_cast<float>(cosf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j))); + } + } +} + +/** + * @brief Compute the angular alignment factors and offsets. + * + * @param weight_count The number of (decimated) weights. + * @param dec_weight_ideal_value The ideal decimated unquantized weight values. + * @param max_angular_steps The maximum number of steps to be tested. + * @param[out] offsets The output angular offsets array. + */ +static void compute_angular_offsets( + unsigned int weight_count, + const float* dec_weight_ideal_value, + unsigned int max_angular_steps, + float* offsets +) { + promise(weight_count > 0); + promise(max_angular_steps > 0); + + alignas(ASTCENC_VECALIGN) int isamplev[BLOCK_MAX_WEIGHTS]; + + // Precompute isample; arrays are always allocated 64 elements long + for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH) + { + // Add 2^23 and interpreting bits extracts round-to-nearest int + vfloat sample = loada(dec_weight_ideal_value + i) * (SINCOS_STEPS - 1.0f) + vfloat(12582912.0f); + vint isample = float_as_int(sample) & vint((SINCOS_STEPS - 1)); + storea(isample, isamplev + i); + } + + // Arrays are multiple of SIMD width (ANGULAR_STEPS), safe to overshoot max + vfloat mult = vfloat(1.0f / (2.0f * astc::PI)); + + for (unsigned int i = 0; i < max_angular_steps; i += ASTCENC_SIMD_WIDTH) + { + vfloat anglesum_x = vfloat::zero(); + vfloat anglesum_y = vfloat::zero(); + + for (unsigned int j = 0; j < weight_count; j++) + { + int isample = isamplev[j]; + anglesum_x += loada(cos_table[isample] + i); + anglesum_y += loada(sin_table[isample] + i); + } + + vfloat angle = atan2(anglesum_y, anglesum_x); + vfloat ofs = angle * mult; + storea(ofs, offsets + i); + } +} + +/** + * @brief For a given step size compute the lowest and highest weight. + * + * Compute the lowest and highest weight that results from quantizing using the given stepsize and + * offset, and then compute the resulting error. The cut errors indicate the error that results from + * forcing samples that should have had one weight value one step up or down. + * + * @param weight_count The number of (decimated) weights. + * @param dec_weight_ideal_value The ideal decimated unquantized weight values. + * @param max_angular_steps The maximum number of steps to be tested. + * @param max_quant_steps The maximum quantization level to be tested. + * @param offsets The angular offsets array. + * @param[out] lowest_weight Per angular step, the lowest weight. + * @param[out] weight_span Per angular step, the span between lowest and highest weight. + * @param[out] error Per angular step, the error. + * @param[out] cut_low_weight_error Per angular step, the low weight cut error. + * @param[out] cut_high_weight_error Per angular step, the high weight cut error. + */ +static void compute_lowest_and_highest_weight( + unsigned int weight_count, + const float* dec_weight_ideal_value, + unsigned int max_angular_steps, + unsigned int max_quant_steps, + const float* offsets, + float* lowest_weight, + int* weight_span, + float* error, + float* cut_low_weight_error, + float* cut_high_weight_error +) { + promise(weight_count > 0); + promise(max_angular_steps > 0); + + vfloat rcp_stepsize = vfloat::lane_id() + vfloat(1.0f); + + // Arrays are ANGULAR_STEPS long, so always safe to run full vectors + for (unsigned int sp = 0; sp < max_angular_steps; sp += ASTCENC_SIMD_WIDTH) + { + vfloat minidx(128.0f); + vfloat maxidx(-128.0f); + vfloat errval = vfloat::zero(); + vfloat cut_low_weight_err = vfloat::zero(); + vfloat cut_high_weight_err = vfloat::zero(); + vfloat offset = loada(offsets + sp); + + for (unsigned int j = 0; j < weight_count; j++) + { + vfloat sval = load1(dec_weight_ideal_value + j) * rcp_stepsize - offset; + vfloat svalrte = round(sval); + vfloat diff = sval - svalrte; + errval += diff * diff; + + // Reset tracker on min hit + vmask mask = svalrte < minidx; + minidx = select(minidx, svalrte, mask); + cut_low_weight_err = select(cut_low_weight_err, vfloat::zero(), mask); + + // Accumulate on min hit + mask = svalrte == minidx; + vfloat accum = cut_low_weight_err + vfloat(1.0f) - vfloat(2.0f) * diff; + cut_low_weight_err = select(cut_low_weight_err, accum, mask); + + // Reset tracker on max hit + mask = svalrte > maxidx; + maxidx = select(maxidx, svalrte, mask); + cut_high_weight_err = select(cut_high_weight_err, vfloat::zero(), mask); + + // Accumulate on max hit + mask = svalrte == maxidx; + accum = cut_high_weight_err + vfloat(1.0f) + vfloat(2.0f) * diff; + cut_high_weight_err = select(cut_high_weight_err, accum, mask); + } + + // Write out min weight and weight span; clamp span to a usable range + vint span = float_to_int(maxidx - minidx + vfloat(1)); + span = min(span, vint(max_quant_steps + 3)); + span = max(span, vint(2)); + storea(minidx, lowest_weight + sp); + storea(span, weight_span + sp); + + // The cut_(lowest/highest)_weight_error indicate the error that results from forcing + // samples that should have had the weight value one step (up/down). + vfloat ssize = 1.0f / rcp_stepsize; + vfloat errscale = ssize * ssize; + storea(errval * errscale, error + sp); + storea(cut_low_weight_err * errscale, cut_low_weight_error + sp); + storea(cut_high_weight_err * errscale, cut_high_weight_error + sp); + + rcp_stepsize = rcp_stepsize + vfloat(ASTCENC_SIMD_WIDTH); + } +} + +/** + * @brief The main function for the angular algorithm. + * + * @param weight_count The number of (decimated) weights. + * @param dec_weight_ideal_value The ideal decimated unquantized weight values. + * @param max_quant_level The maximum quantization level to be tested. + * @param[out] low_value Per angular step, the lowest weight value. + * @param[out] high_value Per angular step, the highest weight value. + */ +static void compute_angular_endpoints_for_quant_levels( + unsigned int weight_count, + const float* dec_weight_ideal_value, + unsigned int max_quant_level, + float low_value[TUNE_MAX_ANGULAR_QUANT + 1], + float high_value[TUNE_MAX_ANGULAR_QUANT + 1] +) { + unsigned int max_quant_steps = steps_for_quant_level[max_quant_level]; + unsigned int max_angular_steps = steps_for_quant_level[max_quant_level]; + + alignas(ASTCENC_VECALIGN) float angular_offsets[ANGULAR_STEPS]; + + compute_angular_offsets(weight_count, dec_weight_ideal_value, + max_angular_steps, angular_offsets); + + alignas(ASTCENC_VECALIGN) float lowest_weight[ANGULAR_STEPS]; + alignas(ASTCENC_VECALIGN) int32_t weight_span[ANGULAR_STEPS]; + alignas(ASTCENC_VECALIGN) float error[ANGULAR_STEPS]; + alignas(ASTCENC_VECALIGN) float cut_low_weight_error[ANGULAR_STEPS]; + alignas(ASTCENC_VECALIGN) float cut_high_weight_error[ANGULAR_STEPS]; + + compute_lowest_and_highest_weight(weight_count, dec_weight_ideal_value, + max_angular_steps, max_quant_steps, + angular_offsets, lowest_weight, weight_span, error, + cut_low_weight_error, cut_high_weight_error); + + // For each quantization level, find the best error terms. Use packed vectors so data-dependent + // branches can become selects. This involves some integer to float casts, but the values are + // small enough so they never round the wrong way. + vfloat4 best_results[36]; + + // Initialize the array to some safe defaults + promise(max_quant_steps > 0); + for (unsigned int i = 0; i < (max_quant_steps + 4); i++) + { + // Lane<0> = Best error + // Lane<1> = Best scale; -1 indicates no solution found + // Lane<2> = Cut low weight + best_results[i] = vfloat4(ERROR_CALC_DEFAULT, -1.0f, 0.0f, 0.0f); + } + + promise(max_angular_steps > 0); + for (unsigned int i = 0; i < max_angular_steps; i++) + { + float i_flt = static_cast<float>(i); + + int idx_span = weight_span[i]; + + float error_cut_low = error[i] + cut_low_weight_error[i]; + float error_cut_high = error[i] + cut_high_weight_error[i]; + float error_cut_low_high = error[i] + cut_low_weight_error[i] + cut_high_weight_error[i]; + + // Check best error against record N + vfloat4 best_result = best_results[idx_span]; + vfloat4 new_result = vfloat4(error[i], i_flt, 0.0f, 0.0f); + vmask4 mask = vfloat4(best_result.lane<0>()) > vfloat4(error[i]); + best_results[idx_span] = select(best_result, new_result, mask); + + // Check best error against record N-1 with either cut low or cut high + best_result = best_results[idx_span - 1]; + + new_result = vfloat4(error_cut_low, i_flt, 1.0f, 0.0f); + mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_low); + best_result = select(best_result, new_result, mask); + + new_result = vfloat4(error_cut_high, i_flt, 0.0f, 0.0f); + mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_high); + best_results[idx_span - 1] = select(best_result, new_result, mask); + + // Check best error against record N-2 with both cut low and high + best_result = best_results[idx_span - 2]; + new_result = vfloat4(error_cut_low_high, i_flt, 1.0f, 0.0f); + mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_low_high); + best_results[idx_span - 2] = select(best_result, new_result, mask); + } + + for (unsigned int i = 0; i <= max_quant_level; i++) + { + unsigned int q = steps_for_quant_level[i]; + int bsi = static_cast<int>(best_results[q].lane<1>()); + + // Did we find anything? +#if defined(ASTCENC_DIAGNOSTICS) + if ((bsi < 0) && print_once) + { + print_once = false; + printf("INFO: Unable to find full encoding within search error limit.\n\n"); + } +#endif + + bsi = astc::max(0, bsi); + + float lwi = lowest_weight[bsi] + best_results[q].lane<2>(); + float hwi = lwi + static_cast<float>(q) - 1.0f; + + float stepsize = 1.0f / (1.0f + static_cast<float>(bsi)); + low_value[i] = (angular_offsets[bsi] + lwi) * stepsize; + high_value[i] = (angular_offsets[bsi] + hwi) * stepsize; + } +} + +/* See header for documentation. */ +void compute_angular_endpoints_1plane( + bool only_always, + const block_size_descriptor& bsd, + const float* dec_weight_ideal_value, + unsigned int max_weight_quant, + compression_working_buffers& tmpbuf +) { + float (&low_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1; + float (&high_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1; + + float (&low_values)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values1; + float (&high_values)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values1; + + unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always + : bsd.decimation_mode_count_selected; + promise(max_decimation_modes > 0); + for (unsigned int i = 0; i < max_decimation_modes; i++) + { + const decimation_mode& dm = bsd.decimation_modes[i]; + if (!dm.is_ref_1_plane(static_cast<quant_method>(max_weight_quant))) + { + continue; + } + + unsigned int weight_count = bsd.get_decimation_info(i).weight_count; + + unsigned int max_precision = dm.maxprec_1plane; + if (max_precision > TUNE_MAX_ANGULAR_QUANT) + { + max_precision = TUNE_MAX_ANGULAR_QUANT; + } + + if (max_precision > max_weight_quant) + { + max_precision = max_weight_quant; + } + + compute_angular_endpoints_for_quant_levels( + weight_count, + dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS, + max_precision, low_values[i], high_values[i]); + } + + unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always + : bsd.block_mode_count_1plane_selected; + promise(max_block_modes > 0); + for (unsigned int i = 0; i < max_block_modes; i++) + { + const block_mode& bm = bsd.block_modes[i]; + assert(!bm.is_dual_plane); + + unsigned int quant_mode = bm.quant_mode; + unsigned int decim_mode = bm.decimation_mode; + + if (quant_mode <= TUNE_MAX_ANGULAR_QUANT) + { + low_value[i] = low_values[decim_mode][quant_mode]; + high_value[i] = high_values[decim_mode][quant_mode]; + } + else + { + low_value[i] = 0.0f; + high_value[i] = 1.0f; + } + } +} + +/* See header for documentation. */ +void compute_angular_endpoints_2planes( + const block_size_descriptor& bsd, + const float* dec_weight_ideal_value, + unsigned int max_weight_quant, + compression_working_buffers& tmpbuf +) { + float (&low_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1; + float (&high_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1; + float (&low_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value2; + float (&high_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value2; + + float (&low_values1)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values1; + float (&high_values1)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values1; + float (&low_values2)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values2; + float (&high_values2)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values2; + + promise(bsd.decimation_mode_count_selected > 0); + for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++) + { + const decimation_mode& dm = bsd.decimation_modes[i]; + if (!dm.is_ref_2_plane(static_cast<quant_method>(max_weight_quant))) + { + continue; + } + + unsigned int weight_count = bsd.get_decimation_info(i).weight_count; + + unsigned int max_precision = dm.maxprec_2planes; + if (max_precision > TUNE_MAX_ANGULAR_QUANT) + { + max_precision = TUNE_MAX_ANGULAR_QUANT; + } + + if (max_precision > max_weight_quant) + { + max_precision = max_weight_quant; + } + + compute_angular_endpoints_for_quant_levels( + weight_count, + dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS, + max_precision, low_values1[i], high_values1[i]); + + compute_angular_endpoints_for_quant_levels( + weight_count, + dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET, + max_precision, low_values2[i], high_values2[i]); + } + + unsigned int start = bsd.block_mode_count_1plane_selected; + unsigned int end = bsd.block_mode_count_1plane_2plane_selected; + for (unsigned int i = start; i < end; i++) + { + const block_mode& bm = bsd.block_modes[i]; + unsigned int quant_mode = bm.quant_mode; + unsigned int decim_mode = bm.decimation_mode; + + if (quant_mode <= TUNE_MAX_ANGULAR_QUANT) + { + low_value1[i] = low_values1[decim_mode][quant_mode]; + high_value1[i] = high_values1[decim_mode][quant_mode]; + low_value2[i] = low_values2[decim_mode][quant_mode]; + high_value2[i] = high_values2[decim_mode][quant_mode]; + } + else + { + low_value1[i] = 0.0f; + high_value1[i] = 1.0f; + low_value2[i] = 0.0f; + high_value2[i] = 1.0f; + } + } +} + +#endif diff --git a/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_quant_xfer_tables.cpp b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_quant_xfer_tables.cpp new file mode 100644 index 00000000000..35bfa08324c --- /dev/null +++ b/3rdparty/bimg/3rdparty/astc-encoder/source/astcenc_weight_quant_xfer_tables.cpp @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: Apache-2.0 +// ---------------------------------------------------------------------------- +// Copyright 2011-2021 Arm Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// ---------------------------------------------------------------------------- + +/** + * @brief Data tables for quantization transfer. + */ + +#include "astcenc_internal.h" + +#define _ 0 // Using _ to indicate an entry that will not be used. + +const quant_and_transfer_table quant_and_xfer_tables[12] { + // Quantization method 0, range 0..1 + { + QUANT_2, + {0, 64}, + {0, 1}, + {0, 64}, + {0x4000,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, + _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, + 0x4000} + }, + // Quantization method 1, range 0..2 + { + QUANT_3, + {0, 32, 64}, + {0, 1, 2}, + {0, 32, 64}, + {0x2000,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, + _,_,0x4000,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, + _,_,_,_,0x4020} + }, + // Quantization method 2, range 0..3 + { + QUANT_4, + {0, 21, 43, 64}, + {0, 1, 2, 3}, + {0, 21, 43, 64}, + {0x1500,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,0x2b00,_,_,_,_, + _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,0x4015,_,_,_,_,_,_,_,_,_,_,_,_, + _,_,_,_,_,_,_,_,0x402b} + }, + // Quantization method 3, range 0..4 + { + QUANT_5, + {0, 16, 32, 48, 64}, + {0, 1, 2, 3, 4}, + {0, 16, 32, 48, 64}, + {0x1000,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,0x2000,_,_,_,_,_,_,_,_,_, + _,_,_,_,_,_,0x3010,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,0x4020,_,_,_, + _,_,_,_,_,_,_,_,_,_,_,_,0x4030} + }, + // Quantization method 4, range 0..5 + { + QUANT_6, + {0, 12, 25, 39, 52, 64}, + {0, 2, 4, 5, 3, 1}, + {0, 64, 12, 52, 25, 39}, + {0x0c00,_,_,_,_,_,_,_,_,_,_,_,0x1900,_,_,_,_,_,_,_,_,_,_,_,_, + 0x270c,_,_,_,_,_,_,_,_,_,_,_,_,_,0x3419,_,_,_,_,_,_,_,_,_,_, + _,_,0x4027,_,_,_,_,_,_,_,_,_,_,_,0x4034} + }, + // Quantization method 5, range 0..7 + { + QUANT_8, + {0, 9, 18, 27, 37, 46, 55, 64}, + {0, 1, 2, 3, 4, 5, 6, 7}, + {0, 9, 18, 27, 37, 46, 55, 64}, + {0x0900,_,_,_,_,_,_,_,_,0x1200,_,_,_,_,_,_,_,_,0x1b09,_,_, + _,_,_,_,_,_,0x2512,_,_,_,_,_,_,_,_,_,0x2e1b,_,_,_,_,_,_,_,_, + 0x3725,_,_,_,_,_,_,_,_,0x402e,_,_,_,_,_,_,_,_,0x4037} + }, + // Quantization method 6, range 0..9 + { + QUANT_10, + {0, 7, 14, 21, 28, 36, 43, 50, 57, 64}, + {0, 2, 4, 6, 8, 9, 7, 5, 3, 1}, + {0, 64, 7, 57, 14, 50, 21, 43, 28, 36}, + {0x0700,_,_,_,_,_,_,0x0e00,_,_,_,_,_,_,0x1507,_,_,_,_,_,_, + 0x1c0e,_,_,_,_,_,_,0x2415,_,_,_,_,_,_,_,0x2b1c,_,_,_,_,_, + _,0x3224,_,_,_,_,_,_,0x392b,_,_,_,_,_,_,0x4032,_,_,_,_,_, + _,0x4039} + }, + // Quantization method 7, range 0..11 + { + QUANT_12, + {0, 5, 11, 17, 23, 28, 36, 41, 47, 53, 59, 64}, + {0, 4, 8, 2, 6, 10, 11, 7, 3, 9, 5, 1}, + {0, 64, 17, 47, 5, 59, 23, 41, 11, 53, 28, 36}, + {0x0500,_,_,_,_,0x0b00,_,_,_,_,_,0x1105,_,_,_,_,_, + 0x170b,_,_,_,_,_,0x1c11,_,_,_,_,0x2417,_,_,_,_,_,_,_, + 0x291c,_,_,_,_,0x2f24,_,_,_,_,_,0x3529,_,_,_,_,_, + 0x3b2f,_,_,_,_,_,0x4035,_,_,_,_,0x403b} + }, + // Quantization method 8, range 0..15 + { + QUANT_16, + {0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64}, + {0x0400,_,_,_,0x0800,_,_,_,0x0c04,_,_,_,0x1108,_,_,_,_, + 0x150c,_,_,_,0x1911,_,_,_,0x1d15,_,_,_,0x2319,_,_,_,_, + _,0x271d,_,_,_,0x2b23,_,_,_,0x2f27,_,_,_,0x342b,_,_,_, + _,0x382f,_,_,_,0x3c34,_,_,_,0x4038,_,_,_,0x403c} + }, + // Quantization method 9, range 0..19 + { + QUANT_20, + {0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 35, 38, 41, 45, 48, 51, 55, 58, 61, 64}, + {0, 4, 8, 12, 16, 2, 6, 10, 14, 18, 19, 15, 11, 7, 3, 17, 13, 9, 5, 1}, + {0, 64, 16, 48, 3, 61, 19, 45, 6, 58, 23, 41, 9, 55, 26, 38, 13, 51, 29, 35}, + {0x0300,_,_,0x0600,_,_,0x0903,_,_,0x0d06,_,_,_, + 0x1009,_,_,0x130d,_,_,0x1710,_,_,_,0x1a13,_,_, + 0x1d17,_,_,0x231a,_,_,_,_,_,0x261d,_,_,0x2923,_,_, + 0x2d26,_,_,_,0x3029,_,_,0x332d,_,_,0x3730,_,_,_, + 0x3a33,_,_,0x3d37,_,_,0x403a,_,_,0x403d} + }, + // Quantization method 10, range 0..23 + { + QUANT_24, + {0, 2, 5, 8, 11, 13, 16, 19, 22, 24, 27, 30, 34, 37, 40, 42, 45, 48, 51, 53, 56, 59, 62, 64}, + {0, 8, 16, 2, 10, 18, 4, 12, 20, 6, 14, 22, 23, 15, 7, 21, 13, 5, 19, 11, 3, 17, 9, 1}, + {0, 64, 8, 56, 16, 48, 24, 40, 2, 62, 11, 53, 19, 45, 27, 37, 5, 59, 13, 51, 22, 42, 30, 34}, + {0x0200,_,0x0500,_,_,0x0802,_,_,0x0b05,_,_,0x0d08, + _,0x100b,_,_,0x130d,_,_,0x1610,_,_,0x1813,_, + 0x1b16,_,_,0x1e18,_,_,0x221b,_,_,_,0x251e,_,_, + 0x2822,_,_,0x2a25,_,0x2d28,_,_,0x302a,_,_,0x332d, + _,_,0x3530,_,0x3833,_,_,0x3b35,_,_,0x3e38,_,_, + 0x403b,_,0x403e} + }, + // Quantization method 11, range 0..31 + { + QUANT_32, + {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, + {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64}, + {0x0200,_,0x0400,_,0x0602,_,0x0804,_,0x0a06,_, + 0x0c08,_,0x0e0a,_,0x100c,_,0x120e,_,0x1410,_, + 0x1612,_,0x1814,_,0x1a16,_,0x1c18,_,0x1e1a,_, + 0x221c,_,_,_,0x241e,_,0x2622,_,0x2824,_,0x2a26,_, + 0x2c28,_,0x2e2a,_,0x302c,_,0x322e,_,0x3430,_, + 0x3632,_,0x3834,_,0x3a36,_,0x3c38,_,0x3e3a,_, + 0x403c,_,0x403e} + } +}; diff --git a/3rdparty/bimg/3rdparty/astc/astc_averages_and_directions.cpp b/3rdparty/bimg/3rdparty/astc/astc_averages_and_directions.cpp deleted file mode 100644 index ece97d07fb1..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_averages_and_directions.cpp +++ /dev/null @@ -1,627 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Implements functions for finding dominant direction of a set of - * colors, using ARM patent pending method. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -#include <math.h> -#include "mathlib.h" - -#ifdef DEBUG_CAPTURE_NAN - #ifndef _GNU_SOURCE - #define _GNU_SOURCE - #endif - - #include <fenv.h> -#endif - -/* routines to compute average colors and dominant directions for blocks with 3 and 4 components. */ - -/* - for a full block, functions to compute averages and dominant directions. The averages and directions are computed separately for each partition. - We have separate versions for blocks with and without alpha, since the processing for blocks with alpha is significantly more expensive. - The direction vectors it produces are NOT normalized. -*/ -void compute_averages_and_directions_rgba(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float4 * color_scalefactors, - float4 * averages, float4 * directions_rgba, float3 * directions_gba, float3 * directions_rba, float3 * directions_rga, float3 * directions_rgb) -{ - int i; - int partition_count = pt->partition_count; - int partition; - - for (partition = 0; partition < partition_count; partition++) - { - const uint8_t *weights = pt->texels_of_partition[partition]; - int texelcount = pt->texels_per_partition[partition]; - - float4 base_sum = float4(0, 0, 0, 0); - float partition_weight = 0.0f; - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = ewb->texel_weight[iwt]; - float4 texel_datum = float4(blk->work_data[4 * iwt], - blk->work_data[4 * iwt + 1], - blk->work_data[4 * iwt + 2], - blk->work_data[4 * iwt + 3]) * weight; - partition_weight += weight; - - base_sum = base_sum + texel_datum; - } - - float4 average = base_sum * 1.0f / MAX(partition_weight, 1e-7f); - averages[partition] = average * color_scalefactors[partition]; - - - float4 sum_xp = float4(0, 0, 0, 0); - float4 sum_yp = float4(0, 0, 0, 0); - float4 sum_zp = float4(0, 0, 0, 0); - float4 sum_wp = float4(0, 0, 0, 0); - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = ewb->texel_weight[iwt]; - float4 texel_datum = float4(blk->work_data[4 * iwt], - blk->work_data[4 * iwt + 1], - blk->work_data[4 * iwt + 2], - blk->work_data[4 * iwt + 3]); - texel_datum = (texel_datum - average) * weight; - - if (texel_datum.x > 0.0f) - sum_xp = sum_xp + texel_datum; - if (texel_datum.y > 0.0f) - sum_yp = sum_yp + texel_datum; - if (texel_datum.z > 0.0f) - sum_zp = sum_zp + texel_datum; - if (texel_datum.w > 0.0f) - sum_wp = sum_wp + texel_datum; - } - - float prod_xp = dot(sum_xp, sum_xp); - float prod_yp = dot(sum_yp, sum_yp); - float prod_zp = dot(sum_zp, sum_zp); - float prod_wp = dot(sum_wp, sum_wp); - - float4 best_vector = sum_xp; - float best_sum = prod_xp; - if (prod_yp > best_sum) - { - best_vector = sum_yp; - best_sum = prod_yp; - } - if (prod_zp > best_sum) - { - best_vector = sum_zp; - best_sum = prod_zp; - } - if (prod_wp > best_sum) - { - best_vector = sum_wp; - best_sum = prod_wp; - } - - directions_rgba[partition] = best_vector; - directions_rgb[partition] = best_vector.xyz; - directions_rga[partition] = best_vector.xyw; - directions_rba[partition] = best_vector.xzw; - directions_gba[partition] = best_vector.yzw; - } -} - - - - -void compute_averages_and_directions_rgb(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float4 * color_scalefactors, float3 * averages, float3 * directions_rgb, float2 * directions_rg, float2 * directions_rb, float2 * directions_gb) -{ - int i; - int partition_count = pt->partition_count; - int partition; - - const float *texel_weights = ewb->texel_weight_rgb; - - for (partition = 0; partition < partition_count; partition++) - { - const uint8_t *weights = pt->texels_of_partition[partition]; - int texelcount = pt->texels_per_partition[partition]; - - float3 base_sum = float3(0, 0, 0); - float partition_weight = 0.0f; - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float3 texel_datum = float3(blk->work_data[4 * iwt], - blk->work_data[4 * iwt + 1], - blk->work_data[4 * iwt + 2]) * weight; - partition_weight += weight; - - base_sum = base_sum + texel_datum; - } - - float4 csf = color_scalefactors[partition]; - float3 average = base_sum * 1.0f / MAX(partition_weight, 1e-7f); - averages[partition] = average * csf.xyz; - - - float3 sum_xp = float3(0, 0, 0); - float3 sum_yp = float3(0, 0, 0); - float3 sum_zp = float3(0, 0, 0); - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float3 texel_datum = float3(blk->work_data[4 * iwt], - blk->work_data[4 * iwt + 1], - blk->work_data[4 * iwt + 2]); - texel_datum = (texel_datum - average) * weight; - - if (texel_datum.x > 0.0f) - sum_xp = sum_xp + texel_datum; - if (texel_datum.y > 0.0f) - sum_yp = sum_yp + texel_datum; - if (texel_datum.z > 0.0f) - sum_zp = sum_zp + texel_datum; - } - - float prod_xp = dot(sum_xp, sum_xp); - float prod_yp = dot(sum_yp, sum_yp); - float prod_zp = dot(sum_zp, sum_zp); - - float3 best_vector = sum_xp; - float best_sum = prod_xp; - if (prod_yp > best_sum) - { - best_vector = sum_yp; - best_sum = prod_yp; - } - if (prod_zp > best_sum) - { - best_vector = sum_zp; - best_sum = prod_zp; - } - - directions_rgb[partition] = best_vector; - directions_gb[partition] = best_vector.yz; - directions_rb[partition] = best_vector.xz; - directions_rg[partition] = best_vector.xy; - } -} - -void compute_averages_and_directions_3_components(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float3 * color_scalefactors, int component1, int component2, int component3, float3 * averages, float3 * directions) -{ - int i; - int partition_count = pt->partition_count; - int partition; - - const float *texel_weights; - if (component1 == 1 && component2 == 2 && component3 == 3) - texel_weights = ewb->texel_weight_gba; - else if (component1 == 0 && component2 == 2 && component3 == 3) - texel_weights = ewb->texel_weight_rba; - else if (component1 == 0 && component2 == 1 && component3 == 3) - texel_weights = ewb->texel_weight_rga; - else if (component1 == 0 && component2 == 1 && component3 == 2) - texel_weights = ewb->texel_weight_rgb; - else - { - texel_weights = ewb->texel_weight_gba; - ASTC_CODEC_INTERNAL_ERROR; - } - - - for (partition = 0; partition < partition_count; partition++) - { - const uint8_t *weights = pt->texels_of_partition[partition]; - int texelcount = pt->texels_per_partition[partition]; - - float3 base_sum = float3(0, 0, 0); - float partition_weight = 0.0f; - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float3 texel_datum = float3(blk->work_data[4 * iwt + component1], - blk->work_data[4 * iwt + component2], - blk->work_data[4 * iwt + component3]) * weight; - partition_weight += weight; - - base_sum = base_sum + texel_datum; - } - - float3 csf = color_scalefactors[partition]; - - float3 average = base_sum * 1.0f / MAX(partition_weight, 1e-7f); - averages[partition] = average * csf.xyz; - - - float3 sum_xp = float3(0, 0, 0); - float3 sum_yp = float3(0, 0, 0); - float3 sum_zp = float3(0, 0, 0); - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float3 texel_datum = float3(blk->work_data[4 * iwt + component1], - blk->work_data[4 * iwt + component2], - blk->work_data[4 * iwt + component3]); - texel_datum = (texel_datum - average) * weight; - - if (texel_datum.x > 0.0f) - sum_xp = sum_xp + texel_datum; - if (texel_datum.y > 0.0f) - sum_yp = sum_yp + texel_datum; - if (texel_datum.z > 0.0f) - sum_zp = sum_zp + texel_datum; - } - - float prod_xp = dot(sum_xp, sum_xp); - float prod_yp = dot(sum_yp, sum_yp); - float prod_zp = dot(sum_zp, sum_zp); - - float3 best_vector = sum_xp; - float best_sum = prod_xp; - if (prod_yp > best_sum) - { - best_vector = sum_yp; - best_sum = prod_yp; - } - if (prod_zp > best_sum) - { - best_vector = sum_zp; - best_sum = prod_zp; - } - - if (dot(best_vector, best_vector) < 1e-18) - best_vector = float3(1, 1, 1); - directions[partition] = best_vector; - } - -} - - - - -void compute_averages_and_directions_2_components(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, const float2 * color_scalefactors, int component1, int component2, float2 * averages, float2 * directions) -{ - int i; - int partition_count = pt->partition_count; - int partition; - - const float *texel_weights; - if (component1 == 0 && component2 == 1) - texel_weights = ewb->texel_weight_rg; - else if (component1 == 0 && component2 == 2) - texel_weights = ewb->texel_weight_rb; - else if (component1 == 1 && component2 == 2) - texel_weights = ewb->texel_weight_gb; - else - { - texel_weights = ewb->texel_weight_rg; - // unsupported set of color components. - ASTC_CODEC_INTERNAL_ERROR; - exit(1); - } - - - for (partition = 0; partition < partition_count; partition++) - { - const uint8_t *weights = pt->texels_of_partition[partition]; - int texelcount = pt->texels_per_partition[partition]; - - float2 base_sum = float2(0, 0); - float partition_weight = 0.0f; - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float2 texel_datum = float2(blk->work_data[4 * iwt + component1], - blk->work_data[4 * iwt + component2]) * weight; - partition_weight += weight; - - base_sum = base_sum + texel_datum; - } - - float2 csf = color_scalefactors[partition]; - - float2 average = base_sum * 1.0f / MAX(partition_weight, 1e-7f); - averages[partition] = average * csf.xy; - - - float2 sum_xp = float2(0, 0); - float2 sum_yp = float2(0, 0); - - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float weight = texel_weights[iwt]; - float2 texel_datum = float2(blk->work_data[4 * iwt + component1], - blk->work_data[4 * iwt + component2]); - texel_datum = (texel_datum - average) * weight; - - if (texel_datum.x > 0.0f) - sum_xp = sum_xp + texel_datum; - if (texel_datum.y > 0.0f) - sum_yp = sum_yp + texel_datum; - } - - float prod_xp = dot(sum_xp, sum_xp); - float prod_yp = dot(sum_yp, sum_yp); - - float2 best_vector = sum_xp; - float best_sum = prod_xp; - if (prod_yp > best_sum) - { - best_vector = sum_yp; - best_sum = prod_yp; - } - - directions[partition] = best_vector; - } - -} - - -#define XPASTE(x,y) x##y -#define PASTE(x,y) XPASTE(x,y) - -#define TWO_COMPONENT_ERROR_FUNC( funcname, c0_iwt, c1_iwt, c01_name, c01_rname ) \ -float funcname( \ - const partition_info *pt, \ - const imageblock *blk, \ - const error_weight_block *ewb, \ - const processed_line2 *plines, \ - float *length_of_lines \ - ) \ - { \ - int i; \ - float errorsum = 0.0f; \ - int partition; \ - for(partition=0; partition<pt->partition_count; partition++) \ - { \ - const uint8_t *weights = pt->texels_of_partition[ partition ]; \ - int texelcount = pt->texels_per_partition[ partition ]; \ - float lowparam = 1e10f; \ - float highparam = -1e10f; \ - processed_line2 l = plines[partition]; \ - if( ewb->contains_zeroweight_texels ) \ - { \ - for(i=0;i<texelcount;i++) \ - { \ - int iwt = weights[i]; \ - float texel_weight = ewb-> PASTE(texel_weight_ , c01_rname) [i]; \ - if( texel_weight > 1e-20f ) \ - { \ - float2 point = float2(blk->work_data[4*iwt + c0_iwt], blk->work_data[4*iwt + c1_iwt] ); \ - float param = dot( point, l.bs ); \ - float2 rp1 = l.amod + param*l.bis; \ - float2 dist = rp1 - point; \ - float4 ews = ewb->error_weights[iwt]; \ - errorsum += dot( ews. c01_name, dist*dist ); \ - if( param < lowparam ) lowparam = param; \ - if( param > highparam ) highparam = param; \ - } \ - } \ - } \ - else \ - { \ - for(i=0;i<texelcount;i++) \ - { \ - int iwt = weights[i]; \ - float2 point = float2(blk->work_data[4*iwt + c0_iwt], blk->work_data[4*iwt + c1_iwt] ); \ - float param = dot( point, l.bs ); \ - float2 rp1 = l.amod + param*l.bis; \ - float2 dist = rp1 - point; \ - float4 ews = ewb->error_weights[iwt]; \ - errorsum += dot( ews. c01_name, dist*dist ); \ - if( param < lowparam ) lowparam = param; \ - if( param > highparam ) highparam = param; \ - } \ - } \ - float linelen = highparam - lowparam; \ - if( !(linelen > 1e-7f) ) \ - linelen = 1e-7f; \ - length_of_lines[partition] = linelen; \ - } \ - return errorsum; \ - } - - -TWO_COMPONENT_ERROR_FUNC(compute_error_squared_rg, 0, 1, xy, rg) -TWO_COMPONENT_ERROR_FUNC(compute_error_squared_rb, 0, 2, xz, rb) -TWO_COMPONENT_ERROR_FUNC(compute_error_squared_gb, 1, 2, yz, gb) -TWO_COMPONENT_ERROR_FUNC(compute_error_squared_ra, 0, 3, zw, ra) - -// function to compute the error across a tile when using a particular set of -// lines for a particular partitioning. Also compute the length of each -// color-space line in each partitioning. - -#define THREE_COMPONENT_ERROR_FUNC( funcname, c0_iwt, c1_iwt, c2_iwt, c012_name, c012_rname ) \ -float funcname( \ - const partition_info *pt, \ - const imageblock *blk, \ - const error_weight_block *ewb, \ - const processed_line3 *plines, \ - float *length_of_lines \ - ) \ - { \ - int i; \ - float errorsum = 0.0f; \ - int partition; \ - for(partition=0; partition<pt->partition_count; partition++) \ - { \ - const uint8_t *weights = pt->texels_of_partition[ partition ]; \ - int texelcount = pt->texels_per_partition[ partition ]; \ - float lowparam = 1e10f; \ - float highparam = -1e10f; \ - processed_line3 l = plines[partition]; \ - if( ewb->contains_zeroweight_texels ) \ - { \ - for(i=0;i<texelcount;i++) \ - { \ - int iwt = weights[i]; \ - float texel_weight = ewb-> PASTE(texel_weight_ , c012_rname) [i]; \ - if( texel_weight > 1e-20f ) \ - { \ - float3 point = float3(blk->work_data[4*iwt + c0_iwt], blk->work_data[4*iwt + c1_iwt], blk->work_data[4*iwt + c2_iwt] ); \ - float param = dot( point, l.bs ); \ - float3 rp1 = l.amod + param*l.bis; \ - float3 dist = rp1 - point; \ - float4 ews = ewb->error_weights[iwt]; \ - errorsum += dot( ews. c012_name, dist*dist ); \ - if( param < lowparam ) lowparam = param; \ - if( param > highparam ) highparam = param; \ - } \ - } \ - } \ - else \ - { \ - for(i=0;i<texelcount;i++) \ - { \ - int iwt = weights[i]; \ - float3 point = float3(blk->work_data[4*iwt + c0_iwt], blk->work_data[4*iwt + c1_iwt], blk->work_data[4*iwt + c2_iwt] ); \ - float param = dot( point, l.bs ); \ - float3 rp1 = l.amod + param*l.bis; \ - float3 dist = rp1 - point; \ - float4 ews = ewb->error_weights[iwt]; \ - errorsum += dot( ews. c012_name, dist*dist ); \ - if( param < lowparam ) lowparam = param; \ - if( param > highparam ) highparam = param; \ - } \ - } \ - float linelen = highparam - lowparam; \ - if( !(linelen > 1e-7f) ) \ - linelen = 1e-7f; \ - length_of_lines[partition] = linelen; \ - } \ - return errorsum; \ - } - -THREE_COMPONENT_ERROR_FUNC(compute_error_squared_gba, 1, 2, 3, yzw, gba) -THREE_COMPONENT_ERROR_FUNC(compute_error_squared_rba, 0, 2, 3, xzw, rba) -THREE_COMPONENT_ERROR_FUNC(compute_error_squared_rga, 0, 1, 3, xyw, rga) -THREE_COMPONENT_ERROR_FUNC(compute_error_squared_rgb, 0, 1, 2, xyz, rgb) - -float compute_error_squared_rgba(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line4 * plines, float *length_of_lines) -{ - int i; - - float errorsum = 0.0f; - int partition; - for (partition = 0; partition < pt->partition_count; partition++) - { - const uint8_t *weights = pt->texels_of_partition[partition]; - int texelcount = pt->texels_per_partition[partition]; - float lowparam = 1e10; - float highparam = -1e10; - - processed_line4 l = plines[partition]; - - if (ewb->contains_zeroweight_texels) - { - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - if (ewb->texel_weight[iwt] > 1e-20) - { - float4 point = float4(blk->work_data[4 * iwt], blk->work_data[4 * iwt + 1], blk->work_data[4 * iwt + 2], blk->work_data[4 * iwt + 3]); - float param = dot(point, l.bs); - float4 rp1 = l.amod + param * l.bis; - float4 dist = rp1 - point; - float4 ews = ewb->error_weights[iwt]; - errorsum += dot(ews, dist * dist); - if (param < lowparam) - lowparam = param; - if (param > highparam) - highparam = param; - } - } - } - else - { - for (i = 0; i < texelcount; i++) - { - int iwt = weights[i]; - float4 point = float4(blk->work_data[4 * iwt], blk->work_data[4 * iwt + 1], blk->work_data[4 * iwt + 2], blk->work_data[4 * iwt + 3]); - float param = dot(point, l.bs); - float4 rp1 = l.amod + param * l.bis; - float4 dist = rp1 - point; - float4 ews = ewb->error_weights[iwt]; - errorsum += dot(ews, dist * dist); - if (param < lowparam) - lowparam = param; - if (param > highparam) - highparam = param; - } - } - - float linelen = highparam - lowparam; - if (!(linelen > 1e-7f)) - linelen = 1e-7f; - length_of_lines[partition] = linelen; - } - - return errorsum; -} - - - -// function to compute the error across a tile when using a particular line for -// a particular partition. -float compute_error_squared_rgb_single_partition(int partition_to_test, int xdim, int ydim, int zdim, const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * lin // the line for the partition. - ) -{ - int i; - - int texels_per_block = xdim * ydim * zdim; - - float errorsum = 0.0f; - - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - float texel_weight = ewb->texel_weight_rgb[i]; - if (partition != partition_to_test || texel_weight < 1e-20) - continue; - float3 point = float3(blk->work_data[4 * i], blk->work_data[4 * i + 1], blk->work_data[4 * i + 2]); - - float param = dot(point, lin->bs); - float3 rp1 = lin->amod + param * lin->bis; - float3 dist = rp1 - point; - float4 ews = ewb->error_weights[i]; - - errorsum += dot(ews.xyz, dist * dist); - } - return errorsum; -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_block_sizes2.cpp b/3rdparty/bimg/3rdparty/astc/astc_block_sizes2.cpp deleted file mode 100644 index 8d42d71c35b..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_block_sizes2.cpp +++ /dev/null @@ -1,977 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief For ASTC, generate the block size descriptor and the associated - * decimation tables. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -extern const float percentile_table_4x4[2048]; -extern const float percentile_table_4x5[2048]; -extern const float percentile_table_4x6[2048]; -extern const float percentile_table_4x8[2048]; -extern const float percentile_table_4x10[2048]; -extern const float percentile_table_4x12[2048]; -extern const float percentile_table_5x4[2048]; -extern const float percentile_table_5x5[2048]; -extern const float percentile_table_5x6[2048]; -extern const float percentile_table_5x8[2048]; -extern const float percentile_table_5x10[2048]; -extern const float percentile_table_5x12[2048]; -extern const float percentile_table_6x4[2048]; -extern const float percentile_table_6x5[2048]; -extern const float percentile_table_6x6[2048]; -extern const float percentile_table_6x8[2048]; -extern const float percentile_table_6x10[2048]; -extern const float percentile_table_6x12[2048]; -extern const float percentile_table_8x4[2048]; -extern const float percentile_table_8x5[2048]; -extern const float percentile_table_8x6[2048]; -extern const float percentile_table_8x8[2048]; -extern const float percentile_table_8x10[2048]; -extern const float percentile_table_8x12[2048]; -extern const float percentile_table_10x4[2048]; -extern const float percentile_table_10x5[2048]; -extern const float percentile_table_10x6[2048]; -extern const float percentile_table_10x8[2048]; -extern const float percentile_table_10x10[2048]; -extern const float percentile_table_10x12[2048]; -extern const float percentile_table_12x4[2048]; -extern const float percentile_table_12x5[2048]; -extern const float percentile_table_12x6[2048]; -extern const float percentile_table_12x8[2048]; -extern const float percentile_table_12x10[2048]; -extern const float percentile_table_12x12[2048]; - -const float *get_2d_percentile_table(int blockdim_x, int blockdim_y) -{ - switch (blockdim_x) - { - case 4: - switch (blockdim_y) - { - case 4: - return percentile_table_4x4; - case 5: - return percentile_table_4x5; - case 6: - return percentile_table_4x6; - case 8: - return percentile_table_4x8; - case 10: - return percentile_table_4x10; - case 12: - return percentile_table_4x12; - } - break; - case 5: - switch (blockdim_y) - { - case 4: - return percentile_table_5x4; - case 5: - return percentile_table_5x5; - case 6: - return percentile_table_5x6; - case 8: - return percentile_table_5x8; - case 10: - return percentile_table_5x10; - case 12: - return percentile_table_5x12; - } - break; - - case 6: - switch (blockdim_y) - { - case 4: - return percentile_table_6x4; - case 5: - return percentile_table_6x5; - case 6: - return percentile_table_6x6; - case 8: - return percentile_table_6x8; - case 10: - return percentile_table_6x10; - case 12: - return percentile_table_6x12; - } - break; - - case 8: - switch (blockdim_y) - { - case 4: - return percentile_table_8x4; - case 5: - return percentile_table_8x5; - case 6: - return percentile_table_8x6; - case 8: - return percentile_table_8x8; - case 10: - return percentile_table_8x10; - case 12: - return percentile_table_8x12; - } - break; - - case 10: - switch (blockdim_y) - { - case 4: - return percentile_table_10x4; - case 5: - return percentile_table_10x5; - case 6: - return percentile_table_10x6; - case 8: - return percentile_table_10x8; - case 10: - return percentile_table_10x10; - case 12: - return percentile_table_10x12; - } - break; - - case 12: - switch (blockdim_y) - { - case 4: - return percentile_table_12x4; - case 5: - return percentile_table_12x5; - case 6: - return percentile_table_12x6; - case 8: - return percentile_table_12x8; - case 10: - return percentile_table_12x10; - case 12: - return percentile_table_12x12; - } - break; - default: - break; - } - - return NULL; // should never happen. -} - -// stubbed for the time being. -static const float dummy_percentile_table_3d[2048] = { 0 }; -const float *get_3d_percentile_table(int blockdim_x, int blockdim_y, int blockdim_z) -{ - IGNORE(blockdim_x); - IGNORE(blockdim_y); - IGNORE(blockdim_z); - return dummy_percentile_table_3d; -} - - - -// return 0 on invalid mode, 1 on valid mode. -static int decode_block_mode_2d(int blockmode, int *Nval, int *Mval, int *dual_weight_plane, int *quant_mode) -{ - int base_quant_mode = (blockmode >> 4) & 1; - int H = (blockmode >> 9) & 1; - int D = (blockmode >> 10) & 1; - - int A = (blockmode >> 5) & 0x3; - - int N = 0, M = 0; - - if ((blockmode & 3) != 0) - { - base_quant_mode |= (blockmode & 3) << 1; - int B = (blockmode >> 7) & 3; - switch ((blockmode >> 2) & 3) - { - case 0: - N = B + 4; - M = A + 2; - break; - case 1: - N = B + 8; - M = A + 2; - break; - case 2: - N = A + 2; - M = B + 8; - break; - case 3: - B &= 1; - if (blockmode & 0x100) - { - N = B + 2; - M = A + 2; - } - else - { - N = A + 2; - M = B + 6; - } - break; - } - } - else - { - base_quant_mode |= ((blockmode >> 2) & 3) << 1; - if (((blockmode >> 2) & 3) == 0) - return 0; - int B = (blockmode >> 9) & 3; - switch ((blockmode >> 7) & 3) - { - case 0: - N = 12; - M = A + 2; - break; - case 1: - N = A + 2; - M = 12; - break; - case 2: - N = A + 6; - M = B + 6; - D = 0; - H = 0; - break; - case 3: - switch ((blockmode >> 5) & 3) - { - case 0: - N = 6; - M = 10; - break; - case 1: - N = 10; - M = 6; - break; - case 2: - case 3: - return 0; - } - break; - } - } - - int weight_count = N * M * (D + 1); - int qmode = (base_quant_mode - 2) + 6 * H; - - int weightbits = compute_ise_bitcount(weight_count, (quantization_method) qmode); - if (weight_count > MAX_WEIGHTS_PER_BLOCK || weightbits < MIN_WEIGHT_BITS_PER_BLOCK || weightbits > MAX_WEIGHT_BITS_PER_BLOCK) - return 0; - - *Nval = N; - *Mval = M; - *dual_weight_plane = D; - *quant_mode = qmode; - return 1; -} - - -static int decode_block_mode_3d(int blockmode, int *Nval, int *Mval, int *Qval, int *dual_weight_plane, int *quant_mode) -{ - int base_quant_mode = (blockmode >> 4) & 1; - int H = (blockmode >> 9) & 1; - int D = (blockmode >> 10) & 1; - - int A = (blockmode >> 5) & 0x3; - - int N = 0, M = 0, Q = 0; - - if ((blockmode & 3) != 0) - { - base_quant_mode |= (blockmode & 3) << 1; - int B = (blockmode >> 7) & 3; - int C = (blockmode >> 2) & 0x3; - N = A + 2; - M = B + 2; - Q = C + 2; - } - else - { - base_quant_mode |= ((blockmode >> 2) & 3) << 1; - if (((blockmode >> 2) & 3) == 0) - return 0; - int B = (blockmode >> 9) & 3; - if (((blockmode >> 7) & 3) != 3) - { - D = 0; - H = 0; - } - switch ((blockmode >> 7) & 3) - { - case 0: - N = 6; - M = B + 2; - Q = A + 2; - break; - case 1: - N = A + 2; - M = 6; - Q = B + 2; - break; - case 2: - N = A + 2; - M = B + 2; - Q = 6; - break; - case 3: - N = 2; - M = 2; - Q = 2; - switch ((blockmode >> 5) & 3) - { - case 0: - N = 6; - break; - case 1: - M = 6; - break; - case 2: - Q = 6; - break; - case 3: - return 0; - } - break; - } - } - - int weight_count = N * M * Q * (D + 1); - int qmode = (base_quant_mode - 2) + 6 * H; - - int weightbits = compute_ise_bitcount(weight_count, (quantization_method) qmode); - if (weight_count > MAX_WEIGHTS_PER_BLOCK || weightbits < MIN_WEIGHT_BITS_PER_BLOCK || weightbits > MAX_WEIGHT_BITS_PER_BLOCK) - return 0; - - *Nval = N; - *Mval = M; - *Qval = Q; - *dual_weight_plane = D; - *quant_mode = qmode; - return 1; -} - - - - -static void initialize_decimation_table_2d( - // dimensions of the block - int xdim, int ydim, - // number of grid points in 2d weight grid - int x_weights, int y_weights, decimation_table * dt) -{ - int i, j; - int x, y; - - int texels_per_block = xdim * ydim; - int weights_per_block = x_weights * y_weights; - - int weightcount_of_texel[MAX_TEXELS_PER_BLOCK]; - int grid_weights_of_texel[MAX_TEXELS_PER_BLOCK][4]; - int weights_of_texel[MAX_TEXELS_PER_BLOCK][4]; - - int texelcount_of_weight[MAX_WEIGHTS_PER_BLOCK]; - int texels_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; - int texelweights_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; - - for (i = 0; i < weights_per_block; i++) - texelcount_of_weight[i] = 0; - for (i = 0; i < texels_per_block; i++) - weightcount_of_texel[i] = 0; - - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - int texel = y * xdim + x; - - int x_weight = (((1024 + xdim / 2) / (xdim - 1)) * x * (x_weights - 1) + 32) >> 6; - int y_weight = (((1024 + ydim / 2) / (ydim - 1)) * y * (y_weights - 1) + 32) >> 6; - - int x_weight_frac = x_weight & 0xF; - int y_weight_frac = y_weight & 0xF; - int x_weight_int = x_weight >> 4; - int y_weight_int = y_weight >> 4; - int qweight[4]; - int weight[4]; - qweight[0] = x_weight_int + y_weight_int * x_weights; - qweight[1] = qweight[0] + 1; - qweight[2] = qweight[0] + x_weights; - qweight[3] = qweight[2] + 1; - - // truncated-precision bilinear interpolation. - int prod = x_weight_frac * y_weight_frac; - - weight[3] = (prod + 8) >> 4; - weight[1] = x_weight_frac - weight[3]; - weight[2] = y_weight_frac - weight[3]; - weight[0] = 16 - x_weight_frac - y_weight_frac + weight[3]; - - for (i = 0; i < 4; i++) - if (weight[i] != 0) - { - grid_weights_of_texel[texel][weightcount_of_texel[texel]] = qweight[i]; - weights_of_texel[texel][weightcount_of_texel[texel]] = weight[i]; - weightcount_of_texel[texel]++; - texels_of_weight[qweight[i]][texelcount_of_weight[qweight[i]]] = texel; - texelweights_of_weight[qweight[i]][texelcount_of_weight[qweight[i]]] = weight[i]; - texelcount_of_weight[qweight[i]]++; - } - } - - for (i = 0; i < texels_per_block; i++) - { - dt->texel_num_weights[i] = weightcount_of_texel[i]; - - // ensure that all 4 entries are actually initialized. - // This allows a branch-free implementation of compute_value_of_texel_flt() - for (j = 0; j < 4; j++) - { - dt->texel_weights_int[i][j] = 0; - dt->texel_weights_float[i][j] = 0.0f; - dt->texel_weights[i][j] = 0; - } - - for (j = 0; j < weightcount_of_texel[i]; j++) - { - dt->texel_weights_int[i][j] = weights_of_texel[i][j]; - dt->texel_weights_float[i][j] = static_cast < float >(weights_of_texel[i][j]) * (1.0f / TEXEL_WEIGHT_SUM); - dt->texel_weights[i][j] = grid_weights_of_texel[i][j]; - } - } - - for (i = 0; i < weights_per_block; i++) - { - dt->weight_num_texels[i] = texelcount_of_weight[i]; - - - for (j = 0; j < texelcount_of_weight[i]; j++) - { - dt->weight_texel[i][j] = texels_of_weight[i][j]; - dt->weights_int[i][j] = texelweights_of_weight[i][j]; - dt->weights_flt[i][j] = static_cast < float >(texelweights_of_weight[i][j]); - } - } - - dt->num_texels = texels_per_block; - dt->num_weights = weights_per_block; - - -} - - - - -static void initialize_decimation_table_3d( - // dimensions of the block - int xdim, int ydim, int zdim, - // number of grid points in 3d weight grid - int x_weights, int y_weights, int z_weights, decimation_table * dt) -{ - int i, j; - int x, y, z; - - int texels_per_block = xdim * ydim * zdim; - int weights_per_block = x_weights * y_weights * z_weights; - - int weightcount_of_texel[MAX_TEXELS_PER_BLOCK]; - int grid_weights_of_texel[MAX_TEXELS_PER_BLOCK][4]; - int weights_of_texel[MAX_TEXELS_PER_BLOCK][4]; - - int texelcount_of_weight[MAX_WEIGHTS_PER_BLOCK]; - int texels_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; - int texelweights_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; - - for (i = 0; i < weights_per_block; i++) - texelcount_of_weight[i] = 0; - for (i = 0; i < texels_per_block; i++) - weightcount_of_texel[i] = 0; - - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - int texel = (z * ydim + y) * xdim + x; - - int x_weight = (((1024 + xdim / 2) / (xdim - 1)) * x * (x_weights - 1) + 32) >> 6; - int y_weight = (((1024 + ydim / 2) / (ydim - 1)) * y * (y_weights - 1) + 32) >> 6; - int z_weight = (((1024 + zdim / 2) / (zdim - 1)) * z * (z_weights - 1) + 32) >> 6; - - int x_weight_frac = x_weight & 0xF; - int y_weight_frac = y_weight & 0xF; - int z_weight_frac = z_weight & 0xF; - int x_weight_int = x_weight >> 4; - int y_weight_int = y_weight >> 4; - int z_weight_int = z_weight >> 4; - int qweight[4]; - int weight[4]; - qweight[0] = (z_weight_int * y_weights + y_weight_int) * x_weights + x_weight_int; - qweight[3] = ((z_weight_int + 1) * y_weights + (y_weight_int + 1)) * x_weights + (x_weight_int + 1); - - // simplex interpolation - int fs = x_weight_frac; - int ft = y_weight_frac; - int fp = z_weight_frac; - - int cas = ((fs > ft) << 2) + ((ft > fp) << 1) + ((fs > fp)); - int N = x_weights; - int NM = x_weights * y_weights; - - int s1, s2, w0, w1, w2, w3; - switch (cas) - { - case 7: - s1 = 1; - s2 = N; - w0 = 16 - fs; - w1 = fs - ft; - w2 = ft - fp; - w3 = fp; - break; - case 3: - s1 = N; - s2 = 1; - w0 = 16 - ft; - w1 = ft - fs; - w2 = fs - fp; - w3 = fp; - break; - case 5: - s1 = 1; - s2 = NM; - w0 = 16 - fs; - w1 = fs - fp; - w2 = fp - ft; - w3 = ft; - break; - case 4: - s1 = NM; - s2 = 1; - w0 = 16 - fp; - w1 = fp - fs; - w2 = fs - ft; - w3 = ft; - break; - case 2: - s1 = N; - s2 = NM; - w0 = 16 - ft; - w1 = ft - fp; - w2 = fp - fs; - w3 = fs; - break; - case 0: - s1 = NM; - s2 = N; - w0 = 16 - fp; - w1 = fp - ft; - w2 = ft - fs; - w3 = fs; - break; - - default: - s1 = NM; - s2 = N; - w0 = 16 - fp; - w1 = fp - ft; - w2 = ft - fs; - w3 = fs; - break; - } - - qweight[1] = qweight[0] + s1; - qweight[2] = qweight[1] + s2; - weight[0] = w0; - weight[1] = w1; - weight[2] = w2; - weight[3] = w3; - - /* - for(i=0;i<4;i++) weight[i] <<= 4; */ - - for (i = 0; i < 4; i++) - if (weight[i] != 0) - { - grid_weights_of_texel[texel][weightcount_of_texel[texel]] = qweight[i]; - weights_of_texel[texel][weightcount_of_texel[texel]] = weight[i]; - weightcount_of_texel[texel]++; - texels_of_weight[qweight[i]][texelcount_of_weight[qweight[i]]] = texel; - texelweights_of_weight[qweight[i]][texelcount_of_weight[qweight[i]]] = weight[i]; - texelcount_of_weight[qweight[i]]++; - } - } - - for (i = 0; i < texels_per_block; i++) - { - dt->texel_num_weights[i] = weightcount_of_texel[i]; - - // ensure that all 4 entries are actually initialized. - // This allows a branch-free implementation of compute_value_of_texel_flt() - for (j = 0; j < 4; j++) - { - dt->texel_weights_int[i][j] = 0; - dt->texel_weights_float[i][j] = 0.0f; - dt->texel_weights[i][j] = 0; - } - - for (j = 0; j < weightcount_of_texel[i]; j++) - { - dt->texel_weights_int[i][j] = weights_of_texel[i][j]; - dt->texel_weights_float[i][j] = static_cast < float >(weights_of_texel[i][j]) * (1.0f / TEXEL_WEIGHT_SUM); - dt->texel_weights[i][j] = grid_weights_of_texel[i][j]; - } - } - - for (i = 0; i < weights_per_block; i++) - { - dt->weight_num_texels[i] = texelcount_of_weight[i]; - for (j = 0; j < texelcount_of_weight[i]; j++) - { - dt->weight_texel[i][j] = texels_of_weight[i][j]; - dt->weights_int[i][j] = texelweights_of_weight[i][j]; - dt->weights_flt[i][j] = static_cast < float >(texelweights_of_weight[i][j]); - } - } - - dt->num_texels = texels_per_block; - dt->num_weights = weights_per_block; -} - - - -void construct_block_size_descriptor_2d(int xdim, int ydim, block_size_descriptor * bsd) -{ - int decimation_mode_index[256]; // for each of the 256 entries in the decim_table_array, its index - int decimation_mode_count = 0; - - int i; - int x_weights; - int y_weights; - - for (i = 0; i < 256; i++) - { - decimation_mode_index[i] = -1; - } - - // gather all the infill-modes that can be used with the current block size - for (x_weights = 2; x_weights <= 12; x_weights++) - for (y_weights = 2; y_weights <= 12; y_weights++) - { - if (x_weights * y_weights > MAX_WEIGHTS_PER_BLOCK) - continue; - decimation_table *dt = new decimation_table; - decimation_mode_index[y_weights * 16 + x_weights] = decimation_mode_count; - initialize_decimation_table_2d(xdim, ydim, x_weights, y_weights, dt); - - int weight_count = x_weights * y_weights; - - int maxprec_1plane = -1; - int maxprec_2planes = -1; - for (i = 0; i < 12; i++) - { - int bits_1plane = compute_ise_bitcount(weight_count, (quantization_method) i); - int bits_2planes = compute_ise_bitcount(2 * weight_count, (quantization_method) i); - if (bits_1plane >= MIN_WEIGHT_BITS_PER_BLOCK && bits_1plane <= MAX_WEIGHT_BITS_PER_BLOCK) - maxprec_1plane = i; - if (bits_2planes >= MIN_WEIGHT_BITS_PER_BLOCK && bits_2planes <= MAX_WEIGHT_BITS_PER_BLOCK) - maxprec_2planes = i; - } - - if (2 * x_weights * y_weights > MAX_WEIGHTS_PER_BLOCK) - maxprec_2planes = -1; - - bsd->permit_encode[decimation_mode_count] = (x_weights <= xdim && y_weights <= ydim); - - bsd->decimation_mode_samples[decimation_mode_count] = weight_count; - bsd->decimation_mode_maxprec_1plane[decimation_mode_count] = maxprec_1plane; - bsd->decimation_mode_maxprec_2planes[decimation_mode_count] = maxprec_2planes; - bsd->decimation_tables[decimation_mode_count] = dt; - - decimation_mode_count++; - } - - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - bsd->decimation_mode_percentile[i] = 1.0f; - } - - for (i = decimation_mode_count; i < MAX_DECIMATION_MODES; i++) - { - bsd->permit_encode[i] = 0; - bsd->decimation_mode_samples[i] = 0; - bsd->decimation_mode_maxprec_1plane[i] = -1; - bsd->decimation_mode_maxprec_2planes[i] = -1; - } - - bsd->decimation_mode_count = decimation_mode_count; - - const float *percentiles = get_2d_percentile_table(xdim, ydim); - - // then construct the list of block formats - for (i = 0; i < 2048; i++) - { - int x_weights, y_weights; - int is_dual_plane; - int quantization_mode; - int fail = 0; - int permit_encode = 1; - - if (decode_block_mode_2d(i, &x_weights, &y_weights, &is_dual_plane, &quantization_mode)) - { - if (x_weights > xdim || y_weights > ydim) - permit_encode = 0; - } - else - { - fail = 1; - permit_encode = 0; - } - - if (fail) - { - bsd->block_modes[i].decimation_mode = -1; - bsd->block_modes[i].quantization_mode = -1; - bsd->block_modes[i].is_dual_plane = -1; - bsd->block_modes[i].permit_encode = 0; - bsd->block_modes[i].permit_decode = 0; - bsd->block_modes[i].percentile = 1.0f; - } - else - { - int decimation_mode = decimation_mode_index[y_weights * 16 + x_weights]; - bsd->block_modes[i].decimation_mode = decimation_mode; - bsd->block_modes[i].quantization_mode = quantization_mode; - bsd->block_modes[i].is_dual_plane = is_dual_plane; - bsd->block_modes[i].permit_encode = permit_encode; - bsd->block_modes[i].permit_decode = permit_encode; // disallow decode of grid size larger than block size. - bsd->block_modes[i].percentile = percentiles[i]; - - if (bsd->decimation_mode_percentile[decimation_mode] > percentiles[i]) - bsd->decimation_mode_percentile[decimation_mode] = percentiles[i]; - } - - } - - if (xdim * ydim <= 64) - { - bsd->texelcount_for_bitmap_partitioning = xdim * ydim; - for (i = 0; i < xdim * ydim; i++) - bsd->texels_for_bitmap_partitioning[i] = i; - } - - else - { - // pick 64 random texels for use with bitmap partitioning. - int arr[MAX_TEXELS_PER_BLOCK]; - for (i = 0; i < xdim * ydim; i++) - arr[i] = 0; - int arr_elements_set = 0; - while (arr_elements_set < 64) - { - int idx = rand() % (xdim * ydim); - if (arr[idx] == 0) - { - arr_elements_set++; - arr[idx] = 1; - } - } - int texel_weights_written = 0; - int idx = 0; - while (texel_weights_written < 64) - { - if (arr[idx]) - bsd->texels_for_bitmap_partitioning[texel_weights_written++] = idx; - idx++; - } - bsd->texelcount_for_bitmap_partitioning = 64; - - } -} - - - -void construct_block_size_descriptor_3d(int xdim, int ydim, int zdim, block_size_descriptor * bsd) -{ - int decimation_mode_index[512]; // for each of the 512 entries in the decim_table_array, its index - int decimation_mode_count = 0; - - int i; - int x_weights; - int y_weights; - int z_weights; - - for (i = 0; i < 512; i++) - { - decimation_mode_index[i] = -1; - } - - // gather all the infill-modes that can be used with the current block size - for (x_weights = 2; x_weights <= 6; x_weights++) - for (y_weights = 2; y_weights <= 6; y_weights++) - for (z_weights = 2; z_weights <= 6; z_weights++) - { - if ((x_weights * y_weights * z_weights) > MAX_WEIGHTS_PER_BLOCK) - continue; - decimation_table *dt = new decimation_table; - decimation_mode_index[z_weights * 64 + y_weights * 8 + x_weights] = decimation_mode_count; - initialize_decimation_table_3d(xdim, ydim, zdim, x_weights, y_weights, z_weights, dt); - - int weight_count = x_weights * y_weights * z_weights; - - int maxprec_1plane = -1; - int maxprec_2planes = -1; - for (i = 0; i < 12; i++) - { - int bits_1plane = compute_ise_bitcount(weight_count, (quantization_method) i); - int bits_2planes = compute_ise_bitcount(2 * weight_count, (quantization_method) i); - if (bits_1plane >= MIN_WEIGHT_BITS_PER_BLOCK && bits_1plane <= MAX_WEIGHT_BITS_PER_BLOCK) - maxprec_1plane = i; - if (bits_2planes >= MIN_WEIGHT_BITS_PER_BLOCK && bits_2planes <= MAX_WEIGHT_BITS_PER_BLOCK) - maxprec_2planes = i; - } - - if ((2 * x_weights * y_weights * z_weights) > MAX_WEIGHTS_PER_BLOCK) - maxprec_2planes = -1; - - bsd->permit_encode[decimation_mode_count] = (x_weights <= xdim && y_weights <= ydim && z_weights <= zdim); - - bsd->decimation_mode_samples[decimation_mode_count] = weight_count; - bsd->decimation_mode_maxprec_1plane[decimation_mode_count] = maxprec_1plane; - bsd->decimation_mode_maxprec_2planes[decimation_mode_count] = maxprec_2planes; - bsd->decimation_tables[decimation_mode_count] = dt; - - decimation_mode_count++; - } - - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - bsd->decimation_mode_percentile[i] = 1.0f; - } - - for (i = decimation_mode_count; i < MAX_DECIMATION_MODES; i++) - { - bsd->permit_encode[i] = 0; - bsd->decimation_mode_samples[i] = 0; - bsd->decimation_mode_maxprec_1plane[i] = -1; - bsd->decimation_mode_maxprec_2planes[i] = -1; - } - - bsd->decimation_mode_count = decimation_mode_count; - - const float *percentiles = get_3d_percentile_table(xdim, ydim, zdim); - - // then construct the list of block formats - for (i = 0; i < 2048; i++) - { - int x_weights, y_weights, z_weights; - int is_dual_plane; - int quantization_mode; - int fail = 0; - int permit_encode = 1; - - if (decode_block_mode_3d(i, &x_weights, &y_weights, &z_weights, &is_dual_plane, &quantization_mode)) - { - if (x_weights > xdim || y_weights > ydim || z_weights > zdim) - permit_encode = 0; - } - else - { - fail = 1; - permit_encode = 0; - } - if (fail) - { - bsd->block_modes[i].decimation_mode = -1; - bsd->block_modes[i].quantization_mode = -1; - bsd->block_modes[i].is_dual_plane = -1; - bsd->block_modes[i].permit_encode = 0; - bsd->block_modes[i].permit_decode = 0; - bsd->block_modes[i].percentile = 1.0f; - } - else - { - int decimation_mode = decimation_mode_index[z_weights * 64 + y_weights * 8 + x_weights]; - bsd->block_modes[i].decimation_mode = decimation_mode; - bsd->block_modes[i].quantization_mode = quantization_mode; - bsd->block_modes[i].is_dual_plane = is_dual_plane; - bsd->block_modes[i].permit_encode = permit_encode; - bsd->block_modes[i].permit_decode = permit_encode; - bsd->block_modes[i].percentile = percentiles[i]; - - if (bsd->decimation_mode_percentile[decimation_mode] > percentiles[i]) - bsd->decimation_mode_percentile[decimation_mode] = percentiles[i]; - } - - } - - if (xdim * ydim * zdim <= 64) - { - bsd->texelcount_for_bitmap_partitioning = xdim * ydim * zdim; - for (i = 0; i < xdim * ydim * zdim; i++) - bsd->texels_for_bitmap_partitioning[i] = i; - } - - else - { - // pick 64 random texels for use with bitmap partitioning. - int arr[MAX_TEXELS_PER_BLOCK]; - for (i = 0; i < xdim * ydim * zdim; i++) - arr[i] = 0; - int arr_elements_set = 0; - while (arr_elements_set < 64) - { - int idx = rand() % (xdim * ydim * zdim); - if (arr[idx] == 0) - { - arr_elements_set++; - arr[idx] = 1; - } - } - int texel_weights_written = 0; - int idx = 0; - while (texel_weights_written < 64) - { - if (arr[idx]) - bsd->texels_for_bitmap_partitioning[texel_weights_written++] = idx; - idx++; - } - bsd->texelcount_for_bitmap_partitioning = 64; - } -} - - - - -static block_size_descriptor *bsd_pointers[4096]; - -// function to obtain a block size descriptor. If the descriptor does not exist, -// it is created as needed. Should not be called from within multi-threaded code. -const block_size_descriptor *get_block_size_descriptor(int xdim, int ydim, int zdim) -{ - int bsd_index = xdim + (ydim << 4) + (zdim << 8); - if (bsd_pointers[bsd_index] == NULL) - { - block_size_descriptor *bsd = new block_size_descriptor; - if (zdim > 1) - construct_block_size_descriptor_3d(xdim, ydim, zdim, bsd); - else - construct_block_size_descriptor_2d(xdim, ydim, bsd); - - bsd_pointers[bsd_index] = bsd; - } - return bsd_pointers[bsd_index]; -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_codec_internals.h b/3rdparty/bimg/3rdparty/astc/astc_codec_internals.h deleted file mode 100644 index 13356ae2c3b..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_codec_internals.h +++ /dev/null @@ -1,816 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012, 2018 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Internal function and data declarations for ASTC codec. - */ -/*----------------------------------------------------------------------------*/ - -#ifndef ASTC_CODEC_INTERNALS_INCLUDED - -#define ASTC_CODEC_INTERNALS_INCLUDED - -#include <stdint.h> -#include <stdlib.h> -#include <math.h> -#include "mathlib.h" - -#ifndef MIN - #define MIN(x,y) ((x)<(y)?(x):(y)) -#endif - -#ifndef MAX - #define MAX(x,y) ((x)>(y)?(x):(y)) -#endif - -// Macro to silence warnings on ignored parameters. -// The presence of this macro should be a signal to look at refactoring. -#define IGNORE(param) ((void)¶m) - -#define astc_isnan(p) ((p)!=(p)) - -// ASTC parameters -#define MAX_TEXELS_PER_BLOCK 216 -#define MAX_WEIGHTS_PER_BLOCK 64 -#define MIN_WEIGHT_BITS_PER_BLOCK 24 -#define MAX_WEIGHT_BITS_PER_BLOCK 96 -#define PARTITION_BITS 10 -#define PARTITION_COUNT (1 << PARTITION_BITS) - -// the sum of weights for one texel. -#define TEXEL_WEIGHT_SUM 16 -#define MAX_DECIMATION_MODES 87 -#define MAX_WEIGHT_MODES 2048 - -// error reporting for codec internal errors. -#define ASTC_CODEC_INTERNAL_ERROR astc_codec_internal_error(__FILE__, __LINE__) - -void astc_codec_internal_error(const char *filename, int linenumber); - -// uncomment this macro to enable checking for inappropriate NaNs; -// works on Linux only, and slows down encoding significantly. -// #define DEBUG_CAPTURE_NAN - -// the PRINT_DIAGNOSTICS macro enables the -diag command line switch, -// which can be used to look for codec bugs -#define DEBUG_PRINT_DIAGNOSTICS - -#ifdef DEBUG_PRINT_DIAGNOSTICS - extern int print_diagnostics; -#endif - -extern int print_tile_errors; -extern int print_statistics; - -extern int perform_srgb_transform; -extern int rgb_force_use_of_hdr; -extern int alpha_force_use_of_hdr; - -struct processed_line2 -{ - float2 amod; - float2 bs; - float2 bis; -}; -struct processed_line3 -{ - float3 amod; - float3 bs; - float3 bis; -}; -struct processed_line4 -{ - float4 amod; - float4 bs; - float4 bis; -}; - -enum astc_decode_mode -{ - DECODE_LDR_SRGB, - DECODE_LDR, - DECODE_HDR -}; - - -/* - Partition table representation: - For each block size, we have 3 tables, each with 1024 partitionings; - these three tables correspond to 2, 3 and 4 partitions respectively. - For each partitioning, we have: - * a 4-entry table indicating how many texels there are in each of the 4 partitions. - This may be from 0 to a very large value. - * a table indicating the partition index of each of the texels in the block. - Each index may be 0, 1, 2 or 3. - * Each element in the table is an uint8_t indicating partition index (0, 1, 2 or 3) -*/ - -struct partition_info -{ - int partition_count; - uint8_t texels_per_partition[4]; - uint8_t partition_of_texel[MAX_TEXELS_PER_BLOCK]; - uint8_t texels_of_partition[4][MAX_TEXELS_PER_BLOCK]; - - uint64_t coverage_bitmaps[4]; // used for the purposes of k-means partition search. -}; - - - - -/* - In ASTC, we don't necessarily provide a weight for every texel. - As such, for each block size, there are a number of patterns where some texels - have their weights computed as a weighted average of more than 1 weight. - As such, the codec uses a data structure that tells us: for each texel, which - weights it is a combination of for each weight, which texels it contributes to. - The decimation_table is this data structure. -*/ -struct decimation_table -{ - int num_texels; - int num_weights; - uint8_t texel_num_weights[MAX_TEXELS_PER_BLOCK]; // number of indices that go into the calculation for a texel - uint8_t texel_weights_int[MAX_TEXELS_PER_BLOCK][4]; // the weight to assign to each weight - float texel_weights_float[MAX_TEXELS_PER_BLOCK][4]; // the weight to assign to each weight - uint8_t texel_weights[MAX_TEXELS_PER_BLOCK][4]; // the weights that go into a texel calculation - uint8_t weight_num_texels[MAX_WEIGHTS_PER_BLOCK]; // the number of texels that a given weight contributes to - uint8_t weight_texel[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; // the texels that the weight contributes to - uint8_t weights_int[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; // the weights that the weight contributes to a texel. - float weights_flt[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK]; // the weights that the weight contributes to a texel. -}; - - - - -/* - data structure describing information that pertains to a block size and its associated block modes. -*/ -struct block_mode -{ - int8_t decimation_mode; - int8_t quantization_mode; - int8_t is_dual_plane; - int8_t permit_encode; - int8_t permit_decode; - float percentile; -}; - - -struct block_size_descriptor -{ - int decimation_mode_count; - int decimation_mode_samples[MAX_DECIMATION_MODES]; - int decimation_mode_maxprec_1plane[MAX_DECIMATION_MODES]; - int decimation_mode_maxprec_2planes[MAX_DECIMATION_MODES]; - float decimation_mode_percentile[MAX_DECIMATION_MODES]; - int permit_encode[MAX_DECIMATION_MODES]; - const decimation_table *decimation_tables[MAX_DECIMATION_MODES + 1]; - block_mode block_modes[MAX_WEIGHT_MODES]; - - // for the k-means bed bitmap partitioning algorithm, we don't - // want to consider more than 64 texels; this array specifies - // which 64 texels (if that many) to consider. - int texelcount_for_bitmap_partitioning; - int texels_for_bitmap_partitioning[64]; -}; - -// data structure representing one block of an image. -// it is expanded to float prior to processing to save some computation time -// on conversions to/from uint8_t (this also allows us to handle HDR textures easily) -struct imageblock -{ - float orig_data[MAX_TEXELS_PER_BLOCK * 4]; // original input data - float work_data[MAX_TEXELS_PER_BLOCK * 4]; // the data that we will compress, either linear or LNS (0..65535 in both cases) - float deriv_data[MAX_TEXELS_PER_BLOCK * 4]; // derivative of the conversion function used, used to modify error weighting - - uint8_t rgb_lns[MAX_TEXELS_PER_BLOCK]; // 1 if RGB data are being treated as LNS - uint8_t alpha_lns[MAX_TEXELS_PER_BLOCK]; // 1 if Alpha data are being treated as LNS - uint8_t nan_texel[MAX_TEXELS_PER_BLOCK]; // 1 if the texel is a NaN-texel. - - float red_min, red_max; - float green_min, green_max; - float blue_min, blue_max; - float alpha_min, alpha_max; - int grayscale; // 1 if R=G=B for every pixel, 0 otherwise - - int xpos, ypos, zpos; -}; - - -struct error_weighting_params -{ - float rgb_power; - float rgb_base_weight; - float rgb_mean_weight; - float rgb_stdev_weight; - float alpha_power; - float alpha_base_weight; - float alpha_mean_weight; - float alpha_stdev_weight; - float rgb_mean_and_stdev_mixing; - int mean_stdev_radius; - int enable_rgb_scale_with_alpha; - int alpha_radius; - int ra_normal_angular_scale; - float block_artifact_suppression; - float rgba_weights[4]; - - float block_artifact_suppression_expanded[MAX_TEXELS_PER_BLOCK]; - - // parameters that deal with heuristic codec speedups - int partition_search_limit; - float block_mode_cutoff; - float texel_avg_error_limit; - float partition_1_to_2_limit; - float lowest_correlation_cutoff; - int max_refinement_iters; -}; - - - - -void update_imageblock_flags(imageblock * pb, int xdim, int ydim, int zdim); - - -void imageblock_initialize_orig_from_work(imageblock * pb, int pixelcount); - - -void imageblock_initialize_work_from_orig(imageblock * pb, int pixelcount); - - - -/* - Data structure representing error weighting for one block of an image. this is used as - a multiplier for the error weight to apply to each color component when computing PSNR. - - This weighting has several uses: it's usable for RA, GA, BA, A weighting, which is useful - for alpha-textures it's usable for HDR textures, where weighting should be approximately inverse to - luminance it's usable for perceptual weighting, where we assign higher weight to low-variability - regions than to high-variability regions. it's usable for suppressing off-edge block content in - case the texture doesn't actually extend to the edge of the block. - - For the default case (everything is evenly weighted), every weight is 1. For the RA,GA,BA,A case, - we multiply the R,G,B weights with that of the alpha. - - Putting the same weight in every component should result in the default case. - The following relations should hold: - - texel_weight_rg[i] = (texel_weight_r[i] + texel_weight_g[i]) / 2 - texel_weight_lum[i] = (texel_weight_r[i] + texel_weight_g[i] + texel_weight_b[i]) / 3 - texel_weight[i] = (texel_weight_r[i] + texel_weight_g[i] + texel_weight_b[i] + texel_weight_a[i] / 4 - */ - -struct error_weight_block -{ - float4 error_weights[MAX_TEXELS_PER_BLOCK]; - float texel_weight[MAX_TEXELS_PER_BLOCK]; - float texel_weight_gba[MAX_TEXELS_PER_BLOCK]; - float texel_weight_rba[MAX_TEXELS_PER_BLOCK]; - float texel_weight_rga[MAX_TEXELS_PER_BLOCK]; - float texel_weight_rgb[MAX_TEXELS_PER_BLOCK]; - - float texel_weight_rg[MAX_TEXELS_PER_BLOCK]; - float texel_weight_rb[MAX_TEXELS_PER_BLOCK]; - float texel_weight_gb[MAX_TEXELS_PER_BLOCK]; - float texel_weight_ra[MAX_TEXELS_PER_BLOCK]; - - float texel_weight_r[MAX_TEXELS_PER_BLOCK]; - float texel_weight_g[MAX_TEXELS_PER_BLOCK]; - float texel_weight_b[MAX_TEXELS_PER_BLOCK]; - float texel_weight_a[MAX_TEXELS_PER_BLOCK]; - - int contains_zeroweight_texels; -}; - - - -struct error_weight_block_orig -{ - float4 error_weights[MAX_TEXELS_PER_BLOCK]; -}; - - -// enumeration of all the quantization methods we support under this format. -enum quantization_method -{ - QUANT_2 = 0, - QUANT_3 = 1, - QUANT_4 = 2, - QUANT_5 = 3, - QUANT_6 = 4, - QUANT_8 = 5, - QUANT_10 = 6, - QUANT_12 = 7, - QUANT_16 = 8, - QUANT_20 = 9, - QUANT_24 = 10, - QUANT_32 = 11, - QUANT_40 = 12, - QUANT_48 = 13, - QUANT_64 = 14, - QUANT_80 = 15, - QUANT_96 = 16, - QUANT_128 = 17, - QUANT_160 = 18, - QUANT_192 = 19, - QUANT_256 = 20 -}; - - -/* - In ASTC, we support relatively many combinations of weight precisions and weight transfer functions. - As such, for each combination we support, we have a hardwired data structure. - - This structure provides the following information: A table, used to estimate the closest quantized - weight for a given floating-point weight. For each quantized weight, the corresponding unquantized - and floating-point values. For each quantized weight, a previous-value and a next-value. -*/ - -struct quantization_and_transfer_table -{ - quantization_method method; - uint8_t unquantized_value[32]; // 0..64 - float unquantized_value_flt[32]; // 0..1 - uint8_t prev_quantized_value[32]; - uint8_t next_quantized_value[32]; - uint8_t closest_quantized_weight[1025]; -}; - -extern const quantization_and_transfer_table quant_and_xfer_tables[12]; - - - -enum endpoint_formats -{ - FMT_LUMINANCE = 0, - FMT_LUMINANCE_DELTA = 1, - FMT_HDR_LUMINANCE_LARGE_RANGE = 2, - FMT_HDR_LUMINANCE_SMALL_RANGE = 3, - FMT_LUMINANCE_ALPHA = 4, - FMT_LUMINANCE_ALPHA_DELTA = 5, - FMT_RGB_SCALE = 6, - FMT_HDR_RGB_SCALE = 7, - FMT_RGB = 8, - FMT_RGB_DELTA = 9, - FMT_RGB_SCALE_ALPHA = 10, - FMT_HDR_RGB = 11, - FMT_RGBA = 12, - FMT_RGBA_DELTA = 13, - FMT_HDR_RGB_LDR_ALPHA = 14, - FMT_HDR_RGBA = 15, -}; - - - -struct symbolic_compressed_block -{ - int error_block; // 1 marks error block, 0 marks non-error-block. - int block_mode; // 0 to 2047. Negative value marks constant-color block (-1: FP16, -2:UINT16) - int partition_count; // 1 to 4; Zero marks a constant-color block. - int partition_index; // 0 to 1023 - int color_formats[4]; // color format for each endpoint color pair. - int color_formats_matched; // color format for all endpoint pairs are matched. - int color_values[4][12]; // quantized endpoint color pairs. - int color_quantization_level; - uint8_t plane1_weights[MAX_WEIGHTS_PER_BLOCK]; // quantized and decimated weights - uint8_t plane2_weights[MAX_WEIGHTS_PER_BLOCK]; - int plane2_color_component; // color component for the secondary plane of weights - int constant_color[4]; // constant-color, as FP16 or UINT16. Used for constant-color blocks only. -}; - - -struct physical_compressed_block -{ - uint8_t data[16]; -}; - - - - -const block_size_descriptor *get_block_size_descriptor(int xdim, int ydim, int zdim); - - -// *********************************************************** -// functions and data pertaining to quantization and encoding -// ********************************************************** -extern const uint8_t color_quantization_tables[21][256]; -extern const uint8_t color_unquantization_tables[21][256]; - -void encode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset); - -void decode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset); - -int compute_ise_bitcount(int items, quantization_method quant); - -void build_quantization_mode_table(void); -extern int quantization_mode_table[17][128]; - - -// ********************************************** -// functions and data pertaining to partitioning -// ********************************************** - -// function to get a pointer to a partition table or an array thereof. -const partition_info *get_partition_table(int xdim, int ydim, int zdim, int partition_count); - - - - -// functions to compute color averages and dominant directions -// for each partition in a block - - -void compute_averages_and_directions_rgb(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float4 * color_scalefactors, float3 * averages, float3 * directions_rgb, float2 * directions_rg, float2 * directions_rb, float2 * directions_gb); - - - -void compute_averages_and_directions_rgba(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float4 * color_scalefactors, - float4 * averages, float4 * directions_rgba, float3 * directions_gba, float3 * directions_rba, float3 * directions_rga, float3 * directions_rgb); - - -void compute_averages_and_directions_3_components(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, - const float3 * color_scalefactors, int component1, int component2, int component3, float3 * averages, float3 * directions); - -void compute_averages_and_directions_2_components(const partition_info * pt, - const imageblock * blk, - const error_weight_block * ewb, const float2 * color_scalefactors, int component1, int component2, float2 * averages, float2 * directions); - -// functions to compute error value across a tile given a partitioning -// (with the assumption that each partitioning has colors lying on a line where -// they are represented with infinite precision. Also return the length of the line -// segments that the partition's colors are actually projected onto. -float compute_error_squared_gba(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * plines, - // output: computed length of the partitioning's line. This is not part of the - // error introduced by partitioning itself, but us used to estimate the error introduced by quantization - float *length_of_lines); - -float compute_error_squared_rba(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * plines, - // output: computed length of the partitioning's line. This is not part of the - // error introduced by partitioning itself, but us used to estimate the error introduced by quantization - float *length_of_lines); - -float compute_error_squared_rga(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * plines, - // output: computed length of the partitioning's line. This is not part of the - // error introduced by partitioning itself, but us used to estimate the error introduced by quantization - float *length_of_lines); - -float compute_error_squared_rgb(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * plines, - // output: computed length of the partitioning's line. This is not part of the - // error introduced by partitioning itself, but us used to estimate the error introduced by quantization - float *length_of_lines); - - -float compute_error_squared_rgba(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line4 * lines, // one line for each of the partitions. The lines are assumed to be normalized. - float *length_of_lines); - -float compute_error_squared_rg(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line2 * plines, float *length_of_lines); - -float compute_error_squared_rb(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line2 * plines, float *length_of_lines); - -float compute_error_squared_gb(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line2 * plines, float *length_of_lines); - -float compute_error_squared_ra(const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line2 * plines, float *length_of_lines); - - -// functions to compute error value across a tile for a particular line function -// for a single partition. -float compute_error_squared_rgb_single_partition(int partition_to_test, int xdim, int ydim, int zdim, const partition_info * pt, // the partition that we use when computing the squared-error. - const imageblock * blk, const error_weight_block * ewb, const processed_line3 * lin // the line for the partition. - ); - - - -// for each partition, compute its color weightings. -void compute_partition_error_color_weightings(int xdim, int ydim, int zdim, const error_weight_block * ewb, const partition_info * pi, float4 error_weightings[4], float4 color_scalefactors[4]); - - - -// function to find the best partitioning for a given block. - -void find_best_partitionings(int partition_search_limit, int xdim, int ydim, int zdim, int partition_count, const imageblock * pb, const error_weight_block * ewb, int candidates_to_return, - // best partitionings to use if the endpoint colors are assumed to be uncorrelated - int *best_partitions_uncorrellated, - // best partitionings to use if the endpoint colors have the same chroma - int *best_partitions_samechroma, - // best partitionings to use if dual plane of weights are present - int *best_partitions_dual_weight_planes); - - -// use k-means clustering to compute a partition ordering for a block. -void kmeans_compute_partition_ordering(int xdim, int ydim, int zdim, int partition_count, const imageblock * blk, int *ordering); - - - - -// ********************************************************* -// functions and data pertaining to images and imageblocks -// ********************************************************* - -struct astc_codec_image -{ - uint8_t ***imagedata8; - uint16_t ***imagedata16; - int xsize; - int ysize; - int zsize; - int padding; -}; - -void destroy_image(astc_codec_image * img); -astc_codec_image *allocate_image(int bitness, int xsize, int ysize, int zsize, int padding); -void initialize_image(astc_codec_image * img); -void fill_image_padding_area(astc_codec_image * img); - - -extern float4 ***input_averages; -extern float4 ***input_variances; -extern float ***input_alpha_averages; - - -// the entries here : 0=red, 1=green, 2=blue, 3=alpha, 4=0.0, 5=1.0 -struct swizzlepattern -{ - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; -}; - - - -int determine_image_channels(const astc_codec_image * img); - -// function to compute regional averages and variances for an image -void compute_averages_and_variances(const astc_codec_image * img, float rgb_power_to_use, float alpha_power_to_use, int avg_kernel_radius, int var_kernel_radius, swizzlepattern swz); - - -/* - Functions to load image from file. - If successful, return an astc_codec_image object. - If unsuccessful, returns NULL. - - *result is used to return a result. In case of a successfully loaded image, bits[2:0] - of *result indicate how many components are present, and bit[7] indicate whether - the input image was LDR or HDR (0=LDR, 1=HDR). - - In case of failure, *result is given a negative value. -*/ - - -astc_codec_image *load_ktx_uncompressed_image(const char *filename, int padding, int *result); -astc_codec_image *load_dds_uncompressed_image(const char *filename, int padding, int *result); -astc_codec_image *load_tga_image(const char *tga_filename, int padding, int *result); -astc_codec_image *load_image_with_stb(const char *filename, int padding, int *result); - -astc_codec_image *astc_codec_load_image(const char *filename, int padding, int *result); -int astc_codec_unlink(const char *filename); - -// function to store image to file -// If successful, returns the number of channels in input image -// If unsuccessful, returns a negative number. -int store_ktx_uncompressed_image(const astc_codec_image * img, const char *filename, int bitness); -int store_dds_uncompressed_image(const astc_codec_image * img, const char *filename, int bitness); -int store_tga_image(const astc_codec_image * img, const char *tga_filename, int bitness); - -int astc_codec_store_image(const astc_codec_image * img, const char *filename, int bitness, const char **format_string); - -int get_output_filename_enforced_bitness(const char *filename); - - -// compute a bunch of error metrics -void compute_error_metrics(int input_image_is_hdr, int input_components, const astc_codec_image * img1, const astc_codec_image * img2, int low_fstop, int high_fstop, int psnrmode); - -// fetch an image-block from the input file -void fetch_imageblock(const astc_codec_image * img, imageblock * pb, // picture-block to initialize with image data - // block dimensions - int xdim, int ydim, int zdim, - // position in picture to fetch block from - int xpos, int ypos, int zpos, swizzlepattern swz); - - -// write an image block to the output file buffer. -// the data written are taken from orig_data. -void write_imageblock(astc_codec_image * img, const imageblock * pb, // picture-block to initialize with image data - // block dimensions - int xdim, int ydim, int zdim, - // position in picture to write block to. - int xpos, int ypos, int zpos, swizzlepattern swz); - - -// helper function to check whether a given picture-block has alpha that is not -// just uniformly 1. -int imageblock_uses_alpha(int xdim, int ydim, int zdim, const imageblock * pb); - - -float compute_imageblock_difference(int xdim, int ydim, int zdim, const imageblock * p1, const imageblock * p2, const error_weight_block * ewb); - - - - - -// *********************************************************** -// functions pertaining to computing texel weights for a block -// *********************************************************** - - -struct endpoints -{ - int partition_count; - float4 endpt0[4]; - float4 endpt1[4]; -}; - - -struct endpoints_and_weights -{ - endpoints ep; - float weights[MAX_TEXELS_PER_BLOCK]; - float weight_error_scale[MAX_TEXELS_PER_BLOCK]; -}; - - -void compute_endpoints_and_ideal_weights_1_plane(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, endpoints_and_weights * ei); - -void compute_endpoints_and_ideal_weights_2_planes(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, int separate_component, - endpoints_and_weights * ei1, // for the three components of the primary plane of weights - endpoints_and_weights * ei2 // for the remaining component. - ); - -void compute_ideal_weights_for_decimation_table(const endpoints_and_weights * eai, const decimation_table * it, float *weight_set, float *weights); - -void compute_ideal_quantized_weights_for_decimation_table(const endpoints_and_weights * eai, - const decimation_table * it, - float low_bound, float high_bound, const float *weight_set_in, float *weight_set_out, uint8_t * quantized_weight_set, int quantization_level); - - -float compute_error_of_weight_set(const endpoints_and_weights * eai, const decimation_table * it, const float *weights); - - -float compute_value_of_texel_flt(int texel_to_get, const decimation_table * it, const float *weights); - - -int compute_value_of_texel_int(int texel_to_get, const decimation_table * it, const int *weights); - - -void merge_endpoints(const endpoints * ep1, // contains three of the color components - const endpoints * ep2, // contains the remaining color component - int separate_component, endpoints * res); - -// functions dealing with color endpoints - -// function to pack a pair of color endpoints into a series of integers. -// the format used may or may not match the format specified; -// the return value is the format actually used. -int pack_color_endpoints(astc_decode_mode decode_mode, float4 color0, float4 color1, float4 rgbs_color, float4 rgbo_color, float2 luminances, int format, int *output, int quantization_level); - - -// unpack a pair of color endpoints from a series of integers. -void unpack_color_endpoints(astc_decode_mode decode_mode, int format, int quantization_level, const int *input, int *rgb_hdr, int *alpha_hdr, int *nan_endpoint, ushort4 * output0, ushort4 * output1); - - -struct encoding_choice_errors -{ - float rgb_scale_error; // error of using LDR RGB-scale instead of complete endpoints. - float rgb_luma_error; // error of using HDR RGB-scale instead of complete endpoints. - float luminance_error; // error of using luminance instead of RGB - float alpha_drop_error; // error of discarding alpha - float rgb_drop_error; // error of discarding RGB - int can_offset_encode; - int can_blue_contract; -}; - -// buffers used to store intermediate data in compress_symbolic_block_fixed_partition_*() -struct compress_fixed_partition_buffers -{ - endpoints_and_weights* ei1; - endpoints_and_weights* ei2; - endpoints_and_weights* eix1; - endpoints_and_weights* eix2; - float *decimated_quantized_weights; - float *decimated_weights; - float *flt_quantized_decimated_quantized_weights; - uint8_t *u8_quantized_decimated_quantized_weights; -}; - -struct compress_symbolic_block_buffers -{ - error_weight_block *ewb; - error_weight_block_orig *ewbo; - symbolic_compressed_block *tempblocks; - imageblock *temp; - compress_fixed_partition_buffers *plane1; - compress_fixed_partition_buffers *planes2; -}; - -void compute_encoding_choice_errors(int xdim, int ydim, int zdim, const imageblock * pb, const partition_info * pi, const error_weight_block * ewb, - int separate_component, // component that is separated out in 2-plane mode, -1 in 1-plane mode - encoding_choice_errors * eci); - - - -void determine_optimal_set_of_endpoint_formats_to_use(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, const endpoints * ep, - int separate_component, // separate color component for 2-plane mode; -1 for single-plane mode - // bitcounts and errors computed for the various quantization methods - const int *qwt_bitcounts, const float *qwt_errors, - // output data - int partition_format_specifiers[4][4], int quantized_weight[4], int quantization_level[4], int quantization_level_mod[4]); - - -void recompute_ideal_colors(int xdim, int ydim, int zdim, int weight_quantization_mode, endpoints * ep, // contains the endpoints we wish to update - float4 * rgbs_vectors, // used to return RGBS-vectors for endpoint mode #6 - float4 * rgbo_vectors, // used to return RGBS-vectors for endpoint mode #7 - float2 * lum_vectors, // used to return luminance-vectors. - const uint8_t * weight_set, // the current set of weight values - const uint8_t * plane2_weight_set, // NULL if plane 2 is not actually used. - int plane2_color_component, // color component for 2nd plane of weights; -1 if the 2nd plane of weights is not present - const partition_info * pi, const decimation_table * it, const imageblock * pb, // picture-block containing the actual data. - const error_weight_block * ewb); - - - -void expand_block_artifact_suppression(int xdim, int ydim, int zdim, error_weighting_params * ewp); - -// Function to set error weights for each color component for each texel in a block. -// Returns the sum of all the error values set. -float prepare_error_weight_block(const astc_codec_image * input_image, - // dimensions of error weight block. - int xdim, int ydim, int zdim, const error_weighting_params * ewp, const imageblock * blk, error_weight_block * ewb, error_weight_block_orig * ewbo); - - -// functions pertaining to weight alignment -void prepare_angular_tables(void); - -void compute_angular_endpoints_1plane(float mode_cutoff, - const block_size_descriptor * bsd, - const float *decimated_quantized_weights, const float *decimated_weights, float low_value[MAX_WEIGHT_MODES], float high_value[MAX_WEIGHT_MODES]); - -void compute_angular_endpoints_2planes(float mode_cutoff, - const block_size_descriptor * bsd, - const float *decimated_quantized_weights, - const float *decimated_weights, - float low_value1[MAX_WEIGHT_MODES], float high_value1[MAX_WEIGHT_MODES], float low_value2[MAX_WEIGHT_MODES], float high_value2[MAX_WEIGHT_MODES]); - - - - -/* *********************************** high-level encode and decode functions ************************************ */ - -float compress_symbolic_block(const astc_codec_image * input_image, - astc_decode_mode decode_mode, int xdim, int ydim, int zdim, const error_weighting_params * ewp, const imageblock * blk, symbolic_compressed_block * scb, - compress_symbolic_block_buffers * tmpbuf); - - -float4 lerp_color_flt(const float4 color0, const float4 color1, float weight, // 0..1 - float plane2_weight, // 0..1 - int plane2_color_component // 0..3; -1 if only one plane of weights is present. - ); - - -ushort4 lerp_color_int(astc_decode_mode decode_mode, ushort4 color0, ushort4 color1, int weight, // 0..64 - int plane2_weight, // 0..64 - int plane2_color_component // 0..3; -1 if only one plane of weights is present. - ); - - -void decompress_symbolic_block(astc_decode_mode decode_mode, - // dimensions of block - int xdim, int ydim, int zdim, - // position of block - int xpos, int ypos, int zpos, const symbolic_compressed_block * scb, imageblock * blk); - - -physical_compressed_block symbolic_to_physical(int xdim, int ydim, int zdim, const symbolic_compressed_block * sc); - -void physical_to_symbolic(int xdim, int ydim, int zdim, physical_compressed_block pb, symbolic_compressed_block * res); - - -uint16_t unorm16_to_sf16(uint16_t p); -uint16_t lns_to_sf16(uint16_t p); - - -#endif diff --git a/3rdparty/bimg/3rdparty/astc/astc_color_quantize.cpp b/3rdparty/bimg/3rdparty/astc/astc_color_quantize.cpp deleted file mode 100644 index caa2ba8033f..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_color_quantize.cpp +++ /dev/null @@ -1,2096 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Color quantization functions for ASTC. - */ -/*----------------------------------------------------------------------------*/ - -#include <stdio.h> - -#include "astc_codec_internals.h" -#include "softfloat.h" -#include <math.h> - -#ifdef DEBUG_PRINT_DIAGNOSTICS - #include <stdio.h> -#endif - -/* - quantize an LDR RGB color. Since this is a fall-back encoding, we cannot actually - fail but must just go on until we can produce a sensible result. - - Due to how this encoding works, color0 cannot be larger than color1; as such, - if color0 is actually larger than color1, then color0 is reduced and color1 is - increased until color0 is no longer larger than color1. -*/ -static inline int cqt_lookup(int quantization_level, int value) -{ - if (value < 0) - value = 0; - else if (value > 255) - value = 255; - return color_quantization_tables[quantization_level][value]; -} - -// clamp an input value to [0,255]; NaN is turned into 0 -static inline float clamp255(float val) -{ - if (val > 255.0f) - val = 255.0f; - else if (val > 0.0f) - { - // deliberately empty - // switching the order of calculation here will fail to handle 0. - } - else - val = 0.0f; - - return val; -} - - - -// clamp an input value to [0,1]; Nan is turned into 0. - -static inline float clamp01(float val) -{ - if (val > 1.0f) - val = 1.0f; - else if (val > 0.0f) - { - // deliberately empty - // switching the order of calculation here will fail to handle 0. - } - else - val = 0.0f; - - return val; -} - - - -void quantize_rgb(float4 color0, // LDR: 0=lowest, 255=highest - float4 color1, int output[6], int quantization_level) -{ - color0.xyz = color0.xyz * (1.0f / 257.0f); - color1.xyz = color1.xyz * (1.0f / 257.0f); - - - float r0 = clamp255(color0.x); - float g0 = clamp255(color0.y); - float b0 = clamp255(color0.z); - - float r1 = clamp255(color1.x); - float g1 = clamp255(color1.y); - float b1 = clamp255(color1.z); - - int ri0, gi0, bi0, ri1, gi1, bi1; - int ri0b, gi0b, bi0b, ri1b, gi1b, bi1b; - float rgb0_addon = 0.5f; - float rgb1_addon = 0.5f; - int iters = 0; - do - { - ri0 = cqt_lookup(quantization_level, (int)floor(r0 + rgb0_addon)); - gi0 = cqt_lookup(quantization_level, (int)floor(g0 + rgb0_addon)); - bi0 = cqt_lookup(quantization_level, (int)floor(b0 + rgb0_addon)); - ri1 = cqt_lookup(quantization_level, (int)floor(r1 + rgb1_addon)); - gi1 = cqt_lookup(quantization_level, (int)floor(g1 + rgb1_addon)); - bi1 = cqt_lookup(quantization_level, (int)floor(b1 + rgb1_addon)); - - ri0b = color_unquantization_tables[quantization_level][ri0]; - gi0b = color_unquantization_tables[quantization_level][gi0]; - bi0b = color_unquantization_tables[quantization_level][bi0]; - ri1b = color_unquantization_tables[quantization_level][ri1]; - gi1b = color_unquantization_tables[quantization_level][gi1]; - bi1b = color_unquantization_tables[quantization_level][bi1]; - - rgb0_addon -= 0.2f; - rgb1_addon += 0.2f; - iters++; - } - while (ri0b + gi0b + bi0b > ri1b + gi1b + bi1b); - - output[0] = ri0; - output[1] = ri1; - output[2] = gi0; - output[3] = gi1; - output[4] = bi0; - output[5] = bi1; -} - - -/* - quantize an RGBA color. */ -void quantize_rgba(float4 color0, float4 color1, int output[8], int quantization_level) -{ - color0.w = color0.w * (1.0f / 257.0f); - color1.w = color1.w * (1.0f / 257.0f); - - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - int ai0 = color_quantization_tables[quantization_level][(int)floor(a0 + 0.5f)]; - int ai1 = color_quantization_tables[quantization_level][(int)floor(a1 + 0.5f)]; - - output[6] = ai0; - output[7] = ai1; - - quantize_rgb(color0, color1, output, quantization_level); -} - - - -/* - attempt to quantize RGB endpoint values with blue-contraction. Returns 1 on failure, 0 on success. */ -int try_quantize_rgb_blue_contract(float4 color0, // assumed to be the smaller color - float4 color1, // assumed to be the larger color - int output[6], int quantization_level) -{ - color0.xyz = color0.xyz * (1.0f / 257.0f); - color1.xyz = color1.xyz * (1.0f / 257.0f); - - float r0 = color0.x; - float g0 = color0.y; - float b0 = color0.z; - - float r1 = color1.x; - float g1 = color1.y; - float b1 = color1.z; - - // inverse blue-contraction. This can produce an overflow; - // just bail out immediately if this is the case. - r0 += (r0 - b0); - g0 += (g0 - b0); - r1 += (r1 - b1); - g1 += (g1 - b1); - - if (r0 < 0.0f || r0 > 255.0f || g0 < 0.0f || g0 > 255.0f || b0 < 0.0f || b0 > 255.0f || - r1 < 0.0f || r1 > 255.0f || g1 < 0.0f || g1 > 255.0f || b1 < 0.0f || b1 > 255.0f) - { - return 0; - } - - // quantize the inverse-blue-contracted color - int ri0 = color_quantization_tables[quantization_level][(int)floor(r0 + 0.5f)]; - int gi0 = color_quantization_tables[quantization_level][(int)floor(g0 + 0.5f)]; - int bi0 = color_quantization_tables[quantization_level][(int)floor(b0 + 0.5f)]; - int ri1 = color_quantization_tables[quantization_level][(int)floor(r1 + 0.5f)]; - int gi1 = color_quantization_tables[quantization_level][(int)floor(g1 + 0.5f)]; - int bi1 = color_quantization_tables[quantization_level][(int)floor(b1 + 0.5f)]; - - // then unquantize again - int ru0 = color_unquantization_tables[quantization_level][ri0]; - int gu0 = color_unquantization_tables[quantization_level][gi0]; - int bu0 = color_unquantization_tables[quantization_level][bi0]; - int ru1 = color_unquantization_tables[quantization_level][ri1]; - int gu1 = color_unquantization_tables[quantization_level][gi1]; - int bu1 = color_unquantization_tables[quantization_level][bi1]; - - // if color #1 is not larger than color #0, then blue-contraction is not a valid approach. - // note that blue-contraction and quantization may itself change this order, which is why - // we must only test AFTER blue-contraction. - if (ru1 + gu1 + bu1 <= ru0 + gu0 + bu0) - return 0; - - output[0] = ri1; - output[1] = ri0; - output[2] = gi1; - output[3] = gi0; - output[4] = bi1; - output[5] = bi0; - - return 1; -} - - - - -/* - quantize an RGBA color with blue-contraction */ -int try_quantize_rgba_blue_contract(float4 color0, float4 color1, int output[8], int quantization_level) -{ - color0.w = color0.w * (1.0f / 257.0f); - color1.w = color1.w * (1.0f / 257.0f); - - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - - output[7] = color_quantization_tables[quantization_level][(int)floor(a0 + 0.5f)]; - output[6] = color_quantization_tables[quantization_level][(int)floor(a1 + 0.5f)]; - - return try_quantize_rgb_blue_contract(color0, color1, output, quantization_level); -} - - -// delta-encoding: -// at decode time, we move one bit from the offset to the base and seize another bit as a sign bit; -// we then unquantize both values as if they contain one extra bit. - -// if the sum of the offsets is nonnegative, then we encode a regular delta. - - -/* - attempt to quantize an RGB endpoint value with delta-encoding. */ - -int try_quantize_rgb_delta(float4 color0, float4 color1, int output[6], int quantization_level) -{ - color0.xyz = color0.xyz * (1.0f / 257.0f); - color1.xyz = color1.xyz * (1.0f / 257.0f); - - float r0 = clamp255(color0.x); - float g0 = clamp255(color0.y); - float b0 = clamp255(color0.z); - - float r1 = clamp255(color1.x); - float g1 = clamp255(color1.y); - float b1 = clamp255(color1.z); - - // transform r0 to unorm9 - int r0a = (int)floor(r0 + 0.5f); - int g0a = (int)floor(g0 + 0.5f); - int b0a = (int)floor(b0 + 0.5f); - r0a <<= 1; - g0a <<= 1; - b0a <<= 1; - - // mask off the top bit - int r0b = r0a & 0xFF; - int g0b = g0a & 0xFF; - int b0b = b0a & 0xFF; - - // quantize, then unquantize in order to get a value that we take - // differences against. - int r0be = color_quantization_tables[quantization_level][r0b]; - int g0be = color_quantization_tables[quantization_level][g0b]; - int b0be = color_quantization_tables[quantization_level][b0b]; - - r0b = color_unquantization_tables[quantization_level][r0be]; - g0b = color_unquantization_tables[quantization_level][g0be]; - b0b = color_unquantization_tables[quantization_level][b0be]; - r0b |= r0a & 0x100; // final unquantized-values for endpoint 0. - g0b |= g0a & 0x100; - b0b |= b0a & 0x100; - - // then, get hold of the second value - int r1d = (int)floor(r1 + 0.5f); - int g1d = (int)floor(g1 + 0.5f); - int b1d = (int)floor(b1 + 0.5f); - - r1d <<= 1; - g1d <<= 1; - b1d <<= 1; - // and take differences! - r1d -= r0b; - g1d -= g0b; - b1d -= b0b; - - // check if the difference is too large to be encodable. - if (r1d > 63 || g1d > 63 || b1d > 63 || r1d < -64 || g1d < -64 || b1d < -64) - return 0; - - // insert top bit of the base into the offset - r1d &= 0x7F; - g1d &= 0x7F; - b1d &= 0x7F; - - r1d |= (r0b & 0x100) >> 1; - g1d |= (g0b & 0x100) >> 1; - b1d |= (b0b & 0x100) >> 1; - - // then quantize & unquantize; if this causes any of the top two bits to flip, - // then encoding fails, since we have then corrupted either the top bit of the base - // or the sign bit of the offset. - int r1de = color_quantization_tables[quantization_level][r1d]; - int g1de = color_quantization_tables[quantization_level][g1d]; - int b1de = color_quantization_tables[quantization_level][b1d]; - - int r1du = color_unquantization_tables[quantization_level][r1de]; - int g1du = color_unquantization_tables[quantization_level][g1de]; - int b1du = color_unquantization_tables[quantization_level][b1de]; - - if (((r1d ^ r1du) | (g1d ^ g1du) | (b1d ^ b1du)) & 0xC0) - return 0; - - // check that the sum of the encoded offsets is nonnegative, else encoding fails - r1du &= 0x7f; - g1du &= 0x7f; - b1du &= 0x7f; - if (r1du & 0x40) - r1du -= 0x80; - if (g1du & 0x40) - g1du -= 0x80; - if (b1du & 0x40) - b1du -= 0x80; - if (r1du + g1du + b1du < 0) - return 0; - - // check that the offsets produce legitimate sums as well. - r1du += r0b; - g1du += g0b; - b1du += b0b; - if (r1du < 0 || r1du > 0x1FF || g1du < 0 || g1du > 0x1FF || b1du < 0 || b1du > 0x1FF) - return 0; - - // OK, we've come this far; we can now encode legitimate values. - output[0] = r0be; - output[1] = r1de; - output[2] = g0be; - output[3] = g1de; - output[4] = b0be; - output[5] = b1de; - - return 1; -} - - -int try_quantize_rgb_delta_blue_contract(float4 color0, float4 color1, int output[6], int quantization_level) -{ - color0.xyz = color0.xyz * (1.0f / 257.0f); - color1.xyz = color1.xyz * (1.0f / 257.0f); - - // switch around endpoint colors already at start. - float r0 = color1.x; - float g0 = color1.y; - float b0 = color1.z; - - float r1 = color0.x; - float g1 = color0.y; - float b1 = color0.z; - - // inverse blue-contraction. This step can perform an overflow, in which case - // we will bail out immediately. - r0 += (r0 - b0); - g0 += (g0 - b0); - r1 += (r1 - b1); - g1 += (g1 - b1); - - if (r0 < 0.0f || r0 > 255.0f || g0 < 0.0f || g0 > 255.0f || b0 < 0.0f || b0 > 255.0f || r1 < 0.0f || r1 > 255.0f || g1 < 0.0f || g1 > 255.0f || b1 < 0.0f || b1 > 255.0f) - return 0; - - // transform r0 to unorm9 - int r0a = (int)floor(r0 + 0.5f); - int g0a = (int)floor(g0 + 0.5f); - int b0a = (int)floor(b0 + 0.5f); - r0a <<= 1; - g0a <<= 1; - b0a <<= 1; - - // mask off the top bit - int r0b = r0a & 0xFF; - int g0b = g0a & 0xFF; - int b0b = b0a & 0xFF; - - // quantize, then unquantize in order to get a value that we take - // differences against. - int r0be = color_quantization_tables[quantization_level][r0b]; - int g0be = color_quantization_tables[quantization_level][g0b]; - int b0be = color_quantization_tables[quantization_level][b0b]; - - r0b = color_unquantization_tables[quantization_level][r0be]; - g0b = color_unquantization_tables[quantization_level][g0be]; - b0b = color_unquantization_tables[quantization_level][b0be]; - r0b |= r0a & 0x100; // final unquantized-values for endpoint 0. - g0b |= g0a & 0x100; - b0b |= b0a & 0x100; - - // then, get hold of the second value - int r1d = (int)floor(r1 + 0.5f); - int g1d = (int)floor(g1 + 0.5f); - int b1d = (int)floor(b1 + 0.5f); - - r1d <<= 1; - g1d <<= 1; - b1d <<= 1; - // and take differences! - r1d -= r0b; - g1d -= g0b; - b1d -= b0b; - - // check if the difference is too large to be encodable. - if (r1d > 63 || g1d > 63 || b1d > 63 || r1d < -64 || g1d < -64 || b1d < -64) - return 0; - - // insert top bit of the base into the offset - r1d &= 0x7F; - g1d &= 0x7F; - b1d &= 0x7F; - - r1d |= (r0b & 0x100) >> 1; - g1d |= (g0b & 0x100) >> 1; - b1d |= (b0b & 0x100) >> 1; - - // then quantize & unquantize; if this causes any of the top two bits to flip, - // then encoding fails, since we have then corrupted either the top bit of the base - // or the sign bit of the offset. - int r1de = color_quantization_tables[quantization_level][r1d]; - int g1de = color_quantization_tables[quantization_level][g1d]; - int b1de = color_quantization_tables[quantization_level][b1d]; - - int r1du = color_unquantization_tables[quantization_level][r1de]; - int g1du = color_unquantization_tables[quantization_level][g1de]; - int b1du = color_unquantization_tables[quantization_level][b1de]; - - if (((r1d ^ r1du) | (g1d ^ g1du) | (b1d ^ b1du)) & 0xC0) - return 0; - - // check that the sum of the encoded offsets is negative, else encoding fails - // note that this is inverse of the test for non-blue-contracted RGB. - r1du &= 0x7f; - g1du &= 0x7f; - b1du &= 0x7f; - if (r1du & 0x40) - r1du -= 0x80; - if (g1du & 0x40) - g1du -= 0x80; - if (b1du & 0x40) - b1du -= 0x80; - if (r1du + g1du + b1du >= 0) - return 0; - - // check that the offsets produce legitimate sums as well. - r1du += r0b; - g1du += g0b; - b1du += b0b; - if (r1du < 0 || r1du > 0x1FF || g1du < 0 || g1du > 0x1FF || b1du < 0 || b1du > 0x1FF) - return 0; - - // OK, we've come this far; we can now encode legitimate values. - output[0] = r0be; - output[1] = r1de; - output[2] = g0be; - output[3] = g1de; - output[4] = b0be; - output[5] = b1de; - - return 1; -} - - -int try_quantize_alpha_delta(float4 color0, float4 color1, int output[8], int quantization_level) -{ - color0.w = color0.w * (1.0f / 257.0f); - color1.w = color1.w * (1.0f / 257.0f); - - // the calculation for alpha-delta is exactly the same as for RGB-delta; see - // the RGB-delta function for comments. - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - - int a0a = (int)floor(a0 + 0.5f); - a0a <<= 1; - int a0b = a0a & 0xFF; - int a0be = color_quantization_tables[quantization_level][a0b]; - a0b = color_unquantization_tables[quantization_level][a0be]; - a0b |= a0a & 0x100; - int a1d = (int)floor(a1 + 0.5f); - a1d <<= 1; - a1d -= a0b; - if (a1d > 63 || a1d < -64) - return 0; - a1d &= 0x7F; - a1d |= (a0b & 0x100) >> 1; - int a1de = color_quantization_tables[quantization_level][a1d]; - int a1du = color_unquantization_tables[quantization_level][a1de]; - if ((a1d ^ a1du) & 0xC0) - return 0; - a1du &= 0x7F; - if (a1du & 0x40) - a1du -= 0x80; - a1du += a0b; - if (a1du < 0 || a1du > 0x1FF) - return 0; - output[6] = a0be; - output[7] = a1de; - return 1; -} - - - -int try_quantize_luminance_alpha_delta(float4 color0, float4 color1, int output[8], int quantization_level) -{ - float l0 = clamp255((color0.x + color0.y + color0.z) * ((1.0f / 3.0f) * (1.0f / 257.0f))); - float l1 = clamp255((color1.x + color1.y + color1.z) * ((1.0f / 3.0f) * (1.0f / 257.0f))); - float a0 = clamp255(color0.w * (1.0f / 257.0f)); - float a1 = clamp255(color1.w * (1.0f / 257.0f)); - - int l0a = (int)floor(l0 + 0.5f); - int a0a = (int)floor(a0 + 0.5f); - l0a <<= 1; - a0a <<= 1; - int l0b = l0a & 0xFF; - int a0b = a0a & 0xFF; - int l0be = color_quantization_tables[quantization_level][l0b]; - int a0be = color_quantization_tables[quantization_level][a0b]; - l0b = color_unquantization_tables[quantization_level][l0be]; - a0b = color_unquantization_tables[quantization_level][a0be]; - l0b |= l0a & 0x100; - a0b |= a0a & 0x100; - int l1d = (int)floor(l1 + 0.5f); - int a1d = (int)floor(a1 + 0.5f); - l1d <<= 1; - a1d <<= 1; - l1d -= l0b; - a1d -= a0b; - if (l1d > 63 || l1d < -64) - return 0; - if (a1d > 63 || a1d < -64) - return 0; - l1d &= 0x7F; - a1d &= 0x7F; - l1d |= (l0b & 0x100) >> 1; - a1d |= (a0b & 0x100) >> 1; - - int l1de = color_quantization_tables[quantization_level][l1d]; - int a1de = color_quantization_tables[quantization_level][a1d]; - int l1du = color_unquantization_tables[quantization_level][l1de]; - int a1du = color_unquantization_tables[quantization_level][a1de]; - if ((l1d ^ l1du) & 0xC0) - return 0; - if ((a1d ^ a1du) & 0xC0) - return 0; - l1du &= 0x7F; - a1du &= 0x7F; - if (l1du & 0x40) - l1du -= 0x80; - if (a1du & 0x40) - a1du -= 0x80; - l1du += l0b; - a1du += a0b; - if (l1du < 0 || l1du > 0x1FF) - return 0; - if (a1du < 0 || a1du > 0x1FF) - return 0; - output[0] = l0be; - output[1] = l1de; - output[2] = a0be; - output[3] = a1de; - - return 1; -} - - - - -int try_quantize_rgba_delta(float4 color0, float4 color1, int output[8], int quantization_level) -{ - int alpha_delta_res = try_quantize_alpha_delta(color0, color1, output, quantization_level); - - if (alpha_delta_res == 0) - return 0; - - return try_quantize_rgb_delta(color0, color1, output, quantization_level); -} - - -int try_quantize_rgba_delta_blue_contract(float4 color0, float4 color1, int output[8], int quantization_level) -{ - // notice that for the alpha encoding, we are swapping around color0 and color1; - // this is because blue-contraction involves swapping around the two colors. - int alpha_delta_res = try_quantize_alpha_delta(color1, color0, output, quantization_level); - - if (alpha_delta_res == 0) - return 0; - - return try_quantize_rgb_delta_blue_contract(color0, color1, output, quantization_level); -} - - - -void quantize_rgbs_new(float4 rgbs_color, // W component is a desired-scale to apply, in the range 0..1 - int output[4], int quantization_level) -{ - rgbs_color.xyz = rgbs_color.xyz * (1.0f / 257.0f); - - float r = clamp255(rgbs_color.x); - float g = clamp255(rgbs_color.y); - float b = clamp255(rgbs_color.z); - - int ri = color_quantization_tables[quantization_level][(int)floor(r + 0.5f)]; - int gi = color_quantization_tables[quantization_level][(int)floor(g + 0.5f)]; - int bi = color_quantization_tables[quantization_level][(int)floor(b + 0.5f)]; - - int ru = color_unquantization_tables[quantization_level][ri]; - int gu = color_unquantization_tables[quantization_level][gi]; - int bu = color_unquantization_tables[quantization_level][bi]; - - float oldcolorsum = rgbs_color.x + rgbs_color.y + rgbs_color.z; - float newcolorsum = (float)(ru + gu + bu); - - float scale = clamp01(rgbs_color.w * (oldcolorsum + 1e-10f) / (newcolorsum + 1e-10f)); - - int scale_idx = (int)floor(scale * 256.0f + 0.5f); - - if (scale_idx < 0) - scale_idx = 0; - else if (scale_idx > 255) - scale_idx = 255; - - output[0] = ri; - output[1] = gi; - output[2] = bi; - output[3] = color_quantization_tables[quantization_level][scale_idx]; -} - - - -void quantize_rgbs_alpha_new(float4 color0, float4 color1, float4 rgbs_color, int output[6], int quantization_level) -{ - color0.w = color0.w * (1.0f / 257.0f); - color1.w = color1.w * (1.0f / 257.0f); - - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - - int ai0 = color_quantization_tables[quantization_level][(int)floor(a0 + 0.5f)]; - int ai1 = color_quantization_tables[quantization_level][(int)floor(a1 + 0.5f)]; - - output[4] = ai0; - output[5] = ai1; - - quantize_rgbs_new(rgbs_color, output, quantization_level); -} - - - -void quantize_luminance(float4 color0, float4 color1, int output[2], int quantization_level) -{ - color0.xyz = color0.xyz * (1.0f / 257.0f); - color1.xyz = color1.xyz * (1.0f / 257.0f); - - float lum0 = clamp255((color0.x + color0.y + color0.z) * (1.0f / 3.0f)); - float lum1 = clamp255((color1.x + color1.y + color1.z) * (1.0f / 3.0f)); - - if (lum0 > lum1) - { - float avg = (lum0 + lum1) * 0.5f; - lum0 = avg; - lum1 = avg; - } - - output[0] = color_quantization_tables[quantization_level][(int)floor(lum0 + 0.5f)]; - output[1] = color_quantization_tables[quantization_level][(int)floor(lum1 + 0.5f)]; -} - - - - - - -void quantize_luminance_alpha(float4 color0, float4 color1, int output[4], int quantization_level) -{ - color0 = color0 * (1.0f / 257.0f); - color1 = color1 * (1.0f / 257.0f); - - float lum0 = clamp255((color0.x + color0.y + color0.z) * (1.0f / 3.0f)); - float lum1 = clamp255((color1.x + color1.y + color1.z) * (1.0f / 3.0f)); - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - - // if the endpoints are *really* close, then pull them apart slightly; - // this affords for >8 bits precision for normal maps. - if (quantization_level > 18 && fabs(lum0 - lum1) < 3.0f) - { - if (lum0 < lum1) - { - lum0 -= 0.5f; - lum1 += 0.5f; - } - else - { - lum0 += 0.5f; - lum1 -= 0.5f; - } - lum0 = clamp255(lum0); - lum1 = clamp255(lum1); - } - if (quantization_level > 18 && fabs(a0 - a1) < 3.0f) - { - if (a0 < a1) - { - a0 -= 0.5f; - a1 += 0.5f; - } - else - { - a0 += 0.5f; - a1 -= 0.5f; - } - a0 = clamp255(a0); - a1 = clamp255(a1); - } - - - output[0] = color_quantization_tables[quantization_level][(int)floor(lum0 + 0.5f)]; - output[1] = color_quantization_tables[quantization_level][(int)floor(lum1 + 0.5f)]; - output[2] = color_quantization_tables[quantization_level][(int)floor(a0 + 0.5f)]; - output[3] = color_quantization_tables[quantization_level][(int)floor(a1 + 0.5f)]; -} - - -void quantize0(float4 color0, float4 color1, int output[8], int quantization_level) -{ - IGNORE(color0); - IGNORE(color1); - IGNORE(output); - IGNORE(quantization_level); - - int i; - for (i = 0; i < 8; i++) - output[i] = 0; -} - - -// quantize and unquantize a number, wile making sure to retain the top two bits. -static inline void quantize_and_unquantize_retain_top_two_bits(int quantization_level, int value_to_quantize, // 0 to 255. - int *quantized_value, int *unquantized_value) -{ - - int perform_loop; - int quantval; - int uquantval; - - do - { - quantval = color_quantization_tables[quantization_level][value_to_quantize]; - uquantval = color_unquantization_tables[quantization_level][quantval]; - - // perform looping if the top two bits were modified by quant/unquant - perform_loop = (value_to_quantize & 0xC0) != (uquantval & 0xC0); - - if ((uquantval & 0xC0) > (value_to_quantize & 0xC0)) - { - // quant/unquant rounded UP so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding up. - value_to_quantize--; - } - else if ((uquantval & 0xC0) < (value_to_quantize & 0xC0)) - { - // quant/unquant rounded DOWN so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding down. - value_to_quantize--; - } - } - while (perform_loop); - - *quantized_value = quantval; - *unquantized_value = uquantval; -} - - -// quantize and unquantize a number, wile making sure to retain the top four bits. -static inline void quantize_and_unquantize_retain_top_four_bits(int quantization_level, int value_to_quantize, // 0 to 255. - int *quantized_value, int *unquantized_value) -{ - - int perform_loop; - int quantval; - int uquantval; - - do - { - quantval = color_quantization_tables[quantization_level][value_to_quantize]; - uquantval = color_unquantization_tables[quantization_level][quantval]; - - // perform looping if the top two bits were modified by quant/unquant - perform_loop = (value_to_quantize & 0xF0) != (uquantval & 0xF0); - - if ((uquantval & 0xF0) > (value_to_quantize & 0xF0)) - { - // quant/unquant rounded UP so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding up. - value_to_quantize--; - } - else if ((uquantval & 0xF0) < (value_to_quantize & 0xF0)) - { - // quant/unquant rounded DOWN so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding down. - value_to_quantize--; - } - } - while (perform_loop); - - *quantized_value = quantval; - *unquantized_value = uquantval; -} - -// quantize and unquantize a number, wile making sure to retain the top two bits. -static inline void quantize_and_unquantize_retain_top_bit(int quantization_level, int value_to_quantize, // 0 to 255. - int *quantized_value, int *unquantized_value) -{ - - int perform_loop; - int quantval; - int uquantval; - - do - { - quantval = color_quantization_tables[quantization_level][value_to_quantize]; - uquantval = color_unquantization_tables[quantization_level][quantval]; - - // perform looping if the top two bits were modified by quant/unquant - perform_loop = (value_to_quantize & 0x80) != (uquantval & 0x80); - - if ((uquantval & 0x80) > (value_to_quantize & 0x80)) - { - // quant/unquant rounded UP so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding up. - value_to_quantize--; - } - else if ((uquantval & 0x80) < (value_to_quantize & 0x80)) - { - // quant/unquant rounded DOWN so that the top two bits changed; - // decrement the input value in hopes that this will avoid rounding down. - value_to_quantize--; - } - } - while (perform_loop); - - *quantized_value = quantval; - *unquantized_value = uquantval; -} - - - - - -/* - HDR color encoding, take #3 */ - - -void quantize_hdr_rgbo3(float4 color, int output[4], int quantization_level) -{ - color.xyz = color.xyz + color.www; - - if (!(color.x > 0.0f)) - color.x = 0.0f; - else if (color.x > 65535.0f) - color.x = 65535.0f; - - if (!(color.y > 0.0f)) - color.y = 0.0f; - else if (color.y > 65535.0f) - color.y = 65535.0f; - - if (!(color.z > 0.0f)) - color.z = 0.0f; - else if (color.z > 65535.0f) - color.z = 65535.0f; - - if (!(color.w > 0.0f)) - color.w = 0.0f; - else if (color.w > 65535.0f) - color.w = 65535.0f; - - float4 color_bak = color; - int majcomp; - if (color.x > color.y && color.x > color.z) - majcomp = 0; // red is largest component - else if (color.y > color.z) - majcomp = 1; // green is largest component - else - majcomp = 2; // blue is largest component - - // swap around the red component and the largest component. - switch (majcomp) - { - case 1: - color = color.yxzw; - break; - case 2: - color = color.zyxw; - break; - default: - break; - } - - static const int mode_bits[5][3] = { - {11, 5, 7}, - {11, 6, 5}, - {10, 5, 8}, - {9, 6, 7}, - {8, 7, 6} - }; - - - static const float mode_cutoffs[5][2] = { - {1024, 4096}, - {2048, 1024}, - {2048, 16384}, - {8192, 16384}, - {32768, 16384} - }; - - static const float mode_rscales[5] = { - 32.0f, - 32.0f, - 64.0f, - 128.0f, - 256.0f, - }; - - static const float mode_scales[5] = { - 1.0f / 32.0f, - 1.0f / 32.0f, - 1.0f / 64.0f, - 1.0f / 128.0f, - 1.0f / 256.0f, - }; - - float r_base = color.x; - float g_base = color.x - color.y; - float b_base = color.x - color.z; - float s_base = color.w; - - int mode; - for (mode = 0; mode < 5; mode++) - { - if (g_base > mode_cutoffs[mode][0] || b_base > mode_cutoffs[mode][0] || s_base > mode_cutoffs[mode][1]) - { - continue; - } - - // encode the mode into a 4-bit vector. - int mode_enc = mode < 4 ? (mode | (majcomp << 2)) : (majcomp | 0xC); - - float mode_scale = mode_scales[mode]; - float mode_rscale = mode_rscales[mode]; - - int gb_intcutoff = 1 << mode_bits[mode][1]; - int s_intcutoff = 1 << mode_bits[mode][2]; - - // first, quantize and unquantize R. - int r_intval = (int)floor(r_base * mode_scale + 0.5f); - - int r_lowbits = r_intval & 0x3f; - - r_lowbits |= (mode_enc & 3) << 6; - - int r_quantval; - int r_uquantval; - quantize_and_unquantize_retain_top_two_bits(quantization_level, r_lowbits, &r_quantval, &r_uquantval); - - r_intval = (r_intval & ~0x3f) | (r_uquantval & 0x3f); - float r_fval = r_intval * mode_rscale; - - - // next, recompute G and B, then quantize and unquantize them. - float g_fval = r_fval - color.y; - float b_fval = r_fval - color.z; - if (g_fval < 0.0f) - g_fval = 0.0f; - else if (g_fval > 65535.0f) - g_fval = 65535.0f; - if (b_fval < 0.0f) - b_fval = 0.0f; - else if (b_fval > 65535.0f) - b_fval = 65535.0f; - - int g_intval = (int)floor(g_fval * mode_scale + 0.5f); - int b_intval = (int)floor(b_fval * mode_scale + 0.5f); - - - if (g_intval >= gb_intcutoff || b_intval >= gb_intcutoff) - { - continue; - } - - int g_lowbits = g_intval & 0x1f; - int b_lowbits = b_intval & 0x1f; - - int bit0 = 0; - int bit1 = 0; - int bit2 = 0; - int bit3 = 0; - - switch (mode) - { - case 0: - case 2: - bit0 = (r_intval >> 9) & 1; - break; - case 1: - case 3: - bit0 = (r_intval >> 8) & 1; - break; - case 4: - case 5: - bit0 = (g_intval >> 6) & 1; - break; - } - - switch (mode) - { - case 0: - case 1: - case 2: - case 3: - bit2 = (r_intval >> 7) & 1; - break; - case 4: - case 5: - bit2 = (b_intval >> 6) & 1; - break; - } - - switch (mode) - { - case 0: - case 2: - bit1 = (r_intval >> 8) & 1; - break; - case 1: - case 3: - case 4: - case 5: - bit1 = (g_intval >> 5) & 1; - break; - } - - switch (mode) - { - case 0: - bit3 = (r_intval >> 10) & 1; - break; - case 2: - bit3 = (r_intval >> 6) & 1; - break; - case 1: - case 3: - case 4: - case 5: - bit3 = (b_intval >> 5) & 1; - break; - } - - g_lowbits |= (mode_enc & 0x4) << 5; - b_lowbits |= (mode_enc & 0x8) << 4; - - g_lowbits |= bit0 << 6; - g_lowbits |= bit1 << 5; - b_lowbits |= bit2 << 6; - b_lowbits |= bit3 << 5; - - int g_quantval; - int b_quantval; - int g_uquantval; - int b_uquantval; - - quantize_and_unquantize_retain_top_four_bits(quantization_level, g_lowbits, &g_quantval, &g_uquantval); - - quantize_and_unquantize_retain_top_four_bits(quantization_level, b_lowbits, &b_quantval, &b_uquantval); - - g_intval = (g_intval & ~0x1f) | (g_uquantval & 0x1f); - b_intval = (b_intval & ~0x1f) | (b_uquantval & 0x1f); - - g_fval = g_intval * mode_rscale; - b_fval = b_intval * mode_rscale; - - - // finally, recompute the scale value, based on the errors - // introduced to red, green and blue. - - // If the error is positive, then the R,G,B errors combined have raised the color - // value overall; as such, the scale value needs to be increased. - float rgb_errorsum = (r_fval - color.x) + (r_fval - g_fval - color.y) + (r_fval - b_fval - color.z); - - float s_fval = s_base + rgb_errorsum * (1.0f / 3.0f); - if (s_fval < 0.0f) - s_fval = 0.0f; - else if (s_fval > 1e9) - s_fval = 1e9; - - int s_intval = (int)floor(s_fval * mode_scale + 0.5f); - - if (s_intval >= s_intcutoff) - { - continue; - } - - int s_lowbits = s_intval & 0x1f; - - int bit4; - int bit5; - int bit6; - switch (mode) - { - case 1: - bit6 = (r_intval >> 9) & 1; - break; - default: - bit6 = (s_intval >> 5) & 1; - break; - } - - switch (mode) - { - case 4: - bit5 = (r_intval >> 7) & 1; - break; - case 1: - bit5 = (r_intval >> 10) & 1; - break; - default: - bit5 = (s_intval >> 6) & 1; - break; - } - - switch (mode) - { - case 2: - bit4 = (s_intval >> 7) & 1; - break; - default: - bit4 = (r_intval >> 6) & 1; - break; - } - - - s_lowbits |= bit6 << 5; - s_lowbits |= bit5 << 6; - s_lowbits |= bit4 << 7; - - int s_quantval; - int s_uquantval; - - quantize_and_unquantize_retain_top_four_bits(quantization_level, s_lowbits, &s_quantval, &s_uquantval); - - s_intval = (s_intval & ~0x1f) | (s_uquantval & 0x1f); - s_fval = s_intval * mode_rscale; - output[0] = r_quantval; - output[1] = g_quantval; - output[2] = b_quantval; - output[3] = s_quantval; - - return; - } - - // failed to encode any of the modes above? In that case, - // encode using mode #5. - int i; - - float vals[4]; - int ivals[4]; - vals[0] = color_bak.x; - vals[1] = color_bak.y; - vals[2] = color_bak.z; - vals[3] = color_bak.w; - - float cvals[3]; - - for (i = 0; i < 3; i++) - { - if (vals[i] < 0.0f) - vals[i] = 0.0f; - else if (vals[i] > 65020.0f) - vals[i] = 65020.0f; - - ivals[i] = (int)floor(vals[i] * (1.0f / 512.0f) + 0.5f); - cvals[i] = ivals[i] * 512.0f; - } - - float rgb_errorsum = (cvals[0] - vals[0]) + (cvals[1] - vals[1]) + (cvals[2] - vals[2]); - vals[3] += rgb_errorsum * (1.0f / 3.0f); - - if (vals[3] < 0.0f) - vals[3] = 0.0f; - else if (vals[3] > 65020.0f) - vals[3] = 65020.0f; - - ivals[3] = (int)floor(vals[3] * (1.0f / 512.0f) + 0.5f); - - int encvals[4]; - - encvals[0] = (ivals[0] & 0x3f) | 0xC0; - encvals[1] = (ivals[1] & 0x7f) | 0x80; - encvals[2] = (ivals[2] & 0x7f) | 0x80; - encvals[3] = (ivals[3] & 0x7f) | ((ivals[0] & 0x40) << 1); - - for (i = 0; i < 4; i++) - { - int dummy; - quantize_and_unquantize_retain_top_four_bits(quantization_level, encvals[i], &(output[i]), &dummy); - } - - return; -} - - - - - - - -void quantize_hdr_rgb3(float4 color0, float4 color1, int output[6], int quantization_level) -{ - if (!(color0.x > 0.0f)) - color0.x = 0.0f; - else if (color0.x > 65535.0f) - color0.x = 65535.0f; - - if (!(color0.y > 0.0f)) - color0.y = 0.0f; - else if (color0.y > 65535.0f) - color0.y = 65535.0f; - - if (!(color0.z > 0.0f)) - color0.z = 0.0f; - else if (color0.z > 65535.0f) - color0.z = 65535.0f; - - if (!(color1.x > 0.0f)) - color1.x = 0.0f; - else if (color1.x > 65535.0f) - color1.x = 65535.0f; - - if (!(color1.y > 0.0f)) - color1.y = 0.0f; - else if (color1.y > 65535.0f) - color1.y = 65535.0f; - - if (!(color1.z > 0.0f)) - color1.z = 0.0f; - else if (color1.z > 65535.0f) - color1.z = 65535.0f; - - float4 color0_bak = color0; - float4 color1_bak = color1; - - int majcomp; - if (color1.x > color1.y && color1.x > color1.z) - majcomp = 0; // red is largest - else if (color1.y > color1.z) - majcomp = 1; // green is largest - else - majcomp = 2; // blue is largest - - // swizzle the components - switch (majcomp) - { - case 1: // red-green swap - color0 = color0.yxzw; - color1 = color1.yxzw; - break; - case 2: // red-blue swap - color0 = color0.zyxw; - color1 = color1.zyxw; - break; - default: - break; - } - - float a_base = color1.x; - if (a_base < 0.0f) - a_base = 0.0f; - else if (a_base > 65535.0f) - a_base = 65535.0f; - - - float b0_base = a_base - color1.y; - float b1_base = a_base - color1.z; - float c_base = a_base - color0.x; - float d0_base = a_base - b0_base - c_base - color0.y; - float d1_base = a_base - b1_base - c_base - color0.z; - - - - // number of bits in the various fields in the various modes - static const int mode_bits[8][4] = { - {9, 7, 6, 7}, - {9, 8, 6, 6}, - {10, 6, 7, 7}, - {10, 7, 7, 6}, - {11, 8, 6, 5}, - {11, 6, 8, 6}, - {12, 7, 7, 5}, - {12, 6, 7, 6} - }; - - // cutoffs to use for the computed values of a,b,c,d, assuming the - // range 0..65535 are LNS values corresponding to fp16. - static const float mode_cutoffs[8][4] = { - {16384, 8192, 8192, 8}, // mode 0: 9,7,6,7 - {32768, 8192, 4096, 8}, // mode 1: 9,8,6,6 - {4096, 8192, 4096, 4}, // mode 2: 10,6,7,7 - {8192, 8192, 2048, 4}, // mode 3: 10,7,7,6 - {8192, 2048, 512, 2}, // mode 4: 11,8,6,5 - {2048, 8192, 1024, 2}, // mode 5: 11,6,8,6 - {2048, 2048, 256, 1}, // mode 6: 12,7,7,5 - {1024, 2048, 512, 1}, // mode 7: 12,6,7,6 - }; - - static const float mode_scales[8] = { - 1.0f / 128.0f, - 1.0f / 128.0f, - 1.0f / 64.0f, - 1.0f / 64.0f, - 1.0f / 32.0f, - 1.0f / 32.0f, - 1.0f / 16.0f, - 1.0f / 16.0f, - }; - - // scaling factors when going from what was encoded in the mode to 16 bits. - static const float mode_rscales[8] = { - 128.0f, - 128.0f, - 64.0f, - 64.0f, - 32.0f, - 32.0f, - 16.0f, - 16.0f - }; - - - // try modes one by one, with the highest-precision mode first. - int mode; - for (mode = 7; mode >= 0; mode--) - { - // for each mode, test if we can in fact accommodate - // the computed b,c,d values. If we clearly can't, then we skip to the next mode. - - float b_cutoff = mode_cutoffs[mode][0]; - float c_cutoff = mode_cutoffs[mode][1]; - float d_cutoff = mode_cutoffs[mode][2]; - - if (b0_base > b_cutoff || b1_base > b_cutoff || c_base > c_cutoff || fabs(d0_base) > d_cutoff || fabs(d1_base) > d_cutoff) - { - continue; - } - - float mode_scale = mode_scales[mode]; - float mode_rscale = mode_rscales[mode]; - - int b_intcutoff = 1 << mode_bits[mode][1]; - int c_intcutoff = 1 << mode_bits[mode][2]; - int d_intcutoff = 1 << (mode_bits[mode][3] - 1); - - // first, quantize and unquantize A, with the assumption that its high bits can be handled safely. - int a_intval = (int)floor(a_base * mode_scale + 0.5f); - int a_lowbits = a_intval & 0xFF; - - int a_quantval = color_quantization_tables[quantization_level][a_lowbits]; - int a_uquantval = color_unquantization_tables[quantization_level][a_quantval]; - a_intval = (a_intval & ~0xFF) | a_uquantval; - float a_fval = a_intval * mode_rscale; - - // next, recompute C, then quantize and unquantize it - float c_fval = a_fval - color0.x; - if (c_fval < 0.0f) - c_fval = 0.0f; - else if (c_fval > 65535.0f) - c_fval = 65535.0f; - - int c_intval = (int)floor(c_fval * mode_scale + 0.5f); - - if (c_intval >= c_intcutoff) - { - continue; - } - - int c_lowbits = c_intval & 0x3f; - - c_lowbits |= (mode & 1) << 7; - c_lowbits |= (a_intval & 0x100) >> 2; - - int c_quantval; - int c_uquantval; - quantize_and_unquantize_retain_top_two_bits(quantization_level, c_lowbits, &c_quantval, &c_uquantval); - c_intval = (c_intval & ~0x3F) | (c_uquantval & 0x3F); - c_fval = c_intval * mode_rscale; - - - // next, recompute B0 and B1, then quantize and unquantize them - float b0_fval = a_fval - color1.y; - float b1_fval = a_fval - color1.z; - if (b0_fval < 0.0f) - b0_fval = 0.0f; - else if (b0_fval > 65535.0f) - b0_fval = 65535.0f; - if (b1_fval < 0.0f) - b1_fval = 0.0f; - else if (b1_fval > 65535.0f) - b1_fval = 65535.0f; - - int b0_intval = (int)floor(b0_fval * mode_scale + 0.5f); - int b1_intval = (int)floor(b1_fval * mode_scale + 0.5f); - - if (b0_intval >= b_intcutoff || b1_intval >= b_intcutoff) - { - continue; - } - - - - int b0_lowbits = b0_intval & 0x3f; - int b1_lowbits = b1_intval & 0x3f; - - int bit0 = 0; - int bit1 = 0; - switch (mode) - { - case 0: - case 1: - case 3: - case 4: - case 6: - bit0 = (b0_intval >> 6) & 1; - break; - case 2: - case 5: - case 7: - bit0 = (a_intval >> 9) & 1; - break; - } - - switch (mode) - { - case 0: - case 1: - case 3: - case 4: - case 6: - bit1 = (b1_intval >> 6) & 1; - break; - case 2: - bit1 = (c_intval >> 6) & 1; - break; - case 5: - case 7: - bit1 = (a_intval >> 10) & 1; - break; - } - - b0_lowbits |= bit0 << 6; - b1_lowbits |= bit1 << 6; - - b0_lowbits |= ((mode >> 1) & 1) << 7; - b1_lowbits |= ((mode >> 2) & 1) << 7; - - int b0_quantval; - int b1_quantval; - int b0_uquantval; - int b1_uquantval; - - quantize_and_unquantize_retain_top_two_bits(quantization_level, b0_lowbits, &b0_quantval, &b0_uquantval); - - quantize_and_unquantize_retain_top_two_bits(quantization_level, b1_lowbits, &b1_quantval, &b1_uquantval); - - b0_intval = (b0_intval & ~0x3f) | (b0_uquantval & 0x3f); - b1_intval = (b1_intval & ~0x3f) | (b1_uquantval & 0x3f); - b0_fval = b0_intval * mode_rscale; - b1_fval = b1_intval * mode_rscale; - - - // finally, recompute D0 and D1, then quantize and unquantize them - float d0_fval = a_fval - b0_fval - c_fval - color0.y; - float d1_fval = a_fval - b1_fval - c_fval - color0.z; - - if (d0_fval < -65535.0f) - d0_fval = -65535.0f; - else if (d0_fval > 65535.0f) - d0_fval = 65535.0f; - - if (d1_fval < -65535.0f) - d1_fval = -65535.0f; - else if (d1_fval > 65535.0f) - d1_fval = 65535.0f; - - int d0_intval = (int)floor(d0_fval * mode_scale + 0.5f); - int d1_intval = (int)floor(d1_fval * mode_scale + 0.5f); - - if (abs(d0_intval) >= d_intcutoff || abs(d1_intval) >= d_intcutoff) - continue; - - // d0_intval += mode_dbiases[mode]; - // d1_intval += mode_dbiases[mode]; - - int d0_lowbits = d0_intval & 0x1f; - int d1_lowbits = d1_intval & 0x1f; - - int bit2 = 0; - int bit3 = 0; - int bit4; - int bit5; - switch (mode) - { - case 0: - case 2: - bit2 = (d0_intval >> 6) & 1; - break; - case 1: - case 4: - bit2 = (b0_intval >> 7) & 1; - break; - case 3: - bit2 = (a_intval >> 9) & 1; - break; - case 5: - bit2 = (c_intval >> 7) & 1; - break; - case 6: - case 7: - bit2 = (a_intval >> 11) & 1; - break; - } - switch (mode) - { - case 0: - case 2: - bit3 = (d1_intval >> 6) & 1; - break; - case 1: - case 4: - bit3 = (b1_intval >> 7) & 1; - break; - case 3: - case 5: - case 6: - case 7: - bit3 = (c_intval >> 6) & 1; - break; - } - - switch (mode) - { - case 4: - case 6: - bit4 = (a_intval >> 9) & 1; - bit5 = (a_intval >> 10) & 1; - break; - default: - bit4 = (d0_intval >> 5) & 1; - bit5 = (d1_intval >> 5) & 1; - break; - } - - d0_lowbits |= bit2 << 6; - d1_lowbits |= bit3 << 6; - d0_lowbits |= bit4 << 5; - d1_lowbits |= bit5 << 5; - - d0_lowbits |= (majcomp & 1) << 7; - d1_lowbits |= ((majcomp >> 1) & 1) << 7; - - int d0_quantval; - int d1_quantval; - int d0_uquantval; - int d1_uquantval; - - quantize_and_unquantize_retain_top_four_bits(quantization_level, d0_lowbits, &d0_quantval, &d0_uquantval); - - quantize_and_unquantize_retain_top_four_bits(quantization_level, d1_lowbits, &d1_quantval, &d1_uquantval); - - output[0] = a_quantval; - output[1] = c_quantval; - output[2] = b0_quantval; - output[3] = b1_quantval; - output[4] = d0_quantval; - output[5] = d1_quantval; - return; - } - - // neither of the modes fit? In this case, we will use a flat representation - // for storing data, using 8 bits for red and green, and 7 bits for blue. - // This gives color accuracy roughly similar to LDR 4:4:3 which is not at all great - // but usable. This representation is used if the light color is more than 4x the - // color value of the dark color. - int i; - float vals[6]; - vals[0] = color0_bak.x; - vals[1] = color1_bak.x; - vals[2] = color0_bak.y; - vals[3] = color1_bak.y; - vals[4] = color0_bak.z; - vals[5] = color1_bak.z; - - - for (i = 0; i < 6; i++) - { - if (vals[i] < 0.0f) - vals[i] = 0.0f; - else if (vals[i] > 65020.0f) - vals[i] = 65020.0f; - } - for (i = 0; i < 4; i++) - { - int idx = (int)floor(vals[i] * 1.0f / 256.0f + 0.5f); - output[i] = color_quantization_tables[quantization_level][idx]; - } - for (i = 4; i < 6; i++) - { - int dummy; - int idx = (int)floor(vals[i] * 1.0f / 512.0f + 0.5f) + 128; - quantize_and_unquantize_retain_top_two_bits(quantization_level, idx, &(output[i]), &dummy); - } - - return; -} - - - - - -void quantize_hdr_rgb_ldr_alpha3(float4 color0, float4 color1, int output[8], int quantization_level) -{ - color0.w *= (1.0f / 257.0f); - color1.w *= (1.0f / 257.0f); - - quantize_hdr_rgb3(color0, color1, output, quantization_level); - - float a0 = clamp255(color0.w); - float a1 = clamp255(color1.w); - int ai0 = color_quantization_tables[quantization_level][(int)floor(a0 + 0.5f)]; - int ai1 = color_quantization_tables[quantization_level][(int)floor(a1 + 0.5f)]; - - output[6] = ai0; - output[7] = ai1; -} - - - -void quantize_hdr_luminance_large_range3(float4 color0, float4 color1, int output[2], int quantization_level) -{ - - float lum1 = (color1.x + color1.y + color1.z) * (1.0f / 3.0f); - float lum0 = (color0.x + color0.y + color0.z) * (1.0f / 3.0f); - - if (lum1 < lum0) - { - float avg = (lum0 + lum1) * 0.5f; - lum0 = avg; - lum1 = avg; - } - - int ilum1 = static_cast < int >(floor(lum1 + 0.5f)); - int ilum0 = static_cast < int >(floor(lum0 + 0.5f)); - - // find the closest encodable point in the upper half of the code-point space - int upper_v0 = (ilum0 + 128) >> 8; - int upper_v1 = (ilum1 + 128) >> 8; - - if (upper_v0 < 0) - upper_v0 = 0; - else if (upper_v0 > 255) - upper_v0 = 255; - - if (upper_v1 < 0) - upper_v1 = 0; - else if (upper_v1 > 255) - upper_v1 = 255; - - // find the closest encodable point in the lower half of the code-point space - int lower_v0 = (ilum1 + 256) >> 8; - int lower_v1 = ilum0 >> 8; - - if (lower_v0 < 0) - lower_v0 = 0; - else if (lower_v0 > 255) - lower_v0 = 255; - - if (lower_v1 < 0) - lower_v1 = 0; - else if (lower_v1 > 255) - lower_v1 = 255; - - // determine the distance between the point in code-point space and the input value - int upper0_dec = upper_v0 << 8; - int upper1_dec = upper_v1 << 8; - int lower0_dec = (lower_v1 << 8) + 128; - int lower1_dec = (lower_v0 << 8) - 128; - - - int upper0_diff = upper0_dec - ilum0; - int upper1_diff = upper1_dec - ilum1; - int lower0_diff = lower0_dec - ilum0; - int lower1_diff = lower1_dec - ilum1; - - int upper_error = (upper0_diff * upper0_diff) + (upper1_diff * upper1_diff); - int lower_error = (lower0_diff * lower0_diff) + (lower1_diff * lower1_diff); - - int v0, v1; - if (upper_error < lower_error) - { - v0 = upper_v0; - v1 = upper_v1; - } - else - { - v0 = lower_v0; - v1 = lower_v1; - } - - // OK; encode. - output[0] = color_quantization_tables[quantization_level][v0]; - output[1] = color_quantization_tables[quantization_level][v1]; -} - - - -int try_quantize_hdr_luminance_small_range3(float4 color0, float4 color1, int output[2], int quantization_level) -{ - float lum1 = (color1.x + color1.y + color1.z) * (1.0f / 3.0f); - float lum0 = (color0.x + color0.y + color0.z) * (1.0f / 3.0f); - - if (lum1 < lum0) - { - float avg = (lum0 + lum1) * 0.5f; - lum0 = avg; - lum1 = avg; - } - - int ilum1 = static_cast < int >(floor(lum1 + 0.5f)); - int ilum0 = static_cast < int >(floor(lum0 + 0.5f)); - - // difference of more than a factor-of-2 results in immediate failure. - if (ilum1 - ilum0 > 2048) - return 0; - - int lowval, highval, diffval; - int v0, v1; - int v0e, v1e; - int v0d, v1d; - - // first, try to encode the high-precision submode - lowval = (ilum0 + 16) >> 5; - highval = (ilum1 + 16) >> 5; - - if (lowval < 0) - lowval = 0; - else if (lowval > 2047) - lowval = 2047; - - if (highval < 0) - highval = 0; - else if (highval > 2047) - highval = 2047; - - v0 = lowval & 0x7F; - v0e = color_quantization_tables[quantization_level][v0]; - v0d = color_unquantization_tables[quantization_level][v0e]; - if ((v0d & 0x80) == 0x80) - goto LOW_PRECISION_SUBMODE; - - lowval = (lowval & ~0x7F) | (v0d & 0x7F); - diffval = highval - lowval; - if (diffval < 0 || diffval > 15) - goto LOW_PRECISION_SUBMODE; - - v1 = ((lowval >> 3) & 0xF0) | diffval; - v1e = color_quantization_tables[quantization_level][v1]; - v1d = color_unquantization_tables[quantization_level][v1e]; - if ((v1d & 0xF0) != (v1 & 0xF0)) - goto LOW_PRECISION_SUBMODE; - - output[0] = v0e; - output[1] = v1e; - return 1; - - - // failed to encode the high-precision submode; well, then try to encode the - // low-precision submode. - LOW_PRECISION_SUBMODE: - - lowval = (ilum0 + 32) >> 6; - highval = (ilum1 + 32) >> 6; - if (lowval < 0) - lowval = 0; - else if (lowval > 1023) - lowval = 1023; - if (highval < 0) - highval = 0; - else if (highval > 1023) - highval = 1023; - - v0 = (lowval & 0x7F) | 0x80; - v0e = color_quantization_tables[quantization_level][v0]; - v0d = color_unquantization_tables[quantization_level][v0e]; - if ((v0d & 0x80) == 0) - return 0; - - lowval = (lowval & ~0x7F) | (v0d & 0x7F); - diffval = highval - lowval; - if (diffval < 0 || diffval > 31) - return 0; - - v1 = ((lowval >> 2) & 0xE0) | diffval; - v1e = color_quantization_tables[quantization_level][v1]; - v1d = color_unquantization_tables[quantization_level][v1e]; - if ((v1d & 0xE0) != (v1 & 0xE0)) - return 0;; - - output[0] = v0e; - output[1] = v1e; - return 1; -} - - -void quantize_hdr_alpha3(float alpha0, float alpha1, int output[2], int quantization_level) -{ - int i; - - if (alpha0 < 0) - alpha0 = 0; - else if (alpha0 > 65280) - alpha0 = 65280; - - if (alpha1 < 0) - alpha1 = 0; - else if (alpha1 > 65280) - alpha1 = 65280; - - int ialpha0 = static_cast < int >(floor(alpha0 + 0.5f)); - int ialpha1 = static_cast < int >(floor(alpha1 + 0.5f)); - - int val0, val1, diffval; - int v6, v7; - int v6e, v7e; - int v6d, v7d; - - // try to encode one of the delta submodes, in decreasing-precision order. - for (i = 2; i >= 0; i--) - { - val0 = (ialpha0 + (128 >> i)) >> (8 - i); - val1 = (ialpha1 + (128 >> i)) >> (8 - i); - - v6 = (val0 & 0x7F) | ((i & 1) << 7); - v6e = color_quantization_tables[quantization_level][v6]; - v6d = color_unquantization_tables[quantization_level][v6e]; - - if ((v6 ^ v6d) & 0x80) - continue; - - val0 = (val0 & ~0x7f) | (v6d & 0x7f); - diffval = val1 - val0; - int cutoff = 32 >> i; - int mask = 2 * cutoff - 1; - - if (diffval < -cutoff || diffval >= cutoff) - continue; - - v7 = ((i & 2) << 6) | ((val0 >> 7) << (6 - i)) | (diffval & mask); - v7e = color_quantization_tables[quantization_level][v7]; - v7d = color_unquantization_tables[quantization_level][v7e]; - - static const int testbits[3] = { 0xE0, 0xF0, 0xF8 }; - - if ((v7 ^ v7d) & testbits[i]) - continue; - - output[0] = v6e; - output[1] = v7e; - return; - } - - // could not encode any of the delta modes; instead encode a flat value - val0 = (ialpha0 + 256) >> 9; - val1 = (ialpha1 + 256) >> 9; - v6 = val0 | 0x80; - v7 = val1 | 0x80; - - v6e = color_quantization_tables[quantization_level][v6]; - v7e = color_quantization_tables[quantization_level][v7]; - output[0] = v6e; - output[1] = v7e; - - return; -} - - - -void quantize_hdr_rgb_alpha3(float4 color0, float4 color1, int output[8], int quantization_level) -{ - quantize_hdr_rgb3(color0, color1, output, quantization_level); - quantize_hdr_alpha3(color0.w, color1.w, output + 6, quantization_level); -} - - - -/* - Quantize a color. When quantizing an RGB or RGBA color, the quantizer may choose a - delta-based representation; as such, it will report back the format it actually used. -*/ -int pack_color_endpoints(astc_decode_mode decode_mode, float4 color0, float4 color1, float4 rgbs_color, float4 rgbo_color, float2 luminances, // ! Unused - int format, int *output, int quantization_level) -{ - - IGNORE(luminances); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : format=%d quantization_level=%d\n", __func__, format, quantization_level); - printf("Color 0: <%g %g %g %g>\n", color0.x, color0.y, color0.z, color0.w); - printf("Color 1: <%g %g %g %g>\n", color1.x, color1.y, color1.z, color1.w); - - } - #endif - - // we do not support negative colors. - color0.x = MAX(color0.x, 0.0f); - color0.y = MAX(color0.y, 0.0f); - color0.z = MAX(color0.z, 0.0f); - color0.w = MAX(color0.w, 0.0f); - color1.x = MAX(color1.x, 0.0f); - color1.y = MAX(color1.y, 0.0f); - color1.z = MAX(color1.z, 0.0f); - color1.w = MAX(color1.w, 0.0f); - - - int retval; - - switch (format) - { - case FMT_RGB: - if (quantization_level <= 18) - { - if (try_quantize_rgb_delta_blue_contract(color0, color1, output, quantization_level)) - { - retval = FMT_RGB_DELTA; - break; - } - if (try_quantize_rgb_delta(color0, color1, output, quantization_level)) - { - retval = FMT_RGB_DELTA; - break; - } - } - if (try_quantize_rgb_blue_contract(color0, color1, output, quantization_level)) - { - retval = FMT_RGB; - break; - } - quantize_rgb(color0, color1, output, quantization_level); - retval = FMT_RGB; - break; - - case FMT_RGBA: - if (quantization_level <= 18) - { - if (try_quantize_rgba_delta_blue_contract(color0, color1, output, quantization_level)) - { - retval = FMT_RGBA_DELTA; - break; - } - if (try_quantize_rgba_delta(color0, color1, output, quantization_level)) - { - retval = FMT_RGBA_DELTA; - break; - } - } - if (try_quantize_rgba_blue_contract(color0, color1, output, quantization_level)) - { - retval = FMT_RGBA; - break; - } - quantize_rgba(color0, color1, output, quantization_level); - retval = FMT_RGBA; - break; - - case FMT_RGB_SCALE: - quantize_rgbs_new(rgbs_color, output, quantization_level); - // quantize_rgbs( color0, color1, output, quantization_level ); - retval = FMT_RGB_SCALE; - break; - - case FMT_HDR_RGB_SCALE: - quantize_hdr_rgbo3(rgbo_color, output, quantization_level); - - // quantize_hdr_rgb_scale( rgbo_color, output, quantization_level ); - retval = FMT_HDR_RGB_SCALE; - break; - - case FMT_HDR_RGB: - quantize_hdr_rgb3(color0, color1, output, quantization_level); - - // quantize_hdr_rgb_rgba( color0, color1, 0, output, quantization_level ); - retval = FMT_HDR_RGB; - break; - - case FMT_RGB_SCALE_ALPHA: - quantize_rgbs_alpha_new(color0, color1, rgbs_color, output, quantization_level); - // quantize_rgbs_alpha( color0, color1, output, quantization_level ); - retval = FMT_RGB_SCALE_ALPHA; - break; - - case FMT_HDR_LUMINANCE_SMALL_RANGE: - case FMT_HDR_LUMINANCE_LARGE_RANGE: - if (try_quantize_hdr_luminance_small_range3(color0, color1, output, quantization_level)) - { - retval = FMT_HDR_LUMINANCE_SMALL_RANGE; - break; - } - quantize_hdr_luminance_large_range3(color0, color1, output, quantization_level); - retval = FMT_HDR_LUMINANCE_LARGE_RANGE; - break; - - case FMT_LUMINANCE: - quantize_luminance(color0, color1, output, quantization_level); - retval = FMT_LUMINANCE; - break; - - case FMT_LUMINANCE_ALPHA: - if (quantization_level <= 18) - { - if (try_quantize_luminance_alpha_delta(color0, color1, output, quantization_level)) - { - retval = FMT_LUMINANCE_ALPHA_DELTA; - break; - } - } - quantize_luminance_alpha(color0, color1, output, quantization_level); - retval = FMT_LUMINANCE_ALPHA; - break; - - case FMT_HDR_RGB_LDR_ALPHA: - quantize_hdr_rgb_ldr_alpha3(color0, color1, output, quantization_level); - retval = FMT_HDR_RGB_LDR_ALPHA; - break; - - case FMT_HDR_RGBA: - quantize_hdr_rgb_alpha3(color0, color1, output, quantization_level); - retval = FMT_HDR_RGBA; - break; - - default: - ASTC_CODEC_INTERNAL_ERROR; - quantize0(color0, color1, output, quantization_level); - retval = FMT_LUMINANCE; - break; - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - int i; - printf("Quantized to format %d\n", retval); - printf("Quantized color:"); - - for (i = 0; i < 8; i++) - printf(" %X", output[i]); - - ushort4 res0; - ushort4 res1; - int rgb_hdr; - int alpha_hdr; - int nan_endpoint; - - unpack_color_endpoints(decode_mode, retval, quantization_level, output, &rgb_hdr, &alpha_hdr, &nan_endpoint, &res0, &res1); - printf("rgb-hdr=%d alpha-hdr=%d nan-endpoint=%d\n", rgb_hdr, alpha_hdr, nan_endpoint); - - printf("Unquantized color 0: <%u %u %u %u>\n", res0.x, res0.y, res0.z, res0.w); - printf("Unquantized color 1: <%u %u %u %u>\n", res1.x, res1.y, res1.z, res1.w); - printf("\n\n"); - } - #endif - - return retval; -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_color_unquantize.cpp b/3rdparty/bimg/3rdparty/astc/astc_color_unquantize.cpp deleted file mode 100644 index 5090cfdfd75..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_color_unquantize.cpp +++ /dev/null @@ -1,970 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Color unquantization functions for ASTC. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -#include "mathlib.h" -#include "softfloat.h" - -int rgb_delta_unpack(const int input[6], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - // unquantize the color endpoints - int r0 = color_unquantization_tables[quantization_level][input[0]]; - int g0 = color_unquantization_tables[quantization_level][input[2]]; - int b0 = color_unquantization_tables[quantization_level][input[4]]; - - int r1 = color_unquantization_tables[quantization_level][input[1]]; - int g1 = color_unquantization_tables[quantization_level][input[3]]; - int b1 = color_unquantization_tables[quantization_level][input[5]]; - - // perform the bit-transfer procedure - r0 |= (r1 & 0x80) << 1; - g0 |= (g1 & 0x80) << 1; - b0 |= (b1 & 0x80) << 1; - r1 &= 0x7F; - g1 &= 0x7F; - b1 &= 0x7F; - if (r1 & 0x40) - r1 -= 0x80; - if (g1 & 0x40) - g1 -= 0x80; - if (b1 & 0x40) - b1 -= 0x80; - - r0 >>= 1; - g0 >>= 1; - b0 >>= 1; - r1 >>= 1; - g1 >>= 1; - b1 >>= 1; - - int rgbsum = r1 + g1 + b1; - - r1 += r0; - g1 += g0; - b1 += b0; - - - int retval; - - int r0e, g0e, b0e; - int r1e, g1e, b1e; - - if (rgbsum >= 0) - { - r0e = r0; - g0e = g0; - b0e = b0; - - r1e = r1; - g1e = g1; - b1e = b1; - - retval = 0; - } - else - { - r0e = (r1 + b1) >> 1; - g0e = (g1 + b1) >> 1; - b0e = b1; - - r1e = (r0 + b0) >> 1; - g1e = (g0 + b0) >> 1; - b1e = b0; - - retval = 1; - } - - if (r0e < 0) - r0e = 0; - else if (r0e > 255) - r0e = 255; - - if (g0e < 0) - g0e = 0; - else if (g0e > 255) - g0e = 255; - - if (b0e < 0) - b0e = 0; - else if (b0e > 255) - b0e = 255; - - if (r1e < 0) - r1e = 0; - else if (r1e > 255) - r1e = 255; - - if (g1e < 0) - g1e = 0; - else if (g1e > 255) - g1e = 255; - - if (b1e < 0) - b1e = 0; - else if (b1e > 255) - b1e = 255; - - output0->x = r0e; - output0->y = g0e; - output0->z = b0e; - output0->w = 0xFF; - - output1->x = r1e; - output1->y = g1e; - output1->z = b1e; - output1->w = 0xFF; - - return retval; -} - - -int rgb_unpack(const int input[6], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - - int ri0b = color_unquantization_tables[quantization_level][input[0]]; - int ri1b = color_unquantization_tables[quantization_level][input[1]]; - int gi0b = color_unquantization_tables[quantization_level][input[2]]; - int gi1b = color_unquantization_tables[quantization_level][input[3]]; - int bi0b = color_unquantization_tables[quantization_level][input[4]]; - int bi1b = color_unquantization_tables[quantization_level][input[5]]; - - if (ri0b + gi0b + bi0b > ri1b + gi1b + bi1b) - { - // blue-contraction - ri0b = (ri0b + bi0b) >> 1; - gi0b = (gi0b + bi0b) >> 1; - ri1b = (ri1b + bi1b) >> 1; - gi1b = (gi1b + bi1b) >> 1; - - output0->x = ri1b; - output0->y = gi1b; - output0->z = bi1b; - output0->w = 255; - - output1->x = ri0b; - output1->y = gi0b; - output1->z = bi0b; - output1->w = 255; - return 1; - } - else - { - output0->x = ri0b; - output0->y = gi0b; - output0->z = bi0b; - output0->w = 255; - - output1->x = ri1b; - output1->y = gi1b; - output1->z = bi1b; - output1->w = 255; - return 0; - } -} - - - - -void rgba_unpack(const int input[8], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int order = rgb_unpack(input, quantization_level, output0, output1); - if (order == 0) - { - output0->w = color_unquantization_tables[quantization_level][input[6]]; - output1->w = color_unquantization_tables[quantization_level][input[7]]; - } - else - { - output0->w = color_unquantization_tables[quantization_level][input[7]]; - output1->w = color_unquantization_tables[quantization_level][input[6]]; - } -} - - - -void rgba_delta_unpack(const int input[8], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int a0 = color_unquantization_tables[quantization_level][input[6]]; - int a1 = color_unquantization_tables[quantization_level][input[7]]; - a0 |= (a1 & 0x80) << 1; - a1 &= 0x7F; - if (a1 & 0x40) - a1 -= 0x80; - a0 >>= 1; - a1 >>= 1; - a1 += a0; - - if (a1 < 0) - a1 = 0; - else if (a1 > 255) - a1 = 255; - - int order = rgb_delta_unpack(input, quantization_level, output0, output1); - if (order == 0) - { - output0->w = a0; - output1->w = a1; - } - else - { - output0->w = a1; - output1->w = a0; - } -} - - -void rgb_scale_unpack(const int input[4], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int ir = color_unquantization_tables[quantization_level][input[0]]; - int ig = color_unquantization_tables[quantization_level][input[1]]; - int ib = color_unquantization_tables[quantization_level][input[2]]; - - int iscale = color_unquantization_tables[quantization_level][input[3]]; - - *output1 = ushort4(ir, ig, ib, 255); - *output0 = ushort4((ir * iscale) >> 8, (ig * iscale) >> 8, (ib * iscale) >> 8, 255); -} - - - -void rgb_scale_alpha_unpack(const int input[6], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - rgb_scale_unpack(input, quantization_level, output0, output1); - output0->w = color_unquantization_tables[quantization_level][input[4]]; - output1->w = color_unquantization_tables[quantization_level][input[5]]; - -} - - -void luminance_unpack(const int input[2], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int lum0 = color_unquantization_tables[quantization_level][input[0]]; - int lum1 = color_unquantization_tables[quantization_level][input[1]]; - *output0 = ushort4(lum0, lum0, lum0, 255); - *output1 = ushort4(lum1, lum1, lum1, 255); -} - - -void luminance_delta_unpack(const int input[2], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int v0 = color_unquantization_tables[quantization_level][input[0]]; - int v1 = color_unquantization_tables[quantization_level][input[1]]; - int l0 = (v0 >> 2) | (v1 & 0xC0); - int l1 = l0 + (v1 & 0x3F); - - if (l1 > 255) - l1 = 255; - - *output0 = ushort4(l0, l0, l0, 255); - *output1 = ushort4(l1, l1, l1, 255); -} - - - - -void luminance_alpha_unpack(const int input[4], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int lum0 = color_unquantization_tables[quantization_level][input[0]]; - int lum1 = color_unquantization_tables[quantization_level][input[1]]; - int alpha0 = color_unquantization_tables[quantization_level][input[2]]; - int alpha1 = color_unquantization_tables[quantization_level][input[3]]; - *output0 = ushort4(lum0, lum0, lum0, alpha0); - *output1 = ushort4(lum1, lum1, lum1, alpha1); -} - - -void luminance_alpha_delta_unpack(const int input[4], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int lum0 = color_unquantization_tables[quantization_level][input[0]]; - int lum1 = color_unquantization_tables[quantization_level][input[1]]; - int alpha0 = color_unquantization_tables[quantization_level][input[2]]; - int alpha1 = color_unquantization_tables[quantization_level][input[3]]; - - lum0 |= (lum1 & 0x80) << 1; - alpha0 |= (alpha1 & 0x80) << 1; - lum1 &= 0x7F; - alpha1 &= 0x7F; - if (lum1 & 0x40) - lum1 -= 0x80; - if (alpha1 & 0x40) - alpha1 -= 0x80; - - lum0 >>= 1; - lum1 >>= 1; - alpha0 >>= 1; - alpha1 >>= 1; - lum1 += lum0; - alpha1 += alpha0; - - if (lum1 < 0) - lum1 = 0; - else if (lum1 > 255) - lum1 = 255; - - if (alpha1 < 0) - alpha1 = 0; - else if (alpha1 > 255) - alpha1 = 255; - - *output0 = ushort4(lum0, lum0, lum0, alpha0); - *output1 = ushort4(lum1, lum1, lum1, alpha1); -} - - - - -// RGB-offset format -void hdr_rgbo_unpack3(const int input[4], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int v0 = color_unquantization_tables[quantization_level][input[0]]; - int v1 = color_unquantization_tables[quantization_level][input[1]]; - int v2 = color_unquantization_tables[quantization_level][input[2]]; - int v3 = color_unquantization_tables[quantization_level][input[3]]; - - int modeval = ((v0 & 0xC0) >> 6) | (((v1 & 0x80) >> 7) << 2) | (((v2 & 0x80) >> 7) << 3); - - int majcomp; - int mode; - if ((modeval & 0xC) != 0xC) - { - majcomp = modeval >> 2; - mode = modeval & 3; - } - else if (modeval != 0xF) - { - majcomp = modeval & 3; - mode = 4; - } - else - { - majcomp = 0; - mode = 5; - } - - int red = v0 & 0x3F; - int green = v1 & 0x1F; - int blue = v2 & 0x1F; - int scale = v3 & 0x1F; - - int bit0 = (v1 >> 6) & 1; - int bit1 = (v1 >> 5) & 1; - int bit2 = (v2 >> 6) & 1; - int bit3 = (v2 >> 5) & 1; - int bit4 = (v3 >> 7) & 1; - int bit5 = (v3 >> 6) & 1; - int bit6 = (v3 >> 5) & 1; - - int ohcomp = 1 << mode; - - if (ohcomp & 0x30) - green |= bit0 << 6; - if (ohcomp & 0x3A) - green |= bit1 << 5; - if (ohcomp & 0x30) - blue |= bit2 << 6; - if (ohcomp & 0x3A) - blue |= bit3 << 5; - - if (ohcomp & 0x3D) - scale |= bit6 << 5; - if (ohcomp & 0x2D) - scale |= bit5 << 6; - if (ohcomp & 0x04) - scale |= bit4 << 7; - - if (ohcomp & 0x3B) - red |= bit4 << 6; - if (ohcomp & 0x04) - red |= bit3 << 6; - - if (ohcomp & 0x10) - red |= bit5 << 7; - if (ohcomp & 0x0F) - red |= bit2 << 7; - - if (ohcomp & 0x05) - red |= bit1 << 8; - if (ohcomp & 0x0A) - red |= bit0 << 8; - - if (ohcomp & 0x05) - red |= bit0 << 9; - if (ohcomp & 0x02) - red |= bit6 << 9; - - if (ohcomp & 0x01) - red |= bit3 << 10; - if (ohcomp & 0x02) - red |= bit5 << 10; - - - // expand to 12 bits. - static const int shamts[6] = { 1, 1, 2, 3, 4, 5 }; - int shamt = shamts[mode]; - red <<= shamt; - green <<= shamt; - blue <<= shamt; - scale <<= shamt; - - // on modes 0 to 4, the values stored for "green" and "blue" are differentials, - // not absolute values. - if (mode != 5) - { - green = red - green; - blue = red - blue; - } - - // switch around components. - int temp; - switch (majcomp) - { - case 1: - temp = red; - red = green; - green = temp; - break; - case 2: - temp = red; - red = blue; - blue = temp; - break; - default: - break; - } - - - int red0 = red - scale; - int green0 = green - scale; - int blue0 = blue - scale; - - // clamp to [0,0xFFF]. - if (red < 0) - red = 0; - if (green < 0) - green = 0; - if (blue < 0) - blue = 0; - - if (red0 < 0) - red0 = 0; - if (green0 < 0) - green0 = 0; - if (blue0 < 0) - blue0 = 0; - - *output0 = ushort4(red0 << 4, green0 << 4, blue0 << 4, 0x7800); - *output1 = ushort4(red << 4, green << 4, blue << 4, 0x7800); -} - - - -void hdr_rgb_unpack3(const int input[6], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - - int v0 = color_unquantization_tables[quantization_level][input[0]]; - int v1 = color_unquantization_tables[quantization_level][input[1]]; - int v2 = color_unquantization_tables[quantization_level][input[2]]; - int v3 = color_unquantization_tables[quantization_level][input[3]]; - int v4 = color_unquantization_tables[quantization_level][input[4]]; - int v5 = color_unquantization_tables[quantization_level][input[5]]; - - // extract all the fixed-placement bitfields - int modeval = ((v1 & 0x80) >> 7) | (((v2 & 0x80) >> 7) << 1) | (((v3 & 0x80) >> 7) << 2); - - int majcomp = ((v4 & 0x80) >> 7) | (((v5 & 0x80) >> 7) << 1); - - if (majcomp == 3) - { - *output0 = ushort4(v0 << 8, v2 << 8, (v4 & 0x7F) << 9, 0x7800); - *output1 = ushort4(v1 << 8, v3 << 8, (v5 & 0x7F) << 9, 0x7800); - return; - } - - int a = v0 | ((v1 & 0x40) << 2); - int b0 = v2 & 0x3f; - int b1 = v3 & 0x3f; - int c = v1 & 0x3f; - int d0 = v4 & 0x7f; - int d1 = v5 & 0x7f; - - // get hold of the number of bits in 'd0' and 'd1' - static const int dbits_tab[8] = { 7, 6, 7, 6, 5, 6, 5, 6 }; - int dbits = dbits_tab[modeval]; - - // extract six variable-placement bits - int bit0 = (v2 >> 6) & 1; - int bit1 = (v3 >> 6) & 1; - - int bit2 = (v4 >> 6) & 1; - int bit3 = (v5 >> 6) & 1; - int bit4 = (v4 >> 5) & 1; - int bit5 = (v5 >> 5) & 1; - - - // and prepend the variable-placement bits depending on mode. - int ohmod = 1 << modeval; // one-hot-mode - if (ohmod & 0xA4) - a |= bit0 << 9; - if (ohmod & 0x8) - a |= bit2 << 9; - if (ohmod & 0x50) - a |= bit4 << 9; - - if (ohmod & 0x50) - a |= bit5 << 10; - if (ohmod & 0xA0) - a |= bit1 << 10; - - if (ohmod & 0xC0) - a |= bit2 << 11; - - if (ohmod & 0x4) - c |= bit1 << 6; - if (ohmod & 0xE8) - c |= bit3 << 6; - - if (ohmod & 0x20) - c |= bit2 << 7; - - - if (ohmod & 0x5B) - b0 |= bit0 << 6; - if (ohmod & 0x5B) - b1 |= bit1 << 6; - - if (ohmod & 0x12) - b0 |= bit2 << 7; - if (ohmod & 0x12) - b1 |= bit3 << 7; - - if (ohmod & 0xAF) - d0 |= bit4 << 5; - if (ohmod & 0xAF) - d1 |= bit5 << 5; - if (ohmod & 0x5) - d0 |= bit2 << 6; - if (ohmod & 0x5) - d1 |= bit3 << 6; - - // sign-extend 'd0' and 'd1' - // note: this code assumes that signed right-shift actually sign-fills, not zero-fills. - int32_t d0x = d0; - int32_t d1x = d1; - int sx_shamt = 32 - dbits; - d0x <<= sx_shamt; - d0x >>= sx_shamt; - d1x <<= sx_shamt; - d1x >>= sx_shamt; - d0 = d0x; - d1 = d1x; - - // expand all values to 12 bits, with left-shift as needed. - int val_shamt = (modeval >> 1) ^ 3; - a <<= val_shamt; - b0 <<= val_shamt; - b1 <<= val_shamt; - c <<= val_shamt; - d0 <<= val_shamt; - d1 <<= val_shamt; - - // then compute the actual color values. - int red1 = a; - int green1 = a - b0; - int blue1 = a - b1; - int red0 = a - c; - int green0 = a - b0 - c - d0; - int blue0 = a - b1 - c - d1; - - // clamp the color components to [0,2^12 - 1] - if (red0 < 0) - red0 = 0; - else if (red0 > 0xFFF) - red0 = 0xFFF; - - if (green0 < 0) - green0 = 0; - else if (green0 > 0xFFF) - green0 = 0xFFF; - - if (blue0 < 0) - blue0 = 0; - else if (blue0 > 0xFFF) - blue0 = 0xFFF; - - if (red1 < 0) - red1 = 0; - else if (red1 > 0xFFF) - red1 = 0xFFF; - - if (green1 < 0) - green1 = 0; - else if (green1 > 0xFFF) - green1 = 0xFFF; - - if (blue1 < 0) - blue1 = 0; - else if (blue1 > 0xFFF) - blue1 = 0xFFF; - - - // switch around the color components - int temp0, temp1; - switch (majcomp) - { - case 1: // switch around red and green - temp0 = red0; - temp1 = red1; - red0 = green0; - red1 = green1; - green0 = temp0; - green1 = temp1; - break; - case 2: // switch around red and blue - temp0 = red0; - temp1 = red1; - red0 = blue0; - red1 = blue1; - blue0 = temp0; - blue1 = temp1; - break; - case 0: // no switch - break; - } - - *output0 = ushort4(red0 << 4, green0 << 4, blue0 << 4, 0x7800); - *output1 = ushort4(red1 << 4, green1 << 4, blue1 << 4, 0x7800); -} - - - - -void hdr_rgb_ldr_alpha_unpack3(const int input[8], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - hdr_rgb_unpack3(input, quantization_level, output0, output1); - - int v6 = color_unquantization_tables[quantization_level][input[6]]; - int v7 = color_unquantization_tables[quantization_level][input[7]]; - output0->w = v6; - output1->w = v7; -} - - - -void hdr_luminance_small_range_unpack(const int input[2], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int v0 = color_unquantization_tables[quantization_level][input[0]]; - int v1 = color_unquantization_tables[quantization_level][input[1]]; - - int y0, y1; - if (v0 & 0x80) - { - y0 = ((v1 & 0xE0) << 4) | ((v0 & 0x7F) << 2); - y1 = (v1 & 0x1F) << 2; - } - else - { - y0 = ((v1 & 0xF0) << 4) | ((v0 & 0x7F) << 1); - y1 = (v1 & 0xF) << 1; - } - - y1 += y0; - if (y1 > 0xFFF) - y1 = 0xFFF; - - *output0 = ushort4(y0 << 4, y0 << 4, y0 << 4, 0x7800); - *output1 = ushort4(y1 << 4, y1 << 4, y1 << 4, 0x7800); -} - - -void hdr_luminance_large_range_unpack(const int input[2], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - int v0 = color_unquantization_tables[quantization_level][input[0]]; - int v1 = color_unquantization_tables[quantization_level][input[1]]; - - int y0, y1; - if (v1 >= v0) - { - y0 = v0 << 4; - y1 = v1 << 4; - } - else - { - y0 = (v1 << 4) + 8; - y1 = (v0 << 4) - 8; - } - *output0 = ushort4(y0 << 4, y0 << 4, y0 << 4, 0x7800); - *output1 = ushort4(y1 << 4, y1 << 4, y1 << 4, 0x7800); -} - - - -void hdr_alpha_unpack(const int input[2], int quantization_level, int *a0, int *a1) -{ - - int v6 = color_unquantization_tables[quantization_level][input[0]]; - int v7 = color_unquantization_tables[quantization_level][input[1]]; - - int selector = ((v6 >> 7) & 1) | ((v7 >> 6) & 2); - v6 &= 0x7F; - v7 &= 0x7F; - if (selector == 3) - { - *a0 = v6 << 5; - *a1 = v7 << 5; - } - else - { - v6 |= (v7 << (selector + 1)) & 0x780; - v7 &= (0x3f >> selector); - v7 ^= 32 >> selector; - v7 -= 32 >> selector; - v6 <<= (4 - selector); - v7 <<= (4 - selector); - v7 += v6; - - if (v7 < 0) - v7 = 0; - else if (v7 > 0xFFF) - v7 = 0xFFF; - - *a0 = v6; - *a1 = v7; - } - - *a0 <<= 4; - *a1 <<= 4; -} - - - -void hdr_rgb_hdr_alpha_unpack3(const int input[8], int quantization_level, ushort4 * output0, ushort4 * output1) -{ - hdr_rgb_unpack3(input, quantization_level, output0, output1); - - int alpha0, alpha1; - hdr_alpha_unpack(input + 6, quantization_level, &alpha0, &alpha1); - - output0->w = alpha0; - output1->w = alpha1; -} - - - - - - -void unpack_color_endpoints(astc_decode_mode decode_mode, int format, int quantization_level, const int *input, int *rgb_hdr, int *alpha_hdr, int *nan_endpoint, ushort4 * output0, ushort4 * output1) -{ - *nan_endpoint = 0; - - switch (format) - { - case FMT_LUMINANCE: - *rgb_hdr = 0; - *alpha_hdr = 0; - luminance_unpack(input, quantization_level, output0, output1); - break; - - case FMT_LUMINANCE_DELTA: - *rgb_hdr = 0; - *alpha_hdr = 0; - luminance_delta_unpack(input, quantization_level, output0, output1); - break; - - case FMT_HDR_LUMINANCE_SMALL_RANGE: - *rgb_hdr = 1; - *alpha_hdr = -1; - hdr_luminance_small_range_unpack(input, quantization_level, output0, output1); - break; - - case FMT_HDR_LUMINANCE_LARGE_RANGE: - *rgb_hdr = 1; - *alpha_hdr = -1; - hdr_luminance_large_range_unpack(input, quantization_level, output0, output1); - break; - - case FMT_LUMINANCE_ALPHA: - *rgb_hdr = 0; - *alpha_hdr = 0; - luminance_alpha_unpack(input, quantization_level, output0, output1); - break; - - case FMT_LUMINANCE_ALPHA_DELTA: - *rgb_hdr = 0; - *alpha_hdr = 0; - luminance_alpha_delta_unpack(input, quantization_level, output0, output1); - break; - - case FMT_RGB_SCALE: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgb_scale_unpack(input, quantization_level, output0, output1); - break; - - case FMT_RGB_SCALE_ALPHA: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgb_scale_alpha_unpack(input, quantization_level, output0, output1); - break; - - case FMT_HDR_RGB_SCALE: - *rgb_hdr = 1; - *alpha_hdr = -1; - hdr_rgbo_unpack3(input, quantization_level, output0, output1); - break; - - case FMT_RGB: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgb_unpack(input, quantization_level, output0, output1); - break; - - case FMT_RGB_DELTA: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgb_delta_unpack(input, quantization_level, output0, output1); - break; - - case FMT_HDR_RGB: - *rgb_hdr = 1; - *alpha_hdr = -1; - hdr_rgb_unpack3(input, quantization_level, output0, output1); - break; - - case FMT_RGBA: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgba_unpack(input, quantization_level, output0, output1); - break; - - case FMT_RGBA_DELTA: - *rgb_hdr = 0; - *alpha_hdr = 0; - rgba_delta_unpack(input, quantization_level, output0, output1); - break; - - case FMT_HDR_RGB_LDR_ALPHA: - *rgb_hdr = 1; - *alpha_hdr = 0; - hdr_rgb_ldr_alpha_unpack3(input, quantization_level, output0, output1); - break; - - case FMT_HDR_RGBA: - *rgb_hdr = 1; - *alpha_hdr = 1; - hdr_rgb_hdr_alpha_unpack3(input, quantization_level, output0, output1); - break; - - default: - ASTC_CODEC_INTERNAL_ERROR; - } - - - - if (*alpha_hdr == -1) - { - if (alpha_force_use_of_hdr) - { - output0->w = 0x7800; - output1->w = 0x7800; - *alpha_hdr = 1; - } - else - { - output0->w = 0x00FF; - output1->w = 0x00FF; - *alpha_hdr = 0; - } - } - - - - switch (decode_mode) - { - case DECODE_LDR_SRGB: - if (*rgb_hdr == 1) - { - output0->x = 0xFF00; - output0->y = 0x0000; - output0->z = 0xFF00; - output0->w = 0xFF00; - output1->x = 0xFF00; - output1->y = 0x0000; - output1->z = 0xFF00; - output1->w = 0xFF00; - } - else - { - output0->x *= 257; - output0->y *= 257; - output0->z *= 257; - output0->w *= 257; - output1->x *= 257; - output1->y *= 257; - output1->z *= 257; - output1->w *= 257; - } - *rgb_hdr = 0; - *alpha_hdr = 0; - break; - - case DECODE_LDR: - if (*rgb_hdr == 1) - { - output0->x = 0xFFFF; - output0->y = 0xFFFF; - output0->z = 0xFFFF; - output0->w = 0xFFFF; - output1->x = 0xFFFF; - output1->y = 0xFFFF; - output1->z = 0xFFFF; - output1->w = 0xFFFF; - *nan_endpoint = 1; - } - else - { - output0->x *= 257; - output0->y *= 257; - output0->z *= 257; - output0->w *= 257; - output1->x *= 257; - output1->y *= 257; - output1->z *= 257; - output1->w *= 257; - } - *rgb_hdr = 0; - *alpha_hdr = 0; - break; - - case DECODE_HDR: - - if (*rgb_hdr == 0) - { - output0->x *= 257; - output0->y *= 257; - output0->z *= 257; - output1->x *= 257; - output1->y *= 257; - output1->z *= 257; - } - if (*alpha_hdr == 0) - { - output0->w *= 257; - output1->w *= 257; - } - break; - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_compress_symbolic.cpp b/3rdparty/bimg/3rdparty/astc/astc_compress_symbolic.cpp deleted file mode 100644 index 152ca05472c..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_compress_symbolic.cpp +++ /dev/null @@ -1,1792 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Compress a block of colors, expressed as a symbolic block, for ASTC. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -#include "softfloat.h" -#include <math.h> -#include <string.h> -#include <stdio.h> - -#ifdef DEBUG_CAPTURE_NAN - #ifndef _GNU_SOURCE - #define _GNU_SOURCE - #endif - - #include <fenv.h> -#endif - -#include <stdio.h> - -int realign_weights(astc_decode_mode decode_mode, - int xdim, int ydim, int zdim, const imageblock * blk, const error_weight_block * ewb, symbolic_compressed_block * scb, uint8_t * weight_set8, uint8_t * plane2_weight_set8) -{ - int i, j; - - // get the appropriate partition descriptor. - int partition_count = scb->partition_count; - const partition_info *pt = get_partition_table(xdim, ydim, zdim, partition_count); - pt += scb->partition_index; - - // get the appropriate block descriptor - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - - const decimation_table *it = ixtab2[bsd->block_modes[scb->block_mode].decimation_mode]; - - int is_dual_plane = bsd->block_modes[scb->block_mode].is_dual_plane; - - // get quantization-parameters - int weight_quantization_level = bsd->block_modes[scb->block_mode].quantization_mode; - - - // decode the color endpoints - ushort4 color_endpoint0[4]; - ushort4 color_endpoint1[4]; - int rgb_hdr[4]; - int alpha_hdr[4]; - int nan_endpoint[4]; - - - for (i = 0; i < partition_count; i++) - unpack_color_endpoints(decode_mode, - scb->color_formats[i], scb->color_quantization_level, scb->color_values[i], &rgb_hdr[i], &alpha_hdr[i], &nan_endpoint[i], &(color_endpoint0[i]), &(color_endpoint1[i])); - - - float uq_plane1_weights[MAX_WEIGHTS_PER_BLOCK]; - float uq_plane2_weights[MAX_WEIGHTS_PER_BLOCK]; - int weight_count = it->num_weights; - - // read and unquantize the weights. - - const quantization_and_transfer_table *qat = &(quant_and_xfer_tables[weight_quantization_level]); - - for (i = 0; i < weight_count; i++) - { - uq_plane1_weights[i] = qat->unquantized_value_flt[weight_set8[i]]; - } - if (is_dual_plane) - { - for (i = 0; i < weight_count; i++) - uq_plane2_weights[i] = qat->unquantized_value_flt[plane2_weight_set8[i]]; - } - - - int plane2_color_component = is_dual_plane ? scb->plane2_color_component : -1; - - // for each weight, unquantize the weight, use it to compute a color and a color error. - // then, increment the weight until the color error stops decreasing - // then, decrement the weight until the color error stops increasing - - #define COMPUTE_ERROR( errorvar ) \ - errorvar = 0.0f; \ - for(j=0;j<texels_to_evaluate;j++) \ - { \ - int texel = it->weight_texel[i][j]; \ - int partition = pt->partition_of_texel[texel]; \ - float plane1_weight = compute_value_of_texel_flt( texel, it, uq_plane1_weights ); \ - float plane2_weight = 0.0f; \ - if( is_dual_plane ) \ - plane2_weight = compute_value_of_texel_flt( texel, it, uq_plane2_weights ); \ - int int_plane1_weight = static_cast<int>(floor( plane1_weight*64.0f + 0.5f ) ); \ - int int_plane2_weight = static_cast<int>(floor( plane2_weight*64.0f + 0.5f ) ); \ - ushort4 lrp_color = lerp_color_int( \ - decode_mode, \ - color_endpoint0[partition], \ - color_endpoint1[partition], \ - int_plane1_weight, \ - int_plane2_weight, \ - plane2_color_component ); \ - float4 color = float4( lrp_color.x, lrp_color.y, lrp_color.z, lrp_color.w ); \ - float4 origcolor = float4( \ - blk->work_data[4*texel], \ - blk->work_data[4*texel+1], \ - blk->work_data[4*texel+2], \ - blk->work_data[4*texel+3] ); \ - float4 error_weight = ewb->error_weights[texel]; \ - float4 colordiff = color - origcolor; \ - errorvar += dot( colordiff*colordiff, error_weight ); \ - } - - - int adjustments = 0; - - for (i = 0; i < weight_count; i++) - { - int current_wt = weight_set8[i]; - int texels_to_evaluate = it->weight_num_texels[i]; - - float current_error; - - COMPUTE_ERROR(current_error); - - // increment until error starts increasing. - while (1) - { - int next_wt = qat->next_quantized_value[current_wt]; - if (next_wt == current_wt) - break; - uq_plane1_weights[i] = qat->unquantized_value_flt[next_wt]; - float next_error; - COMPUTE_ERROR(next_error); - if (next_error < current_error) - { - // succeeded, increment the weight - current_wt = next_wt; - current_error = next_error; - adjustments++; - } - else - { - // failed, back out the attempted increment - uq_plane1_weights[i] = qat->unquantized_value_flt[current_wt]; - break; - } - } - // decrement until error starts increasing - while (1) - { - int prev_wt = qat->prev_quantized_value[current_wt]; - if (prev_wt == current_wt) - break; - uq_plane1_weights[i] = qat->unquantized_value_flt[prev_wt]; - float prev_error; - COMPUTE_ERROR(prev_error); - if (prev_error < current_error) - { - // succeeded, decrement the weight - current_wt = prev_wt; - current_error = prev_error; - adjustments++; - } - else - { - // failed, back out the attempted decrement - uq_plane1_weights[i] = qat->unquantized_value_flt[current_wt]; - break; - } - } - - weight_set8[i] = current_wt; - } - - if (!is_dual_plane) - return adjustments; - - // processing of the second plane of weights - for (i = 0; i < weight_count; i++) - { - int current_wt = plane2_weight_set8[i]; - int texels_to_evaluate = it->weight_num_texels[i]; - - float current_error; - - COMPUTE_ERROR(current_error); - - // increment until error starts increasing. - while (1) - { - int next_wt = qat->next_quantized_value[current_wt]; - if (next_wt == current_wt) - break; - uq_plane2_weights[i] = qat->unquantized_value_flt[next_wt]; - float next_error; - COMPUTE_ERROR(next_error); - if (next_error < current_error) - { - // succeeded, increment the weight - current_wt = next_wt; - current_error = next_error; - adjustments++; - } - else - { - // failed, back out the attempted increment - uq_plane2_weights[i] = qat->unquantized_value_flt[current_wt]; - break; - } - } - // decrement until error starts increasing - while (1) - { - int prev_wt = qat->prev_quantized_value[current_wt]; - if (prev_wt == current_wt) - break; - uq_plane2_weights[i] = qat->unquantized_value_flt[prev_wt]; - float prev_error; - COMPUTE_ERROR(prev_error); - if (prev_error < current_error) - { - // succeeded, decrement the weight - current_wt = prev_wt; - current_error = prev_error; - adjustments++; - } - else - { - // failed, back out the attempted decrement - uq_plane2_weights[i] = qat->unquantized_value_flt[current_wt]; - break; - } - } - - plane2_weight_set8[i] = current_wt; - } - - return adjustments; -} - -/* - function for compressing a block symbolically, given that we have already decided on a partition -*/ - - - -static void compress_symbolic_block_fixed_partition_1_plane(astc_decode_mode decode_mode, - float mode_cutoff, - int max_refinement_iters, - int xdim, int ydim, int zdim, - int partition_count, int partition_index, - const imageblock * blk, const error_weight_block * ewb, symbolic_compressed_block * scb, - compress_fixed_partition_buffers * tmpbuf) -{ - int i, j, k; - - - static const int free_bits_for_partition_count[5] = { 0, 115 - 4, 111 - 4 - PARTITION_BITS, 108 - 4 - PARTITION_BITS, 105 - 4 - PARTITION_BITS }; - - const partition_info *pi = get_partition_table(xdim, ydim, zdim, partition_count); - pi += partition_index; - - // first, compute ideal weights and endpoint colors, under thre assumption that - // there is no quantization or decimation going on. - endpoints_and_weights *ei = tmpbuf->ei1; - endpoints_and_weights *eix = tmpbuf->eix1; - compute_endpoints_and_ideal_weights_1_plane(xdim, ydim, zdim, pi, blk, ewb, ei); - - // next, compute ideal weights and endpoint colors for every decimation. - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - // int block_mode_count = bsd->single_plane_block_mode_count; - - - float *decimated_quantized_weights = tmpbuf->decimated_quantized_weights; - float *decimated_weights = tmpbuf->decimated_weights; - float *flt_quantized_decimated_quantized_weights = tmpbuf->flt_quantized_decimated_quantized_weights; - uint8_t *u8_quantized_decimated_quantized_weights = tmpbuf->u8_quantized_decimated_quantized_weights; - - // for each decimation mode, compute an ideal set of weights - // (that is, weights computed with the assumption that they are not quantized) - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - if (bsd->permit_encode[i] == 0 || bsd->decimation_mode_maxprec_1plane[i] < 0 || bsd->decimation_mode_percentile[i] > mode_cutoff) - continue; - eix[i] = *ei; - compute_ideal_weights_for_decimation_table(&(eix[i]), ixtab2[i], decimated_quantized_weights + i * MAX_WEIGHTS_PER_BLOCK, decimated_weights + i * MAX_WEIGHTS_PER_BLOCK); - - } - - // compute maximum colors for the endpoints and ideal weights. - // for each endpoint-and-ideal-weight pair, compute the smallest weight value - // that will result in a color value greater than 1. - - - float4 min_ep = float4(10, 10, 10, 10); - for (i = 0; i < partition_count; i++) - { - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - float4 ep = (float4(1, 1, 1, 1) - ei->ep.endpt0[i]) / (ei->ep.endpt1[i] - ei->ep.endpt0[i]); - if (ep.x > 0.5f && ep.x < min_ep.x) - min_ep.x = ep.x; - if (ep.y > 0.5f && ep.y < min_ep.y) - min_ep.y = ep.y; - if (ep.z > 0.5f && ep.z < min_ep.z) - min_ep.z = ep.z; - if (ep.w > 0.5f && ep.w < min_ep.w) - min_ep.w = ep.w; - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - } - - float min_wt_cutoff = MIN(MIN(min_ep.x, min_ep.y), MIN(min_ep.z, min_ep.w)); - - // for each mode, use the angular method to compute a shift. - float weight_low_value[MAX_WEIGHT_MODES]; - float weight_high_value[MAX_WEIGHT_MODES]; - - compute_angular_endpoints_1plane(mode_cutoff, bsd, decimated_quantized_weights, decimated_weights, weight_low_value, weight_high_value); - - // for each mode (which specifies a decimation and a quantization): - // * compute number of bits needed for the quantized weights. - // * generate an optimized set of quantized weights. - // * compute quantization errors for the mode. - - int qwt_bitcounts[MAX_WEIGHT_MODES]; - float qwt_errors[MAX_WEIGHT_MODES]; - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (bsd->block_modes[i].permit_encode == 0 || bsd->block_modes[i].is_dual_plane != 0 || bsd->block_modes[i].percentile > mode_cutoff) - { - qwt_errors[i] = 1e38f; - continue; - } - if (weight_high_value[i] > 1.02f * min_wt_cutoff) - weight_high_value[i] = 1.0f; - - int decimation_mode = bsd->block_modes[i].decimation_mode; - if (bsd->decimation_mode_percentile[decimation_mode] > mode_cutoff) - ASTC_CODEC_INTERNAL_ERROR; - - - // compute weight bitcount for the mode - int bits_used_by_weights = compute_ise_bitcount(ixtab2[decimation_mode]->num_weights, - (quantization_method) bsd->block_modes[i].quantization_mode); - int bitcount = free_bits_for_partition_count[partition_count] - bits_used_by_weights; - if (bitcount <= 0 || bits_used_by_weights < 24 || bits_used_by_weights > 96) - { - qwt_errors[i] = 1e38f; - continue; - } - qwt_bitcounts[i] = bitcount; - - - // then, generate the optimized set of weights for the weight mode. - compute_ideal_quantized_weights_for_decimation_table(&(eix[decimation_mode]), - ixtab2[decimation_mode], - weight_low_value[i], weight_high_value[i], - decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * decimation_mode, - flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * i, - u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * i, - bsd->block_modes[i].quantization_mode); - - // then, compute weight-errors for the weight mode. - qwt_errors[i] = compute_error_of_weight_set(&(eix[decimation_mode]), ixtab2[decimation_mode], flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * i); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Block mode %d -> weight error = %f\n", i, qwt_errors[i]); - #endif - } - - // for each weighting mode, determine the optimal combination of color endpoint encodings - // and weight encodings; return results for the 4 best-looking modes. - - int partition_format_specifiers[4][4]; - int quantized_weight[4]; - int color_quantization_level[4]; - int color_quantization_level_mod[4]; - determine_optimal_set_of_endpoint_formats_to_use(xdim, ydim, zdim, pi, blk, ewb, &(ei->ep), -1, // used to flag that we are in single-weight mode - qwt_bitcounts, qwt_errors, partition_format_specifiers, quantized_weight, color_quantization_level, color_quantization_level_mod); - - - // then iterate over the 4 believed-to-be-best modes to find out which one is - // actually best. - for (i = 0; i < 4; i++) - { - uint8_t *u8_weight_src; - int weights_to_copy; - - if (quantized_weight[i] < 0) - { - scb->error_block = 1; - scb++; - continue; - } - - int decimation_mode = bsd->block_modes[quantized_weight[i]].decimation_mode; - int weight_quantization_mode = bsd->block_modes[quantized_weight[i]].quantization_mode; - const decimation_table *it = ixtab2[decimation_mode]; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Selected mode = %d\n", quantized_weight[i]); - printf("Selected decimation mode = %d\n", decimation_mode); - printf("Selected weight-quantization mode = %d\n", weight_quantization_mode); - } - #endif - - u8_weight_src = u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * quantized_weight[i]; - - weights_to_copy = it->num_weights; - - // recompute the ideal color endpoints before storing them. - float4 rgbs_colors[4]; - float4 rgbo_colors[4]; - float2 lum_intervals[4]; - - int l; - for (l = 0; l < max_refinement_iters; l++) - { - recompute_ideal_colors(xdim, ydim, zdim, weight_quantization_mode, &(eix[decimation_mode].ep), rgbs_colors, rgbo_colors, lum_intervals, u8_weight_src, NULL, -1, pi, it, blk, ewb); - - // quantize the chosen color - - // store the colors for the block - for (j = 0; j < partition_count; j++) - { - scb->color_formats[j] = pack_color_endpoints(decode_mode, - eix[decimation_mode].ep.endpt0[j], - eix[decimation_mode].ep.endpt1[j], - rgbs_colors[j], rgbo_colors[j], lum_intervals[j], partition_format_specifiers[i][j], scb->color_values[j], color_quantization_level[i]); - } - - - // if all the color endpoint modes are the same, we get a few more - // bits to store colors; let's see if we can take advantage of this: - // requantize all the colors and see if the endpoint modes remain the same; - // if they do, then exploit it. - scb->color_formats_matched = 0; - - if ((partition_count >= 2 && scb->color_formats[0] == scb->color_formats[1] - && color_quantization_level != color_quantization_level_mod) - && (partition_count == 2 || (scb->color_formats[0] == scb->color_formats[2] && (partition_count == 3 || (scb->color_formats[0] == scb->color_formats[3]))))) - { - int colorvals[4][12]; - int color_formats_mod[4]; - for (j = 0; j < partition_count; j++) - { - color_formats_mod[j] = pack_color_endpoints(decode_mode, - eix[decimation_mode].ep.endpt0[j], - eix[decimation_mode].ep.endpt1[j], - rgbs_colors[j], rgbo_colors[j], lum_intervals[j], partition_format_specifiers[i][j], colorvals[j], color_quantization_level_mod[i]); - } - if (color_formats_mod[0] == color_formats_mod[1] - && (partition_count == 2 || (color_formats_mod[0] == color_formats_mod[2] && (partition_count == 3 || (color_formats_mod[0] == color_formats_mod[3]))))) - { - scb->color_formats_matched = 1; - for (j = 0; j < 4; j++) - for (k = 0; k < 12; k++) - scb->color_values[j][k] = colorvals[j][k]; - for (j = 0; j < 4; j++) - scb->color_formats[j] = color_formats_mod[j]; - } - } - - - // store header fields - scb->partition_count = partition_count; - scb->partition_index = partition_index; - scb->color_quantization_level = scb->color_formats_matched ? color_quantization_level_mod[i] : color_quantization_level[i]; - scb->block_mode = quantized_weight[i]; - scb->error_block = 0; - - if (scb->color_quantization_level < 4) - { - scb->error_block = 1; // should never happen, but cannot prove it impossible. - } - - // perform a final pass over the weights to try to improve them. - int adjustments = realign_weights(decode_mode, - xdim, ydim, zdim, - blk, ewb, scb, - u8_weight_src, - NULL); - - if (adjustments == 0) - break; - } - - for (j = 0; j < weights_to_copy; j++) - scb->plane1_weights[j] = u8_weight_src[j]; - - scb++; - } - -} - - - - - - -static void compress_symbolic_block_fixed_partition_2_planes(astc_decode_mode decode_mode, - float mode_cutoff, - int max_refinement_iters, - int xdim, int ydim, int zdim, - int partition_count, int partition_index, - int separate_component, const imageblock * blk, const error_weight_block * ewb, - symbolic_compressed_block * scb, - compress_fixed_partition_buffers * tmpbuf) -{ - int i, j, k; - - static const int free_bits_for_partition_count[5] = - { 0, 113 - 4, 109 - 4 - PARTITION_BITS, 106 - 4 - PARTITION_BITS, 103 - 4 - PARTITION_BITS }; - - const partition_info *pi = get_partition_table(xdim, ydim, zdim, partition_count); - pi += partition_index; - - // first, compute ideal weights and endpoint colors - endpoints_and_weights *ei1 = tmpbuf->ei1; - endpoints_and_weights *ei2 = tmpbuf->ei2; - endpoints_and_weights *eix1 = tmpbuf->eix1; - endpoints_and_weights *eix2 = tmpbuf->eix2; - compute_endpoints_and_ideal_weights_2_planes(xdim, ydim, zdim, pi, blk, ewb, separate_component, ei1, ei2); - - // next, compute ideal weights and endpoint colors for every decimation. - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - - - float *decimated_quantized_weights = tmpbuf->decimated_quantized_weights; - float *decimated_weights = tmpbuf->decimated_weights; - float *flt_quantized_decimated_quantized_weights = tmpbuf->flt_quantized_decimated_quantized_weights; - uint8_t *u8_quantized_decimated_quantized_weights = tmpbuf->u8_quantized_decimated_quantized_weights; - - // for each decimation mode, compute an ideal set of weights - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - if (bsd->permit_encode[i] == 0 || bsd->decimation_mode_maxprec_2planes[i] < 0 || bsd->decimation_mode_percentile[i] > mode_cutoff) - continue; - - eix1[i] = *ei1; - eix2[i] = *ei2; - compute_ideal_weights_for_decimation_table(&(eix1[i]), ixtab2[i], decimated_quantized_weights + (2 * i) * MAX_WEIGHTS_PER_BLOCK, decimated_weights + (2 * i) * MAX_WEIGHTS_PER_BLOCK); - compute_ideal_weights_for_decimation_table(&(eix2[i]), ixtab2[i], decimated_quantized_weights + (2 * i + 1) * MAX_WEIGHTS_PER_BLOCK, decimated_weights + (2 * i + 1) * MAX_WEIGHTS_PER_BLOCK); - } - - // compute maximum colors for the endpoints and ideal weights. - // for each endpoint-and-ideal-weight pair, compute the smallest weight value - // that will result in a color value greater than 1. - - float4 min_ep1 = float4(10, 10, 10, 10); - float4 min_ep2 = float4(10, 10, 10, 10); - for (i = 0; i < partition_count; i++) - { - - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - float4 ep1 = (float4(1, 1, 1, 1) - ei1->ep.endpt0[i]) / (ei1->ep.endpt1[i] - ei1->ep.endpt0[i]); - if (ep1.x > 0.5f && ep1.x < min_ep1.x) - min_ep1.x = ep1.x; - if (ep1.y > 0.5f && ep1.y < min_ep1.y) - min_ep1.y = ep1.y; - if (ep1.z > 0.5f && ep1.z < min_ep1.z) - min_ep1.z = ep1.z; - if (ep1.w > 0.5f && ep1.w < min_ep1.w) - min_ep1.w = ep1.w; - float4 ep2 = (float4(1, 1, 1, 1) - ei2->ep.endpt0[i]) / (ei2->ep.endpt1[i] - ei2->ep.endpt0[i]); - if (ep2.x > 0.5f && ep2.x < min_ep2.x) - min_ep2.x = ep2.x; - if (ep2.y > 0.5f && ep2.y < min_ep2.y) - min_ep2.y = ep2.y; - if (ep2.z > 0.5f && ep2.z < min_ep2.z) - min_ep2.z = ep2.z; - if (ep2.w > 0.5f && ep2.w < min_ep2.w) - min_ep2.w = ep2.w; - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - } - - float min_wt_cutoff1, min_wt_cutoff2; - switch (separate_component) - { - case 0: - min_wt_cutoff2 = min_ep2.x; - min_ep1.x = 1e30f; - break; - case 1: - min_wt_cutoff2 = min_ep2.y; - min_ep1.y = 1e30f; - break; - case 2: - min_wt_cutoff2 = min_ep2.z; - min_ep1.z = 1e30f; - break; - case 3: - min_wt_cutoff2 = min_ep2.w; - min_ep1.w = 1e30f; - break; - default: - min_wt_cutoff2 = 1e30f; - } - - min_wt_cutoff1 = MIN(MIN(min_ep1.x, min_ep1.y), MIN(min_ep1.z, min_ep1.w)); - - float weight_low_value1[MAX_WEIGHT_MODES]; - float weight_high_value1[MAX_WEIGHT_MODES]; - float weight_low_value2[MAX_WEIGHT_MODES]; - float weight_high_value2[MAX_WEIGHT_MODES]; - - compute_angular_endpoints_2planes(mode_cutoff, bsd, decimated_quantized_weights, decimated_weights, weight_low_value1, weight_high_value1, weight_low_value2, weight_high_value2); - - // for each mode (which specifies a decimation and a quantization): - // * generate an optimized set of quantized weights. - // * compute quantization errors for each mode - // * compute number of bits needed for the quantized weights. - - int qwt_bitcounts[MAX_WEIGHT_MODES]; - float qwt_errors[MAX_WEIGHT_MODES]; - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (bsd->block_modes[i].permit_encode == 0 || bsd->block_modes[i].is_dual_plane != 1 || bsd->block_modes[i].percentile > mode_cutoff) - { - qwt_errors[i] = 1e38f; - continue; - } - int decimation_mode = bsd->block_modes[i].decimation_mode; - - if (weight_high_value1[i] > 1.02f * min_wt_cutoff1) - weight_high_value1[i] = 1.0f; - if (weight_high_value2[i] > 1.02f * min_wt_cutoff2) - weight_high_value2[i] = 1.0f; - - // compute weight bitcount for the mode - int bits_used_by_weights = compute_ise_bitcount(2 * ixtab2[decimation_mode]->num_weights, - (quantization_method) bsd->block_modes[i].quantization_mode); - int bitcount = free_bits_for_partition_count[partition_count] - bits_used_by_weights; - if (bitcount <= 0 || bits_used_by_weights < 24 || bits_used_by_weights > 96) - { - qwt_errors[i] = 1e38f; - continue; - } - qwt_bitcounts[i] = bitcount; - - - // then, generate the optimized set of weights for the mode. - compute_ideal_quantized_weights_for_decimation_table(&(eix1[decimation_mode]), - ixtab2[decimation_mode], - weight_low_value1[i], - weight_high_value1[i], - decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * decimation_mode), - flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i), - u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i), bsd->block_modes[i].quantization_mode); - compute_ideal_quantized_weights_for_decimation_table(&(eix2[decimation_mode]), - ixtab2[decimation_mode], - weight_low_value2[i], - weight_high_value2[i], - decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * decimation_mode + 1), - flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i + 1), - u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i + 1), bsd->block_modes[i].quantization_mode); - - - // then, compute quantization errors for the block mode. - qwt_errors[i] = - compute_error_of_weight_set(&(eix1[decimation_mode]), - ixtab2[decimation_mode], - flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i)) - + compute_error_of_weight_set(&(eix2[decimation_mode]), ixtab2[decimation_mode], flt_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * i + 1)); - } - - - // decide the optimal combination of color endpoint encodings and weight encoodings. - int partition_format_specifiers[4][4]; - int quantized_weight[4]; - int color_quantization_level[4]; - int color_quantization_level_mod[4]; - - endpoints epm; - merge_endpoints(&(ei1->ep), &(ei2->ep), separate_component, &epm); - - determine_optimal_set_of_endpoint_formats_to_use(xdim, ydim, zdim, - pi, - blk, - ewb, - &epm, separate_component, qwt_bitcounts, qwt_errors, partition_format_specifiers, quantized_weight, color_quantization_level, color_quantization_level_mod); - - for (i = 0; i < 4; i++) - { - if (quantized_weight[i] < 0) - { - scb->error_block = 1; - scb++; - continue; - } - - uint8_t *u8_weight1_src; - uint8_t *u8_weight2_src; - int weights_to_copy; - - int decimation_mode = bsd->block_modes[quantized_weight[i]].decimation_mode; - int weight_quantization_mode = bsd->block_modes[quantized_weight[i]].quantization_mode; - const decimation_table *it = ixtab2[decimation_mode]; - - u8_weight1_src = u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * quantized_weight[i]); - u8_weight2_src = u8_quantized_decimated_quantized_weights + MAX_WEIGHTS_PER_BLOCK * (2 * quantized_weight[i] + 1); - - - weights_to_copy = it->num_weights; - - // recompute the ideal color endpoints before storing them. - merge_endpoints(&(eix1[decimation_mode].ep), &(eix2[decimation_mode].ep), separate_component, &epm); - - float4 rgbs_colors[4]; - float4 rgbo_colors[4]; - float2 lum_intervals[4]; - - int l; - for (l = 0; l < max_refinement_iters; l++) - { - recompute_ideal_colors(xdim, ydim, zdim, weight_quantization_mode, &epm, rgbs_colors, rgbo_colors, lum_intervals, u8_weight1_src, u8_weight2_src, separate_component, pi, it, blk, ewb); - - // store the colors for the block - for (j = 0; j < partition_count; j++) - { - scb->color_formats[j] = pack_color_endpoints(decode_mode, - epm.endpt0[j], - epm.endpt1[j], - rgbs_colors[j], rgbo_colors[j], lum_intervals[j], partition_format_specifiers[i][j], scb->color_values[j], color_quantization_level[i]); - } - scb->color_formats_matched = 0; - - if ((partition_count >= 2 && scb->color_formats[0] == scb->color_formats[1] - && color_quantization_level != color_quantization_level_mod) - && (partition_count == 2 || (scb->color_formats[0] == scb->color_formats[2] && (partition_count == 3 || (scb->color_formats[0] == scb->color_formats[3]))))) - { - int colorvals[4][12]; - int color_formats_mod[4]; - for (j = 0; j < partition_count; j++) - { - color_formats_mod[j] = pack_color_endpoints(decode_mode, - epm.endpt0[j], - epm.endpt1[j], - rgbs_colors[j], rgbo_colors[j], lum_intervals[j], partition_format_specifiers[i][j], colorvals[j], color_quantization_level_mod[i]); - } - if (color_formats_mod[0] == color_formats_mod[1] - && (partition_count == 2 || (color_formats_mod[0] == color_formats_mod[2] && (partition_count == 3 || (color_formats_mod[0] == color_formats_mod[3]))))) - { - scb->color_formats_matched = 1; - for (j = 0; j < 4; j++) - for (k = 0; k < 12; k++) - scb->color_values[j][k] = colorvals[j][k]; - for (j = 0; j < 4; j++) - scb->color_formats[j] = color_formats_mod[j]; - } - } - - - // store header fields - scb->partition_count = partition_count; - scb->partition_index = partition_index; - scb->color_quantization_level = scb->color_formats_matched ? color_quantization_level_mod[i] : color_quantization_level[i]; - scb->block_mode = quantized_weight[i]; - scb->plane2_color_component = separate_component; - scb->error_block = 0; - - if (scb->color_quantization_level < 4) - { - scb->error_block = 1; // should never happen, but cannot prove it impossible - } - - int adjustments = realign_weights(decode_mode, - xdim, ydim, zdim, - blk, ewb, scb, - u8_weight1_src, - u8_weight2_src); - - if (adjustments == 0) - break; - } - - for (j = 0; j < weights_to_copy; j++) - { - scb->plane1_weights[j] = u8_weight1_src[j]; - scb->plane2_weights[j] = u8_weight2_src[j]; - } - - scb++; - } - -} - - - - - -void expand_block_artifact_suppression(int xdim, int ydim, int zdim, error_weighting_params * ewp) -{ - int x, y, z; - float centerpos_x = (xdim - 1) * 0.5f; - float centerpos_y = (ydim - 1) * 0.5f; - float centerpos_z = (zdim - 1) * 0.5f; - float *bef = ewp->block_artifact_suppression_expanded; - - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - float xdif = (x - centerpos_x) / xdim; - float ydif = (y - centerpos_y) / ydim; - float zdif = (z - centerpos_z) / zdim; - - float wdif = 0.36f; - float dist = sqrt(xdif * xdif + ydif * ydif + zdif * zdif + wdif * wdif); - *bef = pow(dist, ewp->block_artifact_suppression); - bef++; - } -} - - - -// Function to set error weights for each color component for each texel in a block. -// Returns the sum of all the error values set. - -float prepare_error_weight_block(const astc_codec_image * input_image, - int xdim, int ydim, int zdim, const error_weighting_params * ewp, const imageblock * blk, error_weight_block * ewb, error_weight_block_orig * ewbo) -{ - - int x, y, z; - int idx = 0; - - int any_mean_stdev_weight = - ewp->rgb_base_weight != 1.0 || ewp->alpha_base_weight != 1.0 || ewp->rgb_mean_weight != 0.0 || ewp->rgb_stdev_weight != 0.0 || ewp->alpha_mean_weight != 0.0 || ewp->alpha_stdev_weight != 0.0; - - float4 color_weights = float4(ewp->rgba_weights[0], - ewp->rgba_weights[1], - ewp->rgba_weights[2], - ewp->rgba_weights[3]); - - ewb->contains_zeroweight_texels = 0; - - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - int xpos = x + blk->xpos; - int ypos = y + blk->ypos; - int zpos = z + blk->zpos; - - if (xpos >= input_image->xsize || ypos >= input_image->ysize || zpos >= input_image->zsize) - { - float4 weights = float4(1e-11f, 1e-11f, 1e-11f, 1e-11f); - ewb->error_weights[idx] = weights; - ewb->contains_zeroweight_texels = 1; - } - else - { - float4 error_weight = float4(ewp->rgb_base_weight, - ewp->rgb_base_weight, - ewp->rgb_base_weight, - ewp->alpha_base_weight); - - if (any_mean_stdev_weight) - { - float4 avg = input_averages[zpos][ypos][xpos]; - if (avg.x < 6e-5f) - avg.x = 6e-5f; - if (avg.y < 6e-5f) - avg.y = 6e-5f; - if (avg.z < 6e-5f) - avg.z = 6e-5f; - if (avg.w < 6e-5f) - avg.w = 6e-5f; - /* - printf("avg: %f %f %f %f\n", avg.x, avg.y, avg.z, avg.w ); */ - avg = avg * avg; - - float4 variance = input_variances[zpos][ypos][xpos]; - variance = variance * variance; - - float favg = (avg.x + avg.y + avg.z) * (1.0f / 3.0f); - float fvar = (variance.x + variance.y + variance.z) * (1.0f / 3.0f); - - float mixing = ewp->rgb_mean_and_stdev_mixing; - avg.xyz = float3(favg, favg, favg) * mixing + avg.xyz * (1.0f - mixing); - variance.xyz = float3(fvar, fvar, fvar) * mixing + variance.xyz * (1.0f - mixing); - - float4 stdev = float4(sqrt(MAX(variance.x, 0.0f)), - sqrt(MAX(variance.y, 0.0f)), - sqrt(MAX(variance.z, 0.0f)), - sqrt(MAX(variance.w, 0.0f))); - - avg.xyz = avg.xyz * ewp->rgb_mean_weight; - avg.w = avg.w * ewp->alpha_mean_weight; - stdev.xyz = stdev.xyz * ewp->rgb_stdev_weight; - stdev.w = stdev.w * ewp->alpha_stdev_weight; - error_weight = error_weight + avg + stdev; - - error_weight = float4(1.0f, 1.0f, 1.0f, 1.0f) / error_weight; - } - - if (ewp->ra_normal_angular_scale) - { - float x = (blk->orig_data[4 * idx] - 0.5f) * 2.0f; - float y = (blk->orig_data[4 * idx + 3] - 0.5f) * 2.0f; - float denom = 1.0f - x * x - y * y; - if (denom < 0.1f) - denom = 0.1f; - denom = 1.0f / denom; - error_weight.x *= 1.0f + x * x * denom; - error_weight.w *= 1.0f + y * y * denom; - } - - if (ewp->enable_rgb_scale_with_alpha) - { - float alpha_scale; - if (ewp->alpha_radius != 0) - alpha_scale = input_alpha_averages[zpos][ypos][xpos]; - else - alpha_scale = blk->orig_data[4 * idx + 3]; - if (alpha_scale < 0.0001f) - alpha_scale = 0.0001f; - alpha_scale *= alpha_scale; - error_weight.xyz = error_weight.xyz * alpha_scale; - } - error_weight = error_weight * color_weights; - error_weight = error_weight * ewp->block_artifact_suppression_expanded[idx]; - - // if we perform a conversion from linear to sRGB, then we multiply - // the weight with the derivative of the linear->sRGB transform function. - if (perform_srgb_transform) - { - float r = blk->orig_data[4 * idx]; - float g = blk->orig_data[4 * idx + 1]; - float b = blk->orig_data[4 * idx + 2]; - if (r < 0.0031308f) - r = 12.92f; - else - r = 0.4396f * pow(r, -0.58333f); - if (g < 0.0031308f) - g = 12.92f; - else - g = 0.4396f * pow(g, -0.58333f); - if (b < 0.0031308f) - b = 12.92f; - else - b = 0.4396f * pow(b, -0.58333f); - error_weight.x *= r; - error_weight.y *= g; - error_weight.z *= b; - } - - /* - printf("%f %f %f %f\n", error_weight.x, error_weight.y, error_weight.z, error_weight.w ); - */ - - // when we loaded the block to begin with, we applied a transfer function - // and computed the derivative of the transfer function. However, the - // error-weight computation so far is based on the original color values, - // not the transfer-function values. As such, we must multiply the - // error weights by the derivative of the inverse of the transfer function, - // which is equivalent to dividing by the derivative of the transfer - // function. - - ewbo->error_weights[idx] = error_weight; - - error_weight.x /= (blk->deriv_data[4 * idx] * blk->deriv_data[4 * idx] * 1e-10f); - error_weight.y /= (blk->deriv_data[4 * idx + 1] * blk->deriv_data[4 * idx + 1] * 1e-10f); - error_weight.z /= (blk->deriv_data[4 * idx + 2] * blk->deriv_data[4 * idx + 2] * 1e-10f); - error_weight.w /= (blk->deriv_data[4 * idx + 3] * blk->deriv_data[4 * idx + 3] * 1e-10f); - - /* - printf("--> %f %f %f %f\n", error_weight.x, error_weight.y, error_weight.z, error_weight.w ); - */ - - ewb->error_weights[idx] = error_weight; - if (dot(error_weight, float4(1, 1, 1, 1)) < 1e-10f) - ewb->contains_zeroweight_texels = 1; - } - idx++; - } - - int i; - - float4 error_weight_sum = float4(0, 0, 0, 0); - int texels_per_block = xdim * ydim * zdim; - - for (i = 0; i < texels_per_block; i++) - { - error_weight_sum = error_weight_sum + ewb->error_weights[i]; - - ewb->texel_weight_r[i] = ewb->error_weights[i].x; - ewb->texel_weight_g[i] = ewb->error_weights[i].y; - ewb->texel_weight_b[i] = ewb->error_weights[i].z; - ewb->texel_weight_a[i] = ewb->error_weights[i].w; - - ewb->texel_weight_rg[i] = (ewb->error_weights[i].x + ewb->error_weights[i].y) * 0.5f; - ewb->texel_weight_rb[i] = (ewb->error_weights[i].x + ewb->error_weights[i].z) * 0.5f; - ewb->texel_weight_gb[i] = (ewb->error_weights[i].y + ewb->error_weights[i].z) * 0.5f; - ewb->texel_weight_ra[i] = (ewb->error_weights[i].x + ewb->error_weights[i].w) * 0.5f; - - ewb->texel_weight_gba[i] = (ewb->error_weights[i].y + ewb->error_weights[i].z + ewb->error_weights[i].w) * 0.333333f; - ewb->texel_weight_rba[i] = (ewb->error_weights[i].x + ewb->error_weights[i].z + ewb->error_weights[i].w) * 0.333333f; - ewb->texel_weight_rga[i] = (ewb->error_weights[i].x + ewb->error_weights[i].y + ewb->error_weights[i].w) * 0.333333f; - ewb->texel_weight_rgb[i] = (ewb->error_weights[i].x + ewb->error_weights[i].y + ewb->error_weights[i].z) * 0.333333f; - ewb->texel_weight[i] = (ewb->error_weights[i].x + ewb->error_weights[i].y + ewb->error_weights[i].z + ewb->error_weights[i].w) * 0.25f; - } - - return dot(error_weight_sum, float4(1, 1, 1, 1)); -} - - -/* - functions to analyze block statistical properties: - * simple properties: * mean * variance - * covariance-matrix correllation coefficients - */ - - -// compute averages and covariance matrices for 4 components -static void compute_covariance_matrix(int xdim, int ydim, int zdim, const imageblock * blk, const error_weight_block * ewb, mat4 * cov_matrix) -{ - int i; - - int texels_per_block = xdim * ydim * zdim; - - float r_sum = 0.0f; - float g_sum = 0.0f; - float b_sum = 0.0f; - float a_sum = 0.0f; - float rr_sum = 0.0f; - float gg_sum = 0.0f; - float bb_sum = 0.0f; - float aa_sum = 0.0f; - float rg_sum = 0.0f; - float rb_sum = 0.0f; - float ra_sum = 0.0f; - float gb_sum = 0.0f; - float ga_sum = 0.0f; - float ba_sum = 0.0f; - - float weight_sum = 0.0f; - - for (i = 0; i < texels_per_block; i++) - { - float weight = ewb->texel_weight[i]; - if (weight < 0.0f) - ASTC_CODEC_INTERNAL_ERROR; - weight_sum += weight; - float r = blk->work_data[4 * i]; - float g = blk->work_data[4 * i + 1]; - float b = blk->work_data[4 * i + 2]; - float a = blk->work_data[4 * i + 3]; - r_sum += r * weight; - rr_sum += r * (r * weight); - rg_sum += g * (r * weight); - rb_sum += b * (r * weight); - ra_sum += a * (r * weight); - g_sum += g * weight; - gg_sum += g * (g * weight); - gb_sum += b * (g * weight); - ga_sum += a * (g * weight); - b_sum += b * weight; - bb_sum += b * (b * weight); - ba_sum += a * (b * weight); - a_sum += a * weight; - aa_sum += a * (a * weight); - } - - float rpt = 1.0f / MAX(weight_sum, 1e-7f); - float rs = r_sum; - float gs = g_sum; - float bs = b_sum; - float as = a_sum; - - cov_matrix->v[0] = float4(rr_sum - rs * rs * rpt, rg_sum - rs * gs * rpt, rb_sum - rs * bs * rpt, ra_sum - rs * as * rpt); - cov_matrix->v[1] = float4(rg_sum - rs * gs * rpt, gg_sum - gs * gs * rpt, gb_sum - gs * bs * rpt, ga_sum - gs * as * rpt); - cov_matrix->v[2] = float4(rb_sum - rs * bs * rpt, gb_sum - gs * bs * rpt, bb_sum - bs * bs * rpt, ba_sum - bs * as * rpt); - cov_matrix->v[3] = float4(ra_sum - rs * as * rpt, ga_sum - gs * as * rpt, ba_sum - bs * as * rpt, aa_sum - as * as * rpt); - -} - - - -void prepare_block_statistics(int xdim, int ydim, int zdim, const imageblock * blk, const error_weight_block * ewb, int *is_normal_map, float *lowest_correl) -{ - int i; - - mat4 cov_matrix; - - compute_covariance_matrix(xdim, ydim, zdim, blk, ewb, &cov_matrix); - - // use the covariance matrix to compute - // correllation coefficients - float rr_var = cov_matrix.v[0].x; - float gg_var = cov_matrix.v[1].y; - float bb_var = cov_matrix.v[2].z; - float aa_var = cov_matrix.v[3].w; - - float rg_correlation = cov_matrix.v[0].y / sqrt(MAX(rr_var * gg_var, 1e-30f)); - float rb_correlation = cov_matrix.v[0].z / sqrt(MAX(rr_var * bb_var, 1e-30f)); - float ra_correlation = cov_matrix.v[0].w / sqrt(MAX(rr_var * aa_var, 1e-30f)); - float gb_correlation = cov_matrix.v[1].z / sqrt(MAX(gg_var * bb_var, 1e-30f)); - float ga_correlation = cov_matrix.v[1].w / sqrt(MAX(gg_var * aa_var, 1e-30f)); - float ba_correlation = cov_matrix.v[2].w / sqrt(MAX(bb_var * aa_var, 1e-30f)); - - if (astc_isnan(rg_correlation)) - rg_correlation = 1.0f; - if (astc_isnan(rb_correlation)) - rb_correlation = 1.0f; - if (astc_isnan(ra_correlation)) - ra_correlation = 1.0f; - if (astc_isnan(gb_correlation)) - gb_correlation = 1.0f; - if (astc_isnan(ga_correlation)) - ga_correlation = 1.0f; - if (astc_isnan(ba_correlation)) - ba_correlation = 1.0f; - - float lowest_correlation = MIN(fabs(rg_correlation), fabs(rb_correlation)); - lowest_correlation = MIN(lowest_correlation, fabs(ra_correlation)); - lowest_correlation = MIN(lowest_correlation, fabs(gb_correlation)); - lowest_correlation = MIN(lowest_correlation, fabs(ga_correlation)); - lowest_correlation = MIN(lowest_correlation, fabs(ba_correlation)); - *lowest_correl = lowest_correlation; - - // compute a "normal-map" factor - // this factor should be exactly 0.0 for a normal map, while it may be all over the - // place for anything that is NOT a normal map. We can probably assume that a factor - // of less than 0.2f represents a normal map. - - float nf_sum = 0.0f; - - int texels_per_block = xdim * ydim * zdim; - - for (i = 0; i < texels_per_block; i++) - { - float3 val = float3(blk->orig_data[4 * i], - blk->orig_data[4 * i + 1], - blk->orig_data[4 * i + 2]); - val = (val - float3(0.5f, 0.5f, 0.5f)) * 2.0f; - float length_squared = dot(val, val); - float nf = fabs(length_squared - 1.0f); - nf_sum += nf; - } - float nf_avg = nf_sum / texels_per_block; - *is_normal_map = nf_avg < 0.2; -} - - - - - -void compress_constant_color_block(int xdim, int ydim, int zdim, const imageblock * blk, const error_weight_block * ewb, symbolic_compressed_block * scb) -{ - int texel_count = xdim * ydim * zdim; - int i; - - float4 color_sum = float4(0, 0, 0, 0); - float4 color_weight_sum = float4(0, 0, 0, 0); - - const float *clp = blk->work_data; - for (i = 0; i < texel_count; i++) - { - float4 weights = ewb->error_weights[i]; - float4 color_data = float4(clp[4 * i], clp[4 * i + 1], clp[4 * i + 2], clp[4 * i + 3]); - color_sum = color_sum + (color_data * weights); - color_weight_sum = color_weight_sum + weights; - } - - float4 avg_color = color_sum / color_weight_sum; - - int use_fp16 = blk->rgb_lns[0]; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Averaged color: %f %f %f %f\n", avg_color.x, avg_color.y, avg_color.z, avg_color.w); - } - #endif - - // convert the color - if (blk->rgb_lns[0]) - { - int avg_red = static_cast < int >(floor(avg_color.x + 0.5f)); - int avg_green = static_cast < int >(floor(avg_color.y + 0.5f)); - int avg_blue = static_cast < int >(floor(avg_color.z + 0.5f)); - - if (avg_red < 0) - avg_red = 0; - else if (avg_red > 65535) - avg_red = 65535; - - if (avg_green < 0) - avg_green = 0; - else if (avg_green > 65535) - avg_green = 65535; - - if (avg_blue < 0) - avg_blue = 0; - else if (avg_blue > 65535) - avg_blue = 65535; - - avg_color.x = sf16_to_float(lns_to_sf16(avg_red)); - avg_color.y = sf16_to_float(lns_to_sf16(avg_green)); - avg_color.z = sf16_to_float(lns_to_sf16(avg_blue)); - } - else - { - avg_color.x *= (1.0f / 65535.0f); - avg_color.y *= (1.0f / 65535.0f); - avg_color.z *= (1.0f / 65535.0f); - } - if (blk->alpha_lns[0]) - { - int avg_alpha = static_cast < int >(floor(avg_color.w + 0.5f)); - - if (avg_alpha < 0) - avg_alpha = 0; - else if (avg_alpha > 65535) - avg_alpha = 65535; - - avg_color.w = sf16_to_float(lns_to_sf16(avg_alpha)); - } - else - { - avg_color.w *= (1.0f / 65535.0f); - } - -#ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Averaged color: %f %f %f %f (%d)\n", avg_color.x, avg_color.y, avg_color.z, avg_color.w, use_fp16); - - } -#endif - - if (use_fp16) - { - scb->error_block = 0; - scb->block_mode = -1; - scb->partition_count = 0; - scb->constant_color[0] = float_to_sf16(avg_color.x, SF_NEARESTEVEN); - scb->constant_color[1] = float_to_sf16(avg_color.y, SF_NEARESTEVEN); - scb->constant_color[2] = float_to_sf16(avg_color.z, SF_NEARESTEVEN); - scb->constant_color[3] = float_to_sf16(avg_color.w, SF_NEARESTEVEN); - } - - else - { - scb->error_block = 0; - scb->block_mode = -2; - scb->partition_count = 0; - float red = avg_color.x; - float green = avg_color.y; - float blue = avg_color.z; - float alpha = avg_color.w; - if (red < 0) - red = 0; - else if (red > 1) - red = 1; - if (green < 0) - green = 0; - else if (green > 1) - green = 1; - if (blue < 0) - blue = 0; - else if (blue > 1) - blue = 1; - if (alpha < 0) - alpha = 0; - else if (alpha > 1) - alpha = 1; - scb->constant_color[0] = static_cast < int >(floor(red * 65535.0f + 0.5f)); - scb->constant_color[1] = static_cast < int >(floor(green * 65535.0f + 0.5f)); - scb->constant_color[2] = static_cast < int >(floor(blue * 65535.0f + 0.5f)); - scb->constant_color[3] = static_cast < int >(floor(alpha * 65535.0f + 0.5f)); - } -} - -int block_mode_histogram[2048]; - -float compress_symbolic_block(const astc_codec_image * input_image, - astc_decode_mode decode_mode, int xdim, int ydim, int zdim, const error_weighting_params * ewp, const imageblock * blk, symbolic_compressed_block * scb, - compress_symbolic_block_buffers * tmpbuf) -{ - int i, j; - int xpos = blk->xpos; - int ypos = blk->ypos; - int zpos = blk->zpos; - - int x, y, z; - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Diagnostics of block of dimension %d x %d x %d\n\n", xdim, ydim, zdim); - - printf("XPos: %d YPos: %d ZPos: %d\n", xpos, ypos, zpos); - - printf("Red-min: %f Red-max: %f\n", blk->red_min, blk->red_max); - printf("Green-min: %f Green-max: %f\n", blk->green_min, blk->green_max); - printf("Blue-min: %f Blue-max: %f\n", blk->blue_min, blk->blue_max); - printf("Alpha-min: %f Alpha-max: %f\n", blk->alpha_min, blk->alpha_max); - printf("Grayscale: %d\n", blk->grayscale); - - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - int idx = ((z * ydim + y) * xdim + x) * 4; - printf("Texel (%d %d %d) : orig=< %g, %g, %g, %g >, work=< %g, %g, %g, %g >\n", - x, y, z, - blk->orig_data[idx], - blk->orig_data[idx + 1], blk->orig_data[idx + 2], blk->orig_data[idx + 3], blk->work_data[idx], blk->work_data[idx + 1], blk->work_data[idx + 2], blk->work_data[idx + 3]); - } - printf("\n"); - } - #endif - - - if (blk->red_min == blk->red_max && blk->green_min == blk->green_max && blk->blue_min == blk->blue_max && blk->alpha_min == blk->alpha_max) - { - - // detected a constant-color block. Encode as FP16 if using HDR - scb->error_block = 0; - - if (rgb_force_use_of_hdr) - { - scb->block_mode = -1; - scb->partition_count = 0; - scb->constant_color[0] = float_to_sf16(blk->orig_data[0], SF_NEARESTEVEN); - scb->constant_color[1] = float_to_sf16(blk->orig_data[1], SF_NEARESTEVEN); - scb->constant_color[2] = float_to_sf16(blk->orig_data[2], SF_NEARESTEVEN); - scb->constant_color[3] = float_to_sf16(blk->orig_data[3], SF_NEARESTEVEN); - } - else - { - // Encode as UNORM16 if NOT using HDR. - scb->block_mode = -2; - scb->partition_count = 0; - float red = blk->orig_data[0]; - float green = blk->orig_data[1]; - float blue = blk->orig_data[2]; - float alpha = blk->orig_data[3]; - if (red < 0) - red = 0; - else if (red > 1) - red = 1; - if (green < 0) - green = 0; - else if (green > 1) - green = 1; - if (blue < 0) - blue = 0; - else if (blue > 1) - blue = 1; - if (alpha < 0) - alpha = 0; - else if (alpha > 1) - alpha = 1; - scb->constant_color[0] = (int)floor(red * 65535.0f + 0.5f); - scb->constant_color[1] = (int)floor(green * 65535.0f + 0.5f); - scb->constant_color[2] = (int)floor(blue * 65535.0f + 0.5f); - scb->constant_color[3] = (int)floor(alpha * 65535.0f + 0.5f); - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Block is single-color <%4.4X %4.4X %4.4X %4.4X>\n", scb->constant_color[0], scb->constant_color[1], scb->constant_color[2], scb->constant_color[3]); - } - #endif - - if (print_tile_errors) - printf("0\n"); - - physical_compressed_block psb = symbolic_to_physical(xdim, ydim, zdim, scb); - physical_to_symbolic(xdim, ydim, zdim, psb, scb); - - return 0.0f; - } - - error_weight_block *ewb = tmpbuf->ewb; - error_weight_block_orig *ewbo = tmpbuf->ewbo; - - float error_weight_sum = prepare_error_weight_block(input_image, - xdim, ydim, zdim, - ewp, blk, ewb, ewbo); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n"); - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - int idx = (z * ydim + y) * xdim + x; - printf("ErrorWeight (%d %d %d) : < %g, %g, %g, %g >\n", x, y, z, ewb->error_weights[idx].x, ewb->error_weights[idx].y, ewb->error_weights[idx].z, ewb->error_weights[idx].w); - } - printf("\n"); - } - #endif - - symbolic_compressed_block *tempblocks = tmpbuf->tempblocks; - - float error_of_best_block = 1e20f; - // int modesel=0; - - imageblock *temp = tmpbuf->temp; - - float best_errorvals_in_modes[17]; - for (i = 0; i < 17; i++) - best_errorvals_in_modes[i] = 1e30f; - - int uses_alpha = imageblock_uses_alpha(xdim, ydim, zdim, blk); - - - // compression of average-color blocks disabled for the time being; - // they produce extremely severe block artifacts. -#if 0 - // first, compress an averaged-color block - compress_constant_color_block(xdim, ydim, zdim, blk, ewb, scb); - - decompress_symbolic_block(decode_mode, xdim, ydim, zdim, xpos, ypos, zpos, scb, temp); - - float avgblock_errorval = compute_imageblock_difference(xdim, ydim, zdim, - blk, temp, ewb) * 4.0f; // bias somewhat against the average-color block. - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n-----------------------------------\n"); - printf("Average-color block test completed\n"); - printf("Resulting error value: %g\n", avgblock_errorval); - } - #endif - - - if (avgblock_errorval < error_of_best_block) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Accepted as better than previous-best-error, which was %g\n", error_of_best_block); - #endif - - error_of_best_block = avgblock_errorval; - // *scb = tempblocks[j]; - modesel = 0; - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("-----------------------------------\n"); - } - #endif -#endif - - - float mode_cutoff = ewp->block_mode_cutoff; - - // next, test mode #0. This mode uses 1 plane of weights and 1 partition. - // we test it twice, first with a modecutoff of 0, then with the specified mode-cutoff. - // This causes an early-out that speeds up encoding of "easy" content. - - float modecutoffs[2]; - float errorval_mult[2] = { 2.5, 1 }; - modecutoffs[0] = 0; - modecutoffs[1] = mode_cutoff; - - #if 0 - if ((error_of_best_block / error_weight_sum) < ewp->texel_avg_error_limit) - goto END_OF_TESTS; - #endif - - float best_errorval_in_mode; - for (i = 0; i < 2; i++) - { - compress_symbolic_block_fixed_partition_1_plane(decode_mode, modecutoffs[i], ewp->max_refinement_iters, xdim, ydim, zdim, 1, // partition count - 0, // partition index - blk, ewb, tempblocks, tmpbuf->plane1); - - best_errorval_in_mode = 1e30f; - for (j = 0; j < 4; j++) - { - if (tempblocks[j].error_block) - continue; - decompress_symbolic_block(decode_mode, xdim, ydim, zdim, xpos, ypos, zpos, tempblocks + j, temp); - float errorval = compute_imageblock_difference(xdim, ydim, zdim, - blk, temp, ewb) * errorval_mult[i]; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n-----------------------------------\n"); - printf("Single-weight partition test 0 (1 partition) completed\n"); - printf("Resulting error value: %g\n", errorval); - } - #endif - - if (errorval < best_errorval_in_mode) - best_errorval_in_mode = errorval; - - if (errorval < error_of_best_block) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Accepted as better than previous-best-error, which was %g\n", error_of_best_block); - #endif - - error_of_best_block = errorval; - *scb = tempblocks[j]; - - // modesel = 0; - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("-----------------------------------\n"); - } - #endif - } - - best_errorvals_in_modes[0] = best_errorval_in_mode; - if ((error_of_best_block / error_weight_sum) < ewp->texel_avg_error_limit) - goto END_OF_TESTS; - } - - int is_normal_map; - float lowest_correl; - prepare_block_statistics(xdim, ydim, zdim, blk, ewb, &is_normal_map, &lowest_correl); - - if (is_normal_map && lowest_correl < 0.99f) - lowest_correl = 0.99f; - - // next, test the four possible 1-partition, 2-planes modes - for (i = 0; i < 4; i++) - { - - if (lowest_correl > ewp->lowest_correlation_cutoff) - continue; - - if (blk->grayscale && i != 3) - continue; - - if (!uses_alpha && i == 3) - continue; - - compress_symbolic_block_fixed_partition_2_planes(decode_mode, mode_cutoff, ewp->max_refinement_iters, xdim, ydim, zdim, 1, // partition count - 0, // partition index - i, // the color component to test a separate plane of weights for. - blk, ewb, tempblocks, tmpbuf->planes2); - - best_errorval_in_mode = 1e30f; - for (j = 0; j < 4; j++) - { - if (tempblocks[j].error_block) - continue; - decompress_symbolic_block(decode_mode, xdim, ydim, zdim, xpos, ypos, zpos, tempblocks + j, temp); - float errorval = compute_imageblock_difference(xdim, ydim, zdim, - blk, temp, ewb); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n-----------------------------------\n"); - printf("Dual-weight partition test %d (1 partition) completed\n", i); - printf("Resulting error value: %g\n", errorval); - } - #endif - - if (errorval < best_errorval_in_mode) - best_errorval_in_mode = errorval; - - if (errorval < error_of_best_block) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Accepted as better than previous-best-error, which was %g\n", error_of_best_block); - #endif - - error_of_best_block = errorval; - *scb = tempblocks[j]; - - // modesel = i+1; - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("-----------------------------------\n"); - } - #endif - - best_errorvals_in_modes[i + 1] = best_errorval_in_mode; - } - - if ((error_of_best_block / error_weight_sum) < ewp->texel_avg_error_limit) - goto END_OF_TESTS; - } - - // find best blocks for 2, 3 and 4 partitions - int partition_count; - for (partition_count = 2; partition_count <= 4; partition_count++) - { - int partition_indices_1plane[2]; - int partition_indices_2planes[2]; - - find_best_partitionings(ewp->partition_search_limit, - xdim, ydim, zdim, partition_count, blk, ewb, 1, - &(partition_indices_1plane[0]), &(partition_indices_1plane[1]), &(partition_indices_2planes[0])); - - for (i = 0; i < 2; i++) - { - compress_symbolic_block_fixed_partition_1_plane(decode_mode, mode_cutoff, ewp->max_refinement_iters, xdim, ydim, zdim, partition_count, partition_indices_1plane[i], blk, ewb, tempblocks, tmpbuf->plane1); - - best_errorval_in_mode = 1e30f; - for (j = 0; j < 4; j++) - { - if (tempblocks[j].error_block) - continue; - decompress_symbolic_block(decode_mode, xdim, ydim, zdim, xpos, ypos, zpos, tempblocks + j, temp); - float errorval = compute_imageblock_difference(xdim, ydim, zdim, - blk, temp, ewb); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n-----------------------------------\n"); - printf("Single-weight partition test %d (%d partitions) completed\n", i, partition_count); - printf("Resulting error value: %g\n", errorval); - } - #endif - - if (errorval < best_errorval_in_mode) - best_errorval_in_mode = errorval; - - if (errorval < error_of_best_block) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Accepted as better than previous-best-error, which was %g\n", error_of_best_block); - #endif - - error_of_best_block = errorval; - *scb = tempblocks[j]; - - // modesel = 4*(partition_count-2) + 5 + i; - } - } - - best_errorvals_in_modes[4 * (partition_count - 2) + 5 + i] = best_errorval_in_mode; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("-----------------------------------\n"); - } - #endif - - if ((error_of_best_block / error_weight_sum) < ewp->texel_avg_error_limit) - goto END_OF_TESTS; - } - - - if (partition_count == 2 && !is_normal_map && MIN(best_errorvals_in_modes[5], best_errorvals_in_modes[6]) > (best_errorvals_in_modes[0] * ewp->partition_1_to_2_limit)) - goto END_OF_TESTS; - - // don't bother to check 4 partitions for dual plane of weightss, ever. - if (partition_count == 4) - break; - - for (i = 0; i < 2; i++) - { - if (lowest_correl > ewp->lowest_correlation_cutoff) - continue; - compress_symbolic_block_fixed_partition_2_planes(decode_mode, - mode_cutoff, - ewp->max_refinement_iters, - xdim, ydim, zdim, - partition_count, - partition_indices_2planes[i] & (PARTITION_COUNT - 1), partition_indices_2planes[i] >> PARTITION_BITS, - blk, ewb, tempblocks, tmpbuf->planes2); - - best_errorval_in_mode = 1e30f; - for (j = 0; j < 4; j++) - { - if (tempblocks[j].error_block) - continue; - decompress_symbolic_block(decode_mode, xdim, ydim, zdim, xpos, ypos, zpos, tempblocks + j, temp); - - float errorval = compute_imageblock_difference(xdim, ydim, zdim, - blk, temp, ewb); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("\n-----------------------------------\n"); - printf("Dual-weight partition test %d (%d partitions) completed\n", i, partition_count); - printf("Resulting error value: %g\n", errorval); - } - #endif - - if (errorval < best_errorval_in_mode) - best_errorval_in_mode = errorval; - - if (errorval < error_of_best_block) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Accepted as better than previous-best-error, which was %g\n", error_of_best_block); - #endif - - error_of_best_block = errorval; - *scb = tempblocks[j]; - - // modesel = 4*(partition_count-2) + 5 + 2 + i; - } - } - - best_errorvals_in_modes[4 * (partition_count - 2) + 5 + 2 + i] = best_errorval_in_mode; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("-----------------------------------\n"); - } - #endif - - if ((error_of_best_block / error_weight_sum) < ewp->texel_avg_error_limit) - goto END_OF_TESTS; - } - } - - END_OF_TESTS: - - #if 0 - if (print_statistics) - { - for (i = 0; i < 13; i++) - printf("%f ", best_errorvals_in_modes[i]); - - printf("%d %f %f %f ", modesel, error_of_best_block, - MIN(best_errorvals_in_modes[1], best_errorvals_in_modes[2]) / best_errorvals_in_modes[0], - MIN(MIN(best_errorvals_in_modes[7], best_errorvals_in_modes[8]), best_errorvals_in_modes[9]) / best_errorvals_in_modes[0]); - - printf("\n"); - } - #endif - - if (scb->block_mode >= 0) - block_mode_histogram[scb->block_mode & 0x7ff]++; - - - // compress/decompress to a physical block - physical_compressed_block psb = symbolic_to_physical(xdim, ydim, zdim, scb); - physical_to_symbolic(xdim, ydim, zdim, psb, scb); - - - if (print_tile_errors) - printf("%g\n", error_of_best_block); - - - // mean squared error per color component. - return error_of_best_block / ((float)xdim * ydim * zdim); -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_compute_variance.cpp b/3rdparty/bimg/3rdparty/astc/astc_compute_variance.cpp deleted file mode 100644 index 782bd1dd021..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_compute_variance.cpp +++ /dev/null @@ -1,524 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief ASTC functions to calculate, for each pixel and each color component, - * its variance within an NxN footprint; we want N to be parametric. - * - * The routine below uses summed area tables in order to perform the - * computation in O(1) time per pixel, independent of big N is. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -#include <math.h> -#include "mathlib.h" -#include "softfloat.h" - -float4 *** input_averages; -float *** input_alpha_averages; -float4 *** input_variances; - -#include <stdio.h> - -// routine to compute averages and variances for a pixel region. -// The routine computes both in a single pass, using a summed-area table -// to decouple the running time from the averaging/variance kernel size. - -static void compute_pixel_region_variance(const astc_codec_image * img, float rgb_power_to_use, float alpha_power_to_use, swizzlepattern swz, int use_z_axis, - int source_xoffset,int source_yoffset, int source_zoffset, // position of upper-left pixel in data set - int xsize, int ysize, int zsize, // the size of the region to actually compute averages and variances for. - int avg_var_kernel_radius, int alpha_kernel_radius, - int dest_xoffset, int dest_yoffset, int dest_zoffset) -{ - int x, y, z; - - int kernel_radius = MAX(avg_var_kernel_radius, alpha_kernel_radius); - int kerneldim = 2 * kernel_radius + 1; - - // allocate memory - int xpadsize = xsize + kerneldim; - int ypadsize = ysize + kerneldim; - int zpadsize = zsize + (use_z_axis ? kerneldim : 1); - - double4 ***varbuf1 = new double4 **[zpadsize]; - double4 ***varbuf2 = new double4 **[zpadsize]; - varbuf1[0] = new double4 *[ypadsize * zpadsize]; - varbuf2[0] = new double4 *[ypadsize * zpadsize]; - varbuf1[0][0] = new double4[xpadsize * ypadsize * zpadsize]; - varbuf2[0][0] = new double4[xpadsize * ypadsize * zpadsize]; - - - for (z = 1; z < zpadsize; z++) - { - varbuf1[z] = varbuf1[0] + ypadsize * z; - varbuf2[z] = varbuf2[0] + ypadsize * z; - varbuf1[z][0] = varbuf1[0][0] + xpadsize * ypadsize * z; - varbuf2[z][0] = varbuf2[0][0] + xpadsize * ypadsize * z; - } - - for (z = 0; z < zpadsize; z++) - for (y = 1; y < ypadsize; y++) - { - varbuf1[z][y] = varbuf1[z][0] + xpadsize * y; - varbuf2[z][y] = varbuf2[z][0] + xpadsize * y; - } - - int powers_are_1 = (rgb_power_to_use == 1.0f) && (alpha_power_to_use == 1.0f); - - - // load x and x^2 values into the allocated buffers - if (img->imagedata8) - { - uint8_t data[6]; - data[4] = 0; - data[5] = 255; - - for (z = 0; z < zpadsize - 1; z++) - { - int z_src = z + source_zoffset - (use_z_axis ? kernel_radius : 0); - for (y = 0; y < ypadsize - 1; y++) - { - int y_src = y + source_yoffset - kernel_radius; - for (x = 0; x < xpadsize - 1; x++) - { - int x_src = x + source_xoffset - kernel_radius; - data[0] = img->imagedata8[z_src][y_src][4 * x_src + 0]; - data[1] = img->imagedata8[z_src][y_src][4 * x_src + 1]; - data[2] = img->imagedata8[z_src][y_src][4 * x_src + 2]; - data[3] = img->imagedata8[z_src][y_src][4 * x_src + 3]; - - uint8_t r = data[swz.r]; - uint8_t g = data[swz.g]; - uint8_t b = data[swz.b]; - uint8_t a = data[swz.a]; - - double4 d = double4(r * (1.0 / 255.0), - g * (1.0 / 255.0), - b * (1.0 / 255.0), - a * (1.0 / 255.0)); - - if (perform_srgb_transform) - { - d.x = (d.x <= 0.04045) ? d.x * (1.0 / 12.92) : (d.x <= 1) ? pow((d.x + 0.055) * (1.0 / 1.055), 2.4) : d.x; - d.y = (d.y <= 0.04045) ? d.y * (1.0 / 12.92) : (d.y <= 1) ? pow((d.y + 0.055) * (1.0 / 1.055), 2.4) : d.y; - d.z = (d.z <= 0.04045) ? d.z * (1.0 / 12.92) : (d.z <= 1) ? pow((d.z + 0.055) * (1.0 / 1.055), 2.4) : d.z; - } - - if (!powers_are_1) - { - d.x = pow(MAX(d.x, 1e-6), (double)rgb_power_to_use); - d.y = pow(MAX(d.y, 1e-6), (double)rgb_power_to_use); - d.z = pow(MAX(d.z, 1e-6), (double)rgb_power_to_use); - d.w = pow(MAX(d.w, 1e-6), (double)alpha_power_to_use); - } - - varbuf1[z][y][x] = d; - varbuf2[z][y][x] = d * d; - } - } - } - } - else - { - uint16_t data[6]; - data[4] = 0; - data[5] = 0x3C00; // 1.0 encoded as FP16. - - for (z = 0; z < zpadsize - 1; z++) - { - int z_src = z + source_zoffset - (use_z_axis ? kernel_radius : 0); - for (y = 0; y < ypadsize - 1; y++) - { - int y_src = y + source_yoffset - kernel_radius; - for (x = 0; x < xpadsize - 1; x++) - { - int x_src = x + source_xoffset - kernel_radius; - data[0] = img->imagedata16[z_src][y_src][4 * x_src]; - data[1] = img->imagedata16[z_src][y_src][4 * x_src + 1]; - data[2] = img->imagedata16[z_src][y_src][4 * x_src + 2]; - data[3] = img->imagedata16[z_src][y_src][4 * x_src + 3]; - - uint16_t r = data[swz.r]; - uint16_t g = data[swz.g]; - uint16_t b = data[swz.b]; - uint16_t a = data[swz.a]; - - double4 d = double4(sf16_to_float(r), - sf16_to_float(g), - sf16_to_float(b), - sf16_to_float(a)); - - if (perform_srgb_transform) - { - d.x = (d.x <= 0.04045) ? d.x * (1.0 / 12.92) : (d.x <= 1) ? pow((d.x + 0.055) * (1.0 / 1.055), 2.4) : d.x; - d.y = (d.y <= 0.04045) ? d.y * (1.0 / 12.92) : (d.y <= 1) ? pow((d.y + 0.055) * (1.0 / 1.055), 2.4) : d.y; - d.z = (d.z <= 0.04045) ? d.z * (1.0 / 12.92) : (d.z <= 1) ? pow((d.z + 0.055) * (1.0 / 1.055), 2.4) : d.z; - } - - if (!powers_are_1) - { - d.x = pow(MAX(d.x, 1e-6), (double)rgb_power_to_use); - d.y = pow(MAX(d.y, 1e-6), (double)rgb_power_to_use); - d.z = pow(MAX(d.z, 1e-6), (double)rgb_power_to_use); - d.w = pow(MAX(d.w, 1e-6), (double)alpha_power_to_use); - } - - varbuf1[z][y][x] = d; - varbuf2[z][y][x] = d * d; - } - } - } - } - - - - // pad out buffers with 0s - for (z = 0; z < zpadsize; z++) - { - for (y = 0; y < ypadsize; y++) - { - varbuf1[z][y][xpadsize - 1] = double4(0.0, 0.0, 0.0, 0.0); - varbuf2[z][y][xpadsize - 1] = double4(0.0, 0.0, 0.0, 0.0); - } - for (x = 0; x < xpadsize; x++) - { - varbuf1[z][ypadsize - 1][x] = double4(0.0, 0.0, 0.0, 0.0); - varbuf2[z][ypadsize - 1][x] = double4(0.0, 0.0, 0.0, 0.0); - } - } - - if (use_z_axis) - for (y = 0; y < ypadsize; y++) - for (x = 0; x < xpadsize; x++) - { - varbuf1[zpadsize - 1][y][x] = double4(0.0, 0.0, 0.0, 0.0); - varbuf2[zpadsize - 1][y][x] = double4(0.0, 0.0, 0.0, 0.0); - } - - - // generate summed-area tables for x and x2; this is done in-place - for (z = 0; z < zpadsize; z++) - for (y = 0; y < ypadsize; y++) - { - double4 summa1 = double4(0.0, 0.0, 0.0, 0.0); - double4 summa2 = double4(0.0, 0.0, 0.0, 0.0); - for (x = 0; x < xpadsize; x++) - { - double4 val1 = varbuf1[z][y][x]; - double4 val2 = varbuf2[z][y][x]; - varbuf1[z][y][x] = summa1; - varbuf2[z][y][x] = summa2; - summa1 = summa1 + val1; - summa2 = summa2 + val2; - } - } - - for (z = 0; z < zpadsize; z++) - for (x = 0; x < xpadsize; x++) - { - double4 summa1 = double4(0.0, 0.0, 0.0, 0.0); - double4 summa2 = double4(0.0, 0.0, 0.0, 0.0); - for (y = 0; y < ypadsize; y++) - { - double4 val1 = varbuf1[z][y][x]; - double4 val2 = varbuf2[z][y][x]; - varbuf1[z][y][x] = summa1; - varbuf2[z][y][x] = summa2; - summa1 = summa1 + val1; - summa2 = summa2 + val2; - } - } - - if (use_z_axis) - for (y = 0; y < ypadsize; y++) - for (x = 0; x < xpadsize; x++) - { - double4 summa1 = double4(0.0, 0.0, 0.0, 0.0); - double4 summa2 = double4(0.0, 0.0, 0.0, 0.0); - for (z = 0; z < zpadsize; z++) - { - double4 val1 = varbuf1[z][y][x]; - double4 val2 = varbuf2[z][y][x]; - varbuf1[z][y][x] = summa1; - varbuf2[z][y][x] = summa2; - summa1 = summa1 + val1; - summa2 = summa2 + val2; - } - } - - - int avg_var_kerneldim = 2 * avg_var_kernel_radius + 1; - int alpha_kerneldim = 2 * alpha_kernel_radius + 1; - - - // compute a few constants used in the variance-calculation. - double avg_var_samples; - double alpha_rsamples; - double mul1; - - if (use_z_axis) - { - avg_var_samples = avg_var_kerneldim * avg_var_kerneldim * avg_var_kerneldim; - alpha_rsamples = 1.0 / (alpha_kerneldim * alpha_kerneldim * alpha_kerneldim); - } - else - { - avg_var_samples = avg_var_kerneldim * avg_var_kerneldim; - alpha_rsamples = 1.0 / (alpha_kerneldim * alpha_kerneldim); - } - - - double avg_var_rsamples = 1.0 / avg_var_samples; - if (avg_var_samples == 1) - mul1 = 1.0; - else - mul1 = 1.0 / (avg_var_samples * (avg_var_samples - 1)); - - - double mul2 = avg_var_samples * mul1; - - - // use the summed-area tables to compute variance for each sample-neighborhood - if (use_z_axis) - { - for (z = 0; z < zsize; z++) - { - int z_src = z + kernel_radius; - int z_dst = z + dest_zoffset; - for (y = 0; y < ysize; y++) - { - int y_src = y + kernel_radius; - int y_dst = y + dest_yoffset; - - for (x = 0; x < xsize; x++) - { - int x_src = x + kernel_radius; - int x_dst = x + dest_xoffset; - - // summed-area table lookups for alpha average - double vasum = - (varbuf1[z_src + 1][y_src - alpha_kernel_radius][x_src - alpha_kernel_radius].w - - varbuf1[z_src + 1][y_src - alpha_kernel_radius][x_src + alpha_kernel_radius + 1].w - - varbuf1[z_src + 1][y_src + alpha_kernel_radius + 1][x_src - alpha_kernel_radius].w - + varbuf1[z_src + 1][y_src + alpha_kernel_radius + 1][x_src + alpha_kernel_radius + 1].w) - - (varbuf1[z_src][y_src - alpha_kernel_radius][x_src - alpha_kernel_radius].w - - varbuf1[z_src][y_src - alpha_kernel_radius][x_src + alpha_kernel_radius + 1].w - - varbuf1[z_src][y_src + alpha_kernel_radius + 1][x_src - alpha_kernel_radius].w + varbuf1[z_src][y_src + alpha_kernel_radius + 1][x_src + alpha_kernel_radius + 1].w); - input_alpha_averages[z_dst][y_dst][x_dst] = static_cast < float >(vasum * alpha_rsamples); - - - // summed-area table lookups for RGBA average - double4 v0sum = - (varbuf1[z_src + 1][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src + 1][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] - + varbuf1[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]) - - (varbuf1[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]); - - double4 avg = v0sum * avg_var_rsamples; - - float4 favg = float4(static_cast < float >(avg.x), - static_cast < float >(avg.y), - static_cast < float >(avg.z), - static_cast < float >(avg.w)); - input_averages[z_dst][y_dst][x_dst] = favg; - - - // summed-area table lookups for variance - double4 v1sum = - (varbuf1[z_src + 1][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src + 1][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] - + varbuf1[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]) - - (varbuf1[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]); - double4 v2sum = - (varbuf2[z_src + 1][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf2[z_src + 1][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf2[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] - + varbuf2[z_src + 1][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]) - - (varbuf2[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf2[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf2[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf2[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]); - - // the actual variance - double4 variance = mul2 * v2sum - mul1 * (v1sum * v1sum); - - float4 fvar = float4(static_cast < float >(variance.x), - static_cast < float >(variance.y), - static_cast < float >(variance.z), - static_cast < float >(variance.w)); - input_variances[z_dst][y_dst][x_dst] = fvar; - } - } - } - } - else - { - for (z = 0; z < zsize; z++) - { - int z_src = z; - int z_dst = z + dest_zoffset; - for (y = 0; y < ysize; y++) - { - int y_src = y + kernel_radius; - int y_dst = y + dest_yoffset; - - for (x = 0; x < xsize; x++) - { - int x_src = x + kernel_radius; - int x_dst = x + dest_xoffset; - - // summed-area table lookups for alpha average - double vasum = - varbuf1[z_src][y_src - alpha_kernel_radius][x_src - alpha_kernel_radius].w - - varbuf1[z_src][y_src - alpha_kernel_radius][x_src + alpha_kernel_radius + 1].w - - varbuf1[z_src][y_src + alpha_kernel_radius + 1][x_src - alpha_kernel_radius].w + varbuf1[z_src][y_src + alpha_kernel_radius + 1][x_src + alpha_kernel_radius + 1].w; - input_alpha_averages[z_dst][y_dst][x_dst] = static_cast < float >(vasum * alpha_rsamples); - - - // summed-area table lookups for RGBA average - double4 v0sum = - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]; - - double4 avg = v0sum * avg_var_rsamples; - - float4 favg = float4(static_cast < float >(avg.x), - static_cast < float >(avg.y), - static_cast < float >(avg.z), - static_cast < float >(avg.w)); - input_averages[z_dst][y_dst][x_dst] = favg; - - - // summed-area table lookups for variance - double4 v1sum = - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf1[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf1[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]; - double4 v2sum = - varbuf2[z_src][y_src - avg_var_kernel_radius][x_src - avg_var_kernel_radius] - - varbuf2[z_src][y_src - avg_var_kernel_radius][x_src + avg_var_kernel_radius + 1] - - varbuf2[z_src][y_src + avg_var_kernel_radius + 1][x_src - avg_var_kernel_radius] + varbuf2[z_src][y_src + avg_var_kernel_radius + 1][x_src + avg_var_kernel_radius + 1]; - - // the actual variance - double4 variance = mul2 * v2sum - mul1 * (v1sum * v1sum); - - float4 fvar = float4(static_cast < float >(variance.x), - static_cast < float >(variance.y), - static_cast < float >(variance.z), - static_cast < float >(variance.w)); - input_variances[z_dst][y_dst][x_dst] = fvar; - } - } - } - } - delete[]varbuf2[0][0]; - delete[]varbuf1[0][0]; - delete[]varbuf2[0]; - delete[]varbuf1[0]; - delete[]varbuf2; - delete[]varbuf1; -} - - -static void allocate_input_average_and_variance_buffers(int xsize, int ysize, int zsize) -{ - int y, z; - if (input_averages) - { - delete[]input_averages[0][0]; - delete[]input_averages[0]; - delete[]input_averages; - } - if (input_variances) - { - delete[]input_variances[0][0]; - delete[]input_variances[0]; - delete[]input_variances; - } - if (input_alpha_averages) - { - delete[]input_alpha_averages[0][0]; - delete[]input_alpha_averages[0]; - delete[]input_alpha_averages; - } - - input_averages = new float4 **[zsize]; - input_variances = new float4 **[zsize]; - input_alpha_averages = new float **[zsize]; - - - input_averages[0] = new float4 *[ysize * zsize]; - input_variances[0] = new float4 *[ysize * zsize]; - input_alpha_averages[0] = new float *[ysize * zsize]; - - input_averages[0][0] = new float4[xsize * ysize * zsize]; - input_variances[0][0] = new float4[xsize * ysize * zsize]; - input_alpha_averages[0][0] = new float[xsize * ysize * zsize]; - - for (z = 1; z < zsize; z++) - { - input_averages[z] = input_averages[0] + z * ysize; - input_variances[z] = input_variances[0] + z * ysize; - input_alpha_averages[z] = input_alpha_averages[0] + z * ysize; - - input_averages[z][0] = input_averages[0][0] + z * ysize * xsize; - input_variances[z][0] = input_variances[0][0] + z * ysize * xsize; - input_alpha_averages[z][0] = input_alpha_averages[0][0] + z * ysize * xsize; - } - - for (z = 0; z < zsize; z++) - for (y = 1; y < ysize; y++) - { - input_averages[z][y] = input_averages[z][0] + y * xsize; - input_variances[z][y] = input_variances[z][0] + y * xsize; - input_alpha_averages[z][y] = input_alpha_averages[z][0] + y * xsize; - } - -} - - -// compute averages and variances for the current input image. -void compute_averages_and_variances(const astc_codec_image * img, float rgb_power_to_use, float alpha_power_to_use, int avg_var_kernel_radius, int alpha_kernel_radius, swizzlepattern swz) -{ - int xsize = img->xsize; - int ysize = img->ysize; - int zsize = img->zsize; - allocate_input_average_and_variance_buffers(xsize, ysize, zsize); - - - int x, y, z; - for (z = 0; z < zsize; z += 32) - { - int zblocksize = MIN(32, zsize - z); - for (y = 0; y < ysize; y += 32) - { - int yblocksize = MIN(32, ysize - y); - for (x = 0; x < xsize; x += 32) - { - int xblocksize = MIN(32, xsize - x); - compute_pixel_region_variance(img, - rgb_power_to_use, - alpha_power_to_use, - swz, - (zsize > 1), - x + img->padding, - y + img->padding, z + (zsize > 1 ? img->padding : 0), xblocksize, yblocksize, zblocksize, avg_var_kernel_radius, alpha_kernel_radius, x, y, z); - } - } - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_decompress_symbolic.cpp b/3rdparty/bimg/3rdparty/astc/astc_decompress_symbolic.cpp deleted file mode 100644 index 89ff3441b30..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_decompress_symbolic.cpp +++ /dev/null @@ -1,317 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Decompress a block of colors, expressed as a symbolic block, - * for ASTC. - */ -/*----------------------------------------------------------------------------*/ - -#include <math.h> - -#include "astc_codec_internals.h" - -#include "softfloat.h" -#include <stdio.h> - -int compute_value_of_texel_int(int texel_to_get, const decimation_table * it, const int *weights) -{ - int i; - int summed_value = 8; - int weights_to_evaluate = it->texel_num_weights[texel_to_get]; - for (i = 0; i < weights_to_evaluate; i++) - { - summed_value += weights[it->texel_weights[texel_to_get][i]] * it->texel_weights_int[texel_to_get][i]; - } - return summed_value >> 4; -} - - -ushort4 lerp_color_int(astc_decode_mode decode_mode, ushort4 color0, ushort4 color1, int weight, int plane2_weight, int plane2_color_component // -1 in 1-plane mode - ) -{ - int4 ecolor0 = int4(color0.x, color0.y, color0.z, color0.w); - int4 ecolor1 = int4(color1.x, color1.y, color1.z, color1.w); - - int4 eweight1 = int4(weight, weight, weight, weight); - switch (plane2_color_component) - { - case 0: - eweight1.x = plane2_weight; - break; - case 1: - eweight1.y = plane2_weight; - break; - case 2: - eweight1.z = plane2_weight; - break; - case 3: - eweight1.w = plane2_weight; - break; - default: - break; - } - - int4 eweight0 = int4(64, 64, 64, 64) - eweight1; - - if (decode_mode == DECODE_LDR_SRGB) - { - ecolor0 = ecolor0 >> 8; - ecolor1 = ecolor1 >> 8; - } - int4 color = (ecolor0 * eweight0) + (ecolor1 * eweight1) + int4(32, 32, 32, 32); - color = color >> 6; - if (decode_mode == DECODE_LDR_SRGB) - color = color | (color << 8); - - ushort4 rcolor = ushort4(color.x, color.y, color.z, color.w); - return rcolor; -} - - -void decompress_symbolic_block(astc_decode_mode decode_mode, - int xdim, int ydim, int zdim, // dimensions of block - int xpos, int ypos, int zpos, // position of block - const symbolic_compressed_block * scb, imageblock * blk) -{ - blk->xpos = xpos; - blk->ypos = ypos; - blk->zpos = zpos; - - int i; - - // if we detected an error-block, blow up immediately. - if (scb->error_block) - { - if (decode_mode == DECODE_LDR_SRGB) - { - for (i = 0; i < xdim * ydim * zdim; i++) - { - blk->orig_data[4 * i] = 1.0f; - blk->orig_data[4 * i + 1] = 0.0f; - blk->orig_data[4 * i + 2] = 1.0f; - blk->orig_data[4 * i + 3] = 1.0f; - blk->rgb_lns[i] = 0; - blk->alpha_lns[i] = 0; - blk->nan_texel[i] = 0; - } - } - else - { - for (i = 0; i < xdim * ydim * zdim; i++) - { - blk->orig_data[4 * i] = 0.0f; - blk->orig_data[4 * i + 1] = 0.0f; - blk->orig_data[4 * i + 2] = 0.0f; - blk->orig_data[4 * i + 3] = 0.0f; - blk->rgb_lns[i] = 0; - blk->alpha_lns[i] = 0; - blk->nan_texel[i] = 1; - } - } - - imageblock_initialize_work_from_orig(blk, xdim * ydim * zdim); - update_imageblock_flags(blk, xdim, ydim, zdim); - return; - } - - - - - if (scb->block_mode < 0) - { - float red = 0, green = 0, blue = 0, alpha = 0; - int use_lns = 0; - int use_nan = 0; - - if (scb->block_mode == -2) - { - // For sRGB decoding, we should return only the top 8 bits. - int mask = (decode_mode == DECODE_LDR_SRGB) ? 0xFF00 : 0xFFFF; - - red = sf16_to_float(unorm16_to_sf16(scb->constant_color[0] & mask)); - green = sf16_to_float(unorm16_to_sf16(scb->constant_color[1] & mask)); - blue = sf16_to_float(unorm16_to_sf16(scb->constant_color[2] & mask)); - alpha = sf16_to_float(unorm16_to_sf16(scb->constant_color[3] & mask)); - use_lns = 0; - use_nan = 0; - } - else - { - switch (decode_mode) - { - case DECODE_LDR_SRGB: - red = 1.0f; - green = 0.0f; - blue = 1.0f; - alpha = 1.0f; - use_lns = 0; - use_nan = 0; - break; - case DECODE_LDR: - red = 0.0f; - green = 0.0f; - blue = 0.0f; - alpha = 0.0f; - use_lns = 0; - use_nan = 1; - break; - case DECODE_HDR: - // constant-color block; unpack from FP16 to FP32. - red = sf16_to_float(scb->constant_color[0]); - green = sf16_to_float(scb->constant_color[1]); - blue = sf16_to_float(scb->constant_color[2]); - alpha = sf16_to_float(scb->constant_color[3]); - use_lns = 1; - use_nan = 0; - break; - } - } - - for (i = 0; i < xdim * ydim * zdim; i++) - { - blk->orig_data[4 * i] = red; - blk->orig_data[4 * i + 1] = green; - blk->orig_data[4 * i + 2] = blue; - blk->orig_data[4 * i + 3] = alpha; - blk->rgb_lns[i] = use_lns; - blk->alpha_lns[i] = use_lns; - blk->nan_texel[i] = use_nan; - } - - - imageblock_initialize_work_from_orig(blk, xdim * ydim * zdim); - update_imageblock_flags(blk, xdim, ydim, zdim); - return; - } - - - // get the appropriate partition-table entry - int partition_count = scb->partition_count; - const partition_info *pt = get_partition_table(xdim, ydim, zdim, partition_count); - pt += scb->partition_index; - - // get the appropriate block descriptor - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - - - const decimation_table *it = ixtab2[bsd->block_modes[scb->block_mode].decimation_mode]; - - int is_dual_plane = bsd->block_modes[scb->block_mode].is_dual_plane; - - int weight_quantization_level = bsd->block_modes[scb->block_mode].quantization_mode; - - - // decode the color endpoints - ushort4 color_endpoint0[4]; - ushort4 color_endpoint1[4]; - int rgb_hdr_endpoint[4]; - int alpha_hdr_endpoint[4]; - int nan_endpoint[4]; - - for (i = 0; i < partition_count; i++) - unpack_color_endpoints(decode_mode, - scb->color_formats[i], - scb->color_quantization_level, scb->color_values[i], &(rgb_hdr_endpoint[i]), &(alpha_hdr_endpoint[i]), &(nan_endpoint[i]), &(color_endpoint0[i]), &(color_endpoint1[i])); - - - - - - // first unquantize the weights - int uq_plane1_weights[MAX_WEIGHTS_PER_BLOCK]; - int uq_plane2_weights[MAX_WEIGHTS_PER_BLOCK]; - int weight_count = it->num_weights; - - - const quantization_and_transfer_table *qat = &(quant_and_xfer_tables[weight_quantization_level]); - - for (i = 0; i < weight_count; i++) - { - uq_plane1_weights[i] = qat->unquantized_value[scb->plane1_weights[i]]; - } - if (is_dual_plane) - { - for (i = 0; i < weight_count; i++) - uq_plane2_weights[i] = qat->unquantized_value[scb->plane2_weights[i]]; - } - - - // then undecimate them. - int weights[MAX_TEXELS_PER_BLOCK]; - int plane2_weights[MAX_TEXELS_PER_BLOCK]; - - - int texels_per_block = xdim * ydim * zdim; - for (i = 0; i < texels_per_block; i++) - weights[i] = compute_value_of_texel_int(i, it, uq_plane1_weights); - - if (is_dual_plane) - for (i = 0; i < texels_per_block; i++) - plane2_weights[i] = compute_value_of_texel_int(i, it, uq_plane2_weights); - - - int plane2_color_component = scb->plane2_color_component; - - - // now that we have endpoint colors and weights, we can unpack actual colors for - // each texel. - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - - ushort4 color = lerp_color_int(decode_mode, - color_endpoint0[partition], - color_endpoint1[partition], - weights[i], - plane2_weights[i], - is_dual_plane ? plane2_color_component : -1); - - blk->rgb_lns[i] = rgb_hdr_endpoint[partition]; - blk->alpha_lns[i] = alpha_hdr_endpoint[partition]; - blk->nan_texel[i] = nan_endpoint[partition]; - - blk->work_data[4 * i] = color.x; - blk->work_data[4 * i + 1] = color.y; - blk->work_data[4 * i + 2] = color.z; - blk->work_data[4 * i + 3] = color.w; - } - - imageblock_initialize_orig_from_work(blk, xdim * ydim * zdim); - - update_imageblock_flags(blk, xdim, ydim, zdim); -} - - - -float compute_imageblock_difference(int xdim, int ydim, int zdim, const imageblock * p1, const imageblock * p2, const error_weight_block * ewb) -{ - int i; - int texels_per_block = xdim * ydim * zdim; - float summa = 0.0f; - const float *f1 = p1->work_data; - const float *f2 = p2->work_data; - for (i = 0; i < texels_per_block; i++) - { - float rdiff = fabsf(f1[4 * i] - f2[4 * i]); - float gdiff = fabs(f1[4 * i + 1] - f2[4 * i + 1]); - float bdiff = fabs(f1[4 * i + 2] - f2[4 * i + 2]); - float adiff = fabs(f1[4 * i + 3] - f2[4 * i + 3]); - rdiff = MIN(rdiff, 1e15f); - gdiff = MIN(gdiff, 1e15f); - bdiff = MIN(bdiff, 1e15f); - adiff = MIN(adiff, 1e15f); - - summa += rdiff * rdiff * ewb->error_weights[i].x + gdiff * gdiff * ewb->error_weights[i].y + bdiff * bdiff * ewb->error_weights[i].z + adiff * adiff * ewb->error_weights[i].w; - } - - return summa; -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_encoding_choice_error.cpp b/3rdparty/bimg/3rdparty/astc/astc_encoding_choice_error.cpp deleted file mode 100644 index 5da893bb44b..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_encoding_choice_error.cpp +++ /dev/null @@ -1,310 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Determine color errors for ASTC compression. - * - * We assume that there are two independent sources of color error in - * any given partition. - * - * These are: - * * quantization errors - * * encoding choice errors - * - * Encoding choice errors are errors that come due to encoding choice, - * such as: - * * using luminance instead of RGB - * * using RGB-scale instead of two RGB endpoints. - * * dropping Alpha - * - * Quantization errors occur due to the limited precision we use for - * storing numbers. - * - * Quantization errors generally scale with quantization level, but are - * not actually independent of color encoding. In particular: - * * if we can use offset encoding then quantization error is halved. - * * if we can use blue-contraction, quantization error for red and - * green is halved. - * * quantization error is higher for the HDR endpoint modes. - * - * Other than these errors, quantization error is assumed to be - * proportional to the quantization step. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -#include <math.h> - -#ifdef DEBUG_PRINT_DIAGNOSTICS - #include <stdio.h> -#endif - -// helper function to merge two endpoint-colors -void merge_endpoints(const endpoints * ep1, // contains three of the color components - const endpoints * ep2, // contains the remaining color component - int separate_component, endpoints * res) -{ - int i; - int partition_count = ep1->partition_count; - res->partition_count = partition_count; - for (i = 0; i < partition_count; i++) - { - res->endpt0[i] = ep1->endpt0[i]; - res->endpt1[i] = ep1->endpt1[i]; - } - - switch (separate_component) - { - case 0: - for (i = 0; i < partition_count; i++) - { - res->endpt0[i].x = ep2->endpt0[i].x; - res->endpt1[i].x = ep2->endpt1[i].x; - } - break; - case 1: - for (i = 0; i < partition_count; i++) - { - res->endpt0[i].y = ep2->endpt0[i].y; - res->endpt1[i].y = ep2->endpt1[i].y; - } - break; - case 2: - for (i = 0; i < partition_count; i++) - { - res->endpt0[i].z = ep2->endpt0[i].z; - res->endpt1[i].z = ep2->endpt1[i].z; - } - break; - case 3: - for (i = 0; i < partition_count; i++) - { - res->endpt0[i].w = ep2->endpt0[i].w; - res->endpt1[i].w = ep2->endpt1[i].w; - } - break; - } -} - - - -/* - for a given set of input colors and a given partitioning, determine: color error that results - from RGB-scale encoding (relevant for LDR only) color error that results from RGB-lumashift encoding - (relevant for HDR only) color error that results from luminance-encoding color error that results - form dropping alpha. whether we are eligible for offset encoding whether we are eligible for - blue-contraction - - The input data are: color data partitioning error-weight data - */ - - -void compute_encoding_choice_errors(int xdim, int ydim, int zdim, const imageblock * pb, const partition_info * pi, const error_weight_block * ewb, - int separate_component, // component that is separated out in 2-plane mode, -1 in 1-plane mode - encoding_choice_errors * eci) -{ - int i; - - int partition_count = pi->partition_count; - - int texels_per_block = xdim * ydim * zdim; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : texels-per-block=%dx%dx%d, separate_component=%d, partition-count=%d\n", __func__, xdim, ydim, zdim, separate_component, partition_count); - } - #endif - - float3 averages[4]; - float3 directions_rgb[4]; - float2 directions_rg[4]; - float2 directions_rb[4]; - float2 directions_gb[4]; - - float4 error_weightings[4]; - float4 color_scalefactors[4]; - float4 inverse_color_scalefactors[4]; - - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pi, error_weightings, color_scalefactors); - - compute_averages_and_directions_rgb(pi, pb, ewb, color_scalefactors, averages, directions_rgb, directions_rg, directions_rb, directions_gb); - - line3 uncorr_rgb_lines[4]; - line3 samechroma_rgb_lines[4]; // for LDR-RGB-scale - line3 rgb_luma_lines[4]; // for HDR-RGB-scale - line3 luminance_lines[4]; - - processed_line3 proc_uncorr_rgb_lines[4]; - processed_line3 proc_samechroma_rgb_lines[4]; // for LDR-RGB-scale - processed_line3 proc_rgb_luma_lines[4]; // for HDR-RGB-scale - processed_line3 proc_luminance_lines[4]; - - - for (i = 0; i < partition_count; i++) - { - inverse_color_scalefactors[i].x = 1.0f / MAX(color_scalefactors[i].x, 1e-7f); - inverse_color_scalefactors[i].y = 1.0f / MAX(color_scalefactors[i].y, 1e-7f); - inverse_color_scalefactors[i].z = 1.0f / MAX(color_scalefactors[i].z, 1e-7f); - inverse_color_scalefactors[i].w = 1.0f / MAX(color_scalefactors[i].w, 1e-7f); - - - uncorr_rgb_lines[i].a = averages[i]; - if (dot(directions_rgb[i], directions_rgb[i]) == 0.0f) - uncorr_rgb_lines[i].b = normalize(float3(color_scalefactors[i].xyz)); - else - uncorr_rgb_lines[i].b = normalize(directions_rgb[i]); - - samechroma_rgb_lines[i].a = float3(0, 0, 0); - if (dot(averages[i], averages[i]) < 1e-20) - samechroma_rgb_lines[i].b = normalize(float3(color_scalefactors[i].xyz)); - else - samechroma_rgb_lines[i].b = normalize(averages[i]); - - rgb_luma_lines[i].a = averages[i]; - rgb_luma_lines[i].b = normalize(color_scalefactors[i].xyz); - - luminance_lines[i].a = float3(0, 0, 0); - luminance_lines[i].b = normalize(color_scalefactors[i].xyz); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Partition %d\n", i); - printf("Average = <%g %g %g>\n", averages[i].x, averages[i].y, averages[i].z); - printf("Uncorr-rgb-line = <%g %g %g> + t<%g %g %g>\n", - uncorr_rgb_lines[i].a.x, uncorr_rgb_lines[i].a.y, uncorr_rgb_lines[i].a.z, uncorr_rgb_lines[i].b.x, uncorr_rgb_lines[i].b.y, uncorr_rgb_lines[i].b.z); - printf("Samechroma-line = t<%g %g %g>\n", samechroma_rgb_lines[i].b.x, samechroma_rgb_lines[i].b.y, samechroma_rgb_lines[i].b.z); - } - #endif - - proc_uncorr_rgb_lines[i].amod = (uncorr_rgb_lines[i].a - uncorr_rgb_lines[i].b * dot(uncorr_rgb_lines[i].a, uncorr_rgb_lines[i].b)) * inverse_color_scalefactors[i].xyz; - proc_uncorr_rgb_lines[i].bs = uncorr_rgb_lines[i].b * color_scalefactors[i].xyz; - proc_uncorr_rgb_lines[i].bis = uncorr_rgb_lines[i].b * inverse_color_scalefactors[i].xyz; - - proc_samechroma_rgb_lines[i].amod = (samechroma_rgb_lines[i].a - samechroma_rgb_lines[i].b * dot(samechroma_rgb_lines[i].a, samechroma_rgb_lines[i].b)) * inverse_color_scalefactors[i].xyz; - proc_samechroma_rgb_lines[i].bs = samechroma_rgb_lines[i].b * color_scalefactors[i].xyz; - proc_samechroma_rgb_lines[i].bis = samechroma_rgb_lines[i].b * inverse_color_scalefactors[i].xyz; - - proc_rgb_luma_lines[i].amod = (rgb_luma_lines[i].a - rgb_luma_lines[i].b * dot(rgb_luma_lines[i].a, rgb_luma_lines[i].b)) * inverse_color_scalefactors[i].xyz; - proc_rgb_luma_lines[i].bs = rgb_luma_lines[i].b * color_scalefactors[i].xyz; - proc_rgb_luma_lines[i].bis = rgb_luma_lines[i].b * inverse_color_scalefactors[i].xyz; - - proc_luminance_lines[i].amod = (luminance_lines[i].a - luminance_lines[i].b * dot(luminance_lines[i].a, luminance_lines[i].b)) * inverse_color_scalefactors[i].xyz; - proc_luminance_lines[i].bs = luminance_lines[i].b * color_scalefactors[i].xyz; - proc_luminance_lines[i].bis = luminance_lines[i].b * inverse_color_scalefactors[i].xyz; - - } - - - - float uncorr_rgb_error[4]; - float samechroma_rgb_error[4]; - float rgb_luma_error[4]; - float luminance_rgb_error[4]; - - - for (i = 0; i < partition_count; i++) - { - - uncorr_rgb_error[i] = compute_error_squared_rgb_single_partition(i, xdim, ydim, zdim, pi, pb, ewb, &(proc_uncorr_rgb_lines[i])); - - samechroma_rgb_error[i] = compute_error_squared_rgb_single_partition(i, xdim, ydim, zdim, pi, pb, ewb, &(proc_samechroma_rgb_lines[i])); - - rgb_luma_error[i] = compute_error_squared_rgb_single_partition(i, xdim, ydim, zdim, pi, pb, ewb, &(proc_rgb_luma_lines[i])); - - luminance_rgb_error[i] = compute_error_squared_rgb_single_partition(i, xdim, ydim, zdim, pi, pb, ewb, &(proc_luminance_lines[i])); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Partition %d : uncorr-error=%g samechroma-error=%g rgb-luma-error=%g lum-error=%g\n", - i, uncorr_rgb_error[i], samechroma_rgb_error[i], rgb_luma_error[i], luminance_rgb_error[i]); - } - #endif - } - - // compute the error that arises from just ditching alpha and RGB - float alpha_drop_error[4]; - float rgb_drop_error[4]; - for (i = 0; i < partition_count; i++) - { - alpha_drop_error[i] = 0; - rgb_drop_error[i] = 0; - } - for (i = 0; i < texels_per_block; i++) - { - int partition = pi->partition_of_texel[i]; - float alpha = pb->work_data[4 * i + 3]; - float default_alpha = pb->alpha_lns[i] ? (float)0x7800 : (float)0xFFFF; - - float omalpha = alpha - default_alpha; - alpha_drop_error[partition] += omalpha * omalpha * ewb->error_weights[i].w; - float red = pb->work_data[4 * i]; - float green = pb->work_data[4 * i + 1]; - float blue = pb->work_data[4 * i + 2]; - rgb_drop_error[partition] += red * red * ewb->error_weights[i].x + green * green * ewb->error_weights[i].y + blue * blue * ewb->error_weights[i].z; - } - - // check if we are eligible for blue-contraction and offset-encoding - - endpoints ep; - if (separate_component == -1) - { - endpoints_and_weights ei; - compute_endpoints_and_ideal_weights_1_plane(xdim, ydim, zdim, pi, pb, ewb, &ei); - ep = ei.ep; - } - else - { - endpoints_and_weights ei1, ei2; - compute_endpoints_and_ideal_weights_2_planes(xdim, ydim, zdim, pi, pb, ewb, separate_component, &ei1, &ei2); - - merge_endpoints(&(ei1.ep), &(ei2.ep), separate_component, &ep); - } - - int eligible_for_offset_encode[4]; - int eligible_for_blue_contraction[4]; - - for (i = 0; i < partition_count; i++) - { - float4 endpt0 = ep.endpt0[i]; - float4 endpt1 = ep.endpt1[i]; - float4 endpt_dif = endpt1 - endpt0; - if (fabs(endpt_dif.x) < (0.12 * 65535.0f) && fabs(endpt_dif.y) < (0.12 * 65535.0f) && fabs(endpt_dif.z) < (0.12 * 65535.0f)) - eligible_for_offset_encode[i] = 1; - else - eligible_for_offset_encode[i] = 0; - endpt0.x += (endpt0.x - endpt0.z); - endpt0.y += (endpt0.y - endpt0.z); - endpt1.x += (endpt1.x - endpt1.z); - endpt1.y += (endpt1.y - endpt1.z); - if (endpt0.x > (0.01f * 65535.0f) && endpt0.x < (0.99f * 65535.0f) - && endpt1.x > (0.01f * 65535.0f) && endpt1.x < (0.99f * 65535.0f) - && endpt0.y > (0.01f * 65535.0f) && endpt0.y < (0.99f * 65535.0f) && endpt1.y > (0.01f * 65535.0f) && endpt1.y < (0.99f * 65535.0f)) - eligible_for_blue_contraction[i] = 1; - else - eligible_for_blue_contraction[i] = 0; - } - - - // finally, gather up our results - for (i = 0; i < partition_count; i++) - { - eci[i].rgb_scale_error = (samechroma_rgb_error[i] - uncorr_rgb_error[i]) * 0.7f; // empirical - eci[i].rgb_luma_error = (rgb_luma_error[i] - uncorr_rgb_error[i]) * 1.5f; // wild guess - eci[i].luminance_error = (luminance_rgb_error[i] - uncorr_rgb_error[i]) * 3.0f; // empirical - eci[i].alpha_drop_error = alpha_drop_error[i] * 3.0f; - eci[i].rgb_drop_error = rgb_drop_error[i] * 3.0f; - eci[i].can_offset_encode = eligible_for_offset_encode[i]; - eci[i].can_blue_contract = eligible_for_blue_contraction[i]; - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_find_best_partitioning.cpp b/3rdparty/bimg/3rdparty/astc/astc_find_best_partitioning.cpp deleted file mode 100644 index f53ab1c9b3e..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_find_best_partitioning.cpp +++ /dev/null @@ -1,865 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief ASTC encoding of texture - * - * major step 1: - * * find best partitioning assuming uncorrelated colors - * * find best partitioning assuming RGBS color representation - * - * finding best partitioning for a block: - * * for each available partitioning: - * * compute mean-color-value and dominant direction. - * * this defines two lines, both of which go through the - * mean-color-value: - * * one line has a direction defined by the dominant direction; - * this line is used to assess the error from using an uncorrelated - * color representation. - * * the other line goes through (0,0,0,1) and is used to assess the - * error from using an RGBS color representation. - * * we then compute, as a sum across the block, the squared-errors - * that result from using the dominant-direction-lines and the - * squared-errors that result from using the 0001-lines. - */ -/*----------------------------------------------------------------------------*/ - -/* - * Partition table representation: - * We have 3 tables, each with 1024 partitionings - * (these correspond to the 3x128 hardware partitionings crossed with all the - * partition-transform modes in the hardware.) - * - * For each partitioning, we have: - * * a 4-entry table indicating how many texels there are in each of the 4 - * partitions. this may be from 2 to about 60 or so. - * * a 64-entry table indicating the partition index of each of the 64 texels - * in the block. each index may be 0, 1, 2 or 3. - * - * each element in the table is an uint8_t indicating partition index (0, 1, 2 or 3) - */ - -#include <math.h> - -#include "astc_codec_internals.h" - -#ifdef DEBUG_PRINT_DIAGNOSTICS - #include <stdio.h> -#endif - -#include "mathlib.h" - -int imageblock_uses_alpha(int xdim, int ydim, int zdim, const imageblock * pb) -{ - IGNORE(xdim); - IGNORE(ydim); - IGNORE(zdim); - - return pb->alpha_max != pb->alpha_min; -} - -static void compute_alpha_minmax(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, float *alpha_min, float *alpha_max) -{ - int i; - int partition_count = pt->partition_count; - - int texels_per_block = xdim * ydim * zdim; - - for (i = 0; i < partition_count; i++) - { - alpha_min[i] = 1e38f; - alpha_max[i] = -1e38f; - } - - for (i = 0; i < texels_per_block; i++) - { - if (ewb->texel_weight[i] > 1e-10) - { - int partition = pt->partition_of_texel[i]; - float alphaval = blk->work_data[4 * i + 3]; - if (alphaval > alpha_max[partition]) - alpha_max[partition] = alphaval; - if (alphaval < alpha_min[partition]) - alpha_min[partition] = alphaval; - } - } - - for (i = 0; i < partition_count; i++) - { - if (alpha_min[i] >= alpha_max[i]) - { - alpha_min[i] = 0; - alpha_max[i] = 1e-10f; - } - } -} - - -static void compute_rgb_minmax(int xdim, - int ydim, - int zdim, - const partition_info * pt, - const imageblock * blk, const error_weight_block * ewb, float *red_min, float *red_max, float *green_min, float *green_max, float *blue_min, float *blue_max) -{ - int i; - int partition_count = pt->partition_count; - int texels_per_block = xdim * ydim * zdim; - - for (i = 0; i < partition_count; i++) - { - red_min[i] = 1e38f; - red_max[i] = -1e38f; - green_min[i] = 1e38f; - green_max[i] = -1e38f; - blue_min[i] = 1e38f; - blue_max[i] = -1e38f; - } - - for (i = 0; i < texels_per_block; i++) - { - if (ewb->texel_weight[i] > 1e-10f) - { - int partition = pt->partition_of_texel[i]; - float redval = blk->work_data[4 * i]; - float greenval = blk->work_data[4 * i + 1]; - float blueval = blk->work_data[4 * i + 2]; - if (redval > red_max[partition]) - red_max[partition] = redval; - if (redval < red_min[partition]) - red_min[partition] = redval; - if (greenval > green_max[partition]) - green_max[partition] = greenval; - if (greenval < green_min[partition]) - green_min[partition] = greenval; - if (blueval > blue_max[partition]) - blue_max[partition] = blueval; - if (blueval < blue_min[partition]) - blue_min[partition] = blueval; - } - } - for (i = 0; i < partition_count; i++) - { - if (red_min[i] >= red_max[i]) - { - red_min[i] = 0.0f; - red_max[i] = 1e-10f; - } - if (green_min[i] >= green_max[i]) - { - green_min[i] = 0.0f; - green_max[i] = 1e-10f; - } - if (blue_min[i] >= blue_max[i]) - { - blue_min[i] = 0.0f; - blue_max[i] = 1e-10f; - } - } -} - - - -void compute_partition_error_color_weightings(int xdim, int ydim, int zdim, const error_weight_block * ewb, const partition_info * pi, float4 error_weightings[4], float4 color_scalefactors[4]) -{ - int i; - int texels_per_block = xdim * ydim * zdim; - int pcnt = pi->partition_count; - for (i = 0; i < pcnt; i++) - error_weightings[i] = float4(1e-12f, 1e-12f, 1e-12f, 1e-12f); - for (i = 0; i < texels_per_block; i++) - { - int part = pi->partition_of_texel[i]; - error_weightings[part] = error_weightings[part] + ewb->error_weights[i]; - } - for (i = 0; i < pcnt; i++) - { - error_weightings[i] = error_weightings[i] * (1.0f / pi->texels_per_partition[i]); - } - for (i = 0; i < pcnt; i++) - { - color_scalefactors[i].x = sqrt(error_weightings[i].x); - color_scalefactors[i].y = sqrt(error_weightings[i].y); - color_scalefactors[i].z = sqrt(error_weightings[i].z); - color_scalefactors[i].w = sqrt(error_weightings[i].w); - } - -} - - -/* - main function to identify the best partitioning for a given number of texels */ - - -void find_best_partitionings(int partition_search_limit, int xdim, int ydim, int zdim, int partition_count, - const imageblock * pb, const error_weight_block * ewb, int candidates_to_return, - // best partitionings to use if the endpoint colors are assumed to be uncorrelated - int *best_partitions_uncorrellated, - // best partitionings to use if the endpoint colors have the same chroma - int *best_partitions_samechroma, - // best partitionings to use if using dual plane of weights - int *best_partitions_dual_weight_planes) -{ - - - int i, j; - - int texels_per_block = xdim * ydim * zdim; - - // constant used to estimate quantization error for a given partitioning; - // the optimal value for this constant depends on bitrate. - // These constants have been determined empirically. - - float weight_imprecision_estim = 100; - - if (texels_per_block <= 20) - weight_imprecision_estim = 0.03f; - else if (texels_per_block <= 31) - weight_imprecision_estim = 0.04f; - else if (texels_per_block <= 41) - weight_imprecision_estim = 0.05f; - else - weight_imprecision_estim = 0.055f; - - - int partition_sequence[PARTITION_COUNT]; - - kmeans_compute_partition_ordering(xdim, ydim, zdim, partition_count, pb, partition_sequence); - - - float weight_imprecision_estim_squared = weight_imprecision_estim * weight_imprecision_estim; - -#ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("weight_imprecision_estim = %g\n", weight_imprecision_estim); -#endif - - int uses_alpha = imageblock_uses_alpha(xdim, ydim, zdim, pb); - - const partition_info *ptab = get_partition_table(xdim, ydim, zdim, partition_count); - - // partitioning errors assuming uncorrelated-chrominance endpoints - float uncorr_errors[PARTITION_COUNT]; - // partitioning errors assuming same-chrominance endpoints - float samechroma_errors[PARTITION_COUNT]; - - // partitioning errors assuming that one of the color channels - // is uncorrelated from all the other ones - float separate_errors[4 * PARTITION_COUNT]; - - - float *separate_red_errors = separate_errors; - float *separate_green_errors = separate_errors + PARTITION_COUNT; - float *separate_blue_errors = separate_errors + 2 * PARTITION_COUNT; - float *separate_alpha_errors = separate_errors + 3 * PARTITION_COUNT; - - int defacto_search_limit = PARTITION_COUNT - 1; - - if (uses_alpha) - { - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partition testing with alpha, %d partitions\n\n", partition_count); - #endif - - for (i = 0; i < PARTITION_COUNT; i++) - { - int partition = partition_sequence[i]; - int bk_partition_count = ptab[partition].partition_count; - - if (bk_partition_count < partition_count) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partitioning %d-%d: invalid\n", partition_count, partition); - #endif - - uncorr_errors[i] = 1e35f; - samechroma_errors[i] = 1e35f; - separate_red_errors[i] = 1e35f; - separate_green_errors[i] = 1e35f; - separate_blue_errors[i] = 1e35f; - separate_alpha_errors[i] = 1e35f; - continue; - } - // the sentinel value for partitions above the search limit must be smaller - // than the sentinel value for invalid partitions - if (i >= partition_search_limit) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partitioning %d-%d: excluded from testing\n", partition_count, partition); - #endif - - defacto_search_limit = i; - - uncorr_errors[i] = 1e34f; - samechroma_errors[i] = 1e34f; - separate_red_errors[i] = 1e34f; - separate_green_errors[i] = 1e34f; - separate_blue_errors[i] = 1e34f; - separate_alpha_errors[i] = 1e34f; - break; - } - - // compute the weighting to give to each color channel - // in each partition. - float4 error_weightings[4]; - float4 color_scalefactors[4]; - float4 inverse_color_scalefactors[4]; - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, ptab + partition, error_weightings, color_scalefactors); - - for (j = 0; j < partition_count; j++) - { - inverse_color_scalefactors[j].x = 1.0f / MAX(color_scalefactors[j].x, 1e-7f); - inverse_color_scalefactors[j].y = 1.0f / MAX(color_scalefactors[j].y, 1e-7f); - inverse_color_scalefactors[j].z = 1.0f / MAX(color_scalefactors[j].z, 1e-7f); - inverse_color_scalefactors[j].w = 1.0f / MAX(color_scalefactors[j].w, 1e-7f); - } - - float4 averages[4]; - float4 directions_rgba[4]; - float3 directions_gba[4]; - float3 directions_rba[4]; - float3 directions_rga[4]; - float3 directions_rgb[4]; - - compute_averages_and_directions_rgba(ptab + partition, pb, ewb, color_scalefactors, averages, directions_rgba, directions_gba, directions_rba, directions_rga, directions_rgb); - - line4 uncorr_lines[4]; - line4 samechroma_lines[4]; - line3 separate_red_lines[4]; - line3 separate_green_lines[4]; - line3 separate_blue_lines[4]; - line3 separate_alpha_lines[4]; - - processed_line4 proc_uncorr_lines[4]; - processed_line4 proc_samechroma_lines[4]; - processed_line3 proc_separate_red_lines[4]; - processed_line3 proc_separate_green_lines[4]; - processed_line3 proc_separate_blue_lines[4]; - processed_line3 proc_separate_alpha_lines[4]; - - float uncorr_linelengths[4]; - float samechroma_linelengths[4]; - float separate_red_linelengths[4]; - float separate_green_linelengths[4]; - float separate_blue_linelengths[4]; - float separate_alpha_linelengths[4]; - - - - for (j = 0; j < partition_count; j++) - { - uncorr_lines[j].a = averages[j]; - if (dot(directions_rgba[j], directions_rgba[j]) == 0.0f) - uncorr_lines[j].b = normalize(float4(1, 1, 1, 1)); - else - uncorr_lines[j].b = normalize(directions_rgba[j]); - - proc_uncorr_lines[j].amod = (uncorr_lines[j].a - uncorr_lines[j].b * dot(uncorr_lines[j].a, uncorr_lines[j].b)) * inverse_color_scalefactors[j]; - proc_uncorr_lines[j].bs = (uncorr_lines[j].b * color_scalefactors[j]); - proc_uncorr_lines[j].bis = (uncorr_lines[j].b * inverse_color_scalefactors[j]); - - - samechroma_lines[j].a = float4(0, 0, 0, 0); - if (dot(averages[j], averages[j]) == 0) - samechroma_lines[j].b = normalize(float4(1, 1, 1, 1)); - else - samechroma_lines[j].b = normalize(averages[j]); - - proc_samechroma_lines[j].amod = (samechroma_lines[j].a - samechroma_lines[j].b * dot(samechroma_lines[j].a, samechroma_lines[j].b)) * inverse_color_scalefactors[j]; - proc_samechroma_lines[j].bs = (samechroma_lines[j].b * color_scalefactors[j]); - proc_samechroma_lines[j].bis = (samechroma_lines[j].b * inverse_color_scalefactors[j]); - - separate_red_lines[j].a = averages[j].yzw; - if (dot(directions_gba[j], directions_gba[j]) == 0.0f) - separate_red_lines[j].b = normalize(float3(1, 1, 1)); - else - separate_red_lines[j].b = normalize(directions_gba[j]); - - separate_green_lines[j].a = averages[j].xzw; - if (dot(directions_rba[j], directions_rba[j]) == 0.0f) - separate_green_lines[j].b = normalize(float3(1, 1, 1)); - else - separate_green_lines[j].b = normalize(directions_rba[j]); - - separate_blue_lines[j].a = averages[j].xyw; - if (dot(directions_rga[j], directions_rga[j]) == 0.0f) - separate_blue_lines[j].b = normalize(float3(1, 1, 1)); - else - separate_blue_lines[j].b = normalize(directions_rga[j]); - - separate_alpha_lines[j].a = averages[j].xyz; - if (dot(directions_rgb[j], directions_rgb[j]) == 0.0f) - separate_alpha_lines[j].b = normalize(float3(1, 1, 1)); - else - separate_alpha_lines[j].b = normalize(directions_rgb[j]); - - proc_separate_red_lines[j].amod = (separate_red_lines[j].a - separate_red_lines[j].b * dot(separate_red_lines[j].a, separate_red_lines[j].b)) * inverse_color_scalefactors[j].yzw; - proc_separate_red_lines[j].bs = (separate_red_lines[j].b * color_scalefactors[j].yzw); - proc_separate_red_lines[j].bis = (separate_red_lines[j].b * inverse_color_scalefactors[j].yzw); - - proc_separate_green_lines[j].amod = - (separate_green_lines[j].a - separate_green_lines[j].b * dot(separate_green_lines[j].a, separate_green_lines[j].b)) * inverse_color_scalefactors[j].xzw; - proc_separate_green_lines[j].bs = (separate_green_lines[j].b * color_scalefactors[j].xzw); - proc_separate_green_lines[j].bis = (separate_green_lines[j].b * inverse_color_scalefactors[j].xzw); - - proc_separate_blue_lines[j].amod = (separate_blue_lines[j].a - separate_blue_lines[j].b * dot(separate_blue_lines[j].a, separate_blue_lines[j].b)) * inverse_color_scalefactors[j].xyw; - proc_separate_blue_lines[j].bs = (separate_blue_lines[j].b * color_scalefactors[j].xyw); - proc_separate_blue_lines[j].bis = (separate_blue_lines[j].b * inverse_color_scalefactors[j].xyw); - - proc_separate_alpha_lines[j].amod = - (separate_alpha_lines[j].a - separate_alpha_lines[j].b * dot(separate_alpha_lines[j].a, separate_alpha_lines[j].b)) * inverse_color_scalefactors[j].xyz; - proc_separate_alpha_lines[j].bs = (separate_alpha_lines[j].b * color_scalefactors[j].xyz); - proc_separate_alpha_lines[j].bis = (separate_alpha_lines[j].b * inverse_color_scalefactors[j].xyz); - - } - - float uncorr_error = compute_error_squared_rgba(ptab + partition, - pb, - ewb, - proc_uncorr_lines, - uncorr_linelengths); - float samechroma_error = compute_error_squared_rgba(ptab + partition, - pb, - ewb, - proc_samechroma_lines, - samechroma_linelengths); - - - float separate_red_error = compute_error_squared_gba(ptab + partition, - pb, - ewb, - proc_separate_red_lines, - separate_red_linelengths); - - float separate_green_error = compute_error_squared_rba(ptab + partition, - pb, - ewb, - proc_separate_green_lines, - separate_green_linelengths); - - float separate_blue_error = compute_error_squared_rga(ptab + partition, - pb, - ewb, - proc_separate_blue_lines, - separate_blue_linelengths); - - float separate_alpha_error = compute_error_squared_rgb(ptab + partition, - pb, - ewb, - proc_separate_alpha_lines, - separate_alpha_linelengths); - - // compute minimum & maximum alpha values in each partition - float red_min[4], red_max[4]; - float green_min[4], green_max[4]; - float blue_min[4], blue_max[4]; - float alpha_min[4], alpha_max[4]; - compute_alpha_minmax(xdim, ydim, zdim, ptab + partition, pb, ewb, alpha_min, alpha_max); - - compute_rgb_minmax(xdim, ydim, zdim, ptab + partition, pb, ewb, red_min, red_max, green_min, green_max, blue_min, blue_max); - - /* - Compute an estimate of error introduced by weight quantization imprecision. - This error is computed as follows, for each partition - 1: compute the principal-axis vector (full length) in error-space - 2: convert the principal-axis vector to regular RGB-space - 3: scale the vector by a constant that estimates average quantization error - 4: for each texel, square the vector, then do a dot-product with the texel's error weight; - sum up the results across all texels. - 4(optimized): square the vector once, then do a dot-product with the average texel error, - then multiply by the number of texels. - */ - - for (j = 0; j < partition_count; j++) - { - float tpp = (float)(ptab[partition].texels_per_partition[j]); - - float4 ics = inverse_color_scalefactors[j]; - float4 error_weights = error_weightings[j] * (tpp * weight_imprecision_estim_squared); - - float4 uncorr_vector = (uncorr_lines[j].b * uncorr_linelengths[j]) * ics; - float4 samechroma_vector = (samechroma_lines[j].b * samechroma_linelengths[j]) * ics; - float3 separate_red_vector = (separate_red_lines[j].b * separate_red_linelengths[j]) * ics.yzw; - float3 separate_green_vector = (separate_green_lines[j].b * separate_green_linelengths[j]) * ics.xzw; - float3 separate_blue_vector = (separate_blue_lines[j].b * separate_blue_linelengths[j]) * ics.xyw; - float3 separate_alpha_vector = (separate_alpha_lines[j].b * separate_alpha_linelengths[j]) * ics.xyz; - - uncorr_vector = uncorr_vector * uncorr_vector; - samechroma_vector = samechroma_vector * samechroma_vector; - separate_red_vector = separate_red_vector * separate_red_vector; - separate_green_vector = separate_green_vector * separate_green_vector; - separate_blue_vector = separate_blue_vector * separate_blue_vector; - separate_alpha_vector = separate_alpha_vector * separate_alpha_vector; - - uncorr_error += dot(uncorr_vector, error_weights); - samechroma_error += dot(samechroma_vector, error_weights); - separate_red_error += dot(separate_red_vector, error_weights.yzw); - separate_green_error += dot(separate_green_vector, error_weights.xzw); - separate_blue_error += dot(separate_blue_vector, error_weights.xyw); - separate_alpha_error += dot(separate_alpha_vector, error_weights.xyz); - - float red_scalar = (red_max[j] - red_min[j]); - float green_scalar = (green_max[j] - green_min[j]); - float blue_scalar = (blue_max[j] - blue_min[j]); - float alpha_scalar = (alpha_max[j] - alpha_min[j]); - red_scalar *= red_scalar; - green_scalar *= green_scalar; - blue_scalar *= blue_scalar; - alpha_scalar *= alpha_scalar; - separate_red_error += red_scalar * error_weights.x; - separate_green_error += green_scalar * error_weights.y; - separate_blue_error += blue_scalar * error_weights.z; - separate_alpha_error += alpha_scalar * error_weights.w; - } - - uncorr_errors[i] = uncorr_error; - samechroma_errors[i] = samechroma_error; - separate_red_errors[i] = separate_red_error; - separate_green_errors[i] = separate_green_error; - separate_blue_errors[i] = separate_blue_error; - separate_alpha_errors[i] = separate_alpha_error; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partitioning %d-%d errors: uncorr=%g, samechroma=%g, sep-alpha=%g\n", partition_count, i, uncorr_error, samechroma_error, separate_alpha_error); - #endif - } - } - else - { - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partition testing without alpha, %d partitions\n", partition_count); - #endif - - - - for (i = 0; i < PARTITION_COUNT; i++) - { - - int partition = partition_sequence[i]; - - int bk_partition_count = ptab[partition].partition_count; - if (bk_partition_count < partition_count) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partitioning %d-%d: invalid\n", partition_count, i); - #endif - - uncorr_errors[i] = 1e35f; - samechroma_errors[i] = 1e35f; - separate_red_errors[i] = 1e35f; - separate_green_errors[i] = 1e35f; - separate_blue_errors[i] = 1e35f; - continue; - } - // the sentinel value for valid partitions above the search limit must be smaller - // than the sentinel value for invalid partitions - if (i >= partition_search_limit) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf(" Partitioning %d-%d: excluded from testing\n", partition_count, partition); - #endif - - defacto_search_limit = i; - uncorr_errors[i] = 1e34f; - samechroma_errors[i] = 1e34f; - separate_red_errors[i] = 1e34f; - separate_green_errors[i] = 1e34f; - separate_blue_errors[i] = 1e34f; - break; - - } - - // compute the weighting to give to each color channel - // in each partition. - float4 error_weightings[4]; - float4 color_scalefactors[4]; - float4 inverse_color_scalefactors[4]; - - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, ptab + partition, error_weightings, color_scalefactors); - - for (j = 0; j < partition_count; j++) - { - inverse_color_scalefactors[j].x = 1.0f / MAX(color_scalefactors[j].x, 1e-7f); - inverse_color_scalefactors[j].y = 1.0f / MAX(color_scalefactors[j].y, 1e-7f); - inverse_color_scalefactors[j].z = 1.0f / MAX(color_scalefactors[j].z, 1e-7f); - inverse_color_scalefactors[j].w = 1.0f / MAX(color_scalefactors[j].w, 1e-7f); - } - - float3 averages[4]; - float3 directions_rgb[4]; - float2 directions_rg[4]; - float2 directions_rb[4]; - float2 directions_gb[4]; - - compute_averages_and_directions_rgb(ptab + partition, pb, ewb, color_scalefactors, averages, directions_rgb, directions_rg, directions_rb, directions_gb); - - line3 uncorr_lines[4]; - line3 samechroma_lines[4]; - line2 separate_red_lines[4]; - line2 separate_green_lines[4]; - line2 separate_blue_lines[4]; - - processed_line3 proc_uncorr_lines[4]; - processed_line3 proc_samechroma_lines[4]; - - processed_line2 proc_separate_red_lines[4]; - processed_line2 proc_separate_green_lines[4]; - processed_line2 proc_separate_blue_lines[4]; - - float uncorr_linelengths[4]; - float samechroma_linelengths[4]; - float separate_red_linelengths[4]; - float separate_green_linelengths[4]; - float separate_blue_linelengths[4]; - - for (j = 0; j < partition_count; j++) - { - uncorr_lines[j].a = averages[j]; - if (dot(directions_rgb[j], directions_rgb[j]) == 0.0f) - uncorr_lines[j].b = normalize(float3(1, 1, 1)); - else - uncorr_lines[j].b = normalize(directions_rgb[j]); - - - samechroma_lines[j].a = float3(0, 0, 0); - - if (dot(averages[j], averages[j]) == 0.0f) - samechroma_lines[j].b = normalize(float3(1, 1, 1)); - else - samechroma_lines[j].b = normalize(averages[j]); - - proc_uncorr_lines[j].amod = (uncorr_lines[j].a - uncorr_lines[j].b * dot(uncorr_lines[j].a, uncorr_lines[j].b)) * inverse_color_scalefactors[j].xyz; - proc_uncorr_lines[j].bs = (uncorr_lines[j].b * color_scalefactors[j].xyz); - proc_uncorr_lines[j].bis = (uncorr_lines[j].b * inverse_color_scalefactors[j].xyz); - - proc_samechroma_lines[j].amod = (samechroma_lines[j].a - samechroma_lines[j].b * dot(samechroma_lines[j].a, samechroma_lines[j].b)) * inverse_color_scalefactors[j].xyz; - proc_samechroma_lines[j].bs = (samechroma_lines[j].b * color_scalefactors[j].xyz); - proc_samechroma_lines[j].bis = (samechroma_lines[j].b * inverse_color_scalefactors[j].xyz); - - separate_red_lines[j].a = averages[j].yz; - if (dot(directions_gb[j], directions_gb[j]) == 0.0f) - separate_red_lines[j].b = normalize(float2(1, 1)); - else - separate_red_lines[j].b = normalize(directions_gb[j]); - - separate_green_lines[j].a = averages[j].xz; - if (dot(directions_rb[j], directions_rb[j]) == 0.0f) - separate_green_lines[j].b = normalize(float2(1, 1)); - else - separate_green_lines[j].b = normalize(directions_rb[j]); - - separate_blue_lines[j].a = averages[j].xy; - if (dot(directions_rg[j], directions_rg[j]) == 0.0f) - separate_blue_lines[j].b = normalize(float2(1, 1)); - else - separate_blue_lines[j].b = normalize(directions_rg[j]); - - proc_separate_red_lines[j].amod = (separate_red_lines[j].a - separate_red_lines[j].b * dot(separate_red_lines[j].a, separate_red_lines[j].b)) * inverse_color_scalefactors[j].yz; - proc_separate_red_lines[j].bs = (separate_red_lines[j].b * color_scalefactors[j].yz); - proc_separate_red_lines[j].bis = (separate_red_lines[j].b * inverse_color_scalefactors[j].yz); - - proc_separate_green_lines[j].amod = - (separate_green_lines[j].a - separate_green_lines[j].b * dot(separate_green_lines[j].a, separate_green_lines[j].b)) * inverse_color_scalefactors[j].xz; - proc_separate_green_lines[j].bs = (separate_green_lines[j].b * color_scalefactors[j].xz); - proc_separate_green_lines[j].bis = (separate_green_lines[j].b * inverse_color_scalefactors[j].xz); - - proc_separate_blue_lines[j].amod = (separate_blue_lines[j].a - separate_blue_lines[j].b * dot(separate_blue_lines[j].a, separate_blue_lines[j].b)) * inverse_color_scalefactors[j].xy; - proc_separate_blue_lines[j].bs = (separate_blue_lines[j].b * color_scalefactors[j].xy); - proc_separate_blue_lines[j].bis = (separate_blue_lines[j].b * inverse_color_scalefactors[j].xy); - - } - - float uncorr_error = compute_error_squared_rgb(ptab + partition, - pb, - ewb, - proc_uncorr_lines, - uncorr_linelengths); - float samechroma_error = compute_error_squared_rgb(ptab + partition, - pb, - ewb, - proc_samechroma_lines, - samechroma_linelengths); - - float separate_red_error = compute_error_squared_gb(ptab + partition, - pb, - ewb, - proc_separate_red_lines, - separate_red_linelengths); - - float separate_green_error = compute_error_squared_rb(ptab + partition, - pb, - ewb, - proc_separate_green_lines, - separate_green_linelengths); - - float separate_blue_error = compute_error_squared_rg(ptab + partition, - pb, - ewb, - proc_separate_blue_lines, - separate_blue_linelengths); - - float red_min[4], red_max[4]; - float green_min[4], green_max[4]; - float blue_min[4], blue_max[4]; - - - compute_rgb_minmax(xdim, ydim, zdim, ptab + partition, pb, ewb, red_min, red_max, green_min, green_max, blue_min, blue_max); - - - - /* - compute an estimate of error introduced by weight imprecision. - This error is computed as follows, for each partition - 1: compute the principal-axis vector (full length) in error-space - 2: convert the principal-axis vector to regular RGB-space - 3: scale the vector by a constant that estimates average quantization error. - 4: for each texel, square the vector, then do a dot-product with the texel's error weight; - sum up the results across all texels. - 4(optimized): square the vector once, then do a dot-product with the average texel error, - then multiply by the number of texels. - */ - - - for (j = 0; j < partition_count; j++) - { - float tpp = (float)(ptab[partition].texels_per_partition[j]); - - float3 ics = inverse_color_scalefactors[j].xyz; - float3 error_weights = error_weightings[j].xyz * (tpp * weight_imprecision_estim_squared); - - float3 uncorr_vector = (uncorr_lines[j].b * uncorr_linelengths[j]) * ics; - float3 samechroma_vector = (samechroma_lines[j].b * samechroma_linelengths[j]) * ics; - - float2 separate_red_vector = (separate_red_lines[j].b * separate_red_linelengths[j]) * ics.yz; - float2 separate_green_vector = (separate_green_lines[j].b * separate_green_linelengths[j]) * ics.xz; - float2 separate_blue_vector = (separate_blue_lines[j].b * separate_blue_linelengths[j]) * ics.xy; - - uncorr_vector = uncorr_vector * uncorr_vector; - samechroma_vector = samechroma_vector * samechroma_vector; - separate_red_vector = separate_red_vector * separate_red_vector; - separate_green_vector = separate_green_vector * separate_green_vector; - separate_blue_vector = separate_blue_vector * separate_blue_vector; - - uncorr_error += dot(uncorr_vector, error_weights); - samechroma_error += dot(samechroma_vector, error_weights); - separate_red_error += dot(separate_red_vector, error_weights.yz); - separate_green_error += dot(separate_green_vector, error_weights.xz); - separate_blue_error += dot(separate_blue_vector, error_weights.xy); - - float red_scalar = (red_max[j] - red_min[j]); - float green_scalar = (green_max[j] - green_min[j]); - float blue_scalar = (blue_max[j] - blue_min[j]); - - red_scalar *= red_scalar; - green_scalar *= green_scalar; - blue_scalar *= blue_scalar; - - separate_red_error += red_scalar * error_weights.x; - separate_green_error += green_scalar * error_weights.y; - separate_blue_error += blue_scalar * error_weights.z; - } - - - uncorr_errors[i] = uncorr_error; - samechroma_errors[i] = samechroma_error; - - separate_red_errors[i] = separate_red_error; - separate_green_errors[i] = separate_green_error; - separate_blue_errors[i] = separate_blue_error; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Partitioning %d-%d errors: uncorr=%f, samechroma=%f, sep-red=%f, sep-green=%f, sep-blue=%f\n", - partition_count, partition, uncorr_error, samechroma_error, separate_red_error, separate_green_error, separate_blue_error); - #endif - } - } - - - for (i = 0; i < candidates_to_return; i++) - { - int best_uncorr_partition = 0; - int best_samechroma_partition = 0; - float best_uncorr_error = 1e30f; - float best_samechroma_error = 1e30f; - for (j = 0; j <= defacto_search_limit; j++) - { - if (uncorr_errors[j] < best_uncorr_error) - { - best_uncorr_partition = j; - best_uncorr_error = uncorr_errors[j]; - } - } - best_partitions_uncorrellated[i] = partition_sequence[best_uncorr_partition]; - uncorr_errors[best_uncorr_partition] = 1e30f; - samechroma_errors[best_uncorr_partition] = 1e30f; - - for (j = 0; j <= defacto_search_limit; j++) - { - if (samechroma_errors[j] < best_samechroma_error) - { - best_samechroma_partition = j; - best_samechroma_error = samechroma_errors[j]; - } - } - best_partitions_samechroma[i] = partition_sequence[best_samechroma_partition]; - samechroma_errors[best_samechroma_partition] = 1e30f; - uncorr_errors[best_samechroma_partition] = 1e30f; - } - - for (i = 0; i < 2 * candidates_to_return; i++) - { - int best_partition = 0; - float best_partition_error = 1e30f; - - for (j = 0; j <= defacto_search_limit; j++) - { - if (1 || !uses_alpha) - { - if (separate_errors[j] < best_partition_error) - { - best_partition = j; - best_partition_error = separate_errors[j]; - } - if (separate_errors[j + PARTITION_COUNT] < best_partition_error) - { - best_partition = j + PARTITION_COUNT; - best_partition_error = separate_errors[j + PARTITION_COUNT]; - } - if (separate_errors[j + 2 * PARTITION_COUNT] < best_partition_error) - { - best_partition = j + 2 * PARTITION_COUNT; - best_partition_error = separate_errors[j + 2 * PARTITION_COUNT]; - } - } - if (uses_alpha) - { - if (separate_errors[j + 3 * PARTITION_COUNT] < best_partition_error) - { - best_partition = j + 3 * PARTITION_COUNT; - best_partition_error = separate_errors[j + 3 * PARTITION_COUNT]; - } - } - } - - separate_errors[best_partition] = 1e30f; - best_partition = ((best_partition >> PARTITION_BITS) << PARTITION_BITS) | partition_sequence[best_partition & (PARTITION_COUNT - 1)]; - best_partitions_dual_weight_planes[i] = best_partition; - } - -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_ideal_endpoints_and_weights.cpp b/3rdparty/bimg/3rdparty/astc/astc_ideal_endpoints_and_weights.cpp deleted file mode 100644 index 89d697d136f..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_ideal_endpoints_and_weights.cpp +++ /dev/null @@ -1,2163 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions for computing color endpoints and texel weights. - */ -/*----------------------------------------------------------------------------*/ - -#include <math.h> - -#include "astc_codec_internals.h" - -#ifdef DEBUG_PRINT_DIAGNOSTICS -#include <stdio.h> -#endif - - -#ifdef DEBUG_CAPTURE_NAN - #ifndef _GNU_SOURCE - #define _GNU_SOURCE - #endif - - #include <fenv.h> -#endif - -static void compute_endpoints_and_ideal_weights_1_component(int xdim, int ydim, int zdim, - const partition_info * pt, const imageblock * blk, - const error_weight_block * ewb, endpoints_and_weights * ei, - int component) -{ - int i; - - int partition_count = pt->partition_count; - ei->ep.partition_count = partition_count; - - float lowvalues[4], highvalues[4]; - float partition_error_scale[4]; - float linelengths_rcp[4]; - - int texels_per_block = xdim * ydim * zdim; - - const float *error_weights; - switch (component) - { - case 0: - error_weights = ewb->texel_weight_r; - break; - case 1: - error_weights = ewb->texel_weight_g; - break; - case 2: - error_weights = ewb->texel_weight_b; - break; - case 3: - error_weights = ewb->texel_weight_a; - break; - default: - error_weights = ewb->texel_weight_r; - ASTC_CODEC_INTERNAL_ERROR; - } - - - for (i = 0; i < partition_count; i++) - { - lowvalues[i] = 1e10; - highvalues[i] = -1e10; - } - - for (i = 0; i < texels_per_block; i++) - { - if (error_weights[i] > 1e-10) - { - float value = blk->work_data[4 * i + component]; - int partition = pt->partition_of_texel[i]; - if (value < lowvalues[partition]) - lowvalues[partition] = value; - if (value > highvalues[partition]) - highvalues[partition] = value; - } - } - - for (i = 0; i < partition_count; i++) - { - float diff = highvalues[i] - lowvalues[i]; - if (diff < 0) - { - lowvalues[i] = 0; - highvalues[i] = 0; - } - if (diff < 1e-7f) - diff = 1e-7f; - partition_error_scale[i] = diff * diff; - linelengths_rcp[i] = 1.0f / diff; - } - - for (i = 0; i < texels_per_block; i++) - { - float value = blk->work_data[4 * i + component]; - int partition = pt->partition_of_texel[i]; - value -= lowvalues[partition]; - value *= linelengths_rcp[partition]; - if (value > 1.0f) - value = 1.0f; - else if (!(value > 0.0f)) - value = 0.0f; - - ei->weights[i] = value; - ei->weight_error_scale[i] = partition_error_scale[partition] * error_weights[i]; - if (astc_isnan(ei->weight_error_scale[i])) - { - ASTC_CODEC_INTERNAL_ERROR; - } - } - - for (i = 0; i < partition_count; i++) - { - ei->ep.endpt0[i] = float4(blk->red_min, blk->green_min, blk->blue_min, blk->alpha_min); - ei->ep.endpt1[i] = float4(blk->red_max, blk->green_max, blk->blue_max, blk->alpha_max); - switch (component) - { - case 0: // red/x - ei->ep.endpt0[i].x = lowvalues[i]; - ei->ep.endpt1[i].x = highvalues[i]; - break; - case 1: // green/y - ei->ep.endpt0[i].y = lowvalues[i]; - ei->ep.endpt1[i].y = highvalues[i]; - break; - case 2: // blue/z - ei->ep.endpt0[i].z = lowvalues[i]; - ei->ep.endpt1[i].z = highvalues[i]; - break; - case 3: // alpha/w - ei->ep.endpt0[i].w = lowvalues[i]; - ei->ep.endpt1[i].w = highvalues[i]; - break; - } - } - - // print all the data that this function computes. - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s: %dx%dx%d texels, %d partitions, component=%d\n", __func__, xdim, ydim, zdim, partition_count, component); - printf("Endpoints:\n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low: <%g> => <%g %g %g %g>\n", i, lowvalues[i], ei->ep.endpt0[i].x, ei->ep.endpt0[i].y, ei->ep.endpt0[i].z, ei->ep.endpt0[i].w); - printf("%d High: <%g> => <%g %g %g %g>\n", i, highvalues[i], ei->ep.endpt1[i].x, ei->ep.endpt1[i].y, ei->ep.endpt1[i].z, ei->ep.endpt1[i].w); - } - printf("Ideal-weights:\n"); - - for (i = 0; i < texels_per_block; i++) - { - printf("%3d <%2d %2d %2d>=> %g (weight=%g)\n", i, i % xdim, (i / xdim) % ydim, i / (xdim * ydim), ei->weights[i], ei->weight_error_scale[i]); - } - printf("\n"); - } - #endif -} - - -static void compute_endpoints_and_ideal_weights_2_components(int xdim, int ydim, int zdim, const partition_info * pt, - const imageblock * blk, const error_weight_block * ewb, - endpoints_and_weights * ei, int component1, int component2) -{ - int i; - - int partition_count = pt->partition_count; - ei->ep.partition_count = partition_count; - - float4 error_weightings[4]; - float4 color_scalefactors[4]; - - float2 scalefactors[4]; - - const float *error_weights; - if (component1 == 0 && component2 == 1) - error_weights = ewb->texel_weight_rg; - else if (component1 == 0 && component2 == 2) - error_weights = ewb->texel_weight_rb; - else if (component1 == 1 && component2 == 2) - error_weights = ewb->texel_weight_gb; - else - { - error_weights = ewb->texel_weight_rg; - ASTC_CODEC_INTERNAL_ERROR; - } - - int texels_per_block = xdim * ydim * zdim; - - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pt, error_weightings, color_scalefactors); - - for (i = 0; i < partition_count; i++) - { - float s1 = 0, s2 = 0; - switch (component1) - { - case 0: - s1 = color_scalefactors[i].x; - break; - case 1: - s1 = color_scalefactors[i].y; - break; - case 2: - s1 = color_scalefactors[i].z; - break; - case 3: - s1 = color_scalefactors[i].w; - break; - } - - switch (component2) - { - case 0: - s2 = color_scalefactors[i].x; - break; - case 1: - s2 = color_scalefactors[i].y; - break; - case 2: - s2 = color_scalefactors[i].z; - break; - case 3: - s2 = color_scalefactors[i].w; - break; - } - scalefactors[i] = normalize(float2(s1, s2)) * 1.41421356f; - } - - - float lowparam[4], highparam[4]; - - float2 averages[4]; - float2 directions[4]; - - line2 lines[4]; - float scale[4]; - float length_squared[4]; - - - for (i = 0; i < partition_count; i++) - { - lowparam[i] = 1e10; - highparam[i] = -1e10; - } - - - compute_averages_and_directions_2_components(pt, blk, ewb, scalefactors, component1, component2, averages, directions); - - for (i = 0; i < partition_count; i++) - { - float2 egv = directions[i]; - if (egv.x + egv.y < 0.0f) - directions[i] = float2(0, 0) - egv; - } - - for (i = 0; i < partition_count; i++) - { - lines[i].a = averages[i]; - if (dot(directions[i], directions[i]) == 0.0f) - lines[i].b = normalize(float2(1, 1)); - else - lines[i].b = normalize(directions[i]); - } - - - for (i = 0; i < texels_per_block; i++) - { - if (error_weights[i] > 1e-10) - { - int partition = pt->partition_of_texel[i]; - float2 point = float2(blk->work_data[4 * i + component1], blk->work_data[4 * i + component2]) * scalefactors[partition]; - line2 l = lines[partition]; - float param = dot(point - l.a, l.b); - ei->weights[i] = param; - if (param < lowparam[partition]) - lowparam[partition] = param; - if (param > highparam[partition]) - highparam[partition] = param; - } - else - { - ei->weights[i] = -1e38f; - } - } - - float2 lowvalues[4]; - float2 highvalues[4]; - - - for (i = 0; i < partition_count; i++) - { - float length = highparam[i] - lowparam[i]; - if (length < 0) // case for when none of the texels had any weight - { - lowparam[i] = 0.0f; - highparam[i] = 1e-7f; - } - - // it is possible for a uniform-color partition to produce length=0; this - // causes NaN-production and NaN-propagation later on. Set length to - // a small value to avoid this problem. - if (length < 1e-7f) - length = 1e-7f; - - length_squared[i] = length * length; - scale[i] = 1.0f / length; - - float2 ep0 = lines[i].a + lines[i].b * lowparam[i]; - float2 ep1 = lines[i].a + lines[i].b * highparam[i]; - - ep0 = ep0 / scalefactors[i]; - ep1 = ep1 / scalefactors[i]; - - lowvalues[i] = ep0; - highvalues[i] = ep1; - } - - - for (i = 0; i < partition_count; i++) - { - ei->ep.endpt0[i] = float4(blk->red_min, blk->green_min, blk->blue_min, blk->alpha_min); - ei->ep.endpt1[i] = float4(blk->red_max, blk->green_max, blk->blue_max, blk->alpha_max); - - float2 ep0 = lowvalues[i]; - float2 ep1 = highvalues[i]; - - switch (component1) - { - case 0: - ei->ep.endpt0[i].x = ep0.x; - ei->ep.endpt1[i].x = ep1.x; - break; - case 1: - ei->ep.endpt0[i].y = ep0.x; - ei->ep.endpt1[i].y = ep1.x; - break; - case 2: - ei->ep.endpt0[i].z = ep0.x; - ei->ep.endpt1[i].z = ep1.x; - break; - case 3: - ei->ep.endpt0[i].w = ep0.x; - ei->ep.endpt1[i].w = ep1.x; - break; - } - switch (component2) - { - case 0: - ei->ep.endpt0[i].x = ep0.y; - ei->ep.endpt1[i].x = ep1.y; - break; - case 1: - ei->ep.endpt0[i].y = ep0.y; - ei->ep.endpt1[i].y = ep1.y; - break; - case 2: - ei->ep.endpt0[i].z = ep0.y; - ei->ep.endpt1[i].z = ep1.y; - break; - case 3: - ei->ep.endpt0[i].w = ep0.y; - ei->ep.endpt1[i].w = ep1.y; - break; - } - } - - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - float idx = (ei->weights[i] - lowparam[partition]) * scale[partition]; - if (idx > 1.0f) - idx = 1.0f; - else if (!(idx > 0.0f)) - idx = 0.0f; - - ei->weights[i] = idx; - ei->weight_error_scale[i] = length_squared[partition] * error_weights[i]; - if (astc_isnan(ei->weight_error_scale[i])) - { - ASTC_CODEC_INTERNAL_ERROR; - } - } - - // print all the data that this function computes. - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s: %dx%dx%d texels, %d partitions, component1=%d, component2=%d\n", __func__, xdim, ydim, zdim, partition_count, component1, component2); - printf("Endpoints:\n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low: <%g %g> => <%g %g %g %g>\n", i, lowvalues[i].x, lowvalues[i].y, ei->ep.endpt0[i].x, ei->ep.endpt0[i].y, ei->ep.endpt0[i].z, ei->ep.endpt0[i].w); - printf("%d High: <%g %g> => <%g %g %g %g>\n", i, highvalues[i].x, highvalues[i].y, ei->ep.endpt1[i].x, ei->ep.endpt1[i].y, ei->ep.endpt1[i].z, ei->ep.endpt1[i].w); - } - printf("Ideal-weights:\n"); - - for (i = 0; i < texels_per_block; i++) - { - printf("%3d <%2d %2d %2d>=> %g (weight=%g)\n", i, i % xdim, (i / xdim) % ydim, i / (xdim * ydim), ei->weights[i], ei->weight_error_scale[i]); - } - printf("\n"); - } - #endif -} - -static void compute_endpoints_and_ideal_weights_3_components(int xdim, int ydim, int zdim, const partition_info * pt, - const imageblock * blk, const error_weight_block * ewb, - endpoints_and_weights * ei, int component1, int component2, int component3) -{ - int i; - - int partition_count = pt->partition_count; - ei->ep.partition_count = partition_count; - - float4 error_weightings[4]; - float4 color_scalefactors[4]; - - float3 scalefactors[4]; - - int texels_per_block = xdim * ydim * zdim; - - const float *error_weights; - if (component1 == 1 && component2 == 2 && component3 == 3) - error_weights = ewb->texel_weight_gba; - else if (component1 == 0 && component2 == 2 && component3 == 3) - error_weights = ewb->texel_weight_rba; - else if (component1 == 0 && component2 == 1 && component3 == 3) - error_weights = ewb->texel_weight_rga; - else if (component1 == 0 && component2 == 1 && component3 == 2) - error_weights = ewb->texel_weight_rgb; - else - { - error_weights = ewb->texel_weight_gba; - ASTC_CODEC_INTERNAL_ERROR; - } - - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pt, error_weightings, color_scalefactors); - - for (i = 0; i < partition_count; i++) - { - float s1 = 0, s2 = 0, s3 = 0; - switch (component1) - { - case 0: - s1 = color_scalefactors[i].x; - break; - case 1: - s1 = color_scalefactors[i].y; - break; - case 2: - s1 = color_scalefactors[i].z; - break; - case 3: - s1 = color_scalefactors[i].w; - break; - } - - switch (component2) - { - case 0: - s2 = color_scalefactors[i].x; - break; - case 1: - s2 = color_scalefactors[i].y; - break; - case 2: - s2 = color_scalefactors[i].z; - break; - case 3: - s2 = color_scalefactors[i].w; - break; - } - - switch (component3) - { - case 0: - s3 = color_scalefactors[i].x; - break; - case 1: - s3 = color_scalefactors[i].y; - break; - case 2: - s3 = color_scalefactors[i].z; - break; - case 3: - s3 = color_scalefactors[i].w; - break; - } - scalefactors[i] = normalize(float3(s1, s2, s3)) * 1.73205080f; - } - - - float lowparam[4], highparam[4]; - - float3 averages[4]; - float3 directions[4]; - - line3 lines[4]; - float scale[4]; - float length_squared[4]; - - - for (i = 0; i < partition_count; i++) - { - lowparam[i] = 1e10; - highparam[i] = -1e10; - } - - compute_averages_and_directions_3_components(pt, blk, ewb, scalefactors, component1, component2, component3, averages, directions); - - for (i = 0; i < partition_count; i++) - { - float3 direc = directions[i]; - if (direc.x + direc.y + direc.z < 0.0f) - directions[i] = float3(0, 0, 0) - direc; - } - - for (i = 0; i < partition_count; i++) - { - lines[i].a = averages[i]; - if (dot(directions[i], directions[i]) == 0.0f) - lines[i].b = normalize(float3(1, 1, 1)); - else - lines[i].b = normalize(directions[i]); - } - - - for (i = 0; i < texels_per_block; i++) - { - if (error_weights[i] > 1e-10) - { - int partition = pt->partition_of_texel[i]; - float3 point = float3(blk->work_data[4 * i + component1], blk->work_data[4 * i + component2], blk->work_data[4 * i + component3]) * scalefactors[partition]; - line3 l = lines[partition]; - float param = dot(point - l.a, l.b); - ei->weights[i] = param; - if (param < lowparam[partition]) - lowparam[partition] = param; - if (param > highparam[partition]) - highparam[partition] = param; - } - else - { - ei->weights[i] = -1e38f; - } - } - - float3 lowvalues[4]; - float3 highvalues[4]; - - - for (i = 0; i < partition_count; i++) - { - float length = highparam[i] - lowparam[i]; - if (length < 0) // case for when none of the texels had any weight - { - lowparam[i] = 0.0f; - highparam[i] = 1e-7f; - } - - // it is possible for a uniform-color partition to produce length=0; this - // causes NaN-production and NaN-propagation later on. Set length to - // a small value to avoid this problem. - if (length < 1e-7f) - length = 1e-7f; - - length_squared[i] = length * length; - scale[i] = 1.0f / length; - - float3 ep0 = lines[i].a + lines[i].b * lowparam[i]; - float3 ep1 = lines[i].a + lines[i].b * highparam[i]; - - ep0 = ep0 / scalefactors[i]; - ep1 = ep1 / scalefactors[i]; - - - lowvalues[i] = ep0; - highvalues[i] = ep1; - } - - - for (i = 0; i < partition_count; i++) - { - ei->ep.endpt0[i] = float4(blk->red_min, blk->green_min, blk->blue_min, blk->alpha_min); - ei->ep.endpt1[i] = float4(blk->red_max, blk->green_max, blk->blue_max, blk->alpha_max); - - - float3 ep0 = lowvalues[i]; - float3 ep1 = highvalues[i]; - - switch (component1) - { - case 0: - ei->ep.endpt0[i].x = ep0.x; - ei->ep.endpt1[i].x = ep1.x; - break; - case 1: - ei->ep.endpt0[i].y = ep0.x; - ei->ep.endpt1[i].y = ep1.x; - break; - case 2: - ei->ep.endpt0[i].z = ep0.x; - ei->ep.endpt1[i].z = ep1.x; - break; - case 3: - ei->ep.endpt0[i].w = ep0.x; - ei->ep.endpt1[i].w = ep1.x; - break; - } - switch (component2) - { - case 0: - ei->ep.endpt0[i].x = ep0.y; - ei->ep.endpt1[i].x = ep1.y; - break; - case 1: - ei->ep.endpt0[i].y = ep0.y; - ei->ep.endpt1[i].y = ep1.y; - break; - case 2: - ei->ep.endpt0[i].z = ep0.y; - ei->ep.endpt1[i].z = ep1.y; - break; - case 3: - ei->ep.endpt0[i].w = ep0.y; - ei->ep.endpt1[i].w = ep1.y; - break; - } - switch (component3) - { - case 0: - ei->ep.endpt0[i].x = ep0.z; - ei->ep.endpt1[i].x = ep1.z; - break; - case 1: - ei->ep.endpt0[i].y = ep0.z; - ei->ep.endpt1[i].y = ep1.z; - break; - case 2: - ei->ep.endpt0[i].z = ep0.z; - ei->ep.endpt1[i].z = ep1.z; - break; - case 3: - ei->ep.endpt0[i].w = ep0.z; - ei->ep.endpt1[i].w = ep1.z; - break; - } - } - - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - float idx = (ei->weights[i] - lowparam[partition]) * scale[partition]; - if (idx > 1.0f) - idx = 1.0f; - else if (!(idx > 0.0f)) - idx = 0.0f; - - ei->weights[i] = idx; - ei->weight_error_scale[i] = length_squared[partition] * error_weights[i]; - if (astc_isnan(ei->weight_error_scale[i])) - { - ASTC_CODEC_INTERNAL_ERROR; - } - } - - // print all the data that this function computes. - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s: %dx%dx%d texels, %d partitions, component1=%d, component2=%d, component3=%d\n", __func__, xdim, ydim, zdim, partition_count, component1, component2, component3); - printf("Endpoints:\n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low: <%g %g %f> => <%g %g %g %g>\n", i, lowvalues[i].x, lowvalues[i].y, lowvalues[i].z, ei->ep.endpt0[i].x, ei->ep.endpt0[i].y, ei->ep.endpt0[i].z, ei->ep.endpt0[i].w); - printf("%d High: <%g %g %g> => <%g %g %g %g>\n", i, highvalues[i].x, highvalues[i].y, highvalues[i].z, ei->ep.endpt1[i].x, ei->ep.endpt1[i].y, ei->ep.endpt1[i].z, ei->ep.endpt1[i].w); - } - printf("Ideal-weights:\n"); - - for (i = 0; i < texels_per_block; i++) - { - printf("%3d <%2d %2d %2d>=> %g (weight=%g)\n", i, (i % xdim), (i / xdim) % ydim, i / (xdim * ydim), ei->weights[i], ei->weight_error_scale[i]); - } - printf("\n"); - } - #endif -} - - - -static void compute_endpoints_and_ideal_weights_rgba(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, endpoints_and_weights * ei) -{ - int i; - - - const float *error_weights = ewb->texel_weight; - - int partition_count = pt->partition_count; - float lowparam[4], highparam[4]; - for (i = 0; i < partition_count; i++) - { - lowparam[i] = 1e10; - highparam[i] = -1e10; - } - - float4 averages[4]; - float4 directions_rgba[4]; - float3 directions_gba[4]; - float3 directions_rba[4]; - float3 directions_rga[4]; - float3 directions_rgb[4]; - - line4 lines[4]; - - float scale[4]; - float length_squared[4]; - - float4 error_weightings[4]; - float4 color_scalefactors[4]; - float4 scalefactors[4]; - - int texels_per_block = xdim * ydim * zdim; - - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pt, error_weightings, color_scalefactors); - - for (i = 0; i < partition_count; i++) - scalefactors[i] = normalize(color_scalefactors[i]) * 2.0f; - - - - compute_averages_and_directions_rgba(pt, blk, ewb, scalefactors, averages, directions_rgba, directions_gba, directions_rba, directions_rga, directions_rgb); - - // if the direction-vector ends up pointing from light to dark, FLIP IT! - // this will make the first endpoint the darkest one. - for (i = 0; i < partition_count; i++) - { - float4 direc = directions_rgba[i]; - if (direc.x + direc.y + direc.z < 0.0f) - directions_rgba[i] = float4(0, 0, 0, 0) - direc; - } - - for (i = 0; i < partition_count; i++) - { - lines[i].a = averages[i]; - if (dot(directions_rgba[i], directions_rgba[i]) == 0.0f) - lines[i].b = normalize(float4(1, 1, 1, 1)); - else - lines[i].b = normalize(directions_rgba[i]); - } - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - for (i = 0; i < partition_count; i++) - { - printf("Direction-vector %d: <%f %f %f %f>\n", i, directions_rgba[i].x, directions_rgba[i].y, directions_rgba[i].z, directions_rgba[i].w); - printf("Line %d A: <%f %f %f %f>\n", i, lines[i].a.x, lines[i].a.y, lines[i].a.z, lines[i].a.w); - printf("Line %d B: <%f %f %f %f>\n", i, lines[i].b.x, lines[i].b.y, lines[i].b.z, lines[i].b.w); - printf("Scalefactors %d: <%f %f %f %f>\n", i, scalefactors[i].x, scalefactors[i].y, scalefactors[i].z, scalefactors[i].w); - } - } - #endif - - - for (i = 0; i < texels_per_block; i++) - { - if (error_weights[i] > 1e-10) - { - int partition = pt->partition_of_texel[i]; - - float4 point = float4(blk->work_data[4 * i], blk->work_data[4 * i + 1], blk->work_data[4 * i + 2], blk->work_data[4 * i + 3]) * scalefactors[partition]; - line4 l = lines[partition]; - - float param = dot(point - l.a, l.b); - ei->weights[i] = param; - if (param < lowparam[partition]) - lowparam[partition] = param; - if (param > highparam[partition]) - highparam[partition] = param; - } - else - { - ei->weights[i] = -1e38f; - } - } - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - for (i = 0; i < partition_count; i++) - printf("Partition %d: Lowparam=%f Highparam=%f\n", i, lowparam[i], highparam[i]); - } - #endif - - - for (i = 0; i < partition_count; i++) - { - float length = highparam[i] - lowparam[i]; - if (length < 0) - { - lowparam[i] = 0.0f; - highparam[i] = 1e-7f; - } - - - // it is possible for a uniform-color partition to produce length=0; this - // causes NaN-production and NaN-propagation later on. Set length to - // a small value to avoid this problem. - if (length < 1e-7f) - length = 1e-7f; - - length_squared[i] = length * length; - scale[i] = 1.0f / length; - - ei->ep.endpt0[i] = (lines[i].a + lines[i].b * lowparam[i]) / scalefactors[i]; - ei->ep.endpt1[i] = (lines[i].a + lines[i].b * highparam[i]) / scalefactors[i]; - } - - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - float idx = (ei->weights[i] - lowparam[partition]) * scale[partition]; - if (idx > 1.0f) - idx = 1.0f; - else if (!(idx > 0.0f)) - idx = 0.0f; - ei->weights[i] = idx; - ei->weight_error_scale[i] = error_weights[i] * length_squared[partition]; - if (astc_isnan(ei->weight_error_scale[i])) - { - ASTC_CODEC_INTERNAL_ERROR; - } - } - - - // print all the data that this function computes. - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s: %dx%dx%d texels, %d partitions\n", __func__, xdim, ydim, zdim, partition_count); - printf("Endpoints:\n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low: <%g %g %g %g>\n", i, ei->ep.endpt0[i].x, ei->ep.endpt0[i].y, ei->ep.endpt0[i].z, ei->ep.endpt0[i].w); - printf("%d High: <%g %g %g %g>\n", i, ei->ep.endpt1[i].x, ei->ep.endpt1[i].y, ei->ep.endpt1[i].z, ei->ep.endpt1[i].w); - } - printf("\nIdeal-weights:\n"); - - for (i = 0; i < texels_per_block; i++) - { - printf("%3d <%2d %2d %2d>=> %g (weight=%g)\n", i, i % xdim, (i / xdim) % ydim, i / (xdim * ydim), ei->weights[i], ei->weight_error_scale[i]); - } - printf("\n\n"); - } - #endif - -} - - - -/* - - For a given partitioning, compute: for each partition, the ideal endpoint colors; - these define a color line for the partition. for each pixel, the ideal position of the pixel on the partition's - color line. for each pixel, the length of the color line. - - These data allow us to assess the error introduced by removing and quantizing the per-pixel weights. - - */ - -void compute_endpoints_and_ideal_weights_1_plane(int xdim, int ydim, int zdim, const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, endpoints_and_weights * ei) -{ - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("%s: texels_per_block=%dx%dx%d\n\n", __func__, xdim, ydim, zdim); - #endif - - int uses_alpha = imageblock_uses_alpha(xdim, ydim, zdim, blk); - if (uses_alpha) - { - compute_endpoints_and_ideal_weights_rgba(xdim, ydim, zdim, pt, blk, ewb, ei); - } - else - { - compute_endpoints_and_ideal_weights_3_components(xdim, ydim, zdim, pt, blk, ewb, ei, 0, 1, 2); - } -} - - - -void compute_endpoints_and_ideal_weights_2_planes(int xdim, int ydim, int zdim, const partition_info * pt, - const imageblock * blk, const error_weight_block * ewb, int separate_component, - endpoints_and_weights * ei1, endpoints_and_weights * ei2) -{ - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("%s: texels_per_block=%dx%dx%d, separate_component=%d\n\n", __func__, xdim, ydim, zdim, separate_component); - #endif - - int uses_alpha = imageblock_uses_alpha(xdim, ydim, zdim, blk); - switch (separate_component) - { - case 0: // separate weights for red - if (uses_alpha == 1) - compute_endpoints_and_ideal_weights_3_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 1, 2, 3); - else - compute_endpoints_and_ideal_weights_2_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 1, 2); - compute_endpoints_and_ideal_weights_1_component(xdim, ydim, zdim, pt, blk, ewb, ei2, 0); - break; - - case 1: // separate weights for green - if (uses_alpha == 1) - compute_endpoints_and_ideal_weights_3_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 0, 2, 3); - else - compute_endpoints_and_ideal_weights_2_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 0, 2); - compute_endpoints_and_ideal_weights_1_component(xdim, ydim, zdim, pt, blk, ewb, ei2, 1); - break; - - case 2: // separate weights for blue - if (uses_alpha == 1) - compute_endpoints_and_ideal_weights_3_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 0, 1, 3); - else - compute_endpoints_and_ideal_weights_2_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 0, 1); - compute_endpoints_and_ideal_weights_1_component(xdim, ydim, zdim, pt, blk, ewb, ei2, 2); - break; - - case 3: // separate weights for alpha - if (uses_alpha == 0) - { - ASTC_CODEC_INTERNAL_ERROR; - } - compute_endpoints_and_ideal_weights_3_components(xdim, ydim, zdim, pt, blk, ewb, ei1, 0, 1, 2); - - compute_endpoints_and_ideal_weights_1_component(xdim, ydim, zdim, pt, blk, ewb, ei2, 3); - break; - } - -} - - - -/* - After having computed ideal weights for the case where a weight exists for - every texel, we want to compute the ideal weights for the case where weights - exist only for some texels. - - We do this with a steepest-descent grid solver; this works as follows: - - * First, for each actual weight, perform a weighted averaging based on the - texels affected by the weight. - * Then, set step size to <some initial value> - * Then, repeat: - 1: First, compute for each weight how much the error will change - if we change the weight by an infinitesimal amount. - 2: This produces a vector that points the direction we should step in. - Normalize this vector. - 3: Perform a step - 4: Check if the step actually improved the error. If it did, perform - another step in the same direction; repeat until error no longer - improves. If the *first* step did not improve error, then we halve - the step size. - 5: If the step size dropped down below <some threshold value>, - then we quit, else we go back to #1. - - Subroutines: one routine to apply a step and compute the step's effect on - the error one routine to compute the error change of an infinitesimal - weight change - - Data structures needed: - For every decimation pattern, we need: - * For each weight, a list of <texel, weight> tuples that tell which texels - the weight influences. - * For each texel, a list of <texel, weight> tuples that tell which weights - go into a given texel. -*/ - -float compute_value_of_texel_flt(int texel_to_get, const decimation_table * it, const float *weights) -{ - const uint8_t *texel_weights = it->texel_weights[texel_to_get]; - const float *texel_weights_float = it->texel_weights_float[texel_to_get]; - - return - (weights[texel_weights[0]] * texel_weights_float[0] + weights[texel_weights[1]] * texel_weights_float[1]) + (weights[texel_weights[2]] * texel_weights_float[2] + weights[texel_weights[3]] * texel_weights_float[3]); -} - - -static inline float compute_error_of_texel(const endpoints_and_weights * eai, int texel_to_get, const decimation_table * it, const float *weights) -{ - float current_value = compute_value_of_texel_flt(texel_to_get, it, weights); - float valuedif = current_value - eai->weights[texel_to_get]; - return valuedif * valuedif * eai->weight_error_scale[texel_to_get]; -} - -/* - helper function: given - * for each texel, an ideal weight and an error-modifier these are contained - in an endpoints_and_weights data structure. - * a weight_table data structure - * for each weight, its current value - - compute the change to overall error that results from adding N to the weight -*/ - - -// this routine is rather heavily optimized since it consumes a lot of CPU time. -void compute_two_error_changes_from_perturbing_weight_infill(const endpoints_and_weights * eai, const decimation_table * it, - float *infilled_weights, int weight_to_perturb, - float perturbation1, float perturbation2, float *res1, float *res2) -{ - int num_weights = it->weight_num_texels[weight_to_perturb]; - float error_change0 = 0.0f; - float error_change1 = 0.0f; - int i; - - const uint8_t *weight_texel_ptr = it->weight_texel[weight_to_perturb]; - const float *weights_ptr = it->weights_flt[weight_to_perturb]; - for (i = num_weights - 1; i >= 0; i--) - { - uint8_t weight_texel = weight_texel_ptr[i]; - float weights = weights_ptr[i]; - - float scale = eai->weight_error_scale[weight_texel] * weights; - float old_weight = infilled_weights[weight_texel]; - float ideal_weight = eai->weights[weight_texel]; - - error_change0 += weights * scale; - error_change1 += (old_weight - ideal_weight) * scale; - } - *res1 = error_change0 * (perturbation1 * perturbation1 * (1.0f / (TEXEL_WEIGHT_SUM * TEXEL_WEIGHT_SUM))) + error_change1 * (perturbation1 * (2.0f / TEXEL_WEIGHT_SUM)); - *res2 = error_change0 * (perturbation2 * perturbation2 * (1.0f / (TEXEL_WEIGHT_SUM * TEXEL_WEIGHT_SUM))) + error_change1 * (perturbation2 * (2.0f / TEXEL_WEIGHT_SUM)); -} - - - -float compute_error_of_weight_set(const endpoints_and_weights * eai, const decimation_table * it, const float *weights) -{ - int i; - int texel_count = it->num_texels; - float error_summa = 0.0; - for (i = 0; i < texel_count; i++) - error_summa += compute_error_of_texel(eai, i, it, weights); - return error_summa; -} - - -/* - Given a complete weight set and a decimation table, try to - compute the optimal weight set (assuming infinite precision) - given the selected decimation table. -*/ - -void compute_ideal_weights_for_decimation_table(const endpoints_and_weights * eai, const decimation_table * it, float *weight_set, float *weights) -{ - int i, j, k; - - int blockdim = (int)floor(sqrt((float)it->num_texels) + 0.5f); - int texels_per_block = it->num_texels; - int weight_count = it->num_weights; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : decimation from %d to %d weights\n\n", __func__, it->num_texels, it->num_weights); - printf("Input weight set:\n"); - for (i = 0; i < it->num_texels; i++) - { - printf("%3d <%2d %2d> : %g\n", i, i % blockdim, i / blockdim, eai->weights[i]); - } - printf("\n"); - } - #endif - - - // perform a shortcut in the case of a complete decimation table - if (texels_per_block == weight_count) - { - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("%s : no decimation actually needed: early-out\n\n", __func__); - #endif - - for (i = 0; i < it->num_texels; i++) - { - int texel = it->weight_texel[i][0]; - weight_set[i] = eai->weights[texel]; - weights[i] = eai->weight_error_scale[texel]; - } - return; - } - - - // if the shortcut is not available, we will instead compute a simple estimate - // and perform three rounds of refinement on that estimate. - - float initial_weight_set[MAX_WEIGHTS_PER_BLOCK]; - float infilled_weights[MAX_TEXELS_PER_BLOCK]; - - // compute an initial average for each weight. - for (i = 0; i < weight_count; i++) - { - int texel_count = it->weight_num_texels[i]; - - float weight_weight = 1e-10f; // to avoid 0/0 later on - float initial_weight = 0.0f; - for (j = 0; j < texel_count; j++) - { - int texel = it->weight_texel[i][j]; - float weight = it->weights_flt[i][j]; - float contrib_weight = weight * eai->weight_error_scale[texel]; - weight_weight += contrib_weight; - initial_weight += eai->weights[texel] * contrib_weight; - } - - weights[i] = weight_weight; - weight_set[i] = initial_weight / weight_weight; // this is the 0/0 that is to be avoided. - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - // stash away the initial-weight estimates for later printing - for (i = 0; i < weight_count; i++) - initial_weight_set[i] = weight_set[i]; - } - #endif - - - for (i = 0; i < texels_per_block; i++) - { - infilled_weights[i] = compute_value_of_texel_flt(i, it, weight_set); - } - - const float stepsizes[2] = { 0.25f, 0.125f }; - - for (j = 0; j < 2; j++) - { - float stepsize = stepsizes[j]; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Pass %d, step=%f \n", j, stepsize); - #endif - - for (i = 0; i < weight_count; i++) - { - float weight_val = weight_set[i]; - float error_change_up, error_change_down; - compute_two_error_changes_from_perturbing_weight_infill(eai, it, infilled_weights, i, stepsize, -stepsize, &error_change_up, &error_change_down); - - /* - assume that the error-change function behaves like a quadratic function in the interval examined, - with "error_change_up" and "error_change_down" defining the function at the endpoints - of the interval. Then, find the position where the function's derivative is zero. - - The "fabs(b) >= a" check tests several conditions in one: - if a is negative, then the 2nd derivative of the function is negative; - in this case, f'(x)=0 will maximize error. - If fabs(b) > fabs(a), then f'(x)=0 will lie outside the interval altogether. - If a and b are both 0, then set step to 0; - otherwise, we end up computing 0/0, which produces a lethal NaN. - We can get an a=b=0 situation if an error weight is 0 in the wrong place. - */ - - float step; - float a = (error_change_up + error_change_down) * 2.0f; - float b = error_change_down - error_change_up; - if (fabs(b) >= a) - { - if (a <= 0.0f) - { - if (error_change_up < error_change_down) - step = 1; - else if (error_change_up > error_change_down) - step = -1; - - else - step = 0; - - } - else - { - if (a < 1e-10f) - a = 1e-10f; - step = b / a; - if (step < -1.0f) - step = -1.0f; - else if (step > 1.0f) - step = 1.0f; - } - } - else - step = b / a; - - - step *= stepsize; - float new_weight_val = weight_val + step; - - // update the weight - weight_set[i] = new_weight_val; - // update the infilled-weights - int num_weights = it->weight_num_texels[i]; - float perturbation = (new_weight_val - weight_val) * (1.0f / TEXEL_WEIGHT_SUM); - const uint8_t *weight_texel_ptr = it->weight_texel[i]; - const float *weights_ptr = it->weights_flt[i]; - for (k = num_weights - 1; k >= 0; k--) - { - uint8_t weight_texel = weight_texel_ptr[k]; - float weight_weight = weights_ptr[k]; - infilled_weights[weight_texel] += perturbation * weight_weight; - } - - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("\n"); - #endif - } - - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Error weights, initial-estimates, final-results\n"); - for (i = 0; i < weight_count; i++) - { - printf("%2d -> weight=%g, initial=%g final=%g\n", i, weights[i], initial_weight_set[i], weight_set[i]); - } - printf("\n"); - } - #endif - - return; -} - - - - -/* - For a decimation table, try to compute an optimal weight set, assuming - that the weights are quantized and subject to a transfer function. - - We do this as follows: - First, we take the initial weights and quantize them. This is our initial estimate. - Then, go through the weights one by one; try to perturb then up and down one weight at a - time; apply any perturbations that improve overall error - Repeat until we have made a complete processing pass over all weights without - triggering any perturbations *OR* we have run 4 full passes. -*/ - -void compute_ideal_quantized_weights_for_decimation_table(const endpoints_and_weights * eai, - const decimation_table * it, - float low_bound, float high_bound, const float *weight_set_in, float *weight_set_out, uint8_t * quantized_weight_set, int quantization_level) -{ - int i; - int weight_count = it->num_weights; - int texels_per_block = it->num_texels; - - const quantization_and_transfer_table *qat = &(quant_and_xfer_tables[quantization_level]); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : texels-per-block=%d, weights=%d, quantization-level=%d\n\n", __func__, texels_per_block, weight_count, quantization_level); - - printf("Weight values before quantization:\n"); - for (i = 0; i < weight_count; i++) - printf("%3d : %g\n", i, weight_set_in[i]); - - printf("Low-bound: %f High-bound: %f\n", low_bound, high_bound); - } - #endif - - - // quantize the weight set using both the specified low/high bounds and the - // standard 0..1 weight bounds. - - /* - WTF issue that we need to examine some time - */ - - if (!((high_bound - low_bound) > 0.5f)) - { - low_bound = 0.0f; - high_bound = 1.0f; - } - - float rscale = high_bound - low_bound; - float scale = 1.0f / rscale; - - // rescale the weights so that - // low_bound -> 0 - // high_bound -> 1 - // OK: first, subtract low_bound, then divide by (high_bound - low_bound) - - for (i = 0; i < weight_count; i++) - weight_set_out[i] = (weight_set_in[i] - low_bound) * scale; - - - - static const float quantization_step_table[12] = { - 1.0f / 1.0f, - 1.0f / 2.0f, - 1.0f / 3.0f, - 1.0f / 4.0f, - 1.0f / 5.0f, - 1.0f / 7.0f, - 1.0f / 9.0f, - 1.0f / 11.0f, - 1.0f / 15.0f, - 1.0f / 19.0f, - 1.0f / 23.0f, - 1.0f / 31.0f, - }; - - float quantization_cutoff = quantization_step_table[quantization_level] * 0.333f; - - - int is_perturbable[MAX_WEIGHTS_PER_BLOCK]; - int perturbable_count = 0; - - // quantize the weight set - for (i = 0; i < weight_count; i++) - { - float ix0 = weight_set_out[i]; - if (ix0 < 0.0f) - ix0 = 0.0f; - if (ix0 > 1.0f) - ix0 = 1.0f; - float ix = ix0; - - ix *= 1024.0f; - int ix2 = (int)floor(ix + 0.5f); - int weight = qat->closest_quantized_weight[ix2]; - - ix = qat->unquantized_value_flt[weight]; - weight_set_out[i] = ix; - quantized_weight_set[i] = weight; - - // test whether the error of the weight is greater than 1/3 of the weight spacing; - // if it is not, then it is flagged as "not perturbable". This causes a - // quality loss of about 0.002 dB, which is totally worth the speedup we're getting. - is_perturbable[i] = 0; - if (fabs(ix - ix0) > quantization_cutoff) - { - is_perturbable[i] = 1; - perturbable_count++; - } - } - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Weight values after initial quantization:\n"); - for (i = 0; i < weight_count; i++) - printf("%3d : %g <%d>\n", i, weight_set_out[i], quantized_weight_set[i]); - } - #endif - - - - - // if the decimation table is complete, the quantization above was all we needed to do, - // so we can early-out. - if (it->num_weights == it->num_texels) - { - // invert the weight-scaling that was done initially - // 0 -> low_bound - // 1 -> high_bound - - rscale = high_bound - low_bound; - for (i = 0; i < weight_count; i++) - weight_set_out[i] = (weight_set_out[i] * rscale) + low_bound; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Weight values after adjustment:\n"); - for (i = 0; i < weight_count; i++) - printf("%3d : %g <%d> <error=%g>\n", i, weight_set_out[i], quantized_weight_set[i], weight_set_out[i] - weight_set_in[i]); - printf("\n"); - printf("%s: Early-out\n\n", __func__); - - } - #endif - - return; - } - - - int weights_tested = 0; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - int perturbation_count = 0; - #endif - - // if no weights are flagged as perturbable, don't try to perturb them. - // if only one weight is flagged as perturbable, perturbation is also pointless. - if (perturbable_count > 1) - { - endpoints_and_weights eaix; - for (i = 0; i < texels_per_block; i++) - { - eaix.weights[i] = (eai->weights[i] - low_bound) * scale; - eaix.weight_error_scale[i] = eai->weight_error_scale[i]; - } - - float infilled_weights[MAX_TEXELS_PER_BLOCK]; - for (i = 0; i < texels_per_block; i++) - infilled_weights[i] = compute_value_of_texel_flt(i, it, weight_set_out); - - int weight_to_perturb = 0; - int weights_since_last_perturbation = 0; - int num_weights = it->num_weights; - - while (weights_since_last_perturbation < num_weights && weights_tested < num_weights * 4) - { - int do_quant_mod = 0; - if (is_perturbable[weight_to_perturb]) - { - - int weight_val = quantized_weight_set[weight_to_perturb]; - int weight_next_up = qat->next_quantized_value[weight_val]; - int weight_next_down = qat->prev_quantized_value[weight_val]; - float flt_weight_val = qat->unquantized_value_flt[weight_val]; - float flt_weight_next_up = qat->unquantized_value_flt[weight_next_up]; - float flt_weight_next_down = qat->unquantized_value_flt[weight_next_down]; - - - int do_quant_mod = 0; - - float error_change_up, error_change_down; - - // compute the error change from perturbing the weight either up or down. - compute_two_error_changes_from_perturbing_weight_infill(&eaix, - it, - infilled_weights, - weight_to_perturb, - (flt_weight_next_up - flt_weight_val), (flt_weight_next_down - flt_weight_val), &error_change_up, &error_change_down); - - int new_weight_val; - float flt_new_weight_val; - if (weight_val != weight_next_up && error_change_up < 0.0f) - { - do_quant_mod = 1; - new_weight_val = weight_next_up; - flt_new_weight_val = flt_weight_next_up; - } - else if (weight_val != weight_next_down && error_change_down < 0.0f) - { - do_quant_mod = 1; - new_weight_val = weight_next_down; - flt_new_weight_val = flt_weight_next_down; - } - - - if (do_quant_mod) - { - - // update the weight. - weight_set_out[weight_to_perturb] = flt_new_weight_val; - quantized_weight_set[weight_to_perturb] = new_weight_val; - - // update the infilled-weights - int num_weights = it->weight_num_texels[weight_to_perturb]; - float perturbation = (flt_new_weight_val - flt_weight_val) * (1.0f / TEXEL_WEIGHT_SUM); - const uint8_t *weight_texel_ptr = it->weight_texel[weight_to_perturb]; - const float *weights_ptr = it->weights_flt[weight_to_perturb]; - for (i = num_weights - 1; i >= 0; i--) - { - uint8_t weight_texel = weight_texel_ptr[i]; - float weights = weights_ptr[i]; - infilled_weights[weight_texel] += perturbation * weights; - } - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Perturbation of weight %d : %g\n", weight_to_perturb, perturbation * (float)TEXEL_WEIGHT_SUM); - perturbation_count++; - } - #endif - } - } - - if (do_quant_mod) - weights_since_last_perturbation = 0; - else - weights_since_last_perturbation++; - - weight_to_perturb++; - if (weight_to_perturb >= num_weights) - weight_to_perturb -= num_weights; - - weights_tested++; - } - } - - // invert the weight-scaling that was done initially - // 0 -> low_bound - // 1 -> high_bound - - - for (i = 0; i < weight_count; i++) - weight_set_out[i] = (weight_set_out[i] * rscale) + low_bound; - - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%d weights, %d weight tests, %d perturbations\n", weight_count, weights_tested, perturbation_count); - printf("Weight values after adjustment:\n"); - for (i = 0; i < weight_count; i++) - printf("%3d : %g <%d>\n", i, weight_set_out[i], quantized_weight_set[i]); - printf("\n"); - } - #endif - -} - - - - - - -static inline float mat_square_sum(mat2 p) -{ - float a = p.v[0].x; - float b = p.v[0].y; - float c = p.v[1].x; - float d = p.v[1].y; - return a * a + b * b + c * c + d * d; -} - - - - - - - - - - -/* - for a given weight set, we wish to recompute the colors so that they are optimal for a particular weight set. */ -void recompute_ideal_colors(int xdim, int ydim, int zdim, int weight_quantization_mode, endpoints * ep, // contains the endpoints we wish to update - float4 * rgbs_vectors, // used to return RGBS-vectors. (endpoint mode #6) - float4 * rgbo_vectors, // used to return RGBO-vectors. (endpoint mode #7) - float2 * lum_vectors, // used to return luminance-vectors. - const uint8_t * weight_set8, // the current set of weight values - const uint8_t * plane2_weight_set8, // NULL if plane 2 is not actually used. - int plane2_color_component, // color component for 2nd plane of weights; -1 if the 2nd plane of weights is not present - const partition_info * pi, const decimation_table * it, const imageblock * pb, // picture-block containing the actual data. - const error_weight_block * ewb) -{ - int i, j; - - int texels_per_block = xdim * ydim * zdim; - - const quantization_and_transfer_table *qat = &(quant_and_xfer_tables[weight_quantization_mode]); - - float weight_set[MAX_WEIGHTS_PER_BLOCK]; - float plane2_weight_set[MAX_WEIGHTS_PER_BLOCK]; - - for (i = 0; i < it->num_weights; i++) - { - weight_set[i] = qat->unquantized_value_flt[weight_set8[i]]; - } - if (plane2_weight_set8) - { - for (i = 0; i < it->num_weights; i++) - plane2_weight_set[i] = qat->unquantized_value_flt[plane2_weight_set8[i]]; - } - - int partition_count = pi->partition_count; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : %dx%dx%d texels_per_block, %d partitions, plane2-color-component=%d\n\n", __func__, xdim, ydim, zdim, partition_count, plane2_color_component); - - printf("Pre-adjustment endpoint-colors: \n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low <%g %g %g %g>\n", i, ep->endpt0[i].x, ep->endpt0[i].y, ep->endpt0[i].z, ep->endpt0[i].w); - printf("%d High <%g %g %g %g>\n", i, ep->endpt1[i].x, ep->endpt1[i].y, ep->endpt1[i].z, ep->endpt1[i].w); - } - } - #endif - - - mat2 pmat1_red[4], pmat1_green[4], pmat1_blue[4], pmat1_alpha[4], pmat1_lum[4], pmat1_scale[4]; // matrices for plane of weights 1 - mat2 pmat2_red[4], pmat2_green[4], pmat2_blue[4], pmat2_alpha[4]; // matrices for plane of weights 2 - float2 red_vec[4]; - float2 green_vec[4]; - float2 blue_vec[4]; - float2 alpha_vec[4]; - float2 lum_vec[4]; - float2 scale_vec[4]; - - for (i = 0; i < partition_count; i++) - { - for (j = 0; j < 2; j++) - { - pmat1_red[i].v[j] = float2(0, 0); - pmat2_red[i].v[j] = float2(0, 0); - pmat1_green[i].v[j] = float2(0, 0); - pmat2_green[i].v[j] = float2(0, 0); - pmat1_blue[i].v[j] = float2(0, 0); - pmat2_blue[i].v[j] = float2(0, 0); - pmat1_alpha[i].v[j] = float2(0, 0); - pmat2_alpha[i].v[j] = float2(0, 0); - pmat1_lum[i].v[j] = float2(0, 0); - pmat1_scale[i].v[j] = float2(0, 0); - } - red_vec[i] = float2(0, 0); - green_vec[i] = float2(0, 0); - blue_vec[i] = float2(0, 0); - alpha_vec[i] = float2(0, 0); - lum_vec[i] = float2(0, 0); - scale_vec[i] = float2(0, 0); - } - - - float wmin1[4], wmax1[4]; - float wmin2[4], wmax2[4]; - float red_weight_sum[4]; - float green_weight_sum[4]; - float blue_weight_sum[4]; - float alpha_weight_sum[4]; - float lum_weight_sum[4]; - float scale_weight_sum[4]; - - float red_weight_weight_sum[4]; - float green_weight_weight_sum[4]; - float blue_weight_weight_sum[4]; - - float psum[4]; // sum of (weight * qweight^2) across (red,green,blue) - float qsum[4]; // sum of (weight * qweight * texelval) across (red,green,blue) - - - for (i = 0; i < partition_count; i++) - { - wmin1[i] = 1.0f; - wmax1[i] = 0.0f; - wmin2[i] = 1.0f; - wmax2[i] = 0.0f; - red_weight_sum[i] = 1e-17f; - green_weight_sum[i] = 1e-17f; - blue_weight_sum[i] = 1e-17f; - alpha_weight_sum[i] = 1e-17f; - - lum_weight_sum[i] = 1e-17f; - scale_weight_sum[i] = 1e-17f; - - red_weight_weight_sum[i] = 1e-17f; - green_weight_weight_sum[i] = 1e-17f; - blue_weight_weight_sum[i] = 1e-17f; - - psum[i] = 1e-17f; - qsum[i] = 1e-17f; - } - - - // for each partition, compute the direction that an RGB-scale color endpoint pair would have. - float3 rgb_sum[4]; - float3 rgb_weight_sum[4]; - float3 scale_directions[4]; - float scale_min[4]; - float scale_max[4]; - float lum_min[4]; - float lum_max[4]; - - for (i = 0; i < partition_count; i++) - { - rgb_sum[i] = float3(1e-17f, 1e-17f, 1e-17f); - rgb_weight_sum[i] = float3(1e-17f, 1e-17f, 1e-17f); - } - - - for (i = 0; i < texels_per_block; i++) - { - float3 rgb = float3(pb->work_data[4 * i], pb->work_data[4 * i + 1], pb->work_data[4 * i + 2]); - float3 rgb_weight = float3(ewb->texel_weight_r[i], - ewb->texel_weight_g[i], - ewb->texel_weight_b[i]); - - int part = pi->partition_of_texel[i]; - rgb_sum[part] = rgb_sum[part] + (rgb * rgb_weight); - rgb_weight_sum[part] = rgb_weight_sum[part] + rgb_weight; - } - - for (i = 0; i < partition_count; i++) - { - scale_directions[i] = normalize(rgb_sum[i] / rgb_weight_sum[i]); - scale_max[i] = 0.0f; - scale_min[i] = 1e10f; - lum_max[i] = 0.0f; - lum_min[i] = 1e10f; - } - - - - - - for (i = 0; i < texels_per_block; i++) - { - float r = pb->work_data[4 * i]; - float g = pb->work_data[4 * i + 1]; - float b = pb->work_data[4 * i + 2]; - float a = pb->work_data[4 * i + 3]; - - int part = pi->partition_of_texel[i]; - float idx0 = it ? compute_value_of_texel_flt(i, it, weight_set) : weight_set[i]; - float om_idx0 = 1.0f - idx0; - - if (idx0 > wmax1[part]) - wmax1[part] = idx0; - if (idx0 < wmin1[part]) - wmin1[part] = idx0; - - float red_weight = ewb->texel_weight_r[i]; - float green_weight = ewb->texel_weight_g[i]; - float blue_weight = ewb->texel_weight_b[i]; - float alpha_weight = ewb->texel_weight_a[i]; - - float lum_weight = (red_weight + green_weight + blue_weight); - float scale_weight = lum_weight; - - float lum = (r * red_weight + g * green_weight + b * blue_weight) / lum_weight; - float3 scale_direction = scale_directions[part]; - float scale = dot(scale_direction, float3(r, g, b)); - if (lum < lum_min[part]) - lum_min[part] = scale; - if (lum > lum_max[part]) - lum_max[part] = scale; - if (scale < scale_min[part]) - scale_min[part] = scale; - if (scale > scale_max[part]) - scale_max[part] = scale; - - - red_weight_sum[part] += red_weight; - green_weight_sum[part] += green_weight; - blue_weight_sum[part] += blue_weight; - alpha_weight_sum[part] += alpha_weight; - lum_weight_sum[part] += lum_weight; - scale_weight_sum[part] += scale_weight; - - - pmat1_red[part].v[0].x += om_idx0 * om_idx0 * red_weight; - pmat1_red[part].v[0].y += idx0 * om_idx0 * red_weight; - pmat1_red[part].v[1].x += idx0 * om_idx0 * red_weight; - pmat1_red[part].v[1].y += idx0 * idx0 * red_weight; - - pmat1_green[part].v[0].x += om_idx0 * om_idx0 * green_weight; - pmat1_green[part].v[0].y += idx0 * om_idx0 * green_weight; - pmat1_green[part].v[1].x += idx0 * om_idx0 * green_weight; - pmat1_green[part].v[1].y += idx0 * idx0 * green_weight; - - pmat1_blue[part].v[0].x += om_idx0 * om_idx0 * blue_weight; - pmat1_blue[part].v[0].y += idx0 * om_idx0 * blue_weight; - pmat1_blue[part].v[1].x += idx0 * om_idx0 * blue_weight; - pmat1_blue[part].v[1].y += idx0 * idx0 * blue_weight; - - pmat1_alpha[part].v[0].x += om_idx0 * om_idx0 * alpha_weight; - pmat1_alpha[part].v[0].y += idx0 * om_idx0 * alpha_weight; - pmat1_alpha[part].v[1].x += idx0 * om_idx0 * alpha_weight; - pmat1_alpha[part].v[1].y += idx0 * idx0 * alpha_weight; - - pmat1_lum[part].v[0].x += om_idx0 * om_idx0 * lum_weight; - pmat1_lum[part].v[0].y += idx0 * om_idx0 * lum_weight; - pmat1_lum[part].v[1].x += idx0 * om_idx0 * lum_weight; - pmat1_lum[part].v[1].y += idx0 * idx0 * lum_weight; - - pmat1_scale[part].v[0].x += om_idx0 * om_idx0 * scale_weight; - pmat1_scale[part].v[0].y += idx0 * om_idx0 * scale_weight; - pmat1_scale[part].v[1].x += idx0 * om_idx0 * scale_weight; - pmat1_scale[part].v[1].y += idx0 * idx0 * scale_weight; - - float idx1 = 0.0f, om_idx1 = 0.0f; - if (plane2_weight_set8) - { - idx1 = it ? compute_value_of_texel_flt(i, it, plane2_weight_set) : plane2_weight_set[i]; - om_idx1 = 1.0f - idx1; - if (idx1 > wmax2[part]) - wmax2[part] = idx1; - if (idx1 < wmin2[part]) - wmin2[part] = idx1; - - pmat2_red[part].v[0].x += om_idx1 * om_idx1 * red_weight; - pmat2_red[part].v[0].y += idx1 * om_idx1 * red_weight; - pmat2_red[part].v[1].x += idx1 * om_idx1 * red_weight; - pmat2_red[part].v[1].y += idx1 * idx1 * red_weight; - - pmat2_green[part].v[0].x += om_idx1 * om_idx1 * green_weight; - pmat2_green[part].v[0].y += idx1 * om_idx1 * green_weight; - pmat2_green[part].v[1].x += idx1 * om_idx1 * green_weight; - pmat2_green[part].v[1].y += idx1 * idx1 * green_weight; - - pmat2_blue[part].v[0].x += om_idx1 * om_idx1 * blue_weight; - pmat2_blue[part].v[0].y += idx1 * om_idx1 * blue_weight; - pmat2_blue[part].v[1].x += idx1 * om_idx1 * blue_weight; - pmat2_blue[part].v[1].y += idx1 * idx1 * blue_weight; - - pmat2_alpha[part].v[0].x += om_idx1 * om_idx1 * alpha_weight; - pmat2_alpha[part].v[0].y += idx1 * om_idx1 * alpha_weight; - pmat2_alpha[part].v[1].x += idx1 * om_idx1 * alpha_weight; - pmat2_alpha[part].v[1].y += idx1 * idx1 * alpha_weight; - } - - float red_idx = (plane2_color_component == 0) ? idx1 : idx0; - float green_idx = (plane2_color_component == 1) ? idx1 : idx0; - float blue_idx = (plane2_color_component == 2) ? idx1 : idx0; - float alpha_idx = (plane2_color_component == 3) ? idx1 : idx0; - - - red_vec[part].x += (red_weight * r) * (1.0f - red_idx); - green_vec[part].x += (green_weight * g) * (1.0f - green_idx); - blue_vec[part].x += (blue_weight * b) * (1.0f - blue_idx); - alpha_vec[part].x += (alpha_weight * a) * (1.0f - alpha_idx); - lum_vec[part].x += (lum_weight * lum) * om_idx0; - scale_vec[part].x += (scale_weight * scale) * om_idx0; - - red_vec[part].y += (red_weight * r) * red_idx; - green_vec[part].y += (green_weight * g) * green_idx; - blue_vec[part].y += (blue_weight * b) * blue_idx; - alpha_vec[part].y += (alpha_weight * a) * alpha_idx; - lum_vec[part].y += (lum_weight * lum) * idx0; - scale_vec[part].y += (scale_weight * scale) * idx0; - - red_weight_weight_sum[part] += red_weight * red_idx; - green_weight_weight_sum[part] += green_weight * green_idx; - blue_weight_weight_sum[part] += blue_weight * blue_idx; - - psum[part] += red_weight * red_idx * red_idx + green_weight * green_idx * green_idx + blue_weight * blue_idx * blue_idx; - - } - - // calculations specific to mode #7, the HDR RGB-scale mode. - float red_sum[4]; - float green_sum[4]; - float blue_sum[4]; - for (i = 0; i < partition_count; i++) - { - red_sum[i] = red_vec[i].x + red_vec[i].y; - green_sum[i] = green_vec[i].x + green_vec[i].y; - blue_sum[i] = blue_vec[i].x + blue_vec[i].y; - qsum[i] = red_vec[i].y + green_vec[i].y + blue_vec[i].y; - } - - // RGB+offset for HDR endpoint mode #7 - int rgbo_fail[4]; - for (i = 0; i < partition_count; i++) - { - mat4 mod7_mat; - mod7_mat.v[0] = float4(red_weight_sum[i], 0.0f, 0.0f, red_weight_weight_sum[i]); - mod7_mat.v[1] = float4(0.0f, green_weight_sum[i], 0.0f, green_weight_weight_sum[i]); - mod7_mat.v[2] = float4(0.0f, 0.0f, blue_weight_sum[i], blue_weight_weight_sum[i]); - mod7_mat.v[3] = float4(red_weight_weight_sum[i], green_weight_weight_sum[i], blue_weight_weight_sum[i], psum[i]); - - float4 vect = float4(red_sum[i], green_sum[i], blue_sum[i], qsum[i]); - - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - mat4 rmod7_mat = invert(mod7_mat); - float4 rgbovec = transform(rmod7_mat, vect); - rgbo_vectors[i] = rgbovec; - - // we will occasionally get a failure due to a singular matrix. Record whether such a - // failure has taken place; if it did, compute rgbo_vectors[] with a different method - // later on. - float chkval = dot(rgbovec, rgbovec); - rgbo_fail[i] = chkval != chkval; - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - } - - - - // initialize the luminance and scale vectors with a reasonable default, - // just in case the subsequent calculation blows up. - for (i = 0; i < partition_count; i++) - { - - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - float scalediv = scale_min[i] / scale_max[i]; - if (!(scalediv > 0.0f)) - scalediv = 0.0f; // set to zero if scalediv is zero, negative, or NaN. - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - if (scalediv > 1.0f) - scalediv = 1.0f; - - rgbs_vectors[i] = float4(scale_directions[i] * scale_max[i], scalediv); - lum_vectors[i] = float2(lum_min[i], lum_max[i]); - } - - - - for (i = 0; i < partition_count; i++) - { - - if (wmin1[i] >= wmax1[i] * 0.999) - { - // if all weights in the partition were equal, then just take average - // of all colors in the partition and use that as both endpoint colors. - float4 avg = float4((red_vec[i].x + red_vec[i].y) / red_weight_sum[i], - (green_vec[i].x + green_vec[i].y) / green_weight_sum[i], - (blue_vec[i].x + blue_vec[i].y) / blue_weight_sum[i], - (alpha_vec[i].x + alpha_vec[i].y) / alpha_weight_sum[i]); - - if (plane2_color_component != 0 && avg.x == avg.x) - ep->endpt0[i].x = ep->endpt1[i].x = avg.x; - if (plane2_color_component != 1 && avg.y == avg.y) - ep->endpt0[i].y = ep->endpt1[i].y = avg.y; - if (plane2_color_component != 2 && avg.z == avg.z) - ep->endpt0[i].z = ep->endpt1[i].z = avg.z; - if (plane2_color_component != 3 && avg.w == avg.w) - ep->endpt0[i].w = ep->endpt1[i].w = avg.w; - - rgbs_vectors[i] = float4(scale_directions[i] * scale_max[i], 1.0f); - float lumval = (red_vec[i].x + red_vec[i].y + green_vec[i].x + green_vec[i].y + blue_vec[i].x + blue_vec[i].y) / (red_weight_sum[i] + green_weight_sum[i] + blue_weight_sum[i]); - lum_vectors[i] = float2(lumval, lumval); - } - - else - { - - // otherwise, complete the analytic calculation of ideal-endpoint-values - // for the given set of texel weights and pixel colors. - - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - float red_det1 = determinant(pmat1_red[i]); - float green_det1 = determinant(pmat1_green[i]); - float blue_det1 = determinant(pmat1_blue[i]); - float alpha_det1 = determinant(pmat1_alpha[i]); - float lum_det1 = determinant(pmat1_lum[i]); - float scale_det1 = determinant(pmat1_scale[i]); - - float red_mss1 = mat_square_sum(pmat1_red[i]); - float green_mss1 = mat_square_sum(pmat1_green[i]); - float blue_mss1 = mat_square_sum(pmat1_blue[i]); - float alpha_mss1 = mat_square_sum(pmat1_alpha[i]); - float lum_mss1 = mat_square_sum(pmat1_lum[i]); - float scale_mss1 = mat_square_sum(pmat1_scale[i]); - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Plane-1 partition %d determinants: R=%g G=%g B=%g A=%g L=%g S=%g\n", i, red_det1, green_det1, blue_det1, alpha_det1, lum_det1, scale_det1); - #endif - - pmat1_red[i] = invert(pmat1_red[i]); - pmat1_green[i] = invert(pmat1_green[i]); - pmat1_blue[i] = invert(pmat1_blue[i]); - pmat1_alpha[i] = invert(pmat1_alpha[i]); - pmat1_lum[i] = invert(pmat1_lum[i]); - pmat1_scale[i] = invert(pmat1_scale[i]); - - float4 ep0 = float4(dot(pmat1_red[i].v[0], red_vec[i]), - dot(pmat1_green[i].v[0], green_vec[i]), - dot(pmat1_blue[i].v[0], blue_vec[i]), - dot(pmat1_alpha[i].v[0], alpha_vec[i])); - float4 ep1 = float4(dot(pmat1_red[i].v[1], red_vec[i]), - dot(pmat1_green[i].v[1], green_vec[i]), - dot(pmat1_blue[i].v[1], blue_vec[i]), - dot(pmat1_alpha[i].v[1], alpha_vec[i])); - - float lum_ep0 = dot(pmat1_lum[i].v[0], lum_vec[i]); - float lum_ep1 = dot(pmat1_lum[i].v[1], lum_vec[i]); - float scale_ep0 = dot(pmat1_scale[i].v[0], scale_vec[i]); - float scale_ep1 = dot(pmat1_scale[i].v[1], scale_vec[i]); - - - if (plane2_color_component != 0 && fabs(red_det1) > (red_mss1 * 1e-4f) && ep0.x == ep0.x && ep1.x == ep1.x) - { - ep->endpt0[i].x = ep0.x; - ep->endpt1[i].x = ep1.x; - } - if (plane2_color_component != 1 && fabs(green_det1) > (green_mss1 * 1e-4f) && ep0.y == ep0.y && ep1.y == ep1.y) - { - ep->endpt0[i].y = ep0.y; - ep->endpt1[i].y = ep1.y; - } - if (plane2_color_component != 2 && fabs(blue_det1) > (blue_mss1 * 1e-4f) && ep0.z == ep0.z && ep1.z == ep1.z) - { - ep->endpt0[i].z = ep0.z; - ep->endpt1[i].z = ep1.z; - } - if (plane2_color_component != 3 && fabs(alpha_det1) > (alpha_mss1 * 1e-4f) && ep0.w == ep0.w && ep1.w == ep1.w) - { - ep->endpt0[i].w = ep0.w; - ep->endpt1[i].w = ep1.w; - } - - if (fabs(lum_det1) > (lum_mss1 * 1e-4f) && lum_ep0 == lum_ep0 && lum_ep1 == lum_ep1 && lum_ep0 < lum_ep1) - { - lum_vectors[i].x = lum_ep0; - lum_vectors[i].y = lum_ep1; - } - if (fabs(scale_det1) > (scale_mss1 * 1e-4f) && scale_ep0 == scale_ep0 && scale_ep1 == scale_ep1 && scale_ep0 < scale_ep1) - { - float scalediv = scale_ep0 / scale_ep1; - rgbs_vectors[i] = float4(scale_directions[i] * scale_ep1, scalediv); - } - - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - } - - if (plane2_weight_set8) - { - if (wmin2[i] >= wmax2[i] * 0.999) - { - // if all weights in the partition were equal, then just take average - // of all colors in the partition and use that as both endpoint colors. - float4 avg = float4((red_vec[i].x + red_vec[i].y) / red_weight_sum[i], - (green_vec[i].x + green_vec[i].y) / green_weight_sum[i], - (blue_vec[i].x + blue_vec[i].y) / blue_weight_sum[i], - (alpha_vec[i].x + alpha_vec[i].y) / alpha_weight_sum[i]); - - if (plane2_color_component == 0 && avg.x == avg.x) - ep->endpt0[i].x = ep->endpt1[i].x = avg.x; - if (plane2_color_component == 1 && avg.y == avg.y) - ep->endpt0[i].y = ep->endpt1[i].y = avg.y; - if (plane2_color_component == 2 && avg.z == avg.z) - ep->endpt0[i].z = ep->endpt1[i].z = avg.z; - if (plane2_color_component == 3 && avg.w == avg.w) - ep->endpt0[i].w = ep->endpt1[i].w = avg.w; - } - else - { - - #ifdef DEBUG_CAPTURE_NAN - fedisableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - // otherwise, complete the analytic calculation of ideal-endpoint-values - // for the given set of texel weights and pixel colors. - float red_det2 = determinant(pmat2_red[i]); - float green_det2 = determinant(pmat2_green[i]); - float blue_det2 = determinant(pmat2_blue[i]); - float alpha_det2 = determinant(pmat2_alpha[i]); - - float red_mss2 = mat_square_sum(pmat2_red[i]); - float green_mss2 = mat_square_sum(pmat2_green[i]); - float blue_mss2 = mat_square_sum(pmat2_blue[i]); - float alpha_mss2 = mat_square_sum(pmat2_alpha[i]); - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("Plane-2 partition %d determinants: R=%g G=%g B=%g A=%g\n", i, red_det2, green_det2, blue_det2, alpha_det2); - #endif - - pmat2_red[i] = invert(pmat2_red[i]); - pmat2_green[i] = invert(pmat2_green[i]); - pmat2_blue[i] = invert(pmat2_blue[i]); - pmat2_alpha[i] = invert(pmat2_alpha[i]); - float4 ep0 = float4(dot(pmat2_red[i].v[0], red_vec[i]), - dot(pmat2_green[i].v[0], green_vec[i]), - dot(pmat2_blue[i].v[0], blue_vec[i]), - dot(pmat2_alpha[i].v[0], alpha_vec[i])); - float4 ep1 = float4(dot(pmat2_red[i].v[1], red_vec[i]), - dot(pmat2_green[i].v[1], green_vec[i]), - dot(pmat2_blue[i].v[1], blue_vec[i]), - dot(pmat2_alpha[i].v[1], alpha_vec[i])); - - if (plane2_color_component == 0 && fabs(red_det2) > (red_mss2 * 1e-4f) && ep0.x == ep0.x && ep1.x == ep1.x) - { - ep->endpt0[i].x = ep0.x; - ep->endpt1[i].x = ep1.x; - } - if (plane2_color_component == 1 && fabs(green_det2) > (green_mss2 * 1e-4f) && ep0.y == ep0.y && ep1.y == ep1.y) - { - ep->endpt0[i].y = ep0.y; - ep->endpt1[i].y = ep1.y; - } - if (plane2_color_component == 2 && fabs(blue_det2) > (blue_mss2 * 1e-4f) && ep0.z == ep0.z && ep1.z == ep1.z) - { - ep->endpt0[i].z = ep0.z; - ep->endpt1[i].z = ep1.z; - } - if (plane2_color_component == 3 && fabs(alpha_det2) > (alpha_mss2 * 1e-4f) && ep0.w == ep0.w && ep1.w == ep1.w) - { - ep->endpt0[i].w = ep0.w; - ep->endpt1[i].w = ep1.w; - } - - #ifdef DEBUG_CAPTURE_NAN - feenableexcept(FE_DIVBYZERO | FE_INVALID); - #endif - - } - } - } - - // if the calculation of an RGB-offset vector failed, try to compute - // a somewhat-sensible value anyway - for (i = 0; i < partition_count; i++) - if (rgbo_fail[i]) - { - float4 v0 = ep->endpt0[i]; - float4 v1 = ep->endpt1[i]; - float avgdif = dot(v1.xyz - v0.xyz, float3(1, 1, 1)) * (1.0f / 3.0f); - if (avgdif <= 0.0f) - avgdif = 0.0f; - float4 avg = (v0 + v1) * 0.5f; - float4 ep0 = avg - float4(avgdif, avgdif, avgdif, avgdif) * 0.5f; - - rgbo_vectors[i] = float4(ep0.xyz, avgdif); - } - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("Post-adjustment endpoint-colors: \n"); - for (i = 0; i < partition_count; i++) - { - printf("%d Low <%g %g %g %g>\n", i, ep->endpt0[i].x, ep->endpt0[i].y, ep->endpt0[i].z, ep->endpt0[i].w); - printf("%d High <%g %g %g %g>\n", i, ep->endpt1[i].x, ep->endpt1[i].y, ep->endpt1[i].z, ep->endpt1[i].w); - - printf("%d RGBS: <%g %g %g %g>\n", i, rgbs_vectors[i].x, rgbs_vectors[i].y, rgbs_vectors[i].z, rgbs_vectors[i].w); - - printf("%d RGBO <%g %g %g %g>\n", i, rgbo_vectors[i].x, rgbo_vectors[i].y, rgbo_vectors[i].z, rgbo_vectors[i].w); - - printf("%d Lum: <%g %g>\n", i, lum_vectors[i].x, lum_vectors[i].y); - } - } - #endif - -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_imageblock.cpp b/3rdparty/bimg/3rdparty/astc/astc_imageblock.cpp deleted file mode 100644 index 8740b4cb037..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_imageblock.cpp +++ /dev/null @@ -1,324 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions for managing ASTC codec images. - */ -/*----------------------------------------------------------------------------*/ - -#include <math.h> - -#include "astc_codec_internals.h" - -#include "softfloat.h" -#include <stdint.h> -#include <stdio.h> - -// conversion functions between the LNS representation and the FP16 representation. - -float float_to_lns(float p) -{ - - if (astc_isnan(p) || p <= 1.0f / 67108864.0f) - { - // underflow or NaN value, return 0. - // We count underflow if the input value is smaller than 2^-26. - return 0; - } - - if (fabs(p) >= 65536.0f) - { - // overflow, return a +INF value - return 65535; - } - - int expo; - float normfrac = frexp(p, &expo); - float p1; - if (expo < -13) - { - // input number is smaller than 2^-14. In this case, multiply by 2^25. - p1 = p * 33554432.0f; - expo = 0; - } - else - { - expo += 14; - p1 = (normfrac - 0.5f) * 4096.0f; - } - - if (p1 < 384.0f) - p1 *= 4.0f / 3.0f; - else if (p1 <= 1408.0f) - p1 += 128.0f; - else - p1 = (p1 + 512.0f) * (4.0f / 5.0f); - - p1 += expo * 2048.0f; - return p1 + 1.0f; -} - - - -uint16_t lns_to_sf16(uint16_t p) -{ - - uint16_t mc = p & 0x7FF; - uint16_t ec = p >> 11; - uint16_t mt; - if (mc < 512) - mt = 3 * mc; - else if (mc < 1536) - mt = 4 * mc - 512; - else - mt = 5 * mc - 2048; - - uint16_t res = (ec << 10) | (mt >> 3); - if (res >= 0x7BFF) - res = 0x7BFF; - return res; -} - - -// conversion function from 16-bit LDR value to FP16. -// note: for LDR interpolation, it is impossible to get a denormal result; -// this simplifies the conversion. -// FALSE; we can receive a very small UNORM16 through the constant-block. -uint16_t unorm16_to_sf16(uint16_t p) -{ - if (p == 0xFFFF) - return 0x3C00; // value of 1.0 . - if (p < 4) - return p << 8; - - int lz = clz32(p) - 16; - p <<= (lz + 1); - p >>= 6; - p |= (14 - lz) << 10; - return p; -} - - - - - -void imageblock_initialize_deriv_from_work_and_orig(imageblock * pb, int pixelcount) -{ - int i; - - const float *fptr = pb->orig_data; - const float *wptr = pb->work_data; - float *dptr = pb->deriv_data; - - for (i = 0; i < pixelcount; i++) - { - - // compute derivatives for RGB first - if (pb->rgb_lns[i]) - { - float r = MAX(fptr[0], 6e-5f); - float g = MAX(fptr[1], 6e-5f); - float b = MAX(fptr[2], 6e-5f); - - float rderiv = (float_to_lns(r * 1.05f) - float_to_lns(r)) / (r * 0.05f); - float gderiv = (float_to_lns(g * 1.05f) - float_to_lns(g)) / (g * 0.05f); - float bderiv = (float_to_lns(b * 1.05f) - float_to_lns(b)) / (b * 0.05f); - - // the derivative may not actually take values smaller than 1/32 or larger than 2^25; - // if it does, we clamp it. - if (rderiv < (1.0f / 32.0f)) - rderiv = (1.0f / 32.0f); - else if (rderiv > 33554432.0f) - rderiv = 33554432.0f; - - if (gderiv < (1.0f / 32.0f)) - gderiv = (1.0f / 32.0f); - else if (gderiv > 33554432.0f) - gderiv = 33554432.0f; - - if (bderiv < (1.0f / 32.0f)) - bderiv = (1.0f / 32.0f); - else if (bderiv > 33554432.0f) - bderiv = 33554432.0f; - - dptr[0] = rderiv; - dptr[1] = gderiv; - dptr[2] = bderiv; - } - else - { - dptr[0] = 65535.0f; - dptr[1] = 65535.0f; - dptr[2] = 65535.0f; - } - - - // then compute derivatives for Alpha - if (pb->alpha_lns[i]) - { - float a = MAX(fptr[3], 6e-5f); - float aderiv = (float_to_lns(a * 1.05f) - float_to_lns(a)) / (a * 0.05f); - // the derivative may not actually take values smaller than 1/32 or larger than 2^25; - // if it does, we clamp it. - if (aderiv < (1.0f / 32.0f)) - aderiv = (1.0f / 32.0f); - else if (aderiv > 33554432.0f) - aderiv = 33554432.0f; - - dptr[3] = aderiv; - } - else - { - dptr[3] = 65535.0f; - } - - fptr += 4; - wptr += 4; - dptr += 4; - } -} - - - - -// helper function to initialize the work-data from the orig-data -void imageblock_initialize_work_from_orig(imageblock * pb, int pixelcount) -{ - int i; - float *fptr = pb->orig_data; - float *wptr = pb->work_data; - - for (i = 0; i < pixelcount; i++) - { - if (pb->rgb_lns[i]) - { - wptr[0] = float_to_lns(fptr[0]); - wptr[1] = float_to_lns(fptr[1]); - wptr[2] = float_to_lns(fptr[2]); - } - else - { - wptr[0] = fptr[0] * 65535.0f; - wptr[1] = fptr[1] * 65535.0f; - wptr[2] = fptr[2] * 65535.0f; - } - - if (pb->alpha_lns[i]) - { - wptr[3] = float_to_lns(fptr[3]); - } - else - { - wptr[3] = fptr[3] * 65535.0f; - } - fptr += 4; - wptr += 4; - } - - imageblock_initialize_deriv_from_work_and_orig(pb, pixelcount); -} - - - - -// helper function to initialize the orig-data from the work-data -void imageblock_initialize_orig_from_work(imageblock * pb, int pixelcount) -{ - int i; - float *fptr = pb->orig_data; - float *wptr = pb->work_data; - - for (i = 0; i < pixelcount; i++) - { - if (pb->rgb_lns[i]) - { - fptr[0] = sf16_to_float(lns_to_sf16((uint16_t) wptr[0])); - fptr[1] = sf16_to_float(lns_to_sf16((uint16_t) wptr[1])); - fptr[2] = sf16_to_float(lns_to_sf16((uint16_t) wptr[2])); - } - else - { - fptr[0] = sf16_to_float(unorm16_to_sf16((uint16_t) wptr[0])); - fptr[1] = sf16_to_float(unorm16_to_sf16((uint16_t) wptr[1])); - fptr[2] = sf16_to_float(unorm16_to_sf16((uint16_t) wptr[2])); - } - - if (pb->alpha_lns[i]) - { - fptr[3] = sf16_to_float(lns_to_sf16((uint16_t) wptr[3])); - } - else - { - fptr[3] = sf16_to_float(unorm16_to_sf16((uint16_t) wptr[3])); - } - - fptr += 4; - wptr += 4; - } - - imageblock_initialize_deriv_from_work_and_orig(pb, pixelcount); -} - - -/* - For an imageblock, update its flags. - - The updating is done based on work_data, not orig_data. -*/ -void update_imageblock_flags(imageblock * pb, int xdim, int ydim, int zdim) -{ - int i; - float red_min = 1e38f, red_max = -1e38f; - float green_min = 1e38f, green_max = -1e38f; - float blue_min = 1e38f, blue_max = -1e38f; - float alpha_min = 1e38f, alpha_max = -1e38f; - - int texels_per_block = xdim * ydim * zdim; - - int grayscale = 1; - - for (i = 0; i < texels_per_block; i++) - { - float red = pb->work_data[4 * i]; - float green = pb->work_data[4 * i + 1]; - float blue = pb->work_data[4 * i + 2]; - float alpha = pb->work_data[4 * i + 3]; - if (red < red_min) - red_min = red; - if (red > red_max) - red_max = red; - if (green < green_min) - green_min = green; - if (green > green_max) - green_max = green; - if (blue < blue_min) - blue_min = blue; - if (blue > blue_max) - blue_max = blue; - if (alpha < alpha_min) - alpha_min = alpha; - if (alpha > alpha_max) - alpha_max = alpha; - - if (grayscale == 1 && (red != green || red != blue)) - grayscale = 0; - } - - pb->red_min = red_min; - pb->red_max = red_max; - pb->green_min = green_min; - pb->green_max = green_max; - pb->blue_min = blue_min; - pb->blue_max = blue_max; - pb->alpha_min = alpha_min; - pb->alpha_max = alpha_max; - pb->grayscale = grayscale; -} - diff --git a/3rdparty/bimg/3rdparty/astc/astc_integer_sequence.cpp b/3rdparty/bimg/3rdparty/astc/astc_integer_sequence.cpp deleted file mode 100644 index 58c706a96d8..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_integer_sequence.cpp +++ /dev/null @@ -1,649 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions to encode/decode data using Bounded Integer Sequence - * Encoding. - */ -/*----------------------------------------------------------------------------*/ -#include "astc_codec_internals.h" - // unpacked quint triplets <low,middle,high> for each packed-quint value -static const uint8_t quints_of_integer[128][3] = { - {0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, - {4, 0, 0}, {0, 4, 0}, {4, 4, 0}, {4, 4, 4}, - {0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, - {4, 1, 0}, {1, 4, 0}, {4, 4, 1}, {4, 4, 4}, - {0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, - {4, 2, 0}, {2, 4, 0}, {4, 4, 2}, {4, 4, 4}, - {0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, - {4, 3, 0}, {3, 4, 0}, {4, 4, 3}, {4, 4, 4}, - {0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {3, 0, 1}, - {4, 0, 1}, {0, 4, 1}, {4, 0, 4}, {0, 4, 4}, - {0, 1, 1}, {1, 1, 1}, {2, 1, 1}, {3, 1, 1}, - {4, 1, 1}, {1, 4, 1}, {4, 1, 4}, {1, 4, 4}, - {0, 2, 1}, {1, 2, 1}, {2, 2, 1}, {3, 2, 1}, - {4, 2, 1}, {2, 4, 1}, {4, 2, 4}, {2, 4, 4}, - {0, 3, 1}, {1, 3, 1}, {2, 3, 1}, {3, 3, 1}, - {4, 3, 1}, {3, 4, 1}, {4, 3, 4}, {3, 4, 4}, - {0, 0, 2}, {1, 0, 2}, {2, 0, 2}, {3, 0, 2}, - {4, 0, 2}, {0, 4, 2}, {2, 0, 4}, {3, 0, 4}, - {0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {3, 1, 2}, - {4, 1, 2}, {1, 4, 2}, {2, 1, 4}, {3, 1, 4}, - {0, 2, 2}, {1, 2, 2}, {2, 2, 2}, {3, 2, 2}, - {4, 2, 2}, {2, 4, 2}, {2, 2, 4}, {3, 2, 4}, - {0, 3, 2}, {1, 3, 2}, {2, 3, 2}, {3, 3, 2}, - {4, 3, 2}, {3, 4, 2}, {2, 3, 4}, {3, 3, 4}, - {0, 0, 3}, {1, 0, 3}, {2, 0, 3}, {3, 0, 3}, - {4, 0, 3}, {0, 4, 3}, {0, 0, 4}, {1, 0, 4}, - {0, 1, 3}, {1, 1, 3}, {2, 1, 3}, {3, 1, 3}, - {4, 1, 3}, {1, 4, 3}, {0, 1, 4}, {1, 1, 4}, - {0, 2, 3}, {1, 2, 3}, {2, 2, 3}, {3, 2, 3}, - {4, 2, 3}, {2, 4, 3}, {0, 2, 4}, {1, 2, 4}, - {0, 3, 3}, {1, 3, 3}, {2, 3, 3}, {3, 3, 3}, - {4, 3, 3}, {3, 4, 3}, {0, 3, 4}, {1, 3, 4}, -}; - -// packed quint-value for every unpacked quint-triplet -// indexed by [high][middle][low] -static const uint8_t integer_of_quints[5][5][5] = { - { - {0, 1, 2, 3, 4,}, - {8, 9, 10, 11, 12,}, - {16, 17, 18, 19, 20,}, - {24, 25, 26, 27, 28,}, - {5, 13, 21, 29, 6,}, - }, - { - {32, 33, 34, 35, 36,}, - {40, 41, 42, 43, 44,}, - {48, 49, 50, 51, 52,}, - {56, 57, 58, 59, 60,}, - {37, 45, 53, 61, 14,}, - }, - { - {64, 65, 66, 67, 68,}, - {72, 73, 74, 75, 76,}, - {80, 81, 82, 83, 84,}, - {88, 89, 90, 91, 92,}, - {69, 77, 85, 93, 22,}, - }, - { - {96, 97, 98, 99, 100,}, - {104, 105, 106, 107, 108,}, - {112, 113, 114, 115, 116,}, - {120, 121, 122, 123, 124,}, - {101, 109, 117, 125, 30,}, - }, - { - {102, 103, 70, 71, 38,}, - {110, 111, 78, 79, 46,}, - {118, 119, 86, 87, 54,}, - {126, 127, 94, 95, 62,}, - {39, 47, 55, 63, 31,}, - }, -}; - -// unpacked trit quintuplets <low,_,_,_,high> for each packed-quint value -static const uint8_t trits_of_integer[256][5] = { - {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {2, 0, 0, 0, 0}, {0, 0, 2, 0, 0}, - {0, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {2, 1, 0, 0, 0}, {1, 0, 2, 0, 0}, - {0, 2, 0, 0, 0}, {1, 2, 0, 0, 0}, {2, 2, 0, 0, 0}, {2, 0, 2, 0, 0}, - {0, 2, 2, 0, 0}, {1, 2, 2, 0, 0}, {2, 2, 2, 0, 0}, {2, 0, 2, 0, 0}, - {0, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {2, 0, 1, 0, 0}, {0, 1, 2, 0, 0}, - {0, 1, 1, 0, 0}, {1, 1, 1, 0, 0}, {2, 1, 1, 0, 0}, {1, 1, 2, 0, 0}, - {0, 2, 1, 0, 0}, {1, 2, 1, 0, 0}, {2, 2, 1, 0, 0}, {2, 1, 2, 0, 0}, - {0, 0, 0, 2, 2}, {1, 0, 0, 2, 2}, {2, 0, 0, 2, 2}, {0, 0, 2, 2, 2}, - {0, 0, 0, 1, 0}, {1, 0, 0, 1, 0}, {2, 0, 0, 1, 0}, {0, 0, 2, 1, 0}, - {0, 1, 0, 1, 0}, {1, 1, 0, 1, 0}, {2, 1, 0, 1, 0}, {1, 0, 2, 1, 0}, - {0, 2, 0, 1, 0}, {1, 2, 0, 1, 0}, {2, 2, 0, 1, 0}, {2, 0, 2, 1, 0}, - {0, 2, 2, 1, 0}, {1, 2, 2, 1, 0}, {2, 2, 2, 1, 0}, {2, 0, 2, 1, 0}, - {0, 0, 1, 1, 0}, {1, 0, 1, 1, 0}, {2, 0, 1, 1, 0}, {0, 1, 2, 1, 0}, - {0, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {2, 1, 1, 1, 0}, {1, 1, 2, 1, 0}, - {0, 2, 1, 1, 0}, {1, 2, 1, 1, 0}, {2, 2, 1, 1, 0}, {2, 1, 2, 1, 0}, - {0, 1, 0, 2, 2}, {1, 1, 0, 2, 2}, {2, 1, 0, 2, 2}, {1, 0, 2, 2, 2}, - {0, 0, 0, 2, 0}, {1, 0, 0, 2, 0}, {2, 0, 0, 2, 0}, {0, 0, 2, 2, 0}, - {0, 1, 0, 2, 0}, {1, 1, 0, 2, 0}, {2, 1, 0, 2, 0}, {1, 0, 2, 2, 0}, - {0, 2, 0, 2, 0}, {1, 2, 0, 2, 0}, {2, 2, 0, 2, 0}, {2, 0, 2, 2, 0}, - {0, 2, 2, 2, 0}, {1, 2, 2, 2, 0}, {2, 2, 2, 2, 0}, {2, 0, 2, 2, 0}, - {0, 0, 1, 2, 0}, {1, 0, 1, 2, 0}, {2, 0, 1, 2, 0}, {0, 1, 2, 2, 0}, - {0, 1, 1, 2, 0}, {1, 1, 1, 2, 0}, {2, 1, 1, 2, 0}, {1, 1, 2, 2, 0}, - {0, 2, 1, 2, 0}, {1, 2, 1, 2, 0}, {2, 2, 1, 2, 0}, {2, 1, 2, 2, 0}, - {0, 2, 0, 2, 2}, {1, 2, 0, 2, 2}, {2, 2, 0, 2, 2}, {2, 0, 2, 2, 2}, - {0, 0, 0, 0, 2}, {1, 0, 0, 0, 2}, {2, 0, 0, 0, 2}, {0, 0, 2, 0, 2}, - {0, 1, 0, 0, 2}, {1, 1, 0, 0, 2}, {2, 1, 0, 0, 2}, {1, 0, 2, 0, 2}, - {0, 2, 0, 0, 2}, {1, 2, 0, 0, 2}, {2, 2, 0, 0, 2}, {2, 0, 2, 0, 2}, - {0, 2, 2, 0, 2}, {1, 2, 2, 0, 2}, {2, 2, 2, 0, 2}, {2, 0, 2, 0, 2}, - {0, 0, 1, 0, 2}, {1, 0, 1, 0, 2}, {2, 0, 1, 0, 2}, {0, 1, 2, 0, 2}, - {0, 1, 1, 0, 2}, {1, 1, 1, 0, 2}, {2, 1, 1, 0, 2}, {1, 1, 2, 0, 2}, - {0, 2, 1, 0, 2}, {1, 2, 1, 0, 2}, {2, 2, 1, 0, 2}, {2, 1, 2, 0, 2}, - {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 0, 2, 2, 2}, - {0, 0, 0, 0, 1}, {1, 0, 0, 0, 1}, {2, 0, 0, 0, 1}, {0, 0, 2, 0, 1}, - {0, 1, 0, 0, 1}, {1, 1, 0, 0, 1}, {2, 1, 0, 0, 1}, {1, 0, 2, 0, 1}, - {0, 2, 0, 0, 1}, {1, 2, 0, 0, 1}, {2, 2, 0, 0, 1}, {2, 0, 2, 0, 1}, - {0, 2, 2, 0, 1}, {1, 2, 2, 0, 1}, {2, 2, 2, 0, 1}, {2, 0, 2, 0, 1}, - {0, 0, 1, 0, 1}, {1, 0, 1, 0, 1}, {2, 0, 1, 0, 1}, {0, 1, 2, 0, 1}, - {0, 1, 1, 0, 1}, {1, 1, 1, 0, 1}, {2, 1, 1, 0, 1}, {1, 1, 2, 0, 1}, - {0, 2, 1, 0, 1}, {1, 2, 1, 0, 1}, {2, 2, 1, 0, 1}, {2, 1, 2, 0, 1}, - {0, 0, 1, 2, 2}, {1, 0, 1, 2, 2}, {2, 0, 1, 2, 2}, {0, 1, 2, 2, 2}, - {0, 0, 0, 1, 1}, {1, 0, 0, 1, 1}, {2, 0, 0, 1, 1}, {0, 0, 2, 1, 1}, - {0, 1, 0, 1, 1}, {1, 1, 0, 1, 1}, {2, 1, 0, 1, 1}, {1, 0, 2, 1, 1}, - {0, 2, 0, 1, 1}, {1, 2, 0, 1, 1}, {2, 2, 0, 1, 1}, {2, 0, 2, 1, 1}, - {0, 2, 2, 1, 1}, {1, 2, 2, 1, 1}, {2, 2, 2, 1, 1}, {2, 0, 2, 1, 1}, - {0, 0, 1, 1, 1}, {1, 0, 1, 1, 1}, {2, 0, 1, 1, 1}, {0, 1, 2, 1, 1}, - {0, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {2, 1, 1, 1, 1}, {1, 1, 2, 1, 1}, - {0, 2, 1, 1, 1}, {1, 2, 1, 1, 1}, {2, 2, 1, 1, 1}, {2, 1, 2, 1, 1}, - {0, 1, 1, 2, 2}, {1, 1, 1, 2, 2}, {2, 1, 1, 2, 2}, {1, 1, 2, 2, 2}, - {0, 0, 0, 2, 1}, {1, 0, 0, 2, 1}, {2, 0, 0, 2, 1}, {0, 0, 2, 2, 1}, - {0, 1, 0, 2, 1}, {1, 1, 0, 2, 1}, {2, 1, 0, 2, 1}, {1, 0, 2, 2, 1}, - {0, 2, 0, 2, 1}, {1, 2, 0, 2, 1}, {2, 2, 0, 2, 1}, {2, 0, 2, 2, 1}, - {0, 2, 2, 2, 1}, {1, 2, 2, 2, 1}, {2, 2, 2, 2, 1}, {2, 0, 2, 2, 1}, - {0, 0, 1, 2, 1}, {1, 0, 1, 2, 1}, {2, 0, 1, 2, 1}, {0, 1, 2, 2, 1}, - {0, 1, 1, 2, 1}, {1, 1, 1, 2, 1}, {2, 1, 1, 2, 1}, {1, 1, 2, 2, 1}, - {0, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {2, 2, 1, 2, 1}, {2, 1, 2, 2, 1}, - {0, 2, 1, 2, 2}, {1, 2, 1, 2, 2}, {2, 2, 1, 2, 2}, {2, 1, 2, 2, 2}, - {0, 0, 0, 1, 2}, {1, 0, 0, 1, 2}, {2, 0, 0, 1, 2}, {0, 0, 2, 1, 2}, - {0, 1, 0, 1, 2}, {1, 1, 0, 1, 2}, {2, 1, 0, 1, 2}, {1, 0, 2, 1, 2}, - {0, 2, 0, 1, 2}, {1, 2, 0, 1, 2}, {2, 2, 0, 1, 2}, {2, 0, 2, 1, 2}, - {0, 2, 2, 1, 2}, {1, 2, 2, 1, 2}, {2, 2, 2, 1, 2}, {2, 0, 2, 1, 2}, - {0, 0, 1, 1, 2}, {1, 0, 1, 1, 2}, {2, 0, 1, 1, 2}, {0, 1, 2, 1, 2}, - {0, 1, 1, 1, 2}, {1, 1, 1, 1, 2}, {2, 1, 1, 1, 2}, {1, 1, 2, 1, 2}, - {0, 2, 1, 1, 2}, {1, 2, 1, 1, 2}, {2, 2, 1, 1, 2}, {2, 1, 2, 1, 2}, - {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2}, -}; - -// packed trit-value for every unpacked trit-quintuplet -// indexed by [high][][][][low] -static const uint8_t integer_of_trits[3][3][3][3][3] = { - { - { - { - {0, 1, 2,}, - {4, 5, 6,}, - {8, 9, 10,}, - }, - { - {16, 17, 18,}, - {20, 21, 22,}, - {24, 25, 26,}, - }, - { - {3, 7, 15,}, - {19, 23, 27,}, - {12, 13, 14,}, - }, - }, - { - { - {32, 33, 34,}, - {36, 37, 38,}, - {40, 41, 42,}, - }, - { - {48, 49, 50,}, - {52, 53, 54,}, - {56, 57, 58,}, - }, - { - {35, 39, 47,}, - {51, 55, 59,}, - {44, 45, 46,}, - }, - }, - { - { - {64, 65, 66,}, - {68, 69, 70,}, - {72, 73, 74,}, - }, - { - {80, 81, 82,}, - {84, 85, 86,}, - {88, 89, 90,}, - }, - { - {67, 71, 79,}, - {83, 87, 91,}, - {76, 77, 78,}, - }, - }, - }, - { - { - { - {128, 129, 130,}, - {132, 133, 134,}, - {136, 137, 138,}, - }, - { - {144, 145, 146,}, - {148, 149, 150,}, - {152, 153, 154,}, - }, - { - {131, 135, 143,}, - {147, 151, 155,}, - {140, 141, 142,}, - }, - }, - { - { - {160, 161, 162,}, - {164, 165, 166,}, - {168, 169, 170,}, - }, - { - {176, 177, 178,}, - {180, 181, 182,}, - {184, 185, 186,}, - }, - { - {163, 167, 175,}, - {179, 183, 187,}, - {172, 173, 174,}, - }, - }, - { - { - {192, 193, 194,}, - {196, 197, 198,}, - {200, 201, 202,}, - }, - { - {208, 209, 210,}, - {212, 213, 214,}, - {216, 217, 218,}, - }, - { - {195, 199, 207,}, - {211, 215, 219,}, - {204, 205, 206,}, - }, - }, - }, - { - { - { - {96, 97, 98,}, - {100, 101, 102,}, - {104, 105, 106,}, - }, - { - {112, 113, 114,}, - {116, 117, 118,}, - {120, 121, 122,}, - }, - { - {99, 103, 111,}, - {115, 119, 123,}, - {108, 109, 110,}, - }, - }, - { - { - {224, 225, 226,}, - {228, 229, 230,}, - {232, 233, 234,}, - }, - { - {240, 241, 242,}, - {244, 245, 246,}, - {248, 249, 250,}, - }, - { - {227, 231, 239,}, - {243, 247, 251,}, - {236, 237, 238,}, - }, - }, - { - { - {28, 29, 30,}, - {60, 61, 62,}, - {92, 93, 94,}, - }, - { - {156, 157, 158,}, - {188, 189, 190,}, - {220, 221, 222,}, - }, - { - {31, 63, 127,}, - {159, 191, 255,}, - {252, 253, 254,}, - }, - }, - }, -}; - - - -void find_number_of_bits_trits_quints(int quantization_level, int *bits, int *trits, int *quints) -{ - *bits = 0; - *trits = 0; - *quints = 0; - switch (quantization_level) - { - case QUANT_2: - *bits = 1; - break; - case QUANT_3: - *bits = 0; - *trits = 1; - break; - case QUANT_4: - *bits = 2; - break; - case QUANT_5: - *bits = 0; - *quints = 1; - break; - case QUANT_6: - *bits = 1; - *trits = 1; - break; - case QUANT_8: - *bits = 3; - break; - case QUANT_10: - *bits = 1; - *quints = 1; - break; - case QUANT_12: - *bits = 2; - *trits = 1; - break; - case QUANT_16: - *bits = 4; - break; - case QUANT_20: - *bits = 2; - *quints = 1; - break; - case QUANT_24: - *bits = 3; - *trits = 1; - break; - case QUANT_32: - *bits = 5; - break; - case QUANT_40: - *bits = 3; - *quints = 1; - break; - case QUANT_48: - *bits = 4; - *trits = 1; - break; - case QUANT_64: - *bits = 6; - break; - case QUANT_80: - *bits = 4; - *quints = 1; - break; - case QUANT_96: - *bits = 5; - *trits = 1; - break; - case QUANT_128: - *bits = 7; - break; - case QUANT_160: - *bits = 5; - *quints = 1; - break; - case QUANT_192: - *bits = 6; - *trits = 1; - break; - case QUANT_256: - *bits = 8; - break; - } -} - - -// routine to write up to 8 bits -static inline void write_bits(int value, int bitcount, int bitoffset, uint8_t * ptr) -{ - int mask = (1 << bitcount) - 1; - value &= mask; - ptr += bitoffset >> 3; - bitoffset &= 7; - value <<= bitoffset; - mask <<= bitoffset; - mask = ~mask; - - ptr[0] &= mask; - ptr[0] |= value; - ptr[1] &= mask >> 8; - ptr[1] |= value >> 8; -} - - -// routine to read up to 8 bits -static inline int read_bits(int bitcount, int bitoffset, const uint8_t * ptr) -{ - int mask = (1 << bitcount) - 1; - ptr += bitoffset >> 3; - bitoffset &= 7; - int value = ptr[0] | (ptr[1] << 8); - value >>= bitoffset; - value &= mask; - return value; -} - - - - -void encode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset) -{ - int i; - uint8_t lowparts[64]; - uint8_t highparts[69]; // 64 elements + 5 elements for padding - uint8_t tq_blocks[22]; // trit-blocks or quint-blocks - - int bits, trits, quints; - find_number_of_bits_trits_quints(quantization_level, &bits, &trits, &quints); - - for (i = 0; i < elements; i++) - { - lowparts[i] = input_data[i] & ((1 << bits) - 1); - highparts[i] = input_data[i] >> bits; - } - for (i = elements; i < elements + 5; i++) - highparts[i] = 0; // padding before we start constructing trit-blocks or quint-blocks - - // construct trit-blocks or quint-blocks as necessary - if (trits) - { - int trit_blocks = (elements + 4) / 5; - for (i = 0; i < trit_blocks; i++) - tq_blocks[i] = integer_of_trits[highparts[5 * i + 4]][highparts[5 * i + 3]][highparts[5 * i + 2]][highparts[5 * i + 1]][highparts[5 * i]]; - } - if (quints) - { - int quint_blocks = (elements + 2) / 3; - for (i = 0; i < quint_blocks; i++) - tq_blocks[i] = integer_of_quints[highparts[3 * i + 2]][highparts[3 * i + 1]][highparts[3 * i]]; - } - - // then, write out the actual bits. - int lcounter = 0; - int hcounter = 0; - for (i = 0; i < elements; i++) - { - write_bits(lowparts[i], bits, bit_offset, output_data); - bit_offset += bits; - if (trits) - { - static const int bits_to_write[5] = { 2, 2, 1, 2, 1 }; - static const int block_shift[5] = { 0, 2, 4, 5, 7 }; - static const int next_lcounter[5] = { 1, 2, 3, 4, 0 }; - static const int hcounter_incr[5] = { 0, 0, 0, 0, 1 }; - write_bits(tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data); - bit_offset += bits_to_write[lcounter]; - hcounter += hcounter_incr[lcounter]; - lcounter = next_lcounter[lcounter]; - } - if (quints) - { - static const int bits_to_write[3] = { 3, 2, 2 }; - static const int block_shift[3] = { 0, 3, 5 }; - static const int next_lcounter[3] = { 1, 2, 0 }; - static const int hcounter_incr[3] = { 0, 0, 1 }; - write_bits(tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data); - bit_offset += bits_to_write[lcounter]; - hcounter += hcounter_incr[lcounter]; - lcounter = next_lcounter[lcounter]; - } - } -} - - - - -void decode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset) -{ - int i; - // note: due to how the trit/quint-block unpacking is done in this function, - // we may write more temporary results than the number of outputs - // The maximum actual number of results is 64 bit, but we keep 4 additional elements - // of padding. - uint8_t results[68]; - uint8_t tq_blocks[22]; // trit-blocks or quint-blocks - - int bits, trits, quints; - find_number_of_bits_trits_quints(quantization_level, &bits, &trits, &quints); - - int lcounter = 0; - int hcounter = 0; - - // trit-blocks or quint-blocks must be zeroed out before we collect them in the loop below. - for (i = 0; i < 22; i++) - tq_blocks[i] = 0; - - // collect bits for each element, as well as bits for any trit-blocks and quint-blocks. - for (i = 0; i < elements; i++) - { - results[i] = read_bits(bits, bit_offset, input_data); - bit_offset += bits; - if (trits) - { - static const int bits_to_read[5] = { 2, 2, 1, 2, 1 }; - static const int block_shift[5] = { 0, 2, 4, 5, 7 }; - static const int next_lcounter[5] = { 1, 2, 3, 4, 0 }; - static const int hcounter_incr[5] = { 0, 0, 0, 0, 1 }; - int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data); - bit_offset += bits_to_read[lcounter]; - tq_blocks[hcounter] |= tdata << block_shift[lcounter]; - hcounter += hcounter_incr[lcounter]; - lcounter = next_lcounter[lcounter]; - } - if (quints) - { - static const int bits_to_read[3] = { 3, 2, 2 }; - static const int block_shift[3] = { 0, 3, 5 }; - static const int next_lcounter[3] = { 1, 2, 0 }; - static const int hcounter_incr[3] = { 0, 0, 1 }; - int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data); - bit_offset += bits_to_read[lcounter]; - tq_blocks[hcounter] |= tdata << block_shift[lcounter]; - hcounter += hcounter_incr[lcounter]; - lcounter = next_lcounter[lcounter]; - } - } - - - // unpack trit-blocks or quint-blocks as needed - if (trits) - { - int trit_blocks = (elements + 4) / 5; - for (i = 0; i < trit_blocks; i++) - { - const uint8_t *tritptr = trits_of_integer[tq_blocks[i]]; - results[5 * i] |= tritptr[0] << bits; - results[5 * i + 1] |= tritptr[1] << bits; - results[5 * i + 2] |= tritptr[2] << bits; - results[5 * i + 3] |= tritptr[3] << bits; - results[5 * i + 4] |= tritptr[4] << bits; - } - } - - if (quints) - { - int quint_blocks = (elements + 2) / 3; - for (i = 0; i < quint_blocks; i++) - { - const uint8_t *quintptr = quints_of_integer[tq_blocks[i]]; - results[3 * i] |= quintptr[0] << bits; - results[3 * i + 1] |= quintptr[1] << bits; - results[3 * i + 2] |= quintptr[2] << bits; - } - } - - for (i = 0; i < elements; i++) - output_data[i] = results[i]; -} - - - - -int compute_ise_bitcount(int items, quantization_method quant) -{ - switch (quant) - { - case QUANT_2: - return items; - case QUANT_3: - return (8 * items + 4) / 5; - case QUANT_4: - return 2 * items; - case QUANT_5: - return (7 * items + 2) / 3; - case QUANT_6: - return (13 * items + 4) / 5; - case QUANT_8: - return 3 * items; - case QUANT_10: - return (10 * items + 2) / 3; - case QUANT_12: - return (18 * items + 4) / 5; - case QUANT_16: - return items * 4; - case QUANT_20: - return (13 * items + 2) / 3; - case QUANT_24: - return (23 * items + 4) / 5; - case QUANT_32: - return 5 * items; - case QUANT_40: - return (16 * items + 2) / 3; - case QUANT_48: - return (28 * items + 4) / 5; - case QUANT_64: - return 6 * items; - case QUANT_80: - return (19 * items + 2) / 3; - case QUANT_96: - return (33 * items + 4) / 5; - case QUANT_128: - return 7 * items; - case QUANT_160: - return (22 * items + 2) / 3; - case QUANT_192: - return (38 * items + 4) / 5; - case QUANT_256: - return 8 * items; - default: - return 100000; - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_kmeans_partitioning.cpp b/3rdparty/bimg/3rdparty/astc/astc_kmeans_partitioning.cpp deleted file mode 100644 index 13fd4abc40b..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_kmeans_partitioning.cpp +++ /dev/null @@ -1,520 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief approximate k-means cluster partitioning. Do this in 2 stages - * - * 1: basic clustering, a couple of passes just to get a few clusters - * 2: clustering based on line, a few passes until it seems to - * stabilize. - * - * After clustering is done, we use the clustering result to construct - * one bitmap for each partition. We then scan though the partition table, - * counting how well the bitmaps matched. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -// for k++ means, we need pseudo-random numbers, however using random numbers directly -// results in irreproducible encoding results. As such, we will instead -// just supply a handful of numbers from random.org, and apply an algorithm similar -// to XKCD #221. (http://xkcd.com/221/) -// cluster the texels using the k++ means clustering initialization algorithm. - -void kpp_initialize(int xdim, int ydim, int zdim, int partition_count, const imageblock * blk, float4 * cluster_centers) -{ - int i; - - int texels_per_block = xdim * ydim * zdim; - - int cluster_center_samples[4]; - // pick a random sample as first center-point. - cluster_center_samples[0] = 145897 /* number from random.org */ % texels_per_block; - int samples_selected = 1; - - float distances[MAX_TEXELS_PER_BLOCK]; - - // compute the distance to the first point. - int sample = cluster_center_samples[0]; - float4 center_color = float4(blk->work_data[4 * sample], - blk->work_data[4 * sample + 1], - blk->work_data[4 * sample + 2], - blk->work_data[4 * sample + 3]); - - float distance_sum = 0.0f; - for (i = 0; i < texels_per_block; i++) - { - float4 color = float4(blk->work_data[4 * i], - blk->work_data[4 * i + 1], - blk->work_data[4 * i + 2], - blk->work_data[4 * i + 3]); - float4 diff = color - center_color; - float distance = dot(diff, diff); - distance_sum += distance; - distances[i] = distance; - } - - // more numbers from random.org - float cluster_cutoffs[25] = { - 0.952312f, 0.206893f, 0.835984f, 0.507813f, 0.466170f, - 0.872331f, 0.488028f, 0.866394f, 0.363093f, 0.467905f, - 0.812967f, 0.626220f, 0.932770f, 0.275454f, 0.832020f, - 0.362217f, 0.318558f, 0.240113f, 0.009190f, 0.983995f, - 0.566812f, 0.347661f, 0.731960f, 0.156391f, 0.297786f - }; - - while (1) - { - // pick a point in a weighted-random fashion. - float summa = 0.0f; - float distance_cutoff = distance_sum * cluster_cutoffs[samples_selected + 5 * partition_count]; - for (i = 0; i < texels_per_block; i++) - { - summa += distances[i]; - if (summa >= distance_cutoff) - break; - } - sample = i; - if (sample >= texels_per_block) - sample = texels_per_block - 1; - - - cluster_center_samples[samples_selected] = sample; - samples_selected++; - if (samples_selected >= partition_count) - break; - - // update the distances with the new point. - center_color = float4(blk->work_data[4 * sample], blk->work_data[4 * sample + 1], blk->work_data[4 * sample + 2], blk->work_data[4 * sample + 3]); - - distance_sum = 0.0f; - for (i = 0; i < texels_per_block; i++) - { - float4 color = float4(blk->work_data[4 * i], - blk->work_data[4 * i + 1], - blk->work_data[4 * i + 2], - blk->work_data[4 * i + 3]); - float4 diff = color - center_color; - float distance = dot(diff, diff); - distance = MIN(distance, distances[i]); - distance_sum += distance; - distances[i] = distance; - } - } - - // finally, gather up the results. - for (i = 0; i < partition_count; i++) - { - int sample = cluster_center_samples[i]; - float4 color = float4(blk->work_data[4 * sample], - blk->work_data[4 * sample + 1], - blk->work_data[4 * sample + 2], - blk->work_data[4 * sample + 3]); - cluster_centers[i] = color; - } -} - - -// basic K-means clustering: given a set of cluster centers, -// assign each texel to a partition -void basic_kmeans_assign_pass(int xdim, int ydim, int zdim, int partition_count, const imageblock * blk, const float4 * cluster_centers, int *partition_of_texel) -{ - int i, j; - - int texels_per_block = xdim * ydim * zdim; - - float distances[MAX_TEXELS_PER_BLOCK]; - float4 center_color = cluster_centers[0]; - - int texels_per_partition[4]; - - texels_per_partition[0] = texels_per_block; - for (i = 1; i < partition_count; i++) - texels_per_partition[i] = 0; - - - for (i = 0; i < texels_per_block; i++) - { - float4 color = float4(blk->work_data[4 * i], - blk->work_data[4 * i + 1], - blk->work_data[4 * i + 2], - blk->work_data[4 * i + 3]); - float4 diff = color - center_color; - float distance = dot(diff, diff); - distances[i] = distance; - partition_of_texel[i] = 0; - } - - - - for (j = 1; j < partition_count; j++) - { - float4 center_color = cluster_centers[j]; - - for (i = 0; i < texels_per_block; i++) - { - float4 color = float4(blk->work_data[4 * i], - blk->work_data[4 * i + 1], - blk->work_data[4 * i + 2], - blk->work_data[4 * i + 3]); - float4 diff = color - center_color; - float distance = dot(diff, diff); - if (distance < distances[i]) - { - distances[i] = distance; - texels_per_partition[partition_of_texel[i]]--; - texels_per_partition[j]++; - partition_of_texel[i] = j; - } - } - } - - // it is possible to get a situation where one of the partitions ends up - // without any texels. In this case, we assign texel N to partition N; - // this is silly, but ensures that every partition retains at least one texel. - // Reassigning a texel in this manner may cause another partition to go empty, - // so if we actually did a reassignment, we run the whole loop over again. - int problem_case; - do - { - problem_case = 0; - for (i = 0; i < partition_count; i++) - { - if (texels_per_partition[i] == 0) - { - texels_per_partition[partition_of_texel[i]]--; - texels_per_partition[i]++; - partition_of_texel[i] = i; - problem_case = 1; - } - } - } - while (problem_case != 0); - -} - - -// basic k-means clustering: given a set of cluster assignments -// for the texels, find the center position of each cluster. -void basic_kmeans_update(int xdim, int ydim, int zdim, int partition_count, const imageblock * blk, const int *partition_of_texel, float4 * cluster_centers) -{ - int i; - - int texels_per_block = xdim * ydim * zdim; - - float4 color_sum[4]; - int weight_sum[4]; - - for (i = 0; i < partition_count; i++) - { - color_sum[i] = float4(0, 0, 0, 0); - weight_sum[i] = 0; - } - - - // first, find the center-of-gravity in each cluster - for (i = 0; i < texels_per_block; i++) - { - float4 color = float4(blk->work_data[4 * i], - blk->work_data[4 * i + 1], - blk->work_data[4 * i + 2], - blk->work_data[4 * i + 3]); - int part = partition_of_texel[i]; - color_sum[part] = color_sum[part] + color; - weight_sum[part]++; - } - - for (i = 0; i < partition_count; i++) - { - cluster_centers[i] = color_sum[i] * (1.0f / weight_sum[i]); - } -} - - - - -// after a few rounds of k-means-clustering, we should have a set of 2, 3 or 4 partitions; -// we then turn this set into 2, 3 or 4 bitmaps. Then, for each of the 1024 partitions, -// we try to match the bitmaps as well as possible. - - - - -static inline int bitcount(uint64_t p) -{ - if (sizeof(void *) > 4) - { - uint64_t mask1 = 0x5555555555555555ULL; - uint64_t mask2 = 0x3333333333333333ULL; - uint64_t mask3 = 0x0F0F0F0F0F0F0F0FULL; - // best-known algorithm for 64-bit bitcount, assuming 64-bit processor - // should probably be adapted for use with 32-bit processors and/or processors - // with a POPCNT instruction, but leave that for later. - p -= (p >> 1) & mask1; - p = (p & mask2) + ((p >> 2) & mask2); - p += p >> 4; - p &= mask3; - p *= 0x0101010101010101ULL; - p >>= 56; - return (int)p; - } - else - { - // on 32-bit processor, split the 64-bit input argument in two, - // and bitcount each half separately. - uint32_t p1 = (uint32_t) p; - uint32_t p2 = (uint32_t) (p >> 32); - uint32_t mask1 = 0x55555555U; - uint32_t mask2 = 0x33333333U; - uint32_t mask3 = 0x0F0F0F0FU; - p1 = p1 - ((p1 >> 1) & mask1); - p2 = p2 - ((p2 >> 1) & mask1); - p1 = (p1 & mask2) + ((p1 >> 2) & mask2); - p2 = (p2 & mask2) + ((p2 >> 2) & mask2); - p1 += p1 >> 4; - p2 += p2 >> 4; - p1 &= mask3; - p2 &= mask3; - p1 += p2; - p1 *= 0x01010101U; - p1 >>= 24; - return (int)p1; - } -} - - -// compute the bit-mismatch for a partitioning in 2-partition mode -static inline int partition_mismatch2(uint64_t a0, uint64_t a1, uint64_t b0, uint64_t b1) -{ - int v1 = bitcount(a0 ^ b0) + bitcount(a1 ^ b1); - int v2 = bitcount(a0 ^ b1) + bitcount(a1 ^ b0); - return MIN(v1, v2); -} - - -// compute the bit-mismatch for a partitioning in 3-partition mode -static inline int partition_mismatch3(uint64_t a0, uint64_t a1, uint64_t a2, uint64_t b0, uint64_t b1, uint64_t b2) -{ - int p00 = bitcount(a0 ^ b0); - int p01 = bitcount(a0 ^ b1); - int p02 = bitcount(a0 ^ b2); - - int p10 = bitcount(a1 ^ b0); - int p11 = bitcount(a1 ^ b1); - int p12 = bitcount(a1 ^ b2); - - int p20 = bitcount(a2 ^ b0); - int p21 = bitcount(a2 ^ b1); - int p22 = bitcount(a2 ^ b2); - - int s0 = p11 + p22; - int s1 = p12 + p21; - int v0 = MIN(s0, s1) + p00; - - int s2 = p10 + p22; - int s3 = p12 + p20; - int v1 = MIN(s2, s3) + p01; - - int s4 = p10 + p21; - int s5 = p11 + p20; - int v2 = MIN(s4, s5) + p02; - - if (v1 < v0) - v0 = v1; - if (v2 < v0) - v0 = v2; - - // 9 add, 5 MIN - - return v0; -} - -static inline int MIN3(int a, int b, int c) -{ - int d = MIN(a, b); - return MIN(c, d); -} - -// compute the bit-mismatch for a partitioning in 4-partition mode -static inline int partition_mismatch4(uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3) -{ - int p00 = bitcount(a0 ^ b0); - int p01 = bitcount(a0 ^ b1); - int p02 = bitcount(a0 ^ b2); - int p03 = bitcount(a0 ^ b3); - - int p10 = bitcount(a1 ^ b0); - int p11 = bitcount(a1 ^ b1); - int p12 = bitcount(a1 ^ b2); - int p13 = bitcount(a1 ^ b3); - - int p20 = bitcount(a2 ^ b0); - int p21 = bitcount(a2 ^ b1); - int p22 = bitcount(a2 ^ b2); - int p23 = bitcount(a2 ^ b3); - - int p30 = bitcount(a3 ^ b0); - int p31 = bitcount(a3 ^ b1); - int p32 = bitcount(a3 ^ b2); - int p33 = bitcount(a3 ^ b3); - - int mx23 = MIN(p22 + p33, p23 + p32); - int mx13 = MIN(p21 + p33, p23 + p31); - int mx12 = MIN(p21 + p32, p22 + p31); - int mx03 = MIN(p20 + p33, p23 + p30); - int mx02 = MIN(p20 + p32, p22 + p30); - int mx01 = MIN(p21 + p30, p20 + p31); - - int v0 = p00 + MIN3(p11 + mx23, p12 + mx13, p13 + mx12); - int v1 = p01 + MIN3(p10 + mx23, p12 + mx03, p13 + mx02); - int v2 = p02 + MIN3(p11 + mx03, p10 + mx13, p13 + mx01); - int v3 = p03 + MIN3(p11 + mx02, p12 + mx01, p10 + mx12); - - int x0 = MIN(v0, v1); - int x1 = MIN(v2, v3); - return MIN(x0, x1); - - // 16 bitcount, 17 MIN, 28 ADD -} - - - -void count_partition_mismatch_bits(int xdim, int ydim, int zdim, int partition_count, const uint64_t bitmaps[4], int bitcounts[PARTITION_COUNT]) -{ - int i; - const partition_info *pi = get_partition_table(xdim, ydim, zdim, partition_count); - - if (partition_count == 2) - { - uint64_t bm0 = bitmaps[0]; - uint64_t bm1 = bitmaps[1]; - for (i = 0; i < PARTITION_COUNT; i++) - { - if (pi->partition_count == 2) - { - bitcounts[i] = partition_mismatch2(bm0, bm1, pi->coverage_bitmaps[0], pi->coverage_bitmaps[1]); - } - else - bitcounts[i] = 255; - pi++; - } - } - else if (partition_count == 3) - { - uint64_t bm0 = bitmaps[0]; - uint64_t bm1 = bitmaps[1]; - uint64_t bm2 = bitmaps[2]; - for (i = 0; i < PARTITION_COUNT; i++) - { - if (pi->partition_count == 3) - { - bitcounts[i] = partition_mismatch3(bm0, bm1, bm2, pi->coverage_bitmaps[0], pi->coverage_bitmaps[1], pi->coverage_bitmaps[2]); - } - else - bitcounts[i] = 255; - pi++; - } - } - else if (partition_count == 4) - { - uint64_t bm0 = bitmaps[0]; - uint64_t bm1 = bitmaps[1]; - uint64_t bm2 = bitmaps[2]; - uint64_t bm3 = bitmaps[3]; - for (i = 0; i < PARTITION_COUNT; i++) - { - if (pi->partition_count == 4) - { - bitcounts[i] = partition_mismatch4(bm0, bm1, bm2, bm3, pi->coverage_bitmaps[0], pi->coverage_bitmaps[1], pi->coverage_bitmaps[2], pi->coverage_bitmaps[3]); - } - else - bitcounts[i] = 255; - pi++; - } - } - -} - - -// counting-sort on the mismatch-bits, thereby -// sorting the partitions into an ordering. - -void get_partition_ordering_by_mismatch_bits(const int mismatch_bits[PARTITION_COUNT], int partition_ordering[PARTITION_COUNT]) -{ - int i; - - int mscount[256]; - for (i = 0; i < 256; i++) - mscount[i] = 0; - - for (i = 0; i < PARTITION_COUNT; i++) - mscount[mismatch_bits[i]]++; - - int summa = 0; - for (i = 0; i < 256; i++) - { - int cnt = mscount[i]; - mscount[i] = summa; - summa += cnt; - } - - for (i = 0; i < PARTITION_COUNT; i++) - { - int idx = mscount[mismatch_bits[i]]++; - partition_ordering[idx] = i; - } -} - - - - -void kmeans_compute_partition_ordering(int xdim, int ydim, int zdim, int partition_count, const imageblock * blk, int *ordering) -{ - int i; - - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - - float4 cluster_centers[4]; - int partition_of_texel[MAX_TEXELS_PER_BLOCK]; - - // 3 passes of plain k-means partitioning - for (i = 0; i < 3; i++) - { - if (i == 0) - kpp_initialize(xdim, ydim, zdim, partition_count, blk, cluster_centers); - else - basic_kmeans_update(xdim, ydim, zdim, partition_count, blk, partition_of_texel, cluster_centers); - - basic_kmeans_assign_pass(xdim, ydim, zdim, partition_count, blk, cluster_centers, partition_of_texel); - } - - // at this point, we have a near-ideal partitioning. - - // construct bitmaps - uint64_t bitmaps[4]; - for (i = 0; i < 4; i++) - bitmaps[i] = 0ULL; - - int texels_to_process = bsd->texelcount_for_bitmap_partitioning; - for (i = 0; i < texels_to_process; i++) - { - int idx = bsd->texels_for_bitmap_partitioning[i]; - bitmaps[partition_of_texel[idx]] |= 1ULL << i; - } - - int bitcounts[PARTITION_COUNT]; - // for each entry in the partition table, count bits of partition-mismatch. - count_partition_mismatch_bits(xdim, ydim, zdim, partition_count, bitmaps, bitcounts); - - // finally, sort the partitions by bits-of-partition-mismatch - get_partition_ordering_by_mismatch_bits(bitcounts, ordering); - -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_lib.cpp b/3rdparty/bimg/3rdparty/astc/astc_lib.cpp deleted file mode 100644 index f587236e54d..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_lib.cpp +++ /dev/null @@ -1,681 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * @author Andrew Willmott - * - * @brief Library api for astc codec, to be used as an alternative to astc_toplevel.cpp - */ -/*----------------------------------------------------------------------------*/ - - -#include "astc_lib.h" - -#include "astc_codec_internals.h" - -#include <math.h> -#include <stdio.h> - -// Globals declared in astc_codec_internals.h -int perform_srgb_transform = 0; -int alpha_force_use_of_hdr = 0; -int rgb_force_use_of_hdr = 0; -int print_tile_errors = 0; - -#ifdef DEBUG_PRINT_DIAGNOSTICS - int print_diagnostics = 0; - int diagnostics_tile = -1; -#endif - -// ASTC code expects this to be defined -void astc_codec_internal_error(const char* filename, int line) -{ - fprintf(stderr, "ASTC encode error @ %s:%d\n", filename, line); -} - -// @todo add HDR variants - -namespace -{ - static bool s_tables_initialised = false; - - inline void init_tables() - { - if (!s_tables_initialised) - { - prepare_angular_tables(); - build_quantization_mode_table(); - - s_tables_initialised = true; - } - } - - const swizzlepattern k_swizzles[] = - { - { 0, 1, 2, 3 }, // ASTC_RGBA - { 2, 1, 0, 3 }, // ASTC_BGRA - }; - - void alloc_temp_buffers(compress_symbolic_block_buffers* temp_buffers) - { - temp_buffers->ewb = new error_weight_block; - temp_buffers->ewbo = new error_weight_block_orig; - temp_buffers->tempblocks = new symbolic_compressed_block[4]; - temp_buffers->temp = new imageblock; - - temp_buffers->planes2 = new compress_fixed_partition_buffers; - temp_buffers->planes2->ei1 = new endpoints_and_weights; - temp_buffers->planes2->ei2 = new endpoints_and_weights; - temp_buffers->planes2->eix1 = new endpoints_and_weights[MAX_DECIMATION_MODES]; - temp_buffers->planes2->eix2 = new endpoints_and_weights[MAX_DECIMATION_MODES]; - temp_buffers->planes2->decimated_quantized_weights = new float[2 * MAX_DECIMATION_MODES * MAX_WEIGHTS_PER_BLOCK]; - temp_buffers->planes2->decimated_weights = new float[2 * MAX_DECIMATION_MODES * MAX_WEIGHTS_PER_BLOCK]; - temp_buffers->planes2->flt_quantized_decimated_quantized_weights = new float[2 * MAX_WEIGHT_MODES * MAX_WEIGHTS_PER_BLOCK]; - temp_buffers->planes2->u8_quantized_decimated_quantized_weights = new uint8_t[2 * MAX_WEIGHT_MODES * MAX_WEIGHTS_PER_BLOCK]; - temp_buffers->plane1 = temp_buffers->planes2; - } - - void free_temp_buffers(compress_symbolic_block_buffers* temp_buffers) - { - delete[] temp_buffers->planes2->decimated_quantized_weights; - delete[] temp_buffers->planes2->decimated_weights; - delete[] temp_buffers->planes2->flt_quantized_decimated_quantized_weights; - delete[] temp_buffers->planes2->u8_quantized_decimated_quantized_weights; - delete[] temp_buffers->planes2->eix1; - delete[] temp_buffers->planes2->eix2; - delete temp_buffers->planes2->ei1; - delete temp_buffers->planes2->ei2; - delete temp_buffers->planes2; - - delete[] temp_buffers->tempblocks; - delete temp_buffers->temp; - delete temp_buffers->ewbo; - delete temp_buffers->ewb; - } - - - // More direct version of the astc_codec_image routine, which operates on a - // more conventional 2D image layout. Doesn't support padding, so - // mean_stdev_radius and alpha_radius etc. must be zero. - void to_imageblock - ( - imageblock* pb, - const uint8_t* src_data, - int src_stride, - int xpos, - int ypos, - int xsize, - int ysize, - int xdim, - int ydim, - swizzlepattern swz, - bool srgb - ) - { - float* fptr = pb->orig_data; - - pb->xpos = xpos; - pb->ypos = ypos; - pb->zpos = 0; - - float data[6]; - data[4] = 0; - data[5] = 1; - - for (int y = 0; y < ydim; y++) - { - for (int x = 0; x < xdim; x++) - { - int xi = xpos + x; - int yi = ypos + y; - - if (xi >= xsize) - xi = xsize - 1; - if (yi >= ysize) - yi = ysize - 1; - - int offset = src_stride * yi + 4 * xi; - - int r = src_data[offset + 0]; - int g = src_data[offset + 1]; - int b = src_data[offset + 2]; - int a = src_data[offset + 3]; - - data[0] = r / 255.0f; - data[1] = g / 255.0f; - data[2] = b / 255.0f; - data[3] = a / 255.0f; - - fptr[0] = data[swz.r]; - fptr[1] = data[swz.g]; - fptr[2] = data[swz.b]; - fptr[3] = data[swz.a]; - - fptr += 4; - } - } - - // perform sRGB-to-linear transform on input data, if requested. - int pixelcount = xdim * ydim; - - if (srgb) - { - fptr = pb->orig_data; - - for (int i = 0; i < pixelcount; i++) - { - float r = fptr[0]; - float g = fptr[1]; - float b = fptr[2]; - - if (r <= 0.04045f) - r = r * (1.0f / 12.92f); - else if (r <= 1) - r = pow((r + 0.055f) * (1.0f / 1.055f), 2.4f); - - if (g <= 0.04045f) - g = g * (1.0f / 12.92f); - else if (g <= 1) - g = pow((g + 0.055f) * (1.0f / 1.055f), 2.4f); - - if (b <= 0.04045f) - b = b * (1.0f / 12.92f); - else if (b <= 1) - b = pow((b + 0.055f) * (1.0f / 1.055f), 2.4f); - - fptr[0] = r; - fptr[1] = g; - fptr[2] = b; - - fptr += 4; - } - } - - for (int i = 0; i < pixelcount; i++) - { - pb->rgb_lns [i] = 0; - pb->alpha_lns[i] = 0; - pb->nan_texel[i] = 0; - } - - imageblock_initialize_work_from_orig(pb, pixelcount); - - update_imageblock_flags(pb, xdim, ydim, 1); - } - - void encode_astc - ( - const uint8_t* src, - int src_stride, - swizzlepattern src_swz, - int xsize, - int ysize, - int xdim, - int ydim, - const error_weighting_params* ewp, - astc_decode_mode decode_mode, - uint8_t* dst - ) - { - int xblocks = (xsize + xdim - 1) / xdim; - int yblocks = (ysize + ydim - 1) / ydim; - - get_block_size_descriptor(xdim, ydim, 1); - get_partition_table(xdim, ydim, 1, 0); - - imageblock pb; - - compress_symbolic_block_buffers temp_buffers; - alloc_temp_buffers(&temp_buffers); - - astc_codec_image image_info = { nullptr, nullptr, xsize, ysize, 1, 0 }; - - for (int y = 0; y < yblocks; y++) - for (int x = 0; x < xblocks; x++) - { - to_imageblock(&pb, src, src_stride, x * xdim, y * ydim, xsize, ysize, xdim, ydim, src_swz, decode_mode == DECODE_LDR_SRGB); - - symbolic_compressed_block scb; - compress_symbolic_block(&image_info, decode_mode, xdim, ydim, 1, ewp, &pb, &scb, &temp_buffers); - - physical_compressed_block pcb = symbolic_to_physical(xdim, ydim, 1, &scb); - - uint8_t* dst_block = dst + (y * xblocks + x) * 16; - - *(physical_compressed_block*) dst_block = pcb; - } - - free_temp_buffers(&temp_buffers); - } - - void init_ewp(error_weighting_params& ewp) - { - ewp.rgb_power = 1.0f; - ewp.alpha_power = 1.0f; - ewp.rgb_base_weight = 1.0f; - ewp.alpha_base_weight = 1.0f; - ewp.rgb_mean_weight = 0.0f; - ewp.rgb_stdev_weight = 0.0f; - ewp.alpha_mean_weight = 0.0f; - ewp.alpha_stdev_weight = 0.0f; - - ewp.rgb_mean_and_stdev_mixing = 0.0f; - ewp.mean_stdev_radius = 0; - ewp.enable_rgb_scale_with_alpha = 0; - ewp.alpha_radius = 0; - - ewp.block_artifact_suppression = 0.0f; - ewp.rgba_weights[0] = 1.0f; - ewp.rgba_weights[1] = 1.0f; - ewp.rgba_weights[2] = 1.0f; - ewp.rgba_weights[3] = 1.0f; - ewp.ra_normal_angular_scale = 0; - } - - void setup_ewp(ASTC_COMPRESS_MODE mode, int ydim, int xdim, error_weighting_params& ewp) - { - float oplimit_autoset = 0.0; - float dblimit_autoset_2d = 0.0; - float bmc_autoset = 0.0; - float mincorrel_autoset = 0.0; - - int plimit_autoset = -1; - int maxiters_autoset = 0; - int pcdiv = 1; - - float log10_texels_2d = log((float)(xdim * ydim)) / log(10.0f); - - if (mode == ASTC_COMPRESS_VERY_FAST) - { - plimit_autoset = 2; - oplimit_autoset = 1.0; - dblimit_autoset_2d = MAX(70 - 35 * log10_texels_2d, 53 - 19 * log10_texels_2d); - bmc_autoset = 25; - mincorrel_autoset = 0.5; - maxiters_autoset = 1; - - switch (ydim) - { - case 4: - pcdiv = 240; - break; - case 5: - pcdiv = 56; - break; - case 6: - pcdiv = 64; - break; - case 8: - pcdiv = 47; - break; - case 10: - pcdiv = 36; - break; - case 12: - pcdiv = 30; - break; - default: - pcdiv = 30; - break; - } - } - else if (mode == ASTC_COMPRESS_FAST) - { - plimit_autoset = 4; - oplimit_autoset = 1.0; - mincorrel_autoset = 0.5; - dblimit_autoset_2d = MAX(85 - 35 * log10_texels_2d, 63 - 19 * log10_texels_2d); - bmc_autoset = 50; - maxiters_autoset = 1; - - switch (ydim) - { - case 4: - pcdiv = 60; - break; - case 5: - pcdiv = 27; - break; - case 6: - pcdiv = 30; - break; - case 8: - pcdiv = 24; - break; - case 10: - pcdiv = 16; - break; - case 12: - pcdiv = 20; - break; - default: - pcdiv = 20; - break; - }; - } - else if (mode == ASTC_COMPRESS_MEDIUM) - { - plimit_autoset = 25; - oplimit_autoset = 1.2f; - mincorrel_autoset = 0.75f; - dblimit_autoset_2d = MAX(95 - 35 * log10_texels_2d, 70 - 19 * log10_texels_2d); - bmc_autoset = 75; - maxiters_autoset = 2; - - switch (ydim) - { - case 4: - pcdiv = 25; - break; - case 5: - pcdiv = 15; - break; - case 6: - pcdiv = 15; - break; - case 8: - pcdiv = 10; - break; - case 10: - pcdiv = 8; - break; - case 12: - pcdiv = 6; - break; - default: - pcdiv = 6; - break; - }; - } - else if (mode == ASTC_COMPRESS_THOROUGH) - { - plimit_autoset = 100; - oplimit_autoset = 2.5f; - mincorrel_autoset = 0.95f; - dblimit_autoset_2d = MAX(105 - 35 * log10_texels_2d, 77 - 19 * log10_texels_2d); - bmc_autoset = 95; - maxiters_autoset = 4; - - switch (ydim) - { - case 4: - pcdiv = 12; - break; - case 5: - pcdiv = 7; - break; - case 6: - pcdiv = 7; - break; - case 8: - pcdiv = 5; - break; - case 10: - pcdiv = 4; - break; - case 12: - pcdiv = 3; - break; - default: - pcdiv = 3; - break; - }; - } - else if (mode == ASTC_COMPRESS_EXHAUSTIVE) - { - plimit_autoset = PARTITION_COUNT; - oplimit_autoset = 1000.0f; - mincorrel_autoset = 0.99f; - dblimit_autoset_2d = 999.0f; - bmc_autoset = 100; - maxiters_autoset = 4; - - switch (ydim) - { - case 4: - pcdiv = 3; - break; - case 5: - pcdiv = 1; - break; - case 6: - pcdiv = 1; - break; - case 8: - pcdiv = 1; - break; - case 10: - pcdiv = 1; - break; - case 12: - pcdiv = 1; - break; - default: - pcdiv = 1; - break; - } - } - - int partitions_to_test = plimit_autoset; - float dblimit_2d = dblimit_autoset_2d; - float oplimit = oplimit_autoset; - float mincorrel = mincorrel_autoset; - - int maxiters = maxiters_autoset; - ewp.max_refinement_iters = maxiters; - - ewp.block_mode_cutoff = bmc_autoset / 100.0f; - - float texel_avg_error_limit_2d; - - if (rgb_force_use_of_hdr == 0) - { - texel_avg_error_limit_2d = pow(0.1f, dblimit_2d * 0.1f) * 65535.0f * 65535.0f; - } - else - { - texel_avg_error_limit_2d = 0.0f; - } - ewp.partition_1_to_2_limit = oplimit; - ewp.lowest_correlation_cutoff = mincorrel; - - if (partitions_to_test < 1) - partitions_to_test = 1; - else if (partitions_to_test > PARTITION_COUNT) - partitions_to_test = PARTITION_COUNT; - ewp.partition_search_limit = partitions_to_test; - - ewp.texel_avg_error_limit = texel_avg_error_limit_2d; - - expand_block_artifact_suppression(xdim, ydim, 1, &ewp); - } -} - -size_t astc_compressed_size(int w, int h, int bw, int bh) -{ - int nx = (w + bw - 1) / bw; - int ny = (h + bh - 1) / bh; - - return nx * ny * 16; -} - -void astc_compress -( - int src_width, - int src_height, - const uint8_t* src_data, - ASTC_CHANNELS src_channels, - int src_stride, - - int block_width, - int block_height, - ASTC_COMPRESS_MODE compress_mode, - ASTC_DECODE_MODE decode_mode, - uint8_t* dst_data -) -{ - init_tables(); - - error_weighting_params ewp; - init_ewp(ewp); - setup_ewp(compress_mode, block_width, block_height, ewp); - - if (src_stride == 0) - src_stride = src_width * 4; - - encode_astc - ( - src_data, - src_stride, - k_swizzles[src_channels], - src_width, src_height, - block_width, block_height, - &ewp, - (astc_decode_mode) decode_mode, - dst_data - ); -} - -namespace -{ - // More direct version of the astc_codec_image routine, which operates on a - // more conventional 2D image layout. - void from_imageblock(int xdim, int ydim, const imageblock* pb, bool srgb, swizzlepattern swz, uint8_t* dst_data, int dst_stride) - { - const float* fptr = pb->orig_data; - const uint8_t* nptr = pb->nan_texel; - - for (int y = 0; y < ydim; y++) - { - for (int x = 0; x < xdim; x++) - { - if (*nptr) - { - // NaN-pixel, but we can't display it. Display purple instead. - dst_data[4 * x + swz.r] = 0xFF; - dst_data[4 * x + swz.g] = 0x00; - dst_data[4 * x + swz.b] = 0xFF; - dst_data[4 * x + swz.a] = 0xFF; - } - else - { - float r = fptr[0]; - float g = fptr[1]; - float b = fptr[2]; - float a = fptr[3]; - - if (srgb) - { - if (r <= 0.0031308f) - r = r * 12.92f; - else if (r <= 1) - r = 1.055f * pow(r, (1.0f / 2.4f)) - 0.055f; - - if (g <= 0.0031308f) - g = g * 12.92f; - else if (g <= 1) - g = 1.055f * pow(g, (1.0f / 2.4f)) - 0.055f; - - if (b <= 0.0031308f) - b = b * 12.92f; - else if (b <= 1) - b = 1.055f * pow(b, (1.0f / 2.4f)) - 0.055f; - } - - // clamp to [0,1] - if (r > 1.0f) - r = 1.0f; - if (g > 1.0f) - g = 1.0f; - if (b > 1.0f) - b = 1.0f; - if (a > 1.0f) - a = 1.0f; - - // pack the data - dst_data[4 * x + swz.r] = uint8_t(floorf(r * 255.0f + 0.5f)); - dst_data[4 * x + swz.g] = uint8_t(floorf(g * 255.0f + 0.5f)); - dst_data[4 * x + swz.b] = uint8_t(floorf(b * 255.0f + 0.5f)); - dst_data[4 * x + swz.a] = uint8_t(floorf(a * 255.0f + 0.5f)); - } - - fptr += 4; - nptr++; - } - - dst_data += dst_stride; - } - } -} - -void astc_decompress -( - const uint8_t* src_data, - int xdim, - int ydim, - ASTC_DECODE_MODE decode_mode, - - int xsize, - int ysize, - uint8_t* dst_data, - ASTC_CHANNELS dst_channels, - int dst_stride -) -{ - init_tables(); - - int xblocks = (xsize + xdim - 1) / xdim; - int yblocks = (ysize + ydim - 1) / ydim; - - if (dst_stride == 0) - dst_stride = 4 * xsize; - - imageblock pb; - - for (int y = 0; y < yblocks; y++) - { - int ypos = y * ydim; - int clamp_ydim = MIN(ysize - ypos, ydim); - - uint8_t* dst_row = dst_data + ypos * dst_stride; - - for (int x = 0; x < xblocks; x++) - { - int xpos = x * xdim; - int clamp_xdim = MIN(xsize - xpos, xdim); - - physical_compressed_block pcb = *(const physical_compressed_block *) src_data; - symbolic_compressed_block scb; - - physical_to_symbolic(xdim, ydim, 1, pcb, &scb); - decompress_symbolic_block((astc_decode_mode) decode_mode, xdim, ydim, 1, xpos, ypos, 0, &scb, &pb); - - from_imageblock(clamp_xdim, clamp_ydim, &pb, decode_mode == ASTC_DECODE_LDR_SRGB, k_swizzles[dst_channels], dst_row + xpos * 4, dst_stride); - - src_data += 16; - } - } -} - -// Relevant astc source files. These aren't set up for a bulk build yet though. -#ifdef DISABLED - #include "astc_block_sizes2.cpp" - #include "astc_color_quantize.cpp" - #include "astc_color_unquantize.cpp" - #include "astc_compress_symbolic.cpp" - #include "astc_compute_variance.cpp" - #include "astc_decompress_symbolic.cpp" - #include "astc_encoding_choice_error.cpp" - #include "astc_find_best_partitioning.cpp" - #include "astc_ideal_endpoints_and_weights.cpp" - #include "astc_imageblock.cpp" - #include "astc_integer_sequence.cpp" - #include "astc_kmeans_partitioning.cpp" - #include "astc_partition_tables.cpp" - #include "astc_percentile_tables.cpp" - #include "astc_pick_best_endpoint_format.cpp" - #include "astc_quantization.cpp" - #include "astc_symbolic_physical.cpp" - #include "astc_weight_align.cpp" - #include "astc_weight_quant_xfer_tables.cpp" - #include "mathlib.cpp" - #include "softfloat.cpp" -#endif diff --git a/3rdparty/bimg/3rdparty/astc/astc_lib.h b/3rdparty/bimg/3rdparty/astc/astc_lib.h deleted file mode 100644 index 373f33b82ea..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_lib.h +++ /dev/null @@ -1,73 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * @author Andrew Willmott - * - * @brief Library api for astc codec, to be used as an alternative to astc_toplevel.cpp - */ -/*----------------------------------------------------------------------------*/ - -#ifndef ASTC_LIB_H -#define ASTC_LIB_H - -#include <stdint.h> -#include <stdlib.h> - -enum ASTC_COMPRESS_MODE // Trade-off compression quality for speed -{ - ASTC_COMPRESS_VERY_FAST, - ASTC_COMPRESS_FAST, - ASTC_COMPRESS_MEDIUM, - ASTC_COMPRESS_THOROUGH, - ASTC_COMPRESS_EXHAUSTIVE, -}; - -enum ASTC_DECODE_MODE -{ - ASTC_DECODE_LDR_SRGB, // texture will be decompressed to 8-bit SRGB - ASTC_DECODE_LDR_LINEAR, // texture will be decompressed to 8-bit linear - ASTC_DECODE_HDR // texture will be decompressed to 16-bit linear -}; - -enum ASTC_CHANNELS -{ - ASTC_RGBA, - ASTC_BGRA -}; - - -size_t astc_compressed_size(int block_width, int block_height, int width, int height); -//!< Returns size of the compressed data for a width x height source image, assuming the given block size - -void astc_compress -( - int src_width, - int src_height, - const uint8_t* src_data, - ASTC_CHANNELS src_channels, - int src_stride, - - int block_width, - int block_height, - ASTC_COMPRESS_MODE compress_mode, - ASTC_DECODE_MODE decode_mode, - uint8_t* dst_data -); -//!< Compress 8-bit rgba source image into dst_data (expected to be of size astc_compressed_size(...)) - -void astc_decompress -( - const uint8_t* src_data, - int block_width, - int block_height, - ASTC_DECODE_MODE decode_mode, - - int dst_width, - int dst_height, - uint8_t* dst_data, - ASTC_CHANNELS dst_channels, - int dst_stride -); -//!< Decompress astc source image into 8-bit rgba destination image. - -#endif - diff --git a/3rdparty/bimg/3rdparty/astc/astc_partition_tables.cpp b/3rdparty/bimg/3rdparty/astc/astc_partition_tables.cpp deleted file mode 100644 index 7e0a46011c8..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_partition_tables.cpp +++ /dev/null @@ -1,323 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions to generate partition tables for ASTC. - * - * We generate tables only for the block sizes that have actually been - * specified to the codec. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -static partition_info **partition_tables[4096]; - -/* - Produce a canonicalized representation of a partition pattern - - The largest possible such representation is 432 bits, equal to 7 uint64_t values. -*/ -static void gen_canonicalized_partition_table(int texel_count, const uint8_t * partition_table, uint64_t canonicalized[7]) -{ - int i; - for (i = 0; i < 7; i++) - canonicalized[i] = 0; - - int mapped_index[4]; - int map_weight_count = 0; - for (i = 0; i < 4; i++) - mapped_index[i] = -1; - - for (i = 0; i < texel_count; i++) - { - int index = partition_table[i]; - if (mapped_index[index] == -1) - mapped_index[index] = map_weight_count++; - uint64_t xlat_index = mapped_index[index]; - canonicalized[i >> 5] |= xlat_index << (2 * (i & 0x1F)); - } -} - - -static int compare_canonicalized_partition_tables(const uint64_t part1[7], const uint64_t part2[7]) -{ - if (part1[0] != part2[0]) - return 0; - if (part1[1] != part2[1]) - return 0; - if (part1[2] != part2[2]) - return 0; - if (part1[3] != part2[3]) - return 0; - if (part1[4] != part2[4]) - return 0; - if (part1[5] != part2[5]) - return 0; - if (part1[6] != part2[6]) - return 0; - return 1; -} - - -/* - For a partition table, detect partitionings that are equivalent, then mark them as invalid. This reduces the number of partitions that the codec has to consider and thus improves encode - performance. */ -static void partition_table_zap_equal_elements(int xdim, int ydim, int zdim, partition_info * pi) -{ - int partition_tables_zapped = 0; - - int texel_count = xdim * ydim * zdim; - - int i, j; - uint64_t *canonicalizeds = new uint64_t[PARTITION_COUNT * 7]; - - - for (i = 0; i < PARTITION_COUNT; i++) - { - gen_canonicalized_partition_table(texel_count, pi[i].partition_of_texel, canonicalizeds + i * 7); - } - - for (i = 0; i < PARTITION_COUNT; i++) - { - for (j = 0; j < i; j++) - { - if (compare_canonicalized_partition_tables(canonicalizeds + 7 * i, canonicalizeds + 7 * j)) - { - pi[i].partition_count = 0; - partition_tables_zapped++; - break; - } - } - } - delete[]canonicalizeds; -} - - -uint32_t hash52(uint32_t inp) -{ - inp ^= inp >> 15; - - inp *= 0xEEDE0891; // (2^4+1)*(2^7+1)*(2^17-1) - inp ^= inp >> 5; - inp += inp << 16; - inp ^= inp >> 7; - inp ^= inp >> 3; - inp ^= inp << 6; - inp ^= inp >> 17; - return inp; -} - - - -int select_partition(int seed, int x, int y, int z, int partitioncount, int small_block) -{ - if (small_block) - { - x <<= 1; - y <<= 1; - z <<= 1; - } - - seed += (partitioncount - 1) * 1024; - - uint32_t rnum = hash52(seed); - - uint8_t seed1 = rnum & 0xF; - uint8_t seed2 = (rnum >> 4) & 0xF; - uint8_t seed3 = (rnum >> 8) & 0xF; - uint8_t seed4 = (rnum >> 12) & 0xF; - uint8_t seed5 = (rnum >> 16) & 0xF; - uint8_t seed6 = (rnum >> 20) & 0xF; - uint8_t seed7 = (rnum >> 24) & 0xF; - uint8_t seed8 = (rnum >> 28) & 0xF; - uint8_t seed9 = (rnum >> 18) & 0xF; - uint8_t seed10 = (rnum >> 22) & 0xF; - uint8_t seed11 = (rnum >> 26) & 0xF; - uint8_t seed12 = ((rnum >> 30) | (rnum << 2)) & 0xF; - - // squaring all the seeds in order to bias their distribution - // towards lower values. - seed1 *= seed1; - seed2 *= seed2; - seed3 *= seed3; - seed4 *= seed4; - seed5 *= seed5; - seed6 *= seed6; - seed7 *= seed7; - seed8 *= seed8; - seed9 *= seed9; - seed10 *= seed10; - seed11 *= seed11; - seed12 *= seed12; - - - int sh1, sh2, sh3; - if (seed & 1) - { - sh1 = (seed & 2 ? 4 : 5); - sh2 = (partitioncount == 3 ? 6 : 5); - } - else - { - sh1 = (partitioncount == 3 ? 6 : 5); - sh2 = (seed & 2 ? 4 : 5); - } - sh3 = (seed & 0x10) ? sh1 : sh2; - - seed1 >>= sh1; - seed2 >>= sh2; - seed3 >>= sh1; - seed4 >>= sh2; - seed5 >>= sh1; - seed6 >>= sh2; - seed7 >>= sh1; - seed8 >>= sh2; - - seed9 >>= sh3; - seed10 >>= sh3; - seed11 >>= sh3; - seed12 >>= sh3; - - - - int a = seed1 * x + seed2 * y + seed11 * z + (rnum >> 14); - int b = seed3 * x + seed4 * y + seed12 * z + (rnum >> 10); - int c = seed5 * x + seed6 * y + seed9 * z + (rnum >> 6); - int d = seed7 * x + seed8 * y + seed10 * z + (rnum >> 2); - - - // apply the saw - a &= 0x3F; - b &= 0x3F; - c &= 0x3F; - d &= 0x3F; - - // remove some of the components if we are to output < 4 partitions. - if (partitioncount <= 3) - d = 0; - if (partitioncount <= 2) - c = 0; - if (partitioncount <= 1) - b = 0; - - int partition; - if (a >= b && a >= c && a >= d) - partition = 0; - else if (b >= c && b >= d) - partition = 1; - else if (c >= d) - partition = 2; - else - partition = 3; - return partition; -} - - - -void generate_one_partition_table(int xdim, int ydim, int zdim, int partition_count, int partition_index, partition_info * pt) -{ - int small_block = (xdim * ydim * zdim) < 32; - - uint8_t *partition_of_texel = pt->partition_of_texel; - int x, y, z, i; - - - for (z = 0; z < zdim; z++) - for (y = 0; y < ydim; y++) - for (x = 0; x < xdim; x++) - { - uint8_t part = select_partition(partition_index, x, y, z, partition_count, small_block); - *partition_of_texel++ = part; - } - - - int texels_per_block = xdim * ydim * zdim; - - int counts[4]; - for (i = 0; i < 4; i++) - counts[i] = 0; - - for (i = 0; i < texels_per_block; i++) - { - int partition = pt->partition_of_texel[i]; - pt->texels_of_partition[partition][counts[partition]++] = i; - } - - for (i = 0; i < 4; i++) - pt->texels_per_partition[i] = counts[i]; - - if (counts[0] == 0) - pt->partition_count = 0; - else if (counts[1] == 0) - pt->partition_count = 1; - else if (counts[2] == 0) - pt->partition_count = 2; - else if (counts[3] == 0) - pt->partition_count = 3; - else - pt->partition_count = 4; - - - - for (i = 0; i < 4; i++) - pt->coverage_bitmaps[i] = 0ULL; - - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - int texels_to_process = bsd->texelcount_for_bitmap_partitioning; - for (i = 0; i < texels_to_process; i++) - { - int idx = bsd->texels_for_bitmap_partitioning[i]; - pt->coverage_bitmaps[pt->partition_of_texel[idx]] |= 1ULL << i; - } - -} - -static void generate_partition_tables(int xdim, int ydim, int zdim) -{ - int i; - - - partition_info *one_partition = new partition_info; - partition_info *two_partitions = new partition_info[1024]; - partition_info *three_partitions = new partition_info[1024]; - partition_info *four_partitions = new partition_info[1024]; - - partition_info **partition_table = new partition_info *[5]; - partition_table[0] = NULL; - partition_table[1] = one_partition; - partition_table[2] = two_partitions; - partition_table[3] = three_partitions; - partition_table[4] = four_partitions; - - generate_one_partition_table(xdim, ydim, zdim, 1, 0, one_partition); - for (i = 0; i < 1024; i++) - { - generate_one_partition_table(xdim, ydim, zdim, 2, i, two_partitions + i); - generate_one_partition_table(xdim, ydim, zdim, 3, i, three_partitions + i); - generate_one_partition_table(xdim, ydim, zdim, 4, i, four_partitions + i); - } - - partition_table_zap_equal_elements(xdim, ydim, zdim, two_partitions); - partition_table_zap_equal_elements(xdim, ydim, zdim, three_partitions); - partition_table_zap_equal_elements(xdim, ydim, zdim, four_partitions); - - partition_tables[xdim + 16 * ydim + 256 * zdim] = partition_table; -} - - -const partition_info *get_partition_table(int xdim, int ydim, int zdim, int partition_count) -{ - int ptindex = xdim + 16 * ydim + 256 * zdim; - if (partition_tables[ptindex] == NULL) - generate_partition_tables(xdim, ydim, zdim); - - return partition_tables[ptindex][partition_count]; -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_percentile_tables.cpp b/3rdparty/bimg/3rdparty/astc/astc_percentile_tables.cpp deleted file mode 100644 index 6b96ef8cf72..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_percentile_tables.cpp +++ /dev/null @@ -1,4768 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Percentile tables for ASTC. - */ -/*----------------------------------------------------------------------------*/ - -extern const float percentile_table_4x4[2048]; -extern const float percentile_table_4x5[2048]; -extern const float percentile_table_4x6[2048]; -extern const float percentile_table_4x8[2048]; -extern const float percentile_table_4x10[2048]; -extern const float percentile_table_4x12[2048]; -extern const float percentile_table_5x4[2048]; -extern const float percentile_table_5x5[2048]; -extern const float percentile_table_5x6[2048]; -extern const float percentile_table_5x8[2048]; -extern const float percentile_table_5x10[2048]; -extern const float percentile_table_5x12[2048]; -extern const float percentile_table_6x4[2048]; -extern const float percentile_table_6x5[2048]; -extern const float percentile_table_6x6[2048]; -extern const float percentile_table_6x8[2048]; -extern const float percentile_table_6x10[2048]; -extern const float percentile_table_6x12[2048]; -extern const float percentile_table_8x4[2048]; -extern const float percentile_table_8x5[2048]; -extern const float percentile_table_8x6[2048]; -extern const float percentile_table_8x8[2048]; -extern const float percentile_table_8x10[2048]; -extern const float percentile_table_8x12[2048]; -extern const float percentile_table_10x4[2048]; -extern const float percentile_table_10x5[2048]; -extern const float percentile_table_10x6[2048]; -extern const float percentile_table_10x8[2048]; -extern const float percentile_table_10x10[2048]; -extern const float percentile_table_10x12[2048]; -extern const float percentile_table_12x4[2048]; -extern const float percentile_table_12x5[2048]; -extern const float percentile_table_12x6[2048]; -extern const float percentile_table_12x8[2048]; -extern const float percentile_table_12x10[2048]; -extern const float percentile_table_12x12[2048]; - - -const float percentile_table_4x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.8661f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7732f, 0.8567f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7818f, 0.8914f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.4578f, 0.5679f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4183f, 0.4961f, 0.5321f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9151f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9400f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9678f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8111f, 0.8833f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8299f, 0.8988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9182f, 0.9692f, 0.9820f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9663f, 0.9911f, 0.8707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9088f, 0.9374f, 0.8793f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8750f, 0.8952f, 0.7356f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2746f, 0.0000f, 0.0772f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.1487f, 0.2193f, 0.3263f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9917f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9996f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9516f, 0.9838f, 0.9927f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9731f, 0.9906f, 0.9448f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9900f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9991f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.9987f, 0.9936f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9985f, 0.9615f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9269f, 0.9577f, 0.9057f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9023f, 0.9241f, 0.7499f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9757f, 0.9846f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9922f, 0.9932f, 0.9792f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9881f, 0.9494f, 0.8178f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9780f, 0.8518f, 0.6206f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9472f, 0.7191f, 0.7003f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8356f, 0.3772f, 0.9971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9769f, 0.9875f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, 0.9941f, 0.9861f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9956f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9868f, 0.9854f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9888f, 0.9811f, 0.9706f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9894f, 0.9425f, 0.8465f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9718f, 0.8614f, 0.6422f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9648f, 0.9212f, 0.8412f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9596f, 0.9557f, 0.9537f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9999f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 0.9984f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.9994f, 0.9633f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9829f, 0.9120f, 0.8042f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9744f, 0.9296f, 0.9349f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9993f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9998f, 0.9802f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8874f, 0.5963f, 0.9323f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8239f, 0.7625f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6625f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7895f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_4x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.8886f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7962f, 0.9052f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8118f, 0.9315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7247f, 0.8069f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7789f, 0.8308f, 0.8434f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.4129f, 0.4395f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3795f, 0.3370f, 0.1853f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9566f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8585f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8946f, 0.9102f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9502f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9739f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8854f, 0.9554f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9150f, 0.9748f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7849f, 0.8689f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8214f, 0.7729f, 0.8016f, - 1.0000f, 0.9172f, 0.9659f, 0.9781f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9701f, 0.9873f, 0.8394f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9457f, 0.9637f, 0.9334f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9236f, 0.9407f, 0.7395f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6662f, 0.6019f, 0.7169f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5436f, 0.7007f, 0.7907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.0000f, 0.2718f, 0.5740f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.0959f, 0.5888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9996f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 0.9995f, 0.9986f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9998f, 0.9879f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9441f, 0.9773f, 0.9256f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9812f, 0.9789f, 0.8975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9992f, 0.9949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9990f, 0.9541f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9690f, 0.9841f, 0.9730f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9425f, 0.9602f, 0.8354f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9001f, 0.6261f, 0.6370f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8549f, 0.8473f, 0.6477f, - 1.0000f, 1.0000f, 0.9648f, 0.9867f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9910f, 0.9884f, 0.9827f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9890f, 0.9670f, 0.9077f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9855f, 0.9276f, 0.6753f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9711f, 0.7668f, 0.5272f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9295f, 0.5087f, 0.9971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9488f, 0.6570f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7537f, 0.9590f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9848f, 0.9932f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9955f, 0.9936f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9797f, 0.9720f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9680f, 0.9861f, 0.9353f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.9905f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9915f, 0.9919f, 0.9819f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9895f, 0.9389f, 0.8512f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9764f, 0.8917f, 0.4628f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9756f, 0.8262f, 0.6148f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9372f, 0.6843f, 0.6926f, - 1.0000f, 0.9614f, 0.9194f, 0.8723f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9473f, 0.9515f, 0.9625f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7322f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9126f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9991f, 0.9978f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9999f, 0.9579f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9900f, 0.8789f, 0.7088f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9805f, 0.8620f, 0.9026f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8822f, 0.8655f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8166f, 0.9943f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9997f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9999f, 0.9834f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9215f, 0.5589f, 0.9528f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8756f, 0.7604f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4861f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7467f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_4x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7443f, 0.8512f, - 1.0000f, 1.0000f, 1.0000f, 0.8929f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7596f, 0.8882f, - 1.0000f, 1.0000f, 0.8221f, 0.9144f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7051f, 0.6993f, - 1.0000f, 1.0000f, 0.8355f, 0.9440f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7830f, 0.7737f, 0.7389f, - 1.0000f, 1.0000f, 0.7918f, 0.8542f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2729f, 0.4793f, 0.1616f, - 1.0000f, 0.8322f, 0.8760f, 0.8995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4122f, 0.2349f, 0.0000f, - 1.0000f, 1.0000f, 0.6930f, 0.7495f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7225f, 0.5849f, 0.3642f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9695f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9526f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9550f, 0.9840f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9654f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9036f, 0.9718f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9414f, 0.9800f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8600f, 0.9466f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8859f, 0.8735f, 0.8451f, - 1.0000f, 0.9271f, 0.9605f, 0.9733f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9177f, 0.9626f, 0.9401f, - 1.0000f, 0.9711f, 0.9893f, 0.8255f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9209f, 0.9616f, 0.6804f, - 1.0000f, 0.9584f, 0.9635f, 0.9110f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5268f, 0.4632f, 0.8387f, - 1.0000f, 0.9161f, 0.9316f, 0.7167f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5749f, 0.6867f, 0.8973f, - 1.0000f, 0.7784f, 0.6674f, 0.8115f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0882f, 0.5113f, 1.0000f, - 1.0000f, 0.6455f, 0.8040f, 0.8628f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3374f, 1.0000f, 1.0000f, - 1.0000f, 0.3093f, 0.5529f, 0.8289f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3896f, 0.8811f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9992f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9997f, 0.9830f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9913f, 0.9538f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9871f, 0.9645f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9991f, 0.9921f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9991f, 0.9514f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9761f, 0.9909f, 0.9851f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9490f, 0.9755f, 0.8835f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9286f, 0.7110f, 0.8187f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8951f, 0.9193f, 0.8077f, - 1.0000f, 1.0000f, 0.9679f, 0.9917f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9794f, 0.9703f, 0.9453f, - 1.0000f, 0.9906f, 0.9941f, 0.9856f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9812f, 0.9561f, 0.7876f, - 1.0000f, 0.9866f, 0.9748f, 0.9127f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9687f, 0.7335f, 0.9478f, - 1.0000f, 0.9898f, 0.9387f, 0.7280f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9074f, 0.6294f, 1.0000f, - 1.0000f, 0.9671f, 0.8419f, 0.5944f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9256f, 0.9960f, 1.0000f, - 1.0000f, 0.9502f, 0.6039f, 0.9985f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6607f, 1.0000f, 1.0000f, - 1.0000f, 0.9775f, 0.6127f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8656f, 0.9225f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9845f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9966f, 0.9955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 0.9768f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9740f, 0.9835f, 0.9330f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9986f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9902f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.9880f, 0.9781f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9889f, 0.9301f, 0.8906f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9787f, 0.9055f, 0.4955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9806f, 0.8000f, 0.4303f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9015f, 0.6211f, 0.5645f, - 1.0000f, 0.9663f, 0.9240f, 0.8709f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8151f, 1.0000f, 1.0000f, - 1.0000f, 0.9573f, 0.9595f, 0.9726f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9359f, 1.0000f, 1.0000f, - 1.0000f, 0.7644f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9344f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9983f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9935f, 0.9997f, 0.9373f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9885f, 0.8683f, 0.6534f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9818f, 0.8571f, 0.8786f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7960f, 0.7546f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6376f, 0.9930f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 0.9999f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9999f, 0.9875f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9092f, 0.5405f, 0.9427f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8482f, 0.6740f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4469f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7691f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_4x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6540f, 0.8090f, 1.0000f, 1.0000f, 0.9047f, 0.9767f, - 1.0000f, 1.0000f, 1.0000f, 0.9273f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6869f, 0.8003f, 0.8742f, 1.0000f, 1.0000f, 0.9569f, 0.9887f, - 1.0000f, 1.0000f, 0.8833f, 0.9454f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7850f, 0.6687f, 0.3938f, 1.0000f, 1.0000f, 0.8390f, 0.8033f, - 1.0000f, 1.0000f, 0.8724f, 0.9732f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6489f, 0.6254f, 0.2363f, 1.0000f, 0.8524f, 0.8851f, 0.8117f, - 1.0000f, 1.0000f, 0.8481f, 0.9106f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1043f, 0.1756f, 0.1402f, 1.0000f, 0.8647f, 0.7274f, 0.3414f, - 1.0000f, 0.9000f, 0.9174f, 0.9444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3601f, 0.0000f, 0.4370f, 1.0000f, 0.7036f, 0.6435f, 0.2612f, - 1.0000f, 1.0000f, 0.8143f, 0.8586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8195f, 0.7196f, 0.5069f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9120f, 0.9463f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8920f, 0.9544f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7626f, 0.7881f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7524f, 0.8413f, 0.3226f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8220f, 0.4960f, 0.0657f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5448f, 0.2826f, 0.2081f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9846f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9693f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9673f, 0.9927f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9858f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9921f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9435f, 0.9866f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9738f, 0.9915f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9200f, 0.9750f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9134f, 0.9308f, 0.8903f, - 1.0000f, 0.9405f, 0.9645f, 0.9687f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8606f, 0.7077f, 0.7420f, 1.0000f, 0.9825f, 0.9880f, 0.9761f, - 1.0000f, 0.9834f, 0.9829f, 0.7943f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7593f, 0.7757f, 0.7724f, 1.0000f, 0.9608f, 0.9800f, 0.8436f, - 1.0000f, 0.9600f, 0.9615f, 0.9319f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3037f, 0.8869f, 1.0000f, 1.0000f, 0.5767f, 0.7455f, 0.9577f, - 1.0000f, 0.9212f, 0.9363f, 0.6953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5917f, 1.0000f, 1.0000f, 1.0000f, 0.7311f, 0.8761f, 0.9331f, - 1.0000f, 0.8295f, 0.6995f, 0.8815f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4085f, 0.8545f, 1.0000f, - 1.0000f, 0.6911f, 0.8686f, 0.9062f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6734f, 1.0000f, 1.0000f, - 1.0000f, 0.4502f, 0.7157f, 0.9092f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5613f, 0.9491f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8319f, 0.9016f, 0.9160f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9630f, 0.8503f, 0.8459f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4628f, 0.7912f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6124f, 0.8969f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5172f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9997f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9999f, 0.9638f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 0.9913f, 0.9285f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9854f, 0.9719f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9995f, 0.9901f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9986f, 0.9395f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9795f, 0.9904f, 0.9850f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9473f, 0.9784f, 0.8627f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9623f, 0.7692f, 0.8705f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9385f, 0.9592f, 0.8367f, - 1.0000f, 1.0000f, 0.9773f, 0.9930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9518f, 0.9225f, 0.8062f, 1.0000f, 0.9907f, 0.9552f, 0.9261f, - 1.0000f, 0.9968f, 0.9944f, 0.9891f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9561f, 0.8343f, 0.9999f, 1.0000f, 0.9805f, 0.9509f, 0.6058f, - 1.0000f, 0.9961f, 0.9877f, 0.9535f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9425f, 0.9910f, 1.0000f, 1.0000f, 0.9526f, 0.6190f, 0.8270f, - 1.0000f, 0.9942f, 0.9744f, 0.8245f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7559f, 1.0000f, 1.0000f, 1.0000f, 0.8565f, 0.3782f, 1.0000f, - 1.0000f, 0.9815f, 0.8886f, 0.7235f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8779f, 1.0000f, 1.0000f, 1.0000f, 0.9652f, 0.9949f, 1.0000f, - 1.0000f, 0.9755f, 0.7384f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6639f, 1.0000f, 1.0000f, - 1.0000f, 0.9838f, 0.6590f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9077f, 0.9147f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9810f, 0.9353f, 0.6826f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9584f, 0.8953f, 0.6379f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9342f, 0.5271f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7973f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9700f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9237f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9969f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9894f, 0.9951f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9966f, 0.9955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9869f, 0.9725f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9789f, 0.9873f, 0.9415f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9953f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 0.9935f, 0.9884f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9680f, 0.9249f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, 0.9374f, 0.4852f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9820f, 0.8170f, 0.4740f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8985f, 0.6780f, 0.5691f, - 1.0000f, 0.9659f, 0.9296f, 0.9031f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5843f, 1.0000f, 1.0000f, - 1.0000f, 0.9482f, 0.9666f, 0.9842f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8797f, 1.0000f, 1.0000f, - 1.0000f, 0.8666f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9713f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9998f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, 0.9990f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9918f, 0.9999f, 0.9187f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9897f, 0.7788f, 0.5362f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9778f, 0.7819f, 0.7347f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7117f, 0.6317f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5530f, 0.9937f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9959f, 1.0000f, 0.9985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9500f, 0.5989f, 0.9706f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8936f, 0.7490f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4231f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7659f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_4x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8566f, 0.9251f, 1.0000f, 1.0000f, 0.9271f, 0.9827f, - 1.0000f, 1.0000f, 1.0000f, 0.9436f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8821f, 0.9344f, 0.9492f, 1.0000f, 1.0000f, 0.9428f, 0.9930f, - 1.0000f, 1.0000f, 0.8766f, 0.9560f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8938f, 0.7784f, 0.3546f, 1.0000f, 1.0000f, 0.8646f, 0.8199f, - 1.0000f, 1.0000f, 0.8737f, 0.9734f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7535f, 0.7283f, 0.2504f, 1.0000f, 0.8807f, 0.9012f, 0.8281f, - 1.0000f, 1.0000f, 0.8874f, 0.9280f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7759f, 0.3657f, 0.2921f, 1.0000f, 0.8913f, 0.7955f, 0.3984f, - 1.0000f, 0.9160f, 0.9412f, 0.9671f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6872f, 0.0745f, 0.6100f, 1.0000f, 0.7706f, 0.7381f, 0.3187f, - 1.0000f, 1.0000f, 0.8301f, 0.8707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8468f, 0.7733f, 0.5470f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8047f, 0.7932f, 1.0000f, 1.0000f, 0.9395f, 0.9712f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8358f, 0.8692f, 0.8320f, 1.0000f, 1.0000f, 0.9060f, 0.9627f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8024f, 0.6745f, 0.2645f, 1.0000f, 1.0000f, 0.8377f, 0.8630f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7250f, 0.2193f, 0.2008f, 1.0000f, 0.8220f, 0.8926f, 0.4195f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7112f, 0.1026f, 0.4946f, 1.0000f, 0.8598f, 0.6657f, 0.3055f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3767f, 0.1538f, 1.0000f, 1.0000f, 0.6913f, 0.4682f, 0.4591f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5926f, 0.7979f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5866f, 0.6366f, 0.6562f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6994f, 0.4296f, 0.1289f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6701f, 0.3312f, 0.3434f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0378f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1783f, 0.2350f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9921f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9784f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9831f, 0.9939f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9614f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9884f, 0.9951f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9420f, 0.9815f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9387f, 0.9461f, 0.8661f, - 1.0000f, 0.9567f, 0.9659f, 0.9652f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8834f, 0.8339f, 0.9000f, 1.0000f, 0.9857f, 0.9896f, 0.9760f, - 1.0000f, 0.9901f, 0.9819f, 0.7622f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8135f, 0.9083f, 0.8900f, 1.0000f, 0.9594f, 0.9798f, 0.8002f, - 1.0000f, 0.9677f, 0.9515f, 0.8861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5178f, 0.8550f, 1.0000f, 1.0000f, 0.5985f, 0.7810f, 0.9500f, - 1.0000f, 0.9105f, 0.9201f, 0.6788f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7316f, 1.0000f, 1.0000f, 1.0000f, 0.7650f, 0.8534f, 0.9469f, - 1.0000f, 0.8485f, 0.7476f, 0.9036f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5025f, 0.9072f, 1.0000f, - 1.0000f, 0.7678f, 0.8793f, 0.9221f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7594f, 1.0000f, 1.0000f, - 1.0000f, 0.4772f, 0.7835f, 0.9191f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6044f, 0.9689f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6465f, 0.7034f, 0.9231f, 1.0000f, 0.8451f, 0.9308f, 0.9665f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7349f, 0.8241f, 0.9445f, 1.0000f, 0.9850f, 0.8976f, 0.9149f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6314f, 1.0000f, 1.0000f, 1.0000f, 0.6610f, 0.9261f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7860f, 0.9379f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7147f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5326f, 0.6155f, 0.9573f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5253f, 0.8582f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9988f, 0.9974f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9998f, 0.9530f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9839f, 0.8722f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9770f, 0.9353f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9991f, 0.9890f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9993f, 0.9139f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9854f, 0.9919f, 0.9878f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9545f, 0.9846f, 0.8677f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9640f, 0.7446f, 0.8501f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9241f, 0.9476f, 0.8432f, - 1.0000f, 1.0000f, 0.9779f, 0.9978f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9587f, 0.8887f, 0.6209f, 1.0000f, 0.9871f, 0.9553f, 0.9317f, - 1.0000f, 0.9946f, 0.9959f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9181f, 0.6830f, 0.9999f, 1.0000f, 0.9750f, 0.9335f, 0.5610f, - 1.0000f, 0.9960f, 0.9916f, 0.9707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9048f, 0.9484f, 1.0000f, 1.0000f, 0.9507f, 0.6415f, 0.7506f, - 1.0000f, 0.9914f, 0.9739f, 0.8614f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5398f, 1.0000f, 1.0000f, 1.0000f, 0.8517f, 0.2785f, 1.0000f, - 1.0000f, 0.9835f, 0.9299f, 0.8069f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9601f, 1.0000f, 1.0000f, 1.0000f, 0.9684f, 0.9926f, 1.0000f, - 1.0000f, 0.9864f, 0.8414f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7414f, 1.0000f, 1.0000f, - 1.0000f, 0.9806f, 0.7565f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9289f, 0.9362f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9646f, 0.8156f, 0.9811f, 1.0000f, 0.9843f, 0.9128f, 0.7181f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9211f, 0.6953f, 1.0000f, 1.0000f, 0.9453f, 0.9094f, 0.5542f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9326f, 1.0000f, 1.0000f, 1.0000f, 0.9701f, 0.4859f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7216f, 1.0000f, 1.0000f, 1.0000f, 0.7884f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9789f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8848f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9580f, 0.8752f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9370f, 0.9620f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9765f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9904f, 0.9985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9935f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9977f, 0.9962f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9723f, 0.9793f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9744f, 0.9874f, 0.8964f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9971f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9937f, 0.9941f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9909f, 0.9718f, 0.9523f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9881f, 0.9607f, 0.5805f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9868f, 0.8396f, 0.4496f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8780f, 0.7073f, 0.5743f, - 1.0000f, 0.9729f, 0.9404f, 0.9117f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4398f, 1.0000f, 1.0000f, - 1.0000f, 0.9695f, 0.9633f, 0.9893f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7908f, 1.0000f, 1.0000f, - 1.0000f, 0.8951f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9823f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9997f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9906f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 0.9984f, 0.9955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9899f, 0.9999f, 0.9024f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.8113f, 0.5679f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9775f, 0.8261f, 0.8091f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6262f, 0.4091f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3877f, 0.9802f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9911f, 1.0000f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9538f, 0.6514f, 0.9755f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9171f, 0.8178f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5103f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8988f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_4x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8570f, 0.9351f, 1.0000f, 1.0000f, 0.9461f, 0.9884f, - 1.0000f, 1.0000f, 1.0000f, 0.9476f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8944f, 0.9376f, 0.9581f, 1.0000f, 1.0000f, 0.9811f, 0.9855f, - 1.0000f, 1.0000f, 0.8777f, 0.9658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9121f, 0.8362f, 0.4335f, 1.0000f, 1.0000f, 0.8910f, 0.8445f, - 1.0000f, 1.0000f, 0.9001f, 0.9751f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7894f, 0.7758f, 0.3577f, 1.0000f, 0.9024f, 0.9256f, 0.8509f, - 1.0000f, 1.0000f, 0.9301f, 0.9384f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7873f, 0.5440f, 0.4410f, 1.0000f, 0.9013f, 0.8413f, 0.4701f, - 1.0000f, 0.9468f, 0.9618f, 0.9630f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7346f, 0.1421f, 0.7373f, 1.0000f, 0.8184f, 0.8105f, 0.3846f, - 1.0000f, 1.0000f, 0.8656f, 0.9141f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8886f, 0.8002f, 0.6091f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5378f, 1.0000f, 1.0000f, 1.0000f, 0.6809f, 1.0000f, 0.8396f, 0.8125f, 0.5731f, 1.0000f, 0.9438f, 0.9669f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6042f, 1.0000f, 1.0000f, 1.0000f, 0.6916f, 0.8311f, 0.8752f, 0.8585f, 0.3390f, 1.0000f, 0.9247f, 0.9600f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6616f, 1.0000f, 1.0000f, 1.0000f, 0.1980f, 0.8165f, 0.7143f, 0.3757f, 0.6363f, 1.0000f, 0.8294f, 0.8477f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4913f, 1.0000f, 1.0000f, 1.0000f, 0.1094f, 0.7507f, 0.2782f, 0.2886f, 1.0000f, 0.8222f, 0.8898f, 0.3091f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0310f, 1.0000f, 1.0000f, 1.0000f, 0.5560f, 0.7686f, 0.1850f, 0.6319f, 1.0000f, 0.9191f, 0.7050f, 0.2461f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5675f, 0.2231f, 1.0000f, 1.0000f, 0.6985f, 0.4180f, 0.4557f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8145f, 0.8826f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8023f, 0.7710f, 0.6274f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8328f, 0.5182f, 0.1259f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7290f, 0.2349f, 0.4100f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6951f, 0.0862f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3292f, 0.4773f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9896f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9874f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9865f, 0.9955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7113f, 0.7782f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7981f, 0.8493f, 0.4484f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7082f, 0.2106f, 0.2571f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6183f, 0.0618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4259f, 0.1570f, 1.0000f, 1.0000f, 1.0000f, 0.9717f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1715f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9922f, 0.9966f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9391f, 0.9848f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9574f, 0.9675f, 0.8990f, - 1.0000f, 0.9841f, 0.9587f, 0.9624f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9034f, 0.8540f, 0.9309f, 1.0000f, 0.9732f, 0.9837f, 0.9686f, - 1.0000f, 0.9912f, 0.9827f, 0.7481f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8276f, 0.9399f, 0.8790f, 1.0000f, 0.9343f, 0.9760f, 0.7612f, - 1.0000f, 0.9746f, 0.9612f, 0.8967f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6406f, 0.8863f, 1.0000f, 1.0000f, 0.6138f, 0.8065f, 0.9265f, - 1.0000f, 0.9359f, 0.9292f, 0.6733f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7938f, 1.0000f, 1.0000f, 1.0000f, 0.7637f, 0.8599f, 0.9680f, - 1.0000f, 0.8670f, 0.7232f, 0.8979f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5945f, 0.9483f, 1.0000f, - 1.0000f, 0.7661f, 0.8738f, 0.9423f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8345f, 1.0000f, 1.0000f, - 1.0000f, 0.4981f, 0.8429f, 0.9533f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6695f, 0.9795f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4017f, 1.0000f, 1.0000f, 1.0000f, 0.9707f, 0.6880f, 0.7828f, 0.9519f, 1.0000f, 0.8203f, 0.9162f, 0.9505f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6229f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8044f, 0.8851f, 0.9727f, 1.0000f, 0.9770f, 0.8628f, 0.8711f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6656f, 1.0000f, 1.0000f, 1.0000f, 0.5842f, 0.9099f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7734f, 0.9078f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7401f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5116f, 0.7916f, 0.9498f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6492f, 0.9741f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9971f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9996f, 0.9948f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9989f, 0.9219f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9756f, 0.8524f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9653f, 0.9446f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5617f, 0.8697f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7261f, 0.9702f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9994f, 0.9917f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9986f, 0.9238f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9799f, 0.9920f, 0.9851f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9554f, 0.9787f, 0.8555f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9696f, 0.7805f, 0.8614f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9318f, 0.9540f, 0.8933f, - 1.0000f, 1.0000f, 0.9815f, 0.9981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9326f, 0.8764f, 0.5894f, 1.0000f, 0.9915f, 0.9407f, 0.9200f, - 1.0000f, 0.9940f, 0.9982f, 0.9946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9152f, 0.6449f, 1.0000f, 1.0000f, 0.9722f, 0.9526f, 0.5249f, - 1.0000f, 0.9960f, 0.9893f, 0.9774f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8956f, 0.9210f, 1.0000f, 1.0000f, 0.9647f, 0.6845f, 0.7586f, - 1.0000f, 0.9951f, 0.9868f, 0.8874f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4843f, 1.0000f, 1.0000f, 1.0000f, 0.8724f, 0.2989f, 1.0000f, - 1.0000f, 0.9877f, 0.9453f, 0.8258f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9593f, 1.0000f, 1.0000f, 1.0000f, 0.9779f, 0.9931f, 1.0000f, - 1.0000f, 0.9834f, 0.8814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7850f, 1.0000f, 1.0000f, - 1.0000f, 0.9858f, 0.8240f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9512f, 0.9791f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9664f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 0.9547f, 0.8085f, 0.9335f, 1.0000f, 0.9823f, 0.8802f, 0.6575f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9283f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9067f, 0.5502f, 1.0000f, 1.0000f, 0.9088f, 0.8642f, 0.3484f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9228f, 1.0000f, 1.0000f, 1.0000f, 0.9415f, 0.3668f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5994f, 1.0000f, 1.0000f, 1.0000f, 0.7560f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9636f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8461f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9561f, 0.7018f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8921f, 0.9171f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9368f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9991f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9808f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9962f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, 0.9933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9929f, 0.9956f, 0.9975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9898f, 0.9804f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, 0.9925f, 0.9274f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9131f, 0.7534f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9110f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9949f, 0.9936f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9737f, 0.9567f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9830f, 0.9641f, 0.5787f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9844f, 0.8379f, 0.5048f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8839f, 0.7455f, 0.6771f, - 1.0000f, 0.9819f, 0.9430f, 0.9181f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2677f, 1.0000f, 1.0000f, - 1.0000f, 0.9712f, 0.9691f, 0.9880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7202f, 1.0000f, 1.0000f, - 1.0000f, 0.9490f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9974f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9998f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 1.0000f, 0.9904f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9907f, 0.9992f, 0.9944f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 0.9998f, 0.9056f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9909f, 0.7959f, 0.4630f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9765f, 0.7173f, 0.7318f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6533f, 0.3193f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3931f, 0.9783f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 1.0000f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9871f, 0.7428f, 0.9901f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9606f, 0.9045f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5315f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8683f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9496f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8709f, 0.9575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9171f, 0.9748f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7120f, 0.8199f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7195f, 0.8475f, 0.8778f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.8093f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7545f, 0.8939f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7609f, 0.8637f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8146f, 0.7796f, 0.7858f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.4100f, 0.4583f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3782f, 0.3366f, 0.1850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9259f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9468f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9789f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8600f, 0.9338f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8996f, 0.9424f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9871f, 0.9983f, 0.9979f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9960f, 0.9996f, 0.9853f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9691f, 0.9797f, 0.9711f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9375f, 0.9680f, 0.8560f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7043f, 0.5964f, 0.6432f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6090f, 0.6709f, 0.7735f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9279f, 0.9550f, 0.8744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9701f, 0.9537f, 0.8386f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8879f, 0.5694f, 0.6328f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8247f, 0.8295f, 0.6886f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.0000f, 0.2712f, 0.5397f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.0963f, 0.5833f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9937f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9440f, 0.9832f, 0.9914f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9756f, 0.9930f, 0.9075f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9993f, 0.9943f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9995f, 0.9612f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9563f, 0.9781f, 0.9482f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9509f, 0.9600f, 0.7919f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9846f, 0.9901f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9955f, 0.9949f, 0.9922f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9891f, 0.9319f, 0.8673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9773f, 0.8910f, 0.4347f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9635f, 0.8035f, 0.5226f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9148f, 0.5045f, 0.9972f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9839f, 0.9624f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9647f, 0.9818f, 0.8812f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9739f, 0.8431f, 0.6214f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9409f, 0.6619f, 0.6966f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9523f, 0.6526f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7410f, 0.9658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9720f, 0.9905f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, 0.9876f, 0.9910f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9918f, 0.9896f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9865f, 0.9881f, 0.9811f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9886f, 0.9669f, 0.9238f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9804f, 0.9392f, 0.6799f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9926f, 0.9100f, 0.7478f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9825f, 0.9023f, 0.9216f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4818f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7267f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8518f, 0.8968f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8341f, 0.9952f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9991f, 0.9974f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 0.9995f, 0.9588f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 0.9049f, 0.7977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9765f, 0.9299f, 0.9357f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9994f, 0.9986f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9999f, 0.9730f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9124f, 0.5547f, 0.9454f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8846f, 0.7672f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7339f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9194f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8974f, 0.9705f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9498f, 0.9880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7791f, 0.9035f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8333f, 0.9204f, 0.9408f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6652f, 0.7473f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6369f, 0.6289f, 0.4604f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.7754f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6903f, 0.8953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7639f, 0.8932f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8120f, 0.8089f, 0.7862f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6844f, 0.7250f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6955f, 0.6206f, 0.3520f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2657f, 0.4988f, 0.1727f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5175f, 0.1199f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9727f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8910f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9055f, 0.9093f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9665f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9131f, 0.9740f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9510f, 0.9875f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8027f, 0.9112f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8448f, 0.8392f, 0.8528f, - 1.0000f, 0.9910f, 0.9977f, 0.9974f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9976f, 0.9998f, 0.9802f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9772f, 0.9892f, 0.9845f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9568f, 0.9747f, 0.8704f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7678f, 0.7431f, 0.8728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6783f, 0.8888f, 0.8421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3271f, 0.5343f, 0.8182f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3750f, 0.8502f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9168f, 0.9522f, 0.8304f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9713f, 0.9448f, 0.7598f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9290f, 0.6512f, 0.7056f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8798f, 0.8630f, 0.7515f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3020f, 0.6100f, 0.8243f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4798f, 0.8866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2212f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4198f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9934f, 0.9986f, 0.9991f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9997f, 0.9813f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9435f, 0.9657f, 0.9186f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9835f, 0.9734f, 0.8752f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9993f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9990f, 0.9931f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9996f, 0.9600f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9766f, 0.9900f, 0.9870f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9620f, 0.9790f, 0.8680f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9557f, 0.7202f, 0.7298f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9274f, 0.9150f, 0.7556f, - 1.0000f, 1.0000f, 0.9830f, 0.9888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9951f, 0.9955f, 0.9953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9913f, 0.9590f, 0.9222f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9796f, 0.9257f, 0.5728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9630f, 0.7006f, 0.3980f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8775f, 0.4409f, 0.9903f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9697f, 0.6582f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8058f, 0.9074f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9784f, 0.9681f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9461f, 0.9778f, 0.8605f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9856f, 0.8995f, 0.7344f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9648f, 0.7717f, 0.8475f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9689f, 0.6442f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7962f, 0.9336f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8151f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9984f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9865f, 0.9969f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9946f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9760f, 0.9819f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9610f, 0.9840f, 0.9546f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9936f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 0.9919f, 0.9851f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9896f, 0.9579f, 0.9239f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9753f, 0.9305f, 0.5978f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9808f, 0.9015f, 0.7154f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9639f, 0.8213f, 0.7929f, - 1.0000f, 0.9922f, 0.9395f, 0.7388f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9925f, 0.9321f, 0.9534f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5473f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8363f, 0.9351f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8274f, 0.9983f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9996f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 1.0000f, 0.9916f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9941f, 0.9995f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9999f, 0.9486f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9884f, 0.8554f, 0.6717f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, 0.8655f, 0.9366f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8844f, 0.8580f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7828f, 0.9960f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9997f, 0.9978f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.9994f, 0.9720f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9381f, 0.5602f, 0.9473f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8821f, 0.7105f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5854f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7896f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7676f, 0.8675f, - 1.0000f, 1.0000f, 1.0000f, 0.9864f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7053f, 0.9024f, - 1.0000f, 1.0000f, 0.9386f, 0.9764f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7801f, 0.7480f, - 1.0000f, 1.0000f, 0.9611f, 0.9896f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8218f, 0.8480f, 0.8548f, - 1.0000f, 1.0000f, 0.8503f, 0.9257f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8005f, 0.6960f, 0.3353f, - 1.0000f, 0.8947f, 0.9459f, 0.9561f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6706f, 0.6054f, 0.2768f, - 1.0000f, 1.0000f, 0.7892f, 0.8033f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2041f, 0.1229f, 0.0000f, - 1.0000f, 0.7546f, 0.6863f, 0.4459f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3735f, 0.1652f, 0.2428f, - 1.0000f, 1.0000f, 1.0000f, 0.8695f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8914f, 0.9111f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8267f, 0.9097f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8613f, 0.8655f, 0.8194f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7412f, 0.8060f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7739f, 0.6811f, 0.4847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7978f, 0.6426f, 0.3562f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6252f, 0.4722f, 0.0797f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9822f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9675f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9641f, 0.9852f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9826f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9889f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9352f, 0.9793f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9648f, 0.9912f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8994f, 0.9701f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9083f, 0.9192f, 0.8846f, - 1.0000f, 0.9938f, 0.9983f, 0.9985f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9232f, 0.9570f, 0.9270f, - 1.0000f, 0.9993f, 0.9999f, 0.9808f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9219f, 0.9543f, 0.6541f, - 1.0000f, 0.9753f, 0.9879f, 0.9779f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6188f, 0.5981f, 0.9069f, - 1.0000f, 0.9449f, 0.9689f, 0.8593f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7008f, 0.7950f, 0.9340f, - 1.0000f, 0.8292f, 0.8315f, 0.9166f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3094f, 0.7513f, 1.0000f, - 1.0000f, 0.7303f, 0.9293f, 0.8880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5724f, 1.0000f, 1.0000f, - 1.0000f, 0.4322f, 0.7265f, 0.8387f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5815f, 0.9363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9054f, 0.9516f, 0.8115f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9774f, 0.9329f, 0.7377f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9305f, 0.6369f, 0.7226f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8734f, 0.8457f, 0.7645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4184f, 0.6759f, 0.8897f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5631f, 0.9375f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4043f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6122f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 0.9991f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9999f, 0.9798f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 0.9918f, 0.9469f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9886f, 0.9655f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9996f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9993f, 0.9926f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9995f, 0.9430f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9844f, 0.9934f, 0.9883f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9595f, 0.9860f, 0.8930f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9626f, 0.7580f, 0.8715f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9419f, 0.9587f, 0.8754f, - 1.0000f, 1.0000f, 0.9736f, 0.9956f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9784f, 0.9669f, 0.9552f, - 1.0000f, 0.9945f, 0.9948f, 0.9976f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9682f, 0.9478f, 0.7832f, - 1.0000f, 0.9921f, 0.9603f, 0.9039f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9742f, 0.8434f, 0.9695f, - 1.0000f, 0.9789f, 0.9282f, 0.5534f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9440f, 0.7141f, 1.0000f, - 1.0000f, 0.9634f, 0.7446f, 0.3889f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9497f, 0.9903f, 1.0000f, - 1.0000f, 0.8828f, 0.4971f, 0.9909f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6912f, 1.0000f, 1.0000f, - 1.0000f, 0.9507f, 0.4594f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8978f, 1.0000f, 1.0000f, - 1.0000f, 0.7708f, 0.7612f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9835f, 0.9725f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9534f, 0.9813f, 0.8863f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9747f, 0.9152f, 0.7862f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9731f, 0.8243f, 0.8634f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9719f, 0.7097f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8772f, 0.9179f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9713f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8142f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9959f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9831f, 0.9943f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9929f, 0.9964f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9803f, 0.9868f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9817f, 0.9946f, 0.9408f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9986f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9967f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.9952f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.9875f, 0.9769f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9893f, 0.9525f, 0.9206f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9707f, 0.9245f, 0.5433f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9839f, 0.8571f, 0.5900f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9317f, 0.7185f, 0.6652f, - 1.0000f, 0.9941f, 0.9488f, 0.7922f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8363f, 1.0000f, 1.0000f, - 1.0000f, 0.9900f, 0.9578f, 0.9662f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9618f, 1.0000f, 1.0000f, - 1.0000f, 0.5093f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8410f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8339f, 0.9397f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8526f, 0.9975f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9998f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 1.0000f, 0.9872f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9989f, 0.9954f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9848f, 0.9999f, 0.9139f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9915f, 0.8791f, 0.6311f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9856f, 0.8810f, 0.8963f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7770f, 0.7341f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6597f, 0.9984f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, 0.9997f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 1.0000f, 0.9758f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9009f, 0.5208f, 0.9125f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8168f, 0.6484f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5321f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8087f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7170f, 0.8180f, 1.0000f, 1.0000f, 0.9306f, 0.9785f, - 1.0000f, 1.0000f, 1.0000f, 0.9950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6965f, 0.8011f, 0.8628f, 1.0000f, 1.0000f, 0.9641f, 0.9929f, - 1.0000f, 1.0000f, 0.9698f, 0.9907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8033f, 0.7695f, 0.5461f, 1.0000f, 1.0000f, 0.8906f, 0.8389f, - 1.0000f, 1.0000f, 0.9849f, 0.9943f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7269f, 0.7332f, 0.3842f, 1.0000f, 0.9127f, 0.9202f, 0.8794f, - 1.0000f, 1.0000f, 0.9070f, 0.9559f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7034f, 0.3723f, 0.2234f, 1.0000f, 0.8946f, 0.7922f, 0.3594f, - 1.0000f, 0.9242f, 0.9635f, 0.9682f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6392f, 0.0795f, 0.6486f, 1.0000f, 0.7642f, 0.7103f, 0.3052f, - 1.0000f, 1.0000f, 0.8823f, 0.8660f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0406f, 0.0000f, 1.0000f, 1.0000f, 0.7898f, 0.3960f, 0.1575f, - 1.0000f, 0.8530f, 0.7721f, 0.5161f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1334f, 0.2435f, 1.0000f, 1.0000f, 0.7069f, 0.3461f, 0.4915f, - 1.0000f, 1.0000f, 1.0000f, 0.9138f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9288f, 0.9617f, - 1.0000f, 1.0000f, 0.9366f, 0.9407f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9160f, 0.9468f, - 1.0000f, 1.0000f, 0.8933f, 0.9340f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8139f, 0.8408f, - 1.0000f, 0.9212f, 0.9105f, 0.8580f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8352f, 0.8809f, 0.4176f, - 1.0000f, 1.0000f, 0.8220f, 0.8893f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8780f, 0.5863f, 0.1090f, - 1.0000f, 0.8426f, 0.7589f, 0.5668f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6343f, 0.3327f, 0.2762f, - 1.0000f, 0.8720f, 0.7451f, 0.5388f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6818f, 0.2028f, 0.2913f, - 1.0000f, 0.7507f, 0.6293f, 0.2599f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4473f, 0.1808f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9896f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9781f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9823f, 0.9956f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9937f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9954f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9579f, 0.9919f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9838f, 0.9931f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9391f, 0.9827f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9374f, 0.9518f, 0.9034f, - 1.0000f, 0.9982f, 0.9996f, 0.9986f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8612f, 0.7136f, 0.7301f, 1.0000f, 0.9801f, 0.9909f, 0.9734f, - 1.0000f, 0.9995f, 0.9993f, 0.9789f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7669f, 0.7422f, 0.7773f, 1.0000f, 0.9664f, 0.9820f, 0.8240f, - 1.0000f, 0.9769f, 0.9901f, 0.9773f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4378f, 0.9438f, 1.0000f, 1.0000f, 0.6531f, 0.8119f, 0.9692f, - 1.0000f, 0.9460f, 0.9793f, 0.8750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6780f, 1.0000f, 1.0000f, 1.0000f, 0.8054f, 0.9116f, 0.9629f, - 1.0000f, 0.8596f, 0.8201f, 0.9399f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5237f, 0.8644f, 1.0000f, - 1.0000f, 0.7824f, 0.9357f, 0.9022f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7534f, 1.0000f, 1.0000f, - 1.0000f, 0.4659f, 0.8496f, 0.8997f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6741f, 0.9747f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8972f, 0.9423f, 0.7945f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8334f, 0.9171f, 0.9222f, - 1.0000f, 0.9805f, 0.9182f, 0.6930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9687f, 0.8735f, 0.8563f, - 1.0000f, 0.9415f, 0.6702f, 0.7850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5799f, 0.8444f, 1.0000f, - 1.0000f, 0.8879f, 0.8461f, 0.8160f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7000f, 0.9383f, 1.0000f, - 1.0000f, 0.4831f, 0.7799f, 0.9315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5602f, 1.0000f, 1.0000f, - 1.0000f, 0.6191f, 0.9572f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6242f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7967f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9988f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9997f, 0.9539f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9890f, 0.9192f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9869f, 0.9724f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9995f, 0.9914f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9987f, 0.9232f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9835f, 0.9933f, 0.9893f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9511f, 0.9812f, 0.8705f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9708f, 0.8076f, 0.9081f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9532f, 0.9703f, 0.8690f, - 1.0000f, 1.0000f, 0.9884f, 0.9991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9445f, 0.9332f, 0.8297f, 1.0000f, 0.9856f, 0.9760f, 0.9605f, - 1.0000f, 0.9981f, 0.9959f, 0.9989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9623f, 0.8278f, 0.9999f, 1.0000f, 0.9872f, 0.9552f, 0.6660f, - 1.0000f, 0.9979f, 0.9899f, 0.9525f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9676f, 0.9922f, 1.0000f, 1.0000f, 0.9598f, 0.6894f, 0.8513f, - 1.0000f, 0.9912f, 0.9430f, 0.6139f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8546f, 1.0000f, 1.0000f, 1.0000f, 0.8985f, 0.4070f, 1.0000f, - 1.0000f, 0.9752f, 0.8259f, 0.4566f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9475f, 1.0000f, 1.0000f, 1.0000f, 0.9058f, 0.9453f, 1.0000f, - 1.0000f, 0.8919f, 0.5734f, 0.9973f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4998f, 1.0000f, 1.0000f, - 1.0000f, 0.9566f, 0.4281f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9504f, 1.0000f, 1.0000f, - 1.0000f, 0.8098f, 0.6575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9887f, 0.9756f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9875f, 0.9647f, 0.7479f, - 1.0000f, 0.9653f, 0.9842f, 0.8959f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9670f, 0.9270f, 0.7203f, - 1.0000f, 0.9878f, 0.9483f, 0.8865f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9545f, 0.5978f, 1.0000f, - 1.0000f, 0.9765f, 0.9046f, 0.9490f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8479f, 1.0000f, 1.0000f, - 1.0000f, 0.9777f, 0.7875f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9093f, 1.0000f, 1.0000f, - 1.0000f, 0.9279f, 0.9585f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7362f, 1.0000f, 1.0000f, - 1.0000f, 0.9743f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, 0.9980f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 0.9969f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9852f, 0.9926f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9960f, 0.9963f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9816f, 0.9845f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9831f, 0.9882f, 0.9592f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9996f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9966f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9659f, 0.9297f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9714f, 0.9611f, 0.4745f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, 0.8371f, 0.5080f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8851f, 0.7393f, 0.6035f, - 1.0000f, 0.9976f, 0.9808f, 0.9149f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6439f, 1.0000f, 1.0000f, - 1.0000f, 0.9965f, 0.9729f, 0.9917f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9251f, 1.0000f, 1.0000f, - 1.0000f, 0.5922f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8837f, 0.9497f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8765f, 0.9992f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9994f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 1.0000f, 0.9738f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9797f, 0.9985f, 0.9935f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9904f, 0.9999f, 0.9010f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9866f, 0.7747f, 0.5533f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9719f, 0.7989f, 0.7562f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7616f, 0.6618f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6087f, 0.9975f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9941f, 0.9998f, 0.9859f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9261f, 0.5314f, 0.9323f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8675f, 0.6856f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3191f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7236f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8828f, 0.9380f, 1.0000f, 1.0000f, 0.9474f, 0.9876f, - 1.0000f, 1.0000f, 1.0000f, 0.9969f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9001f, 0.9507f, 0.9435f, 1.0000f, 1.0000f, 0.9629f, 0.9920f, - 1.0000f, 1.0000f, 0.9747f, 0.9932f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9144f, 0.8460f, 0.4607f, 1.0000f, 1.0000f, 0.8863f, 0.8325f, - 1.0000f, 1.0000f, 0.9864f, 0.9957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8000f, 0.7799f, 0.3655f, 1.0000f, 0.9060f, 0.9291f, 0.8517f, - 1.0000f, 1.0000f, 0.9234f, 0.9619f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7902f, 0.3573f, 0.2754f, 1.0000f, 0.9172f, 0.8197f, 0.3042f, - 1.0000f, 0.9550f, 0.9704f, 0.9787f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7244f, 0.0600f, 0.6069f, 1.0000f, 0.7961f, 0.7590f, 0.2856f, - 1.0000f, 1.0000f, 0.8770f, 0.8851f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5834f, 0.1053f, 1.0000f, 1.0000f, 0.8246f, 0.4735f, 0.1961f, - 1.0000f, 0.8503f, 0.7861f, 0.4860f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2949f, 0.5229f, 1.0000f, 1.0000f, 0.7710f, 0.3896f, 0.6023f, - 1.0000f, 1.0000f, 1.0000f, 0.9275f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8614f, 0.8180f, 1.0000f, 1.0000f, 0.9461f, 0.9783f, - 1.0000f, 1.0000f, 0.9358f, 0.9421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8474f, 0.8885f, 0.8356f, 1.0000f, 1.0000f, 0.9298f, 0.9798f, - 1.0000f, 1.0000f, 0.9126f, 0.9415f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8430f, 0.7410f, 0.3736f, 1.0000f, 1.0000f, 0.8719f, 0.8928f, - 1.0000f, 0.9321f, 0.9098f, 0.8586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7733f, 0.3401f, 0.2651f, 1.0000f, 0.8745f, 0.9217f, 0.4542f, - 1.0000f, 1.0000f, 0.8654f, 0.9107f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7686f, 0.1264f, 0.5169f, 1.0000f, 0.9040f, 0.7056f, 0.2329f, - 1.0000f, 0.8840f, 0.8074f, 0.6534f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5046f, 0.1459f, 1.0000f, 1.0000f, 0.7120f, 0.3314f, 0.5107f, - 1.0000f, 0.8874f, 0.7841f, 0.5784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4128f, 0.2091f, 1.0000f, 1.0000f, 0.7514f, 0.3225f, 0.5682f, - 1.0000f, 0.7778f, 0.6958f, 0.2441f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1650f, 1.0000f, 1.0000f, 1.0000f, 0.6114f, 0.3816f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6415f, 0.8110f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5576f, 0.6495f, 0.6684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7302f, 0.5465f, 0.1821f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7273f, 0.4671f, 0.4923f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6203f, 0.0829f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2546f, 0.4985f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9947f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9813f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0322f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9867f, 0.9970f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9794f, 0.9904f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9858f, 0.9968f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9428f, 0.9840f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9556f, 0.9613f, 0.8896f, - 1.0000f, 0.9990f, 0.9988f, 0.9993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9117f, 0.8401f, 0.9079f, 1.0000f, 0.9827f, 0.9897f, 0.9767f, - 1.0000f, 0.9996f, 0.9998f, 0.9881f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8386f, 0.9283f, 0.9242f, 1.0000f, 0.9579f, 0.9771f, 0.8092f, - 1.0000f, 0.9779f, 0.9849f, 0.9759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6247f, 0.9069f, 1.0000f, 1.0000f, 0.6374f, 0.8145f, 0.9365f, - 1.0000f, 0.9487f, 0.9717f, 0.8531f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8038f, 1.0000f, 1.0000f, 1.0000f, 0.7981f, 0.8817f, 0.9730f, - 1.0000f, 0.9030f, 0.8694f, 0.9680f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5882f, 0.8805f, 1.0000f, - 1.0000f, 0.8230f, 0.9650f, 0.9538f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7941f, 1.0000f, 1.0000f, - 1.0000f, 0.3975f, 0.8668f, 0.8960f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7088f, 0.9685f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8781f, 0.9441f, 0.7882f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6925f, 0.7213f, 0.9250f, 1.0000f, 0.8707f, 0.9344f, 0.9722f, - 1.0000f, 0.9873f, 0.9154f, 0.6991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7663f, 0.8416f, 0.9635f, 1.0000f, 0.9861f, 0.9135f, 0.9190f, - 1.0000f, 0.9267f, 0.6720f, 0.7540f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7488f, 1.0000f, 1.0000f, 1.0000f, 0.7182f, 0.9448f, 1.0000f, - 1.0000f, 0.8573f, 0.8163f, 0.8056f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8489f, 0.9739f, 1.0000f, - 1.0000f, 0.5288f, 0.8278f, 0.9624f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6611f, 1.0000f, 1.0000f, - 1.0000f, 0.6823f, 0.9775f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6891f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4478f, 0.6290f, 0.9660f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5629f, 0.8641f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9986f, 0.9960f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9998f, 0.9306f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9763f, 0.8559f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9690f, 0.9329f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9988f, 0.9892f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9994f, 0.9011f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9846f, 0.9934f, 0.9928f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9591f, 0.9902f, 0.8732f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9665f, 0.7755f, 0.8545f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9468f, 0.9525f, 0.8371f, - 1.0000f, 1.0000f, 0.9855f, 0.9995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9574f, 0.8980f, 0.7024f, 1.0000f, 0.9895f, 0.9806f, 0.9562f, - 1.0000f, 0.9980f, 0.9991f, 0.9994f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9455f, 0.7330f, 1.0000f, 1.0000f, 0.9823f, 0.9640f, 0.5930f, - 1.0000f, 0.9955f, 0.9843f, 0.9500f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9181f, 0.9481f, 1.0000f, 1.0000f, 0.9351f, 0.6857f, 0.7436f, - 1.0000f, 0.9899f, 0.9791f, 0.6789f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6159f, 1.0000f, 1.0000f, 1.0000f, 0.8758f, 0.2214f, 1.0000f, - 1.0000f, 0.9918f, 0.8793f, 0.5977f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9020f, 1.0000f, 1.0000f, 1.0000f, 0.8445f, 0.9050f, 1.0000f, - 1.0000f, 0.9258f, 0.6648f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4411f, 1.0000f, 1.0000f, - 1.0000f, 0.9694f, 0.4342f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9519f, 1.0000f, 1.0000f, - 1.0000f, 0.7921f, 0.7151f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9852f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9907f, 0.9816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9675f, 0.8681f, 0.9926f, 1.0000f, 0.9916f, 0.9387f, 0.7639f, - 1.0000f, 0.9585f, 0.9914f, 0.8991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9372f, 0.7615f, 1.0000f, 1.0000f, 0.9494f, 0.9088f, 0.6332f, - 1.0000f, 0.9909f, 0.9645f, 0.8917f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9655f, 1.0000f, 1.0000f, 1.0000f, 0.9544f, 0.4798f, 1.0000f, - 1.0000f, 0.9836f, 0.9225f, 0.9531f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7383f, 1.0000f, 1.0000f, 1.0000f, 0.8128f, 1.0000f, 1.0000f, - 1.0000f, 0.9820f, 0.8263f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9199f, 1.0000f, 1.0000f, - 1.0000f, 0.9568f, 0.9870f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6573f, 1.0000f, 1.0000f, - 1.0000f, 0.9743f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8341f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9608f, 0.9208f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9394f, 0.9726f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9708f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.9976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9922f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 0.9938f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9911f, 0.9973f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9965f, 0.9966f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9755f, 0.9884f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9602f, 0.9833f, 0.9163f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9950f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 0.9930f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9974f, 0.9964f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.9735f, 0.9670f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9802f, 0.9597f, 0.5407f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, 0.8294f, 0.3135f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8949f, 0.7356f, 0.5347f, - 1.0000f, 0.9990f, 0.9940f, 0.9336f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4200f, 1.0000f, 1.0000f, - 1.0000f, 0.9971f, 0.9713f, 0.9954f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8600f, 1.0000f, 1.0000f, - 1.0000f, 0.6754f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9408f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8970f, 0.9513f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 1.0000f, 1.0000f, - 1.0000f, 0.8939f, 0.9986f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9999f, 0.9993f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9997f, 0.9751f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9889f, 0.9992f, 0.9962f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9699f, 0.9997f, 0.8310f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9878f, 0.7462f, 0.5521f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9830f, 0.8019f, 0.8214f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6455f, 0.4052f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4271f, 0.9809f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 0.9999f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 1.0000f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9401f, 0.5733f, 0.9314f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8907f, 0.7566f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3488f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7820f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_5x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8780f, 0.9349f, 1.0000f, 1.0000f, 0.9364f, 0.9901f, - 1.0000f, 1.0000f, 1.0000f, 0.9975f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9180f, 0.9637f, 0.9656f, 1.0000f, 1.0000f, 0.9733f, 0.9894f, - 1.0000f, 1.0000f, 0.9802f, 0.9969f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9172f, 0.8662f, 0.5213f, 1.0000f, 1.0000f, 0.9146f, 0.8459f, - 1.0000f, 1.0000f, 0.9914f, 0.9959f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8116f, 0.8244f, 0.4387f, 1.0000f, 0.9313f, 0.9320f, 0.8625f, - 1.0000f, 1.0000f, 0.9589f, 0.9742f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8289f, 0.4504f, 0.4006f, 1.0000f, 0.9104f, 0.8562f, 0.3463f, - 1.0000f, 0.9494f, 0.9864f, 0.9861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7709f, 0.0893f, 0.6998f, 1.0000f, 0.8274f, 0.7886f, 0.3091f, - 1.0000f, 1.0000f, 0.9069f, 0.9188f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5676f, 0.1455f, 1.0000f, 1.0000f, 0.8362f, 0.5805f, 0.2600f, - 1.0000f, 0.8846f, 0.8197f, 0.5586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4675f, 0.6560f, 1.0000f, 1.0000f, 0.8099f, 0.4897f, 0.7086f, - 1.0000f, 1.0000f, 1.0000f, 0.9506f, 0.5058f, 1.0000f, 1.0000f, 1.0000f, 0.6937f, 1.0000f, 0.8698f, 0.8304f, 0.5929f, 1.0000f, 0.9540f, 0.9721f, - 1.0000f, 1.0000f, 0.9605f, 0.9616f, 0.6722f, 1.0000f, 1.0000f, 1.0000f, 0.7223f, 0.8835f, 0.8745f, 0.8889f, 0.3242f, 1.0000f, 0.9450f, 0.9642f, - 1.0000f, 1.0000f, 0.9391f, 0.9568f, 0.7142f, 1.0000f, 1.0000f, 1.0000f, 0.3317f, 0.8523f, 0.7828f, 0.4842f, 0.7446f, 1.0000f, 0.8549f, 0.8587f, - 1.0000f, 0.9557f, 0.9268f, 0.8710f, 0.6050f, 1.0000f, 1.0000f, 1.0000f, 0.1696f, 0.7997f, 0.4265f, 0.4326f, 1.0000f, 0.8498f, 0.8909f, 0.3167f, - 1.0000f, 1.0000f, 0.8757f, 0.9244f, 0.6816f, 1.0000f, 1.0000f, 1.0000f, 0.6457f, 0.7789f, 0.1325f, 0.5541f, 1.0000f, 0.8939f, 0.6968f, 0.2032f, - 1.0000f, 0.9129f, 0.8066f, 0.6280f, 0.0533f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5312f, 0.1581f, 1.0000f, 1.0000f, 0.7169f, 0.2931f, 0.4787f, - 1.0000f, 0.8929f, 0.8149f, 0.6526f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5450f, 0.3874f, 1.0000f, 1.0000f, 0.7423f, 0.3391f, 0.5763f, - 1.0000f, 0.8333f, 0.7646f, 0.3535f, 0.6422f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2241f, 1.0000f, 1.0000f, 1.0000f, 0.6785f, 0.3807f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8674f, 0.9033f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8347f, 0.7942f, 0.6690f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8734f, 0.5496f, 0.2141f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7749f, 0.3603f, 0.5970f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7375f, 0.0330f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3012f, 0.3739f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9929f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6492f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9923f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9856f, 0.9963f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7513f, 0.7961f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8376f, 0.8536f, 0.4618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7468f, 0.2849f, 0.4137f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7114f, 0.1048f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6207f, 0.1186f, 1.0000f, 1.0000f, 1.0000f, 0.9799f, 0.9960f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1811f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 0.9967f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5263f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9573f, 0.9910f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2429f, 1.0000f, 1.0000f, 1.0000f, 0.9562f, 0.9792f, 0.8977f, - 1.0000f, 0.9987f, 0.9976f, 0.9994f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9357f, 0.8445f, 0.9535f, 1.0000f, 0.9769f, 0.9828f, 0.9680f, - 1.0000f, 0.9998f, 1.0000f, 0.9899f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8650f, 0.9438f, 0.9196f, 1.0000f, 0.9237f, 0.9762f, 0.7729f, - 1.0000f, 0.9844f, 0.9892f, 0.9795f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7301f, 0.9398f, 1.0000f, 1.0000f, 0.6244f, 0.8165f, 0.8919f, - 1.0000f, 0.9675f, 0.9853f, 0.8485f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8612f, 1.0000f, 1.0000f, 1.0000f, 0.7847f, 0.8802f, 0.9847f, - 1.0000f, 0.9051f, 0.8722f, 0.9578f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6387f, 0.9024f, 1.0000f, - 1.0000f, 0.8318f, 0.9611f, 0.9600f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8432f, 1.0000f, 1.0000f, - 1.0000f, 0.4446f, 0.8996f, 0.9405f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7351f, 0.9816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8768f, 0.9431f, 0.7808f, 0.4202f, 1.0000f, 1.0000f, 1.0000f, 0.9703f, 0.7249f, 0.8181f, 0.9716f, 1.0000f, 0.8213f, 0.9260f, 0.9475f, - 1.0000f, 0.9850f, 0.9163f, 0.6626f, 0.6317f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8133f, 0.8987f, 0.9841f, 1.0000f, 0.9750f, 0.8879f, 0.9213f, - 1.0000f, 0.9488f, 0.6877f, 0.7688f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7867f, 1.0000f, 1.0000f, 1.0000f, 0.6130f, 0.9204f, 1.0000f, - 1.0000f, 0.8968f, 0.8418f, 0.8228f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8032f, 0.9512f, 1.0000f, - 1.0000f, 0.4951f, 0.8390f, 0.9651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6010f, 1.0000f, 1.0000f, - 1.0000f, 0.6658f, 0.9781f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7667f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9155f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5162f, 0.8049f, 0.9694f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6846f, 0.9784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9988f, 0.9912f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9994f, 0.9005f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9758f, 0.8259f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9551f, 0.9384f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5888f, 0.8813f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7491f, 0.9729f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9993f, 0.9921f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9991f, 0.9060f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9831f, 0.9925f, 0.9869f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9500f, 0.9777f, 0.8574f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9670f, 0.7979f, 0.8511f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9463f, 0.9661f, 0.9298f, - 1.0000f, 1.0000f, 0.9917f, 0.9996f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9482f, 0.9112f, 0.6352f, 1.0000f, 0.9887f, 0.9523f, 0.9517f, - 1.0000f, 0.9999f, 0.9989f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9078f, 0.7057f, 1.0000f, 1.0000f, 0.9595f, 0.9646f, 0.5110f, - 1.0000f, 0.9984f, 0.9938f, 0.9773f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9121f, 0.9015f, 1.0000f, 1.0000f, 0.9707f, 0.6754f, 0.7196f, - 1.0000f, 0.9940f, 0.9872f, 0.7769f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5005f, 1.0000f, 1.0000f, 1.0000f, 0.8948f, 0.1922f, 1.0000f, - 1.0000f, 0.9858f, 0.8824f, 0.5720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8899f, 1.0000f, 1.0000f, 1.0000f, 0.9411f, 0.9335f, 1.0000f, - 1.0000f, 0.9229f, 0.7275f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5358f, 1.0000f, 1.0000f, - 1.0000f, 0.9631f, 0.5847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9788f, 1.0000f, 1.0000f, - 1.0000f, 0.8686f, 0.8014f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9919f, 0.9927f, 0.9546f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9584f, 0.8404f, 0.9689f, 1.0000f, 0.9954f, 0.9095f, 0.6907f, - 1.0000f, 0.9685f, 0.9941f, 0.9290f, 0.9086f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9252f, 0.6168f, 1.0000f, 1.0000f, 0.9283f, 0.8857f, 0.4072f, - 1.0000f, 0.9943f, 0.9806f, 0.9275f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9444f, 1.0000f, 1.0000f, 1.0000f, 0.9469f, 0.2685f, 1.0000f, - 1.0000f, 0.9885f, 0.9666f, 0.9825f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5405f, 1.0000f, 1.0000f, 1.0000f, 0.7580f, 1.0000f, 1.0000f, - 1.0000f, 0.9946f, 0.8791f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8637f, 1.0000f, 1.0000f, - 1.0000f, 0.9698f, 0.9903f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5631f, 1.0000f, 1.0000f, - 1.0000f, 0.9834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8600f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9738f, 0.7923f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9221f, 0.9371f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9378f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9754f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9955f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 0.9949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9972f, 0.9933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9867f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9725f, 0.9877f, 0.9305f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9342f, 0.8082f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9418f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9962f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9951f, 0.9952f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9819f, 0.9626f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9882f, 0.9529f, 0.4562f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.8958f, 0.3672f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9042f, 0.7535f, 0.6090f, - 1.0000f, 0.9993f, 0.9897f, 0.9621f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2515f, 1.0000f, 1.0000f, - 1.0000f, 0.9978f, 0.9837f, 0.9970f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7602f, 1.0000f, 1.0000f, - 1.0000f, 0.7905f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9138f, 0.9457f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9934f, 1.0000f, 1.0000f, - 1.0000f, 0.9327f, 0.9987f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9992f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 1.0000f, 0.9822f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9956f, 0.9985f, 0.9874f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9880f, 0.9998f, 0.8472f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9908f, 0.7326f, 0.4731f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9425f, 0.7399f, 0.7624f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7027f, 0.2767f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3941f, 0.9812f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9931f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9809f, 0.6593f, 0.9766f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9746f, 0.8868f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2341f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7558f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8992f, 0.9737f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9398f, 0.9849f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7787f, 0.8814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8128f, 0.8718f, 0.9182f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9354f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9262f, 0.9793f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8501f, 0.9493f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8559f, 0.8693f, 0.8377f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7200f, 0.7741f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7542f, 0.6070f, 0.3882f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6630f, 0.8167f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6893f, 0.8641f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7078f, 0.6828f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7595f, 0.7647f, 0.6763f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2717f, 0.4628f, 0.0876f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4109f, 0.2335f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9467f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9664f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9823f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8743f, 0.9454f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9054f, 0.9628f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9904f, 0.9993f, 0.9982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9974f, 0.9998f, 0.9817f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9787f, 0.9897f, 0.9834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9530f, 0.9767f, 0.8767f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8205f, 0.7318f, 0.8008f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7259f, 0.8048f, 0.8471f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9967f, 0.9893f, 0.9427f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9959f, 0.9828f, 0.9619f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9231f, 0.7139f, 0.8242f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8883f, 0.9294f, 0.8277f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3079f, 0.5404f, 0.8311f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3622f, 0.8614f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9073f, 0.9384f, 0.8949f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8928f, 0.9148f, 0.5980f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4291f, 0.5542f, 0.8837f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 1.0000f, - 1.0000f, 0.5667f, 0.7434f, 0.8905f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9998f, - 1.0000f, 0.1611f, 0.4950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9656f, 0.9844f, 0.9934f, - 1.0000f, 0.3359f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9811f, 0.9920f, 0.9165f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9987f, 0.9908f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9988f, 0.9564f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9698f, 0.9799f, 0.9598f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9553f, 0.9638f, 0.7878f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9881f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9948f, 0.9980f, 0.9950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9876f, 0.9413f, 0.8860f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9752f, 0.9130f, 0.5107f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9690f, 0.8530f, 0.5777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9441f, 0.6160f, 0.9985f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9858f, 0.9714f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9730f, 0.9868f, 0.9247f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9781f, 0.8089f, 0.4789f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9092f, 0.6244f, 0.5880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9745f, 0.6409f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8587f, 0.9215f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9722f, 0.9647f, 0.8790f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9805f, 0.9198f, 0.7377f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9681f, 0.6955f, 0.9608f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9979f, - 1.0000f, 0.9309f, 0.6327f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9977f, - 1.0000f, 0.9481f, 0.9961f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9706f, 0.9911f, - 1.0000f, 0.6560f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9926f, 0.9923f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9914f, 0.9901f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9885f, 0.9863f, 0.9760f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9872f, 0.9774f, 0.9505f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9889f, 0.9576f, 0.7488f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9917f, 0.9013f, 0.6696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9854f, 0.8971f, 0.8668f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4462f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7695f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8345f, 0.7965f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6489f, 0.9983f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8440f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9999f, - 1.0000f, 0.9541f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9999f, 0.9971f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9994f, 0.9976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.9998f, 0.9518f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9369f, 0.7921f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9839f, 0.9278f, 0.9325f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9990f, 0.9941f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9999f, 0.9587f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9033f, 0.5262f, 0.9340f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8409f, 0.7016f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7833f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9111f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9817f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9250f, 0.9803f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9595f, 0.9914f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8438f, 0.9238f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8903f, 0.9454f, 0.9562f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7381f, 0.8169f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7219f, 0.6800f, 0.5402f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9620f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9587f, 0.9847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9045f, 0.9696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9090f, 0.9325f, 0.8704f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7951f, 0.7921f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7766f, 0.6848f, 0.4307f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8008f, 0.6425f, 0.3692f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6596f, 0.5073f, 0.0799f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6702f, 0.8143f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7040f, 0.8816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7600f, 0.7419f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7980f, 0.8246f, 0.7891f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7633f, 0.6896f, 0.3081f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6364f, 0.6026f, 0.2805f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9866f, - 1.0000f, 0.2115f, 0.1301f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8951f, - 1.0000f, 0.4014f, 0.1727f, 0.2470f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9420f, 0.9300f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9825f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9881f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9263f, 0.9812f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9628f, 0.9891f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8524f, 0.9186f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8798f, 0.8626f, 0.8545f, - 1.0000f, 0.9939f, 0.9989f, 0.9978f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9993f, 0.9996f, 0.9755f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9799f, 0.9922f, 0.9908f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9604f, 0.9838f, 0.8919f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8415f, 0.8344f, 0.9288f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7531f, 0.9225f, 0.8834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4445f, 0.6235f, 0.8886f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5191f, 0.8967f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9979f, 0.9874f, 0.9349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9959f, 0.9834f, 0.9675f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9544f, 0.7494f, 0.8586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9408f, 0.9535f, 0.8645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4167f, 0.7456f, 0.8320f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5864f, 0.9465f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3509f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6167f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8935f, 0.9199f, 0.8742f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8665f, 0.9029f, 0.5948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5687f, 0.6301f, 0.9431f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 1.0000f, - 1.0000f, 0.6484f, 0.8195f, 0.9385f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9997f, - 1.0000f, 0.3314f, 0.7733f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9996f, 0.9991f, - 1.0000f, 0.6098f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9998f, 0.9842f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9570f, 0.9775f, 0.9312f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, 0.9780f, 0.8852f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9993f, 0.9925f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9991f, 0.9487f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9765f, 0.9895f, 0.9808f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9507f, 0.9760f, 0.8565f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9517f, 0.7342f, 0.7700f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9337f, 0.9133f, 0.7799f, - 1.0000f, 1.0000f, 0.9851f, 0.9977f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9980f, 0.9956f, 0.9962f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9870f, 0.9644f, 0.9212f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9722f, 0.9396f, 0.5301f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9579f, 0.7566f, 0.3857f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8779f, 0.4950f, 0.9957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9683f, 0.7175f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8761f, 0.9014f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9885f, 0.9794f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9750f, 0.9932f, 0.9443f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9830f, 0.8606f, 0.5777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9373f, 0.7302f, 0.6752f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9612f, 0.4702f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7861f, 0.7667f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9690f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8296f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9667f, 0.9716f, 0.8684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9739f, 0.9361f, 0.6992f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9785f, 0.8035f, 0.9821f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9990f, - 1.0000f, 0.9636f, 0.7260f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9986f, - 1.0000f, 0.9553f, 0.9930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9904f, 0.9954f, - 1.0000f, 0.6650f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9948f, 0.9982f, - 1.0000f, 0.8998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9790f, 0.9770f, - 1.0000f, 0.9952f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9703f, 0.9911f, 0.9660f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9917f, 0.9935f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9888f, 0.9898f, 0.9859f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9901f, 0.9710f, 0.9119f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9733f, 0.9476f, 0.5594f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9728f, 0.9104f, 0.7830f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9652f, 0.8461f, 0.8116f, - 1.0000f, 0.9944f, 0.9275f, 0.7131f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9937f, 0.9173f, 0.9147f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4827f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8221f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8368f, 0.8392f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7086f, 0.9984f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8503f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9999f, 0.9999f, - 1.0000f, 0.9744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9997f, 0.9976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9941f, 0.9999f, 0.9526f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9920f, 0.8723f, 0.6944f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9855f, 0.8482f, 0.9160f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9060f, 0.8869f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8271f, 0.9965f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9985f, 0.9992f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9877f, 0.9998f, 0.9497f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9075f, 0.4576f, 0.8983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8063f, 0.6541f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5499f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8090f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8106f, 0.8853f, - 1.0000f, 1.0000f, 1.0000f, 0.9930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7864f, 0.9450f, - 1.0000f, 1.0000f, 0.9513f, 0.9905f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8216f, 0.8013f, - 1.0000f, 1.0000f, 0.9714f, 0.9946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8453f, 0.8645f, 0.8629f, - 1.0000f, 1.0000f, 0.8899f, 0.9579f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8525f, 0.7728f, 0.4573f, - 1.0000f, 0.9120f, 0.9600f, 0.9709f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7079f, 0.7007f, 0.2804f, - 1.0000f, 1.0000f, 0.8434f, 0.8542f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7615f, 0.3425f, 0.0000f, - 1.0000f, 0.8340f, 0.7439f, 0.5516f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6893f, 0.2428f, 0.2979f, - 1.0000f, 1.0000f, 1.0000f, 0.9744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9764f, 0.9869f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9177f, 0.9724f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9199f, 0.9401f, 0.8679f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8379f, 0.8560f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8279f, 0.7219f, 0.5251f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8471f, 0.7185f, 0.4349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6933f, 0.5759f, 0.2166f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.6970f, 0.8397f, 0.0794f, 1.0000f, 1.0000f, 1.0000f, 0.0403f, 1.0000f, 1.0000f, 1.0000f, 0.3571f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8320f, 0.9095f, 0.1892f, 1.0000f, 1.0000f, 1.0000f, 0.1171f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7916f, 0.7837f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8259f, 0.8489f, 0.8083f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7965f, 0.7528f, 0.4100f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6853f, 0.6812f, 0.3279f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9897f, - 1.0000f, 0.7252f, 0.3130f, 0.1542f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9807f, - 1.0000f, 0.6646f, 0.2621f, 0.3846f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9833f, 0.9944f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9903f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9908f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9458f, 0.9895f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9719f, 0.9889f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9252f, 0.9749f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9357f, 0.9375f, 0.8957f, - 1.0000f, 0.9957f, 0.9997f, 0.9982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9607f, 0.9729f, 0.9155f, - 1.0000f, 0.9989f, 0.9999f, 0.9794f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9409f, 0.9572f, 0.6555f, - 1.0000f, 0.9773f, 0.9910f, 0.9844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6508f, 0.6154f, 0.9056f, - 1.0000f, 0.9466f, 0.9803f, 0.8823f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7114f, 0.8151f, 0.9434f, - 1.0000f, 0.8869f, 0.8760f, 0.9392f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3710f, 0.9015f, 1.0000f, - 1.0000f, 0.7941f, 0.9426f, 0.9082f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6312f, 1.0000f, 1.0000f, - 1.0000f, 0.5069f, 0.8129f, 0.8928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6411f, 0.9586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9978f, 0.9913f, 0.9302f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9988f, 0.9826f, 0.9482f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9543f, 0.7284f, 0.8695f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9242f, 0.9339f, 0.8986f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4975f, 0.8360f, 0.8884f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6460f, 0.9647f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5680f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6601f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9132f, 0.9166f, 0.8416f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8728f, 0.9029f, 0.5837f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5908f, 0.6261f, 0.9292f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 1.0000f, - 1.0000f, 0.6362f, 0.8195f, 0.9282f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9999f, - 1.0000f, 0.4781f, 0.9043f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9995f, 0.9986f, - 1.0000f, 0.7043f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9999f, 0.9786f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9940f, 0.9593f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9887f, 0.9687f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9996f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9981f, 0.9841f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9974f, 0.9188f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9777f, 0.9915f, 0.9881f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9474f, 0.9830f, 0.8943f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9621f, 0.7700f, 0.8711f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9330f, 0.9550f, 0.8838f, - 1.0000f, 1.0000f, 0.9925f, 0.9987f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9818f, 0.9790f, 0.9703f, - 1.0000f, 0.9969f, 0.9984f, 0.9996f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9854f, 0.9698f, 0.8662f, - 1.0000f, 0.9942f, 0.9628f, 0.9143f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9768f, 0.8776f, 0.9754f, - 1.0000f, 0.9739f, 0.9498f, 0.5161f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9505f, 0.7408f, 1.0000f, - 1.0000f, 0.9653f, 0.8037f, 0.4225f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9348f, 0.9918f, 1.0000f, - 1.0000f, 0.8507f, 0.5341f, 0.9892f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7469f, 1.0000f, 1.0000f, - 1.0000f, 0.9520f, 0.4880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9490f, 1.0000f, 1.0000f, - 1.0000f, 0.8173f, 0.7557f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9875f, 0.9867f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9682f, 0.9952f, 0.9614f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9848f, 0.8791f, 0.6099f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9321f, 0.7499f, 0.7315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9418f, 0.5430f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8238f, 0.7586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9535f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6208f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9692f, 0.9798f, 0.8913f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9811f, 0.9383f, 0.7378f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, - 1.0000f, 0.9670f, 0.8300f, 0.9822f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9980f, - 1.0000f, 0.9665f, 0.7347f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9955f, - 1.0000f, 0.9565f, 0.9934f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9815f, 0.9947f, - 1.0000f, 0.7149f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9961f, 0.9976f, - 1.0000f, 0.9659f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9863f, 0.9938f, - 1.0000f, 0.9951f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9759f, 0.9977f, 0.9640f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9900f, 0.9954f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9857f, 0.9782f, 0.9837f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9920f, 0.9557f, 0.9272f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9734f, 0.9231f, 0.5598f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9851f, 0.8612f, 0.6039f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9262f, 0.7755f, 0.6689f, - 1.0000f, 0.9949f, 0.9311f, 0.7890f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8578f, 1.0000f, 1.0000f, - 1.0000f, 0.9960f, 0.9366f, 0.9442f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9634f, 1.0000f, 1.0000f, - 1.0000f, 0.4464f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7671f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9001f, 0.9107f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7783f, 0.9998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9990f, 0.9997f, - 1.0000f, 0.9676f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9932f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9860f, 0.9994f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9872f, 1.0000f, 0.9210f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 0.9069f, 0.6771f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9884f, 0.8972f, 0.9221f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8060f, 0.7989f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6730f, 0.9988f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9878f, 0.9992f, 0.9985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9923f, 0.9998f, 0.9528f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8807f, 0.3973f, 0.8595f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7643f, 0.5978f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4681f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7811f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7507f, 0.8312f, 1.0000f, 1.0000f, 0.9510f, 0.9872f, - 1.0000f, 1.0000f, 1.0000f, 0.9960f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7791f, 0.8374f, 0.8893f, 1.0000f, 1.0000f, 0.9809f, 0.9948f, - 1.0000f, 1.0000f, 0.9710f, 0.9957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8389f, 0.7750f, 0.5758f, 1.0000f, 1.0000f, 0.8996f, 0.8296f, - 1.0000f, 1.0000f, 0.9880f, 0.9956f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7361f, 0.7599f, 0.3527f, 1.0000f, 0.9236f, 0.9323f, 0.8634f, - 1.0000f, 1.0000f, 0.9331f, 0.9540f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7483f, 0.4613f, 0.2516f, 1.0000f, 0.9254f, 0.8264f, 0.4148f, - 1.0000f, 0.9441f, 0.9799f, 0.9751f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7013f, 0.1466f, 0.7850f, 1.0000f, 0.7870f, 0.7459f, 0.3889f, - 1.0000f, 1.0000f, 0.8858f, 0.8905f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6250f, 0.0671f, 1.0000f, 1.0000f, 0.8434f, 0.4063f, 0.1118f, - 1.0000f, 0.8578f, 0.7947f, 0.5703f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3140f, 0.4470f, 1.0000f, 1.0000f, 0.7261f, 0.2774f, 0.4683f, - 1.0000f, 1.0000f, 1.0000f, 0.9918f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9600f, 0.9795f, - 1.0000f, 1.0000f, 0.9890f, 0.9950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9306f, 0.9573f, - 1.0000f, 1.0000f, 0.9551f, 0.9818f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8216f, 0.8535f, - 1.0000f, 0.9516f, 0.9534f, 0.9040f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8620f, 0.9083f, 0.4231f, - 1.0000f, 1.0000f, 0.8962f, 0.9007f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9157f, 0.6601f, 0.1800f, - 1.0000f, 0.8916f, 0.7966f, 0.5647f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6677f, 0.3618f, 0.3343f, - 1.0000f, 0.9167f, 0.7927f, 0.5342f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7386f, 0.1634f, 0.3020f, - 1.0000f, 0.7687f, 0.6559f, 0.2897f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5591f, 0.1293f, 1.0000f, - 1.0000f, 1.0000f, 0.7435f, 0.8798f, 0.6474f, 1.0000f, 1.0000f, 1.0000f, 0.1950f, 1.0000f, 1.0000f, 1.0000f, 0.5959f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9051f, 0.9314f, 0.5085f, 1.0000f, 1.0000f, 1.0000f, 0.2645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8507f, 0.8248f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8881f, 0.8973f, 0.8419f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8564f, 0.8059f, 0.5150f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7411f, 0.7729f, 0.4394f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, - 1.0000f, 0.7984f, 0.5279f, 0.2378f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, - 1.0000f, 0.7531f, 0.4313f, 0.5909f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9864f, 0.9927f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9947f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9646f, 0.9920f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9827f, 0.9940f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9461f, 0.9839f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9474f, 0.9568f, 0.8674f, - 1.0000f, 0.9972f, 0.9996f, 0.9991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8700f, 0.7129f, 0.7156f, 1.0000f, 0.9915f, 0.9922f, 0.9770f, - 1.0000f, 0.9998f, 0.9999f, 0.9815f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7708f, 0.7337f, 0.7771f, 1.0000f, 0.9651f, 0.9902f, 0.8449f, - 1.0000f, 0.9792f, 0.9931f, 0.9885f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4819f, 0.9546f, 1.0000f, 1.0000f, 0.6639f, 0.8232f, 0.9697f, - 1.0000f, 0.9454f, 0.9774f, 0.9093f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7043f, 1.0000f, 1.0000f, 1.0000f, 0.8022f, 0.9115f, 0.9631f, - 1.0000f, 0.8822f, 0.8761f, 0.9378f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6340f, 0.9207f, 1.0000f, - 1.0000f, 0.8095f, 0.9562f, 0.9197f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8182f, 1.0000f, 1.0000f, - 1.0000f, 0.5019f, 0.9029f, 0.9281f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7209f, 0.9777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9995f, 0.9925f, 0.9467f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8687f, 0.9347f, 0.9289f, - 1.0000f, 0.9990f, 0.9851f, 0.9589f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9719f, 0.8869f, 0.8846f, - 1.0000f, 0.9675f, 0.7889f, 0.9072f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6058f, 0.8521f, 1.0000f, - 1.0000f, 0.9413f, 0.9492f, 0.9339f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7182f, 0.9400f, 1.0000f, - 1.0000f, 0.4953f, 0.9136f, 0.9272f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6714f, 1.0000f, 1.0000f, - 1.0000f, 0.7101f, 0.9802f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6918f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7665f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9245f, 0.9187f, 0.8492f, 0.5531f, 1.0000f, 1.0000f, 1.0000f, 0.0934f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8606f, 0.8951f, 0.5215f, 0.2100f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6295f, 0.6886f, 0.9522f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, - 1.0000f, 0.6750f, 0.8550f, 0.9528f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9996f, - 1.0000f, 0.6009f, 0.9498f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9995f, 0.9980f, - 1.0000f, 0.8148f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 1.0000f, 0.9504f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9911f, 0.9298f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9875f, 0.9731f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9987f, 0.9788f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9971f, 0.8984f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9684f, 0.9898f, 0.9833f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9363f, 0.9743f, 0.8358f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9665f, 0.7643f, 0.8737f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9370f, 0.9610f, 0.8786f, - 1.0000f, 1.0000f, 0.9942f, 0.9964f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9693f, 0.9661f, 0.8592f, 1.0000f, 0.9913f, 0.9755f, 0.9688f, - 1.0000f, 0.9988f, 0.9970f, 0.9993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9723f, 0.9177f, 1.0000f, 1.0000f, 0.9883f, 0.9762f, 0.6982f, - 1.0000f, 0.9938f, 0.9888f, 0.9595f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9605f, 0.9895f, 1.0000f, 1.0000f, 0.9486f, 0.7235f, 0.8774f, - 1.0000f, 0.9867f, 0.9679f, 0.5809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8464f, 1.0000f, 1.0000f, 1.0000f, 0.9146f, 0.3977f, 1.0000f, - 1.0000f, 0.9739f, 0.8404f, 0.3800f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9727f, 1.0000f, 1.0000f, 1.0000f, 0.8648f, 0.9226f, 1.0000f, - 1.0000f, 0.8939f, 0.5469f, 0.9983f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4542f, 1.0000f, 1.0000f, - 1.0000f, 0.9480f, 0.3251f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9217f, 1.0000f, 1.0000f, - 1.0000f, 0.7621f, 0.6518f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9621f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9935f, 0.9936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9836f, 0.9848f, 0.7831f, - 1.0000f, 0.9878f, 0.9959f, 0.9714f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9702f, 0.9406f, 0.7811f, - 1.0000f, 0.9953f, 0.9385f, 0.7312f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9579f, 0.6430f, 1.0000f, - 1.0000f, 0.9641f, 0.8749f, 0.8810f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8478f, 1.0000f, 1.0000f, - 1.0000f, 0.9584f, 0.6385f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9061f, 1.0000f, 1.0000f, - 1.0000f, 0.8280f, 0.8199f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6950f, 1.0000f, 1.0000f, - 1.0000f, 0.9434f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6155f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9806f, 0.9766f, 0.9104f, 0.0354f, 1.0000f, 1.0000f, 1.0000f, 0.4752f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9900f, 0.9427f, 0.7577f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, - 1.0000f, 0.9785f, 0.9125f, 0.9977f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9994f, - 1.0000f, 0.9812f, 0.8077f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9929f, - 1.0000f, 0.9616f, 0.9968f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9905f, - 1.0000f, 0.7908f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 0.9954f, 0.9984f, - 1.0000f, 0.9557f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9821f, 0.9909f, - 1.0000f, 0.9853f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9830f, 0.9924f, 0.9636f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9978f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9973f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9706f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9945f, 0.9933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9392f, 0.9263f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9670f, 0.9355f, 0.3709f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9907f, 0.8165f, 0.4886f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8724f, 0.7287f, 0.5860f, - 1.0000f, 0.9992f, 0.9759f, 0.8661f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6819f, 1.0000f, 1.0000f, - 1.0000f, 0.9986f, 0.9747f, 0.9842f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9447f, 1.0000f, 1.0000f, - 1.0000f, 0.5406f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9420f, 0.9656f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 1.0000f, 1.0000f, - 1.0000f, 0.9018f, 0.9996f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9999f, 0.9999f, - 1.0000f, 0.9626f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9781f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, 0.9994f, 0.9969f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9856f, 0.9998f, 0.8928f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 0.8003f, 0.6107f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9845f, 0.8113f, 0.8131f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8041f, 0.7073f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6785f, 0.9989f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9893f, 1.0000f, 0.9987f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9870f, 1.0000f, 0.9735f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8327f, 0.3436f, 0.8343f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7554f, 0.6203f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2239f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6852f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9113f, 0.9605f, 1.0000f, 1.0000f, 0.9651f, 0.9905f, - 1.0000f, 1.0000f, 1.0000f, 0.9986f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9297f, 0.9572f, 0.9655f, 1.0000f, 1.0000f, 0.9680f, 0.9938f, - 1.0000f, 1.0000f, 0.9769f, 0.9966f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9434f, 0.8648f, 0.4724f, 1.0000f, 1.0000f, 0.9029f, 0.8206f, - 1.0000f, 1.0000f, 0.9929f, 0.9937f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8133f, 0.7768f, 0.4066f, 1.0000f, 0.9338f, 0.9224f, 0.8220f, - 1.0000f, 1.0000f, 0.9384f, 0.9696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8118f, 0.4443f, 0.3663f, 1.0000f, 0.9358f, 0.8449f, 0.3799f, - 1.0000f, 0.9624f, 0.9881f, 0.9847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7514f, 0.0924f, 0.7271f, 1.0000f, 0.8057f, 0.7892f, 0.3453f, - 1.0000f, 1.0000f, 0.8889f, 0.8976f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7575f, 0.0336f, 1.0000f, 1.0000f, 0.8849f, 0.4385f, 0.1879f, - 1.0000f, 0.8909f, 0.8072f, 0.5233f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3074f, 0.3933f, 1.0000f, 1.0000f, 0.7857f, 0.2391f, 0.5425f, - 1.0000f, 1.0000f, 1.0000f, 0.9927f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8869f, 0.8500f, 1.0000f, 1.0000f, 0.9712f, 0.9720f, - 1.0000f, 1.0000f, 0.9890f, 0.9970f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8985f, 0.9391f, 0.8695f, 1.0000f, 1.0000f, 0.9446f, 0.9736f, - 1.0000f, 1.0000f, 0.9659f, 0.9844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8659f, 0.7674f, 0.4262f, 1.0000f, 1.0000f, 0.8474f, 0.8900f, - 1.0000f, 0.9692f, 0.9783f, 0.9020f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7804f, 0.4000f, 0.3231f, 1.0000f, 0.8838f, 0.9397f, 0.4559f, - 1.0000f, 1.0000f, 0.9246f, 0.9311f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7994f, 0.1379f, 0.6466f, 1.0000f, 0.9208f, 0.7374f, 0.3153f, - 1.0000f, 0.9201f, 0.8397f, 0.6361f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5610f, 0.1510f, 1.0000f, 1.0000f, 0.7219f, 0.3523f, 0.5786f, - 1.0000f, 0.9177f, 0.8423f, 0.5185f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5656f, 0.1641f, 1.0000f, 1.0000f, 0.7875f, 0.2300f, 0.4935f, - 1.0000f, 0.8192f, 0.6885f, 0.2571f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1230f, 1.0000f, 1.0000f, 1.0000f, 0.6501f, 0.2916f, 1.0000f, - 1.0000f, 1.0000f, 0.7712f, 0.9063f, 0.6824f, 1.0000f, 1.0000f, 1.0000f, 0.2659f, 1.0000f, 0.6762f, 0.7749f, 0.7348f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9216f, 0.9193f, 0.6252f, 1.0000f, 1.0000f, 1.0000f, 0.2995f, 0.6215f, 0.7030f, 0.6915f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8550f, 0.8318f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7654f, 0.5869f, 0.2099f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8994f, 0.8752f, 0.8277f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7534f, 0.4778f, 0.5086f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9011f, 0.8624f, 0.5987f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6666f, 0.1080f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7909f, 0.8291f, 0.4987f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4197f, 0.6026f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, - 1.0000f, 0.8088f, 0.5700f, 0.2747f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6289f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, - 1.0000f, 0.7731f, 0.4670f, 0.6855f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0749f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9903f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9799f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9886f, 0.9922f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9525f, 0.9790f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9628f, 0.9610f, 0.8487f, - 1.0000f, 0.9992f, 0.9991f, 0.9990f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9254f, 0.8817f, 0.9231f, 1.0000f, 0.9836f, 0.9925f, 0.9776f, - 1.0000f, 0.9997f, 0.9999f, 0.9897f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8636f, 0.9331f, 0.9504f, 1.0000f, 0.9577f, 0.9838f, 0.8148f, - 1.0000f, 0.9821f, 0.9899f, 0.9849f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6730f, 0.9046f, 1.0000f, 1.0000f, 0.6103f, 0.8162f, 0.9096f, - 1.0000f, 0.9428f, 0.9793f, 0.8740f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8512f, 1.0000f, 1.0000f, 1.0000f, 0.7555f, 0.8763f, 0.9684f, - 1.0000f, 0.9283f, 0.8938f, 0.9762f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6601f, 0.9003f, 1.0000f, - 1.0000f, 0.8525f, 0.9747f, 0.9520f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8263f, 1.0000f, 1.0000f, - 1.0000f, 0.4133f, 0.8706f, 0.9276f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7470f, 0.9812f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9985f, 0.9913f, 0.9546f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7323f, 0.7245f, 0.9104f, 1.0000f, 0.8806f, 0.9552f, 0.9664f, - 1.0000f, 0.9997f, 0.9901f, 0.9637f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7786f, 0.8331f, 0.9765f, 1.0000f, 0.9892f, 0.9261f, 0.9557f, - 1.0000f, 0.9705f, 0.7944f, 0.8948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7693f, 1.0000f, 1.0000f, 1.0000f, 0.7059f, 0.9452f, 1.0000f, - 1.0000f, 0.9458f, 0.9498f, 0.9351f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8671f, 0.9672f, 1.0000f, - 1.0000f, 0.5282f, 0.9304f, 0.9541f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7297f, 1.0000f, 1.0000f, - 1.0000f, 0.7635f, 0.9883f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7492f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8041f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9161f, 0.8929f, 0.8103f, 0.6431f, 1.0000f, 1.0000f, 1.0000f, 0.2832f, 0.5037f, 0.6064f, 0.9728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8575f, 0.8828f, 0.4501f, 0.3732f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5743f, 0.8344f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5829f, 0.6794f, 0.9440f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.6698f, 0.8587f, 0.9581f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9994f, - 1.0000f, 0.6944f, 0.9755f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9989f, 0.9961f, - 1.0000f, 0.8784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9996f, 0.9365f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9806f, 0.8718f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9724f, 0.9470f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9964f, 0.9709f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9973f, 0.8773f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9802f, 0.9915f, 0.9857f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9324f, 0.9824f, 0.8384f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9481f, 0.7423f, 0.8235f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9185f, 0.9422f, 0.8358f, - 1.0000f, 1.0000f, 0.9935f, 0.9989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9596f, 0.9290f, 0.7615f, 1.0000f, 0.9918f, 0.9740f, 0.9830f, - 1.0000f, 0.9977f, 0.9995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9403f, 0.7595f, 1.0000f, 1.0000f, 0.9701f, 0.9633f, 0.6534f, - 1.0000f, 0.9951f, 0.9688f, 0.9591f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9169f, 0.9562f, 1.0000f, 1.0000f, 0.9536f, 0.6634f, 0.7398f, - 1.0000f, 0.9888f, 0.9531f, 0.7113f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7002f, 1.0000f, 1.0000f, 1.0000f, 0.8537f, 0.2204f, 1.0000f, - 1.0000f, 0.9773f, 0.8729f, 0.4325f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8919f, 1.0000f, 1.0000f, 1.0000f, 0.8611f, 0.8410f, 1.0000f, - 1.0000f, 0.9129f, 0.6141f, 0.9975f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3593f, 1.0000f, 1.0000f, - 1.0000f, 0.9410f, 0.3866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9071f, 1.0000f, 1.0000f, - 1.0000f, 0.7927f, 0.5909f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9088f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9941f, 0.9971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9732f, 0.9145f, 0.9950f, 1.0000f, 0.9969f, 0.9345f, 0.7839f, - 1.0000f, 0.9841f, 0.9974f, 0.9796f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9509f, 0.8010f, 1.0000f, 1.0000f, 0.9493f, 0.9515f, 0.6973f, - 1.0000f, 0.9920f, 0.9378f, 0.8026f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9487f, 1.0000f, 1.0000f, 1.0000f, 0.9646f, 0.4884f, 1.0000f, - 1.0000f, 0.9779f, 0.9153f, 0.8957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7447f, 1.0000f, 1.0000f, 1.0000f, 0.8249f, 1.0000f, 1.0000f, - 1.0000f, 0.9751f, 0.7140f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8859f, 1.0000f, 1.0000f, - 1.0000f, 0.9080f, 0.9371f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5330f, 1.0000f, 1.0000f, - 1.0000f, 0.9464f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6325f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9943f, 0.9833f, 0.9137f, 0.7166f, 1.0000f, 1.0000f, 1.0000f, 0.6178f, 0.9615f, 0.9317f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9949f, 0.9567f, 0.7821f, 0.0544f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9601f, 0.9867f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9947f, - 1.0000f, 0.9815f, 0.8966f, 0.9981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9668f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9956f, 0.9994f, - 1.0000f, 0.9786f, 0.8599f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9869f, - 1.0000f, 0.9743f, 0.9982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9874f, - 1.0000f, 0.8462f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9954f, 0.9965f, - 1.0000f, 0.9476f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9879f, 0.9852f, - 1.0000f, 0.9911f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9827f, 0.9980f, 0.9239f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9944f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9959f, 0.9960f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9917f, 0.9967f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9864f, 0.9934f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9037f, 0.9642f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9758f, 0.9676f, 0.3382f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9895f, 0.8177f, 0.2481f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8371f, 0.6396f, 0.4616f, - 1.0000f, 0.9992f, 0.9931f, 0.9268f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5378f, 1.0000f, 1.0000f, - 1.0000f, 0.9993f, 0.9876f, 0.9983f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8562f, 1.0000f, 1.0000f, - 1.0000f, 0.5518f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9121f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9619f, 0.9716f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9416f, 0.9995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8879f, 1.0000f, 1.0000f, 0.5472f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9995f, 0.9998f, - 1.0000f, 0.9586f, 1.0000f, 1.0000f, 0.1993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, 0.9809f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 0.9972f, 0.9940f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9872f, 0.9997f, 0.8795f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.8683f, 0.5565f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, 0.8304f, 0.7977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7192f, 0.4831f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5136f, 0.9909f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9907f, 0.9998f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9818f, 1.0000f, 0.9854f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9054f, 0.3309f, 0.8436f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7961f, 0.6568f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1765f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7086f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_6x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9358f, 0.9687f, 1.0000f, 1.0000f, 0.9758f, 0.9938f, - 1.0000f, 1.0000f, 1.0000f, 0.9976f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9388f, 0.9790f, 0.9579f, 1.0000f, 1.0000f, 0.9956f, 0.9959f, - 1.0000f, 1.0000f, 0.9823f, 0.9981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9393f, 0.8955f, 0.5579f, 1.0000f, 1.0000f, 0.9352f, 0.8171f, - 1.0000f, 1.0000f, 0.9961f, 0.9958f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8496f, 0.8435f, 0.4922f, 1.0000f, 0.9433f, 0.9471f, 0.8360f, - 1.0000f, 1.0000f, 0.9751f, 0.9655f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8472f, 0.5535f, 0.4179f, 1.0000f, 0.9323f, 0.8920f, 0.4065f, - 1.0000f, 0.9817f, 0.9834f, 0.9907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8254f, 0.1082f, 0.7436f, 1.0000f, 0.8460f, 0.7976f, 0.3593f, - 1.0000f, 1.0000f, 0.9410f, 0.9212f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8100f, 0.0675f, 1.0000f, 1.0000f, 0.9170f, 0.5706f, 0.2586f, - 1.0000f, 0.9232f, 0.8531f, 0.6146f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4290f, 0.5024f, 1.0000f, 1.0000f, 0.8588f, 0.3338f, 0.6441f, - 1.0000f, 1.0000f, 1.0000f, 0.9893f, 0.5788f, 1.0000f, 1.0000f, 1.0000f, 0.7577f, 1.0000f, 0.9112f, 0.8718f, 0.6181f, 1.0000f, 0.9829f, 0.9864f, - 1.0000f, 1.0000f, 0.9903f, 0.9939f, 0.7260f, 1.0000f, 1.0000f, 1.0000f, 0.7673f, 0.8972f, 0.9119f, 0.8837f, 0.3715f, 1.0000f, 0.9724f, 0.9663f, - 1.0000f, 1.0000f, 0.9802f, 0.9871f, 0.7457f, 1.0000f, 1.0000f, 1.0000f, 0.3891f, 0.8980f, 0.8185f, 0.5219f, 0.7857f, 1.0000f, 0.8769f, 0.8610f, - 1.0000f, 0.9787f, 0.9854f, 0.9127f, 0.6964f, 1.0000f, 1.0000f, 1.0000f, 0.1538f, 0.8410f, 0.4767f, 0.4973f, 1.0000f, 0.8893f, 0.9225f, 0.2978f, - 1.0000f, 1.0000f, 0.9427f, 0.9279f, 0.7066f, 1.0000f, 1.0000f, 1.0000f, 0.6938f, 0.8565f, 0.1859f, 0.6345f, 1.0000f, 0.9259f, 0.7749f, 0.2331f, - 1.0000f, 0.9539f, 0.8484f, 0.6628f, 0.0953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6598f, 0.2242f, 1.0000f, 1.0000f, 0.7616f, 0.3196f, 0.5264f, - 1.0000f, 0.9498f, 0.8903f, 0.6280f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6214f, 0.2151f, 1.0000f, 1.0000f, 0.8295f, 0.2420f, 0.4664f, - 1.0000f, 0.8738f, 0.7394f, 0.3531f, 0.6473f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1202f, 1.0000f, 1.0000f, 1.0000f, 0.7236f, 0.3051f, 1.0000f, - 1.0000f, 1.0000f, 0.8085f, 0.9252f, 0.6885f, 1.0000f, 1.0000f, 1.0000f, 0.3469f, 1.0000f, 0.9052f, 0.9205f, 0.7635f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9534f, 0.9518f, 0.7188f, 1.0000f, 1.0000f, 1.0000f, 0.3654f, 0.9029f, 0.8423f, 0.7164f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9455f, 0.8385f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9239f, 0.6312f, 0.2750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9272f, 0.9370f, 0.8599f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8321f, 0.3833f, 0.6377f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9060f, 0.9134f, 0.5936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8213f, 0.0524f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8654f, 0.8856f, 0.5074f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4716f, 0.5124f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, - 1.0000f, 0.8334f, 0.7212f, 0.3950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7477f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, - 1.0000f, 0.8938f, 0.5747f, 0.7351f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0193f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9929f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0371f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8114f, 0.8448f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6112f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8911f, 0.9067f, 0.5356f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8069f, 0.3404f, 0.4819f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7804f, 0.1429f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7116f, 0.1645f, 1.0000f, 1.0000f, 1.0000f, 0.9847f, 0.9932f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2668f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9937f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6042f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9744f, 0.9873f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1961f, 1.0000f, 1.0000f, 1.0000f, 0.9731f, 0.9774f, 0.8676f, - 1.0000f, 0.9978f, 0.9990f, 0.9994f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9422f, 0.8643f, 0.9588f, 1.0000f, 0.9808f, 0.9866f, 0.9646f, - 1.0000f, 0.9998f, 0.9993f, 0.9890f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8749f, 0.9329f, 0.9341f, 1.0000f, 0.9292f, 0.9784f, 0.7875f, - 1.0000f, 0.9842f, 0.9946f, 0.9857f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7415f, 0.9265f, 1.0000f, 1.0000f, 0.5900f, 0.8038f, 0.8865f, - 1.0000f, 0.9524f, 0.9899f, 0.8846f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8818f, 1.0000f, 1.0000f, 1.0000f, 0.7518f, 0.8554f, 0.9826f, - 1.0000f, 0.9097f, 0.8929f, 0.9716f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6990f, 0.9191f, 1.0000f, - 1.0000f, 0.8632f, 0.9690f, 0.9598f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8778f, 1.0000f, 1.0000f, - 1.0000f, 0.4612f, 0.9105f, 0.9616f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7786f, 0.9850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9989f, 0.9954f, 0.9574f, 0.4506f, 1.0000f, 1.0000f, 1.0000f, 0.9702f, 0.7557f, 0.8268f, 0.9748f, 1.0000f, 0.8398f, 0.9405f, 0.9593f, - 1.0000f, 0.9990f, 0.9919f, 0.9638f, 0.6077f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8241f, 0.9298f, 0.9915f, 1.0000f, 0.9839f, 0.8963f, 0.9364f, - 1.0000f, 0.9771f, 0.8199f, 0.9021f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8054f, 1.0000f, 1.0000f, 1.0000f, 0.5828f, 0.8542f, 1.0000f, - 1.0000f, 0.9529f, 0.9492f, 0.9564f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8023f, 0.9559f, 1.0000f, - 1.0000f, 0.5446f, 0.9198f, 0.9629f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6007f, 1.0000f, 1.0000f, - 1.0000f, 0.7943f, 0.9877f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8144f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9005f, 0.8875f, 0.7840f, 0.7016f, 1.0000f, 1.0000f, 1.0000f, 0.2503f, 0.5664f, 0.8129f, 0.9761f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8808f, 0.8519f, 0.3774f, 0.4870f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7091f, 0.9764f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6567f, 0.6717f, 0.9347f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 1.0000f, - 1.0000f, 0.6858f, 0.8281f, 0.9694f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 0.6775f, 0.9698f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9982f, 0.9934f, - 1.0000f, 0.8687f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9996f, 0.9044f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9768f, 0.8507f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9667f, 0.9438f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5864f, 0.8827f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7596f, 0.9741f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9970f, 0.9734f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9975f, 0.8697f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9705f, 0.9861f, 0.9793f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9037f, 0.9642f, 0.8227f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9482f, 0.7373f, 0.8373f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9317f, 0.9476f, 0.9184f, - 1.0000f, 1.0000f, 0.9982f, 0.9984f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9611f, 0.9607f, 0.6911f, 1.0000f, 0.9950f, 0.9713f, 0.9569f, - 1.0000f, 0.9994f, 0.9992f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9671f, 0.7538f, 1.0000f, 1.0000f, 0.9875f, 0.9659f, 0.5971f, - 1.0000f, 0.9935f, 0.9852f, 0.9845f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9162f, 0.8728f, 1.0000f, 1.0000f, 0.9926f, 0.6688f, 0.6658f, - 1.0000f, 0.9927f, 0.9796f, 0.7711f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5401f, 1.0000f, 1.0000f, 1.0000f, 0.9013f, 0.1752f, 1.0000f, - 1.0000f, 0.9924f, 0.8788f, 0.5491f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8621f, 1.0000f, 1.0000f, 1.0000f, 0.9246f, 0.7959f, 1.0000f, - 1.0000f, 0.9513f, 0.6746f, 0.9997f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4345f, 1.0000f, 1.0000f, - 1.0000f, 0.9820f, 0.4235f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9382f, 1.0000f, 1.0000f, - 1.0000f, 0.8347f, 0.6536f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9090f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9965f, 0.9991f, 0.9544f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9620f, 0.8708f, 0.9683f, 1.0000f, 0.9901f, 0.9651f, 0.7909f, - 1.0000f, 0.9971f, 0.9996f, 0.9837f, 0.9075f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9720f, 0.6504f, 1.0000f, 1.0000f, 0.9583f, 0.9285f, 0.5173f, - 1.0000f, 0.9964f, 0.9487f, 0.8576f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9444f, 1.0000f, 1.0000f, 1.0000f, 0.9679f, 0.3124f, 1.0000f, - 1.0000f, 0.9911f, 0.9304f, 0.9082f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5310f, 1.0000f, 1.0000f, 1.0000f, 0.7730f, 1.0000f, 1.0000f, - 1.0000f, 0.9882f, 0.7283f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8798f, 1.0000f, 1.0000f, - 1.0000f, 0.9310f, 0.9148f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2827f, 1.0000f, 1.0000f, - 1.0000f, 0.9781f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7041f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9941f, 0.9917f, 0.8989f, 0.7140f, 1.0000f, 1.0000f, 1.0000f, 0.6831f, 0.9905f, 0.7992f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9968f, 0.9755f, 0.7692f, 0.0818f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9449f, 0.9465f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, - 1.0000f, 0.9895f, 0.8884f, 0.9962f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9460f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9984f, - 1.0000f, 0.9814f, 0.8007f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9508f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9955f, - 1.0000f, 0.9727f, 0.9988f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 0.9951f, - 1.0000f, 0.7926f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9923f, 0.9993f, - 1.0000f, 0.9376f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9921f, 0.9738f, - 1.0000f, 0.9884f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9811f, 0.9831f, 0.9399f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9634f, 0.8308f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9549f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9966f, 0.9868f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9709f, 0.9155f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 0.9177f, 0.3267f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.8946f, 0.2903f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9335f, 0.6803f, 0.4399f, - 1.0000f, 0.9995f, 0.9945f, 0.9554f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4122f, 1.0000f, 1.0000f, - 1.0000f, 0.9997f, 0.9880f, 0.9942f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7654f, 1.0000f, 1.0000f, - 1.0000f, 0.7498f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9602f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9805f, 0.9897f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 1.0000f, 1.0000f, - 1.0000f, 0.9141f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8158f, 1.0000f, 1.0000f, 0.6409f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, - 1.0000f, 0.9503f, 1.0000f, 1.0000f, 0.2058f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 1.0000f, 0.9777f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9913f, 0.9995f, 0.9909f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9625f, 0.9996f, 0.8665f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.7892f, 0.4559f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9675f, 0.7767f, 0.7306f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7329f, 0.4008f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4453f, 0.9799f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9886f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 1.0000f, 0.9888f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9416f, 0.5622f, 0.9219f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8997f, 0.7822f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1318f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6247f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5963f, 0.7486f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9848f, 1.0000f, 0.6398f, 0.7224f, 0.8643f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9457f, 0.9860f, 1.0000f, 0.7062f, 0.6776f, 0.3793f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9709f, 0.9926f, 1.0000f, 0.6455f, 0.6093f, 0.2651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8467f, 0.9265f, 1.0000f, 0.1066f, 0.2089f, 0.1762f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9062f, 0.9314f, 0.9622f, 1.0000f, 0.3263f, 0.0000f, 0.4242f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9578f, 0.9940f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9175f, 0.9677f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9161f, 0.9372f, 0.8867f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8299f, 0.8797f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8323f, 0.7413f, 0.5665f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8917f, 0.9715f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9495f, 0.9852f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8491f, 0.8088f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8622f, 0.8900f, 0.8197f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8683f, 0.7659f, 0.3625f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7522f, 0.6725f, 0.2412f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9703f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9016f, 0.9426f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8779f, 0.9447f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7592f, 0.7852f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9885f, - 1.0000f, 0.7820f, 0.8558f, 0.3060f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9909f, - 1.0000f, 0.8396f, 0.4941f, 0.0653f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9436f, 0.9738f, - 1.0000f, 0.5503f, 0.2856f, 0.1425f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9513f, 0.9798f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9968f, 0.9992f, 0.9977f, 1.0000f, 0.7945f, 0.6278f, 0.7185f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9990f, 0.9998f, 0.9636f, 1.0000f, 0.6672f, 0.7339f, 0.7692f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9794f, 0.9918f, 0.9871f, 1.0000f, 0.4107f, 0.8884f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9505f, 0.9749f, 0.8741f, 1.0000f, 0.6512f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8951f, 0.7975f, 0.8833f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8116f, 0.8815f, 0.9119f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9978f, 0.9907f, 0.9228f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9995f, 0.9844f, 0.9696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9600f, 0.7883f, 0.8850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9383f, 0.9563f, 0.8444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4499f, 0.6826f, 0.9032f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5238f, 0.9290f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9808f, 0.9875f, 0.9690f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9585f, 0.9726f, 0.8372f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5745f, 0.7757f, 0.9615f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 1.0000f, - 1.0000f, 0.7264f, 0.8984f, 0.9349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, - 1.0000f, 0.3446f, 0.8580f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9826f, 0.9901f, 0.9949f, - 1.0000f, 0.6566f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9912f, 0.9953f, 0.9147f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8143f, 0.8663f, 0.9105f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 1.0000f, - 1.0000f, 0.9486f, 0.8033f, 0.8420f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9987f, - 1.0000f, 0.4726f, 0.8536f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9994f, 0.9923f, - 1.0000f, 0.6338f, 0.8934f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9991f, 0.9467f, - 1.0000f, 0.5047f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9835f, 0.9856f, 0.9629f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9607f, 0.9743f, 0.7914f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9789f, 0.9895f, 1.0000f, 0.9253f, 0.8601f, 0.8004f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9976f, 0.9888f, 0.9955f, 1.0000f, 0.9522f, 0.7557f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9904f, 0.9530f, 0.9188f, 1.0000f, 0.9547f, 0.9938f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9759f, 0.9302f, 0.4614f, 1.0000f, 0.7104f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9764f, 0.9091f, 0.6875f, 1.0000f, 0.9000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9593f, 0.7301f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9754f, 0.9812f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9784f, 0.9817f, 0.9405f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9769f, 0.8170f, 0.4834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8967f, 0.6972f, 0.5585f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9822f, 0.6924f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9077f, 0.9215f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9830f, 0.9643f, 0.9278f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9664f, 0.9415f, 0.6156f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, - 1.0000f, 0.9539f, 0.6030f, 0.8249f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9983f, - 1.0000f, 0.8703f, 0.3956f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9993f, - 1.0000f, 0.9555f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9732f, 0.9898f, - 1.0000f, 0.7018f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9951f, 0.9966f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9779f, 0.9337f, 0.7145f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9959f, 0.9974f, - 1.0000f, 0.9671f, 0.8760f, 0.6619f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9965f, - 1.0000f, 0.9477f, 0.5146f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9982f, - 1.0000f, 0.8061f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.9920f, 0.9915f, - 1.0000f, 0.9570f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9891f, 0.9657f, - 1.0000f, 0.9360f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9947f, 0.9774f, 0.8274f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9867f, 0.8223f, 0.5420f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9803f, 0.7789f, 0.7626f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4376f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7376f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7725f, 0.6217f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5894f, 0.9936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5821f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9996f, 0.9994f, - 1.0000f, 0.9047f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9997f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9997f, 0.9721f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, 0.9394f, 0.8348f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9863f, 0.9326f, 0.9241f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9882f, 0.9990f, 0.9961f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9878f, 0.9999f, 0.9202f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9133f, 0.5329f, 0.9650f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8514f, 0.7450f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8722f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9839f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6120f, 0.7696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9884f, 1.0000f, 0.6510f, 0.7169f, 0.8592f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9559f, 0.9917f, 1.0000f, 0.7477f, 0.7506f, 0.4645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9756f, 0.9915f, 1.0000f, 0.6919f, 0.7135f, 0.4257f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8997f, 0.9452f, 1.0000f, 0.6803f, 0.3829f, 0.3319f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9256f, 0.9644f, 0.9682f, 1.0000f, 0.6067f, 0.1057f, 0.6598f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.7850f, 0.9046f, 1.0000f, 0.0406f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8524f, 0.7722f, 0.6420f, 1.0000f, 0.1579f, 0.2461f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9765f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9751f, 0.9971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9301f, 0.9847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9265f, 0.9518f, 0.9058f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8641f, 0.8673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8324f, 0.7774f, 0.5302f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8959f, 0.7589f, 0.5644f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7669f, 0.6465f, 0.2254f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9092f, 0.9809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9638f, 0.9866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8880f, 0.8455f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8985f, 0.9168f, 0.8735f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8972f, 0.8142f, 0.3585f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7748f, 0.6956f, 0.2776f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, - 1.0000f, 0.8225f, 0.3945f, 0.0782f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9511f, - 1.0000f, 0.7299f, 0.2913f, 0.4359f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9609f, 0.9655f, - 1.0000f, 1.0000f, 0.9158f, 0.9525f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9034f, 0.9437f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8285f, 0.8472f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, - 1.0000f, 0.8305f, 0.9081f, 0.3712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, - 1.0000f, 0.8720f, 0.5899f, 0.1814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9545f, 0.9931f, - 1.0000f, 0.6172f, 0.3049f, 0.3185f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9869f, 0.9956f, - 1.0000f, 0.7030f, 0.2045f, 0.2621f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9336f, 0.9660f, - 1.0000f, 0.4456f, 0.1321f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9362f, 0.9237f, 0.9137f, - 1.0000f, 0.9978f, 0.9994f, 0.9972f, 1.0000f, 0.7825f, 0.5958f, 0.7100f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9994f, 0.9999f, 0.9596f, 1.0000f, 0.6555f, 0.7235f, 0.7391f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9813f, 0.9900f, 0.9893f, 1.0000f, 0.5442f, 0.9489f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9474f, 0.9789f, 0.8657f, 1.0000f, 0.7800f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8689f, 0.8608f, 0.9371f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7989f, 0.9345f, 0.8894f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5509f, 0.7616f, 0.9413f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6224f, 0.9539f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9996f, 0.9910f, 0.9126f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9993f, 0.9777f, 0.9677f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9742f, 0.8184f, 0.9022f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9496f, 0.9666f, 0.8766f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4904f, 0.8344f, 0.8907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6722f, 0.9649f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4988f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7966f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9828f, 0.9836f, 0.9737f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9571f, 0.9708f, 0.8363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6326f, 0.8400f, 0.9797f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 1.0000f, - 1.0000f, 0.8011f, 0.9199f, 0.9627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9997f, - 1.0000f, 0.5227f, 0.8704f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9998f, 0.9998f, - 1.0000f, 0.7449f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, 0.9879f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9603f, 0.9773f, 0.9246f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9793f, 0.8558f, - 1.0000f, 0.8034f, 0.8795f, 0.9148f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, - 1.0000f, 0.9444f, 0.8099f, 0.8245f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, - 1.0000f, 0.5775f, 0.9010f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9992f, 0.9890f, - 1.0000f, 0.7330f, 0.9353f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9989f, 0.9208f, - 1.0000f, 0.5577f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9843f, 0.9953f, 0.9850f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9552f, 0.9863f, 0.8838f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9698f, 0.8056f, 0.8121f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9503f, 0.9189f, 0.8205f, - 1.0000f, 1.0000f, 0.9873f, 0.9857f, 1.0000f, 0.9178f, 0.8507f, 0.8382f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9970f, 0.9922f, 0.9983f, 1.0000f, 0.9621f, 0.7420f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9903f, 0.9578f, 0.9388f, 1.0000f, 0.9615f, 0.9935f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9703f, 0.9327f, 0.4553f, 1.0000f, 0.7920f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9718f, 0.8265f, 0.4155f, 1.0000f, 0.9584f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8853f, 0.5373f, 0.9948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9747f, 0.7943f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9227f, 0.9274f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9840f, 0.9854f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9713f, 0.9944f, 0.9565f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9801f, 0.8419f, 0.5070f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8867f, 0.7361f, 0.5839f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9429f, 0.4051f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7897f, 0.6993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9805f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8489f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9785f, 0.9824f, 0.9532f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9832f, 0.9590f, 0.6880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, - 1.0000f, 0.9723f, 0.7066f, 0.8625f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9993f, - 1.0000f, 0.8947f, 0.4734f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9980f, - 1.0000f, 0.9115f, 0.9459f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9926f, 0.9967f, - 1.0000f, 0.4820f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9969f, 0.9990f, - 1.0000f, 0.9466f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9781f, 0.9898f, - 1.0000f, 0.9961f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9733f, 0.9905f, 0.9821f, - 1.0000f, 0.9769f, 0.9379f, 0.7643f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9982f, - 1.0000f, 0.9671f, 0.8810f, 0.7267f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9924f, - 1.0000f, 0.9632f, 0.6014f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, 0.9977f, - 1.0000f, 0.8751f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9920f, 0.9913f, - 1.0000f, 0.9104f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9895f, 0.9481f, - 1.0000f, 0.7561f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9876f, 0.9693f, 0.6275f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 0.9688f, 0.8934f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9817f, 0.9283f, 0.9318f, - 1.0000f, 0.9942f, 0.8437f, 0.5710f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9882f, 0.8163f, 0.7874f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3452f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7203f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8078f, 0.6763f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6681f, 0.9964f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6640f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9986f, 0.9984f, - 1.0000f, 0.9292f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 0.9999f, 0.9908f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9991f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 1.0000f, 0.9760f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9069f, 0.7534f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.8824f, 0.9421f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9396f, 0.8921f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8575f, 0.9929f, 1.0000f, - 1.0000f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9728f, 0.9990f, 0.9986f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9860f, 1.0000f, 0.8781f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9218f, 0.5149f, 0.9405f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8541f, 0.6842f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6374f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9309f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6348f, 0.7860f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8285f, 0.9042f, - 1.0000f, 1.0000f, 1.0000f, 0.9940f, 1.0000f, 0.6847f, 0.7223f, 0.8700f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7983f, 0.9615f, - 1.0000f, 1.0000f, 0.9487f, 0.9920f, 1.0000f, 0.7690f, 0.7645f, 0.4879f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8948f, 0.8713f, - 1.0000f, 1.0000f, 0.9808f, 0.9956f, 1.0000f, 0.6912f, 0.7029f, 0.4162f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9254f, 0.9272f, 0.9100f, - 1.0000f, 1.0000f, 0.9263f, 0.9493f, 1.0000f, 0.7141f, 0.4679f, 0.4243f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9081f, 0.8499f, 0.5771f, - 1.0000f, 0.9382f, 0.9724f, 0.9625f, 1.0000f, 0.6704f, 0.2426f, 0.8042f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7798f, 0.8117f, 0.3822f, - 1.0000f, 1.0000f, 0.8969f, 0.9011f, 1.0000f, 0.6075f, 0.0672f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8203f, 0.5325f, 0.1476f, - 1.0000f, 0.8857f, 0.7943f, 0.5976f, 1.0000f, 0.3069f, 0.5075f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7599f, 0.3735f, 0.4746f, - 1.0000f, 1.0000f, 1.0000f, 0.9801f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9845f, 0.9983f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9469f, 0.9779f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9397f, 0.9546f, 0.8787f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8774f, 0.8991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8555f, 0.8022f, 0.5612f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9128f, 0.7623f, 0.5264f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7776f, 0.6470f, 0.2700f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9338f, 0.9885f, 0.7429f, 1.0000f, 1.0000f, 1.0000f, 0.1977f, 1.0000f, 1.0000f, 1.0000f, 0.5823f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9716f, 0.9912f, 0.4944f, 1.0000f, 1.0000f, 1.0000f, 0.1297f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8980f, 0.8410f, 0.5386f, 1.0000f, 1.0000f, 1.0000f, 0.0899f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9109f, 0.9322f, 0.8569f, 0.2287f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9219f, 0.8348f, 0.3994f, 0.0352f, 1.0000f, 1.0000f, 1.0000f, 0.4610f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8003f, 0.7327f, 0.3909f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9937f, - 1.0000f, 0.8528f, 0.4078f, 0.1113f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9905f, - 1.0000f, 0.7249f, 0.2834f, 0.4398f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9907f, 0.9974f, - 1.0000f, 1.0000f, 0.9361f, 0.9707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9353f, 0.9529f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8169f, 0.8394f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, - 1.0000f, 0.8470f, 0.8925f, 0.3268f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, - 1.0000f, 0.8903f, 0.6510f, 0.2565f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9698f, 0.9948f, - 1.0000f, 0.6741f, 0.3553f, 0.4470f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9914f, 0.9957f, - 1.0000f, 0.7196f, 0.1815f, 0.2957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9463f, 0.9872f, - 1.0000f, 0.5503f, 0.1651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9594f, 0.9600f, 0.9183f, - 1.0000f, 0.9981f, 0.9993f, 0.9973f, 1.0000f, 0.7922f, 0.6124f, 0.6880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9605f, 0.9689f, 0.9137f, - 1.0000f, 0.9995f, 0.9998f, 0.9579f, 1.0000f, 0.6777f, 0.7000f, 0.7301f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9511f, 0.9430f, 0.6550f, - 1.0000f, 0.9736f, 0.9870f, 0.9827f, 1.0000f, 0.5926f, 0.9517f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7169f, 0.6971f, 0.9410f, - 1.0000f, 0.9306f, 0.9752f, 0.8316f, 1.0000f, 0.7819f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7881f, 0.8799f, 0.9720f, - 1.0000f, 0.8762f, 0.8725f, 0.9568f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5139f, 0.9443f, 1.0000f, - 1.0000f, 0.8134f, 0.9505f, 0.9022f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7454f, 1.0000f, 1.0000f, - 1.0000f, 0.5445f, 0.8687f, 0.9246f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7085f, 0.9775f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9997f, 0.9926f, 0.9071f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9994f, 0.9836f, 0.9636f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9666f, 0.7840f, 0.8750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9437f, 0.9589f, 0.8846f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5010f, 0.8914f, 0.9237f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7353f, 0.9848f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6666f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9853f, 0.9856f, 0.9685f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9646f, 0.9794f, 0.8363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6430f, 0.8455f, 0.9740f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, - 1.0000f, 0.7963f, 0.9201f, 0.9620f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9996f, - 1.0000f, 0.6307f, 0.9061f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9999f, 0.9996f, - 1.0000f, 0.8379f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, 0.9883f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9928f, 0.9703f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9951f, 0.9656f, - 1.0000f, 0.8219f, 0.8738f, 0.9289f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 1.0000f, - 1.0000f, 0.9631f, 0.8080f, 0.8582f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.5874f, 0.9174f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9992f, 0.9768f, - 1.0000f, 0.7552f, 0.9297f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9980f, 0.8881f, - 1.0000f, 0.6589f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, 0.9962f, 0.9895f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9535f, 0.9862f, 0.9032f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9744f, 0.8268f, 0.8937f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9562f, 0.9694f, 0.9210f, - 1.0000f, 1.0000f, 0.9818f, 0.9787f, 1.0000f, 0.9403f, 0.8649f, 0.8332f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9814f, 0.9821f, 0.9764f, - 1.0000f, 0.9979f, 0.9867f, 0.9976f, 1.0000f, 0.9573f, 0.7755f, 0.9998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9888f, 0.9811f, 0.9090f, - 1.0000f, 0.9953f, 0.9417f, 0.9001f, 1.0000f, 0.9680f, 0.9935f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9842f, 0.9390f, 0.9898f, - 1.0000f, 0.9783f, 0.9165f, 0.3171f, 1.0000f, 0.8236f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9748f, 0.8440f, 1.0000f, - 1.0000f, 0.9641f, 0.8061f, 0.3646f, 1.0000f, 0.9584f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9499f, 0.9878f, 1.0000f, - 1.0000f, 0.8596f, 0.5202f, 0.9982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8675f, 1.0000f, 1.0000f, - 1.0000f, 0.9424f, 0.5558f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9551f, 1.0000f, 1.0000f, - 1.0000f, 0.8252f, 0.8098f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9916f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9890f, 0.9947f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9902f, 0.9924f, 0.9712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9790f, 0.8151f, 0.4541f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9147f, 0.7504f, 0.6026f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9346f, 0.3459f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7528f, 0.6171f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9368f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5718f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9760f, 0.9859f, 0.9651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9772f, 0.9756f, 0.7479f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, - 1.0000f, 0.9728f, 0.7114f, 0.9052f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9986f, - 1.0000f, 0.9156f, 0.4322f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9959f, - 1.0000f, 0.9280f, 0.9330f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9909f, 0.9963f, - 1.0000f, 0.4814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9984f, 1.0000f, - 1.0000f, 0.9192f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9864f, 0.9954f, - 1.0000f, 0.9481f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9900f, 0.9971f, 0.9833f, - 1.0000f, 0.9805f, 0.9456f, 0.8301f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9992f, - 1.0000f, 0.9732f, 0.9119f, 0.7902f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9851f, - 1.0000f, 0.9661f, 0.6627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9893f, 0.9978f, - 1.0000f, 0.8869f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.9918f, 0.9880f, - 1.0000f, 0.8959f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9540f, 0.9675f, - 1.0000f, 0.6941f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9830f, 0.9557f, 0.6218f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9314f, 0.7057f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9523f, 0.8636f, 0.8425f, - 1.0000f, 0.9930f, 0.8822f, 0.6389f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8662f, 1.0000f, 1.0000f, - 1.0000f, 0.9922f, 0.8810f, 0.8485f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9450f, 1.0000f, 1.0000f, - 1.0000f, 0.2138f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6813f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8622f, 0.7379f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7275f, 0.9988f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7734f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9990f, 0.9986f, - 1.0000f, 0.9670f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 1.0000f, 0.9875f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 0.9991f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9999f, 0.9475f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9228f, 0.7576f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, 0.8892f, 0.9610f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8609f, 0.8186f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7668f, 0.9985f, 1.0000f, - 1.0000f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9798f, 0.9999f, 0.9964f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9839f, 1.0000f, 0.9375f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8542f, 0.3364f, 0.8834f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7404f, 0.6265f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5665f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8514f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6539f, 0.8057f, 1.0000f, 1.0000f, 0.8190f, 0.8664f, 1.0000f, 1.0000f, 0.9665f, 0.9926f, - 1.0000f, 1.0000f, 1.0000f, 0.9969f, 1.0000f, 0.7563f, 0.7444f, 0.8563f, 1.0000f, 0.8161f, 0.8706f, 0.8925f, 1.0000f, 1.0000f, 0.9863f, 0.9956f, - 1.0000f, 1.0000f, 0.9786f, 0.9960f, 1.0000f, 0.8529f, 0.8117f, 0.5924f, 1.0000f, 0.9100f, 0.8609f, 0.6746f, 1.0000f, 1.0000f, 0.9382f, 0.8726f, - 1.0000f, 1.0000f, 0.9941f, 0.9942f, 1.0000f, 0.7543f, 0.7979f, 0.4968f, 1.0000f, 0.8368f, 0.8493f, 0.5366f, 1.0000f, 0.9476f, 0.9487f, 0.9213f, - 1.0000f, 1.0000f, 0.9326f, 0.9588f, 1.0000f, 0.7205f, 0.6146f, 0.5412f, 1.0000f, 0.7849f, 0.6074f, 0.3232f, 1.0000f, 0.9498f, 0.8642f, 0.5125f, - 1.0000f, 0.9562f, 0.9712f, 0.9698f, 1.0000f, 0.7113f, 0.3712f, 0.9158f, 1.0000f, 0.7947f, 0.2448f, 0.9190f, 1.0000f, 0.8456f, 0.8102f, 0.4805f, - 1.0000f, 1.0000f, 0.8795f, 0.8987f, 1.0000f, 0.6447f, 0.1531f, 1.0000f, 1.0000f, 0.7065f, 0.0985f, 1.0000f, 1.0000f, 0.8736f, 0.4914f, 0.1851f, - 1.0000f, 0.8907f, 0.8010f, 0.5802f, 1.0000f, 0.5176f, 0.6629f, 1.0000f, 1.0000f, 0.4582f, 0.6110f, 1.0000f, 1.0000f, 0.7602f, 0.3314f, 0.5590f, - 1.0000f, 1.0000f, 1.0000f, 0.9792f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9762f, 0.9823f, - 1.0000f, 1.0000f, 0.9932f, 0.9990f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9638f, 0.9661f, - 1.0000f, 1.0000f, 0.9723f, 0.9817f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8969f, 0.9198f, - 1.0000f, 0.9642f, 0.9743f, 0.9091f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9040f, 0.9332f, 0.5633f, - 1.0000f, 1.0000f, 0.9022f, 0.9221f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9313f, 0.7040f, 0.2718f, - 1.0000f, 0.8716f, 0.8219f, 0.5843f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6991f, 0.4215f, 0.5021f, - 1.0000f, 0.9376f, 0.7749f, 0.5457f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8072f, 0.2258f, 0.3938f, - 1.0000f, 0.7882f, 0.6802f, 0.3150f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6250f, 0.2059f, 1.0000f, - 1.0000f, 1.0000f, 0.9626f, 0.9968f, 0.7963f, 1.0000f, 1.0000f, 1.0000f, 0.1272f, 1.0000f, 1.0000f, 1.0000f, 0.4638f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9916f, 0.9966f, 0.5502f, 1.0000f, 1.0000f, 1.0000f, 0.1745f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9345f, 0.8852f, 0.6478f, 1.0000f, 1.0000f, 1.0000f, 0.1132f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9523f, 0.9613f, 0.8943f, 0.2981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9459f, 0.8916f, 0.4694f, 0.6417f, 1.0000f, 1.0000f, 1.0000f, 0.6216f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8176f, 0.8132f, 0.4860f, 0.0667f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, - 1.0000f, 0.8843f, 0.5074f, 0.2159f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9914f, - 1.0000f, 0.7714f, 0.3556f, 0.5719f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9919f, 0.9989f, - 1.0000f, 1.0000f, 0.9657f, 0.9848f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9705f, 0.9548f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8653f, 0.8815f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9874f, - 1.0000f, 0.9057f, 0.9228f, 0.4750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, - 1.0000f, 0.9258f, 0.7361f, 0.3476f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9806f, 0.9961f, - 1.0000f, 0.7228f, 0.4080f, 0.6037f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9939f, - 1.0000f, 0.7766f, 0.2539f, 0.4468f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9694f, 0.9909f, - 1.0000f, 0.6569f, 0.3635f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9709f, 0.9843f, 0.8996f, - 1.0000f, 0.9990f, 0.9988f, 0.9974f, 1.0000f, 0.7732f, 0.5884f, 0.7136f, 1.0000f, 0.8766f, 0.7273f, 0.7382f, 1.0000f, 0.9927f, 0.9959f, 0.9846f, - 1.0000f, 0.9998f, 0.9994f, 0.9592f, 1.0000f, 0.6600f, 0.6913f, 0.7403f, 1.0000f, 0.7931f, 0.7339f, 0.7464f, 1.0000f, 0.9809f, 0.9904f, 0.8674f, - 1.0000f, 0.9634f, 0.9833f, 0.9780f, 1.0000f, 0.6317f, 0.9605f, 1.0000f, 1.0000f, 0.6000f, 0.9691f, 1.0000f, 1.0000f, 0.7296f, 0.8695f, 0.9889f, - 1.0000f, 0.9265f, 0.9684f, 0.8394f, 1.0000f, 0.8342f, 1.0000f, 1.0000f, 1.0000f, 0.7914f, 1.0000f, 1.0000f, 1.0000f, 0.8552f, 0.9492f, 0.9841f, - 1.0000f, 0.8468f, 0.8233f, 0.9465f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6858f, 0.9236f, 1.0000f, - 1.0000f, 0.7816f, 0.9436f, 0.8598f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8685f, 1.0000f, 1.0000f, - 1.0000f, 0.4279f, 0.8898f, 0.9430f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7484f, 0.9759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9998f, 0.9944f, 0.9279f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9133f, 0.9370f, 0.9401f, - 1.0000f, 0.9998f, 0.9853f, 0.9719f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9828f, 0.8978f, 0.8870f, - 1.0000f, 0.9654f, 0.8275f, 0.9049f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6659f, 0.9125f, 1.0000f, - 1.0000f, 0.9543f, 0.9702f, 0.9320f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8247f, 0.9680f, 1.0000f, - 1.0000f, 0.4342f, 0.9083f, 0.9448f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7898f, 1.0000f, 1.0000f, - 1.0000f, 0.7523f, 0.9851f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7159f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7621f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9887f, 0.9883f, 0.9716f, 0.6774f, 1.0000f, 1.0000f, 1.0000f, 0.0827f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9740f, 0.9771f, 0.8586f, 0.2894f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7016f, 0.8776f, 0.9858f, 0.6509f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.8419f, 0.9442f, 0.9768f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.7089f, 0.9272f, 1.0000f, 0.3396f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9999f, 0.9986f, - 1.0000f, 0.9031f, 1.0000f, 1.0000f, 0.2806f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9996f, 0.9676f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9955f, 0.9538f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9900f, 0.9726f, - 1.0000f, 0.8328f, 0.8756f, 0.9117f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.9557f, 0.8147f, 0.8540f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.6351f, 0.9388f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9971f, 0.9579f, - 1.0000f, 0.8355f, 0.9618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9951f, 0.8381f, - 1.0000f, 0.7696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9630f, 0.9870f, 0.9867f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9306f, 0.9646f, 0.8288f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9736f, 0.8205f, 0.9108f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9553f, 0.9650f, 0.9293f, - 1.0000f, 1.0000f, 0.9876f, 0.9872f, 1.0000f, 0.9596f, 0.8879f, 0.8431f, 1.0000f, 0.9601f, 0.9687f, 0.8805f, 1.0000f, 0.9907f, 0.9746f, 0.9831f, - 1.0000f, 0.9992f, 0.9938f, 0.9985f, 1.0000f, 0.9339f, 0.8026f, 0.9997f, 1.0000f, 0.9753f, 0.9166f, 1.0000f, 1.0000f, 0.9893f, 0.9936f, 0.7783f, - 1.0000f, 0.9947f, 0.9407f, 0.9358f, 1.0000f, 0.9798f, 0.9983f, 1.0000f, 1.0000f, 0.9783f, 0.9897f, 1.0000f, 1.0000f, 0.9825f, 0.8261f, 0.9503f, - 1.0000f, 0.9812f, 0.9413f, 0.4405f, 1.0000f, 0.8824f, 1.0000f, 1.0000f, 1.0000f, 0.9528f, 1.0000f, 1.0000f, 1.0000f, 0.9351f, 0.5546f, 1.0000f, - 1.0000f, 0.9820f, 0.7995f, 0.1638f, 1.0000f, 0.9508f, 1.0000f, 1.0000f, 1.0000f, 0.9777f, 1.0000f, 1.0000f, 1.0000f, 0.8934f, 0.8785f, 1.0000f, - 1.0000f, 0.8620f, 0.4525f, 0.9899f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5320f, 1.0000f, 1.0000f, - 1.0000f, 0.9570f, 0.3789f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8575f, 1.0000f, 1.0000f, - 1.0000f, 0.7658f, 0.5225f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8406f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9930f, 0.9978f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9895f, 0.9789f, 0.8960f, - 1.0000f, 0.9913f, 0.9935f, 0.9948f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9774f, 0.9583f, 0.8087f, - 1.0000f, 0.9865f, 0.9066f, 0.5677f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9733f, 0.7677f, 1.0000f, - 1.0000f, 0.9300f, 0.8444f, 0.7640f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9174f, 1.0000f, 1.0000f, - 1.0000f, 0.9673f, 0.4010f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9250f, 1.0000f, 1.0000f, - 1.0000f, 0.7799f, 0.6284f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6718f, 1.0000f, 1.0000f, - 1.0000f, 0.9150f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3864f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9803f, 0.9856f, 0.9765f, 0.6966f, 1.0000f, 1.0000f, 1.0000f, 0.5963f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9838f, 0.9801f, 0.8833f, 0.0503f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9929f, - 1.0000f, 0.9669f, 0.8042f, 0.9814f, 0.3067f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9972f, - 1.0000f, 0.9518f, 0.6181f, 1.0000f, 0.2356f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9973f, - 1.0000f, 0.9206f, 0.9182f, 1.0000f, 0.0271f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9976f, - 1.0000f, 0.5761f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9979f, 0.9991f, - 1.0000f, 0.8952f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.9970f, - 1.0000f, 0.8315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9836f, 0.9924f, 0.9891f, - 1.0000f, 0.9749f, 0.9566f, 0.8746f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9984f, - 1.0000f, 0.9918f, 0.9364f, 0.8631f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9945f, - 1.0000f, 0.9795f, 0.7318f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9881f, 0.9902f, - 1.0000f, 0.9454f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9921f, 0.9609f, 0.9860f, - 1.0000f, 0.9424f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9471f, 0.9286f, - 1.0000f, 0.6940f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9756f, 0.9394f, 0.4148f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9243f, 0.6384f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9418f, 0.7866f, 0.7251f, - 1.0000f, 0.9982f, 0.9513f, 0.7424f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7182f, 1.0000f, 1.0000f, - 1.0000f, 0.9962f, 0.9482f, 0.9729f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9575f, 1.0000f, 1.0000f, - 1.0000f, 0.1408f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6689f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9533f, 0.9141f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8861f, 0.9996f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9005f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9999f, 0.9967f, - 1.0000f, 0.9949f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9993f, 0.9975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9922f, 0.9999f, 0.9981f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9911f, 1.0000f, 0.8889f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9014f, 0.6886f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9885f, 0.8505f, 0.9074f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8481f, 0.7832f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7582f, 0.9994f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9997f, 0.9985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9879f, 1.0000f, 0.9622f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8302f, 0.2629f, 0.8517f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7504f, 0.5273f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1955f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6830f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8029f, 0.8183f, 1.0000f, 1.0000f, 0.9742f, 0.9767f, 1.0000f, 1.0000f, 0.9905f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 0.9980f, 1.0000f, 0.7921f, 0.8421f, 0.8718f, 1.0000f, 0.9775f, 0.9700f, 0.9587f, 1.0000f, 1.0000f, 0.9598f, 0.9960f, - 1.0000f, 1.0000f, 0.9851f, 0.9976f, 1.0000f, 0.9128f, 0.9270f, 0.5965f, 1.0000f, 0.9576f, 0.9361f, 0.6235f, 1.0000f, 1.0000f, 0.9553f, 0.8776f, - 1.0000f, 1.0000f, 0.9891f, 0.9934f, 1.0000f, 0.8587f, 0.8324f, 0.4406f, 1.0000f, 0.9156f, 0.8737f, 0.5514f, 1.0000f, 0.9684f, 0.9671f, 0.8883f, - 1.0000f, 1.0000f, 0.9750f, 0.9595f, 1.0000f, 0.8490f, 0.7375f, 0.5859f, 1.0000f, 0.9053f, 0.6035f, 0.5065f, 1.0000f, 0.9668f, 0.9014f, 0.4294f, - 1.0000f, 0.9764f, 0.9914f, 0.9819f, 1.0000f, 0.8866f, 0.4180f, 0.8958f, 1.0000f, 0.8875f, 0.1874f, 0.8248f, 1.0000f, 0.8998f, 0.8468f, 0.4064f, - 1.0000f, 1.0000f, 0.9322f, 0.8785f, 1.0000f, 0.7309f, 0.2514f, 1.0000f, 1.0000f, 0.8512f, 0.0364f, 1.0000f, 1.0000f, 0.9233f, 0.5297f, 0.1973f, - 1.0000f, 0.9221f, 0.8143f, 0.4872f, 1.0000f, 0.6655f, 0.7173f, 1.0000f, 1.0000f, 0.5017f, 0.4773f, 1.0000f, 1.0000f, 0.8618f, 0.2848f, 0.4723f, - 1.0000f, 1.0000f, 1.0000f, 0.9927f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9644f, 0.9099f, 1.0000f, 1.0000f, 0.9884f, 0.9874f, - 1.0000f, 1.0000f, 0.9948f, 0.9953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9661f, 0.9616f, 0.9121f, 1.0000f, 1.0000f, 0.9634f, 0.9756f, - 1.0000f, 1.0000f, 0.9733f, 0.9946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9459f, 0.8804f, 0.5593f, 1.0000f, 1.0000f, 0.9436f, 0.9328f, - 1.0000f, 0.9718f, 0.9917f, 0.9038f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8925f, 0.5749f, 0.4969f, 1.0000f, 0.9606f, 0.9630f, 0.6103f, - 1.0000f, 1.0000f, 0.9440f, 0.9468f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8892f, 0.2768f, 0.7770f, 1.0000f, 0.9674f, 0.8209f, 0.4515f, - 1.0000f, 0.9691f, 0.8813f, 0.6847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7242f, 0.2250f, 1.0000f, 1.0000f, 0.8156f, 0.4620f, 0.6569f, - 1.0000f, 0.9569f, 0.8545f, 0.5473f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7265f, 0.1574f, 1.0000f, 1.0000f, 0.8747f, 0.3429f, 0.5251f, - 1.0000f, 0.8917f, 0.7102f, 0.3149f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2067f, 1.0000f, 1.0000f, 1.0000f, 0.7788f, 0.4351f, 1.0000f, - 1.0000f, 1.0000f, 0.9703f, 0.9956f, 0.8934f, 1.0000f, 1.0000f, 1.0000f, 0.1472f, 1.0000f, 0.7219f, 0.7999f, 0.5341f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9893f, 0.9975f, 0.6766f, 1.0000f, 1.0000f, 1.0000f, 0.2599f, 0.7053f, 0.7889f, 0.7353f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9609f, 0.9149f, 0.7621f, 1.0000f, 1.0000f, 1.0000f, 0.1368f, 0.8577f, 0.7460f, 0.3756f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9658f, 0.9627f, 0.9022f, 0.4004f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8727f, 0.6202f, 0.6451f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9561f, 0.9246f, 0.5895f, 0.7541f, 1.0000f, 1.0000f, 1.0000f, 0.6683f, 0.7640f, 0.1675f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9240f, 0.9077f, 0.6001f, 0.1155f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6298f, 0.7003f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, - 1.0000f, 0.9195f, 0.5786f, 0.2998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7287f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, - 1.0000f, 0.8974f, 0.3820f, 0.6329f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0921f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9999f, - 1.0000f, 1.0000f, 0.9591f, 0.9780f, 0.0790f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9772f, 0.9454f, 0.5930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9294f, 0.9046f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, - 1.0000f, 0.9499f, 0.9426f, 0.5112f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, - 1.0000f, 0.9486f, 0.8566f, 0.4568f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9868f, 0.9988f, - 1.0000f, 0.8639f, 0.5205f, 0.6793f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9911f, - 1.0000f, 0.8849f, 0.3361f, 0.5633f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9849f, 0.9931f, - 1.0000f, 0.7905f, 0.4237f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9816f, 0.9876f, 0.8698f, - 1.0000f, 0.9989f, 0.9989f, 0.9988f, 1.0000f, 0.7937f, 0.5554f, 0.6900f, 1.0000f, 0.9371f, 0.8900f, 0.9311f, 1.0000f, 0.9919f, 0.9940f, 0.9786f, - 1.0000f, 0.9999f, 0.9999f, 0.9753f, 1.0000f, 0.6421f, 0.6978f, 0.7439f, 1.0000f, 0.9030f, 0.9381f, 0.9584f, 1.0000f, 0.9715f, 0.9928f, 0.8669f, - 1.0000f, 0.9565f, 0.9811f, 0.9730f, 1.0000f, 0.6169f, 0.9525f, 1.0000f, 1.0000f, 0.7678f, 0.9350f, 1.0000f, 1.0000f, 0.6820f, 0.8629f, 0.9620f, - 1.0000f, 0.9107f, 0.9602f, 0.8299f, 1.0000f, 0.8044f, 1.0000f, 1.0000f, 1.0000f, 0.9189f, 1.0000f, 1.0000f, 1.0000f, 0.8287f, 0.9176f, 0.9853f, - 1.0000f, 0.8966f, 0.8608f, 0.9759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7126f, 0.9069f, 1.0000f, - 1.0000f, 0.8337f, 0.9482f, 0.9412f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8261f, 1.0000f, 1.0000f, - 1.0000f, 0.3221f, 0.7839f, 0.9339f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7150f, 0.9169f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9995f, 0.9920f, 0.9421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7562f, 0.7331f, 0.9114f, 1.0000f, 0.9345f, 0.9529f, 0.9770f, - 1.0000f, 0.9997f, 0.9897f, 0.9678f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8058f, 0.8708f, 0.9904f, 1.0000f, 0.9938f, 0.9387f, 0.9778f, - 1.0000f, 0.9681f, 0.8433f, 0.8991f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8349f, 1.0000f, 1.0000f, 1.0000f, 0.7753f, 0.9736f, 1.0000f, - 1.0000f, 0.9613f, 0.9623f, 0.9508f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9317f, 0.9888f, 1.0000f, - 1.0000f, 0.4921f, 0.9355f, 0.9739f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8222f, 1.0000f, 1.0000f, - 1.0000f, 0.8235f, 0.9872f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7805f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7873f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9857f, 0.9886f, 0.9654f, 0.7953f, 1.0000f, 1.0000f, 1.0000f, 0.1775f, 0.5710f, 0.6069f, 0.9796f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9804f, 0.9862f, 0.8766f, 0.4122f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5823f, 0.8115f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7581f, 0.8840f, 0.9909f, 0.8087f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, - 1.0000f, 0.8409f, 0.9431f, 0.9835f, 0.0191f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.8014f, 0.9521f, 1.0000f, 0.5430f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9996f, 0.9978f, - 1.0000f, 0.9366f, 1.0000f, 1.0000f, 0.3074f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9402f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9900f, 0.9084f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9806f, 0.9464f, - 1.0000f, 0.8397f, 0.8942f, 0.9202f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, - 1.0000f, 0.9557f, 0.8101f, 0.8822f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6598f, 0.9392f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9943f, 0.9572f, - 1.0000f, 0.8386f, 0.9648f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9955f, 0.8170f, - 1.0000f, 0.8689f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9706f, 0.9846f, 0.9860f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9061f, 0.9855f, 0.8445f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9397f, 0.7659f, 0.8534f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9258f, 0.9512f, 0.9162f, - 1.0000f, 1.0000f, 0.9991f, 0.9992f, 1.0000f, 0.9922f, 0.8479f, 0.7734f, 1.0000f, 0.9783f, 0.9724f, 0.8501f, 1.0000f, 0.9970f, 0.9930f, 0.9761f, - 1.0000f, 0.9967f, 0.9997f, 0.9997f, 1.0000f, 0.9491f, 0.7418f, 1.0000f, 1.0000f, 0.9828f, 0.8456f, 1.0000f, 1.0000f, 0.9844f, 0.9944f, 0.7716f, - 1.0000f, 0.9987f, 0.9954f, 0.9300f, 1.0000f, 0.9840f, 0.9978f, 1.0000f, 1.0000f, 0.9799f, 0.9687f, 1.0000f, 1.0000f, 0.9814f, 0.7697f, 0.8831f, - 1.0000f, 0.9925f, 0.9694f, 0.4461f, 1.0000f, 0.8073f, 1.0000f, 1.0000f, 1.0000f, 0.7856f, 1.0000f, 1.0000f, 1.0000f, 0.9709f, 0.3497f, 1.0000f, - 1.0000f, 0.9958f, 0.8598f, 0.2684f, 1.0000f, 0.9252f, 1.0000f, 1.0000f, 1.0000f, 0.8982f, 1.0000f, 1.0000f, 1.0000f, 0.9288f, 0.6711f, 1.0000f, - 1.0000f, 0.9416f, 0.5159f, 0.9998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3627f, 1.0000f, 1.0000f, - 1.0000f, 0.9882f, 0.2341f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8555f, 1.0000f, 1.0000f, - 1.0000f, 0.8129f, 0.3944f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6267f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9935f, 0.9981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9912f, 0.9473f, 0.9974f, 1.0000f, 0.9977f, 0.9580f, 0.8659f, - 1.0000f, 0.9981f, 0.9985f, 0.9947f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, 0.8374f, 1.0000f, 1.0000f, 0.9890f, 0.9333f, 0.7396f, - 1.0000f, 0.9979f, 0.9477f, 0.6391f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9712f, 1.0000f, 1.0000f, 1.0000f, 0.9916f, 0.6136f, 1.0000f, - 1.0000f, 0.9533f, 0.8312f, 0.7521f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7984f, 1.0000f, 1.0000f, 1.0000f, 0.9305f, 1.0000f, 1.0000f, - 1.0000f, 0.9826f, 0.4823f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9537f, 1.0000f, 1.0000f, - 1.0000f, 0.9006f, 0.6627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4672f, 1.0000f, 1.0000f, - 1.0000f, 0.9516f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2923f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9745f, 0.9788f, 0.9727f, 0.8196f, 1.0000f, 1.0000f, 1.0000f, 0.3882f, 0.9936f, 0.8950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9949f, 0.9821f, 0.9276f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9641f, 0.9907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.9801f, 0.8361f, 0.9549f, 0.7028f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9747f, 1.0000f, - 1.0000f, 0.9878f, 0.6360f, 1.0000f, 0.2159f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9880f, - 1.0000f, 0.9637f, 0.9227f, 1.0000f, 0.0654f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9983f, - 1.0000f, 0.6952f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9991f, - 1.0000f, 0.9208f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9971f, - 1.0000f, 0.7969f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9952f, 0.9495f, - 1.0000f, 0.9959f, 0.9664f, 0.8523f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9992f, - 1.0000f, 0.9831f, 0.9182f, 0.8274f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9957f, - 1.0000f, 0.9870f, 0.7501f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9996f, - 1.0000f, 0.9092f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9902f, 0.9837f, - 1.0000f, 0.9504f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 0.9895f, 0.9376f, - 1.0000f, 0.7078f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9809f, 0.3562f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.8909f, 0.3292f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9264f, 0.7196f, 0.5386f, - 1.0000f, 0.9997f, 0.9794f, 0.8649f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6926f, 1.0000f, 1.0000f, - 1.0000f, 0.9996f, 0.9450f, 0.9721f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9214f, 1.0000f, 1.0000f, - 1.0000f, 0.2427f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6540f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9445f, 0.9545f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 1.0000f, 1.0000f, - 1.0000f, 0.9282f, 0.9993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8857f, 1.0000f, 1.0000f, 0.7480f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9994f, 0.9986f, - 1.0000f, 0.9791f, 1.0000f, 1.0000f, 0.1039f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9842f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0517f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9833f, 0.9984f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9864f, 0.9998f, 0.8794f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.8679f, 0.6739f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9142f, 0.9407f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7822f, 0.6874f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6511f, 0.9899f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, 0.9994f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9697f, 1.0000f, 0.9541f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9135f, 0.3692f, 0.8756f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7601f, 0.5671f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1263f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6481f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_8x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8054f, 0.8341f, 1.0000f, 1.0000f, 0.9810f, 0.9675f, 1.0000f, 1.0000f, 0.9943f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 0.9986f, 1.0000f, 0.8624f, 0.8820f, 0.8375f, 1.0000f, 0.9572f, 0.9701f, 0.9819f, 1.0000f, 1.0000f, 0.9868f, 0.9979f, - 1.0000f, 1.0000f, 0.9915f, 0.9972f, 1.0000f, 0.9009f, 0.9330f, 0.6438f, 1.0000f, 0.9640f, 0.9394f, 0.6551f, 1.0000f, 1.0000f, 0.9521f, 0.8586f, - 1.0000f, 1.0000f, 0.9962f, 0.9975f, 1.0000f, 0.9383f, 0.8132f, 0.4939f, 1.0000f, 0.9373f, 0.9017f, 0.5792f, 1.0000f, 0.9525f, 0.9604f, 0.8652f, - 1.0000f, 1.0000f, 0.9899f, 0.9672f, 1.0000f, 0.9053f, 0.7622f, 0.5825f, 1.0000f, 0.9208f, 0.6175f, 0.5434f, 1.0000f, 0.9583f, 0.9232f, 0.3895f, - 1.0000f, 0.9765f, 0.9813f, 0.9742f, 1.0000f, 0.8845f, 0.4758f, 0.8948f, 1.0000f, 0.8595f, 0.1803f, 0.8282f, 1.0000f, 0.9038f, 0.8196f, 0.3837f, - 1.0000f, 1.0000f, 0.9436f, 0.9136f, 1.0000f, 0.7854f, 0.3658f, 1.0000f, 1.0000f, 0.8419f, 0.0601f, 1.0000f, 1.0000f, 0.9478f, 0.5757f, 0.2737f, - 1.0000f, 0.9466f, 0.8397f, 0.5278f, 1.0000f, 0.7148f, 0.7692f, 1.0000f, 1.0000f, 0.5071f, 0.4518f, 1.0000f, 1.0000f, 0.8901f, 0.3415f, 0.5509f, - 1.0000f, 1.0000f, 1.0000f, 0.9940f, 0.6145f, 1.0000f, 1.0000f, 1.0000f, 0.8495f, 1.0000f, 0.9607f, 0.9220f, 0.6381f, 1.0000f, 0.9945f, 0.9969f, - 1.0000f, 1.0000f, 0.9961f, 0.9995f, 0.8246f, 1.0000f, 1.0000f, 1.0000f, 0.8080f, 0.9440f, 0.9634f, 0.9238f, 0.4567f, 1.0000f, 0.9863f, 0.9898f, - 1.0000f, 1.0000f, 0.9779f, 0.9934f, 0.8184f, 1.0000f, 1.0000f, 1.0000f, 0.5358f, 0.9418f, 0.9123f, 0.6323f, 0.8093f, 1.0000f, 0.9357f, 0.8725f, - 1.0000f, 0.9882f, 0.9885f, 0.9279f, 0.8067f, 1.0000f, 1.0000f, 1.0000f, 0.2212f, 0.9002f, 0.5891f, 0.6235f, 1.0000f, 0.9449f, 0.9389f, 0.4420f, - 1.0000f, 1.0000f, 0.9453f, 0.9335f, 0.7823f, 1.0000f, 1.0000f, 1.0000f, 0.7775f, 0.8837f, 0.3086f, 0.7792f, 1.0000f, 0.9506f, 0.7884f, 0.2665f, - 1.0000f, 0.9760f, 0.8760f, 0.6893f, 0.1970f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7395f, 0.3598f, 1.0000f, 1.0000f, 0.8209f, 0.3287f, 0.5858f, - 1.0000f, 0.9723f, 0.8987f, 0.6113f, 0.1450f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7169f, 0.1631f, 1.0000f, 1.0000f, 0.8484f, 0.1888f, 0.3476f, - 1.0000f, 0.9176f, 0.7511f, 0.3951f, 0.6660f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1357f, 1.0000f, 1.0000f, 1.0000f, 0.7473f, 0.2879f, 1.0000f, - 1.0000f, 1.0000f, 0.9762f, 0.9920f, 0.9074f, 1.0000f, 1.0000f, 1.0000f, 0.2133f, 1.0000f, 0.9494f, 0.9445f, 0.6467f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9911f, 0.9973f, 0.7082f, 1.0000f, 1.0000f, 1.0000f, 0.3537f, 0.9547f, 0.8803f, 0.7657f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9794f, 0.9268f, 0.7942f, 1.0000f, 1.0000f, 1.0000f, 0.2592f, 0.9490f, 0.7293f, 0.4615f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9852f, 0.9715f, 0.9319f, 0.5156f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9422f, 0.5546f, 0.7314f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9558f, 0.9510f, 0.6410f, 0.7355f, 1.0000f, 1.0000f, 1.0000f, 0.7273f, 0.8662f, 0.1062f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9482f, 0.9088f, 0.6352f, 0.2368f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6051f, 0.6687f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.9565f, 0.6265f, 0.4370f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7675f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, - 1.0000f, 0.9130f, 0.5197f, 0.7567f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0320f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9985f, - 1.0000f, 1.0000f, 0.9835f, 0.9822f, 0.2443f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8795f, 0.8615f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9666f, 0.9486f, 0.4166f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9413f, 0.9189f, 0.5618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9150f, 0.9156f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8861f, 0.5027f, 0.5653f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, - 1.0000f, 0.9624f, 0.9431f, 0.5723f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8716f, 0.3154f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, - 1.0000f, 0.9621f, 0.8689f, 0.4850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7434f, 0.2053f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9953f, - 1.0000f, 0.8925f, 0.5238f, 0.7037f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4270f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9931f, - 1.0000f, 0.9169f, 0.4711f, 0.6633f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6942f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9865f, 0.9896f, - 1.0000f, 0.8536f, 0.5318f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1541f, 1.0000f, 1.0000f, 1.0000f, 0.9824f, 0.9928f, 0.8743f, - 1.0000f, 0.9989f, 0.9992f, 0.9987f, 1.0000f, 0.7839f, 0.5396f, 0.6792f, 1.0000f, 0.9698f, 0.9081f, 0.9726f, 1.0000f, 0.9887f, 0.9854f, 0.9656f, - 1.0000f, 0.9996f, 0.9999f, 0.9842f, 1.0000f, 0.6523f, 0.6740f, 0.7211f, 1.0000f, 0.9102f, 0.9398f, 0.9600f, 1.0000f, 0.9614f, 0.9859f, 0.8294f, - 1.0000f, 0.9744f, 0.9846f, 0.9787f, 1.0000f, 0.6818f, 0.9663f, 1.0000f, 1.0000f, 0.7986f, 0.9517f, 1.0000f, 1.0000f, 0.6294f, 0.8430f, 0.9362f, - 1.0000f, 0.9067f, 0.9579f, 0.8318f, 1.0000f, 0.8040f, 1.0000f, 1.0000f, 1.0000f, 0.9408f, 1.0000f, 1.0000f, 1.0000f, 0.7870f, 0.8853f, 0.9880f, - 1.0000f, 0.8786f, 0.8566f, 0.9533f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7104f, 0.9163f, 1.0000f, - 1.0000f, 0.8119f, 0.9631f, 0.9352f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8634f, 1.0000f, 1.0000f, - 1.0000f, 0.3352f, 0.8027f, 0.9576f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7252f, 0.9341f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9983f, 0.9966f, 0.9474f, 0.5114f, 1.0000f, 1.0000f, 1.0000f, 0.9659f, 0.8013f, 0.8546f, 0.9908f, 1.0000f, 0.9024f, 0.9457f, 0.9647f, - 1.0000f, 0.9998f, 0.9923f, 0.9808f, 0.6019f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8576f, 0.9529f, 0.9963f, 1.0000f, 0.9891f, 0.9313f, 0.9637f, - 1.0000f, 0.9769f, 0.8680f, 0.9116f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8917f, 1.0000f, 1.0000f, 1.0000f, 0.6990f, 0.9182f, 1.0000f, - 1.0000f, 0.9678f, 0.9597f, 0.9728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8812f, 0.9653f, 1.0000f, - 1.0000f, 0.4895f, 0.9250f, 0.9739f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6843f, 1.0000f, 1.0000f, - 1.0000f, 0.8107f, 0.9844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8452f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8474f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9932f, 0.9925f, 0.9784f, 0.8222f, 1.0000f, 1.0000f, 1.0000f, 0.0960f, 0.5923f, 0.7999f, 0.9906f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9736f, 0.9789f, 0.8734f, 0.3778f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7415f, 0.9627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7759f, 0.9273f, 0.9974f, 0.7928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8933f, 0.9514f, 0.9876f, 0.0163f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.8329f, 0.9586f, 1.0000f, 0.5472f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9988f, 0.9967f, - 1.0000f, 0.9569f, 1.0000f, 1.0000f, 0.3018f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9997f, 0.9201f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9754f, 0.8909f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9772f, 0.9427f, - 1.0000f, 0.8671f, 0.8698f, 0.9346f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6579f, 0.8707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9551f, 0.8258f, 0.8505f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7726f, 0.9806f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6966f, 0.9669f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9968f, 0.9544f, - 1.0000f, 0.8778f, 0.9767f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.8234f, - 1.0000f, 0.8964f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9325f, 0.9752f, 0.9720f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8893f, 0.9540f, 0.8171f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9296f, 0.7743f, 0.8364f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9368f, 0.9470f, 0.9262f, - 1.0000f, 1.0000f, 0.9992f, 0.9995f, 1.0000f, 0.9889f, 0.8829f, 0.7604f, 1.0000f, 0.9562f, 0.9837f, 0.7971f, 1.0000f, 0.9850f, 0.9747f, 0.9826f, - 1.0000f, 1.0000f, 0.9999f, 0.9990f, 1.0000f, 0.9590f, 0.7585f, 1.0000f, 1.0000f, 0.9950f, 0.7899f, 0.9999f, 1.0000f, 0.9989f, 0.9927f, 0.7375f, - 1.0000f, 0.9976f, 0.9782f, 0.9749f, 1.0000f, 0.9774f, 0.9980f, 1.0000f, 1.0000f, 0.9681f, 0.9143f, 1.0000f, 1.0000f, 0.9965f, 0.7060f, 0.7640f, - 1.0000f, 0.9986f, 0.9883f, 0.5689f, 1.0000f, 0.8158f, 1.0000f, 1.0000f, 1.0000f, 0.6917f, 1.0000f, 1.0000f, 1.0000f, 0.9833f, 0.2291f, 1.0000f, - 1.0000f, 0.9984f, 0.8769f, 0.1717f, 1.0000f, 0.9378f, 1.0000f, 1.0000f, 1.0000f, 0.8941f, 1.0000f, 1.0000f, 1.0000f, 0.9195f, 0.7014f, 1.0000f, - 1.0000f, 0.9302f, 0.4805f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3718f, 1.0000f, 1.0000f, - 1.0000f, 0.9918f, 0.3221f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8643f, 1.0000f, 1.0000f, - 1.0000f, 0.8526f, 0.4469f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6205f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9970f, 0.9980f, 0.9801f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9831f, 0.8994f, 0.9731f, 1.0000f, 0.9949f, 0.9799f, 0.8352f, - 1.0000f, 0.9973f, 0.9985f, 0.9954f, 0.8885f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9856f, 0.7530f, 1.0000f, 1.0000f, 0.9839f, 0.9403f, 0.5955f, - 1.0000f, 0.9998f, 0.9291f, 0.7548f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9796f, 1.0000f, 1.0000f, 1.0000f, 0.9757f, 0.4218f, 1.0000f, - 1.0000f, 0.9684f, 0.9109f, 0.9095f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6495f, 1.0000f, 1.0000f, 1.0000f, 0.8515f, 1.0000f, 1.0000f, - 1.0000f, 0.9930f, 0.5582f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8972f, 1.0000f, 1.0000f, - 1.0000f, 0.8751f, 0.6868f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1262f, 1.0000f, 1.0000f, - 1.0000f, 0.9777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4060f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9995f, 0.9921f, 0.9718f, 0.8408f, 1.0000f, 1.0000f, 1.0000f, 0.4320f, 0.9894f, 0.8306f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9971f, 0.9815f, 0.9308f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9791f, 0.9593f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9861f, 0.8463f, 0.9939f, 0.7454f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9686f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9870f, 0.9941f, - 1.0000f, 0.9867f, 0.6766f, 1.0000f, 0.2518f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9644f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9912f, - 1.0000f, 0.9828f, 0.9712f, 1.0000f, 0.1163f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9707f, 0.9892f, - 1.0000f, 0.7190f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9978f, 0.9872f, - 1.0000f, 0.9214f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9958f, - 1.0000f, 0.7808f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.9917f, 0.9536f, - 1.0000f, 0.9956f, 0.9610f, 0.8956f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9689f, 0.8605f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9977f, - 1.0000f, 0.9944f, 0.9244f, 0.7913f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9462f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9878f, - 1.0000f, 0.9874f, 0.7957f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9998f, - 1.0000f, 0.9226f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9947f, 0.9734f, - 1.0000f, 0.9555f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9848f, 0.8869f, - 1.0000f, 0.7709f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9914f, 0.9617f, 0.2949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, 0.9285f, 0.2808f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9709f, 0.7126f, 0.4007f, - 1.0000f, 0.9995f, 0.9650f, 0.9031f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5987f, 1.0000f, 1.0000f, - 1.0000f, 0.9999f, 0.9803f, 0.9955f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8877f, 1.0000f, 1.0000f, - 1.0000f, 0.4113f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7492f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9704f, 0.9502f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9498f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9256f, 1.0000f, 1.0000f, 0.7334f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, - 1.0000f, 0.9935f, 1.0000f, 1.0000f, 0.0731f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9817f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0468f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9692f, 0.9959f, 0.9901f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9903f, 0.9997f, 0.9046f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.8441f, 0.6082f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.8270f, 0.8556f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8145f, 0.6606f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6713f, 0.9909f, 1.0000f, - 1.0000f, 0.9997f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9904f, 1.0000f, 0.9695f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8979f, 0.4663f, 0.9060f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8386f, 0.7231f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0849f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4984f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8283f, 0.9411f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9911f, 1.0000f, 0.8198f, 0.8943f, 0.9580f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9666f, 0.9914f, 1.0000f, 0.8649f, 0.7945f, 0.3479f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9856f, 0.9971f, 1.0000f, 0.7514f, 0.7303f, 0.3081f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8956f, 0.9461f, 1.0000f, 0.7426f, 0.3814f, 0.2020f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9385f, 0.9530f, 0.9764f, 1.0000f, 0.6902f, 0.0741f, 0.6157f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9734f, 1.0000f, 1.0000f, 0.7894f, 0.8086f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9724f, 0.9930f, 1.0000f, 0.8017f, 0.8803f, 0.8262f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9377f, 0.9777f, 1.0000f, 0.7655f, 0.7131f, 0.3217f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9368f, 0.9485f, 0.8846f, 1.0000f, 0.7204f, 0.2192f, 0.2356f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8514f, 0.9082f, 1.0000f, 0.7237f, 0.1048f, 0.4972f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8633f, 0.7710f, 0.6434f, 1.0000f, 0.3704f, 0.1327f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9224f, 0.9768f, 1.0000f, 1.0000f, 0.5507f, 0.7736f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9672f, 0.9919f, 1.0000f, 0.5135f, 0.6036f, 0.5645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8696f, 0.8364f, 1.0000f, 0.6584f, 0.4134f, 0.1824f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8861f, 0.9172f, 0.8459f, 1.0000f, 0.6485f, 0.3593f, 0.5054f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8995f, 0.8177f, 0.4801f, 1.0000f, 0.0383f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7790f, 0.7816f, 0.2803f, 1.0000f, 0.1601f, 0.2510f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9816f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9117f, 0.9634f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9093f, 0.9685f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8421f, 0.8665f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9935f, - 1.0000f, 0.8383f, 0.9058f, 0.4236f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9937f, - 1.0000f, 0.8832f, 0.6727f, 0.2658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9523f, 0.9828f, - 1.0000f, 0.6941f, 0.4888f, 0.3352f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9696f, 0.9845f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9963f, 0.9989f, 0.9966f, 1.0000f, 0.8727f, 0.8344f, 0.9331f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9995f, 0.9999f, 0.9627f, 1.0000f, 0.7919f, 0.9244f, 0.9284f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9842f, 0.9923f, 0.9895f, 1.0000f, 0.5780f, 0.8712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9678f, 0.9835f, 0.8817f, 1.0000f, 0.7683f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9293f, 0.8477f, 0.9139f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8601f, 0.9070f, 0.9350f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9983f, 0.9801f, 0.8773f, 1.0000f, 0.5911f, 0.7167f, 0.9500f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9982f, 0.9702f, 0.9394f, 1.0000f, 0.7056f, 0.8303f, 0.9477f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9641f, 0.7485f, 0.8681f, 1.0000f, 0.6632f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9214f, 0.9538f, 0.8440f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5435f, 0.7600f, 0.9234f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6215f, 0.9654f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9781f, 0.9849f, 0.9729f, 1.0000f, 0.4620f, 0.6327f, 0.9182f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9594f, 0.9749f, 0.8132f, 1.0000f, 0.5214f, 0.8916f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6381f, 0.7969f, 0.9552f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.7763f, 0.8742f, 0.9428f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 0.4032f, 0.9033f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9881f, 0.9926f, 0.9956f, - 1.0000f, 0.7018f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9939f, 0.9045f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8617f, 0.9312f, 0.9608f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 1.0000f, - 1.0000f, 0.9785f, 0.9020f, 0.8888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.6773f, 0.9341f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9990f, 0.9906f, - 1.0000f, 0.8040f, 0.9322f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9988f, 0.9264f, - 1.0000f, 0.7270f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9793f, 0.9820f, 0.9508f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9690f, 0.9718f, 0.7572f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9965f, 0.9916f, 1.0000f, 0.9614f, 0.8402f, 0.6818f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9981f, 0.9921f, 0.9984f, 1.0000f, 0.9161f, 0.6981f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9986f, 0.9621f, 0.9573f, 1.0000f, 0.9105f, 0.9420f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9869f, 0.9444f, 0.6097f, 1.0000f, 0.5289f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9813f, 0.9566f, 0.8109f, 1.0000f, 0.9587f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9773f, 0.8567f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9878f, 0.9838f, 1.0000f, 0.9493f, 0.7456f, 0.9903f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9859f, 0.9852f, 0.9008f, 1.0000f, 0.9274f, 0.6861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9809f, 0.8241f, 0.4433f, 1.0000f, 0.9303f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8929f, 0.7093f, 0.5576f, 1.0000f, 0.7397f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9759f, 0.7628f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9193f, 0.9128f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9872f, 0.9601f, 0.9150f, 1.0000f, 0.9453f, 0.7993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9739f, 0.9403f, 0.5712f, 1.0000f, 0.8902f, 0.9824f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9469f, 0.6535f, 0.7335f, 1.0000f, 0.9647f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9994f, - 1.0000f, 0.8532f, 0.2943f, 1.0000f, 1.0000f, 0.9955f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9990f, - 1.0000f, 0.9436f, 0.9901f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9713f, 0.9951f, - 1.0000f, 0.7868f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9962f, 0.9975f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9797f, 0.9254f, 0.7543f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9985f, - 1.0000f, 0.9545f, 0.8969f, 0.6271f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9941f, - 1.0000f, 0.9660f, 0.4714f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9898f, 0.9974f, - 1.0000f, 0.8063f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, 0.9887f, 0.9945f, - 1.0000f, 0.9707f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9893f, 0.9884f, 0.9754f, - 1.0000f, 0.8982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9890f, 0.8584f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9943f, 0.8323f, 0.5846f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9790f, 0.8495f, 0.8154f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5973f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8875f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6680f, 0.3923f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4527f, 0.9831f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4335f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9997f, 0.9996f, - 1.0000f, 0.7842f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9998f, 0.9947f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 1.0000f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 1.0000f, 0.9909f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9516f, 0.8758f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 0.9559f, 0.9359f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9862f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9805f, 0.9980f, 0.9949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9866f, 0.9995f, 0.8550f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9203f, 0.5363f, 0.9744f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8220f, 0.7366f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8788f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9875f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8462f, 0.9403f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9942f, 1.0000f, 0.8593f, 0.9155f, 0.9673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9628f, 0.9948f, 1.0000f, 0.8912f, 0.8324f, 0.4420f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9895f, 0.9959f, 1.0000f, 0.7946f, 0.7847f, 0.3829f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9332f, 0.9648f, 1.0000f, 0.7965f, 0.3746f, 0.2759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9547f, 0.9812f, 0.9758f, 1.0000f, 0.7256f, 0.0857f, 0.6373f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8371f, 0.9247f, 1.0000f, 0.6694f, 0.0608f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9051f, 0.8356f, 0.7286f, 1.0000f, 0.2660f, 0.5139f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9774f, 1.0000f, 1.0000f, 0.8133f, 0.8022f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9778f, 0.9926f, 1.0000f, 0.8240f, 0.8854f, 0.8387f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9450f, 0.9853f, 1.0000f, 0.8169f, 0.7423f, 0.3906f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9430f, 0.9541f, 0.8741f, 1.0000f, 0.7591f, 0.3143f, 0.4061f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8842f, 0.8753f, 1.0000f, 0.7614f, 0.1271f, 0.5615f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8418f, 0.7723f, 0.5785f, 1.0000f, 0.5261f, 0.1800f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8956f, 0.8096f, 0.6461f, 1.0000f, 0.4489f, 0.1966f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8151f, 0.7369f, 0.2098f, 1.0000f, 0.1456f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9346f, 0.9885f, 1.0000f, 1.0000f, 0.5322f, 0.7701f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9782f, 0.9950f, 1.0000f, 0.5014f, 0.5558f, 0.5947f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8967f, 0.8402f, 1.0000f, 0.6731f, 0.5728f, 0.2558f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9061f, 0.9198f, 0.8805f, 1.0000f, 0.7038f, 0.5078f, 0.6543f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9206f, 0.8307f, 0.3321f, 1.0000f, 0.6240f, 0.1081f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8115f, 0.7448f, 0.2857f, 1.0000f, 0.2954f, 0.5840f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, - 1.0000f, 0.8648f, 0.5500f, 0.1633f, 1.0000f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9530f, - 1.0000f, 0.7765f, 0.3662f, 0.5201f, 1.0000f, 0.0325f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9668f, 0.9602f, - 1.0000f, 1.0000f, 0.9173f, 0.9512f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9223f, 0.9575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8830f, 0.9010f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, - 1.0000f, 0.8564f, 0.9181f, 0.4820f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, - 1.0000f, 0.9138f, 0.6836f, 0.2226f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9737f, 0.9976f, - 1.0000f, 0.7134f, 0.3233f, 0.4624f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9882f, 0.9936f, - 1.0000f, 0.7679f, 0.3050f, 0.4281f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9286f, 0.9697f, - 1.0000f, 0.6149f, 0.2340f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9481f, 0.9375f, 0.9271f, - 1.0000f, 0.9980f, 0.9996f, 0.9967f, 1.0000f, 0.8900f, 0.8448f, 0.9396f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9997f, 0.9998f, 0.9353f, 1.0000f, 0.8003f, 0.9309f, 0.9263f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9847f, 0.9947f, 0.9934f, 1.0000f, 0.6938f, 0.9190f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9524f, 0.9832f, 0.8817f, 1.0000f, 0.8688f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9071f, 0.8978f, 0.9687f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8550f, 0.9653f, 0.9437f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6102f, 0.8274f, 0.9710f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7165f, 0.9732f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9972f, 0.9798f, 0.8578f, 1.0000f, 0.6329f, 0.7315f, 0.9741f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9992f, 0.9728f, 0.9214f, 1.0000f, 0.7342f, 0.8040f, 0.9607f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9597f, 0.7658f, 0.8701f, 1.0000f, 0.7545f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9443f, 0.9518f, 0.8675f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4210f, 0.8187f, 0.9020f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7070f, 0.9558f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6051f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8634f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9815f, 0.9908f, 0.9724f, 1.0000f, 0.3492f, 0.6194f, 0.9147f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9536f, 0.9766f, 0.7984f, 1.0000f, 0.4885f, 0.8989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6619f, 0.8291f, 0.9317f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.7867f, 0.8999f, 0.9719f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.5999f, 0.8792f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9999f, 0.9996f, - 1.0000f, 0.7786f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9994f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9754f, 0.9879f, 0.9361f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9838f, 0.8477f, - 1.0000f, 0.8661f, 0.9324f, 0.9638f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 1.0000f, - 1.0000f, 0.9828f, 0.9110f, 0.9041f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9995f, - 1.0000f, 0.7102f, 0.9658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9987f, 0.9906f, - 1.0000f, 0.8506f, 0.9678f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9989f, 0.9031f, - 1.0000f, 0.6657f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9868f, 0.9922f, 0.9871f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9581f, 0.9887f, 0.8521f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9801f, 0.7907f, 0.8059f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9475f, 0.9129f, 0.8223f, - 1.0000f, 1.0000f, 0.9919f, 0.9978f, 1.0000f, 0.9570f, 0.8621f, 0.7397f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9954f, 0.9986f, 0.9962f, 1.0000f, 0.9382f, 0.7497f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9961f, 0.9663f, 0.9618f, 1.0000f, 0.9231f, 0.9706f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9862f, 0.9456f, 0.5672f, 1.0000f, 0.6285f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9794f, 0.8779f, 0.5894f, 1.0000f, 0.8934f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8923f, 0.6971f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9715f, 0.8866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9643f, 0.9633f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9930f, 0.9873f, 1.0000f, 0.9494f, 0.7636f, 0.9971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9805f, 0.9850f, 0.9424f, 1.0000f, 0.9368f, 0.7568f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9856f, 0.8078f, 0.3407f, 1.0000f, 0.9417f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9091f, 0.7226f, 0.5382f, 1.0000f, 0.7521f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9279f, 0.4139f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7926f, 0.7005f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9469f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8727f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9901f, 0.9701f, 0.9294f, 1.0000f, 0.9389f, 0.7807f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9819f, 0.9302f, 0.6503f, 1.0000f, 0.8945f, 0.9915f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.9488f, 0.6870f, 0.7473f, 1.0000f, 0.9750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9994f, - 1.0000f, 0.8766f, 0.2451f, 1.0000f, 1.0000f, 0.9945f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9990f, - 1.0000f, 0.8714f, 0.9120f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, 0.9966f, - 1.0000f, 0.4351f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9997f, 0.9993f, - 1.0000f, 0.9623f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9844f, 0.9890f, - 1.0000f, 0.9876f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9745f, 0.9903f, 0.9822f, - 1.0000f, 0.9898f, 0.9506f, 0.7887f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9955f, - 1.0000f, 0.9586f, 0.9081f, 0.6801f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9893f, - 1.0000f, 0.9591f, 0.4950f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9958f, - 1.0000f, 0.8607f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9865f, 0.9911f, - 1.0000f, 0.9339f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9770f, 0.9564f, - 1.0000f, 0.6418f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9808f, 0.9762f, 0.6581f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9917f, 0.9683f, 0.9101f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9835f, 0.9612f, 0.9553f, - 1.0000f, 0.9963f, 0.8340f, 0.5442f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9825f, 0.8205f, 0.8257f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3577f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6904f, 0.3984f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4690f, 0.9790f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4557f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9991f, 0.9973f, - 1.0000f, 0.8433f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9999f, 0.9993f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 1.0000f, 0.9859f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9410f, 0.7827f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9239f, 0.9463f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9692f, 0.8878f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8889f, 0.9928f, 1.0000f, - 1.0000f, 0.9975f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9841f, 0.9974f, 0.9913f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9786f, 0.9984f, 0.8492f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9255f, 0.4755f, 0.9164f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8535f, 0.7196f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6766f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9500f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8941f, 0.9467f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8379f, 0.9490f, - 1.0000f, 1.0000f, 1.0000f, 0.9941f, 1.0000f, 0.8989f, 0.9438f, 0.9693f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8151f, 0.9709f, - 1.0000f, 1.0000f, 0.9775f, 0.9933f, 1.0000f, 0.9080f, 0.8642f, 0.4327f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9163f, 0.8850f, - 1.0000f, 1.0000f, 0.9867f, 0.9931f, 1.0000f, 0.8135f, 0.7809f, 0.4020f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9368f, 0.9362f, 0.9130f, - 1.0000f, 1.0000f, 0.9517f, 0.9765f, 1.0000f, 0.8240f, 0.4618f, 0.4733f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9355f, 0.8951f, 0.6674f, - 1.0000f, 0.9665f, 0.9847f, 0.9792f, 1.0000f, 0.7557f, 0.1083f, 0.7349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8352f, 0.8595f, 0.5053f, - 1.0000f, 1.0000f, 0.9245f, 0.9223f, 1.0000f, 0.7599f, 0.0550f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8419f, 0.5920f, 0.1947f, - 1.0000f, 0.9147f, 0.8338f, 0.6978f, 1.0000f, 0.3094f, 0.3954f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7977f, 0.4386f, 0.5523f, - 1.0000f, 1.0000f, 1.0000f, 0.9862f, 1.0000f, 1.0000f, 0.8406f, 0.8392f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9876f, 0.9902f, 1.0000f, 0.8509f, 0.9097f, 0.8458f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9568f, 0.9839f, 1.0000f, 0.8269f, 0.7827f, 0.4145f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9578f, 0.9614f, 0.8471f, 1.0000f, 0.7736f, 0.3680f, 0.4444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8764f, 0.8742f, 1.0000f, 0.7679f, 0.1488f, 0.6830f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8687f, 0.7698f, 0.5151f, 1.0000f, 0.5568f, 0.2373f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9266f, 0.7945f, 0.5747f, 1.0000f, 0.5339f, 0.2167f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7912f, 0.6891f, 0.2565f, 1.0000f, 0.0936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9528f, 0.9936f, 0.7844f, 1.0000f, 0.5293f, 0.7536f, 0.2275f, 1.0000f, 1.0000f, 1.0000f, 0.7063f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9824f, 0.9957f, 0.6204f, 0.5386f, 0.5432f, 0.5703f, 0.1226f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9017f, 0.8497f, 0.7324f, 0.6769f, 0.5792f, 0.3172f, 0.1722f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9062f, 0.9230f, 0.8432f, 0.3542f, 0.7035f, 0.5003f, 0.6920f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9315f, 0.8283f, 0.3817f, 0.7274f, 0.6044f, 0.1606f, 1.0000f, 0.5878f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8041f, 0.7660f, 0.3471f, 0.0336f, 0.4082f, 0.6706f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, - 1.0000f, 0.8891f, 0.4790f, 0.1362f, 0.5199f, 0.6243f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, - 1.0000f, 0.7755f, 0.2752f, 0.4902f, 0.1835f, 0.0761f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9939f, 0.9989f, - 1.0000f, 1.0000f, 0.9450f, 0.9724f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9381f, 0.9750f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8709f, 0.8970f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, - 1.0000f, 0.8365f, 0.9294f, 0.4267f, 0.6125f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, - 1.0000f, 0.9122f, 0.7199f, 0.3248f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9681f, 0.9942f, - 1.0000f, 0.7299f, 0.3886f, 0.6003f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9919f, 0.9975f, - 1.0000f, 0.8057f, 0.2472f, 0.4502f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9632f, 0.9869f, - 1.0000f, 0.6575f, 0.2842f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9661f, 0.9786f, 0.9252f, - 1.0000f, 0.9972f, 0.9986f, 0.9974f, 1.0000f, 0.9106f, 0.8653f, 0.9601f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9677f, 0.9697f, 0.9088f, - 1.0000f, 0.9996f, 0.9996f, 0.9419f, 1.0000f, 0.8181f, 0.9501f, 0.9685f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9689f, 0.9507f, 0.6541f, - 1.0000f, 0.9779f, 0.9954f, 0.9849f, 1.0000f, 0.7172f, 0.9201f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7468f, 0.7249f, 0.9208f, - 1.0000f, 0.9259f, 0.9815f, 0.8484f, 1.0000f, 0.8829f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7862f, 0.8775f, 0.9799f, - 1.0000f, 0.9114f, 0.9139f, 0.9743f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6506f, 0.9705f, 1.0000f, - 1.0000f, 0.8618f, 0.9739f, 0.9444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8166f, 1.0000f, 1.0000f, - 1.0000f, 0.5613f, 0.9193f, 0.9563f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7619f, 0.9890f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9980f, 0.9818f, 0.8607f, 1.0000f, 0.6471f, 0.7444f, 0.9645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9991f, 0.9736f, 0.9321f, 1.0000f, 0.7224f, 0.8254f, 0.9720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9479f, 0.7491f, 0.8311f, 1.0000f, 0.7928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9186f, 0.9280f, 0.8445f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4206f, 0.8676f, 0.9301f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7397f, 0.9809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6949f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8009f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9864f, 0.9881f, 0.9747f, 1.0000f, 0.3611f, 0.6164f, 0.9155f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9636f, 0.9758f, 0.8025f, 1.0000f, 0.4676f, 0.8839f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6359f, 0.8088f, 0.9335f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, - 1.0000f, 0.7718f, 0.8931f, 0.9673f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9999f, - 1.0000f, 0.6642f, 0.9071f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9998f, 0.9996f, - 1.0000f, 0.8120f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9998f, 0.9926f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9963f, 0.9754f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9955f, 0.9796f, - 1.0000f, 0.8797f, 0.9400f, 0.9619f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 1.0000f, - 1.0000f, 0.9812f, 0.9216f, 0.9170f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9997f, - 1.0000f, 0.7007f, 0.9653f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9976f, 0.9649f, - 1.0000f, 0.8583f, 0.9554f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9969f, 0.8731f, - 1.0000f, 0.7090f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 0.9923f, 0.9914f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9549f, 0.9896f, 0.8786f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9802f, 0.8522f, 0.8818f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9713f, 0.9582f, 0.9407f, - 1.0000f, 1.0000f, 0.9885f, 0.9894f, 1.0000f, 0.9761f, 0.9008f, 0.7792f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9806f, 0.9640f, 0.9878f, - 1.0000f, 0.9986f, 0.9953f, 0.9995f, 1.0000f, 0.9610f, 0.8104f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9833f, 0.9830f, 0.9237f, - 1.0000f, 0.9982f, 0.9308f, 0.9432f, 1.0000f, 0.9461f, 0.9728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9904f, 0.9539f, 0.9944f, - 1.0000f, 0.9605f, 0.9596f, 0.3748f, 1.0000f, 0.6609f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9821f, 0.8860f, 1.0000f, - 1.0000f, 0.9857f, 0.8807f, 0.4560f, 1.0000f, 0.9026f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9425f, 0.9929f, 1.0000f, - 1.0000f, 0.9035f, 0.6435f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9394f, 1.0000f, 1.0000f, - 1.0000f, 0.9836f, 0.6861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9732f, 1.0000f, 1.0000f, - 1.0000f, 0.8961f, 0.8559f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9883f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9912f, 0.9906f, 1.0000f, 0.9669f, 0.8225f, 0.9989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9717f, 0.9947f, 0.9388f, 1.0000f, 0.9473f, 0.8210f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9841f, 0.7961f, 0.2929f, 1.0000f, 0.9484f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8881f, 0.7118f, 0.4846f, 1.0000f, 0.7640f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9178f, 0.3324f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7578f, 0.6321f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9273f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6397f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9967f, 0.9827f, 0.9533f, 1.0000f, 0.9455f, 0.7878f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9844f, 0.9558f, 0.6800f, 1.0000f, 0.9044f, 0.9921f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, - 1.0000f, 0.9623f, 0.6738f, 0.7513f, 1.0000f, 0.9701f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9999f, - 1.0000f, 0.8921f, 0.2659f, 1.0000f, 1.0000f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9959f, 0.9978f, - 1.0000f, 0.8698f, 0.8325f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, 0.9979f, - 1.0000f, 0.3399f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9980f, 0.9999f, - 1.0000f, 0.8911f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9918f, 0.9988f, - 1.0000f, 0.9348f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9892f, 0.9958f, 0.9910f, - 1.0000f, 0.9852f, 0.9328f, 0.8534f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 0.9992f, - 1.0000f, 0.9544f, 0.9342f, 0.7421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.9871f, - 1.0000f, 0.9587f, 0.4953f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9900f, 0.9934f, - 1.0000f, 0.8571f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9854f, 0.9925f, 0.9874f, - 1.0000f, 0.9053f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9496f, 0.9768f, - 1.0000f, 0.5246f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9657f, 0.9573f, 0.5962f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9627f, 0.7895f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9772f, 0.8998f, 0.8980f, - 1.0000f, 0.9966f, 0.8547f, 0.6085f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8753f, 1.0000f, 1.0000f, - 1.0000f, 0.9916f, 0.7993f, 0.8630f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9523f, 1.0000f, 1.0000f, - 1.0000f, 0.2057f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7145f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7373f, 0.5103f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5835f, 0.9782f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5478f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9990f, - 1.0000f, 0.8720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, 0.9961f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9997f, 0.9984f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9999f, 0.9789f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9592f, 0.8072f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9413f, 0.9512f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9375f, 0.8871f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8195f, 0.9988f, 1.0000f, - 1.0000f, 0.9985f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9898f, 0.9993f, 0.9951f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9908f, 0.9993f, 0.8297f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8665f, 0.3015f, 0.8901f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7773f, 0.5658f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6282f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9287f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9515f, 0.9479f, 1.0000f, 1.0000f, 0.8697f, 0.8771f, 1.0000f, 1.0000f, 0.9852f, 0.9958f, - 1.0000f, 1.0000f, 1.0000f, 0.9911f, 1.0000f, 0.9611f, 0.9295f, 0.9716f, 1.0000f, 0.8951f, 0.9092f, 0.9403f, 1.0000f, 1.0000f, 0.9972f, 0.9990f, - 1.0000f, 1.0000f, 0.9844f, 0.9969f, 1.0000f, 0.9664f, 0.9466f, 0.6276f, 1.0000f, 0.9493f, 0.9133f, 0.7285f, 1.0000f, 1.0000f, 0.9671f, 0.9099f, - 1.0000f, 1.0000f, 0.9965f, 0.9962f, 1.0000f, 0.9506f, 0.8726f, 0.6061f, 1.0000f, 0.8687f, 0.8894f, 0.5709f, 1.0000f, 0.9763f, 0.9710f, 0.9254f, - 1.0000f, 1.0000f, 0.9651f, 0.9654f, 1.0000f, 0.9339f, 0.6408f, 0.6024f, 1.0000f, 0.8967f, 0.7477f, 0.4102f, 1.0000f, 0.9697f, 0.9447f, 0.6344f, - 1.0000f, 0.9838f, 0.9908f, 0.9749f, 1.0000f, 0.8789f, 0.2292f, 0.8488f, 1.0000f, 0.9085f, 0.4279f, 0.9377f, 1.0000f, 0.9323f, 0.8919f, 0.5870f, - 1.0000f, 1.0000f, 0.9489f, 0.9408f, 1.0000f, 0.8587f, 0.0655f, 1.0000f, 1.0000f, 0.8077f, 0.1170f, 1.0000f, 1.0000f, 0.9272f, 0.5831f, 0.2550f, - 1.0000f, 0.9497f, 0.8762f, 0.6703f, 1.0000f, 0.5236f, 0.5095f, 1.0000f, 1.0000f, 0.6501f, 0.6867f, 1.0000f, 1.0000f, 0.8943f, 0.3798f, 0.6470f, - 1.0000f, 1.0000f, 1.0000f, 0.9858f, 1.0000f, 1.0000f, 0.9397f, 0.8885f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9925f, 0.9871f, - 1.0000f, 1.0000f, 0.9879f, 0.9977f, 1.0000f, 0.9532f, 0.9427f, 0.8477f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, 0.9784f, - 1.0000f, 1.0000f, 0.9647f, 0.9866f, 1.0000f, 0.9192f, 0.8807f, 0.5458f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9248f, 0.9260f, - 1.0000f, 0.9792f, 0.9856f, 0.8618f, 1.0000f, 0.9113f, 0.5750f, 0.5909f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9502f, 0.9644f, 0.6136f, - 1.0000f, 1.0000f, 0.9355f, 0.8735f, 1.0000f, 0.8911f, 0.3399f, 0.7919f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9722f, 0.8576f, 0.4565f, - 1.0000f, 0.9328f, 0.8179f, 0.5142f, 1.0000f, 0.7394f, 0.3860f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8466f, 0.5281f, 0.6098f, - 1.0000f, 0.9626f, 0.8454f, 0.5500f, 1.0000f, 0.7352f, 0.3026f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9140f, 0.3179f, 0.4941f, - 1.0000f, 0.8975f, 0.7239f, 0.3255f, 1.0000f, 0.2379f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7670f, 0.2871f, 1.0000f, - 1.0000f, 1.0000f, 0.9746f, 0.9918f, 0.9035f, 1.0000f, 0.7019f, 0.8062f, 0.1283f, 1.0000f, 1.0000f, 1.0000f, 0.5326f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9875f, 0.9948f, 0.6787f, 0.6618f, 0.6439f, 0.5627f, 0.1819f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9688f, 0.8716f, 0.8311f, 0.8016f, 0.7216f, 0.3922f, 0.1716f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9731f, 0.9553f, 0.8834f, 0.4337f, 0.8780f, 0.5948f, 0.6994f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9528f, 0.9160f, 0.4620f, 0.8407f, 0.7069f, 0.3103f, 1.0000f, 0.4162f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9078f, 0.8207f, 0.4220f, 0.0000f, 0.6731f, 0.6892f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9936f, - 1.0000f, 0.8998f, 0.5542f, 0.2015f, 0.7779f, 0.6841f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, - 1.0000f, 0.8648f, 0.2948f, 0.5044f, 0.1610f, 0.1052f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9991f, - 1.0000f, 1.0000f, 0.9725f, 0.9787f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9771f, 0.9577f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9371f, 0.9361f, 0.0921f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, - 1.0000f, 0.9585f, 0.9737f, 0.6207f, 0.6171f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9898f, - 1.0000f, 0.9704f, 0.8544f, 0.4837f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9903f, 0.9991f, - 1.0000f, 0.8565f, 0.4784f, 0.7118f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 0.9949f, - 1.0000f, 0.9020f, 0.3470f, 0.5415f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9850f, 0.9932f, - 1.0000f, 0.7968f, 0.4509f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9896f, 0.9909f, 0.9071f, - 1.0000f, 0.9987f, 0.9993f, 0.9974f, 1.0000f, 0.9027f, 0.8668f, 0.9565f, 1.0000f, 0.8860f, 0.7436f, 0.7576f, 1.0000f, 0.9959f, 0.9947f, 0.9829f, - 1.0000f, 0.9997f, 0.9999f, 0.9545f, 1.0000f, 0.8431f, 0.9413f, 0.9811f, 1.0000f, 0.8107f, 0.7537f, 0.7633f, 1.0000f, 0.9864f, 0.9929f, 0.8744f, - 1.0000f, 0.9604f, 0.9867f, 0.9827f, 1.0000f, 0.8032f, 0.9470f, 1.0000f, 1.0000f, 0.6560f, 0.9678f, 1.0000f, 1.0000f, 0.7652f, 0.8877f, 0.9774f, - 1.0000f, 0.9064f, 0.9755f, 0.8359f, 1.0000f, 0.9127f, 1.0000f, 1.0000f, 1.0000f, 0.7832f, 1.0000f, 1.0000f, 1.0000f, 0.8607f, 0.9596f, 0.9880f, - 1.0000f, 0.8658f, 0.8816f, 0.9622f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7814f, 0.9510f, 1.0000f, - 1.0000f, 0.8273f, 0.9713f, 0.9461f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9289f, 1.0000f, 1.0000f, - 1.0000f, 0.5189f, 0.9179f, 0.9728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8193f, 0.9719f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9982f, 0.9834f, 0.8851f, 1.0000f, 0.6531f, 0.7498f, 0.9278f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9317f, 0.9456f, 0.9437f, - 1.0000f, 0.9985f, 0.9707f, 0.9283f, 1.0000f, 0.7518f, 0.8165f, 0.9825f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9877f, 0.9056f, 0.8959f, - 1.0000f, 0.9418f, 0.7743f, 0.8522f, 1.0000f, 0.8511f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7143f, 0.9049f, 1.0000f, - 1.0000f, 0.9366f, 0.9387f, 0.9266f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8335f, 0.9782f, 1.0000f, - 1.0000f, 0.3329f, 0.7984f, 0.9350f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8868f, 1.0000f, 1.0000f, - 1.0000f, 0.7191f, 0.9212f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7761f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7952f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9891f, 0.9807f, 0.9740f, 0.8047f, 0.3669f, 0.5585f, 0.9224f, 0.0517f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9619f, 0.9789f, 0.8323f, 0.4043f, 0.4452f, 0.8419f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7093f, 0.8707f, 0.9700f, 0.8500f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8260f, 0.9186f, 0.9823f, 0.0191f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.7167f, 0.9005f, 1.0000f, 0.6814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, 0.9975f, - 1.0000f, 0.8628f, 1.0000f, 1.0000f, 0.1918f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9997f, 0.9752f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0789f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9983f, 0.9640f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9818f, - 1.0000f, 0.9106f, 0.9475f, 0.9573f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 1.0000f, - 1.0000f, 0.9884f, 0.9334f, 0.9382f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.7689f, 0.9684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9964f, 0.9536f, - 1.0000f, 0.8935f, 0.9779f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9966f, 0.9943f, 0.8122f, - 1.0000f, 0.7797f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9588f, 0.9848f, 0.9694f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9218f, 0.9681f, 0.8247f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9557f, 0.8371f, 0.8983f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9561f, 0.9600f, 0.9312f, - 1.0000f, 1.0000f, 0.9963f, 0.9995f, 1.0000f, 0.9901f, 0.8753f, 0.8234f, 1.0000f, 0.9941f, 0.9766f, 0.8843f, 1.0000f, 0.9967f, 0.9893f, 0.9809f, - 1.0000f, 0.9995f, 0.9989f, 0.9980f, 1.0000f, 0.9630f, 0.8220f, 1.0000f, 1.0000f, 0.9913f, 0.9120f, 1.0000f, 1.0000f, 0.9920f, 0.9944f, 0.8298f, - 1.0000f, 0.9973f, 0.9983f, 0.9523f, 1.0000f, 0.9840f, 0.9804f, 1.0000f, 1.0000f, 0.9873f, 0.9888f, 1.0000f, 1.0000f, 0.9970f, 0.8555f, 0.9661f, - 1.0000f, 0.9935f, 0.9820f, 0.4675f, 1.0000f, 0.7308f, 1.0000f, 1.0000f, 1.0000f, 0.9344f, 1.0000f, 1.0000f, 1.0000f, 0.9836f, 0.6918f, 1.0000f, - 1.0000f, 0.9906f, 0.9013f, 0.2200f, 1.0000f, 0.9166f, 1.0000f, 1.0000f, 1.0000f, 0.9882f, 1.0000f, 1.0000f, 1.0000f, 0.9675f, 0.8902f, 1.0000f, - 1.0000f, 0.9423f, 0.5371f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6590f, 1.0000f, 1.0000f, - 1.0000f, 0.9842f, 0.4889f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9236f, 1.0000f, 1.0000f, - 1.0000f, 0.9042f, 0.5986f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7867f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9979f, 0.9956f, 1.0000f, 0.9895f, 0.8136f, 0.9760f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9869f, 0.9933f, 0.9242f, - 1.0000f, 0.9965f, 0.9984f, 0.9569f, 1.0000f, 0.9549f, 0.7884f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 0.9581f, 0.8383f, - 1.0000f, 0.9432f, 0.9153f, 0.3536f, 1.0000f, 0.9776f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.7707f, 1.0000f, - 1.0000f, 0.9633f, 0.7557f, 0.4993f, 1.0000f, 0.7614f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9484f, 1.0000f, 1.0000f, - 1.0000f, 0.9886f, 0.2465f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9658f, 1.0000f, 1.0000f, - 1.0000f, 0.8347f, 0.3603f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6969f, 1.0000f, 1.0000f, - 1.0000f, 0.9607f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2712f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9757f, 0.9905f, 0.9637f, 0.7902f, 0.9592f, 0.6376f, 1.0000f, 0.6241f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9900f, 0.9831f, 0.7849f, 0.1502f, 0.8443f, 0.9768f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.9854f, 0.7457f, 0.8597f, 0.5791f, 0.9147f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9997f, - 1.0000f, 0.9519f, 0.3733f, 1.0000f, 0.2792f, 0.9916f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9998f, - 1.0000f, 0.9691f, 0.6943f, 1.0000f, 0.0378f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.9999f, - 1.0000f, 0.3982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9985f, 1.0000f, - 1.0000f, 0.8798f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9968f, - 1.0000f, 0.6759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9799f, 0.9975f, 0.9937f, - 1.0000f, 0.9971f, 0.9734f, 0.8927f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9917f, - 1.0000f, 0.9860f, 0.9300f, 0.8151f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 0.9921f, - 1.0000f, 0.9802f, 0.6310f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 0.9996f, - 1.0000f, 0.8825f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9986f, 0.9797f, - 1.0000f, 0.9451f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9931f, 0.9668f, - 1.0000f, 0.4730f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.9743f, 0.4395f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9615f, 0.6647f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9794f, 0.8286f, 0.7373f, - 1.0000f, 0.9981f, 0.9306f, 0.7595f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7725f, 1.0000f, 1.0000f, - 1.0000f, 0.9950f, 0.9230f, 0.9816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9846f, 1.0000f, 1.0000f, - 1.0000f, 0.1394f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6675f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8395f, 0.7330f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7263f, 0.9914f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7044f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9998f, 0.9990f, - 1.0000f, 0.8990f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9814f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9923f, 0.9998f, 0.9988f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.9994f, 0.9540f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9392f, 0.8000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9442f, 0.9205f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9173f, 0.8638f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8092f, 0.9994f, 1.0000f, - 1.0000f, 0.9976f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9995f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9862f, 0.9987f, 0.9199f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8678f, 0.2109f, 0.8533f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7936f, 0.5668f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2632f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7415f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9686f, 0.9633f, 1.0000f, 1.0000f, 0.9876f, 0.9703f, 1.0000f, 1.0000f, 0.9951f, 0.9984f, - 1.0000f, 1.0000f, 1.0000f, 0.9987f, 1.0000f, 0.9597f, 0.9643f, 0.9819f, 1.0000f, 0.9912f, 0.9804f, 0.9869f, 1.0000f, 1.0000f, 0.9981f, 0.9988f, - 1.0000f, 1.0000f, 0.9866f, 0.9992f, 1.0000f, 0.9719f, 0.9513f, 0.6842f, 1.0000f, 0.9677f, 0.9562f, 0.6732f, 1.0000f, 1.0000f, 0.9855f, 0.8914f, - 1.0000f, 1.0000f, 0.9976f, 0.9965f, 1.0000f, 0.9489f, 0.9125f, 0.6280f, 1.0000f, 0.9555f, 0.9218f, 0.5776f, 1.0000f, 0.9848f, 0.9797f, 0.9019f, - 1.0000f, 1.0000f, 0.9722f, 0.9737f, 1.0000f, 0.9587f, 0.7179f, 0.6996f, 1.0000f, 0.9485f, 0.6921f, 0.6084f, 1.0000f, 0.9680f, 0.9464f, 0.5482f, - 1.0000f, 0.9928f, 0.9888f, 0.9630f, 1.0000f, 0.9316f, 0.3859f, 0.9151f, 1.0000f, 0.9447f, 0.3221f, 0.8991f, 1.0000f, 0.9326f, 0.8735f, 0.5210f, - 1.0000f, 1.0000f, 0.9365f, 0.9047f, 1.0000f, 0.8590f, 0.1201f, 1.0000f, 1.0000f, 0.8610f, 0.0462f, 1.0000f, 1.0000f, 0.9540f, 0.5048f, 0.2354f, - 1.0000f, 0.9551f, 0.8452f, 0.5669f, 1.0000f, 0.6051f, 0.6704f, 1.0000f, 1.0000f, 0.5983f, 0.6150f, 1.0000f, 1.0000f, 0.8866f, 0.3154f, 0.5406f, - 1.0000f, 1.0000f, 1.0000f, 0.9933f, 1.0000f, 1.0000f, 0.9700f, 0.8930f, 1.0000f, 1.0000f, 0.9765f, 0.9073f, 1.0000f, 1.0000f, 0.9902f, 0.9934f, - 1.0000f, 1.0000f, 0.9981f, 0.9982f, 1.0000f, 0.9750f, 0.9306f, 0.8620f, 1.0000f, 0.9620f, 0.9649f, 0.9113f, 1.0000f, 1.0000f, 0.9873f, 0.9878f, - 1.0000f, 1.0000f, 0.9914f, 0.9909f, 1.0000f, 0.9438f, 0.9067f, 0.5915f, 1.0000f, 0.9614f, 0.9212f, 0.6344f, 1.0000f, 1.0000f, 0.9509f, 0.9341f, - 1.0000f, 0.9852f, 0.9853f, 0.8669f, 1.0000f, 0.9356f, 0.5949f, 0.6407f, 1.0000f, 0.9346f, 0.6376f, 0.4882f, 1.0000f, 0.9729f, 0.9817f, 0.6647f, - 1.0000f, 1.0000f, 0.9573f, 0.9224f, 1.0000f, 0.9274f, 0.4705f, 0.8707f, 1.0000f, 0.9403f, 0.4174f, 0.8396f, 1.0000f, 0.9793f, 0.8539f, 0.5595f, - 1.0000f, 0.9601f, 0.8529f, 0.5705f, 1.0000f, 0.8121f, 0.5007f, 1.0000f, 1.0000f, 0.8227f, 0.3286f, 1.0000f, 1.0000f, 0.9005f, 0.4838f, 0.7513f, - 1.0000f, 0.9580f, 0.8753f, 0.4567f, 1.0000f, 0.7422f, 0.4275f, 1.0000f, 1.0000f, 0.8005f, 0.2434f, 1.0000f, 1.0000f, 0.9144f, 0.3582f, 0.6117f, - 1.0000f, 0.8841f, 0.6788f, 0.2513f, 1.0000f, 0.3348f, 1.0000f, 1.0000f, 1.0000f, 0.2949f, 1.0000f, 1.0000f, 1.0000f, 0.8093f, 0.4613f, 1.0000f, - 1.0000f, 1.0000f, 0.9864f, 0.9965f, 0.8998f, 1.0000f, 0.7243f, 0.8107f, 0.0803f, 1.0000f, 0.7755f, 0.8807f, 0.4324f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9950f, 0.9978f, 0.6895f, 0.7113f, 0.7020f, 0.5330f, 0.1295f, 0.7285f, 0.8149f, 0.7201f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9716f, 0.9012f, 0.8314f, 0.8162f, 0.7403f, 0.4018f, 0.1665f, 0.8953f, 0.7566f, 0.3912f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9826f, 0.9683f, 0.9087f, 0.4518f, 0.8983f, 0.5740f, 0.7068f, 1.0000f, 0.8882f, 0.6815f, 0.6676f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9697f, 0.9468f, 0.5558f, 0.8507f, 0.7495f, 0.3965f, 1.0000f, 0.4659f, 0.8350f, 0.2666f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9434f, 0.8815f, 0.5520f, 0.0320f, 0.7157f, 0.7929f, 1.0000f, 1.0000f, 0.7477f, 0.7804f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, - 1.0000f, 0.9501f, 0.5129f, 0.2591f, 0.7670f, 0.6869f, 1.0000f, 1.0000f, 1.0000f, 0.7601f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.9157f, 0.3086f, 0.5089f, 0.2273f, 0.1008f, 1.0000f, 1.0000f, 1.0000f, 0.1575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9998f, - 1.0000f, 1.0000f, 0.9782f, 0.9947f, 0.3018f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9861f, 0.9815f, 0.6439f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9533f, 0.9300f, 0.2022f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, - 1.0000f, 0.9694f, 0.9476f, 0.6589f, 0.6500f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, - 1.0000f, 0.9832f, 0.8922f, 0.5632f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9925f, 0.9988f, - 1.0000f, 0.9033f, 0.5170f, 0.7771f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9944f, - 1.0000f, 0.8824f, 0.3804f, 0.6469f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9953f, 0.9770f, - 1.0000f, 0.8290f, 0.5291f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9921f, 0.8798f, - 1.0000f, 0.9995f, 0.9992f, 0.9980f, 1.0000f, 0.9230f, 0.8849f, 0.9590f, 1.0000f, 0.9375f, 0.8938f, 0.9370f, 1.0000f, 0.9952f, 0.9968f, 0.9891f, - 1.0000f, 0.9998f, 0.9999f, 0.9623f, 1.0000f, 0.8640f, 0.9493f, 0.9881f, 1.0000f, 0.9163f, 0.9521f, 0.9779f, 1.0000f, 0.9857f, 0.9874f, 0.8890f, - 1.0000f, 0.9529f, 0.9871f, 0.9802f, 1.0000f, 0.8078f, 0.9544f, 1.0000f, 1.0000f, 0.8049f, 0.9442f, 1.0000f, 1.0000f, 0.7091f, 0.8906f, 0.9671f, - 1.0000f, 0.9206f, 0.9652f, 0.8302f, 1.0000f, 0.9175f, 1.0000f, 1.0000f, 1.0000f, 0.9040f, 1.0000f, 1.0000f, 1.0000f, 0.8408f, 0.9336f, 0.9905f, - 1.0000f, 0.9026f, 0.8833f, 0.9784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7531f, 0.9407f, 1.0000f, - 1.0000f, 0.8580f, 0.9611f, 0.9774f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8961f, 1.0000f, 1.0000f, - 1.0000f, 0.3694f, 0.8697f, 0.9674f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7548f, 0.9384f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9986f, 0.9900f, 0.8968f, 1.0000f, 0.7044f, 0.7787f, 0.9517f, 1.0000f, 0.7975f, 0.7738f, 0.9060f, 1.0000f, 0.9295f, 0.9662f, 0.9760f, - 1.0000f, 0.9985f, 0.9788f, 0.9351f, 1.0000f, 0.7459f, 0.8189f, 0.9899f, 1.0000f, 0.8385f, 0.8560f, 0.9927f, 1.0000f, 0.9859f, 0.9451f, 0.9813f, - 1.0000f, 0.9398f, 0.7653f, 0.8486f, 1.0000f, 0.8762f, 1.0000f, 1.0000f, 1.0000f, 0.8550f, 1.0000f, 1.0000f, 1.0000f, 0.7990f, 0.9862f, 1.0000f, - 1.0000f, 0.9188f, 0.9268f, 0.9119f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9235f, 0.9923f, 1.0000f, - 1.0000f, 0.3638f, 0.8497f, 0.9668f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9194f, 1.0000f, 1.0000f, - 1.0000f, 0.7721f, 0.9481f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7346f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7704f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9916f, 0.9926f, 0.9742f, 0.8201f, 0.3525f, 0.5368f, 0.9252f, 0.1389f, 0.6017f, 0.5846f, 0.9800f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9692f, 0.9834f, 0.8570f, 0.4373f, 0.4071f, 0.8277f, 1.0000f, 1.0000f, 0.6312f, 0.8326f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7135f, 0.8858f, 0.9594f, 0.8362f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8430f, 0.9389f, 0.9836f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.8020f, 0.9460f, 1.0000f, 0.7306f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9995f, 0.9964f, - 1.0000f, 0.9285f, 1.0000f, 1.0000f, 0.2108f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9655f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0909f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9918f, 0.9200f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9830f, 0.9607f, - 1.0000f, 0.9290f, 0.9421f, 0.9604f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.9828f, 0.9246f, 0.9525f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.7851f, 0.9734f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9943f, 0.9505f, - 1.0000f, 0.9106f, 0.9840f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9922f, 0.7867f, - 1.0000f, 0.8945f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9636f, 0.9886f, 0.9732f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9132f, 0.9640f, 0.8441f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9472f, 0.7687f, 0.8373f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9321f, 0.9331f, 0.8976f, - 1.0000f, 1.0000f, 0.9970f, 0.9996f, 1.0000f, 0.9956f, 0.9182f, 0.8475f, 1.0000f, 0.9569f, 0.9838f, 0.8688f, 1.0000f, 0.9911f, 0.9938f, 0.9884f, - 1.0000f, 1.0000f, 0.9954f, 0.9995f, 1.0000f, 0.9896f, 0.8725f, 1.0000f, 1.0000f, 0.9972f, 0.9258f, 1.0000f, 1.0000f, 0.9915f, 0.9963f, 0.8176f, - 1.0000f, 1.0000f, 0.9985f, 0.9393f, 1.0000f, 0.9850f, 0.9937f, 1.0000f, 1.0000f, 0.9822f, 0.9705f, 1.0000f, 1.0000f, 0.9993f, 0.7636f, 0.8899f, - 1.0000f, 0.9903f, 0.9844f, 0.3467f, 1.0000f, 0.7883f, 1.0000f, 1.0000f, 1.0000f, 0.8252f, 1.0000f, 1.0000f, 1.0000f, 0.9617f, 0.4750f, 1.0000f, - 1.0000f, 0.9984f, 0.9053f, 0.2192f, 1.0000f, 0.9093f, 1.0000f, 1.0000f, 1.0000f, 0.9497f, 1.0000f, 1.0000f, 1.0000f, 0.9416f, 0.7365f, 1.0000f, - 1.0000f, 0.9811f, 0.4965f, 0.9930f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5251f, 1.0000f, 1.0000f, - 1.0000f, 0.9967f, 0.2879f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8716f, 1.0000f, 1.0000f, - 1.0000f, 0.8600f, 0.4470f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5880f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9974f, 0.9971f, 1.0000f, 0.9969f, 0.8630f, 0.9966f, 1.0000f, 0.9889f, 0.9241f, 0.9946f, 1.0000f, 0.9962f, 0.9767f, 0.8874f, - 1.0000f, 0.9973f, 0.9999f, 0.9762f, 1.0000f, 0.9689f, 0.8135f, 1.0000f, 1.0000f, 0.9868f, 0.8744f, 1.0000f, 1.0000f, 0.9936f, 0.9708f, 0.8064f, - 1.0000f, 0.9990f, 0.9138f, 0.3409f, 1.0000f, 0.9786f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, 1.0000f, 1.0000f, 1.0000f, 0.9957f, 0.6530f, 1.0000f, - 1.0000f, 0.9665f, 0.7584f, 0.6215f, 1.0000f, 0.7960f, 1.0000f, 1.0000f, 1.0000f, 0.8214f, 1.0000f, 1.0000f, 1.0000f, 0.9263f, 1.0000f, 1.0000f, - 1.0000f, 0.9932f, 0.2809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9576f, 1.0000f, 1.0000f, - 1.0000f, 0.8789f, 0.4794f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4923f, 1.0000f, 1.0000f, - 1.0000f, 0.9727f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.1844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9894f, 0.9931f, 0.9646f, 0.8518f, 0.9740f, 0.6760f, 1.0000f, 0.3749f, 0.9755f, 0.8771f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9972f, 0.9806f, 0.8419f, 0.0162f, 0.8659f, 0.9745f, 1.0000f, 1.0000f, 0.9566f, 0.9892f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.9955f, 0.7384f, 0.9080f, 0.7326f, 0.9169f, 1.0000f, 1.0000f, 1.0000f, 0.9791f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.9986f, - 1.0000f, 0.9714f, 0.4225f, 1.0000f, 0.1933f, 0.9795f, 1.0000f, 1.0000f, 1.0000f, 0.9809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.9757f, 0.7914f, 1.0000f, 0.1105f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 0.9997f, - 1.0000f, 0.5444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9977f, 0.9994f, 0.9940f, - 1.0000f, 0.8678f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.6618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 1.0000f, 0.9897f, - 1.0000f, 0.9991f, 0.9658f, 0.9429f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 1.0000f, - 1.0000f, 0.9942f, 0.9425f, 0.8780f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9846f, - 1.0000f, 0.9939f, 0.6971f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9711f, - 1.0000f, 0.9455f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9724f, 0.9777f, - 1.0000f, 0.9752f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9883f, 0.9412f, - 1.0000f, 0.5811f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9982f, 0.9879f, 0.4421f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9536f, 0.4123f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9583f, 0.7820f, 0.6183f, - 1.0000f, 0.9994f, 0.9772f, 0.8035f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7264f, 1.0000f, 1.0000f, - 1.0000f, 0.9959f, 0.9627f, 0.9908f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9379f, 1.0000f, 1.0000f, - 1.0000f, 0.1755f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9100f, 0.8338f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 1.0000f, 1.0000f, - 1.0000f, 0.8265f, 0.9995f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7899f, 1.0000f, 1.0000f, 0.7619f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9747f, 1.0000f, 1.0000f, 0.0584f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, 0.9824f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0694f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9979f, 0.9990f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9842f, 1.0000f, 0.9311f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.9361f, 0.7836f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9919f, 0.9279f, 0.9548f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8649f, 0.7440f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7945f, 0.9959f, 1.0000f, - 1.0000f, 0.9941f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9961f, 1.0000f, 0.9960f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9990f, 0.9559f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8464f, 0.2739f, 0.8240f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7222f, 0.6248f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1483f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6559f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_10x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9716f, 0.9549f, 1.0000f, 1.0000f, 0.9881f, 0.9817f, 1.0000f, 1.0000f, 0.9984f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, 0.9617f, 0.9697f, 0.9825f, 1.0000f, 0.9916f, 0.9941f, 0.9689f, 1.0000f, 1.0000f, 0.9980f, 0.9967f, - 1.0000f, 1.0000f, 0.9940f, 0.9997f, 1.0000f, 0.9641f, 0.9573f, 0.7080f, 1.0000f, 0.9918f, 0.9662f, 0.6854f, 1.0000f, 1.0000f, 0.9815f, 0.8889f, - 1.0000f, 1.0000f, 0.9986f, 0.9932f, 1.0000f, 0.9604f, 0.8881f, 0.6661f, 1.0000f, 0.9456f, 0.9130f, 0.6171f, 1.0000f, 0.9600f, 0.9718f, 0.8502f, - 1.0000f, 1.0000f, 0.9899f, 0.9695f, 1.0000f, 0.9535f, 0.7366f, 0.7328f, 1.0000f, 0.9656f, 0.6923f, 0.6142f, 1.0000f, 0.9768f, 0.9189f, 0.4897f, - 1.0000f, 0.9924f, 0.9956f, 0.9650f, 1.0000f, 0.9349f, 0.4381f, 0.9238f, 1.0000f, 0.9344f, 0.3154f, 0.8961f, 1.0000f, 0.9270f, 0.8531f, 0.4604f, - 1.0000f, 1.0000f, 0.9731f, 0.8982f, 1.0000f, 0.9222f, 0.2598f, 1.0000f, 1.0000f, 0.8512f, 0.0780f, 1.0000f, 1.0000f, 0.9411f, 0.5059f, 0.1684f, - 1.0000f, 0.9853f, 0.8463f, 0.5987f, 1.0000f, 0.7036f, 0.7578f, 1.0000f, 1.0000f, 0.5618f, 0.5823f, 1.0000f, 1.0000f, 0.9093f, 0.2466f, 0.4938f, - 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.6877f, 1.0000f, 0.9506f, 0.9112f, 0.8695f, 1.0000f, 0.9909f, 0.9042f, 0.6611f, 1.0000f, 0.9971f, 0.9973f, - 1.0000f, 1.0000f, 0.9974f, 0.9945f, 0.8704f, 0.9827f, 0.9296f, 0.8642f, 0.7829f, 0.9553f, 0.9673f, 0.9380f, 0.4690f, 1.0000f, 0.9977f, 0.9878f, - 1.0000f, 1.0000f, 0.9936f, 0.9937f, 0.8453f, 0.9626f, 0.9036f, 0.6373f, 0.5889f, 0.9782f, 0.9155f, 0.6586f, 0.8293f, 1.0000f, 0.9483f, 0.9081f, - 1.0000f, 0.9770f, 0.9960f, 0.8975f, 0.7997f, 0.9502f, 0.6401f, 0.6686f, 0.2911f, 0.9460f, 0.6316f, 0.6259f, 1.0000f, 0.9647f, 0.9653f, 0.5176f, - 1.0000f, 1.0000f, 0.9539f, 0.9143f, 0.7943f, 0.9415f, 0.5253f, 0.8772f, 0.8247f, 0.9546f, 0.4239f, 0.8443f, 1.0000f, 0.9667f, 0.8114f, 0.3782f, - 1.0000f, 0.9475f, 0.8596f, 0.5652f, 0.2972f, 0.8259f, 0.5789f, 1.0000f, 1.0000f, 0.7983f, 0.4472f, 1.0000f, 1.0000f, 0.8624f, 0.3886f, 0.6508f, - 1.0000f, 0.9756f, 0.8669f, 0.5440f, 0.3675f, 0.7915f, 0.5583f, 1.0000f, 1.0000f, 0.7814f, 0.2532f, 1.0000f, 1.0000f, 0.9275f, 0.1836f, 0.4092f, - 1.0000f, 0.9254f, 0.6991f, 0.3033f, 0.7123f, 0.4979f, 1.0000f, 1.0000f, 1.0000f, 0.2196f, 1.0000f, 1.0000f, 1.0000f, 0.7439f, 0.3214f, 1.0000f, - 1.0000f, 1.0000f, 0.9913f, 0.9990f, 0.9260f, 1.0000f, 0.7166f, 0.8163f, 0.1280f, 1.0000f, 0.9804f, 0.9570f, 0.5137f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9986f, 0.9958f, 0.6808f, 0.7347f, 0.7269f, 0.5512f, 0.2056f, 0.9692f, 0.9311f, 0.7956f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9912f, 0.9330f, 0.8432f, 0.8578f, 0.8151f, 0.4516f, 0.2333f, 0.9808f, 0.7643f, 0.4815f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9791f, 0.9659f, 0.9233f, 0.5098f, 0.9023f, 0.6230f, 0.7385f, 1.0000f, 0.9398f, 0.6112f, 0.7403f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9765f, 0.9490f, 0.5687f, 0.8738f, 0.8088f, 0.4042f, 1.0000f, 0.6081f, 0.9160f, 0.1911f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9195f, 0.8835f, 0.5548f, 0.0989f, 0.7544f, 0.7770f, 1.0000f, 1.0000f, 0.7101f, 0.7594f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.9580f, 0.6050f, 0.3332f, 0.7527f, 0.7228f, 1.0000f, 1.0000f, 1.0000f, 0.8075f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, - 1.0000f, 0.9393f, 0.3938f, 0.6482f, 0.2400f, 0.1760f, 1.0000f, 1.0000f, 1.0000f, 0.0560f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9998f, - 1.0000f, 1.0000f, 0.9736f, 0.9949f, 0.3729f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9362f, 0.8827f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9895f, 0.9772f, 0.4191f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9402f, 0.9494f, 0.6287f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9664f, 0.9577f, 0.2127f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9200f, 0.5291f, 0.5955f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, - 1.0000f, 0.9786f, 0.9836f, 0.6900f, 0.6710f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8904f, 0.3506f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, - 1.0000f, 0.9594f, 0.8940f, 0.6019f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8304f, 0.2849f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9964f, - 1.0000f, 0.8968f, 0.5403f, 0.7901f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5756f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9953f, - 1.0000f, 0.9463f, 0.4856f, 0.7144f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7492f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9840f, 0.9846f, - 1.0000f, 0.8236f, 0.5856f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1607f, 1.0000f, 1.0000f, 1.0000f, 0.9910f, 0.9903f, 0.8559f, - 1.0000f, 0.9983f, 0.9999f, 0.9988f, 1.0000f, 0.9367f, 0.8747f, 0.9723f, 1.0000f, 0.9713f, 0.9100f, 0.9777f, 1.0000f, 0.9908f, 0.9905f, 0.9823f, - 1.0000f, 0.9998f, 0.9998f, 0.9524f, 1.0000f, 0.8713f, 0.9498f, 0.9708f, 1.0000f, 0.9002f, 0.9584f, 0.9849f, 1.0000f, 0.9597f, 0.9860f, 0.8493f, - 1.0000f, 0.9610f, 0.9926f, 0.9847f, 1.0000f, 0.8550f, 0.9753f, 1.0000f, 1.0000f, 0.8315f, 0.9620f, 1.0000f, 1.0000f, 0.6760f, 0.8540f, 0.9183f, - 1.0000f, 0.9178f, 0.9763f, 0.8270f, 1.0000f, 0.9487f, 1.0000f, 1.0000f, 1.0000f, 0.9353f, 1.0000f, 1.0000f, 1.0000f, 0.8063f, 0.9320f, 0.9884f, - 1.0000f, 0.8873f, 0.8521f, 0.9799f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7692f, 0.9371f, 1.0000f, - 1.0000f, 0.8200f, 0.9590f, 0.9531f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8587f, 1.0000f, 1.0000f, - 1.0000f, 0.3620f, 0.8788f, 0.9784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7660f, 0.9629f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9959f, 0.9900f, 0.9049f, 0.5366f, 0.6946f, 0.7858f, 0.9520f, 0.9687f, 0.8348f, 0.8911f, 0.9876f, 1.0000f, 0.9172f, 0.9635f, 0.9810f, - 1.0000f, 0.9996f, 0.9893f, 0.9467f, 0.6560f, 0.7627f, 0.8023f, 0.9914f, 1.0000f, 0.8843f, 0.9556f, 0.9977f, 1.0000f, 0.9874f, 0.9306f, 0.9793f, - 1.0000f, 0.9448f, 0.7970f, 0.8651f, 1.0000f, 0.9118f, 1.0000f, 1.0000f, 1.0000f, 0.8954f, 1.0000f, 1.0000f, 1.0000f, 0.7058f, 0.9513f, 1.0000f, - 1.0000f, 0.9444f, 0.9431f, 0.9452f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8812f, 0.9898f, 1.0000f, - 1.0000f, 0.3834f, 0.8392f, 0.9644f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8326f, 1.0000f, 1.0000f, - 1.0000f, 0.7676f, 0.9427f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8370f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9935f, 0.9873f, 0.9741f, 0.7887f, 0.3990f, 0.5329f, 0.9291f, 0.0443f, 0.6534f, 0.8422f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9802f, 0.9806f, 0.8606f, 0.3391f, 0.4334f, 0.8188f, 1.0000f, 1.0000f, 0.7561f, 0.9869f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7708f, 0.9062f, 0.9955f, 0.8359f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8858f, 0.9738f, 0.9886f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.8050f, 0.9423f, 1.0000f, 0.7248f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9993f, 0.9968f, - 1.0000f, 0.9149f, 1.0000f, 1.0000f, 0.1529f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9385f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1367f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9788f, 0.9228f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9829f, 0.9542f, - 1.0000f, 0.9325f, 0.9623f, 0.9567f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6831f, 0.9009f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9919f, 0.9440f, 0.9779f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7799f, 0.9842f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9999f, - 1.0000f, 0.8101f, 0.9758f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9887f, 0.9280f, - 1.0000f, 0.9211f, 0.9879f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9934f, 0.8037f, - 1.0000f, 0.9137f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9471f, 0.9638f, 0.9613f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8721f, 0.9479f, 0.8010f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9249f, 0.7510f, 0.8282f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9068f, 0.9265f, 0.9339f, - 1.0000f, 1.0000f, 1.0000f, 0.9994f, 1.0000f, 0.9997f, 0.9358f, 0.8851f, 1.0000f, 0.9733f, 0.9726f, 0.7929f, 1.0000f, 0.9966f, 0.9896f, 0.9684f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9868f, 0.8763f, 1.0000f, 1.0000f, 0.9710f, 0.8819f, 1.0000f, 1.0000f, 0.9987f, 0.9969f, 0.7873f, - 1.0000f, 0.9984f, 0.9927f, 0.9389f, 1.0000f, 0.9855f, 0.9989f, 1.0000f, 1.0000f, 0.9832f, 0.9166f, 1.0000f, 1.0000f, 0.9676f, 0.7308f, 0.8402f, - 1.0000f, 0.9976f, 0.9509f, 0.5721f, 1.0000f, 0.8212f, 1.0000f, 1.0000f, 1.0000f, 0.7457f, 1.0000f, 1.0000f, 1.0000f, 0.9871f, 0.2787f, 1.0000f, - 1.0000f, 0.9992f, 0.8804f, 0.1092f, 1.0000f, 0.8989f, 1.0000f, 1.0000f, 1.0000f, 0.8896f, 1.0000f, 1.0000f, 1.0000f, 0.9751f, 0.6428f, 1.0000f, - 1.0000f, 0.9335f, 0.4427f, 0.9946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4732f, 1.0000f, 1.0000f, - 1.0000f, 0.9962f, 0.2661f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8947f, 1.0000f, 1.0000f, - 1.0000f, 0.8660f, 0.4647f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4141f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9994f, 0.9988f, 0.9743f, 0.9951f, 0.8918f, 0.9844f, 0.9992f, 0.9979f, 0.9075f, 0.9902f, 1.0000f, 0.9857f, 0.9906f, 0.8678f, - 1.0000f, 0.9979f, 0.9989f, 0.9890f, 0.9217f, 0.9821f, 0.8224f, 1.0000f, 1.0000f, 0.9957f, 0.7754f, 1.0000f, 1.0000f, 0.9705f, 0.9607f, 0.7187f, - 1.0000f, 0.9988f, 0.9560f, 0.5215f, 1.0000f, 0.9406f, 1.0000f, 1.0000f, 1.0000f, 0.9892f, 1.0000f, 1.0000f, 1.0000f, 0.9970f, 0.4774f, 1.0000f, - 1.0000f, 0.9681f, 0.8412f, 0.7289f, 1.0000f, 0.8337f, 1.0000f, 1.0000f, 1.0000f, 0.6969f, 1.0000f, 1.0000f, 1.0000f, 0.8796f, 1.0000f, 1.0000f, - 1.0000f, 0.9950f, 0.3094f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9419f, 1.0000f, 1.0000f, - 1.0000f, 0.8381f, 0.5477f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1984f, 1.0000f, 1.0000f, - 1.0000f, 0.9721f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2265f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9933f, 0.9961f, 0.9746f, 0.8126f, 0.9728f, 0.6636f, 1.0000f, 0.3449f, 0.9948f, 0.8730f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9990f, 0.9944f, 0.9029f, 0.0172f, 0.8615f, 0.9760f, 1.0000f, 1.0000f, 0.9795f, 0.9851f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9991f, 0.8139f, 0.9376f, 0.7013f, 0.9301f, 1.0000f, 1.0000f, 1.0000f, 0.9748f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9943f, - 1.0000f, 0.9838f, 0.6784f, 1.0000f, 0.1186f, 0.9974f, 1.0000f, 1.0000f, 1.0000f, 0.9563f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9999f, - 1.0000f, 0.9775f, 0.8473f, 1.0000f, 0.1451f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9939f, - 1.0000f, 0.6201f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 1.0000f, 0.9982f, - 1.0000f, 0.9106f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.9982f, - 1.0000f, 0.6344f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9999f, 0.9866f, - 1.0000f, 1.0000f, 0.9864f, 0.9587f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9929f, 0.8996f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9942f, - 1.0000f, 0.9987f, 0.9700f, 0.8933f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9670f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, - 1.0000f, 0.9968f, 0.7724f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9834f, 0.9930f, - 1.0000f, 0.9244f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9952f, 0.9813f, - 1.0000f, 0.9315f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9528f, 0.9055f, - 1.0000f, 0.6455f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9797f, 0.9517f, 0.3273f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9286f, 0.3564f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9678f, 0.7611f, 0.4560f, - 1.0000f, 0.9999f, 0.9819f, 0.8633f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7207f, 1.0000f, 1.0000f, - 1.0000f, 0.9976f, 0.9917f, 0.9859f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8687f, 1.0000f, 1.0000f, - 1.0000f, 0.2725f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6735f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9435f, 0.8755f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8780f, 0.9882f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8483f, 1.0000f, 1.0000f, 0.7421f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, - 1.0000f, 0.9703f, 1.0000f, 1.0000f, 0.0320f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9995f, 0.9862f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0885f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9889f, 0.9995f, 0.9923f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9921f, 1.0000f, 0.9087f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9938f, 0.9016f, 0.7475f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.8866f, 0.9206f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8926f, 0.7739f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7785f, 0.9954f, 1.0000f, - 1.0000f, 0.9947f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9922f, 0.9971f, 0.9995f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, 0.9632f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8569f, 0.4287f, 0.9124f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8176f, 0.5923f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0673f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5019f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x4[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3987f, 1.0000f, 0.8105f, 0.9465f, 0.6269f, 1.0000f, 1.0000f, 1.0000f, 0.4786f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9946f, 0.4927f, 0.8691f, 0.9409f, 0.9569f, 0.6578f, 1.0000f, 1.0000f, 1.0000f, 0.3330f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9774f, 0.9944f, 0.6317f, 0.9052f, 0.8220f, 0.4495f, 0.2254f, 1.0000f, 1.0000f, 1.0000f, 0.6854f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9899f, 0.9969f, 0.4996f, 0.7915f, 0.7847f, 0.3712f, 0.2484f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9222f, 0.9576f, 0.0327f, 0.7824f, 0.5463f, 0.2705f, 0.5641f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9588f, 0.9649f, 0.9819f, 0.0000f, 0.7730f, 0.1103f, 0.7281f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9765f, 1.0000f, 1.0000f, 0.8239f, 0.8201f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9752f, 0.9908f, 1.0000f, 0.8276f, 0.8844f, 0.8735f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9556f, 0.9815f, 1.0000f, 0.8485f, 0.7369f, 0.4165f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9516f, 0.9700f, 0.9086f, 1.0000f, 0.7451f, 0.2920f, 0.3618f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9140f, 0.9393f, 1.0000f, 0.7892f, 0.1733f, 0.6363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8957f, 0.8434f, 0.7064f, 1.0000f, 0.5583f, 0.1276f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9232f, 0.9870f, 1.0000f, 1.0000f, 0.8023f, 0.9005f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9643f, 0.9885f, 1.0000f, 0.7396f, 0.7581f, 0.5865f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8804f, 0.8417f, 1.0000f, 0.8124f, 0.5064f, 0.1583f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9119f, 0.9280f, 0.8615f, 1.0000f, 0.7251f, 0.3026f, 0.4857f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9242f, 0.8646f, 0.5754f, 1.0000f, 0.7129f, 0.0626f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7937f, 0.8085f, 0.3805f, 1.0000f, 0.3230f, 0.4715f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9791f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9350f, 0.9756f, 1.0000f, 1.0000f, 0.7032f, 0.7530f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9289f, 0.9613f, 1.0000f, 0.7607f, 0.8348f, 0.4076f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8469f, 0.8502f, 1.0000f, 0.7097f, 0.2132f, 0.4338f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, - 1.0000f, 0.8383f, 0.9028f, 0.3427f, 1.0000f, 0.6778f, 0.0888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, - 1.0000f, 0.9192f, 0.6998f, 0.2005f, 1.0000f, 0.5199f, 0.1431f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9722f, 0.9911f, - 1.0000f, 0.7191f, 0.4643f, 0.2370f, 1.0000f, 0.1870f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9880f, 0.9913f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9983f, 0.9986f, 0.9933f, 0.5399f, 0.9017f, 0.8400f, 0.9384f, 0.9543f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9991f, 0.9995f, 0.9434f, 0.6659f, 0.8182f, 0.9162f, 0.8777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9851f, 0.9922f, 0.9841f, 1.0000f, 0.6410f, 0.8920f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9550f, 0.9848f, 0.8706f, 1.0000f, 0.7958f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9367f, 0.8451f, 0.9307f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8749f, 0.9202f, 0.9473f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9989f, 0.9787f, 0.8583f, 1.0000f, 0.6891f, 0.8144f, 0.9844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9973f, 0.9727f, 0.9359f, 1.0000f, 0.8044f, 0.9270f, 0.9637f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9716f, 0.7980f, 0.8763f, 1.0000f, 0.6497f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9495f, 0.9563f, 0.8969f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5919f, 0.8330f, 0.9582f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6928f, 0.9827f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9811f, 0.9855f, 0.9683f, 1.0000f, 0.4570f, 0.8567f, 0.9342f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9450f, 0.9625f, 0.7777f, 1.0000f, 0.6816f, 0.9803f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6454f, 0.8163f, 0.9261f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.7801f, 0.8908f, 0.9601f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.5523f, 0.9426f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9888f, 0.9947f, 0.9953f, - 1.0000f, 0.7682f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9935f, 0.9936f, 0.9130f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8294f, 0.9172f, 0.9442f, 1.0000f, 0.5697f, 0.8993f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 1.0000f, - 1.0000f, 0.9799f, 0.8661f, 0.8981f, 1.0000f, 0.8257f, 0.9834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9985f, - 1.0000f, 0.6220f, 0.9040f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9991f, 0.9920f, - 1.0000f, 0.7870f, 0.9063f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9993f, 0.9376f, - 1.0000f, 0.7556f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9925f, 0.9882f, 0.9672f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9795f, 0.9778f, 0.7657f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9893f, 0.9977f, 0.9417f, 0.9732f, 0.8365f, 0.6171f, 0.9996f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9968f, 0.9972f, 0.9942f, 0.8001f, 0.9151f, 0.6739f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9958f, 0.9770f, 0.9595f, 1.0000f, 0.9182f, 0.9074f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9906f, 0.9689f, 0.6021f, 1.0000f, 0.5267f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9891f, 0.9619f, 0.8312f, 1.0000f, 0.9742f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9830f, 0.8882f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9660f, 0.9823f, 1.0000f, 0.9401f, 0.7424f, 0.9711f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9737f, 0.9864f, 0.9108f, 1.0000f, 0.9315f, 0.5810f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9877f, 0.8630f, 0.5334f, 1.0000f, 0.9324f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8933f, 0.7632f, 0.6537f, 1.0000f, 0.6071f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9861f, 0.8599f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9509f, 0.9631f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9867f, 0.9530f, 0.9298f, 1.0000f, 0.9458f, 0.7339f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9783f, 0.9480f, 0.5132f, 1.0000f, 0.8676f, 0.9502f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, - 1.0000f, 0.9655f, 0.6964f, 0.7222f, 1.0000f, 0.9251f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9994f, - 1.0000f, 0.8535f, 0.2596f, 1.0000f, 1.0000f, 0.9536f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, - 1.0000f, 0.9808f, 0.9896f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9858f, 0.9938f, - 1.0000f, 0.8519f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9951f, 0.9982f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9837f, 0.8895f, 0.6699f, 1.0000f, 0.8870f, 0.7504f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9987f, - 1.0000f, 0.9487f, 0.8551f, 0.3897f, 1.0000f, 0.8791f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9940f, - 1.0000f, 0.9333f, 0.3130f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9931f, 0.9980f, - 1.0000f, 0.7310f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9961f, 0.9967f, - 1.0000f, 0.9677f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9929f, 0.9904f, - 1.0000f, 0.8720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9927f, 0.9965f, 0.8945f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9916f, 0.8065f, 0.4418f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9761f, 0.7478f, 0.7706f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5970f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8857f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6619f, 0.3522f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4252f, 0.9607f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2814f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9992f, 0.9994f, - 1.0000f, 0.7160f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9959f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9997f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 1.0000f, 0.9874f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9706f, 0.9212f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9956f, 0.9747f, 0.9666f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9962f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9918f, 0.9996f, 0.9949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9954f, 0.9996f, 0.8831f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9523f, 0.6121f, 0.9694f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8817f, 0.7753f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9097f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9901f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x5[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3385f, 1.0000f, 0.8818f, 0.9311f, 0.6057f, 1.0000f, 1.0000f, 1.0000f, 0.4810f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9964f, 0.5033f, 0.8904f, 0.9448f, 0.9478f, 0.6751f, 1.0000f, 1.0000f, 1.0000f, 0.2676f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9709f, 0.9951f, 0.6474f, 0.9174f, 0.8674f, 0.5245f, 0.3884f, 1.0000f, 1.0000f, 1.0000f, 0.7693f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9930f, 0.9961f, 0.6015f, 0.8068f, 0.8243f, 0.4456f, 0.3816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9518f, 0.9659f, 0.6327f, 0.8317f, 0.4635f, 0.3678f, 0.7032f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9685f, 0.9763f, 0.9802f, 0.0329f, 0.7475f, 0.1053f, 0.6912f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8447f, 0.9466f, 0.0000f, 0.7305f, 0.0894f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9265f, 0.8518f, 0.7498f, 0.6437f, 0.4517f, 0.6364f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 0.9872f, 1.0000f, 1.0000f, 0.8462f, 0.8490f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9826f, 0.9955f, 1.0000f, 0.8700f, 0.9032f, 0.8883f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9567f, 0.9851f, 1.0000f, 0.8585f, 0.8050f, 0.4923f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9627f, 0.9705f, 0.8840f, 1.0000f, 0.7979f, 0.4016f, 0.5141f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8974f, 0.9077f, 1.0000f, 0.8015f, 0.1211f, 0.5671f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9022f, 0.8198f, 0.6137f, 1.0000f, 0.5581f, 0.1343f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9296f, 0.8531f, 0.7227f, 1.0000f, 0.5760f, 0.2593f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8611f, 0.8033f, 0.2759f, 1.0000f, 0.1907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9415f, 0.9895f, 1.0000f, 1.0000f, 0.8228f, 0.9086f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9689f, 0.9937f, 1.0000f, 0.7672f, 0.7793f, 0.6214f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9140f, 0.8712f, 1.0000f, 0.8347f, 0.5847f, 0.2424f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9333f, 0.9319f, 0.8687f, 1.0000f, 0.7753f, 0.4272f, 0.6718f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9289f, 0.8545f, 0.3748f, 1.0000f, 0.7587f, 0.0520f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8101f, 0.7773f, 0.3310f, 1.0000f, 0.3460f, 0.4334f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, - 1.0000f, 0.8749f, 0.5932f, 0.1797f, 1.0000f, 0.6816f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9729f, - 1.0000f, 0.8213f, 0.4752f, 0.6098f, 1.0000f, 0.0710f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9832f, 0.9645f, - 1.0000f, 1.0000f, 0.9484f, 0.9781f, 1.0000f, 1.0000f, 0.7254f, 0.7543f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9340f, 0.9733f, 1.0000f, 0.7888f, 0.8649f, 0.3609f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.8662f, 0.8725f, 1.0000f, 0.7356f, 0.2840f, 0.5974f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9945f, - 1.0000f, 0.8598f, 0.9059f, 0.3535f, 1.0000f, 0.7089f, 0.1575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, - 1.0000f, 0.9041f, 0.6943f, 0.1460f, 1.0000f, 0.6510f, 0.1686f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9880f, 0.9977f, - 1.0000f, 0.7061f, 0.2920f, 0.4145f, 1.0000f, 0.2016f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9943f, 0.9966f, - 1.0000f, 0.7961f, 0.3155f, 0.4080f, 1.0000f, 0.5296f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9578f, 0.9737f, - 1.0000f, 0.6880f, 0.2228f, 1.0000f, 1.0000f, 0.2330f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9663f, 0.9572f, 0.9422f, - 1.0000f, 0.9980f, 0.9971f, 0.9949f, 0.5088f, 0.9003f, 0.8376f, 0.9191f, 0.9507f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9982f, 0.9995f, 0.9122f, 0.6615f, 0.8433f, 0.9304f, 0.9113f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9874f, 0.9953f, 0.9893f, 1.0000f, 0.7331f, 0.9368f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9513f, 0.9823f, 0.8559f, 1.0000f, 0.8955f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9148f, 0.8862f, 0.9541f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8636f, 0.9636f, 0.9650f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5890f, 0.8390f, 0.9717f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7280f, 0.9835f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9990f, 0.9741f, 0.8361f, 1.0000f, 0.7200f, 0.8419f, 0.9890f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9978f, 0.9622f, 0.9157f, 1.0000f, 0.8134f, 0.9490f, 0.9713f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9588f, 0.7943f, 0.8807f, 1.0000f, 0.7713f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9501f, 0.9641f, 0.9216f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4867f, 0.8984f, 0.9395f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7453f, 0.9785f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6848f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9165f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9770f, 0.9806f, 0.9557f, 1.0000f, 0.4694f, 0.8572f, 0.9460f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9347f, 0.9617f, 0.7609f, 1.0000f, 0.6974f, 0.9842f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6581f, 0.8166f, 0.9068f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.7925f, 0.8945f, 0.9777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9999f, - 1.0000f, 0.6290f, 0.8965f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9999f, 0.9997f, - 1.0000f, 0.8258f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9813f, 0.9926f, 0.9281f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9883f, 0.8504f, - 1.0000f, 0.8332f, 0.9131f, 0.9326f, 1.0000f, 0.5626f, 0.9199f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, 1.0000f, - 1.0000f, 0.9721f, 0.8760f, 0.9257f, 1.0000f, 0.8476f, 0.9888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9984f, - 1.0000f, 0.6252f, 0.9381f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9991f, 0.9909f, - 1.0000f, 0.8150f, 0.9441f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9992f, 0.9241f, - 1.0000f, 0.6176f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9898f, 0.9942f, 0.9900f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9792f, 0.9907f, 0.8924f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9774f, 0.8288f, 0.8182f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9608f, 0.9409f, 0.8303f, - 1.0000f, 1.0000f, 0.9957f, 0.9905f, 0.9402f, 0.9551f, 0.8624f, 0.6546f, 0.9989f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9948f, 0.9959f, 0.9973f, 0.7851f, 0.9472f, 0.7429f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9952f, 0.9361f, 0.9530f, 1.0000f, 0.8914f, 0.8851f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9809f, 0.9697f, 0.4978f, 1.0000f, 0.5346f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9920f, 0.9182f, 0.6684f, 1.0000f, 0.8893f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8934f, 0.7145f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9863f, 0.9013f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9654f, 0.9752f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9598f, 0.9914f, 1.0000f, 0.9672f, 0.7733f, 0.9756f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9816f, 0.9869f, 0.9104f, 1.0000f, 0.9495f, 0.6401f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9938f, 0.8273f, 0.4208f, 1.0000f, 0.9249f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8784f, 0.7565f, 0.5490f, 1.0000f, 0.5716f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9701f, 0.5803f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8118f, 0.7997f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9799f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9233f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9860f, 0.9795f, 0.9429f, 1.0000f, 0.9546f, 0.7869f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9829f, 0.9524f, 0.5443f, 1.0000f, 0.9095f, 0.9725f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.9667f, 0.6650f, 0.7117f, 1.0000f, 0.9435f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9996f, - 1.0000f, 0.8829f, 0.2123f, 1.0000f, 1.0000f, 0.9693f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9995f, - 1.0000f, 0.8994f, 0.9224f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, 0.9967f, - 1.0000f, 0.5395f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9970f, 0.9996f, - 1.0000f, 0.9744f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9885f, 0.9935f, - 1.0000f, 0.9845f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9680f, 0.9902f, 0.9877f, - 1.0000f, 0.9866f, 0.9354f, 0.7380f, 1.0000f, 0.9273f, 0.7813f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9962f, - 1.0000f, 0.9676f, 0.8795f, 0.4396f, 1.0000f, 0.8873f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9981f, 0.9911f, - 1.0000f, 0.9593f, 0.2510f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9932f, - 1.0000f, 0.7630f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9956f, 0.9965f, - 1.0000f, 0.8772f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9854f, 0.9767f, - 1.0000f, 0.5194f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9933f, 0.9916f, 0.7003f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 0.9759f, 0.9375f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9918f, 0.9603f, 0.9839f, - 1.0000f, 0.9979f, 0.7832f, 0.4576f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9819f, 0.7906f, 0.7651f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3077f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7405f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6784f, 0.3232f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3950f, 0.9788f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, 0.9996f, - 1.0000f, 0.7520f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, 0.9946f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 1.0000f, 0.9998f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9960f, 1.0000f, 0.9968f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9562f, 0.8737f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9535f, 0.9454f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9631f, 0.9050f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9388f, 0.9974f, 1.0000f, - 1.0000f, 0.9998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9848f, 0.9989f, 0.9922f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9857f, 0.9999f, 0.8405f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9748f, 0.5536f, 0.9583f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9208f, 0.8085f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7173f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9613f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x6[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.4495f, 1.0000f, 0.9421f, 0.9517f, 0.7621f, 1.0000f, 1.0000f, 1.0000f, 0.4827f, 1.0000f, 0.8797f, 0.9512f, - 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.6889f, 0.9444f, 0.9633f, 0.9713f, 0.7033f, 1.0000f, 1.0000f, 1.0000f, 0.2638f, 1.0000f, 0.8294f, 0.9784f, - 1.0000f, 1.0000f, 0.9823f, 0.9970f, 0.6859f, 0.9375f, 0.9393f, 0.5485f, 0.3832f, 1.0000f, 1.0000f, 1.0000f, 0.7745f, 1.0000f, 0.9591f, 0.9129f, - 1.0000f, 1.0000f, 0.9882f, 0.9939f, 0.7113f, 0.8930f, 0.8759f, 0.5041f, 0.3565f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9665f, 0.9507f, 0.9281f, - 1.0000f, 1.0000f, 0.9836f, 0.9730f, 0.7294f, 0.8913f, 0.5839f, 0.4607f, 0.7344f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9532f, 0.9416f, 0.7269f, - 1.0000f, 0.9778f, 0.9935f, 0.9828f, 0.0834f, 0.8558f, 0.1109f, 0.7512f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9201f, 0.9084f, 0.5666f, - 1.0000f, 1.0000f, 0.9608f, 0.9471f, 0.0187f, 0.8450f, 0.0373f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9187f, 0.7466f, 0.3132f, - 1.0000f, 0.9668f, 0.8905f, 0.7218f, 0.6538f, 0.4989f, 0.4881f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9165f, 0.6078f, 0.6734f, - 1.0000f, 1.0000f, 1.0000f, 0.9864f, 1.0000f, 1.0000f, 0.9357f, 0.8768f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9820f, 0.9980f, 1.0000f, 0.8730f, 0.9351f, 0.9029f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9756f, 0.9866f, 1.0000f, 0.9037f, 0.8815f, 0.5438f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9759f, 0.9812f, 0.8593f, 1.0000f, 0.8778f, 0.4935f, 0.5753f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9587f, 0.9158f, 1.0000f, 0.8922f, 0.1996f, 0.6637f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9497f, 0.8462f, 0.6472f, 1.0000f, 0.6947f, 0.2723f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9746f, 0.9288f, 0.6918f, 1.0000f, 0.7394f, 0.1897f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9256f, 0.7825f, 0.3766f, 1.0000f, 0.1350f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9433f, 0.9957f, 0.8437f, 1.0000f, 0.8963f, 0.9235f, 0.3495f, 1.0000f, 1.0000f, 1.0000f, 0.7663f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9878f, 0.9916f, 0.7556f, 0.8740f, 0.8334f, 0.6505f, 0.1794f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9560f, 0.8399f, 0.7844f, 0.9005f, 0.6797f, 0.3632f, 0.1231f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9616f, 0.9492f, 0.8570f, 0.5195f, 0.8852f, 0.4662f, 0.7087f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9541f, 0.9180f, 0.4381f, 0.8109f, 0.8690f, 0.0976f, 1.0000f, 0.6404f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8824f, 0.8347f, 0.3958f, 0.0687f, 0.5530f, 0.5621f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.9398f, 0.5881f, 0.2094f, 0.6829f, 0.8060f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, - 1.0000f, 0.8980f, 0.3699f, 0.6266f, 0.1691f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9972f, 0.9942f, - 1.0000f, 1.0000f, 0.9649f, 0.9896f, 1.0000f, 1.0000f, 0.8373f, 0.7919f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9595f, 0.9810f, 1.0000f, 0.8870f, 0.8996f, 0.4081f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9091f, 0.8581f, 0.0535f, 0.8139f, 0.4143f, 0.6154f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, - 1.0000f, 0.9339f, 0.9215f, 0.3284f, 0.6116f, 0.8487f, 0.2463f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, - 1.0000f, 0.9683f, 0.7991f, 0.2283f, 1.0000f, 0.7489f, 0.3054f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9908f, 0.9988f, - 1.0000f, 0.8281f, 0.3355f, 0.5246f, 1.0000f, 0.3208f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9975f, 0.9960f, - 1.0000f, 0.9021f, 0.2551f, 0.3895f, 1.0000f, 0.6604f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9899f, 0.9975f, - 1.0000f, 0.7684f, 0.2373f, 1.0000f, 1.0000f, 0.2189f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9940f, 0.9927f, 0.9600f, - 1.0000f, 0.9982f, 0.9982f, 0.9919f, 0.4204f, 0.9122f, 0.8360f, 0.9455f, 0.9242f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 0.9657f, 0.8896f, - 1.0000f, 0.9983f, 0.9998f, 0.9194f, 0.5709f, 0.8523f, 0.9208f, 0.9487f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9698f, 0.9427f, 0.6766f, - 1.0000f, 0.9676f, 0.9870f, 0.9804f, 1.0000f, 0.7599f, 0.9275f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7956f, 0.7882f, 0.9326f, - 1.0000f, 0.9099f, 0.9750f, 0.8267f, 1.0000f, 0.8861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8184f, 0.9136f, 0.9876f, - 1.0000f, 0.9114f, 0.8887f, 0.9793f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7004f, 0.9621f, 1.0000f, - 1.0000f, 0.8604f, 0.9661f, 0.9653f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8321f, 1.0000f, 1.0000f, - 1.0000f, 0.5961f, 0.9269f, 0.9680f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8026f, 0.9850f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9991f, 0.9737f, 0.8254f, 1.0000f, 0.7578f, 0.8412f, 0.9815f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9977f, 0.9706f, 0.9173f, 1.0000f, 0.8154f, 0.9381f, 0.9787f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9522f, 0.7725f, 0.8511f, 1.0000f, 0.8043f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9313f, 0.9404f, 0.9107f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5093f, 0.8939f, 0.9564f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7974f, 0.9645f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7805f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8955f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9796f, 0.9743f, 0.9578f, 1.0000f, 0.5296f, 0.8626f, 0.9569f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9502f, 0.9672f, 0.7938f, 1.0000f, 0.7140f, 0.9726f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6192f, 0.8169f, 0.8700f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7704f, 0.8546f, 0.9765f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.7192f, 0.9068f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9997f, 0.9999f, - 1.0000f, 0.8475f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, 0.9933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9936f, 0.9807f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9961f, 0.9798f, - 1.0000f, 0.8535f, 0.9262f, 0.9476f, 1.0000f, 0.5796f, 0.8680f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9998f, - 1.0000f, 0.9762f, 0.8749f, 0.9013f, 1.0000f, 0.8076f, 0.9874f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9984f, 0.9971f, - 1.0000f, 0.6370f, 0.9144f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9984f, 0.9733f, - 1.0000f, 0.8307f, 0.9410f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9966f, 0.8637f, - 1.0000f, 0.6336f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9848f, 0.9963f, 0.9910f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9723f, 0.9868f, 0.8972f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9894f, 0.8878f, 0.9052f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9790f, 0.9691f, 0.9551f, - 1.0000f, 1.0000f, 0.9892f, 0.9945f, 0.9060f, 0.9833f, 0.8615f, 0.7166f, 0.9967f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.9818f, 0.9923f, - 1.0000f, 0.9965f, 0.9981f, 0.9988f, 0.6702f, 0.9687f, 0.7901f, 0.9998f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9857f, 0.9838f, 0.9465f, - 1.0000f, 0.9965f, 0.9918f, 0.9387f, 1.0000f, 0.9555f, 0.8227f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9801f, 0.9629f, 0.9941f, - 1.0000f, 0.9952f, 0.9781f, 0.3426f, 1.0000f, 0.5575f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9950f, 0.9076f, 1.0000f, - 1.0000f, 0.9985f, 0.9294f, 0.5392f, 1.0000f, 0.8834f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9872f, 0.9951f, 1.0000f, - 1.0000f, 0.9546f, 0.6670f, 0.9968f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8988f, 1.0000f, 1.0000f, - 1.0000f, 0.9937f, 0.7370f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9716f, 1.0000f, 1.0000f, - 1.0000f, 0.9229f, 0.8947f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9641f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9978f, 0.9913f, 1.0000f, 0.9845f, 0.7785f, 0.9709f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9898f, 0.9926f, 0.9449f, 1.0000f, 0.9527f, 0.6571f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9964f, 0.9222f, 0.2892f, 1.0000f, 0.9604f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9740f, 0.7244f, 0.4551f, 1.0000f, 0.5921f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9859f, 0.4020f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8720f, 0.6438f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9720f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7060f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9886f, 0.9840f, 0.9482f, 1.0000f, 0.9775f, 0.7319f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9884f, 0.9537f, 0.6000f, 1.0000f, 0.8787f, 0.9438f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.9831f, 0.6229f, 0.6039f, 1.0000f, 0.9332f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.9151f, 0.1467f, 1.0000f, 1.0000f, 0.9363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9987f, - 1.0000f, 0.9612f, 0.7863f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9994f, - 1.0000f, 0.4264f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9958f, 1.0000f, - 1.0000f, 0.9249f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9915f, 0.9997f, - 1.0000f, 0.8659f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9911f, 0.9992f, 0.9976f, - 1.0000f, 0.9901f, 0.9637f, 0.7765f, 1.0000f, 0.9460f, 0.7443f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9948f, 0.9953f, - 1.0000f, 0.9772f, 0.8843f, 0.5345f, 1.0000f, 0.8212f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 0.9852f, - 1.0000f, 0.9855f, 0.2975f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9930f, - 1.0000f, 0.8093f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9929f, 0.9991f, 0.9946f, - 1.0000f, 0.9045f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9947f, 0.9954f, 0.9769f, - 1.0000f, 0.2809f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9962f, 0.9903f, 0.6976f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9888f, 0.8425f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, 0.9301f, 0.9320f, - 1.0000f, 0.9921f, 0.8499f, 0.4772f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8710f, 1.0000f, 1.0000f, - 1.0000f, 0.9825f, 0.8198f, 0.8240f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9582f, 1.0000f, 1.0000f, - 1.0000f, 0.1579f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6301f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7534f, 0.4323f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5145f, 0.9573f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4438f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.8009f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, 0.9880f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9956f, 1.0000f, 0.9989f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9979f, 1.0000f, 0.9753f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9695f, 0.8806f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9843f, 0.9702f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9625f, 0.8648f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8386f, 0.9995f, 1.0000f, - 1.0000f, 0.9932f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9990f, 0.9905f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9996f, 0.8669f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9345f, 0.4718f, 0.9307f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8124f, 0.7419f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7642f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9369f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x8[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5588f, 1.0000f, 0.9555f, 0.9382f, 0.7966f, 1.0000f, 0.8855f, 0.9307f, 0.5111f, 1.0000f, 0.9837f, 0.9961f, - 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.7459f, 0.9753f, 0.9716f, 0.9745f, 0.7119f, 0.9180f, 0.9366f, 0.9447f, 0.2944f, 1.0000f, 0.9984f, 0.9977f, - 1.0000f, 1.0000f, 0.9795f, 0.9985f, 0.7823f, 0.9875f, 0.9631f, 0.6537f, 0.5466f, 0.9551f, 0.9497f, 0.7555f, 0.8055f, 1.0000f, 0.9832f, 0.9372f, - 1.0000f, 1.0000f, 0.9958f, 0.9928f, 0.7856f, 0.9324f, 0.9187f, 0.5956f, 0.4727f, 0.9296f, 0.9130f, 0.6180f, 1.0000f, 0.9859f, 0.9798f, 0.9590f, - 1.0000f, 1.0000f, 0.9783f, 0.9547f, 0.7142f, 0.9340f, 0.6273f, 0.5989f, 0.7756f, 0.9280f, 0.7721f, 0.4374f, 1.0000f, 0.9722f, 0.9488f, 0.6334f, - 1.0000f, 0.9903f, 0.9917f, 0.9750f, 0.1410f, 0.9042f, 0.2259f, 0.8376f, 1.0000f, 0.9136f, 0.4580f, 0.9216f, 1.0000f, 0.9251f, 0.8903f, 0.6054f, - 1.0000f, 1.0000f, 0.9719f, 0.9433f, 0.1210f, 0.9035f, 0.0611f, 1.0000f, 1.0000f, 0.8465f, 0.1681f, 1.0000f, 1.0000f, 0.9605f, 0.6566f, 0.3930f, - 1.0000f, 0.9786f, 0.8707f, 0.6808f, 0.6622f, 0.5338f, 0.4921f, 1.0000f, 1.0000f, 0.7419f, 0.7593f, 1.0000f, 1.0000f, 0.9302f, 0.5017f, 0.7399f, - 1.0000f, 1.0000f, 1.0000f, 0.9957f, 1.0000f, 1.0000f, 0.9582f, 0.9028f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9948f, - 1.0000f, 1.0000f, 0.9952f, 0.9942f, 1.0000f, 0.9734f, 0.9679f, 0.9143f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9916f, 0.9924f, - 1.0000f, 1.0000f, 0.9899f, 0.9852f, 1.0000f, 0.9313f, 0.9174f, 0.6242f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9666f, 0.9506f, - 1.0000f, 0.9878f, 0.9918f, 0.8600f, 1.0000f, 0.9392f, 0.6211f, 0.6980f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9768f, 0.9841f, 0.6705f, - 1.0000f, 1.0000f, 0.9707f, 0.8863f, 1.0000f, 0.9103f, 0.3685f, 0.7839f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9685f, 0.8779f, 0.4678f, - 1.0000f, 0.9773f, 0.8529f, 0.5381f, 1.0000f, 0.7498f, 0.4530f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9007f, 0.5779f, 0.6117f, - 1.0000f, 0.9790f, 0.8847f, 0.6086f, 1.0000f, 0.7358f, 0.2569f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9334f, 0.4479f, 0.5851f, - 1.0000f, 0.9423f, 0.7612f, 0.3988f, 1.0000f, 0.1593f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8341f, 0.4159f, 1.0000f, - 1.0000f, 1.0000f, 0.9815f, 0.9983f, 0.9049f, 1.0000f, 0.9377f, 0.9110f, 0.1767f, 1.0000f, 1.0000f, 1.0000f, 0.6480f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9925f, 0.9938f, 0.7252f, 0.9465f, 0.8871f, 0.6731f, 0.2795f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9656f, 0.8688f, 0.8364f, 0.9620f, 0.7337f, 0.5507f, 0.0864f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9793f, 0.9645f, 0.8590f, 0.3871f, 0.9329f, 0.5887f, 0.8069f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9725f, 0.9257f, 0.4046f, 0.8725f, 0.8993f, 0.2338f, 1.0000f, 0.4268f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9240f, 0.8539f, 0.4214f, 0.0177f, 0.6594f, 0.7209f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, - 1.0000f, 0.9535f, 0.5815f, 0.2492f, 0.7649f, 0.8139f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, - 1.0000f, 0.9070f, 0.3300f, 0.5742f, 0.0985f, 0.0349f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9969f, 0.9981f, - 1.0000f, 1.0000f, 0.9809f, 0.9877f, 1.0000f, 1.0000f, 0.8152f, 0.7981f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9755f, 0.9842f, 1.0000f, 0.9205f, 0.8770f, 0.5064f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9397f, 0.8956f, 0.2720f, 0.8971f, 0.5294f, 0.7050f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9934f, - 1.0000f, 0.9470f, 0.9543f, 0.5204f, 0.4776f, 0.8697f, 0.4103f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9887f, - 1.0000f, 0.9698f, 0.7919f, 0.3017f, 1.0000f, 0.7950f, 0.5249f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9894f, 0.9987f, - 1.0000f, 0.8486f, 0.3432f, 0.6508f, 1.0000f, 0.5158f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9988f, - 1.0000f, 0.8895f, 0.2016f, 0.3496f, 1.0000f, 0.7004f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9935f, 0.9974f, - 1.0000f, 0.7806f, 0.2870f, 1.0000f, 1.0000f, 0.3090f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9872f, 0.9921f, 0.9350f, - 1.0000f, 0.9995f, 0.9970f, 0.9964f, 0.4426f, 0.9077f, 0.8669f, 0.9479f, 0.9413f, 0.9156f, 0.7439f, 0.7379f, 1.0000f, 0.9956f, 0.9992f, 0.9788f, - 1.0000f, 0.9999f, 0.9998f, 0.9285f, 0.5627f, 0.8678f, 0.9246f, 0.9676f, 1.0000f, 0.8166f, 0.7668f, 0.7686f, 1.0000f, 0.9910f, 0.9926f, 0.8926f, - 1.0000f, 0.9418f, 0.9883f, 0.9807f, 1.0000f, 0.8268f, 0.9601f, 1.0000f, 1.0000f, 0.7274f, 0.9739f, 1.0000f, 1.0000f, 0.8293f, 0.9021f, 0.9943f, - 1.0000f, 0.8941f, 0.9642f, 0.8192f, 1.0000f, 0.9442f, 1.0000f, 1.0000f, 1.0000f, 0.8218f, 1.0000f, 1.0000f, 1.0000f, 0.8919f, 0.9766f, 0.9950f, - 1.0000f, 0.8813f, 0.8560f, 0.9737f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8179f, 0.9567f, 1.0000f, - 1.0000f, 0.8305f, 0.9539f, 0.9408f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9527f, 1.0000f, 1.0000f, - 1.0000f, 0.5424f, 0.9274f, 0.9776f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8125f, 0.9760f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9984f, 0.9854f, 0.8497f, 1.0000f, 0.7888f, 0.8839f, 0.9863f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9623f, 0.9510f, 0.9514f, - 1.0000f, 0.9959f, 0.9649f, 0.9263f, 1.0000f, 0.8387f, 0.9519f, 0.9813f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9828f, 0.9193f, 0.9228f, - 1.0000f, 0.9523f, 0.7872f, 0.8421f, 1.0000f, 0.8805f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7631f, 0.9428f, 1.0000f, - 1.0000f, 0.9222f, 0.9437f, 0.9361f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8761f, 0.9873f, 1.0000f, - 1.0000f, 0.3748f, 0.8243f, 0.9594f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8978f, 1.0000f, 1.0000f, - 1.0000f, 0.7517f, 0.9291f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7935f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8743f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9826f, 0.9802f, 0.9616f, 0.8454f, 0.5666f, 0.8788f, 0.9704f, 0.1105f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9612f, 0.9688f, 0.8011f, 0.4970f, 0.7316f, 0.9870f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6650f, 0.8610f, 0.9403f, 0.8432f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8098f, 0.9149f, 0.9811f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7231f, 0.9162f, 1.0000f, 0.7574f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, 0.9989f, - 1.0000f, 0.8620f, 1.0000f, 1.0000f, 0.1504f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9804f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0481f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9966f, 0.9742f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9839f, - 1.0000f, 0.8949f, 0.9461f, 0.9493f, 1.0000f, 0.6022f, 0.8911f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9999f, - 1.0000f, 0.9834f, 0.9116f, 0.9578f, 1.0000f, 0.8410f, 0.9929f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, - 1.0000f, 0.7164f, 0.9531f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9954f, 0.9563f, - 1.0000f, 0.8580f, 0.9713f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9914f, 0.8280f, - 1.0000f, 0.6422f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9659f, 0.9937f, 0.9848f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9211f, 0.9830f, 0.8399f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9758f, 0.8734f, 0.9199f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9771f, 0.9763f, 0.9701f, - 1.0000f, 1.0000f, 0.9999f, 0.9893f, 0.9123f, 0.9945f, 0.8830f, 0.7704f, 0.9897f, 0.9933f, 0.9728f, 0.8317f, 1.0000f, 0.9986f, 0.9920f, 0.9962f, - 1.0000f, 0.9998f, 0.9999f, 0.9960f, 0.6678f, 0.9634f, 0.8329f, 1.0000f, 1.0000f, 0.9907f, 0.9083f, 1.0000f, 1.0000f, 0.9988f, 0.9992f, 0.8752f, - 1.0000f, 0.9999f, 0.9970f, 0.9318f, 1.0000f, 0.9900f, 0.9387f, 1.0000f, 1.0000f, 0.9973f, 0.9885f, 1.0000f, 1.0000f, 0.9995f, 0.8476f, 0.9502f, - 1.0000f, 0.9981f, 0.9846f, 0.3162f, 1.0000f, 0.6858f, 1.0000f, 1.0000f, 1.0000f, 0.8716f, 1.0000f, 1.0000f, 1.0000f, 0.9932f, 0.7996f, 1.0000f, - 1.0000f, 0.9977f, 0.8796f, 0.2097f, 1.0000f, 0.8640f, 1.0000f, 1.0000f, 1.0000f, 0.9800f, 1.0000f, 1.0000f, 1.0000f, 0.9731f, 0.9097f, 1.0000f, - 1.0000f, 0.9710f, 0.4873f, 0.9844f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7073f, 1.0000f, 1.0000f, - 1.0000f, 0.9818f, 0.4824f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9268f, 1.0000f, 1.0000f, - 1.0000f, 0.9063f, 0.6907f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8256f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9982f, 0.9857f, 1.0000f, 0.9912f, 0.8040f, 0.9880f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9850f, 0.9890f, 0.9234f, - 1.0000f, 0.9940f, 0.9975f, 0.9781f, 1.0000f, 0.9586f, 0.7739f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9913f, 0.9638f, 0.8549f, - 1.0000f, 0.9973f, 0.8963f, 0.2644f, 1.0000f, 0.9672f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.7904f, 1.0000f, - 1.0000f, 0.9609f, 0.6782f, 0.3810f, 1.0000f, 0.6393f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9669f, 1.0000f, 1.0000f, - 1.0000f, 0.9820f, 0.3366f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9822f, 1.0000f, 1.0000f, - 1.0000f, 0.8649f, 0.4629f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7790f, 1.0000f, 1.0000f, - 1.0000f, 0.9778f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.3232f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9980f, 0.9930f, 0.9682f, 0.8570f, 0.9868f, 0.7773f, 1.0000f, 0.7097f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9975f, 0.9575f, 0.7027f, 0.2178f, 0.9014f, 0.9571f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9865f, 0.7187f, 0.6757f, 0.6451f, 0.9168f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 1.0000f, - 1.0000f, 0.9695f, 0.1934f, 1.0000f, 0.2415f, 0.9627f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, - 1.0000f, 0.9662f, 0.6833f, 1.0000f, 0.1314f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9965f, 0.9965f, - 1.0000f, 0.3622f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9990f, 1.0000f, - 1.0000f, 0.8879f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9888f, 0.9994f, - 1.0000f, 0.6148f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9955f, 0.9978f, 0.9953f, - 1.0000f, 0.9963f, 0.9747f, 0.8084f, 1.0000f, 0.9692f, 0.7295f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9972f, - 1.0000f, 0.9824f, 0.9056f, 0.7479f, 1.0000f, 0.8630f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9947f, - 1.0000f, 0.9909f, 0.4321f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9968f, - 1.0000f, 0.8444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9891f, 0.9896f, - 1.0000f, 0.9356f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9976f, 0.9652f, - 1.0000f, 0.1851f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9941f, 0.9882f, 0.5923f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9856f, 0.7536f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 0.8822f, 0.8518f, - 1.0000f, 0.9939f, 0.8934f, 0.6932f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8112f, 1.0000f, 1.0000f, - 1.0000f, 0.9982f, 0.8887f, 0.9475f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9922f, 1.0000f, 1.0000f, - 1.0000f, 0.0739f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5704f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8231f, 0.6883f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6956f, 0.9866f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6364f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 1.0000f, 1.0000f, - 1.0000f, 0.9090f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9905f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9906f, 0.9997f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9946f, 0.9996f, 0.9484f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9971f, 0.9559f, 0.8026f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9598f, 0.9451f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9456f, 0.8508f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8659f, 0.9979f, 1.0000f, - 1.0000f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9991f, 0.9979f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9902f, 0.9998f, 0.9345f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9000f, 0.3559f, 0.8985f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8353f, 0.6303f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5548f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8205f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x10[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6207f, 1.0000f, 0.9776f, 0.9505f, 0.7947f, 1.0000f, 0.9931f, 0.9859f, 0.4908f, 1.0000f, 0.9972f, 0.9997f, - 1.0000f, 1.0000f, 1.0000f, 0.9980f, 0.7324f, 0.9738f, 0.9791f, 0.9809f, 0.7362f, 0.9959f, 0.9913f, 0.9638f, 0.2599f, 1.0000f, 0.9977f, 0.9994f, - 1.0000f, 1.0000f, 0.9957f, 0.9991f, 0.7556f, 0.9800f, 0.9550f, 0.6656f, 0.5596f, 0.9935f, 0.9709f, 0.7168f, 0.8272f, 1.0000f, 0.9874f, 0.9291f, - 1.0000f, 1.0000f, 0.9961f, 0.9844f, 0.8027f, 0.9422f, 0.9112f, 0.6472f, 0.4826f, 0.9821f, 0.9340f, 0.6631f, 1.0000f, 0.9882f, 0.9592f, 0.9172f, - 1.0000f, 1.0000f, 0.9898f, 0.9717f, 0.7343f, 0.9572f, 0.7065f, 0.6914f, 0.8340f, 0.9606f, 0.7208f, 0.6299f, 1.0000f, 0.9833f, 0.9493f, 0.5704f, - 1.0000f, 0.9922f, 0.9852f, 0.9539f, 0.2317f, 0.9211f, 0.3620f, 0.8999f, 1.0000f, 0.9194f, 0.3453f, 0.9068f, 1.0000f, 0.9687f, 0.8601f, 0.5523f, - 1.0000f, 1.0000f, 0.9535f, 0.9074f, 0.1950f, 0.8782f, 0.0978f, 1.0000f, 1.0000f, 0.9386f, 0.1450f, 1.0000f, 1.0000f, 0.9489f, 0.5947f, 0.3046f, - 1.0000f, 0.9760f, 0.8551f, 0.5845f, 0.6847f, 0.6047f, 0.6176f, 1.0000f, 1.0000f, 0.6731f, 0.7147f, 1.0000f, 1.0000f, 0.9020f, 0.4045f, 0.6500f, - 1.0000f, 1.0000f, 1.0000f, 0.9963f, 1.0000f, 1.0000f, 0.9666f, 0.9296f, 1.0000f, 1.0000f, 0.9856f, 0.9395f, 1.0000f, 1.0000f, 0.9986f, 0.9972f, - 1.0000f, 1.0000f, 0.9960f, 0.9991f, 1.0000f, 0.9589f, 0.9452f, 0.9148f, 1.0000f, 0.9876f, 0.9881f, 0.9061f, 1.0000f, 1.0000f, 0.9956f, 0.9728f, - 1.0000f, 1.0000f, 0.9905f, 0.9837f, 1.0000f, 0.9582f, 0.8941f, 0.6527f, 1.0000f, 0.9733f, 0.9448f, 0.6754f, 1.0000f, 1.0000f, 0.9798f, 0.9528f, - 1.0000f, 0.9871f, 0.9947f, 0.8712f, 1.0000f, 0.9404f, 0.6358f, 0.7106f, 1.0000f, 0.9579f, 0.6605f, 0.6329f, 1.0000f, 0.9813f, 0.9817f, 0.6824f, - 1.0000f, 1.0000f, 0.9750f, 0.9200f, 1.0000f, 0.9532f, 0.4742f, 0.8629f, 1.0000f, 0.9565f, 0.4614f, 0.8620f, 1.0000f, 0.9893f, 0.8948f, 0.5913f, - 1.0000f, 0.9755f, 0.8738f, 0.6144f, 1.0000f, 0.8000f, 0.5739f, 1.0000f, 1.0000f, 0.8307f, 0.3565f, 1.0000f, 1.0000f, 0.9250f, 0.5486f, 0.7470f, - 1.0000f, 0.9787f, 0.8919f, 0.5375f, 1.0000f, 0.7488f, 0.4148f, 1.0000f, 1.0000f, 0.8373f, 0.3164f, 1.0000f, 1.0000f, 0.9265f, 0.4524f, 0.6681f, - 1.0000f, 0.9426f, 0.6980f, 0.2734f, 1.0000f, 0.2862f, 1.0000f, 1.0000f, 1.0000f, 0.4388f, 1.0000f, 1.0000f, 1.0000f, 0.8190f, 0.5560f, 1.0000f, - 1.0000f, 1.0000f, 0.9879f, 0.9975f, 0.9118f, 1.0000f, 0.9501f, 0.9270f, 0.1541f, 1.0000f, 0.7790f, 0.8730f, 0.5300f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9951f, 0.9973f, 0.6801f, 0.9648f, 0.8591f, 0.6778f, 0.1875f, 0.7849f, 0.8284f, 0.6936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9767f, 0.8903f, 0.8226f, 0.9730f, 0.7505f, 0.5810f, 0.0442f, 0.9047f, 0.7700f, 0.5262f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9863f, 0.9714f, 0.8880f, 0.3340f, 0.9609f, 0.6387f, 0.8166f, 1.0000f, 0.8864f, 0.7044f, 0.7267f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9896f, 0.9485f, 0.4950f, 0.8395f, 0.9034f, 0.3675f, 1.0000f, 0.3782f, 0.8541f, 0.2667f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9430f, 0.8703f, 0.4867f, 0.0177f, 0.7188f, 0.8014f, 1.0000f, 1.0000f, 0.7961f, 0.7685f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.9286f, 0.5109f, 0.2245f, 0.7654f, 0.8521f, 1.0000f, 1.0000f, 1.0000f, 0.8066f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, - 1.0000f, 0.9013f, 0.2798f, 0.4990f, 0.0771f, 0.1074f, 1.0000f, 1.0000f, 1.0000f, 0.2099f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9999f, - 1.0000f, 1.0000f, 0.9887f, 0.9901f, 0.4295f, 1.0000f, 0.8469f, 0.8078f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9740f, 0.9802f, 0.6870f, 0.9516f, 0.8848f, 0.4658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9557f, 0.9260f, 0.3396f, 0.9228f, 0.5632f, 0.7023f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, - 1.0000f, 0.9561f, 0.9568f, 0.5225f, 0.5148f, 0.9223f, 0.4342f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, - 1.0000f, 0.9866f, 0.8561f, 0.4247f, 1.0000f, 0.8459f, 0.6014f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9916f, 0.9994f, - 1.0000f, 0.8832f, 0.3993f, 0.7286f, 1.0000f, 0.6079f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9932f, - 1.0000f, 0.9160f, 0.2025f, 0.4097f, 1.0000f, 0.7001f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9941f, 0.9915f, - 1.0000f, 0.7606f, 0.3282f, 1.0000f, 1.0000f, 0.3728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9827f, 0.9962f, 0.9054f, - 1.0000f, 0.9994f, 0.9997f, 0.9988f, 0.4479f, 0.9473f, 0.8840f, 0.9575f, 0.9460f, 0.9481f, 0.9041f, 0.9400f, 1.0000f, 0.9936f, 0.9979f, 0.9869f, - 1.0000f, 0.9998f, 0.9998f, 0.9391f, 0.5775f, 0.8790f, 0.9301f, 0.9743f, 1.0000f, 0.9177f, 0.9554f, 0.9825f, 1.0000f, 0.9911f, 0.9929f, 0.9087f, - 1.0000f, 0.9335f, 0.9672f, 0.9654f, 1.0000f, 0.8438f, 0.9619f, 1.0000f, 1.0000f, 0.8129f, 0.9657f, 1.0000f, 1.0000f, 0.7761f, 0.8934f, 0.9925f, - 1.0000f, 0.8956f, 0.9596f, 0.7877f, 1.0000f, 0.9359f, 1.0000f, 1.0000f, 1.0000f, 0.9439f, 1.0000f, 1.0000f, 1.0000f, 0.8872f, 0.9622f, 0.9963f, - 1.0000f, 0.8970f, 0.8648f, 0.9684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7820f, 0.9435f, 1.0000f, - 1.0000f, 0.8249f, 0.9632f, 0.9735f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9100f, 1.0000f, 1.0000f, - 1.0000f, 0.3889f, 0.8531f, 0.9701f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7589f, 0.9363f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9986f, 0.9757f, 0.8815f, 1.0000f, 0.8153f, 0.8807f, 0.9854f, 1.0000f, 0.7987f, 0.7905f, 0.9136f, 1.0000f, 0.9651f, 0.9782f, 0.9848f, - 1.0000f, 0.9990f, 0.9720f, 0.9330f, 1.0000f, 0.8823f, 0.9669f, 0.9914f, 1.0000f, 0.8318f, 0.8685f, 0.9939f, 1.0000f, 0.9902f, 0.9612f, 0.9769f, - 1.0000f, 0.9354f, 0.7522f, 0.8104f, 1.0000f, 0.9106f, 1.0000f, 1.0000f, 1.0000f, 0.8721f, 1.0000f, 1.0000f, 1.0000f, 0.8261f, 0.9771f, 1.0000f, - 1.0000f, 0.9081f, 0.9234f, 0.9183f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9306f, 0.9937f, 1.0000f, - 1.0000f, 0.3941f, 0.8581f, 0.9806f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9349f, 1.0000f, 1.0000f, - 1.0000f, 0.7669f, 0.9586f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7776f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8406f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9892f, 0.9839f, 0.9689f, 0.8773f, 0.6112f, 0.8610f, 0.9811f, 0.1716f, 0.5980f, 0.6238f, 0.9796f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9635f, 0.9762f, 0.8237f, 0.5187f, 0.7399f, 0.9753f, 1.0000f, 1.0000f, 0.6415f, 0.8449f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.6706f, 0.8765f, 0.9497f, 0.8511f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, - 1.0000f, 0.8141f, 0.9166f, 0.9857f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.7731f, 0.9543f, 1.0000f, 0.7228f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9998f, 0.9996f, - 1.0000f, 0.8978f, 1.0000f, 1.0000f, 0.1169f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9675f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.0559f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9953f, 0.9512f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9868f, 0.9712f, - 1.0000f, 0.9316f, 0.9456f, 0.9603f, 1.0000f, 0.6553f, 0.9124f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, - 1.0000f, 0.9846f, 0.9239f, 0.9692f, 1.0000f, 0.8384f, 0.9969f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, - 1.0000f, 0.7380f, 0.9443f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9924f, 0.9377f, - 1.0000f, 0.8896f, 0.9823f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 0.9927f, 0.7805f, - 1.0000f, 0.7863f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9722f, 0.9835f, 0.9841f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9368f, 0.9725f, 0.8501f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9663f, 0.8040f, 0.8667f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9520f, 0.9372f, 0.9629f, - 1.0000f, 1.0000f, 0.9987f, 0.9999f, 0.9477f, 0.9923f, 0.9154f, 0.7834f, 0.9982f, 0.9920f, 0.9794f, 0.8363f, 1.0000f, 0.9982f, 0.9948f, 0.9899f, - 1.0000f, 0.9990f, 0.9975f, 1.0000f, 0.7086f, 0.9819f, 0.8756f, 1.0000f, 1.0000f, 0.9967f, 0.9189f, 1.0000f, 1.0000f, 0.9971f, 0.9960f, 0.8676f, - 1.0000f, 0.9981f, 0.9509f, 0.9413f, 1.0000f, 0.9842f, 0.9468f, 1.0000f, 1.0000f, 0.9946f, 0.9909f, 1.0000f, 1.0000f, 0.9944f, 0.8116f, 0.9130f, - 1.0000f, 0.9976f, 0.9907f, 0.2986f, 1.0000f, 0.7573f, 1.0000f, 1.0000f, 1.0000f, 0.8490f, 1.0000f, 1.0000f, 1.0000f, 0.9952f, 0.5879f, 1.0000f, - 1.0000f, 0.9978f, 0.8963f, 0.1264f, 1.0000f, 0.8747f, 1.0000f, 1.0000f, 1.0000f, 0.9681f, 1.0000f, 1.0000f, 1.0000f, 0.9660f, 0.8427f, 1.0000f, - 1.0000f, 0.9895f, 0.5337f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5668f, 1.0000f, 1.0000f, - 1.0000f, 0.9918f, 0.2925f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9094f, 1.0000f, 1.0000f, - 1.0000f, 0.8639f, 0.4569f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6444f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9985f, 0.9940f, 1.0000f, 0.9973f, 0.8351f, 0.9903f, 1.0000f, 0.9980f, 0.9326f, 0.9989f, 1.0000f, 1.0000f, 0.9933f, 0.8985f, - 1.0000f, 0.9943f, 0.9955f, 0.9873f, 1.0000f, 0.9745f, 0.7746f, 1.0000f, 1.0000f, 0.9938f, 0.9006f, 1.0000f, 1.0000f, 0.9945f, 0.9789f, 0.7974f, - 1.0000f, 0.9850f, 0.9524f, 0.2388f, 1.0000f, 0.9599f, 1.0000f, 1.0000f, 1.0000f, 0.9942f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.7127f, 1.0000f, - 1.0000f, 0.9773f, 0.7247f, 0.4198f, 1.0000f, 0.6892f, 1.0000f, 1.0000f, 1.0000f, 0.8571f, 1.0000f, 1.0000f, 1.0000f, 0.9382f, 1.0000f, 1.0000f, - 1.0000f, 0.9884f, 0.2172f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9642f, 1.0000f, 1.0000f, - 1.0000f, 0.8480f, 0.4434f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6269f, 1.0000f, 1.0000f, - 1.0000f, 0.9311f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2529f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9999f, 0.9981f, 0.9645f, 0.8911f, 0.9990f, 0.7919f, 1.0000f, 0.5031f, 0.9778f, 0.8992f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9970f, 0.9886f, 0.8417f, 0.0322f, 0.9281f, 0.9678f, 1.0000f, 1.0000f, 0.9706f, 0.9889f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9983f, - 1.0000f, 0.9950f, 0.7891f, 0.9206f, 0.7435f, 0.9142f, 1.0000f, 1.0000f, 1.0000f, 0.9861f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 1.0000f, - 1.0000f, 0.9780f, 0.2459f, 1.0000f, 0.0876f, 0.9764f, 1.0000f, 1.0000f, 1.0000f, 0.9928f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9815f, 0.7453f, 1.0000f, 0.1796f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 0.9993f, - 1.0000f, 0.5449f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 1.0000f, 0.9995f, - 1.0000f, 0.8888f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9974f, 1.0000f, - 1.0000f, 0.3223f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9991f, 0.9878f, - 1.0000f, 0.9952f, 0.9695f, 0.8856f, 1.0000f, 0.9703f, 0.8091f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9964f, - 1.0000f, 0.9864f, 0.9275f, 0.7715f, 1.0000f, 0.8799f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9983f, - 1.0000f, 0.9785f, 0.4700f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9949f, 0.9910f, - 1.0000f, 0.8658f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9977f, - 1.0000f, 0.9626f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9970f, 0.9804f, - 1.0000f, 0.3836f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9954f, 0.9930f, 0.5070f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9546f, 0.5412f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9831f, 0.8202f, 0.7305f, - 1.0000f, 0.9993f, 0.9321f, 0.7622f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7638f, 1.0000f, 1.0000f, - 1.0000f, 0.9906f, 0.9217f, 0.9698f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9345f, 1.0000f, 1.0000f, - 1.0000f, 0.0665f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.4784f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8694f, 0.8214f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, 1.0000f, - 1.0000f, 0.8295f, 0.9829f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7417f, 1.0000f, 1.0000f, 0.8178f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 1.0000f, 0.9996f, - 1.0000f, 0.9255f, 1.0000f, 1.0000f, 0.1357f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, 0.9965f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1629f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9919f, 0.9996f, 0.9966f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9967f, 0.9984f, 0.9616f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, 0.9464f, 0.7933f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9417f, 0.9748f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9027f, 0.8053f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8329f, 0.9992f, 1.0000f, - 1.0000f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, 1.0000f, 0.9984f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 1.0000f, 0.9409f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8926f, 0.3509f, 0.9244f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7540f, 0.6579f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3105f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6958f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; - -const float percentile_table_12x12[2048] = { - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6459f, 1.0000f, 0.9751f, 0.9422f, 0.8309f, 1.0000f, 0.9931f, 0.9934f, 0.5189f, 1.0000f, 0.9984f, 0.9983f, - 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.8017f, 0.9895f, 0.9892f, 0.9881f, 0.7254f, 0.9869f, 0.9932f, 0.9621f, 0.2345f, 1.0000f, 0.9979f, 0.9988f, - 1.0000f, 1.0000f, 0.9882f, 0.9996f, 0.7782f, 0.9961f, 0.9574f, 0.7435f, 0.5997f, 0.9965f, 0.9644f, 0.7057f, 0.8481f, 1.0000f, 0.9914f, 0.9132f, - 1.0000f, 1.0000f, 0.9990f, 0.9919f, 0.8255f, 0.9789f, 0.9227f, 0.6933f, 0.5375f, 0.9716f, 0.9361f, 0.6779f, 1.0000f, 0.9858f, 0.9781f, 0.9066f, - 1.0000f, 1.0000f, 0.9834f, 0.9545f, 0.7662f, 0.9394f, 0.6975f, 0.7216f, 0.8351f, 0.9755f, 0.7097f, 0.6513f, 1.0000f, 0.9863f, 0.9178f, 0.4660f, - 1.0000f, 0.9725f, 0.9933f, 0.9434f, 0.2471f, 0.9189f, 0.3612f, 0.9206f, 1.0000f, 0.9402f, 0.3160f, 0.9155f, 1.0000f, 0.9609f, 0.8537f, 0.4868f, - 1.0000f, 1.0000f, 0.9701f, 0.8698f, 0.3053f, 0.8945f, 0.1802f, 1.0000f, 1.0000f, 0.9097f, 0.1414f, 1.0000f, 1.0000f, 0.9735f, 0.5781f, 0.2594f, - 1.0000f, 0.9799f, 0.8628f, 0.5549f, 0.7235f, 0.6824f, 0.6996f, 1.0000f, 1.0000f, 0.6566f, 0.6802f, 1.0000f, 1.0000f, 0.9138f, 0.3107f, 0.6348f, - 1.0000f, 1.0000f, 1.0000f, 0.9960f, 0.7016f, 1.0000f, 0.9711f, 0.9184f, 0.8830f, 1.0000f, 0.9853f, 0.9378f, 0.6710f, 1.0000f, 0.9975f, 0.9957f, - 1.0000f, 1.0000f, 0.9975f, 0.9999f, 0.8723f, 0.9875f, 0.9486f, 0.9398f, 0.8210f, 0.9647f, 0.9783f, 0.9021f, 0.5149f, 1.0000f, 0.9980f, 0.9926f, - 1.0000f, 1.0000f, 0.9947f, 0.9888f, 0.8966f, 0.9873f, 0.9252f, 0.7117f, 0.6615f, 0.9718f, 0.9475f, 0.7137f, 0.8952f, 1.0000f, 0.9596f, 0.9339f, - 1.0000f, 0.9923f, 0.9870f, 0.8878f, 0.8637f, 0.9464f, 0.6891f, 0.7469f, 0.4040f, 0.9507f, 0.6756f, 0.6869f, 1.0000f, 0.9830f, 0.9704f, 0.5812f, - 1.0000f, 1.0000f, 0.9593f, 0.9167f, 0.8068f, 0.9558f, 0.5302f, 0.8806f, 0.8233f, 0.9825f, 0.4531f, 0.8915f, 1.0000f, 0.9739f, 0.8443f, 0.3467f, - 1.0000f, 0.9836f, 0.8757f, 0.5651f, 0.2714f, 0.7977f, 0.6291f, 1.0000f, 1.0000f, 0.8116f, 0.4268f, 1.0000f, 1.0000f, 0.9109f, 0.3660f, 0.6540f, - 1.0000f, 0.9921f, 0.9172f, 0.5515f, 0.5480f, 0.7737f, 0.5446f, 1.0000f, 1.0000f, 0.8341f, 0.3317f, 1.0000f, 1.0000f, 0.9161f, 0.2772f, 0.5265f, - 1.0000f, 0.9531f, 0.7273f, 0.2944f, 0.7452f, 0.4618f, 1.0000f, 1.0000f, 1.0000f, 0.3755f, 1.0000f, 1.0000f, 1.0000f, 0.8092f, 0.3851f, 1.0000f, - 1.0000f, 1.0000f, 0.9887f, 0.9982f, 0.9281f, 1.0000f, 0.9658f, 0.9352f, 0.1329f, 1.0000f, 0.9864f, 0.9410f, 0.5030f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9988f, 0.9984f, 0.6954f, 0.9479f, 0.8814f, 0.7157f, 0.2014f, 0.9811f, 0.9453f, 0.7796f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9809f, 0.9084f, 0.8663f, 0.9661f, 0.8030f, 0.6263f, 0.0965f, 0.9913f, 0.7895f, 0.5717f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9680f, 0.9757f, 0.9059f, 0.3993f, 0.9748f, 0.6663f, 0.8592f, 1.0000f, 0.9688f, 0.6912f, 0.7937f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9706f, 0.9639f, 0.5227f, 0.8500f, 0.9247f, 0.3368f, 1.0000f, 0.4827f, 0.9335f, 0.1946f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9561f, 0.8619f, 0.4909f, 0.0437f, 0.7752f, 0.8518f, 1.0000f, 1.0000f, 0.7347f, 0.7767f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.9677f, 0.5749f, 0.2886f, 0.7291f, 0.8372f, 1.0000f, 1.0000f, 1.0000f, 0.8681f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9996f, - 1.0000f, 0.9313f, 0.3265f, 0.6234f, 0.1063f, 0.2147f, 1.0000f, 1.0000f, 1.0000f, 0.1153f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9978f, 1.0000f, - 1.0000f, 1.0000f, 0.9856f, 0.9986f, 0.4990f, 1.0000f, 0.8413f, 0.8128f, 1.0000f, 1.0000f, 0.9348f, 0.8452f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9954f, 0.9803f, 0.4949f, 0.9457f, 0.9257f, 0.5339f, 1.0000f, 0.9872f, 0.9691f, 0.6591f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9721f, 0.9272f, 0.4087f, 0.9641f, 0.5875f, 0.7631f, 1.0000f, 0.9535f, 0.5843f, 0.6687f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, - 1.0000f, 0.9723f, 0.9445f, 0.6147f, 0.6206f, 0.8789f, 0.4703f, 1.0000f, 1.0000f, 0.9149f, 0.4444f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9890f, - 1.0000f, 0.9841f, 0.8610f, 0.4313f, 1.0000f, 0.8176f, 0.6117f, 1.0000f, 1.0000f, 0.8740f, 0.2408f, 1.0000f, 1.0000f, 1.0000f, 0.9986f, 0.9985f, - 1.0000f, 0.8994f, 0.4178f, 0.7486f, 1.0000f, 0.6431f, 1.0000f, 1.0000f, 1.0000f, 0.6404f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 0.9916f, - 1.0000f, 0.9300f, 0.2829f, 0.5584f, 1.0000f, 0.7417f, 1.0000f, 1.0000f, 1.0000f, 0.8298f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9963f, 0.9964f, - 1.0000f, 0.8244f, 0.4575f, 1.0000f, 1.0000f, 0.4744f, 1.0000f, 1.0000f, 1.0000f, 0.2214f, 1.0000f, 1.0000f, 1.0000f, 0.9944f, 0.9900f, 0.8838f, - 1.0000f, 0.9996f, 0.9997f, 0.9984f, 0.4786f, 0.9295f, 0.8862f, 0.9633f, 0.9490f, 0.9672f, 0.9200f, 0.9730f, 1.0000f, 0.9956f, 0.9946f, 0.9742f, - 1.0000f, 0.9999f, 0.9991f, 0.9603f, 0.5967f, 0.8870f, 0.9430f, 0.9650f, 1.0000f, 0.9322f, 0.9555f, 0.9787f, 1.0000f, 0.9815f, 0.9866f, 0.8893f, - 1.0000f, 0.9482f, 0.9813f, 0.9524f, 1.0000f, 0.8715f, 0.9805f, 1.0000f, 1.0000f, 0.8773f, 0.9766f, 1.0000f, 1.0000f, 0.7196f, 0.8930f, 0.9764f, - 1.0000f, 0.8959f, 0.9606f, 0.7825f, 1.0000f, 0.9460f, 1.0000f, 1.0000f, 1.0000f, 0.9630f, 1.0000f, 1.0000f, 1.0000f, 0.8383f, 0.9521f, 0.9948f, - 1.0000f, 0.8490f, 0.8140f, 0.9565f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7502f, 0.9386f, 1.0000f, - 1.0000f, 0.7810f, 0.9382f, 0.9636f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8320f, 1.0000f, 1.0000f, - 1.0000f, 0.2999f, 0.8646f, 0.9777f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7600f, 0.9696f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9997f, 0.9850f, 0.9027f, 0.5618f, 0.8055f, 0.9001f, 0.9974f, 0.9685f, 0.8509f, 0.8556f, 0.9959f, 1.0000f, 0.9441f, 0.9774f, 0.9762f, - 1.0000f, 0.9965f, 0.9693f, 0.9242f, 0.6376f, 0.8423f, 0.9666f, 0.9922f, 1.0000f, 0.8987f, 0.9669f, 0.9991f, 1.0000f, 0.9938f, 0.9528f, 0.9891f, - 1.0000f, 0.9612f, 0.7923f, 0.8574f, 1.0000f, 0.9237f, 1.0000f, 1.0000f, 1.0000f, 0.9344f, 1.0000f, 1.0000f, 1.0000f, 0.7535f, 0.9768f, 1.0000f, - 1.0000f, 0.9449f, 0.9374f, 0.9517f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9040f, 0.9955f, 1.0000f, - 1.0000f, 0.3213f, 0.8923f, 0.9708f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8765f, 1.0000f, 1.0000f, - 1.0000f, 0.7552f, 0.9548f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7839f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8706f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9918f, 0.9807f, 0.9627f, 0.8546f, 0.6176f, 0.8781f, 0.9819f, 0.0866f, 0.6733f, 0.8288f, 0.9937f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9744f, 0.9737f, 0.8199f, 0.3803f, 0.7519f, 0.9930f, 1.0000f, 1.0000f, 0.7615f, 0.9885f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.7365f, 0.9090f, 0.9793f, 0.8277f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8528f, 0.9568f, 0.9837f, 0.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9990f, 0.9999f, - 1.0000f, 0.7722f, 0.9414f, 1.0000f, 0.6847f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9977f, 0.9969f, - 1.0000f, 0.8885f, 1.0000f, 1.0000f, 0.0762f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 0.9624f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1243f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9920f, 0.9276f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9827f, 0.9500f, - 1.0000f, 0.9304f, 0.9600f, 0.9571f, 1.0000f, 0.6639f, 0.9078f, 1.0000f, 1.0000f, 0.7310f, 0.8980f, 1.0000f, 1.0000f, 1.0000f, 0.9989f, 1.0000f, - 1.0000f, 0.9896f, 0.9211f, 0.9675f, 1.0000f, 0.8362f, 0.9969f, 1.0000f, 1.0000f, 0.8004f, 0.9848f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, - 1.0000f, 0.7853f, 0.9779f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9941f, 0.9331f, - 1.0000f, 0.9120f, 0.9894f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9999f, 0.9899f, 0.7867f, - 1.0000f, 0.8222f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9365f, 0.9746f, 0.9653f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9034f, 0.9538f, 0.7964f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9581f, 0.8043f, 0.8462f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9357f, 0.9493f, 0.9390f, - 1.0000f, 1.0000f, 0.9983f, 0.9987f, 0.9103f, 0.9995f, 0.9426f, 0.8973f, 0.9978f, 0.9971f, 0.9839f, 0.8152f, 1.0000f, 0.9945f, 0.9970f, 0.9795f, - 1.0000f, 0.9991f, 0.9998f, 0.9973f, 0.7647f, 0.9728f, 0.9114f, 1.0000f, 1.0000f, 0.9976f, 0.9216f, 0.9999f, 1.0000f, 0.9981f, 0.9844f, 0.8164f, - 1.0000f, 0.9999f, 0.9828f, 0.9541f, 1.0000f, 0.9797f, 0.9664f, 1.0000f, 1.0000f, 0.9785f, 0.9590f, 1.0000f, 1.0000f, 0.9980f, 0.7568f, 0.8854f, - 1.0000f, 0.9843f, 0.9832f, 0.4132f, 1.0000f, 0.8187f, 1.0000f, 1.0000f, 1.0000f, 0.8393f, 1.0000f, 1.0000f, 1.0000f, 0.9683f, 0.4401f, 1.0000f, - 1.0000f, 0.9952f, 0.8565f, 0.0654f, 1.0000f, 0.9144f, 1.0000f, 1.0000f, 1.0000f, 0.9126f, 1.0000f, 1.0000f, 1.0000f, 0.9867f, 0.7177f, 1.0000f, - 1.0000f, 0.9884f, 0.2654f, 0.9999f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.5906f, 1.0000f, 1.0000f, - 1.0000f, 0.9927f, 0.1728f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8672f, 1.0000f, 1.0000f, - 1.0000f, 0.8731f, 0.5070f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.3946f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 0.9986f, 1.0000f, 0.9929f, 0.9979f, 0.8655f, 0.9861f, 0.9990f, 0.9902f, 0.9262f, 0.9732f, 1.0000f, 0.9972f, 0.9943f, 0.8583f, - 1.0000f, 0.9939f, 0.9846f, 0.9698f, 0.9047f, 0.9772f, 0.7991f, 1.0000f, 1.0000f, 0.9948f, 0.8403f, 1.0000f, 1.0000f, 0.9967f, 0.9878f, 0.7328f, - 1.0000f, 0.9942f, 0.9587f, 0.3898f, 1.0000f, 0.9791f, 1.0000f, 1.0000f, 1.0000f, 0.9851f, 1.0000f, 1.0000f, 1.0000f, 0.9958f, 0.6087f, 1.0000f, - 1.0000f, 0.9406f, 0.7707f, 0.5410f, 1.0000f, 0.7881f, 1.0000f, 1.0000f, 1.0000f, 0.7077f, 1.0000f, 1.0000f, 1.0000f, 0.9286f, 1.0000f, 1.0000f, - 1.0000f, 0.9910f, 0.2280f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9511f, 1.0000f, 1.0000f, - 1.0000f, 0.8601f, 0.4223f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.2533f, 1.0000f, 1.0000f, - 1.0000f, 0.9821f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.2080f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9959f, 0.9713f, 0.9753f, 0.8471f, 0.9879f, 0.8331f, 1.0000f, 0.3708f, 0.9967f, 0.9014f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9966f, 0.9877f, 0.8689f, 0.0321f, 0.9577f, 0.9770f, 1.0000f, 1.0000f, 0.9817f, 0.9940f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9992f, - 1.0000f, 0.9928f, 0.8080f, 0.9497f, 0.7037f, 0.9222f, 1.0000f, 1.0000f, 1.0000f, 0.9859f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9615f, 0.5110f, 1.0000f, 0.0206f, 0.9907f, 1.0000f, 1.0000f, 1.0000f, 0.9801f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9973f, 0.9992f, - 1.0000f, 0.9369f, 0.7400f, 1.0000f, 0.1654f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9991f, 0.9962f, - 1.0000f, 0.5684f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 0.9909f, 1.0000f, - 1.0000f, 0.8938f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9968f, 0.9992f, - 1.0000f, 0.4488f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9987f, 0.9955f, 0.9898f, - 1.0000f, 0.9936f, 0.9855f, 0.8900f, 1.0000f, 0.9584f, 0.7950f, 1.0000f, 1.0000f, 0.9953f, 0.9072f, 1.0000f, 1.0000f, 1.0000f, 0.9997f, 1.0000f, - 1.0000f, 0.9905f, 0.9655f, 0.8266f, 1.0000f, 0.9232f, 1.0000f, 1.0000f, 1.0000f, 0.9504f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.9981f, 0.6320f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9998f, 1.0000f, - 1.0000f, 0.9008f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9823f, 0.9618f, - 1.0000f, 0.9195f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9988f, 0.9935f, 0.9468f, - 1.0000f, 0.3515f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9915f, 0.9904f, 0.3418f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9985f, 0.9471f, 0.4357f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9924f, 0.7909f, 0.6057f, - 1.0000f, 0.9971f, 0.9903f, 0.8822f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.7382f, 1.0000f, 1.0000f, - 1.0000f, 0.9989f, 0.9912f, 0.9759f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9326f, 1.0000f, 1.0000f, - 1.0000f, 0.1874f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.5936f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.9290f, 0.8748f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8104f, 0.9963f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 0.8433f, 1.0000f, 1.0000f, 0.7692f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, - 1.0000f, 0.9514f, 1.0000f, 1.0000f, 0.0546f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9993f, 1.0000f, 0.9949f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1497f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9976f, 0.9950f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, 0.9994f, 0.9418f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9908f, 0.9309f, 0.7584f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9951f, 0.9267f, 0.9318f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9437f, 0.7677f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8797f, 0.9994f, 1.0000f, - 1.0000f, 0.9982f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9994f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9995f, 0.9995f, 0.9551f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.9053f, 0.3564f, 0.8908f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.8846f, 0.6486f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.1577f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 0.6027f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, -}; diff --git a/3rdparty/bimg/3rdparty/astc/astc_pick_best_endpoint_format.cpp b/3rdparty/bimg/3rdparty/astc/astc_pick_best_endpoint_format.cpp deleted file mode 100644 index 5c6360ddc91..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_pick_best_endpoint_format.cpp +++ /dev/null @@ -1,938 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions to pick the best ASTC endpoint format for a given block. - */ -/*----------------------------------------------------------------------------*/ -#include "astc_codec_internals.h" - -#ifdef DEBUG_PRINT_DIAGNOSTICS - #include <stdio.h> -#endif - -#include <math.h> - -/* - functions to determine, for a given partitioning, which color endpoint formats are the best to use. - - */ - - -// for a given partition, compute for every (integer-component-count, quantization-level) -// the color error. - - -static void compute_color_error_for_every_integer_count_and_quantization_level(int encode_hdr_rgb, // 1 = perform HDR encoding, 0 = perform LDR encoding. - int encode_hdr_alpha, int partition_index, const partition_info * pi, - const encoding_choice_errors * eci, // pointer to the structure for the CURRENT partition. - const endpoints * ep, float4 error_weightings[4], - // arrays to return results back through. - float best_error[21][4], int format_of_choice[21][4]) -{ - int i, j; - int partition_size = pi->texels_per_partition[partition_index]; - - static const float baseline_quant_error[21] = { - (65536.0f * 65536.0f / 18.0f), // 2 values, 1 step - (65536.0f * 65536.0f / 18.0f) / (2 * 2), // 3 values, 2 steps - (65536.0f * 65536.0f / 18.0f) / (3 * 3), // 4 values, 3 steps - (65536.0f * 65536.0f / 18.0f) / (4 * 4), // 5 values - (65536.0f * 65536.0f / 18.0f) / (5 * 5), - (65536.0f * 65536.0f / 18.0f) / (7 * 7), - (65536.0f * 65536.0f / 18.0f) / (9 * 9), - (65536.0f * 65536.0f / 18.0f) / (11 * 11), - (65536.0f * 65536.0f / 18.0f) / (15 * 15), - (65536.0f * 65536.0f / 18.0f) / (19 * 19), - (65536.0f * 65536.0f / 18.0f) / (23 * 23), - (65536.0f * 65536.0f / 18.0f) / (31 * 31), - (65536.0f * 65536.0f / 18.0f) / (39 * 39), - (65536.0f * 65536.0f / 18.0f) / (47 * 47), - (65536.0f * 65536.0f / 18.0f) / (63 * 63), - (65536.0f * 65536.0f / 18.0f) / (79 * 79), - (65536.0f * 65536.0f / 18.0f) / (95 * 95), - (65536.0f * 65536.0f / 18.0f) / (127 * 127), - (65536.0f * 65536.0f / 18.0f) / (159 * 159), - (65536.0f * 65536.0f / 18.0f) / (191 * 191), - (65536.0f * 65536.0f / 18.0f) / (255 * 255) - }; - - float4 ep0 = ep->endpt0[partition_index]; - float4 ep1 = ep->endpt1[partition_index]; - - float ep0_max = MAX(MAX(ep0.x, ep0.y), ep0.z); - float ep0_min = MIN(MIN(ep0.x, ep0.y), ep0.z); - float ep1_max = MAX(MAX(ep1.x, ep1.y), ep1.z); - float ep1_min = MIN(MIN(ep1.x, ep1.y), ep1.z); - - ep0_min = MAX(ep0_min, 0.0f); - ep1_min = MAX(ep1_min, 0.0f); - ep0_max = MAX(ep0_max, 1e-10f); - ep1_max = MAX(ep1_max, 1e-10f); - - float4 error_weight = error_weightings[partition_index]; - - float error_weight_rgbsum = error_weight.x + error_weight.y + error_weight.z; - - float range_upper_limit_rgb = encode_hdr_rgb ? 61440.0f : 65535.0f; - float range_upper_limit_alpha = encode_hdr_alpha ? 61440.0f : 65535.0f; - - // it is possible to get endpoint colors significantly outside [0,upper-limit] - // even if the input data are safely contained in [0,upper-limit]; - // we need to add an error term for this situation, - float4 ep0_range_error_high; - float4 ep1_range_error_high; - float4 ep0_range_error_low; - float4 ep1_range_error_low; - - ep0_range_error_high.x = MAX(0.0f, ep0.x - range_upper_limit_rgb); - ep0_range_error_high.y = MAX(0.0f, ep0.y - range_upper_limit_rgb); - ep0_range_error_high.z = MAX(0.0f, ep0.z - range_upper_limit_rgb); - ep0_range_error_high.w = MAX(0.0f, ep0.w - range_upper_limit_alpha); - ep1_range_error_high.x = MAX(0.0f, ep1.x - range_upper_limit_rgb); - ep1_range_error_high.y = MAX(0.0f, ep1.y - range_upper_limit_rgb); - ep1_range_error_high.z = MAX(0.0f, ep1.z - range_upper_limit_rgb); - ep1_range_error_high.w = MAX(0.0f, ep1.w - range_upper_limit_alpha); - - ep0_range_error_low.x = MIN(0.0f, ep0.x); - ep0_range_error_low.y = MIN(0.0f, ep0.y); - ep0_range_error_low.z = MIN(0.0f, ep0.z); - ep0_range_error_low.w = MIN(0.0f, ep0.w); - ep1_range_error_low.x = MIN(0.0f, ep1.x); - ep1_range_error_low.y = MIN(0.0f, ep1.y); - ep1_range_error_low.z = MIN(0.0f, ep1.z); - ep1_range_error_low.w = MIN(0.0f, ep1.w); - - float4 sum_range_error = - (ep0_range_error_low * ep0_range_error_low) + (ep1_range_error_low * ep1_range_error_low) + (ep0_range_error_high * ep0_range_error_high) + (ep1_range_error_high * ep1_range_error_high); - float rgb_range_error = dot(sum_range_error.xyz, error_weight.xyz) * 0.5f * partition_size; - float alpha_range_error = sum_range_error.w * error_weight.w * 0.5f * partition_size; - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : partition=%d\nrgb-error_wt=%f alpha_error_wt=%f\n", __func__, partition_index, error_weight_rgbsum, error_weight.w); - - printf("ep0 = %f %f %f %f\n", ep0.x, ep0.y, ep0.z, ep0.w); - printf("ep1 = %f %f %f %f\n", ep1.x, ep1.y, ep1.z, ep1.w); - - - printf("rgb_range_error = %f, alpha_range_error = %f\n", rgb_range_error, alpha_range_error); - - printf("rgb-luma-error: %f\n", eci->rgb_luma_error); - } - #endif - - if (encode_hdr_rgb) - { - - // collect some statistics - float af, cf; - if (ep1.x > ep1.y && ep1.x > ep1.z) - { - af = ep1.x; - cf = ep1.x - ep0.x; - } - else if (ep1.y > ep1.z) - { - af = ep1.y; - cf = ep1.y - ep0.y; - } - else - { - af = ep1.z; - cf = ep1.z - ep0.z; - } - - float bf = af - ep1_min; // estimate of color-component spread in high endpoint color - float3 prd = ep1.xyz - float3(cf, cf, cf); - float3 pdif = prd - ep0.xyz; - // estimate of color-component spread in low endpoint color - float df = MAX(MAX(fabs(pdif.x), fabs(pdif.y)), fabs(pdif.z)); - - int b = (int)bf; - int c = (int)cf; - int d = (int)df; - - - // determine which one of the 6 submodes is likely to be used in - // case of an RGBO-mode - int rgbo_mode = 5; // 7 bits per component - // mode 4: 8 7 6 - if (b < 32768 && c < 16384) - rgbo_mode = 4; - // mode 3: 9 6 7 - if (b < 8192 && c < 16384) - rgbo_mode = 3; - // mode 2: 10 5 8 - if (b < 2048 && c < 16384) - rgbo_mode = 2; - // mode 1: 11 6 5 - if (b < 2048 && c < 1024) - rgbo_mode = 1; - // mode 0: 11 5 7 - if (b < 1024 && c < 4096) - rgbo_mode = 0; - - // determine which one of the 9 submodes is likely to be used in - // case of an RGB-mode. - int rgb_mode = 8; // 8 bits per component, except 7 bits for blue - - // mode 0: 9 7 6 7 - if (b < 16384 && c < 8192 && d < 8192) - rgb_mode = 0; - // mode 1: 9 8 6 6 - if (b < 32768 && c < 8192 && d < 4096) - rgb_mode = 1; - // mode 2: 10 6 7 7 - if (b < 4096 && c < 8192 && d < 4096) - rgb_mode = 2; - // mode 3: 10 7 7 6 - if (b < 8192 && c < 8192 && d < 2048) - rgb_mode = 3; - // mode 4: 11 8 6 5 - if (b < 8192 && c < 2048 && d < 512) - rgb_mode = 4; - // mode 5: 11 6 8 6 - if (b < 2048 && c < 8192 && d < 1024) - rgb_mode = 5; - // mode 6: 12 7 7 5 - if (b < 2048 && c < 2048 && d < 256) - rgb_mode = 6; - // mode 7: 12 6 7 6 - if (b < 1024 && c < 2048 && d < 512) - rgb_mode = 7; - - - static const float rgbo_error_scales[6] = { 4.0f, 4.0f, 16.0f, 64.0f, 256.0f, 1024.0f }; - static const float rgb_error_scales[9] = { 64.0f, 64.0f, 16.0f, 16.0f, 4.0f, 4.0f, 1.0f, 1.0f, 384.0f }; - - float mode7mult = rgbo_error_scales[rgbo_mode] * 0.0015f; // empirically determined .... - float mode11mult = rgb_error_scales[rgb_mode] * 0.010f; // empirically determined .... - - - float lum_high = (ep1.x + ep1.y + ep1.z) * (1.0f / 3.0f); - float lum_low = (ep0.x + ep0.y + ep0.z) * (1.0f / 3.0f); - float lumdif = lum_high - lum_low; - float mode23mult = lumdif < 960 ? 4.0f : lumdif < 3968 ? 16.0f : 128.0f; - - mode23mult *= 0.0005f; // empirically determined .... - - - - // pick among the available HDR endpoint modes - for (i = 0; i < 8; i++) - { - best_error[i][3] = 1e30f; - format_of_choice[i][3] = encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA; - best_error[i][2] = 1e30f; - format_of_choice[i][2] = FMT_HDR_RGB; - best_error[i][1] = 1e30f; - format_of_choice[i][1] = FMT_HDR_RGB_SCALE; - best_error[i][0] = 1e30f; - format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE; - } - - - for (i = 8; i < 21; i++) - { - // base_quant_error should depend on the scale-factor that would be used - // during actual encode of the color value. - - float base_quant_error = baseline_quant_error[i] * partition_size * 1.0f; - float rgb_quantization_error = error_weight_rgbsum * base_quant_error * 2.0f; - float alpha_quantization_error = error_weight.w * base_quant_error * 2.0f; - float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("rgba-quant = %f can_offset_encode=%d\n", rgba_quantization_error, eci->can_offset_encode); - #endif - - // for 8 integers, we have two encodings: one with HDR alpha and another one - // with LDR alpha. - - float full_hdr_rgba_error = rgba_quantization_error + rgb_range_error + alpha_range_error; - best_error[i][3] = full_hdr_rgba_error; - format_of_choice[i][3] = encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA; - - // for 6 integers, we have one HDR-RGB encoding - float full_hdr_rgb_error = (rgb_quantization_error * mode11mult) + rgb_range_error + eci->alpha_drop_error; - best_error[i][2] = full_hdr_rgb_error; - format_of_choice[i][2] = FMT_HDR_RGB; - - // for 4 integers, we have one HDR-RGB-Scale encoding - float hdr_rgb_scale_error = (rgb_quantization_error * mode7mult) + rgb_range_error + eci->alpha_drop_error + eci->rgb_luma_error; - - best_error[i][1] = hdr_rgb_scale_error; - format_of_choice[i][1] = FMT_HDR_RGB_SCALE; - - // for 2 integers, we assume luminance-with-large-range - float hdr_luminance_error = (rgb_quantization_error * mode23mult) + rgb_range_error + eci->alpha_drop_error + eci->luminance_error; - best_error[i][0] = hdr_luminance_error; - format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - for (j = 0; j < 4; j++) - { - printf("(hdr) quant-level=%d ints=%d format=%d error=%f\n", i, j, format_of_choice[i][j], best_error[i][j]); - } - } - #endif - } - } - - - else - { - for (i = 0; i < 4; i++) - { - best_error[i][3] = 1e30f; - best_error[i][2] = 1e30f; - best_error[i][1] = 1e30f; - best_error[i][0] = 1e30f; - - format_of_choice[i][3] = FMT_RGBA; - format_of_choice[i][2] = FMT_RGB; - format_of_choice[i][1] = FMT_RGB_SCALE; - format_of_choice[i][0] = FMT_LUMINANCE; - } - - - // pick among the available LDR endpoint modes - for (i = 4; i < 21; i++) - { - float base_quant_error = baseline_quant_error[i] * partition_size * 1.0f; - float rgb_quantization_error = error_weight_rgbsum * base_quant_error; - float alpha_quantization_error = error_weight.w * base_quant_error; - float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - printf("rgba-quant = %f can_offset_encode=%d\n", rgba_quantization_error, eci->can_offset_encode); - #endif - - // for 8 integers, the available encodings are: - // full LDR RGB-Alpha - float full_ldr_rgba_error = rgba_quantization_error; - if (eci->can_blue_contract) - full_ldr_rgba_error *= 0.625f; - if (eci->can_offset_encode && i <= 18) - full_ldr_rgba_error *= 0.5f; - full_ldr_rgba_error += rgb_range_error + alpha_range_error; - - best_error[i][3] = full_ldr_rgba_error; - format_of_choice[i][3] = FMT_RGBA; - - // for 6 integers, we have: - // - an LDR-RGB encoding - // - an RGBS + Alpha encoding (LDR) - - float full_ldr_rgb_error = rgb_quantization_error; - if (eci->can_blue_contract) - full_ldr_rgb_error *= 0.5f; - if (eci->can_offset_encode && i <= 18) - full_ldr_rgb_error *= 0.25f; - full_ldr_rgb_error += eci->alpha_drop_error + rgb_range_error; - - float rgbs_alpha_error = rgba_quantization_error + eci->rgb_scale_error + rgb_range_error + alpha_range_error; - - if (rgbs_alpha_error < full_ldr_rgb_error) - { - best_error[i][2] = rgbs_alpha_error; - format_of_choice[i][2] = FMT_RGB_SCALE_ALPHA; - } - else - { - best_error[i][2] = full_ldr_rgb_error; - format_of_choice[i][2] = FMT_RGB; - } - - - // for 4 integers, we have a Luminance-Alpha encoding and the RGBS encoding - float ldr_rgbs_error = rgb_quantization_error + eci->alpha_drop_error + eci->rgb_scale_error + rgb_range_error; - - float lum_alpha_error = rgba_quantization_error + eci->luminance_error + rgb_range_error + alpha_range_error; - - if (ldr_rgbs_error < lum_alpha_error) - { - best_error[i][1] = ldr_rgbs_error; - format_of_choice[i][1] = FMT_RGB_SCALE; - } - else - { - best_error[i][1] = lum_alpha_error; - format_of_choice[i][1] = FMT_LUMINANCE_ALPHA; - } - - - // for 2 integers, we have a Luminance-encoding and an Alpha-encoding. - float luminance_error = rgb_quantization_error + eci->alpha_drop_error + eci->luminance_error + rgb_range_error; - - best_error[i][0] = luminance_error; - format_of_choice[i][0] = FMT_LUMINANCE; - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - for (j = 0; j < 4; j++) - { - printf(" (ldr) quant-level=%d ints=%d format=%d error=%f\n", i, j, format_of_choice[i][j], best_error[i][j]); - } - } - #endif - } - } -} - - - -// for 1 partition, find the best combination (one format + a quantization level) for a given bitcount - -static void one_partition_find_best_combination_for_bitcount(float combined_best_error[21][4], - int formats_of_choice[21][4], int bits_available, int *best_quantization_level, int *best_formats, float *error_of_best_combination) -{ - int i; - int best_integer_count = -1; - float best_integer_count_error = 1e20f; - for (i = 0; i < 4; i++) - { - // compute the quantization level for a given number of integers and a given number of bits. - int quantization_level = quantization_mode_table[i + 1][bits_available]; - if (quantization_level == -1) - continue; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all. - if (combined_best_error[quantization_level][i] < best_integer_count_error) - { - best_integer_count_error = combined_best_error[quantization_level][i]; - best_integer_count = i; - } - } - - int ql = quantization_mode_table[best_integer_count + 1][bits_available]; - - *best_quantization_level = ql; - *error_of_best_combination = best_integer_count_error; - if (ql >= 0) - *best_formats = formats_of_choice[ql][best_integer_count]; - else - *best_formats = FMT_LUMINANCE; - -} - - - -// for 2 partitions, find the best format combinations for every (quantization-mode, integer-count) combination - -static void two_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[2][21][4], // indexed by (partition, quant-level, integer-pair-count-minus-1) - int format_of_choice[2][21][4], - float combined_best_error[21][7], // indexed by (quant-level, integer-pair-count-minus-2) - int formats_of_choice[21][7][2]) -{ - int i, j; - - for (i = 0; i < 21; i++) - for (j = 0; j < 7; j++) - combined_best_error[i][j] = 1e30f; - - int quant; - for (quant = 5; quant < 21; quant++) - { - for (i = 0; i < 4; i++) // integer-count for first endpoint-pair - { - for (j = 0; j < 4; j++) // integer-count for second endpoint-pair - { - int low2 = MIN(i, j); - int high2 = MAX(i, j); - if ((high2 - low2) > 1) - continue; - - int intcnt = i + j; - float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j], 1e10f); - if (errorterm <= combined_best_error[quant][intcnt]) - { - combined_best_error[quant][intcnt] = errorterm; - formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i]; - formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j]; - } - } - } - } -} - - -// for 2 partitions, find the best combination (two formats + a quantization level) for a given bitcount - -static void two_partitions_find_best_combination_for_bitcount(float combined_best_error[21][7], - int formats_of_choice[21][7][2], - int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination) -{ - int i; - - int best_integer_count = 0; - float best_integer_count_error = 1e20f; - int integer_count; - - for (integer_count = 2; integer_count <= 8; integer_count++) - { - // compute the quantization level for a given number of integers and a given number of bits. - int quantization_level = quantization_mode_table[integer_count][bits_available]; - if (quantization_level == -1) - break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all. - float integer_count_error = combined_best_error[quantization_level][integer_count - 2]; - if (integer_count_error < best_integer_count_error) - { - best_integer_count_error = integer_count_error; - best_integer_count = integer_count; - } - } - - int ql = quantization_mode_table[best_integer_count][bits_available]; - int ql_mod = quantization_mode_table[best_integer_count][bits_available + 2]; - - *best_quantization_level = ql; - *best_quantization_level_mod = ql_mod; - *error_of_best_combination = best_integer_count_error; - if (ql >= 0) - { - for (i = 0; i < 2; i++) - best_formats[i] = formats_of_choice[ql][best_integer_count - 2][i]; - } - else - { - for (i = 0; i < 2; i++) - best_formats[i] = FMT_LUMINANCE; - } -} - - - - -// for 3 partitions, find the best format combinations for every (quantization-mode, integer-count) combination - -static void three_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[3][21][4], // indexed by (partition, quant-level, integer-count) - int format_of_choice[3][21][4], float combined_best_error[21][10], int formats_of_choice[21][10][3]) -{ - int i, j, k; - - for (i = 0; i < 21; i++) - for (j = 0; j < 10; j++) - combined_best_error[i][j] = 1e30f; - - int quant; - for (quant = 5; quant < 21; quant++) - { - for (i = 0; i < 4; i++) // integer-count for first endpoint-pair - { - for (j = 0; j < 4; j++) // integer-count for second endpoint-pair - { - int low2 = MIN(i, j); - int high2 = MAX(i, j); - if ((high2 - low2) > 1) - continue; - for (k = 0; k < 4; k++) // integer-count for third endpoint-pair - { - int low3 = MIN(k, low2); - int high3 = MAX(k, high2); - if ((high3 - low3) > 1) - continue; - - int intcnt = i + j + k; - float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k], 1e10f); - if (errorterm <= combined_best_error[quant][intcnt]) - { - combined_best_error[quant][intcnt] = errorterm; - formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i]; - formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j]; - formats_of_choice[quant][intcnt][2] = format_of_choice[2][quant][k]; - } - } - } - } - } -} - - -// for 3 partitions, find the best combination (three formats + a quantization level) for a given bitcount - -static void three_partitions_find_best_combination_for_bitcount(float combined_best_error[21][10], - int formats_of_choice[21][10][3], - int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination) -{ - int i; - - int best_integer_count = 0; - float best_integer_count_error = 1e20f; - int integer_count; - - for (integer_count = 3; integer_count <= 9; integer_count++) - { - // compute the quantization level for a given number of integers and a given number of bits. - int quantization_level = quantization_mode_table[integer_count][bits_available]; - if (quantization_level == -1) - break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all. - float integer_count_error = combined_best_error[quantization_level][integer_count - 3]; - if (integer_count_error < best_integer_count_error) - { - best_integer_count_error = integer_count_error; - best_integer_count = integer_count; - } - } - - int ql = quantization_mode_table[best_integer_count][bits_available]; - int ql_mod = quantization_mode_table[best_integer_count][bits_available + 5]; - - *best_quantization_level = ql; - *best_quantization_level_mod = ql_mod; - *error_of_best_combination = best_integer_count_error; - if (ql >= 0) - { - for (i = 0; i < 3; i++) - best_formats[i] = formats_of_choice[ql][best_integer_count - 3][i]; - } - else - { - for (i = 0; i < 3; i++) - best_formats[i] = FMT_LUMINANCE; - } -} - - - - -// for 4 partitions, find the best format combinations for every (quantization-mode, integer-count) combination - -static void four_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[4][21][4], // indexed by (partition, quant-level, integer-count) - int format_of_choice[4][21][4], float combined_best_error[21][13], int formats_of_choice[21][13][4]) -{ - int i, j, k, l; - - for (i = 0; i < 21; i++) - for (j = 0; j < 13; j++) - combined_best_error[i][j] = 1e30f; - - int quant; - for (quant = 5; quant < 21; quant++) - { - for (i = 0; i < 4; i++) // integer-count for first endpoint-pair - { - for (j = 0; j < 4; j++) // integer-count for second endpoint-pair - { - int low2 = MIN(i, j); - int high2 = MAX(i, j); - if ((high2 - low2) > 1) - continue; - for (k = 0; k < 4; k++) // integer-count for third endpoint-pair - { - int low3 = MIN(k, low2); - int high3 = MAX(k, high2); - if ((high3 - low3) > 1) - continue; - for (l = 0; l < 4; l++) // integer-count for fourth endpoint-pair - { - int low4 = MIN(l, low3); - int high4 = MAX(l, high3); - if ((high4 - low4) > 1) - continue; - - int intcnt = i + j + k + l; - float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k] + best_error[3][quant][l], 1e10f); - if (errorterm <= combined_best_error[quant][intcnt]) - { - combined_best_error[quant][intcnt] = errorterm; - formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i]; - formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j]; - formats_of_choice[quant][intcnt][2] = format_of_choice[2][quant][k]; - formats_of_choice[quant][intcnt][3] = format_of_choice[3][quant][l]; - } - } - } - } - } - } -} - - - - - - -// for 4 partitions, find the best combination (four formats + a quantization level) for a given bitcount - -static void four_partitions_find_best_combination_for_bitcount(float combined_best_error[21][13], - int formats_of_choice[21][13][4], - int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination) -{ - int i; - int best_integer_count = 0; - float best_integer_count_error = 1e20f; - int integer_count; - - for (integer_count = 4; integer_count <= 9; integer_count++) - { - // compute the quantization level for a given number of integers and a given number of bits. - int quantization_level = quantization_mode_table[integer_count][bits_available]; - if (quantization_level == -1) - break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all. - float integer_count_error = combined_best_error[quantization_level][integer_count - 4]; - if (integer_count_error < best_integer_count_error) - { - best_integer_count_error = integer_count_error; - best_integer_count = integer_count; - } - } - - int ql = quantization_mode_table[best_integer_count][bits_available]; - int ql_mod = quantization_mode_table[best_integer_count][bits_available + 8]; - - *best_quantization_level = ql; - *best_quantization_level_mod = ql_mod; - *error_of_best_combination = best_integer_count_error; - if (ql >= 0) - { - for (i = 0; i < 4; i++) - best_formats[i] = formats_of_choice[ql][best_integer_count - 4][i]; - } - else - { - for (i = 0; i < 4; i++) - best_formats[i] = FMT_LUMINANCE; - } -} - - - -/* - The determine_optimal_set_of_endpoint_formats_to_use() function. - - It identifies, for each mode, which set of color endpoint encodings - produces the best overall result. It then reports back which 4 modes - look best, along with the ideal color encoding combination for each. - - It takes as input: - a partitioning an imageblock, - a set of color endpoints. - for each mode, the number of bits available for color encoding and the error incurred by quantization. - in case of 2 plane of weights, a specifier for which color component to use for the second plane of weights. - - It delivers as output for each of the 4 selected modes: - format specifier - for each partition - quantization level to use - modified quantization level to use - (when all format specifiers are equal) - */ - -void determine_optimal_set_of_endpoint_formats_to_use(int xdim, int ydim, int zdim, - const partition_info * pt, const imageblock * blk, const error_weight_block * ewb, - const endpoints * ep, - int separate_component, // separate color component for 2-plane mode; -1 for single-plane mode - // bitcounts and errors computed for the various quantization methods - const int *qwt_bitcounts, const float *qwt_errors, - // output data - int partition_format_specifiers[4][4], int quantized_weight[4], - int quantization_level[4], int quantization_level_mod[4]) -{ - int i, j; - int partition_count = pt->partition_count; - - int encode_hdr_rgb = blk->rgb_lns[0]; - int encode_hdr_alpha = blk->alpha_lns[0]; - - - // call a helper function to compute the errors that result from various - // encoding choices (such as using luminance instead of RGB, discarding Alpha, - // using RGB-scale in place of two separate RGB endpoints and so on) - encoding_choice_errors eci[4]; - compute_encoding_choice_errors(xdim, ydim, zdim, blk, pt, ewb, separate_component, eci); - - // for each partition, compute the error weights to apply for that partition. - float4 error_weightings[4]; - float4 dummied_color_scalefactors[4]; // only used to receive data - compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pt, error_weightings, dummied_color_scalefactors); - - - float best_error[4][21][4]; - int format_of_choice[4][21][4]; - for (i = 0; i < partition_count; i++) - compute_color_error_for_every_integer_count_and_quantization_level(encode_hdr_rgb, encode_hdr_alpha, i, pt, &(eci[i]), ep, error_weightings, best_error[i], format_of_choice[i]); - - float errors_of_best_combination[MAX_WEIGHT_MODES]; - int best_quantization_levels[MAX_WEIGHT_MODES]; - int best_quantization_levels_mod[MAX_WEIGHT_MODES]; - int best_ep_formats[MAX_WEIGHT_MODES][4]; - - // code for the case where the block contains 1 partition - if (partition_count == 1) - { - int best_quantization_level; - int best_format; - float error_of_best_combination; - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (qwt_errors[i] >= 1e29f) - { - errors_of_best_combination[i] = 1e30f; - continue; - } - - one_partition_find_best_combination_for_bitcount(best_error[0], format_of_choice[0], qwt_bitcounts[i], &best_quantization_level, &best_format, &error_of_best_combination); - error_of_best_combination += qwt_errors[i]; - - errors_of_best_combination[i] = error_of_best_combination; - best_quantization_levels[i] = best_quantization_level; - best_quantization_levels_mod[i] = best_quantization_level; - best_ep_formats[i][0] = best_format; - } - } - - // code for the case where the block contains 2 partitions - else if (partition_count == 2) - { - int best_quantization_level; - int best_quantization_level_mod; - int best_formats[2]; - float error_of_best_combination; - - float combined_best_error[21][7]; - int formats_of_choice[21][7][2]; - - two_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice); - - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (qwt_errors[i] >= 1e29f) - { - errors_of_best_combination[i] = 1e30f; - continue; - } - - two_partitions_find_best_combination_for_bitcount(combined_best_error, formats_of_choice, qwt_bitcounts[i], - &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination); - - error_of_best_combination += qwt_errors[i]; - - errors_of_best_combination[i] = error_of_best_combination; - best_quantization_levels[i] = best_quantization_level; - best_quantization_levels_mod[i] = best_quantization_level_mod; - best_ep_formats[i][0] = best_formats[0]; - best_ep_formats[i][1] = best_formats[1]; - } - } - - // code for the case where the block contains 3 partitions - else if (partition_count == 3) - { - int best_quantization_level; - int best_quantization_level_mod; - int best_formats[3]; - float error_of_best_combination; - - float combined_best_error[21][10]; - int formats_of_choice[21][10][3]; - - three_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice); - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (qwt_errors[i] >= 1e29f) - { - errors_of_best_combination[i] = 1e30f; - continue; - } - - three_partitions_find_best_combination_for_bitcount(combined_best_error, - formats_of_choice, qwt_bitcounts[i], &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination); - error_of_best_combination += qwt_errors[i]; - - errors_of_best_combination[i] = error_of_best_combination; - best_quantization_levels[i] = best_quantization_level; - best_quantization_levels_mod[i] = best_quantization_level_mod; - best_ep_formats[i][0] = best_formats[0]; - best_ep_formats[i][1] = best_formats[1]; - best_ep_formats[i][2] = best_formats[2]; - } - } - - // code for the case where the block contains 4 partitions - else if (partition_count == 4) - { - int best_quantization_level; - int best_quantization_level_mod; - int best_formats[4]; - float error_of_best_combination; - - float combined_best_error[21][13]; - int formats_of_choice[21][13][4]; - - four_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice); - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (qwt_errors[i] >= 1e29f) - { - errors_of_best_combination[i] = 1e30f; - continue; - } - four_partitions_find_best_combination_for_bitcount(combined_best_error, - formats_of_choice, qwt_bitcounts[i], &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination); - error_of_best_combination += qwt_errors[i]; - - errors_of_best_combination[i] = error_of_best_combination; - best_quantization_levels[i] = best_quantization_level; - best_quantization_levels_mod[i] = best_quantization_level_mod; - best_ep_formats[i][0] = best_formats[0]; - best_ep_formats[i][1] = best_formats[1]; - best_ep_formats[i][2] = best_formats[2]; - best_ep_formats[i][3] = best_formats[3]; - } - } - - // finally, go through the results and pick the 4 best-looking modes. - - int best_error_weights[4]; - - for (i = 0; i < 4; i++) - { - float best_ep_error = 1e30f; - int best_error_index = -1; - for (j = 0; j < MAX_WEIGHT_MODES; j++) - { - if (errors_of_best_combination[j] < best_ep_error && best_quantization_levels[j] >= 5) - { - best_ep_error = errors_of_best_combination[j]; - best_error_index = j; - } - } - best_error_weights[i] = best_error_index; - - if(best_error_index >= 0) - { - errors_of_best_combination[best_error_index] = 1e30f; - } - } - - for (i = 0; i < 4; i++) - { - quantized_weight[i] = best_error_weights[i]; - if (quantized_weight[i] >= 0) - { - quantization_level[i] = best_quantization_levels[best_error_weights[i]]; - quantization_level_mod[i] = best_quantization_levels_mod[best_error_weights[i]]; - for (j = 0; j < partition_count; j++) - { - partition_format_specifiers[i][j] = best_ep_formats[best_error_weights[i]][j]; - } - } - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_quantization.cpp b/3rdparty/bimg/3rdparty/astc/astc_quantization.cpp deleted file mode 100644 index 183c5a334a5..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_quantization.cpp +++ /dev/null @@ -1,558 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions and data table related to data quantization in ASTC. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -const uint8_t color_quantization_tables[21][256] = { - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, - }, - { - 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, - }, - { - 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, - 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, - 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, - 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, - 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, - 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, - 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, - 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, - 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, - 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, - }, - { - 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, - 16, 24, 24, 24, 24, 24, 24, 32, 32, 32, 32, 32, 32, 32, 2, 2, - 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 10, 18, 18, 18, 18, 18, - 18, 26, 26, 26, 26, 26, 26, 26, 34, 34, 34, 34, 34, 34, 4, 4, - 4, 4, 4, 4, 4, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, - 20, 20, 28, 28, 28, 28, 28, 28, 36, 36, 36, 36, 36, 36, 36, 6, - 6, 6, 6, 6, 6, 14, 14, 14, 14, 14, 14, 14, 22, 22, 22, 22, - 22, 22, 30, 30, 30, 30, 30, 30, 30, 38, 38, 38, 38, 38, 38, 38, - 39, 39, 39, 39, 39, 39, 39, 31, 31, 31, 31, 31, 31, 31, 23, 23, - 23, 23, 23, 23, 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, - 7, 37, 37, 37, 37, 37, 37, 37, 29, 29, 29, 29, 29, 29, 21, 21, - 21, 21, 21, 21, 21, 13, 13, 13, 13, 13, 13, 5, 5, 5, 5, 5, - 5, 5, 35, 35, 35, 35, 35, 35, 27, 27, 27, 27, 27, 27, 27, 19, - 19, 19, 19, 19, 19, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, - 3, 3, 33, 33, 33, 33, 33, 33, 33, 25, 25, 25, 25, 25, 25, 17, - 17, 17, 17, 17, 17, 17, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, - }, - { - 0, 0, 0, 16, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32, 2, 2, - 2, 2, 2, 18, 18, 18, 18, 18, 18, 34, 34, 34, 34, 34, 4, 4, - 4, 4, 4, 4, 20, 20, 20, 20, 20, 36, 36, 36, 36, 36, 6, 6, - 6, 6, 6, 6, 22, 22, 22, 22, 22, 38, 38, 38, 38, 38, 38, 8, - 8, 8, 8, 8, 24, 24, 24, 24, 24, 24, 40, 40, 40, 40, 40, 10, - 10, 10, 10, 10, 26, 26, 26, 26, 26, 26, 42, 42, 42, 42, 42, 12, - 12, 12, 12, 12, 12, 28, 28, 28, 28, 28, 44, 44, 44, 44, 44, 14, - 14, 14, 14, 14, 14, 30, 30, 30, 30, 30, 46, 46, 46, 46, 46, 46, - 47, 47, 47, 47, 47, 47, 31, 31, 31, 31, 31, 15, 15, 15, 15, 15, - 15, 45, 45, 45, 45, 45, 29, 29, 29, 29, 29, 13, 13, 13, 13, 13, - 13, 43, 43, 43, 43, 43, 27, 27, 27, 27, 27, 27, 11, 11, 11, 11, - 11, 41, 41, 41, 41, 41, 25, 25, 25, 25, 25, 25, 9, 9, 9, 9, - 9, 39, 39, 39, 39, 39, 39, 23, 23, 23, 23, 23, 7, 7, 7, 7, - 7, 7, 37, 37, 37, 37, 37, 21, 21, 21, 21, 21, 5, 5, 5, 5, - 5, 5, 35, 35, 35, 35, 35, 19, 19, 19, 19, 19, 19, 3, 3, 3, - 3, 3, 33, 33, 33, 33, 33, 17, 17, 17, 17, 17, 17, 1, 1, 1, - }, - { - 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, - 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, - 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, - 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, - 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, - 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, - 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, - 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, - 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, - 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39, - 40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, - 44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, - 47, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, - 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, - 55, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, - 59, 60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, - }, - { - 0, 0, 16, 16, 16, 32, 32, 32, 48, 48, 48, 48, 64, 64, 64, 2, - 2, 2, 18, 18, 18, 34, 34, 34, 50, 50, 50, 50, 66, 66, 66, 4, - 4, 4, 20, 20, 20, 36, 36, 36, 36, 52, 52, 52, 68, 68, 68, 6, - 6, 6, 22, 22, 22, 38, 38, 38, 38, 54, 54, 54, 70, 70, 70, 8, - 8, 8, 24, 24, 24, 24, 40, 40, 40, 56, 56, 56, 72, 72, 72, 10, - 10, 10, 26, 26, 26, 26, 42, 42, 42, 58, 58, 58, 74, 74, 74, 12, - 12, 12, 12, 28, 28, 28, 44, 44, 44, 60, 60, 60, 76, 76, 76, 14, - 14, 14, 14, 30, 30, 30, 46, 46, 46, 62, 62, 62, 78, 78, 78, 78, - 79, 79, 79, 79, 63, 63, 63, 47, 47, 47, 31, 31, 31, 15, 15, 15, - 15, 77, 77, 77, 61, 61, 61, 45, 45, 45, 29, 29, 29, 13, 13, 13, - 13, 75, 75, 75, 59, 59, 59, 43, 43, 43, 27, 27, 27, 27, 11, 11, - 11, 73, 73, 73, 57, 57, 57, 41, 41, 41, 25, 25, 25, 25, 9, 9, - 9, 71, 71, 71, 55, 55, 55, 39, 39, 39, 39, 23, 23, 23, 7, 7, - 7, 69, 69, 69, 53, 53, 53, 37, 37, 37, 37, 21, 21, 21, 5, 5, - 5, 67, 67, 67, 51, 51, 51, 51, 35, 35, 35, 19, 19, 19, 3, 3, - 3, 65, 65, 65, 49, 49, 49, 49, 33, 33, 33, 17, 17, 17, 1, 1, - }, - { - 0, 0, 32, 32, 64, 64, 64, 2, 2, 2, 34, 34, 66, 66, 66, 4, - 4, 4, 36, 36, 68, 68, 68, 6, 6, 6, 38, 38, 70, 70, 70, 8, - 8, 8, 40, 40, 40, 72, 72, 10, 10, 10, 42, 42, 42, 74, 74, 12, - 12, 12, 44, 44, 44, 76, 76, 14, 14, 14, 46, 46, 46, 78, 78, 16, - 16, 16, 48, 48, 48, 80, 80, 80, 18, 18, 50, 50, 50, 82, 82, 82, - 20, 20, 52, 52, 52, 84, 84, 84, 22, 22, 54, 54, 54, 86, 86, 86, - 24, 24, 56, 56, 56, 88, 88, 88, 26, 26, 58, 58, 58, 90, 90, 90, - 28, 28, 60, 60, 60, 92, 92, 92, 30, 30, 62, 62, 62, 94, 94, 94, - 95, 95, 95, 63, 63, 63, 31, 31, 93, 93, 93, 61, 61, 61, 29, 29, - 91, 91, 91, 59, 59, 59, 27, 27, 89, 89, 89, 57, 57, 57, 25, 25, - 87, 87, 87, 55, 55, 55, 23, 23, 85, 85, 85, 53, 53, 53, 21, 21, - 83, 83, 83, 51, 51, 51, 19, 19, 81, 81, 81, 49, 49, 49, 17, 17, - 17, 79, 79, 47, 47, 47, 15, 15, 15, 77, 77, 45, 45, 45, 13, 13, - 13, 75, 75, 43, 43, 43, 11, 11, 11, 73, 73, 41, 41, 41, 9, 9, - 9, 71, 71, 71, 39, 39, 7, 7, 7, 69, 69, 69, 37, 37, 5, 5, - 5, 67, 67, 67, 35, 35, 3, 3, 3, 65, 65, 65, 33, 33, 1, 1, - }, - { - 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, - 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, - 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, - 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, - 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, - 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47, - 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, - 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, - 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, - 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79, - 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, - 88, 88, 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 94, 94, 95, 95, - 96, 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 103, 103, - 104, 104, 105, 105, 106, 106, 107, 107, 108, 108, 109, 109, 110, 110, 111, 111, - 112, 112, 113, 113, 114, 114, 115, 115, 116, 116, 117, 117, 118, 118, 119, 119, - 120, 120, 121, 121, 122, 122, 123, 123, 124, 124, 125, 125, 126, 126, 127, 127, - }, - { - 0, 32, 32, 64, 96, 96, 128, 128, 2, 34, 34, 66, 98, 98, 130, 130, - 4, 36, 36, 68, 100, 100, 132, 132, 6, 38, 38, 70, 102, 102, 134, 134, - 8, 40, 40, 72, 104, 104, 136, 136, 10, 42, 42, 74, 106, 106, 138, 138, - 12, 44, 44, 76, 108, 108, 140, 140, 14, 46, 46, 78, 110, 110, 142, 142, - 16, 48, 48, 80, 112, 112, 144, 144, 18, 50, 50, 82, 114, 114, 146, 146, - 20, 52, 52, 84, 116, 116, 148, 148, 22, 54, 54, 86, 118, 118, 150, 150, - 24, 56, 56, 88, 120, 120, 152, 152, 26, 58, 58, 90, 122, 122, 154, 154, - 28, 60, 60, 92, 124, 124, 156, 156, 30, 62, 62, 94, 126, 126, 158, 158, - 159, 159, 127, 127, 95, 63, 63, 31, 157, 157, 125, 125, 93, 61, 61, 29, - 155, 155, 123, 123, 91, 59, 59, 27, 153, 153, 121, 121, 89, 57, 57, 25, - 151, 151, 119, 119, 87, 55, 55, 23, 149, 149, 117, 117, 85, 53, 53, 21, - 147, 147, 115, 115, 83, 51, 51, 19, 145, 145, 113, 113, 81, 49, 49, 17, - 143, 143, 111, 111, 79, 47, 47, 15, 141, 141, 109, 109, 77, 45, 45, 13, - 139, 139, 107, 107, 75, 43, 43, 11, 137, 137, 105, 105, 73, 41, 41, 9, - 135, 135, 103, 103, 71, 39, 39, 7, 133, 133, 101, 101, 69, 37, 37, 5, - 131, 131, 99, 99, 67, 35, 35, 3, 129, 129, 97, 97, 65, 33, 33, 1, - }, - { - 0, 64, 128, 128, 2, 66, 130, 130, 4, 68, 132, 132, 6, 70, 134, 134, - 8, 72, 136, 136, 10, 74, 138, 138, 12, 76, 140, 140, 14, 78, 142, 142, - 16, 80, 144, 144, 18, 82, 146, 146, 20, 84, 148, 148, 22, 86, 150, 150, - 24, 88, 152, 152, 26, 90, 154, 154, 28, 92, 156, 156, 30, 94, 158, 158, - 32, 96, 160, 160, 34, 98, 162, 162, 36, 100, 164, 164, 38, 102, 166, 166, - 40, 104, 168, 168, 42, 106, 170, 170, 44, 108, 172, 172, 46, 110, 174, 174, - 48, 112, 176, 176, 50, 114, 178, 178, 52, 116, 180, 180, 54, 118, 182, 182, - 56, 120, 184, 184, 58, 122, 186, 186, 60, 124, 188, 188, 62, 126, 190, 190, - 191, 191, 127, 63, 189, 189, 125, 61, 187, 187, 123, 59, 185, 185, 121, 57, - 183, 183, 119, 55, 181, 181, 117, 53, 179, 179, 115, 51, 177, 177, 113, 49, - 175, 175, 111, 47, 173, 173, 109, 45, 171, 171, 107, 43, 169, 169, 105, 41, - 167, 167, 103, 39, 165, 165, 101, 37, 163, 163, 99, 35, 161, 161, 97, 33, - 159, 159, 95, 31, 157, 157, 93, 29, 155, 155, 91, 27, 153, 153, 89, 25, - 151, 151, 87, 23, 149, 149, 85, 21, 147, 147, 83, 19, 145, 145, 81, 17, - 143, 143, 79, 15, 141, 141, 77, 13, 139, 139, 75, 11, 137, 137, 73, 9, - 135, 135, 71, 7, 133, 133, 69, 5, 131, 131, 67, 3, 129, 129, 65, 1, - }, - { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, - 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, - 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, - 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, - 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, - 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, - 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, - 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, - 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, - }, -}; - - -const uint8_t color_unquantization_tables[21][256] = { - { - 0, 255, - }, - { - 0, 128, 255, - }, - { - 0, 85, 170, 255, - }, - { - 0, 64, 128, 192, 255, - }, - { - 0, 255, 51, 204, 102, 153, - }, - { - 0, 36, 73, 109, 146, 182, 219, 255, - }, - { - 0, 255, 28, 227, 56, 199, 84, 171, 113, 142, - }, - { - 0, 255, 69, 186, 23, 232, 92, 163, 46, 209, 116, 139, - }, - { - 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, - }, - { - 0, 255, 67, 188, 13, 242, 80, 175, 27, 228, 94, 161, 40, 215, 107, 148, - 54, 201, 121, 134, - }, - { - 0, 255, 33, 222, 66, 189, 99, 156, 11, 244, 44, 211, 77, 178, 110, 145, - 22, 233, 55, 200, 88, 167, 121, 134, - }, - { - 0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90, 99, 107, 115, 123, - 132, 140, 148, 156, 165, 173, 181, 189, 198, 206, 214, 222, 231, 239, 247, 255, - }, - { - 0, 255, 32, 223, 65, 190, 97, 158, 6, 249, 39, 216, 71, 184, 104, 151, - 13, 242, 45, 210, 78, 177, 110, 145, 19, 236, 52, 203, 84, 171, 117, 138, - 26, 229, 58, 197, 91, 164, 123, 132, - }, - { - 0, 255, 16, 239, 32, 223, 48, 207, 65, 190, 81, 174, 97, 158, 113, 142, - 5, 250, 21, 234, 38, 217, 54, 201, 70, 185, 86, 169, 103, 152, 119, 136, - 11, 244, 27, 228, 43, 212, 59, 196, 76, 179, 92, 163, 108, 147, 124, 131, - }, - { - 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, - 65, 69, 73, 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, - 130, 134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, - 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, - }, - { - 0, 255, 16, 239, 32, 223, 48, 207, 64, 191, 80, 175, 96, 159, 112, 143, - 3, 252, 19, 236, 35, 220, 51, 204, 67, 188, 83, 172, 100, 155, 116, 139, - 6, 249, 22, 233, 38, 217, 54, 201, 71, 184, 87, 168, 103, 152, 119, 136, - 9, 246, 25, 230, 42, 213, 58, 197, 74, 181, 90, 165, 106, 149, 122, 133, - 13, 242, 29, 226, 45, 210, 61, 194, 77, 178, 93, 162, 109, 146, 125, 130, - }, - { - 0, 255, 8, 247, 16, 239, 24, 231, 32, 223, 40, 215, 48, 207, 56, 199, - 64, 191, 72, 183, 80, 175, 88, 167, 96, 159, 104, 151, 112, 143, 120, 135, - 2, 253, 10, 245, 18, 237, 26, 229, 35, 220, 43, 212, 51, 204, 59, 196, - 67, 188, 75, 180, 83, 172, 91, 164, 99, 156, 107, 148, 115, 140, 123, 132, - 5, 250, 13, 242, 21, 234, 29, 226, 37, 218, 45, 210, 53, 202, 61, 194, - 70, 185, 78, 177, 86, 169, 94, 161, 102, 153, 110, 145, 118, 137, 126, 129, - }, - { - 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, - 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, - 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, - 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, - 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, - 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, - 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, - 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, - }, - { - 0, 255, 8, 247, 16, 239, 24, 231, 32, 223, 40, 215, 48, 207, 56, 199, - 64, 191, 72, 183, 80, 175, 88, 167, 96, 159, 104, 151, 112, 143, 120, 135, - 1, 254, 9, 246, 17, 238, 25, 230, 33, 222, 41, 214, 49, 206, 57, 198, - 65, 190, 73, 182, 81, 174, 89, 166, 97, 158, 105, 150, 113, 142, 121, 134, - 3, 252, 11, 244, 19, 236, 27, 228, 35, 220, 43, 212, 51, 204, 59, 196, - 67, 188, 75, 180, 83, 172, 91, 164, 99, 156, 107, 148, 115, 140, 123, 132, - 4, 251, 12, 243, 20, 235, 28, 227, 36, 219, 44, 211, 52, 203, 60, 195, - 68, 187, 76, 179, 84, 171, 92, 163, 100, 155, 108, 147, 116, 139, 124, 131, - 6, 249, 14, 241, 22, 233, 30, 225, 38, 217, 46, 209, 54, 201, 62, 193, - 70, 185, 78, 177, 86, 169, 94, 161, 102, 153, 110, 145, 118, 137, 126, 129, - }, - { - 0, 255, 4, 251, 8, 247, 12, 243, 16, 239, 20, 235, 24, 231, 28, 227, - 32, 223, 36, 219, 40, 215, 44, 211, 48, 207, 52, 203, 56, 199, 60, 195, - 64, 191, 68, 187, 72, 183, 76, 179, 80, 175, 84, 171, 88, 167, 92, 163, - 96, 159, 100, 155, 104, 151, 108, 147, 112, 143, 116, 139, 120, 135, 124, 131, - 1, 254, 5, 250, 9, 246, 13, 242, 17, 238, 21, 234, 25, 230, 29, 226, - 33, 222, 37, 218, 41, 214, 45, 210, 49, 206, 53, 202, 57, 198, 61, 194, - 65, 190, 69, 186, 73, 182, 77, 178, 81, 174, 85, 170, 89, 166, 93, 162, - 97, 158, 101, 154, 105, 150, 109, 146, 113, 142, 117, 138, 121, 134, 125, 130, - 2, 253, 6, 249, 10, 245, 14, 241, 18, 237, 22, 233, 26, 229, 30, 225, - 34, 221, 38, 217, 42, 213, 46, 209, 50, 205, 54, 201, 58, 197, 62, 193, - 66, 189, 70, 185, 74, 181, 78, 177, 82, 173, 86, 169, 90, 165, 94, 161, - 98, 157, 102, 153, 106, 149, 110, 145, 114, 141, 118, 137, 122, 133, 126, 129, - }, - { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, - 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, - 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, - 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, - 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, - 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, - 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, - 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, - 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, - }, -}; - -// quantization_mode_table[integercount/2][bits] gives -// us the quantization level for a given integer count and number of bits that -// the integer may fit into. This is needed for color decoding, -// and for the color encoding. -int quantization_mode_table[17][128]; - -void build_quantization_mode_table(void) -{ - int i, j; - for (i = 0; i <= 16; i++) - for (j = 0; j < 128; j++) - quantization_mode_table[i][j] = -1; - - for (i = 0; i < 21; i++) - for (j = 1; j <= 16; j++) - { - int p = compute_ise_bitcount(2 * j, (quantization_method) i); - if (p < 128) - quantization_mode_table[j][p] = i; - } - for (i = 0; i <= 16; i++) - { - int largest_value_so_far = -1; - for (j = 0; j < 128; j++) - { - if (quantization_mode_table[i][j] > largest_value_so_far) - largest_value_so_far = quantization_mode_table[i][j]; - else - quantization_mode_table[i][j] = largest_value_so_far; - } - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_symbolic_physical.cpp b/3rdparty/bimg/3rdparty/astc/astc_symbolic_physical.cpp deleted file mode 100644 index d395b8cfcd0..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_symbolic_physical.cpp +++ /dev/null @@ -1,431 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Functions to convert a compressed block between the symbolic and - * the physical representation. - */ -/*----------------------------------------------------------------------------*/ - -#include "astc_codec_internals.h" - -// routine to write up to 8 bits -static inline void write_bits(int value, int bitcount, int bitoffset, uint8_t * ptr) -{ - int mask = (1 << bitcount) - 1; - value &= mask; - ptr += bitoffset >> 3; - bitoffset &= 7; - value <<= bitoffset; - mask <<= bitoffset; - mask = ~mask; - - ptr[0] &= mask; - ptr[0] |= value; - ptr[1] &= mask >> 8; - ptr[1] |= value >> 8; -} - - -// routine to read up to 8 bits -static inline int read_bits(int bitcount, int bitoffset, const uint8_t * ptr) -{ - int mask = (1 << bitcount) - 1; - ptr += bitoffset >> 3; - bitoffset &= 7; - int value = ptr[0] | (ptr[1] << 8); - value >>= bitoffset; - value &= mask; - return value; -} - - -int bitrev8(int p) -{ - p = ((p & 0xF) << 4) | ((p >> 4) & 0xF); - p = ((p & 0x33) << 2) | ((p >> 2) & 0x33); - p = ((p & 0x55) << 1) | ((p >> 1) & 0x55); - return p; -} - - - - -physical_compressed_block symbolic_to_physical(int xdim, int ydim, int zdim, const symbolic_compressed_block * sc) -{ - int i, j; - physical_compressed_block res; - - - if (sc->block_mode == -2) - { - // UNORM16 constant-color block. - // This encodes separate constant-color blocks. There is currently - // no attempt to coalesce them into larger void-extents. - - static const uint8_t cbytes[8] = { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - for (i = 0; i < 8; i++) - res.data[i] = cbytes[i]; - - for (i = 0; i < 4; i++) - { - res.data[2 * i + 8] = sc->constant_color[i] & 0xFF; - res.data[2 * i + 9] = (sc->constant_color[i] >> 8) & 0xFF; - } - return res; - } - - - if (sc->block_mode == -1) - { - // FP16 constant-color block. - // This encodes separate constant-color blocks. There is currently - // no attempt to coalesce them into larger void-extents. - - static const uint8_t cbytes[8] = { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - for (i = 0; i < 8; i++) - res.data[i] = cbytes[i]; - - for (i = 0; i < 4; i++) - { - res.data[2 * i + 8] = sc->constant_color[i] & 0xFF; - res.data[2 * i + 9] = (sc->constant_color[i] >> 8) & 0xFF; - } - return res; - } - - - - int partition_count = sc->partition_count; - - // first, compress the weights. They are encoded as an ordinary - // integer-sequence, then bit-reversed - uint8_t weightbuf[16]; - for (i = 0; i < 16; i++) - weightbuf[i] = 0; - - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - - - int weight_count = ixtab2[bsd->block_modes[sc->block_mode].decimation_mode]->num_weights; - int weight_quantization_method = bsd->block_modes[sc->block_mode].quantization_mode; - int is_dual_plane = bsd->block_modes[sc->block_mode].is_dual_plane; - - int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count; - - int bits_for_weights = compute_ise_bitcount(real_weight_count, - (quantization_method) weight_quantization_method); - - - if (is_dual_plane) - { - uint8_t weights[64]; - for (i = 0; i < weight_count; i++) - { - weights[2 * i] = sc->plane1_weights[i]; - weights[2 * i + 1] = sc->plane2_weights[i]; - } - encode_ise(weight_quantization_method, real_weight_count, weights, weightbuf, 0); - } - else - { - encode_ise(weight_quantization_method, weight_count, sc->plane1_weights, weightbuf, 0); - } - - for (i = 0; i < 16; i++) - res.data[i] = bitrev8(weightbuf[15 - i]); - - write_bits(sc->block_mode, 11, 0, res.data); - write_bits(partition_count - 1, 2, 11, res.data); - - int below_weights_pos = 128 - bits_for_weights; - - // encode partition index and color endpoint types for blocks with - // 2 or more partitions. - if (partition_count > 1) - { - write_bits(sc->partition_index, 6, 13, res.data); - write_bits(sc->partition_index >> 6, PARTITION_BITS - 6, 19, res.data); - - if (sc->color_formats_matched) - { - write_bits(sc->color_formats[0] << 2, 6, 13 + PARTITION_BITS, res.data); - } - else - { - // go through the selected endpoint type classes for each partition - // in order to determine the lowest class present. - int low_class = 4; - for (i = 0; i < partition_count; i++) - { - int class_of_format = sc->color_formats[i] >> 2; - if (class_of_format < low_class) - low_class = class_of_format; - } - if (low_class == 3) - low_class = 2; - int encoded_type = low_class + 1; - int bitpos = 2; - for (i = 0; i < partition_count; i++) - { - int classbit_of_format = (sc->color_formats[i] >> 2) - low_class; - - encoded_type |= classbit_of_format << bitpos; - bitpos++; - } - for (i = 0; i < partition_count; i++) - { - int lowbits_of_format = sc->color_formats[i] & 3; - encoded_type |= lowbits_of_format << bitpos; - bitpos += 2; - } - int encoded_type_lowpart = encoded_type & 0x3F; - int encoded_type_highpart = encoded_type >> 6; - int encoded_type_highpart_size = (3 * partition_count) - 4; - int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size; - write_bits(encoded_type_lowpart, 6, 13 + PARTITION_BITS, res.data); - write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, res.data); - - below_weights_pos -= encoded_type_highpart_size; - } - } - - else - write_bits(sc->color_formats[0], 4, 13, res.data); - - // in dual-plane mode, encode the color component of the second plane of weights - if (is_dual_plane) - write_bits(sc->plane2_color_component, 2, below_weights_pos - 2, res.data); - - // finally, encode the color bits - // first, get hold of all the color components to encode - uint8_t values_to_encode[32]; - int valuecount_to_encode = 0; - for (i = 0; i < sc->partition_count; i++) - { - int vals = 2 * (sc->color_formats[i] >> 2) + 2; - for (j = 0; j < vals; j++) - values_to_encode[j + valuecount_to_encode] = sc->color_values[i][j]; - valuecount_to_encode += vals; - } - // then, encode an ISE based on them. - encode_ise(sc->color_quantization_level, valuecount_to_encode, values_to_encode, res.data, (sc->partition_count == 1 ? 17 : 19 + PARTITION_BITS)); - - return res; -} - - -void physical_to_symbolic(int xdim, int ydim, int zdim, physical_compressed_block pb, symbolic_compressed_block * res) -{ - uint8_t bswapped[16]; - int i, j; - - res->error_block = 0; - - // get hold of the block-size descriptor and the decimation tables. - const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim); - const decimation_table *const *ixtab2 = bsd->decimation_tables; - - // extract header fields - int block_mode = read_bits(11, 0, pb.data); - - - if ((block_mode & 0x1FF) == 0x1FC) - { - // void-extent block! - - // check what format the data has - if (block_mode & 0x200) - res->block_mode = -1; // floating-point - else - res->block_mode = -2; // unorm16. - - res->partition_count = 0; - for (i = 0; i < 4; i++) - { - res->constant_color[i] = pb.data[2 * i + 8] | (pb.data[2 * i + 9] << 8); - } - - // additionally, check that the void-extent - if (zdim == 1) - { - // 2D void-extent - int rsvbits = read_bits(2, 10, pb.data); - if (rsvbits != 3) - res->error_block = 1; - - int vx_low_s = read_bits(8, 12, pb.data) | (read_bits(5, 12 + 8, pb.data) << 8); - int vx_high_s = read_bits(8, 25, pb.data) | (read_bits(5, 25 + 8, pb.data) << 8); - int vx_low_t = read_bits(8, 38, pb.data) | (read_bits(5, 38 + 8, pb.data) << 8); - int vx_high_t = read_bits(8, 51, pb.data) | (read_bits(5, 51 + 8, pb.data) << 8); - - int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF && vx_low_t == 0x1FFF && vx_high_t == 0x1FFF; - - if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones) - res->error_block = 1; - } - else - { - // 3D void-extent - int vx_low_s = read_bits(9, 10, pb.data); - int vx_high_s = read_bits(9, 19, pb.data); - int vx_low_t = read_bits(9, 28, pb.data); - int vx_high_t = read_bits(9, 37, pb.data); - int vx_low_p = read_bits(9, 46, pb.data); - int vx_high_p = read_bits(9, 55, pb.data); - - int all_ones = vx_low_s == 0x1FF && vx_high_s == 0x1FF && vx_low_t == 0x1FF && vx_high_t == 0x1FF && vx_low_p == 0x1FF && vx_high_p == 0x1FF; - - if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_p >= vx_high_p) && !all_ones) - res->error_block = 1; - } - - return; - } - - if (bsd->block_modes[block_mode].permit_decode == 0) - { - res->error_block = 1; - return; - } - - int weight_count = ixtab2[bsd->block_modes[block_mode].decimation_mode]->num_weights; - int weight_quantization_method = bsd->block_modes[block_mode].quantization_mode; - int is_dual_plane = bsd->block_modes[block_mode].is_dual_plane; - - int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count; - - int partition_count = read_bits(2, 11, pb.data) + 1; - - res->block_mode = block_mode; - res->partition_count = partition_count; - - for (i = 0; i < 16; i++) - bswapped[i] = bitrev8(pb.data[15 - i]); - - int bits_for_weights = compute_ise_bitcount(real_weight_count, - (quantization_method) weight_quantization_method); - - int below_weights_pos = 128 - bits_for_weights; - - if (is_dual_plane) - { - uint8_t indices[64]; - decode_ise(weight_quantization_method, real_weight_count, bswapped, indices, 0); - for (i = 0; i < weight_count; i++) - { - res->plane1_weights[i] = indices[2 * i]; - res->plane2_weights[i] = indices[2 * i + 1]; - } - } - else - { - decode_ise(weight_quantization_method, weight_count, bswapped, res->plane1_weights, 0); - } - - if (is_dual_plane && partition_count == 4) - res->error_block = 1; - - - - res->color_formats_matched = 0; - - // then, determine the format of each endpoint pair - int color_formats[4]; - int encoded_type_highpart_size = 0; - if (partition_count == 1) - { - color_formats[0] = read_bits(4, 13, pb.data); - res->partition_index = 0; - } - else - { - encoded_type_highpart_size = (3 * partition_count) - 4; - below_weights_pos -= encoded_type_highpart_size; - int encoded_type = read_bits(6, 13 + PARTITION_BITS, pb.data) | (read_bits(encoded_type_highpart_size, below_weights_pos, pb.data) << 6); - int baseclass = encoded_type & 0x3; - if (baseclass == 0) - { - for (i = 0; i < partition_count; i++) - { - color_formats[i] = (encoded_type >> 2) & 0xF; - } - below_weights_pos += encoded_type_highpart_size; - res->color_formats_matched = 1; - encoded_type_highpart_size = 0; - } - else - { - int bitpos = 2; - baseclass--; - for (i = 0; i < partition_count; i++) - { - color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2; - bitpos++; - } - for (i = 0; i < partition_count; i++) - { - color_formats[i] |= (encoded_type >> bitpos) & 3; - bitpos += 2; - } - } - res->partition_index = read_bits(6, 13, pb.data) | (read_bits(PARTITION_BITS - 6, 19, pb.data) << 6); - - } - for (i = 0; i < partition_count; i++) - res->color_formats[i] = color_formats[i]; - - - // then, determine the number of integers we need to unpack for the endpoint pairs - int color_integer_count = 0; - for (i = 0; i < partition_count; i++) - { - int endpoint_class = color_formats[i] >> 2; - color_integer_count += (endpoint_class + 1) * 2; - } - - if (color_integer_count > 18) - res->error_block = 1; - - // then, determine the color endpoint format to use for these integers - static const int color_bits_arr[5] = { -1, 115 - 4, 113 - 4 - PARTITION_BITS, 113 - 4 - PARTITION_BITS, 113 - 4 - PARTITION_BITS }; - int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size; - if (is_dual_plane) - color_bits -= 2; - if (color_bits < 0) - color_bits = 0; - - int color_quantization_level = quantization_mode_table[color_integer_count >> 1][color_bits]; - res->color_quantization_level = color_quantization_level; - if (color_quantization_level < 4) - res->error_block = 1; - - - // then unpack the integer-bits - uint8_t values_to_decode[32]; - decode_ise(color_quantization_level, color_integer_count, pb.data, values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_BITS)); - - // and distribute them over the endpoint types - int valuecount_to_decode = 0; - - for (i = 0; i < partition_count; i++) - { - int vals = 2 * (color_formats[i] >> 2) + 2; - for (j = 0; j < vals; j++) - res->color_values[i][j] = values_to_decode[j + valuecount_to_decode]; - valuecount_to_decode += vals; - } - - // get hold of color component for second-plane in the case of dual plane of weights. - if (is_dual_plane) - res->plane2_color_component = read_bits(2, below_weights_pos - 2, pb.data); - -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_weight_align.cpp b/3rdparty/bimg/3rdparty/astc/astc_weight_align.cpp deleted file mode 100644 index 789438fd137..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_weight_align.cpp +++ /dev/null @@ -1,600 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Angular-sum algorithm for weight alignment. - * - * This algorithm works as follows: - * * we compute a complex number P as (cos s*i, sin s*i) for each - * weight, where i is the input value and s is a scaling factor - * based on the spacing between the weights. - * * we then add together complex numbers for all the weights. - * * we then compute the length and angle of the resulting sum. - * - * This should produce the following results: - * * perfect alignment results in a vector whose length is equal to - * the sum of lengths of all inputs - * * even distribution results in a vector of length 0. - * * all samples identical results in perfect alignment for every - * scaling. - * - * For each scaling factor within a given set, we compute an alignment - * factor from 0 to 1. This should then result in some scalings standing - * out as having particularly good alignment factors; we can use this to - * produce a set of candidate scale/shift values for various quantization - * levels; we should then actually try them and see what happens. - * - * Assuming N quantization steps, the scaling factor becomes s=2*PI*(N-1); - * we should probably have about 1 scaling factor for every 1/4 - * quantization step (perhaps 1/8 for low levels of quantization) - */ -/*----------------------------------------------------------------------------*/ - -#define _USE_MATH_DEFINES // for M_PI on windows - -#include <math.h> -#include "astc_codec_internals.h" - -#ifdef DEBUG_PRINT_DIAGNOSTICS - #include <stdio.h> -#endif - -static const float angular_steppings[] = { - 1.0, 1.125, - 1.25, 1.375, - 1.5, 1.625, - 1.75, 1.875, - - 2.0, 2.25, 2.5, 2.75, - 3.0, 3.25, 3.5, 3.75, - 4.0, 4.25, 4.5, 4.75, - 5.0, 5.25, 5.5, 5.75, - 6.0, 6.25, 6.5, 6.75, - 7.0, 7.25, 7.5, 7.75, - - 8.0, 8.5, - 9.0, 9.5, - 10.0, 10.5, - 11.0, 11.5, - 12.0, 12.5, - 13.0, 13.5, - 14.0, 14.5, - 15.0, 15.5, - 16.0, 16.5, - 17.0, 17.5, - 18.0, 18.5, - 19.0, 19.5, - 20.0, 20.5, - 21.0, 21.5, - 22.0, 22.5, - 23.0, 23.5, - 24.0, 24.5, - 25.0, 25.5, - 26.0, 26.5, - 27.0, 27.5, - 28.0, 28.5, - 29.0, 29.5, - 30.0, 30.5, - 31.0, 31.5, - 32.0, 32.5, - 33.0, 33.5, - 34.0, 34.5, - 35.0, 35.5, -}; - -#define ANGULAR_STEPS ((int)(sizeof(angular_steppings)/sizeof(angular_steppings[0]))) - -static float stepsizes[ANGULAR_STEPS]; -static float stepsizes_sqr[ANGULAR_STEPS]; - -static int max_angular_steps_needed_for_quant_level[13]; - -// we store sine/cosine values for 64 possible weight values; this causes -// slight quality loss compared to using sin() and cos() directly. - -#define SINCOS_STEPS 64 - -static float sin_table[SINCOS_STEPS][ANGULAR_STEPS]; -static float cos_table[SINCOS_STEPS][ANGULAR_STEPS]; - -void prepare_angular_tables(void) -{ - int i, j; - int max_angular_steps_needed_for_quant_steps[40]; - for (i = 0; i < ANGULAR_STEPS; i++) - { - stepsizes[i] = 1.0f / angular_steppings[i]; - stepsizes_sqr[i] = stepsizes[i] * stepsizes[i]; - - for (j = 0; j < SINCOS_STEPS; j++) - { - sin_table[j][i] = static_cast < float >(sin((2.0f * M_PI / (SINCOS_STEPS - 1.0f)) * angular_steppings[i] * j)); - cos_table[j][i] = static_cast < float >(cos((2.0f * M_PI / (SINCOS_STEPS - 1.0f)) * angular_steppings[i] * j)); - } - - int p = static_cast < int >(floor(angular_steppings[i])) + 1; - max_angular_steps_needed_for_quant_steps[p] = MIN(i + 1, ANGULAR_STEPS - 1); - } - - - // yes, the next-to-last entry is supposed to have the value 33. This because under - // ASTC, the 32-weight mode leaves a double-sized hole in the middle of the - // weight space, so we are better off matching 33 weights than 32. - static const int steps_of_level[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 33, 36 }; - - for (i = 0; i < 13; i++) - max_angular_steps_needed_for_quant_level[i] = max_angular_steps_needed_for_quant_steps[steps_of_level[i]]; - -} - - -union if32 -{ - float f; - int32_t s; - uint32_t u; -}; - - -// function to compute angular sums; then, from the -// angular sums, compute alignment factor and offset. - -/* static inline */ -void compute_angular_offsets(int samplecount, const float *samples, const float *sample_weights, int max_angular_steps, float *offsets) -{ - int i, j; - - float anglesum_x[ANGULAR_STEPS]; - float anglesum_y[ANGULAR_STEPS]; - - for (i = 0; i < max_angular_steps; i++) - { - anglesum_x[i] = 0; - anglesum_y[i] = 0; - } - - - // compute the angle-sums. - for (i = 0; i < samplecount; i++) - { - float sample = samples[i]; - float sample_weight = sample_weights[i]; - if32 p; - p.f = (sample * (SINCOS_STEPS - 1.0f)) + 12582912.0f; - unsigned int isample = p.u & 0x3F; - - const float *sinptr = sin_table[isample]; - const float *cosptr = cos_table[isample]; - - for (j = 0; j < max_angular_steps; j++) - { - float cp = cosptr[j]; - float sp = sinptr[j]; - - anglesum_x[j] += cp * sample_weight; - anglesum_y[j] += sp * sample_weight; - } - } - - // post-process the angle-sums - for (i = 0; i < max_angular_steps; i++) - { - float angle = atan2(anglesum_y[i], anglesum_x[i]); // positive angle -> positive offset - offsets[i] = angle * (stepsizes[i] * (1.0f / (2.0f * (float)M_PI))); - } -} - - - -// for a given step-size and a given offset, compute the -// lowest and highest weight that results from quantizing using the stepsize & offset. -// also, compute the resulting error. - - -/* static inline */ -void compute_lowest_and_highest_weight(int samplecount, const float *samples, const float *sample_weights, - int max_angular_steps, const float *offsets, - int8_t * lowest_weight, int8_t * highest_weight, - float *error, float *cut_low_weight_error, float *cut_high_weight_error) -{ - int i; - - int sp; - - float error_from_forcing_weight_down[60]; - float error_from_forcing_weight_either_way[60]; - for (i = 0; i < 60; i++) - { - error_from_forcing_weight_down[i] = 0; - error_from_forcing_weight_either_way[i] = 0; - } - - // weight + 12 - static const unsigned int idxtab[256] = { - - 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, - 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 55, 55, 55, 55, - 55, 55, 55, 55, 55, 55, 55, 55, - 55, 55, 55, 55, 55, 55, 55, 55, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 2, 3, - 4, 5, 6, 7, 8, 9, 10, 11, - - 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, - 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 55, 55, 55, 55, - 55, 55, 55, 55, 55, 55, 55, 55, - 55, 55, 55, 55, 55, 55, 55, 55, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 2, 3, - 4, 5, 6, 7, 8, 9, 10, 11 - }; - - - - for (sp = 0; sp < max_angular_steps; sp++) - { - unsigned int minidx_bias12 = 55; - unsigned int maxidx_bias12 = 0; - - float errval = 0.0f; - - float rcp_stepsize = angular_steppings[sp]; - float offset = offsets[sp]; - - float scaled_offset = rcp_stepsize * offset; - - - for (i = 0; i < samplecount - 1; i += 2) - { - float wt1 = sample_weights[i]; - float wt2 = sample_weights[i + 1]; - if32 p1, p2; - float sval1 = (samples[i] * rcp_stepsize) - scaled_offset; - float sval2 = (samples[i + 1] * rcp_stepsize) - scaled_offset; - p1.f = sval1 + 12582912.0f; // FP representation abuse to avoid floor() and float->int conversion - p2.f = sval2 + 12582912.0f; // FP representation abuse to avoid floor() and float->int conversion - float isval1 = p1.f - 12582912.0f; - float isval2 = p2.f - 12582912.0f; - float dif1 = sval1 - isval1; - float dif2 = sval2 - isval2; - - errval += (dif1 * wt1) * dif1; - errval += (dif2 * wt2) * dif2; - - // table lookups that really perform a minmax function. - unsigned int idx1_bias12 = idxtab[p1.u & 0xFF]; - unsigned int idx2_bias12 = idxtab[p2.u & 0xFF]; - - if (idx1_bias12 < minidx_bias12) - minidx_bias12 = idx1_bias12; - if (idx1_bias12 > maxidx_bias12) - maxidx_bias12 = idx1_bias12; - if (idx2_bias12 < minidx_bias12) - minidx_bias12 = idx2_bias12; - if (idx2_bias12 > maxidx_bias12) - maxidx_bias12 = idx2_bias12; - - error_from_forcing_weight_either_way[idx1_bias12] += wt1; - error_from_forcing_weight_down[idx1_bias12] += (dif1 * wt1); - - error_from_forcing_weight_either_way[idx2_bias12] += wt2; - error_from_forcing_weight_down[idx2_bias12] += (dif2 * wt2); - } - - if (samplecount & 1) - { - i = samplecount - 1; - float wt = sample_weights[i]; - if32 p; - float sval = (samples[i] * rcp_stepsize) - scaled_offset; - p.f = sval + 12582912.0f; // FP representation abuse to avoid floor() and float->int conversion - float isval = p.f - 12582912.0f; - float dif = sval - isval; - - errval += (dif * wt) * dif; - - unsigned int idx_bias12 = idxtab[p.u & 0xFF]; - - if (idx_bias12 < minidx_bias12) - minidx_bias12 = idx_bias12; - if (idx_bias12 > maxidx_bias12) - maxidx_bias12 = idx_bias12; - - error_from_forcing_weight_either_way[idx_bias12] += wt; - error_from_forcing_weight_down[idx_bias12] += dif * wt; - } - - - lowest_weight[sp] = (int)minidx_bias12 - 12; - highest_weight[sp] = (int)maxidx_bias12 - 12; - error[sp] = errval; - - // the cut_(lowest/highest)_weight_error indicate the error that results from - // forcing samples that should have had the (lowest/highest) weight value - // one step (up/down). - cut_low_weight_error[sp] = error_from_forcing_weight_either_way[minidx_bias12] - 2.0f * error_from_forcing_weight_down[minidx_bias12]; - cut_high_weight_error[sp] = error_from_forcing_weight_either_way[maxidx_bias12] + 2.0f * error_from_forcing_weight_down[maxidx_bias12]; - - // clear out the error-from-forcing values we actually used in this pass - // so that these are clean for the next pass. - unsigned int ui; - for (ui = minidx_bias12 & ~0x3; ui <= maxidx_bias12; ui += 4) - { - error_from_forcing_weight_either_way[ui] = 0; - error_from_forcing_weight_down[ui] = 0; - error_from_forcing_weight_either_way[ui + 1] = 0; - error_from_forcing_weight_down[ui + 1] = 0; - error_from_forcing_weight_either_way[ui + 2] = 0; - error_from_forcing_weight_down[ui + 2] = 0; - error_from_forcing_weight_either_way[ui + 3] = 0; - error_from_forcing_weight_down[ui + 3] = 0; - } - } - - - for (sp = 0; sp < max_angular_steps; sp++) - { - float errscale = stepsizes_sqr[sp]; - error[sp] *= errscale; - cut_low_weight_error[sp] *= errscale; - cut_high_weight_error[sp] *= errscale; - } -} - - - -// main function for running the angular algorithm. - - -void compute_angular_endpoints_for_quantization_levels(int samplecount, const float *samples, const float *sample_weights, int max_quantization_level, float low_value[12], float high_value[12]) -{ - int i; - - - max_quantization_level++; // Temporarily increase level - needs refinement - - static const int quantization_steps_for_level[13] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 33, 36 }; - int max_quantization_steps = quantization_steps_for_level[max_quantization_level]; - - float offsets[ANGULAR_STEPS]; - - int max_angular_steps = max_angular_steps_needed_for_quant_level[max_quantization_level]; - - compute_angular_offsets(samplecount, samples, sample_weights, max_angular_steps, offsets); - - - // the +4 offsets are to allow for vectorization within compute_lowest_and_highest_weight(). - int8_t lowest_weight[ANGULAR_STEPS + 4]; - int8_t highest_weight[ANGULAR_STEPS + 4]; - float error[ANGULAR_STEPS + 4]; - - float cut_low_weight_error[ANGULAR_STEPS + 4]; - float cut_high_weight_error[ANGULAR_STEPS + 4]; - - compute_lowest_and_highest_weight(samplecount, samples, sample_weights, max_angular_steps, offsets, lowest_weight, highest_weight, error, cut_low_weight_error, cut_high_weight_error); - - - #ifdef DEBUG_PRINT_DIAGNOSTICS - if (print_diagnostics) - { - printf("%s : max-angular-steps=%d \n", __func__, max_angular_steps); - printf("Samplecount=%d, max_quantization_level=%d\n", samplecount, max_quantization_level); - for (i = 0; i < samplecount; i++) - printf("Sample %d : %f (weight %f)\n", i, samples[i], sample_weights[i]); - - for (i = 0; i < max_angular_steps; i++) - { - printf("%d: offset=%f error=%f lowest=%d highest=%d cl=%f ch=%f\n", i, offsets[i], error[i], lowest_weight[i], highest_weight[i], cut_low_weight_error[i], cut_high_weight_error[i]); - } - printf("\n"); - } - #endif - - // for each quantization level, find the best error terms. - float best_errors[40]; - int best_scale[40]; - uint8_t cut_low_weight[40]; - - for (i = 0; i < (max_quantization_steps + 4); i++) - { - best_errors[i] = 1e30f; - best_scale[i] = -1; // Indicates no solution found - cut_low_weight[i] = 0; - } - - - - for (i = 0; i < max_angular_steps; i++) - { - int samplecount = highest_weight[i] - lowest_weight[i] + 1; - if (samplecount >= (max_quantization_steps + 4)) - { - continue; - } - if (samplecount < 2) - samplecount = 2; - - if (best_errors[samplecount] > error[i]) - { - best_errors[samplecount] = error[i]; - best_scale[samplecount] = i; - cut_low_weight[samplecount] = 0; - } - - float error_cut_low = error[i] + cut_low_weight_error[i]; - float error_cut_high = error[i] + cut_high_weight_error[i]; - float error_cut_low_high = error[i] + cut_low_weight_error[i] + cut_high_weight_error[i]; - - if (best_errors[samplecount - 1] > error_cut_low) - { - best_errors[samplecount - 1] = error_cut_low; - best_scale[samplecount - 1] = i; - cut_low_weight[samplecount - 1] = 1; - } - - if (best_errors[samplecount - 1] > error_cut_high) - { - best_errors[samplecount - 1] = error_cut_high; - best_scale[samplecount - 1] = i; - cut_low_weight[samplecount - 1] = 0; - } - - if (best_errors[samplecount - 2] > error_cut_low_high) - { - best_errors[samplecount - 2] = error_cut_low_high; - best_scale[samplecount - 2] = i; - cut_low_weight[samplecount - 2] = 1; - } - - } - - // if we got a better error-value for a low sample count than for a high one, - // use the low sample count error value for the higher sample count as well. - for (i = 3; i <= max_quantization_steps; i++) - { - if (best_errors[i] > best_errors[i - 1]) - { - best_errors[i] = best_errors[i - 1]; - best_scale[i] = best_scale[i - 1]; - cut_low_weight[i] = cut_low_weight[i - 1]; - } - } - - - max_quantization_level--; // Decrease level again (see corresponding ++, above) - - static const int ql_weights[12] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 33 }; - for (i = 0; i <= max_quantization_level; i++) - { - int q = ql_weights[i]; - int bsi = best_scale[q]; - - // Did we find anything? - if(bsi < 0) - { - printf("ERROR: Unable to find an encoding within the specified error limits. Please revise the error limit values and try again.\n"); - exit(1); - } - - float stepsize = stepsizes[bsi]; - int lwi = lowest_weight[bsi] + cut_low_weight[q]; - int hwi = lwi + q - 1; - float offset = offsets[bsi]; - - low_value[i] = offset + lwi * stepsize; - high_value[i] = offset + hwi * stepsize; - } - -} - - -// helper functions that will compute ideal angular-endpoints -// for a given set of weights and a given block size descriptors - -void compute_angular_endpoints_1plane(float mode_cutoff, const block_size_descriptor * bsd, - const float *decimated_quantized_weights, const float *decimated_weights, - float low_value[MAX_WEIGHT_MODES], float high_value[MAX_WEIGHT_MODES]) -{ - int i; - float low_values[MAX_DECIMATION_MODES][12]; - float high_values[MAX_DECIMATION_MODES][12]; - - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - int samplecount = bsd->decimation_mode_samples[i]; - int quant_mode = bsd->decimation_mode_maxprec_1plane[i]; - float percentile = bsd->decimation_mode_percentile[i]; - int permit_encode = bsd->permit_encode[i]; - if (permit_encode == 0 || samplecount < 1 || quant_mode < 0 || percentile > mode_cutoff) - continue; - - - compute_angular_endpoints_for_quantization_levels(samplecount, - decimated_quantized_weights + i * MAX_WEIGHTS_PER_BLOCK, - decimated_weights + i * MAX_WEIGHTS_PER_BLOCK, quant_mode, low_values[i], high_values[i]); - } - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (bsd->block_modes[i].is_dual_plane != 0 || bsd->block_modes[i].percentile > mode_cutoff) - continue; - int quant_mode = bsd->block_modes[i].quantization_mode; - int decim_mode = bsd->block_modes[i].decimation_mode; - - low_value[i] = low_values[decim_mode][quant_mode]; - high_value[i] = high_values[decim_mode][quant_mode]; - } - -} - - - -void compute_angular_endpoints_2planes(float mode_cutoff, - const block_size_descriptor * bsd, - const float *decimated_quantized_weights, - const float *decimated_weights, - float low_value1[MAX_WEIGHT_MODES], float high_value1[MAX_WEIGHT_MODES], float low_value2[MAX_WEIGHT_MODES], float high_value2[MAX_WEIGHT_MODES]) -{ - int i; - float low_values1[MAX_DECIMATION_MODES][12]; - float high_values1[MAX_DECIMATION_MODES][12]; - float low_values2[MAX_DECIMATION_MODES][12]; - float high_values2[MAX_DECIMATION_MODES][12]; - - for (i = 0; i < MAX_DECIMATION_MODES; i++) - { - int samplecount = bsd->decimation_mode_samples[i]; - int quant_mode = bsd->decimation_mode_maxprec_2planes[i]; - float percentile = bsd->decimation_mode_percentile[i]; - int permit_encode = bsd->permit_encode[i]; - if (permit_encode == 0 || samplecount < 1 || quant_mode < 0 || percentile > mode_cutoff) - continue; - - compute_angular_endpoints_for_quantization_levels(samplecount, - decimated_quantized_weights + 2 * i * MAX_WEIGHTS_PER_BLOCK, - decimated_weights + 2 * i * MAX_WEIGHTS_PER_BLOCK, quant_mode, low_values1[i], high_values1[i]); - - compute_angular_endpoints_for_quantization_levels(samplecount, - decimated_quantized_weights + (2 * i + 1) * MAX_WEIGHTS_PER_BLOCK, - decimated_weights + (2 * i + 1) * MAX_WEIGHTS_PER_BLOCK, quant_mode, low_values2[i], high_values2[i]); - - } - - for (i = 0; i < MAX_WEIGHT_MODES; i++) - { - if (bsd->block_modes[i].is_dual_plane != 1 || bsd->block_modes[i].percentile > mode_cutoff) - continue; - int quant_mode = bsd->block_modes[i].quantization_mode; - int decim_mode = bsd->block_modes[i].decimation_mode; - - low_value1[i] = low_values1[decim_mode][quant_mode]; - high_value1[i] = high_values1[decim_mode][quant_mode]; - low_value2[i] = low_values2[decim_mode][quant_mode]; - high_value2[i] = high_values2[decim_mode][quant_mode]; - } -} diff --git a/3rdparty/bimg/3rdparty/astc/astc_weight_quant_xfer_tables.cpp b/3rdparty/bimg/3rdparty/astc/astc_weight_quant_xfer_tables.cpp deleted file mode 100644 index 4294735021d..00000000000 --- a/3rdparty/bimg/3rdparty/astc/astc_weight_quant_xfer_tables.cpp +++ /dev/null @@ -1,1003 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Quantization transfer tables for ASTC. - */ -/*----------------------------------------------------------------------------*/ - - -#include "astc_codec_internals.h" - -const quantization_and_transfer_table quant_and_xfer_tables[12] = { - // quantization method 0, range 0..1 - { - QUANT_2, - {0, 64,}, - { - 0.000000, 1.000000,}, - {0, 0,}, - {1, 1,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 1, range 0..2 - { - QUANT_3, - {0, 32, 64,}, - { - 0.000000, 0.500000, 1.000000,}, - {0, 0, 1,}, - {1, 2, 2,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, - }, - }, - - - - - // quantization method 2, range 0..3 - { - QUANT_4, - {0, 21, 43, 64,}, - { - 0.000000, 0.328125, 0.671875, 1.000000,}, - {0, 0, 1, 2,}, - {1, 2, 3, 3,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, - }, - }, - - - - - // quantization method 3, range 0..4 - { - QUANT_5, - {0, 16, 32, 48, 64,}, - { - 0.000000, 0.250000, 0.500000, 0.750000, - 1.000000,}, - {0, 0, 1, 2, 3,}, - {1, 2, 3, 4, 4,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, - }, - }, - - - - - // quantization method 4, range 0..5 - { - QUANT_6, - {0, 64, 12, 52, 25, 39,}, - { - 0.000000, 1.000000, 0.187500, 0.812500, - 0.390625, 0.609375,}, - {0, 3, 0, 5, 2, 4,}, - {2, 1, 4, 1, 5, 3,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 5, range 0..7 - { - QUANT_8, - {0, 9, 18, 27, 37, 46, 55, 64,}, - { - 0.000000, 0.140625, 0.281250, 0.421875, - 0.578125, 0.718750, 0.859375, 1.000000,}, - {0, 0, 1, 2, 3, 4, 5, 6,}, - {1, 2, 3, 4, 5, 6, 7, 7,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, - }, - }, - - - - - // quantization method 6, range 0..9 - { - QUANT_10, - {0, 64, 7, 57, 14, 50, 21, 43, 28, 36,}, - { - 0.000000, 1.000000, 0.109375, 0.890625, - 0.218750, 0.781250, 0.328125, 0.671875, - 0.437500, 0.562500,}, - {0, 3, 0, 5, 2, 7, 4, 9, 6, 8,}, - {2, 1, 4, 1, 6, 3, 8, 5, 9, 7,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 7, range 0..11 - { - QUANT_12, - {0, 64, 17, 47, 5, 59, 23, 41, 11, 53, 28, 36,}, - { - 0.000000, 1.000000, 0.265625, 0.734375, - 0.078125, 0.921875, 0.359375, 0.640625, - 0.171875, 0.828125, 0.437500, 0.562500,}, - {0, 5, 8, 7, 0, 9, 2, 11, 4, 3, 6, 10,}, - {4, 1, 6, 9, 8, 1, 10, 3, 2, 5, 11, 7,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 8, range 0..15 - { - QUANT_16, - {0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64,}, - { - 0.000000, 0.062500, 0.125000, 0.187500, - 0.265625, 0.328125, 0.390625, 0.453125, - 0.546875, 0.609375, 0.671875, 0.734375, - 0.812500, 0.875000, 0.937500, 1.000000,}, - {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,}, - {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, - }, - }, - - - - - // quantization method 9, range 0..19 - { - QUANT_20, - {0, 64, 16, 48, 3, 61, 19, 45, 6, 58, 23, 41, 9, 55, 26, 38, 13, 51, 29, 35,}, - { - 0.000000, 1.000000, 0.250000, 0.750000, - 0.046875, 0.953125, 0.296875, 0.703125, - 0.093750, 0.906250, 0.359375, 0.640625, - 0.140625, 0.859375, 0.406250, 0.593750, - 0.203125, 0.796875, 0.453125, 0.546875,}, - {0, 5, 16, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 17, 10, 19, 12, 3, 14, 18,}, - {4, 1, 6, 17, 8, 1, 10, 3, 12, 5, 14, 7, 16, 9, 18, 11, 2, 13, 19, 15,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 10, range 0..23 - { - QUANT_24, - {0, 64, 8, 56, 16, 48, 24, 40, 2, 62, 11, 53, 19, 45, 27, 37, 5, 59, 13, 51, 22, 42, 30, 34,}, - { - 0.000000, 1.000000, 0.125000, 0.875000, - 0.250000, 0.750000, 0.375000, 0.625000, - 0.031250, 0.968750, 0.171875, 0.828125, - 0.296875, 0.703125, 0.421875, 0.578125, - 0.078125, 0.921875, 0.203125, 0.796875, - 0.343750, 0.656250, 0.468750, 0.531250,}, - {0, 9, 16, 11, 18, 13, 20, 15, 0, 17, 2, 19, 4, 21, 6, 23, 8, 3, 10, 5, 12, 7, 14, 22,}, - {8, 1, 10, 17, 12, 19, 14, 21, 16, 1, 18, 3, 20, 5, 22, 7, 2, 9, 4, 11, 6, 13, 23, 15,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 20, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 22, 22, 22, 22, 22, 22, 22, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 21, 21, 21, 21, 21, 21, 21, 21, 21, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, - }, - }, - - - - - // quantization method 11, range 0..31 - { - QUANT_32, - {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64,}, - { - 0.000000, 0.031250, 0.062500, 0.093750, - 0.125000, 0.156250, 0.187500, 0.218750, - 0.250000, 0.281250, 0.312500, 0.343750, - 0.375000, 0.406250, 0.437500, 0.468750, - 0.531250, 0.562500, 0.593750, 0.625000, - 0.656250, 0.687500, 0.718750, 0.750000, - 0.781250, 0.812500, 0.843750, 0.875000, - 0.906250, 0.937500, 0.968750, 1.000000,}, - {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,}, - {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31,}, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, - 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, - 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, - 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, - 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, - 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, - 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, - 31, - }, - }, - -}; diff --git a/3rdparty/bimg/3rdparty/astc/license.txt b/3rdparty/bimg/3rdparty/astc/license.txt deleted file mode 100644 index a33bccd57e7..00000000000 --- a/3rdparty/bimg/3rdparty/astc/license.txt +++ /dev/null @@ -1,137 +0,0 @@ -END USER LICENCE AGREEMENT FOR THE MALI ASTC SPECIFICATION AND SOFTWARE CODEC, -VERSION: 1.3 - -THIS END USER LICENCE AGREEMENT ("LICENCE") IS A LEGAL AGREEMENT BETWEEN YOU -(EITHER A SINGLE INDIVIDUAL, OR SINGLE LEGAL ENTITY) AND ARM LIMITED ("ARM") -FOR THE USE OF THE SOFTWARE ACCOMPANYING THIS LICENCE. ARM IS ONLY WILLING -TO LICENSE THE SOFTWARE TO YOU ON CONDITION THAT YOU ACCEPT ALL OF THE TERMS -IN THIS LICENCE. BY CLICKING "I AGREE" OR BY INSTALLING OR OTHERWISE USING -OR COPYING THE SOFTWARE YOU INDICATE THAT YOU AGREE TO BE BOUND BY ALL THE -TERMS OF THIS LICENCE. - -IF YOU DO NOT AGREE TO THE TERMS OF THIS LICENCE, ARM IS UNWILLING TO LICENSE -THE SOFTWARE TO YOU AND YOU MAY NOT INSTALL, USE OR COPY THE SOFTWARE. - -1. DEFINITIONS. - -"Authorised Purpose" means the use of the Software solely to develop products -and tools which implement the Khronos ASTC specification to; -(i) compress texture images into ASTC format ("Compression Results"); -(ii) distribute such Compression Results to third parties; and -(iii) decompress texture images stored in ASTC format. - -"Software" means the source code and Software binaries accompanying this -Licence, and any printed, electronic or online documentation supplied with it, -in all cases relating to the MALI ASTC SPECIFICATION AND SOFTWARE CODEC. - -2. LICENCE GRANT. - -ARM hereby grants to you, subject to the terms and conditions of this Licence, -a nonexclusive, nontransferable, free of charge, royalty free, worldwide -licence to use, copy, modify and (subject to Clause 3 below) distribute the -Software solely for the Authorised Purpose. - -No right is granted to use the Software to develop hardware. - -Notwithstanding the foregoing, nothing in this Licence prevents you from -using the Software to develop products that conform to an application -programming interface specification issued by The Khronos Group Inc. -("Khronos"), provided that you have licences to develop such products -under the relevant Khronos agreements. - - 3. RESTRICTIONS ON USE OF THE SOFTWARE. - -RESTRICTIONS ON TRANSFER OF LICENSED RIGHTS: The rights granted to you under -this Licence may not be assigned by you to any third party without the prior -written consent of ARM. - -TITLE AND RESERVATION OF RIGHTS: You acquire no rights to the Software other -than as expressly provided by this Licence. The Software is licensed not sold. -ARM does not transfer title to the Software to you. In no event shall the -licences granted in Clause 2 be construed as granting you expressly or by -implication, estoppel or otherwise, licences to any ARM technology other than -the Software. - -NOTICES: You shall not remove from the Software any copyright notice or other -notice (whether ARM's or its licensor's), and you shall ensure that any such -notice is reproduced in any copies of the whole or any part of the Software -made by you. You shall not use ARM's or its licensor's name, logo or -trademarks to market Compression Results. If you distribute the Software to a -third party, you agree to include a copy of this Licence with such -distribution. - -4. NO SUPPORT. - -ARM has no obligation to support or to continue providing or updating any of -the Software. - -5. NO WARRANTIES. - -YOU AGREE THAT THE SOFTWARE IS LICENSED "AS IS", AND THAT ARM EXPRESSLY -DISCLAIMS ALL REPRESENTATIONS, WARRANTIES, CONDITIONS OR OTHER TERMS, EXPRESS, -IMPLIED OR STATUTORY, TO THE FULLEST EXTENT PERMITTED BY LAW. YOU EXPRESSLY -ASSUME ALL LIABILITIES AND RISKS, FOR USE OR OPERATION OF ANY APPLICATION -PROGRAMS YOU CREATE WITH THE SOFTWARE, AND YOU ASSUME THE ENTIRE COST OF ALL -NECESSARY SERVICING, REPAIR OR CORRECTION. - -6. LIMITATION OF LIABILITY. - -TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL ARM BE -LIABLE FOR ANY INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES -(INCLUDING LOSS OF PROFITS) ARISING OUT OF THE USE OR INABILITY TO USE THE -SOFTWARE WHETHER BASED ON A CLAIM UNDER CONTRACT, TORT OR OTHER LEGAL THEORY, -EVEN IF ARM WAS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -ARM does not seek to limit or exclude liability for death or personal injury -arising from ARM's negligence and because some jurisdictions do not permit the -exclusion or limitation of liability for consequential or incidental damages -the above limitation relating to liability for consequential damages may not -apply to you. - -NOTWITHSTANDING ANYTHING TO THE CONTRARY CONTAINED IN THIS LICENCE, THE -MAXIMUM LIABILITY OF ARM TO YOU IN AGGREGATE FOR ALL CLAIMS MADE AGAINST ARM -IN CONTRACT TORT OR OTHERWISE UNDER OR IN CONNECTION WITH THE SUBJECT MATTER -OF THIS LICENCE SHALL NOT EXCEED THE GREATER OF THE TOTAL OF SUMS PAID BY YOU -TO ARM (IF ANY) FOR THIS LICENCE AND US$5.00. - -7. U.S. GOVERNMENT END USERS. - -US Government Restrictions: Use, duplication, reproduction, release, -modification, disclosure or transfer of this commercial product and -accompanying documentation is restricted in accordance with the terms -of this Licence. - -8. TERM AND TERMINATION. - -This Licence shall remain in force until terminated by you or by ARM. Without -prejudice to any of its other rights if you are in breach of any of the terms -and conditions of this Licence then ARM may terminate this Licence immediately -upon giving written notice to you. You may terminate this Licence at any time. - -Upon termination of this Licence by you or by ARM you shall stop using the -Software and destroy all copies of the Software in your possession together -with all documentation and related materials. The provisions of Clauses 1, 3, -4, 5, 6, 7, 8 and 9 shall survive termination of this Licence. - -9. GENERAL. - -This Licence is governed by English Law. Except where ARM agrees otherwise in -a written contract signed by you and ARM, this is the only agreement between -you and ARM relating to the Software and it may only be modified by written -agreement between you and ARM. Except as expressly agreed in writing, this -Licence may not be modified by purchase orders, advertising or other -representation by any person. If any clause in this Licence is held by a court -of law to be illegal or unenforceable the remaining provisions of this Licence -shall not be affected thereby. The failure by ARM to enforce any of the -provisions of this Licence, unless waived in writing, shall not constitute a -waiver of ARM's rights to enforce such provision or any other provision of -this Licence in the future. - -You agree to comply fully with all laws and regulations of the United States -and other countries ("Export Laws") to assure that the Software is not; -(1) exported, directly or indirectly, in violation of Export Laws, either to -any countries that are subject to U.S.A. export restrictions or to any end -user who has been prohibited from participating in the U.S.A. export -transactions by any federal agency of the U.S.A. government; or -(2) intended to be used for any purpose prohibited by Export Laws, including, -without limitation, nuclear, chemical, or biological weapons proliferation. diff --git a/3rdparty/bimg/3rdparty/astc/mathlib.cpp b/3rdparty/bimg/3rdparty/astc/mathlib.cpp deleted file mode 100644 index aa6eac1ff5b..00000000000 --- a/3rdparty/bimg/3rdparty/astc/mathlib.cpp +++ /dev/null @@ -1,781 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Library of math functions. - */ -/*----------------------------------------------------------------------------*/ - -#define _USE_MATH_DEFINES // for M_PI on windows - -#include <time.h> -#include <stdlib.h> -#include <stdio.h> -#include <math.h> -#include "mathlib.h" - -#ifdef WIN32 - double cbrt(double n) - { - return n < 0 ? -pow(-n, 1.0 / 3.0) : pow(n, 1.0 / 3.0); - } -#endif - -/************************** - basic OpenCL functions -**************************/ - -float inversesqrt(float p) -{ - return 1.0f / sqrt(p); -} -float acospi(float p) -{ - return static_cast < float >(acos(p) * (1.0f / M_PI)); -}; -float sinpi(float p) -{ - return static_cast < float >(sin(p * M_PI)); -} -float cospi(float p) -{ - return static_cast < float >(cos(p * M_PI)); -} - - -float nan(int p) -{ - union - { - int p; - float q; - } v; - v.p = p | 0x7FC00000U; - return v.q; -} - -#if !defined(_MSC_VER) && (__cplusplus < 201103L) -float fmax(float p, float q) -{ - if (p != p) - return q; - if (q != q) - return p; - if (p > q) - return p; - return q; -} - -float fmin(float p, float q) -{ - if (p != p) - return q; - if (q != q) - return p; - if (p < q) - return p; - return q; -} -#endif // C++11 - -float2 fmax(float2 p, float2 q) -{ - return float2(fmax(p.x, q.x), fmax(p.y, q.y)); -} - -float3 fmax(float3 p, float3 q) -{ - return float3(fmax(p.x, q.x), fmax(p.y, q.y), fmax(p.z, q.z)); -} - -float4 fmax(float4 p, float4 q) -{ - return float4(fmax(p.x, q.x), fmax(p.y, q.y), fmax(p.z, q.z), fmax(p.w, q.w)); -} - - -float2 fmin(float2 p, float2 q) -{ - return float2(fmin(p.x, q.x), fmin(p.y, q.y)); -} - -float3 fmin(float3 p, float3 q) -{ - return float3(fmin(p.x, q.x), fmin(p.y, q.y), fmin(p.z, q.z)); -} - -float4 fmin(float4 p, float4 q) -{ - return float4(fmin(p.x, q.x), fmin(p.y, q.y), fmin(p.z, q.z), fmin(p.w, q.w)); -} - -/* - float dot( float2 p, float2 q ) { return p.x*q.x + p.y*q.y; } float dot( float3 p, float3 q ) { return p.x*q.x + p.y*q.y + p.z*q.z; } float dot( float4 p, float4 q ) { return p.x*q.x + p.y*q.y + - p.z*q.z + p.w*q.w; } */ - -float3 cross(float3 p, float3 q) -{ - return p.yzx * q.zxy - p.zxy * q.yzx; -} - -float4 cross(float4 p, float4 q) -{ - return float4(p.yzx * q.zxy - p.zxy * q.yzx, 0.0f); -} - -float length(float2 p) -{ - return sqrt(dot(p, p)); -} - -float length(float3 p) -{ - return sqrt(dot(p, p)); -} - -float length(float4 p) -{ - return sqrt(dot(p, p)); -} - -float length_sqr(float2 p) -{ - return dot(p, p); -} - -float length_sqr(float3 p) -{ - return dot(p, p); -} - -float length_sqr(float4 p) -{ - return dot(p, p); -} - - -float distance(float2 p, float2 q) -{ - return length(q - p); -} - -float distance(float3 p, float3 q) -{ - return length(q - p); -} - -float distance(float4 p, float4 q) -{ - return length(q - p); -} - -float distance_sqr(float2 p, float2 q) -{ - return length_sqr(q - p); -} - -float distance_sqr(float3 p, float3 q) -{ - return length_sqr(q - p); -} - -float distance_sqr(float4 p, float4 q) -{ - return length_sqr(q - p); -} - - -float2 normalize(float2 p) -{ - return p / length(p); -} - -float3 normalize(float3 p) -{ - return p / length(p); -} - -float4 normalize(float4 p) -{ - return p / length(p); -} - - -/************************************************** - matrix functions, for 2x2, 3x3 and 4x4 matrices: - - * trace - * determinant - * transform - * inverse - * adjugate - * characteristic polynomial - * eigenvalue - * eigenvector - - additionally, root solver - for 2nd, 3rd and 4th degree monic polynomials. - -*************************************************/ - -/* - struct mat2 { float2 v[2]; }; - struct mat3 { float3 v[3]; }; - struct mat4 { float4 v[4]; }; -*/ - -float trace(mat2 p) -{ - return p.v[0].x + p.v[1].y; -} - -float trace(mat3 p) -{ - return p.v[0].x + p.v[1].y + p.v[2].z; -} - -float trace(mat4 p) -{ - return p.v[0].x + p.v[1].y + p.v[2].z + p.v[3].w; -} - -float determinant(mat2 p) -{ - float2 v = p.v[0].xy * p.v[1].yx; - return v.x - v.y; -} - -float determinant(mat3 p) -{ - return dot(p.v[0], cross(p.v[1], p.v[2])); -} - -float determinant(mat4 p) -{ - return dot(p.v[0], - float4(dot(p.v[1].yzw, cross(p.v[2].yzw, p.v[3].yzw)), - -dot(p.v[1].xzw, cross(p.v[2].xzw, p.v[3].xzw)), dot(p.v[1].xyw, cross(p.v[2].xyw, p.v[3].xyw)), -dot(p.v[1].xyz, cross(p.v[2].xyz, p.v[3].xyz)))); -} - - -/* - characteristic polynomials for matrices. These polynomials are monic, meaning that the coefficient of the highest component is 1; this component is omitted. The first component is the constant - part. */ - -float2 characteristic_poly(mat2 p) -{ - return float2(determinant(p), -trace(p)); -} - - -float3 characteristic_poly(mat3 p) -{ - float2 v1 = (p.v[0].xy * p.v[1].yx) + (p.v[0].xz * p.v[2].zx) + (p.v[1].yz * p.v[2].zy); - - return float3(-determinant(p), v1.x - v1.y, -trace(p)); -} - - -float4 characteristic_poly(mat4 p) -{ - float2 v1 = (p.v[0].xy * p.v[1].yx) + (p.v[0].xz * p.v[2].zx) + (p.v[0].xw * p.v[3].wx) + (p.v[1].yz * p.v[2].zy) + (p.v[1].yw * p.v[3].wy) + (p.v[2].zw * p.v[3].wz); - - return float4(determinant(p), - -dot(p.v[1].yzw, cross(p.v[2].yzw, p.v[3].yzw)) - - dot(p.v[0].xzw, cross(p.v[2].xzw, p.v[3].xzw)) - dot(p.v[0].xyw, cross(p.v[1].xyw, p.v[3].xyw)) - dot(p.v[0].xyz, cross(p.v[1].xyz, p.v[2].xyz)), v1.x - v1.y, -trace(p)); -} - - -/* - Root finders for monic polynomials (highest coefficient is equal to 1) - - Returns a vector with length equal to the number of roots that the polynomial has; - for roots that do not genuinely exist, we return NaN. - - The polynomial is basically - - poly(n) = p.x + p.y*n + p.z*n^2 + p.w*n^3 - - (including only the components of the vector that actually exist; the next coefficient - has the value 1, and the remaining ones have value 0. ) - */ - - -float2 solve_monic(float2 p) -{ - float v = sqrt(p.y * p.y - 4 * p.x); - return (p.yy + float2(v, -v)) * -0.5f; -} - -float3 solve_monic(float3 p) -{ - - p = p * (1.0f / 3.0f); - - float pz = p.z; - - // compute a normalization value to scale the vector by. - // The normalization factor is divided by 2^20. - // This is supposed to make internal calculations unlikely - // to overflow while also making underflows unlikely. - float scal = 1.0f; - - float cx = static_cast < float >(cbrt(fabs(p.x))); - float cy = static_cast < float >(cbrt(fabs(p.y))); - scal = fmax(fmax(fabsf(p.z), cx), cy * cy) * (1.0f / 1048576.0f); - float rscal = 1.0f / scal; - p = p * float3(rscal * rscal * rscal, rscal * rscal, rscal); - - float bb = p.z * p.z; // div scal^2 - - float nq = bb - p.y; // div scal^2 - float r = 1.5f * (p.y * p.z - p.x) - p.z * bb; // div scal^3 - float nq3 = nq * nq * nq; // div scal^6 - float r2 = r * r; // div scal^6 - - if (nq3 < r2) - { - // one root - float root = sqrt(r2 - nq3); // div scal^3 - float s = static_cast < float >(cbrt(r + root)); // div scal - float t = static_cast < float >(cbrt(r - root)); // div scal - return float3((s + t) * scal - pz, nan(0), nan(0)); - } - else - { - // three roots - float phi_r = inversesqrt(nq3); // div scal ^ -3 - float phi_root = static_cast < float >(cbrt(phi_r * nq3)); // div scal - float theta = acospi(r * phi_r); - theta *= 1.0f / 3.0f; - float ncprod = phi_root * cospi(theta); - float dev = 1.73205080756887729353f * phi_root * sinpi(theta); - return float3(2 * ncprod, -dev - ncprod, dev - ncprod) * scal - pz; - } -} - - -/* - * This function is not overflow-safe. Use with care. - */ -float4 solve_monic(float4 p) -{ - - // step 1: depress the input polynomial - float bias = p.w * 0.25f; - float3 qv = float3((-3.0f / 256.0f) * p.w * p.w, (1.0f / 8.0f) * p.w, (-3.0 / 8.0f)); - float3 rv = float3((1.0f / 16.0f) * p.z * p.w - (1.0f / 4.0f) * p.y, (-1.0f / 2.0f) * p.z, 0.0f); - float3 qx = float3(qv * p.w + rv) * p.w + p.xyz; - - // step 2: solve a cubic equation to get hold of a parameter p. - float3 monicp = float3(-qx.y * qx.y, (qx.z * qx.z) - (4.0f * qx.x), 2.0f * qx.z); - float4 v = float4(solve_monic(monicp), 1e-37f); - - // the cubic equation may have multiple solutions; at least one of them - // is numerically at least nonnegative (but may have become negative as a result of - // a roundoff error). We use fmax() to extract this value or a very small positive value. - float2 v2 = fmax(v.xy, v.zw); - float p2 = fmax(v2.x, v2.y); // p^2 - float pr = inversesqrt(p2); // 1/p - float pm = p2 * pr; // p - - // step 3: use the solution for the cubic equation to set up two quadratic equations; - // these two equations then result in the 4 possible roots. - float f1 = qx.z + p2; - float f2 = qx.y * pr; - float s = 0.5f * (f1 + f2); - float q = 0.5f * (f1 - f2); - - float4 res = float4(solve_monic(float2(q, pm)), - solve_monic(float2(s, -pm))); - - // finally, order the results and apply the bias. - if (res.x != res.x) - return res.zwxy - bias; - else - return res - bias; -} - - - -float2 transform(mat2 p, float2 q) -{ - return float2(dot(p.v[0], q), dot(p.v[1], q)); -} - - -float3 transform(mat3 p, float3 q) -{ - return float3(dot(p.v[0], q), dot(p.v[1], q), dot(p.v[2], q)); -} - - -float4 transform(mat4 p, float4 q) -{ - return float4(dot(p.v[0], q), dot(p.v[1], q), dot(p.v[2], q), dot(p.v[3], q)); -} - - - -mat2 adjugate(mat2 p) -{ - mat2 res; - res.v[0] = float2(p.v[1].y, -p.v[0].y); - res.v[1] = float2(-p.v[1].x, p.v[0].x); - return res; -} - - - -mat2 invert(mat2 p) -{ - float rdet = 1.0f / determinant(p); - mat2 res; - res.v[0] = float2(p.v[1].y, -p.v[0].y) * rdet; - res.v[1] = float2(-p.v[1].x, p.v[0].x) * rdet; - return res; -} - - - -mat3 adjugate(mat3 p) -{ - mat3 res; - float3 prd0 = cross(p.v[1], p.v[2]); - float3 prd1 = cross(p.v[2], p.v[0]); - float3 prd2 = cross(p.v[0], p.v[1]); - res.v[0] = float3(prd0.x, prd1.x, prd2.x); - res.v[1] = float3(prd0.y, prd1.y, prd2.y); - res.v[2] = float3(prd0.z, prd1.z, prd2.z); - return res; -} - - - -mat3 invert(mat3 p) -{ - float3 cross0 = cross(p.v[1], p.v[2]); - float det = dot(cross0, p.v[0]); - float rdet = 1.0f / det; - mat3 res; - float3 prd0 = cross0 * rdet; - float3 prd1 = cross(p.v[2], p.v[0]) * rdet; - float3 prd2 = cross(p.v[0], p.v[1]) * rdet; - res.v[0] = float3(prd0.x, prd1.x, prd2.x); - res.v[1] = float3(prd0.y, prd1.y, prd2.y); - res.v[2] = float3(prd0.z, prd1.z, prd2.z); - return res; -} - - - -mat4 adjugate(mat4 p) -{ - mat4 res; - - float3 bpc0 = cross(p.v[2].yzw, p.v[3].yzw); - float3 tpc0 = cross(p.v[0].yzw, p.v[1].yzw); - res.v[0] = float4(dot(bpc0, p.v[1].yzw), -dot(bpc0, p.v[0].yzw), dot(tpc0, p.v[3].yzw), -dot(tpc0, p.v[2].yzw)); - - float3 bpc1 = cross(p.v[2].xzw, p.v[3].xzw); - float3 tpc1 = cross(p.v[0].xzw, p.v[1].xzw); - res.v[1] = float4(-dot(bpc1, p.v[1].xzw), dot(bpc1, p.v[0].xzw), -dot(tpc1, p.v[3].xzw), dot(tpc1, p.v[2].xzw)); - - float3 bpc2 = cross(p.v[2].xyw, p.v[3].xyw); - float3 tpc2 = cross(p.v[0].xyw, p.v[1].xyw); - res.v[2] = float4(dot(bpc2, p.v[1].xyw), -dot(bpc2, p.v[0].xyw), dot(tpc2, p.v[3].xyw), -dot(tpc2, p.v[2].xyw)); - - float3 bpc3 = cross(p.v[2].xyz, p.v[3].xyz); - float3 tpc3 = cross(p.v[0].xyz, p.v[1].xyz); - res.v[3] = float4(-dot(bpc3, p.v[1].xyz), dot(bpc3, p.v[0].xyz), -dot(tpc3, p.v[3].xyz), dot(tpc3, p.v[2].xyz)); - - return res; -} - - - -mat4 invert(mat4 p) -{ - // cross products between the bottom two rows - float3 bpc0 = cross(p.v[2].yzw, p.v[3].yzw); - float3 bpc1 = cross(p.v[2].xzw, p.v[3].xzw); - float3 bpc2 = cross(p.v[2].xyw, p.v[3].xyw); - float3 bpc3 = cross(p.v[2].xyz, p.v[3].xyz); - - // dot-products for the top rows - float4 row1 = float4(dot(bpc0, p.v[1].yzw), - -dot(bpc1, p.v[1].xzw), - dot(bpc2, p.v[1].xyw), - -dot(bpc3, p.v[1].xyz)); - - float det = dot(p.v[0], row1); - float rdet = 1.0f / det; - - mat4 res; - - float3 tpc0 = cross(p.v[0].yzw, p.v[1].yzw); - res.v[0] = float4(row1.x, -dot(bpc0, p.v[0].yzw), dot(tpc0, p.v[3].yzw), -dot(tpc0, p.v[2].yzw)) * rdet; - - float3 tpc1 = cross(p.v[0].xzw, p.v[1].xzw); - res.v[1] = float4(row1.y, dot(bpc1, p.v[0].xzw), -dot(tpc1, p.v[3].xzw), dot(tpc1, p.v[2].xzw)) * rdet; - float3 tpc2 = cross(p.v[0].xyw, p.v[1].xyw); - - res.v[2] = float4(row1.z, -dot(bpc2, p.v[0].xyw), dot(tpc2, p.v[3].xyw), -dot(tpc2, p.v[2].xyw)) * rdet; - - float3 tpc3 = cross(p.v[0].xyz, p.v[1].xyz); - res.v[3] = float4(row1.w, dot(bpc3, p.v[0].xyz), -dot(tpc3, p.v[3].xyz), dot(tpc3, p.v[2].xyz)) * rdet; - - - return res; -} - - - -float2 eigenvalues(mat2 p) -{ - return solve_monic(characteristic_poly(p)); -} - -float3 eigenvalues(mat3 p) -{ - return solve_monic(characteristic_poly(p)); -} - -float4 eigenvalues(mat4 p) -{ - return solve_monic(characteristic_poly(p)); -} - -float2 eigenvector(mat2 p, float eigvl) -{ - // for a mat2, we first reverse-subtract the eigenvalue from the matrix diagonal, - // then return whichever row had the larger sum-of-absolute-values. - float4 v = float4(p.v[0], p.v[1]); - v.xw = eigvl - v.xw; - if (fabs(v.x) + fabs(v.y) > fabs(v.z) + fabs(v.w)) - return v.yx; - else - return v.wz; -} - - -float3 eigenvector(mat3 p, float eigvl) -{ - // for a mat3, we obtain the eigenvector as follows: - // step 1: subtract the eigenvalue from the matrix diagonal - // step 2: take two cross products between rows in the matrix - // step 3: return whichever of the cross products resulted in a longer vector. - - float3 r0 = p.v[0]; - float3 r1 = p.v[1]; - float3 r2 = p.v[2]; - - r0.x = r0.x - eigvl; - r1.y = r1.y - eigvl; - r2.z = r2.z - eigvl; - - float3 v1 = cross(r0, r1); - float3 v2 = cross(r1, r2); - - float len1 = dot(v1, v1); - float len2 = dot(v2, v2); - return len1 > len2 ? v1 : v2; -} - - -// generalized cross product: 3 vectors with 4 components each. -// The result is a vector that is perpendicular to all the three specified vectors. - -// it works in the sense that it produces a perpendicular-to-everything vector, -// but it has not been tested whether it points in the "right" direction. -float4 gcross(float4 p, float4 q, float4 r) -{ - return float4(dot(p.yzw, cross(q.yzw, r.yzw)), -dot(p.xzw, cross(q.xzw, r.xzw)), dot(p.xyw, cross(q.xyw, r.xyw)), -dot(p.xyz, cross(q.xyz, r.xyz))); -} - - - -float4 eigenvector(mat4 p, float eigvl) -{ - float4 r0 = p.v[0]; - float4 r1 = p.v[1]; - float4 r2 = p.v[2]; - float4 r3 = p.v[3]; - - r0.x = r0.x - eigvl; - r1.y = r1.y - eigvl; - r2.z = r2.z - eigvl; - r3.w = r3.w - eigvl; - - // generate four candidate vectors using the generalized cross product. - // These will in general point in the same direction (or 180 degree opposite), - // however they will have different lengths. Pick the longest one. - float3 tpc0 = cross(r0.yzw, r1.yzw); - float3 tpc1 = cross(r0.xzw, r1.xzw); - float3 tpc2 = cross(r0.xyw, r1.xyw); - float3 tpc3 = cross(r0.xyz, r1.xyz); - - float4 v1 = float4(dot(r2.yzw, tpc0), - -dot(r2.xzw, tpc1), - dot(r2.xyw, tpc2), - -dot(r2.xyz, tpc3)); - - float4 v2 = float4(dot(r3.yzw, tpc0), - -dot(r3.xzw, tpc1), - dot(r3.xyw, tpc2), - -dot(r3.xyz, tpc3)); - - float3 bpc0 = cross(r2.yzw, r3.yzw); - float3 bpc1 = cross(r2.xzw, r3.xzw); - float3 bpc2 = cross(r2.xyw, r3.xyw); - float3 bpc3 = cross(r2.xyz, r3.xyz); - - float4 v3 = float4(dot(r0.yzw, bpc0), - -dot(r0.xzw, bpc1), - dot(r0.xyw, bpc2), - -dot(r0.xyz, bpc3)); - - float4 v4 = float4(dot(r1.yzw, bpc0), - -dot(r1.xzw, bpc1), - dot(r1.xyw, bpc2), - -dot(r1.xyz, bpc3)); - - float len1 = dot(v1, v1); - float len2 = dot(v2, v2); - float len3 = dot(v3, v3); - float len4 = dot(v4, v4); - - if (fmax(len1, len2) > fmax(len3, len4)) - return len1 > len2 ? v1 : v2; - else - return len3 > len4 ? v3 : v4; -} - - -// matrix multiply - -mat2 operator *(mat2 a, mat2 b) -{ - mat2 res; - res.v[0] = a.v[0].x * b.v[0] + a.v[0].y * b.v[1]; - res.v[1] = a.v[1].x * b.v[0] + a.v[1].y * b.v[1]; - return res; -} - -mat3 operator *(mat3 a, mat3 b) -{ - mat3 res; - res.v[0] = a.v[0].x * b.v[0] + a.v[0].y * b.v[1] + a.v[0].z * b.v[2]; - res.v[1] = a.v[1].x * b.v[0] + a.v[1].y * b.v[1] + a.v[1].z * b.v[2]; - res.v[2] = a.v[2].x * b.v[0] + a.v[2].y * b.v[1] + a.v[2].z * b.v[2]; - return res; -} - -mat4 operator *(mat4 a, mat4 b) -{ - mat4 res; - res.v[0] = a.v[0].x * b.v[0] + a.v[0].y * b.v[1] + a.v[0].z * b.v[2] + a.v[0].w * b.v[3]; - res.v[1] = a.v[1].x * b.v[0] + a.v[1].y * b.v[1] + a.v[1].z * b.v[2] + a.v[1].w * b.v[3]; - res.v[2] = a.v[2].x * b.v[0] + a.v[2].y * b.v[1] + a.v[2].z * b.v[2] + a.v[2].w * b.v[3]; - res.v[3] = a.v[3].x * b.v[0] + a.v[3].y * b.v[1] + a.v[3].z * b.v[2] + a.v[3].w * b.v[3]; - return res; -} - - - -/************************* - -simple geometric functions - -*************************/ - - -// return parameter value for the point on the line closest to the specified point -float param_nearest_on_line(float2 point, line2 line) -{ - return dot(point - line.a, line.b) / dot(line.b, line.b); -} - -float param_nearest_on_line(float3 point, line3 line) -{ - return dot(point - line.a, line.b) / dot(line.b, line.b); -} - -float param_nearest_on_line(float4 point, line4 line) -{ - return dot(point - line.a, line.b) / dot(line.b, line.b); -} - - -// return distance between point and line -float point_line_distance(float2 point, line2 line) -{ - return distance(point, line.a + line.b * param_nearest_on_line(point, line)); -} - -float point_line_distance(float3 point, line3 line) -{ - return distance(point, line.a + line.b * param_nearest_on_line(point, line)); -} - -float point_line_distance(float4 point, line4 line) -{ - return distance(point, line.a + line.b * param_nearest_on_line(point, line)); -} - - -float point_line_distance_sqr(float2 point, line2 line) -{ - return distance_sqr(point, line.a + line.b * param_nearest_on_line(point, line)); -} - -float point_line_distance_sqr(float3 point, line3 line) -{ - return distance_sqr(point, line.a + line.b * param_nearest_on_line(point, line)); -} - -float point_line_distance_sqr(float4 point, line4 line) -{ - return distance_sqr(point, line.a + line.b * param_nearest_on_line(point, line)); -} - - - -// distance between plane/hyperplane in 3D and 4D -float point_plane_3d_distance(float3 point, plane_3d plane) -{ - return dot(point - plane.root_point, plane.normal); -} - - -float point_hyperplane_4d_distance(float4 point, hyperplane_4d plane) -{ - return dot(point - plane.root_point, plane.normal); -} - - -// helper functions to produce a 3D plane from three points and a 4D hyperplane from four points. -plane_3d generate_plane_from_points(float3 point0, float3 point1, float3 point2) -{ - plane_3d res; - res.root_point = point0; - res.normal = normalize(cross(point1 - point0, point2 - point0)); - return res; -} - -hyperplane_4d generate_hyperplane_from_points(float4 point0, float4 point1, float4 point2, float4 point3) -{ - hyperplane_4d res; - res.root_point = point0; - res.normal = normalize(gcross(point1 - point0, point2 - point0, point3 - point0)); - return res; -} - - diff --git a/3rdparty/bimg/3rdparty/astc/mathlib.h b/3rdparty/bimg/3rdparty/astc/mathlib.h deleted file mode 100644 index 377ea0e21f2..00000000000 --- a/3rdparty/bimg/3rdparty/astc/mathlib.h +++ /dev/null @@ -1,200 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012, 2018 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Internal math library declarations for ASTC codec. - */ -/*----------------------------------------------------------------------------*/ - -#ifndef MATHLIB_H_INCLUDED - -#define MATHLIB_H_INCLUDED - -#include "vectypes.h" - -// basic OpenCL functions -float inversesqrt(float p); -float acospi(float p); -float sinpi(float p); -float cospi(float p); - -float nan(int p); - -#if !defined(_MSC_VER) && (__cplusplus < 201103L) -float fmax(float p, float q); -float fmin(float p, float q); -#endif // C++11 - -float2 fmax(float2 p, float2 q); - -float3 fmax(float3 p, float3 q); - -float4 fmax(float4 p, float4 q); -float2 fmin(float2 p, float2 q); -float3 fmin(float3 p, float3 q); -float4 fmin(float4 p, float4 q); - -/* - float dot( float2 p, float2 q ); - float dot( float3 p, float3 q ); - float dot( float4 p, float4 q ); -*/ - -static inline float dot(float2 p, float2 q) -{ - return p.x * q.x + p.y * q.y; -} -static inline float dot(float3 p, float3 q) -{ - return p.x * q.x + p.y * q.y + p.z * q.z; -} -static inline float dot(float4 p, float4 q) -{ - return p.x * q.x + p.y * q.y + p.z * q.z + p.w * q.w; -} - - -float3 cross(float3 p, float3 q); -float4 cross(float4 p, float4 q); - -float length(float2 p); -float length(float3 p); -float length(float4 p); - -float length_sqr(float2 p); -float length_sqr(float3 p); -float length_sqr(float4 p); - -float distance(float2 p, float2 q); -float distance(float3 p, float3 q); -float distance(float4 p, float4 q); - -float distance_sqr(float2 p, float2 q); -float distance_sqr(float3 p, float3 q); -float distance_sqr(float4 p, float4 q); - -float2 normalize(float2 p); -float3 normalize(float3 p); -float4 normalize(float4 p); - - - -// functions other than just basic OpenCL functions - -float4 gcross(float4 p, float4 q, float4 r); - -struct mat2 -{ - float2 v[2]; -}; -struct mat3 -{ - float3 v[3]; -}; -struct mat4 -{ - float4 v[4]; -}; - -float trace(mat2 p); -float trace(mat3 p); -float trace(mat4 p); - -float determinant(mat2 p); -float determinant(mat3 p); -float determinant(mat4 p); - -float2 characteristic_poly(mat2 p); -float3 characteristic_poly(mat3 p); -float4 characteristic_poly(mat4 p); - -float2 solve_monic(float2 p); -float3 solve_monic(float3 p); -float4 solve_monic(float4 p); - -float2 transform(mat2 p, float2 q); -float3 transform(mat3 p, float3 q); -float4 transform(mat4 p, float4 q); - -mat2 adjugate(mat2 p); -mat3 adjugate(mat3 p); -mat4 adjugate(mat4 p); - -mat2 invert(mat2 p); -mat3 invert(mat3 p); -mat4 invert(mat4 p); - -float2 eigenvalues(mat2 p); -float3 eigenvalues(mat3 p); -float4 eigenvalues(mat4 p); - -float2 eigenvector(mat2 p, float eigvl); -float3 eigenvector(mat3 p, float eigvl); -float4 eigenvector(mat4 p, float eigvl); - -mat2 operator *(mat2 a, mat2 b); -mat3 operator *(mat3 a, mat3 b); -mat4 operator *(mat4 a, mat4 b); - - - -// parametric line, 2D: The line is given by line = a + b*t. -struct line2 -{ - float2 a; - float2 b; -}; - -// parametric line, 3D -struct line3 -{ - float3 a; - float3 b; -}; - -struct line4 -{ - float4 a; - float4 b; -}; - -// plane/hyperplane defined by a point and a normal vector -struct plane_3d -{ - float3 root_point; - float3 normal; // normalized -}; - -struct hyperplane_4d -{ - float4 root_point; - float4 normal; // normalized -}; - -float param_nearest_on_line(float2 point, line2 line); -float param_nearest_on_line(float3 point, line3 line); -float param_nearest_on_line(float4 point, line4 line); - -float point_line_distance(float2 point, line2 line); -float point_line_distance(float3 point, line3 line); -float point_line_distance(float4 point, line4 line); - -float point_line_distance_sqr(float2 point, line2 line); -float point_line_distance_sqr(float3 point, line3 line); -float point_line_distance_sqr(float4 point, line4 line); - -float point_plane_3d_distance(float3 point, plane_3d plane); -float point_hyperplane_4d_distance(float4 point, hyperplane_4d plane); - -plane_3d generate_plane_from_points(float3 point0, float3 point1, float3 point2); -hyperplane_4d generate_hyperplane_from_points(float4 point0, float4 point1, float4 point2, float4 point3); - - -#endif diff --git a/3rdparty/bimg/3rdparty/astc/readme.txt b/3rdparty/bimg/3rdparty/astc/readme.txt deleted file mode 100644 index 17ad54eb81c..00000000000 --- a/3rdparty/bimg/3rdparty/astc/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Library version of astc-encoder, from https://github.com/andrewwillmott/astc-encoder. diff --git a/3rdparty/bimg/3rdparty/astc/softfloat.h b/3rdparty/bimg/3rdparty/astc/softfloat.h deleted file mode 100644 index 1cf9ba767fb..00000000000 --- a/3rdparty/bimg/3rdparty/astc/softfloat.h +++ /dev/null @@ -1,95 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Soft IEEE-754 floating point library. - */ -/*----------------------------------------------------------------------------*/ - -#ifndef SOFTFLOAT_H_INCLUDED - -#define SOFTFLOAT_H_INCLUDED - -#if defined __cplusplus -extern "C" -{ -#endif - -#if defined __cplusplus && !defined(_MSC_VER) - - /* if compiling as C++, we need to define these macros in order to obtain all the macros in stdint.h . */ - #define __STDC_LIMIT_MACROS - #define __STDC_CONSTANT_MACROS - #include <stdint.h> - -#else - - typedef unsigned char uint8_t; - typedef signed char int8_t; - typedef unsigned short uint16_t; - typedef signed short int16_t; - typedef unsigned int uint32_t; - typedef signed int int32_t; - -#endif - - -uint32_t clz32(uint32_t p); - - -/* targets that don't have UINT32_C probably don't have the rest of C99s stdint.h */ -#ifndef UINT32_C - - #define PASTE(a) a - #define UINT64_C(a) PASTE(a##ULL) - #define UINT32_C(a) PASTE(a##U) - #define INT64_C(a) PASTE(a##LL) - #define INT32_C(a) a - - #define PRIX32 "X" - #define PRId32 "d" - #define PRIu32 "u" - #define PRIX64 "LX" - #define PRId64 "Ld" - #define PRIu64 "Lu" - -#endif - - /* sized soft-float types. These are mapped to the sized integer types of C99, instead of C's - floating-point types; this is because the library needs to maintain exact, bit-level control on all - operations on these data types. */ - typedef uint16_t sf16; - typedef uint32_t sf32; - - /* the five rounding modes that IEEE-754r defines */ - typedef enum - { - SF_UP = 0, /* round towards positive infinity */ - SF_DOWN = 1, /* round towards negative infinity */ - SF_TOZERO = 2, /* round towards zero */ - SF_NEARESTEVEN = 3, /* round toward nearest value; if mid-between, round to even value */ - SF_NEARESTAWAY = 4 /* round toward nearest value; if mid-between, round away from zero */ - } roundmode; - - /* narrowing float->float conversions */ - sf16 sf32_to_sf16(sf32, roundmode); - - /* widening float->float conversions */ - sf32 sf16_to_sf32(sf16); - - sf16 float_to_sf16(float, roundmode); - float sf16_to_float(sf16); - - -#if defined __cplusplus -} -#endif - -#endif diff --git a/3rdparty/bimg/3rdparty/astc/vectypes.h b/3rdparty/bimg/3rdparty/astc/vectypes.h deleted file mode 100644 index aa206d72adc..00000000000 --- a/3rdparty/bimg/3rdparty/astc/vectypes.h +++ /dev/null @@ -1,16209 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/** - * This confidential and proprietary software may be used only as - * authorised by a licensing agreement from ARM Limited - * (C) COPYRIGHT 2011-2012 ARM Limited - * ALL RIGHTS RESERVED - * - * The entire notice above must be reproduced on all authorised - * copies and copies may only be made to the extent permitted - * by a licensing agreement from ARM Limited. - * - * @brief Template library for fixed-size vectors. - */ -/*----------------------------------------------------------------------------*/ - -#include <string.h> -#include <stdint.h> - -typedef unsigned int uint; -typedef unsigned short ushort; -typedef unsigned long ulong; -typedef unsigned char uchar; -typedef signed char schar; - -template < typename vtype > class vtype2; - -template < typename vtype > class vtype3; - -template < typename vtype > class vtype4; - -template < typename vtype > struct vtype2_xx_ref -{ - vtype2 < vtype > *v; - vtype2_xx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xy_ref -{ - vtype2 < vtype > *v; - vtype2_xy_ref(vtype2 < vtype > *p):v(p) - { - }; - inline vtype2_xy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype2_yx_ref -{ - vtype2 < vtype > *v; - vtype2_yx_ref(vtype2 < vtype > *p):v(p) - { - }; - inline vtype2_yx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype2_yy_ref -{ - vtype2 < vtype > *v; - vtype2_yy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xx_ref -{ - vtype3 < vtype > *v; - vtype3_xx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xy_ref -{ - vtype3 < vtype > *v; - vtype3_xy_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_xy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_xz_ref -{ - vtype3 < vtype > *v; - vtype3_xz_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_xz_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_yx_ref -{ - vtype3 < vtype > *v; - vtype3_yx_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_yx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_yy_ref -{ - vtype3 < vtype > *v; - vtype3_yy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yz_ref -{ - vtype3 < vtype > *v; - vtype3_yz_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_yz_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_zx_ref -{ - vtype3 < vtype > *v; - vtype3_zx_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_zx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_zy_ref -{ - vtype3 < vtype > *v; - vtype3_zy_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_zy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype3_zz_ref -{ - vtype3 < vtype > *v; - vtype3_zz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xx_ref -{ - vtype4 < vtype > *v; - vtype4_xx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xy_ref -{ - vtype4 < vtype > *v; - vtype4_xy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_xz_ref -{ - vtype4 < vtype > *v; - vtype4_xz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xz_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_xw_ref -{ - vtype4 < vtype > *v; - vtype4_xw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xw_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_yx_ref -{ - vtype4 < vtype > *v; - vtype4_yx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_yy_ref -{ - vtype4 < vtype > *v; - vtype4_yy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yz_ref -{ - vtype4 < vtype > *v; - vtype4_yz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yz_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_yw_ref -{ - vtype4 < vtype > *v; - vtype4_yw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yw_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_zx_ref -{ - vtype4 < vtype > *v; - vtype4_zx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_zy_ref -{ - vtype4 < vtype > *v; - vtype4_zy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_zz_ref -{ - vtype4 < vtype > *v; - vtype4_zz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zw_ref -{ - vtype4 < vtype > *v; - vtype4_zw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zw_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_wx_ref -{ - vtype4 < vtype > *v; - vtype4_wx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wx_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_wy_ref -{ - vtype4 < vtype > *v; - vtype4_wy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wy_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_wz_ref -{ - vtype4 < vtype > *v; - vtype4_wz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wz_ref & operator=(const vtype2 < vtype > &); -}; -template < typename vtype > struct vtype4_ww_ref -{ - vtype4 < vtype > *v; - vtype4_ww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxx_ref -{ - vtype2 < vtype > *v; - vtype2_xxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxy_ref -{ - vtype2 < vtype > *v; - vtype2_xxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyx_ref -{ - vtype2 < vtype > *v; - vtype2_xyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyy_ref -{ - vtype2 < vtype > *v; - vtype2_xyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxx_ref -{ - vtype2 < vtype > *v; - vtype2_yxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxy_ref -{ - vtype2 < vtype > *v; - vtype2_yxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyx_ref -{ - vtype2 < vtype > *v; - vtype2_yyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyy_ref -{ - vtype2 < vtype > *v; - vtype2_yyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxx_ref -{ - vtype3 < vtype > *v; - vtype3_xxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxy_ref -{ - vtype3 < vtype > *v; - vtype3_xxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxz_ref -{ - vtype3 < vtype > *v; - vtype3_xxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyx_ref -{ - vtype3 < vtype > *v; - vtype3_xyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyy_ref -{ - vtype3 < vtype > *v; - vtype3_xyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyz_ref -{ - vtype3 < vtype > *v; - vtype3_xyz_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_xyz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_xzx_ref -{ - vtype3 < vtype > *v; - vtype3_xzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzy_ref -{ - vtype3 < vtype > *v; - vtype3_xzy_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_xzy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_xzz_ref -{ - vtype3 < vtype > *v; - vtype3_xzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxx_ref -{ - vtype3 < vtype > *v; - vtype3_yxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxy_ref -{ - vtype3 < vtype > *v; - vtype3_yxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxz_ref -{ - vtype3 < vtype > *v; - vtype3_yxz_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_yxz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_yyx_ref -{ - vtype3 < vtype > *v; - vtype3_yyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyy_ref -{ - vtype3 < vtype > *v; - vtype3_yyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyz_ref -{ - vtype3 < vtype > *v; - vtype3_yyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzx_ref -{ - vtype3 < vtype > *v; - vtype3_yzx_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_yzx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_yzy_ref -{ - vtype3 < vtype > *v; - vtype3_yzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzz_ref -{ - vtype3 < vtype > *v; - vtype3_yzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxx_ref -{ - vtype3 < vtype > *v; - vtype3_zxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxy_ref -{ - vtype3 < vtype > *v; - vtype3_zxy_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_zxy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_zxz_ref -{ - vtype3 < vtype > *v; - vtype3_zxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyx_ref -{ - vtype3 < vtype > *v; - vtype3_zyx_ref(vtype3 < vtype > *p):v(p) - { - }; - inline vtype3_zyx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype3_zyy_ref -{ - vtype3 < vtype > *v; - vtype3_zyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyz_ref -{ - vtype3 < vtype > *v; - vtype3_zyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzx_ref -{ - vtype3 < vtype > *v; - vtype3_zzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzy_ref -{ - vtype3 < vtype > *v; - vtype3_zzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzz_ref -{ - vtype3 < vtype > *v; - vtype3_zzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxx_ref -{ - vtype4 < vtype > *v; - vtype4_xxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxy_ref -{ - vtype4 < vtype > *v; - vtype4_xxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxz_ref -{ - vtype4 < vtype > *v; - vtype4_xxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxw_ref -{ - vtype4 < vtype > *v; - vtype4_xxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyx_ref -{ - vtype4 < vtype > *v; - vtype4_xyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyy_ref -{ - vtype4 < vtype > *v; - vtype4_xyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyz_ref -{ - vtype4 < vtype > *v; - vtype4_xyz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xyz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xyw_ref -{ - vtype4 < vtype > *v; - vtype4_xyw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xyw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xzx_ref -{ - vtype4 < vtype > *v; - vtype4_xzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzy_ref -{ - vtype4 < vtype > *v; - vtype4_xzy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xzy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xzz_ref -{ - vtype4 < vtype > *v; - vtype4_xzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzw_ref -{ - vtype4 < vtype > *v; - vtype4_xzw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xzw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xwx_ref -{ - vtype4 < vtype > *v; - vtype4_xwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwy_ref -{ - vtype4 < vtype > *v; - vtype4_xwy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xwy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xwz_ref -{ - vtype4 < vtype > *v; - vtype4_xwz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xwz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_xww_ref -{ - vtype4 < vtype > *v; - vtype4_xww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxx_ref -{ - vtype4 < vtype > *v; - vtype4_yxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxy_ref -{ - vtype4 < vtype > *v; - vtype4_yxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxz_ref -{ - vtype4 < vtype > *v; - vtype4_yxz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yxz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_yxw_ref -{ - vtype4 < vtype > *v; - vtype4_yxw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yxw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_yyx_ref -{ - vtype4 < vtype > *v; - vtype4_yyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyy_ref -{ - vtype4 < vtype > *v; - vtype4_yyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyz_ref -{ - vtype4 < vtype > *v; - vtype4_yyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyw_ref -{ - vtype4 < vtype > *v; - vtype4_yyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzx_ref -{ - vtype4 < vtype > *v; - vtype4_yzx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yzx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_yzy_ref -{ - vtype4 < vtype > *v; - vtype4_yzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzz_ref -{ - vtype4 < vtype > *v; - vtype4_yzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzw_ref -{ - vtype4 < vtype > *v; - vtype4_yzw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yzw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_ywx_ref -{ - vtype4 < vtype > *v; - vtype4_ywx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_ywx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_ywy_ref -{ - vtype4 < vtype > *v; - vtype4_ywy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywz_ref -{ - vtype4 < vtype > *v; - vtype4_ywz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_ywz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_yww_ref -{ - vtype4 < vtype > *v; - vtype4_yww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxx_ref -{ - vtype4 < vtype > *v; - vtype4_zxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxy_ref -{ - vtype4 < vtype > *v; - vtype4_zxy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zxy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zxz_ref -{ - vtype4 < vtype > *v; - vtype4_zxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxw_ref -{ - vtype4 < vtype > *v; - vtype4_zxw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zxw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zyx_ref -{ - vtype4 < vtype > *v; - vtype4_zyx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zyx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zyy_ref -{ - vtype4 < vtype > *v; - vtype4_zyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyz_ref -{ - vtype4 < vtype > *v; - vtype4_zyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyw_ref -{ - vtype4 < vtype > *v; - vtype4_zyw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zyw_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zzx_ref -{ - vtype4 < vtype > *v; - vtype4_zzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzy_ref -{ - vtype4 < vtype > *v; - vtype4_zzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzz_ref -{ - vtype4 < vtype > *v; - vtype4_zzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzw_ref -{ - vtype4 < vtype > *v; - vtype4_zzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwx_ref -{ - vtype4 < vtype > *v; - vtype4_zwx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zwx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zwy_ref -{ - vtype4 < vtype > *v; - vtype4_zwy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zwy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_zwz_ref -{ - vtype4 < vtype > *v; - vtype4_zwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zww_ref -{ - vtype4 < vtype > *v; - vtype4_zww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxx_ref -{ - vtype4 < vtype > *v; - vtype4_wxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxy_ref -{ - vtype4 < vtype > *v; - vtype4_wxy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wxy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wxz_ref -{ - vtype4 < vtype > *v; - vtype4_wxz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wxz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wxw_ref -{ - vtype4 < vtype > *v; - vtype4_wxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyx_ref -{ - vtype4 < vtype > *v; - vtype4_wyx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wyx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wyy_ref -{ - vtype4 < vtype > *v; - vtype4_wyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyz_ref -{ - vtype4 < vtype > *v; - vtype4_wyz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wyz_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wyw_ref -{ - vtype4 < vtype > *v; - vtype4_wyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzx_ref -{ - vtype4 < vtype > *v; - vtype4_wzx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wzx_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wzy_ref -{ - vtype4 < vtype > *v; - vtype4_wzy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wzy_ref & operator=(const vtype3 < vtype > &); -}; -template < typename vtype > struct vtype4_wzz_ref -{ - vtype4 < vtype > *v; - vtype4_wzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzw_ref -{ - vtype4 < vtype > *v; - vtype4_wzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwx_ref -{ - vtype4 < vtype > *v; - vtype4_wwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwy_ref -{ - vtype4 < vtype > *v; - vtype4_wwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwz_ref -{ - vtype4 < vtype > *v; - vtype4_wwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_www_ref -{ - vtype4 < vtype > *v; - vtype4_www_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxxx_ref -{ - vtype2 < vtype > *v; - vtype2_xxxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxxy_ref -{ - vtype2 < vtype > *v; - vtype2_xxxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxyx_ref -{ - vtype2 < vtype > *v; - vtype2_xxyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xxyy_ref -{ - vtype2 < vtype > *v; - vtype2_xxyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyxx_ref -{ - vtype2 < vtype > *v; - vtype2_xyxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyxy_ref -{ - vtype2 < vtype > *v; - vtype2_xyxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyyx_ref -{ - vtype2 < vtype > *v; - vtype2_xyyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_xyyy_ref -{ - vtype2 < vtype > *v; - vtype2_xyyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxxx_ref -{ - vtype2 < vtype > *v; - vtype2_yxxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxxy_ref -{ - vtype2 < vtype > *v; - vtype2_yxxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxyx_ref -{ - vtype2 < vtype > *v; - vtype2_yxyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yxyy_ref -{ - vtype2 < vtype > *v; - vtype2_yxyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyxx_ref -{ - vtype2 < vtype > *v; - vtype2_yyxx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyxy_ref -{ - vtype2 < vtype > *v; - vtype2_yyxy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyyx_ref -{ - vtype2 < vtype > *v; - vtype2_yyyx_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype2_yyyy_ref -{ - vtype2 < vtype > *v; - vtype2_yyyy_ref(vtype2 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxxx_ref -{ - vtype3 < vtype > *v; - vtype3_xxxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxxy_ref -{ - vtype3 < vtype > *v; - vtype3_xxxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxxz_ref -{ - vtype3 < vtype > *v; - vtype3_xxxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxyx_ref -{ - vtype3 < vtype > *v; - vtype3_xxyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxyy_ref -{ - vtype3 < vtype > *v; - vtype3_xxyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxyz_ref -{ - vtype3 < vtype > *v; - vtype3_xxyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxzx_ref -{ - vtype3 < vtype > *v; - vtype3_xxzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxzy_ref -{ - vtype3 < vtype > *v; - vtype3_xxzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xxzz_ref -{ - vtype3 < vtype > *v; - vtype3_xxzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyxx_ref -{ - vtype3 < vtype > *v; - vtype3_xyxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyxy_ref -{ - vtype3 < vtype > *v; - vtype3_xyxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyxz_ref -{ - vtype3 < vtype > *v; - vtype3_xyxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyyx_ref -{ - vtype3 < vtype > *v; - vtype3_xyyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyyy_ref -{ - vtype3 < vtype > *v; - vtype3_xyyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyyz_ref -{ - vtype3 < vtype > *v; - vtype3_xyyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyzx_ref -{ - vtype3 < vtype > *v; - vtype3_xyzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyzy_ref -{ - vtype3 < vtype > *v; - vtype3_xyzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xyzz_ref -{ - vtype3 < vtype > *v; - vtype3_xyzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzxx_ref -{ - vtype3 < vtype > *v; - vtype3_xzxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzxy_ref -{ - vtype3 < vtype > *v; - vtype3_xzxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzxz_ref -{ - vtype3 < vtype > *v; - vtype3_xzxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzyx_ref -{ - vtype3 < vtype > *v; - vtype3_xzyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzyy_ref -{ - vtype3 < vtype > *v; - vtype3_xzyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzyz_ref -{ - vtype3 < vtype > *v; - vtype3_xzyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzzx_ref -{ - vtype3 < vtype > *v; - vtype3_xzzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzzy_ref -{ - vtype3 < vtype > *v; - vtype3_xzzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_xzzz_ref -{ - vtype3 < vtype > *v; - vtype3_xzzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxxx_ref -{ - vtype3 < vtype > *v; - vtype3_yxxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxxy_ref -{ - vtype3 < vtype > *v; - vtype3_yxxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxxz_ref -{ - vtype3 < vtype > *v; - vtype3_yxxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxyx_ref -{ - vtype3 < vtype > *v; - vtype3_yxyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxyy_ref -{ - vtype3 < vtype > *v; - vtype3_yxyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxyz_ref -{ - vtype3 < vtype > *v; - vtype3_yxyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxzx_ref -{ - vtype3 < vtype > *v; - vtype3_yxzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxzy_ref -{ - vtype3 < vtype > *v; - vtype3_yxzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yxzz_ref -{ - vtype3 < vtype > *v; - vtype3_yxzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyxx_ref -{ - vtype3 < vtype > *v; - vtype3_yyxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyxy_ref -{ - vtype3 < vtype > *v; - vtype3_yyxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyxz_ref -{ - vtype3 < vtype > *v; - vtype3_yyxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyyx_ref -{ - vtype3 < vtype > *v; - vtype3_yyyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyyy_ref -{ - vtype3 < vtype > *v; - vtype3_yyyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyyz_ref -{ - vtype3 < vtype > *v; - vtype3_yyyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyzx_ref -{ - vtype3 < vtype > *v; - vtype3_yyzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyzy_ref -{ - vtype3 < vtype > *v; - vtype3_yyzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yyzz_ref -{ - vtype3 < vtype > *v; - vtype3_yyzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzxx_ref -{ - vtype3 < vtype > *v; - vtype3_yzxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzxy_ref -{ - vtype3 < vtype > *v; - vtype3_yzxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzxz_ref -{ - vtype3 < vtype > *v; - vtype3_yzxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzyx_ref -{ - vtype3 < vtype > *v; - vtype3_yzyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzyy_ref -{ - vtype3 < vtype > *v; - vtype3_yzyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzyz_ref -{ - vtype3 < vtype > *v; - vtype3_yzyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzzx_ref -{ - vtype3 < vtype > *v; - vtype3_yzzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzzy_ref -{ - vtype3 < vtype > *v; - vtype3_yzzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_yzzz_ref -{ - vtype3 < vtype > *v; - vtype3_yzzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxxx_ref -{ - vtype3 < vtype > *v; - vtype3_zxxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxxy_ref -{ - vtype3 < vtype > *v; - vtype3_zxxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxxz_ref -{ - vtype3 < vtype > *v; - vtype3_zxxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxyx_ref -{ - vtype3 < vtype > *v; - vtype3_zxyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxyy_ref -{ - vtype3 < vtype > *v; - vtype3_zxyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxyz_ref -{ - vtype3 < vtype > *v; - vtype3_zxyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxzx_ref -{ - vtype3 < vtype > *v; - vtype3_zxzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxzy_ref -{ - vtype3 < vtype > *v; - vtype3_zxzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zxzz_ref -{ - vtype3 < vtype > *v; - vtype3_zxzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyxx_ref -{ - vtype3 < vtype > *v; - vtype3_zyxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyxy_ref -{ - vtype3 < vtype > *v; - vtype3_zyxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyxz_ref -{ - vtype3 < vtype > *v; - vtype3_zyxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyyx_ref -{ - vtype3 < vtype > *v; - vtype3_zyyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyyy_ref -{ - vtype3 < vtype > *v; - vtype3_zyyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyyz_ref -{ - vtype3 < vtype > *v; - vtype3_zyyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyzx_ref -{ - vtype3 < vtype > *v; - vtype3_zyzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyzy_ref -{ - vtype3 < vtype > *v; - vtype3_zyzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zyzz_ref -{ - vtype3 < vtype > *v; - vtype3_zyzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzxx_ref -{ - vtype3 < vtype > *v; - vtype3_zzxx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzxy_ref -{ - vtype3 < vtype > *v; - vtype3_zzxy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzxz_ref -{ - vtype3 < vtype > *v; - vtype3_zzxz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzyx_ref -{ - vtype3 < vtype > *v; - vtype3_zzyx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzyy_ref -{ - vtype3 < vtype > *v; - vtype3_zzyy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzyz_ref -{ - vtype3 < vtype > *v; - vtype3_zzyz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzzx_ref -{ - vtype3 < vtype > *v; - vtype3_zzzx_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzzy_ref -{ - vtype3 < vtype > *v; - vtype3_zzzy_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype3_zzzz_ref -{ - vtype3 < vtype > *v; - vtype3_zzzz_ref(vtype3 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxxx_ref -{ - vtype4 < vtype > *v; - vtype4_xxxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxxy_ref -{ - vtype4 < vtype > *v; - vtype4_xxxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxxz_ref -{ - vtype4 < vtype > *v; - vtype4_xxxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxxw_ref -{ - vtype4 < vtype > *v; - vtype4_xxxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxyx_ref -{ - vtype4 < vtype > *v; - vtype4_xxyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxyy_ref -{ - vtype4 < vtype > *v; - vtype4_xxyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxyz_ref -{ - vtype4 < vtype > *v; - vtype4_xxyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxyw_ref -{ - vtype4 < vtype > *v; - vtype4_xxyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxzx_ref -{ - vtype4 < vtype > *v; - vtype4_xxzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxzy_ref -{ - vtype4 < vtype > *v; - vtype4_xxzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxzz_ref -{ - vtype4 < vtype > *v; - vtype4_xxzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxzw_ref -{ - vtype4 < vtype > *v; - vtype4_xxzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxwx_ref -{ - vtype4 < vtype > *v; - vtype4_xxwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxwy_ref -{ - vtype4 < vtype > *v; - vtype4_xxwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxwz_ref -{ - vtype4 < vtype > *v; - vtype4_xxwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xxww_ref -{ - vtype4 < vtype > *v; - vtype4_xxww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyxx_ref -{ - vtype4 < vtype > *v; - vtype4_xyxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyxy_ref -{ - vtype4 < vtype > *v; - vtype4_xyxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyxz_ref -{ - vtype4 < vtype > *v; - vtype4_xyxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyxw_ref -{ - vtype4 < vtype > *v; - vtype4_xyxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyyx_ref -{ - vtype4 < vtype > *v; - vtype4_xyyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyyy_ref -{ - vtype4 < vtype > *v; - vtype4_xyyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyyz_ref -{ - vtype4 < vtype > *v; - vtype4_xyyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyyw_ref -{ - vtype4 < vtype > *v; - vtype4_xyyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyzx_ref -{ - vtype4 < vtype > *v; - vtype4_xyzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyzy_ref -{ - vtype4 < vtype > *v; - vtype4_xyzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyzz_ref -{ - vtype4 < vtype > *v; - vtype4_xyzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xyzw_ref -{ - vtype4 < vtype > *v; - vtype4_xyzw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xyzw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xywx_ref -{ - vtype4 < vtype > *v; - vtype4_xywx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xywy_ref -{ - vtype4 < vtype > *v; - vtype4_xywy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xywz_ref -{ - vtype4 < vtype > *v; - vtype4_xywz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xywz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xyww_ref -{ - vtype4 < vtype > *v; - vtype4_xyww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzxx_ref -{ - vtype4 < vtype > *v; - vtype4_xzxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzxy_ref -{ - vtype4 < vtype > *v; - vtype4_xzxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzxz_ref -{ - vtype4 < vtype > *v; - vtype4_xzxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzxw_ref -{ - vtype4 < vtype > *v; - vtype4_xzxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzyx_ref -{ - vtype4 < vtype > *v; - vtype4_xzyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzyy_ref -{ - vtype4 < vtype > *v; - vtype4_xzyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzyz_ref -{ - vtype4 < vtype > *v; - vtype4_xzyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzyw_ref -{ - vtype4 < vtype > *v; - vtype4_xzyw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xzyw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xzzx_ref -{ - vtype4 < vtype > *v; - vtype4_xzzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzzy_ref -{ - vtype4 < vtype > *v; - vtype4_xzzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzzz_ref -{ - vtype4 < vtype > *v; - vtype4_xzzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzzw_ref -{ - vtype4 < vtype > *v; - vtype4_xzzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzwx_ref -{ - vtype4 < vtype > *v; - vtype4_xzwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzwy_ref -{ - vtype4 < vtype > *v; - vtype4_xzwy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xzwy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xzwz_ref -{ - vtype4 < vtype > *v; - vtype4_xzwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xzww_ref -{ - vtype4 < vtype > *v; - vtype4_xzww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwxx_ref -{ - vtype4 < vtype > *v; - vtype4_xwxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwxy_ref -{ - vtype4 < vtype > *v; - vtype4_xwxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwxz_ref -{ - vtype4 < vtype > *v; - vtype4_xwxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwxw_ref -{ - vtype4 < vtype > *v; - vtype4_xwxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwyx_ref -{ - vtype4 < vtype > *v; - vtype4_xwyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwyy_ref -{ - vtype4 < vtype > *v; - vtype4_xwyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwyz_ref -{ - vtype4 < vtype > *v; - vtype4_xwyz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xwyz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xwyw_ref -{ - vtype4 < vtype > *v; - vtype4_xwyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwzx_ref -{ - vtype4 < vtype > *v; - vtype4_xwzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwzy_ref -{ - vtype4 < vtype > *v; - vtype4_xwzy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_xwzy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_xwzz_ref -{ - vtype4 < vtype > *v; - vtype4_xwzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwzw_ref -{ - vtype4 < vtype > *v; - vtype4_xwzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwwx_ref -{ - vtype4 < vtype > *v; - vtype4_xwwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwwy_ref -{ - vtype4 < vtype > *v; - vtype4_xwwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwwz_ref -{ - vtype4 < vtype > *v; - vtype4_xwwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_xwww_ref -{ - vtype4 < vtype > *v; - vtype4_xwww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxxx_ref -{ - vtype4 < vtype > *v; - vtype4_yxxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxxy_ref -{ - vtype4 < vtype > *v; - vtype4_yxxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxxz_ref -{ - vtype4 < vtype > *v; - vtype4_yxxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxxw_ref -{ - vtype4 < vtype > *v; - vtype4_yxxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxyx_ref -{ - vtype4 < vtype > *v; - vtype4_yxyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxyy_ref -{ - vtype4 < vtype > *v; - vtype4_yxyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxyz_ref -{ - vtype4 < vtype > *v; - vtype4_yxyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxyw_ref -{ - vtype4 < vtype > *v; - vtype4_yxyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxzx_ref -{ - vtype4 < vtype > *v; - vtype4_yxzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxzy_ref -{ - vtype4 < vtype > *v; - vtype4_yxzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxzz_ref -{ - vtype4 < vtype > *v; - vtype4_yxzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxzw_ref -{ - vtype4 < vtype > *v; - vtype4_yxzw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yxzw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_yxwx_ref -{ - vtype4 < vtype > *v; - vtype4_yxwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxwy_ref -{ - vtype4 < vtype > *v; - vtype4_yxwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yxwz_ref -{ - vtype4 < vtype > *v; - vtype4_yxwz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yxwz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_yxww_ref -{ - vtype4 < vtype > *v; - vtype4_yxww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyxx_ref -{ - vtype4 < vtype > *v; - vtype4_yyxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyxy_ref -{ - vtype4 < vtype > *v; - vtype4_yyxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyxz_ref -{ - vtype4 < vtype > *v; - vtype4_yyxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyxw_ref -{ - vtype4 < vtype > *v; - vtype4_yyxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyyx_ref -{ - vtype4 < vtype > *v; - vtype4_yyyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyyy_ref -{ - vtype4 < vtype > *v; - vtype4_yyyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyyz_ref -{ - vtype4 < vtype > *v; - vtype4_yyyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyyw_ref -{ - vtype4 < vtype > *v; - vtype4_yyyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyzx_ref -{ - vtype4 < vtype > *v; - vtype4_yyzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyzy_ref -{ - vtype4 < vtype > *v; - vtype4_yyzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyzz_ref -{ - vtype4 < vtype > *v; - vtype4_yyzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyzw_ref -{ - vtype4 < vtype > *v; - vtype4_yyzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yywx_ref -{ - vtype4 < vtype > *v; - vtype4_yywx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yywy_ref -{ - vtype4 < vtype > *v; - vtype4_yywy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yywz_ref -{ - vtype4 < vtype > *v; - vtype4_yywz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yyww_ref -{ - vtype4 < vtype > *v; - vtype4_yyww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzxx_ref -{ - vtype4 < vtype > *v; - vtype4_yzxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzxy_ref -{ - vtype4 < vtype > *v; - vtype4_yzxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzxz_ref -{ - vtype4 < vtype > *v; - vtype4_yzxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzxw_ref -{ - vtype4 < vtype > *v; - vtype4_yzxw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yzxw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_yzyx_ref -{ - vtype4 < vtype > *v; - vtype4_yzyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzyy_ref -{ - vtype4 < vtype > *v; - vtype4_yzyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzyz_ref -{ - vtype4 < vtype > *v; - vtype4_yzyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzyw_ref -{ - vtype4 < vtype > *v; - vtype4_yzyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzzx_ref -{ - vtype4 < vtype > *v; - vtype4_yzzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzzy_ref -{ - vtype4 < vtype > *v; - vtype4_yzzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzzz_ref -{ - vtype4 < vtype > *v; - vtype4_yzzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzzw_ref -{ - vtype4 < vtype > *v; - vtype4_yzzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzwx_ref -{ - vtype4 < vtype > *v; - vtype4_yzwx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_yzwx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_yzwy_ref -{ - vtype4 < vtype > *v; - vtype4_yzwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzwz_ref -{ - vtype4 < vtype > *v; - vtype4_yzwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_yzww_ref -{ - vtype4 < vtype > *v; - vtype4_yzww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywxx_ref -{ - vtype4 < vtype > *v; - vtype4_ywxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywxy_ref -{ - vtype4 < vtype > *v; - vtype4_ywxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywxz_ref -{ - vtype4 < vtype > *v; - vtype4_ywxz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_ywxz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_ywxw_ref -{ - vtype4 < vtype > *v; - vtype4_ywxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywyx_ref -{ - vtype4 < vtype > *v; - vtype4_ywyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywyy_ref -{ - vtype4 < vtype > *v; - vtype4_ywyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywyz_ref -{ - vtype4 < vtype > *v; - vtype4_ywyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywyw_ref -{ - vtype4 < vtype > *v; - vtype4_ywyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywzx_ref -{ - vtype4 < vtype > *v; - vtype4_ywzx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_ywzx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_ywzy_ref -{ - vtype4 < vtype > *v; - vtype4_ywzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywzz_ref -{ - vtype4 < vtype > *v; - vtype4_ywzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywzw_ref -{ - vtype4 < vtype > *v; - vtype4_ywzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywwx_ref -{ - vtype4 < vtype > *v; - vtype4_ywwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywwy_ref -{ - vtype4 < vtype > *v; - vtype4_ywwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywwz_ref -{ - vtype4 < vtype > *v; - vtype4_ywwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_ywww_ref -{ - vtype4 < vtype > *v; - vtype4_ywww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxxx_ref -{ - vtype4 < vtype > *v; - vtype4_zxxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxxy_ref -{ - vtype4 < vtype > *v; - vtype4_zxxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxxz_ref -{ - vtype4 < vtype > *v; - vtype4_zxxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxxw_ref -{ - vtype4 < vtype > *v; - vtype4_zxxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxyx_ref -{ - vtype4 < vtype > *v; - vtype4_zxyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxyy_ref -{ - vtype4 < vtype > *v; - vtype4_zxyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxyz_ref -{ - vtype4 < vtype > *v; - vtype4_zxyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxyw_ref -{ - vtype4 < vtype > *v; - vtype4_zxyw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zxyw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zxzx_ref -{ - vtype4 < vtype > *v; - vtype4_zxzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxzy_ref -{ - vtype4 < vtype > *v; - vtype4_zxzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxzz_ref -{ - vtype4 < vtype > *v; - vtype4_zxzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxzw_ref -{ - vtype4 < vtype > *v; - vtype4_zxzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxwx_ref -{ - vtype4 < vtype > *v; - vtype4_zxwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxwy_ref -{ - vtype4 < vtype > *v; - vtype4_zxwy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zxwy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zxwz_ref -{ - vtype4 < vtype > *v; - vtype4_zxwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zxww_ref -{ - vtype4 < vtype > *v; - vtype4_zxww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyxx_ref -{ - vtype4 < vtype > *v; - vtype4_zyxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyxy_ref -{ - vtype4 < vtype > *v; - vtype4_zyxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyxz_ref -{ - vtype4 < vtype > *v; - vtype4_zyxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyxw_ref -{ - vtype4 < vtype > *v; - vtype4_zyxw_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zyxw_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zyyx_ref -{ - vtype4 < vtype > *v; - vtype4_zyyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyyy_ref -{ - vtype4 < vtype > *v; - vtype4_zyyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyyz_ref -{ - vtype4 < vtype > *v; - vtype4_zyyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyyw_ref -{ - vtype4 < vtype > *v; - vtype4_zyyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyzx_ref -{ - vtype4 < vtype > *v; - vtype4_zyzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyzy_ref -{ - vtype4 < vtype > *v; - vtype4_zyzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyzz_ref -{ - vtype4 < vtype > *v; - vtype4_zyzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyzw_ref -{ - vtype4 < vtype > *v; - vtype4_zyzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zywx_ref -{ - vtype4 < vtype > *v; - vtype4_zywx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zywx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zywy_ref -{ - vtype4 < vtype > *v; - vtype4_zywy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zywz_ref -{ - vtype4 < vtype > *v; - vtype4_zywz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zyww_ref -{ - vtype4 < vtype > *v; - vtype4_zyww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzxx_ref -{ - vtype4 < vtype > *v; - vtype4_zzxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzxy_ref -{ - vtype4 < vtype > *v; - vtype4_zzxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzxz_ref -{ - vtype4 < vtype > *v; - vtype4_zzxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzxw_ref -{ - vtype4 < vtype > *v; - vtype4_zzxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzyx_ref -{ - vtype4 < vtype > *v; - vtype4_zzyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzyy_ref -{ - vtype4 < vtype > *v; - vtype4_zzyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzyz_ref -{ - vtype4 < vtype > *v; - vtype4_zzyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzyw_ref -{ - vtype4 < vtype > *v; - vtype4_zzyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzzx_ref -{ - vtype4 < vtype > *v; - vtype4_zzzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzzy_ref -{ - vtype4 < vtype > *v; - vtype4_zzzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzzz_ref -{ - vtype4 < vtype > *v; - vtype4_zzzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzzw_ref -{ - vtype4 < vtype > *v; - vtype4_zzzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzwx_ref -{ - vtype4 < vtype > *v; - vtype4_zzwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzwy_ref -{ - vtype4 < vtype > *v; - vtype4_zzwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzwz_ref -{ - vtype4 < vtype > *v; - vtype4_zzwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zzww_ref -{ - vtype4 < vtype > *v; - vtype4_zzww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwxx_ref -{ - vtype4 < vtype > *v; - vtype4_zwxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwxy_ref -{ - vtype4 < vtype > *v; - vtype4_zwxy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zwxy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zwxz_ref -{ - vtype4 < vtype > *v; - vtype4_zwxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwxw_ref -{ - vtype4 < vtype > *v; - vtype4_zwxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwyx_ref -{ - vtype4 < vtype > *v; - vtype4_zwyx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_zwyx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_zwyy_ref -{ - vtype4 < vtype > *v; - vtype4_zwyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwyz_ref -{ - vtype4 < vtype > *v; - vtype4_zwyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwyw_ref -{ - vtype4 < vtype > *v; - vtype4_zwyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwzx_ref -{ - vtype4 < vtype > *v; - vtype4_zwzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwzy_ref -{ - vtype4 < vtype > *v; - vtype4_zwzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwzz_ref -{ - vtype4 < vtype > *v; - vtype4_zwzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwzw_ref -{ - vtype4 < vtype > *v; - vtype4_zwzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwwx_ref -{ - vtype4 < vtype > *v; - vtype4_zwwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwwy_ref -{ - vtype4 < vtype > *v; - vtype4_zwwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwwz_ref -{ - vtype4 < vtype > *v; - vtype4_zwwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_zwww_ref -{ - vtype4 < vtype > *v; - vtype4_zwww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxxx_ref -{ - vtype4 < vtype > *v; - vtype4_wxxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxxy_ref -{ - vtype4 < vtype > *v; - vtype4_wxxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxxz_ref -{ - vtype4 < vtype > *v; - vtype4_wxxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxxw_ref -{ - vtype4 < vtype > *v; - vtype4_wxxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxyx_ref -{ - vtype4 < vtype > *v; - vtype4_wxyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxyy_ref -{ - vtype4 < vtype > *v; - vtype4_wxyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxyz_ref -{ - vtype4 < vtype > *v; - vtype4_wxyz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wxyz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wxyw_ref -{ - vtype4 < vtype > *v; - vtype4_wxyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxzx_ref -{ - vtype4 < vtype > *v; - vtype4_wxzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxzy_ref -{ - vtype4 < vtype > *v; - vtype4_wxzy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wxzy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wxzz_ref -{ - vtype4 < vtype > *v; - vtype4_wxzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxzw_ref -{ - vtype4 < vtype > *v; - vtype4_wxzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxwx_ref -{ - vtype4 < vtype > *v; - vtype4_wxwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxwy_ref -{ - vtype4 < vtype > *v; - vtype4_wxwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxwz_ref -{ - vtype4 < vtype > *v; - vtype4_wxwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wxww_ref -{ - vtype4 < vtype > *v; - vtype4_wxww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyxx_ref -{ - vtype4 < vtype > *v; - vtype4_wyxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyxy_ref -{ - vtype4 < vtype > *v; - vtype4_wyxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyxz_ref -{ - vtype4 < vtype > *v; - vtype4_wyxz_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wyxz_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wyxw_ref -{ - vtype4 < vtype > *v; - vtype4_wyxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyyx_ref -{ - vtype4 < vtype > *v; - vtype4_wyyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyyy_ref -{ - vtype4 < vtype > *v; - vtype4_wyyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyyz_ref -{ - vtype4 < vtype > *v; - vtype4_wyyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyyw_ref -{ - vtype4 < vtype > *v; - vtype4_wyyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyzx_ref -{ - vtype4 < vtype > *v; - vtype4_wyzx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wyzx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wyzy_ref -{ - vtype4 < vtype > *v; - vtype4_wyzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyzz_ref -{ - vtype4 < vtype > *v; - vtype4_wyzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyzw_ref -{ - vtype4 < vtype > *v; - vtype4_wyzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wywx_ref -{ - vtype4 < vtype > *v; - vtype4_wywx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wywy_ref -{ - vtype4 < vtype > *v; - vtype4_wywy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wywz_ref -{ - vtype4 < vtype > *v; - vtype4_wywz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wyww_ref -{ - vtype4 < vtype > *v; - vtype4_wyww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzxx_ref -{ - vtype4 < vtype > *v; - vtype4_wzxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzxy_ref -{ - vtype4 < vtype > *v; - vtype4_wzxy_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wzxy_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wzxz_ref -{ - vtype4 < vtype > *v; - vtype4_wzxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzxw_ref -{ - vtype4 < vtype > *v; - vtype4_wzxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzyx_ref -{ - vtype4 < vtype > *v; - vtype4_wzyx_ref(vtype4 < vtype > *p):v(p) - { - }; - inline vtype4_wzyx_ref & operator=(const vtype4 < vtype > &); -}; -template < typename vtype > struct vtype4_wzyy_ref -{ - vtype4 < vtype > *v; - vtype4_wzyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzyz_ref -{ - vtype4 < vtype > *v; - vtype4_wzyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzyw_ref -{ - vtype4 < vtype > *v; - vtype4_wzyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzzx_ref -{ - vtype4 < vtype > *v; - vtype4_wzzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzzy_ref -{ - vtype4 < vtype > *v; - vtype4_wzzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzzz_ref -{ - vtype4 < vtype > *v; - vtype4_wzzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzzw_ref -{ - vtype4 < vtype > *v; - vtype4_wzzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzwx_ref -{ - vtype4 < vtype > *v; - vtype4_wzwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzwy_ref -{ - vtype4 < vtype > *v; - vtype4_wzwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzwz_ref -{ - vtype4 < vtype > *v; - vtype4_wzwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wzww_ref -{ - vtype4 < vtype > *v; - vtype4_wzww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwxx_ref -{ - vtype4 < vtype > *v; - vtype4_wwxx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwxy_ref -{ - vtype4 < vtype > *v; - vtype4_wwxy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwxz_ref -{ - vtype4 < vtype > *v; - vtype4_wwxz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwxw_ref -{ - vtype4 < vtype > *v; - vtype4_wwxw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwyx_ref -{ - vtype4 < vtype > *v; - vtype4_wwyx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwyy_ref -{ - vtype4 < vtype > *v; - vtype4_wwyy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwyz_ref -{ - vtype4 < vtype > *v; - vtype4_wwyz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwyw_ref -{ - vtype4 < vtype > *v; - vtype4_wwyw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwzx_ref -{ - vtype4 < vtype > *v; - vtype4_wwzx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwzy_ref -{ - vtype4 < vtype > *v; - vtype4_wwzy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwzz_ref -{ - vtype4 < vtype > *v; - vtype4_wwzz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwzw_ref -{ - vtype4 < vtype > *v; - vtype4_wwzw_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwwx_ref -{ - vtype4 < vtype > *v; - vtype4_wwwx_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwwy_ref -{ - vtype4 < vtype > *v; - vtype4_wwwy_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwwz_ref -{ - vtype4 < vtype > *v; - vtype4_wwwz_ref(vtype4 < vtype > *p):v(p) - { - }; -}; -template < typename vtype > struct vtype4_wwww_ref -{ - vtype4 < vtype > *v; - vtype4_wwww_ref(vtype4 < vtype > *p):v(p) - { - }; -}; - -template < typename vtype > class vtype2 -{ - public: - vtype x, y; - vtype2() - { - }; - vtype2(vtype p, vtype q):x(p), y(q) - { - }; - vtype2(const vtype2 & p):x(p.x), y(p.y) - { - }; - inline vtype2(const vtype2_xx_ref < vtype > &v); - inline vtype2(const vtype2_xy_ref < vtype > &v); - inline vtype2(const vtype2_yx_ref < vtype > &v); - inline vtype2(const vtype2_yy_ref < vtype > &v); - inline vtype2(const vtype3_xx_ref < vtype > &v); - inline vtype2(const vtype3_xy_ref < vtype > &v); - inline vtype2(const vtype3_xz_ref < vtype > &v); - inline vtype2(const vtype3_yx_ref < vtype > &v); - inline vtype2(const vtype3_yy_ref < vtype > &v); - inline vtype2(const vtype3_yz_ref < vtype > &v); - inline vtype2(const vtype3_zx_ref < vtype > &v); - inline vtype2(const vtype3_zy_ref < vtype > &v); - inline vtype2(const vtype3_zz_ref < vtype > &v); - inline vtype2(const vtype4_xx_ref < vtype > &v); - inline vtype2(const vtype4_xy_ref < vtype > &v); - inline vtype2(const vtype4_xz_ref < vtype > &v); - inline vtype2(const vtype4_xw_ref < vtype > &v); - inline vtype2(const vtype4_yx_ref < vtype > &v); - inline vtype2(const vtype4_yy_ref < vtype > &v); - inline vtype2(const vtype4_yz_ref < vtype > &v); - inline vtype2(const vtype4_yw_ref < vtype > &v); - inline vtype2(const vtype4_zx_ref < vtype > &v); - inline vtype2(const vtype4_zy_ref < vtype > &v); - inline vtype2(const vtype4_zz_ref < vtype > &v); - inline vtype2(const vtype4_zw_ref < vtype > &v); - inline vtype2(const vtype4_wx_ref < vtype > &v); - inline vtype2(const vtype4_wy_ref < vtype > &v); - inline vtype2(const vtype4_wz_ref < vtype > &v); - inline vtype2(const vtype4_ww_ref < vtype > &v); - vtype2_xx_ref < vtype > xx() - { - return vtype2_xx_ref < vtype > (this); - } - vtype2_xy_ref < vtype > xy() - { - return vtype2_xy_ref < vtype > (this); - } - vtype2_yx_ref < vtype > yx() - { - return vtype2_yx_ref < vtype > (this); - } - vtype2_yy_ref < vtype > yy() - { - return vtype2_yy_ref < vtype > (this); - } - vtype2_xxx_ref < vtype > xxx() - { - return vtype2_xxx_ref < vtype > (this); - } - vtype2_xxy_ref < vtype > xxy() - { - return vtype2_xxy_ref < vtype > (this); - } - vtype2_xyx_ref < vtype > xyx() - { - return vtype2_xyx_ref < vtype > (this); - } - vtype2_xyy_ref < vtype > xyy() - { - return vtype2_xyy_ref < vtype > (this); - } - vtype2_yxx_ref < vtype > yxx() - { - return vtype2_yxx_ref < vtype > (this); - } - vtype2_yxy_ref < vtype > yxy() - { - return vtype2_yxy_ref < vtype > (this); - } - vtype2_yyx_ref < vtype > yyx() - { - return vtype2_yyx_ref < vtype > (this); - } - vtype2_yyy_ref < vtype > yyy() - { - return vtype2_yyy_ref < vtype > (this); - } - vtype2_xxxx_ref < vtype > xxxx() - { - return vtype2_xxxx_ref < vtype > (this); - } - vtype2_xxxy_ref < vtype > xxxy() - { - return vtype2_xxxy_ref < vtype > (this); - } - vtype2_xxyx_ref < vtype > xxyx() - { - return vtype2_xxyx_ref < vtype > (this); - } - vtype2_xxyy_ref < vtype > xxyy() - { - return vtype2_xxyy_ref < vtype > (this); - } - vtype2_xyxx_ref < vtype > xyxx() - { - return vtype2_xyxx_ref < vtype > (this); - } - vtype2_xyxy_ref < vtype > xyxy() - { - return vtype2_xyxy_ref < vtype > (this); - } - vtype2_xyyx_ref < vtype > xyyx() - { - return vtype2_xyyx_ref < vtype > (this); - } - vtype2_xyyy_ref < vtype > xyyy() - { - return vtype2_xyyy_ref < vtype > (this); - } - vtype2_yxxx_ref < vtype > yxxx() - { - return vtype2_yxxx_ref < vtype > (this); - } - vtype2_yxxy_ref < vtype > yxxy() - { - return vtype2_yxxy_ref < vtype > (this); - } - vtype2_yxyx_ref < vtype > yxyx() - { - return vtype2_yxyx_ref < vtype > (this); - } - vtype2_yxyy_ref < vtype > yxyy() - { - return vtype2_yxyy_ref < vtype > (this); - } - vtype2_yyxx_ref < vtype > yyxx() - { - return vtype2_yyxx_ref < vtype > (this); - } - vtype2_yyxy_ref < vtype > yyxy() - { - return vtype2_yyxy_ref < vtype > (this); - } - vtype2_yyyx_ref < vtype > yyyx() - { - return vtype2_yyyx_ref < vtype > (this); - } - vtype2_yyyy_ref < vtype > yyyy() - { - return vtype2_yyyy_ref < vtype > (this); - } -}; - -template < typename vtype > class vtype3 -{ - public: - vtype x, y, z; - vtype3() - { - }; - vtype3(vtype p, vtype q, vtype r):x(p), y(q), z(r) - { - }; - vtype3(const vtype3 & p):x(p.x), y(p.y), z(p.z) - { - }; - vtype3(vtype p, const vtype2 < vtype > &q):x(p), y(q.x), z(q.y) - { - }; - vtype3(const vtype2 < vtype > &p, vtype q):x(p.x), y(p.y), z(q) - { - }; - inline vtype3(const vtype2_xxx_ref < vtype > &v); - inline vtype3(const vtype2_xxy_ref < vtype > &v); - inline vtype3(const vtype2_xyx_ref < vtype > &v); - inline vtype3(const vtype2_xyy_ref < vtype > &v); - inline vtype3(const vtype2_yxx_ref < vtype > &v); - inline vtype3(const vtype2_yxy_ref < vtype > &v); - inline vtype3(const vtype2_yyx_ref < vtype > &v); - inline vtype3(const vtype2_yyy_ref < vtype > &v); - inline vtype3(const vtype3_xxx_ref < vtype > &v); - inline vtype3(const vtype3_xxy_ref < vtype > &v); - inline vtype3(const vtype3_xxz_ref < vtype > &v); - inline vtype3(const vtype3_xyx_ref < vtype > &v); - inline vtype3(const vtype3_xyy_ref < vtype > &v); - inline vtype3(const vtype3_xyz_ref < vtype > &v); - inline vtype3(const vtype3_xzx_ref < vtype > &v); - inline vtype3(const vtype3_xzy_ref < vtype > &v); - inline vtype3(const vtype3_xzz_ref < vtype > &v); - inline vtype3(const vtype3_yxx_ref < vtype > &v); - inline vtype3(const vtype3_yxy_ref < vtype > &v); - inline vtype3(const vtype3_yxz_ref < vtype > &v); - inline vtype3(const vtype3_yyx_ref < vtype > &v); - inline vtype3(const vtype3_yyy_ref < vtype > &v); - inline vtype3(const vtype3_yyz_ref < vtype > &v); - inline vtype3(const vtype3_yzx_ref < vtype > &v); - inline vtype3(const vtype3_yzy_ref < vtype > &v); - inline vtype3(const vtype3_yzz_ref < vtype > &v); - inline vtype3(const vtype3_zxx_ref < vtype > &v); - inline vtype3(const vtype3_zxy_ref < vtype > &v); - inline vtype3(const vtype3_zxz_ref < vtype > &v); - inline vtype3(const vtype3_zyx_ref < vtype > &v); - inline vtype3(const vtype3_zyy_ref < vtype > &v); - inline vtype3(const vtype3_zyz_ref < vtype > &v); - inline vtype3(const vtype3_zzx_ref < vtype > &v); - inline vtype3(const vtype3_zzy_ref < vtype > &v); - inline vtype3(const vtype3_zzz_ref < vtype > &v); - inline vtype3(const vtype4_xxx_ref < vtype > &v); - inline vtype3(const vtype4_xxy_ref < vtype > &v); - inline vtype3(const vtype4_xxz_ref < vtype > &v); - inline vtype3(const vtype4_xxw_ref < vtype > &v); - inline vtype3(const vtype4_xyx_ref < vtype > &v); - inline vtype3(const vtype4_xyy_ref < vtype > &v); - inline vtype3(const vtype4_xyz_ref < vtype > &v); - inline vtype3(const vtype4_xyw_ref < vtype > &v); - inline vtype3(const vtype4_xzx_ref < vtype > &v); - inline vtype3(const vtype4_xzy_ref < vtype > &v); - inline vtype3(const vtype4_xzz_ref < vtype > &v); - inline vtype3(const vtype4_xzw_ref < vtype > &v); - inline vtype3(const vtype4_xwx_ref < vtype > &v); - inline vtype3(const vtype4_xwy_ref < vtype > &v); - inline vtype3(const vtype4_xwz_ref < vtype > &v); - inline vtype3(const vtype4_xww_ref < vtype > &v); - inline vtype3(const vtype4_yxx_ref < vtype > &v); - inline vtype3(const vtype4_yxy_ref < vtype > &v); - inline vtype3(const vtype4_yxz_ref < vtype > &v); - inline vtype3(const vtype4_yxw_ref < vtype > &v); - inline vtype3(const vtype4_yyx_ref < vtype > &v); - inline vtype3(const vtype4_yyy_ref < vtype > &v); - inline vtype3(const vtype4_yyz_ref < vtype > &v); - inline vtype3(const vtype4_yyw_ref < vtype > &v); - inline vtype3(const vtype4_yzx_ref < vtype > &v); - inline vtype3(const vtype4_yzy_ref < vtype > &v); - inline vtype3(const vtype4_yzz_ref < vtype > &v); - inline vtype3(const vtype4_yzw_ref < vtype > &v); - inline vtype3(const vtype4_ywx_ref < vtype > &v); - inline vtype3(const vtype4_ywy_ref < vtype > &v); - inline vtype3(const vtype4_ywz_ref < vtype > &v); - inline vtype3(const vtype4_yww_ref < vtype > &v); - inline vtype3(const vtype4_zxx_ref < vtype > &v); - inline vtype3(const vtype4_zxy_ref < vtype > &v); - inline vtype3(const vtype4_zxz_ref < vtype > &v); - inline vtype3(const vtype4_zxw_ref < vtype > &v); - inline vtype3(const vtype4_zyx_ref < vtype > &v); - inline vtype3(const vtype4_zyy_ref < vtype > &v); - inline vtype3(const vtype4_zyz_ref < vtype > &v); - inline vtype3(const vtype4_zyw_ref < vtype > &v); - inline vtype3(const vtype4_zzx_ref < vtype > &v); - inline vtype3(const vtype4_zzy_ref < vtype > &v); - inline vtype3(const vtype4_zzz_ref < vtype > &v); - inline vtype3(const vtype4_zzw_ref < vtype > &v); - inline vtype3(const vtype4_zwx_ref < vtype > &v); - inline vtype3(const vtype4_zwy_ref < vtype > &v); - inline vtype3(const vtype4_zwz_ref < vtype > &v); - inline vtype3(const vtype4_zww_ref < vtype > &v); - inline vtype3(const vtype4_wxx_ref < vtype > &v); - inline vtype3(const vtype4_wxy_ref < vtype > &v); - inline vtype3(const vtype4_wxz_ref < vtype > &v); - inline vtype3(const vtype4_wxw_ref < vtype > &v); - inline vtype3(const vtype4_wyx_ref < vtype > &v); - inline vtype3(const vtype4_wyy_ref < vtype > &v); - inline vtype3(const vtype4_wyz_ref < vtype > &v); - inline vtype3(const vtype4_wyw_ref < vtype > &v); - inline vtype3(const vtype4_wzx_ref < vtype > &v); - inline vtype3(const vtype4_wzy_ref < vtype > &v); - inline vtype3(const vtype4_wzz_ref < vtype > &v); - inline vtype3(const vtype4_wzw_ref < vtype > &v); - inline vtype3(const vtype4_wwx_ref < vtype > &v); - inline vtype3(const vtype4_wwy_ref < vtype > &v); - inline vtype3(const vtype4_wwz_ref < vtype > &v); - inline vtype3(const vtype4_www_ref < vtype > &v); - vtype3_xx_ref < vtype > xx() - { - return vtype3_xx_ref < vtype > (this); - } - vtype3_xy_ref < vtype > xy() - { - return vtype3_xy_ref < vtype > (this); - } - vtype3_xz_ref < vtype > xz() - { - return vtype3_xz_ref < vtype > (this); - } - vtype3_yx_ref < vtype > yx() - { - return vtype3_yx_ref < vtype > (this); - } - vtype3_yy_ref < vtype > yy() - { - return vtype3_yy_ref < vtype > (this); - } - vtype3_yz_ref < vtype > yz() - { - return vtype3_yz_ref < vtype > (this); - } - vtype3_zx_ref < vtype > zx() - { - return vtype3_zx_ref < vtype > (this); - } - vtype3_zy_ref < vtype > zy() - { - return vtype3_zy_ref < vtype > (this); - } - vtype3_zz_ref < vtype > zz() - { - return vtype3_zz_ref < vtype > (this); - } - vtype3_xxx_ref < vtype > xxx() - { - return vtype3_xxx_ref < vtype > (this); - } - vtype3_xxy_ref < vtype > xxy() - { - return vtype3_xxy_ref < vtype > (this); - } - vtype3_xxz_ref < vtype > xxz() - { - return vtype3_xxz_ref < vtype > (this); - } - vtype3_xyx_ref < vtype > xyx() - { - return vtype3_xyx_ref < vtype > (this); - } - vtype3_xyy_ref < vtype > xyy() - { - return vtype3_xyy_ref < vtype > (this); - } - vtype3_xyz_ref < vtype > xyz() - { - return vtype3_xyz_ref < vtype > (this); - } - vtype3_xzx_ref < vtype > xzx() - { - return vtype3_xzx_ref < vtype > (this); - } - vtype3_xzy_ref < vtype > xzy() - { - return vtype3_xzy_ref < vtype > (this); - } - vtype3_xzz_ref < vtype > xzz() - { - return vtype3_xzz_ref < vtype > (this); - } - vtype3_yxx_ref < vtype > yxx() - { - return vtype3_yxx_ref < vtype > (this); - } - vtype3_yxy_ref < vtype > yxy() - { - return vtype3_yxy_ref < vtype > (this); - } - vtype3_yxz_ref < vtype > yxz() - { - return vtype3_yxz_ref < vtype > (this); - } - vtype3_yyx_ref < vtype > yyx() - { - return vtype3_yyx_ref < vtype > (this); - } - vtype3_yyy_ref < vtype > yyy() - { - return vtype3_yyy_ref < vtype > (this); - } - vtype3_yyz_ref < vtype > yyz() - { - return vtype3_yyz_ref < vtype > (this); - } - vtype3_yzx_ref < vtype > yzx() - { - return vtype3_yzx_ref < vtype > (this); - } - vtype3_yzy_ref < vtype > yzy() - { - return vtype3_yzy_ref < vtype > (this); - } - vtype3_yzz_ref < vtype > yzz() - { - return vtype3_yzz_ref < vtype > (this); - } - vtype3_zxx_ref < vtype > zxx() - { - return vtype3_zxx_ref < vtype > (this); - } - vtype3_zxy_ref < vtype > zxy() - { - return vtype3_zxy_ref < vtype > (this); - } - vtype3_zxz_ref < vtype > zxz() - { - return vtype3_zxz_ref < vtype > (this); - } - vtype3_zyx_ref < vtype > zyx() - { - return vtype3_zyx_ref < vtype > (this); - } - vtype3_zyy_ref < vtype > zyy() - { - return vtype3_zyy_ref < vtype > (this); - } - vtype3_zyz_ref < vtype > zyz() - { - return vtype3_zyz_ref < vtype > (this); - } - vtype3_zzx_ref < vtype > zzx() - { - return vtype3_zzx_ref < vtype > (this); - } - vtype3_zzy_ref < vtype > zzy() - { - return vtype3_zzy_ref < vtype > (this); - } - vtype3_zzz_ref < vtype > zzz() - { - return vtype3_zzz_ref < vtype > (this); - } - vtype3_xxxx_ref < vtype > xxxx() - { - return vtype3_xxxx_ref < vtype > (this); - } - vtype3_xxxy_ref < vtype > xxxy() - { - return vtype3_xxxy_ref < vtype > (this); - } - vtype3_xxxz_ref < vtype > xxxz() - { - return vtype3_xxxz_ref < vtype > (this); - } - vtype3_xxyx_ref < vtype > xxyx() - { - return vtype3_xxyx_ref < vtype > (this); - } - vtype3_xxyy_ref < vtype > xxyy() - { - return vtype3_xxyy_ref < vtype > (this); - } - vtype3_xxyz_ref < vtype > xxyz() - { - return vtype3_xxyz_ref < vtype > (this); - } - vtype3_xxzx_ref < vtype > xxzx() - { - return vtype3_xxzx_ref < vtype > (this); - } - vtype3_xxzy_ref < vtype > xxzy() - { - return vtype3_xxzy_ref < vtype > (this); - } - vtype3_xxzz_ref < vtype > xxzz() - { - return vtype3_xxzz_ref < vtype > (this); - } - vtype3_xyxx_ref < vtype > xyxx() - { - return vtype3_xyxx_ref < vtype > (this); - } - vtype3_xyxy_ref < vtype > xyxy() - { - return vtype3_xyxy_ref < vtype > (this); - } - vtype3_xyxz_ref < vtype > xyxz() - { - return vtype3_xyxz_ref < vtype > (this); - } - vtype3_xyyx_ref < vtype > xyyx() - { - return vtype3_xyyx_ref < vtype > (this); - } - vtype3_xyyy_ref < vtype > xyyy() - { - return vtype3_xyyy_ref < vtype > (this); - } - vtype3_xyyz_ref < vtype > xyyz() - { - return vtype3_xyyz_ref < vtype > (this); - } - vtype3_xyzx_ref < vtype > xyzx() - { - return vtype3_xyzx_ref < vtype > (this); - } - vtype3_xyzy_ref < vtype > xyzy() - { - return vtype3_xyzy_ref < vtype > (this); - } - vtype3_xyzz_ref < vtype > xyzz() - { - return vtype3_xyzz_ref < vtype > (this); - } - vtype3_xzxx_ref < vtype > xzxx() - { - return vtype3_xzxx_ref < vtype > (this); - } - vtype3_xzxy_ref < vtype > xzxy() - { - return vtype3_xzxy_ref < vtype > (this); - } - vtype3_xzxz_ref < vtype > xzxz() - { - return vtype3_xzxz_ref < vtype > (this); - } - vtype3_xzyx_ref < vtype > xzyx() - { - return vtype3_xzyx_ref < vtype > (this); - } - vtype3_xzyy_ref < vtype > xzyy() - { - return vtype3_xzyy_ref < vtype > (this); - } - vtype3_xzyz_ref < vtype > xzyz() - { - return vtype3_xzyz_ref < vtype > (this); - } - vtype3_xzzx_ref < vtype > xzzx() - { - return vtype3_xzzx_ref < vtype > (this); - } - vtype3_xzzy_ref < vtype > xzzy() - { - return vtype3_xzzy_ref < vtype > (this); - } - vtype3_xzzz_ref < vtype > xzzz() - { - return vtype3_xzzz_ref < vtype > (this); - } - vtype3_yxxx_ref < vtype > yxxx() - { - return vtype3_yxxx_ref < vtype > (this); - } - vtype3_yxxy_ref < vtype > yxxy() - { - return vtype3_yxxy_ref < vtype > (this); - } - vtype3_yxxz_ref < vtype > yxxz() - { - return vtype3_yxxz_ref < vtype > (this); - } - vtype3_yxyx_ref < vtype > yxyx() - { - return vtype3_yxyx_ref < vtype > (this); - } - vtype3_yxyy_ref < vtype > yxyy() - { - return vtype3_yxyy_ref < vtype > (this); - } - vtype3_yxyz_ref < vtype > yxyz() - { - return vtype3_yxyz_ref < vtype > (this); - } - vtype3_yxzx_ref < vtype > yxzx() - { - return vtype3_yxzx_ref < vtype > (this); - } - vtype3_yxzy_ref < vtype > yxzy() - { - return vtype3_yxzy_ref < vtype > (this); - } - vtype3_yxzz_ref < vtype > yxzz() - { - return vtype3_yxzz_ref < vtype > (this); - } - vtype3_yyxx_ref < vtype > yyxx() - { - return vtype3_yyxx_ref < vtype > (this); - } - vtype3_yyxy_ref < vtype > yyxy() - { - return vtype3_yyxy_ref < vtype > (this); - } - vtype3_yyxz_ref < vtype > yyxz() - { - return vtype3_yyxz_ref < vtype > (this); - } - vtype3_yyyx_ref < vtype > yyyx() - { - return vtype3_yyyx_ref < vtype > (this); - } - vtype3_yyyy_ref < vtype > yyyy() - { - return vtype3_yyyy_ref < vtype > (this); - } - vtype3_yyyz_ref < vtype > yyyz() - { - return vtype3_yyyz_ref < vtype > (this); - } - vtype3_yyzx_ref < vtype > yyzx() - { - return vtype3_yyzx_ref < vtype > (this); - } - vtype3_yyzy_ref < vtype > yyzy() - { - return vtype3_yyzy_ref < vtype > (this); - } - vtype3_yyzz_ref < vtype > yyzz() - { - return vtype3_yyzz_ref < vtype > (this); - } - vtype3_yzxx_ref < vtype > yzxx() - { - return vtype3_yzxx_ref < vtype > (this); - } - vtype3_yzxy_ref < vtype > yzxy() - { - return vtype3_yzxy_ref < vtype > (this); - } - vtype3_yzxz_ref < vtype > yzxz() - { - return vtype3_yzxz_ref < vtype > (this); - } - vtype3_yzyx_ref < vtype > yzyx() - { - return vtype3_yzyx_ref < vtype > (this); - } - vtype3_yzyy_ref < vtype > yzyy() - { - return vtype3_yzyy_ref < vtype > (this); - } - vtype3_yzyz_ref < vtype > yzyz() - { - return vtype3_yzyz_ref < vtype > (this); - } - vtype3_yzzx_ref < vtype > yzzx() - { - return vtype3_yzzx_ref < vtype > (this); - } - vtype3_yzzy_ref < vtype > yzzy() - { - return vtype3_yzzy_ref < vtype > (this); - } - vtype3_yzzz_ref < vtype > yzzz() - { - return vtype3_yzzz_ref < vtype > (this); - } - vtype3_zxxx_ref < vtype > zxxx() - { - return vtype3_zxxx_ref < vtype > (this); - } - vtype3_zxxy_ref < vtype > zxxy() - { - return vtype3_zxxy_ref < vtype > (this); - } - vtype3_zxxz_ref < vtype > zxxz() - { - return vtype3_zxxz_ref < vtype > (this); - } - vtype3_zxyx_ref < vtype > zxyx() - { - return vtype3_zxyx_ref < vtype > (this); - } - vtype3_zxyy_ref < vtype > zxyy() - { - return vtype3_zxyy_ref < vtype > (this); - } - vtype3_zxyz_ref < vtype > zxyz() - { - return vtype3_zxyz_ref < vtype > (this); - } - vtype3_zxzx_ref < vtype > zxzx() - { - return vtype3_zxzx_ref < vtype > (this); - } - vtype3_zxzy_ref < vtype > zxzy() - { - return vtype3_zxzy_ref < vtype > (this); - } - vtype3_zxzz_ref < vtype > zxzz() - { - return vtype3_zxzz_ref < vtype > (this); - } - vtype3_zyxx_ref < vtype > zyxx() - { - return vtype3_zyxx_ref < vtype > (this); - } - vtype3_zyxy_ref < vtype > zyxy() - { - return vtype3_zyxy_ref < vtype > (this); - } - vtype3_zyxz_ref < vtype > zyxz() - { - return vtype3_zyxz_ref < vtype > (this); - } - vtype3_zyyx_ref < vtype > zyyx() - { - return vtype3_zyyx_ref < vtype > (this); - } - vtype3_zyyy_ref < vtype > zyyy() - { - return vtype3_zyyy_ref < vtype > (this); - } - vtype3_zyyz_ref < vtype > zyyz() - { - return vtype3_zyyz_ref < vtype > (this); - } - vtype3_zyzx_ref < vtype > zyzx() - { - return vtype3_zyzx_ref < vtype > (this); - } - vtype3_zyzy_ref < vtype > zyzy() - { - return vtype3_zyzy_ref < vtype > (this); - } - vtype3_zyzz_ref < vtype > zyzz() - { - return vtype3_zyzz_ref < vtype > (this); - } - vtype3_zzxx_ref < vtype > zzxx() - { - return vtype3_zzxx_ref < vtype > (this); - } - vtype3_zzxy_ref < vtype > zzxy() - { - return vtype3_zzxy_ref < vtype > (this); - } - vtype3_zzxz_ref < vtype > zzxz() - { - return vtype3_zzxz_ref < vtype > (this); - } - vtype3_zzyx_ref < vtype > zzyx() - { - return vtype3_zzyx_ref < vtype > (this); - } - vtype3_zzyy_ref < vtype > zzyy() - { - return vtype3_zzyy_ref < vtype > (this); - } - vtype3_zzyz_ref < vtype > zzyz() - { - return vtype3_zzyz_ref < vtype > (this); - } - vtype3_zzzx_ref < vtype > zzzx() - { - return vtype3_zzzx_ref < vtype > (this); - } - vtype3_zzzy_ref < vtype > zzzy() - { - return vtype3_zzzy_ref < vtype > (this); - } - vtype3_zzzz_ref < vtype > zzzz() - { - return vtype3_zzzz_ref < vtype > (this); - } -}; - -template < typename vtype > class vtype4 -{ - public: - vtype x, y, z, w; - vtype4() - { - }; - vtype4(vtype p, vtype q, vtype r, vtype s):x(p), y(q), z(r), w(s) - { - }; - vtype4(const vtype2 < vtype > &p, const vtype2 < vtype > &q):x(p.x), y(p.y), z(q.x), w(q.y) - { - }; - vtype4(const vtype2 < vtype > &p, vtype q, vtype r):x(p.x), y(p.y), z(q), w(r) - { - }; - vtype4(vtype p, const vtype2 < vtype > &q, vtype r):x(p), y(q.x), z(q.y), w(r) - { - }; - vtype4(vtype p, vtype q, const vtype2 < vtype > &r):x(p), y(q), z(r.x), w(r.y) - { - }; - vtype4(const vtype3 < vtype > &p, vtype q):x(p.x), y(p.y), z(p.z), w(q) - { - }; - vtype4(vtype p, const vtype3 < vtype > &q):x(p), y(q.x), z(q.y), w(q.z) - { - }; - vtype4(const vtype4 & p):x(p.x), y(p.y), z(p.z), w(p.w) - { - }; - inline vtype4(const vtype2_xxxx_ref < vtype > &v); - inline vtype4(const vtype2_xxxy_ref < vtype > &v); - inline vtype4(const vtype2_xxyx_ref < vtype > &v); - inline vtype4(const vtype2_xxyy_ref < vtype > &v); - inline vtype4(const vtype2_xyxx_ref < vtype > &v); - inline vtype4(const vtype2_xyxy_ref < vtype > &v); - inline vtype4(const vtype2_xyyx_ref < vtype > &v); - inline vtype4(const vtype2_xyyy_ref < vtype > &v); - inline vtype4(const vtype2_yxxx_ref < vtype > &v); - inline vtype4(const vtype2_yxxy_ref < vtype > &v); - inline vtype4(const vtype2_yxyx_ref < vtype > &v); - inline vtype4(const vtype2_yxyy_ref < vtype > &v); - inline vtype4(const vtype2_yyxx_ref < vtype > &v); - inline vtype4(const vtype2_yyxy_ref < vtype > &v); - inline vtype4(const vtype2_yyyx_ref < vtype > &v); - inline vtype4(const vtype2_yyyy_ref < vtype > &v); - inline vtype4(const vtype3_xxxx_ref < vtype > &v); - inline vtype4(const vtype3_xxxy_ref < vtype > &v); - inline vtype4(const vtype3_xxxz_ref < vtype > &v); - inline vtype4(const vtype3_xxyx_ref < vtype > &v); - inline vtype4(const vtype3_xxyy_ref < vtype > &v); - inline vtype4(const vtype3_xxyz_ref < vtype > &v); - inline vtype4(const vtype3_xxzx_ref < vtype > &v); - inline vtype4(const vtype3_xxzy_ref < vtype > &v); - inline vtype4(const vtype3_xxzz_ref < vtype > &v); - inline vtype4(const vtype3_xyxx_ref < vtype > &v); - inline vtype4(const vtype3_xyxy_ref < vtype > &v); - inline vtype4(const vtype3_xyxz_ref < vtype > &v); - inline vtype4(const vtype3_xyyx_ref < vtype > &v); - inline vtype4(const vtype3_xyyy_ref < vtype > &v); - inline vtype4(const vtype3_xyyz_ref < vtype > &v); - inline vtype4(const vtype3_xyzx_ref < vtype > &v); - inline vtype4(const vtype3_xyzy_ref < vtype > &v); - inline vtype4(const vtype3_xyzz_ref < vtype > &v); - inline vtype4(const vtype3_xzxx_ref < vtype > &v); - inline vtype4(const vtype3_xzxy_ref < vtype > &v); - inline vtype4(const vtype3_xzxz_ref < vtype > &v); - inline vtype4(const vtype3_xzyx_ref < vtype > &v); - inline vtype4(const vtype3_xzyy_ref < vtype > &v); - inline vtype4(const vtype3_xzyz_ref < vtype > &v); - inline vtype4(const vtype3_xzzx_ref < vtype > &v); - inline vtype4(const vtype3_xzzy_ref < vtype > &v); - inline vtype4(const vtype3_xzzz_ref < vtype > &v); - inline vtype4(const vtype3_yxxx_ref < vtype > &v); - inline vtype4(const vtype3_yxxy_ref < vtype > &v); - inline vtype4(const vtype3_yxxz_ref < vtype > &v); - inline vtype4(const vtype3_yxyx_ref < vtype > &v); - inline vtype4(const vtype3_yxyy_ref < vtype > &v); - inline vtype4(const vtype3_yxyz_ref < vtype > &v); - inline vtype4(const vtype3_yxzx_ref < vtype > &v); - inline vtype4(const vtype3_yxzy_ref < vtype > &v); - inline vtype4(const vtype3_yxzz_ref < vtype > &v); - inline vtype4(const vtype3_yyxx_ref < vtype > &v); - inline vtype4(const vtype3_yyxy_ref < vtype > &v); - inline vtype4(const vtype3_yyxz_ref < vtype > &v); - inline vtype4(const vtype3_yyyx_ref < vtype > &v); - inline vtype4(const vtype3_yyyy_ref < vtype > &v); - inline vtype4(const vtype3_yyyz_ref < vtype > &v); - inline vtype4(const vtype3_yyzx_ref < vtype > &v); - inline vtype4(const vtype3_yyzy_ref < vtype > &v); - inline vtype4(const vtype3_yyzz_ref < vtype > &v); - inline vtype4(const vtype3_yzxx_ref < vtype > &v); - inline vtype4(const vtype3_yzxy_ref < vtype > &v); - inline vtype4(const vtype3_yzxz_ref < vtype > &v); - inline vtype4(const vtype3_yzyx_ref < vtype > &v); - inline vtype4(const vtype3_yzyy_ref < vtype > &v); - inline vtype4(const vtype3_yzyz_ref < vtype > &v); - inline vtype4(const vtype3_yzzx_ref < vtype > &v); - inline vtype4(const vtype3_yzzy_ref < vtype > &v); - inline vtype4(const vtype3_yzzz_ref < vtype > &v); - inline vtype4(const vtype3_zxxx_ref < vtype > &v); - inline vtype4(const vtype3_zxxy_ref < vtype > &v); - inline vtype4(const vtype3_zxxz_ref < vtype > &v); - inline vtype4(const vtype3_zxyx_ref < vtype > &v); - inline vtype4(const vtype3_zxyy_ref < vtype > &v); - inline vtype4(const vtype3_zxyz_ref < vtype > &v); - inline vtype4(const vtype3_zxzx_ref < vtype > &v); - inline vtype4(const vtype3_zxzy_ref < vtype > &v); - inline vtype4(const vtype3_zxzz_ref < vtype > &v); - inline vtype4(const vtype3_zyxx_ref < vtype > &v); - inline vtype4(const vtype3_zyxy_ref < vtype > &v); - inline vtype4(const vtype3_zyxz_ref < vtype > &v); - inline vtype4(const vtype3_zyyx_ref < vtype > &v); - inline vtype4(const vtype3_zyyy_ref < vtype > &v); - inline vtype4(const vtype3_zyyz_ref < vtype > &v); - inline vtype4(const vtype3_zyzx_ref < vtype > &v); - inline vtype4(const vtype3_zyzy_ref < vtype > &v); - inline vtype4(const vtype3_zyzz_ref < vtype > &v); - inline vtype4(const vtype3_zzxx_ref < vtype > &v); - inline vtype4(const vtype3_zzxy_ref < vtype > &v); - inline vtype4(const vtype3_zzxz_ref < vtype > &v); - inline vtype4(const vtype3_zzyx_ref < vtype > &v); - inline vtype4(const vtype3_zzyy_ref < vtype > &v); - inline vtype4(const vtype3_zzyz_ref < vtype > &v); - inline vtype4(const vtype3_zzzx_ref < vtype > &v); - inline vtype4(const vtype3_zzzy_ref < vtype > &v); - inline vtype4(const vtype3_zzzz_ref < vtype > &v); - inline vtype4(const vtype4_xxxx_ref < vtype > &v); - inline vtype4(const vtype4_xxxy_ref < vtype > &v); - inline vtype4(const vtype4_xxxz_ref < vtype > &v); - inline vtype4(const vtype4_xxxw_ref < vtype > &v); - inline vtype4(const vtype4_xxyx_ref < vtype > &v); - inline vtype4(const vtype4_xxyy_ref < vtype > &v); - inline vtype4(const vtype4_xxyz_ref < vtype > &v); - inline vtype4(const vtype4_xxyw_ref < vtype > &v); - inline vtype4(const vtype4_xxzx_ref < vtype > &v); - inline vtype4(const vtype4_xxzy_ref < vtype > &v); - inline vtype4(const vtype4_xxzz_ref < vtype > &v); - inline vtype4(const vtype4_xxzw_ref < vtype > &v); - inline vtype4(const vtype4_xxwx_ref < vtype > &v); - inline vtype4(const vtype4_xxwy_ref < vtype > &v); - inline vtype4(const vtype4_xxwz_ref < vtype > &v); - inline vtype4(const vtype4_xxww_ref < vtype > &v); - inline vtype4(const vtype4_xyxx_ref < vtype > &v); - inline vtype4(const vtype4_xyxy_ref < vtype > &v); - inline vtype4(const vtype4_xyxz_ref < vtype > &v); - inline vtype4(const vtype4_xyxw_ref < vtype > &v); - inline vtype4(const vtype4_xyyx_ref < vtype > &v); - inline vtype4(const vtype4_xyyy_ref < vtype > &v); - inline vtype4(const vtype4_xyyz_ref < vtype > &v); - inline vtype4(const vtype4_xyyw_ref < vtype > &v); - inline vtype4(const vtype4_xyzx_ref < vtype > &v); - inline vtype4(const vtype4_xyzy_ref < vtype > &v); - inline vtype4(const vtype4_xyzz_ref < vtype > &v); - inline vtype4(const vtype4_xyzw_ref < vtype > &v); - inline vtype4(const vtype4_xywx_ref < vtype > &v); - inline vtype4(const vtype4_xywy_ref < vtype > &v); - inline vtype4(const vtype4_xywz_ref < vtype > &v); - inline vtype4(const vtype4_xyww_ref < vtype > &v); - inline vtype4(const vtype4_xzxx_ref < vtype > &v); - inline vtype4(const vtype4_xzxy_ref < vtype > &v); - inline vtype4(const vtype4_xzxz_ref < vtype > &v); - inline vtype4(const vtype4_xzxw_ref < vtype > &v); - inline vtype4(const vtype4_xzyx_ref < vtype > &v); - inline vtype4(const vtype4_xzyy_ref < vtype > &v); - inline vtype4(const vtype4_xzyz_ref < vtype > &v); - inline vtype4(const vtype4_xzyw_ref < vtype > &v); - inline vtype4(const vtype4_xzzx_ref < vtype > &v); - inline vtype4(const vtype4_xzzy_ref < vtype > &v); - inline vtype4(const vtype4_xzzz_ref < vtype > &v); - inline vtype4(const vtype4_xzzw_ref < vtype > &v); - inline vtype4(const vtype4_xzwx_ref < vtype > &v); - inline vtype4(const vtype4_xzwy_ref < vtype > &v); - inline vtype4(const vtype4_xzwz_ref < vtype > &v); - inline vtype4(const vtype4_xzww_ref < vtype > &v); - inline vtype4(const vtype4_xwxx_ref < vtype > &v); - inline vtype4(const vtype4_xwxy_ref < vtype > &v); - inline vtype4(const vtype4_xwxz_ref < vtype > &v); - inline vtype4(const vtype4_xwxw_ref < vtype > &v); - inline vtype4(const vtype4_xwyx_ref < vtype > &v); - inline vtype4(const vtype4_xwyy_ref < vtype > &v); - inline vtype4(const vtype4_xwyz_ref < vtype > &v); - inline vtype4(const vtype4_xwyw_ref < vtype > &v); - inline vtype4(const vtype4_xwzx_ref < vtype > &v); - inline vtype4(const vtype4_xwzy_ref < vtype > &v); - inline vtype4(const vtype4_xwzz_ref < vtype > &v); - inline vtype4(const vtype4_xwzw_ref < vtype > &v); - inline vtype4(const vtype4_xwwx_ref < vtype > &v); - inline vtype4(const vtype4_xwwy_ref < vtype > &v); - inline vtype4(const vtype4_xwwz_ref < vtype > &v); - inline vtype4(const vtype4_xwww_ref < vtype > &v); - inline vtype4(const vtype4_yxxx_ref < vtype > &v); - inline vtype4(const vtype4_yxxy_ref < vtype > &v); - inline vtype4(const vtype4_yxxz_ref < vtype > &v); - inline vtype4(const vtype4_yxxw_ref < vtype > &v); - inline vtype4(const vtype4_yxyx_ref < vtype > &v); - inline vtype4(const vtype4_yxyy_ref < vtype > &v); - inline vtype4(const vtype4_yxyz_ref < vtype > &v); - inline vtype4(const vtype4_yxyw_ref < vtype > &v); - inline vtype4(const vtype4_yxzx_ref < vtype > &v); - inline vtype4(const vtype4_yxzy_ref < vtype > &v); - inline vtype4(const vtype4_yxzz_ref < vtype > &v); - inline vtype4(const vtype4_yxzw_ref < vtype > &v); - inline vtype4(const vtype4_yxwx_ref < vtype > &v); - inline vtype4(const vtype4_yxwy_ref < vtype > &v); - inline vtype4(const vtype4_yxwz_ref < vtype > &v); - inline vtype4(const vtype4_yxww_ref < vtype > &v); - inline vtype4(const vtype4_yyxx_ref < vtype > &v); - inline vtype4(const vtype4_yyxy_ref < vtype > &v); - inline vtype4(const vtype4_yyxz_ref < vtype > &v); - inline vtype4(const vtype4_yyxw_ref < vtype > &v); - inline vtype4(const vtype4_yyyx_ref < vtype > &v); - inline vtype4(const vtype4_yyyy_ref < vtype > &v); - inline vtype4(const vtype4_yyyz_ref < vtype > &v); - inline vtype4(const vtype4_yyyw_ref < vtype > &v); - inline vtype4(const vtype4_yyzx_ref < vtype > &v); - inline vtype4(const vtype4_yyzy_ref < vtype > &v); - inline vtype4(const vtype4_yyzz_ref < vtype > &v); - inline vtype4(const vtype4_yyzw_ref < vtype > &v); - inline vtype4(const vtype4_yywx_ref < vtype > &v); - inline vtype4(const vtype4_yywy_ref < vtype > &v); - inline vtype4(const vtype4_yywz_ref < vtype > &v); - inline vtype4(const vtype4_yyww_ref < vtype > &v); - inline vtype4(const vtype4_yzxx_ref < vtype > &v); - inline vtype4(const vtype4_yzxy_ref < vtype > &v); - inline vtype4(const vtype4_yzxz_ref < vtype > &v); - inline vtype4(const vtype4_yzxw_ref < vtype > &v); - inline vtype4(const vtype4_yzyx_ref < vtype > &v); - inline vtype4(const vtype4_yzyy_ref < vtype > &v); - inline vtype4(const vtype4_yzyz_ref < vtype > &v); - inline vtype4(const vtype4_yzyw_ref < vtype > &v); - inline vtype4(const vtype4_yzzx_ref < vtype > &v); - inline vtype4(const vtype4_yzzy_ref < vtype > &v); - inline vtype4(const vtype4_yzzz_ref < vtype > &v); - inline vtype4(const vtype4_yzzw_ref < vtype > &v); - inline vtype4(const vtype4_yzwx_ref < vtype > &v); - inline vtype4(const vtype4_yzwy_ref < vtype > &v); - inline vtype4(const vtype4_yzwz_ref < vtype > &v); - inline vtype4(const vtype4_yzww_ref < vtype > &v); - inline vtype4(const vtype4_ywxx_ref < vtype > &v); - inline vtype4(const vtype4_ywxy_ref < vtype > &v); - inline vtype4(const vtype4_ywxz_ref < vtype > &v); - inline vtype4(const vtype4_ywxw_ref < vtype > &v); - inline vtype4(const vtype4_ywyx_ref < vtype > &v); - inline vtype4(const vtype4_ywyy_ref < vtype > &v); - inline vtype4(const vtype4_ywyz_ref < vtype > &v); - inline vtype4(const vtype4_ywyw_ref < vtype > &v); - inline vtype4(const vtype4_ywzx_ref < vtype > &v); - inline vtype4(const vtype4_ywzy_ref < vtype > &v); - inline vtype4(const vtype4_ywzz_ref < vtype > &v); - inline vtype4(const vtype4_ywzw_ref < vtype > &v); - inline vtype4(const vtype4_ywwx_ref < vtype > &v); - inline vtype4(const vtype4_ywwy_ref < vtype > &v); - inline vtype4(const vtype4_ywwz_ref < vtype > &v); - inline vtype4(const vtype4_ywww_ref < vtype > &v); - inline vtype4(const vtype4_zxxx_ref < vtype > &v); - inline vtype4(const vtype4_zxxy_ref < vtype > &v); - inline vtype4(const vtype4_zxxz_ref < vtype > &v); - inline vtype4(const vtype4_zxxw_ref < vtype > &v); - inline vtype4(const vtype4_zxyx_ref < vtype > &v); - inline vtype4(const vtype4_zxyy_ref < vtype > &v); - inline vtype4(const vtype4_zxyz_ref < vtype > &v); - inline vtype4(const vtype4_zxyw_ref < vtype > &v); - inline vtype4(const vtype4_zxzx_ref < vtype > &v); - inline vtype4(const vtype4_zxzy_ref < vtype > &v); - inline vtype4(const vtype4_zxzz_ref < vtype > &v); - inline vtype4(const vtype4_zxzw_ref < vtype > &v); - inline vtype4(const vtype4_zxwx_ref < vtype > &v); - inline vtype4(const vtype4_zxwy_ref < vtype > &v); - inline vtype4(const vtype4_zxwz_ref < vtype > &v); - inline vtype4(const vtype4_zxww_ref < vtype > &v); - inline vtype4(const vtype4_zyxx_ref < vtype > &v); - inline vtype4(const vtype4_zyxy_ref < vtype > &v); - inline vtype4(const vtype4_zyxz_ref < vtype > &v); - inline vtype4(const vtype4_zyxw_ref < vtype > &v); - inline vtype4(const vtype4_zyyx_ref < vtype > &v); - inline vtype4(const vtype4_zyyy_ref < vtype > &v); - inline vtype4(const vtype4_zyyz_ref < vtype > &v); - inline vtype4(const vtype4_zyyw_ref < vtype > &v); - inline vtype4(const vtype4_zyzx_ref < vtype > &v); - inline vtype4(const vtype4_zyzy_ref < vtype > &v); - inline vtype4(const vtype4_zyzz_ref < vtype > &v); - inline vtype4(const vtype4_zyzw_ref < vtype > &v); - inline vtype4(const vtype4_zywx_ref < vtype > &v); - inline vtype4(const vtype4_zywy_ref < vtype > &v); - inline vtype4(const vtype4_zywz_ref < vtype > &v); - inline vtype4(const vtype4_zyww_ref < vtype > &v); - inline vtype4(const vtype4_zzxx_ref < vtype > &v); - inline vtype4(const vtype4_zzxy_ref < vtype > &v); - inline vtype4(const vtype4_zzxz_ref < vtype > &v); - inline vtype4(const vtype4_zzxw_ref < vtype > &v); - inline vtype4(const vtype4_zzyx_ref < vtype > &v); - inline vtype4(const vtype4_zzyy_ref < vtype > &v); - inline vtype4(const vtype4_zzyz_ref < vtype > &v); - inline vtype4(const vtype4_zzyw_ref < vtype > &v); - inline vtype4(const vtype4_zzzx_ref < vtype > &v); - inline vtype4(const vtype4_zzzy_ref < vtype > &v); - inline vtype4(const vtype4_zzzz_ref < vtype > &v); - inline vtype4(const vtype4_zzzw_ref < vtype > &v); - inline vtype4(const vtype4_zzwx_ref < vtype > &v); - inline vtype4(const vtype4_zzwy_ref < vtype > &v); - inline vtype4(const vtype4_zzwz_ref < vtype > &v); - inline vtype4(const vtype4_zzww_ref < vtype > &v); - inline vtype4(const vtype4_zwxx_ref < vtype > &v); - inline vtype4(const vtype4_zwxy_ref < vtype > &v); - inline vtype4(const vtype4_zwxz_ref < vtype > &v); - inline vtype4(const vtype4_zwxw_ref < vtype > &v); - inline vtype4(const vtype4_zwyx_ref < vtype > &v); - inline vtype4(const vtype4_zwyy_ref < vtype > &v); - inline vtype4(const vtype4_zwyz_ref < vtype > &v); - inline vtype4(const vtype4_zwyw_ref < vtype > &v); - inline vtype4(const vtype4_zwzx_ref < vtype > &v); - inline vtype4(const vtype4_zwzy_ref < vtype > &v); - inline vtype4(const vtype4_zwzz_ref < vtype > &v); - inline vtype4(const vtype4_zwzw_ref < vtype > &v); - inline vtype4(const vtype4_zwwx_ref < vtype > &v); - inline vtype4(const vtype4_zwwy_ref < vtype > &v); - inline vtype4(const vtype4_zwwz_ref < vtype > &v); - inline vtype4(const vtype4_zwww_ref < vtype > &v); - inline vtype4(const vtype4_wxxx_ref < vtype > &v); - inline vtype4(const vtype4_wxxy_ref < vtype > &v); - inline vtype4(const vtype4_wxxz_ref < vtype > &v); - inline vtype4(const vtype4_wxxw_ref < vtype > &v); - inline vtype4(const vtype4_wxyx_ref < vtype > &v); - inline vtype4(const vtype4_wxyy_ref < vtype > &v); - inline vtype4(const vtype4_wxyz_ref < vtype > &v); - inline vtype4(const vtype4_wxyw_ref < vtype > &v); - inline vtype4(const vtype4_wxzx_ref < vtype > &v); - inline vtype4(const vtype4_wxzy_ref < vtype > &v); - inline vtype4(const vtype4_wxzz_ref < vtype > &v); - inline vtype4(const vtype4_wxzw_ref < vtype > &v); - inline vtype4(const vtype4_wxwx_ref < vtype > &v); - inline vtype4(const vtype4_wxwy_ref < vtype > &v); - inline vtype4(const vtype4_wxwz_ref < vtype > &v); - inline vtype4(const vtype4_wxww_ref < vtype > &v); - inline vtype4(const vtype4_wyxx_ref < vtype > &v); - inline vtype4(const vtype4_wyxy_ref < vtype > &v); - inline vtype4(const vtype4_wyxz_ref < vtype > &v); - inline vtype4(const vtype4_wyxw_ref < vtype > &v); - inline vtype4(const vtype4_wyyx_ref < vtype > &v); - inline vtype4(const vtype4_wyyy_ref < vtype > &v); - inline vtype4(const vtype4_wyyz_ref < vtype > &v); - inline vtype4(const vtype4_wyyw_ref < vtype > &v); - inline vtype4(const vtype4_wyzx_ref < vtype > &v); - inline vtype4(const vtype4_wyzy_ref < vtype > &v); - inline vtype4(const vtype4_wyzz_ref < vtype > &v); - inline vtype4(const vtype4_wyzw_ref < vtype > &v); - inline vtype4(const vtype4_wywx_ref < vtype > &v); - inline vtype4(const vtype4_wywy_ref < vtype > &v); - inline vtype4(const vtype4_wywz_ref < vtype > &v); - inline vtype4(const vtype4_wyww_ref < vtype > &v); - inline vtype4(const vtype4_wzxx_ref < vtype > &v); - inline vtype4(const vtype4_wzxy_ref < vtype > &v); - inline vtype4(const vtype4_wzxz_ref < vtype > &v); - inline vtype4(const vtype4_wzxw_ref < vtype > &v); - inline vtype4(const vtype4_wzyx_ref < vtype > &v); - inline vtype4(const vtype4_wzyy_ref < vtype > &v); - inline vtype4(const vtype4_wzyz_ref < vtype > &v); - inline vtype4(const vtype4_wzyw_ref < vtype > &v); - inline vtype4(const vtype4_wzzx_ref < vtype > &v); - inline vtype4(const vtype4_wzzy_ref < vtype > &v); - inline vtype4(const vtype4_wzzz_ref < vtype > &v); - inline vtype4(const vtype4_wzzw_ref < vtype > &v); - inline vtype4(const vtype4_wzwx_ref < vtype > &v); - inline vtype4(const vtype4_wzwy_ref < vtype > &v); - inline vtype4(const vtype4_wzwz_ref < vtype > &v); - inline vtype4(const vtype4_wzww_ref < vtype > &v); - inline vtype4(const vtype4_wwxx_ref < vtype > &v); - inline vtype4(const vtype4_wwxy_ref < vtype > &v); - inline vtype4(const vtype4_wwxz_ref < vtype > &v); - inline vtype4(const vtype4_wwxw_ref < vtype > &v); - inline vtype4(const vtype4_wwyx_ref < vtype > &v); - inline vtype4(const vtype4_wwyy_ref < vtype > &v); - inline vtype4(const vtype4_wwyz_ref < vtype > &v); - inline vtype4(const vtype4_wwyw_ref < vtype > &v); - inline vtype4(const vtype4_wwzx_ref < vtype > &v); - inline vtype4(const vtype4_wwzy_ref < vtype > &v); - inline vtype4(const vtype4_wwzz_ref < vtype > &v); - inline vtype4(const vtype4_wwzw_ref < vtype > &v); - inline vtype4(const vtype4_wwwx_ref < vtype > &v); - inline vtype4(const vtype4_wwwy_ref < vtype > &v); - inline vtype4(const vtype4_wwwz_ref < vtype > &v); - inline vtype4(const vtype4_wwww_ref < vtype > &v); - vtype4_xx_ref < vtype > xx() - { - return vtype4_xx_ref < vtype > (this); - } - vtype4_xy_ref < vtype > xy() - { - return vtype4_xy_ref < vtype > (this); - } - vtype4_xz_ref < vtype > xz() - { - return vtype4_xz_ref < vtype > (this); - } - vtype4_xw_ref < vtype > xw() - { - return vtype4_xw_ref < vtype > (this); - } - vtype4_yx_ref < vtype > yx() - { - return vtype4_yx_ref < vtype > (this); - } - vtype4_yy_ref < vtype > yy() - { - return vtype4_yy_ref < vtype > (this); - } - vtype4_yz_ref < vtype > yz() - { - return vtype4_yz_ref < vtype > (this); - } - vtype4_yw_ref < vtype > yw() - { - return vtype4_yw_ref < vtype > (this); - } - vtype4_zx_ref < vtype > zx() - { - return vtype4_zx_ref < vtype > (this); - } - vtype4_zy_ref < vtype > zy() - { - return vtype4_zy_ref < vtype > (this); - } - vtype4_zz_ref < vtype > zz() - { - return vtype4_zz_ref < vtype > (this); - } - vtype4_zw_ref < vtype > zw() - { - return vtype4_zw_ref < vtype > (this); - } - vtype4_wx_ref < vtype > wx() - { - return vtype4_wx_ref < vtype > (this); - } - vtype4_wy_ref < vtype > wy() - { - return vtype4_wy_ref < vtype > (this); - } - vtype4_wz_ref < vtype > wz() - { - return vtype4_wz_ref < vtype > (this); - } - vtype4_ww_ref < vtype > ww() - { - return vtype4_ww_ref < vtype > (this); - } - vtype4_xxx_ref < vtype > xxx() - { - return vtype4_xxx_ref < vtype > (this); - } - vtype4_xxy_ref < vtype > xxy() - { - return vtype4_xxy_ref < vtype > (this); - } - vtype4_xxz_ref < vtype > xxz() - { - return vtype4_xxz_ref < vtype > (this); - } - vtype4_xxw_ref < vtype > xxw() - { - return vtype4_xxw_ref < vtype > (this); - } - vtype4_xyx_ref < vtype > xyx() - { - return vtype4_xyx_ref < vtype > (this); - } - vtype4_xyy_ref < vtype > xyy() - { - return vtype4_xyy_ref < vtype > (this); - } - vtype4_xyz_ref < vtype > xyz() - { - return vtype4_xyz_ref < vtype > (this); - } - vtype4_xyw_ref < vtype > xyw() - { - return vtype4_xyw_ref < vtype > (this); - } - vtype4_xzx_ref < vtype > xzx() - { - return vtype4_xzx_ref < vtype > (this); - } - vtype4_xzy_ref < vtype > xzy() - { - return vtype4_xzy_ref < vtype > (this); - } - vtype4_xzz_ref < vtype > xzz() - { - return vtype4_xzz_ref < vtype > (this); - } - vtype4_xzw_ref < vtype > xzw() - { - return vtype4_xzw_ref < vtype > (this); - } - vtype4_xwx_ref < vtype > xwx() - { - return vtype4_xwx_ref < vtype > (this); - } - vtype4_xwy_ref < vtype > xwy() - { - return vtype4_xwy_ref < vtype > (this); - } - vtype4_xwz_ref < vtype > xwz() - { - return vtype4_xwz_ref < vtype > (this); - } - vtype4_xww_ref < vtype > xww() - { - return vtype4_xww_ref < vtype > (this); - } - vtype4_yxx_ref < vtype > yxx() - { - return vtype4_yxx_ref < vtype > (this); - } - vtype4_yxy_ref < vtype > yxy() - { - return vtype4_yxy_ref < vtype > (this); - } - vtype4_yxz_ref < vtype > yxz() - { - return vtype4_yxz_ref < vtype > (this); - } - vtype4_yxw_ref < vtype > yxw() - { - return vtype4_yxw_ref < vtype > (this); - } - vtype4_yyx_ref < vtype > yyx() - { - return vtype4_yyx_ref < vtype > (this); - } - vtype4_yyy_ref < vtype > yyy() - { - return vtype4_yyy_ref < vtype > (this); - } - vtype4_yyz_ref < vtype > yyz() - { - return vtype4_yyz_ref < vtype > (this); - } - vtype4_yyw_ref < vtype > yyw() - { - return vtype4_yyw_ref < vtype > (this); - } - vtype4_yzx_ref < vtype > yzx() - { - return vtype4_yzx_ref < vtype > (this); - } - vtype4_yzy_ref < vtype > yzy() - { - return vtype4_yzy_ref < vtype > (this); - } - vtype4_yzz_ref < vtype > yzz() - { - return vtype4_yzz_ref < vtype > (this); - } - vtype4_yzw_ref < vtype > yzw() - { - return vtype4_yzw_ref < vtype > (this); - } - vtype4_ywx_ref < vtype > ywx() - { - return vtype4_ywx_ref < vtype > (this); - } - vtype4_ywy_ref < vtype > ywy() - { - return vtype4_ywy_ref < vtype > (this); - } - vtype4_ywz_ref < vtype > ywz() - { - return vtype4_ywz_ref < vtype > (this); - } - vtype4_yww_ref < vtype > yww() - { - return vtype4_yww_ref < vtype > (this); - } - vtype4_zxx_ref < vtype > zxx() - { - return vtype4_zxx_ref < vtype > (this); - } - vtype4_zxy_ref < vtype > zxy() - { - return vtype4_zxy_ref < vtype > (this); - } - vtype4_zxz_ref < vtype > zxz() - { - return vtype4_zxz_ref < vtype > (this); - } - vtype4_zxw_ref < vtype > zxw() - { - return vtype4_zxw_ref < vtype > (this); - } - vtype4_zyx_ref < vtype > zyx() - { - return vtype4_zyx_ref < vtype > (this); - } - vtype4_zyy_ref < vtype > zyy() - { - return vtype4_zyy_ref < vtype > (this); - } - vtype4_zyz_ref < vtype > zyz() - { - return vtype4_zyz_ref < vtype > (this); - } - vtype4_zyw_ref < vtype > zyw() - { - return vtype4_zyw_ref < vtype > (this); - } - vtype4_zzx_ref < vtype > zzx() - { - return vtype4_zzx_ref < vtype > (this); - } - vtype4_zzy_ref < vtype > zzy() - { - return vtype4_zzy_ref < vtype > (this); - } - vtype4_zzz_ref < vtype > zzz() - { - return vtype4_zzz_ref < vtype > (this); - } - vtype4_zzw_ref < vtype > zzw() - { - return vtype4_zzw_ref < vtype > (this); - } - vtype4_zwx_ref < vtype > zwx() - { - return vtype4_zwx_ref < vtype > (this); - } - vtype4_zwy_ref < vtype > zwy() - { - return vtype4_zwy_ref < vtype > (this); - } - vtype4_zwz_ref < vtype > zwz() - { - return vtype4_zwz_ref < vtype > (this); - } - vtype4_zww_ref < vtype > zww() - { - return vtype4_zww_ref < vtype > (this); - } - vtype4_wxx_ref < vtype > wxx() - { - return vtype4_wxx_ref < vtype > (this); - } - vtype4_wxy_ref < vtype > wxy() - { - return vtype4_wxy_ref < vtype > (this); - } - vtype4_wxz_ref < vtype > wxz() - { - return vtype4_wxz_ref < vtype > (this); - } - vtype4_wxw_ref < vtype > wxw() - { - return vtype4_wxw_ref < vtype > (this); - } - vtype4_wyx_ref < vtype > wyx() - { - return vtype4_wyx_ref < vtype > (this); - } - vtype4_wyy_ref < vtype > wyy() - { - return vtype4_wyy_ref < vtype > (this); - } - vtype4_wyz_ref < vtype > wyz() - { - return vtype4_wyz_ref < vtype > (this); - } - vtype4_wyw_ref < vtype > wyw() - { - return vtype4_wyw_ref < vtype > (this); - } - vtype4_wzx_ref < vtype > wzx() - { - return vtype4_wzx_ref < vtype > (this); - } - vtype4_wzy_ref < vtype > wzy() - { - return vtype4_wzy_ref < vtype > (this); - } - vtype4_wzz_ref < vtype > wzz() - { - return vtype4_wzz_ref < vtype > (this); - } - vtype4_wzw_ref < vtype > wzw() - { - return vtype4_wzw_ref < vtype > (this); - } - vtype4_wwx_ref < vtype > wwx() - { - return vtype4_wwx_ref < vtype > (this); - } - vtype4_wwy_ref < vtype > wwy() - { - return vtype4_wwy_ref < vtype > (this); - } - vtype4_wwz_ref < vtype > wwz() - { - return vtype4_wwz_ref < vtype > (this); - } - vtype4_www_ref < vtype > www() - { - return vtype4_www_ref < vtype > (this); - } - vtype4_xxxx_ref < vtype > xxxx() - { - return vtype4_xxxx_ref < vtype > (this); - } - vtype4_xxxy_ref < vtype > xxxy() - { - return vtype4_xxxy_ref < vtype > (this); - } - vtype4_xxxz_ref < vtype > xxxz() - { - return vtype4_xxxz_ref < vtype > (this); - } - vtype4_xxxw_ref < vtype > xxxw() - { - return vtype4_xxxw_ref < vtype > (this); - } - vtype4_xxyx_ref < vtype > xxyx() - { - return vtype4_xxyx_ref < vtype > (this); - } - vtype4_xxyy_ref < vtype > xxyy() - { - return vtype4_xxyy_ref < vtype > (this); - } - vtype4_xxyz_ref < vtype > xxyz() - { - return vtype4_xxyz_ref < vtype > (this); - } - vtype4_xxyw_ref < vtype > xxyw() - { - return vtype4_xxyw_ref < vtype > (this); - } - vtype4_xxzx_ref < vtype > xxzx() - { - return vtype4_xxzx_ref < vtype > (this); - } - vtype4_xxzy_ref < vtype > xxzy() - { - return vtype4_xxzy_ref < vtype > (this); - } - vtype4_xxzz_ref < vtype > xxzz() - { - return vtype4_xxzz_ref < vtype > (this); - } - vtype4_xxzw_ref < vtype > xxzw() - { - return vtype4_xxzw_ref < vtype > (this); - } - vtype4_xxwx_ref < vtype > xxwx() - { - return vtype4_xxwx_ref < vtype > (this); - } - vtype4_xxwy_ref < vtype > xxwy() - { - return vtype4_xxwy_ref < vtype > (this); - } - vtype4_xxwz_ref < vtype > xxwz() - { - return vtype4_xxwz_ref < vtype > (this); - } - vtype4_xxww_ref < vtype > xxww() - { - return vtype4_xxww_ref < vtype > (this); - } - vtype4_xyxx_ref < vtype > xyxx() - { - return vtype4_xyxx_ref < vtype > (this); - } - vtype4_xyxy_ref < vtype > xyxy() - { - return vtype4_xyxy_ref < vtype > (this); - } - vtype4_xyxz_ref < vtype > xyxz() - { - return vtype4_xyxz_ref < vtype > (this); - } - vtype4_xyxw_ref < vtype > xyxw() - { - return vtype4_xyxw_ref < vtype > (this); - } - vtype4_xyyx_ref < vtype > xyyx() - { - return vtype4_xyyx_ref < vtype > (this); - } - vtype4_xyyy_ref < vtype > xyyy() - { - return vtype4_xyyy_ref < vtype > (this); - } - vtype4_xyyz_ref < vtype > xyyz() - { - return vtype4_xyyz_ref < vtype > (this); - } - vtype4_xyyw_ref < vtype > xyyw() - { - return vtype4_xyyw_ref < vtype > (this); - } - vtype4_xyzx_ref < vtype > xyzx() - { - return vtype4_xyzx_ref < vtype > (this); - } - vtype4_xyzy_ref < vtype > xyzy() - { - return vtype4_xyzy_ref < vtype > (this); - } - vtype4_xyzz_ref < vtype > xyzz() - { - return vtype4_xyzz_ref < vtype > (this); - } - vtype4_xyzw_ref < vtype > xyzw() - { - return vtype4_xyzw_ref < vtype > (this); - } - vtype4_xywx_ref < vtype > xywx() - { - return vtype4_xywx_ref < vtype > (this); - } - vtype4_xywy_ref < vtype > xywy() - { - return vtype4_xywy_ref < vtype > (this); - } - vtype4_xywz_ref < vtype > xywz() - { - return vtype4_xywz_ref < vtype > (this); - } - vtype4_xyww_ref < vtype > xyww() - { - return vtype4_xyww_ref < vtype > (this); - } - vtype4_xzxx_ref < vtype > xzxx() - { - return vtype4_xzxx_ref < vtype > (this); - } - vtype4_xzxy_ref < vtype > xzxy() - { - return vtype4_xzxy_ref < vtype > (this); - } - vtype4_xzxz_ref < vtype > xzxz() - { - return vtype4_xzxz_ref < vtype > (this); - } - vtype4_xzxw_ref < vtype > xzxw() - { - return vtype4_xzxw_ref < vtype > (this); - } - vtype4_xzyx_ref < vtype > xzyx() - { - return vtype4_xzyx_ref < vtype > (this); - } - vtype4_xzyy_ref < vtype > xzyy() - { - return vtype4_xzyy_ref < vtype > (this); - } - vtype4_xzyz_ref < vtype > xzyz() - { - return vtype4_xzyz_ref < vtype > (this); - } - vtype4_xzyw_ref < vtype > xzyw() - { - return vtype4_xzyw_ref < vtype > (this); - } - vtype4_xzzx_ref < vtype > xzzx() - { - return vtype4_xzzx_ref < vtype > (this); - } - vtype4_xzzy_ref < vtype > xzzy() - { - return vtype4_xzzy_ref < vtype > (this); - } - vtype4_xzzz_ref < vtype > xzzz() - { - return vtype4_xzzz_ref < vtype > (this); - } - vtype4_xzzw_ref < vtype > xzzw() - { - return vtype4_xzzw_ref < vtype > (this); - } - vtype4_xzwx_ref < vtype > xzwx() - { - return vtype4_xzwx_ref < vtype > (this); - } - vtype4_xzwy_ref < vtype > xzwy() - { - return vtype4_xzwy_ref < vtype > (this); - } - vtype4_xzwz_ref < vtype > xzwz() - { - return vtype4_xzwz_ref < vtype > (this); - } - vtype4_xzww_ref < vtype > xzww() - { - return vtype4_xzww_ref < vtype > (this); - } - vtype4_xwxx_ref < vtype > xwxx() - { - return vtype4_xwxx_ref < vtype > (this); - } - vtype4_xwxy_ref < vtype > xwxy() - { - return vtype4_xwxy_ref < vtype > (this); - } - vtype4_xwxz_ref < vtype > xwxz() - { - return vtype4_xwxz_ref < vtype > (this); - } - vtype4_xwxw_ref < vtype > xwxw() - { - return vtype4_xwxw_ref < vtype > (this); - } - vtype4_xwyx_ref < vtype > xwyx() - { - return vtype4_xwyx_ref < vtype > (this); - } - vtype4_xwyy_ref < vtype > xwyy() - { - return vtype4_xwyy_ref < vtype > (this); - } - vtype4_xwyz_ref < vtype > xwyz() - { - return vtype4_xwyz_ref < vtype > (this); - } - vtype4_xwyw_ref < vtype > xwyw() - { - return vtype4_xwyw_ref < vtype > (this); - } - vtype4_xwzx_ref < vtype > xwzx() - { - return vtype4_xwzx_ref < vtype > (this); - } - vtype4_xwzy_ref < vtype > xwzy() - { - return vtype4_xwzy_ref < vtype > (this); - } - vtype4_xwzz_ref < vtype > xwzz() - { - return vtype4_xwzz_ref < vtype > (this); - } - vtype4_xwzw_ref < vtype > xwzw() - { - return vtype4_xwzw_ref < vtype > (this); - } - vtype4_xwwx_ref < vtype > xwwx() - { - return vtype4_xwwx_ref < vtype > (this); - } - vtype4_xwwy_ref < vtype > xwwy() - { - return vtype4_xwwy_ref < vtype > (this); - } - vtype4_xwwz_ref < vtype > xwwz() - { - return vtype4_xwwz_ref < vtype > (this); - } - vtype4_xwww_ref < vtype > xwww() - { - return vtype4_xwww_ref < vtype > (this); - } - vtype4_yxxx_ref < vtype > yxxx() - { - return vtype4_yxxx_ref < vtype > (this); - } - vtype4_yxxy_ref < vtype > yxxy() - { - return vtype4_yxxy_ref < vtype > (this); - } - vtype4_yxxz_ref < vtype > yxxz() - { - return vtype4_yxxz_ref < vtype > (this); - } - vtype4_yxxw_ref < vtype > yxxw() - { - return vtype4_yxxw_ref < vtype > (this); - } - vtype4_yxyx_ref < vtype > yxyx() - { - return vtype4_yxyx_ref < vtype > (this); - } - vtype4_yxyy_ref < vtype > yxyy() - { - return vtype4_yxyy_ref < vtype > (this); - } - vtype4_yxyz_ref < vtype > yxyz() - { - return vtype4_yxyz_ref < vtype > (this); - } - vtype4_yxyw_ref < vtype > yxyw() - { - return vtype4_yxyw_ref < vtype > (this); - } - vtype4_yxzx_ref < vtype > yxzx() - { - return vtype4_yxzx_ref < vtype > (this); - } - vtype4_yxzy_ref < vtype > yxzy() - { - return vtype4_yxzy_ref < vtype > (this); - } - vtype4_yxzz_ref < vtype > yxzz() - { - return vtype4_yxzz_ref < vtype > (this); - } - vtype4_yxzw_ref < vtype > yxzw() - { - return vtype4_yxzw_ref < vtype > (this); - } - vtype4_yxwx_ref < vtype > yxwx() - { - return vtype4_yxwx_ref < vtype > (this); - } - vtype4_yxwy_ref < vtype > yxwy() - { - return vtype4_yxwy_ref < vtype > (this); - } - vtype4_yxwz_ref < vtype > yxwz() - { - return vtype4_yxwz_ref < vtype > (this); - } - vtype4_yxww_ref < vtype > yxww() - { - return vtype4_yxww_ref < vtype > (this); - } - vtype4_yyxx_ref < vtype > yyxx() - { - return vtype4_yyxx_ref < vtype > (this); - } - vtype4_yyxy_ref < vtype > yyxy() - { - return vtype4_yyxy_ref < vtype > (this); - } - vtype4_yyxz_ref < vtype > yyxz() - { - return vtype4_yyxz_ref < vtype > (this); - } - vtype4_yyxw_ref < vtype > yyxw() - { - return vtype4_yyxw_ref < vtype > (this); - } - vtype4_yyyx_ref < vtype > yyyx() - { - return vtype4_yyyx_ref < vtype > (this); - } - vtype4_yyyy_ref < vtype > yyyy() - { - return vtype4_yyyy_ref < vtype > (this); - } - vtype4_yyyz_ref < vtype > yyyz() - { - return vtype4_yyyz_ref < vtype > (this); - } - vtype4_yyyw_ref < vtype > yyyw() - { - return vtype4_yyyw_ref < vtype > (this); - } - vtype4_yyzx_ref < vtype > yyzx() - { - return vtype4_yyzx_ref < vtype > (this); - } - vtype4_yyzy_ref < vtype > yyzy() - { - return vtype4_yyzy_ref < vtype > (this); - } - vtype4_yyzz_ref < vtype > yyzz() - { - return vtype4_yyzz_ref < vtype > (this); - } - vtype4_yyzw_ref < vtype > yyzw() - { - return vtype4_yyzw_ref < vtype > (this); - } - vtype4_yywx_ref < vtype > yywx() - { - return vtype4_yywx_ref < vtype > (this); - } - vtype4_yywy_ref < vtype > yywy() - { - return vtype4_yywy_ref < vtype > (this); - } - vtype4_yywz_ref < vtype > yywz() - { - return vtype4_yywz_ref < vtype > (this); - } - vtype4_yyww_ref < vtype > yyww() - { - return vtype4_yyww_ref < vtype > (this); - } - vtype4_yzxx_ref < vtype > yzxx() - { - return vtype4_yzxx_ref < vtype > (this); - } - vtype4_yzxy_ref < vtype > yzxy() - { - return vtype4_yzxy_ref < vtype > (this); - } - vtype4_yzxz_ref < vtype > yzxz() - { - return vtype4_yzxz_ref < vtype > (this); - } - vtype4_yzxw_ref < vtype > yzxw() - { - return vtype4_yzxw_ref < vtype > (this); - } - vtype4_yzyx_ref < vtype > yzyx() - { - return vtype4_yzyx_ref < vtype > (this); - } - vtype4_yzyy_ref < vtype > yzyy() - { - return vtype4_yzyy_ref < vtype > (this); - } - vtype4_yzyz_ref < vtype > yzyz() - { - return vtype4_yzyz_ref < vtype > (this); - } - vtype4_yzyw_ref < vtype > yzyw() - { - return vtype4_yzyw_ref < vtype > (this); - } - vtype4_yzzx_ref < vtype > yzzx() - { - return vtype4_yzzx_ref < vtype > (this); - } - vtype4_yzzy_ref < vtype > yzzy() - { - return vtype4_yzzy_ref < vtype > (this); - } - vtype4_yzzz_ref < vtype > yzzz() - { - return vtype4_yzzz_ref < vtype > (this); - } - vtype4_yzzw_ref < vtype > yzzw() - { - return vtype4_yzzw_ref < vtype > (this); - } - vtype4_yzwx_ref < vtype > yzwx() - { - return vtype4_yzwx_ref < vtype > (this); - } - vtype4_yzwy_ref < vtype > yzwy() - { - return vtype4_yzwy_ref < vtype > (this); - } - vtype4_yzwz_ref < vtype > yzwz() - { - return vtype4_yzwz_ref < vtype > (this); - } - vtype4_yzww_ref < vtype > yzww() - { - return vtype4_yzww_ref < vtype > (this); - } - vtype4_ywxx_ref < vtype > ywxx() - { - return vtype4_ywxx_ref < vtype > (this); - } - vtype4_ywxy_ref < vtype > ywxy() - { - return vtype4_ywxy_ref < vtype > (this); - } - vtype4_ywxz_ref < vtype > ywxz() - { - return vtype4_ywxz_ref < vtype > (this); - } - vtype4_ywxw_ref < vtype > ywxw() - { - return vtype4_ywxw_ref < vtype > (this); - } - vtype4_ywyx_ref < vtype > ywyx() - { - return vtype4_ywyx_ref < vtype > (this); - } - vtype4_ywyy_ref < vtype > ywyy() - { - return vtype4_ywyy_ref < vtype > (this); - } - vtype4_ywyz_ref < vtype > ywyz() - { - return vtype4_ywyz_ref < vtype > (this); - } - vtype4_ywyw_ref < vtype > ywyw() - { - return vtype4_ywyw_ref < vtype > (this); - } - vtype4_ywzx_ref < vtype > ywzx() - { - return vtype4_ywzx_ref < vtype > (this); - } - vtype4_ywzy_ref < vtype > ywzy() - { - return vtype4_ywzy_ref < vtype > (this); - } - vtype4_ywzz_ref < vtype > ywzz() - { - return vtype4_ywzz_ref < vtype > (this); - } - vtype4_ywzw_ref < vtype > ywzw() - { - return vtype4_ywzw_ref < vtype > (this); - } - vtype4_ywwx_ref < vtype > ywwx() - { - return vtype4_ywwx_ref < vtype > (this); - } - vtype4_ywwy_ref < vtype > ywwy() - { - return vtype4_ywwy_ref < vtype > (this); - } - vtype4_ywwz_ref < vtype > ywwz() - { - return vtype4_ywwz_ref < vtype > (this); - } - vtype4_ywww_ref < vtype > ywww() - { - return vtype4_ywww_ref < vtype > (this); - } - vtype4_zxxx_ref < vtype > zxxx() - { - return vtype4_zxxx_ref < vtype > (this); - } - vtype4_zxxy_ref < vtype > zxxy() - { - return vtype4_zxxy_ref < vtype > (this); - } - vtype4_zxxz_ref < vtype > zxxz() - { - return vtype4_zxxz_ref < vtype > (this); - } - vtype4_zxxw_ref < vtype > zxxw() - { - return vtype4_zxxw_ref < vtype > (this); - } - vtype4_zxyx_ref < vtype > zxyx() - { - return vtype4_zxyx_ref < vtype > (this); - } - vtype4_zxyy_ref < vtype > zxyy() - { - return vtype4_zxyy_ref < vtype > (this); - } - vtype4_zxyz_ref < vtype > zxyz() - { - return vtype4_zxyz_ref < vtype > (this); - } - vtype4_zxyw_ref < vtype > zxyw() - { - return vtype4_zxyw_ref < vtype > (this); - } - vtype4_zxzx_ref < vtype > zxzx() - { - return vtype4_zxzx_ref < vtype > (this); - } - vtype4_zxzy_ref < vtype > zxzy() - { - return vtype4_zxzy_ref < vtype > (this); - } - vtype4_zxzz_ref < vtype > zxzz() - { - return vtype4_zxzz_ref < vtype > (this); - } - vtype4_zxzw_ref < vtype > zxzw() - { - return vtype4_zxzw_ref < vtype > (this); - } - vtype4_zxwx_ref < vtype > zxwx() - { - return vtype4_zxwx_ref < vtype > (this); - } - vtype4_zxwy_ref < vtype > zxwy() - { - return vtype4_zxwy_ref < vtype > (this); - } - vtype4_zxwz_ref < vtype > zxwz() - { - return vtype4_zxwz_ref < vtype > (this); - } - vtype4_zxww_ref < vtype > zxww() - { - return vtype4_zxww_ref < vtype > (this); - } - vtype4_zyxx_ref < vtype > zyxx() - { - return vtype4_zyxx_ref < vtype > (this); - } - vtype4_zyxy_ref < vtype > zyxy() - { - return vtype4_zyxy_ref < vtype > (this); - } - vtype4_zyxz_ref < vtype > zyxz() - { - return vtype4_zyxz_ref < vtype > (this); - } - vtype4_zyxw_ref < vtype > zyxw() - { - return vtype4_zyxw_ref < vtype > (this); - } - vtype4_zyyx_ref < vtype > zyyx() - { - return vtype4_zyyx_ref < vtype > (this); - } - vtype4_zyyy_ref < vtype > zyyy() - { - return vtype4_zyyy_ref < vtype > (this); - } - vtype4_zyyz_ref < vtype > zyyz() - { - return vtype4_zyyz_ref < vtype > (this); - } - vtype4_zyyw_ref < vtype > zyyw() - { - return vtype4_zyyw_ref < vtype > (this); - } - vtype4_zyzx_ref < vtype > zyzx() - { - return vtype4_zyzx_ref < vtype > (this); - } - vtype4_zyzy_ref < vtype > zyzy() - { - return vtype4_zyzy_ref < vtype > (this); - } - vtype4_zyzz_ref < vtype > zyzz() - { - return vtype4_zyzz_ref < vtype > (this); - } - vtype4_zyzw_ref < vtype > zyzw() - { - return vtype4_zyzw_ref < vtype > (this); - } - vtype4_zywx_ref < vtype > zywx() - { - return vtype4_zywx_ref < vtype > (this); - } - vtype4_zywy_ref < vtype > zywy() - { - return vtype4_zywy_ref < vtype > (this); - } - vtype4_zywz_ref < vtype > zywz() - { - return vtype4_zywz_ref < vtype > (this); - } - vtype4_zyww_ref < vtype > zyww() - { - return vtype4_zyww_ref < vtype > (this); - } - vtype4_zzxx_ref < vtype > zzxx() - { - return vtype4_zzxx_ref < vtype > (this); - } - vtype4_zzxy_ref < vtype > zzxy() - { - return vtype4_zzxy_ref < vtype > (this); - } - vtype4_zzxz_ref < vtype > zzxz() - { - return vtype4_zzxz_ref < vtype > (this); - } - vtype4_zzxw_ref < vtype > zzxw() - { - return vtype4_zzxw_ref < vtype > (this); - } - vtype4_zzyx_ref < vtype > zzyx() - { - return vtype4_zzyx_ref < vtype > (this); - } - vtype4_zzyy_ref < vtype > zzyy() - { - return vtype4_zzyy_ref < vtype > (this); - } - vtype4_zzyz_ref < vtype > zzyz() - { - return vtype4_zzyz_ref < vtype > (this); - } - vtype4_zzyw_ref < vtype > zzyw() - { - return vtype4_zzyw_ref < vtype > (this); - } - vtype4_zzzx_ref < vtype > zzzx() - { - return vtype4_zzzx_ref < vtype > (this); - } - vtype4_zzzy_ref < vtype > zzzy() - { - return vtype4_zzzy_ref < vtype > (this); - } - vtype4_zzzz_ref < vtype > zzzz() - { - return vtype4_zzzz_ref < vtype > (this); - } - vtype4_zzzw_ref < vtype > zzzw() - { - return vtype4_zzzw_ref < vtype > (this); - } - vtype4_zzwx_ref < vtype > zzwx() - { - return vtype4_zzwx_ref < vtype > (this); - } - vtype4_zzwy_ref < vtype > zzwy() - { - return vtype4_zzwy_ref < vtype > (this); - } - vtype4_zzwz_ref < vtype > zzwz() - { - return vtype4_zzwz_ref < vtype > (this); - } - vtype4_zzww_ref < vtype > zzww() - { - return vtype4_zzww_ref < vtype > (this); - } - vtype4_zwxx_ref < vtype > zwxx() - { - return vtype4_zwxx_ref < vtype > (this); - } - vtype4_zwxy_ref < vtype > zwxy() - { - return vtype4_zwxy_ref < vtype > (this); - } - vtype4_zwxz_ref < vtype > zwxz() - { - return vtype4_zwxz_ref < vtype > (this); - } - vtype4_zwxw_ref < vtype > zwxw() - { - return vtype4_zwxw_ref < vtype > (this); - } - vtype4_zwyx_ref < vtype > zwyx() - { - return vtype4_zwyx_ref < vtype > (this); - } - vtype4_zwyy_ref < vtype > zwyy() - { - return vtype4_zwyy_ref < vtype > (this); - } - vtype4_zwyz_ref < vtype > zwyz() - { - return vtype4_zwyz_ref < vtype > (this); - } - vtype4_zwyw_ref < vtype > zwyw() - { - return vtype4_zwyw_ref < vtype > (this); - } - vtype4_zwzx_ref < vtype > zwzx() - { - return vtype4_zwzx_ref < vtype > (this); - } - vtype4_zwzy_ref < vtype > zwzy() - { - return vtype4_zwzy_ref < vtype > (this); - } - vtype4_zwzz_ref < vtype > zwzz() - { - return vtype4_zwzz_ref < vtype > (this); - } - vtype4_zwzw_ref < vtype > zwzw() - { - return vtype4_zwzw_ref < vtype > (this); - } - vtype4_zwwx_ref < vtype > zwwx() - { - return vtype4_zwwx_ref < vtype > (this); - } - vtype4_zwwy_ref < vtype > zwwy() - { - return vtype4_zwwy_ref < vtype > (this); - } - vtype4_zwwz_ref < vtype > zwwz() - { - return vtype4_zwwz_ref < vtype > (this); - } - vtype4_zwww_ref < vtype > zwww() - { - return vtype4_zwww_ref < vtype > (this); - } - vtype4_wxxx_ref < vtype > wxxx() - { - return vtype4_wxxx_ref < vtype > (this); - } - vtype4_wxxy_ref < vtype > wxxy() - { - return vtype4_wxxy_ref < vtype > (this); - } - vtype4_wxxz_ref < vtype > wxxz() - { - return vtype4_wxxz_ref < vtype > (this); - } - vtype4_wxxw_ref < vtype > wxxw() - { - return vtype4_wxxw_ref < vtype > (this); - } - vtype4_wxyx_ref < vtype > wxyx() - { - return vtype4_wxyx_ref < vtype > (this); - } - vtype4_wxyy_ref < vtype > wxyy() - { - return vtype4_wxyy_ref < vtype > (this); - } - vtype4_wxyz_ref < vtype > wxyz() - { - return vtype4_wxyz_ref < vtype > (this); - } - vtype4_wxyw_ref < vtype > wxyw() - { - return vtype4_wxyw_ref < vtype > (this); - } - vtype4_wxzx_ref < vtype > wxzx() - { - return vtype4_wxzx_ref < vtype > (this); - } - vtype4_wxzy_ref < vtype > wxzy() - { - return vtype4_wxzy_ref < vtype > (this); - } - vtype4_wxzz_ref < vtype > wxzz() - { - return vtype4_wxzz_ref < vtype > (this); - } - vtype4_wxzw_ref < vtype > wxzw() - { - return vtype4_wxzw_ref < vtype > (this); - } - vtype4_wxwx_ref < vtype > wxwx() - { - return vtype4_wxwx_ref < vtype > (this); - } - vtype4_wxwy_ref < vtype > wxwy() - { - return vtype4_wxwy_ref < vtype > (this); - } - vtype4_wxwz_ref < vtype > wxwz() - { - return vtype4_wxwz_ref < vtype > (this); - } - vtype4_wxww_ref < vtype > wxww() - { - return vtype4_wxww_ref < vtype > (this); - } - vtype4_wyxx_ref < vtype > wyxx() - { - return vtype4_wyxx_ref < vtype > (this); - } - vtype4_wyxy_ref < vtype > wyxy() - { - return vtype4_wyxy_ref < vtype > (this); - } - vtype4_wyxz_ref < vtype > wyxz() - { - return vtype4_wyxz_ref < vtype > (this); - } - vtype4_wyxw_ref < vtype > wyxw() - { - return vtype4_wyxw_ref < vtype > (this); - } - vtype4_wyyx_ref < vtype > wyyx() - { - return vtype4_wyyx_ref < vtype > (this); - } - vtype4_wyyy_ref < vtype > wyyy() - { - return vtype4_wyyy_ref < vtype > (this); - } - vtype4_wyyz_ref < vtype > wyyz() - { - return vtype4_wyyz_ref < vtype > (this); - } - vtype4_wyyw_ref < vtype > wyyw() - { - return vtype4_wyyw_ref < vtype > (this); - } - vtype4_wyzx_ref < vtype > wyzx() - { - return vtype4_wyzx_ref < vtype > (this); - } - vtype4_wyzy_ref < vtype > wyzy() - { - return vtype4_wyzy_ref < vtype > (this); - } - vtype4_wyzz_ref < vtype > wyzz() - { - return vtype4_wyzz_ref < vtype > (this); - } - vtype4_wyzw_ref < vtype > wyzw() - { - return vtype4_wyzw_ref < vtype > (this); - } - vtype4_wywx_ref < vtype > wywx() - { - return vtype4_wywx_ref < vtype > (this); - } - vtype4_wywy_ref < vtype > wywy() - { - return vtype4_wywy_ref < vtype > (this); - } - vtype4_wywz_ref < vtype > wywz() - { - return vtype4_wywz_ref < vtype > (this); - } - vtype4_wyww_ref < vtype > wyww() - { - return vtype4_wyww_ref < vtype > (this); - } - vtype4_wzxx_ref < vtype > wzxx() - { - return vtype4_wzxx_ref < vtype > (this); - } - vtype4_wzxy_ref < vtype > wzxy() - { - return vtype4_wzxy_ref < vtype > (this); - } - vtype4_wzxz_ref < vtype > wzxz() - { - return vtype4_wzxz_ref < vtype > (this); - } - vtype4_wzxw_ref < vtype > wzxw() - { - return vtype4_wzxw_ref < vtype > (this); - } - vtype4_wzyx_ref < vtype > wzyx() - { - return vtype4_wzyx_ref < vtype > (this); - } - vtype4_wzyy_ref < vtype > wzyy() - { - return vtype4_wzyy_ref < vtype > (this); - } - vtype4_wzyz_ref < vtype > wzyz() - { - return vtype4_wzyz_ref < vtype > (this); - } - vtype4_wzyw_ref < vtype > wzyw() - { - return vtype4_wzyw_ref < vtype > (this); - } - vtype4_wzzx_ref < vtype > wzzx() - { - return vtype4_wzzx_ref < vtype > (this); - } - vtype4_wzzy_ref < vtype > wzzy() - { - return vtype4_wzzy_ref < vtype > (this); - } - vtype4_wzzz_ref < vtype > wzzz() - { - return vtype4_wzzz_ref < vtype > (this); - } - vtype4_wzzw_ref < vtype > wzzw() - { - return vtype4_wzzw_ref < vtype > (this); - } - vtype4_wzwx_ref < vtype > wzwx() - { - return vtype4_wzwx_ref < vtype > (this); - } - vtype4_wzwy_ref < vtype > wzwy() - { - return vtype4_wzwy_ref < vtype > (this); - } - vtype4_wzwz_ref < vtype > wzwz() - { - return vtype4_wzwz_ref < vtype > (this); - } - vtype4_wzww_ref < vtype > wzww() - { - return vtype4_wzww_ref < vtype > (this); - } - vtype4_wwxx_ref < vtype > wwxx() - { - return vtype4_wwxx_ref < vtype > (this); - } - vtype4_wwxy_ref < vtype > wwxy() - { - return vtype4_wwxy_ref < vtype > (this); - } - vtype4_wwxz_ref < vtype > wwxz() - { - return vtype4_wwxz_ref < vtype > (this); - } - vtype4_wwxw_ref < vtype > wwxw() - { - return vtype4_wwxw_ref < vtype > (this); - } - vtype4_wwyx_ref < vtype > wwyx() - { - return vtype4_wwyx_ref < vtype > (this); - } - vtype4_wwyy_ref < vtype > wwyy() - { - return vtype4_wwyy_ref < vtype > (this); - } - vtype4_wwyz_ref < vtype > wwyz() - { - return vtype4_wwyz_ref < vtype > (this); - } - vtype4_wwyw_ref < vtype > wwyw() - { - return vtype4_wwyw_ref < vtype > (this); - } - vtype4_wwzx_ref < vtype > wwzx() - { - return vtype4_wwzx_ref < vtype > (this); - } - vtype4_wwzy_ref < vtype > wwzy() - { - return vtype4_wwzy_ref < vtype > (this); - } - vtype4_wwzz_ref < vtype > wwzz() - { - return vtype4_wwzz_ref < vtype > (this); - } - vtype4_wwzw_ref < vtype > wwzw() - { - return vtype4_wwzw_ref < vtype > (this); - } - vtype4_wwwx_ref < vtype > wwwx() - { - return vtype4_wwwx_ref < vtype > (this); - } - vtype4_wwwy_ref < vtype > wwwy() - { - return vtype4_wwwy_ref < vtype > (this); - } - vtype4_wwwz_ref < vtype > wwwz() - { - return vtype4_wwwz_ref < vtype > (this); - } - vtype4_wwww_ref < vtype > wwww() - { - return vtype4_wwww_ref < vtype > (this); - } -}; - -template < typename vtype > vtype2_xy_ref < vtype > &vtype2_xy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype2_yx_ref < vtype > &vtype2_yx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype3_xy_ref < vtype > &vtype3_xy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype3_xz_ref < vtype > &vtype3_xz_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - return *this; -} - -template < typename vtype > vtype3_yx_ref < vtype > &vtype3_yx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype3_yz_ref < vtype > &vtype3_yz_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - return *this; -} - -template < typename vtype > vtype3_zx_ref < vtype > &vtype3_zx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype3_zy_ref < vtype > &vtype3_zy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype3_xyz_ref < vtype > &vtype3_xyz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype3_xzy_ref < vtype > &vtype3_xzy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype3_yxz_ref < vtype > &vtype3_yxz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype3_yzx_ref < vtype > &vtype3_yzx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype3_zxy_ref < vtype > &vtype3_zxy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype3_zyx_ref < vtype > &vtype3_zyx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_xy_ref < vtype > &vtype4_xy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype4_xz_ref < vtype > &vtype4_xz_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - return *this; -} - -template < typename vtype > vtype4_xw_ref < vtype > &vtype4_xw_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->x = other.x; - v->w = other.y; - return *this; -} - -template < typename vtype > vtype4_yx_ref < vtype > &vtype4_yx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype4_yz_ref < vtype > &vtype4_yz_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - return *this; -} - -template < typename vtype > vtype4_yw_ref < vtype > &vtype4_yw_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->y = other.x; - v->w = other.y; - return *this; -} - -template < typename vtype > vtype4_zx_ref < vtype > &vtype4_zx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype4_zy_ref < vtype > &vtype4_zy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype4_zw_ref < vtype > &vtype4_zw_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->z = other.x; - v->w = other.y; - return *this; -} - -template < typename vtype > vtype4_wx_ref < vtype > &vtype4_wx_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->w = other.x; - v->x = other.y; - return *this; -} - -template < typename vtype > vtype4_wy_ref < vtype > &vtype4_wy_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->w = other.x; - v->y = other.y; - return *this; -} - -template < typename vtype > vtype4_wz_ref < vtype > &vtype4_wz_ref < vtype >::operator=(const vtype2 < vtype > &other) -{ - v->w = other.x; - v->z = other.y; - return *this; -} - -template < typename vtype > vtype4_xyz_ref < vtype > &vtype4_xyz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_xyw_ref < vtype > &vtype4_xyw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_xzy_ref < vtype > &vtype4_xzy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_xzw_ref < vtype > &vtype4_xzw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_xwy_ref < vtype > &vtype4_xwy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->w = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_xwz_ref < vtype > &vtype4_xwz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->x = other.x; - v->w = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_yxz_ref < vtype > &vtype4_yxz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_yxw_ref < vtype > &vtype4_yxw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_yzx_ref < vtype > &vtype4_yzx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_yzw_ref < vtype > &vtype4_yzw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_ywx_ref < vtype > &vtype4_ywx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->w = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_ywz_ref < vtype > &vtype4_ywz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->y = other.x; - v->w = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_zxy_ref < vtype > &vtype4_zxy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_zxw_ref < vtype > &vtype4_zxw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_zyx_ref < vtype > &vtype4_zyx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_zyw_ref < vtype > &vtype4_zyw_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - v->w = other.z; - return *this; -} - -template < typename vtype > vtype4_zwx_ref < vtype > &vtype4_zwx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->w = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_zwy_ref < vtype > &vtype4_zwy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->z = other.x; - v->w = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_wxy_ref < vtype > &vtype4_wxy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->x = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_wxz_ref < vtype > &vtype4_wxz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->x = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_wyx_ref < vtype > &vtype4_wyx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->y = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_wyz_ref < vtype > &vtype4_wyz_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->y = other.y; - v->z = other.z; - return *this; -} - -template < typename vtype > vtype4_wzx_ref < vtype > &vtype4_wzx_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->z = other.y; - v->x = other.z; - return *this; -} - -template < typename vtype > vtype4_wzy_ref < vtype > &vtype4_wzy_ref < vtype >::operator=(const vtype3 < vtype > &other) -{ - v->w = other.x; - v->z = other.y; - v->y = other.z; - return *this; -} - -template < typename vtype > vtype4_xyzw_ref < vtype > &vtype4_xyzw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - v->z = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_xywz_ref < vtype > &vtype4_xywz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->y = other.y; - v->w = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_xzyw_ref < vtype > &vtype4_xzyw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - v->y = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_xzwy_ref < vtype > &vtype4_xzwy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->z = other.y; - v->w = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_xwyz_ref < vtype > &vtype4_xwyz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->w = other.y; - v->y = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_xwzy_ref < vtype > &vtype4_xwzy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->x = other.x; - v->w = other.y; - v->z = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_yxzw_ref < vtype > &vtype4_yxzw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - v->z = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_yxwz_ref < vtype > &vtype4_yxwz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->x = other.y; - v->w = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_yzxw_ref < vtype > &vtype4_yzxw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - v->x = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_yzwx_ref < vtype > &vtype4_yzwx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->z = other.y; - v->w = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype4_ywxz_ref < vtype > &vtype4_ywxz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->w = other.y; - v->x = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_ywzx_ref < vtype > &vtype4_ywzx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->y = other.x; - v->w = other.y; - v->z = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype4_zxyw_ref < vtype > &vtype4_zxyw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - v->y = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_zxwy_ref < vtype > &vtype4_zxwy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->x = other.y; - v->w = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_zyxw_ref < vtype > &vtype4_zyxw_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - v->x = other.z; - v->w = other.w; - return *this; -} - -template < typename vtype > vtype4_zywx_ref < vtype > &vtype4_zywx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->y = other.y; - v->w = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype4_zwxy_ref < vtype > &vtype4_zwxy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->w = other.y; - v->x = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_zwyx_ref < vtype > &vtype4_zwyx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->z = other.x; - v->w = other.y; - v->y = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype4_wxyz_ref < vtype > &vtype4_wxyz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->x = other.y; - v->y = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_wxzy_ref < vtype > &vtype4_wxzy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->x = other.y; - v->z = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_wyxz_ref < vtype > &vtype4_wyxz_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->y = other.y; - v->x = other.z; - v->z = other.w; - return *this; -} - -template < typename vtype > vtype4_wyzx_ref < vtype > &vtype4_wyzx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->y = other.y; - v->z = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype4_wzxy_ref < vtype > &vtype4_wzxy_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->z = other.y; - v->x = other.z; - v->y = other.w; - return *this; -} - -template < typename vtype > vtype4_wzyx_ref < vtype > &vtype4_wzyx_ref < vtype >::operator=(const vtype4 < vtype > &other) -{ - v->w = other.x; - v->z = other.y; - v->y = other.z; - v->x = other.w; - return *this; -} - -template < typename vtype > vtype2 < vtype >::vtype2(const vtype2_xx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype2_xy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype2_yx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype2_yy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_xx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_xy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_xz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_yx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_yy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_yz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_zx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_zy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype3_zz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_xx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_xy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_xz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_xw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_yx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_yy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_yz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_yw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_zx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_zy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_zz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_zw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_wx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_wy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_wz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; -} -template < typename vtype > vtype2 < vtype >::vtype2(const vtype4_ww_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_xxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_xxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_xyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_xyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_yxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_yxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_yyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype2_yyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_xzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_yzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype3_zzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xxw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xyw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xzw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xwx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xwy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xwz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_xww_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yxw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yyw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yzw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_ywx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_ywy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_ywz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_yww_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zxw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zyw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zzw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zwx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zwy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zwz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_zww_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wxx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wxy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wxz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wxw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wyx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wyy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wyz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wyw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wzx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wzy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wzz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wzw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->w; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wwx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->x; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wwy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->y; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_wwz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->z; -} -template < typename vtype > vtype3 < vtype >::vtype3(const vtype4_www_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xxxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xxxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xxyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xxyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xyxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xyxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xyyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_xyyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yxxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yxxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yxyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yxyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yyxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yyxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yyyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype2_yyyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xxzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xyzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_xzzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yxzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yyzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_yzzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zxzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zyzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype3_zzzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxxw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxyw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxzw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxwx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxwy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxwz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xxww_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->x; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyxw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyyw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyzw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xywx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xywy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xywz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xyww_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->y; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzxw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzyw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzzw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzwx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzwy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzwz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xzww_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->z; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwxx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwxy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwxz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwxw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwyx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwyy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwyz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwyw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwzx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwzy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwzz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwzw_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwwx_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwwy_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwwz_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_xwww_ref < vtype > &v) -{ - x = v.v->x; - y = v.v->w; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxxw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxyw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxzw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxwx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxwy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxwz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yxww_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->x; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyxw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyyw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyzw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yywx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yywy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yywz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yyww_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->y; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzxw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzyw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzzw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzwx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzwy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzwz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_yzww_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->z; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywxx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywxy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywxz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywxw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywyx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywyy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywyz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywyw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywzx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywzy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywzz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywzw_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywwx_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywwy_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywwz_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_ywww_ref < vtype > &v) -{ - x = v.v->y; - y = v.v->w; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxxw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxyw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxzw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxwx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxwy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxwz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zxww_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->x; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyxw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyyw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyzw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zywx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zywy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zywz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zyww_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->y; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzxw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzyw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzzw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzwx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzwy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzwz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zzww_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->z; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwxx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwxy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwxz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwxw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwyx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwyy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwyz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwyw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwzx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwzy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwzz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwzw_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwwx_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwwy_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwwz_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_zwww_ref < vtype > &v) -{ - x = v.v->z; - y = v.v->w; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxxx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxxy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxxz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxxw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxyx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxyy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxyz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxyw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxzx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxzy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxzz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxzw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxwx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxwy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxwz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wxww_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->x; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyxx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyxy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyxz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyxw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyyx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyyy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyyz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyyw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyzx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyzy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyzz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyzw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wywx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wywy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wywz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wyww_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->y; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzxx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzxy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzxz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzxw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzyx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzyy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzyz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzyw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzzx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzzy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzzz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzzw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzwx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzwy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzwz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wzww_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->z; - z = v.v->w; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwxx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->x; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwxy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->x; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwxz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->x; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwxw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->x; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwyx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->y; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwyy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->y; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwyz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->y; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwyw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->y; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwzx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->z; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwzy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->z; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwzz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->z; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwzw_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->z; - w = v.v->w; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwwx_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->w; - w = v.v->x; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwwy_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->w; - w = v.v->y; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwwz_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->w; - w = v.v->z; -} -template < typename vtype > vtype4 < vtype >::vtype4(const vtype4_wwww_ref < vtype > &v) -{ - x = v.v->w; - y = v.v->w; - z = v.v->w; - w = v.v->w; -} -typedef vtype2 < float >float2; -typedef vtype3 < float >float3; -typedef vtype4 < float >float4; -typedef vtype2 < double >double2; -typedef vtype3 < double >double3; -typedef vtype4 < double >double4; -typedef vtype2 < int >int2; -typedef vtype3 < int >int3; -typedef vtype4 < int >int4; -typedef vtype2 < uint > uint2; -typedef vtype3 < uint > uint3; -typedef vtype4 < uint > uint4; -typedef vtype2 < short >short2; -typedef vtype3 < short >short3; -typedef vtype4 < short >short4; -typedef vtype2 < ushort > ushort2; -typedef vtype3 < ushort > ushort3; -typedef vtype4 < ushort > ushort4; -typedef vtype2 < long >long2; -typedef vtype3 < long >long3; -typedef vtype4 < long >long4; -typedef vtype2 < ulong > ulong2; -typedef vtype3 < ulong > ulong3; -typedef vtype4 < ulong > ulong4; -static inline float2 operator+(float2 p, float2 q) -{ - return float2(p.x + q.x, p.y + q.y); -} -static inline float3 operator+(float3 p, float3 q) -{ - return float3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline float4 operator+(float4 p, float4 q) -{ - return float4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline float2 operator+(float2 p, float q) -{ - return float2(p.x + q, p.y + q); -} -static inline float3 operator+(float3 p, float q) -{ - return float3(p.x + q, p.y + q, p.z + q); -} -static inline float4 operator+(float4 p, float q) -{ - return float4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline float2 operator+(float p, float2 q) -{ - return float2(p + q.x, p + q.y); -} -static inline float3 operator+(float p, float3 q) -{ - return float3(p + q.x, p + q.y, p + q.z); -} -static inline float4 operator+(float p, float4 q) -{ - return float4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline double2 operator+(double2 p, double2 q) -{ - return double2(p.x + q.x, p.y + q.y); -} -static inline double3 operator+(double3 p, double3 q) -{ - return double3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline double4 operator+(double4 p, double4 q) -{ - return double4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline double2 operator+(double2 p, double q) -{ - return double2(p.x + q, p.y + q); -} -static inline double3 operator+(double3 p, double q) -{ - return double3(p.x + q, p.y + q, p.z + q); -} -static inline double4 operator+(double4 p, double q) -{ - return double4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline double2 operator+(double p, double2 q) -{ - return double2(p + q.x, p + q.y); -} -static inline double3 operator+(double p, double3 q) -{ - return double3(p + q.x, p + q.y, p + q.z); -} -static inline double4 operator+(double p, double4 q) -{ - return double4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline int2 operator+(int2 p, int2 q) -{ - return int2(p.x + q.x, p.y + q.y); -} -static inline int3 operator+(int3 p, int3 q) -{ - return int3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline int4 operator+(int4 p, int4 q) -{ - return int4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline int2 operator+(int2 p, int q) -{ - return int2(p.x + q, p.y + q); -} -static inline int3 operator+(int3 p, int q) -{ - return int3(p.x + q, p.y + q, p.z + q); -} -static inline int4 operator+(int4 p, int q) -{ - return int4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline int2 operator+(int p, int2 q) -{ - return int2(p + q.x, p + q.y); -} -static inline int3 operator+(int p, int3 q) -{ - return int3(p + q.x, p + q.y, p + q.z); -} -static inline int4 operator+(int p, int4 q) -{ - return int4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline uint2 operator+(uint2 p, uint2 q) -{ - return uint2(p.x + q.x, p.y + q.y); -} -static inline uint3 operator+(uint3 p, uint3 q) -{ - return uint3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline uint4 operator+(uint4 p, uint4 q) -{ - return uint4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline uint2 operator+(uint2 p, uint q) -{ - return uint2(p.x + q, p.y + q); -} -static inline uint3 operator+(uint3 p, uint q) -{ - return uint3(p.x + q, p.y + q, p.z + q); -} -static inline uint4 operator+(uint4 p, uint q) -{ - return uint4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline uint2 operator+(uint p, uint2 q) -{ - return uint2(p + q.x, p + q.y); -} -static inline uint3 operator+(uint p, uint3 q) -{ - return uint3(p + q.x, p + q.y, p + q.z); -} -static inline uint4 operator+(uint p, uint4 q) -{ - return uint4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline short2 operator+(short2 p, short2 q) -{ - return short2(p.x + q.x, p.y + q.y); -} -static inline short3 operator+(short3 p, short3 q) -{ - return short3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline short4 operator+(short4 p, short4 q) -{ - return short4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline short2 operator+(short2 p, short q) -{ - return short2(p.x + q, p.y + q); -} -static inline short3 operator+(short3 p, short q) -{ - return short3(p.x + q, p.y + q, p.z + q); -} -static inline short4 operator+(short4 p, short q) -{ - return short4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline short2 operator+(short p, short2 q) -{ - return short2(p + q.x, p + q.y); -} -static inline short3 operator+(short p, short3 q) -{ - return short3(p + q.x, p + q.y, p + q.z); -} -static inline short4 operator+(short p, short4 q) -{ - return short4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline ushort2 operator+(ushort2 p, ushort2 q) -{ - return ushort2(p.x + q.x, p.y + q.y); -} -static inline ushort3 operator+(ushort3 p, ushort3 q) -{ - return ushort3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline ushort4 operator+(ushort4 p, ushort4 q) -{ - return ushort4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline ushort2 operator+(ushort2 p, ushort q) -{ - return ushort2(p.x + q, p.y + q); -} -static inline ushort3 operator+(ushort3 p, ushort q) -{ - return ushort3(p.x + q, p.y + q, p.z + q); -} -static inline ushort4 operator+(ushort4 p, ushort q) -{ - return ushort4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline ushort2 operator+(ushort p, ushort2 q) -{ - return ushort2(p + q.x, p + q.y); -} -static inline ushort3 operator+(ushort p, ushort3 q) -{ - return ushort3(p + q.x, p + q.y, p + q.z); -} -static inline ushort4 operator+(ushort p, ushort4 q) -{ - return ushort4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline long2 operator+(long2 p, long2 q) -{ - return long2(p.x + q.x, p.y + q.y); -} -static inline long3 operator+(long3 p, long3 q) -{ - return long3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline long4 operator+(long4 p, long4 q) -{ - return long4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline long2 operator+(long2 p, long q) -{ - return long2(p.x + q, p.y + q); -} -static inline long3 operator+(long3 p, long q) -{ - return long3(p.x + q, p.y + q, p.z + q); -} -static inline long4 operator+(long4 p, long q) -{ - return long4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline long2 operator+(long p, long2 q) -{ - return long2(p + q.x, p + q.y); -} -static inline long3 operator+(long p, long3 q) -{ - return long3(p + q.x, p + q.y, p + q.z); -} -static inline long4 operator+(long p, long4 q) -{ - return long4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline ulong2 operator+(ulong2 p, ulong2 q) -{ - return ulong2(p.x + q.x, p.y + q.y); -} -static inline ulong3 operator+(ulong3 p, ulong3 q) -{ - return ulong3(p.x + q.x, p.y + q.y, p.z + q.z); -} -static inline ulong4 operator+(ulong4 p, ulong4 q) -{ - return ulong4(p.x + q.x, p.y + q.y, p.z + q.z, p.w + q.w); -} -static inline ulong2 operator+(ulong2 p, ulong q) -{ - return ulong2(p.x + q, p.y + q); -} -static inline ulong3 operator+(ulong3 p, ulong q) -{ - return ulong3(p.x + q, p.y + q, p.z + q); -} -static inline ulong4 operator+(ulong4 p, ulong q) -{ - return ulong4(p.x + q, p.y + q, p.z + q, p.w + q); -} -static inline ulong2 operator+(ulong p, ulong2 q) -{ - return ulong2(p + q.x, p + q.y); -} -static inline ulong3 operator+(ulong p, ulong3 q) -{ - return ulong3(p + q.x, p + q.y, p + q.z); -} -static inline ulong4 operator+(ulong p, ulong4 q) -{ - return ulong4(p + q.x, p + q.y, p + q.z, p + q.w); -} -static inline float2 operator-(float2 p, float2 q) -{ - return float2(p.x - q.x, p.y - q.y); -} -static inline float3 operator-(float3 p, float3 q) -{ - return float3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline float4 operator-(float4 p, float4 q) -{ - return float4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline float2 operator-(float2 p, float q) -{ - return float2(p.x - q, p.y - q); -} -static inline float3 operator-(float3 p, float q) -{ - return float3(p.x - q, p.y - q, p.z - q); -} -static inline float4 operator-(float4 p, float q) -{ - return float4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline float2 operator-(float p, float2 q) -{ - return float2(p - q.x, p - q.y); -} -static inline float3 operator-(float p, float3 q) -{ - return float3(p - q.x, p - q.y, p - q.z); -} -static inline float4 operator-(float p, float4 q) -{ - return float4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline double2 operator-(double2 p, double2 q) -{ - return double2(p.x - q.x, p.y - q.y); -} -static inline double3 operator-(double3 p, double3 q) -{ - return double3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline double4 operator-(double4 p, double4 q) -{ - return double4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline double2 operator-(double2 p, double q) -{ - return double2(p.x - q, p.y - q); -} -static inline double3 operator-(double3 p, double q) -{ - return double3(p.x - q, p.y - q, p.z - q); -} -static inline double4 operator-(double4 p, double q) -{ - return double4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline double2 operator-(double p, double2 q) -{ - return double2(p - q.x, p - q.y); -} -static inline double3 operator-(double p, double3 q) -{ - return double3(p - q.x, p - q.y, p - q.z); -} -static inline double4 operator-(double p, double4 q) -{ - return double4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline int2 operator-(int2 p, int2 q) -{ - return int2(p.x - q.x, p.y - q.y); -} -static inline int3 operator-(int3 p, int3 q) -{ - return int3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline int4 operator-(int4 p, int4 q) -{ - return int4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline int2 operator-(int2 p, int q) -{ - return int2(p.x - q, p.y - q); -} -static inline int3 operator-(int3 p, int q) -{ - return int3(p.x - q, p.y - q, p.z - q); -} -static inline int4 operator-(int4 p, int q) -{ - return int4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline int2 operator-(int p, int2 q) -{ - return int2(p - q.x, p - q.y); -} -static inline int3 operator-(int p, int3 q) -{ - return int3(p - q.x, p - q.y, p - q.z); -} -static inline int4 operator-(int p, int4 q) -{ - return int4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline uint2 operator-(uint2 p, uint2 q) -{ - return uint2(p.x - q.x, p.y - q.y); -} -static inline uint3 operator-(uint3 p, uint3 q) -{ - return uint3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline uint4 operator-(uint4 p, uint4 q) -{ - return uint4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline uint2 operator-(uint2 p, uint q) -{ - return uint2(p.x - q, p.y - q); -} -static inline uint3 operator-(uint3 p, uint q) -{ - return uint3(p.x - q, p.y - q, p.z - q); -} -static inline uint4 operator-(uint4 p, uint q) -{ - return uint4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline uint2 operator-(uint p, uint2 q) -{ - return uint2(p - q.x, p - q.y); -} -static inline uint3 operator-(uint p, uint3 q) -{ - return uint3(p - q.x, p - q.y, p - q.z); -} -static inline uint4 operator-(uint p, uint4 q) -{ - return uint4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline short2 operator-(short2 p, short2 q) -{ - return short2(p.x - q.x, p.y - q.y); -} -static inline short3 operator-(short3 p, short3 q) -{ - return short3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline short4 operator-(short4 p, short4 q) -{ - return short4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline short2 operator-(short2 p, short q) -{ - return short2(p.x - q, p.y - q); -} -static inline short3 operator-(short3 p, short q) -{ - return short3(p.x - q, p.y - q, p.z - q); -} -static inline short4 operator-(short4 p, short q) -{ - return short4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline short2 operator-(short p, short2 q) -{ - return short2(p - q.x, p - q.y); -} -static inline short3 operator-(short p, short3 q) -{ - return short3(p - q.x, p - q.y, p - q.z); -} -static inline short4 operator-(short p, short4 q) -{ - return short4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline ushort2 operator-(ushort2 p, ushort2 q) -{ - return ushort2(p.x - q.x, p.y - q.y); -} -static inline ushort3 operator-(ushort3 p, ushort3 q) -{ - return ushort3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline ushort4 operator-(ushort4 p, ushort4 q) -{ - return ushort4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline ushort2 operator-(ushort2 p, ushort q) -{ - return ushort2(p.x - q, p.y - q); -} -static inline ushort3 operator-(ushort3 p, ushort q) -{ - return ushort3(p.x - q, p.y - q, p.z - q); -} -static inline ushort4 operator-(ushort4 p, ushort q) -{ - return ushort4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline ushort2 operator-(ushort p, ushort2 q) -{ - return ushort2(p - q.x, p - q.y); -} -static inline ushort3 operator-(ushort p, ushort3 q) -{ - return ushort3(p - q.x, p - q.y, p - q.z); -} -static inline ushort4 operator-(ushort p, ushort4 q) -{ - return ushort4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline long2 operator-(long2 p, long2 q) -{ - return long2(p.x - q.x, p.y - q.y); -} -static inline long3 operator-(long3 p, long3 q) -{ - return long3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline long4 operator-(long4 p, long4 q) -{ - return long4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline long2 operator-(long2 p, long q) -{ - return long2(p.x - q, p.y - q); -} -static inline long3 operator-(long3 p, long q) -{ - return long3(p.x - q, p.y - q, p.z - q); -} -static inline long4 operator-(long4 p, long q) -{ - return long4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline long2 operator-(long p, long2 q) -{ - return long2(p - q.x, p - q.y); -} -static inline long3 operator-(long p, long3 q) -{ - return long3(p - q.x, p - q.y, p - q.z); -} -static inline long4 operator-(long p, long4 q) -{ - return long4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline ulong2 operator-(ulong2 p, ulong2 q) -{ - return ulong2(p.x - q.x, p.y - q.y); -} -static inline ulong3 operator-(ulong3 p, ulong3 q) -{ - return ulong3(p.x - q.x, p.y - q.y, p.z - q.z); -} -static inline ulong4 operator-(ulong4 p, ulong4 q) -{ - return ulong4(p.x - q.x, p.y - q.y, p.z - q.z, p.w - q.w); -} -static inline ulong2 operator-(ulong2 p, ulong q) -{ - return ulong2(p.x - q, p.y - q); -} -static inline ulong3 operator-(ulong3 p, ulong q) -{ - return ulong3(p.x - q, p.y - q, p.z - q); -} -static inline ulong4 operator-(ulong4 p, ulong q) -{ - return ulong4(p.x - q, p.y - q, p.z - q, p.w - q); -} -static inline ulong2 operator-(ulong p, ulong2 q) -{ - return ulong2(p - q.x, p - q.y); -} -static inline ulong3 operator-(ulong p, ulong3 q) -{ - return ulong3(p - q.x, p - q.y, p - q.z); -} -static inline ulong4 operator-(ulong p, ulong4 q) -{ - return ulong4(p - q.x, p - q.y, p - q.z, p - q.w); -} -static inline float2 operator*(float2 p, float2 q) -{ - return float2(p.x * q.x, p.y * q.y); -} -static inline float3 operator*(float3 p, float3 q) -{ - return float3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline float4 operator*(float4 p, float4 q) -{ - return float4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline float2 operator*(float2 p, float q) -{ - return float2(p.x * q, p.y * q); -} -static inline float3 operator*(float3 p, float q) -{ - return float3(p.x * q, p.y * q, p.z * q); -} -static inline float4 operator*(float4 p, float q) -{ - return float4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline float2 operator*(float p, float2 q) -{ - return float2(p * q.x, p * q.y); -} -static inline float3 operator*(float p, float3 q) -{ - return float3(p * q.x, p * q.y, p * q.z); -} -static inline float4 operator*(float p, float4 q) -{ - return float4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline double2 operator*(double2 p, double2 q) -{ - return double2(p.x * q.x, p.y * q.y); -} -static inline double3 operator*(double3 p, double3 q) -{ - return double3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline double4 operator*(double4 p, double4 q) -{ - return double4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline double2 operator*(double2 p, double q) -{ - return double2(p.x * q, p.y * q); -} -static inline double3 operator*(double3 p, double q) -{ - return double3(p.x * q, p.y * q, p.z * q); -} -static inline double4 operator*(double4 p, double q) -{ - return double4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline double2 operator*(double p, double2 q) -{ - return double2(p * q.x, p * q.y); -} -static inline double3 operator*(double p, double3 q) -{ - return double3(p * q.x, p * q.y, p * q.z); -} -static inline double4 operator*(double p, double4 q) -{ - return double4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline int2 operator*(int2 p, int2 q) -{ - return int2(p.x * q.x, p.y * q.y); -} -static inline int3 operator*(int3 p, int3 q) -{ - return int3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline int4 operator*(int4 p, int4 q) -{ - return int4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline int2 operator*(int2 p, int q) -{ - return int2(p.x * q, p.y * q); -} -static inline int3 operator*(int3 p, int q) -{ - return int3(p.x * q, p.y * q, p.z * q); -} -static inline int4 operator*(int4 p, int q) -{ - return int4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline int2 operator*(int p, int2 q) -{ - return int2(p * q.x, p * q.y); -} -static inline int3 operator*(int p, int3 q) -{ - return int3(p * q.x, p * q.y, p * q.z); -} -static inline int4 operator*(int p, int4 q) -{ - return int4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline uint2 operator*(uint2 p, uint2 q) -{ - return uint2(p.x * q.x, p.y * q.y); -} -static inline uint3 operator*(uint3 p, uint3 q) -{ - return uint3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline uint4 operator*(uint4 p, uint4 q) -{ - return uint4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline uint2 operator*(uint2 p, uint q) -{ - return uint2(p.x * q, p.y * q); -} -static inline uint3 operator*(uint3 p, uint q) -{ - return uint3(p.x * q, p.y * q, p.z * q); -} -static inline uint4 operator*(uint4 p, uint q) -{ - return uint4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline uint2 operator*(uint p, uint2 q) -{ - return uint2(p * q.x, p * q.y); -} -static inline uint3 operator*(uint p, uint3 q) -{ - return uint3(p * q.x, p * q.y, p * q.z); -} -static inline uint4 operator*(uint p, uint4 q) -{ - return uint4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline short2 operator*(short2 p, short2 q) -{ - return short2(p.x * q.x, p.y * q.y); -} -static inline short3 operator*(short3 p, short3 q) -{ - return short3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline short4 operator*(short4 p, short4 q) -{ - return short4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline short2 operator*(short2 p, short q) -{ - return short2(p.x * q, p.y * q); -} -static inline short3 operator*(short3 p, short q) -{ - return short3(p.x * q, p.y * q, p.z * q); -} -static inline short4 operator*(short4 p, short q) -{ - return short4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline short2 operator*(short p, short2 q) -{ - return short2(p * q.x, p * q.y); -} -static inline short3 operator*(short p, short3 q) -{ - return short3(p * q.x, p * q.y, p * q.z); -} -static inline short4 operator*(short p, short4 q) -{ - return short4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline ushort2 operator*(ushort2 p, ushort2 q) -{ - return ushort2(p.x * q.x, p.y * q.y); -} -static inline ushort3 operator*(ushort3 p, ushort3 q) -{ - return ushort3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline ushort4 operator*(ushort4 p, ushort4 q) -{ - return ushort4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline ushort2 operator*(ushort2 p, ushort q) -{ - return ushort2(p.x * q, p.y * q); -} -static inline ushort3 operator*(ushort3 p, ushort q) -{ - return ushort3(p.x * q, p.y * q, p.z * q); -} -static inline ushort4 operator*(ushort4 p, ushort q) -{ - return ushort4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline ushort2 operator*(ushort p, ushort2 q) -{ - return ushort2(p * q.x, p * q.y); -} -static inline ushort3 operator*(ushort p, ushort3 q) -{ - return ushort3(p * q.x, p * q.y, p * q.z); -} -static inline ushort4 operator*(ushort p, ushort4 q) -{ - return ushort4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline long2 operator*(long2 p, long2 q) -{ - return long2(p.x * q.x, p.y * q.y); -} -static inline long3 operator*(long3 p, long3 q) -{ - return long3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline long4 operator*(long4 p, long4 q) -{ - return long4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline long2 operator*(long2 p, long q) -{ - return long2(p.x * q, p.y * q); -} -static inline long3 operator*(long3 p, long q) -{ - return long3(p.x * q, p.y * q, p.z * q); -} -static inline long4 operator*(long4 p, long q) -{ - return long4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline long2 operator*(long p, long2 q) -{ - return long2(p * q.x, p * q.y); -} -static inline long3 operator*(long p, long3 q) -{ - return long3(p * q.x, p * q.y, p * q.z); -} -static inline long4 operator*(long p, long4 q) -{ - return long4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline ulong2 operator*(ulong2 p, ulong2 q) -{ - return ulong2(p.x * q.x, p.y * q.y); -} -static inline ulong3 operator*(ulong3 p, ulong3 q) -{ - return ulong3(p.x * q.x, p.y * q.y, p.z * q.z); -} -static inline ulong4 operator*(ulong4 p, ulong4 q) -{ - return ulong4(p.x * q.x, p.y * q.y, p.z * q.z, p.w * q.w); -} -static inline ulong2 operator*(ulong2 p, ulong q) -{ - return ulong2(p.x * q, p.y * q); -} -static inline ulong3 operator*(ulong3 p, ulong q) -{ - return ulong3(p.x * q, p.y * q, p.z * q); -} -static inline ulong4 operator*(ulong4 p, ulong q) -{ - return ulong4(p.x * q, p.y * q, p.z * q, p.w * q); -} -static inline ulong2 operator*(ulong p, ulong2 q) -{ - return ulong2(p * q.x, p * q.y); -} -static inline ulong3 operator*(ulong p, ulong3 q) -{ - return ulong3(p * q.x, p * q.y, p * q.z); -} -static inline ulong4 operator*(ulong p, ulong4 q) -{ - return ulong4(p * q.x, p * q.y, p * q.z, p * q.w); -} -static inline float2 operator/(float2 p, float2 q) -{ - return float2(p.x / q.x, p.y / q.y); -} -static inline float3 operator/(float3 p, float3 q) -{ - return float3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline float4 operator/(float4 p, float4 q) -{ - return float4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline float2 operator/(float2 p, float q) -{ - return float2(p.x / q, p.y / q); -} -static inline float3 operator/(float3 p, float q) -{ - return float3(p.x / q, p.y / q, p.z / q); -} -static inline float4 operator/(float4 p, float q) -{ - return float4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline float2 operator/(float p, float2 q) -{ - return float2(p / q.x, p / q.y); -} -static inline float3 operator/(float p, float3 q) -{ - return float3(p / q.x, p / q.y, p / q.z); -} -static inline float4 operator/(float p, float4 q) -{ - return float4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline double2 operator/(double2 p, double2 q) -{ - return double2(p.x / q.x, p.y / q.y); -} -static inline double3 operator/(double3 p, double3 q) -{ - return double3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline double4 operator/(double4 p, double4 q) -{ - return double4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline double2 operator/(double2 p, double q) -{ - return double2(p.x / q, p.y / q); -} -static inline double3 operator/(double3 p, double q) -{ - return double3(p.x / q, p.y / q, p.z / q); -} -static inline double4 operator/(double4 p, double q) -{ - return double4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline double2 operator/(double p, double2 q) -{ - return double2(p / q.x, p / q.y); -} -static inline double3 operator/(double p, double3 q) -{ - return double3(p / q.x, p / q.y, p / q.z); -} -static inline double4 operator/(double p, double4 q) -{ - return double4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline int2 operator/(int2 p, int2 q) -{ - return int2(p.x / q.x, p.y / q.y); -} -static inline int3 operator/(int3 p, int3 q) -{ - return int3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline int4 operator/(int4 p, int4 q) -{ - return int4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline int2 operator/(int2 p, int q) -{ - return int2(p.x / q, p.y / q); -} -static inline int3 operator/(int3 p, int q) -{ - return int3(p.x / q, p.y / q, p.z / q); -} -static inline int4 operator/(int4 p, int q) -{ - return int4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline int2 operator/(int p, int2 q) -{ - return int2(p / q.x, p / q.y); -} -static inline int3 operator/(int p, int3 q) -{ - return int3(p / q.x, p / q.y, p / q.z); -} -static inline int4 operator/(int p, int4 q) -{ - return int4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline uint2 operator/(uint2 p, uint2 q) -{ - return uint2(p.x / q.x, p.y / q.y); -} -static inline uint3 operator/(uint3 p, uint3 q) -{ - return uint3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline uint4 operator/(uint4 p, uint4 q) -{ - return uint4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline uint2 operator/(uint2 p, uint q) -{ - return uint2(p.x / q, p.y / q); -} -static inline uint3 operator/(uint3 p, uint q) -{ - return uint3(p.x / q, p.y / q, p.z / q); -} -static inline uint4 operator/(uint4 p, uint q) -{ - return uint4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline uint2 operator/(uint p, uint2 q) -{ - return uint2(p / q.x, p / q.y); -} -static inline uint3 operator/(uint p, uint3 q) -{ - return uint3(p / q.x, p / q.y, p / q.z); -} -static inline uint4 operator/(uint p, uint4 q) -{ - return uint4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline short2 operator/(short2 p, short2 q) -{ - return short2(p.x / q.x, p.y / q.y); -} -static inline short3 operator/(short3 p, short3 q) -{ - return short3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline short4 operator/(short4 p, short4 q) -{ - return short4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline short2 operator/(short2 p, short q) -{ - return short2(p.x / q, p.y / q); -} -static inline short3 operator/(short3 p, short q) -{ - return short3(p.x / q, p.y / q, p.z / q); -} -static inline short4 operator/(short4 p, short q) -{ - return short4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline short2 operator/(short p, short2 q) -{ - return short2(p / q.x, p / q.y); -} -static inline short3 operator/(short p, short3 q) -{ - return short3(p / q.x, p / q.y, p / q.z); -} -static inline short4 operator/(short p, short4 q) -{ - return short4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline ushort2 operator/(ushort2 p, ushort2 q) -{ - return ushort2(p.x / q.x, p.y / q.y); -} -static inline ushort3 operator/(ushort3 p, ushort3 q) -{ - return ushort3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline ushort4 operator/(ushort4 p, ushort4 q) -{ - return ushort4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline ushort2 operator/(ushort2 p, ushort q) -{ - return ushort2(p.x / q, p.y / q); -} -static inline ushort3 operator/(ushort3 p, ushort q) -{ - return ushort3(p.x / q, p.y / q, p.z / q); -} -static inline ushort4 operator/(ushort4 p, ushort q) -{ - return ushort4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline ushort2 operator/(ushort p, ushort2 q) -{ - return ushort2(p / q.x, p / q.y); -} -static inline ushort3 operator/(ushort p, ushort3 q) -{ - return ushort3(p / q.x, p / q.y, p / q.z); -} -static inline ushort4 operator/(ushort p, ushort4 q) -{ - return ushort4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline long2 operator/(long2 p, long2 q) -{ - return long2(p.x / q.x, p.y / q.y); -} -static inline long3 operator/(long3 p, long3 q) -{ - return long3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline long4 operator/(long4 p, long4 q) -{ - return long4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline long2 operator/(long2 p, long q) -{ - return long2(p.x / q, p.y / q); -} -static inline long3 operator/(long3 p, long q) -{ - return long3(p.x / q, p.y / q, p.z / q); -} -static inline long4 operator/(long4 p, long q) -{ - return long4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline long2 operator/(long p, long2 q) -{ - return long2(p / q.x, p / q.y); -} -static inline long3 operator/(long p, long3 q) -{ - return long3(p / q.x, p / q.y, p / q.z); -} -static inline long4 operator/(long p, long4 q) -{ - return long4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline ulong2 operator/(ulong2 p, ulong2 q) -{ - return ulong2(p.x / q.x, p.y / q.y); -} -static inline ulong3 operator/(ulong3 p, ulong3 q) -{ - return ulong3(p.x / q.x, p.y / q.y, p.z / q.z); -} -static inline ulong4 operator/(ulong4 p, ulong4 q) -{ - return ulong4(p.x / q.x, p.y / q.y, p.z / q.z, p.w / q.w); -} -static inline ulong2 operator/(ulong2 p, ulong q) -{ - return ulong2(p.x / q, p.y / q); -} -static inline ulong3 operator/(ulong3 p, ulong q) -{ - return ulong3(p.x / q, p.y / q, p.z / q); -} -static inline ulong4 operator/(ulong4 p, ulong q) -{ - return ulong4(p.x / q, p.y / q, p.z / q, p.w / q); -} -static inline ulong2 operator/(ulong p, ulong2 q) -{ - return ulong2(p / q.x, p / q.y); -} -static inline ulong3 operator/(ulong p, ulong3 q) -{ - return ulong3(p / q.x, p / q.y, p / q.z); -} -static inline ulong4 operator/(ulong p, ulong4 q) -{ - return ulong4(p / q.x, p / q.y, p / q.z, p / q.w); -} -static inline int2 operator%(int2 p, int2 q) -{ - return int2(p.x % q.x, p.y % q.y); -} -static inline int3 operator%(int3 p, int3 q) -{ - return int3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline int4 operator%(int4 p, int4 q) -{ - return int4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline int2 operator%(int2 p, int q) -{ - return int2(p.x % q, p.y % q); -} -static inline int3 operator%(int3 p, int q) -{ - return int3(p.x % q, p.y % q, p.z % q); -} -static inline int4 operator%(int4 p, int q) -{ - return int4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline int2 operator%(int p, int2 q) -{ - return int2(p % q.x, p % q.y); -} -static inline int3 operator%(int p, int3 q) -{ - return int3(p % q.x, p % q.y, p % q.z); -} -static inline int4 operator%(int p, int4 q) -{ - return int4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline uint2 operator%(uint2 p, uint2 q) -{ - return uint2(p.x % q.x, p.y % q.y); -} -static inline uint3 operator%(uint3 p, uint3 q) -{ - return uint3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline uint4 operator%(uint4 p, uint4 q) -{ - return uint4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline uint2 operator%(uint2 p, uint q) -{ - return uint2(p.x % q, p.y % q); -} -static inline uint3 operator%(uint3 p, uint q) -{ - return uint3(p.x % q, p.y % q, p.z % q); -} -static inline uint4 operator%(uint4 p, uint q) -{ - return uint4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline uint2 operator%(uint p, uint2 q) -{ - return uint2(p % q.x, p % q.y); -} -static inline uint3 operator%(uint p, uint3 q) -{ - return uint3(p % q.x, p % q.y, p % q.z); -} -static inline uint4 operator%(uint p, uint4 q) -{ - return uint4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline short2 operator%(short2 p, short2 q) -{ - return short2(p.x % q.x, p.y % q.y); -} -static inline short3 operator%(short3 p, short3 q) -{ - return short3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline short4 operator%(short4 p, short4 q) -{ - return short4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline short2 operator%(short2 p, short q) -{ - return short2(p.x % q, p.y % q); -} -static inline short3 operator%(short3 p, short q) -{ - return short3(p.x % q, p.y % q, p.z % q); -} -static inline short4 operator%(short4 p, short q) -{ - return short4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline short2 operator%(short p, short2 q) -{ - return short2(p % q.x, p % q.y); -} -static inline short3 operator%(short p, short3 q) -{ - return short3(p % q.x, p % q.y, p % q.z); -} -static inline short4 operator%(short p, short4 q) -{ - return short4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline ushort2 operator%(ushort2 p, ushort2 q) -{ - return ushort2(p.x % q.x, p.y % q.y); -} -static inline ushort3 operator%(ushort3 p, ushort3 q) -{ - return ushort3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline ushort4 operator%(ushort4 p, ushort4 q) -{ - return ushort4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline ushort2 operator%(ushort2 p, ushort q) -{ - return ushort2(p.x % q, p.y % q); -} -static inline ushort3 operator%(ushort3 p, ushort q) -{ - return ushort3(p.x % q, p.y % q, p.z % q); -} -static inline ushort4 operator%(ushort4 p, ushort q) -{ - return ushort4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline ushort2 operator%(ushort p, ushort2 q) -{ - return ushort2(p % q.x, p % q.y); -} -static inline ushort3 operator%(ushort p, ushort3 q) -{ - return ushort3(p % q.x, p % q.y, p % q.z); -} -static inline ushort4 operator%(ushort p, ushort4 q) -{ - return ushort4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline long2 operator%(long2 p, long2 q) -{ - return long2(p.x % q.x, p.y % q.y); -} -static inline long3 operator%(long3 p, long3 q) -{ - return long3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline long4 operator%(long4 p, long4 q) -{ - return long4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline long2 operator%(long2 p, long q) -{ - return long2(p.x % q, p.y % q); -} -static inline long3 operator%(long3 p, long q) -{ - return long3(p.x % q, p.y % q, p.z % q); -} -static inline long4 operator%(long4 p, long q) -{ - return long4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline long2 operator%(long p, long2 q) -{ - return long2(p % q.x, p % q.y); -} -static inline long3 operator%(long p, long3 q) -{ - return long3(p % q.x, p % q.y, p % q.z); -} -static inline long4 operator%(long p, long4 q) -{ - return long4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline ulong2 operator%(ulong2 p, ulong2 q) -{ - return ulong2(p.x % q.x, p.y % q.y); -} -static inline ulong3 operator%(ulong3 p, ulong3 q) -{ - return ulong3(p.x % q.x, p.y % q.y, p.z % q.z); -} -static inline ulong4 operator%(ulong4 p, ulong4 q) -{ - return ulong4(p.x % q.x, p.y % q.y, p.z % q.z, p.w % q.w); -} -static inline ulong2 operator%(ulong2 p, ulong q) -{ - return ulong2(p.x % q, p.y % q); -} -static inline ulong3 operator%(ulong3 p, ulong q) -{ - return ulong3(p.x % q, p.y % q, p.z % q); -} -static inline ulong4 operator%(ulong4 p, ulong q) -{ - return ulong4(p.x % q, p.y % q, p.z % q, p.w % q); -} -static inline ulong2 operator%(ulong p, ulong2 q) -{ - return ulong2(p % q.x, p % q.y); -} -static inline ulong3 operator%(ulong p, ulong3 q) -{ - return ulong3(p % q.x, p % q.y, p % q.z); -} -static inline ulong4 operator%(ulong p, ulong4 q) -{ - return ulong4(p % q.x, p % q.y, p % q.z, p % q.w); -} -static inline int2 operator&(int2 p, int2 q) -{ - return int2(p.x & q.x, p.y & q.y); -} -static inline int3 operator&(int3 p, int3 q) -{ - return int3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline int4 operator&(int4 p, int4 q) -{ - return int4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline int2 operator&(int2 p, int q) -{ - return int2(p.x & q, p.y & q); -} -static inline int3 operator&(int3 p, int q) -{ - return int3(p.x & q, p.y & q, p.z & q); -} -static inline int4 operator&(int4 p, int q) -{ - return int4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline int2 operator&(int p, int2 q) -{ - return int2(p & q.x, p & q.y); -} -static inline int3 operator&(int p, int3 q) -{ - return int3(p & q.x, p & q.y, p & q.z); -} -static inline int4 operator&(int p, int4 q) -{ - return int4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline uint2 operator&(uint2 p, uint2 q) -{ - return uint2(p.x & q.x, p.y & q.y); -} -static inline uint3 operator&(uint3 p, uint3 q) -{ - return uint3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline uint4 operator&(uint4 p, uint4 q) -{ - return uint4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline uint2 operator&(uint2 p, uint q) -{ - return uint2(p.x & q, p.y & q); -} -static inline uint3 operator&(uint3 p, uint q) -{ - return uint3(p.x & q, p.y & q, p.z & q); -} -static inline uint4 operator&(uint4 p, uint q) -{ - return uint4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline uint2 operator&(uint p, uint2 q) -{ - return uint2(p & q.x, p & q.y); -} -static inline uint3 operator&(uint p, uint3 q) -{ - return uint3(p & q.x, p & q.y, p & q.z); -} -static inline uint4 operator&(uint p, uint4 q) -{ - return uint4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline short2 operator&(short2 p, short2 q) -{ - return short2(p.x & q.x, p.y & q.y); -} -static inline short3 operator&(short3 p, short3 q) -{ - return short3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline short4 operator&(short4 p, short4 q) -{ - return short4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline short2 operator&(short2 p, short q) -{ - return short2(p.x & q, p.y & q); -} -static inline short3 operator&(short3 p, short q) -{ - return short3(p.x & q, p.y & q, p.z & q); -} -static inline short4 operator&(short4 p, short q) -{ - return short4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline short2 operator&(short p, short2 q) -{ - return short2(p & q.x, p & q.y); -} -static inline short3 operator&(short p, short3 q) -{ - return short3(p & q.x, p & q.y, p & q.z); -} -static inline short4 operator&(short p, short4 q) -{ - return short4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline ushort2 operator&(ushort2 p, ushort2 q) -{ - return ushort2(p.x & q.x, p.y & q.y); -} -static inline ushort3 operator&(ushort3 p, ushort3 q) -{ - return ushort3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline ushort4 operator&(ushort4 p, ushort4 q) -{ - return ushort4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline ushort2 operator&(ushort2 p, ushort q) -{ - return ushort2(p.x & q, p.y & q); -} -static inline ushort3 operator&(ushort3 p, ushort q) -{ - return ushort3(p.x & q, p.y & q, p.z & q); -} -static inline ushort4 operator&(ushort4 p, ushort q) -{ - return ushort4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline ushort2 operator&(ushort p, ushort2 q) -{ - return ushort2(p & q.x, p & q.y); -} -static inline ushort3 operator&(ushort p, ushort3 q) -{ - return ushort3(p & q.x, p & q.y, p & q.z); -} -static inline ushort4 operator&(ushort p, ushort4 q) -{ - return ushort4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline long2 operator&(long2 p, long2 q) -{ - return long2(p.x & q.x, p.y & q.y); -} -static inline long3 operator&(long3 p, long3 q) -{ - return long3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline long4 operator&(long4 p, long4 q) -{ - return long4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline long2 operator&(long2 p, long q) -{ - return long2(p.x & q, p.y & q); -} -static inline long3 operator&(long3 p, long q) -{ - return long3(p.x & q, p.y & q, p.z & q); -} -static inline long4 operator&(long4 p, long q) -{ - return long4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline long2 operator&(long p, long2 q) -{ - return long2(p & q.x, p & q.y); -} -static inline long3 operator&(long p, long3 q) -{ - return long3(p & q.x, p & q.y, p & q.z); -} -static inline long4 operator&(long p, long4 q) -{ - return long4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline ulong2 operator&(ulong2 p, ulong2 q) -{ - return ulong2(p.x & q.x, p.y & q.y); -} -static inline ulong3 operator&(ulong3 p, ulong3 q) -{ - return ulong3(p.x & q.x, p.y & q.y, p.z & q.z); -} -static inline ulong4 operator&(ulong4 p, ulong4 q) -{ - return ulong4(p.x & q.x, p.y & q.y, p.z & q.z, p.w & q.w); -} -static inline ulong2 operator&(ulong2 p, ulong q) -{ - return ulong2(p.x & q, p.y & q); -} -static inline ulong3 operator&(ulong3 p, ulong q) -{ - return ulong3(p.x & q, p.y & q, p.z & q); -} -static inline ulong4 operator&(ulong4 p, ulong q) -{ - return ulong4(p.x & q, p.y & q, p.z & q, p.w & q); -} -static inline ulong2 operator&(ulong p, ulong2 q) -{ - return ulong2(p & q.x, p & q.y); -} -static inline ulong3 operator&(ulong p, ulong3 q) -{ - return ulong3(p & q.x, p & q.y, p & q.z); -} -static inline ulong4 operator&(ulong p, ulong4 q) -{ - return ulong4(p & q.x, p & q.y, p & q.z, p & q.w); -} -static inline int2 operator|(int2 p, int2 q) -{ - return int2(p.x | q.x, p.y | q.y); -} -static inline int3 operator|(int3 p, int3 q) -{ - return int3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline int4 operator|(int4 p, int4 q) -{ - return int4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline int2 operator|(int2 p, int q) -{ - return int2(p.x | q, p.y | q); -} -static inline int3 operator|(int3 p, int q) -{ - return int3(p.x | q, p.y | q, p.z | q); -} -static inline int4 operator|(int4 p, int q) -{ - return int4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline int2 operator|(int p, int2 q) -{ - return int2(p | q.x, p | q.y); -} -static inline int3 operator|(int p, int3 q) -{ - return int3(p | q.x, p | q.y, p | q.z); -} -static inline int4 operator|(int p, int4 q) -{ - return int4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline uint2 operator|(uint2 p, uint2 q) -{ - return uint2(p.x | q.x, p.y | q.y); -} -static inline uint3 operator|(uint3 p, uint3 q) -{ - return uint3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline uint4 operator|(uint4 p, uint4 q) -{ - return uint4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline uint2 operator|(uint2 p, uint q) -{ - return uint2(p.x | q, p.y | q); -} -static inline uint3 operator|(uint3 p, uint q) -{ - return uint3(p.x | q, p.y | q, p.z | q); -} -static inline uint4 operator|(uint4 p, uint q) -{ - return uint4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline uint2 operator|(uint p, uint2 q) -{ - return uint2(p | q.x, p | q.y); -} -static inline uint3 operator|(uint p, uint3 q) -{ - return uint3(p | q.x, p | q.y, p | q.z); -} -static inline uint4 operator|(uint p, uint4 q) -{ - return uint4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline short2 operator|(short2 p, short2 q) -{ - return short2(p.x | q.x, p.y | q.y); -} -static inline short3 operator|(short3 p, short3 q) -{ - return short3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline short4 operator|(short4 p, short4 q) -{ - return short4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline short2 operator|(short2 p, short q) -{ - return short2(p.x | q, p.y | q); -} -static inline short3 operator|(short3 p, short q) -{ - return short3(p.x | q, p.y | q, p.z | q); -} -static inline short4 operator|(short4 p, short q) -{ - return short4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline short2 operator|(short p, short2 q) -{ - return short2(p | q.x, p | q.y); -} -static inline short3 operator|(short p, short3 q) -{ - return short3(p | q.x, p | q.y, p | q.z); -} -static inline short4 operator|(short p, short4 q) -{ - return short4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline ushort2 operator|(ushort2 p, ushort2 q) -{ - return ushort2(p.x | q.x, p.y | q.y); -} -static inline ushort3 operator|(ushort3 p, ushort3 q) -{ - return ushort3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline ushort4 operator|(ushort4 p, ushort4 q) -{ - return ushort4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline ushort2 operator|(ushort2 p, ushort q) -{ - return ushort2(p.x | q, p.y | q); -} -static inline ushort3 operator|(ushort3 p, ushort q) -{ - return ushort3(p.x | q, p.y | q, p.z | q); -} -static inline ushort4 operator|(ushort4 p, ushort q) -{ - return ushort4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline ushort2 operator|(ushort p, ushort2 q) -{ - return ushort2(p | q.x, p | q.y); -} -static inline ushort3 operator|(ushort p, ushort3 q) -{ - return ushort3(p | q.x, p | q.y, p | q.z); -} -static inline ushort4 operator|(ushort p, ushort4 q) -{ - return ushort4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline long2 operator|(long2 p, long2 q) -{ - return long2(p.x | q.x, p.y | q.y); -} -static inline long3 operator|(long3 p, long3 q) -{ - return long3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline long4 operator|(long4 p, long4 q) -{ - return long4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline long2 operator|(long2 p, long q) -{ - return long2(p.x | q, p.y | q); -} -static inline long3 operator|(long3 p, long q) -{ - return long3(p.x | q, p.y | q, p.z | q); -} -static inline long4 operator|(long4 p, long q) -{ - return long4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline long2 operator|(long p, long2 q) -{ - return long2(p | q.x, p | q.y); -} -static inline long3 operator|(long p, long3 q) -{ - return long3(p | q.x, p | q.y, p | q.z); -} -static inline long4 operator|(long p, long4 q) -{ - return long4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline ulong2 operator|(ulong2 p, ulong2 q) -{ - return ulong2(p.x | q.x, p.y | q.y); -} -static inline ulong3 operator|(ulong3 p, ulong3 q) -{ - return ulong3(p.x | q.x, p.y | q.y, p.z | q.z); -} -static inline ulong4 operator|(ulong4 p, ulong4 q) -{ - return ulong4(p.x | q.x, p.y | q.y, p.z | q.z, p.w | q.w); -} -static inline ulong2 operator|(ulong2 p, ulong q) -{ - return ulong2(p.x | q, p.y | q); -} -static inline ulong3 operator|(ulong3 p, ulong q) -{ - return ulong3(p.x | q, p.y | q, p.z | q); -} -static inline ulong4 operator|(ulong4 p, ulong q) -{ - return ulong4(p.x | q, p.y | q, p.z | q, p.w | q); -} -static inline ulong2 operator|(ulong p, ulong2 q) -{ - return ulong2(p | q.x, p | q.y); -} -static inline ulong3 operator|(ulong p, ulong3 q) -{ - return ulong3(p | q.x, p | q.y, p | q.z); -} -static inline ulong4 operator|(ulong p, ulong4 q) -{ - return ulong4(p | q.x, p | q.y, p | q.z, p | q.w); -} -static inline int2 operator^(int2 p, int2 q) -{ - return int2(p.x ^ q.x, p.y ^ q.y); -} -static inline int3 operator^(int3 p, int3 q) -{ - return int3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline int4 operator^(int4 p, int4 q) -{ - return int4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline int2 operator^(int2 p, int q) -{ - return int2(p.x ^ q, p.y ^ q); -} -static inline int3 operator^(int3 p, int q) -{ - return int3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline int4 operator^(int4 p, int q) -{ - return int4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline int2 operator^(int p, int2 q) -{ - return int2(p ^ q.x, p ^ q.y); -} -static inline int3 operator^(int p, int3 q) -{ - return int3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline int4 operator^(int p, int4 q) -{ - return int4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline uint2 operator^(uint2 p, uint2 q) -{ - return uint2(p.x ^ q.x, p.y ^ q.y); -} -static inline uint3 operator^(uint3 p, uint3 q) -{ - return uint3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline uint4 operator^(uint4 p, uint4 q) -{ - return uint4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline uint2 operator^(uint2 p, uint q) -{ - return uint2(p.x ^ q, p.y ^ q); -} -static inline uint3 operator^(uint3 p, uint q) -{ - return uint3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline uint4 operator^(uint4 p, uint q) -{ - return uint4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline uint2 operator^(uint p, uint2 q) -{ - return uint2(p ^ q.x, p ^ q.y); -} -static inline uint3 operator^(uint p, uint3 q) -{ - return uint3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline uint4 operator^(uint p, uint4 q) -{ - return uint4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline short2 operator^(short2 p, short2 q) -{ - return short2(p.x ^ q.x, p.y ^ q.y); -} -static inline short3 operator^(short3 p, short3 q) -{ - return short3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline short4 operator^(short4 p, short4 q) -{ - return short4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline short2 operator^(short2 p, short q) -{ - return short2(p.x ^ q, p.y ^ q); -} -static inline short3 operator^(short3 p, short q) -{ - return short3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline short4 operator^(short4 p, short q) -{ - return short4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline short2 operator^(short p, short2 q) -{ - return short2(p ^ q.x, p ^ q.y); -} -static inline short3 operator^(short p, short3 q) -{ - return short3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline short4 operator^(short p, short4 q) -{ - return short4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline ushort2 operator^(ushort2 p, ushort2 q) -{ - return ushort2(p.x ^ q.x, p.y ^ q.y); -} -static inline ushort3 operator^(ushort3 p, ushort3 q) -{ - return ushort3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline ushort4 operator^(ushort4 p, ushort4 q) -{ - return ushort4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline ushort2 operator^(ushort2 p, ushort q) -{ - return ushort2(p.x ^ q, p.y ^ q); -} -static inline ushort3 operator^(ushort3 p, ushort q) -{ - return ushort3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline ushort4 operator^(ushort4 p, ushort q) -{ - return ushort4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline ushort2 operator^(ushort p, ushort2 q) -{ - return ushort2(p ^ q.x, p ^ q.y); -} -static inline ushort3 operator^(ushort p, ushort3 q) -{ - return ushort3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline ushort4 operator^(ushort p, ushort4 q) -{ - return ushort4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline long2 operator^(long2 p, long2 q) -{ - return long2(p.x ^ q.x, p.y ^ q.y); -} -static inline long3 operator^(long3 p, long3 q) -{ - return long3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline long4 operator^(long4 p, long4 q) -{ - return long4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline long2 operator^(long2 p, long q) -{ - return long2(p.x ^ q, p.y ^ q); -} -static inline long3 operator^(long3 p, long q) -{ - return long3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline long4 operator^(long4 p, long q) -{ - return long4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline long2 operator^(long p, long2 q) -{ - return long2(p ^ q.x, p ^ q.y); -} -static inline long3 operator^(long p, long3 q) -{ - return long3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline long4 operator^(long p, long4 q) -{ - return long4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline ulong2 operator^(ulong2 p, ulong2 q) -{ - return ulong2(p.x ^ q.x, p.y ^ q.y); -} -static inline ulong3 operator^(ulong3 p, ulong3 q) -{ - return ulong3(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z); -} -static inline ulong4 operator^(ulong4 p, ulong4 q) -{ - return ulong4(p.x ^ q.x, p.y ^ q.y, p.z ^ q.z, p.w ^ q.w); -} -static inline ulong2 operator^(ulong2 p, ulong q) -{ - return ulong2(p.x ^ q, p.y ^ q); -} -static inline ulong3 operator^(ulong3 p, ulong q) -{ - return ulong3(p.x ^ q, p.y ^ q, p.z ^ q); -} -static inline ulong4 operator^(ulong4 p, ulong q) -{ - return ulong4(p.x ^ q, p.y ^ q, p.z ^ q, p.w ^ q); -} -static inline ulong2 operator^(ulong p, ulong2 q) -{ - return ulong2(p ^ q.x, p ^ q.y); -} -static inline ulong3 operator^(ulong p, ulong3 q) -{ - return ulong3(p ^ q.x, p ^ q.y, p ^ q.z); -} -static inline ulong4 operator^(ulong p, ulong4 q) -{ - return ulong4(p ^ q.x, p ^ q.y, p ^ q.z, p ^ q.w); -} -static inline int2 operator<<(int2 p, int2 q) -{ - return int2(p.x << q.x, p.y << q.y); -} -static inline int3 operator<<(int3 p, int3 q) -{ - return int3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline int4 operator<<(int4 p, int4 q) -{ - return int4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline int2 operator<<(int2 p, int q) -{ - return int2(p.x << q, p.y << q); -} -static inline int3 operator<<(int3 p, int q) -{ - return int3(p.x << q, p.y << q, p.z << q); -} -static inline int4 operator<<(int4 p, int q) -{ - return int4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline int2 operator<<(int p, int2 q) -{ - return int2(p << q.x, p << q.y); -} -static inline int3 operator<<(int p, int3 q) -{ - return int3(p << q.x, p << q.y, p << q.z); -} -static inline int4 operator<<(int p, int4 q) -{ - return int4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline uint2 operator<<(uint2 p, uint2 q) -{ - return uint2(p.x << q.x, p.y << q.y); -} -static inline uint3 operator<<(uint3 p, uint3 q) -{ - return uint3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline uint4 operator<<(uint4 p, uint4 q) -{ - return uint4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline uint2 operator<<(uint2 p, uint q) -{ - return uint2(p.x << q, p.y << q); -} -static inline uint3 operator<<(uint3 p, uint q) -{ - return uint3(p.x << q, p.y << q, p.z << q); -} -static inline uint4 operator<<(uint4 p, uint q) -{ - return uint4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline uint2 operator<<(uint p, uint2 q) -{ - return uint2(p << q.x, p << q.y); -} -static inline uint3 operator<<(uint p, uint3 q) -{ - return uint3(p << q.x, p << q.y, p << q.z); -} -static inline uint4 operator<<(uint p, uint4 q) -{ - return uint4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline short2 operator<<(short2 p, short2 q) -{ - return short2(p.x << q.x, p.y << q.y); -} -static inline short3 operator<<(short3 p, short3 q) -{ - return short3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline short4 operator<<(short4 p, short4 q) -{ - return short4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline short2 operator<<(short2 p, short q) -{ - return short2(p.x << q, p.y << q); -} -static inline short3 operator<<(short3 p, short q) -{ - return short3(p.x << q, p.y << q, p.z << q); -} -static inline short4 operator<<(short4 p, short q) -{ - return short4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline short2 operator<<(short p, short2 q) -{ - return short2(p << q.x, p << q.y); -} -static inline short3 operator<<(short p, short3 q) -{ - return short3(p << q.x, p << q.y, p << q.z); -} -static inline short4 operator<<(short p, short4 q) -{ - return short4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline ushort2 operator<<(ushort2 p, ushort2 q) -{ - return ushort2(p.x << q.x, p.y << q.y); -} -static inline ushort3 operator<<(ushort3 p, ushort3 q) -{ - return ushort3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline ushort4 operator<<(ushort4 p, ushort4 q) -{ - return ushort4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline ushort2 operator<<(ushort2 p, ushort q) -{ - return ushort2(p.x << q, p.y << q); -} -static inline ushort3 operator<<(ushort3 p, ushort q) -{ - return ushort3(p.x << q, p.y << q, p.z << q); -} -static inline ushort4 operator<<(ushort4 p, ushort q) -{ - return ushort4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline ushort2 operator<<(ushort p, ushort2 q) -{ - return ushort2(p << q.x, p << q.y); -} -static inline ushort3 operator<<(ushort p, ushort3 q) -{ - return ushort3(p << q.x, p << q.y, p << q.z); -} -static inline ushort4 operator<<(ushort p, ushort4 q) -{ - return ushort4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline long2 operator<<(long2 p, long2 q) -{ - return long2(p.x << q.x, p.y << q.y); -} -static inline long3 operator<<(long3 p, long3 q) -{ - return long3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline long4 operator<<(long4 p, long4 q) -{ - return long4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline long2 operator<<(long2 p, long q) -{ - return long2(p.x << q, p.y << q); -} -static inline long3 operator<<(long3 p, long q) -{ - return long3(p.x << q, p.y << q, p.z << q); -} -static inline long4 operator<<(long4 p, long q) -{ - return long4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline long2 operator<<(long p, long2 q) -{ - return long2(p << q.x, p << q.y); -} -static inline long3 operator<<(long p, long3 q) -{ - return long3(p << q.x, p << q.y, p << q.z); -} -static inline long4 operator<<(long p, long4 q) -{ - return long4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline ulong2 operator<<(ulong2 p, ulong2 q) -{ - return ulong2(p.x << q.x, p.y << q.y); -} -static inline ulong3 operator<<(ulong3 p, ulong3 q) -{ - return ulong3(p.x << q.x, p.y << q.y, p.z << q.z); -} -static inline ulong4 operator<<(ulong4 p, ulong4 q) -{ - return ulong4(p.x << q.x, p.y << q.y, p.z << q.z, p.w << q.w); -} -static inline ulong2 operator<<(ulong2 p, ulong q) -{ - return ulong2(p.x << q, p.y << q); -} -static inline ulong3 operator<<(ulong3 p, ulong q) -{ - return ulong3(p.x << q, p.y << q, p.z << q); -} -static inline ulong4 operator<<(ulong4 p, ulong q) -{ - return ulong4(p.x << q, p.y << q, p.z << q, p.w << q); -} -static inline ulong2 operator<<(ulong p, ulong2 q) -{ - return ulong2(p << q.x, p << q.y); -} -static inline ulong3 operator<<(ulong p, ulong3 q) -{ - return ulong3(p << q.x, p << q.y, p << q.z); -} -static inline ulong4 operator<<(ulong p, ulong4 q) -{ - return ulong4(p << q.x, p << q.y, p << q.z, p << q.w); -} -static inline int2 operator>>(int2 p, int2 q) -{ - return int2(p.x >> q.x, p.y >> q.y); -} -static inline int3 operator>>(int3 p, int3 q) -{ - return int3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline int4 operator>>(int4 p, int4 q) -{ - return int4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline int2 operator>>(int2 p, int q) -{ - return int2(p.x >> q, p.y >> q); -} -static inline int3 operator>>(int3 p, int q) -{ - return int3(p.x >> q, p.y >> q, p.z >> q); -} -static inline int4 operator>>(int4 p, int q) -{ - return int4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline int2 operator>>(int p, int2 q) -{ - return int2(p >> q.x, p >> q.y); -} -static inline int3 operator>>(int p, int3 q) -{ - return int3(p >> q.x, p >> q.y, p >> q.z); -} -static inline int4 operator>>(int p, int4 q) -{ - return int4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline uint2 operator>>(uint2 p, uint2 q) -{ - return uint2(p.x >> q.x, p.y >> q.y); -} -static inline uint3 operator>>(uint3 p, uint3 q) -{ - return uint3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline uint4 operator>>(uint4 p, uint4 q) -{ - return uint4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline uint2 operator>>(uint2 p, uint q) -{ - return uint2(p.x >> q, p.y >> q); -} -static inline uint3 operator>>(uint3 p, uint q) -{ - return uint3(p.x >> q, p.y >> q, p.z >> q); -} -static inline uint4 operator>>(uint4 p, uint q) -{ - return uint4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline uint2 operator>>(uint p, uint2 q) -{ - return uint2(p >> q.x, p >> q.y); -} -static inline uint3 operator>>(uint p, uint3 q) -{ - return uint3(p >> q.x, p >> q.y, p >> q.z); -} -static inline uint4 operator>>(uint p, uint4 q) -{ - return uint4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline short2 operator>>(short2 p, short2 q) -{ - return short2(p.x >> q.x, p.y >> q.y); -} -static inline short3 operator>>(short3 p, short3 q) -{ - return short3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline short4 operator>>(short4 p, short4 q) -{ - return short4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline short2 operator>>(short2 p, short q) -{ - return short2(p.x >> q, p.y >> q); -} -static inline short3 operator>>(short3 p, short q) -{ - return short3(p.x >> q, p.y >> q, p.z >> q); -} -static inline short4 operator>>(short4 p, short q) -{ - return short4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline short2 operator>>(short p, short2 q) -{ - return short2(p >> q.x, p >> q.y); -} -static inline short3 operator>>(short p, short3 q) -{ - return short3(p >> q.x, p >> q.y, p >> q.z); -} -static inline short4 operator>>(short p, short4 q) -{ - return short4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline ushort2 operator>>(ushort2 p, ushort2 q) -{ - return ushort2(p.x >> q.x, p.y >> q.y); -} -static inline ushort3 operator>>(ushort3 p, ushort3 q) -{ - return ushort3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline ushort4 operator>>(ushort4 p, ushort4 q) -{ - return ushort4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline ushort2 operator>>(ushort2 p, ushort q) -{ - return ushort2(p.x >> q, p.y >> q); -} -static inline ushort3 operator>>(ushort3 p, ushort q) -{ - return ushort3(p.x >> q, p.y >> q, p.z >> q); -} -static inline ushort4 operator>>(ushort4 p, ushort q) -{ - return ushort4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline ushort2 operator>>(ushort p, ushort2 q) -{ - return ushort2(p >> q.x, p >> q.y); -} -static inline ushort3 operator>>(ushort p, ushort3 q) -{ - return ushort3(p >> q.x, p >> q.y, p >> q.z); -} -static inline ushort4 operator>>(ushort p, ushort4 q) -{ - return ushort4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline long2 operator>>(long2 p, long2 q) -{ - return long2(p.x >> q.x, p.y >> q.y); -} -static inline long3 operator>>(long3 p, long3 q) -{ - return long3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline long4 operator>>(long4 p, long4 q) -{ - return long4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline long2 operator>>(long2 p, long q) -{ - return long2(p.x >> q, p.y >> q); -} -static inline long3 operator>>(long3 p, long q) -{ - return long3(p.x >> q, p.y >> q, p.z >> q); -} -static inline long4 operator>>(long4 p, long q) -{ - return long4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline long2 operator>>(long p, long2 q) -{ - return long2(p >> q.x, p >> q.y); -} -static inline long3 operator>>(long p, long3 q) -{ - return long3(p >> q.x, p >> q.y, p >> q.z); -} -static inline long4 operator>>(long p, long4 q) -{ - return long4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline ulong2 operator>>(ulong2 p, ulong2 q) -{ - return ulong2(p.x >> q.x, p.y >> q.y); -} -static inline ulong3 operator>>(ulong3 p, ulong3 q) -{ - return ulong3(p.x >> q.x, p.y >> q.y, p.z >> q.z); -} -static inline ulong4 operator>>(ulong4 p, ulong4 q) -{ - return ulong4(p.x >> q.x, p.y >> q.y, p.z >> q.z, p.w >> q.w); -} -static inline ulong2 operator>>(ulong2 p, ulong q) -{ - return ulong2(p.x >> q, p.y >> q); -} -static inline ulong3 operator>>(ulong3 p, ulong q) -{ - return ulong3(p.x >> q, p.y >> q, p.z >> q); -} -static inline ulong4 operator>>(ulong4 p, ulong q) -{ - return ulong4(p.x >> q, p.y >> q, p.z >> q, p.w >> q); -} -static inline ulong2 operator>>(ulong p, ulong2 q) -{ - return ulong2(p >> q.x, p >> q.y); -} -static inline ulong3 operator>>(ulong p, ulong3 q) -{ - return ulong3(p >> q.x, p >> q.y, p >> q.z); -} -static inline ulong4 operator>>(ulong p, ulong4 q) -{ - return ulong4(p >> q.x, p >> q.y, p >> q.z, p >> q.w); -} -static inline int2 operator==(float2 p, float2 q) -{ - return int2(p.x == q.x, p.y == q.y); -} -static inline int3 operator==(float3 p, float3 q) -{ - return int3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline int4 operator==(float4 p, float4 q) -{ - return int4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline int2 operator==(float2 p, float q) -{ - return int2(p.x == q, p.y == q); -} -static inline int3 operator==(float3 p, float q) -{ - return int3(p.x == q, p.y == q, p.z == q); -} -static inline int4 operator==(float4 p, float q) -{ - return int4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline int2 operator==(float p, float2 q) -{ - return int2(p == q.x, p == q.y); -} -static inline int3 operator==(float p, float3 q) -{ - return int3(p == q.x, p == q.y, p == q.z); -} -static inline int4 operator==(float p, float4 q) -{ - return int4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline long2 operator==(double2 p, double2 q) -{ - return long2(p.x == q.x, p.y == q.y); -} -static inline long3 operator==(double3 p, double3 q) -{ - return long3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline long4 operator==(double4 p, double4 q) -{ - return long4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline long2 operator==(double2 p, double q) -{ - return long2(p.x == q, p.y == q); -} -static inline long3 operator==(double3 p, double q) -{ - return long3(p.x == q, p.y == q, p.z == q); -} -static inline long4 operator==(double4 p, double q) -{ - return long4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline long2 operator==(double p, double2 q) -{ - return long2(p == q.x, p == q.y); -} -static inline long3 operator==(double p, double3 q) -{ - return long3(p == q.x, p == q.y, p == q.z); -} -static inline long4 operator==(double p, double4 q) -{ - return long4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline int2 operator==(int2 p, int2 q) -{ - return int2(p.x == q.x, p.y == q.y); -} -static inline int3 operator==(int3 p, int3 q) -{ - return int3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline int4 operator==(int4 p, int4 q) -{ - return int4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline int2 operator==(int2 p, int q) -{ - return int2(p.x == q, p.y == q); -} -static inline int3 operator==(int3 p, int q) -{ - return int3(p.x == q, p.y == q, p.z == q); -} -static inline int4 operator==(int4 p, int q) -{ - return int4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline int2 operator==(int p, int2 q) -{ - return int2(p == q.x, p == q.y); -} -static inline int3 operator==(int p, int3 q) -{ - return int3(p == q.x, p == q.y, p == q.z); -} -static inline int4 operator==(int p, int4 q) -{ - return int4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline int2 operator==(uint2 p, uint2 q) -{ - return int2(p.x == q.x, p.y == q.y); -} -static inline int3 operator==(uint3 p, uint3 q) -{ - return int3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline int4 operator==(uint4 p, uint4 q) -{ - return int4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline int2 operator==(uint2 p, uint q) -{ - return int2(p.x == q, p.y == q); -} -static inline int3 operator==(uint3 p, uint q) -{ - return int3(p.x == q, p.y == q, p.z == q); -} -static inline int4 operator==(uint4 p, uint q) -{ - return int4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline int2 operator==(uint p, uint2 q) -{ - return int2(p == q.x, p == q.y); -} -static inline int3 operator==(uint p, uint3 q) -{ - return int3(p == q.x, p == q.y, p == q.z); -} -static inline int4 operator==(uint p, uint4 q) -{ - return int4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline short2 operator==(short2 p, short2 q) -{ - return short2(p.x == q.x, p.y == q.y); -} -static inline short3 operator==(short3 p, short3 q) -{ - return short3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline short4 operator==(short4 p, short4 q) -{ - return short4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline short2 operator==(short2 p, short q) -{ - return short2(p.x == q, p.y == q); -} -static inline short3 operator==(short3 p, short q) -{ - return short3(p.x == q, p.y == q, p.z == q); -} -static inline short4 operator==(short4 p, short q) -{ - return short4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline short2 operator==(short p, short2 q) -{ - return short2(p == q.x, p == q.y); -} -static inline short3 operator==(short p, short3 q) -{ - return short3(p == q.x, p == q.y, p == q.z); -} -static inline short4 operator==(short p, short4 q) -{ - return short4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline short2 operator==(ushort2 p, ushort2 q) -{ - return short2(p.x == q.x, p.y == q.y); -} -static inline short3 operator==(ushort3 p, ushort3 q) -{ - return short3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline short4 operator==(ushort4 p, ushort4 q) -{ - return short4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline short2 operator==(ushort2 p, ushort q) -{ - return short2(p.x == q, p.y == q); -} -static inline short3 operator==(ushort3 p, ushort q) -{ - return short3(p.x == q, p.y == q, p.z == q); -} -static inline short4 operator==(ushort4 p, ushort q) -{ - return short4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline short2 operator==(ushort p, ushort2 q) -{ - return short2(p == q.x, p == q.y); -} -static inline short3 operator==(ushort p, ushort3 q) -{ - return short3(p == q.x, p == q.y, p == q.z); -} -static inline short4 operator==(ushort p, ushort4 q) -{ - return short4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline long2 operator==(long2 p, long2 q) -{ - return long2(p.x == q.x, p.y == q.y); -} -static inline long3 operator==(long3 p, long3 q) -{ - return long3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline long4 operator==(long4 p, long4 q) -{ - return long4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline long2 operator==(long2 p, long q) -{ - return long2(p.x == q, p.y == q); -} -static inline long3 operator==(long3 p, long q) -{ - return long3(p.x == q, p.y == q, p.z == q); -} -static inline long4 operator==(long4 p, long q) -{ - return long4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline long2 operator==(long p, long2 q) -{ - return long2(p == q.x, p == q.y); -} -static inline long3 operator==(long p, long3 q) -{ - return long3(p == q.x, p == q.y, p == q.z); -} -static inline long4 operator==(long p, long4 q) -{ - return long4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline long2 operator==(ulong2 p, ulong2 q) -{ - return long2(p.x == q.x, p.y == q.y); -} -static inline long3 operator==(ulong3 p, ulong3 q) -{ - return long3(p.x == q.x, p.y == q.y, p.z == q.z); -} -static inline long4 operator==(ulong4 p, ulong4 q) -{ - return long4(p.x == q.x, p.y == q.y, p.z == q.z, p.w == q.w); -} -static inline long2 operator==(ulong2 p, ulong q) -{ - return long2(p.x == q, p.y == q); -} -static inline long3 operator==(ulong3 p, ulong q) -{ - return long3(p.x == q, p.y == q, p.z == q); -} -static inline long4 operator==(ulong4 p, ulong q) -{ - return long4(p.x == q, p.y == q, p.z == q, p.w == q); -} -static inline long2 operator==(ulong p, ulong2 q) -{ - return long2(p == q.x, p == q.y); -} -static inline long3 operator==(ulong p, ulong3 q) -{ - return long3(p == q.x, p == q.y, p == q.z); -} -static inline long4 operator==(ulong p, ulong4 q) -{ - return long4(p == q.x, p == q.y, p == q.z, p == q.w); -} -static inline int2 operator!=(float2 p, float2 q) -{ - return int2(p.x != q.x, p.y != q.y); -} -static inline int3 operator!=(float3 p, float3 q) -{ - return int3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline int4 operator!=(float4 p, float4 q) -{ - return int4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline int2 operator!=(float2 p, float q) -{ - return int2(p.x != q, p.y != q); -} -static inline int3 operator!=(float3 p, float q) -{ - return int3(p.x != q, p.y != q, p.z != q); -} -static inline int4 operator!=(float4 p, float q) -{ - return int4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline int2 operator!=(float p, float2 q) -{ - return int2(p != q.x, p != q.y); -} -static inline int3 operator!=(float p, float3 q) -{ - return int3(p != q.x, p != q.y, p != q.z); -} -static inline int4 operator!=(float p, float4 q) -{ - return int4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline long2 operator!=(double2 p, double2 q) -{ - return long2(p.x != q.x, p.y != q.y); -} -static inline long3 operator!=(double3 p, double3 q) -{ - return long3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline long4 operator!=(double4 p, double4 q) -{ - return long4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline long2 operator!=(double2 p, double q) -{ - return long2(p.x != q, p.y != q); -} -static inline long3 operator!=(double3 p, double q) -{ - return long3(p.x != q, p.y != q, p.z != q); -} -static inline long4 operator!=(double4 p, double q) -{ - return long4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline long2 operator!=(double p, double2 q) -{ - return long2(p != q.x, p != q.y); -} -static inline long3 operator!=(double p, double3 q) -{ - return long3(p != q.x, p != q.y, p != q.z); -} -static inline long4 operator!=(double p, double4 q) -{ - return long4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline int2 operator!=(int2 p, int2 q) -{ - return int2(p.x != q.x, p.y != q.y); -} -static inline int3 operator!=(int3 p, int3 q) -{ - return int3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline int4 operator!=(int4 p, int4 q) -{ - return int4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline int2 operator!=(int2 p, int q) -{ - return int2(p.x != q, p.y != q); -} -static inline int3 operator!=(int3 p, int q) -{ - return int3(p.x != q, p.y != q, p.z != q); -} -static inline int4 operator!=(int4 p, int q) -{ - return int4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline int2 operator!=(int p, int2 q) -{ - return int2(p != q.x, p != q.y); -} -static inline int3 operator!=(int p, int3 q) -{ - return int3(p != q.x, p != q.y, p != q.z); -} -static inline int4 operator!=(int p, int4 q) -{ - return int4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline int2 operator!=(uint2 p, uint2 q) -{ - return int2(p.x != q.x, p.y != q.y); -} -static inline int3 operator!=(uint3 p, uint3 q) -{ - return int3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline int4 operator!=(uint4 p, uint4 q) -{ - return int4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline int2 operator!=(uint2 p, uint q) -{ - return int2(p.x != q, p.y != q); -} -static inline int3 operator!=(uint3 p, uint q) -{ - return int3(p.x != q, p.y != q, p.z != q); -} -static inline int4 operator!=(uint4 p, uint q) -{ - return int4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline int2 operator!=(uint p, uint2 q) -{ - return int2(p != q.x, p != q.y); -} -static inline int3 operator!=(uint p, uint3 q) -{ - return int3(p != q.x, p != q.y, p != q.z); -} -static inline int4 operator!=(uint p, uint4 q) -{ - return int4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline short2 operator!=(short2 p, short2 q) -{ - return short2(p.x != q.x, p.y != q.y); -} -static inline short3 operator!=(short3 p, short3 q) -{ - return short3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline short4 operator!=(short4 p, short4 q) -{ - return short4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline short2 operator!=(short2 p, short q) -{ - return short2(p.x != q, p.y != q); -} -static inline short3 operator!=(short3 p, short q) -{ - return short3(p.x != q, p.y != q, p.z != q); -} -static inline short4 operator!=(short4 p, short q) -{ - return short4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline short2 operator!=(short p, short2 q) -{ - return short2(p != q.x, p != q.y); -} -static inline short3 operator!=(short p, short3 q) -{ - return short3(p != q.x, p != q.y, p != q.z); -} -static inline short4 operator!=(short p, short4 q) -{ - return short4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline short2 operator!=(ushort2 p, ushort2 q) -{ - return short2(p.x != q.x, p.y != q.y); -} -static inline short3 operator!=(ushort3 p, ushort3 q) -{ - return short3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline short4 operator!=(ushort4 p, ushort4 q) -{ - return short4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline short2 operator!=(ushort2 p, ushort q) -{ - return short2(p.x != q, p.y != q); -} -static inline short3 operator!=(ushort3 p, ushort q) -{ - return short3(p.x != q, p.y != q, p.z != q); -} -static inline short4 operator!=(ushort4 p, ushort q) -{ - return short4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline short2 operator!=(ushort p, ushort2 q) -{ - return short2(p != q.x, p != q.y); -} -static inline short3 operator!=(ushort p, ushort3 q) -{ - return short3(p != q.x, p != q.y, p != q.z); -} -static inline short4 operator!=(ushort p, ushort4 q) -{ - return short4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline long2 operator!=(long2 p, long2 q) -{ - return long2(p.x != q.x, p.y != q.y); -} -static inline long3 operator!=(long3 p, long3 q) -{ - return long3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline long4 operator!=(long4 p, long4 q) -{ - return long4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline long2 operator!=(long2 p, long q) -{ - return long2(p.x != q, p.y != q); -} -static inline long3 operator!=(long3 p, long q) -{ - return long3(p.x != q, p.y != q, p.z != q); -} -static inline long4 operator!=(long4 p, long q) -{ - return long4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline long2 operator!=(long p, long2 q) -{ - return long2(p != q.x, p != q.y); -} -static inline long3 operator!=(long p, long3 q) -{ - return long3(p != q.x, p != q.y, p != q.z); -} -static inline long4 operator!=(long p, long4 q) -{ - return long4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline long2 operator!=(ulong2 p, ulong2 q) -{ - return long2(p.x != q.x, p.y != q.y); -} -static inline long3 operator!=(ulong3 p, ulong3 q) -{ - return long3(p.x != q.x, p.y != q.y, p.z != q.z); -} -static inline long4 operator!=(ulong4 p, ulong4 q) -{ - return long4(p.x != q.x, p.y != q.y, p.z != q.z, p.w != q.w); -} -static inline long2 operator!=(ulong2 p, ulong q) -{ - return long2(p.x != q, p.y != q); -} -static inline long3 operator!=(ulong3 p, ulong q) -{ - return long3(p.x != q, p.y != q, p.z != q); -} -static inline long4 operator!=(ulong4 p, ulong q) -{ - return long4(p.x != q, p.y != q, p.z != q, p.w != q); -} -static inline long2 operator!=(ulong p, ulong2 q) -{ - return long2(p != q.x, p != q.y); -} -static inline long3 operator!=(ulong p, ulong3 q) -{ - return long3(p != q.x, p != q.y, p != q.z); -} -static inline long4 operator!=(ulong p, ulong4 q) -{ - return long4(p != q.x, p != q.y, p != q.z, p != q.w); -} -static inline int2 operator>(float2 p, float2 q) -{ - return int2(p.x > q.x, p.y > q.y); -} -static inline int3 operator>(float3 p, float3 q) -{ - return int3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline int4 operator>(float4 p, float4 q) -{ - return int4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline int2 operator>(float2 p, float q) -{ - return int2(p.x > q, p.y > q); -} -static inline int3 operator>(float3 p, float q) -{ - return int3(p.x > q, p.y > q, p.z > q); -} -static inline int4 operator>(float4 p, float q) -{ - return int4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline int2 operator>(float p, float2 q) -{ - return int2(p > q.x, p > q.y); -} -static inline int3 operator>(float p, float3 q) -{ - return int3(p > q.x, p > q.y, p > q.z); -} -static inline int4 operator>(float p, float4 q) -{ - return int4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline long2 operator>(double2 p, double2 q) -{ - return long2(p.x > q.x, p.y > q.y); -} -static inline long3 operator>(double3 p, double3 q) -{ - return long3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline long4 operator>(double4 p, double4 q) -{ - return long4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline long2 operator>(double2 p, double q) -{ - return long2(p.x > q, p.y > q); -} -static inline long3 operator>(double3 p, double q) -{ - return long3(p.x > q, p.y > q, p.z > q); -} -static inline long4 operator>(double4 p, double q) -{ - return long4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline long2 operator>(double p, double2 q) -{ - return long2(p > q.x, p > q.y); -} -static inline long3 operator>(double p, double3 q) -{ - return long3(p > q.x, p > q.y, p > q.z); -} -static inline long4 operator>(double p, double4 q) -{ - return long4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline int2 operator>(int2 p, int2 q) -{ - return int2(p.x > q.x, p.y > q.y); -} -static inline int3 operator>(int3 p, int3 q) -{ - return int3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline int4 operator>(int4 p, int4 q) -{ - return int4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline int2 operator>(int2 p, int q) -{ - return int2(p.x > q, p.y > q); -} -static inline int3 operator>(int3 p, int q) -{ - return int3(p.x > q, p.y > q, p.z > q); -} -static inline int4 operator>(int4 p, int q) -{ - return int4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline int2 operator>(int p, int2 q) -{ - return int2(p > q.x, p > q.y); -} -static inline int3 operator>(int p, int3 q) -{ - return int3(p > q.x, p > q.y, p > q.z); -} -static inline int4 operator>(int p, int4 q) -{ - return int4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline int2 operator>(uint2 p, uint2 q) -{ - return int2(p.x > q.x, p.y > q.y); -} -static inline int3 operator>(uint3 p, uint3 q) -{ - return int3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline int4 operator>(uint4 p, uint4 q) -{ - return int4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline int2 operator>(uint2 p, uint q) -{ - return int2(p.x > q, p.y > q); -} -static inline int3 operator>(uint3 p, uint q) -{ - return int3(p.x > q, p.y > q, p.z > q); -} -static inline int4 operator>(uint4 p, uint q) -{ - return int4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline int2 operator>(uint p, uint2 q) -{ - return int2(p > q.x, p > q.y); -} -static inline int3 operator>(uint p, uint3 q) -{ - return int3(p > q.x, p > q.y, p > q.z); -} -static inline int4 operator>(uint p, uint4 q) -{ - return int4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline short2 operator>(short2 p, short2 q) -{ - return short2(p.x > q.x, p.y > q.y); -} -static inline short3 operator>(short3 p, short3 q) -{ - return short3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline short4 operator>(short4 p, short4 q) -{ - return short4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline short2 operator>(short2 p, short q) -{ - return short2(p.x > q, p.y > q); -} -static inline short3 operator>(short3 p, short q) -{ - return short3(p.x > q, p.y > q, p.z > q); -} -static inline short4 operator>(short4 p, short q) -{ - return short4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline short2 operator>(short p, short2 q) -{ - return short2(p > q.x, p > q.y); -} -static inline short3 operator>(short p, short3 q) -{ - return short3(p > q.x, p > q.y, p > q.z); -} -static inline short4 operator>(short p, short4 q) -{ - return short4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline short2 operator>(ushort2 p, ushort2 q) -{ - return short2(p.x > q.x, p.y > q.y); -} -static inline short3 operator>(ushort3 p, ushort3 q) -{ - return short3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline short4 operator>(ushort4 p, ushort4 q) -{ - return short4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline short2 operator>(ushort2 p, ushort q) -{ - return short2(p.x > q, p.y > q); -} -static inline short3 operator>(ushort3 p, ushort q) -{ - return short3(p.x > q, p.y > q, p.z > q); -} -static inline short4 operator>(ushort4 p, ushort q) -{ - return short4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline short2 operator>(ushort p, ushort2 q) -{ - return short2(p > q.x, p > q.y); -} -static inline short3 operator>(ushort p, ushort3 q) -{ - return short3(p > q.x, p > q.y, p > q.z); -} -static inline short4 operator>(ushort p, ushort4 q) -{ - return short4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline long2 operator>(long2 p, long2 q) -{ - return long2(p.x > q.x, p.y > q.y); -} -static inline long3 operator>(long3 p, long3 q) -{ - return long3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline long4 operator>(long4 p, long4 q) -{ - return long4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline long2 operator>(long2 p, long q) -{ - return long2(p.x > q, p.y > q); -} -static inline long3 operator>(long3 p, long q) -{ - return long3(p.x > q, p.y > q, p.z > q); -} -static inline long4 operator>(long4 p, long q) -{ - return long4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline long2 operator>(long p, long2 q) -{ - return long2(p > q.x, p > q.y); -} -static inline long3 operator>(long p, long3 q) -{ - return long3(p > q.x, p > q.y, p > q.z); -} -static inline long4 operator>(long p, long4 q) -{ - return long4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline long2 operator>(ulong2 p, ulong2 q) -{ - return long2(p.x > q.x, p.y > q.y); -} -static inline long3 operator>(ulong3 p, ulong3 q) -{ - return long3(p.x > q.x, p.y > q.y, p.z > q.z); -} -static inline long4 operator>(ulong4 p, ulong4 q) -{ - return long4(p.x > q.x, p.y > q.y, p.z > q.z, p.w > q.w); -} -static inline long2 operator>(ulong2 p, ulong q) -{ - return long2(p.x > q, p.y > q); -} -static inline long3 operator>(ulong3 p, ulong q) -{ - return long3(p.x > q, p.y > q, p.z > q); -} -static inline long4 operator>(ulong4 p, ulong q) -{ - return long4(p.x > q, p.y > q, p.z > q, p.w > q); -} -static inline long2 operator>(ulong p, ulong2 q) -{ - return long2(p > q.x, p > q.y); -} -static inline long3 operator>(ulong p, ulong3 q) -{ - return long3(p > q.x, p > q.y, p > q.z); -} -static inline long4 operator>(ulong p, ulong4 q) -{ - return long4(p > q.x, p > q.y, p > q.z, p > q.w); -} -static inline int2 operator<(float2 p, float2 q) -{ - return int2(p.x < q.x, p.y < q.y); -} -static inline int3 operator<(float3 p, float3 q) -{ - return int3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline int4 operator<(float4 p, float4 q) -{ - return int4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline int2 operator<(float2 p, float q) -{ - return int2(p.x < q, p.y < q); -} -static inline int3 operator<(float3 p, float q) -{ - return int3(p.x < q, p.y < q, p.z < q); -} -static inline int4 operator<(float4 p, float q) -{ - return int4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline int2 operator<(float p, float2 q) -{ - return int2(p < q.x, p < q.y); -} -static inline int3 operator<(float p, float3 q) -{ - return int3(p < q.x, p < q.y, p < q.z); -} -static inline int4 operator<(float p, float4 q) -{ - return int4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline long2 operator<(double2 p, double2 q) -{ - return long2(p.x < q.x, p.y < q.y); -} -static inline long3 operator<(double3 p, double3 q) -{ - return long3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline long4 operator<(double4 p, double4 q) -{ - return long4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline long2 operator<(double2 p, double q) -{ - return long2(p.x < q, p.y < q); -} -static inline long3 operator<(double3 p, double q) -{ - return long3(p.x < q, p.y < q, p.z < q); -} -static inline long4 operator<(double4 p, double q) -{ - return long4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline long2 operator<(double p, double2 q) -{ - return long2(p < q.x, p < q.y); -} -static inline long3 operator<(double p, double3 q) -{ - return long3(p < q.x, p < q.y, p < q.z); -} -static inline long4 operator<(double p, double4 q) -{ - return long4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline int2 operator<(int2 p, int2 q) -{ - return int2(p.x < q.x, p.y < q.y); -} -static inline int3 operator<(int3 p, int3 q) -{ - return int3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline int4 operator<(int4 p, int4 q) -{ - return int4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline int2 operator<(int2 p, int q) -{ - return int2(p.x < q, p.y < q); -} -static inline int3 operator<(int3 p, int q) -{ - return int3(p.x < q, p.y < q, p.z < q); -} -static inline int4 operator<(int4 p, int q) -{ - return int4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline int2 operator<(int p, int2 q) -{ - return int2(p < q.x, p < q.y); -} -static inline int3 operator<(int p, int3 q) -{ - return int3(p < q.x, p < q.y, p < q.z); -} -static inline int4 operator<(int p, int4 q) -{ - return int4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline int2 operator<(uint2 p, uint2 q) -{ - return int2(p.x < q.x, p.y < q.y); -} -static inline int3 operator<(uint3 p, uint3 q) -{ - return int3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline int4 operator<(uint4 p, uint4 q) -{ - return int4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline int2 operator<(uint2 p, uint q) -{ - return int2(p.x < q, p.y < q); -} -static inline int3 operator<(uint3 p, uint q) -{ - return int3(p.x < q, p.y < q, p.z < q); -} -static inline int4 operator<(uint4 p, uint q) -{ - return int4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline int2 operator<(uint p, uint2 q) -{ - return int2(p < q.x, p < q.y); -} -static inline int3 operator<(uint p, uint3 q) -{ - return int3(p < q.x, p < q.y, p < q.z); -} -static inline int4 operator<(uint p, uint4 q) -{ - return int4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline short2 operator<(short2 p, short2 q) -{ - return short2(p.x < q.x, p.y < q.y); -} -static inline short3 operator<(short3 p, short3 q) -{ - return short3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline short4 operator<(short4 p, short4 q) -{ - return short4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline short2 operator<(short2 p, short q) -{ - return short2(p.x < q, p.y < q); -} -static inline short3 operator<(short3 p, short q) -{ - return short3(p.x < q, p.y < q, p.z < q); -} -static inline short4 operator<(short4 p, short q) -{ - return short4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline short2 operator<(short p, short2 q) -{ - return short2(p < q.x, p < q.y); -} -static inline short3 operator<(short p, short3 q) -{ - return short3(p < q.x, p < q.y, p < q.z); -} -static inline short4 operator<(short p, short4 q) -{ - return short4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline short2 operator<(ushort2 p, ushort2 q) -{ - return short2(p.x < q.x, p.y < q.y); -} -static inline short3 operator<(ushort3 p, ushort3 q) -{ - return short3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline short4 operator<(ushort4 p, ushort4 q) -{ - return short4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline short2 operator<(ushort2 p, ushort q) -{ - return short2(p.x < q, p.y < q); -} -static inline short3 operator<(ushort3 p, ushort q) -{ - return short3(p.x < q, p.y < q, p.z < q); -} -static inline short4 operator<(ushort4 p, ushort q) -{ - return short4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline short2 operator<(ushort p, ushort2 q) -{ - return short2(p < q.x, p < q.y); -} -static inline short3 operator<(ushort p, ushort3 q) -{ - return short3(p < q.x, p < q.y, p < q.z); -} -static inline short4 operator<(ushort p, ushort4 q) -{ - return short4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline long2 operator<(long2 p, long2 q) -{ - return long2(p.x < q.x, p.y < q.y); -} -static inline long3 operator<(long3 p, long3 q) -{ - return long3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline long4 operator<(long4 p, long4 q) -{ - return long4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline long2 operator<(long2 p, long q) -{ - return long2(p.x < q, p.y < q); -} -static inline long3 operator<(long3 p, long q) -{ - return long3(p.x < q, p.y < q, p.z < q); -} -static inline long4 operator<(long4 p, long q) -{ - return long4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline long2 operator<(long p, long2 q) -{ - return long2(p < q.x, p < q.y); -} -static inline long3 operator<(long p, long3 q) -{ - return long3(p < q.x, p < q.y, p < q.z); -} -static inline long4 operator<(long p, long4 q) -{ - return long4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline long2 operator<(ulong2 p, ulong2 q) -{ - return long2(p.x < q.x, p.y < q.y); -} -static inline long3 operator<(ulong3 p, ulong3 q) -{ - return long3(p.x < q.x, p.y < q.y, p.z < q.z); -} -static inline long4 operator<(ulong4 p, ulong4 q) -{ - return long4(p.x < q.x, p.y < q.y, p.z < q.z, p.w < q.w); -} -static inline long2 operator<(ulong2 p, ulong q) -{ - return long2(p.x < q, p.y < q); -} -static inline long3 operator<(ulong3 p, ulong q) -{ - return long3(p.x < q, p.y < q, p.z < q); -} -static inline long4 operator<(ulong4 p, ulong q) -{ - return long4(p.x < q, p.y < q, p.z < q, p.w < q); -} -static inline long2 operator<(ulong p, ulong2 q) -{ - return long2(p < q.x, p < q.y); -} -static inline long3 operator<(ulong p, ulong3 q) -{ - return long3(p < q.x, p < q.y, p < q.z); -} -static inline long4 operator<(ulong p, ulong4 q) -{ - return long4(p < q.x, p < q.y, p < q.z, p < q.w); -} -static inline int2 operator>=(float2 p, float2 q) -{ - return int2(p.x >= q.x, p.y >= q.y); -} -static inline int3 operator>=(float3 p, float3 q) -{ - return int3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline int4 operator>=(float4 p, float4 q) -{ - return int4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline int2 operator>=(float2 p, float q) -{ - return int2(p.x >= q, p.y >= q); -} -static inline int3 operator>=(float3 p, float q) -{ - return int3(p.x >= q, p.y >= q, p.z >= q); -} -static inline int4 operator>=(float4 p, float q) -{ - return int4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline int2 operator>=(float p, float2 q) -{ - return int2(p >= q.x, p >= q.y); -} -static inline int3 operator>=(float p, float3 q) -{ - return int3(p >= q.x, p >= q.y, p >= q.z); -} -static inline int4 operator>=(float p, float4 q) -{ - return int4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline long2 operator>=(double2 p, double2 q) -{ - return long2(p.x >= q.x, p.y >= q.y); -} -static inline long3 operator>=(double3 p, double3 q) -{ - return long3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline long4 operator>=(double4 p, double4 q) -{ - return long4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline long2 operator>=(double2 p, double q) -{ - return long2(p.x >= q, p.y >= q); -} -static inline long3 operator>=(double3 p, double q) -{ - return long3(p.x >= q, p.y >= q, p.z >= q); -} -static inline long4 operator>=(double4 p, double q) -{ - return long4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline long2 operator>=(double p, double2 q) -{ - return long2(p >= q.x, p >= q.y); -} -static inline long3 operator>=(double p, double3 q) -{ - return long3(p >= q.x, p >= q.y, p >= q.z); -} -static inline long4 operator>=(double p, double4 q) -{ - return long4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline int2 operator>=(int2 p, int2 q) -{ - return int2(p.x >= q.x, p.y >= q.y); -} -static inline int3 operator>=(int3 p, int3 q) -{ - return int3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline int4 operator>=(int4 p, int4 q) -{ - return int4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline int2 operator>=(int2 p, int q) -{ - return int2(p.x >= q, p.y >= q); -} -static inline int3 operator>=(int3 p, int q) -{ - return int3(p.x >= q, p.y >= q, p.z >= q); -} -static inline int4 operator>=(int4 p, int q) -{ - return int4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline int2 operator>=(int p, int2 q) -{ - return int2(p >= q.x, p >= q.y); -} -static inline int3 operator>=(int p, int3 q) -{ - return int3(p >= q.x, p >= q.y, p >= q.z); -} -static inline int4 operator>=(int p, int4 q) -{ - return int4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline int2 operator>=(uint2 p, uint2 q) -{ - return int2(p.x >= q.x, p.y >= q.y); -} -static inline int3 operator>=(uint3 p, uint3 q) -{ - return int3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline int4 operator>=(uint4 p, uint4 q) -{ - return int4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline int2 operator>=(uint2 p, uint q) -{ - return int2(p.x >= q, p.y >= q); -} -static inline int3 operator>=(uint3 p, uint q) -{ - return int3(p.x >= q, p.y >= q, p.z >= q); -} -static inline int4 operator>=(uint4 p, uint q) -{ - return int4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline int2 operator>=(uint p, uint2 q) -{ - return int2(p >= q.x, p >= q.y); -} -static inline int3 operator>=(uint p, uint3 q) -{ - return int3(p >= q.x, p >= q.y, p >= q.z); -} -static inline int4 operator>=(uint p, uint4 q) -{ - return int4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline short2 operator>=(short2 p, short2 q) -{ - return short2(p.x >= q.x, p.y >= q.y); -} -static inline short3 operator>=(short3 p, short3 q) -{ - return short3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline short4 operator>=(short4 p, short4 q) -{ - return short4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline short2 operator>=(short2 p, short q) -{ - return short2(p.x >= q, p.y >= q); -} -static inline short3 operator>=(short3 p, short q) -{ - return short3(p.x >= q, p.y >= q, p.z >= q); -} -static inline short4 operator>=(short4 p, short q) -{ - return short4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline short2 operator>=(short p, short2 q) -{ - return short2(p >= q.x, p >= q.y); -} -static inline short3 operator>=(short p, short3 q) -{ - return short3(p >= q.x, p >= q.y, p >= q.z); -} -static inline short4 operator>=(short p, short4 q) -{ - return short4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline short2 operator>=(ushort2 p, ushort2 q) -{ - return short2(p.x >= q.x, p.y >= q.y); -} -static inline short3 operator>=(ushort3 p, ushort3 q) -{ - return short3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline short4 operator>=(ushort4 p, ushort4 q) -{ - return short4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline short2 operator>=(ushort2 p, ushort q) -{ - return short2(p.x >= q, p.y >= q); -} -static inline short3 operator>=(ushort3 p, ushort q) -{ - return short3(p.x >= q, p.y >= q, p.z >= q); -} -static inline short4 operator>=(ushort4 p, ushort q) -{ - return short4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline short2 operator>=(ushort p, ushort2 q) -{ - return short2(p >= q.x, p >= q.y); -} -static inline short3 operator>=(ushort p, ushort3 q) -{ - return short3(p >= q.x, p >= q.y, p >= q.z); -} -static inline short4 operator>=(ushort p, ushort4 q) -{ - return short4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline long2 operator>=(long2 p, long2 q) -{ - return long2(p.x >= q.x, p.y >= q.y); -} -static inline long3 operator>=(long3 p, long3 q) -{ - return long3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline long4 operator>=(long4 p, long4 q) -{ - return long4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline long2 operator>=(long2 p, long q) -{ - return long2(p.x >= q, p.y >= q); -} -static inline long3 operator>=(long3 p, long q) -{ - return long3(p.x >= q, p.y >= q, p.z >= q); -} -static inline long4 operator>=(long4 p, long q) -{ - return long4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline long2 operator>=(long p, long2 q) -{ - return long2(p >= q.x, p >= q.y); -} -static inline long3 operator>=(long p, long3 q) -{ - return long3(p >= q.x, p >= q.y, p >= q.z); -} -static inline long4 operator>=(long p, long4 q) -{ - return long4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline long2 operator>=(ulong2 p, ulong2 q) -{ - return long2(p.x >= q.x, p.y >= q.y); -} -static inline long3 operator>=(ulong3 p, ulong3 q) -{ - return long3(p.x >= q.x, p.y >= q.y, p.z >= q.z); -} -static inline long4 operator>=(ulong4 p, ulong4 q) -{ - return long4(p.x >= q.x, p.y >= q.y, p.z >= q.z, p.w >= q.w); -} -static inline long2 operator>=(ulong2 p, ulong q) -{ - return long2(p.x >= q, p.y >= q); -} -static inline long3 operator>=(ulong3 p, ulong q) -{ - return long3(p.x >= q, p.y >= q, p.z >= q); -} -static inline long4 operator>=(ulong4 p, ulong q) -{ - return long4(p.x >= q, p.y >= q, p.z >= q, p.w >= q); -} -static inline long2 operator>=(ulong p, ulong2 q) -{ - return long2(p >= q.x, p >= q.y); -} -static inline long3 operator>=(ulong p, ulong3 q) -{ - return long3(p >= q.x, p >= q.y, p >= q.z); -} -static inline long4 operator>=(ulong p, ulong4 q) -{ - return long4(p >= q.x, p >= q.y, p >= q.z, p >= q.w); -} -static inline int2 operator<=(float2 p, float2 q) -{ - return int2(p.x <= q.x, p.y <= q.y); -} -static inline int3 operator<=(float3 p, float3 q) -{ - return int3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline int4 operator<=(float4 p, float4 q) -{ - return int4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline int2 operator<=(float2 p, float q) -{ - return int2(p.x <= q, p.y <= q); -} -static inline int3 operator<=(float3 p, float q) -{ - return int3(p.x <= q, p.y <= q, p.z <= q); -} -static inline int4 operator<=(float4 p, float q) -{ - return int4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline int2 operator<=(float p, float2 q) -{ - return int2(p <= q.x, p <= q.y); -} -static inline int3 operator<=(float p, float3 q) -{ - return int3(p <= q.x, p <= q.y, p <= q.z); -} -static inline int4 operator<=(float p, float4 q) -{ - return int4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline long2 operator<=(double2 p, double2 q) -{ - return long2(p.x <= q.x, p.y <= q.y); -} -static inline long3 operator<=(double3 p, double3 q) -{ - return long3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline long4 operator<=(double4 p, double4 q) -{ - return long4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline long2 operator<=(double2 p, double q) -{ - return long2(p.x <= q, p.y <= q); -} -static inline long3 operator<=(double3 p, double q) -{ - return long3(p.x <= q, p.y <= q, p.z <= q); -} -static inline long4 operator<=(double4 p, double q) -{ - return long4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline long2 operator<=(double p, double2 q) -{ - return long2(p <= q.x, p <= q.y); -} -static inline long3 operator<=(double p, double3 q) -{ - return long3(p <= q.x, p <= q.y, p <= q.z); -} -static inline long4 operator<=(double p, double4 q) -{ - return long4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline int2 operator<=(int2 p, int2 q) -{ - return int2(p.x <= q.x, p.y <= q.y); -} -static inline int3 operator<=(int3 p, int3 q) -{ - return int3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline int4 operator<=(int4 p, int4 q) -{ - return int4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline int2 operator<=(int2 p, int q) -{ - return int2(p.x <= q, p.y <= q); -} -static inline int3 operator<=(int3 p, int q) -{ - return int3(p.x <= q, p.y <= q, p.z <= q); -} -static inline int4 operator<=(int4 p, int q) -{ - return int4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline int2 operator<=(int p, int2 q) -{ - return int2(p <= q.x, p <= q.y); -} -static inline int3 operator<=(int p, int3 q) -{ - return int3(p <= q.x, p <= q.y, p <= q.z); -} -static inline int4 operator<=(int p, int4 q) -{ - return int4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline int2 operator<=(uint2 p, uint2 q) -{ - return int2(p.x <= q.x, p.y <= q.y); -} -static inline int3 operator<=(uint3 p, uint3 q) -{ - return int3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline int4 operator<=(uint4 p, uint4 q) -{ - return int4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline int2 operator<=(uint2 p, uint q) -{ - return int2(p.x <= q, p.y <= q); -} -static inline int3 operator<=(uint3 p, uint q) -{ - return int3(p.x <= q, p.y <= q, p.z <= q); -} -static inline int4 operator<=(uint4 p, uint q) -{ - return int4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline int2 operator<=(uint p, uint2 q) -{ - return int2(p <= q.x, p <= q.y); -} -static inline int3 operator<=(uint p, uint3 q) -{ - return int3(p <= q.x, p <= q.y, p <= q.z); -} -static inline int4 operator<=(uint p, uint4 q) -{ - return int4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline short2 operator<=(short2 p, short2 q) -{ - return short2(p.x <= q.x, p.y <= q.y); -} -static inline short3 operator<=(short3 p, short3 q) -{ - return short3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline short4 operator<=(short4 p, short4 q) -{ - return short4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline short2 operator<=(short2 p, short q) -{ - return short2(p.x <= q, p.y <= q); -} -static inline short3 operator<=(short3 p, short q) -{ - return short3(p.x <= q, p.y <= q, p.z <= q); -} -static inline short4 operator<=(short4 p, short q) -{ - return short4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline short2 operator<=(short p, short2 q) -{ - return short2(p <= q.x, p <= q.y); -} -static inline short3 operator<=(short p, short3 q) -{ - return short3(p <= q.x, p <= q.y, p <= q.z); -} -static inline short4 operator<=(short p, short4 q) -{ - return short4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline short2 operator<=(ushort2 p, ushort2 q) -{ - return short2(p.x <= q.x, p.y <= q.y); -} -static inline short3 operator<=(ushort3 p, ushort3 q) -{ - return short3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline short4 operator<=(ushort4 p, ushort4 q) -{ - return short4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline short2 operator<=(ushort2 p, ushort q) -{ - return short2(p.x <= q, p.y <= q); -} -static inline short3 operator<=(ushort3 p, ushort q) -{ - return short3(p.x <= q, p.y <= q, p.z <= q); -} -static inline short4 operator<=(ushort4 p, ushort q) -{ - return short4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline short2 operator<=(ushort p, ushort2 q) -{ - return short2(p <= q.x, p <= q.y); -} -static inline short3 operator<=(ushort p, ushort3 q) -{ - return short3(p <= q.x, p <= q.y, p <= q.z); -} -static inline short4 operator<=(ushort p, ushort4 q) -{ - return short4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline long2 operator<=(long2 p, long2 q) -{ - return long2(p.x <= q.x, p.y <= q.y); -} -static inline long3 operator<=(long3 p, long3 q) -{ - return long3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline long4 operator<=(long4 p, long4 q) -{ - return long4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline long2 operator<=(long2 p, long q) -{ - return long2(p.x <= q, p.y <= q); -} -static inline long3 operator<=(long3 p, long q) -{ - return long3(p.x <= q, p.y <= q, p.z <= q); -} -static inline long4 operator<=(long4 p, long q) -{ - return long4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline long2 operator<=(long p, long2 q) -{ - return long2(p <= q.x, p <= q.y); -} -static inline long3 operator<=(long p, long3 q) -{ - return long3(p <= q.x, p <= q.y, p <= q.z); -} -static inline long4 operator<=(long p, long4 q) -{ - return long4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline long2 operator<=(ulong2 p, ulong2 q) -{ - return long2(p.x <= q.x, p.y <= q.y); -} -static inline long3 operator<=(ulong3 p, ulong3 q) -{ - return long3(p.x <= q.x, p.y <= q.y, p.z <= q.z); -} -static inline long4 operator<=(ulong4 p, ulong4 q) -{ - return long4(p.x <= q.x, p.y <= q.y, p.z <= q.z, p.w <= q.w); -} -static inline long2 operator<=(ulong2 p, ulong q) -{ - return long2(p.x <= q, p.y <= q); -} -static inline long3 operator<=(ulong3 p, ulong q) -{ - return long3(p.x <= q, p.y <= q, p.z <= q); -} -static inline long4 operator<=(ulong4 p, ulong q) -{ - return long4(p.x <= q, p.y <= q, p.z <= q, p.w <= q); -} -static inline long2 operator<=(ulong p, ulong2 q) -{ - return long2(p <= q.x, p <= q.y); -} -static inline long3 operator<=(ulong p, ulong3 q) -{ - return long3(p <= q.x, p <= q.y, p <= q.z); -} -static inline long4 operator<=(ulong p, ulong4 q) -{ - return long4(p <= q.x, p <= q.y, p <= q.z, p <= q.w); -} -static inline int2 operator&&(float2 p, float2 q) -{ - return int2(p.x && q.x, p.y && q.y); -} -static inline int3 operator&&(float3 p, float3 q) -{ - return int3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline int4 operator&&(float4 p, float4 q) -{ - return int4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline int2 operator&&(float2 p, float q) -{ - return int2(p.x && q, p.y && q); -} -static inline int3 operator&&(float3 p, float q) -{ - return int3(p.x && q, p.y && q, p.z && q); -} -static inline int4 operator&&(float4 p, float q) -{ - return int4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline int2 operator&&(float p, float2 q) -{ - return int2(p && q.x, p && q.y); -} -static inline int3 operator&&(float p, float3 q) -{ - return int3(p && q.x, p && q.y, p && q.z); -} -static inline int4 operator&&(float p, float4 q) -{ - return int4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline long2 operator&&(double2 p, double2 q) -{ - return long2(p.x && q.x, p.y && q.y); -} -static inline long3 operator&&(double3 p, double3 q) -{ - return long3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline long4 operator&&(double4 p, double4 q) -{ - return long4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline long2 operator&&(double2 p, double q) -{ - return long2(p.x && q, p.y && q); -} -static inline long3 operator&&(double3 p, double q) -{ - return long3(p.x && q, p.y && q, p.z && q); -} -static inline long4 operator&&(double4 p, double q) -{ - return long4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline long2 operator&&(double p, double2 q) -{ - return long2(p && q.x, p && q.y); -} -static inline long3 operator&&(double p, double3 q) -{ - return long3(p && q.x, p && q.y, p && q.z); -} -static inline long4 operator&&(double p, double4 q) -{ - return long4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline int2 operator&&(int2 p, int2 q) -{ - return int2(p.x && q.x, p.y && q.y); -} -static inline int3 operator&&(int3 p, int3 q) -{ - return int3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline int4 operator&&(int4 p, int4 q) -{ - return int4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline int2 operator&&(int2 p, int q) -{ - return int2(p.x && q, p.y && q); -} -static inline int3 operator&&(int3 p, int q) -{ - return int3(p.x && q, p.y && q, p.z && q); -} -static inline int4 operator&&(int4 p, int q) -{ - return int4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline int2 operator&&(int p, int2 q) -{ - return int2(p && q.x, p && q.y); -} -static inline int3 operator&&(int p, int3 q) -{ - return int3(p && q.x, p && q.y, p && q.z); -} -static inline int4 operator&&(int p, int4 q) -{ - return int4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline int2 operator&&(uint2 p, uint2 q) -{ - return int2(p.x && q.x, p.y && q.y); -} -static inline int3 operator&&(uint3 p, uint3 q) -{ - return int3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline int4 operator&&(uint4 p, uint4 q) -{ - return int4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline int2 operator&&(uint2 p, uint q) -{ - return int2(p.x && q, p.y && q); -} -static inline int3 operator&&(uint3 p, uint q) -{ - return int3(p.x && q, p.y && q, p.z && q); -} -static inline int4 operator&&(uint4 p, uint q) -{ - return int4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline int2 operator&&(uint p, uint2 q) -{ - return int2(p && q.x, p && q.y); -} -static inline int3 operator&&(uint p, uint3 q) -{ - return int3(p && q.x, p && q.y, p && q.z); -} -static inline int4 operator&&(uint p, uint4 q) -{ - return int4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline short2 operator&&(short2 p, short2 q) -{ - return short2(p.x && q.x, p.y && q.y); -} -static inline short3 operator&&(short3 p, short3 q) -{ - return short3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline short4 operator&&(short4 p, short4 q) -{ - return short4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline short2 operator&&(short2 p, short q) -{ - return short2(p.x && q, p.y && q); -} -static inline short3 operator&&(short3 p, short q) -{ - return short3(p.x && q, p.y && q, p.z && q); -} -static inline short4 operator&&(short4 p, short q) -{ - return short4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline short2 operator&&(short p, short2 q) -{ - return short2(p && q.x, p && q.y); -} -static inline short3 operator&&(short p, short3 q) -{ - return short3(p && q.x, p && q.y, p && q.z); -} -static inline short4 operator&&(short p, short4 q) -{ - return short4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline short2 operator&&(ushort2 p, ushort2 q) -{ - return short2(p.x && q.x, p.y && q.y); -} -static inline short3 operator&&(ushort3 p, ushort3 q) -{ - return short3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline short4 operator&&(ushort4 p, ushort4 q) -{ - return short4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline short2 operator&&(ushort2 p, ushort q) -{ - return short2(p.x && q, p.y && q); -} -static inline short3 operator&&(ushort3 p, ushort q) -{ - return short3(p.x && q, p.y && q, p.z && q); -} -static inline short4 operator&&(ushort4 p, ushort q) -{ - return short4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline short2 operator&&(ushort p, ushort2 q) -{ - return short2(p && q.x, p && q.y); -} -static inline short3 operator&&(ushort p, ushort3 q) -{ - return short3(p && q.x, p && q.y, p && q.z); -} -static inline short4 operator&&(ushort p, ushort4 q) -{ - return short4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline long2 operator&&(long2 p, long2 q) -{ - return long2(p.x && q.x, p.y && q.y); -} -static inline long3 operator&&(long3 p, long3 q) -{ - return long3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline long4 operator&&(long4 p, long4 q) -{ - return long4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline long2 operator&&(long2 p, long q) -{ - return long2(p.x && q, p.y && q); -} -static inline long3 operator&&(long3 p, long q) -{ - return long3(p.x && q, p.y && q, p.z && q); -} -static inline long4 operator&&(long4 p, long q) -{ - return long4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline long2 operator&&(long p, long2 q) -{ - return long2(p && q.x, p && q.y); -} -static inline long3 operator&&(long p, long3 q) -{ - return long3(p && q.x, p && q.y, p && q.z); -} -static inline long4 operator&&(long p, long4 q) -{ - return long4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline long2 operator&&(ulong2 p, ulong2 q) -{ - return long2(p.x && q.x, p.y && q.y); -} -static inline long3 operator&&(ulong3 p, ulong3 q) -{ - return long3(p.x && q.x, p.y && q.y, p.z && q.z); -} -static inline long4 operator&&(ulong4 p, ulong4 q) -{ - return long4(p.x && q.x, p.y && q.y, p.z && q.z, p.w && q.w); -} -static inline long2 operator&&(ulong2 p, ulong q) -{ - return long2(p.x && q, p.y && q); -} -static inline long3 operator&&(ulong3 p, ulong q) -{ - return long3(p.x && q, p.y && q, p.z && q); -} -static inline long4 operator&&(ulong4 p, ulong q) -{ - return long4(p.x && q, p.y && q, p.z && q, p.w && q); -} -static inline long2 operator&&(ulong p, ulong2 q) -{ - return long2(p && q.x, p && q.y); -} -static inline long3 operator&&(ulong p, ulong3 q) -{ - return long3(p && q.x, p && q.y, p && q.z); -} -static inline long4 operator&&(ulong p, ulong4 q) -{ - return long4(p && q.x, p && q.y, p && q.z, p && q.w); -} -static inline int2 operator||(float2 p, float2 q) -{ - return int2(p.x || q.x, p.y || q.y); -} -static inline int3 operator||(float3 p, float3 q) -{ - return int3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline int4 operator||(float4 p, float4 q) -{ - return int4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline int2 operator||(float2 p, float q) -{ - return int2(p.x || q, p.y || q); -} -static inline int3 operator||(float3 p, float q) -{ - return int3(p.x || q, p.y || q, p.z || q); -} -static inline int4 operator||(float4 p, float q) -{ - return int4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline int2 operator||(float p, float2 q) -{ - return int2(p || q.x, p || q.y); -} -static inline int3 operator||(float p, float3 q) -{ - return int3(p || q.x, p || q.y, p || q.z); -} -static inline int4 operator||(float p, float4 q) -{ - return int4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline long2 operator||(double2 p, double2 q) -{ - return long2(p.x || q.x, p.y || q.y); -} -static inline long3 operator||(double3 p, double3 q) -{ - return long3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline long4 operator||(double4 p, double4 q) -{ - return long4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline long2 operator||(double2 p, double q) -{ - return long2(p.x || q, p.y || q); -} -static inline long3 operator||(double3 p, double q) -{ - return long3(p.x || q, p.y || q, p.z || q); -} -static inline long4 operator||(double4 p, double q) -{ - return long4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline long2 operator||(double p, double2 q) -{ - return long2(p || q.x, p || q.y); -} -static inline long3 operator||(double p, double3 q) -{ - return long3(p || q.x, p || q.y, p || q.z); -} -static inline long4 operator||(double p, double4 q) -{ - return long4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline int2 operator||(int2 p, int2 q) -{ - return int2(p.x || q.x, p.y || q.y); -} -static inline int3 operator||(int3 p, int3 q) -{ - return int3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline int4 operator||(int4 p, int4 q) -{ - return int4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline int2 operator||(int2 p, int q) -{ - return int2(p.x || q, p.y || q); -} -static inline int3 operator||(int3 p, int q) -{ - return int3(p.x || q, p.y || q, p.z || q); -} -static inline int4 operator||(int4 p, int q) -{ - return int4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline int2 operator||(int p, int2 q) -{ - return int2(p || q.x, p || q.y); -} -static inline int3 operator||(int p, int3 q) -{ - return int3(p || q.x, p || q.y, p || q.z); -} -static inline int4 operator||(int p, int4 q) -{ - return int4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline int2 operator||(uint2 p, uint2 q) -{ - return int2(p.x || q.x, p.y || q.y); -} -static inline int3 operator||(uint3 p, uint3 q) -{ - return int3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline int4 operator||(uint4 p, uint4 q) -{ - return int4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline int2 operator||(uint2 p, uint q) -{ - return int2(p.x || q, p.y || q); -} -static inline int3 operator||(uint3 p, uint q) -{ - return int3(p.x || q, p.y || q, p.z || q); -} -static inline int4 operator||(uint4 p, uint q) -{ - return int4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline int2 operator||(uint p, uint2 q) -{ - return int2(p || q.x, p || q.y); -} -static inline int3 operator||(uint p, uint3 q) -{ - return int3(p || q.x, p || q.y, p || q.z); -} -static inline int4 operator||(uint p, uint4 q) -{ - return int4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline short2 operator||(short2 p, short2 q) -{ - return short2(p.x || q.x, p.y || q.y); -} -static inline short3 operator||(short3 p, short3 q) -{ - return short3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline short4 operator||(short4 p, short4 q) -{ - return short4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline short2 operator||(short2 p, short q) -{ - return short2(p.x || q, p.y || q); -} -static inline short3 operator||(short3 p, short q) -{ - return short3(p.x || q, p.y || q, p.z || q); -} -static inline short4 operator||(short4 p, short q) -{ - return short4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline short2 operator||(short p, short2 q) -{ - return short2(p || q.x, p || q.y); -} -static inline short3 operator||(short p, short3 q) -{ - return short3(p || q.x, p || q.y, p || q.z); -} -static inline short4 operator||(short p, short4 q) -{ - return short4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline short2 operator||(ushort2 p, ushort2 q) -{ - return short2(p.x || q.x, p.y || q.y); -} -static inline short3 operator||(ushort3 p, ushort3 q) -{ - return short3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline short4 operator||(ushort4 p, ushort4 q) -{ - return short4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline short2 operator||(ushort2 p, ushort q) -{ - return short2(p.x || q, p.y || q); -} -static inline short3 operator||(ushort3 p, ushort q) -{ - return short3(p.x || q, p.y || q, p.z || q); -} -static inline short4 operator||(ushort4 p, ushort q) -{ - return short4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline short2 operator||(ushort p, ushort2 q) -{ - return short2(p || q.x, p || q.y); -} -static inline short3 operator||(ushort p, ushort3 q) -{ - return short3(p || q.x, p || q.y, p || q.z); -} -static inline short4 operator||(ushort p, ushort4 q) -{ - return short4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline long2 operator||(long2 p, long2 q) -{ - return long2(p.x || q.x, p.y || q.y); -} -static inline long3 operator||(long3 p, long3 q) -{ - return long3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline long4 operator||(long4 p, long4 q) -{ - return long4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline long2 operator||(long2 p, long q) -{ - return long2(p.x || q, p.y || q); -} -static inline long3 operator||(long3 p, long q) -{ - return long3(p.x || q, p.y || q, p.z || q); -} -static inline long4 operator||(long4 p, long q) -{ - return long4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline long2 operator||(long p, long2 q) -{ - return long2(p || q.x, p || q.y); -} -static inline long3 operator||(long p, long3 q) -{ - return long3(p || q.x, p || q.y, p || q.z); -} -static inline long4 operator||(long p, long4 q) -{ - return long4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline long2 operator||(ulong2 p, ulong2 q) -{ - return long2(p.x || q.x, p.y || q.y); -} -static inline long3 operator||(ulong3 p, ulong3 q) -{ - return long3(p.x || q.x, p.y || q.y, p.z || q.z); -} -static inline long4 operator||(ulong4 p, ulong4 q) -{ - return long4(p.x || q.x, p.y || q.y, p.z || q.z, p.w || q.w); -} -static inline long2 operator||(ulong2 p, ulong q) -{ - return long2(p.x || q, p.y || q); -} -static inline long3 operator||(ulong3 p, ulong q) -{ - return long3(p.x || q, p.y || q, p.z || q); -} -static inline long4 operator||(ulong4 p, ulong q) -{ - return long4(p.x || q, p.y || q, p.z || q, p.w || q); -} -static inline long2 operator||(ulong p, ulong2 q) -{ - return long2(p || q.x, p || q.y); -} -static inline long3 operator||(ulong p, ulong3 q) -{ - return long3(p || q.x, p || q.y, p || q.z); -} -static inline long4 operator||(ulong p, ulong4 q) -{ - return long4(p || q.x, p || q.y, p || q.z, p || q.w); -} -static inline float2 convert_float2(float2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(float3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(float4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(double2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(double3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(double4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(int2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(int3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(int4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(uint2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(uint3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(uint4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(short2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(short3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(short4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(ushort2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(ushort3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(ushort4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(long2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(long3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(long4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline float2 convert_float2(ulong2 p) -{ - return float2((float)p.x, (float)p.y); -} -static inline float3 convert_float3(ulong3 p) -{ - return float3((float)p.x, (float)p.y, (float)p.z); -} -static inline float4 convert_float4(ulong4 p) -{ - return float4((float)p.x, (float)p.y, (float)p.z, (float)p.w); -} -static inline double2 convert_double2(float2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(float3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(float4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(double2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(double3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(double4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(int2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(int3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(int4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(uint2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(uint3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(uint4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(short2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(short3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(short4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(ushort2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(ushort3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(ushort4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(long2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(long3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(long4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline double2 convert_double2(ulong2 p) -{ - return double2((double)p.x, (double)p.y); -} -static inline double3 convert_double3(ulong3 p) -{ - return double3((double)p.x, (double)p.y, (double)p.z); -} -static inline double4 convert_double4(ulong4 p) -{ - return double4((double)p.x, (double)p.y, (double)p.z, (double)p.w); -} -static inline int2 convert_int2(float2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(float3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(float4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(double2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(double3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(double4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(int2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(int3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(int4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(uint2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(uint3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(uint4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(short2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(short3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(short4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(ushort2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(ushort3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(ushort4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(long2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(long3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(long4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline int2 convert_int2(ulong2 p) -{ - return int2((int)p.x, (int)p.y); -} -static inline int3 convert_int3(ulong3 p) -{ - return int3((int)p.x, (int)p.y, (int)p.z); -} -static inline int4 convert_int4(ulong4 p) -{ - return int4((int)p.x, (int)p.y, (int)p.z, (int)p.w); -} -static inline uint2 convert_uint2(float2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(float3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(float4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(double2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(double3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(double4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(int2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(int3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(int4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(uint2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(uint3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(uint4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(short2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(short3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(short4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(ushort2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(ushort3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(ushort4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(long2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(long3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(long4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline uint2 convert_uint2(ulong2 p) -{ - return uint2((uint) p.x, (uint) p.y); -} -static inline uint3 convert_uint3(ulong3 p) -{ - return uint3((uint) p.x, (uint) p.y, (uint) p.z); -} -static inline uint4 convert_uint4(ulong4 p) -{ - return uint4((uint) p.x, (uint) p.y, (uint) p.z, (uint) p.w); -} -static inline short2 convert_short2(float2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(float3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(float4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(double2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(double3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(double4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(int2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(int3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(int4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(uint2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(uint3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(uint4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(short2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(short3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(short4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(ushort2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(ushort3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(ushort4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(long2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(long3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(long4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline short2 convert_short2(ulong2 p) -{ - return short2((short)p.x, (short)p.y); -} -static inline short3 convert_short3(ulong3 p) -{ - return short3((short)p.x, (short)p.y, (short)p.z); -} -static inline short4 convert_short4(ulong4 p) -{ - return short4((short)p.x, (short)p.y, (short)p.z, (short)p.w); -} -static inline ushort2 convert_ushort2(float2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(float3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(float4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(double2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(double3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(double4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(int2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(int3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(int4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(uint2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(uint3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(uint4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(short2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(short3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(short4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(ushort2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(ushort3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(ushort4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(long2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(long3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(long4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline ushort2 convert_ushort2(ulong2 p) -{ - return ushort2((ushort) p.x, (ushort) p.y); -} -static inline ushort3 convert_ushort3(ulong3 p) -{ - return ushort3((ushort) p.x, (ushort) p.y, (ushort) p.z); -} -static inline ushort4 convert_ushort4(ulong4 p) -{ - return ushort4((ushort) p.x, (ushort) p.y, (ushort) p.z, (ushort) p.w); -} -static inline long2 convert_long2(float2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(float3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(float4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(double2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(double3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(double4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(int2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(int3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(int4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(uint2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(uint3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(uint4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(short2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(short3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(short4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(ushort2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(ushort3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(ushort4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(long2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(long3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(long4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline long2 convert_long2(ulong2 p) -{ - return long2((long)p.x, (long)p.y); -} -static inline long3 convert_long3(ulong3 p) -{ - return long3((long)p.x, (long)p.y, (long)p.z); -} -static inline long4 convert_long4(ulong4 p) -{ - return long4((long)p.x, (long)p.y, (long)p.z, (long)p.w); -} -static inline ulong2 convert_ulong2(float2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(float3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(float4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(double2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(double3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(double4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(int2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(int3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(int4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(uint2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(uint3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(uint4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(short2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(short3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(short4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(ushort2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(ushort3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(ushort4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(long2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(long3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(long4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline ulong2 convert_ulong2(ulong2 p) -{ - return ulong2((ulong) p.x, (ulong) p.y); -} -static inline ulong3 convert_ulong3(ulong3 p) -{ - return ulong3((ulong) p.x, (ulong) p.y, (ulong) p.z); -} -static inline ulong4 convert_ulong4(ulong4 p) -{ - return ulong4((ulong) p.x, (ulong) p.y, (ulong) p.z, (ulong) p.w); -} -static inline float2 as_float2(float2 inp) -{ - float2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline int2 as_int2(float2 inp) -{ - int2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline uint2 as_uint2(float2 inp) -{ - uint2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline double2 as_double2(double2 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(double2 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(double2 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline float2 as_float2(int2 inp) -{ - float2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline int2 as_int2(int2 inp) -{ - int2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline uint2 as_uint2(int2 inp) -{ - uint2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline float2 as_float2(uint2 inp) -{ - float2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline int2 as_int2(uint2 inp) -{ - int2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline uint2 as_uint2(uint2 inp) -{ - uint2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline short2 as_short2(short2 inp) -{ - short2 val1; - memcpy(&val1, &inp, 4); - return val1; -} -static inline ushort2 as_ushort2(short2 inp) -{ - ushort2 val1; - memcpy(&val1, &inp, 4); - return val1; -} -static inline short2 as_short2(ushort2 inp) -{ - short2 val1; - memcpy(&val1, &inp, 4); - return val1; -} -static inline ushort2 as_ushort2(ushort2 inp) -{ - ushort2 val1; - memcpy(&val1, &inp, 4); - return val1; -} -static inline double2 as_double2(long2 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(long2 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(long2 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline double2 as_double2(ulong2 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(ulong2 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(ulong2 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline short4 as_short4(float2 inp) -{ - short4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline ushort4 as_ushort4(float2 inp) -{ - ushort4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline float4 as_float4(double2 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(double2 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(double2 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline short4 as_short4(int2 inp) -{ - short4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline ushort4 as_ushort4(int2 inp) -{ - ushort4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline short4 as_short4(uint2 inp) -{ - short4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline ushort4 as_ushort4(uint2 inp) -{ - ushort4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline float4 as_float4(long2 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(long2 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(long2 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline float4 as_float4(ulong2 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(ulong2 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(ulong2 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline float3 as_float3(float3 inp) -{ - float3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline int3 as_int3(float3 inp) -{ - int3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline uint3 as_uint3(float3 inp) -{ - uint3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline double3 as_double3(double3 inp) -{ - double3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline long3 as_long3(double3 inp) -{ - long3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline ulong3 as_ulong3(double3 inp) -{ - ulong3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline float3 as_float3(int3 inp) -{ - float3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline int3 as_int3(int3 inp) -{ - int3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline uint3 as_uint3(int3 inp) -{ - uint3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline float3 as_float3(uint3 inp) -{ - float3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline int3 as_int3(uint3 inp) -{ - int3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline uint3 as_uint3(uint3 inp) -{ - uint3 val1; - memcpy(&val1, &inp, 12); - return val1; -} -static inline short3 as_short3(short3 inp) -{ - short3 val1; - memcpy(&val1, &inp, 6); - return val1; -} -static inline ushort3 as_ushort3(short3 inp) -{ - ushort3 val1; - memcpy(&val1, &inp, 6); - return val1; -} -static inline short3 as_short3(ushort3 inp) -{ - short3 val1; - memcpy(&val1, &inp, 6); - return val1; -} -static inline ushort3 as_ushort3(ushort3 inp) -{ - ushort3 val1; - memcpy(&val1, &inp, 6); - return val1; -} -static inline double3 as_double3(long3 inp) -{ - double3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline long3 as_long3(long3 inp) -{ - long3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline ulong3 as_ulong3(long3 inp) -{ - ulong3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline double3 as_double3(ulong3 inp) -{ - double3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline long3 as_long3(ulong3 inp) -{ - long3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline ulong3 as_ulong3(ulong3 inp) -{ - ulong3 val1; - memcpy(&val1, &inp, 24); - return val1; -} -static inline double2 as_double2(float4 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(float4 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(float4 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline double2 as_double2(int4 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(int4 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(int4 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline double2 as_double2(uint4 inp) -{ - double2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline long2 as_long2(uint4 inp) -{ - long2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline ulong2 as_ulong2(uint4 inp) -{ - ulong2 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline float2 as_float2(short4 inp) -{ - float2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline int2 as_int2(short4 inp) -{ - int2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline uint2 as_uint2(short4 inp) -{ - uint2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline float2 as_float2(ushort4 inp) -{ - float2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline int2 as_int2(ushort4 inp) -{ - int2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline uint2 as_uint2(ushort4 inp) -{ - uint2 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline float4 as_float4(float4 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(float4 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(float4 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline double4 as_double4(double4 inp) -{ - double4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline long4 as_long4(double4 inp) -{ - long4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline ulong4 as_ulong4(double4 inp) -{ - ulong4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline float4 as_float4(int4 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(int4 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(int4 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline float4 as_float4(uint4 inp) -{ - float4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline int4 as_int4(uint4 inp) -{ - int4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline uint4 as_uint4(uint4 inp) -{ - uint4 val1; - memcpy(&val1, &inp, 16); - return val1; -} -static inline short4 as_short4(short4 inp) -{ - short4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline ushort4 as_ushort4(short4 inp) -{ - ushort4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline short4 as_short4(ushort4 inp) -{ - short4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline ushort4 as_ushort4(ushort4 inp) -{ - ushort4 val1; - memcpy(&val1, &inp, 8); - return val1; -} -static inline double4 as_double4(long4 inp) -{ - double4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline long4 as_long4(long4 inp) -{ - long4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline ulong4 as_ulong4(long4 inp) -{ - ulong4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline double4 as_double4(ulong4 inp) -{ - double4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline long4 as_long4(ulong4 inp) -{ - long4 val1; - memcpy(&val1, &inp, 32); - return val1; -} -static inline ulong4 as_ulong4(ulong4 inp) -{ - ulong4 val1; - memcpy(&val1, &inp, 32); - return val1; -} - -#define xx xx() -#define xy xy() -#define xz xz() -#define xw xw() -#define yx yx() -#define yy yy() -#define yz yz() -#define yw yw() -#define zx zx() -#define zy zy() -#define zz zz() -#define zw zw() -#define wx wx() -#define wy wy() -#define wz wz() -#define ww ww() -#define xxx xxx() -#define xxy xxy() -#define xxz xxz() -#define xxw xxw() -#define xyx xyx() -#define xyy xyy() -#define xyz xyz() -#define xyw xyw() -#define xzx xzx() -#define xzy xzy() -#define xzz xzz() -#define xzw xzw() -#define xwx xwx() -#define xwy xwy() -#define xwz xwz() -#define xww xww() -#define yxx yxx() -#define yxy yxy() -#define yxz yxz() -#define yxw yxw() -#define yyx yyx() -#define yyy yyy() -#define yyz yyz() -#define yyw yyw() -#define yzx yzx() -#define yzy yzy() -#define yzz yzz() -#define yzw yzw() -#define ywx ywx() -#define ywy ywy() -#define ywz ywz() -#define yww yww() -#define zxx zxx() -#define zxy zxy() -#define zxz zxz() -#define zxw zxw() -#define zyx zyx() -#define zyy zyy() -#define zyz zyz() -#define zyw zyw() -#define zzx zzx() -#define zzy zzy() -#define zzz zzz() -#define zzw zzw() -#define zwx zwx() -#define zwy zwy() -#define zwz zwz() -#define zww zww() -#define wxx wxx() -#define wxy wxy() -#define wxz wxz() -#define wxw wxw() -#define wyx wyx() -#define wyy wyy() -#define wyz wyz() -#define wyw wyw() -#define wzx wzx() -#define wzy wzy() -#define wzz wzz() -#define wzw wzw() -#define wwx wwx() -#define wwy wwy() -#define wwz wwz() -#define www www() -#define xxxx xxxx() -#define xxxy xxxy() -#define xxxz xxxz() -#define xxxw xxxw() -#define xxyx xxyx() -#define xxyy xxyy() -#define xxyz xxyz() -#define xxyw xxyw() -#define xxzx xxzx() -#define xxzy xxzy() -#define xxzz xxzz() -#define xxzw xxzw() -#define xxwx xxwx() -#define xxwy xxwy() -#define xxwz xxwz() -#define xxww xxww() -#define xyxx xyxx() -#define xyxy xyxy() -#define xyxz xyxz() -#define xyxw xyxw() -#define xyyx xyyx() -#define xyyy xyyy() -#define xyyz xyyz() -#define xyyw xyyw() -#define xyzx xyzx() -#define xyzy xyzy() -#define xyzz xyzz() -#define xyzw xyzw() -#define xywx xywx() -#define xywy xywy() -#define xywz xywz() -#define xyww xyww() -#define xzxx xzxx() -#define xzxy xzxy() -#define xzxz xzxz() -#define xzxw xzxw() -#define xzyx xzyx() -#define xzyy xzyy() -#define xzyz xzyz() -#define xzyw xzyw() -#define xzzx xzzx() -#define xzzy xzzy() -#define xzzz xzzz() -#define xzzw xzzw() -#define xzwx xzwx() -#define xzwy xzwy() -#define xzwz xzwz() -#define xzww xzww() -#define xwxx xwxx() -#define xwxy xwxy() -#define xwxz xwxz() -#define xwxw xwxw() -#define xwyx xwyx() -#define xwyy xwyy() -#define xwyz xwyz() -#define xwyw xwyw() -#define xwzx xwzx() -#define xwzy xwzy() -#define xwzz xwzz() -#define xwzw xwzw() -#define xwwx xwwx() -#define xwwy xwwy() -#define xwwz xwwz() -#define xwww xwww() -#define yxxx yxxx() -#define yxxy yxxy() -#define yxxz yxxz() -#define yxxw yxxw() -#define yxyx yxyx() -#define yxyy yxyy() -#define yxyz yxyz() -#define yxyw yxyw() -#define yxzx yxzx() -#define yxzy yxzy() -#define yxzz yxzz() -#define yxzw yxzw() -#define yxwx yxwx() -#define yxwy yxwy() -#define yxwz yxwz() -#define yxww yxww() -#define yyxx yyxx() -#define yyxy yyxy() -#define yyxz yyxz() -#define yyxw yyxw() -#define yyyx yyyx() -#define yyyy yyyy() -#define yyyz yyyz() -#define yyyw yyyw() -#define yyzx yyzx() -#define yyzy yyzy() -#define yyzz yyzz() -#define yyzw yyzw() -#define yywx yywx() -#define yywy yywy() -#define yywz yywz() -#define yyww yyww() -#define yzxx yzxx() -#define yzxy yzxy() -#define yzxz yzxz() -#define yzxw yzxw() -#define yzyx yzyx() -#define yzyy yzyy() -#define yzyz yzyz() -#define yzyw yzyw() -#define yzzx yzzx() -#define yzzy yzzy() -#define yzzz yzzz() -#define yzzw yzzw() -#define yzwx yzwx() -#define yzwy yzwy() -#define yzwz yzwz() -#define yzww yzww() -#define ywxx ywxx() -#define ywxy ywxy() -#define ywxz ywxz() -#define ywxw ywxw() -#define ywyx ywyx() -#define ywyy ywyy() -#define ywyz ywyz() -#define ywyw ywyw() -#define ywzx ywzx() -#define ywzy ywzy() -#define ywzz ywzz() -#define ywzw ywzw() -#define ywwx ywwx() -#define ywwy ywwy() -#define ywwz ywwz() -#define ywww ywww() -#define zxxx zxxx() -#define zxxy zxxy() -#define zxxz zxxz() -#define zxxw zxxw() -#define zxyx zxyx() -#define zxyy zxyy() -#define zxyz zxyz() -#define zxyw zxyw() -#define zxzx zxzx() -#define zxzy zxzy() -#define zxzz zxzz() -#define zxzw zxzw() -#define zxwx zxwx() -#define zxwy zxwy() -#define zxwz zxwz() -#define zxww zxww() -#define zyxx zyxx() -#define zyxy zyxy() -#define zyxz zyxz() -#define zyxw zyxw() -#define zyyx zyyx() -#define zyyy zyyy() -#define zyyz zyyz() -#define zyyw zyyw() -#define zyzx zyzx() -#define zyzy zyzy() -#define zyzz zyzz() -#define zyzw zyzw() -#define zywx zywx() -#define zywy zywy() -#define zywz zywz() -#define zyww zyww() -#define zzxx zzxx() -#define zzxy zzxy() -#define zzxz zzxz() -#define zzxw zzxw() -#define zzyx zzyx() -#define zzyy zzyy() -#define zzyz zzyz() -#define zzyw zzyw() -#define zzzx zzzx() -#define zzzy zzzy() -#define zzzz zzzz() -#define zzzw zzzw() -#define zzwx zzwx() -#define zzwy zzwy() -#define zzwz zzwz() -#define zzww zzww() -#define zwxx zwxx() -#define zwxy zwxy() -#define zwxz zwxz() -#define zwxw zwxw() -#define zwyx zwyx() -#define zwyy zwyy() -#define zwyz zwyz() -#define zwyw zwyw() -#define zwzx zwzx() -#define zwzy zwzy() -#define zwzz zwzz() -#define zwzw zwzw() -#define zwwx zwwx() -#define zwwy zwwy() -#define zwwz zwwz() -#define zwww zwww() -#define wxxx wxxx() -#define wxxy wxxy() -#define wxxz wxxz() -#define wxxw wxxw() -#define wxyx wxyx() -#define wxyy wxyy() -#define wxyz wxyz() -#define wxyw wxyw() -#define wxzx wxzx() -#define wxzy wxzy() -#define wxzz wxzz() -#define wxzw wxzw() -#define wxwx wxwx() -#define wxwy wxwy() -#define wxwz wxwz() -#define wxww wxww() -#define wyxx wyxx() -#define wyxy wyxy() -#define wyxz wyxz() -#define wyxw wyxw() -#define wyyx wyyx() -#define wyyy wyyy() -#define wyyz wyyz() -#define wyyw wyyw() -#define wyzx wyzx() -#define wyzy wyzy() -#define wyzz wyzz() -#define wyzw wyzw() -#define wywx wywx() -#define wywy wywy() -#define wywz wywz() -#define wyww wyww() -#define wzxx wzxx() -#define wzxy wzxy() -#define wzxz wzxz() -#define wzxw wzxw() -#define wzyx wzyx() -#define wzyy wzyy() -#define wzyz wzyz() -#define wzyw wzyw() -#define wzzx wzzx() -#define wzzy wzzy() -#define wzzz wzzz() -#define wzzw wzzw() -#define wzwx wzwx() -#define wzwy wzwy() -#define wzwz wzwz() -#define wzww wzww() -#define wwxx wwxx() -#define wwxy wwxy() -#define wwxz wwxz() -#define wwxw wwxw() -#define wwyx wwyx() -#define wwyy wwyy() -#define wwyz wwyz() -#define wwyw wwyw() -#define wwzx wwzx() -#define wwzy wwzy() -#define wwzz wwzz() -#define wwzw wwzw() -#define wwwx wwwx() -#define wwwy wwwy() -#define wwwz wwwz() -#define wwww wwww() diff --git a/3rdparty/bimg/3rdparty/lodepng/LICENSE b/3rdparty/bimg/3rdparty/lodepng/LICENSE new file mode 100644 index 00000000000..a5fb0603d9b --- /dev/null +++ b/3rdparty/bimg/3rdparty/lodepng/LICENSE @@ -0,0 +1,21 @@ +Copyright (c) 2005-2018 Lode Vandevenne + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + diff --git a/3rdparty/bimg/3rdparty/lodepng/README.md b/3rdparty/bimg/3rdparty/lodepng/README.md deleted file mode 100644 index 35f925494f6..00000000000 --- a/3rdparty/bimg/3rdparty/lodepng/README.md +++ /dev/null @@ -1,10 +0,0 @@ -LodePNG -------- - -PNG encoder and decoder in C and C++. - -Home page: http://lodev.org/lodepng/ - -Only two files are needed to allow your program to read and write PNG files: lodepng.cpp and lodepng.h. - -The other files in the project are just examples, unit tests, etc... diff --git a/3rdparty/bimg/3rdparty/lodepng/lodepng.cpp b/3rdparty/bimg/3rdparty/lodepng/lodepng.cpp index 7baf7f9274e..ee8cf33d293 100644 --- a/3rdparty/bimg/3rdparty/lodepng/lodepng.cpp +++ b/3rdparty/bimg/3rdparty/lodepng/lodepng.cpp @@ -1,7 +1,7 @@ /* -LodePNG version 20160501 +LodePNG version 20200306 -Copyright (c) 2005-2016 Lode Vandevenne +Copyright (c) 2005-2020 Lode Vandevenne This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -30,16 +30,21 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for #include "lodepng.h" -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> +#ifdef LODEPNG_COMPILE_DISK +#include <limits.h> /* LONG_MAX */ +#include <stdio.h> /* file handling */ +#endif /* LODEPNG_COMPILE_DISK */ + +#ifdef LODEPNG_COMPILE_ALLOCATORS +#include <stdlib.h> /* allocations */ +#endif /* LODEPNG_COMPILE_ALLOCATORS */ #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/ #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/ #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/ #endif /*_MSC_VER */ -const char* LODEPNG_VERSION_STRING = "20160501"; +const char* LODEPNG_VERSION_STRING = "20200306"; /* This source file is built up in the following large parts. The code sections @@ -50,6 +55,12 @@ with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way -The C++ wrapper around all of the above */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* // Tools for C, and common code for PNG and Zlib. // */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ + /*The malloc, realloc and free functions defined here with "lodepng_" in front of the name, so that you can easily change them to others related to your platform if needed. Everything else in the code calls these. Pass @@ -60,41 +71,113 @@ lodepng source code. Don't forget to remove "static" if you copypaste them from here.*/ #ifdef LODEPNG_COMPILE_ALLOCATORS -static void* lodepng_malloc(size_t size) -{ +static void* lodepng_malloc(size_t size) { +#ifdef LODEPNG_MAX_ALLOC + if(size > LODEPNG_MAX_ALLOC) return 0; +#endif return malloc(size); } -static void* lodepng_realloc(void* ptr, size_t new_size) -{ +/* NOTE: when realloc returns NULL, it leaves the original memory untouched */ +static void* lodepng_realloc(void* ptr, size_t new_size) { +#ifdef LODEPNG_MAX_ALLOC + if(new_size > LODEPNG_MAX_ALLOC) return 0; +#endif return realloc(ptr, new_size); } -static void lodepng_free(void* ptr) -{ +static void lodepng_free(void* ptr) { free(ptr); } #else /*LODEPNG_COMPILE_ALLOCATORS*/ +/* TODO: support giving additional void* payload to the custom allocators */ void* lodepng_malloc(size_t size); void* lodepng_realloc(void* ptr, size_t new_size); void lodepng_free(void* ptr); #endif /*LODEPNG_COMPILE_ALLOCATORS*/ -/* ////////////////////////////////////////////////////////////////////////// */ -/* ////////////////////////////////////////////////////////////////////////// */ -/* // Tools for C, and common code for PNG and Zlib. // */ -/* ////////////////////////////////////////////////////////////////////////// */ -/* ////////////////////////////////////////////////////////////////////////// */ +/* convince the compiler to inline a function, for use when this measurably improves performance */ +/* inline is not available in C90, but use it when supported by the compiler */ +#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined(__cplusplus) && (__cplusplus >= 199711L)) +#define LODEPNG_INLINE inline +#else +#define LODEPNG_INLINE /* not available */ +#endif + +/* restrict is not available in C90, but use it when supported by the compiler */ +#if (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) ||\ + (defined(_MSC_VER) && (_MSC_VER >= 1400)) || \ + (defined(__WATCOMC__) && (__WATCOMC__ >= 1250) && !defined(__cplusplus)) +#define LODEPNG_RESTRICT __restrict +#else +#define LODEPNG_RESTRICT /* not available */ +#endif + +/* Replacements for C library functions such as memcpy and strlen, to support platforms +where a full C library is not available. The compiler can recognize them and compile +to something as fast. */ + +static void lodepng_memcpy(void* LODEPNG_RESTRICT dst, + const void* LODEPNG_RESTRICT src, size_t size) { + size_t i; + for(i = 0; i < size; i++) ((char*)dst)[i] = ((const char*)src)[i]; +} + +static void lodepng_memset(void* LODEPNG_RESTRICT dst, + int value, size_t num) { + size_t i; + for(i = 0; i < num; i++) ((char*)dst)[i] = (char)value; +} + +/* does not check memory out of bounds, do not use on untrusted data */ +static size_t lodepng_strlen(const char* a) { + const char* orig = a; + /* avoid warning about unused function in case of disabled COMPILE... macros */ + (void)(&lodepng_strlen); + while(*a) a++; + return (size_t)(a - orig); +} + +#define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define LODEPNG_MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define LODEPNG_ABS(x) ((x) < 0 ? -(x) : (x)) + +#if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER) +/* Safely check if adding two integers will overflow (no undefined +behavior, compiler removing the code, etc...) and output result. */ +static int lodepng_addofl(size_t a, size_t b, size_t* result) { + *result = a + b; /* Unsigned addition is well defined and safe in C90 */ + return *result < a; +} +#endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER)*/ + +#ifdef LODEPNG_COMPILE_DECODER +/* Safely check if multiplying two integers will overflow (no undefined +behavior, compiler removing the code, etc...) and output result. */ +static int lodepng_mulofl(size_t a, size_t b, size_t* result) { + *result = a * b; /* Unsigned multiplication is well defined and safe in C90 */ + return (a != 0 && *result / a != b); +} + +#ifdef LODEPNG_COMPILE_ZLIB +/* Safely check if a + b > c, even if overflow could happen. */ +static int lodepng_gtofl(size_t a, size_t b, size_t c) { + size_t d; + if(lodepng_addofl(a, b, &d)) return 1; + return d > c; +} +#endif /*LODEPNG_COMPILE_ZLIB*/ +#endif /*LODEPNG_COMPILE_DECODER*/ + /* Often in case of an error a value is assigned to a variable and then it breaks out of a loop (to go to the cleanup phase of a function). This macro does that. It makes the error handling code shorter and more readable. -Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83); +Example: if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83); */ -#define CERROR_BREAK(errorvar, code)\ -{\ +#define CERROR_BREAK(errorvar, code){\ errorvar = code;\ break;\ } @@ -103,22 +186,19 @@ Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83); #define ERROR_BREAK(code) CERROR_BREAK(error, code) /*Set error var to the error code, and return it.*/ -#define CERROR_RETURN_ERROR(errorvar, code)\ -{\ +#define CERROR_RETURN_ERROR(errorvar, code){\ errorvar = code;\ return code;\ } /*Try the code, if it returns error, also return the error.*/ -#define CERROR_TRY_RETURN(call)\ -{\ +#define CERROR_TRY_RETURN(call){\ unsigned error = call;\ if(error) return error;\ } /*Set error var to the error code, and return from the void function.*/ -#define CERROR_RETURN(errorvar, code)\ -{\ +#define CERROR_RETURN(errorvar, code){\ errorvar = code;\ return;\ } @@ -133,65 +213,43 @@ About uivector, ucvector and string: */ #ifdef LODEPNG_COMPILE_ZLIB +#ifdef LODEPNG_COMPILE_ENCODER /*dynamic vector of unsigned ints*/ -typedef struct uivector -{ +typedef struct uivector { unsigned* data; size_t size; /*size in number of unsigned longs*/ size_t allocsize; /*allocated size in bytes*/ } uivector; -static void uivector_cleanup(void* p) -{ +static void uivector_cleanup(void* p) { ((uivector*)p)->size = ((uivector*)p)->allocsize = 0; lodepng_free(((uivector*)p)->data); ((uivector*)p)->data = NULL; } /*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned uivector_reserve(uivector* p, size_t allocsize) -{ - if(allocsize > p->allocsize) - { - size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2); +static unsigned uivector_resize(uivector* p, size_t size) { + size_t allocsize = size * sizeof(unsigned); + if(allocsize > p->allocsize) { + size_t newsize = allocsize + (p->allocsize >> 1u); void* data = lodepng_realloc(p->data, newsize); - if(data) - { + if(data) { p->allocsize = newsize; p->data = (unsigned*)data; } else return 0; /*error: not enough memory*/ } - return 1; -} - -/*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned uivector_resize(uivector* p, size_t size) -{ - if(!uivector_reserve(p, size * sizeof(unsigned))) return 0; p->size = size; return 1; /*success*/ } -/*resize and give all new elements the value*/ -static unsigned uivector_resizev(uivector* p, size_t size, unsigned value) -{ - size_t oldsize = p->size, i; - if(!uivector_resize(p, size)) return 0; - for(i = oldsize; i < size; ++i) p->data[i] = value; - return 1; -} - -static void uivector_init(uivector* p) -{ +static void uivector_init(uivector* p) { p->data = NULL; p->size = p->allocsize = 0; } -#ifdef LODEPNG_COMPILE_ENCODER /*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned uivector_push_back(uivector* p, unsigned c) -{ +static unsigned uivector_push_back(uivector* p, unsigned c) { if(!uivector_resize(p, p->size + 1)) return 0; p->data[p->size - 1] = c; return 1; @@ -202,130 +260,73 @@ static unsigned uivector_push_back(uivector* p, unsigned c) /* /////////////////////////////////////////////////////////////////////////// */ /*dynamic vector of unsigned chars*/ -typedef struct ucvector -{ +typedef struct ucvector { unsigned char* data; size_t size; /*used size*/ size_t allocsize; /*allocated size*/ } ucvector; /*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned ucvector_reserve(ucvector* p, size_t allocsize) -{ - if(allocsize > p->allocsize) - { - size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2); +static unsigned ucvector_resize(ucvector* p, size_t size) { + if(size > p->allocsize) { + size_t newsize = size + (p->allocsize >> 1u); void* data = lodepng_realloc(p->data, newsize); - if(data) - { + if(data) { p->allocsize = newsize; p->data = (unsigned char*)data; } else return 0; /*error: not enough memory*/ } - return 1; -} - -/*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned ucvector_resize(ucvector* p, size_t size) -{ - if(!ucvector_reserve(p, size * sizeof(unsigned char))) return 0; p->size = size; return 1; /*success*/ } -#ifdef LODEPNG_COMPILE_PNG - -static void ucvector_cleanup(void* p) -{ - ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0; - lodepng_free(((ucvector*)p)->data); - ((ucvector*)p)->data = NULL; -} - -static void ucvector_init(ucvector* p) -{ - p->data = NULL; - p->size = p->allocsize = 0; -} -#endif /*LODEPNG_COMPILE_PNG*/ - -#ifdef LODEPNG_COMPILE_ZLIB -/*you can both convert from vector to buffer&size and vica versa. If you use -init_buffer to take over a buffer and size, it is not needed to use cleanup*/ -static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size) -{ - p->data = buffer; - p->allocsize = p->size = size; -} -#endif /*LODEPNG_COMPILE_ZLIB*/ - -#if (defined(LODEPNG_COMPILE_PNG) && defined(LODEPNG_COMPILE_ANCILLARY_CHUNKS)) || defined(LODEPNG_COMPILE_ENCODER) -/*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned ucvector_push_back(ucvector* p, unsigned char c) -{ - if(!ucvector_resize(p, p->size + 1)) return 0; - p->data[p->size - 1] = c; - return 1; +static ucvector ucvector_init(unsigned char* buffer, size_t size) { + ucvector v; + v.data = buffer; + v.allocsize = v.size = size; + return v; } -#endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ - /* ////////////////////////////////////////////////////////////////////////// */ #ifdef LODEPNG_COMPILE_PNG #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -/*returns 1 if success, 0 if failure ==> nothing done*/ -static unsigned string_resize(char** out, size_t size) -{ - char* data = (char*)lodepng_realloc(*out, size + 1); - if(data) - { - data[size] = 0; /*null termination char*/ - *out = data; - } - return data != 0; -} - -/*init a {char*, size_t} pair for use as string*/ -static void string_init(char** out) -{ - *out = NULL; - string_resize(out, 0); -} -/*free the above pair again*/ -static void string_cleanup(char** out) -{ +/*free string pointer and set it to NULL*/ +static void string_cleanup(char** out) { lodepng_free(*out); *out = NULL; } -static void string_set(char** out, const char* in) -{ - size_t insize = strlen(in), i; - if(string_resize(out, insize)) - { - for(i = 0; i != insize; ++i) - { - (*out)[i] = in[i]; - } +static char* alloc_string_sized(const char* in, size_t insize) { + char* out = (char*)lodepng_malloc(insize + 1); + if(out) { + lodepng_memcpy(out, in, insize); + out[insize] = 0; } + return out; +} + +/* dynamically allocates a new string with a copy of the null terminated input text */ +static char* alloc_string(const char* in) { + return alloc_string_sized(in, lodepng_strlen(in)); } #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ #endif /*LODEPNG_COMPILE_PNG*/ /* ////////////////////////////////////////////////////////////////////////// */ -unsigned lodepng_read32bitInt(const unsigned char* buffer) -{ - return (unsigned)((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]); +#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG) +static unsigned lodepng_read32bitInt(const unsigned char* buffer) { + return (((unsigned)buffer[0] << 24u) | ((unsigned)buffer[1] << 16u) | + ((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]); } +#endif /*defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)*/ #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER) /*buffer must have at least 4 allocated bytes available*/ -static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) -{ +static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) { buffer[0] = (unsigned char)((value >> 24) & 0xff); buffer[1] = (unsigned char)((value >> 16) & 0xff); buffer[2] = (unsigned char)((value >> 8) & 0xff); @@ -333,14 +334,6 @@ static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) } #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ -#ifdef LODEPNG_COMPILE_ENCODER -static void lodepng_add32bitInt(ucvector* buffer, unsigned value) -{ - ucvector_resize(buffer, buffer->size + 4); /*todo: give error if resize failed*/ - lodepng_set32bitInt(&buffer->data[buffer->size - 4], value); -} -#endif /*LODEPNG_COMPILE_ENCODER*/ - /* ////////////////////////////////////////////////////////////////////////// */ /* / File IO / */ /* ////////////////////////////////////////////////////////////////////////// */ @@ -348,15 +341,13 @@ static void lodepng_add32bitInt(ucvector* buffer, unsigned value) #ifdef LODEPNG_COMPILE_DISK /* returns negative value on error. This should be pure C compatible, so no fstat. */ -static long lodepng_filesize(const char* filename) -{ +static long lodepng_filesize(const char* filename) { FILE* file; long size; file = fopen(filename, "rb"); if(!file) return -1; - if(fseek(file, 0, SEEK_END) != 0) - { + if(fseek(file, 0, SEEK_END) != 0) { fclose(file); return -1; } @@ -370,8 +361,7 @@ static long lodepng_filesize(const char* filename) } /* load file into buffer that already has the correct allocated size. Returns error code.*/ -static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) -{ +static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) { FILE* file; size_t readsize; file = fopen(filename, "rb"); @@ -380,14 +370,13 @@ static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* readsize = fread(out, 1, size, file); fclose(file); - if (readsize != size) return 78; + if(readsize != size) return 78; return 0; } -unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) -{ +unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) { long size = lodepng_filesize(filename); - if (size < 0) return 78; + if(size < 0) return 78; *outsize = (size_t)size; *out = (unsigned char*)lodepng_malloc((size_t)size); @@ -397,12 +386,11 @@ unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* fil } /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ -unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) -{ +unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) { FILE* file; file = fopen(filename, "wb" ); if(!file) return 79; - fwrite((char*)buffer , 1 , buffersize, file); + fwrite(buffer, 1, buffersize, file); fclose(file); return 0; } @@ -417,52 +405,211 @@ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const #ifdef LODEPNG_COMPILE_ZLIB #ifdef LODEPNG_COMPILE_ENCODER -/*TODO: this ignores potential out of memory errors*/ -#define addBitToStream(/*size_t**/ bitpointer, /*ucvector**/ bitstream, /*unsigned char*/ bit)\ -{\ - /*add a new byte at the end*/\ - if(((*bitpointer) & 7) == 0) ucvector_push_back(bitstream, (unsigned char)0);\ - /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/\ - (bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7));\ - ++(*bitpointer);\ + +typedef struct { + ucvector* data; + unsigned char bp; /*ok to overflow, indicates bit pos inside byte*/ +} LodePNGBitWriter; + +static void LodePNGBitWriter_init(LodePNGBitWriter* writer, ucvector* data) { + writer->data = data; + writer->bp = 0; } -static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) -{ - size_t i; - for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1)); +/*TODO: this ignores potential out of memory errors*/ +#define WRITEBIT(writer, bit){\ + /* append new byte */\ + if(((writer->bp) & 7u) == 0) {\ + if(!ucvector_resize(writer->data, writer->data->size + 1)) return;\ + writer->data->data[writer->data->size - 1] = 0;\ + }\ + (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\ + ++writer->bp;\ +} + +/* LSB of value is written first, and LSB of bytes is used first */ +static void writeBits(LodePNGBitWriter* writer, unsigned value, size_t nbits) { + if(nbits == 1) { /* compiler should statically compile this case if nbits == 1 */ + WRITEBIT(writer, value); + } else { + /* TODO: increase output size only once here rather than in each WRITEBIT */ + size_t i; + for(i = 0; i != nbits; ++i) { + WRITEBIT(writer, (unsigned char)((value >> i) & 1)); + } + } } -static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) -{ +/* This one is to use for adding huffman symbol, the value bits are written MSB first */ +static void writeBitsReversed(LodePNGBitWriter* writer, unsigned value, size_t nbits) { size_t i; - for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1)); + for(i = 0; i != nbits; ++i) { + /* TODO: increase output size only once here rather than in each WRITEBIT */ + WRITEBIT(writer, (unsigned char)((value >> (nbits - 1u - i)) & 1u)); + } } #endif /*LODEPNG_COMPILE_ENCODER*/ #ifdef LODEPNG_COMPILE_DECODER -#define READBIT(bitpointer, bitstream) ((bitstream[bitpointer >> 3] >> (bitpointer & 0x7)) & (unsigned char)1) +typedef struct { + const unsigned char* data; + size_t size; /*size of data in bytes*/ + size_t bitsize; /*size of data in bits, end of valid bp values, should be 8*size*/ + size_t bp; + unsigned buffer; /*buffer for reading bits. NOTE: 'unsigned' must support at least 32 bits*/ +} LodePNGBitReader; + +/* data size argument is in bytes. Returns error if size too large causing overflow */ +static unsigned LodePNGBitReader_init(LodePNGBitReader* reader, const unsigned char* data, size_t size) { + size_t temp; + reader->data = data; + reader->size = size; + /* size in bits, return error if overflow (if size_t is 32 bit this supports up to 500MB) */ + if(lodepng_mulofl(size, 8u, &reader->bitsize)) return 105; + /*ensure incremented bp can be compared to bitsize without overflow even when it would be incremented 32 too much and + trying to ensure 32 more bits*/ + if(lodepng_addofl(reader->bitsize, 64u, &temp)) return 105; + reader->bp = 0; + reader->buffer = 0; + return 0; /*ok*/ +} -static unsigned char readBitFromStream(size_t* bitpointer, const unsigned char* bitstream) -{ - unsigned char result = (unsigned char)(READBIT(*bitpointer, bitstream)); - ++(*bitpointer); +/* +ensureBits functions: +Ensures the reader can at least read nbits bits in one or more readBits calls, +safely even if not enough bits are available. +Returns 1 if there are enough bits available, 0 if not. +*/ + +/*See ensureBits documentation above. This one ensures exactly 1 bit */ +/*static unsigned ensureBits1(LodePNGBitReader* reader) { + if(reader->bp >= reader->bitsize) return 0; + reader->buffer = (unsigned)reader->data[reader->bp >> 3u] >> (reader->bp & 7u); + return 1; +}*/ + +/*See ensureBits documentation above. This one ensures up to 9 bits */ +static unsigned ensureBits9(LodePNGBitReader* reader, size_t nbits) { + size_t start = reader->bp >> 3u; + size_t size = reader->size; + if(start + 1u < size) { + reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u); + reader->buffer >>= (reader->bp & 7u); + return 1; + } else { + reader->buffer = 0; + if(start + 0u < size) reader->buffer |= reader->data[start + 0]; + reader->buffer >>= (reader->bp & 7u); + return reader->bp + nbits <= reader->bitsize; + } +} + +/*See ensureBits documentation above. This one ensures up to 17 bits */ +static unsigned ensureBits17(LodePNGBitReader* reader, size_t nbits) { + size_t start = reader->bp >> 3u; + size_t size = reader->size; + if(start + 2u < size) { + reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | + ((unsigned)reader->data[start + 2] << 16u); + reader->buffer >>= (reader->bp & 7u); + return 1; + } else { + reader->buffer = 0; + if(start + 0u < size) reader->buffer |= reader->data[start + 0]; + if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); + reader->buffer >>= (reader->bp & 7u); + return reader->bp + nbits <= reader->bitsize; + } +} + +/*See ensureBits documentation above. This one ensures up to 25 bits */ +static LODEPNG_INLINE unsigned ensureBits25(LodePNGBitReader* reader, size_t nbits) { + size_t start = reader->bp >> 3u; + size_t size = reader->size; + if(start + 3u < size) { + reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | + ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); + reader->buffer >>= (reader->bp & 7u); + return 1; + } else { + reader->buffer = 0; + if(start + 0u < size) reader->buffer |= reader->data[start + 0]; + if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); + if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); + reader->buffer >>= (reader->bp & 7u); + return reader->bp + nbits <= reader->bitsize; + } +} + +/*See ensureBits documentation above. This one ensures up to 32 bits */ +static LODEPNG_INLINE unsigned ensureBits32(LodePNGBitReader* reader, size_t nbits) { + size_t start = reader->bp >> 3u; + size_t size = reader->size; + if(start + 4u < size) { + reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | + ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); + reader->buffer >>= (reader->bp & 7u); + reader->buffer |= (((unsigned)reader->data[start + 4] << 24u) << (8u - (reader->bp & 7u))); + return 1; + } else { + reader->buffer = 0; + if(start + 0u < size) reader->buffer |= reader->data[start + 0]; + if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); + if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); + if(start + 3u < size) reader->buffer |= ((unsigned)reader->data[start + 3] << 24u); + reader->buffer >>= (reader->bp & 7u); + return reader->bp + nbits <= reader->bitsize; + } +} + +/* Get bits without advancing the bit pointer. Must have enough bits available with ensureBits. Max nbits is 31. */ +static unsigned peekBits(LodePNGBitReader* reader, size_t nbits) { + /* The shift allows nbits to be only up to 31. */ + return reader->buffer & ((1u << nbits) - 1u); +} + +/* Must have enough bits available with ensureBits */ +static void advanceBits(LodePNGBitReader* reader, size_t nbits) { + reader->buffer >>= nbits; + reader->bp += nbits; +} + +/* Must have enough bits available with ensureBits */ +static unsigned readBits(LodePNGBitReader* reader, size_t nbits) { + unsigned result = peekBits(reader, nbits); + advanceBits(reader, nbits); return result; } -static unsigned readBitsFromStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) -{ - unsigned result = 0, i; - for(i = 0; i != nbits; ++i) - { - result += ((unsigned)READBIT(*bitpointer, bitstream)) << i; - ++(*bitpointer); +/* Public for testing only. steps and result must have numsteps values. */ +unsigned lode_png_test_bitreader(const unsigned char* data, size_t size, + size_t numsteps, const size_t* steps, unsigned* result) { + size_t i; + LodePNGBitReader reader; + unsigned error = LodePNGBitReader_init(&reader, data, size); + if(error) return 0; + for(i = 0; i < numsteps; i++) { + size_t step = steps[i]; + unsigned ok; + if(step > 25) ok = ensureBits32(&reader, step); + else if(step > 17) ok = ensureBits25(&reader, step); + else if(step > 9) ok = ensureBits17(&reader, step); + else ok = ensureBits9(&reader, step); + if(!ok) return 0; + result[i] = readBits(&reader, step); } - return result; + return 1; } #endif /*LODEPNG_COMPILE_DECODER*/ +static unsigned reverseBits(unsigned bits, unsigned num) { + /*TODO: implement faster lookup table based version when needed*/ + unsigned i, result = 0; + for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i; + return result; +} + /* ////////////////////////////////////////////////////////////////////////// */ /* / Deflate - Huffman / */ /* ////////////////////////////////////////////////////////////////////////// */ @@ -496,8 +643,8 @@ static const unsigned DISTANCEEXTRA[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; -/*the order in which "code length alphabet code lengths" are stored, out of this -the huffman tree of the dynamic huffman tree lengths is generated*/ +/*the order in which "code length alphabet code lengths" are stored as specified by deflate, out of this the huffman +tree of the dynamic huffman tree lengths is generated*/ static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; @@ -506,98 +653,149 @@ static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES] /* Huffman tree struct, containing multiple representations of the tree */ -typedef struct HuffmanTree -{ - unsigned* tree2d; - unsigned* tree1d; - unsigned* lengths; /*the lengths of the codes of the 1d-tree*/ +typedef struct HuffmanTree { + unsigned* codes; /*the huffman codes (bit patterns representing the symbols)*/ + unsigned* lengths; /*the lengths of the huffman codes*/ unsigned maxbitlen; /*maximum number of bits a single code can get*/ unsigned numcodes; /*number of symbols in the alphabet = number of codes*/ + /* for reading only */ + unsigned char* table_len; /*length of symbol from lookup table, or max length if secondary lookup needed*/ + unsigned short* table_value; /*value of symbol from lookup table, or pointer to secondary table if needed*/ } HuffmanTree; -/*function used for debug purposes to draw the tree in ascii art with C++*/ -/* -static void HuffmanTree_draw(HuffmanTree* tree) -{ - std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl; - for(size_t i = 0; i != tree->tree1d.size; ++i) - { - if(tree->lengths.data[i]) - std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl; - } - std::cout << std::endl; -}*/ - -static void HuffmanTree_init(HuffmanTree* tree) -{ - tree->tree2d = 0; - tree->tree1d = 0; +static void HuffmanTree_init(HuffmanTree* tree) { + tree->codes = 0; tree->lengths = 0; + tree->table_len = 0; + tree->table_value = 0; } -static void HuffmanTree_cleanup(HuffmanTree* tree) -{ - lodepng_free(tree->tree2d); - lodepng_free(tree->tree1d); +static void HuffmanTree_cleanup(HuffmanTree* tree) { + lodepng_free(tree->codes); lodepng_free(tree->lengths); -} - -/*the tree representation used by the decoder. return value is error*/ -static unsigned HuffmanTree_make2DTree(HuffmanTree* tree) -{ - unsigned nodefilled = 0; /*up to which node it is filled*/ - unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/ - unsigned n, i; - - tree->tree2d = (unsigned*)lodepng_malloc(tree->numcodes * 2 * sizeof(unsigned)); - if(!tree->tree2d) return 83; /*alloc fail*/ - - /* - convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means - uninited, a value >= numcodes is an address to another bit, a value < numcodes - is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as - many columns as codes - 1. - A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. - Here, the internal nodes are stored (what their 0 and 1 option point to). - There is only memory for such good tree currently, if there are more nodes - (due to too long length codes), error 55 will happen - */ - for(n = 0; n < tree->numcodes * 2; ++n) - { - tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/ - } - - for(n = 0; n < tree->numcodes; ++n) /*the codes*/ - { - for(i = 0; i != tree->lengths[n]; ++i) /*the bits for this code*/ - { - unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1); - /*oversubscribed, see comment in lodepng_error_text*/ - if(treepos > 2147483647 || treepos + 2 > tree->numcodes) return 55; - if(tree->tree2d[2 * treepos + bit] == 32767) /*not yet filled in*/ - { - if(i + 1 == tree->lengths[n]) /*last bit*/ - { - tree->tree2d[2 * treepos + bit] = n; /*put the current code in it*/ - treepos = 0; - } - else - { - /*put address of the next step in here, first that address has to be found of course - (it's just nodefilled + 1)...*/ - ++nodefilled; - /*addresses encoded with numcodes added to it*/ - tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes; - treepos = nodefilled; - } + lodepng_free(tree->table_len); + lodepng_free(tree->table_value); +} + +/* amount of bits for first huffman table lookup (aka root bits), see HuffmanTree_makeTable and huffmanDecodeSymbol.*/ +/* values 8u and 9u work the fastest */ +#define FIRSTBITS 9u + +/* a symbol value too big to represent any valid symbol, to indicate reading disallowed huffman bits combination, +which is possible in case of only 0 or 1 present symbols. */ +#define INVALIDSYMBOL 65535u + +/* make table for huffman decoding */ +static unsigned HuffmanTree_makeTable(HuffmanTree* tree) { + static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/ + static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u; + size_t i, numpresent, pointer, size; /*total table size*/ + unsigned* maxlens = (unsigned*)lodepng_malloc(headsize * sizeof(unsigned)); + if(!maxlens) return 83; /*alloc fail*/ + + /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/ + lodepng_memset(maxlens, 0, headsize * sizeof(*maxlens)); + for(i = 0; i < tree->numcodes; i++) { + unsigned symbol = tree->codes[i]; + unsigned l = tree->lengths[i]; + unsigned index; + if(l <= FIRSTBITS) continue; /*symbols that fit in first table don't increase secondary table size*/ + /*get the FIRSTBITS MSBs, the MSBs of the symbol are encoded first. See later comment about the reversing*/ + index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS); + maxlens[index] = LODEPNG_MAX(maxlens[index], l); + } + /* compute total table size: size of first table plus all secondary tables for symbols longer than FIRSTBITS */ + size = headsize; + for(i = 0; i < headsize; ++i) { + unsigned l = maxlens[i]; + if(l > FIRSTBITS) size += (1u << (l - FIRSTBITS)); + } + tree->table_len = (unsigned char*)lodepng_malloc(size * sizeof(*tree->table_len)); + tree->table_value = (unsigned short*)lodepng_malloc(size * sizeof(*tree->table_value)); + if(!tree->table_len || !tree->table_value) { + lodepng_free(maxlens); + /* freeing tree->table values is done at a higher scope */ + return 83; /*alloc fail*/ + } + /*initialize with an invalid length to indicate unused entries*/ + for(i = 0; i < size; ++i) tree->table_len[i] = 16; + + /*fill in the first table for long symbols: max prefix size and pointer to secondary tables*/ + pointer = headsize; + for(i = 0; i < headsize; ++i) { + unsigned l = maxlens[i]; + if(l <= FIRSTBITS) continue; + tree->table_len[i] = l; + tree->table_value[i] = pointer; + pointer += (1u << (l - FIRSTBITS)); + } + lodepng_free(maxlens); + + /*fill in the first table for short symbols, or secondary table for long symbols*/ + numpresent = 0; + for(i = 0; i < tree->numcodes; ++i) { + unsigned l = tree->lengths[i]; + unsigned symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/ + /*reverse bits, because the huffman bits are given in MSB first order but the bit reader reads LSB first*/ + unsigned reverse = reverseBits(symbol, l); + if(l == 0) continue; + numpresent++; + + if(l <= FIRSTBITS) { + /*short symbol, fully in first table, replicated num times if l < FIRSTBITS*/ + unsigned num = 1u << (FIRSTBITS - l); + unsigned j; + for(j = 0; j < num; ++j) { + /*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB's*/ + unsigned index = reverse | (j << l); + if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short symbol*/ + tree->table_len[index] = l; + tree->table_value[index] = i; + } + } else { + /*long symbol, shares prefix with other long symbols in first lookup table, needs second lookup*/ + /*the FIRSTBITS MSBs of the symbol are the first table index*/ + unsigned index = reverse & mask; + unsigned maxlen = tree->table_len[index]; + /*log2 of secondary table length, should be >= l - FIRSTBITS*/ + unsigned tablelen = maxlen - FIRSTBITS; + unsigned start = tree->table_value[index]; /*starting index in secondary table*/ + unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary table*/ + unsigned j; + if(maxlen < l) return 55; /*invalid tree: long symbol shares prefix with short symbol*/ + for(j = 0; j < num; ++j) { + unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */ + unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS))); + tree->table_len[index2] = l; + tree->table_value[index2] = i; } - else treepos = tree->tree2d[2 * treepos + bit] - tree->numcodes; } } - for(n = 0; n < tree->numcodes * 2; ++n) - { - if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/ + if(numpresent < 2) { + /* In case of exactly 1 symbol, in theory the huffman symbol needs 0 bits, + but deflate uses 1 bit instead. In case of 0 symbols, no symbols can + appear at all, but such huffman tree could still exist (e.g. if distance + codes are never used). In both cases, not all symbols of the table will be + filled in. Fill them in with an invalid symbol value so returning them from + huffmanDecodeSymbol will cause error. */ + for(i = 0; i < size; ++i) { + if(tree->table_len[i] == 16) { + /* As length, use a value smaller than FIRSTBITS for the head table, + and a value larger than FIRSTBITS for the secondary table, to ensure + valid behavior for advanceBits when reading this symbol. */ + tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1); + tree->table_value[i] = INVALIDSYMBOL; + } + } + } else { + /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. + If that is not the case (due to too long length codes), the table will not + have been fully used, and this is an error (not all bit combinations can be + decoded): an oversubscribed huffman tree, indicated by error 55. */ + for(i = 0; i < size; ++i) { + if(tree->table_len[i] == 16) return 55; + } } return 0; @@ -608,44 +806,40 @@ Second step for the ...makeFromLengths and ...makeFromFrequencies functions. numcodes, lengths and maxbitlen must already be filled in correctly. return value is error. */ -static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) -{ - uivector blcount; - uivector nextcode; +static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) { + unsigned* blcount; + unsigned* nextcode; unsigned error = 0; unsigned bits, n; - uivector_init(&blcount); - uivector_init(&nextcode); + tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned)); + blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); + nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); + if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/ - tree->tree1d = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned)); - if(!tree->tree1d) error = 83; /*alloc fail*/ - - if(!uivector_resizev(&blcount, tree->maxbitlen + 1, 0) - || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0)) - error = 83; /*alloc fail*/ - - if(!error) - { + if(!error) { + for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0; /*step 1: count number of instances of each code length*/ - for(bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]]; + for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]]; /*step 2: generate the nextcode values*/ - for(bits = 1; bits <= tree->maxbitlen; ++bits) - { - nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1; + for(bits = 1; bits <= tree->maxbitlen; ++bits) { + nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u; } /*step 3: generate all the codes*/ - for(n = 0; n != tree->numcodes; ++n) - { - if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++; + for(n = 0; n != tree->numcodes; ++n) { + if(tree->lengths[n] != 0) { + tree->codes[n] = nextcode[tree->lengths[n]]++; + /*remove superfluous bits from the code*/ + tree->codes[n] &= ((1u << tree->lengths[n]) - 1u); + } } } - uivector_cleanup(&blcount); - uivector_cleanup(&nextcode); + lodepng_free(blcount); + lodepng_free(nextcode); - if(!error) return HuffmanTree_make2DTree(tree); - else return error; + if(!error) error = HuffmanTree_makeTable(tree); + return error; } /* @@ -654,8 +848,7 @@ by Deflate. maxbitlen is the maximum bits that a code in the tree can have. return value is error. */ static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, - size_t numcodes, unsigned maxbitlen) -{ + size_t numcodes, unsigned maxbitlen) { unsigned i; tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned)); if(!tree->lengths) return 83; /*alloc fail*/ @@ -671,8 +864,7 @@ static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* b Jyrki Katajainen, Alistair Moffat, Andrew Turpin, 1995.*/ /*chain node for boundary package merge*/ -typedef struct BPMNode -{ +typedef struct BPMNode { int weight; /*the sum of all weights in this chain*/ unsigned index; /*index of this leaf node (called "count" in the paper)*/ struct BPMNode* tail; /*the next nodes in this chain (null if last)*/ @@ -680,8 +872,7 @@ typedef struct BPMNode } BPMNode; /*lists of chains*/ -typedef struct BPMLists -{ +typedef struct BPMLists { /*memory pool*/ unsigned memsize; BPMNode* memory; @@ -695,26 +886,22 @@ typedef struct BPMLists } BPMLists; /*creates a new chain node with the given parameters, from the memory in the lists */ -static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail) -{ +static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail) { unsigned i; BPMNode* result; /*memory full, so garbage collect*/ - if(lists->nextfree >= lists->numfree) - { + if(lists->nextfree >= lists->numfree) { /*mark only those that are in use*/ for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0; - for(i = 0; i != lists->listsize; ++i) - { + for(i = 0; i != lists->listsize; ++i) { BPMNode* node; for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1; for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1; } /*collect those that are free*/ lists->numfree = 0; - for(i = 0; i != lists->memsize; ++i) - { + for(i = 0; i != lists->memsize; ++i) { if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i]; } lists->nextfree = 0; @@ -728,58 +915,48 @@ static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMN } /*sort the leaves with stable mergesort*/ -static void bpmnode_sort(BPMNode* leaves, size_t num) -{ +static void bpmnode_sort(BPMNode* leaves, size_t num) { BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num); size_t width, counter = 0; - for(width = 1; width < num; width *= 2) - { + for(width = 1; width < num; width *= 2) { BPMNode* a = (counter & 1) ? mem : leaves; BPMNode* b = (counter & 1) ? leaves : mem; size_t p; - for(p = 0; p < num; p += 2 * width) - { + for(p = 0; p < num; p += 2 * width) { size_t q = (p + width > num) ? num : (p + width); size_t r = (p + 2 * width > num) ? num : (p + 2 * width); size_t i = p, j = q, k; - for(k = p; k < r; k++) - { + for(k = p; k < r; k++) { if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++]; else b[k] = a[j++]; } } counter++; } - if(counter & 1) memcpy(leaves, mem, sizeof(*leaves) * num); + if(counter & 1) lodepng_memcpy(leaves, mem, sizeof(*leaves) * num); lodepng_free(mem); } /*Boundary Package Merge step, numpresent is the amount of leaves, and c is the current chain.*/ -static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num) -{ +static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num) { unsigned lastindex = lists->chains1[c]->index; - if(c == 0) - { + if(c == 0) { if(lastindex >= numpresent) return; lists->chains0[c] = lists->chains1[c]; lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0); - } - else - { + } else { /*sum of the weights of the head nodes of the previous lookahead chains.*/ int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight; lists->chains0[c] = lists->chains1[c]; - if(lastindex < numpresent && sum > leaves[lastindex].weight) - { + if(lastindex < numpresent && sum > leaves[lastindex].weight) { lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[c]->tail); return; } lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]); /*in the end we are only interested in the chain of the last list, so no need to recurse if we're at the last one (this gives measurable speedup)*/ - if(num + 1 < (int)(2 * numpresent - 2)) - { + if(num + 1 < (int)(2 * numpresent - 2)) { boundaryPM(lists, leaves, numpresent, c - 1, num); boundaryPM(lists, leaves, numpresent, c - 1, num); } @@ -787,47 +964,39 @@ static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int } unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, - size_t numcodes, unsigned maxbitlen) -{ + size_t numcodes, unsigned maxbitlen) { unsigned error = 0; unsigned i; size_t numpresent = 0; /*number of symbols with non-zero frequency*/ BPMNode* leaves; /*the symbols, only those with > 0 frequency*/ if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/ - if((1u << maxbitlen) < numcodes) return 80; /*error: represent all symbols*/ + if((1u << maxbitlen) < (unsigned)numcodes) return 80; /*error: represent all symbols*/ leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves)); if(!leaves) return 83; /*alloc fail*/ - for(i = 0; i != numcodes; ++i) - { - if(frequencies[i] > 0) - { + for(i = 0; i != numcodes; ++i) { + if(frequencies[i] > 0) { leaves[numpresent].weight = (int)frequencies[i]; leaves[numpresent].index = i; ++numpresent; } } - for(i = 0; i != numcodes; ++i) lengths[i] = 0; + lodepng_memset(lengths, 0, numcodes * sizeof(*lengths)); /*ensure at least two present symbols. There should be at least one symbol according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To make these work as well ensure there are at least two symbols. The Package-Merge code below also doesn't work correctly if there's only one - symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/ - if(numpresent == 0) - { + symbol, it'd give it the theoretical 0 bits but in practice zlib wants 1 bit*/ + if(numpresent == 0) { lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/ - } - else if(numpresent == 1) - { + } else if(numpresent == 1) { lengths[leaves[0].index] = 1; lengths[leaves[0].index == 0 ? 1 : 0] = 1; - } - else - { + } else { BPMLists lists; BPMNode* node; @@ -843,15 +1012,13 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*)); if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/ - if(!error) - { + if(!error) { for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i]; bpmnode_create(&lists, leaves[0].weight, 1, 0); bpmnode_create(&lists, leaves[1].weight, 2, 0); - for(i = 0; i != lists.listsize; ++i) - { + for(i = 0; i != lists.listsize; ++i) { lists.chains0[i] = &lists.memory[0]; lists.chains1[i] = &lists.memory[1]; } @@ -859,8 +1026,7 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/ for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i); - for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) - { + for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) { for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index]; } } @@ -877,36 +1043,22 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen /*Create the Huffman tree given the symbol frequencies*/ static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies, - size_t mincodes, size_t numcodes, unsigned maxbitlen) -{ + size_t mincodes, size_t numcodes, unsigned maxbitlen) { unsigned error = 0; while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/ + tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned)); + if(!tree->lengths) return 83; /*alloc fail*/ tree->maxbitlen = maxbitlen; tree->numcodes = (unsigned)numcodes; /*number of symbols*/ - tree->lengths = (unsigned*)lodepng_realloc(tree->lengths, numcodes * sizeof(unsigned)); - if(!tree->lengths) return 83; /*alloc fail*/ - /*initialize all lengths to 0*/ - memset(tree->lengths, 0, numcodes * sizeof(unsigned)); error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen); if(!error) error = HuffmanTree_makeFromLengths2(tree); return error; } - -static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index) -{ - return tree->tree1d[index]; -} - -static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index) -{ - return tree->lengths[index]; -} #endif /*LODEPNG_COMPILE_ENCODER*/ /*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/ -static unsigned generateFixedLitLenTree(HuffmanTree* tree) -{ +static unsigned generateFixedLitLenTree(HuffmanTree* tree) { unsigned i, error = 0; unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); if(!bitlen) return 83; /*alloc fail*/ @@ -924,8 +1076,7 @@ static unsigned generateFixedLitLenTree(HuffmanTree* tree) } /*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/ -static unsigned generateFixedDistanceTree(HuffmanTree* tree) -{ +static unsigned generateFixedDistanceTree(HuffmanTree* tree) { unsigned i, error = 0; unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); if(!bitlen) return 83; /*alloc fail*/ @@ -941,26 +1092,21 @@ static unsigned generateFixedDistanceTree(HuffmanTree* tree) #ifdef LODEPNG_COMPILE_DECODER /* -returns the code, or (unsigned)(-1) if error happened -inbitlength is the length of the complete buffer, in bits (so its byte length times 8) +returns the code. The bit reader must already have been ensured at least 15 bits */ -static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp, - const HuffmanTree* codetree, size_t inbitlength) -{ - unsigned treepos = 0, ct; - for(;;) - { - if(*bp >= inbitlength) return (unsigned)(-1); /*error: end of input memory reached without endcode*/ - /* - decode the symbol from the tree. The "readBitFromStream" code is inlined in - the expression below because this is the biggest bottleneck while decoding - */ - ct = codetree->tree2d[(treepos << 1) + READBIT(*bp, in)]; - ++(*bp); - if(ct < codetree->numcodes) return ct; /*the symbol is decoded, return it*/ - else treepos = ct - codetree->numcodes; /*symbol not yet decoded, instead move tree position*/ - - if(treepos >= codetree->numcodes) return (unsigned)(-1); /*error: it appeared outside the codetree*/ +static unsigned huffmanDecodeSymbol(LodePNGBitReader* reader, const HuffmanTree* codetree) { + unsigned short code = peekBits(reader, FIRSTBITS); + unsigned short l = codetree->table_len[code]; + unsigned short value = codetree->table_value[code]; + if(l <= FIRSTBITS) { + advanceBits(reader, l); + return value; + } else { + unsigned index2; + advanceBits(reader, FIRSTBITS); + index2 = value + peekBits(reader, l - FIRSTBITS); + advanceBits(reader, codetree->table_len[index2] - FIRSTBITS); + return codetree->table_value[index2]; } } #endif /*LODEPNG_COMPILE_DECODER*/ @@ -971,22 +1117,20 @@ static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp, /* / Inflator (Decompressor) / */ /* ////////////////////////////////////////////////////////////////////////// */ -/*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/ -static void getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) -{ - /*TODO: check for out of memory errors*/ - generateFixedLitLenTree(tree_ll); - generateFixedDistanceTree(tree_d); +/*get the tree of a deflated block with fixed tree, as specified in the deflate specification +Returns error code.*/ +static unsigned getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) { + unsigned error = generateFixedLitLenTree(tree_ll); + if(error) return error; + return generateFixedDistanceTree(tree_d); } /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, - const unsigned char* in, size_t* bp, size_t inlength) -{ + LodePNGBitReader* reader) { /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/ unsigned error = 0; unsigned n, HLIT, HDIST, HCLEN, i; - size_t inbitlength = inlength * 8; /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/ unsigned* bitlen_ll = 0; /*lit,len code lengths*/ @@ -995,30 +1139,31 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, unsigned* bitlen_cl = 0; HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/ - if((*bp) + 14 > (inlength << 3)) return 49; /*error: the bit pointer is or will go past the memory*/ + if(!ensureBits17(reader, 14)) return 49; /*error: the bit pointer is or will go past the memory*/ /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/ - HLIT = readBitsFromStream(bp, in, 5) + 257; + HLIT = readBits(reader, 5) + 257; /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/ - HDIST = readBitsFromStream(bp, in, 5) + 1; + HDIST = readBits(reader, 5) + 1; /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ - HCLEN = readBitsFromStream(bp, in, 4) + 4; + HCLEN = readBits(reader, 4) + 4; - if((*bp) + HCLEN * 3 > (inlength << 3)) return 50; /*error: the bit pointer is or will go past the memory*/ + bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); + if(!bitlen_cl) return 83 /*alloc fail*/; HuffmanTree_init(&tree_cl); - while(!error) - { + while(!error) { /*read the code length codes out of 3 * (amount of code length codes) bits*/ - - bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); - if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/); - - for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i) - { - if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3); - else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/ + if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) { + ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/ + } + for(i = 0; i != HCLEN; ++i) { + ensureBits9(reader, 3); /*out of bounds already checked above */ + bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3); + } + for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) { + bitlen_cl[CLCL_ORDER[i]] = 0; } error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7); @@ -1028,83 +1173,69 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); - for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0; - for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0; + lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll)); + lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d)); /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/ i = 0; - while(i < HLIT + HDIST) - { - unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength); - if(code <= 15) /*a length code*/ - { + while(i < HLIT + HDIST) { + unsigned code; + ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/ + code = huffmanDecodeSymbol(reader, &tree_cl); + if(code <= 15) /*a length code*/ { if(i < HLIT) bitlen_ll[i] = code; else bitlen_d[i - HLIT] = code; ++i; - } - else if(code == 16) /*repeat previous*/ - { + } else if(code == 16) /*repeat previous*/ { unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/ unsigned value; /*set value to the previous code*/ if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/ - if((*bp + 2) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ - replength += readBitsFromStream(bp, in, 2); + replength += readBits(reader, 2); if(i < HLIT + 1) value = bitlen_ll[i - 1]; else value = bitlen_d[i - HLIT - 1]; /*repeat this value in the next lengths*/ - for(n = 0; n < replength; ++n) - { + for(n = 0; n < replength; ++n) { if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/ if(i < HLIT) bitlen_ll[i] = value; else bitlen_d[i - HLIT] = value; ++i; } - } - else if(code == 17) /*repeat "0" 3-10 times*/ - { + } else if(code == 17) /*repeat "0" 3-10 times*/ { unsigned replength = 3; /*read in the bits that indicate repeat length*/ - if((*bp + 3) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ - replength += readBitsFromStream(bp, in, 3); + replength += readBits(reader, 3); /*repeat this value in the next lengths*/ - for(n = 0; n < replength; ++n) - { + for(n = 0; n < replength; ++n) { if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/ if(i < HLIT) bitlen_ll[i] = 0; else bitlen_d[i - HLIT] = 0; ++i; } - } - else if(code == 18) /*repeat "0" 11-138 times*/ - { + } else if(code == 18) /*repeat "0" 11-138 times*/ { unsigned replength = 11; /*read in the bits that indicate repeat length*/ - if((*bp + 7) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ - replength += readBitsFromStream(bp, in, 7); + replength += readBits(reader, 7); /*repeat this value in the next lengths*/ - for(n = 0; n < replength; ++n) - { + for(n = 0; n < replength; ++n) { if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/ if(i < HLIT) bitlen_ll[i] = 0; else bitlen_d[i - HLIT] = 0; ++i; } + } else /*if(code == INVALIDSYMBOL)*/ { + ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/ } - else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ - { - if(code == (unsigned)(-1)) - { - /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol - (10=no endcode, 11=wrong jump outside of tree)*/ - error = (*bp) > inbitlength ? 10 : 11; - } - else error = 16; /*unexisting code, this can never happen*/ - break; + /*check if any of the ensureBits above went out of bounds*/ + if(reader->bp > reader->bitsize) { + /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol + (10=no endcode, 11=wrong jump outside of tree)*/ + /* TODO: revise error codes 10,11,50: the above comment is no longer valid */ + ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ } } if(error) break; @@ -1127,92 +1258,88 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, return error; } -/*inflate a block with dynamic of fixed Huffman tree*/ -static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size_t* bp, - size_t* pos, size_t inlength, unsigned btype) -{ +/*inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.*/ +static unsigned inflateHuffmanBlock(ucvector* out, LodePNGBitReader* reader, + unsigned btype) { unsigned error = 0; HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/ HuffmanTree tree_d; /*the huffman tree for distance codes*/ - size_t inbitlength = inlength * 8; HuffmanTree_init(&tree_ll); HuffmanTree_init(&tree_d); - if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d); - else if(btype == 2) error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength); + if(btype == 1) error = getTreeInflateFixed(&tree_ll, &tree_d); + else /*if(btype == 2)*/ error = getTreeInflateDynamic(&tree_ll, &tree_d, reader); - while(!error) /*decode all symbols until end reached, breaks at end code*/ - { + while(!error) /*decode all symbols until end reached, breaks at end code*/ { /*code_ll is literal, length or end code*/ - unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength); - if(code_ll <= 255) /*literal symbol*/ - { - /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/ - if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/); - out->data[*pos] = (unsigned char)code_ll; - ++(*pos); - } - else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ - { + unsigned code_ll; + ensureBits25(reader, 20); /* up to 15 for the huffman symbol, up to 5 for the length extra bits */ + code_ll = huffmanDecodeSymbol(reader, &tree_ll); + if(code_ll <= 255) /*literal symbol*/ { + if(!ucvector_resize(out, out->size + 1)) ERROR_BREAK(83 /*alloc fail*/); + out->data[out->size - 1] = (unsigned char)code_ll; + } else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ { unsigned code_d, distance; unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/ - size_t start, forward, backward, length; + size_t start, backward, length; /*part 1: get length base*/ length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX]; /*part 2: get extra bits and add the value of that to length*/ numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX]; - if((*bp + numextrabits_l) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ - length += readBitsFromStream(bp, in, numextrabits_l); + if(numextrabits_l != 0) { + /* bits already ensured above */ + length += readBits(reader, numextrabits_l); + } /*part 3: get distance code*/ - code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength); - if(code_d > 29) - { - if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ - { - /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol - (10=no endcode, 11=wrong jump outside of tree)*/ - error = (*bp) > inlength * 8 ? 10 : 11; + ensureBits32(reader, 28); /* up to 15 for the huffman symbol, up to 13 for the extra bits */ + code_d = huffmanDecodeSymbol(reader, &tree_d); + if(code_d > 29) { + if(code_d <= 31) { + ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/ + } else /* if(code_d == INVALIDSYMBOL) */{ + ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/ } - else error = 18; /*error: invalid distance code (30-31 are never used)*/ - break; } distance = DISTANCEBASE[code_d]; /*part 4: get extra bits from distance*/ numextrabits_d = DISTANCEEXTRA[code_d]; - if((*bp + numextrabits_d) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ - distance += readBitsFromStream(bp, in, numextrabits_d); + if(numextrabits_d != 0) { + /* bits already ensured above */ + distance += readBits(reader, numextrabits_d); + } /*part 5: fill in all the out[n] values based on the length and dist*/ - start = (*pos); + start = out->size; if(distance > start) ERROR_BREAK(52); /*too long backward distance*/ backward = start - distance; - if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/); - if (distance < length) { - for(forward = 0; forward < length; ++forward) - { - out->data[(*pos)++] = out->data[backward++]; + if(!ucvector_resize(out, out->size + length)) ERROR_BREAK(83 /*alloc fail*/); + if(distance < length) { + size_t forward; + lodepng_memcpy(out->data + start, out->data + backward, distance); + start += distance; + for(forward = distance; forward < length; ++forward) { + out->data[start++] = out->data[backward++]; } } else { - memcpy(out->data + *pos, out->data + backward, length); - *pos += length; + lodepng_memcpy(out->data + start, out->data + backward, length); } - } - else if(code_ll == 256) - { + } else if(code_ll == 256) { break; /*end code, break the loop*/ + } else /*if(code_ll == INVALIDSYMBOL)*/ { + ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/ } - else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ - { + /*check if any of the ensureBits above went out of bounds*/ + if(reader->bp > reader->bitsize) { /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol (10=no endcode, 11=wrong jump outside of tree)*/ - error = ((*bp) > inlength * 8) ? 10 : 11; - break; + /* TODO: revise error codes 10,11,50: the above comment is no longer valid */ + ERROR_BREAK(51); /*error, bit pointer jumps past memory*/ } } @@ -1222,57 +1349,56 @@ static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size return error; } -static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, size_t* bp, size_t* pos, size_t inlength) -{ - size_t p; - unsigned LEN, NLEN, n, error = 0; +static unsigned inflateNoCompression(ucvector* out, LodePNGBitReader* reader, + const LodePNGDecompressSettings* settings) { + size_t bytepos; + size_t size = reader->size; + unsigned LEN, NLEN, error = 0; /*go to first boundary of byte*/ - while(((*bp) & 0x7) != 0) ++(*bp); - p = (*bp) / 8; /*byte position*/ + bytepos = (reader->bp + 7u) >> 3u; /*read LEN (2 bytes) and NLEN (2 bytes)*/ - if(p + 4 >= inlength) return 52; /*error, bit pointer will jump past memory*/ - LEN = in[p] + 256u * in[p + 1]; p += 2; - NLEN = in[p] + 256u * in[p + 1]; p += 2; + if(bytepos + 4 >= size) return 52; /*error, bit pointer will jump past memory*/ + LEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2; + NLEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2; /*check if 16-bit NLEN is really the one's complement of LEN*/ - if(LEN + NLEN != 65535) return 21; /*error: NLEN is not one's complement of LEN*/ + if(!settings->ignore_nlen && LEN + NLEN != 65535) { + return 21; /*error: NLEN is not one's complement of LEN*/ + } - if(!ucvector_resize(out, (*pos) + LEN)) return 83; /*alloc fail*/ + if(!ucvector_resize(out, out->size + LEN)) return 83; /*alloc fail*/ /*read the literal data: LEN bytes are now stored in the out buffer*/ - if(p + LEN > inlength) return 23; /*error: reading outside of in buffer*/ - for(n = 0; n < LEN; ++n) out->data[(*pos)++] = in[p++]; + if(bytepos + LEN > size) return 23; /*error: reading outside of in buffer*/ + + lodepng_memcpy(out->data + out->size - LEN, reader->data + bytepos, LEN); + bytepos += LEN; - (*bp) = p * 8; + reader->bp = bytepos << 3u; return error; } static unsigned lodepng_inflatev(ucvector* out, const unsigned char* in, size_t insize, - const LodePNGDecompressSettings* settings) -{ - /*bit pointer in the "in" data, current byte is bp >> 3, current bit is bp & 0x7 (from lsb to msb of the byte)*/ - size_t bp = 0; + const LodePNGDecompressSettings* settings) { unsigned BFINAL = 0; - size_t pos = 0; /*byte position in the out buffer*/ - unsigned error = 0; + LodePNGBitReader reader; + unsigned error = LodePNGBitReader_init(&reader, in, insize); - (void)settings; + if(error) return error; - while(!BFINAL) - { + while(!BFINAL) { unsigned BTYPE; - if(bp + 2 >= insize * 8) return 52; /*error, bit pointer will jump past memory*/ - BFINAL = readBitFromStream(&bp, in); - BTYPE = 1u * readBitFromStream(&bp, in); - BTYPE += 2u * readBitFromStream(&bp, in); + if(!ensureBits9(&reader, 3)) return 52; /*error, bit pointer will jump past memory*/ + BFINAL = readBits(&reader, 1); + BTYPE = readBits(&reader, 2); if(BTYPE == 3) return 20; /*error: invalid BTYPE*/ - else if(BTYPE == 0) error = inflateNoCompression(out, in, &bp, &pos, insize); /*no compression*/ - else error = inflateHuffmanBlock(out, in, &bp, &pos, insize, BTYPE); /*compression, BTYPE 01 or 10*/ + else if(BTYPE == 0) error = inflateNoCompression(out, &reader, settings); /*no compression*/ + else error = inflateHuffmanBlock(out, &reader, BTYPE); /*compression, BTYPE 01 or 10*/ if(error) return error; } @@ -1282,28 +1408,22 @@ static unsigned lodepng_inflatev(ucvector* out, unsigned lodepng_inflate(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, - const LodePNGDecompressSettings* settings) -{ - unsigned error; - ucvector v; - ucvector_init_buffer(&v, *out, *outsize); - error = lodepng_inflatev(&v, in, insize, settings); + const LodePNGDecompressSettings* settings) { + ucvector v = ucvector_init(*out, *outsize); + unsigned error = lodepng_inflatev(&v, in, insize, settings); *out = v.data; *outsize = v.size; return error; } -static unsigned inflate(unsigned char** out, size_t* outsize, - const unsigned char* in, size_t insize, - const LodePNGDecompressSettings* settings) -{ - if(settings->custom_inflate) - { - return settings->custom_inflate(out, outsize, in, insize, settings); - } - else - { - return lodepng_inflate(out, outsize, in, insize, settings); +static unsigned inflatev(ucvector* out, const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings) { + if(settings->custom_inflate) { + unsigned error = settings->custom_inflate(&out->data, &out->size, in, insize, settings); + out->allocsize = out->size; + return error; + } else { + return lodepng_inflatev(out, in, insize, settings); } } @@ -1317,31 +1437,23 @@ static unsigned inflate(unsigned char** out, size_t* outsize, static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258; -/*bitlen is the size in bits of the code*/ -static void addHuffmanSymbol(size_t* bp, ucvector* compressed, unsigned code, unsigned bitlen) -{ - addBitsToStreamReversed(bp, compressed, code, bitlen); -} - /*search the index in the array, that has the largest value smaller than or equal to the given value, given array must be sorted (if no value is smaller, it returns the size of the given array)*/ -static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) -{ +static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) { /*binary search (only small gain over linear). TODO: use CPU log2 instruction for getting symbols instead*/ size_t left = 1; size_t right = array_size - 1; while(left <= right) { size_t mid = (left + right) >> 1; - if (array[mid] >= value) right = mid - 1; + if(array[mid] >= value) right = mid - 1; else left = mid + 1; } if(left >= array_size || array[left] > value) left--; return left; } -static void addLengthDistance(uivector* values, size_t length, size_t distance) -{ +static void addLengthDistance(uivector* values, size_t length, size_t distance) { /*values in encoded vector are those used by deflate: 0-255: literal bytes 256: end @@ -1353,10 +1465,15 @@ static void addLengthDistance(uivector* values, size_t length, size_t distance) unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance); unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]); - uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX); - uivector_push_back(values, extra_length); - uivector_push_back(values, dist_code); - uivector_push_back(values, extra_distance); + size_t pos = values->size; + /*TODO: return error when this fails (out of memory)*/ + unsigned ok = uivector_resize(values, values->size + 4); + if(ok) { + values->data[pos + 0] = length_code + FIRST_LENGTH_CODE_INDEX; + values->data[pos + 1] = extra_length; + values->data[pos + 2] = dist_code; + values->data[pos + 3] = extra_distance; + } } /*3 bytes of data get encoded into two bytes. The hash cannot use more than 3 @@ -1364,8 +1481,7 @@ bytes as input because 3 is the minimum match length for deflate*/ static const unsigned HASH_NUM_VALUES = 65536; static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as initializer*/ -typedef struct Hash -{ +typedef struct Hash { int* head; /*hash value to head circular pos - can be outdated if went around window*/ /*circular pos to prev circular pos*/ unsigned short* chain; @@ -1378,8 +1494,7 @@ typedef struct Hash unsigned short* zeros; /*length of zeros streak, used as a second hash chain*/ } Hash; -static unsigned hash_init(Hash* hash, unsigned windowsize) -{ +static unsigned hash_init(Hash* hash, unsigned windowsize) { unsigned i; hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES); hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize); @@ -1389,8 +1504,7 @@ static unsigned hash_init(Hash* hash, unsigned windowsize) hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1)); hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); - if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) - { + if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) { return 83; /*alloc fail*/ } @@ -1405,8 +1519,7 @@ static unsigned hash_init(Hash* hash, unsigned windowsize) return 0; } -static void hash_cleanup(Hash* hash) -{ +static void hash_cleanup(Hash* hash) { lodepng_free(hash->head); lodepng_free(hash->val); lodepng_free(hash->chain); @@ -1418,29 +1531,26 @@ static void hash_cleanup(Hash* hash) -static unsigned getHash(const unsigned char* data, size_t size, size_t pos) -{ +static unsigned getHash(const unsigned char* data, size_t size, size_t pos) { unsigned result = 0; - if(pos + 2 < size) - { + if(pos + 2 < size) { /*A simple shift and xor hash is used. Since the data of PNGs is dominated by zeroes due to the filters, a better hash does not have a significant effect on speed in traversing the chain, and causes more time spend on calculating the hash.*/ - result ^= (unsigned)(data[pos + 0] << 0u); - result ^= (unsigned)(data[pos + 1] << 4u); - result ^= (unsigned)(data[pos + 2] << 8u); + result ^= ((unsigned)data[pos + 0] << 0u); + result ^= ((unsigned)data[pos + 1] << 4u); + result ^= ((unsigned)data[pos + 2] << 8u); } else { size_t amount, i; if(pos >= size) return 0; amount = size - pos; - for(i = 0; i != amount; ++i) result ^= (unsigned)(data[pos + i] << (i * 8u)); + for(i = 0; i != amount; ++i) result ^= ((unsigned)data[pos + i] << (i * 8u)); } return result & HASH_BIT_MASK; } -static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) -{ +static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) { const unsigned char* start = data + pos; const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH; if(end > data + size) end = data + size; @@ -1451,15 +1561,14 @@ static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) } /*wpos = pos & (windowsize - 1)*/ -static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros) -{ +static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros) { hash->val[wpos] = (int)hashval; if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval]; - hash->head[hashval] = wpos; + hash->head[hashval] = (int)wpos; hash->zeros[wpos] = numzeros; if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros]; - hash->headz[numzeros] = wpos; + hash->headz[numzeros] = (int)wpos; } /* @@ -1473,12 +1582,11 @@ this hash technique is one out of several ways to speed this up. */ static unsigned encodeLZ77(uivector* out, Hash* hash, const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize, - unsigned minmatch, unsigned nicematch, unsigned lazymatching) -{ + unsigned minmatch, unsigned nicematch, unsigned lazymatching) { size_t pos; unsigned i, error = 0; /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/ - unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8; + unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8u; unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64; unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/ @@ -1499,20 +1607,16 @@ static unsigned encodeLZ77(uivector* out, Hash* hash, if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH; - for(pos = inpos; pos < insize; ++pos) - { + for(pos = inpos; pos < insize; ++pos) { size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/ unsigned chainlength = 0; hashval = getHash(in, insize, pos); - if(usezeros && hashval == 0) - { + if(usezeros && hashval == 0) { if(numzeros == 0) numzeros = countZeros(in, insize, pos); else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; - } - else - { + } else { numzeros = 0; } @@ -1528,37 +1632,32 @@ static unsigned encodeLZ77(uivector* out, Hash* hash, /*search for the longest string*/ prev_offset = 0; - for(;;) - { + for(;;) { if(chainlength++ >= maxchainlength) break; - current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize; + current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize); if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/ prev_offset = current_offset; - if(current_offset > 0) - { + if(current_offset > 0) { /*test the next characters*/ foreptr = &in[pos]; backptr = &in[pos - current_offset]; /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/ - if(numzeros >= 3) - { + if(numzeros >= 3) { unsigned skip = hash->zeros[hashpos]; if(skip > numzeros) skip = numzeros; backptr += skip; foreptr += skip; } - while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ - { + while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ { ++backptr; ++foreptr; } current_length = (unsigned)(foreptr - &in[pos]); - if(current_length > length) - { + if(current_length > length) { length = current_length; /*the longest length*/ offset = current_offset; /*the offset that is related to this longest length*/ /*jump out once a length of max length is found (speed gain). This also jumps @@ -1569,39 +1668,30 @@ static unsigned encodeLZ77(uivector* out, Hash* hash, if(hashpos == hash->chain[hashpos]) break; - if(numzeros >= 3 && length > numzeros) - { + if(numzeros >= 3 && length > numzeros) { hashpos = hash->chainz[hashpos]; if(hash->zeros[hashpos] != numzeros) break; - } - else - { + } else { hashpos = hash->chain[hashpos]; /*outdated hash value, happens if particular value was not encountered in whole last window*/ if(hash->val[hashpos] != (int)hashval) break; } } - if(lazymatching) - { - if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) - { + if(lazymatching) { + if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) { lazy = 1; lazylength = length; lazyoffset = offset; continue; /*try the next byte*/ } - if(lazy) - { + if(lazy) { lazy = 0; if(pos == 0) ERROR_BREAK(81); - if(length > lazylength + 1) - { + if(length > lazylength + 1) { /*push the previous character as literal*/ if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/); - } - else - { + } else { length = lazylength; offset = lazyoffset; hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/ @@ -1613,31 +1703,22 @@ static unsigned encodeLZ77(uivector* out, Hash* hash, if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/); /*encode it as length/distance pair or literal value*/ - if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ - { + if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ { if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); - } - else if(length < minmatch || (length == 3 && offset > 4096)) - { + } else if(length < minmatch || (length == 3 && offset > 4096)) { /*compensate for the fact that longer offsets have more extra bits, a length of only 3 may be not worth it then*/ if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); - } - else - { + } else { addLengthDistance(out, length, offset); - for(i = 1; i < length; ++i) - { + for(i = 1; i < length; ++i) { ++pos; wpos = pos & (windowsize - 1); hashval = getHash(in, insize, pos); - if(usezeros && hashval == 0) - { + if(usezeros && hashval == 0) { if(numzeros == 0) numzeros = countZeros(in, insize, pos); else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; - } - else - { + } else { numzeros = 0; } updateHashChain(hash, wpos, hashval, numzeros); @@ -1650,38 +1731,34 @@ static unsigned encodeLZ77(uivector* out, Hash* hash, /* /////////////////////////////////////////////////////////////////////////// */ -static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) -{ +static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) { /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte, 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/ - size_t i, j, numdeflateblocks = (datasize + 65534) / 65535; + size_t i, numdeflateblocks = (datasize + 65534u) / 65535u; unsigned datapos = 0; - for(i = 0; i != numdeflateblocks; ++i) - { + for(i = 0; i != numdeflateblocks; ++i) { unsigned BFINAL, BTYPE, LEN, NLEN; unsigned char firstbyte; + size_t pos = out->size; BFINAL = (i == numdeflateblocks - 1); BTYPE = 0; - firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1)); - ucvector_push_back(out, firstbyte); - LEN = 65535; - if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos; + if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos; NLEN = 65535 - LEN; - ucvector_push_back(out, (unsigned char)(LEN & 255)); - ucvector_push_back(out, (unsigned char)(LEN >> 8)); - ucvector_push_back(out, (unsigned char)(NLEN & 255)); - ucvector_push_back(out, (unsigned char)(NLEN >> 8)); + if(!ucvector_resize(out, out->size + LEN + 5)) return 83; /*alloc fail*/ - /*Decompressed data*/ - for(j = 0; j < 65535 && datapos < datasize; ++j) - { - ucvector_push_back(out, data[datapos++]); - } + firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1u) << 1u) + ((BTYPE & 2u) << 1u)); + out->data[pos + 0] = firstbyte; + out->data[pos + 1] = (unsigned char)(LEN & 255); + out->data[pos + 2] = (unsigned char)(LEN >> 8u); + out->data[pos + 3] = (unsigned char)(NLEN & 255); + out->data[pos + 4] = (unsigned char)(NLEN >> 8u); + lodepng_memcpy(out->data + pos + 5, data + datapos, LEN); + datapos += LEN; } return 0; @@ -1692,16 +1769,13 @@ write the lz77-encoded data, which has lit, len and dist codes, to compressed st tree_ll: the tree for lit and len codes. tree_d: the tree for distance codes. */ -static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encoded, - const HuffmanTree* tree_ll, const HuffmanTree* tree_d) -{ +static void writeLZ77data(LodePNGBitWriter* writer, const uivector* lz77_encoded, + const HuffmanTree* tree_ll, const HuffmanTree* tree_d) { size_t i = 0; - for(i = 0; i != lz77_encoded->size; ++i) - { + for(i = 0; i != lz77_encoded->size; ++i) { unsigned val = lz77_encoded->data[i]; - addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val)); - if(val > 256) /*for a length code, 3 more things have to be added*/ - { + writeBitsReversed(writer, tree_ll->codes[val], tree_ll->lengths[val]); + if(val > 256) /*for a length code, 3 more things have to be added*/ { unsigned length_index = val - FIRST_LENGTH_CODE_INDEX; unsigned n_length_extra_bits = LENGTHEXTRA[length_index]; unsigned length_extra_bits = lz77_encoded->data[++i]; @@ -1712,19 +1786,17 @@ static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encode unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index]; unsigned distance_extra_bits = lz77_encoded->data[++i]; - addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits); - addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code), - HuffmanTree_getLength(tree_d, distance_code)); - addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits); + writeBits(writer, length_extra_bits, n_length_extra_bits); + writeBitsReversed(writer, tree_d->codes[distance_code], tree_d->lengths[distance_code]); + writeBits(writer, distance_extra_bits, n_distance_extra_bits); } } } /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/ -static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, +static unsigned deflateDynamic(LodePNGBitWriter* writer, Hash* hash, const unsigned char* data, size_t datapos, size_t dataend, - const LodePNGCompressSettings* settings, unsigned final) -{ + const LodePNGCompressSettings* settings, unsigned final) { unsigned error = 0; /* @@ -1734,7 +1806,7 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, another huffman tree is used for the dist values ("d"). These two trees are stored using their code lengths, and to compress even more these code lengths are also run-length encoded and huffman compressed. This gives a huffman tree - of code lengths "cl". The code lenghts used to describe this third tree are + of code lengths "cl". The code lengths used to describe this third tree are the code length code lengths ("clcl"). */ @@ -1743,159 +1815,140 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, HuffmanTree tree_ll; /*tree for lit,len values*/ HuffmanTree tree_d; /*tree for distance codes*/ HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/ - uivector frequencies_ll; /*frequency of lit,len codes*/ - uivector frequencies_d; /*frequency of dist codes*/ - uivector frequencies_cl; /*frequency of code length codes*/ - uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/ - uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/ - /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl - (these are written as is in the file, it would be crazy to compress these using yet another huffman - tree that needs to be represented by yet another set of code lengths)*/ - uivector bitlen_cl; + unsigned* frequencies_ll = 0; /*frequency of lit,len codes*/ + unsigned* frequencies_d = 0; /*frequency of dist codes*/ + unsigned* frequencies_cl = 0; /*frequency of code length codes*/ + unsigned* bitlen_lld = 0; /*lit,len,dist code lengths (int bits), literally (without repeat codes).*/ + unsigned* bitlen_lld_e = 0; /*bitlen_lld encoded with repeat codes (this is a rudimentary run length compression)*/ size_t datasize = dataend - datapos; /* - Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies: + If we could call "bitlen_cl" the the code length code lengths ("clcl"), that is the bit lengths of codes to represent + tree_cl in CLCL_ORDER, then due to the huffman compression of huffman tree representations ("two levels"), there are + some analogies: bitlen_lld is to tree_cl what data is to tree_ll and tree_d. bitlen_lld_e is to bitlen_lld what lz77_encoded is to data. bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded. */ unsigned BFINAL = final; - size_t numcodes_ll, numcodes_d, i; + size_t i; + size_t numcodes_ll, numcodes_d, numcodes_lld, numcodes_lld_e, numcodes_cl; unsigned HLIT, HDIST, HCLEN; uivector_init(&lz77_encoded); HuffmanTree_init(&tree_ll); HuffmanTree_init(&tree_d); HuffmanTree_init(&tree_cl); - uivector_init(&frequencies_ll); - uivector_init(&frequencies_d); - uivector_init(&frequencies_cl); - uivector_init(&bitlen_lld); - uivector_init(&bitlen_lld_e); - uivector_init(&bitlen_cl); + /* could fit on stack, but >1KB is on the larger side so allocate instead */ + frequencies_ll = (unsigned*)lodepng_malloc(286 * sizeof(*frequencies_ll)); + frequencies_d = (unsigned*)lodepng_malloc(30 * sizeof(*frequencies_d)); + frequencies_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(*frequencies_cl)); + + if(!frequencies_ll || !frequencies_d || !frequencies_cl) error = 83; /*alloc fail*/ /*This while loop never loops due to a break at the end, it is here to allow breaking out of it to the cleanup phase on error conditions.*/ - while(!error) - { - if(settings->use_lz77) - { + while(!error) { + lodepng_memset(frequencies_ll, 0, 286 * sizeof(*frequencies_ll)); + lodepng_memset(frequencies_d, 0, 30 * sizeof(*frequencies_d)); + lodepng_memset(frequencies_cl, 0, NUM_CODE_LENGTH_CODES * sizeof(*frequencies_cl)); + + if(settings->use_lz77) { error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, settings->minmatch, settings->nicematch, settings->lazymatching); if(error) break; - } - else - { + } else { if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/); for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/ } - if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/); - if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/); - /*Count the frequencies of lit, len and dist codes*/ - for(i = 0; i != lz77_encoded.size; ++i) - { + for(i = 0; i != lz77_encoded.size; ++i) { unsigned symbol = lz77_encoded.data[i]; - ++frequencies_ll.data[symbol]; - if(symbol > 256) - { + ++frequencies_ll[symbol]; + if(symbol > 256) { unsigned dist = lz77_encoded.data[i + 2]; - ++frequencies_d.data[dist]; + ++frequencies_d[dist]; i += 3; } } - frequencies_ll.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/ + frequencies_ll[256] = 1; /*there will be exactly 1 end code, at the end of the block*/ /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/ - error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.data, 257, frequencies_ll.size, 15); + error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll, 257, 286, 15); if(error) break; /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/ - error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.data, 2, frequencies_d.size, 15); + error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d, 2, 30, 15); if(error) break; - numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286; - numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30; + numcodes_ll = LODEPNG_MIN(tree_ll.numcodes, 286); + numcodes_d = LODEPNG_MIN(tree_d.numcodes, 30); /*store the code lengths of both generated trees in bitlen_lld*/ - for(i = 0; i != numcodes_ll; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i)); - for(i = 0; i != numcodes_d; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i)); + numcodes_lld = numcodes_ll + numcodes_d; + bitlen_lld = (unsigned*)lodepng_malloc(numcodes_lld * sizeof(*bitlen_lld)); + /*numcodes_lld_e never needs more size than bitlen_lld*/ + bitlen_lld_e = (unsigned*)lodepng_malloc(numcodes_lld * sizeof(*bitlen_lld_e)); + if(!bitlen_lld || !bitlen_lld_e) ERROR_BREAK(83); /*alloc fail*/ + numcodes_lld_e = 0; + + for(i = 0; i != numcodes_ll; ++i) bitlen_lld[i] = tree_ll.lengths[i]; + for(i = 0; i != numcodes_d; ++i) bitlen_lld[numcodes_ll + i] = tree_d.lengths[i]; /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times), 17 (3-10 zeroes), 18 (11-138 zeroes)*/ - for(i = 0; i != (unsigned)bitlen_lld.size; ++i) - { - unsigned j = 0; /*amount of repititions*/ - while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) ++j; + for(i = 0; i != numcodes_lld; ++i) { + unsigned j = 0; /*amount of repetitions*/ + while(i + j + 1 < numcodes_lld && bitlen_lld[i + j + 1] == bitlen_lld[i]) ++j; - if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/ - { + if(bitlen_lld[i] == 0 && j >= 2) /*repeat code for zeroes*/ { ++j; /*include the first zero*/ - if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ - { - uivector_push_back(&bitlen_lld_e, 17); - uivector_push_back(&bitlen_lld_e, j - 3); - } - else /*repeat code 18 supports max 138 zeroes*/ - { + if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ { + bitlen_lld_e[numcodes_lld_e++] = 17; + bitlen_lld_e[numcodes_lld_e++] = j - 3; + } else /*repeat code 18 supports max 138 zeroes*/ { if(j > 138) j = 138; - uivector_push_back(&bitlen_lld_e, 18); - uivector_push_back(&bitlen_lld_e, j - 11); + bitlen_lld_e[numcodes_lld_e++] = 18; + bitlen_lld_e[numcodes_lld_e++] = j - 11; } i += (j - 1); - } - else if(j >= 3) /*repeat code for value other than zero*/ - { + } else if(j >= 3) /*repeat code for value other than zero*/ { size_t k; - unsigned num = j / 6, rest = j % 6; - uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); - for(k = 0; k < num; ++k) - { - uivector_push_back(&bitlen_lld_e, 16); - uivector_push_back(&bitlen_lld_e, 6 - 3); + unsigned num = j / 6u, rest = j % 6u; + bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i]; + for(k = 0; k < num; ++k) { + bitlen_lld_e[numcodes_lld_e++] = 16; + bitlen_lld_e[numcodes_lld_e++] = 6 - 3; } - if(rest >= 3) - { - uivector_push_back(&bitlen_lld_e, 16); - uivector_push_back(&bitlen_lld_e, rest - 3); + if(rest >= 3) { + bitlen_lld_e[numcodes_lld_e++] = 16; + bitlen_lld_e[numcodes_lld_e++] = rest - 3; } else j -= rest; i += j; - } - else /*too short to benefit from repeat code*/ - { - uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); + } else /*too short to benefit from repeat code*/ { + bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i]; } } /*generate tree_cl, the huffmantree of huffmantrees*/ - - if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/); - for(i = 0; i != bitlen_lld_e.size; ++i) - { - ++frequencies_cl.data[bitlen_lld_e.data[i]]; + for(i = 0; i != numcodes_lld_e; ++i) { + ++frequencies_cl[bitlen_lld_e[i]]; /*after a repeat code come the bits that specify the number of repetitions, those don't need to be in the frequencies_cl calculation*/ - if(bitlen_lld_e.data[i] >= 16) ++i; + if(bitlen_lld_e[i] >= 16) ++i; } - error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data, - frequencies_cl.size, frequencies_cl.size, 7); + error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl, + NUM_CODE_LENGTH_CODES, NUM_CODE_LENGTH_CODES, 7); if(error) break; - if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/); - for(i = 0; i != tree_cl.numcodes; ++i) - { - /*lenghts of code length tree is in the order as specified by deflate*/ - bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]); + /*compute amount of code-length-code-lengths to output*/ + numcodes_cl = NUM_CODE_LENGTH_CODES; + /*trim zeros at the end (using CLCL_ORDER), but minimum size must be 4 (see HCLEN below)*/ + while(numcodes_cl > 4u && tree_cl.lengths[CLCL_ORDER[numcodes_cl - 1u]] == 0) { + numcodes_cl--; } - while(bitlen_cl.data[bitlen_cl.size - 1] == 0 && bitlen_cl.size > 4) - { - /*remove zeros at the end, but minimum size must be 4*/ - if(!uivector_resize(&bitlen_cl, bitlen_cl.size - 1)) ERROR_BREAK(83 /*alloc fail*/); - } - if(error) break; /* Write everything into the output @@ -1903,7 +1956,7 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, After the BFINAL and BTYPE, the dynamic block consists out of the following: - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN - (HCLEN+4)*3 bits code lengths of code length alphabet - - HLIT + 257 code lenghts of lit/length alphabet (encoded using the code length + - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length alphabet, + possible repetition codes 16, 17, 18) - HDIST + 1 code lengths of distance alphabet (encoded using the code length alphabet, + possible repetition codes 16, 17, 18) @@ -1912,41 +1965,39 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, */ /*Write block type*/ - addBitToStream(bp, out, BFINAL); - addBitToStream(bp, out, 0); /*first bit of BTYPE "dynamic"*/ - addBitToStream(bp, out, 1); /*second bit of BTYPE "dynamic"*/ + writeBits(writer, BFINAL, 1); + writeBits(writer, 0, 1); /*first bit of BTYPE "dynamic"*/ + writeBits(writer, 1, 1); /*second bit of BTYPE "dynamic"*/ /*write the HLIT, HDIST and HCLEN values*/ + /*all three sizes take trimmed ending zeroes into account, done either by HuffmanTree_makeFromFrequencies + or in the loop for numcodes_cl above, which saves space. */ HLIT = (unsigned)(numcodes_ll - 257); HDIST = (unsigned)(numcodes_d - 1); - HCLEN = (unsigned)bitlen_cl.size - 4; - /*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/ - while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) --HCLEN; - addBitsToStream(bp, out, HLIT, 5); - addBitsToStream(bp, out, HDIST, 5); - addBitsToStream(bp, out, HCLEN, 4); - - /*write the code lenghts of the code length alphabet*/ - for(i = 0; i != HCLEN + 4; ++i) addBitsToStream(bp, out, bitlen_cl.data[i], 3); - - /*write the lenghts of the lit/len AND the dist alphabet*/ - for(i = 0; i != bitlen_lld_e.size; ++i) - { - addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]), - HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i])); + HCLEN = (unsigned)(numcodes_cl - 4); + writeBits(writer, HLIT, 5); + writeBits(writer, HDIST, 5); + writeBits(writer, HCLEN, 4); + + /*write the code lengths of the code length alphabet ("bitlen_cl")*/ + for(i = 0; i != numcodes_cl; ++i) writeBits(writer, tree_cl.lengths[CLCL_ORDER[i]], 3); + + /*write the lengths of the lit/len AND the dist alphabet*/ + for(i = 0; i != numcodes_lld_e; ++i) { + writeBitsReversed(writer, tree_cl.codes[bitlen_lld_e[i]], tree_cl.lengths[bitlen_lld_e[i]]); /*extra bits of repeat codes*/ - if(bitlen_lld_e.data[i] == 16) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 2); - else if(bitlen_lld_e.data[i] == 17) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 3); - else if(bitlen_lld_e.data[i] == 18) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 7); + if(bitlen_lld_e[i] == 16) writeBits(writer, bitlen_lld_e[++i], 2); + else if(bitlen_lld_e[i] == 17) writeBits(writer, bitlen_lld_e[++i], 3); + else if(bitlen_lld_e[i] == 18) writeBits(writer, bitlen_lld_e[++i], 7); } /*write the compressed data symbols*/ - writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); + writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d); /*error: the length of the end code 256 must be larger than 0*/ - if(HuffmanTree_getLength(&tree_ll, 256) == 0) ERROR_BREAK(64); + if(tree_ll.lengths[256] == 0) ERROR_BREAK(64); /*write the end code*/ - addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); + writeBitsReversed(writer, tree_ll.codes[256], tree_ll.lengths[256]); break; /*end of error-while*/ } @@ -1956,21 +2007,19 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, HuffmanTree_cleanup(&tree_ll); HuffmanTree_cleanup(&tree_d); HuffmanTree_cleanup(&tree_cl); - uivector_cleanup(&frequencies_ll); - uivector_cleanup(&frequencies_d); - uivector_cleanup(&frequencies_cl); - uivector_cleanup(&bitlen_lld_e); - uivector_cleanup(&bitlen_lld); - uivector_cleanup(&bitlen_cl); + lodepng_free(frequencies_ll); + lodepng_free(frequencies_d); + lodepng_free(frequencies_cl); + lodepng_free(bitlen_lld); + lodepng_free(bitlen_lld_e); return error; } -static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash, +static unsigned deflateFixed(LodePNGBitWriter* writer, Hash* hash, const unsigned char* data, size_t datapos, size_t dataend, - const LodePNGCompressSettings* settings, unsigned final) -{ + const LodePNGCompressSettings* settings, unsigned final) { HuffmanTree tree_ll; /*tree for literal values and length codes*/ HuffmanTree tree_d; /*tree for distance codes*/ @@ -1981,31 +2030,29 @@ static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash, HuffmanTree_init(&tree_ll); HuffmanTree_init(&tree_d); - generateFixedLitLenTree(&tree_ll); - generateFixedDistanceTree(&tree_d); + error = generateFixedLitLenTree(&tree_ll); + if(!error) error = generateFixedDistanceTree(&tree_d); - addBitToStream(bp, out, BFINAL); - addBitToStream(bp, out, 1); /*first bit of BTYPE*/ - addBitToStream(bp, out, 0); /*second bit of BTYPE*/ + if(!error) { + writeBits(writer, BFINAL, 1); + writeBits(writer, 1, 1); /*first bit of BTYPE*/ + writeBits(writer, 0, 1); /*second bit of BTYPE*/ - if(settings->use_lz77) /*LZ77 encoded*/ - { - uivector lz77_encoded; - uivector_init(&lz77_encoded); - error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, - settings->minmatch, settings->nicematch, settings->lazymatching); - if(!error) writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); - uivector_cleanup(&lz77_encoded); - } - else /*no LZ77, but still will be Huffman compressed*/ - { - for(i = datapos; i < dataend; ++i) - { - addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i])); + if(settings->use_lz77) /*LZ77 encoded*/ { + uivector lz77_encoded; + uivector_init(&lz77_encoded); + error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, + settings->minmatch, settings->nicematch, settings->lazymatching); + if(!error) writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d); + uivector_cleanup(&lz77_encoded); + } else /*no LZ77, but still will be Huffman compressed*/ { + for(i = datapos; i < dataend; ++i) { + writeBitsReversed(writer, tree_ll.codes[data[i]], tree_ll.lengths[data[i]]); + } } + /*add END code*/ + if(!error) writeBitsReversed(writer,tree_ll.codes[256], tree_ll.lengths[256]); } - /*add END code*/ - if(!error) addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); /*cleanup*/ HuffmanTree_cleanup(&tree_ll); @@ -2015,20 +2062,20 @@ static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash, } static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize, - const LodePNGCompressSettings* settings) -{ + const LodePNGCompressSettings* settings) { unsigned error = 0; size_t i, blocksize, numdeflateblocks; - size_t bp = 0; /*the bit pointer*/ Hash hash; + LodePNGBitWriter writer; + + LodePNGBitWriter_init(&writer, out); if(settings->btype > 2) return 61; else if(settings->btype == 0) return deflateNoCompression(out, in, insize); else if(settings->btype == 1) blocksize = insize; - else /*if(settings->btype == 2)*/ - { + else /*if(settings->btype == 2)*/ { /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/ - blocksize = insize / 8 + 8; + blocksize = insize / 8u + 8; if(blocksize < 65536) blocksize = 65536; if(blocksize > 262144) blocksize = 262144; } @@ -2037,17 +2084,17 @@ static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t if(numdeflateblocks == 0) numdeflateblocks = 1; error = hash_init(&hash, settings->windowsize); - if(error) return error; - for(i = 0; i != numdeflateblocks && !error; ++i) - { - unsigned final = (i == numdeflateblocks - 1); - size_t start = i * blocksize; - size_t end = start + blocksize; - if(end > insize) end = insize; + if(!error) { + for(i = 0; i != numdeflateblocks && !error; ++i) { + unsigned final = (i == numdeflateblocks - 1); + size_t start = i * blocksize; + size_t end = start + blocksize; + if(end > insize) end = insize; - if(settings->btype == 1) error = deflateFixed(out, &bp, &hash, in, start, end, settings, final); - else if(settings->btype == 2) error = deflateDynamic(out, &bp, &hash, in, start, end, settings, final); + if(settings->btype == 1) error = deflateFixed(&writer, &hash, in, start, end, settings, final); + else if(settings->btype == 2) error = deflateDynamic(&writer, &hash, in, start, end, settings, final); + } } hash_cleanup(&hash); @@ -2057,12 +2104,9 @@ static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t unsigned lodepng_deflate(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, - const LodePNGCompressSettings* settings) -{ - unsigned error; - ucvector v; - ucvector_init_buffer(&v, *out, *outsize); - error = lodepng_deflatev(&v, in, insize, settings); + const LodePNGCompressSettings* settings) { + ucvector v = ucvector_init(*out, *outsize); + unsigned error = lodepng_deflatev(&v, in, insize, settings); *out = v.data; *outsize = v.size; return error; @@ -2070,14 +2114,10 @@ unsigned lodepng_deflate(unsigned char** out, size_t* outsize, static unsigned deflate(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, - const LodePNGCompressSettings* settings) -{ - if(settings->custom_deflate) - { + const LodePNGCompressSettings* settings) { + if(settings->custom_deflate) { return settings->custom_deflate(out, outsize, in, insize, settings); - } - else - { + } else { return lodepng_deflate(out, outsize, in, insize, settings); } } @@ -2085,36 +2125,32 @@ static unsigned deflate(unsigned char** out, size_t* outsize, #endif /*LODEPNG_COMPILE_DECODER*/ /* ////////////////////////////////////////////////////////////////////////// */ -/* / Adler32 */ +/* / Adler32 / */ /* ////////////////////////////////////////////////////////////////////////// */ -static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) -{ - unsigned s1 = adler & 0xffff; - unsigned s2 = (adler >> 16) & 0xffff; +static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) { + unsigned s1 = adler & 0xffffu; + unsigned s2 = (adler >> 16u) & 0xffffu; - while(len > 0) - { - /*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/ - unsigned amount = len > 5550 ? 5550 : len; + while(len != 0u) { + unsigned i; + /*at least 5552 sums can be done before the sums overflow, saving a lot of module divisions*/ + unsigned amount = len > 5552u ? 5552u : len; len -= amount; - while(amount > 0) - { + for(i = 0; i != amount; ++i) { s1 += (*data++); s2 += s1; - --amount; } - s1 %= 65521; - s2 %= 65521; + s1 %= 65521u; + s2 %= 65521u; } - return (s2 << 16) | s1; + return (s2 << 16u) | s1; } /*Return the adler32 of the bytes data[0..len-1]*/ -static unsigned adler32(const unsigned char* data, unsigned len) -{ - return update_adler32(1L, data, len); +static unsigned adler32(const unsigned char* data, unsigned len) { + return update_adler32(1u, data, len); } /* ////////////////////////////////////////////////////////////////////////// */ @@ -2123,16 +2159,15 @@ static unsigned adler32(const unsigned char* data, unsigned len) #ifdef LODEPNG_COMPILE_DECODER -unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGDecompressSettings* settings) -{ +static unsigned lodepng_zlib_decompressv(ucvector* out, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings) { unsigned error = 0; unsigned CM, CINFO, FDICT; if(insize < 2) return 53; /*error, size of zlib data too small*/ /*read information from zlib header*/ - if((in[0] * 256 + in[1]) % 31 != 0) - { + if((in[0] * 256 + in[1]) % 31 != 0) { /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/ return 24; } @@ -2143,41 +2178,55 @@ unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const uns FDICT = (in[1] >> 5) & 1; /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/ - if(CM != 8 || CINFO > 7) - { + if(CM != 8 || CINFO > 7) { /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/ return 25; } - if(FDICT != 0) - { + if(FDICT != 0) { /*error: the specification of PNG says about the zlib stream: "The additional flags shall not specify a preset dictionary."*/ return 26; } - error = inflate(out, outsize, in + 2, insize - 2, settings); + error = inflatev(out, in + 2, insize - 2, settings); if(error) return error; - if(!settings->ignore_adler32) - { + if(!settings->ignore_adler32) { unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]); - unsigned checksum = adler32(*out, (unsigned)(*outsize)); + unsigned checksum = adler32(out->data, (unsigned)(out->size)); if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/ } return 0; /*no error*/ } -static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGDecompressSettings* settings) -{ - if(settings->custom_zlib) - { + +unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGDecompressSettings* settings) { + ucvector v = ucvector_init(*out, *outsize); + unsigned error = lodepng_zlib_decompressv(&v, in, insize, settings); + *out = v.data; + *outsize = v.size; + return error; +} + +/*expected_size is expected output size, to avoid intermediate allocations. Set to 0 if not known. */ +static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size, + const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings) { + if(settings->custom_zlib) { return settings->custom_zlib(out, outsize, in, insize, settings); - } - else - { - return lodepng_zlib_decompress(out, outsize, in, insize, settings); + } else { + unsigned error; + ucvector v = ucvector_init(*out, *outsize); + if(expected_size) { + /*reserve the memory to avoid intermediate reallocations*/ + ucvector_resize(&v, *outsize + expected_size); + v.size = *outsize; + } + error = lodepng_zlib_decompressv(&v, in, insize, settings); + *out = v.data; + *outsize = v.size; + return error; } } @@ -2186,56 +2235,48 @@ static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsi #ifdef LODEPNG_COMPILE_ENCODER unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGCompressSettings* settings) -{ - /*initially, *out must be NULL and outsize 0, if you just give some random *out - that's pointing to a non allocated buffer, this'll crash*/ - ucvector outv; + size_t insize, const LodePNGCompressSettings* settings) { size_t i; unsigned error; unsigned char* deflatedata = 0; size_t deflatesize = 0; - /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/ - unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/ - unsigned FLEVEL = 0; - unsigned FDICT = 0; - unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64; - unsigned FCHECK = 31 - CMFFLG % 31; - CMFFLG += FCHECK; - - /*ucvector-controlled version of the output buffer, for dynamic array*/ - ucvector_init_buffer(&outv, *out, *outsize); - - ucvector_push_back(&outv, (unsigned char)(CMFFLG >> 8)); - ucvector_push_back(&outv, (unsigned char)(CMFFLG & 255)); - error = deflate(&deflatedata, &deflatesize, in, insize, settings); - if(!error) - { - unsigned ADLER32 = adler32(in, (unsigned)insize); - for(i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]); - lodepng_free(deflatedata); - lodepng_add32bitInt(&outv, ADLER32); + *out = NULL; + *outsize = 0; + if(!error) { + *outsize = deflatesize + 6; + *out = (unsigned char*)lodepng_malloc(*outsize); + if(!*out) error = 83; /*alloc fail*/ } - *out = outv.data; - *outsize = outv.size; + if(!error) { + unsigned ADLER32 = adler32(in, (unsigned)insize); + /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/ + unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/ + unsigned FLEVEL = 0; + unsigned FDICT = 0; + unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64; + unsigned FCHECK = 31 - CMFFLG % 31; + CMFFLG += FCHECK; + + (*out)[0] = (unsigned char)(CMFFLG >> 8); + (*out)[1] = (unsigned char)(CMFFLG & 255); + for(i = 0; i != deflatesize; ++i) (*out)[i + 2] = deflatedata[i]; + lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32); + } + lodepng_free(deflatedata); return error; } /* compress using the default or custom zlib function */ static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGCompressSettings* settings) -{ - if(settings->custom_zlib) - { + size_t insize, const LodePNGCompressSettings* settings) { + if(settings->custom_zlib) { return settings->custom_zlib(out, outsize, in, insize, settings); - } - else - { + } else { return lodepng_zlib_compress(out, outsize, in, insize, settings); } } @@ -2245,17 +2286,16 @@ static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsign #else /*no LODEPNG_COMPILE_ZLIB*/ #ifdef LODEPNG_COMPILE_DECODER -static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGDecompressSettings* settings) -{ +static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size, + const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings) { if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ + (void)expected_size; return settings->custom_zlib(out, outsize, in, insize, settings); } #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, - size_t insize, const LodePNGCompressSettings* settings) -{ + size_t insize, const LodePNGCompressSettings* settings) { if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ return settings->custom_zlib(out, outsize, in, insize, settings); } @@ -2270,8 +2310,7 @@ static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsign /*this is a good tradeoff between speed and compression ratio*/ #define DEFAULT_WINDOWSIZE 2048 -void lodepng_compress_settings_init(LodePNGCompressSettings* settings) -{ +void lodepng_compress_settings_init(LodePNGCompressSettings* settings) { /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/ settings->btype = 2; settings->use_lz77 = 1; @@ -2292,16 +2331,16 @@ const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT #ifdef LODEPNG_COMPILE_DECODER -void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) -{ +void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) { settings->ignore_adler32 = 0; + settings->ignore_nlen = 0; settings->custom_zlib = 0; settings->custom_inflate = 0; settings->custom_context = 0; } -const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0}; +const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0, 0}; #endif /*LODEPNG_COMPILE_DECODER*/ @@ -2356,13 +2395,11 @@ static unsigned lodepng_crc32_table[256] = { }; /*Return the CRC of the bytes buf[0..len-1].*/ -unsigned lodepng_crc32(const unsigned char* data, size_t length) -{ +unsigned lodepng_crc32(const unsigned char* data, size_t length) { unsigned r = 0xffffffffu; size_t i; - for(i = 0; i < length; ++i) - { - r = lodepng_crc32_table[(r ^ data[i]) & 0xff] ^ (r >> 8); + for(i = 0; i < length; ++i) { + r = lodepng_crc32_table[(r ^ data[i]) & 0xffu] ^ (r >> 8u); } return r ^ 0xffffffffu; } @@ -2371,46 +2408,33 @@ unsigned lodepng_crc32(const unsigned char* data, size_t length); #endif /* !LODEPNG_NO_COMPILE_CRC */ /* ////////////////////////////////////////////////////////////////////////// */ -/* / Reading and writing single bits and bytes from/to stream for LodePNG / */ +/* / Reading and writing PNG color channel bits / */ /* ////////////////////////////////////////////////////////////////////////// */ -static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) -{ +/* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first, +so LodePNGBitWriter and LodePNGBitReader can't be used for those. */ + +static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) { unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1); ++(*bitpointer); return result; } -static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) -{ +/* TODO: make this faster */ +static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) { unsigned result = 0; size_t i; - for(i = 0 ; i < nbits; ++i) - { - result <<= 1; + for(i = 0 ; i < nbits; ++i) { + result <<= 1u; result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream); } return result; } -#ifdef LODEPNG_COMPILE_DECODER -static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) -{ - /*the current bit in bitstream must be 0 for this to work*/ - if(bit) - { - /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/ - bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); - } - ++(*bitpointer); -} -#endif /*LODEPNG_COMPILE_DECODER*/ - -static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) -{ +static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) { /*the current bit in bitstream may be 0 or 1 for this to work*/ - if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7)))); - else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7))); + if(bit == 0) bitstream[(*bitpointer) >> 3u] &= (unsigned char)(~(1u << (7u - ((*bitpointer) & 7u)))); + else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u))); ++(*bitpointer); } @@ -2418,51 +2442,42 @@ static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, /* / PNG chunks / */ /* ////////////////////////////////////////////////////////////////////////// */ -unsigned lodepng_chunk_length(const unsigned char* chunk) -{ +unsigned lodepng_chunk_length(const unsigned char* chunk) { return lodepng_read32bitInt(&chunk[0]); } -void lodepng_chunk_type(char type[5], const unsigned char* chunk) -{ +void lodepng_chunk_type(char type[5], const unsigned char* chunk) { unsigned i; for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i]; type[4] = 0; /*null termination char*/ } -unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) -{ - if(strlen(type) != 4) return 0; +unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) { + if(lodepng_strlen(type) != 4) return 0; return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]); } -unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) -{ +unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) { return((chunk[4] & 32) != 0); } -unsigned char lodepng_chunk_private(const unsigned char* chunk) -{ +unsigned char lodepng_chunk_private(const unsigned char* chunk) { return((chunk[6] & 32) != 0); } -unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) -{ +unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) { return((chunk[7] & 32) != 0); } -unsigned char* lodepng_chunk_data(unsigned char* chunk) -{ +unsigned char* lodepng_chunk_data(unsigned char* chunk) { return &chunk[8]; } -const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) -{ +const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) { return &chunk[8]; } -unsigned lodepng_chunk_check_crc(const unsigned char* chunk) -{ +unsigned lodepng_chunk_check_crc(const unsigned char* chunk) { unsigned length = lodepng_chunk_length(chunk); unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]); /*the CRC is taken of the data and the 4 chunk type letters, not the length*/ @@ -2471,37 +2486,72 @@ unsigned lodepng_chunk_check_crc(const unsigned char* chunk) else return 0; } -void lodepng_chunk_generate_crc(unsigned char* chunk) -{ +void lodepng_chunk_generate_crc(unsigned char* chunk) { unsigned length = lodepng_chunk_length(chunk); unsigned CRC = lodepng_crc32(&chunk[4], length + 4); lodepng_set32bitInt(chunk + 8 + length, CRC); } -unsigned char* lodepng_chunk_next(unsigned char* chunk) -{ - unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; - return &chunk[total_chunk_length]; +unsigned char* lodepng_chunk_next(unsigned char* chunk, unsigned char* end) { + if(chunk >= end || end - chunk < 12) return end; /*too small to contain a chunk*/ + if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47 + && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) { + /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */ + return chunk + 8; + } else { + size_t total_chunk_length; + unsigned char* result; + if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return end; + result = chunk + total_chunk_length; + if(result < chunk) return end; /*pointer overflow*/ + return result; + } +} + +const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk, const unsigned char* end) { + if(chunk >= end || end - chunk < 12) return end; /*too small to contain a chunk*/ + if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47 + && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) { + /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */ + return chunk + 8; + } else { + size_t total_chunk_length; + const unsigned char* result; + if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return end; + result = chunk + total_chunk_length; + if(result < chunk) return end; /*pointer overflow*/ + return result; + } +} + +unsigned char* lodepng_chunk_find(unsigned char* chunk, unsigned char* end, const char type[5]) { + for(;;) { + if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ + if(lodepng_chunk_type_equals(chunk, type)) return chunk; + chunk = lodepng_chunk_next(chunk, end); + } } -const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk) -{ - unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; - return &chunk[total_chunk_length]; +const unsigned char* lodepng_chunk_find_const(const unsigned char* chunk, const unsigned char* end, const char type[5]) { + for(;;) { + if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ + if(lodepng_chunk_type_equals(chunk, type)) return chunk; + chunk = lodepng_chunk_next_const(chunk, end); + } } -unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk) -{ +unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsigned char* chunk) { unsigned i; - unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; + size_t total_chunk_length, new_length; unsigned char *chunk_start, *new_buffer; - size_t new_length = (*outlength) + total_chunk_length; - if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/ + + if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return 77; + if(lodepng_addofl(*outsize, total_chunk_length, &new_length)) return 77; new_buffer = (unsigned char*)lodepng_realloc(*out, new_length); if(!new_buffer) return 83; /*alloc fail*/ (*out) = new_buffer; - (*outlength) = new_length; + (*outsize) = new_length; chunk_start = &(*out)[new_length - total_chunk_length]; for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i]; @@ -2509,30 +2559,36 @@ unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsi return 0; } -unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, - const char* type, const unsigned char* data) -{ - unsigned i; - unsigned char *chunk, *new_buffer; - size_t new_length = (*outlength) + length + 12; - if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/ - new_buffer = (unsigned char*)lodepng_realloc(*out, new_length); - if(!new_buffer) return 83; /*alloc fail*/ - (*out) = new_buffer; - (*outlength) = new_length; - chunk = &(*out)[(*outlength) - length - 12]; +/*Sets length and name and allocates the space for data and crc but does not +set data or crc yet. Returns the start of the chunk in chunk. The start of +the data is at chunk + 8. To finalize chunk, add the data, then use +lodepng_chunk_generate_crc */ +static unsigned lodepng_chunk_init(unsigned char** chunk, + ucvector* out, + unsigned length, const char* type) { + size_t new_length = out->size; + if(lodepng_addofl(new_length, length, &new_length)) return 77; + if(lodepng_addofl(new_length, 12, &new_length)) return 77; + if(!ucvector_resize(out, new_length)) return 83; /*alloc fail*/ + *chunk = out->data + new_length - length - 12u; /*1: length*/ - lodepng_set32bitInt(chunk, (unsigned)length); + lodepng_set32bitInt(*chunk, length); /*2: chunk name (4 letters)*/ - chunk[4] = (unsigned char)type[0]; - chunk[5] = (unsigned char)type[1]; - chunk[6] = (unsigned char)type[2]; - chunk[7] = (unsigned char)type[3]; + lodepng_memcpy(*chunk + 4, type, 4); + + return 0; +} + +/* like lodepng_chunk_create but with custom allocsize */ +static unsigned lodepng_chunk_createv(ucvector* out, + unsigned length, const char* type, const unsigned char* data) { + unsigned char* chunk; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, length, type)); /*3: the data*/ - for(i = 0; i != length; ++i) chunk[8 + i] = data[i]; + lodepng_memcpy(chunk + 8, data, length); /*4: CRC (of the chunkname characters and the data)*/ lodepng_chunk_generate_crc(chunk); @@ -2540,48 +2596,54 @@ unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned l return 0; } +unsigned lodepng_chunk_create(unsigned char** out, size_t* outsize, + unsigned length, const char* type, const unsigned char* data) { + ucvector v = ucvector_init(*out, *outsize); + unsigned error = lodepng_chunk_createv(&v, length, type, data); + *out = v.data; + *outsize = v.size; + return error; +} + /* ////////////////////////////////////////////////////////////////////////// */ -/* / Color types and such / */ +/* / Color types, channels, bits / */ /* ////////////////////////////////////////////////////////////////////////// */ -/*return type is a LodePNG error code*/ -static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) /*bd = bitdepth*/ -{ - switch(colortype) - { - case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/ - case 2: if(!( bd == 8 || bd == 16)) return 37; break; /*RGB*/ - case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/ - case 4: if(!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/ - case 6: if(!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/ - default: return 31; +/*checks if the colortype is valid and the bitdepth bd is allowed for this colortype. +Return value is a LodePNG error code.*/ +static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) { + switch(colortype) { + case LCT_GREY: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; + case LCT_RGB: if(!( bd == 8 || bd == 16)) return 37; break; + case LCT_PALETTE: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; + case LCT_GREY_ALPHA: if(!( bd == 8 || bd == 16)) return 37; break; + case LCT_RGBA: if(!( bd == 8 || bd == 16)) return 37; break; + case LCT_MAX_OCTET_VALUE: return 31; /* invalid color type */ + default: return 31; /* invalid color type */ } return 0; /*allowed color type / bits combination*/ } -static unsigned getNumColorChannels(LodePNGColorType colortype) -{ - switch(colortype) - { - case 0: return 1; /*grey*/ - case 2: return 3; /*RGB*/ - case 3: return 1; /*palette*/ - case 4: return 2; /*grey + alpha*/ - case 6: return 4; /*RGBA*/ +static unsigned getNumColorChannels(LodePNGColorType colortype) { + switch(colortype) { + case LCT_GREY: return 1; + case LCT_RGB: return 3; + case LCT_PALETTE: return 1; + case LCT_GREY_ALPHA: return 2; + case LCT_RGBA: return 4; + case LCT_MAX_OCTET_VALUE: return 0; /* invalid color type */ + default: return 0; /*invalid color type*/ } - return 0; /*unexisting color type*/ } -static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) -{ +static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) { /*bits per pixel is amount of channels * bits per channel*/ return getNumColorChannels(colortype) * bitdepth; } /* ////////////////////////////////////////////////////////////////////////// */ -void lodepng_color_mode_init(LodePNGColorMode* info) -{ +void lodepng_color_mode_init(LodePNGColorMode* info) { info->key_defined = 0; info->key_r = info->key_g = info->key_b = 0; info->colortype = LCT_RGBA; @@ -2590,69 +2652,78 @@ void lodepng_color_mode_init(LodePNGColorMode* info) info->palettesize = 0; } -void lodepng_color_mode_cleanup(LodePNGColorMode* info) -{ +/*allocates palette memory if needed, and initializes all colors to black*/ +static void lodepng_color_mode_alloc_palette(LodePNGColorMode* info) { + size_t i; + /*if the palette is already allocated, it will have size 1024 so no reallocation needed in that case*/ + /*the palette must have room for up to 256 colors with 4 bytes each.*/ + if(!info->palette) info->palette = (unsigned char*)lodepng_malloc(1024); + if(!info->palette) return; /*alloc fail*/ + for(i = 0; i != 256; ++i) { + /*Initialize all unused colors with black, the value used for invalid palette indices. + This is an error according to the PNG spec, but common PNG decoders make it black instead. + That makes color conversion slightly faster due to no error handling needed.*/ + info->palette[i * 4 + 0] = 0; + info->palette[i * 4 + 1] = 0; + info->palette[i * 4 + 2] = 0; + info->palette[i * 4 + 3] = 255; + } +} + +void lodepng_color_mode_cleanup(LodePNGColorMode* info) { lodepng_palette_clear(info); } -unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) -{ - size_t i; +unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) { lodepng_color_mode_cleanup(dest); - *dest = *source; - if(source->palette) - { + lodepng_memcpy(dest, source, sizeof(LodePNGColorMode)); + if(source->palette) { dest->palette = (unsigned char*)lodepng_malloc(1024); if(!dest->palette && source->palettesize) return 83; /*alloc fail*/ - for(i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i]; + lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4); } return 0; } -static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) -{ +LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth) { + LodePNGColorMode result; + lodepng_color_mode_init(&result); + result.colortype = colortype; + result.bitdepth = bitdepth; + return result; +} + +static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) { size_t i; if(a->colortype != b->colortype) return 0; if(a->bitdepth != b->bitdepth) return 0; if(a->key_defined != b->key_defined) return 0; - if(a->key_defined) - { + if(a->key_defined) { if(a->key_r != b->key_r) return 0; if(a->key_g != b->key_g) return 0; if(a->key_b != b->key_b) return 0; } - /*if one of the palette sizes is 0, then we consider it to be the same as the - other: it means that e.g. the palette was not given by the user and should be - considered the same as the palette inside the PNG.*/ - if(1/*a->palettesize != 0 && b->palettesize != 0*/) { - if(a->palettesize != b->palettesize) return 0; - for(i = 0; i != a->palettesize * 4; ++i) - { - if(a->palette[i] != b->palette[i]) return 0; - } + if(a->palettesize != b->palettesize) return 0; + for(i = 0; i != a->palettesize * 4; ++i) { + if(a->palette[i] != b->palette[i]) return 0; } return 1; } -void lodepng_palette_clear(LodePNGColorMode* info) -{ +void lodepng_palette_clear(LodePNGColorMode* info) { if(info->palette) lodepng_free(info->palette); info->palette = 0; info->palettesize = 0; } unsigned lodepng_palette_add(LodePNGColorMode* info, - unsigned char r, unsigned char g, unsigned char b, unsigned char a) -{ - unsigned char* data; - /*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with - the max of 256 colors, it'll have the exact alloc size*/ - if(!info->palette) /*allocate palette if empty*/ - { - /*room for 256 colors with 4 bytes each*/ - data = (unsigned char*)lodepng_realloc(info->palette, 1024); - if(!data) return 83; /*alloc fail*/ - else info->palette = data; + unsigned char r, unsigned char g, unsigned char b, unsigned char a) { + if(!info->palette) /*allocate palette if empty*/ { + lodepng_color_mode_alloc_palette(info); + if(!info->palette) return 83; /*alloc fail*/ + } + if(info->palettesize >= 256) { + return 108; /*too many palette values*/ } info->palette[4 * info->palettesize + 0] = r; info->palette[4 * info->palettesize + 1] = g; @@ -2662,108 +2733,118 @@ unsigned lodepng_palette_add(LodePNGColorMode* info, return 0; } -unsigned lodepng_get_bpp(const LodePNGColorMode* info) -{ - /*calculate bits per pixel out of colortype and bitdepth*/ +/*calculate bits per pixel out of colortype and bitdepth*/ +unsigned lodepng_get_bpp(const LodePNGColorMode* info) { return lodepng_get_bpp_lct(info->colortype, info->bitdepth); } -unsigned lodepng_get_channels(const LodePNGColorMode* info) -{ +unsigned lodepng_get_channels(const LodePNGColorMode* info) { return getNumColorChannels(info->colortype); } -unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) -{ +unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) { return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA; } -unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) -{ +unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) { return (info->colortype & 4) != 0; /*4 or 6*/ } -unsigned lodepng_is_palette_type(const LodePNGColorMode* info) -{ +unsigned lodepng_is_palette_type(const LodePNGColorMode* info) { return info->colortype == LCT_PALETTE; } -unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) -{ +unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) { size_t i; - for(i = 0; i != info->palettesize; ++i) - { + for(i = 0; i != info->palettesize; ++i) { if(info->palette[i * 4 + 3] < 255) return 1; } return 0; } -unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) -{ +unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) { return info->key_defined || lodepng_is_alpha_type(info) || lodepng_has_palette_alpha(info); } -size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) -{ - /*will not overflow for any color type if roughly w * h < 268435455*/ - size_t bpp = lodepng_get_bpp(color); - size_t n = w * h; - return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; +static size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) { + size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth); + size_t n = (size_t)w * (size_t)h; + return ((n / 8u) * bpp) + ((n & 7u) * bpp + 7u) / 8u; } -size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) -{ - /*will not overflow for any color type if roughly w * h < 268435455*/ - size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth); - size_t n = w * h; - return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; +size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) { + return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth); } #ifdef LODEPNG_COMPILE_PNG + +/*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer, +and in addition has one extra byte per line: the filter byte. So this gives a larger +result than lodepng_get_raw_size. Set h to 1 to get the size of 1 row including filter byte. */ +static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, unsigned bpp) { + /* + 1 for the filter byte, and possibly plus padding bits per line. */ + /* Ignoring casts, the expression is equal to (w * bpp + 7) / 8 + 1, but avoids overflow of w * bpp */ + size_t line = ((size_t)(w / 8u) * bpp) + 1u + ((w & 7u) * bpp + 7u) / 8u; + return (size_t)h * line; +} + #ifdef LODEPNG_COMPILE_DECODER -/*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer*/ -static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, const LodePNGColorMode* color) -{ - /*will not overflow for any color type if roughly w * h < 268435455*/ - size_t bpp = lodepng_get_bpp(color); - size_t line = ((w / 8) * bpp) + ((w & 7) * bpp + 7) / 8; - return h * line; +/*Safely checks whether size_t overflow can be caused due to amount of pixels. +This check is overcautious rather than precise. If this check indicates no overflow, +you can safely compute in a size_t (but not an unsigned): +-(size_t)w * (size_t)h * 8 +-amount of bytes in IDAT (including filter, padding and Adam7 bytes) +-amount of bytes in raw color model +Returns 1 if overflow possible, 0 if not. +*/ +static int lodepng_pixel_overflow(unsigned w, unsigned h, + const LodePNGColorMode* pngcolor, const LodePNGColorMode* rawcolor) { + size_t bpp = LODEPNG_MAX(lodepng_get_bpp(pngcolor), lodepng_get_bpp(rawcolor)); + size_t numpixels, total; + size_t line; /* bytes per line in worst case */ + + if(lodepng_mulofl((size_t)w, (size_t)h, &numpixels)) return 1; + if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per channel color */ + + /* Bytes per scanline with the expression "(w / 8u) * bpp) + ((w & 7u) * bpp + 7u) / 8u" */ + if(lodepng_mulofl((size_t)(w / 8u), bpp, &line)) return 1; + if(lodepng_addofl(line, ((w & 7u) * bpp + 7u) / 8u, &line)) return 1; + + if(lodepng_addofl(line, 5, &line)) return 1; /* 5 bytes overhead per line: 1 filterbyte, 4 for Adam7 worst case */ + if(lodepng_mulofl(line, h, &total)) return 1; /* Total bytes in worst case */ + + return 0; /* no overflow */ } #endif /*LODEPNG_COMPILE_DECODER*/ #endif /*LODEPNG_COMPILE_PNG*/ #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -static void LodePNGUnknownChunks_init(LodePNGInfo* info) -{ +static void LodePNGUnknownChunks_init(LodePNGInfo* info) { unsigned i; for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0; for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0; } -static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) -{ +static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) { unsigned i; for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]); } -static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) -{ +static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) { unsigned i; LodePNGUnknownChunks_cleanup(dest); - for(i = 0; i != 3; ++i) - { + for(i = 0; i != 3; ++i) { size_t j; dest->unknown_chunks_size[i] = src->unknown_chunks_size[i]; dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]); if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/ - for(j = 0; j < src->unknown_chunks_size[i]; ++j) - { + for(j = 0; j < src->unknown_chunks_size[i]; ++j) { dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j]; } } @@ -2773,18 +2854,15 @@ static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* /******************************************************************************/ -static void LodePNGText_init(LodePNGInfo* info) -{ +static void LodePNGText_init(LodePNGInfo* info) { info->text_num = 0; info->text_keys = NULL; info->text_strings = NULL; } -static void LodePNGText_cleanup(LodePNGInfo* info) -{ +static void LodePNGText_cleanup(LodePNGInfo* info) { size_t i; - for(i = 0; i != info->text_num; ++i) - { + for(i = 0; i != info->text_num; ++i) { string_cleanup(&info->text_keys[i]); string_cleanup(&info->text_strings[i]); } @@ -2792,52 +2870,45 @@ static void LodePNGText_cleanup(LodePNGInfo* info) lodepng_free(info->text_strings); } -static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) -{ +static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) { size_t i = 0; dest->text_keys = 0; dest->text_strings = 0; dest->text_num = 0; - for(i = 0; i != source->text_num; ++i) - { + for(i = 0; i != source->text_num; ++i) { CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i])); } return 0; } -void lodepng_clear_text(LodePNGInfo* info) -{ - LodePNGText_cleanup(info); -} - -unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) -{ +static unsigned lodepng_add_text_sized(LodePNGInfo* info, const char* key, const char* str, size_t size) { char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1))); char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num + 1))); - if(!new_keys || !new_strings) - { - lodepng_free(new_keys); - lodepng_free(new_strings); - return 83; /*alloc fail*/ - } - ++info->text_num; - info->text_keys = new_keys; - info->text_strings = new_strings; + if(new_keys) info->text_keys = new_keys; + if(new_strings) info->text_strings = new_strings; - string_init(&info->text_keys[info->text_num - 1]); - string_set(&info->text_keys[info->text_num - 1], key); + if(!new_keys || !new_strings) return 83; /*alloc fail*/ - string_init(&info->text_strings[info->text_num - 1]); - string_set(&info->text_strings[info->text_num - 1], str); + ++info->text_num; + info->text_keys[info->text_num - 1] = alloc_string(key); + info->text_strings[info->text_num - 1] = alloc_string_sized(str, size); + if(!info->text_keys[info->text_num - 1] || !info->text_strings[info->text_num - 1]) return 83; /*alloc fail*/ return 0; } +unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) { + return lodepng_add_text_sized(info, key, str, lodepng_strlen(str)); +} + +void lodepng_clear_text(LodePNGInfo* info) { + LodePNGText_cleanup(info); +} + /******************************************************************************/ -static void LodePNGIText_init(LodePNGInfo* info) -{ +static void LodePNGIText_init(LodePNGInfo* info) { info->itext_num = 0; info->itext_keys = NULL; info->itext_langtags = NULL; @@ -2845,11 +2916,9 @@ static void LodePNGIText_init(LodePNGInfo* info) info->itext_strings = NULL; } -static void LodePNGIText_cleanup(LodePNGInfo* info) -{ +static void LodePNGIText_cleanup(LodePNGInfo* info) { size_t i; - for(i = 0; i != info->itext_num; ++i) - { + for(i = 0; i != info->itext_num; ++i) { string_cleanup(&info->itext_keys[i]); string_cleanup(&info->itext_langtags[i]); string_cleanup(&info->itext_transkeys[i]); @@ -2861,67 +2930,85 @@ static void LodePNGIText_cleanup(LodePNGInfo* info) lodepng_free(info->itext_strings); } -static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) -{ +static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) { size_t i = 0; dest->itext_keys = 0; dest->itext_langtags = 0; dest->itext_transkeys = 0; dest->itext_strings = 0; dest->itext_num = 0; - for(i = 0; i != source->itext_num; ++i) - { + for(i = 0; i != source->itext_num; ++i) { CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i], source->itext_transkeys[i], source->itext_strings[i])); } return 0; } -void lodepng_clear_itext(LodePNGInfo* info) -{ +void lodepng_clear_itext(LodePNGInfo* info) { LodePNGIText_cleanup(info); } -unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, - const char* transkey, const char* str) -{ +static unsigned lodepng_add_itext_sized(LodePNGInfo* info, const char* key, const char* langtag, + const char* transkey, const char* str, size_t size) { char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1))); char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1))); char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1))); char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1))); - if(!new_keys || !new_langtags || !new_transkeys || !new_strings) - { - lodepng_free(new_keys); - lodepng_free(new_langtags); - lodepng_free(new_transkeys); - lodepng_free(new_strings); - return 83; /*alloc fail*/ - } + + if(new_keys) info->itext_keys = new_keys; + if(new_langtags) info->itext_langtags = new_langtags; + if(new_transkeys) info->itext_transkeys = new_transkeys; + if(new_strings) info->itext_strings = new_strings; + + if(!new_keys || !new_langtags || !new_transkeys || !new_strings) return 83; /*alloc fail*/ ++info->itext_num; - info->itext_keys = new_keys; - info->itext_langtags = new_langtags; - info->itext_transkeys = new_transkeys; - info->itext_strings = new_strings; - string_init(&info->itext_keys[info->itext_num - 1]); - string_set(&info->itext_keys[info->itext_num - 1], key); + info->itext_keys[info->itext_num - 1] = alloc_string(key); + info->itext_langtags[info->itext_num - 1] = alloc_string(langtag); + info->itext_transkeys[info->itext_num - 1] = alloc_string(transkey); + info->itext_strings[info->itext_num - 1] = alloc_string_sized(str, size); - string_init(&info->itext_langtags[info->itext_num - 1]); - string_set(&info->itext_langtags[info->itext_num - 1], langtag); + return 0; +} - string_init(&info->itext_transkeys[info->itext_num - 1]); - string_set(&info->itext_transkeys[info->itext_num - 1], transkey); +unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, + const char* transkey, const char* str) { + return lodepng_add_itext_sized(info, key, langtag, transkey, str, lodepng_strlen(str)); +} - string_init(&info->itext_strings[info->itext_num - 1]); - string_set(&info->itext_strings[info->itext_num - 1], str); +/* same as set but does not delete */ +static unsigned lodepng_assign_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) { + if(profile_size == 0) return 100; /*invalid ICC profile size*/ - return 0; + info->iccp_name = alloc_string(name); + info->iccp_profile = (unsigned char*)lodepng_malloc(profile_size); + + if(!info->iccp_name || !info->iccp_profile) return 83; /*alloc fail*/ + + lodepng_memcpy(info->iccp_profile, profile, profile_size); + info->iccp_profile_size = profile_size; + + return 0; /*ok*/ +} + +unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) { + if(info->iccp_name) lodepng_clear_icc(info); + info->iccp_defined = 1; + + return lodepng_assign_icc(info, name, profile, profile_size); +} + +void lodepng_clear_icc(LodePNGInfo* info) { + string_cleanup(&info->iccp_name); + lodepng_free(info->iccp_profile); + info->iccp_profile = NULL; + info->iccp_profile_size = 0; + info->iccp_defined = 0; } #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ -void lodepng_info_init(LodePNGInfo* info) -{ +void lodepng_info_init(LodePNGInfo* info) { lodepng_color_mode_init(&info->color); info->interlace_method = 0; info->compression_method = 0; @@ -2936,31 +3023,41 @@ void lodepng_info_init(LodePNGInfo* info) info->time_defined = 0; info->phys_defined = 0; + info->gama_defined = 0; + info->chrm_defined = 0; + info->srgb_defined = 0; + info->iccp_defined = 0; + info->iccp_name = NULL; + info->iccp_profile = NULL; + LodePNGUnknownChunks_init(info); #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } -void lodepng_info_cleanup(LodePNGInfo* info) -{ +void lodepng_info_cleanup(LodePNGInfo* info) { lodepng_color_mode_cleanup(&info->color); #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS LodePNGText_cleanup(info); LodePNGIText_cleanup(info); + lodepng_clear_icc(info); + LodePNGUnknownChunks_cleanup(info); #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } -unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) -{ +unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) { lodepng_info_cleanup(dest); - *dest = *source; + lodepng_memcpy(dest, source, sizeof(LodePNGInfo)); lodepng_color_mode_init(&dest->color); CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color)); #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS CERROR_TRY_RETURN(LodePNGText_copy(dest, source)); CERROR_TRY_RETURN(LodePNGIText_copy(dest, source)); + if(source->iccp_defined) { + CERROR_TRY_RETURN(lodepng_assign_icc(dest, source->iccp_name, source->iccp_profile, source->iccp_profile_size)); + } LodePNGUnknownChunks_init(dest); CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source)); @@ -2968,25 +3065,17 @@ unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) return 0; } -void lodepng_info_swap(LodePNGInfo* a, LodePNGInfo* b) -{ - LodePNGInfo temp = *a; - *a = *b; - *b = temp; -} - /* ////////////////////////////////////////////////////////////////////////// */ /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/ -static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) -{ +static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) { unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/ /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/ unsigned p = index & m; in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/ in = in << (bits * (m - p)); - if(p == 0) out[index * bits / 8] = in; - else out[index * bits / 8] |= in; + if(p == 0) out[index * bits / 8u] = in; + else out[index * bits / 8u] |= in; } typedef struct ColorTree ColorTree; @@ -2997,26 +3086,20 @@ This is the data structure used to count the number of unique colors and to get index for a color. It's like an octree, but because the alpha channel is used too, each node has 16 instead of 8 children. */ -struct ColorTree -{ +struct ColorTree { ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/ int index; /*the payload. Only has a meaningful value if this is in the last level*/ }; -static void color_tree_init(ColorTree* tree) -{ - int i; - for(i = 0; i != 16; ++i) tree->children[i] = 0; +static void color_tree_init(ColorTree* tree) { + lodepng_memset(tree->children, 0, 16 * sizeof(*tree->children)); tree->index = -1; } -static void color_tree_cleanup(ColorTree* tree) -{ +static void color_tree_cleanup(ColorTree* tree) { int i; - for(i = 0; i != 16; ++i) - { - if(tree->children[i]) - { + for(i = 0; i != 16; ++i) { + if(tree->children[i]) { color_tree_cleanup(tree->children[i]); lodepng_free(tree->children[i]); } @@ -3024,11 +3107,9 @@ static void color_tree_cleanup(ColorTree* tree) } /*returns -1 if color not present, its index otherwise*/ -static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) -{ +static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) { int bit = 0; - for(bit = 0; bit < 8; ++bit) - { + for(bit = 0; bit < 8; ++bit) { int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); if(!tree->children[i]) return -1; else tree = tree->children[i]; @@ -3037,95 +3118,74 @@ static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, uns } #ifdef LODEPNG_COMPILE_ENCODER -static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) -{ +static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) { return color_tree_get(tree, r, g, b, a) >= 0; } #endif /*LODEPNG_COMPILE_ENCODER*/ /*color is not allowed to already exist. -Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")*/ -static void color_tree_add(ColorTree* tree, - unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index) -{ +Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist") +Returns error code, or 0 if ok*/ +static unsigned color_tree_add(ColorTree* tree, + unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index) { int bit; - for(bit = 0; bit < 8; ++bit) - { + for(bit = 0; bit < 8; ++bit) { int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); - if(!tree->children[i]) - { + if(!tree->children[i]) { tree->children[i] = (ColorTree*)lodepng_malloc(sizeof(ColorTree)); + if(!tree->children[i]) return 83; /*alloc fail*/ color_tree_init(tree->children[i]); } tree = tree->children[i]; } tree->index = (int)index; + return 0; } /*put a pixel, given its RGBA color, into image of any color type*/ static unsigned rgba8ToPixel(unsigned char* out, size_t i, const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, - unsigned char r, unsigned char g, unsigned char b, unsigned char a) -{ - if(mode->colortype == LCT_GREY) - { - unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; - if(mode->bitdepth == 8) out[i] = grey; - else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey; - else - { - /*take the most significant bits of grey*/ - grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1); - addColorBits(out, i, mode->bitdepth, grey); - } - } - else if(mode->colortype == LCT_RGB) - { - if(mode->bitdepth == 8) - { + unsigned char r, unsigned char g, unsigned char b, unsigned char a) { + if(mode->colortype == LCT_GREY) { + unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/ + if(mode->bitdepth == 8) out[i] = gray; + else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray; + else { + /*take the most significant bits of gray*/ + gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u); + addColorBits(out, i, mode->bitdepth, gray); + } + } else if(mode->colortype == LCT_RGB) { + if(mode->bitdepth == 8) { out[i * 3 + 0] = r; out[i * 3 + 1] = g; out[i * 3 + 2] = b; - } - else - { + } else { out[i * 6 + 0] = out[i * 6 + 1] = r; out[i * 6 + 2] = out[i * 6 + 3] = g; out[i * 6 + 4] = out[i * 6 + 5] = b; } - } - else if(mode->colortype == LCT_PALETTE) - { + } else if(mode->colortype == LCT_PALETTE) { int index = color_tree_get(tree, r, g, b, a); if(index < 0) return 82; /*color not in palette*/ if(mode->bitdepth == 8) out[i] = index; else addColorBits(out, i, mode->bitdepth, (unsigned)index); - } - else if(mode->colortype == LCT_GREY_ALPHA) - { - unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; - if(mode->bitdepth == 8) - { - out[i * 2 + 0] = grey; + } else if(mode->colortype == LCT_GREY_ALPHA) { + unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/ + if(mode->bitdepth == 8) { + out[i * 2 + 0] = gray; out[i * 2 + 1] = a; - } - else if(mode->bitdepth == 16) - { - out[i * 4 + 0] = out[i * 4 + 1] = grey; + } else if(mode->bitdepth == 16) { + out[i * 4 + 0] = out[i * 4 + 1] = gray; out[i * 4 + 2] = out[i * 4 + 3] = a; } - } - else if(mode->colortype == LCT_RGBA) - { - if(mode->bitdepth == 8) - { + } else if(mode->colortype == LCT_RGBA) { + if(mode->bitdepth == 8) { out[i * 4 + 0] = r; out[i * 4 + 1] = g; out[i * 4 + 2] = b; out[i * 4 + 3] = a; - } - else - { + } else { out[i * 8 + 0] = out[i * 8 + 1] = r; out[i * 8 + 2] = out[i * 8 + 3] = g; out[i * 8 + 4] = out[i * 8 + 5] = b; @@ -3139,33 +3199,25 @@ static unsigned rgba8ToPixel(unsigned char* out, size_t i, /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/ static void rgba16ToPixel(unsigned char* out, size_t i, const LodePNGColorMode* mode, - unsigned short r, unsigned short g, unsigned short b, unsigned short a) -{ - if(mode->colortype == LCT_GREY) - { - unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; - out[i * 2 + 0] = (grey >> 8) & 255; - out[i * 2 + 1] = grey & 255; - } - else if(mode->colortype == LCT_RGB) - { + unsigned short r, unsigned short g, unsigned short b, unsigned short a) { + if(mode->colortype == LCT_GREY) { + unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/ + out[i * 2 + 0] = (gray >> 8) & 255; + out[i * 2 + 1] = gray & 255; + } else if(mode->colortype == LCT_RGB) { out[i * 6 + 0] = (r >> 8) & 255; out[i * 6 + 1] = r & 255; out[i * 6 + 2] = (g >> 8) & 255; out[i * 6 + 3] = g & 255; out[i * 6 + 4] = (b >> 8) & 255; out[i * 6 + 5] = b & 255; - } - else if(mode->colortype == LCT_GREY_ALPHA) - { - unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; - out[i * 4 + 0] = (grey >> 8) & 255; - out[i * 4 + 1] = grey & 255; + } else if(mode->colortype == LCT_GREY_ALPHA) { + unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/ + out[i * 4 + 0] = (gray >> 8) & 255; + out[i * 4 + 1] = gray & 255; out[i * 4 + 2] = (a >> 8) & 255; out[i * 4 + 3] = a & 255; - } - else if(mode->colortype == LCT_RGBA) - { + } else if(mode->colortype == LCT_RGBA) { out[i * 8 + 0] = (r >> 8) & 255; out[i * 8 + 1] = r & 255; out[i * 8 + 2] = (g >> 8) & 255; @@ -3181,24 +3233,17 @@ static void rgba16ToPixel(unsigned char* out, size_t i, static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a, const unsigned char* in, size_t i, - const LodePNGColorMode* mode) -{ - if(mode->colortype == LCT_GREY) - { - if(mode->bitdepth == 8) - { + const LodePNGColorMode* mode) { + if(mode->colortype == LCT_GREY) { + if(mode->bitdepth == 8) { *r = *g = *b = in[i]; if(mode->key_defined && *r == mode->key_r) *a = 0; else *a = 255; - } - else if(mode->bitdepth == 16) - { + } else if(mode->bitdepth == 16) { *r = *g = *b = in[i * 2 + 0]; if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; else *a = 255; - } - else - { + } else { unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ size_t j = i * mode->bitdepth; unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); @@ -3206,17 +3251,12 @@ static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, if(mode->key_defined && value == mode->key_r) *a = 0; else *a = 255; } - } - else if(mode->colortype == LCT_RGB) - { - if(mode->bitdepth == 8) - { + } else if(mode->colortype == LCT_RGB) { + if(mode->bitdepth == 8) { *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2]; if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0; else *a = 255; - } - else - { + } else { *r = in[i * 6 + 0]; *g = in[i * 6 + 2]; *b = in[i * 6 + 4]; @@ -3225,56 +3265,33 @@ static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; else *a = 255; } - } - else if(mode->colortype == LCT_PALETTE) - { + } else if(mode->colortype == LCT_PALETTE) { unsigned index; if(mode->bitdepth == 8) index = in[i]; - else - { + else { size_t j = i * mode->bitdepth; index = readBitsFromReversedStream(&j, in, mode->bitdepth); } - - if(index >= mode->palettesize) - { - /*This is an error according to the PNG spec, but common PNG decoders make it black instead. - Done here too, slightly faster due to no error handling needed.*/ - *r = *g = *b = 0; - *a = 255; - } - else - { - *r = mode->palette[index * 4 + 0]; - *g = mode->palette[index * 4 + 1]; - *b = mode->palette[index * 4 + 2]; - *a = mode->palette[index * 4 + 3]; - } - } - else if(mode->colortype == LCT_GREY_ALPHA) - { - if(mode->bitdepth == 8) - { + /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/ + *r = mode->palette[index * 4 + 0]; + *g = mode->palette[index * 4 + 1]; + *b = mode->palette[index * 4 + 2]; + *a = mode->palette[index * 4 + 3]; + } else if(mode->colortype == LCT_GREY_ALPHA) { + if(mode->bitdepth == 8) { *r = *g = *b = in[i * 2 + 0]; *a = in[i * 2 + 1]; - } - else - { + } else { *r = *g = *b = in[i * 4 + 0]; *a = in[i * 4 + 2]; } - } - else if(mode->colortype == LCT_RGBA) - { - if(mode->bitdepth == 8) - { + } else if(mode->colortype == LCT_RGBA) { + if(mode->bitdepth == 8) { *r = in[i * 4 + 0]; *g = in[i * 4 + 1]; *b = in[i * 4 + 2]; *a = in[i * 4 + 3]; - } - else - { + } else { *r = in[i * 8 + 0]; *g = in[i * 8 + 2]; *b = in[i * 8 + 4]; @@ -3285,136 +3302,171 @@ static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, /*Similar to getPixelColorRGBA8, but with all the for loops inside of the color mode test cases, optimized to convert the colors much faster, when converting -to RGBA or RGB with 8 bit per cannel. buffer must be RGBA or RGB output with -enough memory, if has_alpha is true the output is RGBA. mode has the color mode -of the input buffer.*/ -static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels, - unsigned has_alpha, const unsigned char* in, - const LodePNGColorMode* mode) -{ - unsigned num_channels = has_alpha ? 4 : 3; +to the common case of RGBA with 8 bit per channel. buffer must be RGBA with +enough memory.*/ +static void getPixelColorsRGBA8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels, + const unsigned char* LODEPNG_RESTRICT in, + const LodePNGColorMode* mode) { + unsigned num_channels = 4; size_t i; - if(mode->colortype == LCT_GREY) - { - if(mode->bitdepth == 8) - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + if(mode->colortype == LCT_GREY) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = buffer[1] = buffer[2] = in[i]; - if(has_alpha) buffer[3] = mode->key_defined && in[i] == mode->key_r ? 0 : 255; + buffer[3] = 255; } - } - else if(mode->bitdepth == 16) - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + if(mode->key_defined) { + buffer -= numpixels * num_channels; + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + if(buffer[0] == mode->key_r) buffer[3] = 0; + } + } + } else if(mode->bitdepth == 16) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = buffer[1] = buffer[2] = in[i * 2]; - if(has_alpha) buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; + buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; } - } - else - { + } else { unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ size_t j = 0; - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest; - if(has_alpha) buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; + buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; } } - } - else if(mode->colortype == LCT_RGB) - { - if(mode->bitdepth == 8) - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { - buffer[0] = in[i * 3 + 0]; - buffer[1] = in[i * 3 + 1]; - buffer[2] = in[i * 3 + 2]; - if(has_alpha) buffer[3] = mode->key_defined && buffer[0] == mode->key_r - && buffer[1]== mode->key_g && buffer[2] == mode->key_b ? 0 : 255; + } else if(mode->colortype == LCT_RGB) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + lodepng_memcpy(buffer, &in[i * 3], 3); + buffer[3] = 255; } - } - else - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + if(mode->key_defined) { + buffer -= numpixels * num_channels; + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + if(buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0; + } + } + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = in[i * 6 + 0]; buffer[1] = in[i * 6 + 2]; buffer[2] = in[i * 6 + 4]; - if(has_alpha) buffer[3] = mode->key_defined + buffer[3] = mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255; } } - } - else if(mode->colortype == LCT_PALETTE) - { - unsigned index; - size_t j = 0; - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { - if(mode->bitdepth == 8) index = in[i]; - else index = readBitsFromReversedStream(&j, in, mode->bitdepth); - - if(index >= mode->palettesize) - { - /*This is an error according to the PNG spec, but most PNG decoders make it black instead. - Done here too, slightly faster due to no error handling needed.*/ - buffer[0] = buffer[1] = buffer[2] = 0; - if(has_alpha) buffer[3] = 255; + } else if(mode->colortype == LCT_PALETTE) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + unsigned index = in[i]; + /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/ + lodepng_memcpy(buffer, &mode->palette[index * 4], 4); } - else - { - buffer[0] = mode->palette[index * 4 + 0]; - buffer[1] = mode->palette[index * 4 + 1]; - buffer[2] = mode->palette[index * 4 + 2]; - if(has_alpha) buffer[3] = mode->palette[index * 4 + 3]; + } else { + size_t j = 0; + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); + /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/ + lodepng_memcpy(buffer, &mode->palette[index * 4], 4); } } - } - else if(mode->colortype == LCT_GREY_ALPHA) - { - if(mode->bitdepth == 8) - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + } else if(mode->colortype == LCT_GREY_ALPHA) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0]; - if(has_alpha) buffer[3] = in[i * 2 + 1]; + buffer[3] = in[i * 2 + 1]; } - } - else - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0]; - if(has_alpha) buffer[3] = in[i * 4 + 2]; + buffer[3] = in[i * 4 + 2]; + } + } + } else if(mode->colortype == LCT_RGBA) { + if(mode->bitdepth == 8) { + lodepng_memcpy(buffer, in, numpixels * 4); + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = in[i * 8 + 0]; + buffer[1] = in[i * 8 + 2]; + buffer[2] = in[i * 8 + 4]; + buffer[3] = in[i * 8 + 6]; } } } - else if(mode->colortype == LCT_RGBA) - { - if(mode->bitdepth == 8) - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { - buffer[0] = in[i * 4 + 0]; - buffer[1] = in[i * 4 + 1]; - buffer[2] = in[i * 4 + 2]; - if(has_alpha) buffer[3] = in[i * 4 + 3]; +} + +/*Similar to getPixelColorsRGBA8, but with 3-channel RGB output.*/ +static void getPixelColorsRGB8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels, + const unsigned char* LODEPNG_RESTRICT in, + const LodePNGColorMode* mode) { + const unsigned num_channels = 3; + size_t i; + if(mode->colortype == LCT_GREY) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = buffer[1] = buffer[2] = in[i]; + } + } else if(mode->bitdepth == 16) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = buffer[1] = buffer[2] = in[i * 2]; + } + } else { + unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ + size_t j = 0; + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); + buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest; + } + } + } else if(mode->colortype == LCT_RGB) { + if(mode->bitdepth == 8) { + lodepng_memcpy(buffer, in, numpixels * 3); + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = in[i * 6 + 0]; + buffer[1] = in[i * 6 + 2]; + buffer[2] = in[i * 6 + 4]; + } + } + } else if(mode->colortype == LCT_PALETTE) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + unsigned index = in[i]; + /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/ + lodepng_memcpy(buffer, &mode->palette[index * 4], 3); + } + } else { + size_t j = 0; + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); + /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/ + lodepng_memcpy(buffer, &mode->palette[index * 4], 3); + } + } + } else if(mode->colortype == LCT_GREY_ALPHA) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0]; + } + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0]; } } - else - { - for(i = 0; i != numpixels; ++i, buffer += num_channels) - { + } else if(mode->colortype == LCT_RGBA) { + if(mode->bitdepth == 8) { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { + lodepng_memcpy(buffer, &in[i * 4], 3); + } + } else { + for(i = 0; i != numpixels; ++i, buffer += num_channels) { buffer[0] = in[i * 8 + 0]; buffer[1] = in[i * 8 + 2]; buffer[2] = in[i * 8 + 4]; - if(has_alpha) buffer[3] = in[i * 8 + 6]; } } } @@ -3423,16 +3475,12 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels, /*Get RGBA16 color of pixel with index i (y * width + x) from the raw image with given color type, but the given color type must be 16-bit itself.*/ static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a, - const unsigned char* in, size_t i, const LodePNGColorMode* mode) -{ - if(mode->colortype == LCT_GREY) - { + const unsigned char* in, size_t i, const LodePNGColorMode* mode) { + if(mode->colortype == LCT_GREY) { *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1]; if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; else *a = 65535; - } - else if(mode->colortype == LCT_RGB) - { + } else if(mode->colortype == LCT_RGB) { *r = 256u * in[i * 6 + 0] + in[i * 6 + 1]; *g = 256u * in[i * 6 + 2] + in[i * 6 + 3]; *b = 256u * in[i * 6 + 4] + in[i * 6 + 5]; @@ -3441,14 +3489,10 @@ static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned s && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; else *a = 65535; - } - else if(mode->colortype == LCT_GREY_ALPHA) - { + } else if(mode->colortype == LCT_GREY_ALPHA) { *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1]; *a = 256u * in[i * 4 + 2] + in[i * 4 + 3]; - } - else if(mode->colortype == LCT_RGBA) - { + } else if(mode->colortype == LCT_RGBA) { *r = 256u * in[i * 8 + 0] + in[i * 8 + 1]; *g = 256u * in[i * 8 + 2] + in[i * 8 + 3]; *b = 256u * in[i * 8 + 4] + in[i * 8 + 5]; @@ -3458,91 +3502,153 @@ static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned s unsigned lodepng_convert(unsigned char* out, const unsigned char* in, const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, - unsigned w, unsigned h) -{ + unsigned w, unsigned h) { size_t i; ColorTree tree; - size_t numpixels = w * h; + size_t numpixels = (size_t)w * (size_t)h; + unsigned error = 0; - if(lodepng_color_mode_equal(mode_out, mode_in)) - { + if(mode_in->colortype == LCT_PALETTE && !mode_in->palette) { + return 107; /* error: must provide palette if input mode is palette */ + } + + if(lodepng_color_mode_equal(mode_out, mode_in)) { size_t numbytes = lodepng_get_raw_size(w, h, mode_in); - for(i = 0; i != numbytes; ++i) out[i] = in[i]; + lodepng_memcpy(out, in, numbytes); return 0; } - if(mode_out->colortype == LCT_PALETTE) - { + if(mode_out->colortype == LCT_PALETTE) { size_t palettesize = mode_out->palettesize; const unsigned char* palette = mode_out->palette; - size_t palsize = size_t(1) << mode_out->bitdepth; + size_t palsize = (size_t)1u << mode_out->bitdepth; /*if the user specified output palette but did not give the values, assume they want the values of the input color type (assuming that one is palette). Note that we never create a new palette ourselves.*/ - if(palettesize == 0) - { + if(palettesize == 0) { palettesize = mode_in->palettesize; palette = mode_in->palette; + /*if the input was also palette with same bitdepth, then the color types are also + equal, so copy literally. This to preserve the exact indices that were in the PNG + even in case there are duplicate colors in the palette.*/ + if(mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth) { + size_t numbytes = lodepng_get_raw_size(w, h, mode_in); + lodepng_memcpy(out, in, numbytes); + return 0; + } } if(palettesize < palsize) palsize = palettesize; color_tree_init(&tree); - for(i = 0; i != palsize; ++i) - { + for(i = 0; i != palsize; ++i) { const unsigned char* p = &palette[i * 4]; - color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned int)(i)); + error = color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned)i); + if(error) break; } } - if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) - { - for(i = 0; i != numpixels; ++i) - { - unsigned short r = 0, g = 0, b = 0, a = 0; - getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); - rgba16ToPixel(out, i, mode_out, r, g, b, a); + if(!error) { + if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) { + for(i = 0; i != numpixels; ++i) { + unsigned short r = 0, g = 0, b = 0, a = 0; + getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); + rgba16ToPixel(out, i, mode_out, r, g, b, a); + } + } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) { + getPixelColorsRGBA8(out, numpixels, in, mode_in); + } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) { + getPixelColorsRGB8(out, numpixels, in, mode_in); + } else { + unsigned char r = 0, g = 0, b = 0, a = 0; + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); + error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a); + if(error) break; + } } } - else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) - { - getPixelColorsRGBA8(out, numpixels, 1, in, mode_in); - } - else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) - { - getPixelColorsRGBA8(out, numpixels, 0, in, mode_in); + + if(mode_out->colortype == LCT_PALETTE) { + color_tree_cleanup(&tree); } - else - { - unsigned char r = 0, g = 0, b = 0, a = 0; - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); - CERROR_TRY_RETURN(rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a)); - } + + return error; +} + + +/* Converts a single rgb color without alpha from one type to another, color bits truncated to +their bitdepth. In case of single channel (gray or palette), only the r channel is used. Slow +function, do not use to process all pixels of an image. Alpha channel not supported on purpose: +this is for bKGD, supporting alpha may prevent it from finding a color in the palette, from the +specification it looks like bKGD should ignore the alpha values of the palette since it can use +any palette index but doesn't have an alpha channel. Idem with ignoring color key. */ +unsigned lodepng_convert_rgb( + unsigned* r_out, unsigned* g_out, unsigned* b_out, + unsigned r_in, unsigned g_in, unsigned b_in, + const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in) { + unsigned r = 0, g = 0, b = 0; + unsigned mul = 65535 / ((1u << mode_in->bitdepth) - 1u); /*65535, 21845, 4369, 257, 1*/ + unsigned shift = 16 - mode_out->bitdepth; + + if(mode_in->colortype == LCT_GREY || mode_in->colortype == LCT_GREY_ALPHA) { + r = g = b = r_in * mul; + } else if(mode_in->colortype == LCT_RGB || mode_in->colortype == LCT_RGBA) { + r = r_in * mul; + g = g_in * mul; + b = b_in * mul; + } else if(mode_in->colortype == LCT_PALETTE) { + if(r_in >= mode_in->palettesize) return 82; + r = mode_in->palette[r_in * 4 + 0] * 257u; + g = mode_in->palette[r_in * 4 + 1] * 257u; + b = mode_in->palette[r_in * 4 + 2] * 257u; + } else { + return 31; } - if(mode_out->colortype == LCT_PALETTE) - { - color_tree_cleanup(&tree); + /* now convert to output format */ + if(mode_out->colortype == LCT_GREY || mode_out->colortype == LCT_GREY_ALPHA) { + *r_out = r >> shift ; + } else if(mode_out->colortype == LCT_RGB || mode_out->colortype == LCT_RGBA) { + *r_out = r >> shift ; + *g_out = g >> shift ; + *b_out = b >> shift ; + } else if(mode_out->colortype == LCT_PALETTE) { + unsigned i; + /* a 16-bit color cannot be in the palette */ + if((r >> 8) != (r & 255) || (g >> 8) != (g & 255) || (b >> 8) != (b & 255)) return 82; + for(i = 0; i < mode_out->palettesize; i++) { + unsigned j = i * 4; + if((r >> 8) == mode_out->palette[j + 0] && (g >> 8) == mode_out->palette[j + 1] && + (b >> 8) == mode_out->palette[j + 2]) { + *r_out = i; + return 0; + } + } + return 82; + } else { + return 31; } - return 0; /*no error*/ + return 0; } #ifdef LODEPNG_COMPILE_ENCODER -void lodepng_color_profile_init(LodePNGColorProfile* profile) -{ - profile->colored = 0; - profile->key = 0; - profile->alpha = 0; - profile->key_r = profile->key_g = profile->key_b = 0; - profile->numcolors = 0; - profile->bits = 1; +void lodepng_color_stats_init(LodePNGColorStats* stats) { + /*stats*/ + stats->colored = 0; + stats->key = 0; + stats->key_r = stats->key_g = stats->key_b = 0; + stats->alpha = 0; + stats->numcolors = 0; + stats->bits = 1; + stats->numpixels = 0; + /*settings*/ + stats->allow_palette = 1; + stats->allow_greyscale = 1; } /*function used for debug purposes with C++*/ -/*void printColorProfile(LodePNGColorProfile* p) -{ +/*void printColorStats(LodePNGColorStats* p) { std::cout << "colored: " << (int)p->colored << ", "; std::cout << "key: " << (int)p->key << ", "; std::cout << "key_r: " << (int)p->key_r << ", "; @@ -3554,237 +3660,260 @@ void lodepng_color_profile_init(LodePNGColorProfile* profile) }*/ /*Returns how many bits needed to represent given value (max 8 bit)*/ -static unsigned getValueRequiredBits(unsigned char value) -{ +static unsigned getValueRequiredBits(unsigned char value) { if(value == 0 || value == 255) return 1; /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/ if(value % 17 == 0) return value % 85 == 0 ? 2 : 4; return 8; } -/*profile must already have been inited with mode. -It's ok to set some parameters of profile to done already.*/ -unsigned lodepng_get_color_profile(LodePNGColorProfile* profile, - const unsigned char* in, unsigned w, unsigned h, - const LodePNGColorMode* mode) -{ - unsigned error = 0; +/*stats must already have been inited. */ +unsigned lodepng_compute_color_stats(LodePNGColorStats* stats, + const unsigned char* in, unsigned w, unsigned h, + const LodePNGColorMode* mode_in) { size_t i; ColorTree tree; - size_t numpixels = w * h; + size_t numpixels = (size_t)w * (size_t)h; + unsigned error = 0; - unsigned colored_done = lodepng_is_greyscale_type(mode) ? 1 : 0; - unsigned alpha_done = lodepng_can_have_alpha(mode) ? 0 : 1; + /* mark things as done already if it would be impossible to have a more expensive case */ + unsigned colored_done = lodepng_is_greyscale_type(mode_in) ? 1 : 0; + unsigned alpha_done = lodepng_can_have_alpha(mode_in) ? 0 : 1; unsigned numcolors_done = 0; - unsigned bpp = lodepng_get_bpp(mode); - unsigned bits_done = bpp == 1 ? 1 : 0; + unsigned bpp = lodepng_get_bpp(mode_in); + unsigned bits_done = (stats->bits == 1 && bpp == 1) ? 1 : 0; + unsigned sixteen = 0; /* whether the input image is 16 bit */ unsigned maxnumcolors = 257; - unsigned sixteen = 0; - if(bpp <= 8) maxnumcolors = bpp == 1 ? 2 : (bpp == 2 ? 4 : (bpp == 4 ? 16 : 256)); + if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, stats->numcolors + (1u << bpp)); + + stats->numpixels += numpixels; + + /*if palette not allowed, no need to compute numcolors*/ + if(!stats->allow_palette) numcolors_done = 1; color_tree_init(&tree); + /*If the stats was already filled in from previous data, fill its palette in tree + and mark things as done already if we know they are the most expensive case already*/ + if(stats->alpha) alpha_done = 1; + if(stats->colored) colored_done = 1; + if(stats->bits == 16) numcolors_done = 1; + if(stats->bits >= bpp) bits_done = 1; + if(stats->numcolors >= maxnumcolors) numcolors_done = 1; + + if(!numcolors_done) { + for(i = 0; i < stats->numcolors; i++) { + const unsigned char* color = &stats->palette[i * 4]; + error = color_tree_add(&tree, color[0], color[1], color[2], color[3], i); + if(error) goto cleanup; + } + } + /*Check if the 16-bit input is truly 16-bit*/ - if(mode->bitdepth == 16) - { - unsigned short r, g, b, a; - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); + if(mode_in->bitdepth == 16 && !sixteen) { + unsigned short r = 0, g = 0, b = 0, a = 0; + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) || - (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/ - { + (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/ { + stats->bits = 16; sixteen = 1; + bits_done = 1; + numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ break; } } } - if(sixteen) - { + if(sixteen) { unsigned short r = 0, g = 0, b = 0, a = 0; - profile->bits = 16; - bits_done = numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); - if(!colored_done && (r != g || r != b)) - { - profile->colored = 1; + if(!colored_done && (r != g || r != b)) { + stats->colored = 1; colored_done = 1; } - if(!alpha_done) - { - unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b); - if(a != 65535 && (a != 0 || (profile->key && !matchkey))) - { - profile->alpha = 1; + if(!alpha_done) { + unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); + if(a != 65535 && (a != 0 || (stats->key && !matchkey))) { + stats->alpha = 1; + stats->key = 0; alpha_done = 1; - if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ - } - else if(a == 0 && !profile->alpha && !profile->key) - { - profile->key = 1; - profile->key_r = r; - profile->key_g = g; - profile->key_b = b; - } - else if(a == 65535 && profile->key && matchkey) - { + } else if(a == 0 && !stats->alpha && !stats->key) { + stats->key = 1; + stats->key_r = r; + stats->key_g = g; + stats->key_b = b; + } else if(a == 65535 && stats->key && matchkey) { /* Color key cannot be used if an opaque pixel also has that RGB color. */ - profile->alpha = 1; + stats->alpha = 1; + stats->key = 0; alpha_done = 1; } } if(alpha_done && numcolors_done && colored_done && bits_done) break; } - if(profile->key && !profile->alpha) - { - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); - if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b) - { + if(stats->key && !stats->alpha) { + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); + if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { /* Color key cannot be used if an opaque pixel also has that RGB color. */ - profile->alpha = 1; + stats->alpha = 1; + stats->key = 0; alpha_done = 1; } } } - } - else /* < 16-bit */ - { + } else /* < 16-bit */ { unsigned char r = 0, g = 0, b = 0, a = 0; - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode); + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); - if(!bits_done && profile->bits < 8) - { - /*only r is checked, < 8 bits is only relevant for greyscale*/ + if(!bits_done && stats->bits < 8) { + /*only r is checked, < 8 bits is only relevant for grayscale*/ unsigned bits = getValueRequiredBits(r); - if(bits > profile->bits) profile->bits = bits; + if(bits > stats->bits) stats->bits = bits; } - bits_done = (profile->bits >= bpp); + bits_done = (stats->bits >= bpp); - if(!colored_done && (r != g || r != b)) - { - profile->colored = 1; + if(!colored_done && (r != g || r != b)) { + stats->colored = 1; colored_done = 1; - if(profile->bits < 8) profile->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/ + if(stats->bits < 8) stats->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/ } - if(!alpha_done) - { - unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b); - if(a != 255 && (a != 0 || (profile->key && !matchkey))) - { - profile->alpha = 1; + if(!alpha_done) { + unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); + if(a != 255 && (a != 0 || (stats->key && !matchkey))) { + stats->alpha = 1; + stats->key = 0; alpha_done = 1; - if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ - } - else if(a == 0 && !profile->alpha && !profile->key) - { - profile->key = 1; - profile->key_r = r; - profile->key_g = g; - profile->key_b = b; - } - else if(a == 255 && profile->key && matchkey) - { + if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ + } else if(a == 0 && !stats->alpha && !stats->key) { + stats->key = 1; + stats->key_r = r; + stats->key_g = g; + stats->key_b = b; + } else if(a == 255 && stats->key && matchkey) { /* Color key cannot be used if an opaque pixel also has that RGB color. */ - profile->alpha = 1; + stats->alpha = 1; + stats->key = 0; alpha_done = 1; - if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ + if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ } } - if(!numcolors_done) - { - if(!color_tree_has(&tree, r, g, b, a)) - { - color_tree_add(&tree, r, g, b, a, profile->numcolors); - if(profile->numcolors < 256) - { - unsigned char* p = profile->palette; - unsigned n = profile->numcolors; + if(!numcolors_done) { + if(!color_tree_has(&tree, r, g, b, a)) { + error = color_tree_add(&tree, r, g, b, a, stats->numcolors); + if(error) goto cleanup; + if(stats->numcolors < 256) { + unsigned char* p = stats->palette; + unsigned n = stats->numcolors; p[n * 4 + 0] = r; p[n * 4 + 1] = g; p[n * 4 + 2] = b; p[n * 4 + 3] = a; } - ++profile->numcolors; - numcolors_done = profile->numcolors >= maxnumcolors; + ++stats->numcolors; + numcolors_done = stats->numcolors >= maxnumcolors; } } if(alpha_done && numcolors_done && colored_done && bits_done) break; } - if(profile->key && !profile->alpha) - { - for(i = 0; i != numpixels; ++i) - { - getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode); - if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b) - { + if(stats->key && !stats->alpha) { + for(i = 0; i != numpixels; ++i) { + getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); + if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { /* Color key cannot be used if an opaque pixel also has that RGB color. */ - profile->alpha = 1; + stats->alpha = 1; + stats->key = 0; alpha_done = 1; + if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ } } } - /*make the profile's key always 16-bit for consistency - repeat each byte twice*/ - profile->key_r += (profile->key_r << 8); - profile->key_g += (profile->key_g << 8); - profile->key_b += (profile->key_b << 8); + /*make the stats's key always 16-bit for consistency - repeat each byte twice*/ + stats->key_r += (stats->key_r << 8); + stats->key_g += (stats->key_g << 8); + stats->key_b += (stats->key_b << 8); } +cleanup: color_tree_cleanup(&tree); return error; } -/*Automatically chooses color type that gives smallest amount of bits in the -output image, e.g. grey if there are only greyscale pixels, palette if there -are less than 256 colors, ... -Updates values of mode with a potentially smaller color model. mode_out should -contain the user chosen color model, but will be overwritten with the new chosen one.*/ -unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, - const unsigned char* image, unsigned w, unsigned h, - const LodePNGColorMode* mode_in) -{ - LodePNGColorProfile prof; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +/*Adds a single color to the color stats. The stats must already have been inited. The color must be given as 16-bit +(with 2 bytes repeating for 8-bit and 65535 for opaque alpha channel). This function is expensive, do not call it for +all pixels of an image but only for a few additional values. */ +static unsigned lodepng_color_stats_add(LodePNGColorStats* stats, + unsigned r, unsigned g, unsigned b, unsigned a) { + unsigned error = 0; + unsigned char image[8]; + LodePNGColorMode mode; + lodepng_color_mode_init(&mode); + image[0] = r >> 8; image[1] = r; image[2] = g >> 8; image[3] = g; + image[4] = b >> 8; image[5] = b; image[6] = a >> 8; image[7] = a; + mode.bitdepth = 16; + mode.colortype = LCT_RGBA; + error = lodepng_compute_color_stats(stats, image, 1, 1, &mode); + lodepng_color_mode_cleanup(&mode); + return error; +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +/*Computes a minimal PNG color model that can contain all colors as indicated by the stats. +The stats should be computed with lodepng_compute_color_stats. +mode_in is raw color profile of the image the stats were computed on, to copy palette order from when relevant. +Minimal PNG color model means the color type and bit depth that gives smallest amount of bits in the output image, +e.g. gray if only grayscale pixels, palette if less than 256 colors, color key if only single transparent color, ... +This is used if auto_convert is enabled (it is by default). +*/ +static unsigned auto_choose_color(LodePNGColorMode* mode_out, + const LodePNGColorMode* mode_in, + const LodePNGColorStats* stats) { unsigned error = 0; - unsigned i, n, palettebits, grey_ok, palette_ok; + unsigned palettebits; + size_t i, n; + size_t numpixels = stats->numpixels; + unsigned palette_ok, gray_ok; + + unsigned alpha = stats->alpha; + unsigned key = stats->key; + unsigned bits = stats->bits; - lodepng_color_profile_init(&prof); - error = lodepng_get_color_profile(&prof, image, w, h, mode_in); - if(error) return error; mode_out->key_defined = 0; - if(prof.key && w * h <= 16) - { - prof.alpha = 1; /*too few pixels to justify tRNS chunk overhead*/ - if(prof.bits < 8) prof.bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ + if(key && numpixels <= 16) { + alpha = 1; /*too few pixels to justify tRNS chunk overhead*/ + key = 0; + if(bits < 8) bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ } - grey_ok = !prof.colored && !prof.alpha; /*grey without alpha, with potentially low bits*/ - n = prof.numcolors; + + gray_ok = !stats->colored; + if(!stats->allow_greyscale) gray_ok = 0; + if(!gray_ok && bits < 8) bits = 8; + + n = stats->numcolors; palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8)); - palette_ok = n <= 256 && (n * 2 < w * h) && prof.bits <= 8; - if(w * h < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/ - if(grey_ok && prof.bits <= palettebits) palette_ok = 0; /*grey is less overhead*/ + palette_ok = n <= 256 && bits <= 8 && n != 0; /*n==0 means likely numcolors wasn't computed*/ + if(numpixels < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/ + if(gray_ok && !alpha && bits <= palettebits) palette_ok = 0; /*gray is less overhead*/ + if(!stats->allow_palette) palette_ok = 0; - if(palette_ok) - { - unsigned char* p = prof.palette; + if(palette_ok) { + const unsigned char* p = stats->palette; lodepng_palette_clear(mode_out); /*remove potential earlier palette*/ - for(i = 0; i != prof.numcolors; ++i) - { + for(i = 0; i != stats->numcolors; ++i) { error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]); if(error) break; } @@ -3793,25 +3922,20 @@ unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, mode_out->bitdepth = palettebits; if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize - && mode_in->bitdepth == mode_out->bitdepth) - { + && mode_in->bitdepth == mode_out->bitdepth) { /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/ lodepng_color_mode_cleanup(mode_out); lodepng_color_mode_copy(mode_out, mode_in); } - } - else /*8-bit or 16-bit per channel*/ - { - mode_out->bitdepth = prof.bits; - mode_out->colortype = prof.alpha ? (prof.colored ? LCT_RGBA : LCT_GREY_ALPHA) - : (prof.colored ? LCT_RGB : LCT_GREY); - - if(prof.key && !prof.alpha) - { - unsigned mask = (1u << mode_out->bitdepth) - 1u; /*profile always uses 16-bit, mask converts it*/ - mode_out->key_r = prof.key_r & mask; - mode_out->key_g = prof.key_g & mask; - mode_out->key_b = prof.key_b & mask; + } else /*8-bit or 16-bit per channel*/ { + mode_out->bitdepth = bits; + mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA) + : (gray_ok ? LCT_GREY : LCT_RGB); + if(key) { + unsigned mask = (1u << mode_out->bitdepth) - 1u; /*stats always uses 16-bit, mask converts it*/ + mode_out->key_r = stats->key_r & mask; + mode_out->key_g = stats->key_g & mask; + mode_out->key_b = stats->key_b & mask; mode_out->key_defined = 1; } } @@ -3822,19 +3946,17 @@ unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, #endif /* #ifdef LODEPNG_COMPILE_ENCODER */ /* -Paeth predicter, used by PNG filter type 4 +Paeth predictor, used by PNG filter type 4 The parameters are of type short, but should come from unsigned chars, the shorts are only needed to make the paeth calculation correct. */ -static unsigned char paethPredictor(short a, short b, short c) -{ - short pa = abs(b - c); - short pb = abs(a - c); - short pc = abs(a + b - c - c); - - if(pc < pa && pc < pb) return (unsigned char)c; - else if(pb < pa) return (unsigned char)b; - else return (unsigned char)a; +static unsigned char paethPredictor(short a, short b, short c) { + short pa = LODEPNG_ABS(b - c); + short pb = LODEPNG_ABS(a - c); + short pc = LODEPNG_ABS(a + b - c - c); + /* return input value associated with smallest of pa, pb, pc (with certain priority if equal) */ + if(pb < pa) { a = b; pa = pb; } + return (pc < pa) ? c : a; } /*shared values used by multiple Adam7 related functions*/ @@ -3860,14 +3982,12 @@ bpp: bits per pixel end at a full byte */ static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], - size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) -{ + size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) { /*the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass*/ unsigned i; /*calculate width and height in pixels of each pass*/ - for(i = 0; i != 7; ++i) - { + for(i = 0; i != 7; ++i) { passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; if(passw[i] == 0) passh[i] = 0; @@ -3875,15 +3995,14 @@ static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t fil } filter_passstart[0] = padded_passstart[0] = passstart[0] = 0; - for(i = 0; i != 7; ++i) - { + for(i = 0; i != 7; ++i) { /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ filter_passstart[i + 1] = filter_passstart[i] - + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); + + ((passw[i] && passh[i]) ? passh[i] * (1u + (passw[i] * bpp + 7u) / 8u) : 0); /*bits padded if needed to fill full byte at end of each scanline*/ - padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); + padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7u) / 8u); /*only padded at end of reduced image*/ - passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; + passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7u) / 8u; } } @@ -3895,60 +4014,51 @@ static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t fil /*read the information from the header and store it in the LodePNGInfo. return value is error*/ unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, - const unsigned char* in, size_t insize) -{ + const unsigned char* in, size_t insize) { + unsigned width, height; LodePNGInfo* info = &state->info_png; - if(insize == 0 || in == 0) - { + if(insize == 0 || in == 0) { CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ } - if(insize < 33) - { + if(insize < 33) { CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/ } /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/ + /* TODO: remove this. One should use a new LodePNGState for new sessions */ lodepng_info_cleanup(info); lodepng_info_init(info); if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 - || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) - { + || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) { CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/ } - if(lodepng_chunk_length(in + 8) != 13) - { + if(lodepng_chunk_length(in + 8) != 13) { CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/ } - if(!lodepng_chunk_type_equals(in + 8, "IHDR")) - { + if(!lodepng_chunk_type_equals(in + 8, "IHDR")) { CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ } /*read the values given in the header*/ - *w = lodepng_read32bitInt(&in[16]); - *h = lodepng_read32bitInt(&in[20]); + width = lodepng_read32bitInt(&in[16]); + height = lodepng_read32bitInt(&in[20]); + /*TODO: remove the undocumented feature that allows to give null pointers to width or height*/ + if(w) *w = width; + if(h) *h = height; info->color.bitdepth = in[24]; info->color.colortype = (LodePNGColorType)in[25]; info->compression_method = in[26]; info->filter_method = in[27]; info->interlace_method = in[28]; - if(*w == 0 || *h == 0) - { - CERROR_RETURN_ERROR(state->error, 93); - } - - if(!state->decoder.ignore_crc) - { - unsigned CRC = lodepng_read32bitInt(&in[29]); - unsigned checksum = lodepng_crc32(&in[12], 17); - if(CRC != checksum) - { - CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ - } - } + /*errors returned only after the parsing so other values are still output*/ + /*error: invalid image size*/ + if(width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93); + /*error: invalid colortype or bitdepth combination*/ + state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); + if(state->error) return state->error; /*error: only compression method 0 is allowed in the specification*/ if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); /*error: only filter method 0 is allowed in the specification*/ @@ -3956,13 +4066,19 @@ unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, /*error: only interlace methods 0 and 1 exist in the specification*/ if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); - state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); + if(!state->decoder.ignore_crc) { + unsigned CRC = lodepng_read32bitInt(&in[29]); + unsigned checksum = lodepng_crc32(&in[12], 17); + if(CRC != checksum) { + CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ + } + } + return state->error; } static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, - size_t bytewidth, unsigned char filterType, size_t length) -{ + size_t bytewidth, unsigned char filterType, size_t length) { /* For PNG filter method 0 unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte, @@ -3973,8 +4089,7 @@ static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scan */ size_t i; - switch(filterType) - { + switch(filterType) { case 0: for(i = 0; i != length; ++i) recon[i] = scanline[i]; break; @@ -3983,59 +4098,83 @@ static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scan for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth]; break; case 2: - if(precon) - { + if(precon) { for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i]; - } - else - { + } else { for(i = 0; i != length; ++i) recon[i] = scanline[i]; } break; case 3: - if(precon) - { - for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1); - for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >> 1); - } - else - { + if(precon) { + for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1u); + for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >> 1u); + } else { for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i]; - for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1); + for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1u); } break; case 4: - if(precon) - { - for(i = 0; i != bytewidth; ++i) - { + if(precon) { + for(i = 0; i != bytewidth; ++i) { recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/ } - for(i = bytewidth; i < length; ++i) - { + + /* Unroll independent paths of the paeth predictor. A 6x and 8x version would also be possible but that + adds too much code. Whether this actually speeds anything up at all depends on compiler and settings. */ + if(bytewidth >= 4) { + for(; i + 3 < length; i += 4) { + size_t j = i - bytewidth; + unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2], s3 = scanline[i + 3]; + unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2], r3 = recon[j + 3]; + unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2], p3 = precon[i + 3]; + unsigned char q0 = precon[j + 0], q1 = precon[j + 1], q2 = precon[j + 2], q3 = precon[j + 3]; + recon[i + 0] = s0 + paethPredictor(r0, p0, q0); + recon[i + 1] = s1 + paethPredictor(r1, p1, q1); + recon[i + 2] = s2 + paethPredictor(r2, p2, q2); + recon[i + 3] = s3 + paethPredictor(r3, p3, q3); + } + } else if(bytewidth >= 3) { + for(; i + 2 < length; i += 3) { + size_t j = i - bytewidth; + unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2]; + unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2]; + unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2]; + unsigned char q0 = precon[j + 0], q1 = precon[j + 1], q2 = precon[j + 2]; + recon[i + 0] = s0 + paethPredictor(r0, p0, q0); + recon[i + 1] = s1 + paethPredictor(r1, p1, q1); + recon[i + 2] = s2 + paethPredictor(r2, p2, q2); + } + } else if(bytewidth >= 2) { + for(; i + 1 < length; i += 2) { + size_t j = i - bytewidth; + unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1]; + unsigned char r0 = recon[j + 0], r1 = recon[j + 1]; + unsigned char p0 = precon[i + 0], p1 = precon[i + 1]; + unsigned char q0 = precon[j + 0], q1 = precon[j + 1]; + recon[i + 0] = s0 + paethPredictor(r0, p0, q0); + recon[i + 1] = s1 + paethPredictor(r1, p1, q1); + } + } + + for(; i != length; ++i) { recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); } - } - else - { - for(i = 0; i != bytewidth; ++i) - { + } else { + for(i = 0; i != bytewidth; ++i) { recon[i] = scanline[i]; } - for(i = bytewidth; i < length; ++i) - { + for(i = bytewidth; i < length; ++i) { /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/ recon[i] = (scanline[i] + recon[i - bytewidth]); } } break; - default: return 36; /*error: unexisting filter type given*/ + default: return 36; /*error: invalid filter type given*/ } return 0; } -static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) -{ +static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) { /* For PNG filter method 0 this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times) @@ -4048,11 +4187,11 @@ static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w unsigned char* prevline = 0; /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ - size_t bytewidth = (bpp + 7) / 8; - size_t linebytes = (w * bpp + 7) / 8; + size_t bytewidth = (bpp + 7u) / 8u; + /*the width of a scanline in bytes, not including the filter type*/ + size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; - for(y = 0; y < h; ++y) - { + for(y = 0; y < h; ++y) { size_t outindex = linebytes * y; size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ unsigned char filterType = in[inindex]; @@ -4076,50 +4215,40 @@ out must be big enough AND must be 0 everywhere if bpp < 8 in the current implem (because that's likely a little bit faster) NOTE: comments about padding bits are only relevant if bpp < 8 */ -static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) -{ +static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) { unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; unsigned i; Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); - if(bpp >= 8) - { - for(i = 0; i != 7; ++i) - { + if(bpp >= 8) { + for(i = 0; i != 7; ++i) { unsigned x, y, b; - size_t bytewidth = bpp / 8; + size_t bytewidth = bpp / 8u; for(y = 0; y < passh[i]; ++y) - for(x = 0; x < passw[i]; ++x) - { + for(x = 0; x < passw[i]; ++x) { size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth; - size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; - for(b = 0; b < bytewidth; ++b) - { + size_t pixeloutstart = ((ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * (size_t)w + + ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bytewidth; + for(b = 0; b < bytewidth; ++b) { out[pixeloutstart + b] = in[pixelinstart + b]; } } } - } - else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ - { - for(i = 0; i != 7; ++i) - { + } else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ { + for(i = 0; i != 7; ++i) { unsigned x, y, b; unsigned ilinebits = bpp * passw[i]; unsigned olinebits = bpp * w; size_t obp, ibp; /*bit pointers (for out and in buffer)*/ for(y = 0; y < passh[i]; ++y) - for(x = 0; x < passw[i]; ++x) - { + for(x = 0; x < passw[i]; ++x) { ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp); - obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; - for(b = 0; b < bpp; ++b) - { + obp = (ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bpp; + for(b = 0; b < bpp; ++b) { unsigned char bit = readBitFromReversedStream(&ibp, in); - /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/ - setBitOfReversedStream0(&obp, out, bit); + setBitOfReversedStream(&obp, out, bit); } } } @@ -4127,8 +4256,7 @@ static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsig } static void removePaddingBits(unsigned char* out, const unsigned char* in, - size_t olinebits, size_t ilinebits, unsigned h) -{ + size_t olinebits, size_t ilinebits, unsigned h) { /* After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers @@ -4141,11 +4269,9 @@ static void removePaddingBits(unsigned char* out, const unsigned char* in, unsigned y; size_t diff = ilinebits - olinebits; size_t ibp = 0, obp = 0; /*input and output bit pointers*/ - for(y = 0; y < h; ++y) - { + for(y = 0; y < h; ++y) { size_t x; - for(x = 0; x < olinebits; ++x) - { + for(x = 0; x < olinebits; ++x) { unsigned char bit = readBitFromReversedStream(&ibp, in); setBitOfReversedStream(&obp, out, bit); } @@ -4157,46 +4283,39 @@ static void removePaddingBits(unsigned char* out, const unsigned char* in, the IDAT chunks (with filter index bytes and possible padding bits) return value is error*/ static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, - unsigned w, unsigned h, const LodePNGInfo* info_png) -{ + unsigned w, unsigned h, const LodePNGInfo* info_png) { /* This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. Steps: - *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8) + *) if no Adam7: 1) unfilter 2) remove padding bits (= possible extra bits per scanline if bpp < 8) *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace NOTE: the in buffer will be overwritten with intermediate data! */ unsigned bpp = lodepng_get_bpp(&info_png->color); if(bpp == 0) return 31; /*error: invalid colortype*/ - if(info_png->interlace_method == 0) - { - if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) - { + if(info_png->interlace_method == 0) { + if(bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) { CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp)); - removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h); + removePaddingBits(out, in, w * bpp, ((w * bpp + 7u) / 8u) * 8u, h); } /*we can immediately filter into the out buffer, no other steps needed*/ else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp)); - } - else /*interlace_method is 1 (Adam7)*/ - { + } else /*interlace_method is 1 (Adam7)*/ { unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; unsigned i; Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); - for(i = 0; i != 7; ++i) - { + for(i = 0; i != 7; ++i) { CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp)); /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, move bytes instead of bits or move not at all*/ - if(bpp < 8) - { + if(bpp < 8) { /*remove padding bits in scanlines; after this there still may be padding bits between the different reduced images: each reduced image still starts nicely at a byte*/ removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, - ((passw[i] * bpp + 7) / 8) * 8, passh[i]); + ((passw[i] * bpp + 7u) / 8u) * 8u, passh[i]); } } @@ -4206,21 +4325,17 @@ static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, return 0; } -static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) -{ +static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) { unsigned pos = 0, i; - if(color->palette) lodepng_free(color->palette); - color->palettesize = chunkLength / 3; - color->palette = (unsigned char*)lodepng_malloc(4 * color->palettesize); - if(!color->palette && color->palettesize) - { + color->palettesize = chunkLength / 3u; + if(color->palettesize == 0 || color->palettesize > 256) return 38; /*error: palette too small or big*/ + lodepng_color_mode_alloc_palette(color); + if(!color->palette && color->palettesize) { color->palettesize = 0; return 83; /*alloc fail*/ } - if(color->palettesize > 256) return 38; /*error: palette too big*/ - for(i = 0; i != color->palettesize; ++i) - { + for(i = 0; i != color->palettesize; ++i) { color->palette[4 * i + 0] = data[pos++]; /*R*/ color->palette[4 * i + 1] = data[pos++]; /*G*/ color->palette[4 * i + 2] = data[pos++]; /*B*/ @@ -4230,26 +4345,20 @@ static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* dat return 0; /* OK */ } -static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) -{ +static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) { unsigned i; - if(color->colortype == LCT_PALETTE) - { + if(color->colortype == LCT_PALETTE) { /*error: more alpha values given than there are palette entries*/ - if(chunkLength > color->palettesize) return 38; + if(chunkLength > color->palettesize) return 39; for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i]; - } - else if(color->colortype == LCT_GREY) - { - /*error: this chunk must be 2 bytes for greyscale image*/ + } else if(color->colortype == LCT_GREY) { + /*error: this chunk must be 2 bytes for grayscale image*/ if(chunkLength != 2) return 30; color->key_defined = 1; color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1]; - } - else if(color->colortype == LCT_RGB) - { + } else if(color->colortype == LCT_RGB) { /*error: this chunk must be 6 bytes for RGB image*/ if(chunkLength != 6) return 41; @@ -4266,29 +4375,28 @@ static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* dat #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*background color chunk (bKGD)*/ -static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) -{ - if(info->color.colortype == LCT_PALETTE) - { +static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { + if(info->color.colortype == LCT_PALETTE) { /*error: this chunk must be 1 byte for indexed color image*/ if(chunkLength != 1) return 43; + /*error: invalid palette index, or maybe this chunk appeared before PLTE*/ + if(data[0] >= info->color.palettesize) return 103; + info->background_defined = 1; info->background_r = info->background_g = info->background_b = data[0]; - } - else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) - { - /*error: this chunk must be 2 bytes for greyscale image*/ + } else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { + /*error: this chunk must be 2 bytes for grayscale image*/ if(chunkLength != 2) return 44; + /*the values are truncated to bitdepth in the PNG file*/ info->background_defined = 1; info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1]; - } - else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) - { - /*error: this chunk must be 6 bytes for greyscale image*/ + } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { + /*error: this chunk must be 6 bytes for grayscale image*/ if(chunkLength != 6) return 45; + /*the values are truncated to bitdepth in the PNG file*/ info->background_defined = 1; info->background_r = 256u * data[0] + data[1]; info->background_g = 256u * data[2] + data[3]; @@ -4299,14 +4407,11 @@ static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, siz } /*text chunk (tEXt)*/ -static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) -{ +static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { unsigned error = 0; char *key = 0, *str = 0; - unsigned i; - while(!error) /*not really a while loop, only used to break on error*/ - { + while(!error) /*not really a while loop, only used to break on error*/ { unsigned length, string2_begin; length = 0; @@ -4318,17 +4423,17 @@ static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, siz key = (char*)lodepng_malloc(length + 1); if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(key, data, length); key[length] = 0; - for(i = 0; i != length; ++i) key[i] = (char)data[i]; string2_begin = length + 1; /*skip keyword null terminator*/ - length = chunkLength < string2_begin ? 0 : chunkLength - string2_begin; + length = (unsigned)(chunkLength < string2_begin ? 0 : chunkLength - string2_begin); str = (char*)lodepng_malloc(length + 1); if(!str) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(str, data + string2_begin, length); str[length] = 0; - for(i = 0; i != length; ++i) str[i] = (char)data[string2_begin + i]; error = lodepng_add_text(info, key, str); @@ -4343,19 +4448,15 @@ static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, siz /*compressed text chunk (zTXt)*/ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, - const unsigned char* data, size_t chunkLength) -{ + const unsigned char* data, size_t chunkLength) { unsigned error = 0; - unsigned i; unsigned length, string2_begin; char *key = 0; - ucvector decoded; - - ucvector_init(&decoded); + unsigned char* str = 0; + size_t size = 0; - while(!error) /*not really a while loop, only used to break on error*/ - { + while(!error) /*not really a while loop, only used to break on error*/ { for(length = 0; length < chunkLength && data[length] != 0; ++length) ; if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/ @@ -4363,47 +4464,40 @@ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSetting key = (char*)lodepng_malloc(length + 1); if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(key, data, length); key[length] = 0; - for(i = 0; i != length; ++i) key[i] = (char)data[i]; if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ string2_begin = length + 2; if(string2_begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ - length = chunkLength - string2_begin; + length = (unsigned)chunkLength - string2_begin; /*will fail if zlib error, e.g. if length is too small*/ - error = zlib_decompress(&decoded.data, &decoded.size, - (unsigned char*)(&data[string2_begin]), + error = zlib_decompress(&str, &size, 0, &data[string2_begin], length, zlibsettings); if(error) break; - ucvector_push_back(&decoded, 0); - - error = lodepng_add_text(info, key, (char*)decoded.data); + error = lodepng_add_text_sized(info, key, (char*)str, size); break; } lodepng_free(key); - ucvector_cleanup(&decoded); + lodepng_free(str); return error; } /*international text chunk (iTXt)*/ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, - const unsigned char* data, size_t chunkLength) -{ + const unsigned char* data, size_t chunkLength) { unsigned error = 0; unsigned i; unsigned length, begin, compressed; char *key = 0, *langtag = 0, *transkey = 0; - ucvector decoded; - ucvector_init(&decoded); - while(!error) /*not really a while loop, only used to break on error*/ - { + while(!error) /*not really a while loop, only used to break on error*/ { /*Quick check if the chunk length isn't too small. Even without check it'd still fail with other error checks below if it's too short. This just gives a different error code.*/ if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/ @@ -4416,8 +4510,8 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting key = (char*)lodepng_malloc(length + 1); if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(key, data, length); key[length] = 0; - for(i = 0; i != length; ++i) key[i] = (char)data[i]; /*read the compression method*/ compressed = data[length + 1]; @@ -4434,8 +4528,8 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting langtag = (char*)lodepng_malloc(length + 1); if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(langtag, data + begin, length); langtag[length] = 0; - for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i]; /*read the transkey*/ begin += length + 1; @@ -4445,33 +4539,25 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting transkey = (char*)lodepng_malloc(length + 1); if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/ + lodepng_memcpy(transkey, data + begin, length); transkey[length] = 0; - for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i]; /*read the actual text*/ begin += length + 1; - length = chunkLength < begin ? 0 : chunkLength - begin; + length = (unsigned)chunkLength < begin ? 0 : (unsigned)chunkLength - begin; - if(compressed) - { + if(compressed) { + unsigned char* str = 0; + size_t size = 0; /*will fail if zlib error, e.g. if length is too small*/ - error = zlib_decompress(&decoded.data, &decoded.size, - (unsigned char*)(&data[begin]), + error = zlib_decompress(&str, &size, 0, &data[begin], length, zlibsettings); - if(error) break; - if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size; - ucvector_push_back(&decoded, 0); + if(!error) error = lodepng_add_itext_sized(info, key, langtag, transkey, (char*)str, size); + lodepng_free(str); + } else { + error = lodepng_add_itext_sized(info, key, langtag, transkey, (char*)(data + begin), length); } - else - { - if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/); - - decoded.data[length] = 0; - for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i]; - } - - error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data); break; } @@ -4479,13 +4565,11 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting lodepng_free(key); lodepng_free(langtag); lodepng_free(transkey); - ucvector_cleanup(&decoded); return error; } -static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) -{ +static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { if(chunkLength != 7) return 73; /*invalid tIME chunk size*/ info->time_defined = 1; @@ -4499,8 +4583,7 @@ static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, siz return 0; /* OK */ } -static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) -{ +static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { if(chunkLength != 9) return 74; /*invalid pHYs chunk size*/ info->phys_defined = 1; @@ -4510,20 +4593,139 @@ static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, siz return 0; /* OK */ } + +static unsigned readChunk_gAMA(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { + if(chunkLength != 4) return 96; /*invalid gAMA chunk size*/ + + info->gama_defined = 1; + info->gama_gamma = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; + + return 0; /* OK */ +} + +static unsigned readChunk_cHRM(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { + if(chunkLength != 32) return 97; /*invalid cHRM chunk size*/ + + info->chrm_defined = 1; + info->chrm_white_x = 16777216u * data[ 0] + 65536u * data[ 1] + 256u * data[ 2] + data[ 3]; + info->chrm_white_y = 16777216u * data[ 4] + 65536u * data[ 5] + 256u * data[ 6] + data[ 7]; + info->chrm_red_x = 16777216u * data[ 8] + 65536u * data[ 9] + 256u * data[10] + data[11]; + info->chrm_red_y = 16777216u * data[12] + 65536u * data[13] + 256u * data[14] + data[15]; + info->chrm_green_x = 16777216u * data[16] + 65536u * data[17] + 256u * data[18] + data[19]; + info->chrm_green_y = 16777216u * data[20] + 65536u * data[21] + 256u * data[22] + data[23]; + info->chrm_blue_x = 16777216u * data[24] + 65536u * data[25] + 256u * data[26] + data[27]; + info->chrm_blue_y = 16777216u * data[28] + 65536u * data[29] + 256u * data[30] + data[31]; + + return 0; /* OK */ +} + +static unsigned readChunk_sRGB(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) { + if(chunkLength != 1) return 98; /*invalid sRGB chunk size (this one is never ignored)*/ + + info->srgb_defined = 1; + info->srgb_intent = data[0]; + + return 0; /* OK */ +} + +static unsigned readChunk_iCCP(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, + const unsigned char* data, size_t chunkLength) { + unsigned error = 0; + unsigned i; + size_t size = 0; + + unsigned length, string2_begin; + + info->iccp_defined = 1; + if(info->iccp_name) lodepng_clear_icc(info); + + for(length = 0; length < chunkLength && data[length] != 0; ++length) ; + if(length + 2 >= chunkLength) return 75; /*no null termination, corrupt?*/ + if(length < 1 || length > 79) return 89; /*keyword too short or long*/ + + info->iccp_name = (char*)lodepng_malloc(length + 1); + if(!info->iccp_name) return 83; /*alloc fail*/ + + info->iccp_name[length] = 0; + for(i = 0; i != length; ++i) info->iccp_name[i] = (char)data[i]; + + if(data[length + 1] != 0) return 72; /*the 0 byte indicating compression must be 0*/ + + string2_begin = length + 2; + if(string2_begin > chunkLength) return 75; /*no null termination, corrupt?*/ + + length = (unsigned)chunkLength - string2_begin; + error = zlib_decompress(&info->iccp_profile, &size, 0, + &data[string2_begin], + length, zlibsettings); + info->iccp_profile_size = size; + if(!error && !info->iccp_profile_size) error = 100; /*invalid ICC profile size*/ + return error; +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +unsigned lodepng_inspect_chunk(LodePNGState* state, size_t pos, + const unsigned char* in, size_t insize) { + const unsigned char* chunk = in + pos; + unsigned chunkLength; + const unsigned char* data; + unsigned unhandled = 0; + unsigned error = 0; + + if(pos + 4 > insize) return 30; + chunkLength = lodepng_chunk_length(chunk); + if(chunkLength > 2147483647) return 63; + data = lodepng_chunk_data_const(chunk); + if(data + chunkLength + 4 > in + insize) return 30; + + if(lodepng_chunk_type_equals(chunk, "PLTE")) { + error = readChunk_PLTE(&state->info_png.color, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "tRNS")) { + error = readChunk_tRNS(&state->info_png.color, data, chunkLength); +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + } else if(lodepng_chunk_type_equals(chunk, "bKGD")) { + error = readChunk_bKGD(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "tEXt")) { + error = readChunk_tEXt(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "zTXt")) { + error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "iTXt")) { + error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "tIME")) { + error = readChunk_tIME(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "pHYs")) { + error = readChunk_pHYs(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "gAMA")) { + error = readChunk_gAMA(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "cHRM")) { + error = readChunk_cHRM(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "sRGB")) { + error = readChunk_sRGB(&state->info_png, data, chunkLength); + } else if(lodepng_chunk_type_equals(chunk, "iCCP")) { + error = readChunk_iCCP(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + } else { + /* unhandled chunk is ok (is not an error) */ + unhandled = 1; + } + + if(!error && !unhandled && !state->decoder.ignore_crc) { + if(lodepng_chunk_check_crc(chunk)) return 57; /*invalid CRC*/ + } + + return error; +} /*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, LodePNGState* state, - const unsigned char* in, size_t insize) -{ + const unsigned char* in, size_t insize) { unsigned char IEND = 0; const unsigned char* chunk; - size_t i; - ucvector idat; /*the data from idat chunks*/ - ucvector scanlines; - size_t predict; - size_t numpixels; + unsigned char* idat; /*the data from idat chunks, zlib compressed*/ + size_t idatsize = 0; + unsigned char* scanlines = 0; + size_t scanlines_size = 0, expected_size = 0; size_t outsize = 0; /*for unknown chunk order*/ @@ -4532,129 +4734,129 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/ #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - /*provide some proper output values if error will happen*/ + + /* safe output values in case error happens */ *out = 0; + *w = *h = 0; state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/ if(state->error) return; - numpixels = *w * *h; + if(lodepng_pixel_overflow(*w, *h, &state->info_png.color, &state->info_raw)) { + CERROR_RETURN(state->error, 92); /*overflow possible due to amount of pixels*/ + } - /*multiplication overflow*/ - if(*h != 0 && numpixels / *h != *w) CERROR_RETURN(state->error, 92); - /*multiplication overflow possible further below. Allows up to 2^31-1 pixel - bytes with 16-bit RGBA, the rest is room for filter bytes.*/ - if(numpixels > 268435455) CERROR_RETURN(state->error, 92); + /*the input filesize is a safe upper bound for the sum of idat chunks size*/ + idat = (unsigned char*)lodepng_malloc(insize); + if(!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/ - ucvector_init(&idat); chunk = &in[33]; /*first byte of the first chunk after the header*/ /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is put at the start of the in buffer*/ - while(!IEND && !state->error) - { + while(!IEND && !state->error) { unsigned chunkLength; const unsigned char* data; /*the data in the chunk*/ /*error: size of the in buffer too small to contain next chunk*/ - if((size_t)((chunk - in) + 12) > insize || chunk < in) CERROR_BREAK(state->error, 30); + if((size_t)((chunk - in) + 12) > insize || chunk < in) { + if(state->decoder.ignore_end) break; /*other errors may still happen though*/ + CERROR_BREAK(state->error, 30); + } /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/ chunkLength = lodepng_chunk_length(chunk); /*error: chunk length larger than the max PNG chunk size*/ - if(chunkLength > 2147483647) CERROR_BREAK(state->error, 63); + if(chunkLength > 2147483647) { + if(state->decoder.ignore_end) break; /*other errors may still happen though*/ + CERROR_BREAK(state->error, 63); + } - if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) - { + if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) { CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/ } data = lodepng_chunk_data_const(chunk); + unknown = 0; + /*IDAT chunk, containing compressed image data*/ - if(lodepng_chunk_type_equals(chunk, "IDAT")) - { - size_t oldsize = idat.size; - if(!ucvector_resize(&idat, oldsize + chunkLength)) CERROR_BREAK(state->error, 83 /*alloc fail*/); - for(i = 0; i != chunkLength; ++i) idat.data[oldsize + i] = data[i]; + if(lodepng_chunk_type_equals(chunk, "IDAT")) { + size_t newsize; + if(lodepng_addofl(idatsize, chunkLength, &newsize)) CERROR_BREAK(state->error, 95); + if(newsize > insize) CERROR_BREAK(state->error, 95); + lodepng_memcpy(idat + idatsize, data, chunkLength); + idatsize += chunkLength; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS critical_pos = 3; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - } - /*IEND chunk*/ - else if(lodepng_chunk_type_equals(chunk, "IEND")) - { + } else if(lodepng_chunk_type_equals(chunk, "IEND")) { + /*IEND chunk*/ IEND = 1; - } - /*palette chunk (PLTE)*/ - else if(lodepng_chunk_type_equals(chunk, "PLTE")) - { + } else if(lodepng_chunk_type_equals(chunk, "PLTE")) { + /*palette chunk (PLTE)*/ state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength); if(state->error) break; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS critical_pos = 2; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - } - /*palette transparency chunk (tRNS)*/ - else if(lodepng_chunk_type_equals(chunk, "tRNS")) - { + } else if(lodepng_chunk_type_equals(chunk, "tRNS")) { + /*palette transparency chunk (tRNS). Even though this one is an ancillary chunk , it is still compiled + in without 'LODEPNG_COMPILE_ANCILLARY_CHUNKS' because it contains essential color information that + affects the alpha channel of pixels. */ state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength); if(state->error) break; - } #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS - /*background color chunk (bKGD)*/ - else if(lodepng_chunk_type_equals(chunk, "bKGD")) - { + /*background color chunk (bKGD)*/ + } else if(lodepng_chunk_type_equals(chunk, "bKGD")) { state->error = readChunk_bKGD(&state->info_png, data, chunkLength); if(state->error) break; - } - /*text chunk (tEXt)*/ - else if(lodepng_chunk_type_equals(chunk, "tEXt")) - { - if(state->decoder.read_text_chunks) - { + } else if(lodepng_chunk_type_equals(chunk, "tEXt")) { + /*text chunk (tEXt)*/ + if(state->decoder.read_text_chunks) { state->error = readChunk_tEXt(&state->info_png, data, chunkLength); if(state->error) break; } - } - /*compressed text chunk (zTXt)*/ - else if(lodepng_chunk_type_equals(chunk, "zTXt")) - { - if(state->decoder.read_text_chunks) - { + } else if(lodepng_chunk_type_equals(chunk, "zTXt")) { + /*compressed text chunk (zTXt)*/ + if(state->decoder.read_text_chunks) { state->error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); if(state->error) break; } - } - /*international text chunk (iTXt)*/ - else if(lodepng_chunk_type_equals(chunk, "iTXt")) - { - if(state->decoder.read_text_chunks) - { + } else if(lodepng_chunk_type_equals(chunk, "iTXt")) { + /*international text chunk (iTXt)*/ + if(state->decoder.read_text_chunks) { state->error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); if(state->error) break; } - } - else if(lodepng_chunk_type_equals(chunk, "tIME")) - { + } else if(lodepng_chunk_type_equals(chunk, "tIME")) { state->error = readChunk_tIME(&state->info_png, data, chunkLength); if(state->error) break; - } - else if(lodepng_chunk_type_equals(chunk, "pHYs")) - { + } else if(lodepng_chunk_type_equals(chunk, "pHYs")) { state->error = readChunk_pHYs(&state->info_png, data, chunkLength); if(state->error) break; - } + } else if(lodepng_chunk_type_equals(chunk, "gAMA")) { + state->error = readChunk_gAMA(&state->info_png, data, chunkLength); + if(state->error) break; + } else if(lodepng_chunk_type_equals(chunk, "cHRM")) { + state->error = readChunk_cHRM(&state->info_png, data, chunkLength); + if(state->error) break; + } else if(lodepng_chunk_type_equals(chunk, "sRGB")) { + state->error = readChunk_sRGB(&state->info_png, data, chunkLength); + if(state->error) break; + } else if(lodepng_chunk_type_equals(chunk, "iCCP")) { + state->error = readChunk_iCCP(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); + if(state->error) break; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - else /*it's not an implemented chunk type, so ignore it: skip over the data*/ - { + } else /*it's not an implemented chunk type, so ignore it: skip over the data*/ { /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/ - if(!lodepng_chunk_ancillary(chunk)) CERROR_BREAK(state->error, 69); + if(!state->decoder.ignore_critical && !lodepng_chunk_ancillary(chunk)) { + CERROR_BREAK(state->error, 69); + } unknown = 1; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS - if(state->decoder.remember_unknown_chunks) - { + if(state->decoder.remember_unknown_chunks) { state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1], &state->info_png.unknown_chunks_size[critical_pos - 1], chunk); if(state->error) break; @@ -4662,94 +4864,81 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } - if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ - { + if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ { if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/ } - if(!IEND) chunk = lodepng_chunk_next_const(chunk); - } - - ucvector_init(&scanlines); - /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation. - If the decompressed size does not match the prediction, the image must be corrupt.*/ - if(state->info_png.interlace_method == 0) - { - /*The extra *h is added because this are the filter bytes every scanline starts with*/ - predict = lodepng_get_raw_size_idat(*w, *h, &state->info_png.color) + *h; - } - else - { - /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/ - const LodePNGColorMode* color = &state->info_png.color; - predict = 0; - predict += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3); - if(*w > 4) predict += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3); - predict += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, color) + ((*h + 3) >> 3); - if(*w > 2) predict += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, color) + ((*h + 3) >> 2); - predict += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, color) + ((*h + 1) >> 2); - if(*w > 1) predict += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, color) + ((*h + 1) >> 1); - predict += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, color) + ((*h + 0) >> 1); - } - if(!state->error && !ucvector_reserve(&scanlines, predict)) state->error = 83; /*alloc fail*/ - if(!state->error) - { - state->error = zlib_decompress(&scanlines.data, &scanlines.size, idat.data, - idat.size, &state->decoder.zlibsettings); - if(!state->error && scanlines.size != predict) state->error = 91; /*decompressed size doesn't match prediction*/ - } - ucvector_cleanup(&idat); - - if(!state->error) - { + if(!IEND) chunk = lodepng_chunk_next_const(chunk, in + insize); + } + + if(state->info_png.color.colortype == LCT_PALETTE && !state->info_png.color.palette) { + state->error = 106; /* error: PNG file must have PLTE chunk if color type is palette */ + } + + if(!state->error) { + /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation. + If the decompressed size does not match the prediction, the image must be corrupt.*/ + if(state->info_png.interlace_method == 0) { + size_t bpp = lodepng_get_bpp(&state->info_png.color); + expected_size = lodepng_get_raw_size_idat(*w, *h, bpp); + } else { + size_t bpp = lodepng_get_bpp(&state->info_png.color); + /*Adam-7 interlaced: expected size is the sum of the 7 sub-images sizes*/ + expected_size = 0; + expected_size += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, bpp); + if(*w > 4) expected_size += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, bpp); + expected_size += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, bpp); + if(*w > 2) expected_size += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, bpp); + expected_size += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, bpp); + if(*w > 1) expected_size += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, bpp); + expected_size += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, bpp); + } + + state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, &state->decoder.zlibsettings); + } + if(!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn't match prediction*/ + lodepng_free(idat); + + if(!state->error) { outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color); *out = (unsigned char*)lodepng_malloc(outsize); if(!*out) state->error = 83; /*alloc fail*/ } - if(!state->error) - { - for(i = 0; i < outsize; i++) (*out)[i] = 0; - state->error = postProcessScanlines(*out, scanlines.data, *w, *h, &state->info_png); + if(!state->error) { + lodepng_memset(*out, 0, outsize); + state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png); } - ucvector_cleanup(&scanlines); + lodepng_free(scanlines); } unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, LodePNGState* state, - const unsigned char* in, size_t insize) -{ + const unsigned char* in, size_t insize) { *out = 0; decodeGeneric(out, w, h, state, in, insize); if(state->error) return state->error; - if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) - { + if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) { /*same color type, no copying or converting of data needed*/ /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype the raw image has to the end user*/ - if(!state->decoder.color_convert) - { + if(!state->decoder.color_convert) { state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color); if(state->error) return state->error; } - } - else - { - /*color conversion needed; sort of copy of the data*/ + } else { /*color conversion needed*/ unsigned char* data = *out; size_t outsize; /*TODO: check if this works according to the statement in the documentation: "The converter can convert - from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/ + from grayscale input color type, to 8-bit grayscale or grayscale with alpha"*/ if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) - && !(state->info_raw.bitdepth == 8)) - { + && !(state->info_raw.bitdepth == 8)) { return 56; /*unsupported color mode conversion*/ } outsize = lodepng_get_raw_size(*w, *h, &state->info_raw); *out = (unsigned char*)lodepng_malloc(outsize); - if(!(*out)) - { + if(!(*out)) { state->error = 83; /*alloc fail*/ } else state->error = lodepng_convert(*out, data, &state->info_raw, @@ -4760,8 +4949,7 @@ unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, } unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, - size_t insize, LodePNGColorType colortype, unsigned bitdepth) -{ + size_t insize, LodePNGColorType colortype, unsigned bitdepth) { unsigned error; LodePNGState state; lodepng_state_init(&state); @@ -4772,48 +4960,47 @@ unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, co return error; } -unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) -{ +unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) { return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8); } -unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) -{ +unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) { return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8); } #ifdef LODEPNG_COMPILE_DISK unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { unsigned char* buffer = 0; size_t buffersize; unsigned error; + /* safe output values in case error happens */ + *out = 0; + *w = *h = 0; error = lodepng_load_file(&buffer, &buffersize, filename); if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth); lodepng_free(buffer); return error; } -unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) -{ +unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) { return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8); } -unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) -{ +unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) { return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8); } #endif /*LODEPNG_COMPILE_DISK*/ -void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) -{ +void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) { settings->color_convert = 1; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS settings->read_text_chunks = 1; settings->remember_unknown_chunks = 0; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ settings->ignore_crc = 0; + settings->ignore_critical = 0; + settings->ignore_end = 0; lodepng_decompress_settings_init(&settings->zlibsettings); } @@ -4821,8 +5008,7 @@ void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) -void lodepng_state_init(LodePNGState* state) -{ +void lodepng_state_init(LodePNGState* state) { #ifdef LODEPNG_COMPILE_DECODER lodepng_decoder_settings_init(&state->decoder); #endif /*LODEPNG_COMPILE_DECODER*/ @@ -4834,14 +5020,12 @@ void lodepng_state_init(LodePNGState* state) state->error = 1; } -void lodepng_state_cleanup(LodePNGState* state) -{ +void lodepng_state_cleanup(LodePNGState* state) { lodepng_color_mode_cleanup(&state->info_raw); lodepng_info_cleanup(&state->info_png); } -void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) -{ +void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) { lodepng_state_cleanup(dest); *dest = *source; lodepng_color_mode_init(&dest->info_raw); @@ -4858,289 +5042,300 @@ void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) /* / PNG Encoder / */ /* ////////////////////////////////////////////////////////////////////////// */ -/*chunkName must be string of 4 characters*/ -static unsigned addChunk(ucvector* out, const char* chunkName, const unsigned char* data, size_t length) -{ - CERROR_TRY_RETURN(lodepng_chunk_create(&out->data, &out->size, (unsigned)length, chunkName, data)); - out->allocsize = out->size; /*fix the allocsize again*/ - return 0; -} -static void writeSignature(ucvector* out) -{ +static unsigned writeSignature(ucvector* out) { + size_t pos = out->size; + const unsigned char signature[] = {137, 80, 78, 71, 13, 10, 26, 10}; /*8 bytes PNG signature, aka the magic bytes*/ - ucvector_push_back(out, 137); - ucvector_push_back(out, 80); - ucvector_push_back(out, 78); - ucvector_push_back(out, 71); - ucvector_push_back(out, 13); - ucvector_push_back(out, 10); - ucvector_push_back(out, 26); - ucvector_push_back(out, 10); + if(!ucvector_resize(out, out->size + 8)) return 83; /*alloc fail*/ + lodepng_memcpy(out->data + pos, signature, 8); + return 0; } static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method) -{ - unsigned error = 0; - ucvector header; - ucvector_init(&header); + LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method) { + unsigned char *chunk, *data; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 13, "IHDR")); + data = chunk + 8; + + lodepng_set32bitInt(data + 0, w); /*width*/ + lodepng_set32bitInt(data + 4, h); /*height*/ + data[8] = (unsigned char)bitdepth; /*bit depth*/ + data[9] = (unsigned char)colortype; /*color type*/ + data[10] = 0; /*compression method*/ + data[11] = 0; /*filter method*/ + data[12] = interlace_method; /*interlace method*/ - lodepng_add32bitInt(&header, w); /*width*/ - lodepng_add32bitInt(&header, h); /*height*/ - ucvector_push_back(&header, (unsigned char)bitdepth); /*bit depth*/ - ucvector_push_back(&header, (unsigned char)colortype); /*color type*/ - ucvector_push_back(&header, 0); /*compression method*/ - ucvector_push_back(&header, 0); /*filter method*/ - ucvector_push_back(&header, interlace_method); /*interlace method*/ + lodepng_chunk_generate_crc(chunk); + return 0; +} - error = addChunk(out, "IHDR", header.data, header.size); - ucvector_cleanup(&header); +/* only adds the chunk if needed (there is a key or palette with alpha) */ +static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info) { + unsigned char* chunk; + size_t i, j = 8; - return error; -} + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, info->palettesize * 3, "PLTE")); -static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info) -{ - unsigned error = 0; - size_t i; - ucvector PLTE; - ucvector_init(&PLTE); - for(i = 0; i != info->palettesize * 4; ++i) - { + for(i = 0; i != info->palettesize; ++i) { /*add all channels except alpha channel*/ - if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]); + chunk[j++] = info->palette[i * 4 + 0]; + chunk[j++] = info->palette[i * 4 + 1]; + chunk[j++] = info->palette[i * 4 + 2]; } - error = addChunk(out, "PLTE", PLTE.data, PLTE.size); - ucvector_cleanup(&PLTE); - return error; + lodepng_chunk_generate_crc(chunk); + return 0; } -static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info) -{ - unsigned error = 0; - size_t i; - ucvector tRNS; - ucvector_init(&tRNS); - if(info->colortype == LCT_PALETTE) - { - size_t amount = info->palettesize; +static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info) { + unsigned char* chunk = 0; + + if(info->colortype == LCT_PALETTE) { + size_t i, amount = info->palettesize; /*the tail of palette values that all have 255 as alpha, does not have to be encoded*/ - for(i = info->palettesize; i != 0; --i) - { - if(info->palette[4 * (i - 1) + 3] == 255) --amount; - else break; + for(i = info->palettesize; i != 0; --i) { + if(info->palette[4 * (i - 1) + 3] != 255) break; + --amount; } - /*add only alpha channel*/ - for(i = 0; i != amount; ++i) ucvector_push_back(&tRNS, info->palette[4 * i + 3]); - } - else if(info->colortype == LCT_GREY) - { - if(info->key_defined) - { - ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255)); + if(amount) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, amount, "tRNS")); + /*add the alpha channel values from the palette*/ + for(i = 0; i != amount; ++i) chunk[8 + i] = info->palette[4 * i + 3]; } - } - else if(info->colortype == LCT_RGB) - { - if(info->key_defined) - { - ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_g >> 8)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_g & 255)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_b >> 8)); - ucvector_push_back(&tRNS, (unsigned char)(info->key_b & 255)); + } else if(info->colortype == LCT_GREY) { + if(info->key_defined) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 2, "tRNS")); + chunk[8] = (unsigned char)(info->key_r >> 8); + chunk[9] = (unsigned char)(info->key_r & 255); + } + } else if(info->colortype == LCT_RGB) { + if(info->key_defined) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 6, "tRNS")); + chunk[8] = (unsigned char)(info->key_r >> 8); + chunk[9] = (unsigned char)(info->key_r & 255); + chunk[10] = (unsigned char)(info->key_g >> 8); + chunk[11] = (unsigned char)(info->key_g & 255); + chunk[12] = (unsigned char)(info->key_b >> 8); + chunk[13] = (unsigned char)(info->key_b & 255); } } - error = addChunk(out, "tRNS", tRNS.data, tRNS.size); - ucvector_cleanup(&tRNS); - - return error; + if(chunk) lodepng_chunk_generate_crc(chunk); + return 0; } static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize, - LodePNGCompressSettings* zlibsettings) -{ - ucvector zlibdata; + LodePNGCompressSettings* zlibsettings) { unsigned error = 0; + unsigned char* zlib = 0; + size_t zlibsize = 0; - /*compress with the Zlib compressor*/ - ucvector_init(&zlibdata); - error = zlib_compress(&zlibdata.data, &zlibdata.size, data, datasize, zlibsettings); - if(!error) error = addChunk(out, "IDAT", zlibdata.data, zlibdata.size); - ucvector_cleanup(&zlibdata); - + error = zlib_compress(&zlib, &zlibsize, data, datasize, zlibsettings); + if(!error) { + error = lodepng_chunk_createv(out, zlibsize, "IDAT", zlib); + } + lodepng_free(zlib); return error; } -static unsigned addChunk_IEND(ucvector* out) -{ - unsigned error = 0; - error = addChunk(out, "IEND", 0, 0); - return error; +static unsigned addChunk_IEND(ucvector* out) { + return lodepng_chunk_createv(out, 0, "IEND", 0); } #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring) -{ - unsigned error = 0; - size_t i; - ucvector text; - ucvector_init(&text); - for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)keyword[i]); - if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ - ucvector_push_back(&text, 0); /*0 termination char*/ - for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)textstring[i]); - error = addChunk(out, "tEXt", text.data, text.size); - ucvector_cleanup(&text); - - return error; +static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring) { + unsigned char* chunk = 0; + size_t keysize = lodepng_strlen(keyword), textsize = lodepng_strlen(textstring); + size_t size = keysize + 1 + textsize; + if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/ + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, size, "tEXt")); + lodepng_memcpy(chunk + 8, keyword, keysize); + chunk[8 + keysize] = 0; /*null termination char*/ + lodepng_memcpy(chunk + 9 + keysize, textstring, textsize); + lodepng_chunk_generate_crc(chunk); + return 0; } static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* textstring, - LodePNGCompressSettings* zlibsettings) -{ + LodePNGCompressSettings* zlibsettings) { unsigned error = 0; - ucvector data, compressed; - size_t i, textsize = strlen(textstring); - - ucvector_init(&data); - ucvector_init(&compressed); - for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]); - if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ - ucvector_push_back(&data, 0); /*0 termination char*/ - ucvector_push_back(&data, 0); /*compression method: 0*/ - - error = zlib_compress(&compressed.data, &compressed.size, - (unsigned char*)textstring, textsize, zlibsettings); - if(!error) - { - for(i = 0; i != compressed.size; ++i) ucvector_push_back(&data, compressed.data[i]); - error = addChunk(out, "zTXt", data.data, data.size); - } - - ucvector_cleanup(&compressed); - ucvector_cleanup(&data); + unsigned char* chunk = 0; + unsigned char* compressed = 0; + size_t compressedsize = 0; + size_t textsize = lodepng_strlen(textstring); + size_t keysize = lodepng_strlen(keyword); + if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/ + + error = zlib_compress(&compressed, &compressedsize, + (const unsigned char*)textstring, textsize, zlibsettings); + if(!error) { + size_t size = keysize + 2 + compressedsize; + error = lodepng_chunk_init(&chunk, out, size, "zTXt"); + } + if(!error) { + lodepng_memcpy(chunk + 8, keyword, keysize); + chunk[8 + keysize] = 0; /*null termination char*/ + chunk[9 + keysize] = 0; /*compression method: 0*/ + lodepng_memcpy(chunk + 10 + keysize, compressed, compressedsize); + lodepng_chunk_generate_crc(chunk); + } + + lodepng_free(compressed); return error; } -static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* keyword, const char* langtag, - const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) -{ +static unsigned addChunk_iTXt(ucvector* out, unsigned compress, const char* keyword, const char* langtag, + const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) { unsigned error = 0; - ucvector data; - size_t i, textsize = strlen(textstring); - - ucvector_init(&data); - - for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]); - if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ - ucvector_push_back(&data, 0); /*null termination char*/ - ucvector_push_back(&data, compressed ? 1 : 0); /*compression flag*/ - ucvector_push_back(&data, 0); /*compression method*/ - for(i = 0; langtag[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)langtag[i]); - ucvector_push_back(&data, 0); /*null termination char*/ - for(i = 0; transkey[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)transkey[i]); - ucvector_push_back(&data, 0); /*null termination char*/ - - if(compressed) - { - ucvector compressed_data; - ucvector_init(&compressed_data); - error = zlib_compress(&compressed_data.data, &compressed_data.size, - (unsigned char*)textstring, textsize, zlibsettings); - if(!error) - { - for(i = 0; i != compressed_data.size; ++i) ucvector_push_back(&data, compressed_data.data[i]); - } - ucvector_cleanup(&compressed_data); - } - else /*not compressed*/ - { - for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)textstring[i]); - } - - if(!error) error = addChunk(out, "iTXt", data.data, data.size); - ucvector_cleanup(&data); + unsigned char* chunk = 0; + unsigned char* compressed = 0; + size_t compressedsize = 0; + size_t textsize = lodepng_strlen(textstring); + size_t keysize = lodepng_strlen(keyword), langsize = lodepng_strlen(langtag), transsize = lodepng_strlen(transkey); + + if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/ + + if(compress) { + error = zlib_compress(&compressed, &compressedsize, + (const unsigned char*)textstring, textsize, zlibsettings); + } + if(!error) { + size_t size = keysize + 3 + langsize + 1 + transsize + 1 + (compress ? compressedsize : textsize); + error = lodepng_chunk_init(&chunk, out, size, "iTXt"); + } + if(!error) { + size_t pos = 8; + lodepng_memcpy(chunk + pos, keyword, keysize); + pos += keysize; + chunk[pos++] = 0; /*null termination char*/ + chunk[pos++] = (compress ? 1 : 0); /*compression flag*/ + chunk[pos++] = 0; /*compression method: 0*/ + lodepng_memcpy(chunk + pos, langtag, langsize); + pos += langsize; + chunk[pos++] = 0; /*null termination char*/ + lodepng_memcpy(chunk + pos, transkey, transsize); + pos += transsize; + chunk[pos++] = 0; /*null termination char*/ + if(compress) { + lodepng_memcpy(chunk + pos, compressed, compressedsize); + } else { + lodepng_memcpy(chunk + pos, textstring, textsize); + } + lodepng_chunk_generate_crc(chunk); + } + + lodepng_free(compressed); return error; } -static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info) -{ - unsigned error = 0; - ucvector bKGD; - ucvector_init(&bKGD); - if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) - { - ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); - } - else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) - { - ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_g >> 8)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_g & 255)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_b >> 8)); - ucvector_push_back(&bKGD, (unsigned char)(info->background_b & 255)); - } - else if(info->color.colortype == LCT_PALETTE) - { - ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); /*palette index*/ - } - - error = addChunk(out, "bKGD", bKGD.data, bKGD.size); - ucvector_cleanup(&bKGD); +static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info) { + unsigned char* chunk = 0; + if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 2, "bKGD")); + chunk[8] = (unsigned char)(info->background_r >> 8); + chunk[9] = (unsigned char)(info->background_r & 255); + } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 6, "bKGD")); + chunk[8] = (unsigned char)(info->background_r >> 8); + chunk[9] = (unsigned char)(info->background_r & 255); + chunk[10] = (unsigned char)(info->background_g >> 8); + chunk[11] = (unsigned char)(info->background_g & 255); + chunk[12] = (unsigned char)(info->background_b >> 8); + chunk[13] = (unsigned char)(info->background_b & 255); + } else if(info->color.colortype == LCT_PALETTE) { + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 1, "bKGD")); + chunk[8] = (unsigned char)(info->background_r & 255); /*palette index*/ + } + if(chunk) lodepng_chunk_generate_crc(chunk); + return 0; +} - return error; +static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time) { + unsigned char* chunk; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 7, "tIME")); + chunk[8] = (unsigned char)(time->year >> 8); + chunk[9] = (unsigned char)(time->year & 255); + chunk[10] = (unsigned char)time->month; + chunk[11] = (unsigned char)time->day; + chunk[12] = (unsigned char)time->hour; + chunk[13] = (unsigned char)time->minute; + chunk[14] = (unsigned char)time->second; + lodepng_chunk_generate_crc(chunk); + return 0; } -static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time) -{ - unsigned error = 0; - unsigned char* data = (unsigned char*)lodepng_malloc(7); - if(!data) return 83; /*alloc fail*/ - data[0] = (unsigned char)(time->year >> 8); - data[1] = (unsigned char)(time->year & 255); - data[2] = (unsigned char)time->month; - data[3] = (unsigned char)time->day; - data[4] = (unsigned char)time->hour; - data[5] = (unsigned char)time->minute; - data[6] = (unsigned char)time->second; - error = addChunk(out, "tIME", data, 7); - lodepng_free(data); - return error; +static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info) { + unsigned char* chunk; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 9, "pHYs")); + lodepng_set32bitInt(chunk + 8, info->phys_x); + lodepng_set32bitInt(chunk + 12, info->phys_y); + chunk[16] = info->phys_unit; + lodepng_chunk_generate_crc(chunk); + return 0; } -static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info) -{ - unsigned error = 0; - ucvector data; - ucvector_init(&data); +static unsigned addChunk_gAMA(ucvector* out, const LodePNGInfo* info) { + unsigned char* chunk; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 4, "gAMA")); + lodepng_set32bitInt(chunk + 8, info->gama_gamma); + lodepng_chunk_generate_crc(chunk); + return 0; +} - lodepng_add32bitInt(&data, info->phys_x); - lodepng_add32bitInt(&data, info->phys_y); - ucvector_push_back(&data, info->phys_unit); +static unsigned addChunk_cHRM(ucvector* out, const LodePNGInfo* info) { + unsigned char* chunk; + CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 32, "cHRM")); + lodepng_set32bitInt(chunk + 8, info->chrm_white_x); + lodepng_set32bitInt(chunk + 12, info->chrm_white_y); + lodepng_set32bitInt(chunk + 16, info->chrm_red_x); + lodepng_set32bitInt(chunk + 20, info->chrm_red_y); + lodepng_set32bitInt(chunk + 24, info->chrm_green_x); + lodepng_set32bitInt(chunk + 28, info->chrm_green_y); + lodepng_set32bitInt(chunk + 32, info->chrm_blue_x); + lodepng_set32bitInt(chunk + 36, info->chrm_blue_y); + lodepng_chunk_generate_crc(chunk); + return 0; +} - error = addChunk(out, "pHYs", data.data, data.size); - ucvector_cleanup(&data); +static unsigned addChunk_sRGB(ucvector* out, const LodePNGInfo* info) { + unsigned char data = info->srgb_intent; + return lodepng_chunk_createv(out, 1, "sRGB", &data); +} +static unsigned addChunk_iCCP(ucvector* out, const LodePNGInfo* info, LodePNGCompressSettings* zlibsettings) { + unsigned error = 0; + unsigned char* chunk = 0; + unsigned char* compressed = 0; + size_t compressedsize = 0; + size_t keysize = lodepng_strlen(info->iccp_name); + + if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/ + error = zlib_compress(&compressed, &compressedsize, + info->iccp_profile, info->iccp_profile_size, zlibsettings); + if(!error) { + size_t size = keysize + 2 + compressedsize; + error = lodepng_chunk_init(&chunk, out, size, "iCCP"); + } + if(!error) { + lodepng_memcpy(chunk + 8, info->iccp_name, keysize); + chunk[8 + keysize] = 0; /*null termination char*/ + chunk[9 + keysize] = 0; /*compression method: 0*/ + lodepng_memcpy(chunk + 10 + keysize, compressed, compressedsize); + lodepng_chunk_generate_crc(chunk); + } + + lodepng_free(compressed); return error; } #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline, - size_t length, size_t bytewidth, unsigned char filterType) -{ + size_t length, size_t bytewidth, unsigned char filterType) { size_t i; - switch(filterType) - { + switch(filterType) { case 0: /*None*/ for(i = 0; i != length; ++i) out[i] = scanline[i]; break; @@ -5149,71 +5344,73 @@ static void filterScanline(unsigned char* out, const unsigned char* scanline, co for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth]; break; case 2: /*Up*/ - if(prevline) - { + if(prevline) { for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i]; - } - else - { + } else { for(i = 0; i != length; ++i) out[i] = scanline[i]; } break; case 3: /*Average*/ - if(prevline) - { + if(prevline) { for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1); for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) >> 1); - } - else - { + } else { for(i = 0; i != bytewidth; ++i) out[i] = scanline[i]; for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1); } break; case 4: /*Paeth*/ - if(prevline) - { + if(prevline) { /*paethPredictor(0, prevline[i], 0) is always prevline[i]*/ for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]); - for(i = bytewidth; i < length; ++i) - { + for(i = bytewidth; i < length; ++i) { out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth])); } - } - else - { + } else { for(i = 0; i != bytewidth; ++i) out[i] = scanline[i]; /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/ for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]); } break; - default: return; /*unexisting filter type given*/ + default: return; /*invalid filter type given*/ } } -/* log2 approximation. A slight bit faster than std::log. */ -static float flog2(float f) -{ - float result = 0; - while(f > 32) { result += 4; f /= 16; } - while(f > 2) { ++result; f /= 2; } - return result + 1.442695f * (f * f * f / 3 - 3 * f * f / 2 + 3 * f - 1.83333f); +/* integer binary logarithm, max return value is 31 */ +static size_t ilog2(size_t i) { + size_t result = 0; + if(i >= 65536) { result += 16; i >>= 16; } + if(i >= 256) { result += 8; i >>= 8; } + if(i >= 16) { result += 4; i >>= 4; } + if(i >= 4) { result += 2; i >>= 2; } + if(i >= 2) { result += 1; /*i >>= 1;*/ } + return result; +} + +/* integer approximation for i * log2(i), helper function for LFS_ENTROPY */ +static size_t ilog2i(size_t i) { + size_t l; + if(i == 0) return 0; + l = ilog2(i); + /* approximate i*log2(i): l is integer logarithm, ((i - (1u << l)) << 1u) + linearly approximates the missing fractional part multiplied by i */ + return i * l + ((i - (1u << l)) << 1u); } static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, - const LodePNGColorMode* info, const LodePNGEncoderSettings* settings) -{ + const LodePNGColorMode* color, const LodePNGEncoderSettings* settings) { /* For PNG filter method 0 - out must be a buffer with as size: h + (w * h * bpp + 7) / 8, because there are + out must be a buffer with as size: h + (w * h * bpp + 7u) / 8u, because there are the scanlines with 1 extra byte per scanline */ - unsigned bpp = lodepng_get_bpp(info); + unsigned bpp = lodepng_get_bpp(color); /*the width of a scanline in bytes, not including the filter type*/ - size_t linebytes = (w * bpp + 7) / 8; + size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; + /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ - size_t bytewidth = (bpp + 7) / 8; + size_t bytewidth = (bpp + 7u) / 8u; const unsigned char* prevline = 0; unsigned x, y; unsigned error = 0; @@ -5233,67 +5430,54 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, heuristic is used. */ if(settings->filter_palette_zero && - (info->colortype == LCT_PALETTE || info->bitdepth < 8)) strategy = LFS_ZERO; + (color->colortype == LCT_PALETTE || color->bitdepth < 8)) strategy = LFS_ZERO; if(bpp == 0) return 31; /*error: invalid color type*/ - if(strategy == LFS_ZERO) - { - for(y = 0; y != h; ++y) - { + if(strategy >= LFS_ZERO && strategy <= LFS_FOUR) { + unsigned char type = (unsigned char)strategy; + for(y = 0; y != h; ++y) { size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ size_t inindex = linebytes * y; - out[outindex] = 0; /*filter type byte*/ - filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, 0); + out[outindex] = type; /*filter type byte*/ + filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type); prevline = &in[inindex]; } - } - else if(strategy == LFS_MINSUM) - { + } else if(strategy == LFS_MINSUM) { /*adaptive filtering*/ - size_t sum[5]; unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/ size_t smallest = 0; unsigned char type, bestType = 0; - for(type = 0; type != 5; ++type) - { + for(type = 0; type != 5; ++type) { attempt[type] = (unsigned char*)lodepng_malloc(linebytes); - if(!attempt[type]) return 83; /*alloc fail*/ + if(!attempt[type]) error = 83; /*alloc fail*/ } - if(!error) - { - for(y = 0; y != h; ++y) - { + if(!error) { + for(y = 0; y != h; ++y) { /*try the 5 filter types*/ - for(type = 0; type != 5; ++type) - { + for(type = 0; type != 5; ++type) { + size_t sum = 0; filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); /*calculate the sum of the result*/ - sum[type] = 0; - if(type == 0) - { - for(x = 0; x != linebytes; ++x) sum[type] += (unsigned char)(attempt[type][x]); - } - else - { - for(x = 0; x != linebytes; ++x) - { + if(type == 0) { + for(x = 0; x != linebytes; ++x) sum += (unsigned char)(attempt[type][x]); + } else { + for(x = 0; x != linebytes; ++x) { /*For differences, each byte should be treated as signed, values above 127 are negative (converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there. This means filtertype 0 is almost never chosen, but that is justified.*/ unsigned char s = attempt[type][x]; - sum[type] += s < 128 ? s : (255U - s); + sum += s < 128 ? s : (255U - s); } } /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ - if(type == 0 || sum[type] < smallest) - { + if(type == 0 || sum < smallest) { bestType = type; - smallest = sum[type]; + smallest = sum; } } @@ -5306,57 +5490,47 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, } for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); - } - else if(strategy == LFS_ENTROPY) - { - float sum[5]; + } else if(strategy == LFS_ENTROPY) { unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/ - float smallest = 0; + size_t bestSum = 0; unsigned type, bestType = 0; unsigned count[256]; - for(type = 0; type != 5; ++type) - { + for(type = 0; type != 5; ++type) { attempt[type] = (unsigned char*)lodepng_malloc(linebytes); - if(!attempt[type]) return 83; /*alloc fail*/ - } - - for(y = 0; y != h; ++y) - { - /*try the 5 filter types*/ - for(type = 0; type != 5; ++type) - { - filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); - for(x = 0; x != 256; ++x) count[x] = 0; - for(x = 0; x != linebytes; ++x) ++count[attempt[type][x]]; - ++count[type]; /*the filter type itself is part of the scanline*/ - sum[type] = 0; - for(x = 0; x != 256; ++x) - { - float p = count[x] / (float)(linebytes + 1); - sum[type] += count[x] == 0 ? 0 : flog2(1 / p) * p; - } - /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ - if(type == 0 || sum[type] < smallest) - { - bestType = type; - smallest = sum[type]; + if(!attempt[type]) error = 83; /*alloc fail*/ + } + + if(!error) { + for(y = 0; y != h; ++y) { + /*try the 5 filter types*/ + for(type = 0; type != 5; ++type) { + size_t sum = 0; + filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); + lodepng_memset(count, 0, 256 * sizeof(*count)); + for(x = 0; x != linebytes; ++x) ++count[attempt[type][x]]; + ++count[type]; /*the filter type itself is part of the scanline*/ + for(x = 0; x != 256; ++x) { + sum += ilog2i(count[x]); + } + /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ + if(type == 0 || sum > bestSum) { + bestType = type; + bestSum = sum; + } } - } - prevline = &in[y * linebytes]; + prevline = &in[y * linebytes]; - /*now fill the out values*/ - out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ - for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; + /*now fill the out values*/ + out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ + for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; + } } for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); - } - else if(strategy == LFS_PREDEFINED) - { - for(y = 0; y != h; ++y) - { + } else if(strategy == LFS_PREDEFINED) { + for(y = 0; y != h; ++y) { size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ size_t inindex = linebytes * y; unsigned char type = settings->predefined_filters[y]; @@ -5364,9 +5538,7 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type); prevline = &in[inindex]; } - } - else if(strategy == LFS_BRUTE_FORCE) - { + } else if(strategy == LFS_BRUTE_FORCE) { /*brute force filter chooser. deflate the scanline after every filter attempt to see which one deflates best. This is very slow and gives only slightly smaller, sometimes even larger, result*/ @@ -5375,7 +5547,8 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, size_t smallest = 0; unsigned type = 0, bestType = 0; unsigned char* dummy; - LodePNGCompressSettings zlibsettings = settings->zlibsettings; + LodePNGCompressSettings zlibsettings; + lodepng_memcpy(&zlibsettings, &settings->zlibsettings, sizeof(LodePNGCompressSettings)); /*use fixed tree on the attempts so that the tree is not adapted to the filtertype on purpose, to simulate the true case where the tree is the same for the whole image. Sometimes it gives better result with dynamic tree anyway. Using the fixed tree sometimes gives worse, but in rare @@ -5385,33 +5558,31 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, images only, so disable it*/ zlibsettings.custom_zlib = 0; zlibsettings.custom_deflate = 0; - for(type = 0; type != 5; ++type) - { + for(type = 0; type != 5; ++type) { attempt[type] = (unsigned char*)lodepng_malloc(linebytes); - if(!attempt[type]) return 83; /*alloc fail*/ - } - for(y = 0; y != h; ++y) /*try the 5 filter types*/ - { - for(type = 0; type != 5; ++type) - { - unsigned testsize = linebytes; - /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/ - - filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); - size[type] = 0; - dummy = 0; - zlib_compress(&dummy, &size[type], attempt[type], testsize, &zlibsettings); - lodepng_free(dummy); - /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/ - if(type == 0 || size[type] < smallest) - { - bestType = type; - smallest = size[type]; + if(!attempt[type]) error = 83; /*alloc fail*/ + } + if(!error) { + for(y = 0; y != h; ++y) /*try the 5 filter types*/ { + for(type = 0; type != 5; ++type) { + unsigned testsize = (unsigned)linebytes; + /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/ + + filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); + size[type] = 0; + dummy = 0; + zlib_compress(&dummy, &size[type], attempt[type], testsize, &zlibsettings); + lodepng_free(dummy); + /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/ + if(type == 0 || size[type] < smallest) { + bestType = type; + smallest = size[type]; + } } + prevline = &in[y * linebytes]; + out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ + for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; } - prevline = &in[y * linebytes]; - out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ - for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; } for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); } @@ -5421,18 +5592,15 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, } static void addPaddingBits(unsigned char* out, const unsigned char* in, - size_t olinebits, size_t ilinebits, unsigned h) -{ + size_t olinebits, size_t ilinebits, unsigned h) { /*The opposite of the removePaddingBits function olinebits must be >= ilinebits*/ unsigned y; size_t diff = olinebits - ilinebits; size_t obp = 0, ibp = 0; /*bit pointers*/ - for(y = 0; y != h; ++y) - { + for(y = 0; y != h; ++y) { size_t x; - for(x = 0; x < ilinebits; ++x) - { + for(x = 0; x < ilinebits; ++x) { unsigned char bit = readBitFromReversedStream(&ibp, in); setBitOfReversedStream(&obp, out, bit); } @@ -5453,47 +5621,37 @@ in has the following size in bits: w * h * bpp. out is possibly bigger due to padding bits between reduced images NOTE: comments about padding bits are only relevant if bpp < 8 */ -static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) -{ +static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) { unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; unsigned i; Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); - if(bpp >= 8) - { - for(i = 0; i != 7; ++i) - { + if(bpp >= 8) { + for(i = 0; i != 7; ++i) { unsigned x, y, b; - size_t bytewidth = bpp / 8; + size_t bytewidth = bpp / 8u; for(y = 0; y < passh[i]; ++y) - for(x = 0; x < passw[i]; ++x) - { + for(x = 0; x < passw[i]; ++x) { size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth; - for(b = 0; b < bytewidth; ++b) - { + for(b = 0; b < bytewidth; ++b) { out[pixeloutstart + b] = in[pixelinstart + b]; } } } - } - else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ - { - for(i = 0; i != 7; ++i) - { + } else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ { + for(i = 0; i != 7; ++i) { unsigned x, y, b; unsigned ilinebits = bpp * passw[i]; unsigned olinebits = bpp * w; size_t obp, ibp; /*bit pointers (for out and in buffer)*/ for(y = 0; y < passh[i]; ++y) - for(x = 0; x < passw[i]; ++x) - { + for(x = 0; x < passw[i]; ++x) { ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; obp = (8 * passstart[i]) + (y * ilinebits + x * bpp); - for(b = 0; b < bpp; ++b) - { + for(b = 0; b < bpp; ++b) { unsigned char bit = readBitFromReversedStream(&ibp, in); setBitOfReversedStream(&obp, out, bit); } @@ -5506,45 +5664,36 @@ static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigne return value is error**/ static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, unsigned w, unsigned h, - const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) -{ + const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) { /* This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps: - *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter + *) if no Adam7: 1) add padding bits (= possible extra bits per scanline if bpp < 8) 2) filter *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter */ unsigned bpp = lodepng_get_bpp(&info_png->color); unsigned error = 0; - if(info_png->interlace_method == 0) - { - *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/ + if(info_png->interlace_method == 0) { + *outsize = h + (h * ((w * bpp + 7u) / 8u)); /*image size plus an extra byte per scanline + possible padding bits*/ *out = (unsigned char*)lodepng_malloc(*outsize); if(!(*out) && (*outsize)) error = 83; /*alloc fail*/ - if(!error) - { + if(!error) { /*non multiple of 8 bits per scanline, padding bits needed per scanline*/ - if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) - { - unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7) / 8)); + if(bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) { + unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7u) / 8u)); if(!padded) error = 83; /*alloc fail*/ - if(!error) - { - addPaddingBits(padded, in, ((w * bpp + 7) / 8) * 8, w * bpp, h); + if(!error) { + addPaddingBits(padded, in, ((w * bpp + 7u) / 8u) * 8u, w * bpp, h); error = filter(*out, padded, w, h, &info_png->color, settings); } lodepng_free(padded); - } - else - { + } else { /*we can immediately filter into the out buffer, no other steps needed*/ error = filter(*out, in, w, h, &info_png->color, settings); } } - } - else /*interlace_method is 1 (Adam7)*/ - { + } else /*interlace_method is 1 (Adam7)*/ { unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; unsigned char* adam7; @@ -5558,25 +5707,20 @@ static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const adam7 = (unsigned char*)lodepng_malloc(passstart[7]); if(!adam7 && passstart[7]) error = 83; /*alloc fail*/ - if(!error) - { + if(!error) { unsigned i; Adam7_interlace(adam7, in, w, h, bpp); - for(i = 0; i != 7; ++i) - { - if(bpp < 8) - { + for(i = 0; i != 7; ++i) { + if(bpp < 8) { unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]); if(!padded) ERROR_BREAK(83); /*alloc fail*/ addPaddingBits(padded, &adam7[passstart[i]], - ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]); + ((passw[i] * bpp + 7u) / 8u) * 8u, passw[i] * bpp, passh[i]); error = filter(&(*out)[filter_passstart[i]], padded, passw[i], passh[i], &info_png->color, settings); lodepng_free(padded); - } - else - { + } else { error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], passw[i], passh[i], &info_png->color, settings); } @@ -5591,232 +5735,292 @@ static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const return error; } -/* -palette must have 4 * palettesize bytes allocated, and given in format RGBARGBARGBARGBA... -returns 0 if the palette is opaque, -returns 1 if the palette has a single color with alpha 0 ==> color key -returns 2 if the palette is semi-translucent. -*/ -static unsigned getPaletteTranslucency(const unsigned char* palette, size_t palettesize) -{ - size_t i; - unsigned key = 0; - unsigned r = 0, g = 0, b = 0; /*the value of the color with alpha 0, so long as color keying is possible*/ - for(i = 0; i != palettesize; ++i) - { - if(!key && palette[4 * i + 3] == 0) - { - r = palette[4 * i + 0]; g = palette[4 * i + 1]; b = palette[4 * i + 2]; - key = 1; - i = (size_t)(-1); /*restart from beginning, to detect earlier opaque colors with key's value*/ - } - else if(palette[4 * i + 3] != 255) return 2; - /*when key, no opaque RGB may have key's RGB*/ - else if(key && r == palette[i * 4 + 0] && g == palette[i * 4 + 1] && b == palette[i * 4 + 2]) return 2; - } - return key; -} - #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) -{ +static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) { unsigned char* inchunk = data; - while((size_t)(inchunk - data) < datasize) - { + while((size_t)(inchunk - data) < datasize) { CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk)); out->allocsize = out->size; /*fix the allocsize again*/ - inchunk = lodepng_chunk_next(inchunk); + inchunk = lodepng_chunk_next(inchunk, data + datasize); } return 0; } + +static unsigned isGrayICCProfile(const unsigned char* profile, unsigned size) { + /* + It is a gray profile if bytes 16-19 are "GRAY", rgb profile if bytes 16-19 + are "RGB ". We do not perform any full parsing of the ICC profile here, other + than check those 4 bytes to grayscale profile. Other than that, validity of + the profile is not checked. This is needed only because the PNG specification + requires using a non-gray color model if there is an ICC profile with "RGB " + (sadly limiting compression opportunities if the input data is grayscale RGB + data), and requires using a gray color model if it is "GRAY". + */ + if(size < 20) return 0; + return profile[16] == 'G' && profile[17] == 'R' && profile[18] == 'A' && profile[19] == 'Y'; +} + +static unsigned isRGBICCProfile(const unsigned char* profile, unsigned size) { + /* See comment in isGrayICCProfile*/ + if(size < 20) return 0; + return profile[16] == 'R' && profile[17] == 'G' && profile[18] == 'B' && profile[19] == ' '; +} #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ unsigned lodepng_encode(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h, - LodePNGState* state) -{ - LodePNGInfo info; - ucvector outv; + LodePNGState* state) { unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ size_t datasize = 0; + ucvector outv = ucvector_init(NULL, 0); + LodePNGInfo info; + const LodePNGInfo* info_png = &state->info_png; + + lodepng_info_init(&info); /*provide some proper output values if error will happen*/ *out = 0; *outsize = 0; state->error = 0; - lodepng_info_init(&info); - lodepng_info_copy(&info, &state->info_png); - - if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette) - && (info.color.palettesize == 0 || info.color.palettesize > 256)) - { + /*check input values validity*/ + if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette) + && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) { state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ - return state->error; - } - - if(state->encoder.auto_convert) - { - state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw); + goto cleanup; } - if(state->error) return state->error; - - if(state->encoder.zlibsettings.btype > 2) - { - CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ + if(state->encoder.zlibsettings.btype > 2) { + state->error = 61; /*error: invalid btype*/ + goto cleanup; } - if(state->info_png.interlace_method > 1) - { - CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ + if(info_png->interlace_method > 1) { + state->error = 71; /*error: invalid interlace mode*/ + goto cleanup; } - - state->error = checkColorValidity(info.color.colortype, info.color.bitdepth); - if(state->error) return state->error; /*error: unexisting color type given*/ + state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth); + if(state->error) goto cleanup; /*error: invalid color type given*/ state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); - if(state->error) return state->error; /*error: unexisting color type given*/ + if(state->error) goto cleanup; /*error: invalid color type given*/ - if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) - { + /* color convert and compute scanline filter types */ + lodepng_info_copy(&info, &state->info_png); + if(state->encoder.auto_convert) { + LodePNGColorStats stats; + lodepng_color_stats_init(&stats); +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + if(info_png->iccp_defined && + isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { + /*the PNG specification does not allow to use palette with a GRAY ICC profile, even + if the palette has only gray colors, so disallow it.*/ + stats.allow_palette = 0; + } + if(info_png->iccp_defined && + isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { + /*the PNG specification does not allow to use grayscale color with RGB ICC profile, so disallow gray.*/ + stats.allow_greyscale = 0; + } +#endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */ + state->error = lodepng_compute_color_stats(&stats, image, w, h, &state->info_raw); + if(state->error) goto cleanup; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + if(info_png->background_defined) { + /*the background chunk's color must be taken into account as well*/ + unsigned r = 0, g = 0, b = 0; + LodePNGColorMode mode16 = lodepng_color_mode_make(LCT_RGB, 16); + lodepng_convert_rgb(&r, &g, &b, info_png->background_r, info_png->background_g, info_png->background_b, &mode16, &info_png->color); + state->error = lodepng_color_stats_add(&stats, r, g, b, 65535); + if(state->error) goto cleanup; + } +#endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */ + state->error = auto_choose_color(&info.color, &state->info_raw, &stats); + if(state->error) goto cleanup; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*also convert the background chunk*/ + if(info_png->background_defined) { + if(lodepng_convert_rgb(&info.background_r, &info.background_g, &info.background_b, + info_png->background_r, info_png->background_g, info_png->background_b, &info.color, &info_png->color)) { + state->error = 104; + goto cleanup; + } + } +#endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */ + } +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + if(info_png->iccp_defined) { + unsigned gray_icc = isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); + unsigned rgb_icc = isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); + unsigned gray_png = info.color.colortype == LCT_GREY || info.color.colortype == LCT_GREY_ALPHA; + if(!gray_icc && !rgb_icc) { + state->error = 100; /* Disallowed profile color type for PNG */ + goto cleanup; + } + if(gray_icc != gray_png) { + /*Not allowed to use RGB/RGBA/palette with GRAY ICC profile or vice versa, + or in case of auto_convert, it wasn't possible to find appropriate model*/ + state->error = state->encoder.auto_convert ? 102 : 101; + goto cleanup; + } + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) { unsigned char* converted; - size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8; + size_t size = ((size_t)w * (size_t)h * (size_t)lodepng_get_bpp(&info.color) + 7u) / 8u; converted = (unsigned char*)lodepng_malloc(size); if(!converted && size) state->error = 83; /*alloc fail*/ - if(!state->error) - { + if(!state->error) { state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); } - if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); + if(!state->error) { + state->error = preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); + } lodepng_free(converted); + if(state->error) goto cleanup; + } else { + state->error = preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); + if(state->error) goto cleanup; } - else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); - ucvector_init(&outv); - while(!state->error) /*while only executed once, to break on error*/ - { + /* output all PNG chunks */ { #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS size_t i; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /*write signature and chunks*/ - writeSignature(&outv); + state->error = writeSignature(&outv); + if(state->error) goto cleanup; /*IHDR*/ - addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method); + state->error = addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method); + if(state->error) goto cleanup; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*unknown chunks between IHDR and PLTE*/ - if(info.unknown_chunks_data[0]) - { + if(info.unknown_chunks_data[0]) { state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]); - if(state->error) break; + if(state->error) goto cleanup; } -#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - /*PLTE*/ - if(info.color.colortype == LCT_PALETTE) - { - addChunk_PLTE(&outv, &info.color); + /*color profile chunks must come before PLTE */ + if(info.iccp_defined) { + state->error = addChunk_iCCP(&outv, &info, &state->encoder.zlibsettings); + if(state->error) goto cleanup; } - if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA)) - { - addChunk_PLTE(&outv, &info.color); + if(info.srgb_defined) { + state->error = addChunk_sRGB(&outv, &info); + if(state->error) goto cleanup; } - /*tRNS*/ - if(info.color.colortype == LCT_PALETTE && getPaletteTranslucency(info.color.palette, info.color.palettesize) != 0) - { - addChunk_tRNS(&outv, &info.color); + if(info.gama_defined) { + state->error = addChunk_gAMA(&outv, &info); + if(state->error) goto cleanup; } - if((info.color.colortype == LCT_GREY || info.color.colortype == LCT_RGB) && info.color.key_defined) - { - addChunk_tRNS(&outv, &info.color); + if(info.chrm_defined) { + state->error = addChunk_cHRM(&outv, &info); + if(state->error) goto cleanup; } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + /*PLTE*/ + if(info.color.colortype == LCT_PALETTE) { + state->error = addChunk_PLTE(&outv, &info.color); + if(state->error) goto cleanup; + } + if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA)) { + /*force_palette means: write suggested palette for truecolor in PLTE chunk*/ + state->error = addChunk_PLTE(&outv, &info.color); + if(state->error) goto cleanup; + } + /*tRNS (this will only add if when necessary) */ + state->error = addChunk_tRNS(&outv, &info.color); + if(state->error) goto cleanup; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*bKGD (must come between PLTE and the IDAt chunks*/ - if(info.background_defined) addChunk_bKGD(&outv, &info); + if(info.background_defined) { + state->error = addChunk_bKGD(&outv, &info); + if(state->error) goto cleanup; + } /*pHYs (must come before the IDAT chunks)*/ - if(info.phys_defined) addChunk_pHYs(&outv, &info); + if(info.phys_defined) { + state->error = addChunk_pHYs(&outv, &info); + if(state->error) goto cleanup; + } /*unknown chunks between PLTE and IDAT*/ - if(info.unknown_chunks_data[1]) - { + if(info.unknown_chunks_data[1]) { state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]); - if(state->error) break; + if(state->error) goto cleanup; } #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /*IDAT (multiple IDAT chunks must be consecutive)*/ state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings); - if(state->error) break; + if(state->error) goto cleanup; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*tIME*/ - if(info.time_defined) addChunk_tIME(&outv, &info.time); + if(info.time_defined) { + state->error = addChunk_tIME(&outv, &info.time); + if(state->error) goto cleanup; + } /*tEXt and/or zTXt*/ - for(i = 0; i != info.text_num; ++i) - { - if(strlen(info.text_keys[i]) > 79) - { + for(i = 0; i != info.text_num; ++i) { + if(lodepng_strlen(info.text_keys[i]) > 79) { state->error = 66; /*text chunk too large*/ - break; + goto cleanup; } - if(strlen(info.text_keys[i]) < 1) - { + if(lodepng_strlen(info.text_keys[i]) < 1) { state->error = 67; /*text chunk too small*/ - break; - } - if(state->encoder.text_compression) - { - addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings); + goto cleanup; } - else - { - addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); + if(state->encoder.text_compression) { + state->error = addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings); + if(state->error) goto cleanup; + } else { + state->error = addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); + if(state->error) goto cleanup; } } /*LodePNG version id in text chunk*/ - if(state->encoder.add_id) - { - unsigned alread_added_id_text = 0; - for(i = 0; i != info.text_num; ++i) - { - if(!strcmp(info.text_keys[i], "LodePNG")) - { - alread_added_id_text = 1; + if(state->encoder.add_id) { + unsigned already_added_id_text = 0; + for(i = 0; i != info.text_num; ++i) { + const char* k = info.text_keys[i]; + /* Could use strcmp, but we're not calling or reimplementing this C library function for this use only */ + if(k[0] == 'L' && k[1] == 'o' && k[2] == 'd' && k[3] == 'e' && + k[4] == 'P' && k[5] == 'N' && k[6] == 'G' && k[7] == '\0') { + already_added_id_text = 1; break; } } - if(alread_added_id_text == 0) - { - addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/ + if(already_added_id_text == 0) { + state->error = addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/ + if(state->error) goto cleanup; } } /*iTXt*/ - for(i = 0; i != info.itext_num; ++i) - { - if(strlen(info.itext_keys[i]) > 79) - { + for(i = 0; i != info.itext_num; ++i) { + if(lodepng_strlen(info.itext_keys[i]) > 79) { state->error = 66; /*text chunk too large*/ - break; + goto cleanup; } - if(strlen(info.itext_keys[i]) < 1) - { + if(lodepng_strlen(info.itext_keys[i]) < 1) { state->error = 67; /*text chunk too small*/ - break; + goto cleanup; } - addChunk_iTXt(&outv, state->encoder.text_compression, - info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i], - &state->encoder.zlibsettings); + state->error = addChunk_iTXt( + &outv, state->encoder.text_compression, + info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i], + &state->encoder.zlibsettings); + if(state->error) goto cleanup; } /*unknown chunks between IDAT and IEND*/ - if(info.unknown_chunks_data[2]) - { + if(info.unknown_chunks_data[2]) { state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]); - if(state->error) break; + if(state->error) goto cleanup; } #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ - addChunk_IEND(&outv); - - break; /*this isn't really a while loop; no error happened so break out now!*/ + state->error = addChunk_IEND(&outv); + if(state->error) goto cleanup; } +cleanup: lodepng_info_cleanup(&info); lodepng_free(data); + /*instead of cleaning the vector up, give it to the output*/ *out = outv.data; *outsize = outv.size; @@ -5825,8 +6029,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize, } unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsigned char* image, - unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) -{ + unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) { unsigned error; LodePNGState state; lodepng_state_init(&state); @@ -5840,20 +6043,17 @@ unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsig return error; } -unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) -{ +unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) { return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGBA, 8); } -unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) -{ +unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) { return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGB, 8); } #ifdef LODEPNG_COMPILE_DISK unsigned lodepng_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { unsigned char* buffer; size_t buffersize; unsigned error = lodepng_encode_memory(&buffer, &buffersize, image, w, h, colortype, bitdepth); @@ -5862,19 +6062,16 @@ unsigned lodepng_encode_file(const char* filename, const unsigned char* image, u return error; } -unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) -{ +unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) { return lodepng_encode_file(filename, image, w, h, LCT_RGBA, 8); } -unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) -{ +unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) { return lodepng_encode_file(filename, image, w, h, LCT_RGB, 8); } #endif /*LODEPNG_COMPILE_DISK*/ -void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) -{ +void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) { lodepng_compress_settings_init(&settings->zlibsettings); settings->filter_palette_zero = 1; settings->filter_strategy = LFS_MINSUM; @@ -5895,10 +6092,8 @@ void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) This returns the description of a numerical error code in English. This is also the documentation of all the error codes. */ -const char* lodepng_error_text(unsigned code) -{ - switch(code) - { +const char* lodepng_error_text(unsigned code) { + switch(code) { case 0: return "no error, everything went ok"; case 1: return "nothing done yet"; /*the Encoder/Decoder has done nothing yet, error checking makes no sense yet*/ case 10: return "end of input memory reached without huffman end code"; /*while huffman decoding*/ @@ -5906,16 +6101,18 @@ const char* lodepng_error_text(unsigned code) case 13: return "problem while processing dynamic deflate block"; case 14: return "problem while processing dynamic deflate block"; case 15: return "problem while processing dynamic deflate block"; - case 16: return "unexisting code while processing dynamic deflate block"; + /*this error could happen if there are only 0 or 1 symbols present in the huffman code:*/ + case 16: return "invalid code while processing dynamic deflate block"; case 17: return "end of out buffer memory reached while inflating"; case 18: return "invalid distance code while inflating"; case 19: return "end of out buffer memory reached while inflating"; case 20: return "invalid deflate block BTYPE encountered while decoding"; case 21: return "NLEN is not ones complement of LEN in a deflate block"; - /*end of out buffer memory reached while inflating: - This can happen if the inflated deflate data is longer than the amount of bytes required to fill up - all the pixels of the image, given the color depth and image dimensions. Something that doesn't - happen in a normal, well encoded, PNG image.*/ + + /*end of out buffer memory reached while inflating: + This can happen if the inflated deflate data is longer than the amount of bytes required to fill up + all the pixels of the image, given the color depth and image dimensions. Something that doesn't + happen in a normal, well encoded, PNG image.*/ case 22: return "end of out buffer memory reached while inflating"; case 23: return "end of in buffer memory reached while inflating"; case 24: return "invalid FCHECK in zlib header"; @@ -5933,13 +6130,13 @@ const char* lodepng_error_text(unsigned code) case 35: return "chunk length of a chunk is too large or the chunk too small"; case 36: return "illegal PNG filter type encountered"; case 37: return "illegal bit depth for this color type given"; - case 38: return "the palette is too big"; /*more than 256 colors*/ - case 39: return "more palette alpha values given in tRNS chunk than there are colors in the palette"; - case 40: return "tRNS chunk has wrong size for greyscale image"; + case 38: return "the palette is too small or too big"; /*0, or more than 256 colors*/ + case 39: return "tRNS chunk before PLTE or has more entries than palette size"; + case 40: return "tRNS chunk has wrong size for grayscale image"; case 41: return "tRNS chunk has wrong size for RGB image"; case 42: return "tRNS chunk appeared while it was not allowed for this color type"; case 43: return "bKGD chunk has wrong size for palette image"; - case 44: return "bKGD chunk has wrong size for greyscale image"; + case 44: return "bKGD chunk has wrong size for grayscale image"; case 45: return "bKGD chunk has wrong size for RGB image"; case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?"; case 49: return "jumped past memory while generating dynamic huffman tree"; @@ -5950,7 +6147,7 @@ const char* lodepng_error_text(unsigned code) case 54: return "repeat symbol in tree while there was no value symbol yet"; /*jumped past tree while generating huffman tree, this could be when the tree will have more leaves than symbols after generating it out of the - given lenghts. They call this an oversubscribed dynamic bit lengths tree in zlib.*/ + given lengths. They call this an oversubscribed dynamic bit lengths tree in zlib.*/ case 55: return "jumped past tree while generating huffman tree"; case 56: return "given output image colortype or bitdepth not supported for color conversion"; case 57: return "invalid CRC encountered (checking CRC can be disabled)"; @@ -5958,17 +6155,18 @@ const char* lodepng_error_text(unsigned code) case 59: return "requested color conversion not supported"; case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)"; case 61: return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)"; - /*LodePNG leaves the choice of RGB to greyscale conversion formula to the user.*/ - case 62: return "conversion from color to greyscale not supported"; - case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk"; /*(2^31-1)*/ + /*LodePNG leaves the choice of RGB to grayscale conversion formula to the user.*/ + case 62: return "conversion from color to grayscale not supported"; + /*(2^31-1)*/ + case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk"; /*this would result in the inability of a deflated block to ever contain an end code. It must be at least 1.*/ case 64: return "the length of the END symbol 256 in the Huffman tree is 0"; case 66: return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes"; case 67: return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte"; case 68: return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors"; case 69: return "unknown chunk type with 'critical' flag encountered by the decoder"; - case 71: return "unexisting interlace mode given to encoder (must be 0 or 1)"; - case 72: return "while decoding, unexisting compression method encountering in zTXt or iTXt chunk (it must be 0)"; + case 71: return "invalid interlace mode given to encoder (must be 0 or 1)"; + case 72: return "while decoding, invalid compression method encountering in zTXt or iTXt chunk (it must be 0)"; case 73: return "invalid tIME chunk size"; case 74: return "invalid pHYs chunk size"; /*length could be wrong, or data chopped off*/ @@ -5979,7 +6177,7 @@ const char* lodepng_error_text(unsigned code) case 79: return "failed to open file for writing"; case 80: return "tried creating a tree of 0 symbols"; case 81: return "lazy matching at pos 0 is impossible"; - case 82: return "color conversion to palette requested while a color isn't in palette"; + case 82: return "color conversion to palette requested while a color isn't in palette, or index out of bounds"; case 83: return "memory allocation failed"; case 84: return "given image too small to contain all pixels to be encoded"; case 86: return "impossible offset in lz77 encoding (internal bug)"; @@ -5989,9 +6187,23 @@ const char* lodepng_error_text(unsigned code) /*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/ case 90: return "windowsize must be a power of two"; case 91: return "invalid decompressed idat size"; - case 92: return "too many pixels, not supported"; + case 92: return "integer overflow due to too many pixels"; case 93: return "zero width or height is invalid"; case 94: return "header chunk must have a size of 13 bytes"; + case 95: return "integer overflow with combined idat chunk size"; + case 96: return "invalid gAMA chunk size"; + case 97: return "invalid cHRM chunk size"; + case 98: return "invalid sRGB chunk size"; + case 99: return "invalid sRGB rendering intent"; + case 100: return "invalid ICC profile color type, the PNG specification only allows RGB or GRAY"; + case 101: return "PNG specification does not allow RGB ICC profile on gray color types and vice versa"; + case 102: return "not allowed to set grayscale ICC profile with colored pixels by PNG specification"; + case 103: return "invalid palette index in bKGD chunk. Maybe it came before PLTE chunk?"; + case 104: return "invalid bKGD color while encoding (e.g. palette index out of range)"; + case 105: return "integer overflow of bitsize"; + case 106: return "PNG file must have PLTE chunk if color type is palette"; + case 107: return "color convert from palette mode requested without setting the palette data in it"; + case 108: return "tried to add more than 256 values to a palette"; } return "unknown error code"; } @@ -6004,12 +6216,10 @@ const char* lodepng_error_text(unsigned code) /* ////////////////////////////////////////////////////////////////////////// */ #ifdef LODEPNG_COMPILE_CPP -namespace lodepng -{ +namespace lodepng { #ifdef LODEPNG_COMPILE_DISK -unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename) -{ +unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename) { long size = lodepng_filesize(filename.c_str()); if(size < 0) return 78; buffer.resize((size_t)size); @@ -6017,8 +6227,7 @@ unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filena } /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ -unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename) -{ +unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename) { return lodepng_save_file(buffer.empty() ? 0 : &buffer[0], buffer.size(), filename.c_str()); } #endif /* LODEPNG_COMPILE_DISK */ @@ -6026,13 +6235,11 @@ unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& #ifdef LODEPNG_COMPILE_ZLIB #ifdef LODEPNG_COMPILE_DECODER unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, - const LodePNGDecompressSettings& settings) -{ + const LodePNGDecompressSettings& settings) { unsigned char* buffer = 0; size_t buffersize = 0; - unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings); - if(buffer) - { + unsigned error = zlib_decompress(&buffer, &buffersize, 0, in, insize, &settings); + if(buffer) { out.insert(out.end(), &buffer[0], &buffer[buffersize]); lodepng_free(buffer); } @@ -6040,21 +6247,18 @@ unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, si } unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, - const LodePNGDecompressSettings& settings) -{ + const LodePNGDecompressSettings& settings) { return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings); } #endif /* LODEPNG_COMPILE_DECODER */ #ifdef LODEPNG_COMPILE_ENCODER unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, - const LodePNGCompressSettings& settings) -{ + const LodePNGCompressSettings& settings) { unsigned char* buffer = 0; size_t buffersize = 0; unsigned error = zlib_compress(&buffer, &buffersize, in, insize, &settings); - if(buffer) - { + if(buffer) { out.insert(out.end(), &buffer[0], &buffer[buffersize]); lodepng_free(buffer); } @@ -6062,8 +6266,7 @@ unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size } unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, - const LodePNGCompressSettings& settings) -{ + const LodePNGCompressSettings& settings) { return compress(out, in.empty() ? 0 : &in[0], in.size(), settings); } #endif /* LODEPNG_COMPILE_ENCODER */ @@ -6072,24 +6275,20 @@ unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned ch #ifdef LODEPNG_COMPILE_PNG -State::State() -{ +State::State() { lodepng_state_init(this); } -State::State(const State& other) -{ +State::State(const State& other) { lodepng_state_init(this); lodepng_state_copy(this, &other); } -State::~State() -{ +State::~State() { lodepng_state_cleanup(this); } -State& State::operator=(const State& other) -{ +State& State::operator=(const State& other) { lodepng_state_copy(this, &other); return *this; } @@ -6097,36 +6296,31 @@ State& State::operator=(const State& other) #ifdef LODEPNG_COMPILE_DECODER unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const unsigned char* in, - size_t insize, LodePNGColorType colortype, unsigned bitdepth) -{ - unsigned char* buffer; + size_t insize, LodePNGColorType colortype, unsigned bitdepth) { + unsigned char* buffer = 0; unsigned error = lodepng_decode_memory(&buffer, &w, &h, in, insize, colortype, bitdepth); - if(buffer && !error) - { + if(buffer && !error) { State state; state.info_raw.colortype = colortype; state.info_raw.bitdepth = bitdepth; size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); out.insert(out.end(), &buffer[0], &buffer[buffersize]); - lodepng_free(buffer); } + lodepng_free(buffer); return error; } unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, - const std::vector<unsigned char>& in, LodePNGColorType colortype, unsigned bitdepth) -{ + const std::vector<unsigned char>& in, LodePNGColorType colortype, unsigned bitdepth) { return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colortype, bitdepth); } unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, State& state, - const unsigned char* in, size_t insize) -{ + const unsigned char* in, size_t insize) { unsigned char* buffer = NULL; unsigned error = lodepng_decode(&buffer, &w, &h, &state, in, insize); - if(buffer && !error) - { + if(buffer && !error) { size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); out.insert(out.end(), &buffer[0], &buffer[buffersize]); } @@ -6136,16 +6330,16 @@ unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, State& state, - const std::vector<unsigned char>& in) -{ + const std::vector<unsigned char>& in) { return decode(out, w, h, state, in.empty() ? 0 : &in[0], in.size()); } #ifdef LODEPNG_COMPILE_DISK unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { std::vector<unsigned char> buffer; + /* safe output values in case error happens */ + w = h = 0; unsigned error = load_file(buffer, filename); if(error) return error; return decode(out, w, h, buffer, colortype, bitdepth); @@ -6155,13 +6349,11 @@ unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const #ifdef LODEPNG_COMPILE_ENCODER unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { unsigned char* buffer; size_t buffersize; unsigned error = lodepng_encode_memory(&buffer, &buffersize, in, w, h, colortype, bitdepth); - if(buffer) - { + if(buffer) { out.insert(out.end(), &buffer[0], &buffer[buffersize]); lodepng_free(buffer); } @@ -6170,21 +6362,18 @@ unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsign unsigned encode(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; return encode(out, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); } unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h, - State& state) -{ + State& state) { unsigned char* buffer; size_t buffersize; unsigned error = lodepng_encode(&buffer, &buffersize, in, w, h, &state); - if(buffer) - { + if(buffer) { out.insert(out.end(), &buffer[0], &buffer[buffersize]); lodepng_free(buffer); } @@ -6193,8 +6382,7 @@ unsigned encode(std::vector<unsigned char>& out, unsigned encode(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, unsigned w, unsigned h, - State& state) -{ + State& state) { if(lodepng_get_raw_size(w, h, &state.info_raw) > in.size()) return 84; return encode(out, in.empty() ? 0 : &in[0], w, h, state); } @@ -6202,8 +6390,7 @@ unsigned encode(std::vector<unsigned char>& out, #ifdef LODEPNG_COMPILE_DISK unsigned encode(const std::string& filename, const unsigned char* in, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { std::vector<unsigned char> buffer; unsigned error = encode(buffer, in, w, h, colortype, bitdepth); if(!error) error = save_file(buffer, filename); @@ -6212,8 +6399,7 @@ unsigned encode(const std::string& filename, unsigned encode(const std::string& filename, const std::vector<unsigned char>& in, unsigned w, unsigned h, - LodePNGColorType colortype, unsigned bitdepth) -{ + LodePNGColorType colortype, unsigned bitdepth) { if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; return encode(filename, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); } diff --git a/3rdparty/bimg/3rdparty/lodepng/lodepng.h b/3rdparty/bimg/3rdparty/lodepng/lodepng.h index 94e81955bf2..a386459f923 100644 --- a/3rdparty/bimg/3rdparty/lodepng/lodepng.h +++ b/3rdparty/bimg/3rdparty/lodepng/lodepng.h @@ -1,7 +1,7 @@ /* -LodePNG version 20160501 +LodePNG version 20200306 -Copyright (c) 2005-2016 Lode Vandevenne +Copyright (c) 2005-2020 Lode Vandevenne This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -44,36 +44,44 @@ the custom_zlib field of the compress and decompress settings*/ #ifndef LODEPNG_NO_COMPILE_ZLIB #define LODEPNG_COMPILE_ZLIB #endif + /*png encoder and png decoder*/ #ifndef LODEPNG_NO_COMPILE_PNG #define LODEPNG_COMPILE_PNG #endif + /*deflate&zlib decoder and png decoder*/ #ifndef LODEPNG_NO_COMPILE_DECODER #define LODEPNG_COMPILE_DECODER #endif + /*deflate&zlib encoder and png encoder*/ #ifndef LODEPNG_NO_COMPILE_ENCODER #define LODEPNG_COMPILE_ENCODER #endif + /*the optional built in harddisk file loading and saving functions*/ #ifndef LODEPNG_NO_COMPILE_DISK #define LODEPNG_COMPILE_DISK #endif + /*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/ #ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS #define LODEPNG_COMPILE_ANCILLARY_CHUNKS #endif + /*ability to convert error numerical codes to English text string*/ #ifndef LODEPNG_NO_COMPILE_ERROR_TEXT #define LODEPNG_COMPILE_ERROR_TEXT #endif + /*Compile the default allocators (C's free, malloc and realloc). If you disable this, you can define the functions lodepng_free, lodepng_malloc and lodepng_realloc in your source files with custom allocators.*/ #ifndef LODEPNG_NO_COMPILE_ALLOCATORS #define LODEPNG_COMPILE_ALLOCATORS #endif + /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ #ifdef __cplusplus #ifndef LODEPNG_NO_COMPILE_CPP @@ -87,14 +95,19 @@ source files with custom allocators.*/ #endif /*LODEPNG_COMPILE_CPP*/ #ifdef LODEPNG_COMPILE_PNG -/*The PNG color types (also used for raw).*/ -typedef enum LodePNGColorType -{ - LCT_GREY = 0, /*greyscale: 1,2,4,8,16 bit*/ +/*The PNG color types (also used for raw image).*/ +typedef enum LodePNGColorType { + LCT_GREY = 0, /*grayscale: 1,2,4,8,16 bit*/ LCT_RGB = 2, /*RGB: 8,16 bit*/ LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/ - LCT_GREY_ALPHA = 4, /*greyscale with alpha: 8,16 bit*/ - LCT_RGBA = 6 /*RGB with alpha: 8,16 bit*/ + LCT_GREY_ALPHA = 4, /*grayscale with alpha: 8,16 bit*/ + LCT_RGBA = 6, /*RGB with alpha: 8,16 bit*/ + /*LCT_MAX_OCTET_VALUE lets the compiler allow this enum to represent any invalid + byte value from 0 to 255 that could be present in an invalid PNG file header. Do + not use, compare with or set the name LCT_MAX_OCTET_VALUE, instead either use + the valid color type names above, or numeric values like 1 or 7 when checking for + particular disallowed color type byte values, or cast to integer to print it.*/ + LCT_MAX_OCTET_VALUE = 255 } LodePNGColorType; #ifdef LODEPNG_COMPILE_DECODER @@ -196,8 +209,7 @@ unsigned lodepng_encode24_file(const char* filename, #ifdef LODEPNG_COMPILE_CPP -namespace lodepng -{ +namespace lodepng { #ifdef LODEPNG_COMPILE_DECODER /*Same as lodepng_decode_memory, but decodes to an std::vector. The colortype is the format to output the pixels to. Default is RGBA 8-bit per channel.*/ @@ -253,17 +265,17 @@ const char* lodepng_error_text(unsigned code); #ifdef LODEPNG_COMPILE_DECODER /*Settings for zlib decompression*/ typedef struct LodePNGDecompressSettings LodePNGDecompressSettings; -struct LodePNGDecompressSettings -{ +struct LodePNGDecompressSettings { + /* Check LodePNGDecoderSettings for more ignorable errors such as ignore_crc */ unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/ + unsigned ignore_nlen; /*ignore complement of len checksum in uncompressed blocks*/ /*use custom zlib decoder instead of built in one (default: null)*/ unsigned (*custom_zlib)(unsigned char**, size_t*, const unsigned char*, size_t, const LodePNGDecompressSettings*); /*use custom deflate decoder instead of built in one (default: null) - if custom_zlib is used, custom_deflate is ignored since only the built in - zlib function will call custom_deflate*/ + if custom_zlib is not null, custom_inflate is ignored (the zlib format uses deflate)*/ unsigned (*custom_inflate)(unsigned char**, size_t*, const unsigned char*, size_t, const LodePNGDecompressSettings*); @@ -281,13 +293,12 @@ Settings for zlib compression. Tweaking these settings tweaks the balance between speed and compression ratio. */ typedef struct LodePNGCompressSettings LodePNGCompressSettings; -struct LodePNGCompressSettings /*deflate = compress*/ -{ +struct LodePNGCompressSettings /*deflate = compress*/ { /*LZ77 related settings*/ unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/ unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/ unsigned windowsize; /*must be a power of two <= 32768. higher compresses more but is slower. Default value: 2048.*/ - unsigned minmatch; /*mininum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ + unsigned minmatch; /*minimum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/ unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/ @@ -315,8 +326,7 @@ Color mode of an image. Contains all information required to decode the pixel bits to RGBA colors. This information is the same as used in the PNG file format, and is used both for PNG and raw image data in LodePNG. */ -typedef struct LodePNGColorMode -{ +typedef struct LodePNGColorMode { /*header (IHDR)*/ LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/ unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/ @@ -325,23 +335,25 @@ typedef struct LodePNGColorMode palette (PLTE and tRNS) Dynamically allocated with the colors of the palette, including alpha. - When encoding a PNG, to store your colors in the palette of the LodePNGColorMode, first use - lodepng_palette_clear, then for each color use lodepng_palette_add. - If you encode an image without alpha with palette, don't forget to put value 255 in each A byte of the palette. + This field may not be allocated directly, use lodepng_color_mode_init first, + then lodepng_palette_add per color to correctly initialize it (to ensure size + of exactly 1024 bytes). + + The alpha channels must be set as well, set them to 255 for opaque images. When decoding, by default you can ignore this palette, since LodePNG already fills the palette colors in the pixels of the raw RGBA output. The palette is only supported for color type 3. */ - unsigned char* palette; /*palette in RGBARGBA... order. When allocated, must be either 0, or have size 1024*/ - size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/ + unsigned char* palette; /*palette in RGBARGBA... order. Must be either 0, or when allocated must have 1024 bytes*/ + size_t palettesize; /*palette size in number of colors (amount of used bytes is 4 * palettesize)*/ /* transparent color key (tRNS) This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit. - For greyscale PNGs, r, g and b will all 3 be set to the same. + For grayscale PNGs, r, g and b will all 3 be set to the same. When decoding, by default you can ignore this information, since LodePNG sets pixels with this key to transparent already in the raw RGBA output. @@ -349,7 +361,7 @@ typedef struct LodePNGColorMode The color key is only supported for color types 0 and 2. */ unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/ - unsigned key_r; /*red/greyscale component of color key*/ + unsigned key_r; /*red/grayscale component of color key*/ unsigned key_g; /*green component of color key*/ unsigned key_b; /*blue component of color key*/ } LodePNGColorMode; @@ -359,6 +371,8 @@ void lodepng_color_mode_init(LodePNGColorMode* info); void lodepng_color_mode_cleanup(LodePNGColorMode* info); /*return value is error code (0 means no error)*/ unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source); +/* Makes a temporary LodePNGColorMode that does not need cleanup (no palette) */ +LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth); void lodepng_palette_clear(LodePNGColorMode* info); /*add 1 color to the palette*/ @@ -370,7 +384,7 @@ unsigned lodepng_get_bpp(const LodePNGColorMode* info); /*get the amount of color channels used, based on colortype in the struct. If a palette is used, it counts as 1 channel.*/ unsigned lodepng_get_channels(const LodePNGColorMode* info); -/*is it a greyscale type? (only colortype 0 or 4)*/ +/*is it a grayscale type? (only colortype 0 or 4)*/ unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info); /*has it got an alpha channel? (only colortype 2 or 6)*/ unsigned lodepng_is_alpha_type(const LodePNGColorMode* info); @@ -392,8 +406,7 @@ size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* colo #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*The information of a Time chunk in PNG.*/ -typedef struct LodePNGTime -{ +typedef struct LodePNGTime { unsigned year; /*2 bytes used (0-65535)*/ unsigned month; /*1-12*/ unsigned day; /*1-31*/ @@ -404,28 +417,39 @@ typedef struct LodePNGTime #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /*Information about the PNG image, except pixels, width and height.*/ -typedef struct LodePNGInfo -{ +typedef struct LodePNGInfo { /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/ unsigned compression_method;/*compression method of the original file. Always 0.*/ unsigned filter_method; /*filter method of the original file*/ - unsigned interlace_method; /*interlace method of the original file*/ + unsigned interlace_method; /*interlace method of the original file: 0=none, 1=Adam7*/ LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/ #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /* - suggested background color chunk (bKGD) - This color uses the same color mode as the PNG (except alpha channel), which can be 1-bit to 16-bit. + Suggested background color chunk (bKGD) + + This uses the same color mode and bit depth as the PNG (except no alpha channel), + with values truncated to the bit depth in the unsigned integer. + + For grayscale and palette PNGs, the value is stored in background_r. The values + in background_g and background_b are then unused. - For greyscale PNGs, r, g and b will all 3 be set to the same. When encoding - the encoder writes the red one. For palette PNGs: When decoding, the RGB value - will be stored, not a palette index. But when encoding, specify the index of - the palette in background_r, the other two are then ignored. + So when decoding, you may get these in a different color mode than the one you requested + for the raw pixels. - The decoder does not use this background color to edit the color of pixels. + When encoding with auto_convert, you must use the color model defined in info_png.color for + these values. The encoder normally ignores info_png.color when auto_convert is on, but will + use it to interpret these values (and convert copies of them to its chosen color model). + + When encoding, avoid setting this to an expensive color, such as a non-gray value + when the image is gray, or the compression will be worse since it will be forced to + write the PNG with a more expensive color mode (when auto_convert is on). + + The decoder does not use this background color to edit the color of pixels. This is a + completely optional metadata feature. */ unsigned background_defined; /*is a suggested background color given?*/ - unsigned background_r; /*red component of suggested background color*/ + unsigned background_r; /*red/gray/palette component of suggested background color*/ unsigned background_g; /*green component of suggested background color*/ unsigned background_b; /*blue component of suggested background color*/ @@ -436,6 +460,10 @@ typedef struct LodePNGInfo text_strings, while text_keys are keywords that give a short description what the actual text represents, e.g. Title, Author, Description, or anything else. + All the string fields below including keys, names and language tags are null terminated. + The PNG specification uses null characters for the keys, names and tags, and forbids null + characters to appear in the main text which is why we can use null termination everywhere here. + A keyword is minimum 1 character and maximum 79 characters long. It's discouraged to use a single line length longer than 79 characters for texts. @@ -468,11 +496,86 @@ typedef struct LodePNGInfo unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ /* - unknown chunks - There are 3 buffers, one for each position in the PNG where unknown chunks can appear - each buffer contains all unknown chunks for that position consecutively - The 3 buffers are the unknown chunks between certain critical chunks: - 0: IHDR-PLTE, 1: PLTE-IDAT, 2: IDAT-IEND + Color profile related chunks: gAMA, cHRM, sRGB, iCPP + + LodePNG does not apply any color conversions on pixels in the encoder or decoder and does not interpret these color + profile values. It merely passes on the information. If you wish to use color profiles and convert colors, please + use these values with a color management library. + + See the PNG, ICC and sRGB specifications for more information about the meaning of these values. + */ + + /* gAMA chunk: optional, overridden by sRGB or iCCP if those are present. */ + unsigned gama_defined; /* Whether a gAMA chunk is present (0 = not present, 1 = present). */ + unsigned gama_gamma; /* Gamma exponent times 100000 */ + + /* cHRM chunk: optional, overridden by sRGB or iCCP if those are present. */ + unsigned chrm_defined; /* Whether a cHRM chunk is present (0 = not present, 1 = present). */ + unsigned chrm_white_x; /* White Point x times 100000 */ + unsigned chrm_white_y; /* White Point y times 100000 */ + unsigned chrm_red_x; /* Red x times 100000 */ + unsigned chrm_red_y; /* Red y times 100000 */ + unsigned chrm_green_x; /* Green x times 100000 */ + unsigned chrm_green_y; /* Green y times 100000 */ + unsigned chrm_blue_x; /* Blue x times 100000 */ + unsigned chrm_blue_y; /* Blue y times 100000 */ + + /* + sRGB chunk: optional. May not appear at the same time as iCCP. + If gAMA is also present gAMA must contain value 45455. + If cHRM is also present cHRM must contain respectively 31270,32900,64000,33000,30000,60000,15000,6000. + */ + unsigned srgb_defined; /* Whether an sRGB chunk is present (0 = not present, 1 = present). */ + unsigned srgb_intent; /* Rendering intent: 0=perceptual, 1=rel. colorimetric, 2=saturation, 3=abs. colorimetric */ + + /* + iCCP chunk: optional. May not appear at the same time as sRGB. + + LodePNG does not parse or use the ICC profile (except its color space header field for an edge case), a + separate library to handle the ICC data (not included in LodePNG) format is needed to use it for color + management and conversions. + + For encoding, if iCCP is present, gAMA and cHRM are recommended to be added as well with values that match the ICC + profile as closely as possible, if you wish to do this you should provide the correct values for gAMA and cHRM and + enable their '_defined' flags since LodePNG will not automatically compute them from the ICC profile. + + For encoding, the ICC profile is required by the PNG specification to be an "RGB" profile for non-gray + PNG color types and a "GRAY" profile for gray PNG color types. If you disable auto_convert, you must ensure + the ICC profile type matches your requested color type, else the encoder gives an error. If auto_convert is + enabled (the default), and the ICC profile is not a good match for the pixel data, this will result in an encoder + error if the pixel data has non-gray pixels for a GRAY profile, or a silent less-optimal compression of the pixel + data if the pixels could be encoded as grayscale but the ICC profile is RGB. + + To avoid this do not set an ICC profile in the image unless there is a good reason for it, and when doing so + make sure you compute it carefully to avoid the above problems. + */ + unsigned iccp_defined; /* Whether an iCCP chunk is present (0 = not present, 1 = present). */ + char* iccp_name; /* Null terminated string with profile name, 1-79 bytes */ + /* + The ICC profile in iccp_profile_size bytes. + Don't allocate this buffer yourself. Use the init/cleanup functions + correctly and use lodepng_set_icc and lodepng_clear_icc. + */ + unsigned char* iccp_profile; + unsigned iccp_profile_size; /* The size of iccp_profile in bytes */ + + /* End of color profile related chunks */ + + + /* + unknown chunks: chunks not known by LodePNG, passed on byte for byte. + + There are 3 buffers, one for each position in the PNG where unknown chunks can appear. + Each buffer contains all unknown chunks for that position consecutively. + The 3 positions are: + 0: between IHDR and PLTE, 1: between PLTE and IDAT, 2: between IDAT and IEND. + + For encoding, do not store critical chunks or known chunks that are enabled with a "_defined" flag + above in here, since the encoder will blindly follow this and could then encode an invalid PNG file + (such as one with two IHDR chunks or the disallowed combination of sRGB with iCCP). But do use + this if you wish to store an ancillary chunk that is not supported by LodePNG (such as sPLT or hIST), + or any non-standard PNG chunk. + Do not allocate or traverse this data yourself. Use the chunk traversing functions declared later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct. */ @@ -488,12 +591,16 @@ void lodepng_info_cleanup(LodePNGInfo* info); unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source); #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str); /*push back both texts at once*/ +void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ -void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/ unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/ +void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/ + +/*replaces if exists*/ +unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size); +void lodepng_clear_icc(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /* @@ -516,11 +623,17 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in, Settings for the decoder. This contains settings for the PNG and the Zlib decoder, but not the Info settings from the Info structs. */ -typedef struct LodePNGDecoderSettings -{ +typedef struct LodePNGDecoderSettings { LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/ + /* Check LodePNGDecompressSettings for more ignorable errors such as ignore_adler32 */ unsigned ignore_crc; /*ignore CRC checksums*/ + unsigned ignore_critical; /*ignore unknown critical chunks*/ + unsigned ignore_end; /*ignore issues at end of file if possible (missing IEND chunk, too large chunk, ...)*/ + /* TODO: make a system involving warnings with levels and a strict mode instead. Other potentially recoverable + errors: srgb rendering intent value, size of content of ancillary chunks, more than 79 characters for some + strings, placement/combination rules for ancillary chunks, crc of unknown chunks, allowed characters + in string keys, etc... */ unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ @@ -536,10 +649,14 @@ void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings); #ifdef LODEPNG_COMPILE_ENCODER /*automatically use color type with less bits per pixel if losslessly possible. Default: AUTO*/ -typedef enum LodePNGFilterStrategy -{ +typedef enum LodePNGFilterStrategy { /*every filter at zero*/ - LFS_ZERO, + LFS_ZERO = 0, + /*every filter at 1, 2, 3 or 4 (paeth), unlike LFS_ZERO not a good choice, but for testing*/ + LFS_ONE = 1, + LFS_TWO = 2, + LFS_THREE = 3, + LFS_FOUR = 4, /*Use filter that gives minimum sum, as described in the official PNG filter heuristic.*/ LFS_MINSUM, /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending @@ -554,36 +671,36 @@ typedef enum LodePNGFilterStrategy LFS_PREDEFINED } LodePNGFilterStrategy; -/*Gives characteristics about the colors of the image, which helps decide which color model to use for encoding. +/*Gives characteristics about the integer RGBA colors of the image (count, alpha channel usage, bit depth, ...), +which helps decide which color model to use for encoding. Used internally by default if "auto_convert" is enabled. Public because it's useful for custom algorithms.*/ -typedef struct LodePNGColorProfile -{ - unsigned colored; /*not greyscale*/ - unsigned key; /*if true, image is not opaque. Only if true and alpha is false, color key is possible.*/ - unsigned short key_r; /*these values are always in 16-bit bitdepth in the profile*/ +typedef struct LodePNGColorStats { + unsigned colored; /*not grayscale*/ + unsigned key; /*image is not opaque and color key is possible instead of full alpha*/ + unsigned short key_r; /*key values, always as 16-bit, in 8-bit case the byte is duplicated, e.g. 65535 means 255*/ unsigned short key_g; unsigned short key_b; - unsigned alpha; /*alpha channel or alpha palette required*/ - unsigned numcolors; /*amount of colors, up to 257. Not valid if bits == 16.*/ - unsigned char palette[1024]; /*Remembers up to the first 256 RGBA colors, in no particular order*/ - unsigned bits; /*bits per channel (not for palette). 1,2 or 4 for greyscale only. 16 if 16-bit per channel required.*/ -} LodePNGColorProfile; - -void lodepng_color_profile_init(LodePNGColorProfile* profile); - -/*Get a LodePNGColorProfile of the image.*/ -unsigned lodepng_get_color_profile(LodePNGColorProfile* profile, - const unsigned char* image, unsigned w, unsigned h, - const LodePNGColorMode* mode_in); -/*The function LodePNG uses internally to decide the PNG color with auto_convert. -Chooses an optimal color model, e.g. grey if only grey pixels, palette if < 256 colors, ...*/ -unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, - const unsigned char* image, unsigned w, unsigned h, - const LodePNGColorMode* mode_in); + unsigned alpha; /*image is not opaque and alpha channel or alpha palette required*/ + unsigned numcolors; /*amount of colors, up to 257. Not valid if bits == 16 or allow_palette is disabled.*/ + unsigned char palette[1024]; /*Remembers up to the first 256 RGBA colors, in no particular order, only valid when numcolors is valid*/ + unsigned bits; /*bits per channel (not for palette). 1,2 or 4 for grayscale only. 16 if 16-bit per channel required.*/ + size_t numpixels; + + /*user settings for computing/using the stats*/ + unsigned allow_palette; /*default 1. if 0, disallow choosing palette colortype in auto_choose_color, and don't count numcolors*/ + unsigned allow_greyscale; /*default 1. if 0, choose RGB or RGBA even if the image only has gray colors*/ +} LodePNGColorStats; + +void lodepng_color_stats_init(LodePNGColorStats* stats); + +/*Get a LodePNGColorStats of the image. The stats must already have been inited. +Returns error code (e.g. alloc fail) or 0 if ok.*/ +unsigned lodepng_compute_color_stats(LodePNGColorStats* stats, + const unsigned char* image, unsigned w, unsigned h, + const LodePNGColorMode* mode_in); /*Settings for the encoder.*/ -typedef struct LodePNGEncoderSettings -{ +typedef struct LodePNGEncoderSettings { LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/ unsigned auto_convert; /*automatically choose output PNG color type. Default: true*/ @@ -619,8 +736,7 @@ void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings); #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) /*The settings, state and information for extended encoding and decoding.*/ -typedef struct LodePNGState -{ +typedef struct LodePNGState { #ifdef LODEPNG_COMPILE_DECODER LodePNGDecoderSettings decoder; /*the decoding settings*/ #endif /*LODEPNG_COMPILE_DECODER*/ @@ -630,10 +746,6 @@ typedef struct LodePNGState LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/ LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/ unsigned error; -#ifdef LODEPNG_COMPILE_CPP - /* For the lodepng::State subclass. */ - virtual ~LodePNGState(){} -#endif } LodePNGState; /*init, cleanup and copy functions to use with this struct*/ @@ -653,7 +765,7 @@ unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, /* Read the PNG header, but not the actual data. This returns only the information -that is in the header chunk of the PNG, such as width, height and color type. The +that is in the IHDR chunk of the PNG, such as width, height and color type. The information is placed in the info_png field of the LodePNGState. */ unsigned lodepng_inspect(unsigned* w, unsigned* h, @@ -661,6 +773,20 @@ unsigned lodepng_inspect(unsigned* w, unsigned* h, const unsigned char* in, size_t insize); #endif /*LODEPNG_COMPILE_DECODER*/ +/* +Reads one metadata chunk (other than IHDR) of the PNG file and outputs what it +read in the state. Returns error code on failure. +Use lodepng_inspect first with a new state, then e.g. lodepng_chunk_find_const +to find the desired chunk type, and if non null use lodepng_inspect_chunk (with +chunk_pointer - start_of_file as pos). +Supports most metadata chunks from the PNG standard (gAMA, bKGD, tEXt, ...). +Ignores unsupported, unknown, non-metadata or IHDR chunks (without error). +Requirements: &in[pos] must point to start of a chunk, must use regular +lodepng_inspect first since format of most other chunks depends on IHDR, and if +there is a PLTE chunk, that one must be inspected before tRNS or bKGD. +*/ +unsigned lodepng_inspect_chunk(LodePNGState* state, size_t pos, + const unsigned char* in, size_t insize); #ifdef LODEPNG_COMPILE_ENCODER /*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/ @@ -674,11 +800,23 @@ The lodepng_chunk functions are normally not needed, except to traverse the unknown chunks stored in the LodePNGInfo struct, or add new ones to it. It also allows traversing the chunks of an encoded PNG file yourself. -PNG standard chunk naming conventions: -First byte: uppercase = critical, lowercase = ancillary -Second byte: uppercase = public, lowercase = private -Third byte: must be uppercase -Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy +The chunk pointer always points to the beginning of the chunk itself, that is +the first byte of the 4 length bytes. + +In the PNG file format, chunks have the following format: +-4 bytes length: length of the data of the chunk in bytes (chunk itself is 12 bytes longer) +-4 bytes chunk type (ASCII a-z,A-Z only, see below) +-length bytes of data (may be 0 bytes if length was 0) +-4 bytes of CRC, computed on chunk name + data + +The first chunk starts at the 8th byte of the PNG file, the entire rest of the file +exists out of concatenated chunks with the above format. + +PNG standard chunk ASCII naming conventions: +-First byte: uppercase = critical, lowercase = ancillary +-Second byte: uppercase = public, lowercase = private +-Third byte: must be uppercase +-Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy */ /* @@ -713,24 +851,38 @@ unsigned lodepng_chunk_check_crc(const unsigned char* chunk); /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/ void lodepng_chunk_generate_crc(unsigned char* chunk); -/*iterate to next chunks. don't use on IEND chunk, as there is no next chunk then*/ -unsigned char* lodepng_chunk_next(unsigned char* chunk); -const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk); +/* +Iterate to next chunks, allows iterating through all chunks of the PNG file. +Input must be at the beginning of a chunk (result of a previous lodepng_chunk_next call, +or the 8th byte of a PNG file which always has the first chunk), or alternatively may +point to the first byte of the PNG file (which is not a chunk but the magic header, the +function will then skip over it and return the first real chunk). +Will output pointer to the start of the next chunk, or at or beyond end of the file if there +is no more chunk after this or possibly if the chunk is corrupt. +Start this process at the 8th byte of the PNG file. +In a non-corrupt PNG file, the last chunk should have name "IEND". +*/ +unsigned char* lodepng_chunk_next(unsigned char* chunk, unsigned char* end); +const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk, const unsigned char* end); + +/*Finds the first chunk with the given type in the range [chunk, end), or returns NULL if not found.*/ +unsigned char* lodepng_chunk_find(unsigned char* chunk, unsigned char* end, const char type[5]); +const unsigned char* lodepng_chunk_find_const(const unsigned char* chunk, const unsigned char* end, const char type[5]); /* Appends chunk to the data in out. The given chunk should already have its chunk header. -The out variable and outlength are updated to reflect the new reallocated buffer. +The out variable and outsize are updated to reflect the new reallocated buffer. Returns error code (0 if it went ok) */ -unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk); +unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsigned char* chunk); /* Appends new chunk to out. The chunk to append is given by giving its length, type and data separately. The type is a 4-letter string. -The out variable and outlength are updated to reflect the new reallocated buffer. +The out variable and outsize are updated to reflect the new reallocated buffer. Returne error code (0 if it went ok) */ -unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, +unsigned lodepng_chunk_create(unsigned char** out, size_t* outsize, unsigned length, const char* type, const unsigned char* data); @@ -814,15 +966,13 @@ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const #ifdef LODEPNG_COMPILE_CPP /* The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. */ -namespace lodepng -{ +namespace lodepng { #ifdef LODEPNG_COMPILE_PNG -class State : public LodePNGState -{ +class State : public LodePNGState { public: State(); State(const State& other); - virtual ~State(); + ~State(); State& operator=(const State& other); }; @@ -890,15 +1040,21 @@ TODO: [.] test if there are no memory leaks or security exploits - done a lot but needs to be checked often [.] check compatibility with various compilers - done but needs to be redone for every newer version [X] converting color to 16-bit per channel types -[ ] read all public PNG chunk types (but never let the color profile and gamma ones touch RGB values) +[X] support color profile chunk types (but never let them touch RGB values by default) +[ ] support all public PNG chunk types (almost done except sBIT, sPLT and hIST) [ ] make sure encoder generates no chunks with size > (2^31)-1 [ ] partial decoding (stream processing) [X] let the "isFullyOpaque" function check color keys and transparent palettes too [X] better name for the variables "codes", "codesD", "codelengthcodes", "clcl" and "lldl" -[ ] don't stop decoding on errors like 69, 57, 58 (make warnings) +[ ] allow treating some errors like warnings, when image is recoverable (e.g. 69, 57, 58) +[ ] make warnings like: oob palette, checksum fail, data after iend, wrong/unknown crit chunk, no null terminator in text, ... +[ ] error messages with line numbers (and version) +[ ] errors in state instead of as return code? +[ ] new errors/warnings like suspiciously big decompressed ztxt or iccp chunk [ ] let the C++ wrapper catch exceptions coming from the standard library and return LodePNG error codes [ ] allow user to provide custom color conversion functions, e.g. for premultiplied alpha, padding bits or not, ... [ ] allow user to give data (void*) to custom allocator +[X] provide alternatives for C library functions not present on some platforms (memcpy, ...) */ #endif /*LODEPNG_H inclusion guard*/ @@ -993,8 +1149,10 @@ The following features are supported by the decoder: *) zlib decompression (inflate) *) zlib compression (deflate) *) CRC32 and ADLER32 checksums +*) colorimetric color profile conversions: currently experimentally available in lodepng_util.cpp only, + plus alternatively ability to pass on chroma/gamma/ICC profile information to other color management system. *) handling of unknown chunks, allowing making a PNG editor that stores custom and unknown chunks. -*) the following chunks are supported (generated/interpreted) by both encoder and decoder: +*) the following chunks are supported by both encoder and decoder: IHDR: header information PLTE: color palette IDAT: pixel data @@ -1006,6 +1164,10 @@ The following features are supported by the decoder: bKGD: suggested background color pHYs: physical dimensions tIME: modification time + cHRM: RGB chromaticities + gAMA: RGB gamma correction + iCCP: ICC color profile + sRGB: rendering intent 1.2. features not supported --------------------------- @@ -1014,10 +1176,10 @@ The following features are _not_ supported: *) some features needed to make a conformant PNG-Editor might be still missing. *) partial loading/stream processing. All data must be available and is processed in one call. -*) The following public chunks are not supported but treated as unknown chunks by LodePNG - cHRM, gAMA, iCCP, sRGB, sBIT, hIST, sPLT - Some of these are not supported on purpose: LodePNG wants to provide the RGB values - stored in the pixels, not values modified by system dependent gamma or color models. +*) The following public chunks are not (yet) supported but treated as unknown chunks by LodePNG: + sBIT + hIST + sPLT 2. C and C++ version @@ -1091,7 +1253,7 @@ LodePNGColorMode info_raw When decoding, here you can specify which color type you want the resulting raw image to be. If this is different from the colortype of the PNG, then the decoder will automatically convert the result. This conversion -always works, except if you want it to convert a color PNG to greyscale or to +always works, except if you want it to convert a color PNG to grayscale or to a palette with missing colors. By default, 32-bit color is used for the result. @@ -1187,7 +1349,7 @@ can encode the colors of all pixels without information loss. An important thing to note about LodePNG, is that the color type of the PNG, and the color type of the raw image, are completely independent. By default, when you decode a PNG, you get the result as a raw image in the color type you want, -no matter whether the PNG was encoded with a palette, greyscale or RGBA color. +no matter whether the PNG was encoded with a palette, grayscale or RGBA color. And if you encode an image, by default LodePNG will automatically choose the PNG color type that gives good compression based on the values of colors and amount of colors in the image. It can be configured to let you control it instead as @@ -1195,10 +1357,10 @@ well, though. To be able to do this, LodePNG does conversions from one color mode to another. It can convert from almost any color type to any other color type, except the -following conversions: RGB to greyscale is not supported, and converting to a +following conversions: RGB to grayscale is not supported, and converting to a palette when the palette doesn't have a required color is not supported. This is not supported on purpose: this is information loss which requires a color -reduction algorithm that is beyong the scope of a PNG encoder (yes, RGB to grey +reduction algorithm that is beyond the scope of a PNG encoder (yes, RGB to gray is easy, but there are multiple ways if you want to give some channels more weight). @@ -1219,10 +1381,10 @@ decoding to have another color type, a conversion is done by LodePNG. The PNG specification gives the following color types: -0: greyscale, bit depths 1, 2, 4, 8, 16 +0: grayscale, bit depths 1, 2, 4, 8, 16 2: RGB, bit depths 8 and 16 3: palette, bit depths 1, 2, 4 and 8 -4: greyscale with alpha, bit depths 8 and 16 +4: grayscale with alpha, bit depths 8 and 16 6: RGBA, bit depths 8 and 16 Bit depth is the amount of bits per pixel per color channel. So the total amount @@ -1271,15 +1433,22 @@ To avoid some confusion: the raw image correctly before encoding. -both encoder and decoder use the same color converter. +The function lodepng_convert does the color conversion. It is available in the +interface but normally isn't needed since the encoder and decoder already call +it. + Non supported color conversions: --color to greyscale: no error is thrown, but the result will look ugly because -only the red channel is taken --anything to palette when that palette does not have that color in it: in this -case an error is thrown +-color to grayscale when non-gray pixels are present: no error is thrown, but +the result will look ugly because only the red channel is taken (it assumes all +three channels are the same in this case so ignores green and blue). The reason +no error is given is to allow converting from three-channel grayscale images to +one-channel even if there are numerical imprecisions. +-anything to palette when the palette does not have an exact match for a from-color +in it: in this case an error is thrown Supported color conversions: -anything to 8-bit RGB, 8-bit RGBA, 16-bit RGB, 16-bit RGBA --any grey or grey+alpha, to grey or grey+alpha +-any gray or gray+alpha, to gray or gray+alpha -anything to a palette, as long as the palette has the requested colors in it -removing alpha channel -higher to smaller bitdepth, and vice versa @@ -1292,10 +1461,6 @@ false. as the PNG has, by setting the color_convert setting to false. Settings in info_raw are then ignored. -The function lodepng_convert does the color conversion. It is available in the -interface but normally isn't needed since the encoder and decoder already call -it. - 6.3. padding bits ----------------- @@ -1304,7 +1469,7 @@ have a bit amount that isn't a multiple of 8, then padding bits are used so that scanline starts at a fresh byte. But that is NOT true for the LodePNG raw input and output. The raw input image you give to the encoder, and the raw output image you get from the decoder will NOT have these padding bits, e.g. in the case of a 1-bit image with a width -of 7 pixels, the first pixel of the second scanline will the the 8th bit of the first byte, +of 7 pixels, the first pixel of the second scanline will the 8th bit of the first byte, not the first bit of a new byte. 6.4. A note about 16-bits per channel and endianness @@ -1405,12 +1570,12 @@ Iterate to the next chunk. This works if you have a buffer with consecutive chun functions do no boundary checking of the allocated data whatsoever, so make sure there is enough data available in the buffer to be able to go to the next chunk. -unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk): -unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, +unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsigned char* chunk): +unsigned lodepng_chunk_create(unsigned char** out, size_t* outsize, unsigned length, const char* type, const unsigned char* data): These functions are used to create new chunks that are appended to the data in *out that has -length *outlength. The append function appends an existing chunk to the new data. The create +length *outsize. The append function appends an existing chunk to the new data. The create function creates a new chunk with the given parameters and appends it. Type is the 4-letter name of the chunk. @@ -1421,7 +1586,7 @@ The LodePNGInfo struct contains fields with the unknown chunk in it. It has 3 buffers (each with size) to contain 3 types of unknown chunks: the ones that come before the PLTE chunk, the ones that come between the PLTE and the IDAT chunks, and the ones that come after the IDAT chunks. -It's necessary to make the distionction between these 3 cases because the PNG +It's necessary to make the distinction between these 3 cases because the PNG standard forces to keep the ordering of unknown chunks compared to the critical chunks, but does not force any other ordering rules. @@ -1504,7 +1669,7 @@ C and C++. *) Other Compilers If you encounter problems on any compilers, feel free to let me know and I may -try to fix it if the compiler is modern and standards complient. +try to fix it if the compiler is modern and standards compliant. 10. examples @@ -1519,8 +1684,7 @@ examples can be found on the LodePNG website. #include "lodepng.h" #include <iostream> -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { const char* filename = argc > 1 ? argv[1] : "test.png"; //load and decode @@ -1539,8 +1703,7 @@ int main(int argc, char *argv[]) #include "lodepng.h" -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { unsigned error; unsigned char* image; size_t width, height; @@ -1566,6 +1729,8 @@ For decoding: state.decoder.zlibsettings.ignore_adler32: ignore ADLER32 checksums state.decoder.zlibsettings.custom_...: use custom inflate function state.decoder.ignore_crc: ignore CRC checksums +state.decoder.ignore_critical: ignore unknown critical chunks +state.decoder.ignore_end: ignore missing IEND chunk. May fail if this corruption causes other errors state.decoder.color_convert: convert internal PNG color to chosen one state.decoder.read_text_chunks: whether to read in text metadata chunks state.decoder.remember_unknown_chunks: whether to read in unknown chunks @@ -1607,12 +1772,33 @@ yyyymmdd. Some changes aren't backwards compatible. Those are indicated with a (!) symbol. +Not all changes are listed here, the commit history in github lists more: +https://github.com/lvandeve/lodepng + +*) 06 mar 2020: simplified some of the dynamic memory allocations. +*) 12 jan 2020: (!) added 'end' argument to lodepng_chunk_next to allow correct + overflow checks. +*) 14 aug 2019: around 25% faster decoding thanks to huffman lookup tables. +*) 15 jun 2019: (!) auto_choose_color API changed (for bugfix: don't use palette + if gray ICC profile) and non-ICC LodePNGColorProfile renamed to + LodePNGColorStats. +*) 30 dec 2018: code style changes only: removed newlines before opening braces. +*) 10 sep 2018: added way to inspect metadata chunks without full decoding. +*) 19 aug 2018: (!) fixed color mode bKGD is encoded with and made it use + palette index in case of palette. +*) 10 aug 2018: (!) added support for gAMA, cHRM, sRGB and iCCP chunks. This + change is backwards compatible unless you relied on unknown_chunks for those. +*) 11 jun 2018: less restrictive check for pixel size integer overflow +*) 14 jan 2018: allow optionally ignoring a few more recoverable errors +*) 17 sep 2017: fix memory leak for some encoder input error cases +*) 27 nov 2016: grey+alpha auto color model detection bugfix *) 18 apr 2016: Changed qsort to custom stable sort (for platforms w/o qsort). *) 09 apr 2016: Fixed colorkey usage detection, and better file loading (within the limits of pure C90). *) 08 dec 2015: Made load_file function return error if file can't be opened. *) 24 okt 2015: Bugfix with decoding to palette output. *) 18 apr 2015: Boundary PM instead of just package-merge for faster encoding. +*) 24 aug 2014: Moved to github *) 23 aug 2014: Reduced needless memory usage of decoder. *) 28 jun 2014: Removed fix_png setting, always support palette OOB for simplicity. Made ColorProfile public. @@ -1620,25 +1806,25 @@ symbol. *) 22 dec 2013: Power of two windowsize required for optimization. *) 15 apr 2013: Fixed bug with LAC_ALPHA and color key. *) 25 mar 2013: Added an optional feature to ignore some PNG errors (fix_png). -*) 11 mar 2013 (!): Bugfix with custom free. Changed from "my" to "lodepng_" +*) 11 mar 2013: (!) Bugfix with custom free. Changed from "my" to "lodepng_" prefix for the custom allocators and made it possible with a new #define to use custom ones in your project without needing to change lodepng's code. *) 28 jan 2013: Bugfix with color key. *) 27 okt 2012: Tweaks in text chunk keyword length error handling. -*) 8 okt 2012 (!): Added new filter strategy (entropy) and new auto color mode. +*) 8 okt 2012: (!) Added new filter strategy (entropy) and new auto color mode. (no palette). Better deflate tree encoding. New compression tweak settings. Faster color conversions while decoding. Some internal cleanups. *) 23 sep 2012: Reduced warnings in Visual Studio a little bit. -*) 1 sep 2012 (!): Removed #define's for giving custom (de)compression functions +*) 1 sep 2012: (!) Removed #define's for giving custom (de)compression functions and made it work with function pointers instead. *) 23 jun 2012: Added more filter strategies. Made it easier to use custom alloc and free functions and toggle #defines from compiler flags. Small fixes. -*) 6 may 2012 (!): Made plugging in custom zlib/deflate functions more flexible. -*) 22 apr 2012 (!): Made interface more consistent, renaming a lot. Removed +*) 6 may 2012: (!) Made plugging in custom zlib/deflate functions more flexible. +*) 22 apr 2012: (!) Made interface more consistent, renaming a lot. Removed redundant C++ codec classes. Reduced amount of structs. Everything changed, but it is cleaner now imho and functionality remains the same. Also fixed several bugs and shrunk the implementation code. Made new samples. -*) 6 nov 2011 (!): By default, the encoder now automatically chooses the best +*) 6 nov 2011: (!) By default, the encoder now automatically chooses the best PNG color model and bit depth, based on the amount and type of colors of the raw image. For this, autoLeaveOutAlphaChannel replaced by auto_choose_color. *) 9 okt 2011: simpler hash chain implementation for the encoder. @@ -1647,7 +1833,7 @@ symbol. A bug with the PNG filtertype heuristic was fixed, so that it chooses much better ones (it's quite significant). A setting to do an experimental, slow, brute force search for PNG filter types is added. -*) 17 aug 2011 (!): changed some C zlib related function names. +*) 17 aug 2011: (!) changed some C zlib related function names. *) 16 aug 2011: made the code less wide (max 120 characters per line). *) 17 apr 2011: code cleanup. Bugfixes. Convert low to 16-bit per sample colors. *) 21 feb 2011: fixed compiling for C90. Fixed compiling with sections disabled. @@ -1755,5 +1941,5 @@ Domain: gmail dot com. Account: lode dot vandevenne. -Copyright (c) 2005-2016 Lode Vandevenne +Copyright (c) 2005-2020 Lode Vandevenne */ diff --git a/3rdparty/bimg/3rdparty/maratis-tcl/LICENSE b/3rdparty/bimg/3rdparty/maratis-tcl/LICENSE deleted file mode 100644 index d1832710e23..00000000000 --- a/3rdparty/bimg/3rdparty/maratis-tcl/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -Maratis Tiny C library - -Copyright (c) 2015 Anael Seghezzi <www.maratis3d.com> - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would - be appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not - be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. diff --git a/3rdparty/bimg/3rdparty/maratis-tcl/m_image.h b/3rdparty/bimg/3rdparty/maratis-tcl/m_image.h deleted file mode 100644 index 9923482fcaa..00000000000 --- a/3rdparty/bimg/3rdparty/maratis-tcl/m_image.h +++ /dev/null @@ -1,2340 +0,0 @@ -/*====================================================================== - Maratis Tiny C Library - version 1.0 ------------------------------------------------------------------------- - Copyright (c) 2015 Anael Seghezzi <www.maratis3d.org> - Copyright (c) 2015 Marti Maria Saguer - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would - be appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not - be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. - -========================================================================*/ -/* - Image manipulation : - - transformation (re-frame, mirror, rotation) - - conversions (float, half, ubyte, linear, greyscale...) - - filtering (convolution, Gaussian blur, Harris) - - scaling (pyramid, generic, bilinear) - - morphology (flood-fill, dilate, erode, thinning) - - edge and corner detection (Sobel, Harris) - - to create the implementation, - #define M_IMAGE_IMPLEMENTATION - in *one* C/CPP file that includes this file. - - optional: - include after *m_math.h* - - ////////////////////////////////////////////////////// - Example: create a 256x256 float image with 1 component: - - struct m_image foo1 = M_IMAGE_IDENTITY(); - struct m_image foo2 = M_IMAGE_IDENTITY(); - int x, y; - - m_image_create(&foo1, M_FLOAT, 256, 256, 1); - memset(foo1.data, 0, foo1.size * sizeof(float)); // clear to zero - - y = 128; x = 128; - ((float *)foo1.data)[y * foo1.width + x] = 1.0f; // set (x, y) pixel to one - - m_image_gaussian_blur(&foo2, &foo1, 3, 3); // apply Gaussian blur - - m_image_destroy(&foo2); - m_image_destroy(&foo1); -*/ - -#ifndef M_IMAGE_H -#define M_IMAGE_H - -#include <stdint.h> - -#define M_IMAGE_VERSION 1 - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef MIAPI -#define MIAPI extern -#endif - -#define M_VOID 0 -#define M_BOOL 1 -#define M_BYTE 2 -#define M_UBYTE 3 -#define M_SHORT 4 -#define M_USHORT 5 -#define M_INT 6 -#define M_UINT 7 -#define M_HALF 8 -#define M_FLOAT 9 -#define M_DOUBLE 10 - -struct m_image -{ - void *data; - int size; - int width; - int height; - int comp; - char type; -}; - -/* identity, must be used before calling m_image_create */ -#define M_IMAGE_IDENTITY() {0, 0, 0, 0, 0, 0} - -/* fully supported types are: M_UBYTE, M_USHORT, M_HALF, M_FLOAT - partially supported types: M_BYTE, M_SHORT, M_INT, M_UINT (no support for conversion) */ -MIAPI void m_image_create(struct m_image *image, char type, int width, int height, int comp); -MIAPI void m_image_destroy(struct m_image *image); - -MIAPI void m_image_ubyte_to_float(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_ushort_to_float(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_half_to_float(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_float_to_ubyte(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_float_to_ushort(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_float_to_half(struct m_image *dest, const struct m_image *src); - -MIAPI void m_image_copy(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_copy_sub_image(struct m_image *dest, const struct m_image *src, int x, int y, int w, int h); -MIAPI void m_image_reframe(struct m_image *dest, const struct m_image *src, int left, int top, int right, int bottom); -MIAPI void m_image_extract_component(struct m_image *dest, const struct m_image *src, int c); -MIAPI void m_image_rotate_left(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_rotate_right(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_rotate_180(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_mirror_x(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_mirror_y(struct m_image *dest, const struct m_image *src); - -MIAPI void m_image_premultiply(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_unpremultiply(struct m_image *dest, const struct m_image *src); - -/* float/half conversion */ -MIAPI float m_half2float(uint16_t h); -MIAPI uint16_t m_float2half(float flt); - -/* raw processing */ -MIAPI void m_gaussian_kernel(float *dest, int size); -MIAPI void m_sst(float *dest, const float *src, int count); -MIAPI void m_harris_response(float *dest, const float *src, int count); -MIAPI void m_tfm(float *dest, const float *src, int count); -MIAPI void m_normalize(float *dest, const float *src, int size); /* dest = src / norm(src) */ -MIAPI void m_normalize_sum(float *dest, const float *src, int size); /* dest = src / sum(src) */ -MIAPI float m_mean(const float *src, int size); -MIAPI float m_squared_distance(const float *src1, const float *src2, int size); -MIAPI float m_convolution(const float *src1, const float *src2, int size); /* a dot product really */ -MIAPI float m_chi_squared_distance(const float *src1, const float *src2, int size); /* good at estimating signed hystograms difference */ - -/* conversion to 1 component (float image only) */ -MIAPI void m_image_grey(struct m_image *dest, const struct m_image *src); /* from RGB src */ -MIAPI void m_image_max(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_max_abs(struct m_image *dest, const struct m_image *src); - -/* summed area table (also called "integral image") */ -MIAPI void m_image_summed_area(struct m_image *dest, const struct m_image *src); - -/* convolutions (float image only) */ -/* if alpha channel, src image must be pre-multiplied */ -MIAPI void m_image_convolution_h(struct m_image *dest, const struct m_image *src, float *kernel, int size); /* horizontal */ -MIAPI void m_image_convolution_v(struct m_image *dest, const struct m_image *src, float *kernel, int size); /* vertical */ -MIAPI void m_image_gaussian_blur(struct m_image *dest, const struct m_image *src, int dx, int dy); - -/* edge and corner (float 1 component image only) */ -MIAPI void m_image_sobel(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_harris(struct m_image *dest, const struct m_image *src, int radius); - -/* morphology (ubyte 1 component image only) */ -MIAPI int m_image_floodfill_4x(struct m_image *dest, int x, int y, unsigned char ref, unsigned char value, unsigned short *stack, int stack_size); -MIAPI int m_image_floodfill_8x(struct m_image *dest, int x, int y, unsigned char ref, unsigned char value, unsigned short *stack, int stack_size); -MIAPI void m_image_dilate(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_erode(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_edge_4x(struct m_image *dest, const struct m_image *src, unsigned char ref); -MIAPI void m_image_thin(struct m_image *dest); - -/* non maxima suppression (float image only) */ -MIAPI void m_image_non_max_supp(struct m_image *dest, const struct m_image *src, int radius, float threshold); - -/* detect Harris corners - margin: margin around the image to exclude corners - radius: maxima radius - threshold: Harris response threshold - corners: corners coordinates of size max_count * 2 - max_count: maximum number of corners - return corner count */ -MIAPI int m_image_corner_harris(const struct m_image *src, int margin, int radius, float threshold, int *corners, int max_count); - -/* resizing (float image only) */ -MIAPI void m_image_sub_pixel(const struct m_image *src, float x, float y, float *result); -MIAPI void m_image_pyrdown(struct m_image *dest, const struct m_image *src); -MIAPI void m_image_resize(struct m_image *dest, const struct m_image *src, int new_width, int new_height); - -#ifdef __cplusplus -} -#endif -/* -----------------------------------------------------------------------*/ -#endif /* M_IMAGE_H */ - -#ifdef M_IMAGE_IMPLEMENTATION - -#include <stdlib.h> -#include <stdio.h> -#include <memory.h> -#include <math.h> -#include <assert.h> - -#ifndef M_SAFE_FREE -#define M_SAFE_FREE(p) {if (p) {free(p); (p) = NULL;}} -#endif - -#ifndef M_MIN -#define M_MIN(a, b) (((a) < (b)) ? (a) : (b)) -#endif -#ifndef M_MAX -#define M_MAX(a, b) (((a) > (b)) ? (a) : (b)) -#endif -#ifndef M_ABS -#define M_ABS(a) (((a) < 0) ? -(a) : (a)) -#endif -#ifndef M_CLAMP -#define M_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) -#endif - -MIAPI void m_gaussian_kernel(float *dest, int size) -{ - if(size == 3) { - dest[0] = 0.25f; - dest[1] = 0.50f; - dest[2] = 0.25f; - } - else { - - float *k = dest; - float sigma = 1.6f; - float rs, s2; - float sum = 0.0f; - int radius = (size - 1) / 2; - int r; - - s2 = 1.0f / expf(sigma * sigma * 2.25f); - rs = sigma / (float)radius; - - /* compute gaussian kernel */ - for(r = -radius; r <= radius; r++) { - float x = fabsf(r * rs); - float v = (1.0f / expf(x * x)) - s2; - *k = v; - sum += v; - k++; - } - - /* normalize */ - if (sum > 0.0f) { - float isum = 1.0f / sum; - for (r = 0; r < size; r++) - dest[r] *= isum; - } - } -} - -MIAPI void m_sst(float *dest, const float *src, int count) -{ - int i; - for (i = 0; i < count; i++) { - float dx = src[0]; - float dy = src[1]; - dest[0] = dx*dx; - dest[1] = dy*dy; - dest[2] = dx*dy; - src += 2; - dest += 3; - } -} - -MIAPI void m_harris_response(float *dest, const float *src, int count) -{ - int i; - for (i = 0; i < count; i++) { - float dx2 = src[0]; - float dy2 = src[1]; - float dxy = src[2]; - *dest = (dx2 * dy2 - dxy * dxy) / (dx2 + dy2 + 1e-8f); - src += 3; - dest++; - } -} - -MIAPI void m_tfm(float *dest, const float *src, int count) -{ - int i; - for (i = 0; i < count; i++) { - - if (src[0] < src[1]) { - float dx2 = src[0]; - float dy2 = src[1]; - float dxy = src[2]; - float lambda = 0.5f * (dy2 + dx2 + sqrtf((dy2 * dy2) - (2.0f * dx2 * dy2) + (dx2 * dx2) + (4.0f * dxy * dxy))); - dest[0] = dx2 - lambda; - dest[1] = dxy; - } - else { - float dy2 = src[0]; - float dx2 = src[1]; - float dxy = src[2]; - float lambda = 0.5f * (dy2 + dx2 + sqrtf((dy2 * dy2) - (2.0f * dx2 * dy2) + (dx2 * dx2) + (4.0f * dxy * dxy))); - dest[0] = dxy; - dest[1] = dx2 - lambda; - } - - src += 3; - dest += 2; - } -} - -MIAPI float m_chi_squared_distance(const float *src1, const float *src2, int size) -{ - int i; - float score = 0; - for (i = 0; i < size; i++) { - - float val1 = src1[i]; - float val2 = src2[i]; - - /* chi squared distance */ - if ((val1 + val2) > 0) { - float x = val2 - val1; - score += (x * x) / (val1 + val2); - } - } - - return score * 0.5f; -} - -MIAPI float m_convolution(const float *src1, const float *src2, int size) -{ - float c = 0; int i; - for (i = 0; i < size; i++) - c += src1[i] * src2[i]; - return c; -} - -MIAPI void m_normalize(float *dest, const float *src, int size) -{ - float sum = 0.0f; int i; - for(i = 0; i < size; i++) - sum += src[i] * src[i]; - - if (sum > 0.0f) { - sum = 1.0f / sqrtf(sum); - for(i = 0; i < size; i++) - dest[i] = src[i] * sum; - } - else { - memset(dest, 0, size * sizeof(float)); - } -} - -MIAPI void m_normalize_sum(float *dest, const float *src, int size) -{ - float sum = 0.0f; int i; - for(i = 0; i < size; i++) - sum += src[i]; - - if (sum > 0.0f) { - sum = 1.0f / sum; - for(i = 0; i < size; i++) - dest[i] = src[i] * sum; - } - else { - memset(dest, 0, size * sizeof(float)); - } -} - -MIAPI float m_mean(const float *src, int size) -{ - float mean = 0; int i; - for (i = 0; i < size; i++) - mean += (*src++); - return size > 0 ? mean / (float)size : 0; -} - -MIAPI float m_squared_distance(const float *src1, const float *src2, int size) -{ - float score = 0; int i; - for (i = 0; i < size; i++) { - float x = src2[i] - src1[i]; - score += x * x; - } - return score; -} - -/* m_half2float / m_float2half : - a big thanks to Marti Maria Saguer for allowing the use of this code - under the zlib license from "Little Color Management System" (cmshalf.c) */ - -/* This code is inspired in the paper "Fast Half Float Conversions" - by Jeroen van der Zijp */ - -static uint32_t m__mantissa[2048] = { -0x00000000, 0x33800000, 0x34000000, 0x34400000, 0x34800000, 0x34a00000, -0x34c00000, 0x34e00000, 0x35000000, 0x35100000, 0x35200000, 0x35300000, -0x35400000, 0x35500000, 0x35600000, 0x35700000, 0x35800000, 0x35880000, -0x35900000, 0x35980000, 0x35a00000, 0x35a80000, 0x35b00000, 0x35b80000, -0x35c00000, 0x35c80000, 0x35d00000, 0x35d80000, 0x35e00000, 0x35e80000, -0x35f00000, 0x35f80000, 0x36000000, 0x36040000, 0x36080000, 0x360c0000, -0x36100000, 0x36140000, 0x36180000, 0x361c0000, 0x36200000, 0x36240000, -0x36280000, 0x362c0000, 0x36300000, 0x36340000, 0x36380000, 0x363c0000, -0x36400000, 0x36440000, 0x36480000, 0x364c0000, 0x36500000, 0x36540000, -0x36580000, 0x365c0000, 0x36600000, 0x36640000, 0x36680000, 0x366c0000, -0x36700000, 0x36740000, 0x36780000, 0x367c0000, 0x36800000, 0x36820000, -0x36840000, 0x36860000, 0x36880000, 0x368a0000, 0x368c0000, 0x368e0000, -0x36900000, 0x36920000, 0x36940000, 0x36960000, 0x36980000, 0x369a0000, -0x369c0000, 0x369e0000, 0x36a00000, 0x36a20000, 0x36a40000, 0x36a60000, -0x36a80000, 0x36aa0000, 0x36ac0000, 0x36ae0000, 0x36b00000, 0x36b20000, -0x36b40000, 0x36b60000, 0x36b80000, 0x36ba0000, 0x36bc0000, 0x36be0000, -0x36c00000, 0x36c20000, 0x36c40000, 0x36c60000, 0x36c80000, 0x36ca0000, -0x36cc0000, 0x36ce0000, 0x36d00000, 0x36d20000, 0x36d40000, 0x36d60000, -0x36d80000, 0x36da0000, 0x36dc0000, 0x36de0000, 0x36e00000, 0x36e20000, -0x36e40000, 0x36e60000, 0x36e80000, 0x36ea0000, 0x36ec0000, 0x36ee0000, -0x36f00000, 0x36f20000, 0x36f40000, 0x36f60000, 0x36f80000, 0x36fa0000, -0x36fc0000, 0x36fe0000, 0x37000000, 0x37010000, 0x37020000, 0x37030000, -0x37040000, 0x37050000, 0x37060000, 0x37070000, 0x37080000, 0x37090000, -0x370a0000, 0x370b0000, 0x370c0000, 0x370d0000, 0x370e0000, 0x370f0000, -0x37100000, 0x37110000, 0x37120000, 0x37130000, 0x37140000, 0x37150000, -0x37160000, 0x37170000, 0x37180000, 0x37190000, 0x371a0000, 0x371b0000, -0x371c0000, 0x371d0000, 0x371e0000, 0x371f0000, 0x37200000, 0x37210000, -0x37220000, 0x37230000, 0x37240000, 0x37250000, 0x37260000, 0x37270000, -0x37280000, 0x37290000, 0x372a0000, 0x372b0000, 0x372c0000, 0x372d0000, -0x372e0000, 0x372f0000, 0x37300000, 0x37310000, 0x37320000, 0x37330000, -0x37340000, 0x37350000, 0x37360000, 0x37370000, 0x37380000, 0x37390000, -0x373a0000, 0x373b0000, 0x373c0000, 0x373d0000, 0x373e0000, 0x373f0000, -0x37400000, 0x37410000, 0x37420000, 0x37430000, 0x37440000, 0x37450000, -0x37460000, 0x37470000, 0x37480000, 0x37490000, 0x374a0000, 0x374b0000, -0x374c0000, 0x374d0000, 0x374e0000, 0x374f0000, 0x37500000, 0x37510000, -0x37520000, 0x37530000, 0x37540000, 0x37550000, 0x37560000, 0x37570000, -0x37580000, 0x37590000, 0x375a0000, 0x375b0000, 0x375c0000, 0x375d0000, -0x375e0000, 0x375f0000, 0x37600000, 0x37610000, 0x37620000, 0x37630000, -0x37640000, 0x37650000, 0x37660000, 0x37670000, 0x37680000, 0x37690000, -0x376a0000, 0x376b0000, 0x376c0000, 0x376d0000, 0x376e0000, 0x376f0000, -0x37700000, 0x37710000, 0x37720000, 0x37730000, 0x37740000, 0x37750000, -0x37760000, 0x37770000, 0x37780000, 0x37790000, 0x377a0000, 0x377b0000, -0x377c0000, 0x377d0000, 0x377e0000, 0x377f0000, 0x37800000, 0x37808000, -0x37810000, 0x37818000, 0x37820000, 0x37828000, 0x37830000, 0x37838000, -0x37840000, 0x37848000, 0x37850000, 0x37858000, 0x37860000, 0x37868000, -0x37870000, 0x37878000, 0x37880000, 0x37888000, 0x37890000, 0x37898000, -0x378a0000, 0x378a8000, 0x378b0000, 0x378b8000, 0x378c0000, 0x378c8000, -0x378d0000, 0x378d8000, 0x378e0000, 0x378e8000, 0x378f0000, 0x378f8000, -0x37900000, 0x37908000, 0x37910000, 0x37918000, 0x37920000, 0x37928000, -0x37930000, 0x37938000, 0x37940000, 0x37948000, 0x37950000, 0x37958000, -0x37960000, 0x37968000, 0x37970000, 0x37978000, 0x37980000, 0x37988000, -0x37990000, 0x37998000, 0x379a0000, 0x379a8000, 0x379b0000, 0x379b8000, -0x379c0000, 0x379c8000, 0x379d0000, 0x379d8000, 0x379e0000, 0x379e8000, -0x379f0000, 0x379f8000, 0x37a00000, 0x37a08000, 0x37a10000, 0x37a18000, -0x37a20000, 0x37a28000, 0x37a30000, 0x37a38000, 0x37a40000, 0x37a48000, -0x37a50000, 0x37a58000, 0x37a60000, 0x37a68000, 0x37a70000, 0x37a78000, -0x37a80000, 0x37a88000, 0x37a90000, 0x37a98000, 0x37aa0000, 0x37aa8000, -0x37ab0000, 0x37ab8000, 0x37ac0000, 0x37ac8000, 0x37ad0000, 0x37ad8000, -0x37ae0000, 0x37ae8000, 0x37af0000, 0x37af8000, 0x37b00000, 0x37b08000, -0x37b10000, 0x37b18000, 0x37b20000, 0x37b28000, 0x37b30000, 0x37b38000, -0x37b40000, 0x37b48000, 0x37b50000, 0x37b58000, 0x37b60000, 0x37b68000, -0x37b70000, 0x37b78000, 0x37b80000, 0x37b88000, 0x37b90000, 0x37b98000, -0x37ba0000, 0x37ba8000, 0x37bb0000, 0x37bb8000, 0x37bc0000, 0x37bc8000, -0x37bd0000, 0x37bd8000, 0x37be0000, 0x37be8000, 0x37bf0000, 0x37bf8000, -0x37c00000, 0x37c08000, 0x37c10000, 0x37c18000, 0x37c20000, 0x37c28000, -0x37c30000, 0x37c38000, 0x37c40000, 0x37c48000, 0x37c50000, 0x37c58000, -0x37c60000, 0x37c68000, 0x37c70000, 0x37c78000, 0x37c80000, 0x37c88000, -0x37c90000, 0x37c98000, 0x37ca0000, 0x37ca8000, 0x37cb0000, 0x37cb8000, -0x37cc0000, 0x37cc8000, 0x37cd0000, 0x37cd8000, 0x37ce0000, 0x37ce8000, -0x37cf0000, 0x37cf8000, 0x37d00000, 0x37d08000, 0x37d10000, 0x37d18000, -0x37d20000, 0x37d28000, 0x37d30000, 0x37d38000, 0x37d40000, 0x37d48000, -0x37d50000, 0x37d58000, 0x37d60000, 0x37d68000, 0x37d70000, 0x37d78000, -0x37d80000, 0x37d88000, 0x37d90000, 0x37d98000, 0x37da0000, 0x37da8000, -0x37db0000, 0x37db8000, 0x37dc0000, 0x37dc8000, 0x37dd0000, 0x37dd8000, -0x37de0000, 0x37de8000, 0x37df0000, 0x37df8000, 0x37e00000, 0x37e08000, -0x37e10000, 0x37e18000, 0x37e20000, 0x37e28000, 0x37e30000, 0x37e38000, -0x37e40000, 0x37e48000, 0x37e50000, 0x37e58000, 0x37e60000, 0x37e68000, -0x37e70000, 0x37e78000, 0x37e80000, 0x37e88000, 0x37e90000, 0x37e98000, -0x37ea0000, 0x37ea8000, 0x37eb0000, 0x37eb8000, 0x37ec0000, 0x37ec8000, -0x37ed0000, 0x37ed8000, 0x37ee0000, 0x37ee8000, 0x37ef0000, 0x37ef8000, -0x37f00000, 0x37f08000, 0x37f10000, 0x37f18000, 0x37f20000, 0x37f28000, -0x37f30000, 0x37f38000, 0x37f40000, 0x37f48000, 0x37f50000, 0x37f58000, -0x37f60000, 0x37f68000, 0x37f70000, 0x37f78000, 0x37f80000, 0x37f88000, -0x37f90000, 0x37f98000, 0x37fa0000, 0x37fa8000, 0x37fb0000, 0x37fb8000, -0x37fc0000, 0x37fc8000, 0x37fd0000, 0x37fd8000, 0x37fe0000, 0x37fe8000, -0x37ff0000, 0x37ff8000, 0x38000000, 0x38004000, 0x38008000, 0x3800c000, -0x38010000, 0x38014000, 0x38018000, 0x3801c000, 0x38020000, 0x38024000, -0x38028000, 0x3802c000, 0x38030000, 0x38034000, 0x38038000, 0x3803c000, -0x38040000, 0x38044000, 0x38048000, 0x3804c000, 0x38050000, 0x38054000, -0x38058000, 0x3805c000, 0x38060000, 0x38064000, 0x38068000, 0x3806c000, -0x38070000, 0x38074000, 0x38078000, 0x3807c000, 0x38080000, 0x38084000, -0x38088000, 0x3808c000, 0x38090000, 0x38094000, 0x38098000, 0x3809c000, -0x380a0000, 0x380a4000, 0x380a8000, 0x380ac000, 0x380b0000, 0x380b4000, -0x380b8000, 0x380bc000, 0x380c0000, 0x380c4000, 0x380c8000, 0x380cc000, -0x380d0000, 0x380d4000, 0x380d8000, 0x380dc000, 0x380e0000, 0x380e4000, -0x380e8000, 0x380ec000, 0x380f0000, 0x380f4000, 0x380f8000, 0x380fc000, -0x38100000, 0x38104000, 0x38108000, 0x3810c000, 0x38110000, 0x38114000, -0x38118000, 0x3811c000, 0x38120000, 0x38124000, 0x38128000, 0x3812c000, -0x38130000, 0x38134000, 0x38138000, 0x3813c000, 0x38140000, 0x38144000, -0x38148000, 0x3814c000, 0x38150000, 0x38154000, 0x38158000, 0x3815c000, -0x38160000, 0x38164000, 0x38168000, 0x3816c000, 0x38170000, 0x38174000, -0x38178000, 0x3817c000, 0x38180000, 0x38184000, 0x38188000, 0x3818c000, -0x38190000, 0x38194000, 0x38198000, 0x3819c000, 0x381a0000, 0x381a4000, -0x381a8000, 0x381ac000, 0x381b0000, 0x381b4000, 0x381b8000, 0x381bc000, -0x381c0000, 0x381c4000, 0x381c8000, 0x381cc000, 0x381d0000, 0x381d4000, -0x381d8000, 0x381dc000, 0x381e0000, 0x381e4000, 0x381e8000, 0x381ec000, -0x381f0000, 0x381f4000, 0x381f8000, 0x381fc000, 0x38200000, 0x38204000, -0x38208000, 0x3820c000, 0x38210000, 0x38214000, 0x38218000, 0x3821c000, -0x38220000, 0x38224000, 0x38228000, 0x3822c000, 0x38230000, 0x38234000, -0x38238000, 0x3823c000, 0x38240000, 0x38244000, 0x38248000, 0x3824c000, -0x38250000, 0x38254000, 0x38258000, 0x3825c000, 0x38260000, 0x38264000, -0x38268000, 0x3826c000, 0x38270000, 0x38274000, 0x38278000, 0x3827c000, -0x38280000, 0x38284000, 0x38288000, 0x3828c000, 0x38290000, 0x38294000, -0x38298000, 0x3829c000, 0x382a0000, 0x382a4000, 0x382a8000, 0x382ac000, -0x382b0000, 0x382b4000, 0x382b8000, 0x382bc000, 0x382c0000, 0x382c4000, -0x382c8000, 0x382cc000, 0x382d0000, 0x382d4000, 0x382d8000, 0x382dc000, -0x382e0000, 0x382e4000, 0x382e8000, 0x382ec000, 0x382f0000, 0x382f4000, -0x382f8000, 0x382fc000, 0x38300000, 0x38304000, 0x38308000, 0x3830c000, -0x38310000, 0x38314000, 0x38318000, 0x3831c000, 0x38320000, 0x38324000, -0x38328000, 0x3832c000, 0x38330000, 0x38334000, 0x38338000, 0x3833c000, -0x38340000, 0x38344000, 0x38348000, 0x3834c000, 0x38350000, 0x38354000, -0x38358000, 0x3835c000, 0x38360000, 0x38364000, 0x38368000, 0x3836c000, -0x38370000, 0x38374000, 0x38378000, 0x3837c000, 0x38380000, 0x38384000, -0x38388000, 0x3838c000, 0x38390000, 0x38394000, 0x38398000, 0x3839c000, -0x383a0000, 0x383a4000, 0x383a8000, 0x383ac000, 0x383b0000, 0x383b4000, -0x383b8000, 0x383bc000, 0x383c0000, 0x383c4000, 0x383c8000, 0x383cc000, -0x383d0000, 0x383d4000, 0x383d8000, 0x383dc000, 0x383e0000, 0x383e4000, -0x383e8000, 0x383ec000, 0x383f0000, 0x383f4000, 0x383f8000, 0x383fc000, -0x38400000, 0x38404000, 0x38408000, 0x3840c000, 0x38410000, 0x38414000, -0x38418000, 0x3841c000, 0x38420000, 0x38424000, 0x38428000, 0x3842c000, -0x38430000, 0x38434000, 0x38438000, 0x3843c000, 0x38440000, 0x38444000, -0x38448000, 0x3844c000, 0x38450000, 0x38454000, 0x38458000, 0x3845c000, -0x38460000, 0x38464000, 0x38468000, 0x3846c000, 0x38470000, 0x38474000, -0x38478000, 0x3847c000, 0x38480000, 0x38484000, 0x38488000, 0x3848c000, -0x38490000, 0x38494000, 0x38498000, 0x3849c000, 0x384a0000, 0x384a4000, -0x384a8000, 0x384ac000, 0x384b0000, 0x384b4000, 0x384b8000, 0x384bc000, -0x384c0000, 0x384c4000, 0x384c8000, 0x384cc000, 0x384d0000, 0x384d4000, -0x384d8000, 0x384dc000, 0x384e0000, 0x384e4000, 0x384e8000, 0x384ec000, -0x384f0000, 0x384f4000, 0x384f8000, 0x384fc000, 0x38500000, 0x38504000, -0x38508000, 0x3850c000, 0x38510000, 0x38514000, 0x38518000, 0x3851c000, -0x38520000, 0x38524000, 0x38528000, 0x3852c000, 0x38530000, 0x38534000, -0x38538000, 0x3853c000, 0x38540000, 0x38544000, 0x38548000, 0x3854c000, -0x38550000, 0x38554000, 0x38558000, 0x3855c000, 0x38560000, 0x38564000, -0x38568000, 0x3856c000, 0x38570000, 0x38574000, 0x38578000, 0x3857c000, -0x38580000, 0x38584000, 0x38588000, 0x3858c000, 0x38590000, 0x38594000, -0x38598000, 0x3859c000, 0x385a0000, 0x385a4000, 0x385a8000, 0x385ac000, -0x385b0000, 0x385b4000, 0x385b8000, 0x385bc000, 0x385c0000, 0x385c4000, -0x385c8000, 0x385cc000, 0x385d0000, 0x385d4000, 0x385d8000, 0x385dc000, -0x385e0000, 0x385e4000, 0x385e8000, 0x385ec000, 0x385f0000, 0x385f4000, -0x385f8000, 0x385fc000, 0x38600000, 0x38604000, 0x38608000, 0x3860c000, -0x38610000, 0x38614000, 0x38618000, 0x3861c000, 0x38620000, 0x38624000, -0x38628000, 0x3862c000, 0x38630000, 0x38634000, 0x38638000, 0x3863c000, -0x38640000, 0x38644000, 0x38648000, 0x3864c000, 0x38650000, 0x38654000, -0x38658000, 0x3865c000, 0x38660000, 0x38664000, 0x38668000, 0x3866c000, -0x38670000, 0x38674000, 0x38678000, 0x3867c000, 0x38680000, 0x38684000, -0x38688000, 0x3868c000, 0x38690000, 0x38694000, 0x38698000, 0x3869c000, -0x386a0000, 0x386a4000, 0x386a8000, 0x386ac000, 0x386b0000, 0x386b4000, -0x386b8000, 0x386bc000, 0x386c0000, 0x386c4000, 0x386c8000, 0x386cc000, -0x386d0000, 0x386d4000, 0x386d8000, 0x386dc000, 0x386e0000, 0x386e4000, -0x386e8000, 0x386ec000, 0x386f0000, 0x386f4000, 0x386f8000, 0x386fc000, -0x38700000, 0x38704000, 0x38708000, 0x3870c000, 0x38710000, 0x38714000, -0x38718000, 0x3871c000, 0x38720000, 0x38724000, 0x38728000, 0x3872c000, -0x38730000, 0x38734000, 0x38738000, 0x3873c000, 0x38740000, 0x38744000, -0x38748000, 0x3874c000, 0x38750000, 0x38754000, 0x38758000, 0x3875c000, -0x38760000, 0x38764000, 0x38768000, 0x3876c000, 0x38770000, 0x38774000, -0x38778000, 0x3877c000, 0x38780000, 0x38784000, 0x38788000, 0x3878c000, -0x38790000, 0x38794000, 0x38798000, 0x3879c000, 0x387a0000, 0x387a4000, -0x387a8000, 0x387ac000, 0x387b0000, 0x387b4000, 0x387b8000, 0x387bc000, -0x387c0000, 0x387c4000, 0x387c8000, 0x387cc000, 0x387d0000, 0x387d4000, -0x387d8000, 0x387dc000, 0x387e0000, 0x387e4000, 0x387e8000, 0x387ec000, -0x387f0000, 0x387f4000, 0x387f8000, 0x387fc000, 0x38000000, 0x38002000, -0x38004000, 0x38006000, 0x38008000, 0x3800a000, 0x3800c000, 0x3800e000, -0x38010000, 0x38012000, 0x38014000, 0x38016000, 0x38018000, 0x3801a000, -0x3801c000, 0x3801e000, 0x38020000, 0x38022000, 0x38024000, 0x38026000, -0x38028000, 0x3802a000, 0x3802c000, 0x3802e000, 0x38030000, 0x38032000, -0x38034000, 0x38036000, 0x38038000, 0x3803a000, 0x3803c000, 0x3803e000, -0x38040000, 0x38042000, 0x38044000, 0x38046000, 0x38048000, 0x3804a000, -0x3804c000, 0x3804e000, 0x38050000, 0x38052000, 0x38054000, 0x38056000, -0x38058000, 0x3805a000, 0x3805c000, 0x3805e000, 0x38060000, 0x38062000, -0x38064000, 0x38066000, 0x38068000, 0x3806a000, 0x3806c000, 0x3806e000, -0x38070000, 0x38072000, 0x38074000, 0x38076000, 0x38078000, 0x3807a000, -0x3807c000, 0x3807e000, 0x38080000, 0x38082000, 0x38084000, 0x38086000, -0x38088000, 0x3808a000, 0x3808c000, 0x3808e000, 0x38090000, 0x38092000, -0x38094000, 0x38096000, 0x38098000, 0x3809a000, 0x3809c000, 0x3809e000, -0x380a0000, 0x380a2000, 0x380a4000, 0x380a6000, 0x380a8000, 0x380aa000, -0x380ac000, 0x380ae000, 0x380b0000, 0x380b2000, 0x380b4000, 0x380b6000, -0x380b8000, 0x380ba000, 0x380bc000, 0x380be000, 0x380c0000, 0x380c2000, -0x380c4000, 0x380c6000, 0x380c8000, 0x380ca000, 0x380cc000, 0x380ce000, -0x380d0000, 0x380d2000, 0x380d4000, 0x380d6000, 0x380d8000, 0x380da000, -0x380dc000, 0x380de000, 0x380e0000, 0x380e2000, 0x380e4000, 0x380e6000, -0x380e8000, 0x380ea000, 0x380ec000, 0x380ee000, 0x380f0000, 0x380f2000, -0x380f4000, 0x380f6000, 0x380f8000, 0x380fa000, 0x380fc000, 0x380fe000, -0x38100000, 0x38102000, 0x38104000, 0x38106000, 0x38108000, 0x3810a000, -0x3810c000, 0x3810e000, 0x38110000, 0x38112000, 0x38114000, 0x38116000, -0x38118000, 0x3811a000, 0x3811c000, 0x3811e000, 0x38120000, 0x38122000, -0x38124000, 0x38126000, 0x38128000, 0x3812a000, 0x3812c000, 0x3812e000, -0x38130000, 0x38132000, 0x38134000, 0x38136000, 0x38138000, 0x3813a000, -0x3813c000, 0x3813e000, 0x38140000, 0x38142000, 0x38144000, 0x38146000, -0x38148000, 0x3814a000, 0x3814c000, 0x3814e000, 0x38150000, 0x38152000, -0x38154000, 0x38156000, 0x38158000, 0x3815a000, 0x3815c000, 0x3815e000, -0x38160000, 0x38162000, 0x38164000, 0x38166000, 0x38168000, 0x3816a000, -0x3816c000, 0x3816e000, 0x38170000, 0x38172000, 0x38174000, 0x38176000, -0x38178000, 0x3817a000, 0x3817c000, 0x3817e000, 0x38180000, 0x38182000, -0x38184000, 0x38186000, 0x38188000, 0x3818a000, 0x3818c000, 0x3818e000, -0x38190000, 0x38192000, 0x38194000, 0x38196000, 0x38198000, 0x3819a000, -0x3819c000, 0x3819e000, 0x381a0000, 0x381a2000, 0x381a4000, 0x381a6000, -0x381a8000, 0x381aa000, 0x381ac000, 0x381ae000, 0x381b0000, 0x381b2000, -0x381b4000, 0x381b6000, 0x381b8000, 0x381ba000, 0x381bc000, 0x381be000, -0x381c0000, 0x381c2000, 0x381c4000, 0x381c6000, 0x381c8000, 0x381ca000, -0x381cc000, 0x381ce000, 0x381d0000, 0x381d2000, 0x381d4000, 0x381d6000, -0x381d8000, 0x381da000, 0x381dc000, 0x381de000, 0x381e0000, 0x381e2000, -0x381e4000, 0x381e6000, 0x381e8000, 0x381ea000, 0x381ec000, 0x381ee000, -0x381f0000, 0x381f2000, 0x381f4000, 0x381f6000, 0x381f8000, 0x381fa000, -0x381fc000, 0x381fe000, 0x38200000, 0x38202000, 0x38204000, 0x38206000, -0x38208000, 0x3820a000, 0x3820c000, 0x3820e000, 0x38210000, 0x38212000, -0x38214000, 0x38216000, 0x38218000, 0x3821a000, 0x3821c000, 0x3821e000, -0x38220000, 0x38222000, 0x38224000, 0x38226000, 0x38228000, 0x3822a000, -0x3822c000, 0x3822e000, 0x38230000, 0x38232000, 0x38234000, 0x38236000, -0x38238000, 0x3823a000, 0x3823c000, 0x3823e000, 0x38240000, 0x38242000, -0x38244000, 0x38246000, 0x38248000, 0x3824a000, 0x3824c000, 0x3824e000, -0x38250000, 0x38252000, 0x38254000, 0x38256000, 0x38258000, 0x3825a000, -0x3825c000, 0x3825e000, 0x38260000, 0x38262000, 0x38264000, 0x38266000, -0x38268000, 0x3826a000, 0x3826c000, 0x3826e000, 0x38270000, 0x38272000, -0x38274000, 0x38276000, 0x38278000, 0x3827a000, 0x3827c000, 0x3827e000, -0x38280000, 0x38282000, 0x38284000, 0x38286000, 0x38288000, 0x3828a000, -0x3828c000, 0x3828e000, 0x38290000, 0x38292000, 0x38294000, 0x38296000, -0x38298000, 0x3829a000, 0x3829c000, 0x3829e000, 0x382a0000, 0x382a2000, -0x382a4000, 0x382a6000, 0x382a8000, 0x382aa000, 0x382ac000, 0x382ae000, -0x382b0000, 0x382b2000, 0x382b4000, 0x382b6000, 0x382b8000, 0x382ba000, -0x382bc000, 0x382be000, 0x382c0000, 0x382c2000, 0x382c4000, 0x382c6000, -0x382c8000, 0x382ca000, 0x382cc000, 0x382ce000, 0x382d0000, 0x382d2000, -0x382d4000, 0x382d6000, 0x382d8000, 0x382da000, 0x382dc000, 0x382de000, -0x382e0000, 0x382e2000, 0x382e4000, 0x382e6000, 0x382e8000, 0x382ea000, -0x382ec000, 0x382ee000, 0x382f0000, 0x382f2000, 0x382f4000, 0x382f6000, -0x382f8000, 0x382fa000, 0x382fc000, 0x382fe000, 0x38300000, 0x38302000, -0x38304000, 0x38306000, 0x38308000, 0x3830a000, 0x3830c000, 0x3830e000, -0x38310000, 0x38312000, 0x38314000, 0x38316000, 0x38318000, 0x3831a000, -0x3831c000, 0x3831e000, 0x38320000, 0x38322000, 0x38324000, 0x38326000, -0x38328000, 0x3832a000, 0x3832c000, 0x3832e000, 0x38330000, 0x38332000, -0x38334000, 0x38336000, 0x38338000, 0x3833a000, 0x3833c000, 0x3833e000, -0x38340000, 0x38342000, 0x38344000, 0x38346000, 0x38348000, 0x3834a000, -0x3834c000, 0x3834e000, 0x38350000, 0x38352000, 0x38354000, 0x38356000, -0x38358000, 0x3835a000, 0x3835c000, 0x3835e000, 0x38360000, 0x38362000, -0x38364000, 0x38366000, 0x38368000, 0x3836a000, 0x3836c000, 0x3836e000, -0x38370000, 0x38372000, 0x38374000, 0x38376000, 0x38378000, 0x3837a000, -0x3837c000, 0x3837e000, 0x38380000, 0x38382000, 0x38384000, 0x38386000, -0x38388000, 0x3838a000, 0x3838c000, 0x3838e000, 0x38390000, 0x38392000, -0x38394000, 0x38396000, 0x38398000, 0x3839a000, 0x3839c000, 0x3839e000, -0x383a0000, 0x383a2000, 0x383a4000, 0x383a6000, 0x383a8000, 0x383aa000, -0x383ac000, 0x383ae000, 0x383b0000, 0x383b2000, 0x383b4000, 0x383b6000, -0x383b8000, 0x383ba000, 0x383bc000, 0x383be000, 0x383c0000, 0x383c2000, -0x383c4000, 0x383c6000, 0x383c8000, 0x383ca000, 0x383cc000, 0x383ce000, -0x383d0000, 0x383d2000, 0x383d4000, 0x383d6000, 0x383d8000, 0x383da000, -0x383dc000, 0x383de000, 0x383e0000, 0x383e2000, 0x383e4000, 0x383e6000, -0x383e8000, 0x383ea000, 0x383ec000, 0x383ee000, 0x383f0000, 0x383f2000, -0x383f4000, 0x383f6000, 0x383f8000, 0x383fa000, 0x383fc000, 0x383fe000, -0x38400000, 0x38402000, 0x38404000, 0x38406000, 0x38408000, 0x3840a000, -0x3840c000, 0x3840e000, 0x38410000, 0x38412000, 0x38414000, 0x38416000, -0x38418000, 0x3841a000, 0x3841c000, 0x3841e000, 0x38420000, 0x38422000, -0x38424000, 0x38426000, 0x38428000, 0x3842a000, 0x3842c000, 0x3842e000, -0x38430000, 0x38432000, 0x38434000, 0x38436000, 0x38438000, 0x3843a000, -0x3843c000, 0x3843e000, 0x38440000, 0x38442000, 0x38444000, 0x38446000, -0x38448000, 0x3844a000, 0x3844c000, 0x3844e000, 0x38450000, 0x38452000, -0x38454000, 0x38456000, 0x38458000, 0x3845a000, 0x3845c000, 0x3845e000, -0x38460000, 0x38462000, 0x38464000, 0x38466000, 0x38468000, 0x3846a000, -0x3846c000, 0x3846e000, 0x38470000, 0x38472000, 0x38474000, 0x38476000, -0x38478000, 0x3847a000, 0x3847c000, 0x3847e000, 0x38480000, 0x38482000, -0x38484000, 0x38486000, 0x38488000, 0x3848a000, 0x3848c000, 0x3848e000, -0x38490000, 0x38492000, 0x38494000, 0x38496000, 0x38498000, 0x3849a000, -0x3849c000, 0x3849e000, 0x384a0000, 0x384a2000, 0x384a4000, 0x384a6000, -0x384a8000, 0x384aa000, 0x384ac000, 0x384ae000, 0x384b0000, 0x384b2000, -0x384b4000, 0x384b6000, 0x384b8000, 0x384ba000, 0x384bc000, 0x384be000, -0x384c0000, 0x384c2000, 0x384c4000, 0x384c6000, 0x384c8000, 0x384ca000, -0x384cc000, 0x384ce000, 0x384d0000, 0x384d2000, 0x384d4000, 0x384d6000, -0x384d8000, 0x384da000, 0x384dc000, 0x384de000, 0x384e0000, 0x384e2000, -0x384e4000, 0x384e6000, 0x384e8000, 0x384ea000, 0x384ec000, 0x384ee000, -0x384f0000, 0x384f2000, 0x384f4000, 0x384f6000, 0x384f8000, 0x384fa000, -0x384fc000, 0x384fe000, 0x38500000, 0x38502000, 0x38504000, 0x38506000, -0x38508000, 0x3850a000, 0x3850c000, 0x3850e000, 0x38510000, 0x38512000, -0x38514000, 0x38516000, 0x38518000, 0x3851a000, 0x3851c000, 0x3851e000, -0x38520000, 0x38522000, 0x38524000, 0x38526000, 0x38528000, 0x3852a000, -0x3852c000, 0x3852e000, 0x38530000, 0x38532000, 0x38534000, 0x38536000, -0x38538000, 0x3853a000, 0x3853c000, 0x3853e000, 0x38540000, 0x38542000, -0x38544000, 0x38546000, 0x38548000, 0x3854a000, 0x3854c000, 0x3854e000, -0x38550000, 0x38552000, 0x38554000, 0x38556000, 0x38558000, 0x3855a000, -0x3855c000, 0x3855e000, 0x38560000, 0x38562000, 0x38564000, 0x38566000, -0x38568000, 0x3856a000, 0x3856c000, 0x3856e000, 0x38570000, 0x38572000, -0x38574000, 0x38576000, 0x38578000, 0x3857a000, 0x3857c000, 0x3857e000, -0x38580000, 0x38582000, 0x38584000, 0x38586000, 0x38588000, 0x3858a000, -0x3858c000, 0x3858e000, 0x38590000, 0x38592000, 0x38594000, 0x38596000, -0x38598000, 0x3859a000, 0x3859c000, 0x3859e000, 0x385a0000, 0x385a2000, -0x385a4000, 0x385a6000, 0x385a8000, 0x385aa000, 0x385ac000, 0x385ae000, -0x385b0000, 0x385b2000, 0x385b4000, 0x385b6000, 0x385b8000, 0x385ba000, -0x385bc000, 0x385be000, 0x385c0000, 0x385c2000, 0x385c4000, 0x385c6000, -0x385c8000, 0x385ca000, 0x385cc000, 0x385ce000, 0x385d0000, 0x385d2000, -0x385d4000, 0x385d6000, 0x385d8000, 0x385da000, 0x385dc000, 0x385de000, -0x385e0000, 0x385e2000, 0x385e4000, 0x385e6000, 0x385e8000, 0x385ea000, -0x385ec000, 0x385ee000, 0x385f0000, 0x385f2000, 0x385f4000, 0x385f6000, -0x385f8000, 0x385fa000, 0x385fc000, 0x385fe000, 0x38600000, 0x38602000, -0x38604000, 0x38606000, 0x38608000, 0x3860a000, 0x3860c000, 0x3860e000, -0x38610000, 0x38612000, 0x38614000, 0x38616000, 0x38618000, 0x3861a000, -0x3861c000, 0x3861e000, 0x38620000, 0x38622000, 0x38624000, 0x38626000, -0x38628000, 0x3862a000, 0x3862c000, 0x3862e000, 0x38630000, 0x38632000, -0x38634000, 0x38636000, 0x38638000, 0x3863a000, 0x3863c000, 0x3863e000, -0x38640000, 0x38642000, 0x38644000, 0x38646000, 0x38648000, 0x3864a000, -0x3864c000, 0x3864e000, 0x38650000, 0x38652000, 0x38654000, 0x38656000, -0x38658000, 0x3865a000, 0x3865c000, 0x3865e000, 0x38660000, 0x38662000, -0x38664000, 0x38666000, 0x38668000, 0x3866a000, 0x3866c000, 0x3866e000, -0x38670000, 0x38672000, 0x38674000, 0x38676000, 0x38678000, 0x3867a000, -0x3867c000, 0x3867e000, 0x38680000, 0x38682000, 0x38684000, 0x38686000, -0x38688000, 0x3868a000, 0x3868c000, 0x3868e000, 0x38690000, 0x38692000, -0x38694000, 0x38696000, 0x38698000, 0x3869a000, 0x3869c000, 0x3869e000, -0x386a0000, 0x386a2000, 0x386a4000, 0x386a6000, 0x386a8000, 0x386aa000, -0x386ac000, 0x386ae000, 0x386b0000, 0x386b2000, 0x386b4000, 0x386b6000, -0x386b8000, 0x386ba000, 0x386bc000, 0x386be000, 0x386c0000, 0x386c2000, -0x386c4000, 0x386c6000, 0x386c8000, 0x386ca000, 0x386cc000, 0x386ce000, -0x386d0000, 0x386d2000, 0x386d4000, 0x386d6000, 0x386d8000, 0x386da000, -0x386dc000, 0x386de000, 0x386e0000, 0x386e2000, 0x386e4000, 0x386e6000, -0x386e8000, 0x386ea000, 0x386ec000, 0x386ee000, 0x386f0000, 0x386f2000, -0x386f4000, 0x386f6000, 0x386f8000, 0x386fa000, 0x386fc000, 0x386fe000, -0x38700000, 0x38702000, 0x38704000, 0x38706000, 0x38708000, 0x3870a000, -0x3870c000, 0x3870e000, 0x38710000, 0x38712000, 0x38714000, 0x38716000, -0x38718000, 0x3871a000, 0x3871c000, 0x3871e000, 0x38720000, 0x38722000, -0x38724000, 0x38726000, 0x38728000, 0x3872a000, 0x3872c000, 0x3872e000, -0x38730000, 0x38732000, 0x38734000, 0x38736000, 0x38738000, 0x3873a000, -0x3873c000, 0x3873e000, 0x38740000, 0x38742000, 0x38744000, 0x38746000, -0x38748000, 0x3874a000, 0x3874c000, 0x3874e000, 0x38750000, 0x38752000, -0x38754000, 0x38756000, 0x38758000, 0x3875a000, 0x3875c000, 0x3875e000, -0x38760000, 0x38762000, 0x38764000, 0x38766000, 0x38768000, 0x3876a000, -0x3876c000, 0x3876e000, 0x38770000, 0x38772000, 0x38774000, 0x38776000, -0x38778000, 0x3877a000, 0x3877c000, 0x3877e000, 0x38780000, 0x38782000, -0x38784000, 0x38786000, 0x38788000, 0x3878a000, 0x3878c000, 0x3878e000, -0x38790000, 0x38792000, 0x38794000, 0x38796000, 0x38798000, 0x3879a000, -0x3879c000, 0x3879e000, 0x387a0000, 0x387a2000, 0x387a4000, 0x387a6000, -0x387a8000, 0x387aa000, 0x387ac000, 0x387ae000, 0x387b0000, 0x387b2000, -0x387b4000, 0x387b6000, 0x387b8000, 0x387ba000, 0x387bc000, 0x387be000, -0x387c0000, 0x387c2000, 0x387c4000, 0x387c6000, 0x387c8000, 0x387ca000, -0x387cc000, 0x387ce000, 0x387d0000, 0x387d2000, 0x387d4000, 0x387d6000, -0x387d8000, 0x387da000, 0x387dc000, 0x387de000, 0x387e0000, 0x387e2000, -0x387e4000, 0x387e6000, 0x387e8000, 0x387ea000, 0x387ec000, 0x387ee000, -0x387f0000, 0x387f2000, 0x387f4000, 0x387f6000, 0x387f8000, 0x387fa000, -0x387fc000, 0x387fe000 -}; - -static uint16_t m__offset[64] = { -0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0000, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, -0x0400, 0x0400, 0x0400, 0x0400 -}; - -static uint32_t m__exponent[64] = { -0x00000000, 0x00800000, 0x01000000, 0x01800000, 0x02000000, 0x02800000, -0x03000000, 0x03800000, 0x04000000, 0x04800000, 0x05000000, 0x05800000, -0x06000000, 0x06800000, 0x07000000, 0x07800000, 0x08000000, 0x08800000, -0x09000000, 0x09800000, 0x0a000000, 0x0a800000, 0x0b000000, 0x0b800000, -0x0c000000, 0x0c800000, 0x0d000000, 0x0d800000, 0x0e000000, 0x0e800000, -0x0f000000, 0x47800000, 0x80000000, 0x80800000, 0x81000000, 0x81800000, -0x82000000, 0x82800000, 0x83000000, 0x83800000, 0x84000000, 0x84800000, -0x85000000, 0x85800000, 0x86000000, 0x86800000, 0x87000000, 0x87800000, -0x88000000, 0x88800000, 0x89000000, 0x89800000, 0x8a000000, 0x8a800000, -0x8b000000, 0x8b800000, 0x8c000000, 0x8c800000, 0x8d000000, 0x8d800000, -0x8e000000, 0x8e800000, 0x8f000000, 0xc7800000 -}; - -static uint16_t m__base[512] = { -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, -0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x0c00, 0x1000, 0x1400, 0x1800, 0x1c00, -0x2000, 0x2400, 0x2800, 0x2c00, 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x4400, -0x4800, 0x4c00, 0x5000, 0x5400, 0x5800, 0x5c00, 0x6000, 0x6400, 0x6800, 0x6c00, -0x7000, 0x7400, 0x7800, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, -0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, -0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8001, -0x8002, 0x8004, 0x8008, 0x8010, 0x8020, 0x8040, 0x8080, 0x8100, 0x8200, 0x8400, -0x8800, 0x8c00, 0x9000, 0x9400, 0x9800, 0x9c00, 0xa000, 0xa400, 0xa800, 0xac00, -0xb000, 0xb400, 0xb800, 0xbc00, 0xc000, 0xc400, 0xc800, 0xcc00, 0xd000, 0xd400, -0xd800, 0xdc00, 0xe000, 0xe400, 0xe800, 0xec00, 0xf000, 0xf400, 0xf800, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, -0xfc00, 0xfc00 -}; - -static uint8_t m__shift[512] = { -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x17, -0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, -0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, -0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0d, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, -0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, -0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, -0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, -0x18, 0x18, 0x18, 0x18, 0x0d -}; - -MIAPI float m_half2float(uint16_t h) -{ - union { - float flt; - uint32_t num; - } out; - - int n = h >> 10; - - out.num = m__mantissa[ (h & 0x3ff) + m__offset[n]] + m__exponent[n]; - return out.flt; -} - -MIAPI uint16_t m_float2half(float flt) -{ - union { - float flt; - uint32_t num; - } in; - - uint32_t n, j; - - in.flt = flt; - n = in.num; - j = (n >> 23) & 0x1ff; - - return (uint16_t) ((uint32_t) m__base[j] + ((n & 0x007fffff) >> m__shift[j])); -} - -MIAPI void m_image_create(struct m_image *image, char type, int width, int height, int comp) -{ - int size = width * height * comp; - assert(size > 0); - - /* already allocated */ - if (image->data != 0 && type == image->type && width == image->width && height == image->height && comp == image->comp) - return; - - M_SAFE_FREE(image->data); - - switch (type) { - case M_BYTE: - case M_UBYTE: - image->data = malloc(size * sizeof(uint8_t)); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - image->data = malloc(size * sizeof(uint16_t)); - break; - case M_INT: - case M_UINT: - image->data = malloc(size * sizeof(uint32_t)); - break; - case M_FLOAT: - image->data = malloc(size * sizeof(float)); - break; - default: - assert(0); - return; - } - - image->type = type; - image->width = width; - image->height = height; - image->comp = comp; - image->size = size; -} - -MIAPI void m_image_destroy(struct m_image *image) -{ - M_SAFE_FREE(image->data); - memset(image, 0, sizeof(struct m_image)); -} - -MIAPI void m_image_copy(struct m_image *dest, const struct m_image *src) -{ - m_image_create(dest, src->type, src->width, src->height, src->comp); - - switch (dest->type) { - case M_BYTE: - case M_UBYTE: - memcpy(dest->data, src->data, dest->size*sizeof(char)); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - memcpy(dest->data, src->data, dest->size*sizeof(short)); - break; - case M_INT: - case M_UINT: - memcpy(dest->data, src->data, dest->size*sizeof(int)); - break; - case M_FLOAT: - memcpy(dest->data, src->data, dest->size*sizeof(float)); - break; - default: - assert(0); - break; - } -} - -MIAPI void m_image_copy_sub_image(struct m_image *dest, const struct m_image *src, int x, int y, int w, int h) -{ - #define M_COPY_SUBI(T)\ - {\ - T *sData = (T *)src->data + (miny * src->width + minx) * comp;\ - T *dData = (T *)dest->data;\ - int y;\ - for (y = miny; y <= maxy; y++) {\ - memcpy(dData, sData, dstep * sizeof(T));\ - dData += dstep;\ - sData += sstep;\ - }\ - } - - int comp = src->comp; - int minx = M_MAX(0, x); - int miny = M_MAX(0, y); - int maxx = M_CLAMP(x + w - 1, 0, src->width - 1); - int maxy = M_CLAMP(y + h - 1, 0, src->height - 1); - int dwidth = 1 + maxx - minx; - int dheight = 1 + maxy - miny; - int sstep = src->width * comp; - int dstep = dwidth * comp; - - m_image_create(dest, src->type, dwidth, dheight, src->comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_COPY_SUBI(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_COPY_SUBI(short); - break; - case M_INT: - case M_UINT: - M_COPY_SUBI(int); - break; - case M_FLOAT: - M_COPY_SUBI(float); - break; - default: - assert(0); - break; - } - - #undef M_COPY_SUBI -} - -MIAPI void m_image_ubyte_to_float(struct m_image *dest, const struct m_image *src) -{ - uint8_t *src_data; - float *dest_data; - float ubyte_div = 1.0f / 255.0f; - int i; - - m_image_create(dest, M_FLOAT, src->width, src->height, src->comp); - - src_data = (uint8_t *)src->data; - dest_data = (float *)dest->data; - for (i = 0; i < src->size; i++) - dest_data[i] = (float)src_data[i] * ubyte_div; -} - -MIAPI void m_image_ushort_to_float(struct m_image *dest, const struct m_image *src) -{ - uint16_t *src_data; - float *dest_data; - float ushort_div = 1.0f / (float)65535; - int i; - - m_image_create(dest, M_FLOAT, src->width, src->height, src->comp); - - src_data = (uint16_t *)src->data; - dest_data = (float *)dest->data; - for (i = 0; i < src->size; i++) - dest_data[i] = (float)src_data[i] * ushort_div; -} - -MIAPI void m_image_half_to_float(struct m_image *dest, const struct m_image *src) -{ - uint16_t *src_data; - float *dest_data; - int i; - - m_image_create(dest, M_FLOAT, src->width, src->height, src->comp); - - src_data = (uint16_t *)src->data; - dest_data = (float *)dest->data; - for (i = 0; i < src->size; i++) - dest_data[i] = m_half2float(src_data[i]); -} - -MIAPI void m_image_float_to_ubyte(struct m_image *dest, const struct m_image *src) -{ - float *src_data; - uint8_t *dest_data; - int i; - - m_image_create(dest, M_UBYTE, src->width, src->height, src->comp); - - src_data = (float *)src->data; - dest_data = (uint8_t *)dest->data; - for (i = 0; i < src->size; i++) { - int x = (int)(src_data[i] * 255); - dest_data[i] = (uint8_t)M_CLAMP(x, 0, 255); - } -} - -MIAPI void m_image_float_to_ushort(struct m_image *dest, const struct m_image *src) -{ - float *src_data; - uint16_t *dest_data; - int i; - - m_image_create(dest, M_USHORT, src->width, src->height, src->comp); - - src_data = (float *)src->data; - dest_data = (uint16_t *)dest->data; - for (i = 0; i < src->size; i++) { - int x = (int)(src_data[i] * 65535); - dest_data[i] = (uint16_t)M_CLAMP(x, 0, 65535); - } -} - -MIAPI void m_image_float_to_half(struct m_image *dest, const struct m_image *src) -{ - float *src_data; - uint16_t *dest_data; - int i; - - m_image_create(dest, M_USHORT, src->width, src->height, src->comp); - - src_data = (float *)src->data; - dest_data = (uint16_t *)dest->data; - for (i = 0; i < src->size; i++) - dest_data[i] = m_float2half(src_data[i]); -} - -MIAPI void m_image_extract_component(struct m_image *dest, const struct m_image *src, int c) -{ - #define M_EXTRACT(T)\ - {\ - T *dest_pixel = (T *)dest->data;\ - T *src_pixel = (T *)src->data;\ - for (i = 0; i < size; i += comp) {\ - (*dest_pixel) = src_pixel[c];\ - dest_pixel++;\ - src_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int size = src->size; - int i; - - if(c >= src->comp) { - assert(0); - return; - } - - m_image_create(dest, src->type, width, height, 1); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_EXTRACT(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_EXTRACT(short); - break; - case M_INT: - case M_UINT: - M_EXTRACT(int); - break; - case M_FLOAT: - M_EXTRACT(float); - break; - default: - assert(0); - break; - } - - #undef M_EXTRACT -} - -MIAPI void m_image_reframe(struct m_image *dest, const struct m_image *src, int left, int top, int right, int bottom) -{ - #define M_REFRAME(T)\ - {\ - T *src_data;\ - T *src_pixel;\ - T *dest_pixel;\ - int c;\ - int x, y;\ - m_image_create(dest, src->type, width2, height2, comp);\ - src_data = (T *)src->data;\ - dest_pixel = (T *)dest->data;\ - for (y = 0; y < height2; y++) {\ - T *src_y;\ - int ys = y - top;\ - src_y = src_data + M_CLAMP(ys, 0, hm1) * width * comp;\ - for (x = 0; x < width2; x++) {\ - int xs = x - left;\ - src_pixel = src_y + M_CLAMP(xs, 0, wm1) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - }\ - } - - if(left != 0 || top != 0 || right != 0 || bottom != 0) { - - int comp = src->comp; - int width = src->width; - int height = src->height; - int width2 = width + left + right; - int height2 = height + top + bottom; - int wm1 = width - 1; - int hm1 = height - 1; - - if(width2 > 0 && height2 > 0) { - - switch(src->type) { - case M_BYTE: - case M_UBYTE: - M_REFRAME(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_REFRAME(short); - break; - case M_INT: - case M_UINT: - M_REFRAME(int); - break; - case M_FLOAT: - M_REFRAME(float); - break; - default: - assert(0); - break; - } - } - else { - assert(0); - } - } - else { - m_image_copy(dest, src); - } - - #undef M_REFRAME -} - -MIAPI void m_image_rotate_left(struct m_image *dest, const struct m_image *src) -{ - #define M_ROTATE_L(T)\ - {\ - T *src_data = (T *)src->data;\ - T *dest_pixel = (T *)dest->data;\ - for (y = 0; y < width; y++)\ - for (x = 0; x < height; x++) {\ - T *src_pixel = src_data + (x * width + (width - 1 - y)) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - m_image_create(dest, src->type, height, width, comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_ROTATE_L(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_ROTATE_L(short); - break; - case M_INT: - case M_UINT: - M_ROTATE_L(int); - break; - case M_FLOAT: - M_ROTATE_L(float); - break; - default: - assert(0); - break; - } - - #undef M_ROTATE_L -} - -MIAPI void m_image_rotate_right(struct m_image *dest, const struct m_image *src) -{ - #define M_ROTATE_R(T)\ - {\ - T *src_data = (T *)src->data;\ - T *dest_pixel = (T *)dest->data;\ - for (y = 0; y < width; y++)\ - for (x = 0; x < height; x++) {\ - T *src_pixel = src_data + ((height - 1 - x) * width + y) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - m_image_create(dest, src->type, height, width, comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_ROTATE_R(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_ROTATE_R(short); - break; - case M_INT: - case M_UINT: - M_ROTATE_R(int); - break; - case M_FLOAT: - M_ROTATE_R(float); - break; - default: - assert(0); - break; - } - - #undef M_ROTATE_R -} - -MIAPI void m_image_rotate_180(struct m_image *dest, const struct m_image *src) -{ - #define M_ROTATE_180(T)\ - {\ - T *src_data = (T *)src->data;\ - T *dest_pixel = (T *)dest->data;\ - for (y = 0; y < height; y++)\ - for (x = 0; x < width; x++) {\ - T *src_pixel = src_data + ((height - 1 - y) * width + (width - 1 - x)) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - m_image_create(dest, src->type, width, height, comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_ROTATE_180(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_ROTATE_180(short); - break; - case M_INT: - case M_UINT: - M_ROTATE_180(int); - break; - case M_FLOAT: - M_ROTATE_180(float); - break; - default: - assert(0); - break; - } - - #undef M_ROTATE_180 -} - -MIAPI void m_image_mirror_x(struct m_image *dest, const struct m_image *src) -{ - #define M_MIRROR_X(T)\ - {\ - T *src_data = (T *)src->data;\ - T *dest_pixel = (T *)dest->data;\ - for (y = 0; y < height; y++)\ - for (x = 0; x < width; x++) {\ - T *src_pixel = src_data + (y * width + (width - 1 - x)) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - m_image_create(dest, src->type, width, height, comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_MIRROR_X(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_MIRROR_X(short); - break; - case M_INT: - case M_UINT: - M_MIRROR_X(int); - break; - case M_FLOAT: - M_MIRROR_X(float); - break; - default: - assert(0); - break; - } - - #undef M_MIRROR_X -} - -MIAPI void m_image_mirror_y(struct m_image *dest, const struct m_image *src) -{ - #define M_MIRROR_Y(T)\ - {\ - T *src_data = (T *)src->data;\ - T *dest_pixel = (T *)dest->data;\ - for (y = 0; y < height; y++)\ - for (x = 0; x < width; x++) {\ - T *src_pixel = src_data + ((height - 1 - y) * width + x) * comp;\ - for (c = 0; c < comp; c++)\ - dest_pixel[c] = src_pixel[c];\ - dest_pixel += comp;\ - }\ - } - - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - m_image_create(dest, src->type, width, height, comp); - - switch(src->type) - { - case M_BYTE: - case M_UBYTE: - M_MIRROR_Y(char); - break; - case M_SHORT: - case M_USHORT: - case M_HALF: - M_MIRROR_Y(short); - break; - case M_INT: - case M_UINT: - M_MIRROR_Y(int); - break; - case M_FLOAT: - M_MIRROR_Y(float); - break; - default: - assert(0); - break; - } - - #undef M_MIRROR_Y -} - -MIAPI void m_image_premultiply(struct m_image *dest, const struct m_image *src) -{ - float *dest_p, *src_p; - int i; - - assert(src->size > 0 && src->type == M_FLOAT && src->comp == 4); - - m_image_create(dest, M_FLOAT, src->width, src->height, 4); - dest_p = (float *)dest->data; - src_p = (float *)src->data; - - for (i = 0; i < src->size; i+=4) { - dest_p[0] = src_p[0] * src_p[3]; - dest_p[1] = src_p[1] * src_p[3]; - dest_p[2] = src_p[2] * src_p[3]; - dest_p[3] = src_p[3]; - dest_p += 4; - src_p += 4; - } -} - -MIAPI void m_image_unpremultiply(struct m_image *dest, const struct m_image *src) -{ - float *dest_p, *src_p; - int i; - - assert(src->size > 0 && src->type == M_FLOAT && src->comp == 4); - - m_image_create(dest, M_FLOAT, src->width, src->height, 4); - dest_p = (float *)dest->data; - src_p = (float *)src->data; - - for (i = 0; i < src->size; i+=4) { - float x = 1.0 / src_p[3]; - dest_p[0] = src_p[0] * x; - dest_p[1] = src_p[1] * x; - dest_p[2] = src_p[2] * x; - dest_p[3] = src_p[3]; - dest_p += 4; - src_p += 4; - } -} - -MIAPI void m_image_summed_area(struct m_image *dest, const struct m_image *src) -{ - float *src_pixel; - float *dest_pixel; - int width = src->width; - int height = src->height; - int comp = src->comp; - int x, y, c; - - assert(src->size > 0 && src->type == M_FLOAT); - if (dest != src) - m_image_copy(dest, src); - - /* horiz sum */ - dest_pixel = (float *)dest->data; - for (y = 0; y < height; y++) { - float *prev_pixel = dest_pixel; - dest_pixel += comp; - - for (x = 1; x < width; x++) { - for (c = 0; c < comp; c++) - dest_pixel[c] += prev_pixel[c]; - - prev_pixel = dest_pixel; - dest_pixel += comp; - } - } - - /* vertical sum */ - src_pixel = (float *)dest->data; - dest_pixel = (float *)dest->data + width * comp; - for (y = 1; y < height; y++) - for (x = 0; x < width; x++) { - - for (c = 0; c < comp; c++) - dest_pixel[c] += src_pixel[c]; - - src_pixel += comp; - dest_pixel += comp; - } -} - -MIAPI void m_image_convolution_h(struct m_image *dest, const struct m_image *src, float *kernel, int size) -{ - struct m_image copy = M_IMAGE_IDENTITY(); - float *src_data; - float *dest_data; - int width = src->width; - int height = src->height; - int comp = src->comp; - int radius = (size - 1) / 2; - int y, ystep, ystepc; - - assert(src->size > 0 && src->type == M_FLOAT); - - /* create source and destination images */ - m_image_reframe(©, src, radius, 0, radius, 0); /* apply clamped margin */ - m_image_create(dest, M_FLOAT, width, height, comp); - - /* clear */ - memset(dest->data, 0, dest->size * sizeof(float)); - - src_data = (float *)copy.data; - dest_data = (float *)dest->data; - ystep = width * comp; - ystepc = copy.width * comp; - - #pragma omp parallel for schedule(dynamic, 8) - for (y=0; y<height; y++) { - - float *dest_pixel = dest_data + y * ystep; - float *src_pixel_y = src_data + y * ystepc; - int x; - - for (x = 0; x < width; x++) { - - float *src_pixel; - int i, k; - - src_pixel = src_pixel_y + (x * comp); - - /* apply kernel */ - for (k = 0; k < size; k++) { - float v = kernel[k]; - for (i = 0; i < comp; i++) - dest_pixel[i] += (*src_pixel++) * v; - } - - dest_pixel += comp; - } - } - - m_image_destroy(©); -} - -MIAPI void m_image_convolution_v(struct m_image *dest, const struct m_image *src, float *kernel, int size) -{ - struct m_image copy = M_IMAGE_IDENTITY(); - float *src_data; - float *dest_data; - int width = src->width; - int height = src->height; - int comp = src->comp; - int radius = (size - 1) / 2; - int y, ystep; - - assert(src->size > 0 && src->type == M_FLOAT); - - /* create source and destination images */ - m_image_reframe(©, src, 0, radius, 0, radius); /* apply clamped margin */ - m_image_create(dest, M_FLOAT, width, height, comp); - - /* clear */ - memset(dest->data, 0, dest->size * sizeof(float)); - - src_data = (float *)copy.data; - dest_data = (float *)dest->data; - ystep = width * comp; - - #pragma omp parallel for schedule(dynamic, 8) - for (y=0; y<height; y++) { - - float *dest_pixel = dest_data + y * ystep; - int x; - - for (x = 0; x < width; x++) { - - float *src_pixel; - int i, k; - - src_pixel = src_data + (y * width + x) * comp; - - /* apply kernel */ - for (k = 0; k < size; k++) { - float v = kernel[k]; - for (i = 0; i < comp; i++) - dest_pixel[i] += src_pixel[i] * v; - src_pixel += ystep; - } - - dest_pixel += comp; - } - } - - m_image_destroy(©); -} - -MIAPI void m_image_gaussian_blur(struct m_image *dest, const struct m_image *src, int dx, int dy) -{ - struct m_image tmp = M_IMAGE_IDENTITY(); - float *kernelx = NULL, *kernely = NULL; - int kernelx_size = dx * 2 + 1; - int kernely_size = dy * 2 + 1; - - assert(src->size > 0 && src->type == M_FLOAT); - - /* exit */ - if (dx == 0 && dy == 0) { - m_image_copy(dest, src); - return; - } - - /* x blur */ - if (dx > 0) { - kernelx = (float *)malloc(kernelx_size * sizeof(float)); - m_gaussian_kernel(kernelx, kernelx_size); - if (dy > 0) - m_image_convolution_h(&tmp, src, kernelx, kernelx_size); - else - m_image_convolution_h(dest, src, kernelx, kernelx_size); - } - - /* y blur */ - if (dy > 0) { - kernely = (float *)malloc(kernely_size * sizeof(float)); - m_gaussian_kernel(kernely, kernely_size); - if (dx > 0) - m_image_convolution_v(dest, &tmp, kernely, kernely_size); - else - m_image_convolution_v(dest, src, kernely, kernely_size); - } - - m_image_destroy(&tmp); - if(kernely) free(kernely); - if(kernelx) free(kernelx); -} - -MIAPI void m_image_grey(struct m_image *dest, const struct m_image *src) -{ - float *src_pixel; - float *dest_pixel; - int size = src->size; - int i, c = src->comp; - - assert(src->size > 0 && src->type == M_FLOAT && src->comp > 2); - - m_image_create(dest, M_FLOAT, src->width, src->height, 1); - - src_pixel = (float *)src->data; - dest_pixel = (float *)dest->data; - - for (i = 0; i < size; i+=c) { - float v = src_pixel[0] * 0.3f + src_pixel[1] * 0.5f + src_pixel[2] * 0.2f; - *dest_pixel = v; - dest_pixel++; - src_pixel+=c; - } -} - -MIAPI void m_image_max(struct m_image *dest, const struct m_image *src) -{ - float *src_pixel; - float *dest_pixel; - int size = src->size; - int i, j, c = src->comp; - - assert(src->size > 0 && src->type == M_FLOAT); - - m_image_create(dest, M_FLOAT, src->width, src->height, 1); - - src_pixel = (float *)src->data; - dest_pixel = (float *)dest->data; - - for (i = 0; i < size; i+=c) { - float v = src_pixel[0]; - for (j = 1; j < c; j++) - v = M_MAX(v, src_pixel[j]); - *dest_pixel = v; - dest_pixel++; - src_pixel+=c; - } -} - -MIAPI void m_image_max_abs(struct m_image *dest, const struct m_image *src) -{ - float *src_pixel; - float *dest_pixel; - int size = src->size; - int i, j, c = src->comp; - - assert(src->size > 0 && src->type == M_FLOAT); - - m_image_create(dest, M_FLOAT, src->width, src->height, 1); - - src_pixel = (float *)src->data; - dest_pixel = (float *)dest->data; - - for (i = 0; i < size; i+=c) { - float v = fabsf(src_pixel[0]); - for (j = 1; j < c; j++) - v = M_MAX(v, fabsf(src_pixel[j])); - *dest_pixel = v; - dest_pixel++; - src_pixel+=c; - } -} - -static float m__convolve_pixel(float *data, int width, float *kernel) -{ - float sum = 0; int i, j; - for (i = 0; i < 3; i++) { - float *pixel = data + width * i; - for (j = 0; j < 3; j++) { - sum += (*pixel) * (*kernel); - pixel++; - kernel++; - } - } - return sum; -} - -MIAPI void m_image_sobel(struct m_image *dest, const struct m_image *src) -{ - struct m_image copy = M_IMAGE_IDENTITY(); - float ky[9] = {-1, -2, -1, 0, 0, 0, 1, 2, 1}; - float kx[9] = {-1, 0, 1, -2, 0, 2, -1, 0, 1}; - float *src_pixel; - float *dest_pixel; - int width = src->width; - int height = src->height; - int w2 = width + 2; - int x, y; - - assert(src->size > 0 && src->type == M_FLOAT && src->comp == 1); - - /* create source and destination images */ - m_image_reframe(©, src, 1, 1, 1, 1); /* apply clamped margin */ - m_image_create(dest, M_FLOAT, width, height, 2); - - src_pixel = (float *)copy.data; - dest_pixel = (float *)dest->data; - - for (y = 0; y < height; y++) { - for (x = 0; x < width; x++) { - dest_pixel[0] = m__convolve_pixel(src_pixel, w2, kx); - dest_pixel[1] = m__convolve_pixel(src_pixel, w2, ky); - src_pixel++; - dest_pixel += 2; - } - src_pixel += 2; - } - - m_image_destroy(©); -} - -MIAPI void m_image_harris(struct m_image *dest, const struct m_image *src, int radius) -{ - struct m_image tmp1 = M_IMAGE_IDENTITY(); - struct m_image tmp2 = M_IMAGE_IDENTITY(); - - /* sobel */ - m_image_sobel(&tmp1, src); - - /* sst */ - m_image_create(&tmp2, M_FLOAT, src->width, src->height, 3); - m_sst((float *)tmp2.data, (float *)tmp1.data, src->width * src->height); - - /* blur */ - m_image_copy(&tmp1, &tmp2); - m_image_gaussian_blur(&tmp2, &tmp1, radius, radius); - - /* harris response */ - m_image_create(dest, M_FLOAT, src->width, src->height, 1); - m_harris_response((float *)dest->data, (float *)tmp2.data, src->width * src->height); - - m_image_destroy(&tmp1); - m_image_destroy(&tmp2); -} - -#define M_WRITE_PIXEL(dest, x0, y0, v) {*(dest + w * (y0) + (x0)) = v;} -#define M_PUSH_PIXEL(x2, y2) if((stack_i+3) < stack_size && m__test_pixel(data, w, h, x2, y2, ref)) {\ - stack_i+=2;\ - stack[stack_i] = (unsigned short)(x2);\ - stack[stack_i+1] = (unsigned short)(y2);\ - M_WRITE_PIXEL(data, x2, y2, value);\ -} - -static int m__test_pixel(unsigned char *src, int w, int h, int x, int y, unsigned char ref) -{ - if (! (x >= 0 && x < w && y >= 0 && y < h)) - return 0; - return (*(src + w * y + x) == ref); -} - -MIAPI int m_image_floodfill_4x(struct m_image *dest, int x, int y, unsigned char ref, unsigned char value, unsigned short *stack, int stack_size) -{ - unsigned char *data = (unsigned char *)dest->data; - int w = dest->width; - int h = dest->height; - int stack_i = 0; - - assert(dest->size > 0 && dest->type == M_UBYTE); - - if(! m__test_pixel(data, w, h, x, y, ref)) - return 0; - - stack[0] = (unsigned short)x; - stack[1] = (unsigned short)y; - M_WRITE_PIXEL(data, x, y, value); - - while (stack_i >= 0) { - - x = stack[stack_i]; - y = stack[stack_i+1]; - stack_i-=2; - - M_PUSH_PIXEL(x + 1, y) - M_PUSH_PIXEL(x - 1, y) - M_PUSH_PIXEL(x, y + 1) - M_PUSH_PIXEL(x, y - 1) - } - - return 1; -} - -MIAPI int m_image_floodfill_8x(struct m_image *dest, int x, int y, unsigned char ref, unsigned char value, unsigned short *stack, int stack_size) -{ - unsigned char *data = (unsigned char *)dest->data; - int w = dest->width; - int h = dest->height; - int stack_i = 0; - - assert(dest->size > 0 && dest->type == M_UBYTE); - - if(! m__test_pixel(data, w, h, x, y, ref)) - return 0; - - stack[0] = (unsigned short)x; - stack[1] = (unsigned short)y; - M_WRITE_PIXEL(data, x, y, value); - - while (stack_i >= 0) { - - x = stack[stack_i]; - y = stack[stack_i+1]; - stack_i-=2; - - M_PUSH_PIXEL(x + 1, y) - M_PUSH_PIXEL(x - 1, y) - M_PUSH_PIXEL(x, y + 1) - M_PUSH_PIXEL(x, y - 1) - M_PUSH_PIXEL(x + 1, y + 1) - M_PUSH_PIXEL(x + 1, y - 1) - M_PUSH_PIXEL(x - 1, y + 1) - M_PUSH_PIXEL(x - 1, y - 1) - } - - return 1; -} - -#undef M_WRITE_PIXEL -#undef M_PUSH_PIXEL - -static void m__dilate_erode(struct m_image *dest, const struct m_image *src, unsigned char ref, unsigned char value, int copy) -{ - unsigned char *src_data = (unsigned char *)src->data; - unsigned char *src_pixel = src_data; - unsigned char *dest_pixel; - int w = src->width; - int h = src->height; - int y; - - assert(src->size > 0 && src->type == M_UBYTE); - - m_image_create(dest, M_UBYTE, w, h, 1); - dest_pixel = (unsigned char *)dest->data; - if (copy) - memcpy(dest_pixel, src_data, dest->size * sizeof(char)); - else - memset(dest_pixel, 0, dest->size * sizeof(char)); - - for (y=0; y<h; y++) { - - int x; - for (x=0; x<w; x++) { - - unsigned char c1, c2, c3, c4, c5; - c1 = *src_pixel; - - if (c1 == ref) { - c2 = x > 0 ? *(src_data + y * w + (x - 1)) : c1; - c3 = y > 0 ? *(src_data + (y - 1) * w + x) : c1; - c4 = (x + 1) < w ? *(src_data + y * w + x + 1) : c1; - c5 = (y + 1) < h ? *(src_data + (y + 1) * w + x) : c1; - if (c2 != c1 || c3 != c1 || c4 != c1 || c5 != c1) - *dest_pixel = value; - } - - src_pixel++; - dest_pixel++; - } - } -} - -MIAPI void m_image_dilate(struct m_image *dest, const struct m_image *src) -{ - m__dilate_erode(dest, src, 0, 255, 1); -} - -MIAPI void m_image_erode(struct m_image *dest, const struct m_image *src) -{ - m__dilate_erode(dest, src, 255, 0, 1); -} - -MIAPI void m_image_edge_4x(struct m_image *dest, const struct m_image *src, unsigned char ref) -{ - m__dilate_erode(dest, src, ref, 255, 0); -} - -/* Following C code from the article - "Efficient Binary Image Thinning using Neighborhood Maps" - by Joseph M. Cychosz, in "Graphics Gems IV", Academic Press, 1994 - Thins the image using Rosenfeld's parallel thinning algorithm. -*/ - -/* Direction m__masks: - N S W E -*/ -static int m__masks[] = {0200, 0002, 0040, 0010}; - -/* True if pixel neighbor map indicates the pixel is 8-simple and - not an end point and thus can be deleted. The neighborhood - map is defined as an integer of bits abcdefghi with a non-zero - bit representing a non-zero pixel. The bit assignment for the - neighborhood is: - - a b c - d e f - g h i -*/ -static unsigned char m__delete_map[512] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - -MIAPI void m_image_thin(struct m_image *dest) -{ - unsigned char *data; /* image data */ - unsigned char ** ip; /* scanline pointers, ip[y][x] */ - unsigned char * qb; /* Neighborhood maps of previous scanline */ - int xsize, ysize; /* Image resolution */ - int x, y; /* Pixel location */ - int i; /* Pass index */ - int pc = 0; /* Pass count */ - int count = 1; /* Deleted pixel count */ - int p, q; /* Neighborhood maps of adjacent cells */ - int m; /* Deletion direction mask */ - - assert(dest->size > 0 && dest->type == M_UBYTE); - - data = (unsigned char *)dest->data; - xsize = dest->width; - ysize = dest->height; - - qb = (unsigned char *)malloc(xsize * sizeof(char)); - qb[xsize-1] = 0; /* Used for lower-right pixel */ - - /* alloc scanline pointers */ - ip = (unsigned char **)malloc(sizeof(void *) * ysize); - - /* set scanline pointers */ - for (y=0; y<ysize; y++) { - ip[y] = data + y*xsize; - } - - while (count) { /* Scan image while deletions */ - - pc++; - count = 0; - - for (i=0; i<4; i++) { - - m = m__masks[i]; - - /* Build initial previous scan buffer */ - p = ip[0][0] != 0; - for (x=0; x<xsize-1; x++) { - p = ((p<<1)&0006) | (ip[0][x+1] != 0); - qb[x] = (unsigned char)p; - } - - /* Scan image for pixel deletion candidates */ - for (y=0; y<ysize-1; y++) { - q = qb[0]; - p = ((q<<3)&0110) | (ip[y+1][0] != 0); - - for (x=0; x<xsize-1; x++) { - q = qb[x]; - p = ((p<<1)&0666) | ((q<<3)&0110) | (ip[y+1][x+1] != 0); - qb[x] = (unsigned char)p; - - if (((p&m) == 0) && m__delete_map[p]) { - if (ip[y][x] != 0) { - count++; - ip[y][x] = 0; - } - } - } - - /* Process right edge pixel */ - p = (p<<1)&0666; - if ((p&m) == 0 && m__delete_map[p]) { - if (ip[y][xsize-1] != 0) { - count++; - ip[y][xsize-1] = 0; - } - } - } - - /* Process bottom scan line */ - for (x=0; x<xsize; x++) { - q = qb[x]; - p = ((p<<1)&0666) | ((q<<3)&0110); - if ((p&m) == 0 && m__delete_map[p]) { - if (ip[ysize-1][x] != 0) { - count++; - ip[ysize-1][x] = 0; - } - } - } - } - } - - free(qb); - free(ip); -} - -MIAPI void m_image_non_max_supp(struct m_image *dest, const struct m_image *src, int radius, float threshold) -{ - float *src_data, *dest_data; - float *src_pixel, *dest_pixel; - int width = src->width; - int height = src->height; - int x, y; - - assert(src->size > 0 && src->type == M_FLOAT && src->comp == 1); - - m_image_copy(dest, src); - - src_data = (float *)src->data; - dest_data = (float *)dest->data; - src_pixel = src_data; - dest_pixel = dest_data; - - for (y = 0; y < height; y++) - for (x = 0; x < width; x++) { - - int minx, miny, maxx, maxy, xx, yy; - - if (*src_pixel < threshold) { - *dest_pixel = 0; - goto end; - } - - minx = M_MAX(0, x - radius); - miny = M_MAX(0, y - radius); - maxx = M_MIN(width - 1, x + radius); - maxy = M_MIN(height - 1, y + radius); - - for (yy = miny; yy <= maxy; yy++) - for (xx = minx; xx <= maxx; xx++) { - - float *src_pixel2 = src_data + yy*width + xx; - if (*src_pixel2 > *src_pixel) { - *dest_pixel = 0; - goto end; - } - } - - end: - src_pixel++; - dest_pixel++; - } -} - -MIAPI int m_image_corner_harris(const struct m_image *src, int margin, int radius, float threshold, int *corners, int max_count) -{ - struct m_image harris = M_IMAGE_IDENTITY(); - struct m_image nms = M_IMAGE_IDENTITY(); - float *pixel; - int width = src->width; - int height = src->height; - int wm = width - margin; - int hm = height - margin; - int x, y, count; - - if (width <= (margin * 2) || height <= (margin * 2)) - return 0; - - m_image_harris(&harris, src, radius); - m_image_non_max_supp(&nms, &harris, radius, threshold); - - count = 0; - pixel = (float *)nms.data; - - for (y = 0; y < height; y++) - for (x = 0; x < width; x++) { - - if (count == max_count) - goto end; - - if ((*pixel) > 0 && x >= margin && y >= margin && x < wm && y < hm) { - corners[count*2] = x; - corners[count*2+1] = y; - count++; - } - pixel++; - } - - end: - m_image_destroy(&nms); - m_image_destroy(&harris); - return count; -} - -MIAPI void m_image_sub_pixel(const struct m_image *src, float x, float y, float *result) -{ - float *colors0, *colors1, *colors2, *colors3; - float *src_data = (float *)src->data; - int width = src->width; - int height = src->height; - int comp = src->comp; - int c; - float fx, fy; - int wm = width - 1; - int hm = height - 1; - int ix, iy, ix2, iy2; - - ix = (int)x; - iy = (int)y; - fx = x - (float)ix; - fy = y - (float)iy; - fx = M_MAX(fx, 0); - fy = M_MAX(fy, 0); - - ix = M_CLAMP(ix, 0, wm); - iy = M_CLAMP(iy, 0, hm); - ix2 = ix + 1; - iy2 = iy + 1; - ix2 = M_MIN(ix2, wm); - iy2 = M_MIN(iy2, hm); - - colors0 = src_data + (width * iy + ix) * comp; - colors1 = src_data + (width * iy + ix2) * comp; - colors2 = src_data + (width * iy2 + ix) * comp; - colors3 = src_data + (width * iy2 + ix2) * comp; - - for(c = 0; c < comp; c++) { - float A = colors0[c] + (colors2[c] - colors0[c]) * fy; - float B = colors1[c] + (colors3[c] - colors1[c]) * fy; - result[c] = A + (B - A) * fx; - } -} - -/* slow TODO better */ -static void m__bilinear(struct m_image *dest, const struct m_image *src, float dx, float dy, float offset) -{ - float *dest_data = (float *)dest->data; - int width = dest->width; - int height = dest->height; - int comp = src->comp; - int y, ystep = width * comp; - - #pragma omp parallel for schedule(dynamic, 8) - for (y = 0; y < height; y++) { - float *dest_pixel = dest_data + y * ystep; int x; - for (x = 0; x < width; x++) { - m_image_sub_pixel(src, ((float)x + 0.5f) * dx + offset, ((float)y + 0.5f) * dy + offset, dest_pixel); - dest_pixel += comp; - } - } -} - -MIAPI void m_image_pyrdown(struct m_image *dest, const struct m_image *src) -{ - struct m_image tmp = M_IMAGE_IDENTITY(); - float *src_data; - float *dest_pixel; - int width = src->width; - int height = src->height; - int comp = src->comp; - int comp2 = comp * 2; - int ystep = width * comp * 2; - int w2 = width / 2; - int h2 = height / 2; - int x, y, i; - - m_image_gaussian_blur(&tmp, src, 1, 1); - m_image_create(dest, M_FLOAT, w2, h2, comp); - - src_data = (float *)tmp.data; - dest_pixel = (float *)dest->data; - - for (y = 0; y < h2; y++) { - float *src_pixel = src_data + y * ystep; - for (x = 0; x < w2; x++) { - for (i = 0; i < comp; i++) - dest_pixel[i] = src_pixel[i]; - dest_pixel += comp; - src_pixel += comp2; - } - } - - m_image_destroy(&tmp); -} - -MIAPI void m_image_resize(struct m_image *dest, const struct m_image *src, int new_width, int new_height) -{ - struct m_image tmp = M_IMAGE_IDENTITY(); - int width = src->width; - int height = src->height; - int comp = src->comp; - float rx = (float)width / (float)new_width; - float ry = (float)height / (float)new_height; - - assert(src->size > 0 && src->type == M_FLOAT); - m_image_create(dest, M_FLOAT, new_width, new_height, comp); - - if (new_width < width || new_height < height) { - float r = M_MAX(rx, ry); - int ir = (int)r - 1; - if (ir > 0) { - m_image_gaussian_blur(&tmp, src, ir, ir); - m__bilinear(dest, &tmp, rx, ry, -0.5f); - } - else { - m__bilinear(dest, src, rx, ry, -0.5f); - } - } - else { - m__bilinear(dest, src, rx, ry, -0.5f); - } - - m_image_destroy(&tmp); -} - -#endif /* M_IMAGE_IMPLEMENTATION */ diff --git a/3rdparty/bimg/3rdparty/nvtt/nvcore/nvcore.h b/3rdparty/bimg/3rdparty/nvtt/nvcore/nvcore.h index e2458e2710d..5bd5d7b71a4 100644 --- a/3rdparty/bimg/3rdparty/nvtt/nvcore/nvcore.h +++ b/3rdparty/bimg/3rdparty/nvtt/nvcore/nvcore.h @@ -1,4 +1,4 @@ -// This code is in the public domain -- Ignacio Castaño <castano@gmail.com> +// This code is in the public domain -- Ignacio Castaño <castano@gmail.com> #ifndef NV_CORE_H #define NV_CORE_H @@ -357,7 +357,7 @@ NV_COMPILER_CHECK(sizeof(uint32) == 4); # include "defsgnuclinux.h" # elif NV_OS_DARWIN || NV_OS_FREEBSD || NV_OS_OPENBSD # include "defsgnucdarwin.h" -# elif NV_OS_MINGW +# elif NV_OS_MINGW || (NV_CC_CLANG && NV_OS_WIN32) # include "defsgnucwin32.h" # elif NV_OS_CYGWIN # error "GCC: Cygwin not supported" diff --git a/3rdparty/bimg/3rdparty/nvtt/nvcore/posh.h b/3rdparty/bimg/3rdparty/nvtt/nvcore/posh.h index b597125f9ac..d9511743b45 100644 --- a/3rdparty/bimg/3rdparty/nvtt/nvcore/posh.h +++ b/3rdparty/bimg/3rdparty/nvtt/nvcore/posh.h @@ -258,9 +258,11 @@ LLVM: #endif #if defined _MSC_VER +#if !defined __clang__ # define POSH_COMPILER_STRING "Microsoft Visual C++" # define POSH_COMPILER_MSVC 1 #endif +#endif #if defined __SUNPRO_C # define POSH_COMPILER_STRING "Sun Pro" @@ -490,12 +492,12 @@ LLVM: # define POSH_CPU_SPARC 1 #endif -#if defined ARM || defined __arm__ || defined _ARM +#if defined ARM || defined __arm__ || defined _ARM || defined _M_ARM # define POSH_CPU_STRONGARM 1 # define POSH_CPU_STRING "ARM" #endif -#if defined __aarch64__ +#if defined __aarch64__ || defined _M_ARM64 # define POSH_CPU_AARCH64 1 # define POSH_CPU_STRING "ARM64" #endif diff --git a/3rdparty/bimg/3rdparty/nvtt/nvtt.cpp b/3rdparty/bimg/3rdparty/nvtt/nvtt.cpp index 0cc0df481eb..62f943b1026 100644 --- a/3rdparty/bimg/3rdparty/nvtt/nvtt.cpp +++ b/3rdparty/bimg/3rdparty/nvtt/nvtt.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include "nvtt.h" diff --git a/3rdparty/bimg/3rdparty/stb/stb_image.h b/3rdparty/bimg/3rdparty/stb/stb_image.h index d7d3e1ab9c9..accef4839cb 100644 --- a/3rdparty/bimg/3rdparty/stb/stb_image.h +++ b/3rdparty/bimg/3rdparty/stb/stb_image.h @@ -1,17 +1,5 @@ -#if defined(__has_warning) -# if __has_warning("-Wcomma") -# pragma GCC diagnostic ignored "-Wcomma" // Possible misuse of comma operator here -# endif -#endif - -#if defined(_MSC_VER) -# pragma warning(disable:4244) // warning C4244: '=': conversion from 'int' to 'stbi__uint16', possible loss of data -# pragma warning(disable:4245) // warning C4245: 'argument': conversion from 'int' to 'char', signed/unsigned mismatch -# pragma warning(disable:4456) // warning C4456: declaration of 'k' hides previous local declaration -#endif - -/* stb_image - v2.15 - public domain image loader - http://nothings.org/stb_image.h - no warranty implied; use at your own risk +/* stb_image - v2.26 - public domain image loader - http://nothings.org/stb + no warranty implied; use at your own risk Do this: #define STB_IMAGE_IMPLEMENTATION @@ -60,6 +48,17 @@ LICENSE RECENT REVISION HISTORY: + 2.26 (2020-07-13) many minor fixes + 2.25 (2020-02-02) fix warnings + 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically + 2.23 (2019-08-11) fix clang static analysis warning + 2.22 (2019-03-04) gif fixes, fix warnings + 2.21 (2019-02-25) fix typo in comment + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings + 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes @@ -70,10 +69,6 @@ RECENT REVISION HISTORY: correct channel count for PNG & BMP 2.10 (2016-01-22) avoid warning introduced in 2.09 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED - 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA - 2.07 (2015-09-13) partial animated GIF support - limited 16-bit PSD support - minor bugs, code cleanup, and compiler warnings See end of file for full revision history. @@ -89,28 +84,40 @@ RECENT REVISION HISTORY: Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) github:urraka (animated gif) Junggon Kim (PNM comments) - Daniel Gibson (16-bit TGA) + Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) socks-the-fox (16-bit PNG) Jeremy Sawicki (handle all ImageNet JPGs) - Optimizations & bugfixes - Fabian "ryg" Giesen + Optimizations & bugfixes Mikhail Morozov (1-bit BMP) + Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) Arseny Kapoulkine + John-Mark Allen + Carmelo J Fdez-Aguera Bug & warning fixes - Marc LeBlanc David Woo Guillaume George Martins Mozeiko - Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan - Dave Moore Roy Eltham Hayaki Saito Nathan Reed - Won Chun Luke Graham Johan Duparc Nick Verigakis - the Horde3D community Thomas Ruf Ronny Chevalier Baldur Karlsson - Janez Zemva John Bartholomew Michal Cichon github:rlyeh - Jonathan Blow Ken Hamada Tero Hanninen github:romigrou - Laurent Gomila Cort Stratton Sergio Gonzalez github:svdijk - Aruelien Pocheville Thibault Reuille Cass Everitt github:snagar - Ryamond Barbiero Paul Du Bois Engin Manap github:Zelex - Michaelangel007@github Philipp Wiesemann Dale Weiler github:grim210 - Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:sammyhw - Blazej Dariusz Roszkowski Gregory Mullen github:phprus - + Marc LeBlanc David Woo Guillaume George Martins Mozeiko + Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski + Phil Jordan Dave Moore Roy Eltham + Hayaki Saito Nathan Reed Won Chun + Luke Graham Johan Duparc Nick Verigakis the Horde3D community + Thomas Ruf Ronny Chevalier github:rlyeh + Janez Zemva John Bartholomew Michal Cichon github:romigrou + Jonathan Blow Ken Hamada Tero Hanninen github:svdijk + Laurent Gomila Cort Stratton github:snagar + Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex + Cass Everitt Ryamond Barbiero github:grim210 + Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw + Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus + Josh Tobin Matthew Gregan github:poppolopoppo + Julian Raschke Gregory Mullen Christian Floisand github:darealshinji + Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 + Brad Weinberger Matvey Cherevko [reserved] + Luca Sas Alexander Veselov Zack Middleton [reserved] + Ryan C. Gordon [reserved] [reserved] + DO NOT ADD YOUR NAME HERE + + To add your name to the credits, pick a random blank space in the middle and fill it. + 80% of merge conflicts on stb PRs are due to people adding their name at the end + of the credits. */ #ifndef STBI_INCLUDE_STB_IMAGE_H @@ -119,10 +126,8 @@ RECENT REVISION HISTORY: // DOCUMENTATION // // Limitations: -// - no 16-bit-per-channel PNG // - no 12-bit-per-channel JPEG // - no JPEGs with arithmetic coding -// - no 1-bit BMP // - GIF always returns *comp=4 // // Basic usage (see HDR discussion below for HDR usage): @@ -146,11 +151,12 @@ RECENT REVISION HISTORY: // with each pixel consisting of N interleaved 8-bit components; the first // pixel pointed to is top-left-most in the image. There is no padding between // image scanlines or between pixels, regardless of format. The number of -// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise. -// If req_comp is non-zero, *comp has the number of components that _would_ -// have been output otherwise. E.g. if you set req_comp to 4, you will always -// get RGBA output, but you can check *comp to see if it's trivially opaque -// because e.g. there were only 3 channels in the source image. +// components N is 'desired_channels' if desired_channels is non-zero, or +// *channels_in_file otherwise. If desired_channels is non-zero, +// *channels_in_file has the number of components that _would_ have been +// output otherwise. E.g. if you set desired_channels to 4, you will always +// get RGBA output, but you can check *channels_in_file to see if it's trivially +// opaque because e.g. there were only 3 channels in the source image. // // An output image with N components has the following components interleaved // in this order in each pixel: @@ -162,16 +168,26 @@ RECENT REVISION HISTORY: // 4 red, green, blue, alpha // // If image loading fails for any reason, the return value will be NULL, -// and *x, *y, *comp will be unchanged. The function stbi_failure_reason() -// can be queried for an extremely brief, end-user unfriendly explanation -// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid -// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly +// and *x, *y, *channels_in_file will be unchanged. The function +// stbi_failure_reason() can be queried for an extremely brief, end-user +// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS +// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly // more user-friendly ones. // // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. // // =========================================================================== // +// UNICODE: +// +// If compiling for Windows and you wish to use Unicode filenames, compile +// with +// #define STBI_WINDOWS_UTF8 +// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert +// Windows wchar_t filenames to utf8. +// +// =========================================================================== +// // Philosophy // // stb libraries are designed with the following priorities: @@ -182,12 +198,12 @@ RECENT REVISION HISTORY: // // Sometimes I let "good performance" creep up in priority over "easy to maintain", // and for best performance I may provide less-easy-to-use APIs that give higher -// performance, in addition to the easy to use ones. Nevertheless, it's important +// performance, in addition to the easy-to-use ones. Nevertheless, it's important // to keep in mind that from the standpoint of you, a client of this library, // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. // // Some secondary priorities arise directly from the first two, some of which -// make more explicit reasons why performance can't be emphasized. +// provide more explicit reasons why performance can't be emphasized. // // - Portable ("ease of use") // - Small source code footprint ("easy to maintain") @@ -230,11 +246,10 @@ RECENT REVISION HISTORY: // // HDR image support (disable by defining STBI_NO_HDR) // -// stb_image now supports loading HDR images in general, and currently -// the Radiance .HDR file format, although the support is provided -// generically. You can still load any file through the existing interface; -// if you attempt to load an HDR file, it will be automatically remapped to -// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; +// stb_image supports loading HDR images in general, and currently the Radiance +// .HDR file format specifically. You can still load any file through the existing +// interface; if you attempt to load an HDR file, it will be automatically remapped +// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; // both of these constants can be reconfigured through this interface: // // stbi_hdr_to_ldr_gamma(2.2f); @@ -268,7 +283,7 @@ RECENT REVISION HISTORY: // // By default we convert iphone-formatted PNGs back to RGB, even though // they are internally encoded differently. You can disable this conversion -// by by calling stbi_convert_iphone_png_to_rgb(0), in which case +// by calling stbi_convert_iphone_png_to_rgb(0), in which case // you will always just get the native iphone "format" through (which // is BGR stored in RGB). // @@ -312,7 +327,14 @@ RECENT REVISION HISTORY: // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB // - +// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater +// than that size (in either width or height) without further processing. +// This is to let programs in the wild set an upper bound to prevent +// denial-of-service attacks on untrusted data, as one could generate a +// valid image of gigantic dimensions and force stb_image to allocate a +// huge block of memory and spend disproportionate time decoding it. By +// default this is set to (1 << 24), which is 16777216, but that's still +// very big. #ifndef STBI_NO_STDIO #include <stdio.h> @@ -322,7 +344,7 @@ RECENT REVISION HISTORY: enum { - STBI_default = 0, // only used for req_comp + STBI_default = 0, // only used for desired_channels STBI_grey = 1, STBI_grey_alpha = 2, @@ -330,6 +352,7 @@ enum STBI_rgb_alpha = 4 }; +#include <stdlib.h> typedef unsigned char stbi_uc; typedef unsigned short stbi_us; @@ -337,11 +360,13 @@ typedef unsigned short stbi_us; extern "C" { #endif +#ifndef STBIDEF #ifdef STB_IMAGE_STATIC #define STBIDEF static #else #define STBIDEF extern #endif +#endif ////////////////////////////////////////////////////////////////////////////// // @@ -364,36 +389,46 @@ typedef struct // 8-bits-per-channel interface // -STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels); #ifndef STBI_NO_STDIO -STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); // for stbi_load_from_file, file pointer is left pointing immediately after image #endif +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +#endif + +#ifdef STBI_WINDOWS_UTF8 +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); +#endif + //////////////////////////////////// // // 16-bits-per-channel interface // -STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + #ifndef STBI_NO_STDIO +STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); #endif -// @TODO the other variants //////////////////////////////////// // // float-per-channel interface // #ifndef STBI_NO_LINEAR - STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); #ifndef STBI_NO_STDIO + STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); #endif #endif @@ -418,7 +453,7 @@ STBIDEF int stbi_is_hdr_from_file(FILE *f); // get a VERY brief reason for failure -// NOT THREADSAFE +// on most compilers (and ALL modern mainstream compilers) this is threadsafe STBIDEF const char *stbi_failure_reason (void); // free the loaded image -- this is just free() @@ -427,11 +462,14 @@ STBIDEF void stbi_image_free (void *retval_from_stbi_load); // get image dimensions & components without fully decoding STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); #ifndef STBI_NO_STDIO -STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); -STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); - +STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit (char const *filename); +STBIDEF int stbi_is_16_bit_from_file(FILE *f); #endif @@ -448,6 +486,11 @@ STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert); // flip the image vertically, so the first pixel in the output array is the bottom left STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip); +// as above, but only applies to images loaded on the thread that calls the function +// this function is only available if your compiler supports thread-local variables; +// calling it will fail to link if your compiler doesn't +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip); + // ZLIB client - used by PNG, available for other purposes STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); @@ -515,7 +558,7 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch #include <limits.h> #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) -#include <math.h> // ldexp +#include <math.h> // ldexp, pow #endif #ifndef STBI_NO_STDIO @@ -527,6 +570,12 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch #define STBI_ASSERT(x) assert(x) #endif +#ifdef __cplusplus +#define STBI_EXTERN extern "C" +#else +#define STBI_EXTERN extern +#endif + #ifndef _MSC_VER #ifdef __cplusplus @@ -538,6 +587,23 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch #define stbi_inline __forceinline #endif +#ifndef STBI_NO_THREAD_LOCALS + #if defined(__cplusplus) && __cplusplus >= 201103L + #define STBI_THREAD_LOCAL thread_local + #elif defined(__GNUC__) && __GNUC__ < 5 + #define STBI_THREAD_LOCAL __thread + #elif defined(_MSC_VER) + #define STBI_THREAD_LOCAL __declspec(thread) + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) + #define STBI_THREAD_LOCAL _Thread_local + #endif + + #ifndef STBI_THREAD_LOCAL + #if defined(__GNUC__) + #define STBI_THREAD_LOCAL __thread + #endif + #endif +#endif #ifdef _MSC_VER typedef unsigned short stbi__uint16; @@ -651,15 +717,19 @@ static int stbi__cpuid3(void) #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name -static int stbi__sse2_available() +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) { int info3 = stbi__cpuid3(); return ((info3 >> 26) & 1) != 0; } +#endif + #else // assume GCC-style if not VC++ #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) -static int stbi__sse2_available() +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) { // If we're even attempting to compile this on GCC/Clang, that means // -msse2 is on, which means the compiler is allowed to use SSE2 @@ -667,6 +737,8 @@ static int stbi__sse2_available() return 1; } #endif + +#endif #endif // ARM NEON @@ -684,6 +756,10 @@ static int stbi__sse2_available() #define STBI_SIMD_ALIGN(type, name) type name #endif +#ifndef STBI_MAX_DIMENSIONS +#define STBI_MAX_DIMENSIONS (1 << 24) +#endif + /////////////////////////////////////////////// // // stbi__context struct and start_xxx functions @@ -701,6 +777,7 @@ typedef struct int read_from_callbacks; int buflen; stbi_uc buffer_start[128]; + int callback_already_read; stbi_uc *img_buffer, *img_buffer_end; stbi_uc *img_buffer_original, *img_buffer_original_end; @@ -714,6 +791,7 @@ static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) { s->io.read = NULL; s->read_from_callbacks = 0; + s->callback_already_read = 0; s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; } @@ -725,7 +803,8 @@ static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void * s->io_user_data = user; s->buflen = sizeof(s->buffer_start); s->read_from_callbacks = 1; - s->img_buffer_original = s->buffer_start; + s->callback_already_read = 0; + s->img_buffer = s->img_buffer_original = s->buffer_start; stbi__refill_buffer(s); s->img_buffer_original_end = s->img_buffer_end; } @@ -739,12 +818,17 @@ static int stbi__stdio_read(void *user, char *data, int size) static void stbi__stdio_skip(void *user, int n) { + int ch; fseek((FILE*) user, n, SEEK_CUR); + ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */ + if (ch != EOF) { + ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */ + } } static int stbi__stdio_eof(void *user) { - return feof((FILE*) user); + return feof((FILE*) user) || ferror((FILE *) user); } static stbi_io_callbacks stbi__stdio_callbacks = @@ -795,6 +879,7 @@ static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); static int stbi__png_test(stbi__context *s); static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__png_is16(stbi__context *s); #endif #ifndef STBI_NO_BMP @@ -813,6 +898,7 @@ static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); static int stbi__psd_test(stbi__context *s); static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__psd_is16(stbi__context *s); #endif #ifndef STBI_NO_HDR @@ -830,6 +916,7 @@ static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); #ifndef STBI_NO_GIF static int stbi__gif_test(stbi__context *s); static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); #endif @@ -839,19 +926,24 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); #endif -// this is not threadsafe -static const char *stbi__g_failure_reason; +static +#ifdef STBI_THREAD_LOCAL +STBI_THREAD_LOCAL +#endif +const char *stbi__g_failure_reason; STBIDEF const char *stbi_failure_reason(void) { return stbi__g_failure_reason; } +#ifndef STBI_NO_FAILURE_STRINGS static int stbi__err(const char *str) { stbi__g_failure_reason = str; return 0; } +#endif static void *stbi__malloc(size_t size) { @@ -890,11 +982,13 @@ static int stbi__mul2sizes_valid(int a, int b) return a <= INT_MAX/b; } +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow static int stbi__mad2sizes_valid(int a, int b, int add) { return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); } +#endif // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow static int stbi__mad3sizes_valid(int a, int b, int c, int add) @@ -904,18 +998,22 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add) } // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) { return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); } +#endif +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) // mallocs with size overflow checking static void *stbi__malloc_mad2(int a, int b, int add) { if (!stbi__mad2sizes_valid(a, b, add)) return NULL; return stbi__malloc(a*b + add); } +#endif static void *stbi__malloc_mad3(int a, int b, int c, int add) { @@ -923,11 +1021,13 @@ static void *stbi__malloc_mad3(int a, int b, int c, int add) return stbi__malloc(a*b*c + add); } +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) { if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; return stbi__malloc(a*b*c*d + add); } +#endif // stbi__err - error // stbi__errpf - error returning pointer to float @@ -957,13 +1057,29 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); #endif -static int stbi__vertically_flip_on_load = 0; +static int stbi__vertically_flip_on_load_global = 0; STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) { - stbi__vertically_flip_on_load = flag_true_if_should_flip; + stbi__vertically_flip_on_load_global = flag_true_if_should_flip; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global +#else +static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; + +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_local = flag_true_if_should_flip; + stbi__vertically_flip_on_load_set = 1; } +#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ + ? stbi__vertically_flip_on_load_local \ + : stbi__vertically_flip_on_load_global) +#endif // STBI_THREAD_LOCAL + static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) { memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields @@ -985,6 +1101,8 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re #endif #ifndef STBI_NO_PSD if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); + #else + STBI_NOTUSED(bpc); #endif #ifndef STBI_NO_PIC if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); @@ -1041,6 +1159,44 @@ static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int chan return enlarged; } +static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) +{ + int row; + size_t bytes_per_row = (size_t)w * bytes_per_pixel; + stbi_uc temp[2048]; + stbi_uc *bytes = (stbi_uc *)image; + + for (row = 0; row < (h>>1); row++) { + stbi_uc *row0 = bytes + row*bytes_per_row; + stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; + // swap row0 with row1 + size_t bytes_left = bytes_per_row; + while (bytes_left) { + size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); + memcpy(temp, row0, bytes_copy); + memcpy(row0, row1, bytes_copy); + memcpy(row1, temp, bytes_copy); + row0 += bytes_copy; + row1 += bytes_copy; + bytes_left -= bytes_copy; + } + } +} + +#ifndef STBI_NO_GIF +static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) +{ + int slice; + int slice_size = w * h * bytes_per_pixel; + + stbi_uc *bytes = (stbi_uc *)image; + for (slice = 0; slice < z; ++slice) { + stbi__vertical_flip(bytes, w, h, bytes_per_pixel); + bytes += slice_size; + } +} +#endif + static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) { stbi__result_info ri; @@ -1049,8 +1205,10 @@ static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, if (result == NULL) return NULL; + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + if (ri.bits_per_channel != 8) { - STBI_ASSERT(ri.bits_per_channel == 16); result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); ri.bits_per_channel = 8; } @@ -1058,21 +1216,8 @@ static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, // @TODO: move stbi__convert_format to here if (stbi__vertically_flip_on_load) { - int w = *x, h = *y; int channels = req_comp ? req_comp : *comp; - int row,col,z; - stbi_uc *image = (stbi_uc *) result; - - // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once - for (row = 0; row < (h>>1); row++) { - for (col = 0; col < w; col++) { - for (z = 0; z < channels; z++) { - stbi_uc temp = image[(row * w + col) * channels + z]; - image[(row * w + col) * channels + z] = image[((h - row - 1) * w + col) * channels + z]; - image[((h - row - 1) * w + col) * channels + z] = temp; - } - } - } + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); } return (unsigned char *) result; @@ -1086,8 +1231,10 @@ static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, if (result == NULL) return NULL; + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + if (ri.bits_per_channel != 16) { - STBI_ASSERT(ri.bits_per_channel == 8); result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); ri.bits_per_channel = 16; } @@ -1096,55 +1243,57 @@ static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision if (stbi__vertically_flip_on_load) { - int w = *x, h = *y; int channels = req_comp ? req_comp : *comp; - int row,col,z; - stbi__uint16 *image = (stbi__uint16 *) result; - - // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once - for (row = 0; row < (h>>1); row++) { - for (col = 0; col < w; col++) { - for (z = 0; z < channels; z++) { - stbi__uint16 temp = image[(row * w + col) * channels + z]; - image[(row * w + col) * channels + z] = image[((h - row - 1) * w + col) * channels + z]; - image[((h - row - 1) * w + col) * channels + z] = temp; - } - } - } + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); } return (stbi__uint16 *) result; } -#ifndef STBI_NO_HDR +#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) { if (stbi__vertically_flip_on_load && result != NULL) { - int w = *x, h = *y; - int depth = req_comp ? req_comp : *comp; - int row,col,z; - float temp; - - // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once - for (row = 0; row < (h>>1); row++) { - for (col = 0; col < w; col++) { - for (z = 0; z < depth; z++) { - temp = result[(row * w + col) * depth + z]; - result[(row * w + col) * depth + z] = result[((h - row - 1) * w + col) * depth + z]; - result[((h - row - 1) * w + col) * depth + z] = temp; - } - } - } + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); } } #endif #ifndef STBI_NO_STDIO +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) +STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); +STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); +#endif + +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) +{ + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); +} +#endif + static FILE *stbi__fopen(char const *filename, char const *mode) { FILE *f; -#if defined(_MSC_VER) && _MSC_VER >= 1400 +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) + wchar_t wMode[64]; + wchar_t wFilename[1024]; + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) + return 0; + + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) + return 0; + +#if _MSC_VER >= 1400 + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; +#else + f = _wfopen(wFilename, wMode); +#endif + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 if (0 != fopen_s(&f, filename, mode)) f=0; #else @@ -1203,6 +1352,20 @@ STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, i #endif //!STBI_NO_STDIO +STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi__context s; @@ -1217,6 +1380,22 @@ STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *u return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); } +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_mem(&s,buffer,len); + + result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); + if (stbi__vertically_flip_on_load) { + stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); + } + + return result; +} +#endif + #ifndef STBI_NO_LINEAR static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) { @@ -1300,12 +1479,16 @@ STBIDEF int stbi_is_hdr (char const *filename) return result; } -STBIDEF int stbi_is_hdr_from_file(FILE *f) +STBIDEF int stbi_is_hdr_from_file(FILE *f) { #ifndef STBI_NO_HDR + long pos = ftell(f); + int res; stbi__context s; stbi__start_file(&s,f); - return stbi__hdr_test(&s); + res = stbi__hdr_test(&s); + fseek(f, pos, SEEK_SET); + return res; #else STBI_NOTUSED(f); return 0; @@ -1354,6 +1537,7 @@ enum static void stbi__refill_buffer(stbi__context *s) { int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); + s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original); if (n == 0) { // at end of file, treat same as if from memory, but need to handle case // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file @@ -1378,6 +1562,9 @@ stbi_inline static stbi_uc stbi__get8(stbi__context *s) return 0; } +#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else stbi_inline static int stbi__at_eof(stbi__context *s) { if (s->io.read) { @@ -1389,9 +1576,14 @@ stbi_inline static int stbi__at_eof(stbi__context *s) return s->img_buffer >= s->img_buffer_end; } +#endif +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) +// nothing +#else static void stbi__skip(stbi__context *s, int n) { + if (n == 0) return; // already there! if (n < 0) { s->img_buffer = s->img_buffer_end; return; @@ -1406,7 +1598,11 @@ static void stbi__skip(stbi__context *s, int n) } s->img_buffer += n; } +#endif +#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) +// nothing +#else static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) { if (s->io.read) { @@ -1430,18 +1626,27 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) } else return 0; } +#endif +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else static int stbi__get16be(stbi__context *s) { int z = stbi__get8(s); return (z << 8) + stbi__get8(s); } +#endif +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else static stbi__uint32 stbi__get32be(stbi__context *s) { stbi__uint32 z = stbi__get16be(s); return (z << 16) + stbi__get16be(s); } +#endif #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) // nothing @@ -1463,7 +1668,9 @@ static stbi__uint32 stbi__get32le(stbi__context *s) #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings - +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else ////////////////////////////////////////////////////////////////////////////// // // generic converter from built-in img_n to req_comp @@ -1479,7 +1686,11 @@ static stbi_uc stbi__compute_y(int r, int g, int b) { return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); } +#endif +#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) { int i,j; @@ -1503,19 +1714,19 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r // convert source image with img_n components to one with req_comp components; // avoid switch per pixel, so use switch per scanline and massive macros switch (STBI__COMBO(img_n, req_comp)) { - STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break; + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; - STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; STBI__CASE(2,1) { dest[0]=src[0]; } break; STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; - STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break; - STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; - STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; - STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break; - STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break; - default: STBI_ASSERT(0); + STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion"); } #undef STBI__CASE } @@ -1523,12 +1734,20 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r STBI_FREE(data); return good; } +#endif +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else static stbi__uint16 stbi__compute_y_16(int r, int g, int b) { return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); } +#endif +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) { int i,j; @@ -1552,19 +1771,19 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r // convert source image with img_n components to one with req_comp components; // avoid switch per pixel, so use switch per scanline and massive macros switch (STBI__COMBO(img_n, req_comp)) { - STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break; + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; - STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; STBI__CASE(2,1) { dest[0]=src[0]; } break; STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; - STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break; - STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; - STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; - STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break; - STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break; - default: STBI_ASSERT(0); + STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion"); } #undef STBI__CASE } @@ -1572,6 +1791,7 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r STBI_FREE(data); return good; } +#endif #ifndef STBI_NO_LINEAR static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) @@ -1587,7 +1807,11 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) for (k=0; k < n; ++k) { output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); } - if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f; + } + if (n < comp) { + for (i=0; i < x*y; ++i) { + output[i*comp + n] = data[i*comp + n]/255.0f; + } } STBI_FREE(data); return output; @@ -1717,7 +1941,8 @@ typedef struct static int stbi__build_huffman(stbi__huffman *h, int *count) { - int i,j,k=0,code; + int i,j,k=0; + unsigned int code; // build size list for each symbol (from JPEG spec) for (i=0; i < 16; ++i) for (j=0; j < count[i]; ++j) @@ -1733,7 +1958,7 @@ static int stbi__build_huffman(stbi__huffman *h, int *count) if (h->size[k] == j) { while (h->size[k] == j) h->code[k++] = (stbi__uint16) (code++); - if (code-1 >= (1 << j)) return stbi__err("bad code lengths","Corrupt JPEG"); + if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); } // compute largest code + 1 for this size, preshifted as needed later h->maxcode[j] = code << (16-j); @@ -1777,7 +2002,7 @@ static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) if (k < m) k += (~0U << magbits) + 1; // if the result is small enough, we can fit it in fast_ac table if (k >= -128 && k <= 127) - fast_ac[i] = (stbi__int16) ((k << 8) + (run << 4) + (len + magbits)); + fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); } } } @@ -1786,7 +2011,7 @@ static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) static void stbi__grow_buffer_unsafe(stbi__jpeg *j) { do { - int b = j->nomore ? 0 : stbi__get8(j->s); + unsigned int b = j->nomore ? 0 : stbi__get8(j->s); if (b == 0xff) { int c = stbi__get8(j->s); while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes @@ -1802,7 +2027,7 @@ static void stbi__grow_buffer_unsafe(stbi__jpeg *j) } // (1 << n) - 1 -static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; +static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; // decode a jpeg huffman value from the bitstream stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) @@ -1855,7 +2080,7 @@ stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) } // bias[n] = (-1<<n) + 1 -static int const stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767}; +static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767}; // combined JPEG 'receive' and JPEG 'extend', since baseline // always extends everything it receives. @@ -1867,7 +2092,7 @@ stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n) sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB k = stbi_lrot(j->code_buffer, n); - STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask))); + if (n < 0 || n >= (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask))) return 0; j->code_buffer = k & ~stbi__bmask[n]; k &= stbi__bmask[n]; j->code_bits -= n; @@ -1898,7 +2123,7 @@ stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) // given a value that's at position X in the zigzag stream, // where does it appear in the 8x8 matrix coded as row-major? -static stbi_uc stbi__jpeg_dezigzag[64+15] = +static const stbi_uc stbi__jpeg_dezigzag[64+15] = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, @@ -1978,6 +2203,7 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__ // first scan for DC coefficient, must be first memset(data,0,64*sizeof(data[0])); // 0 all the ac values now t = stbi__jpeg_huff_decode(j, hdc); + if (t == -1) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); diff = t ? stbi__extend_receive(j, t) : 0; dc = j->img_comp[b].dc_pred + diff; @@ -2124,7 +2350,7 @@ stbi_inline static stbi_uc stbi__clamp(int x) } #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) -#define stbi__fsh(x) ((x) << 12) +#define stbi__fsh(x) ((x) * 4096) // derived from jidctint -- DCT_ISLOW #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ @@ -2179,7 +2405,7 @@ static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) // (1|2|3|4|5|6|7)==0 0 seconds // all separate -0.047 seconds // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds - int dcterm = d[0] << 2; + int dcterm = d[0]*4; v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; } else { STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) @@ -2818,7 +3044,7 @@ static int stbi__process_marker(stbi__jpeg *z, int m) if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); for (i=0; i < 64; ++i) - z->dequant[t][stbi__jpeg_dezigzag[i]] = sixteen ? stbi__get16be(z->s) : stbi__get8(z->s); + z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); L -= (sixteen ? 129 : 65); } return L==0; @@ -2968,6 +3194,8 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan) p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); c = stbi__get8(s); if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG"); s->img_n = c; @@ -2980,7 +3208,7 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan) z->rgb = 0; for (i=0; i < s->img_n; ++i) { - static unsigned char rgb[3] = { 'R', 'G', 'B' }; + static const unsigned char rgb[3] = { 'R', 'G', 'B' }; z->img_comp[i].id = stbi__get8(s); if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) ++z->rgb; @@ -3105,8 +3333,8 @@ static int stbi__decode_jpeg_image(stbi__jpeg *j) } else if (stbi__DNL(m)) { int Ld = stbi__get16be(j->s); stbi__uint32 NL = stbi__get16be(j->s); - if (Ld != 4) stbi__err("bad DNL len", "Corrupt JPEG"); - if (NL != j->s->img_y) stbi__err("bad DNL height", "Corrupt JPEG"); + if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG"); + if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG"); } else { if (!stbi__process_marker(j, m)) return 0; } @@ -3559,7 +3787,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp int k; unsigned int i,j; stbi_uc *output; - stbi_uc *coutput[4]; + stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; stbi__resample res_comp[4]; @@ -3623,20 +3851,20 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp } else if (z->s->img_n == 4) { if (z->app14_color_transform == 0) { // CMYK for (i=0; i < z->s->img_x; ++i) { - stbi_uc k = coutput[3][i]; - out[0] = stbi__blinn_8x8(coutput[0][i], k); - out[1] = stbi__blinn_8x8(coutput[1][i], k); - out[2] = stbi__blinn_8x8(coutput[2][i], k); + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(coutput[0][i], m); + out[1] = stbi__blinn_8x8(coutput[1][i], m); + out[2] = stbi__blinn_8x8(coutput[2][i], m); out[3] = 255; out += n; } } else if (z->app14_color_transform == 2) { // YCCK z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); for (i=0; i < z->s->img_x; ++i) { - stbi_uc k = coutput[3][i]; - out[0] = stbi__blinn_8x8(255 - out[0], k); - out[1] = stbi__blinn_8x8(255 - out[1], k); - out[2] = stbi__blinn_8x8(255 - out[2], k); + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(255 - out[0], m); + out[1] = stbi__blinn_8x8(255 - out[1], m); + out[2] = stbi__blinn_8x8(255 - out[2], m); out += n; } } else { // YCbCr + alpha? Ignore the fourth channel for now @@ -3661,10 +3889,10 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp } } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { for (i=0; i < z->s->img_x; ++i) { - stbi_uc k = coutput[3][i]; - stbi_uc r = stbi__blinn_8x8(coutput[0][i], k); - stbi_uc g = stbi__blinn_8x8(coutput[1][i], k); - stbi_uc b = stbi__blinn_8x8(coutput[2][i], k); + stbi_uc m = coutput[3][i]; + stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); + stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); + stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); out[0] = stbi__compute_y(r, g, b); out[1] = 255; out += n; @@ -3680,7 +3908,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp if (n == 1) for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; else - for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255; + for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } } } } @@ -3848,16 +4076,23 @@ typedef struct stbi__zhuffman z_length, z_distance; } stbi__zbuf; +stbi_inline static int stbi__zeof(stbi__zbuf *z) +{ + return (z->zbuffer >= z->zbuffer_end); +} + stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) { - if (z->zbuffer >= z->zbuffer_end) return 0; - return *z->zbuffer++; + return stbi__zeof(z) ? 0 : *z->zbuffer++; } static void stbi__fill_bits(stbi__zbuf *z) { do { - STBI_ASSERT(z->code_buffer < (1U << z->num_bits)); + if (z->code_buffer >= (1U << z->num_bits)) { + z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */ + return; + } z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; z->num_bits += 8; } while (z->num_bits <= 24); @@ -3882,10 +4117,11 @@ static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) for (s=STBI__ZFAST_BITS+1; ; ++s) if (k < z->maxcode[s]) break; - if (s == 16) return -1; // invalid code! + if (s >= 16) return -1; // invalid code! // code size is s, so: b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; - STBI_ASSERT(z->size[b] == s); + if (b >= sizeof (z->size)) return -1; // some data was corrupt somewhere! + if (z->size[b] != s) return -1; // was originally an assert, but report failure instead. a->code_buffer >>= s; a->num_bits -= s; return z->value[b]; @@ -3894,7 +4130,12 @@ static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) { int b,s; - if (a->num_bits < 16) stbi__fill_bits(a); + if (a->num_bits < 16) { + if (stbi__zeof(a)) { + return -1; /* report error for unexpected end of data. */ + } + stbi__fill_bits(a); + } b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; if (b) { s = b >> 9; @@ -3908,13 +4149,16 @@ stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes { char *q; - int cur, limit, old_limit; + unsigned int cur, limit, old_limit; z->zout = zout; if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); - cur = (int) (z->zout - z->zout_start); - limit = old_limit = (int) (z->zout_end - z->zout_start); - while (cur + n > limit) + cur = (unsigned int) (z->zout - z->zout_start); + limit = old_limit = (unsigned) (z->zout_end - z->zout_start); + if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory"); + while (cur + n > limit) { + if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory"); limit *= 2; + } q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); STBI_NOTUSED(old_limit); if (q == NULL) return stbi__err("outofmem", "Out of memory"); @@ -3924,18 +4168,18 @@ static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room return 1; } -static int stbi__zlength_base[31] = { +static const int stbi__zlength_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 }; -static int stbi__zlength_extra[31]= +static const int stbi__zlength_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; -static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, +static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; -static int stbi__zdist_extra[32] = +static const int stbi__zdist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; static int stbi__parse_huffman_block(stbi__zbuf *a) @@ -3982,7 +4226,7 @@ static int stbi__parse_huffman_block(stbi__zbuf *a) static int stbi__compute_huffman_codes(stbi__zbuf *a) { - static stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; + static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; stbi__zhuffman z_codelength; stbi_uc lencodes[286+32+137];//padding for maximum single op stbi_uc codelength_sizes[19]; @@ -4012,11 +4256,12 @@ static int stbi__compute_huffman_codes(stbi__zbuf *a) c = stbi__zreceive(a,2)+3; if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); fill = lencodes[n-1]; - } else if (c == 17) + } else if (c == 17) { c = stbi__zreceive(a,3)+3; - else { - STBI_ASSERT(c == 18); + } else if (c == 18) { c = stbi__zreceive(a,7)+11; + } else { + return stbi__err("bad codelengths", "Corrupt PNG"); } if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); memset(lencodes+n, fill, c); @@ -4042,7 +4287,7 @@ static int stbi__parse_uncompressed_block(stbi__zbuf *a) a->code_buffer >>= 8; a->num_bits -= 8; } - STBI_ASSERT(a->num_bits == 0); + if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG"); // now fill header the normal way while (k < 4) header[k++] = stbi__zget8(a); @@ -4064,6 +4309,7 @@ static int stbi__parse_zlib_header(stbi__zbuf *a) int cm = cmf & 15; /* int cinfo = cmf >> 4; */ int flg = stbi__zget8(a); + if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png @@ -4241,7 +4487,7 @@ static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) static int stbi__check_png_header(stbi__context *s) { - static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; + static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; int i; for (i=0; i < 8; ++i) if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG"); @@ -4287,7 +4533,7 @@ static int stbi__paeth(int a, int b, int c) return c; } -static stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; +static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; // create the png data from post-deflated data static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) @@ -4307,13 +4553,14 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into if (!a->out) return stbi__err("outofmem", "Out of memory"); + if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG"); img_width_bytes = (((img_n * x * depth) + 7) >> 3); img_len = (img_width_bytes + 1) * y; - if (s->img_x == x && s->img_y == y) { - if (raw_len != img_len) return stbi__err("not enough pixels","Corrupt PNG"); - } else { // interlaced: - if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); - } + + // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, + // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), + // so just check for raw_len < img_len always. + if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); for (j=0; j < y; ++j) { stbi_uc *cur = a->out + stride*j; @@ -4324,7 +4571,7 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r return stbi__err("invalid filter","Corrupt PNG"); if (depth < 8) { - STBI_ASSERT(img_width_bytes <= x); + if (img_width_bytes > x) return stbi__err("invalid width","Corrupt PNG"); cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place filter_bytes = 1; width = img_width_bytes; @@ -4666,9 +4913,10 @@ static void stbi__de_iphone(stbi__png *z) stbi_uc a = p[3]; stbi_uc t = p[0]; if (a) { - p[0] = p[2] * 255 / a; - p[1] = p[1] * 255 / a; - p[2] = t * 255 / a; + stbi_uc half = a / 2; + p[0] = (p[2] * 255 + half) / a; + p[1] = (p[1] * 255 + half) / a; + p[2] = ( t * 255 + half) / a; } else { p[0] = p[2]; p[2] = t; @@ -4687,12 +4935,12 @@ static void stbi__de_iphone(stbi__png *z) } } -#define STBI__PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d)) +#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) { stbi_uc palette[1024], pal_img_n=0; - stbi_uc has_trans=0, tc[3]; + stbi_uc has_trans=0, tc[3]={0}; stbi__uint16 tc16[3]; stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; int first=1,k,interlace=0, color=0, is_iphone=0; @@ -4718,8 +4966,10 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (!first) return stbi__err("multiple IHDR","Corrupt PNG"); first = 0; if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); - s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); - s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); + s->img_x = stbi__get32be(s); + s->img_y = stbi__get32be(s); + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); @@ -4831,8 +5081,13 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (req_comp >= 3) s->img_out_n = req_comp; if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) return 0; + } else if (has_trans) { + // non-paletted image with tRNS -> source image has (constant) alpha + ++s->img_n; } STBI_FREE(z->expanded); z->expanded = NULL; + // end of PNG chunk, read and skip CRC + stbi__get32be(s); return 1; } @@ -4863,10 +5118,12 @@ static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, st void *result=NULL; if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { - if (p->depth < 8) + if (p->depth <= 8) ri->bits_per_channel = 8; + else if (p->depth == 16) + ri->bits_per_channel = 16; else - ri->bits_per_channel = p->depth; + return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth"); result = p->out; p->out = NULL; if (req_comp && req_comp != p->s->img_out_n) { @@ -4921,6 +5178,19 @@ static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) p.s = s; return stbi__png_info_raw(&p, x, y, comp); } + +static int stbi__png_is16(stbi__context *s) +{ + stbi__png p; + p.s = s; + if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) + return 0; + if (p.depth != 16) { + stbi__rewind(p.s); + return 0; + } + return 1; +} #endif // Microsoft/Windows BMP image @@ -4954,11 +5224,11 @@ static int stbi__high_bit(unsigned int z) { int n=0; if (z == 0) return -1; - if (z >= 0x10000) n += 16, z >>= 16; - if (z >= 0x00100) n += 8, z >>= 8; - if (z >= 0x00010) n += 4, z >>= 4; - if (z >= 0x00004) n += 2, z >>= 2; - if (z >= 0x00002) n += 1, z >>= 1; + if (z >= 0x10000) { n += 16; z >>= 16; } + if (z >= 0x00100) { n += 8; z >>= 8; } + if (z >= 0x00010) { n += 4; z >>= 4; } + if (z >= 0x00004) { n += 2; z >>= 2; } + if (z >= 0x00002) { n += 1;/* >>= 1;*/ } return n; } @@ -4972,27 +5242,34 @@ static int stbi__bitcount(unsigned int a) return a & 0xff; } -static int stbi__shiftsigned(int v, int shift, int bits) -{ - int result; - int z=0; - - if (shift < 0) v <<= -shift; - else v >>= shift; - result = v; - - z = bits; - while (z < 8) { - result += v >> z; - z += bits; - } - return result; +// extract an arbitrarily-aligned N-bit value (N=bits) +// from v, and then make it 8-bits long and fractionally +// extend it to full full range. +static int stbi__shiftsigned(unsigned int v, int shift, int bits) +{ + static unsigned int mul_table[9] = { + 0, + 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, + 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, + }; + static unsigned int shift_table[9] = { + 0, 0,0,1,0,2,4,6,0, + }; + if (shift < 0) + v <<= -shift; + else + v >>= shift; + STBI_ASSERT(v < 256); + v >>= (8-bits); + STBI_ASSERT(bits >= 0 && bits <= 8); + return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; } typedef struct { int bpp, offset, hsz; unsigned int mr,mg,mb,ma, all_a; + int extra_read; } stbi__bmp_data; static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) @@ -5005,6 +5282,9 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) info->offset = stbi__get32le(s); info->hsz = hsz = stbi__get32le(s); info->mr = info->mg = info->mb = info->ma = 0; + info->extra_read = 14; + + if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP"); if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); if (hsz == 12) { @@ -5016,7 +5296,6 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) } if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); info->bpp = stbi__get16le(s); - if (info->bpp == 1) return stbi__errpuc("monochrome", "BMP type not supported: 1-bit"); if (hsz != 12) { int compress = stbi__get32le(s); if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); @@ -5049,6 +5328,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) info->mr = stbi__get32le(s); info->mg = stbi__get32le(s); info->mb = stbi__get32le(s); + info->extra_read += 12; // not documented, but generated by photoshop and handled by mspaint if (info->mr == info->mg && info->mg == info->mb) { // ?!?!? @@ -5097,6 +5377,9 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req flip_vertically = ((int) s->img_y) > 0; s->img_y = abs((int) s->img_y); + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + mr = info.mr; mg = info.mg; mb = info.mb; @@ -5105,13 +5388,22 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req if (info.hsz == 12) { if (info.bpp < 24) - psize = (info.offset - 14 - 24) / 3; + psize = (info.offset - info.extra_read - 24) / 3; } else { if (info.bpp < 16) - psize = (info.offset - 14 - info.hsz) >> 2; + psize = (info.offset - info.extra_read - info.hsz) >> 2; + } + if (psize == 0) { + STBI_ASSERT(info.offset == s->callback_already_read + (int) (s->img_buffer - s->img_buffer_original)); + if (info.offset != s->callback_already_read + (s->img_buffer - s->buffer_start)) { + return stbi__errpuc("bad offset", "Corrupt BMP"); + } } - s->img_n = ma ? 4 : 3; + if (info.bpp == 24 && ma == 0xff000000) + s->img_n = 3; + else + s->img_n = ma ? 4 : 3; if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 target = req_comp; else @@ -5133,36 +5425,56 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req if (info.hsz != 12) stbi__get8(s); pal[i][3] = 255; } - stbi__skip(s, info.offset - 14 - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); - if (info.bpp == 4) width = (s->img_x + 1) >> 1; + stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); + if (info.bpp == 1) width = (s->img_x + 7) >> 3; + else if (info.bpp == 4) width = (s->img_x + 1) >> 1; else if (info.bpp == 8) width = s->img_x; else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); } pad = (-width)&3; - for (j=0; j < (int) s->img_y; ++j) { - for (i=0; i < (int) s->img_x; i += 2) { - int v=stbi__get8(s),v2=0; - if (info.bpp == 4) { - v2 = v & 15; - v >>= 4; + if (info.bpp == 1) { + for (j=0; j < (int) s->img_y; ++j) { + int bit_offset = 7, v = stbi__get8(s); + for (i=0; i < (int) s->img_x; ++i) { + int color = (v>>bit_offset)&0x1; + out[z++] = pal[color][0]; + out[z++] = pal[color][1]; + out[z++] = pal[color][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + if((--bit_offset) < 0) { + bit_offset = 7; + v = stbi__get8(s); + } } - out[z++] = pal[v][0]; - out[z++] = pal[v][1]; - out[z++] = pal[v][2]; - if (target == 4) out[z++] = 255; - if (i+1 == (int) s->img_x) break; - v = (info.bpp == 8) ? stbi__get8(s) : v2; - out[z++] = pal[v][0]; - out[z++] = pal[v][1]; - out[z++] = pal[v][2]; - if (target == 4) out[z++] = 255; + stbi__skip(s, pad); + } + } else { + for (j=0; j < (int) s->img_y; ++j) { + for (i=0; i < (int) s->img_x; i += 2) { + int v=stbi__get8(s),v2=0; + if (info.bpp == 4) { + v2 = v & 15; + v >>= 4; + } + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + v = (info.bpp == 8) ? stbi__get8(s) : v2; + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + } + stbi__skip(s, pad); } - stbi__skip(s, pad); } } else { int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; int z = 0; int easy=0; - stbi__skip(s, info.offset - 14 - info.hsz); + stbi__skip(s, info.offset - info.extra_read - info.hsz); if (info.bpp == 24) width = 3 * s->img_x; else if (info.bpp == 16) width = 2*s->img_x; else /* bpp = 32 and pad = 0 */ width=0; @@ -5180,6 +5492,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); + if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } } for (j=0; j < (int) s->img_y; ++j) { if (easy) { @@ -5197,7 +5510,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req int bpp = info.bpp; for (i=0; i < (int) s->img_x; ++i) { stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); - int a; + unsigned int a; out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); @@ -5221,7 +5534,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req stbi_uc *p1 = out + j *s->img_x*target; stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; for (i=0; i < (int) s->img_x*target; ++i) { - t = p1[i], p1[i] = p2[i], p2[i] = t; + t = p1[i]; p1[i] = p2[i]; p2[i] = t; } } } @@ -5245,14 +5558,14 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) { // only RGB or RGBA (incl. 16bit) or grey allowed - if(is_rgb16) *is_rgb16 = 0; + if (is_rgb16) *is_rgb16 = 0; switch(bits_per_pixel) { case 8: return STBI_grey; case 16: if(is_grey) return STBI_grey_alpha; - // else: fall-through + // fallthrough case 15: if(is_rgb16) *is_rgb16 = 1; - return STBI_rgb; - case 24: // fall-through + return STBI_rgb; + case 24: // fallthrough case 32: return bits_per_pixel/8; default: return 0; } @@ -5401,6 +5714,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req int RLE_repeating = 0; int read_next_pixel = 1; STBI_NOTUSED(ri); + STBI_NOTUSED(tga_x_origin); // @TODO + STBI_NOTUSED(tga_y_origin); // @TODO + + if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); // do a tiny bit of precessing if ( tga_image_type >= 8 ) @@ -5441,6 +5759,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req // do I need to load a palette? if ( tga_indexed) { + if (tga_palette_len == 0) { /* you have to have at least one entry! */ + STBI_FREE(tga_data); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + // any data to skip? (offset usually = 0) stbi__skip(s, tga_palette_start ); // load the palette @@ -5564,6 +5887,7 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req // Microsoft's C compilers happy... [8^( tga_palette_start = tga_palette_len = tga_palette_bits = tga_x_origin = tga_y_origin = 0; + STBI_NOTUSED(tga_palette_start); // OK, done return tga_data; } @@ -5648,6 +5972,9 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req h = stbi__get32be(s); w = stbi__get32be(s); + if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + // Make sure the depth is 8 bits. bitdepth = stbi__get16be(s); if (bitdepth != 8 && bitdepth != 16) @@ -5711,7 +6038,7 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req // Else if n is 128, noop. // Endloop - // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data, + // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, // which we're going to just skip. stbi__skip(s, h * channelCount * 2 ); @@ -6002,6 +6329,10 @@ static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_c x = stbi__get16be(s); y = stbi__get16be(s); + + if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)"); if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode"); @@ -6047,11 +6378,13 @@ typedef struct typedef struct { int w,h; - stbi_uc *out, *old_out; // output buffer (always 4 components) - int flags, bgindex, ratio, transparent, eflags, delay; + stbi_uc *out; // output buffer (always 4 components) + stbi_uc *background; // The current "background" as far as a gif is concerned + stbi_uc *history; + int flags, bgindex, ratio, transparent, eflags; stbi_uc pal[256][4]; stbi_uc lpal[256][4]; - stbi__gif_lzw codes[4096]; + stbi__gif_lzw codes[8192]; stbi_uc *color_table; int parse, step; int lflags; @@ -6059,6 +6392,7 @@ typedef struct int max_x, max_y; int cur_x, cur_y; int line_size; + int delay; } stbi__gif; static int stbi__gif_test_raw(stbi__context *s) @@ -6107,6 +6441,9 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in g->ratio = stbi__get8(s); g->transparent = -1; + if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments if (is_info) return 1; @@ -6134,6 +6471,7 @@ static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) { stbi_uc *p, *c; + int idx; // recurse to decode the prefixes, since the linked-list is backwards, // and working backwards through an interleaved image would be nasty @@ -6142,10 +6480,12 @@ static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) if (g->cur_y >= g->max_y) return; - p = &g->out[g->cur_x + g->cur_y]; - c = &g->color_table[g->codes[code].suffix * 4]; + idx = g->cur_x + g->cur_y; + p = &g->out[idx]; + g->history[idx / 4] = 1; - if (c[3] >= 128) { + c = &g->color_table[g->codes[code].suffix * 4]; + if (c[3] > 128) { // don't render transparent pixels; p[0] = c[2]; p[1] = c[1]; p[2] = c[0]; @@ -6219,11 +6559,16 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) stbi__skip(s,len); return g->out; } else if (code <= avail) { - if (first) return stbi__errpuc("no clear code", "Corrupt GIF"); + if (first) { + return stbi__errpuc("no clear code", "Corrupt GIF"); + } if (oldcode >= 0) { p = &g->codes[avail++]; - if (avail > 4096) return stbi__errpuc("too many codes", "Corrupt GIF"); + if (avail > 8192) { + return stbi__errpuc("too many codes", "Corrupt GIF"); + } + p->prefix = (stbi__int16) oldcode; p->first = g->codes[oldcode].first; p->suffix = (code == avail) ? p->first : g->codes[code].first; @@ -6245,62 +6590,77 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) } } -static void stbi__fill_gif_background(stbi__gif *g, int x0, int y0, int x1, int y1) -{ - int x, y; - stbi_uc *c = g->pal[g->bgindex]; - for (y = y0; y < y1; y += 4 * g->w) { - for (x = x0; x < x1; x += 4) { - stbi_uc *p = &g->out[y + x]; - p[0] = c[2]; - p[1] = c[1]; - p[2] = c[0]; - p[3] = 0; - } - } -} - // this function is designed to support animated gifs, although stb_image doesn't support it -static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp) +// two back is the image from two frames ago, used for a very specific disposal format +static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) { - int i; - stbi_uc *prev_out = 0; - - if (g->out == 0 && !stbi__gif_header(s, g, comp,0)) - return 0; // stbi__g_failure_reason set by stbi__gif_header + int dispose; + int first_frame; + int pi; + int pcount; + STBI_NOTUSED(req_comp); - if (!stbi__mad3sizes_valid(g->w, g->h, 4, 0)) - return stbi__errpuc("too large", "GIF too large"); + // on first frame, any non-written pixels get the background colour (non-transparent) + first_frame = 0; + if (g->out == 0) { + if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header + if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) + return stbi__errpuc("too large", "GIF image is too large"); + pcount = g->w * g->h; + g->out = (stbi_uc *) stbi__malloc(4 * pcount); + g->background = (stbi_uc *) stbi__malloc(4 * pcount); + g->history = (stbi_uc *) stbi__malloc(pcount); + if (!g->out || !g->background || !g->history) + return stbi__errpuc("outofmem", "Out of memory"); + + // image is treated as "transparent" at the start - ie, nothing overwrites the current background; + // background colour is only used for pixels that are not rendered first frame, after that "background" + // color refers to the color that was there the previous frame. + memset(g->out, 0x00, 4 * pcount); + memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) + memset(g->history, 0x00, pcount); // pixels that were affected previous frame + first_frame = 1; + } else { + // second frame - how do we dispose of the previous one? + dispose = (g->eflags & 0x1C) >> 2; + pcount = g->w * g->h; - prev_out = g->out; - g->out = (stbi_uc *) stbi__malloc_mad3(4, g->w, g->h, 0); - if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory"); + if ((dispose == 3) && (two_back == 0)) { + dispose = 2; // if I don't have an image to revert back to, default to the old background + } - switch ((g->eflags & 0x1C) >> 2) { - case 0: // unspecified (also always used on 1st frame) - stbi__fill_gif_background(g, 0, 0, 4 * g->w, 4 * g->w * g->h); - break; - case 1: // do not dispose - if (prev_out) memcpy(g->out, prev_out, 4 * g->w * g->h); - g->old_out = prev_out; - break; - case 2: // dispose to background - if (prev_out) memcpy(g->out, prev_out, 4 * g->w * g->h); - stbi__fill_gif_background(g, g->start_x, g->start_y, g->max_x, g->max_y); - break; - case 3: // dispose to previous - if (g->old_out) { - for (i = g->start_y; i < g->max_y; i += 4 * g->w) - memcpy(&g->out[i + g->start_x], &g->old_out[i + g->start_x], g->max_x - g->start_x); + if (dispose == 3) { // use previous graphic + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); + } } - break; + } else if (dispose == 2) { + // restore what was changed last frame to background before that frame; + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); + } + } + } else { + // This is a non-disposal case eithe way, so just + // leave the pixels as is, and they will become the new background + // 1: do not dispose + // 0: not specified. + } + + // background is what out is after the undoing of the previou frame; + memcpy( g->background, g->out, 4 * g->w * g->h ); } + // clear my history; + memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame + for (;;) { - switch (stbi__get8(s)) { + int tag = stbi__get8(s); + switch (tag) { case 0x2C: /* Image Descriptor */ { - int prev_trans = -1; stbi__int32 x, y, w, h; stbi_uc *o; @@ -6319,6 +6679,13 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i g->cur_x = g->start_x; g->cur_y = g->start_y; + // if the width of the specified rectangle is 0, that means + // we may not see *any* pixels or the image is malformed; + // to make sure this is caught, move the current y down to + // max_y (which is what out_gif_code checks). + if (w == 0) + g->cur_y = g->max_y; + g->lflags = stbi__get8(s); if (g->lflags & 0x40) { @@ -6333,19 +6700,24 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); g->color_table = (stbi_uc *) g->lpal; } else if (g->flags & 0x80) { - if (g->transparent >= 0 && (g->eflags & 0x01)) { - prev_trans = g->pal[g->transparent][3]; - g->pal[g->transparent][3] = 0; - } g->color_table = (stbi_uc *) g->pal; } else return stbi__errpuc("missing color table", "Corrupt GIF"); o = stbi__process_gif_raster(s, g); - if (o == NULL) return NULL; - - if (prev_trans != -1) - g->pal[g->transparent][3] = (stbi_uc) prev_trans; + if (!o) return NULL; + + // if this was the first frame, + pcount = g->w * g->h; + if (first_frame && (g->bgindex > 0)) { + // if first frame, any pixel not drawn to gets the background color + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi] == 0) { + g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; + memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); + } + } + } return o; } @@ -6353,19 +6725,35 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i case 0x21: // Comment Extension. { int len; - if (stbi__get8(s) == 0xF9) { // Graphic Control Extension. + int ext = stbi__get8(s); + if (ext == 0xF9) { // Graphic Control Extension. len = stbi__get8(s); if (len == 4) { g->eflags = stbi__get8(s); - g->delay = stbi__get16le(s); - g->transparent = stbi__get8(s); + g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. + + // unset old transparent + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 255; + } + if (g->eflags & 0x01) { + g->transparent = stbi__get8(s); + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 0; + } + } else { + // don't need transparent + stbi__skip(s, 1); + g->transparent = -1; + } } else { stbi__skip(s, len); break; } } - while ((len = stbi__get8(s)) != 0) + while ((len = stbi__get8(s)) != 0) { stbi__skip(s, len); + } break; } @@ -6376,28 +6764,112 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i return stbi__errpuc("unknown code", "Corrupt GIF"); } } +} - STBI_NOTUSED(req_comp); +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + if (stbi__gif_test(s)) { + int layers = 0; + stbi_uc *u = 0; + stbi_uc *out = 0; + stbi_uc *two_back = 0; + stbi__gif g; + int stride; + int out_size = 0; + int delays_size = 0; + memset(&g, 0, sizeof(g)); + if (delays) { + *delays = 0; + } + + do { + u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + + if (u) { + *x = g.w; + *y = g.h; + ++layers; + stride = g.w * g.h * 4; + + if (out) { + void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride ); + if (NULL == tmp) { + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + return stbi__errpuc("outofmem", "Out of memory"); + } + else { + out = (stbi_uc*) tmp; + out_size = layers * stride; + } + + if (delays) { + *delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers ); + delays_size = layers * sizeof(int); + } + } else { + out = (stbi_uc*)stbi__malloc( layers * stride ); + out_size = layers * stride; + if (delays) { + *delays = (int*) stbi__malloc( layers * sizeof(int) ); + delays_size = layers * sizeof(int); + } + } + memcpy( out + ((layers - 1) * stride), u, stride ); + if (layers >= 2) { + two_back = out - 2 * stride; + } + + if (delays) { + (*delays)[layers - 1U] = g.delay; + } + } + } while (u != 0); + + // free temp buffer; + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + + // do the final conversion after loading everything; + if (req_comp && req_comp != 4) + out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); + + *z = layers; + return out; + } else { + return stbi__errpuc("not GIF", "Image was not as a gif type."); + } } static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) { stbi_uc *u = 0; - stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); - memset(g, 0, sizeof(*g)); + stbi__gif g; + memset(&g, 0, sizeof(g)); STBI_NOTUSED(ri); - u = stbi__gif_load_next(s, g, comp, req_comp); + u = stbi__gif_load_next(s, &g, comp, req_comp, 0); if (u == (stbi_uc *) s) u = 0; // end of animated gif marker if (u) { - *x = g->w; - *y = g->h; + *x = g.w; + *y = g.h; + + // moved conversion to after successful load so that the same + // can be done for multiple frames. if (req_comp && req_comp != 4) - u = stbi__convert_format(u, 4, req_comp, g->w, g->h); + u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + } else if (g.out) { + // if there was an error and we allocated an image buffer, free it! + STBI_FREE(g.out); } - else if (g->out) - STBI_FREE(g->out); - STBI_FREE(g); + + // free buffers needed for multiple frame loading; + STBI_FREE(g.history); + STBI_FREE(g.background); + return u; } @@ -6521,6 +6993,9 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re token += 3; width = (int) strtol(token, NULL, 10); + if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + *x = width; *y = height; @@ -6668,7 +7143,12 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) return 0; if (x) *x = s->img_x; if (y) *y = s->img_y; - if (comp) *comp = info.ma ? 4 : 3; + if (comp) { + if (info.bpp == 24 && info.ma == 0xff000000) + *comp = 3; + else + *comp = info.ma ? 4 : 3; + } return 1; } #endif @@ -6676,7 +7156,7 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) #ifndef STBI_NO_PSD static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) { - int channelCount, dummy; + int channelCount, dummy, depth; if (!x) x = &dummy; if (!y) y = &dummy; if (!comp) comp = &dummy; @@ -6696,7 +7176,8 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) } *y = stbi__get32be(s); *x = stbi__get32be(s); - if (stbi__get16be(s) != 8) { + depth = stbi__get16be(s); + if (depth != 8 && depth != 16) { stbi__rewind( s ); return 0; } @@ -6707,6 +7188,33 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) *comp = 4; return 1; } + +static int stbi__psd_is16(stbi__context *s) +{ + int channelCount, depth; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + (void) stbi__get32be(s); + (void) stbi__get32be(s); + depth = stbi__get16be(s); + if (depth != 16) { + stbi__rewind( s ); + return 0; + } + return 1; +} #endif #ifndef STBI_NO_PIC @@ -6802,6 +7310,9 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n)) return 0; + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + *x = s->img_x; *y = s->img_y; if (comp) *comp = s->img_n; @@ -6937,6 +7448,19 @@ static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) return stbi__err("unknown image type", "Image not of any known type, or corrupt"); } +static int stbi__is_16_main(stbi__context *s) +{ + #ifndef STBI_NO_PNG + if (stbi__png_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_is16(s)) return 1; + #endif + + return 0; +} + #ifndef STBI_NO_STDIO STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) { @@ -6958,6 +7482,27 @@ STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) fseek(f,pos,SEEK_SET); return r; } + +STBIDEF int stbi_is_16_bit(char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_is_16_bit_from_file(f); + fclose(f); + return result; +} + +STBIDEF int stbi_is_16_bit_from_file(FILE *f) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__is_16_main(&s); + fseek(f,pos,SEEK_SET); + return r; +} #endif // !STBI_NO_STDIO STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) @@ -6974,10 +7519,38 @@ STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int return stbi__info_main(&s,x,y,comp); } +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__is_16_main(&s); +} + +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__is_16_main(&s); +} + #endif // STB_IMAGE_IMPLEMENTATION /* revision history: + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug + 1-bit BMP + *_is_16_bit api + avoid warnings + 2.16 (2017-07-23) all functions have 16-bit variants; + STBI_NO_STDIO works again; + compilation fixes; + fix rounding in unpremultiply; + optimize vertical flip; + disable raw_len validation; + documentation fixes 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; warning fixes; disable run-time SSE detection on gcc; uniform handling of optional "return" values; diff --git a/3rdparty/bimg/3rdparty/stb/stb_image_resize.h b/3rdparty/bimg/3rdparty/stb/stb_image_resize.h index 031ca99dc35..42a8efb13f8 100644 --- a/3rdparty/bimg/3rdparty/stb/stb_image_resize.h +++ b/3rdparty/bimg/3rdparty/stb/stb_image_resize.h @@ -1,4 +1,4 @@ -/* stb_image_resize - v0.95 - public domain image resizing +/* stb_image_resize - v0.96 - public domain image resizing by Jorge L Rodriguez (@VinoBS) - 2014 http://github.com/nothings/stb @@ -20,8 +20,8 @@ output_pixels, out_w, out_h, 0, num_channels , alpha_chan , 0) stbir_resize_uint8_srgb_edgemode( - input_pixels , in_w , in_h , 0, - output_pixels, out_w, out_h, 0, + input_pixels , in_w , in_h , 0, + output_pixels, out_w, out_h, 0, num_channels , alpha_chan , 0, STBIR_EDGE_CLAMP) // WRAP/REFLECT/ZERO @@ -159,6 +159,8 @@ Nathan Reed: warning fixes REVISIONS + 0.97 (2020-02-02) fixed warning + 0.96 (2019-03-04) fixed warnings 0.95 (2017-07-23) fixed warnings 0.94 (2017-03-18) fixed warnings 0.93 (2017-03-03) fixed bug with certain combinations of heights @@ -193,6 +195,7 @@ typedef uint16_t stbir_uint16; typedef uint32_t stbir_uint32; #endif +#ifndef STBIRDEF #ifdef STB_IMAGE_RESIZE_STATIC #define STBIRDEF static #else @@ -202,7 +205,7 @@ typedef uint32_t stbir_uint32; #define STBIRDEF extern #endif #endif - +#endif ////////////////////////////////////////////////////////////////////////////// // @@ -231,7 +234,7 @@ STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , i int num_channels); -// The following functions interpret image data as gamma-corrected sRGB. +// The following functions interpret image data as gamma-corrected sRGB. // Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel, // or otherwise provide the index of the alpha channel. Flags value // of 0 will probably do the right thing if you're not sure what @@ -304,19 +307,19 @@ typedef enum STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context); STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context); STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context); @@ -348,7 +351,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context); @@ -356,7 +359,7 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context, float x_scale, float y_scale, @@ -366,7 +369,7 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context, float s0, float t0, float s1, float t1); @@ -668,14 +671,14 @@ static const stbir_uint32 fp32_to_srgb8_tab4[104] = { 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559, 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723, }; - + static stbir_uint8 stbir__linear_to_srgb_uchar(float in) { static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps static const stbir__FP32 minval = { (127-13) << 23 }; stbir_uint32 tab,bias,scale,t; stbir__FP32 f; - + // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively. // The tests are carefully written so that NaNs map to 0, same as in the reference // implementation. @@ -683,13 +686,13 @@ static stbir_uint8 stbir__linear_to_srgb_uchar(float in) in = minval.f; if (in > almostone.f) in = almostone.f; - + // Do the table lookup and unpack bias, scale f.f = in; tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20]; bias = (tab >> 16) << 9; scale = tab & 0xffff; - + // Grab next-highest mantissa bits and perform linear interpolation t = (f.u >> 12) & 0xff; return (unsigned char) ((bias + scale*t) >> 16); @@ -1236,7 +1239,7 @@ static float* stbir__get_decode_buffer(stbir__info* stbir_info) return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels]; } -#define STBIR__DECODE(type, colorspace) ((type) * (STBIR_MAX_COLORSPACES) + (colorspace)) +#define STBIR__DECODE(type, colorspace) ((int)(type) * (STBIR_MAX_COLORSPACES) + (int)(colorspace)) static void stbir__decode_scanline(stbir__info* stbir_info, int n) { @@ -2324,8 +2327,9 @@ static int stbir__resize_allocated(stbir__info *info, if (alpha_channel < 0) flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED; - if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) + if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) { STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels); + } if (alpha_channel >= info->channels) return 0; @@ -2442,7 +2446,7 @@ static int stbir__resize_arbitrary( return 0; result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes, - output_data, output_stride_in_bytes, + output_data, output_stride_in_bytes, alpha_channel, flags, type, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required); @@ -2496,7 +2500,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -2508,7 +2512,7 @@ STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -2521,7 +2525,7 @@ STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, int num_channels, int alpha_channel, int flags, - stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -2535,7 +2539,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context) { @@ -2550,7 +2554,7 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context, float x_scale, float y_scale, @@ -2571,7 +2575,7 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, stbir_datatype datatype, int num_channels, int alpha_channel, int flags, - stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context, float s0, float t0, float s1, float t1) @@ -2590,38 +2594,38 @@ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. -Anyone is free to copy, modify, publish, use, compile, sell, or distribute this -software, either in source code form or as a compiled binary, for any purpose, +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. -In jurisdictions that recognize copyright laws, the author or authors of this -software dedicate any and all copyright interest in the software to the public -domain. We make this dedication for the benefit of the public at large and to -the detriment of our heirs and successors. We intend this dedication to be an -overt act of relinquishment in perpetuity of all present and future rights to +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ diff --git a/3rdparty/bimg/3rdparty/stb/stb_image_write.h b/3rdparty/bimg/3rdparty/stb/stb_image_write.h index df623393dd0..95943eb607b 100644 --- a/3rdparty/bimg/3rdparty/stb/stb_image_write.h +++ b/3rdparty/bimg/3rdparty/stb/stb_image_write.h @@ -1,5 +1,5 @@ -/* stb_image_write - v1.05 - public domain - http://nothings.org/stb/stb_image_write.h - writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010-2015 +/* stb_image_write - v1.15 - public domain - http://nothings.org/stb + writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015 no warranty implied; use at your own risk Before #including, @@ -12,41 +12,64 @@ ABOUT: - This header file is a library for writing images to C stdio. It could be - adapted to write to memory or a general streaming interface; let me know. + This header file is a library for writing images to C stdio or a callback. The PNG output is not optimal; it is 20-50% larger than the file - written by a decent optimizing implementation. This library is designed - for source code compactness and simplicity, not optimal image file size - or run-time performance. + written by a decent optimizing implementation; though providing a custom + zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that. + This library is designed for source code compactness and simplicity, + not optimal image file size or run-time performance. BUILDING: You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h. You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace malloc,realloc,free. - You can define STBIW_MEMMOVE() to replace memmove() + You can #define STBIW_MEMMOVE() to replace memmove() + You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function + for PNG compression (instead of the builtin one), it must have the following signature: + unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality); + The returned data will be freed with STBIW_FREE() (free() by default), + so it must be heap allocated with STBIW_MALLOC() (malloc() by default), + +UNICODE: + + If compiling for Windows and you wish to use Unicode filenames, compile + with + #define STBIW_WINDOWS_UTF8 + and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert + Windows wchar_t filenames to utf8. USAGE: - There are four functions, one for each image file format: + There are five functions, one for each image file format: int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); + int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality); int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); - There are also four equivalent functions that use an arbitrary write function. You are + void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically + + There are also five equivalent functions that use an arbitrary write function. You are expected to open/close your file-equivalent before and after calling these: int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); + int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); where the callback is: void stbi_write_func(void *context, void *data, int size); + You can configure it with these global variables: + int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE + int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression + int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode + + You can define STBI_WRITE_NO_STDIO to disable the file variant of these functions, so the library will not use stdio.h at all. However, this will also disable HDR writing, because it requires stdio for formatted output. @@ -73,6 +96,9 @@ USAGE: writer, both because it is in BGR order and because it may have padding at the end of the line.) + PNG allows you to set the deflate compression level by setting the global + variable 'stbi_write_png_compression_level' (it defaults to 8). + HDR expects linear float data. Since the format is always 32-bit rgb(e) data, alpha (if provided) is discarded, and for monochrome data it is replicated across all three channels. @@ -80,20 +106,23 @@ USAGE: TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed data, set the global variable 'stbi_write_tga_with_rle' to 0. + JPEG does ignore alpha channels in input data; quality is between 1 and 100. + Higher quality looks better but results in a bigger image. + JPEG baseline (no JPEG progressive). + CREDITS: - PNG/BMP/TGA - Sean Barrett - HDR - Baldur Karlsson - TGA monochrome: - Jean-Sebastien Guay - misc enhancements: - Tim Kelsey - TGA RLE - Alan Hickman - initial file IO callback implementation - Emmanuel Julien + + Sean Barrett - PNG/BMP/TGA + Baldur Karlsson - HDR + Jean-Sebastien Guay - TGA monochrome + Tim Kelsey - misc enhancements + Alan Hickman - TGA RLE + Emmanuel Julien - initial file IO callback implementation + Jon Olick - original jo_jpeg.cpp code + Daniel Gibson - integrate JPEG, allow external zlib + Aarni Koskela - allow choosing PNG filter + bugfixes: github:Chribba Guillaume Chereau @@ -105,7 +134,13 @@ CREDITS: Thatcher Ulrich github:poppolopoppo Patrick Boettcher - + github:xeekworx + Cap Petschulat + Simon Rodriguez + Ivan Tikhonov + github:ignotion + Adam Schackart + LICENSE See end of file for license information. @@ -115,15 +150,25 @@ LICENSE #ifndef INCLUDE_STB_IMAGE_WRITE_H #define INCLUDE_STB_IMAGE_WRITE_H -#ifdef __cplusplus -extern "C" { -#endif +#include <stdlib.h> +// if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline' +#ifndef STBIWDEF #ifdef STB_IMAGE_WRITE_STATIC -#define STBIWDEF static +#define STBIWDEF static +#else +#ifdef __cplusplus +#define STBIWDEF extern "C" #else -#define STBIWDEF extern +#define STBIWDEF extern +#endif +#endif +#endif + +#ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations extern int stbi_write_tga_with_rle; +extern int stbi_write_png_compression_level; +extern int stbi_write_force_png_filter; #endif #ifndef STBI_WRITE_NO_STDIO @@ -131,6 +176,11 @@ STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); +STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); + +#ifdef STBI_WINDOWS_UTF8 +STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); +#endif #endif typedef void stbi_write_func(void *context, void *data, int size); @@ -139,10 +189,9 @@ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); +STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); -#ifdef __cplusplus -} -#endif +STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean); #endif//INCLUDE_STB_IMAGE_WRITE_H @@ -197,10 +246,29 @@ STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) +#ifdef STB_IMAGE_WRITE_STATIC +static int stbi_write_png_compression_level = 8; +static int stbi_write_tga_with_rle = 1; +static int stbi_write_force_png_filter = -1; +#else +int stbi_write_png_compression_level = 8; +int stbi_write_tga_with_rle = 1; +int stbi_write_force_png_filter = -1; +#endif + +static int stbi__flip_vertically_on_write = 0; + +STBIWDEF void stbi_flip_vertically_on_write(int flag) +{ + stbi__flip_vertically_on_write = flag; +} + typedef struct { stbi_write_func *func; void *context; + unsigned char buffer[64]; + int buf_used; } stbi__write_context; // initialize a callback-based context @@ -217,9 +285,52 @@ static void stbi__stdio_write(void *context, void *data, int size) fwrite(data,1,size,(FILE*) context); } +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) +#ifdef __cplusplus +#define STBIW_EXTERN extern "C" +#else +#define STBIW_EXTERN extern +#endif +STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); +STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); + +STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) +{ + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); +} +#endif + +static FILE *stbiw__fopen(char const *filename, char const *mode) +{ + FILE *f; +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) + wchar_t wMode[64]; + wchar_t wFilename[1024]; + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) + return 0; + + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) + return 0; + +#if _MSC_VER >= 1400 + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; +#else + f = _wfopen(wFilename, wMode); +#endif + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != fopen_s(&f, filename, mode)) + f=0; +#else + f = fopen(filename, mode); +#endif + return f; +} + static int stbi__start_write_file(stbi__write_context *s, const char *filename) { - FILE *f = fopen(filename, "wb"); + FILE *f = stbiw__fopen(filename, "wb"); stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); return f != NULL; } @@ -234,12 +345,6 @@ static void stbi__end_write_file(stbi__write_context *s) typedef unsigned int stbiw_uint32; typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; -#ifdef STB_IMAGE_WRITE_STATIC -static int stbi_write_tga_with_rle = 1; -#else -int stbi_write_tga_with_rle = 1; -#endif - static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) { while (*fmt) { @@ -277,11 +382,36 @@ static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) va_end(v); } +static void stbiw__write_flush(stbi__write_context *s) +{ + if (s->buf_used) { + s->func(s->context, &s->buffer, s->buf_used); + s->buf_used = 0; + } +} + +static void stbiw__putc(stbi__write_context *s, unsigned char c) +{ + s->func(s->context, &c, 1); +} + +static void stbiw__write1(stbi__write_context *s, unsigned char a) +{ + if (s->buf_used + 1 > sizeof(s->buffer)) + stbiw__write_flush(s); + s->buffer[s->buf_used++] = a; +} + static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) { - unsigned char arr[3]; - arr[0] = a, arr[1] = b, arr[2] = c; - s->func(s->context, arr, 3); + int n; + if (s->buf_used + 3 > sizeof(s->buffer)) + stbiw__write_flush(s); + n = s->buf_used; + s->buf_used = n+3; + s->buffer[n+0] = a; + s->buffer[n+1] = b; + s->buffer[n+2] = c; } static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) @@ -290,7 +420,7 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in int k; if (write_alpha < 0) - s->func(s->context, &d[comp - 1], 1); + stbiw__write1(s, d[comp - 1]); switch (comp) { case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case @@ -298,7 +428,7 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in if (expand_mono) stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp else - s->func(s->context, d, 1); // monochrome TGA + stbiw__write1(s, d[0]); // monochrome TGA break; case 4: if (!write_alpha) { @@ -314,7 +444,7 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in break; } if (write_alpha > 0) - s->func(s->context, &d[comp - 1], 1); + stbiw__write1(s, d[comp - 1]); } static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) @@ -325,16 +455,21 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i if (y <= 0) return; - if (vdir < 0) - j_end = -1, j = y-1; - else - j_end = y, j = 0; + if (stbi__flip_vertically_on_write) + vdir *= -1; + + if (vdir < 0) { + j_end = -1; j = y-1; + } else { + j_end = y; j = 0; + } for (; j != j_end; j += vdir) { for (i=0; i < x; ++i) { unsigned char *d = (unsigned char *) data + (j*x+i)*comp; stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); } + stbiw__write_flush(s); s->func(s->context, &zero, scanline_pad); } } @@ -364,7 +499,7 @@ static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, c STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; stbi__start_write_callbacks(&s, func, context); return stbi_write_bmp_core(&s, x, y, comp, data); } @@ -372,7 +507,7 @@ STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, #ifndef STBI_WRITE_NO_STDIO STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; if (stbi__start_write_file(&s,filename)) { int r = stbi_write_bmp_core(&s, x, y, comp, data); stbi__end_write_file(&s); @@ -396,11 +531,21 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); } else { int i,j,k; + int jend, jdir; stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); - for (j = y - 1; j >= 0; --j) { - unsigned char *row = (unsigned char *) data + j * x * comp; + if (stbi__flip_vertically_on_write) { + j = 0; + jend = y; + jdir = 1; + } else { + j = y-1; + jend = -1; + jdir = -1; + } + for (; j != jend; j += jdir) { + unsigned char *row = (unsigned char *) data + j * x * comp; int len; for (i = 0; i < x; i += len) { @@ -435,32 +580,33 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v if (diff) { unsigned char header = STBIW_UCHAR(len - 1); - s->func(s->context, &header, 1); + stbiw__write1(s, header); for (k = 0; k < len; ++k) { stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); } } else { unsigned char header = STBIW_UCHAR(len - 129); - s->func(s->context, &header, 1); + stbiw__write1(s, header); stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); } } } + stbiw__write_flush(s); } return 1; } -int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) +STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; stbi__start_write_callbacks(&s, func, context); return stbi_write_tga_core(&s, x, y, comp, (void *) data); } #ifndef STBI_WRITE_NO_STDIO -int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) +STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; if (stbi__start_write_file(&s,filename)) { int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); stbi__end_write_file(&s); @@ -476,7 +622,7 @@ int stbi_write_tga(char const *filename, int x, int y, int comp, const void *dat #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) -void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) +static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) { int exponent; float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); @@ -493,7 +639,7 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) } } -void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) +static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) { unsigned char lengthbyte = STBIW_UCHAR(length+128); STBIW_ASSERT(length+128 <= 255); @@ -501,7 +647,7 @@ void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char dat s->func(s->context, &databyte, 1); } -void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) +static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) { unsigned char lengthbyte = STBIW_UCHAR(length); STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code @@ -509,7 +655,7 @@ void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *d s->func(s->context, data, length); } -void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) +static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) { unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; unsigned char rgbe[4]; @@ -610,27 +756,31 @@ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, f char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; s->func(s->context, header, sizeof(header)-1); +#ifdef __STDC_WANT_SECURE_LIB__ + len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); +#else len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); +#endif s->func(s->context, buffer, len); for(i=0; i < y; i++) - stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*i*x); + stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)); STBIW_FREE(scratch); return 1; } } -int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) +STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; stbi__start_write_callbacks(&s, func, context); return stbi_write_hdr_core(&s, x, y, comp, (float *) data); } #ifndef STBI_WRITE_NO_STDIO -int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) +STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) { - stbi__write_context s; + stbi__write_context s = { 0 }; if (stbi__start_write_file(&s,filename)) { int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); stbi__end_write_file(&s); @@ -646,8 +796,9 @@ int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *da // PNG writer // +#ifndef STBIW_ZLIB_COMPRESS // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() -#define stbiw__sbraw(a) ((int *) (a) - 2) +#define stbiw__sbraw(a) ((int *) (void *) (a) - 2) #define stbiw__sbm(a) stbiw__sbraw(a)[0] #define stbiw__sbn(a) stbiw__sbraw(a)[1] @@ -726,8 +877,14 @@ static unsigned int stbiw__zhash(unsigned char *data) #define stbiw__ZHASH 16384 -unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) +#endif // STBIW_ZLIB_COMPRESS + +STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) { +#ifdef STBIW_ZLIB_COMPRESS + // user provided a zlib compress implementation, use that + return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality); +#else // use builtin static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 }; static unsigned char lengtheb[]= { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 }; @@ -735,7 +892,9 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l unsigned int bitbuf=0; int i,j, bitcount=0; unsigned char *out = NULL; - unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**)); + unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(unsigned char**)); + if (hash_table == NULL) + return NULL; if (quality < 5) quality = 5; stbiw__sbpush(out, 0x78); // DEFLATE 32K window @@ -756,7 +915,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l for (j=0; j < n; ++j) { if (hlist[j]-data > i-32768) { // if entry lies within window int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i); - if (d >= best) best=d,bestloc=hlist[j]; + if (d >= best) { best=d; bestloc=hlist[j]; } } } // when hash table entry is too long, delete half the entries @@ -815,8 +974,8 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l int blocklen = (int) (data_len % 5552); j=0; while (j < data_len) { - for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1; - s1 %= 65521, s2 %= 65521; + for (i=0; i < blocklen; ++i) { s1 += data[j+i]; s2 += s1; } + s1 %= 65521; s2 %= 65521; j += blocklen; blocklen = 5552; } @@ -829,10 +988,14 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l // make returned pointer freeable STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len); return (unsigned char *) stbiw__sbraw(out); +#endif // STBIW_ZLIB_COMPRESS } static unsigned int stbiw__crc32(unsigned char *buffer, int len) { +#ifdef STBIW_CRC32 + return STBIW_CRC32(buffer, len); +#else static unsigned int crc_table[256] = { 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, @@ -874,6 +1037,7 @@ static unsigned int stbiw__crc32(unsigned char *buffer, int len) for (i=0; i < len; ++i) crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; return ~crc; +#endif } #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4) @@ -895,61 +1059,91 @@ static unsigned char stbiw__paeth(int a, int b, int c) } // @OPTIMIZE: provide an option that always forces left-predict or paeth predict -unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) +static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer) +{ + static int mapping[] = { 0,1,2,3,4 }; + static int firstmap[] = { 0,1,0,5,6 }; + int *mymap = (y != 0) ? mapping : firstmap; + int i; + int type = mymap[filter_type]; + unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y); + int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes; + + if (type==0) { + memcpy(line_buffer, z, width*n); + return; + } + + // first loop isn't optimized since it's just one pixel + for (i = 0; i < n; ++i) { + switch (type) { + case 1: line_buffer[i] = z[i]; break; + case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break; + case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break; + case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-signed_stride],0)); break; + case 5: line_buffer[i] = z[i]; break; + case 6: line_buffer[i] = z[i]; break; + } + } + switch (type) { + case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break; + case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break; + case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break; + case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break; + case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break; + case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; + } +} + +STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) { + int force_filter = stbi_write_force_png_filter; int ctype[5] = { -1, 0, 4, 2, 6 }; unsigned char sig[8] = { 137,80,78,71,13,10,26,10 }; unsigned char *out,*o, *filt, *zlib; signed char *line_buffer; - int i,j,k,p,zlen; + int j,zlen; if (stride_bytes == 0) stride_bytes = x * n; + if (force_filter >= 5) { + force_filter = -1; + } + filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0; line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; } for (j=0; j < y; ++j) { - static int mapping[] = { 0,1,2,3,4 }; - static int firstmap[] = { 0,1,0,5,6 }; - int *mymap = (j != 0) ? mapping : firstmap; - int best = 0, bestval = 0x7fffffff; - for (p=0; p < 2; ++p) { - for (k= p?best:0; k < 5; ++k) { // @TODO: clarity: rewrite this to go 0..5, and 'continue' the unwanted ones during 2nd pass - int type = mymap[k],est=0; - unsigned char *z = pixels + stride_bytes*j; - for (i=0; i < n; ++i) - switch (type) { - case 0: line_buffer[i] = z[i]; break; - case 1: line_buffer[i] = z[i]; break; - case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break; - case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break; - case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break; - case 5: line_buffer[i] = z[i]; break; - case 6: line_buffer[i] = z[i]; break; - } - for (i=n; i < x*n; ++i) { - switch (type) { - case 0: line_buffer[i] = z[i]; break; - case 1: line_buffer[i] = z[i] - z[i-n]; break; - case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break; - case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break; - case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break; - case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break; - case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; - } - } - if (p) break; - for (i=0; i < x*n; ++i) + int filter_type; + if (force_filter > -1) { + filter_type = force_filter; + stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer); + } else { // Estimate the best filter by running through all of them: + int best_filter = 0, best_filter_val = 0x7fffffff, est, i; + for (filter_type = 0; filter_type < 5; filter_type++) { + stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer); + + // Estimate the entropy of the line using this filter; the less, the better. + est = 0; + for (i = 0; i < x*n; ++i) { est += abs((signed char) line_buffer[i]); - if (est < bestval) { bestval = est; best = k; } + } + if (est < best_filter_val) { + best_filter_val = est; + best_filter = filter_type; + } + } + if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it + stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer); + filter_type = best_filter; } } - // when we get here, best contains the filter type, and line_buffer contains the data - filt[j*(x*n+1)] = (unsigned char) best; + // when we get here, filter_type contains the filter type, and line_buffer contains the data + filt[j*(x*n+1)] = (unsigned char) filter_type; STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n); } STBIW_FREE(line_buffer); - zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory + zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level); STBIW_FREE(filt); if (!zlib) return 0; @@ -992,9 +1186,10 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const { FILE *f; int len; - unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); + unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; - f = fopen(filename, "wb"); + + f = stbiw__fopen(filename, "wb"); if (!f) { STBIW_FREE(png); return 0; } fwrite(png, 1, len, f); fclose(f); @@ -1006,16 +1201,419 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) { int len; - unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); + unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; func(context, png, len); STBIW_FREE(png); return 1; } + +/* *************************************************************************** + * + * JPEG writer + * + * This is based on Jon Olick's jo_jpeg.cpp: + * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html + */ + +static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18, + 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 }; + +static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) { + int bitBuf = *bitBufP, bitCnt = *bitCntP; + bitCnt += bs[1]; + bitBuf |= bs[0] << (24 - bitCnt); + while(bitCnt >= 8) { + unsigned char c = (bitBuf >> 16) & 255; + stbiw__putc(s, c); + if(c == 255) { + stbiw__putc(s, 0); + } + bitBuf <<= 8; + bitCnt -= 8; + } + *bitBufP = bitBuf; + *bitCntP = bitCnt; +} + +static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) { + float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p; + float z1, z2, z3, z4, z5, z11, z13; + + float tmp0 = d0 + d7; + float tmp7 = d0 - d7; + float tmp1 = d1 + d6; + float tmp6 = d1 - d6; + float tmp2 = d2 + d5; + float tmp5 = d2 - d5; + float tmp3 = d3 + d4; + float tmp4 = d3 - d4; + + // Even part + float tmp10 = tmp0 + tmp3; // phase 2 + float tmp13 = tmp0 - tmp3; + float tmp11 = tmp1 + tmp2; + float tmp12 = tmp1 - tmp2; + + d0 = tmp10 + tmp11; // phase 3 + d4 = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * 0.707106781f; // c4 + d2 = tmp13 + z1; // phase 5 + d6 = tmp13 - z1; + + // Odd part + tmp10 = tmp4 + tmp5; // phase 2 + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + // The rotator is modified from fig 4-8 to avoid extra negations. + z5 = (tmp10 - tmp12) * 0.382683433f; // c6 + z2 = tmp10 * 0.541196100f + z5; // c2-c6 + z4 = tmp12 * 1.306562965f + z5; // c2+c6 + z3 = tmp11 * 0.707106781f; // c4 + + z11 = tmp7 + z3; // phase 5 + z13 = tmp7 - z3; + + *d5p = z13 + z2; // phase 6 + *d3p = z13 - z2; + *d1p = z11 + z4; + *d7p = z11 - z4; + + *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6; +} + +static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) { + int tmp1 = val < 0 ? -val : val; + val = val < 0 ? val-1 : val; + bits[1] = 1; + while(tmp1 >>= 1) { + ++bits[1]; + } + bits[0] = val & ((1<<bits[1])-1); +} + +static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) { + const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] }; + const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] }; + int dataOff, i, j, n, diff, end0pos, x, y; + int DU[64]; + + // DCT rows + for(dataOff=0, n=du_stride*8; dataOff<n; dataOff+=du_stride) { + stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+1], &CDU[dataOff+2], &CDU[dataOff+3], &CDU[dataOff+4], &CDU[dataOff+5], &CDU[dataOff+6], &CDU[dataOff+7]); + } + // DCT columns + for(dataOff=0; dataOff<8; ++dataOff) { + stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+du_stride], &CDU[dataOff+du_stride*2], &CDU[dataOff+du_stride*3], &CDU[dataOff+du_stride*4], + &CDU[dataOff+du_stride*5], &CDU[dataOff+du_stride*6], &CDU[dataOff+du_stride*7]); + } + // Quantize/descale/zigzag the coefficients + for(y = 0, j=0; y < 8; ++y) { + for(x = 0; x < 8; ++x,++j) { + float v; + i = y*du_stride+x; + v = CDU[i]*fdtbl[j]; + // DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f)); + // ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway? + DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? v - 0.5f : v + 0.5f); + } + } + + // Encode DC + diff = DU[0] - DC; + if (diff == 0) { + stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[0]); + } else { + unsigned short bits[2]; + stbiw__jpg_calcBits(diff, bits); + stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[bits[1]]); + stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); + } + // Encode ACs + end0pos = 63; + for(; (end0pos>0)&&(DU[end0pos]==0); --end0pos) { + } + // end0pos = first element in reverse order !=0 + if(end0pos == 0) { + stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); + return DU[0]; + } + for(i = 1; i <= end0pos; ++i) { + int startpos = i; + int nrzeroes; + unsigned short bits[2]; + for (; DU[i]==0 && i<=end0pos; ++i) { + } + nrzeroes = i-startpos; + if ( nrzeroes >= 16 ) { + int lng = nrzeroes>>4; + int nrmarker; + for (nrmarker=1; nrmarker <= lng; ++nrmarker) + stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes); + nrzeroes &= 15; + } + stbiw__jpg_calcBits(DU[i], bits); + stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]); + stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); + } + if(end0pos != 63) { + stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); + } + return DU[0]; +} + +static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) { + // Constants that don't pollute global namespace + static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0}; + static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; + static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d}; + static const unsigned char std_ac_luminance_values[] = { + 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, + 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, + 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, + 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, + 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, + 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, + 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa + }; + static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; + static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; + static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77}; + static const unsigned char std_ac_chrominance_values[] = { + 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, + 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, + 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, + 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, + 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, + 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, + 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa + }; + // Huffman tables + static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}}; + static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}}; + static const unsigned short YAC_HT[256][2] = { + {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0}, + {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} + }; + static const unsigned short UVAC_HT[256][2] = { + {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, + {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0}, + {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} + }; + static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22, + 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99}; + static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99, + 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}; + static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, + 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f }; + + int row, col, i, k, subsample; + float fdtbl_Y[64], fdtbl_UV[64]; + unsigned char YTable[64], UVTable[64]; + + if(!data || !width || !height || comp > 4 || comp < 1) { + return 0; + } + + quality = quality ? quality : 90; + subsample = quality <= 90 ? 1 : 0; + quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; + quality = quality < 50 ? 5000 / quality : 200 - quality * 2; + + for(i = 0; i < 64; ++i) { + int uvti, yti = (YQT[i]*quality+50)/100; + YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti); + uvti = (UVQT[i]*quality+50)/100; + UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); + } + + for(row = 0, k = 0; row < 8; ++row) { + for(col = 0; col < 8; ++col, ++k) { + fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); + fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); + } + } + + // Write Headers + { + static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 }; + static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 }; + const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width), + 3,1,(unsigned char)(subsample?0x22:0x11),0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 }; + s->func(s->context, (void*)head0, sizeof(head0)); + s->func(s->context, (void*)YTable, sizeof(YTable)); + stbiw__putc(s, 1); + s->func(s->context, UVTable, sizeof(UVTable)); + s->func(s->context, (void*)head1, sizeof(head1)); + s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1); + s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values)); + stbiw__putc(s, 0x10); // HTYACinfo + s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1); + s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values)); + stbiw__putc(s, 1); // HTUDCinfo + s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1); + s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values)); + stbiw__putc(s, 0x11); // HTUACinfo + s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1); + s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values)); + s->func(s->context, (void*)head2, sizeof(head2)); + } + + // Encode 8x8 macroblocks + { + static const unsigned short fillBits[] = {0x7F, 7}; + int DCY=0, DCU=0, DCV=0; + int bitBuf=0, bitCnt=0; + // comp == 2 is grey+alpha (alpha is ignored) + int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0; + const unsigned char *dataR = (const unsigned char *)data; + const unsigned char *dataG = dataR + ofsG; + const unsigned char *dataB = dataR + ofsB; + int x, y, pos; + if(subsample) { + for(y = 0; y < height; y += 16) { + for(x = 0; x < width; x += 16) { + float Y[256], U[256], V[256]; + for(row = y, pos = 0; row < y+16; ++row) { + // row >= height => use last input row + int clamped_row = (row < height) ? row : height - 1; + int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; + for(col = x; col < x+16; ++col, ++pos) { + // if col >= width => use pixel from last input column + int p = base_p + ((col < width) ? col : (width-1))*comp; + float r = dataR[p], g = dataG[p], b = dataB[p]; + Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; + U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; + V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; + } + } + DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+0, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); + DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+8, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); + DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+128, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); + DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+136, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); + + // subsample U,V + { + float subU[64], subV[64]; + int yy, xx; + for(yy = 0, pos = 0; yy < 8; ++yy) { + for(xx = 0; xx < 8; ++xx, ++pos) { + int j = yy*32+xx*2; + subU[pos] = (U[j+0] + U[j+1] + U[j+16] + U[j+17]) * 0.25f; + subV[pos] = (V[j+0] + V[j+1] + V[j+16] + V[j+17]) * 0.25f; + } + } + DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); + DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); + } + } + } + } else { + for(y = 0; y < height; y += 8) { + for(x = 0; x < width; x += 8) { + float Y[64], U[64], V[64]; + for(row = y, pos = 0; row < y+8; ++row) { + // row >= height => use last input row + int clamped_row = (row < height) ? row : height - 1; + int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; + for(col = x; col < x+8; ++col, ++pos) { + // if col >= width => use pixel from last input column + int p = base_p + ((col < width) ? col : (width-1))*comp; + float r = dataR[p], g = dataG[p], b = dataB[p]; + Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; + U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; + V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; + } + } + + DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, YDC_HT, YAC_HT); + DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); + DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); + } + } + } + + // Do the bit alignment of the EOI marker + stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits); + } + + // EOI + stbiw__putc(s, 0xFF); + stbiw__putc(s, 0xD9); + + return 1; +} + +STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) +{ + stbi__write_context s = { 0 }; + stbi__start_write_callbacks(&s, func, context); + return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); +} + + +#ifndef STBI_WRITE_NO_STDIO +STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) +{ + stbi__write_context s = { 0 }; + if (stbi__start_write_file(&s,filename)) { + int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); + stbi__end_write_file(&s); + return r; + } else + return 0; +} +#endif + #endif // STB_IMAGE_WRITE_IMPLEMENTATION /* Revision history + 1.14 (2020-02-02) updated JPEG writer to downsample chroma channels + 1.13 + 1.12 + 1.11 (2019-08-11) + + 1.10 (2019-02-07) + support utf8 filenames in Windows; fix warnings and platform ifdefs + 1.09 (2018-02-11) + fix typo in zlib quality API, improve STB_I_W_STATIC in C++ + 1.08 (2018-01-29) + add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter + 1.07 (2017-07-24) + doc fix + 1.06 (2017-07-23) + writing JPEG (using Jon Olick's code) + 1.05 ??? 1.04 (2017-03-03) monochrome BMP expansion 1.03 ??? @@ -1055,38 +1653,38 @@ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. -Anyone is free to copy, modify, publish, use, compile, sell, or distribute this -software, either in source code form or as a compiled binary, for any purpose, +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. -In jurisdictions that recognize copyright laws, the author or authors of this -software dedicate any and all copyright interest in the software to the public -domain. We make this dedication for the benefit of the public at large and to -the detriment of our heirs and successors. We intend this dedication to be an -overt act of relinquishment in perpetuity of all present and future rights to +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ diff --git a/3rdparty/bimg/3rdparty/tinyexr/README.md b/3rdparty/bimg/3rdparty/tinyexr/README.md index aea03fd97a4..b570c4567ff 100644 --- a/3rdparty/bimg/3rdparty/tinyexr/README.md +++ b/3rdparty/bimg/3rdparty/tinyexr/README.md @@ -1,5 +1,7 @@ # Tiny OpenEXR image library. +[](https://lgtm.com/projects/g/syoyo/tinyexr/alerts/) +  [](https://ci.appveyor.com/project/syoyo/tinyexr/branch/master) @@ -8,38 +10,106 @@ [](https://scan.coverity.com/projects/5827) -`tinyexr` is a small, single header-only library to load and save OpenEXR(.exr) images. -`tinyexr` is written in portable C++(no library dependency except for STL), thus `tinyexr` is good to embed into your application. +`tinyexr` is a small, single header-only library to load and save OpenEXR (.exr) images. +`tinyexr` is written in portable C++ (no library dependency except for STL), thus `tinyexr` is good to embed into your application. To use `tinyexr`, simply copy `tinyexr.h` into your project. -`tinyexr` currently supports: - -* OpenEXR version 1.x. -* Normal image - * Scanline format. - * Uncompress("compress" = 0), ZIPS("compress" = 2), ZIP compression("compress" = 3) and PIZ compression("compress" = 4). - * Half/Uint/Float pixel type. - * Custom attributes(up to 128) -* Deep image - * Scanline format. - * ZIPS compression("compress" = 2). - * Half, float pixel type. -* Litte endian machine. -* Limited support for big endian machine. - * read/write normal image. +# Features + +Current status of `tinyexr` is: + +- OpenEXR v1 image + - [x] Scanline format + - [x] Tiled format + - [x] Tile format with no LoD (load). + - [x] Tile format with LoD (load). + - [x] Tile format with no LoD (save). + - [x] Tile format with LoD (save). + - [x] Custom attributes +- OpenEXR v2 image + - [ ] Multipart format + - [x] Load multi-part image + - [x] Save multi-part image + - [ ] Load multi-part deep image + - [ ] Save multi-part deep image +- OpenEXR v2 deep image + - [x] Loading scanline + ZIPS + HALF or FLOAT pixel type. +- Compression + - [x] NONE + - [x] RLE + - [x] ZIP + - [x] ZIPS + - [x] PIZ + - [x] ZFP (tinyexr extension) + - [ ] B44? + - [ ] B44A? + - [ ] PIX24? +- Line order. + - [x] Increasing, decreasing (load) + - [ ] Random? + - [x] Increasing (save) + - [ ] decreasing (save) +- Pixel format (UINT, FLOAT). + - [x] UINT, FLOAT (load) + - [x] UINT, FLOAT (deep load) + - [x] UINT, FLOAT (save) + - [ ] UINT, FLOAT (deep save) +- Support for big endian machine. + - [x] Loading scanline image + - [x] Saving scanline image + - [x] Loading multi-part channel EXR (not tested) + - [x] Saving multi-part channel EXR (not tested) + - [ ] Loading deep image + - [ ] Saving deep image +- Optimization + - [x] C++11 thread loading + - [ ] C++11 thread saving + - [ ] ISPC? + - [x] OpenMP multi-threading in EXR loading. + - [x] OpenMP multi-threading in EXR saving. + - [ ] OpenMP multi-threading in deep image loading. + - [ ] OpenMP multi-threading in deep image saving. * C interface. - * You can easily write language bindings(e.g. golang) -* EXR saving - * with ZIP compression. -* JavaScript library - * Through emscripten. + * You can easily write language bindings (e.g. golang) + +# Supported platform + +* [x] x86-64 + * [x] Windows 7 or later + * [x] Linux(posix) system + * [x] macOS +* [x] AARCH64 + * [x] aarch64 linux(e.g. Raspberry Pi) + * [x] Android + * [x] iOS + * [ ] macOS(Should work) +* [ ] RISC-V(Should work) +* [x] Big endian machine(not maintained, but should work) + * SPARC, PowerPC, ... +* [x] WebAssembly(JavaScript) + * Loader only(See ) +* [x] Python binding + * Loader only https://pypi.org/project/pytinyexr/ + +# Requirements + +* C++ compiler(C++11 recommended. C++03 may work) + +# Use case + +## New TinyEXR (v0.9.5+) + +* Godot. Multi-platform 2D and 3D game engine https://godotengine.org/ +* Filament. PBR engine(used in a converter tool). https://github.com/google/filament +* PyEXR. Loading OpenEXR (.exr) images using Python. https://github.com/ialhashim/PyEXR +* The-Forge. The Forge Cross-Platform Rendering Framework PC, Linux, Ray Tracing, macOS / iOS, Android, XBOX, PS4 https://github.com/ConfettiFX/The-Forge +* Your project here! -# Use case +## Older TinyEXR (v0.9.0) * mallie https://github.com/lighttransport/mallie -* PBRT v3 https://github.com/mmp/pbrt-v3 * Cinder 0.9.0 https://libcinder.org/notes/v0.9.0 -* Piccante(develop branch) http://piccantelib.net/ +* Piccante (develop branch) http://piccantelib.net/ * Your project here! ## Examples @@ -47,79 +117,240 @@ To use `tinyexr`, simply copy `tinyexr.h` into your project. * [examples/deepview/](examples/deepview) Deep image view * [examples/rgbe2exr/](examples/rgbe2exr) .hdr to EXR converter * [examples/exr2rgbe/](examples/exr2rgbe) EXR to .hdr converter +* [examples/ldr2exr/](examples/exr2rgbe) LDR to EXR converter +* [examples/exr2ldr/](examples/exr2ldr) EXR to LDR converter +* [examples/exr2fptiff/](examples/exr2fptiff) EXR to 32bit floating point TIFF converter + * for 32bit floating point TIFF to EXR convert, see https://github.com/syoyo/tinydngloader/tree/master/examples/fptiff2exr +* [examples/cube2longlat/](examples/cube2longlat) Cubemap to longlat (equirectangler) converter + +## Experimental + +* [experimental/js/](experimental/js) JavaScript port using Emscripten ## Usage NOTE: **API is still subject to change**. See the source code for details. -Include `tinyexr.h` with `TINYEXR_IMPLEMENTATION` flag(do this only for **one** .cc file). +Include `tinyexr.h` with `TINYEXR_IMPLEMENTATION` flag (do this only for **one** .cc file). -``` +```cpp +//Please include your own zlib-compatible API header before +//including `tinyexr.h` when you disable `TINYEXR_USE_MINIZ` +//#define TINYEXR_USE_MINIZ 0 +//#include "zlib.h" #define TINYEXR_IMPLEMENTATION #include "tinyexr.h" ``` -Quickly reading RGB(A) EXR file. +### Compile flags -``` +* `TINYEXR_USE_MINIZ` Use embedded miniz (default = 1). Please include `zlib.h` header (before `tinyexr.h`) if you disable miniz support. +* `TINYEXR_USE_PIZ` Enable PIZ compression support (default = 1) +* `TINYEXR_USE_ZFP` Enable ZFP compression supoort (TinyEXR extension, default = 0) +* `TINYEXR_USE_THREAD` Enable threaded loading using C++11 thread (Requires C++11 compiler, default = 0) +* `TINYEXR_USE_OPENMP` Enable OpenMP threading support (default = 1 if `_OPENMP` is defined) + * Use `TINYEXR_USE_OPENMP=0` to force disable OpenMP code path even if OpenMP is available/enabled in the compiler. + +### Quickly reading RGB(A) EXR file. + +```cpp const char* input = "asakusa.exr"; float* out; // width * height * RGBA int width; int height; - const char* err; + const char* err = NULL; // or nullptr in C++11 int ret = LoadEXR(&out, &width, &height, input, &err); + + if (ret != TINYEXR_SUCCESS) { + if (err) { + fprintf(stderr, "ERR : %s\n", err); + FreeEXRErrorMessage(err); // release memory of error message. + } + } else { + ... + free(out); // release memory of image data + } + ``` -Loading EXR from a file. +### Reading layered RGB(A) EXR file. + +If you want to read EXR image with layer info (channel has a name with delimiter `.`), please use `LoadEXRWithLayer` API. + +You need to know layer name in advance (e.g. through `EXRLayers` API). + +```cpp + const char* input = ...; + const char* layer_name = "diffuse"; // or use EXRLayers to get list of layer names in .exr + float* out; // width * height * RGBA + int width; + int height; + const char* err = NULL; // or nullptr in C++11 + + // will read `diffuse.R`, `diffuse.G`, `diffuse.B`, (`diffuse.A`) channels + int ret = LoadEXRWithLayer(&out, &width, &height, input, layer_name, &err); + + if (ret != TINYEXR_SUCCESS) { + if (err) { + fprintf(stderr, "ERR : %s\n", err); + FreeEXRErrorMessage(err); // release memory of error message. + } + } else { + ... + free(out); // release memory of image data + } ``` - const char* input = "asakusa.exr"; - const char* err; - EXRImage exrImage; - InitEXRImage(&exrImage); +### Loading Singlepart EXR from a file. + +Scanline and tiled format are supported. + +```cpp + // 1. Read EXR version. + EXRVersion exr_version; + + int ret = ParseEXRVersionFromFile(&exr_version, argv[1]); + if (ret != 0) { + fprintf(stderr, "Invalid EXR file: %s\n", argv[1]); + return -1; + } + + if (exr_version.multipart) { + // must be multipart flag is false. + return -1; + } + + // 2. Read EXR header + EXRHeader exr_header; + InitEXRHeader(&exr_header); - int ret = ParseMultiChannelEXRHeaderFromFile(&exrImage, input, &err); + const char* err = NULL; // or `nullptr` in C++11 or later. + ret = ParseEXRHeaderFromFile(&exr_header, &exr_version, argv[1], &err); if (ret != 0) { fprintf(stderr, "Parse EXR err: %s\n", err); - return; + FreeEXRErrorMessage(err); // free's buffer for an error message + return ret; } - //// Uncomment if you want reading HALF image as FLOAT. - //for (int i = 0; i < exrImage.num_channels; i++) { - // if (exrImage.pixel_types[i] = TINYEXR_PIXELTYPE_HALF) { - // exrImage.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; - // } - //} + // // Read HALF channel as FLOAT. + // for (int i = 0; i < exr_header.num_channels; i++) { + // if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) { + // exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; + // } + // } + + EXRImage exr_image; + InitEXRImage(&exr_image); - ret = LoadMultiChannelEXRFromFile(&exrImage, input, &err); + ret = LoadEXRImageFromFile(&exr_image, &exr_header, argv[1], &err); if (ret != 0) { fprintf(stderr, "Load EXR err: %s\n", err); - return; + FreeEXRHeader(&exr_header); + FreeEXRErrorMessage(err); // free's buffer for an error message + return ret; } + + // 3. Access image data + // `exr_image.images` will be filled when EXR is scanline format. + // `exr_image.tiled` will be filled when EXR is tiled format. + + // 4. Free image data + FreeEXRImage(&exr_image); + FreeEXRHeader(&exr_header); ``` -Saving EXR file. +### Loading Multipart EXR from a file. + +Scanline and tiled format are supported. + +```cpp + // 1. Read EXR version. + EXRVersion exr_version; + + int ret = ParseEXRVersionFromFile(&exr_version, argv[1]); + if (ret != 0) { + fprintf(stderr, "Invalid EXR file: %s\n", argv[1]); + return -1; + } + + if (!exr_version.multipart) { + // must be multipart flag is true. + return -1; + } + + // 2. Read EXR headers in the EXR. + EXRHeader **exr_headers; // list of EXRHeader pointers. + int num_exr_headers; + const char *err = NULL; // or nullptr in C++11 or later + + // Memory for EXRHeader is allocated inside of ParseEXRMultipartHeaderFromFile, + ret = ParseEXRMultipartHeaderFromFile(&exr_headers, &num_exr_headers, &exr_version, argv[1], &err); + if (ret != 0) { + fprintf(stderr, "Parse EXR err: %s\n", err); + FreeEXRErrorMessage(err); // free's buffer for an error message + return ret; + } + + printf("num parts = %d\n", num_exr_headers); + + + // 3. Load images. + + // Prepare array of EXRImage. + std::vector<EXRImage> images(num_exr_headers); + for (int i =0; i < num_exr_headers; i++) { + InitEXRImage(&images[i]); + } + + ret = LoadEXRMultipartImageFromFile(&images.at(0), const_cast<const EXRHeader**>(exr_headers), num_exr_headers, argv[1], &err); + if (ret != 0) { + fprintf(stderr, "Parse EXR err: %s\n", err); + FreeEXRErrorMessage(err); // free's buffer for an error message + return ret; + } + + printf("Loaded %d part images\n", num_exr_headers); + + // 4. Access image data + // `exr_image.images` will be filled when EXR is scanline format. + // `exr_image.tiled` will be filled when EXR is tiled format. + + // 5. Free images + for (int i =0; i < num_exr_headers; i++) { + FreeEXRImage(&images.at(i)); + } + // 6. Free headers. + for (int i =0; i < num_exr_headers; i++) { + FreeEXRHeader(exr_headers[i]); + free(exr_headers[i]); + } + free(exr_headers); ``` + + +Saving Scanline EXR file. + +```cpp + // See `examples/rgbe2exr/` for more details. bool SaveEXR(const float* rgb, int width, int height, const char* outfilename) { - float* channels[3]; + EXRHeader header; + InitEXRHeader(&header); EXRImage image; InitEXRImage(&image); image.num_channels = 3; - // Must be BGR(A) order, since most of EXR viewers expect this channel order. - const char* channel_names[] = {"B", "G", "R"}; // "B", "G", "R", "A" for RGBA image - std::vector<float> images[3]; images[0].resize(width * height); images[1].resize(width * height); images[2].resize(width * height); + // Split RGBRGBRGB... into R, G and B layer for (int i = 0; i < width * height; i++) { images[0][i] = rgb[3*i+0]; images[1][i] = rgb[3*i+1]; @@ -131,31 +362,38 @@ Saving EXR file. image_ptr[1] = &(images[1].at(0)); // G image_ptr[2] = &(images[0].at(0)); // R - image.channel_names = channel_names; image.images = (unsigned char**)image_ptr; image.width = width; image.height = height; - image.compression = TINYEXR_COMPRESSIONTYPE_ZIP; - image.pixel_types = (int *)malloc(sizeof(int) * image.num_channels); - image.requested_pixel_types = (int *)malloc(sizeof(int) * image.num_channels); - for (int i = 0; i < image.num_channels; i++) { - image.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image - image.requested_pixel_types[i] = TINYEXR_PIXELTYPE_HALF; // pixel type of output image to be stored in .EXR + header.num_channels = 3; + header.channels = (EXRChannelInfo *)malloc(sizeof(EXRChannelInfo) * header.num_channels); + // Must be (A)BGR order, since most of EXR viewers expect this channel order. + strncpy(header.channels[0].name, "B", 255); header.channels[0].name[strlen("B")] = '\0'; + strncpy(header.channels[1].name, "G", 255); header.channels[1].name[strlen("G")] = '\0'; + strncpy(header.channels[2].name, "R", 255); header.channels[2].name[strlen("R")] = '\0'; + + header.pixel_types = (int *)malloc(sizeof(int) * header.num_channels); + header.requested_pixel_types = (int *)malloc(sizeof(int) * header.num_channels); + for (int i = 0; i < header.num_channels; i++) { + header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image + header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_HALF; // pixel type of output image to be stored in .EXR } - const char* err; - int ret = SaveMultiChannelEXRToFile(&image, outfilename, &err); - if (ret != 0) { + const char* err = NULL; // or nullptr in C++11 or later. + int ret = SaveEXRImageToFile(&image, &header, outfilename, &err); + if (ret != TINYEXR_SUCCESS) { fprintf(stderr, "Save EXR err: %s\n", err); + FreeEXRErrorMessage(err); // free's buffer for an error message return ret; } printf("Saved exr file. [ %s ] \n", outfilename); - free(image.pixel_types); - free(image.requested_pixel_types); + free(rgb); - return ret; + free(header.channels); + free(header.pixel_types); + free(header.requested_pixel_types); } ``` @@ -164,14 +402,14 @@ Saving EXR file. Reading deep image EXR file. See `example/deepview` for actual usage. -``` +```cpp const char* input = "deepimage.exr"; - const char* err; + const char* err = NULL; // or nullptr DeepImage deepImage; int ret = LoadDeepEXR(&deepImage, input, &err); - // acccess to each sample in the deep pixel. + // access to each sample in the deep pixel. for (int y = 0; y < deepImage.height; y++) { int sampleNum = deepImage.offset_table[y][deepImage.width-1]; for (int x = 0; x < deepImage.width-1; x++) { @@ -196,49 +434,101 @@ See `example/deepview` for actual usage.  +## TinyEXR extension + +### ZFP + +#### NOTE + +TinyEXR adds ZFP compression as an experimemtal support (Linux and MacOSX only). + +ZFP only supports FLOAT format pixel, and its image width and height must be the multiple of 4, since ZFP compresses pixels with 4x4 pixel block. + +#### Setup + +Checkout zfp repo as an submodule. + + $ git submodule update --init + +#### Build + +Then build ZFP + + $ cd deps/ZFP + $ mkdir -p lib # Create `lib` directory if not exist + $ make + +Set `1` to `TINYEXT_USE_ZFP` define in `tinyexr.h` + +Build your app with linking `deps/ZFP/lib/libzfp.a` + +#### ZFP attribute + +For ZFP EXR image, the following attribute must exist in its EXR image. + +* `zfpCompressionType` (uchar). + * 0 = fixed rate compression + * 1 = precision based variable rate compression + * 2 = accuracy based variable rate compression + +And the one of following attributes must exist in EXR, depending on the `zfpCompressionType` value. + +* `zfpCompressionRate` (double) + * Specifies compression rate for fixed rate compression. +* `zfpCompressionPrecision` (int32) + * Specifies the number of bits for precision based variable rate compression. +* `zfpCompressionTolerance` (double) + * Specifies the tolerance value for accuracy based variable rate compression. + +#### Note on ZFP compression. + +At least ZFP code itself works well on big endian machine. + +## Unit tests + +See `test/unit` directory. + ## TODO Contribution is welcome! - [ ] Compression - - [ ] NONE("compress" = 0, load) - - [ ] RLE("compress" = 1, load) - - [x] ZIPS("compress" = 2, load) - - [x] ZIP("compress" = 3, load) - - [x] PIZ("compress" = 4, load) - - [x] NONE("compress" = 0, save) - - [ ] RLE("compress" = 1, save) - - [x] ZIPS("compress" = 2, save) - - [x] ZIP("compress" = 3, save) - - [ ] PIZ("compress" = 4, save) + - [ ] B44? + - [ ] B44A? + - [ ] PIX24? - [ ] Custom attributes - - [x] Normal image(EXR 1.x) - - [ ] Deep image(EXR 2.x) -- [ ] JavaScript library + - [x] Normal image (EXR 1.x) + - [ ] Deep image (EXR 2.x) +- [ ] JavaScript library (experimental, using Emscripten) - [x] LoadEXRFromMemory - [ ] SaveMultiChannelEXR - [ ] Deep image save/load - [ ] Write from/to memory buffer. - - [x] SaveMultiChannelEXR - - [x] LoadMultiChannelEXR - [ ] Deep image save/load - [ ] Tile format. -- [ ] Support for various compression type. - - [x] zstd compression(Not in OpenEXR spec, though) + - [x] Tile format with no LoD (load). + - [ ] Tile format with LoD (load). + - [ ] Tile format with no LoD (save). + - [ ] Tile format with LoD (save). +- [ ] Support for custom compression type. + - [x] zfp compression (Not in OpenEXR spec, though) + - [ ] zstd? - [x] Multi-channel. -- [ ] Multi-part(EXR2.0) +- [ ] Multi-part (EXR2.0) + - [x] Load multi-part image + - [ ] Load multi-part deep image - [ ] Line order. - - [x] Increasing, decreasing(load) + - [x] Increasing, decreasing (load) - [ ] Random? - - [ ] Increasing, decreasing(save) -- [ ] Pixel format(UINT, FLOAT). - - [x] UINT, FLOAT(load) - - [x] UINT, FLOAT(deep load) - - [x] UINT, FLOAT(save) - - [ ] UINT, FLOAT(deep save) -- [ ] Full support for big endian machine. - - [x] Loading multi channel EXR - - [x] Saving multi channel EXR + - [ ] Increasing, decreasing (save) +- [ ] Pixel format (UINT, FLOAT). + - [x] UINT, FLOAT (load) + - [x] UINT, FLOAT (deep load) + - [x] UINT, FLOAT (save) + - [ ] UINT, FLOAT (deep save) +- [ ] Support for big endian machine. + - [ ] Loading multi-part channel EXR + - [ ] Saving multi-part channel EXR - [ ] Loading deep image - [ ] Saving deep image - [ ] Optimization @@ -248,6 +538,10 @@ Contribution is welcome! - [ ] OpenMP multi-threading in deep image loading. - [ ] OpenMP multi-threading in deep image saving. +## Python bindings + +`pytinyexr` is available: https://pypi.org/project/pytinyexr/ (loading only as of 0.9.1) + ## Similar or related projects * miniexr: https://github.com/aras-p/miniexr (Write OpenEXR) @@ -264,11 +558,11 @@ Contribution is welcome! ## Author(s) -Syoyo Fujita(syoyo@lighttransport.com) +Syoyo Fujita (syoyo@lighttransport.com) ## Contributor(s) -* Matt Ebb (http://mattebb.com) : deep image example. Thanks! -* Matt Pharr (http://pharr.org/matt/) : Testing tinyexr with OpenEXR(IlmImf). Thanks! -* Andrew Bell (https://github.com/andrewfb) & Richard Eakin (https://github.com/richardeakin) : Improving TinyEXR API. Thanks! -* Mike Wong (https://github.com/mwkm) : ZIPS compression support in loading. Thanks! +* Matt Ebb (http://mattebb.com): deep image example. Thanks! +* Matt Pharr (http://pharr.org/matt/): Testing tinyexr with OpenEXR(IlmImf). Thanks! +* Andrew Bell (https://github.com/andrewfb) & Richard Eakin (https://github.com/richardeakin): Improving TinyEXR API. Thanks! +* Mike Wong (https://github.com/mwkm): ZIPS compression support in loading. Thanks! diff --git a/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/ChangeLog.md b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/ChangeLog.md new file mode 100644 index 00000000000..4ae15a8cdfd --- /dev/null +++ b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/ChangeLog.md @@ -0,0 +1,196 @@ +## Changelog + +### 2.2.0 + + - Fix examples with amalgamation + - Modified cmake script to support shared library mode and find_package + - Fix for misleading doc comment on `mz_zip_reader_init_cfile` function + - Add include location tolerance and stop forcing `_GNU_SOURCE` + - Fix: mz_zip_reader_locate_file_v2 returns an mz_bool + - Fix large file system checks + - Add #elif to enable an external mz_crc32() to be linked in + - Write with dynamic size (size of file/data to be added not known before adding) + - Added uncompress2 for zlib compatibility + - Add support for building as a Meson subproject + - Added OSSFuzz support; Integrate with CIFuzz + - Add pkg-config file + - Fixed use-of-uninitialized value msan error when copying dist bytes with no output bytes written. + - mz_zip_validate_file(): fix memory leak on errors + - Fixed MSAN use-of-uninitialized in tinfl_decompress when invalid dist is decoded. In this instance dist was 31 which s_dist_base translates as 0 + - Add flag to set (compressed) size in local file header + - avoid use of uninitialized value in tdefl_record_literal + +### 2.1.0 + + - More instances of memcpy instead of cast and use memcpy per default + - Remove inline for c90 support + - New function to read files via callback functions when adding them + - Fix out of bounds read while reading Zip64 extended information + - guard memcpy when n == 0 because buffer may be NULL + - Implement inflateReset() function + - Move comp/decomp alloc/free prototypes under guarding #ifndef MZ_NO_MALLOC + - Fix large file support under Windows + - Don't warn if _LARGEFILE64_SOURCE is not defined to 1 + - Fixes for MSVC warnings + - Remove check that path of file added to archive contains ':' or '\' + - Add !defined check on MINIZ_USE_ALIGNED_LOADS_AND_STORES + +### 2.0.8 + + - Remove unimplemented functions (mz_zip_locate_file and mz_zip_locate_file_v2) + - Add license, changelog, readme and example files to release zip + - Fix heap overflow to user buffer in tinfl_status tinfl_decompress + - Fix corrupt archive if uncompressed file smaller than 4 byte and the file is added by mz_zip_writer_add_mem* + +### 2.0.7 + + - Removed need in C++ compiler in cmake build + - Fixed a lot of uninitialized value errors found with Valgrind by memsetting m_dict to 0 in tdefl_init + - Fix resource leak in mz_zip_reader_init_file_v2 + - Fix assert with mz_zip_writer_add_mem* w/MZ_DEFAULT_COMPRESSION + - cmake build: install library and headers + - Remove _LARGEFILE64_SOURCE requirement from apple defines for large files + +### 2.0.6 + + - Improve MZ_ZIP_FLAG_WRITE_ZIP64 documentation + - Remove check for cur_archive_file_ofs > UINT_MAX because cur_archive_file_ofs is not used after this point + - Add cmake debug configuration + - Fix PNG height when creating png files + - Add "iterative" file extraction method based on mz_zip_reader_extract_to_callback. + - Option to use memcpy for unaligned data access + - Define processor/arch macros as zero if not set to one + +### 2.0.4/2.0.5 + + - Fix compilation with the various omission compile definitions + +### 2.0.3 + +- Fix GCC/clang compile warnings +- Added callback for periodic flushes (for ZIP file streaming) +- Use UTF-8 for file names in ZIP files per default + +### 2.0.2 + +- Fix source backwards compatibility with 1.x +- Fix a ZIP bit not being set correctly + +### 2.0.1 + +- Added some tests +- Added CI +- Make source code ANSI C compatible + +### 2.0.0 beta + +- Matthew Sitton merged miniz 1.x to Rich Geldreich's vogl ZIP64 changes. Miniz is now licensed as MIT since the vogl code base is MIT licensed +- Miniz is now split into several files +- Miniz does now not seek backwards when creating ZIP files. That is the ZIP files can be streamed +- Miniz automatically switches to the ZIP64 format when the created ZIP files goes over ZIP file limits +- Similar to [SQLite](https://www.sqlite.org/amalgamation.html) the Miniz source code is amalgamated into one miniz.c/miniz.h pair in a build step (amalgamate.sh). Please use miniz.c/miniz.h in your projects +- Miniz 2 is only source back-compatible with miniz 1.x. It breaks binary compatibility because structures changed + +### v1.16 BETA Oct 19, 2013 + +Still testing, this release is downloadable from [here](http://www.tenacioussoftware.com/miniz_v116_beta_r1.7z). Two key inflator-only robustness and streaming related changes. Also merged in tdefl_compressor_alloc(), tdefl_compressor_free() helpers to make script bindings easier for rustyzip. I would greatly appreciate any help with testing or any feedback. + +The inflator in raw (non-zlib) mode is now usable on gzip or similar streams that have a bunch of bytes following the raw deflate data (problem discovered by rustyzip author williamw520). This version should never read beyond the last byte of the raw deflate data independent of how many bytes you pass into the input buffer. + +The inflator now has a new failure status TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS (-4). Previously, if the inflator was starved of bytes and could not make progress (because the input buffer was empty and the caller did not set the TINFL_FLAG_HAS_MORE_INPUT flag - say on truncated or corrupted compressed data stream) it would append all 0's to the input and try to soldier on. This is scary behavior if the caller didn't know when to stop accepting output (because it didn't know how much uncompressed data was expected, or didn't enforce a sane maximum). v1.16 will instead return TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS immediately if it needs 1 or more bytes to make progress, the input buf is empty, and the caller has indicated that no more input is available. This is a "soft" failure, so you can call the inflator again with more input and it will try to continue, or you can give up and fail. This could be very useful in network streaming scenarios. + +- The inflator coroutine func. is subtle and complex so I'm being cautious about this release. I would greatly appreciate any help with testing or any feedback. + I feel good about these changes, and they've been through several hours of automated testing, but they will probably not fix anything for the majority of prev. users so I'm + going to mark this release as beta for a few weeks and continue testing it at work/home on various things. +- The inflator in raw (non-zlib) mode is now usable on gzip or similar data streams that have a bunch of bytes following the raw deflate data (problem discovered by rustyzip author williamw520). + This version should *never* read beyond the last byte of the raw deflate data independent of how many bytes you pass into the input buffer. This issue was caused by the various Huffman bitbuffer lookahead optimizations, and + would not be an issue if the caller knew and enforced the precise size of the raw compressed data *or* if the compressed data was in zlib format (i.e. always followed by the byte aligned zlib adler32). + So in other words, you can now call the inflator on deflate streams that are followed by arbitrary amounts of data and it's guaranteed that decompression will stop exactly on the last byte. +- The inflator now has a new failure status: TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS (-4). Previously, if the inflator was starved of bytes and could not make progress (because the input buffer was empty and the + caller did not set the TINFL_FLAG_HAS_MORE_INPUT flag - say on truncated or corrupted compressed data stream) it would append all 0's to the input and try to soldier on. + This is scary, because in the worst case, I believe it was possible for the prev. inflator to start outputting large amounts of literal data. If the caller didn't know when to stop accepting output + (because it didn't know how much uncompressed data was expected, or didn't enforce a sane maximum) it could continue forever. v1.16 cannot fall into this failure mode, instead it'll return + TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS immediately if it needs 1 or more bytes to make progress, the input buf is empty, and the caller has indicated that no more input is available. This is a "soft" + failure, so you can call the inflator again with more input and it will try to continue, or you can give up and fail. This could be very useful in network streaming scenarios. +- Added documentation to all the tinfl return status codes, fixed miniz_tester so it accepts double minus params for Linux, tweaked example1.c, added a simple "follower bytes" test to miniz_tester.cpp. +### v1.15 r4 STABLE - Oct 13, 2013 + +Merged over a few very minor bug fixes that I fixed in the zip64 branch. This is downloadable from [here](http://code.google.com/p/miniz/downloads/list) and also in SVN head (as of 10/19/13). + + +### v1.15 - Oct. 13, 2013 + +Interim bugfix release while I work on the next major release with zip64 and streaming compression/decompression support. Fixed the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com), which could cause the locate files func to not find files when this flag was specified. Also fixed a bug in mz_zip_reader_extract_to_mem_no_alloc() with user provided read buffers (thanks kymoon). I also merged lots of compiler fixes from various github repo branches and Google Code issue reports. I finally added cmake support (only tested under for Linux so far), compiled and tested with clang v3.3 and gcc 4.6 (under Linux), added defl_write_image_to_png_file_in_memory_ex() (supports Y flipping for OpenGL use, real-time compression), added a new PNG example (example6.c - Mandelbrot), and I added 64-bit file I/O support (stat64(), etc.) for glibc. + +- Critical fix for the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com) which could cause locate files to not find files. This bug + would only have occured in earlier versions if you explicitly used this flag, OR if you used mz_zip_extract_archive_file_to_heap() or mz_zip_add_mem_to_archive_file_in_place() + (which used this flag). If you can't switch to v1.15 but want to fix this bug, just remove the uses of this flag from both helper funcs (and of course don't use the flag). +- Bugfix in mz_zip_reader_extract_to_mem_no_alloc() from kymoon when pUser_read_buf is not NULL and compressed size is > uncompressed size +- Fixing mz_zip_reader_extract_*() funcs so they don't try to extract compressed data from directory entries, to account for weird zipfiles which contain zero-size compressed data on dir entries. + Hopefully this fix won't cause any issues on weird zip archives, because it assumes the low 16-bits of zip external attributes are DOS attributes (which I believe they always are in practice). +- Fixing mz_zip_reader_is_file_a_directory() so it doesn't check the internal attributes, just the filename and external attributes +- mz_zip_reader_init_file() - missing MZ_FCLOSE() call if the seek failed +- Added cmake support for Linux builds which builds all the examples, tested with clang v3.3 and gcc v4.6. +- Clang fix for tdefl_write_image_to_png_file_in_memory() from toffaletti +- Merged MZ_FORCEINLINE fix from hdeanclark +- Fix <time.h> include before config #ifdef, thanks emil.brink +- Added tdefl_write_image_to_png_file_in_memory_ex(): supports Y flipping (super useful for OpenGL apps), and explicit control over the compression level (so you can + set it to 1 for real-time compression). +- Merged in some compiler fixes from paulharris's github repro. +- Retested this build under Windows (VS 2010, including static analysis), tcc 0.9.26, gcc v4.6 and clang v3.3. +- Added example6.c, which dumps an image of the mandelbrot set to a PNG file. +- Modified example2 to help test the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY flag more. +- In r3: Bugfix to mz_zip_writer_add_file() found during merge: Fix possible src file fclose() leak if alignment bytes+local header file write faiiled +- In r4: Minor bugfix to mz_zip_writer_add_from_zip_reader(): Was pushing the wrong central dir header offset, appears harmless in this release, but it became a problem in the zip64 branch + +### v1.14 - May 20, 2012 + +(SVN Only) Minor tweaks to get miniz.c compiling with the Tiny C Compiler, added #ifndef MINIZ_NO_TIME guards around utime.h includes. Adding mz_free() function, so the caller can free heap blocks returned by miniz using whatever heap functions it has been configured to use, MSVC specific fixes to use "safe" variants of several functions (localtime_s, fopen_s, freopen_s). + +MinGW32/64 GCC 4.6.1 compiler fixes: added MZ_FORCEINLINE, #include <time.h> (thanks fermtect). + +Compiler specific fixes, some from fermtect. I upgraded to TDM GCC 4.6.1 and now static __forceinline is giving it fits, so I'm changing all usage of __forceinline to MZ_FORCEINLINE and forcing gcc to use __attribute__((__always_inline__)) (and MSVC to use __forceinline). Also various fixes from fermtect for MinGW32: added #include , 64-bit ftell/fseek fixes. + +### v1.13 - May 19, 2012 + +From jason@cornsyrup.org and kelwert@mtu.edu - Most importantly, fixed mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bits. Temporarily/locally slammed in "typedef unsigned long mz_ulong" and re-ran a randomized regression test on ~500k files. Other stuff: + +Eliminated a bunch of warnings when compiling with GCC 32-bit/64. Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze (static analysis) option and fixed all warnings (except for the silly "Use of the comma-operator in a tested expression.." analysis warning, which I purposely use to work around a MSVC compiler warning). + +Created 32-bit and 64-bit Codeblocks projects/workspace. Built and tested Linux executables. The codeblocks workspace is compatible with Linux+Win32/x64. Added miniz_tester solution/project, which is a useful little app derived from LZHAM's tester app that I use as part of the regression test. Ran miniz.c and tinfl.c through another series of regression testing on ~500,000 files and archives. Modified example5.c so it purposely disables a bunch of high-level functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the MINIZ_NO_STDIO bug report.) + +Fix ftell() usage in a few of the examples so they exit with an error on files which are too large (a limitation of the examples, not miniz itself). Fix fail logic handling in mz_zip_add_mem_to_archive_file_in_place() so it always calls mz_zip_writer_finalize_archive() and mz_zip_writer_end(), even if the file add fails. + +- From jason@cornsyrup.org and kelwert@mtu.edu - Fix mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bit. +- Temporarily/locally slammed in "typedef unsigned long mz_ulong" and re-ran a randomized regression test on ~500k files. +- Eliminated a bunch of warnings when compiling with GCC 32-bit/64. +- Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze (static analysis) option and fixed all warnings (except for the silly +"Use of the comma-operator in a tested expression.." analysis warning, which I purposely use to work around a MSVC compiler warning). +- Created 32-bit and 64-bit Codeblocks projects/workspace. Built and tested Linux executables. The codeblocks workspace is compatible with Linux+Win32/x64. +- Added miniz_tester solution/project, which is a useful little app derived from LZHAM's tester app that I use as part of the regression test. +- Ran miniz.c and tinfl.c through another series of regression testing on ~500,000 files and archives. +- Modified example5.c so it purposely disables a bunch of high-level functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the MINIZ_NO_STDIO bug report.) +- Fix ftell() usage in examples so they exit with an error on files which are too large (a limitation of the examples, not miniz itself). + +### v1.12 - 4/12/12 + +More comments, added low-level example5.c, fixed a couple minor level_and_flags issues in the archive API's. +level_and_flags can now be set to MZ_DEFAULT_COMPRESSION. Thanks to Bruce Dawson <bruced@valvesoftware.com> for the feedback/bug report. + +### v1.11 - 5/28/11 + +Added statement from unlicense.org + +### v1.10 - 5/27/11 + +- Substantial compressor optimizations: +- Level 1 is now ~4x faster than before. The L1 compressor's throughput now varies between 70-110MB/sec. on a Core i7 (actual throughput varies depending on the type of data, and x64 vs. x86). +- Improved baseline L2-L9 compression perf. Also, greatly improved compression perf. issues on some file types. +- Refactored the compression code for better readability and maintainability. +- Added level 10 compression level (L10 has slightly better ratio than level 9, but could have a potentially large drop in throughput on some files). + +### v1.09 - 5/15/11 + +Initial stable release. + + diff --git a/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/LICENSE b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/LICENSE new file mode 100644 index 00000000000..b6ff45a30fb --- /dev/null +++ b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/LICENSE @@ -0,0 +1,22 @@ +Copyright 2013-2014 RAD Game Tools and Valve Software +Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC + +All Rights Reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.c b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.c new file mode 100644 index 00000000000..87bdedb1886 --- /dev/null +++ b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.c @@ -0,0 +1,7733 @@ +#include "miniz.h" +/************************************************************************** + * + * Copyright 2013-2014 RAD Game Tools and Valve Software + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + + + +typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1]; +typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1]; +typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1]; + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------- zlib-style API's */ + +mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) +{ + mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); + size_t block_len = buf_len % 5552; + if (!ptr) + return MZ_ADLER32_INIT; + while (buf_len) + { + for (i = 0; i + 7 < block_len; i += 8, ptr += 8) + { + s1 += ptr[0], s2 += s1; + s1 += ptr[1], s2 += s1; + s1 += ptr[2], s2 += s1; + s1 += ptr[3], s2 += s1; + s1 += ptr[4], s2 += s1; + s1 += ptr[5], s2 += s1; + s1 += ptr[6], s2 += s1; + s1 += ptr[7], s2 += s1; + } + for (; i < block_len; ++i) + s1 += *ptr++, s2 += s1; + s1 %= 65521U, s2 %= 65521U; + buf_len -= block_len; + block_len = 5552; + } + return (s2 << 16) + s1; +} + +/* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/ */ +#if 0 + mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) + { + static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; + mz_uint32 crcu32 = (mz_uint32)crc; + if (!ptr) + return MZ_CRC32_INIT; + crcu32 = ~crcu32; + while (buf_len--) + { + mz_uint8 b = *ptr++; + crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; + crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; + } + return ~crcu32; + } +#elif defined(USE_EXTERNAL_MZCRC) +/* If USE_EXTERNAL_CRC is defined, an external module will export the + * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version. + * Depending on the impl, it may be necessary to ~ the input/output crc values. + */ +mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len); +#else +/* Faster, but larger CPU cache footprint. + */ +mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) +{ + static const mz_uint32 s_crc_table[256] = + { + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, + 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, + 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, + 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, + 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, + 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, + 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, + 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, + 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, + 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, + 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, + 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, + 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, + 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, + 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, + 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, + 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, + 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, + 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, + 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, + 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, + 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, + 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, + 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, + 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, + 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, + 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, + 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D + }; + + mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF; + const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr; + + while (buf_len >= 4) + { + crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; + crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF]; + crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF]; + crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF]; + pByte_buf += 4; + buf_len -= 4; + } + + while (buf_len) + { + crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; + ++pByte_buf; + --buf_len; + } + + return ~crc32; +} +#endif + +void mz_free(void *p) +{ + MZ_FREE(p); +} + +MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size) +{ + (void)opaque, (void)items, (void)size; + return MZ_MALLOC(items * size); +} +MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address) +{ + (void)opaque, (void)address; + MZ_FREE(address); +} +MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size) +{ + (void)opaque, (void)address, (void)items, (void)size; + return MZ_REALLOC(address, items * size); +} + +const char *mz_version(void) +{ + return MZ_VERSION; +} + +#ifndef MINIZ_NO_ZLIB_APIS + +int mz_deflateInit(mz_streamp pStream, int level) +{ + return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); +} + +int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) +{ + tdefl_compressor *pComp; + mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); + + if (!pStream) + return MZ_STREAM_ERROR; + if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) + return MZ_PARAM_ERROR; + + pStream->data_type = 0; + pStream->adler = MZ_ADLER32_INIT; + pStream->msg = NULL; + pStream->reserved = 0; + pStream->total_in = 0; + pStream->total_out = 0; + if (!pStream->zalloc) + pStream->zalloc = miniz_def_alloc_func; + if (!pStream->zfree) + pStream->zfree = miniz_def_free_func; + + pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor)); + if (!pComp) + return MZ_MEM_ERROR; + + pStream->state = (struct mz_internal_state *)pComp; + + if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) + { + mz_deflateEnd(pStream); + return MZ_PARAM_ERROR; + } + + return MZ_OK; +} + +int mz_deflateReset(mz_streamp pStream) +{ + if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) + return MZ_STREAM_ERROR; + pStream->total_in = pStream->total_out = 0; + tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags); + return MZ_OK; +} + +int mz_deflate(mz_streamp pStream, int flush) +{ + size_t in_bytes, out_bytes; + mz_ulong orig_total_in, orig_total_out; + int mz_status = MZ_OK; + + if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) + return MZ_STREAM_ERROR; + if (!pStream->avail_out) + return MZ_BUF_ERROR; + + if (flush == MZ_PARTIAL_FLUSH) + flush = MZ_SYNC_FLUSH; + + if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE) + return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; + + orig_total_in = pStream->total_in; + orig_total_out = pStream->total_out; + for (;;) + { + tdefl_status defl_status; + in_bytes = pStream->avail_in; + out_bytes = pStream->avail_out; + + defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush); + pStream->next_in += (mz_uint)in_bytes; + pStream->avail_in -= (mz_uint)in_bytes; + pStream->total_in += (mz_uint)in_bytes; + pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state); + + pStream->next_out += (mz_uint)out_bytes; + pStream->avail_out -= (mz_uint)out_bytes; + pStream->total_out += (mz_uint)out_bytes; + + if (defl_status < 0) + { + mz_status = MZ_STREAM_ERROR; + break; + } + else if (defl_status == TDEFL_STATUS_DONE) + { + mz_status = MZ_STREAM_END; + break; + } + else if (!pStream->avail_out) + break; + else if ((!pStream->avail_in) && (flush != MZ_FINISH)) + { + if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) + break; + return MZ_BUF_ERROR; /* Can't make forward progress without some input. + */ + } + } + return mz_status; +} + +int mz_deflateEnd(mz_streamp pStream) +{ + if (!pStream) + return MZ_STREAM_ERROR; + if (pStream->state) + { + pStream->zfree(pStream->opaque, pStream->state); + pStream->state = NULL; + } + return MZ_OK; +} + +mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) +{ + (void)pStream; + /* This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.) */ + return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); +} + +int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level) +{ + int status; + mz_stream stream; + memset(&stream, 0, sizeof(stream)); + + /* In case mz_ulong is 64-bits (argh I hate longs). */ + if ((source_len | *pDest_len) > 0xFFFFFFFFU) + return MZ_PARAM_ERROR; + + stream.next_in = pSource; + stream.avail_in = (mz_uint32)source_len; + stream.next_out = pDest; + stream.avail_out = (mz_uint32)*pDest_len; + + status = mz_deflateInit(&stream, level); + if (status != MZ_OK) + return status; + + status = mz_deflate(&stream, MZ_FINISH); + if (status != MZ_STREAM_END) + { + mz_deflateEnd(&stream); + return (status == MZ_OK) ? MZ_BUF_ERROR : status; + } + + *pDest_len = stream.total_out; + return mz_deflateEnd(&stream); +} + +int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) +{ + return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION); +} + +mz_ulong mz_compressBound(mz_ulong source_len) +{ + return mz_deflateBound(NULL, source_len); +} + +typedef struct +{ + tinfl_decompressor m_decomp; + mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; + int m_window_bits; + mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; + tinfl_status m_last_status; +} inflate_state; + +int mz_inflateInit2(mz_streamp pStream, int window_bits) +{ + inflate_state *pDecomp; + if (!pStream) + return MZ_STREAM_ERROR; + if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) + return MZ_PARAM_ERROR; + + pStream->data_type = 0; + pStream->adler = 0; + pStream->msg = NULL; + pStream->total_in = 0; + pStream->total_out = 0; + pStream->reserved = 0; + if (!pStream->zalloc) + pStream->zalloc = miniz_def_alloc_func; + if (!pStream->zfree) + pStream->zfree = miniz_def_free_func; + + pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state)); + if (!pDecomp) + return MZ_MEM_ERROR; + + pStream->state = (struct mz_internal_state *)pDecomp; + + tinfl_init(&pDecomp->m_decomp); + pDecomp->m_dict_ofs = 0; + pDecomp->m_dict_avail = 0; + pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; + pDecomp->m_first_call = 1; + pDecomp->m_has_flushed = 0; + pDecomp->m_window_bits = window_bits; + + return MZ_OK; +} + +int mz_inflateInit(mz_streamp pStream) +{ + return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); +} + +int mz_inflateReset(mz_streamp pStream) +{ + inflate_state *pDecomp; + if (!pStream) + return MZ_STREAM_ERROR; + + pStream->data_type = 0; + pStream->adler = 0; + pStream->msg = NULL; + pStream->total_in = 0; + pStream->total_out = 0; + pStream->reserved = 0; + + pDecomp = (inflate_state *)pStream->state; + + tinfl_init(&pDecomp->m_decomp); + pDecomp->m_dict_ofs = 0; + pDecomp->m_dict_avail = 0; + pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; + pDecomp->m_first_call = 1; + pDecomp->m_has_flushed = 0; + /* pDecomp->m_window_bits = window_bits */; + + return MZ_OK; +} + +int mz_inflate(mz_streamp pStream, int flush) +{ + inflate_state *pState; + mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; + size_t in_bytes, out_bytes, orig_avail_in; + tinfl_status status; + + if ((!pStream) || (!pStream->state)) + return MZ_STREAM_ERROR; + if (flush == MZ_PARTIAL_FLUSH) + flush = MZ_SYNC_FLUSH; + if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) + return MZ_STREAM_ERROR; + + pState = (inflate_state *)pStream->state; + if (pState->m_window_bits > 0) + decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; + orig_avail_in = pStream->avail_in; + + first_call = pState->m_first_call; + pState->m_first_call = 0; + if (pState->m_last_status < 0) + return MZ_DATA_ERROR; + + if (pState->m_has_flushed && (flush != MZ_FINISH)) + return MZ_STREAM_ERROR; + pState->m_has_flushed |= (flush == MZ_FINISH); + + if ((flush == MZ_FINISH) && (first_call)) + { + /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */ + decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; + in_bytes = pStream->avail_in; + out_bytes = pStream->avail_out; + status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags); + pState->m_last_status = status; + pStream->next_in += (mz_uint)in_bytes; + pStream->avail_in -= (mz_uint)in_bytes; + pStream->total_in += (mz_uint)in_bytes; + pStream->adler = tinfl_get_adler32(&pState->m_decomp); + pStream->next_out += (mz_uint)out_bytes; + pStream->avail_out -= (mz_uint)out_bytes; + pStream->total_out += (mz_uint)out_bytes; + + if (status < 0) + return MZ_DATA_ERROR; + else if (status != TINFL_STATUS_DONE) + { + pState->m_last_status = TINFL_STATUS_FAILED; + return MZ_BUF_ERROR; + } + return MZ_STREAM_END; + } + /* flush != MZ_FINISH then we must assume there's more input. */ + if (flush != MZ_FINISH) + decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; + + if (pState->m_dict_avail) + { + n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); + memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); + pStream->next_out += n; + pStream->avail_out -= n; + pStream->total_out += n; + pState->m_dict_avail -= n; + pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); + return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; + } + + for (;;) + { + in_bytes = pStream->avail_in; + out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; + + status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags); + pState->m_last_status = status; + + pStream->next_in += (mz_uint)in_bytes; + pStream->avail_in -= (mz_uint)in_bytes; + pStream->total_in += (mz_uint)in_bytes; + pStream->adler = tinfl_get_adler32(&pState->m_decomp); + + pState->m_dict_avail = (mz_uint)out_bytes; + + n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); + memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); + pStream->next_out += n; + pStream->avail_out -= n; + pStream->total_out += n; + pState->m_dict_avail -= n; + pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); + + if (status < 0) + return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */ + else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) + return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */ + else if (flush == MZ_FINISH) + { + /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */ + if (status == TINFL_STATUS_DONE) + return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; + /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. */ + else if (!pStream->avail_out) + return MZ_BUF_ERROR; + } + else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) + break; + } + + return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; +} + +int mz_inflateEnd(mz_streamp pStream) +{ + if (!pStream) + return MZ_STREAM_ERROR; + if (pStream->state) + { + pStream->zfree(pStream->opaque, pStream->state); + pStream->state = NULL; + } + return MZ_OK; +} +int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len) +{ + mz_stream stream; + int status; + memset(&stream, 0, sizeof(stream)); + + /* In case mz_ulong is 64-bits (argh I hate longs). */ + if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU) + return MZ_PARAM_ERROR; + + stream.next_in = pSource; + stream.avail_in = (mz_uint32)*pSource_len; + stream.next_out = pDest; + stream.avail_out = (mz_uint32)*pDest_len; + + status = mz_inflateInit(&stream); + if (status != MZ_OK) + return status; + + status = mz_inflate(&stream, MZ_FINISH); + *pSource_len = *pSource_len - stream.avail_in; + if (status != MZ_STREAM_END) + { + mz_inflateEnd(&stream); + return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; + } + *pDest_len = stream.total_out; + + return mz_inflateEnd(&stream); +} + +int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) +{ + return mz_uncompress2(pDest, pDest_len, pSource, &source_len); +} + +const char *mz_error(int err) +{ + static struct + { + int m_err; + const char *m_pDesc; + } s_error_descs[] = + { + { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" } + }; + mz_uint i; + for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) + if (s_error_descs[i].m_err == err) + return s_error_descs[i].m_pDesc; + return NULL; +} + +#endif /*MINIZ_NO_ZLIB_APIS */ + +#ifdef __cplusplus +} +#endif + +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ +/************************************************************************** + * + * Copyright 2013-2014 RAD Game Tools and Valve Software + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + + + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------- Low-level Compression (independent from all decompression API's) */ + +/* Purposely making these tables static for faster init and thread safety. */ +static const mz_uint16 s_tdefl_len_sym[256] = + { + 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, 272, 272, + 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, 276, 276, 276, 276, + 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, + 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, + 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, + 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, + 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, + 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285 + }; + +static const mz_uint8 s_tdefl_len_extra[256] = + { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0 + }; + +static const mz_uint8 s_tdefl_small_dist_sym[512] = + { + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 + }; + +static const mz_uint8 s_tdefl_small_dist_extra[512] = + { + 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7 + }; + +static const mz_uint8 s_tdefl_large_dist_sym[128] = + { + 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 + }; + +static const mz_uint8 s_tdefl_large_dist_extra[128] = + { + 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 + }; + +/* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */ +typedef struct +{ + mz_uint16 m_key, m_sym_index; +} tdefl_sym_freq; +static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1) +{ + mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; + tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; + MZ_CLEAR_OBJ(hist); + for (i = 0; i < num_syms; i++) + { + mz_uint freq = pSyms0[i].m_key; + hist[freq & 0xFF]++; + hist[256 + ((freq >> 8) & 0xFF)]++; + } + while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) + total_passes--; + for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) + { + const mz_uint32 *pHist = &hist[pass << 8]; + mz_uint offsets[256], cur_ofs = 0; + for (i = 0; i < 256; i++) + { + offsets[i] = cur_ofs; + cur_ofs += pHist[i]; + } + for (i = 0; i < num_syms; i++) + pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i]; + { + tdefl_sym_freq *t = pCur_syms; + pCur_syms = pNew_syms; + pNew_syms = t; + } + } + return pCur_syms; +} + +/* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. */ +static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n) +{ + int root, leaf, next, avbl, used, dpth; + if (n == 0) + return; + else if (n == 1) + { + A[0].m_key = 1; + return; + } + A[0].m_key += A[1].m_key; + root = 0; + leaf = 2; + for (next = 1; next < n - 1; next++) + { + if (leaf >= n || A[root].m_key < A[leaf].m_key) + { + A[next].m_key = A[root].m_key; + A[root++].m_key = (mz_uint16)next; + } + else + A[next].m_key = A[leaf++].m_key; + if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) + { + A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key); + A[root++].m_key = (mz_uint16)next; + } + else + A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key); + } + A[n - 2].m_key = 0; + for (next = n - 3; next >= 0; next--) + A[next].m_key = A[A[next].m_key].m_key + 1; + avbl = 1; + used = dpth = 0; + root = n - 2; + next = n - 1; + while (avbl > 0) + { + while (root >= 0 && (int)A[root].m_key == dpth) + { + used++; + root--; + } + while (avbl > used) + { + A[next--].m_key = (mz_uint16)(dpth); + avbl--; + } + avbl = 2 * used; + dpth++; + used = 0; + } +} + +/* Limits canonical Huffman code table's max code size. */ +enum +{ + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 +}; +static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size) +{ + int i; + mz_uint32 total = 0; + if (code_list_len <= 1) + return; + for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) + pNum_codes[max_code_size] += pNum_codes[i]; + for (i = max_code_size; i > 0; i--) + total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i)); + while (total != (1UL << max_code_size)) + { + pNum_codes[max_code_size]--; + for (i = max_code_size - 1; i > 0; i--) + if (pNum_codes[i]) + { + pNum_codes[i]--; + pNum_codes[i + 1] += 2; + break; + } + total--; + } +} + +static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table) +{ + int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; + mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; + MZ_CLEAR_OBJ(num_codes); + if (static_table) + { + for (i = 0; i < table_len; i++) + num_codes[d->m_huff_code_sizes[table_num][i]]++; + } + else + { + tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms; + int num_used_syms = 0; + const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0]; + for (i = 0; i < table_len; i++) + if (pSym_count[i]) + { + syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; + syms0[num_used_syms++].m_sym_index = (mz_uint16)i; + } + + pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); + tdefl_calculate_minimum_redundancy(pSyms, num_used_syms); + + for (i = 0; i < num_used_syms; i++) + num_codes[pSyms[i].m_key]++; + + tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit); + + MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); + MZ_CLEAR_OBJ(d->m_huff_codes[table_num]); + for (i = 1, j = num_used_syms; i <= code_size_limit; i++) + for (l = num_codes[i]; l > 0; l--) + d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); + } + + next_code[1] = 0; + for (j = 0, i = 2; i <= code_size_limit; i++) + next_code[i] = j = ((j + num_codes[i - 1]) << 1); + + for (i = 0; i < table_len; i++) + { + mz_uint rev_code = 0, code, code_size; + if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) + continue; + code = next_code[code_size]++; + for (l = code_size; l > 0; l--, code >>= 1) + rev_code = (rev_code << 1) | (code & 1); + d->m_huff_codes[table_num][i] = (mz_uint16)rev_code; + } +} + +#define TDEFL_PUT_BITS(b, l) \ + do \ + { \ + mz_uint bits = b; \ + mz_uint len = l; \ + MZ_ASSERT(bits <= ((1U << len) - 1U)); \ + d->m_bit_buffer |= (bits << d->m_bits_in); \ + d->m_bits_in += len; \ + while (d->m_bits_in >= 8) \ + { \ + if (d->m_pOutput_buf < d->m_pOutput_buf_end) \ + *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \ + d->m_bit_buffer >>= 8; \ + d->m_bits_in -= 8; \ + } \ + } \ + MZ_MACRO_END + +#define TDEFL_RLE_PREV_CODE_SIZE() \ + { \ + if (rle_repeat_count) \ + { \ + if (rle_repeat_count < 3) \ + { \ + d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \ + while (rle_repeat_count--) \ + packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \ + } \ + else \ + { \ + d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \ + packed_code_sizes[num_packed_code_sizes++] = 16; \ + packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \ + } \ + rle_repeat_count = 0; \ + } \ + } + +#define TDEFL_RLE_ZERO_CODE_SIZE() \ + { \ + if (rle_z_count) \ + { \ + if (rle_z_count < 3) \ + { \ + d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \ + while (rle_z_count--) \ + packed_code_sizes[num_packed_code_sizes++] = 0; \ + } \ + else if (rle_z_count <= 10) \ + { \ + d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \ + packed_code_sizes[num_packed_code_sizes++] = 17; \ + packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \ + } \ + else \ + { \ + d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \ + packed_code_sizes[num_packed_code_sizes++] = 18; \ + packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \ + } \ + rle_z_count = 0; \ + } \ + } + +static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + +static void tdefl_start_dynamic_block(tdefl_compressor *d) +{ + int num_lit_codes, num_dist_codes, num_bit_lengths; + mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index; + mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF; + + d->m_huff_count[0][256] = 1; + + tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE); + tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE); + + for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) + if (d->m_huff_code_sizes[0][num_lit_codes - 1]) + break; + for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) + if (d->m_huff_code_sizes[1][num_dist_codes - 1]) + break; + + memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes); + memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes); + total_code_sizes_to_pack = num_lit_codes + num_dist_codes; + num_packed_code_sizes = 0; + rle_z_count = 0; + rle_repeat_count = 0; + + memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2); + for (i = 0; i < total_code_sizes_to_pack; i++) + { + mz_uint8 code_size = code_sizes_to_pack[i]; + if (!code_size) + { + TDEFL_RLE_PREV_CODE_SIZE(); + if (++rle_z_count == 138) + { + TDEFL_RLE_ZERO_CODE_SIZE(); + } + } + else + { + TDEFL_RLE_ZERO_CODE_SIZE(); + if (code_size != prev_code_size) + { + TDEFL_RLE_PREV_CODE_SIZE(); + d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1); + packed_code_sizes[num_packed_code_sizes++] = code_size; + } + else if (++rle_repeat_count == 6) + { + TDEFL_RLE_PREV_CODE_SIZE(); + } + } + prev_code_size = code_size; + } + if (rle_repeat_count) + { + TDEFL_RLE_PREV_CODE_SIZE(); + } + else + { + TDEFL_RLE_ZERO_CODE_SIZE(); + } + + tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE); + + TDEFL_PUT_BITS(2, 2); + + TDEFL_PUT_BITS(num_lit_codes - 257, 5); + TDEFL_PUT_BITS(num_dist_codes - 1, 5); + + for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) + if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) + break; + num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1)); + TDEFL_PUT_BITS(num_bit_lengths - 4, 4); + for (i = 0; (int)i < num_bit_lengths; i++) + TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); + + for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;) + { + mz_uint code = packed_code_sizes[packed_code_sizes_index++]; + MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2); + TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); + if (code >= 16) + TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]); + } +} + +static void tdefl_start_static_block(tdefl_compressor *d) +{ + mz_uint i; + mz_uint8 *p = &d->m_huff_code_sizes[0][0]; + + for (i = 0; i <= 143; ++i) + *p++ = 8; + for (; i <= 255; ++i) + *p++ = 9; + for (; i <= 279; ++i) + *p++ = 7; + for (; i <= 287; ++i) + *p++ = 8; + + memset(d->m_huff_code_sizes[1], 5, 32); + + tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE); + tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE); + + TDEFL_PUT_BITS(1, 2); +} + +static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS +static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) +{ + mz_uint flags; + mz_uint8 *pLZ_codes; + mz_uint8 *pOutput_buf = d->m_pOutput_buf; + mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf; + mz_uint64 bit_buffer = d->m_bit_buffer; + mz_uint bits_in = d->m_bits_in; + +#define TDEFL_PUT_BITS_FAST(b, l) \ + { \ + bit_buffer |= (((mz_uint64)(b)) << bits_in); \ + bits_in += (l); \ + } + + flags = 1; + for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) + { + if (flags == 1) + flags = *pLZ_codes++ | 0x100; + + if (flags & 1) + { + mz_uint s0, s1, n0, n1, sym, num_extra_bits; + mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16 *)(pLZ_codes + 1); + pLZ_codes += 3; + + MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); + + /* This sequence coaxes MSVC into using cmov's vs. jmp's. */ + s0 = s_tdefl_small_dist_sym[match_dist & 511]; + n0 = s_tdefl_small_dist_extra[match_dist & 511]; + s1 = s_tdefl_large_dist_sym[match_dist >> 8]; + n1 = s_tdefl_large_dist_extra[match_dist >> 8]; + sym = (match_dist < 512) ? s0 : s1; + num_extra_bits = (match_dist < 512) ? n0 : n1; + + MZ_ASSERT(d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); + } + else + { + mz_uint lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + + if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) + { + flags >>= 1; + lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + + if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) + { + flags >>= 1; + lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + } + } + } + + if (pOutput_buf >= d->m_pOutput_buf_end) + return MZ_FALSE; + + *(mz_uint64 *)pOutput_buf = bit_buffer; + pOutput_buf += (bits_in >> 3); + bit_buffer >>= (bits_in & ~7); + bits_in &= 7; + } + +#undef TDEFL_PUT_BITS_FAST + + d->m_pOutput_buf = pOutput_buf; + d->m_bits_in = 0; + d->m_bit_buffer = 0; + + while (bits_in) + { + mz_uint32 n = MZ_MIN(bits_in, 16); + TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n); + bit_buffer >>= n; + bits_in -= n; + } + + TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); + + return (d->m_pOutput_buf < d->m_pOutput_buf_end); +} +#else +static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) +{ + mz_uint flags; + mz_uint8 *pLZ_codes; + + flags = 1; + for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) + { + if (flags == 1) + flags = *pLZ_codes++ | 0x100; + if (flags & 1) + { + mz_uint sym, num_extra_bits; + mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); + pLZ_codes += 3; + + MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); + + if (match_dist < 512) + { + sym = s_tdefl_small_dist_sym[match_dist]; + num_extra_bits = s_tdefl_small_dist_extra[match_dist]; + } + else + { + sym = s_tdefl_large_dist_sym[match_dist >> 8]; + num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8]; + } + MZ_ASSERT(d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); + } + else + { + mz_uint lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + } + } + + TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); + + return (d->m_pOutput_buf < d->m_pOutput_buf_end); +} +#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */ + +static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) +{ + if (static_block) + tdefl_start_static_block(d); + else + tdefl_start_dynamic_block(d); + return tdefl_compress_lz_codes(d); +} + +static int tdefl_flush_block(tdefl_compressor *d, int flush) +{ + mz_uint saved_bit_buf, saved_bits_in; + mz_uint8 *pSaved_output_buf; + mz_bool comp_block_succeeded = MZ_FALSE; + int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; + mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf; + + d->m_pOutput_buf = pOutput_buf_start; + d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16; + + MZ_ASSERT(!d->m_output_flush_remaining); + d->m_output_flush_ofs = 0; + d->m_output_flush_remaining = 0; + + *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left); + d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); + + if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) + { + TDEFL_PUT_BITS(0x78, 8); + TDEFL_PUT_BITS(0x01, 8); + } + + TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); + + pSaved_output_buf = d->m_pOutput_buf; + saved_bit_buf = d->m_bit_buffer; + saved_bits_in = d->m_bits_in; + + if (!use_raw_block) + comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48)); + + /* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */ + if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && + ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) + { + mz_uint i; + d->m_pOutput_buf = pSaved_output_buf; + d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; + TDEFL_PUT_BITS(0, 2); + if (d->m_bits_in) + { + TDEFL_PUT_BITS(0, 8 - d->m_bits_in); + } + for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) + { + TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); + } + for (i = 0; i < d->m_total_lz_bytes; ++i) + { + TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8); + } + } + /* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. */ + else if (!comp_block_succeeded) + { + d->m_pOutput_buf = pSaved_output_buf; + d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; + tdefl_compress_block(d, MZ_TRUE); + } + + if (flush) + { + if (flush == TDEFL_FINISH) + { + if (d->m_bits_in) + { + TDEFL_PUT_BITS(0, 8 - d->m_bits_in); + } + if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) + { + mz_uint i, a = d->m_adler32; + for (i = 0; i < 4; i++) + { + TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); + a <<= 8; + } + } + } + else + { + mz_uint i, z = 0; + TDEFL_PUT_BITS(0, 3); + if (d->m_bits_in) + { + TDEFL_PUT_BITS(0, 8 - d->m_bits_in); + } + for (i = 2; i; --i, z ^= 0xFFFF) + { + TDEFL_PUT_BITS(z & 0xFFFF, 16); + } + } + } + + MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end); + + memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); + memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); + + d->m_pLZ_code_buf = d->m_lz_code_buf + 1; + d->m_pLZ_flags = d->m_lz_code_buf; + d->m_num_flags_left = 8; + d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes; + d->m_total_lz_bytes = 0; + d->m_block_index++; + + if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) + { + if (d->m_pPut_buf_func) + { + *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; + if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user)) + return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED); + } + else if (pOutput_buf_start == d->m_output_buf) + { + int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs)); + memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy); + d->m_out_buf_ofs += bytes_to_copy; + if ((n -= bytes_to_copy) != 0) + { + d->m_output_flush_ofs = bytes_to_copy; + d->m_output_flush_remaining = n; + } + } + else + { + d->m_out_buf_ofs += n; + } + } + + return d->m_output_flush_remaining; +} + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES +#ifdef MINIZ_UNALIGNED_USE_MEMCPY +static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8* p) +{ + mz_uint16 ret; + memcpy(&ret, p, sizeof(mz_uint16)); + return ret; +} +static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16* p) +{ + mz_uint16 ret; + memcpy(&ret, p, sizeof(mz_uint16)); + return ret; +} +#else +#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p) +#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p) +#endif +static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) +{ + mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len; + mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; + const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q; + mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s); + MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); + if (max_match_len <= match_len) + return; + for (;;) + { + for (;;) + { + if (--num_probes_left == 0) + return; +#define TDEFL_PROBE \ + next_probe_pos = d->m_next[probe_pos]; \ + if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \ + return; \ + probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ + if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \ + break; + TDEFL_PROBE; + TDEFL_PROBE; + TDEFL_PROBE; + } + if (!dist) + break; + q = (const mz_uint16 *)(d->m_dict + probe_pos); + if (TDEFL_READ_UNALIGNED_WORD2(q) != s01) + continue; + p = s; + probe_len = 32; + do + { + } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && + (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0)); + if (!probe_len) + { + *pMatch_dist = dist; + *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN); + break; + } + else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len) + { + *pMatch_dist = dist; + if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len) + break; + c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]); + } + } +} +#else +static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) +{ + mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len; + mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; + const mz_uint8 *s = d->m_dict + pos, *p, *q; + mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1]; + MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); + if (max_match_len <= match_len) + return; + for (;;) + { + for (;;) + { + if (--num_probes_left == 0) + return; +#define TDEFL_PROBE \ + next_probe_pos = d->m_next[probe_pos]; \ + if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \ + return; \ + probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ + if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \ + break; + TDEFL_PROBE; + TDEFL_PROBE; + TDEFL_PROBE; + } + if (!dist) + break; + p = s; + q = d->m_dict + probe_pos; + for (probe_len = 0; probe_len < max_match_len; probe_len++) + if (*p++ != *q++) + break; + if (probe_len > match_len) + { + *pMatch_dist = dist; + if ((*pMatch_len = match_len = probe_len) == max_match_len) + return; + c0 = d->m_dict[pos + match_len]; + c1 = d->m_dict[pos + match_len - 1]; + } + } +} +#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */ + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN +#ifdef MINIZ_UNALIGNED_USE_MEMCPY +static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8* p) +{ + mz_uint32 ret; + memcpy(&ret, p, sizeof(mz_uint32)); + return ret; +} +#else +#define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32 *)(p) +#endif +static mz_bool tdefl_compress_fast(tdefl_compressor *d) +{ + /* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. */ + mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left; + mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags; + mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; + + while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) + { + const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096; + mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; + mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size); + d->m_src_buf_left -= num_bytes_to_process; + lookahead_size += num_bytes_to_process; + + while (num_bytes_to_process) + { + mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process); + memcpy(d->m_dict + dst_pos, d->m_pSrc, n); + if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) + memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos)); + d->m_pSrc += n; + dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK; + num_bytes_to_process -= n; + } + + dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size); + if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE)) + break; + + while (lookahead_size >= 4) + { + mz_uint cur_match_dist, cur_match_len = 1; + mz_uint8 *pCur_dict = d->m_dict + cur_pos; + mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF; + mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK; + mz_uint probe_pos = d->m_hash[hash]; + d->m_hash[hash] = (mz_uint16)lookahead_pos; + + if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((TDEFL_READ_UNALIGNED_WORD32(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram)) + { + const mz_uint16 *p = (const mz_uint16 *)pCur_dict; + const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos); + mz_uint32 probe_len = 32; + do + { + } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && + (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0)); + cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q); + if (!probe_len) + cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0; + + if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U))) + { + cur_match_len = 1; + *pLZ_code_buf++ = (mz_uint8)first_trigram; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + d->m_huff_count[0][(mz_uint8)first_trigram]++; + } + else + { + mz_uint32 s0, s1; + cur_match_len = MZ_MIN(cur_match_len, lookahead_size); + + MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE)); + + cur_match_dist--; + + pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN); +#ifdef MINIZ_UNALIGNED_USE_MEMCPY + memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist)); +#else + *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist; +#endif + pLZ_code_buf += 3; + *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80); + + s0 = s_tdefl_small_dist_sym[cur_match_dist & 511]; + s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8]; + d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++; + + d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++; + } + } + else + { + *pLZ_code_buf++ = (mz_uint8)first_trigram; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + d->m_huff_count[0][(mz_uint8)first_trigram]++; + } + + if (--num_flags_left == 0) + { + num_flags_left = 8; + pLZ_flags = pLZ_code_buf++; + } + + total_lz_bytes += cur_match_len; + lookahead_pos += cur_match_len; + dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE); + cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK; + MZ_ASSERT(lookahead_size >= cur_match_len); + lookahead_size -= cur_match_len; + + if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) + { + int n; + d->m_lookahead_pos = lookahead_pos; + d->m_lookahead_size = lookahead_size; + d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; + d->m_pLZ_code_buf = pLZ_code_buf; + d->m_pLZ_flags = pLZ_flags; + d->m_num_flags_left = num_flags_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + total_lz_bytes = d->m_total_lz_bytes; + pLZ_code_buf = d->m_pLZ_code_buf; + pLZ_flags = d->m_pLZ_flags; + num_flags_left = d->m_num_flags_left; + } + } + + while (lookahead_size) + { + mz_uint8 lit = d->m_dict[cur_pos]; + + total_lz_bytes++; + *pLZ_code_buf++ = lit; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + if (--num_flags_left == 0) + { + num_flags_left = 8; + pLZ_flags = pLZ_code_buf++; + } + + d->m_huff_count[0][lit]++; + + lookahead_pos++; + dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE); + cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; + lookahead_size--; + + if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) + { + int n; + d->m_lookahead_pos = lookahead_pos; + d->m_lookahead_size = lookahead_size; + d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; + d->m_pLZ_code_buf = pLZ_code_buf; + d->m_pLZ_flags = pLZ_flags; + d->m_num_flags_left = num_flags_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + total_lz_bytes = d->m_total_lz_bytes; + pLZ_code_buf = d->m_pLZ_code_buf; + pLZ_flags = d->m_pLZ_flags; + num_flags_left = d->m_num_flags_left; + } + } + } + + d->m_lookahead_pos = lookahead_pos; + d->m_lookahead_size = lookahead_size; + d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; + d->m_pLZ_code_buf = pLZ_code_buf; + d->m_pLZ_flags = pLZ_flags; + d->m_num_flags_left = num_flags_left; + return MZ_TRUE; +} +#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */ + +static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit) +{ + d->m_total_lz_bytes++; + *d->m_pLZ_code_buf++ = lit; + *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1); + if (--d->m_num_flags_left == 0) + { + d->m_num_flags_left = 8; + d->m_pLZ_flags = d->m_pLZ_code_buf++; + } + d->m_huff_count[0][lit]++; +} + +static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist) +{ + mz_uint32 s0, s1; + + MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE)); + + d->m_total_lz_bytes += match_len; + + d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN); + + match_dist -= 1; + d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF); + d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8); + d->m_pLZ_code_buf += 3; + + *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80); + if (--d->m_num_flags_left == 0) + { + d->m_num_flags_left = 8; + d->m_pLZ_flags = d->m_pLZ_code_buf++; + } + + s0 = s_tdefl_small_dist_sym[match_dist & 511]; + s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127]; + d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; + d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; +} + +static mz_bool tdefl_compress_normal(tdefl_compressor *d) +{ + const mz_uint8 *pSrc = d->m_pSrc; + size_t src_buf_left = d->m_src_buf_left; + tdefl_flush flush = d->m_flush; + + while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) + { + mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos; + /* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */ + if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) + { + mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2; + mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]; + mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); + const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process; + src_buf_left -= num_bytes_to_process; + d->m_lookahead_size += num_bytes_to_process; + while (pSrc != pSrc_end) + { + mz_uint8 c = *pSrc++; + d->m_dict[dst_pos] = c; + if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) + d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; + hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); + d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; + d->m_hash[hash] = (mz_uint16)(ins_pos); + dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; + ins_pos++; + } + } + else + { + while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) + { + mz_uint8 c = *pSrc++; + mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; + src_buf_left--; + d->m_dict[dst_pos] = c; + if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) + d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; + if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) + { + mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2; + mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); + d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; + d->m_hash[hash] = (mz_uint16)(ins_pos); + } + } + } + d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size); + if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) + break; + + /* Simple lazy/greedy parsing state machine. */ + len_to_move = 1; + cur_match_dist = 0; + cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1); + cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; + if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) + { + if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) + { + mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK]; + cur_match_len = 0; + while (cur_match_len < d->m_lookahead_size) + { + if (d->m_dict[cur_pos + cur_match_len] != c) + break; + cur_match_len++; + } + if (cur_match_len < TDEFL_MIN_MATCH_LEN) + cur_match_len = 0; + else + cur_match_dist = 1; + } + } + else + { + tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len); + } + if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) + { + cur_match_dist = cur_match_len = 0; + } + if (d->m_saved_match_len) + { + if (cur_match_len > d->m_saved_match_len) + { + tdefl_record_literal(d, (mz_uint8)d->m_saved_lit); + if (cur_match_len >= 128) + { + tdefl_record_match(d, cur_match_len, cur_match_dist); + d->m_saved_match_len = 0; + len_to_move = cur_match_len; + } + else + { + d->m_saved_lit = d->m_dict[cur_pos]; + d->m_saved_match_dist = cur_match_dist; + d->m_saved_match_len = cur_match_len; + } + } + else + { + tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist); + len_to_move = d->m_saved_match_len - 1; + d->m_saved_match_len = 0; + } + } + else if (!cur_match_dist) + tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]); + else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) + { + tdefl_record_match(d, cur_match_len, cur_match_dist); + len_to_move = cur_match_len; + } + else + { + d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]; + d->m_saved_match_dist = cur_match_dist; + d->m_saved_match_len = cur_match_len; + } + /* Move the lookahead forward by len_to_move bytes. */ + d->m_lookahead_pos += len_to_move; + MZ_ASSERT(d->m_lookahead_size >= len_to_move); + d->m_lookahead_size -= len_to_move; + d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE); + /* Check if it's time to flush the current LZ codes to the internal output buffer. */ + if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || + ((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) + { + int n; + d->m_pSrc = pSrc; + d->m_src_buf_left = src_buf_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + } + } + + d->m_pSrc = pSrc; + d->m_src_buf_left = src_buf_left; + return MZ_TRUE; +} + +static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d) +{ + if (d->m_pIn_buf_size) + { + *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; + } + + if (d->m_pOut_buf_size) + { + size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining); + memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n); + d->m_output_flush_ofs += (mz_uint)n; + d->m_output_flush_remaining -= (mz_uint)n; + d->m_out_buf_ofs += n; + + *d->m_pOut_buf_size = d->m_out_buf_ofs; + } + + return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY; +} + +tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush) +{ + if (!d) + { + if (pIn_buf_size) + *pIn_buf_size = 0; + if (pOut_buf_size) + *pOut_buf_size = 0; + return TDEFL_STATUS_BAD_PARAM; + } + + d->m_pIn_buf = pIn_buf; + d->m_pIn_buf_size = pIn_buf_size; + d->m_pOut_buf = pOut_buf; + d->m_pOut_buf_size = pOut_buf_size; + d->m_pSrc = (const mz_uint8 *)(pIn_buf); + d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0; + d->m_out_buf_ofs = 0; + d->m_flush = flush; + + if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || + (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) + { + if (pIn_buf_size) + *pIn_buf_size = 0; + if (pOut_buf_size) + *pOut_buf_size = 0; + return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM); + } + d->m_wants_to_finish |= (flush == TDEFL_FINISH); + + if ((d->m_output_flush_remaining) || (d->m_finished)) + return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN + if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) && + ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) && + ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0)) + { + if (!tdefl_compress_fast(d)) + return d->m_prev_return_status; + } + else +#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */ + { + if (!tdefl_compress_normal(d)) + return d->m_prev_return_status; + } + + if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf)) + d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf); + + if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) + { + if (tdefl_flush_block(d, flush) < 0) + return d->m_prev_return_status; + d->m_finished = (flush == TDEFL_FINISH); + if (flush == TDEFL_FULL_FLUSH) + { + MZ_CLEAR_OBJ(d->m_hash); + MZ_CLEAR_OBJ(d->m_next); + d->m_dict_size = 0; + } + } + + return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); +} + +tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush) +{ + MZ_ASSERT(d->m_pPut_buf_func); + return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush); +} + +tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + d->m_pPut_buf_func = pPut_buf_func; + d->m_pPut_buf_user = pPut_buf_user; + d->m_flags = (mz_uint)(flags); + d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3; + d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; + d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; + if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) + MZ_CLEAR_OBJ(d->m_hash); + d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0; + d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0; + d->m_pLZ_code_buf = d->m_lz_code_buf + 1; + d->m_pLZ_flags = d->m_lz_code_buf; + *d->m_pLZ_flags = 0; + d->m_num_flags_left = 8; + d->m_pOutput_buf = d->m_output_buf; + d->m_pOutput_buf_end = d->m_output_buf; + d->m_prev_return_status = TDEFL_STATUS_OKAY; + d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0; + d->m_adler32 = 1; + d->m_pIn_buf = NULL; + d->m_pOut_buf = NULL; + d->m_pIn_buf_size = NULL; + d->m_pOut_buf_size = NULL; + d->m_flush = TDEFL_NO_FLUSH; + d->m_pSrc = NULL; + d->m_src_buf_left = 0; + d->m_out_buf_ofs = 0; + if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) + MZ_CLEAR_OBJ(d->m_dict); + memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); + memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); + return TDEFL_STATUS_OKAY; +} + +tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d) +{ + return d->m_prev_return_status; +} + +mz_uint32 tdefl_get_adler32(tdefl_compressor *d) +{ + return d->m_adler32; +} + +mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + tdefl_compressor *pComp; + mz_bool succeeded; + if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) + return MZ_FALSE; + pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); + if (!pComp) + return MZ_FALSE; + succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY); + succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE); + MZ_FREE(pComp); + return succeeded; +} + +typedef struct +{ + size_t m_size, m_capacity; + mz_uint8 *m_pBuf; + mz_bool m_expandable; +} tdefl_output_buffer; + +static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser) +{ + tdefl_output_buffer *p = (tdefl_output_buffer *)pUser; + size_t new_size = p->m_size + len; + if (new_size > p->m_capacity) + { + size_t new_capacity = p->m_capacity; + mz_uint8 *pNew_buf; + if (!p->m_expandable) + return MZ_FALSE; + do + { + new_capacity = MZ_MAX(128U, new_capacity << 1U); + } while (new_size > new_capacity); + pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity); + if (!pNew_buf) + return MZ_FALSE; + p->m_pBuf = pNew_buf; + p->m_capacity = new_capacity; + } + memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len); + p->m_size = new_size; + return MZ_TRUE; +} + +void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) +{ + tdefl_output_buffer out_buf; + MZ_CLEAR_OBJ(out_buf); + if (!pOut_len) + return MZ_FALSE; + else + *pOut_len = 0; + out_buf.m_expandable = MZ_TRUE; + if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) + return NULL; + *pOut_len = out_buf.m_size; + return out_buf.m_pBuf; +} + +size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags) +{ + tdefl_output_buffer out_buf; + MZ_CLEAR_OBJ(out_buf); + if (!pOut_buf) + return 0; + out_buf.m_pBuf = (mz_uint8 *)pOut_buf; + out_buf.m_capacity = out_buf_len; + if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) + return 0; + return out_buf.m_size; +} + +static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; + +/* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). */ +mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy) +{ + mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); + if (window_bits > 0) + comp_flags |= TDEFL_WRITE_ZLIB_HEADER; + + if (!level) + comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; + else if (strategy == MZ_FILTERED) + comp_flags |= TDEFL_FILTER_MATCHES; + else if (strategy == MZ_HUFFMAN_ONLY) + comp_flags &= ~TDEFL_MAX_PROBES_MASK; + else if (strategy == MZ_FIXED) + comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS; + else if (strategy == MZ_RLE) + comp_flags |= TDEFL_RLE_MATCHES; + + return comp_flags; +} + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) */ +#endif + +/* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at + http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/. + This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. */ +void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip) +{ + /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */ + static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; + tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); + tdefl_output_buffer out_buf; + int i, bpl = w * num_chans, y, z; + mz_uint32 c; + *pLen_out = 0; + if (!pComp) + return NULL; + MZ_CLEAR_OBJ(out_buf); + out_buf.m_expandable = MZ_TRUE; + out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h); + if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity))) + { + MZ_FREE(pComp); + return NULL; + } + /* write dummy header */ + for (z = 41; z; --z) + tdefl_output_buffer_putter(&z, 1, &out_buf); + /* compress image data */ + tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER); + for (y = 0; y < h; ++y) + { + tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH); + tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH); + } + if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE) + { + MZ_FREE(pComp); + MZ_FREE(out_buf.m_pBuf); + return NULL; + } + /* write real header */ + *pLen_out = out_buf.m_size - 41; + { + static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 }; + mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, + 0x0a, 0x1a, 0x0a, 0x00, 0x00, + 0x00, 0x0d, 0x49, 0x48, 0x44, + 0x52, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x49, 0x44, 0x41, + 0x54 }; + pnghdr[18] = (mz_uint8)(w >> 8); + pnghdr[19] = (mz_uint8)w; + pnghdr[22] = (mz_uint8)(h >> 8); + pnghdr[23] = (mz_uint8)h; + pnghdr[25] = chans[num_chans]; + pnghdr[33] = (mz_uint8)(*pLen_out >> 24); + pnghdr[34] = (mz_uint8)(*pLen_out >> 16); + pnghdr[35] = (mz_uint8)(*pLen_out >> 8); + pnghdr[36] = (mz_uint8)*pLen_out; + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17); + for (i = 0; i < 4; ++i, c <<= 8) + ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24); + memcpy(out_buf.m_pBuf, pnghdr, 41); + } + /* write footer (IDAT CRC-32, followed by IEND chunk) */ + if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) + { + *pLen_out = 0; + MZ_FREE(pComp); + MZ_FREE(out_buf.m_pBuf); + return NULL; + } + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4); + for (i = 0; i < 4; ++i, c <<= 8) + (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24); + /* compute final size of file, grab compressed data buffer and return */ + *pLen_out += 57; + MZ_FREE(pComp); + return out_buf.m_pBuf; +} +void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out) +{ + /* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out) */ + return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE); +} + +#ifndef MINIZ_NO_MALLOC +/* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */ +/* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */ +/* structure size and allocation mechanism. */ +tdefl_compressor *tdefl_compressor_alloc() +{ + return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); +} + +void tdefl_compressor_free(tdefl_compressor *pComp) +{ + MZ_FREE(pComp); +} +#endif + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#ifdef __cplusplus +} +#endif + /************************************************************************** + * + * Copyright 2013-2014 RAD Game Tools and Valve Software + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + + + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------- Low-level Decompression (completely independent from all compression API's) */ + +#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l) +#define TINFL_MEMSET(p, c, l) memset(p, c, l) + +#define TINFL_CR_BEGIN \ + switch (r->m_state) \ + { \ + case 0: +#define TINFL_CR_RETURN(state_index, result) \ + do \ + { \ + status = result; \ + r->m_state = state_index; \ + goto common_exit; \ + case state_index:; \ + } \ + MZ_MACRO_END +#define TINFL_CR_RETURN_FOREVER(state_index, result) \ + do \ + { \ + for (;;) \ + { \ + TINFL_CR_RETURN(state_index, result); \ + } \ + } \ + MZ_MACRO_END +#define TINFL_CR_FINISH } + +#define TINFL_GET_BYTE(state_index, c) \ + do \ + { \ + while (pIn_buf_cur >= pIn_buf_end) \ + { \ + TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \ + } \ + c = *pIn_buf_cur++; \ + } \ + MZ_MACRO_END + +#define TINFL_NEED_BITS(state_index, n) \ + do \ + { \ + mz_uint c; \ + TINFL_GET_BYTE(state_index, c); \ + bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ + num_bits += 8; \ + } while (num_bits < (mz_uint)(n)) +#define TINFL_SKIP_BITS(state_index, n) \ + do \ + { \ + if (num_bits < (mz_uint)(n)) \ + { \ + TINFL_NEED_BITS(state_index, n); \ + } \ + bit_buf >>= (n); \ + num_bits -= (n); \ + } \ + MZ_MACRO_END +#define TINFL_GET_BITS(state_index, b, n) \ + do \ + { \ + if (num_bits < (mz_uint)(n)) \ + { \ + TINFL_NEED_BITS(state_index, n); \ + } \ + b = bit_buf & ((1 << (n)) - 1); \ + bit_buf >>= (n); \ + num_bits -= (n); \ + } \ + MZ_MACRO_END + +/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */ +/* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a */ +/* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the */ +/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */ +#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ + do \ + { \ + temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ + if (temp >= 0) \ + { \ + code_len = temp >> 9; \ + if ((code_len) && (num_bits >= code_len)) \ + break; \ + } \ + else if (num_bits > TINFL_FAST_LOOKUP_BITS) \ + { \ + code_len = TINFL_FAST_LOOKUP_BITS; \ + do \ + { \ + temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ + } while ((temp < 0) && (num_bits >= (code_len + 1))); \ + if (temp >= 0) \ + break; \ + } \ + TINFL_GET_BYTE(state_index, c); \ + bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ + num_bits += 8; \ + } while (num_bits < 15); + +/* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read */ +/* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully */ +/* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. */ +/* The slow path is only executed at the very end of the input buffer. */ +/* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes */ +/* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */ +#define TINFL_HUFF_DECODE(state_index, sym, pHuff) \ + do \ + { \ + int temp; \ + mz_uint code_len, c; \ + if (num_bits < 15) \ + { \ + if ((pIn_buf_end - pIn_buf_cur) < 2) \ + { \ + TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ + } \ + else \ + { \ + bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \ + pIn_buf_cur += 2; \ + num_bits += 16; \ + } \ + } \ + if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ + code_len = temp >> 9, temp &= 511; \ + else \ + { \ + code_len = TINFL_FAST_LOOKUP_BITS; \ + do \ + { \ + temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ + } while (temp < 0); \ + } \ + sym = temp; \ + bit_buf >>= code_len; \ + num_bits -= code_len; \ + } \ + MZ_MACRO_END + +tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags) +{ + static const int s_length_base[31] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; + static const int s_length_extra[31] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 }; + static const int s_dist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 }; + static const int s_dist_extra[32] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }; + static const mz_uint8 s_length_dezigzag[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + static const int s_min_table_sizes[3] = { 257, 1, 4 }; + + tinfl_status status = TINFL_STATUS_FAILED; + mz_uint32 num_bits, dist, counter, num_extra; + tinfl_bit_buf_t bit_buf; + const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; + mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; + size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; + + /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */ + if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) + { + *pIn_buf_size = *pOut_buf_size = 0; + return TINFL_STATUS_BAD_PARAM; + } + + num_bits = r->m_num_bits; + bit_buf = r->m_bit_buf; + dist = r->m_dist; + counter = r->m_counter; + num_extra = r->m_num_extra; + dist_from_out_buf_start = r->m_dist_from_out_buf_start; + TINFL_CR_BEGIN + + bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; + r->m_z_adler32 = r->m_check_adler32 = 1; + if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) + { + TINFL_GET_BYTE(1, r->m_zhdr0); + TINFL_GET_BYTE(2, r->m_zhdr1); + counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); + if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) + counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); + if (counter) + { + TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); + } + } + + do + { + TINFL_GET_BITS(3, r->m_final, 3); + r->m_type = r->m_final >> 1; + if (r->m_type == 0) + { + TINFL_SKIP_BITS(5, num_bits & 7); + for (counter = 0; counter < 4; ++counter) + { + if (num_bits) + TINFL_GET_BITS(6, r->m_raw_header[counter], 8); + else + TINFL_GET_BYTE(7, r->m_raw_header[counter]); + } + if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) + { + TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); + } + while ((counter) && (num_bits)) + { + TINFL_GET_BITS(51, dist, 8); + while (pOut_buf_cur >= pOut_buf_end) + { + TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); + } + *pOut_buf_cur++ = (mz_uint8)dist; + counter--; + } + while (counter) + { + size_t n; + while (pOut_buf_cur >= pOut_buf_end) + { + TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); + } + while (pIn_buf_cur >= pIn_buf_end) + { + TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); + } + n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter); + TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); + pIn_buf_cur += n; + pOut_buf_cur += n; + counter -= (mz_uint)n; + } + } + else if (r->m_type == 3) + { + TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); + } + else + { + if (r->m_type == 1) + { + mz_uint8 *p = r->m_tables[0].m_code_size; + mz_uint i; + r->m_table_sizes[0] = 288; + r->m_table_sizes[1] = 32; + TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); + for (i = 0; i <= 143; ++i) + *p++ = 8; + for (; i <= 255; ++i) + *p++ = 9; + for (; i <= 279; ++i) + *p++ = 7; + for (; i <= 287; ++i) + *p++ = 8; + } + else + { + for (counter = 0; counter < 3; counter++) + { + TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); + r->m_table_sizes[counter] += s_min_table_sizes[counter]; + } + MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); + for (counter = 0; counter < r->m_table_sizes[2]; counter++) + { + mz_uint s; + TINFL_GET_BITS(14, s, 3); + r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; + } + r->m_table_sizes[2] = 19; + } + for (; (int)r->m_type >= 0; r->m_type--) + { + int tree_next, tree_cur; + tinfl_huff_table *pTable; + mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; + pTable = &r->m_tables[r->m_type]; + MZ_CLEAR_OBJ(total_syms); + MZ_CLEAR_OBJ(pTable->m_look_up); + MZ_CLEAR_OBJ(pTable->m_tree); + for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) + total_syms[pTable->m_code_size[i]]++; + used_syms = 0, total = 0; + next_code[0] = next_code[1] = 0; + for (i = 1; i <= 15; ++i) + { + used_syms += total_syms[i]; + next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); + } + if ((65536 != total) && (used_syms > 1)) + { + TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); + } + for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) + { + mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; + if (!code_size) + continue; + cur_code = next_code[code_size]++; + for (l = code_size; l > 0; l--, cur_code >>= 1) + rev_code = (rev_code << 1) | (cur_code & 1); + if (code_size <= TINFL_FAST_LOOKUP_BITS) + { + mz_int16 k = (mz_int16)((code_size << 9) | sym_index); + while (rev_code < TINFL_FAST_LOOKUP_SIZE) + { + pTable->m_look_up[rev_code] = k; + rev_code += (1 << code_size); + } + continue; + } + if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) + { + pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; + tree_cur = tree_next; + tree_next -= 2; + } + rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); + for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) + { + tree_cur -= ((rev_code >>= 1) & 1); + if (!pTable->m_tree[-tree_cur - 1]) + { + pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; + tree_cur = tree_next; + tree_next -= 2; + } + else + tree_cur = pTable->m_tree[-tree_cur - 1]; + } + tree_cur -= ((rev_code >>= 1) & 1); + pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; + } + if (r->m_type == 2) + { + for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) + { + mz_uint s; + TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); + if (dist < 16) + { + r->m_len_codes[counter++] = (mz_uint8)dist; + continue; + } + if ((dist == 16) && (!counter)) + { + TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); + } + num_extra = "\02\03\07"[dist - 16]; + TINFL_GET_BITS(18, s, num_extra); + s += "\03\03\013"[dist - 16]; + TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); + counter += s; + } + if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) + { + TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); + } + TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); + TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); + } + } + for (;;) + { + mz_uint8 *pSrc; + for (;;) + { + if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) + { + TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); + if (counter >= 256) + break; + while (pOut_buf_cur >= pOut_buf_end) + { + TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); + } + *pOut_buf_cur++ = (mz_uint8)counter; + } + else + { + int sym2; + mz_uint code_len; +#if TINFL_USE_64BIT_BITBUF + if (num_bits < 30) + { + bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); + pIn_buf_cur += 4; + num_bits += 32; + } +#else + if (num_bits < 15) + { + bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); + pIn_buf_cur += 2; + num_bits += 16; + } +#endif + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) + code_len = sym2 >> 9; + else + { + code_len = TINFL_FAST_LOOKUP_BITS; + do + { + sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; + } while (sym2 < 0); + } + counter = sym2; + bit_buf >>= code_len; + num_bits -= code_len; + if (counter & 256) + break; + +#if !TINFL_USE_64BIT_BITBUF + if (num_bits < 15) + { + bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); + pIn_buf_cur += 2; + num_bits += 16; + } +#endif + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) + code_len = sym2 >> 9; + else + { + code_len = TINFL_FAST_LOOKUP_BITS; + do + { + sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; + } while (sym2 < 0); + } + bit_buf >>= code_len; + num_bits -= code_len; + + pOut_buf_cur[0] = (mz_uint8)counter; + if (sym2 & 256) + { + pOut_buf_cur++; + counter = sym2; + break; + } + pOut_buf_cur[1] = (mz_uint8)sym2; + pOut_buf_cur += 2; + } + } + if ((counter &= 511) == 256) + break; + + num_extra = s_length_extra[counter - 257]; + counter = s_length_base[counter - 257]; + if (num_extra) + { + mz_uint extra_bits; + TINFL_GET_BITS(25, extra_bits, num_extra); + counter += extra_bits; + } + + TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); + num_extra = s_dist_extra[dist]; + dist = s_dist_base[dist]; + if (num_extra) + { + mz_uint extra_bits; + TINFL_GET_BITS(27, extra_bits, num_extra); + dist += extra_bits; + } + + dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; + if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) + { + TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); + } + + pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask); + + if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) + { + while (counter--) + { + while (pOut_buf_cur >= pOut_buf_end) + { + TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); + } + *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask]; + } + continue; + } +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES + else if ((counter >= 9) && (counter <= dist)) + { + const mz_uint8 *pSrc_end = pSrc + (counter & ~7); + do + { +#ifdef MINIZ_UNALIGNED_USE_MEMCPY + memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32)*2); +#else + ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0]; + ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1]; +#endif + pOut_buf_cur += 8; + } while ((pSrc += 8) < pSrc_end); + if ((counter &= 7) < 3) + { + if (counter) + { + pOut_buf_cur[0] = pSrc[0]; + if (counter > 1) + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur += counter; + } + continue; + } + } +#endif + while(counter>2) + { + pOut_buf_cur[0] = pSrc[0]; + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur[2] = pSrc[2]; + pOut_buf_cur += 3; + pSrc += 3; + counter -= 3; + } + if (counter > 0) + { + pOut_buf_cur[0] = pSrc[0]; + if (counter > 1) + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur += counter; + } + } + } + } while (!(r->m_final & 1)); + + /* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */ + /* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */ + TINFL_SKIP_BITS(32, num_bits & 7); + while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) + { + --pIn_buf_cur; + num_bits -= 8; + } + bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); + MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams). */ + + if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) + { + for (counter = 0; counter < 4; ++counter) + { + mz_uint s; + if (num_bits) + TINFL_GET_BITS(41, s, 8); + else + TINFL_GET_BYTE(42, s); + r->m_z_adler32 = (r->m_z_adler32 << 8) | s; + } + } + TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); + + TINFL_CR_FINISH + +common_exit: + /* As long as we aren't telling the caller that we NEED more input to make forward progress: */ + /* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */ + /* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */ + if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) + { + while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) + { + --pIn_buf_cur; + num_bits -= 8; + } + } + r->m_num_bits = num_bits; + r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); + r->m_dist = dist; + r->m_counter = counter; + r->m_num_extra = num_extra; + r->m_dist_from_out_buf_start = dist_from_out_buf_start; + *pIn_buf_size = pIn_buf_cur - pIn_buf_next; + *pOut_buf_size = pOut_buf_cur - pOut_buf_next; + if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) + { + const mz_uint8 *ptr = pOut_buf_next; + size_t buf_len = *pOut_buf_size; + mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; + size_t block_len = buf_len % 5552; + while (buf_len) + { + for (i = 0; i + 7 < block_len; i += 8, ptr += 8) + { + s1 += ptr[0], s2 += s1; + s1 += ptr[1], s2 += s1; + s1 += ptr[2], s2 += s1; + s1 += ptr[3], s2 += s1; + s1 += ptr[4], s2 += s1; + s1 += ptr[5], s2 += s1; + s1 += ptr[6], s2 += s1; + s1 += ptr[7], s2 += s1; + } + for (; i < block_len; ++i) + s1 += *ptr++, s2 += s1; + s1 %= 65521U, s2 %= 65521U; + buf_len -= block_len; + block_len = 5552; + } + r->m_check_adler32 = (s2 << 16) + s1; + if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) + status = TINFL_STATUS_ADLER32_MISMATCH; + } + return status; +} + +/* Higher level helper functions. */ +void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) +{ + tinfl_decompressor decomp; + void *pBuf = NULL, *pNew_buf; + size_t src_buf_ofs = 0, out_buf_capacity = 0; + *pOut_len = 0; + tinfl_init(&decomp); + for (;;) + { + size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity; + tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8 *)pBuf, pBuf ? (mz_uint8 *)pBuf + *pOut_len : NULL, &dst_buf_size, + (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); + if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) + { + MZ_FREE(pBuf); + *pOut_len = 0; + return NULL; + } + src_buf_ofs += src_buf_size; + *pOut_len += dst_buf_size; + if (status == TINFL_STATUS_DONE) + break; + new_out_buf_capacity = out_buf_capacity * 2; + if (new_out_buf_capacity < 128) + new_out_buf_capacity = 128; + pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity); + if (!pNew_buf) + { + MZ_FREE(pBuf); + *pOut_len = 0; + return NULL; + } + pBuf = pNew_buf; + out_buf_capacity = new_out_buf_capacity; + } + return pBuf; +} + +size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags) +{ + tinfl_decompressor decomp; + tinfl_status status; + tinfl_init(&decomp); + status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); + return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len; +} + +int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + int result = 0; + tinfl_decompressor decomp; + mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE); + size_t in_buf_ofs = 0, dict_ofs = 0; + if (!pDict) + return TINFL_STATUS_FAILED; + tinfl_init(&decomp); + for (;;) + { + size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; + tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size, + (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))); + in_buf_ofs += in_buf_size; + if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) + break; + if (status != TINFL_STATUS_HAS_MORE_OUTPUT) + { + result = (status == TINFL_STATUS_DONE); + break; + } + dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1); + } + MZ_FREE(pDict); + *pIn_buf_size = in_buf_ofs; + return result; +} + +#ifndef MINIZ_NO_MALLOC +tinfl_decompressor *tinfl_decompressor_alloc() +{ + tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor)); + if (pDecomp) + tinfl_init(pDecomp); + return pDecomp; +} + +void tinfl_decompressor_free(tinfl_decompressor *pDecomp) +{ + MZ_FREE(pDecomp); +} +#endif + +#ifdef __cplusplus +} +#endif + /************************************************************************** + * + * Copyright 2013-2014 RAD Game Tools and Valve Software + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC + * Copyright 2016 Martin Raiber + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + + +#ifndef MINIZ_NO_ARCHIVE_APIS + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------- .ZIP archive reading */ + +#ifdef MINIZ_NO_STDIO +#define MZ_FILE void * +#else +#include <sys/stat.h> + +#if defined(_MSC_VER) || defined(__MINGW64__) +static FILE *mz_fopen(const char *pFilename, const char *pMode) +{ + FILE *pFile = NULL; + fopen_s(&pFile, pFilename, pMode); + return pFile; +} +static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) +{ + FILE *pFile = NULL; + if (freopen_s(&pFile, pPath, pMode, pStream)) + return NULL; + return pFile; +} +#ifndef MINIZ_NO_TIME +#include <sys/utime.h> +#endif +#define MZ_FOPEN mz_fopen +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#define MZ_FTELL64 _ftelli64 +#define MZ_FSEEK64 _fseeki64 +#define MZ_FILE_STAT_STRUCT _stat64 +#define MZ_FILE_STAT _stat64 +#define MZ_FFLUSH fflush +#define MZ_FREOPEN mz_freopen +#define MZ_DELETE_FILE remove +#elif defined(__MINGW32__) +#ifndef MINIZ_NO_TIME +#include <sys/utime.h> +#endif +#define MZ_FOPEN(f, m) fopen(f, m) +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#define MZ_FTELL64 ftello64 +#define MZ_FSEEK64 fseeko64 +#define MZ_FILE_STAT_STRUCT _stat +#define MZ_FILE_STAT _stat +#define MZ_FFLUSH fflush +#define MZ_FREOPEN(f, m, s) freopen(f, m, s) +#define MZ_DELETE_FILE remove +#elif defined(__TINYC__) +#ifndef MINIZ_NO_TIME +#include <sys/utime.h> +#endif +#define MZ_FOPEN(f, m) fopen(f, m) +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#define MZ_FTELL64 ftell +#define MZ_FSEEK64 fseek +#define MZ_FILE_STAT_STRUCT stat +#define MZ_FILE_STAT stat +#define MZ_FFLUSH fflush +#define MZ_FREOPEN(f, m, s) freopen(f, m, s) +#define MZ_DELETE_FILE remove +#elif defined(__USE_LARGEFILE64) /* gcc, clang */ +#ifndef MINIZ_NO_TIME +#include <utime.h> +#endif +#define MZ_FOPEN(f, m) fopen64(f, m) +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#define MZ_FTELL64 ftello64 +#define MZ_FSEEK64 fseeko64 +#define MZ_FILE_STAT_STRUCT stat64 +#define MZ_FILE_STAT stat64 +#define MZ_FFLUSH fflush +#define MZ_FREOPEN(p, m, s) freopen64(p, m, s) +#define MZ_DELETE_FILE remove +#elif defined(__APPLE__) +#ifndef MINIZ_NO_TIME +#include <utime.h> +#endif +#define MZ_FOPEN(f, m) fopen(f, m) +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#define MZ_FTELL64 ftello +#define MZ_FSEEK64 fseeko +#define MZ_FILE_STAT_STRUCT stat +#define MZ_FILE_STAT stat +#define MZ_FFLUSH fflush +#define MZ_FREOPEN(p, m, s) freopen(p, m, s) +#define MZ_DELETE_FILE remove + +#else +#pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.") +#ifndef MINIZ_NO_TIME +#include <utime.h> +#endif +#define MZ_FOPEN(f, m) fopen(f, m) +#define MZ_FCLOSE fclose +#define MZ_FREAD fread +#define MZ_FWRITE fwrite +#ifdef __STRICT_ANSI__ +#define MZ_FTELL64 ftell +#define MZ_FSEEK64 fseek +#else +#define MZ_FTELL64 ftello +#define MZ_FSEEK64 fseeko +#endif +#define MZ_FILE_STAT_STRUCT stat +#define MZ_FILE_STAT stat +#define MZ_FFLUSH fflush +#define MZ_FREOPEN(f, m, s) freopen(f, m, s) +#define MZ_DELETE_FILE remove +#endif /* #ifdef _MSC_VER */ +#endif /* #ifdef MINIZ_NO_STDIO */ + +#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c)) + +/* Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff. */ +enum +{ + /* ZIP archive identifiers and record sizes */ + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, + MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, + MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50, + MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30, + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46, + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22, + + /* ZIP64 archive identifier and record sizes */ + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50, + MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50, + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56, + MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20, + MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001, + MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50, + MZ_ZIP_DATA_DESCRIPTER_SIZE64 = 24, + MZ_ZIP_DATA_DESCRIPTER_SIZE32 = 16, + + /* Central directory header record offsets */ + MZ_ZIP_CDH_SIG_OFS = 0, + MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4, + MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6, + MZ_ZIP_CDH_BIT_FLAG_OFS = 8, + MZ_ZIP_CDH_METHOD_OFS = 10, + MZ_ZIP_CDH_FILE_TIME_OFS = 12, + MZ_ZIP_CDH_FILE_DATE_OFS = 14, + MZ_ZIP_CDH_CRC32_OFS = 16, + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20, + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, + MZ_ZIP_CDH_FILENAME_LEN_OFS = 28, + MZ_ZIP_CDH_EXTRA_LEN_OFS = 30, + MZ_ZIP_CDH_COMMENT_LEN_OFS = 32, + MZ_ZIP_CDH_DISK_START_OFS = 34, + MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36, + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38, + MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42, + + /* Local directory header offsets */ + MZ_ZIP_LDH_SIG_OFS = 0, + MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4, + MZ_ZIP_LDH_BIT_FLAG_OFS = 6, + MZ_ZIP_LDH_METHOD_OFS = 8, + MZ_ZIP_LDH_FILE_TIME_OFS = 10, + MZ_ZIP_LDH_FILE_DATE_OFS = 12, + MZ_ZIP_LDH_CRC32_OFS = 14, + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18, + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22, + MZ_ZIP_LDH_FILENAME_LEN_OFS = 26, + MZ_ZIP_LDH_EXTRA_LEN_OFS = 28, + MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR = 1 << 3, + + /* End of central directory offsets */ + MZ_ZIP_ECDH_SIG_OFS = 0, + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4, + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8, + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, + MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12, + MZ_ZIP_ECDH_CDIR_OFS_OFS = 16, + MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20, + + /* ZIP64 End of central directory locator offsets */ + MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */ + MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, /* 4 bytes */ + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, /* 8 bytes */ + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, /* 4 bytes */ + + /* ZIP64 End of central directory header offsets */ + MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */ + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, /* 8 bytes */ + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, /* 2 bytes */ + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, /* 2 bytes */ + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, /* 4 bytes */ + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, /* 4 bytes */ + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, /* 8 bytes */ + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, /* 8 bytes */ + MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, /* 8 bytes */ + MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */ + MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0, + MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10, + MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1, + MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32, + MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64, + MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192, + MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 = 1 << 11 +}; + +typedef struct +{ + void *m_p; + size_t m_size, m_capacity; + mz_uint m_element_size; +} mz_zip_array; + +struct mz_zip_internal_state_tag +{ + mz_zip_array m_central_dir; + mz_zip_array m_central_dir_offsets; + mz_zip_array m_sorted_central_dir_offsets; + + /* The flags passed in when the archive is initially opened. */ + uint32_t m_init_flags; + + /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */ + mz_bool m_zip64; + + /* MZ_TRUE if we found zip64 extended info in the central directory (m_zip64 will also be slammed to true too, even if we didn't find a zip64 end of central dir header, etc.) */ + mz_bool m_zip64_has_extended_info_fields; + + /* These fields are used by the file, FILE, memory, and memory/heap read/write helpers. */ + MZ_FILE *m_pFile; + mz_uint64 m_file_archive_start_ofs; + + void *m_pMem; + size_t m_mem_size; + size_t m_mem_capacity; +}; + +#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size + +#if defined(DEBUG) || defined(_DEBUG) +static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index) +{ + MZ_ASSERT(index < pArray->m_size); + return index; +} +#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)] +#else +#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index] +#endif + +static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size) +{ + memset(pArray, 0, sizeof(mz_zip_array)); + pArray->m_element_size = element_size; +} + +static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray) +{ + pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p); + memset(pArray, 0, sizeof(mz_zip_array)); +} + +static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing) +{ + void *pNew_p; + size_t new_capacity = min_new_capacity; + MZ_ASSERT(pArray->m_element_size); + if (pArray->m_capacity >= min_new_capacity) + return MZ_TRUE; + if (growing) + { + new_capacity = MZ_MAX(1, pArray->m_capacity); + while (new_capacity < min_new_capacity) + new_capacity *= 2; + } + if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity))) + return MZ_FALSE; + pArray->m_p = pNew_p; + pArray->m_capacity = new_capacity; + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing) +{ + if (new_capacity > pArray->m_capacity) + { + if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) + return MZ_FALSE; + } + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing) +{ + if (new_size > pArray->m_capacity) + { + if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) + return MZ_FALSE; + } + pArray->m_size = new_size; + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n) +{ + return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE); +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n) +{ + size_t orig_size = pArray->m_size; + if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) + return MZ_FALSE; + if (n > 0) + memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size); + return MZ_TRUE; +} + +#ifndef MINIZ_NO_TIME +static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date) +{ + struct tm tm; + memset(&tm, 0, sizeof(tm)); + tm.tm_isdst = -1; + tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900; + tm.tm_mon = ((dos_date >> 5) & 15) - 1; + tm.tm_mday = dos_date & 31; + tm.tm_hour = (dos_time >> 11) & 31; + tm.tm_min = (dos_time >> 5) & 63; + tm.tm_sec = (dos_time << 1) & 62; + return mktime(&tm); +} + +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS +static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date) +{ +#ifdef _MSC_VER + struct tm tm_struct; + struct tm *tm = &tm_struct; + errno_t err = localtime_s(tm, &time); + if (err) + { + *pDOS_date = 0; + *pDOS_time = 0; + return; + } +#else + struct tm *tm = localtime(&time); +#endif /* #ifdef _MSC_VER */ + + *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1)); + *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday); +} +#endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */ + +#ifndef MINIZ_NO_STDIO +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS +static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime) +{ + struct MZ_FILE_STAT_STRUCT file_stat; + + /* On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. */ + if (MZ_FILE_STAT(pFilename, &file_stat) != 0) + return MZ_FALSE; + + *pTime = file_stat.st_mtime; + + return MZ_TRUE; +} +#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/ + +static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time) +{ + struct utimbuf t; + + memset(&t, 0, sizeof(t)); + t.actime = access_time; + t.modtime = modified_time; + + return !utime(pFilename, &t); +} +#endif /* #ifndef MINIZ_NO_STDIO */ +#endif /* #ifndef MINIZ_NO_TIME */ + +static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num) +{ + if (pZip) + pZip->m_last_error = err_num; + return MZ_FALSE; +} + +static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags) +{ + (void)flags; + if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!pZip->m_pAlloc) + pZip->m_pAlloc = miniz_def_alloc_func; + if (!pZip->m_pFree) + pZip->m_pFree = miniz_def_free_func; + if (!pZip->m_pRealloc) + pZip->m_pRealloc = miniz_def_realloc_func; + + pZip->m_archive_size = 0; + pZip->m_central_directory_file_ofs = 0; + pZip->m_total_files = 0; + pZip->m_last_error = MZ_ZIP_NO_ERROR; + + if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); + pZip->m_pState->m_init_flags = flags; + pZip->m_pState->m_zip64 = MZ_FALSE; + pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE; + + pZip->m_zip_mode = MZ_ZIP_MODE_READING; + + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index) +{ + const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE; + const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index)); + mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS); + mz_uint8 l = 0, r = 0; + pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + pE = pL + MZ_MIN(l_len, r_len); + while (pL < pE) + { + if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) + break; + pL++; + pR++; + } + return (pL == pE) ? (l_len < r_len) : (l < r); +} + +#define MZ_SWAP_UINT32(a, b) \ + do \ + { \ + mz_uint32 t = a; \ + a = b; \ + b = t; \ + } \ + MZ_MACRO_END + +/* Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.) */ +static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip) +{ + mz_zip_internal_state *pState = pZip->m_pState; + const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; + const mz_zip_array *pCentral_dir = &pState->m_central_dir; + mz_uint32 *pIndices; + mz_uint32 start, end; + const mz_uint32 size = pZip->m_total_files; + + if (size <= 1U) + return; + + pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); + + start = (size - 2U) >> 1U; + for (;;) + { + mz_uint64 child, root = start; + for (;;) + { + if ((child = (root << 1U) + 1U) >= size) + break; + child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]))); + if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) + break; + MZ_SWAP_UINT32(pIndices[root], pIndices[child]); + root = child; + } + if (!start) + break; + start--; + } + + end = size - 1; + while (end > 0) + { + mz_uint64 child, root = 0; + MZ_SWAP_UINT32(pIndices[end], pIndices[0]); + for (;;) + { + if ((child = (root << 1U) + 1U) >= end) + break; + child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])); + if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) + break; + MZ_SWAP_UINT32(pIndices[root], pIndices[child]); + root = child; + } + end--; + } +} + +static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs) +{ + mz_int64 cur_file_ofs; + mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; + mz_uint8 *pBuf = (mz_uint8 *)buf_u32; + + /* Basic sanity checks - reject files which are too small */ + if (pZip->m_archive_size < record_size) + return MZ_FALSE; + + /* Find the record by scanning the file from the end towards the beginning. */ + cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0); + for (;;) + { + int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs); + + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n) + return MZ_FALSE; + + for (i = n - 4; i >= 0; --i) + { + mz_uint s = MZ_READ_LE32(pBuf + i); + if (s == record_sig) + { + if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size) + break; + } + } + + if (i >= 0) + { + cur_file_ofs += i; + break; + } + + /* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */ + if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (MZ_UINT16_MAX + record_size))) + return MZ_FALSE; + + cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); + } + + *pOfs = cur_file_ofs; + return MZ_TRUE; +} + +static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags) +{ + mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0; + mz_uint64 cdir_ofs = 0; + mz_int64 cur_file_ofs = 0; + const mz_uint8 *p; + + mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; + mz_uint8 *pBuf = (mz_uint8 *)buf_u32; + mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0); + mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pZip64_locator = (mz_uint8 *)zip64_end_of_central_dir_locator_u32; + + mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pZip64_end_of_central_dir = (mz_uint8 *)zip64_end_of_central_dir_header_u32; + + mz_uint64 zip64_end_of_central_dir_ofs = 0; + + /* Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there. */ + if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + + if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs)) + return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR); + + /* Read and verify the end of central directory record. */ + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + + if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) + { + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) + { + if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) + { + zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS); + if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + + if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) + { + if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) + { + pZip->m_pState->m_zip64 = MZ_TRUE; + } + } + } + } + } + + pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS); + cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS); + num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS); + cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS); + cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS); + cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS); + + if (pZip->m_pState->m_zip64) + { + mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS); + mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS); + mz_uint64 zip64_cdir_total_entries_on_this_disk = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS); + mz_uint64 zip64_size_of_end_of_central_dir_record = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS); + mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS); + + if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if (zip64_total_num_of_disks != 1U) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); + + /* Check for miniz's practical limits */ + if (zip64_cdir_total_entries > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + + pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries; + + if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + + cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk; + + /* Check for miniz's current practical limits (sorry, this should be enough for millions of files) */ + if (zip64_size_of_central_directory > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + + cdir_size = (mz_uint32)zip64_size_of_central_directory; + + num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS); + + cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS); + + cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS); + } + + if (pZip->m_total_files != cdir_entries_on_this_disk) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); + + if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); + + if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + pZip->m_central_directory_file_ofs = cdir_ofs; + + if (pZip->m_total_files) + { + mz_uint i, n; + /* Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and possibly another to hold the sorted indices. */ + if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || + (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + if (sort_central_dir) + { + if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + /* Now create an index into the central directory file records, do some basic sanity checking on each record */ + p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p; + for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) + { + mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size; + mz_uint64 comp_size, decomp_size, local_header_ofs; + + if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p); + + if (sort_central_dir) + MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i; + + comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); + filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS); + + if ((!pZip->m_pState->m_zip64_has_extended_info_fields) && + (ext_data_size) && + (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX)) + { + /* Attempt to find zip64 extended information field in the entry's extra data */ + mz_uint32 extra_size_remaining = ext_data_size; + + if (extra_size_remaining) + { + const mz_uint8 *pExtra_data; + void* buf = NULL; + + if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n) + { + buf = MZ_MALLOC(ext_data_size); + if(buf==NULL) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size, buf, ext_data_size) != ext_data_size) + { + MZ_FREE(buf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + + pExtra_data = (mz_uint8*)buf; + } + else + { + pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size; + } + + do + { + mz_uint32 field_id; + mz_uint32 field_data_size; + + if (extra_size_remaining < (sizeof(mz_uint16) * 2)) + { + MZ_FREE(buf); + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + field_id = MZ_READ_LE16(pExtra_data); + field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); + + if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) + { + MZ_FREE(buf); + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) + { + /* Ok, the archive didn't have any zip64 headers but it uses a zip64 extended information field so mark it as zip64 anyway (this can occur with infozip's zip util when it reads compresses files from stdin). */ + pZip->m_pState->m_zip64 = MZ_TRUE; + pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE; + break; + } + + pExtra_data += sizeof(mz_uint16) * 2 + field_data_size; + extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size; + } while (extra_size_remaining); + + MZ_FREE(buf); + } + } + + /* I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh */ + if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) + { + if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS); + if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1))) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); + + if (comp_size != MZ_UINT32_MAX) + { + if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + + if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + n -= total_header_size; + p += total_header_size; + } + } + + if (sort_central_dir) + mz_zip_reader_sort_central_dir_offsets_by_filename(pZip); + + return MZ_TRUE; +} + +void mz_zip_zero_struct(mz_zip_archive *pZip) +{ + if (pZip) + MZ_CLEAR_OBJ(*pZip); +} + +static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error) +{ + mz_bool status = MZ_TRUE; + + if (!pZip) + return MZ_FALSE; + + if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + { + if (set_last_error) + pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER; + + return MZ_FALSE; + } + + if (pZip->m_pState) + { + mz_zip_internal_state *pState = pZip->m_pState; + pZip->m_pState = NULL; + + mz_zip_array_clear(pZip, &pState->m_central_dir); + mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); + mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); + +#ifndef MINIZ_NO_STDIO + if (pState->m_pFile) + { + if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) + { + if (MZ_FCLOSE(pState->m_pFile) == EOF) + { + if (set_last_error) + pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED; + status = MZ_FALSE; + } + } + pState->m_pFile = NULL; + } +#endif /* #ifndef MINIZ_NO_STDIO */ + + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + } + pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; + + return status; +} + +mz_bool mz_zip_reader_end(mz_zip_archive *pZip) +{ + return mz_zip_reader_end_internal(pZip, MZ_TRUE); +} +mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags) +{ + if ((!pZip) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_reader_init_internal(pZip, flags)) + return MZ_FALSE; + + pZip->m_zip_type = MZ_ZIP_TYPE_USER; + pZip->m_archive_size = size; + + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end_internal(pZip, MZ_FALSE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n); + memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s); + return s; +} + +mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags) +{ + if (!pMem) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + + if (!mz_zip_reader_init_internal(pZip, flags)) + return MZ_FALSE; + + pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY; + pZip->m_archive_size = size; + pZip->m_pRead = mz_zip_mem_read_func; + pZip->m_pIO_opaque = pZip; + pZip->m_pNeeds_keepalive = NULL; + +#ifdef __cplusplus + pZip->m_pState->m_pMem = const_cast<void *>(pMem); +#else + pZip->m_pState->m_pMem = (void *)pMem; +#endif + + pZip->m_pState->m_mem_size = size; + + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end_internal(pZip, MZ_FALSE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); + + file_ofs += pZip->m_pState->m_file_archive_start_ofs; + + if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) + return 0; + + return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile); +} + +mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags) +{ + return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0); +} + +mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags, mz_uint64 file_start_ofs, mz_uint64 archive_size) +{ + mz_uint64 file_size; + MZ_FILE *pFile; + + if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE))) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pFile = MZ_FOPEN(pFilename, "rb"); + if (!pFile) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + + file_size = archive_size; + if (!file_size) + { + if (MZ_FSEEK64(pFile, 0, SEEK_END)) + { + MZ_FCLOSE(pFile); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); + } + + file_size = MZ_FTELL64(pFile); + } + + /* TODO: Better sanity check archive_size and the # of actual remaining bytes */ + + if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + { + MZ_FCLOSE(pFile); + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + } + + if (!mz_zip_reader_init_internal(pZip, flags)) + { + MZ_FCLOSE(pFile); + return MZ_FALSE; + } + + pZip->m_zip_type = MZ_ZIP_TYPE_FILE; + pZip->m_pRead = mz_zip_file_read_func; + pZip->m_pIO_opaque = pZip; + pZip->m_pState->m_pFile = pFile; + pZip->m_archive_size = file_size; + pZip->m_pState->m_file_archive_start_ofs = file_start_ofs; + + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end_internal(pZip, MZ_FALSE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags) +{ + mz_uint64 cur_file_ofs; + + if ((!pZip) || (!pFile)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + + cur_file_ofs = MZ_FTELL64(pFile); + + if (!archive_size) + { + if (MZ_FSEEK64(pFile, 0, SEEK_END)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); + + archive_size = MZ_FTELL64(pFile) - cur_file_ofs; + + if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); + } + + if (!mz_zip_reader_init_internal(pZip, flags)) + return MZ_FALSE; + + pZip->m_zip_type = MZ_ZIP_TYPE_CFILE; + pZip->m_pRead = mz_zip_file_read_func; + + pZip->m_pIO_opaque = pZip; + pZip->m_pState->m_pFile = pFile; + pZip->m_archive_size = archive_size; + pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs; + + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end_internal(pZip, MZ_FALSE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +#endif /* #ifndef MINIZ_NO_STDIO */ + +static MZ_FORCEINLINE const mz_uint8 *mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index) +{ + if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) + return NULL; + return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); +} + +mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index) +{ + mz_uint m_bit_flag; + const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + if (!p) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return MZ_FALSE; + } + + m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + return (m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0; +} + +mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index) +{ + mz_uint bit_flag; + mz_uint method; + + const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + if (!p) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return MZ_FALSE; + } + + method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); + bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + + if ((method != 0) && (method != MZ_DEFLATED)) + { + mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); + return MZ_FALSE; + } + + if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) + { + mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + return MZ_FALSE; + } + + if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG) + { + mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index) +{ + mz_uint filename_len, attribute_mapping_id, external_attr; + const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + if (!p) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return MZ_FALSE; + } + + filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + if (filename_len) + { + if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/') + return MZ_TRUE; + } + + /* Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. */ + /* Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS directory flag and ignore the source OS ID in the created by field. */ + /* FIXME: Remove this check? Is it necessary - we already check the filename. */ + attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8; + (void)attribute_mapping_id; + + external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); + if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0) + { + return MZ_TRUE; + } + + return MZ_FALSE; +} + +static mz_bool mz_zip_file_stat_internal(mz_zip_archive *pZip, mz_uint file_index, const mz_uint8 *pCentral_dir_header, mz_zip_archive_file_stat *pStat, mz_bool *pFound_zip64_extra_data) +{ + mz_uint n; + const mz_uint8 *p = pCentral_dir_header; + + if (pFound_zip64_extra_data) + *pFound_zip64_extra_data = MZ_FALSE; + + if ((!p) || (!pStat)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + /* Extract fields from the central directory record. */ + pStat->m_file_index = file_index; + pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index); + pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS); + pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS); + pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); +#ifndef MINIZ_NO_TIME + pStat->m_time = mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS)); +#endif + pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS); + pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS); + pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); + pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); + + /* Copy as much of the filename and comment as possible. */ + n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1); + memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); + pStat->m_filename[n] = '\0'; + + n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS); + n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1); + pStat->m_comment_size = n; + memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), n); + pStat->m_comment[n] = '\0'; + + /* Set some flags for convienance */ + pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index); + pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index); + pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index); + + /* See if we need to read any zip64 extended information fields. */ + /* Confusingly, these zip64 fields can be present even on non-zip64 archives (Debian zip on a huge files from stdin piped to stdout creates them). */ + if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX) + { + /* Attempt to find zip64 extended information field in the entry's extra data */ + mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS); + + if (extra_size_remaining) + { + const mz_uint8 *pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + + do + { + mz_uint32 field_id; + mz_uint32 field_data_size; + + if (extra_size_remaining < (sizeof(mz_uint16) * 2)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + field_id = MZ_READ_LE16(pExtra_data); + field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); + + if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) + { + const mz_uint8 *pField_data = pExtra_data + sizeof(mz_uint16) * 2; + mz_uint32 field_data_remaining = field_data_size; + + if (pFound_zip64_extra_data) + *pFound_zip64_extra_data = MZ_TRUE; + + if (pStat->m_uncomp_size == MZ_UINT32_MAX) + { + if (field_data_remaining < sizeof(mz_uint64)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + pStat->m_uncomp_size = MZ_READ_LE64(pField_data); + pField_data += sizeof(mz_uint64); + field_data_remaining -= sizeof(mz_uint64); + } + + if (pStat->m_comp_size == MZ_UINT32_MAX) + { + if (field_data_remaining < sizeof(mz_uint64)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + pStat->m_comp_size = MZ_READ_LE64(pField_data); + pField_data += sizeof(mz_uint64); + field_data_remaining -= sizeof(mz_uint64); + } + + if (pStat->m_local_header_ofs == MZ_UINT32_MAX) + { + if (field_data_remaining < sizeof(mz_uint64)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + pStat->m_local_header_ofs = MZ_READ_LE64(pField_data); + pField_data += sizeof(mz_uint64); + field_data_remaining -= sizeof(mz_uint64); + } + + break; + } + + pExtra_data += sizeof(mz_uint16) * 2 + field_data_size; + extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size; + } while (extra_size_remaining); + } + } + + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags) +{ + mz_uint i; + if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) + return 0 == memcmp(pA, pB, len); + for (i = 0; i < len; ++i) + if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) + return MZ_FALSE; + return MZ_TRUE; +} + +static MZ_FORCEINLINE int mz_zip_filename_compare(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, mz_uint r_len) +{ + const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE; + mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS); + mz_uint8 l = 0, r = 0; + pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + pE = pL + MZ_MIN(l_len, r_len); + while (pL < pE) + { + if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) + break; + pL++; + pR++; + } + return (pL == pE) ? (int)(l_len - r_len) : (l - r); +} + +static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex) +{ + mz_zip_internal_state *pState = pZip->m_pState; + const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; + const mz_zip_array *pCentral_dir = &pState->m_central_dir; + mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); + const uint32_t size = pZip->m_total_files; + const mz_uint filename_len = (mz_uint)strlen(pFilename); + + if (pIndex) + *pIndex = 0; + + if (size) + { + /* yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and */ + /* honestly the major expense here on 32-bit CPU's will still be the filename compare */ + mz_int64 l = 0, h = (mz_int64)size - 1; + + while (l <= h) + { + mz_int64 m = l + ((h - l) >> 1); + uint32_t file_index = pIndices[(uint32_t)m]; + + int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len); + if (!comp) + { + if (pIndex) + *pIndex = file_index; + return MZ_TRUE; + } + else if (comp < 0) + l = m + 1; + else + h = m - 1; + } + } + + return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); +} + +int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags) +{ + mz_uint32 index; + if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index)) + return -1; + else + return (int)index; +} + +mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex) +{ + mz_uint file_index; + size_t name_len, comment_len; + + if (pIndex) + *pIndex = 0; + + if ((!pZip) || (!pZip->m_pState) || (!pName)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + /* See if we can use a binary search */ + if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) && + (pZip->m_zip_mode == MZ_ZIP_MODE_READING) && + ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) + { + return mz_zip_locate_file_binary_search(pZip, pName, pIndex); + } + + /* Locate the entry by scanning the entire central directory */ + name_len = strlen(pName); + if (name_len > MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + comment_len = pComment ? strlen(pComment) : 0; + if (comment_len > MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + for (file_index = 0; file_index < pZip->m_total_files; file_index++) + { + const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); + mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS); + const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + if (filename_len < name_len) + continue; + if (comment_len) + { + mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS); + const char *pFile_comment = pFilename + filename_len + file_extra_len; + if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags))) + continue; + } + if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) + { + int ofs = filename_len - 1; + do + { + if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) + break; + } while (--ofs >= 0); + ofs++; + pFilename += ofs; + filename_len -= ofs; + } + if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags))) + { + if (pIndex) + *pIndex = file_index; + return MZ_TRUE; + } + } + + return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); +} + +mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +{ + int status = TINFL_STATUS_DONE; + mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; + mz_zip_archive_file_stat file_stat; + void *pRead_buf; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + tinfl_decompressor inflator; + + if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + /* A directory or zero length file */ + if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) + return MZ_TRUE; + + /* Encryption and patch files are not supported. */ + if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + + /* This function only supports decompressing stored and deflate. */ + if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); + + /* Ensure supplied output buffer is large enough. */ + needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; + if (buf_size < needed_size) + return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL); + + /* Read and parse the local directory entry. */ + cur_file_ofs = file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) + { + /* The file is stored or the caller has requested the compressed data. */ + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0) + { + if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) + return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED); + } +#endif + + return MZ_TRUE; + } + + /* Decompress the file either directly from memory or from a file input buffer. */ + tinfl_init(&inflator); + + if (pZip->m_pState->m_pMem) + { + /* Read directly from the archive in memory. */ + pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; + read_buf_size = read_buf_avail = file_stat.m_comp_size; + comp_remaining = 0; + } + else if (pUser_read_buf) + { + /* Use a user provided read buffer. */ + if (!user_read_buf_size) + return MZ_FALSE; + pRead_buf = (mz_uint8 *)pUser_read_buf; + read_buf_size = user_read_buf_size; + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + else + { + /* Temporarily allocate a read buffer. */ + read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); + if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + + do + { + /* The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size above */ + size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs); + if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + status = TINFL_STATUS_FAILED; + mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); + break; + } + cur_file_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + read_buf_ofs = 0; + } + in_buf_size = (size_t)read_buf_avail; + status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0)); + read_buf_avail -= in_buf_size; + read_buf_ofs += in_buf_size; + out_buf_ofs += out_buf_size; + } while (status == TINFL_STATUS_NEEDS_MORE_INPUT); + + if (status == TINFL_STATUS_DONE) + { + /* Make sure the entire file was decompressed, and check its CRC. */ + if (out_buf_ofs != file_stat.m_uncomp_size) + { + mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); + status = TINFL_STATUS_FAILED; + } +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) + { + mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED); + status = TINFL_STATUS_FAILED; + } +#endif + } + + if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + + return status == TINFL_STATUS_DONE; +} + +mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +{ + mz_uint32 file_index; + if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) + return MZ_FALSE; + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size); +} + +mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags) +{ + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); +} + +mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags) +{ + return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0); +} + +void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags) +{ + mz_uint64 comp_size, uncomp_size, alloc_size; + const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + void *pBuf; + + if (pSize) + *pSize = 0; + + if (!p) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return NULL; + } + + comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + + alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; + if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) + { + mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + return NULL; + } + + if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + return NULL; + } + + if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return NULL; + } + + if (pSize) + *pSize = (size_t)alloc_size; + return pBuf; +} + +void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags) +{ + mz_uint32 file_index; + if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) + { + if (pSize) + *pSize = 0; + return MZ_FALSE; + } + return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags); +} + +mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags) +{ + int status = TINFL_STATUS_DONE; +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + mz_uint file_crc32 = MZ_CRC32_INIT; +#endif + mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs; + mz_zip_archive_file_stat file_stat; + void *pRead_buf = NULL; + void *pWrite_buf = NULL; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + + if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + /* A directory or zero length file */ + if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) + return MZ_TRUE; + + /* Encryption and patch files are not supported. */ + if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + + /* This function only supports decompressing stored and deflate. */ + if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); + + /* Read and do some minimal validation of the local directory entry (this doesn't crack the zip64 stuff, which we already have from the central dir) */ + cur_file_ofs = file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + /* Decompress the file either directly from memory or from a file input buffer. */ + if (pZip->m_pState->m_pMem) + { + pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; + read_buf_size = read_buf_avail = file_stat.m_comp_size; + comp_remaining = 0; + } + else + { + read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); + if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + + if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) + { + /* The file is stored or the caller has requested the compressed data. */ + if (pZip->m_pState->m_pMem) + { + if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) + { + mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); + status = TINFL_STATUS_FAILED; + } + else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + { +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)file_stat.m_comp_size); +#endif + } + + cur_file_ofs += file_stat.m_comp_size; + out_buf_ofs += file_stat.m_comp_size; + comp_remaining = 0; + } + else + { + while (comp_remaining) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + status = TINFL_STATUS_FAILED; + break; + } + +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + { + file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail); + } +#endif + + if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); + status = TINFL_STATUS_FAILED; + break; + } + + cur_file_ofs += read_buf_avail; + out_buf_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + } + } + } + else + { + tinfl_decompressor inflator; + tinfl_init(&inflator); + + if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + status = TINFL_STATUS_FAILED; + } + else + { + do + { + mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + status = TINFL_STATUS_FAILED; + break; + } + cur_file_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + read_buf_ofs = 0; + } + + in_buf_size = (size_t)read_buf_avail; + status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); + read_buf_avail -= in_buf_size; + read_buf_ofs += in_buf_size; + + if (out_buf_size) + { + if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) + { + mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); + status = TINFL_STATUS_FAILED; + break; + } + +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size); +#endif + if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) + { + mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); + status = TINFL_STATUS_FAILED; + break; + } + } + } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT)); + } + } + + if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) + { + /* Make sure the entire file was decompressed, and check its CRC. */ + if (out_buf_ofs != file_stat.m_uncomp_size) + { + mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); + status = TINFL_STATUS_FAILED; + } +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + else if (file_crc32 != file_stat.m_crc32) + { + mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); + status = TINFL_STATUS_FAILED; + } +#endif + } + + if (!pZip->m_pState->m_pMem) + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + + if (pWrite_buf) + pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf); + + return status == TINFL_STATUS_DONE; +} + +mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags) +{ + mz_uint32 file_index; + if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) + return MZ_FALSE; + + return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags); +} + +mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags) +{ + mz_zip_reader_extract_iter_state *pState; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + + /* Argument sanity check */ + if ((!pZip) || (!pZip->m_pState)) + return NULL; + + /* Allocate an iterator status structure */ + pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state)); + if (!pState) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + return NULL; + } + + /* Fetch file details */ + if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + /* Encryption and patch files are not supported. */ + if (pState->file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) + { + mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + /* This function only supports decompressing stored and deflate. */ + if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED)) + { + mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + /* Init state - save args */ + pState->pZip = pZip; + pState->flags = flags; + + /* Init state - reset variables to defaults */ + pState->status = TINFL_STATUS_DONE; +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + pState->file_crc32 = MZ_CRC32_INIT; +#endif + pState->read_buf_ofs = 0; + pState->out_buf_ofs = 0; + pState->pRead_buf = NULL; + pState->pWrite_buf = NULL; + pState->out_blk_remain = 0; + + /* Read and parse the local directory entry. */ + pState->cur_file_ofs = pState->file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + pState->cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + + /* Decompress the file either directly from memory or from a file input buffer. */ + if (pZip->m_pState->m_pMem) + { + pState->pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + pState->cur_file_ofs; + pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size; + pState->comp_remaining = pState->file_stat.m_comp_size; + } + else + { + if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) + { + /* Decompression required, therefore intermediate read buffer required */ + pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); + if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size))) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + } + else + { + /* Decompression not required - we will be reading directly into user buffer, no temp buf required */ + pState->read_buf_size = 0; + } + pState->read_buf_avail = 0; + pState->comp_remaining = pState->file_stat.m_comp_size; + } + + if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) + { + /* Decompression required, init decompressor */ + tinfl_init( &pState->inflator ); + + /* Allocate write buffer */ + if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + if (pState->pRead_buf) + pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf); + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + return NULL; + } + } + + return pState; +} + +mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags) +{ + mz_uint32 file_index; + + /* Locate file index by name */ + if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) + return NULL; + + /* Construct iterator */ + return mz_zip_reader_extract_iter_new(pZip, file_index, flags); +} + +size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size) +{ + size_t copied_to_caller = 0; + + /* Argument sanity check */ + if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) + return 0; + + if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) + { + /* The file is stored or the caller has requested the compressed data, calc amount to return. */ + copied_to_caller = (size_t)MZ_MIN( buf_size, pState->comp_remaining ); + + /* Zip is in memory....or requires reading from a file? */ + if (pState->pZip->m_pState->m_pMem) + { + /* Copy data to caller's buffer */ + memcpy( pvBuf, pState->pRead_buf, copied_to_caller ); + pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller; + } + else + { + /* Read directly into caller's buffer */ + if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller) + { + /* Failed to read all that was asked for, flag failure and alert user */ + mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); + pState->status = TINFL_STATUS_FAILED; + copied_to_caller = 0; + } + } + +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + /* Compute CRC if not returning compressed data only */ + if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8 *)pvBuf, copied_to_caller); +#endif + + /* Advance offsets, dec counters */ + pState->cur_file_ofs += copied_to_caller; + pState->out_buf_ofs += copied_to_caller; + pState->comp_remaining -= copied_to_caller; + } + else + { + do + { + /* Calc ptr to write buffer - given current output pos and block size */ + mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + + /* Calc max output size - given current output pos and block size */ + size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + + if (!pState->out_blk_remain) + { + /* Read more data from file if none available (and reading from file) */ + if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) + { + /* Calc read size */ + pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining); + if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, (size_t)pState->read_buf_avail) != pState->read_buf_avail) + { + mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); + pState->status = TINFL_STATUS_FAILED; + break; + } + + /* Advance offsets, dec counters */ + pState->cur_file_ofs += pState->read_buf_avail; + pState->comp_remaining -= pState->read_buf_avail; + pState->read_buf_ofs = 0; + } + + /* Perform decompression */ + in_buf_size = (size_t)pState->read_buf_avail; + pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs, &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); + pState->read_buf_avail -= in_buf_size; + pState->read_buf_ofs += in_buf_size; + + /* Update current output block size remaining */ + pState->out_blk_remain = out_buf_size; + } + + if (pState->out_blk_remain) + { + /* Calc amount to return. */ + size_t to_copy = MZ_MIN( (buf_size - copied_to_caller), pState->out_blk_remain ); + + /* Copy data to caller's buffer */ + memcpy( (uint8_t*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy ); + +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + /* Perform CRC */ + pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy); +#endif + + /* Decrement data consumed from block */ + pState->out_blk_remain -= to_copy; + + /* Inc output offset, while performing sanity check */ + if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size) + { + mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); + pState->status = TINFL_STATUS_FAILED; + break; + } + + /* Increment counter of data copied to caller */ + copied_to_caller += to_copy; + } + } while ( (copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)) ); + } + + /* Return how many bytes were copied into user buffer */ + return copied_to_caller; +} + +mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState) +{ + int status; + + /* Argument sanity check */ + if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState)) + return MZ_FALSE; + + /* Was decompression completed and requested? */ + if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) + { + /* Make sure the entire file was decompressed, and check its CRC. */ + if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) + { + mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); + pState->status = TINFL_STATUS_FAILED; + } +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + else if (pState->file_crc32 != pState->file_stat.m_crc32) + { + mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); + pState->status = TINFL_STATUS_FAILED; + } +#endif + } + + /* Free buffers */ + if (!pState->pZip->m_pState->m_pMem) + pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf); + if (pState->pWrite_buf) + pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf); + + /* Save status */ + status = pState->status; + + /* Free context */ + pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState); + + return status == TINFL_STATUS_DONE; +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n) +{ + (void)ofs; + + return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque); +} + +mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags) +{ + mz_bool status; + mz_zip_archive_file_stat file_stat; + MZ_FILE *pFile; + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + if ((file_stat.m_is_directory) || (!file_stat.m_is_supported)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); + + pFile = MZ_FOPEN(pDst_filename, "wb"); + if (!pFile) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + + status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags); + + if (MZ_FCLOSE(pFile) == EOF) + { + if (status) + mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); + + status = MZ_FALSE; + } + +#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO) + if (status) + mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time); +#endif + + return status; +} + +mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags) +{ + mz_uint32 file_index; + if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index)) + return MZ_FALSE; + + return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags); +} + +mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags) +{ + mz_zip_archive_file_stat file_stat; + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + if ((file_stat.m_is_directory) || (!file_stat.m_is_supported)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); + + return mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags); +} + +mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags) +{ + mz_uint32 file_index; + if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index)) + return MZ_FALSE; + + return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags); +} +#endif /* #ifndef MINIZ_NO_STDIO */ + +static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n) +{ + mz_uint32 *p = (mz_uint32 *)pOpaque; + (void)file_ofs; + *p = (mz_uint32)mz_crc32(*p, (const mz_uint8 *)pBuf, n); + return n; +} + +mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags) +{ + mz_zip_archive_file_stat file_stat; + mz_zip_internal_state *pState; + const mz_uint8 *pCentral_dir_header; + mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE; + mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + mz_uint64 local_header_ofs = 0; + mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32; + mz_uint64 local_header_comp_size, local_header_uncomp_size; + mz_uint32 uncomp_crc32 = MZ_CRC32_INIT; + mz_bool has_data_descriptor; + mz_uint32 local_header_bit_flags; + + mz_zip_array file_data_array; + mz_zip_array_init(&file_data_array, 1); + + if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (file_index > pZip->m_total_files) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + pCentral_dir_header = mz_zip_get_cdh(pZip, file_index); + + if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir)) + return MZ_FALSE; + + /* A directory or zero length file */ + if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size)) + return MZ_TRUE; + + /* Encryption and patch files are not supported. */ + if (file_stat.m_is_encrypted) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); + + /* This function only supports stored and deflate. */ + if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); + + if (!file_stat.m_is_supported) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); + + /* Read and parse the local directory entry. */ + local_header_ofs = file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS); + local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS); + local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS); + local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS); + local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); + has_data_descriptor = (local_header_bit_flags & 8) != 0; + + if (local_header_filename_len != strlen(file_stat.m_filename)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size) > pZip->m_archive_size) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + goto handle_failure; + } + + if (local_header_filename_len) + { + if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, local_header_filename_len) != local_header_filename_len) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + goto handle_failure; + } + + /* I've seen 1 archive that had the same pathname, but used backslashes in the local dir and forward slashes in the central dir. Do we care about this? For now, this case will fail validation. */ + if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0) + { + mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); + goto handle_failure; + } + } + + if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) + { + mz_uint32 extra_size_remaining = local_header_extra_len; + const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p; + + if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, file_data_array.m_p, local_header_extra_len) != local_header_extra_len) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + goto handle_failure; + } + + do + { + mz_uint32 field_id, field_data_size, field_total_size; + + if (extra_size_remaining < (sizeof(mz_uint16) * 2)) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + goto handle_failure; + } + + field_id = MZ_READ_LE16(pExtra_data); + field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); + field_total_size = field_data_size + sizeof(mz_uint16) * 2; + + if (field_total_size > extra_size_remaining) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + goto handle_failure; + } + + if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) + { + const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32); + + if (field_data_size < sizeof(mz_uint64) * 2) + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + goto handle_failure; + } + + local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data); + local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); + + found_zip64_ext_data_in_ldir = MZ_TRUE; + break; + } + + pExtra_data += field_total_size; + extra_size_remaining -= field_total_size; + } while (extra_size_remaining); + } + + /* TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip) */ + /* I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data descriptors */ + if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) + { + mz_uint8 descriptor_buf[32]; + mz_bool has_id; + const mz_uint8 *pSrc; + mz_uint32 file_crc32; + mz_uint64 comp_size = 0, uncomp_size = 0; + + mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4; + + if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size, descriptor_buf, sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s)) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + goto handle_failure; + } + + has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID); + pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf; + + file_crc32 = MZ_READ_LE32(pSrc); + + if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) + { + comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32)); + uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64)); + } + else + { + comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32)); + uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32)); + } + + if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) + { + mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); + goto handle_failure; + } + } + else + { + if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size)) + { + mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); + goto handle_failure; + } + } + + mz_zip_array_clear(pZip, &file_data_array); + + if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) + { + if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0)) + return MZ_FALSE; + + /* 1 more check to be sure, although the extract checks too. */ + if (uncomp_crc32 != file_stat.m_crc32) + { + mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); + return MZ_FALSE; + } + } + + return MZ_TRUE; + +handle_failure: + mz_zip_array_clear(pZip, &file_data_array); + return MZ_FALSE; +} + +mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags) +{ + mz_zip_internal_state *pState; + uint32_t i; + + if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + /* Basic sanity checks */ + if (!pState->m_zip64) + { + if (pZip->m_total_files > MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + if (pZip->m_archive_size > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + } + else + { + if (pZip->m_total_files >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + } + + for (i = 0; i < pZip->m_total_files; i++) + { + if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags) + { + mz_uint32 found_index; + mz_zip_archive_file_stat stat; + + if (!mz_zip_reader_file_stat(pZip, i, &stat)) + return MZ_FALSE; + + if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index)) + return MZ_FALSE; + + /* This check can fail if there are duplicate filenames in the archive (which we don't check for when writing - that's up to the user) */ + if (found_index != i) + return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); + } + + if (!mz_zip_validate_file(pZip, i, flags)) + return MZ_FALSE; + } + + return MZ_TRUE; +} + +mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr) +{ + mz_bool success = MZ_TRUE; + mz_zip_archive zip; + mz_zip_error actual_err = MZ_ZIP_NO_ERROR; + + if ((!pMem) || (!size)) + { + if (pErr) + *pErr = MZ_ZIP_INVALID_PARAMETER; + return MZ_FALSE; + } + + mz_zip_zero_struct(&zip); + + if (!mz_zip_reader_init_mem(&zip, pMem, size, flags)) + { + if (pErr) + *pErr = zip.m_last_error; + return MZ_FALSE; + } + + if (!mz_zip_validate_archive(&zip, flags)) + { + actual_err = zip.m_last_error; + success = MZ_FALSE; + } + + if (!mz_zip_reader_end_internal(&zip, success)) + { + if (!actual_err) + actual_err = zip.m_last_error; + success = MZ_FALSE; + } + + if (pErr) + *pErr = actual_err; + + return success; +} + +#ifndef MINIZ_NO_STDIO +mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr) +{ + mz_bool success = MZ_TRUE; + mz_zip_archive zip; + mz_zip_error actual_err = MZ_ZIP_NO_ERROR; + + if (!pFilename) + { + if (pErr) + *pErr = MZ_ZIP_INVALID_PARAMETER; + return MZ_FALSE; + } + + mz_zip_zero_struct(&zip); + + if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0)) + { + if (pErr) + *pErr = zip.m_last_error; + return MZ_FALSE; + } + + if (!mz_zip_validate_archive(&zip, flags)) + { + actual_err = zip.m_last_error; + success = MZ_FALSE; + } + + if (!mz_zip_reader_end_internal(&zip, success)) + { + if (!actual_err) + actual_err = zip.m_last_error; + success = MZ_FALSE; + } + + if (pErr) + *pErr = actual_err; + + return success; +} +#endif /* #ifndef MINIZ_NO_STDIO */ + +/* ------------------- .ZIP archive writing */ + +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v) +{ + p[0] = (mz_uint8)v; + p[1] = (mz_uint8)(v >> 8); +} +static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v) +{ + p[0] = (mz_uint8)v; + p[1] = (mz_uint8)(v >> 8); + p[2] = (mz_uint8)(v >> 16); + p[3] = (mz_uint8)(v >> 24); +} +static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v) +{ + mz_write_le32(p, (mz_uint32)v); + mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32)); +} + +#define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v)) +#define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v)) +#define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v)) + +static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_zip_internal_state *pState = pZip->m_pState; + mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size); + + if (!n) + return 0; + + /* An allocation this big is likely to just fail on 32-bit systems, so don't even go there. */ + if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); + return 0; + } + + if (new_size > pState->m_mem_capacity) + { + void *pNew_block; + size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity); + + while (new_capacity < new_size) + new_capacity *= 2; + + if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + return 0; + } + + pState->m_pMem = pNew_block; + pState->m_mem_capacity = new_capacity; + } + memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n); + pState->m_mem_size = (size_t)new_size; + return n; +} + +static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error) +{ + mz_zip_internal_state *pState; + mz_bool status = MZ_TRUE; + + if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) + { + if (set_last_error) + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return MZ_FALSE; + } + + pState = pZip->m_pState; + pZip->m_pState = NULL; + mz_zip_array_clear(pZip, &pState->m_central_dir); + mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); + mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); + +#ifndef MINIZ_NO_STDIO + if (pState->m_pFile) + { + if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) + { + if (MZ_FCLOSE(pState->m_pFile) == EOF) + { + if (set_last_error) + mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); + status = MZ_FALSE; + } + } + + pState->m_pFile = NULL; + } +#endif /* #ifndef MINIZ_NO_STDIO */ + + if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem); + pState->m_pMem = NULL; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; + return status; +} + +mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags) +{ + mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0; + + if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) + { + if (!pZip->m_pRead) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + } + + if (pZip->m_file_offset_alignment) + { + /* Ensure user specified file offset alignment is a power of 2. */ + if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + } + + if (!pZip->m_pAlloc) + pZip->m_pAlloc = miniz_def_alloc_func; + if (!pZip->m_pFree) + pZip->m_pFree = miniz_def_free_func; + if (!pZip->m_pRealloc) + pZip->m_pRealloc = miniz_def_realloc_func; + + pZip->m_archive_size = existing_size; + pZip->m_central_directory_file_ofs = 0; + pZip->m_total_files = 0; + + if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); + + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); + + pZip->m_pState->m_zip64 = zip64; + pZip->m_pState->m_zip64_has_extended_info_fields = zip64; + + pZip->m_zip_type = MZ_ZIP_TYPE_USER; + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size) +{ + return mz_zip_writer_init_v2(pZip, existing_size, 0); +} + +mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags) +{ + pZip->m_pWrite = mz_zip_heap_write_func; + pZip->m_pNeeds_keepalive = NULL; + + if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) + pZip->m_pRead = mz_zip_mem_read_func; + + pZip->m_pIO_opaque = pZip; + + if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags)) + return MZ_FALSE; + + pZip->m_zip_type = MZ_ZIP_TYPE_HEAP; + + if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) + { + if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size))) + { + mz_zip_writer_end_internal(pZip, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + pZip->m_pState->m_mem_capacity = initial_allocation_size; + } + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size) +{ + return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning, initial_allocation_size, 0); +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); + + file_ofs += pZip->m_pState->m_file_archive_start_ofs; + + if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); + return 0; + } + + return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile); +} + +mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning) +{ + return mz_zip_writer_init_file_v2(pZip, pFilename, size_to_reserve_at_beginning, 0); +} + +mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags) +{ + MZ_FILE *pFile; + + pZip->m_pWrite = mz_zip_file_write_func; + pZip->m_pNeeds_keepalive = NULL; + + if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) + pZip->m_pRead = mz_zip_file_read_func; + + pZip->m_pIO_opaque = pZip; + + if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags)) + return MZ_FALSE; + + if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb"))) + { + mz_zip_writer_end(pZip); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + } + + pZip->m_pState->m_pFile = pFile; + pZip->m_zip_type = MZ_ZIP_TYPE_FILE; + + if (size_to_reserve_at_beginning) + { + mz_uint64 cur_ofs = 0; + char buf[4096]; + + MZ_CLEAR_OBJ(buf); + + do + { + size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) + { + mz_zip_writer_end(pZip); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + cur_ofs += n; + size_to_reserve_at_beginning -= n; + } while (size_to_reserve_at_beginning); + } + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags) +{ + pZip->m_pWrite = mz_zip_file_write_func; + pZip->m_pNeeds_keepalive = NULL; + + if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) + pZip->m_pRead = mz_zip_file_read_func; + + pZip->m_pIO_opaque = pZip; + + if (!mz_zip_writer_init_v2(pZip, 0, flags)) + return MZ_FALSE; + + pZip->m_pState->m_pFile = pFile; + pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); + pZip->m_zip_type = MZ_ZIP_TYPE_CFILE; + + return MZ_TRUE; +} +#endif /* #ifndef MINIZ_NO_STDIO */ + +mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags) +{ + mz_zip_internal_state *pState; + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) + { + /* We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about the existing 32-bit data descriptors that could follow the compressed data?) */ + if (!pZip->m_pState->m_zip64) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + } + + /* No sense in trying to write to an archive that's already at the support max size */ + if (pZip->m_pState->m_zip64) + { + if (pZip->m_total_files == MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + else + { + if (pZip->m_total_files == MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + + if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); + } + + pState = pZip->m_pState; + + if (pState->m_pFile) + { +#ifdef MINIZ_NO_STDIO + (void)pFilename; + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); +#else + if (pZip->m_pIO_opaque != pZip) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) + { + if (!pFilename) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */ + if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) + { + /* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */ + mz_zip_reader_end_internal(pZip, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + } + } + + pZip->m_pWrite = mz_zip_file_write_func; + pZip->m_pNeeds_keepalive = NULL; +#endif /* #ifdef MINIZ_NO_STDIO */ + } + else if (pState->m_pMem) + { + /* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */ + if (pZip->m_pIO_opaque != pZip) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState->m_mem_capacity = pState->m_mem_size; + pZip->m_pWrite = mz_zip_heap_write_func; + pZip->m_pNeeds_keepalive = NULL; + } + /* Archive is being read via a user provided read function - make sure the user has specified a write function too. */ + else if (!pZip->m_pWrite) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + /* Start writing new files at the archive's current central directory location. */ + /* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer. */ + pZip->m_archive_size = pZip->m_central_directory_file_ofs; + pZip->m_central_directory_file_ofs = 0; + + /* Clear the sorted central dir offsets, they aren't useful or maintained now. */ + /* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */ + /* TODO: We could easily maintain the sorted central directory offsets. */ + mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets); + + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename) +{ + return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0); +} + +/* TODO: pArchive_name is a terrible name here! */ +mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags) +{ + return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0); +} + +typedef struct +{ + mz_zip_archive *m_pZip; + mz_uint64 m_cur_archive_file_ofs; + mz_uint64 m_comp_size; +} mz_zip_writer_add_state; + +static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser) +{ + mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser; + if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len) + return MZ_FALSE; + + pState->m_cur_archive_file_ofs += len; + pState->m_comp_size += len; + return MZ_TRUE; +} + +#define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2) +#define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3) +static mz_uint32 mz_zip_writer_create_zip64_extra_data(mz_uint8 *pBuf, mz_uint64 *pUncomp_size, mz_uint64 *pComp_size, mz_uint64 *pLocal_header_ofs) +{ + mz_uint8 *pDst = pBuf; + mz_uint32 field_size = 0; + + MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID); + MZ_WRITE_LE16(pDst + 2, 0); + pDst += sizeof(mz_uint16) * 2; + + if (pUncomp_size) + { + MZ_WRITE_LE64(pDst, *pUncomp_size); + pDst += sizeof(mz_uint64); + field_size += sizeof(mz_uint64); + } + + if (pComp_size) + { + MZ_WRITE_LE64(pDst, *pComp_size); + pDst += sizeof(mz_uint64); + field_size += sizeof(mz_uint64); + } + + if (pLocal_header_ofs) + { + MZ_WRITE_LE64(pDst, *pLocal_header_ofs); + pDst += sizeof(mz_uint64); + field_size += sizeof(mz_uint64); + } + + MZ_WRITE_LE16(pBuf + 2, field_size); + + return (mz_uint32)(pDst - pBuf); +} + +static mz_bool mz_zip_writer_create_local_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date) +{ + (void)pZip; + memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size); + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, + mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size, + mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, + mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, + mz_uint64 local_header_ofs, mz_uint32 ext_attributes) +{ + (void)pZip; + memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX)); + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size, + const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_size, + mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, + mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, + mz_uint64 local_header_ofs, mz_uint32 ext_attributes, + const char *user_extra_data, mz_uint user_extra_data_len) +{ + mz_zip_internal_state *pState = pZip->m_pState; + mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size; + size_t orig_central_dir_size = pState->m_central_dir.m_size; + mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; + + if (!pZip->m_pState->m_zip64) + { + if (local_header_ofs > 0xFFFFFFFF) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); + } + + /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ + if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + user_extra_data_len + comment_size) >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + + if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, (mz_uint16)(extra_size + user_extra_data_len), comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, ¢ral_dir_ofs, 1))) + { + /* Try to resize the central directory array back into its original state. */ + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name) +{ + /* Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a drive letter, and cannot use DOS-style backward slashes. */ + if (*pArchive_name == '/') + return MZ_FALSE; + + /* Making sure the name does not contain drive letters or DOS style backward slashes is the responsibility of the program using miniz*/ + + return MZ_TRUE; +} + +static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip) +{ + mz_uint32 n; + if (!pZip->m_file_offset_alignment) + return 0; + n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1)); + return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1)); +} + +static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n) +{ + char buf[4096]; + memset(buf, 0, MZ_MIN(sizeof(buf), n)); + while (n) + { + mz_uint32 s = MZ_MIN(sizeof(buf), n); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_file_ofs += s; + n -= s; + } + return MZ_TRUE; +} + +mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, + mz_uint64 uncomp_size, mz_uint32 uncomp_crc32) +{ + return mz_zip_writer_add_mem_ex_v2(pZip, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, uncomp_size, uncomp_crc32, NULL, NULL, 0, NULL, 0); +} + +mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, + mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T *last_modified, + const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) +{ + mz_uint16 method = 0, dos_time = 0, dos_date = 0; + mz_uint level, ext_attributes = 0, num_alignment_padding_bytes; + mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0; + size_t archive_name_size; + mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; + tdefl_compressor *pComp = NULL; + mz_bool store_data_uncompressed; + mz_zip_internal_state *pState; + mz_uint8 *pExtra_data = NULL; + mz_uint32 extra_size = 0; + mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE]; + mz_uint16 bit_flags = 0; + + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + + if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) + bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + + if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) + bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; + + level = level_and_flags & 0xF; + store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + if (pState->m_zip64) + { + if (pZip->m_total_files == MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + else + { + if (pZip->m_total_files == MZ_UINT16_MAX) + { + pState->m_zip64 = MZ_TRUE; + /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ + } + if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) + { + pState->m_zip64 = MZ_TRUE; + /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ + } + } + + if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); + +#ifndef MINIZ_NO_TIME + if (last_modified != NULL) + { + mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date); + } + else + { + MZ_TIME_T cur_time; + time(&cur_time); + mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date); + } +#endif /* #ifndef MINIZ_NO_TIME */ + + if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + { + uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size); + uncomp_size = buf_size; + if (uncomp_size <= 3) + { + level = 0; + store_data_uncompressed = MZ_TRUE; + } + } + + archive_name_size = strlen(pArchive_name); + if (archive_name_size > MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ + if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + + if (!pState->m_zip64) + { + /* Bail early if the archive would obviously become too large */ + if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len + + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len + + MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF) + { + pState->m_zip64 = MZ_TRUE; + /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ + } + } + + if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) + { + /* Set DOS Subdirectory attribute bit. */ + ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG; + + /* Subdirectories cannot contain data. */ + if ((buf_size) || (uncomp_size)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + } + + /* Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A good idea if we're doing an in-place modification.) */ + if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + if ((!store_data_uncompressed) && (buf_size)) + { + if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return MZ_FALSE; + } + + local_dir_header_ofs += num_alignment_padding_bytes; + if (pZip->m_file_offset_alignment) + { + MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); + } + cur_archive_file_ofs += num_alignment_padding_bytes; + + MZ_CLEAR_OBJ(local_dir_header); + + if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + { + method = MZ_DEFLATED; + } + + if (pState->m_zip64) + { + if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) + { + pExtra_data = extra_data; + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, bit_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + cur_archive_file_ofs += archive_name_size; + + if (pExtra_data != NULL) + { + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += extra_size; + } + } + else + { + if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + cur_archive_file_ofs += archive_name_size; + } + + if (user_extra_data_len > 0) + { + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += user_extra_data_len; + } + + if (store_data_uncompressed) + { + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_file_ofs += buf_size; + comp_size = buf_size; + } + else if (buf_size) + { + mz_zip_writer_add_state state; + + state.m_pZip = pZip; + state.m_cur_archive_file_ofs = cur_archive_file_ofs; + state.m_comp_size = 0; + + if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) || + (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED); + } + + comp_size = state.m_comp_size; + cur_archive_file_ofs = state.m_cur_archive_file_ofs; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + pComp = NULL; + + if (uncomp_size) + { + mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64]; + mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32; + + MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR); + + MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID); + MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32); + if (pExtra_data == NULL) + { + if (comp_size > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + MZ_WRITE_LE32(local_dir_footer + 8, comp_size); + MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size); + } + else + { + MZ_WRITE_LE64(local_dir_footer + 8, comp_size); + MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size); + local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64; + } + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size) + return MZ_FALSE; + + cur_archive_file_ofs += local_dir_footer_size; + } + + if (pExtra_data != NULL) + { + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, + comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes, + user_extra_data_central, user_extra_data_central_len)) + return MZ_FALSE; + + pZip->m_total_files++; + pZip->m_archive_size = cur_archive_file_ofs; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, + const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) +{ + mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; + mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; + mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; + size_t archive_name_size; + mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; + mz_uint8 *pExtra_data = NULL; + mz_uint32 extra_size = 0; + mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE]; + mz_zip_internal_state *pState; + mz_uint64 file_ofs = 0, cur_archive_header_file_ofs; + + if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) + gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; + + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + level = level_and_flags & 0xF; + + /* Sanity checks */ + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) + { + /* Source file is too large for non-zip64 */ + /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ + pState->m_zip64 = MZ_TRUE; + } + + /* We could support this, but why? */ + if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); + + if (pState->m_zip64) + { + if (pZip->m_total_files == MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + else + { + if (pZip->m_total_files == MZ_UINT16_MAX) + { + pState->m_zip64 = MZ_TRUE; + /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ + } + } + + archive_name_size = strlen(pArchive_name); + if (archive_name_size > MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ + if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + + if (!pState->m_zip64) + { + /* Bail early if the archive would obviously become too large */ + if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + + archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024 + + MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) > 0xFFFFFFFF) + { + pState->m_zip64 = MZ_TRUE; + /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ + } + } + +#ifndef MINIZ_NO_TIME + if (pFile_time) + { + mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date); + } +#endif + + if (max_size <= 3) + level = 0; + + if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) + { + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_file_ofs += num_alignment_padding_bytes; + local_dir_header_ofs = cur_archive_file_ofs; + + if (pZip->m_file_offset_alignment) + { + MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0); + } + + if (max_size && level) + { + method = MZ_DEFLATED; + } + + MZ_CLEAR_OBJ(local_dir_header); + if (pState->m_zip64) + { + if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) + { + pExtra_data = extra_data; + if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, + (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + else + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, NULL, + NULL, + (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_file_ofs += archive_name_size; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += extra_size; + } + else + { + if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_file_ofs += archive_name_size; + } + + if (user_extra_data_len > 0) + { + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_file_ofs += user_extra_data_len; + } + + if (max_size) + { + void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE); + if (!pRead_buf) + { + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (!level) + { + while (1) + { + size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); + if (n == 0) + break; + + if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + file_ofs += n; + uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); + cur_archive_file_ofs += n; + } + uncomp_size = file_ofs; + comp_size = uncomp_size; + } + else + { + mz_bool result = MZ_FALSE; + mz_zip_writer_add_state state; + tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)); + if (!pComp) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + state.m_pZip = pZip; + state.m_cur_archive_file_ofs = cur_archive_file_ofs; + state.m_comp_size = 0; + + if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + } + + for (;;) + { + tdefl_status status; + tdefl_flush flush = TDEFL_NO_FLUSH; + + size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); + if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) + { + mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + break; + } + + file_ofs += n; + uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); + + if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque)) + flush = TDEFL_FULL_FLUSH; + + if (n == 0) + flush = TDEFL_FINISH; + + status = tdefl_compress_buffer(pComp, pRead_buf, n, flush); + if (status == TDEFL_STATUS_DONE) + { + result = MZ_TRUE; + break; + } + else if (status != TDEFL_STATUS_OKAY) + { + mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED); + break; + } + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + + if (!result) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return MZ_FALSE; + } + + uncomp_size = file_ofs; + comp_size = state.m_comp_size; + cur_archive_file_ofs = state.m_cur_archive_file_ofs; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + } + + if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) + { + mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64]; + mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32; + + MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID); + MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32); + if (pExtra_data == NULL) + { + if (comp_size > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + MZ_WRITE_LE32(local_dir_footer + 8, comp_size); + MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size); + } + else + { + MZ_WRITE_LE64(local_dir_footer + 8, comp_size); + MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size); + local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64; + } + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size) + return MZ_FALSE; + + cur_archive_file_ofs += local_dir_footer_size; + } + + if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) + { + if (pExtra_data != NULL) + { + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, + (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), + (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size, + (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size, + uncomp_crc32, method, gen_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + cur_archive_header_file_ofs = local_dir_header_ofs; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + if (pExtra_data != NULL) + { + cur_archive_header_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_header_file_ofs += archive_name_size; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_header_file_ofs += extra_size; + } + } + + if (pExtra_data != NULL) + { + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, comment_size, + uncomp_size, comp_size, uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes, + user_extra_data_central, user_extra_data_central_len)) + return MZ_FALSE; + + pZip->m_total_files++; + pZip->m_archive_size = cur_archive_file_ofs; + + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO + +static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + MZ_FILE *pSrc_file = (MZ_FILE *)pOpaque; + mz_int64 cur_ofs = MZ_FTELL64(pSrc_file); + + if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET)))) + return 0; + + return MZ_FREAD(pBuf, 1, n, pSrc_file); +} + +mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, + const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) +{ + return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size, pFile_time, pComment, comment_size, level_and_flags, + user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len); +} + +mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags) +{ + MZ_FILE *pSrc_file = NULL; + mz_uint64 uncomp_size = 0; + MZ_TIME_T file_modified_time; + MZ_TIME_T *pFile_time = NULL; + mz_bool status; + + memset(&file_modified_time, 0, sizeof(file_modified_time)); + +#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO) + pFile_time = &file_modified_time; + if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED); +#endif + + pSrc_file = MZ_FOPEN(pSrc_filename, "rb"); + if (!pSrc_file) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); + + MZ_FSEEK64(pSrc_file, 0, SEEK_END); + uncomp_size = MZ_FTELL64(pSrc_file); + MZ_FSEEK64(pSrc_file, 0, SEEK_SET); + + status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size, pFile_time, pComment, comment_size, level_and_flags, NULL, 0, NULL, 0); + + MZ_FCLOSE(pSrc_file); + + return status; +} +#endif /* #ifndef MINIZ_NO_STDIO */ + +static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array *pNew_ext, mz_zip_archive *pZip, const mz_uint8 *pExt, uint32_t ext_len, mz_uint64 *pComp_size, mz_uint64 *pUncomp_size, mz_uint64 *pLocal_header_ofs, mz_uint32 *pDisk_start) +{ + /* + 64 should be enough for any new zip64 data */ + if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE)) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE); + + if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) + { + mz_uint8 new_ext_block[64]; + mz_uint8 *pDst = new_ext_block; + mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID); + mz_write_le16(pDst + sizeof(mz_uint16), 0); + pDst += sizeof(mz_uint16) * 2; + + if (pUncomp_size) + { + mz_write_le64(pDst, *pUncomp_size); + pDst += sizeof(mz_uint64); + } + + if (pComp_size) + { + mz_write_le64(pDst, *pComp_size); + pDst += sizeof(mz_uint64); + } + + if (pLocal_header_ofs) + { + mz_write_le64(pDst, *pLocal_header_ofs); + pDst += sizeof(mz_uint64); + } + + if (pDisk_start) + { + mz_write_le32(pDst, *pDisk_start); + pDst += sizeof(mz_uint32); + } + + mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2)); + + if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block)) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if ((pExt) && (ext_len)) + { + mz_uint32 extra_size_remaining = ext_len; + const mz_uint8 *pExtra_data = pExt; + + do + { + mz_uint32 field_id, field_data_size, field_total_size; + + if (extra_size_remaining < (sizeof(mz_uint16) * 2)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + field_id = MZ_READ_LE16(pExtra_data); + field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); + field_total_size = field_data_size + sizeof(mz_uint16) * 2; + + if (field_total_size > extra_size_remaining) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) + { + if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size)) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + pExtra_data += field_total_size; + extra_size_remaining -= field_total_size; + } while (extra_size_remaining); + } + + return MZ_TRUE; +} + +/* TODO: This func is now pretty freakin complex due to zip64, split it up? */ +mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index) +{ + mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size; + mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs; + mz_uint64 cur_src_file_ofs, cur_dst_file_ofs; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; + mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; + size_t orig_central_dir_size; + mz_zip_internal_state *pState; + void *pBuf; + const mz_uint8 *pSrc_central_header; + mz_zip_archive_file_stat src_file_stat; + mz_uint32 src_filename_len, src_comment_len, src_ext_len; + mz_uint32 local_header_filename_size, local_header_extra_len; + mz_uint64 local_header_comp_size, local_header_uncomp_size; + mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE; + + /* Sanity checks */ + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + /* Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible */ + if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + /* Get pointer to the source central dir header and crack it */ + if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index))) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS); + src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS); + src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS); + src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len; + + /* TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32 fudge factor in case we need to add more extra data) */ + if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + if (!pState->m_zip64) + { + if (pZip->m_total_files == MZ_UINT16_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + else + { + /* TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing. */ + if (pZip->m_total_files == MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + + if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL)) + return MZ_FALSE; + + cur_src_file_ofs = src_file_stat.m_local_header_ofs; + cur_dst_file_ofs = pZip->m_archive_size; + + /* Read the source archive's local dir header */ + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + + cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; + + /* Compute the total size we need to copy (filename+extra data+compressed data) */ + local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS); + local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS); + local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS); + src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.m_comp_size; + + /* Try to find a zip64 extended information field */ + if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) + { + mz_zip_array file_data_array; + const mz_uint8 *pExtra_data; + mz_uint32 extra_size_remaining = local_header_extra_len; + + mz_zip_array_init(&file_data_array, 1); + if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE)) + { + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, file_data_array.m_p, local_header_extra_len) != local_header_extra_len) + { + mz_zip_array_clear(pZip, &file_data_array); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + + pExtra_data = (const mz_uint8 *)file_data_array.m_p; + + do + { + mz_uint32 field_id, field_data_size, field_total_size; + + if (extra_size_remaining < (sizeof(mz_uint16) * 2)) + { + mz_zip_array_clear(pZip, &file_data_array); + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + field_id = MZ_READ_LE16(pExtra_data); + field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); + field_total_size = field_data_size + sizeof(mz_uint16) * 2; + + if (field_total_size > extra_size_remaining) + { + mz_zip_array_clear(pZip, &file_data_array); + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) + { + const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32); + + if (field_data_size < sizeof(mz_uint64) * 2) + { + mz_zip_array_clear(pZip, &file_data_array); + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + } + + local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data); + local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); /* may be 0 if there's a descriptor */ + + found_zip64_ext_data_in_ldir = MZ_TRUE; + break; + } + + pExtra_data += field_total_size; + extra_size_remaining -= field_total_size; + } while (extra_size_remaining); + + mz_zip_array_clear(pZip, &file_data_array); + } + + if (!pState->m_zip64) + { + /* Try to detect if the new archive will most likely wind up too big and bail early (+(sizeof(mz_uint32) * 4) is for the optional descriptor which could be present, +64 is a fudge factor). */ + /* We also check when the archive is finalized so this doesn't need to be perfect. */ + mz_uint64 approx_new_archive_size = cur_dst_file_ofs + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining + (sizeof(mz_uint32) * 4) + + pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64; + + if (approx_new_archive_size >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + } + + /* Write dest archive padding */ + if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes)) + return MZ_FALSE; + + cur_dst_file_ofs += num_alignment_padding_bytes; + + local_dir_header_ofs = cur_dst_file_ofs; + if (pZip->m_file_offset_alignment) + { + MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); + } + + /* The original zip's local header+ext block doesn't change, even with zip64, so we can just copy it over to the dest zip */ + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; + + /* Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional data descriptor */ + if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining))))) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + while (src_archive_bytes_remaining) + { + n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining); + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + cur_src_file_ofs += n; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + cur_dst_file_ofs += n; + + src_archive_bytes_remaining -= n; + } + + /* Now deal with the optional data descriptor */ + bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); + if (bit_flags & 8) + { + /* Copy data descriptor */ + if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) + { + /* src is zip64, dest must be zip64 */ + + /* name uint32_t's */ + /* id 1 (optional in zip64?) */ + /* crc 1 */ + /* comp_size 2 */ + /* uncomp_size 2 */ + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + + n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5); + } + else + { + /* src is NOT zip64 */ + mz_bool has_id; + + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); + } + + has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID); + + if (pZip->m_pState->m_zip64) + { + /* dest is zip64, so upgrade the data descriptor */ + const mz_uint32 *pSrc_descriptor = (const mz_uint32 *)((const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0)); + const mz_uint32 src_crc32 = pSrc_descriptor[0]; + const mz_uint64 src_comp_size = pSrc_descriptor[1]; + const mz_uint64 src_uncomp_size = pSrc_descriptor[2]; + + mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID); + mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32); + mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 2, src_comp_size); + mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size); + + n = sizeof(mz_uint32) * 6; + } + else + { + /* dest is NOT zip64, just copy it as-is */ + n = sizeof(mz_uint32) * (has_id ? 4 : 3); + } + } + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_src_file_ofs += n; + cur_dst_file_ofs += n; + } + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + + /* Finally, add the new central dir header */ + orig_central_dir_size = pState->m_central_dir.m_size; + + memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); + + if (pState->m_zip64) + { + /* This is the painful part: We need to write a new central dir header + ext block with updated zip64 fields, and ensure the old fields (if any) are not included. */ + const mz_uint8 *pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len; + mz_zip_array new_ext_block; + + mz_zip_array_init(&new_ext_block, sizeof(mz_uint8)); + + MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX); + MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX); + MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX); + + if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, &local_dir_header_ofs, NULL)) + { + mz_zip_array_clear(pZip, &new_ext_block); + return MZ_FALSE; + } + + MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size); + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) + { + mz_zip_array_clear(pZip, &new_ext_block); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len)) + { + mz_zip_array_clear(pZip, &new_ext_block); + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size)) + { + mz_zip_array_clear(pZip, &new_ext_block); + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len)) + { + mz_zip_array_clear(pZip, &new_ext_block); + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + mz_zip_array_clear(pZip, &new_ext_block); + } + else + { + /* sanity checks */ + if (cur_dst_file_ofs > MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + if (local_dir_header_ofs >= MZ_UINT32_MAX) + return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); + + MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs); + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size)) + { + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + } + + /* This shouldn't trigger unless we screwed up during the initial sanity checks */ + if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) + { + /* TODO: Support central dirs >= 32-bits in size */ + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); + } + + n = (mz_uint32)orig_central_dir_size; + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) + { + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + } + + pZip->m_total_files++; + pZip->m_archive_size = cur_dst_file_ofs; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) +{ + mz_zip_internal_state *pState; + mz_uint64 central_dir_ofs, central_dir_size; + mz_uint8 hdr[256]; + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + pState = pZip->m_pState; + + if (pState->m_zip64) + { + if ((pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX)) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + else + { + if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)) + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); + } + + central_dir_ofs = 0; + central_dir_size = 0; + if (pZip->m_total_files) + { + /* Write central directory */ + central_dir_ofs = pZip->m_archive_size; + central_dir_size = pState->m_central_dir.m_size; + pZip->m_central_directory_file_ofs = central_dir_ofs; + if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + pZip->m_archive_size += central_dir_size; + } + + if (pState->m_zip64) + { + /* Write zip64 end of central directory header */ + mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size; + + MZ_CLEAR_OBJ(hdr); + MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64)); + MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */ + MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs); + if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE; + + /* Write zip64 end of central directory locator */ + MZ_CLEAR_OBJ(hdr); + MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG); + MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr); + MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1); + if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE; + } + + /* Write end of central directory record */ + MZ_CLEAR_OBJ(hdr); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); + MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size)); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs)); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + +#ifndef MINIZ_NO_STDIO + if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); +#endif /* #ifndef MINIZ_NO_STDIO */ + + pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE; + + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED; + return MZ_TRUE; +} + +mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize) +{ + if ((!ppBuf) || (!pSize)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + *ppBuf = NULL; + *pSize = 0; + + if ((!pZip) || (!pZip->m_pState)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (pZip->m_pWrite != mz_zip_heap_write_func) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + if (!mz_zip_writer_finalize_archive(pZip)) + return MZ_FALSE; + + *ppBuf = pZip->m_pState->m_pMem; + *pSize = pZip->m_pState->m_mem_size; + pZip->m_pState->m_pMem = NULL; + pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_end(mz_zip_archive *pZip) +{ + return mz_zip_writer_end_internal(pZip, MZ_TRUE); +} + +#ifndef MINIZ_NO_STDIO +mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags) +{ + return mz_zip_add_mem_to_archive_file_in_place_v2(pZip_filename, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, NULL); +} + +mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_zip_error *pErr) +{ + mz_bool status, created_new_archive = MZ_FALSE; + mz_zip_archive zip_archive; + struct MZ_FILE_STAT_STRUCT file_stat; + mz_zip_error actual_err = MZ_ZIP_NO_ERROR; + + mz_zip_zero_struct(&zip_archive); + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + + if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) + { + if (pErr) + *pErr = MZ_ZIP_INVALID_PARAMETER; + return MZ_FALSE; + } + + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + { + if (pErr) + *pErr = MZ_ZIP_INVALID_FILENAME; + return MZ_FALSE; + } + + /* Important: The regular non-64 bit version of stat() can fail here if the file is very large, which could cause the archive to be overwritten. */ + /* So be sure to compile with _LARGEFILE64_SOURCE 1 */ + if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) + { + /* Create a new archive. */ + if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0, level_and_flags)) + { + if (pErr) + *pErr = zip_archive.m_last_error; + return MZ_FALSE; + } + + created_new_archive = MZ_TRUE; + } + else + { + /* Append to an existing archive. */ + if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) + { + if (pErr) + *pErr = zip_archive.m_last_error; + return MZ_FALSE; + } + + if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags)) + { + if (pErr) + *pErr = zip_archive.m_last_error; + + mz_zip_reader_end_internal(&zip_archive, MZ_FALSE); + + return MZ_FALSE; + } + } + + status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0); + actual_err = zip_archive.m_last_error; + + /* Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always succeed, but we can try.) */ + if (!mz_zip_writer_finalize_archive(&zip_archive)) + { + if (!actual_err) + actual_err = zip_archive.m_last_error; + + status = MZ_FALSE; + } + + if (!mz_zip_writer_end_internal(&zip_archive, status)) + { + if (!actual_err) + actual_err = zip_archive.m_last_error; + + status = MZ_FALSE; + } + + if ((!status) && (created_new_archive)) + { + /* It's a new archive and something went wrong, so just delete it. */ + int ignoredStatus = MZ_DELETE_FILE(pZip_filename); + (void)ignoredStatus; + } + + if (pErr) + *pErr = actual_err; + + return status; +} + +void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr) +{ + mz_uint32 file_index; + mz_zip_archive zip_archive; + void *p = NULL; + + if (pSize) + *pSize = 0; + + if ((!pZip_filename) || (!pArchive_name)) + { + if (pErr) + *pErr = MZ_ZIP_INVALID_PARAMETER; + + return NULL; + } + + mz_zip_zero_struct(&zip_archive); + if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) + { + if (pErr) + *pErr = zip_archive.m_last_error; + + return NULL; + } + + if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags, &file_index)) + { + p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags); + } + + mz_zip_reader_end_internal(&zip_archive, p != NULL); + + if (pErr) + *pErr = zip_archive.m_last_error; + + return p; +} + +void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags) +{ + return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name, NULL, pSize, flags, NULL); +} + +#endif /* #ifndef MINIZ_NO_STDIO */ + +#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ + +/* ------------------- Misc utils */ + +mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip) +{ + return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID; +} + +mz_zip_type mz_zip_get_type(mz_zip_archive *pZip) +{ + return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID; +} + +mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num) +{ + mz_zip_error prev_err; + + if (!pZip) + return MZ_ZIP_INVALID_PARAMETER; + + prev_err = pZip->m_last_error; + + pZip->m_last_error = err_num; + return prev_err; +} + +mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip) +{ + if (!pZip) + return MZ_ZIP_INVALID_PARAMETER; + + return pZip->m_last_error; +} + +mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip) +{ + return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR); +} + +mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip) +{ + mz_zip_error prev_err; + + if (!pZip) + return MZ_ZIP_INVALID_PARAMETER; + + prev_err = pZip->m_last_error; + + pZip->m_last_error = MZ_ZIP_NO_ERROR; + return prev_err; +} + +const char *mz_zip_get_error_string(mz_zip_error mz_err) +{ + switch (mz_err) + { + case MZ_ZIP_NO_ERROR: + return "no error"; + case MZ_ZIP_UNDEFINED_ERROR: + return "undefined error"; + case MZ_ZIP_TOO_MANY_FILES: + return "too many files"; + case MZ_ZIP_FILE_TOO_LARGE: + return "file too large"; + case MZ_ZIP_UNSUPPORTED_METHOD: + return "unsupported method"; + case MZ_ZIP_UNSUPPORTED_ENCRYPTION: + return "unsupported encryption"; + case MZ_ZIP_UNSUPPORTED_FEATURE: + return "unsupported feature"; + case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR: + return "failed finding central directory"; + case MZ_ZIP_NOT_AN_ARCHIVE: + return "not a ZIP archive"; + case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED: + return "invalid header or archive is corrupted"; + case MZ_ZIP_UNSUPPORTED_MULTIDISK: + return "unsupported multidisk archive"; + case MZ_ZIP_DECOMPRESSION_FAILED: + return "decompression failed or archive is corrupted"; + case MZ_ZIP_COMPRESSION_FAILED: + return "compression failed"; + case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE: + return "unexpected decompressed size"; + case MZ_ZIP_CRC_CHECK_FAILED: + return "CRC-32 check failed"; + case MZ_ZIP_UNSUPPORTED_CDIR_SIZE: + return "unsupported central directory size"; + case MZ_ZIP_ALLOC_FAILED: + return "allocation failed"; + case MZ_ZIP_FILE_OPEN_FAILED: + return "file open failed"; + case MZ_ZIP_FILE_CREATE_FAILED: + return "file create failed"; + case MZ_ZIP_FILE_WRITE_FAILED: + return "file write failed"; + case MZ_ZIP_FILE_READ_FAILED: + return "file read failed"; + case MZ_ZIP_FILE_CLOSE_FAILED: + return "file close failed"; + case MZ_ZIP_FILE_SEEK_FAILED: + return "file seek failed"; + case MZ_ZIP_FILE_STAT_FAILED: + return "file stat failed"; + case MZ_ZIP_INVALID_PARAMETER: + return "invalid parameter"; + case MZ_ZIP_INVALID_FILENAME: + return "invalid filename"; + case MZ_ZIP_BUF_TOO_SMALL: + return "buffer too small"; + case MZ_ZIP_INTERNAL_ERROR: + return "internal error"; + case MZ_ZIP_FILE_NOT_FOUND: + return "file not found"; + case MZ_ZIP_ARCHIVE_TOO_LARGE: + return "archive is too large"; + case MZ_ZIP_VALIDATION_FAILED: + return "validation failed"; + case MZ_ZIP_WRITE_CALLBACK_FAILED: + return "write calledback failed"; + default: + break; + } + + return "unknown error"; +} + +/* Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra field, argh. */ +mz_bool mz_zip_is_zip64(mz_zip_archive *pZip) +{ + if ((!pZip) || (!pZip->m_pState)) + return MZ_FALSE; + + return pZip->m_pState->m_zip64; +} + +size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip) +{ + if ((!pZip) || (!pZip->m_pState)) + return 0; + + return pZip->m_pState->m_central_dir.m_size; +} + +mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip) +{ + return pZip ? pZip->m_total_files : 0; +} + +mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip) +{ + if (!pZip) + return 0; + return pZip->m_archive_size; +} + +mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip) +{ + if ((!pZip) || (!pZip->m_pState)) + return 0; + return pZip->m_pState->m_file_archive_start_ofs; +} + +MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip) +{ + if ((!pZip) || (!pZip->m_pState)) + return 0; + return pZip->m_pState->m_pFile; +} + +size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) + return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + + return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n); +} + +mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size) +{ + mz_uint n; + const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + if (!p) + { + if (filename_buf_size) + pFilename[0] = '\0'; + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + return 0; + } + n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + if (filename_buf_size) + { + n = MZ_MIN(n, filename_buf_size - 1); + memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); + pFilename[n] = '\0'; + } + return n + 1; +} + +mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat) +{ + return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL); +} + +mz_bool mz_zip_end(mz_zip_archive *pZip) +{ + if (!pZip) + return MZ_FALSE; + + if (pZip->m_zip_mode == MZ_ZIP_MODE_READING) + return mz_zip_reader_end(pZip); +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)) + return mz_zip_writer_end(pZip); +#endif + + return MZ_FALSE; +} + +#ifdef __cplusplus +} +#endif + +#endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/ diff --git a/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.h b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.h new file mode 100644 index 00000000000..6cc398c9203 --- /dev/null +++ b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/miniz.h @@ -0,0 +1,1350 @@ +#define MINIZ_EXPORT +/* miniz.c 2.2.0 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing + See "unlicense" statement at the end of this file. + Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013 + Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt + + Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define + MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). + + * Low-level Deflate/Inflate implementation notes: + + Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or + greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses + approximately as well as zlib. + + Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function + coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or into a memory + block large enough to hold the entire file. + + The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation. + + * zlib-style API notes: + + miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in + zlib replacement in many apps: + The z_stream struct, optional memory allocation callbacks + deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound + inflateInit/inflateInit2/inflate/inflateReset/inflateEnd + compress, compress2, compressBound, uncompress + CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines. + Supports raw deflate streams or standard zlib streams with adler-32 checking. + + Limitations: + The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries. + I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but + there are no guarantees that miniz.c pulls this off perfectly. + + * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by + Alex Evans. Supports 1-4 bytes/pixel images. + + * ZIP archive API notes: + + The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to + get the job done with minimal fuss. There are simple API's to retrieve file information, read files from + existing archives, create new archives, append new files to existing archives, or clone archive data from + one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h), + or you can specify custom file read/write callbacks. + + - Archive reading: Just call this function to read a single file from a disk archive: + + void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, + size_t *pSize, mz_uint zip_flags); + + For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central + directory is located and read as-is into memory, and subsequent file access only occurs when reading individual files. + + - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file: + + int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); + + The locate operation can optionally check file comments too, which (as one example) can be used to identify + multiple versions of the same file in an archive. This function uses a simple linear search through the central + directory, so it's not very fast. + + Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and + retrieve detailed info on each file by calling mz_zip_reader_file_stat(). + + - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data + to disk and builds an exact image of the central directory in memory. The central directory image is written + all at once at the end of the archive file when the archive is finalized. + + The archive writer can optionally align each file's local header and file data to any power of 2 alignment, + which can be useful when the archive will be read from optical media. Also, the writer supports placing + arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still + readable by any ZIP tool. + + - Archive appending: The simple way to add a single file to an archive is to call this function: + + mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, + const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); + + The archive will be created if it doesn't already exist, otherwise it'll be appended to. + Note the appending is done in-place and is not an atomic operation, so if something goes wrong + during the operation it's possible the archive could be left without a central directory (although the local + file headers and file data will be fine, so the archive will be recoverable). + + For more complex archive modification scenarios: + 1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to + preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the + compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and + you're done. This is safe but requires a bunch of temporary disk space or heap memory. + + 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(), + append new files as needed, then finalize the archive which will write an updated central directory to the + original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a + possibility that the archive's central directory could be lost with this method if anything goes wrong, though. + + - ZIP archive support limitations: + No spanning support. Extraction functions can only handle unencrypted, stored or deflated files. + Requires streams capable of seeking. + + * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the + below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. + + * Important: For best perf. be sure to customize the below macros for your target platform: + #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 + #define MINIZ_LITTLE_ENDIAN 1 + #define MINIZ_HAS_64BIT_REGISTERS 1 + + * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before including miniz.c to ensure miniz + uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be able to process large files + (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). +*/ +#pragma once + + + +/* Defines to completely disable specific portions of miniz.c: + If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. */ + +/* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. */ +/*#define MINIZ_NO_STDIO */ + +/* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or */ +/* get/set file times, and the C run-time funcs that get/set times won't be called. */ +/* The current downside is the times written to your archives will be from 1979. */ +/*#define MINIZ_NO_TIME */ + +/* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */ +/*#define MINIZ_NO_ARCHIVE_APIS */ + +/* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP archive API's. */ +/*#define MINIZ_NO_ARCHIVE_WRITING_APIS */ + +/* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. */ +/*#define MINIZ_NO_ZLIB_APIS */ + +/* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. */ +/*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ + +/* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. + Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc + callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user + functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. */ +/*#define MINIZ_NO_MALLOC */ + +#if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) +/* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux */ +#define MINIZ_NO_TIME +#endif + +#include <stddef.h> + +#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) +#include <time.h> +#endif + +#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__) +/* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */ +#define MINIZ_X86_OR_X64_CPU 1 +#else +#define MINIZ_X86_OR_X64_CPU 0 +#endif + +#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU +/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ +#define MINIZ_LITTLE_ENDIAN 1 +#else +#define MINIZ_LITTLE_ENDIAN 0 +#endif + +/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ +#if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) +#if MINIZ_X86_OR_X64_CPU +/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. */ +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 +#define MINIZ_UNALIGNED_USE_MEMCPY +#else +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 +#endif +#endif + +#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__) +/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). */ +#define MINIZ_HAS_64BIT_REGISTERS 1 +#else +#define MINIZ_HAS_64BIT_REGISTERS 0 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------- zlib-style API Definitions. */ + +/* For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! */ +typedef unsigned long mz_ulong; + +/* mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap. */ +MINIZ_EXPORT void mz_free(void *p); + +#define MZ_ADLER32_INIT (1) +/* mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. */ +MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len); + +#define MZ_CRC32_INIT (0) +/* mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. */ +MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len); + +/* Compression strategies. */ +enum +{ + MZ_DEFAULT_STRATEGY = 0, + MZ_FILTERED = 1, + MZ_HUFFMAN_ONLY = 2, + MZ_RLE = 3, + MZ_FIXED = 4 +}; + +/* Method */ +#define MZ_DEFLATED 8 + +/* Heap allocation callbacks. +Note that mz_alloc_func parameter types purposely differ from zlib's: items/size is size_t, not unsigned long. */ +typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); +typedef void (*mz_free_func)(void *opaque, void *address); +typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size); + +/* Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */ +enum +{ + MZ_NO_COMPRESSION = 0, + MZ_BEST_SPEED = 1, + MZ_BEST_COMPRESSION = 9, + MZ_UBER_COMPRESSION = 10, + MZ_DEFAULT_LEVEL = 6, + MZ_DEFAULT_COMPRESSION = -1 +}; + +#define MZ_VERSION "10.2.0" +#define MZ_VERNUM 0xA100 +#define MZ_VER_MAJOR 10 +#define MZ_VER_MINOR 2 +#define MZ_VER_REVISION 0 +#define MZ_VER_SUBREVISION 0 + +#ifndef MINIZ_NO_ZLIB_APIS + +/* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer to the zlib docs). */ +enum +{ + MZ_NO_FLUSH = 0, + MZ_PARTIAL_FLUSH = 1, + MZ_SYNC_FLUSH = 2, + MZ_FULL_FLUSH = 3, + MZ_FINISH = 4, + MZ_BLOCK = 5 +}; + +/* Return status codes. MZ_PARAM_ERROR is non-standard. */ +enum +{ + MZ_OK = 0, + MZ_STREAM_END = 1, + MZ_NEED_DICT = 2, + MZ_ERRNO = -1, + MZ_STREAM_ERROR = -2, + MZ_DATA_ERROR = -3, + MZ_MEM_ERROR = -4, + MZ_BUF_ERROR = -5, + MZ_VERSION_ERROR = -6, + MZ_PARAM_ERROR = -10000 +}; + +/* Window bits */ +#define MZ_DEFAULT_WINDOW_BITS 15 + +struct mz_internal_state; + +/* Compression/decompression stream struct. */ +typedef struct mz_stream_s +{ + const unsigned char *next_in; /* pointer to next byte to read */ + unsigned int avail_in; /* number of bytes available at next_in */ + mz_ulong total_in; /* total number of bytes consumed so far */ + + unsigned char *next_out; /* pointer to next byte to write */ + unsigned int avail_out; /* number of bytes that can be written to next_out */ + mz_ulong total_out; /* total number of bytes produced so far */ + + char *msg; /* error msg (unused) */ + struct mz_internal_state *state; /* internal state, allocated by zalloc/zfree */ + + mz_alloc_func zalloc; /* optional heap allocation function (defaults to malloc) */ + mz_free_func zfree; /* optional heap free function (defaults to free) */ + void *opaque; /* heap alloc function user pointer */ + + int data_type; /* data_type (unused) */ + mz_ulong adler; /* adler32 of the source or uncompressed data */ + mz_ulong reserved; /* not used */ +} mz_stream; + +typedef mz_stream *mz_streamp; + +/* Returns the version string of miniz.c. */ +MINIZ_EXPORT const char *mz_version(void); + +/* mz_deflateInit() initializes a compressor with default options: */ +/* Parameters: */ +/* pStream must point to an initialized mz_stream struct. */ +/* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */ +/* level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. */ +/* (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */ +/* Return values: */ +/* MZ_OK on success. */ +/* MZ_STREAM_ERROR if the stream is bogus. */ +/* MZ_PARAM_ERROR if the input parameters are bogus. */ +/* MZ_MEM_ERROR on out of memory. */ +MINIZ_EXPORT int mz_deflateInit(mz_streamp pStream, int level); + +/* mz_deflateInit2() is like mz_deflate(), except with more control: */ +/* Additional parameters: */ +/* method must be MZ_DEFLATED */ +/* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer) */ +/* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */ +MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy); + +/* Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */ +MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream); + +/* mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. */ +/* Parameters: */ +/* pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. */ +/* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. */ +/* Return values: */ +/* MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be written but the output buffer is full). */ +/* MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the stream anymore. */ +/* MZ_STREAM_ERROR if the stream is bogus. */ +/* MZ_PARAM_ERROR if one of the parameters is invalid. */ +/* MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the input buffer or free up some output space and try again.) */ +MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush); + +/* mz_deflateEnd() deinitializes a compressor: */ +/* Return values: */ +/* MZ_OK on success. */ +/* MZ_STREAM_ERROR if the stream is bogus. */ +MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream); + +/* mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH. */ +MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); + +/* Single-call compression functions mz_compress() and mz_compress2(): */ +/* Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. */ +MINIZ_EXPORT int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); +MINIZ_EXPORT int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level); + +/* mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). */ +MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len); + +/* Initializes a decompressor. */ +MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream); + +/* mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer: */ +/* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). */ +MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits); + +/* Quickly resets a compressor without having to reallocate anything. Same as calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */ +MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream); + +/* Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible. */ +/* Parameters: */ +/* pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. */ +/* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */ +/* On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster). */ +/* MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data. */ +/* Return values: */ +/* MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full. */ +/* MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified. */ +/* MZ_STREAM_ERROR if the stream is bogus. */ +/* MZ_DATA_ERROR if the deflate stream is invalid. */ +/* MZ_PARAM_ERROR if one of the parameters is invalid. */ +/* MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again */ +/* with more input data, or with more room in the output buffer (except when using single call decompression, described above). */ +MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush); + +/* Deinitializes a decompressor. */ +MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream); + +/* Single-call decompression. */ +/* Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. */ +MINIZ_EXPORT int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); +MINIZ_EXPORT int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len); + +/* Returns a string description of the specified error code, or NULL if the error code is invalid. */ +MINIZ_EXPORT const char *mz_error(int err); + +/* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset of zlib that miniz.c supports. */ +/* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. */ +#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES +typedef unsigned char Byte; +typedef unsigned int uInt; +typedef mz_ulong uLong; +typedef Byte Bytef; +typedef uInt uIntf; +typedef char charf; +typedef int intf; +typedef void *voidpf; +typedef uLong uLongf; +typedef void *voidp; +typedef void *const voidpc; +#define Z_NULL 0 +#define Z_NO_FLUSH MZ_NO_FLUSH +#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH +#define Z_SYNC_FLUSH MZ_SYNC_FLUSH +#define Z_FULL_FLUSH MZ_FULL_FLUSH +#define Z_FINISH MZ_FINISH +#define Z_BLOCK MZ_BLOCK +#define Z_OK MZ_OK +#define Z_STREAM_END MZ_STREAM_END +#define Z_NEED_DICT MZ_NEED_DICT +#define Z_ERRNO MZ_ERRNO +#define Z_STREAM_ERROR MZ_STREAM_ERROR +#define Z_DATA_ERROR MZ_DATA_ERROR +#define Z_MEM_ERROR MZ_MEM_ERROR +#define Z_BUF_ERROR MZ_BUF_ERROR +#define Z_VERSION_ERROR MZ_VERSION_ERROR +#define Z_PARAM_ERROR MZ_PARAM_ERROR +#define Z_NO_COMPRESSION MZ_NO_COMPRESSION +#define Z_BEST_SPEED MZ_BEST_SPEED +#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION +#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION +#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY +#define Z_FILTERED MZ_FILTERED +#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY +#define Z_RLE MZ_RLE +#define Z_FIXED MZ_FIXED +#define Z_DEFLATED MZ_DEFLATED +#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS +#define alloc_func mz_alloc_func +#define free_func mz_free_func +#define internal_state mz_internal_state +#define z_stream mz_stream +#define deflateInit mz_deflateInit +#define deflateInit2 mz_deflateInit2 +#define deflateReset mz_deflateReset +#define deflate mz_deflate +#define deflateEnd mz_deflateEnd +#define deflateBound mz_deflateBound +#define compress mz_compress +#define compress2 mz_compress2 +#define compressBound mz_compressBound +#define inflateInit mz_inflateInit +#define inflateInit2 mz_inflateInit2 +#define inflateReset mz_inflateReset +#define inflate mz_inflate +#define inflateEnd mz_inflateEnd +#define uncompress mz_uncompress +#define uncompress2 mz_uncompress2 +#define crc32 mz_crc32 +#define adler32 mz_adler32 +#define MAX_WBITS 15 +#define MAX_MEM_LEVEL 9 +#define zError mz_error +#define ZLIB_VERSION MZ_VERSION +#define ZLIB_VERNUM MZ_VERNUM +#define ZLIB_VER_MAJOR MZ_VER_MAJOR +#define ZLIB_VER_MINOR MZ_VER_MINOR +#define ZLIB_VER_REVISION MZ_VER_REVISION +#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION +#define zlibVersion mz_version +#define zlib_version mz_version() +#endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ + +#endif /* MINIZ_NO_ZLIB_APIS */ + +#ifdef __cplusplus +} +#endif + + + + + +#pragma once +#include <assert.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + + + +/* ------------------- Types and macros */ +typedef unsigned char mz_uint8; +typedef signed short mz_int16; +typedef unsigned short mz_uint16; +typedef unsigned int mz_uint32; +typedef unsigned int mz_uint; +typedef int64_t mz_int64; +typedef uint64_t mz_uint64; +typedef int mz_bool; + +#define MZ_FALSE (0) +#define MZ_TRUE (1) + +/* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */ +#ifdef _MSC_VER +#define MZ_MACRO_END while (0, 0) +#else +#define MZ_MACRO_END while (0) +#endif + +#ifdef MINIZ_NO_STDIO +#define MZ_FILE void * +#else +#include <stdio.h> +#define MZ_FILE FILE +#endif /* #ifdef MINIZ_NO_STDIO */ + +#ifdef MINIZ_NO_TIME +typedef struct mz_dummy_time_t_tag +{ + int m_dummy; +} mz_dummy_time_t; +#define MZ_TIME_T mz_dummy_time_t +#else +#define MZ_TIME_T time_t +#endif + +#define MZ_ASSERT(x) assert(x) + +#ifdef MINIZ_NO_MALLOC +#define MZ_MALLOC(x) NULL +#define MZ_FREE(x) (void)x, ((void)0) +#define MZ_REALLOC(p, x) NULL +#else +#define MZ_MALLOC(x) malloc(x) +#define MZ_FREE(x) free(x) +#define MZ_REALLOC(p, x) realloc(p, x) +#endif + +#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN +#define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) +#define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) +#else +#define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U)) +#define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U)) +#endif + +#define MZ_READ_LE64(p) (((mz_uint64)MZ_READ_LE32(p)) | (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) << 32U)) + +#ifdef _MSC_VER +#define MZ_FORCEINLINE __forceinline +#elif defined(__GNUC__) +#define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__)) +#else +#define MZ_FORCEINLINE inline +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +extern MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size); +extern MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address); +extern MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size); + +#define MZ_UINT16_MAX (0xFFFFU) +#define MZ_UINT32_MAX (0xFFFFFFFFU) + +#ifdef __cplusplus +} +#endif + #pragma once + + +#ifdef __cplusplus +extern "C" { +#endif +/* ------------------- Low-level Compression API Definitions */ + +/* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). */ +#define TDEFL_LESS_MEMORY 0 + +/* tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): */ +/* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). */ +enum +{ + TDEFL_HUFFMAN_ONLY = 0, + TDEFL_DEFAULT_MAX_PROBES = 128, + TDEFL_MAX_PROBES_MASK = 0xFFF +}; + +/* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data. */ +/* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). */ +/* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. */ +/* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory). */ +/* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) */ +/* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */ +/* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */ +/* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */ +/* The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). */ +enum +{ + TDEFL_WRITE_ZLIB_HEADER = 0x01000, + TDEFL_COMPUTE_ADLER32 = 0x02000, + TDEFL_GREEDY_PARSING_FLAG = 0x04000, + TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, + TDEFL_RLE_MATCHES = 0x10000, + TDEFL_FILTER_MATCHES = 0x20000, + TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, + TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 +}; + +/* High level compression functions: */ +/* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). */ +/* On entry: */ +/* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */ +/* flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression. */ +/* On return: */ +/* Function returns a pointer to the compressed data, or NULL on failure. */ +/* *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. */ +/* The caller must free() the returned block when it's no longer needed. */ +MINIZ_EXPORT void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); + +/* tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. */ +/* Returns 0 on failure. */ +MINIZ_EXPORT size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); + +/* Compresses an image to a compressed PNG file in memory. */ +/* On entry: */ +/* pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. */ +/* The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory. */ +/* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL */ +/* If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). */ +/* On return: */ +/* Function returns a pointer to the compressed data, or NULL on failure. */ +/* *pLen_out will be set to the size of the PNG image file. */ +/* The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed. */ +MINIZ_EXPORT void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip); +MINIZ_EXPORT void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out); + +/* Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */ +typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser); + +/* tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. */ +MINIZ_EXPORT mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); + +enum +{ + TDEFL_MAX_HUFF_TABLES = 3, + TDEFL_MAX_HUFF_SYMBOLS_0 = 288, + TDEFL_MAX_HUFF_SYMBOLS_1 = 32, + TDEFL_MAX_HUFF_SYMBOLS_2 = 19, + TDEFL_LZ_DICT_SIZE = 32768, + TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, + TDEFL_MIN_MATCH_LEN = 3, + TDEFL_MAX_MATCH_LEN = 258 +}; + +/* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes). */ +#if TDEFL_LESS_MEMORY +enum +{ + TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, + TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, + TDEFL_MAX_HUFF_SYMBOLS = 288, + TDEFL_LZ_HASH_BITS = 12, + TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, + TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, + TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS +}; +#else +enum +{ + TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, + TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, + TDEFL_MAX_HUFF_SYMBOLS = 288, + TDEFL_LZ_HASH_BITS = 15, + TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, + TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, + TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS +}; +#endif + +/* The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions. */ +typedef enum { + TDEFL_STATUS_BAD_PARAM = -2, + TDEFL_STATUS_PUT_BUF_FAILED = -1, + TDEFL_STATUS_OKAY = 0, + TDEFL_STATUS_DONE = 1 +} tdefl_status; + +/* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */ +typedef enum { + TDEFL_NO_FLUSH = 0, + TDEFL_SYNC_FLUSH = 2, + TDEFL_FULL_FLUSH = 3, + TDEFL_FINISH = 4 +} tdefl_flush; + +/* tdefl's compression state structure. */ +typedef struct +{ + tdefl_put_buf_func_ptr m_pPut_buf_func; + void *m_pPut_buf_user; + mz_uint m_flags, m_max_probes[2]; + int m_greedy_parsing; + mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; + mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; + mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer; + mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish; + tdefl_status m_prev_return_status; + const void *m_pIn_buf; + void *m_pOut_buf; + size_t *m_pIn_buf_size, *m_pOut_buf_size; + tdefl_flush m_flush; + const mz_uint8 *m_pSrc; + size_t m_src_buf_left, m_out_buf_ofs; + mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; + mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; + mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; + mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; + mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; +} tdefl_compressor; + +/* Initializes the compressor. */ +/* There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. */ +/* pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call the tdefl_compress_buffer() API for compression. */ +/* If pBut_buf_func is NULL the user should always call the tdefl_compress() API. */ +/* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) */ +MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); + +/* Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible. */ +MINIZ_EXPORT tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush); + +/* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. */ +/* tdefl_compress_buffer() always consumes the entire input buffer. */ +MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush); + +MINIZ_EXPORT tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d); +MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor *d); + +/* Create tdefl_compress() flags given zlib-style compression parameters. */ +/* level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) */ +/* window_bits may be -15 (raw deflate) or 15 (zlib) */ +/* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED */ +MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy); + +#ifndef MINIZ_NO_MALLOC +/* Allocate the tdefl_compressor structure in C so that */ +/* non-C language bindings to tdefl_ API don't need to worry about */ +/* structure size and allocation mechanism. */ +MINIZ_EXPORT tdefl_compressor *tdefl_compressor_alloc(void); +MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor *pComp); +#endif + +#ifdef __cplusplus +} +#endif + #pragma once + +/* ------------------- Low-level Decompression API Definitions */ + +#ifdef __cplusplus +extern "C" { +#endif +/* Decompression flags used by tinfl_decompress(). */ +/* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. */ +/* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. */ +/* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). */ +/* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. */ +enum +{ + TINFL_FLAG_PARSE_ZLIB_HEADER = 1, + TINFL_FLAG_HAS_MORE_INPUT = 2, + TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, + TINFL_FLAG_COMPUTE_ADLER32 = 8 +}; + +/* High level decompression functions: */ +/* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). */ +/* On entry: */ +/* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. */ +/* On return: */ +/* Function returns a pointer to the decompressed data, or NULL on failure. */ +/* *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. */ +/* The caller must call mz_free() on the returned block when it's no longer needed. */ +MINIZ_EXPORT void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); + +/* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. */ +/* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. */ +#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) +MINIZ_EXPORT size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); + +/* tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. */ +/* Returns 1 on success or 0 on failure. */ +typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser); +MINIZ_EXPORT int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); + +struct tinfl_decompressor_tag; +typedef struct tinfl_decompressor_tag tinfl_decompressor; + +#ifndef MINIZ_NO_MALLOC +/* Allocate the tinfl_decompressor structure in C so that */ +/* non-C language bindings to tinfl_ API don't need to worry about */ +/* structure size and allocation mechanism. */ +MINIZ_EXPORT tinfl_decompressor *tinfl_decompressor_alloc(void); +MINIZ_EXPORT void tinfl_decompressor_free(tinfl_decompressor *pDecomp); +#endif + +/* Max size of LZ dictionary. */ +#define TINFL_LZ_DICT_SIZE 32768 + +/* Return status. */ +typedef enum { + /* This flags indicates the inflator needs 1 or more input bytes to make forward progress, but the caller is indicating that no more are available. The compressed data */ + /* is probably corrupted. If you call the inflator again with more bytes it'll try to continue processing the input but this is a BAD sign (either the data is corrupted or you called it incorrectly). */ + /* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */ + TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4, + + /* This flag indicates that one or more of the input parameters was obviously bogus. (You can try calling it again, but if you get this error the calling code is wrong.) */ + TINFL_STATUS_BAD_PARAM = -3, + + /* This flags indicate the inflator is finished but the adler32 check of the uncompressed data didn't match. If you call it again it'll return TINFL_STATUS_DONE. */ + TINFL_STATUS_ADLER32_MISMATCH = -2, + + /* This flags indicate the inflator has somehow failed (bad code, corrupted input, etc.). If you call it again without resetting via tinfl_init() it it'll just keep on returning the same status failure code. */ + TINFL_STATUS_FAILED = -1, + + /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */ + + /* This flag indicates the inflator has returned every byte of uncompressed data that it can, has consumed every byte that it needed, has successfully reached the end of the deflate stream, and */ + /* if zlib headers and adler32 checking enabled that it has successfully checked the uncompressed data's adler32. If you call it again you'll just get TINFL_STATUS_DONE over and over again. */ + TINFL_STATUS_DONE = 0, + + /* This flag indicates the inflator MUST have more input data (even 1 byte) before it can make any more forward progress, or you need to clear the TINFL_FLAG_HAS_MORE_INPUT */ + /* flag on the next call if you don't have any more source data. If the source data was somehow corrupted it's also possible (but unlikely) for the inflator to keep on demanding input to */ + /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */ + TINFL_STATUS_NEEDS_MORE_INPUT = 1, + + /* This flag indicates the inflator definitely has 1 or more bytes of uncompressed data available, but it cannot write this data into the output buffer. */ + /* Note if the source compressed data was corrupted it's possible for the inflator to return a lot of uncompressed data to the caller. I've been assuming you know how much uncompressed data to expect */ + /* (either exact or worst case) and will stop calling the inflator and fail after receiving too much. In pure streaming scenarios where you have no idea how many bytes to expect this may not be possible */ + /* so I may need to add some code to address this. */ + TINFL_STATUS_HAS_MORE_OUTPUT = 2 +} tinfl_status; + +/* Initializes the decompressor to its initial state. */ +#define tinfl_init(r) \ + do \ + { \ + (r)->m_state = 0; \ + } \ + MZ_MACRO_END +#define tinfl_get_adler32(r) (r)->m_check_adler32 + +/* Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. */ +/* This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. */ +MINIZ_EXPORT tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags); + +/* Internal/private bits follow. */ +enum +{ + TINFL_MAX_HUFF_TABLES = 3, + TINFL_MAX_HUFF_SYMBOLS_0 = 288, + TINFL_MAX_HUFF_SYMBOLS_1 = 32, + TINFL_MAX_HUFF_SYMBOLS_2 = 19, + TINFL_FAST_LOOKUP_BITS = 10, + TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS +}; + +typedef struct +{ + mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; + mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; +} tinfl_huff_table; + +#if MINIZ_HAS_64BIT_REGISTERS +#define TINFL_USE_64BIT_BITBUF 1 +#else +#define TINFL_USE_64BIT_BITBUF 0 +#endif + +#if TINFL_USE_64BIT_BITBUF +typedef mz_uint64 tinfl_bit_buf_t; +#define TINFL_BITBUF_SIZE (64) +#else +typedef mz_uint32 tinfl_bit_buf_t; +#define TINFL_BITBUF_SIZE (32) +#endif + +struct tinfl_decompressor_tag +{ + mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; + tinfl_bit_buf_t m_bit_buf; + size_t m_dist_from_out_buf_start; + tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; + mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; +}; + +#ifdef __cplusplus +} +#endif + +#pragma once + + +/* ------------------- ZIP archive reading/writing */ + +#ifndef MINIZ_NO_ARCHIVE_APIS + +#ifdef __cplusplus +extern "C" { +#endif + +enum +{ + /* Note: These enums can be reduced as needed to save memory or stack space - they are pretty conservative. */ + MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, + MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512, + MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512 +}; + +typedef struct +{ + /* Central directory file index. */ + mz_uint32 m_file_index; + + /* Byte offset of this entry in the archive's central directory. Note we currently only support up to UINT_MAX or less bytes in the central dir. */ + mz_uint64 m_central_dir_ofs; + + /* These fields are copied directly from the zip's central dir. */ + mz_uint16 m_version_made_by; + mz_uint16 m_version_needed; + mz_uint16 m_bit_flag; + mz_uint16 m_method; + +#ifndef MINIZ_NO_TIME + MZ_TIME_T m_time; +#endif + + /* CRC-32 of uncompressed data. */ + mz_uint32 m_crc32; + + /* File's compressed size. */ + mz_uint64 m_comp_size; + + /* File's uncompressed size. Note, I've seen some old archives where directory entries had 512 bytes for their uncompressed sizes, but when you try to unpack them you actually get 0 bytes. */ + mz_uint64 m_uncomp_size; + + /* Zip internal and external file attributes. */ + mz_uint16 m_internal_attr; + mz_uint32 m_external_attr; + + /* Entry's local header file offset in bytes. */ + mz_uint64 m_local_header_ofs; + + /* Size of comment in bytes. */ + mz_uint32 m_comment_size; + + /* MZ_TRUE if the entry appears to be a directory. */ + mz_bool m_is_directory; + + /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip doesn't support) */ + mz_bool m_is_encrypted; + + /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a compression method we support. */ + mz_bool m_is_supported; + + /* Filename. If string ends in '/' it's a subdirectory entry. */ + /* Guaranteed to be zero terminated, may be truncated to fit. */ + char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; + + /* Comment field. */ + /* Guaranteed to be zero terminated, may be truncated to fit. */ + char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; + +} mz_zip_archive_file_stat; + +typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n); +typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n); +typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque); + +struct mz_zip_internal_state_tag; +typedef struct mz_zip_internal_state_tag mz_zip_internal_state; + +typedef enum { + MZ_ZIP_MODE_INVALID = 0, + MZ_ZIP_MODE_READING = 1, + MZ_ZIP_MODE_WRITING = 2, + MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 +} mz_zip_mode; + +typedef enum { + MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, + MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, + MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, + MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800, + MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG = 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each file as its validated to ensure the func finds the file in the central dir (intended for testing) */ + MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 0x2000, /* validate the local headers, but don't decompress the entire file and check the crc32 */ + MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* always use the zip64 file format, instead of the original zip file format with automatic switch to zip64. Use as flags parameter with mz_zip_writer_init*_v2 */ + MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000, + MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000, + /*After adding a compressed file, seek back + to local file header and set the correct sizes*/ + MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000 +} mz_zip_flags; + +typedef enum { + MZ_ZIP_TYPE_INVALID = 0, + MZ_ZIP_TYPE_USER, + MZ_ZIP_TYPE_MEMORY, + MZ_ZIP_TYPE_HEAP, + MZ_ZIP_TYPE_FILE, + MZ_ZIP_TYPE_CFILE, + MZ_ZIP_TOTAL_TYPES +} mz_zip_type; + +/* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or modify this enum. */ +typedef enum { + MZ_ZIP_NO_ERROR = 0, + MZ_ZIP_UNDEFINED_ERROR, + MZ_ZIP_TOO_MANY_FILES, + MZ_ZIP_FILE_TOO_LARGE, + MZ_ZIP_UNSUPPORTED_METHOD, + MZ_ZIP_UNSUPPORTED_ENCRYPTION, + MZ_ZIP_UNSUPPORTED_FEATURE, + MZ_ZIP_FAILED_FINDING_CENTRAL_DIR, + MZ_ZIP_NOT_AN_ARCHIVE, + MZ_ZIP_INVALID_HEADER_OR_CORRUPTED, + MZ_ZIP_UNSUPPORTED_MULTIDISK, + MZ_ZIP_DECOMPRESSION_FAILED, + MZ_ZIP_COMPRESSION_FAILED, + MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE, + MZ_ZIP_CRC_CHECK_FAILED, + MZ_ZIP_UNSUPPORTED_CDIR_SIZE, + MZ_ZIP_ALLOC_FAILED, + MZ_ZIP_FILE_OPEN_FAILED, + MZ_ZIP_FILE_CREATE_FAILED, + MZ_ZIP_FILE_WRITE_FAILED, + MZ_ZIP_FILE_READ_FAILED, + MZ_ZIP_FILE_CLOSE_FAILED, + MZ_ZIP_FILE_SEEK_FAILED, + MZ_ZIP_FILE_STAT_FAILED, + MZ_ZIP_INVALID_PARAMETER, + MZ_ZIP_INVALID_FILENAME, + MZ_ZIP_BUF_TOO_SMALL, + MZ_ZIP_INTERNAL_ERROR, + MZ_ZIP_FILE_NOT_FOUND, + MZ_ZIP_ARCHIVE_TOO_LARGE, + MZ_ZIP_VALIDATION_FAILED, + MZ_ZIP_WRITE_CALLBACK_FAILED, + MZ_ZIP_TOTAL_ERRORS +} mz_zip_error; + +typedef struct +{ + mz_uint64 m_archive_size; + mz_uint64 m_central_directory_file_ofs; + + /* We only support up to UINT32_MAX files in zip64 mode. */ + mz_uint32 m_total_files; + mz_zip_mode m_zip_mode; + mz_zip_type m_zip_type; + mz_zip_error m_last_error; + + mz_uint64 m_file_offset_alignment; + + mz_alloc_func m_pAlloc; + mz_free_func m_pFree; + mz_realloc_func m_pRealloc; + void *m_pAlloc_opaque; + + mz_file_read_func m_pRead; + mz_file_write_func m_pWrite; + mz_file_needs_keepalive m_pNeeds_keepalive; + void *m_pIO_opaque; + + mz_zip_internal_state *m_pState; + +} mz_zip_archive; + +typedef struct +{ + mz_zip_archive *pZip; + mz_uint flags; + + int status; +#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS + mz_uint file_crc32; +#endif + mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, out_buf_ofs, cur_file_ofs; + mz_zip_archive_file_stat file_stat; + void *pRead_buf; + void *pWrite_buf; + + size_t out_blk_remain; + + tinfl_decompressor inflator; + +} mz_zip_reader_extract_iter_state; + +/* -------- ZIP reading */ + +/* Inits a ZIP archive reader. */ +/* These functions read and validate the archive's central directory. */ +MINIZ_EXPORT mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags); + +MINIZ_EXPORT mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags); + +#ifndef MINIZ_NO_STDIO +/* Read a archive from a disk file. */ +/* file_start_ofs is the file offset where the archive actually begins, or 0. */ +/* actual_archive_size is the true total size of the archive, which may be smaller than the file's actual size on disk. If zero the entire file is treated as the archive. */ +MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags); +MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags, mz_uint64 file_start_ofs, mz_uint64 archive_size); + +/* Read an archive from an already opened FILE, beginning at the current file position. */ +/* The archive is assumed to be archive_size bytes long. If archive_size is 0, then the entire rest of the file is assumed to contain the archive. */ +/* The FILE will NOT be closed when mz_zip_reader_end() is called. */ +MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags); +#endif + +/* Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used. */ +MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive *pZip); + +/* -------- ZIP reading or writing */ + +/* Clears a mz_zip_archive struct to all zeros. */ +/* Important: This must be done before passing the struct to any mz_zip functions. */ +MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive *pZip); + +MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip); +MINIZ_EXPORT mz_zip_type mz_zip_get_type(mz_zip_archive *pZip); + +/* Returns the total number of files in the archive. */ +MINIZ_EXPORT mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip); + +MINIZ_EXPORT mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip); +MINIZ_EXPORT mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip); +MINIZ_EXPORT MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip); + +/* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. */ +MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n); + +/* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. These functions retrieve/manipulate this field. */ +/* Note that the m_last_error functionality is not thread safe. */ +MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num); +MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip); +MINIZ_EXPORT mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip); +MINIZ_EXPORT mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip); +MINIZ_EXPORT const char *mz_zip_get_error_string(mz_zip_error mz_err); + +/* MZ_TRUE if the archive file entry is a directory entry. */ +MINIZ_EXPORT mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index); + +/* MZ_TRUE if the file is encrypted/strong encrypted. */ +MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index); + +/* MZ_TRUE if the compression method is supported, and the file is not encrypted, and the file is not a compressed patch file. */ +MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index); + +/* Retrieves the filename of an archive file entry. */ +/* Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of bytes needed to fully store the filename. */ +MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size); + +/* Attempts to locates a file in the archive's central directory. */ +/* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */ +/* Returns -1 if the file cannot be found. */ +MINIZ_EXPORT int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *file_index); + +/* Returns detailed information about an archive file entry. */ +MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat); + +/* MZ_TRUE if the file is in zip64 format. */ +/* A file is considered zip64 if it contained a zip64 end of central directory marker, or if it contained any zip64 extended file information fields in the central directory. */ +MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive *pZip); + +/* Returns the total central directory size in bytes. */ +/* The current max supported size is <= MZ_UINT32_MAX. */ +MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip); + +/* Extracts a archive file to a memory buffer using no memory allocation. */ +/* There must be at least enough room on the stack to store the inflator's state (~34KB or so). */ +MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); + +/* Extracts a archive file to a memory buffer. */ +MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags); + +/* Extracts a archive file to a dynamically allocated heap buffer. */ +/* The memory will be allocated via the mz_zip_archive's alloc/realloc functions. */ +/* Returns NULL and sets the last error on failure. */ +MINIZ_EXPORT void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags); +MINIZ_EXPORT void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags); + +/* Extracts a archive file using a callback function to output the file's data. */ +MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); + +/* Extract a file iteratively */ +MINIZ_EXPORT mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); +MINIZ_EXPORT mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags); +MINIZ_EXPORT size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState); + +#ifndef MINIZ_NO_STDIO +/* Extracts a archive file to a disk file and sets its last accessed and modified times. */ +/* This function only extracts files, not archive directory records. */ +MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags); + +/* Extracts a archive file starting at the current position in the destination FILE stream. */ +MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *File, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags); +#endif + +#if 0 +/* TODO */ + typedef void *mz_zip_streaming_extract_state_ptr; + mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); + uint64_t mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); + uint64_t mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); + mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, uint64_t new_ofs); + size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size); + mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); +#endif + +/* This function compares the archive's local headers, the optional local zip64 extended information block, and the optional descriptor following the compressed data vs. the data in the central directory. */ +/* It also validates that each file can be successfully uncompressed unless the MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */ +MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); + +/* Validates an entire archive by calling mz_zip_validate_file() on each file. */ +MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags); + +/* Misc utils/helpers, valid for ZIP reading or writing */ +MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr); +MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr); + +/* Universal end function - calls either mz_zip_reader_end() or mz_zip_writer_end(). */ +MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive *pZip); + +/* -------- ZIP writing */ + +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +/* Inits a ZIP archive writer. */ +/*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init or mz_zip_writer_init_v2*/ +/*The output is streamable, i.e. file_ofs in mz_file_write_func always increases only by n*/ +MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size); +MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags); + +MINIZ_EXPORT mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size); +MINIZ_EXPORT mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags); + +#ifndef MINIZ_NO_STDIO +MINIZ_EXPORT mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning); +MINIZ_EXPORT mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags); +MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags); +#endif + +/* Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive. */ +/* For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for writing. If the file can't be reopened, mz_zip_reader_end() will be called. */ +/* For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which defaults to realloc unless you've overridden it). */ +/* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL. */ +/* Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before */ +/* the archive is finalized the file's central directory will be hosed. */ +MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename); +MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags); + +/* Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. */ +/* To add a directory entry, call this method with an archive name ending in a forwardslash with an empty buffer. */ +/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ +MINIZ_EXPORT mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags); + +/* Like mz_zip_writer_add_mem(), except you can specify a file comment field, and optionally supply the function with already compressed data. */ +/* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA flag is specified. */ +MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, + mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); + +MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, + mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T *last_modified, const char *user_extra_data_local, mz_uint user_extra_data_local_len, + const char *user_extra_data_central, mz_uint user_extra_data_central_len); + +/* Adds the contents of a file to an archive. This function also records the disk file's modified time into the archive. */ +/* File data is supplied via a read callback function. User mz_zip_writer_add_(c)file to add a file directly.*/ +MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, + const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data_local, mz_uint user_extra_data_local_len, + const char *user_extra_data_central, mz_uint user_extra_data_central_len); + + +#ifndef MINIZ_NO_STDIO +/* Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. */ +/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ +MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); + +/* Like mz_zip_writer_add_file(), except the file data is read from the specified FILE stream. */ +MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, + const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data_local, mz_uint user_extra_data_local_len, + const char *user_extra_data_central, mz_uint user_extra_data_central_len); +#endif + +/* Adds a file to an archive by fully cloning the data from another archive. */ +/* This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data (it may add or modify the zip64 local header extra data field), and the optional descriptor following the compressed data. */ +MINIZ_EXPORT mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index); + +/* Finalizes the archive by writing the central directory records followed by the end of central directory record. */ +/* After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). */ +/* An archive must be manually finalized by calling this function for it to be valid. */ +MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); + +/* Finalizes a heap archive, returning a poiner to the heap block and its size. */ +/* The heap block will be allocated using the mz_zip_archive's alloc/realloc callbacks. */ +MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize); + +/* Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. */ +/* Note for the archive to be valid, it *must* have been finalized before ending (this function will not do it for you). */ +MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive *pZip); + +/* -------- Misc. high-level helper functions: */ + +/* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. */ +/* Note this is NOT a fully safe operation. If it crashes or dies in some way your archive can be left in a screwed up state (without a central directory). */ +/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ +/* TODO: Perhaps add an option to leave the existing central dir in place in case the add dies? We could then truncate the file (so the old central dir would be at the end) if something goes wrong. */ +MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); +MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_zip_error *pErr); + +/* Reads a single file from an archive into a heap block. */ +/* If pComment is not NULL, only the file with the specified comment will be extracted. */ +/* Returns NULL on failure. */ +MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags); +MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr); + +#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ + +#ifdef __cplusplus +} +#endif + +#endif /* MINIZ_NO_ARCHIVE_APIS */ diff --git a/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/readme.md b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/readme.md new file mode 100644 index 00000000000..3f8fd73247f --- /dev/null +++ b/3rdparty/bimg/3rdparty/tinyexr/deps/miniz/readme.md @@ -0,0 +1,34 @@ +## Miniz + +Miniz is a lossless, high performance data compression library in a single source file that implements the zlib (RFC 1950) and Deflate (RFC 1951) compressed data format specification standards. It supports the most commonly used functions exported by the zlib library, but is a completely independent implementation so zlib's licensing requirements do not apply. Miniz also contains simple to use functions for writing .PNG format image files and reading/writing/appending .ZIP format archives. Miniz's compression speed has been tuned to be comparable to zlib's, and it also has a specialized real-time compressor function designed to compare well against fastlz/minilzo. + +## Usage + +Please use the files from the [releases page](https://github.com/richgel999/miniz/releases) in your projects. Do not use the git checkout directly! The different source and header files are [amalgamated](https://www.sqlite.org/amalgamation.html) into one `miniz.c`/`miniz.h` pair in a build step (`amalgamate.sh`). Include `miniz.c` and `miniz.h` in your project to use Miniz. + +## Features + +* MIT licensed +* A portable, single source and header file library written in plain C. Tested with GCC, clang and Visual Studio. +* Easily tuned and trimmed down by defines +* A drop-in replacement for zlib's most used API's (tested in several open source projects that use zlib, such as libpng and libzip). +* Fills a single threaded performance vs. compression ratio gap between several popular real-time compressors and zlib. For example, at level 1, miniz.c compresses around 5-9% better than minilzo, but is approx. 35% slower. At levels 2-9, miniz.c is designed to compare favorably against zlib's ratio and speed. See the miniz performance comparison page for example timings. +* Not a block based compressor: miniz.c fully supports stream based processing using a coroutine-style implementation. The zlib-style API functions can be called a single byte at a time if that's all you've got. +* Easy to use. The low-level compressor (tdefl) and decompressor (tinfl) have simple state structs which can be saved/restored as needed with simple memcpy's. The low-level codec API's don't use the heap in any way. +* Entire inflater (including optional zlib header parsing and Adler-32 checking) is implemented in a single function as a coroutine, which is separately available in a small (~550 line) source file: miniz_tinfl.c +* A fairly complete (but totally optional) set of .ZIP archive manipulation and extraction API's. The archive functionality is intended to solve common problems encountered in embedded, mobile, or game development situations. (The archive API's are purposely just powerful enough to write an entire archiver given a bit of additional higher-level logic.) + +## Known Problems + +* No support for encrypted archives. Not sure how useful this stuff is in practice. +* Minimal documentation. The assumption is that the user is already familiar with the basic zlib API. I need to write an API wiki - for now I've tried to place key comments before each enum/API, and I've included 6 examples that demonstrate how to use the module's major features. + +## Special Thanks + +Thanks to Alex Evans for the PNG writer function. Also, thanks to Paul Holden and Thorsten Scheuermann for feedback and testing, Matt Pritchard for all his encouragement, and Sean Barrett's various public domain libraries for inspiration (and encouraging me to write miniz.c in C, which was much more enjoyable and less painful than I thought it would be considering I've been programming in C++ for so long). + +Thanks to Bruce Dawson for reporting a problem with the level_and_flags archive API parameter (which is fixed in v1.12) and general feedback, and Janez Zemva for indirectly encouraging me into writing more examples. + +## Patents + +I was recently asked if miniz avoids patent issues. miniz purposely uses the same core algorithms as the ones used by zlib. The compressor uses vanilla hash chaining as described [here](https://datatracker.ietf.org/doc/html/rfc1951#section-4). Also see the [gzip FAQ](https://web.archive.org/web/20160308045258/http://www.gzip.org/#faq11). In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too. diff --git a/3rdparty/bimg/3rdparty/tinyexr/tinyexr.h b/3rdparty/bimg/3rdparty/tinyexr/tinyexr.h index 3255bbc7350..b8ecad20ec5 100644 --- a/3rdparty/bimg/3rdparty/tinyexr/tinyexr.h +++ b/3rdparty/bimg/3rdparty/tinyexr/tinyexr.h @@ -1,5 +1,7 @@ +#ifndef TINYEXR_H_ +#define TINYEXR_H_ /* -Copyright (c) 2014 - 2018, Syoyo Fujita and many contributors. +Copyright (c) 2014 - 2021, Syoyo Fujita and many contributors. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -63,8 +65,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // End of OpenEXR license ------------------------------------------------- -#ifndef TINYEXR_H_ -#define TINYEXR_H_ // // @@ -89,7 +89,21 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. extern "C" { #endif -// Use embedded miniz or not to decode ZIP format pixel. Linking with zlib +#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \ + defined(__i386) || defined(__i486__) || defined(__i486) || \ + defined(i386) || defined(__ia64__) || defined(__x86_64__) +#define TINYEXR_X86_OR_X64_CPU 1 +#else +#define TINYEXR_X86_OR_X64_CPU 0 +#endif + +#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || TINYEXR_X86_OR_X64_CPU +#define TINYEXR_LITTLE_ENDIAN 1 +#else +#define TINYEXR_LITTLE_ENDIAN 0 +#endif + +// Use miniz or not to decode ZIP format pixel. Linking with zlib // required if this flas is 0. #ifndef TINYEXR_USE_MINIZ #define TINYEXR_USE_MINIZ (1) @@ -105,19 +119,33 @@ extern "C" { // http://computation.llnl.gov/projects/floating-point-compression #endif +#ifndef TINYEXR_USE_THREAD +#define TINYEXR_USE_THREAD (0) // No threaded loading. +// http://computation.llnl.gov/projects/floating-point-compression +#endif + +#ifndef TINYEXR_USE_OPENMP +#ifdef _OPENMP +#define TINYEXR_USE_OPENMP (1) +#else +#define TINYEXR_USE_OPENMP (0) +#endif +#endif + #define TINYEXR_SUCCESS (0) #define TINYEXR_ERROR_INVALID_MAGIC_NUMBER (-1) #define TINYEXR_ERROR_INVALID_EXR_VERSION (-2) #define TINYEXR_ERROR_INVALID_ARGUMENT (-3) #define TINYEXR_ERROR_INVALID_DATA (-4) #define TINYEXR_ERROR_INVALID_FILE (-5) -#define TINYEXR_ERROR_INVALID_PARAMETER (-5) -#define TINYEXR_ERROR_CANT_OPEN_FILE (-6) -#define TINYEXR_ERROR_UNSUPPORTED_FORMAT (-7) -#define TINYEXR_ERROR_INVALID_HEADER (-8) -#define TINYEXR_ERROR_UNSUPPORTED_FEATURE (-9) -#define TINYEXR_ERROR_CANT_WRITE_FILE (-10) -#define TINYEXR_ERROR_SERIALZATION_FAILED (-11) +#define TINYEXR_ERROR_INVALID_PARAMETER (-6) +#define TINYEXR_ERROR_CANT_OPEN_FILE (-7) +#define TINYEXR_ERROR_UNSUPPORTED_FORMAT (-8) +#define TINYEXR_ERROR_INVALID_HEADER (-9) +#define TINYEXR_ERROR_UNSUPPORTED_FEATURE (-10) +#define TINYEXR_ERROR_CANT_WRITE_FILE (-11) +#define TINYEXR_ERROR_SERIALZATION_FAILED (-12) +#define TINYEXR_ERROR_LAYER_NOT_FOUND (-13) // @note { OpenEXR file format: http://www.openexr.com/openexrfilelayout.pdf } @@ -149,9 +177,13 @@ extern "C" { typedef struct _EXRVersion { int version; // this must be 2 - int tiled; // tile format image + // tile format image; + // not zero for only a single-part "normal" tiled file (according to spec.) + int tiled; int long_name; // long name attribute - int non_image; // deep image(EXR 2.0) + // deep image(EXR 2.0); + // for a multi-part file, indicates that at least one part is of type deep* (according to spec.) + int non_image; int multipart; // multi-part(EXR 2.0) } EXRVersion; @@ -184,11 +216,18 @@ typedef struct _EXRTile { unsigned char **images; // image[channels][pixels] } EXRTile; +typedef struct _EXRBox2i { + int min_x; + int min_y; + int max_x; + int max_y; +} EXRBox2i; + typedef struct _EXRHeader { float pixel_aspect_ratio; int line_order; - int data_window[4]; - int display_window[4]; + EXRBox2i data_window; + EXRBox2i display_window; float screen_window_center[2]; float screen_window_width; @@ -202,6 +241,8 @@ typedef struct _EXRHeader { int tile_rounding_mode; int long_name; + // for a single-part file, agree with the version field bit 11 + // for a multi-part file, it is consistent with the type of part int non_image; int multipart; unsigned int header_len; @@ -224,7 +265,11 @@ typedef struct _EXRHeader { // ParseEXRHeaderFrom(Meomory|File), then users // can edit it(only valid for HALF pixel type // channel) - + // name attribute required for multipart files; + // must be unique and non empty (according to spec.); + // use EXRSetNameAttr for setting value; + // max 255 character allowed - excluding terminating zero + char name[256]; } EXRHeader; typedef struct _EXRMultiPartHeader { @@ -236,6 +281,10 @@ typedef struct _EXRMultiPartHeader { typedef struct _EXRImage { EXRTile *tiles; // Tiled pixel data. The application must reconstruct image // from tiles manually. NULL if scanline format. + struct _EXRImage* next_level; // NULL if scanline format or image is the last level. + int level_x; // x level index + int level_y; // y level index + unsigned char **images; // image[channels][pixels]. NULL if tiled format. int width; @@ -263,7 +312,7 @@ typedef struct _DeepImage { int pad0; } DeepImage; -// @deprecated { to be removed. } +// @deprecated { For backward compatibility. Not recommended to use. } // Loads single-frame OpenEXR image. Assume EXR image contains A(single channel // alpha) or RGB(A) channels. // Application must free image data as returned by `out_rgba` @@ -273,6 +322,37 @@ typedef struct _DeepImage { extern int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, const char **err); +// Loads single-frame OpenEXR image by specifying layer name. Assume EXR image +// contains A(single channel alpha) or RGB(A) channels. Application must free +// image data as returned by `out_rgba` Result image format is: float x RGBA x +// width x hight Returns negative value and may set error string in `err` when +// there's an error When the specified layer name is not found in the EXR file, +// the function will return `TINYEXR_ERROR_LAYER_NOT_FOUND`. +extern int LoadEXRWithLayer(float **out_rgba, int *width, int *height, + const char *filename, const char *layer_name, + const char **err); + +// +// Get layer infos from EXR file. +// +// @param[out] layer_names List of layer names. Application must free memory +// after using this. +// @param[out] num_layers The number of layers +// @param[out] err Error string(will be filled when the function returns error +// code). Free it using FreeEXRErrorMessage after using this value. +// +// @return TINYEXR_SUCCEES upon success. +// +extern int EXRLayers(const char *filename, const char **layer_names[], + int *num_layers, const char **err); + +// @deprecated { to be removed. } +// Simple wrapper API for ParseEXRHeaderFromFile. +// checking given file is a EXR file(by just look up header) +// @return TINYEXR_SUCCEES for EXR image, TINYEXR_ERROR_INVALID_HEADER for +// others +extern int IsEXR(const char *filename); + // @deprecated { to be removed. } // Saves single-frame OpenEXR image. Assume EXR image contains RGB(A) channels. // components must be 1(Grayscale), 3(RGB) or 4(RGBA). @@ -288,19 +368,25 @@ extern int SaveEXR(const float *data, const int width, const int height, const int components, const int save_as_fp16, const char *filename, const char **err); +// Returns the number of resolution levels of the image (including the base) +extern int EXRNumLevels(const EXRImage* exr_image); + // Initialize EXRHeader struct extern void InitEXRHeader(EXRHeader *exr_header); +// Set name attribute of EXRHeader struct (it makes a copy) +extern void EXRSetNameAttr(EXRHeader *exr_header, const char* name); + // Initialize EXRImage struct extern void InitEXRImage(EXRImage *exr_image); -// Free's internal data of EXRHeader struct +// Frees internal data of EXRHeader struct extern int FreeEXRHeader(EXRHeader *exr_header); -// Free's internal data of EXRImage struct +// Frees internal data of EXRImage struct extern int FreeEXRImage(EXRImage *exr_image); -// Free's error message +// Frees error message extern void FreeEXRErrorMessage(const char *msg); // Parse EXR version header of a file. @@ -414,6 +500,30 @@ extern size_t SaveEXRImageToMemory(const EXRImage *image, const EXRHeader *exr_header, unsigned char **memory, const char **err); +// Saves multi-channel, multi-frame OpenEXR image to a memory. +// Image is compressed using EXRImage.compression value. +// File global attributes (eg. display_window) must be set in the first header. +// Returns negative value and may set error string in `err` when there's an +// error +// When there was an error message, Application must free `err` with +// FreeEXRErrorMessage() +extern int SaveEXRMultipartImageToFile(const EXRImage *images, + const EXRHeader **exr_headers, + unsigned int num_parts, + const char *filename, const char **err); + +// Saves multi-channel, multi-frame OpenEXR image to a memory. +// Image is compressed using EXRImage.compression value. +// File global attributes (eg. display_window) must be set in the first header. +// Return the number of bytes if success. +// Return zero and will set error string in `err` when there's an +// error. +// When there was an error message, Application must free `err` with +// FreeEXRErrorMessage() +extern size_t SaveEXRMultipartImageToMemory(const EXRImage *images, + const EXRHeader **exr_headers, + unsigned int num_parts, + unsigned char **memory, const char **err); // Loads single-frame OpenEXR deep image. // Application must free memory of variables in DeepImage(image, offset_table) // Returns negative value and may set error string in `err` when there's an @@ -455,31 +565,54 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, #endif // TINYEXR_H_ #ifdef TINYEXR_IMPLEMENTATION -#ifndef TINYEXR_IMPLEMENTATION_DEIFNED -#define TINYEXR_IMPLEMENTATION_DEIFNED +#ifndef TINYEXR_IMPLEMENTATION_DEFINED +#define TINYEXR_IMPLEMENTATION_DEFINED + +#ifdef _WIN32 + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include <windows.h> // for UTF-8 + +#endif #include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> -#include <iostream> #include <sstream> +// #include <iostream> // debug + #include <limits> #include <string> #include <vector> +#include <set> -#if __cplusplus > 199711L +// https://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support +#if __cplusplus > 199711L || (defined(_MSC_VER) && _MSC_VER >= 1900) +#define TINYEXR_HAS_CXX11 (1) // C++11 #include <cstdint> + +#if TINYEXR_USE_THREAD +#include <atomic> +#include <thread> +#endif + #endif // __cplusplus > 199711L -#ifdef _OPENMP +#if TINYEXR_USE_OPENMP #include <omp.h> #endif #if TINYEXR_USE_MINIZ +#include <miniz.h> #else // Issue #46. Please include your own zlib-compatible API header before // including `tinyexr.h` @@ -487,13 +620,19 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, #endif #if TINYEXR_USE_ZFP + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Weverything" +#endif + #include "zfp.h" + +#ifdef __clang__ +#pragma clang diagnostic pop #endif -#if __cplusplus > 199711L -// C++11 -#include <cstdint> -#endif // __cplusplus > 199711L +#endif namespace tinyexr { @@ -515,6461 +654,6 @@ typedef long long tinyexr_int64; #endif #endif -#if TINYEXR_USE_MINIZ - -namespace miniz { - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wc++11-long-long" -#pragma clang diagnostic ignored "-Wold-style-cast" -#pragma clang diagnostic ignored "-Wpadded" -#pragma clang diagnostic ignored "-Wsign-conversion" -#pragma clang diagnostic ignored "-Wc++11-extensions" -#pragma clang diagnostic ignored "-Wconversion" -#pragma clang diagnostic ignored "-Wunused-function" -#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" -#pragma clang diagnostic ignored "-Wundef" - -#if __has_warning("-Wcomma") -#pragma clang diagnostic ignored "-Wcomma" -#endif - -#if __has_warning("-Wmacro-redefined") -#pragma clang diagnostic ignored "-Wmacro-redefined" -#endif - -#if __has_warning("-Wcast-qual") -#pragma clang diagnostic ignored "-Wcast-qual" -#endif - -#if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" -#endif - -#if __has_warning("-Wtautological-constant-compare") -#pragma clang diagnostic ignored "-Wtautological-constant-compare" -#endif - -#endif - -/* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP - reading/writing/appending, PNG writing - See "unlicense" statement at the end of this file. - Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013 - Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: - http://www.ietf.org/rfc/rfc1951.txt - - Most API's defined in miniz.c are optional. For example, to disable the - archive related functions just define - MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO - (see the list below for more macros). - - * Change History - 10/13/13 v1.15 r4 - Interim bugfix release while I work on the next major - release with Zip64 support (almost there!): - - Critical fix for the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug - (thanks kahmyong.moon@hp.com) which could cause locate files to not find - files. This bug - would only have occured in earlier versions if you explicitly used this - flag, OR if you used mz_zip_extract_archive_file_to_heap() or - mz_zip_add_mem_to_archive_file_in_place() - (which used this flag). If you can't switch to v1.15 but want to fix - this bug, just remove the uses of this flag from both helper funcs (and of - course don't use the flag). - - Bugfix in mz_zip_reader_extract_to_mem_no_alloc() from kymoon when - pUser_read_buf is not NULL and compressed size is > uncompressed size - - Fixing mz_zip_reader_extract_*() funcs so they don't try to extract - compressed data from directory entries, to account for weird zipfiles which - contain zero-size compressed data on dir entries. - Hopefully this fix won't cause any issues on weird zip archives, - because it assumes the low 16-bits of zip external attributes are DOS - attributes (which I believe they always are in practice). - - Fixing mz_zip_reader_is_file_a_directory() so it doesn't check the - internal attributes, just the filename and external attributes - - mz_zip_reader_init_file() - missing MZ_FCLOSE() call if the seek failed - - Added cmake support for Linux builds which builds all the examples, - tested with clang v3.3 and gcc v4.6. - - Clang fix for tdefl_write_image_to_png_file_in_memory() from toffaletti - - Merged MZ_FORCEINLINE fix from hdeanclark - - Fix <time.h> include before config #ifdef, thanks emil.brink - - Added tdefl_write_image_to_png_file_in_memory_ex(): supports Y flipping - (super useful for OpenGL apps), and explicit control over the compression - level (so you can - set it to 1 for real-time compression). - - Merged in some compiler fixes from paulharris's github repro. - - Retested this build under Windows (VS 2010, including static analysis), - tcc 0.9.26, gcc v4.6 and clang v3.3. - - Added example6.c, which dumps an image of the mandelbrot set to a PNG - file. - - Modified example2 to help test the - MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY flag more. - - In r3: Bugfix to mz_zip_writer_add_file() found during merge: Fix - possible src file fclose() leak if alignment bytes+local header file write - faiiled - - In r4: Minor bugfix to mz_zip_writer_add_from_zip_reader(): - Was pushing the wrong central dir header offset, appears harmless in this - release, but it became a problem in the zip64 branch - 5/20/12 v1.14 - MinGW32/64 GCC 4.6.1 compiler fixes: added MZ_FORCEINLINE, - #include <time.h> (thanks fermtect). - 5/19/12 v1.13 - From jason@cornsyrup.org and kelwert@mtu.edu - Fix - mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bit. - - Temporarily/locally slammed in "typedef unsigned long mz_ulong" and - re-ran a randomized regression test on ~500k files. - - Eliminated a bunch of warnings when compiling with GCC 32-bit/64. - - Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze - (static analysis) option and fixed all warnings (except for the silly - "Use of the comma-operator in a tested expression.." analysis warning, - which I purposely use to work around a MSVC compiler warning). - - Created 32-bit and 64-bit Codeblocks projects/workspace. Built and - tested Linux executables. The codeblocks workspace is compatible with - Linux+Win32/x64. - - Added miniz_tester solution/project, which is a useful little app - derived from LZHAM's tester app that I use as part of the regression test. - - Ran miniz.c and tinfl.c through another series of regression testing on - ~500,000 files and archives. - - Modified example5.c so it purposely disables a bunch of high-level - functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the - MINIZ_NO_STDIO bug report.) - - Fix ftell() usage in examples so they exit with an error on files which - are too large (a limitation of the examples, not miniz itself). - 4/12/12 v1.12 - More comments, added low-level example5.c, fixed a couple - minor level_and_flags issues in the archive API's. - level_and_flags can now be set to MZ_DEFAULT_COMPRESSION. Thanks to Bruce - Dawson <bruced@valvesoftware.com> for the feedback/bug report. - 5/28/11 v1.11 - Added statement from unlicense.org - 5/27/11 v1.10 - Substantial compressor optimizations: - - Level 1 is now ~4x faster than before. The L1 compressor's throughput - now varies between 70-110MB/sec. on a - - Core i7 (actual throughput varies depending on the type of data, and x64 - vs. x86). - - Improved baseline L2-L9 compression perf. Also, greatly improved - compression perf. issues on some file types. - - Refactored the compression code for better readability and - maintainability. - - Added level 10 compression level (L10 has slightly better ratio than - level 9, but could have a potentially large - drop in throughput on some files). - 5/15/11 v1.09 - Initial stable release. - - * Low-level Deflate/Inflate implementation notes: - - Compression: Use the "tdefl" API's. The compressor supports raw, static, - and dynamic blocks, lazy or - greedy parsing, match length filtering, RLE-only, and Huffman-only streams. - It performs and compresses - approximately as well as zlib. - - Decompression: Use the "tinfl" API's. The entire decompressor is - implemented as a single function - coroutine: see tinfl_decompress(). It supports decompression into a 32KB - (or larger power of 2) wrapping buffer, or into a memory - block large enough to hold the entire file. - - The low-level tdefl/tinfl API's do not make any use of dynamic memory - allocation. - - * zlib-style API notes: - - miniz.c implements a fairly large subset of zlib. There's enough - functionality present for it to be a drop-in - zlib replacement in many apps: - The z_stream struct, optional memory allocation callbacks - deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound - inflateInit/inflateInit2/inflate/inflateEnd - compress, compress2, compressBound, uncompress - CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly - routines. - Supports raw deflate streams or standard zlib streams with adler-32 - checking. - - Limitations: - The callback API's are not implemented yet. No support for gzip headers or - zlib static dictionaries. - I've tried to closely emulate zlib's various flavors of stream flushing - and return status codes, but - there are no guarantees that miniz.c pulls this off perfectly. - - * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, - originally written by - Alex Evans. Supports 1-4 bytes/pixel images. - - * ZIP archive API notes: - - The ZIP archive API's where designed with simplicity and efficiency in - mind, with just enough abstraction to - get the job done with minimal fuss. There are simple API's to retrieve file - information, read files from - existing archives, create new archives, append new files to existing - archives, or clone archive data from - one archive to another. It supports archives located in memory or the heap, - on disk (using stdio.h), - or you can specify custom file read/write callbacks. - - - Archive reading: Just call this function to read a single file from a - disk archive: - - void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const - char *pArchive_name, - size_t *pSize, mz_uint zip_flags); - - For more complex cases, use the "mz_zip_reader" functions. Upon opening an - archive, the entire central - directory is located and read as-is into memory, and subsequent file access - only occurs when reading individual files. - - - Archives file scanning: The simple way is to use this function to scan a - loaded archive for a specific file: - - int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, - const char *pComment, mz_uint flags); - - The locate operation can optionally check file comments too, which (as one - example) can be used to identify - multiple versions of the same file in an archive. This function uses a - simple linear search through the central - directory, so it's not very fast. - - Alternately, you can iterate through all the files in an archive (using - mz_zip_reader_get_num_files()) and - retrieve detailed info on each file by calling mz_zip_reader_file_stat(). - - - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer - immediately writes compressed file data - to disk and builds an exact image of the central directory in memory. The - central directory image is written - all at once at the end of the archive file when the archive is finalized. - - The archive writer can optionally align each file's local header and file - data to any power of 2 alignment, - which can be useful when the archive will be read from optical media. Also, - the writer supports placing - arbitrary data blobs at the very beginning of ZIP archives. Archives - written using either feature are still - readable by any ZIP tool. - - - Archive appending: The simple way to add a single file to an archive is - to call this function: - - mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, - const char *pArchive_name, - const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 - comment_size, mz_uint level_and_flags); - - The archive will be created if it doesn't already exist, otherwise it'll be - appended to. - Note the appending is done in-place and is not an atomic operation, so if - something goes wrong - during the operation it's possible the archive could be left without a - central directory (although the local - file headers and file data will be fine, so the archive will be - recoverable). - - For more complex archive modification scenarios: - 1. The safest way is to use a mz_zip_reader to read the existing archive, - cloning only those bits you want to - preserve into a new archive using using the - mz_zip_writer_add_from_zip_reader() function (which compiles the - compressed file data as-is). When you're done, delete the old archive and - rename the newly written archive, and - you're done. This is safe but requires a bunch of temporary disk space or - heap memory. - - 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using - mz_zip_writer_init_from_reader(), - append new files as needed, then finalize the archive which will write an - updated central directory to the - original archive. (This is basically what - mz_zip_add_mem_to_archive_file_in_place() does.) There's a - possibility that the archive's central directory could be lost with this - method if anything goes wrong, though. - - - ZIP archive support limitations: - No zip64 or spanning support. Extraction functions can only handle - unencrypted, stored or deflated files. - Requires streams capable of seeking. - - * This is a header file library, like stb_image.c. To get only a header file, - either cut and paste the - below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then - include miniz.c from it. - - * Important: For best perf. be sure to customize the below macros for your - target platform: - #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 - #define MINIZ_LITTLE_ENDIAN 1 - #define MINIZ_HAS_64BIT_REGISTERS 1 - - * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before - including miniz.c to ensure miniz - uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be - able to process large files - (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). -*/ - -#ifndef MINIZ_HEADER_INCLUDED -#define MINIZ_HEADER_INCLUDED - -//#include <stdlib.h> - -// Defines to completely disable specific portions of miniz.c: -// If all macros here are defined the only functionality remaining will be -// CRC-32, adler-32, tinfl, and tdefl. - -// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on -// stdio for file I/O. -//#define MINIZ_NO_STDIO - -// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able -// to get the current time, or -// get/set file times, and the C run-time funcs that get/set times won't be -// called. -// The current downside is the times written to your archives will be from 1979. -#define MINIZ_NO_TIME - -// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. -#define MINIZ_NO_ARCHIVE_APIS - -// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive -// API's. -//#define MINIZ_NO_ARCHIVE_WRITING_APIS - -// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression -// API's. -//#define MINIZ_NO_ZLIB_APIS - -// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent -// conflicts against stock zlib. -//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES - -// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. -// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom -// user alloc/free/realloc -// callbacks to the zlib and archive API's, and a few stand-alone helper API's -// which don't provide custom user -// functions (such as tdefl_compress_mem_to_heap() and -// tinfl_decompress_mem_to_heap()) won't work. -//#define MINIZ_NO_MALLOC - -#if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) -// TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc -// on Linux -#define MINIZ_NO_TIME -#endif - -#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) -//#include <time.h> -#endif - -#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \ - defined(__i386) || defined(__i486__) || defined(__i486) || \ - defined(i386) || defined(__ia64__) || defined(__x86_64__) -// MINIZ_X86_OR_X64_CPU is only used to help set the below macros. -#define MINIZ_X86_OR_X64_CPU 1 -#endif - -#if defined(__sparcv9) -// Big endian -#else -#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU -// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. -#define MINIZ_LITTLE_ENDIAN 1 -#endif -#endif - -#if MINIZ_X86_OR_X64_CPU -// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient -// integer loads and stores from unaligned addresses. -//#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 -#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES \ - 0 // disable to suppress compiler warnings -#endif - -#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || \ - defined(_LP64) || defined(__LP64__) || defined(__ia64__) || \ - defined(__x86_64__) -// Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are -// reasonably fast (and don't involve compiler generated calls to helper -// functions). -#define MINIZ_HAS_64BIT_REGISTERS 1 -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -// ------------------- zlib-style API Definitions. - -// For more compatibility with zlib, miniz.c uses unsigned long for some -// parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! -typedef unsigned long mz_ulong; - -// mz_free() internally uses the MZ_FREE() macro (which by default calls free() -// unless you've modified the MZ_MALLOC macro) to release a block allocated from -// the heap. -void mz_free(void *p); - -#define MZ_ADLER32_INIT (1) -// mz_adler32() returns the initial adler-32 value to use when called with -// ptr==NULL. -mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len); - -#define MZ_CRC32_INIT (0) -// mz_crc32() returns the initial CRC-32 value to use when called with -// ptr==NULL. -mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len); - -// Compression strategies. -enum { - MZ_DEFAULT_STRATEGY = 0, - MZ_FILTERED = 1, - MZ_HUFFMAN_ONLY = 2, - MZ_RLE = 3, - MZ_FIXED = 4 -}; - -// Method -#define MZ_DEFLATED 8 - -#ifndef MINIZ_NO_ZLIB_APIS - -// Heap allocation callbacks. -// Note that mz_alloc_func parameter types purpsosely differ from zlib's: -// items/size is size_t, not unsigned long. -typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); -typedef void (*mz_free_func)(void *opaque, void *address); -typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, - size_t size); - -#define MZ_VERSION "9.1.15" -#define MZ_VERNUM 0x91F0 -#define MZ_VER_MAJOR 9 -#define MZ_VER_MINOR 1 -#define MZ_VER_REVISION 15 -#define MZ_VER_SUBREVISION 0 - -// Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The -// other values are for advanced use (refer to the zlib docs). -enum { - MZ_NO_FLUSH = 0, - MZ_PARTIAL_FLUSH = 1, - MZ_SYNC_FLUSH = 2, - MZ_FULL_FLUSH = 3, - MZ_FINISH = 4, - MZ_BLOCK = 5 -}; - -// Return status codes. MZ_PARAM_ERROR is non-standard. -enum { - MZ_OK = 0, - MZ_STREAM_END = 1, - MZ_NEED_DICT = 2, - MZ_ERRNO = -1, - MZ_STREAM_ERROR = -2, - MZ_DATA_ERROR = -3, - MZ_MEM_ERROR = -4, - MZ_BUF_ERROR = -5, - MZ_VERSION_ERROR = -6, - MZ_PARAM_ERROR = -10000 -}; - -// Compression levels: 0-9 are the standard zlib-style levels, 10 is best -// possible compression (not zlib compatible, and may be very slow), -// MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. -enum { - MZ_NO_COMPRESSION = 0, - MZ_BEST_SPEED = 1, - MZ_BEST_COMPRESSION = 9, - MZ_UBER_COMPRESSION = 10, - MZ_DEFAULT_LEVEL = 6, - MZ_DEFAULT_COMPRESSION = -1 -}; - -// Window bits -#define MZ_DEFAULT_WINDOW_BITS 15 - -struct mz_internal_state; - -// Compression/decompression stream struct. -typedef struct mz_stream_s { - const unsigned char *next_in; // pointer to next byte to read - unsigned int avail_in; // number of bytes available at next_in - mz_ulong total_in; // total number of bytes consumed so far - - unsigned char *next_out; // pointer to next byte to write - unsigned int avail_out; // number of bytes that can be written to next_out - mz_ulong total_out; // total number of bytes produced so far - - char *msg; // error msg (unused) - struct mz_internal_state *state; // internal state, allocated by zalloc/zfree - - mz_alloc_func - zalloc; // optional heap allocation function (defaults to malloc) - mz_free_func zfree; // optional heap free function (defaults to free) - void *opaque; // heap alloc function user pointer - - int data_type; // data_type (unused) - mz_ulong adler; // adler32 of the source or uncompressed data - mz_ulong reserved; // not used -} mz_stream; - -typedef mz_stream *mz_streamp; - -// Returns the version string of miniz.c. -const char *mz_version(void); - -// mz_deflateInit() initializes a compressor with default options: -// Parameters: -// pStream must point to an initialized mz_stream struct. -// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. -// level 1 enables a specially optimized compression function that's been -// optimized purely for performance, not ratio. -// (This special func. is currently only enabled when -// MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) -// Return values: -// MZ_OK on success. -// MZ_STREAM_ERROR if the stream is bogus. -// MZ_PARAM_ERROR if the input parameters are bogus. -// MZ_MEM_ERROR on out of memory. -int mz_deflateInit(mz_streamp pStream, int level); - -// mz_deflateInit2() is like mz_deflate(), except with more control: -// Additional parameters: -// method must be MZ_DEFLATED -// window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with -// zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no -// header or footer) -// mem_level must be between [1, 9] (it's checked but ignored by miniz.c) -int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, - int mem_level, int strategy); - -// Quickly resets a compressor without having to reallocate anything. Same as -// calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). -int mz_deflateReset(mz_streamp pStream); - -// mz_deflate() compresses the input to output, consuming as much of the input -// and producing as much output as possible. -// Parameters: -// pStream is the stream to read from and write to. You must initialize/update -// the next_in, avail_in, next_out, and avail_out members. -// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or -// MZ_FINISH. -// Return values: -// MZ_OK on success (when flushing, or if more input is needed but not -// available, and/or there's more output to be written but the output buffer -// is full). -// MZ_STREAM_END if all input has been consumed and all output bytes have been -// written. Don't call mz_deflate() on the stream anymore. -// MZ_STREAM_ERROR if the stream is bogus. -// MZ_PARAM_ERROR if one of the parameters is invalid. -// MZ_BUF_ERROR if no forward progress is possible because the input and/or -// output buffers are empty. (Fill up the input buffer or free up some output -// space and try again.) -int mz_deflate(mz_streamp pStream, int flush); - -// mz_deflateEnd() deinitializes a compressor: -// Return values: -// MZ_OK on success. -// MZ_STREAM_ERROR if the stream is bogus. -int mz_deflateEnd(mz_streamp pStream); - -// mz_deflateBound() returns a (very) conservative upper bound on the amount of -// data that could be generated by deflate(), assuming flush is set to only -// MZ_NO_FLUSH or MZ_FINISH. -mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); - -// Single-call compression functions mz_compress() and mz_compress2(): -// Returns MZ_OK on success, or one of the error codes from mz_deflate() on -// failure. -int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len); -int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len, int level); - -// mz_compressBound() returns a (very) conservative upper bound on the amount of -// data that could be generated by calling mz_compress(). -mz_ulong mz_compressBound(mz_ulong source_len); - -// Initializes a decompressor. -int mz_inflateInit(mz_streamp pStream); - -// mz_inflateInit2() is like mz_inflateInit() with an additional option that -// controls the window size and whether or not the stream has been wrapped with -// a zlib header/footer: -// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -// -MZ_DEFAULT_WINDOW_BITS (raw deflate). -int mz_inflateInit2(mz_streamp pStream, int window_bits); - -// Decompresses the input stream to the output, consuming only as much of the -// input as needed, and writing as much to the output as possible. -// Parameters: -// pStream is the stream to read from and write to. You must initialize/update -// the next_in, avail_in, next_out, and avail_out members. -// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. -// On the first call, if flush is MZ_FINISH it's assumed the input and output -// buffers are both sized large enough to decompress the entire stream in a -// single call (this is slightly faster). -// MZ_FINISH implies that there are no more source bytes available beside -// what's already in the input buffer, and that the output buffer is large -// enough to hold the rest of the decompressed data. -// Return values: -// MZ_OK on success. Either more input is needed but not available, and/or -// there's more output to be written but the output buffer is full. -// MZ_STREAM_END if all needed input has been consumed and all output bytes -// have been written. For zlib streams, the adler-32 of the decompressed data -// has also been verified. -// MZ_STREAM_ERROR if the stream is bogus. -// MZ_DATA_ERROR if the deflate stream is invalid. -// MZ_PARAM_ERROR if one of the parameters is invalid. -// MZ_BUF_ERROR if no forward progress is possible because the input buffer is -// empty but the inflater needs more input to continue, or if the output -// buffer is not large enough. Call mz_inflate() again -// with more input data, or with more room in the output buffer (except when -// using single call decompression, described above). -int mz_inflate(mz_streamp pStream, int flush); - -// Deinitializes a decompressor. -int mz_inflateEnd(mz_streamp pStream); - -// Single-call decompression. -// Returns MZ_OK on success, or one of the error codes from mz_inflate() on -// failure. -int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len); - -// Returns a string description of the specified error code, or NULL if the -// error code is invalid. -const char *mz_error(int err); - -// Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used -// as a drop-in replacement for the subset of zlib that miniz.c supports. -// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you -// use zlib in the same project. -#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES -typedef unsigned char Byte; -typedef unsigned int uInt; -typedef mz_ulong uLong; -typedef Byte Bytef; -typedef uInt uIntf; -typedef char charf; -typedef int intf; -typedef void *voidpf; -typedef uLong uLongf; -typedef void *voidp; -typedef void *const voidpc; -#define Z_NULL 0 -#define Z_NO_FLUSH MZ_NO_FLUSH -#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH -#define Z_SYNC_FLUSH MZ_SYNC_FLUSH -#define Z_FULL_FLUSH MZ_FULL_FLUSH -#define Z_FINISH MZ_FINISH -#define Z_BLOCK MZ_BLOCK -#define Z_OK MZ_OK -#define Z_STREAM_END MZ_STREAM_END -#define Z_NEED_DICT MZ_NEED_DICT -#define Z_ERRNO MZ_ERRNO -#define Z_STREAM_ERROR MZ_STREAM_ERROR -#define Z_DATA_ERROR MZ_DATA_ERROR -#define Z_MEM_ERROR MZ_MEM_ERROR -#define Z_BUF_ERROR MZ_BUF_ERROR -#define Z_VERSION_ERROR MZ_VERSION_ERROR -#define Z_PARAM_ERROR MZ_PARAM_ERROR -#define Z_NO_COMPRESSION MZ_NO_COMPRESSION -#define Z_BEST_SPEED MZ_BEST_SPEED -#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION -#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION -#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY -#define Z_FILTERED MZ_FILTERED -#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY -#define Z_RLE MZ_RLE -#define Z_FIXED MZ_FIXED -#define Z_DEFLATED MZ_DEFLATED -#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS -#define alloc_func mz_alloc_func -#define free_func mz_free_func -#define internal_state mz_internal_state -#define z_stream mz_stream -#define deflateInit mz_deflateInit -#define deflateInit2 mz_deflateInit2 -#define deflateReset mz_deflateReset -#define deflate mz_deflate -#define deflateEnd mz_deflateEnd -#define deflateBound mz_deflateBound -#define compress mz_compress -#define compress2 mz_compress2 -#define compressBound mz_compressBound -#define inflateInit mz_inflateInit -#define inflateInit2 mz_inflateInit2 -#define inflate mz_inflate -#define inflateEnd mz_inflateEnd -#define uncompress mz_uncompress -#define crc32 mz_crc32 -#define adler32 mz_adler32 -#define MAX_WBITS 15 -#define MAX_MEM_LEVEL 9 -#define zError mz_error -#define ZLIB_VERSION MZ_VERSION -#define ZLIB_VERNUM MZ_VERNUM -#define ZLIB_VER_MAJOR MZ_VER_MAJOR -#define ZLIB_VER_MINOR MZ_VER_MINOR -#define ZLIB_VER_REVISION MZ_VER_REVISION -#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION -#define zlibVersion mz_version -#define zlib_version mz_version() -#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES - -#endif // MINIZ_NO_ZLIB_APIS - -// ------------------- Types and macros - -typedef unsigned char mz_uint8; -typedef signed short mz_int16; -typedef unsigned short mz_uint16; -typedef unsigned int mz_uint32; -typedef unsigned int mz_uint; -typedef long long mz_int64; -typedef unsigned long long mz_uint64; -typedef int mz_bool; - -#define MZ_FALSE (0) -#define MZ_TRUE (1) - -// An attempt to work around MSVC's spammy "warning C4127: conditional -// expression is constant" message. -#ifdef _MSC_VER -#define MZ_MACRO_END while (0, 0) -#else -#define MZ_MACRO_END while (0) -#endif - -// ------------------- ZIP archive reading/writing - -#ifndef MINIZ_NO_ARCHIVE_APIS - -enum { - MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, - MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260, - MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256 -}; - -typedef struct { - mz_uint32 m_file_index; - mz_uint32 m_central_dir_ofs; - mz_uint16 m_version_made_by; - mz_uint16 m_version_needed; - mz_uint16 m_bit_flag; - mz_uint16 m_method; -#ifndef MINIZ_NO_TIME - time_t m_time; -#endif - mz_uint32 m_crc32; - mz_uint64 m_comp_size; - mz_uint64 m_uncomp_size; - mz_uint16 m_internal_attr; - mz_uint32 m_external_attr; - mz_uint64 m_local_header_ofs; - mz_uint32 m_comment_size; - char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; - char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; -} mz_zip_archive_file_stat; - -typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, - void *pBuf, size_t n); -typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, - const void *pBuf, size_t n); - -struct mz_zip_internal_state_tag; -typedef struct mz_zip_internal_state_tag mz_zip_internal_state; - -typedef enum { - MZ_ZIP_MODE_INVALID = 0, - MZ_ZIP_MODE_READING = 1, - MZ_ZIP_MODE_WRITING = 2, - MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 -} mz_zip_mode; - -typedef struct mz_zip_archive_tag { - mz_uint64 m_archive_size; - mz_uint64 m_central_directory_file_ofs; - mz_uint m_total_files; - mz_zip_mode m_zip_mode; - - mz_uint m_file_offset_alignment; - - mz_alloc_func m_pAlloc; - mz_free_func m_pFree; - mz_realloc_func m_pRealloc; - void *m_pAlloc_opaque; - - mz_file_read_func m_pRead; - mz_file_write_func m_pWrite; - void *m_pIO_opaque; - - mz_zip_internal_state *m_pState; - -} mz_zip_archive; - -typedef enum { - MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, - MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, - MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, - MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800 -} mz_zip_flags; - -// ZIP archive reading - -// Inits a ZIP archive reader. -// These functions read and validate the archive's central directory. -mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, - mz_uint32 flags); -mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, - size_t size, mz_uint32 flags); - -#ifndef MINIZ_NO_STDIO -mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, - mz_uint32 flags); -#endif - -// Returns the total number of files in the archive. -mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip); - -// Returns detailed information about an archive file entry. -mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, - mz_zip_archive_file_stat *pStat); - -// Determines if an archive file entry is a directory entry. -mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, - mz_uint file_index); -mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, - mz_uint file_index); - -// Retrieves the filename of an archive file entry. -// Returns the number of bytes written to pFilename, or if filename_buf_size is -// 0 this function returns the number of bytes needed to fully store the -// filename. -mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, - char *pFilename, mz_uint filename_buf_size); - -// Attempts to locates a file in the archive's central directory. -// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH -// Returns -1 if the file cannot be found. -int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, - const char *pComment, mz_uint flags); - -// Extracts a archive file to a memory buffer using no memory allocation. -mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, - mz_uint file_index, void *pBuf, - size_t buf_size, mz_uint flags, - void *pUser_read_buf, - size_t user_read_buf_size); -mz_bool mz_zip_reader_extract_file_to_mem_no_alloc( - mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, - mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); - -// Extracts a archive file to a memory buffer. -mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, - void *pBuf, size_t buf_size, - mz_uint flags); -mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, - const char *pFilename, void *pBuf, - size_t buf_size, mz_uint flags); - -// Extracts a archive file to a dynamically allocated heap buffer. -void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, - size_t *pSize, mz_uint flags); -void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, - const char *pFilename, size_t *pSize, - mz_uint flags); - -// Extracts a archive file using a callback function to output the file's data. -mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, - mz_uint file_index, - mz_file_write_func pCallback, - void *pOpaque, mz_uint flags); -mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, - const char *pFilename, - mz_file_write_func pCallback, - void *pOpaque, mz_uint flags); - -#ifndef MINIZ_NO_STDIO -// Extracts a archive file to a disk file and sets its last accessed and -// modified times. -// This function only extracts files, not archive directory records. -mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, - const char *pDst_filename, mz_uint flags); -mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, - const char *pArchive_filename, - const char *pDst_filename, - mz_uint flags); -#endif - -// Ends archive reading, freeing all allocations, and closing the input archive -// file if mz_zip_reader_init_file() was used. -mz_bool mz_zip_reader_end(mz_zip_archive *pZip); - -// ZIP archive writing - -#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS - -// Inits a ZIP archive writer. -mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size); -mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, - size_t size_to_reserve_at_beginning, - size_t initial_allocation_size); - -#ifndef MINIZ_NO_STDIO -mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, - mz_uint64 size_to_reserve_at_beginning); -#endif - -// Converts a ZIP archive reader object into a writer object, to allow efficient -// in-place file appends to occur on an existing archive. -// For archives opened using mz_zip_reader_init_file, pFilename must be the -// archive's filename so it can be reopened for writing. If the file can't be -// reopened, mz_zip_reader_end() will be called. -// For archives opened using mz_zip_reader_init_mem, the memory block must be -// growable using the realloc callback (which defaults to realloc unless you've -// overridden it). -// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's -// user provided m_pWrite function cannot be NULL. -// Note: In-place archive modification is not recommended unless you know what -// you're doing, because if execution stops or something goes wrong before -// the archive is finalized the file's central directory will be hosed. -mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, - const char *pFilename); - -// Adds the contents of a memory buffer to an archive. These functions record -// the current local time into the archive. -// To add a directory entry, call this method with an archive name ending in a -// forwardslash with empty buffer. -// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, -// MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or -// just set to MZ_DEFAULT_COMPRESSION. -mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, - const void *pBuf, size_t buf_size, - mz_uint level_and_flags); -mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, - const char *pArchive_name, const void *pBuf, - size_t buf_size, const void *pComment, - mz_uint16 comment_size, - mz_uint level_and_flags, mz_uint64 uncomp_size, - mz_uint32 uncomp_crc32); - -#ifndef MINIZ_NO_STDIO -// Adds the contents of a disk file to an archive. This function also records -// the disk file's modified time into the archive. -// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, -// MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or -// just set to MZ_DEFAULT_COMPRESSION. -mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, - const char *pSrc_filename, const void *pComment, - mz_uint16 comment_size, mz_uint level_and_flags); -#endif - -// Adds a file to an archive by fully cloning the data from another archive. -// This function fully clones the source file's compressed data (no -// recompression), along with its full filename, extra data, and comment fields. -mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, - mz_zip_archive *pSource_zip, - mz_uint file_index); - -// Finalizes the archive by writing the central directory records followed by -// the end of central directory record. -// After an archive is finalized, the only valid call on the mz_zip_archive -// struct is mz_zip_writer_end(). -// An archive must be manually finalized by calling this function for it to be -// valid. -mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); -mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, - size_t *pSize); - -// Ends archive writing, freeing all allocations, and closing the output file if -// mz_zip_writer_init_file() was used. -// Note for the archive to be valid, it must have been finalized before ending. -mz_bool mz_zip_writer_end(mz_zip_archive *pZip); - -// Misc. high-level helper functions: - -// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) -// appends a memory blob to a ZIP archive. -// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, -// MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or -// just set to MZ_DEFAULT_COMPRESSION. -mz_bool mz_zip_add_mem_to_archive_file_in_place( - const char *pZip_filename, const char *pArchive_name, const void *pBuf, - size_t buf_size, const void *pComment, mz_uint16 comment_size, - mz_uint level_and_flags); - -// Reads a single file from an archive into a heap block. -// Returns NULL on failure. -void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, - const char *pArchive_name, - size_t *pSize, mz_uint zip_flags); - -#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS - -#endif // #ifndef MINIZ_NO_ARCHIVE_APIS - -// ------------------- Low-level Decompression API Definitions - -// Decompression flags used by tinfl_decompress(). -// TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and -// ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the -// input is a raw deflate stream. -// TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available -// beyond the end of the supplied input buffer. If clear, the input buffer -// contains all remaining input. -// TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large -// enough to hold the entire decompressed stream. If clear, the output buffer is -// at least the size of the dictionary (typically 32KB). -// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the -// decompressed bytes. -enum { - TINFL_FLAG_PARSE_ZLIB_HEADER = 1, - TINFL_FLAG_HAS_MORE_INPUT = 2, - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, - TINFL_FLAG_COMPUTE_ADLER32 = 8 -}; - -// High level decompression functions: -// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block -// allocated via malloc(). -// On entry: -// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data -// to decompress. -// On return: -// Function returns a pointer to the decompressed data, or NULL on failure. -// *pOut_len will be set to the decompressed data's size, which could be larger -// than src_buf_len on uncompressible data. -// The caller must call mz_free() on the returned block when it's no longer -// needed. -void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, - size_t *pOut_len, int flags); - -// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block -// in memory. -// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes -// written on success. -#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) -size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, - const void *pSrc_buf, size_t src_buf_len, - int flags); - -// tinfl_decompress_mem_to_callback() decompresses a block in memory to an -// internal 32KB buffer, and a user provided callback function will be called to -// flush the buffer. -// Returns 1 on success or 0 on failure. -typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser); -int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, - tinfl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags); - -struct tinfl_decompressor_tag; -typedef struct tinfl_decompressor_tag tinfl_decompressor; - -// Max size of LZ dictionary. -#define TINFL_LZ_DICT_SIZE 32768 - -// Return status. -typedef enum { - TINFL_STATUS_BAD_PARAM = -3, - TINFL_STATUS_ADLER32_MISMATCH = -2, - TINFL_STATUS_FAILED = -1, - TINFL_STATUS_DONE = 0, - TINFL_STATUS_NEEDS_MORE_INPUT = 1, - TINFL_STATUS_HAS_MORE_OUTPUT = 2 -} tinfl_status; - -// Initializes the decompressor to its initial state. -#define tinfl_init(r) \ - do { \ - (r)->m_state = 0; \ - } \ - MZ_MACRO_END -#define tinfl_get_adler32(r) (r)->m_check_adler32 - -// Main low-level decompressor coroutine function. This is the only function -// actually needed for decompression. All the other functions are just -// high-level helpers for improved usability. -// This is a universal API, i.e. it can be used as a building block to build any -// desired higher level decompression API. In the limit case, it can be called -// once per every byte input or output. -tinfl_status tinfl_decompress(tinfl_decompressor *r, - const mz_uint8 *pIn_buf_next, - size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, - mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, - const mz_uint32 decomp_flags); - -// Internal/private bits follow. -enum { - TINFL_MAX_HUFF_TABLES = 3, - TINFL_MAX_HUFF_SYMBOLS_0 = 288, - TINFL_MAX_HUFF_SYMBOLS_1 = 32, - TINFL_MAX_HUFF_SYMBOLS_2 = 19, - TINFL_FAST_LOOKUP_BITS = 10, - TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS -}; - -typedef struct { - mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; - mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], - m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; -} tinfl_huff_table; - - -#ifndef MINIZ_HAS_64BIT_REGISTERS -# define MINIZ_HAS_64BIT_REGISTERS 0 -#endif - -#ifndef TINFL_USE_64BIT_BITBUF -# if MINIZ_HAS_64BIT_REGISTERS -# define TINFL_USE_64BIT_BITBUF 1 -# else -# define TINFL_USE_64BIT_BITBUF 0 -# endif -#endif - -#if TINFL_USE_64BIT_BITBUF -typedef mz_uint64 tinfl_bit_buf_t; -#define TINFL_BITBUF_SIZE (64) -#else -typedef mz_uint32 tinfl_bit_buf_t; -#define TINFL_BITBUF_SIZE (32) -#endif - -struct tinfl_decompressor_tag { - mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, - m_check_adler32, m_dist, m_counter, m_num_extra, - m_table_sizes[TINFL_MAX_HUFF_TABLES]; - tinfl_bit_buf_t m_bit_buf; - size_t m_dist_from_out_buf_start; - tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; - mz_uint8 m_raw_header[4], - m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; -}; - -// ------------------- Low-level Compression API Definitions - -// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly -// slower, and raw/dynamic blocks will be output more frequently). -#define TDEFL_LESS_MEMORY 0 - -// tdefl_init() compression flags logically OR'd together (low 12 bits contain -// the max. number of probes per dictionary search): -// TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes -// per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap -// compression), 4095=Huffman+LZ (slowest/best compression). -enum { - TDEFL_HUFFMAN_ONLY = 0, - TDEFL_DEFAULT_MAX_PROBES = 128, - TDEFL_MAX_PROBES_MASK = 0xFFF -}; - -// TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before -// the deflate data, and the Adler-32 of the source data at the end. Otherwise, -// you'll get raw deflate data. -// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even -// when not writing zlib headers). -// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more -// efficient lazy parsing. -// TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's -// initialization time to the minimum, but the output may vary from run to run -// given the same input (depending on the contents of memory). -// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) -// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. -// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. -// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. -// The low 12 bits are reserved to control the max # of hash probes per -// dictionary lookup (see TDEFL_MAX_PROBES_MASK). -enum { - TDEFL_WRITE_ZLIB_HEADER = 0x01000, - TDEFL_COMPUTE_ADLER32 = 0x02000, - TDEFL_GREEDY_PARSING_FLAG = 0x04000, - TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, - TDEFL_RLE_MATCHES = 0x10000, - TDEFL_FILTER_MATCHES = 0x20000, - TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, - TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 -}; - -// High level compression functions: -// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block -// allocated via malloc(). -// On entry: -// pSrc_buf, src_buf_len: Pointer and size of source block to compress. -// flags: The max match finder probes (default is 128) logically OR'd against -// the above flags. Higher probes are slower but improve compression. -// On return: -// Function returns a pointer to the compressed data, or NULL on failure. -// *pOut_len will be set to the compressed data's size, which could be larger -// than src_buf_len on uncompressible data. -// The caller must free() the returned block when it's no longer needed. -void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, - size_t *pOut_len, int flags); - -// tdefl_compress_mem_to_mem() compresses a block in memory to another block in -// memory. -// Returns 0 on failure. -size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, - const void *pSrc_buf, size_t src_buf_len, - int flags); - -// Compresses an image to a compressed PNG file in memory. -// On entry: -// pImage, w, h, and num_chans describe the image to compress. num_chans may be -// 1, 2, 3, or 4. -// The image pitch in bytes per scanline will be w*num_chans. The leftmost -// pixel on the top scanline is stored first in memory. -// level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, -// MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL -// If flip is true, the image will be flipped on the Y axis (useful for OpenGL -// apps). -// On return: -// Function returns a pointer to the compressed data, or NULL on failure. -// *pLen_out will be set to the size of the PNG image file. -// The caller must mz_free() the returned heap block (which will typically be -// larger than *pLen_out) when it's no longer needed. -void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, - int h, int num_chans, - size_t *pLen_out, - mz_uint level, mz_bool flip); -void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, - int num_chans, size_t *pLen_out); - -// Output stream interface. The compressor uses this interface to write -// compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. -typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len, - void *pUser); - -// tdefl_compress_mem_to_output() compresses a block to an output stream. The -// above helpers use this function internally. -mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, - tdefl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags); - -enum { - TDEFL_MAX_HUFF_TABLES = 3, - TDEFL_MAX_HUFF_SYMBOLS_0 = 288, - TDEFL_MAX_HUFF_SYMBOLS_1 = 32, - TDEFL_MAX_HUFF_SYMBOLS_2 = 19, - TDEFL_LZ_DICT_SIZE = 32768, - TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, - TDEFL_MIN_MATCH_LEN = 3, - TDEFL_MAX_MATCH_LEN = 258 -}; - -// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed -// output block (using static/fixed Huffman codes). -#if TDEFL_LESS_MEMORY -enum { - TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, - TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, - TDEFL_MAX_HUFF_SYMBOLS = 288, - TDEFL_LZ_HASH_BITS = 12, - TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, - TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, - TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS -}; -#else -enum { - TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, - TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, - TDEFL_MAX_HUFF_SYMBOLS = 288, - TDEFL_LZ_HASH_BITS = 15, - TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, - TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, - TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS -}; -#endif - -// The low-level tdefl functions below may be used directly if the above helper -// functions aren't flexible enough. The low-level functions don't make any heap -// allocations, unlike the above helper functions. -typedef enum { - TDEFL_STATUS_BAD_PARAM = -2, - TDEFL_STATUS_PUT_BUF_FAILED = -1, - TDEFL_STATUS_OKAY = 0, - TDEFL_STATUS_DONE = 1 -} tdefl_status; - -// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums -typedef enum { - TDEFL_NO_FLUSH = 0, - TDEFL_SYNC_FLUSH = 2, - TDEFL_FULL_FLUSH = 3, - TDEFL_FINISH = 4 -} tdefl_flush; - -// tdefl's compression state structure. -typedef struct { - tdefl_put_buf_func_ptr m_pPut_buf_func; - void *m_pPut_buf_user; - mz_uint m_flags, m_max_probes[2]; - int m_greedy_parsing; - mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; - mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; - mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, - m_bit_buffer; - mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, - m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, - m_wants_to_finish; - tdefl_status m_prev_return_status; - const void *m_pIn_buf; - void *m_pOut_buf; - size_t *m_pIn_buf_size, *m_pOut_buf_size; - tdefl_flush m_flush; - const mz_uint8 *m_pSrc; - size_t m_src_buf_left, m_out_buf_ofs; - mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; - mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; - mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; - mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; - mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; - mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; - mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; - mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; -} tdefl_compressor; - -// Initializes the compressor. -// There is no corresponding deinit() function because the tdefl API's do not -// dynamically allocate memory. -// pBut_buf_func: If NULL, output data will be supplied to the specified -// callback. In this case, the user should call the tdefl_compress_buffer() API -// for compression. -// If pBut_buf_func is NULL the user should always call the tdefl_compress() -// API. -// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, -// etc.) -tdefl_status tdefl_init(tdefl_compressor *d, - tdefl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags); - -// Compresses a block of data, consuming as much of the specified input buffer -// as possible, and writing as much compressed data to the specified output -// buffer as possible. -tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, - size_t *pIn_buf_size, void *pOut_buf, - size_t *pOut_buf_size, tdefl_flush flush); - -// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a -// non-NULL tdefl_put_buf_func_ptr. -// tdefl_compress_buffer() always consumes the entire input buffer. -tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, - size_t in_buf_size, tdefl_flush flush); - -tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d); -mz_uint32 tdefl_get_adler32(tdefl_compressor *d); - -// Can't use tdefl_create_comp_flags_from_zip_params if MINIZ_NO_ZLIB_APIS isn't -// defined, because it uses some of its macros. -#ifndef MINIZ_NO_ZLIB_APIS -// Create tdefl_compress() flags given zlib-style compression parameters. -// level may range from [0,10] (where 10 is absolute max compression, but may be -// much slower on some files) -// window_bits may be -15 (raw deflate) or 15 (zlib) -// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, -// MZ_RLE, or MZ_FIXED -mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, - int strategy); -#endif // #ifndef MINIZ_NO_ZLIB_APIS - -#ifdef __cplusplus -} -#endif - -#endif // MINIZ_HEADER_INCLUDED - -// ------------------- End of Header: Implementation follows. (If you only want -// the header, define MINIZ_HEADER_FILE_ONLY.) - -#ifndef MINIZ_HEADER_FILE_ONLY - -typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1]; -typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1]; -typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1]; - -//#include <assert.h> -//#include <string.h> - -#define MZ_ASSERT(x) assert(x) - -#ifdef MINIZ_NO_MALLOC -#define MZ_MALLOC(x) NULL -#define MZ_FREE(x) (void)x, ((void)0) -#define MZ_REALLOC(p, x) NULL -#else -#define MZ_MALLOC(x) malloc(x) -#define MZ_FREE(x) free(x) -#define MZ_REALLOC(p, x) realloc(p, x) -#endif - -#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN -#define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) -#define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) -#else -#define MZ_READ_LE16(p) \ - ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \ - ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U)) -#define MZ_READ_LE32(p) \ - ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \ - ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | \ - ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | \ - ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U)) -#endif - -#ifdef _MSC_VER -#define MZ_FORCEINLINE __forceinline -#elif defined(__GNUC__) -#define MZ_FORCEINLINE inline __attribute__((__always_inline__)) -#else -#define MZ_FORCEINLINE inline -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -// ------------------- zlib-style API's - -mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) { - mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); - size_t block_len = buf_len % 5552; - if (!ptr) return MZ_ADLER32_INIT; - while (buf_len) { - for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { - s1 += ptr[0], s2 += s1; - s1 += ptr[1], s2 += s1; - s1 += ptr[2], s2 += s1; - s1 += ptr[3], s2 += s1; - s1 += ptr[4], s2 += s1; - s1 += ptr[5], s2 += s1; - s1 += ptr[6], s2 += s1; - s1 += ptr[7], s2 += s1; - } - for (; i < block_len; ++i) s1 += *ptr++, s2 += s1; - s1 %= 65521U, s2 %= 65521U; - buf_len -= block_len; - block_len = 5552; - } - return (s2 << 16) + s1; -} - -// Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C -// implementation that balances processor cache usage against speed": -// http://www.geocities.com/malbrain/ -mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) { - static const mz_uint32 s_crc32[16] = { - 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, - 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, - 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c}; - mz_uint32 crcu32 = (mz_uint32)crc; - if (!ptr) return MZ_CRC32_INIT; - crcu32 = ~crcu32; - while (buf_len--) { - mz_uint8 b = *ptr++; - crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; - crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; - } - return ~crcu32; -} - -void mz_free(void *p) { MZ_FREE(p); } - -#ifndef MINIZ_NO_ZLIB_APIS - -static void *def_alloc_func(void *opaque, size_t items, size_t size) { - (void)opaque, (void)items, (void)size; - return MZ_MALLOC(items * size); -} -static void def_free_func(void *opaque, void *address) { - (void)opaque, (void)address; - MZ_FREE(address); -} -// static void *def_realloc_func(void *opaque, void *address, size_t items, -// size_t size) { -// (void)opaque, (void)address, (void)items, (void)size; -// return MZ_REALLOC(address, items * size); -//} - -const char *mz_version(void) { return MZ_VERSION; } - -int mz_deflateInit(mz_streamp pStream, int level) { - return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, - MZ_DEFAULT_STRATEGY); -} - -int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, - int mem_level, int strategy) { - tdefl_compressor *pComp; - mz_uint comp_flags = - TDEFL_COMPUTE_ADLER32 | - tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); - - if (!pStream) return MZ_STREAM_ERROR; - if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || - ((window_bits != MZ_DEFAULT_WINDOW_BITS) && - (-window_bits != MZ_DEFAULT_WINDOW_BITS))) - return MZ_PARAM_ERROR; - - pStream->data_type = 0; - pStream->adler = MZ_ADLER32_INIT; - pStream->msg = NULL; - pStream->reserved = 0; - pStream->total_in = 0; - pStream->total_out = 0; - if (!pStream->zalloc) pStream->zalloc = def_alloc_func; - if (!pStream->zfree) pStream->zfree = def_free_func; - - pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, - sizeof(tdefl_compressor)); - if (!pComp) return MZ_MEM_ERROR; - - pStream->state = (struct mz_internal_state *)pComp; - - if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) { - mz_deflateEnd(pStream); - return MZ_PARAM_ERROR; - } - - return MZ_OK; -} - -int mz_deflateReset(mz_streamp pStream) { - if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || - (!pStream->zfree)) - return MZ_STREAM_ERROR; - pStream->total_in = pStream->total_out = 0; - tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, - ((tdefl_compressor *)pStream->state)->m_flags); - return MZ_OK; -} - -int mz_deflate(mz_streamp pStream, int flush) { - size_t in_bytes, out_bytes; - mz_ulong orig_total_in, orig_total_out; - int mz_status = MZ_OK; - - if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || - (!pStream->next_out)) - return MZ_STREAM_ERROR; - if (!pStream->avail_out) return MZ_BUF_ERROR; - - if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; - - if (((tdefl_compressor *)pStream->state)->m_prev_return_status == - TDEFL_STATUS_DONE) - return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; - - orig_total_in = pStream->total_in; - orig_total_out = pStream->total_out; - for (;;) { - tdefl_status defl_status; - in_bytes = pStream->avail_in; - out_bytes = pStream->avail_out; - - defl_status = tdefl_compress((tdefl_compressor *)pStream->state, - pStream->next_in, &in_bytes, pStream->next_out, - &out_bytes, (tdefl_flush)flush); - pStream->next_in += (mz_uint)in_bytes; - pStream->avail_in -= (mz_uint)in_bytes; - pStream->total_in += (mz_uint)in_bytes; - pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state); - - pStream->next_out += (mz_uint)out_bytes; - pStream->avail_out -= (mz_uint)out_bytes; - pStream->total_out += (mz_uint)out_bytes; - - if (defl_status < 0) { - mz_status = MZ_STREAM_ERROR; - break; - } else if (defl_status == TDEFL_STATUS_DONE) { - mz_status = MZ_STREAM_END; - break; - } else if (!pStream->avail_out) - break; - else if ((!pStream->avail_in) && (flush != MZ_FINISH)) { - if ((flush) || (pStream->total_in != orig_total_in) || - (pStream->total_out != orig_total_out)) - break; - return MZ_BUF_ERROR; // Can't make forward progress without some input. - } - } - return mz_status; -} - -int mz_deflateEnd(mz_streamp pStream) { - if (!pStream) return MZ_STREAM_ERROR; - if (pStream->state) { - pStream->zfree(pStream->opaque, pStream->state); - pStream->state = NULL; - } - return MZ_OK; -} - -mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) { - (void)pStream; - // This is really over conservative. (And lame, but it's actually pretty - // tricky to compute a true upper bound given the way tdefl's blocking works.) - return MZ_MAX(128 + (source_len * 110) / 100, - 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); -} - -int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len, int level) { - int status; - mz_stream stream; - memset(&stream, 0, sizeof(stream)); - - // In case mz_ulong is 64-bits (argh I hate longs). - if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; - - stream.next_in = pSource; - stream.avail_in = (mz_uint32)source_len; - stream.next_out = pDest; - stream.avail_out = (mz_uint32)*pDest_len; - - status = mz_deflateInit(&stream, level); - if (status != MZ_OK) return status; - - status = mz_deflate(&stream, MZ_FINISH); - if (status != MZ_STREAM_END) { - mz_deflateEnd(&stream); - return (status == MZ_OK) ? MZ_BUF_ERROR : status; - } - - *pDest_len = stream.total_out; - return mz_deflateEnd(&stream); -} - -int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len) { - return mz_compress2(pDest, pDest_len, pSource, source_len, - MZ_DEFAULT_COMPRESSION); -} - -mz_ulong mz_compressBound(mz_ulong source_len) { - return mz_deflateBound(NULL, source_len); -} - -typedef struct { - tinfl_decompressor m_decomp; - mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; - int m_window_bits; - mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; - tinfl_status m_last_status; -} inflate_state; - -int mz_inflateInit2(mz_streamp pStream, int window_bits) { - inflate_state *pDecomp; - if (!pStream) return MZ_STREAM_ERROR; - if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && - (-window_bits != MZ_DEFAULT_WINDOW_BITS)) - return MZ_PARAM_ERROR; - - pStream->data_type = 0; - pStream->adler = 0; - pStream->msg = NULL; - pStream->total_in = 0; - pStream->total_out = 0; - pStream->reserved = 0; - if (!pStream->zalloc) pStream->zalloc = def_alloc_func; - if (!pStream->zfree) pStream->zfree = def_free_func; - - pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, - sizeof(inflate_state)); - if (!pDecomp) return MZ_MEM_ERROR; - - pStream->state = (struct mz_internal_state *)pDecomp; - - tinfl_init(&pDecomp->m_decomp); - pDecomp->m_dict_ofs = 0; - pDecomp->m_dict_avail = 0; - pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; - pDecomp->m_first_call = 1; - pDecomp->m_has_flushed = 0; - pDecomp->m_window_bits = window_bits; - - return MZ_OK; -} - -int mz_inflateInit(mz_streamp pStream) { - return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); -} - -int mz_inflate(mz_streamp pStream, int flush) { - inflate_state *pState; - mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; - size_t in_bytes, out_bytes, orig_avail_in; - tinfl_status status; - - if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR; - if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; - if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) - return MZ_STREAM_ERROR; - - pState = (inflate_state *)pStream->state; - if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; - orig_avail_in = pStream->avail_in; - - first_call = pState->m_first_call; - pState->m_first_call = 0; - if (pState->m_last_status < 0) return MZ_DATA_ERROR; - - if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR; - pState->m_has_flushed |= (flush == MZ_FINISH); - - if ((flush == MZ_FINISH) && (first_call)) { - // MZ_FINISH on the first call implies that the input and output buffers are - // large enough to hold the entire compressed/decompressed file. - decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; - in_bytes = pStream->avail_in; - out_bytes = pStream->avail_out; - status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, - pStream->next_out, pStream->next_out, &out_bytes, - decomp_flags); - pState->m_last_status = status; - pStream->next_in += (mz_uint)in_bytes; - pStream->avail_in -= (mz_uint)in_bytes; - pStream->total_in += (mz_uint)in_bytes; - pStream->adler = tinfl_get_adler32(&pState->m_decomp); - pStream->next_out += (mz_uint)out_bytes; - pStream->avail_out -= (mz_uint)out_bytes; - pStream->total_out += (mz_uint)out_bytes; - - if (status < 0) - return MZ_DATA_ERROR; - else if (status != TINFL_STATUS_DONE) { - pState->m_last_status = TINFL_STATUS_FAILED; - return MZ_BUF_ERROR; - } - return MZ_STREAM_END; - } - // flush != MZ_FINISH then we must assume there's more input. - if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; - - if (pState->m_dict_avail) { - n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); - memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); - pStream->next_out += n; - pStream->avail_out -= n; - pStream->total_out += n; - pState->m_dict_avail -= n; - pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); - return ((pState->m_last_status == TINFL_STATUS_DONE) && - (!pState->m_dict_avail)) - ? MZ_STREAM_END - : MZ_OK; - } - - for (;;) { - in_bytes = pStream->avail_in; - out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; - - status = tinfl_decompress( - &pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, - pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags); - pState->m_last_status = status; - - pStream->next_in += (mz_uint)in_bytes; - pStream->avail_in -= (mz_uint)in_bytes; - pStream->total_in += (mz_uint)in_bytes; - pStream->adler = tinfl_get_adler32(&pState->m_decomp); - - pState->m_dict_avail = (mz_uint)out_bytes; - - n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); - memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); - pStream->next_out += n; - pStream->avail_out -= n; - pStream->total_out += n; - pState->m_dict_avail -= n; - pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); - - if (status < 0) - return MZ_DATA_ERROR; // Stream is corrupted (there could be some - // uncompressed data left in the output dictionary - - // oh well). - else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) - return MZ_BUF_ERROR; // Signal caller that we can't make forward progress - // without supplying more input or by setting flush - // to MZ_FINISH. - else if (flush == MZ_FINISH) { - // The output buffer MUST be large to hold the remaining uncompressed data - // when flush==MZ_FINISH. - if (status == TINFL_STATUS_DONE) - return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; - // status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's - // at least 1 more byte on the way. If there's no more room left in the - // output buffer then something is wrong. - else if (!pStream->avail_out) - return MZ_BUF_ERROR; - } else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || - (!pStream->avail_out) || (pState->m_dict_avail)) - break; - } - - return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) - ? MZ_STREAM_END - : MZ_OK; -} - -int mz_inflateEnd(mz_streamp pStream) { - if (!pStream) return MZ_STREAM_ERROR; - if (pStream->state) { - pStream->zfree(pStream->opaque, pStream->state); - pStream->state = NULL; - } - return MZ_OK; -} - -int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, - const unsigned char *pSource, mz_ulong source_len) { - mz_stream stream; - int status; - memset(&stream, 0, sizeof(stream)); - - // In case mz_ulong is 64-bits (argh I hate longs). - if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; - - stream.next_in = pSource; - stream.avail_in = (mz_uint32)source_len; - stream.next_out = pDest; - stream.avail_out = (mz_uint32)*pDest_len; - - status = mz_inflateInit(&stream); - if (status != MZ_OK) return status; - - status = mz_inflate(&stream, MZ_FINISH); - if (status != MZ_STREAM_END) { - mz_inflateEnd(&stream); - return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR - : status; - } - *pDest_len = stream.total_out; - - return mz_inflateEnd(&stream); -} - -const char *mz_error(int err) { - static struct { - int m_err; - const char *m_pDesc; - } s_error_descs[] = {{MZ_OK, ""}, - {MZ_STREAM_END, "stream end"}, - {MZ_NEED_DICT, "need dictionary"}, - {MZ_ERRNO, "file error"}, - {MZ_STREAM_ERROR, "stream error"}, - {MZ_DATA_ERROR, "data error"}, - {MZ_MEM_ERROR, "out of memory"}, - {MZ_BUF_ERROR, "buf error"}, - {MZ_VERSION_ERROR, "version error"}, - {MZ_PARAM_ERROR, "parameter error"}}; - mz_uint i; - for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) - if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc; - return NULL; -} - -#endif // MINIZ_NO_ZLIB_APIS - -// ------------------- Low-level Decompression (completely independent from all -// compression API's) - -#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l) -#define TINFL_MEMSET(p, c, l) memset(p, c, l) - -#define TINFL_CR_BEGIN \ - switch (r->m_state) { \ - case 0: -#define TINFL_CR_RETURN(state_index, result) \ - do { \ - status = result; \ - r->m_state = state_index; \ - goto common_exit; \ - case state_index:; \ - } \ - MZ_MACRO_END -#define TINFL_CR_RETURN_FOREVER(state_index, result) \ - do { \ - for (;;) { \ - TINFL_CR_RETURN(state_index, result); \ - } \ - } \ - MZ_MACRO_END -#define TINFL_CR_FINISH } - -// TODO: If the caller has indicated that there's no more input, and we attempt -// to read beyond the input buf, then something is wrong with the input because -// the inflator never -// reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of -// the stream with 0's in this scenario. -#define TINFL_GET_BYTE(state_index, c) \ - do { \ - if (pIn_buf_cur >= pIn_buf_end) { \ - for (;;) { \ - if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \ - TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \ - if (pIn_buf_cur < pIn_buf_end) { \ - c = *pIn_buf_cur++; \ - break; \ - } \ - } else { \ - c = 0; \ - break; \ - } \ - } \ - } else \ - c = *pIn_buf_cur++; \ - } \ - MZ_MACRO_END - -#define TINFL_NEED_BITS(state_index, n) \ - do { \ - mz_uint c; \ - TINFL_GET_BYTE(state_index, c); \ - bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ - num_bits += 8; \ - } while (num_bits < (mz_uint)(n)) -#define TINFL_SKIP_BITS(state_index, n) \ - do { \ - if (num_bits < (mz_uint)(n)) { \ - TINFL_NEED_BITS(state_index, n); \ - } \ - bit_buf >>= (n); \ - num_bits -= (n); \ - } \ - MZ_MACRO_END -#define TINFL_GET_BITS(state_index, b, n) \ - do { \ - if (num_bits < (mz_uint)(n)) { \ - TINFL_NEED_BITS(state_index, n); \ - } \ - b = bit_buf & ((1 << (n)) - 1); \ - bit_buf >>= (n); \ - num_bits -= (n); \ - } \ - MZ_MACRO_END - -// TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes -// remaining in the input buffer falls below 2. -// It reads just enough bytes from the input stream that are needed to decode -// the next Huffman code (and absolutely no more). It works by trying to fully -// decode a -// Huffman code by using whatever bits are currently present in the bit buffer. -// If this fails, it reads another byte, and tries again until it succeeds or -// until the -// bit buffer contains >=15 bits (deflate's max. Huffman code size). -#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ - do { \ - temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ - if (temp >= 0) { \ - code_len = temp >> 9; \ - if ((code_len) && (num_bits >= code_len)) break; \ - } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \ - code_len = TINFL_FAST_LOOKUP_BITS; \ - do { \ - temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ - } while ((temp < 0) && (num_bits >= (code_len + 1))); \ - if (temp >= 0) break; \ - } \ - TINFL_GET_BYTE(state_index, c); \ - bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ - num_bits += 8; \ - } while (num_bits < 15); - -// TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex -// than you would initially expect because the zlib API expects the decompressor -// to never read -// beyond the final byte of the deflate stream. (In other words, when this macro -// wants to read another byte from the input, it REALLY needs another byte in -// order to fully -// decode the next Huffman code.) Handling this properly is particularly -// important on raw deflate (non-zlib) streams, which aren't followed by a byte -// aligned adler-32. -// The slow path is only executed at the very end of the input buffer. -#define TINFL_HUFF_DECODE(state_index, sym, pHuff) \ - do { \ - int temp; \ - mz_uint code_len, c; \ - if (num_bits < 15) { \ - if ((pIn_buf_end - pIn_buf_cur) < 2) { \ - TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ - } else { \ - bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | \ - (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \ - pIn_buf_cur += 2; \ - num_bits += 16; \ - } \ - } \ - if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= \ - 0) \ - code_len = temp >> 9, temp &= 511; \ - else { \ - code_len = TINFL_FAST_LOOKUP_BITS; \ - do { \ - temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ - } while (temp < 0); \ - } \ - sym = temp; \ - bit_buf >>= code_len; \ - num_bits -= code_len; \ - } \ - MZ_MACRO_END - -tinfl_status tinfl_decompress(tinfl_decompressor *r, - const mz_uint8 *pIn_buf_next, - size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, - mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, - const mz_uint32 decomp_flags) { - static const int s_length_base[31] = { - 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, - 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; - static const int s_length_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, - 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, - 4, 4, 5, 5, 5, 5, 0, 0, 0}; - static const int s_dist_base[32] = { - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, - 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, - 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0}; - static const int s_dist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, - 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, - 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; - static const mz_uint8 s_length_dezigzag[19] = { - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; - static const int s_min_table_sizes[3] = {257, 1, 4}; - - tinfl_status status = TINFL_STATUS_FAILED; - mz_uint32 num_bits, dist, counter, num_extra; - tinfl_bit_buf_t bit_buf; - const mz_uint8 *pIn_buf_cur = pIn_buf_next, - *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; - mz_uint8 *pOut_buf_cur = pOut_buf_next, - *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; - size_t out_buf_size_mask = - (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) - ? (size_t)-1 - : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, - dist_from_out_buf_start; - - // Ensure the output buffer's size is a power of 2, unless the output buffer - // is large enough to hold the entire output file (in which case it doesn't - // matter). - if (((out_buf_size_mask + 1) & out_buf_size_mask) || - (pOut_buf_next < pOut_buf_start)) { - *pIn_buf_size = *pOut_buf_size = 0; - return TINFL_STATUS_BAD_PARAM; - } - - num_bits = r->m_num_bits; - bit_buf = r->m_bit_buf; - dist = r->m_dist; - counter = r->m_counter; - num_extra = r->m_num_extra; - dist_from_out_buf_start = r->m_dist_from_out_buf_start; - TINFL_CR_BEGIN - - bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; - r->m_z_adler32 = r->m_check_adler32 = 1; - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) { - TINFL_GET_BYTE(1, r->m_zhdr0); - TINFL_GET_BYTE(2, r->m_zhdr1); - counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || - (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); - if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) - counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || - ((out_buf_size_mask + 1) < - (size_t)(1ULL << (8U + (r->m_zhdr0 >> 4))))); - if (counter) { - TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); - } - } - - do { - TINFL_GET_BITS(3, r->m_final, 3); - r->m_type = r->m_final >> 1; - if (r->m_type == 0) { - TINFL_SKIP_BITS(5, num_bits & 7); - for (counter = 0; counter < 4; ++counter) { - if (num_bits) - TINFL_GET_BITS(6, r->m_raw_header[counter], 8); - else - TINFL_GET_BYTE(7, r->m_raw_header[counter]); - } - if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != - (mz_uint)(0xFFFF ^ - (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { - TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); - } - while ((counter) && (num_bits)) { - TINFL_GET_BITS(51, dist, 8); - while (pOut_buf_cur >= pOut_buf_end) { - TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); - } - *pOut_buf_cur++ = (mz_uint8)dist; - counter--; - } - while (counter) { - size_t n; - while (pOut_buf_cur >= pOut_buf_end) { - TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); - } - while (pIn_buf_cur >= pIn_buf_end) { - if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { - TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT); - } else { - TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED); - } - } - n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), - (size_t)(pIn_buf_end - pIn_buf_cur)), - counter); - TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); - pIn_buf_cur += n; - pOut_buf_cur += n; - counter -= (mz_uint)n; - } - } else if (r->m_type == 3) { - TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); - } else { - if (r->m_type == 1) { - mz_uint8 *p = r->m_tables[0].m_code_size; - mz_uint i; - r->m_table_sizes[0] = 288; - r->m_table_sizes[1] = 32; - TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); - for (i = 0; i <= 143; ++i) *p++ = 8; - for (; i <= 255; ++i) *p++ = 9; - for (; i <= 279; ++i) *p++ = 7; - for (; i <= 287; ++i) *p++ = 8; - } else { - for (counter = 0; counter < 3; counter++) { - TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); - r->m_table_sizes[counter] += s_min_table_sizes[counter]; - } - MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); - for (counter = 0; counter < r->m_table_sizes[2]; counter++) { - mz_uint s; - TINFL_GET_BITS(14, s, 3); - r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; - } - r->m_table_sizes[2] = 19; - } - for (; (int)r->m_type >= 0; r->m_type--) { - int tree_next, tree_cur; - tinfl_huff_table *pTable; - mz_uint i, j, used_syms, total, sym_index, next_code[17], - total_syms[16]; - pTable = &r->m_tables[r->m_type]; - MZ_CLEAR_OBJ(total_syms); - MZ_CLEAR_OBJ(pTable->m_look_up); - MZ_CLEAR_OBJ(pTable->m_tree); - for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) - total_syms[pTable->m_code_size[i]]++; - used_syms = 0, total = 0; - next_code[0] = next_code[1] = 0; - for (i = 1; i <= 15; ++i) { - used_syms += total_syms[i]; - next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); - } - if ((65536 != total) && (used_syms > 1)) { - TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); - } - for (tree_next = -1, sym_index = 0; - sym_index < r->m_table_sizes[r->m_type]; ++sym_index) { - mz_uint rev_code = 0, l, cur_code, - code_size = pTable->m_code_size[sym_index]; - if (!code_size) continue; - cur_code = next_code[code_size]++; - for (l = code_size; l > 0; l--, cur_code >>= 1) - rev_code = (rev_code << 1) | (cur_code & 1); - if (code_size <= TINFL_FAST_LOOKUP_BITS) { - mz_int16 k = (mz_int16)((code_size << 9) | sym_index); - while (rev_code < TINFL_FAST_LOOKUP_SIZE) { - pTable->m_look_up[rev_code] = k; - rev_code += (1 << code_size); - } - continue; - } - if (0 == - (tree_cur = pTable->m_look_up[rev_code & - (TINFL_FAST_LOOKUP_SIZE - 1)])) { - pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = - (mz_int16)tree_next; - tree_cur = tree_next; - tree_next -= 2; - } - rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); - for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) { - tree_cur -= ((rev_code >>= 1) & 1); - if (!pTable->m_tree[-tree_cur - 1]) { - pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; - tree_cur = tree_next; - tree_next -= 2; - } else - tree_cur = pTable->m_tree[-tree_cur - 1]; - } - tree_cur -= ((rev_code >>= 1) & 1); - pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; - } - if (r->m_type == 2) { - for (counter = 0; - counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) { - mz_uint s; - TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); - if (dist < 16) { - r->m_len_codes[counter++] = (mz_uint8)dist; - continue; - } - if ((dist == 16) && (!counter)) { - TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); - } - num_extra = "\02\03\07"[dist - 16]; - TINFL_GET_BITS(18, s, num_extra); - s += "\03\03\013"[dist - 16]; - TINFL_MEMSET(r->m_len_codes + counter, - (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); - counter += s; - } - if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) { - TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); - } - TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, - r->m_table_sizes[0]); - TINFL_MEMCPY(r->m_tables[1].m_code_size, - r->m_len_codes + r->m_table_sizes[0], - r->m_table_sizes[1]); - } - } - for (;;) { - mz_uint8 *pSrc; - for (;;) { - if (((pIn_buf_end - pIn_buf_cur) < 4) || - ((pOut_buf_end - pOut_buf_cur) < 2)) { - TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); - if (counter >= 256) break; - while (pOut_buf_cur >= pOut_buf_end) { - TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); - } - *pOut_buf_cur++ = (mz_uint8)counter; - } else { - int sym2; - mz_uint code_len; -#if TINFL_USE_64BIT_BITBUF - if (num_bits < 30) { - bit_buf |= - (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); - pIn_buf_cur += 4; - num_bits += 32; - } -#else - if (num_bits < 15) { - bit_buf |= - (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); - pIn_buf_cur += 2; - num_bits += 16; - } -#endif - if ((sym2 = - r->m_tables[0] - .m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= - 0) - code_len = sym2 >> 9; - else { - code_len = TINFL_FAST_LOOKUP_BITS; - do { - sym2 = r->m_tables[0] - .m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; - } while (sym2 < 0); - } - counter = sym2; - bit_buf >>= code_len; - num_bits -= code_len; - if (counter & 256) break; - -#if !TINFL_USE_64BIT_BITBUF - if (num_bits < 15) { - bit_buf |= - (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); - pIn_buf_cur += 2; - num_bits += 16; - } -#endif - if ((sym2 = - r->m_tables[0] - .m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= - 0) - code_len = sym2 >> 9; - else { - code_len = TINFL_FAST_LOOKUP_BITS; - do { - sym2 = r->m_tables[0] - .m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; - } while (sym2 < 0); - } - bit_buf >>= code_len; - num_bits -= code_len; - - pOut_buf_cur[0] = (mz_uint8)counter; - if (sym2 & 256) { - pOut_buf_cur++; - counter = sym2; - break; - } - pOut_buf_cur[1] = (mz_uint8)sym2; - pOut_buf_cur += 2; - } - } - if ((counter &= 511) == 256) break; - - num_extra = s_length_extra[counter - 257]; - counter = s_length_base[counter - 257]; - if (num_extra) { - mz_uint extra_bits; - TINFL_GET_BITS(25, extra_bits, num_extra); - counter += extra_bits; - } - - TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); - num_extra = s_dist_extra[dist]; - dist = s_dist_base[dist]; - if (num_extra) { - mz_uint extra_bits; - TINFL_GET_BITS(27, extra_bits, num_extra); - dist += extra_bits; - } - - dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; - if ((dist > dist_from_out_buf_start) && - (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) { - TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); - } - - pSrc = pOut_buf_start + - ((dist_from_out_buf_start - dist) & out_buf_size_mask); - - if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) { - while (counter--) { - while (pOut_buf_cur >= pOut_buf_end) { - TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); - } - *pOut_buf_cur++ = - pOut_buf_start[(dist_from_out_buf_start++ - dist) & - out_buf_size_mask]; - } - continue; - } -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES - else if ((counter >= 9) && (counter <= dist)) { - const mz_uint8 *pSrc_end = pSrc + (counter & ~7); - do { - ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0]; - ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1]; - pOut_buf_cur += 8; - } while ((pSrc += 8) < pSrc_end); - if ((counter &= 7) < 3) { - if (counter) { - pOut_buf_cur[0] = pSrc[0]; - if (counter > 1) pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur += counter; - } - continue; - } - } -#endif - do { - pOut_buf_cur[0] = pSrc[0]; - pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur[2] = pSrc[2]; - pOut_buf_cur += 3; - pSrc += 3; - } while ((int)(counter -= 3) > 2); - if ((int)counter > 0) { - pOut_buf_cur[0] = pSrc[0]; - if ((int)counter > 1) pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur += counter; - } - } - } - } while (!(r->m_final & 1)); - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) { - TINFL_SKIP_BITS(32, num_bits & 7); - for (counter = 0; counter < 4; ++counter) { - mz_uint s; - if (num_bits) - TINFL_GET_BITS(41, s, 8); - else - TINFL_GET_BYTE(42, s); - r->m_z_adler32 = (r->m_z_adler32 << 8) | s; - } - } - TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); - TINFL_CR_FINISH - -common_exit: - r->m_num_bits = num_bits; - r->m_bit_buf = bit_buf; - r->m_dist = dist; - r->m_counter = counter; - r->m_num_extra = num_extra; - r->m_dist_from_out_buf_start = dist_from_out_buf_start; - *pIn_buf_size = pIn_buf_cur - pIn_buf_next; - *pOut_buf_size = pOut_buf_cur - pOut_buf_next; - if ((decomp_flags & - (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && - (status >= 0)) { - const mz_uint8 *ptr = pOut_buf_next; - size_t buf_len = *pOut_buf_size; - mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, - s2 = r->m_check_adler32 >> 16; - size_t block_len = buf_len % 5552; - while (buf_len) { - for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { - s1 += ptr[0], s2 += s1; - s1 += ptr[1], s2 += s1; - s1 += ptr[2], s2 += s1; - s1 += ptr[3], s2 += s1; - s1 += ptr[4], s2 += s1; - s1 += ptr[5], s2 += s1; - s1 += ptr[6], s2 += s1; - s1 += ptr[7], s2 += s1; - } - for (; i < block_len; ++i) s1 += *ptr++, s2 += s1; - s1 %= 65521U, s2 %= 65521U; - buf_len -= block_len; - block_len = 5552; - } - r->m_check_adler32 = (s2 << 16) + s1; - if ((status == TINFL_STATUS_DONE) && - (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && - (r->m_check_adler32 != r->m_z_adler32)) - status = TINFL_STATUS_ADLER32_MISMATCH; - } - return status; -} - -// Higher level helper functions. -void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, - size_t *pOut_len, int flags) { - tinfl_decompressor decomp; - void *pBuf = NULL, *pNew_buf; - size_t src_buf_ofs = 0, out_buf_capacity = 0; - *pOut_len = 0; - tinfl_init(&decomp); - for (;;) { - size_t src_buf_size = src_buf_len - src_buf_ofs, - dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity; - tinfl_status status = tinfl_decompress( - &decomp, (const mz_uint8 *)pSrc_buf + src_buf_ofs, &src_buf_size, - (mz_uint8 *)pBuf, pBuf ? (mz_uint8 *)pBuf + *pOut_len : NULL, - &dst_buf_size, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); - if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) { - MZ_FREE(pBuf); - *pOut_len = 0; - return NULL; - } - src_buf_ofs += src_buf_size; - *pOut_len += dst_buf_size; - if (status == TINFL_STATUS_DONE) break; - new_out_buf_capacity = out_buf_capacity * 2; - if (new_out_buf_capacity < 128) new_out_buf_capacity = 128; - pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity); - if (!pNew_buf) { - MZ_FREE(pBuf); - *pOut_len = 0; - return NULL; - } - pBuf = pNew_buf; - out_buf_capacity = new_out_buf_capacity; - } - return pBuf; -} - -size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, - const void *pSrc_buf, size_t src_buf_len, - int flags) { - tinfl_decompressor decomp; - tinfl_status status; - tinfl_init(&decomp); - status = - tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, - (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, - (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); - return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED - : out_buf_len; -} - -int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, - tinfl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags) { - int result = 0; - tinfl_decompressor decomp; - mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE); - size_t in_buf_ofs = 0, dict_ofs = 0; - if (!pDict) return TINFL_STATUS_FAILED; - tinfl_init(&decomp); - for (;;) { - size_t in_buf_size = *pIn_buf_size - in_buf_ofs, - dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; - tinfl_status status = - tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, - &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size, - (flags & - ~(TINFL_FLAG_HAS_MORE_INPUT | - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))); - in_buf_ofs += in_buf_size; - if ((dst_buf_size) && - (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) - break; - if (status != TINFL_STATUS_HAS_MORE_OUTPUT) { - result = (status == TINFL_STATUS_DONE); - break; - } - dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1); - } - MZ_FREE(pDict); - *pIn_buf_size = in_buf_ofs; - return result; -} - -// ------------------- Low-level Compression (independent from all decompression -// API's) - -// Purposely making these tables static for faster init and thread safety. -static const mz_uint16 s_tdefl_len_sym[256] = { - 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, - 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, - 272, 272, 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, - 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, - 276, 276, 276, 276, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, - 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, - 278, 278, 278, 278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279, - 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, - 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, 281, 281, 281, 281, - 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, - 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282, - 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, - 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 283, 283, 283, - 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, - 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 284, - 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, - 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, - 285}; - -static const mz_uint8 s_tdefl_len_extra[256] = { - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0}; - -static const mz_uint8 s_tdefl_small_dist_sym[512] = { - 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, - 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17}; - -static const mz_uint8 s_tdefl_small_dist_extra[512] = { - 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; - -static const mz_uint8 s_tdefl_large_dist_sym[128] = { - 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, - 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, - 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29}; - -static const mz_uint8 s_tdefl_large_dist_extra[128] = { - 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; - -// Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted -// values. -typedef struct { mz_uint16 m_key, m_sym_index; } tdefl_sym_freq; -static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, - tdefl_sym_freq *pSyms0, - tdefl_sym_freq *pSyms1) { - mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; - tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; - MZ_CLEAR_OBJ(hist); - for (i = 0; i < num_syms; i++) { - mz_uint freq = pSyms0[i].m_key; - hist[freq & 0xFF]++; - hist[256 + ((freq >> 8) & 0xFF)]++; - } - while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) - total_passes--; - for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) { - const mz_uint32 *pHist = &hist[pass << 8]; - mz_uint offsets[256], cur_ofs = 0; - for (i = 0; i < 256; i++) { - offsets[i] = cur_ofs; - cur_ofs += pHist[i]; - } - for (i = 0; i < num_syms; i++) - pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = - pCur_syms[i]; - { - tdefl_sym_freq *t = pCur_syms; - pCur_syms = pNew_syms; - pNew_syms = t; - } - } - return pCur_syms; -} - -// tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, -// alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. -static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n) { - int root, leaf, next, avbl, used, dpth; - if (n == 0) - return; - else if (n == 1) { - A[0].m_key = 1; - return; - } - A[0].m_key += A[1].m_key; - root = 0; - leaf = 2; - for (next = 1; next < n - 1; next++) { - if (leaf >= n || A[root].m_key < A[leaf].m_key) { - A[next].m_key = A[root].m_key; - A[root++].m_key = (mz_uint16)next; - } else - A[next].m_key = A[leaf++].m_key; - if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) { - A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key); - A[root++].m_key = (mz_uint16)next; - } else - A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key); - } - A[n - 2].m_key = 0; - for (next = n - 3; next >= 0; next--) - A[next].m_key = A[A[next].m_key].m_key + 1; - avbl = 1; - used = dpth = 0; - root = n - 2; - next = n - 1; - while (avbl > 0) { - while (root >= 0 && (int)A[root].m_key == dpth) { - used++; - root--; - } - while (avbl > used) { - A[next--].m_key = (mz_uint16)(dpth); - avbl--; - } - avbl = 2 * used; - dpth++; - used = 0; - } -} - -// Limits canonical Huffman code table's max code size. -enum { TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 }; -static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, - int code_list_len, - int max_code_size) { - int i; - mz_uint32 total = 0; - if (code_list_len <= 1) return; - for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) - pNum_codes[max_code_size] += pNum_codes[i]; - for (i = max_code_size; i > 0; i--) - total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i)); - while (total != (1UL << max_code_size)) { - pNum_codes[max_code_size]--; - for (i = max_code_size - 1; i > 0; i--) - if (pNum_codes[i]) { - pNum_codes[i]--; - pNum_codes[i + 1] += 2; - break; - } - total--; - } -} - -static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, - int table_len, int code_size_limit, - int static_table) { - int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; - mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; - MZ_CLEAR_OBJ(num_codes); - if (static_table) { - for (i = 0; i < table_len; i++) - num_codes[d->m_huff_code_sizes[table_num][i]]++; - } else { - tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], - *pSyms; - int num_used_syms = 0; - const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0]; - for (i = 0; i < table_len; i++) - if (pSym_count[i]) { - syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; - syms0[num_used_syms++].m_sym_index = (mz_uint16)i; - } - - pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); - tdefl_calculate_minimum_redundancy(pSyms, num_used_syms); - - for (i = 0; i < num_used_syms; i++) num_codes[pSyms[i].m_key]++; - - tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, - code_size_limit); - - MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); - MZ_CLEAR_OBJ(d->m_huff_codes[table_num]); - for (i = 1, j = num_used_syms; i <= code_size_limit; i++) - for (l = num_codes[i]; l > 0; l--) - d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); - } - - next_code[1] = 0; - for (j = 0, i = 2; i <= code_size_limit; i++) - next_code[i] = j = ((j + num_codes[i - 1]) << 1); - - for (i = 0; i < table_len; i++) { - mz_uint rev_code = 0, code, code_size; - if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) continue; - code = next_code[code_size]++; - for (l = code_size; l > 0; l--, code >>= 1) - rev_code = (rev_code << 1) | (code & 1); - d->m_huff_codes[table_num][i] = (mz_uint16)rev_code; - } -} - -#define TDEFL_PUT_BITS(b, l) \ - do { \ - mz_uint bits = b; \ - mz_uint len = l; \ - MZ_ASSERT(bits <= ((1U << len) - 1U)); \ - d->m_bit_buffer |= (bits << d->m_bits_in); \ - d->m_bits_in += len; \ - while (d->m_bits_in >= 8) { \ - if (d->m_pOutput_buf < d->m_pOutput_buf_end) \ - *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \ - d->m_bit_buffer >>= 8; \ - d->m_bits_in -= 8; \ - } \ - } \ - MZ_MACRO_END - -#define TDEFL_RLE_PREV_CODE_SIZE() \ - { \ - if (rle_repeat_count) { \ - if (rle_repeat_count < 3) { \ - d->m_huff_count[2][prev_code_size] = (mz_uint16)( \ - d->m_huff_count[2][prev_code_size] + rle_repeat_count); \ - while (rle_repeat_count--) \ - packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \ - } else { \ - d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \ - packed_code_sizes[num_packed_code_sizes++] = 16; \ - packed_code_sizes[num_packed_code_sizes++] = \ - (mz_uint8)(rle_repeat_count - 3); \ - } \ - rle_repeat_count = 0; \ - } \ - } - -#define TDEFL_RLE_ZERO_CODE_SIZE() \ - { \ - if (rle_z_count) { \ - if (rle_z_count < 3) { \ - d->m_huff_count[2][0] = \ - (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \ - while (rle_z_count--) packed_code_sizes[num_packed_code_sizes++] = 0; \ - } else if (rle_z_count <= 10) { \ - d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \ - packed_code_sizes[num_packed_code_sizes++] = 17; \ - packed_code_sizes[num_packed_code_sizes++] = \ - (mz_uint8)(rle_z_count - 3); \ - } else { \ - d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \ - packed_code_sizes[num_packed_code_sizes++] = 18; \ - packed_code_sizes[num_packed_code_sizes++] = \ - (mz_uint8)(rle_z_count - 11); \ - } \ - rle_z_count = 0; \ - } \ - } - -static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; - -static void tdefl_start_dynamic_block(tdefl_compressor *d) { - int num_lit_codes, num_dist_codes, num_bit_lengths; - mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, - rle_repeat_count, packed_code_sizes_index; - mz_uint8 - code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], - packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], - prev_code_size = 0xFF; - - d->m_huff_count[0][256] = 1; - - tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE); - tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE); - - for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) - if (d->m_huff_code_sizes[0][num_lit_codes - 1]) break; - for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) - if (d->m_huff_code_sizes[1][num_dist_codes - 1]) break; - - memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes); - memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], - num_dist_codes); - total_code_sizes_to_pack = num_lit_codes + num_dist_codes; - num_packed_code_sizes = 0; - rle_z_count = 0; - rle_repeat_count = 0; - - memset(&d->m_huff_count[2][0], 0, - sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2); - for (i = 0; i < total_code_sizes_to_pack; i++) { - mz_uint8 code_size = code_sizes_to_pack[i]; - if (!code_size) { - TDEFL_RLE_PREV_CODE_SIZE(); - if (++rle_z_count == 138) { - TDEFL_RLE_ZERO_CODE_SIZE(); - } - } else { - TDEFL_RLE_ZERO_CODE_SIZE(); - if (code_size != prev_code_size) { - TDEFL_RLE_PREV_CODE_SIZE(); - d->m_huff_count[2][code_size] = - (mz_uint16)(d->m_huff_count[2][code_size] + 1); - packed_code_sizes[num_packed_code_sizes++] = code_size; - } else if (++rle_repeat_count == 6) { - TDEFL_RLE_PREV_CODE_SIZE(); - } - } - prev_code_size = code_size; - } - if (rle_repeat_count) { - TDEFL_RLE_PREV_CODE_SIZE(); - } else { - TDEFL_RLE_ZERO_CODE_SIZE(); - } - - tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE); - - TDEFL_PUT_BITS(2, 2); - - TDEFL_PUT_BITS(num_lit_codes - 257, 5); - TDEFL_PUT_BITS(num_dist_codes - 1, 5); - - for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) - if (d->m_huff_code_sizes - [2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) - break; - num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1)); - TDEFL_PUT_BITS(num_bit_lengths - 4, 4); - for (i = 0; (int)i < num_bit_lengths; i++) - TDEFL_PUT_BITS( - d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); - - for (packed_code_sizes_index = 0; - packed_code_sizes_index < num_packed_code_sizes;) { - mz_uint code = packed_code_sizes[packed_code_sizes_index++]; - MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2); - TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); - if (code >= 16) - TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], - "\02\03\07"[code - 16]); - } -} - -static void tdefl_start_static_block(tdefl_compressor *d) { - mz_uint i; - mz_uint8 *p = &d->m_huff_code_sizes[0][0]; - - for (i = 0; i <= 143; ++i) *p++ = 8; - for (; i <= 255; ++i) *p++ = 9; - for (; i <= 279; ++i) *p++ = 7; - for (; i <= 287; ++i) *p++ = 8; - - memset(d->m_huff_code_sizes[1], 5, 32); - - tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE); - tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE); - - TDEFL_PUT_BITS(1, 2); -} - -static const mz_uint mz_bitmasks[17] = { - 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, - 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF}; - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && \ - MINIZ_HAS_64BIT_REGISTERS -static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) { - mz_uint flags; - mz_uint8 *pLZ_codes; - mz_uint8 *pOutput_buf = d->m_pOutput_buf; - mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf; - mz_uint64 bit_buffer = d->m_bit_buffer; - mz_uint bits_in = d->m_bits_in; - -#define TDEFL_PUT_BITS_FAST(b, l) \ - { \ - bit_buffer |= (((mz_uint64)(b)) << bits_in); \ - bits_in += (l); \ - } - - flags = 1; - for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; - flags >>= 1) { - if (flags == 1) flags = *pLZ_codes++ | 0x100; - - if (flags & 1) { - mz_uint s0, s1, n0, n1, sym, num_extra_bits; - mz_uint match_len = pLZ_codes[0], - match_dist = *(const mz_uint16 *)(pLZ_codes + 1); - pLZ_codes += 3; - - MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], - d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); - TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], - s_tdefl_len_extra[match_len]); - - // This sequence coaxes MSVC into using cmov's vs. jmp's. - s0 = s_tdefl_small_dist_sym[match_dist & 511]; - n0 = s_tdefl_small_dist_extra[match_dist & 511]; - s1 = s_tdefl_large_dist_sym[match_dist >> 8]; - n1 = s_tdefl_large_dist_extra[match_dist >> 8]; - sym = (match_dist < 512) ? s0 : s1; - num_extra_bits = (match_dist < 512) ? n0 : n1; - - MZ_ASSERT(d->m_huff_code_sizes[1][sym]); - TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], - d->m_huff_code_sizes[1][sym]); - TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], - num_extra_bits); - } else { - mz_uint lit = *pLZ_codes++; - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], - d->m_huff_code_sizes[0][lit]); - - if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { - flags >>= 1; - lit = *pLZ_codes++; - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], - d->m_huff_code_sizes[0][lit]); - - if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { - flags >>= 1; - lit = *pLZ_codes++; - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], - d->m_huff_code_sizes[0][lit]); - } - } - } - - if (pOutput_buf >= d->m_pOutput_buf_end) return MZ_FALSE; - - *(mz_uint64 *)pOutput_buf = bit_buffer; - pOutput_buf += (bits_in >> 3); - bit_buffer >>= (bits_in & ~7); - bits_in &= 7; - } - -#undef TDEFL_PUT_BITS_FAST - - d->m_pOutput_buf = pOutput_buf; - d->m_bits_in = 0; - d->m_bit_buffer = 0; - - while (bits_in) { - mz_uint32 n = MZ_MIN(bits_in, 16); - TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n); - bit_buffer >>= n; - bits_in -= n; - } - - TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); - - return (d->m_pOutput_buf < d->m_pOutput_buf_end); -} -#else -static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) { - mz_uint flags; - mz_uint8 *pLZ_codes; - - flags = 1; - for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; - flags >>= 1) { - if (flags == 1) flags = *pLZ_codes++ | 0x100; - if (flags & 1) { - mz_uint sym, num_extra_bits; - mz_uint match_len = pLZ_codes[0], - match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); - pLZ_codes += 3; - - MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); - TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], - d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); - TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], - s_tdefl_len_extra[match_len]); - - if (match_dist < 512) { - sym = s_tdefl_small_dist_sym[match_dist]; - num_extra_bits = s_tdefl_small_dist_extra[match_dist]; - } else { - sym = s_tdefl_large_dist_sym[match_dist >> 8]; - num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8]; - } - MZ_ASSERT(d->m_huff_code_sizes[1][sym]); - TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); - TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); - } else { - mz_uint lit = *pLZ_codes++; - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); - TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); - } - } - - TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); - - return (d->m_pOutput_buf < d->m_pOutput_buf_end); -} -#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && - // MINIZ_HAS_64BIT_REGISTERS - -static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) { - if (static_block) - tdefl_start_static_block(d); - else - tdefl_start_dynamic_block(d); - return tdefl_compress_lz_codes(d); -} - -static int tdefl_flush_block(tdefl_compressor *d, int flush) { - mz_uint saved_bit_buf, saved_bits_in; - mz_uint8 *pSaved_output_buf; - mz_bool comp_block_succeeded = MZ_FALSE; - int n, use_raw_block = - ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && - (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; - mz_uint8 *pOutput_buf_start = - ((d->m_pPut_buf_func == NULL) && - ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) - ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) - : d->m_output_buf; - - d->m_pOutput_buf = pOutput_buf_start; - d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16; - - MZ_ASSERT(!d->m_output_flush_remaining); - d->m_output_flush_ofs = 0; - d->m_output_flush_remaining = 0; - - *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left); - d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); - - if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) { - TDEFL_PUT_BITS(0x78, 8); - TDEFL_PUT_BITS(0x01, 8); - } - - TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); - - pSaved_output_buf = d->m_pOutput_buf; - saved_bit_buf = d->m_bit_buffer; - saved_bits_in = d->m_bits_in; - - if (!use_raw_block) - comp_block_succeeded = - tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || - (d->m_total_lz_bytes < 48)); - - // If the block gets expanded, forget the current contents of the output - // buffer and send a raw block instead. - if (((use_raw_block) || - ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= - d->m_total_lz_bytes))) && - ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) { - mz_uint i; - d->m_pOutput_buf = pSaved_output_buf; - d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; - TDEFL_PUT_BITS(0, 2); - if (d->m_bits_in) { - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); - } - for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) { - TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); - } - for (i = 0; i < d->m_total_lz_bytes; ++i) { - TDEFL_PUT_BITS( - d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], - 8); - } - } - // Check for the extremely unlikely (if not impossible) case of the compressed - // block not fitting into the output buffer when using dynamic codes. - else if (!comp_block_succeeded) { - d->m_pOutput_buf = pSaved_output_buf; - d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; - tdefl_compress_block(d, MZ_TRUE); - } - - if (flush) { - if (flush == TDEFL_FINISH) { - if (d->m_bits_in) { - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); - } - if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) { - mz_uint i, a = d->m_adler32; - for (i = 0; i < 4; i++) { - TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); - a <<= 8; - } - } - } else { - mz_uint i, z = 0; - TDEFL_PUT_BITS(0, 3); - if (d->m_bits_in) { - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); - } - for (i = 2; i; --i, z ^= 0xFFFF) { - TDEFL_PUT_BITS(z & 0xFFFF, 16); - } - } - } - - MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end); - - memset(&d->m_huff_count[0][0], 0, - sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); - memset(&d->m_huff_count[1][0], 0, - sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); - - d->m_pLZ_code_buf = d->m_lz_code_buf + 1; - d->m_pLZ_flags = d->m_lz_code_buf; - d->m_num_flags_left = 8; - d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes; - d->m_total_lz_bytes = 0; - d->m_block_index++; - - if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) { - if (d->m_pPut_buf_func) { - *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; - if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user)) - return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED); - } else if (pOutput_buf_start == d->m_output_buf) { - int bytes_to_copy = (int)MZ_MIN( - (size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs)); - memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, - bytes_to_copy); - d->m_out_buf_ofs += bytes_to_copy; - if ((n -= bytes_to_copy) != 0) { - d->m_output_flush_ofs = bytes_to_copy; - d->m_output_flush_remaining = n; - } - } else { - d->m_out_buf_ofs += n; - } - } - - return d->m_output_flush_remaining; -} - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES -#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p) -static MZ_FORCEINLINE void tdefl_find_match( - tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, - mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) { - mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, - match_len = *pMatch_len, probe_pos = pos, next_probe_pos, - probe_len; - mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; - const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q; - mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), - s01 = TDEFL_READ_UNALIGNED_WORD(s); - MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); - if (max_match_len <= match_len) return; - for (;;) { - for (;;) { - if (--num_probes_left == 0) return; -#define TDEFL_PROBE \ - next_probe_pos = d->m_next[probe_pos]; \ - if ((!next_probe_pos) || \ - ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \ - return; \ - probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ - if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \ - break; - TDEFL_PROBE; - TDEFL_PROBE; - TDEFL_PROBE; - } - if (!dist) break; - q = (const mz_uint16 *)(d->m_dict + probe_pos); - if (TDEFL_READ_UNALIGNED_WORD(q) != s01) continue; - p = s; - probe_len = 32; - do { - } while ( - (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && - (--probe_len > 0)); - if (!probe_len) { - *pMatch_dist = dist; - *pMatch_len = MZ_MIN(max_match_len, TDEFL_MAX_MATCH_LEN); - break; - } else if ((probe_len = ((mz_uint)(p - s) * 2) + - (mz_uint)(*(const mz_uint8 *)p == - *(const mz_uint8 *)q)) > match_len) { - *pMatch_dist = dist; - if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == - max_match_len) - break; - c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]); - } - } -} -#else -static MZ_FORCEINLINE void tdefl_find_match( - tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, - mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) { - mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, - match_len = *pMatch_len, probe_pos = pos, next_probe_pos, - probe_len; - mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; - const mz_uint8 *s = d->m_dict + pos, *p, *q; - mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1]; - MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); - if (max_match_len <= match_len) return; - for (;;) { - for (;;) { - if (--num_probes_left == 0) return; -#define TDEFL_PROBE \ - next_probe_pos = d->m_next[probe_pos]; \ - if ((!next_probe_pos) || \ - ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \ - return; \ - probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ - if ((d->m_dict[probe_pos + match_len] == c0) && \ - (d->m_dict[probe_pos + match_len - 1] == c1)) \ - break; - TDEFL_PROBE; - TDEFL_PROBE; - TDEFL_PROBE; - } - if (!dist) break; - p = s; - q = d->m_dict + probe_pos; - for (probe_len = 0; probe_len < max_match_len; probe_len++) - if (*p++ != *q++) break; - if (probe_len > match_len) { - *pMatch_dist = dist; - if ((*pMatch_len = match_len = probe_len) == max_match_len) return; - c0 = d->m_dict[pos + match_len]; - c1 = d->m_dict[pos + match_len - 1]; - } - } -} -#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN -static mz_bool tdefl_compress_fast(tdefl_compressor *d) { - // Faster, minimally featured LZRW1-style match+parse loop with better - // register utilization. Intended for applications where raw throughput is - // valued more highly than ratio. - mz_uint lookahead_pos = d->m_lookahead_pos, - lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, - total_lz_bytes = d->m_total_lz_bytes, - num_flags_left = d->m_num_flags_left; - mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags; - mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; - - while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) { - const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096; - mz_uint dst_pos = - (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; - mz_uint num_bytes_to_process = (mz_uint)MZ_MIN( - d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size); - d->m_src_buf_left -= num_bytes_to_process; - lookahead_size += num_bytes_to_process; - - while (num_bytes_to_process) { - mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process); - memcpy(d->m_dict + dst_pos, d->m_pSrc, n); - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) - memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, - MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos)); - d->m_pSrc += n; - dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK; - num_bytes_to_process -= n; - } - - dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size); - if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE)) - break; - - while (lookahead_size >= 4) { - mz_uint cur_match_dist, cur_match_len = 1; - mz_uint8 *pCur_dict = d->m_dict + cur_pos; - mz_uint first_trigram = (*(const mz_uint32 *)pCur_dict) & 0xFFFFFF; - mz_uint hash = - (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & - TDEFL_LEVEL1_HASH_SIZE_MASK; - mz_uint probe_pos = d->m_hash[hash]; - d->m_hash[hash] = (mz_uint16)lookahead_pos; - - if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= - dict_size) && - ((*(const mz_uint32 *)(d->m_dict + - (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & - 0xFFFFFF) == first_trigram)) { - const mz_uint16 *p = (const mz_uint16 *)pCur_dict; - const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos); - mz_uint32 probe_len = 32; - do { - } while ((TDEFL_READ_UNALIGNED_WORD(++p) == - TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == - TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == - TDEFL_READ_UNALIGNED_WORD(++q)) && - (TDEFL_READ_UNALIGNED_WORD(++p) == - TDEFL_READ_UNALIGNED_WORD(++q)) && - (--probe_len > 0)); - cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + - (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q); - if (!probe_len) - cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0; - - if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || - ((cur_match_len == TDEFL_MIN_MATCH_LEN) && - (cur_match_dist >= 8U * 1024U))) { - cur_match_len = 1; - *pLZ_code_buf++ = (mz_uint8)first_trigram; - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); - d->m_huff_count[0][(mz_uint8)first_trigram]++; - } else { - mz_uint32 s0, s1; - cur_match_len = MZ_MIN(cur_match_len, lookahead_size); - - MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && - (cur_match_dist >= 1) && - (cur_match_dist <= TDEFL_LZ_DICT_SIZE)); - - cur_match_dist--; - - pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN); - *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist; - pLZ_code_buf += 3; - *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80); - - s0 = s_tdefl_small_dist_sym[cur_match_dist & 511]; - s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8]; - d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++; - - d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - - TDEFL_MIN_MATCH_LEN]]++; - } - } else { - *pLZ_code_buf++ = (mz_uint8)first_trigram; - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); - d->m_huff_count[0][(mz_uint8)first_trigram]++; - } - - if (--num_flags_left == 0) { - num_flags_left = 8; - pLZ_flags = pLZ_code_buf++; - } - - total_lz_bytes += cur_match_len; - lookahead_pos += cur_match_len; - dict_size = MZ_MIN(dict_size + cur_match_len, TDEFL_LZ_DICT_SIZE); - cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK; - MZ_ASSERT(lookahead_size >= cur_match_len); - lookahead_size -= cur_match_len; - - if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) { - int n; - d->m_lookahead_pos = lookahead_pos; - d->m_lookahead_size = lookahead_size; - d->m_dict_size = dict_size; - d->m_total_lz_bytes = total_lz_bytes; - d->m_pLZ_code_buf = pLZ_code_buf; - d->m_pLZ_flags = pLZ_flags; - d->m_num_flags_left = num_flags_left; - if ((n = tdefl_flush_block(d, 0)) != 0) - return (n < 0) ? MZ_FALSE : MZ_TRUE; - total_lz_bytes = d->m_total_lz_bytes; - pLZ_code_buf = d->m_pLZ_code_buf; - pLZ_flags = d->m_pLZ_flags; - num_flags_left = d->m_num_flags_left; - } - } - - while (lookahead_size) { - mz_uint8 lit = d->m_dict[cur_pos]; - - total_lz_bytes++; - *pLZ_code_buf++ = lit; - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); - if (--num_flags_left == 0) { - num_flags_left = 8; - pLZ_flags = pLZ_code_buf++; - } - - d->m_huff_count[0][lit]++; - - lookahead_pos++; - dict_size = MZ_MIN(dict_size + 1, TDEFL_LZ_DICT_SIZE); - cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; - lookahead_size--; - - if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) { - int n; - d->m_lookahead_pos = lookahead_pos; - d->m_lookahead_size = lookahead_size; - d->m_dict_size = dict_size; - d->m_total_lz_bytes = total_lz_bytes; - d->m_pLZ_code_buf = pLZ_code_buf; - d->m_pLZ_flags = pLZ_flags; - d->m_num_flags_left = num_flags_left; - if ((n = tdefl_flush_block(d, 0)) != 0) - return (n < 0) ? MZ_FALSE : MZ_TRUE; - total_lz_bytes = d->m_total_lz_bytes; - pLZ_code_buf = d->m_pLZ_code_buf; - pLZ_flags = d->m_pLZ_flags; - num_flags_left = d->m_num_flags_left; - } - } - } - - d->m_lookahead_pos = lookahead_pos; - d->m_lookahead_size = lookahead_size; - d->m_dict_size = dict_size; - d->m_total_lz_bytes = total_lz_bytes; - d->m_pLZ_code_buf = pLZ_code_buf; - d->m_pLZ_flags = pLZ_flags; - d->m_num_flags_left = num_flags_left; - return MZ_TRUE; -} -#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN - -static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, - mz_uint8 lit) { - d->m_total_lz_bytes++; - *d->m_pLZ_code_buf++ = lit; - *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1); - if (--d->m_num_flags_left == 0) { - d->m_num_flags_left = 8; - d->m_pLZ_flags = d->m_pLZ_code_buf++; - } - d->m_huff_count[0][lit]++; -} - -static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, - mz_uint match_len, - mz_uint match_dist) { - mz_uint32 s0, s1; - - MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && - (match_dist <= TDEFL_LZ_DICT_SIZE)); - - d->m_total_lz_bytes += match_len; - - d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN); - - match_dist -= 1; - d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF); - d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8); - d->m_pLZ_code_buf += 3; - - *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80); - if (--d->m_num_flags_left == 0) { - d->m_num_flags_left = 8; - d->m_pLZ_flags = d->m_pLZ_code_buf++; - } - - s0 = s_tdefl_small_dist_sym[match_dist & 511]; - s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127]; - d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; - - if (match_len >= TDEFL_MIN_MATCH_LEN) - d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; -} - -static mz_bool tdefl_compress_normal(tdefl_compressor *d) { - const mz_uint8 *pSrc = d->m_pSrc; - size_t src_buf_left = d->m_src_buf_left; - tdefl_flush flush = d->m_flush; - - while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) { - mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos; - // Update dictionary and hash chains. Keeps the lookahead size equal to - // TDEFL_MAX_MATCH_LEN. - if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) { - mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & - TDEFL_LZ_DICT_SIZE_MASK, - ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2; - mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] - << TDEFL_LZ_HASH_SHIFT) ^ - d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]; - mz_uint num_bytes_to_process = (mz_uint)MZ_MIN( - src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); - const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process; - src_buf_left -= num_bytes_to_process; - d->m_lookahead_size += num_bytes_to_process; - while (pSrc != pSrc_end) { - mz_uint8 c = *pSrc++; - d->m_dict[dst_pos] = c; - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) - d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; - hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); - d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; - d->m_hash[hash] = (mz_uint16)(ins_pos); - dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; - ins_pos++; - } - } else { - while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) { - mz_uint8 c = *pSrc++; - mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & - TDEFL_LZ_DICT_SIZE_MASK; - src_buf_left--; - d->m_dict[dst_pos] = c; - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) - d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; - if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) { - mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2; - mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] - << (TDEFL_LZ_HASH_SHIFT * 2)) ^ - (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] - << TDEFL_LZ_HASH_SHIFT) ^ - c) & - (TDEFL_LZ_HASH_SIZE - 1); - d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; - d->m_hash[hash] = (mz_uint16)(ins_pos); - } - } - } - d->m_dict_size = - MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size); - if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) break; - - // Simple lazy/greedy parsing state machine. - len_to_move = 1; - cur_match_dist = 0; - cur_match_len = - d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1); - cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; - if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) { - if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) { - mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK]; - cur_match_len = 0; - while (cur_match_len < d->m_lookahead_size) { - if (d->m_dict[cur_pos + cur_match_len] != c) break; - cur_match_len++; - } - if (cur_match_len < TDEFL_MIN_MATCH_LEN) - cur_match_len = 0; - else - cur_match_dist = 1; - } - } else { - tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, - d->m_lookahead_size, &cur_match_dist, &cur_match_len); - } - if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && - (cur_match_dist >= 8U * 1024U)) || - (cur_pos == cur_match_dist) || - ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) { - cur_match_dist = cur_match_len = 0; - } - if (d->m_saved_match_len) { - if (cur_match_len > d->m_saved_match_len) { - tdefl_record_literal(d, (mz_uint8)d->m_saved_lit); - if (cur_match_len >= 128) { - tdefl_record_match(d, cur_match_len, cur_match_dist); - d->m_saved_match_len = 0; - len_to_move = cur_match_len; - } else { - d->m_saved_lit = d->m_dict[cur_pos]; - d->m_saved_match_dist = cur_match_dist; - d->m_saved_match_len = cur_match_len; - } - } else { - tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist); - len_to_move = d->m_saved_match_len - 1; - d->m_saved_match_len = 0; - } - } else if (!cur_match_dist) - tdefl_record_literal(d, - d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]); - else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || - (cur_match_len >= 128)) { - tdefl_record_match(d, cur_match_len, cur_match_dist); - len_to_move = cur_match_len; - } else { - d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]; - d->m_saved_match_dist = cur_match_dist; - d->m_saved_match_len = cur_match_len; - } - // Move the lookahead forward by len_to_move bytes. - d->m_lookahead_pos += len_to_move; - MZ_ASSERT(d->m_lookahead_size >= len_to_move); - d->m_lookahead_size -= len_to_move; - d->m_dict_size = - MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE); - // Check if it's time to flush the current LZ codes to the internal output - // buffer. - if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || - ((d->m_total_lz_bytes > 31 * 1024) && - (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= - d->m_total_lz_bytes) || - (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) { - int n; - d->m_pSrc = pSrc; - d->m_src_buf_left = src_buf_left; - if ((n = tdefl_flush_block(d, 0)) != 0) - return (n < 0) ? MZ_FALSE : MZ_TRUE; - } - } - - d->m_pSrc = pSrc; - d->m_src_buf_left = src_buf_left; - return MZ_TRUE; -} - -static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d) { - if (d->m_pIn_buf_size) { - *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; - } - - if (d->m_pOut_buf_size) { - size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, - d->m_output_flush_remaining); - memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, - d->m_output_buf + d->m_output_flush_ofs, n); - d->m_output_flush_ofs += (mz_uint)n; - d->m_output_flush_remaining -= (mz_uint)n; - d->m_out_buf_ofs += n; - - *d->m_pOut_buf_size = d->m_out_buf_ofs; - } - - return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE - : TDEFL_STATUS_OKAY; -} - -tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, - size_t *pIn_buf_size, void *pOut_buf, - size_t *pOut_buf_size, tdefl_flush flush) { - if (!d) { - if (pIn_buf_size) *pIn_buf_size = 0; - if (pOut_buf_size) *pOut_buf_size = 0; - return TDEFL_STATUS_BAD_PARAM; - } - - d->m_pIn_buf = pIn_buf; - d->m_pIn_buf_size = pIn_buf_size; - d->m_pOut_buf = pOut_buf; - d->m_pOut_buf_size = pOut_buf_size; - d->m_pSrc = (const mz_uint8 *)(pIn_buf); - d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0; - d->m_out_buf_ofs = 0; - d->m_flush = flush; - - if (((d->m_pPut_buf_func != NULL) == - ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || - (d->m_prev_return_status != TDEFL_STATUS_OKAY) || - (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || - (pIn_buf_size && *pIn_buf_size && !pIn_buf) || - (pOut_buf_size && *pOut_buf_size && !pOut_buf)) { - if (pIn_buf_size) *pIn_buf_size = 0; - if (pOut_buf_size) *pOut_buf_size = 0; - return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM); - } - d->m_wants_to_finish |= (flush == TDEFL_FINISH); - - if ((d->m_output_flush_remaining) || (d->m_finished)) - return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN - if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) && - ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) && - ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | - TDEFL_RLE_MATCHES)) == 0)) { - if (!tdefl_compress_fast(d)) return d->m_prev_return_status; - } else -#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN - { - if (!tdefl_compress_normal(d)) return d->m_prev_return_status; - } - - if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && - (pIn_buf)) - d->m_adler32 = - (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, - d->m_pSrc - (const mz_uint8 *)pIn_buf); - - if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && - (!d->m_output_flush_remaining)) { - if (tdefl_flush_block(d, flush) < 0) return d->m_prev_return_status; - d->m_finished = (flush == TDEFL_FINISH); - if (flush == TDEFL_FULL_FLUSH) { - MZ_CLEAR_OBJ(d->m_hash); - MZ_CLEAR_OBJ(d->m_next); - d->m_dict_size = 0; - } - } - - return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); -} - -tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, - size_t in_buf_size, tdefl_flush flush) { - MZ_ASSERT(d->m_pPut_buf_func); - return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush); -} - -tdefl_status tdefl_init(tdefl_compressor *d, - tdefl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags) { - d->m_pPut_buf_func = pPut_buf_func; - d->m_pPut_buf_user = pPut_buf_user; - d->m_flags = (mz_uint)(flags); - d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3; - d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; - d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; - if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_OBJ(d->m_hash); - d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = - d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0; - d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = - d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0; - d->m_pLZ_code_buf = d->m_lz_code_buf + 1; - d->m_pLZ_flags = d->m_lz_code_buf; - d->m_num_flags_left = 8; - d->m_pOutput_buf = d->m_output_buf; - d->m_pOutput_buf_end = d->m_output_buf; - d->m_prev_return_status = TDEFL_STATUS_OKAY; - d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0; - d->m_adler32 = 1; - d->m_pIn_buf = NULL; - d->m_pOut_buf = NULL; - d->m_pIn_buf_size = NULL; - d->m_pOut_buf_size = NULL; - d->m_flush = TDEFL_NO_FLUSH; - d->m_pSrc = NULL; - d->m_src_buf_left = 0; - d->m_out_buf_ofs = 0; - memset(&d->m_huff_count[0][0], 0, - sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); - memset(&d->m_huff_count[1][0], 0, - sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); - return TDEFL_STATUS_OKAY; -} - -tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d) { - return d->m_prev_return_status; -} - -mz_uint32 tdefl_get_adler32(tdefl_compressor *d) { return d->m_adler32; } - -mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, - tdefl_put_buf_func_ptr pPut_buf_func, - void *pPut_buf_user, int flags) { - tdefl_compressor *pComp; - mz_bool succeeded; - if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) return MZ_FALSE; - pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); - if (!pComp) return MZ_FALSE; - succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == - TDEFL_STATUS_OKAY); - succeeded = - succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == - TDEFL_STATUS_DONE); - MZ_FREE(pComp); - return succeeded; -} - -typedef struct { - size_t m_size, m_capacity; - mz_uint8 *m_pBuf; - mz_bool m_expandable; -} tdefl_output_buffer; - -static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, - void *pUser) { - tdefl_output_buffer *p = (tdefl_output_buffer *)pUser; - size_t new_size = p->m_size + len; - if (new_size > p->m_capacity) { - size_t new_capacity = p->m_capacity; - mz_uint8 *pNew_buf; - if (!p->m_expandable) return MZ_FALSE; - do { - new_capacity = MZ_MAX(128U, new_capacity << 1U); - } while (new_size > new_capacity); - pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity); - if (!pNew_buf) return MZ_FALSE; - p->m_pBuf = pNew_buf; - p->m_capacity = new_capacity; - } - memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len); - p->m_size = new_size; - return MZ_TRUE; -} - -void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, - size_t *pOut_len, int flags) { - tdefl_output_buffer out_buf; - MZ_CLEAR_OBJ(out_buf); - if (!pOut_len) - return MZ_FALSE; - else - *pOut_len = 0; - out_buf.m_expandable = MZ_TRUE; - if (!tdefl_compress_mem_to_output( - pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) - return NULL; - *pOut_len = out_buf.m_size; - return out_buf.m_pBuf; -} - -size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, - const void *pSrc_buf, size_t src_buf_len, - int flags) { - tdefl_output_buffer out_buf; - MZ_CLEAR_OBJ(out_buf); - if (!pOut_buf) return 0; - out_buf.m_pBuf = (mz_uint8 *)pOut_buf; - out_buf.m_capacity = out_buf_len; - if (!tdefl_compress_mem_to_output( - pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) - return 0; - return out_buf.m_size; -} - -#ifndef MINIZ_NO_ZLIB_APIS -static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, - 128, 256, 512, 768, 1500}; - -// level may actually range from [0,10] (10 is a "hidden" max level, where we -// want a bit more compression and it's fine if throughput to fall off a cliff -// on some files). -mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, - int strategy) { - mz_uint comp_flags = - s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | - ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); - if (window_bits > 0) comp_flags |= TDEFL_WRITE_ZLIB_HEADER; - - if (!level) - comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; - else if (strategy == MZ_FILTERED) - comp_flags |= TDEFL_FILTER_MATCHES; - else if (strategy == MZ_HUFFMAN_ONLY) - comp_flags &= ~TDEFL_MAX_PROBES_MASK; - else if (strategy == MZ_FIXED) - comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS; - else if (strategy == MZ_RLE) - comp_flags |= TDEFL_RLE_MATCHES; - - return comp_flags; -} -#endif // MINIZ_NO_ZLIB_APIS - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4204) // nonstandard extension used : non-constant - // aggregate initializer (also supported by GNU - // C and C99, so no big deal) -#pragma warning(disable : 4244) // 'initializing': conversion from '__int64' to - // 'int', possible loss of data -#pragma warning(disable : 4267) // 'argument': conversion from '__int64' to - // 'int', possible loss of data -#pragma warning(disable : 4996) // 'strdup': The POSIX name for this item is - // deprecated. Instead, use the ISO C and C++ - // conformant name: _strdup. -#endif - -// Simple PNG writer function by Alex Evans, 2011. Released into the public -// domain: https://gist.github.com/908299, more context at -// http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/. -// This is actually a modification of Alex's original code so PNG files -// generated by this function pass pngcheck. -void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, - int h, int num_chans, - size_t *pLen_out, - mz_uint level, mz_bool flip) { - // Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was - // defined. - static const mz_uint s_tdefl_png_num_probes[11] = { - 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500}; - tdefl_compressor *pComp = - (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); - tdefl_output_buffer out_buf; - int i, bpl = w * num_chans, y, z; - mz_uint32 c; - *pLen_out = 0; - if (!pComp) return NULL; - MZ_CLEAR_OBJ(out_buf); - out_buf.m_expandable = MZ_TRUE; - out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h); - if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity))) { - MZ_FREE(pComp); - return NULL; - } - // write dummy header - for (z = 41; z; --z) tdefl_output_buffer_putter(&z, 1, &out_buf); - // compress image data - tdefl_init( - pComp, tdefl_output_buffer_putter, &out_buf, - s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER); - for (y = 0; y < h; ++y) { - tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH); - tdefl_compress_buffer(pComp, - (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, - bpl, TDEFL_NO_FLUSH); - } - if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != - TDEFL_STATUS_DONE) { - MZ_FREE(pComp); - MZ_FREE(out_buf.m_pBuf); - return NULL; - } - // write real header - *pLen_out = out_buf.m_size - 41; - { - static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06}; - mz_uint8 pnghdr[41] = {0x89, - 0x50, - 0x4e, - 0x47, - 0x0d, - 0x0a, - 0x1a, - 0x0a, - 0x00, - 0x00, - 0x00, - 0x0d, - 0x49, - 0x48, - 0x44, - 0x52, - 0, - 0, - (mz_uint8)(w >> 8), - (mz_uint8)w, - 0, - 0, - (mz_uint8)(h >> 8), - (mz_uint8)h, - 8, - chans[num_chans], - 0, - 0, - 0, - 0, - 0, - 0, - 0, - (mz_uint8)(*pLen_out >> 24), - (mz_uint8)(*pLen_out >> 16), - (mz_uint8)(*pLen_out >> 8), - (mz_uint8)*pLen_out, - 0x49, - 0x44, - 0x41, - 0x54}; - c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17); - for (i = 0; i < 4; ++i, c <<= 8) - ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24); - memcpy(out_buf.m_pBuf, pnghdr, 41); - } - // write footer (IDAT CRC-32, followed by IEND chunk) - if (!tdefl_output_buffer_putter( - "\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) { - *pLen_out = 0; - MZ_FREE(pComp); - MZ_FREE(out_buf.m_pBuf); - return NULL; - } - c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, - *pLen_out + 4); - for (i = 0; i < 4; ++i, c <<= 8) - (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24); - // compute final size of file, grab compressed data buffer and return - *pLen_out += 57; - MZ_FREE(pComp); - return out_buf.m_pBuf; -} -void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, - int num_chans, size_t *pLen_out) { - // Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we - // can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's - // where #defined out) - return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, - pLen_out, 6, MZ_FALSE); -} - -// ------------------- .ZIP archive reading - -#ifndef MINIZ_NO_ARCHIVE_APIS -#error "No arvhive APIs" - -#ifdef MINIZ_NO_STDIO -#define MZ_FILE void * -#else -#include <stdio.h> -#include <sys/stat.h> - -#if defined(_MSC_VER) || defined(__MINGW64__) -static FILE *mz_fopen(const char *pFilename, const char *pMode) { - FILE *pFile = NULL; - fopen_s(&pFile, pFilename, pMode); - return pFile; -} -static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) { - FILE *pFile = NULL; - if (freopen_s(&pFile, pPath, pMode, pStream)) return NULL; - return pFile; -} -#ifndef MINIZ_NO_TIME -#include <sys/utime.h> -#endif -#define MZ_FILE FILE -#define MZ_FOPEN mz_fopen -#define MZ_FCLOSE fclose -#define MZ_FREAD fread -#define MZ_FWRITE fwrite -#define MZ_FTELL64 _ftelli64 -#define MZ_FSEEK64 _fseeki64 -#define MZ_FILE_STAT_STRUCT _stat -#define MZ_FILE_STAT _stat -#define MZ_FFLUSH fflush -#define MZ_FREOPEN mz_freopen -#define MZ_DELETE_FILE remove -#elif defined(__MINGW32__) -#ifndef MINIZ_NO_TIME -#include <sys/utime.h> -#endif -#define MZ_FILE FILE -#define MZ_FOPEN(f, m) fopen(f, m) -#define MZ_FCLOSE fclose -#define MZ_FREAD fread -#define MZ_FWRITE fwrite -#define MZ_FTELL64 ftello64 -#define MZ_FSEEK64 fseeko64 -#define MZ_FILE_STAT_STRUCT _stat -#define MZ_FILE_STAT _stat -#define MZ_FFLUSH fflush -#define MZ_FREOPEN(f, m, s) freopen(f, m, s) -#define MZ_DELETE_FILE remove -#elif defined(__TINYC__) -#ifndef MINIZ_NO_TIME -#include <sys/utime.h> -#endif -#define MZ_FILE FILE -#define MZ_FOPEN(f, m) fopen(f, m) -#define MZ_FCLOSE fclose -#define MZ_FREAD fread -#define MZ_FWRITE fwrite -#define MZ_FTELL64 ftell -#define MZ_FSEEK64 fseek -#define MZ_FILE_STAT_STRUCT stat -#define MZ_FILE_STAT stat -#define MZ_FFLUSH fflush -#define MZ_FREOPEN(f, m, s) freopen(f, m, s) -#define MZ_DELETE_FILE remove -#elif defined(__GNUC__) && defined(_LARGEFILE64_SOURCE) && _LARGEFILE64_SOURCE -#ifndef MINIZ_NO_TIME -#include <utime.h> -#endif -#define MZ_FILE FILE -#define MZ_FOPEN(f, m) fopen64(f, m) -#define MZ_FCLOSE fclose -#define MZ_FREAD fread -#define MZ_FWRITE fwrite -#define MZ_FTELL64 ftello64 -#define MZ_FSEEK64 fseeko64 -#define MZ_FILE_STAT_STRUCT stat64 -#define MZ_FILE_STAT stat64 -#define MZ_FFLUSH fflush -#define MZ_FREOPEN(p, m, s) freopen64(p, m, s) -#define MZ_DELETE_FILE remove -#else -#ifndef MINIZ_NO_TIME -#include <utime.h> -#endif -#define MZ_FILE FILE -#define MZ_FOPEN(f, m) fopen(f, m) -#define MZ_FCLOSE fclose -#define MZ_FREAD fread -#define MZ_FWRITE fwrite -#define MZ_FTELL64 ftello -#define MZ_FSEEK64 fseeko -#define MZ_FILE_STAT_STRUCT stat -#define MZ_FILE_STAT stat -#define MZ_FFLUSH fflush -#define MZ_FREOPEN(f, m, s) freopen(f, m, s) -#define MZ_DELETE_FILE remove -#endif // #ifdef _MSC_VER -#endif // #ifdef MINIZ_NO_STDIO - -#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c)) - -// Various ZIP archive enums. To completely avoid cross platform compiler -// alignment and platform endian issues, miniz.c doesn't use structs for any of -// this stuff. -enum { - // ZIP archive identifiers and record sizes - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, - MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, - MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50, - MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30, - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46, - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22, - // Central directory header record offsets - MZ_ZIP_CDH_SIG_OFS = 0, - MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4, - MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6, - MZ_ZIP_CDH_BIT_FLAG_OFS = 8, - MZ_ZIP_CDH_METHOD_OFS = 10, - MZ_ZIP_CDH_FILE_TIME_OFS = 12, - MZ_ZIP_CDH_FILE_DATE_OFS = 14, - MZ_ZIP_CDH_CRC32_OFS = 16, - MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20, - MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, - MZ_ZIP_CDH_FILENAME_LEN_OFS = 28, - MZ_ZIP_CDH_EXTRA_LEN_OFS = 30, - MZ_ZIP_CDH_COMMENT_LEN_OFS = 32, - MZ_ZIP_CDH_DISK_START_OFS = 34, - MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36, - MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38, - MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42, - // Local directory header offsets - MZ_ZIP_LDH_SIG_OFS = 0, - MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4, - MZ_ZIP_LDH_BIT_FLAG_OFS = 6, - MZ_ZIP_LDH_METHOD_OFS = 8, - MZ_ZIP_LDH_FILE_TIME_OFS = 10, - MZ_ZIP_LDH_FILE_DATE_OFS = 12, - MZ_ZIP_LDH_CRC32_OFS = 14, - MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18, - MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22, - MZ_ZIP_LDH_FILENAME_LEN_OFS = 26, - MZ_ZIP_LDH_EXTRA_LEN_OFS = 28, - // End of central directory offsets - MZ_ZIP_ECDH_SIG_OFS = 0, - MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4, - MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, - MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8, - MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, - MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12, - MZ_ZIP_ECDH_CDIR_OFS_OFS = 16, - MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20, -}; - -typedef struct { - void *m_p; - size_t m_size, m_capacity; - mz_uint m_element_size; -} mz_zip_array; - -struct mz_zip_internal_state_tag { - mz_zip_array m_central_dir; - mz_zip_array m_central_dir_offsets; - mz_zip_array m_sorted_central_dir_offsets; - MZ_FILE *m_pFile; - void *m_pMem; - size_t m_mem_size; - size_t m_mem_capacity; -}; - -#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) \ - (array_ptr)->m_element_size = element_size -#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) \ - ((element_type *)((array_ptr)->m_p))[index] - -static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, - mz_zip_array *pArray) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p); - memset(pArray, 0, sizeof(mz_zip_array)); -} - -static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, - mz_zip_array *pArray, - size_t min_new_capacity, - mz_uint growing) { - void *pNew_p; - size_t new_capacity = min_new_capacity; - MZ_ASSERT(pArray->m_element_size); - if (pArray->m_capacity >= min_new_capacity) return MZ_TRUE; - if (growing) { - new_capacity = MZ_MAX(1, pArray->m_capacity); - while (new_capacity < min_new_capacity) new_capacity *= 2; - } - if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, - pArray->m_element_size, new_capacity))) - return MZ_FALSE; - pArray->m_p = pNew_p; - pArray->m_capacity = new_capacity; - return MZ_TRUE; -} - -static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, - mz_zip_array *pArray, - size_t new_capacity, - mz_uint growing) { - if (new_capacity > pArray->m_capacity) { - if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) - return MZ_FALSE; - } - return MZ_TRUE; -} - -static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, - mz_zip_array *pArray, - size_t new_size, - mz_uint growing) { - if (new_size > pArray->m_capacity) { - if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) - return MZ_FALSE; - } - pArray->m_size = new_size; - return MZ_TRUE; -} - -static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, - mz_zip_array *pArray, - size_t n) { - return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE); -} - -static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, - mz_zip_array *pArray, - const void *pElements, - size_t n) { - size_t orig_size = pArray->m_size; - if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) - return MZ_FALSE; - memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size, - pElements, n * pArray->m_element_size); - return MZ_TRUE; -} - -#ifndef MINIZ_NO_TIME -static time_t mz_zip_dos_to_time_t(int dos_time, int dos_date) { - struct tm tm; - memset(&tm, 0, sizeof(tm)); - tm.tm_isdst = -1; - tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900; - tm.tm_mon = ((dos_date >> 5) & 15) - 1; - tm.tm_mday = dos_date & 31; - tm.tm_hour = (dos_time >> 11) & 31; - tm.tm_min = (dos_time >> 5) & 63; - tm.tm_sec = (dos_time << 1) & 62; - return mktime(&tm); -} - -static void mz_zip_time_to_dos_time(time_t time, mz_uint16 *pDOS_time, - mz_uint16 *pDOS_date) { -#ifdef _MSC_VER - struct tm tm_struct; - struct tm *tm = &tm_struct; - errno_t err = localtime_s(tm, &time); - if (err) { - *pDOS_date = 0; - *pDOS_time = 0; - return; - } -#else - struct tm *tm = localtime(&time); -#endif - *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + - ((tm->tm_sec) >> 1)); - *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + - ((tm->tm_mon + 1) << 5) + tm->tm_mday); -} -#endif - -#ifndef MINIZ_NO_STDIO -static mz_bool mz_zip_get_file_modified_time(const char *pFilename, - mz_uint16 *pDOS_time, - mz_uint16 *pDOS_date) { -#ifdef MINIZ_NO_TIME - (void)pFilename; - *pDOS_date = *pDOS_time = 0; -#else - struct MZ_FILE_STAT_STRUCT file_stat; - // On Linux with x86 glibc, this call will fail on large files (>= 0x80000000 - // bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. - if (MZ_FILE_STAT(pFilename, &file_stat) != 0) return MZ_FALSE; - mz_zip_time_to_dos_time(file_stat.st_mtime, pDOS_time, pDOS_date); -#endif // #ifdef MINIZ_NO_TIME - return MZ_TRUE; -} - -#ifndef MINIZ_NO_TIME -static mz_bool mz_zip_set_file_times(const char *pFilename, time_t access_time, - time_t modified_time) { - struct utimbuf t; - t.actime = access_time; - t.modtime = modified_time; - return !utime(pFilename, &t); -} -#endif // #ifndef MINIZ_NO_TIME -#endif // #ifndef MINIZ_NO_STDIO - -static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, - mz_uint32 flags) { - (void)flags; - if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) - return MZ_FALSE; - - if (!pZip->m_pAlloc) pZip->m_pAlloc = def_alloc_func; - if (!pZip->m_pFree) pZip->m_pFree = def_free_func; - if (!pZip->m_pRealloc) pZip->m_pRealloc = def_realloc_func; - - pZip->m_zip_mode = MZ_ZIP_MODE_READING; - pZip->m_archive_size = 0; - pZip->m_central_directory_file_ofs = 0; - pZip->m_total_files = 0; - - if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) - return MZ_FALSE; - memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, - sizeof(mz_uint8)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, - sizeof(mz_uint32)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, - sizeof(mz_uint32)); - return MZ_TRUE; -} - -static MZ_FORCEINLINE mz_bool -mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, - const mz_zip_array *pCentral_dir_offsets, - mz_uint l_index, mz_uint r_index) { - const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT( - pCentral_dir_array, mz_uint8, - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, - l_index)), - *pE; - const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT( - pCentral_dir_array, mz_uint8, - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index)); - mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), - r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS); - mz_uint8 l = 0, r = 0; - pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; - pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; - pE = pL + MZ_MIN(l_len, r_len); - while (pL < pE) { - if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) break; - pL++; - pR++; - } - return (pL == pE) ? (l_len < r_len) : (l < r); -} - -#define MZ_SWAP_UINT32(a, b) \ - do { \ - mz_uint32 t = a; \ - a = b; \ - b = t; \ - } \ - MZ_MACRO_END - -// Heap sort of lowercased filenames, used to help accelerate plain central -// directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), -// but it could allocate memory.) -static void mz_zip_reader_sort_central_dir_offsets_by_filename( - mz_zip_archive *pZip) { - mz_zip_internal_state *pState = pZip->m_pState; - const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; - const mz_zip_array *pCentral_dir = &pState->m_central_dir; - mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT( - &pState->m_sorted_central_dir_offsets, mz_uint32, 0); - const int size = pZip->m_total_files; - int start = (size - 2) >> 1, end; - while (start >= 0) { - int child, root = start; - for (;;) { - if ((child = (root << 1) + 1) >= size) break; - child += - (((child + 1) < size) && - (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, - pIndices[child], pIndices[child + 1]))); - if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, - pIndices[root], pIndices[child])) - break; - MZ_SWAP_UINT32(pIndices[root], pIndices[child]); - root = child; - } - start--; - } - - end = size - 1; - while (end > 0) { - int child, root = 0; - MZ_SWAP_UINT32(pIndices[end], pIndices[0]); - for (;;) { - if ((child = (root << 1) + 1) >= end) break; - child += - (((child + 1) < end) && - mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, - pIndices[child], pIndices[child + 1])); - if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, - pIndices[root], pIndices[child])) - break; - MZ_SWAP_UINT32(pIndices[root], pIndices[child]); - root = child; - } - end--; - } -} - -static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, - mz_uint32 flags) { - mz_uint cdir_size, num_this_disk, cdir_disk_index; - mz_uint64 cdir_ofs; - mz_int64 cur_file_ofs; - const mz_uint8 *p; - mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; - mz_uint8 *pBuf = (mz_uint8 *)buf_u32; - mz_bool sort_central_dir = - ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0); - // Basic sanity checks - reject files which are too small, and check the first - // 4 bytes of the file to make sure a local header is there. - if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) - return MZ_FALSE; - // Find the end of central directory record by scanning the file from the end - // towards the beginning. - cur_file_ofs = - MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0); - for (;;) { - int i, - n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs); - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n) - return MZ_FALSE; - for (i = n - 4; i >= 0; --i) - if (MZ_READ_LE32(pBuf + i) == MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) break; - if (i >= 0) { - cur_file_ofs += i; - break; - } - if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= - (0xFFFF + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE))) - return MZ_FALSE; - cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); - } - // Read and verify the end of central directory record. - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) - return MZ_FALSE; - if ((MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) || - ((pZip->m_total_files = - MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS)) != - MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS))) - return MZ_FALSE; - - num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS); - cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS); - if (((num_this_disk | cdir_disk_index) != 0) && - ((num_this_disk != 1) || (cdir_disk_index != 1))) - return MZ_FALSE; - - if ((cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS)) < - pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) - return MZ_FALSE; - - cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS); - if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size) return MZ_FALSE; - - pZip->m_central_directory_file_ofs = cdir_ofs; - - if (pZip->m_total_files) { - mz_uint i, n; - - // Read the entire central directory into a heap block, and allocate another - // heap block to hold the unsorted central dir file record offsets, and - // another to hold the sorted indices. - if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, - MZ_FALSE)) || - (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, - pZip->m_total_files, MZ_FALSE))) - return MZ_FALSE; - - if (sort_central_dir) { - if (!mz_zip_array_resize(pZip, - &pZip->m_pState->m_sorted_central_dir_offsets, - pZip->m_total_files, MZ_FALSE)) - return MZ_FALSE; - } - - if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, - pZip->m_pState->m_central_dir.m_p, - cdir_size) != cdir_size) - return MZ_FALSE; - - // Now create an index into the central directory file records, do some - // basic sanity checking on each record, and check for zip64 entries (which - // are not yet supported). - p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p; - for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) { - mz_uint total_header_size, comp_size, decomp_size, disk_index; - if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || - (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) - return MZ_FALSE; - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, - i) = - (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p); - if (sort_central_dir) - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, - mz_uint32, i) = i; - comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); - decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); - if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && - (decomp_size != comp_size)) || - (decomp_size && !comp_size) || (decomp_size == 0xFFFFFFFF) || - (comp_size == 0xFFFFFFFF)) - return MZ_FALSE; - disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS); - if ((disk_index != num_this_disk) && (disk_index != 1)) return MZ_FALSE; - if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + - MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size) - return MZ_FALSE; - if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + - MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + - MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > - n) - return MZ_FALSE; - n -= total_header_size; - p += total_header_size; - } - } - - if (sort_central_dir) - mz_zip_reader_sort_central_dir_offsets_by_filename(pZip); - - return MZ_TRUE; -} - -mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, - mz_uint32 flags) { - if ((!pZip) || (!pZip->m_pRead)) return MZ_FALSE; - if (!mz_zip_reader_init_internal(pZip, flags)) return MZ_FALSE; - pZip->m_archive_size = size; - if (!mz_zip_reader_read_central_dir(pZip, flags)) { - mz_zip_reader_end(pZip); - return MZ_FALSE; - } - return MZ_TRUE; -} - -static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, - void *pBuf, size_t n) { - mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; - size_t s = (file_ofs >= pZip->m_archive_size) - ? 0 - : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n); - memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s); - return s; -} - -mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, - size_t size, mz_uint32 flags) { - if (!mz_zip_reader_init_internal(pZip, flags)) return MZ_FALSE; - pZip->m_archive_size = size; - pZip->m_pRead = mz_zip_mem_read_func; - pZip->m_pIO_opaque = pZip; -#ifdef __cplusplus - pZip->m_pState->m_pMem = const_cast<void *>(pMem); -#else - pZip->m_pState->m_pMem = (void *)pMem; -#endif - pZip->m_pState->m_mem_size = size; - if (!mz_zip_reader_read_central_dir(pZip, flags)) { - mz_zip_reader_end(pZip); - return MZ_FALSE; - } - return MZ_TRUE; -} - -#ifndef MINIZ_NO_STDIO -static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, - void *pBuf, size_t n) { - mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; - mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); - if (((mz_int64)file_ofs < 0) || - (((cur_ofs != (mz_int64)file_ofs)) && - (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) - return 0; - return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile); -} - -mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, - mz_uint32 flags) { - mz_uint64 file_size; - MZ_FILE *pFile = MZ_FOPEN(pFilename, "rb"); - if (!pFile) return MZ_FALSE; - if (MZ_FSEEK64(pFile, 0, SEEK_END)) { - MZ_FCLOSE(pFile); - return MZ_FALSE; - } - file_size = MZ_FTELL64(pFile); - if (!mz_zip_reader_init_internal(pZip, flags)) { - MZ_FCLOSE(pFile); - return MZ_FALSE; - } - pZip->m_pRead = mz_zip_file_read_func; - pZip->m_pIO_opaque = pZip; - pZip->m_pState->m_pFile = pFile; - pZip->m_archive_size = file_size; - if (!mz_zip_reader_read_central_dir(pZip, flags)) { - mz_zip_reader_end(pZip); - return MZ_FALSE; - } - return MZ_TRUE; -} -#endif // #ifndef MINIZ_NO_STDIO - -mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip) { - return pZip ? pZip->m_total_files : 0; -} - -static MZ_FORCEINLINE const mz_uint8 *mz_zip_reader_get_cdh( - mz_zip_archive *pZip, mz_uint file_index) { - if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files) || - (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) - return NULL; - return &MZ_ZIP_ARRAY_ELEMENT( - &pZip->m_pState->m_central_dir, mz_uint8, - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, - file_index)); -} - -mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, - mz_uint file_index) { - mz_uint m_bit_flag; - const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); - if (!p) return MZ_FALSE; - m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); - return (m_bit_flag & 1); -} - -mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, - mz_uint file_index) { - mz_uint filename_len, external_attr; - const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); - if (!p) return MZ_FALSE; - - // First see if the filename ends with a '/' character. - filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); - if (filename_len) { - if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/') - return MZ_TRUE; - } - - // Bugfix: This code was also checking if the internal attribute was non-zero, - // which wasn't correct. - // Most/all zip writers (hopefully) set DOS file/directory attributes in the - // low 16-bits, so check for the DOS directory flag and ignore the source OS - // ID in the created by field. - // FIXME: Remove this check? Is it necessary - we already check the filename. - external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); - if ((external_attr & 0x10) != 0) return MZ_TRUE; - - return MZ_FALSE; -} - -mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, - mz_zip_archive_file_stat *pStat) { - mz_uint n; - const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); - if ((!p) || (!pStat)) return MZ_FALSE; - - // Unpack the central directory record. - pStat->m_file_index = file_index; - pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT( - &pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index); - pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS); - pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS); - pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); - pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); -#ifndef MINIZ_NO_TIME - pStat->m_time = - mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), - MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS)); -#endif - pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS); - pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); - pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); - pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS); - pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); - pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); - - // Copy as much of the filename and comment as possible. - n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); - n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1); - memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); - pStat->m_filename[n] = '\0'; - - n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS); - n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1); - pStat->m_comment_size = n; - memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + - MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), - n); - pStat->m_comment[n] = '\0'; - - return MZ_TRUE; -} - -mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, - char *pFilename, mz_uint filename_buf_size) { - mz_uint n; - const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); - if (!p) { - if (filename_buf_size) pFilename[0] = '\0'; - return 0; - } - n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); - if (filename_buf_size) { - n = MZ_MIN(n, filename_buf_size - 1); - memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); - pFilename[n] = '\0'; - } - return n + 1; -} - -static MZ_FORCEINLINE mz_bool mz_zip_reader_string_equal(const char *pA, - const char *pB, - mz_uint len, - mz_uint flags) { - mz_uint i; - if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) return 0 == memcmp(pA, pB, len); - for (i = 0; i < len; ++i) - if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) return MZ_FALSE; - return MZ_TRUE; -} - -static MZ_FORCEINLINE int mz_zip_reader_filename_compare( - const mz_zip_array *pCentral_dir_array, - const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, - mz_uint r_len) { - const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT( - pCentral_dir_array, mz_uint8, - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, - l_index)), - *pE; - mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS); - mz_uint8 l = 0, r = 0; - pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; - pE = pL + MZ_MIN(l_len, r_len); - while (pL < pE) { - if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) break; - pL++; - pR++; - } - return (pL == pE) ? (int)(l_len - r_len) : (l - r); -} - -static int mz_zip_reader_locate_file_binary_search(mz_zip_archive *pZip, - const char *pFilename) { - mz_zip_internal_state *pState = pZip->m_pState; - const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; - const mz_zip_array *pCentral_dir = &pState->m_central_dir; - mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT( - &pState->m_sorted_central_dir_offsets, mz_uint32, 0); - const int size = pZip->m_total_files; - const mz_uint filename_len = (mz_uint)strlen(pFilename); - int l = 0, h = size - 1; - while (l <= h) { - int m = (l + h) >> 1, file_index = pIndices[m], - comp = - mz_zip_reader_filename_compare(pCentral_dir, pCentral_dir_offsets, - file_index, pFilename, filename_len); - if (!comp) - return file_index; - else if (comp < 0) - l = m + 1; - else - h = m - 1; - } - return -1; -} - -int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, - const char *pComment, mz_uint flags) { - mz_uint file_index; - size_t name_len, comment_len; - if ((!pZip) || (!pZip->m_pState) || (!pName) || - (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) - return -1; - if (((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && - (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) - return mz_zip_reader_locate_file_binary_search(pZip, pName); - name_len = strlen(pName); - if (name_len > 0xFFFF) return -1; - comment_len = pComment ? strlen(pComment) : 0; - if (comment_len > 0xFFFF) return -1; - for (file_index = 0; file_index < pZip->m_total_files; file_index++) { - const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT( - &pZip->m_pState->m_central_dir, mz_uint8, - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, - file_index)); - mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS); - const char *pFilename = - (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; - if (filename_len < name_len) continue; - if (comment_len) { - mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), - file_comment_len = - MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS); - const char *pFile_comment = pFilename + filename_len + file_extra_len; - if ((file_comment_len != comment_len) || - (!mz_zip_reader_string_equal(pComment, pFile_comment, - file_comment_len, flags))) - continue; - } - if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) { - int ofs = filename_len - 1; - do { - if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || - (pFilename[ofs] == ':')) - break; - } while (--ofs >= 0); - ofs++; - pFilename += ofs; - filename_len -= ofs; - } - if ((filename_len == name_len) && - (mz_zip_reader_string_equal(pName, pFilename, filename_len, flags))) - return file_index; - } - return -1; -} - -mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, - mz_uint file_index, void *pBuf, - size_t buf_size, mz_uint flags, - void *pUser_read_buf, - size_t user_read_buf_size) { - int status = TINFL_STATUS_DONE; - mz_uint64 needed_size, cur_file_ofs, comp_remaining, - out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; - mz_zip_archive_file_stat file_stat; - void *pRead_buf; - mz_uint32 - local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / - sizeof(mz_uint32)]; - mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; - tinfl_decompressor inflator; - - if ((buf_size) && (!pBuf)) return MZ_FALSE; - - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; - - // Empty file, or a directory (but not always a directory - I've seen odd zips - // with directories that have compressed data which inflates to 0 bytes) - if (!file_stat.m_comp_size) return MZ_TRUE; - - // Entry is a subdirectory (I've seen old zips with dir entries which have - // compressed deflate data which inflates to 0 bytes, but these entries claim - // to uncompress to 512 bytes in the headers). - // I'm torn how to handle this case - should it fail instead? - if (mz_zip_reader_is_file_a_directory(pZip, file_index)) return MZ_TRUE; - - // Encryption and patch files are not supported. - if (file_stat.m_bit_flag & (1 | 32)) return MZ_FALSE; - - // This function only supports stored and deflate. - if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && - (file_stat.m_method != MZ_DEFLATED)) - return MZ_FALSE; - - // Ensure supplied output buffer is large enough. - needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size - : file_stat.m_uncomp_size; - if (buf_size < needed_size) return MZ_FALSE; - - // Read and parse the local directory entry. - cur_file_ofs = file_stat.m_local_header_ofs; - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) - return MZ_FALSE; - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) - return MZ_FALSE; - - cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); - if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) - return MZ_FALSE; - - if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) { - // The file is stored or the caller has requested the compressed data. - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, - (size_t)needed_size) != needed_size) - return MZ_FALSE; - return ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) != 0) || - (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, - (size_t)file_stat.m_uncomp_size) == file_stat.m_crc32); - } - - // Decompress the file either directly from memory or from a file input - // buffer. - tinfl_init(&inflator); - - if (pZip->m_pState->m_pMem) { - // Read directly from the archive in memory. - pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; - read_buf_size = read_buf_avail = file_stat.m_comp_size; - comp_remaining = 0; - } else if (pUser_read_buf) { - // Use a user provided read buffer. - if (!user_read_buf_size) return MZ_FALSE; - pRead_buf = (mz_uint8 *)pUser_read_buf; - read_buf_size = user_read_buf_size; - read_buf_avail = 0; - comp_remaining = file_stat.m_comp_size; - } else { - // Temporarily allocate a read buffer. - read_buf_size = - MZ_MIN(file_stat.m_comp_size, (mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE); -#ifdef _MSC_VER - if (((0, sizeof(size_t) == sizeof(mz_uint32))) && - (read_buf_size > 0x7FFFFFFF)) -#else - if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) -#endif - return MZ_FALSE; - if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, - (size_t)read_buf_size))) - return MZ_FALSE; - read_buf_avail = 0; - comp_remaining = file_stat.m_comp_size; - } - - do { - size_t in_buf_size, - out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs); - if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) { - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, - (size_t)read_buf_avail) != read_buf_avail) { - status = TINFL_STATUS_FAILED; - break; - } - cur_file_ofs += read_buf_avail; - comp_remaining -= read_buf_avail; - read_buf_ofs = 0; - } - in_buf_size = (size_t)read_buf_avail; - status = tinfl_decompress( - &inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, - (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | - (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0)); - read_buf_avail -= in_buf_size; - read_buf_ofs += in_buf_size; - out_buf_ofs += out_buf_size; - } while (status == TINFL_STATUS_NEEDS_MORE_INPUT); - - if (status == TINFL_STATUS_DONE) { - // Make sure the entire file was decompressed, and check its CRC. - if ((out_buf_ofs != file_stat.m_uncomp_size) || - (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, - (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)) - status = TINFL_STATUS_FAILED; - } - - if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - - return status == TINFL_STATUS_DONE; -} - -mz_bool mz_zip_reader_extract_file_to_mem_no_alloc( - mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, - mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) { - int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); - if (file_index < 0) return MZ_FALSE; - return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, - flags, pUser_read_buf, - user_read_buf_size); -} - -mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, - void *pBuf, size_t buf_size, - mz_uint flags) { - return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, - flags, NULL, 0); -} - -mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, - const char *pFilename, void *pBuf, - size_t buf_size, mz_uint flags) { - return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, - buf_size, flags, NULL, 0); -} - -void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, - size_t *pSize, mz_uint flags) { - mz_uint64 comp_size, uncomp_size, alloc_size; - const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); - void *pBuf; - - if (pSize) *pSize = 0; - if (!p) return NULL; - - comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); - uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); - - alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; -#ifdef _MSC_VER - if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) -#else - if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) -#endif - return NULL; - if (NULL == - (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) - return NULL; - - if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, - flags)) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - return NULL; - } - - if (pSize) *pSize = (size_t)alloc_size; - return pBuf; -} - -void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, - const char *pFilename, size_t *pSize, - mz_uint flags) { - int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); - if (file_index < 0) { - if (pSize) *pSize = 0; - return MZ_FALSE; - } - return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags); -} - -mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, - mz_uint file_index, - mz_file_write_func pCallback, - void *pOpaque, mz_uint flags) { - int status = TINFL_STATUS_DONE; - mz_uint file_crc32 = MZ_CRC32_INIT; - mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, - out_buf_ofs = 0, cur_file_ofs; - mz_zip_archive_file_stat file_stat; - void *pRead_buf = NULL; - void *pWrite_buf = NULL; - mz_uint32 - local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / - sizeof(mz_uint32)]; - mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; - - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; - - // Empty file, or a directory (but not always a directory - I've seen odd zips - // with directories that have compressed data which inflates to 0 bytes) - if (!file_stat.m_comp_size) return MZ_TRUE; - - // Entry is a subdirectory (I've seen old zips with dir entries which have - // compressed deflate data which inflates to 0 bytes, but these entries claim - // to uncompress to 512 bytes in the headers). - // I'm torn how to handle this case - should it fail instead? - if (mz_zip_reader_is_file_a_directory(pZip, file_index)) return MZ_TRUE; - - // Encryption and patch files are not supported. - if (file_stat.m_bit_flag & (1 | 32)) return MZ_FALSE; - - // This function only supports stored and deflate. - if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && - (file_stat.m_method != MZ_DEFLATED)) - return MZ_FALSE; - - // Read and parse the local directory entry. - cur_file_ofs = file_stat.m_local_header_ofs; - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) - return MZ_FALSE; - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) - return MZ_FALSE; - - cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); - if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) - return MZ_FALSE; - - // Decompress the file either directly from memory or from a file input - // buffer. - if (pZip->m_pState->m_pMem) { - pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; - read_buf_size = read_buf_avail = file_stat.m_comp_size; - comp_remaining = 0; - } else { - read_buf_size = - MZ_MIN(file_stat.m_comp_size, (mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE); - if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, - (size_t)read_buf_size))) - return MZ_FALSE; - read_buf_avail = 0; - comp_remaining = file_stat.m_comp_size; - } - - if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) { - // The file is stored or the caller has requested the compressed data. - if (pZip->m_pState->m_pMem) { -#ifdef _MSC_VER - if (((0, sizeof(size_t) == sizeof(mz_uint32))) && - (file_stat.m_comp_size > 0xFFFFFFFF)) -#else - if (((sizeof(size_t) == sizeof(mz_uint32))) && - (file_stat.m_comp_size > 0xFFFFFFFF)) -#endif - return MZ_FALSE; - if (pCallback(pOpaque, out_buf_ofs, pRead_buf, - (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) - status = TINFL_STATUS_FAILED; - else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) - file_crc32 = - (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, - (size_t)file_stat.m_comp_size); - cur_file_ofs += file_stat.m_comp_size; - out_buf_ofs += file_stat.m_comp_size; - comp_remaining = 0; - } else { - while (comp_remaining) { - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, - (size_t)read_buf_avail) != read_buf_avail) { - status = TINFL_STATUS_FAILED; - break; - } - - if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) - file_crc32 = (mz_uint32)mz_crc32( - file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail); - - if (pCallback(pOpaque, out_buf_ofs, pRead_buf, - (size_t)read_buf_avail) != read_buf_avail) { - status = TINFL_STATUS_FAILED; - break; - } - cur_file_ofs += read_buf_avail; - out_buf_ofs += read_buf_avail; - comp_remaining -= read_buf_avail; - } - } - } else { - tinfl_decompressor inflator; - tinfl_init(&inflator); - - if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, - TINFL_LZ_DICT_SIZE))) - status = TINFL_STATUS_FAILED; - else { - do { - mz_uint8 *pWrite_buf_cur = - (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); - size_t in_buf_size, - out_buf_size = - TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); - if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) { - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, - (size_t)read_buf_avail) != read_buf_avail) { - status = TINFL_STATUS_FAILED; - break; - } - cur_file_ofs += read_buf_avail; - comp_remaining -= read_buf_avail; - read_buf_ofs = 0; - } - - in_buf_size = (size_t)read_buf_avail; - status = tinfl_decompress( - &inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, - (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, - comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); - read_buf_avail -= in_buf_size; - read_buf_ofs += in_buf_size; - - if (out_buf_size) { - if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != - out_buf_size) { - status = TINFL_STATUS_FAILED; - break; - } - file_crc32 = - (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size); - if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) { - status = TINFL_STATUS_FAILED; - break; - } - } - } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || - (status == TINFL_STATUS_HAS_MORE_OUTPUT)); - } - } - - if ((status == TINFL_STATUS_DONE) && - (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) { - // Make sure the entire file was decompressed, and check its CRC. - if ((out_buf_ofs != file_stat.m_uncomp_size) || - (file_crc32 != file_stat.m_crc32)) - status = TINFL_STATUS_FAILED; - } - - if (!pZip->m_pState->m_pMem) pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - if (pWrite_buf) pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf); - - return status == TINFL_STATUS_DONE; -} - -mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, - const char *pFilename, - mz_file_write_func pCallback, - void *pOpaque, mz_uint flags) { - int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); - if (file_index < 0) return MZ_FALSE; - return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, - flags); -} - -#ifndef MINIZ_NO_STDIO -static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, - const void *pBuf, size_t n) { - (void)ofs; - return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque); -} - -mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, - const char *pDst_filename, - mz_uint flags) { - mz_bool status; - mz_zip_archive_file_stat file_stat; - MZ_FILE *pFile; - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; - pFile = MZ_FOPEN(pDst_filename, "wb"); - if (!pFile) return MZ_FALSE; - status = mz_zip_reader_extract_to_callback( - pZip, file_index, mz_zip_file_write_callback, pFile, flags); - if (MZ_FCLOSE(pFile) == EOF) return MZ_FALSE; -#ifndef MINIZ_NO_TIME - if (status) - mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time); -#endif - return status; -} -#endif // #ifndef MINIZ_NO_STDIO - -mz_bool mz_zip_reader_end(mz_zip_archive *pZip) { - if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || - (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) - return MZ_FALSE; - - if (pZip->m_pState) { - mz_zip_internal_state *pState = pZip->m_pState; - pZip->m_pState = NULL; - mz_zip_array_clear(pZip, &pState->m_central_dir); - mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); - mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); - -#ifndef MINIZ_NO_STDIO - if (pState->m_pFile) { - MZ_FCLOSE(pState->m_pFile); - pState->m_pFile = NULL; - } -#endif // #ifndef MINIZ_NO_STDIO - - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); - } - pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; - - return MZ_TRUE; -} - -#ifndef MINIZ_NO_STDIO -mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, - const char *pArchive_filename, - const char *pDst_filename, - mz_uint flags) { - int file_index = - mz_zip_reader_locate_file(pZip, pArchive_filename, NULL, flags); - if (file_index < 0) return MZ_FALSE; - return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags); -} -#endif - -// ------------------- .ZIP archive writing - -#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS - -static void mz_write_le16(mz_uint8 *p, mz_uint16 v) { - p[0] = (mz_uint8)v; - p[1] = (mz_uint8)(v >> 8); -} -static void mz_write_le32(mz_uint8 *p, mz_uint32 v) { - p[0] = (mz_uint8)v; - p[1] = (mz_uint8)(v >> 8); - p[2] = (mz_uint8)(v >> 16); - p[3] = (mz_uint8)(v >> 24); -} -#define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v)) -#define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v)) - -mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size) { - if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || - (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) - return MZ_FALSE; - - if (pZip->m_file_offset_alignment) { - // Ensure user specified file offset alignment is a power of 2. - if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1)) - return MZ_FALSE; - } - - if (!pZip->m_pAlloc) pZip->m_pAlloc = def_alloc_func; - if (!pZip->m_pFree) pZip->m_pFree = def_free_func; - if (!pZip->m_pRealloc) pZip->m_pRealloc = def_realloc_func; - - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; - pZip->m_archive_size = existing_size; - pZip->m_central_directory_file_ofs = 0; - pZip->m_total_files = 0; - - if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) - return MZ_FALSE; - memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, - sizeof(mz_uint8)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, - sizeof(mz_uint32)); - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, - sizeof(mz_uint32)); - return MZ_TRUE; -} - -static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, - const void *pBuf, size_t n) { - mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; - mz_zip_internal_state *pState = pZip->m_pState; - mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size); -#ifdef _MSC_VER - if ((!n) || - ((0, sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))) -#else - if ((!n) || - ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))) -#endif - return 0; - if (new_size > pState->m_mem_capacity) { - void *pNew_block; - size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity); - while (new_capacity < new_size) new_capacity *= 2; - if (NULL == (pNew_block = pZip->m_pRealloc( - pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) - return 0; - pState->m_pMem = pNew_block; - pState->m_mem_capacity = new_capacity; - } - memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n); - pState->m_mem_size = (size_t)new_size; - return n; -} - -mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, - size_t size_to_reserve_at_beginning, - size_t initial_allocation_size) { - pZip->m_pWrite = mz_zip_heap_write_func; - pZip->m_pIO_opaque = pZip; - if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning)) return MZ_FALSE; - if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, - size_to_reserve_at_beginning))) { - if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, initial_allocation_size))) { - mz_zip_writer_end(pZip); - return MZ_FALSE; - } - pZip->m_pState->m_mem_capacity = initial_allocation_size; - } - return MZ_TRUE; -} - -#ifndef MINIZ_NO_STDIO -static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, - const void *pBuf, size_t n) { - mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; - mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); - if (((mz_int64)file_ofs < 0) || - (((cur_ofs != (mz_int64)file_ofs)) && - (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) - return 0; - return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile); -} - -mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, - mz_uint64 size_to_reserve_at_beginning) { - MZ_FILE *pFile; - pZip->m_pWrite = mz_zip_file_write_func; - pZip->m_pIO_opaque = pZip; - if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning)) return MZ_FALSE; - if (NULL == (pFile = MZ_FOPEN(pFilename, "wb"))) { - mz_zip_writer_end(pZip); - return MZ_FALSE; - } - pZip->m_pState->m_pFile = pFile; - if (size_to_reserve_at_beginning) { - mz_uint64 cur_ofs = 0; - char buf[4096]; - MZ_CLEAR_OBJ(buf); - do { - size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning); - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) { - mz_zip_writer_end(pZip); - return MZ_FALSE; - } - cur_ofs += n; - size_to_reserve_at_beginning -= n; - } while (size_to_reserve_at_beginning); - } - return MZ_TRUE; -} -#endif // #ifndef MINIZ_NO_STDIO - -mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, - const char *pFilename) { - mz_zip_internal_state *pState; - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) - return MZ_FALSE; - // No sense in trying to write to an archive that's already at the support max - // size - if ((pZip->m_total_files == 0xFFFF) || - ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)) - return MZ_FALSE; - - pState = pZip->m_pState; - - if (pState->m_pFile) { -#ifdef MINIZ_NO_STDIO - pFilename; - return MZ_FALSE; -#else - // Archive is being read from stdio - try to reopen as writable. - if (pZip->m_pIO_opaque != pZip) return MZ_FALSE; - if (!pFilename) return MZ_FALSE; - pZip->m_pWrite = mz_zip_file_write_func; - if (NULL == - (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) { - // The mz_zip_archive is now in a bogus state because pState->m_pFile is - // NULL, so just close it. - mz_zip_reader_end(pZip); - return MZ_FALSE; - } -#endif // #ifdef MINIZ_NO_STDIO - } else if (pState->m_pMem) { - // Archive lives in a memory block. Assume it's from the heap that we can - // resize using the realloc callback. - if (pZip->m_pIO_opaque != pZip) return MZ_FALSE; - pState->m_mem_capacity = pState->m_mem_size; - pZip->m_pWrite = mz_zip_heap_write_func; - } - // Archive is being read via a user provided read function - make sure the - // user has specified a write function too. - else if (!pZip->m_pWrite) - return MZ_FALSE; - - // Start writing new files at the archive's current central directory - // location. - pZip->m_archive_size = pZip->m_central_directory_file_ofs; - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; - pZip->m_central_directory_file_ofs = 0; - - return MZ_TRUE; -} - -mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, - const void *pBuf, size_t buf_size, - mz_uint level_and_flags) { - return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, - level_and_flags, 0, 0); -} - -typedef struct { - mz_zip_archive *m_pZip; - mz_uint64 m_cur_archive_file_ofs; - mz_uint64 m_comp_size; -} mz_zip_writer_add_state; - -static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, - void *pUser) { - mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser; - if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, - pState->m_cur_archive_file_ofs, pBuf, - len) != len) - return MZ_FALSE; - pState->m_cur_archive_file_ofs += len; - pState->m_comp_size += len; - return MZ_TRUE; -} - -static mz_bool mz_zip_writer_create_local_dir_header( - mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, - mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, - mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, - mz_uint16 dos_time, mz_uint16 dos_date) { - (void)pZip; - memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE); - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date); - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32); - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, comp_size); - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, uncomp_size); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size); - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size); - return MZ_TRUE; -} - -static mz_bool mz_zip_writer_create_central_dir_header( - mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, - mz_uint16 extra_size, mz_uint16 comment_size, mz_uint64 uncomp_size, - mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, - mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, - mz_uint64 local_header_ofs, mz_uint32 ext_attributes) { - (void)pZip; - memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, comp_size); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, uncomp_size); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size); - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes); - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_header_ofs); - return MZ_TRUE; -} - -static mz_bool mz_zip_writer_add_to_central_dir( - mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size, - const void *pExtra, mz_uint16 extra_size, const void *pComment, - mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, - mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, - mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, - mz_uint32 ext_attributes) { - mz_zip_internal_state *pState = pZip->m_pState; - mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size; - size_t orig_central_dir_size = pState->m_central_dir.m_size; - mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; - - // No zip64 support yet - if ((local_header_ofs > 0xFFFFFFFF) || - (((mz_uint64)pState->m_central_dir.m_size + - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + - comment_size) > 0xFFFFFFFF)) - return MZ_FALSE; - - if (!mz_zip_writer_create_central_dir_header( - pZip, central_dir_header, filename_size, extra_size, comment_size, - uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, - dos_date, local_header_ofs, ext_attributes)) - return MZ_FALSE; - - if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, - filename_size)) || - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, - extra_size)) || - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, - comment_size)) || - (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, - ¢ral_dir_ofs, 1))) { - // Try to push the central directory array back into its original state. - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, - MZ_FALSE); - return MZ_FALSE; - } - - return MZ_TRUE; -} - -static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name) { - // Basic ZIP archive filename validity checks: Valid filenames cannot start - // with a forward slash, cannot contain a drive letter, and cannot use - // DOS-style backward slashes. - if (*pArchive_name == '/') return MZ_FALSE; - while (*pArchive_name) { - if ((*pArchive_name == '\\') || (*pArchive_name == ':')) return MZ_FALSE; - pArchive_name++; - } - return MZ_TRUE; -} - -static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment( - mz_zip_archive *pZip) { - mz_uint32 n; - if (!pZip->m_file_offset_alignment) return 0; - n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1)); - return (pZip->m_file_offset_alignment - n) & - (pZip->m_file_offset_alignment - 1); -} - -static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, - mz_uint64 cur_file_ofs, mz_uint32 n) { - char buf[4096]; - memset(buf, 0, MZ_MIN(sizeof(buf), n)); - while (n) { - mz_uint32 s = MZ_MIN(sizeof(buf), n); - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s) - return MZ_FALSE; - cur_file_ofs += s; - n -= s; - } - return MZ_TRUE; -} - -mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, - const char *pArchive_name, const void *pBuf, - size_t buf_size, const void *pComment, - mz_uint16 comment_size, - mz_uint level_and_flags, mz_uint64 uncomp_size, - mz_uint32 uncomp_crc32) { - mz_uint16 method = 0, dos_time = 0, dos_date = 0; - mz_uint level, ext_attributes = 0, num_alignment_padding_bytes; - mz_uint64 local_dir_header_ofs = pZip->m_archive_size, - cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0; - size_t archive_name_size; - mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; - tdefl_compressor *pComp = NULL; - mz_bool store_data_uncompressed; - mz_zip_internal_state *pState; - - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; - level = level_and_flags & 0xF; - store_data_uncompressed = - ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); - - if ((!pZip) || (!pZip->m_pState) || - (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || - (!pArchive_name) || ((comment_size) && (!pComment)) || - (pZip->m_total_files == 0xFFFF) || (level > MZ_UBER_COMPRESSION)) - return MZ_FALSE; - - pState = pZip->m_pState; - - if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) - return MZ_FALSE; - // No zip64 support yet - if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) return MZ_FALSE; - if (!mz_zip_writer_validate_archive_name(pArchive_name)) return MZ_FALSE; - -#ifndef MINIZ_NO_TIME - { - time_t cur_time; - time(&cur_time); - mz_zip_time_to_dos_time(cur_time, &dos_time, &dos_date); - } -#endif // #ifndef MINIZ_NO_TIME - - archive_name_size = strlen(pArchive_name); - if (archive_name_size > 0xFFFF) return MZ_FALSE; - - num_alignment_padding_bytes = - mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); - - // no zip64 support yet - if ((pZip->m_total_files == 0xFFFF) || - ((pZip->m_archive_size + num_alignment_padding_bytes + - MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + - comment_size + archive_name_size) > 0xFFFFFFFF)) - return MZ_FALSE; - - if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) { - // Set DOS Subdirectory attribute bit. - ext_attributes |= 0x10; - // Subdirectories cannot contain data. - if ((buf_size) || (uncomp_size)) return MZ_FALSE; - } - - // Try to do any allocations before writing to the archive, so if an - // allocation fails the file remains unmodified. (A good idea if we're doing - // an in-place modification.) - if ((!mz_zip_array_ensure_room( - pZip, &pState->m_central_dir, - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size)) || - (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) - return MZ_FALSE; - - if ((!store_data_uncompressed) && (buf_size)) { - if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)))) - return MZ_FALSE; - } - - if (!mz_zip_writer_write_zeros( - pZip, cur_archive_file_ofs, - num_alignment_padding_bytes + sizeof(local_dir_header))) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - return MZ_FALSE; - } - local_dir_header_ofs += num_alignment_padding_bytes; - if (pZip->m_file_offset_alignment) { - MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == - 0); - } - cur_archive_file_ofs += - num_alignment_padding_bytes + sizeof(local_dir_header); - - MZ_CLEAR_OBJ(local_dir_header); - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, - archive_name_size) != archive_name_size) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - return MZ_FALSE; - } - cur_archive_file_ofs += archive_name_size; - - if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { - uncomp_crc32 = - (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size); - uncomp_size = buf_size; - if (uncomp_size <= 3) { - level = 0; - store_data_uncompressed = MZ_TRUE; - } - } - - if (store_data_uncompressed) { - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, - buf_size) != buf_size) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - return MZ_FALSE; - } - - cur_archive_file_ofs += buf_size; - comp_size = buf_size; - - if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) method = MZ_DEFLATED; - } else if (buf_size) { - mz_zip_writer_add_state state; - - state.m_pZip = pZip; - state.m_cur_archive_file_ofs = cur_archive_file_ofs; - state.m_comp_size = 0; - - if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, - tdefl_create_comp_flags_from_zip_params( - level, -15, MZ_DEFAULT_STRATEGY)) != - TDEFL_STATUS_OKAY) || - (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != - TDEFL_STATUS_DONE)) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - return MZ_FALSE; - } - - comp_size = state.m_comp_size; - cur_archive_file_ofs = state.m_cur_archive_file_ofs; - - method = MZ_DEFLATED; - } - - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - pComp = NULL; - - // no zip64 support yet - if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF)) - return MZ_FALSE; - - if (!mz_zip_writer_create_local_dir_header( - pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, - comp_size, uncomp_crc32, method, 0, dos_time, dos_date)) - return MZ_FALSE; - - if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, - sizeof(local_dir_header)) != sizeof(local_dir_header)) - return MZ_FALSE; - - if (!mz_zip_writer_add_to_central_dir( - pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, - comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, - dos_time, dos_date, local_dir_header_ofs, ext_attributes)) - return MZ_FALSE; - - pZip->m_total_files++; - pZip->m_archive_size = cur_archive_file_ofs; - - return MZ_TRUE; -} - -#ifndef MINIZ_NO_STDIO -mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, - const char *pSrc_filename, const void *pComment, - mz_uint16 comment_size, - mz_uint level_and_flags) { - mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; - mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; - mz_uint64 local_dir_header_ofs = pZip->m_archive_size, - cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, - comp_size = 0; - size_t archive_name_size; - mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; - MZ_FILE *pSrc_file = NULL; - - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; - level = level_and_flags & 0xF; - - if ((!pZip) || (!pZip->m_pState) || - (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || - ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) - return MZ_FALSE; - if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) return MZ_FALSE; - if (!mz_zip_writer_validate_archive_name(pArchive_name)) return MZ_FALSE; - - archive_name_size = strlen(pArchive_name); - if (archive_name_size > 0xFFFF) return MZ_FALSE; - - num_alignment_padding_bytes = - mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); - - // no zip64 support yet - if ((pZip->m_total_files == 0xFFFF) || - ((pZip->m_archive_size + num_alignment_padding_bytes + - MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + - comment_size + archive_name_size) > 0xFFFFFFFF)) - return MZ_FALSE; - - if (!mz_zip_get_file_modified_time(pSrc_filename, &dos_time, &dos_date)) - return MZ_FALSE; - - pSrc_file = MZ_FOPEN(pSrc_filename, "rb"); - if (!pSrc_file) return MZ_FALSE; - MZ_FSEEK64(pSrc_file, 0, SEEK_END); - uncomp_size = MZ_FTELL64(pSrc_file); - MZ_FSEEK64(pSrc_file, 0, SEEK_SET); - - if (uncomp_size > 0xFFFFFFFF) { - // No zip64 support yet - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - if (uncomp_size <= 3) level = 0; - - if (!mz_zip_writer_write_zeros( - pZip, cur_archive_file_ofs, - num_alignment_padding_bytes + sizeof(local_dir_header))) { - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - local_dir_header_ofs += num_alignment_padding_bytes; - if (pZip->m_file_offset_alignment) { - MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == - 0); - } - cur_archive_file_ofs += - num_alignment_padding_bytes + sizeof(local_dir_header); - - MZ_CLEAR_OBJ(local_dir_header); - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, - archive_name_size) != archive_name_size) { - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - cur_archive_file_ofs += archive_name_size; - - if (uncomp_size) { - mz_uint64 uncomp_remaining = uncomp_size; - void *pRead_buf = - pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE); - if (!pRead_buf) { - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - - if (!level) { - while (uncomp_remaining) { - mz_uint n = - (mz_uint)MZ_MIN((mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE, uncomp_remaining); - if ((MZ_FREAD(pRead_buf, 1, n, pSrc_file) != n) || - (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, - n) != n)) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - uncomp_crc32 = - (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); - uncomp_remaining -= n; - cur_archive_file_ofs += n; - } - comp_size = uncomp_size; - } else { - mz_bool result = MZ_FALSE; - mz_zip_writer_add_state state; - tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)); - if (!pComp) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - - state.m_pZip = pZip; - state.m_cur_archive_file_ofs = cur_archive_file_ofs; - state.m_comp_size = 0; - - if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, - tdefl_create_comp_flags_from_zip_params( - level, -15, MZ_DEFAULT_STRATEGY)) != - TDEFL_STATUS_OKAY) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - - for (;;) { - size_t in_buf_size = (mz_uint32)MZ_MIN(uncomp_remaining, - (mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE); - tdefl_status status; - - if (MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size) - break; - - uncomp_crc32 = (mz_uint32)mz_crc32( - uncomp_crc32, (const mz_uint8 *)pRead_buf, in_buf_size); - uncomp_remaining -= in_buf_size; - - status = tdefl_compress_buffer( - pComp, pRead_buf, in_buf_size, - uncomp_remaining ? TDEFL_NO_FLUSH : TDEFL_FINISH); - if (status == TDEFL_STATUS_DONE) { - result = MZ_TRUE; - break; - } else if (status != TDEFL_STATUS_OKAY) - break; - } - - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); - - if (!result) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - MZ_FCLOSE(pSrc_file); - return MZ_FALSE; - } - - comp_size = state.m_comp_size; - cur_archive_file_ofs = state.m_cur_archive_file_ofs; - - method = MZ_DEFLATED; - } - - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); - } - - MZ_FCLOSE(pSrc_file); - pSrc_file = NULL; - - // no zip64 support yet - if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF)) - return MZ_FALSE; - - if (!mz_zip_writer_create_local_dir_header( - pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, - comp_size, uncomp_crc32, method, 0, dos_time, dos_date)) - return MZ_FALSE; - - if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, - sizeof(local_dir_header)) != sizeof(local_dir_header)) - return MZ_FALSE; - - if (!mz_zip_writer_add_to_central_dir( - pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, - comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, - dos_time, dos_date, local_dir_header_ofs, ext_attributes)) - return MZ_FALSE; - - pZip->m_total_files++; - pZip->m_archive_size = cur_archive_file_ofs; - - return MZ_TRUE; -} -#endif // #ifndef MINIZ_NO_STDIO - -mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, - mz_zip_archive *pSource_zip, - mz_uint file_index) { - mz_uint n, bit_flags, num_alignment_padding_bytes; - mz_uint64 comp_bytes_remaining, local_dir_header_ofs; - mz_uint64 cur_src_file_ofs, cur_dst_file_ofs; - mz_uint32 - local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / - sizeof(mz_uint32)]; - mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; - mz_uint8 central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; - size_t orig_central_dir_size; - mz_zip_internal_state *pState; - void *pBuf; - const mz_uint8 *pSrc_central_header; - - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) - return MZ_FALSE; - if (NULL == - (pSrc_central_header = mz_zip_reader_get_cdh(pSource_zip, file_index))) - return MZ_FALSE; - pState = pZip->m_pState; - - num_alignment_padding_bytes = - mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); - - // no zip64 support yet - if ((pZip->m_total_files == 0xFFFF) || - ((pZip->m_archive_size + num_alignment_padding_bytes + - MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) > - 0xFFFFFFFF)) - return MZ_FALSE; - - cur_src_file_ofs = - MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS); - cur_dst_file_ofs = pZip->m_archive_size; - - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, - pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) - return MZ_FALSE; - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) - return MZ_FALSE; - cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; - - if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, - num_alignment_padding_bytes)) - return MZ_FALSE; - cur_dst_file_ofs += num_alignment_padding_bytes; - local_dir_header_ofs = cur_dst_file_ofs; - if (pZip->m_file_offset_alignment) { - MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == - 0); - } - - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) - return MZ_FALSE; - cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; - - n = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); - comp_bytes_remaining = - n + MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); - - if (NULL == (pBuf = pZip->m_pAlloc( - pZip->m_pAlloc_opaque, 1, - (size_t)MZ_MAX(sizeof(mz_uint32) * 4, - MZ_MIN((mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE, - comp_bytes_remaining))))) - return MZ_FALSE; - - while (comp_bytes_remaining) { - n = (mz_uint)MZ_MIN((mz_uint)MZ_ZIP_MAX_IO_BUF_SIZE, comp_bytes_remaining); - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, - n) != n) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - return MZ_FALSE; - } - cur_src_file_ofs += n; - - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - return MZ_FALSE; - } - cur_dst_file_ofs += n; - - comp_bytes_remaining -= n; - } - - bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); - if (bit_flags & 8) { - // Copy data descriptor - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, - sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - return MZ_FALSE; - } - - n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == 0x08074b50) ? 4 : 3); - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - return MZ_FALSE; - } - - cur_src_file_ofs += n; - cur_dst_file_ofs += n; - } - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); - - // no zip64 support yet - if (cur_dst_file_ofs > 0xFFFFFFFF) return MZ_FALSE; - - orig_central_dir_size = pState->m_central_dir.m_size; - - memcpy(central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); - MZ_WRITE_LE32(central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, - local_dir_header_ofs); - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_header, - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) - return MZ_FALSE; - - n = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS) + - MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS) + - MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS); - if (!mz_zip_array_push_back( - pZip, &pState->m_central_dir, - pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n)) { - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, - MZ_FALSE); - return MZ_FALSE; - } - - if (pState->m_central_dir.m_size > 0xFFFFFFFF) return MZ_FALSE; - n = (mz_uint32)orig_central_dir_size; - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) { - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, - MZ_FALSE); - return MZ_FALSE; - } - - pZip->m_total_files++; - pZip->m_archive_size = cur_dst_file_ofs; - - return MZ_TRUE; -} - -mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) { - mz_zip_internal_state *pState; - mz_uint64 central_dir_ofs, central_dir_size; - mz_uint8 hdr[MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE]; - - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) - return MZ_FALSE; - - pState = pZip->m_pState; - - // no zip64 support yet - if ((pZip->m_total_files > 0xFFFF) || - ((pZip->m_archive_size + pState->m_central_dir.m_size + - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)) - return MZ_FALSE; - - central_dir_ofs = 0; - central_dir_size = 0; - if (pZip->m_total_files) { - // Write central directory - central_dir_ofs = pZip->m_archive_size; - central_dir_size = pState->m_central_dir.m_size; - pZip->m_central_directory_file_ofs = central_dir_ofs; - if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, - pState->m_central_dir.m_p, - (size_t)central_dir_size) != central_dir_size) - return MZ_FALSE; - pZip->m_archive_size += central_dir_size; - } - - // Write end of central directory record - MZ_CLEAR_OBJ(hdr); - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); - MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, - pZip->m_total_files); - MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files); - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, central_dir_size); - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, central_dir_ofs); - - if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, - sizeof(hdr)) != sizeof(hdr)) - return MZ_FALSE; -#ifndef MINIZ_NO_STDIO - if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF)) return MZ_FALSE; -#endif // #ifndef MINIZ_NO_STDIO - - pZip->m_archive_size += sizeof(hdr); - - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED; - return MZ_TRUE; -} - -mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, - size_t *pSize) { - if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pSize)) return MZ_FALSE; - if (pZip->m_pWrite != mz_zip_heap_write_func) return MZ_FALSE; - if (!mz_zip_writer_finalize_archive(pZip)) return MZ_FALSE; - - *pBuf = pZip->m_pState->m_pMem; - *pSize = pZip->m_pState->m_mem_size; - pZip->m_pState->m_pMem = NULL; - pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0; - return MZ_TRUE; -} - -mz_bool mz_zip_writer_end(mz_zip_archive *pZip) { - mz_zip_internal_state *pState; - mz_bool status = MZ_TRUE; - if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || - ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && - (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) - return MZ_FALSE; - - pState = pZip->m_pState; - pZip->m_pState = NULL; - mz_zip_array_clear(pZip, &pState->m_central_dir); - mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); - mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); - -#ifndef MINIZ_NO_STDIO - if (pState->m_pFile) { - MZ_FCLOSE(pState->m_pFile); - pState->m_pFile = NULL; - } -#endif // #ifndef MINIZ_NO_STDIO - - if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) { - pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem); - pState->m_pMem = NULL; - } - - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); - pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; - return status; -} - -#ifndef MINIZ_NO_STDIO -mz_bool mz_zip_add_mem_to_archive_file_in_place( - const char *pZip_filename, const char *pArchive_name, const void *pBuf, - size_t buf_size, const void *pComment, mz_uint16 comment_size, - mz_uint level_and_flags) { - mz_bool status, created_new_archive = MZ_FALSE; - mz_zip_archive zip_archive; - struct MZ_FILE_STAT_STRUCT file_stat; - MZ_CLEAR_OBJ(zip_archive); - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; - if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || - ((comment_size) && (!pComment)) || - ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) - return MZ_FALSE; - if (!mz_zip_writer_validate_archive_name(pArchive_name)) return MZ_FALSE; - if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) { - // Create a new archive. - if (!mz_zip_writer_init_file(&zip_archive, pZip_filename, 0)) - return MZ_FALSE; - created_new_archive = MZ_TRUE; - } else { - // Append to an existing archive. - if (!mz_zip_reader_init_file( - &zip_archive, pZip_filename, - level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) - return MZ_FALSE; - if (!mz_zip_writer_init_from_reader(&zip_archive, pZip_filename)) { - mz_zip_reader_end(&zip_archive); - return MZ_FALSE; - } - } - status = - mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, - pComment, comment_size, level_and_flags, 0, 0); - // Always finalize, even if adding failed for some reason, so we have a valid - // central directory. (This may not always succeed, but we can try.) - if (!mz_zip_writer_finalize_archive(&zip_archive)) status = MZ_FALSE; - if (!mz_zip_writer_end(&zip_archive)) status = MZ_FALSE; - if ((!status) && (created_new_archive)) { - // It's a new archive and something went wrong, so just delete it. - int ignoredStatus = MZ_DELETE_FILE(pZip_filename); - (void)ignoredStatus; - } - return status; -} - -void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, - const char *pArchive_name, - size_t *pSize, mz_uint flags) { - int file_index; - mz_zip_archive zip_archive; - void *p = NULL; - - if (pSize) *pSize = 0; - - if ((!pZip_filename) || (!pArchive_name)) return NULL; - - MZ_CLEAR_OBJ(zip_archive); - if (!mz_zip_reader_init_file( - &zip_archive, pZip_filename, - flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) - return NULL; - - if ((file_index = mz_zip_reader_locate_file(&zip_archive, pArchive_name, NULL, - flags)) >= 0) - p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags); - - mz_zip_reader_end(&zip_archive); - return p; -} - -#endif // #ifndef MINIZ_NO_STDIO - -#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS - -#endif // #ifndef MINIZ_NO_ARCHIVE_APIS - -#ifdef __cplusplus -} -#endif - -#endif // MINIZ_HEADER_FILE_ONLY - -/* - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to <http://unlicense.org/> -*/ - -// ---------------------- end of miniz ---------------------------------------- - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#ifdef _MSC_VER -#pragma warning(pop) -#endif -} // namespace miniz -#else - -// Reuse MINIZ_LITTE_ENDIAN macro - -#if defined(__sparcv9) -// Big endian -#else -#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU -// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. -#define MINIZ_LITTLE_ENDIAN 1 -#endif -#endif - -#endif // TINYEXR_USE_MINIZ - // static bool IsBigEndian(void) { // union { // unsigned int i; @@ -7000,7 +684,7 @@ static void cpy2(unsigned short *dst_val, const unsigned short *src_val) { } static void swap2(unsigned short *val) { -#ifdef MINIZ_LITTLE_ENDIAN +#ifdef TINYEXR_LITTLE_ENDIAN (void)val; #else unsigned short tmp = *val; @@ -7012,6 +696,15 @@ static void swap2(unsigned short *val) { #endif } +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif static void cpy4(int *dst_val, const int *src_val) { unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val); const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val); @@ -7041,9 +734,16 @@ static void cpy4(float *dst_val, const float *src_val) { dst[2] = src[2]; dst[3] = src[3]; } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif static void swap4(unsigned int *val) { -#ifdef MINIZ_LITTLE_ENDIAN +#ifdef TINYEXR_LITTLE_ENDIAN (void)val; #else unsigned int tmp = *val; @@ -7057,6 +757,36 @@ static void swap4(unsigned int *val) { #endif } +static void swap4(int *val) { +#ifdef TINYEXR_LITTLE_ENDIAN + (void)val; +#else + int tmp = *val; + unsigned char *dst = reinterpret_cast<unsigned char *>(val); + unsigned char *src = reinterpret_cast<unsigned char *>(&tmp); + + dst[0] = src[3]; + dst[1] = src[2]; + dst[2] = src[1]; + dst[3] = src[0]; +#endif +} + +static void swap4(float *val) { +#ifdef TINYEXR_LITTLE_ENDIAN + (void)val; +#else + float tmp = *val; + unsigned char *dst = reinterpret_cast<unsigned char *>(val); + unsigned char *src = reinterpret_cast<unsigned char *>(&tmp); + + dst[0] = src[3]; + dst[1] = src[2]; + dst[2] = src[1]; + dst[3] = src[0]; +#endif +} + #if 0 static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 *src_val) { unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val); @@ -7074,7 +804,7 @@ static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 #endif static void swap8(tinyexr::tinyexr_uint64 *val) { -#ifdef MINIZ_LITTLE_ENDIAN +#ifdef TINYEXR_LITTLE_ENDIAN (void)val; #else tinyexr::tinyexr_uint64 tmp = (*val); @@ -7093,12 +823,11 @@ static void swap8(tinyexr::tinyexr_uint64 *val) { } // https://gist.github.com/rygorous/2156668 -// Reuse MINIZ_LITTLE_ENDIAN flag from miniz. union FP32 { unsigned int u; float f; struct { -#if MINIZ_LITTLE_ENDIAN +#if TINYEXR_LITTLE_ENDIAN unsigned int Mantissa : 23; unsigned int Exponent : 8; unsigned int Sign : 1; @@ -7118,7 +847,7 @@ union FP32 { union FP16 { unsigned short u; struct { -#if MINIZ_LITTLE_ENDIAN +#if TINYEXR_LITTLE_ENDIAN unsigned int Mantissa : 10; unsigned int Exponent : 5; unsigned int Sign : 1; @@ -7304,7 +1033,7 @@ static void WriteAttributeToMemory(std::vector<unsigned char> *out, out->insert(out->end(), type, type + strlen(type) + 1); int outLen = len; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&outLen)); + tinyexr::swap4(&outLen); out->insert(out->end(), reinterpret_cast<unsigned char *>(&outLen), reinterpret_cast<unsigned char *>(&outLen) + sizeof(int)); out->insert(out->end(), data, data + len); @@ -7313,6 +1042,7 @@ static void WriteAttributeToMemory(std::vector<unsigned char> *out, typedef struct { std::string name; // less than 255 bytes long int pixel_type; + int requested_pixel_type; int x_sampling; int y_sampling; unsigned char p_linear; @@ -7320,12 +1050,19 @@ typedef struct { } ChannelInfo; typedef struct { + int min_x; + int min_y; + int max_x; + int max_y; +} Box2iInfo; + +struct HeaderInfo { std::vector<tinyexr::ChannelInfo> channels; std::vector<EXRAttribute> attributes; - int data_window[4]; + Box2iInfo data_window; int line_order; - int display_window[4]; + Box2iInfo display_window; float screen_window_center[2]; float screen_window_width; float pixel_aspect_ratio; @@ -7333,6 +1070,7 @@ typedef struct { int chunk_count; // Tiled format + int tiled; // Non-zero if the part is tiled. int tile_size_x; int tile_size_y; int tile_level_mode; @@ -7342,19 +1080,24 @@ typedef struct { int compression_type; + // required for multi-part or non-image files + std::string name; + // required for multi-part or non-image files + std::string type; + void clear() { channels.clear(); attributes.clear(); - data_window[0] = 0; - data_window[1] = 0; - data_window[2] = 0; - data_window[3] = 0; + data_window.min_x = 0; + data_window.min_y = 0; + data_window.max_x = 0; + data_window.max_y = 0; line_order = 0; - display_window[0] = 0; - display_window[1] = 0; - display_window[2] = 0; - display_window[3] = 0; + display_window.min_x = 0; + display_window.min_y = 0; + display_window.max_x = 0; + display_window.max_y = 0; screen_window_center[0] = 0.0f; screen_window_center[1] = 0.0f; screen_window_width = 0.0f; @@ -7363,6 +1106,7 @@ typedef struct { chunk_count = 0; // Tiled format + tiled = 0; tile_size_x = 0; tile_size_y = 0; tile_level_mode = 0; @@ -7370,8 +1114,11 @@ typedef struct { header_len = 0; compression_type = 0; + + name.clear(); + type.clear(); } -} HeaderInfo; +}; static bool ReadChannelInfo(std::vector<ChannelInfo> &channels, const std::vector<unsigned char> &data) { @@ -7410,9 +1157,9 @@ static bool ReadChannelInfo(std::vector<ChannelInfo> &channels, memcpy(&info.y_sampling, p, sizeof(int)); // int p += 4; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info.pixel_type)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info.x_sampling)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info.y_sampling)); + tinyexr::swap4(&info.pixel_type); + tinyexr::swap4(&info.x_sampling); + tinyexr::swap4(&info.y_sampling); channels.push_back(info); } @@ -7439,12 +1186,12 @@ static void WriteChannelInfo(std::vector<unsigned char> &data, (*p) = '\0'; p++; - int pixel_type = channels[c].pixel_type; + int pixel_type = channels[c].requested_pixel_type; int x_sampling = channels[c].x_sampling; int y_sampling = channels[c].y_sampling; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&pixel_type)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&x_sampling)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&y_sampling)); + tinyexr::swap4(&pixel_type); + tinyexr::swap4(&x_sampling); + tinyexr::swap4(&y_sampling); memcpy(p, &pixel_type, sizeof(int)); p += sizeof(int); @@ -7518,11 +1265,11 @@ static void CompressZip(unsigned char *dst, // Compress the data using miniz // - miniz::mz_ulong outSize = miniz::mz_compressBound(src_size); - int ret = miniz::mz_compress( + mz_ulong outSize = mz_compressBound(src_size); + int ret = mz_compress( dst, &outSize, static_cast<const unsigned char *>(&tmpBuf.at(0)), src_size); - assert(ret == miniz::MZ_OK); + assert(ret == MZ_OK); (void)ret; compressedSize = outSize; @@ -7555,8 +1302,8 @@ static bool DecompressZip(unsigned char *dst, #if TINYEXR_USE_MINIZ int ret = - miniz::mz_uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size); - if (miniz::MZ_OK != ret) { + mz_uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size); + if (MZ_OK != ret) { return false; } #else @@ -7612,6 +1359,9 @@ static bool DecompressZip(unsigned char *dst, #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wsign-conversion" +#if __has_warning("-Wextra-semi-stmt") +#pragma clang diagnostic ignored "-Wextra-semi-stmt" +#endif #endif #ifdef _MSC_VER @@ -7650,7 +1400,7 @@ static int rleCompress(int inLength, const char in[], signed char out[]) { if (runEnd - runStart >= MIN_RUN_LENGTH) { // - // Compressable run + // Compressible run // *outWrite++ = static_cast<char>(runEnd - runStart) - 1; @@ -7696,7 +1446,8 @@ static int rleUncompress(int inLength, int maxLength, const signed char in[], int count = -(static_cast<int>(*in++)); inLength -= count + 1; - if (0 > (maxLength -= count)) return 0; + // Fixes #116: Add bounds check to in buffer. + if ((0 > (maxLength -= count)) || (inLength < 0)) return 0; memcpy(out, in, count); out += count; @@ -7790,13 +1541,19 @@ static void CompressRle(unsigned char *dst, } } -static void DecompressRle(unsigned char *dst, +static bool DecompressRle(unsigned char *dst, const unsigned long uncompressed_size, const unsigned char *src, unsigned long src_size) { if (uncompressed_size == src_size) { // Data is not compressed(Issue 40). memcpy(dst, src, src_size); - return; + return true; + } + + // Workaround for issue #112. + // TODO(syoyo): Add more robust out-of-bounds check in `rleUncompress`. + if (src_size <= 2) { + return false; } std::vector<unsigned char> tmpBuf(uncompressed_size); @@ -7805,8 +1562,9 @@ static void DecompressRle(unsigned char *dst, static_cast<int>(uncompressed_size), reinterpret_cast<const signed char *>(src), reinterpret_cast<char *>(&tmpBuf.at(0))); - assert(ret == static_cast<int>(uncompressed_size)); - (void)ret; + if (ret != static_cast<int>(uncompressed_size)) { + return false; + } // // Apply EXR-specific? postprocess. Grabbed from OpenEXR's @@ -7845,6 +1603,8 @@ static void DecompressRle(unsigned char *dst, break; } } + + return true; } #if TINYEXR_USE_PIZ @@ -7863,6 +1623,10 @@ static void DecompressRle(unsigned char *dst, #pragma clang diagnostic ignored "-Wcast-qual" #endif +#if __has_warning("-Wextra-semi-stmt") +#pragma clang diagnostic ignored "-Wextra-semi-stmt" +#endif + #endif // @@ -7980,7 +1744,7 @@ static void wav2Encode( int p2 = 2; // == 1 << (level+1) // - // Hierachical loop on smaller dimension n + // Hierarchical loop on smaller dimension n // while (p2 <= n) { @@ -8211,9 +1975,9 @@ const int HUF_DECMASK = HUF_DECSIZE - 1; struct HufDec { // short code long code //------------------------------- - int len : 8; // code length 0 - int lit : 24; // lit p size - int *p; // 0 lits + unsigned int len : 8; // code length 0 + unsigned int lit : 24; // lit p size + unsigned int *p; // 0 lits }; inline long long hufLength(long long code) { return code & 63; } @@ -8556,7 +2320,7 @@ static bool hufUnpackEncTable( int lc = 0; for (; im <= iM; im++) { - if (p - *pcode > ni) { + if (p - *pcode >= ni) { return false; } @@ -8669,14 +2433,14 @@ static bool hufBuildDecTable(const long long *hcode, // i : encoding table pl->lit++; if (pl->p) { - int *p = pl->p; - pl->p = new int[pl->lit]; + unsigned int *p = pl->p; + pl->p = new unsigned int[pl->lit]; for (int i = 0; i < pl->lit - 1; ++i) pl->p[i] = p[i]; delete[] p; } else { - pl->p = new int[1]; + pl->p = new unsigned int[1]; } pl->p[pl->lit - 1] = im; @@ -8854,7 +2618,8 @@ static bool getCode(int po, int rlc, long long &c, int &lc, const char *&in, if (out + cs > oe) return false; // Bounds check for safety - if ((out - 1) <= ob) return false; + // Issue 100. + if ((out - 1) < ob) return false; unsigned short s = out[-1]; while (cs-- > 0) *out++ = s; @@ -9182,7 +2947,7 @@ static bool CompressPiz(unsigned char *outPtr, unsigned int *outSize, unsigned short minNonZero; unsigned short maxNonZero; -#if !MINIZ_LITTLE_ENDIAN +#if !TINYEXR_LITTLE_ENDIAN // @todo { PIZ compression on BigEndian architecture. } assert(0); return false; @@ -9205,7 +2970,7 @@ static bool CompressPiz(unsigned char *outPtr, unsigned int *outSize, // cd.ys = c.channel().ySampling; size_t pixelSize = sizeof(int); // UINT and FLOAT - if (channelInfo[c].pixel_type == TINYEXR_PIXELTYPE_HALF) { + if (channelInfo[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) { pixelSize = sizeof(short); } @@ -9308,7 +3073,7 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, unsigned short minNonZero; unsigned short maxNonZero; -#if !MINIZ_LITTLE_ENDIAN +#if !TINYEXR_LITTLE_ENDIAN // @todo { PIZ compression on BigEndian architecture. } assert(0); return false; @@ -9347,6 +3112,10 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, tinyexr::cpy4(&length, reinterpret_cast<const int *>(ptr)); ptr += sizeof(int); + if (size_t((ptr - inPtr) + length) > inLen) { + return false; + } + std::vector<unsigned short> tmpBuffer(tmpBufSize); hufUncompress(reinterpret_cast<const char *>(ptr), length, &tmpBuffer); @@ -9410,35 +3179,48 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, #endif // TINYEXR_USE_PIZ #if TINYEXR_USE_ZFP + struct ZFPCompressionParam { double rate; - int precision; + unsigned int precision; + unsigned int __pad0; double tolerance; int type; // TINYEXR_ZFP_COMPRESSIONTYPE_* + unsigned int __pad1; ZFPCompressionParam() { type = TINYEXR_ZFP_COMPRESSIONTYPE_RATE; rate = 2.0; precision = 0; - tolerance = 0.0f; + tolerance = 0.0; } }; -bool FindZFPCompressionParam(ZFPCompressionParam *param, - const EXRAttribute *attributes, - int num_attributes) { +static bool FindZFPCompressionParam(ZFPCompressionParam *param, + const EXRAttribute *attributes, + int num_attributes, std::string *err) { bool foundType = false; for (int i = 0; i < num_attributes; i++) { - if ((strcmp(attributes[i].name, "zfpCompressionType") == 0) && - (attributes[i].size == 1)) { - param->type = static_cast<int>(attributes[i].value[0]); - - foundType = true; + if ((strcmp(attributes[i].name, "zfpCompressionType") == 0)) { + if (attributes[i].size == 1) { + param->type = static_cast<int>(attributes[i].value[0]); + foundType = true; + break; + } else { + if (err) { + (*err) += + "zfpCompressionType attribute must be uchar(1 byte) type.\n"; + } + return false; + } } } if (!foundType) { + if (err) { + (*err) += "`zfpCompressionType` attribute not found.\n"; + } return false; } @@ -9450,6 +3232,11 @@ bool FindZFPCompressionParam(ZFPCompressionParam *param, return true; } } + + if (err) { + (*err) += "`zfpCompressionRate` attribute not found.\n"; + } + } else if (param->type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) { for (int i = 0; i < num_attributes; i++) { if ((strcmp(attributes[i].name, "zfpCompressionPrecision") == 0) && @@ -9458,6 +3245,11 @@ bool FindZFPCompressionParam(ZFPCompressionParam *param, return true; } } + + if (err) { + (*err) += "`zfpCompressionPrecision` attribute not found.\n"; + } + } else if (param->type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) { for (int i = 0; i < num_attributes; i++) { if ((strcmp(attributes[i].name, "zfpCompressionTolerance") == 0) && @@ -9466,8 +3258,14 @@ bool FindZFPCompressionParam(ZFPCompressionParam *param, return true; } } + + if (err) { + (*err) += "`zfpCompressionTolerance` attribute not found.\n"; + } } else { - assert(0); + if (err) { + (*err) += "Unknown value specified for `zfpCompressionType`.\n"; + } } return false; @@ -9475,10 +3273,11 @@ bool FindZFPCompressionParam(ZFPCompressionParam *param, // Assume pixel format is FLOAT for all channels. static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, - int num_channels, const unsigned char *src, + size_t num_channels, const unsigned char *src, unsigned long src_size, const ZFPCompressionParam ¶m) { - size_t uncompressed_size = dst_width * dst_num_lines * num_channels; + size_t uncompressed_size = + size_t(dst_width) * size_t(dst_num_lines) * num_channels; if (uncompressed_size == src_size) { // Data is not compressed(Issue 40). @@ -9491,22 +3290,24 @@ static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, assert((dst_width % 4) == 0); assert((dst_num_lines % 4) == 0); - if ((dst_width & 3U) || (dst_num_lines & 3U)) { + if ((size_t(dst_width) & 3U) || (size_t(dst_num_lines) & 3U)) { return false; } field = zfp_field_2d(reinterpret_cast<void *>(const_cast<unsigned char *>(src)), - zfp_type_float, dst_width, dst_num_lines * num_channels); + zfp_type_float, static_cast<unsigned int>(dst_width), + static_cast<unsigned int>(dst_num_lines) * + static_cast<unsigned int>(num_channels)); zfp = zfp_stream_open(NULL); if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_RATE) { - zfp_stream_set_rate(zfp, param.rate, zfp_type_float, /* dimention */ 2, + zfp_stream_set_rate(zfp, param.rate, zfp_type_float, /* dimension */ 2, /* write random access */ 0); } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) { - zfp_stream_set_precision(zfp, param.precision, zfp_type_float); + zfp_stream_set_precision(zfp, param.precision); } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) { - zfp_stream_set_accuracy(zfp, param.tolerance, zfp_type_float); + zfp_stream_set_accuracy(zfp, param.tolerance); } else { assert(0); } @@ -9519,17 +3320,17 @@ static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, zfp_stream_set_bit_stream(zfp, stream); zfp_stream_rewind(zfp); - size_t image_size = dst_width * dst_num_lines; + size_t image_size = size_t(dst_width) * size_t(dst_num_lines); - for (int c = 0; c < num_channels; c++) { + for (size_t c = 0; c < size_t(num_channels); c++) { // decompress 4x4 pixel block. - for (int y = 0; y < dst_num_lines; y += 4) { - for (int x = 0; x < dst_width; x += 4) { + for (size_t y = 0; y < size_t(dst_num_lines); y += 4) { + for (size_t x = 0; x < size_t(dst_width); x += 4) { float fblock[16]; zfp_decode_block_float_2(zfp, fblock); - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { - dst[c * image_size + ((y + j) * dst_width + (x + i))] = + for (size_t j = 0; j < 4; j++) { + for (size_t i = 0; i < 4; i++) { + dst[c * image_size + ((y + j) * size_t(dst_width) + (x + i))] = fblock[j * 4 + i]; } } @@ -9545,31 +3346,33 @@ static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, } // Assume pixel format is FLOAT for all channels. -bool CompressZfp(std::vector<unsigned char> *outBuf, unsigned int *outSize, - const float *inPtr, int width, int num_lines, int num_channels, - const ZFPCompressionParam ¶m) { +static bool CompressZfp(std::vector<unsigned char> *outBuf, + unsigned int *outSize, const float *inPtr, int width, + int num_lines, int num_channels, + const ZFPCompressionParam ¶m) { zfp_stream *zfp = NULL; zfp_field *field = NULL; assert((width % 4) == 0); assert((num_lines % 4) == 0); - if ((width & 3U) || (num_lines & 3U)) { + if ((size_t(width) & 3U) || (size_t(num_lines) & 3U)) { return false; } // create input array. field = zfp_field_2d(reinterpret_cast<void *>(const_cast<float *>(inPtr)), - zfp_type_float, width, num_lines * num_channels); + zfp_type_float, static_cast<unsigned int>(width), + static_cast<unsigned int>(num_lines * num_channels)); zfp = zfp_stream_open(NULL); if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_RATE) { zfp_stream_set_rate(zfp, param.rate, zfp_type_float, 2, 0); } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) { - zfp_stream_set_precision(zfp, param.precision, zfp_type_float); + zfp_stream_set_precision(zfp, param.precision); } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) { - zfp_stream_set_accuracy(zfp, param.tolerance, zfp_type_float); + zfp_stream_set_accuracy(zfp, param.tolerance); } else { assert(0); } @@ -9582,17 +3385,17 @@ bool CompressZfp(std::vector<unsigned char> *outBuf, unsigned int *outSize, zfp_stream_set_bit_stream(zfp, stream); zfp_field_free(field); - size_t image_size = width * num_lines; + size_t image_size = size_t(width) * size_t(num_lines); - for (int c = 0; c < num_channels; c++) { + for (size_t c = 0; c < size_t(num_channels); c++) { // compress 4x4 pixel block. - for (int y = 0; y < num_lines; y += 4) { - for (int x = 0; x < width; x += 4) { + for (size_t y = 0; y < size_t(num_lines); y += 4) { + for (size_t x = 0; x < size_t(width); x += 4) { float fblock[16]; - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { + for (size_t j = 0; j < 4; j++) { + for (size_t i = 0; i < 4; i++) { fblock[j * 4 + i] = - inPtr[c * image_size + ((y + j) * width + (x + i))]; + inPtr[c * image_size + ((y + j) * size_t(width) + (x + i))]; } } zfp_encode_block_float_2(zfp, fblock); @@ -9601,7 +3404,7 @@ bool CompressZfp(std::vector<unsigned char> *outBuf, unsigned int *outSize, } zfp_stream_flush(zfp); - (*outSize) = zfp_stream_compressed_size(zfp); + (*outSize) = static_cast<unsigned int>(zfp_stream_compressed_size(zfp)); zfp_stream_close(zfp); @@ -9614,6 +3417,9 @@ bool CompressZfp(std::vector<unsigned char> *outBuf, unsigned int *outSize, // ----------------------------------------------------------------- // +// heuristics +#define TINYEXR_DIMENSION_THRESHOLD (1024 * 8192) + // TODO(syoyo): Refactor function arguments. static bool DecodePixelData(/* out */ unsigned char **out_images, const int *requested_pixel_types, @@ -9641,8 +3447,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, reinterpret_cast<unsigned char *>(&outBuf.at(0)), data_ptr, tmpBufLen, data_len, static_cast<int>(num_channels), channels, width, num_lines); - assert(ret); - (void)ret; + if (!ret) { + return false; + } // For PIZ_COMPRESSION: // pixel sample data for channel 0 for scanline 0 @@ -9687,16 +3494,18 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } else { // HALF -> FLOAT FP32 f32 = half_to_float(hf); float *image = reinterpret_cast<float **>(out_images)[c]; + size_t offset = 0; if (line_order == 0) { - image += (static_cast<size_t>(line_no) + v) * + offset = (static_cast<size_t>(line_no) + v) * static_cast<size_t>(x_stride) + u; } else { - image += static_cast<size_t>( + offset = static_cast<size_t>( (height - 1 - (line_no + static_cast<int>(v)))) * static_cast<size_t>(x_stride) + u; } + image += offset; *image = f32.f; } } @@ -9823,16 +3632,19 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } else { // HALF -> FLOAT tinyexr::FP32 f32 = half_to_float(hf); float *image = reinterpret_cast<float **>(out_images)[c]; + size_t offset = 0; if (line_order == 0) { - image += (static_cast<size_t>(line_no) + v) * + offset = (static_cast<size_t>(line_no) + v) * static_cast<size_t>(x_stride) + u; } else { - image += (static_cast<size_t>(height) - 1U - + offset = (static_cast<size_t>(height) - 1U - (static_cast<size_t>(line_no) + v)) * static_cast<size_t>(x_stride) + u; } + image += offset; + *image = f32.f; } } @@ -9905,10 +3717,15 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, pixel_data_size); unsigned long dstLen = static_cast<unsigned long>(outBuf.size()); - assert(dstLen > 0); - tinyexr::DecompressRle(reinterpret_cast<unsigned char *>(&outBuf.at(0)), - dstLen, data_ptr, - static_cast<unsigned long>(data_len)); + if (dstLen == 0) { + return false; + } + + if (!tinyexr::DecompressRle( + reinterpret_cast<unsigned char *>(&outBuf.at(0)), dstLen, data_ptr, + static_cast<unsigned long>(data_len))) { + return false; + } // For RLE_COMPRESSION: // pixel sample data for channel 0 for scanline 0 @@ -10030,8 +3847,10 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { #if TINYEXR_USE_ZFP tinyexr::ZFPCompressionParam zfp_compression_param; - if (!FindZFPCompressionParam(&zfp_compression_param, attributes, - num_attributes)) { + std::string e; + if (!tinyexr::FindZFPCompressionParam(&zfp_compression_param, attributes, + int(num_attributes), &e)) { + // This code path should not be reachable. assert(0); return false; } @@ -10222,7 +4041,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, return true; } -static void DecodeTiledPixelData( +static bool DecodeTiledPixelData( unsigned char **out_images, int *width, int *height, const int *requested_pixel_types, const unsigned char *data_ptr, size_t data_len, int compression_type, int line_order, int data_width, @@ -10231,8 +4050,11 @@ static void DecodeTiledPixelData( const EXRAttribute *attributes, size_t num_channels, const EXRChannelInfo *channels, const std::vector<size_t> &channel_offset_list) { - assert(tile_offset_x * tile_size_x < data_width); - assert(tile_offset_y * tile_size_y < data_height); + // Here, data_width and data_height are the dimensions of the current (sub)level. + if (tile_size_x * tile_offset_x > data_width || + tile_size_y * tile_offset_y > data_height) { + return false; + } // Compute actual image size in a tile. if ((tile_offset_x + 1) * tile_size_x >= data_width) { @@ -10248,11 +4070,11 @@ static void DecodeTiledPixelData( } // Image size = tile size. - DecodePixelData(out_images, requested_pixel_types, data_ptr, data_len, - compression_type, line_order, (*width), tile_size_y, - /* stride */ tile_size_x, /* y */ 0, /* line_no */ 0, - (*height), pixel_data_size, num_attributes, attributes, - num_channels, channels, channel_offset_list); + return DecodePixelData(out_images, requested_pixel_types, data_ptr, data_len, + compression_type, line_order, (*width), tile_size_y, + /* stride */ tile_size_x, /* y */ 0, /* line_no */ 0, + (*height), pixel_data_size, num_attributes, attributes, + num_channels, channels, channel_offset_list); } static bool ComputeChannelLayout(std::vector<size_t> *channel_offset_list, @@ -10326,6 +4148,18 @@ static unsigned char **AllocateImage(int num_channels, return images; } +#ifdef _WIN32 +static inline std::wstring UTF8ToWchar(const std::string &str) { + int wstr_size = + MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0); + std::wstring wstr(wstr_size, 0); + MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], + (int)wstr.size()); + return wstr; +} +#endif + + static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, const EXRVersion *version, std::string *err, const unsigned char *buf, size_t size) { @@ -10364,21 +4198,27 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, bool has_pixel_aspect_ratio = false; bool has_screen_window_center = false; bool has_screen_window_width = false; + bool has_name = false; + bool has_type = false; + + info->name.clear(); + info->type.clear(); - info->data_window[0] = 0; - info->data_window[1] = 0; - info->data_window[2] = 0; - info->data_window[3] = 0; + info->data_window.min_x = 0; + info->data_window.min_y = 0; + info->data_window.max_x = 0; + info->data_window.max_y = 0; info->line_order = 0; // @fixme - info->display_window[0] = 0; - info->display_window[1] = 0; - info->display_window[2] = 0; - info->display_window[3] = 0; + info->display_window.min_x = 0; + info->display_window.min_y = 0; + info->display_window.max_x = 0; + info->display_window.max_y = 0; info->screen_window_center[0] = 0.0f; info->screen_window_center[1] = 0.0f; info->screen_window_width = -1.0f; info->pixel_aspect_ratio = -1.0f; + info->tiled = 0; info->tile_size_x = -1; info->tile_size_y = -1; info->tile_level_mode = -1; @@ -10413,7 +4253,8 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, marker += marker_size; size -= marker_size; - if (version->tiled && attr_name.compare("tiles") == 0) { + // For a multipart file, the version field 9th bit is 0. + if ((version->tiled || version->multipart || version->non_image) && attr_name.compare("tiles") == 0) { unsigned int x_size, y_size; unsigned char tile_mode; assert(data.size() == 9); @@ -10423,13 +4264,21 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, tinyexr::swap4(&x_size); tinyexr::swap4(&y_size); + if (x_size > static_cast<unsigned int>(std::numeric_limits<int>::max()) || + y_size > static_cast<unsigned int>(std::numeric_limits<int>::max())) { + if (err) { + (*err) = "Tile sizes were invalid."; + } + return TINYEXR_ERROR_UNSUPPORTED_FORMAT; + } + info->tile_size_x = static_cast<int>(x_size); info->tile_size_y = static_cast<int>(y_size); // mode = levelMode + roundingMode * 16 info->tile_level_mode = tile_mode & 0x3; info->tile_rounding_mode = (tile_mode >> 4) & 0x1; - + info->tiled = 1; } else if (attr_name.compare("compression") == 0) { bool ok = false; if (data[0] < TINYEXR_COMPRESSIONTYPE_PIZ) { @@ -10494,30 +4343,26 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, } else if (attr_name.compare("dataWindow") == 0) { if (data.size() >= 16) { - memcpy(&info->data_window[0], &data.at(0), sizeof(int)); - memcpy(&info->data_window[1], &data.at(4), sizeof(int)); - memcpy(&info->data_window[2], &data.at(8), sizeof(int)); - memcpy(&info->data_window[3], &data.at(12), sizeof(int)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info->data_window[0])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info->data_window[1])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info->data_window[2])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info->data_window[3])); + memcpy(&info->data_window.min_x, &data.at(0), sizeof(int)); + memcpy(&info->data_window.min_y, &data.at(4), sizeof(int)); + memcpy(&info->data_window.max_x, &data.at(8), sizeof(int)); + memcpy(&info->data_window.max_y, &data.at(12), sizeof(int)); + tinyexr::swap4(&info->data_window.min_x); + tinyexr::swap4(&info->data_window.min_y); + tinyexr::swap4(&info->data_window.max_x); + tinyexr::swap4(&info->data_window.max_y); has_data_window = true; } } else if (attr_name.compare("displayWindow") == 0) { if (data.size() >= 16) { - memcpy(&info->display_window[0], &data.at(0), sizeof(int)); - memcpy(&info->display_window[1], &data.at(4), sizeof(int)); - memcpy(&info->display_window[2], &data.at(8), sizeof(int)); - memcpy(&info->display_window[3], &data.at(12), sizeof(int)); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->display_window[0])); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->display_window[1])); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->display_window[2])); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->display_window[3])); + memcpy(&info->display_window.min_x, &data.at(0), sizeof(int)); + memcpy(&info->display_window.min_y, &data.at(4), sizeof(int)); + memcpy(&info->display_window.max_x, &data.at(8), sizeof(int)); + memcpy(&info->display_window.max_y, &data.at(12), sizeof(int)); + tinyexr::swap4(&info->display_window.min_x); + tinyexr::swap4(&info->display_window.min_y); + tinyexr::swap4(&info->display_window.max_x); + tinyexr::swap4(&info->display_window.max_y); has_display_window = true; } @@ -10529,32 +4374,44 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, } else if (attr_name.compare("pixelAspectRatio") == 0) { if (data.size() >= sizeof(float)) { memcpy(&info->pixel_aspect_ratio, &data.at(0), sizeof(float)); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->pixel_aspect_ratio)); + tinyexr::swap4(&info->pixel_aspect_ratio); has_pixel_aspect_ratio = true; } } else if (attr_name.compare("screenWindowCenter") == 0) { if (data.size() >= 8) { memcpy(&info->screen_window_center[0], &data.at(0), sizeof(float)); memcpy(&info->screen_window_center[1], &data.at(4), sizeof(float)); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->screen_window_center[0])); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->screen_window_center[1])); + tinyexr::swap4(&info->screen_window_center[0]); + tinyexr::swap4(&info->screen_window_center[1]); has_screen_window_center = true; } } else if (attr_name.compare("screenWindowWidth") == 0) { if (data.size() >= sizeof(float)) { memcpy(&info->screen_window_width, &data.at(0), sizeof(float)); - tinyexr::swap4( - reinterpret_cast<unsigned int *>(&info->screen_window_width)); + tinyexr::swap4(&info->screen_window_width); has_screen_window_width = true; } } else if (attr_name.compare("chunkCount") == 0) { if (data.size() >= sizeof(int)) { memcpy(&info->chunk_count, &data.at(0), sizeof(int)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&info->chunk_count)); + tinyexr::swap4(&info->chunk_count); + } + } else if (attr_name.compare("name") == 0) { + if (!data.empty() && data[0]) { + data.push_back(0); + size_t len = strlen(reinterpret_cast<const char*>(&data[0])); + info->name.resize(len); + info->name.assign(reinterpret_cast<const char*>(&data[0]), len); + has_name = true; + } + } else if (attr_name.compare("type") == 0) { + if (!data.empty() && data[0]) { + data.push_back(0); + size_t len = strlen(reinterpret_cast<const char*>(&data[0])); + info->type.resize(len); + info->type.assign(reinterpret_cast<const char*>(&data[0]), len); + has_type = true; } } else { // Custom attribute(up to TINYEXR_MAX_CUSTOM_ATTRIBUTES) @@ -10620,6 +4477,17 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, << std::endl; } + if (version->multipart || version->non_image) { + if (!has_name) { + ss_err << "\"name\" attribute not found in the header." + << std::endl; + } + if (!has_type) { + ss_err << "\"type\" attribute not found in the header." + << std::endl; + } + } + if (!(ss_err.str().empty())) { if (err) { (*err) += ss_err.str(); @@ -10640,22 +4508,40 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { exr_header->screen_window_center[1] = info.screen_window_center[1]; exr_header->screen_window_width = info.screen_window_width; exr_header->chunk_count = info.chunk_count; - exr_header->display_window[0] = info.display_window[0]; - exr_header->display_window[1] = info.display_window[1]; - exr_header->display_window[2] = info.display_window[2]; - exr_header->display_window[3] = info.display_window[3]; - exr_header->data_window[0] = info.data_window[0]; - exr_header->data_window[1] = info.data_window[1]; - exr_header->data_window[2] = info.data_window[2]; - exr_header->data_window[3] = info.data_window[3]; + exr_header->display_window.min_x = info.display_window.min_x; + exr_header->display_window.min_y = info.display_window.min_y; + exr_header->display_window.max_x = info.display_window.max_x; + exr_header->display_window.max_y = info.display_window.max_y; + exr_header->data_window.min_x = info.data_window.min_x; + exr_header->data_window.min_y = info.data_window.min_y; + exr_header->data_window.max_x = info.data_window.max_x; + exr_header->data_window.max_y = info.data_window.max_y; exr_header->line_order = info.line_order; exr_header->compression_type = info.compression_type; - + exr_header->tiled = info.tiled; exr_header->tile_size_x = info.tile_size_x; exr_header->tile_size_y = info.tile_size_y; exr_header->tile_level_mode = info.tile_level_mode; exr_header->tile_rounding_mode = info.tile_rounding_mode; + EXRSetNameAttr(exr_header, info.name.c_str()); + + if (!info.type.empty()) { + if (info.type == "scanlineimage") { + assert(!exr_header->tiled); + } else if (info.type == "tiledimage") { + assert(exr_header->tiled); + } else if (info.type == "deeptile") { + exr_header->non_image = 1; + assert(exr_header->tiled); + } else if (info.type == "deepscanline") { + exr_header->non_image = 1; + assert(!exr_header->tiled); + } else { + assert(false); + } + } + exr_header->num_channels = static_cast<int>(info.channels.size()); exr_header->channels = static_cast<EXRChannelInfo *>(malloc( @@ -10706,7 +4592,7 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { memcpy(exr_header->custom_attributes[i].type, info.attributes[i].type, 256); exr_header->custom_attributes[i].size = info.attributes[i].size; - // Just copy poiner + // Just copy pointer exr_header->custom_attributes[i].value = info.attributes[i].value; } @@ -10717,8 +4603,216 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { exr_header->header_len = info.header_len; } +struct OffsetData { + OffsetData() : num_x_levels(0), num_y_levels(0) {} + std::vector<std::vector<std::vector <tinyexr::tinyexr_uint64> > > offsets; + int num_x_levels; + int num_y_levels; +}; + +int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { + switch (tile_level_mode) { + case TINYEXR_TILE_ONE_LEVEL: + return 0; + + case TINYEXR_TILE_MIPMAP_LEVELS: + return lx; + + case TINYEXR_TILE_RIPMAP_LEVELS: + return lx + ly * num_x_levels; + + default: + assert(false); + } + return 0; +} + +static int LevelSize(int toplevel_size, int level, int tile_rounding_mode) { + assert(level >= 0); + + int b = (int)(1u << (unsigned)level); + int level_size = toplevel_size / b; + + if (tile_rounding_mode == TINYEXR_TILE_ROUND_UP && level_size * b < toplevel_size) + level_size += 1; + + return std::max(level_size, 1); +} + +static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, + const OffsetData& offset_data, + const std::vector<size_t>& channel_offset_list, + int pixel_data_size, + const unsigned char* head, const size_t size, + std::string* err) { + int num_channels = exr_header->num_channels; + + int level_index = LevelIndex(exr_image->level_x, exr_image->level_y, exr_header->tile_level_mode, offset_data.num_x_levels); + int num_y_tiles = (int)offset_data.offsets[level_index].size(); + assert(num_y_tiles); + int num_x_tiles = (int)offset_data.offsets[level_index][0].size(); + assert(num_x_tiles); + int num_tiles = num_x_tiles * num_y_tiles; + + int err_code = TINYEXR_SUCCESS; + + enum { + EF_SUCCESS = 0, + EF_INVALID_DATA = 1, + EF_INSUFFICIENT_DATA = 2, + EF_FAILED_TO_DECODE = 4 + }; +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::atomic<unsigned> error_flag(EF_SUCCESS); +#else + unsigned error_flag(EF_SUCCESS); +#endif + + // Although the spec says : "...the data window is subdivided into an array of smaller rectangles...", + // the IlmImf library allows the dimensions of the tile to be larger (or equal) than the dimensions of the data window. +#if 0 + if ((exr_header->tile_size_x > exr_image->width || exr_header->tile_size_y > exr_image->height) && + exr_image->level_x == 0 && exr_image->level_y == 0) { + if (err) { + (*err) += "Failed to decode tile data.\n"; + } + err_code = TINYEXR_ERROR_INVALID_DATA; + } +#endif + exr_image->tiles = static_cast<EXRTile*>( + calloc(sizeof(EXRTile), static_cast<size_t>(num_tiles))); + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::vector<std::thread> workers; + std::atomic<int> tile_count(0); + + int num_threads = std::max(1, int(std::thread::hardware_concurrency())); + if (num_threads > int(num_tiles)) { + num_threads = int(num_tiles); + } + + for (int t = 0; t < num_threads; t++) { + workers.emplace_back(std::thread([&]() + { + int tile_idx = 0; + while ((tile_idx = tile_count++) < num_tiles) { + +#else +#if TINYEXR_USE_OPENMP +#pragma omp parallel for +#endif + for (int tile_idx = 0; tile_idx < num_tiles; tile_idx++) { +#endif + // Allocate memory for each tile. + exr_image->tiles[tile_idx].images = tinyexr::AllocateImage( + num_channels, exr_header->channels, + exr_header->requested_pixel_types, exr_header->tile_size_x, + exr_header->tile_size_y); + + int x_tile = tile_idx % num_x_tiles; + int y_tile = tile_idx / num_x_tiles; + // 16 byte: tile coordinates + // 4 byte : data size + // ~ : data(uncompressed or compressed) + tinyexr::tinyexr_uint64 offset = offset_data.offsets[level_index][y_tile][x_tile]; + if (offset + sizeof(int) * 5 > size) { + // Insufficient data size. + error_flag |= EF_INSUFFICIENT_DATA; + continue; + } + + size_t data_size = + size_t(size - (offset + sizeof(int) * 5)); + const unsigned char* data_ptr = + reinterpret_cast<const unsigned char*>(head + offset); + + int tile_coordinates[4]; + memcpy(tile_coordinates, data_ptr, sizeof(int) * 4); + tinyexr::swap4(&tile_coordinates[0]); + tinyexr::swap4(&tile_coordinates[1]); + tinyexr::swap4(&tile_coordinates[2]); + tinyexr::swap4(&tile_coordinates[3]); + + if (tile_coordinates[2] != exr_image->level_x) { + // Invalid data. + error_flag |= EF_INVALID_DATA; + continue; + } + if (tile_coordinates[3] != exr_image->level_y) { + // Invalid data. + error_flag |= EF_INVALID_DATA; + continue; + } + + int data_len; + memcpy(&data_len, data_ptr + 16, + sizeof(int)); // 16 = sizeof(tile_coordinates) + tinyexr::swap4(&data_len); + + if (data_len < 2 || size_t(data_len) > data_size) { + // Insufficient data size. + error_flag |= EF_INSUFFICIENT_DATA; + continue; + } + + // Move to data addr: 20 = 16 + 4; + data_ptr += 20; + bool ret = tinyexr::DecodeTiledPixelData( + exr_image->tiles[tile_idx].images, + &(exr_image->tiles[tile_idx].width), + &(exr_image->tiles[tile_idx].height), + exr_header->requested_pixel_types, data_ptr, + static_cast<size_t>(data_len), exr_header->compression_type, + exr_header->line_order, + exr_image->width, exr_image->height, + tile_coordinates[0], tile_coordinates[1], exr_header->tile_size_x, + exr_header->tile_size_y, static_cast<size_t>(pixel_data_size), + static_cast<size_t>(exr_header->num_custom_attributes), + exr_header->custom_attributes, + static_cast<size_t>(exr_header->num_channels), + exr_header->channels, channel_offset_list); + + if (!ret) { + // Failed to decode tile data. + error_flag |= EF_FAILED_TO_DECODE; + } + + exr_image->tiles[tile_idx].offset_x = tile_coordinates[0]; + exr_image->tiles[tile_idx].offset_y = tile_coordinates[1]; + exr_image->tiles[tile_idx].level_x = tile_coordinates[2]; + exr_image->tiles[tile_idx].level_y = tile_coordinates[3]; + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + } + })); + } // num_thread loop + + for (auto& t : workers) { + t.join(); + } + +#else + } // parallel for +#endif + + // Even in the event of an error, the reserved memory may be freed. + exr_image->num_channels = num_channels; + exr_image->num_tiles = static_cast<int>(num_tiles); + + if (error_flag) err_code = TINYEXR_ERROR_INVALID_DATA; + if (err) { + if (error_flag & EF_INSUFFICIENT_DATA) { + (*err) += "Insufficient data length.\n"; + } + if (error_flag & EF_FAILED_TO_DECODE) { + (*err) += "Failed to decode tile data.\n"; + } + } + return err_code; +} + static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, - const std::vector<tinyexr::tinyexr_uint64> &offsets, + const OffsetData& offset_data, const unsigned char *head, const size_t size, std::string *err) { int num_channels = exr_header->num_channels; @@ -10730,11 +4824,57 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, num_scanline_blocks = 32; } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { num_scanline_blocks = 16; + +#if TINYEXR_USE_ZFP + tinyexr::ZFPCompressionParam zfp_compression_param; + if (!FindZFPCompressionParam(&zfp_compression_param, + exr_header->custom_attributes, + int(exr_header->num_custom_attributes), err)) { + return TINYEXR_ERROR_INVALID_HEADER; + } +#endif + } + + if (exr_header->data_window.max_x < exr_header->data_window.min_x || + exr_header->data_window.max_y < exr_header->data_window.min_y) { + if (err) { + (*err) += "Invalid data window.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; } - int data_width = exr_header->data_window[2] - exr_header->data_window[0] + 1; - int data_height = exr_header->data_window[3] - exr_header->data_window[1] + 1; + int data_width = + exr_header->data_window.max_x - exr_header->data_window.min_x + 1; + int data_height = + exr_header->data_window.max_y - exr_header->data_window.min_y + 1; + + // Do not allow too large data_width and data_height. header invalid? + { + if ((data_width > TINYEXR_DIMENSION_THRESHOLD) || (data_height > TINYEXR_DIMENSION_THRESHOLD)) { + if (err) { + std::stringstream ss; + ss << "data_with or data_height too large. data_width: " << data_width + << ", " + << "data_height = " << data_height << std::endl; + (*err) += ss.str(); + } + return TINYEXR_ERROR_INVALID_DATA; + } + if (exr_header->tiled) { + if ((exr_header->tile_size_x > TINYEXR_DIMENSION_THRESHOLD) || (exr_header->tile_size_y > TINYEXR_DIMENSION_THRESHOLD)) { + if (err) { + std::stringstream ss; + ss << "tile with or tile height too large. tile width: " << exr_header->tile_size_x + << ", " + << "tile height = " << exr_header->tile_size_y << std::endl; + (*err) += ss.str(); + } + return TINYEXR_ERROR_INVALID_DATA; + } + } + } + const std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0]; size_t num_blocks = offsets.size(); std::vector<size_t> channel_offset_list; @@ -10749,155 +4889,225 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_ERROR_INVALID_DATA; } - bool invalid_data = false; // TODO(LTE): Use atomic lock for MT safety. +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::atomic<bool> invalid_data(false); +#else + bool invalid_data(false); +#endif if (exr_header->tiled) { - size_t num_tiles = offsets.size(); // = # of blocks - - exr_image->tiles = static_cast<EXRTile *>( - calloc(sizeof(EXRTile), static_cast<size_t>(num_tiles))); - - for (size_t tile_idx = 0; tile_idx < num_tiles; tile_idx++) { - // Allocate memory for each tile. - exr_image->tiles[tile_idx].images = tinyexr::AllocateImage( - num_channels, exr_header->channels, exr_header->requested_pixel_types, - exr_header->tile_size_x, exr_header->tile_size_y); - - // 16 byte: tile coordinates - // 4 byte : data size - // ~ : data(uncompressed or compressed) - if (offsets[tile_idx] + sizeof(int) * 5 > size) { - if (err) { - (*err) += "Insufficient data size.\n"; - } - return TINYEXR_ERROR_INVALID_DATA; + // value check + if (exr_header->tile_size_x < 0) { + if (err) { + std::stringstream ss; + ss << "Invalid tile size x : " << exr_header->tile_size_x << "\n"; + (*err) += ss.str(); } + return TINYEXR_ERROR_INVALID_HEADER; + } - size_t data_size = size - size_t(offsets[tile_idx] + sizeof(int) * 5); - const unsigned char *data_ptr = - reinterpret_cast<const unsigned char *>(head + offsets[tile_idx]); - - int tile_coordinates[4]; - memcpy(tile_coordinates, data_ptr, sizeof(int) * 4); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&tile_coordinates[0])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&tile_coordinates[1])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&tile_coordinates[2])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&tile_coordinates[3])); - - // @todo{ LoD } - if (tile_coordinates[2] != 0) { - return TINYEXR_ERROR_UNSUPPORTED_FEATURE; - } - if (tile_coordinates[3] != 0) { - return TINYEXR_ERROR_UNSUPPORTED_FEATURE; + if (exr_header->tile_size_y < 0) { + if (err) { + std::stringstream ss; + ss << "Invalid tile size y : " << exr_header->tile_size_y << "\n"; + (*err) += ss.str(); } - - int data_len; - memcpy(&data_len, data_ptr + 16, - sizeof(int)); // 16 = sizeof(tile_coordinates) - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data_len)); - - if (data_len < 4 || size_t(data_len) > data_size) { - if (err) { - (*err) += "Insufficient data length.\n"; + return TINYEXR_ERROR_INVALID_HEADER; + } + if (exr_header->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) { + EXRImage* level_image = NULL; + for (int level = 0; level < offset_data.num_x_levels; ++level) { + if (!level_image) { + level_image = exr_image; + } else { + level_image->next_level = new EXRImage; + InitEXRImage(level_image->next_level); + level_image = level_image->next_level; } - return TINYEXR_ERROR_INVALID_DATA; + level_image->width = + LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level, exr_header->tile_rounding_mode); + level_image->height = + LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level, exr_header->tile_rounding_mode); + level_image->level_x = level; + level_image->level_y = level; + + int ret = DecodeTiledLevel(level_image, exr_header, + offset_data, + channel_offset_list, + pixel_data_size, + head, size, + err); + if (ret != TINYEXR_SUCCESS) return ret; } + } else { + EXRImage* level_image = NULL; + for (int level_y = 0; level_y < offset_data.num_y_levels; ++level_y) + for (int level_x = 0; level_x < offset_data.num_x_levels; ++level_x) { + if (!level_image) { + level_image = exr_image; + } else { + level_image->next_level = new EXRImage; + InitEXRImage(level_image->next_level); + level_image = level_image->next_level; + } - // Move to data addr: 20 = 16 + 4; - data_ptr += 20; - - tinyexr::DecodeTiledPixelData( - exr_image->tiles[tile_idx].images, - &(exr_image->tiles[tile_idx].width), - &(exr_image->tiles[tile_idx].height), - exr_header->requested_pixel_types, data_ptr, - static_cast<size_t>(data_len), exr_header->compression_type, - exr_header->line_order, data_width, data_height, tile_coordinates[0], - tile_coordinates[1], exr_header->tile_size_x, exr_header->tile_size_y, - static_cast<size_t>(pixel_data_size), - static_cast<size_t>(exr_header->num_custom_attributes), - exr_header->custom_attributes, - static_cast<size_t>(exr_header->num_channels), exr_header->channels, - channel_offset_list); - - exr_image->tiles[tile_idx].offset_x = tile_coordinates[0]; - exr_image->tiles[tile_idx].offset_y = tile_coordinates[1]; - exr_image->tiles[tile_idx].level_x = tile_coordinates[2]; - exr_image->tiles[tile_idx].level_y = tile_coordinates[3]; - - exr_image->num_tiles = static_cast<int>(num_tiles); + level_image->width = + LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level_x, exr_header->tile_rounding_mode); + level_image->height = + LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level_y, exr_header->tile_rounding_mode); + level_image->level_x = level_x; + level_image->level_y = level_y; + + int ret = DecodeTiledLevel(level_image, exr_header, + offset_data, + channel_offset_list, + pixel_data_size, + head, size, + err); + if (ret != TINYEXR_SUCCESS) return ret; + } } } else { // scanline format + // Don't allow too large image(256GB * pixel_data_size or more). Workaround + // for #104. + size_t total_data_len = + size_t(data_width) * size_t(data_height) * size_t(num_channels); + const bool total_data_len_overflown = + sizeof(void *) == 8 ? (total_data_len >= 0x4000000000) : false; + if ((total_data_len == 0) || total_data_len_overflown) { + if (err) { + std::stringstream ss; + ss << "Image data size is zero or too large: width = " << data_width + << ", height = " << data_height << ", channels = " << num_channels + << std::endl; + (*err) += ss.str(); + } + return TINYEXR_ERROR_INVALID_DATA; + } exr_image->images = tinyexr::AllocateImage( num_channels, exr_header->channels, exr_header->requested_pixel_types, data_width, data_height); -#ifdef _OPENMP +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::vector<std::thread> workers; + std::atomic<int> y_count(0); + + int num_threads = std::max(1, int(std::thread::hardware_concurrency())); + if (num_threads > int(num_blocks)) { + num_threads = int(num_blocks); + } + + for (int t = 0; t < num_threads; t++) { + workers.emplace_back(std::thread([&]() { + int y = 0; + while ((y = y_count++) < int(num_blocks)) { + +#else + +#if TINYEXR_USE_OPENMP #pragma omp parallel for #endif for (int y = 0; y < static_cast<int>(num_blocks); y++) { - size_t y_idx = static_cast<size_t>(y); - - if (offsets[y_idx] + sizeof(int) * 2 > size) { - invalid_data = true; - } else { - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(uncompressed or compressed) - size_t data_size = size - size_t(offsets[y_idx] + sizeof(int) * 2); - const unsigned char *data_ptr = - reinterpret_cast<const unsigned char *>(head + offsets[y_idx]); - - int line_no; - memcpy(&line_no, data_ptr, sizeof(int)); - int data_len; - memcpy(&data_len, data_ptr + 4, sizeof(int)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&line_no)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data_len)); - - if (size_t(data_len) > data_size) { - invalid_data = true; - } else { - int end_line_no = (std::min)(line_no + num_scanline_blocks, - (exr_header->data_window[3] + 1)); - int num_lines = end_line_no - line_no; - // assert(num_lines > 0); +#endif + size_t y_idx = static_cast<size_t>(y); - if (num_lines <= 0) { + if (offsets[y_idx] + sizeof(int) * 2 > size) { invalid_data = true; } else { - // Move to data addr: 8 = 4 + 4; - data_ptr += 8; - - // Adjust line_no with data_window.bmin.y - line_no -= exr_header->data_window[1]; + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(uncompressed or compressed) + size_t data_size = + size_t(size - (offsets[y_idx] + sizeof(int) * 2)); + const unsigned char *data_ptr = + reinterpret_cast<const unsigned char *>(head + offsets[y_idx]); + + int line_no; + memcpy(&line_no, data_ptr, sizeof(int)); + int data_len; + memcpy(&data_len, data_ptr + 4, sizeof(int)); + tinyexr::swap4(&line_no); + tinyexr::swap4(&data_len); + + if (size_t(data_len) > data_size) { + invalid_data = true; - if (line_no < 0) { + } else if ((line_no > (2 << 20)) || (line_no < -(2 << 20))) { + // Too large value. Assume this is invalid + // 2**20 = 1048576 = heuristic value. + invalid_data = true; + } else if (data_len == 0) { + // TODO(syoyo): May be ok to raise the threshold for example + // `data_len < 4` invalid_data = true; } else { - if (!tinyexr::DecodePixelData( - exr_image->images, exr_header->requested_pixel_types, - data_ptr, static_cast<size_t>(data_len), - exr_header->compression_type, exr_header->line_order, - data_width, data_height, data_width, y, line_no, - num_lines, static_cast<size_t>(pixel_data_size), - static_cast<size_t>(exr_header->num_custom_attributes), - exr_header->custom_attributes, - static_cast<size_t>(exr_header->num_channels), - exr_header->channels, channel_offset_list)) { + // line_no may be negative. + int end_line_no = (std::min)(line_no + num_scanline_blocks, + (exr_header->data_window.max_y + 1)); + + int num_lines = end_line_no - line_no; + + if (num_lines <= 0) { invalid_data = true; + } else { + // Move to data addr: 8 = 4 + 4; + data_ptr += 8; + + // Adjust line_no with data_window.bmin.y + + // overflow check + tinyexr_int64 lno = + static_cast<tinyexr_int64>(line_no) - + static_cast<tinyexr_int64>(exr_header->data_window.min_y); + if (lno > std::numeric_limits<int>::max()) { + line_no = -1; // invalid + } else if (lno < -std::numeric_limits<int>::max()) { + line_no = -1; // invalid + } else { + line_no -= exr_header->data_window.min_y; + } + + if (line_no < 0) { + invalid_data = true; + } else { + if (!tinyexr::DecodePixelData( + exr_image->images, exr_header->requested_pixel_types, + data_ptr, static_cast<size_t>(data_len), + exr_header->compression_type, exr_header->line_order, + data_width, data_height, data_width, y, line_no, + num_lines, static_cast<size_t>(pixel_data_size), + static_cast<size_t>( + exr_header->num_custom_attributes), + exr_header->custom_attributes, + static_cast<size_t>(exr_header->num_channels), + exr_header->channels, channel_offset_list)) { + invalid_data = true; + } + } } } } + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) } - } + })); + } + + for (auto &t : workers) { + t.join(); + } +#else } // omp parallel +#endif } if (invalid_data) { + if (err) { + std::stringstream ss; + (*err) += "Invalid data found when decoding pixels.\n"; + } return TINYEXR_ERROR_INVALID_DATA; } @@ -10941,8 +5151,8 @@ static bool ReconstructLineOffsets( return false; } - tinyexr::swap4(reinterpret_cast<unsigned int *>(&y)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data_len)); + tinyexr::swap4(&y); + tinyexr::swap4(&data_len); (*offsets)[i] = offset; @@ -10952,6 +5162,370 @@ static bool ReconstructLineOffsets( return true; } + +static int FloorLog2(unsigned x) { + // + // For x > 0, floorLog2(y) returns floor(log(x)/log(2)). + // + int y = 0; + while (x > 1) { + y += 1; + x >>= 1u; + } + return y; +} + + +static int CeilLog2(unsigned x) { + // + // For x > 0, ceilLog2(y) returns ceil(log(x)/log(2)). + // + int y = 0; + int r = 0; + while (x > 1) { + if (x & 1) + r = 1; + + y += 1; + x >>= 1u; + } + return y + r; +} + +static int RoundLog2(int x, int tile_rounding_mode) { + return (tile_rounding_mode == TINYEXR_TILE_ROUND_DOWN) ? FloorLog2(static_cast<unsigned>(x)) : CeilLog2(static_cast<unsigned>(x)); +} + +static int CalculateNumXLevels(const EXRHeader* exr_header) { + int min_x = exr_header->data_window.min_x; + int max_x = exr_header->data_window.max_x; + int min_y = exr_header->data_window.min_y; + int max_y = exr_header->data_window.max_y; + + int num = 0; + switch (exr_header->tile_level_mode) { + case TINYEXR_TILE_ONE_LEVEL: + + num = 1; + break; + + case TINYEXR_TILE_MIPMAP_LEVELS: + + { + int w = max_x - min_x + 1; + int h = max_y - min_y + 1; + num = RoundLog2(std::max(w, h), exr_header->tile_rounding_mode) + 1; + } + break; + + case TINYEXR_TILE_RIPMAP_LEVELS: + + { + int w = max_x - min_x + 1; + num = RoundLog2(w, exr_header->tile_rounding_mode) + 1; + } + break; + + default: + + assert(false); + } + + return num; +} + +static int CalculateNumYLevels(const EXRHeader* exr_header) { + int min_x = exr_header->data_window.min_x; + int max_x = exr_header->data_window.max_x; + int min_y = exr_header->data_window.min_y; + int max_y = exr_header->data_window.max_y; + int num = 0; + + switch (exr_header->tile_level_mode) { + case TINYEXR_TILE_ONE_LEVEL: + + num = 1; + break; + + case TINYEXR_TILE_MIPMAP_LEVELS: + + { + int w = max_x - min_x + 1; + int h = max_y - min_y + 1; + num = RoundLog2(std::max(w, h), exr_header->tile_rounding_mode) + 1; + } + break; + + case TINYEXR_TILE_RIPMAP_LEVELS: + + { + int h = max_y - min_y + 1; + num = RoundLog2(h, exr_header->tile_rounding_mode) + 1; + } + break; + + default: + + assert(false); + } + + return num; +} + +static void CalculateNumTiles(std::vector<int>& numTiles, + int toplevel_size, + int size, + int tile_rounding_mode) { + for (unsigned i = 0; i < numTiles.size(); i++) { + int l = LevelSize(toplevel_size, i, tile_rounding_mode); + assert(l <= std::numeric_limits<int>::max() - size + 1); + + numTiles[i] = (l + size - 1) / size; + } +} + +static void PrecalculateTileInfo(std::vector<int>& num_x_tiles, + std::vector<int>& num_y_tiles, + const EXRHeader* exr_header) { + int min_x = exr_header->data_window.min_x; + int max_x = exr_header->data_window.max_x; + int min_y = exr_header->data_window.min_y; + int max_y = exr_header->data_window.max_y; + + int num_x_levels = CalculateNumXLevels(exr_header); + int num_y_levels = CalculateNumYLevels(exr_header); + + num_x_tiles.resize(num_x_levels); + num_y_tiles.resize(num_y_levels); + + CalculateNumTiles(num_x_tiles, + max_x - min_x + 1, + exr_header->tile_size_x, + exr_header->tile_rounding_mode); + + CalculateNumTiles(num_y_tiles, + max_y - min_y + 1, + exr_header->tile_size_y, + exr_header->tile_rounding_mode); +} + +static void InitSingleResolutionOffsets(OffsetData& offset_data, size_t num_blocks) { + offset_data.offsets.resize(1); + offset_data.offsets[0].resize(1); + offset_data.offsets[0][0].resize(num_blocks); + offset_data.num_x_levels = 1; + offset_data.num_y_levels = 1; +} + +// Return sum of tile blocks. +static int InitTileOffsets(OffsetData& offset_data, + const EXRHeader* exr_header, + const std::vector<int>& num_x_tiles, + const std::vector<int>& num_y_tiles) { + int num_tile_blocks = 0; + offset_data.num_x_levels = static_cast<int>(num_x_tiles.size()); + offset_data.num_y_levels = static_cast<int>(num_y_tiles.size()); + switch (exr_header->tile_level_mode) { + case TINYEXR_TILE_ONE_LEVEL: + case TINYEXR_TILE_MIPMAP_LEVELS: + assert(offset_data.num_x_levels == offset_data.num_y_levels); + offset_data.offsets.resize(offset_data.num_x_levels); + + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { + offset_data.offsets[l].resize(num_y_tiles[l]); + + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) { + offset_data.offsets[l][dy].resize(num_x_tiles[l]); + num_tile_blocks += num_x_tiles[l]; + } + } + break; + + case TINYEXR_TILE_RIPMAP_LEVELS: + + offset_data.offsets.resize(static_cast<size_t>(offset_data.num_x_levels) * static_cast<size_t>(offset_data.num_y_levels)); + + for (int ly = 0; ly < offset_data.num_y_levels; ++ly) { + for (int lx = 0; lx < offset_data.num_x_levels; ++lx) { + int l = ly * offset_data.num_x_levels + lx; + offset_data.offsets[l].resize(num_y_tiles[ly]); + + for (size_t dy = 0; dy < offset_data.offsets[l].size(); ++dy) { + offset_data.offsets[l][dy].resize(num_x_tiles[lx]); + num_tile_blocks += num_x_tiles[lx]; + } + } + } + break; + + default: + assert(false); + } + return num_tile_blocks; +} + +static bool IsAnyOffsetsAreInvalid(const OffsetData& offset_data) { + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) + for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) + if (reinterpret_cast<const tinyexr::tinyexr_int64&>(offset_data.offsets[l][dy][dx]) <= 0) + return true; + + return false; +} + +static bool isValidTile(const EXRHeader* exr_header, + const OffsetData& offset_data, + int dx, int dy, int lx, int ly) { + if (lx < 0 || ly < 0 || dx < 0 || dy < 0) return false; + int num_x_levels = offset_data.num_x_levels; + int num_y_levels = offset_data.num_y_levels; + switch (exr_header->tile_level_mode) { + case TINYEXR_TILE_ONE_LEVEL: + + if (lx == 0 && + ly == 0 && + offset_data.offsets.size() > 0 && + offset_data.offsets[0].size() > static_cast<size_t>(dy) && + offset_data.offsets[0][dy].size() > static_cast<size_t>(dx)) { + return true; + } + + break; + + case TINYEXR_TILE_MIPMAP_LEVELS: + + if (lx < num_x_levels && + ly < num_y_levels && + offset_data.offsets.size() > static_cast<size_t>(lx) && + offset_data.offsets[lx].size() > static_cast<size_t>(dy) && + offset_data.offsets[lx][dy].size() > static_cast<size_t>(dx)) { + return true; + } + + break; + + case TINYEXR_TILE_RIPMAP_LEVELS: + { + size_t idx = static_cast<size_t>(lx) + static_cast<size_t>(ly)* static_cast<size_t>(num_x_levels); + if (lx < num_x_levels && + ly < num_y_levels && + (offset_data.offsets.size() > idx) && + offset_data.offsets[idx].size() > static_cast<size_t>(dy) && + offset_data.offsets[idx][dy].size() > static_cast<size_t>(dx)) { + return true; + } + } + + break; + + default: + + return false; + } + + return false; +} + +static void ReconstructTileOffsets(OffsetData& offset_data, + const EXRHeader* exr_header, + const unsigned char* head, const unsigned char* marker, const size_t /*size*/, + bool isMultiPartFile, + bool isDeep) { + int numXLevels = offset_data.num_x_levels; + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) { + for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { + tinyexr::tinyexr_uint64 tileOffset = marker - head; + + if (isMultiPartFile) { + //int partNumber; + marker += sizeof(int); + } + + int tileX; + memcpy(&tileX, marker, sizeof(int)); + tinyexr::swap4(&tileX); + marker += sizeof(int); + + int tileY; + memcpy(&tileY, marker, sizeof(int)); + tinyexr::swap4(&tileY); + marker += sizeof(int); + + int levelX; + memcpy(&levelX, marker, sizeof(int)); + tinyexr::swap4(&levelX); + marker += sizeof(int); + + int levelY; + memcpy(&levelY, marker, sizeof(int)); + tinyexr::swap4(&levelY); + marker += sizeof(int); + + if (isDeep) { + tinyexr::tinyexr_int64 packed_offset_table_size; + memcpy(&packed_offset_table_size, marker, sizeof(tinyexr::tinyexr_int64)); + tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64*>(&packed_offset_table_size)); + marker += sizeof(tinyexr::tinyexr_int64); + + tinyexr::tinyexr_int64 packed_sample_size; + memcpy(&packed_sample_size, marker, sizeof(tinyexr::tinyexr_int64)); + tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64*>(&packed_sample_size)); + marker += sizeof(tinyexr::tinyexr_int64); + + // next Int64 is unpacked sample size - skip that too + marker += packed_offset_table_size + packed_sample_size + 8; + + } else { + + int dataSize; + memcpy(&dataSize, marker, sizeof(int)); + tinyexr::swap4(&dataSize); + marker += sizeof(int); + marker += dataSize; + } + + if (!isValidTile(exr_header, offset_data, + tileX, tileY, levelX, levelY)) + return; + + int level_idx = LevelIndex(levelX, levelY, exr_header->tile_level_mode, numXLevels); + offset_data.offsets[level_idx][tileY][tileX] = tileOffset; + } + } + } +} + +// marker output is also +static int ReadOffsets(OffsetData& offset_data, + const unsigned char* head, + const unsigned char*& marker, + const size_t size, + const char** err) { + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) { + for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { + tinyexr::tinyexr_uint64 offset; + if ((marker + sizeof(tinyexr_uint64)) >= (head + size)) { + tinyexr::SetErrorMessage("Insufficient data size in offset table.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + + memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64)); + tinyexr::swap8(&offset); + if (offset >= size) { + tinyexr::SetErrorMessage("Invalid offset value in DecodeEXRImage.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + marker += sizeof(tinyexr::tinyexr_uint64); // = 8 + offset_data.offsets[l][dy][dx] = offset; + } + } + } + return TINYEXR_SUCCESS; +} + static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, const unsigned char *head, const unsigned char *marker, const size_t size, @@ -10971,109 +5545,144 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, num_scanline_blocks = 16; } - int data_width = exr_header->data_window[2] - exr_header->data_window[0]; - if (data_width >= std::numeric_limits<int>::max()) { + if (exr_header->data_window.max_x < exr_header->data_window.min_x || + exr_header->data_window.max_x - exr_header->data_window.min_x == + std::numeric_limits<int>::max()) { // Issue 63 - tinyexr::SetErrorMessage("Invalid data window value", err); + tinyexr::SetErrorMessage("Invalid data width value", err); return TINYEXR_ERROR_INVALID_DATA; } - data_width++; + int data_width = + exr_header->data_window.max_x - exr_header->data_window.min_x + 1; - int data_height = exr_header->data_window[3] - exr_header->data_window[1]; - if (data_height >= std::numeric_limits<int>::max()) { + if (exr_header->data_window.max_y < exr_header->data_window.min_y || + exr_header->data_window.max_y - exr_header->data_window.min_y == + std::numeric_limits<int>::max()) { tinyexr::SetErrorMessage("Invalid data height value", err); return TINYEXR_ERROR_INVALID_DATA; } - data_height++; + int data_height = + exr_header->data_window.max_y - exr_header->data_window.min_y + 1; - if ((data_width < 0) || (data_height < 0)) { - tinyexr::SetErrorMessage("data window or data height is negative.", err); - return TINYEXR_ERROR_INVALID_DATA; + // Do not allow too large data_width and data_height. header invalid? + { + if (data_width > TINYEXR_DIMENSION_THRESHOLD) { + tinyexr::SetErrorMessage("data width too large.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + if (data_height > TINYEXR_DIMENSION_THRESHOLD) { + tinyexr::SetErrorMessage("data height too large.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + } + + if (exr_header->tiled) { + if (exr_header->tile_size_x > TINYEXR_DIMENSION_THRESHOLD) { + tinyexr::SetErrorMessage("tile width too large.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + if (exr_header->tile_size_y > TINYEXR_DIMENSION_THRESHOLD) { + tinyexr::SetErrorMessage("tile height too large.", err); + return TINYEXR_ERROR_INVALID_DATA; + } } // Read offset tables. + OffsetData offset_data; size_t num_blocks = 0; + // For a multi-resolution image, the size of the offset table will be calculated from the other attributes of the header. + // If chunk_count > 0 then chunk_count must be equal to the calculated tile count. + if (exr_header->tiled) { + { + std::vector<int> num_x_tiles, num_y_tiles; + PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header); + num_blocks = InitTileOffsets(offset_data, exr_header, num_x_tiles, num_y_tiles); + if (exr_header->chunk_count > 0) { + if (exr_header->chunk_count != static_cast<int>(num_blocks)) { + tinyexr::SetErrorMessage("Invalid offset table size.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + } + } - if (exr_header->chunk_count > 0) { + int ret = ReadOffsets(offset_data, head, marker, size, err); + if (ret != TINYEXR_SUCCESS) return ret; + if (IsAnyOffsetsAreInvalid(offset_data)) { + ReconstructTileOffsets(offset_data, exr_header, + head, marker, size, + exr_header->multipart, exr_header->non_image); + } + } else if (exr_header->chunk_count > 0) { // Use `chunkCount` attribute. num_blocks = static_cast<size_t>(exr_header->chunk_count); - } else if (exr_header->tiled) { - // @todo { LoD } - size_t num_x_tiles = static_cast<size_t>(data_width) / - static_cast<size_t>(exr_header->tile_size_x); - if (num_x_tiles * static_cast<size_t>(exr_header->tile_size_x) < - static_cast<size_t>(data_width)) { - num_x_tiles++; - } - size_t num_y_tiles = static_cast<size_t>(data_height) / - static_cast<size_t>(exr_header->tile_size_y); - if (num_y_tiles * static_cast<size_t>(exr_header->tile_size_y) < - static_cast<size_t>(data_height)) { - num_y_tiles++; - } - - num_blocks = num_x_tiles * num_y_tiles; + InitSingleResolutionOffsets(offset_data, num_blocks); } else { num_blocks = static_cast<size_t>(data_height) / - static_cast<size_t>(num_scanline_blocks); + static_cast<size_t>(num_scanline_blocks); if (num_blocks * static_cast<size_t>(num_scanline_blocks) < - static_cast<size_t>(data_height)) { + static_cast<size_t>(data_height)) { num_blocks++; } - } - - std::vector<tinyexr::tinyexr_uint64> offsets(num_blocks); - - for (size_t y = 0; y < num_blocks; y++) { - tinyexr::tinyexr_uint64 offset; - // Issue #81 - if ((marker + sizeof(tinyexr_uint64)) >= (head + size)) { - tinyexr::SetErrorMessage("Insufficient data size in offset table.", err); - return TINYEXR_ERROR_INVALID_DATA; - } - memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64)); - tinyexr::swap8(&offset); - if (offset >= size) { - tinyexr::SetErrorMessage("Invalid offset value in DecodeEXRImage.", err); - return TINYEXR_ERROR_INVALID_DATA; - } - marker += sizeof(tinyexr::tinyexr_uint64); // = 8 - offsets[y] = offset; + InitSingleResolutionOffsets(offset_data, num_blocks); } - // If line offsets are invalid, we try to reconstruct it. - // See OpenEXR/IlmImf/ImfScanLineInputFile.cpp::readLineOffsets() for details. - for (size_t y = 0; y < num_blocks; y++) { - if (offsets[y] <= 0) { - // TODO(syoyo) Report as warning? - // if (err) { - // stringstream ss; - // ss << "Incomplete lineOffsets." << std::endl; - // (*err) += ss.str(); - //} - bool ret = + if (!exr_header->tiled) { + std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0]; + for (size_t y = 0; y < num_blocks; y++) { + tinyexr::tinyexr_uint64 offset; + // Issue #81 + if ((marker + sizeof(tinyexr_uint64)) >= (head + size)) { + tinyexr::SetErrorMessage("Insufficient data size in offset table.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + + memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64)); + tinyexr::swap8(&offset); + if (offset >= size) { + tinyexr::SetErrorMessage("Invalid offset value in DecodeEXRImage.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + marker += sizeof(tinyexr::tinyexr_uint64); // = 8 + offsets[y] = offset; + } + + // If line offsets are invalid, we try to reconstruct it. + // See OpenEXR/IlmImf/ImfScanLineInputFile.cpp::readLineOffsets() for details. + for (size_t y = 0; y < num_blocks; y++) { + if (offsets[y] <= 0) { + // TODO(syoyo) Report as warning? + // if (err) { + // stringstream ss; + // ss << "Incomplete lineOffsets." << std::endl; + // (*err) += ss.str(); + //} + bool ret = ReconstructLineOffsets(&offsets, num_blocks, head, marker, size); - if (ret) { - // OK - break; - } else { - tinyexr::SetErrorMessage( + if (ret) { + // OK + break; + } else { + tinyexr::SetErrorMessage( "Cannot reconstruct lineOffset table in DecodeEXRImage.", err); - return TINYEXR_ERROR_INVALID_DATA; + return TINYEXR_ERROR_INVALID_DATA; + } } } } { std::string e; - int ret = DecodeChunk(exr_image, exr_header, offsets, head, size, &e); + int ret = DecodeChunk(exr_image, exr_header, offset_data, head, size, &e); if (ret != TINYEXR_SUCCESS) { if (!e.empty()) { tinyexr::SetErrorMessage(e, err); } +#if 1 + FreeEXRImage(exr_image); +#else // release memory(if exists) if ((exr_header->num_channels > 0) && exr_image && exr_image->images) { for (size_t c = 0; c < size_t(exr_header->num_channels); c++) { @@ -11085,16 +5694,117 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, free(exr_image->images); exr_image->images = NULL; } +#endif } return ret; } } +static void GetLayers(const EXRHeader &exr_header, + std::vector<std::string> &layer_names) { + // Naive implementation + // Group channels by layers + // go over all channel names, split by periods + // collect unique names + layer_names.clear(); + for (int c = 0; c < exr_header.num_channels; c++) { + std::string full_name(exr_header.channels[c].name); + const size_t pos = full_name.find_last_of('.'); + if (pos != std::string::npos && pos != 0 && pos + 1 < full_name.size()) { + full_name.erase(pos); + if (std::find(layer_names.begin(), layer_names.end(), full_name) == + layer_names.end()) + layer_names.push_back(full_name); + } + } +} + +struct LayerChannel { + explicit LayerChannel(size_t i, std::string n) : index(i), name(n) {} + size_t index; + std::string name; +}; + +static void ChannelsInLayer(const EXRHeader &exr_header, + const std::string layer_name, + std::vector<LayerChannel> &channels) { + channels.clear(); + for (int c = 0; c < exr_header.num_channels; c++) { + std::string ch_name(exr_header.channels[c].name); + if (layer_name.empty()) { + const size_t pos = ch_name.find_last_of('.'); + if (pos != std::string::npos && pos < ch_name.size()) { + ch_name = ch_name.substr(pos + 1); + } + } else { + const size_t pos = ch_name.find(layer_name + '.'); + if (pos == std::string::npos) continue; + if (pos == 0) { + ch_name = ch_name.substr(layer_name.size() + 1); + } + } + LayerChannel ch(size_t(c), ch_name); + channels.push_back(ch); + } +} + } // namespace tinyexr +int EXRLayers(const char *filename, const char **layer_names[], int *num_layers, + const char **err) { + EXRVersion exr_version; + EXRHeader exr_header; + InitEXRHeader(&exr_header); + + { + int ret = ParseEXRVersionFromFile(&exr_version, filename); + if (ret != TINYEXR_SUCCESS) { + tinyexr::SetErrorMessage("Invalid EXR header.", err); + return ret; + } + + if (exr_version.multipart || exr_version.non_image) { + tinyexr::SetErrorMessage( + "Loading multipart or DeepImage is not supported in LoadEXR() API", + err); + return TINYEXR_ERROR_INVALID_DATA; // @fixme. + } + } + + int ret = ParseEXRHeaderFromFile(&exr_header, &exr_version, filename, err); + if (ret != TINYEXR_SUCCESS) { + FreeEXRHeader(&exr_header); + return ret; + } + + std::vector<std::string> layer_vec; + tinyexr::GetLayers(exr_header, layer_vec); + + (*num_layers) = int(layer_vec.size()); + (*layer_names) = static_cast<const char **>( + malloc(sizeof(const char *) * static_cast<size_t>(layer_vec.size()))); + for (size_t c = 0; c < static_cast<size_t>(layer_vec.size()); c++) { +#ifdef _MSC_VER + (*layer_names)[c] = _strdup(layer_vec[c].c_str()); +#else + (*layer_names)[c] = strdup(layer_vec[c].c_str()); +#endif + } + + FreeEXRHeader(&exr_header); + return TINYEXR_SUCCESS; +} + int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, const char **err) { + return LoadEXRWithLayer(out_rgba, width, height, filename, + /* layername */ NULL, err); +} + +int LoadEXRWithLayer(float **out_rgba, int *width, int *height, + const char *filename, const char *layername, + const char **err) { if (out_rgba == NULL) { tinyexr::SetErrorMessage("Invalid argument for LoadEXR()", err); return TINYEXR_ERROR_INVALID_ARGUMENT; @@ -11109,7 +5819,10 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, { int ret = ParseEXRVersionFromFile(&exr_version, filename); if (ret != TINYEXR_SUCCESS) { - tinyexr::SetErrorMessage("Invalid EXR header.", err); + std::stringstream ss; + ss << "Failed to open EXR file or read version info from EXR file. code(" + << ret << ")"; + tinyexr::SetErrorMessage(ss.str(), err); return ret; } @@ -11136,6 +5849,7 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, } } + // TODO: Probably limit loading to layers (channels) selected by layer index { int ret = LoadEXRImageFromFile(&exr_image, &exr_header, filename, err); if (ret != TINYEXR_SUCCESS) { @@ -11149,33 +5863,85 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, int idxG = -1; int idxB = -1; int idxA = -1; - for (int c = 0; c < exr_header.num_channels; c++) { - if (strcmp(exr_header.channels[c].name, "R") == 0) { - idxR = c; - } else if (strcmp(exr_header.channels[c].name, "G") == 0) { - idxG = c; - } else if (strcmp(exr_header.channels[c].name, "B") == 0) { - idxB = c; - } else if (strcmp(exr_header.channels[c].name, "A") == 0) { - idxA = c; - } + + std::vector<std::string> layer_names; + tinyexr::GetLayers(exr_header, layer_names); + + std::vector<tinyexr::LayerChannel> channels; + tinyexr::ChannelsInLayer( + exr_header, layername == NULL ? "" : std::string(layername), channels); + + if (channels.size() < 1) { + tinyexr::SetErrorMessage("Layer Not Found", err); + FreeEXRHeader(&exr_header); + FreeEXRImage(&exr_image); + return TINYEXR_ERROR_LAYER_NOT_FOUND; } - if ((idxA == 0) && (idxR == -1) && (idxG == -1) && (idxB == -1)) { - // Alpha channel only. + size_t ch_count = channels.size() < 4 ? channels.size() : 4; + for (size_t c = 0; c < ch_count; c++) { + const tinyexr::LayerChannel &ch = channels[c]; - if (exr_header.tiled) { - // todo.implement this + if (ch.name == "R") { + idxR = int(ch.index); + } else if (ch.name == "G") { + idxG = int(ch.index); + } else if (ch.name == "B") { + idxB = int(ch.index); + } else if (ch.name == "A") { + idxA = int(ch.index); } + } + + if (channels.size() == 1) { + int chIdx = int(channels.front().index); + // Grayscale channel only. + (*out_rgba) = reinterpret_cast<float *>( malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) * static_cast<size_t>(exr_image.height))); - for (int i = 0; i < exr_image.width * exr_image.height; i++) { - const float val = reinterpret_cast<float **>(exr_image.images)[0][i]; - (*out_rgba)[4 * i + 0] = val; - (*out_rgba)[4 * i + 1] = val; - (*out_rgba)[4 * i + 2] = val; - (*out_rgba)[4 * i + 3] = val; + + if (exr_header.tiled) { + for (int it = 0; it < exr_image.num_tiles; it++) { + for (int j = 0; j < exr_header.tile_size_y; j++) { + for (int i = 0; i < exr_header.tile_size_x; i++) { + const int ii = exr_image.tiles[it].offset_x * + static_cast<int>(exr_header.tile_size_x) + + i; + const int jj = exr_image.tiles[it].offset_y * + static_cast<int>(exr_header.tile_size_y) + + j; + const int idx = ii + jj * static_cast<int>(exr_image.width); + + // out of region check. + if (ii >= exr_image.width) { + continue; + } + if (jj >= exr_image.height) { + continue; + } + const int srcIdx = i + j * exr_header.tile_size_x; + unsigned char **src = exr_image.tiles[it].images; + (*out_rgba)[4 * idx + 0] = + reinterpret_cast<float **>(src)[chIdx][srcIdx]; + (*out_rgba)[4 * idx + 1] = + reinterpret_cast<float **>(src)[chIdx][srcIdx]; + (*out_rgba)[4 * idx + 2] = + reinterpret_cast<float **>(src)[chIdx][srcIdx]; + (*out_rgba)[4 * idx + 3] = + reinterpret_cast<float **>(src)[chIdx][srcIdx]; + } + } + } + } else { + for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const float val = + reinterpret_cast<float **>(exr_image.images)[chIdx][i]; + (*out_rgba)[4 * i + 0] = val; + (*out_rgba)[4 * i + 1] = val; + (*out_rgba)[4 * i + 2] = val; + (*out_rgba)[4 * i + 3] = val; + } } } else { // Assume RGB(A) @@ -11183,22 +5949,22 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, if (idxR == -1) { tinyexr::SetErrorMessage("R channel not found", err); - // @todo { free exr_image } FreeEXRHeader(&exr_header); + FreeEXRImage(&exr_image); return TINYEXR_ERROR_INVALID_DATA; } if (idxG == -1) { tinyexr::SetErrorMessage("G channel not found", err); - // @todo { free exr_image } FreeEXRHeader(&exr_header); + FreeEXRImage(&exr_image); return TINYEXR_ERROR_INVALID_DATA; } if (idxB == -1) { tinyexr::SetErrorMessage("B channel not found", err); - // @todo { free exr_image } FreeEXRHeader(&exr_header); + FreeEXRImage(&exr_image); return TINYEXR_ERROR_INVALID_DATA; } @@ -11207,7 +5973,7 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, static_cast<size_t>(exr_image.height))); if (exr_header.tiled) { for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) + for (int j = 0; j < exr_header.tile_size_y; j++) { for (int i = 0; i < exr_header.tile_size_x; i++) { const int ii = exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; @@ -11237,6 +6003,7 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, (*out_rgba)[4 * idx + 3] = 1.0; } } + } } } else { for (int i = 0; i < exr_image.width * exr_image.height; i++) { @@ -11265,6 +6032,17 @@ int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, return TINYEXR_SUCCESS; } +int IsEXR(const char *filename) { + EXRVersion exr_version; + + int ret = ParseEXRVersionFromFile(&exr_version, filename); + if (ret != TINYEXR_SUCCESS) { + return ret; + } + + return TINYEXR_SUCCESS; +} + int ParseEXRHeaderFromMemory(EXRHeader *exr_header, const EXRVersion *version, const unsigned char *memory, size_t size, const char **err) { @@ -11300,8 +6078,8 @@ int ParseEXRHeaderFromMemory(EXRHeader *exr_header, const EXRVersion *version, ConvertHeader(exr_header, info); - // transfoer `tiled` from version. - exr_header->tiled = version->tiled; + exr_header->multipart = version->multipart ? 1 : 0; + exr_header->non_image = version->non_image ? 1 : 0; return ret; } @@ -11322,7 +6100,9 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, int ret = ParseEXRVersionFromMemory(&exr_version, memory, size); if (ret != TINYEXR_SUCCESS) { - tinyexr::SetErrorMessage("Failed to parse EXR version", err); + std::stringstream ss; + ss << "Failed to parse EXR version. code(" << ret << ")"; + tinyexr::SetErrorMessage(ss.str(), err); return ret; } @@ -11361,75 +6141,127 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, } } - if (idxR == -1) { - tinyexr::SetErrorMessage("R channel not found", err); - - // @todo { free exr_image } - return TINYEXR_ERROR_INVALID_DATA; - } - - if (idxG == -1) { - tinyexr::SetErrorMessage("G channel not found", err); - // @todo { free exr_image } - return TINYEXR_ERROR_INVALID_DATA; - } - - if (idxB == -1) { - tinyexr::SetErrorMessage("B channel not found", err); - // @todo { free exr_image } - return TINYEXR_ERROR_INVALID_DATA; - } + // TODO(syoyo): Refactor removing same code as used in LoadEXR(). + if (exr_header.num_channels == 1) { + // Grayscale channel only. - (*out_rgba) = reinterpret_cast<float *>( - malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) * - static_cast<size_t>(exr_image.height))); + (*out_rgba) = reinterpret_cast<float *>( + malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) * + static_cast<size_t>(exr_image.height))); - if (exr_header.tiled) { - for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) - for (int i = 0; i < exr_header.tile_size_x; i++) { - const int ii = - exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; - const int jj = - exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; - const int idx = ii + jj * exr_image.width; + if (exr_header.tiled) { + for (int it = 0; it < exr_image.num_tiles; it++) { + for (int j = 0; j < exr_header.tile_size_y; j++) { + for (int i = 0; i < exr_header.tile_size_x; i++) { + const int ii = + exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; + const int jj = + exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; + const int idx = ii + jj * exr_image.width; - // out of region check. - if (ii >= exr_image.width) { - continue; - } - if (jj >= exr_image.height) { - continue; - } - const int srcIdx = i + j * exr_header.tile_size_x; - unsigned char **src = exr_image.tiles[it].images; - (*out_rgba)[4 * idx + 0] = - reinterpret_cast<float **>(src)[idxR][srcIdx]; - (*out_rgba)[4 * idx + 1] = - reinterpret_cast<float **>(src)[idxG][srcIdx]; - (*out_rgba)[4 * idx + 2] = - reinterpret_cast<float **>(src)[idxB][srcIdx]; - if (idxA != -1) { + // out of region check. + if (ii >= exr_image.width) { + continue; + } + if (jj >= exr_image.height) { + continue; + } + const int srcIdx = i + j * exr_header.tile_size_x; + unsigned char **src = exr_image.tiles[it].images; + (*out_rgba)[4 * idx + 0] = + reinterpret_cast<float **>(src)[0][srcIdx]; + (*out_rgba)[4 * idx + 1] = + reinterpret_cast<float **>(src)[0][srcIdx]; + (*out_rgba)[4 * idx + 2] = + reinterpret_cast<float **>(src)[0][srcIdx]; (*out_rgba)[4 * idx + 3] = - reinterpret_cast<float **>(src)[idxA][srcIdx]; - } else { - (*out_rgba)[4 * idx + 3] = 1.0; + reinterpret_cast<float **>(src)[0][srcIdx]; } } + } + } else { + for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const float val = reinterpret_cast<float **>(exr_image.images)[0][i]; + (*out_rgba)[4 * i + 0] = val; + (*out_rgba)[4 * i + 1] = val; + (*out_rgba)[4 * i + 2] = val; + (*out_rgba)[4 * i + 3] = val; + } } + } else { - for (int i = 0; i < exr_image.width * exr_image.height; i++) { - (*out_rgba)[4 * i + 0] = - reinterpret_cast<float **>(exr_image.images)[idxR][i]; - (*out_rgba)[4 * i + 1] = - reinterpret_cast<float **>(exr_image.images)[idxG][i]; - (*out_rgba)[4 * i + 2] = - reinterpret_cast<float **>(exr_image.images)[idxB][i]; - if (idxA != -1) { - (*out_rgba)[4 * i + 3] = - reinterpret_cast<float **>(exr_image.images)[idxA][i]; - } else { - (*out_rgba)[4 * i + 3] = 1.0; + // TODO(syoyo): Support non RGBA image. + + if (idxR == -1) { + tinyexr::SetErrorMessage("R channel not found", err); + + // @todo { free exr_image } + return TINYEXR_ERROR_INVALID_DATA; + } + + if (idxG == -1) { + tinyexr::SetErrorMessage("G channel not found", err); + // @todo { free exr_image } + return TINYEXR_ERROR_INVALID_DATA; + } + + if (idxB == -1) { + tinyexr::SetErrorMessage("B channel not found", err); + // @todo { free exr_image } + return TINYEXR_ERROR_INVALID_DATA; + } + + (*out_rgba) = reinterpret_cast<float *>( + malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) * + static_cast<size_t>(exr_image.height))); + + if (exr_header.tiled) { + for (int it = 0; it < exr_image.num_tiles; it++) { + for (int j = 0; j < exr_header.tile_size_y; j++) + for (int i = 0; i < exr_header.tile_size_x; i++) { + const int ii = + exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; + const int jj = + exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; + const int idx = ii + jj * exr_image.width; + + // out of region check. + if (ii >= exr_image.width) { + continue; + } + if (jj >= exr_image.height) { + continue; + } + const int srcIdx = i + j * exr_header.tile_size_x; + unsigned char **src = exr_image.tiles[it].images; + (*out_rgba)[4 * idx + 0] = + reinterpret_cast<float **>(src)[idxR][srcIdx]; + (*out_rgba)[4 * idx + 1] = + reinterpret_cast<float **>(src)[idxG][srcIdx]; + (*out_rgba)[4 * idx + 2] = + reinterpret_cast<float **>(src)[idxB][srcIdx]; + if (idxA != -1) { + (*out_rgba)[4 * idx + 3] = + reinterpret_cast<float **>(src)[idxA][srcIdx]; + } else { + (*out_rgba)[4 * idx + 3] = 1.0; + } + } + } + } else { + for (int i = 0; i < exr_image.width * exr_image.height; i++) { + (*out_rgba)[4 * i + 0] = + reinterpret_cast<float **>(exr_image.images)[idxR][i]; + (*out_rgba)[4 * i + 1] = + reinterpret_cast<float **>(exr_image.images)[idxG][i]; + (*out_rgba)[4 * i + 2] = + reinterpret_cast<float **>(exr_image.images)[idxB][i]; + if (idxA != -1) { + (*out_rgba)[4 * i + 3] = + reinterpret_cast<float **>(exr_image.images)[idxA][i]; + } else { + (*out_rgba)[4 * i + 3] = 1.0; + } } } } @@ -11450,11 +6282,22 @@ int LoadEXRImageFromFile(EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "rb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); + // TODO(syoyo): return wfopen_s erro code + return TINYEXR_ERROR_CANT_OPEN_FILE; + } #else - FILE *fp = fopen(filename, "rb"); + // Unknown compiler + fp = fopen(filename, "rb"); +#endif +#else + fp = fopen(filename, "rb"); #endif if (!fp) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); @@ -11509,510 +6352,919 @@ int LoadEXRImageFromMemory(EXRImage *exr_image, const EXRHeader *exr_header, err); } -size_t SaveEXRImageToMemory(const EXRImage *exr_image, - const EXRHeader *exr_header, - unsigned char **memory_out, const char **err) { - if (exr_image == NULL || memory_out == NULL || - exr_header->compression_type < 0) { - tinyexr::SetErrorMessage("Invalid argument for SaveEXRImageToMemory", err); - return 0; - } +namespace tinyexr +{ -#if !TINYEXR_USE_PIZ - if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { - tinyexr::SetErrorMessage("PIZ compression is not supported in this build", - err); - return 0; - } -#endif +// out_data must be allocated initially with the block-header size +// of the current image(-part) type +static bool EncodePixelData(/* out */ std::vector<unsigned char>& out_data, + const unsigned char* const* images, + int compression_type, + int /*line_order*/, + int width, // for tiled : tile.width + int /*height*/, // for tiled : header.tile_size_y + int x_stride, // for tiled : header.tile_size_x + int line_no, // for tiled : 0 + int num_lines, // for tiled : tile.height + size_t pixel_data_size, + const std::vector<ChannelInfo>& channels, + const std::vector<size_t>& channel_offset_list, + const void* compression_param = 0) // zfp compression param +{ + size_t buf_size = static_cast<size_t>(width) * + static_cast<size_t>(num_lines) * + static_cast<size_t>(pixel_data_size); + //int last2bit = (buf_size & 3); + // buf_size must be multiple of four + //if(last2bit) buf_size += 4 - last2bit; + std::vector<unsigned char> buf(buf_size); -#if !TINYEXR_USE_ZFP - if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { - tinyexr::SetErrorMessage("ZFP compression is not supported in this build", - err); - return 0; - } -#endif + size_t start_y = static_cast<size_t>(line_no); + for (size_t c = 0; c < channels.size(); c++) { + if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) { + if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_FLOAT) { + for (int y = 0; y < num_lines; y++) { + // Assume increasing Y + float *line_ptr = reinterpret_cast<float *>(&buf.at( + static_cast<size_t>(pixel_data_size * y * width) + + channel_offset_list[c] * + static_cast<size_t>(width))); + for (int x = 0; x < width; x++) { + tinyexr::FP16 h16; + h16.u = reinterpret_cast<const unsigned short * const *>( + images)[c][(y + start_y) * x_stride + x]; -#if TINYEXR_USE_ZFP - for (size_t i = 0; i < static_cast<size_t>(exr_header->num_channels); i++) { - if (exr_header->requested_pixel_types[i] != TINYEXR_PIXELTYPE_FLOAT) { - tinyexr::SetErrorMessage("Pixel type must be FLOAT for ZFP compression", - err); - return 0; + tinyexr::FP32 f32 = half_to_float(h16); + + tinyexr::swap4(&f32.f); + + // line_ptr[x] = f32.f; + tinyexr::cpy4(line_ptr + x, &(f32.f)); + } + } + } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) { + for (int y = 0; y < num_lines; y++) { + // Assume increasing Y + unsigned short *line_ptr = reinterpret_cast<unsigned short *>( + &buf.at(static_cast<size_t>(pixel_data_size * y * + width) + + channel_offset_list[c] * + static_cast<size_t>(width))); + for (int x = 0; x < width; x++) { + unsigned short val = reinterpret_cast<const unsigned short * const *>( + images)[c][(y + start_y) * x_stride + x]; + + tinyexr::swap2(&val); + + // line_ptr[x] = val; + tinyexr::cpy2(line_ptr + x, &val); + } + } + } else { + assert(0); + } + + } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { + if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) { + for (int y = 0; y < num_lines; y++) { + // Assume increasing Y + unsigned short *line_ptr = reinterpret_cast<unsigned short *>( + &buf.at(static_cast<size_t>(pixel_data_size * y * + width) + + channel_offset_list[c] * + static_cast<size_t>(width))); + for (int x = 0; x < width; x++) { + tinyexr::FP32 f32; + f32.f = reinterpret_cast<const float * const *>( + images)[c][(y + start_y) * x_stride + x]; + + tinyexr::FP16 h16; + h16 = float_to_half_full(f32); + + tinyexr::swap2(reinterpret_cast<unsigned short *>(&h16.u)); + + // line_ptr[x] = h16.u; + tinyexr::cpy2(line_ptr + x, &(h16.u)); + } + } + } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_FLOAT) { + for (int y = 0; y < num_lines; y++) { + // Assume increasing Y + float *line_ptr = reinterpret_cast<float *>(&buf.at( + static_cast<size_t>(pixel_data_size * y * width) + + channel_offset_list[c] * + static_cast<size_t>(width))); + for (int x = 0; x < width; x++) { + float val = reinterpret_cast<const float * const *>( + images)[c][(y + start_y) * x_stride + x]; + + tinyexr::swap4(&val); + + // line_ptr[x] = val; + tinyexr::cpy4(line_ptr + x, &val); + } + } + } else { + assert(0); + } + } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) { + for (int y = 0; y < num_lines; y++) { + // Assume increasing Y + unsigned int *line_ptr = reinterpret_cast<unsigned int *>(&buf.at( + static_cast<size_t>(pixel_data_size * y * width) + + channel_offset_list[c] * static_cast<size_t>(width))); + for (int x = 0; x < width; x++) { + unsigned int val = reinterpret_cast<const unsigned int * const *>( + images)[c][(y + start_y) * x_stride + x]; + + tinyexr::swap4(&val); + + // line_ptr[x] = val; + tinyexr::cpy4(line_ptr + x, &val); + } + } } } + + if (compression_type == TINYEXR_COMPRESSIONTYPE_NONE) { + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(uncompressed) + out_data.insert(out_data.end(), buf.begin(), buf.end()); + + } else if ((compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) || + (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP)) { +#if TINYEXR_USE_MINIZ + std::vector<unsigned char> block(mz_compressBound( + static_cast<unsigned long>(buf.size()))); +#else + std::vector<unsigned char> block( + compressBound(static_cast<uLong>(buf.size()))); #endif + tinyexr::tinyexr_uint64 outSize = block.size(); - std::vector<unsigned char> memory; + tinyexr::CompressZip(&block.at(0), outSize, + reinterpret_cast<const unsigned char *>(&buf.at(0)), + static_cast<unsigned long>(buf.size())); - // Header - { - const char header[] = {0x76, 0x2f, 0x31, 0x01}; - memory.insert(memory.end(), header, header + 4); - } + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(compressed) + unsigned int data_len = static_cast<unsigned int>(outSize); // truncate - // Version, scanline. - { - char marker[] = {2, 0, 0, 0}; - /* @todo - if (exr_header->tiled) { - marker[1] |= 0x2; - } - if (exr_header->long_name) { - marker[1] |= 0x4; - } - if (exr_header->non_image) { - marker[1] |= 0x8; - } - if (exr_header->multipart) { - marker[1] |= 0x10; - } - */ - memory.insert(memory.end(), marker, marker + 4); - } + out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); - int num_scanlines = 1; - if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) { - num_scanlines = 16; - } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { - num_scanlines = 32; - } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { - num_scanlines = 16; - } + } else if (compression_type == TINYEXR_COMPRESSIONTYPE_RLE) { + // (buf.size() * 3) / 2 would be enough. + std::vector<unsigned char> block((buf.size() * 3) / 2); - // Write attributes. - std::vector<tinyexr::ChannelInfo> channels; - { - std::vector<unsigned char> data; + tinyexr::tinyexr_uint64 outSize = block.size(); - for (int c = 0; c < exr_header->num_channels; c++) { - tinyexr::ChannelInfo info; - info.p_linear = 0; - info.pixel_type = exr_header->requested_pixel_types[c]; - info.x_sampling = 1; - info.y_sampling = 1; - info.name = std::string(exr_header->channels[c].name); - channels.push_back(info); - } + tinyexr::CompressRle(&block.at(0), outSize, + reinterpret_cast<const unsigned char *>(&buf.at(0)), + static_cast<unsigned long>(buf.size())); - tinyexr::WriteChannelInfo(data, channels); + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(compressed) + unsigned int data_len = static_cast<unsigned int>(outSize); // truncate + out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); - tinyexr::WriteAttributeToMemory(&memory, "channels", "chlist", &data.at(0), - static_cast<int>(data.size())); - } + } else if (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { +#if TINYEXR_USE_PIZ + unsigned int bufLen = + 8192 + static_cast<unsigned int>( + 2 * static_cast<unsigned int>( + buf.size())); // @fixme { compute good bound. } + std::vector<unsigned char> block(bufLen); + unsigned int outSize = static_cast<unsigned int>(block.size()); + + CompressPiz(&block.at(0), &outSize, + reinterpret_cast<const unsigned char *>(&buf.at(0)), + buf.size(), channels, width, num_lines); + + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(compressed) + unsigned int data_len = outSize; + out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); - { - int comp = exr_header->compression_type; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&comp)); - tinyexr::WriteAttributeToMemory( - &memory, "compression", "compression", - reinterpret_cast<const unsigned char *>(&comp), 1); - } +#else + assert(0); +#endif + } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { +#if TINYEXR_USE_ZFP + const ZFPCompressionParam* zfp_compression_param = reinterpret_cast<const ZFPCompressionParam*>(compression_param); + std::vector<unsigned char> block; + unsigned int outSize; - { - int data[4] = {0, 0, exr_image->width - 1, exr_image->height - 1}; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data[0])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data[1])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data[2])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&data[3])); - tinyexr::WriteAttributeToMemory( - &memory, "dataWindow", "box2i", - reinterpret_cast<const unsigned char *>(data), sizeof(int) * 4); - tinyexr::WriteAttributeToMemory( - &memory, "displayWindow", "box2i", - reinterpret_cast<const unsigned char *>(data), sizeof(int) * 4); - } + tinyexr::CompressZfp( + &block, &outSize, reinterpret_cast<const float *>(&buf.at(0)), + width, num_lines, static_cast<int>(channels.size()), *zfp_compression_param); - { - unsigned char line_order = 0; // @fixme { read line_order from EXRHeader } - tinyexr::WriteAttributeToMemory(&memory, "lineOrder", "lineOrder", - &line_order, 1); - } + // 4 byte: scan line + // 4 byte: data size + // ~ : pixel data(compressed) + unsigned int data_len = outSize; + out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); - { - float aspectRatio = 1.0f; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&aspectRatio)); - tinyexr::WriteAttributeToMemory( - &memory, "pixelAspectRatio", "float", - reinterpret_cast<const unsigned char *>(&aspectRatio), sizeof(float)); +#else + (void)compression_param; + assert(0); +#endif + } else { + assert(0); + return false; } - { - float center[2] = {0.0f, 0.0f}; - tinyexr::swap4(reinterpret_cast<unsigned int *>(¢er[0])); - tinyexr::swap4(reinterpret_cast<unsigned int *>(¢er[1])); - tinyexr::WriteAttributeToMemory( - &memory, "screenWindowCenter", "v2f", - reinterpret_cast<const unsigned char *>(center), 2 * sizeof(float)); + return true; +} + +static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_header, + const std::vector<tinyexr::ChannelInfo>& channels, + std::vector<std::vector<unsigned char> >& data_list, + size_t start_index, // for data_list + int num_x_tiles, int num_y_tiles, + const std::vector<size_t>& channel_offset_list, + int pixel_data_size, + const void* compression_param, // must be set if zfp compression is enabled + std::string* err) { + int num_tiles = num_x_tiles * num_y_tiles; + assert(num_tiles == level_image->num_tiles); + + if ((exr_header->tile_size_x > level_image->width || exr_header->tile_size_y > level_image->height) && + level_image->level_x == 0 && level_image->level_y == 0) { + if (err) { + (*err) += "Failed to encode tile data.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; } - { - float w = static_cast<float>(exr_image->width); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&w)); - tinyexr::WriteAttributeToMemory(&memory, "screenWindowWidth", "float", - reinterpret_cast<const unsigned char *>(&w), - sizeof(float)); + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::atomic<bool> invalid_data(false); +#else + bool invalid_data(false); +#endif + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::vector<std::thread> workers; + std::atomic<int> tile_count(0); + + int num_threads = std::max(1, int(std::thread::hardware_concurrency())); + if (num_threads > int(num_tiles)) { + num_threads = int(num_tiles); } - // Custom attributes - if (exr_header->num_custom_attributes > 0) { - for (int i = 0; i < exr_header->num_custom_attributes; i++) { - tinyexr::WriteAttributeToMemory( - &memory, exr_header->custom_attributes[i].name, - exr_header->custom_attributes[i].type, - reinterpret_cast<const unsigned char *>( - exr_header->custom_attributes[i].value), - exr_header->custom_attributes[i].size); + for (int t = 0; t < num_threads; t++) { + workers.emplace_back(std::thread([&]() { + int i = 0; + while ((i = tile_count++) < num_tiles) { + +#else + // Use signed int since some OpenMP compiler doesn't allow unsigned type for + // `parallel for` +#if TINYEXR_USE_OPENMP +#pragma omp parallel for +#endif + for (int i = 0; i < num_tiles; i++) { + +#endif + size_t tile_idx = static_cast<size_t>(i); + size_t data_idx = tile_idx + start_index; + + int x_tile = i % num_x_tiles; + int y_tile = i / num_x_tiles; + + EXRTile& tile = level_image->tiles[tile_idx]; + + const unsigned char* const* images = + static_cast<const unsigned char* const*>(tile.images); + + data_list[data_idx].resize(5*sizeof(int)); + size_t data_header_size = data_list[data_idx].size(); + bool ret = EncodePixelData(data_list[data_idx], + images, + exr_header->compression_type, + 0, // increasing y + tile.width, + exr_header->tile_size_y, + exr_header->tile_size_x, + 0, + tile.height, + pixel_data_size, + channels, + channel_offset_list, + compression_param); + if (!ret) { + invalid_data = true; + continue; } + assert(data_list[data_idx].size() > data_header_size); + int data_len = static_cast<int>(data_list[data_idx].size() - data_header_size); + //tileX, tileY, levelX, levelY // pixel_data_size(int) + memcpy(&data_list[data_idx][0], &x_tile, sizeof(int)); + memcpy(&data_list[data_idx][4], &y_tile, sizeof(int)); + memcpy(&data_list[data_idx][8], &level_image->level_x, sizeof(int)); + memcpy(&data_list[data_idx][12], &level_image->level_y, sizeof(int)); + memcpy(&data_list[data_idx][16], &data_len, sizeof(int)); + + swap4(reinterpret_cast<int*>(&data_list[data_idx][0])); + swap4(reinterpret_cast<int*>(&data_list[data_idx][4])); + swap4(reinterpret_cast<int*>(&data_list[data_idx][8])); + swap4(reinterpret_cast<int*>(&data_list[data_idx][12])); + swap4(reinterpret_cast<int*>(&data_list[data_idx][16])); + +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) } +})); + } + + for (auto &t : workers) { + t.join(); + } +#else + } // omp parallel +#endif - { // end of header - unsigned char e = 0; - memory.push_back(e); + if (invalid_data) { + if (err) { + (*err) += "Failed to encode tile data.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; } + return TINYEXR_SUCCESS; +} - int num_blocks = exr_image->height / num_scanlines; - if (num_blocks * num_scanlines < exr_image->height) { - num_blocks++; +static int NumScanlines(int compression_type) { + int num_scanlines = 1; + if (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) { + num_scanlines = 16; + } else if (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { + num_scanlines = 32; + } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { + num_scanlines = 16; } + return num_scanlines; +} - std::vector<tinyexr::tinyexr_uint64> offsets(static_cast<size_t>(num_blocks)); +static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, + const std::vector<ChannelInfo>& channels, + int num_blocks, + tinyexr_uint64 chunk_offset, // starting offset of current chunk + bool is_multipart, + OffsetData& offset_data, // output block offsets, must be initialized + std::vector<std::vector<unsigned char> >& data_list, // output + tinyexr_uint64& total_size, // output: ending offset of current chunk + std::string* err) { + int num_scanlines = NumScanlines(exr_header->compression_type); - size_t headerSize = memory.size(); - tinyexr::tinyexr_uint64 offset = - headerSize + - static_cast<size_t>(num_blocks) * - sizeof( - tinyexr::tinyexr_int64); // sizeof(header) + sizeof(offsetTable) + data_list.resize(num_blocks); - std::vector<std::vector<unsigned char> > data_list( - static_cast<size_t>(num_blocks)); std::vector<size_t> channel_offset_list( - static_cast<size_t>(exr_header->num_channels)); + static_cast<size_t>(exr_header->num_channels)); int pixel_data_size = 0; - size_t channel_offset = 0; - for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) { - channel_offset_list[c] = channel_offset; - if (exr_header->requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) { - pixel_data_size += sizeof(unsigned short); - channel_offset += sizeof(unsigned short); - } else if (exr_header->requested_pixel_types[c] == - TINYEXR_PIXELTYPE_FLOAT) { - pixel_data_size += sizeof(float); - channel_offset += sizeof(float); - } else if (exr_header->requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT) { - pixel_data_size += sizeof(unsigned int); - channel_offset += sizeof(unsigned int); - } else { - assert(0); + { + size_t channel_offset = 0; + for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) { + channel_offset_list[c] = channel_offset; + if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) { + pixel_data_size += sizeof(unsigned short); + channel_offset += sizeof(unsigned short); + } else if (channels[c].requested_pixel_type == + TINYEXR_PIXELTYPE_FLOAT) { + pixel_data_size += sizeof(float); + channel_offset += sizeof(float); + } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_UINT) { + pixel_data_size += sizeof(unsigned int); + channel_offset += sizeof(unsigned int); + } else { + assert(0); + } } } + const void* compression_param = 0; #if TINYEXR_USE_ZFP tinyexr::ZFPCompressionParam zfp_compression_param; // Use ZFP compression parameter from custom attributes(if such a parameter // exists) { + std::string e; bool ret = tinyexr::FindZFPCompressionParam( - &zfp_compression_param, exr_header->custom_attributes, - exr_header->num_custom_attributes); + &zfp_compression_param, exr_header->custom_attributes, + exr_header->num_custom_attributes, &e); if (!ret) { // Use predefined compression parameter. zfp_compression_param.type = 0; zfp_compression_param.rate = 2; } + compression_param = &zfp_compression_param; } #endif -// Use signed int since some OpenMP compiler doesn't allow unsigned type for -// `parallel for` -#ifdef _OPENMP -#pragma omp parallel for -#endif - for (int i = 0; i < num_blocks; i++) { - size_t ii = static_cast<size_t>(i); - int start_y = num_scanlines * i; - int endY = (std::min)(num_scanlines * (i + 1), exr_image->height); - int h = endY - start_y; + tinyexr_uint64 offset = chunk_offset; + tinyexr_uint64 doffset = is_multipart ? 4u : 0u; - std::vector<unsigned char> buf( - static_cast<size_t>(exr_image->width * h * pixel_data_size)); + if (exr_image->tiles) { + const EXRImage* level_image = exr_image; + size_t block_idx = 0; + tinyexr::tinyexr_uint64 block_data_size = 0; + int num_levels = (exr_header->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) ? + offset_data.num_x_levels : (offset_data.num_x_levels * offset_data.num_y_levels); + for (int level_index = 0; level_index < num_levels; ++level_index) { + if (!level_image) { + if (err) { + (*err) += "Invalid number of tiled levels for EncodeChunk\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } - for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) { - if (exr_header->pixel_types[c] == TINYEXR_PIXELTYPE_HALF) { - if (exr_header->requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT) { - for (int y = 0; y < h; y++) { - // Assume increasing Y - float *line_ptr = reinterpret_cast<float *>(&buf.at( - static_cast<size_t>(pixel_data_size * y * exr_image->width) + - channel_offset_list[c] * - static_cast<size_t>(exr_image->width))); - for (int x = 0; x < exr_image->width; x++) { - tinyexr::FP16 h16; - h16.u = reinterpret_cast<unsigned short **>( - exr_image->images)[c][(y + start_y) * exr_image->width + x]; - - tinyexr::FP32 f32 = half_to_float(h16); - - tinyexr::swap4(reinterpret_cast<unsigned int *>(&f32.f)); - - // line_ptr[x] = f32.f; - tinyexr::cpy4(line_ptr + x, &(f32.f)); - } - } - } else if (exr_header->requested_pixel_types[c] == - TINYEXR_PIXELTYPE_HALF) { - for (int y = 0; y < h; y++) { - // Assume increasing Y - unsigned short *line_ptr = reinterpret_cast<unsigned short *>( - &buf.at(static_cast<size_t>(pixel_data_size * y * - exr_image->width) + - channel_offset_list[c] * - static_cast<size_t>(exr_image->width))); - for (int x = 0; x < exr_image->width; x++) { - unsigned short val = reinterpret_cast<unsigned short **>( - exr_image->images)[c][(y + start_y) * exr_image->width + x]; - - tinyexr::swap2(&val); - - // line_ptr[x] = val; - tinyexr::cpy2(line_ptr + x, &val); - } - } - } else { - assert(0); + int level_index_from_image = LevelIndex(level_image->level_x, level_image->level_y, + exr_header->tile_level_mode, offset_data.num_x_levels); + if (level_index_from_image != level_index) { + if (err) { + (*err) += "Incorrect level ordering in tiled image\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } + int num_y_tiles = (int)offset_data.offsets[level_index].size(); + assert(num_y_tiles); + int num_x_tiles = (int)offset_data.offsets[level_index][0].size(); + assert(num_x_tiles); + + std::string e; + int ret = EncodeTiledLevel(level_image, + exr_header, + channels, + data_list, + block_idx, + num_x_tiles, + num_y_tiles, + channel_offset_list, + pixel_data_size, + compression_param, + &e); + if (ret != TINYEXR_SUCCESS) { + if (!e.empty() && err) { + (*err) += e; } + return ret; + } - } else if (exr_header->pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT) { - if (exr_header->requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) { - for (int y = 0; y < h; y++) { - // Assume increasing Y - unsigned short *line_ptr = reinterpret_cast<unsigned short *>( - &buf.at(static_cast<size_t>(pixel_data_size * y * - exr_image->width) + - channel_offset_list[c] * - static_cast<size_t>(exr_image->width))); - for (int x = 0; x < exr_image->width; x++) { - tinyexr::FP32 f32; - f32.f = reinterpret_cast<float **>( - exr_image->images)[c][(y + start_y) * exr_image->width + x]; - - tinyexr::FP16 h16; - h16 = float_to_half_full(f32); - - tinyexr::swap2(reinterpret_cast<unsigned short *>(&h16.u)); - - // line_ptr[x] = h16.u; - tinyexr::cpy2(line_ptr + x, &(h16.u)); - } - } - } else if (exr_header->requested_pixel_types[c] == - TINYEXR_PIXELTYPE_FLOAT) { - for (int y = 0; y < h; y++) { - // Assume increasing Y - float *line_ptr = reinterpret_cast<float *>(&buf.at( - static_cast<size_t>(pixel_data_size * y * exr_image->width) + - channel_offset_list[c] * - static_cast<size_t>(exr_image->width))); - for (int x = 0; x < exr_image->width; x++) { - float val = reinterpret_cast<float **>( - exr_image->images)[c][(y + start_y) * exr_image->width + x]; - - tinyexr::swap4(reinterpret_cast<unsigned int *>(&val)); - - // line_ptr[x] = val; - tinyexr::cpy4(line_ptr + x, &val); - } - } - } else { - assert(0); + for (size_t j = 0; j < static_cast<size_t>(num_y_tiles); ++j) + for (size_t i = 0; i < static_cast<size_t>(num_x_tiles); ++i) { + offset_data.offsets[level_index][j][i] = offset; + swap8(reinterpret_cast<tinyexr_uint64*>(&offset_data.offsets[level_index][j][i])); + offset += data_list[block_idx].size() + doffset; + block_data_size += data_list[block_idx].size(); + ++block_idx; } - } else if (exr_header->pixel_types[c] == TINYEXR_PIXELTYPE_UINT) { - for (int y = 0; y < h; y++) { - // Assume increasing Y - unsigned int *line_ptr = reinterpret_cast<unsigned int *>(&buf.at( - static_cast<size_t>(pixel_data_size * y * exr_image->width) + - channel_offset_list[c] * static_cast<size_t>(exr_image->width))); - for (int x = 0; x < exr_image->width; x++) { - unsigned int val = reinterpret_cast<unsigned int **>( - exr_image->images)[c][(y + start_y) * exr_image->width + x]; + level_image = level_image->next_level; + } + assert(static_cast<int>(block_idx) == num_blocks); + total_size = offset; + } else { // scanlines + std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0]; - tinyexr::swap4(&val); +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + std::atomic<bool> invalid_data(false); + std::vector<std::thread> workers; + std::atomic<int> block_count(0); - // line_ptr[x] = val; - tinyexr::cpy4(line_ptr + x, &val); - } - } + int num_threads = std::min(std::max(1, int(std::thread::hardware_concurrency())), num_blocks); + + for (int t = 0; t < num_threads; t++) { + workers.emplace_back(std::thread([&]() { + int i = 0; + while ((i = block_count++) < num_blocks) { + +#else + bool invalid_data(false); +#if TINYEXR_USE_OPENMP +#pragma omp parallel for +#endif + for (int i = 0; i < num_blocks; i++) { + +#endif + int start_y = num_scanlines * i; + int end_Y = (std::min)(num_scanlines * (i + 1), exr_image->height); + int num_lines = end_Y - start_y; + + const unsigned char* const* images = + static_cast<const unsigned char* const*>(exr_image->images); + + data_list[i].resize(2*sizeof(int)); + size_t data_header_size = data_list[i].size(); + + bool ret = EncodePixelData(data_list[i], + images, + exr_header->compression_type, + 0, // increasing y + exr_image->width, + exr_image->height, + exr_image->width, + start_y, + num_lines, + pixel_data_size, + channels, + channel_offset_list, + compression_param); + if (!ret) { + invalid_data = true; + continue; // "break" cannot be used with OpenMP } + assert(data_list[i].size() > data_header_size); + int data_len = static_cast<int>(data_list[i].size() - data_header_size); + memcpy(&data_list[i][0], &start_y, sizeof(int)); + memcpy(&data_list[i][4], &data_len, sizeof(int)); + + swap4(reinterpret_cast<int*>(&data_list[i][0])); + swap4(reinterpret_cast<int*>(&data_list[i][4])); +#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) + } + })); } - if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_NONE) { - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(uncompressed) - std::vector<unsigned char> header(8); - unsigned int data_len = static_cast<unsigned int>(buf.size()); - memcpy(&header.at(0), &start_y, sizeof(int)); - memcpy(&header.at(4), &data_len, sizeof(unsigned int)); + for (auto &t : workers) { + t.join(); + } +#else + } // omp parallel +#endif - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(0))); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(4))); + if (invalid_data) { + if (err) { + (*err) += "Failed to encode scanline data.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } - data_list[ii].insert(data_list[ii].end(), header.begin(), header.end()); - data_list[ii].insert(data_list[ii].end(), buf.begin(), - buf.begin() + data_len); + for (size_t i = 0; i < static_cast<size_t>(num_blocks); i++) { + offsets[i] = offset; + tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64 *>(&offsets[i])); + offset += data_list[i].size() + doffset; + } - } else if ((exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) || - (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZIP)) { -#if TINYEXR_USE_MINIZ - std::vector<unsigned char> block(tinyexr::miniz::mz_compressBound( - static_cast<unsigned long>(buf.size()))); + total_size = static_cast<size_t>(offset); + } + return TINYEXR_SUCCESS; +} + +// can save a single or multi-part image (no deep* formats) +static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, + const EXRHeader** exr_headers, + unsigned int num_parts, + unsigned char** memory_out, const char** err) { + if (exr_images == NULL || exr_headers == NULL || num_parts == 0 || + memory_out == NULL) { + SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory", + err); + return 0; + } + { + for (unsigned int i = 0; i < num_parts; ++i) { + if (exr_headers[i]->compression_type < 0) { + SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory", + err); + return 0; + } +#if !TINYEXR_USE_PIZ + if (exr_headers[i]->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { + SetErrorMessage("PIZ compression is not supported in this build", + err); + return 0; + } +#endif +#if !TINYEXR_USE_ZFP + if (exr_headers[i]->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { + SetErrorMessage("ZFP compression is not supported in this build", + err); + return 0; + } #else - std::vector<unsigned char> block( - compressBound(static_cast<uLong>(buf.size()))); + for (int c = 0; c < exr_header->num_channels; ++c) { + if (exr_headers[i]->requested_pixel_types[c] != TINYEXR_PIXELTYPE_FLOAT) { + SetErrorMessage("Pixel type must be FLOAT for ZFP compression", + err); + return 0; + } + } #endif - tinyexr::tinyexr_uint64 outSize = block.size(); - - tinyexr::CompressZip(&block.at(0), outSize, - reinterpret_cast<const unsigned char *>(&buf.at(0)), - static_cast<unsigned long>(buf.size())); + } + } - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(compressed) - std::vector<unsigned char> header(8); - unsigned int data_len = static_cast<unsigned int>(outSize); // truncate - memcpy(&header.at(0), &start_y, sizeof(int)); - memcpy(&header.at(4), &data_len, sizeof(unsigned int)); + std::vector<unsigned char> memory; - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(0))); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(4))); + // Header + { + const char header[] = { 0x76, 0x2f, 0x31, 0x01 }; + memory.insert(memory.end(), header, header + 4); + } - data_list[ii].insert(data_list[ii].end(), header.begin(), header.end()); - data_list[ii].insert(data_list[ii].end(), block.begin(), - block.begin() + data_len); + // Version + // using value from the first header + int long_name = exr_headers[0]->long_name; + { + char marker[] = { 2, 0, 0, 0 }; + /* @todo + if (exr_header->non_image) { + marker[1] |= 0x8; + } + */ + // tiled + if (num_parts == 1 && exr_images[0].tiles) { + marker[1] |= 0x2; + } + // long_name + if (long_name) { + marker[1] |= 0x4; + } + // multipart + if (num_parts > 1) { + marker[1] |= 0x10; + } + memory.insert(memory.end(), marker, marker + 4); + } - } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_RLE) { - // (buf.size() * 3) / 2 would be enough. - std::vector<unsigned char> block((buf.size() * 3) / 2); + int total_chunk_count = 0; + std::vector<int> chunk_count(num_parts); + std::vector<OffsetData> offset_data(num_parts); + for (unsigned int i = 0; i < num_parts; ++i) { + if (!exr_images[i].tiles) { + int num_scanlines = NumScanlines(exr_headers[i]->compression_type); + chunk_count[i] = + (exr_images[i].height + num_scanlines - 1) / num_scanlines; + InitSingleResolutionOffsets(offset_data[i], chunk_count[i]); + total_chunk_count += chunk_count[i]; + } else { + { + std::vector<int> num_x_tiles, num_y_tiles; + PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i]); + chunk_count[i] = + InitTileOffsets(offset_data[i], exr_headers[i], num_x_tiles, num_y_tiles); + total_chunk_count += chunk_count[i]; + } + } + } + // Write attributes to memory buffer. + std::vector< std::vector<tinyexr::ChannelInfo> > channels(num_parts); + { + std::set<std::string> partnames; + for (unsigned int i = 0; i < num_parts; ++i) { + //channels + { + std::vector<unsigned char> data; + + for (int c = 0; c < exr_headers[i]->num_channels; c++) { + tinyexr::ChannelInfo info; + info.p_linear = 0; + info.pixel_type = exr_headers[i]->pixel_types[c]; + info.requested_pixel_type = exr_headers[i]->requested_pixel_types[c]; + info.x_sampling = 1; + info.y_sampling = 1; + info.name = std::string(exr_headers[i]->channels[c].name); + channels[i].push_back(info); + } - tinyexr::tinyexr_uint64 outSize = block.size(); + tinyexr::WriteChannelInfo(data, channels[i]); - tinyexr::CompressRle(&block.at(0), outSize, - reinterpret_cast<const unsigned char *>(&buf.at(0)), - static_cast<unsigned long>(buf.size())); + tinyexr::WriteAttributeToMemory(&memory, "channels", "chlist", &data.at(0), + static_cast<int>(data.size())); + } - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(compressed) - std::vector<unsigned char> header(8); - unsigned int data_len = static_cast<unsigned int>(outSize); // truncate - memcpy(&header.at(0), &start_y, sizeof(int)); - memcpy(&header.at(4), &data_len, sizeof(unsigned int)); + { + int comp = exr_headers[i]->compression_type; + swap4(&comp); + WriteAttributeToMemory( + &memory, "compression", "compression", + reinterpret_cast<const unsigned char*>(&comp), 1); + } - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(0))); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(4))); + { + int data[4] = { 0, 0, exr_images[i].width - 1, exr_images[i].height - 1 }; + swap4(&data[0]); + swap4(&data[1]); + swap4(&data[2]); + swap4(&data[3]); + WriteAttributeToMemory( + &memory, "dataWindow", "box2i", + reinterpret_cast<const unsigned char*>(data), sizeof(int) * 4); + + int data0[4] = { 0, 0, exr_images[0].width - 1, exr_images[0].height - 1 }; + swap4(&data0[0]); + swap4(&data0[1]); + swap4(&data0[2]); + swap4(&data0[3]); + // Note: must be the same across parts (currently, using value from the first header) + WriteAttributeToMemory( + &memory, "displayWindow", "box2i", + reinterpret_cast<const unsigned char*>(data0), sizeof(int) * 4); + } - data_list[ii].insert(data_list[ii].end(), header.begin(), header.end()); - data_list[ii].insert(data_list[ii].end(), block.begin(), - block.begin() + data_len); + { + unsigned char line_order = 0; // @fixme { read line_order from EXRHeader } + WriteAttributeToMemory(&memory, "lineOrder", "lineOrder", + &line_order, 1); + } - } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { -#if TINYEXR_USE_PIZ - unsigned int bufLen = - 8192 + static_cast<unsigned int>( - 2 * static_cast<unsigned int>( - buf.size())); // @fixme { compute good bound. } - std::vector<unsigned char> block(bufLen); - unsigned int outSize = static_cast<unsigned int>(block.size()); - - CompressPiz(&block.at(0), &outSize, - reinterpret_cast<const unsigned char *>(&buf.at(0)), - buf.size(), channels, exr_image->width, h); - - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(compressed) - std::vector<unsigned char> header(8); - unsigned int data_len = outSize; - memcpy(&header.at(0), &start_y, sizeof(int)); - memcpy(&header.at(4), &data_len, sizeof(unsigned int)); - - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(0))); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(4))); - - data_list[ii].insert(data_list[ii].end(), header.begin(), header.end()); - data_list[ii].insert(data_list[ii].end(), block.begin(), - block.begin() + data_len); + { + // Note: must be the same across parts + float aspectRatio = 1.0f; + swap4(&aspectRatio); + WriteAttributeToMemory( + &memory, "pixelAspectRatio", "float", + reinterpret_cast<const unsigned char*>(&aspectRatio), sizeof(float)); + } -#else - assert(0); -#endif - } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { -#if TINYEXR_USE_ZFP - std::vector<unsigned char> block; - unsigned int outSize; + { + float center[2] = { 0.0f, 0.0f }; + swap4(¢er[0]); + swap4(¢er[1]); + WriteAttributeToMemory( + &memory, "screenWindowCenter", "v2f", + reinterpret_cast<const unsigned char*>(center), 2 * sizeof(float)); + } - tinyexr::CompressZfp( - &block, &outSize, reinterpret_cast<const float *>(&buf.at(0)), - exr_image->width, h, exr_header->num_channels, zfp_compression_param); + { + float w = 1.0f; + swap4(&w); + WriteAttributeToMemory(&memory, "screenWindowWidth", "float", + reinterpret_cast<const unsigned char*>(&w), + sizeof(float)); + } - // 4 byte: scan line - // 4 byte: data size - // ~ : pixel data(compressed) - std::vector<unsigned char> header(8); - unsigned int data_len = outSize; - memcpy(&header.at(0), &start_y, sizeof(int)); - memcpy(&header.at(4), &data_len, sizeof(unsigned int)); + if (exr_images[i].tiles) { + unsigned char tile_mode = static_cast<unsigned char>(exr_headers[i]->tile_level_mode & 0x3); + if (exr_headers[i]->tile_rounding_mode) tile_mode |= (1u << 4u); + //unsigned char data[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + unsigned int datai[3] = { 0, 0, 0 }; + unsigned char* data = reinterpret_cast<unsigned char*>(&datai[0]); + datai[0] = static_cast<unsigned int>(exr_headers[i]->tile_size_x); + datai[1] = static_cast<unsigned int>(exr_headers[i]->tile_size_y); + data[8] = tile_mode; + swap4(reinterpret_cast<unsigned int*>(&data[0])); + swap4(reinterpret_cast<unsigned int*>(&data[4])); + WriteAttributeToMemory( + &memory, "tiles", "tiledesc", + reinterpret_cast<const unsigned char*>(data), 9); + } - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(0))); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&header.at(4))); + // must be present for multi-part files - according to spec. + if (num_parts > 1) { + // name + { + size_t len = 0; + if ((len = strlen(exr_headers[i]->name)) > 0) { + partnames.insert(std::string(exr_headers[i]->name)); + if (partnames.size() != i + 1) { + SetErrorMessage("'name' attributes must be unique for a multi-part file", err); + return 0; + } + WriteAttributeToMemory( + &memory, "name", "string", + reinterpret_cast<const unsigned char*>(exr_headers[i]->name), + static_cast<int>(len)); + } else { + SetErrorMessage("Invalid 'name' attribute for a multi-part file", err); + return 0; + } + } + // type + { + const char* type = "scanlineimage"; + if (exr_images[i].tiles) type = "tiledimage"; + WriteAttributeToMemory( + &memory, "type", "string", + reinterpret_cast<const unsigned char*>(type), + static_cast<int>(strlen(type))); + } + // chunkCount + { + WriteAttributeToMemory( + &memory, "chunkCount", "int", + reinterpret_cast<const unsigned char*>(&chunk_count[i]), + 4); + } + } - data_list[ii].insert(data_list[ii].end(), header.begin(), header.end()); - data_list[ii].insert(data_list[ii].end(), block.begin(), - block.begin() + data_len); + // Custom attributes + if (exr_headers[i]->num_custom_attributes > 0) { + for (int j = 0; j < exr_headers[i]->num_custom_attributes; j++) { + tinyexr::WriteAttributeToMemory( + &memory, exr_headers[i]->custom_attributes[j].name, + exr_headers[i]->custom_attributes[j].type, + reinterpret_cast<const unsigned char*>( + exr_headers[i]->custom_attributes[j].value), + exr_headers[i]->custom_attributes[j].size); + } + } -#else - assert(0); -#endif - } else { - assert(0); + { // end of header + memory.push_back(0); + } } - } // omp parallel - - for (size_t i = 0; i < static_cast<size_t>(num_blocks); i++) { - offsets[i] = offset; - tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64 *>(&offsets[i])); - offset += data_list[i].size(); + } + if (num_parts > 1) { + // end of header list + memory.push_back(0); } - size_t totalSize = static_cast<size_t>(offset); - { - memory.insert( - memory.end(), reinterpret_cast<unsigned char *>(&offsets.at(0)), - reinterpret_cast<unsigned char *>(&offsets.at(0)) + - sizeof(tinyexr::tinyexr_uint64) * static_cast<size_t>(num_blocks)); + tinyexr_uint64 chunk_offset = memory.size() + size_t(total_chunk_count) * sizeof(tinyexr_uint64); + + tinyexr_uint64 total_size = 0; + std::vector< std::vector< std::vector<unsigned char> > > data_lists(num_parts); + for (unsigned int i = 0; i < num_parts; ++i) { + std::string e; + int ret = EncodeChunk(&exr_images[i], exr_headers[i], + channels[i], + chunk_count[i], + // starting offset of current chunk after part-number + chunk_offset, + num_parts > 1, + offset_data[i], // output: block offsets, must be initialized + data_lists[i], // output + total_size, // output + &e); + if (ret != TINYEXR_SUCCESS) { + if (!e.empty()) { + tinyexr::SetErrorMessage(e, err); + } + return 0; + } + chunk_offset = total_size; } - if ( memory.size() == 0 ) { + // Allocating required memory + if (total_size == 0) { // something went wrong tinyexr::SetErrorMessage("Output memory size is zero", err); return 0; } - - (*memory_out) = static_cast<unsigned char *>(malloc(totalSize)); - memcpy((*memory_out), &memory.at(0), memory.size()); - unsigned char *memory_ptr = *memory_out + memory.size(); - - for (size_t i = 0; i < static_cast<size_t>(num_blocks); i++) { - memcpy(memory_ptr, &data_list[i].at(0), data_list[i].size()); - memory_ptr += data_list[i].size(); + (*memory_out) = static_cast<unsigned char*>(malloc(total_size)); + + // Writing header + memcpy((*memory_out), &memory[0], memory.size()); + unsigned char* memory_ptr = *memory_out + memory.size(); + size_t sum = memory.size(); + + // Writing offset data for chunks + for (unsigned int i = 0; i < num_parts; ++i) { + if (exr_images[i].tiles) { + const EXRImage* level_image = &exr_images[i]; + int num_levels = (exr_headers[i]->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) ? + offset_data[i].num_x_levels : (offset_data[i].num_x_levels * offset_data[i].num_y_levels); + for (int level_index = 0; level_index < num_levels; ++level_index) { + for (size_t j = 0; j < offset_data[i].offsets[level_index].size(); ++j) { + size_t num_bytes = sizeof(tinyexr_uint64) * offset_data[i].offsets[level_index][j].size(); + sum += num_bytes; + assert(sum <= total_size); + memcpy(memory_ptr, + reinterpret_cast<unsigned char*>(&offset_data[i].offsets[level_index][j][0]), + num_bytes); + memory_ptr += num_bytes; + } + level_image = level_image->next_level; + } + } else { + size_t num_bytes = sizeof(tinyexr::tinyexr_uint64) * static_cast<size_t>(chunk_count[i]); + sum += num_bytes; + assert(sum <= total_size); + std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data[i].offsets[0][0]; + memcpy(memory_ptr, reinterpret_cast<unsigned char*>(&offsets[0]), num_bytes); + memory_ptr += num_bytes; + } + } + + // Writing chunk data + for (unsigned int i = 0; i < num_parts; ++i) { + for (size_t j = 0; j < static_cast<size_t>(chunk_count[i]); ++j) { + if (num_parts > 1) { + sum += 4; + assert(sum <= total_size); + unsigned int part_number = i; + swap4(&part_number); + memcpy(memory_ptr, &part_number, 4); + memory_ptr += 4; + } + sum += data_lists[i][j].size(); + assert(sum <= total_size); + memcpy(memory_ptr, &data_lists[i][j][0], data_lists[i][j].size()); + memory_ptr += data_lists[i][j].size(); + } } + assert(sum == total_size); + return total_size; // OK +} + +} // tinyexr - return totalSize; // OK +size_t SaveEXRImageToMemory(const EXRImage* exr_image, + const EXRHeader* exr_header, + unsigned char** memory_out, const char** err) { + return tinyexr::SaveEXRNPartImageToMemory(exr_image, &exr_header, 1, memory_out, err); } int SaveEXRImageToFile(const EXRImage *exr_image, const EXRHeader *exr_header, @@ -12039,14 +7291,26 @@ int SaveEXRImageToFile(const EXRImage *exr_image, const EXRHeader *exr_header, } #endif -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "wb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"wb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename), + err); + return TINYEXR_ERROR_CANT_WRITE_FILE; + } +#else + // Unknown compiler + fp = fopen(filename, "wb"); +#endif #else - FILE *fp = fopen(filename, "wb"); + fp = fopen(filename, "wb"); #endif if (!fp) { - tinyexr::SetErrorMessage("Cannot write a file", err); + tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename), + err); return TINYEXR_ERROR_CANT_WRITE_FILE; } @@ -12072,16 +7336,96 @@ int SaveEXRImageToFile(const EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_SUCCESS; } +size_t SaveEXRMultipartImageToMemory(const EXRImage* exr_images, + const EXRHeader** exr_headers, + unsigned int num_parts, + unsigned char** memory_out, const char** err) { + if (exr_images == NULL || exr_headers == NULL || num_parts < 2 || + memory_out == NULL) { + tinyexr::SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory", + err); + return 0; + } + return tinyexr::SaveEXRNPartImageToMemory(exr_images, exr_headers, num_parts, memory_out, err); +} + +int SaveEXRMultipartImageToFile(const EXRImage* exr_images, + const EXRHeader** exr_headers, + unsigned int num_parts, + const char* filename, + const char** err) { + if (exr_images == NULL || exr_headers == NULL || num_parts < 2) { + tinyexr::SetErrorMessage("Invalid argument for SaveEXRMultipartImageToFile", + err); + return TINYEXR_ERROR_INVALID_ARGUMENT; + } + + FILE *fp = NULL; +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"wb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename), + err); + return TINYEXR_ERROR_CANT_WRITE_FILE; + } +#else + // Unknown compiler + fp = fopen(filename, "wb"); +#endif +#else + fp = fopen(filename, "wb"); +#endif + if (!fp) { + tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename), + err); + return TINYEXR_ERROR_CANT_WRITE_FILE; + } + + unsigned char *mem = NULL; + size_t mem_size = SaveEXRMultipartImageToMemory(exr_images, exr_headers, num_parts, &mem, err); + if (mem_size == 0) { + return TINYEXR_ERROR_SERIALZATION_FAILED; + } + + size_t written_size = 0; + if ((mem_size > 0) && mem) { + written_size = fwrite(mem, 1, mem_size, fp); + } + free(mem); + + fclose(fp); + + if (written_size != mem_size) { + tinyexr::SetErrorMessage("Cannot write a file", err); + return TINYEXR_ERROR_CANT_WRITE_FILE; + } + + return TINYEXR_SUCCESS; +} + int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { if (deep_image == NULL) { tinyexr::SetErrorMessage("Invalid argument for LoadDeepEXR", err); return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _MSC_VER +#ifdef _WIN32 FILE *fp = NULL; - errno_t errcode = fopen_s(&fp, filename, "rb"); - if ((0 != errcode) || (!fp)) { +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot read a file " + std::string(filename), + err); + return TINYEXR_ERROR_CANT_OPEN_FILE; + } +#else + // Unknown compiler + fp = fopen(filename, "rb"); +#endif + if (!fp) { tinyexr::SetErrorMessage("Cannot read a file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; @@ -12169,6 +7513,9 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { size_t marker_size; if (!tinyexr::ReadAttribute(&attr_name, &attr_type, &data, &marker_size, marker, size)) { + std::stringstream ss; + ss << "Failed to parse attribute\n"; + tinyexr::SetErrorMessage(ss.str(), err); return TINYEXR_ERROR_INVALID_DATA; } marker += marker_size; @@ -12212,10 +7559,10 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { memcpy(&dy, &data.at(4), sizeof(int)); memcpy(&dw, &data.at(8), sizeof(int)); memcpy(&dh, &data.at(12), sizeof(int)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&dx)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&dy)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&dw)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&dh)); + tinyexr::swap4(&dx); + tinyexr::swap4(&dy); + tinyexr::swap4(&dw); + tinyexr::swap4(&dh); } else if (attr_name.compare("displayWindow") == 0) { int x; @@ -12226,10 +7573,10 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { memcpy(&y, &data.at(4), sizeof(int)); memcpy(&w, &data.at(8), sizeof(int)); memcpy(&h, &data.at(12), sizeof(int)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&x)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&y)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&w)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&h)); + tinyexr::swap4(&x); + tinyexr::swap4(&y); + tinyexr::swap4(&w); + tinyexr::swap4(&h); } } @@ -12317,7 +7664,7 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { memcpy(&unpackedSampleDataSize, data_ptr + 20, sizeof(tinyexr::tinyexr_int64)); - tinyexr::swap4(reinterpret_cast<unsigned int *>(&line_no)); + tinyexr::swap4(&line_no); tinyexr::swap8( reinterpret_cast<tinyexr::tinyexr_uint64 *>(&packedOffsetTableSize)); tinyexr::swap8( @@ -12463,6 +7810,9 @@ void InitEXRImage(EXRImage *exr_image) { exr_image->images = NULL; exr_image->tiles = NULL; + exr_image->next_level = NULL; + exr_image->level_x = 0; + exr_image->level_y = 0; exr_image->num_tiles = 0; } @@ -12509,14 +7859,43 @@ int FreeEXRHeader(EXRHeader *exr_header) { free(exr_header->custom_attributes); } + EXRSetNameAttr(exr_header, NULL); + return TINYEXR_SUCCESS; } +void EXRSetNameAttr(EXRHeader* exr_header, const char* name) { + if (exr_header == NULL) { + return; + } + memset(exr_header->name, 0, 256); + if (name != NULL) { + size_t len = std::min(strlen(name), (size_t)255); + if (len) { + memcpy(exr_header->name, name, len); + } + } +} + +int EXRNumLevels(const EXRImage* exr_image) { + if (exr_image == NULL) return 0; + if(exr_image->images) return 1; // scanlines + int levels = 1; + const EXRImage* level_image = exr_image; + while((level_image = level_image->next_level)) ++levels; + return levels; +} + int FreeEXRImage(EXRImage *exr_image) { if (exr_image == NULL) { return TINYEXR_ERROR_INVALID_ARGUMENT; } + if (exr_image->next_level) { + FreeEXRImage(exr_image->next_level); + delete exr_image->next_level; + } + for (int i = 0; i < exr_image->num_channels; i++) { if (exr_image->images && exr_image->images[i]) { free(exr_image->images[i]); @@ -12552,11 +7931,21 @@ int ParseEXRHeaderFromFile(EXRHeader *exr_header, const EXRVersion *exr_version, return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "rb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); + return TINYEXR_ERROR_INVALID_FILE; + } #else - FILE *fp = fopen(filename, "rb"); + // Unknown compiler + fp = fopen(filename, "rb"); +#endif +#else + fp = fopen(filename, "rb"); #endif if (!fp) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); @@ -12648,11 +8037,11 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, static_cast<EXRHeader **>(malloc(sizeof(EXRHeader *) * infos.size())); for (size_t i = 0; i < infos.size(); i++) { EXRHeader *exr_header = static_cast<EXRHeader *>(malloc(sizeof(EXRHeader))); + memset(exr_header, 0, sizeof(EXRHeader)); ConvertHeader(exr_header, infos[i]); - // transfoer `tiled` from version. - exr_header->tiled = exr_version->tiled; + exr_header->multipart = exr_version->multipart ? 1 : 0; (*exr_headers)[i] = exr_header; } @@ -12672,11 +8061,21 @@ int ParseEXRMultipartHeaderFromFile(EXRHeader ***exr_headers, int *num_headers, return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "rb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); + return TINYEXR_ERROR_INVALID_FILE; + } #else - FILE *fp = fopen(filename, "rb"); + // Unknown compiler + fp = fopen(filename, "rb"); +#endif +#else + fp = fopen(filename, "rb"); #endif if (!fp) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); @@ -12768,11 +8167,20 @@ int ParseEXRVersionFromFile(EXRVersion *version, const char *filename) { return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "rb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t err = _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (err != 0) { + // TODO(syoyo): return wfopen_s erro code + return TINYEXR_ERROR_CANT_OPEN_FILE; + } #else - FILE *fp = fopen(filename, "rb"); + // Unknown compiler + fp = fopen(filename, "rb"); +#endif +#else + fp = fopen(filename, "rb"); #endif if (!fp) { return TINYEXR_ERROR_CANT_OPEN_FILE; @@ -12839,51 +8247,82 @@ int LoadEXRMultipartImageFromMemory(EXRImage *exr_images, // http://www.openexr.com/openexrfilelayout.pdf // Load chunk offset table. - std::vector<std::vector<tinyexr::tinyexr_uint64> > chunk_offset_table_list; + std::vector<tinyexr::OffsetData> chunk_offset_table_list; + chunk_offset_table_list.reserve(num_parts); for (size_t i = 0; i < static_cast<size_t>(num_parts); i++) { - std::vector<tinyexr::tinyexr_uint64> offset_table( - static_cast<size_t>(exr_headers[i]->chunk_count)); - - for (size_t c = 0; c < offset_table.size(); c++) { - tinyexr::tinyexr_uint64 offset; - memcpy(&offset, marker, 8); - tinyexr::swap8(&offset); + chunk_offset_table_list.resize(chunk_offset_table_list.size() + 1); + tinyexr::OffsetData& offset_data = chunk_offset_table_list.back(); + if (!exr_headers[i]->tiled || exr_headers[i]->tile_level_mode == TINYEXR_TILE_ONE_LEVEL) { + tinyexr::InitSingleResolutionOffsets(offset_data, exr_headers[i]->chunk_count); + std::vector<tinyexr::tinyexr_uint64>& offset_table = offset_data.offsets[0][0]; + + for (size_t c = 0; c < offset_table.size(); c++) { + tinyexr::tinyexr_uint64 offset; + memcpy(&offset, marker, 8); + tinyexr::swap8(&offset); + + if (offset >= size) { + tinyexr::SetErrorMessage("Invalid offset size in EXR header chunks.", + err); + return TINYEXR_ERROR_INVALID_DATA; + } - if (offset >= size) { - tinyexr::SetErrorMessage("Invalid offset size in EXR header chunks.", - err); - return TINYEXR_ERROR_INVALID_DATA; + offset_table[c] = offset + 4; // +4 to skip 'part number' + marker += 8; + } + } else { + { + std::vector<int> num_x_tiles, num_y_tiles; + tinyexr::PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i]); + int num_blocks = InitTileOffsets(offset_data, exr_headers[i], num_x_tiles, num_y_tiles); + if (num_blocks != exr_headers[i]->chunk_count) { + tinyexr::SetErrorMessage("Invalid offset table size.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + } + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) { + for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { + tinyexr::tinyexr_uint64 offset; + memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64)); + tinyexr::swap8(&offset); + if (offset >= size) { + tinyexr::SetErrorMessage("Invalid offset size in EXR header chunks.", + err); + return TINYEXR_ERROR_INVALID_DATA; + } + offset_data.offsets[l][dy][dx] = offset + 4; // +4 to skip 'part number' + marker += sizeof(tinyexr::tinyexr_uint64); // = 8 + } + } } - - offset_table[c] = offset + 4; // +4 to skip 'part number' - marker += 8; } - - chunk_offset_table_list.push_back(offset_table); } // Decode image. for (size_t i = 0; i < static_cast<size_t>(num_parts); i++) { - std::vector<tinyexr::tinyexr_uint64> &offset_table = - chunk_offset_table_list[i]; + tinyexr::OffsetData &offset_data = chunk_offset_table_list[i]; // First check 'part number' is identitical to 'i' - for (size_t c = 0; c < offset_table.size(); c++) { - const unsigned char *part_number_addr = - memory + offset_table[c] - 4; // -4 to move to 'part number' field. - unsigned int part_no; - memcpy(&part_no, part_number_addr, sizeof(unsigned int)); // 4 - tinyexr::swap4(&part_no); - - if (part_no != i) { - tinyexr::SetErrorMessage("Invalid `part number' in EXR header chunks.", - err); - return TINYEXR_ERROR_INVALID_DATA; - } - } + for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) + for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) + for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { + + const unsigned char *part_number_addr = + memory + offset_data.offsets[l][dy][dx] - 4; // -4 to move to 'part number' field. + unsigned int part_no; + memcpy(&part_no, part_number_addr, sizeof(unsigned int)); // 4 + tinyexr::swap4(&part_no); + + if (part_no != i) { + tinyexr::SetErrorMessage("Invalid `part number' in EXR header chunks.", + err); + return TINYEXR_ERROR_INVALID_DATA; + } + } std::string e; - int ret = tinyexr::DecodeChunk(&exr_images[i], exr_headers[i], offset_table, + int ret = tinyexr::DecodeChunk(&exr_images[i], exr_headers[i], offset_data, memory, size, &e); if (ret != TINYEXR_SUCCESS) { if (!e.empty()) { @@ -12906,11 +8345,21 @@ int LoadEXRMultipartImageFromFile(EXRImage *exr_images, return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 FILE *fp = NULL; - fopen_s(&fp, filename, "rb"); +#ifdef _WIN32 +#if defined(_MSC_VER) || defined(__MINGW32__) // MSVC, MinGW gcc or clang + errno_t errcode = + _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); + if (errcode != 0) { + tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); + return TINYEXR_ERROR_CANT_OPEN_FILE; + } #else - FILE *fp = fopen(filename, "rb"); + // Unknown compiler + fp = fopen(filename, "rb"); +#endif +#else + fp = fopen(filename, "rb"); #endif if (!fp) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); @@ -13080,5 +8529,5 @@ int SaveEXR(const float *data, int width, int height, int components, #pragma clang diagnostic pop #endif -#endif // TINYEXR_IMPLEMENTATION_DEIFNED +#endif // TINYEXR_IMPLEMENTATION_DEFINED #endif // TINYEXR_IMPLEMENTATION diff --git a/3rdparty/bimg/LICENSE b/3rdparty/bimg/LICENSE index 3df43e30a2a..e21e5d5f338 100644 --- a/3rdparty/bimg/LICENSE +++ b/3rdparty/bimg/LICENSE @@ -1,4 +1,4 @@ -Copyright 2010-2018 Branimir Karadzic. All rights reserved. +Copyright 2010-2022 Branimir Karadzic Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -10,13 +10,13 @@ are permitted provided that the following conditions are met: this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT -SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/3rdparty/bimg/README.md b/3rdparty/bimg/README.md index f918ab634d4..d24e8c1f715 100644 --- a/3rdparty/bimg/README.md +++ b/3rdparty/bimg/README.md @@ -3,10 +3,9 @@ bimg Image library. -[](https://travis-ci.org/bkaradzic/bimg) [](https://ci.appveyor.com/project/bkaradzic/bimg) [](https://bkaradzic.github.io/bgfx/license.html) -[](https://gitter.im/bkaradzic/bgfx) +[](https://discord.gg/9eMbv7J) Contact ------- @@ -23,27 +22,25 @@ https://github.com/bkaradzic/bimg <img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png"> </a> - Copyright 2010-2018 Branimir Karadzic. All rights reserved. + Copyright 2010-2022 Branimir Karadzic - https://github.com/bkaradzic/bimg + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/3rdparty/bimg/include/bimg/bimg.h b/3rdparty/bimg/include/bimg/bimg.h index 337bce8afd7..07a5db6d002 100644 --- a/3rdparty/bimg/include/bimg/bimg.h +++ b/3rdparty/bimg/include/bimg/bimg.h @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #ifndef BIMG_IMAGE_H_HEADER_GUARD @@ -69,11 +69,19 @@ namespace bimg ATCE, //!< ATCE RGBA 8 BPP explicit alpha ATCI, //!< ATCI RGBA 8 BPP interpolated alpha ASTC4x4, //!< ASTC 4x4 8.0 BPP + ASTC5x4, //!< ASTC 5x4 6.40 BPP ASTC5x5, //!< ASTC 5x5 5.12 BPP + ASTC6x5, //!< ASTC 6x5 4.27 BPP ASTC6x6, //!< ASTC 6x6 3.56 BPP ASTC8x5, //!< ASTC 8x5 3.20 BPP ASTC8x6, //!< ASTC 8x6 2.67 BPP + ASTC8x8, //!< ASTC 8x8 2.00 BPP ASTC10x5, //!< ASTC 10x5 2.56 BPP + ASTC10x6, //!< ASTC 10x6 2.13 BPP + ASTC10x8, //!< ASTC 10x8 1.60 BPP + ASTC10x10, //!< ASTC 10x10 1.28 BPP + ASTC12x10, //!< ASTC 12x10 1.07 BPP + ASTC12x12, //!< ASTC 12x12 0.89 BPP Unknown, // Compressed formats above. @@ -121,8 +129,11 @@ namespace bimg RGBA32I, RGBA32U, RGBA32F, + B5G6R5, R5G6B5, + BGRA4, RGBA4, + BGR5A1, RGB5A1, RGB10A2, RG11B10F, @@ -195,6 +206,7 @@ namespace bimg bool m_cubeMap; bool m_ktx; bool m_ktxLE; + bool m_pvr3; bool m_srgb; }; @@ -425,6 +437,7 @@ namespace bimg , uint32_t _height , uint32_t _depth , uint32_t _srcPitch + , uint32_t _dstPitch ); /// @@ -540,6 +553,7 @@ namespace bimg , uint32_t _depth , uint8_t _numMips , uint32_t _numLayers + , bool _srgb , const void* _src , bx::Error* _err = NULL ); diff --git a/3rdparty/bimg/include/bimg/decode.h b/3rdparty/bimg/include/bimg/decode.h index d092b2f7ec7..d5c942d2f98 100644 --- a/3rdparty/bimg/include/bimg/decode.h +++ b/3rdparty/bimg/include/bimg/decode.h @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #ifndef BIMG_DECODE_H_HEADER_GUARD diff --git a/3rdparty/bimg/include/bimg/encode.h b/3rdparty/bimg/include/bimg/encode.h index 326728d2700..26fd79cefad 100644 --- a/3rdparty/bimg/include/bimg/encode.h +++ b/3rdparty/bimg/include/bimg/encode.h @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #ifndef BIMG_ENCODE_H_HEADER_GUARD @@ -18,6 +18,10 @@ namespace bimg Highest, Fastest, + NormalMapDefault, // Treat the source as a normal map + NormalMapHighest, + NormalMapFastest, + Count }; }; @@ -113,6 +117,7 @@ namespace bimg , const void* _src , float _alphaRef , float _scale = 1.0f + , uint32_t _upscale = 1 ); /// @@ -124,6 +129,7 @@ namespace bimg , void* _src , float _coverage , float _alphaRef + , uint32_t _upscale = 1 ); /// diff --git a/3rdparty/bimg/makefile b/3rdparty/bimg/makefile index cd11a89f7b6..f97c689f5e2 100644 --- a/3rdparty/bimg/makefile +++ b/3rdparty/bimg/makefile @@ -1,6 +1,6 @@ # -# Copyright 2011-2018 Branimir Karadzic. All rights reserved. -# License: https://github.com/bkaradzic/bimg#license-bsd-2-clause +# Copyright 2011-2022 Branimir Karadzic. All rights reserved. +# License: https://github.com/bkaradzic/bimg/blob/master/LICENSE # UNAME := $(shell uname) @@ -42,14 +42,15 @@ projgen: ## Generate project files for all configurations. $(GENIE) --with-tools vs2017 $(GENIE) --with-tools --gcc=mingw-gcc gmake $(GENIE) --with-tools --gcc=linux-gcc gmake - $(GENIE) --with-tools --gcc=osx gmake + $(GENIE) --with-tools --gcc=osx-x64 gmake $(GENIE) --with-tools --xcode=osx xcode8 $(GENIE) --with-tools --xcode=ios xcode8 $(GENIE) --gcc=freebsd gmake $(GENIE) --gcc=android-arm gmake - $(GENIE) --gcc=android-mips gmake + $(GENIE) --gcc=android-arm64 gmake $(GENIE) --gcc=android-x86 gmake - $(GENIE) --gcc=asmjs gmake + $(GENIE) --gcc=wasm2js gmake + $(GENIE) --gcc=wasm gmake $(GENIE) --gcc=ios-arm gmake $(GENIE) --gcc=ios-arm64 gmake $(GENIE) --gcc=ios-simulator gmake @@ -64,13 +65,13 @@ android-arm-release: .build/projects/gmake-android-arm ## Build - Android ARM Re $(MAKE) -R -C .build/projects/gmake-android-arm config=release android-arm: android-arm-debug android-arm-release ## Build - Android ARM Debug and Release -.build/projects/gmake-android-mips: - $(GENIE) --gcc=android-mips gmake -android-mips-debug: .build/projects/gmake-android-mips ## Build - Android MIPS Debug - $(MAKE) -R -C .build/projects/gmake-android-mips config=debug -android-mips-release: .build/projects/gmake-android-mips ## Build - Android MIPS Release - $(MAKE) -R -C .build/projects/gmake-android-mips config=release -android-mips: android-mips-debug android-mips-release ## Build - Android MIPS Debug and Release +.build/projects/gmake-android-arm64: + $(GENIE) --gcc=android-arm64 gmake +android-arm64-debug: .build/projects/gmake-android-arm64 ## Build - Android ARM64 Debug + $(MAKE) -R -C .build/projects/gmake-android-arm64 config=debug +android-arm64-release: .build/projects/gmake-android-arm64 ## Build - Android ARM64 Release + $(MAKE) -R -C .build/projects/gmake-android-arm64 config=release +android-arm64: android-arm64-debug android-arm64-release ## Build - Android ARM64 Debug and Release .build/projects/gmake-android-x86: $(GENIE) --gcc=android-x86 gmake @@ -80,13 +81,21 @@ android-x86-release: .build/projects/gmake-android-x86 ## Build - Android x86 De $(MAKE) -R -C .build/projects/gmake-android-x86 config=release android-x86: android-x86-debug android-x86-release ## Build - Android x86 Debug and Release -.build/projects/gmake-asmjs: - $(GENIE) --gcc=asmjs gmake -asmjs-debug: .build/projects/gmake-asmjs ## Build - Emscripten Debug - $(MAKE) -R -C .build/projects/gmake-asmjs config=debug -asmjs-release: .build/projects/gmake-asmjs ## Build - Emscripten Release - $(MAKE) -R -C .build/projects/gmake-asmjs config=release -asmjs: asmjs-debug asmjs-release ## Build - Emscripten Debug and Release +.build/projects/gmake-wasm2js: + $(GENIE) --gcc=wasm2js gmake +wasm2js-debug: .build/projects/gmake-wasm2js ## Build - Emscripten Debug + $(MAKE) -R -C .build/projects/gmake-wasm2js config=debug +wasm2js-release: .build/projects/gmake-wasm2js ## Build - Emscripten Release + $(MAKE) -R -C .build/projects/gmake-wasm2js config=release +wasm2js: wasm2js-debug wasm2js-release ## Build - Emscripten Debug and Release + +.build/projects/gmake-wasm: + $(GENIE) --gcc=wasm gmake +wasm-debug: .build/projects/gmake-wasm ## Build - Emscripten Debug + $(MAKE) -R -C .build/projects/gmake-wasm config=debug +wasm-release: .build/projects/gmake-wasm ## Build - Emscripten Release + $(MAKE) -R -C .build/projects/gmake-wasm config=release +wasm: wasm-debug wasm-release ## Build - Emscripten Debug and Release .build/projects/gmake-linux: $(GENIE) --with-tools --gcc=linux-gcc gmake @@ -144,13 +153,21 @@ vs2017-release64: .build/projects/vs2017 ## Build - vs2017 x64 Release devenv .build/projects/vs2017/bimg.sln /Build "Release|x64" vs2017: vs2017-debug32 vs2017-release32 vs2017-debug64 vs2017-release64 ## Build - vs2017 x86/x64 Debug and Release -.build/projects/gmake-osx: - $(GENIE) --with-tools --gcc=osx gmake -osx-debug64: .build/projects/gmake-osx ## Build - OSX x64 Debug - $(MAKE) -C .build/projects/gmake-osx config=debug64 -osx-release64: .build/projects/gmake-osx ## Build - OSX x64 Release - $(MAKE) -C .build/projects/gmake-osx config=release64 -osx: osx-debug64 osx-release64 ## Build - OSX x64 Debug and Release +.build/projects/gmake-osx-x64: + $(GENIE) --gcc=osx-x64 gmake +osx-x64-debug: .build/projects/gmake-osx-x64 + make -C .build/projects/gmake-osx config=debug +osx-x64-release: .build/projects/gmake-osx-x64 + make -C .build/projects/gmake-osx config=release +osx-x64: osx-x64-debug osx-x64-release + +.build/projects/gmake-osx-arm64: + $(GENIE) --gcc=osx-arm64 gmake +osx-arm64-debug: .build/projects/gmake-osx-arm64 + make -C .build/projects/gmake-osx-arm64 config=debug +osx-arm64-release: .build/projects/gmake-osx-arm64 + make -C .build/projects/gmake-osx-arm64 config=release +osx-arm64: osx-arm64-debug osx-arm64-release .build/projects/gmake-ios-arm: $(GENIE) --gcc=ios-arm gmake @@ -192,7 +209,7 @@ rpi-release: .build/projects/gmake-rpi ## Build - RasberryPi Release $(MAKE) -R -C .build/projects/gmake-rpi config=release rpi: rpi-debug rpi-release ## Build - RasberryPi Debug and Release -build-darwin: osx +build-darwin: osx-x64 build-linux: linux-debug64 linux-release64 @@ -222,9 +239,9 @@ UNAME := $(shell uname) ifeq ($(UNAME),$(filter $(UNAME),Linux Darwin FreeBSD GNU/kFreeBSD)) ifeq ($(UNAME),$(filter $(UNAME),Darwin)) OS=darwin -BUILD_PROJECT_DIR=gmake-osx -BUILD_OUTPUT_DIR=osx64_clang -BUILD_TOOLS_CONFIG=release64 +BUILD_PROJECT_DIR=gmake-osx-x64 +BUILD_OUTPUT_DIR=osx-x64 +BUILD_TOOLS_CONFIG=release BUILD_TOOLS_SUFFIX=Release EXE= else @@ -275,33 +292,15 @@ clean-tools: ## Clean tools projects. -$(SILENT) rm -r .build/projects/$(BUILD_PROJECT_DIR) dist-windows: .build/projects/gmake-mingw-gcc - $(SILENT) $(MAKE) -C .build/projects/gmake-mingw-gcc config=release64 -j 6 geometryc - $(SILENT) cp .build/win64_mingw-gcc/bin/geometrycRelease.exe tools/bin/windows/geometryc.exe - $(SILENT) $(MAKE) -C .build/projects/gmake-mingw-gcc config=release64 -j 6 shaderc - $(SILENT) cp .build/win64_mingw-gcc/bin/shadercRelease.exe tools/bin/windows/shaderc.exe - $(SILENT) $(MAKE) -C .build/projects/gmake-mingw-gcc config=release64 -j 6 texturec - $(SILENT) cp .build/win64_mingw-gcc/bin/texturecRelease.exe tools/bin/windows/texturec.exe - $(SILENT) $(MAKE) -C .build/projects/gmake-mingw-gcc config=release64 -j 6 texturev - $(SILENT) cp .build/win64_mingw-gcc/bin/texturevRelease.exe tools/bin/windows/texturev.exe + $(SILENT) $(MAKE) -C .build/projects/gmake-mingw-gcc config=release64 -j 6 texturec + $(SILENT) cp .build/win64_mingw-gcc/bin/texturecRelease.exe tools/bin/windows/texturec.exe dist-linux: .build/projects/gmake-linux - $(SILENT) $(MAKE) -C .build/projects/gmake-linux config=release64 -j 6 geometryc - $(SILENT) cp .build/linux64_gcc/bin/geometrycRelease tools/bin/linux/geometryc - $(SILENT) $(MAKE) -C .build/projects/gmake-linux config=release64 -j 6 shaderc - $(SILENT) cp .build/linux64_gcc/bin/shadercRelease tools/bin/linux/shaderc - $(SILENT) $(MAKE) -C .build/projects/gmake-linux config=release64 -j 6 texturec - $(SILENT) cp .build/linux64_gcc/bin/texturecRelease tools/bin/linux/texturec - $(SILENT) $(MAKE) -C .build/projects/gmake-linux config=release64 -j 6 texturev - $(SILENT) cp .build/linux64_gcc/bin/texturevRelease tools/bin/linux/texturev - -dist-darwin: .build/projects/gmake-osx - $(SILENT) $(MAKE) -C .build/projects/gmake-osx config=release64 -j 6 geometryc - $(SILENT) cp .build/osx64_clang/bin/geometrycRelease tools/bin/darwin/geometryc - $(SILENT) $(MAKE) -C .build/projects/gmake-osx config=release64 -j 6 shaderc - $(SILENT) cp .build/osx64_clang/bin/shadercRelease tools/bin/darwin/shaderc - $(SILENT) $(MAKE) -C .build/projects/gmake-osx config=release64 -j 6 texturec - $(SILENT) cp .build/osx64_clang/bin/texturecRelease tools/bin/darwin/texturec - $(SILENT) $(MAKE) -C .build/projects/gmake-osx config=release64 -j 6 texturev - $(SILENT) cp .build/osx64_clang/bin/texturevRelease tools/bin/darwin/texturev + $(SILENT) $(MAKE) -C .build/projects/gmake-linux config=release64 -j 6 texturec + $(SILENT) cp .build/linux64_gcc/bin/texturecRelease tools/bin/linux/texturec + +dist-darwin: .build/projects/gmake-osx-x64 + $(SILENT) $(MAKE) -C .build/projects/gmake-osx-x64 config=release -j 6 texturec + $(SILENT) cp .build/osx-x64/bin/texturecRelease tools/bin/darwin/texturec dist: clean dist-windows dist-linux dist-darwin diff --git a/3rdparty/bimg/scripts/bimg.lua b/3rdparty/bimg/scripts/bimg.lua index a70460ef092..0dde12d3899 100644 --- a/3rdparty/bimg/scripts/bimg.lua +++ b/3rdparty/bimg/scripts/bimg.lua @@ -1,30 +1,34 @@ -- --- Copyright 2010-2018 Branimir Karadzic. All rights reserved. +-- Copyright 2010-2022 Branimir Karadzic. All rights reserved. -- License: https://github.com/bkaradzic/bx#license-bsd-2-clause -- -function filesexist(_srcPath, _dstPath, _files) - for _, file in ipairs(_files) do - file = path.getrelative(_srcPath, file) - local filePath = path.join(_dstPath, file) - if not os.isfile(filePath) then return false end - end - - return true -end - project "bimg" kind "StaticLib" includedirs { - path.join(BX_DIR, "include"), path.join(BIMG_DIR, "include"), + path.join(BIMG_DIR, "3rdparty/astc-encoder/include"), + path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz"), } files { path.join(BIMG_DIR, "include/**"), path.join(BIMG_DIR, "src/image.*"), path.join(BIMG_DIR, "src/image_gnf.cpp"), + + path.join(BIMG_DIR, "3rdparty/astc-encoder/source/**.cpp"), + path.join(BIMG_DIR, "3rdparty/astc-encoder/source/**.h"), + + path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz/miniz.*"), + } + + using_bx() + + configuration {} + + removeflags { + "FloatFast", -- astc-encoder doesn't work with it. } configuration { "linux-*" } diff --git a/3rdparty/bimg/scripts/bimg_decode.lua b/3rdparty/bimg/scripts/bimg_decode.lua index ca045ed1b92..143027e731e 100644 --- a/3rdparty/bimg/scripts/bimg_decode.lua +++ b/3rdparty/bimg/scripts/bimg_decode.lua @@ -1,5 +1,5 @@ -- --- Copyright 2010-2018 Branimir Karadzic. All rights reserved. +-- Copyright 2010-2022 Branimir Karadzic. All rights reserved. -- License: https://github.com/bkaradzic/bx#license-bsd-2-clause -- @@ -7,11 +7,9 @@ project "bimg_decode" kind "StaticLib" includedirs { - path.join(BX_DIR, "include"), path.join(BIMG_DIR, "include"), path.join(BIMG_DIR, "3rdparty"), - path.join(BIMG_DIR, "3rdparty/nvtt"), - path.join(BIMG_DIR, "3rdparty/iqa/include"), + path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz"), } files { @@ -19,6 +17,8 @@ project "bimg_decode" path.join(BIMG_DIR, "src/image_decode.*"), } + using_bx() + configuration { "linux-*" } buildoptions { "-fPIC", diff --git a/3rdparty/bimg/scripts/bimg_encode.lua b/3rdparty/bimg/scripts/bimg_encode.lua index 06649108b82..6bc48d345e0 100644 --- a/3rdparty/bimg/scripts/bimg_encode.lua +++ b/3rdparty/bimg/scripts/bimg_encode.lua @@ -1,5 +1,5 @@ -- --- Copyright 2010-2018 Branimir Karadzic. All rights reserved. +-- Copyright 2010-2022 Branimir Karadzic. All rights reserved. -- License: https://github.com/bkaradzic/bx#license-bsd-2-clause -- @@ -7,11 +7,12 @@ project "bimg_encode" kind "StaticLib" includedirs { - path.join(BX_DIR, "include"), path.join(BIMG_DIR, "include"), path.join(BIMG_DIR, "3rdparty"), - path.join(BIMG_DIR, "3rdparty/nvtt"), + path.join(BIMG_DIR, "3rdparty/astc-encoder/include"), path.join(BIMG_DIR, "3rdparty/iqa/include"), + path.join(BIMG_DIR, "3rdparty/nvtt"), + path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz"), } files { @@ -30,16 +31,35 @@ project "bimg_encode" path.join(BIMG_DIR, "3rdparty/nvtt/**.h"), path.join(BIMG_DIR, "3rdparty/pvrtc/**.cpp"), path.join(BIMG_DIR, "3rdparty/pvrtc/**.h"), - path.join(BIMG_DIR, "3rdparty/astc/**.cpp"), - path.join(BIMG_DIR, "3rdparty/astc/**.h"), path.join(BIMG_DIR, "3rdparty/tinyexr/**.h"), path.join(BIMG_DIR, "3rdparty/iqa/include/**.h"), path.join(BIMG_DIR, "3rdparty/iqa/source/**.c"), } + using_bx() + + configuration {} + + removeflags { + "FloatFast", -- astc-encoder doesn't work with it. + } + configuration { "linux-*" } buildoptions { "-fPIC", } + configuration { "mingw* or linux* or osx*" } + buildoptions { + "-Wno-implicit-fallthrough", + "-Wno-shadow", + "-Wno-shift-negative-value", + "-Wno-undef", + } + + buildoptions_cpp { + "-Wno-class-memaccess", + "-Wno-deprecated-copy", + } + configuration {} diff --git a/3rdparty/bimg/scripts/genie.lua b/3rdparty/bimg/scripts/genie.lua index 93acdfb712f..2cd13581109 100644 --- a/3rdparty/bimg/scripts/genie.lua +++ b/3rdparty/bimg/scripts/genie.lua @@ -1,6 +1,6 @@ -- --- Copyright 2010-2018 Branimir Karadzic. All rights reserved. --- License: https://github.com/bkaradzic/bimg#license-bsd-2-clause +-- Copyright 2010-2022 Branimir Karadzic. All rights reserved. +-- License: https://github.com/bkaradzic/bimg/blob/master/LICENSE -- newoption { @@ -37,7 +37,6 @@ solution "bimg" end language "C++" - startproject "example-00-helloworld" MODULE_DIR = path.getabsolute("..") BIMG_DIR = path.getabsolute("..") @@ -64,12 +63,11 @@ function copyLib() end group "libs" +dofile(path.join(BX_DIR, "scripts/bx.lua")) dofile "bimg.lua" dofile "bimg_decode.lua" dofile "bimg_encode.lua" -dofile(path.join(BX_DIR, "scripts/bx.lua")) - if _OPTIONS["with-tools"] then group "tools" dofile "texturec.lua" diff --git a/3rdparty/bimg/scripts/texturec.lua b/3rdparty/bimg/scripts/texturec.lua index 9bb3ef63649..2a392696701 100644 --- a/3rdparty/bimg/scripts/texturec.lua +++ b/3rdparty/bimg/scripts/texturec.lua @@ -1,13 +1,12 @@ -- --- Copyright 2010-2018 Branimir Karadzic. All rights reserved. --- License: https://github.com/bkaradzic/bimg#license-bsd-2-clause +-- Copyright 2010-2022 Branimir Karadzic. All rights reserved. +-- License: https://github.com/bkaradzic/bimg/blob/master/LICENSE -- project "texturec" kind "ConsoleApp" includedirs { - path.join(BX_DIR, "include"), path.join(BIMG_DIR, "include"), } @@ -20,13 +19,14 @@ project "texturec" "bimg_decode", "bimg_encode", "bimg", - "bx", } + using_bx() + configuration { "mingw-*" } targetextension ".exe" - configuration { "osx" } + configuration { "osx*" } links { "Cocoa.framework", } diff --git a/3rdparty/bimg/src/bimg_p.h b/3rdparty/bimg/src/bimg_p.h index c74531d2b5e..909d654f848 100644 --- a/3rdparty/bimg/src/bimg_p.h +++ b/3rdparty/bimg/src/bimg_p.h @@ -1,19 +1,56 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #ifndef BIMG_P_H_HEADER_GUARD #define BIMG_P_H_HEADER_GUARD +#ifndef BX_CONFIG_DEBUG +# error "BX_CONFIG_DEBUG must be defined in build script!" +#endif // BIMG_CONFIG_DEBUG + +#if BX_CONFIG_DEBUG +# define BX_TRACE _BIMG_TRACE +# define BX_WARN _BIMG_WARN +# define BX_ASSERT _BIMG_ASSERT +#endif // BX_CONFIG_DEBUG + +#define BX_ASSERT2 BX_ASSERT + +#define _BIMG_TRACE(_format, ...) \ + BX_MACRO_BLOCK_BEGIN \ + bx::debugPrintf(__FILE__ "(" BX_STRINGIZE(__LINE__) "): BX " _format "\n", ##__VA_ARGS__); \ + BX_MACRO_BLOCK_END + +#define _BIMG_WARN(_condition, _format, ...) \ + BX_MACRO_BLOCK_BEGIN \ + if (!BX_IGNORE_C4127(_condition) ) \ + { \ + BX_TRACE("WARN " _format, ##__VA_ARGS__); \ + } \ + BX_MACRO_BLOCK_END + +#define _BIMG_ASSERT(_condition, _format, ...) \ + BX_MACRO_BLOCK_BEGIN \ + if (!BX_IGNORE_C4127(_condition) ) \ + { \ + BX_TRACE("ASSERT " _format, ##__VA_ARGS__); \ + bx::debugBreak(); \ + } \ + BX_MACRO_BLOCK_END + #include <bimg/bimg.h> #include <bx/allocator.h> +#include <bx/debug.h> #include <bx/readerwriter.h> #include <bx/pixelformat.h> #include <bx/endian.h> #include <bx/error.h> #include <bx/simd_t.h> +#include "config.h" + #define BIMG_CHUNK_MAGIC_TEX BX_MAKEFOURCC('T', 'E', 'X', 0x0) #define BIMG_CHUNK_MAGIC_GNF BX_MAKEFOURCC('G', 'N', 'F', ' ') @@ -48,7 +85,7 @@ namespace bimg if (_hasMips) { const uint32_t max = bx::max(_width, _height, _depth); - const uint32_t num = 1 + uint32_t(bx::log2(float(max) ) ); + const uint32_t num = 1 + uint32_t(bx::log2((int32_t)max) ); return uint8_t(num); } diff --git a/3rdparty/bimg/src/config.h b/3rdparty/bimg/src/config.h new file mode 100644 index 00000000000..8d4d94fe99a --- /dev/null +++ b/3rdparty/bimg/src/config.h @@ -0,0 +1,59 @@ +/* + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE + */ + +#ifndef BIMG_CONFIG_H_HEADER_GUARD +#define BIMG_CONFIG_H_HEADER_GUARD + +#include <bx/bx.h> + +#ifndef BIMG_DECODE_ENABLE +# define BIMG_DECODE_ENABLE 1 +#endif // BIMG_DECODE_ENABLE + +#ifndef BIMG_DECODE_BC1 +# define BIMG_DECODE_BC1 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC1 + +#ifndef BIMG_DECODE_BC2 +# define BIMG_DECODE_BC2 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC2 + +#ifndef BIMG_DECODE_BC3 +# define BIMG_DECODE_BC3 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC3 + +#ifndef BIMG_DECODE_BC4 +# define BIMG_DECODE_BC4 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC4 + +#ifndef BIMG_DECODE_BC5 +# define BIMG_DECODE_BC5 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC5 + +#ifndef BIMG_DECODE_BC6 +# define BIMG_DECODE_BC6 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC6 + +#ifndef BIMG_DECODE_BC7 +# define BIMG_DECODE_BC7 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_BC7 + +#ifndef BIMG_DECODE_ATC +# define BIMG_DECODE_ATC BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_ATC + +#ifndef BIMG_DECODE_ASTC +# define BIMG_DECODE_ASTC BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_ASTC + +#ifndef BIMG_DECODE_ETC1 +# define BIMG_DECODE_ETC1 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_ETC1 + +#ifndef BIMG_DECODE_ETC2 +# define BIMG_DECODE_ETC2 BIMG_DECODE_ENABLE +#endif // BIMG_DECODE_ETC2 + +#endif // BIMG_CONFIG_H_HEADER_GUARD diff --git a/3rdparty/bimg/src/image.cpp b/3rdparty/bimg/src/image.cpp index 6bf0d5245da..63d46254a09 100644 --- a/3rdparty/bimg/src/image.cpp +++ b/3rdparty/bimg/src/image.cpp @@ -1,14 +1,14 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include "bimg_p.h" #include <bx/hash.h> -#if BIMG_CONFIG_ASTC_DECODE -# include "../3rdparty/astc/astc_lib.h" -#endif // BIMG_CONFIG_ASTC_DECODE +#include <astcenc.h> + +#include <bx/debug.h> namespace bimg { @@ -46,11 +46,19 @@ namespace bimg { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCE { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCI { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC4x4 + { 6, 5, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x4 { 6, 5, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x5 + { 4, 6, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x5 { 4, 6, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x6 { 4, 8, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x5 { 3, 8, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x6 + { 2, 8, 8, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x8 { 3, 10, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x5 + { 2, 10, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x6 + { 2, 10, 8, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x8 + { 1, 10,10, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x10 + { 1, 12,10, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC12x10 + { 1, 12,12, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC12x12 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // Unknown { 1, 8, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R1 { 8, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, uint8_t(bx::EncodingType::Unorm) }, // A8 @@ -96,8 +104,11 @@ namespace bimg { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Int ) }, // RGBA32I { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Uint ) }, // RGBA32U { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Float) }, // RGBA32F + { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // B5G6R5 { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // R5G6B5 + { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // BGRA4 { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // RGBA4 + { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // BGR5A1 { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // RGB5A1 { 32, 1, 1, 4, 1, 1, 0, 0, 10, 10, 10, 2, uint8_t(bx::EncodingType::Unorm) }, // RGB10A2 { 32, 1, 1, 4, 1, 1, 0, 0, 11, 11, 10, 0, uint8_t(bx::EncodingType::Unorm) }, // RG11B10F @@ -136,11 +147,19 @@ namespace bimg "ATCE", // ATCE "ATCI", // ATCI "ASTC4x4", // ASTC4x4 + "ASTC5x4", // ASTC5x4 "ASTC5x5", // ASTC5x5 + "ASTC6x5", // ASTC6x5 "ASTC6x6", // ASTC6x6 "ASTC8x5", // ASTC8x5 "ASTC8x6", // ASTC8x6 + "ASTC8x8", // ASTC8x8 "ASTC10x5", // ASTC10x5 + "ASTC10x6", // ASTC10x6 + "ASTC10x8", // ASTC10x8 + "ASTC10x10", // ASTC10x10 + "ASTC12x10", // ASTC12x10 + "ASTC12x12", // ASTC12x12 "<unknown>", // Unknown "R1", // R1 "A8", // A8 @@ -186,11 +205,14 @@ namespace bimg "RGBA32I", // RGBA32I "RGBA32U", // RGBA32U "RGBA32F", // RGBA32F + "B5G6R5", // B5G6R5 "R5G6B5", // R5G6B5 + "BGRA4", // BGRA4 "RGBA4", // RGBA4 + "BGR5A1", // BGR5A1 "RGB5A1", // RGB5A1 "RGB10A2", // RGB10A2 - "RG11B10F", // RG11B10F + "RG11B10F", // RG11B10F "<unknown>", // UnknownDepth "D16", // D16 "D24", // D24 @@ -252,6 +274,11 @@ namespace bimg const char* getName(TextureFormat::Enum _format) { + if (_format >= TextureFormat::Count) + { + return "Unknown?!"; + } + return s_textureFormatName[_format]; } @@ -341,7 +368,7 @@ namespace bimg return size; } - void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid) + BX_NO_INLINE void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid) { uint32_t* dst = (uint32_t*)_dst; for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii) @@ -350,7 +377,7 @@ namespace bimg } } - void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1) + BX_NO_INLINE void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1) { uint32_t* dst = (uint32_t*)_dst; for (uint32_t yy = 0; yy < _height; ++yy) @@ -566,8 +593,8 @@ namespace bimg for (uint16_t side = 0; side < numSides; ++side) { - bimg::ImageMip mip; - bimg::imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); + ImageMip mip; + imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); const uint32_t pitch = _imageContainer->m_width*16; const uint32_t slice = _imageContainer->m_height*pitch; @@ -612,8 +639,8 @@ namespace bimg for (uint16_t side = 0; side < numSides; ++side) { - bimg::ImageMip mip; - bimg::imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); + ImageMip mip; + imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); const uint32_t pitch = _imageContainer->m_width*16; const uint32_t slice = _imageContainer->m_height*pitch; @@ -928,7 +955,7 @@ namespace bimg xyz[1] += rgba1[5]; xyz[2] += rgba1[6]; - bx::store(dst, bx::normalize(bx::load(xyz) ) ); + bx::store(dst, bx::normalize(bx::load<bx::Vec3>(xyz) ) ); } } } @@ -1014,7 +1041,7 @@ namespace bimg for (uint32_t zz = 0; zz < _depth; ++zz, src += _srcPitch*_height, dst += _dstPitch*_height) { - bx::memCopy(dst, src, pitch, _height, _srcPitch, _dstPitch); + bx::memCopy(dst, _dstPitch, src, _srcPitch, pitch, _height); } } @@ -1053,14 +1080,22 @@ namespace bimg { NULL, NULL }, // ATCE { NULL, NULL }, // ATCI { NULL, NULL }, // ASTC4x4 + { NULL, NULL }, // ASTC5x4 { NULL, NULL }, // ASTC5x5 + { NULL, NULL }, // ASTC6x5 { NULL, NULL }, // ASTC6x6 { NULL, NULL }, // ASTC8x5 { NULL, NULL }, // ASTC8x6 + { NULL, NULL }, // ASTC8x8 { NULL, NULL }, // ASTC10x5 + { NULL, NULL }, // ASTC10x6 + { NULL, NULL }, // ASTC10x8 + { NULL, NULL }, // ASTC10x10 + { NULL, NULL }, // ASTC12x10 + { NULL, NULL }, // ASTC12x12 { NULL, NULL }, // Unknown { NULL, NULL }, // R1 - { bx::packR8, bx::unpackR8 }, // A8 + { bx::packA8, bx::unpackA8 }, // A8 { bx::packR8, bx::unpackR8 }, // R8 { bx::packR8I, bx::unpackR8I }, // R8I { bx::packR8U, bx::unpackR8U }, // R8U @@ -1103,8 +1138,11 @@ namespace bimg { bx::packRgba32I, bx::unpackRgba32I }, // RGBA32I { bx::packRgba32U, bx::unpackRgba32U }, // RGBA32U { bx::packRgba32F, bx::unpackRgba32F }, // RGBA32F + { bx::packB5G6R5, bx::unpackB5G6R5 }, // B5G6R5 { bx::packR5G6B5, bx::unpackR5G6B5 }, // R5G6B5 + { bx::packBgra4, bx::unpackBgra4 }, // BGRA4 { bx::packRgba4, bx::unpackRgba4 }, // RGBA4 + { bx::packBgr5a1, bx::unpackBgr5a1 }, // BGR5A1 { bx::packRgb5a1, bx::unpackRgb5a1 }, // RGB5A1 { bx::packRgb10A2, bx::unpackRgb10A2 }, // RGB10A2 { bx::packRG11B10F, bx::unpackRG11B10F }, // RG11B10F @@ -1118,7 +1156,7 @@ namespace bimg { bx::packR32F, bx::unpackR32F }, // D32F { bx::packR8, bx::unpackR8 }, // D0S8 }; - BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) ); + BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) ); PackFn getPack(TextureFormat::Enum _format) { @@ -1154,16 +1192,14 @@ namespace bimg } } - void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch) + void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch) { const uint8_t* src = (uint8_t*)_src; uint8_t* dst = (uint8_t*)_dst; - const uint32_t dstPitch = _width * _dstBpp / 8; - for (uint32_t zz = 0; zz < _depth; ++zz) { - for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += dstPitch) + for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _dstPitch) { for (uint32_t xx = 0; xx < _width; ++xx) { @@ -1175,7 +1211,7 @@ namespace bimg } } - bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch) + bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch) { UnpackFn unpack = s_packUnpack[_srcFormat].unpack; PackFn pack = s_packUnpack[_dstFormat].pack; @@ -1205,7 +1241,7 @@ namespace bimg const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel; const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel; - imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch); + imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch, _dstPitch); return true; } @@ -1220,7 +1256,10 @@ namespace bimg return true; } - return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8); + const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel; + const uint32_t dstPitch = _width * dstBpp / 8; + + return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8, dstPitch); } ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input, bool _convertMips) @@ -1258,7 +1297,7 @@ namespace bimg , mip.m_height , mip.m_depth ); - BX_CHECK(ok, "Conversion from %s to %s failed!" + BX_ASSERT(ok, "Conversion from %s to %s failed!" , getName(_input.m_format) , getName(output->m_format) ); @@ -1278,11 +1317,15 @@ namespace bimg bx::MemoryReader reader(_src, _size); uint32_t magic; - bx::read(&reader, magic); + bx::read(&reader, magic, bx::ErrorIgnore{}); ImageContainer imageContainer; - if (magicT != magic - || !parseFnT(imageContainer, &reader, _err) ) + if (magicT != magic) + { + return NULL; + } + + if (!parseFnT(imageContainer, &reader, _err) ) { return NULL; } @@ -1319,7 +1362,7 @@ namespace bimg return output; } - uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to) + static uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to) { using namespace bx; uint32_t tmp0 = uint32_sll(1, _to); @@ -1335,8 +1378,13 @@ namespace bimg return uint8_t(result); } - void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) ) + { + return; + } + uint8_t colors[4*3]; uint32_t c0 = _src[0] | (_src[1] << 8); @@ -1366,8 +1414,13 @@ namespace bimg } } - void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) ) + { + return; + } + uint8_t colors[4*4]; uint32_t c0 = _src[0] | (_src[1] << 8); @@ -1417,8 +1470,13 @@ namespace bimg } } - void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC2) ) + { + return; + } + for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4) { uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf; @@ -1426,8 +1484,13 @@ namespace bimg } } - void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) ) + { + return; + } + uint8_t alpha[8]; alpha[0] = _src[0]; alpha[1] = _src[1]; @@ -1543,69 +1606,69 @@ namespace bimg static const uint32_t s_bptcP3[] = { // 76543210 0000 1111 2222 3333 4444 5555 6666 7777 0xaa685050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2 - 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1 - 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1 - 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1 - 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2 - 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2 - 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1 - 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1 - 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2 - 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2 - 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 - 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2 - 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2 - 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2 - 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2 - 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0 - 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2 - 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0 - 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2 - 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1 - 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2 - 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1 - 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2 - 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0 - 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0 - 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2 - 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0 - 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1 - 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2 - 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2 - 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1 - 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1 - 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2 - 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1 - 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2 - 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0 - 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0 - 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0 - 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0 - 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1 - 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1 - 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2 - 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1 - 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2 - 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1 - 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1 - 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1 - 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1 - 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2 - 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1 - 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2 - 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2 - 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2 - 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2 - 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2 - 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2 - 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2 - 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2 - 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2 - 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2 - 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1 - 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2 - 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 - 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0 + 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1 + 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1 + 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1 + 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2 + 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2 + 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1 + 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1 + 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2 + 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2 + 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 + 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2 + 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2 + 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2 + 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2 + 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0 + 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2 + 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0 + 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2 + 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1 + 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2 + 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1 + 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2 + 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0 + 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0 + 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2 + 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0 + 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1 + 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2 + 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2 + 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1 + 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1 + 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2 + 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1 + 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2 + 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0 + 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0 + 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0 + 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0 + 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1 + 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1 + 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2 + 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1 + 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2 + 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1 + 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1 + 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1 + 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1 + 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2 + 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1 + 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2 + 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2 + 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2 + 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2 + 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2 + 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2 + 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2 + 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2 + 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2 + 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2 + 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1 + 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2 + 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 + 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0 }; static const uint8_t s_bptcA2[] = @@ -1683,7 +1746,7 @@ namespace bimg uint16_t m_bitPos; }; - uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits) + static uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits) { const uint16_t maxValue = 1<<(_endpointBits-1); @@ -1733,7 +1796,7 @@ namespace bimg return ( (_value<<15) + 0x4000) >> (_endpointBits-1); } - uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed) + static uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed) { if (_signed) { @@ -1746,7 +1809,7 @@ namespace bimg return (_value * 31) >> 6; } - uint16_t signExtend(uint16_t _value, uint8_t _numBits) + static uint16_t signExtend(uint16_t _value, uint8_t _numBits) { const uint16_t mask = 1 << (_numBits - 1); const uint16_t result = (_value ^ mask) - mask; @@ -1801,330 +1864,348 @@ namespace bimg { 0, 0, 0, { 0, 0, 0 } }, // - }; - void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed) + static void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed) { - BitReader bit(_src); - - uint8_t mode = uint8_t(bit.read(2) ); - if (mode & 2) - { - // 5-bit mode - mode |= bit.read(3) << 2; - } - - const Bc6hModeInfo& mi = s_bc6hModeInfo[mode]; - if (0 == mi.endpointBits) + if (!BX_ENABLED(BIMG_DECODE_BC6) ) { - bx::memSet(_dst, 0, 16*3*2); return; } + uint8_t src[16]; + bx::memCopy(src, _src, 16); + + BitReader bit(src); + + uint8_t mode = uint8_t(bit.read(2)); + uint16_t epR[4] = { /* rw, rx, ry, rz */ }; uint16_t epG[4] = { /* gw, gx, gy, gz */ }; uint16_t epB[4] = { /* bw, bx, by, bz */ }; - switch (mode) - { - case 0: - epG[2] |= bit.read( 1) << 4; - epB[2] |= bit.read( 1) << 4; - epB[3] |= bit.read( 1) << 4; - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 5) << 0; - epG[3] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 3; - break; + if (mode & 2) + { + // 5-bit mode + mode |= bit.read(3) << 2; - case 1: - epG[2] |= bit.read( 1) << 5; - epG[3] |= bit.read( 1) << 4; - epG[3] |= bit.read( 1) << 5; - epR[0] |= bit.read( 7) << 0; - epB[3] |= bit.read( 1) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 7) << 0; - epB[2] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 2; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 7) << 0; - epB[3] |= bit.read( 1) << 3; - epB[3] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 6) << 0; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 6) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 6) << 0; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 6) << 0; - epR[3] |= bit.read( 6) << 0; - break; + if (0 == s_bc6hModeInfo[mode].endpointBits) + { + bx::memSet(_dst, 0, 16*3*2); + return; + } - case 2: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 5) << 0; - epR[0] |= bit.read( 1) << 10; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 4) << 0; - epG[0] |= bit.read( 1) << 10; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 4) << 0; - epB[0] |= bit.read( 1) << 10; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 3; - break; + switch (mode) + { + case 2: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 5) << 0; + epR[0] |= bit.read( 1) << 10; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 4) << 0; + epG[0] |= bit.read( 1) << 10; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 4) << 0; + epB[0] |= bit.read( 1) << 10; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 3; + break; - case 3: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read(10) << 0; - epG[1] |= bit.read(10) << 0; - epB[1] |= bit.read(10) << 0; - break; + case 3: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read(10) << 0; + epG[1] |= bit.read(10) << 0; + epB[1] |= bit.read(10) << 0; + break; - case 6: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 4) << 0; - epR[0] |= bit.read( 1) << 10; - epG[3] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 5) << 0; - epG[0] |= bit.read( 1) << 10; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 4) << 0; - epB[0] |= bit.read( 1) << 10; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 4) << 0; - epB[3] |= bit.read( 1) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 4) << 0; - epG[2] |= bit.read( 1) << 4; - epB[3] |= bit.read( 1) << 3; - break; + case 6: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 4) << 0; + epR[0] |= bit.read( 1) << 10; + epG[3] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 5) << 0; + epG[0] |= bit.read( 1) << 10; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 4) << 0; + epB[0] |= bit.read( 1) << 10; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 4) << 0; + epB[3] |= bit.read( 1) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 4) << 0; + epG[2] |= bit.read( 1) << 4; + epB[3] |= bit.read( 1) << 3; + break; - case 7: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 9) << 0; - epR[0] |= bit.read( 1) << 10; - epG[1] |= bit.read( 9) << 0; - epG[0] |= bit.read( 1) << 10; - epB[1] |= bit.read( 9) << 0; - epB[0] |= bit.read( 1) << 10; - break; + case 7: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 9) << 0; + epR[0] |= bit.read( 1) << 10; + epG[1] |= bit.read( 9) << 0; + epG[0] |= bit.read( 1) << 10; + epB[1] |= bit.read( 9) << 0; + epB[0] |= bit.read( 1) << 10; + break; - case 10: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 4) << 0; - epR[0] |= bit.read( 1) << 10; - epB[2] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 4) << 0; - epG[0] |= bit.read( 1) << 10; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 5) << 0; - epB[0] |= bit.read( 1) << 10; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 4) << 0; - epB[3] |= bit.read( 1) << 1; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 4) << 0; - epB[3] |= bit.read( 1) << 4; - epB[3] |= bit.read( 1) << 3; - break; + case 10: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 4) << 0; + epR[0] |= bit.read( 1) << 10; + epB[2] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 4) << 0; + epG[0] |= bit.read( 1) << 10; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 5) << 0; + epB[0] |= bit.read( 1) << 10; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 4) << 0; + epB[3] |= bit.read( 1) << 1; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 4) << 0; + epB[3] |= bit.read( 1) << 4; + epB[3] |= bit.read( 1) << 3; + break; - case 11: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 8) << 0; - epR[0] |= bit.read( 1) << 11; - epR[0] |= bit.read( 1) << 10; - epG[1] |= bit.read( 8) << 0; - epG[0] |= bit.read( 1) << 11; - epG[0] |= bit.read( 1) << 10; - epB[1] |= bit.read( 8) << 0; - epB[0] |= bit.read( 1) << 11; - epB[0] |= bit.read( 1) << 10; - break; + case 11: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 8) << 0; + epR[0] |= bit.read( 1) << 11; + epR[0] |= bit.read( 1) << 10; + epG[1] |= bit.read( 8) << 0; + epG[0] |= bit.read( 1) << 11; + epG[0] |= bit.read( 1) << 10; + epB[1] |= bit.read( 8) << 0; + epB[0] |= bit.read( 1) << 11; + epB[0] |= bit.read( 1) << 10; + break; - case 14: - epR[0] |= bit.read( 9) << 0; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 9) << 0; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 9) << 0; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 5) << 0; - epG[3] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 3; - break; + case 14: + epR[0] |= bit.read( 9) << 0; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 9) << 0; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 9) << 0; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 5) << 0; + epG[3] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 3; + break; - case 15: - epR[0] |= bit.read(10) << 0; - epG[0] |= bit.read(10) << 0; - epB[0] |= bit.read(10) << 0; - epR[1] |= bit.read( 4) << 0; - epR[0] |= bit.read( 1) << 15; - epR[0] |= bit.read( 1) << 14; - epR[0] |= bit.read( 1) << 13; - epR[0] |= bit.read( 1) << 12; - epR[0] |= bit.read( 1) << 11; - epR[0] |= bit.read( 1) << 10; - epG[1] |= bit.read( 4) << 0; - epG[0] |= bit.read( 1) << 15; - epG[0] |= bit.read( 1) << 14; - epG[0] |= bit.read( 1) << 13; - epG[0] |= bit.read( 1) << 12; - epG[0] |= bit.read( 1) << 11; - epG[0] |= bit.read( 1) << 10; - epB[1] |= bit.read( 4) << 0; - epB[0] |= bit.read( 1) << 15; - epB[0] |= bit.read( 1) << 14; - epB[0] |= bit.read( 1) << 13; - epB[0] |= bit.read( 1) << 12; - epB[0] |= bit.read( 1) << 11; - epB[0] |= bit.read( 1) << 10; - break; + case 15: + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 4) << 0; + epR[0] |= bit.read( 1) << 15; + epR[0] |= bit.read( 1) << 14; + epR[0] |= bit.read( 1) << 13; + epR[0] |= bit.read( 1) << 12; + epR[0] |= bit.read( 1) << 11; + epR[0] |= bit.read( 1) << 10; + epG[1] |= bit.read( 4) << 0; + epG[0] |= bit.read( 1) << 15; + epG[0] |= bit.read( 1) << 14; + epG[0] |= bit.read( 1) << 13; + epG[0] |= bit.read( 1) << 12; + epG[0] |= bit.read( 1) << 11; + epG[0] |= bit.read( 1) << 10; + epB[1] |= bit.read( 4) << 0; + epB[0] |= bit.read( 1) << 15; + epB[0] |= bit.read( 1) << 14; + epB[0] |= bit.read( 1) << 13; + epB[0] |= bit.read( 1) << 12; + epB[0] |= bit.read( 1) << 11; + epB[0] |= bit.read( 1) << 10; + break; - case 18: - epR[0] |= bit.read( 8) << 0; - epG[3] |= bit.read( 1) << 4; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 8) << 0; - epB[3] |= bit.read( 1) << 2; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 8) << 0; - epB[3] |= bit.read( 1) << 3; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 6) << 0; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 6) << 0; - epR[3] |= bit.read( 6) << 0; - break; + case 18: + epR[0] |= bit.read( 8) << 0; + epG[3] |= bit.read( 1) << 4; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 8) << 0; + epB[3] |= bit.read( 1) << 2; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 8) << 0; + epB[3] |= bit.read( 1) << 3; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 6) << 0; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 6) << 0; + epR[3] |= bit.read( 6) << 0; + break; - case 22: - epR[0] |= bit.read( 8) << 0; - epB[3] |= bit.read( 1) << 0; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 8) << 0; - epG[2] |= bit.read( 1) << 5; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 8) << 0; - epG[3] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 5) << 0; - epG[3] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 6) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 3; - break; + case 22: + epR[0] |= bit.read( 8) << 0; + epB[3] |= bit.read( 1) << 0; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 8) << 0; + epG[2] |= bit.read( 1) << 5; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 8) << 0; + epG[3] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 5) << 0; + epG[3] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 6) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 3; + break; - case 26: - epR[0] |= bit.read( 8) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 8) << 0; - epB[2] |= bit.read( 1) << 5; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 8) << 0; - epB[3] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 5) << 0; - epG[3] |= bit.read( 1) << 4; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 6) << 0; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 2; - epR[3] |= bit.read( 5) << 0; - epB[3] |= bit.read( 1) << 3; - break; + case 26: + epR[0] |= bit.read( 8) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 8) << 0; + epB[2] |= bit.read( 1) << 5; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 8) << 0; + epB[3] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 5) << 0; + epG[3] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 6) << 0; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 3; + break; - case 30: - epR[0] |= bit.read( 6) << 0; - epG[3] |= bit.read( 1) << 4; - epB[3] |= bit.read( 1) << 0; - epB[3] |= bit.read( 1) << 1; - epB[2] |= bit.read( 1) << 4; - epG[0] |= bit.read( 6) << 0; - epG[2] |= bit.read( 1) << 5; - epB[2] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 2; - epG[2] |= bit.read( 1) << 4; - epB[0] |= bit.read( 6) << 0; - epG[3] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 3; - epB[3] |= bit.read( 1) << 5; - epB[3] |= bit.read( 1) << 4; - epR[1] |= bit.read( 6) << 0; - epG[2] |= bit.read( 4) << 0; - epG[1] |= bit.read( 6) << 0; - epG[3] |= bit.read( 4) << 0; - epB[1] |= bit.read( 6) << 0; - epB[2] |= bit.read( 4) << 0; - epR[2] |= bit.read( 6) << 0; - epR[3] |= bit.read( 6) << 0; - break; + case 30: + epR[0] |= bit.read( 6) << 0; + epG[3] |= bit.read( 1) << 4; + epB[3] |= bit.read( 1) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 6) << 0; + epG[2] |= bit.read( 1) << 5; + epB[2] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 2; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 6) << 0; + epG[3] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 3; + epB[3] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 6) << 0; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 6) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 6) << 0; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 6) << 0; + epR[3] |= bit.read( 6) << 0; + break; - default: - break; + default: + break; + } + } + else + { + switch (mode) + { + case 0: + epG[2] |= bit.read( 1) << 4; + epB[2] |= bit.read( 1) << 4; + epB[3] |= bit.read( 1) << 4; + epR[0] |= bit.read(10) << 0; + epG[0] |= bit.read(10) << 0; + epB[0] |= bit.read(10) << 0; + epR[1] |= bit.read( 5) << 0; + epG[3] |= bit.read( 1) << 4; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 2; + epR[3] |= bit.read( 5) << 0; + epB[3] |= bit.read( 1) << 3; + break; + + case 1: + epG[2] |= bit.read( 1) << 5; + epG[3] |= bit.read( 1) << 4; + epG[3] |= bit.read( 1) << 5; + epR[0] |= bit.read( 7) << 0; + epB[3] |= bit.read( 1) << 0; + epB[3] |= bit.read( 1) << 1; + epB[2] |= bit.read( 1) << 4; + epG[0] |= bit.read( 7) << 0; + epB[2] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 2; + epG[2] |= bit.read( 1) << 4; + epB[0] |= bit.read( 7) << 0; + epB[3] |= bit.read( 1) << 3; + epB[3] |= bit.read( 1) << 5; + epB[3] |= bit.read( 1) << 4; + epR[1] |= bit.read( 6) << 0; + epG[2] |= bit.read( 4) << 0; + epG[1] |= bit.read( 6) << 0; + epG[3] |= bit.read( 4) << 0; + epB[1] |= bit.read( 6) << 0; + epB[2] |= bit.read( 4) << 0; + epR[2] |= bit.read( 6) << 0; + epR[3] |= bit.read( 6) << 0; + break; + + default: + break; + } } + const Bc6hModeInfo mi = s_bc6hModeInfo[mode]; + if (_signed) { epR[0] = signExtend(epR[0], mi.endpointBits); @@ -2208,8 +2289,13 @@ namespace bimg } } - void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16]) + static void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16]) { + if (!BX_ENABLED(BIMG_DECODE_BC6) ) + { + return; + } + uint16_t tmp[16*3]; decodeBlockBc6h(tmp, _src, true); @@ -2256,8 +2342,13 @@ namespace bimg { 2, 6, 0, 0, 5, 5, 1, 0, { 2, 0 } }, // 7 }; - void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16]) + static void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16]) { + if (!BX_ENABLED(BIMG_DECODE_BC7) ) + { + return; + } + BitReader bit(_src); uint8_t mode = 0; @@ -2445,8 +2536,13 @@ namespace bimg // ATC // - void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_ATC) ) + { + return; + } + uint8_t colors[4*4]; uint32_t c0 = _src[0] | (_src[1] << 8); @@ -2513,7 +2609,7 @@ namespace bimg static const uint8_t s_etc2Mod[] = { 3, 6, 11, 16, 23, 32, 41, 64 }; - uint8_t uint8_sat(int32_t _a) + static uint8_t uint8_sat(int32_t _a) { using namespace bx; const uint32_t min = uint32_imin(_a, 255); @@ -2521,19 +2617,19 @@ namespace bimg return (uint8_t)result; } - uint8_t uint8_satadd(int32_t _a, int32_t _b) + static uint8_t uint8_satadd(int32_t _a, int32_t _b) { const int32_t add = _a + _b; return uint8_sat(add); } - void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8]) { uint8_t rgb[16]; // 0 1 2 3 4 5 6 7 // 7654321076543210765432107654321076543210765432107654321076543210 - // ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll + // ...rr.rrggggbbbbrrrrggggbbbbDD.Dmmmmmmmmmmmmmmmmllllllllllllllll // ^ ^ ^ ^ ^ // +-- c0 +-- c1 | +-- msb +-- lsb // +-- dist @@ -2555,7 +2651,7 @@ namespace bimg rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8); rgb[10] = bitRangeConvert(rgb[10], 4, 8); - uint8_t dist = (_src[3] >> 1) & 0x7; + uint8_t dist = ((_src[3] >> 1) & 0x6) | (_src[3] & 0x1); int32_t mod = s_etc2Mod[dist]; rgb[ 4] = uint8_satadd(rgb[ 8], mod); @@ -2586,13 +2682,13 @@ namespace bimg } } - void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8]) { uint8_t rgb[16]; // 0 1 2 3 4 5 6 7 // 7654321076543210765432107654321076543210765432107654321076543210 - // .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll + // .rrrrggg...gb.bbbrrrrggggbbbbD.Dmmmmmmmmmmmmmmmmllllllllllllllll // ^ ^ ^ ^ ^ // +-- c0 +-- c1 | +-- msb +-- lsb // +-- dist @@ -2610,7 +2706,7 @@ namespace bimg rgb[ 9] = ( (_src[2] << 1) & 0xe) | (_src[3] >> 7) ; - rgb[10] = (_src[2] >> 3) & 0xf; + rgb[10] = (_src[3] >> 3) & 0xf; rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8); rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8); @@ -2621,7 +2717,7 @@ namespace bimg uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]); uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]); - uint8_t dist = (_src[3] & 0x6) | (col0 >= col1); + uint8_t dist = (_src[3] & 0x4) | ((_src[3]<<1)&0x2) | (col0 >= col1); int32_t mod = s_etc2Mod[dist]; rgb[ 4] = uint8_satadd(rgb[ 0], -mod); @@ -2660,7 +2756,7 @@ namespace bimg } } - void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8]) { // 0 1 2 3 4 5 6 7 // 7654321076543210765432107654321076543210765432107654321076543210 @@ -2752,8 +2848,13 @@ namespace bimg } } - void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8]) + static void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) ) + { + return; + } + bool flipBit = 0 != (_src[3] & 0x1); bool diffBit = 0 != (_src[3] & 0x2); @@ -2866,6 +2967,52 @@ namespace bimg } } + static const int8_t s_etc2aMod[16][8] = + { + { -3, -6, -9, -15, 2, 5, 8, 14 }, + { -3, -7, -10, -13, 2, 6, 9, 12 }, + { -2, -5, -8, -13, 1, 4, 7, 12 }, + { -2, -4, -6, -13, 1, 3, 5, 12 }, + { -3, -6, -8, -12, 2, 5, 7, 11 }, + { -3, -7, -9, -11, 2, 6, 8, 10 }, + { -4, -7, -8, -11, 3, 6, 7, 10 }, + { -3, -5, -8, -11, 2, 4, 7, 10 }, + { -2, -6, -8, -10, 1, 5, 7, 9 }, + { -2, -5, -8, -10, 1, 4, 7, 9 }, + { -2, -4, -8, -10, 1, 3, 7, 9 }, + { -2, -5, -7, -10, 1, 4, 6, 9 }, + { -3, -4, -7, -10, 2, 3, 6, 9 }, + { -1, -2, -3, -10, 0, 1, 2, 9 }, + { -4, -6, -8, -9, 3, 5, 7, 8 }, + { -3, -5, -7, -9, 2, 4, 6, 8 } + }; + + void decodeBlockEtc2Alpha(uint8_t _dst[16 * 4], const uint8_t _src[8]) + { + if (!BX_ENABLED(BIMG_DECODE_ETC2)) + { + return; + } + + const int32_t bc = _src[0]; + const int8_t *modTable = s_etc2aMod[_src[1] & 0x0f]; + const int32_t mult = (_src[1] & 0xf0) >> 4; + const uint64_t indices = ((uint64_t)_src[2] << 40) + | ((uint64_t)_src[3] << 32) + | ((uint64_t)_src[4] << 24) + | ((uint64_t)_src[5] << 16) + | ((uint64_t)_src[6] << 8) + | _src[7]; + + for (int ii = 0; ii < 16; ii++) { + const uint32_t idx = (ii & 0xc) | ((ii & 0x3) << 4); + const int32_t mod = modTable[(indices >> (45 - ii * 3)) & 0x7]; + + _dst[idx + 3] = uint8_satadd(bc, mod*mult); + } + } + + static const uint8_t s_pvrtcFactors[16][4] = { { 4, 4, 4, 4 }, @@ -2922,7 +3069,7 @@ namespace bimg ; } - void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor) + static void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor) { if (0 != (_block & (1<<15) ) ) { @@ -2938,7 +3085,7 @@ namespace bimg } } - void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor) + static void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor) { if (0 != (_block & (1<<31) ) ) { @@ -2954,7 +3101,7 @@ namespace bimg } } - void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height) + static void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height) { // 0 1 2 3 4 5 6 7 // 7654321076543210765432107654321076543210765432107654321076543210 @@ -3026,7 +3173,7 @@ namespace bimg } } - void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor) + static void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor) { if (0 != (_block & (1<<15) ) ) { @@ -3044,7 +3191,7 @@ namespace bimg } } - void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor) + static void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor) { if (0 != (_block & (1<<31) ) ) { @@ -3062,7 +3209,7 @@ namespace bimg } } - void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height) + static void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height) { // 0 1 2 3 4 5 6 7 // 7654321076543210765432107654321076543210765432107654321076543210 @@ -3152,7 +3299,7 @@ namespace bimg const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1; uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format); - ImageContainer* imageContainer = (ImageContainer*)BX_ALIGNED_ALLOC(_allocator, size + BX_ALIGN_16(sizeof(ImageContainer) ), 16); + ImageContainer* imageContainer = (ImageContainer*)BX_ALIGNED_ALLOC(_allocator, size + bx::alignUp(sizeof(ImageContainer), 16), 16); imageContainer->m_allocator = _allocator; imageContainer->m_data = bx::alignPtr(imageContainer + 1, 0, 16); @@ -3168,6 +3315,7 @@ namespace bimg imageContainer->m_hasAlpha = false; imageContainer->m_cubeMap = _cubeMap; imageContainer->m_ktx = false; + imageContainer->m_pvr3 = false; imageContainer->m_ktxLE = false; imageContainer->m_srgb = false; @@ -3177,6 +3325,7 @@ namespace bimg } return imageContainer; + } void imageFree(ImageContainer* _imageContainer) @@ -3199,20 +3348,28 @@ namespace bimg #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U') #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0') -#define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1') -#define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2') -#define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A') -#define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2') -#define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4') -#define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ') -#define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E') -#define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I') -#define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4') -#define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5') -#define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6') -#define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5') -#define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6') -#define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5') +#define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1') +#define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2') +#define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A') +#define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2') +#define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4') +#define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ') +#define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E') +#define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I') +#define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4') +#define DDS_ASTC5x4 BX_MAKEFOURCC('A', 'S', '5', '4') +#define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5') +#define DDS_ASTC6x5 BX_MAKEFOURCC('A', 'S', '6', '5') +#define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6') +#define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5') +#define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6') +#define DDS_ASTC8x8 BX_MAKEFOURCC('A', 'S', '8', '8') +#define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5') +#define DDS_ASTC10x6 BX_MAKEFOURCC('A', 'S', ':', '6') +#define DDS_ASTC10x8 BX_MAKEFOURCC('A', 'S', ':', '8') +#define DDS_ASTC10x10 BX_MAKEFOURCC('A', 'S', ':', ':') +#define DDS_ASTC12x10 BX_MAKEFOURCC('A', 'S', '<', ':') +#define DDS_ASTC12x12 BX_MAKEFOURCC('A', 'S', '<', '<') #define DDS_R8G8B8 20 #define DDS_A8R8G8B8 21 @@ -3340,11 +3497,19 @@ namespace bimg { DDS_ATCE, TextureFormat::ATCE, false }, { DDS_ATCI, TextureFormat::ATCI, false }, { DDS_ASTC4x4, TextureFormat::ASTC4x4, false }, + { DDS_ASTC5x4, TextureFormat::ASTC5x4, false }, { DDS_ASTC5x5, TextureFormat::ASTC5x5, false }, + { DDS_ASTC6x5, TextureFormat::ASTC6x5, false }, { DDS_ASTC6x6, TextureFormat::ASTC6x6, false }, { DDS_ASTC8x5, TextureFormat::ASTC8x5, false }, { DDS_ASTC8x6, TextureFormat::ASTC8x6, false }, + { DDS_ASTC8x8, TextureFormat::ASTC8x8, false }, { DDS_ASTC10x5, TextureFormat::ASTC10x5, false }, + { DDS_ASTC10x6, TextureFormat::ASTC10x6, false }, + { DDS_ASTC10x8, TextureFormat::ASTC10x8, false }, + { DDS_ASTC10x10, TextureFormat::ASTC10x10,false }, + { DDS_ASTC12x10, TextureFormat::ASTC12x10,false }, + { DDS_ASTC12x12, TextureFormat::ASTC12x12,false }, { DDS_A16B16G16R16, TextureFormat::RGBA16, false }, { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false }, @@ -3363,12 +3528,16 @@ namespace bimg { DDS_A16B16G16R16, TextureFormat::RGBA16, false }, { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false }, { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false }, + { DDS_R5G6B5, TextureFormat::B5G6R5, false }, { DDS_R5G6B5, TextureFormat::R5G6B5, false }, + { DDS_A4R4G4B4, TextureFormat::BGRA4, false }, { DDS_A4R4G4B4, TextureFormat::RGBA4, false }, + { DDS_A1R5G5B5, TextureFormat::BGR5A1, false }, { DDS_A1R5G5B5, TextureFormat::RGB5A1, false }, { DDS_A2B10G10R10, TextureFormat::RGB10A2, false }, }; + static const TranslateDdsFormat s_translateDxgiFormat[] = { { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false }, @@ -3402,8 +3571,11 @@ namespace bimg { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false }, { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false }, { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false }, + { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::B5G6R5, false }, { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false }, + { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::BGRA4, false }, { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false }, + { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::BGR5A1, false }, { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false }, { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false }, { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false }, @@ -3422,8 +3594,11 @@ namespace bimg { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 }, { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S }, { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U }, + { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x0000000f, 0x000000f0, 0x00000f00, 0x0000f000 }, TextureFormat::BGRA4 }, { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 }, + { 16, DDPF_RGB, { 0x0000001f, 0x000007e0, 0x0000f800, 0x00000000 }, TextureFormat::B5G6R5 }, { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 }, + { 16, DDPF_RGB, { 0x0000001f, 0x000003e0, 0x00007c00, 0x00008000 }, TextureFormat::BGR5A1 }, { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 }, { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 }, { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 }, @@ -3450,6 +3625,7 @@ namespace bimg if (!_err->isOk() || headerSize < DDS_HEADER_SIZE) { + BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid header size."); return false; } @@ -3530,6 +3706,8 @@ namespace bimg total += bx::read(_reader, miscFlags2, _err); } + BX_UNUSED(total); + if (!_err->isOk() ) { return false; @@ -3621,6 +3799,7 @@ namespace bimg _imageContainer.m_cubeMap = cubeMap; _imageContainer.m_ktx = false; _imageContainer.m_ktxLE = false; + _imageContainer.m_pvr3 = false; _imageContainer.m_srgb = srgb; return true; @@ -3673,17 +3852,33 @@ namespace bimg #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93 #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0 +#define KTX_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1 #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2 +#define KTX_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3 #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4 #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5 #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6 +#define KTX_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7 #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8 +#define KTX_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9 +#define KTX_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA +#define KTX_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB +#define KTX_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC +#define KTX_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0 +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1 #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2 +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3 #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4 #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5 #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6 +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7 #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8 +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9 +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC +#define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD #define KTX_A8 0x803C #define KTX_R8 0x8229 @@ -3797,11 +3992,19 @@ namespace bimg { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, }, // ATCE { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, }, // ATCI { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_ZERO, }, // ASTC4x4 + { KTX_COMPRESSED_RGBA_ASTC_5x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, KTX_COMPRESSED_RGBA_ASTC_5x4_KHR, KTX_ZERO, }, // ASTC5x4 { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_ZERO, }, // ASTC5x5 + { KTX_COMPRESSED_RGBA_ASTC_6x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, KTX_COMPRESSED_RGBA_ASTC_6x5_KHR, KTX_ZERO, }, // ASTC6x5 { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_ZERO, }, // ASTC6x6 { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_ZERO, }, // ASTC8x5 { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_ZERO, }, // ASTC8x6 + { KTX_COMPRESSED_RGBA_ASTC_8x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, KTX_COMPRESSED_RGBA_ASTC_8x8_KHR, KTX_ZERO, }, // ASTC8x8 { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_ZERO, }, // ASTC10x5 + { KTX_COMPRESSED_RGBA_ASTC_10x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, KTX_COMPRESSED_RGBA_ASTC_10x6_KHR, KTX_ZERO, }, // ASTC10x6 + { KTX_COMPRESSED_RGBA_ASTC_10x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, KTX_COMPRESSED_RGBA_ASTC_10x8_KHR, KTX_ZERO, }, // ASTC10x8 + { KTX_COMPRESSED_RGBA_ASTC_10x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, KTX_COMPRESSED_RGBA_ASTC_10x10_KHR, KTX_ZERO, }, // ASTC10x10 + { KTX_COMPRESSED_RGBA_ASTC_12x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, KTX_COMPRESSED_RGBA_ASTC_12x10_KHR, KTX_ZERO, }, // ASTC12x10 + { KTX_COMPRESSED_RGBA_ASTC_12x12_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, KTX_COMPRESSED_RGBA_ASTC_12x12_KHR, KTX_ZERO, }, // ASTC12x12 { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1 { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8 @@ -3847,8 +4050,11 @@ namespace bimg { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F + { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // B5G6R5 { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5 + { KTX_RGBA4, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // BGRA4 { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4 + { KTX_RGB5_A1, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // BGR5A1 { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1 { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2 { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F @@ -3875,60 +4081,67 @@ namespace bimg BX_ERROR_SCOPE(_err); uint8_t identifier[8]; - bx::read(_reader, identifier); + bx::read(_reader, identifier, _err); if (identifier[1] != '1' && identifier[2] != '1') { + BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized version."); return false; } uint32_t endianness; - bx::read(_reader, endianness); + bx::read(_reader, endianness, _err); bool fromLittleEndian = 0x04030201 == endianness; uint32_t glType; - bx::readHE(_reader, glType, fromLittleEndian); + bx::readHE(_reader, glType, fromLittleEndian, _err); uint32_t glTypeSize; - bx::readHE(_reader, glTypeSize, fromLittleEndian); + bx::readHE(_reader, glTypeSize, fromLittleEndian, _err); uint32_t glFormat; - bx::readHE(_reader, glFormat, fromLittleEndian); + bx::readHE(_reader, glFormat, fromLittleEndian, _err); uint32_t glInternalFormat; - bx::readHE(_reader, glInternalFormat, fromLittleEndian); + bx::readHE(_reader, glInternalFormat, fromLittleEndian, _err); uint32_t glBaseInternalFormat; - bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian); + bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian, _err); uint32_t width; - bx::readHE(_reader, width, fromLittleEndian); + bx::readHE(_reader, width, fromLittleEndian, _err); uint32_t height; - bx::readHE(_reader, height, fromLittleEndian); + bx::readHE(_reader, height, fromLittleEndian, _err); uint32_t depth; - bx::readHE(_reader, depth, fromLittleEndian); + bx::readHE(_reader, depth, fromLittleEndian, _err); uint32_t numberOfArrayElements; - bx::readHE(_reader, numberOfArrayElements, fromLittleEndian); + bx::readHE(_reader, numberOfArrayElements, fromLittleEndian, _err); uint32_t numFaces; - bx::readHE(_reader, numFaces, fromLittleEndian); + bx::readHE(_reader, numFaces, fromLittleEndian, _err); uint32_t numMips; - bx::readHE(_reader, numMips, fromLittleEndian); + bx::readHE(_reader, numMips, fromLittleEndian, _err); uint32_t metaDataSize; - bx::readHE(_reader, metaDataSize, fromLittleEndian); + bx::readHE(_reader, metaDataSize, fromLittleEndian, _err); + + if (!_err->isOk() ) + { + return false; + } // skip meta garbage... int64_t offset = bx::skip(_reader, metaDataSize); TextureFormat::Enum format = TextureFormat::Unknown; bool hasAlpha = false; + bool srgb = false; for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii) { @@ -3937,6 +4150,14 @@ namespace bimg format = TextureFormat::Enum(ii); break; } + + if (s_translateKtxFormat[ii].m_internalFmtSrgb == glInternalFormat + && s_translateKtxFormat[ii].m_fmt == glBaseInternalFormat) + { + format = TextureFormat::Enum(ii); + srgb = true; + break; + } } if (TextureFormat::Unknown == format) @@ -3966,11 +4187,12 @@ namespace bimg _imageContainer.m_cubeMap = numFaces > 1; _imageContainer.m_ktx = true; _imageContainer.m_ktxLE = fromLittleEndian; - _imageContainer.m_srgb = false; + _imageContainer.m_pvr3 = false; + _imageContainer.m_srgb = srgb; if (TextureFormat::Unknown == format) { - BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format."); + BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized image format."); return false; } @@ -4011,8 +4233,11 @@ namespace bimg #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8) #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16) #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32) +#define PVR3_BGR565 PVR3_MAKE8CC('b', 'g', 'r', 0, 5, 6, 5, 0) #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0) +#define PVR3_BGRA4 PVR3_MAKE8CC('b', 'g', 'r', 'a', 4, 4, 4, 4) #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4) +#define PVR3_BGRA51 PVR3_MAKE8CC('b', 'g', 'r', 'a', 5, 5, 5, 1) #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1) #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2) @@ -4057,8 +4282,11 @@ namespace bimg { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F }, { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U }, { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F }, + { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::B5G6R5 }, { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 }, + { PVR3_BGRA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA4 }, { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 }, + { PVR3_BGRA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGR5A1 }, { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 }, { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 }, }; @@ -4068,37 +4296,42 @@ namespace bimg BX_ERROR_SCOPE(_err); uint32_t flags; - bx::read(_reader, flags); + bx::read(_reader, flags, _err); uint64_t pixelFormat; - bx::read(_reader, pixelFormat); + bx::read(_reader, pixelFormat, _err); uint32_t colorSpace; - bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB + bx::read(_reader, colorSpace, _err); // 0 - linearRGB, 1 - sRGB uint32_t channelType; - bx::read(_reader, channelType); + bx::read(_reader, channelType, _err); uint32_t height; - bx::read(_reader, height); + bx::read(_reader, height, _err); uint32_t width; - bx::read(_reader, width); + bx::read(_reader, width, _err); uint32_t depth; - bx::read(_reader, depth); + bx::read(_reader, depth, _err); uint32_t numSurfaces; - bx::read(_reader, numSurfaces); + bx::read(_reader, numSurfaces, _err); uint32_t numFaces; - bx::read(_reader, numFaces); + bx::read(_reader, numFaces, _err); uint32_t numMips; - bx::read(_reader, numMips); + bx::read(_reader, numMips, _err); uint32_t metaDataSize; - bx::read(_reader, metaDataSize); + bx::read(_reader, metaDataSize, _err); + + if (!_err->isOk() ) + { + return false; + } // skip meta garbage... int64_t offset = bx::skip(_reader, metaDataSize); @@ -4131,6 +4364,7 @@ namespace bimg _imageContainer.m_cubeMap = numFaces > 1; _imageContainer.m_ktx = false; _imageContainer.m_ktxLE = false; + _imageContainer.m_pvr3 = true; _imageContainer.m_srgb = colorSpace > 0; return TextureFormat::Unknown != format; @@ -4167,7 +4401,7 @@ namespace bimg else if (BIMG_CHUNK_MAGIC_TEX == magic) { TextureCreate tc; - bx::read(_reader, tc); + bx::read(_reader, tc, _err); _imageContainer.m_format = tc.m_format; _imageContainer.m_orientation = Orientation::R0; @@ -4192,6 +4426,7 @@ namespace bimg _imageContainer.m_cubeMap = tc.m_cubeMap; _imageContainer.m_ktx = false; _imageContainer.m_ktxLE = false; + _imageContainer.m_pvr3 = false; _imageContainer.m_srgb = false; return _err->isOk(); @@ -4226,12 +4461,12 @@ namespace bimg uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8); void* temp = BX_ALLOC(_allocator, size); imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat); - imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4); + imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch); BX_FREE(_allocator, temp); } else { - imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch); + imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch); } } } @@ -4249,106 +4484,147 @@ namespace bimg switch (_srcFormat) { case TextureFormat::BC1: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC1) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt1(temp, src); - src += 8; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt1(temp, src); + src += 8; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC2: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC2) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt23A(temp+3, src); - src += 8; - decodeBlockDxt(temp, src); - src += 8; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt23A(temp+3, src); + src += 8; + decodeBlockDxt(temp, src); + src += 8; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC3: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC3) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp+3, src); - src += 8; - decodeBlockDxt(temp, src); - src += 8; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt45A(temp+3, src); + src += 8; + decodeBlockDxt(temp, src); + src += 8; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC4: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC4) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp, src); - src += 8; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt45A(temp, src); + src += 8; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC5: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC5) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp+2, src); - src += 8; - decodeBlockDxt45A(temp+1, src); - src += 8; - - for (uint32_t ii = 0; ii < 16; ++ii) + for (uint32_t xx = 0; xx < width; ++xx) { - float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f; - float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f; - float nz = bx::sqrt(1.0f - nx*nx - ny*ny); - temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f); - temp[ii*4+3] = 0; - } + decodeBlockDxt45A(temp+2, src); + src += 8; + decodeBlockDxt45A(temp+1, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t ii = 0; ii < 16; ++ii) + { + float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f; + float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f; + float nz = bx::sqrt(1.0f - nx*nx - ny*ny); + temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f); + temp[ii*4+3] = 0; + } + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC6H: + if (BX_ENABLED(BIMG_DECODE_BC6) ) { ImageContainer* rgba32f = imageAlloc(_allocator , TextureFormat::RGBA32F @@ -4360,49 +4636,92 @@ namespace bimg , false ); imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat); - imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16); + imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch); imageFree(rgba32f); } + else + { + BX_WARN(false, "BC6 decoder is disabled (BIMG_DECODE_BC6)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC7: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC7) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockBc7(temp, src); - src += 16; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockBc7(temp, src); + src += 16; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC7 decoder is disabled (BIMG_DECODE_BC7)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::ETC1: case TextureFormat::ETC2: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockEtc12(temp, src); - src += 8; - - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockEtc12(temp, src); + src += 8; + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_DECODE_ETC1/ETC2)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::ETC2A: - BX_WARN(false, "ETC2A decoder is not implemented."); - imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + if (BX_ENABLED(BIMG_DECODE_ETC2)) + { + for (uint32_t yy = 0; yy < height; ++yy) + { + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockEtc12(temp, src + 8); + decodeBlockEtc2Alpha(temp, src); + + src += 16; + + uint8_t* block = &dst[yy*_dstPitch * 4 + xx * 16]; + bx::memCopy(&block[0 * _dstPitch], &temp[0], 16); + bx::memCopy(&block[1 * _dstPitch], &temp[16], 16); + bx::memCopy(&block[2 * _dstPitch], &temp[32], 16); + bx::memCopy(&block[3 * _dstPitch], &temp[48], 16); + } + } + } + else + { + BX_WARN(false, "ETC2 decoder is disabled (BIMG_DECODE_ETC2)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00)); + } break; case TextureFormat::ETC2A1: @@ -4518,29 +4837,21 @@ namespace bimg break; case TextureFormat::ASTC4x4: + case TextureFormat::ASTC5x4: case TextureFormat::ASTC5x5: + case TextureFormat::ASTC6x5: case TextureFormat::ASTC6x6: case TextureFormat::ASTC8x5: case TextureFormat::ASTC8x6: + case TextureFormat::ASTC8x8: case TextureFormat::ASTC10x5: -#if BIMG_CONFIG_ASTC_DECODE - astc_decompress - ( - (const uint8_t*) _src, - s_imageBlockInfo[_srcFormat].blockWidth, - s_imageBlockInfo[_srcFormat].blockHeight, - ASTC_DECODE_LDR_LINEAR, - - _width, - _height, - (uint8_t*) _dst, - ASTC_BGRA, - _dstPitch - ); -#else - BX_WARN(false, "ASTC decoder is not implemented."); - imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); -#endif + case TextureFormat::ASTC10x6: + case TextureFormat::ASTC10x8: + case TextureFormat::ASTC10x10: + case TextureFormat::ASTC12x10: + case TextureFormat::ASTC12x12: + imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat); + imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch); break; case TextureFormat::RGBA8: @@ -4554,7 +4865,7 @@ namespace bimg { const uint32_t srcPitch = _width * 4; const uint32_t size = bx::uint32_min(srcPitch, _dstPitch); - bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch); + bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height); } break; @@ -4562,7 +4873,7 @@ namespace bimg { const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel; const uint32_t srcPitch = _width * srcBpp / 8; - if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch) ) + if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) ) { // Failed to convert, just make ugly red-yellow checkerboard texture. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) ); @@ -4580,7 +4891,7 @@ namespace bimg { const uint32_t srcPitch = _width * 4; const uint32_t size = bx::uint32_min(srcPitch, _dstPitch); - bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch); + bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height); } break; @@ -4591,6 +4902,98 @@ namespace bimg } break; + case TextureFormat::ASTC4x4: + case TextureFormat::ASTC5x4: + case TextureFormat::ASTC5x5: + case TextureFormat::ASTC6x5: + case TextureFormat::ASTC6x6: + case TextureFormat::ASTC8x5: + case TextureFormat::ASTC8x6: + case TextureFormat::ASTC8x8: + case TextureFormat::ASTC10x5: + case TextureFormat::ASTC10x6: + case TextureFormat::ASTC10x8: + case TextureFormat::ASTC10x10: + case TextureFormat::ASTC12x10: + case TextureFormat::ASTC12x12: + if (BX_ENABLED(BIMG_DECODE_ASTC) ) + { + const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_srcFormat); + + astcenc_config config{}; + + astcenc_error status = astcenc_config_init( + ASTCENC_PRF_LDR + , astcBlockInfo.blockWidth + , astcBlockInfo.blockHeight + , 1 + , ASTCENC_PRE_MEDIUM + , ASTCENC_FLG_DECOMPRESS_ONLY + , &config + ); + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in config init %s", astcenc_get_error_string(status)); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); + break; + } + + astcenc_context* context; + status = astcenc_context_alloc(&config, 1, &context); + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in context alloc %s", astcenc_get_error_string(status)); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); + break; + } + + //Put image data into an astcenc_image + astcenc_image image{}; + image.dim_x = _width; + image.dim_y = _height; + image.dim_z = 1; + image.data_type = ASTCENC_TYPE_U8; + image.data = &_dst; + + const uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat); + + static const astcenc_swizzle swizzle + { //0123/rgba swizzle corresponds to ASTC_RGBA + ASTCENC_SWZ_R, + ASTCENC_SWZ_G, + ASTCENC_SWZ_B, + ASTCENC_SWZ_A, + }; + + status = astcenc_decompress_image( + context + , (const uint8_t*)_src + , size + , &image + , &swizzle + , 0 + ); + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in compress image %s", astcenc_get_error_string(status)); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); + + astcenc_context_free(context); + break; + } + + astcenc_context_free(context); + } + else + { + BX_WARN(false, "ASTC decoder is disabled (BIMG_DECODE_ASTC)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } + break; + default: { const uint32_t srcPitch = _width * 4; @@ -4753,7 +5156,7 @@ namespace bimg } else { - imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch); + imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch); } break; } @@ -4789,7 +5192,7 @@ namespace bimg const uint8_t* data = (const uint8_t*)_data; const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1); - if (_imageContainer.m_ktx) + if (_imageContainer.m_ktx || _imageContainer.m_pvr3) { uint32_t width = _imageContainer.m_width; uint32_t height = _imageContainer.m_height; @@ -4802,21 +5205,20 @@ namespace bimg depth = bx::max<uint32_t>(1, depth); const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize; - if (mipSize != width*height*depth*bpp/8) - { - BX_TRACE("x"); - } - const uint32_t size = mipSize*numSides; - uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE); - BX_CHECK(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize); - BX_UNUSED(size, imageSize); + if (_imageContainer.m_ktx) + { + const uint32_t size = mipSize * numSides; + uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE); + BX_ASSERT(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize); + BX_UNUSED(size, imageSize); - offset += sizeof(uint32_t); + offset += sizeof(uint32_t); + } for (uint16_t side = 0; side < numSides; ++side) { - BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size); + BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size); if (side == _side && lod == _lod) @@ -4853,7 +5255,7 @@ namespace bimg for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod) { - BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size); + BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size); width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth); height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight); @@ -5238,13 +5640,13 @@ namespace bimg return total; } - static int32_t imageWriteDdsHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, bx::Error* _err) + static int32_t imageWriteDdsHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err) { BX_ERROR_SCOPE(_err); uint32_t ddspf = UINT32_MAX; uint32_t dxgiFormat = UINT32_MAX; - uint32_t fourccFormat = UINT32_MAX; + uint32_t fourccFormat = UINT32_MAX; for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii) { @@ -5267,23 +5669,25 @@ namespace bimg } } - if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat) - { - for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii) - { - if (s_translateDdsFourccFormat[ii].m_textureFormat == _format) - { - fourccFormat = s_translateDdsFourccFormat[ii].m_format; - break; - } - } - } - - if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat && UINT32_MAX == fourccFormat) - { - BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported."); - return 0; - } + if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat) + { + for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii) + { + if (s_translateDdsFourccFormat[ii].m_textureFormat == _format) + { + fourccFormat = s_translateDdsFourccFormat[ii].m_format; + break; + } + } + } + + if (UINT32_MAX == ddspf + && UINT32_MAX == dxgiFormat + && UINT32_MAX == fourccFormat) + { + BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported."); + return 0; + } const uint32_t bpp = getBitsPerPixel(_format); @@ -5313,7 +5717,6 @@ namespace bimg total += bx::write(_writer, pitchOrLinearSize, _err); total += bx::write(_writer, _depth, _err); total += bx::write(_writer, uint32_t(_numMips), _err); - total += bx::writeRep(_writer, 0, 44, _err); // reserved1 if (UINT32_MAX != ddspf) @@ -5331,13 +5734,17 @@ namespace bimg total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err); - if (UINT32_MAX != fourccFormat) - total += bx::write(_writer, fourccFormat, _err); - else - total += bx::write(_writer, uint32_t(DDS_DX10), _err); + if (UINT32_MAX != fourccFormat) + { + total += bx::write(_writer, fourccFormat, _err); + } + else + { + total += bx::write(_writer, uint32_t(DDS_DX10), _err); + } - total += bx::write(_writer, uint32_t(0), _err); // bitCount - total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask + total += bx::write(_writer, uint32_t(0), _err); // bitCount + total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask } uint32_t caps[4] = @@ -5347,7 +5754,8 @@ namespace bimg 0, 0, }; - total += bx::write(_writer, caps, sizeof(caps) ); + + total += bx::write(_writer, caps, sizeof(caps), _err); total += bx::writeRep(_writer, 0, 4, _err); // reserved2 @@ -5359,10 +5767,10 @@ namespace bimg if (UINT32_MAX != dxgiFormat) { - total += bx::write(_writer, dxgiFormat); + total += bx::write(_writer, dxgiFormat, _err); total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags - total += bx::write(_writer, uint32_t(1), _err); // arraySize + total += bx::write(_writer, uint32_t(_numLayers), _err); // arraySize total += bx::write(_writer, uint32_t(0), _err); // miscFlags2 BX_WARN(total-headerStart == DDS_HEADER_SIZE+20 @@ -5388,6 +5796,7 @@ namespace bimg , _imageContainer.m_height , _imageContainer.m_depth , _imageContainer.m_numMips + , _imageContainer.m_numLayers , _err ); @@ -5396,7 +5805,7 @@ namespace bimg return total; } - for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides && _err->isOk(); ++side) + for (uint8_t side = 0, numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1); side < numSides && _err->isOk(); ++side) { for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod) { @@ -5411,19 +5820,24 @@ namespace bimg return total; } - static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err) + static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, bx::Error* _err) { BX_ERROR_SCOPE(_err); const KtxFormatInfo& tfi = s_translateKtxFormat[_format]; + uint32_t internalFmt = tfi.m_internalFmt; + if (_srgb && tfi.m_internalFmtSrgb != KTX_ZERO) { + internalFmt = tfi.m_internalFmtSrgb; + } + int32_t total = 0; total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err); total += bx::write(_writer, uint32_t(0x04030201), _err); total += bx::write(_writer, uint32_t(0), _err); // glType total += bx::write(_writer, uint32_t(1), _err); // glTypeSize total += bx::write(_writer, uint32_t(0), _err); // glFormat - total += bx::write(_writer, tfi.m_internalFmt, _err); // glInternalFormat + total += bx::write(_writer, internalFmt, _err); // glInternalFormat total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat total += bx::write(_writer, _width, _err); total += bx::write(_writer, _height, _err); @@ -5437,12 +5851,12 @@ namespace bimg return total; } - int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, const void* _src, bx::Error* _err) + int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, const void* _src, bx::Error* _err) { BX_ERROR_SCOPE(_err); int32_t total = 0; - total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _err); + total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _srgb, _err); if (!_err->isOk() ) { @@ -5479,8 +5893,8 @@ namespace bimg { for (uint8_t side = 0; side < numSides && _err->isOk(); ++side) { - total += bx::write(_writer, src, size, _err); - src += size; + total += bx::write(_writer, src, mipSize, _err); + src += mipSize; } } @@ -5505,6 +5919,7 @@ namespace bimg , _imageContainer.m_depth , _imageContainer.m_numMips , _imageContainer.m_numLayers + , _imageContainer.m_srgb , _err ); diff --git a/3rdparty/bimg/src/image_cubemap_filter.cpp b/3rdparty/bimg/src/image_cubemap_filter.cpp index a967926aa1c..99338998238 100644 --- a/3rdparty/bimg/src/image_cubemap_filter.cpp +++ b/3rdparty/bimg/src/image_cubemap_filter.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include "bimg_p.h" @@ -259,22 +259,46 @@ namespace bimg const float* src2 = (const float*)&srcData[y1*srcPitch + x0*16]; const float* src3 = (const float*)&srcData[y1*srcPitch + x1*16]; - const float tx = srcU - float(int32_t(x0) ); - const float ty = srcV - float(int32_t(y0) ); - const float omtx = 1.0f - tx; - const float omty = 1.0f - ty; + const float tx = srcU - float(int32_t(x0) ); + const float ty = srcV - float(int32_t(y0) ); + const float omtx = 1.0f - tx; + const float omty = 1.0f - ty; - float p0[4]; - bx::vec4Mul(p0, src0, omtx*omty); - - float p1[4]; - bx::vec4Mul(p1, src1, tx*omty); - - float p2[4]; - bx::vec4Mul(p2, src2, omtx*ty); - - float p3[4]; - bx::vec4Mul(p3, src3, tx*ty); + const float p0x = omtx*omty; + const float p0[4] = + { + src0[0] * p0x, + src0[1] * p0x, + src0[2] * p0x, + src0[3] * p0x, + }; + + const float p1x = tx*omty; + const float p1[4] = + { + src1[0] * p1x, + src1[1] * p1x, + src1[2] * p1x, + src1[3] * p1x, + }; + + const float p2x = omtx*ty; + const float p2[4] = + { + src2[0] * p2x, + src2[1] * p2x, + src2[2] * p2x, + src2[3] * p2x, + }; + + const float p3x = tx*ty; + const float p3[4] = + { + src3[0] * p3x, + src3[1] * p3x, + src3[2] * p3x, + src3[3] * p3x, + }; const float rr = p0[0] + p1[0] + p2[0] + p3[0]; const float gg = p0[1] + p1[1] + p2[1] + p3[1]; @@ -312,15 +336,17 @@ namespace bimg if (_input.m_depth != 1 && _input.m_numLayers != 1 && _input.m_format != TextureFormat::RGBA32F - && _input.m_width/6 != _input.m_height) + && ( (_input.m_width != _input.m_height*6) || (_input.m_width*6 != _input.m_height) ) ) { BX_ERROR_SET(_err, BIMG_ERROR, "Input image format is not strip projection."); return NULL; } + const bool horizontal = _input.m_width == _input.m_height*6; const uint32_t srcPitch = _input.m_width*16; - const uint32_t dstWidth = _input.m_height; + const uint32_t dstWidth = horizontal ? _input.m_height : _input.m_width; const uint32_t dstPitch = dstWidth*16; + const uint32_t step = horizontal ? dstPitch : dstPitch*dstWidth; ImageContainer* output = imageAlloc(_allocator , _input.m_format @@ -334,12 +360,12 @@ namespace bimg const uint8_t* srcData = (const uint8_t*)_input.m_data; - for (uint8_t side = 0; side < 6 && _err->isOk(); ++side, srcData += dstPitch) + for (uint8_t side = 0; side < 6 && _err->isOk(); ++side, srcData += step) { ImageMip dstMip; imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip); - bx::memCopy(const_cast<uint8_t*>(dstMip.m_data), srcData, dstPitch, dstWidth, srcPitch, dstPitch); + bx::memCopy(const_cast<uint8_t*>(dstMip.m_data), dstPitch, srcData, srcPitch, dstPitch, dstWidth); } return output; @@ -404,10 +430,10 @@ namespace bimg { Aabb() { - m_min[0] = bx::kFloatMax; - m_min[1] = bx::kFloatMax; - m_max[0] = -bx::kFloatMax; - m_max[1] = -bx::kFloatMax; + m_min[0] = bx::max<float>(); + m_min[1] = bx::max<float>(); + m_max[0] = bx::min<float>(); + m_max[1] = bx::min<float>(); } void add(float _x, float _y) @@ -429,10 +455,10 @@ namespace bimg bool isEmpty() const { // Has to have at least two points added so that no value is equal to initial state. - return ( (m_min[0] == bx::kFloatMax) - || (m_min[1] == bx::kFloatMax) - || (m_max[0] == -bx::kFloatMax) - || (m_max[1] == -bx::kFloatMax) + return ( (m_min[0] == bx::max<float>() ) + || (m_min[1] == bx::max<float>() ) + || (m_max[0] == bx::min<float>() ) + || (m_max[1] == bx::min<float>() ) ); } @@ -823,8 +849,8 @@ namespace bimg const float kGoldenSection = 0.61803398875f; float offset = kGoldenSection; - bx::Vec3 tangentX; - bx::Vec3 tangentY; + bx::Vec3 tangentX(bx::init::None); + bx::Vec3 tangentY(bx::init::None); bx::calcTangentFrame(tangentX, tangentY, _dir); for (uint32_t ii = 0; ii < kNumSamples; ++ii) @@ -870,7 +896,7 @@ namespace bimg if (0.0f < totalWeight) { - // Optimized Reversible Tonemapper for Resovle + // Optimized Reversible Tonemapper for Resolve // https://web.archive.org/web/20180717182019/https://gpuopen.com/optimized-reversible-tonemapper-for-resolve/ // Average, then reverse the tonemapper // @@ -954,7 +980,7 @@ namespace bimg { const float* normal = (const float*)&nsaMip.m_data[(yy*nsaMip.m_width+xx)*(nsaMip.m_bpp/8)]; const float solidAngle = normal[3]; - const float ndotl = bx::clamp(bx::dot(bx::load(normal), _dir), 0.0f, 1.0f); + const float ndotl = bx::clamp(bx::dot(bx::load<bx::Vec3>(normal), _dir), 0.0f, 1.0f); if (ndotl >= _specularAngle) { diff --git a/3rdparty/bimg/src/image_decode.cpp b/3rdparty/bimg/src/image_decode.cpp index 0ce4023e89a..dd391e5f8d1 100644 --- a/3rdparty/bimg/src/image_decode.cpp +++ b/3rdparty/bimg/src/image_decode.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include "bimg_p.h" @@ -12,11 +12,10 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits") BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter") BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-value") BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wdeprecated-declarations") +BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4018) // warning C4018: '<': signed/unsigned mismatch BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: '' : unreferenced formal parameter +BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4389) // warning C4389 : '==' : signed / unsigned mismatch BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505) // warning C4505: 'tinyexr::miniz::def_realloc_func': unreferenced local function has been removed -#if BX_PLATFORM_EMSCRIPTEN -# include <compat/ctype.h> -#endif // BX_PLATFORM_EMSCRIPTEN #define MINIZ_NO_ARCHIVE_APIS #define MINIZ_NO_STDIO #define TINYEXR_IMPLEMENTATION @@ -25,6 +24,8 @@ BX_PRAGMA_DIAGNOSTIC_POP() BX_PRAGMA_DIAGNOSTIC_PUSH(); BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant +BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4267) // warning C4267: '=' : conversion from 'size_t' to 'unsigned short', possible loss of data +BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4334) // warning C4334: '<<' : result of 32 - bit shift implicitly converted to 64 bits(was 64 - bit shift intended ? ) #define LODEPNG_NO_COMPILE_ENCODER #define LODEPNG_NO_COMPILE_DISK #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS @@ -49,9 +50,11 @@ void lodepng_free(void* _ptr) } BX_PRAGMA_DIAGNOSTIC_PUSH(); +BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast") BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers"); BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow"); -BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast") +BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wsign-compare"); +BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-but-set-variable"); BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Warray-bounds"); #if BX_COMPILER_GCC >= 60000 BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation"); @@ -107,8 +110,8 @@ namespace bimg case 1: case 2: case 4: - format = bimg::TextureFormat::R8; - palette = false; + palette = LCT_PALETTE == state.info_raw.colortype; + format = palette ? bimg::TextureFormat::RGBA8 : bimg::TextureFormat::R8; supported = true; break; @@ -140,6 +143,9 @@ namespace bimg palette = true; supported = true; break; + + case LCT_MAX_OCTET_VALUE: + break; } break; @@ -194,6 +200,9 @@ namespace bimg case LCT_PALETTE: break; + + case LCT_MAX_OCTET_VALUE: + break; } break; @@ -206,7 +215,10 @@ namespace bimg const uint8_t* copyData = data; TextureFormat::Enum dstFormat = format; - if (1 == state.info_raw.bitdepth + if (palette) { + copyData = NULL; + } + else if (1 == state.info_raw.bitdepth || 2 == state.info_raw.bitdepth || 4 == state.info_raw.bitdepth) { @@ -218,10 +230,6 @@ namespace bimg dstFormat = bimg::TextureFormat::RGBA16; copyData = NULL; } - else if (palette) - { - copyData = NULL; - } output = imageAlloc(_allocator , dstFormat @@ -234,7 +242,48 @@ namespace bimg , copyData ); - if (1 == state.info_raw.bitdepth) + if (palette) + { + if (1 == state.info_raw.bitdepth) { + for (uint32_t ii = 0, num = width*height/8; ii < num; ++ii) + { + uint8_t* dst = (uint8_t*)output->m_data + ii*32; + bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>7)&0x1)*4, 4); + bx::memCopy(dst + 4, state.info_raw.palette + ( (data[ii]>>6)&0x1)*4, 4); + bx::memCopy(dst + 8, state.info_raw.palette + ( (data[ii]>>5)&0x1)*4, 4); + bx::memCopy(dst + 12, state.info_raw.palette + ( (data[ii]>>4)&0x1)*4, 4); + bx::memCopy(dst + 16, state.info_raw.palette + ( (data[ii]>>3)&0x1)*4, 4); + bx::memCopy(dst + 20, state.info_raw.palette + ( (data[ii]>>2)&0x1)*4, 4); + bx::memCopy(dst + 24, state.info_raw.palette + ( (data[ii]>>1)&0x1)*4, 4); + bx::memCopy(dst + 28, state.info_raw.palette + ( data[ii] &0x1)*4, 4); + } + } + else if (2 == state.info_raw.bitdepth) { + for (uint32_t ii = 0, num = width*height/4; ii < num; ++ii) + { + uint8_t* dst = (uint8_t*)output->m_data + ii*16; + bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>6)&0x3)*4, 4); + bx::memCopy(dst + 4, state.info_raw.palette + ( (data[ii]>>4)&0x3)*4, 4); + bx::memCopy(dst + 8, state.info_raw.palette + ( (data[ii]>>2)&0x3)*4, 4); + bx::memCopy(dst + 12, state.info_raw.palette + ( data[ii] &0x3)*4, 4); + } + } + else if (4 == state.info_raw.bitdepth) { + for (uint32_t ii = 0, num = width*height/2; ii < num; ++ii) + { + uint8_t* dst = (uint8_t*)output->m_data + ii*8; + bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>4)&0xf)*4, 4); + bx::memCopy(dst + 4, state.info_raw.palette + ( data[ii] &0xf)*4, 4); + } + } + else { + for (uint32_t ii = 0, num = width*height; ii < num; ++ii) + { + bx::memCopy( (uint8_t*)output->m_data + ii*4, state.info_raw.palette + data[ii]*4, 4); + } + } + } + else if (1 == state.info_raw.bitdepth) { for (uint32_t ii = 0, num = width*height/8; ii < num; ++ii) { @@ -296,12 +345,77 @@ namespace bimg dst[3] = UINT16_MAX; } } - else if (palette) + + switch (state.info_raw.colortype) //Check for alpha values { - for (uint32_t ii = 0, num = width*height; ii < num; ++ii) - { - bx::memCopy( (uint8_t*)output->m_data + ii*4, state.info_raw.palette + data[ii]*4, 4); - } + case LCT_GREY: + case LCT_RGB: + break; + + case LCT_GREY_ALPHA: + if (8 == state.info_raw.bitdepth) + { + for (uint32_t ii = 0, num = width * height; ii < num; ++ii) + { + const uint8_t* rgba = (uint8_t*)data + ii * 2; + bool has_alpha = rgba[1] < UINT8_MAX; + if (has_alpha) + { + output->m_hasAlpha = has_alpha; + break; + } + } + } + else if(16 == state.info_raw.bitdepth) + { + for (uint32_t ii = 0, num = width * height; ii < num; ++ii) + { + const uint16_t* rgba = (uint16_t*)data + ii * 2; + bool has_alpha = rgba[1] < UINT16_MAX; + if (has_alpha) + { + output->m_hasAlpha = has_alpha; + break; + } + } + } + break; + + case LCT_RGBA: + if (8 == state.info_raw.bitdepth) + { + for (uint32_t ii = 0, num = width * height; ii < num; ++ii) + { + const uint8_t* dst = (uint8_t*)output->m_data + ii * 4; + bool has_alpha = dst[3] < UINT8_MAX; + if (has_alpha) + { + output->m_hasAlpha = has_alpha; + break; + } + } + } + else if (16 == state.info_raw.bitdepth) + { + for (uint32_t ii = 0, num = width * height; ii < num; ++ii) + { + const uint16_t* dst = (uint16_t*)output->m_data + ii * 4; + bool has_alpha = dst[3] < UINT16_MAX; + if (has_alpha) + { + output->m_hasAlpha = has_alpha; + break; + } + } + } + break; + + case LCT_PALETTE: + output->m_hasAlpha = lodepng_has_palette_alpha(&state.info_raw); + break; + + case LCT_MAX_OCTET_VALUE: + break; } } else @@ -331,6 +445,8 @@ namespace bimg uint32_t width = 0; uint32_t height = 0; + bool hasAlpha = false; + uint8_t* data = NULL; const char* err = NULL; EXRHeader exrHeader; @@ -375,7 +491,6 @@ namespace bimg if (UINT8_MAX != idxR) { const bool asFloat = exrHeader.pixel_types[idxR] == TINYEXR_PIXELTYPE_FLOAT; - uint32_t srcBpp = 32; uint32_t dstBpp = asFloat ? 32 : 16; format = asFloat ? TextureFormat::R32F : TextureFormat::R16F; uint32_t stepR = 1; @@ -385,7 +500,6 @@ namespace bimg if (UINT8_MAX != idxG) { - srcBpp += 32; dstBpp = asFloat ? 64 : 32; format = asFloat ? TextureFormat::RG32F : TextureFormat::RG16F; stepG = 1; @@ -393,7 +507,6 @@ namespace bimg if (UINT8_MAX != idxB) { - srcBpp += 32; dstBpp = asFloat ? 128 : 64; format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F; stepB = 1; @@ -401,7 +514,6 @@ namespace bimg if (UINT8_MAX != idxA) { - srcBpp += 32; dstBpp = asFloat ? 128 : 64; format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F; stepA = 1; @@ -431,6 +543,8 @@ namespace bimg }; bx::memCopy(&data[ii * bytesPerPixel], rgba, bytesPerPixel); + hasAlpha |= (hasAlpha || rgba[3] < 1.0f); + srcR += stepR; srcG += stepG; srcB += stepB; @@ -457,6 +571,8 @@ namespace bimg }; bx::memCopy(&data[ii * bytesPerPixel], rgba, bytesPerPixel); + hasAlpha |= (hasAlpha || rgba[3] < UINT16_MAX); + srcR += stepR; srcG += stepG; srcB += stepB; @@ -512,8 +628,10 @@ namespace bimg , data ); BX_FREE(_allocator, data); + output->m_hasAlpha = hasAlpha; } + return output; } @@ -647,7 +765,10 @@ namespace bimg for (uint32_t ii = 0; err.isOk() && ii < numEntries; ++ii) { - // https://sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html + // Reference(s): + // - EXIF Tags + // https://web.archive.org/web/20190218005249/https://sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html + // uint16_t tag; bx::readHE(&reader, tag, littleEndian, &err); diff --git a/3rdparty/bimg/src/image_encode.cpp b/3rdparty/bimg/src/image_encode.cpp index 09586fe5105..e006940a3a8 100644 --- a/3rdparty/bimg/src/image_encode.cpp +++ b/3rdparty/bimg/src/image_encode.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include <bimg/encode.h> @@ -12,7 +12,7 @@ #include <nvtt/nvtt.h> #include <pvrtc/PvrTcEncoder.h> #include <edtaa3/edtaa3func.h> -#include <astc/astc_lib.h> +#include <astcenc.h> BX_PRAGMA_DIAGNOSTIC_PUSH(); BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // warning C4100: 'alloc_context': unreferenced formal parameter @@ -30,19 +30,29 @@ namespace bimg { static uint32_t s_squishQuality[] = { + // Standard + squish::kColourClusterFit, // Default + squish::kColourIterativeClusterFit, // Highest + squish::kColourRangeFit, // Fastest + // Normal map squish::kColourClusterFit, // Default squish::kColourIterativeClusterFit, // Highest squish::kColourRangeFit, // Fastest }; BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_squishQuality) ); - static const ASTC_COMPRESS_MODE s_astcQuality[] = - { - ASTC_COMPRESS_MEDIUM, // Default - ASTC_COMPRESS_THOROUGH, // Highest - ASTC_COMPRESS_FAST, // Fastest - }; - BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_astcQuality)); + static const float s_astcQuality[] = + { + // Standard + ASTCENC_PRE_MEDIUM, // Default + ASTCENC_PRE_THOROUGH, // Highest + ASTCENC_PRE_FAST, // Fastest + // Normal map + ASTCENC_PRE_MEDIUM, // Default + ASTCENC_PRE_THOROUGH, // Highest + ASTCENC_PRE_FAST, // Fastest + }; + BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_astcQuality) ); void imageEncodeFromRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _format, Quality::Enum _quality, bx::Error* _err) { @@ -132,18 +142,103 @@ namespace bimg break; case TextureFormat::ASTC4x4: + case TextureFormat::ASTC5x4: case TextureFormat::ASTC5x5: + case TextureFormat::ASTC6x5: case TextureFormat::ASTC6x6: case TextureFormat::ASTC8x5: case TextureFormat::ASTC8x6: + case TextureFormat::ASTC8x8: case TextureFormat::ASTC10x5: + case TextureFormat::ASTC10x6: + case TextureFormat::ASTC10x8: + case TextureFormat::ASTC10x10: + case TextureFormat::ASTC12x10: + case TextureFormat::ASTC12x12: { - const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_format); + const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_format); + + astcenc_config config{}; + + uint32_t astcFlags = ASTCENC_FLG_SELF_DECOMPRESS_ONLY; + + if (Quality::NormalMapDefault <= _quality) + { + astcFlags |= ASTCENC_FLG_MAP_NORMAL; + } + + astcenc_error status = astcenc_config_init( + ASTCENC_PRF_LDR + , astcBlockInfo.blockWidth + , astcBlockInfo.blockHeight + , 1 + , s_astcQuality[_quality] + , astcFlags + , &config + ); + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in config init %s", astcenc_get_error_string(status) ); + BX_ERROR_SET(_err, BIMG_ERROR, "Unable to initialize astc config!"); + break; + } + + astcenc_context* context; + status = astcenc_context_alloc(&config, 1, &context); + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in context alloc %s", astcenc_get_error_string(status) ); + BX_ERROR_SET(_err, BIMG_ERROR, "Unable to alloc astc context!"); + break; + } + + astcenc_image image{}; + image.dim_x = _width; + image.dim_y = _height; + image.dim_z = 1; + image.data_type = ASTCENC_TYPE_U8; + image.data = (void**)&src; + + const size_t blockCountX = (_width + astcBlockInfo.blockWidth - 1) / astcBlockInfo.blockWidth; + const size_t blockCountY = (_height + astcBlockInfo.blockHeight - 1) / astcBlockInfo.blockHeight; + const size_t compLen = blockCountX * blockCountY * 16; - ASTC_COMPRESS_MODE compress_mode = s_astcQuality[_quality]; - ASTC_DECODE_MODE decode_mode = ASTC_DECODE_LDR_LINEAR; + if (Quality::NormalMapDefault <= _quality) + { + static const astcenc_swizzle swizzle + { //0001/rrrg swizzle corresponds to ASTC_ENC_NORMAL_RA + ASTCENC_SWZ_R, + ASTCENC_SWZ_R, + ASTCENC_SWZ_R, + ASTCENC_SWZ_G, + }; + + status = astcenc_compress_image(context, &image, &swizzle, dst, compLen, 0); + } + else + { + static const astcenc_swizzle swizzle + { //0123/rgba swizzle corresponds to ASTC_RGBA + ASTCENC_SWZ_R, + ASTCENC_SWZ_G, + ASTCENC_SWZ_B, + ASTCENC_SWZ_A, + }; + + status = astcenc_compress_image(context, &image, &swizzle, dst, compLen, 0); + } + + if (status != ASTCENC_SUCCESS) + { + BX_TRACE("astc error in compress image %s", astcenc_get_error_string(status) ); + BX_ERROR_SET(_err, BIMG_ERROR, "Unable to compress astc image!"); + astcenc_context_free(context); + break; + } - astc_compress(_width, _height, src, ASTC_RGBA, srcPitch, astcBlockInfo.blockWidth, astcBlockInfo.blockHeight, compress_mode, decode_mode, dst); + astcenc_context_free(context); } break; @@ -152,7 +247,7 @@ namespace bimg break; case TextureFormat::RGBA8: - bx::memCopy(_dst, _src, srcPitch, _height, srcPitch, dstPitch); + bx::memCopy(_dst, dstPitch, _src, srcPitch, srcPitch, _height); break; default: @@ -235,11 +330,19 @@ namespace bimg case TextureFormat::PTC14: case TextureFormat::PTC14A: case TextureFormat::ASTC4x4: + case TextureFormat::ASTC5x4: case TextureFormat::ASTC5x5: + case TextureFormat::ASTC6x5: case TextureFormat::ASTC6x6: case TextureFormat::ASTC8x5: case TextureFormat::ASTC8x6: + case TextureFormat::ASTC8x8: case TextureFormat::ASTC10x5: + case TextureFormat::ASTC10x6: + case TextureFormat::ASTC10x8: + case TextureFormat::ASTC10x10: + case TextureFormat::ASTC12x10: + case TextureFormat::ASTC12x12: { uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*4); imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat); @@ -490,7 +593,7 @@ namespace bimg return rgba[3]; } - float imageAlphaTestCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, float _alphaRef, float _scale) + float imageAlphaTestCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, float _alphaRef, float _scale, uint32_t _upscale) { UnpackFn unpack = getUnpack(_format); if (NULL == unpack) @@ -501,7 +604,8 @@ namespace bimg float coverage = 0.0f; const uint8_t* src = (const uint8_t*)_src; const uint32_t xstep = getBitsPerPixel(_format) / 8; - const float numSamples = 8.0f; + const uint32_t numSamples = _upscale; + const float sampleStep = 1.0f / numSamples; for (uint32_t yy = 0, ystep = _srcPitch; yy < _height-1; ++yy, src += ystep) { @@ -513,9 +617,9 @@ namespace bimg float alpha01 = _scale * getAlpha(unpack, data+ystep); float alpha11 = _scale * getAlpha(unpack, data+ystep+xstep); - for (float fy = 0.5f/numSamples; fy < 1.0f; fy += 1.0f) + for (float fy = 0.0f; fy < 1.0f; fy += sampleStep) { - for (float fx = 0.5f/numSamples; fx < 1.0f; fx += 1.0f) + for (float fx = 0.0f; fx < 1.0f; fx += sampleStep) { float alpha = 0.0f + alpha00 * (1.0f - fx) * (1.0f - fy) @@ -536,7 +640,7 @@ namespace bimg return coverage / float(_width*_height*numSamples*numSamples); } - void imageScaleAlphaToCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, void* _src, float _desiredCoverage, float _alphaRef) + void imageScaleAlphaToCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, void* _src, float _desiredCoverage, float _alphaRef, uint32_t _upscale) { PackFn pack = getPack(_format); UnpackFn unpack = getUnpack(_format); @@ -550,7 +654,7 @@ namespace bimg float max = 4.0f; float scale = 1.0f; - for (uint32_t ii = 0; ii < 8; ++ii) + for (uint32_t ii = 0; ii < 10; ++ii) { float coverage = imageAlphaTestCoverage( _format @@ -560,6 +664,7 @@ namespace bimg , _src , _alphaRef , scale + , _upscale ); if (coverage < _desiredCoverage) diff --git a/3rdparty/bimg/src/image_gnf.cpp b/3rdparty/bimg/src/image_gnf.cpp index 77c6d0a6332..7d7f0aa190e 100644 --- a/3rdparty/bimg/src/image_gnf.cpp +++ b/3rdparty/bimg/src/image_gnf.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ #include "bimg_p.h" @@ -21,7 +21,7 @@ namespace bimg bx::MemoryReader reader(_src, _size); uint32_t magic; - bx::read(&reader, magic); + bx::read(&reader, magic, bx::ErrorIgnore{}); ImageContainer imageContainer; if (BIMG_CHUNK_MAGIC_GNF != magic diff --git a/3rdparty/bimg/tools/texturec/texturec.cpp b/3rdparty/bimg/tools/texturec/texturec.cpp index b9ea4a2291f..f53bbd50917 100644 --- a/3rdparty/bimg/tools/texturec/texturec.cpp +++ b/3rdparty/bimg/tools/texturec/texturec.cpp @@ -1,9 +1,8 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause + * Copyright 2011-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE */ -#include <stdio.h> #include <bx/allocator.h> #include <bx/readerwriter.h> #include <bx/endian.h> @@ -13,7 +12,7 @@ #include <bimg/encode.h> #if 0 -# define DBG(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__) +# define DBG(_format, ...) bx::printf("" _format "\n", ##__VA_ARGS__) #else # define DBG(...) BX_NOOP() #endif // DEBUG @@ -90,12 +89,8 @@ void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32 const float* rgba = (const float*)&src[0]; for (uint32_t xx = 0; xx < _width; ++xx, rgba += 4, dst += 16) { - float xyz[3]; - - xyz[0] = rgba[0]; - xyz[1] = rgba[1]; - xyz[2] = rgba[2]; - bx::vec3Norm( (float*)dst, xyz); + const bx::Vec3 xyz = bx::load<bx::Vec3>(rgba); + bx::store(dst, bx::normalize(xyz) ); } } } @@ -161,12 +156,12 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat); const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat); const uint32_t blockWidth = outputBlockInfo.blockWidth; - const uint32_t blockHeight = outputBlockInfo.blockHeight; - const uint32_t minBlockX = outputBlockInfo.minBlockX; - const uint32_t minBlockY = outputBlockInfo.minBlockY; - uint32_t outputWidth = bx::max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth); - uint32_t outputHeight = bx::max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight); - uint32_t outputDepth = input->m_depth; + const uint32_t blockHeight = outputBlockInfo.blockHeight; + const uint32_t minBlockX = outputBlockInfo.minBlockX; + const uint32_t minBlockY = outputBlockInfo.minBlockY; + uint32_t outputWidth = bx::max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth); + uint32_t outputHeight = bx::max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight); + uint32_t outputDepth = input->m_depth; if (_options.mips && _options.mipSkip != 0) @@ -200,20 +195,19 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData } else if (_options.strip) { - if (outputDepth == 1 - && outputWidth/6 == outputHeight) + if (outputDepth == 1 + && ( (outputWidth == outputHeight*6) || (outputWidth*6 == outputHeight) ) ) { - if (outputWidth/6 > _options.maxSize) - { - outputWidth = _options.maxSize*6; - outputHeight = _options.maxSize; - } + const bool horizontal = outputWidth == outputHeight*6; + + outputWidth = bx::min(outputWidth, horizontal ? _options.maxSize*6 : _options.maxSize); + outputHeight = bx::min(outputHeight, horizontal ? _options.maxSize : _options.maxSize*6); } else { bimg::imageFree(input); - BX_ERROR_SET(_err, TEXTRUREC_ERROR, "Input image format is not horizontal strip."); + BX_ERROR_SET(_err, TEXTRUREC_ERROR, "Input image format is not horizontal or vertical strip."); return NULL; } } @@ -330,7 +324,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData bimg::ImageContainer* dst; - if (outputWidth/2 == outputHeight) + if (outputWidth == outputHeight*2) { dst = bimg::imageCubemapFromLatLongRgba32F(_allocator, *src, true, _err); bimg::imageFree(src); @@ -454,6 +448,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , rgba ); + bimg::Quality::Enum nmapQuality = bimg::Quality::Enum(_options.quality + bimg::Quality::NormalMapDefault); bimg::imageEncodeFromRgba32f(_allocator , dstData , rgbaDst @@ -461,7 +456,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_height , dstMip.m_depth , outputFormat - , _options.quality + , nmapQuality , _err ); @@ -493,7 +488,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_height , dstMip.m_depth , outputFormat - , _options.quality + , nmapQuality , _err ); } @@ -624,10 +619,10 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , bimg::TextureFormat::R8 ); temp = BX_ALLOC(_allocator, size); - uint8_t* rgba = (uint8_t*)temp; + uint8_t* r8 = (uint8_t*)temp; bimg::imageDecodeToR8(_allocator - , rgba + , r8 , mip.m_data , mip.m_width , mip.m_height @@ -644,8 +639,85 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , mip.m_width , mip.m_height , mip.m_width - , rgba + , r8 + ); + + if (_options.mips) { + const float alphaRef = 0.5f; + float coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::A8 + , mip.m_width + , mip.m_height + , mip.m_width + , r8 + , alphaRef ); + + size = bimg::imageGetSize( + NULL + , uint16_t(dstMip.m_width) + , uint16_t(dstMip.m_height) + , uint16_t(dstMip.m_depth) + , false + , false + , 1 + , bimg::TextureFormat::RGBA8 + ); + void* rgbaTemp = BX_ALLOC(_allocator, size); + uint8_t* rgba = (uint8_t*)rgbaTemp; + + bimg::imageDecodeToRgba8( + _allocator + , rgba + , dstMip.m_data + , dstMip.m_width + , dstMip.m_height + , dstMip.m_width * 4 + , bimg::TextureFormat::A8 + ); + + for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod) { + bimg::imageRgba8Downsample2x2(rgba + , dstMip.m_width + , dstMip.m_height + , dstMip.m_depth + , dstMip.m_width * 4 + , bx::strideAlign(dstMip.m_width / 2, blockWidth) * 4 + , rgba + ); + + // For each mip, upscale to original size, + // scale image alpha to get same coverage as mip0 + uint32_t upsample = 1 << lod; + uint32_t destWidth = dstMip.m_width / 2; + uint32_t destHeight = dstMip.m_height / 2; + bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8 + , destWidth + , destHeight + , destWidth * 4 + , rgba + , coverage + , alphaRef + , upsample + ); + + bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip); + dstData = const_cast<uint8_t*>(dstMip.m_data); + + bimg::imageEncodeFromRgba8( + _allocator + , dstData + , rgba + , dstMip.m_width + , dstMip.m_height + , dstMip.m_depth + , bimg::TextureFormat::A8 + , _options.quality + , _err + ); + } + + BX_FREE(_allocator, rgbaTemp); + } } // RGBA8 else @@ -787,7 +859,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , uint16_t(mip.m_height) ); - printf("%f\n", result); + bx::printf("%f\n", result); BX_FREE(_allocator, ref); } @@ -814,7 +886,7 @@ void help(const char* _error = NULL, bool _showHelp = true) { if (NULL != _error) { - fprintf(stderr, "Error:\n%s\n\n", _error); + bx::printf("Error:\n%s\n\n", _error); if (!_showHelp) { @@ -822,17 +894,17 @@ void help(const char* _error = NULL, bool _showHelp = true) } } - fprintf(stderr - , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" - "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n" - "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n" + bx::printf( + "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" + "Copyright 2011-2022 Branimir Karadzic. All rights reserved.\n" + "License: https://github.com/bkaradzic/bimg/blob/master/LICENSE\n\n" , BIMG_TEXTUREC_VERSION_MAJOR , BIMG_TEXTUREC_VERSION_MINOR , BIMG_API_VERSION ); - fprintf(stderr - , "Usage: texturec -f <in> -o <out> [-t <texture format>]\n" + bx::printf( + "Usage: texturec -f <in> -o <out> [-t <texture format>]\n" "\n" "Supported file formats:\n" @@ -846,7 +918,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " *.png (input, output) Portable Network Graphics.\n" " *.psd (input) Photoshop Document.\n" " *.pvr (input) PowerVR.\n" - " *.tga (input) Targa.\n" + " *.tga (input) Truevision TGA.\n" "\n" "Options:\n" @@ -858,19 +930,19 @@ void help(const char* _error = NULL, bool _showHelp = true) " -q <quality> Encoding quality (default, fastest, highest).\n" " -m, --mips Generate mip-maps.\n" " --mipskip <N> Skip <N> number of mips.\n" - " -n, --normalmap Input texture is normal map.\n" + " -n, --normalmap Input texture is normal map. (Implies --linear)\n" " --equirect Input texture is equirectangular projection of cubemap.\n" - " --strip Input texture is horizontal strip of cubemap.\n" + " --strip Input texture is horizontal or vertical strip of cubemap.\n" " --sdf Compute SDF texture.\n" " --ref <alpha> Alpha reference value.\n" " --iqa Image Quality Assessment\n" " --pma Premultiply alpha into RGB channel.\n" " --linear Input and output texture is linear color space (gamma correction won't be applied).\n" " --max <max size> Maximum width/height (image will be scaled down and\n" - " aspect ratio will be preserved.\n" + " aspect ratio will be preserved)\n" " --radiance <model> Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n" " --as <extension> Save as.\n" - " --formats List all supported formats.\n" + " --formats List all supported formats.\n" " --validate *DEBUG* Validate that output image produced matches after loading.\n" "\n" @@ -878,17 +950,19 @@ void help(const char* _error = NULL, bool _showHelp = true) ); } -void help(const char* _str, const bx::Error& _err) +void help(const bx::StringView _str, const bx::Error& _err) { std::string str; - if (_str != NULL) + if (!_str.isEmpty() ) { - str.append(_str); - str.append(" "); + str.append(_str.getPtr(), _str.getTerm() - _str.getPtr() ); + str.append(": "); } const bx::StringView& sv = _err.getMessage(); + str.append("'"); str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() ); + str.append("'"); help(str.c_str(), false); } @@ -923,8 +997,8 @@ int main(int _argc, const char* _argv[]) if (cmdLine.hasArg('v', "version") ) { - fprintf(stderr - , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" + bx::printf( + "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" , BIMG_TEXTUREC_VERSION_MAJOR , BIMG_TEXTUREC_VERSION_MINOR , BIMG_API_VERSION @@ -940,20 +1014,26 @@ int main(int _argc, const char* _argv[]) if (cmdLine.hasArg("formats")) { - printf("Uncompressed formats:\n"); + bx::printf("Uncompressed formats:\n"); - for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - printf("Compressed formats:\n"); + bx::printf("Compressed formats:\n"); - for (int format = 0; format < bimg::TextureFormat::Unknown; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = 0; format < bimg::TextureFormat::Unknown; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - return bx::kExitSuccess; + return bx::kExitSuccess; } const char* inputFileName = cmdLine.findOption('f'); @@ -1010,6 +1090,12 @@ int main(int _argc, const char* _argv[]) return bx::kExitFailure; } + // Normal maps are always linear + if (options.normalMap) + { + options.linear = true; + } + const char* maxSize = cmdLine.findOption("max"); if (NULL != maxSize) { @@ -1134,6 +1220,8 @@ int main(int _argc, const char* _argv[]) if (NULL != output) { + output->m_srgb = !options.linear; + bx::FileWriter writer; if (bx::open(&writer, outputFileName, false, &err) ) { @@ -1198,7 +1286,7 @@ int main(int _argc, const char* _argv[]) if (!err.isOk() ) { - help(NULL, err); + help("", err); return bx::kExitFailure; } } @@ -1289,7 +1377,7 @@ int main(int _argc, const char* _argv[]) } else { - help(NULL, err); + help("Failed to create output", err); return bx::kExitFailure; } |