//main.cpp#include #include #include #include #include "Image.h"#include #include Image readPPM(const char* file);void writePPM(const Image &img, const char* file);using namespace std;Image CalculateMean(vector set1);Image CalculateMedian(vector set1);Image CalculateSigmaMean(vector set1);int main(){ std::cout << "************************************" << std::endl; std::cout << "Image Stacker / Image Scaler" << std::endl; std::cout << "************************************" << std::endl; //**************************************************** //reading PPM files //Specifing the image dimensions (checked from opening images in notepad ++) //**************************************************** Image *img1 = new Image(3264, 2448); *img1 = readPPM("Images/ImageStacker_set1/IMG_1.ppm"); Image *img2 = new Image(3264, 2448); *img2 = readPPM("Images/ImageStacker_set1/IMG_2.

ppm");  Image *img3 = new Image(3264, 2448); *img3 = readPPM("Images/ImageStacker_set1/IMG_3.ppm");  Image *img4 = new Image(3264, 2448); *img4 = readPPM("Images/ImageStacker_set1/IMG_4.ppm");  Image *img5 = new Image(3264, 2448); *img5 = readPPM("Images/ImageStacker_set1/IMG_5.ppm");  Image *img6 = new Image(3264, 2448); *img6 = readPPM("Images/ImageStacker_set1/IMG_6.ppm");  Image *img7 = new Image(3264, 2448); *img7 = readPPM("Images/ImageStacker_set1/IMG_7.

ppm");  Image *img8 = new Image(3264, 2448); *img8 = readPPM("Images/ImageStacker_set1/IMG_8.ppm"); Image *img12 = new Image(3264, 2448); *img12 = readPPM("Images/ImageStacker_set1/IMG_9.ppm"); Image *img9 = new Image(3264, 2448); *img9 = readPPM("Images/ImageStacker_set1/IMG_10.ppm");   Image *img10 = new Image(3264, 2448); *img10 = readPPM("Images/ImageStacker_set1/IMG_12.ppm");    Image *img11 = new Image(3264, 2448); *img11 = readPPM("Images/ImageStacker_set1/IMG_11.

ppm");  Image *img13 = new Image(3264, 2448); *img13 = readPPM("Images/ImageStacker_set1/IMG_13.ppm"); //You can access the individual pixel data //This accesses and writes out the r, g, b pixel values for the first pixel //Values are multiplied by 255 as they are 'clamped' between 0 and 1 cout<< "image 1 colour frequency (in respect to red, green, blue)"<< std::endl; cout << (img1->pixels0.r)*255 << std::endl; cout << (img1->pixels0.g)*255 << std::endl; cout << (img1->pixels0.b)*255 << std::endl; cout<< "image 2 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img2->pixels0.

r)*255 << std::endl; cout << (img2->pixels0.g)*255 << std::endl; cout << (img2->pixels0.b)*255 << std::endl;  cout<< "image 3 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img3->pixels0.r)*255 << std::endl; cout << (img3->pixels0.g)*255 << std::endl; cout << (img3->pixels0.

b)*255 << std::endl; cout<< "image 4 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img4->pixels0.r)*255 << std::endl; cout << (img4->pixels0.g)*255 << std::endl; cout << (img4->pixels0.b)*255 << std::endl; cout<< "image 5 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img5->pixels0.r)*255 << std::endl; cout << (img5->pixels0.g)*255 << std::endl; cout << (img5->pixels0.

b)*255 << std::endl; cout<< "image 6 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img6->pixels0.r)*255 << std::endl; cout << (img6->pixels0.g)*255 << std::endl; cout << (img6->pixels0.b)*255 << std::endl;  cout<< "image 7 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img7->pixels0.

r)*255 << std::endl; cout << (img7->pixels0.g)*255 << std::endl; cout << (img3->pixels0.b)*255 << std::endl; cout<< "image 8 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img8->pixels0.r)*255 << std::endl; cout << (img8->pixels0.g)*255 << std::endl; cout << (img8->pixels0.

b)*255 << std::endl; cout<< "image 9 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img9->pixels0.r)*255 << std::endl; cout << (img9->pixels0.g)*255 << std::endl; cout << (img9->pixels0.b)*255 << std::endl; cout<< "image 10 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img10->pixels0.

r)*255 << std::endl; cout << (img10->pixels0.g)*255 << std::endl; cout << (img10->pixels0.b)*255 << std::endl;  cout<< "image 11 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img11->pixels0.r)*255 << std::endl; cout << (img11->pixels0.g)*255 << std::endl; cout << (img11->pixels0.b)*255 << std::endl; cout<< "image 12 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img12->pixels0.

r)*255 << std::endl; cout << (img12->pixels0.g)*255 << std::endl; cout << (img12->pixels0.b)*255 << std::endl; cout<< "image 13 colour frequency (in respect to red, green, blue"<< std::endl; cout << (img13->pixels0.r)*255 << std::endl; cout << (img13->pixels0.

g)*255 << std::endl; cout << (img13->pixels0.b)*255 << std::endl; vector set1; set1.push_back(*img1); set1.push_back(*img2); set1.

