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