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// Overview 19cc1dc7a3Sopenharmony_ci// ======== 20cc1dc7a3Sopenharmony_ci// 21cc1dc7a3Sopenharmony_ci// This is a utility tool to automatically generate single tile test vectors 22cc1dc7a3Sopenharmony_ci// out of a larger test image. This tool takes three input images: 23cc1dc7a3Sopenharmony_ci// 24cc1dc7a3Sopenharmony_ci// - the uncompressed referenced, 25cc1dc7a3Sopenharmony_ci// - the known-good compressed reference, 26cc1dc7a3Sopenharmony_ci// - a new compressed image. 27cc1dc7a3Sopenharmony_ci// 28cc1dc7a3Sopenharmony_ci// The two compressed images are compared block-by-block, and if any block 29cc1dc7a3Sopenharmony_ci// differences are found the worst block is extracted from the uncompressed 30cc1dc7a3Sopenharmony_ci// reference and written back to disk as a single tile output image. 31cc1dc7a3Sopenharmony_ci// 32cc1dc7a3Sopenharmony_ci// Limitations 33cc1dc7a3Sopenharmony_ci// =========== 34cc1dc7a3Sopenharmony_ci// 35cc1dc7a3Sopenharmony_ci// This tool only currently supports 2D LDR images. 36cc1dc7a3Sopenharmony_ci// 37cc1dc7a3Sopenharmony_ci// Build 38cc1dc7a3Sopenharmony_ci// ===== 39cc1dc7a3Sopenharmony_ci// 40cc1dc7a3Sopenharmony_ci// g++ astc_test_autoextract_hdr.cpp -I../Source -o astc_test_autoextract_hdr 41cc1dc7a3Sopenharmony_ci 42cc1dc7a3Sopenharmony_ci#include <stdio.h> 43cc1dc7a3Sopenharmony_ci#include <stdlib.h> 44cc1dc7a3Sopenharmony_ci 45cc1dc7a3Sopenharmony_ci#define STB_IMAGE_IMPLEMENTATION 46cc1dc7a3Sopenharmony_ci#include "stb_image.h" 47cc1dc7a3Sopenharmony_ci 48cc1dc7a3Sopenharmony_ci#define STB_IMAGE_WRITE_IMPLEMENTATION 49cc1dc7a3Sopenharmony_ci#include "stb_image_write.h" 50cc1dc7a3Sopenharmony_ci 51cc1dc7a3Sopenharmony_ci/** 52cc1dc7a3Sopenharmony_ci * @brief Compute the array offset in a 2D image 53cc1dc7a3Sopenharmony_ci */ 54cc1dc7a3Sopenharmony_ciint pix(int x_pix, int y_idx, int x_idx, int chans, int p_idx) 55cc1dc7a3Sopenharmony_ci{ 56cc1dc7a3Sopenharmony_ci return ((y_idx * x_pix) + x_idx) * chans + p_idx; 57cc1dc7a3Sopenharmony_ci} 58cc1dc7a3Sopenharmony_ci 59cc1dc7a3Sopenharmony_ciint main(int argc, char **argv) 60cc1dc7a3Sopenharmony_ci{ 61cc1dc7a3Sopenharmony_ci 62cc1dc7a3Sopenharmony_ci // Parse command line 63cc1dc7a3Sopenharmony_ci if (argc < 6) 64cc1dc7a3Sopenharmony_ci { 65cc1dc7a3Sopenharmony_ci printf("Usage: astc_test_extract <blocksize> <ref> <good> <bad> <out>\n"); 66cc1dc7a3Sopenharmony_ci return 1; 67cc1dc7a3Sopenharmony_ci } 68cc1dc7a3Sopenharmony_ci 69cc1dc7a3Sopenharmony_ci int blockdim_x, blockdim_y; 70cc1dc7a3Sopenharmony_ci if (sscanf(argv[1], "%dx%d", &blockdim_x, &blockdim_y) < 2) 71cc1dc7a3Sopenharmony_ci { 72cc1dc7a3Sopenharmony_ci printf("blocksize must be of form WxH; e.g. 8x4\n"); 73cc1dc7a3Sopenharmony_ci return 1; 74cc1dc7a3Sopenharmony_ci } 75cc1dc7a3Sopenharmony_ci 76cc1dc7a3Sopenharmony_ci // Load the original reference image 77cc1dc7a3Sopenharmony_ci int ref_dim_x, ref_dim_y, ref_ncomp; 78cc1dc7a3Sopenharmony_ci float* data_ref = (float*)stbi_loadf(argv[2], &ref_dim_x, &ref_dim_y, &ref_ncomp, 4); 79cc1dc7a3Sopenharmony_ci if (!data_ref) 80cc1dc7a3Sopenharmony_ci { 81cc1dc7a3Sopenharmony_ci printf("Failed to load reference image.\n"); 82cc1dc7a3Sopenharmony_ci return 1; 83cc1dc7a3Sopenharmony_ci } 84cc1dc7a3Sopenharmony_ci 85cc1dc7a3Sopenharmony_ci // Load the good test image 86cc1dc7a3Sopenharmony_ci int good_dim_x, good_dim_y, good_ncomp; 87cc1dc7a3Sopenharmony_ci float* data_good = (float*)stbi_loadf(argv[3], &good_dim_x, &good_dim_y, &good_ncomp, 4); 88cc1dc7a3Sopenharmony_ci if (!data_good) 89cc1dc7a3Sopenharmony_ci { 90cc1dc7a3Sopenharmony_ci printf("Failed to load good test image.\n"); 91cc1dc7a3Sopenharmony_ci return 1; 92cc1dc7a3Sopenharmony_ci } 93cc1dc7a3Sopenharmony_ci 94cc1dc7a3Sopenharmony_ci // Load the bad test image 95cc1dc7a3Sopenharmony_ci int bad_dim_x, bad_dim_y, bad_ncomp; 96cc1dc7a3Sopenharmony_ci float* data_bad = (float*)stbi_loadf(argv[4], &bad_dim_x, &bad_dim_y, &bad_ncomp, 4); 97cc1dc7a3Sopenharmony_ci if (!data_bad) 98cc1dc7a3Sopenharmony_ci { 99cc1dc7a3Sopenharmony_ci printf("Failed to load bad test image.\n"); 100cc1dc7a3Sopenharmony_ci return 1; 101cc1dc7a3Sopenharmony_ci } 102cc1dc7a3Sopenharmony_ci 103cc1dc7a3Sopenharmony_ci if (ref_dim_x != good_dim_x || ref_dim_x != bad_dim_x || 104cc1dc7a3Sopenharmony_ci ref_dim_y != good_dim_y || ref_dim_y != bad_dim_y) 105cc1dc7a3Sopenharmony_ci { 106cc1dc7a3Sopenharmony_ci printf("Failed as images are different resolutions.\n"); 107cc1dc7a3Sopenharmony_ci return 1; 108cc1dc7a3Sopenharmony_ci } 109cc1dc7a3Sopenharmony_ci 110cc1dc7a3Sopenharmony_ci 111cc1dc7a3Sopenharmony_ci int x_blocks = (ref_dim_x + blockdim_x - 1) / blockdim_x; 112cc1dc7a3Sopenharmony_ci int y_blocks = (ref_dim_y + blockdim_y - 1) / blockdim_y; 113cc1dc7a3Sopenharmony_ci 114cc1dc7a3Sopenharmony_ci float* errorsums = (float*)malloc(x_blocks * y_blocks * 4); 115cc1dc7a3Sopenharmony_ci for (int i = 0; i < x_blocks * y_blocks; i++) 116cc1dc7a3Sopenharmony_ci { 117cc1dc7a3Sopenharmony_ci errorsums[i] = 0; 118cc1dc7a3Sopenharmony_ci } 119cc1dc7a3Sopenharmony_ci 120cc1dc7a3Sopenharmony_ci // Diff the two test images to find blocks that differ 121cc1dc7a3Sopenharmony_ci for (int y = 0; y < ref_dim_y; y++) 122cc1dc7a3Sopenharmony_ci { 123cc1dc7a3Sopenharmony_ci for (int x = 0; x < ref_dim_x; x++) 124cc1dc7a3Sopenharmony_ci { 125cc1dc7a3Sopenharmony_ci int x_block = x / blockdim_x; 126cc1dc7a3Sopenharmony_ci int y_block = y / blockdim_y; 127cc1dc7a3Sopenharmony_ci 128cc1dc7a3Sopenharmony_ci float r_gd = data_good[pix(ref_dim_x, y, x, 4, 0)]; 129cc1dc7a3Sopenharmony_ci float g_gd = data_good[pix(ref_dim_x, y, x, 4, 1)]; 130cc1dc7a3Sopenharmony_ci float b_gd = data_good[pix(ref_dim_x, y, x, 4, 2)]; 131cc1dc7a3Sopenharmony_ci float a_gd = data_good[pix(ref_dim_x, y, x, 4, 3)]; 132cc1dc7a3Sopenharmony_ci 133cc1dc7a3Sopenharmony_ci float r_bd = data_bad[pix(ref_dim_x, y, x, 4, 0)]; 134cc1dc7a3Sopenharmony_ci float g_bd = data_bad[pix(ref_dim_x, y, x, 4, 1)]; 135cc1dc7a3Sopenharmony_ci float b_bd = data_bad[pix(ref_dim_x, y, x, 4, 2)]; 136cc1dc7a3Sopenharmony_ci float a_bd = data_bad[pix(ref_dim_x, y, x, 4, 3)]; 137cc1dc7a3Sopenharmony_ci 138cc1dc7a3Sopenharmony_ci float r_diff = (r_gd - r_bd) * (r_gd - r_bd); 139cc1dc7a3Sopenharmony_ci float g_diff = (g_gd - g_bd) * (g_gd - g_bd); 140cc1dc7a3Sopenharmony_ci float b_diff = (b_gd - b_bd) * (b_gd - b_bd); 141cc1dc7a3Sopenharmony_ci float a_diff = (a_gd - a_bd) * (a_gd - a_bd); 142cc1dc7a3Sopenharmony_ci 143cc1dc7a3Sopenharmony_ci float diff = abs(r_diff) + abs(g_diff) + abs(b_diff) + abs(a_diff); 144cc1dc7a3Sopenharmony_ci errorsums[pix(x_blocks, y_block, x_block, 1, 0)] += diff; 145cc1dc7a3Sopenharmony_ci } 146cc1dc7a3Sopenharmony_ci } 147cc1dc7a3Sopenharmony_ci 148cc1dc7a3Sopenharmony_ci // Diff the two test images to find blocks that differ 149cc1dc7a3Sopenharmony_ci float worst_error = 0.0f; 150cc1dc7a3Sopenharmony_ci int worst_x_block = 0; 151cc1dc7a3Sopenharmony_ci int worst_y_block = 0; 152cc1dc7a3Sopenharmony_ci for (int y = 0; y < y_blocks; y++) 153cc1dc7a3Sopenharmony_ci { 154cc1dc7a3Sopenharmony_ci for (int x = 0; x < x_blocks; x++) 155cc1dc7a3Sopenharmony_ci { 156cc1dc7a3Sopenharmony_ci float error = errorsums[pix(x_blocks, y, x, 1, 0)]; 157cc1dc7a3Sopenharmony_ci if (error > worst_error) 158cc1dc7a3Sopenharmony_ci { 159cc1dc7a3Sopenharmony_ci worst_error = error; 160cc1dc7a3Sopenharmony_ci worst_x_block = x; 161cc1dc7a3Sopenharmony_ci worst_y_block = y; 162cc1dc7a3Sopenharmony_ci } 163cc1dc7a3Sopenharmony_ci } 164cc1dc7a3Sopenharmony_ci } 165cc1dc7a3Sopenharmony_ci 166cc1dc7a3Sopenharmony_ci if (worst_error == 0.0f) 167cc1dc7a3Sopenharmony_ci { 168cc1dc7a3Sopenharmony_ci printf("No block errors found\n"); 169cc1dc7a3Sopenharmony_ci } 170cc1dc7a3Sopenharmony_ci else 171cc1dc7a3Sopenharmony_ci { 172cc1dc7a3Sopenharmony_ci int start_y = worst_y_block * blockdim_y; 173cc1dc7a3Sopenharmony_ci int start_x = worst_x_block * blockdim_x; 174cc1dc7a3Sopenharmony_ci 175cc1dc7a3Sopenharmony_ci int end_y = (worst_y_block + 1) * blockdim_y; 176cc1dc7a3Sopenharmony_ci int end_x = (worst_x_block + 1) * blockdim_x; 177cc1dc7a3Sopenharmony_ci 178cc1dc7a3Sopenharmony_ci if (end_x > ref_dim_x) 179cc1dc7a3Sopenharmony_ci { 180cc1dc7a3Sopenharmony_ci end_x = ref_dim_x; 181cc1dc7a3Sopenharmony_ci } 182cc1dc7a3Sopenharmony_ci 183cc1dc7a3Sopenharmony_ci if (end_y > ref_dim_y) 184cc1dc7a3Sopenharmony_ci { 185cc1dc7a3Sopenharmony_ci end_y = ref_dim_y; 186cc1dc7a3Sopenharmony_ci } 187cc1dc7a3Sopenharmony_ci 188cc1dc7a3Sopenharmony_ci int outblk_x = end_x - start_x; 189cc1dc7a3Sopenharmony_ci int outblk_y = end_y - start_y; 190cc1dc7a3Sopenharmony_ci 191cc1dc7a3Sopenharmony_ci printf("Block errors found at ~(%u, %u) px\n", start_x, start_y); 192cc1dc7a3Sopenharmony_ci 193cc1dc7a3Sopenharmony_ci float* data_out = (float*)malloc(blockdim_x * blockdim_y * 4 * 4); 194cc1dc7a3Sopenharmony_ci for (int y = 0; y < outblk_y; y++) 195cc1dc7a3Sopenharmony_ci { 196cc1dc7a3Sopenharmony_ci for (int x = 0; x < outblk_x; x++) 197cc1dc7a3Sopenharmony_ci { 198cc1dc7a3Sopenharmony_ci data_out[(y * outblk_x * 4) + (x * 4) + 0] = data_ref[((start_y + y) * ref_dim_x * 4) + ((start_x + x) * 4) + 0]; 199cc1dc7a3Sopenharmony_ci data_out[(y * outblk_x * 4) + (x * 4) + 1] = data_ref[((start_y + y) * ref_dim_x * 4) + ((start_x + x) * 4) + 1]; 200cc1dc7a3Sopenharmony_ci data_out[(y * outblk_x * 4) + (x * 4) + 2] = data_ref[((start_y + y) * ref_dim_x * 4) + ((start_x + x) * 4) + 2]; 201cc1dc7a3Sopenharmony_ci data_out[(y * outblk_x * 4) + (x * 4) + 3] = data_ref[((start_y + y) * ref_dim_x * 4) + ((start_x + x) * 4) + 3]; 202cc1dc7a3Sopenharmony_ci } 203cc1dc7a3Sopenharmony_ci } 204cc1dc7a3Sopenharmony_ci 205cc1dc7a3Sopenharmony_ci // Write out the worst bad block (from original reference) 206cc1dc7a3Sopenharmony_ci stbi_write_hdr(argv[5], outblk_x, outblk_y, 4, data_out); 207cc1dc7a3Sopenharmony_ci 208cc1dc7a3Sopenharmony_ci free(data_out); 209cc1dc7a3Sopenharmony_ci } 210cc1dc7a3Sopenharmony_ci 211cc1dc7a3Sopenharmony_ci free(errorsums); 212cc1dc7a3Sopenharmony_ci stbi_image_free(data_ref); 213cc1dc7a3Sopenharmony_ci stbi_image_free(data_good); 214cc1dc7a3Sopenharmony_ci stbi_image_free(data_bad); 215cc1dc7a3Sopenharmony_ci return 0; 216cc1dc7a3Sopenharmony_ci} 217