shithub: jbig2

Download patch

ref: 91c71a44412a052616c45f6e50a0431a103514a8
parent: ef06d243c1a26abd9223181b8c5751ceb051258e
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Thu Jun 27 10:02:08 EDT 2002

checkpoint in-progress text decoder. close but no data yet.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@93 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_text.c,v 1.5 2002/06/24 23:28:13 giles Exp $
+    $Id: jbig2_text.c,v 1.6 2002/06/27 14:02:08 giles Exp $
 */
 
 #include <stddef.h>
@@ -98,6 +98,7 @@
     int S,T;
     int x,y;
     bool first_symbol = TRUE;
+    uint32_t index, max_id;
     Jbig2Image *IB;
     Jbig2ArithState *as;
     Jbig2ArithIntCtx *IADT = NULL;
@@ -107,6 +108,18 @@
     Jbig2ArithIntCtx *IAID = NULL;
     int code;
     
+    max_id = 0;
+    for (index = 0; index < n_dicts; index ++) {
+        max_id += dicts[index]->n_symbols;
+    }
+    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+        "symbol list contains %d glyphs in %d dictionaries", max_id, n_dicts);
+    
+    if (params->SBREFINE) {
+        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+            "text regions with refinement bitmaps NYI");
+    }
+    
     if (!params->SBHUFF) {
         Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
         as = jbig2_arith_new(ctx, ws);
@@ -126,8 +139,9 @@
     } else {
         code = jbig2_arith_int_decode(IADT, as, &STRIPT);
     }
+    fprintf(stderr, "decoded stript value %d (scale to %d)\n", STRIPT, -STRIPT*params->SBSTRIPS);
     /* 6.4.5 (2) */
-    STRIPT *= -params->SBSTRIPS;
+    STRIPT *= -(params->SBSTRIPS);
     FIRSTS = 0;
     NINSTANCES = 0;
     
@@ -141,6 +155,7 @@
         }
         DT *= params->SBSTRIPS;
         STRIPT += DT;
+        fprintf(stderr, "decoded DT = %d, STRIPT = %d\n", DT, STRIPT);
         
         /* (3c) */
         if (first_symbol) {
@@ -152,6 +167,8 @@
             }
             FIRSTS += DFS;
             CURS = FIRSTS;
+            first_symbol = FALSE;
+            fprintf(stderr, "decoded DFS = %d (first symbol) CURS = %d\n", DFS, CURS);
         } else {
             /* 6.4.8 */
             if (params->SBHUFF) {
@@ -159,18 +176,60 @@
             } else {
                 code = jbig2_arith_int_decode(IADS, as, &IDS);
             }
+            if (code) {
+                fprintf(stderr, "Symbol instance S coordinate OOB: End of Strip\n");
+                continue;
+            }
             CURS += IDS + params->SBDSOFFSET;
+            fprintf(stderr, "decoded IDS = %d, CURS = %d\n", IDS, CURS);
         }
+
         /* 6.4.9 */
-        code = jbig2_arith_int_decode(IAIT, as, &CURT);
+        if (params->SBSTRIPS == 1) {
+            CURT = 0;
+        } else if (params->SBHUFF) {
+            /* todo */
+        } else {
+            code = jbig2_arith_int_decode(IAIT, as, &CURT);
+        }
         T = STRIPT + CURT;
+        fprintf(stderr, "decoded CURT = %d, STRIPT = %d, T = %d\n", CURT, STRIPT, T);
         
-        /* (3b.iv) / 6.4.10 */
-        code = jbig2_arith_int_decode(IAID, as, &ID);
+        /* (3b.iv) / 6.4.10 decode the symbol id */
+        if (params->SBHUFF) {
+            /* todo */
+        } else {
+            code = jbig2_arith_int_decode(IAID, as, &ID);
+        }
+        if (ID < 0 || ID >= max_id) {
+            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+                "symbol id out of range! (%d/%d)", ID, max_id);
+        }
+        fprintf(stderr, "decoded symbol id = %d (code = %d)\n", ID, code);
+
+        /* (3b.v) look up the symbol bitmap IB */
+        {
+            int id = ID;
+            
+            index = 0;
+            while (id >= dicts[index]->n_symbols)
+                id -= dicts[index++]->n_symbols;
+            IB = dicts[index]->glyphs[id];
+        }
         
-        /* (3b.v) */
+        /* (3b.vi) */
+        if ((!params->TRANSPOSED) && (params->REFCORNER > 1)) {
+            CURS += IB->width - 1;
+        } else if ((params->TRANSPOSED) && (params->REFCORNER < 2)) {
+            CURS += IB->height - 1;
+        }
+        
+        /* (3b.vii) */
+        S = CURS;
+        
+         /* (3b.vii) */
         if (!params->TRANSPOSED) {
-          switch (params->REFCORNER) {	// FIXME: double check offsets
+          switch (params->REFCORNER) {  // FIXME: double check offsets
             case JBIG2_CORNER_TOPLEFT: x = S; y = T; break;
             case JBIG2_CORNER_TOPRIGHT: x = S - IB->width; y = T; break;
             case JBIG2_CORNER_BOTTOMLEFT: x = S; y = T - IB->height; break;
@@ -184,23 +243,12 @@
             case JBIG2_CORNER_BOTTOMRIGHT: x = S - IB->width; y = T - IB->height; break;
           }
         }
-            
-        /* (3b.vi) */
-        if ((!params->TRANSPOSED) && (params->REFCORNER > 1)) {
-            CURS += IB->width - 1;
-        } else if ((params->TRANSPOSED) && (params->REFCORNER < 2)) {
-            CURS += IB->height - 1;
-        }
         
-        /* (3b.vii) */
-        S = CURS;
-        
-        /* (3b.viii) */
-        // todo: choose glyph bitmap
-        
         /* (3b.ix) */
         jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
-            "composing glyph id %d at (%d, %d)", ID, x, y);
+            "composing glyph id %d: %dx%d @ (%d,%d) symbol %d/%d", 
+            ID, IB->width, IB->height, x, y, NINSTANCES + 1,
+            params->SBNUMINSTANCES);
         jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
         
         /* (3b.x) */
@@ -229,7 +277,6 @@
     Jbig2Image *image, *page_image;
     Jbig2SymbolDict **dicts;
     int n_dicts;
-    uint32_t num_instances;
     uint16_t flags;
     uint16_t huffman_flags;
     int8_t sbrat[4];
@@ -248,6 +295,7 @@
     
     params.SBHUFF = flags & 0x0001;
     params.SBREFINE = flags & 0x0002;
+    params.SBSTRIPS = 1 << ((flags & 0x000c) >> 2);
     params.REFCORNER = (flags & 0x0030) >> 4;
     params.TRANSPOSED = flags & 0x0040;
     params.SBCOMBOP = (flags & 0x00e0) >> 7;
@@ -278,7 +326,7 @@
       }
     
     /* 7.4.3.1.4 */
-    num_instances = jbig2_get_int32(segment_data + offset);
+    params.SBNUMINSTANCES = jbig2_get_int32(segment_data + offset);
     offset += 4;
     
     if (params.SBHUFF) {
@@ -306,7 +354,7 @@
     jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
         "text region: %d x %d @ (%d,%d) %d symbols",
         region_info.width, region_info.height,
-        region_info.x, region_info.y, num_instances);
+        region_info.x, region_info.y, params.SBNUMINSTANCES);
     }
     
     /* compose the list of symbol dictionaries */