/
image.cpp
307 lines (272 loc) · 5.95 KB
/
image.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
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
/*
* TU Eindhoven
* Eindhoven, The Netherlands
*
* Name : image.c
*
* Author : Francesco Comaschi (f.comaschi@tue.nl)
*
* Date : November 12, 2012
*
* Function : Functions to manage .pgm images and integral images
*
* History :
* 12-11-12 : Initial version.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>
*
* In other words, you are welcome to use, share and improve this program.
* You are forbidden to forbid anyone else to use, share and improve
* what you give them. Happy coding!
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "image.h"
#include "stdio-wrapper.h"
char* strrev(char* str)
{
char *p1, *p2;
if (!str || !*str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
//int chartoi(const char *string)
//{
// int i;
// i=0;
// while(*string)
// {
// // i<<3 is equivalent of multiplying by 2*2*2 or 8
// // so i<<3 + i<<1 means multiply by 10
// i=(i<<3) + (i<<1) + (*string - '0');
// string++;
//
// // Dont increment i!
//
// }
// return(i);
//}
int myatoi (char* string)
{
int sign = 1;
// how many characters in the string
int length = strlen(string);
int i = 0;
int number = 0;
// handle sign
if (string[0] == '-')
{
sign = -1;
i++;
}
// for (i; i < length; i++)
while(i < length)
{
// handle the decimal place if there is one
if (string[i] == '.')
break;
number = number * 10 + (string[i]- 48);
i++;
}
number *= sign;
return number;
}
void itochar(int x, char* szBuffer, int radix)
{
int i = 0, n, xx;
n = x;
while (n > 0)
{
xx = n%radix;
n = n/radix;
szBuffer[i++] = '0' + xx;
}
szBuffer[i] = '\0';
strrev(szBuffer);
}
int readPgm(char *fileName, MyImage *image)
{
FILE *in_file;
char ch;
int type;
char version[3];
char line[100];
char mystring [20];
char *pch;
int i;
long int position;
in_file = fopen(fileName, "r");
if (in_file == NULL)
{
printf("ERROR: Unable to open file %s\n\n", fileName);
return -1;
}
printf("\nReading image file: %s\n", fileName);
// Determine image type (only pgm format is allowed)*/
ch = fgetc(in_file);
if(ch != 'P')
{
printf("ERROR: Not valid pgm file type\n");
return -1;
}
ch = fgetc(in_file);
/*convert the one digit integer currently represented as a character to
an integer(48 == '0')*/
type = ch - 48;
if(type != 5)
{
printf("ERROR: only pgm raw format is allowed\n");
return -1;
}
// Skip comments
// char line[100];
while ((ch = fgetc(in_file)) != EOF && isspace(ch));
position = ftell(in_file);
// skip comments
if (ch == '#')
{
fgets(line, sizeof(line), in_file);
while ((ch = fgetc(in_file)) != EOF && isspace(ch));
position = ftell(in_file);
}
fseek(in_file, position-1, SEEK_SET);
fgets (mystring , 20, in_file);
pch = (char *)strtok(mystring," ");
image->width = atoi(pch);
pch = (char *)strtok(NULL," ");
image->height = atoi(pch);
fgets (mystring , 5, in_file);
image->maxgrey = atoi(mystring);
image->data = (unsigned char*)malloc(sizeof(unsigned char)*(image->height*image->width));//new unsigned char[row*col];
image->flag = 1;
for(i=0;i<(image->height*image->width);i++)
{
ch = fgetc(in_file);
image->data[i] = (unsigned char)ch;
}
fclose(in_file);
return 0;
}
int writePgm(char *fileName, MyImage *image)
{
char parameters_str[5];
int i;
const char *format = "P5";
if (image->flag == 0)
{
return -1;
}
FILE *fp = fopen(fileName, "w");
if (!fp)
{
printf("Unable to open file %s\n", fileName);
return -1;
}
fputs(format, fp);
fputc('\n', fp);
itochar(image->width, parameters_str, 10);
fputs(parameters_str, fp);
parameters_str[0] = 0;
fputc(' ', fp);
itochar(image->height, parameters_str, 10);
fputs(parameters_str, fp);
parameters_str[0] = 0;
fputc('\n', fp);
itochar(image->maxgrey, parameters_str, 10);
fputs(parameters_str, fp);
fputc('\n', fp);
for (i = 0; i < (image->width * image->height); i++)
{
fputc(image->data[i], fp);
}
fclose(fp);
return 0;
}
int cpyPgm(MyImage* src, MyImage* dst)
{
int i = 0;
if (src->flag == 0)
{
printf("No data available in the specified source image\n");
return -1;
}
dst->width = src->width;
dst->height = src->height;
dst->maxgrey = src->maxgrey;
dst->data = (unsigned char*)malloc(sizeof(unsigned char)*(dst->height*dst->width));
dst->flag = 1;
for (i = 0; i < (dst->width * dst->height); i++)
{
dst->data[i] = src->data[i];
}
}
void createImage(int width, int height, MyImage *image)
{
image->width = width;
image->height = height;
image->flag = 1;
image->data = (unsigned char *)malloc(sizeof(unsigned char)*(height*width));
}
void createSumImage(int width, int height, MyIntImage *image)
{
image->width = width;
image->height = height;
image->flag = 1;
image->data = (int *)malloc(sizeof(int)*(height*width));
}
int freeImage(MyImage* image)
{
if (image->flag == 0)
{
printf("no image to delete\n");
return -1;
}
else
{
// printf("image deleted\n");
free(image->data);
return 0;
}
}
int freeSumImage(MyIntImage* image)
{
if (image->flag == 0)
{
printf("no image to delete\n");
return -1;
}
else
{
// printf("image deleted\n");
free(image->data);
return 0;
}
}
void setImage(int width, int height, MyImage *image)
{
image->width = width;
image->height = height;
}
void setSumImage(int width, int height, MyIntImage *image)
{
image->width = width;
image->height = height;
}