1cc1dc7a3Sopenharmony_ci// SPDX-License-Identifier: Apache-2.0 2cc1dc7a3Sopenharmony_ci// ---------------------------------------------------------------------------- 3cc1dc7a3Sopenharmony_ci// Copyright 2021 Arm Limited 4cc1dc7a3Sopenharmony_ci// 5cc1dc7a3Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); you may not 6cc1dc7a3Sopenharmony_ci// use this file except in compliance with the License. You may obtain a copy 7cc1dc7a3Sopenharmony_ci// of the License at: 8cc1dc7a3Sopenharmony_ci// 9cc1dc7a3Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 10cc1dc7a3Sopenharmony_ci// 11cc1dc7a3Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 12cc1dc7a3Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13cc1dc7a3Sopenharmony_ci// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14cc1dc7a3Sopenharmony_ci// License for the specific language governing permissions and limitations 15cc1dc7a3Sopenharmony_ci// under the License. 16cc1dc7a3Sopenharmony_ci// ---------------------------------------------------------------------------- 17cc1dc7a3Sopenharmony_ci 18cc1dc7a3Sopenharmony_ci// This is a utility tool to encode HDR into RGBM, or decode RGBM into HDR. 19cc1dc7a3Sopenharmony_ci 20cc1dc7a3Sopenharmony_ci#include <stdint.h> 21cc1dc7a3Sopenharmony_ci#include <stdio.h> 22cc1dc7a3Sopenharmony_ci#include <stdlib.h> 23cc1dc7a3Sopenharmony_ci 24cc1dc7a3Sopenharmony_ci#include "astcenc_mathlib.h" 25cc1dc7a3Sopenharmony_ci 26cc1dc7a3Sopenharmony_ci#define STB_IMAGE_IMPLEMENTATION 27cc1dc7a3Sopenharmony_ci#include "stb_image.h" 28cc1dc7a3Sopenharmony_ci 29cc1dc7a3Sopenharmony_ci#define STB_IMAGE_WRITE_IMPLEMENTATION 30cc1dc7a3Sopenharmony_ci#include "stb_image_write.h" 31cc1dc7a3Sopenharmony_ci 32cc1dc7a3Sopenharmony_ci#define MODE_ENCODE 0 33cc1dc7a3Sopenharmony_ci#define MODE_DECODE 1 34cc1dc7a3Sopenharmony_ci 35cc1dc7a3Sopenharmony_ciint main(int argc, char **argv) 36cc1dc7a3Sopenharmony_ci{ 37cc1dc7a3Sopenharmony_ci // Parse command line 38cc1dc7a3Sopenharmony_ci if (argc != 6) 39cc1dc7a3Sopenharmony_ci { 40cc1dc7a3Sopenharmony_ci printf("Usage: astc_rgbm_codec [-ch|-dh] <M> <low_clamp> <source> <dest>\n"); 41cc1dc7a3Sopenharmony_ci exit(1); 42cc1dc7a3Sopenharmony_ci } 43cc1dc7a3Sopenharmony_ci 44cc1dc7a3Sopenharmony_ci int opmode; 45cc1dc7a3Sopenharmony_ci if (strcmp(argv[1], "-ch") == 0) 46cc1dc7a3Sopenharmony_ci { 47cc1dc7a3Sopenharmony_ci opmode = MODE_ENCODE; 48cc1dc7a3Sopenharmony_ci } 49cc1dc7a3Sopenharmony_ci else if (strcmp(argv[1], "-dh") == 0) 50cc1dc7a3Sopenharmony_ci { 51cc1dc7a3Sopenharmony_ci opmode = MODE_DECODE; 52cc1dc7a3Sopenharmony_ci } 53cc1dc7a3Sopenharmony_ci else 54cc1dc7a3Sopenharmony_ci { 55cc1dc7a3Sopenharmony_ci printf("ERROR: Bad operation mode\n"); 56cc1dc7a3Sopenharmony_ci exit(1); 57cc1dc7a3Sopenharmony_ci } 58cc1dc7a3Sopenharmony_ci 59cc1dc7a3Sopenharmony_ci float rgbm_multiplier = atof(argv[2]); 60cc1dc7a3Sopenharmony_ci float low_clamp = atof(argv[3]); 61cc1dc7a3Sopenharmony_ci 62cc1dc7a3Sopenharmony_ci const char* src_file = argv[4]; 63cc1dc7a3Sopenharmony_ci const char* dst_file = argv[5]; 64cc1dc7a3Sopenharmony_ci 65cc1dc7a3Sopenharmony_ci // Convert an HDR input file into an RGBM encoded LDR file 66cc1dc7a3Sopenharmony_ci if (opmode == MODE_ENCODE) 67cc1dc7a3Sopenharmony_ci { 68cc1dc7a3Sopenharmony_ci // Load the input image 69cc1dc7a3Sopenharmony_ci int dim_x; 70cc1dc7a3Sopenharmony_ci int dim_y; 71cc1dc7a3Sopenharmony_ci const float* data_in = stbi_loadf(src_file, &dim_x, &dim_y, nullptr, 4); 72cc1dc7a3Sopenharmony_ci if (!data_in) 73cc1dc7a3Sopenharmony_ci { 74cc1dc7a3Sopenharmony_ci printf("ERROR: Failed to load input image.\n"); 75cc1dc7a3Sopenharmony_ci exit(1); 76cc1dc7a3Sopenharmony_ci } 77cc1dc7a3Sopenharmony_ci 78cc1dc7a3Sopenharmony_ci // Allocate the output image 79cc1dc7a3Sopenharmony_ci uint8_t* data_out = (uint8_t*)malloc(4 * dim_y * dim_x); 80cc1dc7a3Sopenharmony_ci if (!data_out) 81cc1dc7a3Sopenharmony_ci { 82cc1dc7a3Sopenharmony_ci printf("ERROR: Failed to allow output image.\n"); 83cc1dc7a3Sopenharmony_ci exit(1); 84cc1dc7a3Sopenharmony_ci } 85cc1dc7a3Sopenharmony_ci 86cc1dc7a3Sopenharmony_ci // For each pixel apply RGBM encoding 87cc1dc7a3Sopenharmony_ci for (int y = 0; y < dim_y; y++) 88cc1dc7a3Sopenharmony_ci { 89cc1dc7a3Sopenharmony_ci const float* row_in = data_in + (4 * dim_x * y); 90cc1dc7a3Sopenharmony_ci uint8_t* row_out = data_out + (4 * dim_x * y); 91cc1dc7a3Sopenharmony_ci 92cc1dc7a3Sopenharmony_ci for (int x = 0; x < dim_x; x++) 93cc1dc7a3Sopenharmony_ci { 94cc1dc7a3Sopenharmony_ci const float* pixel_in = row_in + 4 * x; 95cc1dc7a3Sopenharmony_ci uint8_t* pixel_out = row_out + 4 * x; 96cc1dc7a3Sopenharmony_ci 97cc1dc7a3Sopenharmony_ci float r_in = pixel_in[0] / rgbm_multiplier; 98cc1dc7a3Sopenharmony_ci float g_in = pixel_in[1] / rgbm_multiplier; 99cc1dc7a3Sopenharmony_ci float b_in = pixel_in[2] / rgbm_multiplier; 100cc1dc7a3Sopenharmony_ci 101cc1dc7a3Sopenharmony_ci float max_rgb = astc::max(r_in, g_in, b_in); 102cc1dc7a3Sopenharmony_ci 103cc1dc7a3Sopenharmony_ci // Ensure we always round up to next largest M 104cc1dc7a3Sopenharmony_ci float m_scale = astc::min(1.0f, ceil(max_rgb * 255.0f) / 255.0f); 105cc1dc7a3Sopenharmony_ci 106cc1dc7a3Sopenharmony_ci // But keep well above zero to avoid clamps in the compressor 107cc1dc7a3Sopenharmony_ci m_scale = astc::max(m_scale, low_clamp / 255.0f); 108cc1dc7a3Sopenharmony_ci 109cc1dc7a3Sopenharmony_ci float r_scale = astc::min(1.0f, r_in / m_scale); 110cc1dc7a3Sopenharmony_ci float g_scale = astc::min(1.0f, g_in / m_scale); 111cc1dc7a3Sopenharmony_ci float b_scale = astc::min(1.0f, b_in / m_scale); 112cc1dc7a3Sopenharmony_ci 113cc1dc7a3Sopenharmony_ci pixel_out[0] = (uint8_t)(r_scale * 255.0f); 114cc1dc7a3Sopenharmony_ci pixel_out[1] = (uint8_t)(g_scale * 255.0f); 115cc1dc7a3Sopenharmony_ci pixel_out[2] = (uint8_t)(b_scale * 255.0f); 116cc1dc7a3Sopenharmony_ci pixel_out[3] = (uint8_t)(m_scale * 255.0f); 117cc1dc7a3Sopenharmony_ci } 118cc1dc7a3Sopenharmony_ci } 119cc1dc7a3Sopenharmony_ci 120cc1dc7a3Sopenharmony_ci // Write out the result 121cc1dc7a3Sopenharmony_ci stbi_write_png(dst_file, dim_x, dim_y, 4, data_out, 4 * dim_x); 122cc1dc7a3Sopenharmony_ci } 123cc1dc7a3Sopenharmony_ci // Convert an RGBM encoded LDR file into an HDR file 124cc1dc7a3Sopenharmony_ci else 125cc1dc7a3Sopenharmony_ci { 126cc1dc7a3Sopenharmony_ci // Load the input image 127cc1dc7a3Sopenharmony_ci int dim_x; 128cc1dc7a3Sopenharmony_ci int dim_y; 129cc1dc7a3Sopenharmony_ci const uint8_t* data_in = stbi_load(src_file, &dim_x, &dim_y, nullptr, 4); 130cc1dc7a3Sopenharmony_ci if (!data_in) 131cc1dc7a3Sopenharmony_ci { 132cc1dc7a3Sopenharmony_ci printf("ERROR: Failed to load input image.\n"); 133cc1dc7a3Sopenharmony_ci exit(1); 134cc1dc7a3Sopenharmony_ci } 135cc1dc7a3Sopenharmony_ci 136cc1dc7a3Sopenharmony_ci // Allocate the output image 137cc1dc7a3Sopenharmony_ci float* data_out = (float*)malloc(4 * dim_y * dim_x * sizeof(float)); 138cc1dc7a3Sopenharmony_ci if (!data_out) 139cc1dc7a3Sopenharmony_ci { 140cc1dc7a3Sopenharmony_ci printf("ERROR: Failed to allow output image.\n"); 141cc1dc7a3Sopenharmony_ci exit(1); 142cc1dc7a3Sopenharmony_ci } 143cc1dc7a3Sopenharmony_ci 144cc1dc7a3Sopenharmony_ci // For each pixel apply RGBM decoding 145cc1dc7a3Sopenharmony_ci for (int y = 0; y < dim_y; y++) 146cc1dc7a3Sopenharmony_ci { 147cc1dc7a3Sopenharmony_ci const uint8_t* row_in = data_in + (4 * dim_x * y); 148cc1dc7a3Sopenharmony_ci float* row_out = data_out + (4 * dim_x * y); 149cc1dc7a3Sopenharmony_ci 150cc1dc7a3Sopenharmony_ci for (int x = 0; x < dim_x; x++) 151cc1dc7a3Sopenharmony_ci { 152cc1dc7a3Sopenharmony_ci const uint8_t* pixel_in = row_in + 4 * x; 153cc1dc7a3Sopenharmony_ci float* pixel_out = row_out + 4 * x; 154cc1dc7a3Sopenharmony_ci 155cc1dc7a3Sopenharmony_ci float r_scale = ((float)pixel_in[0]) / 255.0f; 156cc1dc7a3Sopenharmony_ci float g_scale = ((float)pixel_in[1]) / 255.0f; 157cc1dc7a3Sopenharmony_ci float b_scale = ((float)pixel_in[2]) / 255.0f; 158cc1dc7a3Sopenharmony_ci 159cc1dc7a3Sopenharmony_ci float m_scale = ((float)pixel_in[3]) / 255.0f; 160cc1dc7a3Sopenharmony_ci 161cc1dc7a3Sopenharmony_ci pixel_out[0] = r_scale * (m_scale * rgbm_multiplier); 162cc1dc7a3Sopenharmony_ci pixel_out[1] = g_scale * (m_scale * rgbm_multiplier); 163cc1dc7a3Sopenharmony_ci pixel_out[2] = b_scale * (m_scale * rgbm_multiplier); 164cc1dc7a3Sopenharmony_ci pixel_out[3] = 1.0f; 165cc1dc7a3Sopenharmony_ci } 166cc1dc7a3Sopenharmony_ci } 167cc1dc7a3Sopenharmony_ci 168cc1dc7a3Sopenharmony_ci // Write out the result 169cc1dc7a3Sopenharmony_ci stbi_write_hdr(dst_file, dim_x, dim_y, 4, data_out); 170cc1dc7a3Sopenharmony_ci } 171cc1dc7a3Sopenharmony_ci 172cc1dc7a3Sopenharmony_ci return 0; 173cc1dc7a3Sopenharmony_ci} 174