Advertisement
Guest User

Pixelation Program

a guest
Feb 13th, 2013
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.10 KB | None | 0 0
  1. //╔═══════════════════════════════════════════╗\\
  2. //║ Alex Braniff - CpSc246 Adv Programming    ║\\
  3. //║ Spring 2013 - Pixelating PPM Files        ║\\
  4. //╚═══════════════════════════════════════════╝\\
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. //My bag to hold pixel information
  14. struct Pixel{
  15.     int red, green, blue;
  16. };
  17.  
  18. //╔═══════════════════════════════════════════╗\\
  19. //║ Global Variable Library                   ║\\
  20. //╚═══════════════════════════════════════════╝\\
  21.  
  22. const int MAXCOLS = 882;
  23. const int MAXROWS = 883;
  24. const int PSEUDOMAXROWS = 50;
  25.  
  26. //╔═══════════════════════════════════════════╗\\
  27. //║ Function Prototypes                       ║\\
  28. //╚═══════════════════════════════════════════╝\\
  29.  
  30. void initialize(            bool& FLAG,
  31.                             int& squareSize,
  32.                             string& inFileName,
  33.                             string& outFileName,
  34.                             ifstream& inFile,
  35.                             ofstream& outFile,
  36.                             int& numRows,
  37.                             int& numCols,
  38.                             int& maxColor
  39.                             );
  40.  
  41. void readPixelStripe(       Pixel pixArray[][MAXCOLS],
  42.                             int stripe,
  43.                             int squareSize,
  44.                             ifstream& inFile
  45.                             );
  46.  
  47. void readPixel(             Pixel pixArray[][MAXCOLS],
  48.                             int stripe,
  49.                             int row,
  50.                             int col,
  51.                             ifstream& inFile
  52.                             );
  53.  
  54. void getAveragesForRGB(     Pixel pixArray[][MAXCOLS],
  55.                             int squareSize,
  56.                             int square,
  57.                             int numSquaresPerStripe
  58.                             );
  59.  
  60. void writePixel(            int redAvg,
  61.                             int greenAvg,
  62.                             int blueAvg,
  63.                             ofstream& outFile
  64.                             );
  65.  
  66. void getUserPreferences(    bool& FLAG,
  67.                             int& squareSize,
  68.                             string& inFileName,
  69.                             string& outFileName,
  70.                             ifstream& inFile,
  71.                             ofstream& outFile
  72.                             );
  73.  
  74. void printHeading();
  75.  
  76. //╔═══════════════════════════════════════════╗\\
  77. //║ Main                                      ║\\
  78. //╚═══════════════════════════════════════════╝\\
  79.  
  80. int main(){
  81.  
  82. //═════════════Variable Dictionary═════════════\\
  83.  
  84. Pixel pixArray[PSEUDOMAXROWS][MAXCOLS];     //store pixel info for a stripe
  85.  
  86. int numRows, numCols, maxColor;         //ppm file attributes
  87. int squareSize;                         //store how big each square should be
  88.  
  89. bool FLAG = 0;                          //generic FLAG variable
  90.  
  91. string inFileName;                      //hold path to inFile from user input
  92. string outFileName;                     //hold path to outFile from user input
  93.  
  94. ifstream inFile;                        //input file
  95. ofstream outFile;                       //output file
  96.  
  97.  
  98. //go through initialization of key components of program
  99. initialize(FLAG, squareSize, inFileName, outFileName, inFile, outFile, numRows, numCols, maxColor);
  100.  
  101. cout << "\n\nProcessing image\n\n";
  102.  
  103. //set some variables up with useful info
  104. int numStripes = MAXCOLS / squareSize;          //number of stripes in image
  105. int numSquaresPerStripe = MAXROWS / squareSize; //number of squares in each stripe
  106.  
  107. //initialize averages for future use
  108. int redAvg, greenAvg, blueAvg;
  109.  
  110. for(int stripe=0;stripe<=numStripes;stripe++){//go through each stripe
  111.     cout << "\r" << stripe << " out of " << numStripes;//let user know we're working
  112.     readPixelStripe(pixArray, stripe, squareSize, inFile);//read in one stripe
  113.     for(int square=0;square<numSquaresPerStripe;square++){//go through each square in each stripe
  114.         getAveragesForRGB(pixArray, squareSize, square, numSquaresPerStripe);//find the average RGB value for the current square and replace array data
  115.     }
  116.     for(int row=0;row<squareSize;row++){
  117.         for(int col=0;col<MAXCOLS;col++){//write each average in the stripe to the outFile
  118.             writePixel(pixArray[row][col].red, pixArray[row][col].green, pixArray[row][col].blue, outFile);
  119.         }
  120.     }
  121. }
  122.  
  123. inFile.close(); //must close files
  124. outFile.close();
  125.  
  126. //need this to denote a good termination
  127. cout<<"\n\n\n\t\tNormal Termination ***\n";
  128.  
  129. //allow user to see stuff on the screen
  130. system("PAUSE");
  131. return 0;
  132. }//end of main
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. //C:\Development\C++\CPSC246\Braniff.ppm
  143. //C:\Development\C++\CPSC246\ffinarB.ppm
  144.  
  145. //══════════════════Initialize══════════════════\\
  146.  
  147. void initialize(bool& FLAG, int& squareSize, string& inFileName, string& outFileName, ifstream& inFile, ofstream& outFile, int& numRows, int& numCols, int& maxColor){
  148.     //This routine will setup our files and process PPM header
  149.     string str;  //string to hold header info
  150.  
  151.     //set the attributes to zero: then if any of the
  152.     // file i/o goes bad, the caller will have zero data
  153.     numRows = numCols = maxColor = 0;
  154.  
  155.     printHeading(); //handles mandatory CpSc246 Headers
  156.  
  157.     getUserPreferences(FLAG, squareSize, inFileName, outFileName, inFile, outFile);//gets input output files
  158.  
  159.     //both files opened, so process (and verify) the header info
  160.     getline(inFile,str);    //must start with "P3"
  161.     if (!(str.compare("P3") == 0)) {//error
  162.         cerr<<"Bad file header: not P3\n";
  163.         inFile.close();
  164.         outFile.close();
  165.         return;
  166.     } //okay
  167.     else //we're okay - so write header to new file
  168.         outFile<<str<<endl;
  169.  
  170.     //get the comment line
  171.     getline(inFile,str);    //get comment
  172.     if (str.at(0) != '#'){//error
  173.         cerr<<"Missing comment in line 2\n";
  174.         inFile.close();
  175.         outFile.close();
  176.         return;
  177.     }
  178.     else //okay
  179.         outFile<<str<<endl;  //write it out to new file
  180.  
  181.     //get file attributes & put to new file, too.
  182.     inFile >> numCols >> numRows >> maxColor;
  183.     outFile << numCols << ' ' << numRows << endl
  184.     << maxColor << endl;
  185.     }//initialize
  186.    
  187.     //get user input for file paths and squareSize
  188.     void getUserPreferences(bool& FLAG, int& squareSize, string& inFileName, string& outFileName, ifstream& inFile, ofstream& outFile){
  189.     while(FLAG == 0){//Get input file path
  190.         cout << "\nPath of file to be pixelated: ";
  191.         cin >> inFileName;//placeholder for file path
  192.         inFile.open(inFileName.c_str());//open file using the string inputted
  193.         if(inFile.is_open()){//make sure input file exists
  194.             FLAG = 1;//get out of loop
  195.         }else{//reset loop
  196.             inFileName = "";
  197.             inFile.close();
  198.             cout << "\nFile does not exist.\n";
  199.         }
  200.     }
  201.  
  202.     //reset flag variable for use again
  203.     FLAG = 0;
  204.     int areYouSure = 0;//make sure user wants to use this file
  205.     while(FLAG == 0){//get output file path
  206.         cout << "\nPath of file to be written: ";
  207.         cin >> outFileName;//placeholder for file path
  208.         outFile.open(outFileName.c_str());//open file using the string inputted
  209.         if(outFile.is_open()){//make sure output file path exists
  210.             cout << "\nAre you sure?\n0 - NO\n1 - YES\n\n";
  211.             cin >> areYouSure;
  212.             if(areYouSure != 1){//reset loop if not sure
  213.                 areYouSure = 0;
  214.                 outFileName = "";
  215.                 outFile.close();
  216.             }else{//get out of loop
  217.                 FLAG = 1;
  218.             }
  219.         }else{//get out of loop
  220.             FLAG = 1;
  221.         }
  222.     }
  223.  
  224.     //reset flag variable for use again
  225.     FLAG = 0;
  226.     while(FLAG == 0){//get size of squares to be pixelated
  227.         cout << "\nHow big do you want the pixelated squares to be? (15-50)\n";
  228.         cin >> squareSize;
  229.         if(squareSize <= 50 && squareSize >=15){//make sure nuber is between 15 and 50
  230.             FLAG = 1;//get out of loop
  231.         }else{//reset loop for reiteration
  232.             squareSize = 0;
  233.             cout << "\nThe value must be between 15 and 50.\n";
  234.         }
  235.     }
  236. }
  237.  
  238.  
  239.  
  240.  
  241.    
  242. //═══════════════════Heading════════════════════\\
  243.  
  244. //outputs headin information required for course
  245. void printHeading(){
  246.     cout << "+============================================+\n";
  247.     cout << "+===Alex Braniff===CpSc246 Adv Programming===+\n";
  248.     cout << "+===Spring 2013=======Pixelating PPM Files===+\n";
  249.     cout << "+============================================+\n";
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256. //══════════════════Read Stripe═══════════════════\\
  257.  
  258. //read in one stripe of pixels to pixArray[][]
  259. void readPixelStripe(Pixel pixArray[][MAXCOLS], int stripe, int squareSize, ifstream& inFile){
  260.     for(int row=0;row<squareSize;row++){
  261.         for(int col=0;col<MAXCOLS;col++){
  262.             readPixel(pixArray, stripe, row, col, inFile);
  263.         }
  264.     }
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. //══════════════════Averages═══════════════════\\
  272.  
  273. void getAveragesForRGB(Pixel pixArray[][MAXCOLS], int squareSize, int square, int numSquaresPerStripe){
  274.  
  275.     //initialize variables needed for function
  276.     int start, stop;
  277.     int redAvg, greenAvg, blueAvg;
  278.  
  279.     //reset averages for current square's usage
  280.     redAvg = 0;
  281.     greenAvg = 0;
  282.     blueAvg = 0;
  283.  
  284.     start = 0;//reset start number
  285.     for(int row=0;row<squareSize;row++){
  286.         if(row != 0)//adds one to the start as long as it doesn't NEED to be 0
  287.             start = ((square * MAXROWS) / (MAXCOLS / squareSize)) + 1;//starting point for loop over the columns
  288.         stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));//stopping point for   ^^^
  289.         for(int col=start;col<stop;col++){
  290.             //add each rgb value to the sum to be divided later
  291.             redAvg += pixArray[row][col].red;
  292.             greenAvg += pixArray[row][col].green;
  293.             blueAvg += pixArray[row][col].blue;
  294.         }
  295.     }
  296.  
  297.     //divide by number of pixels in square for average
  298.     redAvg /= (squareSize * squareSize);
  299.     greenAvg /= (squareSize * squareSize);
  300.     blueAvg /= (squareSize * squareSize);
  301.  
  302.  
  303.     start = 0;//reset start number
  304.     for(int row=0;row<squareSize;row++){
  305.         if(row != 0)
  306.             start = ((square * MAXROWS) / (MAXCOLS / squareSize)) + 1;
  307.         stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));
  308.         for(int col=start;col<stop;col++){
  309.             //write each average into each piece of the array
  310.             pixArray[row][col].red = redAvg;
  311.             pixArray[row][col].green = greenAvg;
  312.             pixArray[row][col].blue = blueAvg;
  313.         }
  314.     }
  315. }
  316.  
  317.  
  318.  
  319.  
  320.  
  321. //══════════════════Read Pixel═══════════════════\\
  322.  
  323. //reads pixel information from the inFile
  324. void readPixel(Pixel pixArray[][MAXCOLS], int stripe, int row, int col, ifstream& inFile){
  325.     inFile>>pixArray[row][col].red>>pixArray[row][col].green>>pixArray[row][col].blue;
  326.     string stringReturn = to_string(pixArray[row][col].red) + "-" + to_string(pixArray[row][col].green) + "-" + to_string(pixArray[row][col].blue);
  327. }
  328.  
  329.  
  330.  
  331.  
  332.  
  333. //══════════════════Write Pixel══════════════════\\
  334.  
  335. //takes three integers and writes them to the outFile as pixel info
  336. void writePixel(int redAvg, int greenAvg, int blueAvg, ofstream& outFile){
  337.     outFile << redAvg << ' '    //write 3 values (RGB) per line
  338.     << greenAvg << ' '
  339.     << blueAvg << endl;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement