]> granicus.if.org Git - libjpeg-turbo/commitdiff
tjLoadImage(): return TJPF_GRAY for grayscale BMPs
authorDRC <information@libjpeg-turbo.org>
Sun, 19 Nov 2017 14:43:07 +0000 (08:43 -0600)
committerDRC <information@libjpeg-turbo.org>
Sun, 19 Nov 2017 14:43:07 +0000 (08:43 -0600)
... if *pixelFormat=TJPF_UNKNOWN is passed to the function.

rdbmp.c
tjunittest.c

diff --git a/rdbmp.c b/rdbmp.c
index 44a62585d21aa729e622037d4b0360f7dfbe4bd4..e1c1208adde9a04942a8a646cb40eb21b3c664cc 100644 (file)
--- a/rdbmp.c
+++ b/rdbmp.c
@@ -133,6 +133,9 @@ read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
     break;
   }
 
+  if (sinfo->cinfo->in_color_space == JCS_UNKNOWN && gray)
+    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
+
   if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
     ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
 }
index e723a4f38b20b3e1c9f457d007bcf72f802ade86..f7d6e61a6bf41e5850ed870ad4339467a5dd580e 100644 (file)
@@ -792,7 +792,7 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
 {
        char filename[80], *md5sum, md5buf[65];
        int ps=tjPixelSize[pf], pitch=PAD(width*ps, align),
-               loadWidth=0, loadHeight=0, retval=0;
+               loadWidth=0, loadHeight=0, retval=0, pixelFormat=pf;
        unsigned char *buf=NULL;
        char *md5ref;
 
@@ -858,6 +858,22 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
                        retval=-1;  goto bailout;
                }
        }
+       /* Verify that tjLoadImage() returns the proper "preferred" pixel format for
+          the file type. */
+       tjFree(buf);  buf=NULL;
+       pf=pixelFormat;
+       pixelFormat=TJPF_UNKNOWN;
+       if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pixelFormat,
+               flags))==NULL)
+               _throwtj();
+       if((pf==TJPF_GRAY && pixelFormat!=TJPF_GRAY) ||
+               (pf!=TJPF_GRAY && !strcasecmp(ext, "bmp") && pixelFormat!=TJPF_BGR) ||
+               (pf!=TJPF_GRAY && !strcasecmp(ext, "ppm") && pixelFormat!=TJPF_RGB))
+       {
+               printf("\n   tjLoadImage() returned unexpected pixel format: %s\n",
+                       pixFormatStr[pixelFormat]);
+               retval=-1;
+       }
        unlink(filename);
 
        bailout: