7987
|
1 ## Copyright (C) 2008 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{}) |
|
21 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{map}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{}) |
|
22 ## Write images in various file formats. |
|
23 ## |
|
24 ## If @var{fmt} is missing, the file extension (if any) of |
|
25 ## @var{filename} is used to determine the format. |
|
26 ## |
|
27 ## The parameter-value pairs (@var{p1}, @var{v1}, @dots{}) are optional. |
|
28 ## @end deftypefn |
|
29 |
|
30 function imwrite (varargin) |
|
31 |
|
32 persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ... |
|
33 "pbm", "pgm", "png", "ppm", "svg", "tiff" }; |
|
34 |
|
35 img = []; |
|
36 map = []; |
|
37 fmt = ""; |
|
38 |
|
39 if (nargin > 1 && isnumeric (varargin{1})) |
|
40 img = varargin{1}; |
|
41 offset = 2; |
|
42 if (isnumeric (varargin{2})) |
|
43 map = varargin{2}; |
|
44 if (isempty (map)) |
|
45 error ("imwrite: colormap must not be empty"); |
|
46 endif |
|
47 offset = 3; |
|
48 endif |
|
49 if (offset <= nargin && ischar (varargin{offset})) |
|
50 filename = varargin{offset}; |
|
51 offset++; |
|
52 if (rem (nargin - offset, 2) == 0 && ischar (varargin{offset})) |
|
53 fmt = varargin{offset}; |
|
54 offset++; |
|
55 endif |
|
56 else |
|
57 print_usage (); |
|
58 endif |
|
59 if (offset < nargin) |
|
60 warning ("imwrite: parameter-value options not implemented"); |
|
61 endif |
|
62 else |
|
63 print_usage (); |
|
64 endif |
|
65 |
|
66 filename = tilde_expand (filename); |
|
67 |
|
68 if (isempty (fmt)) |
|
69 [d, n, fmt] = fileparts (filename); |
|
70 if (! isempty (fmt)) |
|
71 fmt = fmt(2:end); |
|
72 endif |
|
73 endif |
|
74 |
|
75 if (issparse (img) || issparse (map)) |
|
76 error ("imwrite: sparse images not supported"); |
|
77 endif |
|
78 |
|
79 if (isempty (img)) |
|
80 error ("imwrite: invalid empty image"); |
|
81 endif |
|
82 |
|
83 if (! strcmp (fmt, accepted_formats)) |
|
84 error ("imwrite: %s: unsupported or invalid image format", fmt); |
|
85 endif |
|
86 |
|
87 img_class = class (img); |
|
88 map_class = class (map); |
|
89 |
|
90 if (isempty (map)) |
|
91 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) |
|
92 nd = ndims (img); |
|
93 if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) |
|
94 img = uint8 (img * 255); |
|
95 endif |
|
96 if (nd == 3 && size (img, 3) != 3) |
|
97 error ("imwrite: invalid dimensions for truecolor image"); |
|
98 endif |
|
99 if (nd == 4 && size (img, 3) != 1) |
|
100 error ("imwrite: invalid size for multiframe image"); |
|
101 endif |
|
102 if (nd > 5) |
|
103 error ("imwrite: invalid %d-dimensional image data", nd); |
|
104 endif |
|
105 else |
|
106 error ("imwrite: %s: invalid class for truecolor image", img_class); |
|
107 endif |
|
108 __magick_write__ (filename, fmt, img); |
|
109 else |
|
110 if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) |
|
111 if (strcmp (img_class, "double")) |
|
112 img = uint8 (img - 1); |
|
113 endif |
|
114 if (ndims (img) != 2) |
|
115 error ("imwrite: invalid size for indexed image"); |
|
116 endif |
|
117 else |
|
118 error ("imwrite: %s: invalid class for indexed image data", img_class); |
|
119 endif |
|
120 if (isa (map, "double")) |
|
121 if (ndims (map) != 2 || size (map, 2) != 3) |
|
122 error ("imwrite: invalid size for colormap"); |
|
123 endif |
|
124 else |
|
125 error ("imwrite: %s invalid class for indexed image colormap", |
|
126 class (map)); |
|
127 endif |
|
128 __magick_write__ (filename, fmt, img, map); |
|
129 endif |
|
130 |
|
131 endfunction |