drawdib.c 13.4 KB
Newer Older
1
/*
2 3
 * Copyright 2000 Bradley Baetz
 *
4 5 6 7 8 9 10 11 12 13 14 15
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17
 *
18
 * FIXME: Some flags are ignored
19
 *
20 21 22
 * Handle palettes
 */

23 24
#include <stdarg.h>
#include <stdio.h>
25
#include <string.h>
26

27 28
#include "windef.h"
#include "winbase.h"
29 30
#include "wingdi.h"
#include "winuser.h"
31
#include "vfw.h"
32

33
#include "wine/debug.h"
34

35
WINE_DEFAULT_DEBUG_CHANNEL(msvideo);
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
typedef struct tagWINE_HDD {
    HDC                 hdc;
    INT                 dxDst;
    INT                 dyDst;
    LPBITMAPINFOHEADER  lpbi;
    INT                 dxSrc;
    INT                 dySrc;
    HPALETTE            hpal;		/* Palette to use for the DIB */
    BOOL                begun;		/* DrawDibBegin has been called */
    LPBITMAPINFOHEADER  lpbiOut;	/* Output format */
    HIC                 hic;		/* HIC for decompression */
    HDC                 hMemDC;		/* DC for buffering */
    HBITMAP             hOldDib;	/* Original Dib */
    HBITMAP             hDib;		/* DibSection */
    LPVOID              lpvbits;	/* Buffer for holding decompressed dib */
    HDRAWDIB            hSelf;
    struct tagWINE_HDD* next;
54 55
} WINE_HDD;

56
static int num_colours(const BITMAPINFOHEADER *lpbi)
57 58 59 60 61 62 63 64
{
	if(lpbi->biClrUsed)
		return lpbi->biClrUsed;
	if(lpbi->biBitCount<=8)
		return 1<<lpbi->biBitCount;
	return 0;
}

65 66 67 68 69
static WINE_HDD*        HDD_FirstHdd /* = NULL */;

static WINE_HDD*       MSVIDEO_GetHddPtr(HDRAWDIB hd)
{
    WINE_HDD*   hdd;
70

71
    for (hdd = HDD_FirstHdd; hdd != NULL && hdd->hSelf != hd; hdd = hdd->next);
72
    return hdd;
73 74
}

75 76
static DWORD HDD_HandleRef = 1;

77
/***********************************************************************
78
 *		DrawDibOpen		[MSVFW32.@]
79
 */
80 81 82
HDRAWDIB VFWAPI DrawDibOpen(void) 
{
    WINE_HDD*   whdd;
83

84 85 86 87
    TRACE("(void)\n");
	
    whdd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_HDD));
    TRACE("=> %p\n", whdd);
88

89 90
    while (MSVIDEO_GetHddPtr((HDRAWDIB)HDD_HandleRef) != NULL) HDD_HandleRef++;
    whdd->hSelf = (HDRAWDIB)HDD_HandleRef++;
91

92 93
    whdd->next = HDD_FirstHdd;
    HDD_FirstHdd = whdd;
94

95
    return whdd->hSelf;
96 97 98
}

/***********************************************************************
99
 *		DrawDibClose		[MSVFW32.@]
100
 */
101 102 103
BOOL VFWAPI DrawDibClose(HDRAWDIB hdd) 
{
    WINE_HDD* whdd = MSVIDEO_GetHddPtr(hdd);
104
    WINE_HDD** p;
105

106
    TRACE("(%p)\n", hdd);
107

108
    if (!whdd) return FALSE;
109

110
    if (whdd->begun) DrawDibEnd(hdd);
111

112 113 114 115 116 117 118 119 120
    for (p = &HDD_FirstHdd; *p != NULL; p = &((*p)->next))
    {
        if (*p == whdd)
        {
            *p = whdd->next;
            break;
        }
    }

121
    HeapFree(GetProcessHeap(), 0, whdd);
122

123 124
    return TRUE;
}
125

