-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataOP.c
231 lines (166 loc) · 4.37 KB
/
dataOP.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
#include<stdio.h>
#include<stdlib.h>
#include"./dataOP.h"
void blockdrawMirror(unsigned char *In, int IWidth, int IHeight, int X, int Y, unsigned char** Out, int OWidth, int OHeight)
{
unsigned char *block;
int i,j,k,l;
block=malloc(sizeof(char)*OWidth*OHeight);
for(i=0;i<OHeight;i++)
for(j=0;j<OWidth;j++)
{
k=Y+i;
l=X+j;
if(l<0)
l=abs(l)-1;
else if(l>=IWidth)
l=IWidth-(l-IWidth+1);
if(k<0)
k=abs(k)-1;
else if(k>=IWidth)
k=IHeight-(k-IHeight+1);
block[i*OWidth+j]=In[k*IWidth+l];
//used to print output
//printf("%d,%d:%d,%d : {%u,%u}\n",i,j,k,l,block[i*OWidth+j],In[k*IWidth+l]);
}
*Out=block;
}
void blockinsert(unsigned char *In, int IWidth, int IHeight, int X, int Y, unsigned char* Out, int OWidth, int OHeight)
{
int i,j;
for(i=0;i<OWidth;i++)
for(j=0;j<OHeight;j++)
In[(X+i)*IWidth+Y+j]=Out[i*OWidth+j];
}
void wavelet(unsigned char *block, int Width, int Height, int **pointer)
{
int i,j;
unsigned int max=0,min=10000;
int *tmp,*tmp2;
if(Width%2 | Height%2)
{
printf("error, block's width or height is odd");
return;
}
if(Height<0)
Height=abs(Height);
tmp = malloc(sizeof(int)*Width*Height);
tmp2 = malloc(sizeof(int)*Width*Height);
//vertical
for(i=0;i<Height;i++)
for(j=0;j<Width/2;j++)
{
tmp[i*Width+j] = block[i*Width+j*2]+block[i*Width+j*2+1];
tmp[i*Width+j+Width/2] = (int)block[i*Width+j*2]-(int)block[i*Width+j*2+1];
}
//horizontal
for(i=0;i<Height/2;i++)
for(j=0;j<Width;j++)
{
tmp2[i*Width+j] = tmp[i*2*Width+j]+tmp[(i*2+1)*Width+j];
tmp2[(i+Height/2)*Width+j] = tmp[i*2*Width+j]-tmp[(i*2+1)*Width+j];
}
//normalize
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
tmp2[i*Width+j] = tmp2[i*Width+j]/4;
}
*pointer=tmp2;
free(tmp);
}
void binerizeItUC(int *In, int Width, int Height, unsigned char**Out, int T)
{
int i,j;
int *Iblock;
unsigned char *Oblock;
Iblock = malloc(sizeof(int)*Width*Height);
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
Iblock[i*Width+j] = abs(In[i*Width+j]);
Oblock = malloc(sizeof(int)*Width*Height);
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
if(T==256)
Oblock[i*Width+j] = Iblock[i*Width+j];
else if(Iblock[i*Width+j]>T)
Oblock[i*Width+j] = 255;
else
Oblock[i*Width+j]=0;
*Out = Oblock;
free(Iblock);
}
void binerizeUCtUC(unsigned char *Iblock, int Width, int Height, unsigned char **Out, int T)
{
int i,j;
unsigned char *Oblock;
Oblock = malloc(sizeof(int)*Width*Height);
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
if(T==256)
Oblock[i*Width+j] = Iblock[i*Width+j];
else if(Iblock[i*Width+j]>T)
Oblock[i*Width+j] = 255;
else
Oblock[i*Width+j]=0;
*Out = Oblock;
}
void colorRGBtYUV(struct charcontainer_3 *In, int Width, int Height)
{
int i,j;
struct intcontainer_3 *block, *block2;
block = malloc(sizeof(struct intcontainer_3));
block->A=malloc(sizeof(int)*Width*Height);
block->B=malloc(sizeof(int)*Width*Height);
block->C=malloc(sizeof(int)*Width*Height);
block2 = malloc(sizeof(struct intcontainer_3));
block2->A=malloc(sizeof(int)*Width*Height);
block2->B=malloc(sizeof(int)*Width*Height);
block2->C=malloc(sizeof(int)*Width*Height);
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
block->A[i*Width+j]=(int)In->RY[i*Width+j];
block->B[i*Width+j]=(int)In->GU[i*Width+j];
block->C[i*Width+j]=(int)In->BV[i*Width+j];
}
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
block2->A[i*Width+j]=(block->A[i*Width+j]*299+block->B[i*Width+j]*587+block->C[i*Width+j]*114)/1000;
block2->B[i*Width+j]=(block->B[i*Width+j]-block2->A[i*Width+j])*492/1000+128;
block2->C[i*Width+j]=(block->C[i*Width+j]-block2->A[i*Width+j])*877/1000+128;
}
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
In->RY[i*Width+j]=(unsigned char)block2->A[i*Width+j];
In->GU[i*Width+j]=(unsigned char)block2->B[i*Width+j];
In->BV[i*Width+j]=(unsigned char)block2->C[i*Width+j];
}
free(block->A);
free(block->B);
free(block->C);
free(block);
free(block2->A);
free(block2->B);
free(block2->C);
free(block2);
}
void avgFilter(unsigned char *Img, int Width, int Height, int N)
{
int i,j,k,l;
int tmp;
unsigned char *block;
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
blockdrawMirror(Img,Width,Height,j-N/2,i-N/2,&block,N,N);
tmp = 0;
for(k=0;k<N;k++)
for(l=0;l<N;l++)
tmp+=block[k*N+l];
Img[i*Width+j] = tmp/(N*N);
free(block);
}
}