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

Last change on this file since 9966 was 9966, checked in by sandervl, 22 years ago

Fix for partial dibsection resyncs

File size: 30.5 KB
Line 
1/* $Id: dibsect.cpp,v 1.65 2003-04-01 15:57:05 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#include <win32api.h>
23#include <winconst.h>
24#include <winuser32.h>
25#include <cpuhlp.h>
26#include <dcdata.h>
27#include "dibsect.h"
28#include "oslibgpi.h"
29#include "rgbcvt.h"
30
31#define DBG_LOCALLOG DBG_dibsect
32#include "dbglocal.h"
33
34
35//******************************************************************************
36//******************************************************************************
37DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
38 : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsDblBuffer(NULL),
39 hdc(0), hwndParent(0)
40{
41 int palsize=0;
42
43 bmpsize = pbmi->biWidth;
44 /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
45
46 this->fFlip = fFlip;
47 os2bmphdrsize = sizeof(BITMAPINFO2);
48
49 switch(pbmi->biBitCount)
50 {
51 case 1:
52 bmpsize = ((bmpsize + 31) & ~31) / 8;
53 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
54 os2bmphdrsize += palsize;
55 break;
56 case 4:
57 bmpsize = ((bmpsize + 7) & ~7) / 2;
58 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
59 os2bmphdrsize += palsize;
60 break;
61 case 8:
62 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
63 os2bmphdrsize += palsize;
64 bmpsize = (bmpsize + 3) & ~3;
65 break;
66 case 16:
67 bmpsize *= 2;
68 bmpsize = (bmpsize + 3) & ~3;
69 break;
70 case 24:
71 bmpsize *= 3;
72 bmpsize = (bmpsize + 3) & ~3;
73 break;
74 case 32:
75 bmpsize *= 4;
76 break;
77 default:
78 dprintf(("Unsupported nr of bits %d", pbmi->biBitCount));
79 DebugInt3();
80 break;
81 }
82
83 this->hSection = hSection;
84 this->dwOffset = dwOffset;
85 if(hSection) {
86 bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight - dwOffset);
87 if(!bmpBits) {
88 dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
89 DebugInt3();
90 }
91 }
92 if(!bmpBits) {
93 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
94 }
95 memset(bmpBits, 0, bmpsize*pbmi->biHeight);
96
97 pOS2bmp = (BITMAPINFO2 *)malloc(os2bmphdrsize);
98
99 memset(pOS2bmp, /* set header + palette entries to zero */
100 0,
101 os2bmphdrsize);
102
103 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
104 pOS2bmp->cx = pbmi->biWidth;
105 pOS2bmp->cy = pbmi->biHeight;
106 pOS2bmp->cPlanes = pbmi->biPlanes;
107 pOS2bmp->cBitCount = pbmi->biBitCount;
108 pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4)
109 //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
110 if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
111 pOS2bmp->ulCompression = 0;
112 }
113 pOS2bmp->cbImage = pbmi->biSizeImage;
114 dprintf(("handle %x", handle));
115 dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
116 dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
117 dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
118 dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
119 dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
120 dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
121 dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
122
123 // clear DIBSECTION structure
124 memset(&dibinfo, 0, sizeof(dibinfo));
125
126 // copy BITMAPINFOHEADER data into DIBSECTION structure
127 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
128 dibinfo.dsBm.bmType = 0;
129 dibinfo.dsBm.bmWidth = pbmi->biWidth;
130 dibinfo.dsBm.bmHeight = pbmi->biHeight;
131 dibinfo.dsBm.bmWidthBytes= bmpsize;
132 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
133 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
134 dibinfo.dsBm.bmBits = bmpBits;
135
136 dibinfo.dshSection = hSection;
137 dibinfo.dsOffset = dwOffset;
138
139 if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
140 {
141 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
142 if(palsize) {
143 SetDIBColorTable(0, (1 << pbmi->biBitCount), (RGBQUAD *)(pbmi+1));
144 }
145 }
146 else {
147 switch(pbmi->biBitCount)
148 {
149 case 16:
150 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
151 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
152 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
153 break;
154
155 case 24:
156 case 32:
157 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff0000;
158 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x00ff00;
159 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x0000ff;
160 if(dibinfo.dsBitfields[0] != 0xff0000 && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff) {
161 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
162 }
163 break;
164 }
165 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
166 }
167 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
168 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
169 DosAllocMem((PPVOID)&bmpBitsDblBuffer, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
170 }
171
172 this->handle = handle;
173 this->iUsage = iUsage;
174
175 lock();
176 if(section == NULL)
177 {
178 dprintf(("section was NULL\n"));
179 section = this;
180 }
181 else
182 {
183 DIBSection *dsect = section;
184 dprintf2(("Increment section starting at %08X\n",dsect));
185
186 while ( (dsect->next != this) &&
187 (dsect->next != NULL) )
188 {
189//// dprintf2(("Increment section to %08X\n",dsect->next));
190 dsect = dsect->next;
191 }
192 dsect->next = this;
193 }
194 unlock();
195}
196//******************************************************************************
197//******************************************************************************
198DIBSection::~DIBSection()
199{
200 dprintf(("Delete DIBSection %x", handle));
201
202 if(hSection) {
203 UnmapViewOfFile(bmpBits);
204 }
205 else
206 if(bmpBits)
207 DosFreeMem(bmpBits);
208
209 if(bmpBitsDblBuffer)
210 DosFreeMem(bmpBitsDblBuffer);
211
212 if(pOS2bmp)
213 free(pOS2bmp);
214
215 lock();
216 if(section == this)
217 {
218 section = this->next;
219 }
220 else
221 {
222 DIBSection *dsect = section;
223
224 while(dsect->next != this)
225 {
226 dsect = dsect->next;
227 }
228 dsect->next = this->next;
229 }
230 unlock();
231}
232//******************************************************************************
233//******************************************************************************
234int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
235 lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
236 UINT coloruse)
237{
238 lines = (int)lines >= 0 ? (int)lines : (int)-lines;
239 int palsize=0;
240
241 bmpsize = pbmi->biWidth;
242 os2bmphdrsize = sizeof(BITMAPINFO2);
243
244 switch(pbmi->biBitCount)
245 {
246 case 1:
247 bmpsize = ((bmpsize + 31) & ~31) / 8;
248 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
249 os2bmphdrsize += palsize;
250 break;
251 case 4:
252 bmpsize = ((bmpsize + 7) & ~7) / 2;
253 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
254 os2bmphdrsize += palsize;
255 break;
256 case 8:
257 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
258 os2bmphdrsize += palsize;
259 bmpsize = (bmpsize + 3) & ~3;
260 break;
261 case 16:
262 bmpsize *= 2;
263 bmpsize = (bmpsize + 3) & ~3;
264 break;
265 case 24:
266 bmpsize *= 3;
267 bmpsize = (bmpsize + 3) & ~3;
268 break;
269 case 32:
270 bmpsize *= 4;
271 break;
272 }
273
274 //SvL: TODO: Correct??
275 if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
276 pOS2bmp->cBitCount != pbmi->biBitCount)
277 {
278 char *oldbits = bmpBits;
279 int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
280
281 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
282 memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
283 DosFreeMem(oldbits);
284 }
285 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmphdrsize);
286 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
287 pOS2bmp->cx = pbmi->biWidth;
288 pOS2bmp->cy = pbmi->biHeight;
289 pOS2bmp->cPlanes = pbmi->biPlanes;
290 pOS2bmp->cBitCount = pbmi->biBitCount;
291 pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4)
292 //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
293 if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
294 pOS2bmp->ulCompression = 0;
295 }
296 pOS2bmp->cbImage = pbmi->biSizeImage;
297
298 // clear DIBSECTION structure
299 memset(&dibinfo, 0, sizeof(dibinfo));
300
301 // copy BITMAPINFOHEADER data into DIBSECTION structure
302 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
303 dibinfo.dsBm.bmType = 0;
304 dibinfo.dsBm.bmWidth = pbmi->biWidth;
305 dibinfo.dsBm.bmHeight = pbmi->biHeight;
306 dibinfo.dsBm.bmWidthBytes= bmpsize;
307 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
308 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
309 dibinfo.dsBm.bmBits = bmpBits;
310
311 dibinfo.dshSection = hSection;
312 dibinfo.dsOffset = dwOffset;
313
314 if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
315 {
316 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
317 }
318 else {
319 char *pColors = (char *)pbmi + 1;
320
321 switch(pbmi->biBitCount)
322 {
323 case 16:
324 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
325 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
326 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
327 break;
328
329 case 24:
330 case 32:
331 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff0000;
332 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x00ff00;
333 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x0000ff;
334 if(dibinfo.dsBitfields[0] != 0xff0000 && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff) {
335 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
336 }
337 break;
338 }
339 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
340 }
341
342 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
343 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
344 if(bmpBitsDblBuffer) {
345 DosFreeMem(bmpBitsDblBuffer);
346 }
347 DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
348 }
349
350 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
351 if(palsize) {
352 SetDIBColorTable(0, 1 << pbmi->biBitCount, (RGBQUAD *)(pbmi+1));
353 }
354
355 if(bits)
356 {
357 if(pOS2bmp->ulCompression == BCA_UNCOMP) {
358 int size = bmpsize*lines;
359 memcpy(bmpBits+bmpsize*startscan, bits, size);
360 }
361 else {
362 dprintf(("Compressed image!!"));
363 if(startscan != 0) {
364 dprintf(("WARNING: Compressed image & startscan != 0!!!!"));
365 }
366 memcpy(bmpBits, bits, pbmi->biSizeImage);
367 }
368 }
369 return(lines);
370}
371//******************************************************************************
372//******************************************************************************
373int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
374{
375 int i, end;
376
377 dprintf(("SetDIBColorTable %d %d %x", startIdx, cEntries, rgb));
378
379 if(pOS2bmp->cBitCount > 8) {
380 dprintf(("DIBSection::SetDIBColorTable: bpp > 8; ignore"));
381 return 0;
382 }
383
384 end = startIdx + cEntries;
385 if(end > (1 << pOS2bmp->cBitCount)) {
386 end = (1 << pOS2bmp->cBitCount);
387 cEntries = end - startIdx;
388 }
389
390 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
391
392 for(i=startIdx;i<end;i++)
393 {
394 pOS2bmp->argbColor[i].fcOptions = 0;
395#ifdef DEBUG
396 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
397#endif
398 }
399 return(cEntries);
400}
401//******************************************************************************
402//******************************************************************************
403int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry)
404{
405 int i, end;
406
407 if(pOS2bmp->cBitCount > 8) {
408 dprintf(("DIBSection::SetDIBColorTable: bpp > 8; ignore"));
409 return 0;
410 }
411
412 end = startIdx + cEntries;
413 if(end > (1 << pOS2bmp->cBitCount)) {
414 end = (1 << pOS2bmp->cBitCount);
415 }
416 for(i=startIdx;i<end;i++)
417 {
418 pOS2bmp->argbColor[i].fcOptions = 0;
419 pOS2bmp->argbColor[i].bBlue = palentry[i].peBlue;
420 pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen;
421 pOS2bmp->argbColor[i].bRed = palentry[i].peRed;
422#ifdef DEBUG
423 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
424#endif
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 dprintf(("DIBSection::BitBlt hdc size (%d,%d) (WINDOW)", hdcWidth, hdcHeight));
492 }
493 else {
494 hdcHeight = pHps->bitmapHeight;
495 hdcWidth = pHps->bitmapWidth;
496 dprintf(("DIBSection::BitBlt hdc size (%d,%d) (BMP)", hdcWidth, hdcHeight));
497 }
498
499 //Don't clip destination size to destination DC size
500 //This messes up the two bitmaps in the opening window of Opera 6
501 //(choice between MDI & SDI interface)
502
503 //win32 coordinates are relative to left top, OS/2 expects left bottom
504 //source rectangle is non-inclusive (top, right not included)
505 //destination rectangle is incl.-inclusive (everything included)
506
507 point[0].x = nXdest;
508 point[1].x = nXdest + nDestWidth - 1;
509#ifdef INVERT
510 point[0].y = hdcHeight - nYdest - nDestHeight;
511 point[1].y = hdcHeight - nYdest - 1;
512#else
513 point[0].y = nYdest;
514 point[1].y = nYdest + nDestHeight - 1;
515#endif
516
517 //target rectangle is inclusive-inclusive
518 point[2].x = nXsrc;
519 point[3].x = nXsrc + nSrcWidth;
520#ifdef INVERT
521 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
522 point[3].y = pOS2bmp->cy - nYsrc;
523#else
524 point[2].y = nYsrc;
525 point[3].y = nYsrc + nSrcHeight;
526#endif
527
528#ifdef INVERT
529 oldyinversion = GpiQueryYInversion(hps);
530 if(oldyinversion != 0) {
531 POINT viewpt, winpt;
532
533 GetViewportOrgEx(hps, &viewpt);
534 GetWindowOrgEx(hps, &winpt);
535
536 dprintf(("Viewport origin (%d,%d)", viewpt.x, viewpt.y));
537 dprintf(("Windows origin (%d,%d)", winpt.x, winpt.y));
538
539 /* By resetting y inversion to 0, we must take the new windows
540 * origin into account. The default matrix set up for the origin
541 * depends on y inversion. Therefor we must add the y origin value,
542 * multiplied by two, to the top & bottom coordinates
543 */
544 point[0].y -= winpt.y*2;
545 point[1].y -= winpt.y*2;
546
547 /* By resetting y inversion to 0, we must take the new viewport
548 * origin into account. The default matrix set up for the origin
549 * depends on y inversion. Therefor we must subtract the y origin value,
550 * multiplied by two, from the top & bottom coordinates
551 */
552 point[0].y -= viewpt.y*2;
553 point[1].y -= viewpt.y*2;
554
555 GpiEnableYInversion(hps, 0);
556 fRestoryYInversion = TRUE;
557 }
558#else
559 dprintf(("Sync destination dibsection: hdc y inversion = %d", GpiQueryYInversion(hdc)));
560#endif
561
562 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
563 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
564 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
565
566#ifdef DEBUG
567 RECTL rcltemp;
568 GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
569 dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
570#endif
571
572 if(fFlip & FLIP_HOR)
573 {
574 ULONG x;
575 x = point[0].x;
576 point[0].x = point[1].x;
577 point[1].x = x;
578 }
579
580 ULONG os2mode, winmode;
581
582 os2mode = BBO_OR;
583 winmode = GetStretchBltMode(hdcDest);
584 switch(winmode) {
585 case BLACKONWHITE_W:
586 os2mode = BBO_AND;
587 break;
588 case WHITEONBLACK_W:
589 case HALFTONE_W: //TODO:
590 os2mode = BBO_OR;
591 break;
592 case COLORONCOLOR_W:
593 os2mode = BBO_IGNORE;
594 break;
595 }
596#ifndef INVERT
597 if(!(fFlip & FLIP_VERT)) {
598#else
599 if(fFlip & FLIP_VERT) {
600#endif
601 //manually reverse bitmap data
602 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
603 char *dst = bmpBitsDblBuffer;
604 for(int i=0;i<pOS2bmp->cy;i++) {
605 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
606 dst += dibinfo.dsBm.bmWidthBytes;
607 src -= dibinfo.dsBm.bmWidthBytes;
608 }
609 bitmapBits = bmpBitsDblBuffer;
610 }
611 else bitmapBits = bmpBits;
612
613 //Translate ROP
614 Rop = Rop >> 16;
615
616 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
617 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
618 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
619
620 if(bmpBitsDblBuffer == NULL)
621 DebugInt3();
622
623 // PH 2000/10/01 - Fix for Beyond Compare 1.9d
624 // Note: according to documentation, cmImage can be zero for
625 // RGB- / non-compressed bitmaps.
626 int iLength = pOS2bmp->cbImage;
627 if (iLength == 0)
628 iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
629
630 if (iLength > 0)
631 {
632 if(CPUFeatures & CPUID_MMX)
633 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
634 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
635 }
636 else
637 {
638 dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
639 }
640 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], Rop, os2mode);
641 }
642 else {
643 rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], Rop, os2mode);
644 }
645 if(rc == GPI_OK) {
646 DIBSection *destdib = DIBSection::findHDC(hdcDest);
647 if(destdib) {
648 destdib->sync(hps, nYdest, nDestHeight, FALSE);
649 }
650#ifdef INVERT
651 //restore old y inversion height
652 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
653#endif
654 SetLastError(ERROR_SUCCESS_W);
655
656 return(TRUE);
657 }
658#ifdef INVERT
659 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
660#endif
661
662 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));
663 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
664 return(FALSE);
665}
666//******************************************************************************
667//******************************************************************************
668void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
669{
670 APIRET rc;
671 char *destBuf;
672
673 dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
674
675 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
676 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
677
678#ifdef INVERT
679 int oldyinversion = 0;
680 if(orgYInversion == TRUE) {
681 oldyinversion = GpiQueryYInversion(hdc);
682 dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
683 if(oldyinversion != 0) {
684#ifdef DEBUG
685 POINT point;
686 GetViewportOrgEx(hdc, &point);
687 dprintf(("Viewport origin (%d,%d)", point.x, point.y));
688#endif
689 GpiEnableYInversion(hdc, 0);
690 }
691 }
692#else
693 dprintf(("Sync destination dibsection: hdc y inversion = %d", GpiQueryYInversion(hdc)));
694#endif
695
696#ifndef INVERT
697 if(!(fFlip & FLIP_VERT)) {
698#else
699 if(fFlip & FLIP_VERT) {
700#endif
701 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
702
703 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
704 // will fail in that case (CoolEdit 2000). Perhaps because the returned
705 // compressed image is larger than the original.
706 // Use uncompressed size instead
707 // NOTE: The correct size will be returned by GpiQueryBitmapBits
708 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
709
710#ifdef INVERT
711 int dest = dibinfo.dsBm.bmHeight - nYdest - nDestHeight;
712#else
713 int dest = nYdest;
714#endif
715 rc = GpiQueryBitmapBits(hdc, dest, nDestHeight, destBuf,
716 tmphdr);
717 if(rc == GPI_ALTERROR) {
718 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
719 }
720
721 //manually reverse bitmap data
722 char *src = destBuf;
723 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
724 for(int i=0;i<nDestHeight;i++) {
725 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
726 dst -= dibinfo.dsBm.bmWidthBytes;
727 src += dibinfo.dsBm.bmWidthBytes;
728 }
729 }
730 else {
731 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
732 // will fail in that case (CoolEdit 2000). Perhaps because the returned
733 // compressed image is larger than the original.
734 // Use uncompressed size instead
735 // NOTE: The correct size will be returned by GpiQueryBitmapBits
736 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
737
738 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
739
740#ifdef INVERT
741 int dest = dibinfo.dsBm.bmHeight - nYdest - nDestHeight;
742#else
743 int dest = nYdest;
744#endif
745 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
746 tmphdr);
747 if(rc == GPI_ALTERROR) {
748 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
749 }
750#ifdef DEBUG_PALETTE
751 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
752 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
753 {
754 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
755 }
756 }
757#endif
758 }
759 memcpy(pOS2bmp, tmphdr, os2bmphdrsize);
760
761 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
762 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
763
764 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
765
766 if(CPUFeatures & CPUID_MMX) {
767 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
768 }
769 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
770 }
771
772 free(tmphdr);
773 if(rc != nDestHeight) {
774 dprintf(("!WARNING!: GpiQueryBitmapBits returned %d instead of %d scanlines", rc, nDestHeight));
775 }
776
777#ifdef INVERT
778 if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
779#endif
780
781}
782//******************************************************************************
783//manual sync if no stretching and bpp is the same
784//WARNING: this also assumes the colortables are the same
785//******************************************************************************
786void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
787{
788 char *srcbuf, *destbuf;
789 int linesize;
790
791 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
792 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
793 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
794 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
795 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
796 for(int i=0;i<heightDst;i++) {
797 memcpy(destbuf, srcbuf, linesize);
798 destbuf += dibinfo.dsBm.bmWidthBytes;
799 srcbuf += linesize;
800 }
801}
802//******************************************************************************
803//******************************************************************************
804void DIBSection::SelectDIBObject(HDC hdc)
805{
806 this->hdc = hdc;
807 hwndParent = WindowFromDC(hdc);
808 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
809}
810//******************************************************************************
811//******************************************************************************
812DIBSection *DIBSection::findObj(HANDLE handle)
813{
814 // PH 2001-08-18 shortcut for performance optimization
815 if (!section)
816 return NULL;
817
818 DIBSection *dsect = section;
819 lock();
820
821 do
822 {
823 if(dsect->handle == handle)
824 {
825 unlock();
826 return(dsect);
827 }
828 dsect = dsect->next;
829 }
830 while(dsect);
831
832 unlock();
833 return(NULL);
834}
835//******************************************************************************
836//A bitmap can only be selected into one DC, so this works.
837//******************************************************************************
838DIBSection *DIBSection::findHDC(HDC hdc)
839{
840 // PH 2001-08-18 shortcut for performance optimization
841 if (!section)
842 return NULL;
843
844 DIBSection *dsect = section;
845
846 lock();
847 do
848 {
849 if(dsect->hdc == hdc)
850 {
851 unlock();
852 return(dsect);
853 }
854 dsect = dsect->next;
855 }
856 while(dsect);
857
858 unlock();
859 return(NULL);
860}
861//******************************************************************************
862//******************************************************************************
863void DIBSection::deleteSection(HANDLE handle)
864{
865 DIBSection *dsect = findObj(handle);
866
867 if(dsect)
868 delete dsect;
869}
870//******************************************************************************
871//******************************************************************************
872int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
873{
874 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
875 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
876
877 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
878 if(iSize == sizeof(DIBSECTION))
879 {
880 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
881 return sizeof(DIBSECTION);
882 }
883 else
884 if(iSize == sizeof(BITMAP_W))
885 {
886 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
887 return sizeof(BITMAP_W);
888 }
889 return 0;
890
891}
892//******************************************************************************
893//******************************************************************************
894int DIBSection::GetBitCount()
895{
896 if(pOS2bmp == NULL)
897 return 0;
898 else
899 return pOS2bmp->cBitCount;
900}
901//******************************************************************************
902//******************************************************************************
903int DIBSection::GetHeight()
904{
905 if(pOS2bmp == NULL)
906 return 0;
907 else
908 return pOS2bmp->cy;
909}
910//******************************************************************************
911//******************************************************************************
912int DIBSection::GetWidth()
913{
914 if(pOS2bmp == NULL)
915 return 0;
916 else
917 return pOS2bmp->cx;
918}
919//******************************************************************************
920//******************************************************************************
921void DIBSection::initDIBSection()
922{
923 InitializeCriticalSection(&dibcritsect);
924}
925//******************************************************************************
926//******************************************************************************
927DIBSection *DIBSection::section = NULL;
928CRITICAL_SECTION DIBSection::dibcritsect;
Note: See TracBrowser for help on using the repository browser.