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

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

minor update

File size: 26.9 KB
Line 
1/* $Id: dibsect.cpp,v 1.45 2001-01-05 23:25:30 sandervl Exp $ */
2
3/*
4 * GDI32 DIB sections
5 *
6 * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
7 * Copyright 1998 Patrick Haller
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * NOTE:
12 * This is not a complete solution for CreateDIBSection, but enough for Quake 2!
13 *
14 */
15#define INCL_GPI
16#define INCL_WIN
17#include <os2wrap.h> //Odin32 OS/2 api wrappers
18#include <stdlib.h>
19#include <string.h>
20#include <win32type.h>
21#include <misc.h>
22#define OS2_ONLY
23#include "dibsect.h"
24#include <vmutex.h>
25#include <win32api.h>
26#include <winconst.h>
27#include <winuser32.h>
28#include <cpuhlp.h>
29#include <dcdata.h>
30#include "oslibgpi.h"
31#include "rgbcvt.h"
32
33#define DBG_LOCALLOG DBG_dibsect
34#include "dbglocal.h"
35
36static VMutex dibMutex;
37
38//******************************************************************************
39//******************************************************************************
40DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
41 : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsDblBuffer(NULL)
42{
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; //same as OS/2 (uncompressed, rle8, rle4)
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; //same as OS/2 (uncompressed, rle8, rle4)
300 //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
301 if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
302 pOS2bmp->ulCompression = 0;
303 }
304 pOS2bmp->cbImage = pbmi->biSizeImage;
305
306 // clear DIBSECTION structure
307 memset(&dibinfo, 0, sizeof(dibinfo));
308
309 // copy BITMAPINFOHEADER data into DIBSECTION structure
310 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
311 dibinfo.dsBm.bmType = 0;
312 dibinfo.dsBm.bmWidth = pbmi->biWidth;
313 dibinfo.dsBm.bmHeight = pbmi->biHeight;
314 dibinfo.dsBm.bmWidthBytes= bmpsize;
315 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
316 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
317 dibinfo.dsBm.bmBits = bmpBits;
318
319 dibinfo.dshSection = hSection;
320 dibinfo.dsOffset = dwOffset;
321
322 if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
323 {
324 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
325 }
326 else {
327 char *pColors = (char *)pbmi + 1;
328
329 switch(pbmi->biBitCount)
330 {
331 case 16:
332 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
333 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
334 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
335 break;
336
337 case 24:
338 dibinfo.dsBitfields[0] = 0xff;
339 dibinfo.dsBitfields[1] = 0xff00;
340 dibinfo.dsBitfields[2] = 0xff0000;
341 break;
342
343 case 32:
344 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
345 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
346 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
347 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
348 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
349 }
350 break;
351 }
352 dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
353 }
354
355 //double buffer for rgb 555 dib sections (for conversion) or flipped sections
356 if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
357 if(bmpBitsDblBuffer) {
358 DosFreeMem(bmpBitsDblBuffer);
359 }
360 DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
361 }
362
363 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
364 if(palsize) {
365 SetDIBColorTable(0, 1 << pbmi->biBitCount, (RGBQUAD *)(pbmi+1));
366 }
367
368 if(bits)
369 {
370 if(pOS2bmp->ulCompression == BCA_UNCOMP) {
371 int size = bmpsize*lines;
372 memcpy(bmpBits+bmpsize*startscan, bits, size);
373 }
374 else {
375 dprintf(("Compressed image!!"));
376 if(startscan != 0) {
377 dprintf(("WARNING: Compressed image & startscan != 0!!!!"));
378 }
379 memcpy(bmpBits, bits, pbmi->biSizeImage);
380 }
381 }
382 return(lines);
383}
384//******************************************************************************
385//******************************************************************************
386int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
387{
388 int i;
389
390 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
391 {
392 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
393 return(0);
394 }
395
396 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
397
398 for(i=startIdx;i<cEntries;i++)
399 {
400 pOS2bmp->argbColor[i].fcOptions = 0;
401#ifdef DEBUG_PALETTE
402 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
403#endif
404 }
405
406 return(cEntries);
407}
408//******************************************************************************
409//******************************************************************************
410int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry)
411{
412 int i;
413
414 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
415 {
416 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
417 return(0);
418 }
419
420 for(i=startIdx;i<cEntries;i++)
421 {
422 pOS2bmp->argbColor[i].fcOptions = 0;
423 pOS2bmp->argbColor[i].bBlue = palentry[i].peBlue;
424 pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen;
425 pOS2bmp->argbColor[i].bRed = palentry[i].peRed;
426 }
427
428 return(cEntries);
429}
430//******************************************************************************
431//******************************************************************************
432BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
433 int nDestHeight, int nXsrc, int nYsrc,
434 int nSrcWidth, int nSrcHeight, DWORD Rop)
435{
436 HPS hps = (HPS)hdcDest;
437 POINTL point[4];
438 LONG rc, hdcHeight, hdcWidth;
439 PVOID bitmapBits = NULL;
440 int oldyinversion = 0;
441 BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
442 HWND hwndDest;
443 pDCData pHps;
444
445 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
446 if(!pHps)
447 {
448 SetLastError(ERROR_INVALID_HANDLE_W);
449 return FALSE;
450 }
451
452 hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
453 if(hwndDest && pHps->hwnd && !pHps->isClient) {
454 fFrameWindowDC = TRUE;
455 }
456
457 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
458 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
459 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
460
461 if(hwndDest) {
462 RECT rect;
463
464 if(fFrameWindowDC) {
465 GetWindowRect(hwndDest, &rect);
466 }
467 else GetClientRect(hwndDest, &rect);
468 hdcHeight = rect.bottom - rect.top;
469 hdcWidth = rect.right - rect.left;
470 }
471 else {
472 DIBSection *dsect = DIBSection::findHDC(hdcDest);
473 if(dsect)
474 {
475 hdcHeight = dsect->GetHeight();
476 hdcWidth = dsect->GetWidth();
477 }
478 else
479 {
480 hdcHeight = pOS2bmp->cy;
481 hdcWidth = pOS2bmp->cx;
482 }
483 }
484
485 //win32 coordinates are relative to left top, OS/2 expects left bottom
486 //source rectangle is non-inclusive (top, right not included)
487 //destination rectangle is incl.-inclusive (everything included)
488
489 if(nXdest + nDestWidth > hdcWidth) {
490 nDestWidth = hdcWidth - nXdest;
491 }
492
493 if(nYdest + nDestHeight > hdcHeight) {
494 nDestHeight = hdcHeight - nYdest;
495 }
496
497 point[0].x = nXdest;
498 point[0].y = hdcHeight - nYdest - nDestHeight;
499 point[1].x = nXdest + nDestWidth - 1;
500 point[1].y = hdcHeight - nYdest - 1;
501
502 //target rectangle is inclusive-inclusive
503 if(nXsrc + nSrcWidth > pOS2bmp->cx) {
504 nSrcWidth = pOS2bmp->cx - nXsrc;
505 }
506 if(nYsrc + nSrcHeight > pOS2bmp->cy) {
507 nSrcHeight = pOS2bmp->cy - nYsrc;
508 }
509 point[2].x = nXsrc;
510 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
511 point[3].x = nXsrc + nSrcWidth;
512 point[3].y = pOS2bmp->cy - nYsrc;
513
514 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
515 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
516 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
517
518#ifdef INVERT
519 oldyinversion = GpiQueryYInversion(hps);
520 if(oldyinversion != 0) {
521 GpiEnableYInversion(hps, 0);
522 fRestoryYInversion = TRUE;
523 }
524#endif
525
526#ifdef DEBUG
527 RECTL rcltemp;
528 GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
529 dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
530#endif
531
532 if(fFlip & FLIP_HOR)
533 {
534 ULONG x;
535 x = point[0].x;
536 point[0].x = point[1].x;
537 point[1].x = x;
538 }
539
540 ULONG os2mode, winmode;
541
542 os2mode = BBO_OR;
543 winmode = GetStretchBltMode(hdcDest);
544 switch(winmode) {
545 case BLACKONWHITE_W:
546 os2mode = BBO_AND;
547 break;
548 case WHITEONBLACK_W:
549 case HALFTONE_W: //TODO:
550 os2mode = BBO_OR;
551 break;
552 case COLORONCOLOR_W:
553 os2mode = BBO_IGNORE;
554 break;
555 }
556 if(fFlip & FLIP_VERT) {
557 //manually reverse bitmap data
558 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
559 char *dst = bmpBitsDblBuffer;
560 for(int i=0;i<pOS2bmp->cy;i++) {
561 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
562 dst += dibinfo.dsBm.bmWidthBytes;
563 src -= dibinfo.dsBm.bmWidthBytes;
564 }
565 bitmapBits = bmpBitsDblBuffer;
566 }
567 else bitmapBits = bmpBits;
568
569 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
570 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
571 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
572
573 if(bmpBitsDblBuffer == NULL)
574 DebugInt3();
575
576 // PH 2000/10/01 - Fix for Beyond Compare 1.9d
577 // Note: according to documentation, cmImage can be zero for
578 // RGB- / non-compressed bitmaps.
579 int iLength = pOS2bmp->cbImage;
580 if (iLength == 0)
581 iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
582
583 if (iLength > 0)
584 {
585 if(CPUFeatures & CPUID_MMX)
586 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
587 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
588 }
589 else
590 {
591 dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
592 }
593
594
595 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
596 }
597 else rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
598
599 if(rc == GPI_OK) {
600 DIBSection *destdib = DIBSection::findHDC(hdcDest);
601 if(destdib) {
602 destdib->sync(hps, nYdest, nDestHeight);
603 }
604#ifdef INVERT
605 //restore old y inversion height
606 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
607#endif
608 SetLastError(ERROR_SUCCESS_W);
609
610 return(TRUE);
611 }
612#ifdef INVERT
613 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
614#endif
615
616 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));
617 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
618 return(FALSE);
619}
620//******************************************************************************
621int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
622 void *lpvBits, BITMAPINFO_W * lpbi, UINT uUsage);
623
624//******************************************************************************
625void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight)
626{
627 APIRET rc;
628 char *destBuf;
629
630 dprintf(("Sync destination dibsection %x (%x)", handle, hdc));
631
632#if 0
633 BITMAPINFO_W *tmphdr = (BITMAPINFO_W *)malloc(os2bmphdrsize + sizeof(BITMAPINFO_W));
634 tmphdr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER_W);
635 GetDIBits(hdc, handle, nYdest, nDestHeight, NULL, tmphdr, 0);
636 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
637 rc = GetDIBits(hdc, handle, nYdest, nDestHeight, destBuf, tmphdr, 0);
638#ifdef DEBUG_PALETTE
639 if(rc && tmphdr->bmiHeader.biBitCount <= 8) {
640 for(int i=0;i<(1<<tmphdr->bmiHeader.biBitCount);i++)
641 {
642 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->bmiColors[i])) ));
643 }
644 }
645#endif
646
647#else
648 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
649 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
650
651 if(fFlip & FLIP_VERT) {
652 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
653
654 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
655 tmphdr);
656 //manually reverse bitmap data
657 char *src = destBuf;
658 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
659 for(int i=0;i<nDestHeight;i++) {
660 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
661 dst -= dibinfo.dsBm.bmWidthBytes;
662 src += dibinfo.dsBm.bmWidthBytes;
663 }
664 }
665 else {
666 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
667 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
668 tmphdr);
669#ifdef DEBUG_PALETTE
670 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
671 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
672 {
673 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
674 }
675 }
676#endif
677 }
678#endif
679#if 0
680 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
681 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
682
683 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
684
685 if(CPUFeatures & CPUID_MMX) {
686 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
687 }
688 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
689 }
690#endif
691 free(tmphdr);
692 if(rc != nDestHeight) {
693 DebugInt3();
694 }
695}
696//******************************************************************************
697//manual sync if no stretching and bpp is the same
698//WARNING: this also assumes the colortables are the same
699//******************************************************************************
700void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
701{
702 char *srcbuf, *destbuf;
703 int linesize;
704
705 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
706 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
707 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
708 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
709 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
710 for(int i=0;i<heightDst;i++) {
711 memcpy(destbuf, srcbuf, linesize);
712 destbuf += dibinfo.dsBm.bmWidthBytes;
713 srcbuf += linesize;
714 }
715}
716//******************************************************************************
717//******************************************************************************
718void DIBSection::SelectDIBObject(HDC hdc)
719{
720 this->hdc = hdc;
721 hwndParent = WindowFromDC(hdc);
722 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
723}
724//******************************************************************************
725//******************************************************************************
726DIBSection *DIBSection::find(DWORD handle)
727{
728 DIBSection *dsect = section;
729
730 dibMutex.enter();
731 while(dsect)
732 {
733 if(dsect->handle == handle)
734 {
735 dibMutex.leave();
736 return(dsect);
737 }
738 dsect = dsect->next;
739 }
740 dibMutex.leave();
741 return(NULL);
742}
743//******************************************************************************
744//A bitmap can only be selected into one DC, so this works.
745//******************************************************************************
746DIBSection *DIBSection::findHDC(HDC hdc)
747{
748 DIBSection *dsect = section;
749
750 while(dsect)
751 {
752 if(dsect->hdc == hdc)
753 {
754 return(dsect);
755 }
756 dsect = dsect->next;
757 }
758 return(NULL);
759}
760//******************************************************************************
761//******************************************************************************
762void DIBSection::deleteSection(DWORD handle)
763{
764 DIBSection *dsect = find(handle);
765
766 if(dsect)
767 delete dsect;
768}
769//******************************************************************************
770//******************************************************************************
771int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
772{
773 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
774 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
775
776 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
777 if(iSize == sizeof(DIBSECTION))
778 {
779 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
780 return sizeof(DIBSECTION);
781 }
782 else
783 if(iSize == sizeof(BITMAP_W))
784 {
785 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
786 return sizeof(BITMAP_W);
787 }
788 return 0;
789
790}
791//******************************************************************************
792//******************************************************************************
793int DIBSection::GetBitCount()
794{
795 if(pOS2bmp == NULL)
796 return 0;
797 else
798 return pOS2bmp->cBitCount;
799}
800//******************************************************************************
801//******************************************************************************
802int DIBSection::GetHeight()
803{
804 if(pOS2bmp == NULL)
805 return 0;
806 else
807 return pOS2bmp->cy;
808}
809//******************************************************************************
810//******************************************************************************
811int DIBSection::GetWidth()
812{
813 if(pOS2bmp == NULL)
814 return 0;
815 else
816 return pOS2bmp->cx;
817}
818//******************************************************************************
819//******************************************************************************
820DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.