Implementation taken from https://github.com/ARM-software/astc-encoder/blob/main/Utils/Example/astc_api_example.cpp
| 91 | |
| 92 | // Implementation taken from https://github.com/ARM-software/astc-encoder/blob/main/Utils/Example/astc_api_example.cpp |
| 93 | bool ASTCEncode(ASTCEncodeSettings* settings, uint8_t** out, uint32_t* out_size) |
| 94 | { |
| 95 | // For the purposes of this sample we hard-code the compressor settings |
| 96 | uint32_t thread_count = dmMath::Max(1, settings->m_NumThreads); |
| 97 | uint32_t block_x = 0; |
| 98 | uint32_t block_y = 0; |
| 99 | uint32_t block_z = 1; |
| 100 | astcenc_profile profile = ASTCENC_PRF_LDR; |
| 101 | |
| 102 | if (settings->m_QualityLevel < 0.0 || settings->m_QualityLevel > 100.0f) |
| 103 | { |
| 104 | dmLogError("Invalid quality level, range must be [0..100], but is %f", settings->m_QualityLevel); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (!ParseBlockSizes(settings->m_OutPixelFormat, &block_x, &block_y)) |
| 109 | { |
| 110 | dmLogError("Unable to parse block sizes from pixel format %d", settings->m_OutPixelFormat); |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | const astcenc_swizzle swizzle { |
| 115 | ASTCENC_SWZ_R, |
| 116 | ASTCENC_SWZ_G, |
| 117 | ASTCENC_SWZ_B, |
| 118 | ASTCENC_SWZ_A |
| 119 | }; |
| 120 | |
| 121 | astcenc_config config; |
| 122 | astcenc_error status = astcenc_config_init(profile, block_x, block_y, block_z, settings->m_QualityLevel, 0, &config); |
| 123 | if (status != ASTCENC_SUCCESS) |
| 124 | { |
| 125 | dmLogError("Codec config init failed: %s", astcenc_get_error_string(status)); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // Create a context based on the configuration |
| 130 | astcenc_context* context; |
| 131 | status = astcenc_context_alloc(&config, thread_count, &context); |
| 132 | if (status != ASTCENC_SUCCESS) |
| 133 | { |
| 134 | dmLogError("Codec context alloc failed: %s", astcenc_get_error_string(status)); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Compress the image |
| 139 | uint8_t* image_data = settings->m_Data; |
| 140 | astcenc_image image = {}; |
| 141 | image.dim_x = settings->m_Width; |
| 142 | image.dim_y = settings->m_Height; |
| 143 | image.dim_z = 1; |
| 144 | image.data_type = ASTCENC_TYPE_U8; |
| 145 | image.data = reinterpret_cast<void**>(&image_data); |
| 146 | |
| 147 | // Space needed for 16 bytes of output per compressed block |
| 148 | uint32_t comp_len = GetASTCCompressedDataSize(settings->m_Width, settings->m_Height, block_x, block_y); |
| 149 | uint8_t* comp_data = (uint8_t*)malloc(comp_len); |
| 150 |