126 127 128 129 130 131 132 133 134 135 136 137
/***********************************************************************
 *		DrawDibEnd		[MSVFW32.@]
 */
BOOL VFWAPI DrawDibEnd(HDRAWDIB hdd) 
{
    BOOL ret = TRUE;
    WINE_HDD *whdd = MSVIDEO_GetHddPtr(hdd);

    TRACE("(%p)\n", hdd);

    whdd->hpal = 0; /* Do not free this */
    whdd->hdc = 0;
138 139 140 141
    HeapFree(GetProcessHeap(), 0, whdd->lpbi);
    whdd->lpbi = NULL;
    HeapFree(GetProcessHeap(), 0, whdd->lpbiOut);
    whdd->lpbiOut = NULL;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    whdd->begun = FALSE;

    /*if (whdd->lpvbits)
      HeapFree(GetProcessHeap(), 0, whdd->lpvbuf);*/

    if (whdd->hMemDC) 
    {
        SelectObject(whdd->hMemDC, whdd->hOldDib);
        DeleteDC(whdd->hMemDC);
        whdd->hMemDC = 0;
    }

    if (whdd->hDib) DeleteObject(whdd->hDib);
    whdd->hDib = 0;

    if (whdd->hic) 
    {
        ICDecompressEnd(whdd->hic);
        ICClose(whdd->hic);
        whdd->hic = 0;
    }

    whdd->lpvbits = NULL;

    return ret;
168 169 170
}

/***********************************************************************
171
 *              DrawDibBegin            [MSVFW32.@]
172 173
 */
