]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/jbig2dec/jbig2_image_pbm.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / jbig2dec / jbig2_image_pbm.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 <ctype.h>
28
29 #include "jbig2.h"
30 #include "jbig2_priv.h"
31 #include "jbig2_image.h"
32
33 /* take an image structure and write it to a file in pbm format */
34
35 int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
36 {
37     FILE *out;
38     int error;
39
40     if ((out = fopen(filename, "wb")) == NULL) {
41         fprintf(stderr, "unable to open '%s' for writing", filename);
42         return 1;
43     }
44
45     error = jbig2_image_write_pbm(image, out);
46
47     fclose(out);
48     return (error);
49 }
50
51 /* write out an image struct as a pbm stream to an open file pointer */
52
53 int jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
54 {
55         /* pbm header */
56         fprintf(out, "P4\n%d %d\n", image->width, image->height);
57
58         /* pbm format pads to a byte boundary, so we can
59            just write out the whole data buffer
60            NB: this assumes minimal stride for the width */
61         fwrite(image->data, 1, image->height*image->stride, out);
62
63         /* success */
64         return 0;
65 }
66
67 /* take an image from a file in pbm format */
68 Jbig2Image *jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
69 {
70     FILE *in;
71     Jbig2Image *image;
72
73     if ((in = fopen(filename, "rb")) == NULL) {
74                 fprintf(stderr, "unable to open '%s' for reading\n", filename);
75                 return NULL;
76     }
77
78     image = jbig2_image_read_pbm(ctx, in);
79
80     fclose(in);
81
82     return (image);
83 }
84
85 /* FIXME: should handle multi-image files */
86 Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
87 {
88     int i, dim[2];
89     int done;
90     Jbig2Image *image;
91     int c;
92     char buf[32];
93
94     /* look for 'P4' magic */
95     while ((c = fgetc(in)) != 'P') {
96         if (feof(in)) return NULL;
97     }
98     if ((c = fgetc(in)) != '4') {
99         fprintf(stderr, "not a binary pbm file.\n");
100         return NULL;
101     }
102     /* read size. we must find two decimal numbers representing
103        the image dimensions. 'done' will index whether we're
104        looking for the width or the height and 'i' will be our
105        array index for copying strings into our buffer */
106     done = 0;
107     i = 0;
108     while (done < 2) {
109         c = fgetc(in);
110         /* skip whitespace */
111         if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue;
112         /* skip comments */
113         if (c == '#') {
114             while ((c = fgetc(in)) != '\n');
115             continue;
116         }
117         /* report unexpected eof */
118         if (c == EOF) {
119            fprintf(stderr, "end-of-file parsing pbm header\n");
120            return NULL;
121         }
122         if (isdigit(c)) {
123             buf[i++] = c;
124             while (isdigit(c = fgetc(in))) {
125                 if (i >= 32) {
126                     fprintf(stderr, "pbm parsing error\n");
127                     return NULL;
128                 }
129                 buf[i++] = c;
130             }
131             buf[i] = '\0';
132             if (sscanf(buf, "%d", &dim[done]) != 1) {
133                 fprintf(stderr, "couldn't read pbm image dimensions\n");
134                 return NULL;
135             }
136             i = 0;
137             done++;
138         }
139     }
140     /* allocate image structure */
141     image = jbig2_image_new(ctx, dim[0], dim[1]);
142     if (image == NULL) {
143         fprintf(stderr, "could not allocate %dx%d image for pbm file\n", dim[0], dim[1]);
144         return NULL;
145     }
146     /* the pbm data is byte-aligned, so we can
147        do a simple block read */
148     fread(image->data, 1, image->height*image->stride, in);
149     if (feof(in)) {
150         fprintf(stderr, "unexpected end of pbm file.\n");
151         jbig2_image_release(ctx, image);
152         return NULL;
153     }
154     /* success */
155     return image;
156 }