Move file descriptor into bitmap_file struct, renaming and other small tweaks
This commit is contained in:
parent
626c638ae5
commit
7774f0a1c6
24
lib/bmp.c
24
lib/bmp.c
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
#include "bmp.h"
|
#include "bmp.h"
|
||||||
|
|
||||||
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in) {
|
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap_header *bitmap_in) {
|
||||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
||||||
uint32_t bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
uint32_t bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
||||||
uint32_t bytes_padded_row = bytes_unpadded_row;
|
uint32_t bytes_padded_row = bytes_unpadded_row;
|
||||||
|
|
@ -13,12 +13,12 @@ uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in) {
|
||||||
return bytes_padded_row * bitmap_in->image_height;
|
return bytes_padded_row * bitmap_in->image_height;
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t calc_bitmap_file_size_bytes(const bitmap *bitmap_in) {
|
uint32_t calc_bitmap_file_size_bytes(const bitmap_header *bitmap_in) {
|
||||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
||||||
return sizeof(bitmap) + calc_bitmap_pixel_array_size_bytes(bitmap_in) - 2 ; /* Take away two to account for compiler padding struct */
|
return sizeof(bitmap_header) + calc_bitmap_pixel_array_size_bytes(bitmap_in) - 2 ; /* Take away two to account for compiler padding struct */
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t calc_bitmap_row_byte_position_start(const bitmap *bitmap_in, const uint32_t row_in) {
|
uint32_t calc_bitmap_row_byte_position_start(const bitmap_header *bitmap_in, const uint32_t row_in) {
|
||||||
uint32_t bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
uint32_t bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
||||||
uint32_t bytes_padded_row = bytes_unpadded_row;
|
uint32_t bytes_padded_row = bytes_unpadded_row;
|
||||||
if (bytes_padded_row % 4)
|
if (bytes_padded_row % 4)
|
||||||
|
|
@ -27,12 +27,11 @@ uint32_t calc_bitmap_row_byte_position_start(const bitmap *bitmap_in, const uint
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t calc_bitmap_header_size_bytes() {
|
uint32_t calc_bitmap_header_size_bytes() {
|
||||||
return sizeof(bitmap) - 2 ; /* Take away two to account for compiler padding struct */
|
return sizeof(bitmap_header) - 2 ; /* Take away two to account for compiler padding struct */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Change name to init_bitmap_header */
|
bitmap_header init_bitmap_header(const int32_t image_width_in, const int32_t image_height_in) {
|
||||||
bitmap init_bitmap(const int32_t image_width_in, const int32_t image_height_in) {
|
bitmap_header new_bitmap;
|
||||||
bitmap new_bitmap;
|
|
||||||
/* Bitmap Info Header */
|
/* Bitmap Info Header */
|
||||||
new_bitmap.info_header_size_bytes = 40;
|
new_bitmap.info_header_size_bytes = 40;
|
||||||
new_bitmap.image_width = image_width_in;
|
new_bitmap.image_width = image_width_in;
|
||||||
|
|
@ -58,8 +57,7 @@ bitmap init_bitmap(const int32_t image_width_in, const int32_t image_height_in)
|
||||||
return new_bitmap;
|
return new_bitmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Change the name to init_bitmap_file */
|
bitmap_file init_bitmap_file(const bitmap_header *bitmap_in, const char *filename) {
|
||||||
bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename) {
|
|
||||||
bitmap_file new_bitmap_file;
|
bitmap_file new_bitmap_file;
|
||||||
int8_t write_status;
|
int8_t write_status;
|
||||||
uint32_t current_byte;
|
uint32_t current_byte;
|
||||||
|
|
@ -79,19 +77,19 @@ bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename) {
|
||||||
/* TODO Need an error return type */
|
/* TODO Need an error return type */
|
||||||
return new_bitmap_file;
|
return new_bitmap_file;
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
|
|
||||||
new_bitmap_file.filename = filename;
|
new_bitmap_file.filename = filename;
|
||||||
new_bitmap_file.bitmap_metadata = bitmap_in;
|
new_bitmap_file.bitmap_metadata = bitmap_in;
|
||||||
|
new_bitmap_file.fd = fd;
|
||||||
return new_bitmap_file;
|
return new_bitmap_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
int write_bitmap_pixel(int8_t fd, const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in) {
|
int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in) {
|
||||||
int8_t write_status;
|
int8_t write_status;
|
||||||
int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in->bitmap_metadata, y_in) + ((x_in - 1) * 3);
|
int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in->bitmap_metadata, y_in) + ((x_in - 1) * 3);
|
||||||
uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)bitmap_pixel_in;
|
uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)bitmap_pixel_in;
|
||||||
|
|
||||||
write_status = pwrite(fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
write_status = pwrite(bitmap_file_in->fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
29
lib/bmp.h
29
lib/bmp.h
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/* Change struct name to bitmap header */
|
typedef struct bitmap_header {
|
||||||
typedef struct bitmap {
|
|
||||||
/* File Header */
|
/* File Header */
|
||||||
/* The header field used to identify the BMP and DIB file is 0x42 0x4D in hexadecimal, same as BM in ASCII. The following entries are possible:
|
/* The header field used to identify the BMP and DIB file is 0x42 0x4D in hexadecimal, same as BM in ASCII. The following entries are possible:
|
||||||
|
|
||||||
|
|
@ -39,11 +38,12 @@ typedef struct bitmap {
|
||||||
int32_t vertical_resolution_pixels_per_metre;
|
int32_t vertical_resolution_pixels_per_metre;
|
||||||
uint32_t colors_in_palette;
|
uint32_t colors_in_palette;
|
||||||
uint32_t important_colors;
|
uint32_t important_colors;
|
||||||
} bitmap;
|
} bitmap_header;
|
||||||
|
|
||||||
typedef struct bitmap_file {
|
typedef struct bitmap_file {
|
||||||
char *filename;
|
char *filename;
|
||||||
bitmap *bitmap_metadata;
|
bitmap_header *bitmap_metadata;
|
||||||
|
uint8_t fd;
|
||||||
} bitmap_file;
|
} bitmap_file;
|
||||||
|
|
||||||
typedef struct bitmap_pixel_color {
|
typedef struct bitmap_pixel_color {
|
||||||
|
|
@ -52,16 +52,21 @@ typedef struct bitmap_pixel_color {
|
||||||
uint8_t red;
|
uint8_t red;
|
||||||
} bitmap_pixel_color;
|
} bitmap_pixel_color;
|
||||||
|
|
||||||
/* Change struct name to bitmap header */
|
/* Initialises and writes a new bitmap file with correctly set headers and a blank pixel array. Returns a bitmap_header struct containing all bitmap header info. */
|
||||||
bitmap init_bitmap(const int32_t image_width_in, const int32_t image_height_in);
|
bitmap_header init_bitmap_header(const int32_t image_width_in, const int32_t image_height_in);
|
||||||
/* Change the name to init_bitmap_file */
|
/* Initialises and returns a new bitmap_file struct which contains a str pointer to the filename, the bitmap header metadata and an open file descriptor */
|
||||||
bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename);
|
bitmap_file init_bitmap_file(const bitmap_header *bitmap_in, const char *filename);
|
||||||
|
|
||||||
uint32_t calc_bitmap_row_byte_position_start(const bitmap *bitmap_in, const uint32_t row_in);
|
/* Calculate byte position a particular row starts */
|
||||||
uint32_t calc_bitmap_file_size_bytes(const bitmap *bitmap_in);
|
uint32_t calc_bitmap_row_byte_position_start(const bitmap_header *bitmap_in, const uint32_t row_in);
|
||||||
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in);
|
/* Calculate full size of the bitmap */
|
||||||
|
uint32_t calc_bitmap_file_size_bytes(const bitmap_header *bitmap_in);
|
||||||
|
/* Calculate size of bitmap pixel array only */
|
||||||
|
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap_header *bitmap_in);
|
||||||
|
/* Calculate size of bitmap header only */
|
||||||
uint32_t calc_bitmap_header_size_bytes();
|
uint32_t calc_bitmap_header_size_bytes();
|
||||||
|
|
||||||
int write_bitmap_pixel(const int8_t fd, const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in);
|
/* Write bitmap pixel at particular coordinate */
|
||||||
|
int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
15
main.c
15
main.c
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <wctype.h>
|
||||||
#include "lib/temperature.h"
|
#include "lib/temperature.h"
|
||||||
#include "lib/array.h"
|
#include "lib/array.h"
|
||||||
#include "lib/base10.h"
|
#include "lib/base10.h"
|
||||||
|
|
@ -15,8 +16,6 @@ void swap_int(int *pX, int *pY) {
|
||||||
*pY = temp;
|
*pY = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
/* TODO: Move all of this stuff into its own projects */
|
/* TODO: Move all of this stuff into its own projects */
|
||||||
|
|
@ -41,22 +40,20 @@ int main() {
|
||||||
ss_alloc_free(reserved_memory);
|
ss_alloc_free(reserved_memory);
|
||||||
printf("Memory : %d/%d", ALLOC_SIZE, (int)ss_alloc_get_used_bytes());*/
|
printf("Memory : %d/%d", ALLOC_SIZE, (int)ss_alloc_get_used_bytes());*/
|
||||||
|
|
||||||
const bitmap test = init_bitmap(255, 255);
|
const bitmap_header test = init_bitmap_header(1920, 1080);
|
||||||
/* TODO: Include fd as part of write to bitmap */
|
const bitmap_file test_file = init_bitmap_file(&test, "test.bmp");
|
||||||
const bitmap_file test_file = write_to_bitmap(&test, "test.bmp");
|
|
||||||
bitmap_pixel_color target_pixel;
|
bitmap_pixel_color target_pixel;
|
||||||
|
|
||||||
int32_t width;
|
int32_t width;
|
||||||
int32_t height;
|
int32_t height;
|
||||||
int8_t fd = open(test_file.filename, O_WRONLY | O_CREAT, 0666);
|
|
||||||
for (height = 1; height <= test_file.bitmap_metadata->image_height; ++height) {
|
for (height = 1; height <= test_file.bitmap_metadata->image_height; ++height) {
|
||||||
for (width = 1; width <= test_file.bitmap_metadata->image_width; ++width) {
|
for (width = 1; width <= test_file.bitmap_metadata->image_width; ++width) {
|
||||||
target_pixel.red = ((float)width / (float)test_file.bitmap_metadata->image_width) * 255;
|
target_pixel.red = ((float)width / (float)test_file.bitmap_metadata->image_width) * 255;
|
||||||
target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||||
target_pixel.blue = 255;
|
target_pixel.blue = 100;
|
||||||
write_bitmap_pixel(fd, &test_file, &target_pixel, width, height);
|
write_bitmap_pixel(&test_file, &target_pixel, width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(fd);
|
close(test_file.fd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue