summaryrefslogtreecommitdiff
path: root/basebmp/inc/basebmp/clippedlinerenderer.hxx
blob: 0c484a08ad3f40a5b92af7d3a0054c2c6e57aab1 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: clippedlinerenderer.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: thb $ $Date: 2006-06-02 08:36:14 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX
#define INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX

#ifndef _BGFX_TOOLS_RECTCLIPTOOLS_HXX
#include <basegfx/tools/rectcliptools.hxx>
#endif

namespace basebmp
{

// factored-out bresenham setup code, which is used from two different
// places in renderClippedLine() below. Admittedly messy for the long
// parameter list...
inline bool prepareClip( sal_Int32  a1,
                         sal_Int32  a2,
                         sal_Int32  b1,
                         sal_Int32  da,
                         sal_Int32  db,
                         sal_Int32& o_as,
                         sal_Int32& o_bs,
                         int        sa,
                         int        sb,
                         sal_Int32& io_rem,
                         int&       o_n,
                         sal_uInt32 clipCode1,
                         sal_uInt32 clipCount1,
                         sal_uInt32 clipCode2,
                         sal_uInt32 clipCount2,
                         sal_Int32  aMin,
                         sal_uInt32 aMinFlag,
                         sal_Int32  aMax,
                         sal_uInt32 aMaxFlag,
                         sal_Int32  bMin,
                         sal_uInt32 bMinFlag,
                         sal_Int32  bMax,
                         sal_uInt32 bMaxFlag,
                         bool       bRoundTowardsPt2 )
{
    int ca, cb;
    if( clipCode1 )
    {
        if( clipCode1 & aMinFlag )
        {
            ca = 2*db*(aMin - a1);
            o_as = aMin;
        }
        else if( clipCode1 & aMaxFlag )
        {
            ca = 2*db*(a1 - aMax);
            o_as = aMax;
        }

        if( clipCode1 & bMinFlag )
        {
            cb = 2*da*(bMin - b1);
            o_bs = bMin;
        }
        else if( clipCode1 & bMaxFlag )
        {
            cb = 2*da*(b1 - bMax);
            o_bs = bMax;
        }

        if( clipCount1 == 2 )
            clipCode1 &= (ca + da < cb + !bRoundTowardsPt2) ? ~(aMin|aMax) : ~(bMin|bMax);

        if( clipCode1 & (aMin|aMax) )
        {
            cb = (ca + da - !bRoundTowardsPt2) / (2*da);

            if( sb >= 0 )
            {
                o_bs = b1 + cb;
                if( o_bs > bMax )
                    return;
            }
            else
            {
                o_bs = b1 - cb;
                if( o_bs < bMin )
                    return;
            }

            io_rem += ca - 2*da*cb;
        }
        else
        {
            ca = (cb - da + 2*db - bRoundTowardsPt2) / (2*db);
            if( sa >= 0 )
            {
                o_as = a1 + ca;
                if( o_as > aMax )
                    return;
            }
            else
            {
                o_as = a1 - ca;
                if( o_as < aMin )
                    return;
            }

            io_rem += 2*db*ca - cb;
        }
    }
    else
    {
        o_as = a1; o_bs = b1;
    }

    bool bRetVal = false;
    if( clipCode2 )
    {
        if( clipCount2 == 2 )
        {
            ca = 2*db*((clipCode2 & aMin) ? a1 - aMin : aMax - a1);
            cb = 2*da*((clipCode2 & bMin) ? b1 - bMin : bMax - b1);
            clipCode2 &= (cb + da < ca + bRoundTowardsPt2) ? ~(aMin|aMax) : ~(bMin|bMax);
        }

        if( clipCode2 & (aMin|aMax) )
            o_n = (clipCode2 & aMin) ? o_as - aMin : aMax - o_as;
        else
        {
            o_n = (clipCode2 & bMin) ? o_bs - bMin : bMax - o_bs;
            bRetVal = true;
        }
    }
    else
        o_n = (a2 >= o_as) ? a2 - o_as : o_as - a2;

    return bRetVal;
}


/** Render line to image iterators, clip against given rectangle

    This method renders a line from aPt1 to aPt2, clipped against
    rClipRect (the clipping will take place pixel-perfect, i.e. as if
    the original bresenham-rendered line would have been clipped each
    pixel individually. No slight shifts compared to unclipped lines).

    @param aPt1
    Start point of the line

    @param aPt2
    End point of the line

    @param rClipRect
    Rectangle to clip against

    @param color
    Color value to render the line with

    @param begin
    left-top image iterator

    @param end
    right-bottom image iterator

    @param acc
    Image accessor

    @param bRoundTowardsPt2
    Rounding mode to use. Giving false here results in line pixel tend
    towards pt1, i.e. when a pixel exactly hits the middle between two
    pixel, the pixel closer to pt1 will be chosen. Giving true here
    makes renderClippedLine() choose pt2 in those cases.
 */
template< class Iterator, class Accessor >
void renderClippedLine( basegfx::B2IPoint             aPt1,
                        basegfx::B2IPoint             aPt2,
                        const basegfx::B2IRange&      rClipRect,
                        typename Accessor::value_type color,
                        Iterator                      begin,
                        Iterator                      end,
                        Accessor                      acc,
                        bool                          bRoundTowardsPt2=false )
{
    // Algorithm according to Steven Eker's 'Pixel-perfect line clipping',
    // Graphics Gems V, pp. 314-322
    sal_uInt32 clipCode1 = basegfx::tools::getCohenSutherlandClipFlags(aPt1,
                                                                       rClipRect);
    sal_uInt32 clipCode2 = basegfx::tools::getCohenSutherlandClipFlags(aPt2,
                                                                       rClipRect);

    if( clipCode1 & clipCode2 )
        return; // line fully clipped away

    sal_uInt32 clipCount1 = basegfx::tools::getNumberOfClipPlanes(clipCode1);
    sal_uInt32 clipCount2 = basegfx::tools::getNumberOfClipPlanes(clipCode2);

    if( (clipCode1 != 0 && clipCode2 == 0)
        || (clipCount1 == 2 && clipCount2 == 1) )
    {
        std::swap(clipCount2,clipCount1);
        std::swap(clipCode2,clipCode1);
        std::swap(aPt1,aPt2);
        bRoundTowardsPt2 = !bRoundTowardsPt2;
    }

    const sal_Int32 x1 = aPt1.getX();
    const sal_Int32 x2 = aPt2.getX();
    const sal_Int32 y1 = aPt1.getY();
    const sal_Int32 y2 = aPt2.getY();

    // TODO(E1): This might overflow
    sal_Int32 adx = x2 - x1;
    int sx = 1;
    if( adx < 0 )
    {
        adx *= -1;
        sx = -1;
    }

    // TODO(E1): This might overflow
    sal_Int32 ady = y2 - y1;
    int sy = 1;
    if( ady < 0 )
    {
        ady *= -1;
        sy = -1;
    }

    int n  = 0;
    sal_Int32 xs = x1;
    sal_Int32 ys = y1;
    if( adx >= ady )
    {
        // semi-horizontal line
        sal_Int32 rem = 2*ady - adx - !bRoundTowardsPt2;

        const bool bUseAlternateBresenham(
            prepareClip(x1, x2, y1, adx, ady, xs, ys, sx, sy,
                        rem, n, clipCode1, clipCount1, clipCode1, clipCount2,
                        rClipRect.getMinX(), basegfx::tools::RectClipFlags::LEFT,
                        rClipRect.getMaxX(), basegfx::tools::RectClipFlags::RIGHT,
                        rClipRect.getMinY(), basegfx::tools::RectClipFlags::TOP,
                        rClipRect.getMaxY(), basegfx::tools::RectClipFlags::BOTTOM,
                        bRoundTowardsPt2 ));
        const sal_Int32 diff = 2*(ady - adx);
        if( bUseAlternateBresenham )
        {
            while(true)
            {
                plot(xs,ys);

                if( rem >= 0 )
                {
                    if( --n < 0 )
                        break;
                    rem += diff;
                    ys += sy;
                }
                else
                    rem += 2*ady;

                xs += sx;
            }
        }
        else
        {
            while(true)
            {
                plot(xs,ys);

                if( --n < 0 )
                    break;
                if( rem >= 0 )
                {
                    rem += diff;
                    ys += sy;
                }
                else
                    rem += 2*ady;

                xs += sx;
            }
        }
    }
    else
    {
        // semi-vertical line
        sal_Int32 rem = 2*adx - ady - !bRoundTowardsPt2;

        const bool bUseAlternateBresenham(
            prepareClip(y1, y2, x1, ady, adx, ys, xs, sy, sx,
                        rem, clipCode1, clipCount1, clipCode1, clipCount2,
                        rClipRect.getMinY(), basegfx::tools::RectClipFlags::TOP,
                        rClipRect.getMaxY(), basegfx::tools::RectClipFlags::BOTTOM,
                        rClipRect.getMinX(), basegfx::tools::RectClipFlags::LEFT,
                        rClipRect.getMaxX(), basegfx::tools::RectClipFlags::RIGHT,
                        bRoundTowardsPt2 ));
        const sal_Int32 diff = 2*(ady - adx);
        if( bUseAlternateBresenham )
        {
            while(true)
            {
                plot(xy,ys);

                if( rem >= 0 )
                {
                    if( --n < 0 )
                        break;
                    rem += diff;
                    xs += sx;
                }
                else
                    rem += 2*adx;

                ys += sy;
            }
        }
        else
        {
            while(true)
            {
                plot(xs,ys);

                if( --n < 0 )
                    break;
                if( rem >= 0 )
                {
                    rem += diff;
                    xs += sx;
                }
                else
                    rem += 2*adx;

                ys += sy;
            }
        }
    }
}

} // namespace basebmp

#endif /* INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX */