Question

Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...

  • Your program should be capable of creating a ppm image of the flag of Benin
    • • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6.
    • o The left green field should be 6/15ths of the width of the flag.

Variables

country_code should also include a new option, 4, for the flag of Benin.

struct pixel contains 3 integer values r, g, and b

struct image contains 3 integer values width, height,and country_code

Functions

main

  • • Functions as a driver – o calls other functions as appropriate, does not print out the flag or do any calculations.
  • • Should ensure that the user enters a valid number for the country_code and width.

make_pixel

  • • Instead of receiving 3 integers as its passed parameters, this function should instead take one pixel struct.

make_ppm_header

  • • Instead of receiving 2 integers as its passed parameters, this function should instead take one image struct.

make_ppm_image

  • • Should not perform height calculations or include logic to determine the color of the pixel to print.
  • • This function should only contain ONE set of two nested for loops.
  • • This function should call the appropriate new and old functions to create the PPM image.

get_color

  • • When called, it should return a pixel struct of the appropriate color.
  • • Feel free to create and call smaller functions for each country (such as get_color_italy) from this function, but that is not required.
  • • It takes the parameters column, row, and an image struct.

calculate_height

  • • When called, it should return a completed image struct, with the appropriate number of pixels in height a flag should be when made with the passed width set.
  • • It takes the parameters width and country_code.

You should always break up your programs into small parts, doing each one incrementally, compiling in between each step before continuing to the next step. Working in smaller sections to accomplish the larger goal will help you, especially if you utilize the thinking you developed in previous labs for splitting tasks apart.

For this assignment, it is probably be a good idea to first make your program from Lab 6 work for the flag of Poland with a pixel struct, and then add the new functions one by one. Once you have that completely working, then try to adapt the other flags.

I'm having trouble setting up the functions and getting the image to print out in the output.

Here's my code:

#include<stdio.h>

typedef struct pixel_struct{

int r,g,b;

}pixel;

typedef struct image_struct{

int width;

int height;

int country_code;

}image;

void make_pixel(struct pixel){

}

void make_ppm_header(struct image){

}

void make_ppm_image(){

}

void get_color(int columnm, int row, struct image){

}

void calculate_height(int width, int country_code){

}

int main(){

fprintf(stderr, "Enter the country_code of the chosen flag.(1 for Poland, 2 for Netherlands, 3 for Italy,4 for Benin)");

fscanf(stdin,"%d",&country_choice);

fprintf(stderr,"Enter the width of the flag ");

fscanf(stdin, "%d", &width);

height=(width*2)/3;

case 1:

    printf("Benin Flag\n");

    for(i=0; i<height; i++){

    for(i=0; i<width/4; i++){

    printf("%c%c%c", 0,135,81);

        for(i=0; i<width/3; i++){

    printf("%c%c%c", 252,209,22);

        for(i=0; i<width/3; i++){

    printf("%c%c%c", 232,17,45);

    }

}

}

}

    break;

}

    

return(0);

}

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


#include <stdio.h>

typedef struct pixel_struct {
   int r;
   int g;
   int b;
} pixel;

typedef struct image_struct {
   int width;
   int height;
   int country_code;
} image;

image calculate_height (double width, int country_code);
void make_pixel (pixel pixelInfo);
void make_ppm_header (image imageInfo);
void make_ppm_image (int country_code, int width);
pixel get_color (int column, int row, image imageInfo);

