annotate scripts/image/imwrite.m @ 7987:4a423a042971

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