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

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

update

File size: 29.0 KB
Line 
1/* $Id: dibsect.cpp,v 1.63 2002-04-30 13:11:44 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 rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], Rop, os2mode);
612 }
613 else {
614 rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], Rop, os2mode);
615 }
616 if(rc == GPI_OK) {
617 DIBSection *destdib = DIBSection::findHDC(hdcDest);
618 if(destdib) {
619 destdib->sync(hps, nYdest, nDestHeight, FALSE);
620 }
621#ifdef INVERT
622 //restore old y inversion height
623 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
624#endif
625 SetLastError(ERROR_SUCCESS_W);
626
627 return(TRUE);
628 }
629#ifdef INVERT
630 if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
631#endif
632
633 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));
634 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
635 return(FALSE);
636}
637//******************************************************************************
638//******************************************************************************
639void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
640{
641 APIRET rc;
642 char *destBuf;
643
644 dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
645
646 BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
647 memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
648
649#ifdef INVERT
650 int oldyinversion = 0;
651 if(orgYInversion == TRUE) {
652 oldyinversion = GpiQueryYInversion(hdc);
653 dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
654 if(oldyinversion != 0) {
655#ifdef DEBUG
656 POINT point;
657 GetViewportOrgEx(hdc, &point);
658 dprintf(("Viewport origin (%d,%d)", point.x, point.y));
659#endif
660 GpiEnableYInversion(hdc, 0);
661 }
662 }
663#endif
664
665 if(fFlip & FLIP_VERT) {
666 destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
667
668 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
669 // will fail in that case (CoolEdit 2000). Perhaps because the returned
670 // compressed image is larger than the original.
671 // Use uncompressed size instead
672 // NOTE: The correct size will be returned by GpiQueryBitmapBits
673 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
674
675 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
676 tmphdr);
677 if(rc == GPI_ALTERROR) {
678 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
679 }
680
681 //manually reverse bitmap data
682 char *src = destBuf;
683 char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
684 for(int i=0;i<nDestHeight;i++) {
685 memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
686 dst -= dibinfo.dsBm.bmWidthBytes;
687 src += dibinfo.dsBm.bmWidthBytes;
688 }
689 }
690 else {
691 //SvL: cbImage can be too small for compressed images; GpiQueryBitmapBits
692 // will fail in that case (CoolEdit 2000). Perhaps because the returned
693 // compressed image is larger than the original.
694 // Use uncompressed size instead
695 // NOTE: The correct size will be returned by GpiQueryBitmapBits
696 tmphdr->cbImage = dibinfo.dsBm.bmHeight*dibinfo.dsBm.bmWidthBytes;
697
698 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
699 rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
700 tmphdr);
701 if(rc == GPI_ALTERROR) {
702 dprintf(("ERROR: GpiQueryBitmapBits failed with %x", WinGetLastError(0)));
703 }
704#ifdef DEBUG_PALETTE
705 if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
706 for(int i=0;i<(1<<tmphdr->cBitCount);i++)
707 {
708 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
709 }
710 }
711#endif
712 }
713 memcpy(pOS2bmp, tmphdr, os2bmphdrsize);
714
715 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
716 dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
717
718 destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
719
720 if(CPUFeatures & CPUID_MMX) {
721 RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
722 }
723 else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
724 }
725
726 free(tmphdr);
727 if(rc != nDestHeight) {
728 dprintf(("!WARNING!: GpiQueryBitmapBits returned %d instead of %d scanlines", rc, nDestHeight));
729 }
730
731#ifdef INVERT
732 if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
733#endif
734
735}
736//******************************************************************************
737//manual sync if no stretching and bpp is the same
738//WARNING: this also assumes the colortables are the same
739//******************************************************************************
740void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
741{
742 char *srcbuf, *destbuf;
743 int linesize;
744
745 srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
746 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
747 destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
748 (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
749 linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
750 for(int i=0;i<heightDst;i++) {
751 memcpy(destbuf, srcbuf, linesize);
752 destbuf += dibinfo.dsBm.bmWidthBytes;
753 srcbuf += linesize;
754 }
755}
756//******************************************************************************
757//******************************************************************************
758void DIBSection::SelectDIBObject(HDC hdc)
759{
760 this->hdc = hdc;
761 hwndParent = WindowFromDC(hdc);
762 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
763}
764//******************************************************************************
765//******************************************************************************
766DIBSection *DIBSection::findObj(HANDLE handle)
767{
768 // PH 2001-08-18 shortcut for performance optimization
769 if (!section)
770 return NULL;
771
772 DIBSection *dsect = section;
773 lock();
774
775 do
776 {
777 if(dsect->handle == handle)
778 {
779 unlock();
780 return(dsect);
781 }
782 dsect = dsect->next;
783 }
784 while(dsect);
785
786 unlock();
787 return(NULL);
788}
789//******************************************************************************
790//A bitmap can only be selected into one DC, so this works.
791//******************************************************************************
792DIBSection *DIBSection::findHDC(HDC hdc)
793{
794 // PH 2001-08-18 shortcut for performance optimization
795 if (!section)
796 return NULL;
797
798 DIBSection *dsect = section;
799
800 lock();
801 do
802 {
803 if(dsect->hdc == hdc)
804 {
805 unlock();
806 return(dsect);
807 }
808 dsect = dsect->next;
809 }
810 while(dsect);
811
812 unlock();
813 return(NULL);
814}
815//******************************************************************************
816//******************************************************************************
817void DIBSection::deleteSection(HANDLE handle)
818{
819 DIBSection *dsect = findObj(handle);
820
821 if(dsect)
822 delete dsect;
823}
824//******************************************************************************
825//******************************************************************************
826int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
827{
828 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
829 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
830
831 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
832 if(iSize == sizeof(DIBSECTION))
833 {
834 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
835 return sizeof(DIBSECTION);
836 }
837 else
838 if(iSize == sizeof(BITMAP_W))
839 {
840 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
841 return sizeof(BITMAP_W);
842 }
843 return 0;
844
845}
846//******************************************************************************
847//******************************************************************************
848int DIBSection::GetBitCount()
849{
850 if(pOS2bmp == NULL)
851 return 0;
852 else
853 return pOS2bmp->cBitCount;
854}
855//******************************************************************************
856//******************************************************************************
857int DIBSection::GetHeight()
858{
859 if(pOS2bmp == NULL)
860 return 0;
861 else
862 return pOS2bmp->cy;
863}
864//******************************************************************************
865//******************************************************************************
866int DIBSection::GetWidth()
867{
868 if(pOS2bmp == NULL)
869 return 0;
870 else
871 return pOS2bmp->cx;
872}
873//******************************************************************************
874//******************************************************************************
875void DIBSection::initDIBSection()
876{
877 InitializeCriticalSection(&dibcritsect);
878}
879//******************************************************************************
880//******************************************************************************
881DIBSection *DIBSection::section = NULL;
882CRITICAL_SECTION DIBSection::dibcritsect;
Note: See TracBrowser for help on using the repository browser.