From 3ef33f5a5d8814860ffe3d2934da180730e2237c Mon Sep 17 00:00:00 2001 From: go38coz Date: Fri, 26 Jun 2026 19:03:39 +0200 Subject: [PATCH] Added: Using only 1 Byte per pixel for grascale images to save_bmp --- Implementierung/main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Implementierung/main.c b/Implementierung/main.c index b71bea2..f9a848e 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -7,7 +7,7 @@ #include // for strcpy #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) { return 0; @@ -202,14 +202,14 @@ int main(int argc, char** argv) printf("yashar\n"); tricorn(complexA,width,height,resolution,n,colored,tempArr); printf("yashar\n"); - save_bmp(output_data,tempArr,width,height); + save_bmp(output_data,tempArr,width,height,colored); free(output_data); // 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 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); } - 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_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 file_size = image_size + 54; // File size = Image Size + Headers + size_t bytesPerColor = color ? 3 : 1; + // File header - contains information about the file unsigned char file_header[14] = { '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, // Height in pixels - set later 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, // Image size - set later 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 unsigned char padding_bytes[3] = {0, 0, 0}; - + 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 // 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) { fwrite(padding_bytes,1,row_padding,fptr); // Write row_padding number of padding bytes to the file stream }