-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinescan.c
More file actions
1760 lines (1421 loc) · 44.2 KB
/
linescan.c
File metadata and controls
1760 lines (1421 loc) · 44.2 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MALISCA linescanner
* ===============================
*
* with realtime preview
* Copyright © 2010, Michael Aschauer <m@ash.to>
* license: GPLv3 - see LICENSE or http://www.gnu.org/licenses/gpl-3.0.txt
*/
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <sys/prctl.h>
#include <locale.h>
#include <signal.h>
#include <errno.h>
#include "highgui.h"
#include "cv.h"
CvVideoWriter *writer;
IplImage* buffer4gl, *flatframe, *darkframe;
IplImage* tilebuffer[100];
IplImage* frame_crop;
IplImage* tile_tmp;
pthread_mutex_t frame_mutex;
IplImage* frame;
pthread_mutex_t last_full_frame_mutex;
IplImage* last_full_frame;
pthread_t view_thread;
int view_thread_id;
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
GstElement *sink, *pipeline;
#include <GL/glut.h>
#include <GL/gl.h>
#define IsRGB(s) ((s[0] == 'R') && (s[1] == 'G') && (s[2] == 'B'))
#define IsBGR(s) ((s[0] == 'B') && (s[1] == 'G') && (s[2] == 'R'))
#ifndef GL_CLAMP_TO_BORDER
#define GL_CLAMP_TO_BORDER 0x812D
#endif
#define GL_MIRROR_CLAMP_EXT 0x8742
void *font = GLUT_BITMAP_HELVETICA_12;
//void *font = GLUT_BITMAP_8_BY_13;
GLenum format;
GLuint texture[2];
int draw_line = 1;
int draw_zoom = 0;
int draw_grey = 0;
#include <sys/inotify.h>
#define I_EVENT_SIZE ( sizeof (struct inotify_event) )
#define I_BUF_LEN ( 1024 * ( I_EVENT_SIZE + 16 ) )
#include <sys/stat.h>
struct stat st;
#include <ctype.h>
#include <sys/types.h>
#include "gps.h"
struct gps_data_t *gpsdata;
struct gps_fix_t gpsfix;
char *gpsd_host;
char *gpsd_port;
char gps_status_str[255];
FILE *gpslog_file;
double distance=0;
char *conffile = "linescan.conf";
char *output_file;
char *watch_dir;
char *watch_src_cmd;
char *output_dir = "scan-data";
char *gst_pipeline;
char *gst_jp4pipeline;
char gst_default[255] = "videotestsrc ! ffmpegcolorspace";
char str_info[255];
char str_gps[255];
char size_str[20];
char utc[255];
int scanline = 0;
int line_height = 2;
int buf_height = 32;
int quality = 99;
long framecount = 0;
long outframecount = 0;
int done = 0;
int tilenr, tile_width, tile_height;
int preview_w = 512, preview_h = 512;
int flag_dropframes = 0;
int flag_write_movie = 1;
int flag_write_images = 0;
int flag_verbose = 0;
int flag_display = 1;
int flag_gps = 0;
int flag_watcher_mode = 0;
int flag_prescanned=0;
int flag_downscale=1;
int flag_jp4 = 0;
int flag_calib = 0;
int waiting_eos = 0;
double fps;
double fps_time;
double t, tf, tt, t_total;
char max=0, max_b=0, max_r=0, max_g=0,min_b=0, min_g=0, min_r=0;
// structures for config file
struct confopt {
const char *name;
enum {
co_int,
co_bool,
co_str,
} type;
union {
int *pc_int;
char **pc_str;
};
};
struct confopt confopt[] = {
{ "verbose", co_bool, { .pc_int = &flag_verbose } },
{ "jp4out", co_bool, { .pc_int = &flag_jp4 } },
{ "calib", co_bool, { .pc_int = &flag_calib } },
{ "dropframes", co_int, { .pc_int = &flag_dropframes } },
{ "bufferheight", co_int, { .pc_int = &buf_height } },
{ "lineheight", co_int, { .pc_int = &line_height } },
{ "quality", co_int, { .pc_int = &quality } },
{ "gstpipeline", co_str, { .pc_str = &gst_pipeline } },
{ "jp4pipeline", co_str, { .pc_str = &gst_jp4pipeline } },
{ "outfile", co_str, { .pc_str = &output_file } },
{ "display", co_bool, { .pc_int = &flag_display } },
{ "gps", co_bool, { .pc_int = &flag_gps } },
{ "downscale", co_bool, { .pc_int = &flag_downscale } },
{ "pre-scanned", co_bool, { .pc_int = &flag_prescanned } },
{ "preview-width", co_int, { .pc_int = &preview_w } },
{ "preview-height", co_int, { .pc_int = &preview_h } },
{ "watch", co_bool, { .pc_int = &flag_watcher_mode } },
{ "watch-dir", co_str, { .pc_str = &watch_dir } },
{ "watch-src-cmd", co_str, { .pc_str = &watch_src_cmd } },
{ NULL },
};
void mysignal_handler(int nr) {
printf("\nIGNORE SIGSEGV!\n", nr);
}
// read config file
void read_config(void) {
char line[255];
char *n, *v;
char f[255];
FILE *c;
int linenum = 0;
struct confopt *conf;
sprintf(f, "./%s", conffile);
printf("trying to read config file: %s\n", f);
c = fopen(f, "r");
/* fall back to home and etc if config is not in local folder
* does not work - why?
*/
if (c == NULL) {
sprintf(f, "~/.%s", conffile);
printf("trying to read config file: %s\n", f);
c = fopen(f, "r");
}
if (c == NULL) {
sprintf(f, "/etc/%s", conffile);
printf("trying to read config file: %s\n", f);
c = fopen(f, "r");
}
if (c == NULL) {
return;
}
while (fgets(line, sizeof(line), c) != NULL) {
if (strchr(line, '\n') != NULL) {
linenum++;
*strchr(line, '\n') = 0;
}
if (strchr(line, '#') != NULL)
*strchr(line, '#') = 0;
for (n = line; isspace((unsigned char)*n); n++)
;
if (*n == 0)
continue;
for (v = n; !isspace((unsigned char)*v) && *v != 0; v++)
;
*v = 0;
for (conf = confopt; conf->name != NULL; conf++)
if (strcmp(conf->name, n) == 0)
break;
if (conf->name == NULL) {
printf("%s:%d: option `%s' is not valid",
conffile, linenum, n);
exit(1);
}
for (v++; isspace((unsigned char)*v); v++)
;
if (*v == 0) {
printf("%s:%d: option `%s' does not have a value",
conffile, linenum, n);
exit(1);
}
while (*v != 0 && isspace((unsigned char)v[strlen(v) - 1]))
v[strlen(v) - 1] = 0;
switch (conf->type) {
case co_int:
*conf->pc_int = atoi(v);
break;
case co_bool:
if (strcmp(v, "yes") == 0 ||
strcmp(v, "true") == 0 ||
strcmp(v, "1") == 0)
*conf->pc_int = 1;
else if (strcmp(v, "no") == 0 ||
strcmp(v, "false") == 0 ||
strcmp(v, "0") == 0)
*conf->pc_int = 0;
else {
printf("%s:%d: invalid boolean value",
conffile, linenum, n);
exit(1);
}
break;
case co_str:
if (*conf->pc_str != NULL)
free(*conf->pc_str);
*conf->pc_str = strdup(v);
break;
}
}
if (!gst_pipeline)
gst_pipeline = "videotestsrc ! ffmpegcolorspace";
}
void read_options(int argc, char *argv[]) {
int c;
static struct option long_options[] = {
/* These options set a flag. */
{"verbose", no_argument, &flag_verbose, 1},
{"brief", no_argument, &flag_verbose, 0},
{"display", no_argument, &flag_display, 1},
{"calib", no_argument, &flag_calib, 1},
{"jp4", no_argument, &flag_jp4, 1},
{"watch", no_argument, &flag_watcher_mode, 1},
{"nodisplay", no_argument, &flag_display, 0},
{"nowrite", no_argument, &flag_write_movie, 0},
{"pre", no_argument, &flag_prescanned, 1},
{"gps", no_argument, &flag_gps, 1},
{"no-downscale",no_argument, &flag_downscale, 0},
/* These options don't set a flag.
We distinguish them by their indices. */
{"output", required_argument, 0, 'o'},
{"bufferheight",required_argument, 0, 'b'},
{"lineheight", required_argument, 0, 'l'},
{"quality", required_argument, 0, 'q'},
{"watch-dir", required_argument, 0, 'i'},
{"preview-size",required_argument, 0, 's'},
{"test", no_argument, 0, 't'},
{0, 0, 0, 0}
};
while(1) {
int option_index = 0;
c = getopt_long (argc, argv,"o:b:l:ng:p:i:htvc:s:",long_options, &option_index);
if (c == -1)
break;
switch(c) {
case 0:
break;
case 'o':
output_file = optarg;
flag_write_movie = 1;
flag_write_images = 0;
break;
case 'l':
line_height = atoi(optarg);
break;
case 'b':
buf_height = atoi(optarg);
break;
case 'q':
quality = atoi(optarg);
break;
case 'g':
flag_gps = 1;
break;
case 'v':
flag_verbose = 1;
break;
case 'i':
watch_dir = optarg;
break;
case 's':
preview_h = atoi(optarg);
preview_w = atoi(optarg);
break;
case 't':
sprintf(gst_pipeline, "videotestsrc ! ffmpegcolorspace");
break;
case 'h':
default:
printf("Usage: linescan [options] file\n");
printf("Options:\n");
printf(" -o | --output FILE Write result to video FILE.avi \n");
printf(" --nowrite Dont't write movie file \n");
printf(" -l | --lineheight HEIGHT height of scanline [px]\n");
printf(" -b | --bufferheight HEIGHT height of buffer image [px]\n");
printf(" -q | --quality QUALITY JPEG quality of video file (0-100)\n");
printf(" -g | --gps log GPS data \n");
printf(" --verbose be verbose \n");
printf(" --jp4 jp4 mode (Elphel raw)\n");
printf(" --nodisplay run without preview\n");
printf(" --no-downscale(NOT YET) no downscale image for preview (slower and BROKEN!)\n");
printf(" --calib (NOT YET!) Use calibration (darkframe substraction and flatframe)\n");
printf(" --watch watcher mode (use intofiy to watch a directory)\n");
printf(" -i | --watch-dir directory to watch\n");
printf(" --watch-src-cmd command to launch for watching mode\n");
printf(" --pre source is already line-scanned\n");
printf(" -h | --help print this help\n");
exit(0);
}
}
}
void gps_process()
{
// nothing
}
void gps_askfordata()
{
while (gps_waiting(gpsdata,0)){
//gps_poll(gpsdata);
gps_read(gpsdata);
switch (gpsfix.mode) {
case MODE_2D:
//printf("2D FIX\n");
sprintf(gps_status_str,"2d");
break;
case MODE_3D:
//printf("3D FIX\n");
sprintf(gps_status_str,"3d");
break;
default:
//printf("NO FIX\n");
sprintf(gps_status_str,"none");
break;
}
if (gpsdata->status > 0 && gpsfix.mode >= MODE_2D) {
distance += earth_distance(
gpsfix.latitude, gpsfix.longitude,
gpsdata->fix.latitude, gpsdata->fix.longitude);
}
gpsfix = gpsdata->fix;
}
}
void gps_setup()
{
char logfilename[255];
int ret;
gpsdata = malloc(sizeof(struct gps_data_t));
gpsd_host = "localhost";
gpsd_port = "2947";
if (gpsd_port == NULL) sprintf(gpsd_port,"%d",2947);
if (gpsd_host == NULL) sprintf(gpsd_host,"%s","127.0.0.1");
ret = gps_open(gpsd_host, gpsd_port, gpsdata);
if (!gpsdata) {
fprintf(stderr,
"no gpsd running or network error: %d, %s\n (%s:%s)\n",
errno, gps_errstr(errno), gpsd_host, gpsd_port);
flag_gps = 0;
}
else {
//gps_set_raw_hook(gpsdata, gps_process);
gps_stream(gpsdata, WATCH_ENABLE|WATCH_NEWSTYLE, NULL);
if (flag_jp4) {
sprintf(logfilename, "%s-jp4.avi.log",strtok(output_file,"."));
}
else {
sprintf(logfilename, "%s.log",output_file);
}
gpslog_file = fopen(logfilename,"w");
if (!gpslog_file) {
fprintf(stderr,
"could not open gps-logfile: %s",
logfilename);
}
else {
fprintf(gpslog_file,"#frame/tile; UTC; mode; \
latitude; longitude; altitude; \
speed; distance; track; climb; epx; epy; epv; \
satellites_visible; satellites_used\n");
}
}
gps_clear_fix(&gpsfix);
}
void gps_log()
{
char utc[255];
unix_to_iso8601(gpsdata->fix.time, utc, sizeof(utc));
if (gpslog_file && gpsfix.mode >= MODE_2D && isnan(gpsfix.latitude)==0) {
//if ( (gpsfix.mode >= MODE_2D) && (isnan(gpsfix.latitude)==0) ) {
fprintf(gpslog_file,"%ld; %s; %s; %f; %f; %f; %f; %f; %f; %f; %f; %f; %f; %d; %d\n",
outframecount,
utc,
gps_status_str,
gpsfix.latitude,
gpsfix.longitude,
gpsfix.altitude,
gpsfix.speed * MPS_TO_KPH,
distance / 1000,
gpsfix.track,
gpsfix.climb,
gpsfix.epx,
gpsfix.epy,
gpsfix.epv,
gpsdata->satellites_visible,
gpsdata->satellites_used);
//}
}
}
void gl_init()
{
// create buffer image
if (flag_downscale) {
buffer4gl = cvCreateImage(cvSize(512, 512), 8, 3);
if (frame) printf("%d x %d\n", frame->width, frame->width);
else printf("has no frame");
} else {
buffer4gl = cvCreateImage(cvSize(frame->width, frame->width), 8, 3);
printf("%d x %d\n", frame->width, frame->width);
int i;
tilenr = frame->width / frame->height;
tile_width = (buffer4gl->width );
tile_height = (buffer4gl->height / tilenr);
for(i = 0; i < tilenr; i++) {
tilebuffer[i] = cvCreateImage(cvSize(frame->width, frame->height), 8, 3);
}
}
GLenum format = IsBGR(buffer4gl->channelSeq) ? GL_BGR_EXT : GL_RGBA;
/* new */
glLoadIdentity();
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//GL_LINEAR is better looking than GL_NEAREST but seems slower..
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
buffer4gl->width, buffer4gl->height,
0, format, GL_UNSIGNED_BYTE, buffer4gl->imageData);
}
void gl_write(float x, float y, char *string) {
int len, i;
glColor4f(1.0, 1.0, 1.0, 1.0);
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
void gl_upload(IplImage *f)
{
static char p_name[16];
if (!p_name)
prctl(PR_GET_NAME,p_name);
if (flag_verbose)
g_print("thread %s: enter upload\n", p_name);
double t = (double)cvGetTickCount();
//printf("gl_upload frame");
if (!f) {
printf("could not load frame");
}
else {
int i;
if (flag_downscale) {
if (!tile_tmp) {
tilenr = f->width / f->height;
tile_width = (buffer4gl->width );
tile_height = (buffer4gl->height / tilenr);
tile_tmp = cvCreateImage( cvSize( tile_width, tile_height), 8, 3 );
}
// update last tile
//cvResize(f, tile_tmp, 0);
cvSetImageROI(buffer4gl, cvRect( 0, (tilenr-1) * tile_height, tile_width, tile_height) );
cvResize(f, buffer4gl, 0);
cvResetImageROI(buffer4gl);
GLenum format = IsBGR(buffer4gl->channelSeq) ? GL_BGR_EXT : GL_RGBA;
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexSubImage2D(GL_TEXTURE_2D, 0,
0, 0,
buffer4gl->width, buffer4gl->height,
format, GL_UNSIGNED_BYTE,
buffer4gl->imageData);
//cvReleaseImage( &tile_tmp );
} else {
GLenum format = IsBGR(f->channelSeq) ? GL_BGR_EXT : GL_RGBA;
tilebuffer[0] = cvCloneImage(f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexSubImage2D(GL_TEXTURE_2D, 0,
0, (tilenr -1) * tile_height,
frame->width, frame->height,
format,
GL_UNSIGNED_BYTE,
f->imageData);
}
}
if (flag_verbose) g_print("thread %s: gl upload took %.2fms\n",
p_name,
( (double)cvGetTickCount() - t ) /
( (double)cvGetTickFrequency()*1000.)
);
}
void gl_zoom(IplImage *f) {
if(f) {
GLenum format = IsBGR(f->channelSeq) ? GL_BGR_EXT : GL_RGBA;
frame_crop = cvCreateImage( cvSize( buffer4gl->width, f->height), 8, 3 );
if (!texture[1]) {
glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
cvSetImageROI(frame, cvRect( (f->width - buffer4gl->width)/2, 0, buffer4gl->width, f->height) );
cvResize(f,frame_crop,0);
cvResetImageROI(frame);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
frame_crop->width, frame_crop->height,
0, format, GL_UNSIGNED_BYTE, frame_crop->imageData);
} else {
glBindTexture(GL_TEXTURE_2D, texture[1]);
cvSetImageROI(f, cvRect( (f->width - buffer4gl->width)/2, 0, buffer4gl->width, f->height) );
cvResize(f, frame_crop,0);
cvResetImageROI(f);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
frame_crop->width, frame_crop->height,
0, format, GL_UNSIGNED_BYTE, frame_crop->imageData);
}
cvReleaseImage( &frame_crop );
}
}
void gl_shiftTiles()
{
char p_name[16];
prctl(PR_GET_NAME,p_name);
if (flag_verbose) g_print("thread %s: enter shift tiles\n", p_name);
double t = (double)cvGetTickCount();
int i;
pthread_mutex_lock(&frame_mutex);
if(frame) {
//shift tiles
if (flag_downscale) {
if (!tile_tmp)
tile_tmp = cvCreateImage( cvSize( tile_width, tile_height), 8, 3 );
for(i = 0; i < tilenr -1 ; i++) {
cvSetImageROI(buffer4gl, cvRect(0, (i+1) * tile_height, tile_width, tile_height) );
cvResize(buffer4gl, tile_tmp, 0);
cvResetImageROI(buffer4gl);
cvSetImageROI(buffer4gl, cvRect( 0, (i) * tile_height, tile_width, tile_height) );
cvResize(tile_tmp, buffer4gl, 0);
cvResetImageROI(buffer4gl);
}
//cvReleaseImage( &tile_tmp );
} else {
GLenum format = IsBGR(buffer4gl->channelSeq) ? GL_BGR_EXT : GL_RGBA;
glBindTexture(GL_TEXTURE_2D, texture[0]);
for(i = tilenr -1 ; i > 0; i--) {
cvReleaseImage(&tilebuffer[i]);
tilebuffer[i] = cvCloneImage(tilebuffer[i-1]);
glTexSubImage2D(GL_TEXTURE_2D, 0,
0, (tilenr - i - 1) * tile_height,
tilebuffer[i]->width, tilebuffer[i]->height,
format,
GL_UNSIGNED_BYTE,
tilebuffer[i]->imageData);
}
}
}
pthread_mutex_unlock(&frame_mutex);
if (flag_verbose) g_print("thread %s: gl tileshift took %.2fms\n",
p_name,
( (double)cvGetTickCount() - t ) /
( (double)cvGetTickFrequency()*1000.)
);
}
void gl_reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-width,width,-height,height);
glMatrixMode(GL_MODELVIEW);
if (flag_verbose)
printf("reshaped\n");
}
void gl_draw()
{
static char p_name[16];
if (!p_name)
prctl(PR_GET_NAME,p_name);
if (flag_verbose)
g_print("thread %s: enter draw\n", p_name);
double t = (double)cvGetTickCount();
IplImage* frame_copy;
pthread_mutex_lock(&last_full_frame_mutex);
if (last_full_frame) {
gl_upload(last_full_frame);
if(draw_zoom) gl_zoom(last_full_frame);
//if(draw_zoom) gl_zoom(cvCloneImage(last_full_frame));
gl_shiftTiles();
cvReleaseImage( &last_full_frame );
}
pthread_mutex_unlock(&last_full_frame_mutex);
if(!flag_prescanned) {
pthread_mutex_lock(&frame_mutex);
if (frame) {
//frame_copy = cvCloneImage(frame);
gl_upload(frame);
//cvReleaseImage( &frame_copy );
}
pthread_mutex_unlock(&frame_mutex);
}
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, 0.0);
glRotatef(90, 0, 0, 1);
glRotatef(180, 1, 0, 0);
//double offset = ((2 / (double) tilenr / buf_height) * (double) scanline);
double offset = 0.0;
// make a quad for the main texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
if (!flag_downscale) {
preview_w = frame->width;
preview_h = frame->width;
}
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-preview_h, -preview_w - offset, 0);
glTexCoord2f(1, 0); glVertex3f( preview_h, -preview_w - offset, 0);
glTexCoord2f(1, 1); glVertex3f( preview_h, preview_w - offset, 0);
glTexCoord2f(0, 1); glVertex3f(-preview_h, preview_w - offset, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
// draw zoom in current tile
if(draw_zoom)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-preview_h, preview_w -buf_height , 0.);
glTexCoord2f(1, 0); glVertex3f( preview_h, preview_w -buf_height , 0.);
glTexCoord2f(1, 1); glVertex3f( preview_h, preview_w, 0.);
glTexCoord2f(0, 1); glVertex3f(-preview_h, preview_w, 0.);
glEnd();
glDisable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBegin(GL_LINES);
glVertex3f(-preview_h, preview_w -buf_height , 0.);
glVertex3f( preview_h, preview_w -buf_height , 0.);
glEnd();
}
glDisable(GL_TEXTURE_2D);
// draw surrounding lines
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBegin(GL_LINES);
glVertex3f(-preview_h,-preview_w , 0.);
glVertex3f(-preview_w, preview_w , 0.);
glVertex3f( preview_w, -preview_w, 0.);
glVertex3f( preview_w, preview_w, 0.);
glEnd();
glRotatef(90, 0, 0, 1);
// draw a line
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
if (draw_line > 0) {
glBegin(GL_LINES);
glVertex3f(-preview_h, 0.0, 0.0);
glVertex3f( preview_h, 0.0, 0.0);
glEnd();
}
// draw zoom in current tile
if(draw_grey)
{
glColor4f(.5f, .5f, .5f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0. , -preview_h , 0.);
glTexCoord2f(1, 0); glVertex3f(0. +buf_height, -preview_w , 0.);
glTexCoord2f(1, 1); glVertex3f(0. +buf_height, preview_w, 0.);
glTexCoord2f(0, 1); glVertex3f(0. , preview_h, 0.);
glEnd();
glDisable(GL_TEXTURE_2D);
}
// write text info
gl_write(-preview_h + 4, -preview_w - 8, str_info);
gl_write(-preview_h + 4, preview_w + 20, str_gps);
//gl_write(-508, 510, utc);
glutSwapBuffers();
if (flag_verbose) g_print("thread %s: draw function took %.2fms\n",
p_name,
( (double)cvGetTickCount() - t ) /
( (double)cvGetTickFrequency()*1000.)
);
}
void gl_timer(){
glutPostRedisplay();
glutTimerFunc(50,gl_timer,0);
}
void on_key_up(unsigned char key, int x, int y) {
if (key == 'l') {
draw_line = !draw_line;
}
else if (key == '+') {
line_height++;
}
else if (key == '-') {
if (line_height > 1) line_height--;
}
else if (key == 'z') {
draw_zoom = !draw_zoom;
}
else if (key == 'g') {
draw_grey = !draw_grey;
}
else if (key == 'c') {
flag_calib = !flag_calib;
}
else if (key == 'q') {
waiting_eos = 1;
}
}
void *gl_view_thread_func(void *arg) {
char* argv = "";
int argc = 0;
prctl(PR_SET_NAME,"LS-DISPLAY",0,0,0);
//signal(11,mysignal_handler);
if (flag_verbose)
printf("create view thread\n");
glutInit(&argc,&argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0,0);
if (flag_downscale) {
glutInitWindowSize(preview_w + 4, preview_h + 33);
} else {
while (!frame) {
sleep(1);
printf("display waiting for frame...\n");
}
glutInitWindowSize(frame->width +4, frame->width + 33);
}
if (glutCreateWindow("linescan") == GL_FALSE) exit(1);
gl_init();
glutReshapeFunc(gl_reshape);
glutDisplayFunc(gl_draw);
glutTimerFunc(33, gl_timer, 0);
glutKeyboardUpFunc(on_key_up);
glutMainLoop();
return 0;
}
int get_filesize(char* str, char* file)
{
long long filesize;
if( stat(file, &st) == -1)
filesize = 0;
else
filesize = (st.st_size);
if (filesize > (1024 * 1024 * 1024)) { // GB
sprintf(str,"%.1f%s" , (double) filesize / (1024 * 1024 * 1024), "GB");
} else if (filesize > (1024 * 1024)) {
sprintf(str,"%.1f%s" , (double) filesize / (1024 * 1024), "MB");
} else if (filesize > (1024)) {
sprintf(str,"%.1f%s" , (double) filesize / (1024), "KB");
} else {
sprintf(str,"%.1f%s" , (double) filesize, "B");
}
return 0;
}
void clear_frame(IplImage *frame)
{
//int x,y,j;
//for(y = 0; y < frame->height; y++)
// for(x = 0; x < frame->width; x++)
// for (j = 0; j < 3; j++)
// frame->imageData[y * frame->width * 3 + x * 3 + j] = 0;
bzero(frame->imageData,frame->height * frame->widthStep);
}
void write_images(IplImage *frame)
{
char p_name[16];
double tt;
tt = (double)cvGetTickCount();
char output_filename[255];
sprintf(output_filename, "%s/img-%06ld.jpg",
output_dir,
outframecount);
if (flag_verbose)
printf("thread %s: save to: %s\n",
p_name, output_filename);
if(!cvSaveImage(output_filename, frame, 0))
printf("Could not save: %s\n",output_filename);
if (flag_verbose)
printf("thread %s: save image took %.2fms\n",
p_name,
((double)cvGetTickCount() - tt ) /
((double)cvGetTickFrequency()*1000.) );
}
void write_movie_frame(IplImage *frame)
{
char p_name[16];
double tt;
tt = (double)cvGetTickCount();
cvWriteFrame(writer,frame);
if (flag_verbose) g_print("thread %s: save video frame took %.2fms\n",
p_name,
( (double)cvGetTickCount() - tt ) /
( (double)cvGetTickFrequency()*1000.)
);
get_filesize(size_str,output_file);
}
void write_movie_start(IplImage *frame)
{
CvSize imgSize;
imgSize.width = frame->width;
imgSize.height = frame->height;
if (flag_verbose)
printf("Write output to file: %s\n", output_file);
writer = cvCreateVideoWriter(
output_file,
CV_FOURCC('M','J','P','G'), quality, imgSize, 1);
//CV_FOURCC('I', '4', '2', '0'), 100, imgSize, 1);
}