I04 Answer


/* Puzzle I04 -- gray Bands
|
|
*/
void writeBands(FILE *image, int nrows, int ncols, int nbands)
{
  int r, c;
  int colsPerBand, bandNum, stepSize, grayLevel;


  /* determine how many columns are in each band */
  /* (this may not work out evenly) */
  colsPerBand = ncols/nbands;
 
  /* determine how much brighter each band is from the previous band*/
  /* (this may not work out evenly) */
  stepSize = 255/(nbands-1);
 
  /* write out the pixel data */
  for ( r=0; r<nrows; r++ )
    for ( c=0; c<ncols; c++ )
    {
      /* current column determines the band number */
      bandNum = c/colsPerBand;
 
      /* last band may be wider than others */
      if ( bandNum == nbands ) bandNum = nbands-1;

      /* band number determines the gray level */
      grayLevel = bandNum*stepSize;
      fputc( grayLevel, image );
    }
}

back