-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacov2.R
110 lines (71 loc) · 2.9 KB
/
acov2.R
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
decode_aco_v2 <- function(aco, n_colors) {
con <- rawConnection(aco)
on.exit(close(con))
tmp <- readBin(con, "raw", 4, endian="big")
val <- sapply(1:n_colors, function(i) {
parse_aco_v2_block(con)
})
val[!is.na(val)]
}
parse_aco_v2_block <- function(con) {
colorspace <- b2i(readBin(con, "raw", 2, endian="big"))
color <- NA
if (colorspace == 0) { # RGB
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
color <- rgb(w, x, y, maxColorValue=65535)
} else if (colorspace == 1) { # HSB
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
color <- hsv((w/182.04)/360, ((x/655.35)/360), ((y/655.35)/360))
} else if (colorspace == 2) { # CMYK
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
color <- cmyk((100-w/655.35), (100-x/655.35), (100-y/655.35), (100-z/655.35))
} else if (colorspace == 7) { # Lab
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- readBin(con, "integer", 1, 2, signed=TRUE, endian="big")
y <- readBin(con, "integer", 1, 2, signed=TRUE, endian="big")
z <- b2i(readBin(con, "raw", 2, endian="big"))
# color <- LAB(w/100, x/100, y/100)
color <- NA
message("Lab colors not supported yet")
} else if (colorspace == 8) { # Grayscale
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
message("Grayscale not supported yet")
color <- w/39.0625
} else if (colorspace == 9) { # Wide CMYK
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
color <- cmyk(w/100, x/100, y/100, z/100)
} else {
w <- b2i(readBin(con, "raw", 2, endian="big"))
x <- b2i(readBin(con, "raw", 2, endian="big"))
y <- b2i(readBin(con, "raw", 2, endian="big"))
z <- b2i(readBin(con, "raw", 2, endian="big"))
message("Color format unknown")
color <- NA
}
tmp <- readBin(con, "raw", 2, endian="big")
label <- ""
label_length <- b2i(readBin(con, "raw", 2, endian="big")) * 2
if (label_length > 0) {
label <- readBin(con, "raw", label_length-2, endian="big")
label <- rawToChar(label[1:length(label)], multiple=TRUE)
label <- paste(label, sep="", collapse="")
}
names(color) <- label
tmp <- readBin(con, "raw", 2, endian="big")
return(color)
}