ex0: initial

This commit is contained in:
2025-08-23 22:43:36 +02:00
parent d7e2447a72
commit c84f590587
3 changed files with 95 additions and 0 deletions

63
exercise0/bitmap.c Normal file
View File

@@ -0,0 +1,63 @@
#include <stdlib.h>
#include <stdio.h>
#include "bitmap.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);
}
// 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
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 widthnew=width*3+padding;
uchar* data = calloc(widthnew, sizeof(uchar));
for (int i=0; i<height; i++ ) {
result = fread( data, sizeof(uchar), widthnew, img);
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
}

10
exercise0/bitmap.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef BITMAP_H
#define BITMAP_H
typedef unsigned char uchar;
void savebmp(char *name, uchar *buffer, int x, int y);
void readbmp(char *filename, uchar *array);
#endif

22
exercise0/main.c Normal file
View File

@@ -0,0 +1,22 @@
#include <stdlib.h>
#include <stdio.h>
#include "bitmap.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
;
savebmp("after.bmp", image, XSIZE, YSIZE);
free(image);
return 0;
}