-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemboss.c
90 lines (78 loc) · 2.11 KB
/
emboss.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
#include <stdio.h>
#include <string.h>
#define MAX 32
//int count[MAX]={0};
int main()
{
char str[30],str1[30];
printf("Image name : ");
scanf("%[^\n]%*c", str);
strcpy(str1,str);
strcat(str,".bmp");
FILE *fIn = fopen(str,"r"); //Input File name
strcat(str1,"_emboss.bmp");
FILE *fOut = fopen(str1,"w+"); //Output File name
int i,j,k,l,ch;
unsigned char byte[54],colorTable[1024];
float sum1,sum2,sum3;
float filter[3][3] = {{-1,-1,0},{-1,0,1},{0,1,1}};
if(fIn==NULL) // check if the input file has not been opened succesfully.
{
printf("File does not exist\n");
}
for(i=0;i<54;i++) //read the 54 byte header from fIn
{
byte[i] = getc(fIn);
}
fwrite(byte,sizeof(unsigned char),54,fOut); //write the header back
// extract image height, width and bitDepth from imageHeader
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];
printf("width: %d\n",width);
printf("height: %d\n",height );
int size = height*width; //calculate image size
if(bitDepth <= 8) //if ColorTable present, extract it.
{
fread(colorTable,sizeof(unsigned char),1024,fIn);
fwrite(colorTable,sizeof(unsigned char),1024,fOut);
}
unsigned char buffer[width][height][3]; //to store the image data
unsigned char out_buffer[width][height][3];
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
buffer[i][j][0] = getc(fIn);
buffer[i][j][1] = getc(fIn);
buffer[i][j][2] = getc(fIn);
}
for(i=0;i<height-1;i++)
{
for(j=0;j<width-1;j++)
{
sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
for(k=i;k<=i+2;k++)
{
for(l=j;l<=j+2;l++)
{
sum1 = sum1 + buffer[k][l][0] * filter[l-j][k-i];
sum2 = sum2 + buffer[k][l][1] * filter[l-j][k-i];
sum3 = sum3 + buffer[k][l][2] * filter[l-j][k-i];
}
}
out_buffer[i][j][0] = sum1;
out_buffer[i][j][1] = sum2;
out_buffer[i][j][2] = sum3;
}
}
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
putc(out_buffer[i][j][0],fOut);
putc(out_buffer[i][j][1],fOut);
putc(out_buffer[i][j][2],fOut);
} //write back to the output image
fclose(fIn);
fclose(fOut);
return 0;
}