dinput: BuildActionMap and SetActionMap stubs for generic joystick.
[wine] / dlls / gdi32 / 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 "enhmetafiledrv.h"
28 #include "wine/debug.h"
29
30 BOOL CDECL EMFDRV_PatBlt( PHYSDEV dev, struct bitblt_coords *dst, DWORD rop )
31 {
32     EMRBITBLT emr;
33     BOOL ret;
34
35     emr.emr.iType = EMR_BITBLT;
36     emr.emr.nSize = sizeof(emr);
37     emr.rclBounds.left = dst->log_x;
38     emr.rclBounds.top = dst->log_y;
39     emr.rclBounds.right = dst->log_x + dst->log_width - 1;
40     emr.rclBounds.bottom = dst->log_y + dst->log_height - 1;
41     emr.xDest = dst->log_x;
42     emr.yDest = dst->log_y;
43     emr.cxDest = dst->log_width;
44     emr.cyDest = dst->log_height;
45     emr.dwRop = rop;
46     emr.xSrc = 0;
47     emr.ySrc = 0;
48     emr.xformSrc.eM11 = 1.0;
49     emr.xformSrc.eM12 = 0.0;
50     emr.xformSrc.eM21 = 0.0;
51     emr.xformSrc.eM22 = 1.0;
52     emr.xformSrc.eDx = 0.0;
53     emr.xformSrc.eDy = 0.0;
54     emr.crBkColorSrc = 0;
55     emr.iUsageSrc = 0;
56     emr.offBmiSrc = 0;
57     emr.cbBmiSrc = 0;
58     emr.offBitsSrc = 0;
59     emr.cbBitsSrc = 0;
60
61     ret = EMFDRV_WriteRecord( dev, &emr.emr );
62     if(ret)
63         EMFDRV_UpdateBBox( dev, &emr.rclBounds );
64     return ret;
65 }
66
67 BOOL CDECL EMFDRV_StretchBlt( PHYSDEV devDst, struct bitblt_coords *dst,
68                               PHYSDEV devSrc, struct bitblt_coords *src, DWORD rop )
69 {
70     BOOL ret;
71     PEMRBITBLT pEMR;
72     UINT emrSize;
73     UINT bmiSize;
74     UINT bitsSize;
75     UINT size;
76     BITMAP  BM;
77     WORD nBPP = 0;
78     LPBITMAPINFOHEADER lpBmiH;
79     HBITMAP hBitmap = NULL;
80     DWORD emrType;
81
82     if (devSrc->funcs == devDst->funcs) return FALSE;  /* can't use a metafile DC as source */
83
84     if (src->log_width == dst->log_width && src->log_height == dst->log_height)
85     {
86         emrType = EMR_BITBLT;
87         emrSize = sizeof(EMRBITBLT);
88     }
89     else
90     {
91         emrType = EMR_STRETCHBLT;
92         emrSize = sizeof(EMRSTRETCHBLT);
93     }
94
95     hBitmap = GetCurrentObject(devSrc->hdc, OBJ_BITMAP);
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     bitsSize = DIB_GetDIBWidthBytes(BM.bmWidth, nBPP) * BM.bmHeight;
103     bmiSize = sizeof(BITMAPINFOHEADER) +
104         (nBPP <= 8 ? 1 << nBPP : 0) * sizeof(RGBQUAD);
105
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 = dst->log_x;
115     pEMR->rclBounds.top = dst->log_y;
116     pEMR->rclBounds.right = dst->log_x + dst->log_width - 1;
117     pEMR->rclBounds.bottom = dst->log_y + dst->log_height - 1;
118     pEMR->xDest = dst->log_x;
119     pEMR->yDest = dst->log_y;
120     pEMR->cxDest = dst->log_width;
121     pEMR->cyDest = dst->log_height;
122     pEMR->dwRop = rop;
123     pEMR->xSrc = src->log_x;
124     pEMR->ySrc = src->log_y;
125     GetWorldTransform(devSrc->hdc, &pEMR->xformSrc);
126     pEMR->crBkColorSrc = GetBkColor(devSrc->hdc);
127     pEMR->iUsageSrc = DIB_RGB_COLORS;
128     pEMR->offBmiSrc = emrSize;
129     pEMR->offBitsSrc = emrSize + bmiSize;
130     pEMR->cbBmiSrc = bmiSize;
131     pEMR->cbBitsSrc = bitsSize;
132     if (emrType == EMR_STRETCHBLT) 
133     {
134         PEMRSTRETCHBLT pEMRStretch = (PEMRSTRETCHBLT)pEMR;
135         pEMRStretch->cxSrc = src->log_width;
136         pEMRStretch->cySrc = src->log_height;
137     }
138
139     /* Initialize BITMAPINFO structure */
140     lpBmiH = (LPBITMAPINFOHEADER)((BYTE*)pEMR + pEMR->offBmiSrc);
141
142     lpBmiH->biSize = sizeof(BITMAPINFOHEADER);
143     lpBmiH->biWidth =  BM.bmWidth;
144     lpBmiH->biHeight = BM.bmHeight;
145     lpBmiH->biPlanes = BM.bmPlanes;
146     lpBmiH->biBitCount = nBPP;
147     /* Assume the bitmap isn't compressed and set the BI_RGB flag. */
148     lpBmiH->biCompression = BI_RGB;
149     lpBmiH->biSizeImage = bitsSize;
150     lpBmiH->biYPelsPerMeter = 0;
151     lpBmiH->biXPelsPerMeter = 0;
152     lpBmiH->biClrUsed   = nBPP <= 8 ? 1 << nBPP : 0;
153     /* Set biClrImportant to 0, indicating that all of the
154        device colors are important. */
155     lpBmiH->biClrImportant = 0;
156
157     /* Initialize bitmap bits */
158     if (GetDIBits(devSrc->hdc, hBitmap, 0, (UINT)lpBmiH->biHeight,
159                   (BYTE*)pEMR + pEMR->offBitsSrc,
160                   (LPBITMAPINFO)lpBmiH, DIB_RGB_COLORS))
161     {
162         ret = EMFDRV_WriteRecord(devDst, (EMR*)pEMR);
163         if (ret) EMFDRV_UpdateBBox(devDst, &(pEMR->rclBounds));
164     }
165     else
166         ret = FALSE;
167
168     HeapFree( GetProcessHeap(), 0, pEMR);
169     return ret;
170 }
171
172 INT CDECL EMFDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
173                                       INT heightDst, INT xSrc, INT ySrc,
174                                       INT widthSrc, INT heightSrc,
175                                       const void *bits, const BITMAPINFO *info,
176                                       UINT wUsage, DWORD dwRop )
177 {
178     EMRSTRETCHDIBITS *emr;
179     BOOL ret;
180     UINT bmi_size=0, bits_size, emr_size;
181     
182     bits_size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
183                                      info->bmiHeader.biHeight,
184                                      info->bmiHeader.biBitCount);
185
186     /* calculate the size of the colour table */
187     bmi_size = bitmap_info_size(info, wUsage);
188
189     emr_size = sizeof (EMRSTRETCHDIBITS) + bmi_size + bits_size;
190     emr = HeapAlloc(GetProcessHeap(), 0, emr_size );
191     if (!emr) return 0;
192
193     /* write a bitmap info header (with colours) to the record */
194     memcpy( &emr[1], info, bmi_size);
195
196     /* write bitmap bits to the record */
197     memcpy ( ( (BYTE *) (&emr[1]) ) + bmi_size, bits, bits_size);
198
199     /* fill in the EMR header at the front of our piece of memory */
200     emr->emr.iType = EMR_STRETCHDIBITS;
201     emr->emr.nSize = emr_size;
202
203     emr->xDest     = xDst;
204     emr->yDest     = yDst;
205     emr->cxDest    = widthDst;
206     emr->cyDest    = heightDst;
207     emr->dwRop     = dwRop;
208     emr->xSrc      = xSrc; /* FIXME: only save the piece of the bitmap needed */
209     emr->ySrc      = ySrc;
210
211     emr->iUsageSrc    = wUsage;
212     emr->offBmiSrc    = sizeof (EMRSTRETCHDIBITS);
213     emr->cbBmiSrc     = bmi_size;
214     emr->offBitsSrc   = emr->offBmiSrc + bmi_size; 
215     emr->cbBitsSrc    = bits_size;
216
217     emr->cxSrc = widthSrc;
218     emr->cySrc = heightSrc;
219
220     emr->rclBounds.left   = xDst;
221     emr->rclBounds.top    = yDst;
222     emr->rclBounds.right  = xDst + widthDst;
223     emr->rclBounds.bottom = yDst + heightDst;
224
225     /* save the record we just created */
226     ret = EMFDRV_WriteRecord( dev, &emr->emr );
227     if(ret)
228         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
229
230     HeapFree(GetProcessHeap(), 0, emr);
231
232     return ret ? heightSrc : GDI_ERROR;
233 }
234
235 INT CDECL EMFDRV_SetDIBitsToDevice(
236     PHYSDEV dev, INT xDst, INT yDst, DWORD width, DWORD height,
237     INT xSrc, INT ySrc, UINT startscan, UINT lines,
238     LPCVOID bits, const BITMAPINFO *info, UINT wUsage )
239 {
240     EMRSETDIBITSTODEVICE* pEMR;
241     DWORD size, bmiSize, bitsSize;
242
243     bmiSize = bitmap_info_size(info, wUsage);
244     bitsSize = DIB_GetDIBImageBytes( info->bmiHeader.biWidth,
245                                      info->bmiHeader.biHeight,
246                                      info->bmiHeader.biBitCount );
247     size = sizeof(EMRSETDIBITSTODEVICE) + bmiSize + bitsSize;
248
249     pEMR = HeapAlloc(GetProcessHeap(), 0, size);
250     if (!pEMR) return 0;
251
252     pEMR->emr.iType = EMR_SETDIBITSTODEVICE;
253     pEMR->emr.nSize = size;
254     pEMR->rclBounds.left = xDst;
255     pEMR->rclBounds.top = yDst;
256     pEMR->rclBounds.right = xDst + width - 1;
257     pEMR->rclBounds.bottom = yDst + height - 1;
258     pEMR->xDest = xDst;
259     pEMR->yDest = yDst;
260     pEMR->xSrc = xSrc;
261     pEMR->ySrc = ySrc;
262     pEMR->cxSrc = width;
263     pEMR->cySrc = height;
264     pEMR->offBmiSrc = sizeof(EMRSETDIBITSTODEVICE);
265     pEMR->cbBmiSrc = bmiSize;
266     pEMR->offBitsSrc = sizeof(EMRSETDIBITSTODEVICE) + bmiSize;
267     pEMR->cbBitsSrc = bitsSize;
268     pEMR->iUsageSrc = wUsage;
269     pEMR->iStartScan = startscan;
270     pEMR->cScans = lines;
271     memcpy((BYTE*)pEMR + pEMR->offBmiSrc, info, bmiSize);
272     memcpy((BYTE*)pEMR + pEMR->offBitsSrc, bits, bitsSize);
273
274     if (EMFDRV_WriteRecord(dev, (EMR*)pEMR))
275         EMFDRV_UpdateBBox(dev, &(pEMR->rclBounds));
276
277     HeapFree( GetProcessHeap(), 0, pEMR);
278     return lines;
279 }