Práctica Final. Rutas Aéreas.
imagenES.cpp
Ir a la documentación de este archivo.
1 
12 #include <fstream>
13 #include <string>
14 #include "imagenES.h"
15 using namespace std;
16 
17 
18 TipoImagen LeerTipo(ifstream& f)
19 {
20  char c1,c2;
22 
23  if (f) {
24  c1=f.get();
25  c2=f.get();
26  if (f && c1=='P')
27  switch (c2) {
28  case '5': res= IMG_PGM; break;
29  case '6': res= IMG_PPM; break;
30  default: res= IMG_DESCONOCIDO;
31  }
32  }
33  return res;
34 }
35 
36 // _____________________________________________________________________________
37 
38 TipoImagen LeerTipoImagen(const char nombre[])
39 {
40  ifstream f(nombre);
41  return LeerTipo(f);
42 }
43 
44 
45 // _____________________________________________________________________________
46 
47 char SaltarSeparadores (ifstream& f)
48 {
49  char c;
50  do {
51  c= f.get();
52  } while (isspace(c));
53  f.putback(c);
54  return c;
55 }
56 
57 // _____________________________________________________________________________
58 
59 bool LeerCabecera (ifstream& f, int& filas, int& columnas)
60 {
61  int maxvalor;
62 
63  while (SaltarSeparadores(f)=='#')
64  f.ignore(10000,'\n');
65 
66  f >> columnas >> filas >> maxvalor;
67 
68  if (/*str &&*/ f && filas>0 && filas <5000 && columnas >0 && columnas<5000) {
69  f.get(); // Saltamos separador
70  return true;
71  }
72  else return false;
73 }
74 
75 // _____________________________________________________________________________
76 
77 TipoImagen LeerTipoImagen(const char nombre[], int& filas, int& columnas)
78 {
79  TipoImagen tipo;
80  filas=columnas=0;
81  ifstream f(nombre);
82 
83  tipo=LeerTipo(f);
84  if (tipo!=IMG_DESCONOCIDO)
85  if (!LeerCabecera(f,filas,columnas)) {
86  tipo=IMG_DESCONOCIDO;
87  }
88 
89  return tipo;
90 }
91 
92 
93 // _____________________________________________________________________________
94 
95 bool LeerImagenPPM (const char nombre[], int& filas, int& columnas, unsigned char buffer[])
96 {
97  bool exito= false;
98  filas=0;
99  columnas=0;
100  ifstream f(nombre);
101 
102  if (LeerTipo(f)==IMG_PPM)
103  if (LeerCabecera (f, filas, columnas))
104  if (f.read(reinterpret_cast<char *>(buffer),filas*columnas*3))
105  exito= true;
106 
107  return exito;
108 }
109 
110 
111 // _____________________________________________________________________________
112 
113 bool LeerImagenPGM (const char nombre[], int& filas, int& columnas, unsigned char buffer[])
114 {
115  bool exito= false;
116  filas=0;
117  columnas=0;
118  ifstream f(nombre);
119 
120  if (LeerTipo(f)==IMG_PGM)
121  if (LeerCabecera (f, filas, columnas))
122  if (f.read(reinterpret_cast<char *>(buffer),filas*columnas))
123  exito= true;
124 
125  return exito;
126 }
127 
128 
129 // _____________________________________________________________________________
130 
131 bool EscribirImagenPPM (const char nombre[], const unsigned char datos[], int filas, int columnas)
132 {
133  ofstream f(nombre);
134  bool res= true;
135 
136  if (f) {
137  f << "P6" << endl;
138  f << columnas << ' ' << filas << endl;
139  f << 255 << endl;
140  f.write(reinterpret_cast<const char *>(datos),filas*columnas*3);
141  if (!f) res=false;
142  }
143  return res;
144 }
145 // _____________________________________________________________________________
146 
147 bool EscribirImagenPGM (const char nombre[], const unsigned char datos[], int filas, int columnas)
148 {
149  ofstream f(nombre);
150  bool res= true;
151 
152  if (f) {
153  f << "P5" << endl;
154  f << columnas << ' ' << filas << endl;
155  f << 255 << endl;
156  f.write(reinterpret_cast<const char *>(datos),filas*columnas);
157  if (!f) res=false;
158  }
159  return res;
160 }
161 
162 
163 /* Fin Fichero: imagenES.cpp */
164 
bool LeerImagenPPM(const char nombre[], int &filas, int &columnas, unsigned char buffer[])
Lee una imagen de tipo PPM sobre memoria reservada.
Definition: imagenES.cpp:95
TipoImagen LeerTipoImagen(const char nombre[], int &filas, int &columnas)
Consulta el tipo de imagen del archivo y sus dimensiones.
Definition: imagenES.cpp:77
bool EscribirImagenPGM(const char nombre[], const unsigned char datos[], int filas, int columnas)
Escribe una imagen de tipo PGM.
Definition: imagenES.cpp:147
bool LeerImagenPGM(const char nombre[], int &filas, int &columnas, unsigned char buffer[])
Lee una imagen de tipo PGM sobre memoria reservada.
Definition: imagenES.cpp:113
bool EscribirImagenPPM(const char nombre[], const unsigned char datos[], int filas, int columnas)
Escribe una imagen de tipo PPM.
Definition: imagenES.cpp:131
Fichero cabecera para la E/S de imágenes.
TipoImagen
Tipo de imagen.
Definition: imagenES.h:24
@ IMG_PPM
Imagen tipo PPM.
Definition: imagenES.h:26
@ IMG_DESCONOCIDO
Tipo de imagen desconocido.
Definition: imagenES.h:24
@ IMG_PGM
Imagen tipo PGM.
Definition: imagenES.h:25