shithub: jbig2

Download patch

ref: 2d6d6e0c7b2c97e52a3363eae09841a17fe8e272
parent: f508fadb8bfad2296ff963203183ea25ec6d8080
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Tue Jun 4 12:51:02 EDT 2002

Add pbm import support and a simple 'pbm2png' implementation as a test harness for the image code. No makefile support--just didn't want to lose the code.


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

--- a/jbig2_image.h
+++ b/jbig2_image.h
@@ -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.2 2002/05/08 02:36:04 giles Exp $
+    $Id: jbig2_image.h,v 1.3 2002/06/04 16:51:02 giles Exp $
 */
 
 /*
@@ -21,9 +21,11 @@
 #ifndef _JBIG2_IMAGE_H
 #define _JBIG2_IMAGE_H
 
+#include <stdint.h>
+
 typedef struct _Jbig2Image {
-	int	width, height, stride;
-	uint32	*data;
+	int		width, height, stride;
+	uint32_t	*data;
 } Jbig2Image;
 
 Jbig2Image*	jbig2_image_new(int width, int height);
@@ -34,6 +36,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);
 
 #ifdef HAVE_LIBPNG
 int jbig2_image_write_png_file(Jbig2Image *image, char *filename);
--- 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.1 2002/05/08 02:36:04 giles Exp $
+    $Id: jbig2_image_pbm.c,v 1.2 2002/06/04 16:51:02 giles Exp $
 */
 
 #include <stdio.h>
@@ -61,6 +61,92 @@
 	return 0;
 }
 
+/* take an image from a file in pbm format */
+Jbig2Image *jbig2_image_read_pbm_file(char *filename)
+{
+    FILE *in;
+    Jbig2Image *image;
+    
+    if ((in = fopen(filename, "rb")) == NULL) {
+		fprintf(stderr, "unable to open '%s' for reading\n", filename);
+		return NULL;
+    }
+    
+    image = jbig2_image_read_pbm(in);
+    
+    return (image);
+}
+
+// FIXME: should handle multi-image files
+Jbig2Image *jbig2_image_read_pbm(FILE *in)
+{
+    int i, dim[2];
+    int stride, pbm_stride;
+    int done;
+    Jbig2Image *image;
+    char c,buf[32];
+    byte *data;
+    
+    // look for 'P4' magic
+    while ((c = fgetc(in)) != 'P') {
+        if (feof(in)) return NULL;
+    }
+    if ((c = fgetc(in)) != '4') {
+        fprintf(stderr, "not a binary pbm file.\n");
+        return NULL;
+    }
+    // read size. we must find two decimal numbers representing
+    // the image dimensions. done will index whether we're
+    // looking for the width of the height and i will be our
+    // array index for copying strings into our buffer
+    done = 0;
+    i = 0;
+    while (done < 2) {
+        c = fgetc(in);
+        // skip whitespace
+        if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue;
+        // skip comments
+        if (c == '#') {
+            while ((c = fgetc(in)) != '\n');
+            continue;
+        }
+        if (isdigit(c)) {
+            buf[i++] = c;
+            while (isdigit(buf[i++] = fgetc(in))) {
+                if (feof(in) || i >= 32) {
+                    fprintf(stderr, "pbm parsing error\n");
+                    return NULL;
+                }
+            }
+            buf[i] = '\0';
+            sscanf(buf, "%d", &dim[done]);
+            i = 0;
+            done++;
+        }
+    }
+    // allocate image structure
+    image = jbig2_image_new(dim[0], dim[1]);
+    if (image == NULL) {
+        fprintf(stderr, "could not allocate %dx%d image structure\n", dim[0], dim[1]);
+        return NULL;
+    }
+    // the pbm data is byte-aligned, and our image struct is word-aligned,
+    // so we have to index each line separately
+    pbm_stride = (dim[0] + 1) >> 3;
+    data = (byte *)image->data;
+    for (i = 0; i < dim[1]; i++) {
+        fread(data, sizeof(byte), pbm_stride, in);
+        if (feof(in)) {
+            fprintf(stderr, "unexpected end of pbm file.\n");
+            jbig2_image_free(image);
+            return NULL;
+        }
+        data += image->stride;
+    }
+    
+    // success
+    return image;
+}
 
 #ifdef TEST
 int main(int argc, char *argv[])
--- /dev/null
+++ b/pbm2png.c
@@ -1,0 +1,39 @@
+/*
+    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: pbm2png.c,v 1.1 2002/06/04 16:51:02 giles Exp $
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include "jbig2_image.h"
+
+int main(int argc, char *argv[])
+{
+	Jbig2Image *image;
+        int error;
+
+	image = jbig2_image_read_pbm_file(argv[1]);
+	if(image == NULL) {
+            fprintf(stderr, "error reading pbm file '%s'\n", argv[1]);
+            return 1;
+        } else {
+            fprintf(stderr, "converting %dx%d image to png format\n", image->width, image->height);
+        }
+        
+        error = jbig2_image_write_png_file(image, argv[2]);
+        if (error) {
+            fprintf(stderr, "error writing png file '%s' error %d\n", argv[2], error);
+	}
+
+	return (error);
+}