-
Notifications
You must be signed in to change notification settings - Fork 1
/
wbPPM.cpp
184 lines (149 loc) · 4.21 KB
/
wbPPM.cpp
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
#include <wb.h>
#include <math.h>
static inline float _min(float x, float y) { return x < y ? x : y; }
static inline float _max(float x, float y) { return x > y ? x : y; }
static inline float _clamp(float x, float start, float end) {
return _min(_max(x, start), end);
}
static const char *skipSpaces(const char *line) {
while (*line == ' ' || *line == '\t') {
line++;
if (*line == '\0') {
break;
}
}
return line;
}
static char nextNonSpaceChar(const char *line0) {
const char *line = skipSpaces(line0);
return *line;
}
static wbBool isComment(const char *line) {
char nextChar = nextNonSpaceChar(line);
if (nextChar == '\0') {
return wbTrue;
} else {
return nextChar == '#';
}
}
static void parseDimensions(const char *line0, int *width, int *height) {
const char *line = skipSpaces(line0);
sscanf(line, "%d %d", width, height);
}
static void parseDimensions(const char *line0, int *width, int *height,
int *channels) {
const char *line = skipSpaces(line0);
sscanf(line, "%d %d %d", width, height, channels);
}
static void parseDepth(const char *line0, int *depth) {
const char *line = skipSpaces(line0);
sscanf(line, "%d", depth);
}
static char *nextLine(wbFile_t file) {
char *line = NULL;
while ((line = wbFile_readLine(file)) != NULL) {
if (!isComment(line)) {
break;
}
}
return line;
}
wbImage_t wbPPM_import(const char *filename) {
wbImage_t img;
wbFile_t file;
char *header;
char *line;
int ii, jj, kk, channels;
int width, height, depth;
unsigned char *charData, *charIter;
float *imgData, *floatIter;
float scale;
img = NULL;
file = wbFile_open(filename, "rb");
if (file == NULL) {
printf("Could not open %s\n", filename);
goto cleanup;
}
header = wbFile_readLine(file);
if (header == NULL) {
printf("Could not read from %s\n", filename);
goto cleanup;
} else if (strcmp(header, "P6") != 0 && strcmp(header, "P6\n") != 0 &&
strcmp(header, "S6") != 0 && strcmp(header, "S6\n") != 0) {
printf("Could find magic number for %s\n", filename);
goto cleanup;
}
// the line now contains the dimension information
channels = 3;
line = nextLine(file);
if (strcmp(header, "S6") != 0 && strcmp(header, "S6\n") != 0) {
parseDimensions(line, &width, &height, &channels);
} else {
parseDimensions(line, &width, &height);
}
// the line now contains the depth information
line = nextLine(file);
parseDepth(line, &depth);
// the rest of the lines contain the data in binary format
charData = (unsigned char *)wbFile_read(
file, width * channels * sizeof(unsigned char), height);
img = wbImage_new(width, height, channels);
imgData = wbImage_getData(img);
charIter = charData;
floatIter = imgData;
scale = 1.0f / ((float)depth);
for (ii = 0; ii < height; ii++) {
for (jj = 0; jj < width; jj++) {
for (kk = 0; kk < channels; kk++) {
*floatIter = ((float)*charIter) * scale;
floatIter++;
charIter++;
}
}
}
#ifdef LAZY_FILE_LOAD
wbDelete(charData);
#endif
cleanup:
wbFile_close(file);
return img;
}
void wbPPM_export(const char *filename, wbImage_t img) {
int ii;
int jj;
int kk;
int depth;
int width;
int height;
int channels;
wbFile_t file;
float *floatIter;
unsigned char *charData;
unsigned char *charIter;
file = wbFile_open(filename, "wb+");
width = wbImage_getWidth(img);
height = wbImage_getHeight(img);
channels = wbImage_getChannels(img);
depth = 255;
wbFile_writeLine(file, "P6");
wbFile_writeLine(file, "#Created via wbPPM Export");
wbFile_writeLine(file, wbString(width, " ", height));
wbFile_writeLine(file, wbString(depth));
charData = wbNewArray(unsigned char, width *height *channels);
charIter = charData;
floatIter = wbImage_getData(img);
for (ii = 0; ii < height; ii++) {
for (jj = 0; jj < width; jj++) {
for (kk = 0; kk < channels; kk++) {
*charIter = (unsigned char)ceil(_clamp(*floatIter, 0, 1) * depth);
floatIter++;
charIter++;
}
}
}
wbFile_write(file, charData, width * channels * sizeof(unsigned char),
height);
wbDelete(charData);
wbFile_delete(file);
return;
}