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

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

rgb conversions added + palette fix

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