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

Last change on this file since 2853 was 2853, checked in by sandervl, 26 years ago

* empty log message *

File size: 14.3 KB
Line 
1/* $Id: dibsect.cpp,v 1.20 2000-02-21 20:26:45 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 <winconst.h>
26#include <win32wnd.h>
27#include "oslibgpi.h"
28
29#define DBG_LOCALLOG DBG_dibsect
30#include "dbglocal.h"
31
32//Win32 apis used:
33HWND WIN32API WindowFromDC(HDC hdc);
34BOOL WINAPI UnmapViewOfFile(LPVOID addr);
35LPVOID WINAPI MapViewOfFile(HANDLE mapping, DWORD access, DWORD offset_high,
36 DWORD offset_low, DWORD count);
37
38static VMutex dibMutex;
39
40//******************************************************************************
41//******************************************************************************
42DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
43 : bmpBits(NULL), pOS2bmp(NULL), next(NULL)
44{
45 int os2bmpsize;
46
47 bmpsize = pbmi->biWidth;
48 /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
49
50 this->fFlip = fFlip;
51 os2bmpsize = sizeof(BITMAPINFO2);
52
53 switch(pbmi->biBitCount)
54 {
55 case 1:
56 bmpsize = ((bmpsize + 31) & ~31) / 8;
57 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
58 break;
59 case 4:
60 bmpsize = ((bmpsize + 7) & ~7) / 2;
61 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
62 break;
63 case 8:
64 os2bmpsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
65 bmpsize = (bmpsize + 3) & ~3;
66 break;
67 case 16:
68 bmpsize *= 2;
69 bmpsize = (bmpsize + 3) & ~3;
70 break;
71 case 24:
72 bmpsize *= 3;
73 bmpsize = (bmpsize + 3) & ~3;
74 break;
75 case 32:
76 bmpsize *= 4;
77 break;
78 }
79
80 this->hSection = hSection;
81 if(hSection) {
82 bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight);
83 if(!bmpBits) {
84 dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
85 DebugInt3();
86 }
87 }
88 if(!bmpBits) {
89 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
90 }
91 memset(bmpBits, 0, bmpsize*pbmi->biHeight);
92
93 pOS2bmp = (BITMAPINFO2 *)malloc(os2bmpsize);
94
95 memset(pOS2bmp, /* set header + palette entries to zero */
96 0,
97 os2bmpsize);
98
99 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
100 pOS2bmp->cx = pbmi->biWidth;
101 pOS2bmp->cy = pbmi->biHeight;
102 pOS2bmp->cPlanes = pbmi->biPlanes;
103 pOS2bmp->cBitCount = pbmi->biBitCount;
104 pOS2bmp->ulCompression = pbmi->biCompression;
105 //SvL: Ignore BI_BITFIELDS type (GpiDrawBits fails otherwise)
106 if(pOS2bmp->ulCompression == BI_BITFIELDS) {
107 pOS2bmp->ulCompression = 0;
108 }
109 pOS2bmp->cbImage = pbmi->biSizeImage;
110 dprintf(("handle %x", handle));
111 dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
112 dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
113 dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
114 dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
115 dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
116 dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
117 dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
118
119 // clear DIBSECTION structure
120 memset(&dibinfo, 0, sizeof(dibinfo));
121
122 // copy BITMAPINFOHEADER data into DIBSECTION structure
123 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
124 dibinfo.dsBm.bmType = 0;
125 dibinfo.dsBm.bmWidth = pbmi->biWidth;
126 dibinfo.dsBm.bmHeight = pbmi->biHeight;
127 dibinfo.dsBm.bmWidthBytes= bmpsize;
128 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
129 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
130 dibinfo.dsBm.bmBits = bmpBits;
131
132 dibinfo.dshSection = handle;
133 dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
134
135 if(pbmi->biCompression == BI_BITFIELDS) {
136 dibinfo.dsBitfields[0] = *((DWORD *)pColors);
137 dibinfo.dsBitfields[1] = *((DWORD *)pColors+1);
138 dibinfo.dsBitfields[2] = *((DWORD *)pColors+2);
139 dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
140 }
141
142 this->handle = handle;
143 this->iUsage = iUsage;
144
145 dibMutex.enter();
146 if(section == NULL)
147 {
148 dprintf(("section was NULL\n"));
149 section = this;
150 }
151 else
152 {
153 DIBSection *dsect = section;
154 dprintf2(("Increment section starting at %08X\n",dsect));
155
156 /* @@@PH 98/07/11 fix for dsect->next == NULL */
157 while ( (dsect->next != this) &&
158 (dsect->next != NULL) )
159 {
160 dprintf2(("Increment section to %08X\n",dsect->next));
161 dsect = dsect->next;
162 }
163 dsect->next = this;
164 }
165 dibMutex.leave();
166}
167//******************************************************************************
168//******************************************************************************
169DIBSection::~DIBSection()
170{
171 dprintf(("Delete DIBSection %x", handle));
172
173 if(hSection) {
174 UnmapViewOfFile(bmpBits);
175 }
176 else
177 if(bmpBits)
178 DosFreeMem(bmpBits);
179
180 if(pOS2bmp)
181 free(pOS2bmp);
182
183 dibMutex.enter();
184 if(section == this)
185 {
186 section = this->next;
187 }
188 else
189 {
190 DIBSection *dsect = section;
191
192 while(dsect->next != this)
193 {
194 dsect = dsect->next;
195 }
196 dsect->next = this->next;
197 }
198 dibMutex.leave();
199}
200//******************************************************************************
201//******************************************************************************
202int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
203 lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
204 UINT coloruse)
205{
206 lines = (int)lines >= 0 ? (int)lines : (int)-lines;
207 int os2bmpsize;
208 int palsize=0;
209
210 bmpsize = pbmi->biWidth;
211 os2bmpsize = sizeof(BITMAPINFO2);
212
213 switch(pbmi->biBitCount)
214 {
215 case 1:
216 bmpsize = ((bmpsize + 31) & ~31) / 8;
217 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
218 os2bmpsize += palsize;
219 break;
220 case 4:
221 bmpsize = ((bmpsize + 7) & ~7) / 2;
222 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
223 os2bmpsize += palsize;
224 break;
225 case 8:
226 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
227 os2bmpsize += palsize;
228 bmpsize = (bmpsize + 3) & ~3;
229 break;
230 case 16:
231 bmpsize *= 2;
232 bmpsize = (bmpsize + 3) & ~3;
233 break;
234 case 24:
235 bmpsize *= 3;
236 bmpsize = (bmpsize + 3) & ~3;
237 break;
238 case 32:
239 bmpsize *= 4;
240 break;
241 }
242
243 //SvL: TODO: Correct??
244 if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
245 pOS2bmp->cBitCount != pbmi->biBitCount)
246 {
247 char *oldbits = bmpBits;
248 int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
249
250 DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
251 memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
252 DosFreeMem(oldbits);
253 }
254 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmpsize);
255 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
256 pOS2bmp->cx = pbmi->biWidth;
257 pOS2bmp->cy = pbmi->biHeight;
258 pOS2bmp->cPlanes = pbmi->biPlanes;
259 pOS2bmp->cBitCount = pbmi->biBitCount;
260 pOS2bmp->ulCompression = pbmi->biCompression;
261 pOS2bmp->cbImage = pbmi->biSizeImage;
262
263 // clear DIBSECTION structure
264 memset(&dibinfo, 0, sizeof(dibinfo));
265
266 // copy BITMAPINFOHEADER data into DIBSECTION structure
267 memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
268 dibinfo.dsBm.bmType = 0;
269 dibinfo.dsBm.bmWidth = pbmi->biWidth;
270 dibinfo.dsBm.bmHeight = pbmi->biHeight;
271 dibinfo.dsBm.bmWidthBytes= bmpsize;
272 dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
273 dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
274 dibinfo.dsBm.bmBits = bmpBits;
275
276 dibinfo.dshSection = hSection;
277 dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
278
279 if(pbmi->biCompression == BI_BITFIELDS)
280 {
281 char *pColors = (char *)pbmi + 1;
282
283 dibinfo.dsBitfields[0] = *((DWORD *)pColors);
284 dibinfo.dsBitfields[1] = *((DWORD *)pColors+1);
285 dibinfo.dsBitfields[2] = *((DWORD *)pColors+2);
286 dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
287 }
288
289 dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
290 if(palsize)
291 memcpy(pOS2bmp->argbColor, (char *)pbmi + 1 , palsize);
292
293 if(bits)
294 {
295 int size = bmpsize*lines;
296 memcpy(bmpBits+bmpsize*startscan, bits, size);
297 }
298 return(lines);
299}
300//******************************************************************************
301//******************************************************************************
302int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
303{
304 int i;
305
306 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
307 {
308 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
309 return(0);
310 }
311
312 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
313
314 for(i=startIdx;i<cEntries;i++)
315 {
316 pOS2bmp->argbColor[i].fcOptions = 0;
317 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
318 }
319
320 return(cEntries);
321}
322//******************************************************************************
323//******************************************************************************
324BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
325 int nDestHeight, int nXsrc, int nYsrc,
326 int nSrcWidth, int nSrcHeight, DWORD Rop)
327{
328 HPS hps = (HPS)hdcDest;
329 POINTL point[4];
330 LONG rc;
331
332 HWND hwndDest = WindowFromDC(hdcDest);
333 hwndDest = Win32ToOS2Handle(hwndDest);
334 if(hwndDest != 0)
335 {
336 hps = WinGetPS(hwndDest);
337 }
338 if(hps == 0)
339 {
340 dprintf(("ERROR: DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
341 return(FALSE);
342 }
343
344 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
345 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
346 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
347
348 point[0].x = nXdest;
349 point[0].y = nYdest;
350 point[1].x = nXdest + nDestWidth - 1;
351 point[1].y = nYdest + nDestHeight - 1;
352 point[2].x = nXsrc;
353 point[2].y = nYsrc;
354 if(nXsrc + nSrcWidth > pOS2bmp->cx)
355 {
356 point[3].x = pOS2bmp->cx;
357 }
358 else
359 point[3].x = nXsrc + nSrcWidth;
360
361 if(nYsrc + nSrcHeight > pOS2bmp->cy)
362 {
363 point[3].y = pOS2bmp->cy;
364 }
365 else
366 point[3].y = nYsrc + nSrcHeight;
367
368#if 1
369 if(fFlip & FLIP_VERT)
370 {
371 GpiEnableYInversion(hps, nDestHeight);
372 }
373
374 if(fFlip & FLIP_HOR)
375 {
376 ULONG x;
377 x = point[0].x;
378 point[0].x = point[1].x;
379 point[1].x = x;
380 }
381#endif
382
383 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
384
385 if(hwndDest != 0)
386 {
387 WinReleasePS(hps);
388 }
389 if(rc == GPI_OK) {
390 return(TRUE);
391 }
392 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));
393 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
394 return(FALSE);
395}
396//******************************************************************************
397//******************************************************************************
398void DIBSection::SelectDIBObject(HDC hdc)
399{
400 this->hdc = hdc;
401 hwndParent = WinWindowFromDC(hdc);
402 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
403}
404//******************************************************************************
405//******************************************************************************
406DIBSection *DIBSection::find(DWORD handle)
407{
408 DIBSection *dsect = section;
409
410 dibMutex.enter();
411 while(dsect)
412 {
413 if(dsect->handle == handle)
414 {
415 dibMutex.leave();
416 return(dsect);
417 }
418 dsect = dsect->next;
419 }
420 dibMutex.leave();
421 return(NULL);
422}
423//******************************************************************************
424//A bitmap can only be selected into one DC, so this works.
425//******************************************************************************
426DIBSection *DIBSection::findHDC(HDC hdc)
427{
428 DIBSection *dsect = section;
429
430 while(dsect)
431 {
432 if(dsect->hdc == hdc)
433 {
434 return(dsect);
435 }
436 dsect = dsect->next;
437 }
438 return(NULL);
439}
440//******************************************************************************
441//******************************************************************************
442void DIBSection::deleteSection(DWORD handle)
443{
444 DIBSection *dsect = find(handle);
445
446 if(dsect)
447 delete dsect;
448
449}
450//******************************************************************************
451//******************************************************************************
452int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
453{
454 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
455 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
456
457 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
458 if(iSize == sizeof(DIBSECTION))
459 {
460 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
461 return sizeof(DIBSECTION);
462 }
463 else
464 if(iSize == sizeof(BITMAP_W))
465 {
466 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
467 return sizeof(BITMAP_W);
468 }
469 return 0;
470
471}
472//******************************************************************************
473//******************************************************************************
474char DIBSection::GetBitCount()
475{
476 if(pOS2bmp == NULL)
477 return 0;
478 else
479 return pOS2bmp->cBitCount;
480}
481//******************************************************************************
482//******************************************************************************
483DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.