forked from AravisProject/aravis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharvfakecamera.c
1123 lines (954 loc) · 31.1 KB
/
arvfakecamera.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
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
/* Aravis - Digital camera library
*
* Copyright © 2009-2022 Emmanuel Pacaud
*
* This library 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 2 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Emmanuel Pacaud <[email protected]>
*/
/**
* SECTION: arvfakecamera
* @short_description: Fake camera internals
*
* #ArvFakeCamera is a class that simulate a real camera, which provides
* methods for the implementation of #ArvFakeDevice and #ArvFakeStream.
*
* arv-fake-gv-camera is a GV camera simulator based on this class.
*/
#include <arvfakecamera.h>
#include <arvversion.h>
#include <arvgc.h>
#include <arvgcregisternode.h>
#include <arvgvcpprivate.h>
#include <arvbufferprivate.h>
#include <arvdebug.h>
#include <arvmiscprivate.h>
#include <string.h>
#include <math.h>
static char *arv_fake_camera_genicam_filename = NULL;
/*
* arv_set_fake_camera_genicam_filename:
* @filename: path to genicam file
*
* Sets name of genicam file. This needs to be called prior to
* instantiation of the fake camera. This function is not thread safe.
*/
void
arv_set_fake_camera_genicam_filename (const char *filename)
{
g_clear_pointer (&arv_fake_camera_genicam_filename, g_free);
arv_fake_camera_genicam_filename = g_strdup (filename);
}
static const char *
arv_get_fake_camera_genicam_filename (void)
{
return arv_fake_camera_genicam_filename;
}
typedef struct {
void *memory;
char *genicam_xml;
size_t genicam_xml_size;
guint32 frame_id;
double trigger_frequency;
GMutex fill_pattern_mutex;
ArvFakeCameraFillPattern fill_pattern_callback;
void *fill_pattern_data;
} ArvFakeCameraPrivate;
struct _ArvFakeCamera {
GObject object;
ArvFakeCameraPrivate *priv;
};
struct _ArvFakeCameraClass {
GObjectClass parent_class;
};
G_DEFINE_TYPE_WITH_CODE (ArvFakeCamera, arv_fake_camera, G_TYPE_OBJECT, G_ADD_PRIVATE (ArvFakeCamera))
/* ArvFakeCamera implementation */
gboolean
arv_fake_camera_read_memory (ArvFakeCamera *camera, guint32 address, guint32 size, void *buffer)
{
guint32 read_size;
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
g_return_val_if_fail (buffer != NULL, FALSE);
g_return_val_if_fail (size > 0, FALSE);
if (address < ARV_FAKE_CAMERA_MEMORY_SIZE) {
read_size = MIN (address + size, ARV_FAKE_CAMERA_MEMORY_SIZE) - address;
memcpy (buffer, ((char *) camera->priv->memory) + address, read_size);
if (read_size == size)
return TRUE;
size = size - read_size;
address = ARV_FAKE_CAMERA_MEMORY_SIZE;
buffer = ((char *) buffer) + read_size;
}
address -= ARV_FAKE_CAMERA_MEMORY_SIZE;
read_size = MIN (address + size, camera->priv->genicam_xml_size) - address;
memcpy (buffer, ((char *) camera->priv->genicam_xml) + address, read_size);
if (read_size < size)
memset (((char *) buffer) + read_size, 0, size - read_size);
return TRUE;
}
gboolean
arv_fake_camera_write_memory (ArvFakeCamera *camera, guint32 address, guint32 size, const void *buffer)
{
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
g_return_val_if_fail (address + size < ARV_FAKE_CAMERA_MEMORY_SIZE + camera->priv->genicam_xml_size, FALSE);
g_return_val_if_fail (buffer != NULL, FALSE);
g_return_val_if_fail (size > 0, FALSE);
/* genicam_data are read only */
if (address + size > ARV_FAKE_CAMERA_MEMORY_SIZE)
return FALSE;
memcpy (((char *) camera->priv->memory) + address, buffer, size);
return TRUE;
}
/**
* arv_fake_camera_read_register:
* @camera: a #ArvFakeCamera
* @address: the register address
* @value: (out): the register value
*
* Return value: true if the read succeeded, false otherwise
*/
gboolean
arv_fake_camera_read_register (ArvFakeCamera *camera, guint32 address, guint32 *value)
{
gboolean success;
guint32 be_value = 0;
g_return_val_if_fail (value != NULL, FALSE);
success = arv_fake_camera_read_memory (camera, address, sizeof (*value), &be_value);
*value = GUINT32_FROM_BE (be_value);
return success;
}
gboolean
arv_fake_camera_write_register (ArvFakeCamera *camera, guint32 address, guint32 value)
{
guint32 be_value = GUINT32_TO_BE (value);
return arv_fake_camera_write_memory (camera, address, sizeof (value), &be_value);
}
static guint32
_get_register (ArvFakeCamera *camera, guint32 address)
{
guint32 value;
if (address + sizeof (guint32) > ARV_FAKE_CAMERA_MEMORY_SIZE)
return 0;
value = *((guint32 *) (((char *)(camera->priv->memory) + address)));
return GUINT32_FROM_BE (value);
}
size_t
arv_fake_camera_get_payload (ArvFakeCamera *camera)
{
guint32 width, height, pixel_format;
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), 0);
width = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_WIDTH);
height = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_HEIGHT);
pixel_format = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_PIXEL_FORMAT);
return width * height * ARV_PIXEL_FORMAT_BIT_PER_PIXEL(pixel_format)/8;
}
/**
* arv_fake_camera_get_sleep_time_for_next_frame:
* @camera: a #ArvFakeCamera
* @next_timestamp_us: (out) (optional): the timestamp for the next frame in microseconds
*
* Return value: the sleep time for the next frame
*/
guint64
arv_fake_camera_get_sleep_time_for_next_frame (ArvFakeCamera *camera, guint64 *next_timestamp_us)
{
guint64 time_us;
guint64 sleep_time_us;
guint64 frame_period_time_us;
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), 0);
if (_get_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_MODE) == 1)
frame_period_time_us = 1000000L / camera->priv->trigger_frequency;
else
frame_period_time_us = (guint64) _get_register (camera, ARV_FAKE_CAMERA_REGISTER_ACQUISITION_FRAME_PERIOD_US);
if (frame_period_time_us == 0) {
arv_warning_misc ("Invalid zero frame period, defaulting to 1 second");
frame_period_time_us = 1000000L;
}
time_us = g_get_real_time ();
sleep_time_us = frame_period_time_us - (time_us % frame_period_time_us);
if (next_timestamp_us != NULL)
*next_timestamp_us = time_us + sleep_time_us;
return sleep_time_us;
}
void
arv_fake_camera_wait_for_next_frame (ArvFakeCamera *camera)
{
g_return_if_fail (ARV_IS_FAKE_CAMERA (camera));
g_usleep (arv_fake_camera_get_sleep_time_for_next_frame (camera, NULL));
}
static struct {
unsigned char r,g,b;
} jet_colormap [] =
{
{0 , 0 , 132},
{0 , 0 , 136},
{0 , 0 , 140},
{0 , 0 , 144},
{0 , 0 , 148},
{0 , 0 , 152},
{0 , 0 , 156},
{0 , 0 , 160},
{0 , 0 , 164},
{0 , 0 , 168},
{0 , 0 , 172},
{0 , 0 , 176},
{0 , 0 , 180},
{0 , 0 , 184},
{0 , 0 , 188},
{0 , 0 , 192},
{0 , 0 , 196},
{0 , 0 , 200},
{0 , 0 , 204},
{0 , 0 , 208},
{0 , 0 , 212},
{0 , 0 , 216},
{0 , 0 , 220},
{0 , 0 , 224},
{0 , 0 , 228},
{0 , 0 , 232},
{0 , 0 , 236},
{0 , 0 , 240},
{0 , 0 , 244},
{0 , 0 , 248},
{0 , 0 , 252},
{0 , 0 , 255},
{0 , 4 , 255},
{0 , 8 , 255},
{0 , 12 , 255},
{0 , 16 , 255},
{0 , 20 , 255},
{0 , 24 , 255},
{0 , 28 , 255},
{0 , 32 , 255},
{0 , 36 , 255},
{0 , 40 , 255},
{0 , 44 , 255},
{0 , 48 , 255},
{0 , 52 , 255},
{0 , 56 , 255},
{0 , 60 , 255},
{0 , 64 , 255},
{0 , 68 , 255},
{0 , 72 , 255},
{0 , 76 , 255},
{0 , 80 , 255},
{0 , 84 , 255},
{0 , 88 , 255},
{0 , 92 , 255},
{0 , 96 , 255},
{0 , 100 , 255},
{0 , 104 , 255},
{0 , 108 , 255},
{0 , 112 , 255},
{0 , 116 , 255},
{0 , 120 , 255},
{0 , 124 , 255},
{0 , 128 , 255},
{0 , 132 , 255},
{0 , 136 , 255},
{0 , 140 , 255},
{0 , 144 , 255},
{0 , 148 , 255},
{0 , 152 , 255},
{0 , 156 , 255},
{0 , 160 , 255},
{0 , 164 , 255},
{0 , 168 , 255},
{0 , 172 , 255},
{0 , 176 , 255},
{0 , 180 , 255},
{0 , 184 , 255},
{0 , 188 , 255},
{0 , 192 , 255},
{0 , 196 , 255},
{0 , 200 , 255},
{0 , 204 , 255},
{0 , 208 , 255},
{0 , 212 , 255},
{0 , 216 , 255},
{0 , 220 , 255},
{0 , 224 , 255},
{0 , 228 , 255},
{0 , 232 , 255},
{0 , 236 , 255},
{0 , 240 , 255},
{0 , 244 , 255},
{0 , 248 , 255},
{0 , 252 , 255},
{0 , 255 , 255},
{4 , 255 , 252},
{8 , 255 , 248},
{12 , 255 , 244},
{16 , 255 , 240},
{20 , 255 , 236},
{24 , 255 , 232},
{28 , 255 , 228},
{32 , 255 , 224},
{36 , 255 , 220},
{40 , 255 , 216},
{44 , 255 , 212},
{48 , 255 , 208},
{52 , 255 , 204},
{56 , 255 , 200},
{60 , 255 , 196},
{64 , 255 , 192},
{68 , 255 , 188},
{72 , 255 , 184},
{76 , 255 , 180},
{80 , 255 , 176},
{84 , 255 , 172},
{88 , 255 , 168},
{92 , 255 , 164},
{96 , 255 , 160},
{100, 255, 156},
{104, 255, 152},
{108, 255, 148},
{112, 255, 144},
{116, 255, 140},
{120, 255, 136},
{124, 255, 132},
{128, 255, 128},
{132, 255, 124},
{136, 255, 120},
{140, 255, 116},
{144, 255, 112},
{148, 255, 108},
{152, 255, 104},
{156, 255, 100},
{160, 255, 96},
{164, 255, 92},
{168, 255, 88},
{172, 255, 84},
{176, 255, 80},
{180, 255, 76},
{184, 255, 72},
{188, 255, 68},
{192, 255, 64},
{196, 255, 60},
{200, 255, 56},
{204, 255, 52},
{208, 255, 48},
{212, 255, 44},
{216, 255, 40},
{220, 255, 36},
{224, 255, 32},
{228, 255, 28},
{232, 255, 24},
{236, 255, 20},
{240, 255, 16},
{244, 255, 12},
{248, 255, 8},
{252, 255, 4},
{255, 255, 0},
{255, 252, 0},
{255, 248, 0},
{255, 244, 0},
{255, 240, 0},
{255, 236, 0},
{255, 232, 0},
{255, 228, 0},
{255, 224, 0},
{255, 220, 0},
{255, 216, 0},
{255, 212, 0},
{255, 208, 0},
{255, 204, 0},
{255, 200, 0},
{255, 196, 0},
{255, 192, 0},
{255, 188, 0},
{255, 184, 0},
{255, 180, 0},
{255, 176, 0},
{255, 172, 0},
{255, 168, 0},
{255, 164, 0},
{255, 160, 0},
{255, 156, 0},
{255, 152, 0},
{255, 148, 0},
{255, 144, 0},
{255, 140, 0},
{255, 136, 0},
{255, 132, 0},
{255, 128, 0},
{255, 124, 0},
{255, 120, 0},
{255, 116, 0},
{255, 112, 0},
{255, 108, 0},
{255, 104, 0},
{255, 100, 0},
{255, 96, 0},
{255, 92, 0},
{255, 88, 0},
{255, 84, 0},
{255, 80, 0},
{255, 76, 0},
{255, 72, 0},
{255, 68, 0},
{255, 64, 0},
{255, 60, 0},
{255, 56, 0},
{255, 52, 0},
{255, 48, 0},
{255, 44, 0},
{255, 40, 0},
{255, 36, 0},
{255, 32, 0},
{255, 28, 0},
{255, 24, 0},
{255, 20, 0},
{255, 16, 0},
{255, 12, 0},
{255, 8, 0},
{255, 4, 0},
{255, 0, 0},
{252, 0, 0},
{248, 0, 0},
{244, 0, 0},
{240, 0, 0},
{236, 0, 0},
{232, 0, 0},
{228, 0, 0},
{224, 0, 0},
{220, 0, 0},
{216, 0, 0},
{212, 0, 0},
{208, 0, 0},
{204, 0, 0},
{200, 0, 0},
{196, 0, 0},
{192, 0, 0},
{188, 0, 0},
{184, 0, 0},
{180, 0, 0},
{176, 0, 0},
{172, 0, 0},
{168, 0, 0},
{164, 0, 0},
{160, 0, 0},
{156, 0, 0},
{152, 0, 0},
{148, 0, 0},
{144, 0, 0},
{140, 0, 0},
{136, 0, 0},
{132, 0, 0},
{128, 0, 0},
};
static void
arv_fake_camera_diagonal_ramp (ArvBuffer *buffer, void *fill_pattern_data,
guint32 exposure_time_us,
guint32 gain,
ArvPixelFormat pixel_format)
{
double pixel_value;
double scale;
guint32 x, y;
guint32 width;
guint32 height;
g_return_if_fail (buffer != NULL);
g_return_if_fail (buffer->priv->n_parts == 1);
width = buffer->priv->parts[0].width;
height = buffer->priv->parts[0].height;
scale = 1.0 + gain + log10 ((double) exposure_time_us / 10000.0);
switch (pixel_format)
{
case ARV_PIXEL_FORMAT_MONO_8:
if (height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned char *pixel = &buffer->priv->data [y * width + x];
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
*pixel = CLAMP (pixel_value, 0, 255);
}
}
buffer->priv->received_size = height * width;
}
break;
case ARV_PIXEL_FORMAT_MONO_16:
if (2 * height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned short *pixel = (unsigned short *)&buffer->priv->data [2*y * width + 2*x];
pixel_value = (256*x + 256*buffer->priv->frame_id + 256*y) % 65535;
pixel_value *= scale;
*pixel = CLAMP (pixel_value, 0, 65535);
}
}
buffer->priv->received_size = 2 * height * width;
}
break;
case ARV_PIXEL_FORMAT_BAYER_BG_8:
if (height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned int index;
unsigned char *pixel;
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
index = CLAMP (pixel_value, 0, 255);
// BG
// GR
pixel = &buffer->priv->data [y * width + x];
if (x & 1) {
if (y & 1)
*pixel = jet_colormap [index].b;
else
*pixel = jet_colormap [index].g;
} else {
if (y & 1)
*pixel = jet_colormap [index].g;
else
*pixel = jet_colormap [index].r;
}
}
}
buffer->priv->received_size = height * width;
}
break;
case ARV_PIXEL_FORMAT_BAYER_GB_8:
if (height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned int index;
unsigned char *pixel;
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
index = CLAMP (pixel_value, 0, 255);
// GB
// RG
pixel = &buffer->priv->data [y * width + x];
if (x & 1) {
if (y & 1)
*pixel = jet_colormap [index].g;
else
*pixel = jet_colormap [index].b;
} else {
if (y & 1)
*pixel = jet_colormap [index].r;
else
*pixel = jet_colormap [index].g;
}
}
}
buffer->priv->received_size = height * width;
}
break;
case ARV_PIXEL_FORMAT_BAYER_GR_8:
if (height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned int index;
unsigned char *pixel;
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
index = CLAMP (pixel_value, 0, 255);
// GR
// BG
pixel = &buffer->priv->data [y * width + x];
if (x & 1) {
if (y & 1)
*pixel = jet_colormap [index].g;
else
*pixel = jet_colormap [index].r;
} else {
if (y & 1)
*pixel = jet_colormap [index].b;
else
*pixel = jet_colormap [index].g;
}
}
}
buffer->priv->received_size = height * width;
}
break;
case ARV_PIXEL_FORMAT_BAYER_RG_8:
if (height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned int index;
unsigned char *pixel;
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
index = CLAMP (pixel_value, 0, 255);
// RG
// GB
pixel = &buffer->priv->data [y * width + x];
if (x & 1) {
if (y & 1)
*pixel = jet_colormap [index].r;
else
*pixel = jet_colormap [index].g;
} else {
if (y & 1)
*pixel = jet_colormap [index].g;
else
*pixel = jet_colormap [index].b;
}
}
}
buffer->priv->received_size = height * width;
}
break;
case ARV_PIXEL_FORMAT_RGB_8_PACKED:
if (3 * height * width <= buffer->priv->allocated_size) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned char *pixel = &buffer->priv->data [3 * (y * width + x)];
unsigned int index;
pixel_value = (x + buffer->priv->frame_id + y) % 255;
pixel_value *= scale;
index = CLAMP (pixel_value, 0, 255);
pixel[0] = jet_colormap [index].r;
pixel[1] = jet_colormap [index].g;
pixel[2] = jet_colormap [index].b;
}
}
buffer->priv->received_size = 3 * height * width;
}
break;
default:
g_critical ("Unsupported pixel format");
break;
}
}
/**
* arv_fake_camera_set_fill_pattern:
* @camera: a #ArvFakeCamera
* @fill_pattern_callback: (scope call): callback for image filling
* @fill_pattern_data: (closure): image filling user data
*
* Sets the fill pattern callback for custom test images.
*/
void
arv_fake_camera_set_fill_pattern (ArvFakeCamera *camera,
ArvFakeCameraFillPattern fill_pattern_callback,
void *fill_pattern_data)
{
g_return_if_fail (ARV_IS_FAKE_CAMERA (camera));
g_mutex_lock (&camera->priv->fill_pattern_mutex);
if (fill_pattern_callback != NULL) {
camera->priv->fill_pattern_callback = fill_pattern_callback;
camera->priv->fill_pattern_data = fill_pattern_data;
} else {
camera->priv->fill_pattern_callback = arv_fake_camera_diagonal_ramp;
camera->priv->fill_pattern_data = NULL;
}
g_mutex_unlock (&camera->priv->fill_pattern_mutex);
}
/**
* arv_fake_camera_fill_buffer:
* @camera: a #ArvFakeCamera
* @buffer: the #ArvBuffer to fill
* @packet_size: (out) (optional): the packet size
*
* Fill a buffer with data from the fake camera.
*/
void
arv_fake_camera_fill_buffer (ArvFakeCamera *camera, ArvBuffer *buffer, guint32 *packet_size)
{
guint32 width;
guint32 height;
guint32 exposure_time_us;
guint32 gain;
guint32 pixel_format;
size_t payload;
if (camera == NULL || buffer == NULL)
return;
arv_buffer_set_n_parts(buffer, 1);
width = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_WIDTH);
height = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_HEIGHT);
payload = arv_fake_camera_get_payload (camera);
if (buffer->priv->allocated_size < payload) {
buffer->priv->status = ARV_BUFFER_STATUS_SIZE_MISMATCH;
return;
}
/* frame id is a 16 bit value, 0 is invalid */
camera->priv->frame_id = (camera->priv->frame_id + 1) % 65536;
if (camera->priv->frame_id == 0)
camera->priv->frame_id = 1;
buffer->priv->payload_type = ARV_BUFFER_PAYLOAD_TYPE_IMAGE;
buffer->priv->chunk_endianness = G_BIG_ENDIAN;
buffer->priv->status = ARV_BUFFER_STATUS_SUCCESS;
buffer->priv->timestamp_ns = g_get_real_time () * 1000;
buffer->priv->system_timestamp_ns = buffer->priv->timestamp_ns;
buffer->priv->frame_id = camera->priv->frame_id;
buffer->priv->parts[0].data_offset = 0;
buffer->priv->parts[0].component_id = 0;
buffer->priv->parts[0].data_type = ARV_BUFFER_PART_DATA_TYPE_2D_IMAGE;
buffer->priv->parts[0].pixel_format = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_PIXEL_FORMAT);
buffer->priv->parts[0].width = width;
buffer->priv->parts[0].height = height;
buffer->priv->parts[0].x_offset = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_X_OFFSET);
buffer->priv->parts[0].y_offset = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_Y_OFFSET);
buffer->priv->parts[0].x_padding = 0;
buffer->priv->parts[0].y_padding = 0;
g_mutex_lock (&camera->priv->fill_pattern_mutex);
arv_fake_camera_read_register (camera, ARV_FAKE_CAMERA_REGISTER_EXPOSURE_TIME_US, &exposure_time_us);
arv_fake_camera_read_register (camera, ARV_FAKE_CAMERA_REGISTER_GAIN_RAW, &gain);
arv_fake_camera_read_register (camera, ARV_FAKE_CAMERA_REGISTER_PIXEL_FORMAT, &pixel_format);
camera->priv->fill_pattern_callback (buffer, camera->priv->fill_pattern_data,
exposure_time_us, gain, pixel_format);
g_mutex_unlock (&camera->priv->fill_pattern_mutex);
buffer->priv->parts[0].size = buffer->priv->received_size;
if (packet_size != NULL)
*packet_size =
(_get_register (camera, ARV_GVBS_STREAM_CHANNEL_0_PACKET_SIZE_OFFSET) >>
ARV_GVBS_STREAM_CHANNEL_0_PACKET_SIZE_POS) &
ARV_GVBS_STREAM_CHANNEL_0_PACKET_SIZE_MASK;
}
void
arv_fake_camera_set_inet_address (ArvFakeCamera *camera, GInetAddress *address)
{
const guint8 *bytes;
g_return_if_fail (ARV_IS_FAKE_CAMERA (camera));
g_return_if_fail (G_IS_INET_ADDRESS (address));
g_return_if_fail (g_inet_address_get_family (address) == G_SOCKET_FAMILY_IPV4);
bytes = g_inet_address_to_bytes (address);
arv_fake_camera_write_memory (camera, ARV_GVBS_CURRENT_IP_ADDRESS_OFFSET,
g_inet_address_get_native_size (address), (char *) bytes);
}
guint32
arv_fake_camera_get_acquisition_status (ArvFakeCamera *camera)
{
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), 0);
return _get_register (camera, ARV_FAKE_CAMERA_REGISTER_ACQUISITION);
}
/**
* arv_fake_camera_get_stream_address:
* @camera: a #ArvFakeCamera
*
* Return value: (transfer full): the data stream #GSocketAddress for this camera
*/
GSocketAddress *
arv_fake_camera_get_stream_address (ArvFakeCamera *camera)
{
GSocketAddress *stream_socket_address;
GInetAddress *inet_address;
guint32 value;
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), NULL);
arv_fake_camera_read_memory (camera, ARV_GVBS_STREAM_CHANNEL_0_IP_ADDRESS_OFFSET, sizeof (value), &value);
inet_address = g_inet_address_new_from_bytes ((guint8 *) &value, G_SOCKET_FAMILY_IPV4);
stream_socket_address = g_inet_socket_address_new
(inet_address,
_get_register (camera, ARV_GVBS_STREAM_CHANNEL_0_PORT_OFFSET));
g_object_unref (inet_address);
return stream_socket_address;
}
void
arv_fake_camera_set_trigger_frequency (ArvFakeCamera *camera, double frequency)
{
g_return_if_fail (ARV_IS_FAKE_CAMERA (camera));
g_return_if_fail (frequency > 0.0);
camera->priv->trigger_frequency = frequency;
}
gboolean
arv_fake_camera_check_and_acknowledge_software_trigger (ArvFakeCamera *camera)
{
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
if (_get_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_SOFTWARE) == 1) {
arv_fake_camera_write_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_SOFTWARE, 0);
return TRUE;
}
return FALSE;
}
gboolean
arv_fake_camera_is_in_free_running_mode (ArvFakeCamera *camera)
{
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
if (_get_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_MODE) == 0 &&
_get_register (camera, ARV_FAKE_CAMERA_REGISTER_ACQUISITION_FRAME_PERIOD_US) > 0) {
return TRUE;
}
return FALSE;
}
gboolean
arv_fake_camera_is_in_software_trigger_mode (ArvFakeCamera *camera)
{
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
if (_get_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_MODE) == 1 &&
_get_register (camera, ARV_FAKE_CAMERA_REGISTER_TRIGGER_SOURCE) == 1) {
return TRUE;
}
return FALSE;
}
guint32
arv_fake_camera_get_control_channel_privilege (ArvFakeCamera *camera)
{
guint32 value;
arv_fake_camera_read_register (camera, ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_OFFSET, &value);
return value;
}
void
arv_fake_camera_set_control_channel_privilege (ArvFakeCamera *camera, guint32 privilege)
{
arv_fake_camera_write_register (camera, ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_OFFSET, privilege);
}
guint32
arv_fake_camera_get_heartbeat_timeout (ArvFakeCamera *camera)
{
guint32 value;
arv_fake_camera_read_register (camera, ARV_GVBS_HEARTBEAT_TIMEOUT_OFFSET, &value);
return value;
}
/**
* arv_fake_camera_get_genicam_xml:
* @camera: a #ArvFakeCamera
* @size: (out) (optional): the size of the returned XML string
*
* Return value: (transfer none): the genicam XML description of the camera
*/
const char *
arv_fake_camera_get_genicam_xml (ArvFakeCamera *camera, size_t *size)
{
if (size != NULL)
*size = 0;
g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), NULL);
if (size != NULL)
*size = camera->priv->genicam_xml_size;
return camera->priv->genicam_xml;
}
/* GObject implemenation */
ArvFakeCamera *
arv_fake_camera_new_full (const char *serial_number, const char *genicam_filename)
{
ArvFakeCamera *fake_camera;
GError *error = NULL;
char *filename;
void *memory;
char *xml_url;
g_return_val_if_fail (serial_number != NULL, NULL);
g_return_val_if_fail (*serial_number != '\0', NULL);
g_return_val_if_fail (strlen (serial_number) < ARV_GVBS_SERIAL_NUMBER_SIZE, NULL);
fake_camera = g_object_new (ARV_TYPE_FAKE_CAMERA, NULL);
memory = g_malloc0 (ARV_FAKE_CAMERA_MEMORY_SIZE);
g_mutex_init (&fake_camera->priv->fill_pattern_mutex);
fake_camera->priv->fill_pattern_callback = arv_fake_camera_diagonal_ramp;
fake_camera->priv->fill_pattern_data = NULL;
if (genicam_filename != NULL)
filename = g_strdup (genicam_filename);
else if (arv_get_fake_camera_genicam_filename () != NULL)
filename = g_strdup (arv_get_fake_camera_genicam_filename ());
else
filename = NULL;
if (filename) {
if (!g_file_get_contents (filename,
&fake_camera->priv->genicam_xml,
&fake_camera->priv->genicam_xml_size,
&error)) {
g_critical ("Failed to load genicam file '%s': %s",
filename, error != NULL ? error->message : "Unknown reason");