-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
120 lines (76 loc) · 1.96 KB
/
common.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
#include "types.h" // Contains user defined types
#include <stdio.h>
#include <string.h>
#include "common.h"
/* common used function defention */
/* Get image size
* Input: Image file ptr
* Output: width * height * bytes per pixel (3 in our case)
* Description: In BMP Image, width is stored in offset 18,
* and height after that. size is 4 bytes
*/
uint get_image_size_for_bmp(FILE *fptr_image)
{
uint width, height;
// Seek to 18th byte
fseek(fptr_image, 18, SEEK_SET);
// Read the width (an int)
fread(&width, sizeof(int), 1, fptr_image);
// Read the height (an int)
fread(&height, sizeof(int), 1, fptr_image);
fseek(fptr_image, 0, SEEK_SET);
// Return image capacity
return width * height * 3;
}
/* Get Operation Type
* Input: commandline arguments from main
* output: OperationType enum
*/
OperationType check_operation_type(char *argv[])
{
if(!strcmp("-e", argv[1]))
return e_encode;
if(!strcmp("-d", argv[1]))
return e_decode;
return e_unsupported;
}
/* Get file size
* @param FILE *
* return uint file_size
*/
uint get_file_size(FILE *f)
{
fseek(f,0l,SEEK_END);
return ftell(f);
}
/*
* print usage of the program
* @param OperationType mode
*/
void print_usage(OperationType operation_mode)
{
switch(operation_mode)
{
case e_encode:
fprintf(stdout,"usage : \nencoding -> -e <.bmp_file> <.text_file> [output file] \n");
break;
case e_decode:
fprintf(stdout,"usage : \ndecoding -> -d <.bmp_file> [output file]\n");
break;
default:
fprintf(stdout,"usage : \nencoding -> -e <.bmp_file> <.text_file> [output file] \ndecoding -> -d <.bmp_file> [output file]\n");
break;
}
}
/*
* print messages to stdout
* @params const char *, MessageType
* return void
*/
void print_message(const char *msg, MessageType type)
{
if(type == e_error)
fprintf(stderr,"Error: %s\n",msg);
else if(type == e_info)
fprintf(stdin,"INFO: %s\n",msg);
}