comparison mupdf-source/thirdparty/jbig2dec/jbig2_image_png.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 <stdlib.h>
27 #include <png.h>
28
29 #ifndef OLD_LIB_PNG
30 # if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 2
31 # define OLD_LIB_PNG 1
32 # else
33 # define OLD_LIB_PNG 0
34 # endif
35 #endif
36
37 #if OLD_LIB_PNG
38 #include <pngstruct.h>
39 #endif
40
41 #define CVT_PTR(ptr) (ptr)
42
43 #include "jbig2.h"
44 #include "jbig2_priv.h"
45 #include "jbig2_image.h"
46
47 /* take an image structure and write it out in png format */
48
49 static void
50 jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
51 {
52 png_size_t check;
53
54 #if OLD_LIB_PNG
55 png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
56 #else
57 png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
58 #endif
59
60 check = fwrite(data, 1, length, f);
61 if (check != length) {
62 png_error(png_ptr, "write error");
63 }
64 }
65
66 static void
67 jbig2_png_flush(png_structp png_ptr)
68 {
69 #if OLD_LIB_PNG
70 png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
71 #else
72 png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
73 #endif
74
75 if (f != NULL)
76 fflush(f);
77 }
78
79 /* write out an image struct in png format to an open file pointer */
80
81 int
82 jbig2_image_write_png(Jbig2Image *image, FILE *out)
83 {
84 uint32_t i;
85 png_structp png;
86 png_infop info;
87 png_bytep rowpointer;
88
89 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
90 if (png == NULL) {
91 fprintf(stderr, "unable to create png structure\n");
92 return 2;
93 }
94
95 info = png_create_info_struct(png);
96 if (info == NULL) {
97 fprintf(stderr, "unable to create png info structure\n");
98 png_destroy_write_struct(&png, (png_infopp) NULL);
99 return 3;
100 }
101
102 /* set/check error handling */
103 if (setjmp(png_jmpbuf(png))) {
104 /* we've returned here after an internal error */
105 fprintf(stderr, "internal error in libpng saving file\n");
106 png_destroy_write_struct(&png, &info);
107 return 4;
108 }
109
110 /* png_init_io() doesn't work linking dynamically to libpng on win32
111 one has to either link statically or use callbacks because of runtime
112 variations */
113 /* png_init_io(png, out); */
114 png_set_write_fn(png, (png_voidp) out, jbig2_png_write_data, jbig2_png_flush);
115
116 /* now we fill out the info structure with our format data */
117 png_set_IHDR(png, info, image->width, image->height, 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
118 png_write_info(png, info);
119
120 /* png natively treats 0 as black. This will convert for us */
121 png_set_invert_mono(png);
122
123 /* write out each row in turn */
124 rowpointer = (png_bytep) image->data;
125 for (i = 0; i < image->height; i++) {
126 png_write_row(png, rowpointer);
127 rowpointer += image->stride;
128 }
129
130 /* finish and clean up */
131 png_write_end(png, info);
132 png_destroy_write_struct(&png, &info);
133
134 return 0;
135 }
136
137 int
138 jbig2_image_write_png_file(Jbig2Image *image, char *filename)
139 {
140 FILE *out;
141 int code;
142
143 if ((out = fopen(filename, "wb")) == NULL) {
144 fprintf(stderr, "unable to open '%s' for writing\n", filename);
145 return 1;
146 }
147
148 code = jbig2_image_write_png(image, out);
149
150 fclose(out);
151 return (code);
152 }