int main()
{
   double width = 0, country_code = 0;
   int floored_width = 1;
   //loops if country code is invalid
   while (!(country_code == 1 || country_code == 2 || country_code == 3 || country_code == 4)) {
       //prompt user to enter country_code of chosen flag
       fprintf ( stderr, "What country's flag do you want to create? " );
       fscanf(stdin, "%lf", &country_code);
       if (!(country_code == 1 || country_code == 2 || country_code == 3 || country_code == 4)) {
           fprintf ( stderr, "The code has to be a 1, 2, 3, or 4. Please try again.\n\n" ) ;
       }
   }
   //loops if width is invalid (not a positive integer)
   while (width <= 0 || floored_width - width != 0) {
       fprintf(stderr, "What width (in pixels) do you want it to be? ");
       fscanf(stdin, "%lf", &width);
       floored_width = width;
       if (width <= 0 || floored_width - width != 0) {
           fprintf ( stderr, "The width has to be a positive integer. Please try again.\n\n" ) ;
       }
   }
   fprintf(stderr, "\nMaking country %.0lf's flag with width %.0lf pixels...\n", country_code, width);
   // Write the image data
   make_ppm_image(country_code, width);
   fprintf(stderr, "Done!\n\n" );
   return(0);
}
//Calculates the height with proper rounding.
//Hoist to fly ratio depends on flag
image calculate_height (double width, int country_code ) {
   double calculated_height = 0;
   int floored_height = 0;
   //struct image has width, height, and country code
   image imageStruct;
   imageStruct.width = width;
   imageStruct.height = 0;
   imageStruct.country_code = country_code;
   switch (country_code) {
       //if country code 1, calculate poland height
       case 1:
           imageStruct.country_code = 1;
           calculated_height = width * 5 / 8 ;
           floored_height = calculated_height;  
           if (calculated_height - floored_height < .5) {
               imageStruct.height = floored_height;
           }
           else if (calculated_height - floored_height >= .5) {
               imageStruct.height = floored_height + 1;
           }
           //return poland image struct
           return imageStruct;
           break;
       //if country code 2, calculate netherlands height
       case 2:
           imageStruct.country_code = 2;
           calculated_height = width * 2 / 3 ;
           floored_height = calculated_height;
           if (calculated_height - floored_height < .5) {
               imageStruct.height = floored_height;
           }
           else if (calculated_height - floored_height >= .5) {
               imageStruct.height = floored_height + 1;
           }
           //return netherlands image struct
           return imageStruct;
           break;
       //if country code 3, calculate italy height
       case 3:
           imageStruct.country_code = 3;
           calculated_height = width * 2 / 3 ;
           floored_height = calculated_height;
           if (calculated_height - floored_height < .5) {
               imageStruct.height = floored_height;
           }
           else if (calculated_height - floored_height >= .5) {
               imageStruct.height = floored_height + 1;
           }
           //return italy image struct
           return imageStruct;
           break;
       //if country code 4, calculate Benin height
       case 4:
           imageStruct.country_code = 4;
           calculated_height = width * 2 / 3 ;
           floored_height = calculated_height;
           if (calculated_height - floored_height < .5) {
               imageStruct.height = floored_height;
           }
           else if (calculated_height - floored_height >= .5) {
               imageStruct.height = floored_height + 1;
           }
           //return benin image struct
           return imageStruct;
           break;
       default:
           printf ("error: A bad country code went into the calculate height function.\n");
           return imageStruct;  
   }
}

// Creates a pixel with the colors you tell it to use when you call it
// To call it, just do something like make_pixel(244, 244, 244);
void make_pixel (pixel pixelInfo){
   //take in pixel struct, print pixel
   fprintf(stdout, "%c%c%c", pixelInfo.r, pixelInfo.g, pixelInfo.b);
}

// Creates a header with the desired width and height when you call it
void make_ppm_header (image imageInfo){
   fprintf(stdout, "P6\n");
   fprintf(stdout, "%d %d %d\n", imageInfo.width, imageInfo.height, 255);
}

