Skip to content

Commit

Permalink
PNG re-work: test tags from PNG reads and re-work a little
Browse files Browse the repository at this point in the history
libpng synthesizes gAMA and cHRM chunks on input when sRGB is present,
so ignore them if sRGB is present.

No longer generate a png_text tag when the keyword is recognized.

Added png_time
  • Loading branch information
tonycoz committed Apr 29, 2012
1 parent c046b03 commit 86464bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -227,6 +227,7 @@ PNG/t/00load.t
PNG/t/10png.t Test png support
PNG/testimg/badcrc.png
PNG/testimg/bilevel.png
PNG/testimg/comment.png
PNG/testimg/cover.png
PNG/testimg/cover16.png
PNG/testimg/cover16i.png
Expand Down
1 change: 1 addition & 0 deletions PNG/MANIFEST
Expand Up @@ -12,6 +12,7 @@ t/00load.t
t/10png.t
testimg/badcrc.png
testimg/bilevel.png
testimg/comment.png
testimg/cover.png
testimg/cover16.png
testimg/cover16i.png
Expand Down
57 changes: 39 additions & 18 deletions PNG/impng.c
Expand Up @@ -724,25 +724,28 @@ get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr, int bit_depth)
/* the various readers can call png_set_expand(), libpng will make
it's internal record of bit_depth at least 8 in that case */
i_tags_setn(&im->tags, "png_bits", bit_depth);


{
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
int intent;
if (png_get_sRGB(png_ptr, info_ptr, &intent)) {
i_tags_setn(&im->tags, "png_srgb_intent", intent);
}
}
{
else {
/* Ignore these if there's an sRGB chunk, libpng simulates
their existence if there's an sRGB chunk, and the PNG spec says
that these are ignored if the sRGB is present, so ignore them.
*/
double gamma;
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
i_tags_set_float2(&im->tags, "png_gamma", 0, gamma, 4);
}
}
{
double white_x, white_y;
double red_x, red_y;
double green_x, green_y;
double blue_x, blue_y;

if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
i_tags_set_float2(&im->tags, "png_gamma", 0, gamma, 4);
}

if (png_get_cHRM(png_ptr, info_ptr, &white_x, &white_y,
&red_x, &red_y, &green_x, &green_y,
&blue_x, &blue_y)) {
Expand All @@ -765,26 +768,44 @@ get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr, int bit_depth)
int i;
for (i = 0; i < num_text; ++i) {
int j;
char tag_name[50];
sprintf(tag_name, "png_text%d_key", i);
i_tags_set(&im->tags, tag_name, text[i].key, -1);
sprintf(tag_name, "png_text%d_text", i);
i_tags_set(&im->tags, tag_name, text[i].text, -1);
sprintf(tag_name, "png_text%d_type", i);
i_tags_set(&im->tags, tag_name,
(text[i].compression == PNG_TEXT_COMPRESSION_NONE
|| text[i].compression == PNG_TEXT_COMPRESSION_zTXt) ?
"text" : "itxt", -1);
int found = 0;

for (j = 0; j < text_tags_count; ++j) {
if (strcmp(text_tags[j].keyword, text[i].key) == 0) {
i_tags_set(&im->tags, text_tags[j].tagname, text[i].text, -1);
found = 1;
break;
}
}

if (!found) {
char tag_name[50];
sprintf(tag_name, "png_text%d_key", i);
i_tags_set(&im->tags, tag_name, text[i].key, -1);
sprintf(tag_name, "png_text%d_text", i);
i_tags_set(&im->tags, tag_name, text[i].text, -1);
sprintf(tag_name, "png_text%d_type", i);
i_tags_set(&im->tags, tag_name,
(text[i].compression == PNG_TEXT_COMPRESSION_NONE
|| text[i].compression == PNG_TEXT_COMPRESSION_zTXt) ?
"text" : "itxt", -1);
}
}
}
}

{
png_time *mod_time;

if (png_get_tIME(png_ptr, info_ptr, &mod_time)) {
char time_formatted[80];

sprintf(time_formatted, "%d-%02d-%02dT%02d:%02d:%02d",
mod_time->year, mod_time->month, mod_time->day,
mod_time->hour, mod_time->minute, mod_time->second);
i_tags_set(&im->tags, "png_time", time_formatted, -1);
}
}
}

static int
Expand Down
17 changes: 16 additions & 1 deletion PNG/t/10png.t
Expand Up @@ -10,7 +10,7 @@ my $debug_writes = 1;

init_log("testout/t102png.log",1);

plan tests => 204;
plan tests => 210;

# this loads Imager::File::PNG too
ok($Imager::formats{"png"}, "must have png format");
Expand Down Expand Up @@ -568,6 +568,21 @@ SKIP:
is($in->tags(name => "png_bits"), 16, "check png_bits");
}

SKIP:
{
my $im = Imager->new(file => "testimg/comment.png");
ok($im, "read file with comment")
or diag("Cannot read comment.png: ".Imager->errstr);
$im
or skip("Cannot test tags file I can't read", 5);
is($im->tags(name => "i_comment"), "Test comment", "check i_comment");
is($im->tags(name => "png_interlace"), "0", "no interlace");
is($im->tags(name => "png_interlace_name"), "none", "no interlace (text)");
is($im->tags(name => "png_srgb_intent"), "0", "srgb perceptual");
is($im->tags(name => "png_time"), "2012-04-15T03:36:50",
"modification time");
}

sub limited_write {
my ($limit) = @_;

Expand Down
Binary file added PNG/testimg/comment.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 86464bb

Please sign in to comment.