-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_css_js_util.cpp
2265 lines (2063 loc) · 64.2 KB
/
html_css_js_util.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
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
/*
Copyright (c) 2020 Patrick Moffitt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "html_css_js_util.hpp"
/**
* The HTML body for the camera settings user interface.
*
* @return HTML
*/
const std::string html_css_js_util::body_html = R"END(
<body>
<h1>0V5642 Camera Settings</h1>
<div class="feature" id="brightness"><h2>Brightness</h2>
<div class="option" id="brightness_0">Brightness +4</div>
<div class="option" id="brightness_1">Brightness +3</div>
<div class="option" id="brightness_2">Brightness +2</div>
<div class="option" id="brightness_3">Brightness +1</div>
<div class="option" id="brightness_4">Brightness 0</div>
<div class="option" id="brightness_5">Brightness -1</div>
<div class="option" id="brightness_6">Brightness -2</div>
<div class="option" id="brightness_7">Brightness -3</div>
<div class="option" id="brightness_8">Brightness -4</div>
</div>
<div class="feature" id="color_saturation"><h2>Color Saturation</h2>
<div class="option" id="color_saturation_0">Saturation +4</div>
<div class="option" id="color_saturation_1">Saturation +3</div>
<div class="option" id="color_saturation_2">Saturation +2</div>
<div class="option" id="color_saturation_3">Saturation +1</div>
<div class="option" id="color_saturation_4">Saturation 0</div>
<div class="option" id="color_saturation_5">Saturation -1</div>
<div class="option" id="color_saturation_6">Saturation -2</div>
<div class="option" id="color_saturation_7">Saturation -3</div>
<div class="option" id="color_saturation_8">Saturation -4</div>
</div>
<div class="feature" id="contrast"><h2>Contrast</h2>
<div class="option" id="contrast_0">Contrast +4</div>
<div class="option" id="contrast_1">Contrast +3</div>
<div class="option" id="contrast_2">Contrast +2</div>
<div class="option" id="contrast_3">Contrast +1</div>
<div class="option" id="contrast_4">Contrast 0</div>
<div class="option" id="contrast_5">Contrast -1</div>
<div class="option" id="contrast_6">Contrast -2</div>
<div class="option" id="contrast_7">Contrast -3</div>
<div class="option" id="contrast_8">Contrast -4</div>
</div>
<div class="feature" id="effect"><h2>Special Effect</h2>
<div class="option" id="effect_0">Antique</div>
<div class="option" id="effect_1">Bluish</div>
<div class="option" id="effect_2">Greenish</div>
<div class="option" id="effect_3">Reddish</div>
<div class="option" id="effect_4">Black & White</div>
<div class="option" id="effect_5">Negative</div>
<div class="option" id="effect_6">B&W Negative</div>
<div class="option" id="effect_7">Normal</div>
<div class="option" id="effect_8">Sepia</div>
<div class="option" id="effect_9">Overexposure</div>
<div class="option" id="effect_10">Solarize</div>
<div class="option" id="effect_11">Blueish</div>
<div class="option" id="effect_12">Yellowish</div>
</div>
<div class="feature" id="exposure"><h2>Exposure</h2>
<div class="option" id="exposure_0">Exposure_17_EV</div>
<div class="option" id="exposure_1">Exposure_13_EV</div>
<div class="option" id="exposure_2">Exposure_10_EV</div>
<div class="option" id="exposure_3">Exposure_07_EV</div>
<div class="option" id="exposure_4">Exposure_03_EV</div>
<div class="option" id="exposure_5">Exposure_default</div>
<div class="option" id="exposure_6">Exposure03_EV</div>
<div class="option" id="exposure_7">Exposure07_EV</div>
<div class="option" id="exposure_8">Exposure10_EV</div>
<div class="option" id="exposure_9">Exposure13_EV</div>
<div class="option" id="exposure_10">Exposure17_EV</div>
</div>
<div class="feature" id="frames"><h2>Frames</h2>
<div class="option" id="frames_0">One</div>
<div class="option" id="frames_1">Two</div>
<div class="option" id="frames_2">Three</div>
<div class="option" id="frames_3">Four</div>
<div class="option" id="frames_4">Five</div>
<div class="option" id="frames_5">Six</div>
<div class="option" id="frames_255">Movie</div>
</div>
<div class="feature" id="hue"><h2>Hue</h2>
<div class="option" id="hue_0">degree_180</div>
<div class="option" id="hue_1">degree_150</div>
<div class="option" id="hue_2">degree_120</div>
<div class="option" id="hue_3">degree_90</div>
<div class="option" id="hue_4">degree_60</div>
<div class="option" id="hue_5">degree_30</div>
<div class="option" id="hue_6">degree_0</div>
<div class="option" id="hue_7">degree30</div>
<div class="option" id="hue_8">degree60</div>
<div class="option" id="hue_9">degree90</div>
<div class="option" id="hue_10">degree120</div>
<div class="option" id="hue_11">degree150</div>
</div>
<div class="feature" id="jpeg_size"><h2>Size</h2>
<div class="option" id="jpeg_size_0">OV5642_320x240</div>
<div class="option" id="jpeg_size_1">OV5642_640x480</div>
<div class="option" id="jpeg_size_2">OV5642_1024x768</div>
<div class="option" id="jpeg_size_3">OV5642_1280x960</div>
<div class="option" id="jpeg_size_4">OV5642_1600x1200</div>
<div class="option" id="jpeg_size_5">OV5642_2048x1536</div>
<div class="option" id="jpeg_size_6">OV5642_2592x1944</div>
</div>
<div class="feature" id="light_mode"><h2>Light Mode</h2>
<div class="option" id="light_mode_0">Advanced_AWB</div>
<div class="option" id="light_mode_1">Simple_AWB</div>
<div class="option" id="light_mode_2">Manual_day</div>
<div class="option" id="light_mode_3">Manual_A</div>
<div class="option" id="light_mode_4">Manual_cwf</div>
<div class="option" id="light_mode_5">Manual_cloudy</div>
</div>
<div class="feature" id="mirror"><h2>Mirror</h2>
<div class="option" id="mirror_7">Normal</div>
<!--- Only Flip works at present 05 June 2020.
https://github.com/ArduCAM/Arduino/issues/448
<div class="option" id="mirror_0">MIRROR</div> -->
<div class="option" id="mirror_1">FLIP</div>
<!---
<div class="option" id="mirror_2">MIRROR_FLIP</div>
-->
</div>
<div class="feature" id="quality"><h2>Quality</h2>
<div class="option" id="quality_0">high_quality</div>
<div class="option" id="quality_1">default_quality</div>
<div class="option" id="quality_2">low_quality</div>
</div>
<div class="feature" id="sharpness"><h2>Sharpness</h2>
<div class="option" id="sharpness_0">Auto_Sharpness_default</div>
<div class="option" id="sharpness_1">Auto_Sharpness1</div>
<div class="option" id="sharpness_2">Auto_Sharpness2</div>
<div class="option" id="sharpness_3">Manual_Sharpness_off</div>
<div class="option" id="sharpness_4">Manual_Sharpness1</div>
<div class="option" id="sharpness_5">Manual_Sharpness2</div>
<div class="option" id="sharpness_6">Manual_Sharpness3</div>
<div class="option" id="sharpness_7">Manual_Sharpness4</div>
<div class="option" id="sharpness_8">Manual_Sharpness5</div>
</div>
<div class="feature" id="test_pattern"><h2>Test Pattern</h2>
<div class="option" id="test_pattern_7">Normal</div>
<div class="option" id="test_pattern_0">Color_bar</div>
<div class="option" id="test_pattern_1">Color_square</div>
<div class="option" id="test_pattern_2">BW_square</div>
<!--- Does not work.
https://github.com/ArduCAM/Arduino/issues/486
<div class="option" id="test_pattern_3">DLI</div> -->
</div>
<div class="control">
<div class="center">
<div class="option button" id="reset">Load Defaults</div>
<div class="option button" id="new">Load Latest</div>
<div class="option button" id="save">Save</div>
</div>
</div>)END";
/**
* The JavaScript user interface for the camera settings.
*
* @return JavaScript
*/
const std::string html_css_js_util::camera_settings_js = R"END(
<!-- BEGIN camera_settings.js --->
<script>
const cameraSettings = {
jpeg_size: 0, // OV5642_320x240
quality: 1, // default_quality
frames: 0,
light_mode: 0, // Advanced_AWB
color_saturation: 4, // Saturation0
brightness: 4, // Brightness0
contrast: 4, // Contrast0
hue: 6, // degree_0
effect: 7, // Normal
exposure: 5, // Exposure_default
sharpness: 0, // Auto_Sharpness_default
mirror: 7, // Normal
test_pattern: 7, // Normal
init: function(settings) {
this.jpeg_size = settings.jpegSize();
this.optionOnOff("jpeg_size", "jpeg_size_" + this.jpeg_size);
this.quality = settings.quality();
this.optionOnOff("quality", "quality_" + this.quality);
this.frames = parseInt(settings.frames());
this.optionOnOff("frames", "frames_" + this.frames);
this.light_mode = settings.lightMode();
this.optionOnOff("light_mode", "light_mode_" + this.light_mode);
this.color_saturation = settings.colorSaturation();
this.optionOnOff("color_saturation", "color_saturation_" + this.color_saturation);
this.brightness = settings.brightness();
this.optionOnOff("brightness", "brightness_" + this.brightness);
this.contrast = settings.contrast();
this.optionOnOff("contrast", "contrast_" + this.contrast);
this.hue = settings.hue();
this.optionOnOff("hue", "hue_" + this.hue);
this.effect = settings.effect();
this.optionOnOff("effect", "effect_" + this.effect);
this.exposure = settings.exposure();
this.optionOnOff("exposure", "exposure_" + this.exposure);
this.sharpness = settings.sharpness();
this.optionOnOff("sharpness", "sharpness_" + this.sharpness);
this.mirror = settings.mirror();
this.optionOnOff("mirror", "mirror_" + this.mirror);
this.test_pattern = settings.testPattern();
this.optionOnOff("test_pattern", "test_pattern_" + this.test_pattern);
},
newBuffer: new Uint8Array(52),
defaultBuffer: new Uint8Array([ 36, 0, 0, 0, 0, 0, 30, 0, 16, 0, 0, 0, 15, 0, 0, 0,
0, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 0, 0,
8, 0, 7, 0, 30, 0, 0, 0, 0, 0, 0, 7, 7, 5, 7, 6,
4, 4, 4, 1 ]),
optionOnOff: function(feature, option) {
const options = document.getElementById(feature).children;
for (const option of options) {
option.classList.remove("on");
option.classList.add("off");
}
document.getElementById(option).classList.add("on");
document.getElementById(option).classList.remove("off");
},
reset: function() {
var buf = new flatbuffers.ByteBuffer(this.defaultBuffer);
var defaultSettings = OV5642_Settings.Settings.getRootAsSettings(buf);
this.init(defaultSettings);
},
new: function() {
var ws = new WebSocket(wsUrl);
ws.onopen = function() {
ws.send("getNew");
};
ws.onmessage = function(evt) {
var eventUint8Array = null;
var eventArrayBuffer = null;
var fileReader = new FileReader();
fileReader.onload = function(event) {
eventArrayBuffer = event.target.result;
this.newBuffer = new Uint8Array(eventArrayBuffer);
buf = new flatbuffers.ByteBuffer(this.newBuffer);
var new_settings = OV5642_Settings.Settings.getRootAsSettings(buf);
cameraSettings.init(new_settings);
};
fileReader.readAsArrayBuffer(evt.data);
ws.close();
};
ws.onerror = function(event) {
console.error("WebSocket error observed:", event);
};
},
save: function() {
var builder = new flatbuffers.Builder(48);
var buffer = OV5642_Settings.Settings.createSettings(
builder,
this.jpeg_size,
this.quality,
parseInt(this.frames),
this.light_mode,
this.color_saturation,
this.brightness,
this.contrast,
this.hue,
this.effect,
this.exposure,
this.sharpness,
this.mirror,
this.test_pattern);
builder.finish(buffer);
this.newBuffer = builder.asUint8Array();
}
};
</script>
<!-- END camera_settings.js --->
)END";
/**
* The FlatBuffers JavaScript interface library.
*
* @return JavaScript
*/
const std::string html_css_js_util::flat_buffers_js = R"END(
<!-- BEGIN flatbuffers.js --->
<script>
/// @file
/// @addtogroup flatbuffers_javascript_api
/// @{
/// @cond FLATBUFFERS_INTERNAL
/**
* @fileoverview
*
* Need to suppress 'global this' error so the Node.js export line doesn't cause
* closure compile to error out.
* @suppress {globalThis}
*/
/**
* @const
* @namespace
*/
var flatbuffers = {};
/**
* @typedef {number}
*/
flatbuffers.Offset;
/**
* @typedef {{
* bb: flatbuffers.ByteBuffer,
* bb_pos: number
* }}
*/
flatbuffers.Table;
/**
* @type {number}
* @const
*/
flatbuffers.SIZEOF_SHORT = 2;
/**
* @type {number}
* @const
*/
flatbuffers.SIZEOF_INT = 4;
/**
* @type {number}
* @const
*/
flatbuffers.FILE_IDENTIFIER_LENGTH = 4;
/**
* @type {number}
* @const
*/
flatbuffers.SIZE_PREFIX_LENGTH = 4;
/**
* @param {number} low
* @param {number} high
* @returns {flatbuffers.Long}
*/
flatbuffers.createLong = function(low, high) {
return flatbuffers.Long.create(low, high);
};
/**
* @enum {number}
*/
flatbuffers.Encoding = {
UTF8_BYTES: 1,
UTF16_STRING: 2
};
/**
* @type {Int32Array}
* @const
*/
flatbuffers.int32 = new Int32Array(2);
/**
* @type {Float32Array}
* @const
*/
flatbuffers.float32 = new Float32Array(flatbuffers.int32.buffer);
/**
* @type {Float64Array}
* @const
*/
flatbuffers.float64 = new Float64Array(flatbuffers.int32.buffer);
/**
* @type {boolean}
* @const
*/
flatbuffers.isLittleEndian = new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
////////////////////////////////////////////////////////////////////////////////
/**
* @constructor
* @param {number} low
* @param {number} high
*/
flatbuffers.Long = function(low, high) {
/**
* @type {number}
* @const
*/
this.low = low | 0;
/**
* @type {number}
* @const
*/
this.high = high | 0;
};
/**
* @param {number} low
* @param {number} high
* @returns {!flatbuffers.Long}
*/
flatbuffers.Long.create = function(low, high) {
// Special-case zero to avoid GC overhead for default values
return low == 0 && high == 0 ? flatbuffers.Long.ZERO : new flatbuffers.Long(low, high);
};
/**
* @returns {number}
*/
flatbuffers.Long.prototype.toFloat64 = function() {
return (this.low >>> 0) + this.high * 0x100000000;
};
/**
* @param {flatbuffers.Long} other
* @returns {boolean}
*/
flatbuffers.Long.prototype.equals = function(other) {
return this.low == other.low && this.high == other.high;
};
/**
* @type {!flatbuffers.Long}
* @const
*/
flatbuffers.Long.ZERO = new flatbuffers.Long(0, 0);
/// @endcond
////////////////////////////////////////////////////////////////////////////////
/**
* Create a FlatBufferBuilder.
*
* @constructor
* @param {number=} opt_initial_size
*/
flatbuffers.Builder = function(opt_initial_size) {
if (!opt_initial_size) {
var initial_size = 1024;
} else {
var initial_size = opt_initial_size;
}
/**
* @type {flatbuffers.ByteBuffer}
* @private
*/
this.bb = flatbuffers.ByteBuffer.allocate(initial_size);
/**
* Remaining space in the ByteBuffer.
*
* @type {number}
* @private
*/
this.space = initial_size;
/**
* Minimum alignment encountered so far.
*
* @type {number}
* @private
*/
this.minalign = 1;
/**
* The vtable for the current table.
*
* @type {Array.<number>}
* @private
*/
this.vtable = null;
/**
* The amount of fields we're actually using.
*
* @type {number}
* @private
*/
this.vtable_in_use = 0;
/**
* Whether we are currently serializing a table.
*
* @type {boolean}
* @private
*/
this.isNested = false;
/**
* Starting offset of the current struct/table.
*
* @type {number}
* @private
*/
this.object_start = 0;
/**
* List of offsets of all vtables.
*
* @type {Array.<number>}
* @private
*/
this.vtables = [];
/**
* For the current vector being built.
*
* @type {number}
* @private
*/
this.vector_num_elems = 0;
/**
* False omits default values from the serialized data
*
* @type {boolean}
* @private
*/
this.force_defaults = false;
};
flatbuffers.Builder.prototype.clear = function() {
this.bb.clear();
this.space = this.bb.capacity();
this.minalign = 1;
this.vtable = null;
this.vtable_in_use = 0;
this.isNested = false;
this.object_start = 0;
this.vtables = [];
this.vector_num_elems = 0;
this.force_defaults = false;
};
/**
* In order to save space, fields that are set to their default value
* don't get serialized into the buffer. Forcing defaults provides a
* way to manually disable this optimization.
*
* @param {boolean} forceDefaults true always serializes default values
*/
flatbuffers.Builder.prototype.forceDefaults = function(forceDefaults) {
this.force_defaults = forceDefaults;
};
/**
* Get the ByteBuffer representing the FlatBuffer. Only call this after you've
* called finish(). The actual data starts at the ByteBuffer's current position,
* not necessarily at 0.
*
* @returns {flatbuffers.ByteBuffer}
*/
flatbuffers.Builder.prototype.dataBuffer = function() {
return this.bb;
};
/**
* Get the bytes representing the FlatBuffer. Only call this after you've
* called finish().
*
* @returns {!Uint8Array}
*/
flatbuffers.Builder.prototype.asUint8Array = function() {
return this.bb.bytes().subarray(this.bb.position(), this.bb.position() + this.offset());
};
/// @cond FLATBUFFERS_INTERNAL
/**
* Prepare to write an element of `size` after `additional_bytes` have been
* written, e.g. if you write a string, you need to align such the int length
* field is aligned to 4 bytes, and the string data follows it directly. If all
* you need to do is alignment, `additional_bytes` will be 0.
*
* @param {number} size This is the of the new element to write
* @param {number} additional_bytes The padding size
*/
flatbuffers.Builder.prototype.prep = function(size, additional_bytes) {
// Track the biggest thing we've ever aligned to.
if (size > this.minalign) {
this.minalign = size;
}
// Find the amount of alignment needed such that `size` is properly
// aligned after `additional_bytes`
var align_size = ((~(this.bb.capacity() - this.space + additional_bytes)) + 1) & (size - 1);
// Reallocate the buffer if needed.
while (this.space < align_size + size + additional_bytes) {
var old_buf_size = this.bb.capacity();
this.bb = flatbuffers.Builder.growByteBuffer(this.bb);
this.space += this.bb.capacity() - old_buf_size;
}
this.pad(align_size);
};
/**
* @param {number} byte_size
*/
flatbuffers.Builder.prototype.pad = function(byte_size) {
for (var i = 0; i < byte_size; i++) {
this.bb.writeInt8(--this.space, 0);
}
};
/**
* @param {number} value
*/
flatbuffers.Builder.prototype.writeInt8 = function(value) {
this.bb.writeInt8(this.space -= 1, value);
};
/**
* @param {number} value
*/
flatbuffers.Builder.prototype.writeInt16 = function(value) {
this.bb.writeInt16(this.space -= 2, value);
};
/**
* @param {number} value
*/
flatbuffers.Builder.prototype.writeInt32 = function(value) {
this.bb.writeInt32(this.space -= 4, value);
};
/**
* @param {flatbuffers.Long} value
*/
flatbuffers.Builder.prototype.writeInt64 = function(value) {
this.bb.writeInt64(this.space -= 8, value);
};
/**
* @param {number} value
*/
flatbuffers.Builder.prototype.writeFloat32 = function(value) {
this.bb.writeFloat32(this.space -= 4, value);
};
/**
* @param {number} value
*/
flatbuffers.Builder.prototype.writeFloat64 = function(value) {
this.bb.writeFloat64(this.space -= 8, value);
};
/// @endcond
/**
* Add an `int8` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {number} value The `int8` to add the the buffer.
*/
flatbuffers.Builder.prototype.addInt8 = function(value) {
this.prep(1, 0);
this.writeInt8(value);
};
/**
* Add an `int16` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {number} value The `int16` to add the the buffer.
*/
flatbuffers.Builder.prototype.addInt16 = function(value) {
this.prep(2, 0);
this.writeInt16(value);
};
/**
* Add an `int32` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {number} value The `int32` to add the the buffer.
*/
flatbuffers.Builder.prototype.addInt32 = function(value) {
this.prep(4, 0);
this.writeInt32(value);
};
/**
* Add an `int64` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {flatbuffers.Long} value The `int64` to add the the buffer.
*/
flatbuffers.Builder.prototype.addInt64 = function(value) {
this.prep(8, 0);
this.writeInt64(value);
};
/**
* Add a `float32` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {number} value The `float32` to add the the buffer.
*/
flatbuffers.Builder.prototype.addFloat32 = function(value) {
this.prep(4, 0);
this.writeFloat32(value);
};
/**
* Add a `float64` to the buffer, properly aligned, and grows the buffer (if necessary).
* @param {number} value The `float64` to add the the buffer.
*/
flatbuffers.Builder.prototype.addFloat64 = function(value) {
this.prep(8, 0);
this.writeFloat64(value);
};
/// @cond FLATBUFFERS_INTERNAL
/**
* @param {number} voffset
* @param {number} value
* @param {number} defaultValue
*/
flatbuffers.Builder.prototype.addFieldInt8 = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addInt8(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {number} value
* @param {number} defaultValue
*/
flatbuffers.Builder.prototype.addFieldInt16 = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addInt16(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {number} value
* @param {number} defaultValue
*/
flatbuffers.Builder.prototype.addFieldInt32 = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addInt32(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {flatbuffers.Long} value
* @param {flatbuffers.Long} defaultValue
*/
flatbuffers.Builder.prototype.addFieldInt64 = function(voffset, value, defaultValue) {
if (this.force_defaults || !value.equals(defaultValue)) {
this.addInt64(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {number} value
* @param {number} defaultValue
*/
flatbuffers.Builder.prototype.addFieldFloat32 = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addFloat32(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {number} value
* @param {number} defaultValue
*/
flatbuffers.Builder.prototype.addFieldFloat64 = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addFloat64(value);
this.slot(voffset);
}
};
/**
* @param {number} voffset
* @param {flatbuffers.Offset} value
* @param {flatbuffers.Offset} defaultValue
*/
flatbuffers.Builder.prototype.addFieldOffset = function(voffset, value, defaultValue) {
if (this.force_defaults || value != defaultValue) {
this.addOffset(value);
this.slot(voffset);
}
};
/**
* Structs are stored inline, so nothing additional is being added. `d` is always 0.
*
* @param {number} voffset
* @param {flatbuffers.Offset} value
* @param {flatbuffers.Offset} defaultValue
*/
flatbuffers.Builder.prototype.addFieldStruct = function(voffset, value, defaultValue) {
if (value != defaultValue) {
this.nested(value);
this.slot(voffset);
}
};
/**
* Structures are always stored inline, they need to be created right
* where they're used. You'll get this assertion failure if you
* created it elsewhere.
*
* @param {flatbuffers.Offset} obj The offset of the created object
*/
flatbuffers.Builder.prototype.nested = function(obj) {
if (obj != this.offset()) {
throw new Error('FlatBuffers: struct must be serialized inline.');
}
};
/**
* Should not be creating any other object, string or vector
* while an object is being constructed
*/
flatbuffers.Builder.prototype.notNested = function() {
if (this.isNested) {
throw new Error('FlatBuffers: object serialization must not be nested.');
}
};
/**
* Set the current vtable at `voffset` to the current location in the buffer.
*
* @param {number} voffset
*/
flatbuffers.Builder.prototype.slot = function(voffset) {
this.vtable[voffset] = this.offset();
};
/**
* @returns {flatbuffers.Offset} Offset relative to the end of the buffer.
*/
flatbuffers.Builder.prototype.offset = function() {
return this.bb.capacity() - this.space;
};
/**
* Doubles the size of the backing ByteBuffer and copies the old data towards
* the end of the new buffer (since we build the buffer backwards).
*
* @param {flatbuffers.ByteBuffer} bb The current buffer with the existing data
* @returns {!flatbuffers.ByteBuffer} A new byte buffer with the old data copied
* to it. The data is located at the end of the buffer.
*
* uint8Array.set() formally takes {Array<number>|ArrayBufferView}, so to pass
* it a uint8Array we need to suppress the type check:
* @suppress {checkTypes}
*/
flatbuffers.Builder.growByteBuffer = function(bb) {
var old_buf_size = bb.capacity();
// Ensure we don't grow beyond what fits in an int.
if (old_buf_size & 0xC0000000) {
throw new Error('FlatBuffers: cannot grow buffer beyond 2 gigabytes.');
}
var new_buf_size = old_buf_size << 1;
var nbb = flatbuffers.ByteBuffer.allocate(new_buf_size);
nbb.setPosition(new_buf_size - old_buf_size);
nbb.bytes().set(bb.bytes(), new_buf_size - old_buf_size);
return nbb;
};
/// @endcond
/**
* Adds on offset, relative to where it will be written.
*
* @param {flatbuffers.Offset} offset The offset to add.
*/
flatbuffers.Builder.prototype.addOffset = function(offset) {
this.prep(flatbuffers.SIZEOF_INT, 0); // Ensure alignment is already done.
this.writeInt32(this.offset() - offset + flatbuffers.SIZEOF_INT);
};
/// @cond FLATBUFFERS_INTERNAL
/**
* Start encoding a new object in the buffer. Users will not usually need to
* call this directly. The FlatBuffers compiler will generate helper methods
* that call this method internally.
*
* @param {number} numfields
*/
flatbuffers.Builder.prototype.startObject = function(numfields) {
this.notNested();
if (this.vtable == null) {
this.vtable = [];
}
this.vtable_in_use = numfields;
for (var i = 0; i < numfields; i++) {
this.vtable[i] = 0; // This will push additional elements as needed
}
this.isNested = true;
this.object_start = this.offset();
};
/**
* Finish off writing the object that is under construction.
*
* @returns {flatbuffers.Offset} The offset to the object inside `dataBuffer`
*/
flatbuffers.Builder.prototype.endObject = function() {
if (this.vtable == null || !this.isNested) {
throw new Error('FlatBuffers: endObject called without startObject');
}
this.addInt32(0);
var vtableloc = this.offset();
// Trim trailing zeroes.
var i = this.vtable_in_use - 1;
for (; i >= 0 && this.vtable[i] == 0; i--) {}
var trimmed_size = i + 1;
// Write out the current vtable.
for (; i >= 0; i--) {
// Offset relative to the start of the table.
this.addInt16(this.vtable[i] != 0 ? vtableloc - this.vtable[i] : 0);
}
var standard_fields = 2; // The fields below:
this.addInt16(vtableloc - this.object_start);
var len = (trimmed_size + standard_fields) * flatbuffers.SIZEOF_SHORT;
this.addInt16(len);
// Search for an existing vtable that matches the current one.
var existing_vtable = 0;
var vt1 = this.space;
outer_loop:
for (i = 0; i < this.vtables.length; i++) {
var vt2 = this.bb.capacity() - this.vtables[i];
if (len == this.bb.readInt16(vt2)) {
for (var j = flatbuffers.SIZEOF_SHORT; j < len; j += flatbuffers.SIZEOF_SHORT) {
if (this.bb.readInt16(vt1 + j) != this.bb.readInt16(vt2 + j)) {
continue outer_loop;
}
}
existing_vtable = this.vtables[i];
break;
}
}
if (existing_vtable) {
// Found a match:
// Remove the current vtable.
this.space = this.bb.capacity() - vtableloc;
// Point table to existing vtable.
this.bb.writeInt32(this.space, existing_vtable - vtableloc);
} else {
// No match:
// Add the location of the current vtable to the list of vtables.
this.vtables.push(this.offset());
// Point table to current vtable.
this.bb.writeInt32(this.bb.capacity() - vtableloc, this.offset() - vtableloc);
}
this.isNested = false;
return vtableloc;
};
/// @endcond
/**
* Finalize a buffer, poiting to the given `root_table`.
*
* @param {flatbuffers.Offset} root_table
* @param {string=} opt_file_identifier
* @param {boolean=} opt_size_prefix
*/
flatbuffers.Builder.prototype.finish = function(root_table, opt_file_identifier, opt_size_prefix) {
var size_prefix = opt_size_prefix ? flatbuffers.SIZE_PREFIX_LENGTH : 0;
if (opt_file_identifier) {
var file_identifier = opt_file_identifier;