summaryrefslogtreecommitdiffstats
path: root/plug-ins/gimpressionist/plasma.c
blob: 82f921293a5376971938786d62ae5a265abb22a0 (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdlib.h>

#include <gtk/gtk.h>

#include <libgimpmath/gimpmath.h>
#include <libgimp/gimp.h>

#include "gimpressionist.h"
#include "ppmtool.h"
#include "random.h"

static int
pfix (int n)
{
  if (n < 1) return 1;
  if (n > 255) return 255;
  return n;
}

#define PIXEL(y,x,z) p->col[(y) * rowstride + (x) * 3 + z]

static void
mkplasma_sub (ppm_t *p, int x1, int x2, int y1, int y2, float turb)
{
  int rowstride = p->width * 3;
  int r=0;
  int xr, yr, nx, ny;

  xr = abs (x1 - x2);
  yr = abs (y1 - y2);

  if ((xr == 0) && (yr == 0))
    return;

  nx = (x1 + x2)/2;
  ny = (y1 + y2)/2;
  if (!PIXEL (y1,nx,r))
    PIXEL (y1, nx, r) = pfix ((PIXEL (y1, x1, r) + PIXEL (y1, x2, r)) / 2.0 +
                              turb * g_rand_double_range (random_generator,
                                                          -xr / 2.0,
                                                           xr / 2.0));
  if (!PIXEL (y2, nx, r))
    PIXEL (y2, nx, r) = pfix ((PIXEL (y2, x1, r) + PIXEL (y2, x2, r)) / 2.0 +
                              turb * g_rand_double_range (random_generator,
                                                          -xr / 2.0,
                                                           xr / 2.0));
  if (!PIXEL (ny, x1, r))
    PIXEL (ny, x1, r) = pfix ((PIXEL (y1, x1, r)+PIXEL (y2, x1, r)) / 2.0 +
                              turb * g_rand_double_range (random_generator,
                                                          -yr / 2.0,
                                                           yr / 2.0));
  if (!PIXEL (ny, x2, r))
    PIXEL (ny, x2, r) = pfix ((PIXEL (y1, x2, r) + PIXEL (y2, x2, r)) / 2.0 +
                              turb * g_rand_double_range (random_generator,
                                                          -yr / 2.0,
                                                           yr / 2.0));
  if (!PIXEL (ny, nx, r))
    PIXEL (ny, nx, r) =
      pfix ((PIXEL (y1, x1, r) + PIXEL (y1, x2, r) +
             PIXEL (y2, x1, r) + PIXEL( y2, x2, r)) / 4.0 +
            turb * g_rand_double_range (random_generator,
                                        -(xr + yr)/4.0,
                                         (xr + yr)/4.0));

  if (xr>1)
    {
      mkplasma_sub (p, x1, nx, y1, ny, turb);
      mkplasma_sub (p, nx, x2, y1, ny, turb);
    }
  if (yr>1)
    {
      mkplasma_sub (p, x1, nx, ny, y2, turb);
      mkplasma_sub (p, nx, x2, ny, y2, turb);
    }
}

static void
mkplasma_red (ppm_t *p, float turb)
{
  int x = 0, y = 0;
  int rowstride = p->width * 3;

  for (x = 0; x < p->width; x++)
    for (y = 0; y < p->height; y++)
      PIXEL (y, x, 0) = 0;
  x--; y--;
  PIXEL (0, 0, 0) = g_rand_int_range (random_generator, 1, 256);
  PIXEL (y, 0, 0) = g_rand_int_range (random_generator, 1, 256);
  PIXEL (0, x, 0) = g_rand_int_range (random_generator, 1, 256);
  PIXEL (y, x, 0) = g_rand_int_range (random_generator, 1, 256);
  mkplasma_sub (p, 0, x, 0, y, turb);
}

void
mkgrayplasma (ppm_t *p, float turb)
{
  int y, l;

  mkplasma_red (p, turb);
  l = p->width * 3 * p->height;
  for (y = 0; y < l; y += 3)
    p->col[y+1] = p->col[y+2] = p->col[y];
}