-
Notifications
You must be signed in to change notification settings - Fork 126
/
imageSet.c
executable file
·548 lines (485 loc) · 17.1 KB
/
imageSet.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* AR2/imageSet.c
* artoolkitX
*
* This file is part of artoolkitX.
*
* artoolkitX is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* artoolkitX 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2018 Realmax, Inc.
* Copyright 2015 Daqri, LLC.
* Copyright 2006-2015 ARToolworks, Inc.
*
* Author(s): Hirokazu Kato, Philip Lamb
*
*/
#include <ARX/AR/ar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
# define lroundf(x) ((x)>=0.0f?(long)((x)+0.5f):(long)((x)-0.5f))
#endif
#include <ARX/AR2/imageFormat.h>
#include <ARX/AR2/imageSet.h>
static AR2ImageT *ar2GenImageLayer1 ( ARUint8 *image, int xsize, int ysize, int nc, float srcdpi, float dstdpi );
static AR2ImageT *ar2GenImageLayer2 ( AR2ImageT *src, float dstdpi );
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
static void defocus_image ( ARUint8 *img, int xsize, int ysize, int n );
#endif
static AR2ImageSetT *ar2ReadImageSetOld( FILE *fp );
AR2ImageSetT *ar2GenImageSet( ARUint8 *image, int xsize, int ysize, int nc, float dpi, float dpi_list[], int dpi_num )
{
AR2ImageSetT *imageSet;
int i;
if( nc != 1 && nc != 3 ) return NULL;
if( dpi_num <= 0 ) return NULL;
if( dpi_list[0] > dpi ) return NULL;
for( i = 1; i < dpi_num; i++ ) {
if( dpi_list[i] > dpi_list[0] ) return NULL;
}
arMalloc( imageSet, AR2ImageSetT, 1 );
imageSet->num = dpi_num;
arMalloc( imageSet->scale, AR2ImageT*, imageSet->num );
imageSet->scale[0] = ar2GenImageLayer1( image, xsize, ysize, nc, dpi, dpi_list[0] );
for( i = 1; i < dpi_num; i++ ) {
imageSet->scale[i] = ar2GenImageLayer2( imageSet->scale[0], dpi_list[i] );
}
return imageSet;
}
AR2ImageSetT *ar2ReadImageSet( char *filename )
{
FILE *fp;
AR2JpegImageT *jpgImage;
AR2ImageSetT *imageSet;
float dpi;
int i, k1;
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
int j, k2;
ARUint *p1, *p2;
#endif
size_t len;
const char ext[] = ".iset";
char *buf;
len = strlen(filename) + strlen(ext) + 1; // +1 for nul terminator.
arMalloc(buf, char, len);
sprintf(buf, "%s%s", filename, ext);
fp = fopen(buf, "rb");
free(buf);
if (!fp) {
ARLOGe("Error: unable to open file '%s%s' for reading.\n", filename, ext);
return (NULL);
}
arMalloc( imageSet, AR2ImageSetT, 1 );
if( fread(&(imageSet->num), sizeof(imageSet->num), 1, fp) != 1 || imageSet->num <= 0) {
ARLOGe("Error reading imageSet.\n");
goto bail;
}
ARLOGi("Imageset contains %d images.\n", imageSet->num);
arMalloc( imageSet->scale, AR2ImageT*, imageSet->num );
arMalloc( imageSet->scale[0], AR2ImageT, 1 );
jpgImage = ar2ReadJpegImage2(fp); // Caller must free result.
if( jpgImage == NULL || jpgImage->nc != 1 ) {
ARLOGw("Falling back to reading '%s%s' in ARToolKit v4.x format.\n", filename, ext);
free(imageSet->scale[0]);
free(imageSet->scale);
free(imageSet);
if( jpgImage == NULL ) {
rewind(fp);
return ar2ReadImageSetOld(fp);
}
free(jpgImage); //COVHI10396
fclose(fp);
return NULL;
}
imageSet->scale[0]->xsize = jpgImage->xsize;
imageSet->scale[0]->ysize = jpgImage->ysize;
imageSet->scale[0]->dpi = jpgImage->dpi; // The dpi value is not read correctly by jpeglib embedded in OpenCV 2.2.x.
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
imageSet->scale[0]->imgBWBlur[0] = jpgImage->image;
// Create the blurred images.
for( j = 1; j < AR2_BLUR_IMAGE_MAX; j++ ) {
arMalloc( imageSet->scale[0]->imgBWBlur[j], ARUint8, imageSet->scale[0]->xsize * imageSet->scale[0]->ysize);
p1 = dst->imgBWBlur[0];
p2 = dst->imgBWBlur[i];
for( k1 = 0; k1 < imageSet->scale[0]->xsize * imageSet->scale[0]->ysize; k1++ ) *(p2++) = *(p1++);
defocus_image( imageSet->scale[0]->imgBWBlur[j], imageSet->scale[0]->xsize, imageSet->scale[0]->ysize, 3 );
}
#else
imageSet->scale[0]->imgBW = jpgImage->image;
#endif
free(jpgImage);
// Minify for the other scales.
// First, find the list of scales we wrote into the file.
fseek(fp, (long)(-(int)sizeof(dpi)*(imageSet->num - 1)), SEEK_END);
for( i = 1; i < imageSet->num; i++ ) {
if( fread(&dpi, sizeof(dpi), 1, fp) != 1 ) {
for( k1 = 0; k1 < i; k1++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( k2 = 0; k2 < AR2_BLUR_IMAGE_MAX; k2++ ) free(imageSet->scale[k1]->imgBWBlur[k2]);
#else
free(imageSet->scale[k1]->imgBW);
#endif
free(imageSet->scale[k1]);
}
goto bail1;
}
imageSet->scale[i] = ar2GenImageLayer2( imageSet->scale[0], dpi );
if( imageSet->scale[i] == NULL ) {
for( k1 = 0; k1 < i; k1++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( k2 = 0; k2 < AR2_BLUR_IMAGE_MAX; k2++ ) free(imageSet->scale[k1]->imgBWBlur[k2]);
#else
free(imageSet->scale[k1]->imgBW);
#endif
free(imageSet->scale[k1]);
}
goto bail1;
}
}
fclose(fp);
return imageSet;
bail1:
free(imageSet->scale);
bail:
free(imageSet);
fclose(fp);
return NULL;
}
int ar2WriteImageSet( char *filename, AR2ImageSetT *imageSet )
{
FILE *fp;
AR2JpegImageT jpegImage;
int i;
size_t len;
const char ext[] = ".iset";
char *buf;
len = strlen(filename) + strlen(ext) + 1; // +1 for nul terminator.
arMalloc(buf, char, len);
sprintf(buf, "%s%s", filename, ext);
if( (fp=fopen(buf, "wb")) == NULL ) {
ARLOGe("Error: unable to open file '%s' for writing.\n", buf);
free(buf);
return (-1);
}
free(buf);
if( fwrite(&(imageSet->num), sizeof(imageSet->num), 1, fp) != 1 ) goto bailBadWrite;
jpegImage.xsize = imageSet->scale[0]->xsize;
jpegImage.ysize = imageSet->scale[0]->ysize;
jpegImage.dpi = imageSet->scale[0]->dpi;
jpegImage.nc = 1;
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
jpegImage.image = imageSet->scale[0]->imgBWBlur[0];
#else
jpegImage.image = imageSet->scale[0]->imgBW;
#endif
if( ar2WriteJpegImage2(fp, &jpegImage, AR2_DEFAULT_JPEG_IMAGE_QUALITY) < 0 ) goto bailBadWrite;
for( i = 1; i < imageSet->num; i++ ) {
if( fwrite(&(imageSet->scale[i]->dpi), sizeof(imageSet->scale[i]->dpi), 1, fp) != 1 ) goto bailBadWrite;
}
fclose(fp);
return 0;
bailBadWrite:
ARLOGe("Error saving image set: error writing data.\n");
fclose(fp);
return (-1);
}
int ar2FreeImageSet( AR2ImageSetT **imageSet )
{
int i;
if( imageSet == NULL ) return -1;
if( *imageSet == NULL ) return -1;
for( i = 0; i < (*imageSet)->num; i++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( int j = 0; j < AR2_BLUR_IMAGE_MAX; j++ ) {
free( (*imageSet)->scale[i]->imgBWBlur[j] );
}
#else
free( (*imageSet)->scale[i]->imgBW );
#endif
free( (*imageSet)->scale[i] );
}
free( (*imageSet)->scale );
free( *imageSet );
*imageSet = NULL;
return 0;
}
static AR2ImageT *ar2GenImageLayer1( ARUint8 *image, int xsize, int ysize, int nc, float srcdpi, float dstdpi )
{
AR2ImageT *dst;
ARUint8 *p1, *p2;
int wx, wy;
int sx, sy, ex, ey;
int ii, jj, iii, jjj;
int co, value;
wx = (int)lroundf(xsize * dstdpi / srcdpi);
wy = (int)lroundf(ysize * dstdpi / srcdpi);
arMalloc( dst, AR2ImageT, 1 );
dst->xsize = wx;
dst->ysize = wy;
dst->dpi = dstdpi;
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( int i = 0; i < AR2_BLUR_IMAGE_MAX; i++ ) {
arMalloc( dst->imgWBlur[i], ARUint8, wx*wy );
}
p2 = dst->imgBWBlur[0];
#else
arMalloc( dst->imgBW, ARUint8, wx*wy );
p2 = dst->imgBW;
#endif
// Scale down, nearest neighbour.
for( jj = 0; jj < wy; jj++ ) {
sy = (int)lroundf( jj * srcdpi / dstdpi);
ey = (int)lroundf((jj+1) * srcdpi / dstdpi) - 1;
if( ey >= ysize ) ey = ysize - 1;
for( ii = 0; ii < wx; ii++ ) {
sx = (int)lroundf( ii * srcdpi / dstdpi);
ex = (int)lroundf((ii+1) * srcdpi / dstdpi) - 1;
if( ex >= xsize ) ex = xsize - 1;
co = value = 0;
if( nc == 1 ) {
for( jjj = sy; jjj <= ey; jjj++ ) {
p1 = &(image[(jjj*xsize+sx)*nc]);
for( iii = sx; iii <= ex; iii++ ) {
value += *(p1++);
co++;
}
}
}
else {
for( jjj = sy; jjj <= ey; jjj++ ) {
p1 = &(image[(jjj*xsize+sx)*nc]);
for( iii = sx; iii <= ex; iii++ ) {
value += *(p1++);
value += *(p1++);
value += *(p1++);
co+=3;
}
}
}
*(p2++) = value / co;
}
}
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( int i = 1; i < AR2_BLUR_IMAGE_MAX; i++ ) {
p1 = dst->imgBWBlue[0];
p2 = dst->imgBWBlue[i];
for( int j = 0; j < wx*wy; j++ ) *(p2++) = *(p1++);
defocus_image( dst->imgBWBlur[i], wx, wy, 2 );
}
#else
//defocus_image( dst->imgBW, wx, wy, 3 );
#endif
return dst;
}
static AR2ImageT *ar2GenImageLayer2( AR2ImageT *src, float dpi )
{
AR2ImageT *dst;
ARUint8 *p1, *p2;
int wx, wy;
int sx, sy, ex, ey;
int ii, jj, iii, jjj;
int co, value;
wx = (int)lroundf(src->xsize * dpi / src->dpi);
wy = (int)lroundf(src->ysize * dpi / src->dpi);
arMalloc( dst, AR2ImageT, 1 );
dst->xsize = wx;
dst->ysize = wy;
dst->dpi = dpi;
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( int i = 0; i < AR2_BLUR_IMAGE_MAX; i++ ) {
arMalloc( dst->imgBWBlur[i], ARUint8, wx*wy );
}
p2 = dst->imgBWBlue[0];
#else
arMalloc( dst->imgBW, ARUint8, wx*wy );
p2 = dst->imgBW;
#endif
for( jj = 0; jj < wy; jj++ ) {
sy = (int)lroundf( jj * src->dpi / dpi);
ey = (int)lroundf((jj+1) * src->dpi / dpi) - 1;
if( ey >= src->ysize ) ey = src->ysize - 1;
for( ii = 0; ii < wx; ii++ ) {
sx = (int)lroundf( ii * src->dpi / dpi);
ex = (int)lroundf((ii+1) * src->dpi / dpi) - 1;
if( ex >= src->xsize ) ex = src->xsize - 1;
co = value = 0;
for( jjj = sy; jjj <= ey; jjj++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
p1 = &(src->imgBWBlur[0][jjj*src->xsize+sx]);
#else
p1 = &(src->imgBW[jjj*src->xsize+sx]);
#endif
for( iii = sx; iii <= ex; iii++ ) {
value += *(p1++);
co++;
}
}
*(p2++) = value / co;
}
}
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
defocus_image( dst->imageBWBlur[0], wx, wy, 3 );
for( int i = 1; i < AR2_BLUR_IMAGE_MAX; i++ ) {
p1 = dst->imgBWBlue[0];
p2 = dst->imgBWBlue[i];
for( int j = 0; j < wx*wy; j++ ) *(p2++) = *(p1++);
defocus_image( dst->imgBWBlur[i], wx, wy, 2 );
}
#else
//defocus_image( dst->imgBW, wx, wy, 3 );
#endif
return dst;
}
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
static void defocus_image( ARUint8 *img, int xsize, int ysize, int n )
{
ARUint8 *wimg;
int isize;
ARUint8 *p1, *p2;
int i, j, k, w;
isize = xsize * ysize;
arMalloc( wimg, ARUint8, xsize*ysize );
for( k = 0; k < n; k++ ) {
if( k%2 == 0 ) {
p1 = img;
p2 = wimg;
}
else {
p1 = wimg;
p2 = img;
}
for( j = 0; j < ysize; j++ ) {
for( i = 0; i < xsize; i++ ) {
if( i == 0 || j == 0 || i == xsize-1 || j == ysize-1 ) {
*(p2++) = *(p1++);
continue;
}
w = *(p1-xsize-1) + *(p1-xsize) + *(p1-xsize+1)
+ *(p1-1) + *(p1) + *(p1+1)
+ *(p1+xsize-1) + *(p1+xsize) + *(p1+xsize+1);
*(p2++) = w / 9;
p1++;
}
}
}
if( n%2 == 1 ) {
p1 = wimg;
p2 = img;
for( i = 0; i < xsize*ysize; i++ ) *(p2++) = *(p1++);
}
free(wimg);
return;
}
#endif
static AR2ImageSetT *ar2ReadImageSetOld( FILE *fp )
{
AR2ImageSetT *imageSet;
int i, k;
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
int j, l;
#endif
arMalloc( imageSet, AR2ImageSetT, 1 );
if( fread(&(imageSet->num), sizeof(imageSet->num), 1, fp) != 1 || imageSet->num <= 0) {
ARLOGe("Error reading imageSet.\n");
goto bail;
}
arMalloc( imageSet->scale, AR2ImageT*, imageSet->num );
for( i = 0; i < imageSet->num; i++ ) {
arMalloc( imageSet->scale[i], AR2ImageT, 1 );
}
for( i = 0; i < imageSet->num; i++ ) {
if( fread(&(imageSet->scale[i]->xsize), sizeof(imageSet->scale[i]->xsize), 1, fp) != 1 ) {
for( k = 0; k < i; k++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( l = 0; l < AR2_BLUR_IMAGE_MAX; l++) free(imageSet->scale[k]->imgBWBlur[l]);
#else
free(imageSet->scale[k]->imgBW);
#endif
}
for( k = 0; k < imageSet->num; k++ ) free(imageSet->scale[k]);
goto bail1;
}
if( fread(&(imageSet->scale[i]->ysize), sizeof(imageSet->scale[i]->ysize), 1, fp) != 1 ) {
for( k = 0; k < i; k++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( l = 0; l < AR2_BLUR_IMAGE_MAX; l++) free(imageSet->scale[k]->imgBWBlur[l]);
#else
free(imageSet->scale[k]->imgBW);
#endif
}
for( k = 0; k < imageSet->num; k++ ) free(imageSet->scale[k]);
goto bail1;
}
if( fread(&(imageSet->scale[i]->dpi), sizeof(imageSet->scale[i]->dpi), 1, fp) != 1 ) {
for( k = 0; k < i; k++ ) {
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( l = 0; l < AR2_BLUR_IMAGE_MAX; l++) free(imageSet->scale[k]->imgBWBlur[l]);
#else
free(imageSet->scale[k]->imgBW);
#endif
}
for( k = 0; k < imageSet->num; k++ ) free(imageSet->scale[k]);
goto bail1;
}
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( j = 0; j < AR2_BLUR_IMAGE_MAX; j++ ) {
arMalloc( imageSet->scale[i]->imgBWBlur[j], ARUint8, imageSet->scale[i]->xsize * imageSet->scale[i]->ysize);
}
#else
arMalloc( imageSet->scale[i]->imgBW, ARUint8, imageSet->scale[i]->xsize * imageSet->scale[i]->ysize);
#endif
#if AR2_CAPABLE_ADAPTIVE_TEMPLATE
for( j = 0; j < AR2_BLUR_IMAGE_MAX; j++ ) {
if( fread(imageSet->scale[i]->imgBWBlur[j], sizeof(ARUint8), imageSet->scale[i]->xsize * imageSet->scale[i]->ysize, fp)
!= imageSet->scale[i]->xsize * imageSet->scale[i]->ysize ) {
for( k = 0; k <= i; k++ ) {
for( l = 0; l < AR2_BLUR_IMAGE_MAX; l++) free(imageSet->scale[k]->imgBWBlur[l]);
}
for( k = 0; k < imageSet->num; k++ ) free(imageSet->scale[k]);
goto bail1;
}
}
#else
if( fread(imageSet->scale[i]->imgBW, sizeof(ARUint8), imageSet->scale[i]->xsize * imageSet->scale[i]->ysize, fp)
!= imageSet->scale[i]->xsize * imageSet->scale[i]->ysize ) {
for( k = 0; k <= i; k++ ) {
free(imageSet->scale[k]->imgBW);
}
for( k = 0; k < imageSet->num; k++ ) free(imageSet->scale[k]);
goto bail1;
}
#endif
}
fclose(fp);
return imageSet;
bail1:
free(imageSet->scale);
bail:
free(imageSet);
fclose(fp);
return NULL;
}