Rice GLES2 (from mupen64plus-ae) plugin. Compile but doesn't works well on the OpenPa...
[mupen64plus-pandora.git] / source / gles2rice / src / TextureFilters_hq4x.cpp
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - TextureFilters_hq4x.cpp                                 *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C)  2003 MaxSt ( maxst@hiend3d.com )                       *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22 #include <stdlib.h>
23
24 #include "typedefs.h"
25
26 static int   RGBtoYUV[4096];
27 //#define RGB32toYUV(val) (RGBtoYUV[((val&0x00FF0000)>>20)+((val&0x0000FF00)>>12)+((val&0x000000FF)>>4)])
28 inline int RGB32toYUV(uint32 val)
29 {
30     int a,r,g,b,Y,u,v;
31     //r = (val&0x00FF0000)>>16;
32     //g = (val&0x0000FF00)>>8;
33     //b = (val&0x000000FF);
34     a = (val&0xFF000000);
35     r = (val&0x00FF0000)>>16;
36     g = (val&0x0000FF00)>>8;
37     b = (val&0x000000FF);
38     //r = (val&0x00F80000)>>16;
39     //g = (val&0x0000FC00)>>8;
40     //b = (val&0x000000F8);
41     Y = (r + g + b) >> 2;
42     u = 128 + ((r - b) >> 2);
43     v = 128 + ((-r + 2*g -b)>>3);
44     return a + (Y<<16) + (u<<8) + v;
45 }
46 #define RGB16toYUV(val) (RGBtoYUV[(val&0x0FFF)])
47 static int   YUV1, YUV2;
48 const  int   Amask = 0xFF000000;
49 const  int   Ymask = 0x00FF0000;
50 const  int   Umask = 0x0000FF00;
51 const  int   Vmask = 0x000000FF;
52 const  int   trA   = 0x20000000;
53 const  int   trY   = 0x00300000;
54 const  int   trU   = 0x00000700;
55 const  int   trV   = 0x00000006;
56 //const  int   trU   = 0x00001800;
57 //const  int   trV   = 0x00000018;
58
59 #define INTERP_16_MASK_1_3(v) ((v)&0x0F0F)
60 #define INTERP_16_MASK_SHIFT_2_4(v) (((v)&0xF0F0)>>4)
61 #define INTERP_16_MASK_SHIFTBACK_2_4(v) ((INTERP_16_MASK_1_3(v))<<4)
62
63 inline void hq4x_Interp1_16(unsigned char * pc, uint16 p1, uint16 p2)
64 {
65     *((uint16*)pc) = INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*3 + INTERP_16_MASK_1_3(p2)) / 4)
66         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*3 + INTERP_16_MASK_SHIFT_2_4(p2)) / 4 );
67 }
68
69 inline void hq4x_Interp2_16(unsigned char * pc, uint16 p1, uint16 p2, uint16 p3)
70 {
71     *((uint16*)pc) =  INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*2 + INTERP_16_MASK_1_3(p2) + INTERP_16_MASK_1_3(p3)) / 4)
72         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*2 + INTERP_16_MASK_SHIFT_2_4(p2) + INTERP_16_MASK_SHIFT_2_4(p3)) / 4);
73 }
74
75 inline void hq4x_Interp3_16(unsigned char * pc, uint16 p1, uint16 p2)
76 {
77     *((uint16*)pc) =  INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*7 + INTERP_16_MASK_1_3(p2)) / 8)
78         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*7 + INTERP_16_MASK_SHIFT_2_4(p2)) / 8);
79 }
80
81 inline void hq4x_Interp5_16(unsigned char * pc, uint16 p1, uint16 p2)
82 {
83     *((uint16*)pc) =  INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1) + INTERP_16_MASK_1_3(p2)) / 2)
84         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1) + INTERP_16_MASK_SHIFT_2_4(p2)) / 2);
85 }
86
87 inline void hq4x_Interp6_16(unsigned char * pc, uint16 p1, uint16 p2, uint16 p3)
88 {
89     *((uint16*)pc) =  INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*5 + INTERP_16_MASK_1_3(p2)*2 + INTERP_16_MASK_1_3(p3)*1) / 8)
90         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*5 + INTERP_16_MASK_SHIFT_2_4(p2)*2 + INTERP_16_MASK_SHIFT_2_4(p3)*1) / 8);
91 }
92
93 inline void hq4x_Interp7_16(unsigned char * pc, uint16 p1, uint16 p2, uint16 p3)
94 {
95     *((uint16*)pc) =   INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*6 + INTERP_16_MASK_1_3(p2) + INTERP_16_MASK_1_3(p3)) / 8)
96         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*6 + INTERP_16_MASK_SHIFT_2_4(p2) + INTERP_16_MASK_SHIFT_2_4(p3)) / 8);
97 }
98
99 inline void hq4x_Interp8_16(unsigned char * pc, uint16 p1, uint16 p2)
100 {
101     //*((int*)pc) = (c1*5+c2*3)/8;
102     *((uint16*)pc) =   INTERP_16_MASK_1_3((INTERP_16_MASK_1_3(p1)*5 + INTERP_16_MASK_1_3(p2)*3) / 8)
103         | INTERP_16_MASK_SHIFTBACK_2_4((INTERP_16_MASK_SHIFT_2_4(p1)*5 + INTERP_16_MASK_SHIFT_2_4(p2)*3) / 8);
104 }
105
106 #define INTERP_32_MASK_1_3(v) ((v)&0x00FF00FF)
107 #define INTERP_32_MASK_SHIFT_2_4(v) (((v)&0xFF00FF00)>>8)
108 #define INTERP_32_MASK_SHIFTBACK_2_4(v) (((INTERP_32_MASK_1_3(v))<<8))
109
110 inline void hq4x_Interp1_32(unsigned char * pc, uint32 p1, uint32 p2)
111 {
112     *((uint32*)pc) = INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*3 + INTERP_32_MASK_1_3(p2)) / 4)
113         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*3 + INTERP_32_MASK_SHIFT_2_4(p2)) / 4 );
114 }
115
116 inline void hq4x_Interp2_32(unsigned char * pc, uint32 p1, uint32 p2, uint32 p3)
117 {
118     *((uint32*)pc) =  INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*2 + INTERP_32_MASK_1_3(p2) + INTERP_32_MASK_1_3(p3)) / 4)
119         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*2 + INTERP_32_MASK_SHIFT_2_4(p2) + INTERP_32_MASK_SHIFT_2_4(p3)) / 4);
120 }
121
122 inline void hq4x_Interp3_32(unsigned char * pc, uint32 p1, uint32 p2)
123 {
124     *((uint32*)pc) =  INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*7 + INTERP_32_MASK_1_3(p2)) / 8)
125         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*7 + INTERP_32_MASK_SHIFT_2_4(p2)) / 8);
126 }
127
128 inline void hq4x_Interp5_32(unsigned char * pc, uint32 p1, uint32 p2)
129 {
130     *((uint32*)pc) =  INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1) + INTERP_32_MASK_1_3(p2)) / 2)
131         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1) + INTERP_32_MASK_SHIFT_2_4(p2)) / 2);
132 }
133
134 inline void hq4x_Interp6_32(unsigned char * pc, uint32 p1, uint32 p2, uint32 p3)
135 {
136     *((uint32*)pc) =  INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*5 + INTERP_32_MASK_1_3(p2)*2 + INTERP_32_MASK_1_3(p3)*1) / 8)
137         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*5 + INTERP_32_MASK_SHIFT_2_4(p2)*2 + INTERP_32_MASK_SHIFT_2_4(p3)*1) / 8);
138 }
139
140 inline void hq4x_Interp7_32(unsigned char * pc, uint32 p1, uint32 p2, uint32 p3)
141 {
142     *((uint32*)pc) =   INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*6 + INTERP_32_MASK_1_3(p2) + INTERP_32_MASK_1_3(p3)) / 8)
143         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*6 + INTERP_32_MASK_SHIFT_2_4(p2) + INTERP_32_MASK_SHIFT_2_4(p3)) / 8);
144 }
145
146 inline void hq4x_Interp8_32(unsigned char * pc, uint32 p1, uint32 p2)
147 {
148     //*((int*)pc) = (c1*5+c2*3)/8;
149     *((uint32*)pc) =   INTERP_32_MASK_1_3((INTERP_32_MASK_1_3(p1)*5 + INTERP_32_MASK_1_3(p2)*3) / 8)
150         | INTERP_32_MASK_SHIFTBACK_2_4((INTERP_32_MASK_SHIFT_2_4(p1)*5 + INTERP_32_MASK_SHIFT_2_4(p2)*3) / 8);
151 }
152
153 #define PIXEL00_0     *((int*)(pOut)) = c[5];
154 #define PIXEL00_11    hq4x_Interp1(pOut, c[5], c[4]);
155 #define PIXEL00_12    hq4x_Interp1(pOut, c[5], c[2]);
156 #define PIXEL00_20    hq4x_Interp2(pOut, c[5], c[2], c[4]);
157 #define PIXEL00_50    hq4x_Interp5(pOut, c[2], c[4]);
158 #define PIXEL00_80    hq4x_Interp8(pOut, c[5], c[1]);
159 #define PIXEL00_81    hq4x_Interp8(pOut, c[5], c[4]);
160 #define PIXEL00_82    hq4x_Interp8(pOut, c[5], c[2]);
161 #define PIXEL01_0     *((int*)(pOut+BPP)) = c[5];
162 #define PIXEL01_10    hq4x_Interp1(pOut+BPP, c[5], c[1]);
163 #define PIXEL01_12    hq4x_Interp1(pOut+BPP, c[5], c[2]);
164 #define PIXEL01_14    hq4x_Interp1(pOut+BPP, c[2], c[5]);
165 #define PIXEL01_21    hq4x_Interp2(pOut+BPP, c[2], c[5], c[4]);
166 #define PIXEL01_31    hq4x_Interp3(pOut+BPP, c[5], c[4]);
167 #define PIXEL01_50    hq4x_Interp5(pOut+BPP, c[2], c[5]);
168 #define PIXEL01_60    hq4x_Interp6(pOut+BPP, c[5], c[2], c[4]);
169 #define PIXEL01_61    hq4x_Interp6(pOut+BPP, c[5], c[2], c[1]);
170 #define PIXEL01_82    hq4x_Interp8(pOut+BPP, c[5], c[2]);
171 #define PIXEL01_83    hq4x_Interp8(pOut+BPP, c[2], c[4]);
172 #define PIXEL02_0     *((int*)(pOut+BPP2)) = c[5];
173 #define PIXEL02_10    hq4x_Interp1(pOut+BPP2, c[5], c[3]);
174 #define PIXEL02_11    hq4x_Interp1(pOut+BPP2, c[5], c[2]);
175 #define PIXEL02_13    hq4x_Interp1(pOut+BPP2, c[2], c[5]);
176 #define PIXEL02_21    hq4x_Interp2(pOut+BPP2, c[2], c[5], c[6]);
177 #define PIXEL02_32    hq4x_Interp3(pOut+BPP2, c[5], c[6]);
178 #define PIXEL02_50    hq4x_Interp5(pOut+BPP2, c[2], c[5]);
179 #define PIXEL02_60    hq4x_Interp6(pOut+BPP2, c[5], c[2], c[6]);
180 #define PIXEL02_61    hq4x_Interp6(pOut+BPP2, c[5], c[2], c[3]);
181 #define PIXEL02_81    hq4x_Interp8(pOut+BPP2, c[5], c[2]);
182 #define PIXEL02_83    hq4x_Interp8(pOut+BPP2, c[2], c[6]);
183 #define PIXEL03_0     *((int*)(pOut+BPP3)) = c[5];
184 #define PIXEL03_11    hq4x_Interp1(pOut+BPP3, c[5], c[2]);
185 #define PIXEL03_12    hq4x_Interp1(pOut+BPP3, c[5], c[6]);
186 #define PIXEL03_20    hq4x_Interp2(pOut+BPP3, c[5], c[2], c[6]);
187 #define PIXEL03_50    hq4x_Interp5(pOut+BPP3, c[2], c[6]);
188 #define PIXEL03_80    hq4x_Interp8(pOut+BPP3, c[5], c[3]);
189 #define PIXEL03_81    hq4x_Interp8(pOut+BPP3, c[5], c[2]);
190 #define PIXEL03_82    hq4x_Interp8(pOut+BPP3, c[5], c[6]);
191 #define PIXEL10_0     *((int*)(pOut+BpL)) = c[5];
192 #define PIXEL10_10    hq4x_Interp1(pOut+BpL, c[5], c[1]);
193 #define PIXEL10_11    hq4x_Interp1(pOut+BpL, c[5], c[4]);
194 #define PIXEL10_13    hq4x_Interp1(pOut+BpL, c[4], c[5]);
195 #define PIXEL10_21    hq4x_Interp2(pOut+BpL, c[4], c[5], c[2]);
196 #define PIXEL10_32    hq4x_Interp3(pOut+BpL, c[5], c[2]);
197 #define PIXEL10_50    hq4x_Interp5(pOut+BpL, c[4], c[5]);
198 #define PIXEL10_60    hq4x_Interp6(pOut+BpL, c[5], c[4], c[2]);
199 #define PIXEL10_61    hq4x_Interp6(pOut+BpL, c[5], c[4], c[1]);
200 #define PIXEL10_81    hq4x_Interp8(pOut+BpL, c[5], c[4]);
201 #define PIXEL10_83    hq4x_Interp8(pOut+BpL, c[4], c[2]);
202 #define PIXEL11_0     *((int*)(pOut+BpL+BPP)) = c[5];
203 #define PIXEL11_30    hq4x_Interp3(pOut+BpL+BPP, c[5], c[1]);
204 #define PIXEL11_31    hq4x_Interp3(pOut+BpL+BPP, c[5], c[4]);
205 #define PIXEL11_32    hq4x_Interp3(pOut+BpL+BPP, c[5], c[2]);
206 #define PIXEL11_70    hq4x_Interp7(pOut+BpL+BPP, c[5], c[4], c[2]);
207 #define PIXEL12_0     *((int*)(pOut+BpL+BPP2)) = c[5];
208 #define PIXEL12_30    hq4x_Interp3(pOut+BpL+BPP2, c[5], c[3]);
209 #define PIXEL12_31    hq4x_Interp3(pOut+BpL+BPP2, c[5], c[2]);
210 #define PIXEL12_32    hq4x_Interp3(pOut+BpL+BPP2, c[5], c[6]);
211 #define PIXEL12_70    hq4x_Interp7(pOut+BpL+BPP2, c[5], c[6], c[2]);
212 #define PIXEL13_0     *((int*)(pOut+BpL+BPP3)) = c[5];
213 #define PIXEL13_10    hq4x_Interp1(pOut+BpL+BPP3, c[5], c[3]);
214 #define PIXEL13_12    hq4x_Interp1(pOut+BpL+BPP3, c[5], c[6]);
215 #define PIXEL13_14    hq4x_Interp1(pOut+BpL+BPP3, c[6], c[5]);
216 #define PIXEL13_21    hq4x_Interp2(pOut+BpL+BPP3, c[6], c[5], c[2]);
217 #define PIXEL13_31    hq4x_Interp3(pOut+BpL+BPP3, c[5], c[2]);
218 #define PIXEL13_50    hq4x_Interp5(pOut+BpL+BPP3, c[6], c[5]);
219 #define PIXEL13_60    hq4x_Interp6(pOut+BpL+BPP3, c[5], c[6], c[2]);
220 #define PIXEL13_61    hq4x_Interp6(pOut+BpL+BPP3, c[5], c[6], c[3]);
221 #define PIXEL13_82    hq4x_Interp8(pOut+BpL+BPP3, c[5], c[6]);
222 #define PIXEL13_83    hq4x_Interp8(pOut+BpL+BPP3, c[6], c[2]);
223 #define PIXEL20_0     *((int*)(pOut+BpL+BpL)) = c[5];
224 #define PIXEL20_10    hq4x_Interp1(pOut+BpL+BpL, c[5], c[7]);
225 #define PIXEL20_12    hq4x_Interp1(pOut+BpL+BpL, c[5], c[4]);
226 #define PIXEL20_14    hq4x_Interp1(pOut+BpL+BpL, c[4], c[5]);
227 #define PIXEL20_21    hq4x_Interp2(pOut+BpL+BpL, c[4], c[5], c[8]);
228 #define PIXEL20_31    hq4x_Interp3(pOut+BpL+BpL, c[5], c[8]);
229 #define PIXEL20_50    hq4x_Interp5(pOut+BpL+BpL, c[4], c[5]);
230 #define PIXEL20_60    hq4x_Interp6(pOut+BpL+BpL, c[5], c[4], c[8]);
231 #define PIXEL20_61    hq4x_Interp6(pOut+BpL+BpL, c[5], c[4], c[7]);
232 #define PIXEL20_82    hq4x_Interp8(pOut+BpL+BpL, c[5], c[4]);
233 #define PIXEL20_83    hq4x_Interp8(pOut+BpL+BpL, c[4], c[8]);
234 #define PIXEL21_0     *((int*)(pOut+BpL+BpL+BPP)) = c[5];
235 #define PIXEL21_30    hq4x_Interp3(pOut+BpL+BpL+BPP, c[5], c[7]);
236 #define PIXEL21_31    hq4x_Interp3(pOut+BpL+BpL+BPP, c[5], c[8]);
237 #define PIXEL21_32    hq4x_Interp3(pOut+BpL+BpL+BPP, c[5], c[4]);
238 #define PIXEL21_70    hq4x_Interp7(pOut+BpL+BpL+BPP, c[5], c[4], c[8]);
239 #define PIXEL22_0     *((int*)(pOut+BpL+BpL+BPP2)) = c[5];
240 #define PIXEL22_30    hq4x_Interp3(pOut+BpL+BpL+BPP2, c[5], c[9]);
241 #define PIXEL22_31    hq4x_Interp3(pOut+BpL+BpL+BPP2, c[5], c[6]);
242 #define PIXEL22_32    hq4x_Interp3(pOut+BpL+BpL+BPP2, c[5], c[8]);
243 #define PIXEL22_70    hq4x_Interp7(pOut+BpL+BpL+BPP2, c[5], c[6], c[8]);
244 #define PIXEL23_0     *((int*)(pOut+BpL+BpL+BPP3)) = c[5];
245 #define PIXEL23_10    hq4x_Interp1(pOut+BpL+BpL+BPP3, c[5], c[9]);
246 #define PIXEL23_11    hq4x_Interp1(pOut+BpL+BpL+BPP3, c[5], c[6]);
247 #define PIXEL23_13    hq4x_Interp1(pOut+BpL+BpL+BPP3, c[6], c[5]);
248 #define PIXEL23_21    hq4x_Interp2(pOut+BpL+BpL+BPP3, c[6], c[5], c[8]);
249 #define PIXEL23_32    hq4x_Interp3(pOut+BpL+BpL+BPP3, c[5], c[8]);
250 #define PIXEL23_50    hq4x_Interp5(pOut+BpL+BpL+BPP3, c[6], c[5]);
251 #define PIXEL23_60    hq4x_Interp6(pOut+BpL+BpL+BPP3, c[5], c[6], c[8]);
252 #define PIXEL23_61    hq4x_Interp6(pOut+BpL+BpL+BPP3, c[5], c[6], c[9]);
253 #define PIXEL23_81    hq4x_Interp8(pOut+BpL+BpL+BPP3, c[5], c[6]);
254 #define PIXEL23_83    hq4x_Interp8(pOut+BpL+BpL+BPP3, c[6], c[8]);
255 #define PIXEL30_0     *((int*)(pOut+BpL+BpL+BpL)) = c[5];
256 #define PIXEL30_11    hq4x_Interp1(pOut+BpL+BpL+BpL, c[5], c[8]);
257 #define PIXEL30_12    hq4x_Interp1(pOut+BpL+BpL+BpL, c[5], c[4]);
258 #define PIXEL30_20    hq4x_Interp2(pOut+BpL+BpL+BpL, c[5], c[8], c[4]);
259 #define PIXEL30_50    hq4x_Interp5(pOut+BpL+BpL+BpL, c[8], c[4]);
260 #define PIXEL30_80    hq4x_Interp8(pOut+BpL+BpL+BpL, c[5], c[7]);
261 #define PIXEL30_81    hq4x_Interp8(pOut+BpL+BpL+BpL, c[5], c[8]);
262 #define PIXEL30_82    hq4x_Interp8(pOut+BpL+BpL+BpL, c[5], c[4]);
263 #define PIXEL31_0     *((int*)(pOut+BpL+BpL+BpL+BPP)) = c[5];
264 #define PIXEL31_10    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP, c[5], c[7]);
265 #define PIXEL31_11    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP, c[5], c[8]);
266 #define PIXEL31_13    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP, c[8], c[5]);
267 #define PIXEL31_21    hq4x_Interp2(pOut+BpL+BpL+BpL+BPP, c[8], c[5], c[4]);
268 #define PIXEL31_32    hq4x_Interp3(pOut+BpL+BpL+BpL+BPP, c[5], c[4]);
269 #define PIXEL31_50    hq4x_Interp5(pOut+BpL+BpL+BpL+BPP, c[8], c[5]);
270 #define PIXEL31_60    hq4x_Interp6(pOut+BpL+BpL+BpL+BPP, c[5], c[8], c[4]);
271 #define PIXEL31_61    hq4x_Interp6(pOut+BpL+BpL+BpL+BPP, c[5], c[8], c[7]);
272 #define PIXEL31_81    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP, c[5], c[8]);
273 #define PIXEL31_83    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP, c[8], c[4]);
274 #define PIXEL32_0     *((int*)(pOut+BpL+BpL+BpL+BPP2)) = c[5];
275 #define PIXEL32_10    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP2, c[5], c[9]);
276 #define PIXEL32_12    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP2, c[5], c[8]);
277 #define PIXEL32_14    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP2, c[8], c[5]);
278 #define PIXEL32_21    hq4x_Interp2(pOut+BpL+BpL+BpL+BPP2, c[8], c[5], c[6]);
279 #define PIXEL32_31    hq4x_Interp3(pOut+BpL+BpL+BpL+BPP2, c[5], c[6]);
280 #define PIXEL32_50    hq4x_Interp5(pOut+BpL+BpL+BpL+BPP2, c[8], c[5]);
281 #define PIXEL32_60    hq4x_Interp6(pOut+BpL+BpL+BpL+BPP2, c[5], c[8], c[6]);
282 #define PIXEL32_61    hq4x_Interp6(pOut+BpL+BpL+BpL+BPP2, c[5], c[8], c[9]);
283 #define PIXEL32_82    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP2, c[5], c[8]);
284 #define PIXEL32_83    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP2, c[8], c[6]);
285 #define PIXEL33_0     *((int*)(pOut+BpL+BpL+BpL+BPP3)) = c[5];
286 #define PIXEL33_11    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP3, c[5], c[6]);
287 #define PIXEL33_12    hq4x_Interp1(pOut+BpL+BpL+BpL+BPP3, c[5], c[8]);
288 #define PIXEL33_20    hq4x_Interp2(pOut+BpL+BpL+BpL+BPP3, c[5], c[8], c[6]);
289 #define PIXEL33_50    hq4x_Interp5(pOut+BpL+BpL+BpL+BPP3, c[8], c[6]);
290 #define PIXEL33_80    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP3, c[5], c[9]);
291 #define PIXEL33_81    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP3, c[5], c[6]);
292 #define PIXEL33_82    hq4x_Interp8(pOut+BpL+BpL+BpL+BPP3, c[5], c[8]);
293
294
295
296 inline bool Diff_16(uint16 w1, uint16 w2)
297 {
298     YUV1 = RGB16toYUV(w1);
299     YUV2 = RGB16toYUV(w2);
300     return ( ( abs((YUV1 & Amask) - (YUV2 & Amask)) > trA ) ||
301         ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
302         ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
303         ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) );
304 }
305 inline bool Diff_32(uint32 w1, uint32 w2)
306 {
307     YUV1 = RGB32toYUV(w1);
308     YUV2 = RGB32toYUV(w2);
309     return ( ( abs((YUV1 & Amask) - (YUV2 & Amask)) > trA ) ||
310         ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
311         ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
312         ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) );
313 }
314
315 void hq4x_16( unsigned char * pIn, unsigned char * pOut, int Xres, int Yres, int SrcPPL, int BpL )
316 {
317 #define hq4x_Interp1 hq4x_Interp1_16
318 #define hq4x_Interp2 hq4x_Interp2_16
319 #define hq4x_Interp3 hq4x_Interp3_16
320 #define hq4x_Interp4 hq4x_Interp4_16
321 #define hq4x_Interp5 hq4x_Interp5_16
322 #define hq4x_Interp6 hq4x_Interp6_16
323 #define hq4x_Interp7 hq4x_Interp7_16
324 #define hq4x_Interp8 hq4x_Interp8_16
325 #define Diff Diff_16
326 #define BPP   2
327 #define BPP2  4
328 #define BPP3  6
329
330     int  i, j, k;
331     int  prevline, nextline;
332     uint16  w[10];
333     uint16  c[10];
334
335     //   +----+----+----+
336     //   |    |    |    |
337     //   | w1 | w2 | w3 |
338     //   +----+----+----+
339     //   |    |    |    |
340     //   | w4 | w5 | w6 |
341     //   +----+----+----+
342     //   |    |    |    |
343     //   | w7 | w8 | w9 |
344     //   +----+----+----+
345
346     for (j=0; j<Yres; j++)
347     {
348         if (j>0)      prevline = -SrcPPL*2; else prevline = 0;
349         if (j<Yres-1) nextline =  SrcPPL*2; else nextline = 0;
350
351         for (i=0; i<Xres; i++)
352         {
353             w[2] = *((uint16*)(pIn + prevline));
354             w[5] = *((uint16*)pIn);
355             w[8] = *((uint16*)(pIn + nextline));
356
357             if (i>0)
358             {
359                 w[1] = *((uint16*)(pIn + prevline - 2));
360                 w[4] = *((uint16*)(pIn - 2));
361                 w[7] = *((uint16*)(pIn + nextline - 2));
362             }
363             else
364             {
365                 w[1] = w[2];
366                 w[4] = w[5];
367                 w[7] = w[8];
368             }
369
370             if (i<Xres-1)
371             {
372                 w[3] = *((uint16*)(pIn + prevline + 2));
373                 w[6] = *((uint16*)(pIn + 2));
374                 w[9] = *((uint16*)(pIn + nextline + 2));
375             }
376             else
377             {
378                 w[3] = w[2];
379                 w[6] = w[5];
380                 w[9] = w[8];
381             }
382
383             int pattern = 0;
384             int flag = 1;
385
386             YUV1 = RGB16toYUV(w[5]);
387
388             for (k=1; k<=9; k++)
389             {
390                 if (k==5) continue;
391
392                 if ( w[k] != w[5] )
393                 {
394                     YUV2 = RGB16toYUV(w[k]);
395                     if ( ( abs((YUV1 & Amask) - (YUV2 & Amask)) > trA ) ||
396                         ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
397                         ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
398                         ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) )
399                         pattern |= flag;
400                 }
401                 flag <<= 1;
402             }
403
404             for (k=1; k<=9; k++)
405                 c[k] = w[k];
406
407 #include "TextureFilters_hq4x.h"
408
409             pIn+=2;
410             pOut+=8;
411         }
412         pIn += 2*(SrcPPL-Xres);
413         pOut+= 8*(SrcPPL-Xres);
414         pOut+=BpL;
415         pOut+=BpL;
416         pOut+=BpL;
417     }
418
419 #undef BPP
420 #undef BPP2
421 #undef BPP3
422 #undef Diff
423 #undef hq4x_Interp1
424 #undef hq4x_Interp2
425 #undef hq4x_Interp3
426 #undef hq4x_Interp4
427 #undef hq4x_Interp5
428 #undef hq4x_Interp6
429 #undef hq4x_Interp7
430 #undef hq4x_Interp8
431 }
432
433 void hq4x_32( unsigned char * pIn, unsigned char * pOut, int Xres, int Yres, int SrcPPL, int BpL )
434 {
435 #define hq4x_Interp1 hq4x_Interp1_32
436 #define hq4x_Interp2 hq4x_Interp2_32
437 #define hq4x_Interp3 hq4x_Interp3_32
438 #define hq4x_Interp4 hq4x_Interp4_32
439 #define hq4x_Interp5 hq4x_Interp5_32
440 #define hq4x_Interp6 hq4x_Interp6_32
441 #define hq4x_Interp7 hq4x_Interp7_32
442 #define hq4x_Interp8 hq4x_Interp8_32
443 #define Diff Diff_32
444 #define BPP  4
445 #define BPP2  8
446 #define BPP3  12
447
448     int  i, j, k;
449     int  prevline, nextline;
450     uint32  w[10];
451     uint32  c[10];
452
453     //   +----+----+----+
454     //   |    |    |    |
455     //   | w1 | w2 | w3 |
456     //   +----+----+----+
457     //   |    |    |    |
458     //   | w4 | w5 | w6 |
459     //   +----+----+----+
460     //   |    |    |    |
461     //   | w7 | w8 | w9 |
462     //   +----+----+----+
463
464     for (j=0; j<Yres; j++)
465     {
466         if (j>0)      prevline = -SrcPPL*4; else prevline = 0;
467         if (j<Yres-1) nextline =  SrcPPL*4; else nextline = 0;
468
469         for (i=0; i<Xres; i++)
470         {
471             w[2] = *((uint32*)(pIn + prevline));
472             w[5] = *((uint32*)pIn);
473             w[8] = *((uint32*)(pIn + nextline));
474
475             if (i>0)
476             {
477                 w[1] = *((uint32*)(pIn + prevline - 4));
478                 w[4] = *((uint32*)(pIn - 4));
479                 w[7] = *((uint32*)(pIn + nextline - 4));
480             }
481             else
482             {
483                 w[1] = w[2];
484                 w[4] = w[5];
485                 w[7] = w[8];
486             }
487
488             if (i<Xres-1)
489             {
490                 w[3] = *((uint32*)(pIn + prevline + 4));
491                 w[6] = *((uint32*)(pIn + 4));
492                 w[9] = *((uint32*)(pIn + nextline + 4));
493             }
494             else
495             {
496                 w[3] = w[2];
497                 w[6] = w[5];
498                 w[9] = w[8];
499             }
500
501             int pattern = 0;
502             int flag = 1;
503
504             YUV1 = RGB32toYUV(w[5]);
505
506             for (k=1; k<=9; k++)
507             {
508                 if (k==5) continue;
509
510                 if ( w[k] != w[5] )
511                 {
512                     YUV2 = RGB32toYUV(w[k]);
513                     if ( ( abs((YUV1 & Amask) - (YUV2 & Amask)) > trA ) ||
514                         ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
515                         ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
516                         ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) )
517                         pattern |= flag;
518                 }
519                 flag <<= 1;
520             }
521
522             for (k=1; k<=9; k++)
523                 c[k] = w[k];
524
525 #include "TextureFilters_hq4x.h"
526
527             pIn+=4;
528             pOut+=16;
529         }
530
531         pIn += 4*(SrcPPL-Xres);
532         pOut+= 16*(SrcPPL-Xres);
533         pOut+=BpL;
534         pOut+=BpL;
535         pOut+=BpL;
536     }
537
538 #undef BPP
539 #undef BPP2
540 #undef BPP3
541 #undef Diff
542 #undef hq4x_Interp1
543 #undef hq4x_Interp2
544 #undef hq4x_Interp3
545 #undef hq4x_Interp4
546 #undef hq4x_Interp5
547 #undef hq4x_Interp6
548 #undef hq4x_Interp7
549 #undef hq4x_Interp8
550 }
551
552 void hq4x_InitLUTs(void)
553 {
554     static bool done = false;
555     int i, j, k, r, g, b, Y, u, v;
556
557     if( !done )
558     {
559         for (i=0; i<16; i++)
560         {
561             for (j=0; j<16; j++)
562             {
563                 for (k=0; k<16; k++)
564                 {
565                     r = i << 4;
566                     g = j << 4;
567                     b = k << 4;
568                     Y = (r + g + b) >> 2;
569                     u = 128 + ((r - b) >> 2);
570                     v = 128 + ((-r + 2*g -b)>>3);
571                     RGBtoYUV[ (i << 8) + (j << 4) + k ] = (Y<<16) + (u<<8) + v;
572                 }
573             }
574         }
575         done = true;
576     }
577 }
578