ex0: style format

This commit is contained in:
2025-08-23 22:59:42 +02:00
parent cbd3bbb109
commit 48ca366fd7
4 changed files with 58 additions and 62 deletions

3
.clang-format Normal file
View File

@@ -0,0 +1,3 @@
BasedOnStyle: LLVM
ColumnLimit: 0
Cpp11BracedListStyle: false

View File

@@ -1,63 +1,60 @@
#include <stdlib.h>
#include <stdio.h>
#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
// 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<height; i++ ) {
result = fread( data, sizeof(uchar), widthnew, img);
for (int i = 0; i < height; i++) {
result = fread(data, sizeof(uchar), widthnew, img);
if (result != (size_t) widthnew)
{
perror("Error reading file.\n");
}
if (result != (size_t)widthnew) {
perror("Error reading file.\n");
}
for (int j=0; j<width*3; j+=3) {
array[3 * i * width + j + 0] = data[j+0];
array[3 * i * width + j + 1] = data[j+1];
array[3 * i * width + j + 2] = data[j+2];
}
}
fclose(img); //close the file
for (int j = 0; j < width * 3; j += 3) {
array[3 * i * width + j + 0] = data[j + 0];
array[3 * i * width + j + 1] = data[j + 1];
array[3 * i * width + j + 2] = data[j + 2];
}
}
fclose(img); // close the file
}

View File

@@ -1,7 +1,6 @@
#ifndef BITMAP_H
#define BITMAP_H
typedef unsigned char uchar;
void savebmp(char *name, uchar *buffer, int x, int y);

View File

@@ -1,22 +1,19 @@
#include <stdlib.h>
#include <stdio.h>
#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
#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;
}