| 1 | /* $Id: blit.cpp,v 1.43 2003-01-01 16:32:07 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * GDI32 blit 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 <cpuhlp.h> | 
|---|
| 17 | #include <winuser32.h> | 
|---|
| 18 | #include <dbglog.h> | 
|---|
| 19 | #include "dibsect.h" | 
|---|
| 20 | #include "rgbcvt.h" | 
|---|
| 21 |  | 
|---|
| 22 | #define DBG_LOCALLOG    DBG_blit | 
|---|
| 23 | #include "dbglocal.h" | 
|---|
| 24 |  | 
|---|
| 25 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr); | 
|---|
| 26 | ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy); | 
|---|
| 27 |  | 
|---|
| 28 | //****************************************************************************** | 
|---|
| 29 | //****************************************************************************** | 
|---|
| 30 | BOOL WIN32API StretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest, | 
|---|
| 31 | int nWidthDest, int nHeightDest, | 
|---|
| 32 | HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, | 
|---|
| 33 | int nWidthSrc, int nHeightSrc, DWORD dwRop) | 
|---|
| 34 | { | 
|---|
| 35 | BOOL rc; | 
|---|
| 36 |  | 
|---|
| 37 | dprintf(("GDI32: StretchBlt Dest: %x (%d, %d) size (%d, %d) ROP %x", | 
|---|
| 38 | hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, dwRop)); | 
|---|
| 39 | dprintf(("GDI32: StretchBlt Src : %x (%d, %d) size (%d, %d)\n", | 
|---|
| 40 | hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc)); | 
|---|
| 41 |  | 
|---|
| 42 | SetLastError(ERROR_SUCCESS); | 
|---|
| 43 | if(DIBSection::getSection() != NULL) | 
|---|
| 44 | { | 
|---|
| 45 | DIBSection *dsect = DIBSection::findHDC(hdcSrc); | 
|---|
| 46 | if(dsect) | 
|---|
| 47 | { | 
|---|
| 48 | rc  = dsect->BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, | 
|---|
| 49 | nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop); | 
|---|
| 50 | return rc; | 
|---|
| 51 | } | 
|---|
| 52 | } | 
|---|
| 53 | rc = O32_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop); | 
|---|
| 54 | if(DIBSection::getSection() != NULL) { | 
|---|
| 55 | DIBSection *destdib = DIBSection::findHDC(hdcDest); | 
|---|
| 56 | if(destdib) { | 
|---|
| 57 | destdib->sync(hdcDest, nYOriginDest, nHeightDest); | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 | if(rc == FALSE) { | 
|---|
| 61 | dprintf(("!WARNING!: GDI32: StretchBlt returned FALSE; last error %x", rc, GetLastError())); | 
|---|
| 62 | } | 
|---|
| 63 | return rc; | 
|---|
| 64 | } | 
|---|
| 65 | //****************************************************************************** | 
|---|
| 66 | //****************************************************************************** | 
|---|
| 67 | BOOL WIN32API BitBlt(HDC hdcDest, | 
|---|
| 68 | int nXDest, | 
|---|
| 69 | int nYDest, | 
|---|
| 70 | int nWidth, | 
|---|
| 71 | int nHeight, | 
|---|
| 72 | HDC hdcSrc, | 
|---|
| 73 | int nXSrc, | 
|---|
| 74 | int nYSrc, | 
|---|
| 75 | DWORD  dwRop) | 
|---|
| 76 | { | 
|---|
| 77 | BOOL rc; | 
|---|
| 78 |  | 
|---|
| 79 | #ifdef DEBUG | 
|---|
| 80 | POINT point1, point2; | 
|---|
| 81 | GetViewportOrgEx(hdcDest, &point1); | 
|---|
| 82 | GetViewportOrgEx(hdcSrc, &point2); | 
|---|
| 83 | dprintf(("BitBlt: Viewport origin dest (%d,%d) src (%d,%d)", point1.x, point1.y, point2.x, point2.y)); | 
|---|
| 84 | #endif | 
|---|
| 85 |  | 
|---|
| 86 | SetLastError(ERROR_SUCCESS); | 
|---|
| 87 | if(DIBSection::getSection() != NULL) | 
|---|
| 88 | { | 
|---|
| 89 | DIBSection *dsect = DIBSection::findHDC(hdcSrc); | 
|---|
| 90 | if(dsect) | 
|---|
| 91 | { | 
|---|
| 92 | return dsect->BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, nXSrc, nYSrc, nWidth, nHeight, dwRop); | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | dprintf(("GDI32: BitBlt to hdc %X from hdc %x (%d,%d) to (%d,%d), (%d,%d) rop %X\n", | 
|---|
| 96 | hdcDest, hdcSrc, nXSrc, nYSrc, nXDest, nYDest, nWidth, nHeight, dwRop)); | 
|---|
| 97 |  | 
|---|
| 98 | rc = O32_BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop); | 
|---|
| 99 |  | 
|---|
| 100 | if(DIBSection::getSection() != NULL) { | 
|---|
| 101 | DIBSection *destdib = DIBSection::findHDC(hdcDest); | 
|---|
| 102 | if(destdib) { | 
|---|
| 103 | destdib->sync(hdcDest, nYDest, nHeight); | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 | return rc; | 
|---|
| 107 | } | 
|---|
| 108 | //****************************************************************************** | 
|---|
| 109 | //****************************************************************************** | 
|---|
| 110 | static INT SetDIBitsToDevice_(HDC hdc, INT xDest, INT yDest, DWORD cx, | 
|---|
| 111 | DWORD cy, INT xSrc, INT ySrc, | 
|---|
| 112 | UINT startscan, UINT lines, LPCVOID bits, | 
|---|
| 113 | const BITMAPINFO *info, UINT coloruse) | 
|---|
| 114 | { | 
|---|
| 115 | INT result, imgsize, palsize, height, width; | 
|---|
| 116 | char *ptr; | 
|---|
| 117 | ULONG compression = 0, bmpsize; | 
|---|
| 118 | WORD *newbits = NULL; | 
|---|
| 119 | DWORD bitfields[3]; | 
|---|
| 120 |  | 
|---|
| 121 | dprintf(("GDI32: SetDIBitsToDevice hdc:%X xDest:%d yDest:%d, cx:%d, cy:%d, xSrc:%d, ySrc:%d, startscan:%d, lines:%d \nGDI32: bits 0x%X, info 0x%X, coloruse %d", | 
|---|
| 122 | hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse)); | 
|---|
| 123 |  | 
|---|
| 124 | SetLastError(ERROR_SUCCESS); | 
|---|
| 125 | if(info == NULL) { | 
|---|
| 126 | goto invalid_parameter; | 
|---|
| 127 | } | 
|---|
| 128 | height = info->bmiHeader.biHeight; | 
|---|
| 129 | width  = info->bmiHeader.biWidth; | 
|---|
| 130 |  | 
|---|
| 131 | if (height < 0) height = -height; | 
|---|
| 132 | if (!lines || (startscan >= height)) { | 
|---|
| 133 | goto invalid_parameter; | 
|---|
| 134 | } | 
|---|
| 135 | if (startscan + lines > height) lines = height - startscan; | 
|---|
| 136 |  | 
|---|
| 137 | if (ySrc < startscan) ySrc = startscan; | 
|---|
| 138 | else if (ySrc >= startscan + lines) goto invalid_parameter; | 
|---|
| 139 |  | 
|---|
| 140 | if (xSrc >= width) goto invalid_parameter; | 
|---|
| 141 |  | 
|---|
| 142 | if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc; | 
|---|
| 143 |  | 
|---|
| 144 | if (xSrc + cx >= width) cx = width - xSrc; | 
|---|
| 145 |  | 
|---|
| 146 | if (!cx || !cy) goto invalid_parameter; | 
|---|
| 147 |  | 
|---|
| 148 | //SvL: RP7's bitmap size is not correct; fix it here or else | 
|---|
| 149 | //     the blit is messed up in Open32 | 
|---|
| 150 | imgsize = CalcBitmapSize(info->bmiHeader.biBitCount, | 
|---|
| 151 | info->bmiHeader.biWidth, info->bmiHeader.biHeight); | 
|---|
| 152 | bmpsize = info->bmiHeader.biSizeImage; | 
|---|
| 153 | if(info->bmiHeader.biCompression == 0 && info->bmiHeader.biSizeImage && | 
|---|
| 154 | info->bmiHeader.biSizeImage < imgsize) | 
|---|
| 155 | { | 
|---|
| 156 | ((BITMAPINFO *)info)->bmiHeader.biSizeImage = imgsize; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | switch(info->bmiHeader.biBitCount) { | 
|---|
| 160 | case 15: | 
|---|
| 161 | case 16: //Default if BI_BITFIELDS not set is RGB 555 | 
|---|
| 162 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors       : DEFAULT_16BPP_RED_MASK; | 
|---|
| 163 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK; | 
|---|
| 164 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK; | 
|---|
| 165 | break; | 
|---|
| 166 |  | 
|---|
| 167 | case 24: | 
|---|
| 168 | case 32: | 
|---|
| 169 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors       : DEFAULT_24BPP_RED_MASK; | 
|---|
| 170 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK; | 
|---|
| 171 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK; | 
|---|
| 172 | break; | 
|---|
| 173 | default: | 
|---|
| 174 | bitfields[0] = 0; | 
|---|
| 175 | bitfields[1] = 0; | 
|---|
| 176 | bitfields[2] = 0; | 
|---|
| 177 | break; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | if(bitfields[1] == 0x3E0) | 
|---|
| 181 | {//RGB 555? | 
|---|
| 182 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2])); | 
|---|
| 183 |  | 
|---|
| 184 | newbits = (WORD *)malloc(imgsize); | 
|---|
| 185 | if(CPUFeatures & CPUID_MMX) { | 
|---|
| 186 | RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD)); | 
|---|
| 187 | } | 
|---|
| 188 | else | 
|---|
| 189 | RGB555to565(newbits, (WORD *)bits, imgsize/sizeof(WORD)); | 
|---|
| 190 | bits = newbits; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | //SvL: Ignore BI_BITFIELDS type (SetDIBitsToDevice fails otherwise) | 
|---|
| 194 | if(info->bmiHeader.biCompression == BI_BITFIELDS) { | 
|---|
| 195 | ((BITMAPINFO *)info)->bmiHeader.biCompression = 0; | 
|---|
| 196 | compression = BI_BITFIELDS; | 
|---|
| 197 | } | 
|---|
| 198 | if(startscan != 0 || lines != info->bmiHeader.biHeight) { | 
|---|
| 199 | dprintf(("WARNING: SetDIBitsToDevice startscan != 0 || lines != info->bmiHeader.biHeight")); | 
|---|
| 200 | dprintf(("info bmp (%d,%d)", info->bmiHeader.biWidth, info->bmiHeader.biHeight)); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | result = O32_StretchDIBits(hdc, xDest, yDest, cx, cy, xSrc, ySrc, | 
|---|
| 204 | cx, cy, (void *)bits, | 
|---|
| 205 | (PBITMAPINFO)info, coloruse, SRCCOPY); | 
|---|
| 206 |  | 
|---|
| 207 | //Open32 always returns height of bitmap (regardless of how many scanlines were copied) | 
|---|
| 208 | if(result != info->bmiHeader.biHeight) { | 
|---|
| 209 | dprintf(("SetDIBitsToDevice failed with rc %x", result)); | 
|---|
| 210 | } | 
|---|
| 211 | else | 
|---|
| 212 | { | 
|---|
| 213 | result = info->bmiHeader.biHeight; | 
|---|
| 214 |  | 
|---|
| 215 | DIBSection *destdib = DIBSection::findHDC(hdc); | 
|---|
| 216 | if(destdib) { | 
|---|
| 217 | if(cx == info->bmiHeader.biWidth && cy == info->bmiHeader.biHeight && | 
|---|
| 218 | destdib->GetBitCount() == info->bmiHeader.biBitCount && | 
|---|
| 219 | destdib->GetBitCount() == 8) | 
|---|
| 220 | { | 
|---|
| 221 | destdib->sync(xDest, yDest, cx, cy, (PVOID)bits); | 
|---|
| 222 | } | 
|---|
| 223 | else    destdib->sync(hdc, yDest, cy); | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|
| 226 | dprintf(("GDI32: SetDIBitsToDevice hdc:%X xDest:%d yDest:%d, cx:%d, cy:%d, xSrc:%d, ySrc:%d, startscan:%d, lines:%d \nGDI32: bits 0x%X, info 0x%X, coloruse %d returned %d", | 
|---|
| 227 | hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse, result)); | 
|---|
| 228 | dprintf(("GDI32: SetDIBitsToDevice %d %d %d %d %x %d", info->bmiHeader.biWidth, info->bmiHeader.biHeight, info->bmiHeader.biPlanes, info->bmiHeader.biBitCount, info->bmiHeader.biCompression, info->bmiHeader.biSizeImage)); | 
|---|
| 229 |  | 
|---|
| 230 | if(compression == BI_BITFIELDS) { | 
|---|
| 231 | ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS; | 
|---|
| 232 | } | 
|---|
| 233 | if(newbits) free(newbits); | 
|---|
| 234 |  | 
|---|
| 235 | ((BITMAPINFO *)info)->bmiHeader.biSizeImage = bmpsize; | 
|---|
| 236 | return result; | 
|---|
| 237 |  | 
|---|
| 238 | invalid_parameter: | 
|---|
| 239 | SetLastError(ERROR_INVALID_PARAMETER); | 
|---|
| 240 | return 0; | 
|---|
| 241 | } | 
|---|
| 242 | //****************************************************************************** | 
|---|
| 243 | //****************************************************************************** | 
|---|
| 244 | INT WIN32API SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx, | 
|---|
| 245 | DWORD cy, INT xSrc, INT ySrc, | 
|---|
| 246 | UINT startscan, UINT lines, LPCVOID bits, | 
|---|
| 247 | const BITMAPINFO *info, UINT coloruse) | 
|---|
| 248 | { | 
|---|
| 249 | static BOOL fMatrox32BppBug = FALSE; | 
|---|
| 250 | INT rc = 0; | 
|---|
| 251 | char *newBits = NULL; | 
|---|
| 252 |  | 
|---|
| 253 | if(startscan != 0 || lines != abs(info->bmiHeader.biHeight)) { | 
|---|
| 254 | dprintf(("WARNING: SetDIBitsToDevice: startscan != 0 || lines != abs(info->bmiHeader.biHeight")); | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | //If upside down, reverse scanlines and call SetDIBitsToDevice again | 
|---|
| 258 | //    if(info->bmiHeader.biHeight < 0 && info->bmiHeader.biBitCount != 8 && info->bmiHeader.biCompression == 0) { | 
|---|
| 259 | if(info->bmiHeader.biHeight < 0 && (info->bmiHeader.biCompression == BI_RGB || | 
|---|
| 260 | info->bmiHeader.biCompression == BI_BITFIELDS)) | 
|---|
| 261 | { | 
|---|
| 262 | // upside down | 
|---|
| 263 | INT rc = -1; | 
|---|
| 264 | long lLineByte = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth, info->bmiHeader.biBitCount); | 
|---|
| 265 | long lHeight   = -info->bmiHeader.biHeight; | 
|---|
| 266 |  | 
|---|
| 267 | //TODO: doesn't work if memory is readonly!! | 
|---|
| 268 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight; | 
|---|
| 269 |  | 
|---|
| 270 | char *newBits = (char *)malloc( lLineByte * lHeight ); | 
|---|
| 271 | if(newBits) { | 
|---|
| 272 | unsigned char *pbSrc = (unsigned char *)bits + xSrc + lLineByte * (ySrc + lHeight - 1); | 
|---|
| 273 | unsigned char *pbDst = (unsigned char *)newBits; | 
|---|
| 274 | for(int y = 0; y < lHeight; y++) { | 
|---|
| 275 | memcpy( pbDst, pbSrc, lLineByte ); | 
|---|
| 276 | pbDst += lLineByte; | 
|---|
| 277 | pbSrc -= lLineByte; | 
|---|
| 278 | } | 
|---|
| 279 | //We only convert the necessary data so xSrc & ySrc are now 0 | 
|---|
| 280 | rc = SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, 0, 0, startscan, lines, (void *)newBits, info, coloruse ); | 
|---|
| 281 | free( newBits ); | 
|---|
| 282 | } | 
|---|
| 283 | else DebugInt3(); | 
|---|
| 284 |  | 
|---|
| 285 | //TODO: doesn't work if memory is readonly!! | 
|---|
| 286 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight; | 
|---|
| 287 |  | 
|---|
| 288 | return rc; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | //We must convert 32 bpp bitmap data to 24 bpp on systems with the Matrox | 
|---|
| 292 | //display driver. (GpiDrawBits for 32 bpp fails with insufficient memory error) | 
|---|
| 293 | if(info->bmiHeader.biBitCount == 32 && fMatrox32BppBug) | 
|---|
| 294 | { | 
|---|
| 295 | BITMAPINFO newInfo; | 
|---|
| 296 | newInfo.bmiHeader = info->bmiHeader; | 
|---|
| 297 |  | 
|---|
| 298 | long lLineWidth; | 
|---|
| 299 | long lHeight    = (newInfo.bmiHeader.biHeight > 0) ? newInfo.bmiHeader.biHeight : -newInfo.bmiHeader.biHeight; | 
|---|
| 300 | long lWidth     = newInfo.bmiHeader.biWidth; | 
|---|
| 301 |  | 
|---|
| 302 | newInfo.bmiHeader.biBitCount  = 24; | 
|---|
| 303 | newInfo.bmiHeader.biSizeImage = CalcBitmapSize(24, newInfo.bmiHeader.biWidth, | 
|---|
| 304 | newInfo.bmiHeader.biHeight); | 
|---|
| 305 |  | 
|---|
| 306 | lLineWidth = newInfo.bmiHeader.biSizeImage / lHeight; | 
|---|
| 307 |  | 
|---|
| 308 | //convert 32 bits bitmap data to 24 bits | 
|---|
| 309 | newBits = (char *)malloc(newInfo.bmiHeader.biSizeImage+16); //extra room needed for copying (too much) | 
|---|
| 310 | if(!newBits) { | 
|---|
| 311 | DebugInt3(); | 
|---|
| 312 | return -1; | 
|---|
| 313 | } | 
|---|
| 314 | unsigned char *pbSrc = (unsigned char *)bits; | 
|---|
| 315 | unsigned char *pbDst = (unsigned char *)newBits; | 
|---|
| 316 | //not very efficient | 
|---|
| 317 | for(int i = 0; i < lHeight; i++) { | 
|---|
| 318 | for(int j=0;j<lWidth;j++) { | 
|---|
| 319 | *(DWORD *)pbDst = *(DWORD *)pbSrc; | 
|---|
| 320 | pbSrc += 4; | 
|---|
| 321 | pbDst += 3; | 
|---|
| 322 | } | 
|---|
| 323 | //24 bpp scanline must be aligned at 4 byte boundary | 
|---|
| 324 | pbDst += (lLineWidth - 3*lWidth); | 
|---|
| 325 | } | 
|---|
| 326 | rc = SetDIBitsToDevice_( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, newBits, &newInfo, coloruse ); | 
|---|
| 327 | free(newBits); | 
|---|
| 328 | return rc; | 
|---|
| 329 | } | 
|---|
| 330 | rc = SetDIBitsToDevice_( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, bits, info, coloruse ); | 
|---|
| 331 |  | 
|---|
| 332 | if(rc == -1 && info->bmiHeader.biBitCount == 32 && !fMatrox32BppBug) | 
|---|
| 333 | { | 
|---|
| 334 | //The Matrox driver seems to have some difficulty blitting 32bpp | 
|---|
| 335 | //data. (out of memory error) The same operation works fine with SDD. | 
|---|
| 336 | fMatrox32BppBug = TRUE; | 
|---|
| 337 | return SetDIBitsToDevice(hdc, xDest, yDest, cx, | 
|---|
| 338 | cy, xSrc, ySrc, | 
|---|
| 339 | startscan, lines, bits, | 
|---|
| 340 | info, coloruse); | 
|---|
| 341 | } | 
|---|
| 342 | return rc; | 
|---|
| 343 | } | 
|---|
| 344 | //****************************************************************************** | 
|---|
| 345 | //****************************************************************************** | 
|---|
| 346 | BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop) | 
|---|
| 347 | { | 
|---|
| 348 | BOOL rc; | 
|---|
| 349 |  | 
|---|
| 350 | dprintf(("PatBlt %x (%d,%d)(%d,%d) %x", hdc, nXLeft,nYLeft,nWidth,nHeight, dwRop)); | 
|---|
| 351 | //CB: Open32 bug: negative width/height not supported! | 
|---|
| 352 | if (nWidth < 0) | 
|---|
| 353 | { | 
|---|
| 354 | nXLeft += nWidth+1; | 
|---|
| 355 | nWidth = -nWidth; | 
|---|
| 356 | } | 
|---|
| 357 | if (nHeight < 0) | 
|---|
| 358 | { | 
|---|
| 359 | nYLeft += nHeight+1; | 
|---|
| 360 | nHeight = -nHeight; | 
|---|
| 361 | } | 
|---|
| 362 | rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop); | 
|---|
| 363 | if(rc) { | 
|---|
| 364 | DIBSection *destdib = DIBSection::findHDC(hdc); | 
|---|
| 365 | if(destdib) { | 
|---|
| 366 | destdib->sync(hdc, nYLeft, nHeight); | 
|---|
| 367 | } | 
|---|
| 368 | } | 
|---|
| 369 | return(rc); | 
|---|
| 370 | } | 
|---|
| 371 | //****************************************************************************** | 
|---|
| 372 | //****************************************************************************** | 
|---|
| 373 | BOOL WIN32API MaskBlt( HDC arg1, int arg2, int arg3, int arg4, int arg5, HDC   arg6, int arg7, int arg8, HBITMAP arg9, int arg10, int arg11, DWORD  arg12) | 
|---|
| 374 | { | 
|---|
| 375 | return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); | 
|---|
| 376 | } | 
|---|
| 377 | //****************************************************************************** | 
|---|
| 378 | //****************************************************************************** | 
|---|
| 379 | BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc, | 
|---|
| 380 | int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask, | 
|---|
| 381 | int xMast, int yMask) | 
|---|
| 382 | { | 
|---|
| 383 | dprintf(("GDI32: PlgBlt, not implemented\n")); | 
|---|
| 384 | return(FALSE); | 
|---|
| 385 | } | 
|---|
| 386 | //****************************************************************************** | 
|---|
| 387 | //****************************************************************************** | 
|---|
| 388 | static INT StretchDIBits_(HDC hdc, INT xDst, INT yDst, INT widthDst, | 
|---|
| 389 | INT heightDst, INT xSrc, INT ySrc, INT widthSrc, | 
|---|
| 390 | INT heightSrc, const void *bits, | 
|---|
| 391 | const BITMAPINFO *info, UINT wUsage, DWORD dwRop ) | 
|---|
| 392 | { | 
|---|
| 393 | INT rc; | 
|---|
| 394 | DWORD bitfields[3], compression = 0; | 
|---|
| 395 | WORD *newbits = NULL; | 
|---|
| 396 |  | 
|---|
| 397 | dprintf(("GDI32: StretchDIBits %x to (%d,%d) (%d,%d) from (%d,%d) (%d,%d), %x %x %x %x", hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, bits, info, wUsage, dwRop)); | 
|---|
| 398 |  | 
|---|
| 399 | if(info->bmiHeader.biBitCount == 1) { | 
|---|
| 400 | dprintf(("WARNING: StretchDIBits does NOT work correctly for 1 bpp bitmaps!!")); | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | if(wUsage == DIB_PAL_COLORS && info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) | 
|---|
| 404 | { | 
|---|
| 405 | // workaround for open32 bug. | 
|---|
| 406 | // If syscolors > 256 and wUsage == DIB_PAL_COLORS. | 
|---|
| 407 |  | 
|---|
| 408 | int i; | 
|---|
| 409 | USHORT *pColorIndex = (USHORT *)info->bmiColors; | 
|---|
| 410 | RGBQUAD *pColors = (RGBQUAD *) alloca(info->bmiHeader.biClrUsed * | 
|---|
| 411 | sizeof(RGBQUAD)); | 
|---|
| 412 | BITMAPINFO *infoLoc = (BITMAPINFO *) alloca(sizeof(BITMAPINFO) + | 
|---|
| 413 | info->bmiHeader.biClrUsed * sizeof(RGBQUAD)); | 
|---|
| 414 |  | 
|---|
| 415 | memcpy(infoLoc, info, sizeof(BITMAPINFO)); | 
|---|
| 416 |  | 
|---|
| 417 | if(GetDIBColorTable(hdc, 0, info->bmiHeader.biClrUsed, pColors) == 0) { | 
|---|
| 418 | dprintf(("ERROR: StretchDIBits: GetDIBColorTable failed!!")); | 
|---|
| 419 | return FALSE; | 
|---|
| 420 | } | 
|---|
| 421 | for(i=0;i<info->bmiHeader.biClrUsed;i++, pColorIndex++) | 
|---|
| 422 | { | 
|---|
| 423 | infoLoc->bmiColors[i] = pColors[*pColorIndex]; | 
|---|
| 424 | } | 
|---|
| 425 |  | 
|---|
| 426 | rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, | 
|---|
| 427 | widthSrc, heightSrc, (void *)bits, | 
|---|
| 428 | (PBITMAPINFO)infoLoc, DIB_RGB_COLORS, dwRop); | 
|---|
| 429 |  | 
|---|
| 430 | //Open32 always returns height of bitmap (regardless of how many scanlines were copied) | 
|---|
| 431 | if(rc != heightSrc && rc != infoLoc->bmiHeader.biHeight) { | 
|---|
| 432 | dprintf(("StretchDIBits failed with rc %x", rc)); | 
|---|
| 433 | } | 
|---|
| 434 | else { | 
|---|
| 435 | rc = heightSrc; | 
|---|
| 436 |  | 
|---|
| 437 | DIBSection *destdib = DIBSection::findHDC(hdc); | 
|---|
| 438 | if(destdib) { | 
|---|
| 439 | if(widthDst == widthSrc && heightDst == heightSrc && | 
|---|
| 440 | destdib->GetBitCount() == infoLoc->bmiHeader.biBitCount && | 
|---|
| 441 | destdib->GetBitCount() == 8) | 
|---|
| 442 | { | 
|---|
| 443 | destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits); | 
|---|
| 444 | } | 
|---|
| 445 | else        destdib->sync(hdc, yDst, heightDst); | 
|---|
| 446 | } | 
|---|
| 447 | } | 
|---|
| 448 |  | 
|---|
| 449 | return rc; | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | switch(info->bmiHeader.biBitCount) { | 
|---|
| 453 | case 15: | 
|---|
| 454 | case 16: //Default if BI_BITFIELDS not set is RGB 555 | 
|---|
| 455 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors       : DEFAULT_16BPP_RED_MASK; | 
|---|
| 456 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK; | 
|---|
| 457 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK; | 
|---|
| 458 | break; | 
|---|
| 459 | case 24: | 
|---|
| 460 | case 32: | 
|---|
| 461 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors       : DEFAULT_24BPP_RED_MASK; | 
|---|
| 462 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK; | 
|---|
| 463 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK; | 
|---|
| 464 | break; | 
|---|
| 465 | default: | 
|---|
| 466 | bitfields[0] = 0; | 
|---|
| 467 | bitfields[1] = 0; | 
|---|
| 468 | bitfields[2] = 0; | 
|---|
| 469 | break; | 
|---|
| 470 | } | 
|---|
| 471 | if(bitfields[1] == RGB555_GREEN_MASK) | 
|---|
| 472 | {//RGB 555? | 
|---|
| 473 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2])); | 
|---|
| 474 |  | 
|---|
| 475 | ULONG imgsize = CalcBitmapSize(info->bmiHeader.biBitCount, | 
|---|
| 476 | widthSrc, heightSrc); | 
|---|
| 477 | ULONG offset = CalcBitmapSize(info->bmiHeader.biBitCount, | 
|---|
| 478 | xSrc, ySrc)/sizeof(WORD); | 
|---|
| 479 | newbits = (WORD *) HeapAlloc(GetProcessHeap(), 0, imgsize); | 
|---|
| 480 | //bugbug (too much) | 
|---|
| 481 | //bugbug | 
|---|
| 482 | if(CPUFeatures & CPUID_MMX) { | 
|---|
| 483 | RGB555to565MMX(newbits, (WORD *)bits+offset, imgsize/sizeof(WORD)); | 
|---|
| 484 | } | 
|---|
| 485 | else | 
|---|
| 486 | RGB555to565(newbits, (WORD *)bits+offset, imgsize/sizeof(WORD)); | 
|---|
| 487 | bits = newbits; | 
|---|
| 488 | } | 
|---|
| 489 | //SvL: Ignore BI_BITFIELDS type (StretchDIBits fails otherwise) | 
|---|
| 490 | if(info->bmiHeader.biCompression == BI_BITFIELDS) { | 
|---|
| 491 | ((BITMAPINFO *)info)->bmiHeader.biCompression = 0; | 
|---|
| 492 | compression = BI_BITFIELDS; | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, | 
|---|
| 496 | widthSrc, heightSrc, (void *)bits, | 
|---|
| 497 | (PBITMAPINFO)info, wUsage, dwRop); | 
|---|
| 498 |  | 
|---|
| 499 | if(compression == BI_BITFIELDS) { | 
|---|
| 500 | ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS; | 
|---|
| 501 | } | 
|---|
| 502 | if(newbits) HeapFree(GetProcessHeap(), 0, newbits); | 
|---|
| 503 |  | 
|---|
| 504 | //Open32 always returns height of bitmap (regardless of how many scanlines were copied) | 
|---|
| 505 | if(rc != heightSrc && rc != info->bmiHeader.biHeight) { | 
|---|
| 506 | dprintf(("StretchDIBits failed with rc %x", rc)); | 
|---|
| 507 | } | 
|---|
| 508 | else | 
|---|
| 509 | { | 
|---|
| 510 | rc = heightSrc; | 
|---|
| 511 |  | 
|---|
| 512 | DIBSection *destdib = DIBSection::findHDC(hdc); | 
|---|
| 513 | if(destdib) { | 
|---|
| 514 | if(widthDst == widthSrc && heightDst == heightSrc && | 
|---|
| 515 | destdib->GetBitCount() == info->bmiHeader.biBitCount && | 
|---|
| 516 | destdib->GetBitCount() == 8) | 
|---|
| 517 | { | 
|---|
| 518 | destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits); | 
|---|
| 519 | } | 
|---|
| 520 | else destdib->sync(hdc, yDst, heightDst); | 
|---|
| 521 | } | 
|---|
| 522 | } | 
|---|
| 523 |  | 
|---|
| 524 | return rc; | 
|---|
| 525 | } | 
|---|
| 526 | //****************************************************************************** | 
|---|
| 527 | //****************************************************************************** | 
|---|
| 528 | INT WIN32API StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst, | 
|---|
| 529 | INT heightDst, INT xSrc, INT ySrc, INT widthSrc, | 
|---|
| 530 | INT heightSrc, const void *bits, | 
|---|
| 531 | const BITMAPINFO *info, UINT wUsage, DWORD dwRop ) | 
|---|
| 532 | { | 
|---|
| 533 |  | 
|---|
| 534 | if(info->bmiHeader.biHeight < 0) { | 
|---|
| 535 | // upside down | 
|---|
| 536 | INT rc = 0; | 
|---|
| 537 | BITMAPINFO newInfo; | 
|---|
| 538 | newInfo.bmiHeader = info->bmiHeader; | 
|---|
| 539 | long lLineByte = ((newInfo.bmiHeader.biWidth * (info->bmiHeader.biBitCount == 15 ? 16 : info->bmiHeader.biBitCount) + 31) / 32) * 4; | 
|---|
| 540 | long lHeight   = -newInfo.bmiHeader.biHeight; | 
|---|
| 541 | newInfo.bmiHeader.biHeight = -newInfo.bmiHeader.biHeight; | 
|---|
| 542 |  | 
|---|
| 543 | //TODO: doesn't work if memory is readonly!! | 
|---|
| 544 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight; | 
|---|
| 545 |  | 
|---|
| 546 | char *newBits = (char *)malloc( lLineByte * lHeight ); | 
|---|
| 547 | if(newBits) { | 
|---|
| 548 | unsigned char *pbSrc = (unsigned char *)bits + lLineByte * (lHeight - 1); | 
|---|
| 549 | unsigned char *pbDst = (unsigned char *)newBits; | 
|---|
| 550 | for(int y = 0; y < lHeight; y++) { | 
|---|
| 551 | memcpy( pbDst, pbSrc, lLineByte ); | 
|---|
| 552 | pbDst += lLineByte; | 
|---|
| 553 | pbSrc -= lLineByte; | 
|---|
| 554 | } | 
|---|
| 555 | rc = StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, newBits, info, wUsage, dwRop); | 
|---|
| 556 | free( newBits ); | 
|---|
| 557 | } | 
|---|
| 558 |  | 
|---|
| 559 | //TODO: doesn't work if memory is readonly!! | 
|---|
| 560 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight; | 
|---|
| 561 | return rc; | 
|---|
| 562 | } else { | 
|---|
| 563 | return StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, bits, info, wUsage, dwRop); | 
|---|
| 564 | } | 
|---|
| 565 | } | 
|---|
| 566 | //****************************************************************************** | 
|---|
| 567 | //****************************************************************************** | 
|---|
| 568 | int WIN32API SetStretchBltMode( HDC arg1, int  arg2) | 
|---|
| 569 | { | 
|---|
| 570 | if(DIBSection::getSection() != NULL) | 
|---|
| 571 | { | 
|---|
| 572 | DIBSection *dsect = DIBSection::findHDC(arg1); | 
|---|
| 573 | if(dsect) | 
|---|
| 574 | { | 
|---|
| 575 | dprintf(("       - DC is DIBSection\n")); | 
|---|
| 576 | } | 
|---|
| 577 | } | 
|---|
| 578 | return O32_SetStretchBltMode(arg1, arg2); | 
|---|
| 579 | } | 
|---|
| 580 | //****************************************************************************** | 
|---|
| 581 | //****************************************************************************** | 
|---|
| 582 | int WIN32API GetStretchBltMode( HDC arg1) | 
|---|
| 583 | { | 
|---|
| 584 | return O32_GetStretchBltMode(arg1); | 
|---|
| 585 | } | 
|---|
| 586 | //****************************************************************************** | 
|---|
| 587 | //****************************************************************************** | 
|---|
| 588 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr) | 
|---|
| 589 | { | 
|---|
| 590 | ULONG cbPalette; | 
|---|
| 591 |  | 
|---|
| 592 | switch (pBHdr->biBitCount) | 
|---|
| 593 | { | 
|---|
| 594 | case 1: | 
|---|
| 595 | case 4: | 
|---|
| 596 | case 8: | 
|---|
| 597 | cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD); | 
|---|
| 598 | break; | 
|---|
| 599 |  | 
|---|
| 600 | case 16: | 
|---|
| 601 | case 24: | 
|---|
| 602 | case 32: | 
|---|
| 603 | cbPalette = 0; | 
|---|
| 604 | break; | 
|---|
| 605 |  | 
|---|
| 606 | default: | 
|---|
| 607 | dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount)); | 
|---|
| 608 | cbPalette = -1; | 
|---|
| 609 | } | 
|---|
| 610 |  | 
|---|
| 611 | return cbPalette; | 
|---|
| 612 | } | 
|---|
| 613 | //****************************************************************************** | 
|---|
| 614 | //****************************************************************************** | 
|---|
| 615 | ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy) | 
|---|
| 616 | { | 
|---|
| 617 | ULONG alignment; | 
|---|
| 618 | ULONG factor; | 
|---|
| 619 | BOOL flag = TRUE;       //true: '*'     false: '/' | 
|---|
| 620 |  | 
|---|
| 621 | cy = cy < 0 ? -cy : cy; | 
|---|
| 622 |  | 
|---|
| 623 | switch(cBits) | 
|---|
| 624 | { | 
|---|
| 625 | case 1: | 
|---|
| 626 | factor = 8; | 
|---|
| 627 | flag = FALSE; | 
|---|
| 628 | break; | 
|---|
| 629 |  | 
|---|
| 630 | case 4: | 
|---|
| 631 | factor = 2; | 
|---|
| 632 | flag = FALSE; | 
|---|
| 633 | break; | 
|---|
| 634 |  | 
|---|
| 635 | case 8: | 
|---|
| 636 | factor = 1; | 
|---|
| 637 | break; | 
|---|
| 638 |  | 
|---|
| 639 | case 16: | 
|---|
| 640 | factor = 2; | 
|---|
| 641 | break; | 
|---|
| 642 |  | 
|---|
| 643 | case 24: | 
|---|
| 644 | factor = 3; | 
|---|
| 645 | break; | 
|---|
| 646 |  | 
|---|
| 647 | case 32: | 
|---|
| 648 | return cx*cy*4; | 
|---|
| 649 |  | 
|---|
| 650 | default: | 
|---|
| 651 | return 0; | 
|---|
| 652 | } | 
|---|
| 653 |  | 
|---|
| 654 | if (flag) | 
|---|
| 655 | alignment = (cx = (cx*factor)) % 4; | 
|---|
| 656 | else | 
|---|
| 657 | alignment = (cx = ((cx+factor-1)/factor)) % 4; | 
|---|
| 658 |  | 
|---|
| 659 | if (alignment != 0) | 
|---|
| 660 | cx += 4 - alignment; | 
|---|
| 661 |  | 
|---|
| 662 | return cx*cy; | 
|---|
| 663 | } | 
|---|
| 664 | //****************************************************************************** | 
|---|
| 665 | //****************************************************************************** | 
|---|