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
#include <cmath>
#include <cstdlib>
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>
#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 << ")";
}

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

/* Algorithm based on pseudo-code at
   https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring
*/
template<typename Real>
RGBColor
mandelbrot(Real c_x, Real c_y, unsigned int maxiterations)
{
    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 RGBColor(255u,255u,255u); // white
        }

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

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

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

template<typename Real>
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;


    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;

    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 color = mandelbrot(x, y, p.maxiterations).get_rgb();
            pp[0] = color.red   * 255;
            pp[1] = color.green * 255;
            pp[2] = color.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>(cmdname, argc, argv);
}