ref: 7181b7a84184562ccf72e34c11f75b158b58e61b
parent: 15fbf3afe60b17b958fd38ed902ddae67554f804
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Fri Aug 10 19:29:28 EDT 2001
initial image buffer and png output routines git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@32 ded80894-8fb9-0310-811b-c03f3676ab4d
--- /dev/null
+++ b/jbig2_image.c
@@ -1,0 +1,54 @@
+/*
+ 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: jbig2_image.c,v 1.1 2001/08/10 23:29:28 giles Exp $
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "jbig2dec.h"
+#include "jbig2_image.h"
+
+
+/* allocate a Jbig2Image structure and its associated bitmap */
+Jbig2Image* jbig2_image_new(int width, int height)
+{
+ Jbig2Image *image;
+ int stride;
+
+ image = malloc(sizeof(*image));
+ if (image == NULL) {
+ fprintf(stderr, "jbig2dec error: could not allocate image buffer!\n");
+ return NULL;
+ }
+
+ stride = ((width - 1) >> 5) + 1; /* generate a word-aligned stride */
+ image->data = malloc(stride*height);
+ if (image->data == NULL) {
+ fprintf(stderr, "jbig2dec error: could not allocate image data buffer!\n");
+ free(image);
+ return NULL;
+ }
+
+ image->width = width;
+ image->height = height;
+ image->stride = stride;
+
+ return image;
+}
+
+
+/* free a Jbig2Image structure and its associated memory */
+void jbig2_image_free(Jbig2Image *image)
+{
+ free (image->data);
+ free (image);
+}
\ No newline at end of file
--- /dev/null
+++ b/jbig2_image.h
@@ -1,0 +1,28 @@
+/*
+ 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: jbig2_image.h,v 1.1 2001/08/10 23:29:28 giles Exp $
+*/
+
+/*
+ this is the general image structure used by the jbig2dec library
+ images are 1 bpp, packed into word-aligned rows. stride gives
+ the word offset to the next row, while width and height define
+ the size of the image area in pixels.
+*/
+
+typedef struct _Jbig2Image {
+ int width, height, stride;
+ uint32 *data;
+} Jbig2Image;
+
+Jbig2Image* jbig2_image_new(int width, int height);
+void jbig2_image_free(Jbig2Image *image);
+
--- a/makefile
+++ b/makefile
@@ -1,10 +1,10 @@
-CFLAGS=-Wall -g
+CFLAGS=-Wall -g -I/usr/local/include
-APPS=jbig2dec test_arith test_huffman
+APPS=jbig2dec test_arith test_huffman test_png
all: $(APPS)
-jbig2dec: jbig2dec.o jbig2_huffman.o jbig2_arith.o
+jbig2dec: jbig2dec.o jbig2_huffman.o jbig2_arith.o jbig2_image.o
test_huffman: jbig2_huffman.c
gcc $(CFLAGS) -DTEST jbig2_huffman.c -o test_huffman
@@ -12,5 +12,8 @@
test_arith: jbig2_arith.c
gcc $(CFLAGS) -DTEST -DDEBUG jbig2_arith.c -o test_arith
+test_png: png_image.c
+ gcc $(CFLAGS) -DTEST -DDEBUG png_image.c -lpng -lz -o test_png
+
clean:
- rm $(APPS) *.o
\ No newline at end of file
+ rm $(APPS) *.o
--- /dev/null
+++ b/png_image.c
@@ -1,0 +1,111 @@
+/*
+ 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.1 2001/08/10 23:29:28 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_ptr 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_ptr);
+ if (info == NULL) {
+ fprintf(stderr, "unable to create png info structure\n");
+ fclose(fp);
+ 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(fp);
+ 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 = 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(256,128);
+ if (image == NULL) {
+ fprintf(stderr, "failed to create jbig2 image structure!\n");
+ exit(1);
+ }
+
+ data = image->data;
+ for (j = 0; j < stride; j++) {
+ for (i = 0; i < image->height; i++) {
+ data++ = (i+j % 2) * 0xF0F0F0F0;
+ }
+ }
+}
+
+#endif /* TEST */
\ No newline at end of file
--- /dev/null
+++ b/png_image.h
@@ -1,0 +1,20 @@
+/*
+ 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 */