diff options
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/c/texture.c')
-rw-r--r-- | SD-VBS/benchmarks/texture_synthesis/src/c/texture.c | 396 |
1 files changed, 396 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/texture_synthesis/src/c/texture.c b/SD-VBS/benchmarks/texture_synthesis/src/c/texture.c new file mode 100644 index 0000000..fb0f1ae --- /dev/null +++ b/SD-VBS/benchmarks/texture_synthesis/src/c/texture.c | |||
@@ -0,0 +1,396 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "texture.h" | ||
6 | #include <math.h> | ||
7 | |||
8 | int vrstartx, vrfinishx, vrstarty, vrfinishy; | ||
9 | extern params data; | ||
10 | int* candlistx, *candlisty; | ||
11 | int *atlas; | ||
12 | int anotherpass=0,maxcand = 40; | ||
13 | F2D *target, *result; | ||
14 | int *xloopout, *yloopout; | ||
15 | int *xloopin, *yloopin; | ||
16 | |||
17 | double compare_rest(F2D *image,int x, int y, F2D *tar,int x1, int y1, params* data); | ||
18 | |||
19 | /********************************* | ||
20 | This is the main texture synthesis function. Called just once | ||
21 | from main to generate image from 'image' into 'result'. | ||
22 | Synthesis parameters (image and neighborhood sizes) are in global | ||
23 | 'data-> structure. | ||
24 | *********************************/ | ||
25 | |||
26 | //void create_texture(F2D *image, F2D *result, params *data) | ||
27 | void create_texture(F2D *image, params *data) | ||
28 | { | ||
29 | |||
30 | int i,j,k, ncand, bestx,besty; | ||
31 | double diff,curdiff; | ||
32 | int tsx,tsy; | ||
33 | srand48(1); | ||
34 | candlistx = (int*)malloc(sizeof(int)*(data->localx*(data->localy+1)+1)); | ||
35 | candlisty = (int*)malloc(sizeof(int)*(data->localx*(data->localy+1)+1)); | ||
36 | // if(!anotherpass) init(result, image,data); | ||
37 | if(!anotherpass) init(image,data); | ||
38 | |||
39 | for(i=0;i<data->heightout-data->localy/2;i++) | ||
40 | { | ||
41 | for(j=0;j<data->widthout;j++) | ||
42 | { | ||
43 | // First, create a list of candidates for particular pixel. | ||
44 | if(anotherpass) ncand = create_all_candidates(j,i, data); | ||
45 | else ncand = create_candidates(j,i, data); | ||
46 | |||
47 | // If there are multiple candidates, choose the best based on L_2 norm | ||
48 | |||
49 | if(ncand > 1) | ||
50 | { | ||
51 | diff = 1e10; | ||
52 | for(k=0;k<ncand;k++) | ||
53 | { | ||
54 | curdiff = compare_neighb(image, arrayref(candlistx,k),arrayref(candlisty,k),result,j,i, data); | ||
55 | curdiff += compare_rest(image,arrayref(candlistx,k),arrayref(candlisty,k),target,j,i,data); | ||
56 | if(curdiff < diff) | ||
57 | { | ||
58 | diff = curdiff; | ||
59 | bestx = arrayref(candlistx,k); | ||
60 | besty = arrayref(candlisty,k); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | else | ||
65 | { | ||
66 | bestx = arrayref(candlistx,0); | ||
67 | besty = arrayref(candlisty,0); | ||
68 | } | ||
69 | |||
70 | // Copy the best candidate to the output image and record its position | ||
71 | // in the atlas (atlas is used to create candidates) | ||
72 | |||
73 | asubsref(result,a(j,i,data->widthout)+R) = asubsref(image,a(bestx,besty,data->widthin)+R); | ||
74 | // asubsref(result,a(j,i,data->widthout)+G) = asubsref(image,a(bestx,besty,data->widthin)+G); | ||
75 | // asubsref(result,a(j,i,data->widthout)+B) = asubsref(image,a(bestx,besty,data->widthin)+B); | ||
76 | arrayref(atlas,aa(j,i)) = bestx; | ||
77 | arrayref(atlas,aa(j,i)+1) = besty; | ||
78 | } | ||
79 | } | ||
80 | |||
81 | // Use full neighborhoods for the last few rows. This is a small | ||
82 | // fraction of total area - can be ignored for optimization purposes. | ||
83 | |||
84 | for(;i<data->heightout;i++) | ||
85 | { | ||
86 | for(j=0;j<data->widthout;j++) | ||
87 | { | ||
88 | ncand = create_all_candidates(j,i,data); | ||
89 | if(ncand > 1) | ||
90 | { | ||
91 | diff = 1e10; | ||
92 | for(k=0;k<ncand;k++) | ||
93 | { | ||
94 | curdiff = compare_full_neighb(image, arrayref(candlistx,k),arrayref(candlisty,k),result,j,i, data); | ||
95 | if(curdiff < diff) | ||
96 | { | ||
97 | diff = curdiff; | ||
98 | bestx = arrayref(candlistx,k); | ||
99 | besty = arrayref(candlisty,k); | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | else | ||
104 | { | ||
105 | bestx = arrayref(candlistx,0); | ||
106 | besty = arrayref(candlisty,0); | ||
107 | } | ||
108 | |||
109 | asubsref(result,a(j,i,data->widthout)+R) = asubsref(image,a(bestx,besty,data->widthin)+R); | ||
110 | // asubsref(result,a(j,i,data->widthout)+G) = asubsref(image,a(bestx,besty,data->widthin)+G); | ||
111 | // asubsref(result,a(j,i,data->widthout)+B) = asubsref(image,a(bestx,besty,data->widthin)+B); | ||
112 | arrayref(atlas,aa(j,i)) = bestx; | ||
113 | arrayref(atlas,aa(j,i)+1) = besty; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | /********************************* | ||
118 | End of main texture synthesis loop | ||
119 | *********************************/ | ||
120 | |||
121 | for(i=0;i<data->localy/2;i++) | ||
122 | { | ||
123 | for(j=0;j<data->widthout;j++) | ||
124 | { | ||
125 | ncand = create_all_candidates(j,i,data); | ||
126 | if(ncand > 1) | ||
127 | { | ||
128 | diff = 1e10; | ||
129 | for(k=0;k<ncand;k++) | ||
130 | { | ||
131 | curdiff = compare_full_neighb(image,arrayref(candlistx,k),arrayref(candlisty,k),result,j,i, data); | ||
132 | if(curdiff < diff) | ||
133 | { | ||
134 | diff = curdiff; | ||
135 | bestx = arrayref(candlistx,k); | ||
136 | besty = arrayref(candlisty,k); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | bestx = arrayref(candlistx,0); | ||
143 | besty = arrayref(candlisty,0); | ||
144 | } | ||
145 | |||
146 | asubsref(result,a(j,i,data->widthout)+R) = asubsref(image,a(bestx,besty,data->widthin)+R); | ||
147 | // asubsref(result,a(j,i,data->widthout)+G) = asubsref(image,a(bestx,besty,data->widthin)+G); | ||
148 | // asubsref(result,a(j,i,data->widthout)+B) = asubsref(image,a(bestx,besty,data->widthin)+B); | ||
149 | arrayref(atlas,aa(j,i)) = bestx; | ||
150 | arrayref(atlas,aa(j,i)+1) = besty; | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
155 | // Creates a list of valid candidates for given pixel using only L-shaped causal area | ||
156 | |||
157 | int create_candidates(int x,int y, params* data) | ||
158 | { | ||
159 | int address,i,j,k,n = 0; | ||
160 | for(i=0;i<=data->localy/2;i++) | ||
161 | { | ||
162 | for(j=-data->localx/2;j<=data->localx/2;j++) | ||
163 | { | ||
164 | if(i==0 && j>=0) | ||
165 | continue; | ||
166 | address = aa( arrayref(xloopout,x+j), arrayref(yloopout,y-i) ); | ||
167 | arrayref(candlistx,n) = arrayref(atlas,address) - j; | ||
168 | arrayref(candlisty,n) = arrayref(atlas,address+1) + i; | ||
169 | |||
170 | if( arrayref(candlistx,n) >= vrfinishx || arrayref(candlistx,n) < vrstartx) | ||
171 | { | ||
172 | arrayref(candlistx,n) = vrstartx + (int)(drand48()*(vrfinishx-vrstartx)); | ||
173 | arrayref(candlisty,n) = vrstarty + (int)(drand48()*(vrfinishy-vrstarty)); | ||
174 | n++; | ||
175 | continue; | ||
176 | } | ||
177 | |||
178 | if( arrayref(candlisty,n) >= vrfinishy ) | ||
179 | { | ||
180 | arrayref(candlisty,n) = vrstarty + (int)(drand48()*(vrfinishy-vrstarty)); | ||
181 | arrayref(candlistx,n) = vrstartx + (int)(drand48()*(vrfinishx-vrstartx)); | ||
182 | n++; | ||
183 | continue; | ||
184 | } | ||
185 | |||
186 | for(k=0;k<n;k++) | ||
187 | { | ||
188 | if( arrayref(candlistx,n) == arrayref(candlistx,k) && arrayref(candlisty,n) == arrayref(candlisty,k)) | ||
189 | { | ||
190 | n--; | ||
191 | break; | ||
192 | } | ||
193 | } | ||
194 | n++; | ||
195 | } | ||
196 | } | ||
197 | |||
198 | return n; | ||
199 | } | ||
200 | |||
201 | // Created a list of candidates using the complete square around the pixel | ||
202 | |||
203 | int create_all_candidates(int x,int y, params* data) | ||
204 | { | ||
205 | int address,i,j,k,n = 0; | ||
206 | for(i=-data->localy/2;i<=data->localy/2;i++) | ||
207 | { | ||
208 | for(j=-data->localx/2;j<=data->localx/2;j++) | ||
209 | { | ||
210 | if(i==0 && j>=0) | ||
211 | continue; | ||
212 | // printf("Entering = (%d,%d)\n", i,j); | ||
213 | address = aa( arrayref(xloopout,x+j), arrayref(yloopout,y-i) ); | ||
214 | arrayref(candlistx,n) = arrayref(atlas,address)-j; | ||
215 | arrayref(candlisty,n) = arrayref(atlas,address+1)+i; | ||
216 | |||
217 | if( arrayref(candlistx,n) >= vrfinishx || arrayref(candlistx,n) < vrstartx) | ||
218 | { | ||
219 | arrayref(candlistx,n) = vrstartx + (int)(drand48()*(vrfinishx-vrstartx)); | ||
220 | arrayref(candlisty,n) = vrstarty + (int)(drand48()*(vrfinishy-vrstarty)); | ||
221 | n++; | ||
222 | // printf("1: (%d,%d)\t%d\n", i,j,n); | ||
223 | continue; | ||
224 | } | ||
225 | |||
226 | if( arrayref(candlisty,n) >= vrfinishy || arrayref(candlisty,n) < vrstarty) | ||
227 | { | ||
228 | arrayref(candlisty,n) = vrstarty + (int)(drand48()*(vrfinishy-vrstarty)); | ||
229 | arrayref(candlistx,n) = vrstartx + (int)(drand48()*(vrfinishx-vrstartx)); | ||
230 | n++; | ||
231 | // printf("2: (%d,%d)\t%d\n", i,j,n); | ||
232 | continue; | ||
233 | } | ||
234 | |||
235 | for(k=0;k<n;k++) | ||
236 | { | ||
237 | if( arrayref(candlistx,n) == arrayref(candlistx,k) && arrayref(candlisty,n) == arrayref(candlisty,k) ) | ||
238 | { | ||
239 | n--; | ||
240 | // printf("3: (%d,%d)\t%d\n", i,j,n); | ||
241 | break; | ||
242 | } | ||
243 | } | ||
244 | n++; | ||
245 | // printf("4: (%d,%d)\t%d\n", i,j,n); | ||
246 | } | ||
247 | } | ||
248 | |||
249 | return n; | ||
250 | } | ||
251 | |||
252 | // Initializes the output image and atlases to a random collection of pixels | ||
253 | |||
254 | //void init(F2D *result, F2D *image, params* data) | ||
255 | void init(F2D *image, params* data) | ||
256 | { | ||
257 | int i,j,tmpx,tmpy; | ||
258 | vrstartx = data->localx/2; vrstarty = data->localy/2; | ||
259 | vrfinishx = data->widthin-data->localx/2; | ||
260 | vrfinishy = data->heightin-data->localy/2; | ||
261 | for(i=0;i<data->heightout;i++) | ||
262 | { | ||
263 | for(j=0;j<data->widthout;j++) | ||
264 | { | ||
265 | if( | ||
266 | asubsref(target,a(j,i,data->widthout)+R) == 1.0 | ||
267 | // && asubsref(target,a(j,i,data->widthout)+G) == 1.0 | ||
268 | // && asubsref(target,a(j,i,data->widthout)+B) == 1.0 | ||
269 | ) | ||
270 | { | ||
271 | tmpx = vrstartx + (int)(drand48()*(vrfinishx-vrstartx)); | ||
272 | tmpy = vrstarty + (int)(drand48()*(vrfinishy-vrstarty)); | ||
273 | if(!anotherpass) | ||
274 | { | ||
275 | arrayref(atlas,aa(j,i)) = tmpx; | ||
276 | arrayref(atlas,aa(j,i)+1) = tmpy; | ||
277 | asubsref(result,a(j,i,data->widthout)+R) = asubsref(image,a(tmpx,tmpy,data->widthin)+R); | ||
278 | // asubsref(result,a(j,i,data->widthout)+G) = asubsref(image,a(tmpx,tmpy,data->widthin)+G); | ||
279 | // asubsref(result,a(j,i,data->widthout)+B) = asubsref(image,a(tmpx,tmpy,data->widthin)+B); | ||
280 | } | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | |||
285 | return; | ||
286 | } | ||
287 | |||
288 | |||
289 | // Compares two square neighborhoods, returns L_2 difference | ||
290 | |||
291 | double compare_full_neighb(F2D *image,int x, int y, F2D *image1,int x1, int y1, params* data) | ||
292 | { | ||
293 | double tmp,res = 0; | ||
294 | int i,j,addr,addr1; | ||
295 | for(i=-(data->localy/2);i<=data->localy/2;i++) | ||
296 | { | ||
297 | for(j=-(data->localx/2);j<=data->localx/2;j++) | ||
298 | { | ||
299 | if( !( i > 0 && y1 > data->localy && y1+i < data->heightout) ) | ||
300 | { | ||
301 | addr = a(x+j,y+i,data->widthin); | ||
302 | addr1 = a( arrayref(xloopout,x1+j), arrayref(yloopout,y1+i), data->widthout); | ||
303 | |||
304 | tmp = asubsref(image,addr+R) - asubsref(image1,addr1+R); | ||
305 | res += tmp*tmp; | ||
306 | // tmp = asubsref(image,addr+G) - asubsref(image1,addr1+G); | ||
307 | // res += tmp*tmp; | ||
308 | // tmp = asubsref(image,addr+B) - asubsref(image1,addr1+B); | ||
309 | // res += tmp*tmp; | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | |||
314 | return res; | ||
315 | } | ||
316 | |||
317 | // Compares two L-shaped neighborhoods, returns L_2 difference | ||
318 | |||
319 | double compare_neighb(F2D *image,int x, int y, F2D *image1,int x1, int y1, params* data) | ||
320 | { | ||
321 | double tmp,res = 0; | ||
322 | int i,j,addr1,addr; | ||
323 | for(i=-(data->localy/2);i<0;i++) | ||
324 | { | ||
325 | for(j=-(data->localx/2);j<=data->localx/2;j++) | ||
326 | { | ||
327 | addr = a(x+j,y+i,data->widthin); | ||
328 | addr1 = a( arrayref(xloopout,x1+j), arrayref(yloopout,y1+i), data->widthout); | ||
329 | |||
330 | tmp = asubsref(image,addr+R) - asubsref(image1,addr1+R); | ||
331 | res += tmp*tmp; | ||
332 | // tmp = asubsref(image,addr+G) - asubsref(image1,addr1+G); | ||
333 | // res += tmp*tmp; | ||
334 | // tmp = asubsref(image,addr+B) - asubsref(image1,addr1+B); | ||
335 | // res += tmp*tmp; | ||
336 | } | ||
337 | } | ||
338 | |||
339 | for(j=-(data->localx/2);j<0;j++) | ||
340 | { | ||
341 | addr = a(x+j,y,data->widthin); | ||
342 | addr1 = a( arrayref(xloopout,x1+j), y1, data->widthout); | ||
343 | |||
344 | tmp = asubsref(image,addr+R) - asubsref(image1,addr1+R); | ||
345 | res += tmp*tmp; | ||
346 | // tmp = asubsref(image,addr+G) - asubsref(image1,addr1+G); | ||
347 | // res += tmp*tmp; | ||
348 | // tmp = asubsref(image,addr+B) - asubsref(image1,addr1+B); | ||
349 | // res += tmp*tmp; | ||
350 | } | ||
351 | |||
352 | return res; | ||
353 | } | ||
354 | |||
355 | double compare_rest(F2D *image,int x, int y, F2D *tar,int x1, int y1, params* data) | ||
356 | { | ||
357 | double tmp,res = 0; | ||
358 | int i,j,addr,addr1; | ||
359 | |||
360 | for(i=(data->localy/2);i>0;i--) | ||
361 | { | ||
362 | for(j=-(data->localx/2);j<=data->localx/2;j++) | ||
363 | { | ||
364 | addr = a(x+j,y+i,data->widthin); | ||
365 | addr1 = a( arrayref(xloopout,x1+j), arrayref(yloopout,y1+i), data->widthout); | ||
366 | |||
367 | if( asubsref(tar,addr1+R) != 1.0) //KVS? | ||
368 | { | ||
369 | tmp = asubsref(image,addr+R) - asubsref(tar,addr1+R); | ||
370 | res += tmp*tmp; | ||
371 | // tmp = asubsref(image,addr+G) - asubsref(tar,addr1+G); | ||
372 | // res += tmp*tmp; | ||
373 | // tmp = asubsref(image,addr+B) - asubsref(tar,addr1+B); | ||
374 | // res += tmp*tmp; | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | |||
379 | for(j=(data->localx/2);j>0;j--) | ||
380 | { | ||
381 | addr = a(x+j,y,data->widthin); | ||
382 | addr1 = a( arrayref(xloopout,x1+j), y1, data->widthout); | ||
383 | if( asubsref(tar,addr1+R) != 1.0) // KVS? | ||
384 | { | ||
385 | tmp = asubsref(image,addr+R) - asubsref(tar,addr1+R); | ||
386 | res += tmp*tmp; | ||
387 | // tmp = asubsref(image,addr+G) - asubsref(tar,addr1+G); | ||
388 | // res += tmp*tmp; | ||
389 | // tmp = asubsref(image,addr+B) - asubsref(tar,addr1+B); | ||
390 | // res += tmp*tmp; | ||
391 | } | ||
392 | } | ||
393 | |||
394 | return res; | ||
395 | } | ||
396 | |||