| 1 | /* $Id: dibitmap.cpp,v 1.5 2000-03-16 19:18:14 sandervl 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 | rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage); | 
|---|
| 41 |  | 
|---|
| 42 | dprintf(("GDI32: CreateDIBitmap %x %x %x %x returned %x", hdc, fdwInit, lpbInit, fuUsage, rc)); | 
|---|
| 43 |  | 
|---|
| 44 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight; | 
|---|
| 45 |  | 
|---|
| 46 | return rc; | 
|---|
| 47 | } | 
|---|
| 48 | //****************************************************************************** | 
|---|
| 49 | //****************************************************************************** | 
|---|
| 50 | HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight) | 
|---|
| 51 | { | 
|---|
| 52 | dprintf(("GDI32: CreateCompatibleBitmap %x (%d,%d)", hdc, nWidth, nHeight)); | 
|---|
| 53 | return O32_CreateCompatibleBitmap(hdc, nWidth, nHeight); | 
|---|
| 54 | } | 
|---|
| 55 | //****************************************************************************** | 
|---|
| 56 | //CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap | 
|---|
| 57 | //****************************************************************************** | 
|---|
| 58 | HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight) | 
|---|
| 59 | { | 
|---|
| 60 | dprintf(("GDI32: CreateDisardableBitmap\n")); | 
|---|
| 61 | return O32_CreateCompatibleBitmap(hDC, nWidth, nHeight); | 
|---|
| 62 | } | 
|---|
| 63 | //****************************************************************************** | 
|---|
| 64 | //****************************************************************************** | 
|---|
| 65 | HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes, | 
|---|
| 66 | UINT cBitsPerPel, const void *lpvBits) | 
|---|
| 67 | { | 
|---|
| 68 | HBITMAP rc; | 
|---|
| 69 |  | 
|---|
| 70 | rc = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits); | 
|---|
| 71 | dprintf(("GDI32: CreateBitmap (%d,%d) bps %d returned %d\n", nWidth, nHeight, cBitsPerPel, rc)); | 
|---|
| 72 | return(rc); | 
|---|
| 73 | } | 
|---|
| 74 | //****************************************************************************** | 
|---|
| 75 | //****************************************************************************** | 
|---|
| 76 | HBITMAP WIN32API CreateBitmapIndirect( const BITMAP * arg1) | 
|---|
| 77 | { | 
|---|
| 78 | dprintf(("GDI32: CreateBitmapIndirect")); | 
|---|
| 79 | return O32_CreateBitmapIndirect(arg1); | 
|---|
| 80 | } | 
|---|
| 81 | //****************************************************************************** | 
|---|
| 82 | //********************************************************************************* | 
|---|
| 83 | HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage, | 
|---|
| 84 | VOID **ppvBits, HANDLE hSection, DWORD dwOffset) | 
|---|
| 85 | { | 
|---|
| 86 | HBITMAP res = 0; | 
|---|
| 87 | BOOL    fFlip = 0; | 
|---|
| 88 | int     iHeight, iWidth; | 
|---|
| 89 | BOOL    fCreateDC = FALSE; | 
|---|
| 90 |  | 
|---|
| 91 | dprintf(("GDI32: CreateDIBSection %x %x %x %x %d", hdc, iUsage, ppvBits, hSection, dwOffset)); | 
|---|
| 92 |  | 
|---|
| 93 | //SvL: 13-9-98: StarCraft uses bitmap with negative height | 
|---|
| 94 | iWidth = pbmi->bmiHeader.biWidth; | 
|---|
| 95 | if(pbmi->bmiHeader.biWidth < 0) | 
|---|
| 96 | { | 
|---|
| 97 | dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth)); | 
|---|
| 98 | pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth; | 
|---|
| 99 | fFlip = FLIP_HOR; | 
|---|
| 100 | } | 
|---|
| 101 | iHeight = pbmi->bmiHeader.biHeight; | 
|---|
| 102 | if(pbmi->bmiHeader.biHeight < 0) | 
|---|
| 103 | { | 
|---|
| 104 | dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight)); | 
|---|
| 105 | pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight; | 
|---|
| 106 | fFlip |= FLIP_VERT; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | //SvL: RP7 (update) calls this api with hdc == 0 | 
|---|
| 110 | if(hdc == 0) { | 
|---|
| 111 | hdc = GetWindowDC(GetDesktopWindow()); | 
|---|
| 112 | fCreateDC = TRUE; | 
|---|
| 113 | } | 
|---|
| 114 | res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage); | 
|---|
| 115 | if (res) | 
|---|
| 116 | { | 
|---|
| 117 | char PalSize; | 
|---|
| 118 | DIBSection *dsect; | 
|---|
| 119 |  | 
|---|
| 120 | dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip); | 
|---|
| 121 |  | 
|---|
| 122 | if(dsect != NULL) | 
|---|
| 123 | { | 
|---|
| 124 | PalSize = dsect->GetBitCount(); | 
|---|
| 125 | if(PalSize <= 8) | 
|---|
| 126 | { | 
|---|
| 127 | ULONG Pal[256]; | 
|---|
| 128 | LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}}; | 
|---|
| 129 | HPALETTE hpalCur, hpalTmp; | 
|---|
| 130 |  | 
|---|
| 131 | // Now get the current Palette from the DC | 
|---|
| 132 | hpalTmp = CreatePalette(&tmpPal); | 
|---|
| 133 | hpalCur = SelectPalette(hdc, hpalTmp, FALSE); | 
|---|
| 134 |  | 
|---|
| 135 | // and use it to set the DIBColorTable | 
|---|
| 136 | GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal); | 
|---|
| 137 | dsect->SetDIBColorTable(0, 1<< PalSize, (RGBQUAD*)&Pal); | 
|---|
| 138 |  | 
|---|
| 139 | // Restore the DC Palette | 
|---|
| 140 | SelectPalette(hdc,hpalCur,FALSE); | 
|---|
| 141 | DeleteObject(hpalTmp); | 
|---|
| 142 | } | 
|---|
| 143 | //SvL: Shouldn't an app explicitely select the dib section into the hdc? | 
|---|
| 144 | //     (RealPlayer does this) | 
|---|
| 145 | #if 0 | 
|---|
| 146 | // Set the hdc in the DIBSection so we can update the palete if a new | 
|---|
| 147 | // Palette etc. gets selected into the DC. | 
|---|
| 148 |  | 
|---|
| 149 | dsect->SelectDIBObject(hdc); | 
|---|
| 150 | #endif | 
|---|
| 151 |  | 
|---|
| 152 | if(ppvBits!=NULL) | 
|---|
| 153 | *ppvBits = dsect->GetDIBObject(); | 
|---|
| 154 |  | 
|---|
| 155 | pbmi->bmiHeader.biWidth = iWidth; | 
|---|
| 156 | pbmi->bmiHeader.biHeight = iHeight; | 
|---|
| 157 |  | 
|---|
| 158 | if(fCreateDC)     ReleaseDC(GetDesktopWindow(), hdc); | 
|---|
| 159 | return(res); | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 | if(fCreateDC) ReleaseDC(GetDesktopWindow(), hdc); | 
|---|
| 163 |  | 
|---|
| 164 | /* Error.  */ | 
|---|
| 165 | if (res) | 
|---|
| 166 | DeleteObject(res); | 
|---|
| 167 | *ppvBits = NULL; | 
|---|
| 168 | #ifdef DEBUG | 
|---|
| 169 | dprintf(("GDI32: CreateDIBSection, error!\n")); | 
|---|
| 170 | dprintf(("pbmi->biWidth    %d", pbmi->bmiHeader.biWidth)); | 
|---|
| 171 | dprintf(("pbmi->biHeight   %d", pbmi->bmiHeader.biHeight)); | 
|---|
| 172 | dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount)); | 
|---|
| 173 | #endif | 
|---|
| 174 |  | 
|---|
| 175 | return 0; | 
|---|
| 176 | } | 
|---|
| 177 | //****************************************************************************** | 
|---|
| 178 | //****************************************************************************** | 
|---|
| 179 | UINT WIN32API GetDIBColorTable( HDC hdc, UINT uStartIndex, UINT cEntries, | 
|---|
| 180 | RGBQUAD *pColors) | 
|---|
| 181 | { | 
|---|
| 182 | HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL); | 
|---|
| 183 | UINT rc; | 
|---|
| 184 | int i; | 
|---|
| 185 |  | 
|---|
| 186 | rc = O32_GetPaletteEntries(hpal, | 
|---|
| 187 | uStartIndex, | 
|---|
| 188 | cEntries, | 
|---|
| 189 | (PALETTEENTRY *)pColors); | 
|---|
| 190 | for(i=0; | 
|---|
| 191 | i<cEntries; | 
|---|
| 192 | i++) | 
|---|
| 193 | { | 
|---|
| 194 | BYTE tmp; | 
|---|
| 195 | tmp = pColors[i].rgbBlue; | 
|---|
| 196 | pColors[i].rgbBlue = pColors[i].rgbRed; | 
|---|
| 197 | pColors[i].rgbRed = tmp; | 
|---|
| 198 | pColors[i].rgbReserved = 0; | 
|---|
| 199 | } | 
|---|
| 200 | dprintf(("GDI32: GetDIBColorTable returns %d\n", rc)); | 
|---|
| 201 | return(rc); | 
|---|
| 202 | } | 
|---|
| 203 | //****************************************************************************** | 
|---|
| 204 | //****************************************************************************** | 
|---|
| 205 | UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries, | 
|---|
| 206 | RGBQUAD *pColors) | 
|---|
| 207 | { | 
|---|
| 208 | DIBSection *dsect = DIBSection::findHDC(hdc); | 
|---|
| 209 |  | 
|---|
| 210 | dprintf(("GDI32: SetDIBColorTable\n")); | 
|---|
| 211 | if(dsect) | 
|---|
| 212 | { | 
|---|
| 213 | return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors)); | 
|---|
| 214 | } | 
|---|
| 215 | else | 
|---|
| 216 | return(0); | 
|---|
| 217 | } | 
|---|
| 218 | //****************************************************************************** | 
|---|
| 219 | //****************************************************************************** | 
|---|
| 220 | LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID  arg3) | 
|---|
| 221 | { | 
|---|
| 222 | dprintf(("GDI32: GetBitmapBits %x", hBitmap)); | 
|---|
| 223 | return O32_GetBitmapBits(hBitmap, arg2, arg3); | 
|---|
| 224 | } | 
|---|
| 225 | //****************************************************************************** | 
|---|
| 226 | //****************************************************************************** | 
|---|
| 227 | LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID *  arg3) | 
|---|
| 228 | { | 
|---|
| 229 | dprintf(("GDI32: SetBitmapBits %x", hBitmap)); | 
|---|
| 230 | return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3); | 
|---|
| 231 | } | 
|---|
| 232 | //****************************************************************************** | 
|---|
| 233 | //****************************************************************************** | 
|---|
| 234 | BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize) | 
|---|
| 235 | { | 
|---|
| 236 | dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy)); | 
|---|
| 237 | return O32_GetBitmapDimensionEx(hBitmap, pSize); | 
|---|
| 238 | } | 
|---|
| 239 | //****************************************************************************** | 
|---|
| 240 | //****************************************************************************** | 
|---|
| 241 | BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE  arg4) | 
|---|
| 242 | { | 
|---|
| 243 | dprintf(("GDI32: SetBitmapDimensionEx")); | 
|---|
| 244 | return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4); | 
|---|
| 245 | } | 
|---|
| 246 | //****************************************************************************** | 
|---|
| 247 | //****************************************************************************** | 
|---|
| 248 | int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines, | 
|---|
| 249 | void *lpvBits, PBITMAPINFO lpbi, UINT uUsage) | 
|---|
| 250 | { | 
|---|
| 251 | int rc; | 
|---|
| 252 |  | 
|---|
| 253 | rc =  O32_GetDIBits(hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage); | 
|---|
| 254 | dprintf(("GDI32: GetDIBits %x %x %d %d %x %x %d returned %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage, rc)); | 
|---|
| 255 | return rc; | 
|---|
| 256 | } | 
|---|
| 257 | //****************************************************************************** | 
|---|
| 258 | //****************************************************************************** | 
|---|
| 259 | int WIN32API SetDIBits( HDC arg1, HBITMAP arg2, UINT arg3, UINT arg4, const VOID * arg5, const BITMAPINFO * arg6, UINT  arg7) | 
|---|
| 260 | { | 
|---|
| 261 | dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x\n", arg1, arg2, arg3, arg4, arg5, arg6, arg7)); | 
|---|
| 262 |  | 
|---|
| 263 | if(DIBSection::getSection() != NULL) | 
|---|
| 264 | { | 
|---|
| 265 | DIBSection *dsect; | 
|---|
| 266 |  | 
|---|
| 267 | dsect = DIBSection::find((DWORD)arg2); | 
|---|
| 268 | if(dsect) { | 
|---|
| 269 | return dsect->SetDIBits(arg1, arg2, arg3, arg4, arg5, (BITMAPINFOHEADER_W *)&arg6->bmiHeader, arg7); | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 | return O32_SetDIBits(arg1, arg2, arg3, arg4, arg5, arg6, arg7); | 
|---|
| 273 | } | 
|---|
| 274 | //****************************************************************************** | 
|---|
| 275 | //****************************************************************************** | 
|---|