Fixed SetTextAlign(), SetBkMode(), SetROP2(), SetPolyFillMode(), and
[wine] / dlls / gdi / enhmfdrv / bitblt.c
1 /*
2  * Enhanced MetaFile driver BitBlt functions
3  *
4  * Copyright 2002 Huw D M Davies for CodeWeavers
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "gdi.h"
28 #include "enhmetafiledrv.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
32
33 BOOL EMFDRV_PatBlt( PHYSDEV dev, INT left, INT top,
34                     INT width, INT height, DWORD rop )
35 {
36     EMRBITBLT emr;
37     BOOL ret;
38
39     emr.emr.iType = EMR_BITBLT;
40     emr.emr.nSize = sizeof(emr);
41     emr.rclBounds.left = left;
42     emr.rclBounds.top = top;
43     emr.rclBounds.right = left + width - 1;
44     emr.rclBounds.bottom = top + height - 1;
45     emr.xDest = left;
46     emr.yDest = top;
47     emr.cxDest = width;
48     emr.cyDest = height;
49     emr.dwRop = rop;
50     emr.xSrc = 0;
51     emr.ySrc = 0;
52     emr.xformSrc.eM11 = 1.0;
53     emr.xformSrc.eM12 = 0.0;
54     emr.xformSrc.eM21 = 0.0;
55     emr.xformSrc.eM22 = 1.0;
56     emr.xformSrc.eDx = 0.0;
57     emr.xformSrc.eDy = 0.0;
58     emr.crBkColorSrc = 0;
59     emr.iUsageSrc = 0;
60     emr.offBmiSrc = 0;
61     emr.cbBmiSrc = 0;
62     emr.offBitsSrc = 0;
63     emr.cbBitsSrc = 0;
64
65     ret = EMFDRV_WriteRecord( dev, &emr.emr );
66     if(ret)
67         EMFDRV_UpdateBBox( dev, &emr.rclBounds );
68     return ret;
69 }
70
71 /* Utilitarian function used by EMFDRV_BitBlt and EMFDRV_StretchBlt */
72
73 static BOOL EMFDRV_BitBlockTransfer( 
74     PHYSDEV devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,  
75     PHYSDEV devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, DWORD rop,
76     DWORD emrType)
77 {
78     BOOL ret;
79     PEMRBITBLT pEMR;
80     UINT emrSize;
81     UINT bmiSize;
82     UINT bitsSize;
83     UINT size;
84     BITMAP  BM;
85     WORD nBPP;
86     LPBITMAPINFOHEADER lpBmiH;
87     EMFDRV_PDEVICE* physDevSrc = (EMFDRV_PDEVICE*)devSrc;
88     HBITMAP hBitmap = GetCurrentObject(physDevSrc->hdc, OBJ_BITMAP);
89
90     if (emrType == EMR_BITBLT)
91         emrSize = sizeof(EMRBITBLT);
92     else if (emrType == EMR_STRETCHBLT)
93         emrSize = sizeof(EMRSTRETCHBLT);
94     else
95         return FALSE;
96
97     if(sizeof(BITMAP) != GetObjectW(hBitmap, sizeof(BITMAP), &BM))
98         return FALSE;
99
100     nBPP = BM.bmPlanes * BM.bmBitsPixel;
101     if(nBPP > 8) nBPP = 24; /* FIXME Can't get 16bpp to work for some reason */
102
103     bitsSize = DIB_GetDIBWidthBytes(BM.bmWidth, nBPP) * BM.bmHeight;
104     bmiSize = sizeof(BITMAPINFOHEADER) + 
105         (nBPP <= 8 ? 1 << nBPP : 0) * sizeof(RGBQUAD);
106     size = emrSize + bmiSize + bitsSize;
107
108     pEMR = HeapAlloc(GetProcessHeap(), 0, size);
109     if (!pEMR) return FALSE;
110
111     /* Initialize EMR */
112     pEMR->emr.iType = emrType;
113     pEMR->emr.nSize = size;
114     pEMR->rclBounds.left = xDst;
115     pEMR->rclBounds.top = yDst;
116     pEMR->rclBounds.right = xDst + widthDst - 1;
117     pEMR->rclBounds.bottom = yDst + heightDst - 1;
118     pEMR->xDest = xDst;
119     pEMR->yDest = yDst;
120     pEMR->cxDest = widthDst;
121     pEMR->cyDest = heightDst;
122     pEMR->dwRop = rop;
123     pEMR->xSrc = xSrc;
124     pEMR->ySrc = ySrc;
125     pEMR->xformSrc.eM11 = 1.0;  /** FIXME:           */
126     pEMR->xformSrc.eM12 = 0.0;  /** Setting default  */
127     pEMR->xformSrc.eM21 = 0.0;  /** value.           */
128     pEMR->xformSrc.eM22 = 1.0;  /** Where should we  */
129     pEMR->xformSrc.eDx = 0.0;   /** get that info    */
130     pEMR->xformSrc.eDy = 0.0;   /** ????             */
131     pEMR->crBkColorSrc = GetBkColor(physDevSrc->hdc);
132     pEMR->iUsageSrc = DIB_RGB_COLORS;
133     pEMR->offBmiSrc = emrSize;
134     pEMR->cbBmiSrc = bmiSize;
135     pEMR->offBitsSrc = emrSize + bmiSize;
136     pEMR->cbBitsSrc = bitsSize;
137     if (emrType == EMR_STRETCHBLT) 
138     {
139         PEMRSTRETCHBLT pEMRStretch = (PEMRSTRETCHBLT)pEMR;
140         pEMRStretch->cxSrc = widthSrc;
141         pEMRStretch->cySrc = heightSrc;
142     }
143
144     /* Initialize BITMAPINFO structure */
145     lpBmiH = (LPBITMAPINFOHEADER)((BYTE*)pEMR + pEMR->offBmiSrc);
146
147     lpBmiH->biSize = sizeof(BITMAPINFOHEADER); 
148     lpBmiH->biWidth =  BM.bmWidth;
149     lpBmiH->biHeight = BM.bmHeight;
150     lpBmiH->biPlanes = BM.bmPlanes;
151     lpBmiH->biBitCount = nBPP;
152     /* Assume the bitmap isn't compressed and set the BI_RGB flag. */
153     lpBmiH->biCompression = BI_RGB;
154     lpBmiH->biSizeImage = bitsSize;
155     lpBmiH->biYPelsPerMeter = /* 1 meter  = 39.37 inch */
156         MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSX),3937,100);
157     lpBmiH->biXPelsPerMeter = 
158         MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSY),3937,100);
159     lpBmiH->biClrUsed   = nBPP <= 8 ? 1 << nBPP : 0;
160     /* Set biClrImportant to 0, indicating that all of the 
161        device colors are important. */
162     lpBmiH->biClrImportant = 0; 
163
164     /* Initiliaze bitmap bits */
165     if (GetDIBits(physDevSrc->hdc, hBitmap, 0, (UINT)lpBmiH->biHeight,
166                   (BYTE*)pEMR + pEMR->offBitsSrc,
167                   (LPBITMAPINFO)lpBmiH, DIB_RGB_COLORS))
168     {
169         ret = EMFDRV_WriteRecord(devDst, (EMR*)pEMR);
170         if (ret) EMFDRV_UpdateBBox(devDst, &(pEMR->rclBounds));
171     } 
172     else
173         ret = FALSE;
174
175     HeapFree( GetProcessHeap(), 0, pEMR);
176     return ret;
177 }
178
179 BOOL EMFDRV_BitBlt( 
180     PHYSDEV devDst, INT xDst, INT yDst, INT width, INT height,
181     PHYSDEV devSrc, INT xSrc, INT ySrc, DWORD rop)
182 {
183     return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, width, height,  
184                                     devSrc, xSrc, ySrc, width, height, 
185                                     rop, EMR_BITBLT );
186 }
187
188 BOOL EMFDRV_StretchBlt( 
189     PHYSDEV devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,  
190     PHYSDEV devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, DWORD rop )
191 {
192     return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, widthDst, heightDst,  
193                                     devSrc, xSrc, ySrc, widthSrc, heightSrc, 
194                                     rop, EMR_STRETCHBLT );
195 }
196
197 INT EMFDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
198                                       INT heightDst, INT xSrc, INT ySrc,
199                                       INT widthSrc, INT heightSrc,
200                                       const void *bits, const BITMAPINFO *info,
201                                       UINT wUsage, DWORD dwRop )
202 {
203     EMRSTRETCHDIBITS *emr;
204     BOOL ret;
205     UINT bmi_size=0, bits_size, emr_size;
206     
207     bits_size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
208                                      info->bmiHeader.biHeight,
209                                      info->bmiHeader.biBitCount);
210
211     /* calculate the size of the colour table */
212     bmi_size = DIB_BitmapInfoSize(info, wUsage);
213
214     emr_size = sizeof (EMRSTRETCHDIBITS) + bmi_size + bits_size;
215     emr = HeapAlloc(GetProcessHeap(), 0, emr_size );
216     if (!emr) return 0;
217
218     /* write a bitmap info header (with colours) to the record */
219     memcpy( &emr[1], info, bmi_size);
220
221     /* write bitmap bits to the record */
222     memcpy ( ( (BYTE *) (&emr[1]) ) + bmi_size, bits, bits_size);
223
224     /* fill in the EMR header at the front of our piece of memory */
225     emr->emr.iType = EMR_STRETCHDIBITS;
226     emr->emr.nSize = emr_size;
227
228     emr->xDest     = xDst;
229     emr->yDest     = yDst;
230     emr->cxDest    = widthDst;
231     emr->cyDest    = heightDst;
232     emr->dwRop     = dwRop;
233     emr->xSrc      = xSrc; /* FIXME: only save the piece of the bitmap needed */
234     emr->ySrc      = ySrc;
235
236     emr->iUsageSrc    = wUsage;
237     emr->offBmiSrc    = sizeof (EMRSTRETCHDIBITS);
238     emr->cbBmiSrc     = bmi_size;
239     emr->offBitsSrc   = emr->offBmiSrc + bmi_size; 
240     emr->cbBitsSrc    = bits_size;
241
242     emr->cxSrc = widthSrc;
243     emr->cySrc = heightSrc;
244
245     emr->rclBounds.left   = xDst;
246     emr->rclBounds.top    = yDst;
247     emr->rclBounds.right  = xDst + widthDst;
248     emr->rclBounds.bottom = yDst + heightDst;
249
250     /* save the record we just created */
251     ret = EMFDRV_WriteRecord( dev, &emr->emr );
252     if(ret)
253         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
254
255     HeapFree(GetProcessHeap(), 0, emr);
256
257     return ret ? heightSrc : GDI_ERROR;
258 }
259
260 INT EMFDRV_SetDIBitsToDevice( 
261     PHYSDEV dev, INT xDst, INT yDst, DWORD width, DWORD height,
262     INT xSrc, INT ySrc, UINT startscan, UINT lines,
263     LPCVOID bits, const BITMAPINFO *info, UINT wUsage )
264 {
265     EMRSETDIBITSTODEVICE* pEMR;
266     DWORD size, bmiSize, bitsSize;
267
268     bmiSize = DIB_BitmapInfoSize(info, wUsage);
269     bitsSize = DIB_GetDIBImageBytes( info->bmiHeader.biWidth,
270                                      info->bmiHeader.biHeight,
271                                      info->bmiHeader.biBitCount );
272     size = sizeof(EMRSETDIBITSTODEVICE) + bmiSize + bitsSize;
273
274     pEMR = HeapAlloc(GetProcessHeap(), 0, size);
275     if (!pEMR) return 0;
276
277     pEMR->emr.iType = EMR_SETDIBITSTODEVICE;
278     pEMR->emr.nSize = size;
279     pEMR->rclBounds.left = xDst;
280     pEMR->rclBounds.top = yDst;
281     pEMR->rclBounds.right = xDst + width - 1;
282     pEMR->rclBounds.bottom = yDst + height - 1;
283     pEMR->xDest = xDst;
284     pEMR->yDest = yDst;
285     pEMR->xSrc = xSrc;
286     pEMR->ySrc = ySrc;
287     pEMR->cxSrc = width;
288     pEMR->cySrc = height;
289     pEMR->offBmiSrc = sizeof(EMRSETDIBITSTODEVICE);
290     pEMR->cbBmiSrc = bmiSize;
291     pEMR->offBitsSrc = sizeof(EMRSETDIBITSTODEVICE) + bmiSize;
292     pEMR->cbBitsSrc = bitsSize;
293     pEMR->iUsageSrc = wUsage;
294     pEMR->iStartScan = startscan;
295     pEMR->cScans = lines;
296     memcpy((BYTE*)pEMR + pEMR->offBmiSrc, info, bmiSize);
297     memcpy((BYTE*)pEMR + pEMR->offBitsSrc, bits, bitsSize);
298
299     if (EMFDRV_WriteRecord(dev, (EMR*)pEMR))
300         EMFDRV_UpdateBBox(dev, &(pEMR->rclBounds));
301
302     HeapFree( GetProcessHeap(), 0, pEMR);
303     return lines;
304 }