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; }
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; }
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; }