Files

54 lines
2.5 KiB
C

#include "tricorn_V1.h"
#include "color_table.h"
#include "converge_helper.h"
/**
* @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_V1(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 ? height : (ssize_t)(-height);
// Depending on the sign of height we generate the image top-down or bottom up
// As we iterate from the top always, if it is negative we would have to start at the end of the imaginary part and go in the oposite direction when generating
ssize_t imag_direction = height < 0 ? -1 : 1;
float c_start_real = creal(start);
float c_start_imag = height > 0 ? cimag(start) : cimag(start) + ((actual_height-1) * res);
float vertical_step = imag_direction * res;
size_t position = 0; // Start value for the position in the image buffer. Will be accumulated later
float c_imag = c_start_imag; // Start value for the imaginary part, which will be accumulated
for(ssize_t y = 0; y < actual_height; y++){
float c_real = c_start_real;
for(size_t x = 0; x < width; x++){
unsigned value = converge(c_real,c_imag,n);
if(!color){ // Grayscale colors as specified by the assignment r = g = b
img[position] = value;
}
else{ // Temporary colors with random multipliers
img[position] = color_lookup_table[value][0];
img[position + 1] = color_lookup_table[value][1];
img[position + 2] = color_lookup_table[value][2];
}
position += bytesPerColor; // Caculate position for next pixel
c_real += res; // Calculate real part for next pixel
}
c_imag += vertical_step; // For reach row calculate the next value for the imaginary part by accumulation
}
}