summaryrefslogblamecommitdiff
path: root/hw6/bin/image_compare.cpp
blob: 4f1a798032de2f718873e05b4e2e7a3ec975e962 (plain) (tree)

























































































                                                                                                                              
#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;
}