Eliminate some unnecessary direct accesses to DC internals from EMF
[wine.git] / dlls / gdi / enhmfdrv / bitblt.c
blob18a58c58477fe4931e1d6417777cb9c7b065e42b
1 /*
2 * Enhanced MetaFile driver BitBlt functions
4 * Copyright 2002 Huw D M Davies for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
22 #include <string.h>
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"
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 BOOL EMFDRV_PatBlt( PHYSDEV dev, INT left, INT top,
34 INT width, INT height, DWORD rop )
36 EMRBITBLT emr;
37 BOOL ret;
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;
65 ret = EMFDRV_WriteRecord( dev, &emr.emr );
66 if(ret)
67 EMFDRV_UpdateBBox( dev, &emr.rclBounds );
68 return ret;
71 /* Utilitarian function used by EMFDRV_BitBlt and EMFDRV_StretchBlt */
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)
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);
90 if (emrType == EMR_BITBLT)
91 emrSize = sizeof(EMRBITBLT);
92 else if (emrType == EMR_STRETCHBLT)
93 emrSize = sizeof(EMRSTRETCHBLT);
94 else
95 return FALSE;
97 GetObjectW(hBitmap, sizeof(BITMAP), &BM);
99 nBPP = BM.bmPlanes * BM.bmBitsPixel;
100 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 size = emrSize + bmiSize + bitsSize;
107 pEMR = HeapAlloc(GetProcessHeap(), 0, size);
108 if (!pEMR) return FALSE;
110 /* Initialize EMR */
111 pEMR->emr.iType = emrType;
112 pEMR->emr.nSize = size;
113 pEMR->rclBounds.left = xDst;
114 pEMR->rclBounds.top = yDst;
115 pEMR->rclBounds.right = xDst + widthDst - 1;
116 pEMR->rclBounds.bottom = yDst + heightDst - 1;
117 pEMR->xDest = xDst;
118 pEMR->yDest = yDst;
119 pEMR->cxDest = widthDst;
120 pEMR->cyDest = heightDst;
121 pEMR->dwRop = rop;
122 pEMR->xSrc = xSrc;
123 pEMR->ySrc = ySrc;
124 pEMR->xformSrc.eM11 = 1.0; /** FIXME: */
125 pEMR->xformSrc.eM12 = 0.0; /** Setting default */
126 pEMR->xformSrc.eM21 = 0.0; /** value. */
127 pEMR->xformSrc.eM22 = 1.0; /** Where should we */
128 pEMR->xformSrc.eDx = 0.0; /** get that info */
129 pEMR->xformSrc.eDy = 0.0; /** ???? */
130 pEMR->crBkColorSrc = GetBkColor(physDevSrc->hdc);
131 pEMR->iUsageSrc = DIB_RGB_COLORS;
132 pEMR->offBmiSrc = emrSize;
133 pEMR->cbBmiSrc = bmiSize;
134 pEMR->offBitsSrc = emrSize + bmiSize;
135 pEMR->cbBitsSrc = bitsSize;
136 if (emrType == EMR_STRETCHBLT)
138 PEMRSTRETCHBLT pEMRStretch = (PEMRSTRETCHBLT)pEMR;
139 pEMRStretch->cxSrc = widthSrc;
140 pEMRStretch->cySrc = heightSrc;
143 /* Initialize BITMAPINFO structure */
144 lpBmiH = (LPBITMAPINFOHEADER)((BYTE*)pEMR + pEMR->offBmiSrc);
146 lpBmiH->biSize = sizeof(BITMAPINFOHEADER);
147 lpBmiH->biWidth = BM.bmWidth;
148 lpBmiH->biHeight = BM.bmHeight;
149 lpBmiH->biPlanes = BM.bmPlanes;
150 lpBmiH->biBitCount = nBPP;
151 /* Assume the bitmap isn't compressed and set the BI_RGB flag. */
152 lpBmiH->biCompression = BI_RGB;
153 lpBmiH->biSizeImage = bitsSize;
154 lpBmiH->biYPelsPerMeter = /* 1 meter = 39.37 inch */
155 MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSX),3937,100);
156 lpBmiH->biXPelsPerMeter =
157 MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSY),3937,100);
158 lpBmiH->biClrUsed = nBPP <= 8 ? 1 << nBPP : 0;
159 /* Set biClrImportant to 0, indicating that all of the
160 device colors are important. */
161 lpBmiH->biClrImportant = 0;
163 /* Initiliaze bitmap bits */
164 if (GetDIBits(physDevSrc->hdc, hBitmap, 0, (UINT)lpBmiH->biHeight,
165 (BYTE*)pEMR + pEMR->offBitsSrc,
166 (LPBITMAPINFO)lpBmiH, DIB_RGB_COLORS))
168 ret = EMFDRV_WriteRecord(devDst, (EMR*)pEMR);
169 if (ret) EMFDRV_UpdateBBox(devDst, &(pEMR->rclBounds));
171 else
172 ret = FALSE;
174 HeapFree( GetProcessHeap(), 0, pEMR);
175 return ret;
178 BOOL EMFDRV_BitBlt(
179 PHYSDEV devDst, INT xDst, INT yDst, INT width, INT height,
180 PHYSDEV devSrc, INT xSrc, INT ySrc, DWORD rop)
182 return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, width, height,
183 devSrc, xSrc, ySrc, width, height,
184 rop, EMR_BITBLT );
187 BOOL EMFDRV_StretchBlt(
188 PHYSDEV devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,
189 PHYSDEV devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, DWORD rop )
191 return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, widthDst, heightDst,
192 devSrc, xSrc, ySrc, widthSrc, heightSrc,
193 rop, EMR_STRETCHBLT );
196 INT EMFDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
197 INT heightDst, INT xSrc, INT ySrc,
198 INT widthSrc, INT heightSrc,
199 const void *bits, const BITMAPINFO *info,
200 UINT wUsage, DWORD dwRop )
202 EMRSTRETCHDIBITS *emr;
203 BOOL ret;
204 UINT bmi_size=0, bits_size, emr_size;
206 bits_size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
207 info->bmiHeader.biHeight,
208 info->bmiHeader.biBitCount);
210 /* calculate the size of the colour table */
211 bmi_size = DIB_BitmapInfoSize(info, wUsage);
213 emr_size = sizeof (EMRSTRETCHDIBITS) + bmi_size + bits_size;
214 emr = HeapAlloc(GetProcessHeap(), 0, emr_size );
215 if (!emr) return 0;
217 /* write a bitmap info header (with colours) to the record */
218 memcpy( &emr[1], info, bmi_size);
220 /* write bitmap bits to the record */
221 memcpy ( ( (BYTE *) (&emr[1]) ) + bmi_size, bits, bits_size);
223 /* fill in the EMR header at the front of our piece of memory */
224 emr->emr.iType = EMR_STRETCHDIBITS;
225 emr->emr.nSize = emr_size;
227 emr->xDest = xDst;
228 emr->yDest = yDst;
229 emr->cxDest = widthDst;
230 emr->cyDest = heightDst;
231 emr->dwRop = dwRop;
232 emr->xSrc = xSrc; /* FIXME: only save the piece of the bitmap needed */
233 emr->ySrc = ySrc;
235 emr->iUsageSrc = wUsage;
236 emr->offBmiSrc = sizeof (EMRSTRETCHDIBITS);
237 emr->cbBmiSrc = bmi_size;
238 emr->offBitsSrc = emr->offBmiSrc + bmi_size;
239 emr->cbBitsSrc = bits_size;
241 emr->cxSrc = widthSrc;
242 emr->cySrc = heightSrc;
244 emr->rclBounds.left = xDst;
245 emr->rclBounds.top = yDst;
246 emr->rclBounds.right = xDst + widthDst;
247 emr->rclBounds.bottom = yDst + heightDst;
249 /* save the record we just created */
250 ret = EMFDRV_WriteRecord( dev, &emr->emr );
251 if(ret)
252 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
254 HeapFree(GetProcessHeap(), 0, emr);
256 return ret;
259 INT EMFDRV_SetDIBitsToDevice(
260 PHYSDEV dev, INT xDst, INT yDst, DWORD width, DWORD height,
261 INT xSrc, INT ySrc, UINT startscan, UINT lines,
262 LPCVOID bits, const BITMAPINFO *info, UINT wUsage )
264 EMRSETDIBITSTODEVICE* pEMR;
265 DWORD size, bmiSize, bitsSize;
267 bmiSize = DIB_BitmapInfoSize(info, wUsage);
268 bitsSize = DIB_GetDIBImageBytes( info->bmiHeader.biWidth,
269 info->bmiHeader.biHeight,
270 info->bmiHeader.biBitCount );
271 size = sizeof(EMRSETDIBITSTODEVICE) + bmiSize + bitsSize;
273 pEMR = HeapAlloc(GetProcessHeap(), 0, size);
274 if (!pEMR) return 0;
276 pEMR->emr.iType = EMR_SETDIBITSTODEVICE;
277 pEMR->emr.nSize = size;
278 pEMR->rclBounds.left = xDst;
279 pEMR->rclBounds.top = yDst;
280 pEMR->rclBounds.right = xDst + width - 1;
281 pEMR->rclBounds.bottom = yDst + height - 1;
282 pEMR->xDest = xDst;
283 pEMR->yDest = yDst;
284 pEMR->xSrc = xSrc;
285 pEMR->ySrc = ySrc;
286 pEMR->cxSrc = width;
287 pEMR->cySrc = height;
288 pEMR->offBmiSrc = sizeof(EMRSETDIBITSTODEVICE);
289 pEMR->cbBmiSrc = bmiSize;
290 pEMR->offBitsSrc = sizeof(EMRSETDIBITSTODEVICE) + bmiSize;
291 pEMR->cbBitsSrc = bitsSize;
292 pEMR->iUsageSrc = wUsage;
293 pEMR->iStartScan = startscan;
294 pEMR->cScans = lines;
295 memcpy((BYTE*)pEMR + pEMR->offBmiSrc, info, bmiSize);
296 memcpy((BYTE*)pEMR + pEMR->offBitsSrc, bits, bitsSize);
298 if (EMFDRV_WriteRecord(dev, (EMR*)pEMR))
299 EMFDRV_UpdateBBox(dev, &(pEMR->rclBounds));
301 HeapFree( GetProcessHeap(), 0, pEMR);
302 return lines;