Fixed grayscale handling in main.c and tricorn.c
Now grayscale files use a color pallete and in general should be smaller.
This commit is contained in:
+22
-8
@@ -191,13 +191,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
float complex complexA = (starting_point_real + starting_point_imaginary*I);
|
float complex complexA = (starting_point_real + starting_point_imaginary*I);
|
||||||
int bytesPerColour;
|
int bytesPerColour = colored ? 3 : 1;
|
||||||
if (colored)
|
|
||||||
{
|
|
||||||
bytesPerColour = 3;
|
|
||||||
} else {
|
|
||||||
bytesPerColour = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t absolute_height = height >= 0 ? height : height * - 1;
|
ssize_t absolute_height = height >= 0 ? height : height * - 1;
|
||||||
unsigned char *tempArr = malloc(width*absolute_height*bytesPerColour);// allocate image array on the heap to support large images as the stack is usually only 8 MB big
|
unsigned char *tempArr = malloc(width*absolute_height*bytesPerColour);// allocate image array on the heap to support large images as the stack is usually only 8 MB big
|
||||||
@@ -239,6 +233,7 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
|
|||||||
|
|
||||||
size_t image_size = actual_height * row_width;
|
size_t image_size = actual_height * row_width;
|
||||||
size_t file_size = image_size + 54; // File size = Image Size + Headers
|
size_t file_size = image_size + 54; // File size = Image Size + Headers
|
||||||
|
size_t header_offset = color ? 54 : 1078; // If colored then offset = 54 if not we add offset = 54 + 1024; 1024 Bytes for color pallete
|
||||||
|
|
||||||
|
|
||||||
// File header - contains information about the file
|
// File header - contains information about the file
|
||||||
@@ -246,7 +241,7 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
|
|||||||
'B', 'M', // BM - identifies file as a .bmp file
|
'B', 'M', // BM - identifies file as a .bmp file
|
||||||
0, 0, 0, 0, // 4 bytes for file size - will be set later
|
0, 0, 0, 0, // 4 bytes for file size - will be set later
|
||||||
0, 0, 0, 0, // Reserved
|
0, 0, 0, 0, // Reserved
|
||||||
54, 0, 0, 0 // 4 bytes that specify the offset where the image array begins
|
0, 0, 0, 0 // 4 bytes that specify the offset where the image array begins
|
||||||
};
|
};
|
||||||
|
|
||||||
// Serialize the size into 4 bytes - (unsigned char) takes only bottom 4 bytes 1 2 3 4
|
// Serialize the size into 4 bytes - (unsigned char) takes only bottom 4 bytes 1 2 3 4
|
||||||
@@ -255,6 +250,9 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
|
|||||||
file_header[4] = (unsigned char)(file_size >> 16);
|
file_header[4] = (unsigned char)(file_size >> 16);
|
||||||
file_header[5] = (unsigned char)(file_size >> 24);
|
file_header[5] = (unsigned char)(file_size >> 24);
|
||||||
|
|
||||||
|
file_header[10] = (unsigned char)(header_offset);
|
||||||
|
file_header[11] = (unsigned char)(header_offset >> 8);
|
||||||
|
|
||||||
// Info header - contains information about the image itself
|
// Info header - contains information about the image itself
|
||||||
unsigned char info_header[40] = {
|
unsigned char info_header[40] = {
|
||||||
40, 0, 0, 0, // Size of this header
|
40, 0, 0, 0, // Size of this header
|
||||||
@@ -294,6 +292,22 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
|
|||||||
fwrite(file_header,1,14,fptr);
|
fwrite(file_header,1,14,fptr);
|
||||||
fwrite(info_header,1,40,fptr);
|
fwrite(info_header,1,40,fptr);
|
||||||
|
|
||||||
|
|
||||||
|
//If image is grayscale write color dictionary to tell reader what color to visualise for each value
|
||||||
|
// Row 0 - Value 0: [R=0;B=0;G=0]
|
||||||
|
// ...
|
||||||
|
//Row i - Value i: [R=i;B=i;G=i]
|
||||||
|
if (!color) {
|
||||||
|
unsigned char palette[1024];
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
palette[i * 4 + 0] = i; // B
|
||||||
|
palette[i * 4 + 1] = i; // G
|
||||||
|
palette[i * 4 + 2] = i; // R
|
||||||
|
palette[i * 4 + 3] = 0; // Reserved (Padding)
|
||||||
|
}
|
||||||
|
fwrite(palette, 1, 1024, fptr);
|
||||||
|
}
|
||||||
|
|
||||||
// Save padding bytes as array so we can write from it easily to the file later
|
// Save padding bytes as array so we can write from it easily to the file later
|
||||||
unsigned char padding_bytes[3] = {0, 0, 0};
|
unsigned char padding_bytes[3] = {0, 0, 0};
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig
|
|||||||
|
|
||||||
// Absolute height for iteration purposes
|
// Absolute height for iteration purposes
|
||||||
height = height < 0 ? height*-1 : height;
|
height = height < 0 ? height*-1 : height;
|
||||||
|
unsigned bytesPerColor = color ? 3 : 1;
|
||||||
|
|
||||||
// Simple iteration over every pixel in the picture
|
// Simple iteration over every pixel in the picture
|
||||||
for(size_t i = 0; i < width; i++){
|
for(size_t i = 0; i < width; i++){
|
||||||
@@ -15,13 +16,11 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig
|
|||||||
unsigned value = converge(start + (i*res - I*j*res),n);
|
unsigned value = converge(start + (i*res - I*j*res),n);
|
||||||
|
|
||||||
// Pixel value position in the output array
|
// Pixel value position in the output array
|
||||||
size_t position = (j*width +i) *3;
|
size_t position = (j*width +i) * bytesPerColor;
|
||||||
|
|
||||||
|
|
||||||
if(!color){ // Grayscale colors as specified by the assignment r = g = b
|
if(!color){ // Grayscale colors as specified by the assignment r = g = b
|
||||||
img[position] = value;
|
img[position] = value;
|
||||||
img[position + 1] = value;
|
|
||||||
img[position + 2] = value;
|
|
||||||
}
|
}
|
||||||
else{ // Temporary colors with random multipliers
|
else{ // Temporary colors with random multipliers
|
||||||
img[position] = ((value*3) % 255);
|
img[position] = ((value*3) % 255);
|
||||||
|
|||||||
Reference in New Issue
Block a user