97 Answer ― Half


#include <stdlib.h>
#include <stdio.h>
#include "../basicColorImage.c"

void halfSize( colorImage original, colorImage half)
{
  int r, c ;
  pixel pix;

  for ( r=0; r<half.nrows; r++ )
    for ( c=0; c<half.ncols; c++ )
    {
      pix = getColorPixel( original, 2*r, 2*c ) ;
      setColorPixel( half, r, c, pix );
    }
}

int main ( int argc, char* argv[] )
{
  colorImage original;
  colorImage half;

  if ( argc != 3 )
  {
    printf("colorToHalf original.ppm halfSize.ppm\n");
    exit( EXIT_FAILURE );
  }

  /* read in the old image */
  readPPMimage( &original, argv[1] );

  /* create empty color image */
  if ( newColorImage( &half, original.nrows/2, original.ncols/2 ) == NULL )
  {
    printf(">>error<< newColorImage can't allocate memory\n");
    exit( EXIT_FAILURE );
  }
  
  /* fill in half size image */
  halfSize( original, half ) ;

  /* write the image to disk and free memory */
  writePPMimage( half, argv[2] );
  freeColorImage( &original );
  freeColorImage( &half );
}

Comments: