| 1 | /* $Id: dibitmap.cpp,v 1.35 2002-11-26 10:53: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 <cpuhlp.h>
|
|---|
| 18 | #include <winuser32.h>
|
|---|
| 19 | #include "dibsect.h"
|
|---|
| 20 | #include "rgbcvt.h"
|
|---|
| 21 | #include <stats.h>
|
|---|
| 22 | #include "oslibgpi.h"
|
|---|
| 23 | #include <objhandle.h>
|
|---|
| 24 |
|
|---|
| 25 | #define DBG_LOCALLOG DBG_dibitmap
|
|---|
| 26 | #include "dbglocal.h"
|
|---|
| 27 |
|
|---|
| 28 | ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy);
|
|---|
| 29 |
|
|---|
| 30 | //******************************************************************************
|
|---|
| 31 | //******************************************************************************
|
|---|
| 32 | HBITMAP WIN32API CreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *lpbmih,
|
|---|
| 33 | DWORD fdwInit, const void *lpbInit,
|
|---|
| 34 | const BITMAPINFO *lpbmi, UINT fuUsage)
|
|---|
| 35 | {
|
|---|
| 36 | int iHeight;
|
|---|
| 37 | HBITMAP rc;
|
|---|
| 38 | DWORD bitfields[3];
|
|---|
| 39 | WORD *newbits = NULL;
|
|---|
| 40 |
|
|---|
| 41 | //SvL: Completely wrong result when creating a 1bpp bitmap here (converted
|
|---|
| 42 | // to 8bpp by Open32)
|
|---|
| 43 | if(lpbmih->biBitCount == 1) {
|
|---|
| 44 | dprintf(("WARNING: CreateDIBitmap doesn't handle 1bpp bitmaps very well!!!!!"));
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | //TEMPORARY HACK TO PREVENT CRASH IN OPEN32 (WSeB GA)
|
|---|
| 48 | iHeight = lpbmih->biHeight;
|
|---|
| 49 | if(lpbmih->biHeight < 0)
|
|---|
| 50 | {
|
|---|
| 51 | dprintf(("GDI32: CreateDIBitmap negative height! (%d,%d)", lpbmih->biWidth, lpbmih->biHeight));
|
|---|
| 52 | //TODO: doesn't work if memory is readonly!!
|
|---|
| 53 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = -lpbmih->biHeight;
|
|---|
| 54 |
|
|---|
| 55 | if(lpbInit && fdwInit == CBM_INIT) {
|
|---|
| 56 | // upside down
|
|---|
| 57 | HBITMAP rc = 0;
|
|---|
| 58 | long lLineByte = DIB_GetDIBWidthBytes(lpbmih->biWidth, lpbmih->biBitCount);
|
|---|
| 59 | long lHeight = lpbmih->biHeight;
|
|---|
| 60 |
|
|---|
| 61 | newbits = (WORD *)malloc( lLineByte * lHeight );
|
|---|
| 62 | if(newbits) {
|
|---|
| 63 | unsigned char *pbSrc = (unsigned char *)lpbInit + lLineByte * (lHeight - 1);
|
|---|
| 64 | unsigned char *pbDst = (unsigned char *)newbits;
|
|---|
| 65 | for(int y = 0; y < lHeight; y++) {
|
|---|
| 66 | memcpy( pbDst, pbSrc, lLineByte );
|
|---|
| 67 | pbDst += lLineByte;
|
|---|
| 68 | pbSrc -= lLineByte;
|
|---|
| 69 | }
|
|---|
| 70 | rc = CreateDIBitmap(hdc, lpbmih, fdwInit, newbits, lpbmi, fuUsage);
|
|---|
| 71 | free( newbits );
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
|
|---|
| 75 | return rc;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // 2000/09/01 PH Netscape 4.7
|
|---|
| 80 | // If color depth of lpbhmi is 16 bit and lpbmi is 8 bit,
|
|---|
| 81 | // Open32 will crash since it won't allocate any palette color memory,
|
|---|
| 82 | // however wants to copy it later on ...
|
|---|
| 83 | int biBitCount = lpbmih->biBitCount;
|
|---|
| 84 |
|
|---|
| 85 | if (lpbmih->biBitCount != lpbmi->bmiHeader.biBitCount)
|
|---|
| 86 | {
|
|---|
| 87 | dprintf(("GDI32: CreateDIBitmap: color depths of bitmaps differ! (%d,%d\n", lpbmih->biBitCount,
|
|---|
| 88 | lpbmi->bmiHeader.biBitCount));
|
|---|
| 89 |
|
|---|
| 90 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = lpbmi->bmiHeader.biBitCount;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | switch(lpbmih->biBitCount) {
|
|---|
| 94 | case 15:
|
|---|
| 95 | case 16: //Default if BI_BITFIELDS not set is RGB 555
|
|---|
| 96 | bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : DEFAULT_16BPP_RED_MASK;
|
|---|
| 97 | bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
|
|---|
| 98 | bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
|
|---|
| 99 | break;
|
|---|
| 100 | case 24:
|
|---|
| 101 | case 32:
|
|---|
| 102 | bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : DEFAULT_24BPP_RED_MASK;
|
|---|
| 103 | bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
|
|---|
| 104 | bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
|
|---|
| 105 | break;
|
|---|
| 106 | default:
|
|---|
| 107 | bitfields[0] = 0;
|
|---|
| 108 | bitfields[1] = 0;
|
|---|
| 109 | bitfields[2] = 0;
|
|---|
| 110 | break;
|
|---|
| 111 | }
|
|---|
| 112 | if(bitfields[1] == RGB555_GREEN_MASK && lpbInit && fdwInit == CBM_INIT)
|
|---|
| 113 | {//RGB 555?
|
|---|
| 114 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
|
|---|
| 115 |
|
|---|
| 116 | int imgsize = CalcBitmapSize(lpbmih->biBitCount, lpbmih->biWidth, lpbmih->biHeight);
|
|---|
| 117 |
|
|---|
| 118 | newbits = (WORD *)malloc(imgsize);
|
|---|
| 119 | if(CPUFeatures & CPUID_MMX) {
|
|---|
| 120 | RGB555to565MMX(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
|
|---|
| 121 | }
|
|---|
| 122 | else RGB555to565(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
|
|---|
| 123 | lpbInit = newbits;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
|
|---|
| 127 |
|
|---|
| 128 | 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));
|
|---|
| 129 |
|
|---|
| 130 | if(newbits) free(newbits);
|
|---|
| 131 |
|
|---|
| 132 | ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
|
|---|
| 133 | ((BITMAPINFOHEADER *)lpbmih)->biBitCount = biBitCount;
|
|---|
| 134 |
|
|---|
| 135 | if(rc) {
|
|---|
| 136 | STATS_CreateDIBitmap(rc, hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
|
|---|
| 137 | if(bitfields[1] == RGB565_GREEN_MASK) {
|
|---|
| 138 | //mark bitmap as RGB565
|
|---|
| 139 | dprintf(("RGB565 bitmap"));
|
|---|
| 140 | ObjSetHandleFlag(rc, OBJHANDLE_FLAG_BMP_RGB565, TRUE);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return rc;
|
|---|
| 145 | }
|
|---|
| 146 | //******************************************************************************
|
|---|
| 147 | //******************************************************************************
|
|---|
| 148 | HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight)
|
|---|
| 149 | {
|
|---|
| 150 | HBITMAP hBitmap;
|
|---|
| 151 | pDCData pHps;
|
|---|
| 152 |
|
|---|
| 153 | pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
|
|---|
| 154 | if(!pHps)
|
|---|
| 155 | {
|
|---|
| 156 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 157 | return 0;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | hBitmap = O32_CreateCompatibleBitmap(hdc, nWidth, nHeight);
|
|---|
| 161 | if(hBitmap) {
|
|---|
| 162 | STATS_CreateCompatibleBitmap(hBitmap,hdc, nWidth, nHeight);
|
|---|
| 163 | if(pHps->hwnd == 1) { //1 == HWND_DESKTOP
|
|---|
| 164 | dprintf(("Screen compatible bitmap"));
|
|---|
| 165 | ObjSetHandleFlag(hBitmap, OBJHANDLE_FLAG_BMP_SCREEN_COMPATIBLE, 1);
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | return hBitmap;
|
|---|
| 170 | }
|
|---|
| 171 | //******************************************************************************
|
|---|
| 172 | //CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap
|
|---|
| 173 | //******************************************************************************
|
|---|
| 174 | HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
|
|---|
| 175 | {
|
|---|
| 176 | dprintf(("GDI32: CreateDisardableBitmap"));
|
|---|
| 177 | return CreateCompatibleBitmap(hDC, nWidth, nHeight);
|
|---|
| 178 | }
|
|---|
| 179 | //******************************************************************************
|
|---|
| 180 | //******************************************************************************
|
|---|
| 181 | HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes,
|
|---|
| 182 | UINT cBitsPerPel, const void *lpvBits)
|
|---|
| 183 | {
|
|---|
| 184 | HBITMAP hBitmap;
|
|---|
| 185 |
|
|---|
| 186 | hBitmap = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
|
|---|
| 187 | if(hBitmap) STATS_CreateBitmap(hBitmap,nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
|
|---|
| 188 | return(hBitmap);
|
|---|
| 189 | }
|
|---|
| 190 | //******************************************************************************
|
|---|
| 191 | //******************************************************************************
|
|---|
| 192 | HBITMAP WIN32API CreateBitmapIndirect( const BITMAP *pBitmap)
|
|---|
| 193 | {
|
|---|
| 194 | HBITMAP hBitmap;
|
|---|
| 195 |
|
|---|
| 196 | dprintf(("GDI32: CreateBitmapIndirect (%d,%d) bpp %d bits %x", pBitmap->bmWidth, pBitmap->bmHeight, pBitmap->bmBitsPixel, pBitmap->bmBits));
|
|---|
| 197 | hBitmap = O32_CreateBitmapIndirect(pBitmap);
|
|---|
| 198 | if(hBitmap) STATS_CreateBitmapIndirect(hBitmap, pBitmap);
|
|---|
| 199 | return hBitmap;
|
|---|
| 200 | }
|
|---|
| 201 | //******************************************************************************
|
|---|
| 202 | //*********************************************************************************
|
|---|
| 203 | HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage,
|
|---|
| 204 | VOID **ppvBits, HANDLE hSection, DWORD dwOffset)
|
|---|
| 205 | {
|
|---|
| 206 | HBITMAP res = 0;
|
|---|
| 207 | BOOL fFlip = 0;
|
|---|
| 208 | int iHeight, iWidth;
|
|---|
| 209 | BOOL fCreateDC = FALSE;
|
|---|
| 210 |
|
|---|
| 211 | dprintf(("GDI32: CreateDIBSection %x %x %x %x %x %d", hdc, pbmi, iUsage, ppvBits, hSection, dwOffset));
|
|---|
| 212 |
|
|---|
| 213 | //SvL: 13-9-98: StarCraft uses bitmap with negative height
|
|---|
| 214 | iWidth = pbmi->bmiHeader.biWidth;
|
|---|
| 215 | if(pbmi->bmiHeader.biWidth < 0)
|
|---|
| 216 | {
|
|---|
| 217 | dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth));
|
|---|
| 218 | pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth;
|
|---|
| 219 | fFlip = FLIP_HOR;
|
|---|
| 220 | }
|
|---|
| 221 | iHeight = pbmi->bmiHeader.biHeight;
|
|---|
| 222 | if(pbmi->bmiHeader.biHeight < 0)
|
|---|
| 223 | {
|
|---|
| 224 | dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight));
|
|---|
| 225 | pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight;
|
|---|
| 226 | fFlip |= FLIP_VERT;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | //SvL: RP7 (update) calls this api with hdc == 0
|
|---|
| 230 | if(hdc == 0) {
|
|---|
| 231 | hdc = CreateCompatibleDC(0);
|
|---|
| 232 | fCreateDC = TRUE;
|
|---|
| 233 | }
|
|---|
| 234 | res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage);
|
|---|
| 235 | if (res)
|
|---|
| 236 | {
|
|---|
| 237 | char PalSize;
|
|---|
| 238 | DIBSection *dsect;
|
|---|
| 239 |
|
|---|
| 240 | dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip);
|
|---|
| 241 |
|
|---|
| 242 | if(dsect != NULL)
|
|---|
| 243 | {
|
|---|
| 244 | PalSize = dsect->GetBitCount();
|
|---|
| 245 | if(PalSize <= 8)
|
|---|
| 246 | {
|
|---|
| 247 | ULONG Pal[256], nrcolors;
|
|---|
| 248 | LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}};
|
|---|
| 249 | HPALETTE hpalCur, hpalTmp;
|
|---|
| 250 |
|
|---|
| 251 | // Now get the current Palette from the DC
|
|---|
| 252 | hpalTmp = CreatePalette(&tmpPal);
|
|---|
| 253 | hpalCur = SelectPalette(hdc, hpalTmp, FALSE);
|
|---|
| 254 |
|
|---|
| 255 | // and use it to set the DIBColorTable
|
|---|
| 256 | nrcolors = GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal);
|
|---|
| 257 | dsect->SetDIBColorTable(0, nrcolors, (LPPALETTEENTRY)&Pal);
|
|---|
| 258 |
|
|---|
| 259 | // Restore the DC Palette
|
|---|
| 260 | SelectPalette(hdc,hpalCur,FALSE);
|
|---|
| 261 | DeleteObject(hpalTmp);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | if(ppvBits!=NULL)
|
|---|
| 265 | *ppvBits = dsect->GetDIBObject();
|
|---|
| 266 |
|
|---|
| 267 | pbmi->bmiHeader.biWidth = iWidth;
|
|---|
| 268 | pbmi->bmiHeader.biHeight = iHeight;
|
|---|
| 269 |
|
|---|
| 270 | if(fCreateDC) DeleteDC(hdc);
|
|---|
| 271 |
|
|---|
| 272 | STATS_CreateDIBSection(res, hdc, pbmi, iUsage, ppvBits, hSection, dwOffset);
|
|---|
| 273 |
|
|---|
| 274 | return(res);
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | if(fCreateDC) DeleteDC(hdc);
|
|---|
| 278 |
|
|---|
| 279 | /* Error. */
|
|---|
| 280 | if (res)
|
|---|
| 281 | DeleteObject(res);
|
|---|
| 282 | *ppvBits = NULL;
|
|---|
| 283 |
|
|---|
| 284 | #ifdef DEBUG
|
|---|
| 285 | dprintf(("GDI32: CreateDIBSection, error!\n"));
|
|---|
| 286 | dprintf(("pbmi->biWidth %d", pbmi->bmiHeader.biWidth));
|
|---|
| 287 | dprintf(("pbmi->biHeight %d", pbmi->bmiHeader.biHeight));
|
|---|
| 288 | dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount));
|
|---|
| 289 | #endif
|
|---|
| 290 |
|
|---|
| 291 | return 0;
|
|---|
| 292 | }
|
|---|
| 293 | //******************************************************************************
|
|---|
| 294 | //******************************************************************************
|
|---|
| 295 | UINT WIN32API GetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 296 | RGBQUAD *pColors)
|
|---|
| 297 | {
|
|---|
| 298 | DIBSection *dsect = DIBSection::findHDC(hdc);
|
|---|
| 299 | UINT rc;
|
|---|
| 300 | int i;
|
|---|
| 301 |
|
|---|
| 302 | dprintf(("GetDIBColorTable %x %d->%d %x", hdc, uStartIndex, cEntries, pColors));
|
|---|
| 303 |
|
|---|
| 304 | if(dsect)
|
|---|
| 305 | {
|
|---|
| 306 | return(dsect->GetDIBColorTable(uStartIndex, cEntries, pColors));
|
|---|
| 307 | }
|
|---|
| 308 | //TODO: Is this correct?????
|
|---|
| 309 | // Wine returns 0 if bitmap selected into dc with bpp > 8
|
|---|
| 310 | HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL);
|
|---|
| 311 | rc = O32_GetPaletteEntries(hpal, uStartIndex, cEntries, (PALETTEENTRY *)pColors);
|
|---|
| 312 | for(i=0;i<cEntries;i++)
|
|---|
| 313 | {
|
|---|
| 314 | BYTE tmp;
|
|---|
| 315 | tmp = pColors[i].rgbBlue;
|
|---|
| 316 | pColors[i].rgbBlue = pColors[i].rgbRed;
|
|---|
| 317 | pColors[i].rgbRed = tmp;
|
|---|
| 318 | pColors[i].rgbReserved = 0;
|
|---|
| 319 | }
|
|---|
| 320 | dprintf(("GDI32: GetDIBColorTable returns %d\n", rc));
|
|---|
| 321 | return(rc);
|
|---|
| 322 | }
|
|---|
| 323 | //******************************************************************************
|
|---|
| 324 | //******************************************************************************
|
|---|
| 325 | UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
|
|---|
| 326 | RGBQUAD *pColors)
|
|---|
| 327 | {
|
|---|
| 328 | DIBSection *dsect = DIBSection::findHDC(hdc);
|
|---|
| 329 |
|
|---|
| 330 | dprintf(("GDI32: SetDIBColorTable %x %d,%d %x", hdc, uStartIndex, cEntries, pColors));
|
|---|
| 331 | if(dsect)
|
|---|
| 332 | {
|
|---|
| 333 | return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors));
|
|---|
| 334 | }
|
|---|
| 335 | else return(0);
|
|---|
| 336 | }
|
|---|
| 337 | //******************************************************************************
|
|---|
| 338 | //******************************************************************************
|
|---|
| 339 | LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID arg3)
|
|---|
| 340 | {
|
|---|
| 341 | dprintf(("GDI32: GetBitmapBits %x", hBitmap));
|
|---|
| 342 | return O32_GetBitmapBits(hBitmap, arg2, arg3);
|
|---|
| 343 | }
|
|---|
| 344 | //******************************************************************************
|
|---|
| 345 | //******************************************************************************
|
|---|
| 346 | LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID * arg3)
|
|---|
| 347 | {
|
|---|
| 348 | dprintf(("GDI32: SetBitmapBits %x", hBitmap));
|
|---|
| 349 | return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3);
|
|---|
| 350 | }
|
|---|
| 351 | //******************************************************************************
|
|---|
| 352 | //******************************************************************************
|
|---|
| 353 | BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize)
|
|---|
| 354 | {
|
|---|
| 355 | dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy));
|
|---|
| 356 | return O32_GetBitmapDimensionEx(hBitmap, pSize);
|
|---|
| 357 | }
|
|---|
| 358 | //******************************************************************************
|
|---|
| 359 | //******************************************************************************
|
|---|
| 360 | BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE arg4)
|
|---|
| 361 | {
|
|---|
| 362 | dprintf(("GDI32: SetBitmapDimensionEx"));
|
|---|
| 363 | return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4);
|
|---|
| 364 | }
|
|---|
| 365 | //******************************************************************************
|
|---|
| 366 | //******************************************************************************
|
|---|
| 367 | int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
|
|---|
| 368 | void *lpvBits, PBITMAPINFO lpbi, UINT uUsage)
|
|---|
| 369 | {
|
|---|
| 370 | int nrlines;
|
|---|
| 371 | pDCData pHps;
|
|---|
| 372 | HDC hdcMem;
|
|---|
| 373 |
|
|---|
| 374 | dprintf(("GDI32: GetDIBits %x %x %d %d %x %x (biBitCount %d) %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, lpbi->bmiHeader.biBitCount, uUsage));
|
|---|
| 375 |
|
|---|
| 376 | //SvL: WGSS screws up the DC if it's a memory DC
|
|---|
| 377 | // TODO: Fix in WGSS (tries to select another bitmap in the DC which fails)
|
|---|
| 378 | pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
|
|---|
| 379 | if(!pHps)
|
|---|
| 380 | {
|
|---|
| 381 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 382 | return 0;
|
|---|
| 383 | }
|
|---|
| 384 | if(pHps->isMemoryPS) {
|
|---|
| 385 | hdcMem = CreateCompatibleDC(0);
|
|---|
| 386 | }
|
|---|
| 387 | else hdcMem = hdc;
|
|---|
| 388 |
|
|---|
| 389 | if(lpbi->bmiHeader.biHeight < 0) {
|
|---|
| 390 | //NOTE: workaround for WGSS bug; remove when fixed
|
|---|
| 391 | LONG height = lpbi->bmiHeader.biHeight;
|
|---|
| 392 |
|
|---|
| 393 | nrlines = O32_GetDIBits(hdcMem, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
|
|---|
| 394 |
|
|---|
| 395 | //NOTE: workaround for WGSS bug; remove when fixed
|
|---|
| 396 | lpbi->bmiHeader.biHeight = height;
|
|---|
| 397 | }
|
|---|
| 398 | else {
|
|---|
| 399 | nrlines = O32_GetDIBits(hdcMem, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | if(pHps->isMemoryPS)
|
|---|
| 403 | DeleteDC(hdcMem);
|
|---|
| 404 |
|
|---|
| 405 | if(lpvBits) {
|
|---|
| 406 | // set proper color masks (only if lpvBits not NULL)
|
|---|
| 407 | switch(lpbi->bmiHeader.biBitCount) {
|
|---|
| 408 | case 15:
|
|---|
| 409 | case 16: //RGB 565
|
|---|
| 410 | {
|
|---|
| 411 | DWORD dwFlags;
|
|---|
| 412 |
|
|---|
| 413 | dwFlags = ObjQueryHandleFlags(hBitmap);
|
|---|
| 414 | if(dwFlags & (OBJHANDLE_FLAG_BMP_SCREEN_COMPATIBLE|OBJHANDLE_FLAG_BMP_RGB565)) {
|
|---|
| 415 | ((DWORD*)(lpbi->bmiColors))[0] = RGB565_RED_MASK;
|
|---|
| 416 | ((DWORD*)(lpbi->bmiColors))[1] = RGB565_GREEN_MASK;
|
|---|
| 417 | ((DWORD*)(lpbi->bmiColors))[2] = RGB565_BLUE_MASK;
|
|---|
| 418 | }
|
|---|
| 419 | else {
|
|---|
| 420 | ((DWORD*)(lpbi->bmiColors))[0] = RGB555_RED_MASK;
|
|---|
| 421 | ((DWORD*)(lpbi->bmiColors))[1] = RGB555_GREEN_MASK;
|
|---|
| 422 | ((DWORD*)(lpbi->bmiColors))[2] = RGB555_BLUE_MASK;
|
|---|
| 423 | }
|
|---|
| 424 | break;
|
|---|
| 425 | }
|
|---|
| 426 | case 24:
|
|---|
| 427 | case 32:
|
|---|
| 428 | ((DWORD*)(lpbi->bmiColors))[0] = DEFAULT_24BPP_RED_MASK;
|
|---|
| 429 | ((DWORD*)(lpbi->bmiColors))[1] = DEFAULT_24BPP_GREEN_MASK;
|
|---|
| 430 | ((DWORD*)(lpbi->bmiColors))[2] = DEFAULT_24BPP_BLUE_MASK;
|
|---|
| 431 | break;
|
|---|
| 432 | }
|
|---|
| 433 | if(lpbi->bmiHeader.biCompression == BI_RGB && lpbi->bmiHeader.biBitCount > 8) {
|
|---|
| 434 | lpbi->bmiHeader.biCompression = BI_BITFIELDS;
|
|---|
| 435 | dprintf(("BI_BITFIELDS: %x %x %x", ((DWORD*)(lpbi->bmiColors))[0], ((DWORD*)(lpbi->bmiColors))[1], ((DWORD*)(lpbi->bmiColors))[2]));
|
|---|
| 436 | }
|
|---|
| 437 | }
|
|---|
| 438 | if(nrlines && lpvBits && lpbi->bmiHeader.biBitCount == 16 && ((DWORD*)(lpbi->bmiColors))[1] == RGB555_GREEN_MASK)
|
|---|
| 439 | {//RGB 555?
|
|---|
| 440 | dprintf(("RGB 565->555 conversion required"));
|
|---|
| 441 |
|
|---|
| 442 | int imgsize = CalcBitmapSize(lpbi->bmiHeader.biBitCount,
|
|---|
| 443 | lpbi->bmiHeader.biWidth, nrlines);
|
|---|
| 444 |
|
|---|
| 445 | if(CPUFeatures & CPUID_MMX) {
|
|---|
| 446 | RGB565to555MMX((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
|
|---|
| 447 | }
|
|---|
| 448 | else RGB565to555((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | //WGSS/Open32 returns 0 when querying the bitmap info; must return nr of scanlines
|
|---|
| 452 | //as 0 signals failure
|
|---|
| 453 | if(lpvBits == NULL) {
|
|---|
| 454 | nrlines = cScanLines;
|
|---|
| 455 | }
|
|---|
| 456 | return nrlines;
|
|---|
| 457 | }
|
|---|
| 458 | //******************************************************************************
|
|---|
| 459 | //******************************************************************************
|
|---|
| 460 | void WIN32API ConvertRGB555to565(LPVOID dest, LPVOID src, UINT imgsize)
|
|---|
| 461 | {
|
|---|
| 462 | if(CPUFeatures & CPUID_MMX) {
|
|---|
| 463 | RGB555to565MMX((WORD *)dest, (WORD *)src, imgsize/sizeof(WORD));
|
|---|
| 464 | }
|
|---|
| 465 | else RGB555to565((WORD *)dest, (WORD *)src, imgsize/sizeof(WORD));
|
|---|
| 466 | }
|
|---|
| 467 | //******************************************************************************
|
|---|
| 468 | //******************************************************************************
|
|---|
| 469 | int WIN32API SetDIBits(HDC hdc, HBITMAP hBitmap, UINT startscan, UINT numlines, const VOID *pBits,
|
|---|
| 470 | const BITMAPINFO *pBitmapInfo, UINT usage)
|
|---|
| 471 | {
|
|---|
| 472 | int ret;
|
|---|
| 473 | DWORD bitfields[3];
|
|---|
| 474 | WORD *newbits = NULL;
|
|---|
| 475 |
|
|---|
| 476 | dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x", hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage));
|
|---|
| 477 |
|
|---|
| 478 | //SvL: Open32's SetDIBits really messes things up for 1 bpp bitmaps, must use SetBitmapBits
|
|---|
| 479 | if(pBitmapInfo->bmiHeader.biBitCount == 1 && startscan == 0 && numlines == pBitmapInfo->bmiHeader.biHeight)
|
|---|
| 480 | {//WARNING: hack alert!
|
|---|
| 481 | int dibwidth = DIB_GetDIBWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
|
|---|
| 482 | int bmpwidth = BITMAP_GetWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
|
|---|
| 483 | char *newpix = (char *)malloc(dibwidth*pBitmapInfo->bmiHeader.biHeight);
|
|---|
| 484 | char *orgpix = (char *)pBits;
|
|---|
| 485 | int ret;
|
|---|
| 486 |
|
|---|
| 487 | dprintf(("Flipping 1bpp bitmap and calling SetBitmapBits (WORKAROUND) (%d -> %d)", dibwidth, bmpwidth));
|
|---|
| 488 | newpix += ((pBitmapInfo->bmiHeader.biHeight-1)*bmpwidth);
|
|---|
| 489 |
|
|---|
| 490 | //flip bitmap here; SetDIBits assumes origin is left bottom, SetBitmapBits left top
|
|---|
| 491 | //SetDIBits assumes DWORD aligned data
|
|---|
| 492 | //SetBitmapBits assumes WORD aligned data
|
|---|
| 493 | for(int i=0;i<pBitmapInfo->bmiHeader.biHeight;i++) {
|
|---|
| 494 | memcpy(newpix, orgpix, bmpwidth);
|
|---|
| 495 |
|
|---|
| 496 | newpix -= bmpwidth;
|
|---|
| 497 | orgpix += dibwidth;
|
|---|
| 498 | }
|
|---|
| 499 | newpix += bmpwidth;
|
|---|
| 500 | ret = O32_SetBitmapBits(hBitmap, pBitmapInfo->bmiHeader.biSizeImage, newpix);
|
|---|
| 501 |
|
|---|
| 502 | free(newpix);
|
|---|
| 503 | return ret;
|
|---|
| 504 | }
|
|---|
| 505 | #ifdef DEBUG
|
|---|
| 506 | if(pBitmapInfo->bmiHeader.biBitCount == 1) {
|
|---|
| 507 | dprintf(("ERROR: SetDIBits does NOT work well for 1 bpp bitmaps!!!!!"));
|
|---|
| 508 | }
|
|---|
| 509 | #endif
|
|---|
| 510 |
|
|---|
| 511 | switch(pBitmapInfo->bmiHeader.biBitCount) {
|
|---|
| 512 | case 15:
|
|---|
| 513 | case 16: //Default if BI_BITFIELDS not set is RGB 555
|
|---|
| 514 | bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : DEFAULT_16BPP_RED_MASK;
|
|---|
| 515 | bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
|
|---|
| 516 | bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
|
|---|
| 517 | break;
|
|---|
| 518 |
|
|---|
| 519 | case 24:
|
|---|
| 520 | case 32:
|
|---|
| 521 | bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : DEFAULT_24BPP_RED_MASK;
|
|---|
| 522 | bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
|
|---|
| 523 | bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
|
|---|
| 524 | break;
|
|---|
| 525 | default:
|
|---|
| 526 | bitfields[0] = 0;
|
|---|
| 527 | bitfields[1] = 0;
|
|---|
| 528 | bitfields[2] = 0;
|
|---|
| 529 | break;
|
|---|
| 530 | }
|
|---|
| 531 | if(pBits && bitfields[1] == RGB555_GREEN_MASK)
|
|---|
| 532 | {//RGB 555?
|
|---|
| 533 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
|
|---|
| 534 |
|
|---|
| 535 | int imgsize = CalcBitmapSize(pBitmapInfo->bmiHeader.biBitCount,
|
|---|
| 536 | pBitmapInfo->bmiHeader.biWidth, numlines);
|
|---|
| 537 |
|
|---|
| 538 | newbits = (WORD *)malloc(imgsize);
|
|---|
| 539 | if(CPUFeatures & CPUID_MMX) {
|
|---|
| 540 | RGB555to565MMX(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
|
|---|
| 541 | }
|
|---|
| 542 | else RGB555to565(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
|
|---|
| 543 | pBits = newbits;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | if(pBitmapInfo->bmiHeader.biHeight < 0) {
|
|---|
| 547 | dprintf(("!WARNING! Negative height!!"));
|
|---|
| 548 | }
|
|---|
| 549 | ret = O32_SetDIBits(hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage);
|
|---|
| 550 | if(newbits) free(newbits);
|
|---|
| 551 |
|
|---|
| 552 | if(DIBSection::getSection() != NULL)
|
|---|
| 553 | {
|
|---|
| 554 | DIBSection *dsect;
|
|---|
| 555 |
|
|---|
| 556 | dsect = DIBSection::findObj(hBitmap);
|
|---|
| 557 | if(dsect) {
|
|---|
| 558 | HBITMAP hBmpOld = SelectObject(hdc, hBitmap);
|
|---|
| 559 | dsect->sync(hdc, 0, dsect->GetHeight());
|
|---|
| 560 | SelectObject(hdc, hBmpOld);
|
|---|
| 561 | }
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | return ret;
|
|---|
| 565 | }
|
|---|
| 566 | //******************************************************************************
|
|---|
| 567 | //******************************************************************************
|
|---|