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

Last change on this file since 5390 was 5390, checked in by sandervl, 24 years ago

DeleteDC check for DIB section + SelectObject fixes

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