2010-01-15 05:53:15 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2010-01-21 13:18:27 +00:00
|
|
|
* $Id$
|
2010-01-15 05:53:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Matthias Wientapper
|
|
|
|
* Heavily extended 2005 Jens Arnold
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2010-01-21 12:27:07 +00:00
|
|
|
#include "fractal_sets.h"
|
2010-01-15 05:53:15 +00:00
|
|
|
#include "mandelbrot_set.h"
|
|
|
|
|
|
|
|
#define BUTTON_YIELD_TIMEOUT (HZ / 4)
|
|
|
|
|
2010-01-15 06:17:56 +00:00
|
|
|
#ifdef USEGSLIB
|
2010-01-15 11:48:21 +00:00
|
|
|
static unsigned char imgbuffer[LCD_HEIGHT];
|
2010-01-15 06:17:56 +00:00
|
|
|
#else
|
2010-01-15 11:48:21 +00:00
|
|
|
static fb_data imgbuffer[LCD_HEIGHT];
|
2010-01-15 06:17:56 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-30 07:16:16 +00:00
|
|
|
#ifdef USEGSLIB
|
2010-01-30 08:35:16 +00:00
|
|
|
#define LCOLOR(iter) ((iter ^ 7) << 5)
|
2010-01-30 07:16:16 +00:00
|
|
|
#else
|
2010-01-17 21:21:52 +00:00
|
|
|
/*
|
|
|
|
* Spread iter's colors over color range.
|
|
|
|
* 345 (=15*26-45) is max_iter maximal value
|
|
|
|
* This implementation ignores pixel format, thus it is not uniformly spread
|
|
|
|
*/
|
2010-01-30 07:16:16 +00:00
|
|
|
#define LCOLOR(iter) ((iter << LCD_DEPTH) / 345)
|
|
|
|
#endif
|
2010-01-17 21:21:52 +00:00
|
|
|
|
2010-01-15 05:53:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2014-06-18 05:15:00 +00:00
|
|
|
#define COLOR(iter) FB_SCALARPACK(LCOLOR(iter))
|
|
|
|
#define CONVERGENCE_COLOR FB_RGBPACK(0, 0, 0)
|
2010-01-15 05:53:15 +00:00
|
|
|
#else /* greyscale */
|
2010-01-17 21:21:52 +00:00
|
|
|
#define COLOR(iter) (unsigned char)LCOLOR(iter)
|
2010-01-15 12:00:57 +00:00
|
|
|
#define CONVERGENCE_COLOR 0
|
2010-01-15 05:53:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_LCD == LCD_SSD1815
|
|
|
|
/* Recorder, Ondio: pixel_height == 1.25 * pixel_width */
|
|
|
|
#define MB_HEIGHT (LCD_HEIGHT*5/4)
|
|
|
|
#else
|
|
|
|
/* square pixels */
|
|
|
|
#define MB_HEIGHT LCD_HEIGHT
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
#define MB_XOFS (-0x03000000L) /* -0.75 (s5.26) */
|
|
|
|
#if (3000 * MB_HEIGHT / LCD_WIDTH) >= 2400 /* width is limiting factor */
|
|
|
|
#define MB_XFAC (0x06000000LL) /* 1.5 (s5.26) */
|
|
|
|
#define MB_YFAC (MB_XFAC*MB_HEIGHT / LCD_WIDTH)
|
|
|
|
#else /* height is limiting factor */
|
|
|
|
#define MB_YFAC (0x04cccccdLL) /* 1.2 (s5.26) */
|
|
|
|
#define MB_XFAC (MB_YFAC*LCD_WIDTH / MB_HEIGHT)
|
2010-01-15 05:53:15 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-15 11:48:21 +00:00
|
|
|
#ifndef USEGSLIB
|
2010-01-15 05:53:15 +00:00
|
|
|
#define UPDATE_FREQ (HZ/50)
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
/* fixed point format s5.26: sign, 5 bits integer part, 26 bits fractional part */
|
2010-01-19 20:05:19 +00:00
|
|
|
struct fractal_ops *ops;
|
|
|
|
long x_min;
|
|
|
|
long x_max;
|
|
|
|
long x_step;
|
|
|
|
long x_delta;
|
|
|
|
long y_min;
|
|
|
|
long y_max;
|
|
|
|
long y_step;
|
|
|
|
long y_delta;
|
|
|
|
int step_log2;
|
|
|
|
unsigned max_iter;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
static void mandelbrot_init(void);
|
|
|
|
|
|
|
|
static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
|
|
|
|
int (*button_yield_cb)(void *), void *button_yield_ctx);
|
|
|
|
|
|
|
|
static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
|
|
|
|
int (*button_yield_cb)(void *), void *button_yield_ctx);
|
|
|
|
|
|
|
|
static void mandelbrot_move(int dx, int dy);
|
|
|
|
|
2010-01-17 21:15:56 +00:00
|
|
|
static int mandelbrot_zoom(int factor);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
static int mandelbrot_precision(int d);
|
|
|
|
|
|
|
|
struct fractal_ops mandelbrot_ops =
|
|
|
|
{
|
|
|
|
.init = mandelbrot_init,
|
|
|
|
.calc = NULL,
|
|
|
|
.move = mandelbrot_move,
|
|
|
|
.zoom = mandelbrot_zoom,
|
|
|
|
.precision = mandelbrot_precision,
|
|
|
|
};
|
|
|
|
|
2010-01-17 21:15:56 +00:00
|
|
|
#define LOG2_OUT_OF_BOUNDS -32767
|
|
|
|
|
2010-01-15 05:53:15 +00:00
|
|
|
static int ilog2_fp(long value) /* calculate integer log2(value_fp_6.26) */
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (value <= 0)
|
|
|
|
{
|
2010-01-17 21:15:56 +00:00
|
|
|
return LOG2_OUT_OF_BOUNDS;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
else if (value > (1L << 26))
|
|
|
|
{
|
|
|
|
while (value >= (2L << 26))
|
|
|
|
{
|
|
|
|
value >>= 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-28 18:58:43 +00:00
|
|
|
while (value < (1L << 26))
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
|
|
|
value <<= 1;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-01-17 21:15:56 +00:00
|
|
|
static int recalc_parameters(void)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
2010-01-19 20:05:19 +00:00
|
|
|
x_step = (x_max - x_min) / LCD_WIDTH;
|
|
|
|
y_step = (y_max - y_min) / LCD_HEIGHT;
|
|
|
|
step_log2 = ilog2_fp(MIN(x_step, y_step));
|
2010-01-17 21:15:56 +00:00
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
if (step_log2 == LOG2_OUT_OF_BOUNDS)
|
2010-01-17 21:15:56 +00:00
|
|
|
return 1; /* out of bounds */
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
x_delta = X_DELTA(x_step);
|
|
|
|
y_delta = Y_DELTA(y_step);
|
|
|
|
max_iter = MAX(15, -15 * step_log2 - 45);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
ops->calc = (step_log2 <= -10) ?
|
2010-01-15 05:53:15 +00:00
|
|
|
mandelbrot_calc_high_prec : mandelbrot_calc_low_prec;
|
2010-01-17 21:15:56 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mandelbrot_init(void)
|
|
|
|
{
|
2010-01-19 20:05:19 +00:00
|
|
|
ops = &mandelbrot_ops;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
x_min = MB_XOFS - MB_XFAC;
|
|
|
|
x_max = MB_XOFS + MB_XFAC;
|
|
|
|
y_min = -MB_YFAC;
|
|
|
|
y_max = MB_YFAC;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
recalc_parameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
|
|
|
|
int (*button_yield_cb)(void *), void *button_yield_ctx)
|
|
|
|
{
|
|
|
|
#ifndef USEGSLIB
|
|
|
|
long next_update = *rb->current_tick;
|
|
|
|
int last_px = rect->px_min;
|
|
|
|
#endif
|
|
|
|
unsigned n_iter;
|
|
|
|
long a32, b32;
|
|
|
|
short x, x2, y, y2, a, b;
|
|
|
|
int p_x, p_y;
|
|
|
|
unsigned long last_yield = *rb->current_tick;
|
|
|
|
unsigned long last_button_yield = *rb->current_tick;
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
a32 = x_min + x_step * rect->px_min;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
for (p_x = rect->px_min; p_x < rect->px_max; p_x++)
|
|
|
|
{
|
|
|
|
a = a32 >> 16;
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
b32 = y_min + y_step * (LCD_HEIGHT - rect->py_max);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--)
|
|
|
|
{
|
|
|
|
b = b32 >> 16;
|
|
|
|
x = a;
|
|
|
|
y = b;
|
|
|
|
n_iter = 0;
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
while (++n_iter <= max_iter)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
|
|
|
x2 = MULS16_ASR10(x, x);
|
|
|
|
y2 = MULS16_ASR10(y, y);
|
|
|
|
|
|
|
|
if (x2 + y2 > (4<<10)) break;
|
|
|
|
|
|
|
|
y = 2 * MULS16_ASR10(x, y) + b;
|
|
|
|
x = x2 - y2 + a;
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
if (n_iter > max_iter)
|
2010-01-15 12:00:57 +00:00
|
|
|
imgbuffer[p_y] = CONVERGENCE_COLOR;
|
2010-01-15 05:53:15 +00:00
|
|
|
else
|
2010-01-15 12:00:57 +00:00
|
|
|
imgbuffer[p_y] = COLOR(n_iter);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
/* be nice to other threads:
|
|
|
|
* if at least one tick has passed, yield */
|
|
|
|
if (TIME_AFTER(*rb->current_tick, last_yield))
|
|
|
|
{
|
|
|
|
rb->yield();
|
|
|
|
last_yield = *rb->current_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TIME_AFTER(*rb->current_tick, last_button_yield))
|
|
|
|
{
|
|
|
|
if (button_yield_cb(button_yield_ctx))
|
|
|
|
{
|
|
|
|
#ifndef USEGSLIB
|
|
|
|
/* update screen part that was changed since last yield */
|
|
|
|
rb->lcd_update_rect(last_px, rect->py_min,
|
|
|
|
p_x - last_px + 1, rect->py_max - rect->py_min);
|
|
|
|
#endif
|
|
|
|
rect->px_min = (p_x == 0) ? 0 : p_x - 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
b32 += y_step;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
#ifdef USEGSLIB
|
|
|
|
grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1,
|
|
|
|
p_x, rect->py_min, 1, rect->py_max - rect->py_min);
|
|
|
|
#else
|
|
|
|
rb->lcd_bitmap_part(imgbuffer, 0, rect->py_min, 1,
|
|
|
|
p_x, rect->py_min, 1, rect->py_max - rect->py_min);
|
|
|
|
|
|
|
|
if ((p_x == rect->px_max - 1) ||
|
|
|
|
TIME_AFTER(*rb->current_tick, next_update))
|
|
|
|
{
|
|
|
|
next_update = *rb->current_tick + UPDATE_FREQ;
|
|
|
|
|
|
|
|
/* update screen part that was changed since last yield */
|
|
|
|
rb->lcd_update_rect(last_px, rect->py_min,
|
|
|
|
p_x - last_px + 1, rect->py_max - rect->py_min);
|
|
|
|
last_px = p_x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
a32 += x_step;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rect->valid = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
|
|
|
|
int (*button_yield_cb)(void *), void *button_yield_ctx)
|
|
|
|
{
|
|
|
|
#ifndef USEGSLIB
|
|
|
|
long next_update = *rb->current_tick;
|
|
|
|
int last_px = rect->px_min;
|
|
|
|
#endif
|
|
|
|
unsigned n_iter;
|
|
|
|
long x, x2, y, y2, a, b;
|
|
|
|
int p_x, p_y;
|
|
|
|
unsigned long last_yield = *rb->current_tick;
|
|
|
|
unsigned long last_button_yield = *rb->current_tick;
|
|
|
|
|
|
|
|
MULS32_INIT();
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
a = x_min + x_step * rect->px_min;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
for (p_x = rect->px_min; p_x < rect->px_max; p_x++)
|
|
|
|
{
|
2010-01-19 20:05:19 +00:00
|
|
|
b = y_min + y_step * (LCD_HEIGHT - rect->py_max);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--)
|
|
|
|
{
|
|
|
|
x = a;
|
|
|
|
y = b;
|
|
|
|
n_iter = 0;
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
while (++n_iter <= max_iter)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
|
|
|
x2 = MULS32_ASR26(x, x);
|
|
|
|
y2 = MULS32_ASR26(y, y);
|
|
|
|
|
|
|
|
if (x2 + y2 > (4L<<26)) break;
|
|
|
|
|
|
|
|
y = 2 * MULS32_ASR26(x, y) + b;
|
|
|
|
x = x2 - y2 + a;
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
if (n_iter > max_iter)
|
2010-01-15 12:00:57 +00:00
|
|
|
imgbuffer[p_y] = CONVERGENCE_COLOR;
|
2010-01-15 05:53:15 +00:00
|
|
|
else
|
2010-01-15 12:00:57 +00:00
|
|
|
imgbuffer[p_y] = COLOR(n_iter);
|
2010-01-15 05:53:15 +00:00
|
|
|
|
|
|
|
/* be nice to other threads:
|
|
|
|
* if at least one tick has passed, yield */
|
|
|
|
if (TIME_AFTER(*rb->current_tick, last_yield))
|
|
|
|
{
|
|
|
|
rb->yield();
|
|
|
|
last_yield = *rb->current_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TIME_AFTER(*rb->current_tick, last_button_yield))
|
|
|
|
{
|
|
|
|
if (button_yield_cb(button_yield_ctx))
|
|
|
|
{
|
|
|
|
#ifndef USEGSLIB
|
|
|
|
/* update screen part that was changed since last yield */
|
|
|
|
rb->lcd_update_rect(last_px, rect->py_min,
|
|
|
|
p_x - last_px + 1, rect->py_max - rect->py_min);
|
|
|
|
#endif
|
|
|
|
rect->px_min = (p_x == 0) ? 0 : p_x - 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
b += y_step;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
#ifdef USEGSLIB
|
|
|
|
grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1,
|
|
|
|
p_x, rect->py_min, 1, rect->py_max - rect->py_min);
|
|
|
|
#else
|
|
|
|
rb->lcd_bitmap_part(imgbuffer, 0, rect->py_min, 1,
|
|
|
|
p_x, rect->py_min, 1, rect->py_max - rect->py_min);
|
|
|
|
|
|
|
|
if ((p_x == rect->px_max - 1) ||
|
|
|
|
TIME_AFTER(*rb->current_tick, next_update))
|
|
|
|
{
|
|
|
|
next_update = *rb->current_tick + UPDATE_FREQ;
|
|
|
|
|
|
|
|
/* update screen part that was changed since last yield */
|
|
|
|
rb->lcd_update_rect(last_px, rect->py_min,
|
|
|
|
p_x - last_px + 1, rect->py_max - rect->py_min);
|
|
|
|
last_px = p_x;
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-19 20:05:19 +00:00
|
|
|
a += x_step;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rect->valid = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
static void mandelbrot_move(int x_factor, int y_factor)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
2010-03-28 18:58:43 +00:00
|
|
|
long dx = (long)x_factor * x_delta;
|
|
|
|
long dy = (long)y_factor * y_delta;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
x_min += dx;
|
|
|
|
x_max += dx;
|
|
|
|
y_min += dy;
|
|
|
|
y_max += dy;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 21:15:56 +00:00
|
|
|
static int mandelbrot_zoom(int factor)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
2010-01-17 21:15:56 +00:00
|
|
|
int res;
|
2010-01-19 20:05:19 +00:00
|
|
|
long factor_x = (long)factor * x_delta;
|
|
|
|
long factor_y = (long)factor * y_delta;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
2010-01-19 20:05:19 +00:00
|
|
|
x_min += factor_x;
|
|
|
|
x_max -= factor_x;
|
|
|
|
y_min += factor_y;
|
|
|
|
y_max -= factor_y;
|
2010-01-15 05:53:15 +00:00
|
|
|
|
2010-01-17 21:15:56 +00:00
|
|
|
res = recalc_parameters();
|
|
|
|
if (res) /* zoom not possible, revert */
|
2010-01-17 21:21:52 +00:00
|
|
|
{
|
2010-01-17 21:15:56 +00:00
|
|
|
mandelbrot_zoom(-factor);
|
2010-01-17 21:21:52 +00:00
|
|
|
}
|
2010-01-17 21:15:56 +00:00
|
|
|
|
|
|
|
return res;
|
2010-01-15 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mandelbrot_precision(int d)
|
|
|
|
{
|
|
|
|
int changed = 0;
|
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
/* Increase precision */
|
2010-01-15 05:53:15 +00:00
|
|
|
for (; d > 0; d--)
|
|
|
|
{
|
2010-01-19 20:05:19 +00:00
|
|
|
max_iter += max_iter / 2;
|
2010-01-15 05:53:15 +00:00
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
|
2010-03-28 18:58:43 +00:00
|
|
|
/* Decrease precision */
|
2010-01-19 20:05:19 +00:00
|
|
|
for (; d < 0 && max_iter >= 15; d++)
|
2010-01-15 05:53:15 +00:00
|
|
|
{
|
2010-01-19 20:05:19 +00:00
|
|
|
max_iter -= max_iter / 3;
|
2010-01-15 05:53:15 +00:00
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|