BOOL VFWAPI DrawDibBegin(HDRAWDIB hdd,
174 175 176 177 178 179 180 181 182 183 184
                         HDC      hdc,
                         INT      dxDst,
                         INT      dyDst,
                         LPBITMAPINFOHEADER lpbi,
                         INT      dxSrc,
                         INT      dySrc,
                         UINT     wFlags) 
{
    BOOL ret = TRUE;
    WINE_HDD *whdd;

185
    TRACE("(%p,%p,%d,%d,%p,%d,%d,0x%08x)\n",
186 187
          hdd, hdc, dxDst, dyDst, lpbi, dxSrc, dySrc, (DWORD)wFlags);

188
    TRACE("lpbi: %d,%d/%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
          lpbi->biSize, lpbi->biWidth, lpbi->biHeight, lpbi->biPlanes,
          lpbi->biBitCount, lpbi->biCompression, lpbi->biSizeImage,
          lpbi->biXPelsPerMeter, lpbi->biYPelsPerMeter, lpbi->biClrUsed,
          lpbi->biClrImportant);

    if (wFlags & ~(DDF_BUFFER))
        FIXME("wFlags == 0x%08x not handled\n", wFlags & ~(DDF_BUFFER));

    whdd = MSVIDEO_GetHddPtr(hdd);
    if (!whdd) return FALSE;

    if (whdd->begun) DrawDibEnd(hdd);

    if (lpbi->biCompression) 
    {
        DWORD size = 0;

        whdd->hic = ICOpen(ICTYPE_VIDEO, lpbi->biCompression, ICMODE_DECOMPRESS);
        if (!whdd->hic) 
        {
209
            WARN("Could not open IC. biCompression == 0x%08x\n", lpbi->biCompression);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            ret = FALSE;
        }

        if (ret) 
        {
            size = ICDecompressGetFormat(whdd->hic, lpbi, NULL);
            if (size == ICERR_UNSUPPORTED) 
            {
                WARN("Codec doesn't support GetFormat, giving up.\n");
                ret = FALSE;
            }
        }

        if (ret) 
        {
            whdd->lpbiOut = HeapAlloc(GetProcessHeap(), 0, size);

            if (ICDecompressGetFormat(whdd->hic, lpbi, whdd->lpbiOut) != ICERR_OK)
                ret = FALSE;
        }

        if (ret) 
        {
            /* FIXME: Use Ex functions if available? */
            if (ICDecompressBegin(whdd->hic, lpbi, whdd->lpbiOut) != ICERR_OK)
                ret = FALSE;
            
237 238
            TRACE("biSizeImage == %d\n", whdd->lpbiOut->biSizeImage);
            TRACE("biCompression == %d\n", whdd->lpbiOut->biCompression);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
            TRACE("biBitCount == %d\n", whdd->lpbiOut->biBitCount);
        }
    }
    else 
    {
        DWORD dwSize;
        /* No compression */
        TRACE("Not compressed!\n");
        dwSize = lpbi->biSize + num_colours(lpbi)*sizeof(RGBQUAD);
        whdd->lpbiOut = HeapAlloc(GetProcessHeap(), 0, dwSize);
        memcpy(whdd->lpbiOut, lpbi, dwSize);
    }

    if (ret) 
    {
        /*whdd->lpvbuf = HeapAlloc(GetProcessHeap(), 0, whdd->lpbiOut->biSizeImage);*/

        whdd->hMemDC = CreateCompatibleDC(hdc);
257
        TRACE("Creating: %d, %p\n", whdd->lpbiOut->biSize, whdd->lpvbits);
258
        whdd->hDib = CreateDIBSection(whdd->hMemDC, (BITMAPINFO *)whdd->lpbiOut, DIB_RGB_COLORS, &(whdd->lpvbits), 0, 0);
Richard Cohen's avatar
Richard Cohen committed
259
        if (whdd->hDib) 
260
        {
Richard Cohen's avatar
Richard Cohen committed
261 262 263 264 265
            TRACE("Created: %p,%p\n", whdd->hDib, whdd->lpvbits);
        }
        else
        {
            ret = FALSE;
266
            TRACE("Error: %d\n", GetLastError());
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        }
        whdd->hOldDib = SelectObject(whdd->hMemDC, whdd->hDib);
    }

    if (ret) 
    {
        whdd->hdc = hdc;
        whdd->dxDst = dxDst;
        whdd->dyDst = dyDst;
        whdd->lpbi = HeapAlloc(GetProcessHeap(), 0, lpbi->biSize);
        memcpy(whdd->lpbi, lpbi, lpbi->biSize);
        whdd->dxSrc = dxSrc;
        whdd->dySrc = dySrc;
        whdd->begun = TRUE;
        whdd->hpal = 0;
    } 
    else 
    {
        if (whdd->hic)
            ICClose(whdd->hic);
287 288
        HeapFree(GetProcessHeap(), 0, whdd->lpbiOut);
        whdd->lpbiOut = NULL;
289 290 291
    }

    return ret;
292 293 294
}

/**********************************************************************
295
 *		DrawDibDraw		[MSVFW32.@]
296
 */
