Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the Chromium LICENSE file. | |
| 4 | |
| 5 #include "qcms.h" | |
| 6 #include "qcms_test_util.h" | |
| 7 | |
| 8 #include <math.h> | |
| 9 #include <stdio.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <time.h> | |
| 12 | |
| 13 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para' | |
| 14 #define MAX_FLOAT_ERROR 0.000001f | |
| 15 | |
| 16 static const float inverse65535 = (float) (1.0 / 65535.0); | |
| 17 | |
| 18 extern float clamp_float(float a); | |
| 19 | |
| 20 static int get_output_gamma_table(const char *profile_path, uint16_t **table, si ze_t *size, bool precache) | |
| 21 { | |
| 22 qcms_transform *transform; | |
| 23 qcms_profile *sRGB; | |
| 24 qcms_profile *target; | |
| 25 | |
| 26 target = qcms_profile_from_path(profile_path); | |
| 27 if (!target) { | |
| 28 fprintf(stderr, "Invalid input profile\n"); | |
| 29 return EXIT_FAILURE; | |
| 30 } | |
| 31 | |
| 32 if (precache) { | |
|
Noel Gordon
2016/04/21 06:01:53
Why is the precache relevant to this test? The cu
radu.velea
2016/04/21 07:13:45
Yes, but the same calculations are done during pre
| |
| 33 qcms_profile_precache_output_transform(target); | |
| 34 } | |
| 35 | |
| 36 sRGB = qcms_profile_sRGB(); | |
| 37 | |
| 38 transform = qcms_transform_create(sRGB, QCMS_DATA_RGBA_8, target, QCMS_DATA_ RGBA_8, QCMS_INTENT_DEFAULT); | |
| 39 if (!transform) { | |
| 40 fprintf(stderr, "Failed to create colour transform\n"); | |
| 41 qcms_profile_release(sRGB); | |
| 42 qcms_profile_release(target); | |
| 43 return EXIT_FAILURE; | |
| 44 } | |
| 45 | |
| 46 *size = qcms_transform_get_output_trc_rgba(transform, target, QCMS_TRC_USHOR T, NULL); | |
| 47 *table = malloc(*size * sizeof(uint16_t) * 4); | |
| 48 qcms_transform_get_output_trc_rgba(transform, target, QCMS_TRC_USHORT, *tabl e); | |
| 49 | |
| 50 qcms_transform_release(transform); | |
| 51 | |
| 52 qcms_profile_release(sRGB); | |
| 53 qcms_profile_release(target); | |
| 54 | |
| 55 return 0; | |
| 56 } | |
| 57 | |
| 58 static int get_input_gamma_table(const char *profile_path, uint16_t **table, siz e_t expected_size) | |
| 59 { | |
| 60 qcms_transform *transform; | |
| 61 qcms_profile *source; | |
| 62 qcms_profile *sRGB; | |
| 63 size_t size; | |
| 64 | |
| 65 source = qcms_profile_from_path(profile_path); | |
| 66 if (!source) { | |
| 67 fprintf(stderr, "Invalid input profile\n"); | |
| 68 return EXIT_FAILURE; | |
| 69 } | |
| 70 | |
| 71 sRGB = qcms_profile_sRGB(); | |
| 72 | |
| 73 transform = qcms_transform_create(source, QCMS_DATA_RGBA_8, sRGB, QCMS_DATA_ RGBA_8, QCMS_INTENT_DEFAULT); | |
| 74 if (!transform) { | |
| 75 fprintf(stderr, "Failed to create colour transform\n"); | |
| 76 qcms_profile_release(sRGB); | |
| 77 qcms_profile_release(source); | |
| 78 return EXIT_FAILURE; | |
| 79 } | |
| 80 | |
| 81 // For now the size of input gamma table for parametric curves is hardcoded to 256. | |
| 82 // See compute_curve_gamma_table_type_parametric for details. | |
| 83 // Future implementations might decide to return an arbitrary-sized table. | |
| 84 size = qcms_transform_get_input_trc_rgba(transform, source, QCMS_TRC_USHORT, NULL); | |
| 85 if (size != expected_size) { | |
| 86 fprintf(stderr, "Expected input table size (%zu) to match output size (% zu)\n", size, expected_size); | |
| 87 size = size > expected_size ? size : expected_size; | |
| 88 } | |
| 89 | |
| 90 *table = calloc(size, sizeof(uint16_t) * 4); | |
| 91 qcms_transform_get_input_trc_rgba(transform, source, QCMS_TRC_USHORT, *table ); | |
| 92 | |
| 93 qcms_transform_release(transform); | |
| 94 | |
| 95 qcms_profile_release(sRGB); | |
| 96 qcms_profile_release(source); | |
| 97 | |
| 98 return 0; | |
| 99 } | |
| 100 | |
| 101 static int qcms_test_output_trc(size_t width, | |
| 102 size_t height, | |
| 103 int iterations, | |
| 104 const char *in_path, | |
| 105 const char *out_path, | |
| 106 const int force_software) | |
| 107 { | |
| 108 int err = 0; | |
| 109 qcms_profile *profile; | |
| 110 uint16_t *gamma_table_out, *gamma_table_out_precache; | |
| 111 size_t gamma_table_size, gamma_table_size_precache; | |
| 112 int i; | |
| 113 | |
| 114 printf("Test qcms output gamma curve integrity with and without precached ta bles.\n"); | |
| 115 | |
| 116 if (!in_path) { | |
| 117 fprintf(stderr, "%s: please provide valid ICC profiles via -i option\n", __FUNCTION__); | |
| 118 return EXIT_FAILURE; | |
| 119 } | |
| 120 | |
| 121 // Create profiles and transforms, get table and then free resources to make sure none | |
| 122 // of the internal tables are initialized by previous calls. | |
| 123 gamma_table_out = NULL; | |
| 124 gamma_table_size = 0; | |
| 125 if (get_output_gamma_table(in_path, &gamma_table_out, &gamma_table_size, tru e) != 0) { | |
|
Noel Gordon
2016/04/21 06:01:53
true => precache, but the results are stored in ga
radu.velea
2016/04/21 07:13:45
See comment above.
| |
| 126 fprintf(stderr, "Unable to extract output gamma table\n"); | |
| 127 return EXIT_FAILURE; | |
| 128 } | |
| 129 | |
| 130 gamma_table_out_precache = NULL; | |
| 131 gamma_table_size_precache = 0; | |
| 132 if (get_output_gamma_table(in_path, &gamma_table_out_precache, &gamma_table_ size_precache, false) != 0) { | |
|
Noel Gordon
2016/04/21 06:01:53
... false => no pre-cache but the results are stor
radu.velea
2016/04/21 07:13:45
Same as above.
| |
| 133 fprintf(stderr, "Unable to extract precached output gamma table\n"); | |
| 134 return EXIT_FAILURE; | |
| 135 } | |
| 136 | |
| 137 // Check if precached and non-precached tables match in size and contents. | |
| 138 if (gamma_table_size != gamma_table_size_precache) { | |
|
Noel Gordon
2016/04/21 06:01:53
but as mentioned above, why do the precache vs non
radu.velea
2016/04/26 10:23:18
Done.
| |
| 139 fprintf(stderr, "Size mismatch between output table sizes.\n" | |
| 140 "Output gamma size = %zu, gamma size with precahce = %zu\n", gam ma_table_size, gamma_table_size_precache); | |
| 141 free(gamma_table_out); | |
| 142 free(gamma_table_out_precache); | |
| 143 return EXIT_FAILURE; | |
| 144 } | |
| 145 | |
| 146 printf("LUT size = %zu, %zu\n", gamma_table_size, gamma_table_size_precache) ; | |
| 147 | |
| 148 for (i = 0; i < 4 * gamma_table_size; ++i) { | |
| 149 err += abs(gamma_table_out[i] - gamma_table_out_precache[i]); | |
| 150 } | |
| 151 | |
| 152 if (err != 0) { | |
| 153 fprintf(stderr, "Gamma output total error = %d\n.Aborting.", err); | |
| 154 free(gamma_table_out); | |
| 155 free(gamma_table_out_precache); | |
| 156 return EXIT_FAILURE; | |
| 157 } | |
| 158 | |
| 159 profile = qcms_profile_from_path(in_path); | |
| 160 if (!profile) { | |
| 161 fprintf(stderr, "Invalid input profile\n"); | |
| 162 free(gamma_table_out); | |
| 163 free(gamma_table_out_precache); | |
| 164 return EXIT_FAILURE; | |
| 165 } | |
| 166 | |
| 167 // Check only for red curve for now. | |
| 168 if (profile->redTRC->type == PARAMETRIC_CURVE_TYPE) { | |
| 169 int type = - (profile->redTRC->count + 1); | |
| 170 FILE *gamma_file; | |
| 171 uint16_t *gamma_table_in = NULL; | |
| 172 uint16_t *p_table_out, *p_table_in; | |
| 173 double gamma_aprox_in = 0, gamma_aprox_out = 0; | |
| 174 double sum_of_sqares = 0; | |
| 175 char file_name[256] = {0,}; | |
| 176 | |
| 177 printf("Detected parametric curve type = %d\n", profile->redTRC->count); | |
| 178 | |
| 179 sprintf(file_name, "qcms-test-%ld-parametric-gamma-%s.csv", (long int)ti me(NULL), profile->description); | |
| 180 printf("Writing input and output gamma tables to %s\n", file_name); | |
| 181 | |
| 182 printf("gamma = %.6f, a = %.6f, b = %.6f, c = %.6f, d = %.6f, e = %.6f, f = %.6f\n", | |
| 183 profile->redTRC->parameter[0], profile->redTRC->parameter[1], pr ofile->redTRC->parameter[2], | |
| 184 profile->redTRC->parameter[3], profile->redTRC->parameter[4], pr ofile->redTRC->parameter[5], | |
| 185 profile->redTRC->parameter[6]); | |
| 186 | |
| 187 // Write output to stdout and tables into a csv file. | |
| 188 gamma_file = fopen(file_name, "w"); | |
| 189 fprintf(gamma_file, "Parametric gamma values for %s\n", profile->descrip tion); | |
| 190 fprintf(gamma_file, "gamma, a, b, c, d, e, f\n"); | |
| 191 fprintf(gamma_file, "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n", | |
| 192 profile->redTRC->parameter[0], profile->redTRC->parameter[1], pr ofile->redTRC->parameter[2], | |
| 193 profile->redTRC->parameter[3], profile->redTRC->parameter[4], pr ofile->redTRC->parameter[5], | |
| 194 profile->redTRC->parameter[6]); | |
| 195 | |
| 196 get_input_gamma_table(in_path, &gamma_table_in, gamma_table_size); | |
| 197 if (!gamma_table_in) { | |
| 198 fprintf(stderr, "Unable to compute input trc. Aborting\n"); | |
| 199 fclose(gamma_file); | |
| 200 qcms_profile_release(profile); | |
| 201 free(gamma_table_out); | |
| 202 free(gamma_table_out_precache); | |
| 203 return EXIT_FAILURE; | |
| 204 } | |
| 205 | |
| 206 fprintf(gamma_file, "\n\nInput gamma, Output gamma, LCMS Output gamma, O utput gamma error\n"); | |
| 207 | |
| 208 p_table_out = gamma_table_out; | |
| 209 p_table_in = gamma_table_in; | |
| 210 | |
| 211 for (i = 0; i < gamma_table_size; ++i) { | |
| 212 float p = i / (gamma_table_size * 1.0); | |
| 213 float reference_out = clamp_float(evaluate_parametric_curve(type, pr ofile->redTRC->parameter, p)); | |
| 214 float actual_out = *p_table_out * inverse65535; | |
| 215 float error_out = fabs(actual_out - reference_out); | |
| 216 float input = *p_table_in * inverse65535; | |
| 217 | |
|
Noel Gordon
2016/04/21 06:01:53
I don't think the gamma estimator adds to the test
radu.velea
2016/04/21 07:13:45
Ok.
| |
| 218 // Approximate input and output gamma based on http://www.brucelindb loom.com/index.html?Eqn_BestGamma.html | |
| 219 if (p > MAX_FLOAT_ERROR && input > MAX_FLOAT_ERROR && actual_out > M AX_FLOAT_ERROR) { | |
| 220 gamma_aprox_in += log(p) * log(input); | |
| 221 gamma_aprox_out += log(p) * log(actual_out); | |
| 222 sum_of_sqares += log(p) * log(p); | |
| 223 } | |
| 224 | |
| 225 fprintf(gamma_file, "%.6f, %.6f, %6f, %6f\n",input, actual_out, refe rence_out, error_out); | |
| 226 | |
| 227 p_table_out += 4; // Skip other channels. | |
| 228 p_table_in += 4; // Skip other channels. | |
| 229 } | |
| 230 | |
| 231 gamma_aprox_in /= sum_of_sqares; | |
| 232 gamma_aprox_out /= sum_of_sqares; | |
| 233 | |
| 234 printf("Computed gamma from LUTs:\nInput = %.6lf\nOutput = %.6lf\nAccura cy? = %.6lf\n", | |
| 235 gamma_aprox_in, gamma_aprox_out, gamma_aprox_in * gamma_aprox_ou t); | |
| 236 | |
| 237 free(gamma_table_in); | |
| 238 fclose(gamma_file); | |
| 239 } | |
| 240 | |
| 241 qcms_profile_release(profile); | |
| 242 | |
| 243 free(gamma_table_out); | |
| 244 free(gamma_table_out_precache); | |
| 245 | |
| 246 return err; | |
| 247 } | |
| 248 | |
| 249 struct qcms_test_case qcms_test_output_trc_info = { | |
| 250 "qcms_test_output_trc", | |
| 251 qcms_test_output_trc, | |
| 252 QCMS_TEST_DISABLED | |
| 253 }; | |
| OLD | NEW |