Chromium Code Reviews| Index: third_party/qcms/src/tests/qcms_test_output_trc.c |
| diff --git a/third_party/qcms/src/tests/qcms_test_output_trc.c b/third_party/qcms/src/tests/qcms_test_output_trc.c |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..297ef358ac64fa93fed551af14046b65c7fcb88b |
| --- /dev/null |
| +++ b/third_party/qcms/src/tests/qcms_test_output_trc.c |
| @@ -0,0 +1,251 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the Chromium LICENSE file. |
| + |
| +#include "qcms.h" |
| +#include "qcms_test_util.h" |
| + |
| +#include <math.h> |
| +#include <stdio.h> |
| +#include <stdlib.h> |
| +#include <time.h> |
| + |
| +#define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para' |
| +#define MAX_FLOAT_ERROR 0.000001f |
| + |
| +static const float inverse65535 = (float) (1.0 / 65535.0); |
| + |
| +extern float clamp_float(float a); |
| + |
| +static int get_output_gamma_table(const char *profile_path, uint16_t **table, size_t *size, bool precache) |
| +{ |
| + qcms_format_type format = {0, 2}; // RGBA |
|
Noel Gordon
2016/04/19 01:26:05
format: where is this used?
radu.velea
2016/04/19 08:42:06
Done.
|
| + qcms_transform *transform; |
| + qcms_profile *profile, *sRGB; |
|
Noel Gordon
2016/04/19 01:26:05
qcms_profile *sRGB;
qcms_profile *target;
radu.velea
2016/04/19 08:42:07
Done.
|
| + int ret = 0; |
| + |
| + profile = qcms_profile_from_path(profile_path); |
|
Noel Gordon
2016/04/19 01:26:05
target = qcms_profile_from_path(profile_path);
radu.velea
2016/04/19 08:42:06
Done.
|
| + if (!profile) { |
| + fprintf(stderr, "Invalid input profile\n"); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + sRGB = qcms_profile_sRGB(); |
|
Noel Gordon
2016/04/19 01:26:05
Move this, place it after the precache step.
radu.velea
2016/04/19 08:42:06
Done.
|
| + |
| + if (precache) { |
| + qcms_profile_precache_output_transform(profile); |
| + } |
| + |
| + transform = qcms_transform_create(sRGB, QCMS_DATA_RGBA_8, profile, QCMS_DATA_RGBA_8, QCMS_INTENT_DEFAULT); |
| + if (!transform) { |
| + fprintf(stderr, "Failed to create colour transform\n"); |
| + ret = 1; |
|
Noel Gordon
2016/04/19 01:26:06
clean up right here: no real need for the goto, no
radu.velea
2016/04/19 08:42:06
Done.
|
| + goto RELEASE_PROFILES; |
| + } |
| + |
| + *size = qcms_transform_get_output_trc_rgba(transform, profile, QCMS_TRC_USHORT, NULL); |
| + *table = malloc(*size * sizeof(uint16_t) * 4); |
| + qcms_transform_get_output_trc_rgba(transform, profile, QCMS_TRC_USHORT, *table); |
| + |
| + qcms_transform_release(transform); |
| + |
| + RELEASE_PROFILES: |
| + qcms_profile_release(sRGB); |
| + qcms_profile_release(profile); |
| + |
| + return ret; |
| +} |
| + |
| +static int get_input_gamma_table(const char *profile_path, uint16_t **table, size_t expected_size) |
| +{ |
| + qcms_format_type format = {0, 2}; // RGBA |
|
Noel Gordon
2016/04/19 01:26:06
format: where is this used?
radu.velea
2016/04/19 08:42:07
Done.
|
| + qcms_transform *transform; |
| + qcms_profile *profile, *sRGB; |
|
Noel Gordon
2016/04/19 01:26:05
qcms_profile *source;
qcms_profile *sRGB;
radu.velea
2016/04/19 08:42:06
Done.
|
| + size_t size; |
| + int ret = 0; |
| + |
| + profile = qcms_profile_from_path(profile_path); |
| + if (!profile) { |
| + fprintf(stderr, "Invalid input profile\n"); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + sRGB = qcms_profile_sRGB(); |
| + |
| + transform = qcms_transform_create(profile, QCMS_DATA_RGBA_8, sRGB, QCMS_DATA_RGBA_8, QCMS_INTENT_DEFAULT); |
| + if (!transform) { |
| + fprintf(stderr, "Failed to create colour transform\n"); |
| + ret = 1; |
|
Noel Gordon
2016/04/19 01:26:05
again, clean up right here (no need for the goto,
radu.velea
2016/04/19 08:42:07
Done.
|
| + goto RELEASE_PROFILES; |
| + } |
| + |
| + // For now the size of input gamma table for parametric curves is hardcoded to 256. |
| + // See compute_curve_gamma_table_type_parametric for details. |
| + // Future implementations might decide to return an arbitrary-sized table. |
| + size = qcms_transform_get_input_trc_rgba(transform, profile, QCMS_TRC_USHORT, NULL); |
| + if (size != expected_size) { |
| + fprintf(stderr, "Expected input table size (%zu) to match output size (%zu)\n", size, expected_size); |
| + size = size > expected_size ? size : expected_size; |
| + } |
| + |
| + *table = calloc(size, sizeof(uint16_t) * 4); |
| + qcms_transform_get_input_trc_rgba(transform, profile, QCMS_TRC_USHORT, *table); |
| + |
| + qcms_transform_release(transform); |
| + |
| + RELEASE_PROFILES: |
| + qcms_profile_release(sRGB); |
| + qcms_profile_release(profile); |
| + |
| + return ret; |
| +} |
| + |
| +static int qcms_test_output_trc(size_t width, |
| + size_t height, |
| + int iterations, |
| + const char *in_path, |
| + const char *out_path, |
| + const int force_software) |
| +{ |
| + int err = 0; |
| + qcms_profile *profile; |
| + uint16_t *gamma_table_out, *gamma_table_out_precache; |
| + uint16_t *gamma_table_in; |
| + size_t gamma_table_size, gamma_table_size_precache; |
| + char file_name[256] = {0,}; |
|
Noel Gordon
2016/04/19 01:26:05
Move this down to where used.
radu.velea
2016/04/19 08:42:06
Done.
|
| + int i; |
| + |
| + printf("Test qcms output gamma curve integrity with and without precached tables.\n"); |
| + |
| + if (!in_path) { |
| + fprintf(stderr, "%s: please provide valid ICC profiles via -i option\n", __FUNCTION__); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + // Create profiles and transforms, get table and then free resources to make sure none |
| + // of the internal tables are initialized by previous calls. |
| + gamma_table_out = NULL; |
| + gamma_table_size = 0; |
| + if (get_output_gamma_table(in_path, &gamma_table_out, &gamma_table_size, true) != 0) { |
| + fprintf(stderr, "Unable to extract output gamma table\n"); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + gamma_table_out_precache = NULL; |
| + gamma_table_size_precache = 0; |
| + if (get_output_gamma_table(in_path, &gamma_table_out_precache, &gamma_table_size_precache, false) != 0) { |
| + fprintf(stderr, "Unable to extract precached output gamma table\n"); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + // Check if precached and non-precached tables match in size and contents. |
| + if (gamma_table_size != gamma_table_size_precache) { |
| + fprintf(stderr, "Size mismatch between output table sizes.\n" |
| + "Output gamma size = %zu, gamma size with precahce = %zu\n", gamma_table_size, gamma_table_size_precache); |
| + free(gamma_table_out); |
| + free(gamma_table_out_precache); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + printf("LUT size = %zu, %zu\n", gamma_table_size, gamma_table_size_precache); |
| + |
| + for (i = 0; i < 4 * gamma_table_size; ++i) { |
| + err += abs(gamma_table_out[i] - gamma_table_out_precache[i]); |
| + } |
| + |
| + if (err != 0) { |
| + fprintf(stderr, "Gamma output total error = %d\n.Aborting.", err); |
| + |
|
Noel Gordon
2016/04/19 01:26:05
extra line: remove.
radu.velea
2016/04/19 08:42:06
Done.
|
| + free(gamma_table_out); |
| + free(gamma_table_out_precache); |
| + |
|
Noel Gordon
2016/04/19 01:26:05
extra line: remove.
radu.velea
2016/04/19 08:42:06
Done.
|
| + return EXIT_FAILURE; |
| + } |
| + |
| + profile = qcms_profile_from_path(in_path); |
| + if (!profile) { |
| + fprintf(stderr, "Invalid input profile\n"); |
| + |
|
Noel Gordon
2016/04/19 01:26:05
extra line: remove.
radu.velea
2016/04/19 08:42:07
Done.
|
| + free(gamma_table_out); |
| + free(gamma_table_out_precache); |
| + |
|
Noel Gordon
2016/04/19 01:26:05
extra line: remove.
radu.velea
2016/04/19 08:42:07
Done.
|
| + return EXIT_FAILURE; |
| + } |
| + |
| + // Check only for red curve for now. |
| + if (profile->redTRC->type == PARAMETRIC_CURVE_TYPE) { |
| + int type = - (profile->redTRC->count + 1); |
| + FILE *gamma_file; |
| + uint16_t *p_table_out, *p_table_in; |
| + double gamma_aprox_in = 0, gamma_aprox_out = 0; |
| + double sum_of_sqares = 0; |
| + |
| + printf("Detected parametric curve type = %d\n", profile->redTRC->count); |
| + |
| + sprintf(file_name, "qcms-test-%ld-parametric-gamma-%s.csv", (long int)time(NULL), profile->description); |
| + printf("Writing input and output gamma tables to %s\n", file_name); |
| + |
| + printf("gamma = %.6f, a = %.6f, b = %.6f, c = %.6f, d = %.6f, e = %.6f, f = %.6f\n", |
| + profile->redTRC->parameter[0], profile->redTRC->parameter[1], profile->redTRC->parameter[2], |
| + profile->redTRC->parameter[3], profile->redTRC->parameter[4], profile->redTRC->parameter[5], |
| + profile->redTRC->parameter[6]); |
| + |
| + // Write output to stdout and tables into a csv file. |
| + gamma_file = fopen(file_name, "w"); |
| + fprintf(gamma_file, "Parametric gamma values for %s\n", profile->description); |
| + fprintf(gamma_file, "gamma, a, b, c, d, e, f\n"); |
| + fprintf(gamma_file, "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n", |
| + profile->redTRC->parameter[0], profile->redTRC->parameter[1], profile->redTRC->parameter[2], |
| + profile->redTRC->parameter[3], profile->redTRC->parameter[4], profile->redTRC->parameter[5], |
| + profile->redTRC->parameter[6]); |
| + |
| + get_input_gamma_table(in_path, &gamma_table_in, gamma_table_size); |
| + |
| + fprintf(gamma_file, "\n\nInput gamma, Output gamma, LCMS Output gamma, Output gamma error\n"); |
| + |
| + p_table_out = gamma_table_out; |
| + p_table_in = gamma_table_in; |
| + |
| + for (i = 0; i < gamma_table_size; ++i) { |
| + float p = i / (gamma_table_size * 1.0); |
| + float reference_out = clamp_float(evaluate_parametric_curve(type, profile->redTRC->parameter, p)); |
| + float actual_out = *p_table_out * inverse65535; |
| + float error_out = fabs(actual_out - reference_out); |
| + float input = *p_table_in * inverse65535; |
| + |
| + // Approximate input and output gamma based on http://www.brucelindbloom.com/index.html?Eqn_BestGamma.html |
| + if (p > MAX_FLOAT_ERROR && input > MAX_FLOAT_ERROR && actual_out > MAX_FLOAT_ERROR) { |
| + gamma_aprox_in += log(p) * log(input); |
| + gamma_aprox_out += log(p) * log(actual_out); |
| + sum_of_sqares += log(p) * log(p); |
| + } |
| + |
| + fprintf(gamma_file, "%.6f, %.6f, %6f, %6f\n",input, actual_out, reference_out, error_out); |
| + |
| + p_table_out += 4; // Skip other channels. |
| + p_table_in += 4; // Skip other channels. |
| + } |
| + |
| + gamma_aprox_in /= sum_of_sqares; |
| + gamma_aprox_out /= sum_of_sqares; |
| + |
| + printf("Computed gamma from LUTs:\nInput = %.6lf\nOutput = %.6lf\nAccuracy? = %.6lf\n", |
|
radu.velea
2016/04/19 08:42:07
I didn't know how exactly to refer to the aproxInp
|
| + gamma_aprox_in, gamma_aprox_out, gamma_aprox_in * gamma_aprox_out); |
| + |
| + free(gamma_table_in); |
| + fclose(gamma_file); |
| + } |
| + |
| + qcms_profile_release(profile); |
| + |
| + free(gamma_table_out); |
| + free(gamma_table_out_precache); |
| + |
| + return err; |
| +} |
| + |
| +struct qcms_test_case qcms_test_output_trc_info = { |
| + "qcms_test_output_trc", |
| + qcms_test_output_trc, |
| + QCMS_TEST_DISABLED |
| +}; |