// image.cpp // Functions required for MSc Project // Do not alter the contents of this file - simply compile with your program // if your program is called histo, then to compile: // g++ -o histo histo.cpp image.cpp #include #include #include "image.h" void write_image(ofstream &fo, unsigned char image[][256]) { int i; fo << "P5\n"; fo << "256\t256\n255\n"; for (i = 0;i < 256; i++) { fo.write((const char *) image[i],256); } } void read_image(ifstream &fi, unsigned char image[][256]) { char buff[100]; int i; next_line(fi,buff); if (strncmp("P5",buff,2)!=0) { cerr << "wrong input file format\n"; exit(1); } next_line(fi, buff); next_line(fi, buff); for (i=0; i< 256 ; i++) { fi.read((char *) image[i], 256); } } void next_line (ifstream &fptr, char *s) { // Ignores #lines and returns next line of input file in char_buff[] do { fptr.getline(s,100); } while (s[0]=='#'); }