| 1 | /* |
| 2 | This frei0r plugin generates isingnoise images |
| 3 | |
| 4 | Copyright (C) 2004 Georg Seidel <[email protected]> |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with this library; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <assert.h> |
| 23 | #include <math.h> |
| 24 | #include <string.h> |
| 25 | #if defined(_MSC_VER) |
| 26 | #define __STDC_LIMIT_MACROS |
| 27 | #endif /* _MSC_VER */ |
| 28 | #include "frei0r.h" |
| 29 | |
| 30 | //------------------------------------------------------------------------- |
| 31 | |
| 32 | struct IsingField { |
| 33 | char* s; |
| 34 | int xsize; |
| 35 | int ysize; |
| 36 | }; |
| 37 | |
| 38 | static void set_bf(uint32_t bf[3], double t, double b, double s); |
| 39 | static void init_field(struct IsingField* f, int xsize, int ysize); |
| 40 | static void destroy_field(struct IsingField* f); |
| 41 | static void do_step(struct IsingField* f, uint32_t bf[3]); |
| 42 | static void copy_field(const struct IsingField* f, uint32_t* framebuffer); |
| 43 | |
| 44 | //------------------------------------------------------------------------- |
| 45 | |
| 46 | #define MY_RAND_MAX UINT32_MAX |
| 47 | |
| 48 | static uint32_t rnd_lcg1_xn = 1; |
| 49 | |
| 50 | inline static uint32_t rnd_lcg1() |
| 51 | { |
| 52 | rnd_lcg1_xn *= 3039177861U; |
| 53 | |
| 54 | return rnd_lcg1_xn; |
| 55 | } |
| 56 | |
| 57 | #define my_rand() rnd_lcg1() |
| 58 | |
| 59 | |
| 60 | typedef struct ising0r_instance |
| 61 | { |
| 62 | unsigned int width; |
| 63 | unsigned int height; |
| 64 | |
| 65 | double temp; |
| 66 | double border_growth; |
| 67 | double spont_growth; |
| 68 | |
| 69 | struct IsingField f; |
| 70 | uint32_t bf[3]; |
| 71 | } ising0r_instance_t; |
| 72 | |
| 73 | int f0r_init() |
| 74 | { |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | void f0r_deinit() |
| 79 | { /* no initialization required */ } |
| 80 | |
| 81 | void f0r_get_plugin_info(f0r_plugin_info_t* nois0rInfo) |
| 82 | { |
| 83 | nois0rInfo->name = "Ising0r"; |
| 84 | nois0rInfo->author = "Gephex crew"; |
| 85 | nois0rInfo->plugin_type = F0R_PLUGIN_TYPE_SOURCE; |
| 86 | nois0rInfo->color_model = F0R_COLOR_MODEL_PACKED32; |
| 87 | nois0rInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 88 | nois0rInfo->major_version = 0; |
| 89 | nois0rInfo->minor_version = 9; |
| 90 | nois0rInfo->num_params = 3; |
| 91 | nois0rInfo->explanation = "Generates ising noise"; |
| 92 | } |
| 93 | |
| 94 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 95 | { |
| 96 | switch (param_index) |
| 97 | { |
| 98 | case 0: |
| 99 | info->name = "Temperature"; |
| 100 | info->type = F0R_PARAM_DOUBLE; |
| 101 | info->explanation = "Noise Temperature"; break; |
| 102 | case 1: |
| 103 | info->name = "Border Growth"; |
| 104 | info->type = F0R_PARAM_DOUBLE; |
| 105 | info->explanation = "Border Growth"; break; |
| 106 | case 2: |
| 107 | info->name = "Spontaneous Growth"; |
| 108 | info->type = F0R_PARAM_DOUBLE; |
| 109 | info->explanation = "Spontaneous Growth"; break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 114 | { |
| 115 | ising0r_instance_t* inst = (ising0r_instance_t*)calloc(1, sizeof(*inst)); |
| 116 | inst->width = width; |
| 117 | inst->height = height; |
| 118 | |
| 119 | init_field(&inst->f, width, height); |
| 120 | |
| 121 | return (f0r_instance_t)inst; |
| 122 | } |
| 123 | |
| 124 | void f0r_destruct(f0r_instance_t instance) |
| 125 | { |
| 126 | ising0r_instance_t* inst = (ising0r_instance_t*)instance; |
| 127 | destroy_field(&inst->f); |
| 128 | free(inst); |
| 129 | } |
| 130 | |
| 131 | void f0r_set_param_value(f0r_instance_t instance, |
| 132 | f0r_param_t param, int param_index) |
| 133 | { |
| 134 | ising0r_instance_t* inst = (ising0r_instance_t*)instance; |
| 135 | |
| 136 | f0r_param_double* p = (f0r_param_double*) param; |
| 137 | |
| 138 | switch (param_index) |
| 139 | { |
| 140 | case 0: |
| 141 | inst->temp = *p *6; break; |
| 142 | case 1: |
| 143 | inst->border_growth = (1.0 - *p)*100; break; |
| 144 | case 2: |
| 145 | inst->spont_growth = (1.0 - *p)*100; break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void f0r_get_param_value(f0r_instance_t instance, |
| 150 | f0r_param_t param, int param_index) |
| 151 | { |
| 152 | ising0r_instance_t* inst = (ising0r_instance_t*)instance; |
| 153 | |
| 154 | f0r_param_double* p = (f0r_param_double*) param; |
| 155 | |
| 156 | switch (param_index) |
| 157 | { |
| 158 | case 0: |
| 159 | *p = inst->temp / 6; break; |
| 160 | case 1: |
| 161 | *p = 1.0 - inst->border_growth / 100; break; |
| 162 | case 2: |
| 163 | *p = 1.0 - inst->spont_growth / 100; break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void f0r_update(f0r_instance_t instance, double time, |
| 169 | const uint32_t* inframe, uint32_t* outframe) |
| 170 | { |
| 171 | assert(instance); |
| 172 | ising0r_instance_t* inst = (ising0r_instance_t*)instance; |
| 173 | |
| 174 | set_bf(inst->bf, inst->temp, inst->border_growth, inst->spont_growth); |
| 175 | |
| 176 | do_step(&inst->f, inst->bf); |
| 177 | |
| 178 | copy_field(&inst->f, outframe); |
| 179 | } |
| 180 | |
| 181 | //------------------------------------------------------------------------- |
| 182 | |
| 183 | static void set_bf(uint32_t bf[3], double t, double b, double s) |
| 184 | { |
| 185 | /* { |
| 186 | char buffer[128]; |
| 187 | |
| 188 | snprintf(buffer, sizeof(buffer), "Changing bf: (t,b,s)=(%f,%f,%f)\n", |
| 189 | t, b, s); |
| 190 | |
| 191 | s_log(2, buffer); |
| 192 | }*/ |
| 193 | |
| 194 | bf[0] = (uint32_t) (0.5 * MY_RAND_MAX); |
| 195 | |
| 196 | if (t > 0) |
| 197 | { |
| 198 | bf[1] = (uint32_t) (exp(-b/t)*MY_RAND_MAX); |
| 199 | bf[2] = (uint32_t) (exp(-s/t)*MY_RAND_MAX); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | bf[1] = bf[2] = 0; |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | static void init_field(struct IsingField* f, int xsize, int ysize) |
| 209 | { |
| 210 | int x, y; |
| 211 | f->s = (char*) malloc(xsize*ysize); |
| 212 | |
| 213 | f->xsize = xsize; |
| 214 | f->ysize = ysize; |
| 215 | |
| 216 | // memset( |
| 217 | |
| 218 | for (y = 1; y < ysize-1; ++y) |
| 219 | { |
| 220 | int y_base = y*xsize; |
| 221 | for (x = 1; x < xsize-1; ++x) |
| 222 | { |
| 223 | f->s[x + y_base] = (my_rand() < MY_RAND_MAX/2) ? -1 : 1; |
| 224 | } |
| 225 | f->s[y_base] = f->s[xsize-1 + y_base] = 1; |
| 226 | } |
| 227 | |
| 228 | // set first and last line to black |
| 229 | memset(f->s, 1, xsize); |
| 230 | memset(f->s + (ysize-1)*xsize, 1, xsize); |
| 231 | } |
| 232 | |
| 233 | static void destroy_field(struct IsingField* f) |
| 234 | { |
| 235 | if (f->s != 0) |
| 236 | { |
| 237 | free(f->s); |
| 238 | f->s = 0; |
| 239 | // plain paranoia... |
| 240 | f->xsize = 0; |
| 241 | f->ysize = 0; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static void do_step(struct IsingField* f, uint32_t bf[3]) |
| 246 | { |
| 247 | int x, y; |
| 248 | int xsize = f->xsize; |
| 249 | int ysize = f->ysize; |
| 250 | |
| 251 | // start on second pixel of the second line (f->s[1][1]): |
| 252 | int8_t* current = f->s + xsize + 1; |
| 253 | |
| 254 | for (y = ysize-2; y > 0; --y) |
| 255 | { |
| 256 | for (x = xsize-2; x > 0; --x) |
| 257 | { |
| 258 | int sum = |
| 259 | current[-xsize] + current[xsize] + |
| 260 | current[-1] + current[1]; |
| 261 | |
| 262 | int e = *current * sum; |
| 263 | |
| 264 | if (e < 0 || my_rand() < bf[e>>1]) |
| 265 | { |
| 266 | *current *= -1; |
| 267 | } |
| 268 | |
| 269 | ++current; |
| 270 | } |
| 271 | // skip last pixel of this line and first pixel of next line: |
| 272 | current += 2; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void copy_field(const struct IsingField* f, uint32_t* framebuffer) |
| 277 | { |
| 278 | int i; |
| 279 | char* s = f->s; |
| 280 | uint32_t* fr = framebuffer; |
| 281 | |
| 282 | for (i = (f->xsize)*(f->ysize)-1; i >= 0; --i) |
| 283 | { |
| 284 | *(fr++) = *(s++); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | //------------------------------------------------------------------------- |
| 289 |