Added: Using only 1 Byte per pixel for grascale images to save_bmp
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#include <string.h> // 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
|
||||
@@ -285,7 +286,7 @@ void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t he
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user