summaryrefslogtreecommitdiff
path: root/hw2/bin/image_compare.cpp
blob: 4f1a798032de2f718873e05b4e2e7a3ec975e962 (plain) (blame)
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
#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;
}