Skip to content

Commit 96e8fd9

Browse files
Refactor write_image
1 parent 69eb4e9 commit 96e8fd9

File tree

1 file changed

+47
-60
lines changed

1 file changed

+47
-60
lines changed

plotly/export_fig2/write_image.m

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
function output = write_image(pfObj, imageFormat, filename, height, width, scale)
1+
function output = write_image(pfObj, options)
22
% Function to write plotly figures to a supported image format, which
33
% are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps",
44
% "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
513

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;
2717
end
2818

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"
3026
imageFormat = "jpeg";
3127
end
3228

@@ -49,28 +45,26 @@
4945

5046
if ~isfile(kExec) || ~isfile(plyJsLoc)
5147
status = getKaleido();
52-
else
53-
status = 1;
54-
end
55-
56-
if status == 0
57-
return
48+
if status == 0
49+
return
50+
end
5851
end
5952

6053
mjLoc = replace(string(fullfile( ...
6154
wd, "kaleido", "etc", "mathjax", "MathJax.js")), "\", "/");
62-
scope="plotly";
55+
scope = "plotly";
6356

6457
% 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+
);
7468

7569
pfJson = native2unicode(jsonencode(q), "UTF-8");
7670
tFile = string(fullfile(wd, "kaleido", "temp.txt"));
@@ -84,34 +78,27 @@
8478
+ "--allow-file-access-from-files --disable-breakpad " ...
8579
+ "--disable-dev-shm-usage"];
8680

87-
if debug
88-
inputCmd = char(join(cmd, ""));
89-
fprintf("\nDebug info:\n%s\n\n", inputCmd);
90-
end
91-
9281
[code,out] = system(char(join(cmd, "")));
93-
if debug
94-
disp(out);
95-
end
9682

9783
if code ~= 0
9884
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) = [];
10691
end
92+
output = jsondecode(a(end));
10793

10894
if output.code ~= 0
10995
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
11697
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);
117104
end

0 commit comments

Comments
 (0)