-
Notifications
You must be signed in to change notification settings - Fork 20
/
PNGWrite.hpp
145 lines (121 loc) · 5.16 KB
/
PNGWrite.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// This file is part of the `Selene` library.
// Copyright 2017 Michael Hofmann (https://github.com/kmhofmann).
// Distributed under MIT license. See accompanying LICENSE file in the top-level directory.
#ifndef SELENE_IMG_PNG_WRITE_HPP
#define SELENE_IMG_PNG_WRITE_HPP
/// @file
#if defined(SELENE_WITH_LIBPNG)
#include <selene/base/Allocators.hpp>
#include <selene/base/Assert.hpp>
#include <selene/base/MessageLog.hpp>
#include <selene/base/Utils.hpp>
#include <selene/img/BoundingBox.hpp>
#include <selene/img/ImageData.hpp>
#include <selene/img/RowPointers.hpp>
#include <selene/img/detail/Util.hpp>
#include <selene/io/FileWriter.hpp>
#include <selene/io/VectorWriter.hpp>
#include <array>
#include <csetjmp>
#include <cstdio>
#include <memory>
namespace sln {
// Forward declarations
struct PNGCompressionOptions;
class PNGCompressionObject;
namespace detail {
class PNGCompressionCycle;
void set_destination(PNGCompressionObject&, FileWriter&);
void set_destination(PNGCompressionObject&, VectorWriter&);
} // namespace detail
/** \brief PNG compression options.
*
* For more detailed information, consult the libpng manual (libpng-manual.txt) provided with every libpng source
* distribution, or available here: http://www.libpng.org/pub/png/libpng-manual.txt
*/
struct PNGCompressionOptions
{
int compression_level; ///< Compression level; may take values from 0 (no compression) to 9 ("maximal" compression).
bool interlaced; ///< If true, write PNG image as interlaced.
bool set_bgr; ///< If true, convert BGR (supplied) to RGB (written).
bool invert_alpha_channel; ///< If true, invert values in alpha channel (e.g. 0 -> 255).
bool invert_monochrome; ///< If true, invert grayscale or grayscale_alpha image values.
/** \brief Constructor, setting the respective JPEG compression options.
*
* @param compression_level_ Compression level; may take values from 0 (no compression) to 9 ("maximal" compression).
* Defaults to the default zlib compression level.
* @param interlaced_ If true, write PNG image as interlaced.
* @param set_bgr_ If true, convert BGR (supplied) to RGB (written).
* @param invert_alpha_channel_ If true, invert values in alpha channel (e.g. 0 -> 255).
* @param invert_monochrome_ If true, invert grayscale or grayscale_alpha image values.
*/
explicit PNGCompressionOptions(int compression_level_ = -1,
bool interlaced_ = false,
bool set_bgr_ = false,
bool invert_alpha_channel_ = false,
bool invert_monochrome_ = false)
: compression_level(compression_level_)
, interlaced(interlaced_)
, set_bgr(set_bgr_)
, invert_alpha_channel(invert_alpha_channel_)
, invert_monochrome(invert_monochrome_)
{
}
};
/** \brief Opaque PNG compression object, holding internal state.
*
*/
class PNGCompressionObject
{
public:
/// \cond INTERNAL
PNGCompressionObject();
~PNGCompressionObject();
bool valid() const;
bool error_state() const;
const MessageLog& message_log() const;
bool set_image_info(int width, int height, int nr_channels, int bit_depth, bool interlaced, PixelFormat pixel_format);
bool set_compression_parameters(int, bool);
/// \endcond
private:
struct Impl;
std::unique_ptr<Impl> impl_;
friend class detail::PNGCompressionCycle;
friend void detail::set_destination(PNGCompressionObject&, FileWriter&);
friend void detail::set_destination(PNGCompressionObject&, VectorWriter&);
};
/** \brief Writes a PNG image data stream, given the supplied uncompressed image data.
*
* @tparam SinkType Type of the output sink. Can be FileWriter or VectorWriter.
* @param img_data The image data to be written.
* @param sink Output sink instance.
* @param options The compression options.
* @param messages Optional pointer to the message log. If provided, warning and error messages will be output there.
* @return True, if the write operation was successful; false otherwise.
*/
template <typename SinkType>
bool write_png(const ImageData& img_data,
SinkType& sink,
PNGCompressionOptions options = PNGCompressionOptions(),
MessageLog* messages = nullptr);
/** \brief Writes a PNG image data stream, given the supplied uncompressed image data.
*
* This function overload enables re-use of a PNGCompressionObject instance.
*
* @tparam SinkType Type of the output sink. Can be FileWriter or VectorWriter.
* @param img_data The image data to be written.
* @param obj A PNGCompressionObject instance.
* @param sink Output sink instance.
* @param options The compression options.
* @param messages Optional pointer to the message log. If provided, warning and error messages will be output there.
* @return True, if the write operation was successful; false otherwise.
*/
template <typename SinkType>
bool write_png(const ImageData& img_data,
PNGCompressionObject& obj,
SinkType& sink,
PNGCompressionOptions options = PNGCompressionOptions(),
MessageLog* messages = nullptr);
} // namespace sln
#endif // defined(SELENE_WITH_LIBPNG)
#endif // SELENE_IMG_PNG_WRITE_HPP