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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <string>
#include <iostream>
#include <fstream>
using namespace std;
unsigned char * lireImage(FILE * fic_image)
{
unsigned char *image;
char * buf=new char [255];
int ligne=1;
int i=1;
string heigh="";
string width="";
string chaine="";
fread(&buf[i],1,1,fic_image);
while( ligne<4){
if(buf[i]=='\n') {
ligne++;
i++;
fread(&buf[i],1,1,fic_image);
}
if(buf[i]=='#') {
while (buf[i]!='\n'){
i++;
fread(&buf[i],1,1,fic_image);
}
}
if (ligne==2)
{
if (buf[i]!='\n'){
chaine=chaine+buf[i];
}
i++;
fread(&buf[i],1,1,fic_image);
}
else
{
i++;
fread(&buf[i],1,1,fic_image);
}
}
for (int j=0;j<(chaine.length()/2);j++){
heigh= heigh + chaine[j];
}
for (int j=((chaine.length()/2)+1);j<chaine.length();j++){
width= width + chaine[j];
}
image = (unsigned char*)calloc(512 * 512 ,sizeof(unsigned char));
for (int j=0;j<512*512;j++){
fread(&image[i],1,1,fic_image);
//cout << "image[i] = "<< i << "\n";
i++;
}
fclose(fic_image);
return image;
}
FILE * ouvrirFichier(char* nomFichier)
{
FILE * fic_image;
fic_image = fopen(nomFichier,"rb");
if (fic_image == NULL)
{
cout <<"Erreur a l'ouverture de l'image \n" ;
exit(0);
}
else cout <<"Image ouverte avec succes \n";
return fic_image;
}
void EcrireImage(unsigned char* image, int taille, char* NomImage)
{
FILE * fic_image = fopen(NomImage,"wb");
if (fic_image == NULL)
{
printf("Erreur a la creation de l'image \n");
exit(0);
}
else
printf("Image cree avec succes \n");
for(int i=1;i<=taille;i++)
fwrite(&image[i],1,1,fic_image);
fclose(fic_image);
}
int main(){
FILE * fic_image = ouvrirFichier("./test.pgm");
unsigned char * I=lireImage(fic_image);
EcrireImage ( I,512*512,"test2.pgm");//marche pas (entete)
return 0;
} |
Partager