Blame operations/workshop/mandelbrot.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_double(real, _("Real"),      -200.0, 200.0, -1.77,
Packit bc1512
                  _("Real coordinate"))
Packit bc1512
gegl_chant_double(img,  _("Imaginary"), -200.0, 200.0, 0.0,
Packit bc1512
                  _("Imaginary coordinate"))
Packit bc1512
gegl_chant_double(level, _("Level"), -200.0, 200.0, 3.5,
Packit bc1512
                  _("Water level"))
Packit bc1512
gegl_chant_int (maxiter, _("Iterations"), 0, 512, 128,
Packit bc1512
                _("Maximum number of iterations"))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_SOURCE
Packit bc1512
#define GEGL_CHANT_C_FILE           "mandelbrot.c"
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
Packit bc1512
static gfloat
Packit bc1512
mandel_calc(GeglChantO *o, gfloat x, gfloat y)
Packit bc1512
{
Packit bc1512
  gfloat fViewRectReal = o->real;
Packit bc1512
  gfloat fViewRectImg  = o->img;
Packit bc1512
  gfloat fMagLevel     = o->level;
Packit bc1512
Packit bc1512
  gfloat fCReal = fViewRectReal + x * fMagLevel;
Packit bc1512
  gfloat fCImg  = fViewRectImg + y * fMagLevel;
Packit bc1512
  gfloat fZReal = fCReal;
Packit bc1512
  gfloat fZImg  = fCImg;
Packit bc1512
Packit bc1512
        gint n;
Packit bc1512
Packit bc1512
        for (n=0;n<o->maxiter;n++)
Packit bc1512
          {
Packit bc1512
                gfloat fZRealSquared = fZReal * fZReal;
Packit bc1512
                gfloat fZImgSquared = fZImg * fZImg;
Packit bc1512
Packit bc1512
                if (fZRealSquared + fZImgSquared > 4)
Packit bc1512
                        return 1.0*n/(o->maxiter);
Packit bc1512
Packit bc1512
/*                -- z = z^2 + c*/
Packit bc1512
                fZImg = 2 * fZReal * fZImg + fCImg;
Packit bc1512
                fZReal = fZRealSquared - fZImgSquared + fCReal;
Packit bc1512
          }
Packit bc1512
        return 1.0;
Packit bc1512
}
Packit bc1512
Packit bc1512
static void prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  gegl_operation_set_format (operation, "output", babl_format ("Y float"));
Packit bc1512
}
Packit bc1512
Packit bc1512
static GeglRectangle
Packit bc1512
get_bounding_box (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglRectangle result = {-256,-256, 1024, 1024};
Packit bc1512
  return result;
Packit bc1512
}
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *operation,
Packit bc1512
         GeglBuffer          *output,
Packit bc1512
         const GeglRectangle *result,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  gfloat     *buf;
Packit bc1512
  gint        pxsize;
Packit bc1512
Packit bc1512
  g_object_get (output, "px-size", &pxsize, NULL);
Packit bc1512
Packit bc1512
  buf = g_malloc (result->width * result->height * pxsize);
Packit bc1512
Packit bc1512
    {
Packit bc1512
      gfloat *dst = buf;
Packit bc1512
      gint y;
Packit bc1512
      for (y=0; y < result->height; y++)
Packit bc1512
        {
Packit bc1512
          gint x;
Packit bc1512
          for (x=0; x < result->width ; x++)
Packit bc1512
            {
Packit bc1512
              gfloat value;
Packit bc1512
              gfloat nx,ny;
Packit bc1512
Packit bc1512
              nx = (x + result->x);
Packit bc1512
              ny = (y + result->y);
Packit bc1512
Packit bc1512
              nx = (nx/512);
Packit bc1512
              ny = (ny/512);
Packit bc1512
Packit bc1512
              value = mandel_calc (o, nx, ny);
Packit bc1512
Packit bc1512
              *dst++ = value;
Packit bc1512
            }
Packit bc1512
        }
Packit bc1512
    }
Packit bc1512
Packit bc1512
  gegl_buffer_set (output, NULL, 0, babl_format ("Y float"), buf,
Packit bc1512
                   GEGL_AUTO_ROWSTRIDE);
Packit bc1512
  g_free (buf);
Packit bc1512
Packit bc1512
  return  TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass       *operation_class;
Packit bc1512
  GeglOperationSourceClass *source_class;
Packit bc1512
Packit bc1512
  operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  source_class    = GEGL_OPERATION_SOURCE_CLASS (klass);
Packit bc1512
Packit bc1512
  source_class->process = process;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
  operation_class->get_bounding_box = get_bounding_box;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name"        , "gegl:mandelbrot",
Packit bc1512
    "categories"  , "render",
Packit bc1512
    "description" , _("Mandelbrot set renderer"),
Packit bc1512
    NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif