From a669be3da98fcc4f59cd8fa88e0d45f8a19fac27 Mon Sep 17 00:00:00 2001 From: "tor%cs.brown.edu" Date: Wed, 6 Sep 2000 04:42:38 +0000 Subject: [PATCH] Output console warning if chrome images are abusing alpha (8-bit channel to represent binary alpha or fully opaque 1-bit alpha). r=bryner, a=brendan. git-svn-id: svn://10.0.0.236/trunk@78245 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/libimg/src/if.cpp | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/mozilla/modules/libimg/src/if.cpp b/mozilla/modules/libimg/src/if.cpp index a587fc96780..a8e85f83874 100644 --- a/mozilla/modules/libimg/src/if.cpp +++ b/mozilla/modules/libimg/src/if.cpp @@ -1267,6 +1267,63 @@ il_image_abort(il_container *ic) } } +static void +IL_ChromeAlphaAbuseCheck(il_container *ic) +{ + IL_Pixmap *mask = ic->mask; + NI_PixmapHeader *header = &mask->header; + PRUint8 *alpha = (PRUint8 *)mask->bits; + + if (ic->image->header.alpha_bits == 8) { + unsigned num000 = 0, num255 = 0; + for (unsigned y=0; yheight; y++, alpha+=header->widthBytes) + for (unsigned x=0; xwidth; x++) { + if (alpha[x] == 0) + num000++; + else if (alpha[x] == 255) + num255++; + } + if (num000+num255 == header->width*header->height) { + fprintf(stderr, "CHROME ALPHA ABUSE\n"); + fprintf(stderr, "\tusing an 8-bit alpha channel to represent binary alpha\n"); + fprintf(stderr, "\t%s\n", ic->url_address); + fprintf(stderr, "\tnum000=%d num255=%d sum=%d w*h=%d\n", + num000, num255, num000+num255, header->width*header->height); + } + } else if (ic->image->header.alpha_bits == 1) { + PRBool constAlpha=PR_TRUE; + + /* check full bytes */ + for (unsigned y=0; + yheight && constAlpha; + y++, alpha+=header->widthBytes) + for (unsigned x=0; xwidth/8; x++) + if (alpha[x] != 255) { + constAlpha = PR_FALSE; + break; + } + + /* check trailing bits */ + PRUint8 mask = 255; + mask <<= 8-(header->width&7); + if (mask && constAlpha) + for (unsigned y=0; + yheight; + y++, alpha+=header->widthBytes) + if ((alpha[header->width/8]&mask) != mask) { + constAlpha = PR_FALSE; + break; + } + + if (constAlpha) { + fprintf(stderr, "CHROME ALPHA ABUSE\n"); + fprintf(stderr, "\tfully opaque 1-bit alpha mask\n"); + fprintf(stderr, "\t%s\n", ic->url_address); + } + } +} + + void IL_StreamComplete(il_container *ic, PRBool is_multipart) { @@ -1288,6 +1345,10 @@ IL_StreamComplete(il_container *ic, PRBool is_multipart) difference, ic->url_address)); #endif /* DEBUG */ + /* Check for abuse of alpha in chrome images */ + if ((ic->moz_type == TYPE_CHROME) && ic->image->header.alpha_bits) + IL_ChromeAlphaAbuseCheck(ic); + ic->is_multipart = is_multipart; if (ic->imgdec)