Support for probing for fileformats.
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Mon, 19 Aug 2002 22:48:07 +0000 (22:48 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Mon, 19 Aug 2002 22:48:07 +0000 (22:48 +0000)
Imager.xs
image.c
image.h

index 2877f0f..1d8afcb 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1932,6 +1932,11 @@ i_readjpeg_wiol(ig)
 #endif
 
 
+char *
+i_test_format_probe(ig, length)
+        Imager::IO     ig
+              int     length
+
 
 
 #ifdef HAVE_LIBTIFF
diff --git a/image.c b/image.c
index a95313a..d327fec 100644 (file)
--- a/image.c
+++ b/image.c
@@ -2129,6 +2129,53 @@ int i_free_gen_write_data(i_gen_write_data *info, int flush)
   return result;
 }
 
+
+
+
+
+char *
+i_test_format_probe(io_glue *data, int length) {
+
+  static struct {
+    char *magic;
+    char *name;
+  } formats[] = {
+    {"\xFF\xD8", "jpeg"},
+    {"GIF87a", "gif"},
+    {"GIF89a", "gif"},
+    {"P1", "pnm"},
+    {"P2", "pnm"},
+    {"P3", "pnm"},
+    {"P4", "pnm"},
+    {"P5", "pnm"},
+    {"P6", "pnm"},
+    {"MM\0*", "tiff"},
+    {"II*\0", "tiff"},
+    {"BM", "bmp"},
+    {"\x89PNG\x0d\x0a\x1a\x0a", "png"}
+  };
+  unsigned int i;
+  char head[8];
+  char *match = NULL;
+
+  io_glue_commit_types(data);
+  data->readcb(data, head, 8);
+
+  for(i=0; i<sizeof(formats)/sizeof(formats[0]); i++) { 
+    int c = !strncmp(formats[i].magic, head, strlen(formats[i].magic));
+    if (c) {
+      match = formats[i].name;
+      break;
+    }
+  }
+
+  data->seekcb(data, -8, SEEK_CUR);
+  return match;
+}
+
+
+
+
 /*
 =back
 
diff --git a/image.h b/image.h
index f73526e..b21a20e 100644 (file)
--- a/image.h
+++ b/image.h
@@ -509,6 +509,10 @@ extern i_img *i_img_16_new_low(i_img *im, int x, int y, int ch);
 extern i_img *i_img_double_new(int x, int y, int ch);
 extern i_img *i_img_double_new_low(i_img *im, int x, int y, int ch);
 
+
+char * i_test_format_probe(io_glue *data, int length);
+
+
 #ifdef HAVE_LIBJPEG
 i_img *   
 i_readjpeg_wiol(io_glue *ig, int length, char** iptc_itext, int *itlength);