1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
4 This software is provided AS-IS with no warranty, either express or
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
13 CA 94903, U.S.A., +1(415)492-9861, for further information.
31 #include "jbig2_priv.h"
32 #include "jbig2_image.h"
34 /* take an image structure and write it out in png format */
37 jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
41 check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
42 if (check != length) {
43 png_error(png_ptr, "Write Error");
48 jbig2_png_flush(png_structp png_ptr)
51 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
56 int jbig2_image_write_png_file(Jbig2Image *image, char *filename)
61 if ((out = fopen(filename, "wb")) == NULL) {
62 fprintf(stderr, "unable to open '%s' for writing\n", filename);
66 error = jbig2_image_write_png(image, out);
72 /* write out an image struct in png format to an open file pointer */
74 int jbig2_image_write_png(Jbig2Image *image, FILE *out)
81 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
84 fprintf(stderr, "unable to create png structure\n");
88 info = png_create_info_struct(png);
90 fprintf(stderr, "unable to create png info structure\n");
91 png_destroy_write_struct(&png, (png_infopp)NULL);
95 /* set/check error handling */
96 if (setjmp(png_jmpbuf(png))) {
97 /* we've returned here after an internal error */
98 fprintf(stderr, "internal error in libpng saving file\n");
99 png_destroy_write_struct(&png, &info);
103 /* png_init_io() doesn't work linking dynamically to libpng on win32
104 one has to either link statically or use callbacks because of runtime
106 /* png_init_io(png, out); */
107 png_set_write_fn(png, (png_voidp)out, jbig2_png_write_data,
110 /* now we fill out the info structure with our format data */
111 png_set_IHDR(png, info, image->width, image->height,
112 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
113 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
114 png_write_info(png, info);
116 /* png natively treates 0 as black. This will convert for us */
117 png_set_invert_mono(png);
119 /* write out each row in turn */
120 rowpointer = (png_bytep)image->data;
121 for(i = 0; i < image->height; i++) {
122 png_write_row(png, rowpointer);
123 rowpointer += image->stride;
126 /* finish and clean up */
127 png_write_end(png, info);
128 png_destroy_write_struct(&png, &info);