<commit_before>/* Rapicorn
 * Copyright (C) 2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * A copy of the GNU Lesser General Public License should ship along
 * with this library; if not, see http://www.gnu.org/copyleft/.
 */
#include <rapicorn.hh>
#include <rcore/testutils.hh>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define PRGNAME "imgcheck"

namespace {
using namespace Rapicorn;

static double
compare_image_files (const String &image1_file,
                     const String &image2_file)
{
  Pixmap image1 (0, 0), image2 (0, 0);
  /* load images */
  if (!image1.load_png (image1_file))
    fatal ("failed to load \"%s\": %s", image1_file.c_str(), string_from_errno (errno).c_str());

  if (!image2.load_png (image2_file))
    fatal ("failed to load \"%s\": %s", image2_file.c_str(), string_from_errno (errno).c_str());

  /* check sizes */
  if (image1.width() != image2.width() || image1.height() != image2.height())
    return DBL_MAX;

  /* check equality */
  double avgerror = 0, maxerr = 0, npixels = 0, nerrors = 0;
  image1.compare (image2, 0, 0, -1, -1, 0, 0, &avgerror, &maxerr, &nerrors, &npixels);

  return avgerror * nerrors;
}

static double similarity_threshold_perc = 0.5;

static void
help_usage (bool usage_error)
{
  String usage = String () + "Usage: " + PRGNAME + " [OPTIONS] <image1.png> <image2.png>";
  if (usage_error)
    {
      printerr ("%s\n", usage.c_str());
      printerr ("Try '%s --help' for more information.\n", PRGNAME);
      exit_app (1);
    }
  printout ("%s\n", usage.c_str());
  /*         12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
  printout ("Compare image1.png and image2.png according to a similarity threshold.\n");
  printout ("\n");
  printout ("Options:\n");
  printout ("  -t <threshold_percent>        Threshold to consider images equal.\n");
  printout ("  -h, --help                    Display this help and exit.\n");
  printout ("  -v, --version                 Display version and exit.\n");<commit_msg>TESTS: imgcheck: raise pixel threshold from 0.5% to 6% to deal with newer cairo<commit_after>/* Rapicorn
 * Copyright (C) 2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * A copy of the GNU Lesser General Public License should ship along
 * with this library; if not, see http://www.gnu.org/copyleft/.
 */
#include <rapicorn.hh>
#include <rcore/testutils.hh>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define PRGNAME "imgcheck"

namespace {
using namespace Rapicorn;

static double
compare_image_files (const String &image1_file,
                     const String &image2_file)
{
  Pixmap image1 (0, 0), image2 (0, 0);
  /* load images */
  if (!image1.load_png (image1_file))
    fatal ("failed to load \"%s\": %s", image1_file.c_str(), string_from_errno (errno).c_str());

  if (!image2.load_png (image2_file))
    fatal ("failed to load \"%s\": %s", image2_file.c_str(), string_from_errno (errno).c_str());

  /* check sizes */
  if (image1.width() != image2.width() || image1.height() != image2.height())
    return DBL_MAX;

  /* check equality */
  double avgerror = 0, maxerr = 0, npixels = 0, nerrors = 0;
  image1.compare (image2, 0, 0, -1, -1, 0, 0, &avgerror, &maxerr, &nerrors, &npixels);
  if (0) // debugging
    printerr ("image difference: avgerror=%f maxerr=%f nerrors=%g npixels=%g (%u * %u)\n",
              avgerror, maxerr, nerrors, npixels, image1.width(), image1.height());

  return maxerr;
}

static double similarity_threshold_perc = 6.0;

static void
help_usage (bool usage_error)
{
  String usage = String () + "Usage: " + PRGNAME + " [OPTIONS] <image1.png> <image2.png>";
  if (usage_error)
    {
      printerr ("%s\n", usage.c_str());
      printerr ("Try '%s --help' for more information.\n", PRGNAME);
      exit_app (1);
    }
  printout ("%s\n", usage.c_str());
  /*         12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
  printout ("Compare image1.png and image2.png according to a similarity threshold.\n");
  printout ("\n");
  printout ("Options:\n");
  printout ("  -t <threshold_percent>        Threshold to consider images equal.\n");
  printout ("  -h, --help                    Display this help and exit.\n");
  printout ("  -v, --version                 Display version and exit.\n");
{'fraction_non_alphanumeric': 0.13323268541234196, 'fraction_numerical': 0.047556142668428, 'mean_word_length': 3.409317803660566, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3353796', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1678, 'n_words': 575}