shithub: jbig2

Download patch

ref: f22c99da50fac552a8ca9df8ab5f9c9d12a9b752
parent: 9320ddb95f4c003e2d25abf301d07ca1a06eacfa
author: Sebastian Rasmussen <sebras@gmail.com>
date: Tue Apr 24 21:50:07 EDT 2018

jbig2dec: Remember to put all new symbols into ref/agg dictionary.

Previously the symbol dictionary propagated to the text region
decoder for a refinement/aggregate coded symbol did not contain
all necessary symbols. Only the symbols in the symbol dictionaries
referred to (SDINSYMS) by the currently parsed symbol dictionary
would be propagated into the text region decoder (SBSYMS).

Table 17 in the specification states that SBSYMS should be set
according to 6.5.8.2.4 which clearly states that SDINSYMS as well
as any symbols decoded up to that point (NSYMSDECODED symbols in
SDNEWSYMS) ought to be introduced into the dictionary that is
propagated to the text region decoder (SBSYMS). This is now done
by providing the text region decoder not just with a single symbol
dictionary, but both with SDINSYMS and SDNEWSYMS allowing the
decoder to refer to symbols from both.

--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -251,8 +251,7 @@
     Jbig2ArithIntCtx *IARDY = NULL;
     int code = 0;
     Jbig2SymbolDict **refagg_dicts = NULL;
-    int n_refagg_dicts = 1;
-
+    uint32_t i;
     Jbig2TextRegionParams *tparams = NULL;
 
     /* 6.5.5 (3) */
@@ -317,6 +316,21 @@
         goto cleanup2;
     }
 
+    refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, 2);
+    if (refagg_dicts == NULL) {
+        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory allocating dictionary array");
+        goto cleanup4;
+    }
+    refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS);
+    if (refagg_dicts[0] == NULL) {
+        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory allocating symbol dictionary");
+        goto cleanup4;
+    }
+    for (i = 0; i < params->SDNUMINSYMS; i++) {
+        refagg_dicts[0]->glyphs[i] = jbig2_image_reference(ctx, params->SDINSYMS->glyphs[i]);
+    }
+    refagg_dicts[1] = SDNEWSYMS;
+
     /* 6.5.5 (4a) */
     while (NSYMSDECODED < params->SDNUMNEWSYMS) {
         int32_t HCDH, DW;
@@ -448,26 +462,11 @@
 
                     if (REFAGGNINST > 1) {
                         Jbig2Image *image;
-                        uint32_t i;
 
                         if (tparams == NULL) {
                             /* First time through, we need to initialise the */
                             /* various tables for Huffman or adaptive encoding */
                             /* as well as the text region parameters structure */
-                            refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_refagg_dicts);
-                            if (refagg_dicts == NULL) {
-                                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "out of memory allocating dictionary array");
-                                goto cleanup4;
-                            }
-                            refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS + params->SDNUMNEWSYMS);
-                            if (refagg_dicts[0] == NULL) {
-                                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory allocating symbol dictionary");
-                                goto cleanup4;
-                            }
-                            for (i = 0; i < params->SDNUMINSYMS; i++) {
-                                refagg_dicts[0]->glyphs[i] = jbig2_image_reference(ctx, params->SDINSYMS->glyphs[i]);
-                            }
-
                             tparams = jbig2_new(ctx, Jbig2TextRegionParams, 1);
                             if (tparams == NULL) {
                                 code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "out of memory creating text region params");
@@ -531,7 +530,7 @@
 
                         /* multiple symbols are handled as a text region */
                         code = jbig2_decode_text_region(ctx, segment, tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
-                                                        n_refagg_dicts, image, data, size, GR_stats, as, ws);
+                                                        2, image, data, size, GR_stats, as, ws);
                         if (code < 0) {
                             jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region");
                             jbig2_image_release(ctx, image);
@@ -539,7 +538,6 @@
                         }
 
                         SDNEWSYMS->glyphs[NSYMSDECODED] = image;
-                        refagg_dicts[0]->glyphs[params->SDNUMINSYMS + NSYMSDECODED] = jbig2_image_reference(ctx, SDNEWSYMS->glyphs[NSYMSDECODED]);
                     } else {
                         /* 6.5.8.2.2 */
                         /* bool SBHUFF = params->SDHUFF; */
@@ -851,6 +849,7 @@
     if (refagg_dicts != NULL) {
         if (refagg_dicts[0] != NULL)
             jbig2_sd_release(ctx, refagg_dicts[0]);
+        /* skip releasing refagg_dicts[1] as that is the same as SDNEWSYMS */
         jbig2_free(ctx->allocator, refagg_dicts);
     }