-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgm.c
More file actions
138 lines (111 loc) · 2.93 KB
/
Copy pathpgm.c
File metadata and controls
138 lines (111 loc) · 2.93 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "pgm.h"
#include <stdlib.h>
#include <stdio.h>
void * GetMem(size_t Num, size_t Tam) {
void * p;
p = calloc(Num, Tam);
if (p == NULL) {
fprintf(stderr, "Error in asigned memory.\n");
fflush(stderr);
exit(1);
}
return p;
}
void ** GetMem2D(int rows, int columns, int sizeofTipo) {//Contigua
int i;
void ** h;
void * h_container = calloc(rows*columns, sizeofTipo);
h = malloc(sizeof(void*)*rows);
for(int i = 0; i<rows; i++){
h[i] = &(h_container[i*sizeofTipo*columns]);
}
return h;
}
void Free2D(void ** h, int rows) {//Contigua
free(h[0]);
free(h);
}
int getint(FILE *fd){
int c, i;
char dummy[10000];
c = getc(fd);
while (1) /* buscar el siguiente entero */
{
if (c=='#') /* Descartar los comentarios */
fgets(dummy,9000,fd);
if (c==EOF){
return 0;
}
if (c>='0' && c<='9')
break; /* se ha encontrado lo que se buscaba */
c = getc(fd);
}
/* se esta al comienzo de un numero, avanzar hasta el final */
i = 0;
while (1) {
i = (i*10) + (c - '0');
c = getc(fd);
if (c==EOF) return (i);
if (c<'0' || c>'9') break;
}
return (i);
}
unsigned char** pgmread(char* filename, int* x_size, int* y_size) {
FILE *fd;
char header[100];
int tmp;
int x, y, Contador=0;
unsigned char **Imagen;
if ((fd=fopen(filename,"rb")) == NULL){
printf("No puedo abrir %s para lectura!\n", filename);
exit(0);
}
/* Leyendo el encabezado */
header[0]=fgetc(fd);
header[1]=fgetc(fd);
if(!(header[0]=='P' && header[1]=='5')){
printf("Error al leer el archivo\nLa imagen %s no es un PGM!\n",filename);
fclose(fd);
exit(0);
}
*x_size = getint(fd);
*y_size = getint(fd);
tmp = getint(fd);
Imagen = (unsigned char **)GetMem2D(*x_size, *y_size, sizeof(unsigned char));
/* Toma la imagen del archivo */
for (y=0; y<*y_size; y++)
for (x=0; x<*x_size; x++, Contador++)
Imagen[x][y]=(unsigned char)fgetc(fd);
/* Verifico que se leyeron todos los bytes */
if (Contador != (*x_size * *y_size)){
printf("Archivo con longitud incorrecta!\n");
exit(1);
}
fclose(fd);
printf("Cargando imagen '%s' de %ix%i\n", filename, *x_size, *y_size);
return Imagen;
}
int pgmwrite(unsigned char** Imagen, char* filename, int Largo, int Alto) {
FILE *Out;
int x, y, Contador=0;
if ((Out=fopen(filename,"wb")) == NULL){
printf("No puedo escribir la imagen!\n");
return 0;
}
/* Ponemos el encabezado de los PGM */
fprintf(Out,"P5\n");
fprintf(Out,"%d %d\n", Largo, Alto);
fprintf(Out,"255\n");
/* Vacio la imagen al archivo */
for (y=0; y<Alto; y++)
for (x=0; x<Largo; x++, Contador++)
fputc(Imagen[x][y], Out);
/* Verifico que se escriban todos los bytes */
if (Contador != (Largo*Alto)){
printf("Archivo con longitud incorrecta!\n");
return 0;
}
fclose(Out);
printf("\nGuardando imagen '%s' de %ix%i\n", filename, Largo, Alto);
return 1;
}