#include #include #include #include "CPstring.h" #include "pixmap.h" #include "date.h" #include "vector.h" #include "rando.h" // author: Owen Astrachan // modified 3/21/94 // modified 11/29/94 // modified 4/13/95 // // revised: 9/25/1996, move some functions to private section, // clean up reading (Michael Loeb) // // // implementation of pixmap class for manipulating graphics images static const int MAX_ROWS = 500; static const int MAX_COLS = 500; static const int LINE_SIZE = 20; // for output lines (#'s per line) string olaCreator = "# CREATOR: ola pixmap program v 1.1 : "; Pixmap::Pixmap() // postcondition: initialized to 0x0 pixmap of unknown creation and kind { myKind = "unknown"; myCreator = "unknown"; } Pixmap::Pixmap(int rows, int cols) : myImage(rows, cols) { myKind = "unknown"; myCreator = "unknown"; } Pixmap::~Pixmap() { // nothing needs to be done with current implementation } void Pixmap::Read(istream & input) // description: // assume in pbm or pgm format: first line is P1 or P2 (bit-map/gray scale) // second line is comment of creating program // third line is cols, rows (ints) // data follows as space separated numbers: 0,1 for P1, 0-255 for P2 // // precondition: input is open for reading // postcondition: file processed into a pixmap // exceptions: // if reading fails, pixmap is garbage NO ERROR CHECKING DONE { if (input) { int rows, cols; getline(input,myKind); // read P1, P2, etc getline(input,myCreator); // creating program input >> cols >> rows; // rows and cols myImage.SetSize(rows, cols); int j,k; if (myKind == "C1") // reading compressed file { ReadCompressedFile(input); // Read the file } else { ReadNormalFile(input); } } } void Pixmap::ReadCompressedFile(istream & input) // preconditions: 1. input is open for reading // 2. input is in compressed format // 3. The first thing we read is "the actual compressed data" // postcondition: input is stored in Matrix in uncompressed format { myKind = "P1"; // store file as "P1" (uncompressed) myGrayLevel = 1; // default is black/white // You get to fill in this function to read a commpressed data file RandomImage(); } void Pixmap::ReadNormalFile(istream & input) // preconditions: 1. input is open for reading // 2. input is in uncompresed format // 3. The first thing we read is "the actual data" // postcondition: input is stored in Matrix in uncompressed format { if (myKind == "P2") { input >> myGrayLevel; } else { myGrayLevel = 1; // default is black/white } // Replace this call with your code to read the data from a file RandomImage(); } void Pixmap::RandomImage() // postcondition: myImage is filled with random black/white pixels { RandGen randgen; //Random Number generator int j; int k; for (j=0;j= myImage.Cols()) { c = 0; r++; } } output << " " << count << endl; } void Pixmap::HorizReflect() // postcondition: map is reflected horizontally { cout << " **** NOT implemented" << endl; } void Pixmap::VertReflect() // postcondition: map is reflected vertically { int k; for(k=0; k < myImage.Rows(); k++) { int j; int limit = myImage.Cols()/2; // halfway along (columns swapped) for(j=0; j < limit; j++) { int temp = myImage[k][j]; myImage[k][j] = myImage[k][myImage.Cols() - j - 1]; myImage[k][myImage.Cols() - j - 1] = temp; } } } void Pixmap::Invert() // postcondition: map is inverted { cout << " **** NOT implemented" << endl; } int Median(Vector & list, int size) // precondition: size = # elements in list // postcondition: returns middle element of list // (also sorts list) { int j,k; int min,temp; for (j=0; j < size; j++) { min = j; for(k=j+1; k < size; k++) { if (list[k] < list[min]) { min = k; } } temp = list[min]; list[min] = list[j]; list[j] = temp; } return list[size/2]; } void Pixmap::Enhance(int size) // postcondition: median filtering done on image { static unsigned short local[MAX_ROWS][MAX_COLS]; // local array to store // medians Vector list(size*size); // List of Neigbors points used to compute // Median int j,k,m,n; int count=0; if (myImage.Rows() >MAX_ROWS || myImage.Cols() > MAX_COLS) { cerr << "Unable to enhance Image because it's too big" << endl; return; } cout << "enhancing image" << endl; for(j=0; j < myImage.Rows(); j++) { for(k=0; k < myImage.Cols(); k++) { count = 0; // Get all the Neighbor Points and put them into a list for(m=j-size/2; m <= j+size/2; m++) { for(n=k-size/2; n <= k+size/2; n++) { if (0 <= m && 0 <= n && m < myImage.Rows() && n < myImage.Cols()) { list[count] = myImage[m][n]; count++; } } } //Compute median of list local[j][k] = Median(list,count); } } // Replace old values with medians cout << "copying back" << endl; for(j=0; j < myImage.Rows(); j++) { for(k=0; k < myImage.Cols(); k++) { myImage[j][k] = local[j][k]; } } } void Pixmap::Expand(int rowExp, int colExp) { // not implemented cout << endl << " **** NOT implemented" << endl << endl; }