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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "enhmetafiledrv.h"
29 #include "wine/debug.h"
31 BOOL EMFDRV_PatBlt( PHYSDEV dev, INT left, INT top,
32 INT width, INT height, DWORD rop )
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;
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;
63 ret = EMFDRV_WriteRecord( dev, &emr.emr );
65 EMFDRV_UpdateBBox( dev, &emr.rclBounds );
69 /* Utilitarian function used by EMFDRV_BitBlt and EMFDRV_StretchBlt */
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,
84 LPBITMAPINFOHEADER lpBmiH;
85 EMFDRV_PDEVICE* physDevSrc = (EMFDRV_PDEVICE*)devSrc;
86 HBITMAP hBitmap = GetCurrentObject(physDevSrc->hdc, OBJ_BITMAP);
88 if (emrType == EMR_BITBLT)
89 emrSize = sizeof(EMRBITBLT);
90 else if (emrType == EMR_STRETCHBLT)
91 emrSize = sizeof(EMRSTRETCHBLT);
95 if(sizeof(BITMAP) != GetObjectW(hBitmap, sizeof(BITMAP), &BM))
98 nBPP = BM.bmPlanes * BM.bmBitsPixel;
99 if(nBPP > 8) nBPP = 24; /* FIXME Can't get 16bpp to work for some reason */
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;
106 pEMR = HeapAlloc(GetProcessHeap(), 0, size);
107 if (!pEMR) return FALSE;
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;
118 pEMR->cxDest = widthDst;
119 pEMR->cyDest = heightDst;
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)
137 PEMRSTRETCHBLT pEMRStretch = (PEMRSTRETCHBLT)pEMR;
138 pEMRStretch->cxSrc = widthSrc;
139 pEMRStretch->cySrc = heightSrc;
142 /* Initialize BITMAPINFO structure */
143 lpBmiH = (LPBITMAPINFOHEADER)((BYTE*)pEMR + pEMR->offBmiSrc);
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;
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))
167 ret = EMFDRV_WriteRecord(devDst, (EMR*)pEMR);
168 if (ret) EMFDRV_UpdateBBox(devDst, &(pEMR->rclBounds));
173 HeapFree( GetProcessHeap(), 0, pEMR);
178 PHYSDEV devDst, INT xDst, INT yDst, INT width, INT height,
179 PHYSDEV devSrc, INT xSrc, INT ySrc, DWORD rop)
181 return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, width, height,
182 devSrc, xSrc, ySrc, width, height,
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 )
190 return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, widthDst, heightDst,
191 devSrc, xSrc, ySrc, widthSrc, heightSrc,
192 rop, EMR_STRETCHBLT );
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 )
201 EMRSTRETCHDIBITS *emr;
203 UINT bmi_size=0, bits_size, emr_size;
205 bits_size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
206 info->bmiHeader.biHeight,
207 info->bmiHeader.biBitCount);
209 /* calculate the size of the colour table */
210 bmi_size = DIB_BitmapInfoSize(info, wUsage);
212 emr_size = sizeof (EMRSTRETCHDIBITS) + bmi_size + bits_size;
213 emr = HeapAlloc(GetProcessHeap(), 0, emr_size );
216 /* write a bitmap info header (with colours) to the record */
217 memcpy( &emr[1], info, bmi_size);
219 /* write bitmap bits to the record */
220 memcpy ( ( (BYTE *) (&emr[1]) ) + bmi_size, bits, bits_size);
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;
228 emr->cxDest = widthDst;
229 emr->cyDest = heightDst;
231 emr->xSrc = xSrc; /* FIXME: only save the piece of the bitmap needed */
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;
240 emr->cxSrc = widthSrc;
241 emr->cySrc = heightSrc;
243 emr->rclBounds.left = xDst;
244 emr->rclBounds.top = yDst;
245 emr->rclBounds.right = xDst + widthDst;
246 emr->rclBounds.bottom = yDst + heightDst;
248 /* save the record we just created */
249 ret = EMFDRV_WriteRecord( dev, &emr->emr );
251 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
253 HeapFree(GetProcessHeap(), 0, emr);
255 return ret ? heightSrc : GDI_ERROR;
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 )
263 EMRSETDIBITSTODEVICE* pEMR;
264 DWORD size, bmiSize, bitsSize;
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;
272 pEMR = HeapAlloc(GetProcessHeap(), 0, size);
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;
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);
297 if (EMFDRV_WriteRecord(dev, (EMR*)pEMR))
298 EMFDRV_UpdateBBox(dev, &(pEMR->rclBounds));
300 HeapFree( GetProcessHeap(), 0, pEMR);