changeset 11035:8a6b7947f618

Added support for checking supported image types at runtime.
author John Swensen <jpswensen@gmail.com>
date Tue, 28 Sep 2010 17:24:24 -0400
parents 6589aaf769f6
children 169f59f626d3
files scripts/ChangeLog scripts/image/imwrite.m src/ChangeLog src/DLD-FUNCTIONS/__magick_read__.cc
diffstat 4 files changed, 73 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Sep 28 17:23:52 2010 -0400
+++ b/scripts/ChangeLog	Tue Sep 28 17:24:24 2010 -0400
@@ -1,3 +1,8 @@
+2010-09-28  John P. Swensen  <jpswensen@gmail.com>
+
+	* image/imwrite.m: Call __magick_format_list__ to get list of
+	accepted formats.
+
 2010-09-28  Rik <octave@nomad.inbox5.com>
 
 	* scripts/linear-algebra/logm.m, scripts/plot/legend.m, 
--- a/scripts/image/imwrite.m	Tue Sep 28 17:23:52 2010 -0400
+++ b/scripts/image/imwrite.m	Tue Sep 28 17:24:24 2010 -0400
@@ -65,11 +65,12 @@
 
 function imwrite (img, varargin)
   
-  %missing_formats = { "hdf", "jp2", "jpx" }; 
-  persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ...
-    "pbm", "pcx", "pgm", "png", "pnm", "ppm", "ras", ...
-    "tif", "tiff", "xwd" };
+  persistent imwrite_possible_formats = {
+    "bmp" "gif"; "jp2"; "jpg"; "jpx"; "jpeg"; "hdf"; "pbm"; "pcx";
+    "pgm"; "png"; "pnm"; "ppm"; "ras"; "tif"; "tiff"; "xwd" };
 
+  persistent accepted_formats = __magick_format_list__ (imwrite_possible_formats);
+  
   if (nargin < 2 || ! (isnumeric (img) || islogical (img)))
     print_usage ();
   endif
--- a/src/ChangeLog	Tue Sep 28 17:23:52 2010 -0400
+++ b/src/ChangeLog	Tue Sep 28 17:24:24 2010 -0400
@@ -1,3 +1,8 @@
+2010-09-28  John P. Swensen  <jpswensen@gmail.com>
+
+	* DLD-FUNCTIONS/__magick_read__.cc (F__magick_format_list__):
+	New function. 
+
 2010-09-28  John P. Swensen  <jpswensen@gmail.com>
 
 	* DLD-FUNCTIONS/__magick_read__.cc (encode_uint_image)
--- a/src/DLD-FUNCTIONS/__magick_read__.cc	Tue Sep 28 17:23:52 2010 -0400
+++ b/src/DLD-FUNCTIONS/__magick_read__.cc	Tue Sep 28 17:24:24 2010 -0400
@@ -1132,3 +1132,61 @@
 }
 
 #undef GET_PARAM
+
+// Determine the file formats supported by GraphicsMagick.  This is
+// called once at the beginning of imread or imwrite to determine
+// exactly which file formats are supported, so error messages can be
+// displayed properly.
+
+DEFUN_DLD (__magick_format_list__, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn  {Function File} {} __magick_format_list__ (@var{formats})\n\
+Undocumented internal function.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+#ifdef HAVE_MAGICK
+  maybe_initialize_magick ();
+
+  std::list<std::string> accepted_formats;
+
+  if (args.length () == 1)
+    {
+      Cell c = args (0).cell_value ();
+
+      if (! error_state)
+        {
+          for (octave_idx_type i = 0; i < c.nelem (); i++)
+            {
+              try
+                {
+                  std::string fmt = c.elem (i).string_value ();
+
+                  Magick::CoderInfo info(fmt);
+
+                  if (info.isReadable () && info.isWritable ())
+                    accepted_formats.push_back (fmt);
+                }
+              catch (Magick::Exception& e)
+                {
+                  // Do nothing: exception here are simply missing formats.
+                }
+            }
+        }
+      else
+        error ("__magick_format_list__: expecting a cell array of image format names");
+    }
+  else
+    print_usage ();
+
+  retval = Cell (accepted_formats);
+
+#else
+
+  error ("__magick_format_list__: not available in this version of Octave");
+
+#endif
+
+  return retval;
+}