-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·481 lines (390 loc) · 12.4 KB
/
main.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
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
//=============================================================================
// System Includes
//=============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <csignal>
//=============================================================================
// PGR Includes
//=============================================================================
#include <ladybug.h>
#include <ladybuggeom.h>
#include <ladybugrenderer.h>
#include <ladybugstream.h>
//=============================================================================
// Linux/unix headers
//=============================================================================
#include <pthread.h>
#include <sys/time.h>
//=============================================================================
// OpenCV headers
//=============================================================================
#include <cv.h>
#include <highgui.h>
//=============================================================================
// Homebrew barrier synchonization for Cygwin
//=============================================================================
#ifdef __CYGWIN__
#define pthread_barrier_t barrier_t
#define pthread_barrier_attr_t barrier_attr_t
#define pthread_barrier_init(b,a,n) barrier_init(b,n)
#define pthread_barrier_destroy(b) barrier_destroy(b)
#define pthread_barrier_wait(b) barrier_wait(b)
#endif
struct barrier_t{
int needed;
int called;
pthread_mutex_t mutex;
pthread_cond_t cond;
} ;
int barrier_init(barrier_t *barrier,int needed)
{
barrier->needed = needed;
barrier->called = 0;
pthread_mutex_init(&barrier->mutex,NULL);
pthread_cond_init(&barrier->cond,NULL);
return 0;
}
int barrier_destroy(barrier_t *barrier)
{
pthread_mutex_destroy(&barrier->mutex);
pthread_cond_destroy(&barrier->cond);
return 0;
}
int barrier_wait(barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
barrier->called++;
if (barrier->called == barrier->needed) {
barrier->called = 0;
pthread_cond_broadcast(&barrier->cond);
} else {
pthread_cond_wait(&barrier->cond,&barrier->mutex);
}
pthread_mutex_unlock(&barrier->mutex);
return 0;
}
#define COLOR_PROCESSING_METHOD LADYBUG_DOWNSAMPLE4 // The fastest color method suitable for real-time usages
//#define COLOR_PROCESSING_METHOD LADYBUG_HQLINEAR // High quality method suitable for large stitched images
#define DATA_FORMAT LADYBUG_DATAFORMAT_COLOR_SEP_SEQUENTIAL_JPEG
#define IMAGE_IN_BUFFER 16
// The size of the stitched image
#define PANORAMIC_IMAGE_WIDTH 2048
#define PANORAMIC_IMAGE_HEIGHT 1024
unsigned int* pImageDataSize;
unsigned int* pImWidth;
unsigned int* pImHeight;
// Number of threads
// #define THREADS 2
int THREADS = 0;
// This variable is set to 0 when user type Ctrl-c
int execute = 1;
LadybugContext* context = NULL;
LadybugImage* image = NULL;
pthread_t* cam_threads;
pthread_barrier_t barr;
std::ofstream* pOfm;
std::string* pOutputName;
void CleanUp()
{
for(int i=0; i<THREADS; i++)
{
if(context[i])
{
ladybugStop( &(context[i]) );
ladybugDestroyContext( &(context[i]) );
}
}
if(context)
{
delete[] context;
context = NULL;
}
if(image)
{
delete[] image;
image = NULL;
}
pthread_barrier_destroy(&barr);
for(int i=0; i<THREADS; i++)
{
pOfm[i].close();
}
if(cam_threads) delete[] cam_threads;
if(pOfm) delete[] pOfm;
if(pOutputName) delete[] pOutputName;
if(pImageDataSize) delete[] pImageDataSize;
if(pImWidth) delete[] pImWidth;
if(pImHeight) delete[] pImHeight;
}
void trap(int signal)
{
execute = 0;
}
void HANDLE_ERROR(LadybugError error)
{
if( error != LADYBUG_OK )
{
printf( "Error! Ladybug library reported %s\n", ::ladybugErrorToString( error ) );
CleanUp();
exit(1);
}
}
//
// This function reads an image from camera
//
LadybugError startCamera(int CamIdx)
{
LadybugError error;
LadybugCameraInfo caminfo;
int retry = 10;
//
// create ladybug context
//
printf( "Creating ladybug context...\n" );
error = ladybugCreateContext( &(context[CamIdx]) );
if ( error != LADYBUG_OK )
{
printf( "Failed creating ladybug context. Exit.\n" );
return error;
}
//
// Initialize camera
//
printf( "Initializing camera...\n" );
error = ladybugInitializeFromIndex( context[CamIdx], CamIdx );
if( error != LADYBUG_OK ) return error;
//
// Get camera info
//
printf( "Getting camera info...\n" );
error = ladybugGetCameraInfo( context[CamIdx], &caminfo );
if( error != LADYBUG_OK ) return error;
//
// Load config file from the camera head itself
//
printf( "Load configuration file...\n" );
error = ladybugLoadConfig( context[CamIdx], NULL);
if( error != LADYBUG_OK ) return error;
//
// Set the panoramic view angle
//
error = ladybugSetPanoramicViewingAngle( context, LADYBUG_FRONT_0_POLE_5);
if( error != LADYBUG_OK ) return error;
//
// Make the rendering engine use the alpha mask
//
error = ladybugSetAlphaMasking( context[CamIdx], true );
if( error != LADYBUG_OK ) return error;
//
// Set color processing method.
//
error = ladybugSetColorProcessingMethod( context[CamIdx], COLOR_PROCESSING_METHOD );
if( error != LADYBUG_OK ) return error;
//
// Configure output images in Ladybug liabrary
//
printf( "Configure output images in Ladybug library...\n" );
error = ladybugConfigureOutputImages( context, LADYBUG_PANORAMIC );
if( error != LADYBUG_OK ) return error;
error = ladybugSetOffScreenImageSize(
context, LADYBUG_PANORAMIC,
PANORAMIC_IMAGE_WIDTH, PANORAMIC_IMAGE_HEIGHT );
if( error != LADYBUG_OK ) return error;
//
// Auto buffer usage feature
//
error = ladybugSetAutoJPEGQualityControlFlag(context[CamIdx], true);
if( error != LADYBUG_OK ) return error;
//
// Do not wait if there is no image waiting
//
// ladybugSetGrabTimeout(context[CamIdx], 0);
switch( caminfo.deviceType )
{
case LADYBUG_DEVICE_COMPRESSOR:
case LADYBUG_DEVICE_LADYBUG3:
printf( "Starting Ladybug camera...\n" );
error = ladybugStart(
context[CamIdx],
LADYBUG_RESOLUTION_ANY,
DATA_FORMAT);
break;
case LADYBUG_DEVICE_LADYBUG:
default:
printf( "Unsupported device.\n");
error = LADYBUG_FAILED;
}
if( error != LADYBUG_OK ) return error;
//
// Grab an image to inspect the image size
//
error = LADYBUG_FAILED;
while ( error != LADYBUG_OK && retry-- > 0)
{
error = ladybugGrabImage( context[CamIdx], &(image[CamIdx]) );
}
if( error != LADYBUG_OK ) return error;
pImageDataSize[CamIdx] = image[CamIdx].uiDataSizeBytes;
pImWidth[CamIdx] = image[CamIdx].uiCols;
pImHeight[CamIdx] = image[CamIdx].uiRows;
return LADYBUG_OK;
}
void* CameraCtrl(void* idx);
//=============================================================================
// Main Routine
//=============================================================================
int main( int argc, char* argv[] )
{
int ArgCount = 1;
THREADS = atoi(argv[ArgCount++]);
pOutputName = new std::string[THREADS];
for(int i=0; i<THREADS; i++)
{
pOutputName[i] = argv[ArgCount++];
}
pImageDataSize = new unsigned int[THREADS];
pImWidth = new unsigned int[THREADS];
pImHeight = new unsigned int[THREADS];
context = new LadybugContext[THREADS];
image = new LadybugImage[THREADS];
memset(context, 0, THREADS*sizeof(LadybugContext));
memset(context, 0, THREADS*sizeof(LadybugImage));
cam_threads = new pthread_t[THREADS];
pOfm = new std::ofstream[THREADS];
printf("Number of cameras: %d\n", THREADS);
for(int i=0; i<THREADS; i++)
{
printf("Output file name: %s\n", pOutputName[i].c_str());
}
for(int i=0; i<THREADS; i++)
{
pOfm[i].open(pOutputName[i].c_str(), std::ios::binary | std::ios::trunc);
if(!pOfm[i].good())
{
printf("Error creating output file!\n");
CleanUp();
exit(1);
}
}
// Barrier Synchronization
if( pthread_barrier_init(&barr, NULL, THREADS) )
{
printf("Could not initialize pthread barrier");
CleanUp();
exit(1);
}
LadybugError error;
bool ErrorDetected = false;
//
// Initialize and start the camera
//
for(int i=0; i<THREADS; i++)
{
error = startCamera(i);
if( error!= LADYBUG_OK)
{
ErrorDetected = true;
break;
}
}
if(ErrorDetected)
HANDLE_ERROR(error);
signal(SIGINT, &trap);
for(int i=0; i<THREADS; i++)
{
pthread_create(&(cam_threads[i]), NULL, CameraCtrl, &i);
}
for(int i=0; i<THREADS; i++)
{
pthread_join(cam_threads[i], NULL);
}
signal(SIGINT, SIG_DFL);
printf("Cleaning up");
//
// clean up
//
CleanUp();
return 0;
}
void* CameraCtrl(void* idx)
{
LadybugError error;
int CamIdx = *((int*)idx);
unsigned int uiRawCols = 0;
unsigned int uiRawRows = 0;
//
// Set the size of the image to be processed
//
if( ( COLOR_PROCESSING_METHOD == LADYBUG_DOWNSAMPLE4) || ( COLOR_PROCESSING_METHOD == LADYBUG_MONO))
{
uiRawCols = pImageWidth[CamIdx]/2;
uiRawRows = pImageHeight[CamIdx]/2;
}
else
{
uiRawCols = pImageWidth[CamIdx]/2;
uiRawRows = pImageHeight[CamIdx]/2;
}
// Six buffers to hold the color processed (RGB) images
unsigned char* arpTextureBuffers[ LADYBUG_NUM_CAMERAS ] =
{ NULL,NULL,NULL,NULL,NULL,NULL };
//
// Initialize alpha mask size - this can take a long time if the
// masks are not present in the current directory.
//
printf( "Initializing alpha masks (this may take some time)...\n" );
error = ladybugInitializeAlphaMasks( context, uiRawCols, uiRawRows );
if(error != LADYBUG_OK) printf("Camera %d error initializing alpha masks:", error);
// error = ladybugSetPanoramicMappingType( context, LADYBUG_MAP_CYLINDRICAL);
error = ladybugSetPanoramicMappingType( context, LADYBUG_MAP_RADIAL);
if(error != LADYBUG_OK) printf("Camera %d error setting panoramic mapping type", error);
//
// Allocate the image buffers that hold the color-processed
// images for all cameras
//
for( i = 0; i < LADYBUG_NUM_CAMERAS; i++)
{
arpTextureBuffers[ i ] = new unsigned char[ uiRawCols * uiRawRows * 4 ];
}
IplImage* StitchedIm = cvCreateImageHeader(cvSize(PANORAMIC_IMAGE_WIDTH,PANORAMIC_IMAGE_HEIGHT), IPL_DEPTH_8U, 3);
std::string WinName = sprintf("%d", CamIdx);
// Create image window
cvNamedWindow( WinName.c_str(), CV_WINDOW_AUTOSIZE );
while(execute)
{
int rc = pthread_barrier_wait(&barr);
if(rc != 0)
{
printf("Could not wait on barrier\n");
}
// Grab an image from the camera
error = ladybugGrabImage( context[CamIdx], &(image[CamIdx]) );
if(error != LADYBUG_OK) printf("Camera %d error grabbing image:", error);
// Convert the image to 6 RGB buffers
error = ladybugConvertToMultipleBGRU32( context, &image, arpTextureBuffers, NULL );
if(error != LADYBUG_OK) printf("Camera %d error converting image:", error);
// Send the RGB buffers to the graphics card
error = ladybugUpdateTextures( context, LADYBUG_NUM_CAMERAS, (const unsigned char**)arpTextureBuffers);
if(error != LADYBUG_OK) printf("Camera %d error updaing textures:", error);
// Stitch the images (inside the graphics card) and retrieve the output to the user's memory
error = ladybugRenderOffScreenImage(context, LADYBUG_PANORAMIC, &StitchedIm);
if(error != LADYBUG_OK) printf("Camera %d error stitching image:", error);
cvShowImage(WinName.c_str(), StitchedIm);
}
printf("Cleaning up camera thread...\n");
delete[] DataBuffer;
for( i = 0; i < LADYBUG_NUM_CAMERAS; i++)
{
if( arpTextureBuffers[ i ] != NULL )
{
delete [] arpTextureBuffers[ i ];
}
}
cvReleaseImageHeader(&StitchedIm);
cvDestroyWindow( WinName.c_str());
pthread_exit(NULL);
}