colo-icc.lua /size: 4307 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
colo-icc
'
]
=
{
2
version
=
1
.
000
,
3
comment
=
"
companion to colo-ini.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
char
,
byte
,
gsub
,
match
,
format
,
strip
=
string
.
char
,
string
.
byte
,
string
.
gsub
,
string
.
match
,
string
.
format
,
string
.
strip
10
local
readstring
,
readnumber
=
io
.
readstring
,
io
.
readnumber
11
local
band
=
bit32
.
band
12
local
next
=
next
13 14
local
colors
=
attributes
and
attributes
.
colors
or
{
}
-- when used in mtxrun
15 16
local
report_colors
=
logs
.
reporter
(
"
colors
"
,
"
icc
"
)
17 18
local
R
,
Cs
,
lpegmatch
=
lpeg
.
R
,
lpeg
.
Cs
,
lpeg
.
match
19 20
local
invalid
=
R
(
char
(
0
)
.
.
char
(
31
)
)
21
local
cleaned
=
invalid
^
0
*
Cs
(
(
1
-
invalid
)
^
0
)
22 23
function
colors
.
iccprofile
(
filename
,
verbose
)
24
local
fullname
=
resolvers
.
findfile
(
filename
,
"
icc
"
)
or
"
"
25
if
fullname
=
=
"
"
then
26
local
locate
=
resolvers
.
finders
.
byscheme
-- not in mtxrun
27
if
locate
then
28
fullname
=
locate
(
"
loc
"
,
filename
)
29
end
30
end
31
if
fullname
=
=
"
"
then
32
report_colors
(
"
profile %a cannot be found
"
,
filename
)
33
return
nil
,
false
34
end
35
local
f
=
io
.
open
(
fullname
,
"
rb
"
)
36
if
not
f
then
37
report_colors
(
"
profile %a cannot be loaded
"
,
fullname
)
38
return
nil
,
false
39
end
40
local
header
=
{
41
size
=
readnumber
(
f
,
4
)
,
42
cmmtype
=
readnumber
(
f
,
4
)
,
43
version
=
readnumber
(
f
,
4
)
,
44
deviceclass
=
strip
(
readstring
(
f
,
4
)
)
,
45
colorspace
=
strip
(
readstring
(
f
,
4
)
)
,
46
connectionspace
=
strip
(
readstring
(
f
,
4
)
)
,
47
datetime
=
{
48
year
=
readnumber
(
f
,
2
)
,
49
month
=
readnumber
(
f
,
2
)
,
50
day
=
readnumber
(
f
,
2
)
,
51
hour
=
readnumber
(
f
,
2
)
,
52
minutes
=
readnumber
(
f
,
2
)
,
53
seconds
=
readnumber
(
f
,
2
)
,
54
}
,
55
filesignature
=
strip
(
readstring
(
f
,
4
)
)
,
56
platformsignature
=
strip
(
readstring
(
f
,
4
)
)
,
57
options
=
readnumber
(
f
,
4
)
,
58
devicemanufacturer
=
strip
(
readstring
(
f
,
4
)
)
,
59
devicemodel
=
strip
(
readstring
(
f
,
4
)
)
,
60
deviceattributes
=
readnumber
(
f
,
4
)
,
61
renderingintent
=
readnumber
(
f
,
4
)
,
62
illuminantxyz
=
{
63
x
=
readnumber
(
f
,
4
)
,
64
y
=
readnumber
(
f
,
4
)
,
65
z
=
readnumber
(
f
,
4
)
,
66
}
,
67
profilecreator
=
readnumber
(
f
,
4
)
,
68
id
=
strip
(
readstring
(
f
,
16
)
)
,
69
}
70
local
tags
=
{
}
71
for
i
=
1
,
readnumber
(
f
,
128
,
4
)
do
72
tags
[
readstring
(
f
,
4
)
]
=
{
73
offset
=
readnumber
(
f
,
4
)
,
74
length
=
readnumber
(
f
,
4
)
,
75
}
76
end
77
local
o
=
header
.
options
78
header
.
options
=
79
o
=
=
0
and
"
embedded
"
or
80
o
=
=
1
and
"
dependent
"
or
"
unknown
"
81
local
d
=
header
.
deviceattributes
82
header
.
deviceattributes
=
{
83
[
band
(
d
,
1
)
~
=
0
and
"
transparency
"
or
"
reflective
"
]
=
true
,
84
[
band
(
d
,
2
)
~
=
0
and
"
mate
"
or
"
glossy
"
]
=
true
,
85
[
band
(
d
,
3
)
~
=
0
and
"
negative
"
or
"
positive
"
]
=
true
,
86
[
band
(
d
,
4
)
~
=
0
and
"
bw
"
or
"
color
"
]
=
true
,
87
}
88
local
r
=
header
.
renderingintent
89
header
.
renderingintent
=
90
r
=
=
0
and
"
perceptual
"
or
91
r
=
=
1
and
"
relative
"
or
92
r
=
=
2
and
"
saturation
"
or
93
r
=
=
3
and
"
absolute
"
or
"
unknown
"
94
for
tag
,
spec
in
next
,
tags
do
95
if
tag
then
96
local
offset
,
length
=
spec
.
offset
,
spec
.
length
97
local
variant
=
readstring
(
f
,
offset
,
4
)
98
if
variant
=
=
"
text
"
or
variant
=
=
"
desc
"
then
99
local
str
=
readstring
(
f
,
length
-4
)
100
tags
[
tag
]
=
{
101
data
=
str
,
102
cleaned
=
lpegmatch
(
cleaned
,
str
)
,
103
}
104
else
105
if
verbose
then
106
report_colors
(
"
ignoring tag %a or type %a in profile %a
"
,
tag
,
variant
,
fullname
)
107
end
108
tags
[
tag
]
=
nil
109
end
110
end
111
end
112
f
:
close
(
)
113
local
profile
=
{
114
filename
=
filename
,
115
fullname
=
fullname
,
116
header
=
header
,
117
tags
=
tags
,
118
}
119
report_colors
(
"
profile %a loaded
"
,
fullname
)
120
return
profile
,
true
121
end
122