Question

Question: For the picture writing question, if the question says that the picture length (height) and...

Question:

For the picture writing question, if the question says that the
picture length (height) and width are multiples of 5, then be prepared 
(for example) to handle a situation where you are being asked to blacken 
the fourth (vertical) strip  from the left.

Code:

#include
#include
#include
#include

#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 { for (j=0;j {
imgtemp[numCols-1-i][numRows-1-j] = imgtemp[i][j];
}
}
}

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 { 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'\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);
}
}
}

ONLY THIS PART MUST BE EDITED (PLEASE GIVE THIS EDITED PART AS THE SOLUTION):

void addtopixels(int** imgtemp, int value)
{
int i,j;
  
for (i=0;i { for (j=0;j {
imgtemp[numCols-1-i][numRows-1-j] = imgtemp[i][j];
}
}
}

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

void addtopixels(int** imgtemp, int value)
{
   int x = numCols / 5;
   int a = x * 3;
   int b = x * 4;
   int i, j;
   for (i = 0; i < numRows; ++i)
   {
       for (j = 0; j < numCols; ++j)
       {
           if (j >= a && j < b)
               imgtemp[i][j] += value;
       }
   }
}

// also this function of yours seemed to have syntax errors, i fixed it for you.


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");

// errors were below this comment

   for (i = 0; i < numRows; ++i)
   {
       for (j = 0; j < numCols; ++j)
       {
           ci = (char)(imgtemp[i][j]);
           fprintf(fo1, "%c", ci);
       }
   }
}

//test case

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

Add a comment
Know the answer?
Add Answer to:
Question: For the picture writing question, if the question says that the picture length (height) and...
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
  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each...

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • All that is needed is to fill in that one area that says "Student provides missing...

    All that is needed is to fill in that one area that says "Student provides missing code to compare computed hammingCode[] bits to the codeword[] bits to determine the incorrect bit." in the code below. THAT IS ALL THAT IS NEEDED PLUS SCREENSHOT OF COMPILED OUTPUT AFTERWARDS. Please TAKE SCREENSHOT of the compiled output which should look similar to the sample one provided. //------------------------------------------------------ // Problem #6 // Problem6.c //------------------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <math.h>...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

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
ADVERTISEMENT