source: trunk/src/gdi32/dibsect.cpp@ 4127

Last change on this file since 4127 was 4127, checked in by sandervl, 25 years ago

workaround for palette problems in PowerDVD

File size: 23.8 KB
Line 
1/* $Id: dibsect.cpp,v 1.38 2000-08-30 18:05:24 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#define OS2_ONLY
23#include "dibsect.h"
24#include <vmutex.h>
25#include <win32api.h>
26#include <winconst.h>
27#include <win32wnd.h>
28#include <cpuhlp.h>
29#include <dcdata.h>
30#include "oslibgpi.h"
31#include "rgbcvt.h"
32
33#define DBG_LOCALLOG DBG_dibsect
34#include "dbglocal.h"
35
36static VMutex dibMutex;
37
38//******************************************************************************
39//******************************************************************************
40DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
41 : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsDblBuffer(NULL)
42{
43 int palsize=0;
44
45 bmpsize = pbmi->biWidth;
46 /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
47
48 this->fFlip = fFlip;
49 os2bmphdrsize = sizeof(BITMAPINFO2);
50
51 switch(pbmi->biBitCount)
52 {
53 case 1:
54 bmpsize = ((bmpsize + 31) & ~31) / 8;
55 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
56 os2bmphdrsize += palsize;
57 break;
58 case 4:
59 bmpsize = ((bmpsize + 7) & ~7) / 2;
60 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
61 os2bmphdrsize += palsize;
62 break;
63 case 8:
64 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
65 os2bmphdrsize += palsize;
66 bmpsize = (bmpsize + 3) & ~3;
67 break;
68 case 16:
69 bmpsize *= 2;
70 bmpsize = (bmpsize + 3) & ~3;
71 break;
72 case 24:
73 bmpsize *= 3;
74 bmpsize = (bmpsize + 3) & ~3;
75 break;
76 case 32:
77 bmpsize *= 4;
78 break;
79 default:
80 dprintf(("Unsupported nr of bits %d", pbmi->biBitCount));
81 DebugInt3();
82 break;
83 }
84
85 this->hSection = hSection;
86 this->dwOffset = dwOffset;
87 if(hSection) {
88 bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight);
89 if(!bmpBits) {
90 dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
91 DebugInt3();
92 }
93 }
94 if(!bmpBits) {
95 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
96 }
97 memset(bmpBits, 0, bmpsize*pbmi->biHeight);
98
99 pOS2bmp = (BITMAPINFO2 *)malloc(os2bmphdrsize);
100
101 memset(pOS2bmp, /* set header + palette entries to zero */
102 0,
103 os2bmphdrsize);
104
105 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
106 pOS2bmp->cx = pbmi->biWidth;
107 pOS2bmp->cy = pbmi->biHeight;
108 pOS2bmp->cPlanes = pbmi->biPlanes;
109 pOS2bmp->cBitCount = pbmi->biBitCount;
110 pOS2bmp->ulCompression = pbmi->biCompression;
111 //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
112 if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
113 pOS2bmp->ulCompression = 0;
114 }
115 pOS2bmp->cbImage = pbmi->biSizeImage;
116 dprintf(("handle %x", handle));
117 dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
118 dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
119 dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
120 dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
121 dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
122 dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
123 dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
124
125 // clear DIBSECTION structure
126 memset(&dibinfo, 0, sizeof(dibinfo));
127
128 // copy BITMAPINFOHEADER data into DIBSECTION structure
129 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
130 dibinfo.dsBm.bmType = 0;
131 dibinfo.dsBm.bmWidth = pbmi->biWidth;
132 dibinfo.dsBm.bmHeight = pbmi->biHeight;
133 dibinfo.dsBm.bmWidthBytes= bmpsize;
134 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
135 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
136 dibinfo.dsBm.bmBits = bmpBits;
137
138 dibinfo.dshSection = hSection;
139 dibinfo.dsOffset = dwOffset;
140
141 if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
142 {
143 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
144 if(palsize) {
145 SetDIBColorTable(0, (1 << pbmi->biBitCount), (RGBQUAD *)(pbmi+1));
146 }
147 }
148 else {
149 switch(pbmi->biBitCount)
150 {
151 case 16:
152 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
153 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
154 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
155 break;
156
157 case 24:
158 dibinfo.dsBitfields[0] = 0xff;
159 dibinfo.dsBitfields[1] = 0xff00;
160 dibinfo.dsBitfields[2] = 0xff0000;
161 break;
162
163 case 32:
164 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
165 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
166 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
167 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
168 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
169 }
170 break;
171 }
172 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
173 }
174 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
175 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
176 DosAllocMem((PPVOID)&bmpBitsDblBuffer, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
177 }
178
179 this->handle = handle;
180 this->iUsage = iUsage;
181
182 dibMutex.enter();
183 if(section == NULL)
184 {
185 dprintf(("section was NULL\n"));
186 section = this;
187 }
188 else
189 {
190 DIBSection *dsect = section;
191 dprintf2(("Increment section starting at %08X\n",dsect));
192
193 /* @@@PH 98/07/11 fix for dsect->next == NULL */
194 while ( (dsect->next != this) &&
195 (dsect->next != NULL) )
196 {
197 dprintf2(("Increment section to %08X\n",dsect->next));
198 dsect = dsect->next;
199 }
200 dsect->next = this;
201 }
202 dibMutex.leave();
203}
204//******************************************************************************
205//******************************************************************************
206DIBSection::~DIBSection()
207{
208 dprintf(("Delete DIBSection %x", handle));
209
210 if(hSection) {
211 UnmapViewOfFile(bmpBits);
212 }
213 else
214 if(bmpBits)
215 DosFreeMem(bmpBits);
216
217 if(bmpBitsDblBuffer)
218 DosFreeMem(bmpBitsDblBuffer);
219
220 if(pOS2bmp)
221 free(pOS2bmp);
222
223 dibMutex.enter();
224 if(section == this)
225 {
226 section = this->next;
227 }
228 else
229 {
230 DIBSection *dsect = section;
231
232 while(dsect->next != this)
233 {
234 dsect = dsect->next;
235 }
236 dsect->next = this->next;
237 }
238 dibMutex.leave();
239}
240//******************************************************************************
241//******************************************************************************
242int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
243 lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
244 UINT coloruse)
245{
246 lines = (int)lines >= 0 ? (int)lines : (int)-lines;
247 int palsize=0;
248
249 bmpsize = pbmi->biWidth;
250 os2bmphdrsize = sizeof(BITMAPINFO2);
251
252 switch(pbmi->biBitCount)
253 {
254 case 1:
255 bmpsize = ((bmpsize + 31) & ~31) / 8;
256 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
257 os2bmphdrsize += palsize;
258 break;
259 case 4:
260 bmpsize = ((bmpsize + 7) & ~7) / 2;
261 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
262 os2bmphdrsize += palsize;
263 break;
264 case 8:
265 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
266 os2bmphdrsize += palsize;
267 bmpsize = (bmpsize + 3) & ~3;
268 break;
269 case 16:
270 bmpsize *= 2;
271 bmpsize = (bmpsize + 3) & ~3;
272 break;
273 case 24:
274 bmpsize *= 3;
275 bmpsize = (bmpsize + 3) & ~3;
276 break;
277 case 32:
278 bmpsize *= 4;
279 break;
280 }
281
282 //SvL: TODO: Correct??
283 if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
284 pOS2bmp->cBitCount != pbmi->biBitCount)
285 {
286 char *oldbits = bmpBits;
287 int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
288
289 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
290 memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
291 DosFreeMem(oldbits);
292 }
293 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmphdrsize);
294 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
295 pOS2bmp->cx = pbmi->biWidth;
296 pOS2bmp->cy = pbmi->biHeight;
297 pOS2bmp->cPlanes = pbmi->biPlanes;
298 pOS2bmp->cBitCount = pbmi->biBitCount;
299 pOS2bmp->ulCompression = pbmi->biCompression;
300 pOS2bmp->cbImage = pbmi->biSizeImage;
301
302 // clear DIBSECTION structure
303 memset(&dibinfo, 0, sizeof(dibinfo));
304
305 // copy BITMAPINFOHEADER data into DIBSECTION structure
306 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
307 dibinfo.dsBm.bmType = 0;
308 dibinfo.dsBm.bmWidth = pbmi->biWidth;
309 dibinfo.dsBm.bmHeight = pbmi->biHeight;
310 dibinfo.dsBm.bmWidthBytes= bmpsize;
311 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
312 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
313 dibinfo.dsBm.bmBits = bmpBits;
314
315 dibinfo.dshSection = hSection;
316 dibinfo.dsOffset = dwOffset;
317
318 if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
319 {
320 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
321 }
322 else {
323 char *pColors = (char *)pbmi + 1;
324
325 switch(pbmi->biBitCount)
326 {
327 case 16:
328 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
329 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
330 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
331 break;
332
333 case 24:
334 dibinfo.dsBitfields[0] = 0xff;
335 dibinfo.dsBitfields[1] = 0xff00;
336 dibinfo.dsBitfields[2] = 0xff0000;
337 break;
338
339 case 32:
340 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
341 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
342 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
343 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
344 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
345 }
346 break;
347 }
348 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
349 }
350
351 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
352 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
353 if(bmpBitsDblBuffer) {
354 DosFreeMem(bmpBitsDblBuffer);
355 }
356 DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
357 }
358
359 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
360 if(palsize) {
361 SetDIBColorTable(0, 1 << pbmi->biBitCount, (RGBQUAD *)(pbmi+1));
362 }
363
364 if(bits)
365 {
366 int size = bmpsize*lines;
367 memcpy(bmpBits+bmpsize*startscan, bits, size);
368 }
369 return(lines);
370}
371//******************************************************************************
372//******************************************************************************
373int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
374{
375 int i;
376
377 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
378 {
379 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
380 return(0);
381 }
382
383 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
384
385 for(i=startIdx;i<cEntries;i++)
386 {
387 pOS2bmp->argbColor[i].fcOptions = 0;
388#ifdef DEBUG_PALETTE
389 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
390#endif
391 }
392
393 return(cEntries);
394}
395//******************************************************************************
396//******************************************************************************
397BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
398 int nDestHeight, int nXsrc, int nYsrc,
399 int nSrcWidth, int nSrcHeight, DWORD Rop)
400{
401 HPS hps = (HPS)hdcDest;
402 POINTL point[4];
403 LONG rc, hdcHeight, hdcWidth;
404 PVOID bitmapBits = NULL;
405 int oldyinversion = 0;
406 BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
407 HWND hwndDest;
408 pDCData pHps;
409
410 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
411 if(!pHps)
412 {
413 SetLastError(ERROR_INVALID_HANDLE_W);
414 return FALSE;
415 }
416
417 hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
418 if(hwndDest && pHps->hwnd && !pHps->isClient) {
419 fFrameWindowDC = TRUE;
420 }
421
422 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
423 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
424 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
425
426 if(hwndDest) {
427 RECT rect;
428
429 if(fFrameWindowDC) {
430 GetWindowRect(hwndDest, &rect);
431 }
432 else GetClientRect(hwndDest, &rect);
433 hdcHeight = rect.bottom - rect.top;
434 hdcWidth = rect.right - rect.left;
435 }
436 else {
437 hdcHeight = pOS2bmp->cy;
438 hdcWidth = pOS2bmp->cx;
439 }
440
441 //win32 coordinates are relative to left top, OS/2 expects left bottom
442 //source rectangle is non-inclusive (top, right not included)
443 //destination rectangle is incl.-inclusive (everything included)
444 if(nXdest + nDestWidth > hdcWidth) {
445 nDestWidth = hdcWidth - nXdest;
446 }
447 if(nYdest + nDestHeight > hdcHeight) {
448 nDestHeight = hdcHeight - nYdest;
449 }
450 point[0].x = nXdest;
451 point[0].y = hdcHeight - nYdest - nDestHeight;
452 point[1].x = nXdest + nDestWidth - 1;
453 point[1].y = hdcHeight - nYdest - 1;
454
455 //target rectangle is inclusive-inclusive
456 if(nXsrc + nSrcWidth > pOS2bmp->cx) {
457 nSrcWidth = pOS2bmp->cx - nXsrc;
458 }
459 if(nYsrc + nSrcHeight > pOS2bmp->cy) {
460 nSrcHeight = pOS2bmp->cy - nYsrc;
461 }
462 point[2].x = nXsrc;
463 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
464 point[3].x = nXsrc + nSrcWidth;
465 point[3].y = pOS2bmp->cy - nYsrc;
466
467 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
468 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
469 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
470
471/*
472 hwndDest = Win32ToOS2Handle(hwndDest);
473 if(hwndDest != 0)
474 {
475 hps = WinGetPS(hwndDest);
476 }
477*/
478 oldyinversion = GpiQueryYInversion(hps);
479 if(oldyinversion != 0) {
480 GpiEnableYInversion(hps, 0);
481 fRestoryYInversion = TRUE;
482 }
483
484 if(fFlip & FLIP_HOR)
485 {
486 ULONG x;
487 x = point[0].x;
488 point[0].x = point[1].x;
489 point[1].x = x;
490 }
491
492 ULONG os2mode, winmode;
493
494 os2mode = BBO_OR;
495 winmode = GetStretchBltMode(hdcDest);
496 switch(winmode) {
497 case BLACKONWHITE_W:
498 os2mode = BBO_AND;
499 break;
500 case WHITEONBLACK_W:
501 case HALFTONE_W: //TODO:
502 os2mode = BBO_OR;
503 break;
504 case COLORONCOLOR_W:
505 os2mode = BBO_IGNORE;
506 break;
507 }
508 if(fFlip & FLIP_VERT) {
509 //manually reverse bitmap data
510 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
511 char *dst = bmpBitsDblBuffer;
512 for(int i=0;i<pOS2bmp->cy;i++) {
513 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
514 dst += dibinfo.dsBm.bmWidthBytes;
515 src -= dibinfo.dsBm.bmWidthBytes;
516 }
517 bitmapBits = bmpBitsDblBuffer;
518 }
519 else bitmapBits = bmpBits;
520
521 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
522 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
523 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
524
525 if(bmpBitsDblBuffer == NULL)
526 DebugInt3();
527
528 if(CPUFeatures & CPUID_MMX) {
529 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, pOS2bmp->cbImage/sizeof(WORD));
530 }
531 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, pOS2bmp->cbImage/sizeof(WORD));
532 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
533 }
534 else rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
535
536/*
537 if(hwndDest != 0)
538 {
539 WinReleasePS(hps);
540 }
541*/
542 if(rc == GPI_OK) {
543 DIBSection *destdib = DIBSection::findHDC(hdcDest);
544 if(destdib) {
545 destdib->sync(hps, nYdest, nDestHeight);
546 }
547 //restore old y inversion height
548 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
549 SetLastError(ERROR_SUCCESS_W);
550
551 return(TRUE);
552 }
553 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
554
555 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));
556 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
557 return(FALSE);
558}
559//******************************************************************************
560int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
561 void *lpvBits, BITMAPINFO_W * lpbi, UINT uUsage);
562
563//******************************************************************************
564void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight)
565{
566 APIRET rc;
567 char *destBuf;
568
569 dprintf(("Sync destination dibsection %x (%x)", handle, hdc));
570
571#if 0
572 BITMAPINFO_W *tmphdr = (BITMAPINFO_W *)malloc(os2bmphdrsize + sizeof(BITMAPINFO_W));
573 tmphdr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER_W);
574 GetDIBits(hdc, handle, nYdest, nDestHeight, NULL, tmphdr, 0);
575 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
576 rc = GetDIBits(hdc, handle, nYdest, nDestHeight, destBuf, tmphdr, 0);
577#ifdef DEBUG_PALETTE
578 if(rc && tmphdr->bmiHeader.biBitCount <= 8) {
579 for(int i=0;i<(1<<tmphdr->bmiHeader.biBitCount);i++)
580 {
581 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->bmiColors[i])) ));
582 }
583 }
584#endif
585
586#else
587 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
588 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
589
590 if(fFlip & FLIP_VERT) {
591 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
592
593 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
594 tmphdr);
595 //manually reverse bitmap data
596 char *src = destBuf;
597 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
598 for(int i=0;i<nDestHeight;i++) {
599 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
600 dst -= dibinfo.dsBm.bmWidthBytes;
601 src += dibinfo.dsBm.bmWidthBytes;
602 }
603 }
604 else {
605 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
606 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
607 tmphdr);
608#ifdef DEBUG_PALETTE
609 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
610 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
611 {
612 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
613 }
614 }
615#endif
616 }
617#endif
618#if 0
619 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
620 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
621
622 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
623
624 if(CPUFeatures & CPUID_MMX) {
625 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
626 }
627 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
628 }
629#endif
630 free(tmphdr);
631 if(rc != nDestHeight) {
632 DebugInt3();
633 }
634}
635//******************************************************************************
636//manual sync if no stretching and bpp is the same
637//WARNING: this also assumes the colortables are the same
638//******************************************************************************
639void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
640{
641 char *srcbuf, *destbuf;
642 int linesize;
643
644 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
645 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
646 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
647 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
648 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
649 for(int i=0;i<heightDst;i++) {
650 memcpy(destbuf, srcbuf, linesize);
651 destbuf += dibinfo.dsBm.bmWidthBytes;
652 srcbuf += linesize;
653 }
654}
655//******************************************************************************
656//******************************************************************************
657void DIBSection::SelectDIBObject(HDC hdc)
658{
659 this->hdc = hdc;
660 hwndParent = WindowFromDC(hdc);
661 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
662}
663//******************************************************************************
664//******************************************************************************
665DIBSection *DIBSection::find(DWORD handle)
666{
667 DIBSection *dsect = section;
668
669 dibMutex.enter();
670 while(dsect)
671 {
672 if(dsect->handle == handle)
673 {
674 dibMutex.leave();
675 return(dsect);
676 }
677 dsect = dsect->next;
678 }
679 dibMutex.leave();
680 return(NULL);
681}
682//******************************************************************************
683//A bitmap can only be selected into one DC, so this works.
684//******************************************************************************
685DIBSection *DIBSection::findHDC(HDC hdc)
686{
687 DIBSection *dsect = section;
688
689 while(dsect)
690 {
691 if(dsect->hdc == hdc)
692 {
693 return(dsect);
694 }
695 dsect = dsect->next;
696 }
697 return(NULL);
698}
699//******************************************************************************
700//******************************************************************************
701void DIBSection::deleteSection(DWORD handle)
702{
703 DIBSection *dsect = find(handle);
704
705 if(dsect)
706 delete dsect;
707
708}
709//******************************************************************************
710//******************************************************************************
711int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
712{
713 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
714 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
715
716 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
717 if(iSize == sizeof(DIBSECTION))
718 {
719 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
720 return sizeof(DIBSECTION);
721 }
722 else
723 if(iSize == sizeof(BITMAP_W))
724 {
725 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
726 return sizeof(BITMAP_W);
727 }
728 return 0;
729
730}
731//******************************************************************************
732//******************************************************************************
733int DIBSection::GetBitCount()
734{
735 if(pOS2bmp == NULL)
736 return 0;
737 else
738 return pOS2bmp->cBitCount;
739}
740//******************************************************************************
741//******************************************************************************
742int DIBSection::GetHeight()
743{
744 if(pOS2bmp == NULL)
745 return 0;
746 else
747 return pOS2bmp->cy;
748}
749//******************************************************************************
750//******************************************************************************
751DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.