297
BOOL VFWAPI DrawDibDraw(HDRAWDIB hdd, HDC hdc,
298 299 300 301 302 303 304 305
                        INT xDst, INT yDst, INT dxDst, INT dyDst,
                        LPBITMAPINFOHEADER lpbi,
                        LPVOID lpBits,
                        INT xSrc, INT ySrc, INT dxSrc, INT dySrc,
                        UINT wFlags)
{
    WINE_HDD *whdd;
    BOOL ret = TRUE;
306

307
    TRACE("(%p,%p,%d,%d,%d,%d,%p,%p,%d,%d,%d,%d,0x%08x)\n",
308
          hdd, hdc, xDst, yDst, dxDst, dyDst, lpbi, lpBits, xSrc, ySrc, dxSrc, dySrc, (DWORD)wFlags);
309

310
    whdd = MSVIDEO_GetHddPtr(hdd);
311
    if (!whdd) return FALSE;
312

Richard Cohen's avatar
Richard Cohen committed
313 314
    TRACE("whdd=%p\n", whdd);

315
    if (wFlags & ~(DDF_SAME_HDC | DDF_SAME_DRAW | DDF_NOTKEYFRAME | DDF_UPDATE | DDF_DONTDRAW | DDF_BACKGROUNDPAL))
316
        FIXME("wFlags == 0x%08x not handled\n", (DWORD)wFlags);
317

318 319 320 321 322
    if (!lpBits) 
    {
        /* Undocumented? */
        lpBits = (LPSTR)lpbi + (WORD)(lpbi->biSize) + (WORD)(num_colours(lpbi)*sizeof(RGBQUAD));
    }
323 324


325
#define CHANGED(x) (whdd->x != x)
326

327 328 329 330 331 332 333
    if ((!whdd->begun) || 
        (!(wFlags & DDF_SAME_HDC) && CHANGED(hdc)) || 
        (!(wFlags & DDF_SAME_DRAW) && (CHANGED(lpbi) || CHANGED(dxSrc) || CHANGED(dySrc) || CHANGED(dxDst) || CHANGED(dyDst)))) 
    {
        TRACE("Something changed!\n");
        ret = DrawDibBegin(hdd, hdc, dxDst, dyDst, lpbi, dxSrc, dySrc, 0);
    }
334

335
#undef CHANGED
336

337 338 339 340 341 342 343 344
    if ((dxDst == -1) && (dyDst == -1)) 
    {
        dxDst = dxSrc;
        dyDst = dySrc;
    }

    if (!(wFlags & DDF_UPDATE)) 
    {
Richard Cohen's avatar
Richard Cohen committed
345 346
        DWORD biSizeImage = lpbi->biSizeImage;

347
        /* biSizeImage may be set to 0 for BI_RGB (uncompressed) bitmaps */
Richard Cohen's avatar
Richard Cohen committed
348 349
        if ((lpbi->biCompression == BI_RGB) && (biSizeImage == 0))
            biSizeImage = ((lpbi->biWidth * lpbi->biBitCount + 31) / 32) * 4 * lpbi->biHeight;
350 351 352 353 354

        if (lpbi->biCompression) 
        {
            DWORD flags = 0;

355
            TRACE("Compression == 0x%08x\n", lpbi->biCompression);
356 357 358 359 360 361 362 363

            if (wFlags & DDF_NOTKEYFRAME)
                flags |= ICDECOMPRESS_NOTKEYFRAME;

            ICDecompress(whdd->hic, flags, lpbi, lpBits, whdd->lpbiOut, whdd->lpvbits);
        }
        else
        {
Richard Cohen's avatar
Richard Cohen committed
364
            memcpy(whdd->lpvbits, lpBits, biSizeImage);
365 366 367
        }
    }
    if (!(wFlags & DDF_DONTDRAW) && whdd->hpal)
368 369 370 371 372 373
    {
        if ((wFlags & DDF_BACKGROUNDPAL) && ! (wFlags & DDF_SAME_HDC))
         SelectPalette(hdc, whdd->hpal, TRUE);
        else
         SelectPalette(hdc, whdd->hpal, FALSE);
    }
374 375 376 377 378

    if (!(StretchBlt(whdd->hdc, xDst, yDst, dxDst, dyDst, whdd->hMemDC, xSrc, ySrc, dxSrc, dySrc, SRCCOPY)))
        ret = FALSE;
    
    return ret;
379 380 381
}

/*************************************************************************
382
 *		DrawDibStart		[MSVFW32.@]
383 384
 */
BOOL VFWAPI DrawDibStart(HDRAWDIB hdd, DWORD rate) {
385
	FIXME("(%p, %d), stub\n", hdd, rate);
386 387 388 389
	return TRUE;
}

/*************************************************************************
390
 *		DrawDibStop		[MSVFW32.@]
391 392
 */
BOOL VFWAPI DrawDibStop(HDRAWDIB hdd) {
393
	FIXME("(%p), stub\n", hdd);
394 395 396
	return TRUE;
}

