This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
421 lines (346 loc) · 14.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// Created by lara on 02/01/2021.
//
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <bitmap.h>
void info(char **argv);
void copy(char **argv);
void create(char **argv);
void grayscale(char **argv);
void process(char **argv);
void drawCircle(char **argv);
void drawRectangle(char **argv);
void drawTriangle(char **argv);
void help();
int main(int argc, char **argv) {
if(strcmp(argv[1], "help") == 0 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "?") == 0){
help();
}else if (argc == 3 && (strcmp(argv[1], "info") == 0 || strcmp(argv[1], "-i") == 0)) {
info(argv);
} else if (argc == 4 && (strcmp(argv[1], "copy") == 0 || strcmp(argv[1], "-cp") == 0)) {
copy(argv);
} else if (argc == 11 && (strcmp(argv[1], "create") == 0 || strcmp(argv[1], "-c") == 0)) {
create(argv);
} else if (strcmp(argv[1], "draw") == 0 || strcmp(argv[1], "-d") == 0) {
// ./main [draw|-d] [circle/-c/rectangle/-r/triangle/-t] [...] test.bmp
if (argc == 13 && (strcmp(argv[2], "circle") == 0 || strcmp(argv[2], "-c") == 0)) {
drawCircle(argv);
} else if (argc == 20 && (strcmp(argv[2], "rectangle") == 0 || strcmp(argv[2], "-r") == 0)) {
drawRectangle(argv);
} else if ( argc == 17 && (strcmp(argv[2], "triangle") == 0 || strcmp(argv[2], "-t") == 0)) {
drawTriangle(argv);
} else {
fprintf(stderr, "%s: was not recognized as a valid command\n", argv[2]);
}
} else if (argc == 4 && (strcmp(argv[1], "grayscale") == 0 || strcmp(argv[1], "-gs") == 0)) {
grayscale(argv);
} else if (argc == 16 && (strcmp(argv[1], "convolution") == 0 || strcmp(argv[1], "-cv") == 0)) {
process(argv);
} else {
if (argc < 3) {
fprintf(stderr, "Not enough arguments were passed\n");
} else {
fprintf(stderr, "%s: was not recognized as a valid command\n", argv[1]);
}
return 1;
}
return 0;
}
void help(){
char * name = "./lib";
printf("usage: %s\n\n", name);
printf("The following are common commands:\n\n");
printf("get an overview of the bitmap infos (like headers)\n");
printf(" info %s [info|-i] <src-file>\n", name);
printf(" e.g.: '%s info lena.bmp'\n\n", name);
printf("make a copy of an bitmap image\n");
printf(" copy %s [copy|-cp] <src-file> <dest-file>\n", name);
printf(" e.g.: '%s copy lena.bmp lena_copy.bmp'\n\n", name);
printf("grayscale an image (naive (r+g+b)/3 )\n");
printf(" grayscale %s [grayscale|-gs] <src-file> <dest-file>\n", name);
printf(" e.g.: '%s grayscale lena.bmp lena_gray.bmp'\n\n", name);
printf("create 24bpp images and draw geometric shapes on them\n");
printf(" create %s [create|-c] [width:|w:] int [height:/h:] int [color:/c:] int(r) int(g) int(b) <dest-file>\n", name);
printf(" e.g.: '%s create w: 200 h: 200 c: 241 136 201 pink_panther.bmp'\n\n", name);
printf(" draw-circle %s [draw|-d] [circle|-c] [radius:|r:] int [origin:/o:] int(x) int(y) [color:/c:] int(r) int(g) int(b) <dest-file>\n", name);
printf(" e.g.: '%s draw circle r: 100 o: 50 75 c: 247 255 173 pink_panther.bmp'\n\n", name);
printf(" draw-rect %s [draw|-d] [rectangle|-r] [pointE:/e:] int(x) int(y) [pointF:/f:] int(x) int(y) [pointG:/g:] int(x) int(y) [pointH:/h:] int(x) int(y) [color:/c:] int(r) int(g) int(b) <dest-file>\n", name);
printf(" e.g.: '%s draw rectangle e: 50 50 f: 50 100 g: 100 100 h: 100 50 c: 71 167 255 pink_panther.bmp'\n\n", name);
printf(" draw-tri %s [draw|-d] [triangle|-t] [pointE:/e:] int(x) int(y) [pointF:/f:] int(x) int(y) [pointG:/g:] int(x) int(y) [color:/c:] int(r) int(g) int(b) <dest-file>\n", name);
printf(" e.g.: '%s draw triangle e: 50 150 f: 20 300 g: 300 300 c: 16 95 74 pink_panther.bmp'\n\n", name);
printf("simple image processing using image convolution and an 3x3 kernel\n");
printf(" convolution %s [convolution|-cv] [kernel:|k:] int int int int int int int int int [divider:|d:] int <src-file> <dest-file>\n", name);
printf(" e.g.: '%s convolution k: 1 0 -1 0 0 0 -1 0 1 d: 1 lena.bmp lena_edge_detect.bmp'\n\n", name);
}
void info(char **argv) {
// ./main [info|-i] lena.bmp
FILE *source = fopen(argv[2], "rb");
if (source != NULL) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0) {
printf("Bitmap %s:\n", argv[2]);
Bitmap_print(&bitmap);
Bitmap_destroy(&bitmap);
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
fclose(source);
}
void copy(char **argv) {
// ./main [copy|-cp] lena.bmp lena_copy.bmp
FILE *source = fopen(argv[2], "rb");
if (source != NULL) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0) {
FILE *dest = fopen(argv[3], "wr");
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if (copied == 0) {
Bitmap_destroy(&bitmap);
fclose(dest);
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
}
void create(char **argv) {
// ./main [create|-c] [width:/w:] int [height:/h:] int [color:/c:] int int int test.bmp
if ((strcmp(argv[2], "width:") == 0 || strcmp(argv[2], "w:") == 0) &&
(strcmp(argv[4], "height:") == 0 || strcmp(argv[4], "h:") == 0) &&
(strcmp(argv[6], "color:") == 0 || strcmp(argv[6], "c:") == 0)) {
RGB color;
color.red = atoi(argv[7]);
color.green = atoi(argv[8]);
color.blue = atoi(argv[9]);
uint32_t width = atoi(argv[3]);
uint32_t height = atoi(argv[5]);
Bitmap bitmap;
uint32_t created = Bitmap_create(&bitmap, &color, width, height);
if (created == 0) {
FILE *dest = fopen(argv[10], "wb");
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if (copied == 0) {
Bitmap_destroy(&bitmap);
fclose(dest);
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
}
}
void grayscale(char **argv) {
// ./lib [grayscale/-gs] test.bmp test_gray.bmp
FILE *source = fopen(argv[2], "rb");
if (source != NULL) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0 && bitmap.biBitCount == 24) {
if (bitmap.colorTable_size == 0) {
uint32_t grayPixels = Bitmap_naive_grayscaling_px(&bitmap);
if (grayPixels == 1) {
fprintf(stderr, "That did not work\n");
}
} else {
uint32_t grayTable = Bitmap_naive_grayscaling_ct(&bitmap);
if (grayTable == 1) {
fprintf(stderr, "That did not work\n");
}
}
FILE *dest = fopen(argv[3], "wr");
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if (copied == 0) {
Bitmap_destroy(&bitmap);
fclose(dest);
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work. Images must be 24bpp\n");
}
}
void process(char **argv) {
// ./lib [convolution/-cv] [kernel:/k:] int int int int int int int int int [divider:/d:] int test.bmp test_conv.bmp
if ((strcmp(argv[2], "kernel:") == 0 || strcmp(argv[2], "k:") == 0) &&
(strcmp(argv[12], "divider:") == 0 || strcmp(argv[12], "d:") == 0)) {
FILE * source = fopen(argv[14], "rb");
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if(scanned == 0 && bitmap.biBitCount == 24){
int32_t kernel[9];
kernel[0] = atoi(argv[3]);
kernel[1] = atoi(argv[4]);
kernel[2] = atoi(argv[5]);
kernel[3] = atoi(argv[6]);
kernel[4] = atoi(argv[7]);
kernel[5] = atoi(argv[8]);
kernel[6] = atoi(argv[9]);
kernel[7] = atoi(argv[10]);
kernel[8] = atoi(argv[11]);
float divider = atoi(argv[13]);
uint32_t processed = Bitmap_convolution(&bitmap, kernel, divider);
if(processed == 0){
FILE * dest = fopen(argv[15], "wb");
uint32_t written = Bitmap_copyIntoFile(dest, &bitmap);
if(written == 1){
fprintf(stderr, "That did not work\n");
}
fclose(source);
fclose(dest);
Bitmap_destroy(&bitmap);
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work. Images must be 24bpp\n");
fclose(source);
}
}
}
void drawCircle(char **argv) {
// ./main [draw|-d] [circle/-c] [radius:/r:] int [origin:/o:] int int [color:/c:] int int int test.bmp
FILE *source = fopen(argv[12], "rb");
rewind(source);
if (source != NULL) {
if ((strcmp(argv[3], "radius:") == 0 || strcmp(argv[3], "r:") == 0) &&
(strcmp(argv[5], "origin:") == 0 || strcmp(argv[5], "o:") == 0) &&
(strcmp(argv[8], "color:") == 0 || strcmp(argv[8], "c:") == 0)) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0) {
RGB color;
color.red = atoi(argv[9]);
color.green = atoi(argv[10]);
color.blue = atoi(argv[11]);
uint32_t radius = atoi(argv[4]);
Point origin;
origin.x_posn = atoi(argv[6]);
origin.y_posn = atoi(argv[7]);
uint32_t drawn = Bitmap_draw_circle(&bitmap, &origin, radius, &color);
if (drawn == 0) {
FILE * dest = fopen(argv[12], "wb");
rewind(dest);
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if(copied == 1){
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
}
} else {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
}
void drawRectangle(char **argv) {
// ./main [draw|-d] [rectangle/-r] [pointE:/e:] int int [pointF:/f:] int int [pointG:/g:] int int [pointH:/h:] int int [color:/c:] int int int test.bmp
FILE *source = fopen(argv[19], "rb");
rewind(source);
if (source != NULL) {
if ((strcmp(argv[3], "pointE:") == 0 || strcmp(argv[3], "e:") == 0) &&
(strcmp(argv[6], "pointF:") == 0 || strcmp(argv[6], "f:") == 0) &&
(strcmp(argv[9], "pointG:") == 0 || strcmp(argv[9], "g:") == 0) &&
(strcmp(argv[12], "pointH:") == 0 || strcmp(argv[12], "h:") == 0)) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0) {
RGB color;
color.red = atoi(argv[16]);
color.green = atoi(argv[17]);
color.blue = atoi(argv[18]);
Point E;
E.x_posn = atoi(argv[4]);
E.y_posn = atoi(argv[5]);
Point F;
F.x_posn = atoi(argv[7]);
F.y_posn = atoi(argv[8]);
Point G;
G.x_posn = atoi(argv[10]);
G.y_posn = atoi(argv[11]);
Point H;
H.x_posn = atoi(argv[13]);
H.y_posn = atoi(argv[14]);
uint32_t drawn = Bitmap_draw_rect(&bitmap, &E, &F, &G, &H, &color);
if (drawn == 0) {
FILE * dest = fopen(argv[19], "wb");
rewind(dest);
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if(copied == 1){
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
}
void drawTriangle(char **argv) {
// ./main [draw|-d] [triangle/-t] [pointE:/e:] int int [pointF:/f:] int int [pointG:/g:] int int [color:/c:] int int int test.bmp
FILE *source = fopen(argv[16], "rb");
rewind(source);
if (source != NULL) {
if ((strcmp(argv[3], "pointE:") == 0 || strcmp(argv[3], "e:") == 0) &&
(strcmp(argv[6], "pointF:") == 0 || strcmp(argv[6], "f:") == 0) &&
(strcmp(argv[9], "pointG:") == 0 || strcmp(argv[9], "g:") == 0)) {
Bitmap bitmap;
uint32_t scanned = Bitmap_scan(source, &bitmap);
if (scanned == 0) {
RGB color;
color.red = atoi(argv[13]);
color.green = atoi(argv[14]);
color.blue = atoi(argv[15]);
Point E;
E.x_posn = atoi(argv[4]);
E.y_posn = atoi(argv[5]);
Point F;
F.x_posn = atoi(argv[7]);
F.y_posn = atoi(argv[8]);
Point G;
G.x_posn = atoi(argv[10]);
G.y_posn = atoi(argv[11]);
uint32_t drawn = Bitmap_draw_triangle(&bitmap, &E, &F, &G, &color);
if (drawn == 0) {
FILE * dest = fopen(argv[16], "wb");
rewind(dest);
uint32_t copied = Bitmap_copyIntoFile(dest, &bitmap);
if(copied == 1){
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "That did not work\n");
}
} else {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
}