Skip to content

Commit

Permalink
PNG codec can load and save 16-bit grayscale images
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Sep 7, 2024
1 parent c5af802 commit 97381a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/iv.pl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ sub setconv
sub icvt
{
my $im = $_[0]-> IV-> image;
$im->resample( $im->rangeLo, $im->rangeHi, 0, 255)
if $im->type & im::GrayScale && $im->get_bpp != 8;
$im-> set(
conversion => $_[0]-> {conversion},
type => $_[1],
Expand Down
15 changes: 12 additions & 3 deletions img/codec_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static int pngbpp[] = {
imbpp8 | imGrayScale, imbpp8,
imbpp4 | imGrayScale, imbpp4,
imbpp1 | imGrayScale, imbpp1,
imShort,
0
};
static char * features[] = {
Expand Down Expand Up @@ -358,13 +359,18 @@ process_header( PImgLoadFileInstance fi, Bool use_subloader )
&interlace_type, &compression_type, &filter);
channels = l->m_channels;
type = l->m_type;

switch ( bit_depth) {
case 2:
png_set_packing(png_ptr);
break;
case 16:
png_set_strip_16(png_ptr);
if ( color_type != PNG_COLOR_TYPE_GRAY )
#if PNG_LIBPNG_VER >= 10504
png_set_scale_16(png_ptr);
#else
png_set_strip_16(png_ptr);
#endif
break;
}

Expand Down Expand Up @@ -563,7 +569,7 @@ header_available(PImgLoadFileInstance fi)
l->want_nibbles = true;
break;
case 16:
l->m_type = 8;
l->m_type = (color_type == PNG_COLOR_TYPE_GRAY) ? imShort : 8;
break;
default:
sprintf( fi-> errbuf, "Bit depth %d is not supported", bit_depth);
Expand Down Expand Up @@ -1434,6 +1440,9 @@ write_IHDR(PImgSaveFileInstance fi)
if (( i-> type & imBPP) == 24) {
bit_depth = 8;
color_type = PNG_COLOR_TYPE_RGB;
} else if ( !s->icon && i->type == imShort ) {
color_type = PNG_COLOR_TYPE_GRAY;
bit_depth = 16;
} else {
color_type = ( i-> type & imGrayScale) ?
PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_PALETTE;
Expand Down

0 comments on commit 97381a5

Please sign in to comment.