+
+void expect_equal(const uint16_t *ref, const uint16_t *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
+{
+ assert(width > 0);
+ assert(height > 0);
+
+ float *ref_float = new float[width * height];
+ float *result_float = new float[width * height];
+
+ for (unsigned y = 0; y < height; ++y) {
+ for (unsigned x = 0; x < width; ++x) {
+ ref_float[y * width + x] = ref[y * width + x];
+ result_float[y * width + x] = result[y * width + x];
+ }
+ }
+
+ expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
+
+ delete[] ref_float;
+ delete[] result_float;
+}
+
+void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
+{
+ assert(width > 0);
+ assert(height > 0);
+
+ float *ref_float = new float[width * height];
+ float *result_float = new float[width * height];
+
+ for (unsigned y = 0; y < height; ++y) {
+ for (unsigned x = 0; x < width; ++x) {
+ ref_float[y * width + x] = ref[y * width + x];
+ result_float[y * width + x] = result[y * width + x];
+ }
+ }
+
+ expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
+
+ delete[] ref_float;
+ delete[] result_float;
+}
+
+void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
+{
+ double squared_difference = 0.0;
+ for (unsigned i = 0; i < num_values; ++i) {
+ double absolute_error = fabs(expected[i] - result[i]);
+ squared_difference += absolute_error * absolute_error;
+ EXPECT_LT(absolute_error, absolute_error_limit);
+
+ if (expected[i] > 0.0) {
+ double relative_error = fabs(absolute_error / expected[i]);
+
+ EXPECT_LT(relative_error, relative_error_limit);
+ }
+ if (i < num_values - 1) {
+ double delta = expected[i + 1] - expected[i];
+ double local_relative_error = fabs(absolute_error / delta);
+ EXPECT_LT(local_relative_error, local_relative_error_limit);
+ }
+ }
+ double rms = sqrt(squared_difference) / num_values;
+ EXPECT_LT(rms, rms_limit);
+}
+
+double srgb_to_linear(double x)
+{
+ // From the Wikipedia article on sRGB.
+ if (x < 0.04045) {
+ return x / 12.92;
+ } else {
+ return pow((x + 0.055) / 1.055, 2.4);
+ }
+}
+
+double linear_to_srgb(double x)
+{
+ // From the Wikipedia article on sRGB.
+ if (x < 0.0031308) {
+ return 12.92 * x;
+ } else {
+ return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
+ }
+}
+
+DisableComputeShadersTemporarily::DisableComputeShadersTemporarily(bool disable_compute_shaders)
+ : disable_compute_shaders(disable_compute_shaders)
+{
+ init_movit_for_test();
+ saved_compute_shaders_supported = movit_compute_shaders_supported;
+ if (disable_compute_shaders) {
+ movit_compute_shaders_supported = false;
+ }
+}
+
+DisableComputeShadersTemporarily::~DisableComputeShadersTemporarily()
+{
+ movit_compute_shaders_supported = saved_compute_shaders_supported;
+}
+
+bool DisableComputeShadersTemporarily::should_skip()
+{
+ if (disable_compute_shaders) {
+ return false;
+ }
+
+ if (!movit_compute_shaders_supported) {
+ fprintf(stderr, "Compute shaders not supported; skipping.\n");
+ return true;
+ }
+ return false;
+}
+
+#ifdef HAVE_BENCHMARK
+bool DisableComputeShadersTemporarily::should_skip(benchmark::State *benchmark_state)
+{
+ if (disable_compute_shaders) {
+ return false;
+ }
+
+ if (!movit_compute_shaders_supported) {
+ benchmark_state->SkipWithError("Compute shaders not supported");
+ return true;
+ }
+ return false;
+}
+#endif
+
+} // namespace movit