Coverage Report

Created: 2025-07-12 07:23

/src/poppler/goo/PNGWriter.cc
Line
Count
Source (jump to first uncovered line)
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 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
#ifdef 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 = (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
    /* libpng changed the png_set_iCCP() prototype in 1.5.0 */
79
#    if PNG_LIBPNG_VER < 10500
80
    png_charp icc_data_ptr = (png_charp)priv->icc_data;
81
#    else
82
0
    png_const_bytep icc_data_ptr = (png_const_bytep)priv->icc_data;
83
0
#    endif
84
85
0
    if (hDPI < 0 || vDPI < 0) {
86
0
        error(errInternal, -1, "PNGWriter::init: hDPI or vDPI values are invalid {0:f} {1:f}", hDPI, vDPI);
87
0
        return false;
88
0
    }
89
90
0
    const double png_res_x = hDPI / 0.0254;
91
0
    const double png_res_y = vDPI / 0.0254;
92
0
    if (png_res_x > std::numeric_limits<png_uint_32>::max() || png_res_y > std::numeric_limits<png_uint_32>::max()) {
93
0
        error(errInternal, -1, "PNGWriter::init: hDPI or vDPI values are invalid {0:f} {1:f}", hDPI, vDPI);
94
0
        return false;
95
0
    }
96
97
    /* initialize stuff */
98
0
    priv->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
99
0
    if (!priv->png_ptr) {
100
0
        error(errInternal, -1, "png_create_write_struct failed");
101
0
        return false;
102
0
    }
103
104
0
    priv->info_ptr = png_create_info_struct(priv->png_ptr);
105
0
    if (!priv->info_ptr) {
106
0
        error(errInternal, -1, "png_create_info_struct failed");
107
0
        return false;
108
0
    }
109
110
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
111
0
        error(errInternal, -1, "png_jmpbuf failed");
112
0
        return false;
113
0
    }
114
115
    /* write header */
116
0
    png_init_io(priv->png_ptr, f);
117
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
118
0
        error(errInternal, -1, "Error during writing header");
119
0
        return false;
120
0
    }
121
122
    // Set up the type of PNG image and the compression level
123
0
    png_set_compression_level(priv->png_ptr, Z_BEST_COMPRESSION);
124
125
    // Silence silly gcc
126
0
    png_byte bit_depth = -1;
127
0
    png_byte color_type = -1;
128
0
    switch (priv->format) {
129
0
    case RGB:
130
0
        bit_depth = 8;
131
0
        color_type = PNG_COLOR_TYPE_RGB;
132
0
        break;
133
0
    case RGB48:
134
0
        bit_depth = 16;
135
0
        color_type = PNG_COLOR_TYPE_RGB;
136
0
        break;
137
0
    case RGBA:
138
0
        bit_depth = 8;
139
0
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
140
0
        break;
141
0
    case GRAY:
142
0
        bit_depth = 8;
143
0
        color_type = PNG_COLOR_TYPE_GRAY;
144
0
        break;
145
0
    case MONOCHROME:
146
0
        bit_depth = 1;
147
0
        color_type = PNG_COLOR_TYPE_GRAY;
148
0
        break;
149
0
    }
150
0
    png_byte interlace_type = PNG_INTERLACE_NONE;
151
152
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);
153
154
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);
155
156
0
    if (priv->icc_data) {
157
0
        png_set_iCCP(priv->png_ptr, priv->info_ptr, priv->icc_name, PNG_COMPRESSION_TYPE_BASE, icc_data_ptr, priv->icc_data_size);
158
0
    } else if (priv->sRGB_profile) {
159
0
        png_set_sRGB(priv->png_ptr, priv->info_ptr, PNG_sRGB_INTENT_RELATIVE);
160
0
    }
161
162
0
    png_write_info(priv->png_ptr, priv->info_ptr);
163
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
164
0
        error(errInternal, -1, "error during writing png info bytes");
165
0
        return false;
166
0
    }
167
168
0
    return true;
169
0
}
170
171
bool PNGWriter::writePointers(unsigned char **rowPointers, int rowCount)
172
0
{
173
0
    png_write_image(priv->png_ptr, rowPointers);
174
    /* write bytes */
175
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
176
0
        error(errInternal, -1, "Error during writing bytes");
177
0
        return false;
178
0
    }
179
180
0
    return true;
181
0
}
182
183
bool PNGWriter::writeRow(unsigned char **row)
184
0
{
185
    // Write the row to the file
186
0
    png_write_rows(priv->png_ptr, row, 1);
187
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
188
0
        error(errInternal, -1, "error during png row write");
189
0
        return false;
190
0
    }
191
192
0
    return true;
193
0
}
194
195
bool PNGWriter::close()
196
0
{
197
    /* end write */
198
0
    png_write_end(priv->png_ptr, priv->info_ptr);
199
0
    if (setjmp(png_jmpbuf(priv->png_ptr))) {
200
0
        error(errInternal, -1, "Error during end of write");
201
0
        return false;
202
0
    }
203
204
0
    return true;
205
0
}
206
207
#endif