Puzzle 61 - Puzzle 70

Color Image Puzzles

These puzzles involve creating various three-color images.


Puzzle I61 — Color Chip

[E-10] Write a program that creates an image that is all one color. The name of the image file, its size, and the red, green, and blue values are specified on the command line:

C:\PuzzleFolder>colorChip  name.ppm numRows numCols red green blue

Color values are in the range 0 to 255. For example, the command

C:\PuzzleFolder>colorChip  babyBlue.ppm 200 250 175 225 255

creates the image:

babyBlue.ppm

Here is a substantial part of the program. You can fill in the rest. (Recall that the [E-10] rating means that this should take about 10 lines of easy code in addition to the lines included here.)

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

int getColor( int arg, char *argv[] )
{
  int value;
  value = atoi( argv[arg] );
  if ( value < 0 || value > 255 )
  {
    printf("color level %s  must be between 0 and 255\n", argv[arg]);
    exit( EXIT_FAILURE );
  }
  return value;
}

int main(int argc, char *argv[])
{
  int r, nrows, c, ncols, red, grn, blu;
  FILE *image;

  /* check the command line parameters */
  if ( argc != 7 )
  {
    printf("colorChip fileName.ppm nrows ncols red green blue\n");
    return 0;
  }

  /* open the image file for writing in binary mode (see "gotcha!" in Gray Level Images) */
  if ( (image = fopen( argv[1], "wb") ) == NULL )
  {
    printf("file %s could not be created\n", argv[1]);
    return 0;
  }

  nrows = atoi( argv[2] );
  if ( nrows < 1 )
  {
    printf("number of rows must be positive\n");
    return 0;
  }

  ncols = atoi( argv[3] );
  if ( nrows < 1 )
  {
    printf("number of columns must be positive\n");
    return 0;
  }

  /* Get colors from the command line */
  . . .
  
  /* write out the PPM Header information */
  fprintf( image, "P6 ");
  fprintf( image, "%d %d %d ", ncols, nrows, 255 );

  /* write out the pixel data */
  . . .

  /* close the file */
  fclose ( image );

  return 1;
}

[M-10] Modify the program so that the color is specified in hex. The command

C:\PuzzleFolder>colorChipHex  babyBlue.ppm 200 250 AFE1FF 

creates the same image as above. For ease in programming, don't prefix the color code with '#' (as is required in HTML). Use the C function sscanf() and format code %x to scan the argument string.

 


Puzzle I62 — randomPixels

[E-10] Write a program that creates an image where the red, green, and blue value of each pixel is choosen randomly from the range 0 to 255. The name of the image file and its size values are specified on the command line:

C:\PuzzleFolder>randomPixels  name.ppm numRows numCols 

Here is an example image:

Random Pixels
randomPix.ppm

You will probably want to use the srand() and the rand() functions along with % (integer modulo division). Look in the puzzles on random number for more information.

 

Answer


Puzzle I63 — Candy Sprinkles

[E-10] Write a program that creates an image where the background is white with a specified percentage of colored pixels. The red, green, and blue value of each colored pixel is choosen randomly from the range 0 to 255. The name of the image file, its size, and the percentage are specified on the command line:

C:\PuzzleFolder>randomPixels  name.ppm numRows numCols percent

Here is an example image where 10 percent of the pixels are colored:

Candy Sprinkles

Hint: output the pixels in raster order. Toss a 100-sided die for each pixel and compare the outcome with the percentage parameter to decide if the pixel should be colored. If the pixel should be colored, use additional random numbers to pick the colors.

No answer has been provided for this puzzle.


Puzzle I64 — ColorSquares

[M-20] Write a program that divides the image (whatever size the user specifies) into 8 squares across and 8 squares down. Assign each square a color triple that looks like (16r, 16g, 16b) where each of r, g, and b is randomly picked from a range 0 to 15. This way of picking colors give a bolder look than picking each color value randomly from the range 0 to 240. Here is an example:

Random Color Blocks
Random Color Blocks

The command line lists merely the name and size of the image. (You can add other parameters if you wish; the number of blocks per row and column could be parameters.) The dimensions of the image should be multiples of eight, but the program does not check this.

C:\PuzzleFolder>randomQuilt randBlocks.ppm 256 256

