1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/*
Compare Args
Copyright (C) 2006-2011 Yangli Hector Yee
Copyright (C) 2011-2016 Steven Myint, Jeff Terrace
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "compare_args.h"
#include "rgba_image.h"
#include <cassert>
#include <ciso646>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <sstream>
namespace pdiff
{
static const auto VERSION = "2.1";
static const auto USAGE =
"Usage: perceptualdiff [options] image1 image2\n"
"\n"
"Compares image1 and image2 using a perceptually based image metric.\n"
"Images can be in any FreeImage-supported format: TIF, PNG, etc.\n"
"\n"
"Options:\n"
" --verbose Turn on verbose mode\n"
" --fov deg Field of view in degrees [0.1, 89.9] (default: 45.0)\n"
" --threshold p Number of pixels p below which differences are ignored\n"
" --gamma g Value to convert rgb into linear space (default: 2.2)\n"
" --luminance l White luminance (default: 100.0 cdm^-2)\n"
" --luminance-only Only consider luminance; ignore chroma (color) in the\n"
" comparison\n"
" --color-factor How much of color to use [0.0, 1.0] (default: 1.0)\n"
" --down-sample How many powers of two to down sample the image\n"
" (default: 0)\n"
" --scale Scale images to match each other's dimensions\n"
" --sum-errors Print a sum of the luminance and color differences\n"
" --output o Write difference to the file o\n"
" --version Print version\n"
"\n";
static bool option_matches(const char *arg, const std::string &option_name)
{
const auto string_arg = std::string(arg);
return (string_arg == "--" + option_name) or
(string_arg == "-" + option_name);
}
CompareArgs::CompareArgs(int argc, char **argv)
: verbose_(false),
sum_errors_(false),
down_sample_(0)
{
parse_args(argc, argv);
}
static void print_help()
{
std::cout << USAGE;
std::cout << "\n"
<< "OpenMP status: ";
#ifdef _OPENMP
std::cout << "enabled\n";
#else
std::cout << "disabled\n";
#endif
}
void CompareArgs::parse_args(const int argc, char **argv)
{
if (argc <= 1)
{
print_help();
exit(EXIT_FAILURE);
}
auto image_count = 0u;
const char *output_file_name = nullptr;
auto scale = false;
for (auto i = 1; i < argc; i++)
{
try
{
if (option_matches(argv[i], "help") or
option_matches(argv[i], "h"))
{
print_help();
exit(EXIT_SUCCESS);
}
else if (option_matches(argv[i], "fov"))
{
if (++i < argc)
{
parameters_.field_of_view = std::stof(argv[i]);
}
}
else if (option_matches(argv[i], "verbose"))
{
verbose_ = true;
}
else if (option_matches(argv[i], "threshold"))
{
if (++i < argc)
{
auto temporary = std::stoi(argv[i]);
if (temporary < 0)
{
throw PerceptualDiffException(
"-threshold must be positive");
}
parameters_.threshold_pixels =
static_cast<unsigned int>(temporary);
}
}
else if (option_matches(argv[i], "gamma"))
{
if (++i < argc)
{
parameters_.gamma = std::stof(argv[i]);
}
}
else if (option_matches(argv[i], "luminance"))
{
if (++i < argc)
{
parameters_.luminance = std::stof(argv[i]);
}
}
else if (option_matches(argv[i], "luminance-only") or
option_matches(argv[i], "luminanceonly"))
{
parameters_.luminance_only = true;
}
else if (option_matches(argv[i], "sum-errors"))
{
sum_errors_ = true;
}
else if (option_matches(argv[i], "color-factor") or
option_matches(argv[i], "colorfactor"))
{
if (++i < argc)
{
parameters_.color_factor = std::stof(argv[i]);
}
}
else if (option_matches(argv[i], "down-sample") or
option_matches(argv[i], "downsample"))
{
if (++i < argc)
{
auto temporary = std::stoi(argv[i]);
if (temporary < 0)
{
throw PerceptualDiffException(
"--downsample must be positive");
}
down_sample_ = static_cast<unsigned int>(temporary);
assert(down_sample_ <= INT_MAX);
}
}
else if (option_matches(argv[i], "scale"))
{
scale = true;
}
else if (option_matches(argv[i], "output"))
{
if (++i < argc)
{
output_file_name = argv[i];
}
}
else if (option_matches(argv[i], "version"))
{
std::cout << "perceptualdiff " << VERSION << "\n";
exit(EXIT_SUCCESS);
}
else if (image_count < 2)
{
auto image = read_from_file(argv[i]);
++image_count;
if (image_count == 1)
{
image_a_ = image;
}
else
{
image_b_ = image;
}
}
else
{
std::cerr << "Warning: option/file \"" << argv[i]
<< "\" ignored\n";
}
}
catch (const PerceptualDiffException &exception)
{
std::string reason = "";
if (not std::string(exception.what()).empty())
{
reason = std::string("; ") + exception.what();
}
throw ParseException(
"Invalid argument (" + std::string(argv[i]) + ") for " +
argv[i - 1] + reason);
}
catch (const std::invalid_argument &exception)
{
throw ParseException(
"Invalid argument (" + std::string(argv[i]) + ") for " +
argv[i - 1]);
}
}
if (not image_a_ or not image_b_)
{
std::cerr << "Not enough image files specified\n";
exit(EXIT_FAILURE);
}
for (auto i = 0u; i < down_sample_; i++)
{
const auto tmp_a = image_a_->down_sample();
const auto tmp_b = image_b_->down_sample();
if (tmp_a and tmp_b)
{
image_a_ = tmp_a;
image_b_ = tmp_b;
}
else
{
break;
}
if (verbose_)
{
std::cout << "Downsampling by " << (1 << (i + 1)) << "\n";
}
}
if (scale and
(image_a_->get_width() != image_b_->get_width() or
image_a_->get_height() != image_b_->get_height()))
{
auto min_width = image_a_->get_width();
if (image_b_->get_width() < min_width)
{
min_width = image_b_->get_width();
}
auto min_height = image_a_->get_height();
if (image_b_->get_height() < min_height)
{
min_height = image_b_->get_height();
}
if (verbose_)
{
std::cout << "Scaling to " << min_width << " x " << min_height
<< "\n";
}
auto tmp = image_a_->down_sample(min_width, min_height);
if (tmp)
{
image_a_ = tmp;
}
tmp = image_b_->down_sample(min_width, min_height);
if (tmp)
{
image_b_ = tmp;
}
}
if (output_file_name)
{
image_difference_ = std::make_shared<RGBAImage>(image_a_->get_width(),
image_a_->get_height(),
output_file_name);
}
}
void CompareArgs::print_args() const
{
std::cout
<< "Field of view is " << parameters_.field_of_view << " degrees\n"
<< "Threshold pixels is " << parameters_.threshold_pixels
<< " pixels\n"
<< "The gamma is " << parameters_.gamma << "\n"
<< "The display's luminance is " << parameters_.luminance
<< " candela per meter squared\n";
}
}
|