GraphicsMagick: coders/jp2.c: Properly support passing JasPer op...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.43655.1640298194.1346.graphicsmagick-commit@lists.sourceforge.net>
changeset 917bddc905c9 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=917bddc905c9
summary: coders/jp2.c: Properly support passing JasPer options for decoder and encoder, including the 'debug' option.

diffstat:

 ChangeLog                              |    5 +
 VisualMagick/installer/inc/version.isx |    4 +-
 coders/jp2.c                           |  201 ++++++++++++++++++++++-----------
 magick/version.h                       |    4 +-
 www/Changelog.html                     |    4 +
 5 files changed, 147 insertions(+), 71 deletions(-)

diffs (343 lines):

diff -r 0616f69e17d3 -r 917bddc905c9 ChangeLog
--- a/ChangeLog	Wed Dec 22 16:12:34 2021 -0600
+++ b/ChangeLog	Thu Dec 23 16:22:58 2021 -0600
@@ -1,3 +1,8 @@
+2021-12-23  Bob Friesenhahn  <[email protected]>
+
+        * coders/jp2.c: Properly support passing JasPer options for
+        decoder and encoder, including the 'debug' option.
+
 2021-12-22  Bob Friesenhahn  <[email protected]>
 
         * coders/jp2.c (realloc_rlm): JasPer wants its custom memory
diff -r 0616f69e17d3 -r 917bddc905c9 VisualMagick/installer/inc/version.isx
--- a/VisualMagick/installer/inc/version.isx	Wed Dec 22 16:12:34 2021 -0600
+++ b/VisualMagick/installer/inc/version.isx	Thu Dec 23 16:22:58 2021 -0600
@@ -10,5 +10,5 @@
 
 #define public MagickPackageName "GraphicsMagick"
 #define public MagickPackageVersion "1.4"
-#define public MagickPackageVersionAddendum ".020211222"
-#define public MagickPackageReleaseDate "snapshot-20211222"
+#define public MagickPackageVersionAddendum ".020211223"
+#define public MagickPackageReleaseDate "snapshot-20211223"
diff -r 0616f69e17d3 -r 917bddc905c9 coders/jp2.c
--- a/coders/jp2.c	Wed Dec 22 16:12:34 2021 -0600
+++ b/coders/jp2.c	Thu Dec 23 16:22:58 2021 -0600
@@ -93,7 +93,7 @@
 #  endif
 
 /* Development JasPer 3.0.0 jas_initialize() is not yet ready for our purposes */
-#if !(defined(ENABLE_JAS_INITIALIZE) && ENABLE_JAS_INITIALIZE)
+#if !(defined(MAGICK_ENABLE_JAS_INITIALIZE) && MAGICK_ENABLE_JAS_INITIALIZE)
 #undef HAVE_JAS_INITIALIZE
 #endif /* if !defined(ENABLE_JAS_INITIALIZE) */
 
@@ -105,33 +105,44 @@
   WriteJP2Image(const ImageInfo *,Image *);
 
 static MagickBool jasper_initialized=MagickFalse;
-static const char jasper_options[][11] =
+static const char jasper_enc_options[][11] =
   {
+    "cblkheight",
+    "cblkwidth",
+    "debug",
+    "eph",
+    "ilyrrates",
     "imgareatlx",
     "imgareatly",
+    "lazy",
+    "mode",
+    "nomct",
+    "numgbits",
+    "numrlvls",
+    "prcheight",
+    "prcwidth",
+    "prg",
+    "pterm",
+    "rate",
+    "resetprob",
+    "segsym",
+    "sop",
+    "termall",
     "tilegrdtlx",
     "tilegrdtly",
+    "tileheight",
     "tilewidth",
-    "tileheight",
-    "prcwidth",
-    "prcheight",
-    "cblkwidth",
-    "cblkheight",
-    "mode",
-    "ilyrrates",
-    "prg",
-    "nomct",
-    "numrlvls",
-    "sop",
-    "eph",
-    "lazy",
-    "rate",
-    "termall",
-    "segsym",
-    "vcausal",
-    "pterm",
-    "resetprob",
-    "numgbits"
+    "vcausal"
+  };
+
+static const char jasper_dec_options[][12] =
+  {
+    "allow_trunc",
+    "debug",
+    "max_samples",
+    "maxlyrs",
+    "maxpkts",
+    "version"
   };
 
 
