shithub: jbig2

Download patch

ref: 42ead910c9d9d4e3f6dd69e91be16f5de42ffef7
parent: 0459e440350325b04aaa0bd1f70120ce1066716b
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Tue May 7 22:36:04 EDT 2002

Rename the png output source files, and add pbm output support. Also allow
writing to a FILE* instead of a filename so we can send things to stdout, etc.


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

--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-# $Id: Makefile.am,v 1.1 2002/05/08 00:59:20 giles Exp $
+# $Id: Makefile.am,v 1.2 2002/05/08 02:36:04 giles Exp $
 ## process this file with automake to generate Makefile.in
 
 lib_LIBRARIES = libjbig2dec.a
@@ -7,7 +7,7 @@
 	jbig2_arith.c jbig2_arith_int.c jbig2_huffman.c \
 	jbig2_symbol_dict.c \
 	jbig2_generic.c jbig2_mmr.c \
-	jbig2_image.c
+	jbig2_image.c jbig2_image_pbm.c
 
 bin_PROGRAMS = jbig2dec
 #noinst_PROGRAMS = test_huffman test_arith test_png
--- a/jbig2_image.c
+++ b/jbig2_image.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.c,v 1.2 2001/08/13 20:31:59 giles Exp $
+    $Id: jbig2_image.c,v 1.3 2002/05/08 02:36:04 giles Exp $
 */
 
 #include <stdio.h>
