shithub: jbig2

Download patch

ref: 75e38f7a64fa7aace00e9a8a48cdfe1904adecc5
parent: d7d07a0b4a33596ac29f0e3809720e829cf1427b
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Tue Jul 9 05:45:32 EDT 2002

Work around missing end-of-page segment in CVision's PDF-embedded jbig2 streams.
jbig2_complete_page() becomes a public function marking the current page finished
so the client can call it at end-of-data, since data-shovel interface has no way
of passing that out-of-band. The segment dispatch calls a new jbig2_parse_end_of_page()
which does error checking and then calls jbig2_complete_page().


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

--- a/jbig2.h
+++ b/jbig2.h
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
         
-    $Id: jbig2.h,v 1.13 2002/07/08 14:26:02 giles Exp $
+    $Id: jbig2.h,v 1.14 2002/07/09 09:45:32 giles Exp $
 */
 
 #ifdef __cplusplus
@@ -85,6 +85,9 @@
 Jbig2Image *jbig2_page_out (Jbig2Ctx *ctx);
 /* mark a returned page image as read */
 int jbig2_release_page (Jbig2Ctx *ctx, Jbig2Image *image);
+/* mark the current page as complete, regardless of decoder state (for broken streams) */
+int jbig2_complete_page (Jbig2Ctx *ctx);
+
 
 /* segment header routines */
 
--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_page.c,v 1.11 2002/07/08 14:54:01 giles Exp $
+    $Id: jbig2_page.c,v 1.12 2002/07/09 09:45:32 giles Exp $
 */
 
 #ifdef HAVE_CONFIG_H
@@ -167,10 +167,23 @@
  * of the page image will also happen from here (NYI)
  **/
 int
-jbig2_complete_page (Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
+jbig2_complete_page (Jbig2Ctx *ctx)
 {
     uint32_t page_number = ctx->pages[ctx->current_page].number;
-    
+        
+    ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
+
+    return 0;
+}
+
+/**
+ * jbig2_parse_end_of_page: parse an end of page segment
+ **/
+int
+jbig2_parse_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
+{
+    uint32_t page_number = ctx->pages[ctx->current_page].number;
+
     if (segment->page_association != page_number) {
         jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
             "end of page marker for page %d doesn't match current page number %d",
@@ -177,10 +190,11 @@
             segment->page_association, page_number);
     }
     
-    ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
     jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
         "end of page %d", page_number);
 
+    jbig2_complete_page(ctx);
+    
 #ifdef OUTPUT_PBM
     jbig2_image_write_pbm(ctx->pages[ctx->current_page].image, stdout);
 #endif
@@ -228,6 +242,7 @@
     /* find the matching page struct and mark it released */
     for (index = 0; index < ctx->max_page_index; index++) {
         if (ctx->pages[index].image == image) {
+            /* todo: free associated image */
             ctx->pages[index].state = JBIG2_PAGE_RELEASED;
             jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
                 "page %d released by the client", ctx->pages[index].number);
--- a/jbig2_priv.h
+++ b/jbig2_priv.h
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
         
-    $Id: jbig2_priv.h,v 1.13 2002/07/08 19:23:11 giles Exp $
+    $Id: jbig2_priv.h,v 1.14 2002/07/09 09:45:32 giles Exp $
     
     shared library internals
 */
@@ -111,7 +111,7 @@
 };
 
 int jbig2_parse_page_info (Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
-int jbig2_complete_page (Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
+int jbig2_parse_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
 
 typedef enum {
     JBIG2_COMPOSE_OR = 0,
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_segment.c,v 1.16 2002/07/08 19:23:11 giles Exp $
+    $Id: jbig2_segment.c,v 1.17 2002/07/09 09:45:32 giles Exp $
 */
 
 #ifdef HAVE_CONFIG_H
@@ -84,7 +84,8 @@
             jbig2_get_int32(buf + offset);
         offset += referred_to_segment_size;
         jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number,
-            "refers to segment %d", referred_to_segments[i]);
+            "segment %d refers to segment %d",
+            result->number, ctx->segments[referred_to_segments[i]]->number);
       }
       result->referred_to_segments = referred_to_segments;
     }
@@ -185,7 +186,7 @@
     case 48:
       return jbig2_parse_page_info(ctx, segment, segment_data);
     case 49:
-      return jbig2_complete_page(ctx, segment, segment_data);
+      return jbig2_parse_end_of_page(ctx, segment, segment_data);
     case 50:
       return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
         "unhandled segment type 'end of stripe'");
--- a/jbig2dec.c
+++ b/jbig2dec.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2dec.c,v 1.28 2002/07/08 14:54:02 giles Exp $
+    $Id: jbig2dec.c,v 1.29 2002/07/09 09:45:32 giles Exp $
 */
 
 #ifdef HAVE_CONFIG_H
@@ -259,6 +259,11 @@
   {
     Jbig2Image *image;
     
+    /* work around broken CVision embedded streams */
+    if (f_page != NULL)
+      jbig2_complete_page(ctx);
+    
+    /* retrieve and write out all the completed pages */
     while ((image = jbig2_page_out(ctx)) != NULL) {
       write_page_image(&params, image);
       jbig2_release_page(ctx, image);