Skip to content

Commit ac3c366

Browse files
assignment_6: maked more readable blur, and divided into small functions read_bmp_file function, got rid of bmp_header in the arguments of the new_header function
1 parent cf299a3 commit ac3c366

File tree

7 files changed

+49
-35
lines changed

7 files changed

+49
-35
lines changed

assignment-6-image_rotation/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ all:
88
clean:
99
rm -f main *.o
1010

11+
start:
12+
./main -m bmp -t b -a 290
13+
1114
# CC=gcc
1215
# CFLAGS=-Wall -Wextra -Wpedantic
1316
# LDFLAGS=

assignment-6-image_rotation/bmp.c

+22-12
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77

88
static enum read_status read_header(FILE* file, struct bmp_header* header);
99
static struct image read_image(FILE* file, uint32_t height, uint32_t width);
10-
static struct bmp_header new_header(const struct bmp_header* o_header, uint32_t height, uint32_t width);
11-
static void save_bmp(FILE* file, const struct bmp_header* header, const struct image img);
10+
static struct bmp_header new_header(uint32_t height, uint32_t width);
11+
static void save_bmp(FILE* file, const struct image img);
1212
static struct image select_mode(struct image image, char mode, double angle);
13+
static struct image read(const char* r_file);
14+
static void write(const char* w_file, const struct image img, char transform, double angle);
1315

1416
enum open_status open_file(FILE** file, const char* filename, const char* mod);
1517

