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

Last change on this file since 9762 was 9762, checked in by sandervl, 23 years ago

Must manually correct y coordinates for DIB section blit since we reset the y inversion back to 0

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