| 1 | /* $Id: dibitmap.cpp,v 1.14 2001-01-14 17:17:45 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 <winuser32.h>
|
|---|
| 18 | #include "dibsect.h"
|
|---|
| 19 |
|
|---|
| 20 | #define DBG_LOCALLOG DBG_dibitmap
|
|---|
| 21 | #include "dbglocal.h"
|
|---|
| 22 |
|
|---|
| 23 | //******************************************************************************
|
|---|
| 24 | //******************************************************************************
|
|---|
| 25 | HBITMAP WIN32API CreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *lpbmih,
|
|---|
| 26 | DWORD fdwInit, const void *lpbInit,
|
|---|
| 27 | const BITMAPINFO *lpbmi, UINT fuUsage)
|
|---|
| 28 | {
|
|---|
| 29 | int iHeight;
|
|---|
| 30 | HBITMAP rc;
|
|---|
| 31 |
|
|---|
| 32 | //SvL: Completely wrong result when creating a 1bpp bitmap here (converted
|
|---|
| 33 | // to 8bpp by Open32)
|
|---|
| 34 | if(lpbmih->biBitCount == 1) {
|
|---|
| 35 | dprintf(("WARNING: CreateDIBitmap doesn't handle 1bpp bitmaps very well!!!!!"));
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | //TEMPORARY HACK TO PREVENT CRASH IN OPEN32 (WSeB GA)
|
|---|
| 39 |
|
|---|
| 40 | iHeight = lpbmih->biHeight;
|
|---|
| 41 | if(lpbmih->biHeight < 0)
|
|---|
| 42 | {
|
|---|
| 43 | dprintf(("GDI32: CreateDIBitmap negative height! (%d,%d)", lpbmih->biWidth, lpbmih->biHeight));
|
|---|
| 44 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = -lpbmih->biHeight;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // 2000/09/01 PH Netscape 4.7
|
|---|
| 48 | // If color depth of lpbhmi is 16 bit and lpbmi is 8 bit,
|
|---|
| 49 | // Open32 will crash since it won't allocate any palette color memory,
|
|---|
| 50 | // however wants to copy it later on ...
|
|---|
| 51 | int biBitCount = lpbmih->biBitCount;
|
|---|
| 52 |
|
|---|
| 53 | if (lpbmih->biBitCount != lpbmi->bmiHeader.biBitCount)
|
|---|
| 54 | {
|
|---|
| 55 | dprintf(("GDI32: CreateDIBitmap: color depths of bitmaps differ! (%d,%d\n",
|
|---|
| 56 | lpbmih->biBitCount,
|
|---|
| 57 | lpbmi->bmiHeader.biBitCount));
|
|---|
| 58 |
|
|---|
| 59 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = lpbmi->bmiHeader.biBitCount;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
|
|---|
| 63 |
|
|---|
| 64 | dprintf(("GDI32: CreateDIBitmap %x %x %x %x %x returned %x (%d,%d, bps %d)", hdc, lpbmih, fdwInit, lpbInit, fuUsage, rc, lpbmih->biWidth, lpbmih->biHeight, lpbmih->biBitCount));
|
|---|
| 65 |
|
|---|
| 66 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
|
|---|
| 67 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = biBitCount;
|
|---|
| 68 |
|
|---|
| 69 | return rc;
|
|---|
| 70 | }
|
|---|
| 71 | //******************************************************************************
|
|---|
| 72 | //******************************************************************************
|
|---|
| 73 | HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight)
|
|---|
| 74 | {
|
|---|
| 75 | HBITMAP hBitmap;
|
|---|
| 76 |
|
|---|
| 77 | hBitmap = O32_CreateCompatibleBitmap(hdc, nWidth, nHeight);
|
|---|
| 78 | dprintf(("GDI32: CreateCompatibleBitmap %x (%d,%d) returned %x", hdc, nWidth, nHeight, hBitmap));
|
|---|
| 79 | return hBitmap;
|
|---|
| 80 | }
|
|---|
| 81 | //******************************************************************************
|
|---|
| 82 | //CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap
|
|---|
| 83 | //******************************************************************************
|
|---|
| 84 | HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
|
|---|
| 85 | {
|
|---|
| 86 | dprintf(("GDI32: CreateDisardableBitmap\n"));
|
|---|
| 87 | return CreateCompatibleBitmap(hDC, nWidth, nHeight);
|
|---|
| 88 | }
|
|---|
| 89 | //******************************************************************************
|
|---|
| 90 | //******************************************************************************
|
|---|
| 91 | HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes,
|
|---|
| 92 | UINT cBitsPerPel, const void *lpvBits)
|
|---|
| 93 | {
|
|---|
| 94 | HBITMAP hBitmap;
|
|---|
| 95 |
|
|---|
| 96 | hBitmap = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
|
|---|
| 97 | dprintf(("GDI32: CreateBitmap (%d,%d) bps %d returned %x", nWidth, nHeight, cBitsPerPel, hBitmap));
|
|---|
| 98 | return(hBitmap);
|
|---|
| 99 | }
|
|---|
| 100 | //******************************************************************************
|
|---|
| 101 | //******************************************************************************
|
|---|
| 102 | HBITMAP WIN32API CreateBitmapIndirect( const BITMAP * arg1)
|
|---|
| 103 | {
|
|---|
| 104 | dprintf(("GDI32: CreateBitmapIndirect"));
|
|---|
| 105 | return O32_CreateBitmapIndirect(arg1);
|
|---|
| 106 | }
|
|---|
| 107 | //******************************************************************************
|
|---|
| 108 | //*********************************************************************************
|
|---|
| 109 | HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage,
|
|---|
| 110 | VOID **ppvBits, HANDLE hSection, DWORD dwOffset)
|
|---|
| 111 | {
|
|---|
| 112 | HBITMAP res = 0;
|
|---|
| 113 | BOOL fFlip = 0;
|
|---|
| 114 | int iHeight, iWidth;
|
|---|
| 115 | BOOL fCreateDC = FALSE;
|
|---|
| 116 |
|
|---|
| 117 | dprintf(("GDI32: CreateDIBSection %x %x %x %x %x %d", hdc, pbmi, iUsage, ppvBits, hSection, dwOffset));
|
|---|
| 118 |
|
|---|
| 119 | //SvL: 13-9-98: StarCraft uses bitmap with negative height
|
|---|
| 120 | iWidth = pbmi->bmiHeader.biWidth;
|
|---|
| 121 | if(pbmi->bmiHeader.biWidth < 0)
|
|---|
| 122 | {
|
|---|
| 123 | dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth));
|
|---|
| 124 | pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth;
|
|---|
| 125 | fFlip = FLIP_HOR;
|
|---|
| 126 | }
|
|---|
| 127 | iHeight = pbmi->bmiHeader.biHeight;
|
|---|
| 128 | if(pbmi->bmiHeader.biHeight < 0)
|
|---|
| 129 | {
|
|---|
| 130 | dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight));
|
|---|
| 131 | pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight;
|
|---|
| 132 | fFlip |= FLIP_VERT;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | //SvL: RP7 (update) calls this api with hdc == 0
|
|---|
| 136 | if(hdc == 0) {
|
|---|
| 137 | hdc = GetWindowDC(GetDesktopWindow());
|
|---|
| 138 | fCreateDC = TRUE;
|
|---|
| 139 | }
|
|---|
| 140 | res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage);
|
|---|
| 141 | if (res)
|
|---|
| 142 | {
|
|---|
| 143 | char PalSize;
|
|---|
| 144 | DIBSection *dsect;
|
|---|
| 145 |
|
|---|
| 146 | dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip);
|
|---|
| 147 |
|
|---|
| 148 | if(dsect != NULL)
|
|---|
| 149 | {
|
|---|
| 150 | PalSize = dsect->GetBitCount();
|
|---|
| 151 | if(PalSize <= 8)
|
|---|
| 152 | {
|
|---|
| 153 | ULONG Pal[256], nrcolors;
|
|---|
| 154 | LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}};
|
|---|
| 155 | HPALETTE hpalCur, hpalTmp;
|
|---|
| 156 |
|
|---|
| 157 | // Now get the current Palette from the DC
|
|---|
| 158 | hpalTmp = CreatePalette(&tmpPal);
|
|---|
| 159 | hpalCur = SelectPalette(hdc, hpalTmp, FALSE);
|
|---|
| 160 |
|
|---|
| 161 | // and use it to set the DIBColorTable
|
|---|
| 162 | nrcolors = GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal);
|
|---|
| 163 | dsect->SetDIBColorTable(0, nrcolors, (LPPALETTEENTRY)&Pal);
|
|---|
| 164 |
|
|---|
| 165 | // Restore the DC Palette
|
|---|
| 166 | SelectPalette(hdc,hpalCur,FALSE);
|
|---|
| 167 | DeleteObject(hpalTmp);
|
|---|
| 168 | }
|
|---|
| 169 | //SvL: Shouldn't an app explicitely select the dib section into the hdc?
|
|---|
| 170 | // (RealPlayer does this)
|
|---|
| 171 | #if 0
|
|---|
| 172 | // Set the hdc in the DIBSection so we can update the palete if a new
|
|---|
| 173 | // Palette etc. gets selected into the DC.
|
|---|
| 174 |
|
|---|
| 175 | dsect->SelectDIBObject(hdc);
|
|---|
| 176 | #endif
|
|---|
| 177 |
|
|---|
| 178 | if(ppvBits!=NULL)
|
|---|
| 179 | *ppvBits = dsect->GetDIBObject();
|
|---|
| 180 |
|
|---|
| 181 | pbmi->bmiHeader.biWidth = iWidth;
|
|---|
| 182 | pbmi->bmiHeader.biHeight = iHeight;
|
|---|
| 183 |
|
|---|
| 184 | if(fCreateDC) ReleaseDC(GetDesktopWindow(), hdc);
|
|---|
| 185 | return(res);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | if(fCreateDC) ReleaseDC(GetDesktopWindow(), hdc);
|
|---|
| 189 |
|
|---|
| 190 | /* Error. */
|
|---|
| 191 | if (res)
|
|---|
| 192 | DeleteObject(res);
|
|---|
| 193 | *ppvBits = NULL;
|
|---|
| 194 | #ifdef DEBUG
|
|---|
| 195 | dprintf(("GDI32: CreateDIBSection, error!\n"));
|
|---|
| 196 | dprintf(("pbmi->biWidth %d", pbmi->bmiHeader.biWidth));
|
|---|
| 197 | dprintf(("pbmi->biHeight %d", pbmi->bmiHeader.biHeight));
|
|---|
| 198 | dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount));
|
|---|
| 199 | #endif
|
|---|
| 200 |
|
|---|
| 201 | return 0;
|
|---|
| 202 | }
|
|---|
| 203 | //******************************************************************************
|
|---|
| 204 | //******************************************************************************
|
|---|
| 205 | UINT WIN32API GetDIBColorTable( HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 206 | RGBQUAD *pColors)
|
|---|
| 207 | {
|
|---|
| 208 | HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL);
|
|---|
| 209 | UINT rc;
|
|---|
| 210 | int i;
|
|---|
| 211 |
|
|---|
| 212 | rc = O32_GetPaletteEntries(hpal,
|
|---|
| 213 | uStartIndex,
|
|---|
| 214 | cEntries,
|
|---|
| 215 | (PALETTEENTRY *)pColors);
|
|---|
| 216 | for(i=0;
|
|---|
| 217 | i<cEntries;
|
|---|
| 218 | i++)
|
|---|
| 219 | {
|
|---|
| 220 | BYTE tmp;
|
|---|
| 221 | tmp = pColors[i].rgbBlue;
|
|---|
| 222 | pColors[i].rgbBlue = pColors[i].rgbRed;
|
|---|
| 223 | pColors[i].rgbRed = tmp;
|
|---|
| 224 | pColors[i].rgbReserved = 0;
|
|---|
| 225 | }
|
|---|
| 226 | dprintf(("GDI32: GetDIBColorTable returns %d\n", rc));
|
|---|
| 227 | return(rc);
|
|---|
| 228 | }
|
|---|
| 229 | //******************************************************************************
|
|---|
| 230 | //******************************************************************************
|
|---|
| 231 | UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 232 | RGBQUAD *pColors)
|
|---|
| 233 | {
|
|---|
| 234 | DIBSection *dsect = DIBSection::findHDC(hdc);
|
|---|
| 235 |
|
|---|
| 236 | dprintf(("GDI32: SetDIBColorTable\n"));
|
|---|
| 237 | if(dsect)
|
|---|
| 238 | {
|
|---|
| 239 | return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors));
|
|---|
| 240 | }
|
|---|
| 241 | else
|
|---|
| 242 | return(0);
|
|---|
| 243 | }
|
|---|
| 244 | //******************************************************************************
|
|---|
| 245 | //******************************************************************************
|
|---|
| 246 | LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID arg3)
|
|---|
| 247 | {
|
|---|
| 248 | dprintf(("GDI32: GetBitmapBits %x", hBitmap));
|
|---|
| 249 | return O32_GetBitmapBits(hBitmap, arg2, arg3);
|
|---|
| 250 | }
|
|---|
| 251 | //******************************************************************************
|
|---|
| 252 | //******************************************************************************
|
|---|
| 253 | LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID * arg3)
|
|---|
| 254 | {
|
|---|
| 255 | dprintf(("GDI32: SetBitmapBits %x", hBitmap));
|
|---|
| 256 | return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3);
|
|---|
| 257 | }
|
|---|
| 258 | //******************************************************************************
|
|---|
| 259 | //******************************************************************************
|
|---|
| 260 | BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize)
|
|---|
| 261 | {
|
|---|
| 262 | dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy));
|
|---|
| 263 | return O32_GetBitmapDimensionEx(hBitmap, pSize);
|
|---|
| 264 | }
|
|---|
| 265 | //******************************************************************************
|
|---|
| 266 | //******************************************************************************
|
|---|
| 267 | BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE arg4)
|
|---|
| 268 | {
|
|---|
| 269 | dprintf(("GDI32: SetBitmapDimensionEx"));
|
|---|
| 270 | return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4);
|
|---|
| 271 | }
|
|---|
| 272 | //******************************************************************************
|
|---|
| 273 | //******************************************************************************
|
|---|
| 274 | int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
|
|---|
| 275 | void *lpvBits, PBITMAPINFO lpbi, UINT uUsage)
|
|---|
| 276 | {
|
|---|
| 277 | int rc;
|
|---|
| 278 |
|
|---|
| 279 | rc = O32_GetDIBits(hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
|
|---|
| 280 | // set proper color masks!
|
|---|
| 281 | switch(lpbi->bmiHeader.biBitCount) {
|
|---|
| 282 | case 16: //RGB 565
|
|---|
| 283 | ((DWORD*)(lpbi->bmiColors))[0] = 0xF800;
|
|---|
| 284 | ((DWORD*)(lpbi->bmiColors))[1] = 0x07E0;
|
|---|
| 285 | ((DWORD*)(lpbi->bmiColors))[2] = 0x001F;
|
|---|
| 286 | break;
|
|---|
| 287 | case 24:
|
|---|
| 288 | case 32:
|
|---|
| 289 | ((DWORD*)(lpbi->bmiColors))[0] = 0x0000FF;
|
|---|
| 290 | ((DWORD*)(lpbi->bmiColors))[1] = 0x00FF00;
|
|---|
| 291 | ((DWORD*)(lpbi->bmiColors))[2] = 0xFF0000;
|
|---|
| 292 | break;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | dprintf(("GDI32: GetDIBits %x %x %d %d %x %x %d returned %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage, rc));
|
|---|
| 296 | return rc;
|
|---|
| 297 | }
|
|---|
| 298 | //******************************************************************************
|
|---|
| 299 | //******************************************************************************
|
|---|
| 300 | int WIN32API SetDIBits(HDC hdc, HBITMAP hBitmap, UINT startscan, UINT numlines, const VOID *pBits,
|
|---|
| 301 | const BITMAPINFO *pBitmapInfo, UINT usage)
|
|---|
| 302 | {
|
|---|
| 303 | dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x", hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage));
|
|---|
| 304 |
|
|---|
| 305 | if(DIBSection::getSection() != NULL)
|
|---|
| 306 | {
|
|---|
| 307 | DIBSection *dsect;
|
|---|
| 308 |
|
|---|
| 309 | dsect = DIBSection::find((DWORD)hBitmap);
|
|---|
| 310 | if(dsect) {
|
|---|
| 311 | return dsect->SetDIBits(hdc, hBitmap, startscan, numlines, pBits, (BITMAPINFOHEADER_W *)&pBitmapInfo->bmiHeader, usage);
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 | //SvL: Open32's SetDIBits really messes things up for 1 bpp bitmaps, must use SetBitmapBits
|
|---|
| 315 | if(pBitmapInfo->bmiHeader.biBitCount == 1 && startscan == 0 && numlines == pBitmapInfo->bmiHeader.biHeight)
|
|---|
| 316 | {//WARNING: hack alert!
|
|---|
| 317 | int dibwidth = DIB_GetDIBWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
|
|---|
| 318 | int bmpwidth = BITMAP_GetWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
|
|---|
| 319 | char *newpix = (char *)malloc(dibwidth*pBitmapInfo->bmiHeader.biHeight);
|
|---|
| 320 | char *orgpix = (char *)pBits;
|
|---|
| 321 | int ret;
|
|---|
| 322 |
|
|---|
| 323 | dprintf(("Flipping 1bpp bitmap and calling SetBitmapBits (WORKAROUND) (%d -> %d)", dibwidth, bmpwidth));
|
|---|
| 324 | newpix += ((pBitmapInfo->bmiHeader.biHeight-1)*bmpwidth);
|
|---|
| 325 |
|
|---|
| 326 | //flip bitmap here; SetDIBits assumes origin is left bottom, SetBitmapBits left top
|
|---|
| 327 | //SetDIBits assumes DWORD aligned data
|
|---|
| 328 | //SetBitmapBits assumes WORD aligned data
|
|---|
| 329 | for(int i=0;i<pBitmapInfo->bmiHeader.biHeight;i++) {
|
|---|
| 330 | memcpy(newpix, orgpix, bmpwidth);
|
|---|
| 331 |
|
|---|
| 332 | newpix -= bmpwidth;
|
|---|
| 333 | orgpix += dibwidth;
|
|---|
| 334 | }
|
|---|
| 335 | newpix += bmpwidth;
|
|---|
| 336 | ret = O32_SetBitmapBits(hBitmap, pBitmapInfo->bmiHeader.biSizeImage, newpix);
|
|---|
| 337 |
|
|---|
| 338 | free(newpix);
|
|---|
| 339 | return ret;
|
|---|
| 340 | }
|
|---|
| 341 | #ifdef DEBUG
|
|---|
| 342 | if(pBitmapInfo->bmiHeader.biBitCount == 1) {
|
|---|
| 343 | dprintf(("ERROR: SetDIBits does NOT work well for 1 bpp bitmaps!!!!!"));
|
|---|
| 344 | }
|
|---|
| 345 | #endif
|
|---|
| 346 | return O32_SetDIBits(hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage);
|
|---|
| 347 | }
|
|---|
| 348 | //******************************************************************************
|
|---|
| 349 | //******************************************************************************
|
|---|