1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| int ouvrir_image_ppm(Image * img){
int taille;
char buffer[256];
FILE * f=fopen(img->nomng,"r");
perror(strerror(errno));
fgets(buffer,256,f);
if(strncmp(buffer,"P5",strlen("P5"))!=0)
{
printf("erreur type de fichier P6 non trouve !\n");
fclose(f);
return 1;
}
/* lecture des eventuelles lignes de commentaires */
fgets(buffer, 256, f);
while(buffer[0] == '#')
fgets(buffer, 256, f);
sscanf(buffer,"%d %d",&img->w, &img->h);
if((img->w<=0)||(img->h<=0)){
printf("erreur largeur:%d hauteur:%d !\n", img->w,img->h );
fclose(f);
return 1;
}
taille= img->w*img->h;
printf("%d",taille);
img->pixel=(unsigned char *)malloc((img->w)*(img->h)*sizeof(unsigned char));
fgets(buffer, 256, f);
printf("buf:%s",buffer);
if(fread(img->pixel,sizeof(unsigned char),taille,f)!=taille){
printf("erreur fichier invalid");
fclose(f);
return 0;
}
return 0;
} |
Partager