comparison mupdf-source/thirdparty/jbig2dec/jbig2_image_pbm.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* Copyright (C) 2001-2023 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., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /*
17 jbig2dec
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include "os_types.h"
24
25 #include <stdio.h>
26 #include <ctype.h>
27
28 #include "jbig2.h"
29 #include "jbig2_priv.h"
30 #include "jbig2_image.h"
31 #include "jbig2_image_rw.h"
32
33 /* take an image structure and write it to a file in pbm format */
34
35 int
36 jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
37 {
38 FILE *out;
39 int code;
40
41 if ((out = fopen(filename, "wb")) == NULL) {
42 fprintf(stderr, "unable to open '%s' for writing", filename);
43 return 1;
44 }
45
46 code = jbig2_image_write_pbm(image, out);
47
48 fclose(out);
49 return (code);
50 }
51
52 /* write out an image struct as a pbm stream to an open file pointer */
53
54 int
55 jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
56 {
57 /* pbm header */
58 fprintf(out, "P4\n%d %d\n", image->width, image->height);
59
60 /* pbm format pads to a byte boundary, so we can
61 just write out the whole data buffer
62 NB: this assumes minimal stride for the width */
63 fwrite(image->data, 1, image->height * image->stride, out);
64
65 /* success */
66 return 0;
67 }
68
69 /* take an image from a file in pbm format */
70 Jbig2Image *
71 jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
72 {
73 FILE *in;
74 Jbig2Image *image;
75
76 if ((in = fopen(filename, "rb")) == NULL) {
77 fprintf(stderr, "unable to open '%s' for reading\n", filename);
78 return NULL;
79 }
80
81 image = jbig2_image_read_pbm(ctx, in);
82
83 fclose(in);
84
85 return (image);
86 }
87
88 /* FIXME: should handle multi-image files */
89 Jbig2Image *
90 jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
91 {
92 int i, dim[2];
93 int done;
94 Jbig2Image *image;
95 int c;
96 char buf[32];
97
98 /* look for 'P4' magic */
99 while ((c = fgetc(in)) != 'P') {
100 if (feof(in))
101 return NULL;
102 }
103 if ((c = fgetc(in)) != '4') {
104 fprintf(stderr, "not a binary pbm file.\n");
105 return NULL;
106 }
107 /* read size. we must find two decimal numbers representing
108 the image dimensions. 'done' will index whether we're
109 looking for the width or the height and 'i' will be our
110 array index for copying strings into our buffer */
111 done = 0;
112 i = 0;
113 while (done < 2) {
114 c = fgetc(in);
115 /* skip whitespace */
116 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
117 continue;
118 /* skip comments */
119 if (c == '#') {
120 while ((c = fgetc(in)) != '\n');
121 continue;
122 }
123 /* report unexpected eof */
124 if (c == EOF) {
125 fprintf(stderr, "end-of-file parsing pbm header\n");
126 return NULL;
127 }
128 if (isdigit(c)) {
129 buf[i++] = c;
130 while (isdigit(c = fgetc(in))) {
131 if (i >= 32) {
132 fprintf(stderr, "pbm parsing error\n");
133 return NULL;
134 }
135 buf[i++] = c;
136 }
137 buf[i] = '\0';
138 if (sscanf(buf, "%d", &dim[done]) != 1) {
139 fprintf(stderr, "failed to read pbm image dimensions\n");
140 return NULL;
141 }
142 i = 0;
143 done++;
144 }
145 }
146 /* allocate image structure */
147 image = jbig2_image_new(ctx, dim[0], dim[1]);
148 if (image == NULL) {
149 fprintf(stderr, "failed to allocate %dx%d image for pbm file\n", dim[0], dim[1]);
150 return NULL;
151 }
152 /* the pbm data is byte-aligned, so we can
153 do a simple block read */
154 (void)fread(image->data, 1, image->height * image->stride, in);
155 if (feof(in)) {
156 fprintf(stderr, "unexpected end of pbm file.\n");
157 jbig2_image_release(ctx, image);
158 return NULL;
159 }
160 /* success */
161 return image;
162 }