Skip to content
Permalink
Browse files

Rewrite image alpha detection for better readability

  • Loading branch information
jbo-ads committed Nov 20, 2019
1 parent c3f706d commit a3b2ad5f6e826bd2807e9d6337274d572907f43e
Showing with 30 additions and 17 deletions.
  1. +30 −17 lib/imageio.c
@@ -62,24 +62,37 @@ mapcache_image_alpha_type mapcache_imageio_alpha_sniff(mapcache_context *ctx, ma
{
const unsigned char * b = buffer->buf;
mapcache_image_format_type t = mapcache_imageio_header_sniff(ctx,buffer);
if (t==GC_JPEG) {
// A JPG file is opaque
return MC_ALPHA_NO;
} else if (t==GC_PNG && buffer->size >= 26 && (b[12]|32) == 'i' && (b[13]|32) == 'h' && (b[14]|32) == 'd' && (b[15]|32) == 'r') {
// Check color type of PNG file in IHDR chunk
if (b[25] == 0 || b[25] == 2) {
// Gray or RGB without alpha
return MC_ALPHA_NO;
} else if (b[25] == 4 || b[25] == 6) {
// Gray or RGB with alpha
return MC_ALPHA_YES;
} else {
// TODO: Should check palette index
return MC_ALPHA_UNKNOWN;
}
} else {
return MC_ALPHA_UNKNOWN;
mapcache_image_alpha_type alpha_type;

switch (t) {
case GC_JPEG:
// JPEG files are opaque
alpha_type = MC_ALPHA_NO;
break;
case GC_PNG:
if (buffer->size >= 26) {
// Check color type of PNG file in IHDR chunk
if ( (b[12]|32)=='i' && (b[13]|32)=='h' && (b[14]|32)=='d' && (b[15]|32)=='r' ) {
switch (b[25]) {
case 4:
case 6:
// Gray or RGB with alpha
alpha_type = MC_ALPHA_YES;
break;
default:
// Other colortypes have no alpha channel
alpha_type = MC_ALPHA_NO;
}
}
} else {
alpha_type = MC_ALPHA_UNKNOWN;
}
break;
default:
alpha_type = MC_ALPHA_UNKNOWN;
break;
}
return alpha_type;
}

mapcache_image* mapcache_imageio_decode(mapcache_context *ctx, mapcache_buffer *buffer)

0 comments on commit a3b2ad5

Please sign in to comment.
You can’t perform that action at this time.