-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalLib.c
265 lines (227 loc) · 7.9 KB
/
personalLib.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
#include "personalLib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* This function prints the help menu
* @Param: /
* @returns void
*/
void helpmenu(){
printf("-i met daarachter de inputfile\n ");
printf("voorbeeld\n");
printf("./splitchannels -i meme.bmp\n");
}
/*
* This function will read an input file and create 3 files, each contain only the red, green and blue colors of the respective pixels. Also a 4th file with the colors inverted
* char pointer used to store the input bmp file name
* @return void
*/
void code(char* inputFileName){
FILE *f = fopen(inputFileName, "rb");
if (f == NULL) {
printf("something went wrong while opening file");
exit(1);
}
int imageSize = 0;
imageSize = getSizeBmp(f);
int arraySize = imageSize/3;
pixel* RGB = malloc(arraySize * sizeof(*RGB));
unsigned char *bmpheader = malloc(54);
readInputBmpfile(RGB, imageSize, bmpheader, f);
outputBMP(RGB, bmpheader, imageSize,inputFileName);
fclose(f);
free(inputFileName);
free(bmpheader);
free(RGB);
}
/*
*This function gets the size of the bmp image
* @param: File pointer f of input bmp file
* @return: returns the image size as int imageSize
*/
int getSizeBmp(FILE *f){
unsigned char bmpheader[54];
fread(bmpheader, sizeof(unsigned char ), 54, f);
unsigned int breedte = bmpheader[18] | bmpheader[19] << 8 | bmpheader[20] << 16 | bmpheader[21] << 24;
unsigned int hoogte = bmpheader[22] | bmpheader[23] << 8 | bmpheader[24] << 16 | bmpheader[25] << 24;
int imageSize = breedte * hoogte * 3;
return imageSize;
}
/*
* This function read the pixels of the input bmp file and stores them in the array of struct RGB and stores header information into pointer bmpheader
* @param: pointer array of struct pixel
* @param: int imagesize = size of input image
* @param: unsigned char pointer bmpheader: header information of input bmp file
* @param: File pointer f of input bmp file
* @return: void
*/
void readInputBmpfile(pixel* RGB, int imageSize, unsigned char* bmpheader, FILE* f){
rewind(f);
unsigned char tempRGB[imageSize];
fread(bmpheader, sizeof(unsigned char ), 54, f);
fread(tempRGB, sizeof(unsigned char), imageSize, f);
int x = 0;
int arraySize = imageSize/3;
for(int i = 0; i < arraySize; i++){
RGB[i].blue = tempRGB[x];
RGB[i].green = tempRGB[x+1];
RGB[i].red = tempRGB[x+2];
x = x + 3;
}
}
/*
* This function creates 3 files, each contain only the red, green and blue colors of the respective pixels. Also a 4th file with the colors inverted
* @param: pointer array of struct pixel
* @param: unsigned char pointer bmpheader: header information of input bmp file
* @param: int imagesize = size of input image
* @param: char pointer used to store the input bmp file name
* @return: void
*/
void outputBMP(pixel* RGB, unsigned char* bmpheader, int imageSize, char* inputFileName){
removeBMPfromString(inputFileName);
char strRed[] = "_red.bmp";
char strGreen[] = "_green.bmp";
char strBlue[] = "_blue.bmp";
char strInverted[] = "_inverted.bmp";
unsigned char* RGBout = malloc(imageSize * sizeof(*RGBout));
char* outputName;
//rood
outputRGBRed(RGBout, RGB, imageSize);
outputName = outputBMPFileName(inputFileName, strRed); //voor rood
createOutput(bmpheader , outputName, RGBout, imageSize ); // voor rood
//groen
outputRGBGreen(RGBout, RGB, imageSize);
outputName = outputBMPFileName(inputFileName, strGreen); //voor groen
createOutput(bmpheader, outputName, RGBout, imageSize);//voor groen
//blauw
outputRGBBlue(RGBout, RGB, imageSize);
outputName = outputBMPFileName(inputFileName, strBlue);
createOutput(bmpheader, outputName, RGBout, imageSize);
//geinverteerd
outputRGBinverted(RGBout, RGB, imageSize);
outputName = outputBMPFileName(inputFileName, strInverted);
createOutput(bmpheader, outputName, RGBout, imageSize);
free(outputName);
free(RGBout);
}
/*
* This function removes .bmp from the name of the input bmp file
* @param: char pointer used to store the input bmp file name
* @return: void
*/
void removeBMPfromString(char* inputFileName){
int a = strlen(inputFileName);
inputFileName[a-1] = '\0';
inputFileName[a-2] = '\0';
inputFileName[a-3] = '\0';
inputFileName[a-4] = '\0';
}
/*
* This function puts _color.bmp behind the name of the outputFile example: inputFileName = meme.bmp the .bmp is removed in removeBMPfromString this function makes it for color red meme_red.bmp
* @param: char pointer used to store the input bmp file name
* @param: char str[] = contains what has to come behind inputFileName ex: meme_red.bmp str[] contains _red.bmp
* @return: char* outputname = outputname of file
*/
char* outputBMPFileName(char* inputFileName, char str[]){
int sizeName = strlen(inputFileName) + strlen(str);
char* outputName = (char*) malloc(sizeName);
strcpy(outputName, inputFileName);
strcat(outputName, str);
return outputName;
}
/*
* This function creates the outputFile and writes the outputfile
* @param: unsigned char pointer bmpheader: header information of input bmp file
* @param: char* outputName= name of outputfile
* @param: unsigned char pointer RGBout= contains the pixels of the outputFile
* @param: int imagesize = size of input image
* @return: void
*/
void createOutput(unsigned char* bmpheader, char* outputName, unsigned char* RGBout, int imageSize){
FILE *fp;
fp = fopen(outputName, "wb");
if(fp == NULL){
printf("file does not create\n");
exit(0);
}
printf("file created succesfully\n");
fwrite(bmpheader, 1, 54, fp);
fwrite(RGBout, sizeof(unsigned char), imageSize, fp);
printf("output finished\n");
fclose(fp);
}
/*
* This function creates the output rgb for red
* @param: unsigned char pointer RGBRed = contains the output rgb
* @param: pointer array of struct pixel
* @param: int imagesize = size of input image
* @return: void
*/
void outputRGBRed(unsigned char* RGBRed, pixel* RGB, int imageSize){
int x = 0;
int j = imageSize / 3;
for(int i = 0; i < j; i++ ){
RGBRed[x] = 0;
RGBRed[x+1] = 0;
RGBRed[x+2] = 0;
RGBRed[x+2] = RGB[i].red;
x = x +3;
}
}
/*
* This function creates the output rgb for green
* @param: unsigned char pointer RGBGreen = contains the output rgb
* @param: pointer array of struct pixel
* @param: int imageSize = size of input image
* @return: void
*/
void outputRGBGreen(unsigned char* RGBGreen, pixel* RGB, int imageSize){
int x = 0;
int j = imageSize / 3;
for(int i = 0; i < j; i++ ){
RGBGreen[x] = 0;
RGBGreen[x+1] = 0;
RGBGreen[x+2] = 0;
RGBGreen[x+1] = RGB[i].green;
x = x +3;
}
}
/*
* This function creates the output rgb for blue
* @param: unsigned char pointer RGBBlue = contains the output rgb
* @param: pointer array of struct pixel
* @param int imageSize = size of input image
* @return: void
*/
void outputRGBBlue(unsigned char* RGBBlue, pixel* RGB, int imageSize){
int x = 0;
int j = imageSize / 3;
for(int i = 0; i < j; i++){
RGBBlue[x] = 0;
RGBBlue[x+1] = 0;
RGBBlue[x+2] = 0;
RGBBlue[x] = RGB[i].blue;
x = x + 3;
}
}
/*
* This function creates the output rgb for inverted
* @param: unsigned char pointer RGBinverted = contains the output rgb
* @param: pointer array of struct pixel
* @param: int imageSize = size of input image
* @return: void
*/
void outputRGBinverted(unsigned char* RGBinverted, pixel* RGB, int imageSize){
int x = 0;
int j = imageSize / 3;
for(int i = 0; i < j; i++ ){
RGBinverted[x] = 0;
RGBinverted[x+1] = 0;
RGBinverted[x+2] = 0;
RGBinverted[x] = 255 - RGB[i].blue;
RGBinverted[x+1] = 255 - RGB[i].green;
RGBinverted[x+2] = 255 - RGB[i].red;
x = x + 3;
}
}