Making a PPM file in C that uses many shapes

54 Views Asked by At

So I'm working on a program that, ultimately, will create a simple image of a spooky pumpkin (square eyes, nose, rectangle mouth). Right now with my code I am generating a orange circle. The color of the shape can be changed by inputting an RGB code under the 'Index Through the Image' section, and the shape that is being generated (square, circle, rectangle) can be altered by changing the parameters when the program is run ('./SpookyImageGenerator output.ppm 800 600 100', which creates a circle).

Now... I'm able to generate different shapes and different colors as different PPM files. How do I go about getting these shapes to generate 'on top of one another' so to speak in the same PPM so I can create an actual image?

This is the code in my main c file that I am using

#include <stdlib.h>
#include <stdio.h>
#include "MyFunctions.h"

int main(int argc, char *argv[]) {

    int numRows;            // Number of rows in our image.
    int numCols;            // Number of columns in our image.
    int imageSize;          // The total numberof rows * columns in the image
    int Radius;             // The radius of the circle added to image.
    int row, col;           // The marker to know where we are in the image
    int InOrOut;            // Flag for in or outside of the circle

    unsigned char *outImage;     /* Pixel Pointer           */
    unsigned char *ptr;          /* Pointer                 */
    FILE *outputFP;              /* My out put file pointer */

    printf("=======================================\n");
    printf("        This will make an image        \n");
    printf("=======================================\n");

    // Read in some command line arguments.

    if(argc != 5) {
        printf("Usage: ./[1]SpookyImageMaker [2]OutFile [3]numRows [4]numCols [5]Radius \n");
        exit(1);
    }
    if((numRows = atoi(argv[2])) <= 0) {
        printf("The given number rows is not positive!\n");
    }
    if((numCols = atoi(argv[3])) <= 0) {
        printf("The given number of columns is not positive!\n");
    }
    if((Radius = atoi(argv[4])) <= 0) {
        printf("Check your radius -- it should be positive!\n");
    }

    printf("=============================================\n");
    printf("        Now working to set up sizes.         \n");
    printf("=============================================\n");

    imageSize = numRows*numCols*3;
    outImage = (unsigned char *) malloc(imageSize);     

    /* Open the image for writing. */
    if ( (outputFP = fopen(argv[1],"w")) == NULL) {
        perror("output open error");
        printf("Error in opening your given output file!\n");
        exit(2);
    }

    printf("=============================================\n");
    printf("        Index Through the given image.       \n");
    printf("=============================================\n");

    ptr = outImage;

    for( row = 0 ; row < numRows ; row++) {
        for( col = 0 ; col < numCols ; col++){
            printf("Row and Col (%d,%d)\n",row,col);
            InOrOut = CheckRadius(numRows, numCols, Radius, row, col);

            if(InOrOut == 1) {
                *ptr     = 255;    // red pixel
                *(ptr+1) = 117;    // green pixel
                *(ptr+2) = 24;     // blue pixel
            } else {
                *ptr     = 0;    // red pixel
                *(ptr+1) = 0;    // green pixel
                *(ptr+2) = 0;    // blue pixel
            }
            // Advance the pixel pointer
            ptr += 3;
        }
    }

    printf("============================================\n");
    printf("   Write it all out to a file for viewing.  \n");
    printf("============================================\n");

    fprintf(outputFP, "P6 %d %d 255\n", numCols, numRows);
    fwrite(outImage, 1, imageSize, outputFP);

    /* Done */
    fclose(outputFP);
    return 0;
}

HelperFunctions.c file

#include <math.h>                

int CheckRadius(int TotRows, int TotCols, int Radius, int CurPixRow, int CurPixCol){

    int InOrOut   = 0;           // Returns 0 or 1 for out and in side the circle respectively.
    int CenterRow = TotRows/2;   // Place holder for the center with regard to rows.
    int CenterCol = TotCols/2;   // Place holder for the center column.
    int dist      = 0;           // Place in memory for the distance between CurPix and Center.

    // printf("I'm in CheckRadius\n");

    // Compute The distance.
    dist = pow((pow(CenterRow-CurPixRow,2.0)+pow(CenterCol-CurPixCol,2.0)),0.5);

    // Check is dist < Radius and set flag.
    if( dist < Radius){
        InOrOut = 1;
    }

    return InOrOut;
}

In addition to this I have HelperFunctions.c file and a MyFunctions.h file, both of which currently only contain a method to CheckRadius of a circle to determine whether a pixel is in or out of a circle.

Any guidance would be appreciated. Not sure where to go from here.

0

There are 0 best solutions below