Repository navigation
Expand file tree
/
Copy pathSource.cpp
More file actions
307 lines (250 loc) · 8.44 KB
/
Copy pathSource.cpp
File metadata and controls
307 lines (250 loc) · 8.44 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory>
#include <string.h>
/*------structure*/
typedef struct {int rows;
int cols;
unsigned char* data;}
sImage;
/*------prototypes-----*/
long getImageInfo(FILE*, long, int);
void copyImageInfo(FILE* inputFile, FILE* outputFile);
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors);
char* UserInput(int path);//function I'm adding in to have an arbitrary file name
int main(int argc, char* argv[]);
//---some globals---
char* filenamep=UserInput(1);//it'll wait for user to input file name (user needs to include .bmp)
char* outputfile=UserInput(2);//it'll wait for user to input name of output file name (user needs to include .bmp)
char* Prostype = UserInput(3); //user will pick the type choose a particular processing type
char* *variable= &filenamep;
char text[20];
char output[20];
char proccessing[20];
//---call for user input---
int y = main(2, variable);
int main(int argc, char* argv[]){
FILE *bmpInput, *bmpOutput;
sImage originalImage;
sImage edgeImage;
unsigned int X, Y;
int I, J;
long sumX, sumY;
int nColors, SUM;
unsigned long vectorSize;
unsigned long fileSize;
int GX[3][3];
int GY[3][3];
unsigned char *pChar, someChar;
unsigned int row, col;
someChar = '0'; pChar = &someChar;
//3x3 Gx Sobel mask
GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
//3x3 Gy Sobel mask
GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1;
GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0;
GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;
if(proccessing == "sobel"){
//do nothing continue to the code
//maybe need a string or need to add in my own comparator to sobel
return 42;
}
else if(proccessing == "Gaussian blur"){
//gaussian blur filter
}
else if(proccessing == "Object detection"){
//object detection
}
if(argc < 2) {
printf("Usage: %s bmpInput.bmp\n", argv[0]);
exit(0);
};
printf("Reading filename %s\n", argv[0]);
/*-------DECLARE INPUT & OUTPUT FILES-------*/
bmpInput = fopen(argv[0], "rb");
bmpOutput = fopen(outputfile, "wb");
/*---SET POINTER TO BEGINNING OF FILE----*/
fseek(bmpInput, 0L, SEEK_END);
/*-------GET INPUT BMP DATA--------*/
fileSize = getImageInfo(bmpInput, 2, 4);
originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
edgeImage.rows = originalImage.rows;
edgeImage.cols = originalImage.cols;
/*--------PRINT DATA TO SCREEN----------*/
printf("Width: %d\n", originalImage.cols);
printf("Height: %d\n", originalImage.rows);
printf("File size: %lu\n", fileSize);
nColors = (int)getImageInfo(bmpInput, 46, 4);
printf("nColors: %d\n", nColors);
/*------ALLOCATE MEMORY FOR FILES--------*/
vectorSize = fileSize - (14+40+4*nColors);
printf("vectorSize: %lu\n", vectorSize);
edgeImage.data = (unsigned char*) malloc(vectorSize*sizeof(unsigned char));
if(edgeImage.data == NULL) {
printf("Failed to malloc edgeImage.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize);
originalImage.data = (unsigned char*) malloc(vectorSize*sizeof(unsigned char));
if(originalImage.data == NULL) {
printf("Failed to malloc originalImage.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize);
/*------COPY HEADER AND COLOR TABLE---------*/
copyImageInfo(bmpInput, bmpOutput);
copyColorTable(bmpInput, bmpOutput, nColors);
fseek(bmpInput, (14+40+4*nColors), SEEK_SET);
fseek(bmpOutput, (14+40+4*nColors), SEEK_SET);
/* Read input.bmp and store it's raster data into originalImage.data */
for(row=0; row<=originalImage.rows-1; row++) {
for(col=0; col<=originalImage.cols-1; col++) {
fread(pChar, sizeof(char), 1, bmpInput);
*(originalImage.data + row*originalImage.cols + col) = *pChar;
}
}
//----Sobel Algorithm starts here----
for(Y=0; Y<=(originalImage.rows-1); Y++) {
for(X=0; X<=(originalImage.cols-1); X++) {
sumX = 0;
sumY = 0;
/* image boundaries */
if(Y==0 || Y==originalImage.rows-1)
SUM = 0;
else if(X==0 || X==originalImage.cols-1)
SUM = 0;
/* Convolution starts here */
else {
/*-------X GRADIENT APPROXIMATION------*/
for(I=-1; I<=1; I++) {
for(J=-1; J<=1; J++) {
sumX = sumX + (int)( (*(originalImage.data + X + I +
(Y + J)*originalImage.cols)) * GX[I+1][J+1]);
}
}
/*-------Y GRADIENT APPROXIMATION-------*/
for(I=-1; I<=1; I++) {
for(J=-1; J<=1; J++) {
sumY = sumY + (int)( (*(originalImage.data + X + I +
(Y + J)*originalImage.cols)) * GY[I+1][J+1]);
}
}
/*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/
SUM = abs(sumX) + abs(sumY);
}
if(SUM>255) SUM=255;
if(SUM<0) SUM=0;
//threshold---arbritary up for changing
if(SUM>20) SUM=255; //add in a histogram flattening to the filter
else SUM=0;
*(edgeImage.data + X + Y*originalImage.cols) = 255 - (unsigned char)(SUM);
if( fwrite((edgeImage.data + X + Y*originalImage.cols),sizeof(char),1,bmpOutput)!=1){
printf("it failed writing on column: %d row: %d",Y,X);}
}
}
printf("See %s for results\n",output);
fclose(bmpInput);
fclose(bmpOutput);
free(edgeImage.data); /* Finished with edgeImage.data */
free(originalImage.data); /* Finished with originalImage.data */
return 0;
}
/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L;
unsigned char dummy;
int i;
dummy = '0';
ptrC = &dummy;
fseek(inputFile, offset, SEEK_SET);
for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
/* calculate value based on adding bytes */
value = (long)(value + (*ptrC)*(pow(256, (i-1))));
}
return(value);
} /* end of getImageInfo */
/*-------------COPIES HEADER AND INFO HEADER----------------*/
void copyImageInfo(FILE* inputFile, FILE* outputFile)
{
unsigned char *ptrC;
unsigned char dummy;
int i;
dummy = '0';
ptrC = &dummy;
fseek(inputFile, 0L, SEEK_SET);
fseek(outputFile, 0L, SEEK_SET);
for(i=0; i<=50; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}
}
/*----------------COPIES COLOR TABLE-----------------------------*/
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
{
unsigned char *ptrC;
unsigned char dummy;
int i;
dummy = '0';
ptrC = &dummy;
fseek(inputFile, 54L, SEEK_SET);
fseek(outputFile, 54L, SEEK_SET);
for(i=0; i<=(4*nColors); i++) /* there are (4*nColors) bytesin color table */
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}
}
//-----TAKES IN FILE TO MANIPULATE (SYNTAX IS "FILENAME.bmp"-----
char* UserInput(int path){
if(path==1){
fputs("enter the input file name: ", stdout);
fflush(stdout);
if ( fgets(text, sizeof text, stdin) != NULL ){
char *newline = strchr(text, '\n'); /* search for newline character */
if ( newline != NULL ){
*newline = '\0'; /* overwrite trailing newline */
}
printf("file name = \"%s\"\n", output);
}
return text;
}
if(path==2){
fputs("enter the output file name: ", stdout);
fflush(stdout);
if ( fgets(output, sizeof output, stdin) != NULL ){
char *newline = strchr(output, '\n'); /* search for newline character */
if ( newline != NULL ){
*newline = '\0'; /* overwrite trailing newline */
}
printf("file name = \"%s\"\n", output);
}
return output;
}
//Add in a feature to selct which Type of proccessing to use (sobel, Gaussian blur, Object Detection
if(path==3){
fputs("enter the Processing type (sobel, Gaussian blur, Object Detection): ",stdout);
fflush(stdout);
if ( fgets(proccessing, sizeof proccessing, stdin) != NULL ){
char *newline = strchr(proccessing, '\n'); /* search for newline character */
if ( newline != NULL ){
*newline = '\0'; /* overwrite trailing newline */
}
printf("file name = \"%s\"\n", proccessing);
}
return proccessing;
}
}
int Object_Detection(FILE *bmpinput, FILE *bmpoutput)
{
int NumberObject=0;
return NumberObject;
}