Exemple #1
0
int pm_get_signed_int( FILE* file)
{
  register int sign;
  register char ch;
  register int i;

  sign = 1;
  do
    {
      ch = pm_getc( file );
    }
  while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
  
  if ( (ch < '0' || ch > '9') && (ch != '-') )
    pm_erreur( "junk in file where an integer should be" );
  
  if (ch == '-') {
    sign = -1;
    ch = pm_getc( file );
    if ( ch < '0' || ch > '9' )
      pm_erreur( "junk in file where an integer should be" );
  }
  
  i = 0;
  do
    {
      i = i * 10 + ch - '0';
      ch = pm_getc( file );
    }
  while ( ch >= '0' && ch <= '9' );
  
  i *= sign;

  return i;
}
int main(int argc, char* argv[])
    {
    FILE* ifp;
    bit* bitmap;
    int ich1, ich2, rows, cols ;
    int i,j ;


    /* Test des arguments */
    if ( argc != 2 ){
      printf("\nUsage : %s fichier \n\n", argv[0]);
      exit(0);
    }

    /* Ouverture */
    ifp = fopen(argv[1],"r");
    if (ifp == NULL) {
      printf("erreur d'ouverture du fichier %s\n", argv[1]);
      exit(1);
    }

    /* Lecture et vérification du Magic number */
    ich1 = getc( ifp );
    if ( ich1 == EOF )
        pm_erreur( "EOF / erreur de lecture / nombre magique" );
    ich2 = getc( ifp );
    if ( ich2 == EOF )
        pm_erreur( "EOF / erreur de lecture / nombre magique" );
    if(ich2 != '1')
      pm_erreur(" mauvais type de fichier ");

    /* Lecture des dimensions */
    cols = pm_getint( ifp );
    rows = pm_getint( ifp );

    /* Allocation memoire  */
    bitmap = (bit *) malloc(cols * rows * sizeof(bit));

    /* Lecture */
    for(i=0; i < rows; i++)
      for(j=0; j < cols ; j++)
	bitmap[i * cols + j] = pm_getbit(ifp);

    /* Ecriture */
   printf("P2\n");
   printf("%d %d \n", cols, rows);
   printf("1\n");
   for(i=0; i < rows; i++)
      for(j=0; j < cols ; j++)
	printf("%u ",bitmap[i * cols + j] );



      /* fermeture */
      fclose(ifp);
      return 0;
}
Exemple #3
0
ppm_t* ppm_read(char* filename) {
    FILE* ifp;
    ppm_t* ppm = malloc(sizeof(ppm_t));
    int ich1, ich2;
    pixel_format_t pixel_format;
    int i, j;

    ifp = fopen(filename,"r");
    if (ifp == NULL) {
      printf("error in opening file %s\n", filename);
      exit(1);
    }

    /*  Magic number reading */
    ich1 = getc( ifp );
    if ( ich1 == EOF )
        pm_erreur( "EOF / read error / magic number" );
    ich2 = getc( ifp );
    if ( ich2 == EOF )
        pm_erreur( "EOF /read error / magic number" );
    if(ich2 != '3' && ich2 != '6')
      pm_erreur(" wrong file type ");
    else
      if(ich2 == '3')
        pixel_format = ALPHA;
      else
        pixel_format = BINARY;

    /* Reading image dimensions */
    ppm->cols = pm_getint( ifp );
    ppm->rows = pm_getint( ifp );
    ppm->maxval = pm_getint( ifp );

    /* Memory allocation  */
    ppm->pixmap = (color*) malloc(ppm->cols * ppm->rows * sizeof(color));

    /* Reading */
    for(i=0; i < ppm->rows; i++) {
      for (j=0; j < ppm->cols ; j++) {
        if (pixel_format == BINARY) {
          PPM_AT(ppm, i, j).red = pm_getrawbyte(ifp) ;
          PPM_AT(ppm, i, j).green = pm_getrawbyte(ifp) ;
          PPM_AT(ppm, i, j).blue = pm_getrawbyte(ifp) ;
        }
        else {
          PPM_AT(ppm, i, j).red = pm_getint(ifp);
          PPM_AT(ppm, i, j).green = pm_getint(ifp);
          PPM_AT(ppm, i, j).blue = pm_getint(ifp);
        }
      }
    }

    fclose(ifp);

    return ppm;
}
Exemple #4
0
double pm_get_signed_double( FILE* file)
{
  register int sign;
  register char ch;
  register double i;
  register double v;
  register double nb;

  sign = 1;
  do
    {
      ch = pm_getc( file );
    }
  while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
  
  if ( (ch < '0' || ch > '9') && (ch != '-') )
    pm_erreur( "junk in file where an integer should be" );
  
  if (ch == '-') {
    sign = -1;
    ch = pm_getc( file );
    if ( ch < '0' || ch > '9' )
      pm_erreur( "junk in file where an integer should be" );
  }
  
  i = 0.0;
  do
    {
      i = i * 10.0 + (ch - '0');
      ch = pm_getc( file );
    }
  while ( ch >= '0' && ch <= '9' );
  if (ch == '.') {
    ch = pm_getc( file );
    v = 0.0;
    nb = 1;
    do
      {
	v = v * 10.0 + (ch - '0');
	nb *= 0.1;
	ch = pm_getc( file );
      }
    while ( ch >= '0' && ch <= '9' );
    i += v*nb;
  }
  
  i *= sign;

  return i;
}
Exemple #5
0
unsigned char pm_getrawbyte(FILE* file)
    {
    register int iby;

    iby = getc( file );
    if ( iby == EOF )
        pm_erreur("EOF / read error ici" );
    return (unsigned char) iby;
    }
