diff options
author | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
---|---|---|
committer | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
commit | db072ad4dc181eca5a1458656b130beb43f475bf (patch) | |
tree | a3c03c7f5497cb70503e2486662fa85cfb53415a /hw2/bin/image_compare.cpp | |
download | csci427-master.tar.xz csci427-master.zip |
Diffstat (limited to 'hw2/bin/image_compare.cpp')
-rw-r--r-- | hw2/bin/image_compare.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/hw2/bin/image_compare.cpp b/hw2/bin/image_compare.cpp new file mode 100644 index 0000000..4f1a798 --- /dev/null +++ b/hw2/bin/image_compare.cpp @@ -0,0 +1,90 @@ +#include <string> +#include <iostream> + +#include "util.h" +#include "image.h" +#include "imageIO.h" +#include "constants.h" +#include "errorMessage.h" + +int main(int argc, char** argv) +{ + // parse command line + if(argc != 3) + { + std::cout << "Usage: " << argv[0] << " <image A> <image B>" << std::endl; + return -1; + } + + std::string nameA(argv[1]); + std::string nameB(argv[2]); + + // get image extensions + std::string extA = getExtension(nameA); + std::string extB = getExtension(nameB); + + // check if both images have the same extension + if(extA != extB) errorMessage(" => different image types (PPM vs PFM)."); + + // load images + image imgA, imgB; + importImage(nameA, imgA); + importImage(nameB, imgB); + + // check if both have the same dimension + if(imgA.width() != imgB.width() || imgA.height() != imgB.height()) + errorMessage(" => different images sizes (%dx%d) vs (%dx%d).", imgA.width(), imgA.height(), imgB.width(), imgB.height()); + + // compute the difference image + image diff(imgA.width(), imgA.height()); + color avgDiff, maxDiff; + unsigned long diffCount = 0; + + for(image::size_type y=0; y < diff.height(); y++) + for(image::size_type x=0; x < diff.width(); x++) + { + // per pixel difference + color diffColor = imgA(x,y) - imgB(x,y); + diff(x,y) = (extA == "ppm") ? abs(diffColor) : diffColor; + + // if different => update average and max (abs value) + diffColor.abs(); + if(diffColor.r > EPSILON || diffColor.g > EPSILON || diffColor.b > EPSILON) + { + diffCount++; + avgDiff += diffColor; + maxDiff = color(std::max(maxDiff.r, diffColor.r), std::max(maxDiff.g, diffColor.g), std::max(maxDiff.b, diffColor.b)); + } + } + + // output results if different + if(extA == "ppm" && diffCount != 0) + { + avgDiff /= diffCount; + std::cout << " => There are " << diffCount << " pixels that differ." << std::endl; + std::cout << " Average difference: " << avgDiff << " [base 256: " << (avgDiff * 256.0f) << "]" << std::endl; + std::cout << " Maximum difference: " << maxDiff << " [base 256: " << (maxDiff * 256.0f) << "]" << std::endl; + + // write out difference image + exportImage("diff.ppm", diff); + } + + else if(extA == "pfm" && diffCount != 0) + { + avgDiff /= diffCount; + std::cout << " => There are " << diffCount << " pixels that differ." << std::endl; + std::cout << " Average difference: " << avgDiff << std::endl; + std::cout << " Maximum difference: " << maxDiff << std::endl; + + // write out difference image + exportImage("diff.pfm", diff); + } + + // if equal, let the user know + else { + std::cout << " => Both images are equal.\r\n"; + } + + // Done. + return 0; +} |