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