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