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

Last change on this file since 7717 was 7717, checked in by sandervl, 24 years ago

use critical section in dib section class instead of vmutex

File size: 29.0 KB
Line 
1/* $Id: dibsect.cpp,v 1.62 2001-12-31 12:08:20 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_PALETTE
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 }
422
423 return end - startIdx;
424}
425//******************************************************************************
426//******************************************************************************
427int DIBSection::GetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
428{
429 int i, end = startIdx + cEntries;
430
431 if(pOS2bmp->cBitCount > 8) {
432 dprintf(("DIBSection::GetDIBColorTable: bpp > 8 -> return 0"));
433 return 0;
434 }
435 if(end > (1 << pOS2bmp->cBitCount)) {
436 end = (1 << pOS2bmp->cBitCount);
437 dprintf(("DIBSection::GetDIBColorTable: %d->%d", startIdx, end));
438 }
439 memcpy(rgb, &pOS2bmp->argbColor[startIdx], cEntries*sizeof(RGBQUAD));
440
441 for(i=0;i<cEntries;i++) {
442 rgb[i].rgbReserved = 0;
443 }
444
445 return end - startIdx;
446}
447//******************************************************************************
448//******************************************************************************
449BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
450 int nDestHeight, int nXsrc, int nYsrc,
451 int nSrcWidth, int nSrcHeight, DWORD Rop)
452{
453 HPS hps = (HPS)hdcDest;
454 POINTL point[4];
455 LONG rc, hdcHeight, hdcWidth;
456 PVOID bitmapBits = NULL;
457 int oldyinversion = 0;
458 BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
459 HWND hwndDest;
460 pDCData pHps;
461
462 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
463 if(!pHps)
464 {
465 SetLastError(ERROR_INVALID_HANDLE_W);
466 return FALSE;
467 }
468
469 hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
470 if(hwndDest && pHps->hwnd && !pHps->isClient) {
471 fFrameWindowDC = TRUE;
472 }
473
474 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
475 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
476 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
477
478 if(hwndDest) {
479 RECT rect;
480
481 if(fFrameWindowDC) {
482 GetWindowRect(hwndDest, &rect);
483 }
484 else GetClientRect(hwndDest, &rect);
485 hdcHeight = rect.bottom - rect.top;
486 hdcWidth = rect.right - rect.left;
487 dprintf(("DIBSection::BitBlt hdc size (%d,%d) (WINDOW)", hdcWidth, hdcHeight));
488 }
489 else {
490 hdcHeight = pHps->bitmapHeight;
491 hdcWidth = pHps->bitmapWidth;
492 dprintf(("DIBSection::BitBlt hdc size (%d,%d) (BMP)", hdcWidth, hdcHeight));
493 }
494
495 //Don't clip destination size to destination DC size
496 //This messes up the two bitmaps in the opening window of Opera 6
497 //(choice between MDI & SDI interface)
498
499 //win32 coordinates are relative to left top, OS/2 expects left bottom
500 //source rectangle is non-inclusive (top, right not included)
501 //destination rectangle is incl.-inclusive (everything included)
502
503 point[0].x = nXdest;
504 point[1].x = nXdest + nDestWidth - 1;
505#ifdef INVERT
506 point[0].y = hdcHeight - nYdest - nDestHeight;
507 point[1].y = hdcHeight - nYdest - 1;
508#else
509 point[0].y = nYdest;
510 point[1].y = nYdest + nDestHeight - 1;
511#endif
512
513 //target rectangle is inclusive-inclusive
514 point[2].x = nXsrc;
515 point[3].x = nXsrc + nSrcWidth;
516#ifdef INVERT
517 point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
518 point[3].y = pOS2bmp->cy - nYsrc;
519#else
520 point[2].y = nYsrc;
521 point[3].y = nYsrc + nSrcHeight - 1;
522#endif
523
524 dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
525 point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
526 nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
527
528#ifdef INVERT
529 oldyinversion = GpiQueryYInversion(hps);
530 if(oldyinversion != 0) {
531#ifdef DEBUG
532 POINT point;
533 GetViewportOrgEx(hps, &point);
534 dprintf(("Viewport origin (%d,%d)", point.x, point.y));
535#endif
536 GpiEnableYInversion(hps, 0);
537 fRestoryYInversion = TRUE;
538 }
539#endif
540
541#ifdef DEBUG
542 RECTL rcltemp;
543 GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
544 dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
545#endif
546
547 if(fFlip & FLIP_HOR)
548 {
549 ULONG x;
550 x = point[0].x;
551 point[0].x = point[1].x;
552 point[1].x = x;
553 }
554
555 ULONG os2mode, winmode;
556
557 os2mode = BBO_OR;
558 winmode = GetStretchBltMode(hdcDest);
559 switch(winmode) {
560 case BLACKONWHITE_W:
561 os2mode = BBO_AND;
562 break;
563 case WHITEONBLACK_W:
564 case HALFTONE_W: //TODO:
565 os2mode = BBO_OR;
566 break;
567 case COLORONCOLOR_W:
568 os2mode = BBO_IGNORE;
569 break;
570 }
571 if(fFlip & FLIP_VERT) {
572 //manually reverse bitmap data
573 char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
574 char *dst = bmpBitsDblBuffer;
575 for(int i=0;i<pOS2bmp->cy;i++) {
576 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
577 dst += dibinfo.dsBm.bmWidthBytes;
578 src -= dibinfo.dsBm.bmWidthBytes;
579 }
580 bitmapBits = bmpBitsDblBuffer;
581 }
582 else bitmapBits = bmpBits;
583
584 //Translate ROP
585 Rop = Rop >> 16;
586
587 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
588 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
589 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
590
591 if(bmpBitsDblBuffer == NULL)
592 DebugInt3();
593
594 // PH 2000/10/01 - Fix for Beyond Compare 1.9d
595 // Note: according to documentation, cmImage can be zero for
596 // RGB- / non-compressed bitmaps.
597 int iLength = pOS2bmp->cbImage;
598 if (iLength == 0)
599 iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
600
601 if (iLength > 0)
602 {
603 if(CPUFeatures & CPUID_MMX)
604 RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
605 else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
606 }
607 else
608 {
609 dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
610 }
611
612
613 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], Rop, os2mode);
614 }
615 else {
616 rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], Rop, os2mode);
617 }
618 if(rc == GPI_OK) {
619 DIBSection *destdib = DIBSection::findHDC(hdcDest);
620 if(destdib) {
621 destdib->sync(hps, nYdest, nDestHeight, FALSE);
622 }
623#ifdef INVERT
624 //restore old y inversion height
625 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
626#endif
627 SetLastError(ERROR_SUCCESS_W);
628
629 return(TRUE);
630 }
631#ifdef INVERT
632 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
633#endif
634
635 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));
636 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
637 return(FALSE);
638}
639//******************************************************************************
640//******************************************************************************
641void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
642{
643 APIRET rc;
644 char *destBuf;
645
646 dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
647
648 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
649 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
650
651#ifdef INVERT
652 int oldyinversion = 0;
653 if(orgYInversion == TRUE) {
654 oldyinversion = GpiQueryYInversion(hdc);
655 dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
656 if(oldyinversion != 0) {
657#ifdef DEBUG
658 POINT point;
659 GetViewportOrgEx(hdc, &point);
660 dprintf(("Viewport origin (%d,%d)", point.x, point.y));
661#endif
662 GpiEnableYInversion(hdc, 0);
663 }
664 }
665#endif
666
667 if(fFlip & FLIP_VERT) {
668 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
669
670 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
671 // will fail in that case (CoolEdit 2000). Perhaps because the returned
672 // compressed image is larger than the original.
673 // Use uncompressed size instead
674 // NOTE: The correct size will be returned by GpiQueryBitmapBits
675 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
676
677 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
678 tmphdr);
679 if(rc == GPI_ALTERROR) {
680 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
681 }
682
683 //manually reverse bitmap data
684 char *src = destBuf;
685 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
686 for(int i=0;i<nDestHeight;i++) {
687 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
688 dst -= dibinfo.dsBm.bmWidthBytes;
689 src += dibinfo.dsBm.bmWidthBytes;
690 }
691 }
692 else {
693 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
694 // will fail in that case (CoolEdit 2000). Perhaps because the returned
695 // compressed image is larger than the original.
696 // Use uncompressed size instead
697 // NOTE: The correct size will be returned by GpiQueryBitmapBits
698 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
699
700 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
701 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
702 tmphdr);
703 if(rc == GPI_ALTERROR) {
704 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
705 }
706#ifdef DEBUG_PALETTE
707 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
708 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
709 {
710 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
711 }
712 }
713#endif
714 }
715 memcpy(pOS2bmp, tmphdr, os2bmphdrsize);
716
717 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
718 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
719
720 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
721
722 if(CPUFeatures & CPUID_MMX) {
723 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
724 }
725 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
726 }
727
728 free(tmphdr);
729 if(rc != nDestHeight) {
730 DebugInt3();
731 }
732
733#ifdef INVERT
734 if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
735#endif
736
737}
738//******************************************************************************
739//manual sync if no stretching and bpp is the same
740//WARNING: this also assumes the colortables are the same
741//******************************************************************************
742void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
743{
744 char *srcbuf, *destbuf;
745 int linesize;
746
747 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
748 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
749 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
750 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
751 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
752 for(int i=0;i<heightDst;i++) {
753 memcpy(destbuf, srcbuf, linesize);
754 destbuf += dibinfo.dsBm.bmWidthBytes;
755 srcbuf += linesize;
756 }
757}
758//******************************************************************************
759//******************************************************************************
760void DIBSection::SelectDIBObject(HDC hdc)
761{
762 this->hdc = hdc;
763 hwndParent = WindowFromDC(hdc);
764 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
765}
766//******************************************************************************
767//******************************************************************************
768DIBSection *DIBSection::findObj(HANDLE handle)
769{
770 // PH 2001-08-18 shortcut for performance optimization
771 if (!section)
772 return NULL;
773
774 DIBSection *dsect = section;
775 lock();
776
777 do
778 {
779 if(dsect->handle == handle)
780 {
781 unlock();
782 return(dsect);
783 }
784 dsect = dsect->next;
785 }
786 while(dsect);
787
788 unlock();
789 return(NULL);
790}
791//******************************************************************************
792//A bitmap can only be selected into one DC, so this works.
793//******************************************************************************
794DIBSection *DIBSection::findHDC(HDC hdc)
795{
796 // PH 2001-08-18 shortcut for performance optimization
797 if (!section)
798 return NULL;
799
800 DIBSection *dsect = section;
801
802 lock();
803 do
804 {
805 if(dsect->hdc == hdc)
806 {
807 unlock();
808 return(dsect);
809 }
810 dsect = dsect->next;
811 }
812 while(dsect);
813
814 unlock();
815 return(NULL);
816}
817//******************************************************************************
818//******************************************************************************
819void DIBSection::deleteSection(HANDLE handle)
820{
821 DIBSection *dsect = findObj(handle);
822
823 if(dsect)
824 delete dsect;
825}
826//******************************************************************************
827//******************************************************************************
828int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
829{
830 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
831 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
832
833 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
834 if(iSize == sizeof(DIBSECTION))
835 {
836 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
837 return sizeof(DIBSECTION);
838 }
839 else
840 if(iSize == sizeof(BITMAP_W))
841 {
842 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
843 return sizeof(BITMAP_W);
844 }
845 return 0;
846
847}
848//******************************************************************************
849//******************************************************************************
850int DIBSection::GetBitCount()
851{
852 if(pOS2bmp == NULL)
853 return 0;
854 else
855 return pOS2bmp->cBitCount;
856}
857//******************************************************************************
858//******************************************************************************
859int DIBSection::GetHeight()
860{
861 if(pOS2bmp == NULL)
862 return 0;
863 else
864 return pOS2bmp->cy;
865}
866//******************************************************************************
867//******************************************************************************
868int DIBSection::GetWidth()
869{
870 if(pOS2bmp == NULL)
871 return 0;
872 else
873 return pOS2bmp->cx;
874}
875//******************************************************************************
876//******************************************************************************
877void DIBSection::initDIBSection()
878{
879 InitializeCriticalSection(&dibcritsect);
880}
881//******************************************************************************
882//******************************************************************************
883DIBSection *DIBSection::section = NULL;
884CRITICAL_SECTION DIBSection::dibcritsect;
Note: See TracBrowser for help on using the repository browser.