397 398 399 400 401 402 403 404 405
/***********************************************************************
 *              DrawDibChangePalette       [MSVFW32.@]
 */
BOOL VFWAPI DrawDibChangePalette(HDRAWDIB hdd, int iStart, int iLen, LPPALETTEENTRY lppe)
{
    FIXME("(%p, 0x%08x, 0x%08x, %p), stub\n", hdd, iStart, iLen, lppe);
    return TRUE;
}

406
/***********************************************************************
407
 *              DrawDibSetPalette       [MSVFW32.@]
408
 */
409 410 411
BOOL VFWAPI DrawDibSetPalette(HDRAWDIB hdd, HPALETTE hpal) 
{
    WINE_HDD *whdd;
412

413
    TRACE("(%p, %p)\n", hdd, hpal);
414

415 416
    whdd = MSVIDEO_GetHddPtr(hdd);
    if (!whdd) return FALSE;
417

418 419 420 421 422 423 424 425 426
    whdd->hpal = hpal;

    if (whdd->begun) 
    {
        SelectPalette(whdd->hdc, hpal, 0);
        RealizePalette(whdd->hdc);
    }

    return TRUE;
427 428
}

429 430 431 432 433
/***********************************************************************
 *              DrawDibGetBuffer       [MSVFW32.@]
 */
LPVOID VFWAPI DrawDibGetBuffer(HDRAWDIB hdd, LPBITMAPINFOHEADER lpbi, DWORD dwSize, DWORD dwFlags)
{
434
    FIXME("(%p, %p, 0x%08x, 0x%08x), stub\n", hdd, lpbi, dwSize, dwFlags);
435 436 437
    return NULL;
}

438
/***********************************************************************
439
 *              DrawDibGetPalette       [MSVFW32.@]
440
 */
441 442 443 444 445
HPALETTE VFWAPI DrawDibGetPalette(HDRAWDIB hdd) 
{
    WINE_HDD *whdd;

    TRACE("(%p)\n", hdd);
446

447 448
    whdd = MSVIDEO_GetHddPtr(hdd);
    if (!whdd) return FALSE;
449

450
    return whdd->hpal;
451 452 453
}

/***********************************************************************
454
 *              DrawDibRealize          [MSVFW32.@]
455
 */
456 457 458 459 460
UINT VFWAPI DrawDibRealize(HDRAWDIB hdd, HDC hdc, BOOL fBackground) 
{
    WINE_HDD *whdd;
    HPALETTE oldPal;
    UINT ret = 0;
461

462
    FIXME("(%p, %p, %d), stub\n", hdd, hdc, fBackground);
463

464 465
    whdd = MSVIDEO_GetHddPtr(hdd);
    if (!whdd) return FALSE;
466

467 468 469 470 471
    if (!whdd || !(whdd->begun)) 
    {
        ret = 0;
        goto out;
    }
472

473 474
    if (!whdd->hpal)
        whdd->hpal = CreateHalftonePalette(hdc);
475

476 477
    oldPal = SelectPalette(hdc, whdd->hpal, fBackground);
    ret = RealizePalette(hdc);
478

479
 out:
480 481
    TRACE("=> %u\n", ret);
    return ret;
482
}
483

484 485 486 487 488 489 490 491 492 493 494 495
/***********************************************************************
 *              DrawDibTime          [MSVFW32.@]
 */
BOOL VFWAPI DrawDibTime(HDRAWDIB hdd, LPDRAWDIBTIME lpddtime)
{
    FIXME("(%p, %p) stub\n", hdd, lpddtime);
    return FALSE;
}

/***********************************************************************
 *              DrawDibProfileDisplay          [MSVFW32.@]
 */
496 497 498 499 500 501
DWORD VFWAPI DrawDibProfileDisplay(LPBITMAPINFOHEADER lpbi)
{
    FIXME("(%p) stub\n", lpbi);

    return PD_CAN_DRAW_DIB;
}