-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
146 lines (124 loc) · 3.49 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
extern void fadecircle(
unsigned char* image,
int width, int height,
int xc, int yc,
int radius,
unsigned int color
);
int getOffset(unsigned char* image) {
if (!image) {
fprintf(stderr, "Null pointer error in getOffset\n");
exit(EXIT_FAILURE);
}
return (*((int*)(image + 0x0A)));
}
int getWidth(unsigned char* image) {
if (!image) {
fprintf(stderr, "Null pointer error in getWidth\n");
exit(EXIT_FAILURE);
}
return *((int*)(image + 0x12));
}
int getHeight(unsigned char* image) {
if (!image) {
fprintf(stderr, "Null pointer error in getHeight\n");
exit(EXIT_FAILURE);
}
return *((int*)(image + 0x16));
}
size_t getSize(FILE* hFile) {
fseek(hFile, 0, SEEK_END);
size_t size = ftell(hFile);
rewind(hFile);
return size;
}
// Read an unsigned integer from user input
unsigned int readInt32(const char* msg) {
unsigned int result;
printf("%s", msg);
if (scanf("%u", &result) != 1) {
fprintf(stderr, "Error reading integer\n");
exit(EXIT_FAILURE);
}
return result;
}
int main(int argc, char** argv)
{
size_t image_size = 0;
unsigned char* image = NULL;
// Check the command-line arguments
if (argc != 3) {
fprintf(stderr, "Usage: %s [input.bmp] [output.bmp]\n", argv[0]);
return EXIT_FAILURE;
}
// Open the input file
FILE* hFile = fopen(argv[1], "rb");
if (!hFile) {
perror("Error opening the input file");
return EXIT_FAILURE;
}
image_size = getSize(hFile); // Get the size of the input file
// Allocate memory for the image data
image = (unsigned char*)malloc(image_size);
if (!image) {
fprintf(stderr, "Memory allocation failed\n");
fclose(hFile);
return EXIT_FAILURE;
}
// Read the image data from the input file
if (fread(image, image_size, 1, hFile) != 1) {
fprintf(stderr, "Error reading the input file\n");
free(image);
fclose(hFile);
return EXIT_FAILURE;
}
fclose(hFile);
int xc, yc;
printf("X-Center: ");
if (scanf("%i", &xc) != 1) {
fprintf(stderr, "Error reading X-Center\n");
free(image);
return EXIT_FAILURE;
}
printf("Y-Center: ");
if (scanf("%i", &yc) != 1) {
fprintf(stderr, "Error reading Y-Center\n");
free(image);
return EXIT_FAILURE;
}
int radius;
printf("Radius: ");
if (scanf("%i", &radius) != 1) {
fprintf(stderr, "Error reading Radius\n");
free(image);
return EXIT_FAILURE;
}
unsigned int color = 0;
color |= readInt32("R: "); //read Red value
color |= readInt32("G: ") << 8; //read Green value and shift by 8
color |= readInt32("B: ") << 16; //read Blue value and shift by 16
// Get image offset, width, and height
int offset = getOffset(image);
int width = getWidth(image);
int height = getHeight(image);
fadecircle(image + offset, width, height, xc, yc, radius, color);
// Open the output file
hFile = fopen(argv[2], "wb");
if (!hFile) {
perror("Error opening the output file");
free(image);
return EXIT_FAILURE;
}
// Write the modified image data to the output file
if (fwrite(image, image_size, 1, hFile) != 1) {
fprintf(stderr, "Error writing to the output file\n");
free(image);
fclose(hFile);
return EXIT_FAILURE;
}
fclose(hFile);
free(image);
return EXIT_SUCCESS;
}