So, I have to make a program that manipulates a picture in 3 different ways. I h
ID: 3774568 • Letter: S
Question
So, I have to make a program that manipulates a picture in 3 different ways. I have already done that part, but I cannot figure out how to store the images into a new array, which is to be called for the final output that creates the 3 new files. (note that it uses .pgm files)
-------------assignment instructions------------- (I've completed all but the bolded part)
Make the program prompt a menu, and read in the user’s choice.
1) Option 1 should be: read name of picture and read picture, add brightness: the value to be added should be prompted for, and read in.
2) Option 2 should be to Subtract pictures. This operation requires two picture files (which are prompted for), and will "subtract" the second picture from the first. Before the resulting picture is outputted, ensure that the absolute value of the subtraction operation at each pixel location is computed.
3) Option 3 should be the Sobel edge highlighter.
Your program should contain one function for each of the operations as per the above guidelines. Once the user has entered the desired operation and the required number of filepaths to pictures, call your function for that operation. The output of all of the three cases must be stored in a new picture array that will be outputted to three fixed files titled out1.pgm, out2.pgm and out3.pgm respectively.
--------------------------------------
So, how do I create this new array and use it as an argument in the function writeoutpic?
The three functions are named addtopixels, subtractPictures, and sobelfunc.
The final function that takes the images (stored in this new array) and turns them into .pgm files is called writeoutpic.
----all code below-----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 70
#define TRUE 1
#define FALSE 0
int** temppicx, temppicy;
int** img, img2, sobelout;
int numRows, numCols, maxVal, option;
FILE* fo1;
void addtopixels(int** imgtemp);
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 subtractPictures(int** imgtemp, int** pic);
void sobelfunc(int** pic, int** edges, int** tempx, int** tempy);
int main()
{
char fileName[BUFFER_SIZE];
char fileName2[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' ", fileName);
printf("Enter option (1,2,3): ");
scanf("%d", &option);
if (option==1){
addtopixels(img);
}
else if (option==2){
printf("Enter second image filename: ");
scanf("%s", fileName2);
img2 = readpic(fileName2);
printf("Successfully read second image file '%s' ", fileName2);
subtractPictures(img,img2);
}
else if (option==3)
{
sobelout= setImage();
temppicx= setImage();
temppicy= setImage();
sobelfunc(img, sobelout, temppicx, temppicy);
}
writeoutpic("HOW_DO_I_DO_THIS_PART.pgm", img);
free(img);
img = NULL;
free(img2);
img2 = NULL;
return(EXIT_SUCCESS);
}
void addtopixels(int** imgtemp)
{
int i,j,n;
printf("brighten by: ");
scanf("%d", &n);
for (i=0;i { for (j=0;j {
imgtemp[i][j] += n;
}
}
}
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' ", fileName);
exit(EXIT_FAILURE);
}
fprintf(fo1,"P5 ");
fprintf(fo1,"%d %d ", numRows, numCols);
fprintf(fo1,"255 ");
for (i=0;i { for (j=0;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' ", 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 ") != 0)
{
printf("Invalid image ID ");
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 ");
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 ");
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 { for (j=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 { for (j=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 { for (j=0;j {
img[i][j] = ((edges[i][j]*1.0) / maxival) * 255;
}
}
}
void subtractPictures(int** imgtemp, int** pic)
{
int i,j;
for (i=0;i { for (j=0;j {
imgtemp[i][j] -= pic[i][j];
abs(imgtemp[i][j]);
}
}
}
Explanation / Answer
if (!Array.from) { Array.from = (function () { var toStr = Object.prototype.toString; var isCallable = function (fn) { return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; }; var toInteger = function (value) { var number = Number(value); if (isNaN(number)) { return 0; } if (number === 0 || !isFinite(number)) { return number; } return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); }; var maxSafeInteger = Math.pow(2, 53) - 1; var toLength = function (value) { var len = toInteger(value); return Math.min(Math.max(len, 0), maxSafeInteger); }; // The length property of the from method is 1. return function from(arrayLike/*, mapFn, thisArg */) { // 1. Let C be the this value. var C = this; // 2. Let items be ToObject(arrayLike). var items = Object(arrayLike); // 3. ReturnIfAbrupt(items). if (arrayLike == null) { throw new TypeError("Array.from requires an array-like object - not null or undefined"); } // 4. If mapfn is undefined, then let mapping be false. var mapFn = arguments.length > 1 ? arguments[1] : void undefined; var T; if (typeof mapFn !== 'undefined') { // 5. else // 5. a If IsCallable(mapfn) is false, throw a TypeError exception. if (!isCallable(mapFn)) { throw new TypeError('Array.from: when provided, the second argument must be a function'); } // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. if (arguments.length > 2) { T = arguments[2]; } } // 10. Let lenValue be Get(items, "length"). // 11. Let len be ToLength(lenValue). var len = toLength(items.length); // 13. If IsConstructor(C) is true, then // 13. a. Let A be the result of calling the [[Construct]] internal method // of C with an argument list containing the single item len. // 14. a. Else, Let A be ArrayCreate(len). var A = isCallable(C) ? Object(new C(len)) : new Array(len); // 16. Let k be 0. var k = 0; // 17. Repeat, while kRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.