diff --git a/lib/alloc.c b/lib/alloc.c new file mode 100644 index 0000000..d162639 --- /dev/null +++ b/lib/alloc.c @@ -0,0 +1,28 @@ +#include "alloc.h" + +static char ss_alloc_buff[ALLOC_SIZE]; +static char *ss_alloc_p = ss_alloc_buff; + +long ss_alloc_get_free_bytes() { + return ss_alloc_buff + ALLOC_SIZE - ss_alloc_p; +} + +long ss_alloc_get_used_bytes() { + return ss_alloc_p - ss_alloc_buff; +} + +char *ss_alloc(int in) { + if (ss_alloc_get_free_bytes() >= in) { + ss_alloc_p += in; + return ss_alloc_p - in; + } else + return 0; +} + +int ss_alloc_free(char *in) { + if (ss_alloc_buff <= in && in <= ss_alloc_buff + ALLOC_SIZE) { + ss_alloc_p = in; + return 1; + } else + return 0; +} diff --git a/lib/alloc.h b/lib/alloc.h new file mode 100644 index 0000000..2767d08 --- /dev/null +++ b/lib/alloc.h @@ -0,0 +1,13 @@ +#ifndef ALLOC_H +#define ALLOC_H + +#define ALLOC_SIZE 10000 + +// Stupid simple alloc +void ss_alloc_init(); +long ss_alloc_get_free_bytes(); +long ss_alloc_get_used_bytes(); +char* ss_alloc(int n); +int ss_alloc_free(char *n); + +#endif diff --git a/lib/array.c b/lib/array.c index 223c8c1..85af2c1 100755 --- a/lib/array.c +++ b/lib/array.c @@ -2,19 +2,19 @@ #include "array.h" #include "base10.h" -int sizeofStr(char in[]) { +int sizeof_str(char in[]) { int i = 0; for (i = 0; in[i] != '\0'; ++i) ; return i; } -void printStrArray(char in[]) { +void print_strArray(char in[]) { int i; printf("{ "); - for (i = 0; i < sizeofStr(in); ++i) { + for (i = 0; i < sizeof_str(in); ++i) { printf("%c", in[i]); - if (i == sizeofStr(in) - 1) { + if (i == sizeof_str(in) - 1) { printf(" }\n"); } else { printf(", "); @@ -22,7 +22,7 @@ void printStrArray(char in[]) { } } -void printIntArray(intArray *in) { +void print_intArray(intArray *in) { int i; printf("{ "); for (i = 0; i < in->length; ++i) { @@ -35,7 +35,7 @@ void printIntArray(intArray *in) { } } -int checkIntArraySorted(intArray *in) { +int check_intArray_sorted(intArray *in) { int i = 0; while(i < in->length - 1) { if (in->data[i] > in->data[i + 1]) @@ -47,27 +47,27 @@ int checkIntArraySorted(intArray *in) { } /* Convert an integer array into a combined integer */ -int intArrayToInt(intArray *in) { +int intArray_to_int(intArray *in) { int i; int out = 0; - int currentDigit; - for (i = 0, currentDigit = in->length; i <= in->length; ++i, --currentDigit) - out += in->data[i] * intDigitExpand(currentDigit); + int current_digit; + for (i = 0, current_digit = in->length; i <= in->length; ++i, --current_digit) + out += in->data[i] * int_expand_digits(current_digit); return out; } /* Split an integer into an integer array */ -intArray intToIntArray(int in) { - int inLength = intDigitCount(in); - int outData[inLength]; +intArray int_to_intArray(int in) { + int in_length = int_count_digits(in); + int out_data[in_length]; intArray out; - out.data = outData; - out.length = inLength; + out.data = out_data; + out.length = in_length; int i; int j; - for(i = inLength, j = 0; i >= 1; --i, ++j) { - out.data[j] = in / intDigitExpand(i); - in -= intDigitExpand(i) * out.data[j]; + for(i = in_length, j = 0; i >= 1; --i, ++j) { + out.data[j] = in / int_expand_digits(i); + in -= int_expand_digits(i) * out.data[j]; } return out; diff --git a/lib/array.h b/lib/array.h index cd661a8..ec9d733 100755 --- a/lib/array.h +++ b/lib/array.h @@ -1,21 +1,21 @@ #ifndef ARRAY_H #define ARRAY_H -typedef struct IntArray { +typedef struct intArray { unsigned int length; int *data; } intArray; -int sizeofStr(char in[]); +int sizeof_str(char in[]); -void printStrArray(char in[]); +void print_strArray(char in[]); -void printIntArray(intArray *in); +void print_intArray(intArray *in); -int checkIntArraySorted(intArray *in); +int check_intArray_sorted(intArray *in); -intArray intToIntArray(int in); +intArray int_to_intArray(int in); -int intArrayToInt(intArray *in); +int intArray_to_int(intArray *in); #endif diff --git a/lib/base10.c b/lib/base10.c index 7584937..b5296f3 100755 --- a/lib/base10.c +++ b/lib/base10.c @@ -1,16 +1,16 @@ /* Count the number of digits an integer has */ -unsigned long long intDigitCount(unsigned long long in) { - unsigned long long largestDigit; - int digitCount = 0; - for (largestDigit = 1; in / largestDigit != 0; largestDigit *= 10) - ++digitCount; - return digitCount; +unsigned long long int_count_digits(unsigned long long in) { + unsigned long long largest_digit; + int digit_count = 0; + for (largest_digit = 1; in / largest_digit != 0; largest_digit *= 10) + ++digit_count; + return digit_count; } /* Expand an integer into an integer whose first unit is 1 followed by a leading trail of 0's*/ -unsigned long long intDigitExpand(unsigned long long in) { - int expandedDigit; - for (expandedDigit = 1; in != 1; --in) - expandedDigit *= 10; - return expandedDigit; +unsigned long long int_expand_digits(unsigned long long in) { + int expanded_digit; + for (expanded_digit = 1; in != 1; --in) + expanded_digit *= 10; + return expanded_digit; } diff --git a/lib/base10.h b/lib/base10.h index cc4125d..4008027 100755 --- a/lib/base10.h +++ b/lib/base10.h @@ -1,8 +1,8 @@ #ifndef BASE10_H #define BASE10_H -unsigned long long intDigitCount(unsigned long long in); +unsigned long long int_count_digits(unsigned long long in); -unsigned long long intDigitExpand(unsigned long long in); +unsigned long long int_expand_digits(unsigned long long in); #endif diff --git a/lib/bmp.c b/lib/bmp.c new file mode 100644 index 0000000..191329a --- /dev/null +++ b/lib/bmp.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "bmp.h" + +uint32_t calc_bitmap_pixel_array_size_bytes(bitmap *bitmap_in) { + /* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 */ + uint32_t bytes_unpadded = (bitmap_in->image_width * bitmap_in->image_height * bitmap_in->bits_per_pixel) / 8; + return bytes_unpadded + (bytes_unpadded % 8); /* Add padding to ensure 4 byte allignment */ +}; + +uint32_t calc_bitmap_file_size_bytes(bitmap *bitmap_in) { + /* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 */ + return sizeof(bitmap) + calc_bitmap_pixel_array_size_bytes(bitmap_in) - 2 ; /* Take away two to account for compiler padding struct */ +}; + +bitmap init_bitmap(int32_t image_width_in, int32_t image_height_in) { + bitmap new_bitmap; + /* Bitmap Info Header */ + new_bitmap.info_header_size_bytes = 40; + new_bitmap.image_width = image_width_in; + new_bitmap.image_height = image_height_in; + new_bitmap.color_planes_used = 1; + new_bitmap.bits_per_pixel = 24; + new_bitmap.compression_method = 0; + + new_bitmap.image_size = calc_bitmap_pixel_array_size_bytes(&new_bitmap); + new_bitmap.horizontal_resolution_pixels_per_metre = 2835; /* Approx 72DPI */ + new_bitmap.vertical_resolution_pixels_per_metre = 2835; + new_bitmap.colors_in_palette = 0; + new_bitmap.important_colors = 0; + + /* File Header */ + new_bitmap.header_field[0] = 'B'; + new_bitmap.header_field[1] = 'M'; + new_bitmap.file_size_bytes = calc_bitmap_file_size_bytes(&new_bitmap); + new_bitmap.reserved1 = 0; + new_bitmap.reserved2 = 0; + new_bitmap.image_data_start_offset = 54; + + return new_bitmap; +}; + +int write_to_bitmap(bitmap *bitmap_in, char *filename) { + int8_t write_status; + uint8_t *bitmap_in_byte_ptr = (uint8_t *)bitmap_in + 2; /* Offset by two to account for padding */ + int8_t fd = open(filename, O_WRONLY | O_CREAT); + if (fd == -1) + return -1; + write_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_file_size_bytes(bitmap_in)); + if (fd == -1) { + close(fd); + return -1; + } + close(fd); + return 0; +}; diff --git a/lib/bmp.h b/lib/bmp.h new file mode 100644 index 0000000..e67eda4 --- /dev/null +++ b/lib/bmp.h @@ -0,0 +1,50 @@ +#ifndef BMP_H +#define BMP_H + +#include + +typedef struct bitmap { + /* 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: + + BM + Windows 3.1x, 95, NT, ... etc. + BA + OS/2 struct bitmap array + CI + OS/2 struct color icon + CP + OS/2 const color pointer + IC + OS/2 struct icon + PT + OS/2 pointer */ + uint16_t padding_allignment; /* The compiler will pad structs automatically to ensure the struct is byte aligned. (i.e 54 bytes to 56). We declare padding allignment ourselves and we will make sure to account for this. */ + uint8_t header_field[2]; + uint32_t file_size_bytes; + uint16_t reserved1; + uint16_t reserved2; + uint32_t image_data_start_offset; + + /* Bitmap Info Header */ + uint32_t info_header_size_bytes; + int32_t image_width; + int32_t image_height; + uint16_t color_planes_used; + uint16_t bits_per_pixel; + uint32_t compression_method; + uint32_t image_size; + int32_t horizontal_resolution_pixels_per_metre; + int32_t vertical_resolution_pixels_per_metre; + uint32_t colors_in_palette; + uint32_t important_colors; +} bitmap; + +bitmap init_bitmap(int32_t image_width_in, int32_t image_height_in); + +uint32_t calc_bitmap_file_size_bytes(bitmap *bitmap_in); +uint32_t calc_bitmap_pixel_array_size_bytes(bitmap *bitmap_in); + +int write_to_bitmap(bitmap *bitmap_in, char *filename); + +#endif diff --git a/lib/math.c b/lib/math.c index a703b07..a257354 100644 --- a/lib/math.c +++ b/lib/math.c @@ -1,6 +1,6 @@ #include "array.h" -intArray findDivisors(int in) { +intArray find_divisors(int in) { int i; intArray out; for (i = 1; i <= in; ++i) { diff --git a/lib/math.h b/lib/math.h index f474313..2865c09 100644 --- a/lib/math.h +++ b/lib/math.h @@ -3,6 +3,6 @@ #include "array.h" -intArray findDivisors(); +intArray find_divisors(); #endif diff --git a/lib/temperature.c b/lib/temperature.c index 0afea55..4d892ce 100755 --- a/lib/temperature.c +++ b/lib/temperature.c @@ -1,7 +1,7 @@ -float farenheitToCelsius(float farenheit) { +float farenheit_to_celsius(float farenheit) { return (farenheit - 32.0) / 1.8; } -float celsiusToFarenheit(float celsius) { +float celsius_to_farenheit(float celsius) { return (celsius * 1.8) + 32.0; } diff --git a/lib/temperature.h b/lib/temperature.h index 4414682..0afea58 100755 --- a/lib/temperature.h +++ b/lib/temperature.h @@ -1,8 +1,8 @@ #ifndef TEMP_H #define TEMP_H -float farenheitToCelsius(float farenheit); +float farenheit_to_celsius(float farenheit); -float celsiusToFarenheit(float celsius); +float celsius_to_farenheit(float celsius); #endif diff --git a/main.c b/main.c index 8d7cc3b..0b454ba 100755 --- a/main.c +++ b/main.c @@ -1,45 +1,44 @@ #include +#include #include "lib/temperature.h" #include "lib/array.h" #include "lib/base10.h" // #include "lib/math.h" +#include "lib/alloc.h" +#include "lib/bmp.h" -void swapInt(int *pX, int *pY) { +void swap_int(int *pX, int *pY) { int temp = *pX; *pX = *pY; *pY = temp; } -#define ALLOCSIZE 10000 -char allocbuff[ALLOCSIZE]; -char* allocp = allocbuff; - -char* alloc(int n) { - if (allocbuff + ALLOCSIZE - allocp >= n) { - allocp += n; - return allocp - n; - } else - return 0; -} int main() { - intArray unsortedArray; - int unsortedArrayData[] = { 1, 5, 2, 1, 4, 8, 3, 2, 6, 7 }; - unsortedArray.length = sizeof(unsortedArrayData) / sizeof(int); - unsortedArray.data = unsortedArrayData; - intArray sortedArray; - int sortedArrayData[] = { 1, 2, 4, 8, 9, 9, 9 }; - sortedArray.length = sizeof(sortedArrayData) / sizeof(int); - sortedArray.data = sortedArrayData; + // intArray unsorted_array; + // int unsorted_array_data[] = { 1, 5, 2, 1, 4, 8, 3, 2, 6, 7 }; + // unsorted_array.length = sizeof(unsorted_array_data) / sizeof(int); + // unsorted_array.data = unsorted_array_data; + // intArray sorted_array; + // int sorted_array_data[] = { 1, 2, 4, 8, 9, 9, 9 }; + // sorted_array.length = sizeof(sorted_array_data) / sizeof(int); + // sorted_array.data = sorted_array_data; + // + // printf("Unsorted Array : "); + // print_intArray(&unsorted_array); + // printf("Unsorted Array Sorted?: %d\n", check_intArray_sorted(&unsorted_array)); + // printf("Sorted Array : "); + // print_intArray(&sorted_array); + // printf("Sorted Array Sorted?: %d\n", check_intArray_sorted(&sorted_array)); + // + // printf("40C == %dF\n", (int) celsius_to_farenheit(40.0)); - printf("Unsorted Array : "); - printIntArray(&unsortedArray); - printf("Unsorted Array Sorted?: %d\n", checkIntArraySorted(&unsortedArray)); - printf("Sorted Array : "); - printIntArray(&sortedArray); - printf("Sorted Array Sorted?: %d\n", checkIntArraySorted(&sortedArray)); + bitmap test = init_bitmap(2, 2); + write_to_bitmap(&test, "test.bmp"); - printf("40C == %dF\n", (int) celsiusToFarenheit(40.0)); + char *reserved_memory = ss_alloc(100); + ss_alloc_free(reserved_memory); + printf("Memory : %d/%d", ALLOC_SIZE, (int)ss_alloc_get_used_bytes()); } diff --git a/test.bmp b/test.bmp new file mode 100755 index 0000000..8243851 Binary files /dev/null and b/test.bmp differ