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

Last change on this file since 4450 was 4450, checked in by sandervl, 25 years ago

cleanup (layout)

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