Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define BUFFER_SIZE 70
#define TRUE 1
#define FALSE 0
int sel;
int val = 500;
int** img;
int** img2;
int** imgout;
int** temppicx, temppicy;
int** img,sobelout;
int numRows;
int numCols;
int maxVal;
FILE* fo1;
void sel1();
void sel2();
void sel3();
void sel4();
void add_disc(int** imgtemp, int val);
void addtopixels(int** imgtemp, int value);
void subtractfrompixels(int** img, int** img2, int** imgout);
void writeoutpic(char* fileName, int** imgtemp);
int** readpic(char* fileName);
void readHeader(FILE* imgFin);
int isComment(char* line);
void readImgID(char* line);
void readImgSize(char* line);
void readMaxVal(char* line);
int** setImage();
void readBinaryData(FILE* imgFin, int** imgtemp);
void sobelfunc(int** pic, int** edges, int** tempx, int** tempy);
int main() {
char fileName[BUFFER_SIZE];
int i,j,rows,cols;
char ci;
int value1;
int num = 0;
printf("Please choose one of the following options:\n\n");
printf("\t1 - Modify Image Brightness\n");
printf("\t2 - Subtract Images\n");
printf("\t3 - Sobel Edge Highlighter\n");
printf("\t4 - Add Brightness Disk\n\n");
printf("\t5 - EXIT\n\n");
printf("\tOption: ");
scanf("%d", &sel);
// Modify Brightness - calls function 'sel1'
if (sel == 1)
sel1();
// Subtract Images - calls function 'sel2'
if (sel == 2)
sel2();
// Sobel Edge Highlighter - calls function 'sel'3
if (sel == 3)
sel3();
// Brightness disk
if (sel == 4)
sel4();
// exit
if (sel == 5)
return(0);
}
// Option 1
void sel1() {
char* fileName;
printf("\nEnter image filename: ");
scanf("%s", fileName);
img = readpic(fileName);
printf("Successfully read image file '%s'\n\n", fileName);
printf("Enter value to brighten image by: ");
scanf("%d", &val);
// Adds user inputted value of brightness to the entire image
addtopixels(img,val);
writeoutpic("out1.pgm",img);
free(img);
img = NULL;
system("pause");
}
// Option 2
void sel2() {
char* fileName;
int** img;
int** img2;
int** imgout;
printf("\nEnter first image filename: ");
scanf("%s", fileName);
img = readpic(fileName);
printf("Successfully read image file '%s'\n\n", fileName);
printf("Enter image to be subtracted filename: ");
scanf("%s", fileName);
img2 = readpic(fileName);
printf("Successfully read image file '%s'\n\n", fileName);
// Subtracts image 2 from image 1
subtractfrompixels(img,img2,imgout);
system("pause");
}
// Option 3
void sel3() {
char* fileName;
int** temppicx;
int** temppicy;
int** img;
int** sobelout;
printf("\nEnter image filename: ");
scanf("%s", fileName);
img = readpic(fileName);
printf("Successfully read image file '%s'\n\n", fileName);
sobelout= setImage();
temppicx= setImage();
temppicy= setImage();
// function that highlights edges in an image
sobelfunc(img,sobelout,temppicx,temppicy);
writeoutpic("out1.pgm",sobelout);
free(img);
img = NULL;
system("pause");
}
void sel4() {
int i=0;
int j=0;
char* fileName;
int** img;
printf("\nEnter image filename: ");
scanf("%s", fileName);
img = readpic(fileName);
printf("Successfully read image file '%s'\n\n", fileName);
printf("numCols = %d, numRows = %d\n", numCols, numRows);
add_disc(img, val);
writeoutpic("out1.pgm",imgout);
free(img);
img = NULL;
system("pause");
}
void add_disc(int** imgtemp, int val) {
int i,j;
int k;
int** img;
printf("Please enter a radius: \n");
scanf("%d", &val);
printf("Enter value to brighten image by: ");
scanf("%d", k);
img = setimage();
for(i = 0; i < numRows; i++) {
for(j = 0; j < numCols; i++) {
if (val^2 >= ((i-(numRows/2))^2+(j-(numCols/2))^2)) {
img[i][j] += k;
}
}
}
}
void addtopixels(int** imgtemp, int value) {
int i,j;
for (i=0;i<numRows;i++) {
for (j=0;j<numCols;j++) {
imgtemp[i][j] += val;
}
}
}
void subtractfrompixels(int** img, int** img2, int** imgout) {
int i,j;
// Allocates memory to store new image
imgout = setImage();
for (i=0;i<numRows;i++) {
for (j=0;j<numCols;j++) {
// Subtracts image 2 from image 1
imgout[i][j] = abs(img[i][j] - img2[i][j]);
}
}
writeoutpic("out1.pgm",imgout);
free(img);
img = NULL;
img2 = NULL;
}
void writeoutpic(char* fileName, int** imgtemp) {
int i,j;
char ci;
FILE* fo1;
if((fo1 = fopen(fileName, "wb")) == NULL) {
printf("Unable to open out image file '%s'\n", fileName);
exit(EXIT_FAILURE);
}
fprintf(fo1,"P5\n");
fprintf(fo1,"%d %d\n", numRows, numCols);
fprintf(fo1,"255\n");
for (i=0;i<numRows;i++) {
for (j=0;j<numCols;j++) {
ci = (char) (imgtemp[i][j]);
fprintf(fo1,"%c", ci);
}
}
}
int** readpic(char* fileName) {
FILE* imgFin;
int** imgtemp;
if((imgFin = fopen(fileName, "rb")) == NULL) {
printf("Unable to open image file '%s'\n", fileName);
exit(EXIT_FAILURE);
}
readHeader(imgFin);
imgtemp = setImage();
readBinaryData(imgFin, imgtemp);
fclose(imgFin);
return imgtemp;
}
void readHeader(FILE* imgFin) {
int haveReadImgID = FALSE;
int haveReadImgSize = FALSE;
int haveReadMaxVal = FALSE;
char line[BUFFER_SIZE];
while(!(haveReadImgID && haveReadImgSize && haveReadMaxVal)) {
fgets(line, BUFFER_SIZE, imgFin);
if((strlen(line) == 0) || (strlen(line) == 1))
continue;
if(isComment(line))
continue;
if(!(haveReadImgID)) {
readImgID(line);
haveReadImgID = TRUE;
}
else if(!(haveReadImgSize)) {
readImgSize(line);
haveReadImgSize = TRUE;
}
else if(!(haveReadMaxVal)) {
readMaxVal(line);
haveReadMaxVal = TRUE;
}
}
}
int isComment(char *line) {
if(line[0] == '#')
return(TRUE);
return(FALSE);
}
void readImgID(char* line) {
if(strcmp(line, "P5\n") != 0) {
printf("Invalid image ID\n");
exit(EXIT_FAILURE);
}
}
void readImgSize(char* line) {
unsigned i;
for(i = 0; i < strlen(line); ++i) {
if(!((isdigit((int) line[i])) || (isspace((int) line[i])))) {
printf("Invalid image size\n");
exit(EXIT_FAILURE);
}
}
sscanf(line, "%d %d", &numRows, &numCols);
}
void readMaxVal(char* line) {
unsigned i;
for(i = 0; i < strlen(line); ++i) {
if(!((isdigit((int) line[i])) || (isspace((int) line[i])))) {
printf("Invalid image max value\n");
exit(EXIT_FAILURE);
}
}
maxVal = atoi(line);
}
int** setImage() {
int** imgtemp;
unsigned i;
imgtemp = (int**) calloc(numRows, sizeof(int*));
for(i = 0; i < numRows; ++i)
imgtemp[i] = (int*) calloc(numCols, sizeof(int));
return imgtemp;
}
void readBinaryData(FILE* imgFin, int** imgtemp) {
unsigned i;
unsigned j;
for(i = 0; i < numRows; ++i) {
for(j = 0; j < numCols; ++j) {
imgtemp[i][j] =
fgetc(imgFin);
}
}
}
void sobelfunc(int** pic, int** edges, int** tempx, int** tempy) {
int maskx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
int masky[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
int maxival;
int i,j,p,q,mr,sum1,sum2;
double threshold;
mr = 1;
for (i=mr;i<numRows-mr;i++) {
for (j=mr;j<numCols-mr;j++) {
sum1 = 0;
sum2 = 0;
for (p=-mr;p<=mr;p++) {
for (q=-mr;q<=mr;q++) {
sum1 += pic[i+p][j+q] * maskx[p+mr][q+mr];
sum2 += pic[i+p][j+q] * masky[p+mr][q+mr];
}
}
tempx[i][j] = sum1;
tempy[i][j] = sum2;
}
}
maxival = 0;
for (i=mr;i<numRows-mr;i++) {
for (j=mr;j<numCols-mr;j++) {
edges[i][j]=(int) (sqrt((double)((tempx[i][j]*tempx[i][j]) +
(tempy[i][j]*tempy[i][j]))));
if (edges[i][j] > maxival)
maxival = edges[i][j];
}
}
for (i=0;i<numRows;i++) {
for (j=0;j<numCols;j++) {
edges[i][j] = ((edges[i][j]*1.0) / maxival) * 255;
}
}
}