Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. //Amelia Fass
  2. //Got help from programming lab
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. #include <vector>
  8. #include <iomanip>
  9. using namespace std;
  10. class PgmPicture
  11. {
  12. private:
  13. short picture[1024][1024];
  14. //represents number of rows and columns in pgm image
  15. int numRows, numCols;
  16. //constructor intitializes numrows and numcols to 0
  17. public:
  18. PgmPicture();
  19. //reads pgm file into object, returns false if fails to open inputfile
  20. bool readPgmFile(string inputfile);
  21. //writes pgm image to output file, should return 0(succeeds),-1(pgm image unitnitialized)
  22. //or -2(function failed to open outputfile)
  23. int writePgmFile(string outputFile);
  24. void makeNegative ();
  25. void hflip();
  26. void vflip();
  27. };
  28.  
  29. int main(int argc, char **argv)
  30. {
  31. if(argc != 3) {
  32. perror("fail");
  33. return -1;
  34. }
  35. PgmPicture mypic;
  36. mypic.readPgmFile(argv[1]);
  37. //mypic.makeNegative();
  38. //mypic.hflip();
  39. mypic.vflip();
  40. mypic.writePgmFile(argv[2]);
  41.  
  42. return 0;
  43. }
  44. PgmPicture::PgmPicture()
  45. {
  46. numRows = 0;
  47. numCols = 0;
  48. }
  49.  
  50. bool PgmPicture::readPgmFile(string inputfile)
  51. {
  52. ifstream fin;
  53. int i,j,pixel;
  54. string string;
  55. fin.open(inputfile.c_str());
  56. if (fin.fail()) {
  57. return false;
  58. } else {
  59. fin >>string >> numCols >> numRows>>pixel;
  60. for (i=0; i < numRows; i++) {
  61. for (j=0; j < numCols; j++) {
  62. fin >> pixel;
  63. picture[i][j] = pixel;
  64.  
  65. }
  66. }
  67. fin.close();
  68. return true;
  69.  
  70. }
  71. }
  72. int PgmPicture::writePgmFile(string outputFile)
  73. {
  74. int i,j;
  75. ofstream fout;
  76. fout.open(outputFile.c_str());
  77. if(fout.fail()) {
  78. perror("fail");
  79. return -2;
  80. }
  81. fout << "P2 " << numCols << " " << numRows << endl << "255" <<endl;
  82. for (i = 0; i < numRows; i++) {
  83. for (j=0; j < numCols; j++) {
  84. fout << picture[i][j] << " ";
  85. }
  86. fout << endl;
  87. }
  88. fout.close();
  89. if (numRows == 0) {
  90. perror("Error: Tried to output an uninitialized pgm object");
  91. return -1;
  92. } else
  93.  
  94. return 0;
  95.  
  96. }
  97. void PgmPicture:: makeNegative ()
  98. {
  99. int i,j;
  100. for (i = 0; i < numRows; i++) {
  101. for (j=0; j < numCols; j++) {
  102. picture[i][j] = 255 - picture[i][j];
  103. }
  104. }
  105. return;
  106. }
  107. void PgmPicture::hflip(){
  108. int i,j;
  109. short tmp;
  110. for ( i = 0; i < numRows; i++){
  111. for(j = 0;j < (numCols/2);j++){
  112. tmp=picture[i][j];
  113. picture[i][j]=picture[i][numCols-j-1];
  114. picture[i][numCols-j-1]=tmp;
  115.  
  116. }
  117. }
  118. return;
  119. }
  120. void PgmPicture::vflip(){
  121. int i,j;
  122. short tmp;
  123. for ( i = 0; i < (numRows/2); i++){
  124. for(j = 0;j < numCols;j++){
  125. tmp=picture[i][j];
  126. picture[i][j]=picture[numRows-i][j];
  127. picture[numRows-i][j]=tmp;
  128. }
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement