-
Notifications
You must be signed in to change notification settings - Fork 0
/
fxifUtils.js
129 lines (107 loc) · 3.37 KB
/
fxifUtils.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, v. 2.0.
*/
"use strict";
/*
* Some utility functions for FxIF.
*/
function fxifUtilsClass ()
{
var prefInstance = null;
this.exifDone = false;
this.iptcDone = false;
this.xmpDone = false;
this.read16 = function (data, offset, swapbytes)
{
if(!swapbytes)
return (data[offset] << 8) | data[offset+1];
return data[offset] | (data[offset+1] << 8);
}
this.read32 = function (data, offset, swapbytes)
{
if (!swapbytes)
return (data[offset] << 24) | (data[offset+1] << 16) | (data[offset+2] << 8) | data[offset+3];
return data[offset] | (data[offset+1] << 8) | (data[offset+2] << 16) | (data[offset+3] << 24);
}
/* charWidth should normally be 1 and this function thus reads
* the bytes one by one. But reading Unicode needs reading
* 16 Bit values.
* Stops at the first null byte.
*/
this.bytesToString = function (data, offset, num, swapbytes, charWidth)
{
var s = "";
if (charWidth == 1)
{
for (var i = offset; i < offset + num; i++)
{
var charval = data[i];
if (charval == 0)
break;
s += String.fromCharCode(charval);
}
}
else
{
for (var i = offset; i < offset + num; i += 2)
{
var charval = this.read16(data, i, swapbytes);
if (charval == 0)
break;
s += String.fromCharCode(charval);
}
}
return s;
}
/* Doesn’t stop at null bytes. */
this.bytesToStringWithNull = function (data, offset, num)
{
var s = "";
for (var i = offset; i < offset + num; i++)
s += String.fromCharCode(data[i]);
return s;
}
this.dd2dms = function (gpsval)
{
// a bit unconventional calculation to get input edge cases
// like 0x31 / 0x01, 0x0a / 0x01, 0x3c / 0x01 to 49°11'0" instead of 49°10'60"
var gpsDeg = Math.floor(gpsval / 3600);
gpsval -= gpsDeg * 3600.0;
var gpsMin = Math.floor(gpsval / 60);
// round to 2 digits after the comma
var gpsSec = (gpsval - gpsMin * 60.0).toFixed(2);
return new Array(gpsDeg, gpsMin, gpsSec);
}
this.dd2dm = function (gpsval)
{
// a bit unconventional calculation to get input edge cases
// like 0x31 / 0x01, 0x0a / 0x01, 0x3c / 0x01 to 49°11'0" instead of 49°10'60"
var gpsDeg = Math.floor(gpsval / 3600);
gpsval -= gpsDeg * 3600.0;
// round to 2 digits after the comma
var gpsMin = (gpsval / 60).toFixed(4);
return new Array(gpsDeg, gpsMin);
}
this.dd2dd = function (gpsval)
{
// round to 6 digits after the comma
var gpsArr = new Array();
gpsArr.push((gpsval / 3600).toFixed(6));
return gpsArr;
}
this.getPreferences = function ()
{
// console.log("getPreferences");
}
// Retrieves the language which is likely to be the users favourite one.
// Currently we end up using only the first language code.
this.getLang = function ()
{
return browser.i18n.getUILanguage();
}
}
var fxifUtils = new fxifUtilsClass();