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

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

more logging

File size: 26.6 KB
Line 
1/* $Id: dibsect.cpp,v 1.47 2001-03-21 12:31:36 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{
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; //same as OS/2 (uncompressed, rle8, rle4)
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; //same as OS/2 (uncompressed, rle8, rle4)
300 //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
301 if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
302 pOS2bmp->ulCompression = 0;
303 }
304 pOS2bmp->cbImage = pbmi->biSizeImage;
305
306 // clear DIBSECTION structure
307 memset(&dibinfo, 0, sizeof(dibinfo));
308
309 // copy BITMAPINFOHEADER data into DIBSECTION structure
310 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
311 dibinfo.dsBm.bmType = 0;
312 dibinfo.dsBm.bmWidth = pbmi->biWidth;
313 dibinfo.dsBm.bmHeight = pbmi->biHeight;
314 dibinfo.dsBm.bmWidthBytes= bmpsize;
315 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
316 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
317 dibinfo.dsBm.bmBits = bmpBits;
318
319 dibinfo.dshSection = hSection;
320 dibinfo.dsOffset = dwOffset;
321
322 if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
323 {
324 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
325 }
326 else {
327 char *pColors = (char *)pbmi + 1;
328
329 switch(pbmi->biBitCount)
330 {
331 case 16:
332 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
333 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
334 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
335 break;
336
337 case 24:
338 dibinfo.dsBitfields[0] = 0xff;
339 dibinfo.dsBitfields[1] = 0xff00;
340 dibinfo.dsBitfields[2] = 0xff0000;
341 break;
342
343 case 32:
344 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
345 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
346 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
347 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
348 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
349 }
350 break;
351 }
352 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
353 }
354
355 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
356 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
357 if(bmpBitsDblBuffer) {
358 DosFreeMem(bmpBitsDblBuffer);
359 }
360 DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
361 }
362
363 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
364 if(palsize) {
365 SetDIBColorTable(0, 1 << pbmi->biBitCount, (RGBQUAD *)(pbmi+1));
366 }
367
368 if(bits)
369 {
370 if(pOS2bmp->ulCompression == BCA_UNCOMP) {
371 int size = bmpsize*lines;
372 memcpy(bmpBits+bmpsize*startscan, bits, size);
373 }
374 else {
375 dprintf(("Compressed image!!"));
376 if(startscan != 0) {
377 dprintf(("WARNING: Compressed image & startscan != 0!!!!"));
378 }
379 memcpy(bmpBits, bits, pbmi->biSizeImage);
380 }
381 }
382 return(lines);
383}
384//******************************************************************************
385//******************************************************************************
386int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
387{
388 int i;
389
390 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
391 {
392 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
393 return(0);
394 }
395
396 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
397
398 for(i=startIdx;i<cEntries;i++)
399 {
400 pOS2bmp->argbColor[i].fcOptions = 0;
401#ifdef DEBUG_PALETTE
402 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
403#endif
404 }
405
406 return(cEntries);
407}
408//******************************************************************************
409//******************************************************************************
410int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry)
411{
412 int i;
413
414 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
415 {
416 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
417 return(0);
418 }
419
420 for(i=startIdx;i<cEntries;i++)
421 {
422 pOS2bmp->argbColor[i].fcOptions = 0;
423 pOS2bmp->argbColor[i].bBlue = palentry[i].peBlue;
424 pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen;
425 pOS2bmp->argbColor[i].bRed = palentry[i].peRed;
426 }
427
428 return(cEntries);
429}
430//******************************************************************************
431//******************************************************************************
432BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
433 int nDestHeight, int nXsrc, int nYsrc,
434 int nSrcWidth, int nSrcHeight, DWORD Rop)
435{
436 HPS hps = (HPS)hdcDest;
437 POINTL point[4];
438 LONG rc, hdcHeight, hdcWidth;
439 PVOID bitmapBits = NULL;
440 int oldyinversion = 0;
441 BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
442 HWND hwndDest;
443 pDCData pHps;
444
445 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
446 if(!pHps)
447 {
448 SetLastError(ERROR_INVALID_HANDLE_W);
449 return FALSE;
450 }
451
452 hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
453 if(hwndDest && pHps->hwnd && !pHps->isClient) {
454 fFrameWindowDC = TRUE;
455 }
456
457 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
458 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
459 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
460
461 if(hwndDest) {
462 RECT rect;
463
464 if(fFrameWindowDC) {
465 GetWindowRect(hwndDest, &rect);
466 }
467 else GetClientRect(hwndDest, &rect);
468 hdcHeight = rect.bottom - rect.top;
469 hdcWidth = rect.right - rect.left;
470 }
471 else {
472 DIBSection *dsect = DIBSection::findHDC(hdcDest);
473 if(dsect)
474 {
475 hdcHeight = dsect->GetHeight();
476 hdcWidth = dsect->GetWidth();
477 }
478 else
479 {
480 hdcHeight = pOS2bmp->cy;
481 hdcWidth = pOS2bmp->cx;
482 }
483 }
484
485 //win32 coordinates are relative to left top, OS/2 expects left bottom
486 //source rectangle is non-inclusive (top, right not included)
487 //destination rectangle is incl.-inclusive (everything included)
488
489 if(nXdest + nDestWidth > hdcWidth) {
490 nDestWidth = hdcWidth - nXdest;
491 }
492
493 if(nYdest + nDestHeight > hdcHeight) {
494 nDestHeight = hdcHeight - nYdest;
495 }
496
497 point[0].x = nXdest;
498 point[0].y = hdcHeight - nYdest - nDestHeight;
499 point[1].x = nXdest + nDestWidth - 1;
500 point[1].y = hdcHeight - nYdest - 1;
501
502 //target rectangle is inclusive-inclusive
503 if(nXsrc + nSrcWidth > pOS2bmp->cx) {
504 nSrcWidth = pOS2bmp->cx - nXsrc;
505 }
506 if(nYsrc + nSrcHeight > pOS2bmp->cy) {
507 nSrcHeight = pOS2bmp->cy - nYsrc;
508 }
509 point[2].x = nXsrc;
510 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
511 point[3].x = nXsrc + nSrcWidth;
512 point[3].y = pOS2bmp->cy - nYsrc;
513
514 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
515 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
516 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
517
518#ifdef INVERT
519 oldyinversion = GpiQueryYInversion(hps);
520 if(oldyinversion != 0) {
521 GpiEnableYInversion(hps, 0);
522 fRestoryYInversion = TRUE;
523 }
524#endif
525
526#ifdef DEBUG
527 RECTL rcltemp;
528 GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
529 dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
530#endif
531
532 if(fFlip & FLIP_HOR)
533 {
534 ULONG x;
535 x = point[0].x;
536 point[0].x = point[1].x;
537 point[1].x = x;
538 }
539
540 ULONG os2mode, winmode;
541
542 os2mode = BBO_OR;
543 winmode = GetStretchBltMode(hdcDest);
544 switch(winmode) {
545 case BLACKONWHITE_W:
546 os2mode = BBO_AND;
547 break;
548 case WHITEONBLACK_W:
549 case HALFTONE_W: //TODO:
550 os2mode = BBO_OR;
551 break;
552 case COLORONCOLOR_W:
553 os2mode = BBO_IGNORE;
554 break;
555 }
556 if(fFlip & FLIP_VERT) {
557 //manually reverse bitmap data
558 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
559 char *dst = bmpBitsDblBuffer;
560 for(int i=0;i<pOS2bmp->cy;i++) {
561 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
562 dst += dibinfo.dsBm.bmWidthBytes;
563 src -= dibinfo.dsBm.bmWidthBytes;
564 }
565 bitmapBits = bmpBitsDblBuffer;
566 }
567 else bitmapBits = bmpBits;
568
569 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
570 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
571 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
572
573 if(bmpBitsDblBuffer == NULL)
574 DebugInt3();
575
576 // PH 2000/10/01 - Fix for Beyond Compare 1.9d
577 // Note: according to documentation, cmImage can be zero for
578 // RGB- / non-compressed bitmaps.
579 int iLength = pOS2bmp->cbImage;
580 if (iLength == 0)
581 iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
582
583 if (iLength > 0)
584 {
585 if(CPUFeatures & CPUID_MMX)
586 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
587 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
588 }
589 else
590 {
591 dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
592 }
593
594
595 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
596 }
597 else {
598 rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
599 }
600 if(rc == GPI_OK) {
601 DIBSection *destdib = DIBSection::findHDC(hdcDest);
602 if(destdib) {
603 destdib->sync(hps, nYdest, nDestHeight, FALSE);
604 }
605#ifdef INVERT
606 //restore old y inversion height
607 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
608#endif
609 SetLastError(ERROR_SUCCESS_W);
610
611 return(TRUE);
612 }
613#ifdef INVERT
614 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
615#endif
616
617 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));
618 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
619 return(FALSE);
620}
621//******************************************************************************
622//******************************************************************************
623void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
624{
625 APIRET rc;
626 char *destBuf;
627
628 dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
629
630 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
631 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
632
633#ifdef INVERT
634 int oldyinversion = 0;
635 if(orgYInversion == TRUE) {
636 oldyinversion = GpiQueryYInversion(hdc);
637 dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
638 if(oldyinversion != 0) {
639 GpiEnableYInversion(hdc, 0);
640 }
641 }
642#endif
643
644 if(fFlip & FLIP_VERT) {
645 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
646
647 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
648 tmphdr);
649 //manually reverse bitmap data
650 char *src = destBuf;
651 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
652 for(int i=0;i<nDestHeight;i++) {
653 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
654 dst -= dibinfo.dsBm.bmWidthBytes;
655 src += dibinfo.dsBm.bmWidthBytes;
656 }
657 }
658 else {
659 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
660 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
661 tmphdr);
662
663#ifdef DEBUG_PALETTE
664 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
665 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
666 {
667 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
668 }
669 }
670#endif
671 }
672
673#if 0
674 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
675 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
676
677 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
678
679 if(CPUFeatures & CPUID_MMX) {
680 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
681 }
682 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
683 }
684#endif
685 free(tmphdr);
686 if(rc != nDestHeight) {
687 DebugInt3();
688 }
689
690#ifdef INVERT
691 if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
692#endif
693
694}
695//******************************************************************************
696//manual sync if no stretching and bpp is the same
697//WARNING: this also assumes the colortables are the same
698//******************************************************************************
699void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
700{
701 char *srcbuf, *destbuf;
702 int linesize;
703
704 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
705 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
706 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
707 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
708 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
709 for(int i=0;i<heightDst;i++) {
710 memcpy(destbuf, srcbuf, linesize);
711 destbuf += dibinfo.dsBm.bmWidthBytes;
712 srcbuf += linesize;
713 }
714}
715//******************************************************************************
716//******************************************************************************
717void DIBSection::SelectDIBObject(HDC hdc)
718{
719 this->hdc = hdc;
720 hwndParent = WindowFromDC(hdc);
721 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
722}
723//******************************************************************************
724//******************************************************************************
725DIBSection *DIBSection::find(DWORD handle)
726{
727 DIBSection *dsect = section;
728
729 dibMutex.enter();
730 while(dsect)
731 {
732 if(dsect->handle == handle)
733 {
734 dibMutex.leave();
735 return(dsect);
736 }
737 dsect = dsect->next;
738 }
739 dibMutex.leave();
740 return(NULL);
741}
742//******************************************************************************
743//A bitmap can only be selected into one DC, so this works.
744//******************************************************************************
745DIBSection *DIBSection::findHDC(HDC hdc)
746{
747 DIBSection *dsect = section;
748
749 while(dsect)
750 {
751 if(dsect->hdc == hdc)
752 {
753 return(dsect);
754 }
755 dsect = dsect->next;
756 }
757 return(NULL);
758}
759//******************************************************************************
760//******************************************************************************
761void DIBSection::deleteSection(DWORD handle)
762{
763 DIBSection *dsect = find(handle);
764
765 if(dsect)
766 delete dsect;
767}
768//******************************************************************************
769//******************************************************************************
770int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
771{
772 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
773 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
774
775 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
776 if(iSize == sizeof(DIBSECTION))
777 {
778 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
779 return sizeof(DIBSECTION);
780 }
781 else
782 if(iSize == sizeof(BITMAP_W))
783 {
784 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
785 return sizeof(BITMAP_W);
786 }
787 return 0;
788
789}
790//******************************************************************************
791//******************************************************************************
792int DIBSection::GetBitCount()
793{
794 if(pOS2bmp == NULL)
795 return 0;
796 else
797 return pOS2bmp->cBitCount;
798}
799//******************************************************************************
800//******************************************************************************
801int DIBSection::GetHeight()
802{
803 if(pOS2bmp == NULL)
804 return 0;
805 else
806 return pOS2bmp->cy;
807}
808//******************************************************************************
809//******************************************************************************
810int DIBSection::GetWidth()
811{
812 if(pOS2bmp == NULL)
813 return 0;
814 else
815 return pOS2bmp->cx;
816}
817//******************************************************************************
818//******************************************************************************
819DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.