Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
cairo-utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
6 * Authors:
7 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
8 *
9 * Copyright (C) 2010 Authors
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#ifndef SEEN_INKSCAPE_DISPLAY_CAIRO_UTILS_H
14#define SEEN_INKSCAPE_DISPLAY_CAIRO_UTILS_H
15
16#include <2geom/forward.h>
17#include <cairomm/cairomm.h>
18#include "style.h"
19
20typedef struct _GdkPixbuf GdkPixbuf;
21
22void ink_cairo_pixbuf_cleanup(unsigned char *, void *);
23
24namespace Inkscape {
25namespace Colors {
26class Color;
27}
28
31class Pixbuf {
32public:
38
39 explicit Pixbuf(cairo_surface_t *s);
40 explicit Pixbuf(GdkPixbuf *pb);
41 Pixbuf(Inkscape::Pixbuf const &other);
42 ~Pixbuf();
43
44 Pixbuf *cropTo(const Geom::IntRect &area) const;
45
46 GdkPixbuf *getPixbufRaw(bool convert_format = true);
47 GdkPixbuf *getPixbufRaw() const;
48
51 Cairo::RefPtr<Cairo::Surface> getSurface();
52
53 int width() const;
54 int height() const;
55 int rowstride() const;
56 guchar const *pixels() const;
57 guchar *pixels();
58 void markDirty();
59
60 bool hasMimeData() const;
61 guchar const *getMimeData(gsize &len, std::string &mimetype) const;
62 std::string const &originalPath() const { return _path; }
63 time_t modificationTime() const { return _mod_time; }
64
67 static void ensure_pixbuf(GdkPixbuf *pb);
68 static void ensure_argb32(GdkPixbuf *pb);
69
70 static Pixbuf *create_from_data_uri(gchar const *uri, double svgdpi = 0);
71 static Pixbuf *create_from_file(std::string const &fn, double svgddpi = 0);
72 static Pixbuf *create_from_buffer(std::string const &, double svgddpi = 0, std::string const &fn = "");
73
74 private:
75 static Pixbuf *create_from_buffer(gchar *&&, gsize, double svgddpi = 0, std::string const &fn = "");
78
81 void _forceAlpha();
82 void _setMimeData(guchar *data, gsize len, Glib::ustring const &format);
83
86 time_t _mod_time;
87 std::string _path;
90};
91
92} // namespace Inkscape
93
98
99cairo_pattern_t *ink_cairo_pattern_create(Inkscape::Colors::Color const &color, double opacity = 1.0);
100void ink_cairo_pattern_add_color_stop(cairo_pattern_t *ptn, double offset, Inkscape::Colors::Color const &color, double opacity = 1.0);
101void ink_cairo_set_source_color(Cairo::RefPtr<Cairo::Context> ctx, Inkscape::Colors::Color const &color, double opacity = 1.0);
102void ink_cairo_set_source_color(cairo_t *ctx, Inkscape::Colors::Color const &c, double opacity = 1.0);
103void ink_cairo_set_source_rgba32(Cairo::RefPtr<Cairo::Context> ctx, guint32 rgba);
104void ink_cairo_set_source_rgba32(Cairo::Context &ctx, guint32 rgba);
106void ink_cairo_transform(cairo_t *ct, Geom::Affine const &m);
109void ink_cairo_pattern_set_dither(cairo_pattern_t *pattern, bool enabled);
110
111void ink_matrix_to_2geom(Geom::Affine &, cairo_matrix_t const &);
112Geom::Affine ink_matrix_to_2geom(cairo_matrix_t const &cairo_matrix);
113void ink_matrix_to_cairo(cairo_matrix_t &, Geom::Affine const &);
114cairo_operator_t ink_css_blend_to_cairo_operator(SPBlendMode blend_mode);
115SPBlendMode ink_cairo_operator_to_css_blend(cairo_operator_t cairo_operator);
117Cairo::RefPtr<Cairo::ImageSurface> ink_cairo_surface_copy(Cairo::RefPtr<Cairo::ImageSurface> surface);
127
128double srgb_to_linear( const double c );
131
132Cairo::RefPtr<Cairo::Pattern> ink_cairo_pattern_create_slanting_stripes(uint32_t color);
133cairo_pattern_t *ink_cairo_pattern_create_checkerboard(guint32 rgba = 0xC4C4C4FF, bool use_alpha = false);
134// draw drop shadow around the 'rect' with given 'size' and 'color'; shadow extends to the right and bottom of rect
135void ink_cairo_draw_drop_shadow(const Cairo::RefPtr<Cairo::Context> &ctx, const Geom::Rect& rect, double size, guint32 color, double color_alpha);
136
138void convert_pixels_pixbuf_to_argb32(guchar *data, int w, int h, int rs);
139void convert_pixels_argb32_to_pixbuf(guchar *data, int w, int h, int rs, guint32 bgcolor=0);
140
141G_GNUC_CONST guint32 argb32_from_pixbuf(guint32 in);
142G_GNUC_CONST guint32 pixbuf_from_argb32(guint32 in, guint32 bgcolor=0);
143const guchar* pixbuf_to_png(guchar const**rows, guchar* px, int nrows, int ncols, int stride, int color_type, int bit_depth);
144
146G_GNUC_CONST guint32 argb32_from_rgba(guint32 in);
148G_GNUC_CONST guint32 rgba_from_argb32(guint32 in);
149
150
151G_GNUC_CONST inline guint32
152premul_alpha(const guint32 color, const guint32 alpha)
153{
154 const guint32 temp = alpha * color + 128;
155 return (temp + (temp >> 8)) >> 8;
156}
157G_GNUC_CONST inline guint32
158unpremul_alpha(const guint32 color, const guint32 alpha)
159{
160 if (color >= alpha)
161 return 0xff;
162 return (255 * color + alpha/2) / alpha;
163}
164
165// TODO: move those to 2Geom
166void feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width);
167void feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv);
168
169std::optional<Geom::PathVector> extract_pathvector_from_cairo(cairo_t *ct);
170
171#define EXTRACT_ARGB32(px,a,r,g,b) \
172 guint32 a, r, g, b; \
173 a = ((px) & 0xff000000) >> 24; \
174 r = ((px) & 0x00ff0000) >> 16; \
175 g = ((px) & 0x0000ff00) >> 8; \
176 b = ((px) & 0x000000ff);
177
178#define ASSEMBLE_ARGB32(px,a,r,g,b) \
179 guint32 px = (a << 24) | (r << 16) | (g << 8) | b;
180
181inline double srgb_to_linear( const double c ) {
182 if( c < 0.04045 ) {
183 return c / 12.92;
184 } else {
185 return pow( (c+0.055)/1.055, 2.4 );
186 }
187}
188
189
190namespace Inkscape {
191
192namespace Display
193{
194
195inline void ExtractARGB32(guint32 px, guint32 &a, guint32 &r, guint32 &g, guint &b)
196{
197 a = ((px) & 0xff000000) >> 24;
198 r = ((px) & 0x00ff0000) >> 16;
199 g = ((px) & 0x0000ff00) >> 8;
200 b = ((px) & 0x000000ff);
201}
202
203inline void ExtractRGB32(guint32 px, guint32 &r, guint32 &g, guint &b)
204{
205 r = ((px) & 0x00ff0000) >> 16;
206 g = ((px) & 0x0000ff00) >> 8;
207 b = ((px) & 0x000000ff);
208}
209
211{
212 return (a << 24) | (r << 16) | (g << 8) | b;
213}
214
215} // namespace Display
216
217} // namespace Inkscape
218
219#endif // SEEN_INKSCAPE_DISPLAY_CAIRO_UTILS_H
220
221/*
222 Local Variables:
223 mode:c++
224 c-file-style:"stroustrup"
225 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
226 indent-tabs-mode:nil
227 fill-column:99
228 End:
229*/
230// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
uint32_t Color
cairo_pattern_t * ink_cairo_pattern_create(Inkscape::Colors::Color const &color, double opacity=1.0)
SPColorInterpolation get_cairo_surface_ci(cairo_surface_t *surface)
void ink_cairo_draw_drop_shadow(const Cairo::RefPtr< Cairo::Context > &ctx, const Geom::Rect &rect, double size, guint32 color, double color_alpha)
Draw drop shadow around the 'rect' with given 'size' and 'color'; shadow extends to the right and bot...
SPBlendMode ink_cairo_operator_to_css_blend(cairo_operator_t cairo_operator)
void ink_cairo_set_source_rgba32(Cairo::RefPtr< Cairo::Context > ctx, guint32 rgba)
void copy_cairo_surface_ci(cairo_surface_t *in, cairo_surface_t *out)
void convert_pixels_pixbuf_to_argb32(guchar *data, int w, int h, int rs)
Convert pixel data from GdkPixbuf format to ARGB.
G_GNUC_CONST guint32 premul_alpha(const guint32 color, const guint32 alpha)
Colors::Color ink_cairo_surface_average_color(cairo_surface_t *surface, cairo_surface_t *mask=nullptr)
Get the average color from the given surface.
void ink_matrix_to_2geom(Geom::Affine &, cairo_matrix_t const &)
G_GNUC_CONST guint32 rgba_from_argb32(guint32 in)
Convert a pixel in 0xAARRGGBB format to 0xRRGGBBAA format.
int ink_cairo_surface_linear_to_srgb(cairo_surface_t *surface)
Cairo::RefPtr< Cairo::Pattern > ink_cairo_pattern_create_slanting_stripes(uint32_t color)
cairo_surface_t * ink_cairo_extract_alpha(cairo_surface_t *s)
Extract the alpha channel into a new surface.
int ink_cairo_surface_srgb_to_linear(cairo_surface_t *surface)
cairo_surface_t * ink_cairo_surface_create_same_size(cairo_surface_t *s, cairo_content_t c)
cairo_operator_t ink_css_blend_to_cairo_operator(SPBlendMode blend_mode)
double srgb_to_linear(const double c)
cairo_surface_t * ink_cairo_surface_create_identical(cairo_surface_t *s)
Create a surface that differs only in pixel content.
G_GNUC_CONST guint32 argb32_from_rgba(guint32 in)
Convert a pixel in 0xRRGGBBAA format to Cairo ARGB32 format.
cairo_pattern_t * ink_cairo_pattern_create_checkerboard(guint32 rgba=0xC4C4C4FF, bool use_alpha=false)
void ink_cairo_transform(cairo_t *ct, Geom::Affine const &m)
cairo_surface_t * ink_cairo_surface_copy(cairo_surface_t *s)
Create an exact copy of a surface.
void set_cairo_surface_ci(cairo_surface_t *surface, SPColorInterpolation cif)
Set the color_interpolation_value for a Cairo surface.
void ink_cairo_pattern_set_matrix(cairo_pattern_t *cp, Geom::Affine const &m)
G_GNUC_CONST guint32 unpremul_alpha(const guint32 color, const guint32 alpha)
void ink_cairo_pixbuf_cleanup(unsigned char *, void *)
void feed_pathvector_to_cairo(cairo_t *ct, Geom::PathVector const &pathv, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
Feeds path-creating calls to the cairo context translating them from the PathVector,...
void ink_cairo_set_source_color(Cairo::RefPtr< Cairo::Context > ctx, Inkscape::Colors::Color const &color, double opacity=1.0)
The following functions interact between Inkscape color model, and cairo surface rendering.
void ink_cairo_pattern_set_dither(cairo_pattern_t *pattern, bool enabled)
int ink_cairo_surface_get_width(cairo_surface_t *surface)
Return width in pixels.
std::optional< Geom::PathVector > extract_pathvector_from_cairo(cairo_t *ct)
void ink_cairo_surface_blit(cairo_surface_t *src, cairo_surface_t *dest)
int ink_cairo_surface_get_height(cairo_surface_t *surface)
Return height in pixels.
void ink_cairo_pattern_add_color_stop(cairo_pattern_t *ptn, double offset, Inkscape::Colors::Color const &color, double opacity=1.0)
void convert_pixels_argb32_to_pixbuf(guchar *data, int w, int h, int rs, guint32 bgcolor=0)
Convert pixel data from ARGB to GdkPixbuf format.
void ink_cairo_set_hairline(cairo_t *ct)
G_GNUC_CONST guint32 argb32_from_pixbuf(guint32 in)
guint32 ink_cairo_pattern_get_argb32(cairo_pattern_t *pattern)
const guchar * pixbuf_to_png(guchar const **rows, guchar *px, int nrows, int ncols, int stride, int color_type, int bit_depth)
Converts a pixbuf to a PNG data structure.
void convert_cairo_surface_ci(cairo_surface_t *surface, SPColorInterpolation cif)
G_GNUC_CONST guint32 pixbuf_from_argb32(guint32 in, guint32 bgcolor=0)
Convert one pixel from ARGB to GdkPixbuf format.
struct _GdkPixbuf GdkPixbuf
Definition cairo-utils.h:20
void ink_matrix_to_cairo(cairo_matrix_t &, Geom::Affine const &)
cairo_surface_t * ink_cairo_surface_create_output(cairo_surface_t *image, cairo_surface_t *bg)
GdkPixbuf * ink_pixbuf_create_from_cairo_surface(cairo_surface_t *s)
Converts the Cairo surface to a GdkPixbuf pixel format, without allocating extra memory.
Cairo::RefPtr< Cairo::ImageSurface > surface
Definition canvas.cpp:137
3x3 matrix representing an affine transformation.
Definition affine.h:70
Axis aligned, non-empty, generic rectangle.
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Sequence of subpaths.
Definition pathvector.h:122
Axis aligned, non-empty rectangle.
Definition rect.h:92
Class to hold image data for raster images.
Definition cairo-utils.h:31
guchar const * getMimeData(gsize &len, std::string &mimetype) const
Retrieves the original compressed data for the surface, if any.
static GdkPixbuf * apply_embedded_orientation(GdkPixbuf *buf)
Pixbuf * cropTo(const Geom::IntRect &area) const
Create a new Pixbuf with the image cropped to the given area.
std::string const & originalPath() const
Definition cairo-utils.h:62
std::string _path
Definition cairo-utils.h:87
void _ensurePixelsPixbuf()
PixelFormat _pixel_format
Definition cairo-utils.h:88
int rowstride() const
guchar const * pixels() const
Cairo::RefPtr< Cairo::Surface > getSurface()
bool hasMimeData() const
int width() const
GdkPixbuf * getPixbufRaw() const
void ensurePixelFormat(PixelFormat fmt)
Convert the internal pixel format between CAIRO and GDK formats.
cairo_surface_t * getSurfaceRaw()
Converts the pixbuf to Cairo pixel format and returns an image surface which can be used as a source.
static Pixbuf * create_from_file(std::string const &fn, double svgddpi=0)
static void ensure_argb32(GdkPixbuf *pb)
Converts GdkPixbuf's data to premultiplied ARGB.
static Geom::Affine get_embedded_orientation(GdkPixbuf *buf)
Gets any available orientation data and returns it as an affine.
static void ensure_pixbuf(GdkPixbuf *pb)
Converts GdkPixbuf's data back to its native format.
void _setMimeData(guchar *data, gsize len, Glib::ustring const &format)
cairo_surface_t * _surface
Definition cairo-utils.h:85
static Pixbuf * create_from_buffer(std::string const &, double svgddpi=0, std::string const &fn="")
int height() const
static Pixbuf * create_from_data_uri(gchar const *uri, double svgdpi=0)
time_t modificationTime() const
Definition cairo-utils.h:63
PixelFormat pixelFormat() const
Definition cairo-utils.h:65
void _ensurePixelsARGB32()
GdkPixbuf * _pixbuf
Definition cairo-utils.h:84
const double w
Definition conic-4.cpp:19
vector< vpsc::Rectangle * > rs
Geom::IntPoint size
double c[8][4]
unsigned int guint32
_cairo_pattern cairo_pattern_t
struct _cairo_surface cairo_surface_t
Contains forward declarations of 2geom types.
std::unique_ptr< Magick::Image > image
double offset
void ExtractARGB32(guint32 px, guint32 &a, guint32 &r, guint32 &g, guint &b)
void ExtractRGB32(guint32 px, guint32 &r, guint32 &g, guint &b)
guint AssembleARGB32(guint32 a, guint32 r, guint32 g, guint32 b)
Helper class to stream background task notifications as a series of messages.
struct _cairo cairo_t
Definition path-cairo.h:16
int stride
int buf
auto len
Definition safe-printf.h:21
int const char * fmt
Definition safe-printf.h:18
static const Point data[]
SPBlendMode
SPColorInterpolation
SPStyle - a style object for SPItem objects.