Update pdfium_diff printf messages to use the current binary name
The usage messages were confusing as they used image_diff instead of
pdfium_diff as the binary name.
Changes some static consts to constexprs along the way.
Change-Id: I6df02eb6ad4e2ed057c058218b78e155d02b2d00
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64870
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
diff --git a/testing/image_diff/image_diff.cpp b/testing/image_diff/image_diff.cpp
index a102a36..c8f9caf 100644
--- a/testing/image_diff/image_diff.cpp
+++ b/testing/image_diff/image_diff.cpp
@@ -20,6 +20,7 @@
#include "core/fxcrt/fx_memory.h"
#include "testing/image_diff/image_diff_png.h"
+#include "testing/utils/path_service.h"
#include "third_party/base/logging.h"
#include "third_party/base/numerics/safe_conversions.h"
@@ -28,13 +29,13 @@
#endif
// Return codes used by this utility.
-static const int kStatusSame = 0;
-static const int kStatusDifferent = 1;
-static const int kStatusError = 2;
+constexpr int kStatusSame = 0;
+constexpr int kStatusDifferent = 1;
+constexpr int kStatusError = 2;
// Color codes.
-static const uint32_t RGBA_RED = 0x000000ff;
-static const uint32_t RGBA_ALPHA = 0xff000000;
+constexpr uint32_t RGBA_RED = 0x000000ff;
+constexpr uint32_t RGBA_ALPHA = 0xff000000;
class Image {
public:
@@ -193,22 +194,24 @@
return CalculateDifferencePercentage(actual, pixels_different);
}
-void PrintHelp() {
+void PrintHelp(const std::string& binary_name) {
fprintf(
stderr,
"Usage:\n"
- " image_diff OPTIONS <compare file> <reference file>\n"
+ " %s OPTIONS <compare file> <reference file>\n"
" Compares two files on disk, returning 0 when they are the same;\n"
" Passing \"--histogram\" additionally calculates a diff of the\n"
" RGBA value histograms. (which is resistant to shifts in layout)\n"
" Passing \"--reverse-byte-order\" additionally assumes the compare\n"
" file has BGRA byte ordering.\n"
- " image_diff --diff <compare file> <reference file> <output file>\n"
+ " %s --diff <compare file> <reference file> <output file>\n"
" Compares two files on disk, outputs an image that visualizes the\n"
- " difference to <output file>\n");
+ " difference to <output file>\n",
+ binary_name.c_str(), binary_name.c_str());
}
-int CompareImages(const std::string& file1,
+int CompareImages(const std::string& binary_name,
+ const std::string& file1,
const std::string& file2,
bool compare_histograms,
bool reverse_byte_order) {
@@ -220,11 +223,13 @@
? actual_image.CreateFromFilenameWithReverseByteOrder(file1)
: actual_image.CreateFromFilename(file1);
if (!actual_load_result) {
- fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
+ fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
+ file1.c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
- fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
+ fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
+ file2.c_str());
return kStatusError;
}
@@ -275,18 +280,21 @@
return same;
}
-int DiffImages(const std::string& file1,
+int DiffImages(const std::string& binary_name,
+ const std::string& file1,
const std::string& file2,
const std::string& out_file) {
Image actual_image;
Image baseline_image;
if (!actual_image.CreateFromFilename(file1)) {
- fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
+ fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
+ file1.c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
- fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
+ fprintf(stderr, "%s: Unable to open file \"%s\"\n", binary_name.c_str(),
+ file2.c_str());
return kStatusError;
}
@@ -322,6 +330,10 @@
std::string filename2;
std::string diff_filename;
+ // Strip the path from the first arg
+ const char* last_separator = strrchr(argv[0], PATH_SEPARATOR);
+ std::string binary_name = last_separator ? last_separator + 1 : argv[0];
+
int i;
for (i = 1; i < argc; ++i) {
const char* arg = argv[i];
@@ -344,12 +356,13 @@
if (produce_diff_image) {
if (!diff_filename.empty()) {
- return DiffImages(filename1, filename2, diff_filename);
+ return DiffImages(binary_name, filename1, filename2, diff_filename);
}
} else if (!filename2.empty()) {
- return CompareImages(filename1, filename2, histograms, reverse_byte_order);
+ return CompareImages(binary_name, filename1, filename2, histograms,
+ reverse_byte_order);
}
- PrintHelp();
+ PrintHelp(binary_name);
return kStatusError;
}