GraphicsMagick: 2 new changesets

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.11597.1667266580.1567.graphicsmagick-commit@lists.sourceforge.net>
changeset 7476caf5153e in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=7476caf5153e
summary: ReadXPMImage(): Use qsort+bsearch to improve performance of color-lookups.

changeset 04a6be8caf43 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=04a6be8caf43
summary: Merge heads

diffstat:

 ChangeLog                 |   14 ++++++
 PerlMagick/t/input1_1.wpg |  Bin 
 PerlMagick/t/read.t       |    6 ++-
 coders/wpg.c              |    5 +-
 coders/xpm.c              |  104 +++++++++++++++++++++++++++++++++++----------
 www/Changelog.html        |   25 +++++++++++
 6 files changed, 128 insertions(+), 26 deletions(-)

diffs (284 lines):

diff -r bb7f2ca233e9 -r 04a6be8caf43 ChangeLog
--- a/ChangeLog	Mon Oct 31 19:48:00 2022 +0100
+++ b/ChangeLog	Mon Oct 31 20:36:08 2022 -0500
@@ -1,3 +1,17 @@
+2022-10-31  Bob Friesenhahn  <[email protected]>
+
+	* coders/xpm.c (ReadXPMImage): coders/xpm.c (ReadXPMImage): Use
+	qsort+bsearch to improve performance of color-lookups.
+
+2022-11-01  Fojtik Jaroslav  <[email protected]>
+
+	* coders/wpg.c Bilevel image must be allways monochrome.
+        Discard palette if exists.
+	* PerlMagick/t/input1_1.wpg
+	* PerlMagick/t/reference/read/input1_1.wpg.miff New testcase
+        with bilevel monochrome image.
+	* PerlMagick/t/read.t: Added input1_1.wpg to PerlMagick test suite.
+
 2022-10-31  Fojtik Jaroslav  <[email protected]>
 
 	* coders/xpm.c (ReadXPMImage): Output intelligent messag when colormap 
diff -r bb7f2ca233e9 -r 04a6be8caf43 PerlMagick/t/input1_1.wpg
Binary file PerlMagick/t/input1_1.wpg has changed
diff -r bb7f2ca233e9 -r 04a6be8caf43 PerlMagick/t/read.t
--- a/PerlMagick/t/read.t	Mon Oct 31 19:48:00 2022 +0100
+++ b/PerlMagick/t/read.t	Mon Oct 31 20:36:08 2022 -0500
@@ -13,7 +13,7 @@
 # Whenever a new test is added/removed, be sure to update the
 # 1..n ouput.
 #
-BEGIN { $| = 1; $test=1; print "TAP version 13\n1..86\n"; }
+BEGIN { $| = 1; $test=1; print "TAP version 13\n1..87\n"; }
 END {print "not ok $test\n" unless $loaded;}
 use Graphics::Magick;
 $loaded=1;
