]> granicus.if.org Git - imagemagick/commitdiff
Refactor png_write_tIME()
authorGlenn Randers-Pehrson <glennrp@gmail.com>
Tue, 8 Aug 2017 12:31:46 +0000 (08:31 -0400)
committerGlenn Randers-Pehrson <glennrp@gmail.com>
Tue, 8 Aug 2017 12:31:46 +0000 (08:31 -0400)
coders/png.c

index f84af76e7a420fd7ed8c4664701bdacee6911394..833dc6a94a45a001e34977391884b783cf7ca3f7 100644 (file)
@@ -8142,6 +8142,12 @@ static inline MagickBooleanType Magick_png_color_equal(const Image *image,
 static void write_tIME_chunk(Image *image,png_struct *ping,png_info *info,
   const char *date,ExceptionInfo *exception)
 {
+  const char
+    *timestamp;
+
+  int
+    ret;
+
   unsigned int
     day,
     hour,
@@ -8156,61 +8162,64 @@ static void write_tIME_chunk(Image *image,png_struct *ping,png_info *info,
     ptime;
 
   if (date == (const char *) NULL)
-    {
-      const char
-        *timestamp;
-
-      timestamp=GetImageProperty(image,"date:modify",exception);
-      if (sscanf(timestamp,"%d-%d-%dT%d:%d:%d%d:%d",&year,&month,&day,&hour,
-          &minute, &second, &addhours, &addminutes) != 8)
-      {
-          (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
-            "Invalid date format specified for png:tIME","`%s'",
-            image->filename);
-          return;
-      }
-    }
+    timestamp=GetImageProperty(image,"date:modify",exception);
   else
-    {
-      if (sscanf(date,"%d-%d-%dT%d:%d:%d%d:%d",&year,&month,&day,&hour,
-          &minute, &second, &addhours, &addminutes) != 8)
-      {
-          (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
-            "Invalid date format specified for png:tIME","`%s'",
-            image->filename);
-          return;
-      }
-    }
-   ptime.year=(png_uint_16) year;
-   ptime.month=(png_byte) month;
-   ptime.day=(png_byte) day;
-   ptime.hour=(png_byte) hour+addhours;
-   ptime.minute=(png_byte) minute+addminutes;
-   ptime.second=(png_byte) second;
-   if (ptime.minute > 60)
-   {
-      ptime.hour++;
-      ptime.minute-=60;
-   }
-   if (ptime.hour > 24)
-   {
-      ptime.day ++;
-      ptime.hour -=24;
-   }
-   /* To do: fix this for leap years */
-   if (ptime.day > 31 || (ptime.month == 2 && ptime.day > 28) ||
-       ((ptime.month == 4 || ptime.month == 6 || ptime.month == 9 ||
-       ptime.month == 11) && ptime.day > 30))
-   {
-      ptime.month++;
-      ptime.day = 1;
-   }
-   if (ptime.month > 12)
-   {
-      ptime.year++;
-      ptime.month=1;
-   }
-   png_set_tIME(ping,info,&ptime);
+    timestamp=date;
+
+  LogMagickEvent(CoderEvent,GetMagickModule(),
+      "  Writing tIME chunk: timestamp property is %30s\n",timestamp);
+  ret=sscanf(timestamp,"%d-%d-%dT%d:%d:%d",&year,&month,&day,&hour,
+      &minute, &second);
+  if (ret < 6)
+  {
+      (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
+        "Invalid date format specified for png:tIME","`%s'",
+        image->filename);
+      return;
+  }
+  addhours=0;     
+  addminutes=0;     
+  ret=sscanf(timestamp,"%d-%d-%dT%d:%d:%d%d:%d",&year,&month,&day,&hour,
+      &minute, &second, &addhours, &addminutes);
+  if (ret < 6)
+  {
+      LogMagickEvent(CoderEvent,GetMagickModule(),
+          " Invalid timestamp is %60s\n",timestamp);
+      (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
+        "Invalid date format specified for png:tIME","`%s'",
+        image->filename);
+      return;
+  }
+  ptime.year=(png_uint_16) year;
+  ptime.month=(png_byte) month;
+  ptime.day=(png_byte) day;
+  ptime.hour=(png_byte) hour+addhours;
+  ptime.minute=(png_byte) minute+addminutes;
+  ptime.second=(png_byte) second;
+  if (ptime.minute > 60)
+  {
+     ptime.hour++;
+     ptime.minute-=60;
+  }
+  if (ptime.hour > 24)
+  {
+     ptime.day ++;
+     ptime.hour -=24;
+  }
+  /* To do: fix this for leap years */
+  if (ptime.day > 31 || (ptime.month == 2 && ptime.day > 28) ||
+      ((ptime.month == 4 || ptime.month == 6 || ptime.month == 9 ||
+      ptime.month == 11) && ptime.day > 30))
+  {
+     ptime.month++;
+     ptime.day = 1;
+  }
+  if (ptime.month > 12)
+  {
+     ptime.year++;
+     ptime.month=1;
+  }
+  png_set_tIME(ping,info,&ptime);
 }
 #endif