summaryrefslogtreecommitdiffstats
path: root/src/display/drawing-image.cpp
blob: 8f6d060a8802a90a641bd6a9dc247c07f46a6d60 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 * Bitmap image belonging to an SVG drawing.
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *
 * Copyright (C) 2011 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <2geom/bezier-curve.h>

#include "display/drawing.h"
#include "display/drawing-context.h"
#include "display/drawing-image.h"
#include "preferences.h"

#include "display/cairo-utils.h"

namespace Inkscape {

DrawingImage::DrawingImage(Drawing &drawing)
    : DrawingItem(drawing)
    , _pixbuf(nullptr)
{}

DrawingImage::~DrawingImage()
{
    // _pixbuf is owned by SPImage - do not delete it
}

void
DrawingImage::setPixbuf(Inkscape::Pixbuf *pb)
{
    _pixbuf = pb;

    _markForUpdate(STATE_ALL, false);
}

void
DrawingImage::setScale(double sx, double sy)
{
    _scale = Geom::Scale(sx, sy);
    _markForUpdate(STATE_ALL, false);
}

void
DrawingImage::setOrigin(Geom::Point const &o)
{
    _origin = o;
    _markForUpdate(STATE_ALL, false);
}

void
DrawingImage::setClipbox(Geom::Rect const &box)
{
    _clipbox = box;
    _markForUpdate(STATE_ALL, false);
}

Geom::Rect
DrawingImage::bounds() const
{
    if (!_pixbuf) return _clipbox;

    double pw = _pixbuf->width();
    double ph = _pixbuf->height();
    double vw = pw * _scale[Geom::X];
    double vh = ph * _scale[Geom::Y];
    Geom::Point wh(vw, vh);
    Geom::Rect view(_origin, _origin+wh);
    Geom::OptRect res = _clipbox & view;
    Geom::Rect ret = res ? *res : _clipbox;

    return ret;
}

unsigned
DrawingImage::_updateItem(Geom::IntRect const &, UpdateContext const &, unsigned, unsigned)
{
    _markForRendering();

    // Calculate bbox
    if (_pixbuf) {
        Geom::Rect r = bounds() * _ctm;
        _bbox = r.roundOutwards();
    } else {
        _bbox = Geom::OptIntRect();
    }

    return STATE_ALL;
}

unsigned DrawingImage::_renderItem(DrawingContext &dc, Geom::IntRect const &/*area*/, unsigned /*flags*/, DrawingItem * /*stop_at*/)
{
    bool outline = _drawing.outline();

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool imgoutline = prefs->getBool("/options/rendering/imageinoutlinemode", false);

    if (!outline || imgoutline) {
        if (!_pixbuf) return RENDER_OK;

        Inkscape::DrawingContext::Save save(dc);
        dc.transform(_ctm);
        dc.newPath();
        dc.rectangle(_clipbox);
        dc.clip();

        dc.translate(_origin);
        dc.scale(_scale);
        dc.setSource(_pixbuf->getSurfaceRaw(), 0, 0);
        dc.patternSetExtend(CAIRO_EXTEND_PAD);

        if (_style) {
            // See: http://www.w3.org/TR/SVG/painting.html#ImageRenderingProperty
            //      https://drafts.csswg.org/css-images-3/#the-image-rendering
            //      style.h/style.cpp, cairo-render-context.cpp
            //
            // CSS 3 defines:
            //   'optimizeSpeed' as alias for "pixelated"
            //   'optimizeQuality' as alias for "smooth"
            switch (_style->image_rendering.computed) {
                case SP_CSS_IMAGE_RENDERING_OPTIMIZESPEED:
                case SP_CSS_IMAGE_RENDERING_PIXELATED:
                // we don't have an implementation for crisp-edges, but it should *not* smooth or blur
                case SP_CSS_IMAGE_RENDERING_CRISPEDGES:
                    dc.patternSetFilter( CAIRO_FILTER_NEAREST );
                    break;
                case SP_CSS_IMAGE_RENDERING_AUTO:
                case SP_CSS_IMAGE_RENDERING_OPTIMIZEQUALITY:
                default:
                    // In recent Cairo, BEST used Lanczos3, which is prohibitively slow
                    dc.patternSetFilter( CAIRO_FILTER_GOOD );
                    break;
            }
        }

        dc.paint(1);

    } else { // outline; draw a rect instead

        guint32 rgba = prefs->getInt("/options/wireframecolors/images", 0xff0000ff);

        {   Inkscape::DrawingContext::Save save(dc);
            dc.transform(_ctm);
            dc.newPath();

            Geom::Rect r = bounds();
            Geom::Point c00 = r.corner(0);
            Geom::Point c01 = r.corner(3);
            Geom::Point c11 = r.corner(2);
            Geom::Point c10 = r.corner(1);

            dc.moveTo(c00);
            // the box
            dc.lineTo(c10);
            dc.lineTo(c11);
            dc.lineTo(c01);
            dc.lineTo(c00);
            // the diagonals
            dc.lineTo(c11);
            dc.moveTo(c10);
            dc.lineTo(c01);
        }

        dc.setLineWidth(0.5);
        dc.setSource(rgba);
        dc.stroke();
    }
    return RENDER_OK;
}

/** Calculates the closest distance from p to the segment a1-a2*/
static double
distance_to_segment (Geom::Point const &p, Geom::Point const &a1, Geom::Point const &a2)
{
    Geom::LineSegment l(a1, a2);
    Geom::Point np = l.pointAt(l.nearestTime(p));
    return Geom::distance(np, p);
}

DrawingItem *
DrawingImage::_pickItem(Geom::Point const &p, double delta, unsigned /*sticky*/)
{
    if (!_pixbuf) return nullptr;

    bool outline = _drawing.outline() || _drawing.outlineOverlay() || _drawing.getOutlineSensitive();

    if (outline) {
        Geom::Rect r = bounds();
        Geom::Point pick = p * _ctm.inverse();

        // find whether any side or diagonal is within delta
        // to do so, iterate over all pairs of corners
        for (unsigned i = 0; i < 3; ++i) { // for i=3, there is nothing to do
            for (unsigned j = i+1; j < 4; ++j) {
                if (distance_to_segment(pick, r.corner(i), r.corner(j)) < delta) {
                    return this;
                }
            }
        }
        return nullptr;

    } else {
        unsigned char *const pixels = _pixbuf->pixels();
        int width = _pixbuf->width();
        int height = _pixbuf->height();
        size_t rowstride = _pixbuf->rowstride();

        Geom::Point tp = p * _ctm.inverse();
        Geom::Rect r = bounds();

        if (!r.contains(tp))
            return nullptr;

        double vw = width * _scale[Geom::X];
        double vh = height * _scale[Geom::Y];
        int ix = floor((tp[Geom::X] - _origin[Geom::X]) / vw * width);
        int iy = floor((tp[Geom::Y] - _origin[Geom::Y]) / vh * height);

        if ((ix < 0) || (iy < 0) || (ix >= width) || (iy >= height))
            return nullptr;

        unsigned char *pix_ptr = pixels + iy * rowstride + ix * 4;
        // pick if the image is less than 99% transparent
        guint32 alpha = 0;
        if (_pixbuf->pixelFormat() == Inkscape::Pixbuf::PF_CAIRO) {
            guint32 px = *reinterpret_cast<guint32 const *>(pix_ptr);
            alpha = (px & 0xff000000) >> 24;
        } else if (_pixbuf->pixelFormat() == Inkscape::Pixbuf::PF_GDK) {
            alpha = pix_ptr[3];
        } else {
            throw std::runtime_error("Unrecognized pixel format");
        }
        float alpha_f = (alpha / 255.0f) * _opacity;
        return alpha_f > 0.01 ? this : nullptr;
    }
}

} // end namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :