winex11: Always ignore alpha channel with XRender gradients.
[wine] / dlls / gdi32 / dibdrv / bitblt.c
1 /*
2  * DIB driver blitting
3  *
4  * Copyright 2011 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22
23 #include "gdi_private.h"
24 #include "dibdrv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
29
30 #define DST 0   /* Destination dib */
31 #define SRC 1   /* Source dib */
32 #define TMP 2   /* Temporary dib */
33 #define PAT 3   /* Pattern (brush) in destination DC */
34
35 #define OP(src,dst,rop)   (OP_ARGS(src,dst) << 4 | ((rop) - 1))
36 #define OP_ARGS(src,dst)  (((src) << 2) | (dst))
37
38 #define OP_SRC(opcode)    ((opcode) >> 6)
39 #define OP_DST(opcode)    (((opcode) >> 4) & 3)
40 #define OP_SRCDST(opcode) ((opcode) >> 4)
41 #define OP_ROP(opcode)    (((opcode) & 0x0f) + 1)
42
43 #define MAX_OP_LEN  6  /* Longest opcode + 1 for the terminating 0 */
44
45 static const unsigned char BITBLT_Opcodes[256][MAX_OP_LEN] =
46 {
47     { OP(PAT,DST,R2_BLACK) },                                       /* 0x00  0              */
48     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_NOTMERGEPEN) },        /* 0x01  ~(D|(P|S))     */
49     { OP(PAT,SRC,R2_NOTMERGEPEN), OP(SRC,DST,R2_MASKPEN) },         /* 0x02  D&~(P|S)       */
50     { OP(PAT,SRC,R2_NOTMERGEPEN) },                                 /* 0x03  ~(P|S)         */
51     { OP(PAT,DST,R2_NOTMERGEPEN), OP(SRC,DST,R2_MASKPEN) },         /* 0x04  S&~(D|P)       */
52     { OP(PAT,DST,R2_NOTMERGEPEN) },                                 /* 0x05  ~(D|P)         */
53     { OP(SRC,DST,R2_NOTXORPEN), OP(PAT,DST,R2_NOTMERGEPEN), },      /* 0x06  ~(P|~(D^S))    */
54     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_NOTMERGEPEN) },         /* 0x07  ~(P|(D&S))     */
55     { OP(PAT,DST,R2_MASKNOTPEN), OP(SRC,DST,R2_MASKPEN) },          /* 0x08  S&D&~P         */
56     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_NOTMERGEPEN) },          /* 0x09  ~(P|(D^S))     */
57     { OP(PAT,DST,R2_MASKNOTPEN) },                                  /* 0x0a  D&~P           */
58     { OP(SRC,DST,R2_MASKPENNOT), OP(PAT,DST,R2_NOTMERGEPEN) },      /* 0x0b  ~(P|(S&~D))    */
59     { OP(PAT,SRC,R2_MASKNOTPEN) },                                  /* 0x0c  S&~P           */
60     { OP(SRC,DST,R2_MASKNOTPEN), OP(PAT,DST,R2_NOTMERGEPEN) },      /* 0x0d  ~(P|(D&~S))    */
61     { OP(SRC,DST,R2_NOTMERGEPEN), OP(PAT,DST,R2_NOTMERGEPEN) },     /* 0x0e  ~(P|~(D|S))    */
62     { OP(PAT,DST,R2_NOTCOPYPEN) },                                  /* 0x0f  ~P             */
63     { OP(SRC,DST,R2_NOTMERGEPEN), OP(PAT,DST,R2_MASKPEN) },         /* 0x10  P&~(S|D)       */
64     { OP(SRC,DST,R2_NOTMERGEPEN) },                                 /* 0x11  ~(D|S)         */
65     { OP(PAT,DST,R2_NOTXORPEN), OP(SRC,DST,R2_NOTMERGEPEN) },       /* 0x12  ~(S|~(D^P))    */
66     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_NOTMERGEPEN) },         /* 0x13  ~(S|(D&P))     */
67     { OP(PAT,SRC,R2_NOTXORPEN), OP(SRC,DST,R2_NOTMERGEPEN) },       /* 0x14  ~(D|~(P^S))    */
68     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_NOTMERGEPEN) },         /* 0x15  ~(D|(P&S))     */
69     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMASKPEN),
70       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN),
71       OP(PAT,DST,R2_XORPEN) },                                      /* 0x16  P^S^(D&~(P&S)  */
72     { OP(SRC,TMP,R2_COPYPEN), OP(SRC,DST,R2_XORPEN),
73       OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
74       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x17 ~S^((S^P)&(S^D))*/
75     { OP(PAT,SRC,R2_XORPEN), OP(PAT,DST,R2_XORPEN),
76         OP(SRC,DST,R2_MASKPEN) },                                   /* 0x18  (S^P)&(D^P)    */
77     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMASKPEN),
78       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x19  ~S^(D&~(P&S))  */
79     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN),
80       OP(PAT,DST,R2_XORPEN) },                                      /* 0x1a  P^(D|(S&P))    */
81     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
82       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x1b  ~S^(D&(P^S))   */
83     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN),
84       OP(PAT,DST,R2_XORPEN) },                                      /* 0x1c  P^(S|(D&P))    */
85     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_XORPEN),
86       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x1d  ~D^(S&(D^P))   */
87     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_XORPEN) },             /* 0x1e  P^(D|S)        */
88     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_NOTMASKPEN) },         /* 0x1f  ~(P&(D|S))     */
89     { OP(PAT,SRC,R2_MASKPENNOT), OP(SRC,DST,R2_MASKPEN) },          /* 0x20  D&(P&~S)       */
90     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_NOTMERGEPEN) },          /* 0x21  ~(S|(D^P))     */
91     { OP(SRC,DST,R2_MASKNOTPEN) },                                  /* 0x22  ~S&D           */
92     { OP(PAT,DST,R2_MASKPENNOT), OP(SRC,DST,R2_NOTMERGEPEN) },      /* 0x23  ~(S|(P&~D))    */
93     { OP(SRC,DST,R2_XORPEN), OP(PAT,SRC,R2_XORPEN),
94       OP(SRC,DST,R2_MASKPEN) },                                     /* 0x24   (S^P)&(S^D)   */
95     { OP(PAT,SRC,R2_NOTMASKPEN), OP(SRC,DST,R2_MASKPEN),
96       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x25  ~P^(D&~(S&P))  */
97     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPEN),
98       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x26  S^(D|(S&P))    */
99     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTXORPEN),
100       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x27  S^(D|~(P^S))   */
101     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MASKPEN) },              /* 0x28  D&(P^S)        */
102     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPEN),
103       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN),
104       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x29  ~P^S^(D|(P&S)) */
105     { OP(PAT,SRC,R2_NOTMASKPEN), OP(SRC,DST,R2_MASKPEN) },          /* 0x2a  D&~(P&S)       */
106     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
107       OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
108       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x2b ~S^((P^S)&(P^D))*/
109     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_MASKPEN),
110       OP(SRC,DST,R2_XORPEN) },                                      /* 0x2c  S^(P&(S|D))    */
111     { OP(SRC,DST,R2_MERGEPENNOT), OP(PAT,DST,R2_XORPEN) },          /* 0x2d  P^(S|~D)       */
112     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
113       OP(PAT,DST,R2_XORPEN) },                                      /* 0x2e  P^(S|(D^P))    */
114     { OP(SRC,DST,R2_MERGEPENNOT), OP(PAT,DST,R2_NOTMASKPEN) },      /* 0x2f  ~(P&(S|~D))    */
115     { OP(PAT,SRC,R2_MASKPENNOT) },                                  /* 0x30  P&~S           */
116     { OP(PAT,DST,R2_MASKNOTPEN), OP(SRC,DST,R2_NOTMERGEPEN) },      /* 0x31  ~(S|(D&~P))    */
117     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_MERGEPEN),
118       OP(SRC,DST,R2_XORPEN) },                                      /* 0x32  S^(D|P|S)      */
119     { OP(SRC,DST,R2_NOTCOPYPEN) },                                  /* 0x33  ~S             */
120     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_MERGEPEN),
121       OP(SRC,DST,R2_XORPEN) },                                      /* 0x34  S^(P|(D&S))    */
122     { OP(SRC,DST,R2_NOTXORPEN), OP(PAT,DST,R2_MERGEPEN),
123       OP(SRC,DST,R2_XORPEN) },                                      /* 0x35  S^(P|~(D^S))   */
124     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_XORPEN) },             /* 0x36  S^(D|P)        */
125     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_NOTMASKPEN) },         /* 0x37  ~(S&(D|P))     */
126     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN),
127       OP(PAT,DST,R2_XORPEN) },                                      /* 0x38  P^(S&(D|P))    */
128     { OP(PAT,DST,R2_MERGEPENNOT), OP(SRC,DST,R2_XORPEN) },          /* 0x39  S^(P|~D)       */
129     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MERGEPEN),
130       OP(SRC,DST,R2_XORPEN) },                                      /* 0x3a  S^(P|(D^S))    */
131     { OP(PAT,DST,R2_MERGEPENNOT), OP(SRC,DST,R2_NOTMASKPEN) },      /* 0x3b  ~(S&(P|~D))    */
132     { OP(PAT,SRC,R2_XORPEN) },                                      /* 0x3c  P^S            */
133     { OP(SRC,DST,R2_NOTMERGEPEN), OP(PAT,DST,R2_MERGEPEN),
134       OP(SRC,DST,R2_XORPEN) },                                      /* 0x3d  S^(P|~(D|S))   */
135     { OP(SRC,DST,R2_MASKNOTPEN), OP(PAT,DST,R2_MERGEPEN),
136       OP(SRC,DST,R2_XORPEN) },                                      /* 0x3e  S^(P|(D&~S))   */
137     { OP(PAT,SRC,R2_NOTMASKPEN) },                                  /* 0x3f  ~(P&S)         */
138     { OP(SRC,DST,R2_MASKPENNOT), OP(PAT,DST,R2_MASKPEN) },          /* 0x40  P&S&~D         */
139     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_NOTMERGEPEN) },          /* 0x41  ~(D|(P^S))     */
140     { OP(DST,SRC,R2_XORPEN), OP(PAT,DST,R2_XORPEN),
141       OP(SRC,DST,R2_MASKPEN) },                                     /* 0x42  (S^D)&(P^D)    */
142     { OP(SRC,DST,R2_NOTMASKPEN), OP(PAT,DST,R2_MASKPEN),
143       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0x43  ~S^(P&~(D&S))  */
144     { OP(SRC,DST,R2_MASKPENNOT) },                                  /* 0x44  S&~D           */
145     { OP(PAT,SRC,R2_MASKPENNOT), OP(SRC,DST,R2_NOTMERGEPEN) },      /* 0x45  ~(D|(P&~S))    */
146     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MASKPEN),
147       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x46  D^(S|(P&D))    */
148     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
149       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x47  ~P^(S&(D^P))   */
150     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN) },              /* 0x48  S&(P^D)        */
151     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MASKPEN),
152       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN),
153       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x49  ~P^D^(S|(P&D)) */
154     { OP(DST,SRC,R2_MERGEPEN), OP(PAT,SRC,R2_MASKPEN),
155       OP(SRC,DST,R2_XORPEN) },                                      /* 0x4a  D^(P&(S|D))    */
156     { OP(SRC,DST,R2_MERGENOTPEN), OP(PAT,DST,R2_XORPEN) },          /* 0x4b  P^(D|~S)       */
157     { OP(PAT,DST,R2_NOTMASKPEN), OP(SRC,DST,R2_MASKPEN) },          /* 0x4c  S&~(D&P)       */
158     { OP(SRC,TMP,R2_COPYPEN), OP(SRC,DST,R2_XORPEN),
159       OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
160       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x4d ~S^((S^P)|(S^D))*/
161     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
162       OP(PAT,DST,R2_XORPEN) },                                      /* 0x4e  P^(D|(S^P))    */
163     { OP(SRC,DST,R2_MERGENOTPEN), OP(PAT,DST,R2_NOTMASKPEN) },      /* 0x4f  ~(P&(D|~S))    */
164     { OP(PAT,DST,R2_MASKPENNOT) },                                  /* 0x50  P&~D           */
165     { OP(PAT,SRC,R2_MASKNOTPEN), OP(SRC,DST,R2_NOTMERGEPEN) },      /* 0x51  ~(D|(S&~P))    */
166     { OP(DST,SRC,R2_MASKPEN), OP(PAT,SRC,R2_MERGEPEN),
167       OP(SRC,DST,R2_XORPEN) },                                      /* 0x52  D^(P|(S&D))    */
168     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MASKPEN),
169       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0x53  ~S^(P&(D^S))   */
170     { OP(PAT,SRC,R2_NOTMERGEPEN), OP(SRC,DST,R2_NOTMERGEPEN) },     /* 0x54  ~(D|~(P|S))    */
171     { OP(PAT,DST,R2_NOT) },                                         /* 0x55  ~D             */
172     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_XORPEN) },             /* 0x56  D^(P|S)        */
173     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_NOTMASKPEN) },         /* 0x57  ~(D&(P|S))     */
174     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN),
175       OP(PAT,DST,R2_XORPEN) },                                      /* 0x58  P^(D&(P|S))    */
176     { OP(PAT,SRC,R2_MERGEPENNOT), OP(SRC,DST,R2_XORPEN) },          /* 0x59  D^(P|~S)       */
177     { OP(PAT,DST,R2_XORPEN) },                                      /* 0x5a  D^P            */
178     { OP(DST,SRC,R2_NOTMERGEPEN), OP(PAT,SRC,R2_MERGEPEN),
179       OP(SRC,DST,R2_XORPEN) },                                      /* 0x5b  D^(P|~(S|D))   */
180     { OP(DST,SRC,R2_XORPEN), OP(PAT,SRC,R2_MERGEPEN),
181       OP(SRC,DST,R2_XORPEN) },                                      /* 0x5c  D^(P|(S^D))    */
182     { OP(PAT,SRC,R2_MERGEPENNOT), OP(SRC,DST,R2_NOTMASKPEN) },      /* 0x5d  ~(D&(P|~S))    */
183     { OP(DST,SRC,R2_MASKNOTPEN), OP(PAT,SRC,R2_MERGEPEN),
184       OP(SRC,DST,R2_XORPEN) },                                      /* 0x5e  D^(P|(S&~D))   */
185     { OP(PAT,DST,R2_NOTMASKPEN) },                                  /* 0x5f  ~(D&P)         */
186     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MASKPEN) },              /* 0x60  P&(D^S)        */
187     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_MASKPEN),
188       OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_XORPEN),
189       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x61  ~D^S^(P|(D&S)) */
190     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MERGEPEN),
191       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0x62  D^(S&(P|D))    */
192     { OP(PAT,DST,R2_MERGENOTPEN), OP(SRC,DST,R2_XORPEN) },          /* 0x63  S^(D|~P)       */
193     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPEN),
194       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0x64  S^(D&(P|S))    */
195     { OP(PAT,SRC,R2_MERGENOTPEN), OP(SRC,DST,R2_XORPEN) },          /* 0x65  D^(S|~P)       */
196     { OP(SRC,DST,R2_XORPEN) },                                      /* 0x66  S^D            */
197     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMERGEPEN),
198       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x67  S^(D|~(S|P)    */
199     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_NOTMERGEPEN),
200       OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_XORPEN),
201       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x68  ~D^S^(P|~(D|S))*/
202     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_NOTXORPEN) },            /* 0x69  ~P^(D^S)       */
203     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_XORPEN) },              /* 0x6a  D^(P&S)        */
204     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPEN),
205       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN),
206       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x6b  ~P^S^(D&(P|S)) */
207     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_XORPEN) },              /* 0x6c  S^(D&P)        */
208     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MERGEPEN),
209       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN),
210       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x6d  ~P^D^(S&(P|D)) */
211     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPENNOT),
212       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0x6e  S^(D&(P|~S))   */
213     { OP(SRC,DST,R2_NOTXORPEN), OP(PAT,DST,R2_NOTMASKPEN) },        /* 0x6f  ~(P&~(S^D))    */
214     { OP(SRC,DST,R2_NOTMASKPEN), OP(PAT,DST,R2_MASKPEN) },          /* 0x70  P&~(D&S)       */
215     { OP(SRC,TMP,R2_COPYPEN), OP(DST,SRC,R2_XORPEN),
216       OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
217       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x71 ~S^((S^D)&(P^D))*/
218     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
219       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x72  S^(D|(P^S))    */
220     { OP(PAT,DST,R2_MERGENOTPEN), OP(SRC,DST,R2_NOTMASKPEN) },      /* 0x73  ~(S&(D|~P))    */
221     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_XORPEN),
222       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x74   D^(S|(P^D))   */
223     { OP(PAT,SRC,R2_MERGENOTPEN), OP(SRC,DST,R2_NOTMASKPEN) },      /* 0x75  ~(D&(S|~P))    */
224     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPENNOT),
225       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_XORPEN) },             /* 0x76  S^(D|(P&~S))   */
226     { OP(SRC,DST,R2_NOTMASKPEN) },                                  /* 0x77  ~(S&D)         */
227     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_XORPEN) },              /* 0x78  P^(D&S)        */
228     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_MERGEPEN),
229       OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_XORPEN),
230       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0x79  ~D^S^(P&(D|S)) */
231     { OP(DST,SRC,R2_MERGENOTPEN), OP(PAT,SRC,R2_MASKPEN),
232       OP(SRC,DST,R2_XORPEN) },                                      /* 0x7a  D^(P&(S|~D))   */
233     { OP(PAT,DST,R2_NOTXORPEN), OP(SRC,DST,R2_NOTMASKPEN) },        /* 0x7b  ~(S&~(D^P))    */
234     { OP(SRC,DST,R2_MERGENOTPEN), OP(PAT,DST,R2_MASKPEN),
235       OP(SRC,DST,R2_XORPEN) },                                      /* 0x7c  S^(P&(D|~S))   */
236     { OP(PAT,SRC,R2_NOTXORPEN), OP(SRC,DST,R2_NOTMASKPEN) },        /* 0x7d  ~(D&~(P^S))    */
237     { OP(SRC,DST,R2_XORPEN), OP(PAT,SRC,R2_XORPEN),
238       OP(SRC,DST,R2_MERGEPEN) },                                    /* 0x7e  (S^P)|(S^D)    */
239     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_NOTMASKPEN) },          /* 0x7f  ~(D&P&S)       */
240     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_MASKPEN) },             /* 0x80  D&P&S          */
241     { OP(SRC,DST,R2_XORPEN), OP(PAT,SRC,R2_XORPEN),
242       OP(SRC,DST,R2_NOTMERGEPEN) },                                 /* 0x81  ~((S^P)|(S^D)) */
243     { OP(PAT,SRC,R2_NOTXORPEN), OP(SRC,DST,R2_MASKPEN) },           /* 0x82  D&~(P^S)       */
244     { OP(SRC,DST,R2_MERGENOTPEN), OP(PAT,DST,R2_MASKPEN),
245       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0x83  ~S^(P&(D|~S))  */
246     { OP(PAT,DST,R2_NOTXORPEN), OP(SRC,DST,R2_MASKPEN) },           /* 0x84  S&~(D^P)       */
247     { OP(PAT,SRC,R2_MERGENOTPEN), OP(SRC,DST,R2_MASKPEN),
248       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0x85  ~P^(D&(S|~P))  */
249     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_MERGEPEN),
250       OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_XORPEN),
251       OP(TMP,DST,R2_XORPEN) },                                      /* 0x86  D^S^(P&(D|S))  */
252     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_NOTXORPEN) },           /* 0x87  ~P^(D&S)       */
253     { OP(SRC,DST,R2_MASKPEN) },                                     /* 0x88  S&D            */
254     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPENNOT),
255       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0x89  ~S^(D|(P&~S))  */
256     { OP(PAT,SRC,R2_MERGENOTPEN), OP(SRC,DST,R2_MASKPEN) },         /* 0x8a  D&(S|~P)       */
257     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_XORPEN),
258       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0x8b  ~D^(S|(P^D))   */
259     { OP(PAT,DST,R2_MERGENOTPEN), OP(SRC,DST,R2_MASKPEN) },         /* 0x8c  S&(D|~P)       */
260     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
261       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0x8d  ~S^(D|(P^S))   */
262     { OP(SRC,TMP,R2_COPYPEN), OP(DST,SRC,R2_XORPEN),
263       OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
264       OP(TMP,DST,R2_XORPEN) },                                      /* 0x8e  S^((S^D)&(P^D))*/
265     { OP(SRC,DST,R2_NOTMASKPEN), OP(PAT,DST,R2_NOTMASKPEN) },       /* 0x8f  ~(P&~(D&S))    */
266     { OP(SRC,DST,R2_NOTXORPEN), OP(PAT,DST,R2_MASKPEN) },           /* 0x90  P&~(D^S)       */
267     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPENNOT),
268       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x91  ~S^(D&(P|~S))  */
269     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MERGEPEN),
270       OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_XORPEN),
271       OP(TMP,DST,R2_XORPEN) },                                      /* 0x92  D^P^(S&(D|P))  */
272     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_NOTXORPEN) },           /* 0x93  ~S^(P&D)       */
273     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPEN),
274       OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_XORPEN),
275       OP(TMP,DST,R2_XORPEN) },                                      /* 0x94  S^P^(D&(P|S))  */
276     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_NOTXORPEN) },           /* 0x95  ~D^(P&S)       */
277     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_XORPEN) },               /* 0x96  D^P^S          */
278     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMERGEPEN),
279       OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_XORPEN),
280       OP(TMP,DST,R2_XORPEN) },                                      /* 0x97  S^P^(D|~(P|S)) */
281     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMERGEPEN),
282       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0x98  ~S^(D|~(P|S))  */
283     { OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0x99  ~S^D           */
284     { OP(PAT,SRC,R2_MASKPENNOT), OP(SRC,DST,R2_XORPEN) },           /* 0x9a  D^(P&~S)       */
285     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MERGEPEN),
286       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x9b  ~S^(D&(P|S))   */
287     { OP(PAT,DST,R2_MASKPENNOT), OP(SRC,DST,R2_XORPEN) },           /* 0x9c  S^(P&~D)       */
288     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MERGEPEN),
289       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_NOTXORPEN) },           /* 0x9d  ~D^(S&(P|D))   */
290     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_MASKPEN),
291       OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_XORPEN),
292       OP(TMP,DST,R2_XORPEN) },                                      /* 0x9e  D^S^(P|(D&S))  */
293     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_NOTMASKPEN) },           /* 0x9f  ~(P&(D^S))     */
294     { OP(PAT,DST,R2_MASKPEN) },                                     /* 0xa0  D&P            */
295     { OP(PAT,SRC,R2_MASKNOTPEN), OP(SRC,DST,R2_MERGEPEN),
296       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xa1  ~P^(D|(S&~P))  */
297     { OP(PAT,SRC,R2_MERGEPENNOT), OP(SRC,DST,R2_MASKPEN) },         /* 0xa2  D&(P|~S)       */
298     { OP(DST,SRC,R2_XORPEN), OP(PAT,SRC,R2_MERGEPEN),
299       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xa3  ~D^(P|(S^D))   */
300     { OP(PAT,SRC,R2_NOTMERGEPEN), OP(SRC,DST,R2_MERGEPEN),
301       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xa4  ~P^(D|~(S|P))  */
302     { OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xa5  ~P^D           */
303     { OP(PAT,SRC,R2_MASKNOTPEN), OP(SRC,DST,R2_XORPEN) },           /* 0xa6  D^(S&~P)       */
304     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN),
305       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xa7  ~P^(D&(S|P))   */
306     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN) },            /* 0xa8  D&(P|S)        */
307     { OP(PAT,SRC,R2_MERGEPEN), OP(SRC,DST,R2_NOTXORPEN) },          /* 0xa9  ~D^(P|S)       */
308     { OP(PAT,DST,R2_NOP) },                                         /* 0xaa  D              */
309     { OP(PAT,SRC,R2_NOTMERGEPEN), OP(SRC,DST,R2_MERGEPEN) },        /* 0xab  D|~(P|S)       */
310     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MASKPEN),
311       OP(SRC,DST,R2_XORPEN) },                                      /* 0xac  S^(P&(D^S))    */
312     { OP(DST,SRC,R2_MASKPEN), OP(PAT,SRC,R2_MERGEPEN),
313       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xad  ~D^(P|(S&D))   */
314     { OP(PAT,SRC,R2_MASKNOTPEN), OP(SRC,DST,R2_MERGEPEN) },         /* 0xae  D|(S&~P)       */
315     { OP(PAT,DST,R2_MERGENOTPEN) },                                 /* 0xaf  D|~P           */
316     { OP(SRC,DST,R2_MERGENOTPEN), OP(PAT,DST,R2_MASKPEN) },         /* 0xb0  P&(D|~S)       */
317     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
318       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xb1  ~P^(D|(S^P))   */
319     { OP(SRC,TMP,R2_COPYPEN), OP(SRC,DST,R2_XORPEN),
320       OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
321       OP(TMP,DST,R2_XORPEN) },                                      /* 0xb2  S^((S^P)|(S^D))*/
322     { OP(PAT,DST,R2_NOTMASKPEN), OP(SRC,DST,R2_NOTMASKPEN) },       /* 0xb3  ~(S&~(D&P))    */
323     { OP(SRC,DST,R2_MASKPENNOT), OP(PAT,DST,R2_XORPEN) },           /* 0xb4  P^(S&~D)       */
324     { OP(DST,SRC,R2_MERGEPEN), OP(PAT,SRC,R2_MASKPEN),
325       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xb5  ~D^(P&(S|D))   */
326     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MASKPEN),
327       OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_XORPEN),
328       OP(TMP,DST,R2_XORPEN) },                                      /* 0xb6  D^P^(S|(D&P))  */
329     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_NOTMASKPEN) },           /* 0xb7  ~(S&(D^P))     */
330     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
331       OP(PAT,DST,R2_XORPEN) },                                      /* 0xb8  P^(S&(D^P))    */
332     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_MASKPEN),
333       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0xb9  ~D^(S|(P&D))   */
334     { OP(PAT,SRC,R2_MASKPENNOT), OP(SRC,DST,R2_MERGEPEN) },         /* 0xba  D|(P&~S)       */
335     { OP(SRC,DST,R2_MERGENOTPEN) },                                 /* 0xbb  ~S|D           */
336     { OP(SRC,DST,R2_NOTMASKPEN), OP(PAT,DST,R2_MASKPEN),
337       OP(SRC,DST,R2_XORPEN) },                                      /* 0xbc  S^(P&~(D&S))   */
338     { OP(DST,SRC,R2_XORPEN), OP(PAT,DST,R2_XORPEN),
339       OP(SRC,DST,R2_NOTMASKPEN) },                                  /* 0xbd  ~((S^D)&(P^D)) */
340     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN) },             /* 0xbe  D|(P^S)        */
341     { OP(PAT,SRC,R2_NOTMASKPEN), OP(SRC,DST,R2_MERGEPEN) },         /* 0xbf  D|~(P&S)       */
342     { OP(PAT,SRC,R2_MASKPEN) },                                     /* 0xc0  P&S            */
343     { OP(SRC,DST,R2_MASKNOTPEN), OP(PAT,DST,R2_MERGEPEN),
344       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xc1  ~S^(P|(D&~S))  */
345     { OP(SRC,DST,R2_NOTMERGEPEN), OP(PAT,DST,R2_MERGEPEN),
346       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xc2  ~S^(P|~(D|S))  */
347     { OP(PAT,SRC,R2_NOTXORPEN) },                                   /* 0xc3  ~P^S           */
348     { OP(PAT,DST,R2_MERGEPENNOT), OP(SRC,DST,R2_MASKPEN) },         /* 0xc4  S&(P|~D)       */
349     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MERGEPEN),
350       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xc5  ~S^(P|(D^S))   */
351     { OP(PAT,DST,R2_MASKNOTPEN), OP(SRC,DST,R2_XORPEN) },           /* 0xc6  S^(D&~P)       */
352     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN),
353       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xc7  ~P^(S&(D|P))   */
354     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_MASKPEN) },            /* 0xc8  S&(D|P)        */
355     { OP(PAT,DST,R2_MERGEPEN), OP(SRC,DST,R2_NOTXORPEN) },          /* 0xc9  ~S^(P|D)       */
356     { OP(DST,SRC,R2_XORPEN), OP(PAT,SRC,R2_MASKPEN),
357       OP(SRC,DST,R2_XORPEN) },                                      /* 0xca  D^(P&(S^D))    */
358     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_MERGEPEN),
359       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xcb  ~S^(P|(D&S))   */
360     { OP(SRC,DST,R2_COPYPEN) },                                     /* 0xcc  S              */
361     { OP(PAT,DST,R2_NOTMERGEPEN), OP(SRC,DST,R2_MERGEPEN) },        /* 0xcd  S|~(D|P)       */
362     { OP(PAT,DST,R2_MASKNOTPEN), OP(SRC,DST,R2_MERGEPEN) },         /* 0xce  S|(D&~P)       */
363     { OP(PAT,SRC,R2_MERGENOTPEN) },                                 /* 0xcf  S|~P           */
364     { OP(SRC,DST,R2_MERGEPENNOT), OP(PAT,DST,R2_MASKPEN) },         /* 0xd0  P&(S|~D)       */
365     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN),
366       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xd1  ~P^(S|(D^P))   */
367     { OP(SRC,DST,R2_MASKNOTPEN), OP(PAT,DST,R2_XORPEN) },           /* 0xd2  P^(D&~S)       */
368     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_MASKPEN),
369       OP(SRC,DST,R2_NOTXORPEN) },                                   /* 0xd3  ~S^(P&(D|S))   */
370     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
371       OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
372       OP(TMP,DST,R2_XORPEN) },                                      /* 0xd4  S^((S^P)&(D^P))*/
373     { OP(PAT,SRC,R2_NOTMASKPEN), OP(SRC,DST,R2_NOTMASKPEN) },       /* 0xd5  ~(D&~(P&S))    */
374     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPEN),
375       OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_XORPEN),
376       OP(TMP,DST,R2_XORPEN) },                                      /* 0xd6  S^P^(D|(P&S))  */
377     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_NOTMASKPEN) },           /* 0xd7  ~(D&(P^S))     */
378     { OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
379       OP(PAT,DST,R2_XORPEN) },                                      /* 0xd8  P^(D&(S^P))    */
380     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_MASKPEN),
381       OP(SRC,DST,R2_MERGEPEN), OP(TMP,DST,R2_NOTXORPEN) },          /* 0xd9  ~S^(D|(P&S))   */
382     { OP(DST,SRC,R2_NOTMASKPEN), OP(PAT,SRC,R2_MASKPEN),
383       OP(SRC,DST,R2_XORPEN) },                                      /* 0xda  D^(P&~(S&D))   */
384     { OP(SRC,DST,R2_XORPEN), OP(PAT,SRC,R2_XORPEN),
385       OP(SRC,DST,R2_NOTMASKPEN) },                                  /* 0xdb  ~((S^P)&(S^D)) */
386     { OP(PAT,DST,R2_MASKPENNOT), OP(SRC,DST,R2_MERGEPEN) },         /* 0xdc  S|(P&~D)       */
387     { OP(SRC,DST,R2_MERGEPENNOT) },                                 /* 0xdd  S|~D           */
388     { OP(PAT,DST,R2_XORPEN), OP(SRC,DST,R2_MERGEPEN) },             /* 0xde  S|(D^P)        */
389     { OP(PAT,DST,R2_NOTMASKPEN), OP(SRC,DST,R2_MERGEPEN) },         /* 0xdf  S|~(D&P)       */
390     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_MASKPEN) },            /* 0xe0  P&(D|S)        */
391     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_NOTXORPEN) },          /* 0xe1  ~P^(D|S)       */
392     { OP(DST,TMP,R2_COPYPEN), OP(PAT,DST,R2_XORPEN),
393       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0xe2  D^(S&(P^D))    */
394     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN),
395       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xe3  ~P^(S|(D&P))   */
396     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_XORPEN),
397       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0xe4  S^(D&(P^S))    */
398     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN),
399       OP(PAT,DST,R2_NOTXORPEN) },                                   /* 0xe5  ~P^(D|(S&P))   */
400     { OP(SRC,TMP,R2_COPYPEN), OP(PAT,SRC,R2_NOTMASKPEN),
401       OP(SRC,DST,R2_MASKPEN), OP(TMP,DST,R2_XORPEN) },              /* 0xe6  S^(D&~(P&S))   */
402     { OP(PAT,SRC,R2_XORPEN), OP(PAT,DST,R2_XORPEN),
403       OP(SRC,DST,R2_NOTMASKPEN) },                                  /* 0xe7  ~((S^P)&(D^P)) */
404     { OP(SRC,TMP,R2_COPYPEN), OP(SRC,DST,R2_XORPEN),
405       OP(PAT,SRC,R2_XORPEN), OP(SRC,DST,R2_MASKPEN),
406       OP(TMP,DST,R2_XORPEN) },                                      /* 0xe8  S^((S^P)&(S^D))*/
407     { OP(DST,TMP,R2_COPYPEN), OP(SRC,DST,R2_NOTMASKPEN),
408       OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_XORPEN),
409       OP(TMP,DST,R2_NOTXORPEN) },                                   /* 0xe9  ~D^S^(P&~(S&D))*/
410     { OP(PAT,SRC,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN) },            /* 0xea  D|(P&S)        */
411     { OP(PAT,SRC,R2_NOTXORPEN), OP(SRC,DST,R2_MERGEPEN) },          /* 0xeb  D|~(P^S)       */
412     { OP(PAT,DST,R2_MASKPEN), OP(SRC,DST,R2_MERGEPEN) },            /* 0xec  S|(D&P)        */
413     { OP(PAT,DST,R2_NOTXORPEN), OP(SRC,DST,R2_MERGEPEN) },          /* 0xed  S|~(D^P)       */
414     { OP(SRC,DST,R2_MERGEPEN) },                                    /* 0xee  S|D            */
415     { OP(PAT,DST,R2_MERGENOTPEN), OP(SRC,DST,R2_MERGEPEN) },        /* 0xef  S|D|~P         */
416     { OP(PAT,DST,R2_COPYPEN) },                                     /* 0xf0  P              */
417     { OP(SRC,DST,R2_NOTMERGEPEN), OP(PAT,DST,R2_MERGEPEN) },        /* 0xf1  P|~(D|S)       */
418     { OP(SRC,DST,R2_MASKNOTPEN), OP(PAT,DST,R2_MERGEPEN) },         /* 0xf2  P|(D&~S)       */
419     { OP(PAT,SRC,R2_MERGEPENNOT) },                                 /* 0xf3  P|~S           */
420     { OP(SRC,DST,R2_MASKPENNOT), OP(PAT,DST,R2_MERGEPEN) },         /* 0xf4  P|(S&~D)       */
421     { OP(PAT,DST,R2_MERGEPENNOT) },                                 /* 0xf5  P|~D           */
422     { OP(SRC,DST,R2_XORPEN), OP(PAT,DST,R2_MERGEPEN) },             /* 0xf6  P|(D^S)        */
423     { OP(SRC,DST,R2_NOTMASKPEN), OP(PAT,DST,R2_MERGEPEN) },         /* 0xf7  P|~(S&D)       */
424     { OP(SRC,DST,R2_MASKPEN), OP(PAT,DST,R2_MERGEPEN) },            /* 0xf8  P|(D&S)        */
425     { OP(SRC,DST,R2_NOTXORPEN), OP(PAT,DST,R2_MERGEPEN) },          /* 0xf9  P|~(D^S)       */
426     { OP(PAT,DST,R2_MERGEPEN) },                                    /* 0xfa  D|P            */
427     { OP(PAT,SRC,R2_MERGEPENNOT), OP(SRC,DST,R2_MERGEPEN) },        /* 0xfb  D|P|~S         */
428     { OP(PAT,SRC,R2_MERGEPEN) },                                    /* 0xfc  P|S            */
429     { OP(SRC,DST,R2_MERGEPENNOT), OP(PAT,DST,R2_MERGEPEN) },        /* 0xfd  P|S|~D         */
430     { OP(SRC,DST,R2_MERGEPEN), OP(PAT,DST,R2_MERGEPEN) },           /* 0xfe  P|D|S          */
431     { OP(PAT,DST,R2_WHITE) }                                        /* 0xff  1              */
432 };
433
434 static int get_overlap( const dib_info *dst, const RECT *dst_rect,
435                         const dib_info *src, const RECT *src_rect )
436 {
437     const char *src_top, *dst_top;
438     int height, ret = 0;
439
440     if (dst->stride != src->stride) return 0;  /* can't be the same dib */
441     if (dst_rect->right <= src_rect->left) return 0;
442     if (dst_rect->left >= src_rect->right) return 0;
443
444     src_top = (const char *)src->bits.ptr + src_rect->top * src->stride;
445     dst_top = (const char *)dst->bits.ptr + dst_rect->top * dst->stride;
446     height = (dst_rect->bottom - dst_rect->top) * dst->stride;
447
448     if (dst->stride > 0)
449     {
450         if (src_top >= dst_top + height) return 0;
451         if (src_top + height <= dst_top) return 0;
452         if (dst_top < src_top) ret |= OVERLAP_ABOVE;
453         else if (dst_top > src_top) ret |= OVERLAP_BELOW;
454     }
455     else
456     {
457         if (src_top <= dst_top + height) return 0;
458         if (src_top + height >= dst_top) return 0;
459         if (dst_top > src_top) ret |= OVERLAP_ABOVE;
460         else if (dst_top < src_top) ret |= OVERLAP_BELOW;
461     }
462
463     if (dst_rect->left < src_rect->left) ret |= OVERLAP_LEFT;
464     else if (dst_rect->left > src_rect->left) ret |= OVERLAP_RIGHT;
465
466     return ret;
467 }
468
469 static DWORD copy_rect( dib_info *dst, const RECT *dst_rect, const dib_info *src, const RECT *src_rect,
470                         HRGN clip, INT rop2 )
471 {
472     POINT origin;
473     RECT clipped_rect;
474     const WINEREGION *clip_data;
475     int i, start, end, overlap;
476     DWORD and = 0, xor = 0;
477
478     switch (rop2)
479     {
480     case R2_NOT:   and = ~0u;
481         /* fall through */
482     case R2_WHITE: xor = ~0u;
483         /* fall through */
484     case R2_BLACK:
485         if (clip)
486         {
487             clip_data = get_wine_region( clip );
488             for (i = 0; i < clip_data->numRects; i++)
489                 if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
490                     dst->funcs->solid_rects( dst, 1, &clipped_rect, and, xor );
491             release_wine_region( clip );
492         }
493         else dst->funcs->solid_rects( dst, 1, dst_rect, and, xor );
494         /* fall through */
495     case R2_NOP:
496         return ERROR_SUCCESS;
497     }
498
499     origin.x = src_rect->left;
500     origin.y = src_rect->top;
501     overlap = get_overlap( dst, dst_rect, src, src_rect );
502
503     if (clip == NULL) dst->funcs->copy_rect( dst, dst_rect, src, &origin, rop2, overlap );
504     else
505     {
506         clip_data = get_wine_region( clip );
507         if (overlap & OVERLAP_BELOW)
508         {
509             if (overlap & OVERLAP_RIGHT)  /* right to left, bottom to top */
510             {
511                 for (i = clip_data->numRects - 1; i >= 0; i--)
512                 {
513                     if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
514                     {
515                         origin.x = src_rect->left + clipped_rect.left - dst_rect->left;
516                         origin.y = src_rect->top  + clipped_rect.top  - dst_rect->top;
517                         dst->funcs->copy_rect( dst, &clipped_rect, src, &origin, rop2, overlap );
518                     }
519                 }
520             }
521             else  /* left to right, bottom to top */
522             {
523                 for (start = clip_data->numRects - 1; start >= 0; start = end)
524                 {
525                     for (end = start - 1; end >= 0; end--)
526                         if (clip_data->rects[start].top != clip_data->rects[end].top) break;
527
528                     for (i = end + 1; i <= start; i++)
529                     {
530                         if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
531                         {
532                             origin.x = src_rect->left + clipped_rect.left - dst_rect->left;
533                             origin.y = src_rect->top  + clipped_rect.top  - dst_rect->top;
534                             dst->funcs->copy_rect( dst, &clipped_rect, src, &origin, rop2, overlap );
535                         }
536                     }
537                 }
538             }
539         }
540         else if (overlap & OVERLAP_RIGHT)  /* right to left, top to bottom */
541         {
542             for (start = 0; start < clip_data->numRects; start = end)
543             {
544                 for (end = start + 1; end < clip_data->numRects; end++)
545                     if (clip_data->rects[start].top != clip_data->rects[end].top) break;
546
547                 for (i = end - 1; i >= start; i--)
548                 {
549                     if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
550                     {
551                         origin.x = src_rect->left + clipped_rect.left - dst_rect->left;
552                         origin.y = src_rect->top  + clipped_rect.top  - dst_rect->top;
553                         dst->funcs->copy_rect( dst, &clipped_rect, src, &origin, rop2, overlap );
554                     }
555                 }
556             }
557         }
558         else  /* left to right, top to bottom */
559         {
560             for (i = 0; i < clip_data->numRects; i++)
561             {
562                 if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
563                 {
564                     origin.x = src_rect->left + clipped_rect.left - dst_rect->left;
565                     origin.y = src_rect->top  + clipped_rect.top  - dst_rect->top;
566                     dst->funcs->copy_rect( dst, &clipped_rect, src, &origin, rop2, overlap );
567                 }
568             }
569         }
570         release_wine_region( clip );
571     }
572     return ERROR_SUCCESS;
573 }
574
575 static DWORD blend_rect( dib_info *dst, const RECT *dst_rect, const dib_info *src, const RECT *src_rect,
576                          HRGN clip, BLENDFUNCTION blend )
577 {
578     POINT origin;
579     RECT clipped_rect;
580     const WINEREGION *clip_data;
581     int i;
582
583     origin.x = src_rect->left;
584     origin.y = src_rect->top;
585
586     if (clip == NULL) dst->funcs->blend_rect( dst, dst_rect, src, &origin, blend );
587     else
588     {
589         clip_data = get_wine_region( clip );
590         for (i = 0; i < clip_data->numRects; i++)
591         {
592             if (intersect_rect( &clipped_rect, dst_rect, clip_data->rects + i ))
593             {
594                 origin.x = src_rect->left + clipped_rect.left - dst_rect->left;
595                 origin.y = src_rect->top  + clipped_rect.top  - dst_rect->top;
596                 dst->funcs->blend_rect( dst, &clipped_rect, src, &origin, blend );
597             }
598         }
599         release_wine_region( clip );
600     }
601     return ERROR_SUCCESS;
602 }
603
604 static void gradient_rect( dib_info *dib, TRIVERTEX *v, int mode, HRGN clip )
605 {
606     int i;
607     RECT rect, clipped_rect;
608
609     rect.left   = max( v[0].x, 0 );
610     rect.top    = max( v[0].y, 0 );
611     rect.right  = min( v[1].x, dib->width );
612     rect.bottom = min( v[1].y, dib->height );
613
614     if (clip)
615     {
616         const WINEREGION *clip_data = get_wine_region( clip );
617
618         for (i = 0; i < clip_data->numRects; i++)
619         {
620             if (intersect_rect( &clipped_rect, &rect, clip_data->rects + i ))
621                 dib->funcs->gradient_rect( dib, &clipped_rect, v, mode );
622         }
623         release_wine_region( clip );
624     }
625     else dib->funcs->gradient_rect( dib, &rect, v, mode );
626 }
627
628 static DWORD copy_src_bits( dib_info *src, RECT *src_rect )
629 {
630     int y, stride = get_dib_stride( src->width, src->bit_count );
631     int height = src_rect->bottom - src_rect->top;
632     void *ptr = HeapAlloc( GetProcessHeap(), 0, stride * height );
633
634     if (!ptr) return ERROR_OUTOFMEMORY;
635
636     for (y = 0; y < height; y++)
637         memcpy( (char *)ptr + y * stride,
638                 (char *)src->bits.ptr + (src_rect->top + y) * src->stride, stride );
639     src->stride = stride;
640     src->height = height;
641     if (src->bits.free) src->bits.free( &src->bits );
642     src->bits.is_copy = TRUE;
643     src->bits.ptr = ptr;
644     src->bits.free = free_heap_bits;
645     src->bits.param = NULL;
646
647     offset_rect( src_rect, 0, -src_rect->top );
648     return ERROR_SUCCESS;
649 }
650
651 static DWORD create_tmp_dib( const dib_info *copy, int width, int height, dib_info *ret )
652 {
653     copy_dib_color_info( ret, copy );
654     ret->width = width;
655     ret->height = height;
656     ret->stride = get_dib_stride( width, ret->bit_count );
657     ret->bits.ptr = HeapAlloc( GetProcessHeap(), 0, ret->height * ret->stride );
658     ret->bits.is_copy = TRUE;
659     ret->bits.free = free_heap_bits;
660     ret->bits.param = NULL;
661
662     return ret->bits.ptr ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
663 }
664
665 static DWORD execute_rop( dibdrv_physdev *pdev, const RECT *dst_rect, dib_info *src,
666                           const RECT *src_rect, HRGN clip, DWORD rop )
667 {
668     dib_info *dibs[3], *result = src, tmp;
669     RECT rects[3];
670     int width  = dst_rect->right - dst_rect->left;
671     int height = dst_rect->bottom - dst_rect->top;
672     const BYTE *opcode = BITBLT_Opcodes[(rop >> 16) & 0xff];
673     DWORD err;
674
675     dibs[SRC] = src;
676     dibs[DST] = &pdev->dib;
677     dibs[TMP] = 0;
678
679     rects[SRC] = *src_rect;
680     rects[DST] = *dst_rect;
681     rects[TMP] = *dst_rect;
682     offset_rect( &rects[TMP], -rects[TMP].left, -rects[TMP].top );
683
684     for ( ; *opcode; opcode++)
685     {
686         if (OP_DST(*opcode) == DST) result = dibs[DST];
687         if (OP_DST(*opcode) == SRC && src->bits.is_copy == FALSE)
688         {
689             err = copy_src_bits( src, &rects[SRC] );
690             if (err) return err;
691         }
692         if (OP_DST(*opcode) == TMP && !dibs[TMP])
693         {
694             err = create_tmp_dib( &pdev->dib, width, height, &tmp );
695             if (err) return err;
696             dibs[TMP] = &tmp;
697         }
698
699         switch(OP_SRCDST(*opcode))
700         {
701         case OP_ARGS(DST,TMP):
702         case OP_ARGS(SRC,TMP):
703         case OP_ARGS(DST,SRC):
704         case OP_ARGS(SRC,DST):
705         case OP_ARGS(TMP,SRC):
706         case OP_ARGS(TMP,DST):
707             copy_rect( dibs[OP_DST(*opcode)], &rects[OP_DST(*opcode)],
708                        dibs[OP_SRC(*opcode)], &rects[OP_SRC(*opcode)],
709                        OP_DST(*opcode) == DST ? clip : NULL, OP_ROP(*opcode) );
710             break;
711         case OP_ARGS(PAT,DST):
712         case OP_ARGS(PAT,SRC):
713             update_brush_rop( pdev, OP_ROP(*opcode) );
714             pdev->brush_rects( pdev, dibs[OP_DST(*opcode)], 1, &rects[OP_DST(*opcode)],
715                                OP_DST(*opcode) == DST ? clip : NULL );
716             update_brush_rop( pdev, GetROP2(pdev->dev.hdc) );
717             break;
718         }
719     }
720
721     if (dibs[TMP]) free_dib_info( dibs[TMP] );
722
723     if (result == src) copy_rect( dibs[DST], &rects[DST], dibs[SRC], &rects[SRC], clip, R2_COPYPEN );
724
725     return ERROR_SUCCESS;
726 }
727
728 static void set_color_info( const dib_info *dib, BITMAPINFO *info )
729 {
730     DWORD *masks = (DWORD *)info->bmiColors;
731
732     info->bmiHeader.biCompression = BI_RGB;
733     info->bmiHeader.biClrUsed     = 0;
734
735     switch (info->bmiHeader.biBitCount)
736     {
737     case 1:
738     case 4:
739     case 8:
740         if (dib->color_table)
741         {
742             info->bmiHeader.biClrUsed = min( dib->color_table_size, 1 << dib->bit_count );
743             memcpy( info->bmiColors, dib->color_table,
744                     info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
745         }
746         break;
747     case 16:
748         masks[0] = dib->red_mask;
749         masks[1] = dib->green_mask;
750         masks[2] = dib->blue_mask;
751         info->bmiHeader.biCompression = BI_BITFIELDS;
752         break;
753     case 32:
754         if (dib->funcs != &funcs_8888)
755         {
756             masks[0] = dib->red_mask;
757             masks[1] = dib->green_mask;
758             masks[2] = dib->blue_mask;
759             info->bmiHeader.biCompression = BI_BITFIELDS;
760         }
761         break;
762     }
763 }
764
765 /***********************************************************************
766  *           dibdrv_GetImage
767  */
768 DWORD dibdrv_GetImage( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *info,
769                        struct gdi_image_bits *bits, struct bitblt_coords *src )
770 {
771     DWORD ret = ERROR_SUCCESS;
772     dib_info *dib = NULL, stand_alone;
773
774     TRACE( "%p %p %p\n", dev, hbitmap, info );
775
776     info->bmiHeader.biSize          = sizeof(info->bmiHeader);
777     info->bmiHeader.biPlanes        = 1;
778     info->bmiHeader.biCompression   = BI_RGB;
779     info->bmiHeader.biXPelsPerMeter = 0;
780     info->bmiHeader.biYPelsPerMeter = 0;
781     info->bmiHeader.biClrUsed       = 0;
782     info->bmiHeader.biClrImportant  = 0;
783
784     if (hbitmap)
785     {
786         BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
787
788         if (!bmp) return ERROR_INVALID_HANDLE;
789         if (!init_dib_info_from_bitmapobj( &stand_alone, bmp, 0 ))
790         {
791             ret = ERROR_BAD_FORMAT;
792             goto done;
793         }
794         dib = &stand_alone;
795     }
796     else
797     {
798         dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
799         dib = &pdev->dib;
800     }
801
802     info->bmiHeader.biWidth     = dib->width;
803     info->bmiHeader.biHeight    = dib->stride > 0 ? -dib->height : dib->height;
804     info->bmiHeader.biBitCount  = dib->bit_count;
805     info->bmiHeader.biSizeImage = dib->height * abs( dib->stride );
806
807     set_color_info( dib, info );
808
809     if (bits)
810     {
811         bits->ptr = dib->bits.ptr;
812         if (dib->stride < 0)
813             bits->ptr = (char *)bits->ptr + (dib->height - 1) * dib->stride;
814         bits->is_copy = FALSE;
815         bits->free = NULL;
816     }
817
818 done:
819    if (hbitmap)
820    {
821        if (dib) free_dib_info( dib );
822        GDI_ReleaseObj( hbitmap );
823    }
824
825    return ret;
826 }
827
828 static BOOL matching_color_info( const dib_info *dib, const BITMAPINFO *info )
829 {
830     switch (info->bmiHeader.biBitCount)
831     {
832     case 1:
833     case 4:
834     case 8:
835     {
836         RGBQUAD *color_table = (RGBQUAD *)((char *)info + info->bmiHeader.biSize);
837         if (!info->bmiHeader.biClrUsed) return FALSE;
838         if (dib->color_table_size != get_dib_num_of_colors( info )) return FALSE;
839         return !memcmp( color_table, dib->color_table, dib->color_table_size * sizeof(RGBQUAD) );
840     }
841
842     case 16:
843     {
844         DWORD *masks = (DWORD *)info->bmiColors;
845         if (info->bmiHeader.biCompression == BI_RGB) return dib->funcs == &funcs_555;
846         if (info->bmiHeader.biCompression == BI_BITFIELDS)
847             return masks[0] == dib->red_mask && masks[1] == dib->green_mask && masks[2] == dib->blue_mask;
848         break;
849     }
850
851     case 24:
852         return TRUE;
853
854     case 32:
855     {
856         DWORD *masks = (DWORD *)info->bmiColors;
857         if (info->bmiHeader.biCompression == BI_RGB) return dib->funcs == &funcs_8888;
858         if (info->bmiHeader.biCompression == BI_BITFIELDS)
859             return masks[0] == dib->red_mask && masks[1] == dib->green_mask && masks[2] == dib->blue_mask;
860         break;
861     }
862
863     }
864
865     return FALSE;
866 }
867
868 static inline BOOL rop_uses_pat(DWORD rop)
869 {
870     return ((rop >> 4) & 0x0f0000) != (rop & 0x0f0000);
871 }
872
873 /***********************************************************************
874  *           dibdrv_PutImage
875  */
876 DWORD dibdrv_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
877                        const struct gdi_image_bits *bits, struct bitblt_coords *src,
878                        struct bitblt_coords *dst, DWORD rop )
879 {
880     dib_info *dib = NULL, stand_alone;
881     DWORD ret;
882     dib_info src_dib;
883     HRGN saved_clip = NULL;
884     dibdrv_physdev *pdev = NULL;
885
886     TRACE( "%p %p %p\n", dev, hbitmap, info );
887
888     if (hbitmap)
889     {
890         BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
891
892         if (!bmp) return ERROR_INVALID_HANDLE;
893         if (!init_dib_info_from_bitmapobj( &stand_alone, bmp, 0 ))
894         {
895             ret = ERROR_BAD_FORMAT;
896             goto done;
897         }
898         dib = &stand_alone;
899         rop = SRCCOPY;
900     }
901     else
902     {
903         pdev = get_dibdrv_pdev( dev );
904         dib = &pdev->dib;
905     }
906
907     if (info->bmiHeader.biPlanes != 1) goto update_format;
908     if (info->bmiHeader.biBitCount != dib->bit_count) goto update_format;
909     if (!matching_color_info( dib, info )) goto update_format;
910     if (!bits)
911     {
912         ret = ERROR_SUCCESS;
913         goto done;
914     }
915     if ((src->width != dst->width) || (src->height != dst->height))
916     {
917         ret = ERROR_TRANSFORM_NOT_SUPPORTED;
918         goto done;
919     }
920
921     init_dib_info_from_bitmapinfo( &src_dib, info, bits->ptr, 0 );
922     src_dib.bits.is_copy = bits->is_copy;
923
924     if (!hbitmap)
925     {
926         if (clip) saved_clip = add_extra_clipping_region( pdev, clip );
927         clip = pdev->clip;
928     }
929
930     if (!rop_uses_pat( rop ))
931     {
932         int rop2 = ((rop >> 16) & 0xf) + 1;
933         ret = copy_rect( dib, &dst->visrect, &src_dib, &src->visrect, clip, rop2 );
934     }
935     else
936     {
937         ret = execute_rop( pdev, &dst->visrect, &src_dib, &src->visrect, clip, rop );
938     }
939
940     free_dib_info( &src_dib );
941
942     if (saved_clip) restore_clipping_region( pdev, saved_clip );
943
944     goto done;
945
946 update_format:
947     info->bmiHeader.biPlanes   = 1;
948     info->bmiHeader.biBitCount = dib->bit_count;
949     set_color_info( dib, info );
950     ret = ERROR_BAD_FORMAT;
951
952 done:
953     if (hbitmap)
954     {
955        if (dib) free_dib_info( dib );
956        GDI_ReleaseObj( hbitmap );
957     }
958
959     return ret;
960 }
961
962 /***********************************************************************
963  *           dibdrv_BlendImage
964  */
965 DWORD dibdrv_BlendImage( PHYSDEV dev, BITMAPINFO *info, const struct gdi_image_bits *bits,
966                          struct bitblt_coords *src, struct bitblt_coords *dst, BLENDFUNCTION blend )
967 {
968     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
969     dib_info src_dib;
970     DWORD ret;
971
972     TRACE( "%p %p\n", dev, info );
973
974     if (info->bmiHeader.biPlanes != 1) goto update_format;
975     if (info->bmiHeader.biBitCount != 32) goto update_format;
976     if (info->bmiHeader.biCompression == BI_BITFIELDS)
977     {
978         DWORD *masks = (DWORD *)info->bmiColors;
979         if (masks[0] != 0xff0000 || masks[1] != 0x00ff00 || masks[2] != 0x0000ff)
980             goto update_format;
981     }
982
983     if (!bits) return ERROR_SUCCESS;
984     if ((src->width != dst->width) || (src->height != dst->height)) return ERROR_TRANSFORM_NOT_SUPPORTED;
985
986     init_dib_info_from_bitmapinfo( &src_dib, info, bits->ptr, 0 );
987     src_dib.bits.is_copy = bits->is_copy;
988
989     ret = blend_rect( &pdev->dib, &dst->visrect, &src_dib, &src->visrect, pdev->clip, blend );
990
991     free_dib_info( &src_dib );
992     return ret;
993
994 update_format:
995     if (blend.AlphaFormat & AC_SRC_ALPHA)  /* source alpha requires A8R8G8B8 format */
996         return ERROR_INVALID_PARAMETER;
997
998     info->bmiHeader.biPlanes      = 1;
999     info->bmiHeader.biBitCount    = 32;
1000     info->bmiHeader.biCompression = BI_RGB;
1001     info->bmiHeader.biClrUsed     = 0;
1002     return ERROR_BAD_FORMAT;
1003 }
1004
1005 /****************************************************************************
1006  *               calc_1d_stretch_params   (helper for stretch_bitmapinfo)
1007  *
1008  * If one plots the dst axis vertically, the src axis horizontally, then a
1009  * 1-d stretch/shrink is rather like finding the pixels to draw a straight
1010  * line.  Following a Bresenham type argument from point (s_i, d_i) we must
1011  * pick either (s_i + 1, d_i) or (s_i + 1, d_i + 1), however the choice
1012  * depends on where the centre of the next pixel is, ie whether the
1013  * point (s_i + 3/2, d_i + 1) (the mid-point between the two centres)
1014  * lies above or below the idea line.  The differences in error between
1015  * both choices and the current pixel is the same as per Bresenham,
1016  * the only difference is the error between the zeroth and first pixel.
1017  * This is now 3 dy - 2 dx (cf 2 dy - dx for the line case).
1018  *
1019  * Here we use the Bresenham line clipper to provide the start and end points
1020  * and add the additional dy - dx error term by passing this as the bias.
1021  *
1022  */
1023 static DWORD calc_1d_stretch_params( INT dst_start, INT dst_length, INT dst_vis_start, INT dst_vis_end,
1024                                      INT src_start, INT src_length, INT src_vis_start, INT src_vis_end,
1025                                      INT *dst_clipped_start, INT *src_clipped_start,
1026                                      INT *dst_clipped_end, INT *src_clipped_end,
1027                                      struct stretch_params *stretch_params, BOOL *stretch )
1028 {
1029     bres_params bres_params;
1030     POINT start, end, clipped_start, clipped_end;
1031     RECT clip;
1032     int clip_status, m, n;
1033
1034     stretch_params->src_inc = stretch_params->dst_inc = 1;
1035
1036     bres_params.dy = abs( dst_length );
1037     bres_params.dx = abs( src_length );
1038
1039     if (bres_params.dx > bres_params.dy) bres_params.octant = 1;
1040     else bres_params.octant = 2;
1041     if (src_length < 0)
1042     {
1043         bres_params.octant = 5 - bres_params.octant;
1044         stretch_params->src_inc = -1;
1045     }
1046     if (dst_length < 0)
1047     {
1048         bres_params.octant = 9 - bres_params.octant;
1049         stretch_params->dst_inc = -1;
1050     }
1051     bres_params.octant = 1 << (bres_params.octant - 1);
1052
1053     if (bres_params.dx > bres_params.dy)
1054         bres_params.bias = bres_params.dy - bres_params.dx;
1055     else
1056         bres_params.bias = bres_params.dx - bres_params.dy;
1057
1058     start.x = src_start;
1059     start.y = dst_start;
1060     end.x   = src_start + src_length;
1061     end.y   = dst_start + dst_length;
1062
1063     clip.left   = src_vis_start;
1064     clip.right  = src_vis_end;
1065     clip.top    = dst_vis_start;
1066     clip.bottom = dst_vis_end;
1067
1068     clip_status = clip_line( &start, &end, &clip, &bres_params, &clipped_start, &clipped_end );
1069
1070     if (!clip_status) return ERROR_NO_DATA;
1071
1072     m = abs( clipped_start.x - start.x );
1073     n = abs( clipped_start.y - start.y );
1074
1075     if (bres_params.dx > bres_params.dy)
1076     {
1077         stretch_params->err_start = 3 * bres_params.dy - 2 * bres_params.dx +
1078             m * 2 * bres_params.dy - n * 2 * bres_params.dx;
1079         stretch_params->err_add_1 = 2 * bres_params.dy - 2 * bres_params.dx;
1080         stretch_params->err_add_2 = 2 * bres_params.dy;
1081         stretch_params->length = abs( clipped_end.x - clipped_start.x );
1082         *stretch = FALSE;
1083     }
1084     else
1085     {
1086         stretch_params->err_start = 3 * bres_params.dx - 2 * bres_params.dy +
1087             n * 2 * bres_params.dx - m * 2 * bres_params.dy;
1088         stretch_params->err_add_1 = 2 * bres_params.dx - 2 * bres_params.dy;
1089         stretch_params->err_add_2 = 2 * bres_params.dx;
1090         stretch_params->length = abs( clipped_end.y - clipped_start.y );
1091         *stretch = TRUE;
1092     }
1093
1094     /* The endpoint will usually have been clipped out as we don't want to touch
1095        that pixel, if it is we'll increment the length. */
1096     if (end.x != clipped_end.x || end.y != clipped_end.y)
1097     {
1098         clipped_end.x += stretch_params->src_inc;
1099         clipped_end.y += stretch_params->dst_inc;
1100         stretch_params->length++;
1101     }
1102
1103     *src_clipped_start = clipped_start.x;
1104     *dst_clipped_start = clipped_start.y;
1105     *src_clipped_end   = clipped_end.x;
1106     *dst_clipped_end   = clipped_end.y;
1107
1108     return ERROR_SUCCESS;
1109 }
1110
1111
1112 DWORD stretch_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
1113                           const BITMAPINFO *dst_info, void *dst_bits, struct bitblt_coords *dst,
1114                           INT mode )
1115 {
1116     dib_info src_dib, dst_dib;
1117     POINT dst_start, src_start, dst_end, src_end;
1118     RECT rect;
1119     BOOL hstretch, vstretch;
1120     struct stretch_params v_params, h_params;
1121     int err;
1122     DWORD ret;
1123     void (* row_fn)(const dib_info *dst_dib, const POINT *dst_start,
1124                     const dib_info *src_dib, const POINT *src_start,
1125                     const struct stretch_params *params, int mode, BOOL keep_dst);
1126
1127     TRACE("dst %d, %d - %d x %d visrect %s src %d, %d - %d x %d visrect %s\n",
1128           dst->x, dst->y, dst->width, dst->height, wine_dbgstr_rect(&dst->visrect),
1129           src->x, src->y, src->width, src->height, wine_dbgstr_rect(&src->visrect));
1130
1131     if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
1132         return ERROR_BAD_FORMAT;
1133     if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
1134         return ERROR_BAD_FORMAT;
1135
1136     /* v */
1137     ret = calc_1d_stretch_params( dst->y, dst->height, dst->visrect.top, dst->visrect.bottom,
1138                                   src->y, src->height, src->visrect.top, src->visrect.bottom,
1139                                   &dst_start.y, &src_start.y, &dst_end.y, &src_end.y,
1140                                   &v_params, &vstretch );
1141     if (ret) return ret;
1142
1143     /* h */
1144     ret = calc_1d_stretch_params( dst->x, dst->width, dst->visrect.left, dst->visrect.right,
1145                                   src->x, src->width, src->visrect.left, src->visrect.right,
1146                                   &dst_start.x, &src_start.x, &dst_end.x, &src_end.x,
1147                                   &h_params, &hstretch );
1148     if (ret) return ret;
1149
1150     TRACE("got dst start %d, %d inc %d, %d. src start %d, %d inc %d, %d len %d x %d\n",
1151           dst_start.x, dst_start.y, h_params.dst_inc, v_params.dst_inc,
1152           src_start.x, src_start.y, h_params.src_inc, v_params.src_inc,
1153           h_params.length, v_params.length);
1154
1155     get_bounding_rect( &rect, dst_start.x, dst_start.y, dst_end.x - dst_start.x, dst_end.y - dst_start.y );
1156     intersect_rect( &dst->visrect, &dst->visrect, &rect );
1157
1158     dst_start.x -= dst->visrect.left;
1159     dst_start.y -= dst->visrect.top;
1160
1161     err = v_params.err_start;
1162
1163     row_fn = hstretch ? dst_dib.funcs->stretch_row : dst_dib.funcs->shrink_row;
1164
1165     if (vstretch)
1166     {
1167         BOOL need_row = TRUE;
1168         RECT last_row, this_row;
1169         if (hstretch) mode = STRETCH_DELETESCANS;
1170         last_row.left = 0;
1171         last_row.right = dst->visrect.right - dst->visrect.left;
1172
1173         while (v_params.length--)
1174         {
1175             if (need_row)
1176             {
1177                 row_fn( &dst_dib, &dst_start, &src_dib, &src_start, &h_params, mode, FALSE );
1178                 need_row = FALSE;
1179             }
1180             else
1181             {
1182                 last_row.top = dst_start.y - v_params.dst_inc;
1183                 last_row.bottom = dst_start.y;
1184                 this_row = last_row;
1185                 offset_rect( &this_row, 0, v_params.dst_inc );
1186                 copy_rect( &dst_dib, &this_row, &dst_dib, &last_row, NULL, R2_COPYPEN );
1187             }
1188
1189             if (err > 0)
1190             {
1191                 src_start.y += v_params.src_inc;
1192                 need_row = TRUE;
1193                 err += v_params.err_add_1;
1194             }
1195             else err += v_params.err_add_2;
1196             dst_start.y += v_params.dst_inc;
1197         }
1198     }
1199     else
1200     {
1201         int merged_rows = 0;
1202         int faster_mode = mode;
1203
1204         while (v_params.length--)
1205         {
1206             if (hstretch) faster_mode = merged_rows ? mode : STRETCH_DELETESCANS;
1207             row_fn( &dst_dib, &dst_start, &src_dib, &src_start, &h_params, faster_mode, merged_rows != 0 );
1208             merged_rows++;
1209
1210             if (err > 0)
1211             {
1212                 dst_start.y += v_params.dst_inc;
1213                 merged_rows = 0;
1214                 err += v_params.err_add_1;
1215             }
1216             else err += v_params.err_add_2;
1217             src_start.y += v_params.src_inc;
1218         }
1219     }
1220
1221     /* update coordinates, the destination rectangle is always stored at 0,0 */
1222     *src = *dst;
1223     src->x -= src->visrect.left;
1224     src->y -= src->visrect.top;
1225     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
1226     return ERROR_SUCCESS;
1227 }
1228
1229 DWORD blend_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
1230                         const BITMAPINFO *dst_info, void *dst_bits, struct bitblt_coords *dst,
1231                         BLENDFUNCTION blend )
1232 {
1233     dib_info src_dib, dst_dib;
1234
1235     if (!init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
1236         return ERROR_BAD_FORMAT;
1237     if (!init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
1238         return ERROR_BAD_FORMAT;
1239
1240     return blend_rect( &dst_dib, &dst->visrect, &src_dib, &src->visrect, NULL, blend );
1241 }
1242
1243 DWORD gradient_bitmapinfo( const BITMAPINFO *info, void *bits, TRIVERTEX *v, int mode )
1244 {
1245     dib_info dib;
1246
1247     if (!init_dib_info_from_bitmapinfo( &dib, info, bits, 0 )) return ERROR_BAD_FORMAT;
1248     gradient_rect( &dib, v, mode, 0 );
1249     return ERROR_SUCCESS;
1250 }
1251
1252 /***********************************************************************
1253  *           dibdrv_StretchBlt
1254  */
1255 BOOL dibdrv_StretchBlt( PHYSDEV dst_dev, struct bitblt_coords *dst,
1256                         PHYSDEV src_dev, struct bitblt_coords *src, DWORD rop )
1257 {
1258     BOOL ret;
1259     DC *dc_dst = get_dc_ptr( dst_dev->hdc );
1260
1261     if (!dc_dst) return FALSE;
1262
1263     if (dst->width == 1 && src->width > 1) src->width--;
1264     if (dst->height == 1 && src->height > 1) src->height--;
1265
1266     ret = dc_dst->nulldrv.funcs->pStretchBlt( &dc_dst->nulldrv, dst,
1267                                               src_dev, src, rop );
1268     release_dc_ptr( dc_dst );
1269     return ret;
1270 }
1271
1272 /***********************************************************************
1273  *           dibdrv_AlphaBlend
1274  */
1275 BOOL dibdrv_AlphaBlend( PHYSDEV dst_dev, struct bitblt_coords *dst,
1276                         PHYSDEV src_dev, struct bitblt_coords *src, BLENDFUNCTION blend )
1277 {
1278     BOOL ret;
1279     DC *dc_dst = get_dc_ptr( dst_dev->hdc );
1280
1281     if (!dc_dst) return FALSE;
1282
1283     ret = dc_dst->nulldrv.funcs->pAlphaBlend( &dc_dst->nulldrv, dst, src_dev, src, blend );
1284     release_dc_ptr( dc_dst );
1285     return ret;
1286 }
1287
1288 /***********************************************************************
1289  *           dibdrv_GradientFill
1290  */
1291 BOOL dibdrv_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
1292                           void *grad_array, ULONG ngrad, ULONG mode )
1293 {
1294     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
1295     unsigned int i;
1296
1297     if (mode == GRADIENT_FILL_RECT_H || mode == GRADIENT_FILL_RECT_V)
1298     {
1299         const GRADIENT_RECT *rect = grad_array;
1300         TRIVERTEX v[2];
1301         POINT pt[2];
1302
1303         for (i = 0; i < ngrad; i++, rect++)
1304         {
1305             v[0] = vert_array[rect->UpperLeft];
1306             v[1] = vert_array[rect->LowerRight];
1307             pt[0].x = v[0].x;
1308             pt[0].y = v[0].y;
1309             pt[1].x = v[1].x;
1310             pt[1].y = v[1].y;
1311             LPtoDP( dev->hdc, pt, 2 );
1312             if (mode == GRADIENT_FILL_RECT_H)
1313             {
1314                 if (pt[1].x < pt[0].x)  /* swap the colors */
1315                 {
1316                     v[0] = vert_array[rect->LowerRight];
1317                     v[1] = vert_array[rect->UpperLeft];
1318                 }
1319             }
1320             else
1321             {
1322                 if (pt[1].y < pt[0].y)  /* swap the colors */
1323                 {
1324                     v[0] = vert_array[rect->LowerRight];
1325                     v[1] = vert_array[rect->UpperLeft];
1326                 }
1327             }
1328             v[0].x = min( pt[0].x, pt[1].x );
1329             v[0].y = min( pt[0].y, pt[1].y );
1330             v[1].x = max( pt[0].x, pt[1].x );
1331             v[1].y = max( pt[0].y, pt[1].y );
1332             if (pdev->dib.funcs == &funcs_8888 && pdev->dib.compression == BI_BITFIELDS)
1333                 v[0].Alpha = v[1].Alpha = 0;  /* Windows bug: no alpha on a8r8g8b8 created with bitfields */
1334             gradient_rect( &pdev->dib, v, mode, pdev->clip );
1335         }
1336         return TRUE;
1337     }
1338
1339     dev = GET_NEXT_PHYSDEV( dev, pGradientFill );
1340     return dev->funcs->pGradientFill( dev, vert_array, nvert, grad_array, ngrad, mode );
1341 }