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