| 1 | /* $Id: blit.cpp,v 1.6 2000-02-16 19:48:52 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 "misc.h" | 
|---|
| 18 | #include "dibsect.h" | 
|---|
| 19 | #include "rgbcvt.h" | 
|---|
| 20 |  | 
|---|
| 21 | #define DBG_LOCALLOG    DBG_blit | 
|---|
| 22 | #include "dbglocal.h" | 
|---|
| 23 |  | 
|---|
| 24 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr); | 
|---|
| 25 | static ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy); | 
|---|
| 26 |  | 
|---|
| 27 | //****************************************************************************** | 
|---|
| 28 | //****************************************************************************** | 
|---|
| 29 | BOOL WIN32API StretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest, | 
|---|
| 30 | int nWidthDest, int nHeightDest, | 
|---|
| 31 | HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, | 
|---|
| 32 | int nWidthSrc, int nHeightSrc, DWORD dwRop) | 
|---|
| 33 | { | 
|---|
| 34 | BOOL rc; | 
|---|
| 35 |  | 
|---|
| 36 | dprintf(("GDI32: StretchBlt Dest: %x (%d, %d) size (%d, %d)\n", | 
|---|
| 37 | hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest)); | 
|---|
| 38 | dprintf(("GDI32: StretchBlt Src : %x (%d, %d) size (%d, %d)\n", | 
|---|
| 39 | hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc)); | 
|---|
| 40 | if(DIBSection::getSection() != NULL) | 
|---|
| 41 | { | 
|---|
| 42 | DIBSection *dsect = DIBSection::findHDC(hdcSrc); | 
|---|
| 43 | if(dsect) | 
|---|
| 44 | { | 
|---|
| 45 | dprintf((" Do stretched DIB Blt\n")); | 
|---|
| 46 | rc  = dsect->BitBlt( hdcDest, | 
|---|
| 47 | nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, | 
|---|
| 48 | nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, | 
|---|
| 49 | dwRop); | 
|---|
| 50 | return rc; | 
|---|
| 51 | } | 
|---|
| 52 | } | 
|---|
| 53 | return O32_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop); | 
|---|
| 54 | } | 
|---|
| 55 | //****************************************************************************** | 
|---|
| 56 | //****************************************************************************** | 
|---|
| 57 | BOOL WIN32API BitBlt(HDC hdcDest, int arg2, int arg3, int arg4, int arg5, HDC hdcSrc, int arg7, int arg8, DWORD  arg9) | 
|---|
| 58 | { | 
|---|
| 59 | BOOL rc; | 
|---|
| 60 |  | 
|---|
| 61 | if(DIBSection::getSection() != NULL) { | 
|---|
| 62 | DIBSection *dsect = DIBSection::findHDC(hdcSrc); | 
|---|
| 63 | if(dsect) { | 
|---|
| 64 | rc = dsect->BitBlt(hdcDest, arg2, arg3, arg4, arg5, arg7, arg8, arg4, arg5, arg9); | 
|---|
| 65 | if(rc) { | 
|---|
| 66 | BITMAPINFO bmpinfo = {0}; | 
|---|
| 67 | DIBSection *dest = DIBSection::findHDC(hdcDest); | 
|---|
| 68 | if(dest) { | 
|---|
| 69 | dprintf(("Sync dest DIB section")); | 
|---|
| 70 | bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | 
|---|
| 71 | GetDIBits(hdcDest, dest->GetBitmapHandle(), 0, 300, 0, &bmpinfo, dest->GetRGBUsage()); | 
|---|
| 72 | dprintf(("height        %d", bmpinfo.bmiHeader.biHeight)); | 
|---|
| 73 | dprintf(("width         %d", bmpinfo.bmiHeader.biWidth)); | 
|---|
| 74 | dprintf(("biBitCount    %d", bmpinfo.bmiHeader.biBitCount)); | 
|---|
| 75 | GetDIBits(hdcDest, dest->GetBitmapHandle(), 0, 300, dest->GetDIBObject(), &bmpinfo, dest->GetRGBUsage()); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 | return rc; | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 | dprintf(("GDI32: BitBlt to hdc %X from (%d,%d) to (%d,%d), (%d,%d) rop %X\n", hdcDest, arg7, arg8, arg2, arg3, arg4, arg5, arg9)); | 
|---|
| 82 | return O32_BitBlt(hdcDest, arg2, arg3, arg4, arg5, hdcSrc, arg7, arg8, arg9); | 
|---|
| 83 | } | 
|---|
| 84 | //****************************************************************************** | 
|---|
| 85 | //****************************************************************************** | 
|---|
| 86 | INT WIN32API SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx, | 
|---|
| 87 | DWORD cy, INT xSrc, INT ySrc, | 
|---|
| 88 | UINT startscan, UINT lines, LPCVOID bits, | 
|---|
| 89 | const BITMAPINFO *info, UINT coloruse) | 
|---|
| 90 | { | 
|---|
| 91 | INT result, imgsize, palsize, height, width; | 
|---|
| 92 | char *ptr; | 
|---|
| 93 | ULONG compression = 0, iHeight, bmpsize; | 
|---|
| 94 | WORD *newbits = 0; | 
|---|
| 95 |  | 
|---|
| 96 | SetLastError(0); | 
|---|
| 97 | if(info == NULL) { | 
|---|
| 98 | goto invalid_parameter; | 
|---|
| 99 | } | 
|---|
| 100 | height = info->bmiHeader.biHeight; | 
|---|
| 101 | width  = info->bmiHeader.biWidth; | 
|---|
| 102 |  | 
|---|
| 103 | if (height < 0) height = -height; | 
|---|
| 104 | if (!lines || (startscan >= height)) { | 
|---|
| 105 | goto invalid_parameter; | 
|---|
| 106 | } | 
|---|
| 107 | if (startscan + lines > height) lines = height - startscan; | 
|---|
| 108 |  | 
|---|
| 109 | if (ySrc < startscan) ySrc = startscan; | 
|---|
| 110 | else if (ySrc >= startscan + lines) goto invalid_parameter; | 
|---|
| 111 |  | 
|---|
| 112 | if (xSrc >= width) goto invalid_parameter; | 
|---|
| 113 |  | 
|---|
| 114 | if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc; | 
|---|
| 115 |  | 
|---|
| 116 | if (xSrc + cx >= width) cx = width - xSrc; | 
|---|
| 117 |  | 
|---|
| 118 | if (!cx || !cy) goto invalid_parameter; | 
|---|
| 119 |  | 
|---|
| 120 | // EB: ->>> Crazy. Nobody seen this Open32 bug ? | 
|---|
| 121 | // Dont't like dirty pointers, but Open32 needs a bit help. | 
|---|
| 122 | // Only tested with winmine. | 
|---|
| 123 | palsize = QueryPaletteSize((BITMAPINFOHEADER*)&info->bmiHeader); | 
|---|
| 124 | imgsize = CalcBitmapSize(info->bmiHeader.biBitCount, | 
|---|
| 125 | info->bmiHeader.biWidth, info->bmiHeader.biHeight); | 
|---|
| 126 | ptr = ((char *)info) + palsize + sizeof(BITMAPINFOHEADER); | 
|---|
| 127 | if(bits >= ptr && bits < ptr + imgsize) | 
|---|
| 128 | { | 
|---|
| 129 | bits = (char *)bits - imgsize + | 
|---|
| 130 | CalcBitmapSize(info->bmiHeader.biBitCount, | 
|---|
| 131 | info->bmiHeader.biWidth, lines); | 
|---|
| 132 | } | 
|---|
| 133 | // EB: <<<- | 
|---|
| 134 |  | 
|---|
| 135 | //SvL: RP7's bitmap size is not correct; fix it here or else | 
|---|
| 136 | //     the blit is messed up in Open32 | 
|---|
| 137 | bmpsize = info->bmiHeader.biSizeImage; | 
|---|
| 138 | if(info->bmiHeader.biSizeImage && info->bmiHeader.biSizeImage < imgsize) | 
|---|
| 139 | { | 
|---|
| 140 | ((BITMAPINFO *)info)->bmiHeader.biSizeImage = imgsize; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | //SvL: Ignore BI_BITFIELDS type (SetDIBitsToDevice fails otherwise) | 
|---|
| 144 | if(info->bmiHeader.biCompression == BI_BITFIELDS) { | 
|---|
| 145 | DWORD *bitfields = (DWORD *)info->bmiColors; | 
|---|
| 146 |  | 
|---|
| 147 | ((BITMAPINFO *)info)->bmiHeader.biCompression = 0; | 
|---|
| 148 | compression = BI_BITFIELDS; | 
|---|
| 149 |  | 
|---|
| 150 | if(*(bitfields+1) == 0x3E0) | 
|---|
| 151 | {//RGB 555? | 
|---|
| 152 | dprintf(("BI_BITFIELDS compression %x %x %x", *bitfields, *(bitfields+1), *(bitfields+2))); | 
|---|
| 153 |  | 
|---|
| 154 | newbits = (WORD *)malloc(imgsize); | 
|---|
| 155 | if(CPUFeatures & CPUID_MMX) { | 
|---|
| 156 | RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD)); | 
|---|
| 157 | } | 
|---|
| 158 | else    RGB555to565(newbits, (WORD *)bits, imgsize/sizeof(WORD)); | 
|---|
| 159 | bits = newbits; | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | iHeight = info->bmiHeader.biHeight; | 
|---|
| 164 | if(info->bmiHeader.biHeight < 0) { | 
|---|
| 165 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight; | 
|---|
| 166 | } | 
|---|
| 167 | result = O32_SetDIBitsToDevice(hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (PVOID) bits, (PBITMAPINFO)info, coloruse); | 
|---|
| 168 | //SvL: Wrong Open32 return value | 
|---|
| 169 | result = (result == TRUE) ? lines : 0; | 
|---|
| 170 |  | 
|---|
| 171 | 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", | 
|---|
| 172 | hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse, result)); | 
|---|
| 173 | 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)); | 
|---|
| 174 |  | 
|---|
| 175 | if(compression == BI_BITFIELDS) { | 
|---|
| 176 | ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS; | 
|---|
| 177 | if(newbits) free(newbits); | 
|---|
| 178 | } | 
|---|
| 179 | ((BITMAPINFO *)info)->bmiHeader.biHeight = iHeight; | 
|---|
| 180 | ((BITMAPINFO *)info)->bmiHeader.biSizeImage = bmpsize; | 
|---|
| 181 | return result; | 
|---|
| 182 |  | 
|---|
| 183 | invalid_parameter: | 
|---|
| 184 | SetLastError(ERROR_INVALID_PARAMETER); | 
|---|
| 185 | return 0; | 
|---|
| 186 | } | 
|---|
| 187 | //****************************************************************************** | 
|---|
| 188 | //****************************************************************************** | 
|---|
| 189 | BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop) | 
|---|
| 190 | { | 
|---|
| 191 | BOOL rc; | 
|---|
| 192 |  | 
|---|
| 193 | //CB: Open32 bug: negative width/height not supported! | 
|---|
| 194 | if (nWidth < 0) | 
|---|
| 195 | { | 
|---|
| 196 | nXLeft += nWidth+1; | 
|---|
| 197 | nWidth = -nWidth; | 
|---|
| 198 | } | 
|---|
| 199 | if (nHeight < 0) | 
|---|
| 200 | { | 
|---|
| 201 | nYLeft += nHeight+1; | 
|---|
| 202 | nHeight = -nHeight; | 
|---|
| 203 | } | 
|---|
| 204 | rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop); | 
|---|
| 205 | dprintf(("GDI32: PatBlt (%d,%d) (%d,%d) returned %d\n",nXLeft,nYLeft,nWidth,nHeight,rc)); | 
|---|
| 206 | return(rc); | 
|---|
| 207 | } | 
|---|
| 208 | //****************************************************************************** | 
|---|
| 209 | //****************************************************************************** | 
|---|
| 210 | 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) | 
|---|
| 211 | { | 
|---|
| 212 | dprintf(("GDI32: MaskBlt")); | 
|---|
| 213 | return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); | 
|---|
| 214 | } | 
|---|
| 215 | //****************************************************************************** | 
|---|
| 216 | //****************************************************************************** | 
|---|
| 217 | BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc, | 
|---|
| 218 | int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask, | 
|---|
| 219 | int xMast, int yMask) | 
|---|
| 220 | { | 
|---|
| 221 | dprintf(("GDI32: PlgBlt, not implemented\n")); | 
|---|
| 222 | return(FALSE); | 
|---|
| 223 | } | 
|---|
| 224 | //****************************************************************************** | 
|---|
| 225 | //****************************************************************************** | 
|---|
| 226 | int WIN32API SetStretchBltMode( HDC arg1, int  arg2) | 
|---|
| 227 | { | 
|---|
| 228 | dprintf(("GDI32: SetStretchBltMode 0x%08X, 0x%08X\n",arg1, arg2)); | 
|---|
| 229 |  | 
|---|
| 230 | if(DIBSection::getSection() != NULL) | 
|---|
| 231 | { | 
|---|
| 232 | DIBSection *dsect = DIBSection::findHDC(arg1); | 
|---|
| 233 | if(dsect) | 
|---|
| 234 | { | 
|---|
| 235 | dprintf(("       - DC is DIBSection\n")); | 
|---|
| 236 | } | 
|---|
| 237 | } | 
|---|
| 238 | return O32_SetStretchBltMode(arg1, arg2); | 
|---|
| 239 | } | 
|---|
| 240 | //****************************************************************************** | 
|---|
| 241 | //****************************************************************************** | 
|---|
| 242 | int WIN32API GetStretchBltMode( HDC arg1) | 
|---|
| 243 | { | 
|---|
| 244 | dprintf(("GDI32: GetStretchBltMode")); | 
|---|
| 245 | return O32_GetStretchBltMode(arg1); | 
|---|
| 246 | } | 
|---|
| 247 | //****************************************************************************** | 
|---|
| 248 | //****************************************************************************** | 
|---|
| 249 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr) | 
|---|
| 250 | { | 
|---|
| 251 | ULONG cbPalette; | 
|---|
| 252 |  | 
|---|
| 253 | switch (pBHdr->biBitCount) | 
|---|
| 254 | { | 
|---|
| 255 | case 1: | 
|---|
| 256 | case 4: | 
|---|
| 257 | case 8: | 
|---|
| 258 | cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD); | 
|---|
| 259 | break; | 
|---|
| 260 |  | 
|---|
| 261 | case 16: | 
|---|
| 262 | case 24: | 
|---|
| 263 | case 32: | 
|---|
| 264 | cbPalette = 0; | 
|---|
| 265 | break; | 
|---|
| 266 |  | 
|---|
| 267 | default: | 
|---|
| 268 | dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount)); | 
|---|
| 269 | cbPalette = -1; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | return cbPalette; | 
|---|
| 273 | } | 
|---|
| 274 | //****************************************************************************** | 
|---|
| 275 | //****************************************************************************** | 
|---|
| 276 | static ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy) | 
|---|
| 277 | { | 
|---|
| 278 | ULONG alignment; | 
|---|
| 279 | ULONG factor; | 
|---|
| 280 | BOOL flag = TRUE;       //true: '*'     false: '/' | 
|---|
| 281 |  | 
|---|
| 282 | cy = cy < 0 ? -cy : cy; | 
|---|
| 283 |  | 
|---|
| 284 | switch(cBits) | 
|---|
| 285 | { | 
|---|
| 286 | case 1: | 
|---|
| 287 | factor = 8; | 
|---|
| 288 | flag = FALSE; | 
|---|
| 289 | break; | 
|---|
| 290 |  | 
|---|
| 291 | case 4: | 
|---|
| 292 | factor = 2; | 
|---|
| 293 | flag = FALSE; | 
|---|
| 294 | break; | 
|---|
| 295 |  | 
|---|
| 296 | case 8: | 
|---|
| 297 | factor = 1; | 
|---|
| 298 | break; | 
|---|
| 299 |  | 
|---|
| 300 | case 16: | 
|---|
| 301 | factor = 2; | 
|---|
| 302 | break; | 
|---|
| 303 |  | 
|---|
| 304 | case 24: | 
|---|
| 305 | factor = 3; | 
|---|
| 306 | break; | 
|---|
| 307 |  | 
|---|
| 308 | case 32: | 
|---|
| 309 | return cx*cy; | 
|---|
| 310 |  | 
|---|
| 311 | default: | 
|---|
| 312 | return 0; | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | if (flag) | 
|---|
| 316 | alignment = (cx = (cx*factor)) % 4; | 
|---|
| 317 | else | 
|---|
| 318 | alignment = (cx = ((cx+factor-1)/factor)) % 4; | 
|---|
| 319 |  | 
|---|
| 320 | if (alignment != 0) | 
|---|
| 321 | cx += 4 - alignment; | 
|---|
| 322 |  | 
|---|
| 323 | return cx*cy; | 
|---|
| 324 | } | 
|---|
| 325 | //****************************************************************************** | 
|---|
| 326 | //****************************************************************************** | 
|---|