diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-09-20 16:01:46 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-09-20 16:01:46 +0200 |
| commit | 5efde1e3e65af769cb629d55f0a4dd4f87caebe9 (patch) | |
| tree | 88c685fad561e39fac869a36a93e98866e2ccb62 /mandelbrot.c | |
| parent | 95b209426dd7a9f844cf1aa093a2b1c4301f049b (diff) | |
| download | mandelbrot_cpu-5efde1e3e65af769cb629d55f0a4dd4f87caebe9.tar.gz mandelbrot_cpu-5efde1e3e65af769cb629d55f0a4dd4f87caebe9.tar.bz2 mandelbrot_cpu-5efde1e3e65af769cb629d55f0a4dd4f87caebe9.zip | |
Parallel computation, pixel array
- All the pixels are stored in an array, put in a surface then a texture
so we don't draw each pixel one by one.
- The computation of this array is done in parallel (one thread by line)
- Use sscanf instead of atoi hack to parse options
Diffstat (limited to 'mandelbrot.c')
| -rw-r--r-- | mandelbrot.c | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/mandelbrot.c b/mandelbrot.c index 9e7aa1b..1112575 100644 --- a/mandelbrot.c +++ b/mandelbrot.c @@ -1,5 +1,5 @@ #include <stdio.h> -#include <math.h> +#include <pthread.h> #include "header.h" #define PRINT_REAL_LO -2.0 @@ -13,13 +13,16 @@ #define IN_CHAR '*' #define OUT_CHAR ' ' +static void *pixel_row(void *args); + int mandelbrot_in_set(double ca, double cb) { double zr = ca; double zi = cb; double zr_square; double zi_square; - for (int n = 0; n < MAX_ITERATION; n++) + int n; + for (n = 0; n < MAX_ITERATION; n++) { zi_square = zi * zi; zr_square = zr * zr; @@ -31,43 +34,50 @@ int mandelbrot_in_set(double ca, double cb) zi += cb; zr += ca; } - return -1; + return n; } -ColorHexcode mandelbrot_in_set_color(Color *spectrum, double a, double b) +void *mandelbrot_pixels(double real_lo, double real_hi, double imag_lo, + double imag_hi, int width, int height, Color *palette) { - int steps = mandelbrot_in_set(a, b); - int brightness = (255 / MAX_ITERATION) * steps; - Color color; - color.rgb.r = brightness; - color.rgb.g = brightness; - color.rgb.b = brightness; - return color.hexcode; -} - -uint8_t *mandelbrot_create_bits(int width, int height) -{ - uint8_t *bits = (uint8_t*)malloc(3 * width * height); - if (bits == NULL) + Byte *pixels = (Byte*)malloc(width * height * PIXELS_CHANELS); + if (pixels == NULL) + return NULL; + pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t) * height); + if (threads == NULL) return NULL; - return bits; + for (int y = 0; y < height; y++) + { + ThreadArgs *args = (ThreadArgs*)malloc(sizeof(ThreadArgs)); + if (args == NULL) + return NULL; + args->real_lo = real_lo; + args->real_hi = real_hi; + args->width = width; + args->imag = map_range((double)y, 0, height, imag_lo, imag_hi); + args->palette = palette; + args->row = pixels + PIXELS_CHANELS * width * y; + pthread_create(&threads[y], NULL, pixel_row, args); + } + for (int y = 0; y < height; y++) + pthread_join(threads[y], NULL); + free(threads); + return (void*)pixels; } -void mandelbrot_set_bits(uint8_t *bits, Color *spectrum, Point center, double real_range, - double imag_range, int width, int height) +static void *pixel_row(void *void_args) { - double i = center.y - imag_range / 2; - double r = center.x - real_range / 2; - - for (int array_i = 0; array_i < height; array_i++) + ThreadArgs *args = (ThreadArgs*)void_args; + for (int x = 0; x < args->width; x++) { - for (int array_j = 0; array_j < width; array_j++) - { - bits[array_i * (int)height + array_j] = mandelbrot_in_set_color(spectrum, r, i); - r += real_range / width; - } - i += imag_range / height; + double a = map_range((double)x, 0, args->width, args->real_lo, args->real_hi); + Color color = args->palette[mandelbrot_in_set(a, args->imag)]; + args->row[x * PIXELS_CHANELS] = color.rgb.r; + args->row[x * PIXELS_CHANELS + 1] = color.rgb.g; + args->row[x * PIXELS_CHANELS + 2] = color.rgb.b; } + free(args); + return NULL; } void mandelbrot_print(void) |
