Skip to content

Commit

Permalink
Refactored write_tIME_chunk()
Browse files Browse the repository at this point in the history
  • Loading branch information
glennrp committed Aug 8, 2017
1 parent ca4efce commit aff9338
Showing 1 changed file with 54 additions and 56 deletions.
110 changes: 54 additions & 56 deletions coders/png.c
Original file line number Diff line number Diff line change
Expand Up @@ -7921,6 +7921,12 @@ Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
static void write_tIME_chunk(Image *image,png_struct *ping,png_info *info,
const char *date)
{
const char
*timestamp;

int
ret;

unsigned int
day,
hour,
Expand All @@ -7935,63 +7941,55 @@ 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");
if (sscanf(timestamp,"%d-%d-%dT%d:%d:%d%d:%d",&year,&month,&day,&hour,
&minute, &second, &addhours, &addminutes) != 8)
{
(void) ThrowMagickException(&image->exception,GetMagickModule(),
CoderError,
"Invalid date format specified for png:tIME","`%s'",
image->filename);
return;
}
}
timestamp=GetImageProperty(image,"date:modify");
else
{
if (sscanf(date,"%d-%d-%dT%d:%d:%d%d:%d",&year,&month,&day,&hour,
&minute, &second, &addhours, &addminutes) != 8)
{
(void) ThrowMagickException(&image->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);
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)
{
(void) ThrowMagickException(&image->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

Expand Down

0 comments on commit aff9338

Please sign in to comment.