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

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

dibsection blit + setdibits fixes

File size: 17.3 KB
Line 
1/* $Id: dibsect.cpp,v 1.23 2000-03-25 12:19:07 sandervl Exp $ */
2
3/*
4 * GDI32 DIB sections
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 * Copyright 1998 Patrick Haller
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * NOTE:
12 * This is not a complete solution for CreateDIBSection, but enough for Quake 2!
13 *
14 */
15#define INCL_GPI
16#define INCL_WIN
17#include <os2wrap.h> //Odin32 OS/2 api wrappers
18#include <stdlib.h>
19#include <string.h>
20#include <win32type.h>
21#include <misc.h>
22#define OS2_ONLY
23#include "dibsect.h"
24#include <vmutex.h>
25#include <win32api.h>
26#include <winconst.h>
27#include <win32wnd.h>
28#include <cpuhlp.h>
29#include "oslibgpi.h"
30#include "rgbcvt.h"
31
32#define DBG_LOCALLOG DBG_dibsect
33#include "dbglocal.h"
34
35static VMutex dibMutex;
36
37//******************************************************************************
38//******************************************************************************
39DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
40 : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsRGB565(NULL)
41{
42 int os2bmpsize;
43
44 bmpsize = pbmi->biWidth;
45 /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
46
47 this->fFlip = fFlip;
48 os2bmpsize = sizeof(BITMAPINFO2);
49
50 switch(pbmi->biBitCount)
51 {
52 case 1:
53 bmpsize = ((bmpsize + 31) & ~31) / 8;
54 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
55 break;
56 case 4:
57 bmpsize = ((bmpsize + 7) & ~7) / 2;
58 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
59 break;
60 case 8:
61 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
62 bmpsize = (bmpsize + 3) & ~3;
63 break;
64 case 16:
65 bmpsize *= 2;
66 bmpsize = (bmpsize + 3) & ~3;
67 break;
68 case 24:
69 bmpsize *= 3;
70 bmpsize = (bmpsize + 3) & ~3;
71 break;
72 case 32:
73 bmpsize *= 4;
74 break;
75 default:
76 dprintf(("Unsupported nr of bits %d", pbmi->biBitCount));
77 DebugInt3();
78 break;
79 }
80
81 this->hSection = hSection;
82 if(hSection) {
83 bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight);
84 if(!bmpBits) {
85 dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
86 DebugInt3();
87 }
88 }
89 if(!bmpBits) {
90 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
91 }
92 memset(bmpBits, 0, bmpsize*pbmi->biHeight);
93
94 pOS2bmp = (BITMAPINFO2 *)malloc(os2bmpsize);
95
96 memset(pOS2bmp, /* set header + palette entries to zero */
97 0,
98 os2bmpsize);
99
100 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
101 pOS2bmp->cx = pbmi->biWidth;
102 pOS2bmp->cy = pbmi->biHeight;
103 pOS2bmp->cPlanes = pbmi->biPlanes;
104 pOS2bmp->cBitCount = pbmi->biBitCount;
105 pOS2bmp->ulCompression = pbmi->biCompression;
106 //SvL: Ignore BI_BITFIELDS type (GpiDrawBits fails otherwise)
107 if(pOS2bmp->ulCompression == BI_BITFIELDS) {
108 pOS2bmp->ulCompression = 0;
109 }
110 pOS2bmp->cbImage = pbmi->biSizeImage;
111 dprintf(("handle %x", handle));
112 dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
113 dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
114 dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
115 dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
116 dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
117 dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
118 dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
119
120 // clear DIBSECTION structure
121 memset(&dibinfo, 0, sizeof(dibinfo));
122
123 // copy BITMAPINFOHEADER data into DIBSECTION structure
124 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
125 dibinfo.dsBm.bmType = 0;
126 dibinfo.dsBm.bmWidth = pbmi->biWidth;
127 dibinfo.dsBm.bmHeight = pbmi->biHeight;
128 dibinfo.dsBm.bmWidthBytes= bmpsize;
129 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
130 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
131 dibinfo.dsBm.bmBits = bmpBits;
132
133 dibinfo.dshSection = handle;
134 dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
135
136 if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
137 {
138 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
139 }
140 else {
141 switch(pbmi->biBitCount)
142 {
143 case 16:
144 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0x7c00;
145 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0x03e0;
146 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0x001f;
147
148 //double buffer for rgb 555 dib sections (for conversion)
149 if(dibinfo.dsBitfields[1] == 0x03e0) {
150 DosAllocMem((PPVOID)&bmpBitsRGB565, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
151 }
152 break;
153
154 case 24:
155 dibinfo.dsBitfields[0] = 0xff;
156 dibinfo.dsBitfields[1] = 0xff00;
157 dibinfo.dsBitfields[2] = 0xff0000;
158 break;
159
160 case 32:
161 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0xff;
162 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0xff00;
163 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0xff0000;
164 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
165 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
166 }
167 break;
168 }
169 dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
170 }
171
172 this->handle = handle;
173 this->iUsage = iUsage;
174
175 dibMutex.enter();
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 /* @@@PH 98/07/11 fix for dsect->next == NULL */
187 while ( (dsect->next != this) &&
188 (dsect->next != NULL) )
189 {
190 dprintf2(("Increment section to %08X\n",dsect->next));
191 dsect = dsect->next;
192 }
193 dsect->next = this;
194 }
195 dibMutex.leave();
196}
197//******************************************************************************
198//******************************************************************************
199DIBSection::~DIBSection()
200{
201 dprintf(("Delete DIBSection %x", handle));
202
203 if(hSection) {
204 UnmapViewOfFile(bmpBits);
205 }
206 else
207 if(bmpBits)
208 DosFreeMem(bmpBits);
209
210 if(bmpBitsRGB565)
211 DosFreeMem(bmpBitsRGB565);
212
213 if(pOS2bmp)
214 free(pOS2bmp);
215
216 dibMutex.enter();
217 if(section == this)
218 {
219 section = this->next;
220 }
221 else
222 {
223 DIBSection *dsect = section;
224
225 while(dsect->next != this)
226 {
227 dsect = dsect->next;
228 }
229 dsect->next = this->next;
230 }
231 dibMutex.leave();
232}
233//******************************************************************************
234//******************************************************************************
235int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
236 lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
237 UINT coloruse)
238{
239 lines = (int)lines >= 0 ? (int)lines : (int)-lines;
240 int os2bmpsize;
241 int palsize=0;
242
243 bmpsize = pbmi->biWidth;
244 os2bmpsize = sizeof(BITMAPINFO2);
245
246 switch(pbmi->biBitCount)
247 {
248 case 1:
249 bmpsize = ((bmpsize + 31) & ~31) / 8;
250 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
251 os2bmpsize += palsize;
252 break;
253 case 4:
254 bmpsize = ((bmpsize + 7) & ~7) / 2;
255 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
256 os2bmpsize += palsize;
257 break;
258 case 8:
259 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
260 os2bmpsize += palsize;
261 bmpsize = (bmpsize + 3) & ~3;
262 break;
263 case 16:
264 bmpsize *= 2;
265 bmpsize = (bmpsize + 3) & ~3;
266 break;
267 case 24:
268 bmpsize *= 3;
269 bmpsize = (bmpsize + 3) & ~3;
270 break;
271 case 32:
272 bmpsize *= 4;
273 break;
274 }
275
276 //SvL: TODO: Correct??
277 if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
278 pOS2bmp->cBitCount != pbmi->biBitCount)
279 {
280 char *oldbits = bmpBits;
281 int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
282
283 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
284 memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
285 DosFreeMem(oldbits);
286 }
287 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmpsize);
288 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
289 pOS2bmp->cx = pbmi->biWidth;
290 pOS2bmp->cy = pbmi->biHeight;
291 pOS2bmp->cPlanes = pbmi->biPlanes;
292 pOS2bmp->cBitCount = pbmi->biBitCount;
293 pOS2bmp->ulCompression = pbmi->biCompression;
294 pOS2bmp->cbImage = pbmi->biSizeImage;
295
296 // clear DIBSECTION structure
297 memset(&dibinfo, 0, sizeof(dibinfo));
298
299 // copy BITMAPINFOHEADER data into DIBSECTION structure
300 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
301 dibinfo.dsBm.bmType = 0;
302 dibinfo.dsBm.bmWidth = pbmi->biWidth;
303 dibinfo.dsBm.bmHeight = pbmi->biHeight;
304 dibinfo.dsBm.bmWidthBytes= bmpsize;
305 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
306 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
307 dibinfo.dsBm.bmBits = bmpBits;
308
309 dibinfo.dshSection = hSection;
310 dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
311
312 if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
313 {
314 dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
315 }
316 else {
317 char *pColors = (char *)pbmi + 1;
318
319 switch(pbmi->biBitCount)
320 {
321 case 16:
322 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0x7c00;
323 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0x03e0;
324 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0x001f;
325 break;
326
327 case 24:
328 dibinfo.dsBitfields[0] = 0xff;
329 dibinfo.dsBitfields[1] = 0xff00;
330 dibinfo.dsBitfields[2] = 0xff0000;
331 break;
332
333 case 32:
334 dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0xff;
335 dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0xff00;
336 dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0xff0000;
337 if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
338 dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
339 }
340 break;
341 }
342 dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
343 }
344
345 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
346 if(palsize)
347 memcpy(pOS2bmp->argbColor, (char *)pbmi + 1 , palsize);
348
349 if(bits)
350 {
351 int size = bmpsize*lines;
352 memcpy(bmpBits+bmpsize*startscan, bits, size);
353 }
354 return(lines);
355}
356//******************************************************************************
357//******************************************************************************
358int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
359{
360 int i;
361
362 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
363 {
364 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
365 return(0);
366 }
367
368 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
369
370 for(i=startIdx;i<cEntries;i++)
371 {
372 pOS2bmp->argbColor[i].fcOptions = 0;
373 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
374 }
375
376 return(cEntries);
377}
378//******************************************************************************
379//******************************************************************************
380BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
381 int nDestHeight, int nXsrc, int nYsrc,
382 int nSrcWidth, int nSrcHeight, DWORD Rop)
383{
384 HPS hps = (HPS)hdcDest;
385 POINTL point[4];
386 LONG rc;
387 PVOID bitmapBits = NULL;
388
389 HWND hwndDest = WindowFromDC(hdcDest);
390 hwndDest = Win32ToOS2Handle(hwndDest);
391 if(hwndDest != 0)
392 {
393 hps = WinGetPS(hwndDest);
394 }
395 if(hps == 0)
396 {
397 dprintf(("ERROR: DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
398 return(FALSE);
399 }
400
401 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
402 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
403 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
404
405 point[0].x = nXdest;
406 point[0].y = nYdest;
407 point[1].x = nXdest + nDestWidth - 1;
408 point[1].y = nYdest + nDestHeight - 1;
409 point[2].x = nXsrc;
410 point[2].y = nYsrc;
411 if(nXsrc + nSrcWidth > pOS2bmp->cx)
412 {
413 point[3].x = pOS2bmp->cx;
414 }
415 else
416 point[3].x = nXsrc + nSrcWidth;
417
418 if(nYsrc + nSrcHeight > pOS2bmp->cy)
419 {
420 point[3].y = pOS2bmp->cy;
421 }
422 else
423 point[3].y = nYsrc + nSrcHeight;
424
425#if 1
426 if(fFlip & FLIP_VERT)
427 {
428 GpiEnableYInversion(hps, nDestHeight);
429 }
430
431 if(fFlip & FLIP_HOR)
432 {
433 ULONG x;
434 x = point[0].x;
435 point[0].x = point[1].x;
436 point[1].x = x;
437 }
438#endif
439
440 //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
441 if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
442 dprintf(("DIBSection::BitBlt; convert rgb 555 to 565"));
443
444 if(bmpBitsRGB565 == NULL)
445 DebugInt3();
446
447 if(CPUFeatures & CPUID_MMX) {
448 RGB555to565MMX((WORD *)bmpBitsRGB565, (WORD *)bmpBits, pOS2bmp->cbImage/sizeof(WORD));
449 }
450 else RGB555to565((WORD *)bmpBitsRGB565, (WORD *)bmpBits, pOS2bmp->cbImage/sizeof(WORD));
451 rc = GpiDrawBits(hps, bmpBitsRGB565, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
452 }
453 else rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
454
455 if(hwndDest != 0)
456 {
457 WinReleasePS(hps);
458 }
459 if(rc == GPI_OK) {
460 return(TRUE);
461 }
462 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));
463 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
464 return(FALSE);
465}
466//******************************************************************************
467//******************************************************************************
468void DIBSection::SelectDIBObject(HDC hdc)
469{
470 this->hdc = hdc;
471 hwndParent = WinWindowFromDC(hdc);
472 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
473}
474//******************************************************************************
475//******************************************************************************
476DIBSection *DIBSection::find(DWORD handle)
477{
478 DIBSection *dsect = section;
479
480 dibMutex.enter();
481 while(dsect)
482 {
483 if(dsect->handle == handle)
484 {
485 dibMutex.leave();
486 return(dsect);
487 }
488 dsect = dsect->next;
489 }
490 dibMutex.leave();
491 return(NULL);
492}
493//******************************************************************************
494//A bitmap can only be selected into one DC, so this works.
495//******************************************************************************
496DIBSection *DIBSection::findHDC(HDC hdc)
497{
498 DIBSection *dsect = section;
499
500 while(dsect)
501 {
502 if(dsect->hdc == hdc)
503 {
504 return(dsect);
505 }
506 dsect = dsect->next;
507 }
508 return(NULL);
509}
510//******************************************************************************
511//******************************************************************************
512void DIBSection::deleteSection(DWORD handle)
513{
514 DIBSection *dsect = find(handle);
515
516 if(dsect)
517 delete dsect;
518
519}
520//******************************************************************************
521//******************************************************************************
522int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
523{
524 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
525 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
526
527 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
528 if(iSize == sizeof(DIBSECTION))
529 {
530 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
531 return sizeof(DIBSECTION);
532 }
533 else
534 if(iSize == sizeof(BITMAP_W))
535 {
536 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
537 return sizeof(BITMAP_W);
538 }
539 return 0;
540
541}
542//******************************************************************************
543//******************************************************************************
544char DIBSection::GetBitCount()
545{
546 if(pOS2bmp == NULL)
547 return 0;
548 else
549 return pOS2bmp->cBitCount;
550}
551//******************************************************************************
552//******************************************************************************
553DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.