Hints: Think of the image as an array of 8 block-rows by 8 block-columns. Pick the colors for every block in advance of writing the pixels. Hold these values an 8x8 array for each of red, green, and blue. Now, output the pixels in raster order (as with the previous programs). Calculate the block-row of a pixel by dividing the row number of the pixel by the height of a block. Do something similar for the block-column. Look up the colors in the 8x8 arrays and output the pixel.

 

Answer


Puzzle I65 — Red Square

[E-10] Write a program that creates an image of a red square on a white background. The square should be 2/3 the size of the image, and centered in the image.

C:\PuzzleFolder>redSquare redSquare.ppm 300 300

Base the dimensions of the square on the dimensions of the image. If the image is a rectangle, then so will be the central square.

Red Square

If you want, let the user specify the foreground and background colors on the command line.

 

Answer


Puzzle I66 — Red Circle

[E-10] Write a program that creates an image of a red circle on a white background. The circle should have a radius of 1/3 the smallest dimension of the image, and centered in the image.

C:\PuzzleFolder>redCircle redCircle.ppm 300 300

Here is an example:

Red CIrcle
Red Circle

[E-8] In digital images, edges sometimes look better if they are blended with the background. The following image is the same as the previous one, but now pixels at the very edge of the circle are an average of the foreground and background color. Modify your program to do that.

Red CIrcle Blend
Red Circle with Smooth Edges

[M-8] For an extra challange, allow rectangular images and squeeze the circle so that the entire circle fits into the image. Hint: do this by scaling the row number and column number each into the range 0-100 (regardless of their actual ranges). Calculate if a pixel is inside or outside of the circle using the scaled row and column number.

 

No answer has been provided for this puzzle. Look at the gray circle puzzle I08 for some strong hints.


Puzzle I67 — Fade to Black

Create an image that starts at a specified color at the left edge and then fades gradually to black at the right edge. The command line specified the image size and the color of the left edge.

C:\PuzzleFolder>gradientOne fadeBlack.ppm nrows ncols red green blue

Here is a sample:

Gradient from Left to Right

An easy variation of this program is to fade to white instead of to black (see also the next puzzle).

Answer


Puzzle I68 — Two Color Gradient

Create an image that starts at a specified color at the left edge and then blends gradually to another color at the right edge. The command line specified the image size and the color of both edges.

C:\PuzzleFolder>gradientOne fadeBlack.ppm nrows ncols redL greenL blueL redR greenR blueR

Here is a sample image:

Two Color Blend
Two Color Blend

If you want, allow the user to specify the colors using hex notation like FF0055.

 

Answer


Puzzle I69 — Four Color Gradient

Create an image that has colors specified for each of the four corners. The color of each pixel in the image is a blend of each of the four colors of the corners. The command line specified the image size and the color each corner.

C:\PuzzleFolder>gradientFour fourCorners.ppm nrows ncols redUL greenUL blueUL redUR greenUR blueUR redLL greenLL blueLL redLR greenLR blueLR 

Here is a sample image:

Four Color Blend

If you want, allow the user to specify the colors using hex notation like FF0055. This certainly simplifies the command line.

There are many ways in which the four colors can be blended. Here is one method: Find the colors for the left edge as a blend of the colors at the upper left and lower left corners. Find the colors for the right edge as a blend of the colors at the upper right and lower right corners For a pixel at (Row=R, Col=C) first find the color the left edge has at row R and the color the right edge has at row R. Then calculate the color at column C as a blend of those two colors (just like in the two color gradient).

Better yet, think of your own method.

 

No answer has been provided for this puzzle.


Puzzle I70 — Circular Color Gradient

Create an image that has two colors specified: one for for the center of the image and one for the corners. (All four corners have the same color.) The color of each pixel in the image is a blend of each of the the central color and the corner color. The command line specified the image size and the color of the corners.

C:\PuzzleFolder>circularGrad circGrad.ppm nrows ncols cntrR cntrG cntrB crnrR crnrG crnrB

Here is a sample image:

Circular Blend
Circular Color Blend

There are many ways in which thecolors can be blended. Here is one method: First find the maximum distance of any pixel from the center. Call this D for a pixel at (R,C) find the distance d from it to the center. Blend the two colors using the ratio d/D;

Better yet, think of your own method.

 

Sorry, no solution is provided for this puzzle.


Contents ― Return to the main contents page