Exemple #6
0
bit pm_getbit(FILE* file)
    {
    register char ch;

    do
        {
        ch = pm_getc( file );
        }
    while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );

    if ( ch != '0' && ch != '1' )
        pm_erreur("junk in file where bits should be" );

    return ( ch == '1' ) ? 1 : 0;
    }
Exemple #7
0
char pm_getc(FILE* file)
    {
    register int ich;
    register char ch;

    ich = getc( file );
    if ( ich == EOF )
        pm_erreur("EOF / read error" );
    ch = (char) ich;

    if ( ch == '#' )
        {
        do
            {
            ich = getc( file );
            if ( ich == EOF )
                pm_erreur("EOF / read error" );
            ch = (char) ich;
            }
        while ( ch != '\n' && ch != '\r' );
        }

    return ch;
    }
Exemple #8
0
int pm_getint( FILE* file)
    {
    register char ch;
    register int i;

    do
        {
        ch = pm_getc( file );
        }
    while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );

    if ( ch < '0' || ch > '9' )
        pm_erreur( "junk in file where an integer should be" );

    i = 0;
    do
        {
        i = i * 10 + ch - '0';
        ch = pm_getc( file );
        }
    while ( ch >= '0' && ch <= '9' );

    return i;
    }
int main(int argc, char* argv[])
    {
    FILE* ifp;
    gray* graymap;
    int ich1, ich2, rows, cols, maxval, pgmraw ;
    int i, j;


    /* Test des arguments */
    if ( argc != 2 ){
      printf("\nUsage : %s fichier \n\n", argv[0]);
      exit(0);
    }

    /* Ouverture */
    ifp = fopen(argv[1],"r");
    if (ifp == NULL) {
      printf("erreur d'ouverture du fichier %s\n", argv[1]);
      exit(1);
    }

    /* Lecture du Magic number */
    ich1 = getc( ifp );
    if ( ich1 == EOF )
        pm_erreur( "EOF / erreur de lecture / nombre magique" );
    ich2 = getc( ifp );
    if ( ich2 == EOF )
        pm_erreur( "EOF / erreur de lecture / nombre magique" );
    if(ich2 != '2' && ich2 != '5')
      pm_erreur(" mauvais type de fichier ");
    else
      if(ich2 == '2')
	pgmraw = 0;
      else pgmraw = 1;

    /* Lecture des dimensions [DONE] */
    cols = pm_getint( ifp );
    rows = pm_getint( ifp );
    
    /* Allocation memoire  */
    graymap = (gray *) malloc(cols * rows * sizeof(gray));

    /* Lecture [DONE] */
    maxval = pm_getint( ifp );
    for(i=0; i < rows; i++)
      for(j=0; j < cols ; j++)
	if (pgmraw) 
	  graymap[i * cols + j] = pm_getrawbyte(ifp);
	else 
	  graymap[i * cols + j] = pm_getint(ifp);
    
    /* Ecriture */
    if(pgmraw)
      printf("P2\n");
    else
      printf("P5\n");

    printf("%d %d \n", cols, rows);
    printf("%d\n",maxval);
    for(i=0; i < rows; i++)
      for(j=0; j < cols ; j++)
	if (pgmraw) /* on stocke l'information sous forme ASCII */
	  printf( "%u ", graymap[i * cols + j] );
	else /* on stocke l'information sous forme binaire */
	  printf( "%c", graymap[i * cols + j] );

      /* fermeture */
      fclose(ifp);
      return 0;
}