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 size = qcms_transform_get_input_trc_rgba(transform, profile, QCMS_TRC_USHORT
, NULL); |
| 83 if (size != expected_size) { |
| 84 fprintf(stderr, "Expected input table size (%zu) to match output size (%
zu)\n", size, expected_size); |
| 85 size = size > expected_size ? size : expected_size; |
| 86 } |
| 87 |
| 88 *table = calloc(size, sizeof(uint16_t) * 4); |
| 89 qcms_transform_get_input_trc_rgba(transform, profile, QCMS_TRC_USHORT, *tabl
e); |
| 90 |
| 91 qcms_transform_release(transform); |
| 92 |
| 93 RELEASE_PROFILES: |
| 94 qcms_profile_release(sRGB); |
| 95 qcms_profile_release(profile); |
| 96 |
| 97 return ret; |
| 98 } |
| 99 |
| 100 static int qcms_test_output_trc(size_t width, |
| 101 size_t height, |
| 102 int iterations, |
| 103 const char *in_path, |
| 104 const char *out_path, |
| 105 const int force_software) |
| 106 { |
| 107 int err = 0; |
| 108 qcms_profile *profile; |
| 109 uint16_t *gamma_table_out, *gamma_table_out_precache; |
| 110 uint16_t *gamma_table_in; |
| 111 size_t gamma_table_size, gamma_table_size_precache; |
| 112 char file_name[256] = {0,}; |
| 113 int i; |
| 114 |
| 115 printf("Test qcms output gamma curve integrity with and without precached ta
bles.\n"); |
| 116 |
| 117 if (!in_path) { |
| 118 fprintf(stderr, "%s: please provide valid ICC profiles via -i option\n",
__FUNCTION__); |
| 119 return EXIT_FAILURE; |
| 120 } |
| 121 |
| 122 gamma_table_out = NULL; |
| 123 gamma_table_size = 0; |
| 124 if (get_output_gamma_table(in_path, &gamma_table_out, &gamma_table_size, tru
e) != 0) { |
| 125 fprintf(stderr, "Unable to extract output gamma table\n"); |
| 126 return EXIT_FAILURE; |
| 127 } |
| 128 |
| 129 gamma_table_out_precache = NULL; |
| 130 gamma_table_size_precache = 0; |
| 131 if (get_output_gamma_table(in_path, &gamma_table_out_precache, &gamma_table_
size_precache, false) != 0) { |
| 132 fprintf(stderr, "Unable to extract precached output gamma table\n"); |
| 133 return EXIT_FAILURE; |
| 134 } |
| 135 |
| 136 if (gamma_table_size != gamma_table_size_precache) { |
| 137 fprintf(stderr, "Size mismatch between output table sizes.\n" |
| 138 "Output gamma size = %zu, gamma size with precahce = %zu\n", gam
ma_table_size, gamma_table_size_precache); |
| 139 free(gamma_table_out); |
| 140 free(gamma_table_out_precache); |
| 141 return EXIT_FAILURE; |
| 142 } |
| 143 |
| 144 printf("LUT size = %zu, %zu\n", gamma_table_size, gamma_table_size_precache)
; |
| 145 |
| 146 for (i = 0; i < 4 * gamma_table_size; ++i) { |
| 147 err += abs(gamma_table_out[i] - gamma_table_out_precache[i]); |
| 148 } |
| 149 |
| 150 if (err != 0) { |
| 151 fprintf(stderr, "Gamma output total error = %d\n.Aborting.", err); |
| 152 |
| 153 free(gamma_table_out); |
| 154 free(gamma_table_out_precache); |
| 155 |
| 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 |
| 163 free(gamma_table_out); |
| 164 free(gamma_table_out_precache); |
| 165 |
| 166 return EXIT_FAILURE; |
| 167 } |
| 168 |
| 169 // Check only for red curve for now. |
| 170 if (profile->redTRC->type == PARAMETRIC_CURVE_TYPE) { |
| 171 int type = - (profile->redTRC->count + 1); |
| 172 FILE *gamma_file; |
| 173 uint16_t *p_table_out, *p_table_in; |
| 174 double gamma_aprox_in = 0, gamma_aprox_out = 0; |
| 175 double sum_of_sqares = 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 gamma_file = fopen(file_name, "w"); |
| 188 fprintf(gamma_file, "Parametric gamma values for %s\n", profile->descrip
tion); |
| 189 fprintf(gamma_file, "gamma, a, b, c, d, e, f\n"); |
| 190 fprintf(gamma_file, "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n", |
| 191 profile->redTRC->parameter[0], profile->redTRC->parameter[1], pr
ofile->redTRC->parameter[2], |
| 192 profile->redTRC->parameter[3], profile->redTRC->parameter[4], pr
ofile->redTRC->parameter[5], |
| 193 profile->redTRC->parameter[6]); |
| 194 |
| 195 get_input_gamma_table(in_path, &gamma_table_in, gamma_table_size); |
| 196 |
| 197 fprintf(gamma_file, "\n\nInput gamma, Output gamma, LCMS Output gamma, O
utput gamma error\n"); |
| 198 |
| 199 p_table_out = gamma_table_out; |
| 200 p_table_in = gamma_table_in; |
| 201 |
| 202 for (i = 0; i < gamma_table_size; ++i) { |
| 203 float p = i / (gamma_table_size * 1.0); |
| 204 float reference_out = clamp_float(evaluate_parametric_curve(type, pr
ofile->redTRC->parameter, p)); |
| 205 float actual_out = *p_table_out * inverse65535; |
| 206 float error_out = fabs(actual_out - reference_out); |
| 207 float input = *p_table_in * inverse65535; |
| 208 |
| 209 if (p > MAX_FLOAT_ERROR && input > MAX_FLOAT_ERROR && actual_out > M
AX_FLOAT_ERROR) { |
| 210 gamma_aprox_in += log(p) * log(input); |
| 211 gamma_aprox_out += log(p) * log(actual_out); |
| 212 sum_of_sqares += log(p) * log(p); |
| 213 } |
| 214 |
| 215 fprintf(gamma_file, "%.6f, %.6f, %6f, %6f\n",input, actual_out, refe
rence_out, error_out); |
| 216 |
| 217 p_table_out += 4; // Skip other channels. |
| 218 p_table_in += 4; // Skip other channels. |
| 219 } |
| 220 |
| 221 gamma_aprox_in /= sum_of_sqares; |
| 222 gamma_aprox_out /= sum_of_sqares; |
| 223 |
| 224 printf("Computed gamma from LUTs:\nInput = %.6lf\nOutput = %.6lf\nAccura
cy? = %.6lf\n", |
| 225 gamma_aprox_in, gamma_aprox_out, gamma_aprox_in * gamma_aprox_ou
t); |
| 226 |
| 227 free(gamma_table_in); |
| 228 fclose(gamma_file); |
| 229 } |
| 230 |
| 231 qcms_profile_release(profile); |
| 232 |
| 233 free(gamma_table_out); |
| 234 free(gamma_table_out_precache); |
| 235 |
| 236 return err; |
| 237 } |
| 238 |
| 239 struct qcms_test_case qcms_test_output_trc_info = { |
| 240 "qcms_test_output_trc", |
| 241 qcms_test_output_trc, |
| 242 QCMS_TEST_DISABLED |
| 243 }; |
OLD | NEW |