| 1 | /* $Id: dibitmap.cpp,v 1.9 2000-09-01 01:36:14 phaller Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * GDI32 dib & bitmap code
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 1998 Patrick Haller
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 | #include <os2win.h>
|
|---|
| 13 | #include <stdlib.h>
|
|---|
| 14 | #include <stdarg.h>
|
|---|
| 15 | #include <string.h>
|
|---|
| 16 | #include "misc.h"
|
|---|
| 17 | #include "dibsect.h"
|
|---|
| 18 |
|
|---|
| 19 | #define DBG_LOCALLOG DBG_dibitmap
|
|---|
| 20 | #include "dbglocal.h"
|
|---|
| 21 |
|
|---|
| 22 | //******************************************************************************
|
|---|
| 23 | //******************************************************************************
|
|---|
| 24 | HBITMAP WIN32API CreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *lpbmih,
|
|---|
| 25 | DWORD fdwInit, const void *lpbInit,
|
|---|
| 26 | const BITMAPINFO *lpbmi, UINT fuUsage)
|
|---|
| 27 | {
|
|---|
| 28 | int iHeight;
|
|---|
| 29 | HBITMAP rc;
|
|---|
| 30 |
|
|---|
| 31 | //TEMPORARY HACK TO PREVENT CRASH IN OPEN32 (WSeB GA)
|
|---|
| 32 |
|
|---|
| 33 | iHeight = lpbmih->biHeight;
|
|---|
| 34 | if(lpbmih->biHeight < 0)
|
|---|
| 35 | {
|
|---|
| 36 | dprintf(("GDI32: CreateDIBitmap negative height! (%d,%d)", lpbmih->biWidth, lpbmih->biHeight));
|
|---|
| 37 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = -lpbmih->biHeight;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // 2000/09/01 PH Netscape 4.7
|
|---|
| 41 | // If color depth of lpbhmi is 16 bit and lpbmi is 8 bit,
|
|---|
| 42 | // Open32 will crash since it won't allocate any palette color memory,
|
|---|
| 43 | // however wants to copy it later on ...
|
|---|
| 44 | int biBitCount = lpbmih->biBitCount;
|
|---|
| 45 |
|
|---|
| 46 | if (lpbmih->biBitCount != lpbmi->bmiHeader.biBitCount)
|
|---|
| 47 | {
|
|---|
| 48 | dprintf(("GDI32: CreateDIBitmap: color depths of bitmaps differ! (%d,%d\n",
|
|---|
| 49 | lpbmih->biBitCount,
|
|---|
| 50 | lpbmi->bmiHeader.biBitCount));
|
|---|
| 51 |
|
|---|
| 52 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = lpbmi->bmiHeader.biBitCount;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
|
|---|
| 56 |
|
|---|
| 57 | dprintf(("GDI32: CreateDIBitmap %x %x %x %x returned %x", hdc, fdwInit, lpbInit, fuUsage, rc));
|
|---|
| 58 |
|
|---|
| 59 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
|
|---|
| 60 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = biBitCount;
|
|---|
| 61 |
|
|---|
| 62 | return rc;
|
|---|
| 63 | }
|
|---|
| 64 | //******************************************************************************
|
|---|
| 65 | //******************************************************************************
|
|---|
| 66 | HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight)
|
|---|
| 67 | {
|
|---|
| 68 | HBITMAP hBitmap;
|
|---|
| 69 |
|
|---|
| 70 | hBitmap = O32_CreateCompatibleBitmap(hdc, nWidth, nHeight);
|
|---|
| 71 | dprintf(("GDI32: CreateCompatibleBitmap %x (%d,%d) returned %x", hdc, nWidth, nHeight, hBitmap));
|
|---|
| 72 | return hBitmap;
|
|---|
| 73 | }
|
|---|
| 74 | //******************************************************************************
|
|---|
| 75 | //CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap
|
|---|
| 76 | //******************************************************************************
|
|---|
| 77 | HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
|
|---|
| 78 | {
|
|---|
| 79 | dprintf(("GDI32: CreateDisardableBitmap\n"));
|
|---|
| 80 | return O32_CreateCompatibleBitmap(hDC, nWidth, nHeight);
|
|---|
| 81 | }
|
|---|
| 82 | //******************************************************************************
|
|---|
| 83 | //******************************************************************************
|
|---|
| 84 | HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes,
|
|---|
| 85 | UINT cBitsPerPel, const void *lpvBits)
|
|---|
| 86 | {
|
|---|
| 87 | HBITMAP hBitmap;
|
|---|
| 88 |
|
|---|
| 89 | hBitmap = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
|
|---|
| 90 | dprintf(("GDI32: CreateBitmap (%d,%d) bps %d returned %x", nWidth, nHeight, cBitsPerPel, hBitmap));
|
|---|
| 91 | return(hBitmap);
|
|---|
| 92 | }
|
|---|
| 93 | //******************************************************************************
|
|---|
| 94 | //******************************************************************************
|
|---|
| 95 | HBITMAP WIN32API CreateBitmapIndirect( const BITMAP * arg1)
|
|---|
| 96 | {
|
|---|
| 97 | dprintf(("GDI32: CreateBitmapIndirect"));
|
|---|
| 98 | return O32_CreateBitmapIndirect(arg1);
|
|---|
| 99 | }
|
|---|
| 100 | //******************************************************************************
|
|---|
| 101 | //*********************************************************************************
|
|---|
| 102 | HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage,
|
|---|
| 103 | VOID **ppvBits, HANDLE hSection, DWORD dwOffset)
|
|---|
| 104 | {
|
|---|
| 105 | HBITMAP res = 0;
|
|---|
| 106 | BOOL fFlip = 0;
|
|---|
| 107 | int iHeight, iWidth;
|
|---|
| 108 | BOOL fCreateDC = FALSE;
|
|---|
| 109 |
|
|---|
| 110 | dprintf(("GDI32: CreateDIBSection %x %x %x %x %x %d", hdc, pbmi, iUsage, ppvBits, hSection, dwOffset));
|
|---|
| 111 |
|
|---|
| 112 | //SvL: 13-9-98: StarCraft uses bitmap with negative height
|
|---|
| 113 | iWidth = pbmi->bmiHeader.biWidth;
|
|---|
| 114 | if(pbmi->bmiHeader.biWidth < 0)
|
|---|
| 115 | {
|
|---|
| 116 | dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth));
|
|---|
| 117 | pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth;
|
|---|
| 118 | fFlip = FLIP_HOR;
|
|---|
| 119 | }
|
|---|
| 120 | iHeight = pbmi->bmiHeader.biHeight;
|
|---|
| 121 | if(pbmi->bmiHeader.biHeight < 0)
|
|---|
| 122 | {
|
|---|
| 123 | dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight));
|
|---|
| 124 | pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight;
|
|---|
| 125 | fFlip |= FLIP_VERT;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | //SvL: RP7 (update) calls this api with hdc == 0
|
|---|
| 129 | if(hdc == 0) {
|
|---|
| 130 | hdc = GetWindowDC(GetDesktopWindow());
|
|---|
| 131 | fCreateDC = TRUE;
|
|---|
| 132 | }
|
|---|
| 133 | res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage);
|
|---|
| 134 | if (res)
|
|---|
| 135 | {
|
|---|
| 136 | char PalSize;
|
|---|
| 137 | DIBSection *dsect;
|
|---|
| 138 |
|
|---|
| 139 | dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip);
|
|---|
| 140 |
|
|---|
| 141 | if(dsect != NULL)
|
|---|
| 142 | {
|
|---|
| 143 | PalSize = dsect->GetBitCount();
|
|---|
| 144 | if(PalSize <= 8)
|
|---|
| 145 | {
|
|---|
| 146 | ULONG Pal[256], nrcolors;
|
|---|
| 147 | LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}};
|
|---|
| 148 | HPALETTE hpalCur, hpalTmp;
|
|---|
| 149 |
|
|---|
| 150 | // Now get the current Palette from the DC
|
|---|
| 151 | hpalTmp = CreatePalette(&tmpPal);
|
|---|
| 152 | hpalCur = SelectPalette(hdc, hpalTmp, FALSE);
|
|---|
| 153 |
|
|---|
| 154 | // and use it to set the DIBColorTable
|
|---|
| 155 | nrcolors = GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal);
|
|---|
| 156 | dsect->SetDIBColorTable(0, nrcolors, (RGBQUAD*)&Pal);
|
|---|
| 157 |
|
|---|
| 158 | // Restore the DC Palette
|
|---|
| 159 | SelectPalette(hdc,hpalCur,FALSE);
|
|---|
| 160 | DeleteObject(hpalTmp);
|
|---|
| 161 | }
|
|---|
| 162 | //SvL: Shouldn't an app explicitely select the dib section into the hdc?
|
|---|
| 163 | // (RealPlayer does this)
|
|---|
| 164 | #if 0
|
|---|
| 165 | // Set the hdc in the DIBSection so we can update the palete if a new
|
|---|
| 166 | // Palette etc. gets selected into the DC.
|
|---|
| 167 |
|
|---|
| 168 | dsect->SelectDIBObject(hdc);
|
|---|
| 169 | #endif
|
|---|
| 170 |
|
|---|
| 171 | if(ppvBits!=NULL)
|
|---|
| 172 | *ppvBits = dsect->GetDIBObject();
|
|---|
| 173 |
|
|---|
| 174 | pbmi->bmiHeader.biWidth = iWidth;
|
|---|
| 175 | pbmi->bmiHeader.biHeight = iHeight;
|
|---|
| 176 |
|
|---|
| 177 | if(fCreateDC) ReleaseDC(GetDesktopWindow(), hdc);
|
|---|
| 178 | return(res);
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | if(fCreateDC) ReleaseDC(GetDesktopWindow(), hdc);
|
|---|
| 182 |
|
|---|
| 183 | /* Error. */
|
|---|
| 184 | if (res)
|
|---|
| 185 | DeleteObject(res);
|
|---|
| 186 | *ppvBits = NULL;
|
|---|
| 187 | #ifdef DEBUG
|
|---|
| 188 | dprintf(("GDI32: CreateDIBSection, error!\n"));
|
|---|
| 189 | dprintf(("pbmi->biWidth %d", pbmi->bmiHeader.biWidth));
|
|---|
| 190 | dprintf(("pbmi->biHeight %d", pbmi->bmiHeader.biHeight));
|
|---|
| 191 | dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount));
|
|---|
| 192 | #endif
|
|---|
| 193 |
|
|---|
| 194 | return 0;
|
|---|
| 195 | }
|
|---|
| 196 | //******************************************************************************
|
|---|
| 197 | //******************************************************************************
|
|---|
| 198 | UINT WIN32API GetDIBColorTable( HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 199 | RGBQUAD *pColors)
|
|---|
| 200 | {
|
|---|
| 201 | HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL);
|
|---|
| 202 | UINT rc;
|
|---|
| 203 | int i;
|
|---|
| 204 |
|
|---|
| 205 | rc = O32_GetPaletteEntries(hpal,
|
|---|
| 206 | uStartIndex,
|
|---|
| 207 | cEntries,
|
|---|
| 208 | (PALETTEENTRY *)pColors);
|
|---|
| 209 | for(i=0;
|
|---|
| 210 | i<cEntries;
|
|---|
| 211 | i++)
|
|---|
| 212 | {
|
|---|
| 213 | BYTE tmp;
|
|---|
| 214 | tmp = pColors[i].rgbBlue;
|
|---|
| 215 | pColors[i].rgbBlue = pColors[i].rgbRed;
|
|---|
| 216 | pColors[i].rgbRed = tmp;
|
|---|
| 217 | pColors[i].rgbReserved = 0;
|
|---|
| 218 | }
|
|---|
| 219 | dprintf(("GDI32: GetDIBColorTable returns %d\n", rc));
|
|---|
| 220 | return(rc);
|
|---|
| 221 | }
|
|---|
| 222 | //******************************************************************************
|
|---|
| 223 | //******************************************************************************
|
|---|
| 224 | UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 225 | RGBQUAD *pColors)
|
|---|
| 226 | {
|
|---|
| 227 | DIBSection *dsect = DIBSection::findHDC(hdc);
|
|---|
| 228 |
|
|---|
| 229 | dprintf(("GDI32: SetDIBColorTable\n"));
|
|---|
| 230 | if(dsect)
|
|---|
| 231 | {
|
|---|
| 232 | return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors));
|
|---|
| 233 | }
|
|---|
| 234 | else
|
|---|
| 235 | return(0);
|
|---|
| 236 | }
|
|---|
| 237 | //******************************************************************************
|
|---|
| 238 | //******************************************************************************
|
|---|
| 239 | LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID arg3)
|
|---|
| 240 | {
|
|---|
| 241 | dprintf(("GDI32: GetBitmapBits %x", hBitmap));
|
|---|
| 242 | return O32_GetBitmapBits(hBitmap, arg2, arg3);
|
|---|
| 243 | }
|
|---|
| 244 | //******************************************************************************
|
|---|
| 245 | //******************************************************************************
|
|---|
| 246 | LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID * arg3)
|
|---|
| 247 | {
|
|---|
| 248 | dprintf(("GDI32: SetBitmapBits %x", hBitmap));
|
|---|
| 249 | return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3);
|
|---|
| 250 | }
|
|---|
| 251 | //******************************************************************************
|
|---|
| 252 | //******************************************************************************
|
|---|
| 253 | BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize)
|
|---|
| 254 | {
|
|---|
| 255 | dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy));
|
|---|
| 256 | return O32_GetBitmapDimensionEx(hBitmap, pSize);
|
|---|
| 257 | }
|
|---|
| 258 | //******************************************************************************
|
|---|
| 259 | //******************************************************************************
|
|---|
| 260 | BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE arg4)
|
|---|
| 261 | {
|
|---|
| 262 | dprintf(("GDI32: SetBitmapDimensionEx"));
|
|---|
| 263 | return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4);
|
|---|
| 264 | }
|
|---|
| 265 | //******************************************************************************
|
|---|
| 266 | //******************************************************************************
|
|---|
| 267 | int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
|
|---|
| 268 | void *lpvBits, PBITMAPINFO lpbi, UINT uUsage)
|
|---|
| 269 | {
|
|---|
| 270 | int rc;
|
|---|
| 271 |
|
|---|
| 272 | rc = O32_GetDIBits(hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
|
|---|
| 273 | dprintf(("GDI32: GetDIBits %x %x %d %d %x %x %d returned %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage, rc));
|
|---|
| 274 | return rc;
|
|---|
| 275 | }
|
|---|
| 276 | //******************************************************************************
|
|---|
| 277 | //******************************************************************************
|
|---|
| 278 | int WIN32API SetDIBits( HDC arg1, HBITMAP arg2, UINT arg3, UINT arg4, const VOID * arg5, const BITMAPINFO * arg6, UINT arg7)
|
|---|
| 279 | {
|
|---|
| 280 | dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x\n", arg1, arg2, arg3, arg4, arg5, arg6, arg7));
|
|---|
| 281 |
|
|---|
| 282 | if(DIBSection::getSection() != NULL)
|
|---|
| 283 | {
|
|---|
| 284 | DIBSection *dsect;
|
|---|
| 285 |
|
|---|
| 286 | dsect = DIBSection::find((DWORD)arg2);
|
|---|
| 287 | if(dsect) {
|
|---|
| 288 | return dsect->SetDIBits(arg1, arg2, arg3, arg4, arg5, (BITMAPINFOHEADER_W *)&arg6->bmiHeader, arg7);
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 | return O32_SetDIBits(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
|
|---|
| 292 | }
|
|---|
| 293 | //******************************************************************************
|
|---|
| 294 | //******************************************************************************
|
|---|