1 /*
2    (c) Copyright 2001-2011  The world wide DirectFB Open Source Community (directfb.org)
3    (c) Copyright 2000-2004  Convergence (integrated media) GmbH
4 
5    All rights reserved.
6 
7    Written by Denis Oliver Kropp <dok@directfb.org>,
8               Andreas Hundt <andi@fischlustig.de>,
9               Sven Neumann <neo@directfb.org>,
10               Ville Syrjälä <syrjala@sci.fi> and
11               Claudio Ciccani <klan@users.sf.net>.
12 
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 2 of the License, or (at your option) any later version.
17 
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22 
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, write to the
25    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26    Boston, MA 02111-1307, USA.
27 */
28 
29 /*
30  * Example:
31  * #define A_SHIFT 16
32  * #define R_SHIFT 11
33  * #define G_SHIFT 5
34  * #define B_SHIFT 0
35  * #define A_MASK 0xff0000
36  * #define R_MASK 0x00f800
37  * #define G_MASK 0x0007e0
38  * #define B_MASK 0x00001f
39  * #define PIXEL_OUT( a, r, g, b ) PIXEL_ARGB8565( a, r, g, b )
40  * #define EXPAND_Ato8( a ) (a)
41  * #define EXPAND_Rto8( r ) EXPAND_5to8( r )
42  * #define EXPAND_Gto8( g ) EXPAND_6to8( g )
43  * #define EXPAND_Bto8( b ) EXPAND_5to8( b )
44  * #define Sop_PFI_OP_Dacc( op ) Sop_argb8565_##op##_Dacc
45  * #define Sacc_OP_Aop_PFI( op ) Sacc_##op##_Aop_argb8565
46  * #include "template_acc_24.h"
47  */
48 
49 #define RGB_MASK (R_MASK | G_MASK | B_MASK)
50 
51 #if RGB_MASK == 0xffffff
52 #define MASK_RGB( p ) (p)
53 #else
54 #define MASK_RGB( p ) ((p) & RGB_MASK)
55 #endif
56 
57 #define PIXEL( x ) PIXEL_OUT( ((x).RGB.a & 0xFF00) ? 0xFF : (x).RGB.a, \
58                               ((x).RGB.r & 0xFF00) ? 0xFF : (x).RGB.r, \
59                               ((x).RGB.g & 0xFF00) ? 0xFF : (x).RGB.g, \
60                               ((x).RGB.b & 0xFF00) ? 0xFF : (x).RGB.b )
61 
62 #define EXPAND( d, s ) do { \
63      (d).RGB.a = EXPAND_Ato8( (s & A_MASK) >> A_SHIFT ); \
64      (d).RGB.r = EXPAND_Rto8( (s & R_MASK) >> R_SHIFT ); \
65      (d).RGB.g = EXPAND_Gto8( (s & G_MASK) >> G_SHIFT ); \
66      (d).RGB.b = EXPAND_Bto8( (s & B_MASK) >> B_SHIFT ); \
67 } while (0)
68 
69 #ifdef WORD_BIGENDIAN
70 #define READ_PIXEL( s ) \
71      (0 \
72       | ((s)[0] << 16) \
73       | ((s)[1] <<  8) \
74       | ((s)[2] <<  0) \
75      )
76 #define WRITE_PIXEL( d, pix ) \
77      do { \
78           (d)[0] = ((pix) >> 16) & 0xff; \
79           (d)[1] = ((pix) >>  8) & 0xff; \
80           (d)[2] = ((pix) >>  0) & 0xff; \
81      } while (0)
82 #else
83 #define READ_PIXEL( s ) \
84      (0 \
85       | ((s)[2] << 16) \
86       | ((s)[1] <<  8) \
87       | ((s)[0] <<  0) \
88      )
89 #define WRITE_PIXEL( d, pix ) \
90      do { \
91           (d)[0] = ((pix) >>  0) & 0xff; \
92           (d)[1] = ((pix) >>  8) & 0xff; \
93           (d)[2] = ((pix) >> 16) & 0xff; \
94      } while (0)
95 #endif
96 
97 /********************************* Sop_PFI_Sto_Dacc ***************************/
98 
Sop_PFI_OP_Dacc(Sto)99 static void Sop_PFI_OP_Dacc(Sto)( GenefxState *gfxs )
100 {
101      int w     = gfxs->length + 1;
102      int i     = gfxs->Xphase;
103      int SperD = gfxs->SperD;
104 
105      GenefxAccumulator *D = gfxs->Dacc;
106      const u8          *S = gfxs->Sop[0];
107 
108      int Ostep = gfxs->Ostep;
109 
110      if (Ostep != 1)
111           D_UNIMPLEMENTED();
112 
113      while (--w) {
114           int pixelstart = (i >> 16) * 3;
115 
116           u32 s = READ_PIXEL( &S[pixelstart] );
117 
118           EXPAND( *D, s );
119 
120           ++D;
121           i += SperD;
122      }
123 }
124 
125 /********************************* Sop_PFI_SKto_Dacc **************************/
126 
Sop_PFI_OP_Dacc(SKto)127 static void Sop_PFI_OP_Dacc(SKto)( GenefxState *gfxs )
128 {
129      int w     = gfxs->length + 1;
130      int i     = gfxs->Xphase;
131      int SperD = gfxs->SperD;
132      u32 Skey  = gfxs->Skey;
133 
134      GenefxAccumulator *D = gfxs->Dacc;
135      const u8          *S = gfxs->Sop[0];
136 
137      int Ostep = gfxs->Ostep;
138 
139      if (Ostep != 1)
140           D_UNIMPLEMENTED();
141 
142      while (--w) {
143           int pixelstart = (i >> 16) * 3;
144 
145           u32 s = READ_PIXEL( &S[pixelstart] );
146 
147           if (MASK_RGB( s ) != Skey)
148                EXPAND( *D, s );
149           else
150                D->RGB.a = 0xF000;
151 
152           ++D;
153           i += SperD;
154      }
155 }
156 
157 /********************************* Sop_PFI_to_Dacc ****************************/
158 
Sop_PFI_OP_Dacc(to)159 static void Sop_PFI_OP_Dacc(to)( GenefxState *gfxs )
160 {
161      int  w      = gfxs->length + 1;
162      const u8 *S = gfxs->Sop[0];
163      int  Ostep  = gfxs->Ostep * 3;
164 
165      GenefxAccumulator *D = gfxs->Dacc;
166 
167      while (--w) {
168           u32 s = READ_PIXEL( S );
169 
170           EXPAND( *D, s );
171 
172           ++D;
173           S += Ostep;
174      }
175 }
176 
177 /********************************* Sop_PFI_Kto_Dacc ***************************/
178 
Sop_PFI_OP_Dacc(Kto)179 static void Sop_PFI_OP_Dacc(Kto)( GenefxState *gfxs )
180 {
181      int  w      = gfxs->length + 1;
182      const u8 *S = gfxs->Sop[0];
183      u32  Skey   = gfxs->Skey;
184      GenefxAccumulator *D = gfxs->Dacc;
185 
186      int Ostep = gfxs->Ostep * 3;
187 
188      while (--w) {
189           u32 s = READ_PIXEL( S );
190 
191           if (MASK_RGB( s ) != Skey)
192                EXPAND( *D, s );
193           else
194                D->RGB.a = 0xF000;
195 
196           ++D;
197           S += Ostep;
198      }
199 }
200 
201 /********************************* Sacc_to_Aop_PFI ****************************/
202 
Sacc_OP_Aop_PFI(to)203 static void Sacc_OP_Aop_PFI(to)( GenefxState *gfxs )
204 {
205      int l = gfxs->length + 1;
206      const GenefxAccumulator *S = gfxs->Sacc;
207      u8  *D     = gfxs->Aop[0];
208      int  Dstep = gfxs->Astep * 3;
209 
210      while (--l) {
211           if (!(S->RGB.a & 0xF000)) {
212                u32 pix = PIXEL( *S );
213                WRITE_PIXEL( D, pix );
214           }
215 
216           ++S;
217           D += Dstep;
218      }
219 }
220 
221 /********************************* Sacc_Sto_Aop_PFI ***************************/
222 
Sacc_OP_Aop_PFI(Sto)223 static void Sacc_OP_Aop_PFI(Sto)( GenefxState *gfxs )
224 {
225      int                w     = gfxs->length + 1;
226      int                i     = gfxs->Xphase;
227      int                SperD = gfxs->SperD;
228      const GenefxAccumulator *Sacc = gfxs->Sacc;
229      u8                *D     = gfxs->Aop[0];
230      int                Dstep = gfxs->Astep * 3;
231 
232      while (--w) {
233           const GenefxAccumulator *S = &Sacc[i>>16];
234 
235           if (!(S->RGB.a & 0xF000)) {
236                u32 pix = PIXEL( *S );
237                WRITE_PIXEL( D, pix );
238           }
239 
240           D += Dstep;
241           i += SperD;
242      }
243 }
244 
245 /********************************* Sacc_toK_Aop_PFI ***************************/
246 
Sacc_OP_Aop_PFI(toK)247 static void Sacc_OP_Aop_PFI(toK)( GenefxState *gfxs )
248 {
249      int  w     = gfxs->length + 1;
250      u8  *D     = gfxs->Aop[0];
251      u32  Dkey  = gfxs->Dkey;
252      int  Dstep = gfxs->Astep * 3;
253      const GenefxAccumulator *S = gfxs->Sacc;
254 
255      while (--w) {
256           if (!(S->RGB.a & 0xF000)) {
257                u32 pix = READ_PIXEL( D );
258                if (MASK_RGB( pix ) == Dkey) {
259                     pix = PIXEL( *S );
260                     WRITE_PIXEL( D, pix );
261                }
262           }
263 
264           ++S;
265           D += Dstep;
266      }
267 }
268 
269 /********************************* Sacc_StoK_Aop_PFI **************************/
270 
Sacc_OP_Aop_PFI(StoK)271 static void Sacc_OP_Aop_PFI(StoK)( GenefxState *gfxs )
272 {
273      int  w     = gfxs->length + 1;
274      int  i     = gfxs->Xphase;
275      int  SperD = gfxs->SperD;
276      u8  *D     = gfxs->Aop[0];
277      int  Dstep = gfxs->Astep * 3;
278      u32  Dkey  = gfxs->Dkey;
279      const GenefxAccumulator *Sacc  = gfxs->Sacc;
280 
281      while (--w) {
282           const GenefxAccumulator *S = &Sacc[i>>16];
283 
284           if (!(S->RGB.a & 0xF000)) {
285                u32 pix = READ_PIXEL( D );
286                if (MASK_RGB( pix ) == Dkey) {
287                     pix = PIXEL( *S );
288                     WRITE_PIXEL( D, pix );
289                }
290           }
291 
292           D += Dstep;
293           i += SperD;
294      }
295 }
296 
297 /********************************* Sop_PFI_TEX_to_Dacc ***************************/
298 
Sop_PFI_OP_Dacc(TEX_to)299 static void Sop_PFI_OP_Dacc(TEX_to)( GenefxState *gfxs )
300 {
301      int                w     = gfxs->length + 1;
302      int                s     = gfxs->s;
303      int                t     = gfxs->t;
304      int                SperD = gfxs->SperD;
305      int                TperD = gfxs->TperD;
306      u8                *S     = gfxs->Sop[0];
307      int                Ostep = gfxs->Ostep * 3;
308      GenefxAccumulator *D     = gfxs->Dacc;
309      int                sp3   = gfxs->src_pitch / 3;
310 
311      if (Ostep != 1)
312           D_UNIMPLEMENTED();
313 
314      while (--w) {
315           int pixelstart = ((s>>16) + (t>>16) * sp3) * 3;
316           u32 p = READ_PIXEL( &S[pixelstart] );
317 
318           EXPAND( *D, p );
319 
320           D++;
321           s += SperD;
322           t += TperD;
323      }
324 }
325 
326 /********************************* Sop_PFI_TEX_Kto_Dacc ***************************/
327 
Sop_PFI_OP_Dacc(TEX_Kto)328 static void Sop_PFI_OP_Dacc(TEX_Kto)( GenefxState *gfxs )
329 {
330      int                w     = gfxs->length + 1;
331      int                s     = gfxs->s;
332      int                t     = gfxs->t;
333      int                SperD = gfxs->SperD;
334      int                TperD = gfxs->TperD;
335      const u8          *S     = gfxs->Sop[0];
336      u32                Skey  = gfxs->Skey;
337      int                Ostep = gfxs->Ostep * 3;
338      GenefxAccumulator *D     = gfxs->Dacc;
339      int                sp3   = gfxs->src_pitch / 3;
340 
341      if (Ostep != 1)
342           D_UNIMPLEMENTED();
343 
344      while (--w) {
345           int pixelstart = ((s>>16) + (t>>16) * sp3) * 3;
346           u32 p = READ_PIXEL( &S[pixelstart] );
347 
348           if (MASK_RGB( p ) != Skey)
349                EXPAND( *D, p );
350           else
351                D->RGB.a = 0xF000;
352 
353           D++;
354           s += SperD;
355           t += TperD;
356      }
357 }
358 
359 /******************************************************************************/
360 
361 #undef RGB_MASK
362 #undef MASK_RGB
363 #undef PIXEL
364 #undef EXPAND
365 
366 #undef A_SHIFT
367 #undef R_SHIFT
368 #undef G_SHIFT
369 #undef B_SHIFT
370 #undef A_MASK
371 #undef R_MASK
372 #undef G_MASK
373 #undef B_MASK
374 #undef PIXEL_OUT
375 #undef EXPAND_Ato8
376 #undef EXPAND_Rto8
377 #undef EXPAND_Gto8
378 #undef EXPAND_Bto8
379 #undef Sop_PFI_OP_Dacc
380 #undef Sacc_OP_Aop_PFI
381 
382 #undef READ_PIXEL
383 #undef WRITE_PIXEL
384