@@ -311,6 +311,10 @@
 ++$test;
 testReadCompare('input.wbmp', 'reference/read/input_wbmp.miff', q//, 0, 0);
 
+print("WPG (Word Perfect Graphics image, bilevel WPG level 1) ...\n");
+++$test;
+testReadCompare('input1_1.wpg', 'reference/read/input1_1_wpg.miff', q//, 0, 0);
+
 print("WPG (Word Perfect Graphics image, 4 bit depth WPG level 1) ...\n");
 ++$test;
 testReadCompare('input1_4.wpg', 'reference/read/input1_4_wpg.miff', q//, 0, 0);
diff -r bb7f2ca233e9 -r 04a6be8caf43 coders/wpg.c
--- a/coders/wpg.c	Mon Oct 31 19:48:00 2022 +0100
+++ b/coders/wpg.c	Mon Oct 31 20:36:08 2022 -0500
@@ -1303,7 +1303,8 @@
               image->columns=BitmapHeader1.Width;
               image->rows=BitmapHeader1.Heigth;
               bpp=BitmapHeader1.Depth;
-
+				// Whole palette is useless for bilevel image.
+              if(bpp==1) image->storage_class=DirectClass;
               goto UnpackRaster;
 
             case 0x0E:  /*Color palette */
@@ -1365,7 +1366,7 @@
               if(bpp>24)
                 {ThrowReaderException(CoderError,ColorTypeNotSupported,image)}
 
-              if ((image->storage_class != PseudoClass) && (bpp != 24))
+              if ((image->storage_class != PseudoClass) && (bpp != 24) && bpp!=1)
                 {
                   image->colors=1 << bpp;
                   if (!AllocateImageColormap(image,image->colors))
diff -r bb7f2ca233e9 -r 04a6be8caf43 coders/xpm.c
--- a/coders/xpm.c	Mon Oct 31 19:48:00 2022 +0100
+++ b/coders/xpm.c	Mon Oct 31 20:36:08 2022 -0500
@@ -57,7 +57,30 @@
   WritePICONImage(const ImageInfo *,Image *),
   WriteXPMImage(const ImageInfo *,Image *);
 
-typedef magick_uint32_t xpmkey_t;
+typedef magick_uint32_t xpmkeyval_t;
+
+typedef struct xpmkey
+{
+  magick_uint32_t index;        /* Colormap index */
+  xpmkeyval_t keyval;           /* Encoded XPM key value */
+} xpmkey_t;
+
+
+static int XPMKeyCompare(const void *l, const void *r)
+{
+  const xpmkey_t * restrict lp = l;
+  const xpmkey_t * restrict rp = r;
+  int sense;
+
+  if (lp->keyval > rp->keyval)
+    sense = 1;
+  else if (lp->keyval < rp->keyval)
+    sense = -1;
+  else
+    sense = 0;
+
+  return sense;
+}
 
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -233,7 +256,7 @@
 static Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   xpmkey_t
-    *keys = (unsigned int *) NULL;
+    *keys = (xpmkey_t *) NULL;
 
   char
     target[MaxTextExtent],
@@ -243,9 +266,6 @@
   Image
     *image;
 
-  xpmkey_t
-    key;
-
   int
     count;
 
@@ -353,7 +373,7 @@
       (image->columns == 0) || (image->rows == 0) ||
       (image->colors == 0))
     ThrowXPMReaderException(CorruptImageError,ImproperImageHeader,image);
-  if(image->colors > MaxColormapSize)
+  if (image->colors > MaxColormapSize)
     ThrowXPMReaderException(CoderError,ColormapTooLarge,image);
   image->depth=16;	/* TODO: Depth 16 is nonsense in many cases, please fix. */
 
@@ -421,7 +441,10 @@
   if (keys == (xpmkey_t *) NULL)
     ThrowXPMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
   for (i=0; i < image->colors; i++)
-    keys[i]=0;
+    {
+      keys[i].index=0;
+      keys[i].keyval=0;
+    }
   if (!AllocateImageColormap(image,image->colors))
     ThrowXPMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
 
@@ -444,11 +467,12 @@
       if (strlen(p) < width)
         break;
 
-      keys[j]=0;
+      keys[j].index = j;
+      keys[j].keyval = 0;
       for (k=0; k < width; k++)
-        keys[j] |= ((xpmkey_t) p[k]) << (k * 8);
+        keys[j].keyval |= ((xpmkeyval_t) p[k]) << (k * 8);
 
-      /* printf("Key[%u] =\"%s\" (0x%04X)\n", j, p, (unsigned int) keys[j]); */
+      /* printf("Key[%03u] =\"%s\" (0x%04X)\n", keys[j].index, p, (unsigned int) keys[j].keyval); */
       /*
         Parse color.
       */
@@ -521,10 +545,27 @@
   image->depth=GetImageDepth(image,&image->exception);
   image->depth=NormalizeDepthToOctet(image->depth);
 
-  j=0;
-  key=0;
   if (!image_info->ping)
     {
+      xpmkey_t
+        key = { 0, 0 },
+        *keyp;
+
+      xpmkeyval_t
+        keyval;
+
+      j = 0;
+
+      /*
+        Sort keys by kval
+      */
+      qsort((void *) keys,image->colors, sizeof(keys[0]), XPMKeyCompare);
+
+#if 0
+      for (j=0; j < image->colors; j++)
+        printf("Key[%03u] = (0x%04X)\n", keys[j].index, (unsigned int) keys[j].keyval);
+#endif
+
       /*
         Read image pixels.
       */
@@ -544,7 +585,7 @@
           indexes=AccessMutableIndexes(image);
           for (x=0; x < (long) image->columns; x++)
             {
-              key=0;
+              keyval=0;
               for (k=0; k < width; k++)
                 {
                   if (p[k] == '\0')
@@ -552,23 +593,40 @@
                       status=MagickFail;
                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                                             "Unexpected end of row %ld! (k=%u)", y, k);
+                      ThrowXPMReaderException(CorruptImageError,UnexpectedEndOfFile,
+                                              image);
                       break;
                     }
-                  key |= ((unsigned int) p[k]) << (k * 8);
+                  keyval |= ((xpmkeyval_t) p[k]) << (k * 8);
                 }
               if (MagickFail == status)
                 break;
-              /* printf("Key[%ld,%ld] = 0x%04X\n", x, y, (unsigned int) key); */
-              if (key != keys[j])
-                for (j=0; j < Max(image->colors-1,1); j++)
-                  if (key == keys[j])
-                    break;
-              VerifyColormapIndex(image,j);
+              if (keyval != key.keyval)
+                {
+                  key.keyval = keyval;
+                  keyp=(void *) bsearch((const void *) &key,(void *) keys,image->colors,
+                                        sizeof(keys[0]),XPMKeyCompare);
+                  if (keyp != (void *) NULL)
+                    {
+                      key.index = keyp->index;
+                    }
+                  else
+                    {
+                      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                                            "Failed to find key! (0x%04X)",
+                                            key.keyval);
+                      ThrowXPMReaderException(CorruptImageError,CorruptImage,
+                                              image);
+                      break;
+                    }
+                }
+
+              VerifyColormapIndex(image,key.index);
               if (image->storage_class == PseudoClass)
-                indexes[x]=(IndexPacket) j;
-              *r=image->colormap[j];
+                indexes[x]=(IndexPacket) key.index;
+              *r=image->colormap[key.index];
               r->opacity=(Quantum)
-                (j == none ? TransparentOpacity : OpaqueOpacity);
+                (key.index == none ? TransparentOpacity : OpaqueOpacity);
               r++;
               p+=width;
             }
diff -r bb7f2ca233e9 -r 04a6be8caf43 www/Changelog.html
--- a/www/Changelog.html	Mon Oct 31 19:48:00 2022 +0100
+++ b/www/Changelog.html	Mon Oct 31 20:36:08 2022 -0500
@@ -40,6 +40,31 @@
 <p>2022-10-31  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <ul class="simple">
+<li><p>coders/xpm.c (ReadXPMImage): coders/xpm.c (ReadXPMImage): Use
+qsort+bsearch to improve performance of color-lookups.</p></li>
+</ul>
+</blockquote>
+<p>2022-11-01  Fojtik Jaroslav  &lt;<a class="reference external" href="mailto:JaFojtik&#37;&#52;&#48;yandex&#46;com">JaFojtik<span>&#64;</span>yandex<span>&#46;</span>com</a>&gt;</p>
+<blockquote>
+<ul class="simple">
+<li><p>coders/wpg.c Bilevel image must be allways monochrome.
+Discard palette if exists.</p></li>
+<li><p>PerlMagick/t/input1_1.wpg</p></li>
+<li><p>PerlMagick/t/reference/read/input1_1.wpg.miff New testcase
+with bilevel monochrome image.</p></li>
+<li><p>PerlMagick/t/read.t: Added input1_1.wpg to PerlMagick test suite.</p></li>
+</ul>
+</blockquote>
+<p>2022-10-31  Fojtik Jaroslav  &lt;<a class="reference external" href="mailto:JaFojtik&#37;&#52;&#48;yandex&#46;com">JaFojtik<span>&#64;</span>yandex<span>&#46;</span>com</a>&gt;</p>
+<blockquote>
+<ul class="simple">
+<li><p>coders/xpm.c (ReadXPMImage): Output intelligent messag when colormap
+exceeds limit.</p></li>
+</ul>
+</blockquote>
+<p>2022-10-31  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
+<blockquote>
+<ul class="simple">
 <li><p>magick/blob.c (ReadBlobString): Fix oss-fuzz issue 52917
 &quot;graphicsmagick:coder_TXT_fuzzer: Stack-buffer-overflow in
 ReadBlobString&quot;, which occurs due to a bug added in yesterday's
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.