User Tools

Site Tools


Sidebar

projects

  • cci0 (due 20160127)
  • mms0 (due 20160203)
  • dow0 (due 20160210)
  • mbe0 (due 20160224)
  • pnc0 (due 20160302)
  • mbe1 (due 20160309)
  • cos0 (due 20160316)
  • sam0 (due 20160323)
  • cbf0 (due 20160406)
  • afn0 (due 20160413)
  • gfo0 (due 20160420)
haas:spring2016:cprog:projects:cos0

Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: IMAGE PROCESSING - CIRCLE OF SQUARES (cos0)

Objective

To apply our existing knowledge of programming constructs in the application of a specific end result: a program that utilizes an image processing library to produce an image of a circle, constructed of squares.

Prerequisites/Corequisites

In addition to the new skills required on previous projects, to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • can algorithmically generate a circle of points (x, y coordinate pairs)
  • can obtain information from the command-line (argc/argv)
  • can call functions from a library (gd).

Background

We've spent our time so far this semester covering the basic concepts of programming, in C. Variables, selection statements, iteration, functions, structs, etc.

Now that we've gotten through all of them, it is time to start applying those concepts so that deeper and further familiarity can be gained. This project is one such attempt at that.

We all know what a circle is, and in various math classes we've even explored various methods for doing things with circles (they have a radius, a circumference, a diameter), but how do would we enable the computer to draw a circle?

One piece of that, obviously, is the math.

Another piece is the programmatic piece. A circle implies something visual. On the computer that means an image.

So: we are going to use the gd image processing library to aid us in generating the image.

Program

Your task is to write a program that generates an image (PNG file), that is placed in your lab46 webspace for viewing, that does the following:

  • uses the gd image library (libgd)
  • reads the following information from the command-line (lacking this information should result in an error):
    • *(argv+1): image width (in pixels, ranging from 800-1280)
    • *(argv+2): image height (in pixels, ranging from 600-1024)
    • *(argv+3): circle radius (in pixels, should not exceed half of the smaller of width, height)
    • *(argv+4): center x of circle (a value of 0 should result in auto-centering of x with respect to width)
    • *(argv+5): center y of circle (a value of 0 should result in auto-centering of y with respect to height)
    • *(argv+6), if present, will be the name of the output image file produced. If it is absent, assume the default name cos0.png.
  • image width, height is at least 800×600 (can be larger, but not huge, don't go beyond 1280×1024)– and specified on the command-line when running the program
    • if the information isn't supplied, display an error and exit with non-zero status
    • image resolution must not be square (i.e. NOT 800×800, 1000×1000, etc.)– if it is, display an error and exit with non-zero status
  • image has a black background
  • image has an outer border of a color other than black and the circle (unless circle is multiple colors, then the border could be one of the contained colors).
  • draws a circle out of squares
    • circle must be at least 1 color (that isn't the same as the background), but bonus consideration would be given for any of the following:
      • multi-colored (think gradient) circle
      • multiple circles (concentric?)
      • spirals
    • squares should be at least 10×10 (pixels)

Now, gd has functions for drawing circles. You are NOT to use them for this project. As I said, this is a circle of squares. As such, you will be making use of the gdImageFilledRectangle() function.

getting started

You may make use of the following skeleton program to assist you in doing this project.

Note that this code is not in conformance with project specifications, but is provided to give you a starting place for modifications and additions:

/*
 * PROGRAM.c  - skeleton code to use the GD graphics library
 *              to create an image.
 *
 * To compile:  gcc -o PROGRAM PROGRAM.c -lgd
 * To execute:  ./PROGRAM
 *         OR:  ./PROGRAM cos0.png  (creates in current directory)
 *
 *  Functions:  http://www.boutell.com/gd/manual2.0.33.html
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
 
// color values
//
#define BLACK       0
#define BLUE        1
#define GREEN       2
#define RED         3
#define WHITE       4
 
int main(int argc, char **argv)
{
    char                       *outfile;        // name out of the output file
    FILE                       *out;            // output file pointer
    gdImagePtr                  img;            // GD Image Construct
    unsigned int                color[5];       // color array
    unsigned short int          wide, high;     // image attributes
 
    if (argc == 2)
    {
        outfile = *(argv+1);
        fprintf(stdout, "Using '%s' as output filename\n", outfile);
    }
    else
    {
        outfile = (char *) malloc (sizeof(char) * 64);
        strcpy(outfile, "/home/USERNAME/public_html/cos0.png");
    }
 
    // image dimensions
    //
    wide = 800;
    high = 600;
 
    // Create new image of specified wide-ness and high-ness
    //
    img = gdImageCreate(wide, high);
 
    // My GD color definitions
    //
    color[BLACK]     = gdImageColorAllocate(img, 0x00, 0x00, 0x00);
    color[BLUE]      = gdImageColorAllocate(img, 0x00, 0x00, 0xFF);
    color[GREEN]     = gdImageColorAllocate(img, 0x00, 0xFF, 0x00);
    color[RED]       = gdImageColorAllocate(img, 0xFF, 0x00, 0x00);
    color[WHITE]     = gdImageColorAllocate(img, 0xFF, 0xFF, 0xFF);
 
    // Paint the background black
    //
    gdImageFilledRectangle(img, 0, 0, wide-1, high-1, color[BLACK]);
 
    /****************************************************************
     *                                                              *
     *  =============>  YOUR CODE/PROGRAM GOES HERE  <============  *
     *                                                              *
     ****************************************************************/
 
    // Open the file
    //
    if((out = fopen(outfile, "wb")) == NULL)
    {
        fprintf(stderr, "Error opening '%s'\n", outfile);
        exit(1);
    }
 
    // Send image to file
    //
    gdImagePngEx(img, out, -1);
 
    // Close things up
    //
    fclose(out);
    gdImageDestroy(img);
 
    return(0);
}

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no warnings or errors)
  • Output must be correct, and resemble the form given in the sample output above.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must utilize the algorithm presented above
  • Code must be commented
    • have a properly filled-out comment banner at the top
    • have at least 20% of your program consist of //-style descriptive comments
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog cos0 cos0.c cos0.png http://lab46.corning-cc.edu/~USERNAME/cos0.png
Submitting cprog project "cos0":
    -> cos0.c(OK)
    -> cos0.png(OK)
    -> http://lab46.corning-cc.edu/~USERNAME/cos0.png

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/spring2016/cprog/projects/cos0.txt · Last modified: 2016/03/07 17:30 by wedge