shithub: jbig2

Download patch

ref: e12fa07da845515911db8be2f7cfa8ef551061ba
parent: 31dd7ef66dbd1c34df08365aa3c36e6391617f37
author: Tor Andersson <[email protected]>
date: Wed Jul 14 20:49:09 EDT 2010

Use the jbig2_new and jbig2_renew macros instead of calling
jbig2_alloc and jbig2_realloc directly. Also adds a few typecasts
and #defines required to compile the source as C++.

--- a/jbig2.c
+++ b/jbig2.c
@@ -121,7 +121,7 @@
   if (error_callback == NULL)
       error_callback = &jbig2_default_error;
 
-  result = (Jbig2Ctx *)jbig2_alloc(allocator, sizeof(Jbig2Ctx));
+  result = (Jbig2Ctx*)jbig2_alloc(allocator, sizeof(Jbig2Ctx));
   if (result == NULL) {
     error_callback(error_callback_data, "initial context allocation failed!",
                     JBIG2_SEVERITY_FATAL, -1);
@@ -142,12 +142,12 @@
 
   result->n_segments = 0;
   result->n_segments_max = 16;
-  result->segments = (Jbig2Segment **)jbig2_alloc(allocator, result->n_segments_max * sizeof(Jbig2Segment *));
+  result->segments = jbig2_new(result, Jbig2Segment*, result->n_segments_max);
   result->segment_index = 0;
 
   result->current_page = 0;
   result->max_page_index = 4;
-  result->pages = (Jbig2Page *)jbig2_alloc(allocator, result->max_page_index * sizeof(Jbig2Page));
+  result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
   {
     int index;
     for (index = 0; index < result->max_page_index; index++) {
@@ -198,7 +198,7 @@
       do
 	buf_size <<= 1;
       while (buf_size < size);
-      ctx->buf = (byte *)jbig2_alloc(ctx->allocator, buf_size);
+      ctx->buf = jbig2_new(ctx, byte, buf_size);
       ctx->buf_size = buf_size;
       ctx->buf_rd_ix = 0;
       ctx->buf_wr_ix = 0;
@@ -219,7 +219,7 @@
 	  do
 	    buf_size <<= 1;
 	  while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size);
-	  buf = (byte *)jbig2_alloc(ctx->allocator, buf_size);
+	  buf = jbig2_new(ctx, byte, buf_size);
 	  memcpy(buf, ctx->buf + ctx->buf_rd_ix,
 		  ctx->buf_wr_ix - ctx->buf_rd_ix);
 	  jbig2_free(ctx->allocator, ctx->buf);
@@ -412,7 +412,7 @@
 Jbig2WordStream *
 jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size)
 {
-  Jbig2WordStreamBuf *result = (Jbig2WordStreamBuf *)jbig2_alloc(ctx->allocator, sizeof(Jbig2WordStreamBuf));
+  Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
 
   result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
   result->data = data;
--- a/jbig2_arith.c
+++ b/jbig2_arith.c
@@ -180,8 +180,7 @@
 {
   Jbig2ArithState *result;
 
-  result = (Jbig2ArithState *)jbig2_alloc(ctx->allocator,
-	sizeof(Jbig2ArithState));
+  result = jbig2_new(ctx, Jbig2ArithState, 1);
 
   result->ws = ws;
 
--- a/jbig2_arith_iaid.c
+++ b/jbig2_arith_iaid.c
@@ -47,7 +47,7 @@
   int ctx_size = 1 << SBSYMCODELEN;
 
   result->SBSYMCODELEN = SBSYMCODELEN;
-  result->IAIDx = jbig2_alloc(ctx->allocator, ctx_size);
+  result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
   memset(result->IAIDx, 0, ctx_size);
 
   return result;
--- a/jbig2_generic.c
+++ b/jbig2_generic.c
@@ -831,7 +831,7 @@
   else
     {
       int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
-      GB_stats = jbig2_alloc(ctx->allocator, stats_size);
+      GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
       memset(GB_stats, 0, stats_size);
 
       ws = jbig2_word_stream_buf_new(ctx,
--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -75,11 +75,9 @@
   int i;
 
   /* allocate a new struct */
-  new = (Jbig2PatternDict *)jbig2_alloc(ctx->allocator,
-				sizeof(Jbig2PatternDict));
+  new = jbig2_new(ctx, Jbig2PatternDict, 1);
   if (new != NULL) {
-    new->patterns = (Jbig2Image **)jbig2_alloc(ctx->allocator,
-				N*sizeof(Jbig2Image*));
+    new->patterns = jbig2_new(ctx, Jbig2Image*, N);
     if (new->patterns == NULL) {
       jbig2_free(ctx->allocator, new);
       return NULL;
@@ -241,7 +239,7 @@
   if (!params.HDMMR) {
     /* allocate and zero arithmetic coding stats */
     int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
-    GB_stats = jbig2_alloc(ctx->allocator, stats_size);
+    GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
     memset(GB_stats, 0, stats_size);
   }
 
@@ -300,7 +298,7 @@
   params.HMMR = params.flags & 1;
   params.HTEMPLATE = (params.flags & 6) >> 1;
   params.HENABLESKIP = (params.flags & 8) >> 3;
-  params.op = (params.flags & 0x70) >> 4;
+  params.op = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
   params.HDEFPIXEL = (params.flags &0x80) >> 7;
   offset += 1;
 
@@ -346,7 +344,7 @@
   if (!params.HMMR) {
     /* allocate and zero arithmetic coding stats */
     int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
-    GB_stats = jbig2_alloc(ctx->allocator, stats_size);
+    GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
     memset(GB_stats, 0, stats_size);
   }
 
--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -61,8 +61,7 @@
 {
   Jbig2HuffmanState *result;
 
-  result = (Jbig2HuffmanState *)jbig2_alloc(ctx->allocator,
-	sizeof(Jbig2HuffmanState));
+  result = jbig2_new(ctx, Jbig2HuffmanState, 1);
 
   if (result != NULL) {
       result->offset = 0;
@@ -338,7 +337,7 @@
   int CURCODE;
   int CURTEMP;
 
-  LENCOUNT = jbig2_alloc(ctx->allocator, lencountsize);
+  LENCOUNT = jbig2_new(ctx, int, lencountsize);
   if (LENCOUNT == NULL) {
     jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
       "couldn't allocate storage for huffman histogram");
@@ -370,9 +369,9 @@
 	"constructing huffman table log size %d", log_table_size);
   max_j = 1 << log_table_size;
 
-  result = (Jbig2HuffmanTable *)jbig2_alloc(ctx->allocator, sizeof(Jbig2HuffmanTable));
+  result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
   result->log_table_size = log_table_size;
-  entries = (Jbig2HuffmanEntry *)jbig2_alloc(ctx->allocator, max_j * sizeof(Jbig2HuffmanEntry));
+  entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
   result->entries = entries;
 
   LENCOUNT[0] = 0;
--- a/jbig2_image.c
+++ b/jbig2_image.c
@@ -33,7 +33,7 @@
 	Jbig2Image	*image;
 	int		stride;
 
-	image = (Jbig2Image *)jbig2_alloc(ctx->allocator, sizeof(*image));
+	image = jbig2_new(ctx, Jbig2Image, 1);
 	if (image == NULL) {
 		jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
 			       "could not allocate image structure");
@@ -41,7 +41,7 @@
 	}
 
 	stride = ((width - 1) >> 3) + 1; /* generate a byte-aligned stride */
-	image->data = (uint8_t *)jbig2_alloc(ctx->allocator, stride*height);
+	image->data = jbig2_new(ctx, uint8_t, stride*height);
 	if (image->data == NULL) {
                 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
                     "could not allocate image data buffer! [%d bytes]\n", stride*height);
@@ -84,8 +84,7 @@
 {
 	if (width == image->width) {
 	    /* use the same stride, just change the length */
-	    image->data = jbig2_realloc(ctx->allocator,
-                image->data, image->stride*height);
+	    image->data = jbig2_renew(ctx, image->data, uint8_t, image->stride*height);
             if (image->data == NULL) {
                 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
                     "could not resize image buffer!");
--- a/jbig2_metadata.c
+++ b/jbig2_metadata.c
@@ -28,14 +28,14 @@
 /* metadata key,value list object */
 Jbig2Metadata *jbig2_metadata_new(Jbig2Ctx *ctx, Jbig2Encoding encoding)
 {
-    Jbig2Metadata *md = jbig2_alloc(ctx->allocator, sizeof(Jbig2Metadata));
+    Jbig2Metadata *md = jbig2_new(ctx, Jbig2Metadata, 1);
 
     if (md != NULL) {
         md->encoding = encoding;
         md->entries = 0;
         md->max_entries = 4;
-        md->keys = jbig2_alloc(ctx->allocator, md->max_entries*sizeof(char*));
-        md->values = jbig2_alloc(ctx->allocator, md->max_entries*sizeof(char*));
+        md->keys = jbig2_new(ctx, char*, md->max_entries);
+        md->values = jbig2_new(ctx, char*, md->max_entries);
         if (md->keys == NULL || md->values == NULL) {
             jbig2_metadata_free(ctx, md);
             md = NULL;
@@ -64,7 +64,7 @@
 
 static char *jbig2_strndup(Jbig2Ctx *ctx, const char *c, const int len)
 {
-    char *s = jbig2_alloc(ctx->allocator, len*sizeof(char));
+    char *s = jbig2_new(ctx, char, len);
     if (s == NULL) {
         jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
             "unable to duplicate comment string");
@@ -83,8 +83,8 @@
     /* grow the array if necessary */
     if (md->entries == md->max_entries) {
         md->max_entries >>= 2;
-        keys = jbig2_realloc(ctx->allocator, md->keys, md->max_entries);
-        values = jbig2_realloc(ctx->allocator, md->values, md->max_entries);
+        keys = jbig2_renew(ctx, md->keys, char*, md->max_entries);
+        values = jbig2_renew(ctx, md->values, char*, md->max_entries);
         if (keys == NULL || values == NULL) {
             jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
                 "unable to resize metadata structure");
--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -82,7 +82,7 @@
             index++;
             if (index >= ctx->max_page_index) {
                 /* grow the list */
-		ctx->pages = jbig2_realloc(ctx->allocator, ctx->pages,
+		ctx->pages = (Jbig2Page*)jbig2_realloc(ctx->allocator, ctx->pages,
 			(ctx->max_page_index <<= 2) * sizeof(Jbig2Page));
                 for (j=index; j < ctx->max_page_index; j++) {
                     ctx->pages[j].state = JBIG2_PAGE_FREE;
--- a/jbig2_priv.h
+++ b/jbig2_priv.h
@@ -16,7 +16,13 @@
 /* library internals */
 
 typedef uint8_t byte;
-typedef int bool;
+
+#define bool int
+
+#ifdef __cplusplus
+#define template template_C
+#define new new_C
+#endif
 
 #ifndef TRUE
 #define TRUE 1
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -377,8 +377,8 @@
        rules say to use the first one available, and not to
        reuse any intermediate result, so we simply clone it
        and free the original to keep track of this. */
-    params.reference = jbig2_image_clone(ctx, ref->result);
-    jbig2_image_release(ctx, ref->result);
+    params.reference = jbig2_image_clone(ctx, (Jbig2Image*)ref->result);
+    jbig2_image_release(ctx, (Jbig2Image*)ref->result);
     ref->result = NULL;
     jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
       "found reference bitmap in segment %d", ref->number);
@@ -409,7 +409,7 @@
           rsi.width, rsi.height);
 
     stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
-    GR_stats = jbig2_alloc(ctx->allocator, stats_size);
+    GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
     memset(GR_stats, 0, stats_size);
 
     ws = jbig2_word_stream_buf_new(ctx, segment_data + offset,
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -42,8 +42,7 @@
   if (buf_size < 11)
     return NULL;
 
-  result = (Jbig2Segment *)jbig2_alloc(ctx->allocator,
-					     sizeof(Jbig2Segment));
+  result = jbig2_new(ctx, Jbig2Segment, 1);
 
   /* 7.2.2 */
   result->number = jbig2_get_int32(buf);
@@ -83,7 +82,8 @@
     {
       int i;
 
-      referred_to_segments = jbig2_alloc(ctx->allocator, referred_to_segment_count * referred_to_segment_size * sizeof(uint32_t));
+      referred_to_segments = jbig2_new(ctx, uint32_t,
+        referred_to_segment_count * referred_to_segment_size);
 
       for (i = 0; i < referred_to_segment_count; i++) {
         referred_to_segments[i] =
@@ -135,16 +135,16 @@
     switch (segment->flags & 63) {
 	case 0:  /* symbol dictionary */
 	  if (segment->result != NULL)
-	    jbig2_sd_release(ctx, segment->result);
+	    jbig2_sd_release(ctx, (Jbig2SymbolDict*)segment->result);
 	  break;
 	case 4:  /* intermediate text region */
 	case 40: /* intermediate refinement region */
 	  if (segment->result != NULL)
-	    jbig2_image_release(ctx, segment->result);
+	    jbig2_image_release(ctx, (Jbig2Image*)segment->result);
 	  break;
 	case 62:
 	  if (segment->result != NULL)
-	    jbig2_metadata_free(ctx, segment->result);
+	    jbig2_metadata_free(ctx, (Jbig2Metadata*)segment->result);
 	  break;
 	default:
 	  /* anything else is probably an undefined pointer */
@@ -185,7 +185,7 @@
   info->x = jbig2_get_int32(segment_data + 8);
   info->y = jbig2_get_int32(segment_data + 12);
   info->flags = segment_data[16];
-  info->op = info->flags & 0x7;
+  info->op = (Jbig2ComposeOp)(info->flags & 0x7);
 }
 
 /* dispatch code for extension segment parsing */
--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -91,11 +91,9 @@
 {
    Jbig2SymbolDict *new = NULL;
 
-   new = (Jbig2SymbolDict *)jbig2_alloc(ctx->allocator,
-   				sizeof(Jbig2SymbolDict));
+   new = jbig2_new(ctx, Jbig2SymbolDict, 1);
    if (new != NULL) {
-     new->glyphs = (Jbig2Image **)jbig2_alloc(ctx->allocator,
-     				n_symbols*sizeof(Jbig2Image*));
+     new->glyphs = jbig2_new(ctx, Jbig2Image*, n_symbols);
      new->n_symbols = n_symbols;
    } else {
      return NULL;
@@ -158,7 +156,7 @@
     int n_dicts = jbig2_sd_count_referred(ctx, segment);
     int dindex = 0;
 
-    dicts = jbig2_alloc(ctx->allocator, sizeof(Jbig2SymbolDict *) * n_dicts);
+    dicts = jbig2_new(ctx, Jbig2SymbolDict*, n_dicts);
     for (index = 0; index < segment->referred_to_segment_count; index++) {
         rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
         if (rsegment && ((rsegment->flags & 63) == 0)) {
@@ -265,8 +263,7 @@
       SDHUFFRDX = jbig2_build_huffman_table(ctx,
 				&jbig2_huffman_params_O);
       if (!params->SDREFAGG) {
-	  SDNEWSYMWIDTHS = jbig2_alloc(ctx->allocator,
-		sizeof(*SDNEWSYMWIDTHS)*params->SDNUMNEWSYMS);
+	  SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
 	  if (SDNEWSYMWIDTHS == NULL) {
 	    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 		"could not allocate storage for symbol widths");
@@ -401,7 +398,7 @@
 			  /* First time through, we need to initialise the */
 			  /* various tables for Huffman or adaptive encoding */
 			  /* as well as the text region parameters structure */
-			  refagg_dicts = jbig2_alloc(ctx->allocator, sizeof(Jbig2SymbolDict *) * n_refagg_dicts);
+			  refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict*, n_refagg_dicts);
 		          if (refagg_dicts == NULL) {
 			      code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 			       "Out of memory allocating dictionary array");
@@ -420,7 +417,7 @@
 			      refagg_dicts[0]->glyphs[i] = jbig2_image_clone(ctx, params->SDINSYMS->glyphs[i]);
 		          }
 
-			  tparams = jbig2_alloc(ctx->allocator, sizeof(Jbig2TextRegionParams));
+			  tparams = jbig2_new(ctx, Jbig2TextRegionParams, 1);
 			  if (tparams == NULL) {
 			      code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 			      "Out of memory creating text region params");
@@ -898,11 +895,11 @@
   if (!params.SDHUFF) {
       int stats_size = params.SDTEMPLATE == 0 ? 65536 :
 	params.SDTEMPLATE == 1 ? 8192 : 1024;
-      GB_stats = jbig2_alloc(ctx->allocator, stats_size);
+      GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
       memset(GB_stats, 0, stats_size);
       if (params.SDREFAGG) {
 	stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
-	GR_stats = jbig2_alloc(ctx->allocator, stats_size);
+	GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
 	memset(GR_stats, 0, stats_size);
       }
   }
--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -119,7 +119,7 @@
 	}
 
 	/* decode the symbol id codelengths using the runlength table */
-	symcodelengths = jbig2_alloc(ctx->allocator, SBNUMSYMS*sizeof(Jbig2HuffmanLine));
+	symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
 	if (symcodelengths == NULL) {
 	  jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 	    "memory allocation failure reading symbol ID huffman table!");
@@ -437,9 +437,9 @@
     params.SBREFINE = flags & 0x0002;
     params.LOGSBSTRIPS = (flags & 0x000c) >> 2;
     params.SBSTRIPS = 1 << params.LOGSBSTRIPS;
-    params.REFCORNER = (flags & 0x0030) >> 4;
+    params.REFCORNER = (Jbig2RefCorner)((flags & 0x0030) >> 4);
     params.TRANSPOSED = flags & 0x0040;
-    params.SBCOMBOP = (flags & 0x0180) >> 7;
+    params.SBCOMBOP = (Jbig2ComposeOp)((flags & 0x0180) >> 7);
     params.SBDEFPIXEL = flags & 0x0200;
     /* SBDSOFFSET is a signed 5 bit integer */
     params.SBDSOFFSET = (flags & 0x7C00) >> 10;
@@ -680,7 +680,7 @@
     /* 7.4.3.2 (3) */
     if (!params.SBHUFF && params.SBREFINE) {
 	int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
-	GR_stats = jbig2_alloc(ctx->allocator, stats_size);
+	GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
 	memset(GR_stats, 0, stats_size);
     }
 
--- a/jbig2dec.c
+++ b/jbig2dec.c
@@ -67,7 +67,7 @@
 static void
 hash_init(jbig2dec_params_t *params)
 {
-    params->hash_ctx = malloc(sizeof(SHA1_CTX));
+    params->hash_ctx = (SHA1_CTX*)malloc(sizeof(SHA1_CTX));
     if (params->hash_ctx == NULL) {
         fprintf(stderr, "unable to allocate hash state\n");
         params->hash = 0;
@@ -235,7 +235,7 @@
 error_callback(void *error_callback_data, const char *buf, Jbig2Severity severity,
 	       int32_t seg_idx)
 {
-    const jbig2dec_params_t *params = error_callback_data;
+    const jbig2dec_params_t *params = (jbig2dec_params_t *)error_callback_data;
     char *type;
     char segment[22];
 
@@ -296,7 +296,7 @@
       len -= strlen(e);
 
     /* allocate enough space for the base + ext */
-    output_filename = malloc(len + strlen(extension) + 1);
+    output_filename = (char*)malloc(len + strlen(extension) + 1);
     if (output_filename == NULL) {
         fprintf(stderr, "couldn't allocate memory for output_filename\n");
         exit (1);
@@ -435,7 +435,7 @@
   /* any other number of arguments */
     return print_usage();
 
-  ctx = jbig2_ctx_new(NULL, f_page != NULL ? JBIG2_OPTIONS_EMBEDDED : 0,
+  ctx = jbig2_ctx_new(NULL, (Jbig2Options)(f_page != NULL ? JBIG2_OPTIONS_EMBEDDED : 0),
 		      NULL,
 		      error_callback, &params);