rev 432 - trunk

[email protected] Mon, 5 Dec 2005 16:02:26 -0800 (PST)
Newsgroups gmane.comp.printing.ghostscript.jbig2dec.cvs
Message-ID <[email protected]>
Author: giles
Date: 2005-08-25 01:14:21 -0700 (Thu, 25 Aug 2005)
New Revision: 432

Modified:
   trunk/jbig2_huffman.c
   trunk/jbig2_text.c
Log:
Work-in-progress commit of huffman text region support. Fix a number
of bugs in jbig2_huffman_get_bits() and implement symbol id huffman
table decode (a custom table is always included inline in the text
region segment header.)

However, the constructed table gives an out-of-bounds symbol id on
the first read with the UBC test file.


Modified: trunk/jbig2_huffman.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/jbig2_huffman.c	2005-08-23 06:09:55 UTC (rev 431)
+++ trunk/jbig2_huffman.c	2005-08-25 08:14:21 UTC (rev 432)
@@ -90,6 +90,7 @@
=20
 /** debug routines **/
 #ifdef JBIG2_DEBUG
+#include <stdio.h>
 /** print current huffman state */
 void jbig2_dump_huffman_state(Jbig2HuffmanState *hs)
 {
@@ -175,9 +176,18 @@
   int32_t result;
 =09
   result =3D this_word >> (32 - bits);
-  hs->this_word =3D (this_word << bits) |
-	(hs->next_word >> (32 - bits));
   hs->offset_bits +=3D bits;
+  if (hs->offset_bits >=3D 32) {
+    hs->offset +=3D 4;
+    hs->offset_bits -=3D 32;
+    hs->this_word =3D hs->next_word;
+    hs->next_word =3D hs->ws->get_next_word(hs->ws, hs->offset + 4);
+    hs->this_word =3D (hs->this_word << hs->offset_bits) |
+	(hs->next_word >> (32 - hs->offset_bits));
+  } else {
+    hs->this_word =3D (this_word << bits) |
+	(hs->next_word >> (32 - hs->offset_bits));
+  }
=20
   return result;
 }

Modified: trunk/jbig2_text.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/jbig2_text.c	2005-08-23 06:09:55 UTC (rev 431)
+++ trunk/jbig2_text.c	2005-08-25 08:14:21 UTC (rev 432)
@@ -157,7 +157,7 @@
 	Jbig2HuffmanLine runcodelengths[35];
 	Jbig2HuffmanLine *symcodelengths;
 	Jbig2HuffmanParams symcodeparams;
-	int code, err, range, r;
+	int code, err, len, range, r;
=20
 	jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
 	  "huffman coded text region");
@@ -173,7 +173,7 @@
 	  runcodelengths[index].RANGELEN =3D 0;
 	  runcodelengths[index].RANGELOW =3D index;
 	  jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
-	    "  read runcode%d length %d", index, runcodelengths[index]);
+	    "  read runcode%d length %d", index, runcodelengths[index].PREFLEN)=
;
 	}
 	runcodeparams.HTOOB =3D 0;
 	runcodeparams.lines =3D runcodelengths;
@@ -193,20 +193,34 @@
 	    "memory allocation failure reading symbol ID huffman table!");
 	  return -1;
 	}
-	for (index =3D 0; index < SBNUMSYMS; index++) {
+	index =3D 0;
+	while (index < SBNUMSYMS) {
 	  code =3D jbig2_huffman_get(hs, runcodes, &err);
 	  if (err !=3D 0 || code < 0 || code >=3D 35) {
 	    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 	      "error reading symbol ID huffman table!");
-	    return err;
+	    return err ? err : -1;
 	  }
=20
-	  if (code < 32) range =3D 0;
-	  else if (code =3D=3D 32) range =3D jbig2_huffman_get_bits(hs, 2);
-	  else if (code =3D=3D 34) range =3D jbig2_huffman_get_bits(hs, 3);
-	  else if (code =3D=3D 35) range =3D jbig2_huffman_get_bits(hs, 7);
-	  for (r =3D 0; r < range + 1; r++) {
-	    symcodelengths[index+r].PREFLEN =3D code;=20
+	  if (code < 32) {
+	    len =3D code;
+	    range =3D 1;
+	  } else {
+	    if (index < 1) {
+	      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+		"error decoding symbol id table: run length with no antecedent!");
+	      /* todo: memory cleanup */
+	      return -1;
+	    }
+	    len =3D symcodelengths[index-1].PREFLEN;
+	    if (code =3D=3D 32) range =3D jbig2_huffman_get_bits(hs, 2) + 3;
+	    else if (code =3D=3D 33) range =3D jbig2_huffman_get_bits(hs, 3) + =
3;
+	    else if (code =3D=3D 34) range =3D jbig2_huffman_get_bits(hs, 7) + =
11;
+	  }
+	  jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+	    "  read runcode%d at index %d (length %d range %d)", code, index, l=
en, range);
+	  for (r =3D 0; r < range; r++) {
+	    symcodelengths[index+r].PREFLEN =3D len;=20
 	    symcodelengths[index+r].RANGELEN =3D 0;=20
 	    symcodelengths[index+r].RANGELOW =3D index;
 	  }