Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8619 | maxcodehac | 1 | /* |
2 | jbig2dec |
||
3 | |||
4 | Copyright (C) 2009 Artifex Software, Inc. |
||
5 | |||
6 | This software is distributed under license and may not |
||
7 | be copied, modified or distributed except as expressly |
||
8 | authorized under the terms of the license contained in |
||
9 | the file LICENSE in this distribution. |
||
10 | |||
11 | For further licensing information refer to http://artifex.com/ or |
||
12 | contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, |
||
13 | San Rafael, CA 94903, U.S.A., +1(415)492-9861. |
||
14 | */ |
||
15 | |||
16 | #ifdef HAVE_CONFIG_H |
||
17 | #include "config.h" |
||
18 | #endif |
||
19 | #include "os_types.h" |
||
20 | |||
21 | #include |
||
22 | #include |
||
23 | |||
24 | #include "jbig2.h" |
||
25 | #include "jbig2_image.h" |
||
26 | |||
27 | /* take an image structure and write it to a file in pbm format */ |
||
28 | |||
29 | int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename) |
||
30 | { |
||
31 | FILE *out; |
||
32 | int error; |
||
33 | |||
34 | if ((out = fopen(filename, "wb")) == NULL) { |
||
35 | fprintf(stderr, "unable to open '%s' for writing", filename); |
||
36 | return 1; |
||
37 | } |
||
38 | |||
39 | error = jbig2_image_write_pbm(image, out); |
||
40 | |||
41 | fclose(out); |
||
42 | return (error); |
||
43 | } |
||
44 | |||
45 | /* write out an image struct as a pbm stream to an open file pointer */ |
||
46 | |||
47 | int jbig2_image_write_pbm(Jbig2Image *image, FILE *out) |
||
48 | { |
||
49 | /* pbm header */ |
||
50 | fprintf(out, "P4\n%d %d\n", image->width, image->height); |
||
51 | |||
52 | /* pbm format pads to a byte boundary, so we can |
||
53 | just write out the whole data buffer |
||
54 | NB: this assumes minimal stride for the width */ |
||
55 | fwrite(image->data, 1, image->height*image->stride, out); |
||
56 | |||
57 | /* success */ |
||
58 | return 0; |
||
59 | } |
||
60 | |||
61 | /* take an image from a file in pbm format */ |
||
62 | Jbig2Image *jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename) |
||
63 | { |
||
64 | FILE *in; |
||
65 | Jbig2Image *image; |
||
66 | |||
67 | if ((in = fopen(filename, "rb")) == NULL) { |
||
68 | fprintf(stderr, "unable to open '%s' for reading\n", filename); |
||
69 | return NULL; |
||
70 | } |
||
71 | |||
72 | image = jbig2_image_read_pbm(ctx, in); |
||
73 | |||
74 | fclose(in); |
||
75 | |||
76 | return (image); |
||
77 | } |
||
78 | |||
79 | /* FIXME: should handle multi-image files */ |
||
80 | Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in) |
||
81 | { |
||
82 | int i, dim[2]; |
||
83 | int done; |
||
84 | Jbig2Image *image; |
||
85 | int c; |
||
86 | char buf[32]; |
||
87 | |||
88 | /* look for 'P4' magic */ |
||
89 | while ((c = fgetc(in)) != 'P') { |
||
90 | if (feof(in)) return NULL; |
||
91 | } |
||
92 | if ((c = fgetc(in)) != '4') { |
||
93 | fprintf(stderr, "not a binary pbm file.\n"); |
||
94 | return NULL; |
||
95 | } |
||
96 | /* read size. we must find two decimal numbers representing |
||
97 | the image dimensions. 'done' will index whether we're |
||
98 | looking for the width or the height and 'i' will be our |
||
99 | array index for copying strings into our buffer */ |
||
100 | done = 0; |
||
101 | i = 0; |
||
102 | while (done < 2) { |
||
103 | c = fgetc(in); |
||
104 | /* skip whitespace */ |
||
105 | if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue; |
||
106 | /* skip comments */ |
||
107 | if (c == '#') { |
||
108 | while ((c = fgetc(in)) != '\n'); |
||
109 | continue; |
||
110 | } |
||
111 | /* report unexpected eof */ |
||
112 | if (c == EOF) { |
||
113 | fprintf(stderr, "end-of-file parsing pbm header\n"); |
||
114 | return NULL; |
||
115 | } |
||
116 | if (isdigit(c)) { |
||
117 | buf[i++] = c; |
||
118 | while (isdigit(c = fgetc(in))) { |
||
119 | if (i >= 32) { |
||
120 | fprintf(stderr, "pbm parsing error\n"); |
||
121 | return NULL; |
||
122 | } |
||
123 | buf[i++] = c; |
||
124 | } |
||
125 | buf[i] = '\0'; |
||
126 | if (sscanf(buf, "%d", &dim[done]) != 1) { |
||
127 | fprintf(stderr, "couldn't read pbm image dimensions\n"); |
||
128 | return NULL; |
||
129 | } |
||
130 | i = 0; |
||
131 | done++; |
||
132 | } |
||
133 | } |
||
134 | /* allocate image structure */ |
||
135 | image = jbig2_image_new(ctx, dim[0], dim[1]); |
||
136 | if (image == NULL) { |
||
137 | fprintf(stderr, "could not allocate %dx%d image for pbm file\n", dim[0], dim[1]); |
||
138 | return NULL; |
||
139 | } |
||
140 | /* the pbm data is byte-aligned, so we can |
||
141 | do a simple block read */ |
||
142 | fread(image->data, 1, image->height*image->stride, in); |
||
143 | if (feof(in)) { |
||
144 | fprintf(stderr, "unexpected end of pbm file.\n"); |
||
145 | jbig2_image_release(ctx, image); |
||
146 | return NULL; |
||
147 | } |
||
148 | /* success */ |
||
149 | return image; |
||
150 | }> |