Fix padding algorithm and make simple rendering test

This commit is contained in:
Curt Spark 2024-07-27 01:53:40 +01:00
parent a91b447feb
commit d427adb0ea
4 changed files with 39 additions and 30 deletions

View File

@ -1,8 +1,8 @@
#!/bin/sh
rm test.bmp
# rm test.bmp
gcc -g main.c lib/*.c
./a.out
# chmod 777 test.bmp
printf "\n"
xxd test.bmp
#xxd test.bmp

View File

@ -6,8 +6,11 @@
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 */
uint32_t bytes_unpadded = (((bitmap_in->image_width * bitmap_in->image_height) * bitmap_in->bits_per_pixel) / 8);
return bytes_unpadded + ((bytes_unpadded / 6) * 2); /* Add padding to ensure 4 byte allignment */
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) /* 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) {
@ -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 */
};
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() {
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_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_header_size_bytes());
/* 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);
if (fd == -1) {
close(fd);
@ -75,20 +86,12 @@ bitmap_file write_to_bitmap(const bitmap *bitmap_in, const char *filename) {
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;
int32_t byte_position_end = ((bitmap_file_in->bitmap_metadata->image_width * (y_in - 1)) + x_in) * 3; /* Three bytes */
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;
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;
int8_t fd = open(bitmap_file_in->filename, O_WRONLY | O_CREAT, 0666);
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;
};

View File

@ -50,7 +50,6 @@ typedef struct bitmap_pixel_color {
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t padding_bytes_buffer[2];
} bitmap_pixel_color;
/* 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 */
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_pixel_array_size_bytes(const bitmap *bitmap_in);
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

28
main.c
View File

@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include "lib/temperature.h"
#include "lib/array.h"
#include "lib/base10.h"
@ -35,18 +37,22 @@ int main() {
//
// printf("40C == %dF\n", (int) celsius_to_farenheit(40.0));
const bitmap test = init_bitmap(3, 3);
bitmap_file test_file = write_to_bitmap(&test, "test.bmp");
bitmap_pixel_color test_pixel;
test_pixel.red = 255;
test_pixel.green = 255;
test_pixel.blue = 255;
bitmap_pixel_color test_pixel2;
test_pixel2.red = 100;
test_pixel2.green = 100;
test_pixel2.blue = 255;
const bitmap test = init_bitmap(255, 255);
const bitmap_file test_file = write_to_bitmap(&test, "test.bmp");
bitmap_pixel_color target_pixel;
write_bitmap_pixel(&test_file, &test_pixel, 1, 1);
int32_t width;
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 (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.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
target_pixel.blue = 255;
write_bitmap_pixel(fd, &test_file, &target_pixel, width, height);
}
}
close(fd);
char *reserved_memory = ss_alloc(100);
ss_alloc_free(reserved_memory);