]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/jbig2dec/jbig2_image_png.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / jbig2dec / jbig2_image_png.c
1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6
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.
10
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.
14 */
15
16 /*
17     jbig2dec
18 */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include "os_types.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <png.h>
29
30 #include "jbig2.h"
31 #include "jbig2_priv.h"
32 #include "jbig2_image.h"
33
34 /* take an image structure and write it out in png format */
35
36 static void
37 jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
38 {
39     png_size_t check;
40
41     check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
42     if (check != length) {
43       png_error(png_ptr, "Write Error");
44     }
45 }
46
47 static void
48 jbig2_png_flush(png_structp png_ptr)
49 {
50     png_FILE_p io_ptr;
51     io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
52     if (io_ptr != NULL)
53         fflush(io_ptr);
54 }
55
56 int jbig2_image_write_png_file(Jbig2Image *image, char *filename)
57 {
58     FILE *out;
59     int error;
60
61     if ((out = fopen(filename, "wb")) == NULL) {
62                 fprintf(stderr, "unable to open '%s' for writing\n", filename);
63                 return 1;
64     }
65
66     error = jbig2_image_write_png(image, out);
67
68     fclose(out);
69     return (error);
70 }
71
72 /* write out an image struct in png format to an open file pointer */
73
74 int jbig2_image_write_png(Jbig2Image *image, FILE *out)
75 {
76         int             i;
77         png_structp     png;
78         png_infop       info;
79         png_bytep       rowpointer;
80
81         png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
82                 NULL, NULL, NULL);
83         if (png == NULL) {
84                 fprintf(stderr, "unable to create png structure\n");
85                 return 2;
86         }
87
88         info = png_create_info_struct(png);
89         if (info == NULL) {
90             fprintf(stderr, "unable to create png info structure\n");
91             png_destroy_write_struct(&png,  (png_infopp)NULL);
92             return 3;
93         }
94
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);
100                 return 4;
101         }
102
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
105            variations */
106         /* png_init_io(png, out); */
107         png_set_write_fn(png, (png_voidp)out, jbig2_png_write_data,
108             jbig2_png_flush);
109
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);
115
116         /* png natively treates 0 as black. This will convert for us */
117         png_set_invert_mono(png);
118
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;
124         }
125
126         /* finish and clean up */
127         png_write_end(png, info);
128         png_destroy_write_struct(&png, &info);
129
130         return 0;
131 }