-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathwrite_image.m
More file actions
112 lines (96 loc) · 3.16 KB
/
Copy pathwrite_image.m
File metadata and controls
112 lines (96 loc) · 3.16 KB
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
function output = write_image(pfObj, varargin)
% Function to write plotly figures to a supported image format, which
% are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps",
% "json".
options.imageFormat = "png";
options.filename = "";
options.height = pfObj.layout.height;
options.width = pfObj.layout.width;
options.scale = 1;
nargs = numel(varargin);
if mod(nargs, 2) ~= 0
error("write_image:options", "Arguments must be provided as Name, Value pairs.");
end
for k = 1:2:nargs
options.(varargin{k}) = varargin{k+1};
end
% Set default filename based on imageFormat if not provided
if strcmp(options.filename, "")
options.filename = sprintf("figure.%s", options.imageFormat);
end
imageFormat = options.imageFormat;
filename = options.filename;
height = options.height;
width = options.width;
scale = options.scale;
if strcmp(imageFormat, "jpg")
imageFormat = "jpeg";
end
wd = fileparts(fileparts(mfilename("fullpath")));
output = [];
if ~isa(pfObj, "plotlyfig")
fprintf("\nError: Input is not a plotlyfig object.\n\n");
return
end
if isunix()
kExec = fullfile(wd,"kaleido", "kaleido");
cc = "cat";
else
kExec = fullfile(wd,"kaleido", "kaleido.cmd");
cc = "type";
end
plyJsLoc = fullfile(wd,"kaleido", "plotly-latest.min.js");
if ~isfile(kExec) || ~isfile(plyJsLoc)
status = getKaleido();
if status == 0
return
end
end
mjLoc = strrep(fullfile( ...
wd, "kaleido", "etc", "mathjax", "MathJax.js"), '\', '/');
scope = "plotly";
% Prepare input plotly object for Kaleido
q = struct( ...
"data", struct( ...
"data", {pfObj.data}, ...
"layout", rmfield(pfObj.layout, {"height" "width"}) ...
), ...
"format", imageFormat, ...
"height", height, ...
"scale", scale, ...
"width", width ...
);
pfJson = native2unicode(jsonencode(q), "UTF-8");
tFile = fullfile(wd, "kaleido", "temp.txt");
f = fopen(tFile, "w");
fprintf(f, "%s", pfJson);
fclose(f);
cmd = sprintf(['%s %s | %s %s --plotlyjs=''%s'' --mathjax=''file:///%s'' ' ...
'--no-sandbox --disable-gpu ' ...
'--allow-file-access-from-files --disable-breakpad ' ...
'--disable-dev-shm-usage'], cc, tFile, kExec, scope, plyJsLoc, mjLoc);
[code,out] = system(cmd);
if code ~= 0
fprintf("\nFatal: Failed to run Kaleido.\n\n");
return
end
a = strsplit(out, newline);
if strcmp(a{end}, "")
a(end) = [];
end
output = jsondecode(a{end});
if output.code ~= 0
fprintf("\nError: %s\n", output.message);
return
end
if strcmp(imageFormat, "json") || strcmp(imageFormat, "svg")
out = unicode2native(output.result, "UTF-8");
elseif is_octave()
out = __base64_decode_bytes__(output.result);
else
out = matlab.net.base64decode(unicode2native(output.result, "UTF-8"));
end
f = fopen(char(filename), "wb");
fwrite(f, out);
fclose(f);
end