jbig2dec

Ralph Giles <[email protected]> Mon, 03 Feb 2003 12:04:14 -0800
Newsgroups gmane.comp.printing.ghostscript.jbig2dec.cvs
Message-ID <[email protected]>
Update of /cvsroot/jbig2dec/jbig2dec
In directory sc8-pr-cvs1:/tmp/cvs-serv9586

Modified Files:
	jbig2.c jbig2.h 
Log Message:
Provide a default error handler if the client doesn't specify one,
analogous to what we do with the allocator. Also, check for failure
of the initial context allocator and improve api description in the
public header file.


Index: jbig2.c
===================================================================
RCS file: /cvsroot/jbig2dec/jbig2dec/jbig2.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- jbig2.c	5 Aug 2002 22:55:02 -0000	1.17
+++ jbig2.c	3 Feb 2003 20:04:11 -0000	1.18
@@ -70,6 +70,21 @@
   return allocator->realloc (allocator, p, size);
 }
 
+static int
+jbig2_default_error(void *data, const char *msg, 
+                    Jbig2Severity severity, int32_t seg_idx)
+{
+    /* report only fatal errors by default */
+    if (severity == JBIG2_SEVERITY_FATAL) {
+        fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
+        if (seg_idx != -1) fprintf(stderr, " (segment 0x%02x)");
+        fprintf(stderr, "\n");
+        fflush(stderr);
+    }
+    
+    return 0;
+}
+
 int
 jbig2_error (Jbig2Ctx *ctx, Jbig2Severity severity, int segment_number,
 	     const char *fmt, ...)
@@ -101,8 +116,16 @@
 
   if (allocator == NULL)
       allocator = &jbig2_default_allocator;
+  if (error_callback == NULL)
+      error_callback = &jbig2_default_error;
 
   result = (Jbig2Ctx *)jbig2_alloc(allocator, sizeof(Jbig2Ctx));
+  if (result == NULL) {
+    error_callback(error_callback_data, "initial context allocation failed!",
+                    JBIG2_SEVERITY_FATAL, -1);
+    return result;
+  }
+  
   result->allocator = allocator;
   result->options = options;
   result->global_ctx = (const Jbig2Ctx *)global_ctx;

Index: jbig2.h
===================================================================
RCS file: /cvsroot/jbig2dec/jbig2dec/jbig2.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- jbig2.h	9 Jul 2002 09:45:32 -0000	1.14
+++ jbig2.h	3 Feb 2003 20:04:11 -0000	1.15
@@ -54,11 +54,20 @@
 void            jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image);
 
 
+/* errors are returned from the library via a callback. If no callback
+   is provided (a NULL argument is passed ot jbig2_ctx_new) a default
+   handler is used which prints fatal errors to the stderr stream. */
+   
 /* error callback */
 typedef int (*Jbig2ErrorCallback) (void *data,
 				  const char *msg, Jbig2Severity severity,
 				  int32_t seg_idx);
 
+/* memory allocation is likewise done via a set of callbacks so that
+   clients can better control memory usage. If a NULL is passed for
+   this argumennt of jbig2_ctx_new, a default allocator based on malloc()
+   is used. */
+   
 /* dynamic memory callbacks */
 struct _Jbig2Allocator {
   void *(*alloc) (Jbig2Allocator *allocator, size_t size);
@@ -81,11 +90,11 @@
 /* submit data to the decoder */
 int jbig2_data_in (Jbig2Ctx *ctx, const unsigned char *data, size_t size);
 
-/* get the next available decoded page image */
+/* get the next available decoded page image. NULL means there isn't one. */
 Jbig2Image *jbig2_page_out (Jbig2Ctx *ctx);
-/* mark a returned page image as read */
+/* mark a returned page image as no longer needed. */
 int jbig2_release_page (Jbig2Ctx *ctx, Jbig2Image *image);
-/* mark the current page as complete, regardless of decoder state (for broken streams) */
+/* mark the current page as complete, simulating an end-of-page segment (for broken streams) */
 int jbig2_complete_page (Jbig2Ctx *ctx);