Bug 689836 - Huffman tables with only positive values are decoded incorrectly.
Summary: Huffman tables with only positive values are decoded incorrectly.
Status: RESOLVED FIXED
Alias: None
Product: jbig2dec
Classification: Unclassified
Component: Rendering (show other bugs)
Version: master
Hardware: All All
: P4 normal
Assignee: Masaki Ushizaka
URL:
Keywords:
Depends on:
Blocks: 691248
  Show dependency tree
 
Reported: 2008-05-12 14:14 UTC by Justin Greer
Modified: 2010-06-22 07:43 UTC (History)
1 user (show)

See Also:
Customer:
Word Size: ---


Attachments
b689836_b1.patch (796 bytes, patch)
2010-05-03 11:37 UTC, Masaki Ushizaka
Details | Diff
b689836_b2.patch (5.27 KB, patch)
2010-06-17 12:57 UTC, Masaki Ushizaka
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Justin Greer 2008-05-12 14:14:51 UTC
Some of the huffman tables only encode positive values, and so they don't use
the lower-range table line.  In these cases, jbig2dec ends up subtracting
HTOFFSET from RANGELOW instead of adding it.  (Spec section B.4)

An effective (but not completely explicit) patch is to check if the RANGELEN for
the table line is 32 bits.  (Only the upper/lower range lines use 32 bit offsets.)

Patch:

Index: jbig2_huffman.c
===================================================================
--- jbig2_huffman.c	(revision 464)
+++ jbig2_huffman.c	(working copy)
@@ -361,7 +361,7 @@
 	      /* todo: build extension tables */
 	      if (params->HTOOB && CURTEMP == n_lines - 1)
 		eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
-	      if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
+	      if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2) && RANGELEN == 32)
 		eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
 	      if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
 		  for (j = start_j; j < end_j; j++) {
Comment 1 Ralph Giles 2008-05-16 18:32:07 UTC
Thanks for the patch. I don't quite follow this one. Please attach a file so I
can trace through the decode.
Comment 2 Justin Greer 2008-05-16 19:15:25 UTC
Unfortunately I don't currently have an example PDF that I can distribute.

Specifically, the standard huffman tables A, B, D, K, L, M, and N don't use the
lower-range line.  The code used to assume that the second to last entry in the
huffman table was always a lower-range line, so when it decoded values against
that table line (B.4.4 and B.4.5), it would always use the method from B.4.4
(subtraction).

For the tables that don't use a lower-range line, though, it should be using
B.4.5 (addition) on that second-to-last line from the table.  

However, there's no explicit method in the code for specifying whether or not a
table uses a lower-range line.  It works out, though, that the only lines that
use 32-bit offsets are lower-range and upper-range lines.  All custom tables
have a lower-range line, so the "is it 32" check works on custom tables, too.

So basically we can just update the code so that it only sets the
JBIG2_HUFFMAN_FLAGS_ISLOW flag on the 2nd-to-last table line if the rangelen for
that line is 32.


I could provide some debug info from a trace on one of the PDF files I have, if
that'd help, too.  Just let me know what you need.

Comment 3 Masaki Ushizaka 2010-05-03 11:37:04 UTC
Created attachment 6247 [details]
b689836_b1.patch

I apologize for late response.

To me, this seems more like huffman table problems than decoder code.  In jbig2_hufftab.h, the standard tables K, L and M do not have an entry for lower range line (L also lacks upper range line), while A, B, D and N does have lower line.

I made a patch to these tables (also includes fix for wrong RANGELEN value for table N).  I don't have a file to test this issue.
I would like to ask Justin Greer to try this patch to see if it fixes problems he has.
Comment 4 Masaki Ushizaka 2010-06-17 12:57:30 UTC
Created attachment 6382 [details]
b689836_b2.patch

ubc/042_11.jb2 has been affected by this problem (bug 691248).  My previous patch had a terrible mistake and this is a revised one. With this patch, now ubc/042_11.jb2 results a perfect bitmap.
I need to perform some tests before I commit.
Comment 5 Masaki Ushizaka 2010-06-22 07:43:50 UTC
By test code I committed in r11413, now I can expose the problems originally reported, plus a few more.
There ware problems in standard huffman table K, L, M and N.
The same test code also confirmed that b689836_b2.patch solved all the problems.  It was committed in r11415.
Now this issue was solved.  Closing.