view src/filters/hq3x32.cpp @ 294:9f466a332448

merge
author Robert McIntyre <rlm@mit.edu>
date Fri, 30 Mar 2012 18:15:28 -0500
parents f9f4f1b99eed
children
line wrap: on
line source
1 #include "../Port.h"
2 #include "hq_shared32.h"
3 #include "interp.h"
5 #define SIZE_PIXEL 2 // 16bit = 2 bytes
6 #define PIXELTYPE unsigned short
7 #define Interp1 Interp1_16
8 #define Interp2 Interp2_16
9 #define Interp3 Interp3_16
10 #define Interp4 Interp4_16
11 #define Interp5 Interp5_16
13 void hq3x(unsigned char *pIn, unsigned int srcPitch,
14 unsigned char *,
15 unsigned char *pOut, unsigned int dstPitch,
16 int Xres, int Yres)
17 {
18 int i, j;
19 unsigned int line;
20 PIXELTYPE c[10];
22 // +----+----+----+
23 // | | | |
24 // | c1 | c2 | c3 |
25 // +----+----+----+
26 // | | | |
27 // | c4 | c5 | c6 |
28 // +----+----+----+
29 // | | | |
30 // | c7 | c8 | c9 |
31 // +----+----+----+
33 for (j = 0; j < Yres; j++)
34 {
35 if ((j > 0) || (j < Yres - 1))
36 line = srcPitch;
37 else
38 line = 0;
40 for (i = 0; i < Xres; i++)
41 {
42 c[2] = *((PIXELTYPE *)(pIn - line));
43 c[5] = *((PIXELTYPE *)(pIn));
44 c[8] = *((PIXELTYPE *)(pIn + line));
46 if (i > 0)
47 {
48 c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
49 c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL));
50 c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
51 }
52 else
53 {
54 c[1] = c[2];
55 c[4] = c[5];
56 c[7] = c[8];
57 }
59 if (i < Xres - 1)
60 {
61 c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
62 c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL));
63 c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
64 }
65 else
66 {
67 c[3] = c[2];
68 c[6] = c[5];
69 c[9] = c[8];
70 }
72 int pattern = 0;
74 if (interp_16_diff(c[1], c[5]))
75 pattern |= 1 << 0;
76 if (interp_16_diff(c[2], c[5]))
77 pattern |= 1 << 1;
78 if (interp_16_diff(c[3], c[5]))
79 pattern |= 1 << 2;
80 if (interp_16_diff(c[4], c[5]))
81 pattern |= 1 << 3;
82 if (interp_16_diff(c[6], c[5]))
83 pattern |= 1 << 4;
84 if (interp_16_diff(c[7], c[5]))
85 pattern |= 1 << 5;
86 if (interp_16_diff(c[8], c[5]))
87 pattern |= 1 << 6;
88 if (interp_16_diff(c[9], c[5]))
89 pattern |= 1 << 7;
91 #define Diff interp_16_diff
92 #include "hq3x32.h"
93 #undef Diff
94 pIn += SIZE_PIXEL;
95 pOut += 3 << 1;
96 }
97 pIn += srcPitch - (Xres << 1);
98 pOut += dstPitch - (3 * Xres << 1);
99 pOut += dstPitch << 1;
100 // pIn+=SIZE_PIXEL;
101 // pOut+=3*SIZE_PIXEL;
102 //}
103 //pIn+=srcPitch-(4*Xres);
104 //pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
105 //pOut+=2*dstPitch;
106 }
107 }
109 void hq3xS(unsigned char *pIn, unsigned int srcPitch,
110 unsigned char *,
111 unsigned char *pOut, unsigned int dstPitch,
112 int Xres, int Yres)
113 {
114 int i, j;
115 PIXELTYPE c[10];
117 // +----+----+----+
118 // | | | |
119 // | c1 | c2 | c3 |
120 // +----+----+----+
121 // | | | |
122 // | c4 | c5 | c6 |
123 // +----+----+----+
124 // | | | |
125 // | c7 | c8 | c9 |
126 // +----+----+----+
128 for (j = 0; j < Yres; j++)
129 {
130 for (i = 0; i < Xres; i++)
131 {
132 c[2] = *((PIXELTYPE *)(pIn - srcPitch));
133 c[5] = *((PIXELTYPE *)(pIn));
134 c[8] = *((PIXELTYPE *)(pIn + srcPitch));
136 c[1] = *((PIXELTYPE *)(pIn - srcPitch - SIZE_PIXEL));
137 c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL));
138 c[7] = *((PIXELTYPE *)(pIn + srcPitch - SIZE_PIXEL));
140 c[3] = *((PIXELTYPE *)(pIn - srcPitch + SIZE_PIXEL));
141 c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL));
142 c[9] = *((PIXELTYPE *)(pIn + srcPitch + SIZE_PIXEL));
144 int pattern = 0;
146 // hq3xS dynamic edge detection:
147 // simply comparing the center color against its surroundings will give bad results in many cases,
148 // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block
149 int brightArray[10];
150 int maxBright = 0, minBright = 999999;
151 for (int j = 1; j < 10; j++)
152 {
153 int r, g, b;
154 if (interp_bits_per_pixel == 16)
155 {
156 b = (int)((c[j] & 0x1F)) << 3;
157 g = (int)((c[j] & 0x7E0)) >> 3;
158 r = (int)((c[j] & 0xF800)) >> 8;
159 }
160 else
161 {
162 b = (int)((c[j] & 0x1F)) << 3;
163 g = (int)((c[j] & 0x3E0)) >> 2;
164 r = (int)((c[j] & 0x7C00)) >> 7;
165 }
166 const int bright = r + r + r + g + g + g + b + b;
167 if (bright > maxBright) maxBright = bright;
168 if (bright < minBright) minBright = bright;
170 brightArray[j] = bright;
171 }
172 const int diffBright = ((maxBright - minBright) * 7) >> 4;
173 if (diffBright > 7)
174 {
175 #define ABS(x) ((x) < 0 ? -(x) : (x))
177 const int centerBright = brightArray[5];
178 if (ABS(brightArray[1] - centerBright) > diffBright)
179 pattern |= 1 << 0;
180 if (ABS(brightArray[2] - centerBright) > diffBright)
181 pattern |= 1 << 1;
182 if (ABS(brightArray[3] - centerBright) > diffBright)
183 pattern |= 1 << 2;
184 if (ABS(brightArray[4] - centerBright) > diffBright)
185 pattern |= 1 << 3;
186 if (ABS(brightArray[6] - centerBright) > diffBright)
187 pattern |= 1 << 4;
188 if (ABS(brightArray[7] - centerBright) > diffBright)
189 pattern |= 1 << 5;
190 if (ABS(brightArray[8] - centerBright) > diffBright)
191 pattern |= 1 << 6;
192 if (ABS(brightArray[9] - centerBright) > diffBright)
193 pattern |= 1 << 7;
194 }
196 #define Diff(x, y) false //(ABS((x) - (y)) > diffBright)
197 #undef cget
198 #define cget(x) brightArray[x]
199 #include "hq3x32.h"
200 #undef cget
201 #undef Diff
202 pIn += SIZE_PIXEL;
203 pOut += 3 << 1;
204 }
205 pIn += srcPitch - (Xres << 1);
206 pOut += dstPitch - (3 * Xres << 1);
207 pOut += dstPitch << 1;
208 // pIn+=SIZE_PIXEL;
209 // pOut+=3*SIZE_PIXEL;
210 //}
211 //pIn+=srcPitch-(4*Xres);
212 //pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
213 //pOut+=2*dstPitch;
214 }
215 }
217 #undef Interp1
218 #undef Interp2
219 #undef Interp3
220 #undef Interp4
221 #undef Interp5
222 #undef SIZE_PIXEL
223 #undef PIXELTYPE
224 #define SIZE_PIXEL 4 // 32bit = 4 bytes
225 #define PIXELTYPE unsigned int
227 void hq3x32(unsigned char *pIn, unsigned int srcPitch,
228 unsigned char *,
229 unsigned char *pOut, unsigned int dstPitch,
230 int Xres, int Yres)
231 {
232 unsigned int YUV1, YUV2;
233 int i, j, k;
234 unsigned int line;
235 PIXELTYPE c[10];
237 // +----+----+----+
238 // | | | |
239 // | c1 | c2 | c3 |
240 // +----+----+----+
241 // | | | |
242 // | c4 | c5 | c6 |
243 // +----+----+----+
244 // | | | |
245 // | c7 | c8 | c9 |
246 // +----+----+----+
248 for (j = 0; j < Yres; j++)
249 {
250 if ((j > 0) && (j < Yres - 1))
251 line = srcPitch;
252 else
253 line = 0;
255 for (i = 0; i < Xres; i++)
256 {
257 c[2] = *((PIXELTYPE *)(pIn - line));
258 c[5] = *((PIXELTYPE *)(pIn));
259 c[8] = *((PIXELTYPE *)(pIn + line));
261 if (i > 0)
262 {
263 c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
264 c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL));
265 c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
266 }
267 else
268 {
269 c[1] = c[2];
270 c[4] = c[5];
271 c[7] = c[8];
272 }
274 if (i < Xres - 1)
275 {
276 c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
277 c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL));
278 c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
279 }
280 else
281 {
282 c[3] = c[2];
283 c[6] = c[5];
284 c[9] = c[8];
285 }
287 int pattern = 0;
288 int flag = 1;
290 YUV1 = RGBtoYUV(c[5]);
292 for (k = 1; k <= 9; k++)
293 {
294 if (k == 5) continue;
296 if (c[k] != c[5])
297 {
298 YUV2 = RGBtoYUV(c[k]);
299 if (
300 (abs32((YUV1 & Ymask) - (YUV2 & Ymask)) > trY) ||
301 (abs32((YUV1 & Umask) - (YUV2 & Umask)) > trU) ||
302 (abs32((YUV1 & Vmask) - (YUV2 & Vmask)) > trV)
303 )
304 pattern |= flag;
305 }
306 flag <<= 1;
307 }
309 #include "hq3x32.h"
310 pIn += SIZE_PIXEL;
311 pOut += 3 << 2;
312 }
313 pIn += srcPitch - (Xres << 2);
314 pOut += dstPitch - (3 * Xres << 2);
315 pOut += dstPitch << 1;
316 // pIn+=SIZE_PIXEL;
317 // pOut+=3*SIZE_PIXEL;
318 //}
319 //pIn+=srcPitch-(4*Xres);
320 //pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
321 //pOut+=2*dstPitch;
322 }
323 }
325 void hq3xS32(unsigned char *pIn, unsigned int srcPitch,
326 unsigned char *,
327 unsigned char *pOut, unsigned int dstPitch,
328 int Xres, int Yres)
329 {
330 int i, j;
331 unsigned int line;
332 PIXELTYPE c[10];
334 // +----+----+----+
335 // | | | |
336 // | c1 | c2 | c3 |
337 // +----+----+----+
338 // | | | |
339 // | c4 | c5 | c6 |
340 // +----+----+----+
341 // | | | |
342 // | c7 | c8 | c9 |
343 // +----+----+----+
345 for (j = 0; j < Yres; j++)
346 {
347 if ((j > 0) && (j < Yres - 1))
348 line = srcPitch;
349 else
350 line = 0;
352 for (i = 0; i < Xres; i++)
353 {
354 c[2] = *((PIXELTYPE *)(pIn - line));
355 c[5] = *((PIXELTYPE *)(pIn));
356 c[8] = *((PIXELTYPE *)(pIn + line));
358 if (i > 0)
359 {
360 c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
361 c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL));
362 c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
363 }
364 else
365 {
366 c[1] = c[2];
367 c[4] = c[5];
368 c[7] = c[8];
369 }
371 if (i < Xres - 1)
372 {
373 c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
374 c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL));
375 c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
376 }
377 else
378 {
379 c[3] = c[2];
380 c[6] = c[5];
381 c[9] = c[8];
382 }
384 int pattern = 0;
386 // hq3xS dynamic edge detection:
387 // simply comparing the center color against its surroundings will give bad results in many cases,
388 // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block
389 int brightArray[10];
390 int maxBright = 0, minBright = 999999;
391 for (int j = 1; j < 10; j++)
392 {
393 const int b = (int)((c[j] & 0xF8));
394 const int g = (int)((c[j] & 0xF800)) >> 8;
395 const int r = (int)((c[j] & 0xF80000)) >> 16;
396 const int bright = r + r + r + g + g + g + b + b;
397 if (bright > maxBright) maxBright = bright;
398 if (bright < minBright) minBright = bright;
400 brightArray[j] = bright;
401 }
402 int diffBright = ((maxBright - minBright) * 7) >> 4;
403 if (diffBright > 7)
404 {
405 #define ABS(x) ((x) < 0 ? -(x) : (x))
407 const int centerBright = brightArray[5];
408 if (ABS(brightArray[1] - centerBright) > diffBright)
409 pattern |= 1 << 0;
410 if (ABS(brightArray[2] - centerBright) > diffBright)
411 pattern |= 1 << 1;
412 if (ABS(brightArray[3] - centerBright) > diffBright)
413 pattern |= 1 << 2;
414 if (ABS(brightArray[4] - centerBright) > diffBright)
415 pattern |= 1 << 3;
416 if (ABS(brightArray[6] - centerBright) > diffBright)
417 pattern |= 1 << 4;
418 if (ABS(brightArray[7] - centerBright) > diffBright)
419 pattern |= 1 << 5;
420 if (ABS(brightArray[8] - centerBright) > diffBright)
421 pattern |= 1 << 6;
422 if (ABS(brightArray[9] - centerBright) > diffBright)
423 pattern |= 1 << 7;
424 }
426 #define Diff(x, y) false //(ABS((x) - (y)) > diffBright)
427 #undef cget
428 #define cget(x) brightArray[x]
429 #include "hq3x32.h"
430 #undef cget
431 #undef Diff
432 pIn += SIZE_PIXEL;
433 pOut += 3 << 2;
434 }
435 pIn += srcPitch - (Xres << 2);
436 pOut += dstPitch - (3 * Xres << 2);
437 pOut += dstPitch << 1;
438 // pIn+=SIZE_PIXEL;
439 // pOut+=3*SIZE_PIXEL;
440 //}
441 //pIn+=srcPitch-(4*Xres);
442 //pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
443 //pOut+=2*dstPitch;
444 }