Handle wParam in WM_PAINT properly: if non-null, it is the hdc we are
[wine] / dlls / ddraw / dsurface / dib.c
1 /*              DIBSection DirectDrawSurface driver
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 1998-2000 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "ddraw_private.h"
33 #include "d3d_private.h"
34 #include "dsurface/main.h"
35 #include "dsurface/dib.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38
39 /* FIXME */
40 extern HBITMAP DIB_CreateDIBSection( HDC hdc, const BITMAPINFO *bmi, UINT usage, VOID **bits,
41                                      HANDLE section, DWORD offset, DWORD ovr_pitch );
42
43 static IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable;
44
45 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
46 inline static int get_dib_width_bytes( int width, int depth )
47 {
48     int words;
49
50     switch(depth)
51     {
52     case 1:  words = (width + 31) / 32; break;
53     case 4:  words = (width + 7) / 8; break;
54     case 8:  words = (width + 3) / 4; break;
55     case 15:
56     case 16: words = (width + 1) / 2; break;
57     case 24: words = (width * 3 + 3)/4; break;
58     default:
59         WARN("(%d): Unsupported depth\n", depth );
60         /* fall through */
61     case 32: words = width; break;
62     }
63     return 4 * words;
64 }
65
66
67 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
68 {
69     BITMAPINFO* b_info;
70     UINT usage;
71     HDC ddc;
72     DIB_DirectDrawSurfaceImpl* priv = This->private;
73
74     assert(This->surface_desc.lpSurface != NULL);
75
76     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
77     {
78     case 16:
79     case 32:
80         /* Allocate extra space to store the RGB bit masks. */
81         b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
82                            sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
83         break;
84
85     case 24:
86         b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
87                            sizeof(BITMAPINFOHEADER));
88         break;
89
90     default:
91         /* Allocate extra space for a palette. */
92         b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
93                            sizeof(BITMAPINFOHEADER)
94                            + sizeof(RGBQUAD)
95                            * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
96         break;
97     }
98
99     b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
100     b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
101     b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
102     b_info->bmiHeader.biPlanes = 1;
103     b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
104
105     if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
106         && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
107         b_info->bmiHeader.biCompression = BI_RGB;
108     else
109         b_info->bmiHeader.biCompression = BI_BITFIELDS;
110
111     b_info->bmiHeader.biSizeImage
112         = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
113         * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
114
115     b_info->bmiHeader.biXPelsPerMeter = 0;
116     b_info->bmiHeader.biYPelsPerMeter = 0;
117     b_info->bmiHeader.biClrUsed = 0;
118     b_info->bmiHeader.biClrImportant = 0;
119
120     if (!This->surface_desc.u1.lPitch) {
121         /* This can't happen, right? */
122         /* or use GDI_GetObj to get it from the created DIB? */
123         This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
124         This->surface_desc.dwFlags |= DDSD_PITCH;
125     }
126     
127     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
128     {
129     case 16:
130     case 32:
131     {
132         DWORD *masks = (DWORD *) &(b_info->bmiColors);
133
134         usage = 0;
135         masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
136         masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
137         masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
138     }
139     break;
140
141     case 24:
142         /* Nothing to do */
143         usage = DIB_RGB_COLORS;
144         break;
145
146     default:
147         /* Don't know palette */
148         usage = 0;
149         break;
150     }
151
152     ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
153     if (ddc == 0)
154     {
155         HeapFree(GetProcessHeap(), 0, b_info);
156         return HRESULT_FROM_WIN32(GetLastError());
157     }
158
159     priv->dib.DIBsection
160         = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
161                                (DWORD)This->surface_desc.lpSurface,
162                                This->surface_desc.u1.lPitch);
163     DeleteDC(ddc);
164     if (!priv->dib.DIBsection) {
165         ERR("CreateDIBSection failed!\n");
166         HeapFree(GetProcessHeap(), 0, b_info);
167         return HRESULT_FROM_WIN32(GetLastError());
168     }
169
170     TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
171
172     if (!This->surface_desc.lpSurface) {
173         This->surface_desc.lpSurface = priv->dib.bitmap_data;
174         This->surface_desc.dwFlags |= DDSD_LPSURFACE;
175     }
176
177     HeapFree(GetProcessHeap(), 0, b_info);
178
179     /* I don't think it's worth checking for this. */
180     if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
181         ERR("unexpected error creating DirectDrawSurface DIB section\n");
182
183     /* this seems like a good place to put the handle for HAL driver use */
184     This->global_more.hKernelSurface = (ULONG_PTR)priv->dib.DIBsection;
185
186     return S_OK;
187 }
188
189 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
190 {
191     DIB_DirectDrawSurfaceImpl* priv = This->private;
192
193     DeleteObject(priv->dib.DIBsection);
194
195     if (!priv->dib.client_memory)
196         VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
197
198     Main_DirectDrawSurface_final_release(This);
199 }
200
201 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
202                                                 LPDIRECTDRAWSURFACE7* ppDup)
203 {
204     return DIB_DirectDrawSurface_Create(This->ddraw_owner,
205                                         &This->surface_desc, ppDup, NULL);
206 }
207
208 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
209                                         IDirectDrawImpl *pDD,
210                                         const DDSURFACEDESC2 *pDDSD)
211 {
212     HRESULT hr;
213     DIB_DirectDrawSurfaceImpl* priv = This->private;
214
215     TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
216     hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
217     if (FAILED(hr)) return hr;
218
219     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
220                         DIB_IDirectDrawSurface7_VTable);
221
222     This->final_release = DIB_DirectDrawSurface_final_release;
223     This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
224     This->flip_data = DIB_DirectDrawSurface_flip_data;
225
226     This->get_dc     = DIB_DirectDrawSurface_get_dc;
227     This->release_dc = DIB_DirectDrawSurface_release_dc;
228     This->hDC = NULL;
229
230     This->set_palette    = DIB_DirectDrawSurface_set_palette;
231     This->update_palette = DIB_DirectDrawSurface_update_palette;
232
233     TRACE("(%ldx%ld, pitch=%ld)\n",
234           This->surface_desc.dwWidth, This->surface_desc.dwHeight,
235           This->surface_desc.u1.lPitch);
236     /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
237
238     if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
239     {
240         /* "Client memory": it is managed by the application. */
241         /* XXX What if lPitch is not set? Use dwWidth or fail? */
242
243         priv->dib.client_memory = TRUE;
244     }
245     else
246     {
247         if (!(This->surface_desc.dwFlags & DDSD_PITCH))
248         {
249             int pitch = This->surface_desc.u1.lPitch;
250             if (pitch % 8 != 0)
251                 pitch += 8 - (pitch % 8);
252         }
253         /* XXX else: how should lPitch be verified? */
254
255         This->surface_desc.dwFlags |= DDSD_LPSURFACE;
256
257         /* Ensure that DDSD_PITCH is respected for DDPF_FOURCC surfaces too */
258         if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC && !(This->surface_desc.dwFlags & DDSD_PITCH)) {
259             This->surface_desc.lpSurface
260                 = VirtualAlloc(NULL, This->surface_desc.u1.dwLinearSize, MEM_COMMIT, PAGE_READWRITE);
261             This->surface_desc.dwFlags |= DDSD_LINEARSIZE;
262         } else {
263             This->surface_desc.lpSurface
264                 = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
265                            * This->surface_desc.dwHeight + 4, /* The + 4 here is for dumb games reading after the end of the surface
266                                                                  when reading the last byte / half using word access */
267                            MEM_COMMIT, PAGE_READWRITE);
268             This->surface_desc.dwFlags |= DDSD_PITCH;
269         }
270
271         if (This->surface_desc.lpSurface == NULL)
272         {
273             Main_DirectDrawSurface_final_release(This);
274             return HRESULT_FROM_WIN32(GetLastError());
275         }
276
277         priv->dib.client_memory = FALSE;
278     }
279
280     hr = create_dib(This);
281     if (FAILED(hr))
282     {
283         if (!priv->dib.client_memory)
284             VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
285
286         Main_DirectDrawSurface_final_release(This);
287
288         return hr;
289     }
290
291     return DD_OK;
292 }
293
294 /* Not an API */
295 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
296                                      const DDSURFACEDESC2 *pDDSD,
297                                      LPDIRECTDRAWSURFACE7 *ppSurf,
298                                      IUnknown *pUnkOuter)
299 {
300     IDirectDrawSurfaceImpl* This;
301     HRESULT hr;
302     assert(pUnkOuter == NULL);
303
304     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
305                      sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
306     if (This == NULL) return E_OUTOFMEMORY;
307
308     This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
309
310     hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
311     if (FAILED(hr))
312         HeapFree(GetProcessHeap(), 0, This);
313     else
314         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
315
316     return hr;
317
318 }
319
320 /* AddAttachedSurface: generic */
321 /* AddOverlayDirtyRect: generic, unimplemented */
322
323 static HRESULT _Blt_ColorFill(
324     LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
325 ) {
326     int x, y;
327     LPBYTE first;
328
329     /* Do first row */
330
331 #define COLORFILL_ROW(type) { \
332     type *d = (type *) buf; \
333     for (x = 0; x < width; x++) \
334         d[x] = (type) color; \
335     break; \
336 }
337
338     switch(bpp) {
339     case 1: COLORFILL_ROW(BYTE)
340     case 2: COLORFILL_ROW(WORD)
341     case 3: { BYTE *d = (BYTE *) buf;
342               for (x = 0; x < width; x++,d+=3) {
343                 d[0] = (color    ) & 0xFF;
344                 d[1] = (color>> 8) & 0xFF;
345                 d[2] = (color>>16) & 0xFF;
346               }
347               break;}
348     case 4: COLORFILL_ROW(DWORD)
349     default:
350         FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
351         return DDERR_UNSUPPORTED;
352     }
353
354 #undef COLORFILL_ROW
355
356     /* Now copy first row */
357     first = buf;
358     for (y = 1; y < height; y++) {
359         buf += lPitch;
360         memcpy(buf, first, width * bpp);
361     }
362     return DD_OK;
363 }
364
365 static void ComputeShifts(DWORD mask, DWORD* lshift, DWORD* rshift)
366 {
367     int pos = 0;
368     int bits = 0;
369     *lshift = 0;
370     *rshift = 0;
371     
372     if (!mask)
373         return;
374     
375     while(!(mask & (1 << pos)))
376         pos++; 
377     
378     while(mask & (1 << (pos+bits)))
379         bits++;
380     
381     *lshift = pos;
382     *rshift = 8 - bits;
383 }
384
385 /* This is used to factorize the decompression between the Blt and BltFast code */
386 static void DoDXTCDecompression(const DDSURFACEDESC2 *sdesc, const DDSURFACEDESC2 *ddesc)
387 {
388     DWORD rs,rb,rm;
389     DWORD gs,gb,gm;
390     DWORD bs,bb,bm;
391     DWORD as,ab,am;
392
393     if (!s3tc_initialized) {
394         /* FIXME: We may fake this by rendering the texture into the framebuffer using OpenGL functions and reading back
395          *        the framebuffer. This will be slow and somewhat ugly. */ 
396         FIXME("Manual S3TC decompression is not supported in native mode\n");
397         return;
398     }
399     
400     rm = ddesc->u4.ddpfPixelFormat.u2.dwRBitMask;
401     ComputeShifts(rm, &rs, &rb);
402     gm = ddesc->u4.ddpfPixelFormat.u3.dwGBitMask;
403     ComputeShifts(gm, &gs, &gb);
404     bm = ddesc->u4.ddpfPixelFormat.u4.dwBBitMask;
405     ComputeShifts(bm, &bs, &bb);
406     am = ddesc->u4.ddpfPixelFormat.u5.dwRGBAlphaBitMask;
407     ComputeShifts(am, &as, &ab);
408     if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','1')) {
409         int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
410         int pitch = ddesc->u1.lPitch;
411         int width = ddesc->dwWidth;
412         int height = ddesc->dwHeight;
413         int x,y;
414         char* dst = (char*) ddesc->lpSurface;
415         char* src = (char*) sdesc->lpSurface;
416         for (x = 0; x < width; x++)
417             for (y =0; y < height; y++) {
418                 DWORD pixel = 0;
419                 BYTE data[4];
420                 (*fetch_2d_texel_rgba_dxt1)(width, src, x, y, data);
421                 pixel = 0;
422                 pixel |= ((data[0] >> rb) << rs) & rm;
423                 pixel |= ((data[1] >> gb) << gs) & gm;
424                 pixel |= ((data[2] >> bb) << bs) & bm;
425                 pixel |= ((data[3] >> ab) << as) & am;
426                 if (is16)
427                     *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
428                 else
429                     *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
430             }
431     } else if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','3')) {
432         int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
433         int pitch = ddesc->u1.lPitch;
434         int width = ddesc->dwWidth;
435         int height = ddesc->dwHeight;
436         int x,y;
437         char* dst = (char*) ddesc->lpSurface;
438         char* src = (char*) sdesc->lpSurface;
439         for (x = 0; x < width; x++)
440             for (y =0; y < height; y++) {
441                 DWORD pixel = 0;
442                 BYTE data[4];
443                 (*fetch_2d_texel_rgba_dxt3)(width, src, x, y, data);
444                 pixel = 0;
445                 pixel |= ((data[0] >> rb) << rs) & rm;
446                 pixel |= ((data[1] >> gb) << gs) & gm;
447                 pixel |= ((data[2] >> bb) << bs) & bm;
448                 pixel |= ((data[3] >> ab) << as) & am;
449                 if (is16)
450                     *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
451                 else
452                     *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
453             }
454     } else if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','5')) {
455         int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
456         int pitch = ddesc->u1.lPitch;
457         int width = ddesc->dwWidth;
458         int height = ddesc->dwHeight;
459         int x,y;
460         char* dst = (char*) ddesc->lpSurface;
461         char* src = (char*) sdesc->lpSurface;
462         for (x = 0; x < width; x++)
463             for (y =0; y < height; y++) {
464                 DWORD pixel = 0;
465                 BYTE data[4];
466                 (*fetch_2d_texel_rgba_dxt5)(width, src, x, y, data);
467                 pixel = 0;
468                 pixel |= ((data[0] >> rb) << rs) & rm;
469                 pixel |= ((data[1] >> gb) << gs) & gm;
470                 pixel |= ((data[2] >> bb) << bs) & bm;
471                 pixel |= ((data[3] >> ab) << as) & am;
472                 if (is16)
473                     *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
474                 else
475                     *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
476             }
477     }
478 #if 0 /* Usefull for debugging */
479     {
480         static int idx;
481         char texname[255];
482         FILE* f;
483         sprintf(texname, "dxt_%d.pnm", idx++);
484         f = fopen(texname,"w");
485         DDRAW_dump_surface_to_disk(This, f, 1);
486         fclose(f);
487     }
488 #endif
489 }
490
491 HRESULT WINAPI
492 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
493                           LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
494                           DWORD dwFlags, LPDDBLTFX lpbltfx)
495 {
496     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
497     RECT                xdst,xsrc;
498     DDSURFACEDESC2      ddesc,sdesc;
499     HRESULT             ret = DD_OK;
500     int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
501     int x, y;
502     LPBYTE dbuf, sbuf;
503     
504     TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
505
506     if (TRACE_ON(ddraw)) {
507         if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
508         if (rsrc) TRACE("\tsrcrect  :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
509         TRACE("\tflags: ");
510         DDRAW_dump_DDBLT(dwFlags);
511         if (dwFlags & DDBLT_DDFX) {
512             TRACE("\tblitfx: ");
513             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
514         }
515     }
516
517     if ((This->locked) || ((src != NULL) && (((IDirectDrawSurfaceImpl *)src)->locked))) {
518         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
519         return DDERR_SURFACEBUSY;
520     }
521
522     /* First, check if the possible override function handles this case */
523     if (This->aux_blt != NULL) {
524         if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
525     }
526
527     DD_STRUCT_INIT(&ddesc);
528     DD_STRUCT_INIT(&sdesc);
529
530     sdesc.dwSize = sizeof(sdesc);
531     if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
532     ddesc.dwSize = sizeof(ddesc);
533     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
534
535     if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
536         (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
537         if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
538             FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
539             ret = DDERR_INVALIDPIXELFORMAT;
540             goto release;
541         }
542         memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
543         goto release;
544     }
545
546     if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
547         (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
548         DoDXTCDecompression(&sdesc, &ddesc);
549         goto release;
550     }
551     
552     if (rdst) {
553         memcpy(&xdst,rdst,sizeof(xdst));
554     } else {
555         xdst.top        = 0;
556         xdst.bottom     = ddesc.dwHeight;
557         xdst.left       = 0;
558         xdst.right      = ddesc.dwWidth;
559     }
560
561     if (rsrc) {
562         memcpy(&xsrc,rsrc,sizeof(xsrc));
563     } else {
564         if (src) {
565             xsrc.top    = 0;
566             xsrc.bottom = sdesc.dwHeight;
567             xsrc.left   = 0;
568             xsrc.right  = sdesc.dwWidth;
569         } else {
570             memset(&xsrc,0,sizeof(xsrc));
571         }
572     }
573
574     /* First check for the validity of source / destination rectangles. This was
575        verified using a test application + by MSDN.
576     */
577     if ((src != NULL) &&
578         ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
579          (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
580          (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
581          (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
582          (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
583         WARN("Application gave us bad source rectangle for Blt.\n");
584         return DDERR_INVALIDRECT;
585     }
586     /* For the Destination rect, it can be out of bounds on the condition that a clipper
587        is set for the given surface.
588     */
589     if ((This->clipper == NULL) &&
590         ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
591          (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
592          (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
593          (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
594          (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
595         WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
596         return DDERR_INVALIDRECT;
597     }
598     
599     /* Now handle negative values in the rectangles. Warning: only supported for now
600        in the 'simple' cases (ie not in any stretching / rotation cases).
601
602        First, the case where nothing is to be done.
603     */
604     if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
605         ((src != NULL) &&
606          ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
607     {
608         TRACE("Nothing to be done !\n");
609         goto release;
610     }
611
612     /* The easy case : the source-less blits.... */
613     if (src == NULL) {
614         RECT full_rect;
615         RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
616
617         full_rect.left   = 0;
618         full_rect.top    = 0;
619         full_rect.right  = ddesc.dwWidth;
620         full_rect.bottom = ddesc.dwHeight;
621         IntersectRect(&temp_rect, &full_rect, &xdst);
622         xdst = temp_rect;
623     } else {
624         /* Only handle clipping on the destination rectangle */
625         int clip_horiz = (xdst.left < 0) || (xdst.right  > (int) ddesc.dwWidth );
626         int clip_vert  = (xdst.top  < 0) || (xdst.bottom > (int) ddesc.dwHeight);
627         if (clip_vert || clip_horiz) {
628             /* Now check if this is a special case or not... */
629             if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
630                 (((xdst.right  - xdst.left) != (xsrc.right  - xsrc.left)) && clip_horiz) ||
631                 (dwFlags & DDBLT_DDFX)) {
632                 WARN("Out of screen rectangle in special case. Not handled right now.\n");
633                 goto release;
634             }
635
636             if (clip_horiz) {
637               if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
638               if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
639             }
640             if (clip_vert) {
641                 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
642                 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
643             }
644             /* And check if after clipping something is still to be done... */
645             if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
646                 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
647                 TRACE("Nothing to be done after clipping !\n");
648                 goto release;
649             }
650         }
651     }
652
653     bpp = GET_BPP(ddesc);
654     srcheight = xsrc.bottom - xsrc.top;
655     srcwidth = xsrc.right - xsrc.left;
656     dstheight = xdst.bottom - xdst.top;
657     dstwidth = xdst.right - xdst.left;
658     width = (xdst.right - xdst.left) * bpp;
659
660     assert(width <= ddesc.u1.lPitch);
661
662     dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
663
664     if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
665     {
666         static BOOL displayed = FALSE;
667         if (!displayed)
668         {
669             FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
670             displayed = TRUE;
671         }
672         dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
673     }
674
675     /* First, all the 'source-less' blits */
676     if (dwFlags & DDBLT_COLORFILL) {
677         ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
678                              ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
679         dwFlags &= ~DDBLT_COLORFILL;
680     }
681
682     if (dwFlags & DDBLT_DEPTHFILL)
683         FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
684     if (dwFlags & DDBLT_ROP) {
685         /* Catch some degenerate cases here */
686         switch(lpbltfx->dwROP) {
687         case BLACKNESS:
688             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
689             break;
690         case 0xAA0029: /* No-op */
691             break;
692         case WHITENESS:
693             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
694             break;
695         case SRCCOPY: /* well, we do that below ? */
696             break;
697         default:
698             FIXME("Unsupported raster op: %08lx  Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
699             goto error;
700         }
701         dwFlags &= ~DDBLT_ROP;
702     }
703     if (dwFlags & DDBLT_DDROPS) {
704         FIXME("\tDdraw Raster Ops: %08lx  Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
705     }
706     /* Now the 'with source' blits */
707     if (src) {
708         LPBYTE sbase;
709         int sx, xinc, sy, yinc;
710
711         if (!dstwidth || !dstheight) /* hmm... stupid program ? */
712             goto release;
713         sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
714         xinc = (srcwidth << 16) / dstwidth;
715         yinc = (srcheight << 16) / dstheight;
716
717         if (!dwFlags) {
718             /* No effects, we can cheat here */
719             if (dstwidth == srcwidth) {
720                 if (dstheight == srcheight) {
721                     /* No stretching in either direction. This needs to be as
722                      * fast as possible */
723                     sbuf = sbase;
724
725                     /* check for overlapping surfaces */
726                     if (src != iface || xdst.top < xsrc.top ||
727                         xdst.right <= xsrc.left || xsrc.right <= xdst.left)
728                     {
729                         /* no overlap, or dst above src, so copy from top downwards */
730                         for (y = 0; y < dstheight; y++)
731                         {
732                             memcpy(dbuf, sbuf, width);
733                             sbuf += sdesc.u1.lPitch;
734                             dbuf += ddesc.u1.lPitch;
735                         }
736                     }
737                     else if (xdst.top > xsrc.top)  /* copy from bottom upwards */
738                     {
739                         sbuf += (sdesc.u1.lPitch*dstheight);
740                         dbuf += (ddesc.u1.lPitch*dstheight);
741                         for (y = 0; y < dstheight; y++)
742                         {
743                             sbuf -= sdesc.u1.lPitch;
744                             dbuf -= ddesc.u1.lPitch;
745                             memcpy(dbuf, sbuf, width);
746                         }
747                     }
748                     else /* src and dst overlapping on the same line, use memmove */
749                     {
750                         for (y = 0; y < dstheight; y++)
751                         {
752                             memmove(dbuf, sbuf, width);
753                             sbuf += sdesc.u1.lPitch;
754                             dbuf += ddesc.u1.lPitch;
755                         }
756                     }
757                 } else {
758                     /* Stretching in Y direction only */
759                     for (y = sy = 0; y < dstheight; y++, sy += yinc) {
760                         sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
761                         memcpy(dbuf, sbuf, width);
762                         dbuf += ddesc.u1.lPitch;
763                     }
764                 }
765             } else {
766                 /* Stretching in X direction */
767                 int last_sy = -1;
768                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
769                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
770
771                     if ((sy >> 16) == (last_sy >> 16)) {
772                         /* this sourcerow is the same as last sourcerow -
773                          * copy already stretched row
774                          */
775                         memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
776                     } else {
777 #define STRETCH_ROW(type) { \
778                     type *s = (type *) sbuf, *d = (type *) dbuf; \
779                     for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
780                     d[x] = s[sx >> 16]; \
781                     break; }
782
783                     switch(bpp) {
784                     case 1: STRETCH_ROW(BYTE)
785                     case 2: STRETCH_ROW(WORD)
786                     case 4: STRETCH_ROW(DWORD)
787                     case 3: {
788                         LPBYTE s,d = dbuf;
789                         for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
790                             DWORD pixel;
791
792                             s = sbuf+3*(sx>>16);
793                             pixel = s[0]|(s[1]<<8)|(s[2]<<16);
794                             d[0] = (pixel    )&0xff;
795                             d[1] = (pixel>> 8)&0xff;
796                             d[2] = (pixel>>16)&0xff;
797                             d+=3;
798                         }
799                         break;
800                     }
801                     default:
802                         FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
803                         ret = DDERR_UNSUPPORTED;
804                         goto error;
805                     }
806 #undef STRETCH_ROW
807                     }
808                     dbuf += ddesc.u1.lPitch;
809                     last_sy = sy;
810                 }
811             }
812         } else {
813            LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
814            DWORD keylow = 0, keyhigh = 0;
815            if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
816
817               if (dwFlags & DDBLT_KEYSRC) {
818                  keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
819                  keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
820               } else if (dwFlags & DDBLT_KEYDEST){
821                  keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
822                  keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
823               } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
824                  keylow  = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
825                  keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
826               } else {
827                  keylow  = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
828                  keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
829               }
830               dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
831            }
832
833            if (dwFlags & DDBLT_DDFX)  {
834               LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
835               LONG tmpxy;
836               dTopLeft     = dbuf;
837               dTopRight    = dbuf+((dstwidth-1)*bpp);
838               dBottomLeft  = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
839               dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
840
841               if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
842                  /* I don't think we need to do anything about this flag */
843                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
844               }
845               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
846                  tmp          = dTopRight;
847                  dTopRight    = dTopLeft;
848                  dTopLeft     = tmp;
849                  tmp          = dBottomRight;
850                  dBottomRight = dBottomLeft;
851                  dBottomLeft  = tmp;
852                  dstxinc = dstxinc *-1;
853               }
854               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
855                  tmp          = dTopLeft;
856                  dTopLeft     = dBottomLeft;
857                  dBottomLeft  = tmp;
858                  tmp          = dTopRight;
859                  dTopRight    = dBottomRight;
860                  dBottomRight = tmp;
861                  dstyinc = dstyinc *-1;
862               }
863               if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
864                  /* I don't think we need to do anything about this flag */
865                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
866               }
867               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
868                  tmp          = dBottomRight;
869                  dBottomRight = dTopLeft;
870                  dTopLeft     = tmp;
871                  tmp          = dBottomLeft;
872                  dBottomLeft  = dTopRight;
873                  dTopRight    = tmp;
874                  dstxinc = dstxinc * -1;
875                  dstyinc = dstyinc * -1;
876               }
877               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
878                  tmp          = dTopLeft;
879                  dTopLeft     = dBottomLeft;
880                  dBottomLeft  = dBottomRight;
881                  dBottomRight = dTopRight;
882                  dTopRight    = tmp;
883                  tmpxy   = dstxinc;
884                  dstxinc = dstyinc;
885                  dstyinc = tmpxy;
886                  dstxinc = dstxinc * -1;
887               }
888               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
889                  tmp          = dTopLeft;
890                  dTopLeft     = dTopRight;
891                  dTopRight    = dBottomRight;
892                  dBottomRight = dBottomLeft;
893                  dBottomLeft  = tmp;
894                  tmpxy   = dstxinc;
895                  dstxinc = dstyinc;
896                  dstyinc = tmpxy;
897                  dstyinc = dstyinc * -1;
898               }
899               if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
900                  /* I don't think we need to do anything about this flag */
901                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
902               }
903               dbuf = dTopLeft;
904               dwFlags &= ~(DDBLT_DDFX);
905            }
906
907 #define COPY_COLORKEY_FX(type) { \
908             type *s, *d = (type *) dbuf, *dx, tmp; \
909             for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
910                s = (type*)(sbase + (sy >> 16) * sdesc.u1.lPitch); \
911                dx = d; \
912                for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
913                   tmp = s[sx >> 16]; \
914                   if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
915                   dx = (type*)(((LPBYTE)dx)+dstxinc); \
916                } \
917                d = (type*)(((LPBYTE)d)+dstyinc); \
918             } \
919             break; }
920
921             switch (bpp) {
922             case 1: COPY_COLORKEY_FX(BYTE)
923             case 2: COPY_COLORKEY_FX(WORD)
924             case 4: COPY_COLORKEY_FX(DWORD)
925             case 3: {LPBYTE s,d = dbuf, dx;
926                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
927                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
928                     dx = d;
929                     for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
930                         DWORD pixel;
931                         s = sbuf+3*(sx>>16);
932                         pixel = s[0]|(s[1]<<8)|(s[2]<<16);
933                         if (pixel < keylow || pixel > keyhigh){
934                             dx[0] = (pixel    )&0xff;
935                             dx[1] = (pixel>> 8)&0xff;
936                             dx[2] = (pixel>>16)&0xff;
937                         }
938                         dx+= dstxinc;
939                     }
940                     d += dstyinc;
941                 }
942                 break;}
943             default:
944                FIXME("%s color-keyed blit not implemented for bpp %d!\n",
945                   (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
946                   ret = DDERR_UNSUPPORTED;
947                   goto error;
948 #undef COPY_COLORKEY_FX
949             }
950         }
951     }
952
953 error:
954     if (dwFlags && FIXME_ON(ddraw)) {
955         FIXME("\tUnsupported flags: ");
956         DDRAW_dump_DDBLT(dwFlags);
957     }
958
959 release:
960     IDirectDrawSurface7_Unlock(iface,NULL);
961     if (src) IDirectDrawSurface7_Unlock(src,NULL);
962     return DD_OK;
963 }
964
965 /* BltBatch: generic, unimplemented */
966
967 HRESULT WINAPI
968 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
969                               DWORD dsty, LPDIRECTDRAWSURFACE7 src,
970                               LPRECT rsrc, DWORD trans)
971 {
972     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
973     int                 bpp, w, h, x, y;
974     DDSURFACEDESC2      ddesc,sdesc;
975     HRESULT             ret = DD_OK;
976     LPBYTE              sbuf, dbuf;
977     RECT                rsrc2;
978     RECT                lock_src, lock_dst;
979
980     if (TRACE_ON(ddraw)) {
981         TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
982                 This,dstx,dsty,src,rsrc,trans
983         );
984         TRACE("\ttrans:");
985         if (FIXME_ON(ddraw))
986           DDRAW_dump_DDBLTFAST(trans);
987         if (rsrc)
988           TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
989         else
990           TRACE(" srcrect: NULL\n");
991     }
992
993     if ((This->locked) || ((src != NULL) && (((IDirectDrawSurfaceImpl *)src)->locked))) {
994         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
995         return DDERR_SURFACEBUSY;
996     }
997
998     /* First, check if the possible override function handles this case */
999     if (This->aux_bltfast != NULL) {
1000         if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
1001     }
1002
1003     /* Get the surface description without locking to first compute the width / height */
1004     ddesc = This->surface_desc;
1005     sdesc = (ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src))->surface_desc;
1006
1007     if (!rsrc) {
1008         WARN("rsrc is NULL!\n");
1009         rsrc = &rsrc2;
1010         rsrc->left = rsrc->top = 0;
1011         rsrc->right = sdesc.dwWidth;
1012         rsrc->bottom = sdesc.dwHeight;
1013     }
1014
1015     /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1016     if ((rsrc->bottom > sdesc.dwHeight) || (rsrc->bottom < 0) ||
1017         (rsrc->top > sdesc.dwHeight) || (rsrc->top < 0) ||
1018         (rsrc->left > sdesc.dwWidth) || (rsrc->left < 0) ||
1019         (rsrc->right > sdesc.dwWidth) || (rsrc->right < 0) ||
1020         (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top)) {
1021         WARN("Application gave us bad source rectangle for BltFast.\n");
1022         return DDERR_INVALIDRECT;
1023     }
1024  
1025     h=rsrc->bottom-rsrc->top;
1026     if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
1027     if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
1028     if (h<=0) return DDERR_INVALIDRECT;
1029
1030     w=rsrc->right-rsrc->left;
1031     if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
1032     if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
1033     if (w<=0) return DDERR_INVALIDRECT;
1034
1035     /* Now compute the locking rectangle... */
1036     lock_src.left = rsrc->left;
1037     lock_src.top = rsrc->top;
1038     lock_src.right = lock_src.left + w;
1039     lock_src.bottom = lock_src.top + h;
1040
1041     lock_dst.left = dstx;
1042     lock_dst.top = dsty;
1043     lock_dst.right = dstx + w;
1044     lock_dst.bottom = dsty + h;
1045     
1046     /* We need to lock the surfaces, or we won't get refreshes when done. */
1047     sdesc.dwSize = sizeof(sdesc);
1048     IDirectDrawSurface7_Lock(src, &lock_src, &sdesc, DDLOCK_READONLY, 0);
1049     ddesc.dwSize = sizeof(ddesc);
1050     IDirectDrawSurface7_Lock(iface, &lock_dst, &ddesc, DDLOCK_WRITEONLY, 0);
1051
1052     /* Handle first the FOURCC surfaces... */
1053     if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) && (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
1054         if (trans)
1055             FIXME("trans arg not supported when a FOURCC surface is involved\n");
1056         if (dstx || dsty)
1057             FIXME("offset for destination surface is not supported\n");
1058         if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
1059             FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1060             ret = DDERR_INVALIDPIXELFORMAT;
1061             goto error;
1062         }
1063         memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
1064         goto error;
1065     }
1066     if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
1067         (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
1068         DoDXTCDecompression(&sdesc, &ddesc);
1069         goto error;
1070     }
1071     
1072     bpp = GET_BPP(This->surface_desc);
1073     sbuf = (BYTE *) sdesc.lpSurface;
1074     dbuf = (BYTE *) ddesc.lpSurface;
1075     
1076     if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
1077         DWORD keylow, keyhigh;
1078         if (trans & DDBLTFAST_SRCCOLORKEY) {
1079             keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
1080             keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
1081         } else {
1082             /* I'm not sure if this is correct */
1083             FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1084             keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
1085             keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
1086         }
1087
1088 #define COPYBOX_COLORKEY(type) { \
1089             type *d, *s, tmp; \
1090             s = (type *) sdesc.lpSurface; \
1091             d = (type *) ddesc.lpSurface; \
1092             for (y = 0; y < h; y++) { \
1093                 for (x = 0; x < w; x++) { \
1094                     tmp = s[x]; \
1095                     if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1096                 } \
1097                 s = (type *)((BYTE *)s + sdesc.u1.lPitch); \
1098                 d = (type *)((BYTE *)d + ddesc.u1.lPitch); \
1099             } \
1100             break; \
1101         }
1102
1103         switch (bpp) {
1104             case 1: COPYBOX_COLORKEY(BYTE)
1105             case 2: COPYBOX_COLORKEY(WORD)
1106             case 4: COPYBOX_COLORKEY(DWORD)
1107             case 3:
1108             {
1109                 BYTE *d, *s;
1110                 DWORD tmp;
1111                 s = (BYTE *) sdesc.lpSurface;
1112                 d = (BYTE *) ddesc.lpSurface;
1113                 for (y = 0; y < h; y++) {
1114                     for (x = 0; x < w * 3; x += 3) {
1115                         tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1116                         if (tmp < keylow || tmp > keyhigh) {
1117                             d[x + 0] = s[x + 0];
1118                             d[x + 1] = s[x + 1];
1119                             d[x + 2] = s[x + 2];
1120                         }
1121                     }
1122                     s += sdesc.u1.lPitch;
1123                     d += ddesc.u1.lPitch;
1124                 }
1125                 break;
1126             }
1127             default:
1128                 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1129                 ret = DDERR_UNSUPPORTED;
1130                 goto error;
1131         }
1132 #undef COPYBOX_COLORKEY
1133     } else {
1134         int width = w * bpp;
1135
1136         for (y = 0; y < h; y++) {
1137             memcpy(dbuf, sbuf, width);
1138             sbuf += sdesc.u1.lPitch;
1139             dbuf += ddesc.u1.lPitch;
1140         }
1141     }
1142     
1143 error:
1144     IDirectDrawSurface7_Unlock(iface, &lock_dst);
1145     IDirectDrawSurface7_Unlock(src, &lock_src);
1146     return ret;
1147 }
1148
1149 /* ChangeUniquenessValue: generic */
1150 /* DeleteAttachedSurface: generic */
1151 /* EnumAttachedSurfaces: generic */
1152 /* EnumOverlayZOrders: generic, unimplemented */
1153
1154 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
1155                                      IDirectDrawSurfaceImpl* back,
1156                                      DWORD dwFlags)
1157 {
1158     DIB_DirectDrawSurfaceImpl* front_priv = front->private;
1159     DIB_DirectDrawSurfaceImpl* back_priv = back->private;
1160
1161     TRACE("(%p,%p)\n",front,back);
1162
1163     {
1164         HBITMAP tmp;
1165         tmp = front_priv->dib.DIBsection;
1166         front_priv->dib.DIBsection = back_priv->dib.DIBsection;
1167         back_priv->dib.DIBsection = tmp;
1168     }
1169
1170     {
1171         void* tmp;
1172         tmp = front_priv->dib.bitmap_data;
1173         front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
1174         back_priv->dib.bitmap_data = tmp;
1175
1176         tmp = front->surface_desc.lpSurface;
1177         front->surface_desc.lpSurface = back->surface_desc.lpSurface;
1178         back->surface_desc.lpSurface = tmp;
1179     }
1180
1181     /* client_memory should not be different, but just in case */
1182     {
1183         BOOL tmp;
1184         tmp = front_priv->dib.client_memory;
1185         front_priv->dib.client_memory = back_priv->dib.client_memory;
1186         back_priv->dib.client_memory = tmp;
1187     }
1188
1189     return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
1190 }
1191
1192 /* Flip: generic */
1193 /* FreePrivateData: generic */
1194 /* GetAttachedSurface: generic */
1195 /* GetBltStatus: generic */
1196 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
1197 /* GetClipper: generic */
1198 /* GetColorKey: generic */
1199
1200 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1201 {
1202     DIB_PRIV_VAR(priv, This);
1203     HDC hDC;
1204
1205     TRACE("Grabbing a DC for surface: %p\n", This);
1206
1207     hDC = CreateCompatibleDC(0);
1208     priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
1209     if (This->palette)
1210         SelectPalette(hDC, This->palette->hpal, FALSE);
1211
1212     *phDC = hDC;
1213
1214     return S_OK;
1215 }
1216
1217 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1218 {
1219     DIB_PRIV_VAR(priv, This);
1220
1221     TRACE("Releasing DC for surface: %p\n", This);
1222
1223     SelectObject(hDC, priv->dib.holdbitmap);
1224     DeleteDC(hDC);
1225
1226     return S_OK;
1227 }
1228
1229 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1230 {
1231     return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1232 }
1233
1234 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1235 {
1236     return DIB_DirectDrawSurface_free_dc(This, hDC);
1237 }
1238
1239 /* GetDDInterface: generic */
1240 /* GetFlipStatus: generic */
1241 /* GetLOD: generic */
1242 /* GetOverlayPosition: generic */
1243 /* GetPalette: generic */
1244 /* GetPixelFormat: generic */
1245 /* GetPriority: generic */
1246 /* GetPrivateData: generic */
1247 /* GetSurfaceDesc: generic */
1248 /* GetUniquenessValue: generic */
1249 /* Initialize: generic */
1250 /* IsLost: generic */
1251 /* Lock: generic with callback? */
1252 /* PageLock: generic */
1253 /* PageUnlock: generic */
1254
1255 HRESULT WINAPI
1256 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1257 {
1258     TRACE("(%p)\n",iface);
1259     return DD_OK;       /* ??? */
1260 }
1261
1262 /* SetClipper: generic */
1263 /* SetColorKey: generic */
1264 /* SetLOD: generic */
1265 /* SetOverlayPosition: generic */
1266
1267 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1268                                        IDirectDrawPaletteImpl* pal)
1269 {
1270     if (!pal) return;
1271     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1272         This->update_palette(This, pal,
1273                              0, pal->palNumEntries,
1274                              pal->palents);
1275 }
1276
1277 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1278                                           IDirectDrawPaletteImpl* pal,
1279                                           DWORD dwStart, DWORD dwCount,
1280                                           LPPALETTEENTRY palent)
1281 {
1282     RGBQUAD col[256];
1283     unsigned int n;
1284     HDC dc;
1285
1286     TRACE("updating primary palette\n");
1287     for (n=0; n<dwCount; n++) {
1288       col[n].rgbRed   = palent[n].peRed;
1289       col[n].rgbGreen = palent[n].peGreen;
1290       col[n].rgbBlue  = palent[n].peBlue;
1291       col[n].rgbReserved = 0;
1292     }
1293     This->get_dc(This, &dc);
1294     SetDIBColorTable(dc, dwStart, dwCount, col);
1295     This->release_dc(This, dc);
1296
1297     /* Propagate change to backbuffers if there are any */
1298     /* Basically this is a modification of the Flip code to find the backbuffer */
1299     /* and duplicate the palette update there as well */
1300     if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1301         == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1302     {
1303         static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1304         LPDIRECTDRAWSURFACE7 tgt;
1305
1306         HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1307                                                             &back_caps, &tgt);
1308         if (!FAILED(hr))
1309         {
1310             IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1311                                                          IDirectDrawSurface7,tgt);
1312             IDirectDrawSurface7_Release(tgt);
1313             target->get_dc(target, &dc);
1314             SetDIBColorTable(dc, dwStart, dwCount, col);
1315             target->release_dc(target, dc);
1316         }
1317     }
1318 }
1319
1320 /* SetPalette: generic */
1321 /* SetPriority: generic */
1322 /* SetPrivateData: generic */
1323
1324 HRESULT WINAPI
1325 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1326                                      LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1327 {
1328     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1329     DIB_PRIV_VAR(priv, This);
1330     HRESULT hr = DD_OK;
1331     DWORD flags = pDDSD->dwFlags;
1332
1333     if (TRACE_ON(ddraw)) {
1334         TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1335         DDRAW_dump_surface_desc(pDDSD);
1336     }
1337
1338     if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1339         flags &= ~DDSD_PIXELFORMAT;
1340         if (flags & DDSD_LPSURFACE) {
1341             This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1342         } else {
1343             FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1344         }
1345     }
1346     if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1347         HBITMAP oldbmp = priv->dib.DIBsection;
1348         LPVOID oldsurf = This->surface_desc.lpSurface;
1349         BOOL oldc = priv->dib.client_memory;
1350
1351         flags &= ~DDSD_LPSURFACE;
1352
1353         TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1354         This->surface_desc.lpSurface = pDDSD->lpSurface;
1355         priv->dib.client_memory = TRUE;
1356
1357         hr = create_dib(This);
1358         if (FAILED(hr))
1359         {
1360             priv->dib.DIBsection = oldbmp;
1361             This->surface_desc.lpSurface = oldsurf;
1362             priv->dib.client_memory = oldc;
1363             return hr;
1364         }
1365
1366         DeleteObject(oldbmp);
1367
1368         if (!oldc)
1369             VirtualFree(oldsurf, 0, MEM_RELEASE);
1370     }
1371     if (flags) {
1372         WARN("Unhandled flags : %08lx\n", flags);
1373     }
1374     return hr;
1375 }
1376
1377 /* Unlock: ???, need callback */
1378 /* UpdateOverlay: generic */
1379 /* UpdateOverlayDisplay: generic */
1380 /* UpdateOverlayZOrder: generic */
1381
1382 static IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable =
1383 {
1384     Main_DirectDrawSurface_QueryInterface,
1385     Main_DirectDrawSurface_AddRef,
1386     Main_DirectDrawSurface_Release,
1387     Main_DirectDrawSurface_AddAttachedSurface,
1388     Main_DirectDrawSurface_AddOverlayDirtyRect,
1389     DIB_DirectDrawSurface_Blt,
1390     Main_DirectDrawSurface_BltBatch,
1391     DIB_DirectDrawSurface_BltFast,
1392     Main_DirectDrawSurface_DeleteAttachedSurface,
1393     Main_DirectDrawSurface_EnumAttachedSurfaces,
1394     Main_DirectDrawSurface_EnumOverlayZOrders,
1395     Main_DirectDrawSurface_Flip,
1396     Main_DirectDrawSurface_GetAttachedSurface,
1397     Main_DirectDrawSurface_GetBltStatus,
1398     Main_DirectDrawSurface_GetCaps,
1399     Main_DirectDrawSurface_GetClipper,
1400     Main_DirectDrawSurface_GetColorKey,
1401     Main_DirectDrawSurface_GetDC,
1402     Main_DirectDrawSurface_GetFlipStatus,
1403     Main_DirectDrawSurface_GetOverlayPosition,
1404     Main_DirectDrawSurface_GetPalette,
1405     Main_DirectDrawSurface_GetPixelFormat,
1406     Main_DirectDrawSurface_GetSurfaceDesc,
1407     Main_DirectDrawSurface_Initialize,
1408     Main_DirectDrawSurface_IsLost,
1409     Main_DirectDrawSurface_Lock,
1410     Main_DirectDrawSurface_ReleaseDC,
1411     DIB_DirectDrawSurface_Restore,
1412     Main_DirectDrawSurface_SetClipper,
1413     Main_DirectDrawSurface_SetColorKey,
1414     Main_DirectDrawSurface_SetOverlayPosition,
1415     Main_DirectDrawSurface_SetPalette,
1416     Main_DirectDrawSurface_Unlock,
1417     Main_DirectDrawSurface_UpdateOverlay,
1418     Main_DirectDrawSurface_UpdateOverlayDisplay,
1419     Main_DirectDrawSurface_UpdateOverlayZOrder,
1420     Main_DirectDrawSurface_GetDDInterface,
1421     Main_DirectDrawSurface_PageLock,
1422     Main_DirectDrawSurface_PageUnlock,
1423     DIB_DirectDrawSurface_SetSurfaceDesc,
1424     Main_DirectDrawSurface_SetPrivateData,
1425     Main_DirectDrawSurface_GetPrivateData,
1426     Main_DirectDrawSurface_FreePrivateData,
1427     Main_DirectDrawSurface_GetUniquenessValue,
1428     Main_DirectDrawSurface_ChangeUniquenessValue,
1429     Main_DirectDrawSurface_SetPriority,
1430     Main_DirectDrawSurface_GetPriority,
1431     Main_DirectDrawSurface_SetLOD,
1432     Main_DirectDrawSurface_GetLOD
1433 };