| 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 <assert.h> | |
| 9 #include <math.h> | |
| 10 #include <stdio.h> | |
| 11 #include <stdlib.h> | |
| 12 #include <time.h> | |
| 13 | |
| 14 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para' | |
| 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) | |
| 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 sRGB = qcms_profile_sRGB(); | |
| 33 | |
| 34 transform = qcms_transform_create(sRGB, QCMS_DATA_RGBA_8, target, QCMS_DATA_
RGBA_8, QCMS_INTENT_DEFAULT); | |
| 35 if (!transform) { | |
| 36 fprintf(stderr, "Failed to create colour transform\n"); | |
| 37 qcms_profile_release(sRGB); | |
| 38 qcms_profile_release(target); | |
| 39 return EXIT_FAILURE; | |
| 40 } | |
| 41 | |
| 42 *size = qcms_transform_get_output_trc_rgba(transform, target, QCMS_TRC_USHOR
T, NULL); | |
| 43 assert(*size == 256); | |
| 44 | |
| 45 *table = malloc(*size * sizeof(uint16_t) * 4); | |
| 46 qcms_transform_get_output_trc_rgba(transform, target, QCMS_TRC_USHORT, *tabl
e); | |
| 47 | |
| 48 qcms_transform_release(transform); | |
| 49 qcms_profile_release(sRGB); | |
| 50 qcms_profile_release(target); | |
| 51 | |
| 52 return 0; | |
| 53 } | |
| 54 | |
| 55 static int get_input_gamma_table(const char *profile_path, uint16_t **table, siz
e_t *size) | |
| 56 { | |
| 57 qcms_transform *transform; | |
| 58 qcms_profile *source; | |
| 59 qcms_profile *sRGB; | |
| 60 | |
| 61 source = qcms_profile_from_path(profile_path); | |
| 62 if (!source) { | |
| 63 fprintf(stderr, "Invalid input profile\n"); | |
| 64 return EXIT_FAILURE; | |
| 65 } | |
| 66 | |
| 67 sRGB = qcms_profile_sRGB(); | |
| 68 | |
| 69 transform = qcms_transform_create(source, QCMS_DATA_RGBA_8, sRGB, QCMS_DATA_
RGBA_8, QCMS_INTENT_DEFAULT); | |
| 70 if (!transform) { | |
| 71 fprintf(stderr, "Failed to create colour transform\n"); | |
| 72 qcms_profile_release(sRGB); | |
| 73 qcms_profile_release(source); | |
| 74 return EXIT_FAILURE; | |
| 75 } | |
| 76 | |
| 77 *size = qcms_transform_get_input_trc_rgba(transform, source, QCMS_TRC_USHORT
, NULL); | |
| 78 assert(*size == 256); | |
| 79 | |
| 80 *table = calloc(*size, sizeof(uint16_t) * 4); | |
| 81 qcms_transform_get_input_trc_rgba(transform, source, QCMS_TRC_USHORT, *table
); | |
| 82 | |
| 83 qcms_transform_release(transform); | |
| 84 qcms_profile_release(sRGB); | |
| 85 qcms_profile_release(source); | |
| 86 | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 static int qcms_test_output_trc(size_t width, | |
| 91 size_t height, | |
| 92 int iterations, | |
| 93 const char *in_path, | |
| 94 const char *out_path, | |
| 95 const int force_software) | |
| 96 { | |
| 97 qcms_profile *profile; | |
| 98 uint16_t *gamma_table_out; | |
| 99 size_t gamma_table_size; | |
| 100 int i; | |
| 101 | |
| 102 printf("Test qcms output gamma curve table integrity.\n"); | |
| 103 | |
| 104 if (!in_path) { | |
| 105 fprintf(stderr, "%s: please provide valid ICC profiles via -i option\n",
__FUNCTION__); | |
| 106 return EXIT_FAILURE; | |
| 107 } | |
| 108 | |
| 109 // Create profiles and transforms, get table and then free resources to make
sure none | |
| 110 // of the internal tables are initialized by previous calls. | |
| 111 gamma_table_out = NULL; | |
| 112 gamma_table_size = 0; | |
| 113 if (get_output_gamma_table(in_path, &gamma_table_out, &gamma_table_size) !=
0) { | |
| 114 fprintf(stderr, "Unable to extract output gamma table\n"); | |
| 115 return EXIT_FAILURE; | |
| 116 } | |
| 117 | |
| 118 printf("LUT size = %zu\n", gamma_table_size); | |
| 119 | |
| 120 profile = qcms_profile_from_path(in_path); | |
| 121 if (!profile) { | |
| 122 fprintf(stderr, "Invalid input profile\n"); | |
| 123 free(gamma_table_out); | |
| 124 return EXIT_FAILURE; | |
| 125 } | |
| 126 | |
| 127 // Check only for red curve for now. | |
| 128 if (profile->redTRC->type == PARAMETRIC_CURVE_TYPE) { | |
| 129 int type = - (int)(profile->redTRC->count + 1); | |
| 130 FILE *gamma_file; | |
| 131 uint16_t *gamma_table_in = NULL; | |
| 132 uint16_t *p_table_out, *p_table_in; | |
| 133 char file_name[256] = {0,}; | |
| 134 size_t input_size = 0; | |
| 135 | |
| 136 printf("Detected parametric curve type = %d\n", profile->redTRC->count); | |
| 137 | |
| 138 sprintf(file_name, "qcms-test-%ld-parametric-gamma-%s.csv", (long int)ti
me(NULL), profile->description); | |
| 139 printf("Writing input and output gamma tables to %s\n", file_name); | |
| 140 | |
| 141 printf("gamma = %.6f, a = %.6f, b = %.6f, c = %.6f, d = %.6f, e = %.6f,
f = %.6f\n", | |
| 142 profile->redTRC->parameter[0], profile->redTRC->parameter[1], pr
ofile->redTRC->parameter[2], | |
| 143 profile->redTRC->parameter[3], profile->redTRC->parameter[4], pr
ofile->redTRC->parameter[5], | |
| 144 profile->redTRC->parameter[6]); | |
| 145 | |
| 146 // Write output to stdout and tables into a csv file. | |
| 147 gamma_file = fopen(file_name, "w"); | |
| 148 fprintf(gamma_file, "Parametric gamma values for %s\n", profile->descrip
tion); | |
| 149 fprintf(gamma_file, "gamma, a, b, c, d, e, f\n"); | |
| 150 fprintf(gamma_file, "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n", | |
| 151 profile->redTRC->parameter[0], profile->redTRC->parameter[1], pr
ofile->redTRC->parameter[2], | |
| 152 profile->redTRC->parameter[3], profile->redTRC->parameter[4], pr
ofile->redTRC->parameter[5], | |
| 153 profile->redTRC->parameter[6]); | |
| 154 | |
| 155 get_input_gamma_table(in_path, &gamma_table_in, &input_size); | |
| 156 assert(input_size == gamma_table_size); | |
| 157 if (!gamma_table_in) { | |
| 158 fprintf(stderr, "Unable to compute input trc. Aborting\n"); | |
| 159 fclose(gamma_file); | |
| 160 qcms_profile_release(profile); | |
| 161 free(gamma_table_out); | |
| 162 return EXIT_FAILURE; | |
| 163 } | |
| 164 | |
| 165 fprintf(gamma_file, "\n\nInput gamma, Output gamma, LCMS Output gamma, O
utput gamma error\n"); | |
| 166 | |
| 167 p_table_out = gamma_table_out; | |
| 168 p_table_in = gamma_table_in; | |
| 169 | |
| 170 for (i = 0; i < gamma_table_size; ++i) { | |
| 171 float p = i / (gamma_table_size * 1.0); | |
| 172 float reference_out = clamp_float(evaluate_parametric_curve(type, pr
ofile->redTRC->parameter, p)); | |
| 173 float actual_out = *p_table_out * inverse65535; | |
| 174 float error_out = fabs(actual_out - reference_out); | |
| 175 float input = *p_table_in * inverse65535; | |
| 176 | |
| 177 fprintf(gamma_file, "%.6f, %.6f, %6f, %6f\n",input, actual_out, refe
rence_out, error_out); | |
| 178 | |
| 179 p_table_out += 4; // Skip other channels. | |
| 180 p_table_in += 4; // Skip other channels. | |
| 181 } | |
| 182 | |
| 183 free(gamma_table_in); | |
| 184 fclose(gamma_file); | |
| 185 } | |
| 186 | |
| 187 qcms_profile_release(profile); | |
| 188 free(gamma_table_out); | |
| 189 | |
| 190 return 0; | |
| 191 } | |
| 192 | |
| 193 struct qcms_test_case qcms_test_output_trc_info = { | |
| 194 "qcms_test_output_trc", | |
| 195 qcms_test_output_trc, | |
| 196 QCMS_TEST_DISABLED | |
| 197 }; | |
| OLD | NEW |