]> git.imager.perl.org - imager.git/commitdiff
- the tiff reader now puts warning messages produced during reading into
authorTony Cook <tony@develop=help.com>
Sat, 1 Jan 2005 13:45:39 +0000 (13:45 +0000)
committerTony Cook <tony@develop=help.com>
Sat, 1 Jan 2005 13:45:39 +0000 (13:45 +0000)
  the i_warning tag.
  Resolves: https://rt.cpan.org/Ticket/Display.html?id=8722

Changes
MANIFEST
imagei.h
t/t106tiff.t
tiff.c

diff --git a/Changes b/Changes
index 42aff908e50a666a7dfba8f795c0c97f40f5c1c5..65fa598dfb666b247d8fe9d171c058b2a26747d1 100644 (file)
--- a/Changes
+++ b/Changes
@@ -979,6 +979,9 @@ Revision history for Perl extension Imager.
   Imager's drawing functions do.
 - added trivial multiple master support via the FT2 driver
 - added .pcf and .pcf.gz to the list of extensions supported by ft2
+- the tiff reader now puts warning messages produced during reading into
+  the i_warning tag.
+  Resolves: https://rt.cpan.org/Ticket/Display.html?id=8722
 
 =================================================================
 
index 4529c889ff948098ebc3257dd61765f37eb18c78..a84141cfe5a0bbaf337ae686645ce9f8998ef6dc 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -202,6 +202,7 @@ testimg/short8.bmp      8-bit/pixel, data missing from EOF
 testimg/short8rle.bmp   8-bit/pixel compressed, data missing from EOF
 testimg/simple.pbm
 testimg/test_gimp_pal   A simple GIMP palette file
+testimg/tiffwarn.tif   Generates a warning while being read
 testimg/trimgdesc.gif
 testimg/trmiddesc.gif
 testimg/winrgb2.bmp  1-bit bmp base
index 1fbfdd2227743f7d44ff1c5f28eed85fa3e3eb2e..3f2133d9a2ddd227f2548852760aa913aac22e5f 100644 (file)
--- a/imagei.h
+++ b/imagei.h
@@ -45,4 +45,10 @@ extern void i_get_combine(int combine, i_fill_combine_f *, i_fill_combinef_f *);
 
 extern UTIL_table_t i_UTIL_table;
 
+/* Ideally this will move into imconfig.h if we ever probe */
+#if defined(_GNU_SOURCE) || __STDC_VERSION__ >= 199901L
+/* snprintf() is part of C99 and provided by Glibc */
+#define HAVE_SNPRINTF
+#endif
+
 #endif
index 48823beb30368ba59b6db44f8fe01816a60edaed..14565978bef97607059eb5adacb1cef5a623230f 100644 (file)
@@ -1,5 +1,5 @@
 #!perl -w
-print "1..72\n";
+print "1..74\n";
 use Imager qw(:all);
 $^W=1; # warnings during command-line tests
 $|=1;  # give us some progress in the test harness
@@ -24,7 +24,7 @@ i_box_filled($timg, 2, 2, 18, 18, $trans);
 my $test_num;
 
 if (!i_has_format("tiff")) {
-  for (1..72) {
+  for (1..74) {
     print "ok $_ # skip no tiff support\n";
   }
 } else {
@@ -344,6 +344,12 @@ if (!i_has_format("tiff")) {
 
   my ($format) = $imgs[0]->tags(name=>'i_format');
   ok(defined $format && $format eq 'tiff', "check i_format tag");
+
+  my $warned = Imager->new;
+  ok($warned->read(file=>"testimg/tiffwarn.tif"), "read tiffwarn.tif");
+  my ($warning) = $warned->tags(name=>'i_warning');
+  ok(defined $warning && $warning =~ /unknown field with tag 28712/,
+     "check that warning tag set and correct");
 }
 
 sub ok {
diff --git a/tiff.c b/tiff.c
index 89eb4a143a3f06c87fccd5ef782eb0cfe08c89d7..1ce8031eb2fbe708c02e485899669c417de3f9d7 100644 (file)
--- a/tiff.c
+++ b/tiff.c
@@ -61,10 +61,33 @@ static void error_handler(char const *module, char const *fmt, va_list ap) {
   i_push_errorvf(0, fmt, ap);
 }
 
+#define WARN_BUFFER_LIMIT 10000
+static char *warn_buffer = NULL;
+static int warn_buffer_size = 0;
+
 static void warn_handler(char const *module, char const *fmt, va_list ap) {
-  /* for now do nothing, perhaps we could warn(), though that should be
-     done in the XS code, not in the code which isn't mean to know perl 
-     exists ;) */
+  char buf[1000];
+
+  buf[0] = '\0';
+#ifdef HAVE_SNPRINTF
+  vsnprintf(buf, sizeof(buf), fmt, ap);
+#else
+  vsprintf(buf, fmt, ap);
+#endif
+  if (!warn_buffer || strlen(warn_buffer)+strlen(buf)+2 > warn_buffer_size) {
+    int new_size = warn_buffer_size + strlen(buf) + 2;
+    char *old_buffer = warn_buffer;
+    if (new_size > WARN_BUFFER_LIMIT) {
+      new_size = WARN_BUFFER_LIMIT;
+    }
+    warn_buffer = myrealloc(warn_buffer, new_size);
+    if (!old_buffer) *warn_buffer = '\0';
+    warn_buffer_size = new_size;
+  }
+  if (strlen(warn_buffer)+strlen(buf)+2 <= warn_buffer_size) {
+    strcat(warn_buffer, buf);
+    strcat(warn_buffer, "\n");
+  }
 }
 
 static int save_tiff_tags(TIFF *tif, i_img *im);
@@ -200,6 +223,10 @@ static i_img *read_one_tiff(TIFF *tif) {
   }
 
   i_tags_add(&im->tags, "i_format", 0, "tiff", -1, 0);
+  if (warn_buffer && *warn_buffer) {
+    i_tags_add(&im->tags, "i_warning", 0, warn_buffer, -1, 0);
+    *warn_buffer = '\0';
+  }
   
   /*   TIFFPrintDirectory(tif, stdout, 0); good for debugging */
 
@@ -367,6 +394,8 @@ i_readtiff_wiol(io_glue *ig, int length) {
   i_clear_error();
   old_handler = TIFFSetErrorHandler(error_handler);
   old_warn_handler = TIFFSetWarningHandler(warn_handler);
+  if (warn_buffer)
+    *warn_buffer = '\0';
 
   /* Add code to get the filename info from the iolayer */
   /* Also add code to check for mmapped code */
@@ -421,6 +450,8 @@ i_readtiff_multi_wiol(io_glue *ig, int length, int *count) {
   i_clear_error();
   old_handler = TIFFSetErrorHandler(error_handler);
   old_warn_handler = TIFFSetWarningHandler(warn_handler);
+  if (warn_buffer)
+    *warn_buffer = '\0';
 
   /* Add code to get the filename info from the iolayer */
   /* Also add code to check for mmapped code */