diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..da51465 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: LLVM +ColumnLimit: 0 +Cpp11BracedListStyle: false diff --git a/exercise0/bitmap.c b/exercise0/bitmap.c index 1ae2548..a2719ae 100644 --- a/exercise0/bitmap.c +++ b/exercise0/bitmap.c @@ -1,63 +1,60 @@ -#include -#include #include "bitmap.h" - - - - - +#include +#include // save 24-bits bmp file, buffer must be in bmp format: upside-down void savebmp(char *name, uchar *buffer, int x, int y) { - FILE *f=fopen(name,"wb"); - if(!f) { - printf("Error writing image to disk.\n"); - return; - } - unsigned int size=x*y*3+54; - uchar header[54]={'B','M',size&255,(size>>8)&255,(size>>16)&255,size>>24,0, - 0,0,0,54,0,0,0,40,0,0,0,x&255,x>>8,0,0,y&255,y>>8,0,0,1,0,24,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - fwrite(header,1,54,f); - fwrite(buffer,1,x*y*3,f); - fclose(f); + FILE *f = fopen(name, "wb"); + if (!f) { + printf("Error writing image to disk.\n"); + return; + } + unsigned int size = x * y * 3 + 54; + uchar header[54] = { + 'B', 'M', size & 255, (size >> 8) & 255, (size >> 16) & 255, size >> 24, // + 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, x & 255, x >> 8, 0, 0, y & 255, // + y >> 8, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, // + }; + fwrite(header, 1, 54, f); + fwrite(buffer, 1, x * y * 3, f); + fclose(f); } - // read bmp file and store image in contiguous array -void readbmp(char* filename, uchar* array) { - FILE* img = fopen(filename, "rb"); //read the file - uchar header[54]; - size_t result = fread(header, sizeof(uchar), 54, img); // read the 54-byte header +void readbmp(char *filename, uchar *array) { + FILE *img = fopen(filename, "rb"); // read the file + uchar header[54]; + size_t result = + fread(header, sizeof(uchar), 54, img); // read the 54-byte header - if (result != 54) - { - perror("Error reading file.\n"); - return; - } + if (result != 54) { + perror("Error reading file.\n"); + return; + } // extract image height and width from header - int width = *(int*)&header[18]; - int height = *(int*)&header[22]; - int padding=0; - while ((width*3+padding) % 4!=0) padding++; + int width = *(int *)&header[18]; + int height = *(int *)&header[22]; + int padding = 0; + while ((width * 3 + padding) % 4 != 0) + padding++; - int widthnew=width*3+padding; - uchar* data = calloc(widthnew, sizeof(uchar)); + int widthnew = width * 3 + padding; + uchar *data = calloc(widthnew, sizeof(uchar)); - for (int i=0; i -#include #include "bitmap.h" +#include +#include #define XSIZE 2560 // Size of before image #define YSIZE 2048 +int main(void) { + uchar *image = calloc(XSIZE * YSIZE * 3, 1); // Three uchars per pixel (RGB) + readbmp("before.bmp", image); + // Alter the image here + ; -int main(void) -{ - uchar *image = calloc(XSIZE * YSIZE * 3, 1); // Three uchars per pixel (RGB) - readbmp("before.bmp", image); + savebmp("after.bmp", image, XSIZE, YSIZE); - // Alter the image here - ; - - savebmp("after.bmp", image, XSIZE, YSIZE); - - free(image); - return 0; + free(image); + return 0; }