shithub: jbig2

Download patch

ref: dcb620344060d47d77e620d23c0e3e6aa3838f6b
parent: f46505e1adb56bcac9bbf00ec8b264dc50963764
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon Jun 17 12:30:20 EDT 2002

Update the image routines to use the ctx structure for dynamic memory and remove the test framework.


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

--- a/jbig2_image.c
+++ b/jbig2_image.c
@@ -8,33 +8,37 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_image.c,v 1.4 2002/06/15 16:02:54 giles Exp $
+    $Id: jbig2_image.c,v 1.5 2002/06/17 16:30:20 giles Exp $
 */
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 #include "jbig2.h"
+#include "jbig2_priv.h"
 #include "jbig2_image.h"
 
 
 /* allocate a Jbig2Image structure and its associated bitmap */
-Jbig2Image* jbig2_image_new(int width, int height)
+Jbig2Image* jbig2_image_new(Jbig2Ctx *ctx, int width, int height)
 {
 	Jbig2Image	*image;
 	int		stride;
 	
-	image = malloc(sizeof(*image));
+	image = (Jbig2Image *)jbig2_alloc(ctx->allocator, sizeof(*image));
 	if (image == NULL) {
-		fprintf(stderr, "jbig2dec error: could not allocate image buffer!\n");
+		jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+			       "could not allocate image structure");
 		return NULL;
 	}
 	
 	stride = (((width - 1) >> 5) + 1) << 2;	/* generate a word-aligned stride */
-	image->data = malloc(stride*height);
+	image->data = (uint32_t *)jbig2_alloc(ctx->allocator, (stride*height));
 	if (image->data == NULL) {
-		fprintf(stderr, "jbig2dec error: could not allocate image data buffer!\n");
-		free(image);
+                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+                    "could not allocate image data buffer! [%d bytes]\n", stride*height);
+		jbig2_free(ctx->allocator, image);
 		return NULL;
 	}
 	
@@ -47,8 +51,8 @@
 
 
 /* free a Jbig2Image structure and its associated memory */
-void jbig2_image_free(Jbig2Image *image)
+void jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
 {
-	free (image->data);
-	free (image);
+	jbig2_free(ctx->allocator, image->data);
+	jbig2_free(ctx->allocator, image);
 }
--- a/jbig2_image.h
+++ b/jbig2_image.h
@@ -8,9 +8,13 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_image.h,v 1.3 2002/06/04 16:51:02 giles Exp $
+    $Id: jbig2_image.h,v 1.4 2002/06/17 16:30:20 giles Exp $
 */
 
+
+#ifndef _JBIG2_IMAGE_H
+#define _JBIG2_IMAGE_H
+
 /*
    this is the general image structure used by the jbig2dec library
    images are 1 bpp, packed into word-aligned rows. stride gives
@@ -18,18 +22,13 @@
    the size of the image area in pixels.
 */
 
-#ifndef _JBIG2_IMAGE_H
-#define _JBIG2_IMAGE_H
-
-#include <stdint.h>
-
 typedef struct _Jbig2Image {
 	int		width, height, stride;
 	uint32_t	*data;
 } Jbig2Image;
 
-Jbig2Image*	jbig2_image_new(int width, int height);
-void		jbig2_image_free(Jbig2Image *image);
+Jbig2Image*	jbig2_image_new(Jbig2Ctx *ctx, int width, int height);
+void		jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image);
 
 /* routines for dumping the image data in various formats */
 /* FIXME: should these be in the client instead? */
@@ -36,8 +35,8 @@
 
 int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename);
 int jbig2_image_write_pbm(Jbig2Image *image, FILE *out);
-Jbig2Image *jbig2_image_read_pbm_file(char *filename);
-Jbig2Image *jbig2_image_read_pbm(FILE *in);
+Jbig2Image *jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename);
+Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in);
 
 #ifdef HAVE_LIBPNG
 int jbig2_image_write_png_file(Jbig2Image *image, char *filename);
@@ -44,4 +43,4 @@
 int jbig2_image_write_png(Jbig2Image *image, FILE *out);
 #endif
 
-#endif /* _JBIG2_IMAGE_H */
\ No newline at end of file
+#endif /* _JBIG2_IMAGE_H */
--- a/jbig2_image_pbm.c
+++ b/jbig2_image_pbm.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_image_pbm.c,v 1.3 2002/06/15 16:02:54 giles Exp $
+    $Id: jbig2_image_pbm.c,v 1.4 2002/06/17 16:30:20 giles Exp $
 */
 
 #include <stdio.h>
@@ -63,7 +63,7 @@
 }
 
 /* take an image from a file in pbm format */
-Jbig2Image *jbig2_image_read_pbm_file(char *filename)
+Jbig2Image *jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
 {
     FILE *in;
     Jbig2Image *image;
@@ -73,13 +73,13 @@
 		return NULL;
     }
     
-    image = jbig2_image_read_pbm(in);
+    image = jbig2_image_read_pbm(ctx, in);
     
     return (image);
 }
 
 // FIXME: should handle multi-image files
-Jbig2Image *jbig2_image_read_pbm(FILE *in)
+Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
 {
     int i, dim[2];
     int stride, pbm_stride;
@@ -126,7 +126,7 @@
         }
     }
     // allocate image structure
-    image = jbig2_image_new(dim[0], dim[1]);
+    image = jbig2_image_new(ctx, dim[0], dim[1]);
     if (image == NULL) {
         fprintf(stderr, "could not allocate %dx%d image structure\n", dim[0], dim[1]);
         return NULL;
@@ -139,7 +139,7 @@
         fread(data, sizeof(byte), pbm_stride, in);
         if (feof(in)) {
             fprintf(stderr, "unexpected end of pbm file.\n");
-            jbig2_image_free(image);
+            jbig2_image_free(ctx, image);
             return NULL;
         }
         data += image->stride;
@@ -148,30 +148,3 @@
     // success
     return image;
 }
-
-#ifdef TEST
-int main(int argc, char *argv[])
-{
-	int	i,j;
-	Jbig2Image	*image;
-	uint32		*data;
-	char		*filename = "test.pbm";
-	
-	image = jbig2_image_new(384,51);
-	if (image == NULL) {
-		fprintf(stderr, "failed to create jbig2 image structure!\n");
-		exit(1);
-	}
-	
-	fprintf(stderr, "creating checkerboard test image '%s'\n", filename);
-	data = image->data;
-	for (j = 0; j < image->height; j++) {
-		for (i = 0; i < image->stride >> 2; i++) {
-			*data++ = ((j & 16) >> 4) ? 0x0000FFFF: 0xFFFF0000;
-		}
-	}
-	
-	return jbig2_image_write_pbm_file(image, filename);
-}
-
-#endif /* TEST */