@@ -22,7 +22,7 @@
 Jbig2Image* jbig2_image_new(int width, int height)
 {
 	Jbig2Image	*image;
-	int			stride;
+	int		stride;
 	
 	image = malloc(sizeof(*image));
 	if (image == NULL) {
--- a/jbig2_image.h
+++ b/jbig2_image.h
@@ -1,7 +1,7 @@
 /*
     jbig2dec
     
-    Copyright (c) 2001 artofcode LLC.
+    Copyright (c) 2001-2002 artofcode LLC.
     
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2_image.h,v 1.1 2001/08/10 23:29:28 giles Exp $
+    $Id: jbig2_image.h,v 1.2 2002/05/08 02:36:04 giles Exp $
 */
 
 /*
@@ -18,6 +18,9 @@
    the size of the image area in pixels.
 */
 
+#ifndef _JBIG2_IMAGE_H
+#define _JBIG2_IMAGE_H
+
 typedef struct _Jbig2Image {
 	int	width, height, stride;
 	uint32	*data;
@@ -26,3 +29,15 @@
 Jbig2Image*	jbig2_image_new(int width, int height);
 void		jbig2_image_free(Jbig2Image *image);
 
+/* routines for dumping the image data in various formats */
+/* FIXME: should these be in the client instead? */
+
+int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename);
+int jbig2_image_write_pbm(Jbig2Image *image, FILE *out);
+
+#ifdef HAVE_LIBPNG
+int jbig2_image_write_png_file(Jbig2Image *image, char *filename);
+int jbig2_image_write_png(Jbig2Image *image, FILE *out);
+#endif
+
+#endif /* _JBIG2_IMAGE_H */
\ No newline at end of file
--- /dev/null
+++ b/jbig2_image_pbm.c
@@ -1,0 +1,90 @@
+/*
+    jbig2dec
+    
+    Copyright (c) 2002 artofcode LLC.
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    $Id: jbig2_image_pbm.c,v 1.1 2002/05/08 02:36:04 giles Exp $
+*/
+
+#include <stdio.h>
+
+#include "jbig2dec.h"
+#include "jbig2_image.h"
+
+/* take an image structure and write it to a file in pbm format */
+
+int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
+{
+    FILE *out;
+    int	error;
+    
+    if ((out = fopen(filename, "wb")) == NULL) {
+		fprintf(stderr, "unable to open '%s' for writing\n", filename);
+		return 1;
+    }
+    
+    error = jbig2_image_write_pbm(image, out);
+    
+    fclose(out);
+    return (error);
+}
+
+/* write out an image struct as a pbm stream to an open file pointer */
+
+int jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
+{
+        int i, short_stride, extra_bits;
+        byte *p = (byte *)image->data;
+        
+        // pbm header
+        fprintf(out, "P4\n%d %d\n", image->width, image->height);
+        
+        // pbm format pads only to the next byte boundary
+        // so we figure our output byte stride and fixup
+        short_stride = image->width >> 3;
+        extra_bits = image->width - (short_stride << 3);
+        fprintf(stderr, "creating %dx%d pbm image, short_stride %d, extra_bits %d\n",
+            image->width, image->height, short_stride, extra_bits);
+        // write out each row
+        for(i = 0; i < image->height; i++) {
+            fwrite(p, sizeof(byte), short_stride, out);
+            if (extra_bits) fwrite(p + short_stride, sizeof(byte), 1, out);
+            p += image->stride;
+        }
+        
+        /* success */
+	return 0;
+}
+
+
+#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 */
--- /dev/null
+++ b/jbig2_image_png.c
@@ -1,0 +1,121 @@
+/*
+    jbig2dec
+    
+    Copyright (c) 2002 artofcode LLC.
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    $Id: jbig2_image_png.c,v 1.1 2002/05/08 02:36:04 giles Exp $
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <png.h>
+
+#include "jbig2dec.h"
+#include "jbig2_image.h"
+
+/* take an image structure and write it out in png format */
+
+int jbig2_image_write_png_file(Jbig2Image *image, char *filename)
+{
+    FILE *out;
+    int	error;
+    
+    if ((out = fopen(filename, "wb")) == NULL) {
+		fprintf(stderr, "unable to open '%s' for writing\n", filename);
+		return 1;
+    }
+    
+    error = jbig2_image_write_png(image, out);
+    
+    fclose(out);
+    return (error);
+}
+
+/* write out an image struct in png format to an open file pointer */
+
+int jbig2_image_write_png(Jbig2Image *image, FILE *out)
+{
+	int		i;
+	png_structp	png;
+	png_infop	info;
+	png_bytep	rowpointer;
+	
+	png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+		NULL, NULL, NULL);
+	if (png == NULL) {
+		fprintf(stderr, "unable to create png structure\n");
+		return 2;
+	}
+	
+	info = png_create_info_struct(png);
+	if (info == NULL) {
+            fprintf(stderr, "unable to create png info structure\n");
+            png_destroy_write_struct(&png,  (png_infopp)NULL);
+            return 3;
+	}
+
+	/* set/check error handling */
+	if (setjmp(png_jmpbuf(png))) {
+		/* we've returned here after an internal error */
+		fprintf(stderr, "internal error in libpng saving file\n");
+		png_destroy_write_struct(&png, &info);
+		return 4;
+	}
+
+	png_init_io(png, out);
+   
+	/* now we fill out the info structure with our format data */
+	png_set_IHDR(png, info, image->width, image->height,
+		1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
+		PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+	png_write_info(png, info);
+
+	/* png natively treates 0 as black. This will convert for us */
+	png_set_invert_mono(png);
+	
+	/* write out each row in turn */
+	rowpointer = (png_bytep)image->data;
+	for(i = 0; i < image->height; i++) {
+		png_write_row(png, rowpointer);
+		rowpointer += image->stride;
+	}
+	
+	/* finish and clean up */
+	png_write_end(png, info);
+	png_destroy_write_struct(&png, &info);
+		
+	return 0;
+}
+
+
+#ifdef TEST
+int main(int argc, char *argv[])
+{
+	int	i,j;
+	Jbig2Image	*image;
+	uint32		*data;
+	char		*filename = "test.png";
+	
+	image = jbig2_image_new(400,400);
+	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_png(image, filename);
+}
+
+#endif /* TEST */
--- a/png_image.c
+++ /dev/null
@@ -1,114 +1,0 @@
-/*
-    jbig2dec
-    
-    Copyright (c) 2001 artofcode LLC.
-    
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    $Id: png_image.c,v 1.3 2001/08/13 20:31:59 giles Exp $
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <png.h>
-
-#include "jbig2dec.h"
-#include "jbig2_image.h"
-
-/* take and image structure and write it out in png format */
-
-int jbig2_image_write_png(Jbig2Image *image, char *filename)
-{
-	FILE	*out;
-	int		i;
-	png_structp		png;
-	png_infop		info;
-	png_bytep		rowpointer;
-	
-	if ((out = fopen(filename, "wb")) == NULL) {
-		fprintf(stderr, "unable to open '%s' for writing\n", filename);
-		return 1;
-	}
-	
-	png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
-		NULL, NULL, NULL);
-	if (png == NULL) {
-		fprintf(stderr, "unable to create png structure\n");
-		fclose(out);
-		return 2;
-	}
-	
-	info = png_create_info_struct(png);
-	if (info == NULL) {
-	  fprintf(stderr, "unable to create png info structure\n");
-      fclose(out);
-      png_destroy_write_struct(&png,  (png_infopp)NULL);
-      return 3;
-	}
-
-	/* set/check error handling */
-	if (setjmp(png_jmpbuf(png))) {
-		/* we've returned here after an internal error */
-		fprintf(stderr, "internal error in libpng saving file\n");
-		fclose(out);
-		png_destroy_write_struct(&png, &info);
-		return 4;
-	}
-
-	png_init_io(png, out);
-   
-	/* now we fill out the info structure with our format data */
-	png_set_IHDR(png, info, image->width, image->height,
-		1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
-		PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-	png_write_info(png, info);
-
-	/* png natively treates 0 as black. This will convert for us */
-	png_set_invert_mono(png);
-	
-	/* write out each row in turn */
-	rowpointer = (png_bytep)image->data;
-	for(i = 0; i < image->height; i++) {
-		png_write_row(png, rowpointer);
-		rowpointer += image->stride;
-	}
-	
-	/* finish and clean up */
-	png_write_end(png, info);
-	png_destroy_write_struct(&png, &info);
-	
-	fclose(out);
-	
-	return 0;
-}
-
-
-#ifdef TEST
-int main(int argc, char *argv[])
-{
-	int	i,j;
-	Jbig2Image	*image;
-	uint32		*data;
-	char		*filename = "test.png";
-	
-	image = jbig2_image_new(400,400);
-	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_png(image, filename);
-}
-
-#endif /* TEST */
\ No newline at end of file
--- a/png_image.h
+++ /dev/null
@@ -1,20 +1,0 @@
-/*
-    jbig2dec
-    
-    Copyright (c) 2001 artofcode LLC.
-    
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    $Id: png_image.h,v 1.1 2001/08/10 23:29:28 giles Exp $
-*/
-
-#ifndef _JBIG2_PNG_IMAGE_H
-#define _JBIG2_PNG_IMAGE_H
-
-/* take and image structure and write it out in png format */
-int jbig2_image_write_png(Jbig2Image *image, char *filename);
-
-#endif	/* _JBIG2_PNG_IMAGE_H */