-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyppm.c
343 lines (283 loc) · 8.85 KB
/
easyppm.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "easyppm.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
static void easyppm_abort(PPM* ppm, const char* fmt, ...);
static int easyppm_is_grey(ppmcolor c);
static int easyppm_is_black_white(ppmcolor c);
/*
* Creates new PPM from args, aborts if dimensions are invalid
*/
PPM easyppm_create(int width, int height, imagetype itype) {
PPM ppm;
if (width <= 0 || height <= 0)
easyppm_abort(NULL, "Invalid image dimensions (%d, %d)\n", width, height);
ppm.width = width;
ppm.height = height;
if (itype == IMAGETYPE_PBM || itype == IMAGETYPE_PGM) {
ppm.image = (PPMBYTE*)malloc(sizeof(*ppm.image) * width*height);
} else {
ppm.image = (PPMBYTE*)malloc(sizeof(*ppm.image) * width*height*EASYPPM_NUM_CHANNELS);
}
ppm.itype = itype;
return ppm;
}
/*
* Fill entire color buffer with specified color
*/
void easyppm_clear(PPM* ppm, ppmcolor c) {
int x, y;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_clear()\n");
if (ppm->itype == IMAGETYPE_PBM && !easyppm_is_black_white(c))
easyppm_abort(ppm, "Passed invalid color to easyppm_clear() for PBM image\n");
if (ppm->itype == IMAGETYPE_PGM && !easyppm_is_grey(c))
easyppm_abort(ppm, "Passed invalid color to easyppm_clear() for PGM image\n");
for (y = 0; y < ppm->height; y++)
for (x = 0; x < ppm->width; x++)
easyppm_set(ppm, x, y, c);
}
/*
* Set appropriate color (RGB or greyscale)
*/
void easyppm_set(PPM* ppm, int x, int y, ppmcolor c) {
int i;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_set()\n");
if (x < 0 || x >= ppm->width || y < 0 || y > ppm->height)
easyppm_abort(ppm, "Index out of bounds in easyppm_set (%d, %d)\n", x, y);
i = x + y*ppm->width;
if (ppm->itype == IMAGETYPE_PBM || ppm->itype == IMAGETYPE_PGM) {
ppm->image[i] = c.r;
} else {
ppm->image[EASYPPM_NUM_CHANNELS*i + 0] = c.r;
ppm->image[EASYPPM_NUM_CHANNELS*i + 1] = c.g;
ppm->image[EASYPPM_NUM_CHANNELS*i + 2] = c.b;
}
}
/*
* Get appropriate color (RGB or greyscale)
*/
ppmcolor easyppm_get(PPM* ppm, int x, int y) {
ppmcolor c;
int i;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_get()\n");
if (x < 0 || x >= ppm->width || y < 0 || y > ppm->height)
easyppm_abort(ppm, "Index out of bounds in easyppm_get (%d, %d)\n", x, y);
i = x + y*ppm->width;
if (ppm->itype == IMAGETYPE_PBM || ppm->itype == IMAGETYPE_PGM) {
c.r = ppm->image[i];
c.g = ppm->image[i];
c.b = ppm->image[i];
} else {
c.r = ppm->image[EASYPPM_NUM_CHANNELS*i + 0];
c.g = ppm->image[EASYPPM_NUM_CHANNELS*i + 1];
c.b = ppm->image[EASYPPM_NUM_CHANNELS*i + 2];
}
return c;
}
/*
* Create color from 8-bit RGB values
*/
ppmcolor easyppm_rgb(PPMBYTE r, PPMBYTE g, PPMBYTE b) {
ppmcolor c;
c.r = r;
c.g = g;
c.b = b;
return c;
}
/*
* Create color from 8-bit greyscale value
*/
ppmcolor easyppm_grey(PPMBYTE gr) {
ppmcolor c;
c.r = gr;
c.g = gr;
c.b = gr;
return c;
}
/*
* Create color from binary value: 0 for white
* pixel, 1 for black pixel
*/
ppmcolor easyppm_black_white(int bw) {
ppmcolor c;
bw = (bw == 0 ? 255 : 0);
c.r = (char) bw;
c.g = (char) bw;
c.b = (char) bw;
return c;
}
/*
* Gamma-correct entire image by specified amount
*/
void easyppm_gamma_correct(PPM* ppm, float gamma) {
int x, y;
float r, g, b;
float exp = 1 / gamma;
ppmcolor c;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_gamma_correct()\n");
for (y = 0; y < ppm->height; y++) {
for (x = 0; x < ppm->width; x++) {
c = easyppm_get(ppm, x, y);
r = (PPMBYTE)(powf(c.r / (float)EASYPPM_MAX_CHANNEL_VALUE, exp) * 255);
g = (PPMBYTE)(powf(c.g / (float)EASYPPM_MAX_CHANNEL_VALUE, exp) * 255);
b = (PPMBYTE)(powf(c.b / (float)EASYPPM_MAX_CHANNEL_VALUE, exp) * 255);
easyppm_set(ppm, x, y, easyppm_rgb( (char) r, (char) g, (char) b));
}
}
}
/*
* Invert image y-axis for applications that assume an origin
* in the lower left corner (easyppm defaults to an origin in
* the upper left corner)
*/
void easyppm_invert_y(PPM* ppm) {
int x, yt, yb;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_invert_y()\n");
for (yt = 0, yb = ppm->height-1; yt <= yb; yt++, yb--) {
for (x = 0; x < ppm->width; x++) {
ppmcolor tmp = easyppm_get(ppm, x, yb);
easyppm_set(ppm, x, yb, easyppm_get(ppm, x, yt));
easyppm_set(ppm, x, yt, tmp);
}
}
}
/*
* Read image from file. Aborts if file could not be opened
* or the dimensions are invalid.
*/
void easyppm_read(PPM* ppm, const char* path) {
FILE* fp;
char itypestr[3];
int width, height, dummy;
int x, y;
int gr;
int r, g, b;
ppmcolor c;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_read()\n");
easyppm_destroy(ppm);
fp = fopen(path, "r");
if (!fp)
easyppm_abort(NULL, "Could not open file %s for reading\n", path);
dummy = fscanf(fp, "%2s\n", itypestr);
if (strcmp(itypestr, "P1") == 0) {
ppm->itype = IMAGETYPE_PBM;
dummy = fscanf(fp, "%d %d\n", &width, &height);
} else if (strcmp(itypestr, "P2") == 0) {
ppm->itype = IMAGETYPE_PGM;
dummy = fscanf(fp, "%d %d %d\n", &width, &height, &dummy);
} else {
ppm->itype = IMAGETYPE_PPM;
dummy = fscanf(fp, "%d %d %d\n", &width, &height, &dummy);
}
ppm->width = width;
ppm->height = height;
if (width <= 0 || height <= 0) {
fclose(fp);
easyppm_abort(NULL, "Passed invalid dimensions to easyppm_read() (%d, %d)\n", width, height);
}
if (ppm->itype == IMAGETYPE_PBM || ppm->itype == IMAGETYPE_PGM) {
ppm->image = (PPMBYTE*)malloc(sizeof(*ppm->image) * width*height);
} else {
ppm->image = (PPMBYTE*)malloc(sizeof(*ppm->image) * width*height*EASYPPM_NUM_CHANNELS);
}
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (ppm->itype == IMAGETYPE_PBM) {
dummy = fscanf(fp, "%d\n", &gr);
gr = (gr == 0 ? 1 : 0);
c.r = (char) gr;
c.g = (char) gr;
c.b = (char) gr;
} else if (ppm->itype == IMAGETYPE_PGM) {
dummy = fscanf(fp, "%d\n", &gr);
c.r = (char) gr;
c.g = (char) gr;
c.b = (char) gr;
} else {
dummy = fscanf(fp, "%d %d %d\n", &r, &g, &b);
c.r = (char) r;
c.g = (char) g;
c.b = (char) b;
}
easyppm_set(ppm, x, y, c);
}
}
fclose(fp);
}
/*
* Write image to file. Aborts if file could not be opened or
* the file extension on the path doesn't match the image type
* (.pbm for PBM files, .pgm for PGM files, and .ppm for PPM
* files).
*/
void easyppm_write(PPM* ppm, const char* path) {
FILE* fp;
int x, y;
if (!ppm)
easyppm_abort(ppm, "Passed NULL PPM to easyppm_write()\n");
fp = fopen(path, "w");
if (!fp)
easyppm_abort(ppm, "Could not open file %s for writing\n", path);
if (ppm->itype == IMAGETYPE_PBM) {
fprintf(fp, "P1\n");
fprintf(fp, "%d %d\n", ppm->width, ppm->height);
} else if (ppm->itype == IMAGETYPE_PGM) {
fprintf(fp, "P2\n");
fprintf(fp, "%d %d %d\n", ppm->width, ppm->height, EASYPPM_MAX_CHANNEL_VALUE);
} else {
fprintf(fp, "P3\n");
fprintf(fp, "%d %d %d\n", ppm->width, ppm->height, EASYPPM_MAX_CHANNEL_VALUE);
}
for (y = 0; y < ppm->height; y++) {
for (x = 0; x < ppm->width; x++) {
ppmcolor c = easyppm_get(ppm, x, y);
if (ppm->itype == IMAGETYPE_PBM) {
fprintf(fp, "%d\n", c.r == 0 ? 1 : 0);
} else if (ppm->itype == IMAGETYPE_PGM) {
fprintf(fp, "%d\n", c.r);
} else {
fprintf(fp, "%d %d %d\n", c.r, c.g, c.b);
}
}
}
fclose(fp);
}
/*
* Free image buffer
*/
void easyppm_destroy(PPM* ppm) {
if (ppm && ppm->image)
free(ppm->image);
}
/*
* Cleanup resources and abort program
*/
static void easyppm_abort(PPM* ppm, const char* fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
vfprintf(stderr, fmt, argptr);
va_end(argptr);
fprintf(stderr, "Aborting\n");
easyppm_destroy(ppm);
exit(EXIT_FAILURE);
}
/*
* Determine if color is greyscale
*/
static int easyppm_is_grey(ppmcolor c) {
return c.r == c.g && c.g == c.b;
}
/*
* Determine if color is black or white
*/
static int easyppm_is_black_white(ppmcolor c) {
return easyppm_is_grey(c) && (c.r == 255 || c.r == 0);
}