@@ -407,6 +418,7 @@
     (void) jas_stream_close(jp2_stream); \
   if (jp2_image) \
     jas_image_destroy(jp2_image); \
+  MagickFreeMemory(options); \
   ThrowReaderException(code_,reason_,image_); \
 }
 
@@ -541,7 +553,8 @@
     *channel_lut[4];
 
   char
-    options[MaxTextExtent];
+    option_keyval[MaxTextExtent],
+    *options = NULL;
 
   unsigned int
     status;
@@ -564,7 +577,6 @@
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
   (void) memset(channel_lut,0,sizeof(channel_lut));
-  options[0]='\0';
   image=AllocateImage(image_info);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == False)
@@ -650,12 +662,51 @@
   if (jp2_stream == (jas_stream_t *) NULL)
     ThrowReaderException(DelegateError,UnableToManageJP2Stream,image);
 
+  /*
+    Support passing Jasper options.
+  */
   {
-    /* Pass options argument which specifies "max_samples" to cap memory usage. */
-    const magick_uint64_t memory_limit = (magick_uint64_t) GetMagickResourceLimit(MemoryResource);
-    const magick_uint64_t max_samples = memory_limit/4U * sizeof(magick_uint32_t);
+    unsigned int
+      i;
+
+    MagickBool
+      max_samples_specified = MagickFalse;
+
+    for (i=0; i < ArraySize(jasper_dec_options); i++)
+      {
+        const char
+          *option = jasper_dec_options[i];
+
+        const char
+          *value;
+
+        if ((value=AccessDefinition(image_info,"jp2",option)) != NULL)
+          {
+            FormatString(option_keyval,"%s=%.1024s ",option,value);
+            ConcatenateString(&options,option_keyval);
 
-    FormatString(options,"max_samples=%" MAGICK_UINT64_F "u", max_samples);
+            if (LocaleCompare(option,"max_samples") == 0)
+              max_samples_specified=MagickTrue;
+
+            /* Setting debug mode seems to require extra assistance */
+            if (LocaleCompare(option,"debug") == 0)
+              jas_setdbglevel(atoi(value));
+          }
+      }
+
+    /*
+      If max_samples argument was not specified, then pass options
+      argument which specifies "max_samples" to cap memory usage.
+    */
+    if (!max_samples_specified)
+      {
+        const magick_uint64_t memory_limit = (magick_uint64_t) GetMagickResourceLimit(MemoryResource);
+        const magick_uint64_t max_samples = memory_limit/4U * sizeof(magick_uint32_t);
+
+        FormatString(option_keyval,"max_samples=%" MAGICK_UINT64_F "u ", max_samples);
+        ConcatenateString(&options,option_keyval);
+      }
+
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                           "JP2 options = \"%s\"", options);
   }
@@ -969,6 +1020,7 @@
     MagickFreeResourceLimitedMemory(channel_lut[component]);
   jas_matrix_destroy(pixels);
   (void) jas_stream_close(jp2_stream);
+  MagickFreeMemory(options);
   jas_image_destroy(jp2_image);
   StopTimer(&image->timer);
   return(image);
@@ -1179,8 +1231,10 @@
   register int
     x;
 
-  unsigned int
-    rate_specified=False,
+  MagickBool
+    rate_specified=MagickFalse;
+
+  MagickPassFail
     status;
 
   int
