Alright, I'll be straight up about this. This is a homework assignment. I know those are looked down on based on the sticky that an admin posted. So before anyone gets ........ed off, I'm not looking for you to do the entire thing for me. I just need help.
I'm doing an assignment that has much of the necessary code already given to me, as the professor says the code is far too advanced for the scope of our class (intro to C++) and basically I just have to change things here and there to get it to work the way I need it to.
The assignment is all about 2D arrays-pictures, to be precise. The first task is brightness adjustment. The code given to me changes each value within the 2D array of an image to a higher value. I somehow have to change it so that the amount of change is based on a user input. Any clues as to where I should go to implement a change would be helpful. Here is the code as it is given.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 70
#define TRUE 1
#define FALSE 0
int** img;
int numRows;
int numCols;
int maxVal;
FILE* fo1;
void addtopixels(int** imgtemp, int value);
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);
int main()
{
char fileName[BUFFER_SIZE];
int i,j,rows,cols;
char ci;
printf("Enter image filename: ");
scanf("%s", fileName);
img = readpic(fileName);
printf("Successfully read image file '%s'\n", fileName);
addtopixels(img,103);
printf("Enter image filename for output: ");
scanf("%s", fileName);
writeoutpic(fileName,img);
free(img);
img = NULL;
return(EXIT_SUCCESS);
}
void addtopixels(int** imgtemp, int value)
{
int i,j;
for (i=0;i<numRows;i++)
{ for (j=0;j<numCols;j++)
{
imgtemp[i][j] += value;
}
}
}
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);
}
}
}
The above code is, like i said, a contrast editor.
On top of finding what to change so that the contrast increase/decrease (can go either way by using a + or - number), I have to embed this into an options menu (which utilizes if and else ifs), which I have already created. What, if anything, should I leave OUT of the option slot for this particular option and leave at the top by my int main? Can i simply change what I need to change and drop the whole thing into the option slot? I know that ints, floats, etc. can be declared anywhere in the program, assuming that they're declared prior to being used.
thanks for reading, and I really appreciate help because I have a hard time tracking down a small bit of what needs to be changed if I don't really know what's going on in the code as a whole. This class has turned into a disaster.