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