Runtime configuration setting for suppressing JPEG decoder errors.

Add new runtime config variable, images.jpeg.suppressDecoderErrors
which defaults to false in Debug and true otherwise.  When Jpeg errors
are suppressed and an error happens, SkJPEGImageDecoder::onDecode()
will return silently false (Consequently, so will SkImageDecoder's
DecodeFile() and DecodeMemory() functions).

Also, the test_image_decoder program now respects runtime
configuration settings.

BUG=skia:1680
R=scroggo@google.com

Review URL: https://codereview.chromium.org/27230002

git-svn-id: http://skia.googlecode.com/svn/trunk/src@11763 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/images/SkImageDecoder_libjpeg.cpp b/images/SkImageDecoder_libjpeg.cpp
index 6430951..52c7483 100644
--- a/images/SkImageDecoder_libjpeg.cpp
+++ b/images/SkImageDecoder_libjpeg.cpp
@@ -39,13 +39,20 @@
 
 #if defined(SK_DEBUG)
 #define DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS false
+#define DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS false
 #else  // !defined(SK_DEBUG)
 #define DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS true
+#define DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS true
 #endif  // defined(SK_DEBUG)
 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderWarnings,
                 "images.jpeg.suppressDecoderWarnings",
                 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS,
                 "Suppress most JPG warnings when calling decode functions.");
+SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderErrors,
+                "images.jpeg.suppressDecoderErrors",
+                DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS,
+                "Suppress most JPG error messages when decode "
+                "function fails.");
 
 //////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////
@@ -69,6 +76,9 @@
 static void do_nothing_emit_message(jpeg_common_struct*, int) {
     /* do nothing */
 }
+static void do_nothing_output_message(j_common_ptr) {
+    /* do nothing */
+}
 
 static void initialize_info(jpeg_decompress_struct* cinfo, skjpeg_source_mgr* src_mgr) {
     SkASSERT(cinfo != NULL);
@@ -83,6 +93,13 @@
     if (c_suppressJPEGImageDecoderWarnings) {
         cinfo->err->emit_message = &do_nothing_emit_message;
     }
+    /* To suppress error messages with a SK_DEBUG binary, set the
+     * environment variable "skia_images_jpeg_suppressDecoderErrors"
+     * to "true".  Inside a program that links to skia:
+     * SK_CONF_SET("images.jpeg.suppressDecoderErrors", true); */
+    if (c_suppressJPEGImageDecoderErrors) {
+        cinfo->err->output_message = &do_nothing_output_message;
+    }
 }
 
 #ifdef SK_BUILD_FOR_ANDROID
@@ -309,12 +326,12 @@
 // set a break-point in one place to see all error exists.
 static bool return_false(const jpeg_decompress_struct& cinfo,
                          const SkBitmap& bm, const char caller[]) {
-#ifdef SK_DEBUG
-    char buffer[JMSG_LENGTH_MAX];
-    cinfo.err->format_message((const j_common_ptr)&cinfo, buffer);
-    SkDebugf("libjpeg error %d <%s> from %s [%d %d]\n", cinfo.err->msg_code,
-             buffer, caller, bm.width(), bm.height());
-#endif
+    if (!(c_suppressJPEGImageDecoderErrors)) {
+        char buffer[JMSG_LENGTH_MAX];
+        cinfo.err->format_message((const j_common_ptr)&cinfo, buffer);
+        SkDebugf("libjpeg error %d <%s> from %s [%d %d]\n",
+                 cinfo.err->msg_code, buffer, caller, bm.width(), bm.height());
+    }
     return false;   // must always return false
 }