diff scripts/image/imformats.m @ 16949:1eb5e5f0ee13

imformats.m: add tests and fix bug when adding new formats.
author Carnë Draug <carandraug@octave.org>
date Thu, 11 Jul 2013 13:11:55 +0100
parents bfd119642f6a
children 3db796f89695
line wrap: on
line diff
--- a/scripts/image/imformats.m	Thu Jul 11 02:07:10 2013 +0100
+++ b/scripts/image/imformats.m	Thu Jul 11 13:11:55 2013 +0100
@@ -85,7 +85,7 @@
             error ("imformats: FORMAT to %s must be a structure.", arg1);
           endif
           arrayfun (@is_valid_format, arg2);
-          formats(end + numel (b)) = arg2;
+          formats(end + 1: end + numel (arg2)) = arg2;
           varargout{1} = formats;
 
         case {"remove", "update"},
@@ -274,3 +274,48 @@
     bool = strcmp (coder, info.Format);
   end_try_catch
 endfunction
+
+## changing the function to read
+%!testif HAVE_MAGICK
+%! fmt = imformats ("jpg");
+%! fmt.read = @(x) size (x, 2);
+%! imformats ("update", "jpg", fmt);
+%! assert (imread ("this is 30 characters long.jpg"), 30);
+
+## adding a new format
+%!testif HAVE_MAGICK
+%! fmt = imformats ("jpg");
+%! fmt.ext = "junk";
+%! fmt.read = @(x) true();
+%! imformats ("add", fmt);
+%! assert (imread ("some file.junk"), true);
+
+## adding multiple formats in one way
+%!testif HAVE_MAGICK
+%! fmt = imformats ("jpg");
+%! fmt.ext = "junk1";
+%! fmt.read = @(x) true();
+%! fmt(2) = fmt(1);
+%! fmt(2).ext = "junk2";
+%! imformats ("add", fmt);
+%! assert (imread ("some file.junk1"), true);
+%! assert (imread ("some file.junk2"), true);
+
+## changing format
+%!testif HAVE_MAGICK
+%! ori_fmt = mod_fmt = imformats ("jpg");
+%! mod_fmt.description = "Another description";
+%! imformats ("update", "jpg", mod_fmt);
+%! new_fmt = imformats ("jpg");
+%! assert (new_fmt.description, mod_fmt.description);
+%! imformats ("factory");
+%! new_fmt = imformats ("jpg");
+%! assert (new_fmt.description, ori_fmt.description);
+
+## FIXME: how to test for error together with testif?
+## update to an invalid format
+#%!testif HAVE_MAGICK
+#%! fmt = imformats ("jpg");
+#%! fmt = rmfield (fmt, "read");
+#%! error imformats ("update", "jpg", fmt);
+