-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.c
320 lines (304 loc) · 6.35 KB
/
utils.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
/*
============================================================================
Name : general_lincoln.c
Author : LincolnHard
Version :
Copyright : free and open
Description : Hello World in C, Ansi-style
============================================================================
*/
#include "utils.h"
void* pool_starting_address;
char* pool_current_address;
unsigned int pool_current_alloc_size;
void init_memory_pool_lincoln
(
void
)
{
#ifdef WIN32
pool_starting_address = _aligned_malloc(MEMORYPOOL_TOTAL_SIZE, 0x20);
#else
posix_memalign(&pool_starting_address, 0x20, MEMORYPOOL_TOTAL_SIZE);
#endif
if(pool_starting_address == NULL)
{
puts(" failed creating memory pool ");
exit(1);
}
pool_current_address = (char*)pool_starting_address;
pool_current_alloc_size = 0;
}
void* malloc_lincoln
(
size_t len
)
{
void* ptr = NULL;
if(len <= 0)
{
len = 32;
}
pool_current_alloc_size += ((len + 0xF) & (~0xF));
if(pool_current_alloc_size >= MEMORYPOOL_TOTAL_SIZE)
{
puts("failed allocating memory from pool anymore");
free(pool_starting_address);
exit(1);
}
ptr = pool_current_address;
pool_current_address += ((len + 0xF) & (~0xF));
memset(ptr, 0, len);
return ptr;
}
void free_memory_pool_lincoln
(
void
)
{
#ifdef WIN32
_aligned_free(pool_starting_address);
#else
free(pool_starting_address);
#endif
}
//the vector here stores the copy of object instead of address
//push operation here doesn't check whether size is touch boundary or not
//no vector free here, because vector are allocated by pool here. it should be freed with pool
void vector_init_lincoln
(
vector_lincoln* vec,
size_t elem_size,
size_t init_elem_num
)
{
vec->elem_size = elem_size;
vec->num_alloc_elems = (init_elem_num != 0) ? init_elem_num : VECTOR_INIT_SIZE;
vec->num_elems = 0;
vec->beginning = malloc_lincoln(elem_size * vec->num_alloc_elems);
}
void vector_push_lincoln
(
vector_lincoln* vec,
void* elem
)
{
//check space first
if(vec->num_elems == vec->num_alloc_elems)
{
puts("failed push element into vector anymore");
free(pool_starting_address);
exit(1);
}
char* end_address = (char*)vec->beginning + vec->elem_size * vec->num_elems;
memcpy(end_address, elem, vec->elem_size);
vec->num_elems++;
}
void vector_pop_lincoln
(
vector_lincoln* vec,
void* elem
)
{
char* latest_address = (char*)vec->beginning + vec->elem_size * (vec->num_elems - 1);
memcpy(elem, latest_address, vec->elem_size);
vec->num_elems--;
}
void vector_clear_lincoln
(
vector_lincoln* vec
)
{
vec->num_elems = 0;
memset(vec->beginning, 0, vec->elem_size * vec->num_alloc_elems);
}
void vector_qsort_lincoln
(
vector_lincoln* vec,
int (*compare_func)(const void*, const void*)
)
{
qsort(vec->beginning, vec->num_elems, vec->elem_size, compare_func);
}
void read_pgm
(
char* filename,
unsigned char* src
)
{
const int IMG_TOTAL_PIXEL = IMG_WIDTH * IMG_HEIGHT;
FILE* fp;
int character;
char linecontent[15];
char* pch;
fp = fopen(filename, "r");
if(fp == NULL)
{
puts("no such image file");
return;
}
character = fgetc(fp);
if(character != 'P')
{
puts("not a PGM file");
}
character = fgetc(fp);
if(character != '5')
{
puts("not in PGM raw format");
}
fseek(fp, 3, SEEK_SET);
fgets(linecontent, 15, fp);
pch = strtok(linecontent, " ");
//the width check can be commented out if necessary
if(atoi(pch) != IMG_WIDTH)
{
puts("width is invalid");
}
pch = strtok(NULL, " ");
//the height check can be commented out if necessary
if(atoi(pch) != IMG_HEIGHT)
{
puts("height is invalid");
}
//skip the line that record max gray value in PGM (no need)
fgets(linecontent, 5, fp);
//start to read data to buffer
int i;
for(i = 0; i < IMG_TOTAL_PIXEL; ++i)
{
character = fgetc(fp);
src[i] = (unsigned char)character;
}
fclose(fp);
}
void write_pgm
(
char* filename,
const unsigned char* src,
imsize_u16_lincoln srcsize,
rect_u16_lincoln* roi
)
{
char linecontent[15];
FILE* fp;
fp = fopen(filename, "w");
fputs("P5", fp);
fputc('\n', fp);
if(roi == NULL)
{
//image width and height
sprintf(linecontent, "%d %d", srcsize.width, srcsize.height);
fputs(linecontent, fp);
fputc('\n', fp);
//max gray (note: hard code here)
fputs("255", fp);
fputc('\n', fp);
int i;
int totalpixel = srcsize.width * srcsize.height;
for(i = 0; i < totalpixel; ++i)
{
fputc(src[i], fp);
}
}
else
{
//image width and height
sprintf(linecontent, "%d %d", roi->w, roi->h);
fputs(linecontent, fp);
fputc('\n', fp);
//max gray (note: hard code here)
fputs("255", fp);
fputc('\n', fp);
int i, j;
for(j = 0; j < roi->h; ++j)
{
for(i = 0; i < roi->w; ++i)
{
fputc(src[roi->x + i + (roi->y + j) * srcsize.width], fp);
}
}
}
fclose(fp);
}
void draw_rectangle
(
unsigned char* src,
rect_u16_lincoln* rectptr
)
{
memset(src + rectptr->x + rectptr->y * IMG_WIDTH, 255, rectptr->w);
memset(src + rectptr->x + (rectptr->y + rectptr->h) * IMG_WIDTH, 255, rectptr->w);
}
unsigned int int_sqrt
(
unsigned int value
)
{
int i = 0;
unsigned int a = 0;
unsigned int b = 0;
unsigned int c = 0;
unsigned int v = value;
for(i = 0; i < 16; ++i)
{
c <<= 2;
c += (v >> 30);
v <<= 2;
a <<= 1;
b = (a << 1) | 1;
if(c >= b)
{
c -= b;
a++;
}
}
return a;
}
unsigned short u16_round_lincoln
(
float fvalue
)
{
return (unsigned short)(fvalue + (fvalue > 0 ? 0.5 : -0.5));
}
//resizing source using "nearest neighbor" interpolation
void resize_interpolation
(
const unsigned char* src,
const rect_u16_lincoln* srcroi,
const imsize_u16_lincoln dstsize,
unsigned char* dst
)
{
int dstW = (int)dstsize.width;
int dstH = (int)dstsize.height;
int srcW = 0;
int srcH = 0;
const unsigned char* startaddress = NULL;
if(srcroi == NULL)
{
srcW = IMG_WIDTH;
srcH = IMG_HEIGHT;
startaddress = src;
}
else
{
srcW = (int)srcroi->w;
srcH = (int)srcroi->h;
startaddress = src + srcroi->x + srcroi->y * IMG_WIDTH;
}
int i = 0;
int j = 0;
int width_ratio = (int)((srcW << 16) / dstW) + 1;
int height_ratio = (int)((srcH << 16) / dstH) + 1;
for(j = 0; j < dstH; ++j)
{
unsigned char* dst_row_temp = dst + dstW * j;
const unsigned char* src_row_temp = startaddress + IMG_WIDTH * ((j * height_ratio) >> 16);
for(i = 0; i < dstW; ++i)
{
dst_row_temp[i] = src_row_temp[(i * width_ratio) >> 16];
}
}
}