|
1 | | -function output = write_image(pfObj, imageFormat, filename, height, width, scale) |
| 1 | +function output = write_image(pfObj, options) |
2 | 2 | % Function to write plotly figures to a supported image format, which |
3 | 3 | % are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", |
4 | 4 | % "json". |
| 5 | + arguments |
| 6 | + pfObj |
| 7 | + options.imageFormat string = "png" |
| 8 | + options.filename string = "" |
| 9 | + options.height double = pfObj.layout.height |
| 10 | + options.width double = pfObj.layout.width |
| 11 | + options.scale double = 1 |
| 12 | + end |
5 | 13 |
|
6 | | - debug=0; |
7 | | - if nargin < 2 |
8 | | - imageFormat = "png"; |
9 | | - filename = "figure.png"; |
10 | | - height = pfObj.layout.height; |
11 | | - width = pfObj.layout.width; |
12 | | - scale = 1; |
13 | | - elseif nargin < 3 |
14 | | - filename = "figure." + imageFormat; |
15 | | - height = pfObj.layout.height; |
16 | | - width = pfObj.layout.width; |
17 | | - scale = 1; |
18 | | - elseif nargin < 4 |
19 | | - height = pfObj.layout.height; |
20 | | - width = pfObj.layout.width; |
21 | | - scale = 1; |
22 | | - elseif nargin < 5 |
23 | | - width = pfObj.layout.width; |
24 | | - scale = 1; |
25 | | - elseif nargin < 6 |
26 | | - scale = 1; |
| 14 | + % Set default filename based on imageFormat if not provided |
| 15 | + if options.filename == "" |
| 16 | + options.filename = "figure." + options.imageFormat; |
27 | 17 | end |
28 | 18 |
|
29 | | - if strcmpi(imageFormat, "jpg") |
| 19 | + imageFormat = options.imageFormat; |
| 20 | + filename = options.filename; |
| 21 | + height = options.height; |
| 22 | + width = options.width; |
| 23 | + scale = options.scale; |
| 24 | + |
| 25 | + if imageFormat == "jpg" |
30 | 26 | imageFormat = "jpeg"; |
31 | 27 | end |
32 | 28 |
|
|
49 | 45 |
|
50 | 46 | if ~isfile(kExec) || ~isfile(plyJsLoc) |
51 | 47 | status = getKaleido(); |
52 | | - else |
53 | | - status = 1; |
54 | | - end |
55 | | - |
56 | | - if status == 0 |
57 | | - return |
| 48 | + if status == 0 |
| 49 | + return |
| 50 | + end |
58 | 51 | end |
59 | 52 |
|
60 | 53 | mjLoc = replace(string(fullfile( ... |
61 | 54 | wd, "kaleido", "etc", "mathjax", "MathJax.js")), "\", "/"); |
62 | | - scope="plotly"; |
| 55 | + scope = "plotly"; |
63 | 56 |
|
64 | 57 | % Prepare input plotly object for Kaleido |
65 | | - q = struct(); |
66 | | - q.data.data = pfObj.data; |
67 | | - q.data.layout = pfObj.layout; |
68 | | - q.data.layout = rmfield(q.data.layout, "height"); |
69 | | - q.data.layout = rmfield(q.data.layout, "width"); |
70 | | - q.format = string(imageFormat); |
71 | | - q.height = height; |
72 | | - q.scale = scale; |
73 | | - q.width = width; |
| 58 | + q = struct( ... |
| 59 | + "data", struct( ... |
| 60 | + "data", pfObj.data, ... |
| 61 | + "layout", rmfield(pfObj.layout, ["height" "width"]) ... |
| 62 | + ), ... |
| 63 | + "format", string(imageFormat), ... |
| 64 | + "height", height, ... |
| 65 | + "scale", scale, ... |
| 66 | + "width", width ... |
| 67 | + ); |
74 | 68 |
|
75 | 69 | pfJson = native2unicode(jsonencode(q), "UTF-8"); |
76 | 70 | tFile = string(fullfile(wd, "kaleido", "temp.txt")); |
|
84 | 78 | + "--allow-file-access-from-files --disable-breakpad " ... |
85 | 79 | + "--disable-dev-shm-usage"]; |
86 | 80 |
|
87 | | - if debug |
88 | | - inputCmd = char(join(cmd, "")); |
89 | | - fprintf("\nDebug info:\n%s\n\n", inputCmd); |
90 | | - end |
91 | | - |
92 | 81 | [code,out] = system(char(join(cmd, ""))); |
93 | | - if debug |
94 | | - disp(out); |
95 | | - end |
96 | 82 |
|
97 | 83 | if code ~= 0 |
98 | 84 | fprintf("\nFatal: Failed to run Kaleido.\n\n"); |
99 | | - return; |
100 | | - else |
101 | | - a = string(split(out,newline)); |
102 | | - if a(end) == "" |
103 | | - a(end) = []; |
104 | | - end |
105 | | - output = jsondecode(a(end)); |
| 85 | + return |
| 86 | + end |
| 87 | + |
| 88 | + a = string(split(out,newline)); |
| 89 | + if a(end) == "" |
| 90 | + a(end) = []; |
106 | 91 | end |
| 92 | + output = jsondecode(a(end)); |
107 | 93 |
|
108 | 94 | if output.code ~= 0 |
109 | 95 | fprintf("\nError: %s\n", output.message); |
110 | | - else |
111 | | - out = unicode2native(output.result, "UTF-8"); |
112 | | - out = matlab.net.base64decode(out); |
113 | | - f = fopen(char(filename), "wb"); |
114 | | - fwrite(f, out); |
115 | | - fclose(f); |
| 96 | + return |
116 | 97 | end |
| 98 | + |
| 99 | + out = unicode2native(output.result, "UTF-8"); |
| 100 | + out = matlab.net.base64decode(out); |
| 101 | + f = fopen(char(filename), "wb"); |
| 102 | + fwrite(f, out); |
| 103 | + fclose(f); |
117 | 104 | end |
0 commit comments