I33 Answer ― Linear Transformation


#include <stdlib.h>
#include <stdio.h>
#include "basicImage.c"  

/* Transform image pixels */
void linearTransform( image img, int low, int high )
{
  int r, c, p ;

  for ( r=0; r<img.nrows; r++ )
    for ( c=0; c<img.ncols; c++ )
    {
      p = getPixel( img, r, c ) ;
      if ( p<low ) p = 0;
      else if ( p>high ) p = 255;
      else p = (255*(p-low))/(high-low);
      setPixel( img, r, c, (unsigned char)p );
    }
}

int main ( int argc, char* argv[] )
{
  image img; int low, high;
  
  if ( argc != 5 )
  {
    printf( "stretch oldImage newImage low high\n" );
    exit( EXIT_FAILURE );
  }
  
  low = atoi( argv[3] );
  high = atoi( argv[4] );
  
  /* read in the  image */
  readPGMimage( &img, argv[1] );

  /* transform the image */
  linearTransform( img, low, high );
  
  /* write the image to disk and free memory */
  writePGMimage( img, argv[2] );
  freeImage( &img );
  
}

Comments: