changeset 7987:4a423a042971

add imwrite.m
author John W. Eaton <jwe@octave.org>
date Tue, 29 Jul 2008 00:13:28 -0400
parents 3eb2094eefe5
children 21904fe299c8
files scripts/image/imwrite.m
diffstat 1 files changed, 131 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/image/imwrite.m	Tue Jul 29 00:13:28 2008 -0400
@@ -0,0 +1,131 @@
+## Copyright (C) 2008 John W. Eaton
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{})
+## @deftypefnx {Function File} {} imwrite (@var{img}, @var{map}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{})
+## Write images in various file formats.
+##
+## If @var{fmt} is missing, the file extension (if any) of
+## @var{filename} is used to determine the format.
+##
+## The parameter-value pairs (@var{p1}, @var{v1}, @dots{}) are optional.
+## @end deftypefn
+
+function imwrite (varargin)
+
+  persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ...
+    "pbm", "pgm", "png", "ppm", "svg", "tiff" };
+
+  img = [];
+  map = [];
+  fmt = "";
+
+  if (nargin > 1 && isnumeric (varargin{1}))
+    img = varargin{1};
+    offset = 2;
+    if (isnumeric (varargin{2}))
+      map = varargin{2};
+      if (isempty (map))
+	error ("imwrite: colormap must not be empty");
+      endif
+      offset = 3;
+    endif
+    if (offset <= nargin && ischar (varargin{offset}))
+      filename = varargin{offset};
+      offset++;
+      if (rem (nargin - offset, 2) == 0 && ischar (varargin{offset}))
+	fmt = varargin{offset};
+	offset++;
+      endif
+    else
+      print_usage ();
+    endif
+    if (offset < nargin)
+      warning ("imwrite: parameter-value options not implemented");
+    endif
+  else
+    print_usage ();
+  endif
+
+  filename = tilde_expand (filename);
+
+  if (isempty (fmt))
+    [d, n, fmt] = fileparts (filename);
+    if (! isempty (fmt))
+      fmt = fmt(2:end);
+    endif
+  endif
+
+  if (issparse (img) || issparse (map))
+    error ("imwrite: sparse images not supported");
+  endif
+
+  if (isempty (img))
+    error ("imwrite: invalid empty image");
+  endif
+
+  if (! strcmp (fmt, accepted_formats))
+    error ("imwrite: %s: unsupported or invalid image format", fmt);
+  endif
+
+  img_class = class (img);
+  map_class = class (map);
+
+  if (isempty (map))
+    if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"})))
+      nd = ndims (img);
+      if ((nd == 2 || nd == 3) && strcmp (img_class, "double"))
+	img = uint8 (img * 255);
+      endif
+      if (nd == 3 && size (img, 3) != 3)
+	error ("imwrite: invalid dimensions for truecolor image");
+      endif
+      if (nd == 4 && size (img, 3) != 1)
+	error ("imwrite: invalid size for multiframe image");
+      endif
+      if (nd > 5)
+	error ("imwrite: invalid %d-dimensional image data", nd);
+      endif
+    else
+      error ("imwrite: %s: invalid class for truecolor image", img_class);
+    endif
+    __magick_write__ (filename, fmt, img);
+  else
+    if (any (strcmp (img_class, {"uint8", "uint16", "double"})))
+      if (strcmp (img_class, "double"))
+	img = uint8 (img - 1);
+      endif
+      if (ndims (img) != 2)
+	error ("imwrite: invalid size for indexed image");
+      endif
+    else
+      error ("imwrite: %s: invalid class for indexed image data", img_class);
+    endif
+    if (isa (map, "double"))
+      if (ndims (map) != 2 || size (map, 2) != 3)
+	error ("imwrite: invalid size for colormap");
+      endif
+    else
+      error ("imwrite: %s invalid class for indexed image colormap",
+	     class (map));
+    endif
+    __magick_write__ (filename, fmt, img, map);
+  endif
+
+endfunction