16-
void read_bmp_file(const char* r_file, const char* w_file, char transform, double angle) {
18+
void start(const char* r_file, const char* w_file, char transform, double angle) {
19+
struct image img = read(r_file);
20+
write(w_file, img, transform, angle);
21+
}
22+
23+
struct image read(const char* r_file) {
1724
FILE* file;
1825
if(open_file(&file, r_file, "rb") != OPEN_OK) {
1926
puts("Something wrong with read file");
@@ -24,19 +31,21 @@ void read_bmp_file(const char* r_file, const char* w_file, char transform, doubl
2431
puts("Invalid header");
2532
exit(READ_INVALID_HEADER);
2633
}
27-
2834
fseek(file, header.bOffBits, SEEK_SET);
29-
3035
struct image image = read_image(file, header.biHeight, header.biWidth);
3136
fclose(file);
37+
return image;
38+
}
39+
40+
void write(const char* w_file, const struct image img, char transform, double angle) {
41+
FILE* file;
3242

3343
if(open_file(&file, w_file, "wb") != OPEN_OK) {
3444
puts("Somethign wrong with write file");
3545
exit(OPEN_ERR);
3646
}
37-
image = select_mode(image, transform, angle);
38-
header = new_header(&header, image.height, image.width);
39-
save_bmp(file, &header, image);
47+
struct image image = select_mode(img, transform, angle);
48+
save_bmp(file, image);
4049
fclose(file);
4150
}
4251

@@ -77,7 +86,7 @@ enum read_status read_header(FILE* file, struct bmp_header* header) {
7786
return img;
7887
}
7988

80-
struct bmp_header new_header(const struct bmp_header* o_header, uint32_t height, uint32_t width) {
89+
struct bmp_header new_header(uint32_t height, uint32_t width) {
8190
uint8_t padding = set_padding(width);
8291
struct bmp_header new_header = {
8392
BM,
@@ -99,9 +108,10 @@ struct bmp_header new_header(const struct bmp_header* o_header, uint32_t height,
99108
return new_header;
100109
}
101110

102-
void save_bmp(FILE* file, const struct bmp_header* header, const struct image img) {
103-
uint8_t padding = set_padding(header -> biWidth);
104-
fwrite(header, sizeof(struct bmp_header), 1, file);
111+
void save_bmp(FILE* file, const struct image img) {
112+
uint8_t padding = set_padding(img.width);
113+
struct bmp_header header = new_header(img.height, img.width);
114+
fwrite(&header, sizeof(struct bmp_header), 1, file);
105115
for(uint32_t i = 0; i < img.height; i++) {
106116
fwrite(img.data + i * img.width, sizeof(struct pixel), img.width, file);
107117
fwrite(&padding, 1, padding, file);

assignment-6-image_rotation/bmp_struct.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum open_status {
4343
OPEN_ERR
4444
};
4545

46-
void read_bmp_file(const char* r_file, const char* w_file, char transform, double angle);
46+
47+
void start(const char* r_file, const char* w_file, char transform, double angle);
4748

4849
#endif

assignment-6-image_rotation/main

18 KB
Binary file not shown.

assignment-6-image_rotation/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int main(int argc, char** argv) {
1111
char trans = 'a';
1212
char* mode;
1313
int angle = 45;
14+
puts("-----start-----");
1415
while((rez = getopt(argc, argv, "m:t:a:")) != -1) {
1516
switch(rez) {
1617
case 'm':
@@ -30,7 +31,6 @@ int main(int argc, char** argv) {
3031
break;
3132
}
3233
}
33-
puts("-----start-----");
3434
transform("./res/p.bmp", "./res/p1.bmp", mode, trans, (double) angle);
3535
puts("-----end-----");
3636
return 0;

assignment-6-image_rotation/read_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
void transform(const char* r_file, const char* w_file, char* mode, char transform, double angle) {
66
if(strcmp(mode, "bmp") == 0) {
7-
read_bmp_file(r_file, w_file, transform, angle);
7+
start(r_file, w_file, transform, angle);
88
} else {
99
puts("We don't have any other type");
1010
}

assignment-6-image_rotation/rotate_logic.c

+20-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
static uint32_t calc_new_height(const struct image* source, double angle);
66
static uint32_t calc_new_width(const struct image* source, double angle);
7+
static void pixel_blur(struct pixel* const new_pixel, const struct pixel* source, int32_t width)
78

89
struct image rotate_left(const struct image* source) {
910
struct image new_image = create_image(source -> height, source -> width);
@@ -25,7 +26,7 @@ struct image mirror_image(const struct image* source) {
2526
}
2627
}
2728

28-
if(new_image.width / 2 != 0) {
29+
if(new_image.width % 2 != 0) {
2930
for(uint32_t i = 0; i < new_image.height; i++) {
3031
*(new_image.data + (i * new_image.width) + new_image.width + 1) =
3132
*(source -> data + (i * new_image.width) + new_image.width + 1);
@@ -34,32 +35,31 @@ struct image mirror_image(const struct image* source) {
3435
return new_image;
3536
}
3637

38+
void pixel_blur(struct pixel* const new_pixel, const struct pixel* source, int32_t width) {
39+
uint16_t b = 0;
40+
uint16_t g = 0;
41+
uint16_t r = 0;
42+
43+
for(int32_t i = -1; i < 2; i++) {
44+
for (int32_t j = -1; j < 2; j++) {
45+
b += (source + (i * width) + j) -> b;
46+
g += (source + (i * width) + j) -> g;
47+
r += (source + (i * width) + j) -> r;
48+
}
49+
}
50+
51+
new_pixel -> b = b / 9;
52+
new_pixel -> g = g / 9;
53+
new_pixel -> r = r / 9;
54+
}
3755

3856
struct image blur(const struct image* source) {
3957
struct image new_image = create_image(source -> height, source -> width);
40-
uint16_t b = 0;
41-
uint16_t g = 0;
42-
uint16_t r = 0;
4358

4459
// the inner part
4560
for(uint32_t i = 1; i < new_image.height - 1; i++) {
4661
for(uint32_t j = 1; j < new_image.width - 1; j++) {
47-
48-
for(int32_t s_i = -1; s_i < 2; s_i++) {
49-
for (int32_t s_j = -1; s_j < 2; s_j++) {
50-
b += (source -> data + ((i + s_i) * new_image.width) + (j + s_j)) -> b;
51-
g += (source -> data + ((i + s_i) * new_image.width) + (j + s_j)) -> g;
52-
r += (source -> data + ((i + s_i) * new_image.width) + (j + s_j)) -> r;
53-
}
54-
}
55-
56-
(new_image.data + (i * new_image.width) + j) -> b = b / 9;
57-
(new_image.data + (i * new_image.width) + j) -> g = g / 9;
58-
(new_image.data + (i * new_image.width) + j) -> r = r / 9;
59-
60-
b = 0;
61-
g = 0;
62-
r = 0;
62+
pixel_blur((struct pixel*)(new_image.data + (i * new_image.width) + j), (struct pixel*)(source -> data + (i * new_image.width) + j), source -> width);
6363
}
6464
}
6565

0 commit comments

Comments
 (0)