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
#include <cmath>
#include <cstdlib>
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
#include <gdk-pixbuf/gdk-pixbuf.h>

/*
   Compile with

   g++ -std=c++11 -Wall `pkg-config --cflags --libs gdk-pixbuf-2.0` mandelbrot_1.cpp
*/


template<typename Real>
struct Parameters
{
    Real                center_x;
    Real                center_y;
    Real                horizontal_diameter;

    // in pixels:
    unsigned int        width;
    unsigned int        height;

    // parameter for the mandelbrot function:
    unsigned int        maxiterations;

    // derived values
    Real                resolution;
    Real                xmin;
    Real                xmax;
    Real                vertical_diameter;
    Real                ymin;
    Real                ymax;
};

// ----------------------------------------------------------------------------
void
usage(const char* cmdname)
{
    std::cerr << "Usage: " << cmdname << " filename"
              << std::endl;
    exit(1);
}

// ----------------------------------------------------------------------------

struct RGBColor
{
    RGBColor()
        : red(0), green(0), blue(0)
    {
    }

    RGBColor(unsigned red, unsigned green, unsigned blue)
        : red(red/255.0), green(green/255.0), blue(blue/255.0)
    {
    }

    RGBColor(float red, float green, float blue)
        : red(red), green(green), blue(blue)
    {
    }

    RGBColor
    get_rgb() const 
    {
      return *this;
    }

    RGBColor interpolate(const RGBColor& other, float t)
    {
        return RGBColor((red   + (other.red   - red) * t),
                        (green + (other.green - green) * t),
                        (blue  + (other.blue  - blue) * t));
    }

    bool
    operator==(const RGBColor& other)
    {
        return std::fabs(red - other.red)     < DBL_EPSILON &&
               std::fabs(green - other.green) < DBL_EPSILON &&
               std::fabs(blue - other.blue)   < DBL_EPSILON;
    }

    bool
    operator!=(const RGBColor& other)
    {
        return !(*this == other);
    }

    float red;
    float green;
    float blue;
};

std::ostream &
operator<<(std::ostream& out, const RGBColor &color)
{
    return out << "(" << color.red << ", " << color.green <<
                  ", " << color.blue << ")";
}
// ----------------------------------------------------------------------------

template<typename Real, typename Color>
class Colorizer
{
    public:
        using color_type = Color;

        // Hack: Add black and white to default color list.  Construction of
        //       white works only for RGBColor!!! Sole purpose of this is a
        //       simple test of the escape-time algorithm
        Colorizer()
            : color_shift(0), color_progression(3),
              colors{Color(), Color(255u,255u,255u)}
        {
        }

        template<typename Iterator>
        Colorizer(Real color_shift, Real color_progression,
                  Iterator it1, Iterator it2)
            : color_shift(color_shift), color_progression(color_progression),
              colors(it1, it2)
        {
            assert(colors.size() > 0);
        }

        Color
        operator()(unsigned int maxiterations, unsigned int iterations, Real t)
        {
            double value = std::log(iterations+t)/std::log(Real(maxiterations));
            double index = std::fmod(color_shift + value*color_progression,
                                     colors.size());
            unsigned int i    = std::floor(index); double remainder = index - i;
            unsigned int next = (i + 1) % colors.size();
            return colors[index].interpolate(colors[next], remainder);
        }

    private:
        Real color_shift;
        Real color_progression;
        std::vector<Color> colors;
};

// ----------------------------------------------------------------------------

/* Algorithm based on pseudo-code at
   https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring
*/
template<typename Real, typename Colorizer>
typename Colorizer::color_type
mandelbrot(Real c_x, Real c_y, unsigned int maxiterations, Colorizer colorizer)
{
    using Color = typename Colorizer::color_type;

    constexpr Real bailout  = 2;
    constexpr Real bailout2 = bailout * bailout;

    Real x = 0;
    Real y = 0;

    for (unsigned int iteration=0; iteration<maxiterations; ++iteration) {
        Real x_square = x * x;
        Real y_square = y * y;
        Real distance2 = x_square + y_square;

        if (distance2 >= bailout2) {
            return colorizer(maxiterations, iteration, 0);
        }

        Real next_x = x_square - y_square + c_x;
        Real next_y = 2*x*y + c_y;

        x = next_x;
        y = next_y;
    }
    return Color(); // black
}

// ----------------------------------------------------------------------------

template<typename Real, typename Color>
bool
do_main(const char* cmdname, int argc, char** argv)
{
    Parameters<Real> p = {
        .center_x            = -0.7,
        .center_y            = 0,
        .horizontal_diameter = 3.0769,
        .width               = 512,
        .height              = 480,
        .maxiterations       = 1000,
    };

    /* compute derived parameters */
    p.resolution        = p.width / p.horizontal_diameter;
    p.xmin              = p.center_x - p.horizontal_diameter / 2;
    p.xmax              = p.center_x + p.horizontal_diameter / 2;
    p.vertical_diameter = p.height / p.resolution;
    p.ymin              = p.center_y - p.vertical_diameter / 2;
    p.ymax              = p.center_y + p.vertical_diameter / 2;

    if (argc != 1) {
        usage(cmdname);
    }
    std::string filename(*argv++); --argc;

    std::cout << "file name =   " << filename << std::endl;

    GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
                                       p.width, p.height);

    int n_channels     = gdk_pixbuf_get_n_channels(pixbuf);
    int rowstride      = gdk_pixbuf_get_rowstride(pixbuf);
    guchar* pixels     = gdk_pixbuf_get_pixels(pixbuf);

    if (!pixels) return false;

    Colorizer<Real, Color> colorizer;

    for (unsigned int i = 0; i < p.height; ++i) {
        for (unsigned int j = 0; j < p.width; ++j) {
            guchar* pp = pixels + i * rowstride + j * n_channels;
            Real x     = p.xmin + j/p.resolution;
            Real y     = p.ymax - i/p.resolution;
            RGBColor   c = mandelbrot(x, y, p.maxiterations, colorizer).get_rgb();

            pp[0] = c.red   * 255;
            pp[1] = c.green * 255;
            pp[2] = c.blue  * 255;
        }
    }

    return gdk_pixbuf_save(pixbuf, filename.c_str(), "jpeg",
                           nullptr"quality""100"nullptr) != FALSE;
}

#ifndef REAL
#define REAL double
#endif

int
main(int argc, char** argv)
{
    const char* cmdname = *argv++; --argc;

    if (argc == 0) {
        usage(cmdname);
    }

    do_main<REAL, RGBColor>(cmdname, argc, argv);
}