push_back(*img3); set1.push_back(*img4); set1.push_back(*img5); set1.push_back(*img6); set1.push_back(*img7); set1.

push_back(*img8); set1.push_back(*img9); set1.push_back(*img10); set1.push_back(*img11); set1.

push_back(*img12); set1.push_back(*img13);    long begin,end;    ifstream myfile ("main.cpp");    begin = myfile.tellg();    myfile.

seekg (0, ios::end);    end = myfile.tellg();    myfile.close();    cout << "File size is: " << (end-begin) << " bytes.";    Image mean = Image(3264,2448);    mean = CalculateMean(set1);    writePPM(mean, "Set of images have been mean blended");        Image med = Image(3264,2448);    med = CalculateMedian(set1);    writePPM(med, "images median blended");    Image sigmaImage = Image(3264,2448);    sigmaImage = CalculateSigmaMean(set1);    writePPM(sigmaImage, "set of images that have been sigma clipped");}Image CalculateMean(vector set1){    Image *mean = new Image(set10.w, set10.h);    Image::Rgb rgb = Image::Rgb();    for (int pixel = 0; pixel <= (mean->h * mean->w); pixel++)      {          for (int files = 0; files < set1.

size(); files++)          {              rgb += set1files.pixelspixel;          }          mean->pixelspixel.r = rgb.r/13;          mean->pixelspixel.g = rgb.

g/13;          mean->pixelspixel.b = rgb.b/13;          rgb = Image::Rgb();      }      return *mean;  }Image CalculateMedian(vector set1){     Image *med = new Image(set10.w, set10.h);     Image::Rgb rgb = Image::Rgb();      for (int pixel = 0; pixel < (med->h * med->w); pixel++)      {          for (int files = 0; files < set1.

size(); files++)          {              rgb += set1files.pixelspixel;          }          med->pixelspixel.r = rgb.r /(13/2);          med->pixelspixel.g = rgb.

g /(13/2);          med->pixelspixel.b = rgb.b /(13/2);          rgb = Image::Rgb();      }      return *medImage;}Image CalculateSigmaMean(vector set1){     Image *sig = new Image(set10.w, set10.h);     Image::Rgb rgb = Image::Rgb();      for (int pixel = 0; pixel < (sig->h * sig->w); pixel++)      {          for (int files = 0; files < set1.size(); files++)          {              rgb += imgsfiles.

pixelspixel;          }          sig->pixelspixel.r = rgb.r /(13*2);          sig->pixelspixel.g = rgb.g /(13*2);          sig->pixelspixel.b = rgb.b /(13*2);          rgb = Image::Rgb();      }      return *sigmaImage;}//Read ppm files into the codeImage readPPM(const char *filename){ cout << "Reading image ..." << std::endl; ifstream ifs; ifs.open(filename, std::ios::binary); Image src; try {  if (ifs.fail()) {   throw("Can't open the input file - is it named correctly/is it in the right directory?");  }  string header;  int w, h, b;  ifs >> header;  if (strcmp(header.c_str(), "P6") != 0) throw("Can't read the input file - is it in binary format (Has P6 in the header)?");  ifs >> w >> h >> b;  src.w = w;  src.h = h;  //std::cout << w << " " << h << std::endl;  src.pixels = new Image::Rgbw * h; // this is throw an exception if bad_alloc   ifs.ignore(255, ''); // skip empty lines in necessary until we get to the binary data   unsigned char pix3; // read each pixel one by one and convert bytes to floats   for (int i = 0; i < w * h; ++i) {   ifs.read(reinterpret_cast(pix), 3);   src.pixelsi.r = pix0 / 255.f;   src.pixelsi.g = pix1 / 255.f;   src.pixelsi.b = pix2 / 255.f;  }  ifs.close(); } catch (const char *err) {  fprintf(stderr, "%s", err);  ifs.close(); } //Confirm image read //Delete this to prevent multiple lines output cout << "Image read" << std::endl; return src;}//Write data out to a ppm file//Constructs the header as abovevoid writePPM(const Image &img, const char *filename){ //std::cout << filename << std::endl; cout << "Writing image ..." << std::endl; if (img.w == 0 || img.h == 0) { fprintf(stderr, "Can't save an empty image"); return; } ofstream ofs; try {  ofs.open(filename, std::ios::binary); // need to spec. binary mode for Windows users   if (ofs.fail()) throw("Can't open output file");  ofs << "P6" << img.w << " " << img.h << "255";  //std::cout << "P6" << img.w << " " << img.h << "255";  unsigned char r, g, b;  // loop over each pixel in the image, clamp and convert to byte format  for (int i = 0; i <= img.w * img.h; ++i) {   r = static_cast(std::min(1.f, img.pixelsi.r) * 255);   g = static_cast(std::min(1.f, img.pixelsi.g) * 255);   b = static_cast(std::min(1.f, img.pixelsi.b) * 255);   ofs << r << g << b;  }  ofs.close();  //Confirm image write  cout << "Image written" << std::endl; } catch (const char *err) {  fprintf(stderr, "%s", err);  ofs.close(); }}