-
Notifications
You must be signed in to change notification settings - Fork 0
/
myImageIO.h
199 lines (149 loc) · 3.59 KB
/
myImageIO.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
class myImageData{
private:
int mW;
int mH;
int mCH;
double * mData;
void SkipComments(FILE *fp){
int ch;
char line[100];
while ((ch = fgetc(fp)) != EOF && isspace(ch))
;
if (ch == '#') {
fgets(line, sizeof(line), fp);
SkipComments(fp);
}else{
fseek(fp, -1, SEEK_CUR);
}
}
public:
myImageData(void){
this->mData = NULL;
};
~myImageData(){
if(this->mData != NULL){
delete [] this->mData;
}
}
void init(int W, int H, int CH){
this->mW = W;
this->mH = H;
this->mCH = CH;
if(this->mData != NULL) delete [] this->mData;
this->mData = new double[(this->mW)*(this->mH)*(this->mCH)];
}
int getWidth(void){
return this->mW;
}
int getHeight(void){
return this->mH;
}
int getCH(void){
return this->mCH;
}
double * getDataPtr(void){
return this->mData;
}
double get(int x, int y){
return this->mData[y*(this->mW) + x];
}
double get(int x, int y, int CH){
return this->mData[this->mCH * ( y*(this->mW) + x) + CH];
}
void set(int x, int y, double value){
this->mData[y*(this->mW) + x] = value;
}
void set(int x, int y, int CH, double value){
this->mData[this->mCH *(y*(this->mW) + x) + CH] = value;
}
void read(const char *filename);
void save(const char *filename);
};
void myImageData::read(const char *filename){
FILE *file = fopen(filename, "rb");
if(file == NULL){
printf("Cannot open %s\n", filename);
exit(1); //abnormal termination
}
printf("Read an image from: %s\n", filename);
// read ppm/pgm header
char buf[256];
char filetype[256];
int W, H, Range, CH;
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", filetype);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d%d", &W, &H);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d", &Range);
//printf("Header: %s, %d, %d, %d\n", filetype, W, H, Range);
SkipComments(file);
if(strcmp(filetype, "P5") == 0){
CH = 1;
}else if(strcmp(filetype, "P6") == 0){
CH = 3;
}else{
printf("Unknown image type\n");
exit(1); //abnormal termination
}
if(Range != 255){
printf("Invalid data\n");
exit(1); //abnormal termination
}
// create myImageData class
init(W, H, CH);
// read ppm data
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char [datalength];
fread(temp, sizeof(unsigned char), datalength, file);
double * ptr1 = this->mData;
unsigned char *ptr2 = temp;
for(int i = 0; i < datalength; i++){
*ptr1 = (double)*ptr2;
ptr1++;
ptr2++;
}
delete [] temp;
fclose(file);
}
void myImageData::save(const char *filename){
char filenamefull[256];
if(this->mCH == 1){
sprintf(filenamefull, "%s.pgm", filename);
}else{
sprintf(filenamefull, "%s.ppm", filename);
}
FILE *file = fopen(filenamefull, "wb");
printf("Write an image to: %s \n", filenamefull);
if(this->mCH == 1){
fprintf(file, "P5\n");
}else{
fprintf(file, "P6\n");
}
fprintf(file, "%d %d\n", this->mW, this->mH);
fprintf(file, "255\n");
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char [datalength];
double * ptr1 = this->mData;
unsigned char * ptr2 = temp;
for(int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if(value > 255) value = 255;
if(value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
fwrite(temp, sizeof(unsigned char), datalength, file);
delete [] temp;
//fprintf(file, "\n");
fclose(file);
}