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

Last change on this file since 10088 was 10088, checked in by sandervl, 22 years ago

DIB section: do not fetch more colors from a bitmap header than biClrUsed (if ..= 0); GetDIBits: save and restore negative height & log failure

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