76 lines
2.6 KiB
C
76 lines
2.6 KiB
C
#include "tricorn_V2.h"
|
|
|
|
static unsigned converge(float c_real, float c_imag, unsigned n);
|
|
|
|
|
|
/**
|
|
* @brief Generates a Tricorn fractal image using an optimized scalar 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_V2(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) {
|
|
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 > 4.0f) {
|
|
return 255 - 4 * (i % 64);
|
|
}
|
|
|
|
imag = -2.0f * real * imag + c_imag;
|
|
real = real2 - imag2 + c_real;
|
|
}
|
|
return 0;
|
|
} |