GraphicsMagick: coders/wpg.c Remove code duplicity for loading W...

GraphicsMagick Commits <[email protected]> Sat, 21 Oct 2023 05:27:04 -0500
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.10612.1697884034.1961.graphicsmagick-commit@lists.sourceforge.net>
changeset 5fea8d88557e in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=5fea8d88557e
summary: coders/wpg.c Remove code duplicity for loading WPG1 and WPG2 palette.

diffstat:

 ChangeLog    |   5 ++++-
 coders/wpg.c |  43 +++++++++++++++++++++++++------------------
 2 files changed, 29 insertions(+), 19 deletions(-)

diffs (101 lines):

diff -r 7bce17dcefba -r 5fea8d88557e ChangeLog
--- a/ChangeLog	Sat Oct 21 01:07:24 2023 +0200
+++ b/ChangeLog	Sat Oct 21 12:26:40 2023 +0200
@@ -1,4 +1,7 @@
-2023-10-19  Fojtik Jaroslav  <[email protected]>
+2023-10-21  Fojtik Jaroslav  <[email protected]>
+	coders/wpg.c Remove code duplicity for loading WPG1 and WPG2 palette.
+
+2023-10-20  Fojtik Jaroslav  <[email protected]>
 	coders/wpg.c Fix loop end to 256 insert "Palette" chunk header
 	to debug log.
 
diff -r 7bce17dcefba -r 5fea8d88557e coders/wpg.c
--- a/coders/wpg.c	Sat Oct 21 01:07:24 2023 +0200
+++ b/coders/wpg.c	Sat Oct 21 12:26:40 2023 +0200
@@ -1109,6 +1109,26 @@
                             (unsigned int)_WPG_BITMAP_TYPE2.VertRes)
 
 
+typedef struct
+{
+    magick_uint16_t StartIndex;
+    magick_uint16_t NumOfEntries;
+} WPGColorMapRec;
+
+
+void LoadPaletteRec(Image *image, WPGColorMapRec *pWPG_Palette, const int logging)
+{
+  pWPG_Palette->StartIndex=ReadBlobLSBShort(image);
+  pWPG_Palette->NumOfEntries=ReadBlobLSBShort(image);
+  if(logging)
+    (void)LogMagickEvent(CoderEvent,GetMagickModule(),
+                "WPG Color palette:\n"
+                "    StartIndex=%u\n"
+                "    NumOfEntries=%u\n", (unsigned)pWPG_Palette->StartIndex, (unsigned)pWPG_Palette->NumOfEntries);
+
+}
+
+
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
@@ -1206,12 +1226,6 @@
     unsigned int VertRes;
   } WPGBitmapType2;
 
-  typedef struct
-  {
-    magick_uint16_t StartIndex;
-    magick_uint16_t NumOfEntries;
-  } WPGColorMapRec;
-
   /*
   typedef struct {
     unsigned long PS_unknown1;
@@ -1385,7 +1399,7 @@
               image->columns=BitmapHeader1.Width;
               image->rows=BitmapHeader1.Heigth;
               bpp=BitmapHeader1.Depth;
-				// Whole palette is useless for bilevel image.
+				/* Whole palette is useless for bilevel image. */
               if(bpp==1)
               {
                 image->storage_class = PseudoClass;
@@ -1399,14 +1413,10 @@
               goto UnpackRaster;
 
             case 0x0E:  /*Color palette */
-              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
-              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
-              if(logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
-                       "WPG Color palette:\n"
-                       "    StartIndex=%u\n"
-                       "    NumOfEntries=%u\n", (unsigned)WPG_Palette.StartIndex, (unsigned)WPG_Palette.NumOfEntries);
-
+              LoadPaletteRec(image,&WPG_Palette,logging);
               PaletteItems = WPG_Palette.NumOfEntries;
+              if(PaletteItems + WPG_Palette.StartIndex > 256)
+                  ThrowReaderException(CorruptImageError,ColormapExceedsColorsLimit,image);
               if(pPalette==NULL)
               {
                 pPalette = MagickAllocateResourceLimitedMemory(unsigned char *,(size_t)3*256);
@@ -1419,8 +1429,6 @@
                   pPalette[3*i+2] = WPG1_Palette[i].Blue;
                 }
               }
-              if(PaletteItems + WPG_Palette.StartIndex > 256)
-                  ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
               if(ReadBlob(image,PaletteItems*3,pPalette+3*WPG_Palette.StartIndex) != PaletteItems*3)
                   ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
               break;
@@ -1643,8 +1651,7 @@
               StartWPG.PosSizePrecision=ReadBlobByte(image);
               break;
             case 0x0C:    /* Color palette */
-              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
-              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
+              LoadPaletteRec(image,&WPG_Palette,logging);
 
               /* Sanity check for amount of palette entries. */
               if (WPG_Palette.NumOfEntries == 0)