Advertisement
Guest User

pgmUtility.c

a guest
Mar 11th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include "pgmUtility.h"
  6.  
  7. int main(int argc, char *argv[]){
  8.  
  9. /* args useage: ./programName –e edgeWidth       originalImage   newImageFile
  10.         ./programName –c circleCenterRow circleCenterCol radius       originalImage newImageFile
  11.        
  12.         //Extra Credit
  13.         ./programName -­‐ce    circleCenterRow circleCenterCol radius edgeWidth originalImage newImageFile
  14.         ./programName -­‐c -­‐e circleCenterRow circleCenterCol radius edgeWidth originalImage newImageFile
  15. */
  16.     FILE * fp;
  17.     FILE * out; //this will get one of the items stored in argv  
  18. //needs to read in command line arguments.....make sure it can take -ce and -c -e in order to get massive extra credit
  19.     char ** header = (char**) malloc( sizeof(char *) * 4);
  20.     int i;
  21.     int ** pixels;
  22.     for(i = 0; i < 4; i++){
  23.         header[i] = (char *) malloc (sizeof(char) * 100);
  24.     }
  25.     int numRows, numCols;
  26.  
  27.     int m, n, l, x, ch;
  28.         int edgeWidth, circleCenterRow, circleCenterCol, radius;
  29.     char originalImage[100], newImageFile[100];
  30.     if(argc != 5 && argc != 7)
  31.         printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  32.     for( n = 1; n < argc; n++ )            /* Scan through args. */
  33.         {
  34.              switch( (int)argv[n][0] )            /* Check for option character. */
  35.              {
  36.              case '-':x = 0;                   /* Bail out if 1. */
  37.                       l = strlen( argv[n] );
  38.                       for( m = 1; m < l; ++m ) /* Scan through options. */
  39.                       {
  40.                          ch = (int)argv[n][m];
  41.                          switch( ch )
  42.                          {
  43.                      case 'c':  if(argc != 7){
  44.                             printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  45.                             break;
  46.                             }
  47.                             circleCenterRow = atoi(argv[2]);
  48.                             circleCenterCol = atoi(argv[3]);
  49.                             printf("%d   %d\n\n", circleCenterRow, circleCenterCol);
  50.                             radius = atoi(argv[4]);
  51.                             strcpy(originalImage, argv[5]);
  52.                             strcpy(newImageFile, argv[6]);
  53.                             fp = fopen(originalImage, "r");
  54.                             out = fopen(newImageFile, "w");
  55.  
  56.                             pixels = pgmRead(header, &numRows, &numCols, fp);
  57.                             printf("%s", header[3]);
  58.  
  59.                             pgmDrawCircle(pixels, numRows, numCols, circleCenterRow, circleCenterCol, radius, header );
  60.                             pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );                 
  61.                             break;
  62.                            
  63.                      case 'C':  if(argc != 7){
  64.                             printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  65.                             break;
  66.                             }
  67.                             circleCenterRow = atoi(argv[2]);
  68.                             circleCenterCol = atoi(argv[3]);
  69.                             radius = atoi(argv[4]);
  70.                             strcpy(originalImage, argv[5]);
  71.                             strcpy(newImageFile, argv[6]);
  72.                             fp = fopen(originalImage, "r");
  73.                             out = fopen(newImageFile, "w");
  74.                             pixels = pgmRead(header, &numRows, &numCols, fp);
  75.                             pgmDrawCircle(pixels, numRows, numCols, circleCenterRow, circleCenterCol, radius, header );
  76.                             pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
  77.                             break;
  78.                      case 'E':  if(argc != 5){
  79.                             printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  80.                             break;
  81.                             }
  82.                             edgeWidth = atoi(argv[2]);
  83.                             strcpy(originalImage, argv[3]);
  84.                             strcpy(newImageFile, argv[4]);
  85.                             fp = fopen(originalImage, "r");
  86.                             out = fopen(newImageFile, "w");
  87.                             pixels = pgmRead(header, &numRows, &numCols, fp);
  88.                             pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
  89.                             pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
  90.                             break;
  91.                            
  92.                              case 'e':  if(argc != 5){
  93.                             printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  94.                             break;
  95.                             }
  96.                                     edgeWidth = atoi(argv[2]);
  97.                             strcpy(originalImage, argv[3]);
  98.                             strcpy(newImageFile, argv[4]);
  99.                             fp = fopen(originalImage, "r");
  100.                             out = fopen(newImageFile, "w");
  101.                             pixels = pgmRead(header, &numRows, &numCols, fp);
  102.                             pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
  103.                             pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
  104.                             break;
  105.                                
  106.                              default:  printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
  107.                                            x = 1;      /* Not legal option. */
  108.                                            break;
  109.                          }
  110.                          if( x == 1 )
  111.                                 break;
  112.                      }
  113.                      break;
  114.               default:  if(n != 1)
  115.                 break;
  116.        }
  117.      }
  118.         fclose(out);
  119.         fclose(fp);
  120.         free(header);
  121.         free(pixels);
  122.     return 0;
  123.  
  124. }
  125.  
  126. // Implement or define each function prototype listed in pgmUtility.h file.
  127. // NOTE: You can NOT change the input, output, and argument type of the functions in pgmUtility.h
  128. // NOTE: You can NOT change the prototype of any functions listed in pgmUtility.h
  129.  
  130.  
  131. /**
  132.  *  Function Name:
  133.  *      pgmRead()
  134.  *      pgmRead() reads in a pgm image using file I/O, you have to follow the file format.
  135.  *      
  136.  *  @param[in,out]  header  holds the header of the pgm file in a 2D character array
  137.  *                          After we process the pixels in the input image, we write the origianl
  138.  *                          header (or potentially modified) back to a new image file.
  139.  *  @param[in,out]  numRows describes how many rows of pixels in the image.
  140.  *  @param[in,out]  numCols describe how many pixels in one row in the image.
  141.  *  @param[in]      in      FILE pointer, points to an opened image file that we like to read in.
  142.  *  @return         If successful, return all pixels in the pgm image, which is an int **, equivalent to
  143.  *                  a 2D array. Otherwise null.
  144.  *
  145.  */
  146.  
  147. int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in  ){
  148.      
  149.     int i, j;
  150.    
  151.    
  152.    
  153.     //read in the entire header. then rewind
  154.     for(i = 0; i < 4; i++)
  155.         fgets(header[i], 100, in);
  156.    
  157.     rewind(in);
  158.     char x[100];
  159.     fgets(x,100, in);
  160.     fgets(x, 100, in);
  161.     int A=0;
  162.     fscanf(in, "%d %d", numCols, numRows);
  163.     fscanf(in, "%d",&A);
  164.     int ** pixels = (int **) malloc( *numCols * sizeof(int));
  165.     for(i = 0; i < *numCols; i++){
  166.         pixels[i] = (int *) malloc(sizeof(int) * (*numRows));
  167.     }
  168.     //read in pixels linearly. The files seem to have some formatting issues when displayed in gedit, thus iterate through keeping a counter based off the numRows and numCols to know when to change the index in pixels
  169.     for(j = 0; j < *numRows; j++){
  170.         for(i = 0; i < *numCols; i++){
  171.             fscanf(in, "%d", &pixels[i][j]);
  172.             }  
  173.     }
  174.     return pixels;
  175. }
  176.  
  177.  
  178. /**
  179.  *  Function Name:
  180.  *      pgmDrawCircle()
  181.  *      pgmDrawCircle() draw a circle on the image by setting relavant pixels to Zero.
  182.  *
  183.  *  @param[in,out]  pixels  holds all pixels in the pgm image, which a 2D integer array. The array
  184.  *                          is modified after the drawing.
  185.  *  @param[in]      numRows describes how many rows of pixels in the image.
  186.  *  @param[in]      numCols describes how many columns of pixels in one row in the image.
  187.  *  @param[in]      centerCol specifies at which column you like to center your circle.
  188.  *  @param[in]      centerRow specifies at which row you like to center your circle.
  189.  *                        centerCol and centerRow defines the center of the circle.
  190.  *  @param[in]      radius    specifies what the radius of the circle would be, in number of pixels.
  191.  *  @param[in,out]  header returns the new header after draw.
  192.  *                  the circle draw might change the maximum intensity value in the image, so we
  193.  *                  have to change maximum intensity value in the header accordingly.
  194.  *  @return         return 1 if max intensity is changed, otherwise return 0;
  195.  */
  196.  
  197. int pgmDrawCircle( int **pixels, int numRows, int numCols, int centerRow, int centerCol, int radius, char **header ){
  198.     int intensity = 0, new_intensity = 0, i, j;
  199.  
  200. //p and circlecenter are both 1d, 2 element arrays.  The elements of the arrays should be the indexes of those pixels in the larger pixel array
  201. //foreach (pixel p in pixels)
  202. //{
  203. //    if (distance(p, circleCenter) <= radius){
  204. //        drawBlackAt(p);
  205. //    }
  206.     for(i = 0; i < numCols; i++)
  207.         for(j = 0; j < numRows; j++)
  208.             if(pixels[i][j] > intensity)
  209.                 intensity = pixels[i][j];
  210.                
  211.     for(i = 0; i < numCols; i++)
  212.         for(j = 0; j < numRows; j++){
  213.             int p1[] = {j, i}, p2[] = {centerRow, centerCol};      
  214.             if(distance(p1, p2) < radius)
  215.                 pixels[i][j] = 0;
  216.    
  217.         }
  218.  
  219.    
  220.     for(i = 0; i < numCols; i++)
  221.         for(j = 0; j < numRows; j++)
  222.             if(pixels[i][j] > new_intensity)
  223.                 new_intensity = pixels[i][j];
  224.    
  225.     if(new_intensity != intensity){
  226.         char str[15];
  227.         sprintf(str, "%d", new_intensity);
  228.         header[3] = str;   
  229.         return 1;
  230.     } //use strcpy in case this doesn't work
  231.     return 0;
  232.  
  233. }
  234.  
  235.  
  236. /**
  237.  *  Function Name:
  238.  *      pgmDrawEdge()
  239.  *      pgmDrawEdge() draws a black edge frame around the image by setting relavant pixels to Zero.
  240.  *
  241.  *  @param[in,out]  pixels  holds all pixels in the pgm image, which a 2D integer array. The array
  242.  *                          is modified after the drawing.
  243.  *  @param[in]      numRows describes how many rows of pixels in the image.
  244.  *  @param[in]      numCols describes how many columns of pixels in one row in the image.
  245.  *  @param[in]      edgeWidth specifies how wide the edge frame would be, in number of pixels.
  246.  *  @param[in,out]  header returns the new header after draw.
  247.  *                  the function might change the maximum intensity value in the image, so we
  248.  *                  have to change the maximum intensity value in the header accordingly.
  249.  *
  250.  *  @return         return 1 if max intensity is changed by the drawing, otherwise return 0;
  251.  */
  252.    
  253. //TODO  fix the stupid max intensity stuff
  254. int pgmDrawEdge( int **pixels, int numRows, int numCols, int edgeWidth, char **header ){
  255.    
  256.     int i, j;
  257.  
  258.  
  259. //int intensity = highest intensity in pixels
  260.     int intensity = 0, new_intensity = 0;
  261.  
  262.     for(i = 0; i < numCols; i++)
  263.         for(j = 0; j < numRows; j++)
  264.             if(pixels[i][j] > intensity)
  265.                 intensity = pixels[i][j];
  266.  
  267. //treat pixels as a 2d array.
  268. //from pixels[0][0] to pixels[edgeWidth][numRows] and from pixels[numCols - edgewidth][0] to pixels[numCols][numRows]
  269. //from pixels[edgeWidth][0] to pixels[numCols - edgeWidth][edgeWidth] and from pixels[edgeWidth][numRows - edgeWidth] to pixels[numCols - edgeWidth][numRows]
  270.    
  271.     for(i = 0; i < edgeWidth; i++)
  272.         for(j = 0; j < numRows; j++)
  273.             pixels[i][j] = 0;
  274.     for(i = numCols - 1 - edgeWidth; i < numCols; i++)
  275.         for(j = 0; j < numRows; j++)
  276.             pixels[i][j] = 0;
  277.     for(i = 0; i < numCols; i++)
  278.         for(j = 0; j < edgeWidth; j++)
  279.             pixels[i][j] = 0;
  280.     for(i = 0; i < numCols; i++)
  281.         for(j = numRows - 1 - edgeWidth; j < numRows; j++)
  282.             pixels[i][j] = 0;
  283.  
  284. //declare int** intensity_finder = **pixels
  285. //iterate through intensity_finder to find the greatest intensity
  286.     for(i = 0; i < numCols; i++)
  287.         for(j = 0; j < numRows; j++)
  288.             if(pixels[i][j] > new_intensity)
  289.                 new_intensity = pixels[i][j];
  290. //int new_intensity = result;
  291. //compare new_intensity to intensity, if new_intensity is different then return 1;
  292.    
  293.     if(new_intensity != intensity){
  294.         char str[100];
  295.  
  296.         sprintf(str, "%d", new_intensity);
  297.         //strcat(str, "\n");
  298.         printf("%s", str);
  299.  
  300.         header[3] = str;   
  301.         return 1;
  302.     }//possibly need strcpy
  303.     return 0;
  304. }
  305.  
  306.  
  307.  
  308. /**
  309.  *  Function Name:
  310.  *      pgmWrite()
  311.  *      pgmWrite() writes headers and pixels into a pgm image using file I/O.
  312.  *                  writing back image has to strictly follow the image format.
  313.  *
  314.  *  @param[in]  header  holds the header of the pgm file in a 2D character array
  315.  *                          we write the header back to a new image file on disk.
  316.  *  @param[in]  pixels  holds all pixels in the pgm image, which a 2D integer array.edgeWidth = atoi(argv[2]);
  317.                             strcpy(originalImage, argv[3]);
  318.                             strcpy(newImageFile, argv[4]);
  319.                             fp = fopen(originalImage, "r");
  320.                             out = fopen(newImageFile, "w");
  321.                             pixels = pgmRead(header, &numRows, &numCols, fp);
  322.                             pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
  323.                             pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
  324.                             break;
  325.  *  @param[in]  numRows describes how many rows of pixels in the image.
  326.  *  @param[in]  numCols describe how many columns of pixels in one row in the image.
  327.  *  @param[in]  out     FILE pointer, points to an opened text file that we like to write into.
  328.  *  @return     return 0 if the function successfully writes the header and pixels into file.
  329.  *                          else return -1;
  330.  */
  331.  
  332. int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){
  333.  
  334.  
  335. //iterate straight through pixels
  336. //setup with a loop to insert a new line every "numCols" and keep printing until "numRows + 1" is reached (as soon as numRows + 1   break loop)
  337.     int i, j;
  338.     for(i = 0; i < 4; i++){
  339.         //printf("%s", *header[i]);
  340.         fprintf(out, "%s", *header[i]);
  341.        
  342.     }
  343.     //for(i = 0; i < 4; i++)
  344.             //fprintf(out, "*I=%d**%s**", i, header[i]);
  345.        
  346.  
  347.     for(j = 0; j < numRows; j++){
  348.         for(i = 0; i < numCols; i++)
  349.             fprintf(out, "%d ", pixels[i][j]);
  350.         fprintf(out, "\n");
  351.        
  352.        
  353.            
  354.     }
  355.    
  356.    
  357.  
  358. }
  359.  
  360.  
  361. /**
  362.  *  Function Name:
  363.  *      distance()
  364.  *      distance() returns the Euclidean distance between two pixels.
  365.  *
  366.  *  @param[in]  p1  coordinates of pixel one, p1[0] is for row number, p1[1] is for column number
  367.  *  @param[in]  p2  coordinates of pixel two, p2[0] is for row number, p2[1] is for column number
  368.  *  @return         return distance between p1 and p2
  369.  */
  370. double distance( int p1[], int p2[] ){
  371.     int point1_x = p1[0], point1_y = p1[1], point2_x = p2[0], point2_y = p2[1];
  372.  
  373.     int actual_x = point1_x - point2_x;
  374.     int actual_y = point1_y - point2_y;
  375.  
  376.     double square = ((actual_x * actual_x) + (actual_y * actual_y));
  377.     double hyp = sqrt(square);
  378.  
  379.     return hyp;
  380.  
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement