rev 417 - trunk

[email protected] Wed, 27 Jul 2005 01:29:57 -0700 (PDT)
Newsgroups gmane.comp.printing.ghostscript.jbig2dec.cvs
Message-ID <[email protected]>
Author: giles
Date: 2005-07-27 01:29:56 -0700 (Wed, 27 Jul 2005)
New Revision: 417

Modified:
   trunk/jbig2_huffman.c
   trunk/jbig2_symbol_dict.c
Log:
Work in progress commit of huffman support. This version is still buggy a=
nd
includes some serious debug spew.


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-07-13 16:51:13 UTC (rev 416)
+++ trunk/jbig2_huffman.c	2005-07-27 08:29:56 UTC (rev 417)
@@ -25,6 +25,7 @@
 #include "os_types.h"
=20
 #include <stdlib.h>
+#include <stdio.h>
=20
 #include "jbig2.h"
 #include "jbig2_priv.h"
@@ -84,6 +85,25 @@
   return;
 }
=20
+/** debug routine */
+void jbig2_dump_huffman_state(Jbig2HuffmanState *hs)
+{
+  fprintf(stderr, "huffman state %08x %08x offset %d.%d\n",
+	hs->this_word, hs->next_word, hs->offset, hs->offset_bits);=20
+}
+
+/** debug routine */
+void jbig2_dump_huffman_binary(Jbig2HuffmanState *hs)
+{
+  const uint32_t word =3D hs->this_word;
+  int i;
+
+  fprintf(stderr, "huffman binary ");
+  for (i =3D 31; i >=3D 0; i--)
+    fprintf(stderr, ((word >> i) & 1) ? "1" : "0");
+  fprintf(stderr, "\n");
+}
+
 /** Skip bits up to the next byte boundary
  */
 void
@@ -91,7 +111,14 @@
 {
   int bits =3D hs->offset_bits & 7;
=20
-  if (bits) hs->offset_bits +=3D 8 - bits;
+  if (bits) {
+    bits =3D 8 - bits;
+    hs->offset_bits +=3D bits;
+    hs->this_word =3D (hs->this_word << bits) |=20
+	(hs->next_word >> (32 - hs->offset_bits));
+    printf("jbig2_huffman_skip() advancing %d bits (offset now %d)\n",=20
+	bits, hs->offset_bits);
+  }
=20
   if (hs->offset_bits >=3D 32) {
     Jbig2WordStream *ws =3D hs->ws;
@@ -99,6 +126,10 @@
     hs->offset +=3D 4;
     hs->next_word =3D ws->get_next_word (ws, hs->offset + 4);
     hs->offset_bits -=3D 32;
+    if (hs->offset_bits) {
+      hs->this_word =3D (hs->this_word << hs->offset_bits) |
+	(hs->next_word >> (32 - hs->offset_bits));
+    }
   }
 }
=20
@@ -108,10 +139,17 @@
 {
   Jbig2WordStream *ws =3D hs->ws;
=20
-  hs->offset +=3D offset;
-  hs->offset_bits =3D 0;
+  printf("jbig2_huffman_advance() advancing %d bytes\n", offset);
+  hs->offset +=3D offset & ~3;
+  hs->offset_bits +=3D (offset & 3) << 3;
+  if (hs->offset_bits >=3D 32) {
+    hs->offset +=3D 4;
+    hs->offset_bits -=3D 32;
+  }
   hs->this_word =3D ws->get_next_word (ws, hs->offset);
   hs->next_word =3D ws->get_next_word (ws, hs->offset + 4);
+  hs->this_word =3D (hs->this_word << hs->offset_bits) |
+	(hs->next_word >> (32 - hs->offset_bits));
 }
