Question

The task is to implement a program that inputs the name of a PGM file, loads...

The task is to implement a program that inputs the name of a PGM file, loads it from the disk, and then computes the minimal, maximal, and average pixel value for this file. After the program completes the computation, it must explicitly free the memory taken by the image. Note that you need to use some of the provided basic functions for loading images into memory. The output of your program should look as follows:

Minimum = 15 (darkest pixel)

Maximum = 240 (lightest pixel)

Average = 67 (mean of pixels)

(imageio.c file-- https://www.cs.cmu.edu/~eugene/teach/algs00a/progs/imageio.c,

imageio.h file-- https://www.cs.cmu.edu/~eugene/teach/algs00a/progs/imageio.h,

threshold.c file-- https://www.cs.cmu.edu/~eugene/teach/algs00a/progs/threshold.c,

blocks.pgm and building.pgm link- https://www.cs.cmu.edu/~eugene/teach/algs00a/progs/)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Code To Copy

//Main.c

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include "imageio.h"

#include "imageio.c"

void getMinMaxAvg(unsigned char **image, int rows, int cols);

int main(){

char fp[999];

printf("Enter File Path: ");

gets(fp);

int r, c;

unsigned char **image;

read_pgm_image(fp, &image, &r, &c);

getMinMaxAvg(image, r, c);

return 0;

}

//Function to calulate the Min Max And Avg of Image Pixels

void getMinMaxAvg(unsigned char **image, int rows, int cols){

unsigned char min = 255, max = 0;

unsigned int avg=0;

//Loop to iterate over all image pixels

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

if(image[i][j] < min)

  min = image[i][j];

if(image[i][j] > max)

  max = image[i][j];

avg += image[i][j];

}

}

printf("Minimum = %d ", (int)min);

printf("Maximum = %d ", (int)max);

printf("Average = %d ", (int)avg/(rows*cols));

}

//imageio.h

/*******************************************************************************

* Program: imageio.h

* Purpose: This header file contains function prototypes for functions that

* dynamically allocate and free 8-bit (unsigned char) images. It also contains

* prototypes for functions that read and write images to files in raw PGM

* format. This code was written to be used as a teaching resource.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

*******************************************************************************/

#ifndef _PGMIO_

#define _PGMIO_

unsigned char **allocate_image(int rows, int cols);

void free_image(unsigned char **image, int rows);

int read_pgm_image(char *infilename, unsigned char ***image, int *rows, int *cols);

int write_pgm_image(char *outfilename, unsigned char **image, int rows,

int cols, char *comment, int maxval);

int write_gray_bmp(char *outfilename, unsigned char **image, short int rows, short int cols);

#endif

//imageio.c

/*******************************************************************************

* Program: imageio.c

* Purpose: This souce code file contains functions for dynamically allocating

* and freeing 8-bit (unsigned char) images. It also contains functions for

* reading and writing images to files in raw PGM format. This code was written

* to be used as a teaching resource.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

*******************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "imageio.h"

/*******************************************************************************

* Function: allocate_image

* Purpose: This function allocates an image. The image is an array of pointers

* to arrays. The array of pointers will have a length of the number of rows,

* and each of these pointers will point to a separate one dimensional array

* whose length is the number of columns in the image. This scheme was used

* because it allows the image to be accessed using the syntax image[r][c]

* yet still allow the image to be any size.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

*******************************************************************************/

unsigned char **allocate_image(int rows, int cols)

{

unsigned char **image=NULL;

int r, br;

/****************************************************************************

* Allocate an array of pointers of type (unsigned char *). The array is

* allocated to have a length of the number of rows.

****************************************************************************/

if((image = (unsigned char **) calloc(rows, sizeof(unsigned char *)))==NULL){

fprintf(stderr, "Error allocating the array of pointers in allocate_image(). ");

return((unsigned char **)NULL);

}

/****************************************************************************

* For each row, allocate an array of type (unigned char).

****************************************************************************/

for(r=0;r<rows;r++){

if((image[r] = (unsigned char *) calloc(cols, sizeof(unsigned char)))==NULL){

fprintf(stderr, "Error allocating an array in allocate_image(). ");

for(br=0;br<r;br++) free(image[br]);

free(image);

return((unsigned char **)NULL);

}

}

return(image);

}

