Fix padding algorithm and make simple rendering test
This commit is contained in:
parent
a91b447feb
commit
d427adb0ea
|
|
@ -1,8 +1,8 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
rm test.bmp
|
# rm test.bmp
|
||||||
gcc -g main.c lib/*.c
|
gcc -g main.c lib/*.c
|
||||||
./a.out
|
./a.out
|
||||||
# chmod 777 test.bmp
|
# chmod 777 test.bmp
|
||||||
printf "\n"
|
printf "\n"
|
||||||
xxd test.bmp
|
#xxd test.bmp
|
||||||
|
|
|
||||||
31
lib/bmp.c
31
lib/bmp.c
|
|
@ -6,8 +6,11 @@
|
||||||
|
|
||||||
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in) {
|
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *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 = (((bitmap_in->image_width * bitmap_in->image_height) * bitmap_in->bits_per_pixel) / 8);
|
uint32_t bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
||||||
return bytes_unpadded + ((bytes_unpadded / 6) * 2); /* Add padding to ensure 4 byte allignment */
|
uint32_t bytes_padded_row = bytes_unpadded_row;
|
||||||
|
if (bytes_padded_row % 4) /* Ensure 4 byte alignment for every row */
|
||||||
|
bytes_padded_row = (bytes_unpadded_row - (bytes_unpadded_row % 4)) + 4;
|
||||||
|
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 *bitmap_in) {
|
||||||
|
|
@ -15,6 +18,14 @@ uint32_t calc_bitmap_file_size_bytes(const bitmap *bitmap_in) {
|
||||||
return sizeof(bitmap) + calc_bitmap_pixel_array_size_bytes(bitmap_in) - 2 ; /* Take away two to account for compiler padding struct */
|
return sizeof(bitmap) + 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 bytes_unpadded_row = ((bitmap_in->image_width * bitmap_in->bits_per_pixel) / 8);
|
||||||
|
uint32_t bytes_padded_row = bytes_unpadded_row;
|
||||||
|
if (bytes_padded_row % 4)
|
||||||
|
bytes_padded_row = (bytes_unpadded_row - (bytes_unpadded_row % 4)) + 4;
|
||||||
|
return (bytes_padded_row * (row_in - 1));
|
||||||
|
};
|
||||||
|
|
||||||
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) - 2 ; /* Take away two to account for compiler padding struct */
|
||||||
};
|
};
|
||||||
|
|
@ -61,7 +72,7 @@ bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename) {
|
||||||
/* Write Header */
|
/* Write Header */
|
||||||
write_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_header_size_bytes());
|
write_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_header_size_bytes());
|
||||||
/* Write blank pixel array */
|
/* Write blank pixel array */
|
||||||
for (current_byte = bitmap_in->image_data_start_offset; current_byte <= calc_bitmap_file_size_bytes(bitmap_in); ++current_byte)
|
for (current_byte = bitmap_in->image_data_start_offset; current_byte < calc_bitmap_file_size_bytes(bitmap_in); ++current_byte)
|
||||||
write_status = pwrite(fd, blank_byte_buffer, 1, current_byte);
|
write_status = pwrite(fd, blank_byte_buffer, 1, current_byte);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
@ -75,20 +86,12 @@ bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename) {
|
||||||
return new_bitmap_file;
|
return new_bitmap_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
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) {
|
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) {
|
||||||
int8_t write_status;
|
int8_t write_status;
|
||||||
int32_t byte_position_end = ((bitmap_file_in->bitmap_metadata->image_width * (y_in - 1)) + x_in) * 3; /* Three bytes */
|
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_end_padded = byte_position_end + ((byte_position_end / 8) * 2); /* Account for padding of all pixels before this one */
|
|
||||||
int32_t byte_position_start = byte_position_end_padded - 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;
|
||||||
|
|
||||||
int8_t fd = open(bitmap_file_in->filename, O_WRONLY | O_CREAT, 0666);
|
write_status = pwrite(fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
||||||
|
|
||||||
if (fd == -1)
|
|
||||||
return -1;
|
|
||||||
if (byte_position_start % 4)
|
|
||||||
write_status = pwrite(fd, bitmap_pixel_in_byte_ptr, 5, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
|
||||||
else
|
|
||||||
write_status = pwrite(fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ typedef struct bitmap_pixel_color {
|
||||||
uint8_t blue;
|
uint8_t blue;
|
||||||
uint8_t green;
|
uint8_t green;
|
||||||
uint8_t red;
|
uint8_t red;
|
||||||
uint8_t padding_bytes_buffer[2];
|
|
||||||
} bitmap_pixel_color;
|
} bitmap_pixel_color;
|
||||||
|
|
||||||
/* Change struct name to bitmap header */
|
/* Change struct name to bitmap header */
|
||||||
|
|
@ -58,10 +57,11 @@ bitmap init_bitmap(const int32_t image_width_in, const int32_t image_height_in);
|
||||||
/* Change the name to init_bitmap_file */
|
/* Change the name to init_bitmap_file */
|
||||||
bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename);
|
bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename);
|
||||||
|
|
||||||
|
uint32_t calc_bitmap_row_byte_position_start(const bitmap *bitmap_in, const uint32_t row_in);
|
||||||
uint32_t calc_bitmap_file_size_bytes(const bitmap *bitmap_in);
|
uint32_t calc_bitmap_file_size_bytes(const bitmap *bitmap_in);
|
||||||
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in);
|
uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap *bitmap_in);
|
||||||
uint32_t calc_bitmap_header_size_bytes();
|
uint32_t calc_bitmap_header_size_bytes();
|
||||||
|
|
||||||
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);
|
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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
30
main.c
30
main.c
|
|
@ -1,5 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.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"
|
||||||
|
|
@ -35,18 +37,22 @@ int main() {
|
||||||
//
|
//
|
||||||
// printf("40C == %dF\n", (int) celsius_to_farenheit(40.0));
|
// printf("40C == %dF\n", (int) celsius_to_farenheit(40.0));
|
||||||
|
|
||||||
const bitmap test = init_bitmap(3, 3);
|
const bitmap test = init_bitmap(255, 255);
|
||||||
bitmap_file test_file = write_to_bitmap(&test, "test.bmp");
|
const bitmap_file test_file = write_to_bitmap(&test, "test.bmp");
|
||||||
bitmap_pixel_color test_pixel;
|
bitmap_pixel_color target_pixel;
|
||||||
test_pixel.red = 255;
|
|
||||||
test_pixel.green = 255;
|
int32_t width;
|
||||||
test_pixel.blue = 255;
|
int32_t height;
|
||||||
bitmap_pixel_color test_pixel2;
|
int8_t fd = open(test_file.filename, O_WRONLY | O_CREAT, 0666);
|
||||||
test_pixel2.red = 100;
|
for (height = 1; height <= test_file.bitmap_metadata->image_height; ++height) {
|
||||||
test_pixel2.green = 100;
|
for (width = 1; width <= test_file.bitmap_metadata->image_width; ++width) {
|
||||||
test_pixel2.blue = 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;
|
||||||
write_bitmap_pixel(&test_file, &test_pixel, 1, 1);
|
target_pixel.blue = 255;
|
||||||
|
write_bitmap_pixel(fd, &test_file, &target_pixel, width, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
char *reserved_memory = ss_alloc(100);
|
char *reserved_memory = ss_alloc(100);
|
||||||
ss_alloc_free(reserved_memory);
|
ss_alloc_free(reserved_memory);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue