| 1 | /* $Id: dibsect.cpp,v 1.67 2003-07-16 10:46:17 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * GDI32 DIB sections | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998-2000 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 | * NOTE: | 
|---|
| 12 | * This is not a complete solution for CreateDIBSection, but enough for Quake 2! | 
|---|
| 13 | * | 
|---|
| 14 | */ | 
|---|
| 15 | #define  INCL_GPI | 
|---|
| 16 | #define  INCL_WIN | 
|---|
| 17 | #include <os2wrap.h>  //Odin32 OS/2 api wrappers | 
|---|
| 18 | #include <stdlib.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <win32type.h> | 
|---|
| 21 | #include <misc.h> | 
|---|
| 22 | #include <win32api.h> | 
|---|
| 23 | #include <winconst.h> | 
|---|
| 24 | #include <winuser32.h> | 
|---|
| 25 | #include <dcdata.h> | 
|---|
| 26 | #include "dibsect.h" | 
|---|
| 27 | #include "oslibgpi.h" | 
|---|
| 28 | #include "rgbcvt.h" | 
|---|
| 29 |  | 
|---|
| 30 | #define DBG_LOCALLOG  DBG_dibsect | 
|---|
| 31 | #include "dbglocal.h" | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | //****************************************************************************** | 
|---|
| 35 | //****************************************************************************** | 
|---|
| 36 | DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip) | 
|---|
| 37 | : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsDblBuffer(NULL), | 
|---|
| 38 | hdc(0), hwndParent(0) | 
|---|
| 39 | { | 
|---|
| 40 | int  palsize=0; | 
|---|
| 41 |  | 
|---|
| 42 | bmpsize = pbmi->biWidth; | 
|---|
| 43 | /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */ | 
|---|
| 44 |  | 
|---|
| 45 | this->fFlip = fFlip; | 
|---|
| 46 | os2bmphdrsize = sizeof(BITMAPINFO2); | 
|---|
| 47 |  | 
|---|
| 48 | switch(pbmi->biBitCount) | 
|---|
| 49 | { | 
|---|
| 50 | case 1: | 
|---|
| 51 | bmpsize = ((bmpsize + 31) & ~31) / 8; | 
|---|
| 52 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 53 | os2bmphdrsize += palsize; | 
|---|
| 54 | break; | 
|---|
| 55 | case 4: | 
|---|
| 56 | bmpsize = ((bmpsize + 7) & ~7) / 2; | 
|---|
| 57 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 58 | os2bmphdrsize += palsize; | 
|---|
| 59 | break; | 
|---|
| 60 | case 8: | 
|---|
| 61 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 62 | os2bmphdrsize += palsize; | 
|---|
| 63 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 64 | break; | 
|---|
| 65 | case 16: | 
|---|
| 66 | bmpsize *= 2; | 
|---|
| 67 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 68 | break; | 
|---|
| 69 | case 24: | 
|---|
| 70 | bmpsize *= 3; | 
|---|
| 71 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 72 | break; | 
|---|
| 73 | case 32: | 
|---|
| 74 | bmpsize *= 4; | 
|---|
| 75 | break; | 
|---|
| 76 | default: | 
|---|
| 77 | dprintf(("Unsupported nr of bits %d", pbmi->biBitCount)); | 
|---|
| 78 | DebugInt3(); | 
|---|
| 79 | break; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | this->hSection = hSection; | 
|---|
| 83 | this->dwOffset = dwOffset; | 
|---|
| 84 | if(hSection) { | 
|---|
| 85 | bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight - dwOffset); | 
|---|
| 86 | if(!bmpBits) { | 
|---|
| 87 | dprintf(("Dibsection: mapViewOfFile %x failed!", hSection)); | 
|---|
| 88 | DebugInt3(); | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | if(!bmpBits) { | 
|---|
| 92 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT); | 
|---|
| 93 | } | 
|---|
| 94 | memset(bmpBits, 0, bmpsize*pbmi->biHeight); | 
|---|
| 95 |  | 
|---|
| 96 | pOS2bmp = (BITMAPINFO2 *)malloc(os2bmphdrsize); | 
|---|
| 97 |  | 
|---|
| 98 | memset(pOS2bmp, /* set header + palette entries to zero */ | 
|---|
| 99 | 0, | 
|---|
| 100 | os2bmphdrsize); | 
|---|
| 101 |  | 
|---|
| 102 | pOS2bmp->cbFix         = sizeof(BITMAPINFO2) - sizeof(RGB2); | 
|---|
| 103 | pOS2bmp->cx            = pbmi->biWidth; | 
|---|
| 104 | pOS2bmp->cy            = pbmi->biHeight; | 
|---|
| 105 | pOS2bmp->cPlanes       = pbmi->biPlanes; | 
|---|
| 106 | pOS2bmp->cBitCount     = pbmi->biBitCount; | 
|---|
| 107 | pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4) | 
|---|
| 108 | //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise) | 
|---|
| 109 | if(pOS2bmp->ulCompression == BI_BITFIELDS_W) { | 
|---|
| 110 | pOS2bmp->ulCompression = 0; | 
|---|
| 111 | } | 
|---|
| 112 | pOS2bmp->cbImage       = pbmi->biSizeImage; | 
|---|
| 113 | dprintf(("handle                 %x", handle)); | 
|---|
| 114 | dprintf(("pOS2bmp->cx            %d\n", pOS2bmp->cx)); | 
|---|
| 115 | dprintf(("pOS2bmp->cy            %d\n", pOS2bmp->cy)); | 
|---|
| 116 | dprintf(("pOS2bmp->cPlanes       %d\n", pOS2bmp->cPlanes)); | 
|---|
| 117 | dprintf(("pOS2bmp->cBitCount     %d\n", pOS2bmp->cBitCount)); | 
|---|
| 118 | dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression)); | 
|---|
| 119 | dprintf(("pOS2bmp->cbImage       %d\n", pOS2bmp->cbImage)); | 
|---|
| 120 | dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight)); | 
|---|
| 121 |  | 
|---|
| 122 | // clear DIBSECTION structure | 
|---|
| 123 | memset(&dibinfo, 0, sizeof(dibinfo)); | 
|---|
| 124 |  | 
|---|
| 125 | // copy BITMAPINFOHEADER data into DIBSECTION structure | 
|---|
| 126 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi)); | 
|---|
| 127 | dibinfo.dsBm.bmType      = 0; | 
|---|
| 128 | dibinfo.dsBm.bmWidth     = pbmi->biWidth; | 
|---|
| 129 | dibinfo.dsBm.bmHeight    = pbmi->biHeight; | 
|---|
| 130 | dibinfo.dsBm.bmWidthBytes= bmpsize; | 
|---|
| 131 | dibinfo.dsBm.bmPlanes    = pbmi->biPlanes; | 
|---|
| 132 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount; | 
|---|
| 133 | dibinfo.dsBm.bmBits      = bmpBits; | 
|---|
| 134 |  | 
|---|
| 135 | dibinfo.dshSection       = hSection; | 
|---|
| 136 | dibinfo.dsOffset         = dwOffset; | 
|---|
| 137 |  | 
|---|
| 138 | if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8) | 
|---|
| 139 | { | 
|---|
| 140 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0; | 
|---|
| 141 | if(palsize) { | 
|---|
| 142 | SetDIBColorTable(0, (pbmi->biClrUsed) ? pbmi->biClrUsed : (1 << pbmi->biBitCount), (RGBQUAD *)(pbmi+1)); | 
|---|
| 143 | } | 
|---|
| 144 | } | 
|---|
| 145 | else { | 
|---|
| 146 | switch(pbmi->biBitCount) | 
|---|
| 147 | { | 
|---|
| 148 | case 16: | 
|---|
| 149 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00; | 
|---|
| 150 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0; | 
|---|
| 151 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f; | 
|---|
| 152 | break; | 
|---|
| 153 |  | 
|---|
| 154 | case 24: | 
|---|
| 155 | case 32: | 
|---|
| 156 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors       : 0xff0000; | 
|---|
| 157 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x00ff00; | 
|---|
| 158 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x0000ff; | 
|---|
| 159 | if(dibinfo.dsBitfields[0] != 0xff0000 && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff) { | 
|---|
| 160 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!")); | 
|---|
| 161 | } | 
|---|
| 162 | break; | 
|---|
| 163 | } | 
|---|
| 164 | dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2])); | 
|---|
| 165 | } | 
|---|
| 166 | //double buffer for rgb 555 dib sections (for conversion) or flipped sections | 
|---|
| 167 | if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) { | 
|---|
| 168 | DosAllocMem((PPVOID)&bmpBitsDblBuffer, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | this->handle = handle; | 
|---|
| 172 | this->iUsage = iUsage; | 
|---|
| 173 |  | 
|---|
| 174 | lock(); | 
|---|
| 175 | if(section == NULL) | 
|---|
| 176 | { | 
|---|
| 177 | dprintf(("section was NULL\n")); | 
|---|
| 178 | section = this; | 
|---|
| 179 | } | 
|---|
| 180 | else | 
|---|
| 181 | { | 
|---|
| 182 | DIBSection *dsect = section; | 
|---|
| 183 | dprintf2(("Increment section starting at %08X\n",dsect)); | 
|---|
| 184 |  | 
|---|
| 185 | while ( (dsect->next != this) && | 
|---|
| 186 | (dsect->next != NULL) ) | 
|---|
| 187 | { | 
|---|
| 188 | ////       dprintf2(("Increment section to %08X\n",dsect->next)); | 
|---|
| 189 | dsect = dsect->next; | 
|---|
| 190 | } | 
|---|
| 191 | dsect->next = this; | 
|---|
| 192 | } | 
|---|
| 193 | unlock(); | 
|---|
| 194 | } | 
|---|
| 195 | //****************************************************************************** | 
|---|
| 196 | //****************************************************************************** | 
|---|
| 197 | DIBSection::~DIBSection() | 
|---|
| 198 | { | 
|---|
| 199 | dprintf(("Delete DIBSection %x", handle)); | 
|---|
| 200 |  | 
|---|
| 201 | if(hSection) { | 
|---|
| 202 | UnmapViewOfFile(bmpBits); | 
|---|
| 203 | } | 
|---|
| 204 | else | 
|---|
| 205 | if(bmpBits) | 
|---|
| 206 | DosFreeMem(bmpBits); | 
|---|
| 207 |  | 
|---|
| 208 | if(bmpBitsDblBuffer) | 
|---|
| 209 | DosFreeMem(bmpBitsDblBuffer); | 
|---|
| 210 |  | 
|---|
| 211 | if(pOS2bmp) | 
|---|
| 212 | free(pOS2bmp); | 
|---|
| 213 |  | 
|---|
| 214 | lock(); | 
|---|
| 215 | if(section == this) | 
|---|
| 216 | { | 
|---|
| 217 | section = this->next; | 
|---|
| 218 | } | 
|---|
| 219 | else | 
|---|
| 220 | { | 
|---|
| 221 | DIBSection *dsect = section; | 
|---|
| 222 |  | 
|---|
| 223 | while(dsect->next != this) | 
|---|
| 224 | { | 
|---|
| 225 | dsect = dsect->next; | 
|---|
| 226 | } | 
|---|
| 227 | dsect->next = this->next; | 
|---|
| 228 | } | 
|---|
| 229 | unlock(); | 
|---|
| 230 | } | 
|---|
| 231 | //****************************************************************************** | 
|---|
| 232 | //****************************************************************************** | 
|---|
| 233 | int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT | 
|---|
| 234 | lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi, | 
|---|
| 235 | UINT coloruse) | 
|---|
| 236 | { | 
|---|
| 237 | lines = (int)lines >= 0 ? (int)lines : (int)-lines; | 
|---|
| 238 | int  palsize=0; | 
|---|
| 239 |  | 
|---|
| 240 | bmpsize = pbmi->biWidth; | 
|---|
| 241 | os2bmphdrsize = sizeof(BITMAPINFO2); | 
|---|
| 242 |  | 
|---|
| 243 | switch(pbmi->biBitCount) | 
|---|
| 244 | { | 
|---|
| 245 | case 1: | 
|---|
| 246 | bmpsize = ((bmpsize + 31) & ~31) / 8; | 
|---|
| 247 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 248 | os2bmphdrsize += palsize; | 
|---|
| 249 | break; | 
|---|
| 250 | case 4: | 
|---|
| 251 | bmpsize = ((bmpsize + 7) & ~7) / 2; | 
|---|
| 252 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 253 | os2bmphdrsize += palsize; | 
|---|
| 254 | break; | 
|---|
| 255 | case 8: | 
|---|
| 256 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2); | 
|---|
| 257 | os2bmphdrsize += palsize; | 
|---|
| 258 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 259 | break; | 
|---|
| 260 | case 16: | 
|---|
| 261 | bmpsize *= 2; | 
|---|
| 262 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 263 | break; | 
|---|
| 264 | case 24: | 
|---|
| 265 | bmpsize *= 3; | 
|---|
| 266 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 267 | break; | 
|---|
| 268 | case 32: | 
|---|
| 269 | bmpsize *= 4; | 
|---|
| 270 | break; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | //SvL: TODO: Correct?? | 
|---|
| 274 | if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight && | 
|---|
| 275 | pOS2bmp->cBitCount != pbmi->biBitCount) | 
|---|
| 276 | { | 
|---|
| 277 | char *oldbits = bmpBits; | 
|---|
| 278 | int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight; | 
|---|
| 279 |  | 
|---|
| 280 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT); | 
|---|
| 281 | memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight)); | 
|---|
| 282 | DosFreeMem(oldbits); | 
|---|
| 283 | } | 
|---|
| 284 | pOS2bmp    = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmphdrsize); | 
|---|
| 285 | pOS2bmp->cbFix         = sizeof(BITMAPINFO2) - sizeof(RGB2); | 
|---|
| 286 | pOS2bmp->cx            = pbmi->biWidth; | 
|---|
| 287 | pOS2bmp->cy            = pbmi->biHeight; | 
|---|
| 288 | pOS2bmp->cPlanes       = pbmi->biPlanes; | 
|---|
| 289 | pOS2bmp->cBitCount     = pbmi->biBitCount; | 
|---|
| 290 | pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4) | 
|---|
| 291 | //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise) | 
|---|
| 292 | if(pOS2bmp->ulCompression == BI_BITFIELDS_W) { | 
|---|
| 293 | pOS2bmp->ulCompression = 0; | 
|---|
| 294 | } | 
|---|
| 295 | pOS2bmp->cbImage       = pbmi->biSizeImage; | 
|---|
| 296 |  | 
|---|
| 297 | // clear DIBSECTION structure | 
|---|
| 298 | memset(&dibinfo, 0, sizeof(dibinfo)); | 
|---|
| 299 |  | 
|---|
| 300 | // copy BITMAPINFOHEADER data into DIBSECTION structure | 
|---|
| 301 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi)); | 
|---|
| 302 | dibinfo.dsBm.bmType      = 0; | 
|---|
| 303 | dibinfo.dsBm.bmWidth     = pbmi->biWidth; | 
|---|
| 304 | dibinfo.dsBm.bmHeight    = pbmi->biHeight; | 
|---|
| 305 | dibinfo.dsBm.bmWidthBytes= bmpsize; | 
|---|
| 306 | dibinfo.dsBm.bmPlanes    = pbmi->biPlanes; | 
|---|
| 307 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount; | 
|---|
| 308 | dibinfo.dsBm.bmBits      = bmpBits; | 
|---|
| 309 |  | 
|---|
| 310 | dibinfo.dshSection       = hSection; | 
|---|
| 311 | dibinfo.dsOffset         = dwOffset; | 
|---|
| 312 |  | 
|---|
| 313 | if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8) | 
|---|
| 314 | { | 
|---|
| 315 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0; | 
|---|
| 316 | } | 
|---|
| 317 | else { | 
|---|
| 318 | char *pColors = (char *)pbmi + 1; | 
|---|
| 319 |  | 
|---|
| 320 | switch(pbmi->biBitCount) | 
|---|
| 321 | { | 
|---|
| 322 | case 16: | 
|---|
| 323 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00; | 
|---|
| 324 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0; | 
|---|
| 325 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f; | 
|---|
| 326 | break; | 
|---|
| 327 |  | 
|---|
| 328 | case 24: | 
|---|
| 329 | case 32: | 
|---|
| 330 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors       : 0xff0000; | 
|---|
| 331 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x00ff00; | 
|---|
| 332 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x0000ff; | 
|---|
| 333 | if(dibinfo.dsBitfields[0] != 0xff0000 && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff) { | 
|---|
| 334 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!")); | 
|---|
| 335 | } | 
|---|
| 336 | break; | 
|---|
| 337 | } | 
|---|
| 338 | dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2])); | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | //double buffer for rgb 555 dib sections (for conversion) or flipped sections | 
|---|
| 342 | if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) { | 
|---|
| 343 | if(bmpBitsDblBuffer) { | 
|---|
| 344 | DosFreeMem(bmpBitsDblBuffer); | 
|---|
| 345 | } | 
|---|
| 346 | DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression)); | 
|---|
| 350 | if(palsize) { | 
|---|
| 351 | SetDIBColorTable(0, (pbmi->biClrUsed) ? pbmi->biClrUsed : (1 << pbmi->biBitCount), (RGBQUAD *)(pbmi+1)); | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | if(bits) | 
|---|
| 355 | { | 
|---|
| 356 | if(pOS2bmp->ulCompression == BCA_UNCOMP) { | 
|---|
| 357 | int size = bmpsize*lines; | 
|---|
| 358 | memcpy(bmpBits+bmpsize*startscan, bits, size); | 
|---|
| 359 | } | 
|---|
| 360 | else { | 
|---|
| 361 | dprintf(("Compressed image!!")); | 
|---|
| 362 | if(startscan != 0) { | 
|---|
| 363 | dprintf(("WARNING: Compressed image & startscan != 0!!!!")); | 
|---|
| 364 | } | 
|---|
| 365 | memcpy(bmpBits, bits, pbmi->biSizeImage); | 
|---|
| 366 | } | 
|---|
| 367 | } | 
|---|
| 368 | return(lines); | 
|---|
| 369 | } | 
|---|
| 370 | //****************************************************************************** | 
|---|
| 371 | //****************************************************************************** | 
|---|
| 372 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb) | 
|---|
| 373 | { | 
|---|
| 374 | int i, end; | 
|---|
| 375 |  | 
|---|
| 376 | dprintf(("SetDIBColorTable %d %d %x", startIdx, cEntries, rgb)); | 
|---|
| 377 |  | 
|---|
| 378 | if(pOS2bmp->cBitCount > 8) { | 
|---|
| 379 | dprintf(("DIBSection::SetDIBColorTable: bpp > 8; ignore")); | 
|---|
| 380 | return 0; | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 | end = startIdx + cEntries; | 
|---|
| 384 | if(end > (1 << pOS2bmp->cBitCount)) { | 
|---|
| 385 | end = (1 << pOS2bmp->cBitCount); | 
|---|
| 386 | cEntries = end - startIdx; | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2)); | 
|---|
| 390 |  | 
|---|
| 391 | for(i=startIdx;i<end;i++) | 
|---|
| 392 | { | 
|---|
| 393 | pOS2bmp->argbColor[i].fcOptions = 0; | 
|---|
| 394 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) )); | 
|---|
| 395 | } | 
|---|
| 396 | return(cEntries); | 
|---|
| 397 | } | 
|---|
| 398 | //****************************************************************************** | 
|---|
| 399 | //****************************************************************************** | 
|---|
| 400 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry) | 
|---|
| 401 | { | 
|---|
| 402 | int i, end; | 
|---|
| 403 |  | 
|---|
| 404 | if(pOS2bmp->cBitCount > 8) { | 
|---|
| 405 | dprintf(("DIBSection::SetDIBColorTable: bpp > 8; ignore")); | 
|---|
| 406 | return 0; | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | end = startIdx + cEntries; | 
|---|
| 410 | if(end > (1 << pOS2bmp->cBitCount)) { | 
|---|
| 411 | end = (1 << pOS2bmp->cBitCount); | 
|---|
| 412 | } | 
|---|
| 413 | for(i=startIdx;i<end;i++) | 
|---|
| 414 | { | 
|---|
| 415 | pOS2bmp->argbColor[i].fcOptions = 0; | 
|---|
| 416 | pOS2bmp->argbColor[i].bBlue  = palentry[i].peBlue; | 
|---|
| 417 | pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen; | 
|---|
| 418 | pOS2bmp->argbColor[i].bRed   = palentry[i].peRed; | 
|---|
| 419 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) )); | 
|---|
| 420 | } | 
|---|
| 421 |  | 
|---|
| 422 | return end - startIdx; | 
|---|
| 423 | } | 
|---|
| 424 | //****************************************************************************** | 
|---|
| 425 | //****************************************************************************** | 
|---|
| 426 | int DIBSection::GetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb) | 
|---|
| 427 | { | 
|---|
| 428 | int i, end = startIdx + cEntries; | 
|---|
| 429 |  | 
|---|
| 430 | if(pOS2bmp->cBitCount > 8) { | 
|---|
| 431 | dprintf(("DIBSection::GetDIBColorTable: bpp > 8 -> return 0")); | 
|---|
| 432 | return 0; | 
|---|
| 433 | } | 
|---|
| 434 | if(end > (1 << pOS2bmp->cBitCount)) { | 
|---|
| 435 | end = (1 << pOS2bmp->cBitCount); | 
|---|
| 436 | dprintf(("DIBSection::GetDIBColorTable: %d->%d", startIdx, end)); | 
|---|
| 437 | } | 
|---|
| 438 | memcpy(rgb, &pOS2bmp->argbColor[startIdx], cEntries*sizeof(RGBQUAD)); | 
|---|
| 439 |  | 
|---|
| 440 | for(i=0;i<cEntries;i++) { | 
|---|
| 441 | rgb[i].rgbReserved = 0; | 
|---|
| 442 | } | 
|---|
| 443 |  | 
|---|
| 444 | return end - startIdx; | 
|---|
| 445 | } | 
|---|
| 446 | //****************************************************************************** | 
|---|
| 447 | //****************************************************************************** | 
|---|
| 448 | BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth, | 
|---|
| 449 | int nDestHeight, int nXsrc, int nYsrc, | 
|---|
| 450 | int nSrcWidth, int nSrcHeight, DWORD Rop) | 
|---|
| 451 | { | 
|---|
| 452 | HPS    hps = (HPS)hdcDest; | 
|---|
| 453 | POINTL point[4]; | 
|---|
| 454 | LONG   rc, hdcHeight, hdcWidth; | 
|---|
| 455 | PVOID  bitmapBits = NULL; | 
|---|
| 456 | int    oldyinversion = 0; | 
|---|
| 457 | BOOL   fRestoryYInversion = FALSE, fFrameWindowDC = FALSE; | 
|---|
| 458 | HWND   hwndDest; | 
|---|
| 459 | pDCData pHps; | 
|---|
| 460 |  | 
|---|
| 461 | pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest); | 
|---|
| 462 | if(!pHps) | 
|---|
| 463 | { | 
|---|
| 464 | SetLastError(ERROR_INVALID_HANDLE_W); | 
|---|
| 465 | return FALSE; | 
|---|
| 466 | } | 
|---|
| 467 |  | 
|---|
| 468 | hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that | 
|---|
| 469 | if(hwndDest && pHps->hwnd && !pHps->isClient) { | 
|---|
| 470 | fFrameWindowDC = TRUE; | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x", | 
|---|
| 474 | handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight, | 
|---|
| 475 | nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip)); | 
|---|
| 476 |  | 
|---|
| 477 | if(hwndDest) { | 
|---|
| 478 | RECT rect; | 
|---|
| 479 |  | 
|---|
| 480 | if(fFrameWindowDC) { | 
|---|
| 481 | GetWindowRect(hwndDest, &rect); | 
|---|
| 482 | } | 
|---|
| 483 | else  GetClientRect(hwndDest, &rect); | 
|---|
| 484 | hdcHeight = rect.bottom - rect.top; | 
|---|
| 485 | hdcWidth  = rect.right - rect.left; | 
|---|
| 486 | dprintf(("DIBSection::BitBlt hdc size (%d,%d) (WINDOW)", hdcWidth, hdcHeight)); | 
|---|
| 487 | } | 
|---|
| 488 | else { | 
|---|
| 489 | hdcHeight = pHps->bitmapHeight; | 
|---|
| 490 | hdcWidth  = pHps->bitmapWidth; | 
|---|
| 491 | dprintf(("DIBSection::BitBlt hdc size (%d,%d) (BMP)", hdcWidth, hdcHeight)); | 
|---|
| 492 | } | 
|---|
| 493 |  | 
|---|
| 494 | //Don't clip destination size to destination DC size | 
|---|
| 495 | //This messes up the two bitmaps in the opening window of Opera 6 | 
|---|
| 496 | //(choice between MDI & SDI interface) | 
|---|
| 497 |  | 
|---|
| 498 | //win32 coordinates are relative to left top, OS/2 expects left bottom | 
|---|
| 499 | //source rectangle is non-inclusive (top, right not included) | 
|---|
| 500 | //destination rectangle is incl.-inclusive (everything included) | 
|---|
| 501 |  | 
|---|
| 502 | point[0].x = nXdest; | 
|---|
| 503 | point[1].x = nXdest + nDestWidth - 1; | 
|---|
| 504 | #ifdef INVERT | 
|---|
| 505 | point[0].y = hdcHeight - nYdest - nDestHeight; | 
|---|
| 506 | point[1].y = hdcHeight - nYdest - 1; | 
|---|
| 507 | #else | 
|---|
| 508 | point[0].y = nYdest; | 
|---|
| 509 | point[1].y = nYdest + nDestHeight - 1; | 
|---|
| 510 | #endif | 
|---|
| 511 |  | 
|---|
| 512 | //target rectangle is inclusive-inclusive | 
|---|
| 513 | point[2].x = nXsrc; | 
|---|
| 514 | point[3].x = nXsrc + nSrcWidth; | 
|---|
| 515 | #ifdef INVERT | 
|---|
| 516 | point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight; | 
|---|
| 517 | point[3].y = pOS2bmp->cy - nYsrc; | 
|---|
| 518 | #else | 
|---|
| 519 | point[2].y = nYsrc; | 
|---|
| 520 | point[3].y = nYsrc + nSrcHeight; | 
|---|
| 521 | #endif | 
|---|
| 522 |  | 
|---|
| 523 | #ifdef INVERT | 
|---|
| 524 | oldyinversion = GpiQueryYInversion(hps); | 
|---|
| 525 | if(oldyinversion != 0) { | 
|---|
| 526 | POINT viewpt, winpt; | 
|---|
| 527 |  | 
|---|
| 528 | GetViewportOrgEx(hps, &viewpt); | 
|---|
| 529 | GetWindowOrgEx(hps, &winpt); | 
|---|
| 530 |  | 
|---|
| 531 | dprintf(("Viewport origin (%d,%d)", viewpt.x, viewpt.y)); | 
|---|
| 532 | dprintf(("Windows  origin (%d,%d)", winpt.x, winpt.y)); | 
|---|
| 533 |  | 
|---|
| 534 | /* By resetting y inversion to 0, we must take the new windows | 
|---|
| 535 | * origin into account. The default matrix set up for the origin | 
|---|
| 536 | * depends on y inversion. Therefor we must add the y origin value, | 
|---|
| 537 | * multiplied by two, to the top & bottom coordinates | 
|---|
| 538 | */ | 
|---|
| 539 | point[0].y -= winpt.y*2; | 
|---|
| 540 | point[1].y -= winpt.y*2; | 
|---|
| 541 |  | 
|---|
| 542 | /* By resetting y inversion to 0, we must take the new viewport | 
|---|
| 543 | * origin into account. The default matrix set up for the origin | 
|---|
| 544 | * depends on y inversion. Therefor we must subtract the y origin value, | 
|---|
| 545 | * multiplied by two, from the top & bottom coordinates | 
|---|
| 546 | */ | 
|---|
| 547 | point[0].y -= viewpt.y*2; | 
|---|
| 548 | point[1].y -= viewpt.y*2; | 
|---|
| 549 |  | 
|---|
| 550 | GpiEnableYInversion(hps, 0); | 
|---|
| 551 | fRestoryYInversion = TRUE; | 
|---|
| 552 | } | 
|---|
| 553 | #else | 
|---|
| 554 | dprintf(("Sync destination dibsection: hdc y inversion = %d", GpiQueryYInversion(hdc))); | 
|---|
| 555 | #endif | 
|---|
| 556 |  | 
|---|
| 557 | dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y, | 
|---|
| 558 | point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y, | 
|---|
| 559 | nDestWidth, nDestHeight, nSrcWidth, nSrcHeight)); | 
|---|
| 560 |  | 
|---|
| 561 | #ifdef DEBUG | 
|---|
| 562 | RECTL rcltemp; | 
|---|
| 563 | GreGetDCOrigin(hps, (PPOINTL)&rcltemp); | 
|---|
| 564 | dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion)); | 
|---|
| 565 | #endif | 
|---|
| 566 |  | 
|---|
| 567 | if(fFlip & FLIP_HOR) | 
|---|
| 568 | { | 
|---|
| 569 | ULONG x; | 
|---|
| 570 | x = point[0].x; | 
|---|
| 571 | point[0].x = point[1].x; | 
|---|
| 572 | point[1].x = x; | 
|---|
| 573 | } | 
|---|
| 574 |  | 
|---|
| 575 | ULONG os2mode, winmode; | 
|---|
| 576 |  | 
|---|
| 577 | os2mode = BBO_OR; | 
|---|
| 578 | winmode = GetStretchBltMode(hdcDest); | 
|---|
| 579 | switch(winmode) { | 
|---|
| 580 | case BLACKONWHITE_W: | 
|---|
| 581 | os2mode = BBO_AND; | 
|---|
| 582 | break; | 
|---|
| 583 | case WHITEONBLACK_W: | 
|---|
| 584 | case HALFTONE_W: //TODO: | 
|---|
| 585 | os2mode = BBO_OR; | 
|---|
| 586 | break; | 
|---|
| 587 | case COLORONCOLOR_W: | 
|---|
| 588 | os2mode = BBO_IGNORE; | 
|---|
| 589 | break; | 
|---|
| 590 | } | 
|---|
| 591 | #ifndef INVERT | 
|---|
| 592 | if(!(fFlip & FLIP_VERT)) { | 
|---|
| 593 | #else | 
|---|
| 594 | if(fFlip & FLIP_VERT) { | 
|---|
| 595 | #endif | 
|---|
| 596 | //manually reverse bitmap data | 
|---|
| 597 | char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 598 | char *dst = bmpBitsDblBuffer; | 
|---|
| 599 | for(int i=0;i<pOS2bmp->cy;i++) { | 
|---|
| 600 | memcpy(dst, src, dibinfo.dsBm.bmWidthBytes); | 
|---|
| 601 | dst += dibinfo.dsBm.bmWidthBytes; | 
|---|
| 602 | src -= dibinfo.dsBm.bmWidthBytes; | 
|---|
| 603 | } | 
|---|
| 604 | bitmapBits = bmpBitsDblBuffer; | 
|---|
| 605 | } | 
|---|
| 606 | else  bitmapBits = bmpBits; | 
|---|
| 607 |  | 
|---|
| 608 | //Translate ROP | 
|---|
| 609 | Rop = Rop >> 16; | 
|---|
| 610 |  | 
|---|
| 611 | //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc) | 
|---|
| 612 | if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555? | 
|---|
| 613 | dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion)); | 
|---|
| 614 |  | 
|---|
| 615 | if(bmpBitsDblBuffer == NULL) | 
|---|
| 616 | DebugInt3(); | 
|---|
| 617 |  | 
|---|
| 618 | // PH 2000/10/01 - Fix for Beyond Compare 1.9d | 
|---|
| 619 | // Note: according to documentation, cmImage can be zero for | 
|---|
| 620 | // RGB- / non-compressed bitmaps. | 
|---|
| 621 | int iLength = pOS2bmp->cbImage; | 
|---|
| 622 | if (iLength == 0) | 
|---|
| 623 | iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3); | 
|---|
| 624 |  | 
|---|
| 625 | if (iLength > 0) | 
|---|
| 626 | { | 
|---|
| 627 | pRGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD)); | 
|---|
| 628 | } | 
|---|
| 629 | else | 
|---|
| 630 | { | 
|---|
| 631 | dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp)); | 
|---|
| 632 | } | 
|---|
| 633 | rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], Rop, os2mode); | 
|---|
| 634 | } | 
|---|
| 635 | else { | 
|---|
| 636 | rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], Rop, os2mode); | 
|---|
| 637 | } | 
|---|
| 638 | if(rc == GPI_OK) { | 
|---|
| 639 | DIBSection *destdib = DIBSection::findHDC(hdcDest); | 
|---|
| 640 | if(destdib) { | 
|---|
| 641 | destdib->sync(hps, nYdest, nDestHeight, FALSE); | 
|---|
| 642 | } | 
|---|
| 643 | #ifdef INVERT | 
|---|
| 644 | //restore old y inversion height | 
|---|
| 645 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion); | 
|---|
| 646 | #endif | 
|---|
| 647 | SetLastError(ERROR_SUCCESS_W); | 
|---|
| 648 |  | 
|---|
| 649 | return(TRUE); | 
|---|
| 650 | } | 
|---|
| 651 | #ifdef INVERT | 
|---|
| 652 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion); | 
|---|
| 653 | #endif | 
|---|
| 654 |  | 
|---|
| 655 | dprintf(("DIBSection::BitBlt %X (%d,%d) (%d,%d) to (%d,%d) (%d,%d) returned %d\n", hps, point[0].x, point[0].y, point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y, rc)); | 
|---|
| 656 | dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF)); | 
|---|
| 657 | return(FALSE); | 
|---|
| 658 | } | 
|---|
| 659 | //****************************************************************************** | 
|---|
| 660 | //****************************************************************************** | 
|---|
| 661 | void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion) | 
|---|
| 662 | { | 
|---|
| 663 | APIRET rc; | 
|---|
| 664 | char  *destBuf; | 
|---|
| 665 |  | 
|---|
| 666 | dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip)); | 
|---|
| 667 |  | 
|---|
| 668 | BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize); | 
|---|
| 669 | memcpy(tmphdr, pOS2bmp, os2bmphdrsize); | 
|---|
| 670 |  | 
|---|
| 671 | #ifdef INVERT | 
|---|
| 672 | int oldyinversion = 0; | 
|---|
| 673 | if(orgYInversion == TRUE) { | 
|---|
| 674 | oldyinversion = GpiQueryYInversion(hdc); | 
|---|
| 675 | dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion)); | 
|---|
| 676 | if(oldyinversion != 0) { | 
|---|
| 677 | #ifdef DEBUG | 
|---|
| 678 | POINT point; | 
|---|
| 679 | GetViewportOrgEx(hdc, &point); | 
|---|
| 680 | dprintf(("Viewport origin (%d,%d)", point.x, point.y)); | 
|---|
| 681 | #endif | 
|---|
| 682 | GpiEnableYInversion(hdc, 0); | 
|---|
| 683 | } | 
|---|
| 684 | } | 
|---|
| 685 | #else | 
|---|
| 686 | dprintf(("Sync destination dibsection: hdc y inversion = %d", GpiQueryYInversion(hdc))); | 
|---|
| 687 | #endif | 
|---|
| 688 |  | 
|---|
| 689 | #ifndef INVERT | 
|---|
| 690 | if(!(fFlip & FLIP_VERT)) { | 
|---|
| 691 | #else | 
|---|
| 692 | if(fFlip & FLIP_VERT) { | 
|---|
| 693 | #endif | 
|---|
| 694 | destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 695 |  | 
|---|
| 696 | //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits | 
|---|
| 697 | //     will fail in that case (CoolEdit 2000). Perhaps because the returned | 
|---|
| 698 | //     compressed image is larger than the original. | 
|---|
| 699 | //     Use uncompressed size instead | 
|---|
| 700 | //     NOTE: The correct size will be returned by GpiQueryBitmapBits | 
|---|
| 701 | tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 702 |  | 
|---|
| 703 | #ifdef INVERT | 
|---|
| 704 | int dest = dibinfo.dsBm.bmHeight - nYdest - nDestHeight; | 
|---|
| 705 | #else | 
|---|
| 706 | int dest = nYdest; | 
|---|
| 707 | #endif | 
|---|
| 708 | rc = GpiQueryBitmapBits(hdc, dest, nDestHeight, destBuf, | 
|---|
| 709 | tmphdr); | 
|---|
| 710 | if(rc == GPI_ALTERROR) { | 
|---|
| 711 | dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0))); | 
|---|
| 712 | } | 
|---|
| 713 |  | 
|---|
| 714 | //manually reverse bitmap data | 
|---|
| 715 | char *src = destBuf; | 
|---|
| 716 | char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 717 | for(int i=0;i<nDestHeight;i++) { | 
|---|
| 718 | memcpy(dst, src, dibinfo.dsBm.bmWidthBytes); | 
|---|
| 719 | dst -= dibinfo.dsBm.bmWidthBytes; | 
|---|
| 720 | src += dibinfo.dsBm.bmWidthBytes; | 
|---|
| 721 | } | 
|---|
| 722 | } | 
|---|
| 723 | else { | 
|---|
| 724 | //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits | 
|---|
| 725 | //     will fail in that case (CoolEdit 2000). Perhaps because the returned | 
|---|
| 726 | //     compressed image is larger than the original. | 
|---|
| 727 | //     Use uncompressed size instead | 
|---|
| 728 | //     NOTE: The correct size will be returned by GpiQueryBitmapBits | 
|---|
| 729 | tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 730 |  | 
|---|
| 731 | destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 732 |  | 
|---|
| 733 | #ifdef INVERT | 
|---|
| 734 | int dest = dibinfo.dsBm.bmHeight - nYdest - nDestHeight; | 
|---|
| 735 | #else | 
|---|
| 736 | int dest = nYdest; | 
|---|
| 737 | #endif | 
|---|
| 738 | rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf, | 
|---|
| 739 | tmphdr); | 
|---|
| 740 | if(rc == GPI_ALTERROR) { | 
|---|
| 741 | dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0))); | 
|---|
| 742 | } | 
|---|
| 743 | #ifdef DEBUG_PALETTE | 
|---|
| 744 | if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) { | 
|---|
| 745 | for(int i=0;i<(1<<tmphdr->cBitCount);i++) | 
|---|
| 746 | { | 
|---|
| 747 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) )); | 
|---|
| 748 | } | 
|---|
| 749 | } | 
|---|
| 750 | #endif | 
|---|
| 751 | } | 
|---|
| 752 | memcpy(pOS2bmp, tmphdr, os2bmphdrsize); | 
|---|
| 753 |  | 
|---|
| 754 | if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555? | 
|---|
| 755 | dprintf(("DIBSection::sync: convert RGB 565 to RGB 555")); | 
|---|
| 756 |  | 
|---|
| 757 | destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes; | 
|---|
| 758 |  | 
|---|
| 759 | pRGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD)); | 
|---|
| 760 | } | 
|---|
| 761 |  | 
|---|
| 762 | free(tmphdr); | 
|---|
| 763 | if(rc != nDestHeight) { | 
|---|
| 764 | dprintf(("!WARNING!: GpiQueryBitmapBits returned %d instead of %d scanlines", rc, nDestHeight)); | 
|---|
| 765 | } | 
|---|
| 766 |  | 
|---|
| 767 | #ifdef INVERT | 
|---|
| 768 | if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion); | 
|---|
| 769 | #endif | 
|---|
| 770 |  | 
|---|
| 771 | } | 
|---|
| 772 | //****************************************************************************** | 
|---|
| 773 | //manual sync if no stretching and bpp is the same | 
|---|
| 774 | //WARNING: this also assumes the colortables are the same | 
|---|
| 775 | //****************************************************************************** | 
|---|
| 776 | void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits) | 
|---|
| 777 | { | 
|---|
| 778 | char *srcbuf, *destbuf; | 
|---|
| 779 | int  linesize; | 
|---|
| 780 |  | 
|---|
| 781 | srcbuf  = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst + | 
|---|
| 782 | (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx; | 
|---|
| 783 | destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst + | 
|---|
| 784 | (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx; | 
|---|
| 785 | linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx; | 
|---|
| 786 | for(int i=0;i<heightDst;i++) { | 
|---|
| 787 | memcpy(destbuf, srcbuf, linesize); | 
|---|
| 788 | destbuf += dibinfo.dsBm.bmWidthBytes; | 
|---|
| 789 | srcbuf  += linesize; | 
|---|
| 790 | } | 
|---|
| 791 | } | 
|---|
| 792 | //****************************************************************************** | 
|---|
| 793 | //****************************************************************************** | 
|---|
| 794 | void DIBSection::SelectDIBObject(HDC hdc) | 
|---|
| 795 | { | 
|---|
| 796 | this->hdc  = hdc; | 
|---|
| 797 | hwndParent = WindowFromDC(hdc); | 
|---|
| 798 | dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent)); | 
|---|
| 799 | } | 
|---|
| 800 | //****************************************************************************** | 
|---|
| 801 | //****************************************************************************** | 
|---|
| 802 | DIBSection *DIBSection::findObj(HANDLE handle) | 
|---|
| 803 | { | 
|---|
| 804 | // PH 2001-08-18 shortcut for performance optimization | 
|---|
| 805 | if (!section) | 
|---|
| 806 | return NULL; | 
|---|
| 807 |  | 
|---|
| 808 | DIBSection *dsect = section; | 
|---|
| 809 | lock(); | 
|---|
| 810 |  | 
|---|
| 811 | do | 
|---|
| 812 | { | 
|---|
| 813 | if(dsect->handle == handle) | 
|---|
| 814 | { | 
|---|
| 815 | unlock(); | 
|---|
| 816 | return(dsect); | 
|---|
| 817 | } | 
|---|
| 818 | dsect = dsect->next; | 
|---|
| 819 | } | 
|---|
| 820 | while(dsect); | 
|---|
| 821 |  | 
|---|
| 822 | unlock(); | 
|---|
| 823 | return(NULL); | 
|---|
| 824 | } | 
|---|
| 825 | //****************************************************************************** | 
|---|
| 826 | //A bitmap can only be selected into one DC, so this works. | 
|---|
| 827 | //****************************************************************************** | 
|---|
| 828 | DIBSection *DIBSection::findHDC(HDC hdc) | 
|---|
| 829 | { | 
|---|
| 830 | // PH 2001-08-18 shortcut for performance optimization | 
|---|
| 831 | if (!section) | 
|---|
| 832 | return NULL; | 
|---|
| 833 |  | 
|---|
| 834 | DIBSection *dsect = section; | 
|---|
| 835 |  | 
|---|
| 836 | lock(); | 
|---|
| 837 | do | 
|---|
| 838 | { | 
|---|
| 839 | if(dsect->hdc == hdc) | 
|---|
| 840 | { | 
|---|
| 841 | unlock(); | 
|---|
| 842 | return(dsect); | 
|---|
| 843 | } | 
|---|
| 844 | dsect = dsect->next; | 
|---|
| 845 | } | 
|---|
| 846 | while(dsect); | 
|---|
| 847 |  | 
|---|
| 848 | unlock(); | 
|---|
| 849 | return(NULL); | 
|---|
| 850 | } | 
|---|
| 851 | //****************************************************************************** | 
|---|
| 852 | //****************************************************************************** | 
|---|
| 853 | void DIBSection::deleteSection(HANDLE handle) | 
|---|
| 854 | { | 
|---|
| 855 | DIBSection *dsect = findObj(handle); | 
|---|
| 856 |  | 
|---|
| 857 | if(dsect) | 
|---|
| 858 | delete dsect; | 
|---|
| 859 | } | 
|---|
| 860 | //****************************************************************************** | 
|---|
| 861 | //****************************************************************************** | 
|---|
| 862 | int DIBSection::GetDIBSection(int iSize, void *lpBuffer) | 
|---|
| 863 | { | 
|---|
| 864 | DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer; | 
|---|
| 865 | LPBITMAP_W  dsBm        = (LPBITMAP_W)lpBuffer; | 
|---|
| 866 |  | 
|---|
| 867 | dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer)); | 
|---|
| 868 | if(iSize == sizeof(DIBSECTION)) | 
|---|
| 869 | { | 
|---|
| 870 | memcpy(pDIBSection, &dibinfo, sizeof(dibinfo)); | 
|---|
| 871 | return sizeof(DIBSECTION); | 
|---|
| 872 | } | 
|---|
| 873 | else | 
|---|
| 874 | if(iSize == sizeof(BITMAP_W)) | 
|---|
| 875 | { | 
|---|
| 876 | memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm)); | 
|---|
| 877 | return sizeof(BITMAP_W); | 
|---|
| 878 | } | 
|---|
| 879 | return 0; | 
|---|
| 880 |  | 
|---|
| 881 | } | 
|---|
| 882 | //****************************************************************************** | 
|---|
| 883 | //****************************************************************************** | 
|---|
| 884 | int  DIBSection::GetBitCount() | 
|---|
| 885 | { | 
|---|
| 886 | if(pOS2bmp == NULL) | 
|---|
| 887 | return 0; | 
|---|
| 888 | else | 
|---|
| 889 | return pOS2bmp->cBitCount; | 
|---|
| 890 | } | 
|---|
| 891 | //****************************************************************************** | 
|---|
| 892 | //****************************************************************************** | 
|---|
| 893 | int DIBSection::GetHeight() | 
|---|
| 894 | { | 
|---|
| 895 | if(pOS2bmp == NULL) | 
|---|
| 896 | return 0; | 
|---|
| 897 | else | 
|---|
| 898 | return pOS2bmp->cy; | 
|---|
| 899 | } | 
|---|
| 900 | //****************************************************************************** | 
|---|
| 901 | //****************************************************************************** | 
|---|
| 902 | int DIBSection::GetWidth() | 
|---|
| 903 | { | 
|---|
| 904 | if(pOS2bmp == NULL) | 
|---|
| 905 | return 0; | 
|---|
| 906 | else | 
|---|
| 907 | return pOS2bmp->cx; | 
|---|
| 908 | } | 
|---|
| 909 | //****************************************************************************** | 
|---|
| 910 | //****************************************************************************** | 
|---|
| 911 | void DIBSection::initDIBSection() | 
|---|
| 912 | { | 
|---|
| 913 | InitializeCriticalSection(&dibcritsect); | 
|---|
| 914 | } | 
|---|
| 915 | //****************************************************************************** | 
|---|
| 916 | //****************************************************************************** | 
|---|
| 917 | DIBSection      *DIBSection::section = NULL; | 
|---|
| 918 | CRITICAL_SECTION DIBSection::dibcritsect; | 
|---|