@@ -17,14 +17,40 @@ REBOL [
17
17
}
18
18
]
19
19
20
+ register-codec : function [
21
+ {Registers non-native codec to system/codecs and it's suffixes into system/options/file-types}
22
+ codec [object! ] "Codec to register (should be based on system/standard/codec template)"
23
+ /local name suffixes
24
+ ] [
25
+ if not word? name: try [codec/name ][
26
+ cause-error 'Script 'wrong-type 'codec/name
27
+ ]
28
+
29
+ append system/codecs reduce [to set-word! name codec]
30
+
31
+ if block? suffixes: try [codec/suffixes ][
32
+ append append system/options/file-types suffixes name
33
+ ]
34
+ codec
35
+ ]
36
+
20
37
decode : function [
21
38
{Decodes a series of bytes into the related datatype (e.g. image!).}
22
39
type [word! ] {Media type (jpeg, png, etc.)}
23
- data [binary! ] {The data to decode}
40
+ data [binary! string! ] {The data to decode}
24
41
] [
25
42
unless all [
26
43
cod: select system/codecs type
27
- data: do-codec cod/entry 'decode data
44
+ data: either handle? try [cod/entry ] [
45
+ ; original codecs were only natives
46
+ do-codec cod/entry 'decode data
47
+ ][
48
+ either function? try [: cod/decode ][
49
+ cod/decode data
50
+ ][
51
+ cause-error 'internal 'not-done type
52
+ ]
53
+ ]
28
54
][
29
55
cause-error 'access 'no-codec type
30
56
]
@@ -39,7 +65,16 @@ encode: function [
39
65
] [
40
66
unless all [
41
67
cod: select system/codecs type
42
- data: do-codec cod/entry 'encode data
68
+ data: either handle? try [cod/entry ] [
69
+ ; original codecs were only natives
70
+ do-codec cod/entry 'encode data
71
+ ][
72
+ either function? try [: cod/encode ][
73
+ cod/encode data
74
+ ][
75
+ cause-error 'internal 'not-done type
76
+ ]
77
+ ]
43
78
][
44
79
cause-error 'access 'no-codec type
45
80
]
@@ -51,11 +86,21 @@ encoding?: function [
51
86
data [binary! ]
52
87
] [
53
88
foreach [name codec] system/codecs [
54
- if do-codec codec/entry 'identify data [
55
- return name
89
+ either handle? try [cod/entry ] [
90
+ if do-codec codec/entry 'identify data [
91
+ return name
92
+ ]
93
+ ][
94
+ if all [
95
+ function? try [: cod/identify ]
96
+ cod/identify data
97
+ ][
98
+ return name
99
+ ]
56
100
]
101
+
57
102
]
58
103
none
59
104
]
60
105
61
- export [decode encode encoding?]
106
+ export [register-codec decode encode encoding?]
0 commit comments