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

Last change on this file since 7361 was 7361, checked in by phaller, 24 years ago

minor performance improvement

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