예제 #1
0
파일: ppm.c 프로젝트: claf/blur
int ppmb_read ( char *filein_name, int *xsize, int *ysize, int *maxrgb, int **array )
{
  FILE *filein;
  int   numbytes;
  int   result;

  filein = fopen ( filein_name, "rb" );

  if ( filein == NULL ) {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Cannot open the input file %s.\n", filein_name );
    return 1;
  }
/*
  Read the header.
*/
  result = ppmb_read_header ( filein, xsize, ysize, maxrgb );

  if ( result != 0 ) {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  PPMB_READ_HEADER failed.\n" );
    return 1;
  }
/*
  Allocate storage for the data.
*/
  numbytes = 3 * ( *xsize ) * ( *ysize ) * sizeof ( int );

  *array = ( int * ) malloc ( numbytes );

  if ( *array == NULL ) {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Unable to allocate memory for data.\n" );
    printf ( "  Seeking %d bytes.\n", numbytes );
    return 1;
  }

/*
  Read the data.
*/
  result = ppmb_read_data (filein, *xsize, *ysize, *array);

  if ( result != 0 ) {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  PPMB_READ_DATA failed.\n" );
    return 1;
  }
/*
  Close the file.
*/
  fclose ( filein );

  return 0;
}
예제 #2
0
bool ppmb_read ( char *file_name, int *xsize, int *ysize, int *maxrgb,
  unsigned char **rarray, unsigned char **garray, unsigned char **barray )

/******************************************************************************/
/*
  Purpose:

    PPMB_READ reads the header and data from a binary portable pixel map file.
 
  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:
 
    16 June 2012
 
  Author:
 
    John Burkardt

  Parameters:

    Input, char *FILE_NAME, the name of the file containing the binary
    portable pixel map data.

    Output, int *XSIZE, *YSIZE, the number of rows and columns of data.

    Output, int *MAXRGB, the maximum RGB value.

    Output, unsigned char **RARRAY, **GARRAY, **BARRAY, the arrays of XSIZE by YSIZE 
    data values.

    Output, bool PPMB_READ, equals
    true, if the file could not be read,
    false, if the file was read.
*/
{
  FILE *file_pointer;
  int numbytes;
  bool result;

  file_pointer = fopen ( file_name, "rb" );

  if ( file_pointer == NULL )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Cannot open the input file %s.\n", file_name );
    return true;
  }
/*
  Read the header.
*/
  result = ppmb_read_header ( file_pointer, xsize, ysize, maxrgb );

  if ( result )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  PPMB_READ_HEADER failed.\n" );
    return true;
  }
/*
  Allocate storage for the data.
*/
  numbytes = ( *xsize ) * ( *ysize ) * sizeof ( unsigned char );

  *rarray = ( unsigned char * ) malloc ( numbytes );

  if ( *rarray == NULL )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Unable to allocate memory for data.\n" );
    printf ( "  Seeking %d bytes.\n", numbytes );
    return true;
  }

  *garray = ( unsigned char * ) malloc ( numbytes );

  if ( *garray == NULL )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Unable to allocate memory for data.\n" );
    printf ( "  Seeking %d bytes.\n", numbytes );
    return true;
  }

  *barray = ( unsigned char * ) malloc ( numbytes );

  if ( *barray == NULL )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  Unable to allocate memory for data.\n" );
    printf ( "  Seeking %d bytes.\n", numbytes );
    return true;
  }
/*
  Read the data.
*/
  result = ppmb_read_data ( file_pointer, *xsize, *ysize, *rarray, 
    *garray, *barray );

  if ( result )
  {
    printf ( "\n" );
    printf ( "PPMB_READ: Fatal error!\n" );
    printf ( "  PPMB_READ_DATA failed.\n" );
    return true;
  }
/*
  Close the file.
*/
  fclose ( file_pointer );

  return false;
}