-
Notifications
You must be signed in to change notification settings - Fork 4
/
io.c
213 lines (184 loc) · 3.8 KB
/
io.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include "transfo.h"
#ifdef USE_CLOCK
#include <time.h>
#else
#include "cycles.h"
#endif
void read_look_up_table(FILE *map, unsigned char *lut)
{
int n = 0;
unsigned char val;
while (fread(&val, 1, 1, map) != 0) {
lut[n] = val;
n++;
}
}
void read_image(FILE *in, unsigned char *source_image, long size)
{
long i;
fseek(in, 1, SEEK_CUR);
for (i = 0; i < size; i++) {
if (fread(source_image + i, 1, 1, in) == 0) {
perror("fread");
exit(1);
}
}
}
void write_image(char *dest, unsigned char *img, long size, int width, int height, int maxval)
{
FILE *out;
long i;
out = fopen(dest, "w");
if (out == NULL)
{
perror(dest);
exit(EXIT_FAILURE);
}
fprintf(out, "P5\n");
fprintf(out, "%d %d\n", height, width);
fprintf(out, "%d\n", maxval);
for (i = 0; i < size; i ++) {
if (fwrite (img + i, 1, 1, out) == EOF) {
perror ("fwrite");
exit (1);
}
}
fclose (out);
}
#ifdef USE_CLOCK
clock_t transform_image(char *source, char *curve, int light, char *dest)
#else
double transform_image(char *source, char *curve, int light, char *dest)
#endif
{
FILE *in;
FILE *map;
char c1;
char c2;
int height;
int width;
int maxval;
long size;
unsigned char *source_image;
unsigned char *dest_image;
unsigned char *lut;
#ifdef USE_CLOCK
clock_t start;
clock_t stop;
clock_t t;
#else
double t;
#endif
in = fopen(source, "r");
if (in == NULL) {
perror(source);
exit(EXIT_FAILURE);
}
map = fopen(curve, "r");
if (map == NULL) {
perror(curve);
exit(EXIT_FAILURE);
}
fscanf(in, "%c", &c1);
fscanf(in, "%c", &c2);
if (c1 != 'P' || c2 != '5') {
fprintf(stderr, "Error, input file is not PGM\n");
exit(EXIT_FAILURE);
}
fscanf (in, "%d %d", &height, &width);
fscanf (in, "%d", &maxval);
if (maxval > 255) {
fprintf(stderr, "Input file is not a 8 bits gray image.\n");
exit(EXIT_FAILURE);
}
size = width * height;
printf ("%s: %d x %d = %ld pixels\n", source, width, height, size);
/*
* Allocate memory for images and look up table
*/
source_image = (unsigned char *)malloc(sizeof(unsigned char) * size);
if (source_image == NULL) {
perror ("malloc for source image");
exit (1);
}
dest_image = (unsigned char *)malloc(sizeof(unsigned char) * size);
if (dest_image == NULL) {
perror ("malloc for destination image");
exit (1);
}
lut = (unsigned char *)malloc(sizeof(unsigned char) * 256);
if (lut == NULL) {
perror ("malloc for lut");
exit (1);
}
read_look_up_table(map, lut);
read_image(in, source_image, size);
fclose (in);
#ifdef USE_CLOCK
start = clock();
#else
start_counter();
#endif
transfo (width, height, source_image, dest_image, lut, light);
#ifdef USE_CLOCK
stop = clock();
t = stop - start;
printf("%ld clock cycles.\n", t);
#else
t = get_counter();
printf("%f clock cycles.\n", t);
#endif
write_image(dest, dest_image, size, width, height, maxval);
free(source_image);
free(lut);
free(dest_image);
return t;
}
#define FNMAX 255
void run_transfo_file(FILE *tf)
{
char source[FNMAX];
char curve[FNMAX];
char dest[FNMAX];
int light;
#ifdef USE_CLOCK
clock_t total = 0;
#else
double total = 0;
#endif
while (fscanf(tf, "%s %s %d %s", source, curve, &light, dest) == 4) {
printf("%s %s %d %s\n", source, curve, light, dest);
total += transform_image(source, curve, light, dest);
}
#ifdef USE_CLOCK
printf("TOTAL: %ld clock cycles.\n", total);
#else
printf("TOTAL: %f clock cycles.\n", total);
#endif
}
int main (int ac, char *av[])
{
FILE *tf;
char *dname;
if (ac != 2) {
printf("Usage: %s transfofile\n", av[0]);
exit(1);
}
tf = fopen(av[1], "r");
if (tf == NULL) {
perror(av[1]);
exit(EXIT_FAILURE);
}
dname = dirname(av[1]);
if (chdir(dname) == -1) {
perror(dname);
exit(EXIT_FAILURE);
}
run_transfo_file(tf);
fclose(tf);
return 0;
}