Mercurial > octave
annotate scripts/plot/util/saveas.m @ 31212:55415fa6a20f
svgconvert: Use Lossless encoding of images when available (bug #52193)
* acinclude.m4: Check that QPainter::LosslessImageRendering flag is available.
* octave-svgconvert.cc (draw): Use new flag if available.
* print.m (doc): Update word of caution about svgconvert and images.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Mon, 29 Aug 2022 16:36:34 +0200 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30361
diff
changeset
|
3 ## Copyright (C) 2010-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
11253 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24423
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
11253 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24423
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
11253 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
11253 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24423
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
11253 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20778
diff
changeset
|
27 ## @deftypefn {} {} saveas (@var{h}, @var{filename}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20778
diff
changeset
|
28 ## @deftypefnx {} {} saveas (@var{h}, @var{filename}, @var{fmt}) |
30361
157180e55070
doc: Clarify possible values of input "h" for "saveas" (bug #61557).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
29 ## Save graphics object @var{h} to the file @var{filename} in graphics format |
20173
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## @var{fmt}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
31 ## |
30361
157180e55070
doc: Clarify possible values of input "h" for "saveas" (bug #61557).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
32 ## If @var{h} is the handle to a figure object, that figure object is saved. |
157180e55070
doc: Clarify possible values of input "h" for "saveas" (bug #61557).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
33 ## If @var{h} is the handle to a different graphics object, the figure |
157180e55070
doc: Clarify possible values of input "h" for "saveas" (bug #61557).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
34 ## containing that graphics object is saved. |
157180e55070
doc: Clarify possible values of input "h" for "saveas" (bug #61557).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
35 ## |
22758 | 36 ## All device formats accepted by @code{print} may be used. Common formats |
37 ## are: | |
11253 | 38 ## |
39 ## @table @code | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
40 ## |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
41 ## @item ofig |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
42 ## Octave figure file format (default) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
43 ## |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
44 ## @item mfig |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
45 ## Two files: Octave m-file @file{filename.m} containing code |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
46 ## to open Octave figure file @file{filename.ofig} |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
47 ## |
11253 | 48 ## @item ps |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
15411
diff
changeset
|
49 ## PostScript |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
50 ## |
11253 | 51 ## @item eps |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
15411
diff
changeset
|
52 ## Encapsulated PostScript |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
53 ## |
22758 | 54 ## @item pdf |
55 ## Portable Document Format | |
56 ## | |
11253 | 57 ## @item jpg |
58 ## JPEG Image | |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
59 ## |
11253 | 60 ## @item png |
25582
0df6454f58ea
print.m: Allow 'tif' to indicate 'tiff' format (bug #54282).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
61 ## Portable Network Graphics image |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
62 ## |
11253 | 63 ## @item emf |
25582
0df6454f58ea
print.m: Allow 'tif' to indicate 'tiff' format (bug #54282).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
64 ## Enhanced MetaFile |
0df6454f58ea
print.m: Allow 'tif' to indicate 'tiff' format (bug #54282).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
65 ## |
0df6454f58ea
print.m: Allow 'tif' to indicate 'tiff' format (bug #54282).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
66 ## @item tif |
0df6454f58ea
print.m: Allow 'tif' to indicate 'tiff' format (bug #54282).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
67 ## TIFF Image, compressed |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
68 ## |
11253 | 69 ## @end table |
70 ## | |
22758 | 71 ## If @var{fmt} is omitted it is extracted from the extension of |
72 ## @var{filename}. The default format when there is no extension is | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
73 ## @qcode{"ofig"}. |
11253 | 74 ## |
75 ## @example | |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
76 ## @group |
11253 | 77 ## clf (); |
78 ## surf (peaks); | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 ## saveas (1, "figure1.png"); |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
80 ## @end group |
11253 | 81 ## @end example |
82 ## | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
83 ## @seealso{print, savefig, hgsave, orient} |
11253 | 84 ## @end deftypefn |
85 | |
22758 | 86 function saveas (h, filename, fmt) |
11253 | 87 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28314
diff
changeset
|
88 if (nargin < 2) |
11253 | 89 print_usage (); |
90 endif | |
91 | |
24423
1d52db450db4
Change ishandle to return true for Java objects for Matlab compatibility.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
92 if (! ishghandle (h)) |
22758 | 93 error ("saveas: H must be a graphics handle"); |
11253 | 94 endif |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
95 if (! ischar (filename)) |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
96 error ("saveas: FILENAME must be a string"); |
11253 | 97 endif |
98 | |
22758 | 99 if (isfigure (h)) |
100 fig = h; | |
101 else | |
102 fig = ancestor (h, "figure"); | |
103 endif | |
104 | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
105 default_fmt = "ofig"; |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
106 |
11253 | 107 if (nargin == 2) |
22758 | 108 ## Attempt to infer format from filename |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11254
diff
changeset
|
109 [~, ~, ext] = fileparts (filename); |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
110 if (isempty (ext)) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
111 ext = ["." default_fmt]; |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
112 filename = [filename ext]; |
11253 | 113 endif |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
114 fmt = ext(2:end); |
11253 | 115 endif |
116 | |
117 if (nargin == 3) | |
22758 | 118 if (! ischar (fmt)) |
119 error ("saveas: FMT must be a string"); | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
120 elseif (isempty (fmt)) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
121 fmt = default_fmt; |
11253 | 122 endif |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11254
diff
changeset
|
123 [~, ~, ext] = fileparts (filename); |
11253 | 124 if (isempty (ext)) |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
125 ext = ["." fmt]; |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
126 filename = [filename ext]; |
11253 | 127 endif |
128 endif | |
129 | |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
130 fmt = tolower (fmt); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
131 |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
132 if (any (strcmp (fmt, {"ofig", "fig"}))) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
133 savefig (fig, filename); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
134 elseif (any (strcmp (fmt, {"m", "mfig"}))) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
135 [d, n] = fileparts (filename); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
136 mfilename = fullfile (d, [n ".m"]); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
137 figfilename = fullfile (d, [n ".ofig"]); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
138 |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
139 savefig (fig, figfilename); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
140 |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
141 fid = fopen (mfilename, "wt"); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
142 if (fid < 0) |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
143 error ("saveas: could not open '%s' for writing", mfilename); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
144 endif |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
145 fprintf (fid, ['h = openfig ("' figfilename '");' "\n"]); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
146 fclose (fid); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
147 else |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
148 prt_opt = ["-d" fmt]; |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
149 |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
150 print (fig, filename, prt_opt); |
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
151 endif |
11254
31f8534eb055
Add reference to new saveas function in printed manual.
Rik <octave@nomad.inbox5.com>
parents:
11253
diff
changeset
|
152 |
11253 | 153 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
154 |
28314
bc904ed5aad4
saveas.m: Change default format to .ofig (bug #58391).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27978
diff
changeset
|
155 |
22758 | 156 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
157 %!error <Invalid call> saveas () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
158 %!error <Invalid call> saveas (1) |
22758 | 159 %!error <H must be a graphics handle> saveas (Inf, "tst.pdf") |
160 %!error <FILENAME must be a string> saveas (0, 1) | |
161 %!error <FMT must be a string> saveas (0, "tst.pdf", 1) |