diff scripts/image/private/imageIO.m @ 16944:1b3b3ee88284

Add optional extension argument to image IO functions. * imfinfo.m, private/core_imfinfo.m, imread.m, private/core_imread.m, imwrite.m: added support for optional EXT argument. * imwrite.m: in addition to support the extra EXT argument, removed list of supported image formats from help text, and suggested to use imformats() instead. * private/core_imwrite.m: remove usage of __magick_format_list__() which is being replaced by imformats(). Check validity of colormap earlier on. Removed tests that are repeated in imwrite.m (leftovers from copying the file). * private/imageIO.m: deal with separate filename and extension and ignore file existence checking if we are writing. * __magic_read__.cc: remove private function __magick_format_list__() that was only being used by core_imwrite.m and is being replaced by imformats().
author Carnë Draug <carandraug@octave.org>
date Wed, 10 Jul 2013 18:18:21 +0100
parents bfad37d33435
children ee2166121a28
line wrap: on
line diff
--- a/scripts/image/private/imageIO.m	Tue Jul 09 18:53:06 2013 -0700
+++ b/scripts/image/private/imageIO.m	Wed Jul 10 18:18:21 2013 +0100
@@ -16,29 +16,54 @@
 ## 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
+## This function simply connects the function that calls 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.
+## to use if the file extension for the image file 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. Third argument is a cell array, its
+## first element the filename, and the second, an optional file extension to
+## add to filename, if filename alone does not exist. All the others are the
+## original input arguments passed to the original imageIO function which will
+## be passed on to the destination function.
 ##
-## No input checking whatsoever is performed. That is already done by the
+## No input checking whatsoever is performed. That should be performed 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) = [];
+
+  ## It should not be this function job to check if the file exists or not.
+  ## However, we need to know the file extension to use with imformats and
+  ## that means we need to know the actual filename that will be used which
+  ## is dependent on whether a file exists.
+  ##
+  ## If a file named filename{1} exists, then that's it, we will use that
+  ## wether it has an extension or not. If it does not exist and we have
+  ## something in filename{2}, then we will consider it the file extension.
+  ## Note the detail that if we find a file using filename{1} only, then we
+  ## should completely ignore filename{2}. It won't even be used by
+  ## imformats() at all, even if filename{1} has no extension to use with
+  ## imformats().
+  ##
+  ## To further complicate things, when we are going to be writing a
+  ## file, whether the file exists or not does not matter.
+  if (isscalar (filename) || (strcmp (fieldname, "write") &&
+      ! isempty (file_in_path (IMAGE_PATH, filename{1}))))
+    [~, ~, ext] = fileparts (filename{1});
+    if (! isempty (ext))
+      ## remove dot from extension
+      ext(1) = [];
+    endif
+  else
+    ext = filename{2};
   endif
-  fmt = imformats ("ext");
+
+  fmt = imformats (ext);
   ## When there is no match, fmt will be a 1x1 structure with no fields,
-  ## so we can't just use isempty ().
+  ## so we can't just use `isempty (fmt)'.
   if (isempty (fieldnames (fmt)))
     varargout{1:nargout} = core_func (varargin{:});
   else