@@ -1232,6 +1286,53 @@
     }
 
   /*
+    Support passing Jasper options.
+  */
+  {
+    unsigned int
+      i;
+
+    for (i=0; i < ArraySize(jasper_enc_options); i++)
+      {
+        const char
+          *option = jasper_enc_options[i];
+
+        const char
+          *value;
+
+        if ((value=AccessDefinition(image_info,"jp2",option)) != NULL)
+          {
+            if (LocaleCompare(option,"rate") == 0)
+              {
+                /*
+                  It is documented that a rate specification of 1.0 should
+                  result in lossless compression.  However, Jasper only
+                  provides lossless compression if rate was not specified
+                  at all.  Support lossless compression as documented.
+                */
+                const double rate=atof(value);
+
+                if (rate < 1.0-MagickEpsilon)
+                  {
+                    FormatString(option_keyval,"%s=%.1024s ",option,value);
+                    ConcatenateString(&options,option_keyval);
+                    rate_specified=MagickTrue;
+                  }
+              }
+            else
+              {
+                FormatString(option_keyval,"%s=%.1024s ",option,value);
+                ConcatenateString(&options,option_keyval);
+
+                /* Setting debug mode seems to require extra assistance */
+                if (LocaleCompare(option,"debug") == 0)
+                  jas_setdbglevel(atoi(value));
+              }
+          }
+      }
+  }
+
+  /*
     Obtain a JP2 stream.
   */
   jp2_stream=JP2StreamManager(&StreamOperators, image);
@@ -1395,46 +1496,10 @@
   format=jas_image_strtofmt(magick);
 
   /*
-    Support passing Jasper options.
-  */
-  {
-    unsigned int
-      i;
-
-    for (i=0; i < ArraySize(jasper_options); i++)
-      {
-        const char
-          *value;
-
-        if ((value=AccessDefinition(image_info,"jp2",jasper_options[i])) != NULL)
-          {
-            if(LocaleCompare(jasper_options[i],"rate") == 0)
-              rate_specified=True;
-            /*
-              It is documented that a rate specification of 1.0 should
-              result in lossless compression.  However, Jasper only
-              provides lossless compression if rate was not specified
-              at all.  Support lossless compression as documented.
-            */
-            {
-              double
-                rate;
-
-              rate=atof(value);
-              if (rate < 1.0-MagickEpsilon)
-                {
-                  FormatString(option_keyval,"%s=%.1024s ",jasper_options[i],value);
-                  ConcatenateString(&options,option_keyval);
-                }
-            }
-          }
-      }
-  }
-  /*
     Provide an emulation of IJG JPEG "quality" by default if rate was
     not specified.
   */
-  if (rate_specified == False)
+  if (rate_specified == MagickFalse)
     {
       double
         rate=INFINITY;
@@ -1469,6 +1534,8 @@
       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
         "Compression rate: %g (%3.2f:1)",rate,1.0/rate);
     }
+
+
   if (options)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
        "Jasper options: \"%s\"", options);
diff -r 0616f69e17d3 -r 917bddc905c9 magick/version.h
--- a/magick/version.h	Wed Dec 22 16:12:34 2021 -0600
+++ b/magick/version.h	Thu Dec 23 16:22:58 2021 -0600
@@ -38,8 +38,8 @@
 #define MagickLibVersion  0x262300
 #define MagickLibVersionText  "1.4"
 #define MagickLibVersionNumber 26,23,0
-#define MagickChangeDate   "20211222"
-#define MagickReleaseDate  "snapshot-20211222"
+#define MagickChangeDate   "20211223"
+#define MagickReleaseDate  "snapshot-20211223"
 
 /*
   The MagickLibInterfaceNewest and MagickLibInterfaceOldest defines
diff -r 0616f69e17d3 -r 917bddc905c9 www/Changelog.html
--- a/www/Changelog.html	Wed Dec 22 16:12:34 2021 -0600
+++ b/www/Changelog.html	Thu Dec 23 16:22:58 2021 -0600
@@ -35,6 +35,10 @@
 <div class="document">
 
 
+<p>2021-12-23  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>
+* coders/jp2.c: Properly support passing JasPer options for
+decoder and encoder, including the 'debug' option.</blockquote>
 <p>2021-12-22  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>
 <p>* coders/jp2.c (realloc_rlm): JasPer wants its custom memory
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.