ref: 66d05e3c767bad968a98b9f63a88b07cb9df624f
parent: 8fd3ed86d9a6b47715235d32994bd355f5d99f69
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon Feb 3 15:04:11 EST 2003
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. git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@207 ded80894-8fb9-0310-811b-c03f3676ab4d
--- a/jbig2.c
+++ b/jbig2.c
@@ -8,7 +8,7 @@
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
- $Id: jbig2.c,v 1.17 2002/08/05 22:55:02 giles Exp $
+ $Id: jbig2.c,v 1.18 2003/02/03 20:04:11 giles Exp $
*/
#ifdef HAVE_CONFIG_H
@@ -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;
--- 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.14 2002/07/09 09:45:32 giles Exp $
+ $Id: jbig2.h,v 1.15 2003/02/03 20:04:11 giles Exp $
*/
#ifdef __cplusplus
@@ -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);