Skip to content

Commit

Permalink
PNG: Support writing grayscale images [closes #163]
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Apr 6, 2022
1 parent 5e0391e commit 11bdeb4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
47 changes: 35 additions & 12 deletions src/sail-codecs/png/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,13 @@ enum SailPixelFormat png_private_png_color_type_to_pixel_format(int color_type,
}
break;
}

case PNG_COLOR_TYPE_GRAY_ALPHA: {
switch (bit_depth) {
case 8: return SAIL_PIXEL_FORMAT_BPP16_GRAYSCALE_ALPHA;
case 16: return SAIL_PIXEL_FORMAT_BPP32_GRAYSCALE_ALPHA;
}
break;
}

case PNG_COLOR_TYPE_PALETTE: {
switch (bit_depth) {
case 1: return SAIL_PIXEL_FORMAT_BPP1_INDEXED;
Expand All @@ -172,15 +170,13 @@ enum SailPixelFormat png_private_png_color_type_to_pixel_format(int color_type,
}
break;
}

case PNG_COLOR_TYPE_RGB: {
switch (bit_depth) {
case 8: return SAIL_PIXEL_FORMAT_BPP24_RGB;
case 16: return SAIL_PIXEL_FORMAT_BPP48_RGB;
}
break;
}

case PNG_COLOR_TYPE_RGB_ALPHA: {
switch (bit_depth) {
case 8: return SAIL_PIXEL_FORMAT_BPP32_RGBA;
Expand All @@ -204,39 +200,68 @@ sail_status_t png_private_pixel_format_to_png_color_type(enum SailPixelFormat pi
*bit_depth = 1;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP2_INDEXED: {
*color_type = PNG_COLOR_TYPE_PALETTE;
*bit_depth = 2;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP4_INDEXED: {
*color_type = PNG_COLOR_TYPE_PALETTE;
*bit_depth = 4;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP8_INDEXED: {
*color_type = PNG_COLOR_TYPE_PALETTE;
*bit_depth = 8;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP1_GRAYSCALE: {
*color_type = PNG_COLOR_TYPE_GRAY;
*bit_depth = 1;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP2_GRAYSCALE: {
*color_type = PNG_COLOR_TYPE_GRAY;
*bit_depth = 2;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP4_GRAYSCALE: {
*color_type = PNG_COLOR_TYPE_GRAY;
*bit_depth = 4;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP8_GRAYSCALE: {
*color_type = PNG_COLOR_TYPE_GRAY;
*bit_depth = 8;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP16_GRAYSCALE: {
*color_type = PNG_COLOR_TYPE_GRAY;
*bit_depth = 16;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP16_GRAYSCALE_ALPHA: {
*color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
*bit_depth = 8;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP32_GRAYSCALE_ALPHA: {
*color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
*bit_depth = 16;
return SAIL_OK;
}
case SAIL_PIXEL_FORMAT_BPP24_RGB:
case SAIL_PIXEL_FORMAT_BPP24_BGR: {
*color_type = PNG_COLOR_TYPE_RGB;
*bit_depth = 8;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP48_RGB:
case SAIL_PIXEL_FORMAT_BPP48_BGR: {
*color_type = PNG_COLOR_TYPE_RGB;
*bit_depth = 16;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP32_RGBA:
case SAIL_PIXEL_FORMAT_BPP32_BGRA:
case SAIL_PIXEL_FORMAT_BPP32_ARGB:
Expand All @@ -245,7 +270,6 @@ sail_status_t png_private_pixel_format_to_png_color_type(enum SailPixelFormat pi
*bit_depth = 8;
return SAIL_OK;
}

case SAIL_PIXEL_FORMAT_BPP64_RGBA:
case SAIL_PIXEL_FORMAT_BPP64_BGRA:
case SAIL_PIXEL_FORMAT_BPP64_ARGB:
Expand All @@ -254,7 +278,6 @@ sail_status_t png_private_pixel_format_to_png_color_type(enum SailPixelFormat pi
*bit_depth = 16;
return SAIL_OK;
}

default: {
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNSUPPORTED_PIXEL_FORMAT);
}
Expand Down
9 changes: 4 additions & 5 deletions src/sail-codecs/png/png.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,8 @@ SAIL_EXPORT sail_status_t sail_codec_write_seek_next_frame_v6_png(void *state, s

int color_type;
int bit_depth;
SAIL_TRY_OR_EXECUTE(png_private_pixel_format_to_png_color_type(image->pixel_format, &color_type, &bit_depth),
/* cleanup */ SAIL_LOG_ERROR("PNG: %s pixel format is not currently supported for writing", sail_pixel_format_to_string(image->pixel_format));
return __sail_error_result);
SAIL_TRY_OR_CLEANUP(png_private_pixel_format_to_png_color_type(image->pixel_format, &color_type, &bit_depth),
/* cleanup */ SAIL_LOG_ERROR("PNG: %s pixel format is not currently supported for writing", sail_pixel_format_to_string(image->pixel_format)));

/* Write meta data. */
if (png_state->write_options->options & SAIL_OPTION_META_DATA && image->meta_data_node != NULL) {
Expand Down Expand Up @@ -583,11 +582,11 @@ SAIL_EXPORT sail_status_t sail_codec_write_seek_next_frame_v6_png(void *state, s
}

if (image->palette->pixel_format != SAIL_PIXEL_FORMAT_BPP24_RGB) {
SAIL_LOG_ERROR("PNG: Palette not in BPP24-RGB format is not supported");
SAIL_LOG_ERROR("PNG: Only BPP24-RGB palette is currently supported");
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNSUPPORTED_PIXEL_FORMAT);
}

/* Palette is deep copied. */
/* Deep copy palette. */
png_set_PLTE(png_state->png_ptr, png_state->info_ptr, image->palette->data, image->palette->color_count);
}

Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/png/png.codec.info.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tuning=png-filter
[write-features]
features=STATIC;META-DATA;INTERLACED;ICCP
tuning=
output-pixel-formats=BPP1-INDEXED;BPP2-INDEXED;BPP4-INDEXED;BPP8-INDEXED;BPP24-RGB;BPP24-BGR;BPP48-RGB;BPP48-BGR;BPP32-RGBA;BPP32-BGRA;BPP32-ARGB;BPP32-ABGR;BPP64-RGBA;BPP64-BGRA;BPP64-ARGB;BPP64-ABGR
output-pixel-formats=BPP1-INDEXED;BPP2-INDEXED;BPP4-INDEXED;BPP8-INDEXED;BPP1-GRAYSCALE;BPP2-GRAYSCALE;BPP4-GRAYSCALE;BPP8-GRAYSCALE;BPP16-GRAYSCALE;BPP16-GRAYSCALE-ALPHA;BPP32-GRAYSCALE-ALPHA;BPP24-RGB;BPP24-BGR;BPP48-RGB;BPP48-BGR;BPP32-RGBA;BPP32-BGRA;BPP32-ARGB;BPP32-ABGR;BPP64-RGBA;BPP64-BGRA;BPP64-ARGB;BPP64-ABGR
properties=
compression-types=DEFLATE
default-compression=DEFLATE
Expand Down

0 comments on commit 11bdeb4

Please sign in to comment.