/src/poppler/goo/PNGWriter.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // PNGWriter.cc |
4 | | // |
5 | | // This file is licensed under the GPLv2 or later |
6 | | // |
7 | | // Copyright (C) 2009 Warren Toomey <wkt@tuhs.org> |
8 | | // Copyright (C) 2009 Shen Liang <shenzhuxi@gmail.com> |
9 | | // Copyright (C) 2009, 2011-2023, 2025, 2026 Albert Astals Cid <aacid@kde.org> |
10 | | // Copyright (C) 2009 Stefan Thomas <thomas@eload24.com> |
11 | | // Copyright (C) 2010, 2011, 2013, 2017 Adrian Johnson <ajohnson@redneon.com> |
12 | | // Copyright (C) 2011 Thomas Klausner <wiz@danbala.tuwien.ac.at> |
13 | | // Copyright (C) 2012 Pino Toscano <pino@kde.org> |
14 | | // |
15 | | //======================================================================== |
16 | | |
17 | | #include "PNGWriter.h" |
18 | | |
19 | | #if ENABLE_LIBPNG |
20 | | |
21 | | # include <zlib.h> |
22 | | # include <cstdlib> |
23 | | # include <cstring> |
24 | | |
25 | | # include "poppler/Error.h" |
26 | | # include "goo/gmem.h" |
27 | | |
28 | | # include <png.h> |
29 | | |
30 | | struct PNGWriterPrivate |
31 | | { |
32 | 0 | explicit PNGWriterPrivate(PNGWriter::Format f) : format(f) { } |
33 | | |
34 | | PNGWriter::Format format; |
35 | | png_structp png_ptr = nullptr; |
36 | | png_infop info_ptr = nullptr; |
37 | | unsigned char *icc_data = nullptr; |
38 | | int icc_data_size = 0; |
39 | | char *icc_name = nullptr; |
40 | | bool sRGB_profile = false; |
41 | | |
42 | | PNGWriterPrivate(const PNGWriterPrivate &) = delete; |
43 | | PNGWriterPrivate &operator=(const PNGWriterPrivate &) = delete; |
44 | | }; |
45 | | |
46 | | PNGWriter::PNGWriter(Format formatA) |
47 | 0 | { |
48 | 0 | priv = new PNGWriterPrivate(formatA); |
49 | 0 | } |
50 | | |
51 | | PNGWriter::~PNGWriter() |
52 | 0 | { |
53 | | /* cleanup heap allocation */ |
54 | 0 | png_destroy_write_struct(&priv->png_ptr, &priv->info_ptr); |
55 | 0 | if (priv->icc_data) { |
56 | 0 | gfree(priv->icc_data); |
57 | 0 | free(priv->icc_name); |
58 | 0 | } |
59 | |
|
60 | 0 | delete priv; |
61 | 0 | } |
62 | | |
63 | | void PNGWriter::setICCProfile(const char *name, unsigned char *data, int size) |
64 | 0 | { |
65 | 0 | priv->icc_data = static_cast<unsigned char *>(gmalloc(size)); |
66 | 0 | memcpy(priv->icc_data, data, size); |
67 | 0 | priv->icc_data_size = size; |
68 | 0 | priv->icc_name = strdup(name); |
69 | 0 | } |
70 | | |
71 | | void PNGWriter::setSRGBProfile() |
72 | 0 | { |
73 | 0 | priv->sRGB_profile = true; |
74 | 0 | } |
75 | | |
76 | | bool PNGWriter::init(FILE *f, int width, int height, double hDPI, double vDPI) |
77 | 0 | { |
78 | 0 | const auto *icc_data_ptr = const_cast<png_const_bytep>(priv->icc_data); |
79 | |
|
80 | 0 | if (hDPI < 0 || vDPI < 0) { |
81 | 0 | error(errInternal, -1, "PNGWriter::init: hDPI or vDPI values are invalid {0:f} {1:f}", hDPI, vDPI); |
82 | 0 | return false; |
83 | 0 | } |
84 | | |
85 | 0 | const double png_res_x = hDPI / 0.0254; |
86 | 0 | const double png_res_y = vDPI / 0.0254; |
87 | 0 | if (png_res_x > std::numeric_limits<png_uint_32>::max() || png_res_y > std::numeric_limits<png_uint_32>::max()) { |
88 | 0 | error(errInternal, -1, "PNGWriter::init: hDPI or vDPI values are invalid {0:f} {1:f}", hDPI, vDPI); |
89 | 0 | return false; |
90 | 0 | } |
91 | | |
92 | | /* initialize stuff */ |
93 | 0 | priv->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
94 | 0 | if (!priv->png_ptr) { |
95 | 0 | error(errInternal, -1, "png_create_write_struct failed"); |
96 | 0 | return false; |
97 | 0 | } |
98 | | |
99 | 0 | priv->info_ptr = png_create_info_struct(priv->png_ptr); |
100 | 0 | if (!priv->info_ptr) { |
101 | 0 | error(errInternal, -1, "png_create_info_struct failed"); |
102 | 0 | return false; |
103 | 0 | } |
104 | | |
105 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
106 | 0 | error(errInternal, -1, "png_jmpbuf failed"); |
107 | 0 | return false; |
108 | 0 | } |
109 | | |
110 | | /* write header */ |
111 | 0 | png_init_io(priv->png_ptr, f); |
112 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
113 | 0 | error(errInternal, -1, "Error during writing header"); |
114 | 0 | return false; |
115 | 0 | } |
116 | | |
117 | | // Set up the type of PNG image and the compression level |
118 | 0 | png_set_compression_level(priv->png_ptr, Z_BEST_COMPRESSION); |
119 | | |
120 | | // Silence silly gcc |
121 | 0 | png_byte bit_depth = -1; |
122 | 0 | png_byte color_type = -1; |
123 | 0 | switch (priv->format) { |
124 | 0 | case RGB: |
125 | 0 | bit_depth = 8; |
126 | 0 | color_type = PNG_COLOR_TYPE_RGB; |
127 | 0 | break; |
128 | 0 | case RGB48: |
129 | 0 | bit_depth = 16; |
130 | 0 | color_type = PNG_COLOR_TYPE_RGB; |
131 | 0 | break; |
132 | 0 | case RGBA: |
133 | 0 | bit_depth = 8; |
134 | 0 | color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
135 | 0 | break; |
136 | 0 | case GRAY: |
137 | 0 | bit_depth = 8; |
138 | 0 | color_type = PNG_COLOR_TYPE_GRAY; |
139 | 0 | break; |
140 | 0 | case MONOCHROME: |
141 | 0 | bit_depth = 1; |
142 | 0 | color_type = PNG_COLOR_TYPE_GRAY; |
143 | 0 | break; |
144 | 0 | } |
145 | 0 | png_byte interlace_type = PNG_INTERLACE_NONE; |
146 | |
|
147 | 0 | png_set_IHDR(priv->png_ptr, priv->info_ptr, width, height, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
148 | |
|
149 | 0 | png_set_pHYs(priv->png_ptr, priv->info_ptr, static_cast<png_uint_32>(png_res_x), static_cast<png_uint_32>(png_res_y), PNG_RESOLUTION_METER); |
150 | |
|
151 | 0 | if (priv->icc_data) { |
152 | 0 | png_set_iCCP(priv->png_ptr, priv->info_ptr, priv->icc_name, PNG_COMPRESSION_TYPE_BASE, icc_data_ptr, priv->icc_data_size); |
153 | 0 | } else if (priv->sRGB_profile) { |
154 | 0 | png_set_sRGB(priv->png_ptr, priv->info_ptr, PNG_sRGB_INTENT_RELATIVE); |
155 | 0 | } |
156 | |
|
157 | 0 | png_write_info(priv->png_ptr, priv->info_ptr); |
158 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
159 | 0 | error(errInternal, -1, "error during writing png info bytes"); |
160 | 0 | return false; |
161 | 0 | } |
162 | | |
163 | 0 | return true; |
164 | 0 | } |
165 | | |
166 | | bool PNGWriter::writePointers(unsigned char **rowPointers, int /*rowCount*/) |
167 | 0 | { |
168 | 0 | png_write_image(priv->png_ptr, rowPointers); |
169 | | /* write bytes */ |
170 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
171 | 0 | error(errInternal, -1, "Error during writing bytes"); |
172 | 0 | return false; |
173 | 0 | } |
174 | | |
175 | 0 | return true; |
176 | 0 | } |
177 | | |
178 | | bool PNGWriter::writeRow(unsigned char **row) |
179 | 0 | { |
180 | | // Write the row to the file |
181 | 0 | png_write_rows(priv->png_ptr, row, 1); |
182 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
183 | 0 | error(errInternal, -1, "error during png row write"); |
184 | 0 | return false; |
185 | 0 | } |
186 | | |
187 | 0 | return true; |
188 | 0 | } |
189 | | |
190 | | bool PNGWriter::close() |
191 | 0 | { |
192 | | /* end write */ |
193 | 0 | png_write_end(priv->png_ptr, priv->info_ptr); |
194 | 0 | if (setjmp(png_jmpbuf(priv->png_ptr))) { |
195 | 0 | error(errInternal, -1, "Error during end of write"); |
196 | 0 | return false; |
197 | 0 | } |
198 | | |
199 | 0 | return true; |
200 | 0 | } |
201 | | |
202 | | #endif |