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
|
int main (int argc,char *argv[])
{
MCharImage Image1,Image2;
Image1.Load(argv[1]);
}
int MCharImage::Load(const char* PpmOrPgm)
{
int IsColored= 0;
ifstream Image(PpmOrPgm); // créer un flot d'entrée associé à un fichier
if (Image.bad()) //** test la valeur de badbit pour savoir si le flot est dans un état irrécupérable
return 0;
char C;
while ((C = Image.get()) == '#') { do C = Image.get(); while (C != '\n'); };
Image.putback(C);
char Tmp[256];
Image.get(Tmp, 256, '\n');
Image.get();
if (!strcmp(Tmp, "P5")) IsColored= 0;
else if (!strcmp(Tmp, "P6") || !strcmp(Tmp, "P3")) IsColored= 1;
else return 0;
while ((C = Image.get()) == '#') { do C = Image.get(); while (C != '\n'); };
Image.putback(C);
int xx, yy, zz;
Image >> xx >> yy >> zz;
if ((xx <= 0) || (yy <= 0) || (zz!= 255 && zz!= 254))
return 0;
SetSize(xx,yy);
Image.get();
int L= x__*y__; // X__=xx y__=yy
if (!IsColored)
{
Image.read((char*)yuv, L);
}
else if (!strcmp(Tmp, "P6"))
{ // color binary
char* Tmp2= new char[3*L];
Image.read(Tmp2, 3*L);
for (int i= 0; i< L; i++)
yuv[i]= Tmp2[3*i+1];
delete[] Tmp2;
}
else if (!strcmp(Tmp, "P3"))
{ // color ascii
int R, G, B;
for (int i= 0; i< L; i++) {
Image >> R >> G >> B;
yuv[i]= G;
};
};
return 1;
} |
Partager