Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include "pgmUtility.h"
- int main(int argc, char *argv[]){
- /* args useage: ./programName –e edgeWidth originalImage newImageFile
- ./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile
- //Extra Credit
- ./programName -‐ce circleCenterRow circleCenterCol radius edgeWidth originalImage newImageFile
- ./programName -‐c -‐e circleCenterRow circleCenterCol radius edgeWidth originalImage newImageFile
- */
- FILE * fp;
- FILE * out; //this will get one of the items stored in argv
- //needs to read in command line arguments.....make sure it can take -ce and -c -e in order to get massive extra credit
- char ** header = (char**) malloc( sizeof(char *) * 4);
- int i;
- int ** pixels;
- for(i = 0; i < 4; i++){
- header[i] = (char *) malloc (sizeof(char) * 100);
- }
- int numRows, numCols;
- int m, n, l, x, ch;
- int edgeWidth, circleCenterRow, circleCenterCol, radius;
- char originalImage[100], newImageFile[100];
- if(argc != 5 && argc != 7)
- printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- for( n = 1; n < argc; n++ ) /* Scan through args. */
- {
- switch( (int)argv[n][0] ) /* Check for option character. */
- {
- case '-':x = 0; /* Bail out if 1. */
- l = strlen( argv[n] );
- for( m = 1; m < l; ++m ) /* Scan through options. */
- {
- ch = (int)argv[n][m];
- switch( ch )
- {
- case 'c': if(argc != 7){
- printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- break;
- }
- circleCenterRow = atoi(argv[2]);
- circleCenterCol = atoi(argv[3]);
- printf("%d %d\n\n", circleCenterRow, circleCenterCol);
- radius = atoi(argv[4]);
- strcpy(originalImage, argv[5]);
- strcpy(newImageFile, argv[6]);
- fp = fopen(originalImage, "r");
- out = fopen(newImageFile, "w");
- pixels = pgmRead(header, &numRows, &numCols, fp);
- printf("%s", header[3]);
- pgmDrawCircle(pixels, numRows, numCols, circleCenterRow, circleCenterCol, radius, header );
- pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
- break;
- case 'C': if(argc != 7){
- printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- break;
- }
- circleCenterRow = atoi(argv[2]);
- circleCenterCol = atoi(argv[3]);
- radius = atoi(argv[4]);
- strcpy(originalImage, argv[5]);
- strcpy(newImageFile, argv[6]);
- fp = fopen(originalImage, "r");
- out = fopen(newImageFile, "w");
- pixels = pgmRead(header, &numRows, &numCols, fp);
- pgmDrawCircle(pixels, numRows, numCols, circleCenterRow, circleCenterCol, radius, header );
- pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
- break;
- case 'E': if(argc != 5){
- printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- break;
- }
- edgeWidth = atoi(argv[2]);
- strcpy(originalImage, argv[3]);
- strcpy(newImageFile, argv[4]);
- fp = fopen(originalImage, "r");
- out = fopen(newImageFile, "w");
- pixels = pgmRead(header, &numRows, &numCols, fp);
- pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
- pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
- break;
- case 'e': if(argc != 5){
- printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- break;
- }
- edgeWidth = atoi(argv[2]);
- strcpy(originalImage, argv[3]);
- strcpy(newImageFile, argv[4]);
- fp = fopen(originalImage, "r");
- out = fopen(newImageFile, "w");
- pixels = pgmRead(header, &numRows, &numCols, fp);
- pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
- pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
- break;
- default: printf("Usage: ./programName –e edgeWidth originalImage newImageFile\nOR\n./programName –c circleCenterRow circleCenterCol radius originalImage newImageFile\n");
- x = 1; /* Not legal option. */
- break;
- }
- if( x == 1 )
- break;
- }
- break;
- default: if(n != 1)
- break;
- }
- }
- fclose(out);
- fclose(fp);
- free(header);
- free(pixels);
- return 0;
- }
- // Implement or define each function prototype listed in pgmUtility.h file.
- // NOTE: You can NOT change the input, output, and argument type of the functions in pgmUtility.h
- // NOTE: You can NOT change the prototype of any functions listed in pgmUtility.h
- /**
- * Function Name:
- * pgmRead()
- * pgmRead() reads in a pgm image using file I/O, you have to follow the file format.
- *
- * @param[in,out] header holds the header of the pgm file in a 2D character array
- * After we process the pixels in the input image, we write the origianl
- * header (or potentially modified) back to a new image file.
- * @param[in,out] numRows describes how many rows of pixels in the image.
- * @param[in,out] numCols describe how many pixels in one row in the image.
- * @param[in] in FILE pointer, points to an opened image file that we like to read in.
- * @return If successful, return all pixels in the pgm image, which is an int **, equivalent to
- * a 2D array. Otherwise null.
- *
- */
- int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in ){
- int i, j;
- //read in the entire header. then rewind
- for(i = 0; i < 4; i++)
- fgets(header[i], 100, in);
- rewind(in);
- char x[100];
- fgets(x,100, in);
- fgets(x, 100, in);
- int A=0;
- fscanf(in, "%d %d", numCols, numRows);
- fscanf(in, "%d",&A);
- int ** pixels = (int **) malloc( *numCols * sizeof(int));
- for(i = 0; i < *numCols; i++){
- pixels[i] = (int *) malloc(sizeof(int) * (*numRows));
- }
- //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
- for(j = 0; j < *numRows; j++){
- for(i = 0; i < *numCols; i++){
- fscanf(in, "%d", &pixels[i][j]);
- }
- }
- return pixels;
- }
- /**
- * Function Name:
- * pgmDrawCircle()
- * pgmDrawCircle() draw a circle on the image by setting relavant pixels to Zero.
- *
- * @param[in,out] pixels holds all pixels in the pgm image, which a 2D integer array. The array
- * is modified after the drawing.
- * @param[in] numRows describes how many rows of pixels in the image.
- * @param[in] numCols describes how many columns of pixels in one row in the image.
- * @param[in] centerCol specifies at which column you like to center your circle.
- * @param[in] centerRow specifies at which row you like to center your circle.
- * centerCol and centerRow defines the center of the circle.
- * @param[in] radius specifies what the radius of the circle would be, in number of pixels.
- * @param[in,out] header returns the new header after draw.
- * the circle draw might change the maximum intensity value in the image, so we
- * have to change maximum intensity value in the header accordingly.
- * @return return 1 if max intensity is changed, otherwise return 0;
- */
- int pgmDrawCircle( int **pixels, int numRows, int numCols, int centerRow, int centerCol, int radius, char **header ){
- int intensity = 0, new_intensity = 0, i, j;
- //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
- //foreach (pixel p in pixels)
- //{
- // if (distance(p, circleCenter) <= radius){
- // drawBlackAt(p);
- // }
- for(i = 0; i < numCols; i++)
- for(j = 0; j < numRows; j++)
- if(pixels[i][j] > intensity)
- intensity = pixels[i][j];
- for(i = 0; i < numCols; i++)
- for(j = 0; j < numRows; j++){
- int p1[] = {j, i}, p2[] = {centerRow, centerCol};
- if(distance(p1, p2) < radius)
- pixels[i][j] = 0;
- }
- for(i = 0; i < numCols; i++)
- for(j = 0; j < numRows; j++)
- if(pixels[i][j] > new_intensity)
- new_intensity = pixels[i][j];
- if(new_intensity != intensity){
- char str[15];
- sprintf(str, "%d", new_intensity);
- header[3] = str;
- return 1;
- } //use strcpy in case this doesn't work
- return 0;
- }
- /**
- * Function Name:
- * pgmDrawEdge()
- * pgmDrawEdge() draws a black edge frame around the image by setting relavant pixels to Zero.
- *
- * @param[in,out] pixels holds all pixels in the pgm image, which a 2D integer array. The array
- * is modified after the drawing.
- * @param[in] numRows describes how many rows of pixels in the image.
- * @param[in] numCols describes how many columns of pixels in one row in the image.
- * @param[in] edgeWidth specifies how wide the edge frame would be, in number of pixels.
- * @param[in,out] header returns the new header after draw.
- * the function might change the maximum intensity value in the image, so we
- * have to change the maximum intensity value in the header accordingly.
- *
- * @return return 1 if max intensity is changed by the drawing, otherwise return 0;
- */
- //TODO fix the stupid max intensity stuff
- int pgmDrawEdge( int **pixels, int numRows, int numCols, int edgeWidth, char **header ){
- int i, j;
- //int intensity = highest intensity in pixels
- int intensity = 0, new_intensity = 0;
- for(i = 0; i < numCols; i++)
- for(j = 0; j < numRows; j++)
- if(pixels[i][j] > intensity)
- intensity = pixels[i][j];
- //treat pixels as a 2d array.
- //from pixels[0][0] to pixels[edgeWidth][numRows] and from pixels[numCols - edgewidth][0] to pixels[numCols][numRows]
- //from pixels[edgeWidth][0] to pixels[numCols - edgeWidth][edgeWidth] and from pixels[edgeWidth][numRows - edgeWidth] to pixels[numCols - edgeWidth][numRows]
- for(i = 0; i < edgeWidth; i++)
- for(j = 0; j < numRows; j++)
- pixels[i][j] = 0;
- for(i = numCols - 1 - edgeWidth; i < numCols; i++)
- for(j = 0; j < numRows; j++)
- pixels[i][j] = 0;
- for(i = 0; i < numCols; i++)
- for(j = 0; j < edgeWidth; j++)
- pixels[i][j] = 0;
- for(i = 0; i < numCols; i++)
- for(j = numRows - 1 - edgeWidth; j < numRows; j++)
- pixels[i][j] = 0;
- //declare int** intensity_finder = **pixels
- //iterate through intensity_finder to find the greatest intensity
- for(i = 0; i < numCols; i++)
- for(j = 0; j < numRows; j++)
- if(pixels[i][j] > new_intensity)
- new_intensity = pixels[i][j];
- //int new_intensity = result;
- //compare new_intensity to intensity, if new_intensity is different then return 1;
- if(new_intensity != intensity){
- char str[100];
- sprintf(str, "%d", new_intensity);
- //strcat(str, "\n");
- printf("%s", str);
- header[3] = str;
- return 1;
- }//possibly need strcpy
- return 0;
- }
- /**
- * Function Name:
- * pgmWrite()
- * pgmWrite() writes headers and pixels into a pgm image using file I/O.
- * writing back image has to strictly follow the image format.
- *
- * @param[in] header holds the header of the pgm file in a 2D character array
- * we write the header back to a new image file on disk.
- * @param[in] pixels holds all pixels in the pgm image, which a 2D integer array.edgeWidth = atoi(argv[2]);
- strcpy(originalImage, argv[3]);
- strcpy(newImageFile, argv[4]);
- fp = fopen(originalImage, "r");
- out = fopen(newImageFile, "w");
- pixels = pgmRead(header, &numRows, &numCols, fp);
- pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
- pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out );
- break;
- * @param[in] numRows describes how many rows of pixels in the image.
- * @param[in] numCols describe how many columns of pixels in one row in the image.
- * @param[in] out FILE pointer, points to an opened text file that we like to write into.
- * @return return 0 if the function successfully writes the header and pixels into file.
- * else return -1;
- */
- int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){
- //iterate straight through pixels
- //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)
- int i, j;
- for(i = 0; i < 4; i++){
- //printf("%s", *header[i]);
- fprintf(out, "%s", *header[i]);
- }
- //for(i = 0; i < 4; i++)
- //fprintf(out, "*I=%d**%s**", i, header[i]);
- for(j = 0; j < numRows; j++){
- for(i = 0; i < numCols; i++)
- fprintf(out, "%d ", pixels[i][j]);
- fprintf(out, "\n");
- }
- }
- /**
- * Function Name:
- * distance()
- * distance() returns the Euclidean distance between two pixels.
- *
- * @param[in] p1 coordinates of pixel one, p1[0] is for row number, p1[1] is for column number
- * @param[in] p2 coordinates of pixel two, p2[0] is for row number, p2[1] is for column number
- * @return return distance between p1 and p2
- */
- double distance( int p1[], int p2[] ){
- int point1_x = p1[0], point1_y = p1[1], point2_x = p2[0], point2_y = p2[1];
- int actual_x = point1_x - point2_x;
- int actual_y = point1_y - point2_y;
- double square = ((actual_x * actual_x) + (actual_y * actual_y));
- double hyp = sqrt(square);
- return hyp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement