| 269 | } |
| 270 | |
| 271 | static void setupImageCompare(CLI::App& image) |
| 272 | { |
| 273 | auto* cmd = image.add_subcommand("compare", "Compare two images pixel-by-pixel"); |
| 274 | static std::string inputPath1; |
| 275 | static std::string inputPath2; |
| 276 | static std::string diffOutput; |
| 277 | static double threshold = 0.0; |
| 278 | cmd->add_option("image-a", inputPath1, "First image (PAA/PAC/PNG/BMP/TGA/JPG)") |
| 279 | ->required() |
| 280 | ->check(CLI::ExistingFile); |
| 281 | cmd->add_option("image-b", inputPath2, "Second image (PAA/PAC/PNG/BMP/TGA/JPG)") |
| 282 | ->required() |
| 283 | ->check(CLI::ExistingFile); |
| 284 | cmd->add_option("-o,--diff", diffOutput, "Save difference visualization to file (PNG/BMP/TGA)"); |
| 285 | cmd->add_option("-t,--threshold", threshold, "Max allowed mean diff (exit 0 if within, 1 if exceeded)"); |
| 286 | |
| 287 | cmd->callback( |
| 288 | []() |
| 289 | { |
| 290 | auto imgA = Poseidon::Image::FromFile(inputPath1); |
| 291 | if (!imgA.valid()) |
| 292 | { |
| 293 | std::cerr << "Error: Failed to load: " << inputPath1 << std::endl; |
| 294 | throw CLI::RuntimeError(2); |
| 295 | } |
| 296 | |
| 297 | auto imgB = Poseidon::Image::FromFile(inputPath2); |
| 298 | if (!imgB.valid()) |
| 299 | { |
| 300 | std::cerr << "Error: Failed to load: " << inputPath2 << std::endl; |
| 301 | throw CLI::RuntimeError(2); |
| 302 | } |
| 303 | |
| 304 | if (imgA.width() != imgB.width() || imgA.height() != imgB.height()) |
| 305 | { |
| 306 | std::cout << "result: different" << std::endl; |
| 307 | std::cout << "reason: size mismatch (" << imgA.width() << "x" << imgA.height() << " vs " << imgB.width() |
| 308 | << "x" << imgB.height() << ")" << std::endl; |
| 309 | throw CLI::RuntimeError(1); |
| 310 | } |
| 311 | |
| 312 | auto rgbaA = imgA.ToRGBA(); |
| 313 | auto rgbaB = imgB.ToRGBA(); |
| 314 | const auto& pixA = rgbaA.data(); |
| 315 | const auto& pixB = rgbaB.data(); |
| 316 | int w = rgbaA.width(); |
| 317 | int h = rgbaA.height(); |
| 318 | int pixelCount = w * h; |
| 319 | uint64_t totalDiff = 0; |
| 320 | int changedPixels = 0; |
| 321 | int maxDiff = 0; |
| 322 | std::vector<uint8_t> diffPixels; |
| 323 | bool writeDiff = !diffOutput.empty(); |
| 324 | if (writeDiff) |
| 325 | diffPixels.resize(pixelCount * 4); |
| 326 | |
| 327 | for (int i = 0; i < pixelCount; ++i) |
| 328 | { |