File: datafunctions.cpp

package info (click to toggle)
fraqtive 0.4.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 988 kB
  • ctags: 1,249
  • sloc: cpp: 8,387; sh: 103; makefile: 45
file content (349 lines) | stat: -rw-r--r-- 11,793 bytes parent folder | download
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
/**************************************************************************
* This file is part of the Fraqtive program
* Copyright (C) 2004-2008 Michał Męciński
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#include "datafunctions.h"
#include "fractaldata.h"

namespace DataFunctions
{

Position defaultPosition( const FractalType& type )
{
    Position position;
    position.setZoomFactor( -0.45 );

    if ( type.fractal() == MandelbrotFractal && type.exponentType() == IntegralExponent ) {
        if ( type.variant() == GeneratorCore::NormalVariant && type.integralExponent() == 2 ) {
            position.setCenter( QPointF( -0.7, 0 ) );
        } else if ( type.variant() == GeneratorCore::NormalVariant && type.integralExponent() == 4 ) {
            position.setCenter( QPointF( -0.2, 0 ) );
        } else if ( type.variant() == GeneratorCore::ConjugateVariant && type.integralExponent() == 2 ) {
            position.setCenter( QPointF( -0.3, 0 ) );
            position.setZoomFactor( -0.6 );
        } else if ( type.variant() == GeneratorCore::AbsoluteVariant && type.integralExponent() == 2 ) {
            position.setCenter( QPointF( -0.5, -0.5 ) );
            position.setZoomFactor( -0.5 );
        } else if ( type.variant() == GeneratorCore::AbsoluteVariant && type.integralExponent() == 3 ) {
            position.setCenter( QPointF( -0.15, 0.15 ) );
        } else if ( type.variant() == GeneratorCore::AbsoluteImVariant && type.integralExponent() == 2 ) {
            position.setCenter( QPointF( -0.5, 0 ) );
            position.setZoomFactor( -0.5 );
        } else if ( type.variant() == GeneratorCore::AbsoluteImVariant && type.integralExponent() == 3 ) {
            position.setCenter( QPointF( 0, 0.15 ) );
        }
    }

    return position;
}

Gradient defaultGradient()
{
    QPolygonF red;
    red.append( QPointF( 0.0, 0.0 ) );
    red.append( QPointF( 0.1667, 1.0 ) );
    red.append( QPointF( 0.5, 0.0 ) );
    red.append( QPointF( 0.8333, 1.0 ) );
    red.append( QPointF( 1.0, 0.0 ) );

    QPolygonF green;
    green.append( QPointF( 0.0, 0.0 ) );
    green.append( QPointF( 0.25, 1.0 ) );
    green.append( QPointF( 0.5, 0.0 ) );
    green.append( QPointF( 0.6667, 1.0 ) );
    green.append( QPointF( 1.0, 0.0 ) );

    QPolygonF blue;
    blue.append( QPointF( 0.0, 0.0 ) );
    blue.append( QPointF( 0.3333, 1.0 ) );
    blue.append( QPointF( 0.5, 0.0 ) );
    blue.append( QPointF( 0.75, 1.0 ) );
    blue.append( QPointF( 1.0, 0.0 ) );

    Gradient gradient;
    gradient.setRed( red );
    gradient.setGreen( green );
    gradient.setBlue( blue );

    return gradient;
}

ColorMapping defaultColorMapping()
{
    ColorMapping mapping;

    mapping.setScale( 0.056 );

    return mapping;
}

GeneratorSettings defaultGeneratorSettings()
{
    GeneratorSettings settings;

    settings.setCalculationDepth( 2.5 );
    settings.setDetailThreshold( 0.9 );

    return settings;
}

ViewSettings defaultViewSettings()
{
    ViewSettings settings;

    settings.setAntiAliasing( MediumAntiAliasing );
    settings.setMeshResolution( MediumResolution );
    settings.setHeightScale( 0.14 );
    settings.setCameraZoom( 24.0 );

    return settings;
}


static QPolygonF interpolateCubic( const QPolygonF& polygon )
{
    QPolygonF interpolated;

    if ( !polygon.isEmpty() )
        interpolated.append( polygon.first() );

    for ( int i = 1; i < polygon.count(); i++ ) {
        QPointF p1 = polygon.at( i - 1 );
        QPointF p2 = polygon.at( i );
        if ( ( p2.x() - p1.x() ) > 0.03 ) {
            QPainterPath path;
            path.moveTo( p1 );
            path.cubicTo( ( p1.x() + p2.x() ) / 2, p1.y(), ( p1.x() + p2.x() ) / 2, p2.y(), p2.x(), p2.y() );
            int segments = qBound( 1, qRound( ( p2.x() - p1.x() ) * 50.0 ), 10 );
            for ( int j = 1; j <= segments; j++ ) {
                QPointF point = path.pointAtPercent( (double)j / (double)segments );
                interpolated.append( point );
            }
        } else {
            interpolated.append( p2 );
        }
    }

    return interpolated;
}

static double lerp( const QPolygonF& points, int index, double x )
{
    QLineF line( points.at( index - 1 ), points.at( index ) );
    if ( line.dx() == 0.0 )
        return line.y2();
    return line.y1() + line.dy() * ( x - line.x1() ) / line.dx();
}

static QGradientStop makeStop( double x, double ry, double gy, double by )
{
    return QGradientStop( x, QColor( qRound( 255.0 * ry ), qRound( 255.0 * gy ), qRound( 255.0 * by ) ) );
}

QGradientStops calculateGradientStops( const Gradient& gradient )
{
    QPolygonF red = interpolateCubic( gradient.red() );
    QPolygonF green = interpolateCubic( gradient.green() );
    QPolygonF blue = interpolateCubic( gradient.blue() );

    QGradientStops stops;

    if ( red.isEmpty() || green.isEmpty() || blue.isEmpty() )
        return stops;

    stops.append( makeStop( 0.0, red.first().y(), green.first().y(), blue.first().y() ) );

    double x = 0.0;
    int ri = 1, gi = 1, bi = 1;
    while ( ri < red.count() && gi < green.count() && bi < blue.count() ) {
        double rx = red.at( ri ).x();
        double gx = green.at( gi ).x();
        double bx = blue.at( bi ).x();
        x = qMin( qMin( rx, gx ), bx );
        stops.append( makeStop( x, lerp( red, ri, x ), lerp( green, gi, x ), lerp( blue, bi, x ) ) );
        if ( rx == x )
            ri++;
        if ( gx == x )
            gi++;
        if ( bx == x )
            bi++;
    }

    return stops;
}

void fillGradientCache( const Gradient& gradient, QRgb* cache, int size )
{
    if ( gradient.isEmpty() ) {
        for ( int i = 0; i < size; i++ )
            cache[ i ] = qRgb( 0, 0, 0 );
        return;
    }

    QPolygonF red = interpolateCubic( gradient.red() );
    QPolygonF green = interpolateCubic( gradient.green() );
    QPolygonF blue = interpolateCubic( gradient.blue() );

    int ri = 1, gi = 1, bi = 1;
    for ( int i = 0; i < size; i++ ) {
        double x = (double)i / (double)( size - 1 );
        while ( red.at( ri ).x() < x )
            ri++;
        while ( green.at( gi ).x() < x )
            gi++;
        while ( blue.at( bi ).x() < x )
            bi++;
        double ry = lerp( red, ri, x );
        double gy = lerp( green, gi, x );
        double by = lerp( blue, bi, x );
        cache[ i ] = qRgb( qRound( 255.0 * ry ), qRound( 255.0 * gy ), qRound( 255.0 * by ) );
    }
}

ColorMapper::ColorMapper( const QRgb* gradientCache, int gradientSize, QRgb backgroundColor, const ColorMapping& mapping ) :
    m_gradientCache( gradientCache ),
    m_gradientSize( gradientSize ),
    m_backgroundColor( backgroundColor ),
    m_mapping( mapping )
{
}

QRgb ColorMapper::map( double value ) const
{
    if ( value == 0.0 )
        return m_backgroundColor;

    int index;
    if ( m_mapping.isMirrored() ) {
        int scaled = (int)( ( m_mapping.scale() * value + 2 * m_mapping.offset() ) * m_gradientSize );
        index = scaled % ( 2 * m_gradientSize );
        if ( index >= m_gradientSize )
            index = 2 * m_gradientSize - index - 1;
    } else {
        int scaled = (int)( ( m_mapping.scale() * value + m_mapping.offset() ) * m_gradientSize );
        index = scaled % m_gradientSize;
    }

    if ( m_mapping.isReversed() )
        index = m_gradientSize - index - 1;

    return m_gradientCache[ index ];
}

template<int A, int B, int C>
static inline QRgb maskedSum( QRgb color[ 3 ][ 3 ], int mask )
{
    QRgb middle = ( color[ 1 ][ 1 ] & mask );
    QRgb sides = ( color[ 0 ][ 1 ] & mask ) + ( color[ 2 ][ 1 ] & mask ) + ( color[ 1 ][ 0 ] & mask ) + ( color[ 1 ][ 2 ] & mask );
    QRgb corners = ( color[ 0 ][ 0 ] & mask ) + ( color[ 2 ][ 0 ] & mask ) + ( color[ 0 ][ 2 ] & mask ) + ( color[ 2 ][ 2 ] & mask );
    return A * middle + B * sides + C * corners;
}

template<int A, int B, int C>
static inline QRgb calcAntiAliased( QRgb color[ 3 ][ 3 ] )
{
    QRgb sum = maskedSum<A, B, C>( color, 0xf0f0f0 );
    QRgb carry = maskedSum<A, B, C>( color, 0x0f0f0f ) & 0xf0f0f0;
    return ( ( sum + carry ) >> 4 ) | 0xff000000;
}

template<int A, int B, int C>
static void drawAntiAliased( QImage& image, const FractalData* data, const QRect& region, const ColorMapper& mapper )
{
    QRgb color[ 3 ][ 3 ];

    int stride = data->stride();
    int width = region.width();

    for ( int y = region.top(); y <= region.bottom(); y++ ) {
        const double* src = data->buffer() + y * stride + region.left() ;
        QRgb* dest = reinterpret_cast<QRgb*>( image.scanLine( y ) ) + region.left();

        for ( int i = 0; i < 3; i++ ) {
            color[ i ][ 1 ] = mapper.map( src[ i * stride ] );
            color[ i ][ 2 ] = mapper.map( src[ i * stride + 1 ] );
        }

        for ( int x = 0; x < width; x++ ) {
            for ( int i = 0; i < 3; i++ ) {
                color[ i ][ 0 ] = color[ i ][ 1 ];
                color[ i ][ 1 ] = color[ i ][ 2 ];
                color[ i ][ 2 ] = mapper.map( src[ i * stride + x + 2 ] );
            }
            dest[ x ] = calcAntiAliased<A, B, C>( color );
        }
    }
}

void drawImage( QImage& image, const FractalData* data, const QRect& region, const ColorMapper& mapper, AntiAliasing antiAliasing )
{
    switch ( antiAliasing ) {
        case NoAntiAliasing:
            drawAntiAliased<16, 0, 0>( image, data, region, mapper );
            break;
        case LowAntiAliasing:
            drawAntiAliased<12, 1, 0>( image, data, region, mapper );
            break;
        case MediumAntiAliasing:
            drawAntiAliased<8, 1, 1>( image, data, region, mapper );
            break;
        case HighAntiAliasing:
            drawAntiAliased<4, 2, 1>( image, data, region, mapper );
            break;
    }
}

GeneratorCore::Functor* createFunctor( const FractalType& type )
{
    switch ( type.exponentType() ) {
        case IntegralExponent:
            if ( type.fractal() == MandelbrotFractal ) {
                return GeneratorCore::createMandelbrotFastFunctor( type.integralExponent(), type.variant() );
            } else {
                return GeneratorCore::createJuliaFastFunctor( type.parameter().x(),
                    type.parameter().y(), type.integralExponent(), type.variant() );
            }
            break;

        case RealExponent:
            if ( type.fractal() == MandelbrotFractal ) {
                return GeneratorCore::createMandelbrotFunctor( type.realExponent(), type.variant() );
            } else if ( type.fractal() == JuliaFractal ) {
                return GeneratorCore::createJuliaFunctor( type.parameter().x(),
                    type.parameter().y(), type.realExponent(), type.variant() );
            }
            break;
    }

    return NULL;
}

#if defined( HAVE_SSE2 )

GeneratorCore::FunctorSSE2* createFunctorSSE2( const FractalType& type )
{
    switch ( type.exponentType() ) {
        case IntegralExponent:
            if ( GeneratorCore::isSSE2Available() ) {
                if ( type.fractal() == MandelbrotFractal ) {
                    return GeneratorCore::createMandelbrotFunctorSSE2( type.integralExponent(), type.variant() );
                } else if ( type.fractal() == JuliaFractal ) {
                    return GeneratorCore::createJuliaFunctorSSE2( type.parameter().x(),
                        type.parameter().y(), type.integralExponent(), type.variant() );
                }
            }
            break;
    }

    return NULL;
}

#endif

} // namespace DataFunctions