Added: Using only 1 Byte per pixel for grascale images to save_bmp

This commit is contained in:
go38coz
2026-06-26 19:03:39 +02:00
parent 6bb65eea7e
commit 3ef33f5a5d
+8 -7
View File
@@ -7,7 +7,7 @@
#include <string.h> // for strcpy #include <string.h> // for strcpy
#include "tricorn.h" #include "tricorn.h"
void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height); void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height,_Bool color);
unsigned tests(void) { unsigned tests(void) {
return 0; return 0;
@@ -202,14 +202,14 @@ int main(int argc, char** argv)
printf("yashar\n"); printf("yashar\n");
tricorn(complexA,width,height,resolution,n,colored,tempArr); tricorn(complexA,width,height,resolution,n,colored,tempArr);
printf("yashar\n"); printf("yashar\n");
save_bmp(output_data,tempArr,width,height); save_bmp(output_data,tempArr,width,height,colored);
free(output_data); // free(output_data); //
return 0; return 0;
} }
void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height) { void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height,_Bool color) {
// Open a file at the filepath given as a stream in the program // Open a file at the filepath given as a stream in the program
FILE *fptr = fopen(filename,"wb"); FILE *fptr = fopen(filename,"wb");
@@ -218,7 +218,6 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int actual_height = (height < 0) ? -height : height; // Calculate absolute height for iteration and calculation purposes int actual_height = (height < 0) ? -height : height; // Calculate absolute height for iteration and calculation purposes
size_t row_padding = (4 - (width) * 3 % 4) % 4; // Calculate padding for each row as .bmp is 4 bytes aligned size_t row_padding = (4 - (width) * 3 % 4) % 4; // Calculate padding for each row as .bmp is 4 bytes aligned
size_t row_width = width * 3 + row_padding; // Calculate row width with padding size_t row_width = width * 3 + row_padding; // Calculate row width with padding
@@ -226,6 +225,8 @@ 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 bytesPerColor = color ? 3 : 1;
// File header - contains information about the file // File header - contains information about the file
unsigned char file_header[14] = { unsigned char file_header[14] = {
'B', 'M', // BM - identifies file as a .bmp file 'B', 'M', // BM - identifies file as a .bmp file
@@ -246,7 +247,7 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
0, 0, 0, 0, // Width in pixels - set later 0, 0, 0, 0, // Width in pixels - set later
0, 0, 0, 0, // Height in pixels - set later 0, 0, 0, 0, // Height in pixels - set later
1, 0, // Number of color planes - always 1 1, 0, // Number of color planes - always 1
24, 0, // Number of bits per pixel or color depth bytesPerColor * 8, 0, // Number of bits per pixel or color depth
0, 0, 0, 0, // Compression method - 0 for no compression 0, 0, 0, 0, // Compression method - 0 for no compression
0, 0, 0, 0, // Image size - set later 0, 0, 0, 0, // Image size - set later
0x13, 0x0B, 0, 0, // Horizontal resolution in pixels per metre - Set to standart 72 pixels per inch 0x13, 0x0B, 0, 0, // Horizontal resolution in pixels per metre - Set to standart 72 pixels per inch
@@ -281,11 +282,11 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
// 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};
for(int i = 0; i < actual_height; i++) { for(int i = 0; i < actual_height; i++) {
// Write whole row of pixels - img + (i * width * 3) is the position of the row in the image array // Write whole row of pixels - img + (i * width * 3) is the position of the row in the image array
// size = 3 as we are interpreting every pixel as an object // size = 3 as we are interpreting every pixel as an object
fwrite(img + (i * width * 3), 3, width, fptr); fwrite(img + (i * width * bytesPerColor), bytesPerColor, width, fptr);
if(row_padding > 0) { if(row_padding > 0) {
fwrite(padding_bytes,1,row_padding,fptr); // Write row_padding number of padding bytes to the file stream fwrite(padding_bytes,1,row_padding,fptr); // Write row_padding number of padding bytes to the file stream
} }