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

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

dib section sync, setdibits & setpalette bugfixes

File size: 27.8 KB
Line 
1/* $Id: dibsect.cpp,v 1.51 2001-05-25 10:05:29 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 dprintf(("SetDIBColorTable %d %d %x", startIdx, cEntries, rgb));
392 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
393 {
394 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
395 return(0);
396 }
397
398 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
399
400 for(i=startIdx;i<cEntries;i++)
401 {
402 pOS2bmp->argbColor[i].fcOptions = 0;
403#ifdef DEBUG_PALETTE
404 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
405#endif
406 }
407
408 return(cEntries);
409}
410//******************************************************************************
411//******************************************************************************
412int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry)
413{
414 int i;
415
416 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
417 {
418 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
419 return(0);
420 }
421
422 for(i=startIdx;i<cEntries;i++)
423 {
424 pOS2bmp->argbColor[i].fcOptions = 0;
425 pOS2bmp->argbColor[i].bBlue = palentry[i].peBlue;
426 pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen;
427 pOS2bmp->argbColor[i].bRed = palentry[i].peRed;
428 }
429
430 return(cEntries);
431}
432//******************************************************************************
433//******************************************************************************
434BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
435 int nDestHeight, int nXsrc, int nYsrc,
436 int nSrcWidth, int nSrcHeight, DWORD Rop)
437{
438 HPS hps = (HPS)hdcDest;
439 POINTL point[4];
440 LONG rc, hdcHeight, hdcWidth;
441 PVOID bitmapBits = NULL;
442 int oldyinversion = 0;
443 BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
444 HWND hwndDest;
445 pDCData pHps;
446
447 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
448 if(!pHps)
449 {
450 SetLastError(ERROR_INVALID_HANDLE_W);
451 return FALSE;
452 }
453
454 hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
455 if(hwndDest && pHps->hwnd && !pHps->isClient) {
456 fFrameWindowDC = TRUE;
457 }
458
459 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
460 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
461 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
462
463 if(hwndDest) {
464 RECT rect;
465
466 if(fFrameWindowDC) {
467 GetWindowRect(hwndDest, &rect);
468 }
469 else GetClientRect(hwndDest, &rect);
470 hdcHeight = rect.bottom - rect.top;
471 hdcWidth = rect.right - rect.left;
472 }
473 else {
474 hdcHeight = pHps->bitmapHeight;
475 hdcWidth = pHps->bitmapWidth;
476 }
477
478 //win32 coordinates are relative to left top, OS/2 expects left bottom
479 //source rectangle is non-inclusive (top, right not included)
480 //destination rectangle is incl.-inclusive (everything included)
481
482 if(nXdest + nDestWidth > hdcWidth) {
483 nDestWidth = hdcWidth - nXdest;
484 }
485
486 if(nYdest + nDestHeight > hdcHeight) {
487 nDestHeight = hdcHeight - nYdest;
488 }
489
490 point[0].x = nXdest;
491 point[0].y = hdcHeight - nYdest - nDestHeight;
492 point[1].x = nXdest + nDestWidth - 1;
493 point[1].y = hdcHeight - nYdest - 1;
494
495 //target rectangle is inclusive-inclusive
496 if(nXsrc + nSrcWidth > pOS2bmp->cx) {
497 nSrcWidth = pOS2bmp->cx - nXsrc;
498 }
499 if(nYsrc + nSrcHeight > pOS2bmp->cy) {
500 nSrcHeight = pOS2bmp->cy - nYsrc;
501 }
502 point[2].x = nXsrc;
503 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
504 point[3].x = nXsrc + nSrcWidth;
505 point[3].y = pOS2bmp->cy - nYsrc;
506
507 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
508 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
509 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
510
511#ifdef INVERT
512 oldyinversion = GpiQueryYInversion(hps);
513 if(oldyinversion != 0) {
514 GpiEnableYInversion(hps, 0);
515 fRestoryYInversion = TRUE;
516 }
517#endif
518
519#ifdef DEBUG
520 RECTL rcltemp;
521 GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
522 dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
523#endif
524
525 if(fFlip & FLIP_HOR)
526 {
527 ULONG x;
528 x = point[0].x;
529 point[0].x = point[1].x;
530 point[1].x = x;
531 }
532
533 ULONG os2mode, winmode;
534
535 os2mode = BBO_OR;
536 winmode = GetStretchBltMode(hdcDest);
537 switch(winmode) {
538 case BLACKONWHITE_W:
539 os2mode = BBO_AND;
540 break;
541 case WHITEONBLACK_W:
542 case HALFTONE_W: //TODO:
543 os2mode = BBO_OR;
544 break;
545 case COLORONCOLOR_W:
546 os2mode = BBO_IGNORE;
547 break;
548 }
549 if(fFlip & FLIP_VERT) {
550 //manually reverse bitmap data
551 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
552 char *dst = bmpBitsDblBuffer;
553 for(int i=0;i<pOS2bmp->cy;i++) {
554 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
555 dst += dibinfo.dsBm.bmWidthBytes;
556 src -= dibinfo.dsBm.bmWidthBytes;
557 }
558 bitmapBits = bmpBitsDblBuffer;
559 }
560 else bitmapBits = bmpBits;
561
562 switch(Rop) {
563 case 0xcc0020: /* SRCCOPY */
564 Rop = ROP_SRCCOPY;
565 break;
566 case 0xee0086: /* SRCPAINT */
567 Rop = ROP_SRCPAINT;
568 break;
569 case 0x8800c6: /* SRCAND */
570 Rop = ROP_SRCAND;
571 break;
572 case 0x660046: /* SRCINVERT */
573 Rop = ROP_SRCINVERT;
574 break;
575 case 0x440328: /* SRCERASE */
576 Rop = ROP_SRCERASE;
577 break;
578 case 0x330008: /* NOTSRCCOPY */
579 Rop = ROP_NOTSRCCOPY;
580 break;
581 case 0x1100a6: /* NOTSRCERASE */
582 Rop = ROP_NOTSRCERASE;
583 break;
584 case 0xc000ca: /* MERGECOPY */
585 Rop = ROP_MERGECOPY;
586 break;
587 case 0xbb0226: /* MERGEPAINT */
588 Rop = ROP_MERGEPAINT;
589 break;
590 case 0xf00021: /* PATCOPY */
591 Rop = ROP_PATCOPY;
592 break;
593 case 0xfb0a09: /* PATPAINT */
594 Rop = ROP_PATPAINT;
595 break;
596 case 0x5a0049: /* PATINVERT */
597 Rop = ROP_PATINVERT;
598 break;
599 case 0x550009: /* DSTINVERT */
600 Rop = ROP_DSTINVERT;
601 break;
602 case 0x000042: /* BLACKNESS */
603 Rop = ROP_ZERO;
604 break;
605 case 0xff0062: /* WHITENESS */
606 Rop = ROP_ONE;
607 break;
608 default:
609 Rop = ROP_SRCCOPY;
610 break;
611 }
612
613 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
614 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
615 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
616
617 if(bmpBitsDblBuffer == NULL)
618 DebugInt3();
619
620 // PH 2000/10/01 - Fix for Beyond Compare 1.9d
621 // Note: according to documentation, cmImage can be zero for
622 // RGB- / non-compressed bitmaps.
623 int iLength = pOS2bmp->cbImage;
624 if (iLength == 0)
625 iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
626
627 if (iLength > 0)
628 {
629 if(CPUFeatures & CPUID_MMX)
630 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
631 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
632 }
633 else
634 {
635 dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
636 }
637
638
639 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], Rop, os2mode);
640 }
641 else {
642 rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], Rop, os2mode);
643 }
644 if(rc == GPI_OK) {
645 DIBSection *destdib = DIBSection::findHDC(hdcDest);
646 if(destdib) {
647 destdib->sync(hps, nYdest, nDestHeight, FALSE);
648 }
649#ifdef INVERT
650 //restore old y inversion height
651 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
652#endif
653 SetLastError(ERROR_SUCCESS_W);
654
655 return(TRUE);
656 }
657#ifdef INVERT
658 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
659#endif
660
661 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));
662 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
663 return(FALSE);
664}
665//******************************************************************************
666//******************************************************************************
667void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
668{
669 APIRET rc;
670 char *destBuf;
671
672 dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
673
674 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
675 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
676
677#ifdef INVERT
678 int oldyinversion = 0;
679 if(orgYInversion == TRUE) {
680 oldyinversion = GpiQueryYInversion(hdc);
681 dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
682 if(oldyinversion != 0) {
683 GpiEnableYInversion(hdc, 0);
684 }
685 }
686#endif
687
688 if(fFlip & FLIP_VERT) {
689 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
690
691 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
692 tmphdr);
693 //manually reverse bitmap data
694 char *src = destBuf;
695 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
696 for(int i=0;i<nDestHeight;i++) {
697 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
698 dst -= dibinfo.dsBm.bmWidthBytes;
699 src += dibinfo.dsBm.bmWidthBytes;
700 }
701 }
702 else {
703 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
704 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
705 tmphdr);
706
707#ifdef DEBUG_PALETTE
708 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
709 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
710 {
711 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
712 }
713 }
714#endif
715 }
716 memcpy(pOS2bmp, tmphdr, os2bmphdrsize);
717
718#if 0
719 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
720 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
721
722 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
723
724 if(CPUFeatures & CPUID_MMX) {
725 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
726 }
727 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
728 }
729#endif
730 free(tmphdr);
731 if(rc != nDestHeight) {
732 DebugInt3();
733 }
734
735#ifdef INVERT
736 if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
737#endif
738
739}
740//******************************************************************************
741//manual sync if no stretching and bpp is the same
742//WARNING: this also assumes the colortables are the same
743//******************************************************************************
744void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
745{
746 char *srcbuf, *destbuf;
747 int linesize;
748
749 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
750 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
751 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
752 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
753 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
754 for(int i=0;i<heightDst;i++) {
755 memcpy(destbuf, srcbuf, linesize);
756 destbuf += dibinfo.dsBm.bmWidthBytes;
757 srcbuf += linesize;
758 }
759}
760//******************************************************************************
761//******************************************************************************
762void DIBSection::SelectDIBObject(HDC hdc)
763{
764 this->hdc = hdc;
765 hwndParent = WindowFromDC(hdc);
766 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
767}
768//******************************************************************************
769//******************************************************************************
770DIBSection *DIBSection::findObj(HANDLE handle)
771{
772 DIBSection *dsect = section;
773
774 dibMutex.enter();
775 while(dsect)
776 {
777 if(dsect->handle == handle)
778 {
779 dibMutex.leave();
780 return(dsect);
781 }
782 dsect = dsect->next;
783 }
784 dibMutex.leave();
785 return(NULL);
786}
787//******************************************************************************
788//A bitmap can only be selected into one DC, so this works.
789//******************************************************************************
790DIBSection *DIBSection::findHDC(HDC hdc)
791{
792 DIBSection *dsect = section;
793
794 while(dsect)
795 {
796 if(dsect->hdc == hdc)
797 {
798 return(dsect);
799 }
800 dsect = dsect->next;
801 }
802 return(NULL);
803}
804//******************************************************************************
805//******************************************************************************
806void DIBSection::deleteSection(HANDLE handle)
807{
808 DIBSection *dsect = findObj(handle);
809
810 if(dsect)
811 delete dsect;
812}
813//******************************************************************************
814//******************************************************************************
815int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
816{
817 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
818 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
819
820 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
821 if(iSize == sizeof(DIBSECTION))
822 {
823 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
824 return sizeof(DIBSECTION);
825 }
826 else
827 if(iSize == sizeof(BITMAP_W))
828 {
829 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
830 return sizeof(BITMAP_W);
831 }
832 return 0;
833
834}
835//******************************************************************************
836//******************************************************************************
837int DIBSection::GetBitCount()
838{
839 if(pOS2bmp == NULL)
840 return 0;
841 else
842 return pOS2bmp->cBitCount;
843}
844//******************************************************************************
845//******************************************************************************
846int DIBSection::GetHeight()
847{
848 if(pOS2bmp == NULL)
849 return 0;
850 else
851 return pOS2bmp->cy;
852}
853//******************************************************************************
854//******************************************************************************
855int DIBSection::GetWidth()
856{
857 if(pOS2bmp == NULL)
858 return 0;
859 else
860 return pOS2bmp->cx;
861}
862//******************************************************************************
863//******************************************************************************
864DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.