// Pixmap.cpp Dave Reed // // Implementation of Pixmap class for manipulating images //////////////////////////////////////////////////////////////// #include #include #include #include "Pixmap.h" #include "Pixel.h" using namespace std; Pixmap::Pixmap() // constructor -- initializes empty pixel-map { kind = "unknown"; creator = "unknown"; numRows = 0; numCols = 0; } void Pixmap::Read(istream & input) // Assumes: input file contains a pixel-map // Results: initializes empty pixel-map with file contents { if (input){ getline(input, kind); // read P1, P2, P3, etc if (kind == "P1" || kind == "P2" || kind == "P3") { getline(input, creator); input >> numCols >> numRows; image.resize(numRows, numCols); if (kind == "P2" || kind == "P3") { input >> maxValue; } for(int j=0; j < numRows; j++){ for(int k=0; k < numCols; k++){ if (kind == "P1") { image[j][k] = new MonoPixel(); } else if (kind == "P2") { image[j][k] = new GrayPixel(maxValue); } else if (kind == "P3") { image[j][k] = new ColorPixel(maxValue); } image[j][k]->Read(input); } } } else { cout << "UNKNOWN FILE TYPE" << endl; } } else { cout << "FILE NOT FOUND" << endl; } } void Pixmap::HorizReflect() // Results: reflects picture along x-axis { for (int row = 0; row < numRows/2; row++) { for (int col = 0; col < numCols; col++) { Pixel * temp = image[row][col]; image[row][col] = image[numRows-row-1][col]; image[numRows-row-1][col] = temp; } } } void Pixmap::VertReflect() // Results: reflects picture along y-axis { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols/2; col++) { Pixel * temp = image[row][col]; image[row][col] = image[row][numCols-col-1]; image[row][numCols-col-1] = temp; } } } void Pixmap::Invert() // Results: inverts picture by setting pixel = maxPixel-pixel { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { image[row][col]->Invert(); } } } void Pixmap::Write(ostream & output) // Assumes: output is open for writing // Results: Pixmap information is written to output { output << kind << endl; output << "# CREATOR: reedd pix program v 2.2" << endl; output << numCols << " " << numRows << endl; if (kind == "P2" || kind == "P3") { output << maxValue << endl; } for (int j=0; j < numRows; j++){ for (int k=0; k < numCols; k++){ image[j][k]->Write(output); output << " "; } output << endl; } }