summaryrefslogtreecommitdiff
path: root/basebmp/inc/basebmp/clippedlinerenderer.hxx
blob: e023581416d69ce01a8eaf26d6f2965f98440ff3 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX
#define INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX

#include <basegfx/tools/rectcliptools.hxx>
#include <basegfx/point/b2ipoint.hxx>
#include <basegfx/range/b2irange.hxx>

#include <vigra/diff2d.hxx>
#include <vigra/iteratortraits.hxx>

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(0), cb(0);
    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) ? ~(aMinFlag|aMaxFlag) : ~(bMinFlag|bMaxFlag);

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

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

            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 false;
            }
            else
            {
                o_as = a1 - ca;
                if( o_as < aMin )
                    return false;
            }

            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 & aMinFlag) ? a1 - aMin : aMax - a1);
            cb = 2*da*((clipCode2 & bMinFlag) ? b1 - bMin : bMax - b1);
            clipCode2 &= (cb + da < ca + bRoundTowardsPt2) ? ~(aMinFlag|aMaxFlag) : ~(bMinFlag|bMaxFlag);
        }

        if( clipCode2 & (aMinFlag|aMaxFlag) )
            o_n = (clipCode2 & aMinFlag) ? o_as - aMin : aMax - o_as;
        else
        {
            o_n = (clipCode2 & bMinFlag) ? 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,
                        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, clipCode2, 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 ));

        Iterator currIter( begin + vigra::Diff2D(0,ys) );
        typename vigra::IteratorTraits<Iterator>::row_iterator
            rowIter( currIter.rowIterator() + xs );

        adx *= 2;
        ady *= 2;

        if( bUseAlternateBresenham )
        {
            while(true)
            {
                acc.set(color, rowIter);

                if( rem >= 0 )
                {
                    if( --n < 0 )
                        break;

                    ys += sy;
                    xs += sx;
                    rem -= adx;

                    currIter.y += sy;
                    rowIter = currIter.rowIterator() + xs;
                }
                else
                {
                    xs += sx;
                    rowIter += sx;
                }

                rem += ady;
            }
        }
        else
        {
            while(true)
            {
                acc.set(color, rowIter);

                if( --n < 0 )
                    break;

                if( rem >= 0 )
                {
                    ys += sy;
                    xs += sx;
                    rem -= adx;

                    currIter.y += sy;
                    rowIter = currIter.rowIterator() + xs;
                }
                else
                {
                    xs += sx;
                    rowIter += sx;
                }

                rem += ady;
            }
        }
    }
    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, n, clipCode1, clipCount1, clipCode2, 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 ));

        Iterator currIter( begin + vigra::Diff2D(xs,0) );
        typename vigra::IteratorTraits<Iterator>::column_iterator
            colIter( currIter.columnIterator() + ys );

        adx *= 2;
        ady *= 2;

        if( bUseAlternateBresenham )
        {
            while(true)
            {
                acc.set(color, colIter);

                if( rem >= 0 )
                {
                    if( --n < 0 )
                        break;

                    xs += sx;
                    ys += sy;
                    rem -= ady;

                    currIter.x += sx;
                    colIter = currIter.columnIterator() + ys;
                }
                else
                {
                    ys += sy;
                    colIter += sy;
                }

                rem += adx;
            }
        }
        else
        {
            while(true)
            {
                acc.set(color, colIter);

                if( --n < 0 )
                    break;

                if( rem >= 0 )
                {
                    xs += sx;
                    ys += sy;
                    rem -= ady;

                    currIter.x += sx;
                    colIter = currIter.columnIterator() + ys;
                }
                else
                {
                    ys += sy;
                    colIter += sy;
                }

                rem += adx;
            }
        }
    }
}

} // namespace basebmp

#endif /* INCLUDED_BASEBMP_CLIPPEDLINERENDERER_HXX */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */