fdopen: don't rewind the file after creating the FILE* handle. Added
[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 #include "winerror.h"
28 #include "bitmap.h"
29 #include "wine/debug.h"
30 #include "ddraw_private.h"
31 #include "dsurface/main.h"
32 #include "dsurface/dib.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
35
36 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable;
37
38 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
39 inline static int get_dib_width_bytes( int width, int depth )
40 {
41     int words;
42
43     switch(depth)
44     {
45     case 1:  words = (width + 31) / 32; break;
46     case 4:  words = (width + 7) / 8; break;
47     case 8:  words = (width + 3) / 4; break;
48     case 15:
49     case 16: words = (width + 1) / 2; break;
50     case 24: words = (width * 3 + 3)/4; break;
51     default:
52         WARN("(%d): Unsupported depth\n", depth );
53         /* fall through */
54     case 32: words = width; break;
55     }
56     return 4 * words;
57 }
58
59
60 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
61 {
62     BITMAPINFO* b_info;
63     UINT usage;
64     HDC ddc;
65     DIB_DirectDrawSurfaceImpl* priv = This->private;
66
67     assert(This->surface_desc.lpSurface != NULL);
68
69     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
70     {
71     case 16:
72     case 32:
73         /* Allocate extra space to store the RGB bit masks. */
74         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
75                                         sizeof(BITMAPINFOHEADER)
76                                         + 3 * sizeof(DWORD));
77         break;
78
79     case 24:
80         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
81                                         sizeof(BITMAPINFOHEADER));
82         break;
83
84     default:
85         /* Allocate extra space for a palette. */
86         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
87                                         sizeof(BITMAPINFOHEADER)
88                                         + sizeof(RGBQUAD)
89                                         * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
90         break;
91     }
92
93     b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
94     b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
95     b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
96     b_info->bmiHeader.biPlanes = 1;
97     b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
98
99     if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
100         && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
101         b_info->bmiHeader.biCompression = BI_RGB;
102     else
103         b_info->bmiHeader.biCompression = BI_BITFIELDS;
104
105     b_info->bmiHeader.biSizeImage
106         = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
107         * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
108
109     b_info->bmiHeader.biXPelsPerMeter = 0;
110     b_info->bmiHeader.biYPelsPerMeter = 0;
111     b_info->bmiHeader.biClrUsed = 0;
112     b_info->bmiHeader.biClrImportant = 0;
113
114     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
115     {
116     case 16:
117     case 32:
118     {
119         DWORD *masks = (DWORD *) &(b_info->bmiColors);
120
121         usage = 0;
122         masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
123         masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
124         masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
125     }
126     break;
127
128     case 24:
129         /* Nothing to do */
130         usage = DIB_RGB_COLORS;
131         break;
132
133     default:
134         /* Don't know palette */
135         usage = 0;
136         break;
137     }
138
139     ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
140     if (ddc == 0)
141     {
142         HeapFree(GetProcessHeap(), 0, b_info);
143         return HRESULT_FROM_WIN32(GetLastError());
144     }
145
146     priv->dib.DIBsection
147         = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
148                                (DWORD)This->surface_desc.lpSurface,
149                                This->surface_desc.u1.lPitch);
150     DeleteDC(ddc);
151     if (!priv->dib.DIBsection) {
152         ERR("CreateDIBSection failed!\n");
153         HeapFree(GetProcessHeap(), 0, b_info);
154         return HRESULT_FROM_WIN32(GetLastError());
155     }
156
157     TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
158     if (!This->surface_desc.u1.lPitch) {
159         /* This can't happen, right? */
160         /* or use GDI_GetObj to get it from the created DIB? */
161         This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
162         This->surface_desc.dwFlags |= DDSD_PITCH;
163     }
164
165     if (!This->surface_desc.lpSurface) {
166         This->surface_desc.lpSurface = priv->dib.bitmap_data;
167         This->surface_desc.dwFlags |= DDSD_LPSURFACE;
168     }
169
170     HeapFree(GetProcessHeap(), 0, b_info);
171
172     /* I don't think it's worth checking for this. */
173     if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
174         ERR("unexpected error creating DirectDrawSurface DIB section\n");
175
176     /* this seems like a good place to put the handle for HAL driver use */
177     This->global_more.hKernelSurface = (ULONG_PTR)priv->dib.DIBsection;
178
179     return S_OK;
180 }
181
182 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
183 {
184     DIB_DirectDrawSurfaceImpl* priv = This->private;
185
186     DeleteObject(priv->dib.DIBsection);
187
188     if (!priv->dib.client_memory)
189         VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
190
191     Main_DirectDrawSurface_final_release(This);
192 }
193
194 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
195                                                 LPDIRECTDRAWSURFACE7* ppDup)
196 {
197     return DIB_DirectDrawSurface_Create(This->ddraw_owner,
198                                         &This->surface_desc, ppDup, NULL);
199 }
200
201 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
202                                         IDirectDrawImpl *pDD,
203                                         const DDSURFACEDESC2 *pDDSD)
204 {
205     HRESULT hr;
206     DIB_DirectDrawSurfaceImpl* priv = This->private;
207
208     TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
209     hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
210     if (FAILED(hr)) return hr;
211
212     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
213                         DIB_IDirectDrawSurface7_VTable);
214
215     This->final_release = DIB_DirectDrawSurface_final_release;
216     This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
217     This->flip_data = DIB_DirectDrawSurface_flip_data;
218
219     This->get_dc     = DIB_DirectDrawSurface_get_dc;
220     This->release_dc = DIB_DirectDrawSurface_release_dc;
221     This->hDC = NULL;
222
223     This->set_palette    = DIB_DirectDrawSurface_set_palette;
224     This->update_palette = DIB_DirectDrawSurface_update_palette;
225
226     TRACE("(%ldx%ld, pitch=%ld)\n",
227           This->surface_desc.dwWidth, This->surface_desc.dwHeight,
228           This->surface_desc.u1.lPitch);
229     /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
230
231     if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
232     {
233         /* "Client memory": it is managed by the application. */
234         /* XXX What if lPitch is not set? Use dwWidth or fail? */
235
236         priv->dib.client_memory = TRUE;
237     }
238     else
239     {
240         if (!(This->surface_desc.dwFlags & DDSD_PITCH))
241         {
242             int pitch = This->surface_desc.u1.lPitch;
243             if (pitch % 8 != 0)
244                 pitch += 8 - (pitch % 8);
245         }
246         /* XXX else: how should lPitch be verified? */
247
248         This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
249
250         This->surface_desc.lpSurface
251             = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
252                            * This->surface_desc.dwHeight,
253                            MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
254
255         if (This->surface_desc.lpSurface == NULL)
256         {
257             Main_DirectDrawSurface_final_release(This);
258             return HRESULT_FROM_WIN32(GetLastError());
259         }
260
261         priv->dib.client_memory = FALSE;
262     }
263
264     hr = create_dib(This);
265     if (FAILED(hr))
266     {
267         if (!priv->dib.client_memory)
268             VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
269
270         Main_DirectDrawSurface_final_release(This);
271
272         return hr;
273     }
274
275     return DD_OK;
276 }
277
278 /* Not an API */
279 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
280                                      const DDSURFACEDESC2 *pDDSD,
281                                      LPDIRECTDRAWSURFACE7 *ppSurf,
282                                      IUnknown *pUnkOuter)
283 {
284     IDirectDrawSurfaceImpl* This;
285     HRESULT hr;
286     assert(pUnkOuter == NULL);
287
288     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
289                      sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
290     if (This == NULL) return E_OUTOFMEMORY;
291
292     This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
293
294     hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
295     if (FAILED(hr))
296         HeapFree(GetProcessHeap(), 0, This);
297     else
298         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
299
300     return hr;
301
302 }
303
304 /* AddAttachedSurface: generic */
305 /* AddOverlayDirtyRect: generic, unimplemented */
306
307 static HRESULT _Blt_ColorFill(
308     LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
309 ) {
310     int x, y;
311     LPBYTE first;
312
313     /* Do first row */
314
315 #define COLORFILL_ROW(type) { \
316     type *d = (type *) buf; \
317     for (x = 0; x < width; x++) \
318         d[x] = (type) color; \
319     break; \
320 }
321
322     switch(bpp) {
323     case 1: COLORFILL_ROW(BYTE)
324     case 2: COLORFILL_ROW(WORD)
325     case 4: COLORFILL_ROW(DWORD)
326     default:
327         FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
328         return DDERR_UNSUPPORTED;
329     }
330
331 #undef COLORFILL_ROW
332
333     /* Now copy first row */
334     first = buf;
335     for (y = 1; y < height; y++) {
336         buf += lPitch;
337         memcpy(buf, first, width * bpp);
338     }
339     return DD_OK;
340 }
341
342 HRESULT WINAPI
343 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
344                           LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
345                           DWORD dwFlags, LPDDBLTFX lpbltfx)
346 {
347     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
348     RECT                xdst,xsrc;
349     DDSURFACEDESC2      ddesc,sdesc;
350     HRESULT             ret = DD_OK;
351     int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
352     int x, y;
353     LPBYTE dbuf, sbuf;
354
355     TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
356
357     DD_STRUCT_INIT(&ddesc);
358     DD_STRUCT_INIT(&sdesc);
359
360     sdesc.dwSize = sizeof(sdesc);
361     if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
362     ddesc.dwSize = sizeof(ddesc);
363     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
364
365     if (TRACE_ON(ddraw)) {
366         if (rdst) TRACE("\tdestrect :%dx%d-%dx%d\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
367         if (rsrc) TRACE("\tsrcrect  :%dx%d-%dx%d\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
368         TRACE("\tflags: ");
369         DDRAW_dump_DDBLT(dwFlags);
370         if (dwFlags & DDBLT_DDFX) {
371             TRACE("\tblitfx: ");
372             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
373         }
374     }
375
376     if (rdst) {
377         memcpy(&xdst,rdst,sizeof(xdst));
378     } else {
379         xdst.top        = 0;
380         xdst.bottom     = ddesc.dwHeight;
381         xdst.left       = 0;
382         xdst.right      = ddesc.dwWidth;
383     }
384
385     if (rsrc) {
386         memcpy(&xsrc,rsrc,sizeof(xsrc));
387     } else {
388         if (src) {
389             xsrc.top    = 0;
390             xsrc.bottom = sdesc.dwHeight;
391             xsrc.left   = 0;
392             xsrc.right  = sdesc.dwWidth;
393         } else {
394             memset(&xsrc,0,sizeof(xsrc));
395         }
396     }
397
398     /* First check for the validity of source / destination rectangles. This was
399        verified using a test application + by MSDN.
400     */
401     if ((src != NULL) &&
402         ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
403          (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
404          (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
405          (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
406          (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
407         WARN("Application gave us bad source rectangle for Blt.\n");
408         return DDERR_INVALIDRECT;
409     }
410     /* For the Destination rect, it can be out of bounds on the condition that a clipper
411        is set for the given surface.
412     */
413     if ((This->clipper == NULL) &&
414         ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
415          (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
416          (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
417          (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
418          (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
419         WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
420         return DDERR_INVALIDRECT;
421     }
422     
423     /* Now handle negative values in the rectangles. Warning: only supported for now
424        in the 'simple' cases (ie not in any stretching / rotation cases).
425
426        First, the case where nothing is to be done.
427     */
428     if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
429         ((src != NULL) &&
430          ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
431     {
432         TRACE("Nothing to be done !\n");
433         goto release;
434     }
435
436     /* The easy case : the source-less blits.... */
437     if (src == NULL) {
438         RECT full_rect;
439         RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
440
441         full_rect.left   = 0;
442         full_rect.top    = 0;
443         full_rect.right  = ddesc.dwWidth;
444         full_rect.bottom = ddesc.dwHeight;
445         IntersectRect(&temp_rect, &full_rect, &xdst);
446         xdst = temp_rect;
447     } else {
448         /* Only handle clipping on the destination rectangle */
449         int clip_horiz = (xdst.left < 0) || (xdst.right  > (int) ddesc.dwWidth );
450         int clip_vert  = (xdst.top  < 0) || (xdst.bottom > (int) ddesc.dwHeight);
451         if (clip_vert || clip_horiz) {
452             /* Now check if this is a special case or not... */
453             if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
454                 (((xdst.right  - xdst.left) != (xsrc.right  - xsrc.left)) && clip_horiz) ||
455                 (dwFlags & DDBLT_DDFX)) {
456                 WARN("Out of screen rectangle in special case. Not handled right now.\n");
457                 goto release;
458             }
459
460             if (clip_horiz) {
461               if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
462               if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
463             }
464             if (clip_vert) {
465                 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
466                 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
467             }
468             /* And check if after clipping something is still to be done... */
469             if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
470                 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
471                 TRACE("Nothing to be done after clipping !\n");
472                 goto release;
473             }
474         }
475     }
476
477     bpp = GET_BPP(ddesc);
478     srcheight = xsrc.bottom - xsrc.top;
479     srcwidth = xsrc.right - xsrc.left;
480     dstheight = xdst.bottom - xdst.top;
481     dstwidth = xdst.right - xdst.left;
482     width = (xdst.right - xdst.left) * bpp;
483
484     assert(width <= ddesc.u1.lPitch);
485
486     dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
487
488     if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
489     {
490         static BOOL displayed = FALSE;
491         if (!displayed)
492         {
493             FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
494             displayed = TRUE;
495         }
496         dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
497     }
498
499     /* First, all the 'source-less' blits */
500     if (dwFlags & DDBLT_COLORFILL) {
501         ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
502                              ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
503         dwFlags &= ~DDBLT_COLORFILL;
504     }
505
506     if (dwFlags & DDBLT_DEPTHFILL)
507         FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
508     if (dwFlags & DDBLT_ROP) {
509         /* Catch some degenerate cases here */
510         switch(lpbltfx->dwROP) {
511         case BLACKNESS:
512             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
513             break;
514         case 0xAA0029: /* No-op */
515             break;
516         case WHITENESS:
517             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
518             break;
519         case SRCCOPY: /* well, we do that below ? */
520             break;
521         default:
522             FIXME("Unsupported raster op: %08lx  Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
523             goto error;
524         }
525         dwFlags &= ~DDBLT_ROP;
526     }
527     if (dwFlags & DDBLT_DDROPS) {
528         FIXME("\tDdraw Raster Ops: %08lx  Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
529     }
530     /* Now the 'with source' blits */
531     if (src) {
532         LPBYTE sbase;
533         int sx, xinc, sy, yinc;
534
535         if (!dstwidth || !dstheight) /* hmm... stupid program ? */
536             goto release;
537         sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
538         xinc = (srcwidth << 16) / dstwidth;
539         yinc = (srcheight << 16) / dstheight;
540
541         if (!dwFlags) {
542             /* No effects, we can cheat here */
543             if (dstwidth == srcwidth) {
544                 if (dstheight == srcheight) {
545                     /* No stretching in either direction. This needs to be as
546                      * fast as possible */
547                     sbuf = sbase;
548
549                     /* check for overlapping surfaces */
550                     if (src != iface || xdst.top < xsrc.top ||
551                         xdst.right <= xsrc.left || xsrc.right <= xdst.left)
552                     {
553                         /* no overlap, or dst above src, so copy from top downwards */
554                         for (y = 0; y < dstheight; y++)
555                         {
556                             memcpy(dbuf, sbuf, width);
557                             sbuf += sdesc.u1.lPitch;
558                             dbuf += ddesc.u1.lPitch;
559                         }
560                     }
561                     else if (xdst.top > xsrc.top)  /* copy from bottom upwards */
562                     {
563                         sbuf += (sdesc.u1.lPitch*dstheight);
564                         dbuf += (ddesc.u1.lPitch*dstheight);
565                         for (y = 0; y < dstheight; y++)
566                         {
567                             sbuf -= sdesc.u1.lPitch;
568                             dbuf -= ddesc.u1.lPitch;
569                             memcpy(dbuf, sbuf, width);
570                         }
571                     }
572                     else /* src and dst overlapping on the same line, use memmove */
573                     {
574                         for (y = 0; y < dstheight; y++)
575                         {
576                             memmove(dbuf, sbuf, width);
577                             sbuf += sdesc.u1.lPitch;
578                             dbuf += ddesc.u1.lPitch;
579                         }
580                     }
581                 } else {
582                     /* Stretching in Y direction only */
583                     for (y = sy = 0; y < dstheight; y++, sy += yinc) {
584                         sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
585                         memcpy(dbuf, sbuf, width);
586                         dbuf += ddesc.u1.lPitch;
587                     }
588                 }
589             } else {
590                 /* Stretching in X direction */
591                 int last_sy = -1;
592                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
593                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
594
595                     if ((sy >> 16) == (last_sy >> 16)) {
596                         /* this sourcerow is the same as last sourcerow -
597                          * copy already stretched row
598                          */
599                         memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
600                     } else {
601 #define STRETCH_ROW(type) { \
602                     type *s = (type *) sbuf, *d = (type *) dbuf; \
603                     for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
604                     d[x] = s[sx >> 16]; \
605                     break; }
606
607                     switch(bpp) {
608                     case 1: STRETCH_ROW(BYTE)
609                     case 2: STRETCH_ROW(WORD)
610                     case 4: STRETCH_ROW(DWORD)
611                     case 3: {
612                         LPBYTE s,d = dbuf;
613                         for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
614                             DWORD pixel;
615
616                             s = sbuf+3*(sx>>16);
617                             pixel = (s[0]<<16)|(s[1]<<8)|s[2];
618                             d[0] = (pixel>>16)&0xff;
619                             d[1] = (pixel>> 8)&0xff;
620                             d[2] = (pixel    )&0xff;
621                             d+=3;
622                         }
623                         break;
624                     }
625                     default:
626                         FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
627                         ret = DDERR_UNSUPPORTED;
628                         goto error;
629                     }
630 #undef STRETCH_ROW
631                     }
632                     dbuf += ddesc.u1.lPitch;
633                     last_sy = sy;
634                 }
635             }
636         } else {
637            LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
638            DWORD keylow = 0, keyhigh = 0;
639            if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
640
641               if (dwFlags & DDBLT_KEYSRC) {
642                  keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
643                  keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
644               } else if (dwFlags & DDBLT_KEYDEST){
645                  keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
646                  keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
647               } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
648                  keylow  = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
649                  keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
650               } else {
651                  keylow  = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
652                  keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
653               }
654               dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
655            }
656
657            if (dwFlags & DDBLT_DDFX)  {
658               LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
659               LONG tmpxy;
660               dTopLeft     = dbuf;
661               dTopRight    = dbuf+((dstwidth-1)*bpp);
662               dBottomLeft  = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
663               dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
664
665               if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
666                  /* I don't think we need to do anything about this flag */
667                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
668               }
669               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
670                  tmp          = dTopRight;
671                  dTopRight    = dTopLeft;
672                  dTopLeft     = tmp;
673                  tmp          = dBottomRight;
674                  dBottomRight = dBottomLeft;
675                  dBottomLeft  = tmp;
676                  dstxinc = dstxinc *-1;
677               }
678               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
679                  tmp          = dTopLeft;
680                  dTopLeft     = dBottomLeft;
681                  dBottomLeft  = tmp;
682                  tmp          = dTopRight;
683                  dTopRight    = dBottomRight;
684                  dBottomRight = tmp;
685                  dstyinc = dstyinc *-1;
686               }
687               if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
688                  /* I don't think we need to do anything about this flag */
689                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
690               }
691               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
692                  tmp          = dBottomRight;
693                  dBottomRight = dTopLeft;
694                  dTopLeft     = tmp;
695                  tmp          = dBottomLeft;
696                  dBottomLeft  = dTopRight;
697                  dTopRight    = tmp;
698                  dstxinc = dstxinc * -1;
699                  dstyinc = dstyinc * -1;
700               }
701               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
702                  tmp          = dTopLeft;
703                  dTopLeft     = dBottomLeft;
704                  dBottomLeft  = dBottomRight;
705                  dBottomRight = dTopRight;
706                  dTopRight    = tmp;
707                  tmpxy   = dstxinc;
708                  dstxinc = dstyinc;
709                  dstyinc = tmpxy;
710                  dstxinc = dstxinc * -1;
711               }
712               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
713                  tmp          = dTopLeft;
714                  dTopLeft     = dTopRight;
715                  dTopRight    = dBottomRight;
716                  dBottomRight = dBottomLeft;
717                  dBottomLeft  = tmp;
718                  tmpxy   = dstxinc;
719                  dstxinc = dstyinc;
720                  dstyinc = tmpxy;
721                  dstyinc = dstyinc * -1;
722               }
723               if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
724                  /* I don't think we need to do anything about this flag */
725                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
726               }
727               dbuf = dTopLeft;
728               dwFlags &= ~(DDBLT_DDFX);
729            }
730
731 #define COPY_COLORKEY_FX(type) { \
732             type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
733             for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
734                (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
735                (LPBYTE)dx = d; \
736                for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
737                   tmp = s[sx >> 16]; \
738                   if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
739                   (LPBYTE)dx += dstxinc; \
740                   } \
741                (LPBYTE)d += dstyinc; \
742             } \
743             break; }
744
745             switch (bpp) {
746             case 1: COPY_COLORKEY_FX(BYTE)
747             case 2: COPY_COLORKEY_FX(WORD)
748             case 4: COPY_COLORKEY_FX(DWORD)
749             case 3: {LPBYTE s,d = dbuf, dx;
750                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
751                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
752                     dx = d;
753                     for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
754                         DWORD pixel;
755                         s = sbuf+3*(sx>>16);
756                         pixel = (s[0]<<16)|(s[1]<<8)|s[2];
757                         if (pixel < keylow || pixel > keyhigh){
758                             dx[0] = (pixel>>16)&0xff;
759                             dx[1] = (pixel>> 8)&0xff;
760                             dx[2] = (pixel    )&0xff;
761                         }
762                         dx+= dstxinc;
763                     }
764                     d += dstyinc;
765                 }
766                 break;}
767             default:
768                FIXME("%s color-keyed blit not implemented for bpp %d!\n",
769                   (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
770                   ret = DDERR_UNSUPPORTED;
771                   goto error;
772 #undef COPY_COLORKEY_FX
773             }
774         }
775     }
776
777 error:
778     if (dwFlags && FIXME_ON(ddraw)) {
779         FIXME("\tUnsupported flags: ");
780         DDRAW_dump_DDBLT(dwFlags);
781     }
782
783 release:
784     IDirectDrawSurface7_Unlock(iface,NULL);
785     if (src) IDirectDrawSurface7_Unlock(src,NULL);
786     return DD_OK;
787 }
788
789 /* BltBatch: generic, unimplemented */
790
791 HRESULT WINAPI
792 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
793                               DWORD dsty, LPDIRECTDRAWSURFACE7 src,
794                               LPRECT rsrc, DWORD trans)
795 {
796     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
797     int                 bpp, w, h, x, y;
798     DDSURFACEDESC2      ddesc,sdesc;
799     HRESULT             ret = DD_OK;
800     LPBYTE              sbuf, dbuf;
801     RECT                rsrc2;
802
803
804     if (TRACE_ON(ddraw)) {
805         FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
806                 This,dstx,dsty,src,rsrc,trans
807         );
808         FIXME("\ttrans:");
809         if (FIXME_ON(ddraw))
810           DDRAW_dump_DDBLTFAST(trans);
811         if (rsrc)
812           FIXME("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
813         else
814           FIXME(" srcrect: NULL\n");
815     }
816
817     /* We need to lock the surfaces, or we won't get refreshes when done. */
818     sdesc.dwSize = sizeof(sdesc);
819     IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
820     ddesc.dwSize = sizeof(ddesc);
821     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
822
823    if (!rsrc) {
824            WARN("rsrc is NULL!\n");
825            rsrc = &rsrc2;
826            rsrc->left = rsrc->top = 0;
827            rsrc->right = sdesc.dwWidth;
828            rsrc->bottom = sdesc.dwHeight;
829    }
830
831     bpp = GET_BPP(This->surface_desc);
832     sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
833     dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
834
835
836     h=rsrc->bottom-rsrc->top;
837     if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
838     if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
839     if (h<0) h=0;
840
841     w=rsrc->right-rsrc->left;
842     if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
843     if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
844     if (w<0) w=0;
845
846     if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
847         DWORD keylow, keyhigh;
848         if (trans & DDBLTFAST_SRCCOLORKEY) {
849             keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
850             keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
851         } else {
852             /* I'm not sure if this is correct */
853             FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
854             keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
855             keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
856         }
857
858 #define COPYBOX_COLORKEY(type) { \
859     type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
860     s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
861     d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
862     for (y = 0; y < h; y++) { \
863         for (x = 0; x < w; x++) { \
864             tmp = s[x]; \
865             if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
866         } \
867         (LPBYTE)s += sdesc.u1.lPitch; \
868         (LPBYTE)d += ddesc.u1.lPitch; \
869     } \
870     break; \
871 }
872
873         switch (bpp) {
874         case 1: COPYBOX_COLORKEY(BYTE)
875         case 2: COPYBOX_COLORKEY(WORD)
876         case 4: COPYBOX_COLORKEY(DWORD)
877         default:
878             FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
879             ret = DDERR_UNSUPPORTED;
880             goto error;
881         }
882 #undef COPYBOX_COLORKEY
883     } else {
884         int width = w * bpp;
885
886         for (y = 0; y < h; y++) {
887             memcpy(dbuf, sbuf, width);
888             sbuf += sdesc.u1.lPitch;
889             dbuf += ddesc.u1.lPitch;
890         }
891     }
892 error:
893     IDirectDrawSurface7_Unlock(iface, NULL);
894     IDirectDrawSurface7_Unlock(src, NULL);
895     return ret;
896 }
897
898 /* ChangeUniquenessValue: generic */
899 /* DeleteAttachedSurface: generic */
900 /* EnumAttachedSurfaces: generic */
901 /* EnumOverlayZOrders: generic, unimplemented */
902
903 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
904                                      IDirectDrawSurfaceImpl* back,
905                                      DWORD dwFlags)
906 {
907     DIB_DirectDrawSurfaceImpl* front_priv = front->private;
908     DIB_DirectDrawSurfaceImpl* back_priv = back->private;
909
910     TRACE("(%p,%p)\n",front,back);
911
912     {
913         HBITMAP tmp;
914         tmp = front_priv->dib.DIBsection;
915         front_priv->dib.DIBsection = back_priv->dib.DIBsection;
916         back_priv->dib.DIBsection = tmp;
917     }
918
919     {
920         void* tmp;
921         tmp = front_priv->dib.bitmap_data;
922         front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
923         back_priv->dib.bitmap_data = tmp;
924
925         tmp = front->surface_desc.lpSurface;
926         front->surface_desc.lpSurface = back->surface_desc.lpSurface;
927         back->surface_desc.lpSurface = tmp;
928     }
929
930     /* client_memory should not be different, but just in case */
931     {
932         BOOL tmp;
933         tmp = front_priv->dib.client_memory;
934         front_priv->dib.client_memory = back_priv->dib.client_memory;
935         back_priv->dib.client_memory = tmp;
936     }
937
938     return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
939 }
940
941 /* Flip: generic */
942 /* FreePrivateData: generic */
943 /* GetAttachedSurface: generic */
944 /* GetBltStatus: generic */
945 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
946 /* GetClipper: generic */
947 /* GetColorKey: generic */
948
949 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
950 {
951     DIB_PRIV_VAR(priv, This);
952     HDC hDC;
953
954     TRACE("Grabbing a DC for surface: %p\n", This);
955
956     hDC = CreateCompatibleDC(0);
957     priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
958     if (This->palette)
959         SelectPalette(hDC, This->palette->hpal, FALSE);
960
961     *phDC = hDC;
962
963     return S_OK;
964 }
965
966 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
967 {
968     DIB_PRIV_VAR(priv, This);
969
970     TRACE("Releasing DC for surface: %p\n", This);
971
972     SelectObject(hDC, priv->dib.holdbitmap);
973     DeleteDC(hDC);
974
975     return S_OK;
976 }
977
978 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
979 {
980     return DIB_DirectDrawSurface_alloc_dc(This, phDC);
981 }
982
983 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
984 {
985     return DIB_DirectDrawSurface_free_dc(This, hDC);
986 }
987
988 /* GetDDInterface: generic */
989 /* GetFlipStatus: generic */
990 /* GetLOD: generic */
991 /* GetOverlayPosition: generic */
992 /* GetPalette: generic */
993 /* GetPixelFormat: generic */
994 /* GetPriority: generic */
995 /* GetPrivateData: generic */
996 /* GetSurfaceDesc: generic */
997 /* GetUniquenessValue: generic */
998 /* Initialize: generic */
999 /* IsLost: generic */
1000 /* Lock: generic with callback? */
1001 /* PageLock: generic */
1002 /* PageUnlock: generic */
1003
1004 HRESULT WINAPI
1005 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1006 {
1007     TRACE("(%p)\n",iface);
1008     return DD_OK;       /* ??? */
1009 }
1010
1011 /* SetClipper: generic */
1012 /* SetColorKey: generic */
1013 /* SetLOD: generic */
1014 /* SetOverlayPosition: generic */
1015
1016 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1017                                        IDirectDrawPaletteImpl* pal)
1018 {
1019     if (!pal) return;
1020     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1021         This->update_palette(This, pal,
1022                              0, pal->palNumEntries,
1023                              pal->palents);
1024 }
1025
1026 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1027                                           IDirectDrawPaletteImpl* pal,
1028                                           DWORD dwStart, DWORD dwCount,
1029                                           LPPALETTEENTRY palent)
1030 {
1031     RGBQUAD col[256];
1032     int n;
1033     HDC dc;
1034
1035     TRACE("updating primary palette\n");
1036     for (n=0; n<dwCount; n++) {
1037       col[n].rgbRed   = palent[n].peRed;
1038       col[n].rgbGreen = palent[n].peGreen;
1039       col[n].rgbBlue  = palent[n].peBlue;
1040       col[n].rgbReserved = 0;
1041     }
1042     This->get_dc(This, &dc);
1043     SetDIBColorTable(dc, dwStart, dwCount, col);
1044     This->release_dc(This, dc);
1045
1046     /* Propagate change to backbuffers if there are any */
1047     /* Basically this is a modification of the Flip code to find the backbuffer */
1048     /* and duplicate the palette update there as well */
1049     if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1050         == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1051     {
1052         static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1053         LPDIRECTDRAWSURFACE7 tgt;
1054
1055         HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1056                                                             &back_caps, &tgt);
1057         if (!FAILED(hr))
1058         {
1059             IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1060                                                          IDirectDrawSurface7,tgt);
1061             IDirectDrawSurface7_Release(tgt);
1062             target->get_dc(target, &dc);
1063             SetDIBColorTable(dc, dwStart, dwCount, col);
1064             target->release_dc(target, dc);
1065         }
1066     }
1067 }
1068
1069 /* SetPalette: generic */
1070 /* SetPriority: generic */
1071 /* SetPrivateData: generic */
1072
1073 HRESULT WINAPI
1074 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1075                                      LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1076 {
1077     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1078     DIB_PRIV_VAR(priv, This);
1079     HRESULT hr = DD_OK;
1080     DWORD flags = pDDSD->dwFlags;
1081
1082     if (TRACE_ON(ddraw)) {
1083         TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1084         DDRAW_dump_surface_desc(pDDSD);
1085     }
1086
1087     if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1088         flags &= ~DDSD_PIXELFORMAT;
1089         if (flags & DDSD_LPSURFACE) {
1090             This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1091         } else {
1092             FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1093         }
1094     }
1095     if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1096         HBITMAP oldbmp = priv->dib.DIBsection;
1097         LPVOID oldsurf = This->surface_desc.lpSurface;
1098         BOOL oldc = priv->dib.client_memory;
1099
1100         flags &= ~DDSD_LPSURFACE;
1101
1102         TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1103         This->surface_desc.lpSurface = pDDSD->lpSurface;
1104         priv->dib.client_memory = TRUE;
1105
1106         hr = create_dib(This);
1107         if (FAILED(hr))
1108         {
1109             priv->dib.DIBsection = oldbmp;
1110             This->surface_desc.lpSurface = oldsurf;
1111             priv->dib.client_memory = oldc;
1112             return hr;
1113         }
1114
1115         DeleteObject(oldbmp);
1116
1117         if (!oldc)
1118             VirtualFree(oldsurf, 0, MEM_RELEASE);
1119     }
1120     if (flags) {
1121         WARN("Unhandled flags : %08lx\n", flags);
1122     }
1123     return hr;
1124 }
1125
1126 /* Unlock: ???, need callback */
1127 /* UpdateOverlay: generic */
1128 /* UpdateOverlayDisplay: generic */
1129 /* UpdateOverlayZOrder: generic */
1130
1131 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1132 {
1133     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1134     Main_DirectDrawSurface_QueryInterface,
1135     Main_DirectDrawSurface_AddRef,
1136     Main_DirectDrawSurface_Release,
1137     Main_DirectDrawSurface_AddAttachedSurface,
1138     Main_DirectDrawSurface_AddOverlayDirtyRect,
1139     DIB_DirectDrawSurface_Blt,
1140     Main_DirectDrawSurface_BltBatch,
1141     DIB_DirectDrawSurface_BltFast,
1142     Main_DirectDrawSurface_DeleteAttachedSurface,
1143     Main_DirectDrawSurface_EnumAttachedSurfaces,
1144     Main_DirectDrawSurface_EnumOverlayZOrders,
1145     Main_DirectDrawSurface_Flip,
1146     Main_DirectDrawSurface_GetAttachedSurface,
1147     Main_DirectDrawSurface_GetBltStatus,
1148     Main_DirectDrawSurface_GetCaps,
1149     Main_DirectDrawSurface_GetClipper,
1150     Main_DirectDrawSurface_GetColorKey,
1151     Main_DirectDrawSurface_GetDC,
1152     Main_DirectDrawSurface_GetFlipStatus,
1153     Main_DirectDrawSurface_GetOverlayPosition,
1154     Main_DirectDrawSurface_GetPalette,
1155     Main_DirectDrawSurface_GetPixelFormat,
1156     Main_DirectDrawSurface_GetSurfaceDesc,
1157     Main_DirectDrawSurface_Initialize,
1158     Main_DirectDrawSurface_IsLost,
1159     Main_DirectDrawSurface_Lock,
1160     Main_DirectDrawSurface_ReleaseDC,
1161     DIB_DirectDrawSurface_Restore,
1162     Main_DirectDrawSurface_SetClipper,
1163     Main_DirectDrawSurface_SetColorKey,
1164     Main_DirectDrawSurface_SetOverlayPosition,
1165     Main_DirectDrawSurface_SetPalette,
1166     Main_DirectDrawSurface_Unlock,
1167     Main_DirectDrawSurface_UpdateOverlay,
1168     Main_DirectDrawSurface_UpdateOverlayDisplay,
1169     Main_DirectDrawSurface_UpdateOverlayZOrder,
1170     Main_DirectDrawSurface_GetDDInterface,
1171     Main_DirectDrawSurface_PageLock,
1172     Main_DirectDrawSurface_PageUnlock,
1173     DIB_DirectDrawSurface_SetSurfaceDesc,
1174     Main_DirectDrawSurface_SetPrivateData,
1175     Main_DirectDrawSurface_GetPrivateData,
1176     Main_DirectDrawSurface_FreePrivateData,
1177     Main_DirectDrawSurface_GetUniquenessValue,
1178     Main_DirectDrawSurface_ChangeUniquenessValue,
1179     Main_DirectDrawSurface_SetPriority,
1180     Main_DirectDrawSurface_GetPriority,
1181     Main_DirectDrawSurface_SetLOD,
1182     Main_DirectDrawSurface_GetLOD
1183 };