changeset 16906:bfad37d33435

Connect imfinfo with imformats. * private/core_imfinfo.m: new function. Old code from imfinfo() moved here so that imformats() can create function handle to this part of the code only otherwise imfinfo() would get stuck into an endless loop, calling itself while respecting imformats() configuration. * private/imageIO.m: new function. Responsible for connecting the image IO functions with imformats(), and calling the correct function. Will later also be used by imread and imwrite. * imfinfo.m: reduced to minimum input check, until finding filename. Passes all arguments to new function imageIO(). * imformats.m: change calls to imfinfo() to the new core_imfinfo().
author Carnë Draug <carandraug@octave.org>
date Sat, 06 Jul 2013 04:28:04 +0100
parents 562301c71bd3
children 04f4f067eb33
files scripts/image/imfinfo.m scripts/image/imformats.m scripts/image/private/core_imfinfo.m scripts/image/private/imageIO.m
diffstat 4 files changed, 112 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/imfinfo.m	Fri Jul 05 17:10:39 2013 -0400
+++ b/scripts/image/imfinfo.m	Sat Jul 06 04:28:04 2013 +0100
@@ -109,49 +109,10 @@
 ## Author: Soren Hauberg <hauberg@gmail.com>
 
 function info = imfinfo (filename)
-
   if (nargin < 1)
     print_usage ();
-  endif
-
-  if (! ischar (filename))
+  elseif (! ischar (filename))
     error ("imfinfo: FILENAME must be a string");
   endif
-
-  filename = tilde_expand (filename);
-
-  delete_file = false;
-
-  unwind_protect
-
-    fn = file_in_path (IMAGE_PATH, filename);
-
-    if (isempty (fn))
-
-      ## Couldn't find file. See if it's an URL.
-
-      tmp = tmpnam ();
-
-      [fn, status, msg] = urlwrite (filename, tmp);
-
-      if (! status)
-        error ("imfinfo: cannot find %s", filename);
-      endif
-
-      if (! isempty (fn))
-        delete_file = true;
-      endif
-
-    endif
-
-    info = __magick_finfo__ (fn);
-
-  unwind_protect_cleanup
-
-    if (delete_file)
-      unlink (fn);
-    endif
-
-  end_unwind_protect
-
+  info = imageIO (@core_imfinfo, "info", filename, filename);
 endfunction
--- a/scripts/image/imformats.m	Fri Jul 05 17:10:39 2013 -0400
+++ b/scripts/image/imformats.m	Sat Jul 06 04:28:04 2013 +0100
@@ -241,7 +241,7 @@
   endfor
 
   ## the default info, read, and write functions
-  [formats.info ] = deal (@imfinfo);
+  [formats.info ] = deal (@core_imfinfo);
   [formats.read ] = deal (@imread);
   [formats.write] = deal (@imread);
 
@@ -270,7 +270,7 @@
 function bool = isa_magick (coder, filename)
   bool = false;
   try
-    info = imfinfo (filename);
+    info = core_imfinfo (filename);
     bool = strcmp (coder, info.Format);
   end_try_catch
 endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/image/private/core_imfinfo.m	Sat Jul 06 04:28:04 2013 +0100
@@ -0,0 +1,61 @@
+## Copyright (C) 2008-2012 Soren Hauberg
+##
+## 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/>.
+
+## This function does al the work of imfinfo. It exists here as private
+## function so that imfinfo can use other functions if imformats is
+## configured to. It is also needed so that imformats can create a
+## function handle for it.
+
+## Author: Soren Hauberg <hauberg@gmail.com>
+
+function info = core_imfinfo (filename)
+
+  if (nargin < 1)
+    print_usage ("imfinfo");
+  endif
+
+  if (! ischar (filename))
+    error ("imfinfo: FILENAME must be a string");
+  endif
+  filename = tilde_expand (filename);
+
+  delete_file = false;
+  unwind_protect
+
+    fn = file_in_path (IMAGE_PATH, filename);
+
+    if (isempty (fn))
+      ## Couldn't find file. See if it's an URL.
+      [fn, status, msg] = urlwrite (filename, tmpnam ());
+      if (! status)
+        error ("imfinfo: cannot find %s", filename);
+      endif
+
+      if (! isempty (fn))
+        delete_file = true;
+      endif
+    endif
+    info = __magick_finfo__ (fn);
+
+  unwind_protect_cleanup
+    if (delete_file)
+      unlink (fn);
+    endif
+  end_unwind_protect
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/image/private/imageIO.m	Sat Jul 06 04:28:04 2013 +0100
@@ -0,0 +1,47 @@
+## Copyright (C) 2013 Carnë Draug
+##
+## 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/>.
+
+## This function simply connects the function that call it to all
+## other imageIO functions. It does it by checking the file extension
+## of the file and redirecting to the appropriate function after checking
+## with imformats.
+##
+## First argument is a function handle for the default imageIO function (what
+## to use if the extensino is not listed by imformats), second argument is
+## the fieldname in the struct returned by imformats with a function handle
+## for the function to use, and all the others are the input argument mean for
+## the destination function.
+##
+## No input checking whatsoever is performed. That is already done by the
+## function calling it.
+
+function varargout = imageIO (core_func, fieldname, filename, varargin)
+  [~, ~, ext] = fileparts (filename);
+  ## remove dot from extension
+  if (! isempty (ext) && strcmp (ext(1), "."));
+    ext(1) = [];
+  endif
+  fmt = imformats ("ext");
+  ## When there is no match, fmt will be a 1x1 structure with no fields,
+  ## so we can't just use isempty ().
+  if (isempty (fieldnames (fmt)))
+    varargout{1:nargout} = core_func (varargin{:});
+  else
+    varargout{1:nargout} = fmt.(fieldname) (varargin{:});
+  endif
+endfunction