From b630fa965ec61b161396930e341b7ca862236f10 Mon Sep 17 00:00:00 2001 From: Yashar Alan Zenker Date: Sat, 4 Jul 2026 15:28:51 +0200 Subject: [PATCH] Fixed tricorn_V3.c to correctly handle complex numbers and add threaded V4 skeleton --- Implementierung/main.c | 12 ++++-- Implementierung/tricorn_V3.c | 7 ++-- Implementierung/tricorn_V4.c | 77 ++++++++++++++++++++++++++++++++++++ Implementierung/tricorn_V4.h | 5 +++ 4 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 Implementierung/tricorn_V4.c create mode 100644 Implementierung/tricorn_V4.h diff --git a/Implementierung/main.c b/Implementierung/main.c index 4df4c51..431fd96 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -9,6 +9,7 @@ #include "tricorn_V1.h" #include "tricorn_V2.h" #include "tricorn_V3.h" +#include "tricorn_V4.h" void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height,_Bool color); @@ -206,7 +207,7 @@ int main(int argc, char** argv) switch (version_to_use) { case 0: - //Uses highly optimized SIMD version + // Uses highly optimized SIMD version tricorn_V0(complexA,width,height,resolution,n,colored,tempArr); break; case 1: @@ -214,14 +215,17 @@ int main(int argc, char** argv) tricorn_V1(complexA,width,height,resolution,n,colored,tempArr); break; case 2: - //Uses optimized SISD version + // Uses optimized SISD version tricorn_V2(complexA,width,height,resolution,n,colored,tempArr); break; case 3: - //Uses unoptimised comparison implementation + // Uses unoptimised comparison implementation tricorn_V3(complexA,width,height,resolution,n,colored,tempArr); break; - + case 4: + // Uses non SIMD threaded version + tricorn_V4(complexA,width,height,resolution,n,colored,tempArr); + break; default: break; } diff --git a/Implementierung/tricorn_V3.c b/Implementierung/tricorn_V3.c index 1c0ede2..f421cfe 100644 --- a/Implementierung/tricorn_V3.c +++ b/Implementierung/tricorn_V3.c @@ -15,16 +15,15 @@ static unsigned converge(float complex position,unsigned n); */ void tricorn_V3(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ - // Absolute height for iteration purposes - unsigned imag_direction = height < 0 ? -1 : 1; - height = height < 0 ? height*-1 : height; + float imag_direction = height < 0 ? -1.0f : 1.0f; unsigned bytesPerColor = color ? 3 : 1; + height = height < 0? -height : height; // Simple iteration over every pixel in the picture for(size_t i = 0; i < width; i++){ for(ssize_t j = 0; j < height; j++){ - unsigned value = converge(start + (i*res - imag_direction*j*res),n); + unsigned value = converge(start + (i*res - I* imag_direction*j*res),n); // Pixel value position in the output array size_t position = (j*width +i) * bytesPerColor; diff --git a/Implementierung/tricorn_V4.c b/Implementierung/tricorn_V4.c new file mode 100644 index 0000000..8295593 --- /dev/null +++ b/Implementierung/tricorn_V4.c @@ -0,0 +1,77 @@ +#include "tricorn_V2.h" + +static unsigned converge(float c_real, float c_imag, unsigned n); + + +/** + * @brief Generates a Tricorn fractal image using an optimized SISD implementation. + * @param start The starting complex coordinate (bottom-left or top-left depending on height sign). + * @param width The width of the target image in pixels. + * @param height The height of the target image. A negative height reverses the Y-axis calculation. + * @param res The step size (resolution) per pixel in the complex plane. + * @param n The maximum number of mathematical iterations per pixel. + * @param color If true, generates a 24-bit RGB image. If false, writes a grayscale image. + * @param img Pointer to the pre-allocated memory buffer where the bitmap data will be stored. + */ +void tricorn_V4(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ + int bytesPerColor = color ? 3 : 1; + + // Absolute height for iteration purposes + ssize_t actual_height = height < 0 ? (ssize_t)(-height) : height; + ssize_t imag_step = height < 0 ? -1:1; + + float c_start_real = creal(start); + float c_start_imag = cimag(start); + + float c_imag = c_start_imag; + float vertical_step = imag_step * res; + size_t position = 0; + + + // Simple iteration over every pixel in the picture + for(ssize_t y = 0; y < actual_height; y++){ + + // Calculate imaginary part on each row as it stays the same + + float c_real = c_start_real; + for(size_t x = 0; x < width; x++){ + unsigned value = converge(c_real,c_imag,n); + + // Pixel value position in the output array + + if(!color){ // Grayscale colors as specified by the assignment r = g = b + img[position] = value; + } + else{ // Temporary colors with random multipliers + img[position] = ((value*3) % 255); + img[position + 1] = ((value*2) % 255) ; + img[position + 2] = ((value*5) % 255); + } + + position += bytesPerColor; + c_real += res; + + } + c_imag -= vertical_step; + } +} + +// Iterative application of the equation given by the assignment +static inline unsigned converge(float c_real, float c_imag, unsigned n) { + const float convergenceLimit = 4.0f; + float real = 0.0f; + float imag = 0.0f; + + for (unsigned i = 0; i < n; i++) { + float real2 = real * real; + float imag2 = imag * imag; + + if (real2 + imag2 > convergenceLimit) { + return 255 - 4 * (i % 64); + } + + imag = -2.0f * real * imag + c_imag; + real = real2 - imag2 + c_real; + } + return 0; +} \ No newline at end of file diff --git a/Implementierung/tricorn_V4.h b/Implementierung/tricorn_V4.h new file mode 100644 index 0000000..528f773 --- /dev/null +++ b/Implementierung/tricorn_V4.h @@ -0,0 +1,5 @@ +#include +#include +#include +#include +void tricorn_V4(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img); \ No newline at end of file