=20
 /* return the offset of the huffman decode pointer (in bytes)
@@ -120,7 +158,7 @@
 int=20
 jbig2_huffman_offset(Jbig2HuffmanState *hs)
 {
-  return hs->offset + (hs->offset_bits >> 4);
+  return hs->offset + (hs->offset_bits >> 3);
 }
=20
 /* read a number of bits directly from the huffman state
@@ -161,6 +199,13 @@
       flags =3D entry->flags;
       PREFLEN =3D entry->PREFLEN;
=20
+fprintf(stderr, "huffman reading prefix %x (entry %d len %d) flags %d\n"=
,
+	(this_word >> (32 - PREFLEN)),
+	(this_word >> (32 - log_table_size)), PREFLEN, flags);
+
+fprintf(stderr, "huffman state %08x %08x before prefix read\n",
+	this_word, hs->next_word);
+
       next_word =3D hs->next_word;
       offset_bits +=3D PREFLEN;
       if (offset_bits >=3D 32)
@@ -173,8 +218,13 @@
 	  hs->next_word =3D next_word;
 	  PREFLEN =3D offset_bits;
 	}
+fprintf(stderr, "huffman state %08x %08x after prefix read\n",
+	this_word, next_word);
+if (PREFLEN)
       this_word =3D (this_word << PREFLEN) |
 	(next_word >> (32 - offset_bits));
+fprintf(stderr, "huffman state %08x %08x after prefix update\n",
+	this_word, next_word);
       if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT)
 	{
 	  table =3D entry->u.ext_table;
@@ -194,6 +244,9 @@
       else
 	result +=3D HTOFFSET;
=20
+fprintf(stderr, "huffman reading range %x (%d bits)\n",
+	HTOFFSET, RANGELEN);
+
       offset_bits +=3D RANGELEN;
       if (offset_bits >=3D 32)
 	{
@@ -205,6 +258,7 @@
 	  hs->next_word =3D next_word;
 	  RANGELEN =3D offset_bits;
 	}
+if (RANGELEN)
       this_word =3D (this_word << RANGELEN) |
 	(next_word >> (32 - offset_bits));
     }
@@ -215,6 +269,9 @@
   if (oob !=3D NULL)
     *oob =3D (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
=20
+fprintf(stderr, "huffman value is %d%s\n", result,
+	(flags & JBIG2_HUFFMAN_FLAGS_ISOOB) ? " (oob)" : "");
+
   return result;
 }
=20

Modified: trunk/jbig2_symbol_dict.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_symbol_dict.c	2005-07-13 16:51:13 UTC (rev 416)
+++ trunk/jbig2_symbol_dict.c	2005-07-27 08:29:56 UTC (rev 417)
@@ -278,7 +278,9 @@
=20
       /* 6.5.6 */
       if (params->SDHUFF) {
+	jbig2_dump_huffman_state(hs);
 	  HCDH =3D jbig2_huffman_get(hs, params->SDHUFFDH, &code);
+	jbig2_dump_huffman_state(hs);
       } else {
 	  code =3D jbig2_arith_int_decode(IADH, as, &HCDH);
       }
@@ -318,7 +320,11 @@
 	    }
 	  /* 6.5.7 */
 	  if (params->SDHUFF) {
+	      jbig2_dump_huffman_state(hs);
+	      jbig2_dump_huffman_binary(hs);
 	      DW =3D jbig2_huffman_get(hs, params->SDHUFFDW, &code);
+	      jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+		"decoded symbol delta width %d", DW);
 	  } else {
 	      code =3D jbig2_arith_int_decode(IADW, as, &DW);
 	  }
@@ -403,9 +409,13 @@
 		      int ninsyms =3D params->SDINSYMS->n_symbols;
=20
 		      if (params->SDHUFF) {
+	jbig2_dump_huffman_state(hs);
 			  ID =3D jbig2_huffman_get_bits(hs, SBSYMCODELEN);
+	jbig2_dump_huffman_state(hs);
 			  RDX =3D jbig2_huffman_get(hs, SDHUFFRDX, &code);
+	jbig2_dump_huffman_state(hs);
 			  RDY =3D jbig2_huffman_get(hs, SDHUFFRDX, &code);
+	jbig2_dump_huffman_state(hs);
 		      } else {
 			  code =3D jbig2_arith_iaid_decode(IAID, as, (int32_t*)&ID);
 		          code =3D jbig2_arith_int_decode(IARDX, as, &RDX);
@@ -489,7 +499,9 @@
 	  /* todo: memory cleanup */
 	  return NULL;
 	}
+	jbig2_dump_huffman_state(hs);
 	jbig2_huffman_skip(hs);
+	jbig2_dump_huffman_state(hs);
 	image =3D jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
 	if (image =3D=3D NULL) {
 	  jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
@@ -498,14 +510,23 @@
 	  return NULL;
 	}
 	/* todo: if BMSIZE =3D=3D 0 bitmap is uncompressed */
+	if (BMSIZE =3D=3D 0) {
+	  jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+	    "uncompressed collective bitmap NYI!");
+	  return NULL;
+	}
 	jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
-	  "reading %dx%d collective bitmap for %d symbols (%d bytes)",
-	  image->width, image->height, NSYMSDECODED - HCFIRSTSYM, BMSIZE);
+	  "reading %dx%d collective bitmap for %d symbols"
+	  " (%d bytes at data offset %d)",
+	  image->width, image->height, NSYMSDECODED - HCFIRSTSYM,
+	  BMSIZE, jbig2_huffman_offset(hs));
 	rparams.MMR =3D 1;
 	code =3D jbig2_decode_generic_mmr(ctx, segment, &rparams,
 	    data + jbig2_huffman_offset(hs), BMSIZE, image);
         jbig2_image_write_pbm_file(image, "collective.pbm");
+	jbig2_dump_huffman_state(hs);
 	jbig2_huffman_advance(hs, BMSIZE);
+	jbig2_dump_huffman_state(hs);
       }
=20
   } /* end of symbol decode loop */