/*******************************************************************************

* Function: free_image

* Purpose: This function frees the memort that was previously allocated to

* store an image.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

*******************************************************************************/

void free_image(unsigned char **image, int rows)

{

int r;

/****************************************************************************

* Free each row of the image.

****************************************************************************/

for(r=0;r<rows;r++) free(image[r]);

/****************************************************************************

* Free the array of pointers.

****************************************************************************/

free(image);

}

/******************************************************************************

* Function: read_pgm_image

* Purpose: This function reads in an image in raw PGM format. Because the PGM

* format includes the number of columns and the number of rows in the image,

* these are read from the file. Memory to store the image is allocated in this

* function. All comments in the header are discarded in the process of reading

* the image. Upon failure, this function returns 0, upon sucess it returns 1.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

******************************************************************************/

int read_pgm_image(char *infilename, unsigned char ***image, int *rows,

int *cols)

{

FILE *fp;

int r;

char buf[71];

/***************************************************************************

* Open the input image file for reading. If the file can not be opened for

* reading return an error code of 0.

***************************************************************************/

if((fp = fopen(infilename, "r")) == NULL){

fprintf(stderr, "Error reading the file %s in read_pgm_image(). ",

infilename);

return(0);

}

/***************************************************************************

* Verify that the image is in PGM format, read in the number of columns

* and rows in the image and scan past all of the header information.

***************************************************************************/

fgets(buf, 70, fp);

if(strncmp(buf,"P5",2) != 0){

fprintf(stderr, "The file %s is not in PGM format in ", infilename);

fprintf(stderr, "read_pgm_image(). ");

fclose(fp);

return(0);

}

do{ fgets(buf, 70, fp); }while(buf[0] == '#'); /* skip all comment lines */

sscanf(buf, "%d %d", cols, rows);

do{ fgets(buf, 70, fp); }while(buf[0] == '#'); /* skip all comment lines */

/***************************************************************************

* Allocate memory to store the image.

***************************************************************************/

if(((*image) = allocate_image(*rows, *cols)) == NULL) return(0);

/***************************************************************************

* Read in the image from the file, one row at a time.

***************************************************************************/

for(r=0;r<(*rows);r++){

if((*cols) != fread((*image)[r], 1, (*cols), fp)){

fprintf(stderr, "Error reading the image data in read_pgm_image(). ");

fclose(fp);

free_image((*image), *rows);

return(0);

}

}

fclose(fp);

return(1);

}

/******************************************************************************

* Function: write_pgm_image

* Purpose: This function writes an image in raw PGM format. A comment can be

* written to the header if coment != NULL. If there is a comment, it can

* be up to 70 characters long.

* Name: Michael Heath, University of South Florida

* Date: 1/7/2000

******************************************************************************/

int write_pgm_image(char *outfilename, unsigned char **image, int rows,

int cols, char *comment, int maxval)

{

FILE *fp;

int r;

/***************************************************************************

* Open the output image file for writing.

***************************************************************************/

if((fp = fopen(outfilename, "w")) == NULL){

fprintf(stderr, "Error writing the file %s in write_pgm_image(). ",

outfilename);

return(0);

}

/***************************************************************************

* Write the header information to the PGM file.

***************************************************************************/

fprintf(fp, "P5 ");

if(comment != NULL)

if(strlen(comment) <= 70) fprintf(fp, "# %s ", comment);

fprintf(fp, "%d %d ", cols, rows);

fprintf(fp, "%d ", maxval);

/***************************************************************************

* Write the image data to the file.

***************************************************************************/

for(r=0;r<rows;r++){

if(cols != fwrite(image[r], 1, cols, fp)){

fprintf(stderr, "Error writing the image data in write_pgm_image(). ");

fclose(fp);

return(0);

}

}

fclose(fp);

return(1);

}

//Screenshot of the output


Add a comment
Know the answer?
Add Answer to:
The task is to implement a program that inputs the name of a PGM file, loads...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT