Question
#include #include #include #include using namespace std; string get_filename(); void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix); void rotate_a_ninety_degree_PGM(int nc, int
#include
using namespace std;
string get_filename(); void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix); void rotate_a_ninety_degree_PGM(int nc, int nr, int mv, int *pix); void writePGM(string fname, int nc, int nr, int mv, int *pix);
//.......................................
int main() { int ncols = 0, nrows = 0, maxval = 0;//, maxval = 255; int *pix = NULL; // instead of static int pix[1024*1024]; string filename = get_filename();
readPGM(filename, ncols, nrows, maxval, *&pix); rotate_a_ninety_degree_PGM(ncols, nrows, maxval, *&pix); writePGM("90degree_rotation_" + filename, ncols, nrows, maxval, *&pix);
delete[] pix; pix = NULL; // add code to delete the pixel array here! }
//.......................................
// string get_filename() { bool ok = false; string fn = ""; do { if (fn.size() == 0) cout << "filename? "; else cout << "can't open " << fn << ". re-enter filename: "; cin >> fn; ifstream ifs(fn); // make sure we can open the file. if (ifs) ok = true; ifs.close(); } while (!ok); //ifs.close(); return fn; }
//............................
// void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix) { // you need to write this! delete[] pix; //pix = NULL; ifstream ifs; string p2; ifs.open(fname);
ifs >> p2 >> nc >> nr >> mv; //stor the number in the varb.
pix = new int[nc*nr]; //the set up size of array //cout << "The value of area is " << (nc*nr) << endl; for (int i = 0; i < nc*nr; i++) // store the numbers in to array { ifs >> pix[i]; }
ifs.close(); cout << "The max number is " << mv << endl; cout << "The col value is " << nc << endl; cout << "The row value is " << nr << endl; /* for (int i = 0; i < nc*nr; i++) { cout << setw(5) << *pix++; }*/ //ifs.close(); }
//.......................................
// void writePGM(string fname, int nc, int nr, int mv, int *pix) { // you need to write this ! ofstream ifs; ifs.open(fname);
ifs << "P2" << " " << nc << " " << nr << " " << mv << endl; cout << endl; cout << endl;
for (int i = 0; i < nc*nr; i++) //write down the numbers in the file { ifs << setw(5) << pix[i]++; //cout << setw (5) << pix[i]; }
ifs.close();
} //.......................................
void rotate_a_ninety_degree_PGM(int nc, int nr, int maxval, int *pix) {
for (int r = 0; r < nr; r++) {
for (int c = 0; c < nc; c++) { pix[c*nr+(nr-r-1)] = pix[r*nc+c]; } } /*for (int i = 0; i < nc*nr; i++) { cout << setw(5) << *pix++; }*/ }
Is C++ code, How can I using pointer array to rotate 90 degree of my pgm image?
Here is my code, I don't know how to fix my code. Also, I need help find 270 degrees and 180 degrees?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started