imageprocessing/fileFunctions.c
BYTE* readImageFile(char* infile, int width, int height)
{
  int x = 0;
  int y = 0;
  FILE * pIn = NULL;
  BYTE * buffer = NULL;
  BYTE * image = NULL;

  image = (BYTE*) calloc(width*height,sizeof(BYTE));
  /* buffer can hold one line, 3 bytes per pixel */
  buffer = (BYTE*) calloc(width*3,sizeof(BYTE));
  if ((image == NULL) || (buffer == NULL))
     {
     printf("Error allocating image array or buffer\n");  
     return NULL;
     }
  pIn = fopen(infile,"rb");
  if (pIn == NULL)
     {
     printf("Error opening input image %s\n", infile);
     free(buffer);
     free(image);
     return NULL;
     }
  for (y = 0; y < height; y++)
     {
     int retval;
     retval = fread(buffer,sizeof(BYTE),width*3,pIn);
     if (retval  == width*3)
        {
	for (x = 0; x < width; x++)
	    {
	    pixval(x,y,width) = buffer[x*3]; // only need first byte of triplet            
            }
        } 
     else 
        {
	printf("ERROR - fread returned wrong value %d for line %d\n", retval,y);
	free(buffer);
	free(image);
	return NULL;
        } 
     }
  free(buffer);
  fclose(pIn);
  return image;
}
/* Read a raw RGB file, one byte per color per pixel, into
 * a dynamically allocated byte array. Assumed to be 
 * binary so we keep only the first BYTE of each triad 
 * @param infile   name of file to read
 * @param width    number of pixels in each row
 * @param height   number of rows in the image
 * @return allocated, initialized image or NULL if error 
 */
Read a raw RGB file, one byte per color per pixel, into
a dynamically allocated byte array. Assumed to be
binary so we keep only the first BYTE of each triad
@param infile   name of file to read
@param width    number of pixels in each row
@param height   number of rows in the image
@return allocated, initialized image or NULL if error
Read a raw RGB file one byte per color per pixel into a dynamically allocated byte array . Assumed to be binary so we keep only the first BYTE of each triad @param infile name of file to read @param width number of pixels in each row @param height number of rows in the image @return allocated initialized image or NULL if error
BYTE* readImageFile(char* infile, int width, int height)
{
  int x = 0;
  int y = 0;
  FILE * pIn = NULL;
  BYTE * buffer = NULL;
  BYTE * image = NULL;
  image = (BYTE*) calloc(width*height,sizeof(BYTE));
  buffer = (BYTE*) calloc(width*3,sizeof(BYTE));
  if ((image == NULL) || (buffer == NULL))
     {
     printf("Error allocating image array or buffer\n");  
     return NULL;
     }
  pIn = fopen(infile,"rb");
  if (pIn == NULL)
     {
     printf("Error opening input image %s\n", infile);
     free(buffer);
     free(image);
     return NULL;
     }
  for (y = 0; y < height; y++)
     {
     int retval;
     retval = fread(buffer,sizeof(BYTE),width*3,pIn);
     if (retval  == width*3)
        {
	for (x = 0; x < width; x++)
	    {
	    pixval(x,y,width) = buffer[x*3]; 
            }
        } 
     else 
        {
	printf("ERROR - fread returned wrong value %d for line %d\n", retval,y);
	free(buffer);
	free(image);
	return NULL;
        } 
     }
  free(buffer);
  fclose(pIn);
  return image;
}
BYTE * readImageFile ( char * infile , int width , int height ) { int x = 0 ; int y = 0 ; FILE * pIn = NULL ; BYTE * buffer = NULL ; BYTE * image = NULL ; image = ( BYTE * ) calloc ( width * height , sizeof ( BYTE ) ) ; buffer = ( BYTE * ) calloc ( width * 3 , sizeof ( BYTE ) ) ; if ( ( image == NULL ) || ( buffer == NULL ) ) { printf ( " \n " ) ; return NULL ; } pIn = fopen ( infile , " " ) ; if ( pIn == NULL ) { printf ( " \n " , infile ) ; free ( buffer ) ; free ( image ) ; return NULL ; } for ( y = 0 ; y < height ; y ++ ) { int retval ; retval = fread ( buffer , sizeof ( BYTE ) , width * 3 , pIn ) ; if ( retval == width * 3 ) { for ( x = 0 ; x < width ; x ++ ) { pixval ( x , y , width ) = buffer [ x * 3 ] ; } } else { printf ( " \n " , retval , y ) ; free ( buffer ) ; free ( image ) ; return NULL ; } } free ( buffer ) ; fclose ( pIn ) ; return image ; }
Read a raw RGB file, one byte per color per pixel, into
 a dynamically allocated byte array.
Read a raw RGB file one byte per color per pixel into a dynamically allocated byte array .
/* buffer can hold one line, 3 bytes per pixel */ // only need first byte of triplet            