| 1 | /* $Id: dibsect.cpp,v 1.4 1999-06-19 10:54:39 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * GDI32 DIB sections | 
|---|
| 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 | #define  INCL_GPI | 
|---|
| 13 | #define  INCL_WIN | 
|---|
| 14 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 15 | #include <stdlib.h> | 
|---|
| 16 | #include <string.h> | 
|---|
| 17 | #include "win32type.h" | 
|---|
| 18 | #include "misc.h" | 
|---|
| 19 | #define  OS2_ONLY | 
|---|
| 20 | #include "dibsect.h" | 
|---|
| 21 |  | 
|---|
| 22 | //NOTE: | 
|---|
| 23 | //This is not a complete solution for CreateDIBSection, but enough for Quake 2! | 
|---|
| 24 | //****************************************************************************** | 
|---|
| 25 | //****************************************************************************** | 
|---|
| 26 | DIBSection::DIBSection(WINBITMAPINFOHEADER *pbmi, DWORD handle, int fFlip) | 
|---|
| 27 | : bmpBits(NULL), pOS2bmp(NULL) | 
|---|
| 28 | { | 
|---|
| 29 | int bmpsize = pbmi->biWidth, os2bmpsize; | 
|---|
| 30 |  | 
|---|
| 31 | /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */ | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | this->fFlip = fFlip; | 
|---|
| 35 | os2bmpsize = sizeof(BITMAPINFO2); | 
|---|
| 36 |  | 
|---|
| 37 | switch(pbmi->biBitCount) | 
|---|
| 38 | { | 
|---|
| 39 | case 1: | 
|---|
| 40 | bmpsize /= 8; | 
|---|
| 41 | os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2); | 
|---|
| 42 | break; | 
|---|
| 43 | case 4: | 
|---|
| 44 | bmpsize /= 2; | 
|---|
| 45 | os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2); | 
|---|
| 46 | break; | 
|---|
| 47 | case 8: | 
|---|
| 48 | os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2); | 
|---|
| 49 | break; | 
|---|
| 50 | case 16: | 
|---|
| 51 | bmpsize *= 2; | 
|---|
| 52 | break; | 
|---|
| 53 | case 24: | 
|---|
| 54 | bmpsize *= 3; | 
|---|
| 55 | break; | 
|---|
| 56 | case 32: | 
|---|
| 57 | bmpsize *= 4; | 
|---|
| 58 | break; | 
|---|
| 59 | } | 
|---|
| 60 | if(bmpsize & 3) { | 
|---|
| 61 | bmpsize = (bmpsize + 3) & ~3; | 
|---|
| 62 | } | 
|---|
| 63 | bmpBits    = (char *)malloc(bmpsize*pbmi->biHeight); | 
|---|
| 64 |  | 
|---|
| 65 | pOS2bmp    = (BITMAPINFO2 *)malloc(os2bmpsize); | 
|---|
| 66 |  | 
|---|
| 67 | memset(pOS2bmp, /* set header + palette entries to zero */ | 
|---|
| 68 | 0, | 
|---|
| 69 | os2bmpsize); | 
|---|
| 70 |  | 
|---|
| 71 | pOS2bmp->cbFix         = sizeof(BITMAPINFO2) - sizeof(RGB2); | 
|---|
| 72 | pOS2bmp->cx            = pbmi->biWidth; | 
|---|
| 73 | pOS2bmp->cy            = pbmi->biHeight; | 
|---|
| 74 | pOS2bmp->cPlanes       = pbmi->biPlanes; | 
|---|
| 75 | pOS2bmp->cBitCount     = pbmi->biBitCount; | 
|---|
| 76 | pOS2bmp->ulCompression = pbmi->biCompression; | 
|---|
| 77 | pOS2bmp->cbImage       = pbmi->biSizeImage; | 
|---|
| 78 | dprintf(("pOS2bmp->cx            %d\n", pOS2bmp->cx)); | 
|---|
| 79 | dprintf(("pOS2bmp->cy            %d\n", pOS2bmp->cy)); | 
|---|
| 80 | dprintf(("pOS2bmp->cPlanes       %d\n", pOS2bmp->cPlanes)); | 
|---|
| 81 | dprintf(("pOS2bmp->cBitCount     %d\n", pOS2bmp->cBitCount)); | 
|---|
| 82 | dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression)); | 
|---|
| 83 | dprintf(("pOS2bmp->cbImage       %d\n", pOS2bmp->cbImage)); | 
|---|
| 84 |  | 
|---|
| 85 | this->handle = handle; | 
|---|
| 86 |  | 
|---|
| 87 | if(section == NULL) { | 
|---|
| 88 | section = this; | 
|---|
| 89 | } | 
|---|
| 90 | else | 
|---|
| 91 | { | 
|---|
| 92 | DIBSection *dsect = section; | 
|---|
| 93 |  | 
|---|
| 94 | /* @@@PH 98/07/11 fix for dsect->next == NULL */ | 
|---|
| 95 | while ( (dsect->next != this) && | 
|---|
| 96 | (dsect->next != NULL) ) | 
|---|
| 97 | { | 
|---|
| 98 | dsect = dsect->next; | 
|---|
| 99 | } | 
|---|
| 100 | dsect->next = this; | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 | //****************************************************************************** | 
|---|
| 104 | //****************************************************************************** | 
|---|
| 105 | DIBSection::~DIBSection() | 
|---|
| 106 | { | 
|---|
| 107 | if(bmpBits) | 
|---|
| 108 | free(bmpBits); | 
|---|
| 109 | if(pOS2bmp) | 
|---|
| 110 | free(pOS2bmp); | 
|---|
| 111 |  | 
|---|
| 112 | if(section == this) { | 
|---|
| 113 | section = this->next; | 
|---|
| 114 | } | 
|---|
| 115 | else { | 
|---|
| 116 | DIBSection *dsect = section; | 
|---|
| 117 |  | 
|---|
| 118 | while(dsect->next != this) { | 
|---|
| 119 | dsect = dsect->next; | 
|---|
| 120 | } | 
|---|
| 121 | dsect->next = this->next; | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 | //****************************************************************************** | 
|---|
| 125 | //****************************************************************************** | 
|---|
| 126 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb) | 
|---|
| 127 | { | 
|---|
| 128 | int i; | 
|---|
| 129 |  | 
|---|
| 130 | if(startIdx + cEntries > (1 << pOS2bmp->cBitCount)) { | 
|---|
| 131 | dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries)); | 
|---|
| 132 | return(0); | 
|---|
| 133 | } | 
|---|
| 134 | memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2)); | 
|---|
| 135 | for(i=startIdx;i<cEntries;i++) { | 
|---|
| 136 | pOS2bmp->argbColor[i].fcOptions = 0; | 
|---|
| 137 | } | 
|---|
| 138 | return(cEntries); | 
|---|
| 139 | } | 
|---|
| 140 | //****************************************************************************** | 
|---|
| 141 | //****************************************************************************** | 
|---|
| 142 | #if 1 | 
|---|
| 143 | BOOL DIBSection::BitBlt(HDC hdcDest, HWND hwndDest, int nXdest, int nYdest, int nWidth, | 
|---|
| 144 | int nHeight, int nXsrc, int nYsrc, DWORD Rop) | 
|---|
| 145 | { | 
|---|
| 146 | HPS    hps = (HPS)hdcDest; | 
|---|
| 147 | POINTL point[4]; | 
|---|
| 148 | LONG   rc; | 
|---|
| 149 |  | 
|---|
| 150 | if(hwndDest != 0) { | 
|---|
| 151 | hps = WinGetPS(hwndDest); | 
|---|
| 152 | } | 
|---|
| 153 | if(hps == 0) { | 
|---|
| 154 | eprintf(("DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest)); | 
|---|
| 155 | return(FALSE); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | point[0].x = nXdest; | 
|---|
| 159 | point[0].y = nYdest; | 
|---|
| 160 | point[1].x = nXdest + nWidth - 1; | 
|---|
| 161 | point[1].y = nYdest + nHeight - 1; | 
|---|
| 162 | point[2].x = nXsrc; | 
|---|
| 163 | point[2].y = nYsrc; | 
|---|
| 164 | if(nXsrc + nWidth > pOS2bmp->cx) { | 
|---|
| 165 | point[3].x = pOS2bmp->cx; | 
|---|
| 166 | } | 
|---|
| 167 | else  point[3].x = nXsrc + nWidth; | 
|---|
| 168 |  | 
|---|
| 169 | if(nYsrc + nHeight > pOS2bmp->cy) { | 
|---|
| 170 | point[3].y = pOS2bmp->cy; | 
|---|
| 171 | } | 
|---|
| 172 | else  point[3].y = nYsrc + nHeight; | 
|---|
| 173 |  | 
|---|
| 174 | if(fFlip & FLIP_VERT) { | 
|---|
| 175 | ULONG y; | 
|---|
| 176 | y = point[0].y; | 
|---|
| 177 | point[0].y = point[1].y; | 
|---|
| 178 | point[1].y = y; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | if(fFlip & FLIP_HOR) { | 
|---|
| 182 | ULONG x; | 
|---|
| 183 | x = point[0].x; | 
|---|
| 184 | point[0].x = point[1].x; | 
|---|
| 185 | point[1].x = x; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR); | 
|---|
| 189 |  | 
|---|
| 190 | if(hwndDest != 0) { | 
|---|
| 191 | WinReleasePS(hps); | 
|---|
| 192 | } | 
|---|
| 193 | if(rc == GPI_OK) | 
|---|
| 194 | return(TRUE); | 
|---|
| 195 | 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)); | 
|---|
| 196 | dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF)); | 
|---|
| 197 | return(FALSE); | 
|---|
| 198 | } | 
|---|
| 199 | #else | 
|---|
| 200 | BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nWidth, | 
|---|
| 201 | int nHeight, int nXsrc, int nYsrc, DWORD Rop) | 
|---|
| 202 | { | 
|---|
| 203 | HPS    hps = (HPS)hdcDest; | 
|---|
| 204 | POINTL point[4]; | 
|---|
| 205 | LONG   rc; | 
|---|
| 206 |  | 
|---|
| 207 | if(hps == 0) { | 
|---|
| 208 | eprintf(("DIBSection::BitBlt, hps == 0")); | 
|---|
| 209 | return(FALSE); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | //  dprintf(("DIBSection::BitBlt (%d,%d) to (%d,%d) (%d,%d)\n", nXsrc, nYsrc, nXdest, nYdest, nXdest+ nWidth, nYdest + nHeight)); | 
|---|
| 213 | point[0].x = nXdest; | 
|---|
| 214 | point[0].y = nYdest; | 
|---|
| 215 | point[1].x = nXdest + nWidth - 1; | 
|---|
| 216 | point[1].y = nYdest + nHeight - 1; | 
|---|
| 217 | point[2].x = nXsrc; | 
|---|
| 218 | point[2].y = nYsrc; | 
|---|
| 219 | if(nXsrc + nWidth > pOS2bmp->cx) { | 
|---|
| 220 | point[3].x = pOS2bmp->cx; | 
|---|
| 221 | } | 
|---|
| 222 | else  point[3].x = nXsrc + nWidth; | 
|---|
| 223 |  | 
|---|
| 224 | if(nYsrc + nHeight > pOS2bmp->cy) { | 
|---|
| 225 | point[3].y = pOS2bmp->cy; | 
|---|
| 226 | } | 
|---|
| 227 | else  point[3].y = nYsrc + nHeight; | 
|---|
| 228 |  | 
|---|
| 229 | rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR); | 
|---|
| 230 | //  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)); | 
|---|
| 231 |  | 
|---|
| 232 | if(rc == GPI_OK) | 
|---|
| 233 | return(TRUE); | 
|---|
| 234 | 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)); | 
|---|
| 235 | dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndParent)) & 0xFFFF)); | 
|---|
| 236 | return(FALSE); | 
|---|
| 237 | } | 
|---|
| 238 | #endif | 
|---|
| 239 | //****************************************************************************** | 
|---|
| 240 | //****************************************************************************** | 
|---|
| 241 | void DIBSection::SelectDIBObject(HDC hdc) | 
|---|
| 242 | { | 
|---|
| 243 | this->hdc  = hdc; | 
|---|
| 244 | hwndParent = WinWindowFromDC(hdc); | 
|---|
| 245 | } | 
|---|
| 246 | //****************************************************************************** | 
|---|
| 247 | //****************************************************************************** | 
|---|
| 248 | DIBSection *DIBSection::find(DWORD handle) | 
|---|
| 249 | { | 
|---|
| 250 | DIBSection *dsect = section; | 
|---|
| 251 |  | 
|---|
| 252 | while(dsect) { | 
|---|
| 253 | if(dsect->handle == handle) { | 
|---|
| 254 | return(dsect); | 
|---|
| 255 | } | 
|---|
| 256 | dsect = dsect->next; | 
|---|
| 257 | } | 
|---|
| 258 | return(NULL); | 
|---|
| 259 | } | 
|---|
| 260 | //****************************************************************************** | 
|---|
| 261 | //A bitmap can only be selected into one DC, so this works. | 
|---|
| 262 | //****************************************************************************** | 
|---|
| 263 | DIBSection *DIBSection::findHDC(HDC hdc) | 
|---|
| 264 | { | 
|---|
| 265 | DIBSection *dsect = section; | 
|---|
| 266 |  | 
|---|
| 267 | while(dsect) { | 
|---|
| 268 | if(dsect->hdc == hdc) { | 
|---|
| 269 | return(dsect); | 
|---|
| 270 | } | 
|---|
| 271 | dsect = dsect->next; | 
|---|
| 272 | } | 
|---|
| 273 | return(NULL); | 
|---|
| 274 | } | 
|---|
| 275 | //****************************************************************************** | 
|---|
| 276 | //****************************************************************************** | 
|---|
| 277 | void DIBSection::deleteSection(DWORD handle) | 
|---|
| 278 | { | 
|---|
| 279 | DIBSection *dsect = find(handle); | 
|---|
| 280 |  | 
|---|
| 281 | if(dsect) | 
|---|
| 282 | delete dsect; | 
|---|
| 283 |  | 
|---|
| 284 | } | 
|---|
| 285 | //****************************************************************************** | 
|---|
| 286 | //****************************************************************************** | 
|---|
| 287 | DIBSection *DIBSection::section = NULL; | 
|---|