//gets color depending on pixel location
pixel get_color (int column, int row, image imageInfo){
   pixel pixelInfo;
   switch (imageInfo.country_code){
       //if country_code is 1 (poland)
       case 1:
               //Make the top half of the flag white
           if ( row < imageInfo.height / 2 ) {
               // red, 255
               pixelInfo.r = 255;
               // blue, 255
               pixelInfo.g = 255;
               // green, 255
               pixelInfo.b = 255;
               return pixelInfo;
           }
              //Make the bottom half red./a.
           else if ( row >= imageInfo.height / 2 ) {
               // red, 255
               pixelInfo.r = 255;
               // blue, 0
               pixelInfo.g = 0;
               // green, 0
               pixelInfo.b = 0;
               return pixelInfo;
           }
           break;
       //if country_code is 2 (netherlands)
       case 2:
           //Make the top third red
           if (row < imageInfo.height / 3) {
               pixelInfo.r = 174;
               pixelInfo.g = 28;
               pixelInfo.b = 40;
               return pixelInfo;
           }
           //Make the middle white
           else if (row < (imageInfo.height / 3 * 2)) {
               pixelInfo.r = 255;
               pixelInfo.g = 255;
               pixelInfo.b = 255;
               return pixelInfo;
           }
           //Make the bottom third blue
           else if (row >= (imageInfo.height / 3 * 2)) {
               pixelInfo.r = 33;
               pixelInfo.g = 70;
               pixelInfo.b = 139;
               return pixelInfo;
           }
           break;
       //if country_code is 3 (italy)
       case 3:
           //Make the first third green
           if (column < imageInfo.width / 3) {
               pixelInfo.r = 0;
               pixelInfo.g = 140;
               pixelInfo.b = 69;
               return pixelInfo;
           }
           //Make the middle white
           else if (column < imageInfo.width / 3 * 2) {
               pixelInfo.r = 244;
               pixelInfo.g = 245;
               pixelInfo.b = 240;
               return pixelInfo;
           }
           //Make the last third red
           else if (column >= (imageInfo.width / 3 * 2)){
               pixelInfo.r = 205;
               pixelInfo.g = 33;
               pixelInfo.b = 42;
               return pixelInfo;
           }
           break;
       //if country_code is 4 (benin)
       case 4:
           //make gr
           if (column < imageInfo.width * 6 / 15) {
               pixelInfo.r = 0;
               pixelInfo.g = 170;
               pixelInfo.b = 0;
               return pixelInfo;
           }
           //make yellow
           else if (column >= imageInfo.width * 6 / 15 && row < imageInfo.height / 2) {
               pixelInfo.r = 255;
               pixelInfo.g = 255;
               pixelInfo.b = 0;
               return pixelInfo;
           }
           //make red  
           else if (column >= imageInfo.width * 6 / 15 && row >= imageInfo.height / 2) {
               pixelInfo.r = 255;
               pixelInfo.g = 0;
               pixelInfo.b = 0;
               return pixelInfo;
           }      
   }
   return pixelInfo;
}
// Creates a complete ppm image when you call it
void make_ppm_image (int country_code, int width){
   //calculate height using calculate_height function and put it in image struct
   image imageInfo = calculate_height (width, country_code);
   //make_ppm_header takes in one image struct and outputs header
   make_ppm_header (imageInfo);
   //for number of rows in image
   for (int row = 0; row <= imageInfo.height; row++) {
       //for number of columns in a row
       for (int column = 0; column < imageInfo.width; column++) {
           //get_color takes in column, row, and country_code. returns pixel struct
           pixel pixelInfo = get_color ( column, row, imageInfo);
           //make_pixel takes in pixel struct and outputs the pixel rgb
           make_pixel (pixelInfo);
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...
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
  • Write a program called flags.c that is capable of creating an image of the flags of...

    Write a program called flags.c that is capable of creating an image of the flags of Poland, Netherland, and Italy. country_code should be 1, 2, and 3 for Poland, the Netherlands, and Italy, respectively. Here's my code so far #include<stdio.h> void make_pixel(int r,int g, int b); void make_ppm_header(int width,int height); void make_ppm_image(int country_code, int width); int main(){    int width=0;    int country_code=0;    fprintf(stderr, "Enter the country_code of the chosen flag.(1 for Poland, 2 for Netherlands, 3 for Italy)");    fscanf(stdin,...

  • I want to create the flags for Poland, the Netherlands, and Italy using ppm files and...

    I want to create the flags for Poland, the Netherlands, and Italy using ppm files and file redirection. I can't seem to get my pixels lined up correctly to make my flags. Here's my output for when I tried to make my flag for Poland. Any way to fix this? I am working in C #include #include <stdio.h> <stdlib.h> void make_pixel (int r, int g, int b); void make-ppm-header (int width, int height); void make_ppm_image (int country_code, int width) int...

  • Image proccessing in PROGRAMING C NO MAIN FUNCTION NEEDED! Color-Filter an image: 1. All pixels in...

    Image proccessing in PROGRAMING C NO MAIN FUNCTION NEEDED! Color-Filter an image: 1. All pixels in the picture with color in the chosen range will be replaced with new color. The following shows the pseudo code for the color filter. if (R in the range of [target_r - threshold, target_r + threshold]) and (G in the range of [target_g - threshold, target_g + threshold]) and (B in the range of [target_b - threshold, target_b + threshold]) R = replace_r ;...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...

    Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include <stdlib.h> <stdio.h> <string.h> <errno.h> <unistd.h> #include <sys/types.h> #include <sys/ipc.h> Winclude <sys/msg.h> struct my msg st f long int...

  • Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1:...

    Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1: We were unable to transcribe this image3 Encoding an Image in a09q2.py In the next four sections, you will no longer write class methods but rather functions. In each of the next four parts, you should be importing the PPM class from a09q1.py to use with the command from a09q1 import PPM Note that this is similar to how the check module was imported...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect;...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

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